Skip to contentSkip to conversation

iommu/amd: Misc cleanup and trivial fixes

PATCH Applied Vasant Hegde 2 participants 10 messages Aug 28, 2024 – Sep 4, 2024

Subsystems (2)
lore
Share & tools
lore

Vasant Hegde

author maintainer
#0 lore raw

cover iommu/amd: Misc cleanup and trivial fixes

This series contains misc cleanup and trivial fixes.

This series is based on top of iommu/next branch.

-Vasant

Vasant Hegde (8):
iommu/amd: Update event log pointer as soon as processing is complete iommu/amd: Make amd_iommu_is_attach_deferred() static iommu/amd: Remove unused DTE_GCR3_INDEX_* macros iommu/amd: Handle error path in amd_iommu_probe_device() iommu/amd: Make amd_iommu_dev_flush_pasid_all() static iommu/amd: Make amd_iommu_domain_flush_complete() static iommu/amd: Rework amd_iommu_update_and_flush_device_table() iommu/amd: Make amd_iommu_dev_update_dte() static

 drivers/iommu/amd/amd_iommu.h       |  5 --
 drivers/iommu/amd/amd_iommu_types.h |  4 --
 drivers/iommu/amd/io_pgtable.c      |  1 -
 drivers/iommu/amd/iommu.c           | 87 ++++++++++++++---------------
 4 files changed, 41 insertions(+), 56 deletions(-)

base-commit: 0e922e0fecffdb8e0f5ea9c00754f65a17e5a535

Signature · 1 line
2.31.1
author maintainer
#1 lore raw

PATCH 1/8 iommu/amd: Update event log pointer as soon as processing is complete

Update event buffer head pointer once driver completes processing. So that IOMMU can write new log without waiting for driver to complete processing all event logs.

---
 drivers/iommu/amd/iommu.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/iommu/amd/iommu.c +3 -1
index dbe838828efe..e13e401c52de 100644
@@ -825,10 +825,12 @@ static void iommu_poll_events(struct amd_iommu *iommu)
 
 	while (head != tail) {
 		iommu_print_event(iommu, iommu->evt_buf + head);
+
+		/* Update head pointer of hardware ring-buffer */
 		head = (head + EVENT_ENTRY_SIZE) % EVT_BUFFER_SIZE;
+		writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
 	}
 
-	writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
 }
 
 #ifdef CONFIG_IRQ_REMAP
Signature · 1 line
2.31.1
author maintainer
#2 lore raw

PATCH 2/8 iommu/amd: Make amd_iommu_is_attach_deferred() static

In reply to Vasant Hegde

amd_iommu_is_attach_deferred() is a callback function called by iommu_ops. Make it as static.

No functional changes intended.

---
 drivers/iommu/amd/amd_iommu.h | 1 -
 drivers/iommu/amd/iommu.c     | 2 +-
 2 files changed, 1 insertion(+), 2 deletions(-)
drivers/iommu/amd/amd_iommu.h +0 -1
index 52e18b5f99fd..a9e207ec2bbb 100644
@@ -180,7 +180,6 @@ static inline struct protection_domain *to_pdomain(struct iommu_domain *dom)
 }
 
 bool translation_pre_enabled(struct amd_iommu *iommu);
-bool amd_iommu_is_attach_deferred(struct device *dev);
 int __init add_special_device(u8 type, u8 id, u32 *devid, bool cmd_line);
 
 #ifdef CONFIG_DMI
drivers/iommu/amd/iommu.c +1 -1
index e13e401c52de..c57ea47183c3 100644
@@ -2785,7 +2785,7 @@ static void amd_iommu_get_resv_regions(struct device *dev,
 	list_add_tail(&region->list, head);
 }
 
