vfio/nvgrace-gpu: Enable grace blackwell boards
[email protected]
maintainerv3 cover vfio/nvgrace-gpu: Enable grace blackwell boards
From: Ankit Agrawal <[email protected]>
NVIDIA's recently introduced Grace Blackwell (GB) Superchip in continuation with the Grace Hopper (GH) superchip that provides a cache coherent access to CPU and GPU to each other's memory with an internal proprietary chip-to-chip (C2C) cache coherent interconnect. The in-tree nvgrace-gpu driver manages the GH devices. The intention is to extend the support to the new Grace Blackwell boards.
There is a HW defect on GH to support the Multi-Instance GPU (MIG) feature [1] that necessiated the presence of a 1G carved out from the device memory and mapped uncached. The 1G region is shown as a fake BAR (comprising region 2 and 3) to workaround the issue.
The GB systems differ from GH systems in the following aspects.
1. The aforementioned HW defect is fixed on GB systems.
2. There is a usable BAR1 (region 2 and 3) on GB systems for the GPUdirect RDMA feature [2].
This patch series accommodate those GB changes by showing the real physical device BAR1 (region2 and 3) to the VM instead of the fake one. This takes care of both the differences.
The presence of the fix for the HW defect is communicated by the firmware through a DVSEC PCI config register. The module reads this to take a different codepath on GB vs GH.
To improve system bootup time, HBM training is moved out of UEFI in GB system. Poll for the register indicating the training state. Also check the C2C link status if it is ready. Fail the probe if either fails.
Applied over next-20241220 and the required KVM patch (under review on the mailing list) to map the GPU device memory as cacheable [3]. Tested on the Grace Blackwell platform by booting up VM, loading NVIDIA module [4] and running nvidia-smi in the VM.
To run CUDA workloads, there is a dependency on the IOMMUFD and the Nested Page Table patches being worked on separately by Nicolin Chen. ([email protected]). NVIDIA has provided git repositories which includes all the requisite kernel [5] and Qemu [6] patches in case one wants to try.
v2 -> v3
* Incorporated Alex Williamson's suggestion to simplify patch 2/3.
* Updated the code in 3/3 to use time_after() and other miscellaneous suggestions from Alex Williamson.
v1 -> v2
* Rebased to next-20241220.
v1:
Ankit Agrawal (3):
vfio/nvgrace-gpu: Read dvsec register to determine need for uncached
resmem
vfio/nvgrace-gpu: Expose the blackwell device PF BAR1 to the VM vfio/nvgrace-gpu: Check the HBM training and C2C link status
drivers/vfio/pci/nvgrace-gpu/main.c | 150 +++++++++++++++++++++++----- 1 file changed, 126 insertions(+), 24 deletions(-)
Signature · 1 line
2.34.1
[email protected]
maintainerPATCH v3 1/3 vfio/nvgrace-gpu: Read dvsec register to determine need for uncached resmem
From: Ankit Agrawal <[email protected]>
NVIDIA's recently introduced Grace Blackwell (GB) Superchip is a continuation with the Grace Hopper (GH) superchip that provides a cache coherent access to CPU and GPU to each other's memory with an internal proprietary chip-to-chip cache coherent interconnect.
There is a HW defect on GH systems to support the Multi-Instance GPU (MIG) feature [1] that necessiated the presence of a 1G region with uncached mapping carved out from the device memory. The 1G region is shown as a fake BAR (comprising region 2 and 3) to workaround the issue. This is fixed on the GB systems.
The presence of the fix for the HW defect is communicated by the device firmware through the DVSEC PCI config register with ID 3. The module reads this to take a different codepath on GB vs GH.
Scan through the DVSEC registers to identify the correct one and use it to determine the presence of the fix. Save the value in the device's nvgrace_gpu_pci_core_device structure.
--- drivers/vfio/pci/nvgrace-gpu/main.c | 30 +++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+)
drivers/vfio/pci/nvgrace-gpu/main.c +30 -0
@@ -23,6 +23,11 @@/* A hardwired and constant ABI value between the GPU FW and VFIO driver. */ #define MEMBLK_SIZE SZ_512M +#define DVSEC_BITMAP_OFFSET 0xA +#define MIG_SUPPORTED_WITH_CACHED_RESMEM BIT(0) + +#define GPU_CAP_DVSEC_REGISTER 3 +/* * The state of the two device memory region - resmem and usemem - is * saved as struct mem_region.
@@ -46,6 +51,7 @@ struct nvgrace_gpu_pci_core_device {struct mem_region resmem;/* Lock to control device memory kernel mapping */ struct mutex remap_lock; +bool has_mig_hw_bug_fix; };static void nvgrace_gpu_init_fake_bar_emu_regs (struct vfio_device *core_vdev)
@@ -812,6 +818,26 @@ nvgrace_gpu_init_nvdev_struct(struct pci_dev *pdev,return ret; } +static bool nvgrace_gpu_has_mig_hw_bug_fix (struct pci_dev *pdev) +{ +int pcie_dvsec; +u16 dvsec_ctrl16; + + pcie_dvsec =pci_find_dvsec_capability (pdev,PCI_VENDOR_ID_NVIDIA , +GPU_CAP_DVSEC_REGISTER ); + +if (pcie_dvsec) { +pci_read_config_word (pdev, + pcie_dvsec +DVSEC_BITMAP_OFFSET , + &dvsec_ctrl16); + +if (dvsec_ctrl16 &MIG_SUPPORTED_WITH_CACHED_RESMEM ) +return true; + } + +return false; +} +static int nvgrace_gpu_probe (struct pci_dev *pdev,const struct pci_device_id *id) {
@@ -832,6 +858,8 @@ static int nvgrace_gpu_probe(struct pci_dev *pdev,dev_set_drvdata (&pdev->dev, &nvdev->core_device);if (ops == &nvgrace_gpu_pci_ops) { + nvdev->has_mig_hw_bug_fix =nvgrace_gpu_has_mig_hw_bug_fix (pdev); +/* * Device memory properties are identified in the host ACPI * table. Set the nvgrace_gpu_pci_core_device structure.
@@ -868,6 +896,8 @@ static const struct pci_device_id nvgrace_gpu_vfio_pci_table[] = { { PCI_DRIVER_OVERRIDE_DEVICE_VFIO(PCI_VENDOR_ID_NVIDIA, 0x2345) },/* GH200 SKU */ { PCI_DRIVER_OVERRIDE_DEVICE_VFIO(PCI_VENDOR_ID_NVIDIA, 0x2348) }, +/* GB200 SKU */ + { PCI_DRIVER_OVERRIDE_DEVICE_VFIO(PCI_VENDOR_ID_NVIDIA, 0x2941) }, {} };
Signature · 1 line
2.34.1
[email protected]
maintainerPATCH v3 2/3 vfio/nvgrace-gpu: Expose the blackwell device PF BAR1 to the VM
In reply to [email protected]From: Ankit Agrawal <[email protected]>
There is a HW defect on Grace Hopper (GH) to support the Multi-Instance GPU (MIG) feature [1] that necessiated the presence of a 1G region carved out from the device memory and mapped as uncached. The 1G region is shown as a fake BAR (comprising region 2 and 3) to workaround the issue.
The Grace Blackwell systems (GB) differ from GH systems in the following aspects:
1. The aforementioned HW defect is fixed on GB systems.
2. There is a usable BAR1 (region 2 and 3) on GB systems for the GPUdirect RDMA feature [2].
This patch accommodate those GB changes by showing the 64b physical device BAR1 (region2 and 3) to the VM instead of the fake one. This takes care of both the differences.
Moreover, the entire device memory is exposed on GB as cacheable to the VM as there is no carveout required.
--- drivers/vfio/pci/nvgrace-gpu/main.c | 65 ++++++++++++++++++----------- 1 file changed, 41 insertions(+), 24 deletions(-)
drivers/vfio/pci/nvgrace-gpu/main.c +41 -24
@@ -17,9 +17,6 @@#define RESMEM_REGION_INDEX VFIO_PCI_BAR2_REGION_INDEX#define USEMEM_REGION_INDEX VFIO_PCI_BAR4_REGION_INDEX -/* Memory size expected as non cached and reserved by the VM driver */ -#define RESMEM_SIZE SZ_1G -/* A hardwired and constant ABI value between the GPU FW and VFIO driver. */ #define MEMBLK_SIZE SZ_512M
@@ -72,7 +69,7 @@ nvgrace_gpu_memregion(int index,if (index ==USEMEM_REGION_INDEX )return &nvdev->usemem; -if (index ==RESMEM_REGION_INDEX ) +if (nvdev->resmem.memlength && index ==RESMEM_REGION_INDEX )return &nvdev->resmem;return NULL ;
@@ -757,21 +754,31 @@ nvgrace_gpu_init_nvdev_struct(struct pci_dev *pdev, u64 memphys, u64 memlength) { int ret = 0; +u64 resmem_size =0 ;/* -* The VM GPU device driver needs a non-cacheable region to support -* the MIG feature. Since the device memory is mapped as NORMAL cached, -* carve out a region from the end with a different NORMAL_NC -* property (called as reserved memory and represented as resmem). This -* region then is exposed as a 64b BAR (region 2 and 3) to the VM, while -* exposing the rest (termed as usable memory and represented using usemem) -* as cacheable 64b BAR (region 4 and 5). +* On Grace Hopper systems, the VM GPU device driver needs a non-cacheable +* region to support the MIG feature owing to a hardware bug. Since the +* device memory is mapped as NORMAL cached, carve out a region from the end +* with a different NORMAL_NC property (called as reserved memory and +* represented as resmem). This region then is exposed as a 64b BAR +* (region 2 and 3) to the VM, while exposing the rest (termed as usable +* memory and represented using usemem) as cacheable 64b BAR (region 4 and 5). * * devmem (memlength) * |-------------------------------------------------| * | | * usemem.memphys resmem.memphys +* +* This hardware bug is fixed on the Grace Blackwell platforms and the +* presence of fix can be determined through nvdev->has_mig_hw_bug_fix. +* Thus on systems with the hardware fix, there is no need to partition +* the GPU device memory and the entire memory is usable and mapped as +* NORMAL cached. */ +if (!nvdev->has_mig_hw_bug_fix) + resmem_size =SZ_1G ; + nvdev->usemem.memphys = memphys;/*
@@ -780,23 +787,30 @@ nvgrace_gpu_init_nvdev_struct(struct pci_dev *pdev,* memory (usemem) is added to the kernel for usage by the VM * workloads. Make the usable memory size memblock aligned. */ -if (check_sub_overflow (memlength,RESMEM_SIZE , +if (check_sub_overflow (memlength, resmem_size, &nvdev->usemem.memlength)) { ret = -EOVERFLOW ; goto done; } -/* -* The USEMEM part of the device memory has to be MEMBLK_SIZE -* aligned. This is a hardwired ABI value between the GPU FW and -* VFIO driver. The VM device driver is also aware of it and make -* use of the value for its calculation to determine USEMEM size. -*/ - nvdev->usemem.memlength =round_down (nvdev->usemem.memlength, -MEMBLK_SIZE ); -if (nvdev->usemem.memlength ==0 ) { - ret = -EINVAL ; - goto done; +if (!nvdev->has_mig_hw_bug_fix) { +/* +* If the device memory is split to workaround the MIG bug, +* the USEMEM part of the device memory has to be MEMBLK_SIZE +* aligned. This is a hardwired ABI value between the GPU FW and +* VFIO driver. The VM device driver is also aware of it and make +* use of the value for its calculation to determine USEMEM size. +* +* If the hardware has the fix for MIG, there is no requirement +* for splitting the device memory to create RESMEM. The entire +* device memory is usable and will be USEMEM. +*/ + nvdev->usemem.memlength =round_down (nvdev->usemem.memlength, +MEMBLK_SIZE ); +if (nvdev->usemem.memlength ==0 ) { + ret = -EINVAL ; + goto done; + } } if ((check_add_overflow(nvdev->usemem.memphys,
@@ -813,7 +827,10 @@ nvgrace_gpu_init_nvdev_struct(struct pci_dev *pdev,* the BAR size for them. */ nvdev->usemem.bar_size =roundup_pow_of_two (nvdev->usemem.memlength); - nvdev->resmem.bar_size =roundup_pow_of_two (nvdev->resmem.memlength); + +if (nvdev->resmem.memlength) + nvdev->resmem.bar_size = +roundup_pow_of_two (nvdev->resmem.memlength); done:return ret; }
Signature · 1 line
2.34.1
Re: [PATCH v3 2/3] vfio/nvgrace-gpu: Expose the blackwell device PF BAR1 to the VM
Quoted 122 lines · On Fri, 17 Jan 2025 15:23:33 +0000 <[email protected]> wrote:
From: Ankit Agrawal <[email protected]>
There is a HW defect on Grace Hopper (GH) to support the Multi-Instance GPU (MIG) feature [1] that necessiated the presence of a 1G region carved out from the device memory and mapped as uncached. The 1G region is shown as a fake BAR (comprising region 2 and 3) to workaround the issue.
The Grace Blackwell systems (GB) differ from GH systems in the following aspects:
1. The aforementioned HW defect is fixed on GB systems.
2. There is a usable BAR1 (region 2 and 3) on GB systems for the GPUdirect RDMA feature [2].This patch accommodate those GB changes by showing the 64b physical device BAR1 (region2 and 3) to the VM instead of the fake one. This takes care of both the differences.
Moreover, the entire device memory is exposed on GB as cacheable to the VM as there is no carveout required.
Suggested-by Alex Williamson <[email protected]>Signed-off-by Ankit Agrawal <[email protected]>--- drivers/vfio/pci/nvgrace-gpu/main.c | 65 ++++++++++++++++++----------- 1 file changed, 41 insertions(+), 24 deletions(-)drivers/vfio/pci/nvgrace-gpu/main.c +27
-23@@ -17,9 +17,6 @@#define RESMEM_REGION_INDEX VFIO_PCI_BAR2_REGION_INDEX#define USEMEM_REGION_INDEX VFIO_PCI_BAR4_REGION_INDEX -/* Memory size expected as non cached and reserved by the VM driver */ -#define RESMEM_SIZE SZ_1G -/* A hardwired and constant ABI value between the GPU FW and VFIO driver. */ #define MEMBLK_SIZE SZ_512M@@ -72,7 +69,7 @@ nvgrace_gpu_memregion(int index,if (index ==USEMEM_REGION_INDEX )return &nvdev->usemem; -if (index ==RESMEM_REGION_INDEX ) +if (nvdev->resmem.memlength && index ==RESMEM_REGION_INDEX )return &nvdev->resmem;return NULL ;@@ -757,21 +754,31 @@ nvgrace_gpu_init_nvdev_struct(struct pci_dev *pdev, u64 memphys, u64 memlength) { int ret = 0; +u64 resmem_size =0 ;/* -* The VM GPU device driver needs a non-cacheable region to support -* the MIG feature. Since the device memory is mapped as NORMAL cached, -* carve out a region from the end with a different NORMAL_NC -* property (called as reserved memory and represented as resmem). This -* region then is exposed as a 64b BAR (region 2 and 3) to the VM, while -* exposing the rest (termed as usable memory and represented using usemem) -* as cacheable 64b BAR (region 4 and 5). +* On Grace Hopper systems, the VM GPU device driver needs a non-cacheable +* region to support the MIG feature owing to a hardware bug. Since the +* device memory is mapped as NORMAL cached, carve out a region from the end +* with a different NORMAL_NC property (called as reserved memory and +* represented as resmem). This region then is exposed as a 64b BAR +* (region 2 and 3) to the VM, while exposing the rest (termed as usable +* memory and represented using usemem) as cacheable 64b BAR (region 4 and 5). * * devmem (memlength) * |-------------------------------------------------| * | | * usemem.memphys resmem.memphys +* +* This hardware bug is fixed on the Grace Blackwell platforms and the +* presence of fix can be determined through nvdev->has_mig_hw_bug_fix. +* Thus on systems with the hardware fix, there is no need to partition +* the GPU device memory and the entire memory is usable and mapped as +* NORMAL cached. */ +if (!nvdev->has_mig_hw_bug_fix) + resmem_size =SZ_1G ; + nvdev->usemem.memphys = memphys;/* @@ -780,23 +787,30 @@ nvgrace_gpu_init_nvdev_struct(struct pci_dev *pdev,* memory (usemem) is added to the kernel for usage by the VM * workloads. Make the usable memory size memblock aligned. */ -if (check_sub_overflow (memlength,RESMEM_SIZE , +if (check_sub_overflow (memlength, resmem_size, &nvdev->usemem.memlength)) { ret = -EOVERFLOW ; goto done; } -/* -* The USEMEM part of the device memory has to be MEMBLK_SIZE -* aligned. This is a hardwired ABI value between the GPU FW and -* VFIO driver. The VM device driver is also aware of it and make -* use of the value for its calculation to determine USEMEM size. -*/ - nvdev->usemem.memlength =round_down (nvdev->usemem.memlength, -MEMBLK_SIZE ); -if (nvdev->usemem.memlength ==0 ) { - ret = -EINVAL ; - goto done; +if (!nvdev->has_mig_hw_bug_fix) { +/* +* If the device memory is split to workaround the MIG bug, +* the USEMEM part of the device memory has to be MEMBLK_SIZE +* aligned. This is a hardwired ABI value between the GPU FW and +* VFIO driver. The VM device driver is also aware of it and make +* use of the value for its calculation to determine USEMEM size. +*
drivers/vfio/pci/nvgrace-gpu/main.c nvgrace_gpu_init_nvdev_struct(struct pci_dev *pdev, · line 813
Quoted 10 lines
drivers/vfio/pci/nvgrace-gpu/main.c +10
-0+* If the hardware has the fix for MIG, there is no requirement +* for splitting the device memory to create RESMEM. The entire +* device memory is usable and will be USEMEM. +*/ + nvdev->usemem.memlength =round_down (nvdev->usemem.memlength, +MEMBLK_SIZE ); +if (nvdev->usemem.memlength ==0 ) { + ret = -EINVAL ; + goto done; + }
Why does this operation need to be predicated on the buggy device? Does GB have memory that's not a multiple of 512MB? I was expecting this would be a no-op on GB and therefore wouldn't need to be conditional. Thanks,
Alex
Quoted 15 lines
drivers/vfio/pci/nvgrace-gpu/main.c +4
-1} if ((check_add_overflow(nvdev->usemem.memphys,@@ -813,7 +827,10 @@ nvgrace_gpu_init_nvdev_struct(struct pci_dev *pdev,* the BAR size for them. */ nvdev->usemem.bar_size =roundup_pow_of_two (nvdev->usemem.memlength); - nvdev->resmem.bar_size =roundup_pow_of_two (nvdev->resmem.memlength); + +if (nvdev->resmem.memlength) + nvdev->resmem.bar_size = +roundup_pow_of_two (nvdev->resmem.memlength); done:return ret; }
Ankit Agrawal
maintainerRe: [PATCH v3 2/3] vfio/nvgrace-gpu: Expose the blackwell device PF BAR1 to the VM
Quoted 19 lines
Quoted 10 lines
+ if (!nvdev->has_mig_hw_bug_fix) {
+ /*
+ * If the device memory is split to workaround the MIG bug, + * the USEMEM part of the device memory has to beMEMBLK_SIZE+ * aligned. This is a hardwired ABI value between the GPU FW and + * VFIO driver. The VM device driver is also aware of it and make + * use of the value for its calculation to determine USEMEM size. + *
+ * If the hardware has the fix for MIG, there is no requirement + * for splitting the device memory to create RESMEM. The entireQuoted 8 lines
+ * device memory is usable and will be USEMEM.
+ */
+nvdev->usemem.memlength= round_down(nvdev->usemem.memlength, +MEMBLK_SIZE); + if (nvdev->usemem.memlength== 0) {
+ ret =-EINVAL;
+ goto done;
+ }
Quoted 6 lines
Why does this operation need to be predicated on the buggy device? Does GB have memory that's not a multiple of 512MB? I was expecting this would be a no-op on GB and therefore wouldn't need to be conditional. Thanks,
Alex
Thanks Alex, yeah the device memory size is not necessarily 512M aligned.
[email protected]
maintainerPATCH v3 3/3 vfio/nvgrace-gpu: Check the HBM training and C2C link status
In reply to [email protected]From: Ankit Agrawal <[email protected]>
In contrast to Grace Hopper systems, the HBM training has been moved out of the UEFI on the Grace Blackwell systems. This reduces the system bootup time significantly.
The onus of checking whether the HBM training has completed thus falls on the module.
The HBM training status can be determined from a BAR0 register. Similarly, another BAR0 register exposes the status of the CPU-GPU chip-to-chip (C2C) cache coherent interconnect.
Based on testing, 30s is determined to be sufficient to ensure initialization completion on all the Grace based systems. Thus poll these register and check for 30s. If the HBM training is not complete or if the C2C link is not ready, fail the probe.
While the time is not required on Grace Hopper systems, it is beneficial to make the check to ensure the device is in an expected state. Hence keeping it generalized to both the generations.
--- drivers/vfio/pci/nvgrace-gpu/main.c | 55 +++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+)
drivers/vfio/pci/nvgrace-gpu/main.c +55 -0
@@ -5,6 +5,8 @@#include <linux/sizes.h> #include <linux/vfio_pci_core.h> +#include <linux/delay.h> +#include <linux/jiffies.h> /* * The device memory usable to the workloads running in the VM is cached
@@ -25,6 +27,13 @@#define GPU_CAP_DVSEC_REGISTER 3 +#define C2C_LINK_BAR0_OFFSET 0x1498 +#define HBM_TRAINING_BAR0_OFFSET 0x200BC +#define STATUS_READY 0xFF + +#define POLL_QUANTUM_MS 1000 +#define POLL_TIMEOUT_MS (30 * 1000) +/* * The state of the two device memory region - resmem and usemem - is * saved as struct mem_region.
@@ -855,6 +864,48 @@ static bool nvgrace_gpu_has_mig_hw_bug_fix(struct pci_dev *pdev)return false; } +/* +* To reduce the system bootup time, the HBM training has +* been moved out of the UEFI on the Grace-Blackwell systems. +* +* The onus of checking whether the HBM training has completed +* thus falls on the module. The HBM training status can be +* determined from a BAR0 register. +* +* Similarly, another BAR0 register exposes the status of the +* CPU-GPU chip-to-chip (C2C) cache coherent interconnect. +* +* Poll these register and check for 30s. If the HBM training is +* not complete or if the C2C link is not ready, fail the probe. +* +* While the wait is not required on Grace Hopper systems, it +* is beneficial to make the check to ensure the device is in an +* expected state. +*/ +static int nvgrace_gpu_wait_device_ready (struct pci_dev *pdev) +{ +unsigned long timeout = jiffies +msecs_to_jiffies (POLL_TIMEOUT_MS ); +void __iomem *io; +int ret = -ETIME ; + + io =pci_iomap (pdev,0 ,0 ); +if (!io) +return -ENOMEM ; + +do { +if ((ioread32 (io +C2C_LINK_BAR0_OFFSET ) ==STATUS_READY ) && + (ioread32 (io +HBM_TRAINING_BAR0_OFFSET ) ==STATUS_READY )) { + ret =0 ; + goto reg_check_exit; + } +msleep (POLL_QUANTUM_MS ); + }while (!time_after (jiffies, timeout)); + +reg_check_exit: +pci_iounmap (pdev, io); +return ret; +} +static int nvgrace_gpu_probe (struct pci_dev *pdev,const struct pci_device_id *id) {
@@ -863,6 +914,10 @@ static int nvgrace_gpu_probe(struct pci_dev *pdev,u64 memphys, memlength;int ret; + ret =nvgrace_gpu_wait_device_ready (pdev); +if (ret) +return ret; + ret =nvgrace_gpu_fetch_memory_property (pdev, &memphys, &memlength);if (!ret) ops = &nvgrace_gpu_pci_ops;
Signature · 1 line
2.34.1
Re: [PATCH v3 3/3] vfio/nvgrace-gpu: Check the HBM training and C2C link status
Quoted 88 lines · On Fri, 17 Jan 2025 15:23:34 +0000 <[email protected]> wrote:
From: Ankit Agrawal <[email protected]>
In contrast to Grace Hopper systems, the HBM training has been moved out of the UEFI on the Grace Blackwell systems. This reduces the system bootup time significantly.
The onus of checking whether the HBM training has completed thus falls on the module.
The HBM training status can be determined from a BAR0 register. Similarly, another BAR0 register exposes the status of the CPU-GPU chip-to-chip (C2C) cache coherent interconnect.
Based on testing, 30s is determined to be sufficient to ensure initialization completion on all the Grace based systems. Thus poll these register and check for 30s. If the HBM training is not complete or if the C2C link is not ready, fail the probe.
While the time is not required on Grace Hopper systems, it is beneficial to make the check to ensure the device is in an expected state. Hence keeping it generalized to both the generations.
Signed-off-by Ankit Agrawal <[email protected]>--- drivers/vfio/pci/nvgrace-gpu/main.c | 55 +++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+)drivers/vfio/pci/nvgrace-gpu/main.c +39
-0@@ -5,6 +5,8 @@#include <linux/sizes.h> #include <linux/vfio_pci_core.h> +#include <linux/delay.h> +#include <linux/jiffies.h> /* * The device memory usable to the workloads running in the VM is cached @@ -25,6 +27,13 @@#define GPU_CAP_DVSEC_REGISTER 3 +#define C2C_LINK_BAR0_OFFSET 0x1498 +#define HBM_TRAINING_BAR0_OFFSET 0x200BC +#define STATUS_READY 0xFF + +#define POLL_QUANTUM_MS 1000 +#define POLL_TIMEOUT_MS (30 * 1000) +/* * The state of the two device memory region - resmem and usemem - is * saved as struct mem_region. @@ -855,6 +864,48 @@ static bool nvgrace_gpu_has_mig_hw_bug_fix(struct pci_dev *pdev)return false; } +/* +* To reduce the system bootup time, the HBM training has +* been moved out of the UEFI on the Grace-Blackwell systems. +* +* The onus of checking whether the HBM training has completed +* thus falls on the module. The HBM training status can be +* determined from a BAR0 register. +* +* Similarly, another BAR0 register exposes the status of the +* CPU-GPU chip-to-chip (C2C) cache coherent interconnect. +* +* Poll these register and check for 30s. If the HBM training is +* not complete or if the C2C link is not ready, fail the probe. +* +* While the wait is not required on Grace Hopper systems, it +* is beneficial to make the check to ensure the device is in an +* expected state. +*/ +static int nvgrace_gpu_wait_device_ready(struct pci_dev *pdev) +{ + unsigned long timeout = jiffies + msecs_to_jiffies(POLL_TIMEOUT_MS); + void __iomem *io; + int ret = -ETIME; + + io = pci_iomap(pdev, 0, 0); + if (!io) + return -ENOMEM; + + do { + if ((ioread32(io + C2C_LINK_BAR0_OFFSET) == STATUS_READY) &&
drivers/vfio/pci/nvgrace-gpu/main.c static bool nvgrace_gpu_has_mig_hw_bug_fix(struct pci_dev *pdev) · line 906
Quoted 10 lines
drivers/vfio/pci/nvgrace-gpu/main.c +10
-0+ (ioread32(io + HBM_TRAINING_BAR0_OFFSET) == STATUS_READY)) { + ret =0 ; + goto reg_check_exit; + } +msleep (POLL_QUANTUM_MS ); + }while (!time_after (jiffies, timeout)); + +reg_check_exit: +pci_iounmap (pdev, io); +return ret;
We're accessing device memory here but afaict the memory enable bit of the command register is in an indeterminate state. What happens if you use setpci to clear the memory enable bit or 'echo 0 > enable' before binding the driver? Thanks,
Alex
Quoted 16 lines
drivers/vfio/pci/nvgrace-gpu/main.c +6
-0+} +static int nvgrace_gpu_probe (struct pci_dev *pdev,const struct pci_device_id *id) {@@ -863,6 +914,10 @@ static int nvgrace_gpu_probe(struct pci_dev *pdev,u64 memphys, memlength;int ret; + ret =nvgrace_gpu_wait_device_ready (pdev); +if (ret) +return ret; + ret =nvgrace_gpu_fetch_memory_property (pdev, &memphys, &memlength);if (!ret) ops = &nvgrace_gpu_pci_ops;
Ankit Agrawal
maintainerRe: [PATCH v3 3/3] vfio/nvgrace-gpu: Check the HBM training and C2C link status
Quoted 47 lines
Quoted 31 lines
drivers/vfio/pci/nvgrace-gpu/main.c +30
-0+/* +* To reduce the system bootup time, the HBM training has +* been moved out of the UEFI on the Grace-Blackwell systems. +* +* The onus of checking whether the HBM training has completed +* thus falls on the module. The HBM training status can be +* determined from a BAR0 register. +* +* Similarly, another BAR0 register exposes the status of the +* CPU-GPU chip-to-chip (C2C) cache coherent interconnect. +* +* Poll these register and check for 30s. If the HBM training is +* not complete or if the C2C link is not ready, fail the probe. +* +* While the wait is not required on Grace Hopper systems, it +* is beneficial to make the check to ensure the device is in an +* expected state. +*/ +static int nvgrace_gpu_wait_device_ready (struct pci_dev *pdev) +{ + unsigned long timeout = jiffies + msecs_to_jiffies(POLL_TIMEOUT_MS); + void __iomem *io; + int ret = -ETIME; + + io = pci_iomap(pdev, 0, 0); + if (!io) + return -ENOMEM; + + do { + if ((ioread32(io + C2C_LINK_BAR0_OFFSET) == STATUS_READY) &&drivers/vfio/pci/nvgrace-gpu/main.c static bool nvgrace_gpu_has_mig_hw_bug_fix(struct pci_dev *pdev) · line 906
Quoted 10 lines
drivers/vfio/pci/nvgrace-gpu/main.c +10
-0+ (ioread32(io + HBM_TRAINING_BAR0_OFFSET) == STATUS_READY)) { + ret = 0; + goto reg_check_exit; + } + msleep(POLL_QUANTUM_MS); + } while (!time_after(jiffies, timeout)); + +reg_check_exit: + pci_iounmap(pdev, io); + return ret;We're accessing device memory here but afaict the memory enable bit of the command register is in an indeterminate state. What happens if you use setpci to clear the memory enable bit or 'echo 0 > enable' before binding the driver? Thanks,
Quoted 1 line
Alex
Hi Alex, sorry I didn't understand how we are accessing device memory here if the C2C_LINK_BAR0_OFFSET and HBM_TRAINING_BAR0_OFFSET are BAR0 regs. But anyways, I tried 'echo 0 > <sysfs_path>/enable' before device bind. I am not observing any issue and the bind goes through.
Or am I missing something?
Re: [PATCH v3 3/3] vfio/nvgrace-gpu: Check the HBM training and C2C link status
Quoted 46 lines · On Fri, 17 Jan 2025 19:19:42 +0000 Ankit Agrawal <[email protected]> wrote:
Quoted 46 lines
Quoted 30 lines
drivers/vfio/pci/nvgrace-gpu/main.c +30
-0+/* +* To reduce the system bootup time, the HBM training has +* been moved out of the UEFI on the Grace-Blackwell systems. +* +* The onus of checking whether the HBM training has completed +* thus falls on the module. The HBM training status can be +* determined from a BAR0 register. +* +* Similarly, another BAR0 register exposes the status of the +* CPU-GPU chip-to-chip (C2C) cache coherent interconnect. +* +* Poll these register and check for 30s. If the HBM training is +* not complete or if the C2C link is not ready, fail the probe. +* +* While the wait is not required on Grace Hopper systems, it +* is beneficial to make the check to ensure the device is in an +* expected state. +*/ +static int nvgrace_gpu_wait_device_ready (struct pci_dev *pdev) +{ + unsigned long timeout = jiffies + msecs_to_jiffies(POLL_TIMEOUT_MS); + void __iomem *io; + int ret = -ETIME; + + io = pci_iomap(pdev, 0, 0); + if (!io) + return -ENOMEM; + + do { + if ((ioread32(io + C2C_LINK_BAR0_OFFSET) == STATUS_READY) &&drivers/vfio/pci/nvgrace-gpu/main.c static bool nvgrace_gpu_has_mig_hw_bug_fix(struct pci_dev *pdev) · line 906
Quoted 10 lines
drivers/vfio/pci/nvgrace-gpu/main.c +10
-0+ (ioread32(io + HBM_TRAINING_BAR0_OFFSET) == STATUS_READY)) { + ret = 0; + goto reg_check_exit; + } + msleep(POLL_QUANTUM_MS); + } while (!time_after(jiffies, timeout)); + +reg_check_exit: + pci_iounmap(pdev, io); + return ret;We're accessing device memory here but afaict the memory enable bit of the command register is in an indeterminate state. What happens if you use setpci to clear the memory enable bit or 'echo 0 > enable' before binding the driver? Thanks,
Quoted 8 lines
Quoted 1 line
Alex
Hi Alex, sorry I didn't understand how we are accessing device memory here if the
C2C_LINK_BAR0_OFFSETandHBM_TRAINING_BAR0_OFFSETare BAR0 regs. But anyways, I tried 'echo 0 > <sysfs_path>/enable' before device bind. I am not observing any issue and the bind goes through.Or am I missing something?
BAR0 is what I'm referring to as device memory. We cannot access registers in BAR0 unless the memory space enable bit of the command register is set. The nvgrace-gpu driver makes no effort to enable this and I don't think the PCI core does before probe either. Disabling through sysfs will only disable if it was previously enabled, so possibly that test was invalid. Please try with setpci:
# Read command register
$ setpci -s xxxx:xx:xx.x COMMAND # Clear memory enable $ setpci -s xxxx:xx:xx.x COMMAND=0:2 # Re-read command register $ setpci -s xxxx:xx:xx.x COMMAND
Probe driver here now that the memory enable bit should re--back as unset. Thanks,
Alex
Ankit Agrawal
maintainerRe: [PATCH v3 3/3] vfio/nvgrace-gpu: Check the HBM training and C2C link status
Quoted 29 lines
Quoted 6 lines
Quoted 6 lines
We're accessing device memory here but afaict the memory enable bit of the command register is in an indeterminate state. What happens if you use setpci to clear the memory enable bit or 'echo 0 > enable' before binding the driver? Thanks,
Quoted 8 lines
Quoted 1 line
Alex
Hi Alex, sorry I didn't understand how we are accessing device memory here if the
C2C_LINK_BAR0_OFFSETandHBM_TRAINING_BAR0_OFFSETare BAR0 regs. But anyways, I tried 'echo 0 > <sysfs_path>/enable' before device bind. I am not observing any issue and the bind goes through.Or am I missing something?
BAR0 is what I'm referring to as device memory. We cannot access registers in BAR0 unless the memory space enable bit of the command register is set. The nvgrace-gpu driver makes no effort to enable this and I don't think the PCI core does before probe either. Disabling through sysfs will only disable if it was previously enabled, so possibly that test was invalid. Please try with setpci:
# Read command register
$ setpci -s xxxx:xx:xx.x COMMAND # Clear memory enable $ setpci -s xxxx:xx:xx.x COMMAND=0:2 # Re-read command register $ setpci -s xxxx:xx:xx.x COMMAND
Quoted 4 lines
Probe driver here now that the memory enable bit should re--back as unset. Thanks,
Alex
Ok, yeah. I tried to disable through setpci, and the probe is failing with ETIME. Should we check if disabled and return -EIO for such situation to differentiate from timeout?
Re: [PATCH v3 3/3] vfio/nvgrace-gpu: Check the HBM training and C2C link status
Quoted 28 lines · On Fri, 17 Jan 2025 21:13:52 +0000 Ankit Agrawal <[email protected]> wrote:
Quoted 28 lines
Quoted 5 lines
Quoted 5 lines
We're accessing device memory here but afaict the memory enable bit of the command register is in an indeterminate state. What happens if you use setpci to clear the memory enable bit or 'echo 0 > enable' before binding the driver? Thanks,
Quoted 8 lines
Quoted 1 line
Alex
Hi Alex, sorry I didn't understand how we are accessing device memory here if the
C2C_LINK_BAR0_OFFSETandHBM_TRAINING_BAR0_OFFSETare BAR0 regs. But anyways, I tried 'echo 0 > <sysfs_path>/enable' before device bind. I am not observing any issue and the bind goes through.Or am I missing something?
BAR0 is what I'm referring to as device memory. We cannot access registers in BAR0 unless the memory space enable bit of the command register is set. The nvgrace-gpu driver makes no effort to enable this and I don't think the PCI core does before probe either. Disabling through sysfs will only disable if it was previously enabled, so possibly that test was invalid. Please try with setpci:
# Read command register
$ setpci -s xxxx:xx:xx.x COMMAND # Clear memory enable $ setpci -s xxxx:xx:xx.x COMMAND=0:2 # Re-read command register $ setpci -s xxxx:xx:xx.x COMMAND
Quoted 8 lines
Quoted 4 lines
Probe driver here now that the memory enable bit should re--back as unset. Thanks,
Alex
Ok, yeah. I tried to disable through setpci, and the probe is failing with ETIME. Should we check if disabled and return
-EIOfor such situation to differentiate from timeout?
No, the driver needs to enable memory on the device around the iomap rather than assuming the initial state. Thanks,
Alex
Ankit Agrawal
maintainerRe: [PATCH v3 3/3] vfio/nvgrace-gpu: Check the HBM training and C2C link status
Quoted 9 lines
Quoted 4 lines
Ok, yeah. I tried to disable through setpci, and the probe is failing with ETIME. Should we check if disabled and return
-EIOfor such situation to differentiate from timeout?No, the driver needs to enable memory on the device around the iomap rather than assuming the initial state. Thanks,
Alex
Ack, thanks for the suggestion.
I'll change nvgrace_gpu_wait_device_ready to read the PCI_COMMAND through pci_read_config_word before pci_iomap. And if PCI_COMMAND_MEMORY is not set, update through pci_write_config_word.
Ankit Agrawal
maintainerRe: [PATCH v3 3/3] vfio/nvgrace-gpu: Check the HBM training and C2C link status
Quoted 10 lines
Quoted 4 lines
No, the driver needs to enable memory on the device around the iomap rather than assuming the initial state. Thanks,
Alex
Ack, thanks for the suggestion.
I'll change
nvgrace_gpu_wait_device_readyto read thePCI_COMMANDthroughpci_read_config_wordbeforepci_iomap. And ifPCI_COMMAND_MEMORYis not set, update throughpci_write_config_word.
Or perhaps better to call vfio_pci_memory_lock_and_enable(). I'll make the change and send out the next version.