From: "Cédric Le Goater" <clg@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Alex Williamson" <alex@shazbot.org>,
"Cédric Le Goater" <clg@redhat.com>
Subject: [PATCH for-12.0 8/9] vfio/pci: Extract ROM handling into pci-rom.c
Date: Mon, 9 Nov 2026 10:06:24 +0100 [thread overview]
Message-ID: <20261109090625.908592-9-clg@redhat.com> (raw)
In-Reply-To: <20261109090625.908592-1-clg@redhat.com>
Move ROM handling routines into a dedicated pci-rom.c file.
No functional change. Retain existing license 'GPL-2.0-only'.
Signed-off-by: Cédric Le Goater <clg@redhat.com>
---
hw/vfio/pci.h | 1 +
hw/vfio/pci-rom.c | 239 ++++++++++++++++++++++++++++++++++++++++++++
hw/vfio/pci.c | 218 ----------------------------------------
hw/vfio/meson.build | 1 +
4 files changed, 241 insertions(+), 218 deletions(-)
create mode 100644 hw/vfio/pci-rom.c
diff --git a/hw/vfio/pci.h b/hw/vfio/pci.h
index bcbb582c13d3..faf0e9dcf022 100644
--- a/hw/vfio/pci.h
+++ b/hw/vfio/pci.h
@@ -251,6 +251,7 @@ bool vfio_pci_config_setup(VFIOPCIDevice *vdev, Error **errp);
void vfio_pci_config_register_vga(VFIOPCIDevice *vdev);
/* ROM */
+void vfio_pci_size_rom(VFIOPCIDevice *vdev);
bool vfio_opt_rom_in_denylist(VFIOPCIDevice *vdev);
/* VGA */
diff --git a/hw/vfio/pci-rom.c b/hw/vfio/pci-rom.c
new file mode 100644
index 000000000000..c8c59727bf7e
--- /dev/null
+++ b/hw/vfio/pci-rom.c
@@ -0,0 +1,239 @@
+/*
+ * vfio PCI device - ROM support
+ *
+ * Copyright Red Hat, Inc. 2012-2026
+ *
+ * Authors:
+ * Alex Williamson <alex.williamson@redhat.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0-only
+ */
+
+#include "qemu/osdep.h"
+#include <linux/vfio.h>
+
+#include "hw/core/hw-error.h"
+#include "hw/pci/pci_device.h"
+#include "qemu/error-report.h"
+#include "pci.h"
+#include "trace.h"
+#include "qapi/error.h"
+#include "vfio-helpers.h"
+
+static bool vfio_pci_load_rom(VFIOPCIDevice *vdev, Error **errp)
+{
+ VFIODevice *vbasedev = &vdev->vbasedev;
+ struct vfio_region_info *reg_info = NULL;
+ uint64_t size;
+ off_t off = 0;
+ ssize_t bytes;
+ int ret;
+
+ ret = vfio_device_get_region_info(vbasedev, VFIO_PCI_ROM_REGION_INDEX,
+ ®_info);
+
+ if (ret != 0) {
+ error_setg_errno(errp, -ret, "vfio: Error getting ROM info");
+ return false;
+ }
+
+ trace_vfio_pci_load_rom(vbasedev->name, (unsigned long)reg_info->size,
+ (unsigned long)reg_info->offset,
+ (unsigned long)reg_info->flags);
+
+ vdev->rom_size = size = reg_info->size;
+ vdev->rom_offset = reg_info->offset;
+
+ if (!vdev->rom_size) {
+ vdev->rom_size = 0;
+ vdev->rom_offset = 0;
+ error_setg(errp, "vfio-pci: Device ROM size is zero at %s",
+ vbasedev->name);
+ error_append_hint(errp, "Device option ROM contents are probably "
+ "invalid (check dmesg).\nSkip option ROM probe "
+ "with rombar=0, or load from file with romfile=\n");
+ return false;
+ }
+
+ vdev->rom = g_malloc(size);
+ memset(vdev->rom, 0xff, size);
+
+ while (size) {
+ bytes = vbasedev->io_ops->region_read(vbasedev,
+ VFIO_PCI_ROM_REGION_INDEX,
+ off, size, vdev->rom + off);
+
+ if (bytes == 0) {
+ break;
+ } else if (bytes > 0) {
+ off += bytes;
+ size -= bytes;
+ } else {
+ if (bytes == -EINTR || bytes == -EAGAIN) {
+ continue;
+ }
+ error_setg_errno(errp, -bytes, "vfio: Error reading device ROM");
+ g_free(vdev->rom);
+ vdev->rom = NULL;
+ vdev->rom_size = 0;
+ vdev->rom_offset = 0;
+ return false;
+ }
+ }
+
+ /*
+ * Test the ROM signature against our device, if the vendor is correct
+ * but the device ID doesn't match, store the correct device ID and
+ * recompute the checksum. Intel IGD devices need this and are known
+ * to have bogus checksums so we can't simply adjust the checksum.
+ */
+ if (pci_get_word(vdev->rom) == 0xaa55 &&
+ pci_get_word(vdev->rom + 0x18) + 8 < vdev->rom_size &&
+ !memcmp(vdev->rom + pci_get_word(vdev->rom + 0x18), "PCIR", 4)) {
+ uint16_t vid, did;
+
+ vid = pci_get_word(vdev->rom + pci_get_word(vdev->rom + 0x18) + 4);
+ did = pci_get_word(vdev->rom + pci_get_word(vdev->rom + 0x18) + 6);
+
+ if (vid == vdev->vendor_id && did != vdev->device_id) {
+ int i;
+ uint8_t csum, *data = vdev->rom;
+
+ pci_set_word(vdev->rom + pci_get_word(vdev->rom + 0x18) + 6,
+ vdev->device_id);
+ data[6] = 0;
+
+ for (csum = 0, i = 0; i < vdev->rom_size; i++) {
+ csum += data[i];
+ }
+
+ data[6] = -csum;
+ }
+ }
+
+ vfio_rom_quirk_setup(vdev);
+
+ return true;
+}
+
+static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size)
+{
+ VFIOPCIDevice *vdev = opaque;
+ union {
+ uint8_t byte;
+ uint16_t word;
+ uint32_t dword;
+ uint64_t qword;
+ } val = { .qword = ~0ULL };
+ uint64_t data = 0;
+
+ /* Load the ROM lazily when the guest tries to read it */
+ if (unlikely(!vdev->rom && !vdev->rom_read_failed)) {
+ Error *local_err = NULL;
+
+ vdev->rom_read_failed = !vfio_pci_load_rom(vdev, &local_err);
+ if (vdev->rom_read_failed) {
+ error_report_err(local_err);
+ }
+ }
+
+ memcpy(&val, vdev->rom + addr,
+ (addr < vdev->rom_size) ? MIN(size, vdev->rom_size - addr) : 0);
+
+ switch (size) {
+ case 1:
+ data = val.byte;
+ break;
+ case 2:
+ data = le16_to_cpu(val.word);
+ break;
+ case 4:
+ data = le32_to_cpu(val.dword);
+ break;
+ default:
+ hw_error("vfio: unsupported read size, %d bytes\n", size);
+ break;
+ }
+
+ trace_vfio_rom_read(vdev->vbasedev.name, addr, size, data);
+
+ return data;
+}
+
+static void vfio_rom_write(void *opaque, hwaddr addr,
+ uint64_t data, unsigned size)
+{
+}
+
+static const MemoryRegionOps vfio_rom_ops = {
+ .read = vfio_rom_read,
+ .write = vfio_rom_write,
+ .endianness = DEVICE_LITTLE_ENDIAN,
+};
+
+void vfio_pci_size_rom(VFIOPCIDevice *vdev)
+{
+ PCIDevice *pdev = PCI_DEVICE(vdev);
+ VFIODevice *vbasedev = &vdev->vbasedev;
+ uint32_t orig, size = cpu_to_le32((uint32_t)PCI_ROM_ADDRESS_MASK);
+ char *name;
+
+ if (pdev->romfile || !pdev->rom_bar) {
+ /* Since pci handles romfile, just print a message and return */
+ if (vfio_opt_rom_in_denylist(vdev) && pdev->romfile) {
+ warn_report("Device at %s is known to cause system instability"
+ " issues during option rom execution",
+ vdev->vbasedev.name);
+ error_printf("Proceeding anyway since user specified romfile\n");
+ }
+ return;
+ }
+
+ /*
+ * Use the same size ROM BAR as the physical device. The contents
+ * will get filled in later when the guest tries to read it.
+ */
+ if (vfio_pci_config_space_read(vdev, PCI_ROM_ADDRESS, 4, &orig) != 4 ||
+ vfio_pci_config_space_write(vdev, PCI_ROM_ADDRESS, 4, &size) != 4 ||
+ vfio_pci_config_space_read(vdev, PCI_ROM_ADDRESS, 4, &size) != 4 ||
+ vfio_pci_config_space_write(vdev, PCI_ROM_ADDRESS, 4, &orig) != 4) {
+
+ error_report("%s(%s) ROM access failed", __func__, vbasedev->name);
+ return;
+ }
+
+ size = ~(le32_to_cpu(size) & PCI_ROM_ADDRESS_MASK) + 1;
+
+ if (!size) {
+ return;
+ }
+
+ if (vfio_opt_rom_in_denylist(vdev)) {
+ if (pdev->rom_bar > 0) {
+ warn_report("Device at %s is known to cause system instability"
+ " issues during option rom execution",
+ vdev->vbasedev.name);
+ error_printf("Proceeding anyway since user specified"
+ " positive value for rombar\n");
+ } else {
+ warn_report("Rom loading for device at %s has been disabled"
+ " due to system instability issues",
+ vdev->vbasedev.name);
+ error_printf("Specify rombar=1 or romfile to force\n");
+ return;
+ }
+ }
+
+ trace_vfio_pci_size_rom(vdev->vbasedev.name, size);
+
+ name = g_strdup_printf("vfio[%s].rom", vdev->vbasedev.name);
+
+ memory_region_init_io(&pdev->rom, OBJECT(vdev),
+ &vfio_rom_ops, vdev, name, size);
+ g_free(name);
+
+ pci_register_bar(pdev, PCI_ROM_SLOT,
+ PCI_BASE_ADDRESS_SPACE_MEMORY, &pdev->rom);
+
+ vdev->rom_read_failed = false;
+}
diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c
index 075166b56d3c..34dbef744564 100644
--- a/hw/vfio/pci.c
+++ b/hw/vfio/pci.c
@@ -47,102 +47,6 @@
#include "vfio-migration-internal.h"
#include "vfio-helpers.h"
-static bool vfio_pci_load_rom(VFIOPCIDevice *vdev, Error **errp)
-{
- VFIODevice *vbasedev = &vdev->vbasedev;
- struct vfio_region_info *reg_info = NULL;
- uint64_t size;
- off_t off = 0;
- ssize_t bytes;
- int ret;
-
- ret = vfio_device_get_region_info(vbasedev, VFIO_PCI_ROM_REGION_INDEX,
- ®_info);
-
- if (ret != 0) {
- error_setg_errno(errp, -ret, "vfio: Error getting ROM info");
- return false;
- }
-
- trace_vfio_pci_load_rom(vbasedev->name, (unsigned long)reg_info->size,
- (unsigned long)reg_info->offset,
- (unsigned long)reg_info->flags);
-
- vdev->rom_size = size = reg_info->size;
- vdev->rom_offset = reg_info->offset;
-
- if (!vdev->rom_size) {
- vdev->rom_size = 0;
- vdev->rom_offset = 0;
- error_setg(errp, "vfio-pci: Device ROM size is zero at %s",
- vbasedev->name);
- error_append_hint(errp, "Device option ROM contents are probably "
- "invalid (check dmesg).\nSkip option ROM probe "
- "with rombar=0, or load from file with romfile=\n");
- return false;
- }
-
- vdev->rom = g_malloc(size);
- memset(vdev->rom, 0xff, size);
-
- while (size) {
- bytes = vbasedev->io_ops->region_read(vbasedev,
- VFIO_PCI_ROM_REGION_INDEX,
- off, size, vdev->rom + off);
-
- if (bytes == 0) {
- break;
- } else if (bytes > 0) {
- off += bytes;
- size -= bytes;
- } else {
- if (bytes == -EINTR || bytes == -EAGAIN) {
- continue;
- }
- error_setg_errno(errp, -bytes, "vfio: Error reading device ROM");
- g_free(vdev->rom);
- vdev->rom = NULL;
- vdev->rom_size = 0;
- vdev->rom_offset = 0;
- return false;
- }
- }
-
- /*
- * Test the ROM signature against our device, if the vendor is correct
- * but the device ID doesn't match, store the correct device ID and
- * recompute the checksum. Intel IGD devices need this and are known
- * to have bogus checksums so we can't simply adjust the checksum.
- */
- if (pci_get_word(vdev->rom) == 0xaa55 &&
- pci_get_word(vdev->rom + 0x18) + 8 < vdev->rom_size &&
- !memcmp(vdev->rom + pci_get_word(vdev->rom + 0x18), "PCIR", 4)) {
- uint16_t vid, did;
-
- vid = pci_get_word(vdev->rom + pci_get_word(vdev->rom + 0x18) + 4);
- did = pci_get_word(vdev->rom + pci_get_word(vdev->rom + 0x18) + 6);
-
- if (vid == vdev->vendor_id && did != vdev->device_id) {
- int i;
- uint8_t csum, *data = vdev->rom;
-
- pci_set_word(vdev->rom + pci_get_word(vdev->rom + 0x18) + 6,
- vdev->device_id);
- data[6] = 0;
-
- for (csum = 0, i = 0; i < vdev->rom_size; i++) {
- csum += data[i];
- }
-
- data[6] = -csum;
- }
- }
-
- vfio_rom_quirk_setup(vdev);
-
- return true;
-}
-
/* "Raw" read of underlying config space. */
int vfio_pci_config_space_read(VFIOPCIDevice *vdev, off_t offset,
uint32_t size, void *data)
@@ -161,128 +65,6 @@ int vfio_pci_config_space_write(VFIOPCIDevice *vdev, off_t offset,
offset, size, data, false);
}
-static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size)
-{
- VFIOPCIDevice *vdev = opaque;
- union {
- uint8_t byte;
- uint16_t word;
- uint32_t dword;
- uint64_t qword;
- } val = { .qword = ~0ULL };
- uint64_t data = 0;
-
- /* Load the ROM lazily when the guest tries to read it */
- if (unlikely(!vdev->rom && !vdev->rom_read_failed)) {
- Error *local_err = NULL;
-
- vdev->rom_read_failed = !vfio_pci_load_rom(vdev, &local_err);
- if (vdev->rom_read_failed) {
- error_report_err(local_err);
- }
- }
-
- memcpy(&val, vdev->rom + addr,
- (addr < vdev->rom_size) ? MIN(size, vdev->rom_size - addr) : 0);
-
- switch (size) {
- case 1:
- data = val.byte;
- break;
- case 2:
- data = le16_to_cpu(val.word);
- break;
- case 4:
- data = le32_to_cpu(val.dword);
- break;
- default:
- hw_error("vfio: unsupported read size, %d bytes\n", size);
- break;
- }
-
- trace_vfio_rom_read(vdev->vbasedev.name, addr, size, data);
-
- return data;
-}
-
-static void vfio_rom_write(void *opaque, hwaddr addr,
- uint64_t data, unsigned size)
-{
-}
-
-static const MemoryRegionOps vfio_rom_ops = {
- .read = vfio_rom_read,
- .write = vfio_rom_write,
- .endianness = DEVICE_LITTLE_ENDIAN,
-};
-
-static void vfio_pci_size_rom(VFIOPCIDevice *vdev)
-{
- PCIDevice *pdev = PCI_DEVICE(vdev);
- VFIODevice *vbasedev = &vdev->vbasedev;
- uint32_t orig, size = cpu_to_le32((uint32_t)PCI_ROM_ADDRESS_MASK);
- char *name;
-
- if (pdev->romfile || !pdev->rom_bar) {
- /* Since pci handles romfile, just print a message and return */
- if (vfio_opt_rom_in_denylist(vdev) && pdev->romfile) {
- warn_report("Device at %s is known to cause system instability"
- " issues during option rom execution",
- vdev->vbasedev.name);
- error_printf("Proceeding anyway since user specified romfile\n");
- }
- return;
- }
-
- /*
- * Use the same size ROM BAR as the physical device. The contents
- * will get filled in later when the guest tries to read it.
- */
- if (vfio_pci_config_space_read(vdev, PCI_ROM_ADDRESS, 4, &orig) != 4 ||
- vfio_pci_config_space_write(vdev, PCI_ROM_ADDRESS, 4, &size) != 4 ||
- vfio_pci_config_space_read(vdev, PCI_ROM_ADDRESS, 4, &size) != 4 ||
- vfio_pci_config_space_write(vdev, PCI_ROM_ADDRESS, 4, &orig) != 4) {
-
- error_report("%s(%s) ROM access failed", __func__, vbasedev->name);
- return;
- }
-
- size = ~(le32_to_cpu(size) & PCI_ROM_ADDRESS_MASK) + 1;
-
- if (!size) {
- return;
- }
-
- if (vfio_opt_rom_in_denylist(vdev)) {
- if (pdev->rom_bar > 0) {
- warn_report("Device at %s is known to cause system instability"
- " issues during option rom execution",
- vdev->vbasedev.name);
- error_printf("Proceeding anyway since user specified"
- " positive value for rombar\n");
- } else {
- warn_report("Rom loading for device at %s has been disabled"
- " due to system instability issues",
- vdev->vbasedev.name);
- error_printf("Specify rombar=1 or romfile to force\n");
- return;
- }
- }
-
- trace_vfio_pci_size_rom(vdev->vbasedev.name, size);
-
- name = g_strdup_printf("vfio[%s].rom", vdev->vbasedev.name);
-
- memory_region_init_io(&pdev->rom, OBJECT(vdev),
- &vfio_rom_ops, vdev, name, size);
- g_free(name);
-
- pci_register_bar(pdev, PCI_ROM_SLOT,
- PCI_BASE_ADDRESS_SPACE_MEMORY, &pdev->rom);
-
- vdev->rom_read_failed = false;
-}
-
void vfio_vga_write(void *opaque, hwaddr addr,
uint64_t data, unsigned size)
{
diff --git a/hw/vfio/meson.build b/hw/vfio/meson.build
index 0ac1a568e58c..84cec8f077ad 100644
--- a/hw/vfio/meson.build
+++ b/hw/vfio/meson.build
@@ -15,6 +15,7 @@ vfio_ss.add(when: 'CONFIG_VFIO_PCI', if_true: files(
'pci-caps.c',
'pci-irq.c',
'pci-quirks.c',
+ 'pci-rom.c',
'pci.c',
))
vfio_ss.add(when: 'CONFIG_VFIO_CCW', if_true: files('ccw.c'))
--
2.56.0
next prev parent reply other threads:[~2026-11-09 9:07 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-09 9:06 [PATCH for-12.0 0/9] vfio/pci: Split pci.c Cédric Le Goater
2026-11-09 9:06 ` [PATCH for-12.0 1/9] vfio/pci: Extract interrupt handling into pci-irq.c Cédric Le Goater
2026-11-09 9:06 ` [PATCH for-12.0 2/9] vfio/pci: Move vfio_mmap_set_enabled() to region.c Cédric Le Goater
2026-11-30 22:47 ` Alex Williamson
2026-11-09 9:06 ` [PATCH for-12.0 3/9] vfio/pci: Rename IRQ functions Cédric Le Goater
2026-11-30 22:47 ` Alex Williamson
2026-11-09 9:06 ` [PATCH for-12.0 4/9] vfio/pci: Replace deprecated g_memdup() with g_memdup2() Cédric Le Goater
2026-11-09 9:06 ` [PATCH for-12.0 5/9] vfio/pci: Extract PCI capability setup into pci-caps.c Cédric Le Goater
2026-11-09 9:06 ` [PATCH for-12.0 6/9] vfio/pci: Rename capability functions Cédric Le Goater
2026-11-09 9:06 ` [PATCH for-12.0 7/9] vfio/pci: Rename vfio_sub_page_bar_update_mappings() function Cédric Le Goater
2026-11-09 9:06 ` Cédric Le Goater [this message]
2026-11-09 9:06 ` [PATCH for-12.0 9/9] vfio/pci: Rename vfio_opt_rom_in_denylist() to vfio_quirk_rom_in_denylist() Cédric Le Goater
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261109090625.908592-9-clg@redhat.com \
--to=clg@redhat.com \
--cc=alex@shazbot.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.