Kernel KVM virtualization development
 help / color / mirror / Atom feed
* [PATCH v7] vfio/pci: expose ACPI _ROM through a device region
@ 2026-12-04 11:19 Michael S. Tsirkin
  2026-12-04 11:54 ` sashiko-bot
  2026-12-04 15:03 ` Alex Williamson
  0 siblings, 2 replies; 5+ messages in thread
From: Michael S. Tsirkin @ 2026-12-04 11:19 UTC (permalink / raw)
  To: Alex Williamson; +Cc: 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.

Note that _ROM size is not provided by the firmware and has to be parsed
from the image. Let's leave this to userspace.

Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---

Apologies for spamming, v6 was just completely broken.

Changes from v6:
	Declare params after args.
	Handle offsets >=4GB.

Changes from v3:
	Drop rom cache and size heuristics, userspace can handle them.
	Document source of 4k alignment information.
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 | 89 ++++++++++++++++++++++++++++++++
 include/uapi/linux/vfio.h        |  9 ++++
 4 files changed, 108 insertions(+)

diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
index 511dcb49d2f6..8b43b5148f18 100644
--- a/drivers/vfio/pci/vfio_pci_core.c
+++ b/drivers/vfio/pci/vfio_pci_core.c
@@ -831,6 +831,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 4e7162234a2e..1af780d7f1f3 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 df8848087681..35be5a8bc39a 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,92 @@ 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
+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];
+	u64 pos = *ppos & VFIO_PCI_OFFSET_MASK;
+	struct acpi_buffer result = { ACPI_ALLOCATE_BUFFER, NULL };
+	union acpi_object *obj;
+	size_t available;
+	/*
+	 * ACPI limits requests to 4K, and some FW seems to only handle
+	 * full 4K aligned blocks. See
+	 * ad4a36263535 ("drm/nouveau/bios: split out shadow methods").
+	 * and
+	 * d1626a965141 ("drm/nouveau/bios: fetch full 4KiB block to determine ACPI ROM image size").
+	 */
+	size_t skip = pos % SZ_4K;
+	union acpi_object args[] = {
+		{ .integer = { .type = ACPI_TYPE_INTEGER, .value = pos - skip } },
+		{ .integer = { .type = ACPI_TYPE_INTEGER, .value = SZ_4K } },
+	};
+	struct acpi_object_list params = { ARRAY_SIZE(args), args };
+
+	if (iswrite)
+		return -EINVAL;
+
+	if (!count)
+		return 0;
+
+	/*
+	 * A 32-bit AML interpreter cannot handle larger offsets, and ROMs
+	 * larger than 4G are unlikely. Truncate at the 4G boundary.
+	 */
+	if (pos > U32_MAX)
+		return 0;
+
+	if (ACPI_FAILURE(acpi_evaluate_object_typed(region->data, NULL, &params,
+						    &result, ACPI_TYPE_BUFFER)))
+		return -EIO;
+
+	obj = result.pointer;
+	available = min_t(size_t, obj->buffer.length, SZ_4K);
+	if (skip >= available) {
+		kfree(obj);
+		return 0;
+	}
+
+	count = min(count, available - skip);
+	count -= copy_to_user(buf, obj->buffer.pointer + skip, count);
+	kfree(obj);
+	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)
+{
+}
+
+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), method;
+	int ret;
+
+	if (!handle || ACPI_FAILURE(acpi_get_handle(handle, "_ROM", &method)))
+		return;
+
+	ret = vfio_pci_core_register_dev_region(vdev, VFIO_REGION_TYPE_ACPI,
+						VFIO_REGION_SUBTYPE_ACPI_ROM,
+						&vfio_pci_acpi_rom_ops, 0,
+						VFIO_REGION_INFO_FLAG_READ, method);
+	if (ret)
+		pci_info(vdev->pdev, "ACPI _ROM region unavailable (%d)\n", ret);
+}
+#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 e41437fa17ad..6e81844d47ad 100644
--- a/include/uapi/linux/vfio.h
+++ b/include/uapi/linux/vfio.h
@@ -345,6 +345,7 @@ 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_PCI_* */
 
@@ -428,6 +429,14 @@ struct vfio_region_gfx_edid {
 /* sub-types for VFIO_REGION_TYPE_MIGRATION */
 #define VFIO_REGION_SUBTYPE_MIGRATION_DEPRECATED (1)
 
+/* sub-types for VFIO_REGION_TYPE_ACPI */
+/*
+ * Read-only PCI expansion ROM from the device firmware's ACPI _ROM method.
+ * Size is always zero for this subtype. Userspace can parse the ROM image
+ * to determine its size.
+ */
+#define VFIO_REGION_SUBTYPE_ACPI_ROM		(1)
+
 struct vfio_device_migration_info {
 	__u32 device_state;         /* VFIO device state */
 #define VFIO_DEVICE_STATE_V1_STOP      (0)

base-commit: 0b75cdfe60c4db031d8a6dcdf5c7ceafcfe5fa02
-- 
MST

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

end of thread, other threads:[~2026-12-04 16:38 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 11:19 [PATCH v7] vfio/pci: expose ACPI _ROM through a device region Michael S. Tsirkin
2026-12-04 11:54 ` sashiko-bot
2026-12-04 15:03 ` Alex Williamson
2026-12-04 15:41   ` Michael S. Tsirkin
2026-12-04 16:38     ` Alex Williamson

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