Skip to contentSkip to conversation

vfio/nvgpu: Add vfio pci variant module for grace hopper

PATCH Superseded [email protected] 5 participants 10 messages Oct 7, 2023 – Oct 13, 2023

Subsystems (4)

v11 of 19 Superseded Latest is v19 →

lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

PATCH v11 1/1 vfio/nvgpu: 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 cache coherent interconnect.

This representation has a number of limitations compared to a real PCI device, in particular, it does not model the coherent GPU memory aperture as a PCI config space BAR, and PCI doesn't know anything about cacheable memory types.

Provide a VFIO PCI variant driver that adapts the unique PCI representation into a more standard PCI representation facing userspace. The GPU memory aperture is obtained from ACPI using device_property_read_u64(), according to the FW specification, and exported to userspace as a separate VFIO_REGION. Since the device implements only one 64-bit BAR (BAR0), the GPU memory aperture is mapped to the next available PCI BAR (BAR2). Qemu will then naturally generate a PCI device in the VM with two 64-bit BARs (where the cacheable aperture reported in BAR2).

Since this memory region is actually cache coherent with the CPU, the VFIO variant driver will mmap it into VMA using a cacheable mapping. The mapping is done using remap_pfn_range().

PCI BAR are 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.

This goes along with a qemu series 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.
https://lore.kernel.org/all/[email protected]/

This patch is split from a patch series being pursued separately:
https://lore.kernel.org/lkml/[email protected]/

Applied over next-20231005.

---

Link for v10:
https://lore.kernel.org/all/[email protected]/

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.
- Fixed memory leak and added suggested boundary checks on device memory mapping.
- Added code to read all Fs and ignored write on region outside of the physical memory.
- Other miscellaneous cleanup suggestions.

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.

---
 MAINTAINERS                           |   6 +
 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   | 481 ++++++++++++++++++++++++++
 6 files changed, 504 insertions(+)
 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 +6 -0
index e60e800ed91c..c47230def6f1 100644
@@ -22770,6 +22770,12 @@ L:	[email protected]
 S:	Maintained
 F:	drivers/vfio/platform/
 
+VFIO NVIDIA GRACE GPU DRIVER
+M:	Ankit Agrawal <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	drivers/vfio/pci/nvgrace-gpu/
+
 VGA_SWITCHEROO
 R:	Lukas Wunner <[email protected]>
 S:	Maintained
drivers/vfio/pci/Kconfig +2 -0
index 8125e5f37832..2456210e85f1 100644
@@ -65,4 +65,6 @@ source "drivers/vfio/pci/hisilicon/Kconfig"
 
 source "drivers/vfio/pci/pds/Kconfig"
 
+source "drivers/vfio/pci/nvgrace-gpu/Kconfig"
+
 endmenu
drivers/vfio/pci/Makefile +2 -0
index 45167be462d8..1352c65e568a 100644
@@ -13,3 +13,5 @@ obj-$(CONFIG_MLX5_VFIO_PCI)           += mlx5/
 obj-$(CONFIG_HISI_ACC_VFIO_PCI) += hisilicon/
 
 obj-$(CONFIG_PDS_VFIO_PCI) += pds/
+
+obj-$(CONFIG_NVGRACE_GPU_VFIO_PCI) += nvgrace-gpu/
drivers/vfio/pci/nvgrace-gpu/Kconfig new file +10 -0
new file mode 100644
index 000000000000..936e88d8d41d
@@ -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 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
new file mode 100644
index 000000000000..3ca8c187897a
@@ -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 +481 -0

481 lines not on this page · raw message

Signature · 1 line
2.17.1
#1 lore raw
Quoted 5 lines · On Sun, Oct 08, 2023 at 01:52:54AM +0530, [email protected] wrote:

PCI BAR are 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.

Reading garbage or padding in that case?

Confused...

Signature · 1 line
An old man doll... just what I always wanted! - Clara
#2 lore raw
Quoted 10 lines · On Sun, 8 Oct 2023 07:06:41 +0700 Bagas Sanjaya <[email protected]> wrote:
Quoted 5 lines · On Sun, Oct 08, 2023 at 01:52:54AM +0530, [email protected] wrote:

PCI BAR are 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.

Reading garbage or padding in that case?

Confused...

The coherent memory size is rounded to a power-of-2 to be compliant with PCI BAR semantics, but reading beyond the implemented size fills the return buffer with -1 data, as is common on many platforms when reading from an unimplemented section of the address space. Thanks,

Alex

#3 lore raw
Quoted 12 lines · Alex Williamson
Quoted 9 lines · On Sun, 8 Oct 2023 07:06:41 +0700 Bagas Sanjaya <[email protected]> wrote:
Quoted 5 lines · On Sun, Oct 08, 2023 at 01:52:54AM +0530, [email protected] wrote:

PCI BAR are 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.

Reading garbage or padding in that case?

Quoted 9 lines
Quoted 1 line

Confused...

The coherent memory size is rounded to a power-of-2 to be compliant with PCI BAR semantics, but reading beyond the implemented size fills the return buffer with -1 data, as is common on many platforms when reading from an unimplemented section of the address space. Thanks,

Alex

Thanks for the explanation!

Signature · 1 line
An old man doll... just what I always wanted! - Clara
reviewer
#4 lore raw
In reply to [email protected]
Quoted 8 lines

From: [email protected] <[email protected]>
Sent: Sunday, October 8, 2023 4:23 AM

PCI BAR are 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.

my question to v10 was not answered. posted again:
--
Though the variant driver emulates the access to the offset beyond the available memory size, how does the userspace driver or the guest learn to know the actual size and avoid using the invalid hole to hold valid data?

#5 lore raw
Quoted 9 lines · Kevin
Quoted 8 lines

From: [email protected] <[email protected]>
Sent: Sunday, October 8, 2023 4:23 AM

PCI BAR are 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.

Quoted 6 lines

my question to v10 was not answered. posted again:
--
Though the variant driver emulates the access to the offset beyond the available memory size, how does the userspace driver or the guest learn to know the actual size and avoid using the invalid hole to hold valid data?

The device FW knows and tells the VM.

Jason

reviewer
#6 lore raw
Quoted 13 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, October 10, 2023 7:34 PM

Quoted 9 lines · Kevin
Quoted 8 lines

From: [email protected] <[email protected]>
Sent: Sunday, October 8, 2023 4:23 AM

PCI BAR are 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.

Quoted 9 lines
Quoted 6 lines

my question to v10 was not answered. posted again:
--
Though the variant driver emulates the access to the offset beyond the available memory size, how does the userspace driver or the guest learn to know the actual size and avoid using the invalid hole to hold valid data?

The device FW knows and tells the VM.

This driver reads the size info from ACPI and records it as nvdev->memlength.

But nvdev->memlength is not exposed to the userspace. How does the virtual FW acquires this knowledge and then report it to the VM?

One option is to indirectly decide the size based on sparse mmap info. But that is kind of a misuse of sparse mmap then such misuse better fits to be a device specific region as Alex suggested.

Did I overlook an important fact in the overall picture?

#7 lore raw
Quoted 13 lines · Kevin
Quoted 13 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, October 10, 2023 7:34 PM

Quoted 9 lines · Kevin
Quoted 8 lines

From: [email protected] <[email protected]>
Sent: Sunday, October 8, 2023 4:23 AM

PCI BAR are 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.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

my question to v10 was not answered. posted again:

The device FW knows and tells the VM.

This driver reads the size info from ACPI and records it as nvdev->memlength.

Yes, the ACPI tables have a copy of the size.

Quoted 2 lines

But nvdev->memlength is not exposed to the userspace. How does the virtual FW acquires this knowledge and then report it to the VM?

It isn't virtual FW, I said device FW. The device itself knows how it is configured and it can report details about the the memory space. The VM just DMA's a RPC and asks it.

Jason

reviewer Reviewed-by
#8 lore raw
Quoted 20 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, October 11, 2023 8:00 PM

Quoted 9 lines · Kevin
Quoted 9 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, October 10, 2023 7:34 PM

Quoted 5 lines · Kevin
Quoted 5 lines

From: [email protected] <[email protected]>
Sent: Sunday, October 8, 2023 4:23 AM

PCI BAR are 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

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 3 lines

last device PFN up to the next power-of-2 aligned physical address results in reading ~0 and dropped writes.

my question to v10 was not answered. posted again:

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

The device FW knows and tells the VM.

This driver reads the size info from ACPI and records it as nvdev->memlength.

Yes, the ACPI tables have a copy of the size.

So the ACPI copy is mainly introduced for the use of VFIO given the real device driver can already retrieve it from device FW?

Quoted 9 lines
Quoted 2 lines

But nvdev->memlength is not exposed to the userspace. How does the

virtual

Quoted 1 line

FW acquires this knowledge and then report it to the VM?

It isn't virtual FW, I said device FW. The device itself knows how it is configured and it can report details about the the memory space. The VM just DMA's a RPC and asks it.

Interesting. Perhaps this message should be included in the commit message to help compose a clear picture.

With that,

author maintainer
#9 lore raw

Thanks Kevin for the review.

Quoted 13 lines
Quoted 13 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

PCI BAR are 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

Quoted 9 lines
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

last device PFN up to the next power-of-2 aligned physical address results in reading ~0 and dropped writes.

my question to v10 was not answered. posted again:

The device FW knows and tells the VM.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

This driver reads the size info from ACPI and records it as nvdev->memlength.

Yes, the ACPI tables have a copy of the size.

So the ACPI copy is mainly introduced for the use of VFIO given the real device driver can already retrieve it from device FW?

Yes, the device mem size in ACPI DSDT is kept primarily for the vfio use.

Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

But nvdev->memlength is not exposed to the userspace. How does the

virtual

Quoted 1 line

FW acquires this knowledge and then report it to the VM?

Quoted 7 lines
Quoted 4 lines

It isn't virtual FW, I said device FW. The device itself knows how it is configured and it can report details about the the memory space. The VM just DMA's a RPC and asks it.

Interesting. Perhaps this message should be included in the commit message to help compose a clear picture.

I will add it, yes.

Quoted 3 lines

With that,

Thanks!