Skip to contentSkip to conversation

iommu/amd: IVMD handling cleanup and ivmd= boot override

PATCH Superseded Sairaj Kodilkar 2 participants 12 messages Nov 6 – Nov 9

Subsystems (2)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover iommu/amd: IVMD handling cleanup and ivmd= boot override

This series refreshes how the AMD IOMMU driver models IVRS IVMD entries after the hardware exclusion range registers were repurposed.

The first patch renames the associated MMIO offsets to reflect completion-wait store base/limit.

The second patch drops the workaround that forced IVMD exclusion ranges to r/w unity mappings; the driver no longer programs exclusion range registers, so that the workaround is obsolete.

The third patch centralizes IVMD flag definitions, renames the unity-map structures to ivmd_entry, stores the raw IVMD flags byte from ACPI, and derives IOMMU reserved regions (including exclusion as IOMMU_RESV_RESERVED) from those flags.

The fourth patch adds an ivmd= kernel parameter so up to four per-device IVMD-style ranges can be supplied from the command line when the IVRS table is wrong or incomplete, documented in kernel-parameters.txt.

Changes from v2:
https://lore.kernel.org/all/[email protected]/#t

*Patch 2: Report an IVMD exclusion range as IOMMU_RESV_RESERVED in this
patch, instead of leaving that for patch 3. This patch stops forcing the exclusion to read/write unity, the range has no read or write permission. iommu_map() rejects that, so a tree checked out at patch 2 does not boot. Reserving the range in the same patch keeps the series bisectable.

* Patch 4: Drop this patch, ivmd= command line does not have any real use and complicates driver