-bool amd_iommu_is_attach_deferred(struct device *dev)
+static bool amd_iommu_is_attach_deferred(struct device *dev)
 {
 	struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
 
Signature · 1 line
2.31.1
author maintainer
#3 lore raw

PATCH 3/8 iommu/amd: Remove unused DTE_GCR3_INDEX_* macros

In reply to Vasant Hegde

It was added in commit 52815b75682e ("iommu/amd: Add support for IOMMUv2 domain mode"), but never used it. Hence remove these unused macros.

---
 drivers/iommu/amd/amd_iommu_types.h | 4 ----
 1 file changed, 4 deletions(-)
drivers/iommu/amd/amd_iommu_types.h +0 -4
index c9f9a598eb82..c7432296bb90 100644
@@ -415,10 +415,6 @@
 #define DTE_GCR3_VAL_B(x)	(((x) >> 15) & 0x0ffffULL)
 #define DTE_GCR3_VAL_C(x)	(((x) >> 31) & 0x1fffffULL)
 
-#define DTE_GCR3_INDEX_A	0
-#define DTE_GCR3_INDEX_B	1
-#define DTE_GCR3_INDEX_C	1
-
 #define DTE_GCR3_SHIFT_A	58
 #define DTE_GCR3_SHIFT_B	16
 #define DTE_GCR3_SHIFT_C	43
Signature · 1 line
2.31.1
author maintainer
#4 lore raw

PATCH 4/8 iommu/amd: Handle error path in amd_iommu_probe_device()

In reply to Vasant Hegde

Do not try to set max_pasids in error path as dev_data is not allocated.

Fixes a0c47f233e68 ("iommu/amd: Introduce iommu_dev_data.max_pasids")
---
 drivers/iommu/amd/iommu.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/iommu/amd/iommu.c +7 -5
index c57ea47183c3..be31a263ac6f 100644
@@ -2187,11 +2187,12 @@ static struct iommu_device *amd_iommu_probe_device(struct device *dev)
 		dev_err(dev, "Failed to initialize - trying to proceed anyway\n");
 		iommu_dev = ERR_PTR(ret);
 		iommu_ignore_device(iommu, dev);
-	} else {
-		amd_iommu_set_pci_msi_domain(dev, iommu);
-		iommu_dev = &iommu->iommu;
+		goto out_err;
 	}
 
