All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sairaj Kodilkar <sarunkod@amd.com>
To: "Joerg Roedel (AMD)" <joro@8bytes.org>,
	Adrian Huang <ahuang12@lenovo.com>,
	Joerg Roedel <jroedel@suse.de>,
	Robin Murphy <robin.murphy@arm.com>,
	Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>,
	Vasant Hegde <vasant.hegde@amd.com>,
	"Will Deacon" <will@kernel.org>, <iommu@lists.linux.dev>
Cc: <Vasant.Hegde@amd.com>, <Suravee.Suthikulpanit@amd.com>,
	Baoquan He <bhe@redhat.com>,
	Jerry Snitselaar <jsnitsel@redhat.com>,
	Sairaj Kodilkar <sarunkod@amd.com>
Subject: [PATCH v5 2/3] iommu/amd: Do not convert IVMD exclusion range to unity range
Date: Fri, 27 Nov 2026 15:34:13 +0530	[thread overview]
Message-ID: <20261127100414.6980-3-sarunkod@amd.com> (raw)
In-Reply-To: <20261127100414.6980-1-sarunkod@amd.com>

From: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>

Original problem was there could be multiple IVMD exclusion entries
conflicting over a single exclusion range supported in older Linux
kernel. The fix was first introduced in commit 387caf0b759a ("iommu/amd:
Treat per-device exclusion ranges as r/w unity-mapped regions") and in
commit 0bbe4ced53e3 ("iommu/amd: Fix the overwritten field in IVMD header")
, where IVMD exclusions are converted to separate unity_map_entry objects
in pci_seg->unity_map so amd_iommu_get_resv_regions() treated every such
entry as IOMMU_RESV_DIRECT with R/W.

The driver no longer programs the exclusion base/limit registers and
relies on the kernel to keep the IOVA ranges named by exclusion IVMDs
unmapped. Stop forcing exclusion entries to RW-unity. Store the raw
IVMD flags in struct unity_map_entry instead of a pre-shifted
protection value, and derive the reserved region from those flags.
Exclusion entries are now reported as IOMMU_RESV_RESERVED with no
read/write permission.

Reject unity entries that grant neither IR nor IW with a FW_BUG
message, and entries with unknown flag bits. Warn about invalid flag
combinations.

Fixes: 0bbe4ced53e3 ("iommu/amd: Fix the overwritten field in IVMD header")
Cc: Baoquan He <bhe@redhat.com>
Cc: Adrian Huang <ahuang12@lenovo.com>
Cc: Jerry Snitselaar <jsnitsel@redhat.com>
Signed-off-by: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
Signed-off-by: Sairaj Kodilkar <sarunkod@amd.com>
Reviewed-by: Adrian Huang <ahuang12@lenovo.com>
---
 drivers/iommu/amd/amd_iommu_types.h | 12 ++++++----
 drivers/iommu/amd/init.c            | 36 ++++++++++++++++-------------
 drivers/iommu/amd/iommu.c           | 24 +++++++++++++++++--
 3 files changed, 49 insertions(+), 23 deletions(-)

diff --git a/drivers/iommu/amd/amd_iommu_types.h b/drivers/iommu/amd/amd_iommu_types.h
index d1602144c160..0f4c3c65188c 100644
--- a/drivers/iommu/amd/amd_iommu_types.h
+++ b/drivers/iommu/amd/amd_iommu_types.h
@@ -371,9 +371,11 @@
 /* DTE[128:179] | DTE[184:191] */
 #define DTE_DATA2_INTR_MASK	~GENMASK_ULL(55, 52)
 
-#define IOMMU_PROT_MASK 0x03
-#define IOMMU_PROT_IR 0x01
-#define IOMMU_PROT_IW 0x02
+#define IVMD_FLAG_UNITY_MAP	BIT(0)
+#define IVMD_FLAG_IR		BIT(1)
+#define IVMD_FLAG_IW		BIT(2)
+#define IVMD_FLAG_EXCL_RANGE	BIT(3)
+#define IVMD_FLAG_MASK		GENMASK_U32(3, 0)
 
 /* IOMMU capabilities */
 #define IOMMU_CAP_IOTLB   24
@@ -890,8 +892,8 @@ struct unity_map_entry {
 	/* end address to unity map (including) */
 	u64 address_end;
 
-	/* required protection */
-	int prot;
+	/* IVMD flags for the entry */
+	u8 flags;
 };
 
 /*
diff --git a/drivers/iommu/amd/init.c b/drivers/iommu/amd/init.c
index f8cf37a471f8..6d92fb5c81a7 100644
--- a/drivers/iommu/amd/init.c
+++ b/drivers/iommu/amd/init.c
@@ -70,11 +70,6 @@
 #define IVHD_FLAG_RESPASSPW_EN_MASK     0x04
 #define IVHD_FLAG_ISOC_EN_MASK          0x08
 
-#define IVMD_FLAG_EXCL_RANGE            0x08
-#define IVMD_FLAG_IW                    0x04
-#define IVMD_FLAG_IR                    0x02
-#define IVMD_FLAG_UNITY_MAP             0x01
-
 #define ACPI_DEVFLAG_INITPASS           0x01
 #define ACPI_DEVFLAG_EXTINT             0x02
 #define ACPI_DEVFLAG_NMI                0x04
@@ -2742,6 +2737,25 @@ static int __init init_unity_map_range(struct ivmd_header *m,
 	struct amd_iommu_pci_seg *pci_seg;
 	char *s;
 
+	if (m->flags & ~IVMD_FLAG_MASK) {
+		pr_err(FW_BUG "IVMD type %#x devid %#x range %#llx+%#llx: Unknown flags\n",
+		       m->type, m->devid, m->range_start, m->range_length);
+		return -EINVAL;
+	}
+
+	if ((m->flags & IVMD_FLAG_UNITY_MAP) &&
+	    !(m->flags & (IVMD_FLAG_IR | IVMD_FLAG_IW))) {
+		pr_err(FW_BUG "IVMD type %#x devid %#x range %#llx+%#llx: Unity set without IR/IW, ignoring\n",
+		       m->type, m->devid, m->range_start, m->range_length);
+		return -EINVAL;
+	}
+
+	if ((m->flags & IVMD_FLAG_EXCL_RANGE) &&
+	    (m->flags & (IVMD_FLAG_UNITY_MAP | IVMD_FLAG_IR | IVMD_FLAG_IW))) {
+		pr_err(FW_BUG "IVMD type %#x devid %#x range %#llx+%#llx: Invalid IVMD flag combination %#x\n",
+		       m->type, m->devid, m->range_start, m->range_length, m->flags);
+	}
+
 	pci_seg = get_pci_segment(m->pci_seg, ivrs_base);
 	if (pci_seg == NULL)
 		return -ENOMEM;
@@ -2771,17 +2785,7 @@ static int __init init_unity_map_range(struct ivmd_header *m,
 	}
 	e->address_start = PAGE_ALIGN(m->range_start);
 	e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
-	e->prot = m->flags >> 1;
-
-	/*
-	 * Treat per-device exclusion ranges as r/w unity-mapped regions
-	 * since some buggy BIOSes might lead to the overwritten exclusion
-	 * range (exclusion_start and exclusion_length members). This
-	 * happens when there are multiple exclusion ranges (IVMD entries)
-	 * defined in ACPI table.
-	 */
-	if (m->flags & IVMD_FLAG_EXCL_RANGE)
-		e->prot = (IVMD_FLAG_IW | IVMD_FLAG_IR) >> 1;
+	e->flags = m->flags;
 
 	DUMP_printk("%s devid_start: %04x:%02x:%02x.%x devid_end: "
 		    "%04x:%02x:%02x.%x range_start: %016llx range_end: %016llx"
diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c
index 41ee4ad1dcf8..a417bfce0c89 100644
--- a/drivers/iommu/amd/iommu.c
+++ b/drivers/iommu/amd/iommu.c
@@ -3153,10 +3153,30 @@ static void amd_iommu_get_resv_regions(struct device *dev,
 
 		type   = IOMMU_RESV_DIRECT;
 		length = entry->address_end - entry->address_start;
-		if (entry->prot & IOMMU_PROT_IR)
+		if (entry->flags & IVMD_FLAG_IR)
 			prot |= IOMMU_READ;
-		if (entry->prot & IOMMU_PROT_IW)
+		if (entry->flags & IVMD_FLAG_IW)
 			prot |= IOMMU_WRITE;
+		if (entry->flags & IVMD_FLAG_EXCL_RANGE) {
+			/*
+			 * Each IOMMU has only one exclusion base/limit
+			 * register pair (MMIO 0x0020/0x0028). Older kernels
+			 * programmed that pair with the exclusion range,
+			 * which assumed at most one range per IOMMU. Some
+			 * buggy BIOSes list several exclusion IVMD entries for
+			 * the same IOMMU. Writing them all to the single
+			 * register overwrites the earlier ranges, so they
+			 * were lost.
+			 *
+			 * Reporting each range as a reserved region removes
+			 * the one-range limit. The IOMMU core keeps these
+			 * IOVAs out of allocation and never maps them, so any
+			 * number of exclusion ranges is supported without
+			 * touching the register.
+			 */
+			type = IOMMU_RESV_RESERVED;
+			prot = 0;
+		}
 
 		region = iommu_alloc_resv_region(entry->address_start,
 						 length, prot, type,
-- 
2.34.1


  parent reply	other threads:[~2026-11-27 10:05 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27 10:04 [PATCH v5 0/3] iommu/amd: IVMD handling cleanup and ivmd= boot override Sairaj Kodilkar
2026-11-27 10:04 ` [PATCH v5 1/3] iommu/amd: Rename exclusion range base and limit macros Sairaj Kodilkar
2026-11-27 10:04 ` Sairaj Kodilkar [this message]
2026-12-01  8:50   ` [PATCH v5 2/3] iommu/amd: Do not convert IVMD exclusion range to unity range Vasant Hegde
2026-12-01 12:01     ` Sairaj Kodilkar
2026-12-01 12:33       ` Sairaj Kodilkar
2026-11-27 10:04 ` [PATCH v5 3/3] iommu/amd: Rename unity_map symbols to ivmd_entry Sairaj Kodilkar

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=20261127100414.6980-3-sarunkod@amd.com \
    --to=sarunkod@amd.com \
    --cc=ahuang12@lenovo.com \
    --cc=bhe@redhat.com \
    --cc=iommu@lists.linux.dev \
    --cc=joro@8bytes.org \
    --cc=jroedel@suse.de \
    --cc=jsnitsel@redhat.com \
    --cc=robin.murphy@arm.com \
    --cc=suravee.suthikulpanit@amd.com \
    --cc=vasant.hegde@amd.com \
    --cc=will@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.