[PATCH v7 3/4] vfio/spapr_tce: Normalize EEH IOA error injection addresses

From: Narayana Murty N

Date: Mon Nov 23 2026 - 05:24:10 EST


On pseries, RTAS error injection takes a PCI bus address for memory and
I/O load/store functions. VFIO_EEH_PE_INJECT_ERR callers such as QEMU
pass an address within a BAR as the sysfs "resource" file exports it: a
CPU address for a memory BAR and a PCI bus address for an I/O BAR. A
CPU address can differ from the PCI bus address because of PHB window
translation.

On pseries, convert the address before calling eeh_pe_inject_err() to the
PCI bus address of the matching standard BAR (BAR0-BAR5) of a device in
the IOMMU group, keeping its offset. Memory functions match memory BARs
and I/O functions match I/O BARs. Unassigned and disabled BARs are never
matched, and an address that matches no BAR fails with -EINVAL.

Config-space and DMA functions, requests with address and mask both zero,
and requests on other platforms are not changed. The mask is never
modified.

Signed-off-by: Narayana Murty N <nnmlinux@xxxxxxxxxxxxx>
---
drivers/vfio/vfio_iommu_spapr_tce.c | 131 +++++++++++++++++++++++++++-
1 file changed, 130 insertions(+), 1 deletion(-)

diff --git a/drivers/vfio/vfio_iommu_spapr_tce.c b/drivers/vfio/vfio_iommu_spapr_tce.c
index 1c0eec228cc6..c56b649d7094 100644
--- a/drivers/vfio/vfio_iommu_spapr_tce.c
+++ b/drivers/vfio/vfio_iommu_spapr_tce.c
@@ -23,9 +23,11 @@
#include <linux/mm.h>
#include "vfio.h"

+#include <asm/eeh.h>
#include <asm/iommu.h>
#include <asm/tce.h>
#include <asm/mmu_context.h>
+#include <asm/machdep.h>

#define DRIVER_VERSION "0.1"
#define DRIVER_AUTHOR "aik@xxxxxxxxx"
@@ -774,12 +776,116 @@ static long tce_iommu_create_default_window(struct tce_container *container)
return ret;
}

+/*
+ * Look for @addr in the standard BARs (BAR0-BAR5) of @pdev whose resource
+ * type is @res_type, and translate it to the PCI bus address RTAS expects,
+ * keeping the offset into the BAR. Expansion ROM, SR-IOV resources and
+ * bridge windows are not supported.
+ *
+ * @addr is in the domain the sysfs "resource" file exports: the CPU address
+ * for a memory BAR and the PCI bus address for an I/O BAR. Each type has a
+ * single domain, so a match is unambiguous.
+ *
+ * Unassigned BARs are zero-based and flagged IORESOURCE_UNSET on pseries,
+ * so they are skipped, as are disabled ones.
+ */
+static bool vfio_spapr_eeh_normalize_pdev_addr(struct pci_dev *pdev,
+ unsigned long res_type,
+ unsigned long addr,
+ unsigned long *normalized)
+{
+ struct pci_bus_region region;
+ struct resource *res;
+ resource_size_t len, start;
+ int bar;
+
+ for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) {
+ res = &pdev->resource[bar];
+
+ if (resource_type(res) != res_type)
+ continue;
+
+ if (res->flags & (IORESOURCE_UNSET | IORESOURCE_DISABLED))
+ continue;
+
+ len = pci_resource_len(pdev, bar);
+ if (!len)
+ continue;
+
+ pcibios_resource_to_bus(pdev->bus, &region, res);
+ start = res_type == IORESOURCE_IO ? region.start : res->start;
+
+ if (addr >= start && addr - start < len) {
+ *normalized = region.start + (addr - start);
+ return true;
+ }
+ }
+
+ return false;
+}
+
+struct vfio_spapr_eeh_addr_lookup {
+ unsigned long res_type;
+ unsigned long addr;
+ unsigned long normalized;
+ bool found;
+};
+
+static int vfio_spapr_eeh_match_dev(struct device *dev, void *data)
+{
+ struct vfio_spapr_eeh_addr_lookup *lookup = data;
+ struct pci_dev *pdev;
+
+ if (!dev_is_pci(dev))
+ return 0;
+
+ pdev = to_pci_dev(dev);
+ if (vfio_spapr_eeh_normalize_pdev_addr(pdev, lookup->res_type,
+ lookup->addr,
+ &lookup->normalized)) {
+ lookup->found = true;
+ return 1;
+ }
+
+ return 0;
+}
+
+/*
+ * Convert @addr to a PCI bus address using the BARs of type @res_type of the
+ * devices in @group.
+ */
+static int vfio_spapr_eeh_normalize_addr(struct iommu_group *group,
+ unsigned long res_type,
+ unsigned long addr,
+ unsigned long *normalized)
+{
+ struct vfio_spapr_eeh_addr_lookup lookup = {
+ .res_type = res_type,
+ .addr = addr,
+ };
+ int ret;
+
+ ret = iommu_group_for_each_dev(group, &lookup,
+ vfio_spapr_eeh_match_dev);
+ if (ret < 0)
+ return ret;
+
+ if (lookup.found) {
+ *normalized = lookup.normalized;
+ return 0;
+ }
+
+ pr_debug("vfio EEH: addr=0x%lx not found in any matching BAR of group\n",
+ addr);
+ return -EINVAL;
+}
+
static long vfio_spapr_ioctl_eeh_pe_op(struct iommu_group *group,
unsigned long arg)
{
struct eeh_pe *pe;
struct vfio_eeh_pe_op op;
- unsigned long minsz;
+ unsigned long minsz, res_type;

pe = eeh_iommu_group_to_pe(group);
if (!pe)
@@ -818,6 +924,29 @@ static long vfio_spapr_ioctl_eeh_pe_op(struct iommu_group *group,
if (copy_from_user(&op, (void __user *)arg, minsz))
return -EFAULT;

+ /*
+ * Only the pSeries RTAS backend takes a PCI bus address, so
+ * PowerNV requests are passed through unchanged. Config and
+ * DMA functions do not carry BAR addresses, and an address
+ * and mask of zero carries no address.
+ */
+ res_type = eeh_err_func_to_res_type(op.err.func);
+ if (machine_is(pseries) && res_type &&
+ (op.err.type == EEH_ERR_TYPE_32 ||
+ op.err.type == EEH_ERR_TYPE_64) &&
+ (op.err.addr || op.err.mask)) {
+ unsigned long normalized;
+ int ret;
+
+ ret = vfio_spapr_eeh_normalize_addr(group, res_type,
+ op.err.addr,
+ &normalized);
+ if (ret)
+ return ret;
+
+ op.err.addr = normalized;
+ }
+
return eeh_pe_inject_err(pe, op.err.type, op.err.func,
op.err.addr, op.err.mask);
default:
--
2.54.0