+	amd_iommu_set_pci_msi_domain(dev, iommu);
+	iommu_dev = &iommu->iommu;
+
 	/*
 	 * If IOMMU and device supports PASID then it will contain max
 	 * supported PASIDs, else it will be zero.
@@ -2203,11 +2204,12 @@ static struct iommu_device *amd_iommu_probe_device(struct device *dev)
 					     pci_max_pasids(to_pci_dev(dev)));
 	}
 
-	iommu_completion_wait(iommu);
-
 	if (dev_is_pci(dev))
 		pci_prepare_ats(to_pci_dev(dev), PAGE_SHIFT);
 
+out_err:
+	iommu_completion_wait(iommu);
+
 	return iommu_dev;
 }
 
Signature · 1 line
2.31.1
author maintainer
#5 lore raw

PATCH 5/8 iommu/amd: Make amd_iommu_dev_flush_pasid_all() static

In reply to Vasant Hegde

As its not used outside iommu.c. Also rename it as dev_flush_pasid_all().

No functional change intended.

---
 drivers/iommu/amd/amd_iommu.h | 2 --
 drivers/iommu/amd/iommu.c     | 6 +++---
 2 files changed, 3 insertions(+), 5 deletions(-)
drivers/iommu/amd/amd_iommu.h +0 -2
index a9e207ec2bbb..d0a24ec3ada2 100644
@@ -93,8 +93,6 @@ void amd_iommu_domain_flush_pages(struct protection_domain *domain,
 				  u64 address, size_t size);
 void amd_iommu_dev_flush_pasid_pages(struct iommu_dev_data *dev_data,
 				     ioasid_t pasid, u64 address, size_t size);
-void amd_iommu_dev_flush_pasid_all(struct iommu_dev_data *dev_data,
-				   ioasid_t pasid);
 
 #ifdef CONFIG_IRQ_REMAP
 int amd_iommu_create_irq_domain(struct amd_iommu *iommu);
drivers/iommu/amd/iommu.c +3 -3
index be31a263ac6f..c06189a811ea 100644
@@ -1551,8 +1551,8 @@ void amd_iommu_dev_flush_pasid_pages(struct iommu_dev_data *dev_data,
 	iommu_completion_wait(iommu);
 }
 
-void amd_iommu_dev_flush_pasid_all(struct iommu_dev_data *dev_data,
-				   ioasid_t pasid)
+static void dev_flush_pasid_all(struct iommu_dev_data *dev_data,
+				ioasid_t pasid)
 {
 	amd_iommu_dev_flush_pasid_pages(dev_data, 0,
 					CMD_INV_IOMMU_ALL_PAGES_ADDRESS, pasid);
@@ -1818,7 +1818,7 @@ static int update_gcr3(struct iommu_dev_data *dev_data,
 	else
 		*pte = 0;
 
-	amd_iommu_dev_flush_pasid_all(dev_data, pasid);
+	dev_flush_pasid_all(dev_data, pasid);
 	return 0;
 }
 
Signature · 1 line
2.31.1
author maintainer
#6 lore raw

PATCH 6/8 iommu/amd: Make amd_iommu_domain_flush_complete() static

In reply to Vasant Hegde

AMD driver uses amd_iommu_domain_flush_complete() function to make sure IOMMU processed invalidation commands before proceeding. Ideally this should be called from functions which updates DTE/invalidates caches. There is no need to call this function explicitly. This patches makes below changes :

- Rename amd_iommu_domain_flush_complete() -> domain_flush_complete() and make it as static function.

- Rearrage domain_flush_complete() to avoid forward declaration.

- Update amd_iommu_update_and_flush_device_table() to call domain_flush_complete().

---
 drivers/iommu/amd/amd_iommu.h  |  1 -
 drivers/iommu/amd/io_pgtable.c |  1 -
 drivers/iommu/amd/iommu.c      | 37 +++++++++++++++++-----------------
 3 files changed, 19 insertions(+), 20 deletions(-)
drivers/iommu/amd/amd_iommu.h +0 -1
index d0a24ec3ada2..94402b88789d 100644
@@ -88,7 +88,6 @@ void amd_iommu_flush_all_caches(struct amd_iommu *iommu);
 void amd_iommu_update_and_flush_device_table(struct protection_domain *domain);
 void amd_iommu_domain_update(struct protection_domain *domain);
 void amd_iommu_dev_update_dte(struct iommu_dev_data *dev_data, bool set);
-void amd_iommu_domain_flush_complete(struct protection_domain *domain);
 void amd_iommu_domain_flush_pages(struct protection_domain *domain,
 				  u64 address, size_t size);
 void amd_iommu_dev_flush_pasid_pages(struct iommu_dev_data *dev_data,
drivers/iommu/amd/io_pgtable.c +0 -1
index 1074ee25064d..bfbcec68efb9 100644
@@ -175,7 +175,6 @@ static bool increase_address_space(struct protection_domain *domain,
 	domain->iop.root  = pte;
 	domain->iop.mode += 1;
 	amd_iommu_update_and_flush_device_table(domain);
-	amd_iommu_domain_flush_complete(domain);
 
 	/*
 	 * Device Table needs to be updated and flushed before the new root can
drivers/iommu/amd/iommu.c +19 -18
index c06189a811ea..56c51b83ee6b 100644
@@ -1249,6 +1249,22 @@ static int iommu_completion_wait(struct amd_iommu *iommu)
 	return ret;
 }
 
+static void domain_flush_complete(struct protection_domain *domain)
+{
+	int i;
+
+	for (i = 0; i < amd_iommu_get_num_iommus(); ++i) {
+		if (domain && !domain->dev_iommu[i])
+			continue;
+
+		/*
+		 * Devices of this domain are behind this IOMMU
+		 * We need to wait for completion of all commands.
+		 */
+		iommu_completion_wait(amd_iommus[i]);
+	}
+}
+
 static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
 {
 	struct iommu_cmd cmd;
@@ -1485,7 +1501,7 @@ void amd_iommu_domain_flush_pages(struct protection_domain *domain,
 		__domain_flush_pages(domain, address, size);
 
 		/* Wait until IOMMU TLB and all device IOTLB flushes are complete */
-		amd_iommu_domain_flush_complete(domain);
+		domain_flush_complete(domain);
 
 		return;
 	}
@@ -1525,7 +1541,7 @@ void amd_iommu_domain_flush_pages(struct protection_domain *domain,
 	}
 
 	/* Wait until IOMMU TLB and all device IOTLB flushes are complete */
-	amd_iommu_domain_flush_complete(domain);
+	domain_flush_complete(domain);
 }
 
 /* Flush the whole IO/TLB for a given protection domain - including PDE */
