main.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved
  4. */
  5. #include <linux/sizes.h>
  6. #include <linux/vfio_pci_core.h>
  7. /*
  8. * The device memory usable to the workloads running in the VM is cached
  9. * and showcased as a 64b device BAR (comprising of BAR4 and BAR5 region)
  10. * to the VM and is represented as usemem.
  11. * Moreover, the VM GPU device driver needs a non-cacheable region to
  12. * support the MIG feature. This region is also exposed as a 64b BAR
  13. * (comprising of BAR2 and BAR3 region) and represented as resmem.
  14. */
  15. #define RESMEM_REGION_INDEX VFIO_PCI_BAR2_REGION_INDEX
  16. #define USEMEM_REGION_INDEX VFIO_PCI_BAR4_REGION_INDEX
  17. /* A hardwired and constant ABI value between the GPU FW and VFIO driver. */
  18. #define MEMBLK_SIZE SZ_512M
  19. #define DVSEC_BITMAP_OFFSET 0xA
  20. #define MIG_SUPPORTED_WITH_CACHED_RESMEM BIT(0)
  21. #define GPU_CAP_DVSEC_REGISTER 3
  22. /*
  23. * The state of the two device memory region - resmem and usemem - is
  24. * saved as struct mem_region.
  25. */
  26. struct mem_region {
  27. phys_addr_t memphys; /* Base physical address of the region */
  28. size_t memlength; /* Region size */
  29. size_t bar_size; /* Reported region BAR size */
  30. __le64 bar_val; /* Emulated BAR offset registers */
  31. union {
  32. void *memaddr;
  33. void __iomem *ioaddr;
  34. }; /* Base virtual address of the region */
  35. };
  36. struct nvgrace_gpu_pci_core_device {
  37. struct vfio_pci_core_device core_device;
  38. /* Cached and usable memory for the VM. */
  39. struct mem_region usemem;
  40. /* Non cached memory carved out from the end of device memory */
  41. struct mem_region resmem;
  42. /* Lock to control device memory kernel mapping */
  43. struct mutex remap_lock;
  44. bool has_mig_hw_bug;
  45. };
  46. static void nvgrace_gpu_init_fake_bar_emu_regs(struct vfio_device *core_vdev)
  47. {
  48. struct nvgrace_gpu_pci_core_device *nvdev =
  49. container_of(core_vdev, struct nvgrace_gpu_pci_core_device,
  50. core_device.vdev);
  51. nvdev->resmem.bar_val = 0;
  52. nvdev->usemem.bar_val = 0;
  53. }
  54. /* Choose the structure corresponding to the fake BAR with a given index. */
  55. static struct mem_region *
  56. nvgrace_gpu_memregion(int index,
  57. struct nvgrace_gpu_pci_core_device *nvdev)
  58. {
  59. if (index == USEMEM_REGION_INDEX)
  60. return &nvdev->usemem;
  61. if (nvdev->resmem.memlength && index == RESMEM_REGION_INDEX)
  62. return &nvdev->resmem;
  63. return NULL;
  64. }
  65. static int nvgrace_gpu_open_device(struct vfio_device *core_vdev)
  66. {
  67. struct vfio_pci_core_device *vdev =
  68. container_of(core_vdev, struct vfio_pci_core_device, vdev);
  69. struct nvgrace_gpu_pci_core_device *nvdev =
  70. container_of(core_vdev, struct nvgrace_gpu_pci_core_device,
  71. core_device.vdev);
  72. int ret;
  73. ret = vfio_pci_core_enable(vdev);
  74. if (ret)
  75. return ret;
  76. if (nvdev->usemem.memlength) {
  77. nvgrace_gpu_init_fake_bar_emu_regs(core_vdev);
  78. mutex_init(&nvdev->remap_lock);
  79. }
  80. vfio_pci_core_finish_enable(vdev);
  81. return 0;
  82. }
  83. static void nvgrace_gpu_close_device(struct vfio_device *core_vdev)
  84. {
  85. struct nvgrace_gpu_pci_core_device *nvdev =
  86. container_of(core_vdev, struct nvgrace_gpu_pci_core_device,
  87. core_device.vdev);
  88. /* Unmap the mapping to the device memory cached region */
  89. if (nvdev->usemem.memaddr) {
  90. memunmap(nvdev->usemem.memaddr);
  91. nvdev->usemem.memaddr = NULL;
  92. }
  93. /* Unmap the mapping to the device memory non-cached region */
  94. if (nvdev->resmem.ioaddr) {
  95. iounmap(nvdev->resmem.ioaddr);
  96. nvdev->resmem.ioaddr = NULL;
  97. }
  98. mutex_destroy(&nvdev->remap_lock);
  99. vfio_pci_core_close_device(core_vdev);
  100. }
  101. static int nvgrace_gpu_mmap(struct vfio_device *core_vdev,
  102. struct vm_area_struct *vma)
  103. {
  104. struct nvgrace_gpu_pci_core_device *nvdev =
  105. container_of(core_vdev, struct nvgrace_gpu_pci_core_device,
  106. core_device.vdev);
  107. struct mem_region *memregion;
  108. unsigned long start_pfn;
  109. u64 req_len, pgoff, end;
  110. unsigned int index;
  111. int ret = 0;
  112. index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);
  113. memregion = nvgrace_gpu_memregion(index, nvdev);
  114. if (!memregion)
  115. return vfio_pci_core_mmap(core_vdev, vma);
  116. /*
  117. * Request to mmap the BAR. Map to the CPU accessible memory on the
  118. * GPU using the memory information gathered from the system ACPI
  119. * tables.
  120. */
  121. pgoff = vma->vm_pgoff &
  122. ((1U << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
  123. if (check_sub_overflow(vma->vm_end, vma->vm_start, &req_len) ||
  124. check_add_overflow(PHYS_PFN(memregion->memphys), pgoff, &start_pfn) ||
  125. check_add_overflow(PFN_PHYS(pgoff), req_len, &end))
  126. return -EOVERFLOW;
  127. /*
  128. * Check that the mapping request does not go beyond available device
  129. * memory size
  130. */
  131. if (end > memregion->memlength)
  132. return -EINVAL;
  133. /*
  134. * The carved out region of the device memory needs the NORMAL_NC
  135. * property. Communicate as such to the hypervisor.
  136. */
  137. if (index == RESMEM_REGION_INDEX) {
  138. /*
  139. * The nvgrace-gpu module has no issues with uncontained
  140. * failures on NORMAL_NC accesses. VM_ALLOW_ANY_UNCACHED is
  141. * set to communicate to the KVM to S2 map as NORMAL_NC.
  142. * This opens up guest usage of NORMAL_NC for this mapping.
  143. */
  144. vm_flags_set(vma, VM_ALLOW_ANY_UNCACHED);
  145. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  146. }
  147. /*
  148. * Perform a PFN map to the memory and back the device BAR by the
  149. * GPU memory.
  150. *
  151. * The available GPU memory size may not be power-of-2 aligned. The
  152. * remainder is only backed by vfio_device_ops read/write handlers.
  153. *
  154. * During device reset, the GPU is safely disconnected to the CPU
  155. * and access to the BAR will be immediately returned preventing
  156. * machine check.
  157. */
  158. ret = remap_pfn_range(vma, vma->vm_start, start_pfn,
  159. req_len, vma->vm_page_prot);
  160. if (ret)
  161. return ret;
  162. vma->vm_pgoff = start_pfn;
  163. return 0;
  164. }
  165. static long
  166. nvgrace_gpu_ioctl_get_region_info(struct vfio_device *core_vdev,
  167. unsigned long arg)
  168. {
  169. struct nvgrace_gpu_pci_core_device *nvdev =
  170. container_of(core_vdev, struct nvgrace_gpu_pci_core_device,
  171. core_device.vdev);
  172. unsigned long minsz = offsetofend(struct vfio_region_info, offset);
  173. struct vfio_info_cap caps = { .buf = NULL, .size = 0 };
  174. struct vfio_region_info_cap_sparse_mmap *sparse;
  175. struct vfio_region_info info;
  176. struct mem_region *memregion;
  177. u32 size;
  178. int ret;
  179. if (copy_from_user(&info, (void __user *)arg, minsz))
  180. return -EFAULT;
  181. if (info.argsz < minsz)
  182. return -EINVAL;
  183. /*
  184. * Request to determine the BAR region information. Send the
  185. * GPU memory information.
  186. */
  187. memregion = nvgrace_gpu_memregion(info.index, nvdev);
  188. if (!memregion)
  189. return vfio_pci_core_ioctl(core_vdev,
  190. VFIO_DEVICE_GET_REGION_INFO, arg);
  191. size = struct_size(sparse, areas, 1);
  192. /*
  193. * Setup for sparse mapping for the device memory. Only the
  194. * available device memory on the hardware is shown as a
  195. * mappable region.
  196. */
  197. sparse = kzalloc(size, GFP_KERNEL);
  198. if (!sparse)
  199. return -ENOMEM;
  200. sparse->nr_areas = 1;
  201. sparse->areas[0].offset = 0;
  202. sparse->areas[0].size = memregion->memlength;
  203. sparse->header.id = VFIO_REGION_INFO_CAP_SPARSE_MMAP;
  204. sparse->header.version = 1;
  205. ret = vfio_info_add_capability(&caps, &sparse->header, size);
  206. kfree(sparse);
  207. if (ret)
  208. return ret;
  209. info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
  210. /*
  211. * The region memory size may not be power-of-2 aligned.
  212. * Given that the memory as a BAR and may not be
  213. * aligned, roundup to the next power-of-2.
  214. */
  215. info.size = memregion->bar_size;
  216. info.flags = VFIO_REGION_INFO_FLAG_READ |
  217. VFIO_REGION_INFO_FLAG_WRITE |
  218. VFIO_REGION_INFO_FLAG_MMAP;
  219. if (caps.size) {
  220. info.flags |= VFIO_REGION_INFO_FLAG_CAPS;
  221. if (info.argsz < sizeof(info) + caps.size) {
  222. info.argsz = sizeof(info) + caps.size;
  223. info.cap_offset = 0;
  224. } else {
  225. vfio_info_cap_shift(&caps, sizeof(info));
  226. if (copy_to_user((void __user *)arg +
  227. sizeof(info), caps.buf,
  228. caps.size)) {
  229. kfree(caps.buf);
  230. return -EFAULT;
  231. }
  232. info.cap_offset = sizeof(info);
  233. }
  234. kfree(caps.buf);
  235. }
  236. return copy_to_user((void __user *)arg, &info, minsz) ?
  237. -EFAULT : 0;
  238. }
  239. static long nvgrace_gpu_ioctl(struct vfio_device *core_vdev,
  240. unsigned int cmd, unsigned long arg)
  241. {
  242. switch (cmd) {
  243. case VFIO_DEVICE_GET_REGION_INFO:
  244. return nvgrace_gpu_ioctl_get_region_info(core_vdev, arg);
  245. case VFIO_DEVICE_IOEVENTFD:
  246. return -ENOTTY;
  247. case VFIO_DEVICE_RESET:
  248. nvgrace_gpu_init_fake_bar_emu_regs(core_vdev);
  249. fallthrough;
  250. default:
  251. return vfio_pci_core_ioctl(core_vdev, cmd, arg);
  252. }
  253. }
  254. static __le64
  255. nvgrace_gpu_get_read_value(size_t bar_size, u64 flags, __le64 val64)
  256. {
  257. u64 tmp_val;
  258. tmp_val = le64_to_cpu(val64);
  259. tmp_val &= ~(bar_size - 1);
  260. tmp_val |= flags;
  261. return cpu_to_le64(tmp_val);
  262. }
  263. /*
  264. * Both the usable (usemem) and the reserved (resmem) device memory region
  265. * are exposed as a 64b fake device BARs in the VM. These fake BARs must
  266. * respond to the accesses on their respective PCI config space offsets.
  267. *
  268. * resmem BAR owns PCI_BASE_ADDRESS_2 & PCI_BASE_ADDRESS_3.
  269. * usemem BAR owns PCI_BASE_ADDRESS_4 & PCI_BASE_ADDRESS_5.
  270. */
  271. static ssize_t
  272. nvgrace_gpu_read_config_emu(struct vfio_device *core_vdev,
  273. char __user *buf, size_t count, loff_t *ppos)
  274. {
  275. struct nvgrace_gpu_pci_core_device *nvdev =
  276. container_of(core_vdev, struct nvgrace_gpu_pci_core_device,
  277. core_device.vdev);
  278. u64 pos = *ppos & VFIO_PCI_OFFSET_MASK;
  279. struct mem_region *memregion = NULL;
  280. __le64 val64;
  281. size_t register_offset;
  282. loff_t copy_offset;
  283. size_t copy_count;
  284. int ret;
  285. ret = vfio_pci_core_read(core_vdev, buf, count, ppos);
  286. if (ret < 0)
  287. return ret;
  288. if (vfio_pci_core_range_intersect_range(pos, count, PCI_BASE_ADDRESS_2,
  289. sizeof(val64),
  290. &copy_offset, &copy_count,
  291. &register_offset))
  292. memregion = nvgrace_gpu_memregion(RESMEM_REGION_INDEX, nvdev);
  293. else if (vfio_pci_core_range_intersect_range(pos, count,
  294. PCI_BASE_ADDRESS_4,
  295. sizeof(val64),
  296. &copy_offset, &copy_count,
  297. &register_offset))
  298. memregion = nvgrace_gpu_memregion(USEMEM_REGION_INDEX, nvdev);
  299. if (memregion) {
  300. val64 = nvgrace_gpu_get_read_value(memregion->bar_size,
  301. PCI_BASE_ADDRESS_MEM_TYPE_64 |
  302. PCI_BASE_ADDRESS_MEM_PREFETCH,
  303. memregion->bar_val);
  304. if (copy_to_user(buf + copy_offset,
  305. (void *)&val64 + register_offset, copy_count)) {
  306. /*
  307. * The position has been incremented in
  308. * vfio_pci_core_read. Reset the offset back to the
  309. * starting position.
  310. */
  311. *ppos -= count;
  312. return -EFAULT;
  313. }
  314. }
  315. return count;
  316. }
  317. static ssize_t
  318. nvgrace_gpu_write_config_emu(struct vfio_device *core_vdev,
  319. const char __user *buf, size_t count, loff_t *ppos)
  320. {
  321. struct nvgrace_gpu_pci_core_device *nvdev =
  322. container_of(core_vdev, struct nvgrace_gpu_pci_core_device,
  323. core_device.vdev);
  324. u64 pos = *ppos & VFIO_PCI_OFFSET_MASK;
  325. struct mem_region *memregion = NULL;
  326. size_t register_offset;
  327. loff_t copy_offset;
  328. size_t copy_count;
  329. if (vfio_pci_core_range_intersect_range(pos, count, PCI_BASE_ADDRESS_2,
  330. sizeof(u64), &copy_offset,
  331. &copy_count, &register_offset))
  332. memregion = nvgrace_gpu_memregion(RESMEM_REGION_INDEX, nvdev);
  333. else if (vfio_pci_core_range_intersect_range(pos, count, PCI_BASE_ADDRESS_4,
  334. sizeof(u64), &copy_offset,
  335. &copy_count, &register_offset))
  336. memregion = nvgrace_gpu_memregion(USEMEM_REGION_INDEX, nvdev);
  337. if (memregion) {
  338. if (copy_from_user((void *)&memregion->bar_val + register_offset,
  339. buf + copy_offset, copy_count))
  340. return -EFAULT;
  341. *ppos += copy_count;
  342. return copy_count;
  343. }
  344. return vfio_pci_core_write(core_vdev, buf, count, ppos);
  345. }
  346. /*
  347. * Ad hoc map the device memory in the module kernel VA space. Primarily needed
  348. * as vfio does not require the userspace driver to only perform accesses through
  349. * mmaps of the vfio-pci BAR regions and such accesses should be supported using
  350. * vfio_device_ops read/write implementations.
  351. *
  352. * The usemem region is cacheable memory and hence is memremaped.
  353. * The resmem region is non-cached and is mapped using ioremap_wc (NORMAL_NC).
  354. */
  355. static int
  356. nvgrace_gpu_map_device_mem(int index,
  357. struct nvgrace_gpu_pci_core_device *nvdev)
  358. {
  359. struct mem_region *memregion;
  360. int ret = 0;
  361. memregion = nvgrace_gpu_memregion(index, nvdev);
  362. if (!memregion)
  363. return -EINVAL;
  364. mutex_lock(&nvdev->remap_lock);
  365. if (memregion->memaddr)
  366. goto unlock;
  367. if (index == USEMEM_REGION_INDEX)
  368. memregion->memaddr = memremap(memregion->memphys,
  369. memregion->memlength,
  370. MEMREMAP_WB);
  371. else
  372. memregion->ioaddr = ioremap_wc(memregion->memphys,
  373. memregion->memlength);
  374. if (!memregion->memaddr)
  375. ret = -ENOMEM;
  376. unlock:
  377. mutex_unlock(&nvdev->remap_lock);
  378. return ret;
  379. }
  380. /*
  381. * Read the data from the device memory (mapped either through ioremap
  382. * or memremap) into the user buffer.
  383. */
  384. static int
  385. nvgrace_gpu_map_and_read(struct nvgrace_gpu_pci_core_device *nvdev,
  386. char __user *buf, size_t mem_count, loff_t *ppos)
  387. {
  388. unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
  389. u64 offset = *ppos & VFIO_PCI_OFFSET_MASK;
  390. int ret;
  391. if (!mem_count)
  392. return 0;
  393. /*
  394. * Handle read on the BAR regions. Map to the target device memory
  395. * physical address and copy to the request read buffer.
  396. */
  397. ret = nvgrace_gpu_map_device_mem(index, nvdev);
  398. if (ret)
  399. return ret;
  400. if (index == USEMEM_REGION_INDEX) {
  401. if (copy_to_user(buf,
  402. (u8 *)nvdev->usemem.memaddr + offset,
  403. mem_count))
  404. ret = -EFAULT;
  405. } else {
  406. /*
  407. * The hardware ensures that the system does not crash when
  408. * the device memory is accessed with the memory enable
  409. * turned off. It synthesizes ~0 on such read. So there is
  410. * no need to check or support the disablement/enablement of
  411. * BAR through PCI_COMMAND config space register. Pass
  412. * test_mem flag as false.
  413. */
  414. ret = vfio_pci_core_do_io_rw(&nvdev->core_device, false,
  415. nvdev->resmem.ioaddr,
  416. buf, offset, mem_count,
  417. 0, 0, false);
  418. }
  419. return ret;
  420. }
  421. /*
  422. * Read count bytes from the device memory at an offset. The actual device
  423. * memory size (available) may not be a power-of-2. So the driver fakes
  424. * the size to a power-of-2 (reported) when exposing to a user space driver.
  425. *
  426. * Reads starting beyond the reported size generate -EINVAL; reads extending
  427. * beyond the actual device size is filled with ~0; reads extending beyond
  428. * the reported size are truncated.
  429. */
  430. static ssize_t
  431. nvgrace_gpu_read_mem(struct nvgrace_gpu_pci_core_device *nvdev,
  432. char __user *buf, size_t count, loff_t *ppos)
  433. {
  434. u64 offset = *ppos & VFIO_PCI_OFFSET_MASK;
  435. unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
  436. struct mem_region *memregion;
  437. size_t mem_count, i;
  438. u8 val = 0xFF;
  439. int ret;
  440. /* No need to do NULL check as caller does. */
  441. memregion = nvgrace_gpu_memregion(index, nvdev);
  442. if (offset >= memregion->bar_size)
  443. return -EINVAL;
  444. /* Clip short the read request beyond reported BAR size */
  445. count = min(count, memregion->bar_size - (size_t)offset);
  446. /*
  447. * Determine how many bytes to be actually read from the device memory.
  448. * Read request beyond the actual device memory size is filled with ~0,
  449. * while those beyond the actual reported size is skipped.
  450. */
  451. if (offset >= memregion->memlength)
  452. mem_count = 0;
  453. else
  454. mem_count = min(count, memregion->memlength - (size_t)offset);
  455. ret = nvgrace_gpu_map_and_read(nvdev, buf, mem_count, ppos);
  456. if (ret)
  457. return ret;
  458. /*
  459. * Only the device memory present on the hardware is mapped, which may
  460. * not be power-of-2 aligned. A read to an offset beyond the device memory
  461. * size is filled with ~0.
  462. */
  463. for (i = mem_count; i < count; i++) {
  464. ret = put_user(val, (unsigned char __user *)(buf + i));
  465. if (ret)
  466. return ret;
  467. }
  468. *ppos += count;
  469. return count;
  470. }
  471. static ssize_t
  472. nvgrace_gpu_read(struct vfio_device *core_vdev,
  473. char __user *buf, size_t count, loff_t *ppos)
  474. {
  475. unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
  476. struct nvgrace_gpu_pci_core_device *nvdev =
  477. container_of(core_vdev, struct nvgrace_gpu_pci_core_device,
  478. core_device.vdev);
  479. if (nvgrace_gpu_memregion(index, nvdev))
  480. return nvgrace_gpu_read_mem(nvdev, buf, count, ppos);
  481. if (index == VFIO_PCI_CONFIG_REGION_INDEX)
  482. return nvgrace_gpu_read_config_emu(core_vdev, buf, count, ppos);
  483. return vfio_pci_core_read(core_vdev, buf, count, ppos);
  484. }
  485. /*
  486. * Write the data to the device memory (mapped either through ioremap
  487. * or memremap) from the user buffer.
  488. */
  489. static int
  490. nvgrace_gpu_map_and_write(struct nvgrace_gpu_pci_core_device *nvdev,
  491. const char __user *buf, size_t mem_count,
  492. loff_t *ppos)
  493. {
  494. unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
  495. loff_t pos = *ppos & VFIO_PCI_OFFSET_MASK;
  496. int ret;
  497. if (!mem_count)
  498. return 0;
  499. ret = nvgrace_gpu_map_device_mem(index, nvdev);
  500. if (ret)
  501. return ret;
  502. if (index == USEMEM_REGION_INDEX) {
  503. if (copy_from_user((u8 *)nvdev->usemem.memaddr + pos,
  504. buf, mem_count))
  505. return -EFAULT;
  506. } else {
  507. /*
  508. * The hardware ensures that the system does not crash when
  509. * the device memory is accessed with the memory enable
  510. * turned off. It drops such writes. So there is no need to
  511. * check or support the disablement/enablement of BAR
  512. * through PCI_COMMAND config space register. Pass test_mem
  513. * flag as false.
  514. */
  515. ret = vfio_pci_core_do_io_rw(&nvdev->core_device, false,
  516. nvdev->resmem.ioaddr,
  517. (char __user *)buf, pos, mem_count,
  518. 0, 0, true);
  519. }
  520. return ret;
  521. }
  522. /*
  523. * Write count bytes to the device memory at a given offset. The actual device
  524. * memory size (available) may not be a power-of-2. So the driver fakes the
  525. * size to a power-of-2 (reported) when exposing to a user space driver.
  526. *
  527. * Writes extending beyond the reported size are truncated; writes starting
  528. * beyond the reported size generate -EINVAL.
  529. */
  530. static ssize_t
  531. nvgrace_gpu_write_mem(struct nvgrace_gpu_pci_core_device *nvdev,
  532. size_t count, loff_t *ppos, const char __user *buf)
  533. {
  534. unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
  535. u64 offset = *ppos & VFIO_PCI_OFFSET_MASK;
  536. struct mem_region *memregion;
  537. size_t mem_count;
  538. int ret = 0;
  539. /* No need to do NULL check as caller does. */
  540. memregion = nvgrace_gpu_memregion(index, nvdev);
  541. if (offset >= memregion->bar_size)
  542. return -EINVAL;
  543. /* Clip short the write request beyond reported BAR size */
  544. count = min(count, memregion->bar_size - (size_t)offset);
  545. /*
  546. * Determine how many bytes to be actually written to the device memory.
  547. * Do not write to the offset beyond available size.
  548. */
  549. if (offset >= memregion->memlength)
  550. goto exitfn;
  551. /*
  552. * Only the device memory present on the hardware is mapped, which may
  553. * not be power-of-2 aligned. Drop access outside the available device
  554. * memory on the hardware.
  555. */
  556. mem_count = min(count, memregion->memlength - (size_t)offset);
  557. ret = nvgrace_gpu_map_and_write(nvdev, buf, mem_count, ppos);
  558. if (ret)
  559. return ret;
  560. exitfn:
  561. *ppos += count;
  562. return count;
  563. }
  564. static ssize_t
  565. nvgrace_gpu_write(struct vfio_device *core_vdev,
  566. const char __user *buf, size_t count, loff_t *ppos)
  567. {
  568. struct nvgrace_gpu_pci_core_device *nvdev =
  569. container_of(core_vdev, struct nvgrace_gpu_pci_core_device,
  570. core_device.vdev);
  571. unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
  572. if (nvgrace_gpu_memregion(index, nvdev))
  573. return nvgrace_gpu_write_mem(nvdev, count, ppos, buf);
  574. if (index == VFIO_PCI_CONFIG_REGION_INDEX)
  575. return nvgrace_gpu_write_config_emu(core_vdev, buf, count, ppos);
  576. return vfio_pci_core_write(core_vdev, buf, count, ppos);
  577. }
  578. static const struct vfio_device_ops nvgrace_gpu_pci_ops = {
  579. .name = "nvgrace-gpu-vfio-pci",
  580. .init = vfio_pci_core_init_dev,
  581. .release = vfio_pci_core_release_dev,
  582. .open_device = nvgrace_gpu_open_device,
  583. .close_device = nvgrace_gpu_close_device,
  584. .ioctl = nvgrace_gpu_ioctl,
  585. .device_feature = vfio_pci_core_ioctl_feature,
  586. .read = nvgrace_gpu_read,
  587. .write = nvgrace_gpu_write,
  588. .mmap = nvgrace_gpu_mmap,
  589. .request = vfio_pci_core_request,
  590. .match = vfio_pci_core_match,
  591. .bind_iommufd = vfio_iommufd_physical_bind,
  592. .unbind_iommufd = vfio_iommufd_physical_unbind,
  593. .attach_ioas = vfio_iommufd_physical_attach_ioas,
  594. .detach_ioas = vfio_iommufd_physical_detach_ioas,
  595. };
  596. static const struct vfio_device_ops nvgrace_gpu_pci_core_ops = {
  597. .name = "nvgrace-gpu-vfio-pci-core",
  598. .init = vfio_pci_core_init_dev,
  599. .release = vfio_pci_core_release_dev,
  600. .open_device = nvgrace_gpu_open_device,
  601. .close_device = vfio_pci_core_close_device,
  602. .ioctl = vfio_pci_core_ioctl,
  603. .device_feature = vfio_pci_core_ioctl_feature,
  604. .read = vfio_pci_core_read,
  605. .write = vfio_pci_core_write,
  606. .mmap = vfio_pci_core_mmap,
  607. .request = vfio_pci_core_request,
  608. .match = vfio_pci_core_match,
  609. .bind_iommufd = vfio_iommufd_physical_bind,
  610. .unbind_iommufd = vfio_iommufd_physical_unbind,
  611. .attach_ioas = vfio_iommufd_physical_attach_ioas,
  612. .detach_ioas = vfio_iommufd_physical_detach_ioas,
  613. };
  614. static int
  615. nvgrace_gpu_fetch_memory_property(struct pci_dev *pdev,
  616. u64 *pmemphys, u64 *pmemlength)
  617. {
  618. int ret;
  619. /*
  620. * The memory information is present in the system ACPI tables as DSD
  621. * properties nvidia,gpu-mem-base-pa and nvidia,gpu-mem-size.
  622. */
  623. ret = device_property_read_u64(&pdev->dev, "nvidia,gpu-mem-base-pa",
  624. pmemphys);
  625. if (ret)
  626. return ret;
  627. if (*pmemphys > type_max(phys_addr_t))
  628. return -EOVERFLOW;
  629. ret = device_property_read_u64(&pdev->dev, "nvidia,gpu-mem-size",
  630. pmemlength);
  631. if (ret)
  632. return ret;
  633. if (*pmemlength > type_max(size_t))
  634. return -EOVERFLOW;
  635. /*
  636. * If the C2C link is not up due to an error, the coherent device
  637. * memory size is returned as 0. Fail in such case.
  638. */
  639. if (*pmemlength == 0)
  640. return -ENOMEM;
  641. return ret;
  642. }
  643. static int
  644. nvgrace_gpu_init_nvdev_struct(struct pci_dev *pdev,
  645. struct nvgrace_gpu_pci_core_device *nvdev,
  646. u64 memphys, u64 memlength)
  647. {
  648. int ret = 0;
  649. u64 resmem_size = 0;
  650. /*
  651. * On Grace Hopper systems, the VM GPU device driver needs a non-cacheable
  652. * region to support the MIG feature owing to a hardware bug. Since the
  653. * device memory is mapped as NORMAL cached, carve out a region from the end
  654. * with a different NORMAL_NC property (called as reserved memory and
  655. * represented as resmem). This region then is exposed as a 64b BAR
  656. * (region 2 and 3) to the VM, while exposing the rest (termed as usable
  657. * memory and represented using usemem) as cacheable 64b BAR (region 4 and 5).
  658. *
  659. * devmem (memlength)
  660. * |-------------------------------------------------|
  661. * | |
  662. * usemem.memphys resmem.memphys
  663. *
  664. * This hardware bug is fixed on the Grace Blackwell platforms and the
  665. * presence of the bug can be determined through nvdev->has_mig_hw_bug.
  666. * Thus on systems with the hardware fix, there is no need to partition
  667. * the GPU device memory and the entire memory is usable and mapped as
  668. * NORMAL cached (i.e. resmem size is 0).
  669. */
  670. if (nvdev->has_mig_hw_bug)
  671. resmem_size = SZ_1G;
  672. nvdev->usemem.memphys = memphys;
  673. /*
  674. * The device memory exposed to the VM is added to the kernel by the
  675. * VM driver module in chunks of memory block size. Note that only the
  676. * usable memory (usemem) is added to the kernel for usage by the VM
  677. * workloads.
  678. */
  679. if (check_sub_overflow(memlength, resmem_size,
  680. &nvdev->usemem.memlength)) {
  681. ret = -EOVERFLOW;
  682. goto done;
  683. }
  684. /*
  685. * The usemem region is exposed as a 64B Bar composed of region 4 and 5.
  686. * Calculate and save the BAR size for the region.
  687. */
  688. nvdev->usemem.bar_size = roundup_pow_of_two(nvdev->usemem.memlength);
  689. /*
  690. * If the hardware has the fix for MIG, there is no requirement
  691. * for splitting the device memory to create RESMEM. The entire
  692. * device memory is usable and will be USEMEM. Return here for
  693. * such case.
  694. */
  695. if (!nvdev->has_mig_hw_bug)
  696. goto done;
  697. /*
  698. * When the device memory is split to workaround the MIG bug on
  699. * Grace Hopper, the USEMEM part of the device memory has to be
  700. * MEMBLK_SIZE aligned. This is a hardwired ABI value between the
  701. * GPU FW and VFIO driver. The VM device driver is also aware of it
  702. * and make use of the value for its calculation to determine USEMEM
  703. * size. Note that the device memory may not be 512M aligned.
  704. */
  705. nvdev->usemem.memlength = round_down(nvdev->usemem.memlength,
  706. MEMBLK_SIZE);
  707. if (nvdev->usemem.memlength == 0) {
  708. ret = -EINVAL;
  709. goto done;
  710. }
  711. if ((check_add_overflow(nvdev->usemem.memphys,
  712. nvdev->usemem.memlength,
  713. &nvdev->resmem.memphys)) ||
  714. (check_sub_overflow(memlength, nvdev->usemem.memlength,
  715. &nvdev->resmem.memlength))) {
  716. ret = -EOVERFLOW;
  717. goto done;
  718. }
  719. /*
  720. * The resmem region is exposed as a 64b BAR composed of region 2 and 3
  721. * for Grace Hopper. Calculate and save the BAR size for the region.
  722. */
  723. nvdev->resmem.bar_size = roundup_pow_of_two(nvdev->resmem.memlength);
  724. done:
  725. return ret;
  726. }
  727. static bool nvgrace_gpu_has_mig_hw_bug(struct pci_dev *pdev)
  728. {
  729. int pcie_dvsec;
  730. u16 dvsec_ctrl16;
  731. pcie_dvsec = pci_find_dvsec_capability(pdev, PCI_VENDOR_ID_NVIDIA,
  732. GPU_CAP_DVSEC_REGISTER);
  733. if (pcie_dvsec) {
  734. pci_read_config_word(pdev,
  735. pcie_dvsec + DVSEC_BITMAP_OFFSET,
  736. &dvsec_ctrl16);
  737. if (dvsec_ctrl16 & MIG_SUPPORTED_WITH_CACHED_RESMEM)
  738. return false;
  739. }
  740. return true;
  741. }
  742. static int nvgrace_gpu_probe(struct pci_dev *pdev,
  743. const struct pci_device_id *id)
  744. {
  745. const struct vfio_device_ops *ops = &nvgrace_gpu_pci_core_ops;
  746. struct nvgrace_gpu_pci_core_device *nvdev;
  747. u64 memphys, memlength;
  748. int ret;
  749. ret = nvgrace_gpu_fetch_memory_property(pdev, &memphys, &memlength);
  750. if (!ret)
  751. ops = &nvgrace_gpu_pci_ops;
  752. nvdev = vfio_alloc_device(nvgrace_gpu_pci_core_device, core_device.vdev,
  753. &pdev->dev, ops);
  754. if (IS_ERR(nvdev))
  755. return PTR_ERR(nvdev);
  756. dev_set_drvdata(&pdev->dev, &nvdev->core_device);
  757. if (ops == &nvgrace_gpu_pci_ops) {
  758. nvdev->has_mig_hw_bug = nvgrace_gpu_has_mig_hw_bug(pdev);
  759. /*
  760. * Device memory properties are identified in the host ACPI
  761. * table. Set the nvgrace_gpu_pci_core_device structure.
  762. */
  763. ret = nvgrace_gpu_init_nvdev_struct(pdev, nvdev,
  764. memphys, memlength);
  765. if (ret)
  766. goto out_put_vdev;
  767. }
  768. ret = vfio_pci_core_register_device(&nvdev->core_device);
  769. if (ret)
  770. goto out_put_vdev;
  771. return ret;
  772. out_put_vdev:
  773. vfio_put_device(&nvdev->core_device.vdev);
  774. return ret;
  775. }
  776. static void nvgrace_gpu_remove(struct pci_dev *pdev)
  777. {
  778. struct vfio_pci_core_device *core_device = dev_get_drvdata(&pdev->dev);
  779. vfio_pci_core_unregister_device(core_device);
  780. vfio_put_device(&core_device->vdev);
  781. }
  782. static const struct pci_device_id nvgrace_gpu_vfio_pci_table[] = {
  783. /* GH200 120GB */
  784. { PCI_DRIVER_OVERRIDE_DEVICE_VFIO(PCI_VENDOR_ID_NVIDIA, 0x2342) },
  785. /* GH200 480GB */
  786. { PCI_DRIVER_OVERRIDE_DEVICE_VFIO(PCI_VENDOR_ID_NVIDIA, 0x2345) },
  787. {}
  788. };
  789. MODULE_DEVICE_TABLE(pci, nvgrace_gpu_vfio_pci_table);
  790. static struct pci_driver nvgrace_gpu_vfio_pci_driver = {
  791. .name = KBUILD_MODNAME,
  792. .id_table = nvgrace_gpu_vfio_pci_table,
  793. .probe = nvgrace_gpu_probe,
  794. .remove = nvgrace_gpu_remove,
  795. .err_handler = &vfio_pci_core_err_handlers,
  796. .driver_managed_dma = true,
  797. };
  798. module_pci_driver(nvgrace_gpu_vfio_pci_driver);
  799. MODULE_LICENSE("GPL");
  800. MODULE_AUTHOR("Ankit Agrawal <ankita@nvidia.com>");
  801. MODULE_AUTHOR("Aniket Agashe <aniketa@nvidia.com>");
  802. MODULE_DESCRIPTION("VFIO NVGRACE GPU PF - User Level driver for NVIDIA devices with CPU coherently accessible device memory");