vfio/nvgrace-gpu: Add vfio pci variant module for grace hopper
Subsystems (6)
[email protected]
maintainerv19 cover vfio/nvgrace-gpu: Add vfio pci variant module for grace hopper
From: Ankit Agrawal <[email protected]>
NVIDIA's upcoming Grace Hopper Superchip provides a PCI-like device for the on-chip GPU that is the logical OS representation of the internal proprietary chip-to-chip cache coherent interconnect.
The device is peculiar compared to a real PCI device in that whilst there is a real 64b PCI BAR1 (comprising region 2 & region 3) on the device, it is not used to access device memory once the faster chip-to-chip interconnect is initialized (occurs at the time of host system boot). The device memory is accessed instead using the chip-to-chip interconnect that is exposed as a contiguous physically addressable region on the host. Since the device memory is cache coherent with the CPU, it can be mmaped into the user VMA with a cacheable mapping and used like a regular RAM. The device memory is not added to the host kernel, but mapped directly as this reduces memory wastage due to struct pages.
There is also a requirement of a minimum reserved 1G uncached region (termed as resmem) to support the Multi-Instance GPU (MIG) feature [1]. This is to work around a HW defect. Based on [2], the requisite properties (uncached, unaligned access) can be achieved through a VM mapping (S1) of NORMAL_NC and host (S2) mapping with MemAttr[2:0]=0b101. To provide a different non-cached property to the reserved 1G region, it needs to be carved out from the device memory and mapped as a separate region in Qemu VMA with pgprot_writecombine(). pgprot_writecombine() sets the Qemu VMA page properties (pgprot) as NORMAL_NC.
Provide a VFIO PCI variant driver that adapts the unique device memory representation into a more standard PCI representation facing userspace.
The variant driver exposes these two regions - the non-cached reserved (resmem) and the cached rest of the device memory (termed as usemem) as separate VFIO 64b BAR regions. This is divergent from the baremetal approach, where the device memory is exposed as a device memory region. The decision for a different approach was taken in view of the fact that it would necessiate additional code in Qemu to discover and insert those regions in the VM IPA, along with the additional VM ACPI DSDT changes to communiate the device memory region IPA to the VM workloads. Moreover, this behavior would have to be added to a variety of emulators (beyond top of tree Qemu) out there desiring grace hopper support.
Since the device implements 64-bit BAR0, the VFIO PCI variant driver maps the uncached carved out region to the next available PCI BAR (i.e. comprising of region 2 and 3). The cached device memory aperture is assigned BAR region 4 and 5. Qemu will then naturally generate a PCI device in the VM with the uncached aperture reported as BAR2 region, the cacheable as BAR4. The variant driver provides emulation for these fake BARs' PCI config space offset registers.
The hardware ensures that the system does not crash when the memory is accessed with the memory enable turned off. It synthesis ~0 reads and dropped writes on such access. So there is no need to support the disablement/enablement of BAR through PCI_COMMAND config space register.
The memory layout on the host looks like the following:
devmem (memlength)
|--------------------------------------------------| |-------------cached------------------------|--NC--| | | usemem.memphys resmem.memphys
PCI BARs need to be aligned to the power-of-2, but the actual memory on the device may not. A read or write access to the physical address from the last device PFN up to the next power-of-2 aligned physical address results in reading ~0 and dropped writes. Note that the GPU device driver [6] is capable of knowing the exact device memory size through separate means. The device memory size is primarily kept in the system ACPI tables for use by the VFIO PCI variant module.
Note that the usemem memory is added by the VM Nvidia device driver [5] to the VM kernel as memblocks. Hence make the usable memory size memblock (MEMBLK_SIZE) aligned. This is a hardwired ABI value between the GPU FW and VFIO driver. The VM device driver make use of the same value for its calculation to determine USEMEM size.
Currently there is no provision in KVM for a S2 mapping with MemAttr[2:0]=0b101, but there is an ongoing effort to provide the same [3]. As previously mentioned, resmem is mapped pgprot_writecombine(), that sets the Qemu VMA page properties (pgprot) as NORMAL_NC. Using the proposed changes in [3] and [4], KVM marks the region with MemAttr[2:0]=0b101 in S2.
If the device memory properties are not present, the driver registers the vfio-pci-core function pointers. Since there are no ACPI memory properties generated for the VM, the variant driver inside the VM will only use the vfio-pci-core ops and hence try to map the BARs as non cached. This is not a problem as the CPUs have FWB enabled which blocks the VM mapping's ability to override the cacheability set by the host mapping.
This goes along with a qemu series [6] to provides the necessary implementation of the Grace Hopper Superchip firmware specification so that the guest operating system can see the correct ACPI modeling for the coherent GPU device. Verified with the CUDA workload in the VM.
[1] https://www.nvidia.com/en-in/technologies/multi-instance-gpu/
[2] section D8.5.5 of https://developer.arm.com/documentation/ddi0487/latest/
[3] https://lore.kernel.org/all/[email protected]/
[4] https://lore.kernel.org/all/[email protected]/
[5] https://github.com/NVIDIA/open-gpu-kernel-modules
[6] https://lore.kernel.org/all/[email protected]/
Applied over v6.8-rc5.
---
Link for variant driver v18:
https://lore.kernel.org/all/[email protected]/
v18 -> v19
- Removed unnecessary memregion null check in nvgrace_gpu_read[write]_mem based on Yishai's comment.
- Added error check for put_user.
- Added device_feature function pointer as pointed out by Yishia.
- Removed nvgrace_gpu_drvdata and inlined the code.
- Moved the nvdev->usemem.memlength check earlier in nvgrace_gpu_init_nvdev_struct.
- Sorted header list in alphabetical order Per Zhi's comment.
- Rebased to v6.8-rc5.
v17 -> v18
- Reorg nvgrace_gpu_open_device code based on Zhi Wang's suggestion
- Added code to return early in nvgrace_gpu_map_and_read[write] when mem_count is 0.
- Put the module information in MAINTAINERS in alphabetical order based on Alex's suggestion.
- Consolidated check for the union value in nvgrace_gpu_map_device_mem.
- Put comments around usage of MEMBLK_SIZE from Kevin's suggestion.
- Fixed *ppos to revert to the starting position on failure in nvgrace_gpu_read_config_emu.
- Fixed nits suggested by Alex and Kevin.
- Updated commit message to add information on MEMBLK_SIZE and nested VM.
- Rebased to v6.8-rc4.
v16 -> v17
- Moved, renamed and exported the range_intersect_range() per suggestion from Rahul Rameshbabu.
- Updated license from GPLv2 to GPL.
- Fixed S-O-B mistakes.
- Removed nvgrace_gpu_vfio_pci.h based on Alex Williamson's suggestion.
- Refactor [read]write_config_emu based on Alex's suggestion
- Added fallback to vfio-pci-core function pointers in case of absence of memory properties in the host ACPI table as per Alex's suggestion.
- Used anonymous union to represent the mapped device memory.
- Fixed code nits and rephrased comments.
- Rebased to v6.8-rc2.
v15 -> v16
- Added the missing header file causing build failure in v15.
- Moved the range_intersect_range function() to a seperate patch.
- Exported the do_io_rw as GPL and moved to the vfio-pci-core file.
- Added helper function to mask with BAR size and add flag while returning a read on the fake BARs PCI config register.
- Removed the PCI command disable.
- Removed nvgrace_gpu_vfio_pci_fake_bar_mem_region().
- Fixed miscellaneous nits.
v14 -> v15
- Added case to handle VFIO_DEVICE_IOEVENTFD to return -EIO as it is not required on the device.
- Updated the BAR config space handling code to closely resemble by Yishai Hadas (using range_intersect_range) in
https://lore.kernel.org/all/[email protected]
- Changed the bar pci config register from union to u64.
- Adapted the code to disable BAR when it is disabled through PCI_COMMAND.
- Exported and reused the do_io_rw to do mmio accesses.
- Added a new header file to keep the newly declared structures.
- Miscellaneous code fixes suggested by Alex Williamson in v14.
v13 -> v14
- Merged the changes for second BAR implementation for MIG support on the device driver.
https://lore.kernel.org/all/[email protected]/
- Added the missing implementation of sub-word access to fake BARs' PCI config access. Implemented access algorithm suggested by Alex Williamson in the comments (Thanks!)
- Added support to BAR accesses on the reserved memory with Qemu device param x-no-mmap=on.
- Handled endian-ness in the PCI config space access.
- Git commit message change
v12 -> v13
- Added emulation for the PCI config space BAR offset register for the fake BAR.
- commit message updated with more details on the BAR offset emulation.
v11 -> v12
- More details in commit message on device memory size
v10 -> v11
- Removed sysfs attribute to expose the CPU coherent memory feature
- Addressed review comments
v9 -> v10
- Add new sysfs attribute to expose the CPU coherent memory feature.
v8 -> v9
- Minor code adjustment suggested in v8.
v7 -> v8
- Various field names updated.
- Added a new function to handle VFIO_DEVICE_GET_REGION_INFO ioctl.
- Locking protection for memremap to bar region and other changes recommended in v7.
- Added code to fail if the devmem size advertized is 0 in system DSDT.
v6 -> v7
- Handled out-of-bound and overflow conditions at various places to validate input offset and length.
- Added code to return EINVAL for offset beyond region size.
v5 -> v6
- Added the code to handle BAR2 read/write using memremap to the device memory.
v4 -> v5
- Changed the module name from nvgpu-vfio-pci to nvgrace-gpu-vfio-pci.
v3 -> v4
- Mapping the available device memory using sparse mmap. The region outside the device memory is handled by read/write ops.
- Removed the fault handler added in v3.
v2 -> v3
- Added fault handler to map the region outside the physical GPU memory up to the next power-of-2 to a dummy PFN.
- Changed to select instead of "depends on" VFIO_PCI_CORE for all the vfio-pci variant driver.
- Code cleanup based on feedback comments.
- Code implemented and tested against v6.4-rc4.
v1 -> v2
- Updated the wording of reference to BAR offset and replaced with index.
- The GPU memory is exposed at the fixed BAR2_REGION_INDEX.
- Code cleanup based on feedback comments.
Ankit Agrawal (3):
vfio/pci: rename and export do_io_rw()
vfio/pci: rename and export range_intersect_range vfio/nvgrace-gpu: Add vfio pci variant module for grace hopper
MAINTAINERS | 16 +- drivers/vfio/pci/Kconfig | 2 + drivers/vfio/pci/Makefile | 2 + drivers/vfio/pci/nvgrace-gpu/Kconfig | 10 + drivers/vfio/pci/nvgrace-gpu/Makefile | 3 + drivers/vfio/pci/nvgrace-gpu/main.c | 879 ++++++++++++++++++++++++++ drivers/vfio/pci/vfio_pci_config.c | 42 ++ drivers/vfio/pci/vfio_pci_rdwr.c | 16 +- drivers/vfio/pci/virtio/main.c | 72 +-- include/linux/vfio_pci_core.h | 10 +- 10 files changed, 993 insertions(+), 59 deletions(-) create mode 100644 drivers/vfio/pci/nvgrace-gpu/Kconfig create mode 100644 drivers/vfio/pci/nvgrace-gpu/Makefile create mode 100644 drivers/vfio/pci/nvgrace-gpu/main.c
Signature · 1 line
2.34.1
[email protected]
maintainerPATCH v19 1/3 vfio/pci: rename and export do_io_rw()
From: Ankit Agrawal <[email protected]>
do_io_rw() is used to read/write to the device MMIO. The grace hopper VFIO PCI variant driver require this functionality to read/write to its memory.
Rename this as vfio_pci_core functions and export as GPL.
--- drivers/vfio/pci/vfio_pci_rdwr.c | 16 +++++++++------- include/linux/vfio_pci_core.h | 5 ++++- 2 files changed, 13 insertions(+), 8 deletions(-)
drivers/vfio/pci/vfio_pci_rdwr.c +9 -7
@@ -96,10 +96,10 @@ VFIO_IOREAD(32)* reads with -1. This is intended for handling MSI-X vector tables and * leftover space for ROM BARs. */ -static ssize_t do_io_rw (struct vfio_pci_core_device *vdev,bool test_mem, -void __iomem *io,char __user *buf, -loff_t off,size_t count,size_t x_start, -size_t x_end,bool iswrite) +ssize_t vfio_pci_core_do_io_rw (struct vfio_pci_core_device *vdev,bool test_mem, +void __iomem *io,char __user *buf, +loff_t off,size_t count,size_t x_start, +size_t x_end,bool iswrite) {ssize_t done =0 ;int ret;
@@ -201,6 +201,7 @@ static ssize_t do_io_rw(struct vfio_pci_core_device *vdev, bool test_mem,return done; } +EXPORT_SYMBOL_GPL (vfio_pci_core_do_io_rw);int vfio_pci_core_setup_barmap (struct vfio_pci_core_device *vdev,int bar) {
@@ -279,8 +280,8 @@ ssize_t vfio_pci_bar_rw(struct vfio_pci_core_device *vdev, char __user *buf, x_end = vdev->msix_offset + vdev->msix_size; } - done =do_io_rw (vdev, res->flags &IORESOURCE_MEM , io, buf, pos, - count, x_start, x_end, iswrite); + done =vfio_pci_core_do_io_rw (vdev, res->flags &IORESOURCE_MEM , io, buf, pos, + count, x_start, x_end, iswrite);if (done >=0 ) *ppos += done;
@@ -348,7 +349,8 @@ ssize_t vfio_pci_vga_rw(struct vfio_pci_core_device *vdev, char __user *buf,* probing, so we don't currently worry about access in relation * to the memory enable bit in the command register. */ - done =do_io_rw (vdev, false, iomem, buf, off, count,0 ,0 , iswrite); + done =vfio_pci_core_do_io_rw (vdev, false, iomem, buf, off, count, +0 ,0 , iswrite);vga_put (vdev->pdev, rsrc);
include/linux/vfio_pci_core.h +4 -1
@@ -130,7 +130,10 @@ void vfio_pci_core_finish_enable(struct vfio_pci_core_device *vdev);int vfio_pci_core_setup_barmap (struct vfio_pci_core_device *vdev,int bar);pci_ers_result_t vfio_pci_core_aer_err_detected (struct pci_dev *pdev,pci_channel_state_t state); - +ssize_t vfio_pci_core_do_io_rw (struct vfio_pci_core_device *vdev,bool test_mem, +void __iomem *io,char __user *buf, +loff_t off,size_t count,size_t x_start, +size_t x_end,bool iswrite);#define VFIO_IOWRITE_DECLATION (size) \ int vfio_pci_core_iowrite##size(struct vfio_pci_core_device *vdev, \ bool test_mem, u##size val, void __iomem *io);
Signature · 1 line
2.34.1
[email protected]
maintainerPATCH v19 2/3 vfio/pci: rename and export range_intersect_range
In reply to [email protected]From: Ankit Agrawal <[email protected]>
range_intersect_range determines an overlap between two ranges. If an overlap, the helper function returns the overlapping offset and size.
The VFIO PCI variant driver emulates the PCI config space BAR offset registers. These offset may be accessed for read/write with a variety of lengths including sub-word sizes from sub-word offsets. The driver makes use of this helper function to read/write the targeted part of the emulated register.
Make this a vfio_pci_core function, rename and export as GPL. Also update references in virtio driver.
--- drivers/vfio/pci/vfio_pci_config.c | 42 +++++++++++++++++ drivers/vfio/pci/virtio/main.c | 72 +++++++++++------------------- include/linux/vfio_pci_core.h | 5 +++ 3 files changed, 73 insertions(+), 46 deletions(-)
drivers/vfio/pci/vfio_pci_config.c +42 -0
@@ -1966,3 +1966,45 @@ ssize_t vfio_pci_config_rw(struct vfio_pci_core_device *vdev, char __user *buf,return done; } + +/** +* vfio_pci_core_range_intersect_range() - Determine overlap between a buffer +* and register offset ranges. +* @buf_start: start offset of the buffer +* @buf_cnt: number of buffer bytes +* @reg_start: start register offset +* @reg_cnt: number of register bytes +* @buf_offset: start offset of overlap in the buffer +* @intersect_count: number of overlapping bytes +* @register_offset: start offset of overlap in register +* +* Returns: true if there is overlap, false if not. +* The overlap start and size is returned through function args. +*/ +bool vfio_pci_core_range_intersect_range (loff_t buf_start,size_t buf_cnt, +loff_t reg_start,size_t reg_cnt, +loff_t *buf_offset, +size_t *intersect_count, +size_t *register_offset) +{ +if (buf_start <= reg_start && + buf_start + buf_cnt > reg_start) { + *buf_offset = reg_start - buf_start; + *intersect_count =min_t (size_t, reg_cnt, + buf_start + buf_cnt - reg_start); + *register_offset =0 ; +return true; + } + +if (buf_start > reg_start && + buf_start < reg_start + reg_cnt) { + *buf_offset =0 ; + *intersect_count =min_t (size_t, buf_cnt, + reg_start + reg_cnt - buf_start); + *register_offset = buf_start - reg_start; +return true; + } + +return false; +} +EXPORT_SYMBOL_GPL (vfio_pci_core_range_intersect_range);
drivers/vfio/pci/virtio/main.c +26 -46
126 lines not on this page · raw message
include/linux/vfio_pci_core.h +5 -0
@@ -134,6 +134,11 @@ ssize_t vfio_pci_core_do_io_rw(struct vfio_pci_core_device *vdev, bool test_mem, void __iomem *io, char __user *buf, loff_t off, size_t count, size_t x_start, size_t x_end, bool iswrite); +bool vfio_pci_core_range_intersect_range (loff_t buf_start,size_t buf_cnt, +loff_t reg_start,size_t reg_cnt, +loff_t *buf_offset, +size_t *intersect_count, +size_t *register_offset);#define VFIO_IOWRITE_DECLATION (size) \ int vfio_pci_core_iowrite##size(struct vfio_pci_core_device *vdev, \ bool test_mem, u##size val, void __iomem *io);
Signature · 1 line
2.34.1
[email protected]
maintainerPATCH v19 3/3 vfio/nvgrace-gpu: Add vfio pci variant module for grace hopper
In reply to [email protected]From: Ankit Agrawal <[email protected]>
NVIDIA's upcoming Grace Hopper Superchip provides a PCI-like device for the on-chip GPU that is the logical OS representation of the internal proprietary chip-to-chip cache coherent interconnect.
The device is peculiar compared to a real PCI device in that whilst there is a real 64b PCI BAR1 (comprising region 2 & region 3) on the device, it is not used to access device memory once the faster chip-to-chip interconnect is initialized (occurs at the time of host system boot). The device memory is accessed instead using the chip-to-chip interconnect that is exposed as a contiguous physically addressable region on the host. This device memory aperture can be obtained from host ACPI table using device_property_read_u64(), according to the FW specification. Since the device memory is cache coherent with the CPU, it can be mmap into the user VMA with a cacheable mapping using remap_pfn_range() and used like a regular RAM. The device memory is not added to the host kernel, but mapped directly as this reduces memory wastage due to struct pages.
There is also a requirement of a minimum reserved 1G uncached region (termed as resmem) to support the Multi-Instance GPU (MIG) feature [1]. This is to work around a HW defect. Based on [2], the requisite properties (uncached, unaligned access) can be achieved through a VM mapping (S1) of NORMAL_NC and host (S2) mapping with MemAttr[2:0]=0b101. To provide a different non-cached property to the reserved 1G region, it needs to be carved out from the device memory and mapped as a separate region in Qemu VMA with pgprot_writecombine(). pgprot_writecombine() sets the Qemu VMA page properties (pgprot) as NORMAL_NC.
Provide a VFIO PCI variant driver that adapts the unique device memory representation into a more standard PCI representation facing userspace.
The variant driver exposes these two regions - the non-cached reserved (resmem) and the cached rest of the device memory (termed as usemem) as separate VFIO 64b BAR regions. This is divergent from the baremetal approach, where the device memory is exposed as a device memory region. The decision for a different approach was taken in view of the fact that it would necessiate additional code in Qemu to discover and insert those regions in the VM IPA, along with the additional VM ACPI DSDT changes to communicate the device memory region IPA to the VM workloads. Moreover, this behavior would have to be added to a variety of emulators (beyond top of tree Qemu) out there desiring grace hopper support.
Since the device implements 64-bit BAR0, the VFIO PCI variant driver maps the uncached carved out region to the next available PCI BAR (i.e. comprising of region 2 and 3). The cached device memory aperture is assigned BAR region 4 and 5. Qemu will then naturally generate a PCI device in the VM with the uncached aperture reported as BAR2 region, the cacheable as BAR4. The variant driver provides emulation for these fake BARs' PCI config space offset registers.
The hardware ensures that the system does not crash when the memory is accessed with the memory enable turned off. It synthesis ~0 reads and dropped writes on such access. So there is no need to support the disablement/enablement of BAR through PCI_COMMAND config space register.
The memory layout on the host looks like the following:
devmem (memlength)
|--------------------------------------------------| |-------------cached------------------------|--NC--| | | usemem.memphys resmem.memphys
PCI BARs need to be aligned to the power-of-2, but the actual memory on the device may not. A read or write access to the physical address from the last device PFN up to the next power-of-2 aligned physical address results in reading ~0 and dropped writes. Note that the GPU device driver [6] is capable of knowing the exact device memory size through separate means. The device memory size is primarily kept in the system ACPI tables for use by the VFIO PCI variant module.
Note that the usemem memory is added by the VM Nvidia device driver [5] to the VM kernel as memblocks. Hence make the usable memory size memblock (MEMBLK_SIZE) aligned. This is a hardwired ABI value between the GPU FW and VFIO driver. The VM device driver make use of the same value for its calculation to determine USEMEM size.
Currently there is no provision in KVM for a S2 mapping with MemAttr[2:0]=0b101, but there is an ongoing effort to provide the same [3]. As previously mentioned, resmem is mapped pgprot_writecombine(), that sets the Qemu VMA page properties (pgprot) as NORMAL_NC. Using the proposed changes in [3] and [4], KVM marks the region with MemAttr[2:0]=0b101 in S2.
If the device memory properties are not present, the driver registers the vfio-pci-core function pointers. Since there are no ACPI memory properties generated for the VM, the variant driver inside the VM will only use the vfio-pci-core ops and hence try to map the BARs as non cached. This is not a problem as the CPUs have FWB enabled which blocks the VM mapping's ability to override the cacheability set by the host mapping.
This goes along with a qemu series [6] to provides the necessary implementation of the Grace Hopper Superchip firmware specification so that the guest operating system can see the correct ACPI modeling for the coherent GPU device. Verified with the CUDA workload in the VM.
[1] https://www.nvidia.com/en-in/technologies/multi-instance-gpu/
[2] section D8.5.5 of https://developer.arm.com/documentation/ddi0487/latest/
[3] https://lore.kernel.org/all/[email protected]/
[4] https://lore.kernel.org/all/[email protected]/
[5] https://github.com/NVIDIA/open-gpu-kernel-modules
[6] https://lore.kernel.org/all/[email protected]/
--- MAINTAINERS | 16 +- drivers/vfio/pci/Kconfig | 2 + drivers/vfio/pci/Makefile | 2 + drivers/vfio/pci/nvgrace-gpu/Kconfig | 10 + drivers/vfio/pci/nvgrace-gpu/Makefile | 3 + drivers/vfio/pci/nvgrace-gpu/main.c | 879 ++++++++++++++++++++++++++ 6 files changed, 907 insertions(+), 5 deletions(-) create mode 100644 drivers/vfio/pci/nvgrace-gpu/Kconfig create mode 100644 drivers/vfio/pci/nvgrace-gpu/Makefile create mode 100644 drivers/vfio/pci/nvgrace-gpu/main.c
MAINTAINERS +11 -5
@@ -23089,12 +23089,11 @@ L: [email protected] S: Maintained F: drivers/vfio/pci/mlx5/ -VFIO VIRTIO PCI DRIVER -M: Yishai Hadas <[email protected]> +VFIO NVIDIA GRACE GPU DRIVER +M: Ankit Agrawal <[email protected]> L: [email protected] -L: [email protected] -S: Maintained -F: drivers/vfio/pci/virtio +S: Supported +F: drivers/vfio/pci/nvgrace-gpu/ VFIO PCI DEVICE SPECIFIC DRIVERS R: Jason Gunthorpe <[email protected]>
@@ -23119,6 +23118,13 @@ L: [email protected] S: Maintained F: drivers/vfio/platform/ +VFIO VIRTIO PCI DRIVER +M: Yishai Hadas <[email protected]> +L: [email protected] +L: [email protected] +S: Maintained +F: drivers/vfio/pci/virtio + VGA_SWITCHEROO R: Lukas Wunner <[email protected]> S: Maintained
drivers/vfio/pci/Kconfig +2 -0
@@ -67,4 +67,6 @@ source "drivers/vfio/pci/pds/Kconfig" source "drivers/vfio/pci/virtio/Kconfig" +source "drivers/vfio/pci/nvgrace-gpu/Kconfig" + endmenu
drivers/vfio/pci/Makefile +2 -0
@@ -15,3 +15,5 @@ obj-$(CONFIG_HISI_ACC_VFIO_PCI) += hisilicon/ obj-$(CONFIG_PDS_VFIO_PCI) += pds/ obj-$(CONFIG_VIRTIO_VFIO_PCI) += virtio/ + +obj-$(CONFIG_NVGRACE_GPU_VFIO_PCI) += nvgrace-gpu/
drivers/vfio/pci/nvgrace-gpu/Kconfig new file +10 -0
@@ -0,0 +1,10 @@ +# SPDX-License-Identifier: GPL-2.0-only +config NVGRACE_GPU_VFIO_PCI + tristate "VFIO support for the GPU in the NVIDIA Grace Hopper Superchip" + depends on ARM64 || (COMPILE_TEST && 64BIT) + select VFIO_PCI_CORE + help + VFIO support for the GPU in the NVIDIA Grace Hopper Superchip is + required to assign the GPU device to userspace using KVM/qemu/etc. + + If you don't know what to do here, say N.
drivers/vfio/pci/nvgrace-gpu/Makefile new file +3 -0
@@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0-only +obj-$(CONFIG_NVGRACE_GPU_VFIO_PCI) += nvgrace-gpu-vfio-pci.o +nvgrace-gpu-vfio-pci-y := main.o
drivers/vfio/pci/nvgrace-gpu/main.c new file +879 -0
879 lines not on this page · raw message
Signature · 1 line
2.34.1
Alex Williamson
AppliedQuoted 107 lines · On Tue, 20 Feb 2024 17:20:52 +0530 <[email protected]> wrote:
From: Ankit Agrawal <[email protected]>
NVIDIA's upcoming Grace Hopper Superchip provides a PCI-like device for the on-chip GPU that is the logical OS representation of the internal proprietary chip-to-chip cache coherent interconnect.
The device is peculiar compared to a real PCI device in that whilst there is a real 64b PCI BAR1 (comprising region 2 & region 3) on the device, it is not used to access device memory once the faster chip-to-chip interconnect is initialized (occurs at the time of host system boot). The device memory is accessed instead using the chip-to-chip interconnect that is exposed as a contiguous physically addressable region on the host. Since the device memory is cache coherent with the CPU, it can be mmaped into the user VMA with a cacheable mapping and used like a regular RAM. The device memory is not added to the host kernel, but mapped directly as this reduces memory wastage due to struct pages.
There is also a requirement of a minimum reserved 1G uncached region (termed as resmem) to support the Multi-Instance GPU (MIG) feature [1]. This is to work around a HW defect. Based on [2], the requisite properties (uncached, unaligned access) can be achieved through a VM mapping (S1) of
NORMAL_NCand host (S2) mapping with MemAttr[2:0]=0b101. To provide a different non-cached property to the reserved 1G region, it needs to be carved out from the device memory and mapped as a separate region in Qemu VMA withpgprot_writecombine().pgprot_writecombine()sets the Qemu VMA page properties (pgprot) asNORMAL_NC.Provide a VFIO PCI variant driver that adapts the unique device memory representation into a more standard PCI representation facing userspace.
The variant driver exposes these two regions - the non-cached reserved (resmem) and the cached rest of the device memory (termed as usemem) as separate VFIO 64b BAR regions. This is divergent from the baremetal approach, where the device memory is exposed as a device memory region. The decision for a different approach was taken in view of the fact that it would necessiate additional code in Qemu to discover and insert those regions in the VM IPA, along with the additional VM ACPI DSDT changes to communiate the device memory region IPA to the VM workloads. Moreover, this behavior would have to be added to a variety of emulators (beyond top of tree Qemu) out there desiring grace hopper support.
Since the device implements 64-bit BAR0, the VFIO PCI variant driver maps the uncached carved out region to the next available PCI BAR (i.e. comprising of region 2 and 3). The cached device memory aperture is assigned BAR region 4 and 5. Qemu will then naturally generate a PCI device in the VM with the uncached aperture reported as BAR2 region, the cacheable as BAR4. The variant driver provides emulation for these fake BARs' PCI config space offset registers.
The hardware ensures that the system does not crash when the memory is accessed with the memory enable turned off. It synthesis ~0 reads and dropped writes on such access. So there is no need to support the disablement/enablement of BAR through
PCI_COMMANDconfig space register.The memory layout on the host looks like the following:
devmem (memlength)|--------------------------------------------------| |-------------cached------------------------|--NC--| | | usemem.memphys resmem.memphysPCI BARs need to be aligned to the power-of-2, but the actual memory on the device may not. A read or write access to the physical address from the last device PFN up to the next power-of-2 aligned physical address results in reading ~0 and dropped writes. Note that the GPU device driver [6] is capable of knowing the exact device memory size through separate means. The device memory size is primarily kept in the system ACPI tables for use by the VFIO PCI variant module.
Note that the usemem memory is added by the VM Nvidia device driver [5] to the VM kernel as memblocks. Hence make the usable memory size memblock (
MEMBLK_SIZE) aligned. This is a hardwired ABI value between the GPU FW and VFIO driver. The VM device driver make use of the same value for its calculation to determine USEMEM size.Currently there is no provision in KVM for a S2 mapping with MemAttr[2:0]=0b101, but there is an ongoing effort to provide the same [3]. As previously mentioned, resmem is mapped
pgprot_writecombine(), that sets the Qemu VMA page properties (pgprot) asNORMAL_NC. Using the proposed changes in [3] and [4], KVM marks the region with MemAttr[2:0]=0b101 in S2.If the device memory properties are not present, the driver registers the vfio-pci-core function pointers. Since there are no ACPI memory properties generated for the VM, the variant driver inside the VM will only use the vfio-pci-core ops and hence try to map the BARs as non cached. This is not a problem as the CPUs have FWB enabled which blocks the VM mapping's ability to override the cacheability set by the host mapping.
This goes along with a qemu series [6] to provides the necessary implementation of the Grace Hopper Superchip firmware specification so that the guest operating system can see the correct ACPI modeling for the coherent GPU device. Verified with the CUDA workload in the VM.
[1] https://www.nvidia.com/en-in/technologies/multi-instance-gpu/
[2] section D8.5.5 of https://developer.arm.com/documentation/ddi0487/latest/
[3] https://lore.kernel.org/all/[email protected]/
[4] https://lore.kernel.org/all/[email protected]/
[5] https://github.com/NVIDIA/open-gpu-kernel-modules
[6] https://lore.kernel.org/all/[email protected]/Applied over v6.8-rc5.
Signed-off-by Aniket Agashe <[email protected]>Signed-off-by Ankit Agrawal <[email protected]>---
[snip]
Quoted 5 lines
Ankit Agrawal (3):
vfio/pci: rename and exportdo_io_rw()
vfio/pci: rename and exportrange_intersect_rangevfio/nvgrace-gpu: Add vfio pci variant module for grace hopper
Quoted 15 lines
MAINTAINERS | 16 +- drivers/vfio/pci/Kconfig | 2 + drivers/vfio/pci/Makefile | 2 + drivers/vfio/pci/nvgrace-gpu/Kconfig | 10 + drivers/vfio/pci/nvgrace-gpu/Makefile | 3 + drivers/vfio/pci/nvgrace-gpu/main.c | 879 ++++++++++++++++++++++++++ drivers/vfio/pci/vfio_pci_config.c | 42 ++ drivers/vfio/pci/vfio_pci_rdwr.c | 16 +- drivers/vfio/pci/virtio/main.c | 72 +-- include/linux/vfio_pci_core.h | 10 +- 10 files changed, 993 insertions(+), 59 deletions(-) create mode 100644 drivers/vfio/pci/nvgrace-gpu/Kconfig create mode 100644 drivers/vfio/pci/nvgrace-gpu/Makefile create mode 100644 drivers/vfio/pci/nvgrace-gpu/main.c
Applied to vfio next branch for v6.9. Thanks,
Alex
Ankit Agrawal
maintainerQuoted 21 lines
Quoted 15 lines
Ankit Agrawal (3):
vfio/pci: rename and exportdo_io_rw()
vfio/pci: rename and exportrange_intersect_rangevfio/nvgrace-gpu: Add vfio pci variant module for grace hopperMAINTAINERS | 16 +- drivers/vfio/pci/Kconfig | 2 + drivers/vfio/pci/Makefile | 2 + drivers/vfio/pci/nvgrace-gpu/Kconfig | 10 +
drivers/vfio/pci/nvgrace-gpu/Makefile| 3 + drivers/vfio/pci/nvgrace-gpu/main.c | 879 ++++++++++++++++++++++++++ drivers/vfio/pci/vfio_pci_config.c | 42 ++ drivers/vfio/pci/vfio_pci_rdwr.c | 16 +- drivers/vfio/pci/virtio/main.c | 72 +-- include/linux/vfio_pci_core.h | 10 +-Quoted 5 lines
10 files changed, 993 insertions(+), 59 deletions(-)create mode 100644
drivers/vfio/pci/nvgrace-gpu/Kconfigcreate mode 100644drivers/vfio/pci/nvgrace-gpu/Makefilecreate mode 100644drivers/vfio/pci/nvgrace-gpu/main.c
Quoted 3 lines
Applied to vfio next branch for v6.9. Thanks,
Alex
Thanks Alex! Appreciate this along with your guidance and help in the reviews.