@@ -1558,22 +1574,6 @@ static void dev_flush_pasid_all(struct iommu_dev_data *dev_data,
 					CMD_INV_IOMMU_ALL_PAGES_ADDRESS, pasid);
 }
 
-void amd_iommu_domain_flush_complete(struct protection_domain *domain)
-{
-	int i;
-
-	for (i = 0; i < amd_iommu_get_num_iommus(); ++i) {
-		if (domain && !domain->dev_iommu[i])
-			continue;
-
-		/*
-		 * Devices of this domain are behind this IOMMU
-		 * We need to wait for completion of all commands.
-		 */
-		iommu_completion_wait(amd_iommus[i]);
-	}
-}
-
 /* Flush the not present cache if it exists */
 static void domain_flush_np_cache(struct protection_domain *domain,
 		dma_addr_t iova, size_t size)
@@ -1615,6 +1615,7 @@ void amd_iommu_update_and_flush_device_table(struct protection_domain *domain)
 {
 	update_device_table(domain);
 	domain_flush_devices(domain);
+	domain_flush_complete(domain);
 }
 
 void amd_iommu_domain_update(struct protection_domain *domain)
Signature · 1 line
2.31.1
author maintainer
#7 lore raw

PATCH 7/8 iommu/amd: Rework amd_iommu_update_and_flush_device_table()

In reply to Vasant Hegde

Remove separate function to update and flush the device table as only amd_iommu_update_and_flush_device_table() calls these functions.

No functional changes intended.

---
 drivers/iommu/amd/iommu.c | 18 ++++--------------
 1 file changed, 4 insertions(+), 14 deletions(-)
drivers/iommu/amd/iommu.c +4 -14
index 56c51b83ee6b..808959be0902 100644
@@ -1591,15 +1591,7 @@ static void domain_flush_np_cache(struct protection_domain *domain,
 /*
  * This function flushes the DTEs for all devices in domain
  */
-static void domain_flush_devices(struct protection_domain *domain)
-{
-	struct iommu_dev_data *dev_data;
-
-	list_for_each_entry(dev_data, &domain->dev_list, list)
-		device_flush_dte(dev_data);
-}
-
-static void update_device_table(struct protection_domain *domain)
+void amd_iommu_update_and_flush_device_table(struct protection_domain *domain)
 {
 	struct iommu_dev_data *dev_data;
 
@@ -1609,12 +1601,10 @@ static void update_device_table(struct protection_domain *domain)
 		set_dte_entry(iommu, dev_data);
 		clone_aliases(iommu, dev_data->dev);
 	}
-}
 
-void amd_iommu_update_and_flush_device_table(struct protection_domain *domain)
-{
-	update_device_table(domain);
-	domain_flush_devices(domain);
+	list_for_each_entry(dev_data, &domain->dev_list, list)
+		device_flush_dte(dev_data);
+
 	domain_flush_complete(domain);
 }
 
