Skip to contentSkip to conversation

iommu/amd: Use 128-bit cmpxchg operation to update DTE

PATCH Superseded Suravee Suthikulpanit 4 participants 24 messages Nov 13, 2024 – Nov 14, 2024

Subsystems (3)

v10 of 12 Superseded Latest is v12 →

lore
Share & tools
lore

Suravee Suthikulpanit

author reviewer
#0 lore raw

v10 cover iommu/amd: Use 128-bit cmpxchg operation to update DTE

This series modifies current implementation to use 128-bit cmpxchg to update DTE when needed as specified in the AMD I/O Virtualization Techonology (IOMMU) Specification.

Please note that I have verified with the hardware designer, and they have confirmed that the IOMMU hardware has always been implemented with 256-bit read. The next revision of the IOMMU spec will be updated to correctly describe this part. Therefore, I have updated the implementation to avoid unnecessary flushing.

Changes in v10:
* Patch 3: Update patch from Uros.
* Patch 5,7: Change to use __READ_ONCE() with 128-bit data type.

v9: https://lore.kernel.org/lkml/[email protected]/
v8: https://lore.kernel.org/lkml/[email protected]/
v7: https://lore.kernel.org/lkml/[email protected]/
v6: https://lore.kernel.org/lkml/[email protected]/
v5: https://lore.kernel.org/lkml/[email protected]/
v4: https://lore.kernel.org/lkml/[email protected]/
v3: https://lore.kernel.org/lkml/[email protected]/
v2: https://lore.kernel.org/lkml/[email protected]/
v1: https://lore.kernel.org/lkml/[email protected]/

Thanks,
Suravee

Suravee Suthikulpanit (9):
iommu/amd: Misc ACPI IVRS debug info clean up
iommu/amd: Disable AMD IOMMU if CMPXCHG16B feature is not supported iommu/amd: Introduce struct ivhd_dte_flags to store persistent DTE
flags
iommu/amd: Introduce helper function to update 256-bit DTE iommu/amd: Modify set_dte_entry() to use 256-bit DTE helpers iommu/amd: Introduce helper function get_dte256() iommu/amd: Modify clear_dte_entry() to avoid in-place update iommu/amd: Lock DTE before updating the entry with WRITE_ONCE() iommu/amd: Remove amd_iommu_apply_erratum_63()

Uros Bizjak (1):
compiler_types.h: Introduce 128-bit unqualified scalar type support

 drivers/iommu/amd/amd_iommu.h       |   4 +-
 drivers/iommu/amd/amd_iommu_types.h |  41 ++-
 drivers/iommu/amd/init.c            | 229 +++++++++--------
 drivers/iommu/amd/iommu.c           | 370 ++++++++++++++++++++--------
 include/linux/compiler_types.h      |  13 +
 5 files changed, 445 insertions(+), 212 deletions(-)
Signature · 1 line
2.34.1
author reviewer
#1 lore raw

PATCH v10 1/10 iommu/amd: Misc ACPI IVRS debug info clean up

* Remove redundant AMD-Vi prefix.
* Print IVHD device entry settings field using hex value.
* Print root device of IVHD ACPI device entry using hex value.

---
 drivers/iommu/amd/amd_iommu_types.h |  2 +-
 drivers/iommu/amd/init.c            | 35 +++++++++++++----------------
 2 files changed, 16 insertions(+), 21 deletions(-)
