Updating the host from CachyOS 7.2.10 to 7.3 exposed grey display output
on an RTX 5090 passthrough setup after the guest booted Linux. The
triggering kernel change is unknown. Restricting BAR1 to 4 KiB mappings
masks the failure; waiting for firmware readiness also restores output
with stock KVM, unmodified VFIO and verified 1 GiB BAR1 mappings.

GB202's Firmware Security Processor can still be initializing when PCI
reset completes. Wait for the BAR firewall to open and the FSP boot
status to report success before guest firmware runs. Limit the quirk
to the validated physical RTX 5090 product ID, independent of guest
identity overrides.

Preserve the existing function, bus and PM reset order. Bound both
polls with one five-second deadline. Temporarily enable BAR0 memory
decoding through raw VFIO configuration access and restore it on exit.
Byte writes preserve the kernel-managed INTx-disable bit.

Report readiness failures without terminating QEMU. The reset has
already been attempted, so consume the callback without triggering
another reset and allow the normal post-reset cleanup to run.

Link:
https://github.com/NVIDIA/open-gpu-kernel-modules/blob/e6a2e01579fbbd1ca2ed43fef2ee2589e9bd8bd9/src/nvidia/src/kernel/gpu/fsp/arch/blackwell/kern_fsp_gb202.c
Link:
https://github.com/NVIDIA/open-gpu-kernel-modules/blob/e6a2e01579fbbd1ca2ed43fef2ee2589e9bd8bd9/src/nvidia/src/kernel/gpu/bif/arch/blackwell/kernel_bif_gb202.c
Signed-off-by: Gustavo A. <[email protected]>
---

Hi Alex,

Thanks for your reply to my kernel RFC, "vfio/pci: Wait for GB202
firmware after reset". I tried the QEMU resetfn approach you suggested,
using vfio_radeon_reset() as a reference, and the QEMU-only tests restored
normal display output. I'm following up here with the patch and results
for your feedback.

This RFC was developed with AI assistance. I understand QEMU's current
policy declines contributions derived from AI-generated content. Would
an exception be appropriate here, or would these test results help a
maintainer develop an independent fix?

The readiness wait was tested on a Palit RTX 5090 GameRock using QEMU
11.1.3, stock KVM, unmodified VFIO, tracing disabled and verified 1 GiB
BAR1 mappings. Output and refresh were normal on repeated boots. An
unmodified QEMU control, built with the same compiler and configuration,
reproduced grey output under the same conditions.

I would appreciate feedback on whether the reset quirk is the right place
for this wait, whether the five-second timeout and report-and-continue
policy are appropriate, and what evidence would justify adding other
GB202 product IDs.

Thanks,
Gustavo

 hw/vfio/pci-quirks.c | 129 +++++++++++++++++++++++++++++++++++++++++++
 hw/vfio/pci.c        |  48 ++++++++--------
 hw/vfio/pci.h        |   5 ++
 hw/vfio/trace-events |   1 +
 4 files changed, 161 insertions(+), 22 deletions(-)

diff --git a/hw/vfio/pci-quirks.c b/hw/vfio/pci-quirks.c
index a38a0e7..f00f62c 100644
--- a/hw/vfio/pci-quirks.c
+++ b/hw/vfio/pci-quirks.c
@@ -1354,8 +1354,137 @@ out:
     return ret;
 }