Signature · 1 line
2.31.1
author maintainer
#8 lore raw

PATCH 8/8 iommu/amd: Make amd_iommu_dev_update_dte() static

In reply to Vasant Hegde

As its used inside iommu.c only. Also rename function to dev_update_dte() as its static function.

No functional changes intended.

---
 drivers/iommu/amd/amd_iommu.h | 1 -
 drivers/iommu/amd/iommu.c     | 8 ++++----
 2 files changed, 4 insertions(+), 5 deletions(-)
drivers/iommu/amd/amd_iommu.h +0 -1
index 94402b88789d..705dd364afe3 100644
@@ -87,7 +87,6 @@ int amd_iommu_complete_ppr(struct device *dev, u32 pasid, int status, int tag);
 void amd_iommu_flush_all_caches(struct amd_iommu *iommu);
 void amd_iommu_update_and_flush_device_table(struct protection_domain *domain);
 void amd_iommu_domain_update(struct protection_domain *domain);
-void amd_iommu_dev_update_dte(struct iommu_dev_data *dev_data, bool set);
 void amd_iommu_domain_flush_pages(struct protection_domain *domain,
 				  u64 address, size_t size);
 void amd_iommu_dev_flush_pasid_pages(struct iommu_dev_data *dev_data,
drivers/iommu/amd/iommu.c +4 -4
index 808959be0902..82c0ee527732 100644
@@ -1955,7 +1955,7 @@ static void clear_dte_entry(struct amd_iommu *iommu, u16 devid)
 }
 
 /* Update and flush DTE for the given device */
-void amd_iommu_dev_update_dte(struct iommu_dev_data *dev_data, bool set)
+static void dev_update_dte(struct iommu_dev_data *dev_data, bool set)
 {
 	struct amd_iommu *iommu = get_amd_iommu_from_dev(dev_data->dev);
 
@@ -2055,7 +2055,7 @@ static void do_detach(struct iommu_dev_data *dev_data)
 	struct amd_iommu *iommu = get_amd_iommu_from_dev_data(dev_data);
 
 	/* Clear DTE and flush the entry */
-	amd_iommu_dev_update_dte(dev_data, false);
+	dev_update_dte(dev_data, false);
 
 	/* Flush IOTLB and wait for the flushes to finish */
 	amd_iommu_domain_flush_all(domain);
@@ -2470,7 +2470,7 @@ static int blocked_domain_attach_device(struct iommu_domain *domain,
 
 	/* Clear DTE and flush the entry */
 	spin_lock(&dev_data->lock);
-	amd_iommu_dev_update_dte(dev_data, false);
+	dev_update_dte(dev_data, false);
 	spin_unlock(&dev_data->lock);
 
 	return 0;
@@ -2538,7 +2538,7 @@ static int amd_iommu_attach_device(struct iommu_domain *dom,
 	}
 
 	/* Update device table */
-	amd_iommu_dev_update_dte(dev_data, true);
+	dev_update_dte(dev_data, true);
 
 	return ret;
 }
Signature · 1 line
2.31.1
maintainer Applied
#9 lore raw
In reply to Vasant Hegde
Quoted 9 lines · Vasant Hegde

Vasant Hegde (8):
iommu/amd: Update event log pointer as soon as processing is complete iommu/amd: Make amd_iommu_is_attach_deferred() static iommu/amd: Remove unused DTE_GCR3_INDEX_* macros iommu/amd: Handle error path in amd_iommu_probe_device() iommu/amd: Make amd_iommu_dev_flush_pasid_all() static iommu/amd: Make amd_iommu_domain_flush_complete() static iommu/amd: Rework amd_iommu_update_and_flush_device_table() iommu/amd: Make amd_iommu_dev_update_dte() static

Applied, thanks.