The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH v3] vfio/pci: expose ACPI _ROM through a device region
@ 2026-12-03 14:35 Michael S. Tsirkin
  2026-12-03 21:57 ` Alex Williamson
  0 siblings, 1 reply; 3+ messages in thread
From: Michael S. Tsirkin @ 2026-12-03 14:35 UTC (permalink / raw)
  To: linux-kernel; +Cc: Alex Williamson, kvm

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, &params,
+							    &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

^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-03 23:06 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 14:35 [PATCH v3] vfio/pci: expose ACPI _ROM through a device region Michael S. Tsirkin
2026-12-03 21:57 ` Alex Williamson
2026-12-03 23:06   ` Michael S. Tsirkin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox