From: "Michael S. Tsirkin" <mst@redhat.com>
To: linux-kernel@vger.kernel.org
Cc: Alex Williamson <alex@shazbot.org>, kvm@vger.kernel.org
Subject: [PATCH v3] vfio/pci: expose ACPI _ROM through a device region
Date: Thu, 3 Dec 2026 09:35:01 -0500 [thread overview]
Message-ID: <147be6bed8795824d27429a8c717e2d1d08ad89f.1796307888.git.mst@redhat.com> (raw)
Some GPUs provide their option ROM through the ACPI _ROM method.
According to the spec, _ROM, if present, must supercede the
PCI ROM BAR, but VFIO currently provides no means to access its
contents.
Let's report it through a new VFIO_REGION_TYPE_ACPI subtype:
VFIO_REGION_SUBTYPE_ACPI_ROM.
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---
changes from v2:
leave ROM BAR alone to avoid a risk of regressions.
drivers/vfio/pci/vfio_pci_core.c | 1 +
drivers/vfio/pci/vfio_pci_priv.h | 9 +++
drivers/vfio/pci/vfio_pci_rdwr.c | 115 +++++++++++++++++++++++++++++++
include/uapi/linux/vfio.h | 9 +++
4 files changed, 134 insertions(+)
diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
index fcbb2da860e2a..49ec5a3a29ae5 100644
--- a/drivers/vfio/pci/vfio_pci_core.c
+++ b/drivers/vfio/pci/vfio_pci_core.c
@@ -829,6 +829,7 @@ EXPORT_SYMBOL_GPL(vfio_pci_core_close_device);
void vfio_pci_core_finish_enable(struct vfio_pci_core_device *vdev)
{
+ vfio_pci_register_acpi_rom(vdev);
vfio_pci_probe_mmaps(vdev);
#if IS_ENABLED(CONFIG_EEH)
eeh_dev_open(vdev->pdev);
diff --git a/drivers/vfio/pci/vfio_pci_priv.h b/drivers/vfio/pci/vfio_pci_priv.h
index 4e7162234a2eb..1af780d7f1f37 100644
--- a/drivers/vfio/pci/vfio_pci_priv.h
+++ b/drivers/vfio/pci/vfio_pci_priv.h
@@ -44,6 +44,15 @@ ssize_t vfio_pci_config_rw_single(struct vfio_pci_core_device *vdev,
ssize_t vfio_pci_bar_rw(struct vfio_pci_core_device *vdev, char __user *buf,
size_t count, loff_t *ppos, bool iswrite);
+#ifdef CONFIG_ACPI
+void vfio_pci_register_acpi_rom(struct vfio_pci_core_device *vdev);
+#else
+static inline void
+vfio_pci_register_acpi_rom(struct vfio_pci_core_device *vdev)
+{
+}
+#endif
+
#ifdef CONFIG_VFIO_PCI_VGA
ssize_t vfio_pci_vga_rw(struct vfio_pci_core_device *vdev, char __user *buf,
size_t count, loff_t *ppos, bool iswrite);
diff --git a/drivers/vfio/pci/vfio_pci_rdwr.c b/drivers/vfio/pci/vfio_pci_rdwr.c
index df88480876815..71ddf8f500508 100644
--- a/drivers/vfio/pci/vfio_pci_rdwr.c
+++ b/drivers/vfio/pci/vfio_pci_rdwr.c
@@ -10,8 +10,11 @@
* Author: Tom Lyon, pugs@cisco.com
*/
+#include <linux/acpi.h>
#include <linux/fs.h>
#include <linux/pci.h>
+#include <linux/sizes.h>
+#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/vfio.h>
@@ -198,6 +201,118 @@ ssize_t vfio_pci_core_do_io_rw(struct vfio_pci_core_device *vdev, bool test_mem,
}
EXPORT_SYMBOL_GPL(vfio_pci_core_do_io_rw);
+#ifdef CONFIG_ACPI
+/* Bound the firmware probe and cache to a reasonable option ROM size. */
+#define VFIO_PCI_ACPI_ROM_MAX SZ_16M
+
+static ssize_t vfio_pci_acpi_rom_rw(struct vfio_pci_core_device *vdev,
+ char __user *buf, size_t count,
+ loff_t *ppos, bool iswrite)
+{
+ unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
+ struct vfio_pci_region *region = &vdev->region[index - VFIO_PCI_NUM_REGIONS];
+ loff_t pos = *ppos & VFIO_PCI_OFFSET_MASK;
+
+ if (iswrite)
+ return -EINVAL;
+
+ if (!count || pos >= region->size)
+ return 0;
+
+ count = min_t(size_t, count, region->size - pos);
+ count -= copy_to_user(buf, region->data + pos, count);
+ if (!count)
+ return -EFAULT;
+
+ *ppos += count;
+ return count;
+}
+
+static void vfio_pci_acpi_rom_release(struct vfio_pci_core_device *vdev,
+ struct vfio_pci_region *region)
+{
+ kvfree(region->data);
+}
+
+static const struct vfio_pci_regops vfio_pci_acpi_rom_ops = {
+ .rw = vfio_pci_acpi_rom_rw,
+ .release = vfio_pci_acpi_rom_release,
+};
+
+void vfio_pci_register_acpi_rom(struct vfio_pci_core_device *vdev)
+{
+ acpi_handle handle = ACPI_HANDLE(&vdev->pdev->dev);
+ union acpi_object args[] = {
+ { .integer = { .type = ACPI_TYPE_INTEGER } },
+ { .integer = { .type = ACPI_TYPE_INTEGER, .value = SZ_4K } },
+ };
+ struct acpi_object_list params = { ARRAY_SIZE(args), args };
+ size_t size = 0, capacity = 0;
+ void *rom = NULL;
+ acpi_handle method;
+ int ret = -EIO;
+
+ if (!handle || ACPI_FAILURE(acpi_get_handle(handle, "_ROM", &method)))
+ return;
+
+ /*
+ * Some firmware requires 4 KiB-aligned requests. Snapshot the image
+ * at open so reads never invoke AML and the size and contents agree.
+ */
+ for (;;) {
+ struct acpi_buffer result = { ACPI_ALLOCATE_BUFFER, NULL };
+ union acpi_object *obj;
+ size_t len;
+
+ args[0].integer.value = size;
+ if (ACPI_FAILURE(acpi_evaluate_object_typed(method, NULL, ¶ms,
+ &result, ACPI_TYPE_BUFFER)))
+ break;
+
+ obj = result.pointer;
+ len = obj->buffer.length;
+ if (len > SZ_4K || len > VFIO_PCI_ACPI_ROM_MAX - size) {
+ ret = -EFBIG;
+ kfree(obj);
+ break;
+ }
+ if (size + len > capacity) {
+ void *new_rom;
+
+ capacity = max_t(size_t, SZ_4K, capacity * 2);
+ new_rom = kvrealloc(rom, capacity, GFP_KERNEL);
+ if (!new_rom) {
+ ret = -ENOMEM;
+ kfree(obj);
+ break;
+ }
+ rom = new_rom;
+ }
+ if (len)
+ memcpy(rom + size, obj->buffer.pointer, len);
+ kfree(obj);
+ size += len;
+
+ if (len < SZ_4K) {
+ if (!size)
+ return;
+
+ ret = vfio_pci_core_register_dev_region(vdev,
+ VFIO_REGION_TYPE_ACPI,
+ VFIO_REGION_SUBTYPE_ACPI_ROM,
+ &vfio_pci_acpi_rom_ops, size,
+ VFIO_REGION_INFO_FLAG_READ, rom);
+ if (!ret)
+ return;
+ break;
+ }
+ }
+
+ pci_dbg(vdev->pdev, "ACPI _ROM region unavailable (%d)\n", ret);
+ kvfree(rom);
+}
+#endif /* CONFIG_ACPI */
+
ssize_t vfio_pci_bar_rw(struct vfio_pci_core_device *vdev, char __user *buf,
size_t count, loff_t *ppos, bool iswrite)
{
diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h
index e41437fa17ad0..0d77f860020e9 100644
--- a/include/uapi/linux/vfio.h
+++ b/include/uapi/linux/vfio.h
@@ -345,6 +345,15 @@ struct vfio_region_info_cap_type {
#define VFIO_REGION_TYPE_GFX (1)
#define VFIO_REGION_TYPE_CCW (2)
#define VFIO_REGION_TYPE_MIGRATION_DEPRECATED (3)
+#define VFIO_REGION_TYPE_ACPI (4)
+
+/* sub-types for VFIO_REGION_TYPE_ACPI */
+/*
+ * The device firmware's ACPI _ROM image, when available. The region size is
+ * the exact image length in bytes. This region supports read() and pread()
+ * only. It is independent of the PCI expansion ROM region.
+ */
+#define VFIO_REGION_SUBTYPE_ACPI_ROM (1)
/* sub-types for VFIO_REGION_TYPE_PCI_* */
--
MST
next reply other threads:[~2026-12-03 14:35 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-03 14:35 Michael S. Tsirkin [this message]
2026-12-03 21:57 ` Alex Williamson
2026-12-03 23:06 ` Michael S. Tsirkin
2026-12-04 2:03 ` Alex Williamson
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=147be6bed8795824d27429a8c717e2d1d08ad89f.1796307888.git.mst@redhat.com \
--to=mst@redhat.com \
--cc=alex@shazbot.org \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®