drivers/iommu/amd/amd_iommu_types.h +1 -1
index fdb0357e0bb9..af87b1d094c1 100644
@@ -468,7 +468,7 @@ extern bool amd_iommu_dump;
 #define DUMP_printk(format, arg...)				\
 	do {							\
 		if (amd_iommu_dump)				\
-			pr_info("AMD-Vi: " format, ## arg);	\
+			pr_info(format, ## arg);	\
 	} while(0);
 
 /* global flag if IOMMUs cache non-present entries */
drivers/iommu/amd/init.c +15 -20
index 0e0a531042ac..3a7b2b0472fa 100644
@@ -1239,7 +1239,7 @@ static int __init add_acpi_hid_device(u8 *hid, u8 *uid, u32 *devid,
 	entry->cmd_line	= cmd_line;
 	entry->root_devid = (entry->devid & (~0x7));
 
-	pr_info("%s, add hid:%s, uid:%s, rdevid:%d\n",
+	pr_info("%s, add hid:%s, uid:%s, rdevid:%#x\n",
 		entry->cmd_line ? "cmd" : "ivrs",
 		entry->hid, entry->uid, entry->root_devid);
 
@@ -1331,15 +1331,14 @@ static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
 		switch (e->type) {
 		case IVHD_DEV_ALL:
 
-			DUMP_printk("  DEV_ALL\t\t\tflags: %02x\n", e->flags);
+			DUMP_printk("  DEV_ALL\t\t\tsetting: %#02x\n", e->flags);
 
 			for (dev_i = 0; dev_i <= pci_seg->last_bdf; ++dev_i)
 				set_dev_entry_from_acpi(iommu, dev_i, e->flags, 0);
 			break;
 		case IVHD_DEV_SELECT:
 
-			DUMP_printk("  DEV_SELECT\t\t\t devid: %04x:%02x:%02x.%x "
-				    "flags: %02x\n",
+			DUMP_printk("  DEV_SELECT\t\t\tdevid: %04x:%02x:%02x.%x flags: %#02x\n",
 				    seg_id, PCI_BUS_NUM(e->devid),
 				    PCI_SLOT(e->devid),
 				    PCI_FUNC(e->devid),
@@ -1350,8 +1349,7 @@ static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
 			break;
 		case IVHD_DEV_SELECT_RANGE_START:
 
-			DUMP_printk("  DEV_SELECT_RANGE_START\t "
-				    "devid: %04x:%02x:%02x.%x flags: %02x\n",
+			DUMP_printk("  DEV_SELECT_RANGE_START\tdevid: %04x:%02x:%02x.%x flags: %#02x\n",
 				    seg_id, PCI_BUS_NUM(e->devid),
 				    PCI_SLOT(e->devid),
 				    PCI_FUNC(e->devid),
@@ -1364,8 +1362,7 @@ static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
 			break;
 		case IVHD_DEV_ALIAS:
 
-			DUMP_printk("  DEV_ALIAS\t\t\t devid: %04x:%02x:%02x.%x "
-				    "flags: %02x devid_to: %02x:%02x.%x\n",
+			DUMP_printk("  DEV_ALIAS\t\t\tdevid: %04x:%02x:%02x.%x flags: %#02x devid_to: %02x:%02x.%x\n",
 				    seg_id, PCI_BUS_NUM(e->devid),
 				    PCI_SLOT(e->devid),
 				    PCI_FUNC(e->devid),
@@ -1382,9 +1379,7 @@ static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
 			break;
 		case IVHD_DEV_ALIAS_RANGE:
 
-			DUMP_printk("  DEV_ALIAS_RANGE\t\t "
-				    "devid: %04x:%02x:%02x.%x flags: %02x "
-				    "devid_to: %04x:%02x:%02x.%x\n",
+			DUMP_printk("  DEV_ALIAS_RANGE\t\tdevid: %04x:%02x:%02x.%x flags: %#02x devid_to: %04x:%02x:%02x.%x\n",
 				    seg_id, PCI_BUS_NUM(e->devid),
 				    PCI_SLOT(e->devid),
 				    PCI_FUNC(e->devid),
@@ -1401,8 +1396,7 @@ static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
 			break;
 		case IVHD_DEV_EXT_SELECT:
 
-			DUMP_printk("  DEV_EXT_SELECT\t\t devid: %04x:%02x:%02x.%x "
-				    "flags: %02x ext: %08x\n",
+			DUMP_printk("  DEV_EXT_SELECT\t\tdevid: %04x:%02x:%02x.%x flags: %#02x ext: %08x\n",
 				    seg_id, PCI_BUS_NUM(e->devid),
 				    PCI_SLOT(e->devid),
 				    PCI_FUNC(e->devid),
@@ -1414,8 +1408,7 @@ static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
 			break;
 		case IVHD_DEV_EXT_SELECT_RANGE:
 
-			DUMP_printk("  DEV_EXT_SELECT_RANGE\t devid: "
-				    "%04x:%02x:%02x.%x flags: %02x ext: %08x\n",
+			DUMP_printk("  DEV_EXT_SELECT_RANGE\tdevid: %04x:%02x:%02x.%x flags: %#02x ext: %08x\n",
 				    seg_id, PCI_BUS_NUM(e->devid),
 				    PCI_SLOT(e->devid),
 				    PCI_FUNC(e->devid),
@@ -1428,7 +1421,7 @@ static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
 			break;
 		case IVHD_DEV_RANGE_END:
 
-			DUMP_printk("  DEV_RANGE_END\t\t devid: %04x:%02x:%02x.%x\n",
+			DUMP_printk("  DEV_RANGE_END\t\tdevid: %04x:%02x:%02x.%x\n",
 				    seg_id, PCI_BUS_NUM(e->devid),
 				    PCI_SLOT(e->devid),
 				    PCI_FUNC(e->devid));
@@ -1461,11 +1454,12 @@ static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
 			else
 				var = "UNKNOWN";
 
-			DUMP_printk("  DEV_SPECIAL(%s[%d])\t\tdevid: %04x:%02x:%02x.%x\n",
+			DUMP_printk("  DEV_SPECIAL(%s[%d])\t\tdevid: %04x:%02x:%02x.%x, flags: %#02x\n",
 				    var, (int)handle,
 				    seg_id, PCI_BUS_NUM(devid),
 				    PCI_SLOT(devid),
-				    PCI_FUNC(devid));
+				    PCI_FUNC(devid),
+				    e->flags);
 
 			ret = add_special_device(type, handle, &devid, false);
 			if (ret)
@@ -1525,11 +1519,12 @@ static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
 			}
 
 			devid = PCI_SEG_DEVID_TO_SBDF(seg_id, e->devid);
-			DUMP_printk("  DEV_ACPI_HID(%s[%s])\t\tdevid: %04x:%02x:%02x.%x\n",
+			DUMP_printk("  DEV_ACPI_HID(%s[%s])\t\tdevid: %04x:%02x:%02x.%x, flags: %#02x\n",
 				    hid, uid, seg_id,
 				    PCI_BUS_NUM(devid),
 				    PCI_SLOT(devid),
-				    PCI_FUNC(devid));
+				    PCI_FUNC(devid),
+				    e->flags);
 
 			flags = e->flags;
 
Signature · 1 line
2.34.1
author reviewer
#2 lore raw

PATCH v10 2/10 iommu/amd: Disable AMD IOMMU if CMPXCHG16B feature is not supported

In reply to Suravee Suthikulpanit

According to the AMD IOMMU spec, IOMMU hardware reads the entire DTE in a single 256-bit transaction. It is recommended to update DTE using 128-bit operation followed by an INVALIDATE_DEVTAB_ENTYRY command when the IV=1b or V=1b before the change.

According to the AMD BIOS and Kernel Developer's Guide (BDKG) dated back to family 10h Processor [1], which is the first introduction of AMD IOMMU, AMD processor always has CPUID Fn0000_0001_ECX[CMPXCHG16B]=1. Therefore, it is safe to assume cmpxchg128 is available with all AMD processor w/ IOMMU.

In addition, the CMPXCHG16B feature has already been checked separately before enabling the GA, XT, and GAM modes. Consolidate the detection logic, and fail the IOMMU initialization if the feature is not supported.

[1] https://www.amd.com/content/dam/amd/en/documents/archived-tech-docs/programmer-references/31116.pdf

---
 drivers/iommu/amd/init.c | 23 +++++++++--------------
 1 file changed, 9 insertions(+), 14 deletions(-)
drivers/iommu/amd/init.c +9 -14
index 3a7b2b0472fa..c1607b29ebf4 100644
@@ -1752,13 +1752,8 @@ static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h,
 		else
 			iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
 
-		/*
-		 * Note: GA (128-bit IRTE) mode requires cmpxchg16b supports.
-		 * GAM also requires GA mode. Therefore, we need to
-		 * check cmpxchg16b support before enabling it.
-		 */
-		if (!boot_cpu_has(X86_FEATURE_CX16) ||
-		    ((h->efr_attr & (0x1 << IOMMU_FEAT_GASUP_SHIFT)) == 0))
+		/* GAM requires GA mode. */
+		if ((h->efr_attr & (0x1 << IOMMU_FEAT_GASUP_SHIFT)) == 0)
 			amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
 		break;
 	case 0x11:
@@ -1768,13 +1763,8 @@ static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h,
 		else
 			iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
 
-		/*
-		 * Note: GA (128-bit IRTE) mode requires cmpxchg16b supports.
-		 * XT, GAM also requires GA mode. Therefore, we need to
-		 * check cmpxchg16b support before enabling them.
-		 */
-		if (!boot_cpu_has(X86_FEATURE_CX16) ||
-		    ((h->efr_reg & (0x1 << IOMMU_EFR_GASUP_SHIFT)) == 0)) {
+		/* XT and GAM require GA mode. */
+		if ((h->efr_reg & (0x1 << IOMMU_EFR_GASUP_SHIFT)) == 0) {
 			amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
 			break;
 		}
@@ -3028,6 +3018,11 @@ static int __init early_amd_iommu_init(void)
 		return -EINVAL;
 	}
 
+	if (!boot_cpu_has(X86_FEATURE_CX16)) {
+		pr_err("Failed to initialize. The CMPXCHG16B feature is required.\n");
+		return -EINVAL;
+	}
+
 	/*
 	 * Validate checksum here so we don't need to do it when
 	 * we actually parse the table
Signature · 1 line
2.34.1
maintainer
#3 lore raw

Re: [PATCH v10 02/10] iommu/amd: Disable AMD IOMMU if CMPXCHG16B feature is not supported

Quoted 10 lines · Suravee Suthikulpanit

According to the AMD IOMMU spec, IOMMU hardware reads the entire DTE in a single 256-bit transaction. It is recommended to update DTE using 128-bit operation followed by an INVALIDATE_DEVTAB_ENTYRY command when the IV=1b or V=1b before the change.

According to the AMD BIOS and Kernel Developer's Guide (BDKG) dated back to family 10h Processor [1], which is the first introduction of AMD IOMMU, AMD processor always has CPUID Fn0000_0001_ECX[CMPXCHG16B]=1. Therefore, it is safe to assume cmpxchg128 is available with all AMD processor w/ IOMMU.

Makes sense. More specifically, I'm fairly sure the only x86-64 CPUs without cmpxchg16b are very early NetBurst Xeons, while AMD had the instruction from the start, and dropping the runtime check entirely would work just as well.

Arnd

author reviewer
#4 lore raw

PATCH v10 3/10 compiler_types.h: Introduce 128-bit unqualified scalar type support

In reply to Suravee Suthikulpanit

From: Uros Bizjak <[email protected]>

Currently, __READ_ONCE() cannot support 128-bit data type due to the lack of 128-bit support in __unqual_scalar_typeof() macro.

To ensure ordering for 128-bit data type when atomicity is not required, introduce support for an unqualified scalar type __int128 if supported by the architecture.

---
 include/linux/compiler_types.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)
include/linux/compiler_types.h +13 -0
index 1a957ea2f4fe..394e0e60bb27 100644
@@ -469,6 +469,7 @@ struct ftrace_likely_data {
 		unsigned type:	(unsigned type)0,			\
 		signed type:	(signed type)0
 
+#if defined(CONFIG_ARCH_SUPPORTS_INT128) && defined(__SIZEOF_INT128__)
 #define __unqual_scalar_typeof(x) typeof(				\
 		_Generic((x),						\
 			 char:	(char)0,				\
@@ -477,7 +478,19 @@ struct ftrace_likely_data {
 			 __scalar_type_to_expr_cases(int),		\
 			 __scalar_type_to_expr_cases(long),		\
 			 __scalar_type_to_expr_cases(long long),	\
+			 __scalar_type_to_expr_cases(__int128),		\
 			 default: (x)))
+#else
+#define __unqual_scalar_typeof(x) typeof(				\
+		_Generic((x),						\
+			 char:	(char)0,				\
+			 __scalar_type_to_expr_cases(char),		\
+			 __scalar_type_to_expr_cases(short),		\
+			 __scalar_type_to_expr_cases(int),		\
+			 __scalar_type_to_expr_cases(long),		\
+			 __scalar_type_to_expr_cases(long long),	\
+			 default: (x)))
+#endif
 
 /* Is this type a native word size -- useful for atomic operations */
 #define __native_word(t) \
Signature · 1 line
2.34.1
author reviewer
#5 lore raw

PATCH v10 4/10 iommu/amd: Introduce struct ivhd_dte_flags to store persistent DTE flags

In reply to Suravee Suthikulpanit

During early initialization, the driver parses IVRS IVHD block to get list of downstream devices along with their DTE flags (i.e INITPass, EIntPass, NMIPass, SysMgt, Lint0Pass, Lint1Pass). This information is currently store in the device DTE, and needs to be preserved when clearing and configuring each DTE, which makes it difficult to manage.

Introduce struct ivhd_dte_flags to store IVHD DTE settings for a device or range of devices, which are stored in the amd_ivhd_dev_flags_list during initial IVHD parsing.

---
 drivers/iommu/amd/amd_iommu_types.h |  16 ++++
 drivers/iommu/amd/init.c            | 113 +++++++++++++++++++++-------
 2 files changed, 100 insertions(+), 29 deletions(-)
drivers/iommu/amd/amd_iommu_types.h +16 -0
index af87b1d094c1..ae5f1e031722 100644
@@ -220,6 +220,8 @@
 #define DEV_ENTRY_EX            0x67
 #define DEV_ENTRY_SYSMGT1       0x68
 #define DEV_ENTRY_SYSMGT2       0x69
+#define DTE_DATA1_SYSMGT_MASK	GENMASK_ULL(41, 40)
+
 #define DEV_ENTRY_IRQ_TBL_EN	0x80
 #define DEV_ENTRY_INIT_PASS     0xb8
 #define DEV_ENTRY_EINT_PASS     0xb9
@@ -516,6 +518,9 @@ extern struct kmem_cache *amd_iommu_irq_cache;
 #define for_each_pdom_dev_data_safe(pdom_dev_data, next, pdom) \
 	list_for_each_entry_safe((pdom_dev_data), (next), &pdom->dev_data_list, list)
 
+#define for_each_ivhd_dte_flags(entry) \
+	list_for_each_entry((entry), &amd_ivhd_dev_flags_list, list)
+
 struct amd_iommu;
 struct iommu_domain;
 struct irq_domain;
@@ -884,6 +889,17 @@ struct dev_table_entry {
 	u64 data[4];
 };
 
+/*
+ * Structure to sture persistent DTE flags from IVHD
+ */
+struct ivhd_dte_flags {
+	struct list_head list;
+	u16 segid;
+	u16 devid_first;
+	u16 devid_last;
+	struct dev_table_entry dte;
+};
+
 /*
  * One entry for unity mappings parsed out of the ACPI table.
  */
drivers/iommu/amd/init.c +84 -29

154 lines not on this page · raw message

Signature · 1 line
2.34.1
author reviewer
#6 lore raw

PATCH v10 5/10 iommu/amd: Introduce helper function to update 256-bit DTE

In reply to Suravee Suthikulpanit

The current implementation does not follow 128-bit write requirement to update DTE as specified in the AMD I/O Virtualization Techonology (IOMMU) Specification.

Therefore, modify the struct dev_table_entry to contain union of u128 data array, and introduce a helper functions update_dte256() to update DTE using two 128-bit cmpxchg operations to update 256-bit DTE with the modified structure, and take into account the DTE[V, GV] bits when programming the DTE to ensure proper order of DTE programming and flushing.

In addition, introduce a per-DTE spin_lock struct dev_data.dte_lock to provide synchronization when updating the DTE to prevent cmpxchg128 failure.

---
 drivers/iommu/amd/amd_iommu_types.h |  10 ++-
 drivers/iommu/amd/iommu.c           | 116 ++++++++++++++++++++++++++++
 2 files changed, 125 insertions(+), 1 deletion(-)
drivers/iommu/amd/amd_iommu_types.h +9 -1
index ae5f1e031722..ea7922b06325 100644
@@ -427,9 +427,13 @@
 #define DTE_GCR3_SHIFT_C	43
 
 #define DTE_GPT_LEVEL_SHIFT	54
+#define DTE_GPT_LEVEL_MASK	GENMASK_ULL(55, 54)
 
 #define GCR3_VALID		0x01ULL
 
+/* DTE[128:179] | DTE[184:191] */
+#define DTE_DATA2_INTR_MASK	~GENMASK_ULL(55, 52)
+
 #define IOMMU_PAGE_MASK (((1ULL << 52) - 1) & ~0xfffULL)
 #define IOMMU_PTE_PRESENT(pte) ((pte) & IOMMU_PTE_PR)
 #define IOMMU_PTE_DIRTY(pte) ((pte) & IOMMU_PTE_HD)
@@ -842,6 +846,7 @@ struct devid_map {
 struct iommu_dev_data {
 	/*Protect against attach/detach races */
 	struct mutex mutex;
+	spinlock_t dte_lock;              /* DTE lock for 256-bit access */
 
 	struct list_head list;		  /* For domain->dev_list */
 	struct llist_node dev_data_list;  /* For global dev_data_list */
@@ -886,7 +891,10 @@ extern struct list_head amd_iommu_list;
  * Structure defining one entry in the device table
  */
 struct dev_table_entry {
-	u64 data[4];
+	union {
+		u64 data[4];
+		u128 data128[2];
+	};
 };
 
 /*
drivers/iommu/amd/iommu.c +116 -0

140 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#7 lore raw

Re: [PATCH v10 05/10] iommu/amd: Introduce helper function to update 256-bit DTE

drivers/iommu/amd/iommu.c static int amd_iommu_attach_device(struct iommu_domain *dom, · line 98

Quoted 7 lines · Suravee Suthikulpanit

+static void write_dte_upper128(struct dev_table_entry *ptr, struct dev_table_entry *new)

drivers/iommu/amd/iommu.c +4 -0
+{
+	struct dev_table_entry old = {};
+
+	old.data128[1] = __READ_ONCE(ptr->data128[1]);

The __READ_ONCE() in place of READ_ONCE() does make this a lot simpler. After seeing how it is used though, I wonder if this should just be an open-coded volatile pointer access to avoid complicating __unqual_scalar_typeof() further.

Quoted 11 lines
drivers/iommu/amd/iommu.c +11 -0
+	do {
+		/*
+		 * Preserve DTE_DATA2_INTR_MASK. This needs to be
+		 * done here since it requires to be inside
+		 * spin_lock(&dev_data->dte_lock) context.
+		 */
+		new->data[2] &= ~DTE_DATA2_INTR_MASK;
+		new->data[2] |= old.data[2] & DTE_DATA2_INTR_MASK;
+
+	/* Note: try_cmpxchg inherently update &old.data128[1] on failure */
+	} while (!try_cmpxchg128(&ptr->data128[1], &old.data128[1], 
Quoted 1 line

new->data128[1]));

Since this is always done under the lock, is there ever a chance that the try_cmpxchg128() fails? I see that the existing code doesn't have the loop, which makes sense if this is just meant to be an atomic store.

Arnd

#8 lore raw

Re: [PATCH v10 05/10] iommu/amd: Introduce helper function to update 256-bit DTE

Quoted 13 lines · Arnd Bergmann

drivers/iommu/amd/iommu.c static int amd_iommu_attach_device(struct iommu_domain *dom, · line 98

Quoted 7 lines · Suravee Suthikulpanit

+static void write_dte_upper128(struct dev_table_entry *ptr, struct dev_table_entry *new)

drivers/iommu/amd/iommu.c +4 -0
+{
+	struct dev_table_entry old = {};
+
+	old.data128[1] = __READ_ONCE(ptr->data128[1]);

The __READ_ONCE() in place of READ_ONCE() does make this a lot simpler. After seeing how it is used though, I wonder if this should just be an open-coded volatile pointer access to avoid complicating __unqual_scalar_typeof() further.

I've been skeptical we even need the READ_ONCE. This is all under a lock, what is READ_ONCE even protecting against? It is safe to double read.

Quoted 7 lines
Quoted 7 lines
drivers/iommu/amd/iommu.c +7 -0
+	do {
+		/*
+		 * Preserve DTE_DATA2_INTR_MASK. This needs to be
+		 * done here since it requires to be inside
+		 * spin_lock(&dev_data->dte_lock) context.
+		 */
+		new->data[2] &= ~DTE_DATA2_INTR_MASK;
Quoted 8 lines
Quoted 5 lines
drivers/iommu/amd/iommu.c +4 -0
+		new->data[2] |= old.data[2] & DTE_DATA2_INTR_MASK;
+
+	/* Note: try_cmpxchg inherently update &old.data128[1] on failure */
+	} while (!try_cmpxchg128(&ptr->data128[1], &old.data128[1], 

new->data128[1]));

Since this is always done under the lock, is there ever a chance that the try_cmpxchg128() fails?

No, but if something goes wrong and it does fail it still has to progress.

Quoted 2 lines

I see that the existing code doesn't have the loop, which makes sense if this is just meant to be an atomic store.

I think AMD architecture imagined this would be done with a SSE 256 bit store operation. cmpxchg128 is sort of a hacky stand in since we don't have that available.

A more understandable version of all of this might be to have a store128 wrapper function that invokes cmpxchg internally and guarantees the new value is stored regardless.

How hard would it be to invoke the SSE 256 bit store instruction from the kernel? And do all AMD CPUs with IOMMU have it?

Jason

#9 lore raw

Re: [PATCH v10 05/10] iommu/amd: Introduce helper function to update 256-bit DTE

Quoted 16 lines · Jason Gunthorpe
Quoted 13 lines · Arnd Bergmann

drivers/iommu/amd/iommu.c static int amd_iommu_attach_device(struct iommu_domain *dom, · line 98

Quoted 7 lines · Suravee Suthikulpanit

+static void write_dte_upper128(struct dev_table_entry *ptr, struct dev_table_entry *new)

drivers/iommu/amd/iommu.c +4 -0
+{
+   struct dev_table_entry old = {};
+
+   old.data128[1] = __READ_ONCE(ptr->data128[1]);

The __READ_ONCE() in place of READ_ONCE() does make this a lot simpler. After seeing how it is used though, I wonder if this should just be an open-coded volatile pointer access to avoid complicating __unqual_scalar_typeof() further.

Quoted 3 lines

I've been skeptical we even need the READ_ONCE. This is all under a lock, what is READ_ONCE even protecting against? It is safe to double read.

Even without atomicity guarantee, __READ_ONCE() still prevents the compiler from performing unwanted optimizations (please see the first comment in include/asm-generic/rwonce.h) and unwanted reordering of reads and writes when this function is inlined. This macro does cast the read to volatile, but IMO it is much more readable to use __READ_ONCE() than volatile qualifier.

Uros.

#10 lore raw

Re: [PATCH v10 05/10] iommu/amd: Introduce helper function to update 256-bit DTE

Quoted 21 lines · Uros Bizjak
Quoted 16 lines · Jason Gunthorpe
Quoted 13 lines · Arnd Bergmann

drivers/iommu/amd/iommu.c static int amd_iommu_attach_device(struct iommu_domain *dom, · line 98

Quoted 7 lines · Suravee Suthikulpanit

+static void write_dte_upper128(struct dev_table_entry *ptr, struct dev_table_entry *new)

drivers/iommu/amd/iommu.c +4 -0
+{
+   struct dev_table_entry old = {};
+
+   old.data128[1] = __READ_ONCE(ptr->data128[1]);

The __READ_ONCE() in place of READ_ONCE() does make this a lot simpler. After seeing how it is used though, I wonder if this should just be an open-coded volatile pointer access to avoid complicating __unqual_scalar_typeof() further.

Quoted 3 lines

I've been skeptical we even need the READ_ONCE. This is all under a lock, what is READ_ONCE even protecting against? It is safe to double read.

Quoted 6 lines

Even without atomicity guarantee, __READ_ONCE() still prevents the compiler from performing unwanted optimizations (please see the first comment in include/asm-generic/rwonce.h) and unwanted reordering of reads and writes when this function is inlined. This macro does cast the read to volatile, but IMO it is much more readable to use __READ_ONCE() than volatile qualifier.

Yes it does, but please explain to me what "unwanted reordering" is allowed here?

Again, this is all under a lock so ptr->data128 is fully stable and not changing.

Jason

#11 lore raw

Re: [PATCH v10 05/10] iommu/amd: Introduce helper function to update 256-bit DTE

Quoted 30 lines · Jason Gunthorpe
Quoted 21 lines · Uros Bizjak
Quoted 16 lines · Jason Gunthorpe
Quoted 13 lines · Arnd Bergmann

drivers/iommu/amd/iommu.c static int amd_iommu_attach_device(struct iommu_domain *dom, · line 98

Quoted 7 lines · Suravee Suthikulpanit

+static void write_dte_upper128(struct dev_table_entry *ptr, struct dev_table_entry *new)

drivers/iommu/amd/iommu.c +4 -0
+{
+   struct dev_table_entry old = {};
+
+   old.data128[1] = __READ_ONCE(ptr->data128[1]);

The __READ_ONCE() in place of READ_ONCE() does make this a lot simpler. After seeing how it is used though, I wonder if this should just be an open-coded volatile pointer access to avoid complicating __unqual_scalar_typeof() further.

Quoted 3 lines

I've been skeptical we even need the READ_ONCE. This is all under a lock, what is READ_ONCE even protecting against? It is safe to double read.

Quoted 6 lines

Even without atomicity guarantee, __READ_ONCE() still prevents the compiler from performing unwanted optimizations (please see the first comment in include/asm-generic/rwonce.h) and unwanted reordering of reads and writes when this function is inlined. This macro does cast the read to volatile, but IMO it is much more readable to use __READ_ONCE() than volatile qualifier.

Quoted 2 lines

Yes it does, but please explain to me what "unwanted reordering" is allowed here?

It is a static function that will be inlined by the compiler somewhere, so "unwanted reordering" depends on where it will be inlined. *IF* it will be called from safe code, then this limitation for the compiler can be lifted.

Uros.

#12 lore raw

Re: [PATCH v10 05/10] iommu/amd: Introduce helper function to update 256-bit DTE

Quoted 7 lines · Uros Bizjak
Quoted 7 lines
Quoted 6 lines

Even without atomicity guarantee, __READ_ONCE() still prevents the compiler from performing unwanted optimizations (please see the first comment in include/asm-generic/rwonce.h) and unwanted reordering of reads and writes when this function is inlined. This macro does cast the read to volatile, but IMO it is much more readable to use __READ_ONCE() than volatile qualifier.

Quoted 7 lines
Quoted 2 lines

Yes it does, but please explain to me what "unwanted reordering" is allowed here?

It is a static function that will be inlined by the compiler somewhere, so "unwanted reordering" depends on where it will be inlined. *IF* it will be called from safe code, then this limitation for the compiler can be lifted.

As long as the values are read within the spinlock the order does not matter. READ_ONCE() is not required to contain reads within spinlocks.

Jason

#13 lore raw

Re: [PATCH v10 05/10] iommu/amd: Introduce helper function to update 256-bit DTE

Quoted 12 lines · Jason Gunthorpe
Quoted 10 lines · Uros Bizjak
Quoted 9 lines
Quoted 6 lines

Even without atomicity guarantee, __READ_ONCE() still prevents the compiler from performing unwanted optimizations (please see the first comment in include/asm-generic/rwonce.h) and unwanted reordering of reads and writes when this function is inlined. This macro does cast the read to volatile, but IMO it is much more readable to use __READ_ONCE() than volatile qualifier.

Yes it does, but please explain to me what "unwanted reordering" is allowed here?

Quoted 7 lines
Quoted 4 lines

It is a static function that will be inlined by the compiler somewhere, so "unwanted reordering" depends on where it will be inlined. *IF* it will be called from safe code, then this limitation for the compiler can be lifted.

As long as the values are read within the spinlock the order does not matter. READ_ONCE() is not required to contain reads within spinlocks.

Indeed. But then why complicate things with cmpxchg, when we have exclusive access to the shared memory? No other thread can access the data, protected by spinlock; it won't change between invocations of cmpxchg in the loop, and atomic access via cmpxchg is not needed.

Uros.

#14 lore raw

Re: [PATCH v10 05/10] iommu/amd: Introduce helper function to update 256-bit DTE

Quoted 18 lines · Uros Bizjak
Quoted 17 lines · Jason Gunthorpe
Quoted 14 lines · Uros Bizjak
Quoted 9 lines
Quoted 6 lines

Even without atomicity guarantee, __READ_ONCE() still prevents the compiler from performing unwanted optimizations (please see the first comment in include/asm-generic/rwonce.h) and unwanted reordering of reads and writes when this function is inlined. This macro does cast the read to volatile, but IMO it is much more readable to use __READ_ONCE() than volatile qualifier.

Yes it does, but please explain to me what "unwanted reordering" is allowed here?

It is a static function that will be inlined by the compiler somewhere, so "unwanted reordering" depends on where it will be inlined. *IF* it will be called from safe code, then this limitation for the compiler can be lifted.

Quoted 7 lines
Quoted 2 lines

As long as the values are read within the spinlock the order does not matter. READ_ONCE() is not required to contain reads within spinlocks.

Indeed. But then why complicate things with cmpxchg, when we have exclusive access to the shared memory? No other thread can access the data, protected by spinlock; it won't change between invocations of cmpxchg in the loop, and atomic access via cmpxchg is not needed.

This is writing to memory shared by HW and HW is doing a 256 bit atomic load.

It is important that the CPU do a 128 bit atomic write.

cmpxchg is not required, but a 128 bit store is. cmpxchg128 is the only primitive Linux offers.

Jason

#15 lore raw

Re: [PATCH v10 05/10] iommu/amd: Introduce helper function to update 256-bit DTE

Quoted 28 lines · Jason Gunthorpe
Quoted 18 lines · Uros Bizjak
Quoted 17 lines · Jason Gunthorpe
Quoted 14 lines · Uros Bizjak
Quoted 9 lines
Quoted 6 lines

Even without atomicity guarantee, __READ_ONCE() still prevents the compiler from performing unwanted optimizations (please see the first comment in include/asm-generic/rwonce.h) and unwanted reordering of reads and writes when this function is inlined. This macro does cast the read to volatile, but IMO it is much more readable to use __READ_ONCE() than volatile qualifier.

Yes it does, but please explain to me what "unwanted reordering" is allowed here?

It is a static function that will be inlined by the compiler somewhere, so "unwanted reordering" depends on where it will be inlined. *IF* it will be called from safe code, then this limitation for the compiler can be lifted.

Quoted 7 lines
Quoted 2 lines

As long as the values are read within the spinlock the order does not matter. READ_ONCE() is not required to contain reads within spinlocks.

Indeed. But then why complicate things with cmpxchg, when we have exclusive access to the shared memory? No other thread can access the data, protected by spinlock; it won't change between invocations of cmpxchg in the loop, and atomic access via cmpxchg is not needed.

Quoted 7 lines

This is writing to memory shared by HW and HW is doing a 256 bit atomic load.

It is important that the CPU do a 128 bit atomic write.

cmpxchg is not required, but a 128 bit store is. cmpxchg128 is the only primitive Linux offers.

If we want to exercise only the atomic property of cmpxchg16b, then we can look at arch/x86/lib/atomic64_set_cx8.S how cmpxchg8b is used to implement the core of arch_atomic64_set() for x86_32:

SYM_FUNC_START(atomic64_set_cx8)
1:
/* we don't need LOCK_PREFIX since aligned 64-bit writes
* are atomic on 586 and newer */
cmpxchg8b (%esi)
jne 1b

RET
SYM_FUNC_END(atomic64_set_cx8)

we *do* have arch_try_cmpxchg128_local() that declares cmpxchg16b without lock prefix, and perhaps we can use it to create 128bit store, something like:

static __always_inline void iommu_atomic128_set(__int128 *ptr, __int128 val)

{
    __int128 old = *ptr;

    do {
    } while (!arch_try_cmpxchg128_local(ptr, &old, val));
}

Then write_dte_upper128() would look like:

static void write_dte_upper128(struct dev_table_entry *ptr, struct dev_table_entry *new)

{
    struct dev_table_entry old = {}; <--- do we need to initialize struct here?

    old.data128[1] = ptr->data128[1];

    /*
     * Preserve DTE_DATA2_INTR_MASK. This needs to be
     * done here since it requires to be inside
     * spin_lock(&dev_data->dte_lock) context.
     */
    new->data[2] &= ~DTE_DATA2_INTR_MASK;
    new->data[2] |= old.data[2] & DTE_DATA2_INTR_MASK;

    iommu_atomic128_set(&ptr->data128[1], new->data128[1]);
}

and in a similar way implement write_dte_lower128().

(I am away from the keyboard ATM, so the above is not tested, but you got the idea...)

Uros.

#16 lore raw

Re: [PATCH v10 05/10] iommu/amd: Introduce helper function to update 256-bit DTE

Quoted 56 lines · Uros Bizjak
Quoted 28 lines · Jason Gunthorpe
Quoted 18 lines · Uros Bizjak
Quoted 17 lines · Jason Gunthorpe
Quoted 14 lines · Uros Bizjak
Quoted 9 lines
Quoted 6 lines

Even without atomicity guarantee, __READ_ONCE() still prevents the compiler from performing unwanted optimizations (please see the first comment in include/asm-generic/rwonce.h) and unwanted reordering of reads and writes when this function is inlined. This macro does cast the read to volatile, but IMO it is much more readable to use __READ_ONCE() than volatile qualifier.

Yes it does, but please explain to me what "unwanted reordering" is allowed here?

It is a static function that will be inlined by the compiler somewhere, so "unwanted reordering" depends on where it will be inlined. *IF* it will be called from safe code, then this limitation for the compiler can be lifted.

Quoted 7 lines
Quoted 2 lines

As long as the values are read within the spinlock the order does not matter. READ_ONCE() is not required to contain reads within spinlocks.

Indeed. But then why complicate things with cmpxchg, when we have exclusive access to the shared memory? No other thread can access the data, protected by spinlock; it won't change between invocations of cmpxchg in the loop, and atomic access via cmpxchg is not needed.

Quoted 7 lines

This is writing to memory shared by HW and HW is doing a 256 bit atomic load.

It is important that the CPU do a 128 bit atomic write.

cmpxchg is not required, but a 128 bit store is. cmpxchg128 is the only primitive Linux offers.

If we want to exercise only the atomic property of cmpxchg16b, then we can look at arch/x86/lib/atomic64_set_cx8.S how cmpxchg8b is used to implement the core of arch_atomic64_set() for x86_32:

SYM_FUNC_START(atomic64_set_cx8)
1:
/* we don't need LOCK_PREFIX since aligned 64-bit writes
* are atomic on 586 and newer */
cmpxchg8b (%esi)
jne 1b

RET
SYM_FUNC_END(atomic64_set_cx8)

we *do* have arch_try_cmpxchg128_local() that declares cmpxchg16b without lock prefix, and perhaps we can use it to create 128bit store, something like:

Quoted 7 lines

static __always_inline void iommu_atomic128_set(__int128 *ptr, __int128 val)

{
    __int128 old = *ptr;

    do {
    } while (!arch_try_cmpxchg128_local(ptr, &old, val));
}

Eh, I forgot that data does not change, so we don't need the cmpxchg loop.

static __always_inline void iommu_atomic128_set(__int128 *ptr, __int128 val)

 {
     arch_cmpxchg128_local(ptr, *ptr, val);
}

should do the trick.

Uros.

#17 lore raw

Re: [PATCH v10 05/10] iommu/amd: Introduce helper function to update 256-bit DTE

In reply to Uros Bizjak
Quoted 20 lines · Uros Bizjak

Then write_dte_upper128() would look like:

static void write_dte_upper128(struct dev_table_entry *ptr, struct dev_table_entry *new)

{
    struct dev_table_entry old = {}; <--- do we need to initialize struct here?

    old.data128[1] = ptr->data128[1];

    /*
     * Preserve DTE_DATA2_INTR_MASK. This needs to be
     * done here since it requires to be inside
     * spin_lock(&dev_data->dte_lock) context.
     */
    new->data[2] &= ~DTE_DATA2_INTR_MASK;
    new->data[2] |= old.data[2] & DTE_DATA2_INTR_MASK;

    iommu_atomic128_set(&ptr->data128[1], new->data128[1]);
}
Quoted 1 line

and in a similar way implement write_dte_lower128().

That makes sense to me, but also we have drifted really far from the purpose of this series and missed this merge window because of this :\

Let's conclude something please

Jason

#18 lore raw

Re: [PATCH v10 05/10] iommu/amd: Introduce helper function to update 256-bit DTE

Quoted 23 lines · Jason Gunthorpe
Quoted 20 lines · Uros Bizjak

Then write_dte_upper128() would look like:

static void write_dte_upper128(struct dev_table_entry *ptr, struct dev_table_entry *new)

{
    struct dev_table_entry old = {}; <--- do we need to initialize struct here?

    old.data128[1] = ptr->data128[1];

    /*
     * Preserve DTE_DATA2_INTR_MASK. This needs to be
     * done here since it requires to be inside
     * spin_lock(&dev_data->dte_lock) context.
     */
    new->data[2] &= ~DTE_DATA2_INTR_MASK;
    new->data[2] |= old.data[2] & DTE_DATA2_INTR_MASK;

    iommu_atomic128_set(&ptr->data128[1], new->data128[1]);
}
Quoted 6 lines
Quoted 1 line

and in a similar way implement write_dte_lower128().

That makes sense to me, but also we have drifted really far from the purpose of this series and missed this merge window because of this :\

Let's conclude something please

The most important fact here is that data won't change (I was not aware of that), and that we would like to exercise only the 128bit atomic write property of the cmpxchg16b. So, we actually don't need a "lockless" approach, where we would need __READ_ONCE() and cmpxchg loop. The atomic write can be a simple arch_cmpxchg128(ptr, *ptr, new), and because *ptr won't change between preload and compare, the xchg will always succeed.

Uros.

author reviewer
#19 lore raw

PATCH v10 6/10 iommu/amd: Modify set_dte_entry() to use 256-bit DTE helpers

In reply to Suravee Suthikulpanit

Also, the set_dte_entry() is used to program several DTE fields (e.g. stage1 table, stage2 table, domain id, and etc.), which is difficult to keep track with current implementation.

Therefore, separate logic for clearing DTE (i.e. make_clear_dte) and another function for setting up the GCR3 Table Root Pointer, GIOV, GV, GLX, and GuestPagingMode into another function set_dte_gcr3_table().

---
 drivers/iommu/amd/amd_iommu.h       |   2 +
 drivers/iommu/amd/amd_iommu_types.h |  13 +--
 drivers/iommu/amd/init.c            |  30 ++++++-
 drivers/iommu/amd/iommu.c           | 129 ++++++++++++++++------------
 4 files changed, 106 insertions(+), 68 deletions(-)
drivers/iommu/amd/amd_iommu.h +2 -0
index 38509e1019e9..f9260bb8fc85 100644
@@ -183,3 +183,5 @@ void amd_iommu_domain_set_pgtable(struct protection_domain *domain,
 struct dev_table_entry *get_dev_table(struct amd_iommu *iommu);
 
 #endif
+
+struct dev_table_entry *amd_iommu_get_ivhd_dte_flags(u16 segid, u16 devid);
drivers/iommu/amd/amd_iommu_types.h +4 -9
index ea7922b06325..0bbda60d3cdc 100644
@@ -409,8 +409,7 @@
 #define DTE_FLAG_HAD	(3ULL << 7)
 #define DTE_FLAG_GIOV	BIT_ULL(54)
 #define DTE_FLAG_GV	BIT_ULL(55)
-#define DTE_GLX_SHIFT	(56)
-#define DTE_GLX_MASK	(3)
+#define DTE_GLX		GENMASK_ULL(57, 56)
 #define DTE_FLAG_IR	BIT_ULL(61)
 #define DTE_FLAG_IW	BIT_ULL(62)
 
@@ -418,13 +417,9 @@
 #define DTE_FLAG_MASK	(0x3ffULL << 32)
 #define DEV_DOMID_MASK	0xffffULL
 
-#define DTE_GCR3_VAL_A(x)	(((x) >> 12) & 0x00007ULL)
-#define DTE_GCR3_VAL_B(x)	(((x) >> 15) & 0x0ffffULL)
-#define DTE_GCR3_VAL_C(x)	(((x) >> 31) & 0x1fffffULL)
-
-#define DTE_GCR3_SHIFT_A	58
-#define DTE_GCR3_SHIFT_B	16
-#define DTE_GCR3_SHIFT_C	43
+#define DTE_GCR3_14_12	GENMASK_ULL(60, 58)
+#define DTE_GCR3_30_15	GENMASK_ULL(31, 16)
+#define DTE_GCR3_51_31	GENMASK_ULL(63, 43)
 
 #define DTE_GPT_LEVEL_SHIFT	54
 #define DTE_GPT_LEVEL_MASK	GENMASK_ULL(55, 54)
drivers/iommu/amd/init.c +26 -4
index 015c9b045685..1e4b8040c374 100644
@@ -1089,11 +1089,9 @@ static bool __copy_device_table(struct amd_iommu *iommu)
 			}
 			/* If gcr3 table existed, mask it out */
 			if (old_devtb[devid].data[0] & DTE_FLAG_GV) {
-				tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
-				tmp |= DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
+				tmp = (DTE_GCR3_30_15 | DTE_GCR3_51_31);
 				pci_seg->old_dev_tbl_cpy[devid].data[1] &= ~tmp;
-				tmp = DTE_GCR3_VAL_A(~0ULL) << DTE_GCR3_SHIFT_A;
-				tmp |= DTE_FLAG_GV;
+				tmp = (DTE_GCR3_14_12 | DTE_FLAG_GV);
 				pci_seg->old_dev_tbl_cpy[devid].data[0] &= ~tmp;
 			}
 		}
@@ -1144,6 +1142,30 @@ static bool copy_device_table(void)
 	return true;
 }
 
+struct dev_table_entry *amd_iommu_get_ivhd_dte_flags(u16 segid, u16 devid)
+{
+	struct ivhd_dte_flags *e;
+	unsigned int best_len = UINT_MAX;
+	struct dev_table_entry *dte = NULL;
+
+	for_each_ivhd_dte_flags(e) {
+		/*
+		 * Need to go through the whole list to find the smallest range,
+		 * which contains the devid.
+		 */
+		if ((e->segid == segid) &&
+		    (e->devid_first <= devid) && (devid <= e->devid_last)) {
+			unsigned int len = e->devid_last - e->devid_first;
+
+			if (len < best_len) {
+				dte = &(e->dte);
+				best_len = len;
+			}
+		}
+	}
+	return dte;
+}
+
 static bool search_ivhd_dte_flags(u16 segid, u16 first, u16 last)
 {
 	struct ivhd_dte_flags *e;
drivers/iommu/amd/iommu.c +74 -55

164 lines not on this page · raw message

Signature · 1 line
2.34.1
author reviewer
#20 lore raw

PATCH v10 7/10 iommu/amd: Introduce helper function get_dte256()

In reply to Suravee Suthikulpanit

And use it in clone_alias() along with update_dte256(). Also use get_dte256() in dump_dte_entry().

---
 drivers/iommu/amd/iommu.c | 61 ++++++++++++++++++++++++++++++++-------
 1 file changed, 50 insertions(+), 11 deletions(-)
drivers/iommu/amd/iommu.c +50 -11
index f7e36cbb6c4d..2a0411e8ec20 100644
@@ -85,6 +85,8 @@ static void set_dte_entry(struct amd_iommu *iommu,
 
 static void iommu_flush_dte_sync(struct amd_iommu *iommu, u16 devid);
 
+static struct iommu_dev_data *find_dev_data(struct amd_iommu *iommu, u16 devid);
+
 /****************************************************************************
  *
  * Helper functions
@@ -195,6 +197,20 @@ static void update_dte256(struct amd_iommu *iommu, struct iommu_dev_data *dev_da
 	spin_unlock(&dev_data->dte_lock);
 }
 
+static void get_dte256(struct amd_iommu *iommu, struct iommu_dev_data *dev_data,
+		      struct dev_table_entry *dte)
+{
+	struct dev_table_entry *ptr;
+	struct dev_table_entry *dev_table = get_dev_table(iommu);
+
+	ptr = &dev_table[dev_data->devid];
+
+	spin_lock(&dev_data->dte_lock);
+	dte->data128[0] = __READ_ONCE(ptr->data128[0]);
+	dte->data128[1] = __READ_ONCE(ptr->data128[1]);
+	spin_unlock(&dev_data->dte_lock);
+}
+
 static inline bool pdom_is_v2_pgtbl_mode(struct protection_domain *pdom)
 {
 	return (pdom && (pdom->pd_mode == PD_MODE_V2));
@@ -343,9 +359,11 @@ static struct iommu_dev_data *search_dev_data(struct amd_iommu *iommu, u16 devid
 
 static int clone_alias(struct pci_dev *pdev, u16 alias, void *data)
 {
+	struct dev_table_entry new;
 	struct amd_iommu *iommu;
-	struct dev_table_entry *dev_table;
+	struct iommu_dev_data *dev_data, *alias_data;
 	u16 devid = pci_dev_id(pdev);
+	int ret = 0;
 
 	if (devid == alias)
 		return 0;
@@ -354,13 +372,27 @@ static int clone_alias(struct pci_dev *pdev, u16 alias, void *data)
 	if (!iommu)
 		return 0;
 
-	amd_iommu_set_rlookup_table(iommu, alias);
-	dev_table = get_dev_table(iommu);
-	memcpy(dev_table[alias].data,
-	       dev_table[devid].data,
-	       sizeof(dev_table[alias].data));
+	/* Copy the data from pdev */
+	dev_data = dev_iommu_priv_get(&pdev->dev);
+	if (!dev_data) {
+		pr_err("%s : Failed to get dev_data for 0x%x\n", __func__, devid);
+		ret = -EINVAL;
+		goto out;
+	}
+	get_dte256(iommu, dev_data, &new);
 
-	return 0;
+	/* Setup alias */
+	alias_data = find_dev_data(iommu, alias);
+	if (!alias_data) {
+		pr_err("%s : Failed to get alias dev_data for 0x%x\n", __func__, alias);
+		ret = -EINVAL;
+		goto out;
+	}
+	update_dte256(iommu, alias_data, &new);
+
+	amd_iommu_set_rlookup_table(iommu, alias);
+out:
+	return ret;
 }
 
 static void clone_aliases(struct amd_iommu *iommu, struct device *dev)
@@ -633,6 +665,12 @@ static int iommu_init_device(struct amd_iommu *iommu, struct device *dev)
 		return -ENOMEM;
 
 	dev_data->dev = dev;
+
+	/*
+	 * The dev_iommu_priv_set() needes to be called before setup_aliases.
+	 * Otherwise, subsequent call to dev_iommu_priv_get() will fail.
+	 */
+	dev_iommu_priv_set(dev, dev_data);
 	setup_aliases(iommu, dev);
 
 	/*
@@ -646,8 +684,6 @@ static int iommu_init_device(struct amd_iommu *iommu, struct device *dev)
 		dev_data->flags = pdev_get_caps(to_pci_dev(dev));
 	}
 
-	dev_iommu_priv_set(dev, dev_data);
-
 	return 0;
 }
 
@@ -678,10 +714,13 @@ static void iommu_ignore_device(struct amd_iommu *iommu, struct device *dev)
 static void dump_dte_entry(struct amd_iommu *iommu, u16 devid)
 {
 	int i;
-	struct dev_table_entry *dev_table = get_dev_table(iommu);
+	struct dev_table_entry dte;
+	struct iommu_dev_data *dev_data = find_dev_data(iommu, devid);
+
+	get_dte256(iommu, dev_data, &dte);
 
 	for (i = 0; i < 4; ++i)
-		pr_err("DTE[%d]: %016llx\n", i, dev_table[devid].data[i]);
+		pr_err("DTE[%d]: %016llx\n", i, dte.data[i]);
 }
 
 static void dump_command(unsigned long phys_addr)
Signature · 1 line
2.34.1
author reviewer
#21 lore raw

PATCH v10 8/10 iommu/amd: Modify clear_dte_entry() to avoid in-place update

In reply to Suravee Suthikulpanit

By reusing the make_clear_dte() and update_dte256().

Also, there is no need to set TV bit for non-SNP system when clearing DTE for blocked domain, and no longer need to apply erratum 63 in clear_dte() since it is already stored in struct ivhd_dte_flags and apply in set_dte_entry().

---
 drivers/iommu/amd/iommu.c | 21 +++++++++------------
 1 file changed, 9 insertions(+), 12 deletions(-)
drivers/iommu/amd/iommu.c +9 -12
index 2a0411e8ec20..d54579206a04 100644
@@ -2090,19 +2090,16 @@ static void set_dte_entry(struct amd_iommu *iommu,
 	}
 }
 
-static void clear_dte_entry(struct amd_iommu *iommu, u16 devid)
+/*
+ * Clear DMA-remap related flags to block all DMA (blockeded domain)
+ */
+static void clear_dte_entry(struct amd_iommu *iommu, struct iommu_dev_data *dev_data)
 {
-	struct dev_table_entry *dev_table = get_dev_table(iommu);
-
-	/* remove entry from the device table seen by the hardware */
-	dev_table[devid].data[0]  = DTE_FLAG_V;
-
-	if (!amd_iommu_snp_en)
-		dev_table[devid].data[0] |= DTE_FLAG_TV;
-
-	dev_table[devid].data[1] &= DTE_FLAG_MASK;
+	struct dev_table_entry new = {};
+	struct dev_table_entry *dte = &get_dev_table(iommu)[dev_data->devid];
 
-	amd_iommu_apply_erratum_63(iommu, devid);
+	make_clear_dte(dev_data, dte, &new);
+	update_dte256(iommu, dev_data, &new);
 }
 
 /* Update and flush DTE for the given device */
@@ -2113,7 +2110,7 @@ static void dev_update_dte(struct iommu_dev_data *dev_data, bool set)
 	if (set)
 		set_dte_entry(iommu, dev_data);
 	else
-		clear_dte_entry(iommu, dev_data->devid);
+		clear_dte_entry(iommu, dev_data);
 
 	clone_aliases(iommu, dev_data->dev);
 	device_flush_dte(dev_data);
Signature · 1 line
2.34.1
author reviewer
#22 lore raw

PATCH v10 9/10 iommu/amd: Lock DTE before updating the entry with WRITE_ONCE()

In reply to Suravee Suthikulpanit

When updating only within a 64-bit tuple of a DTE, just lock the DTE and use WRITE_ONCE() because it is writing to memory read back by HW.

---
 drivers/iommu/amd/amd_iommu.h |  1 +
 drivers/iommu/amd/iommu.c     | 43 +++++++++++++++++++----------------
 2 files changed, 25 insertions(+), 19 deletions(-)
drivers/iommu/amd/amd_iommu.h +1 -0
index f9260bb8fc85..7b43894f6b90 100644
@@ -185,3 +185,4 @@ struct dev_table_entry *get_dev_table(struct amd_iommu *iommu);
 #endif
 
 struct dev_table_entry *amd_iommu_get_ivhd_dte_flags(u16 segid, u16 devid);
+struct iommu_dev_data *search_dev_data(struct amd_iommu *iommu, u16 devid);
drivers/iommu/amd/iommu.c +24 -19

76 lines not on this page · raw message

Signature · 1 line
2.34.1
author reviewer
#23 lore raw

PATCH v10 10/10 iommu/amd: Remove amd_iommu_apply_erratum_63()

In reply to Suravee Suthikulpanit

Also replace __set_dev_entry_bit() with set_dte_bit() and remove unused helper functions.

---
 drivers/iommu/amd/amd_iommu.h |  1 -
 drivers/iommu/amd/init.c      | 50 +++--------------------------------
 2 files changed, 3 insertions(+), 48 deletions(-)
drivers/iommu/amd/amd_iommu.h +0 -1
index 7b43894f6b90..621ffb5d8669 100644
@@ -16,7 +16,6 @@ irqreturn_t amd_iommu_int_thread_evtlog(int irq, void *data);
 irqreturn_t amd_iommu_int_thread_pprlog(int irq, void *data);
 irqreturn_t amd_iommu_int_thread_galog(int irq, void *data);
 irqreturn_t amd_iommu_int_handler(int irq, void *data);
-void amd_iommu_apply_erratum_63(struct amd_iommu *iommu, u16 devid);
 void amd_iommu_restart_log(struct amd_iommu *iommu, const char *evt_type,
 			   u8 cntrl_intr, u8 cntrl_log,
 			   u32 status_run_mask, u32 status_overflow_mask);
drivers/iommu/amd/init.c +3 -47

75 lines not on this page · raw message

Signature · 1 line
2.34.1