Change from v1:
(https://lore.kernel.org/linux-iommu/[email protected]/)

* Rebase on top of patch "[PATCH] iommu/amd: Remove dead code for exclusion ranges in IVMD" that was submitted by Yu Zhang <[email protected]>.
(https://lore.kernel.org/all/[email protected]/)

* Patch 1: Modified to only rename the marco.

* Patch 3: Reintroduce logic to parse IVMD exclusion flag to allow reserve region of type IOMMU_RESV_RESERVED.

* Patch 4:
- Update the sscan function to scan start, length, flags in hex format only.
- Fix apply_ivmd_cmdline_entries() to not free the cmd, which points to

    the early_ivmd_cmdline_map[]  on failure path.

Suravee Suthikulpanit (2):
iommu/amd: Drop unused global exclusion range fields and init iommu/amd: Do not convert IVMD exclusion range to unity range iommu/amd: Rename unity_map_entry and unit_map to ivmd_entry and
ivhd_entry_map

 drivers/iommu/amd/amd_iommu_types.h | 22 ++++++------
 drivers/iommu/amd/init.c            | 54 ++++++++++++-----------------
 drivers/iommu/amd/iommu.c           | 28 ++++++++++++---
 3 files changed, 58 insertions(+), 46 deletions(-)

Base: 945b3729fca6176fdbb83e38c8116b0132c68fbe

Signature · 1 line
2.34.1
#1 lore raw

PATCH v3 1/3 iommu/amd: Drop unused global exclusion range fields and init

From: Suravee Suthikulpanit <[email protected]>

Remove unused struct amd_iommu.exclusion_start/exclusion_length, the MMIO_EXCL_* flag masks, and iommu_set_exclusion_range(). These struct amd_iommu fields were never assigned, so early_enable_iommu() never programmed the hardware exclusion registers through this path.

Please note that the exclusion range registers have been repurposed to SNP completion-wait store base / limit registers. So, rename MMIO_EXCL_BASE/LIMIT via to MMIO_COMPL_STORE_BASE/LIMIT instead.

---
 drivers/iommu/amd/amd_iommu_types.h | 4 ++--
 drivers/iommu/amd/init.c            | 6 +++---
 2 files changed, 5 insertions(+), 5 deletions(-)
drivers/iommu/amd/amd_iommu_types.h +2 -2
index 8241ef922519..678ceddd711c 100644
@@ -44,8 +44,8 @@
 #define MMIO_CMD_BUF_OFFSET     0x0008
 #define MMIO_EVT_BUF_OFFSET     0x0010
 #define MMIO_CONTROL_OFFSET     0x0018
-#define MMIO_EXCL_BASE_OFFSET   0x0020
-#define MMIO_EXCL_LIMIT_OFFSET  0x0028
+#define MMIO_COMPL_STORE_BASE_OFFSET   0x0020
+#define MMIO_COMPL_STORE_LIMIT_OFFSET  0x0028
 #define MMIO_EXT_FEATURES	0x0030
 #define MMIO_PPR_LOG_OFFSET	0x0038
 #define MMIO_GA_LOG_BASE_OFFSET	0x00e0
drivers/iommu/amd/init.c +3 -3
index 8a410d4aa370..13c96feed64f 100644
@@ -370,14 +370,14 @@ static void iommu_set_cwwb_range(struct amd_iommu *iommu)
 	 * Re-purpose Exclusion base/limit registers for Completion wait
 	 * write-back base/limit.
 	 */
-	memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
+	memcpy_toio(iommu->mmio_base + MMIO_COMPL_STORE_BASE_OFFSET,
 		    &entry, sizeof(entry));
 
 	/* Note:
 	 * Default to 4 Kbytes, which can be specified by setting base
 	 * address equal to the limit address.
 	 */
-	memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
+	memcpy_toio(iommu->mmio_base + MMIO_COMPL_STORE_LIMIT_OFFSET,
 		    &entry, sizeof(entry));
 }
 
@@ -1022,7 +1022,7 @@ static int __init remap_or_alloc_cwwb_sem(struct amd_iommu *iommu)
 		 * completion wait buffer (CWB) address. Read and re-use it.
 		 */
 		pr_info_once("Re-using CWB buffers from the previous kernel\n");
-		paddr = readq(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET) & PM_ADDR_MASK;
+		paddr = readq(iommu->mmio_base + MMIO_COMPL_STORE_BASE_OFFSET) & PM_ADDR_MASK;
 		iommu->cmd_sem = iommu_memremap(paddr, PAGE_SIZE);
 		if (!iommu->cmd_sem)
 			return -ENOMEM;
Signature · 1 line
2.34.1
#4 lore raw

PATCH v3 2/3 iommu/amd: Do not convert IVMD exclusion range to unity range

In reply to Sairaj Kodilkar

From: Suravee Suthikulpanit <[email protected]>

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 warn about invalid flag combinations and unknown flag bits

Fixes 0bbe4ced53e3 ("iommu/amd: Fix the overwritten field in IVMD header")
Cc Baoquan He <[email protected]>
Cc Adrian Huang <[email protected]>
Cc Jerry Snitselaar <[email protected]>
---
 drivers/iommu/amd/amd_iommu_types.h | 12 +++++++-----
 drivers/iommu/amd/init.c            | 26 +++++++++-----------------
 drivers/iommu/amd/iommu.c           | 21 +++++++++++++++++++--
 3 files changed, 35 insertions(+), 24 deletions(-)
drivers/iommu/amd/amd_iommu_types.h +7 -5
index 678ceddd711c..bcb88b476167 100644
@@ -368,9 +368,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		BIT(0)
+#define IVMD_FLAG_IR		BIT(1)
+#define IVMD_FLAG_IW		BIT(2)
+#define IVMD_FLAG_EXCL		BIT(3)
+#define IVMD_FLAG_MASK		GENMASK_U32(3, 0)
 
 /* IOMMU capabilities */
 #define IOMMU_CAP_IOTLB   24
@@ -880,8 +882,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;
 };
 
 /*
drivers/iommu/amd/init.c +9 -17
index 13c96feed64f..1f0ca675df1c 100644
@@ -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
@@ -2636,6 +2631,13 @@ 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_UNITY) &&
+	    !(m->flags & (IVMD_FLAG_IR | IVMD_FLAG_IW | IVMD_FLAG_EXCL))) {
+		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;
+	}
+
 	pci_seg = get_pci_segment(m->pci_seg, ivrs_base);
 	if (pci_seg == NULL)
 		return -ENOMEM;
@@ -2665,17 +2667,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"
@@ -2702,7 +2694,7 @@ static int __init init_memory_definitions(struct acpi_table_header *table)
 
 	while (p < end) {
 		m = (struct ivmd_header *)p;
-		if (m->flags & (IVMD_FLAG_UNITY_MAP | IVMD_FLAG_EXCL_RANGE))
+		if (m->flags & (IVMD_FLAG_UNITY | IVMD_FLAG_EXCL))
 			init_unity_map_range(m, table);
 
 		p += m->length;
drivers/iommu/amd/iommu.c +19 -2
index 56262f6b1f70..699b5f530f11 100644
@@ -3120,12 +3120,29 @@ static void amd_iommu_get_resv_regions(struct device *dev,
 		if (devid < entry->devid_start || devid > entry->devid_end)
 			continue;
 
+		/*
+		 * IVMD_FLAG_UNITY, IVMD_FLAG_IR, IVMD_FLAG_IW are ignored if
+		 * IVMD_FLAG_EXCL is set.
+		 */
+		if ((entry->flags & IVMD_FLAG_EXCL) &&
+		    (entry->flags & (IVMD_FLAG_UNITY | IVMD_FLAG_IR | IVMD_FLAG_IW)))
+			pr_err(FW_BUG "%s: Invalid IVMD flags combination: %#02x for sbdf=%#x. Ignore IW/IR/Unity flags\n",
+			       __func__, entry->flags, sbdf);
+
+		if (entry->flags & ~IVMD_FLAG_MASK)
+			pr_err("%s: Unknown flags: %#02x for sbdf=%#x\n",
+			       __func__, entry->flags & ~IVMD_FLAG_MASK, sbdf);
+
 		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) {
+			type = IOMMU_RESV_RESERVED;
+			prot = 0;
+		}
 
 		region = iommu_alloc_resv_region(entry->address_start,
 						 length, prot, type,
Signature · 1 line
2.34.1
#7 lore raw

PATCH v3 3/3 iommu/amd: Rename unity_map_entry and unit_map to ivmd_entry and ivhd_entry_map

In reply to Sairaj Kodilkar

From: Suravee Suthikulpanit <[email protected]>

Also, rename struct list_head unity_map to ivmd_entry_map along with renaming the unity-map helper functions and add error message when handling invalid IVMD entry.

No functional change intended.

---
 drivers/iommu/amd/amd_iommu_types.h |  6 +++---
 drivers/iommu/amd/init.c            | 22 +++++++++++-----------
 drivers/iommu/amd/iommu.c           |  7 ++++---
 3 files changed, 18 insertions(+), 17 deletions(-)
drivers/iommu/amd/amd_iommu_types.h +3 -3
index bcb88b476167..033cabc5b73a 100644
@@ -609,11 +609,11 @@ struct amd_iommu_pci_seg {
 	u16 *alias_table;
 
 	/*
-	 * A list of required unity mappings we find in ACPI. It is not locked
+	 * A list of required IVMD mappings we find in ACPI. It is not locked
 	 * because as runtime it is only read. It is created at ACPI table
 	 * parsing time.
 	 */
-	struct list_head unity_map;
+	struct list_head ivmd_entry_map;
 };
 
 /*
@@ -869,7 +869,7 @@ struct ivhd_dte_flags {
 /*
  * One entry for unity mappings parsed out of the ACPI table.
  */
-struct unity_map_entry {
+struct ivmd_entry {
 	struct list_head list;
 
 	/* starting device id this entry is used for (including) */
drivers/iommu/amd/init.c +11 -11
index 1f0ca675df1c..9356efe14b3e 100644
@@ -132,7 +132,7 @@ int amd_iommu_pprlog_size = PPRLOG_SIZE_DEF;
 
 /*
  * An AMD IOMMU memory definition structure. It defines things like exclusion
- * ranges for devices and regions that should be unity mapped.
+ * ranges, unity mapping for device and regions.
  */
 struct ivmd_header {
 	u8 type;
@@ -1726,7 +1726,7 @@ static struct amd_iommu_pci_seg *__init alloc_pci_segment(u16 id,
 
 	pci_seg->id = id;
 	init_llist_head(&pci_seg->dev_data_list);
-	INIT_LIST_HEAD(&pci_seg->unity_map);
+	INIT_LIST_HEAD(&pci_seg->ivmd_entry_map);
 	list_add_tail(&pci_seg->list, &amd_iommu_pci_seg_list);
 
 	if (alloc_dev_table(pci_seg))
@@ -2319,8 +2319,8 @@ static int __init amd_iommu_init_pci(void)
 	}
 
 	/*
-	 * Order is important here to make sure any unity map requirements are
-	 * fulfilled. The unity mappings are created and written to the device
+	 * Order is important here to make sure any ivmd map requirements are
+	 * fulfilled. The ivmd mappings are created and written to the device
 	 * table during the iommu_init_pci() call.
 	 *
 	 * After that we call init_device_table_dma() to make sure any
@@ -2606,17 +2606,17 @@ static int iommu_init_irq(struct amd_iommu *iommu)
  *
  * The next functions belong to the third pass of parsing the ACPI
  * table. In this last pass the memory mapping requirements are
- * gathered (like exclusion and unity mapping ranges).
+ * gathered (like exclusion and ivmd mapping ranges).
  *
  ****************************************************************************/
 
-static void __init free_unity_maps(void)
+static void __init free_ivmd_maps(void)
 {
-	struct unity_map_entry *entry, *next;
+	struct ivmd_entry *entry, *next;
 	struct amd_iommu_pci_seg *p, *pci_seg;
 
 	for_each_pci_segment_safe(pci_seg, p) {
-		list_for_each_entry_safe(entry, next, &pci_seg->unity_map, list) {
+		list_for_each_entry_safe(entry, next, &pci_seg->ivmd_entry_map, list) {
 			list_del(&entry->list);
 			kfree(entry);
 		}
@@ -2627,7 +2627,7 @@ static void __init free_unity_maps(void)
 static int __init init_unity_map_range(struct ivmd_header *m,
 				       struct acpi_table_header *ivrs_base)
 {
-	struct unity_map_entry *e = NULL;
+	struct ivmd_entry *e = NULL;
 	struct amd_iommu_pci_seg *pci_seg;
 	char *s;
 
@@ -2678,7 +2678,7 @@ static int __init init_unity_map_range(struct ivmd_header *m,
 		    PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
 		    e->address_start, e->address_end, m->flags);
 
-	list_add_tail(&e->list, &pci_seg->unity_map);
+	list_add_tail(&e->list, &pci_seg->ivmd_entry_map);
 
 	return 0;
 }
@@ -3178,7 +3178,7 @@ static bool __init check_ioapic_information(void)
 static void __init free_dma_resources(void)
 {
 	amd_iommu_pdom_id_destroy();
-	free_unity_maps();
+	free_ivmd_maps();
 }
 
 static void __init ivinfo_init(void *ivrs)
drivers/iommu/amd/iommu.c +4 -3
index 699b5f530f11..d0308fbceac6 100644
@@ -3100,7 +3100,7 @@ static void amd_iommu_get_resv_regions(struct device *dev,
 				       struct list_head *head)
 {
 	struct iommu_resv_region *region;
-	struct unity_map_entry *entry;
+	struct ivmd_entry *entry;
 	struct amd_iommu *iommu;
 	struct amd_iommu_pci_seg *pci_seg;
 	int devid, sbdf;
@@ -3113,7 +3113,7 @@ static void amd_iommu_get_resv_regions(struct device *dev,
 	iommu = get_amd_iommu_from_dev(dev);
 	pci_seg = iommu->pci_seg;
 
-	list_for_each_entry(entry, &pci_seg->unity_map, list) {
+	list_for_each_entry(entry, &pci_seg->ivmd_entry_map, list) {
 		int type, prot = 0;
 		size_t length;
 
@@ -3148,7 +3148,8 @@ static void amd_iommu_get_resv_regions(struct device *dev,
 						 length, prot, type,
 						 GFP_KERNEL);
 		if (!region) {
-			dev_err(dev, "Out of memory allocating dm-regions\n");
+			pr_err("%s: Out of memory allocating reserved regions for sbdf=%#x\n",
+			       __func__, sbdf);
 			return;
 		}
 		list_add_tail(&region->list, head);
Signature · 1 line
2.34.1
#10 lore raw
In reply to Sairaj Kodilkar
Quoted 13 lines · Sairaj Kodilkar

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

This series refreshes how the AMD IOMMU driver models IVRS IVMD entries after the hardware exclusion range registers were repurposed.

The first patch renames the associated MMIO offsets to reflect completion-wait store base/limit.

The second patch drops the workaround that forced IVMD exclusion ranges to r/w unity mappings; the driver no longer programs exclusion range registers, so that the workaround is obsolete.

Quoted 8 lines

The third patch centralizes IVMD flag definitions, renames the unity-map structures to ivmd_entry, stores the raw IVMD flags byte from ACPI, and derives IOMMU reserved regions (including exclusion as IOMMU_RESV_RESERVED) from those flags.

The fourth patch adds an ivmd= kernel parameter so up to four per-device IVMD-style ranges can be supplied from the command line when the IVRS table is wrong or incomplete, documented in kernel-parameters.txt.

if patch 4 is dropped, you can drop mention of patch 4 from cover letter as well.

Quoted 49 lines

Changes from v2:
https://lore.kernel.org/all/[email protected]/#t

*Patch 2: Report an IVMD exclusion range as IOMMU_RESV_RESERVED in this
patch, instead of leaving that for patch 3. This patch stops forcing the exclusion to read/write unity, the range has no read or write permission. iommu_map() rejects that, so a tree checked out at patch 2 does not boot. Reserving the range in the same patch keeps the series bisectable.

* Patch 4: Drop this patch, ivmd= command line does not have any real use and complicates driver

Change from v1:
(https://lore.kernel.org/linux-iommu/[email protected]/)

* Rebase on top of patch "[PATCH] iommu/amd: Remove dead code for exclusion ranges in IVMD" that was submitted by Yu Zhang <[email protected]>.
(https://lore.kernel.org/all/[email protected]/)

* Patch 1: Modified to only rename the marco.

* Patch 3: Reintroduce logic to parse IVMD exclusion flag to allow reserve region of type IOMMU_RESV_RESERVED.

* Patch 4:
- Update the sscan function to scan start, length, flags in hex format only.
- Fix apply_ivmd_cmdline_entries() to not free the cmd, which points to

    the early_ivmd_cmdline_map[]  on failure path.

Suravee Suthikulpanit (2):
iommu/amd: Drop unused global exclusion range fields and init iommu/amd: Do not convert IVMD exclusion range to unity range iommu/amd: Rename unity_map_entry and unit_map to ivmd_entry and
ivhd_entry_map

 drivers/iommu/amd/amd_iommu_types.h | 22 ++++++------
 drivers/iommu/amd/init.c            | 54 ++++++++++++-----------------
 drivers/iommu/amd/iommu.c           | 28 ++++++++++++---
 3 files changed, 58 insertions(+), 46 deletions(-)

Base: 945b3729fca6176fdbb83e38c8116b0132c68fbe
--
2.34.1

#11 lore raw
Quoted 21 lines · Akshay
Quoted 18 lines · Sairaj Kodilkar

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

This series refreshes how the AMD IOMMU driver models IVRS IVMD entries after the hardware exclusion range registers were repurposed.

The first patch renames the associated MMIO offsets to reflect completion-wait store base/limit.

The second patch drops the workaround that forced IVMD exclusion ranges to r/w unity mappings; the driver no longer programs exclusion range registers, so that the workaround is obsolete.

The third patch centralizes IVMD flag definitions, renames the unity-map structures to ivmd_entry, stores the raw IVMD flags byte from ACPI, and derives IOMMU reserved regions (including exclusion as IOMMU_RESV_RESERVED) from those flags.

Quoted 4 lines
Quoted 3 lines

The fourth patch adds an ivmd= kernel parameter so up to four per-device IVMD-style ranges can be supplied from the command line when the IVRS table is wrong or incomplete, documented in kernel-parameters.txt.

if patch 4 is dropped, you can drop mention of patch 4 from cover letter as well.

Thanks for pointing it out

Sairaj

Quoted 49 lines
Quoted 49 lines

Changes from v2:
https://lore.kernel.org/all/[email protected]/#t

*Patch 2: Report an IVMD exclusion range as IOMMU_RESV_RESERVED in this
patch, instead of leaving that for patch 3. This patch stops forcing the exclusion to read/write unity, the range has no read or write permission. iommu_map() rejects that, so a tree checked out at patch 2 does not boot. Reserving the range in the same patch keeps the series bisectable.

* Patch 4: Drop this patch, ivmd= command line does not have any real use and complicates driver

Change from v1:
(https://lore.kernel.org/linux-iommu/[email protected]/)

* Rebase on top of patch "[PATCH] iommu/amd: Remove dead code for exclusion ranges in IVMD" that was submitted by Yu Zhang <[email protected]>.
(https://lore.kernel.org/all/[email protected]/)

* Patch 1: Modified to only rename the marco.

* Patch 3: Reintroduce logic to parse IVMD exclusion flag to allow reserve region of type IOMMU_RESV_RESERVED.

* Patch 4:
- Update the sscan function to scan start, length, flags in hex format only.
- Fix apply_ivmd_cmdline_entries() to not free the cmd, which points to

    the early_ivmd_cmdline_map[]  on failure path.

Suravee Suthikulpanit (2):
iommu/amd: Drop unused global exclusion range fields and init iommu/amd: Do not convert IVMD exclusion range to unity range iommu/amd: Rename unity_map_entry and unit_map to ivmd_entry and
ivhd_entry_map

 drivers/iommu/amd/amd_iommu_types.h | 22 ++++++------
 drivers/iommu/amd/init.c            | 54 ++++++++++++-----------------
 drivers/iommu/amd/iommu.c           | 28 ++++++++++++---
 3 files changed, 58 insertions(+), 46 deletions(-)

Base: 945b3729fca6176fdbb83e38c8116b0132c68fbe
--
2.34.1

Reviewed-by on v3 1/3
#2 lore raw

Re: [PATCH v3 1/3] iommu/amd: Drop unused global exclusion range fields and init

In reply to Sairaj Kodilkar
Quoted 5 lines · Sairaj Kodilkar

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

From: Suravee Suthikulpanit <[email protected]>

Quoted 8 lines

Remove unused struct amd_iommu.exclusion_start/exclusion_length, the MMIO_EXCL_* flag masks, and iommu_set_exclusion_range(). These struct amd_iommu fields were never assigned, so early_enable_iommu() never programmed the hardware exclusion registers through this path.

Please note that the exclusion range registers have been repurposed to SNP completion-wait store base / limit registers. So, rename MMIO_EXCL_BASE/LIMIT via to MMIO_COMPL_STORE_BASE/LIMIT instead.

Quoted 14 lines
---
 drivers/iommu/amd/amd_iommu_types.h | 4 ++--
 drivers/iommu/amd/init.c            | 6 +++---
 2 files changed, 5 insertions(+), 5 deletions(-)
drivers/iommu/amd/amd_iommu_types.h +0 -0
index 8241ef922519..678ceddd711c 100644

drivers/iommu/amd/amd_iommu_types.h line 48

Quoted 7 lines
drivers/iommu/amd/amd_iommu_types.h +2 -2
@@ -44,8 +44,8 @@
 #define MMIO_CMD_BUF_OFFSET     0x0008
 #define MMIO_EVT_BUF_OFFSET     0x0010
 #define MMIO_CONTROL_OFFSET     0x0018
-#define MMIO_EXCL_BASE_OFFSET   0x0020
-#define MMIO_EXCL_LIMIT_OFFSET  0x0028
+#define MMIO_COMPL_STORE_BASE_OFFSET   0x0020
+#define MMIO_COMPL_STORE_LIMIT_OFFSET  0x0028

nit:
1. since it is SW based re-purpose and used in function iommu_set_cwwb_range() suggesting renaming to MMIO_CWWB_BASE_OFFSET & MMIO_CWWB_LIMIT_OFFSET

Quoted 37 lines
drivers/iommu/amd/amd_iommu_types.h +0 -0
 #define MMIO_EXT_FEATURES      0x0030
 #define MMIO_PPR_LOG_OFFSET    0x0038
 #define MMIO_GA_LOG_BASE_OFFSET        0x00e0
drivers/iommu/amd/init.c +3 -4
index 8a410d4aa370..13c96feed64f 100644
@@ -370,14 +370,14 @@ static void iommu_set_cwwb_range(struct amd_iommu *iommu)
         * Re-purpose Exclusion base/limit registers for Completion wait
         * write-back base/limit.
         */
-       memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
+       memcpy_toio(iommu->mmio_base + MMIO_COMPL_STORE_BASE_OFFSET,
                    &entry, sizeof(entry));
 
        /* Note:
         * Default to 4 Kbytes, which can be specified by setting base
         * address equal to the limit address.
         */
-       memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
+       memcpy_toio(iommu->mmio_base + MMIO_COMPL_STORE_LIMIT_OFFSET,
                    &entry, sizeof(entry));
 }
 
@@ -1022,7 +1022,7 @@ static int __init remap_or_alloc_cwwb_sem(struct amd_iommu *iommu)
                 * completion wait buffer (CWB) address. Read and re-use it.
                 */
                pr_info_once("Re-using CWB buffers from the previous kernel\n");
-               paddr = readq(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET) & PM_ADDR_MASK;
+               paddr = readq(iommu->mmio_base + MMIO_COMPL_STORE_BASE_OFFSET) & PM_ADDR_MASK;
                iommu->cmd_sem = iommu_memremap(paddr, PAGE_SIZE);
                if (!iommu->cmd_sem)
                        return -ENOMEM;
--

2.34.1

#5 lore raw

Re: [PATCH v3 2/3] iommu/amd: Do not convert IVMD exclusion range to unity range

In reply to Sairaj Kodilkar
Quoted 118 lines · Sairaj Kodilkar

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

From: Suravee Suthikulpanit <[email protected]>

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 warn about invalid flag combinations and unknown flag bits

Fixes 0bbe4ced53e3 ("iommu/amd: Fix the overwritten field in IVMD header")
Cc Baoquan He <[email protected]>
Cc Adrian Huang <[email protected]>
Cc Jerry Snitselaar <[email protected]>
---
 drivers/iommu/amd/amd_iommu_types.h | 12 +++++++-----
 drivers/iommu/amd/init.c            | 26 +++++++++-----------------
 drivers/iommu/amd/iommu.c           | 21 +++++++++++++++++++--
 3 files changed, 35 insertions(+), 24 deletions(-)
drivers/iommu/amd/amd_iommu_types.h +6 -5
index 678ceddd711c..bcb88b476167 100644
@@ -368,9 +368,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                BIT(0> +#define IVMD_FLAG_IR           BIT(1)
+#define IVMD_FLAG_IW           BIT(2)
+#define IVMD_FLAG_EXCL         BIT(3)
+#define IVMD_FLAG_MASK         GENMASK_U32(3, 0)
 
 /* IOMMU capabilities */
 #define IOMMU_CAP_IOTLB   24
@@ -880,8 +882,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;
 };
 
 /*
drivers/iommu/amd/init.c +8 -16
index 13c96feed64f..1f0ca675df1c 100644
@@ -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
@@ -2636,6 +2631,13 @@ 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_UNITY) &&
+           !(m->flags & (IVMD_FLAG_IR | IVMD_FLAG_IW | IVMD_FLAG_EXCL))) {
+               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;
+       }
+
        pci_seg = get_pci_segment(m->pci_seg, ivrs_base);
        if (pci_seg == NULL)
                return -ENOMEM;
@@ -2665,17 +2667,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"

drivers/iommu/amd/init.c static int __init init_memory_definitions(struct acpi_table_header *table) · line 2697

Quoted 5 lines
drivers/iommu/amd/init.c +1 -1
@@ -2702,7 +2694,7 @@ static int __init init_memory_definitions(struct acpi_table_header *table)
 
        while (p < end) {
                m = (struct ivmd_header *)p;
-               if (m->flags & (IVMD_FLAG_UNITY_MAP | IVMD_FLAG_EXCL_RANGE))
+               if (m->flags & (IVMD_FLAG_UNITY | IVMD_FLAG_EXCL))

the original names were more descriptive, suggest keeping IVMD_FLAG_UNITY_MAP/IVMD_FLAG_EXCL_RANGE rather than shortening them. if you insist on rename, may be send in seperate patch?

Quoted 8 lines
drivers/iommu/amd/init.c +0 -0
                        init_unity_map_range(m, table);
 
                p += m->length;
drivers/iommu/amd/iommu.c +0 -0
index 56262f6b1f70..699b5f530f11 100644

drivers/iommu/amd/iommu.c static void amd_iommu_get_resv_regions(struct device *dev, · line 3128

Quoted 9 lines
drivers/iommu/amd/iommu.c +6 -0
@@ -3120,12 +3120,29 @@ static void amd_iommu_get_resv_regions(struct device *dev,
                if (devid < entry->devid_start || devid > entry->devid_end)
                        continue;
 
+               /*
+                * IVMD_FLAG_UNITY, IVMD_FLAG_IR, IVMD_FLAG_IW are ignored if
+                * IVMD_FLAG_EXCL is set.
+                */
+               if ((entry->flags & IVMD_FLAG_EXCL) &&
+                   (entry->flags & (IVMD_FLAG_UNITY | IVMD_FLAG_IR | IVMD_FLAG_IW)))

Here EXCL & UNITY are flagged as invalid combination Above UNITY & !EXCL are also flagged as invalid. Suggesting revisiting these conditions.

Quoted 9 lines
drivers/iommu/amd/iommu.c +7 -0
+                       pr_err(FW_BUG "%s: Invalid IVMD flags combination: %#02x for sbdf=%#x. Ignore IW/IR/Unity flags\n",
+                              __func__, entry->flags, sbdf);
+
+               if (entry->flags & ~IVMD_FLAG_MASK)
+                       pr_err("%s: Unknown flags: %#02x for sbdf=%#x\n",
+                              __func__, entry->flags & ~IVMD_FLAG_MASK, sbdf);
+
                type   = IOMMU_RESV_DIRECT;
                length = entry->address_end - entry->address_start;

drivers/iommu/amd/iommu.c static void amd_iommu_get_resv_regions(struct device *dev, · line 3145

Quoted 10 lines
drivers/iommu/amd/iommu.c +6 -2
-               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) {
+                       type = IOMMU_RESV_RESERVED;
+                       prot = 0;
+               }

Since in case of invalid flag combinations, we are going ahead with region allocation, will it be beneficial to add a pr_debug emitting relevant details passed on to the iommu_alloc_resv_region()?

Quoted 7 lines
drivers/iommu/amd/iommu.c +0 -1
                region = iommu_alloc_resv_region(entry->address_start,
                                                 length, prot, type,
--

2.34.1

Reviewed-by on v3 3/3
#8 lore raw

Re: [PATCH v3 3/3] iommu/amd: Rename unity_map_entry and unit_map to ivmd_entry and ivhd_entry_map

In reply to Sairaj Kodilkar
Quoted 10 lines · Sairaj Kodilkar

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

From: Suravee Suthikulpanit <[email protected]>

Also, rename struct list_head unity_map to ivmd_entry_map along with renaming the unity-map helper functions and add error message when handling invalid IVMD entry.

No functional change intended.

Quoted 15 lines
---
 drivers/iommu/amd/amd_iommu_types.h |  6 +++---
 drivers/iommu/amd/init.c            | 22 +++++++++++-----------
 drivers/iommu/amd/iommu.c           |  7 ++++---
 3 files changed, 18 insertions(+), 17 deletions(-)
drivers/iommu/amd/amd_iommu_types.h +0 -0
index bcb88b476167..033cabc5b73a 100644

drivers/iommu/amd/amd_iommu_types.h struct amd_iommu_pci_seg { · line 616

Quoted 10 lines
drivers/iommu/amd/amd_iommu_types.h +2 -2
@@ -609,11 +609,11 @@ struct amd_iommu_pci_seg {
        u16 *alias_table;
 
        /*
-        * A list of required unity mappings we find in ACPI. It is not locked
+        * A list of required IVMD mappings we find in ACPI. It is not locked
         * because as runtime it is only read. It is created at ACPI table
         * parsing time.
         */
-       struct list_head unity_map;
+       struct list_head ivmd_entry_map;

nit:
IMO unity_map term was based on spec, which was synonymous with identity mappings. If unity_map term is not being used, then I would suggest keeping it as ivmd_entry_list instead of _map, since it is simply a collection of ivmd entries

Quoted 130 lines
drivers/iommu/amd/amd_iommu_types.h +1 -1
 };
 
 /*
@@ -869,7 +869,7 @@ struct ivhd_dte_flags {
 /*
  * One entry for unity mappings parsed out of the ACPI table.
  */
-struct unity_map_entry {
+struct ivmd_entry {
        struct list_head list;
 
        /* starting device id this entry is used for (including) */
drivers/iommu/amd/init.c +11 -11
index 1f0ca675df1c..9356efe14b3e 100644
@@ -132,7 +132,7 @@ int amd_iommu_pprlog_size = PPRLOG_SIZE_DEF;
 
 /*
  * An AMD IOMMU memory definition structure. It defines things like exclusion
- * ranges for devices and regions that should be unity mapped.
+ * ranges, unity mapping for device and regions.
  */
 struct ivmd_header {
        u8 type;
@@ -1726,7 +1726,7 @@ static struct amd_iommu_pci_seg *__init alloc_pci_segment(u16 id,
 
        pci_seg->id = id;
        init_llist_head(&pci_seg->dev_data_list);
-       INIT_LIST_HEAD(&pci_seg->unity_map);
+       INIT_LIST_HEAD(&pci_seg->ivmd_entry_map);
        list_add_tail(&pci_seg->list, &amd_iommu_pci_seg_list);
 
        if (alloc_dev_table(pci_seg))
@@ -2319,8 +2319,8 @@ static int __init amd_iommu_init_pci(void)
        }
 
        /*
-        * Order is important here to make sure any unity map requirements are
-        * fulfilled. The unity mappings are created and written to the device
+        * Order is important here to make sure any ivmd map requirements are
+        * fulfilled. The ivmd mappings are created and written to the device
         * table during the iommu_init_pci() call.
         *
         * After that we call init_device_table_dma() to make sure any
@@ -2606,17 +2606,17 @@ static int iommu_init_irq(struct amd_iommu *iommu)
  *
  * The next functions belong to the third pass of parsing the ACPI
  * table. In this last pass the memory mapping requirements are
- * gathered (like exclusion and unity mapping ranges).
+ * gathered (like exclusion and ivmd mapping ranges).
  *
  ****************************************************************************/
 
-static void __init free_unity_maps(void)
+static void __init free_ivmd_maps(void)
 {
-       struct unity_map_entry *entry, *next;
+       struct ivmd_entry *entry, *next;
        struct amd_iommu_pci_seg *p, *pci_seg;
 
        for_each_pci_segment_safe(pci_seg, p) {
-               list_for_each_entry_safe(entry, next, &pci_seg->unity_map, list) {
+               list_for_each_entry_safe(entry, next, &pci_seg->ivmd_entry_map, list) {
                        list_del(&entry->list);
                        kfree(entry);
                }
@@ -2627,7 +2627,7 @@ static void __init free_unity_maps(void)
 static int __init init_unity_map_range(struct ivmd_header *m,
                                       struct acpi_table_header *ivrs_base)
 {
-       struct unity_map_entry *e = NULL;
+       struct ivmd_entry *e = NULL;
        struct amd_iommu_pci_seg *pci_seg;
        char *s;
 
@@ -2678,7 +2678,7 @@ static int __init init_unity_map_range(struct ivmd_header *m,
                    PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
                    e->address_start, e->address_end, m->flags);
 
-       list_add_tail(&e->list, &pci_seg->unity_map);
+       list_add_tail(&e->list, &pci_seg->ivmd_entry_map);
 
        return 0;
 }
@@ -3178,7 +3178,7 @@ static bool __init check_ioapic_information(void)
 static void __init free_dma_resources(void)
 {
        amd_iommu_pdom_id_destroy();
-       free_unity_maps();
+       free_ivmd_maps();
 }
 
 static void __init ivinfo_init(void *ivrs)
drivers/iommu/amd/iommu.c +4 -4
index 699b5f530f11..d0308fbceac6 100644
@@ -3100,7 +3100,7 @@ static void amd_iommu_get_resv_regions(struct device *dev,
                                       struct list_head *head)
 {
        struct iommu_resv_region *region;
-       struct unity_map_entry *entry;
+       struct ivmd_entry *entry;
        struct amd_iommu *iommu;
        struct amd_iommu_pci_seg *pci_seg;
        int devid, sbdf;
@@ -3113,7 +3113,7 @@ static void amd_iommu_get_resv_regions(struct device *dev,
        iommu = get_amd_iommu_from_dev(dev);
        pci_seg = iommu->pci_seg;
 
-       list_for_each_entry(entry, &pci_seg->unity_map, list) {
+       list_for_each_entry(entry, &pci_seg->ivmd_entry_map, list) {
                int type, prot = 0;
                size_t length;
 
@@ -3148,7 +3148,8 @@ static void amd_iommu_get_resv_regions(struct device *dev,
                                                 length, prot, type,
                                                 GFP_KERNEL);
                if (!region) {
-                       dev_err(dev, "Out of memory allocating dm-regions\n");
+                       pr_err("%s: Out of memory allocating reserved regions for sbdf=%#x\n",
+                              __func__, sbdf);
                        return;
                }
                list_add_tail(&region->list, head);
--

2.34.1

#3 lore raw

Re: [PATCH v3 1/3] iommu/amd: Drop unused global exclusion range fields and init

In reply to Jindal, Akshay
Quoted 26 lines · Akshay
Quoted 5 lines · Sairaj Kodilkar

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

From: Suravee Suthikulpanit <[email protected]>

Quoted 8 lines

Remove unused struct amd_iommu.exclusion_start/exclusion_length, the MMIO_EXCL_* flag masks, and iommu_set_exclusion_range(). These struct amd_iommu fields were never assigned, so early_enable_iommu() never programmed the hardware exclusion registers through this path.

Please note that the exclusion range registers have been repurposed to SNP completion-wait store base / limit registers. So, rename MMIO_EXCL_BASE/LIMIT via to MMIO_COMPL_STORE_BASE/LIMIT instead.

Quoted 9 lines
---
 drivers/iommu/amd/amd_iommu_types.h | 4 ++--
 drivers/iommu/amd/init.c            | 6 +++---
 2 files changed, 5 insertions(+), 5 deletions(-)
Quoted 15 lines

drivers/iommu/amd/amd_iommu_types.h line 48

Quoted 12 lines
drivers/iommu/amd/amd_iommu_types.h +2 -2
index 8241ef922519..678ceddd711c 100644
@@ -44,8 +44,8 @@
 #define MMIO_CMD_BUF_OFFSET     0x0008
 #define MMIO_EVT_BUF_OFFSET     0x0010
 #define MMIO_CONTROL_OFFSET     0x0018
-#define MMIO_EXCL_BASE_OFFSET   0x0020
-#define MMIO_EXCL_LIMIT_OFFSET  0x0028
+#define MMIO_COMPL_STORE_BASE_OFFSET   0x0020
+#define MMIO_COMPL_STORE_LIMIT_OFFSET  0x0028

nit:
1. since it is SW based re-purpose and used in function iommu_set_cwwb_range() suggesting renaming to MMIO_CWWB_BASE_OFFSET & MMIO_CWWB_LIMIT_OFFSET

What do you mean by SW based re-purpose. Hardware uses the register 0x0020 to store completion wait base when SNP is enabled.

The IOMMU driver always use reg 0x0020 to store the completion wait hence I am in favour of using *EXCL* naming.

Thanks
Sairaj

Quoted 37 lines
Quoted 37 lines
drivers/iommu/amd/amd_iommu_types.h +0 -0
 #define MMIO_EXT_FEATURES      0x0030
 #define MMIO_PPR_LOG_OFFSET    0x0038
 #define MMIO_GA_LOG_BASE_OFFSET        0x00e0
drivers/iommu/amd/init.c +3 -4
index 8a410d4aa370..13c96feed64f 100644
@@ -370,14 +370,14 @@ static void iommu_set_cwwb_range(struct amd_iommu *iommu)
         * Re-purpose Exclusion base/limit registers for Completion wait
         * write-back base/limit.
         */
-       memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
+       memcpy_toio(iommu->mmio_base + MMIO_COMPL_STORE_BASE_OFFSET,
                    &entry, sizeof(entry));
 
        /* Note:
         * Default to 4 Kbytes, which can be specified by setting base
         * address equal to the limit address.
         */
-       memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
+       memcpy_toio(iommu->mmio_base + MMIO_COMPL_STORE_LIMIT_OFFSET,
                    &entry, sizeof(entry));
 }
 
@@ -1022,7 +1022,7 @@ static int __init remap_or_alloc_cwwb_sem(struct amd_iommu *iommu)
                 * completion wait buffer (CWB) address. Read and re-use it.
                 */
                pr_info_once("Re-using CWB buffers from the previous kernel\n");
-               paddr = readq(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET) & PM_ADDR_MASK;
+               paddr = readq(iommu->mmio_base + MMIO_COMPL_STORE_BASE_OFFSET) & PM_ADDR_MASK;
                iommu->cmd_sem = iommu_memremap(paddr, PAGE_SIZE);
                if (!iommu->cmd_sem)
                        return -ENOMEM;
--

2.34.1

#6 lore raw

Re: [PATCH v3 2/3] iommu/amd: Do not convert IVMD exclusion range to unity range

In reply to Jindal, Akshay
Quoted 164 lines · Akshay
Quoted 118 lines · Sairaj Kodilkar

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

From: Suravee Suthikulpanit <[email protected]>

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 warn about invalid flag combinations and unknown flag bits

Fixes 0bbe4ced53e3 ("iommu/amd: Fix the overwritten field in IVMD header")
Cc Baoquan He <[email protected]>
Cc Adrian Huang <[email protected]>
Cc Jerry Snitselaar <[email protected]>
---
 drivers/iommu/amd/amd_iommu_types.h | 12 +++++++-----
 drivers/iommu/amd/init.c            | 26 +++++++++-----------------
 drivers/iommu/amd/iommu.c           | 21 +++++++++++++++++++--
 3 files changed, 35 insertions(+), 24 deletions(-)
drivers/iommu/amd/amd_iommu_types.h +6 -5
index 678ceddd711c..bcb88b476167 100644
@@ -368,9 +368,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                BIT(0> +#define IVMD_FLAG_IR           BIT(1)
+#define IVMD_FLAG_IW           BIT(2)
+#define IVMD_FLAG_EXCL         BIT(3)
+#define IVMD_FLAG_MASK         GENMASK_U32(3, 0)
 
 /* IOMMU capabilities */
 #define IOMMU_CAP_IOTLB   24
@@ -880,8 +882,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;
 };
 
 /*
drivers/iommu/amd/init.c +8 -16
index 13c96feed64f..1f0ca675df1c 100644
@@ -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
@@ -2636,6 +2631,13 @@ 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_UNITY) &&
+           !(m->flags & (IVMD_FLAG_IR | IVMD_FLAG_IW | IVMD_FLAG_EXCL))) {
+               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;
+       }
+
        pci_seg = get_pci_segment(m->pci_seg, ivrs_base);
        if (pci_seg == NULL)
                return -ENOMEM;
@@ -2665,17 +2667,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"

drivers/iommu/amd/init.c static int __init init_memory_definitions(struct acpi_table_header *table) · line 2697

Quoted 5 lines
drivers/iommu/amd/init.c +1 -1
@@ -2702,7 +2694,7 @@ static int __init init_memory_definitions(struct acpi_table_header *table)
 
        while (p < end) {
                m = (struct ivmd_header *)p;
-               if (m->flags & (IVMD_FLAG_UNITY_MAP | IVMD_FLAG_EXCL_RANGE))
+               if (m->flags & (IVMD_FLAG_UNITY | IVMD_FLAG_EXCL))

the original names were more descriptive, suggest keeping IVMD_FLAG_UNITY_MAP/IVMD_FLAG_EXCL_RANGE rather than shortening them. if you insist on rename, may be send in seperate patch?

Quoted 8 lines
drivers/iommu/amd/init.c +0 -0
                        init_unity_map_range(m, table);
 
                p += m->length;
drivers/iommu/amd/iommu.c +0 -0
index 56262f6b1f70..699b5f530f11 100644

drivers/iommu/amd/iommu.c static void amd_iommu_get_resv_regions(struct device *dev, · line 3128

Quoted 9 lines
drivers/iommu/amd/iommu.c +6 -0
@@ -3120,12 +3120,29 @@ static void amd_iommu_get_resv_regions(struct device *dev,
                if (devid < entry->devid_start || devid > entry->devid_end)
                        continue;
 
+               /*
+                * IVMD_FLAG_UNITY, IVMD_FLAG_IR, IVMD_FLAG_IW are ignored if
+                * IVMD_FLAG_EXCL is set.
+                */
+               if ((entry->flags & IVMD_FLAG_EXCL) &&
+                   (entry->flags & (IVMD_FLAG_UNITY | IVMD_FLAG_IR | IVMD_FLAG_IW)))

Here EXCL & UNITY are flagged as invalid combination Above UNITY & !EXCL are also flagged as invalid. Suggesting revisiting these conditions.

Quoted 14 lines
drivers/iommu/amd/iommu.c +9 -2
+                       pr_err(FW_BUG "%s: Invalid IVMD flags combination: %#02x for sbdf=%#x. Ignore IW/IR/Unity flags\n",
+                              __func__, entry->flags, sbdf);
+
+               if (entry->flags & ~IVMD_FLAG_MASK)
+                       pr_err("%s: Unknown flags: %#02x for sbdf=%#x\n",
+                              __func__, entry->flags & ~IVMD_FLAG_MASK, sbdf);
+
                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)
Quoted 8 lines

drivers/iommu/amd/iommu.c static void amd_iommu_get_resv_regions(struct device *dev, · line 3145

Quoted 5 lines
drivers/iommu/amd/iommu.c +4 -0
                        prot |= IOMMU_WRITE;
+               if (entry->flags & IVMD_FLAG_EXCL) {
+                       type = IOMMU_RESV_RESERVED;
+                       prot = 0;
+               }

Since in case of invalid flag combinations, we are going ahead with region allocation, will it be beneficial to add a pr_debug emitting relevant details passed on to the iommu_alloc_resv_region()?

Sorry I made a mistake here,

Here's summary of what I wanted to do,
1. Reject UNITY mapping if IR and IW flags are not present
2. Reject exclusion mapping if either UNITY / IR / IW flags are present

Thanks for pointing it out. I'll update it in the next version and move all the firmware related checks inside init_unity_map_range(). I'll also reject this condition as it does not make sense to add the region with invalid flag combination (a region cannot be both UNITY and EXCL).

Thanks
Sairaj

Quoted 7 lines
Quoted 7 lines
drivers/iommu/amd/iommu.c +0 -1
                region = iommu_alloc_resv_region(entry->address_start,
                                                 length, prot, type,
--

2.34.1

#9 lore raw

Re: [PATCH v3 3/3] iommu/amd: Rename unity_map_entry and unit_map to ivmd_entry and ivhd_entry_map

In reply to Jindal, Akshay
Quoted 31 lines · Akshay
Quoted 10 lines · Sairaj Kodilkar

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

From: Suravee Suthikulpanit <[email protected]>

Also, rename struct list_head unity_map to ivmd_entry_map along with renaming the unity-map helper functions and add error message when handling invalid IVMD entry.

No functional change intended.

Quoted 17 lines
---
 drivers/iommu/amd/amd_iommu_types.h |  6 +++---
 drivers/iommu/amd/init.c            | 22 +++++++++++-----------
 drivers/iommu/amd/iommu.c           |  7 ++++---
 3 files changed, 18 insertions(+), 17 deletions(-)
drivers/iommu/amd/amd_iommu_types.h +0 -0
index bcb88b476167..033cabc5b73a 100644
@@ -609,11 +609,11 @@ struct amd_iommu_pci_seg {
        u16 *alias_table;
 
Quoted 12 lines

drivers/iommu/amd/amd_iommu_types.h struct amd_iommu_pci_seg { · line 616

Quoted 8 lines
drivers/iommu/amd/amd_iommu_types.h +2 -2
        /*
-        * A list of required unity mappings we find in ACPI. It is not locked
+        * A list of required IVMD mappings we find in ACPI. It is not locked
         * because as runtime it is only read. It is created at ACPI table
         * parsing time.
         */
-       struct list_head unity_map;
+       struct list_head ivmd_entry_map;

nit:
IMO unity_map term was based on spec, which was synonymous with identity mappings. If unity_map term is not being used, then I would suggest keeping it as ivmd_entry_list instead of _map, since it is simply a collection of ivmd entries

Sure.

Thanks
Sairaj

Quoted 130 lines
Quoted 130 lines
drivers/iommu/amd/amd_iommu_types.h +1 -1
 };
 
 /*
@@ -869,7 +869,7 @@ struct ivhd_dte_flags {
 /*
  * One entry for unity mappings parsed out of the ACPI table.
  */
-struct unity_map_entry {
+struct ivmd_entry {
        struct list_head list;
 
        /* starting device id this entry is used for (including) */
drivers/iommu/amd/init.c +11 -11
index 1f0ca675df1c..9356efe14b3e 100644
@@ -132,7 +132,7 @@ int amd_iommu_pprlog_size = PPRLOG_SIZE_DEF;
 
 /*
  * An AMD IOMMU memory definition structure. It defines things like exclusion
- * ranges for devices and regions that should be unity mapped.
+ * ranges, unity mapping for device and regions.
  */
 struct ivmd_header {
        u8 type;
@@ -1726,7 +1726,7 @@ static struct amd_iommu_pci_seg *__init alloc_pci_segment(u16 id,
 
        pci_seg->id = id;
        init_llist_head(&pci_seg->dev_data_list);
-       INIT_LIST_HEAD(&pci_seg->unity_map);
+       INIT_LIST_HEAD(&pci_seg->ivmd_entry_map);
        list_add_tail(&pci_seg->list, &amd_iommu_pci_seg_list);
 
        if (alloc_dev_table(pci_seg))
@@ -2319,8 +2319,8 @@ static int __init amd_iommu_init_pci(void)
        }
 
        /*
-        * Order is important here to make sure any unity map requirements are
-        * fulfilled. The unity mappings are created and written to the device
+        * Order is important here to make sure any ivmd map requirements are
+        * fulfilled. The ivmd mappings are created and written to the device
         * table during the iommu_init_pci() call.
         *
         * After that we call init_device_table_dma() to make sure any
@@ -2606,17 +2606,17 @@ static int iommu_init_irq(struct amd_iommu *iommu)
  *
  * The next functions belong to the third pass of parsing the ACPI
  * table. In this last pass the memory mapping requirements are
- * gathered (like exclusion and unity mapping ranges).
+ * gathered (like exclusion and ivmd mapping ranges).
  *
  ****************************************************************************/
 
-static void __init free_unity_maps(void)
+static void __init free_ivmd_maps(void)
 {
-       struct unity_map_entry *entry, *next;
+       struct ivmd_entry *entry, *next;
        struct amd_iommu_pci_seg *p, *pci_seg;
 
        for_each_pci_segment_safe(pci_seg, p) {
-               list_for_each_entry_safe(entry, next, &pci_seg->unity_map, list) {
+               list_for_each_entry_safe(entry, next, &pci_seg->ivmd_entry_map, list) {
                        list_del(&entry->list);
                        kfree(entry);
                }
@@ -2627,7 +2627,7 @@ static void __init free_unity_maps(void)
 static int __init init_unity_map_range(struct ivmd_header *m,
                                       struct acpi_table_header *ivrs_base)
 {
-       struct unity_map_entry *e = NULL;
+       struct ivmd_entry *e = NULL;
        struct amd_iommu_pci_seg *pci_seg;
        char *s;
 
@@ -2678,7 +2678,7 @@ static int __init init_unity_map_range(struct ivmd_header *m,
                    PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
                    e->address_start, e->address_end, m->flags);
 
-       list_add_tail(&e->list, &pci_seg->unity_map);
+       list_add_tail(&e->list, &pci_seg->ivmd_entry_map);
 
        return 0;
 }
@@ -3178,7 +3178,7 @@ static bool __init check_ioapic_information(void)
 static void __init free_dma_resources(void)
 {
        amd_iommu_pdom_id_destroy();
-       free_unity_maps();
+       free_ivmd_maps();
 }
 
 static void __init ivinfo_init(void *ivrs)
drivers/iommu/amd/iommu.c +4 -4
index 699b5f530f11..d0308fbceac6 100644
@@ -3100,7 +3100,7 @@ static void amd_iommu_get_resv_regions(struct device *dev,
                                       struct list_head *head)
 {
        struct iommu_resv_region *region;
-       struct unity_map_entry *entry;
+       struct ivmd_entry *entry;
        struct amd_iommu *iommu;
        struct amd_iommu_pci_seg *pci_seg;
        int devid, sbdf;
@@ -3113,7 +3113,7 @@ static void amd_iommu_get_resv_regions(struct device *dev,
        iommu = get_amd_iommu_from_dev(dev);
        pci_seg = iommu->pci_seg;
 
-       list_for_each_entry(entry, &pci_seg->unity_map, list) {
+       list_for_each_entry(entry, &pci_seg->ivmd_entry_map, list) {
                int type, prot = 0;
                size_t length;
 
@@ -3148,7 +3148,8 @@ static void amd_iommu_get_resv_regions(struct device *dev,
                                                 length, prot, type,
                                                 GFP_KERNEL);
                if (!region) {
-                       dev_err(dev, "Out of memory allocating dm-regions\n");
+                       pr_err("%s: Out of memory allocating reserved regions for sbdf=%#x\n",
+                              __func__, sbdf);
                        return;
                }
                list_add_tail(&region->list, head);
--

2.34.1