+/*
+ * GB202's Firmware Security Processor can still be initializing after
reset.
+ * Wait before guest firmware runs.
+ */
+#define NVIDIA_GB202_BAR_FIREWALL 0x2b8
+#define NVIDIA_GB202_BAR_BLOCKED BIT(24)
+#define NVIDIA_GB202_FSP_STATUS 0xad00bc
+#define NVIDIA_GB202_FSP_READY 0xff
+
+/* Byte writes preserve concurrent updates to the INTx-disable bit. */
+static int vfio_nvidia_gb202_set_memory_decode(VFIOPCIDevice *vdev,
+                                               bool enable, bool
*was_enabled)
+{
+    uint8_t command, value;
+
+    if (vfio_pci_config_space_read(vdev, PCI_COMMAND, 1, &command) != 1 ||
+        command == UINT8_MAX) {
+        return -EIO;
+    }
+    if (was_enabled) {
+        *was_enabled = command & PCI_COMMAND_MEMORY;
+    }
+    value = enable ? command | PCI_COMMAND_MEMORY :
+                     command & ~PCI_COMMAND_MEMORY;
+    if (value == command) {
+        return 0;
+    }
+
+    if (vfio_pci_config_space_write(vdev, PCI_COMMAND, 1, &value) != 1) {
+        return -EIO;
+    }
+    if (vfio_pci_config_space_read(vdev, PCI_COMMAND, 1, &command) != 1 ||
+        command != value) {
+        return -EIO;
+    }
+    return 0;
+}
+
+static int vfio_nvidia_gb202_wait_ready(VFIOPCIDevice *vdev)
+{
+    VFIORegion *bar0 = &vdev->bars[0].region;
+    int64_t deadline = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
+    bool memory_was_enabled = true;
+    uint32_t value;
+    int ret, restore_ret;
+
+    if (vdev->config_size < NVIDIA_GB202_BAR_FIREWALL + sizeof(value) ||
+        vdev->bars[0].ioport || !(bar0->flags &
VFIO_REGION_INFO_FLAG_READ) ||
+        bar0->size < NVIDIA_GB202_FSP_STATUS + sizeof(value)) {
+        return -ENODEV;
+    }
+
+    /* The BAR firewall must open before BAR0 can be accessed. */
+    for (;;) {
+        ret = vfio_pci_config_space_read(vdev, NVIDIA_GB202_BAR_FIREWALL,
+                                         sizeof(value), &value);
+        if (ret != sizeof(value) || value == UINT32_MAX) {
+            return -EIO;
+        }
+        if (!(le32_to_cpu(value) & NVIDIA_GB202_BAR_BLOCKED)) {
+            break;
+        }
+        if (g_get_monotonic_time() >= deadline) {
+            return -ETIMEDOUT;
+        }
+        g_usleep(G_TIME_SPAN_MILLISECOND);
+    }
+
+    ret = vfio_nvidia_gb202_set_memory_decode(vdev, true,
&memory_was_enabled);
+    if (ret) {
+        goto out_restore;
+    }
+
+    for (;;) {
+        value = vfio_region_read(bar0, NVIDIA_GB202_FSP_STATUS,
sizeof(value));
+        if (value == UINT32_MAX) {
+            ret = -EIO;
+            break;
+        }
+        if (value == NVIDIA_GB202_FSP_READY) {
+            break;
+        }
+        if (g_get_monotonic_time() >= deadline) {
+            ret = -ETIMEDOUT;
+            break;
+        }
+        g_usleep(G_TIME_SPAN_MILLISECOND);
+    }
+
+out_restore:
+    if (!memory_was_enabled) {
+        restore_ret = vfio_nvidia_gb202_set_memory_decode(vdev, false,
NULL);
+        if (restore_ret) {
+            ret = restore_ret;
+        }
+    }
+    return ret;
+}
+
+static int vfio_nvidia_gb202_reset(VFIOPCIDevice *vdev)
+{
+    int ret;
+
+    /* A shared bus reset may already have run; keep the normal reset
policy. */
+    vfio_pci_reset_device(vdev);
+    ret = vfio_nvidia_gb202_wait_ready(vdev);
+    trace_vfio_quirk_nvidia_gb202_reset(vdev->vbasedev.name, ret);
+    if (ret) {
+        error_report("vfio: %s: GB202 firmware readiness check failed: %s",
+                     vdev->vbasedev.name, strerror(-ret));
+    }
+
+    /* The reset was already attempted; do not repeat it on a wait
failure. */
+    return 0;
+}
+
 void vfio_setup_resetfn_quirk(VFIOPCIDevice *vdev)
 {
+    uint8_t config[PCI_STD_HEADER_SIZEOF];
+
+    /* Match physical RTX 5090 identity, independent of guest overrides. */
+    if (!vdev->vbasedev.mdev &&
+        vfio_pci_config_space_read(vdev, 0, sizeof(config),
+                                   config) == sizeof(config) &&
+        pci_get_word(config + PCI_VENDOR_ID) == PCI_VENDOR_ID_NVIDIA &&
+        pci_get_word(config + PCI_DEVICE_ID) == 0x2b85 &&
+        config[PCI_CLASS_DEVICE + 1] == PCI_BASE_CLASS_DISPLAY) {
+        vdev->resetfn = vfio_nvidia_gb202_reset;
+        return;
+    }
+
     switch (vdev->vendor_id) {
     case 0x1002:
         switch (vdev->device_id) {
diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c
index 0da2e14..55fddb4 100644
--- a/hw/vfio/pci.c
+++ b/hw/vfio/pci.c
@@ -1141,8 +1141,8 @@ static bool vfio_pci_load_rom(VFIOPCIDevice *vdev,
Error **errp)
 }

 /* "Raw" read of underlying config space. */
-static int vfio_pci_config_space_read(VFIOPCIDevice *vdev, off_t offset,
-                                      uint32_t size, void *data)
+int vfio_pci_config_space_read(VFIOPCIDevice *vdev, off_t offset,
+                               uint32_t size, void *data)
 {
     return vdev->vbasedev.io_ops->region_read(&vdev->vbasedev,
                                               VFIO_PCI_CONFIG_REGION_INDEX,
@@ -1150,8 +1150,8 @@ static int vfio_pci_config_space_read(VFIOPCIDevice
*vdev, off_t offset,
 }

 /* "Raw" write of underlying config space. */
-static int vfio_pci_config_space_write(VFIOPCIDevice *vdev, off_t offset,
-                                       uint32_t size, void *data)
+int vfio_pci_config_space_write(VFIOPCIDevice *vdev, off_t offset,
+                                uint32_t size, void *data)
 {
     return vdev->vbasedev.io_ops->region_write(&vdev->vbasedev,

VFIO_PCI_CONFIG_REGION_INDEX,
@@ -3767,6 +3767,27 @@ static void vfio_exitfn(PCIDevice *pdev)
     }
 }

+void vfio_pci_reset_device(VFIOPCIDevice *vdev)
+{
+    if (vdev->vbasedev.reset_works &&
+        (vdev->has_flr || !vdev->has_pm_reset) &&
+        !ioctl(vdev->vbasedev.fd, VFIO_DEVICE_RESET)) {
+        trace_vfio_pci_reset_flr(vdev->vbasedev.name);
+        return;
+    }
+
+    /* See if we can do our own bus reset */
+    if (!vfio_pci_hot_reset_one(vdev)) {
+        return;
+    }
+
+    /* If nothing else works and the device supports PM reset, use it */
+    if (vdev->vbasedev.reset_works && vdev->has_pm_reset &&
+        !ioctl(vdev->vbasedev.fd, VFIO_DEVICE_RESET)) {
+        trace_vfio_pci_reset_pm(vdev->vbasedev.name);
+    }
+}
+
 static void vfio_pci_reset(DeviceState *dev)
 {
     VFIOPCIDevice *vdev = VFIO_PCI_DEVICE(dev);
@@ -3788,24 +3809,7 @@ static void vfio_pci_reset(DeviceState *dev)
         goto post_reset;
     }

-    if (vdev->vbasedev.reset_works &&
-        (vdev->has_flr || !vdev->has_pm_reset) &&
-        !ioctl(vdev->vbasedev.fd, VFIO_DEVICE_RESET)) {
-        trace_vfio_pci_reset_flr(vdev->vbasedev.name);
-        goto post_reset;
-    }
-
-    /* See if we can do our own bus reset */
-    if (!vfio_pci_hot_reset_one(vdev)) {
-        goto post_reset;
-    }
-
-    /* If nothing else works and the device supports PM reset, use it */
-    if (vdev->vbasedev.reset_works && vdev->has_pm_reset &&
-        !ioctl(vdev->vbasedev.fd, VFIO_DEVICE_RESET)) {
-        trace_vfio_pci_reset_pm(vdev->vbasedev.name);
-        goto post_reset;
-    }
+    vfio_pci_reset_device(vdev);

 post_reset:
     vfio_pci_post_reset(vdev);
diff --git a/hw/vfio/pci.h b/hw/vfio/pci.h
index 7ff52e9..de04761 100644
--- a/hw/vfio/pci.h
+++ b/hw/vfio/pci.h
@@ -226,6 +226,10 @@ void vfio_pci_intx_eoi(VFIODevice *vbasedev);
 void vfio_pci_teardown_msi(VFIOPCIDevice *vdev);

 /* Config space */
+int vfio_pci_config_space_read(VFIOPCIDevice *vdev, off_t offset,
+                               uint32_t size, void *data);
+int vfio_pci_config_space_write(VFIOPCIDevice *vdev, off_t offset,
+                                uint32_t size, void *data);
 uint32_t vfio_pci_read_config(PCIDevice *pdev, uint32_t addr, int len);
 void vfio_pci_write_config(PCIDevice *pdev,
                            uint32_t addr, uint32_t val, int len);
@@ -272,6 +276,7 @@ extern const PropertyInfo qdev_prop_nv_gpudirect_clique;
 struct vfio_pci_hot_reset_info;

 void vfio_pci_pre_reset(VFIOPCIDevice *vdev);
+void vfio_pci_reset_device(VFIOPCIDevice *vdev);
 void vfio_pci_post_reset(VFIOPCIDevice *vdev);
 bool vfio_pci_host_match(PCIHostDeviceAddress *addr, const char *name);
 int vfio_pci_get_pci_hot_reset_info(VFIOPCIDevice *vdev,
diff --git a/hw/vfio/trace-events b/hw/vfio/trace-events
index bfdaf22..7c64373 100644
--- a/hw/vfio/trace-events
+++ b/hw/vfio/trace-events
@@ -80,6 +80,7 @@ vfio_quirk_ati_bonaire_reset_no_smc(const char *name) "%s"
 vfio_quirk_ati_bonaire_reset_timeout(const char *name) "%s"
 vfio_quirk_ati_bonaire_reset_done(const char *name) "%s"
 vfio_quirk_ati_bonaire_reset(const char *name) "%s"
+vfio_quirk_nvidia_gb202_reset(const char *name, int ret) "%s ret %d"
 vfio_ioeventfd_exit(const char *name, uint64_t addr, unsigned size,
uint64_t data) "%s+0x%"PRIx64"[%d]:0x%"PRIx64
 vfio_ioeventfd_handler(const char *name, uint64_t addr, unsigned size,
uint64_t data) "%s+0x%"PRIx64"[%d] -> 0x%"PRIx64
 vfio_ioeventfd_init(const char *name, uint64_t addr, unsigned size,
uint64_t data, bool vfio) "%s+0x%"PRIx64"[%d]:0x%"PRIx64" vfio:%d"
--
2.56.1

Reply via email to