From: Narayana Murty N <nnmlinux@linux.ibm.com>
To: Madhavan Srinivasan <maddy@linux.ibm.com>,
"Oliver O'Halloran" <oohall@gmail.com>,
Timothy Pearson <tpearson@raptorengineering.com>,
Alex Williamson <alex@shazbot.org>,
sourabhjain@linux.ibm.com
Cc: Michael Ellerman <mpe@ellerman.id.au>,
Nicholas Piggin <npiggin@gmail.com>,
Christophe Leroy <chleroy@kernel.org>,
Ritesh Harjani <ritesh.list@gmail.com>,
Shrikanth Hegde <sshegde@linux.ibm.com>,
linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org,
kvm@vger.kernel.org, sbhat@linux.ibm.com, harshpb@linux.ibm.com
Subject: [PATCH v7 3/4] vfio/spapr_tce: Normalize EEH IOA error injection addresses
Date: Mon, 23 Nov 2026 15:52:41 +0530 [thread overview]
Message-ID: <20261123102237.95860-9-nnmlinux@linux.ibm.com> (raw)
In-Reply-To: <20261123102237.95860-6-nnmlinux@linux.ibm.com>
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@linux.ibm.com>
---
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@ozlabs.ru"
@@ -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, ®ion, 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
next prev parent reply other threads:[~2026-11-23 10:24 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-23 10:22 [PATCH v7 0/4] powerpc: Add RTAS based error injection support for pSeries Narayana Murty N
2026-11-23 10:22 ` [PATCH v7 1/4] powerpc/rtas: Handle ibm,open-errinjct return format Narayana Murty N
2026-11-23 10:22 ` [PATCH v7 2/4] powerpc/pseries/eeh: Implement RTAS-based EEH error injection Narayana Murty N
2026-11-23 10:22 ` Narayana Murty N [this message]
2026-11-23 10:22 ` [PATCH v7 4/4] powerpc/powernv/eeh: Map generic EEH error types to OPAL Narayana Murty N
-- strict thread matches above, loose matches on Subject: below --
2026-10-23 7:36 [PATCH v6 0/5] powerpc: Add RTAS based error injection support for pSeries Narayana Murty N
2026-11-23 8:47 ` [PATCH v7 3/4] vfio/spapr_tce: Normalize EEH IOA error injection addresses Narayana Murty N
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=20261123102237.95860-9-nnmlinux@linux.ibm.com \
--to=nnmlinux@linux.ibm.com \
--cc=alex@shazbot.org \
--cc=chleroy@kernel.org \
--cc=harshpb@linux.ibm.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linuxppc-dev@lists.ozlabs.org \
--cc=maddy@linux.ibm.com \
--cc=mpe@ellerman.id.au \
--cc=npiggin@gmail.com \
--cc=oohall@gmail.com \
--cc=ritesh.list@gmail.com \
--cc=sbhat@linux.ibm.com \
--cc=sourabhjain@linux.ibm.com \
--cc=sshegde@linux.ibm.com \
--cc=tpearson@raptorengineering.com \
/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