Skip to contentSkip to conversation

iommu: Make flush queues a proper capability

PATCH Applied Robin Murphy 5 participants 8 messages May 4, 2023 – May 22, 2023

Subsystems (5)
lore
Share & tools
lore

Robin Murphy

author maintainer
#0 lore raw

cover iommu: Make flush queues a proper capability

Hi all,

Since it came up in discussion on the default domain series, it seemed pertinent to dig this idea up, whcih I started a while ago, and finish it off.

Cheers,
Robin.

Robin Murphy (2):
iommu: Add a capability for flush queue support
iommu: Use flush queue capability

 drivers/iommu/amd/iommu.c                   | 2 ++
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 2 +-
 drivers/iommu/arm/arm-smmu/arm-smmu.c       | 4 ++--
 drivers/iommu/dma-iommu.c                   | 3 ++-
 drivers/iommu/intel/iommu.c                 | 2 +-
 drivers/iommu/iommu.c                       | 3 ++-
 include/linux/iommu.h                       | 6 ++++++
 7 files changed, 16 insertions(+), 6 deletions(-)
Signature · 1 line
2.39.2.101.g768bb238c484.dirty
author maintainer
#1 lore raw

PATCH 1/2 iommu: Add a capability for flush queue support

Passing a special type to domain_alloc to indirectly query whether flush queues are a worthwhile optimisation with the given driver is a bit clunky, and looking increasingly anachronistic. Let's put that into an explicit capability instead.

---
 drivers/iommu/amd/iommu.c                   | 2 ++
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 1 +
 drivers/iommu/arm/arm-smmu/arm-smmu.c       | 1 +
 drivers/iommu/intel/iommu.c                 | 1 +
 include/linux/iommu.h                       | 5 +++++
 5 files changed, 10 insertions(+)
drivers/iommu/amd/iommu.c +2 -0
index 4a314647d1f7..9b7bd6bed664 100644
@@ -2293,6 +2293,8 @@ static bool amd_iommu_capable(struct device *dev, enum iommu_cap cap)
 		return amdr_ivrs_remap_support;
 	case IOMMU_CAP_ENFORCE_CACHE_COHERENCY:
 		return true;
+	case IOMMU_CAP_DEFERRED_FLUSH:
+		return true;
 	default:
 		break;
 	}
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +1 -0
index 3fd83fb75722..6d65a7e81df4 100644
@@ -2008,6 +2008,7 @@ static bool arm_smmu_capable(struct device *dev, enum iommu_cap cap)
 		/* Assume that a coherent TCU implies coherent TBUs */
 		return master->smmu->features & ARM_SMMU_FEAT_COHERENCY;
 	case IOMMU_CAP_NOEXEC:
+	case IOMMU_CAP_DEFERRED_FLUSH:
 		return true;
 	default:
 		return false;
drivers/iommu/arm/arm-smmu/arm-smmu.c +1 -0
index 6e0813b26fb6..7f4ee365912c 100644
@@ -1325,6 +1325,7 @@ static bool arm_smmu_capable(struct device *dev, enum iommu_cap cap)
 		return cfg->smmu->features & ARM_SMMU_FEAT_COHERENT_WALK ||
 			device_get_dma_attr(dev) == DEV_DMA_COHERENT;
 	case IOMMU_CAP_NOEXEC:
+	case IOMMU_CAP_DEFERRED_FLUSH:
 		return true;
 	default:
 		return false;
drivers/iommu/intel/iommu.c +1 -0
index b871a6afd803..ff923298f8ed 100644
@@ -4369,6 +4369,7 @@ static bool intel_iommu_capable(struct device *dev, enum iommu_cap cap)
 
 	switch (cap) {
 	case IOMMU_CAP_CACHE_COHERENCY:
+	case IOMMU_CAP_DEFERRED_FLUSH:
 		return true;
 	case IOMMU_CAP_PRE_BOOT_PROTECTION:
 		return dmar_platform_optin();
include/linux/iommu.h +5 -0
index e8c9a7da1060..1b7180d6edae 100644
@@ -127,6 +127,11 @@ enum iommu_cap {
 	 * this device.
 	 */
 	IOMMU_CAP_ENFORCE_CACHE_COHERENCY,
+	/*
+	 * IOMMU driver does not issue TLB maintenance during .unmap, so can
+	 * usefully support the non-strict DMA flush queue.
+	 */
+	IOMMU_CAP_DEFERRED_FLUSH,
 };
 
 /* These are the possible reserved region types */
Signature · 1 line
2.39.2.101.g768bb238c484.dirty
maintainer Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] iommu: Add a capability for flush queue support

Quoted 6 lines · Robin Murphy

Passing a special type to domain_alloc to indirectly query whether flush queues are a worthwhile optimisation with the given driver is a bit clunky, and looking increasingly anachronistic. Let's put that into an explicit capability instead.

Best regards,
baolu

author maintainer
#3 lore raw

PATCH 2/2 iommu: Use flush queue capability

In reply to Robin Murphy

It remains really handy to have distinct DMA domain types within core code for the sake of default domain policy selection, but we can now hide that detail from drivers by using the new capability instead.

---

Note that IOMMU_DOMAIN_ALLOC_FLAGS would go away again with the proposed domain_alloc_paging() interface design.

---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 1 -
 drivers/iommu/arm/arm-smmu/arm-smmu.c       | 3 +--
 drivers/iommu/dma-iommu.c                   | 3 ++-
 drivers/iommu/intel/iommu.c                 | 1 -
 drivers/iommu/iommu.c                       | 3 ++-
 include/linux/iommu.h                       | 1 +
 6 files changed, 6 insertions(+), 6 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0 -1
index 6d65a7e81df4..1ed9c4ed5db9 100644
@@ -2024,7 +2024,6 @@ static struct iommu_domain *arm_smmu_domain_alloc(unsigned type)
 
 	if (type != IOMMU_DOMAIN_UNMANAGED &&
 	    type != IOMMU_DOMAIN_DMA &&
-	    type != IOMMU_DOMAIN_DMA_FQ &&
 	    type != IOMMU_DOMAIN_IDENTITY)
 		return NULL;
 
drivers/iommu/arm/arm-smmu/arm-smmu.c +1 -2
index 7f4ee365912c..a86acd76c1df 100644
@@ -856,8 +856,7 @@ static struct iommu_domain *arm_smmu_domain_alloc(unsigned type)
 	struct arm_smmu_domain *smmu_domain;
 
 	if (type != IOMMU_DOMAIN_UNMANAGED && type != IOMMU_DOMAIN_IDENTITY) {
-		if (using_legacy_binding ||
-		    (type != IOMMU_DOMAIN_DMA && type != IOMMU_DOMAIN_DMA_FQ))
+		if (using_legacy_binding || type != IOMMU_DOMAIN_DMA)
 			return NULL;
 	}
 	/*
drivers/iommu/dma-iommu.c +2 -1
index 7a9f0b0bddbd..c4bdd2587daf 100644
@@ -586,7 +586,8 @@ static int iommu_dma_init_domain(struct iommu_domain *domain, dma_addr_t base,
 		goto done_unlock;
 
 	/* If the FQ fails we can simply fall back to strict mode */
-	if (domain->type == IOMMU_DOMAIN_DMA_FQ && iommu_dma_init_fq(domain))
+	if (domain->type == IOMMU_DOMAIN_DMA_FQ &&
+	    (!device_iommu_capable(dev, IOMMU_CAP_DEFERRED_FLUSH) || iommu_dma_init_fq(domain)))
 		domain->type = IOMMU_DOMAIN_DMA;
 
 	ret = iova_reserve_iommu_regions(dev, domain);
drivers/iommu/intel/iommu.c +0 -1
index ff923298f8ed..8096273b034c 100644
@@ -4064,7 +4064,6 @@ static struct iommu_domain *intel_iommu_domain_alloc(unsigned type)
 	case IOMMU_DOMAIN_BLOCKED:
 		return &blocking_domain;
 	case IOMMU_DOMAIN_DMA:
-	case IOMMU_DOMAIN_DMA_FQ:
 	case IOMMU_DOMAIN_UNMANAGED:
 		dmar_domain = alloc_domain(type);
 		if (!dmar_domain) {
drivers/iommu/iommu.c +2 -1
index f1dcfa3f1a1b..7078bf4a8ec8 100644
@@ -1980,11 +1980,12 @@ static struct iommu_domain *__iommu_domain_alloc(const struct bus_type *bus,
 						 unsigned type)
 {
 	struct iommu_domain *domain;
+	unsigned int alloc_type = type & IOMMU_DOMAIN_ALLOC_FLAGS;
 
 	if (bus == NULL || bus->iommu_ops == NULL)
 		return NULL;
 
-	domain = bus->iommu_ops->domain_alloc(type);
+	domain = bus->iommu_ops->domain_alloc(alloc_type);
 	if (!domain)
 		return NULL;
 
include/linux/iommu.h +1 -0
index 1b7180d6edae..d31642596675 100644
@@ -65,6 +65,7 @@ struct iommu_domain_geometry {
 
 #define __IOMMU_DOMAIN_SVA	(1U << 4)  /* Shared process address space */
 
+#define IOMMU_DOMAIN_ALLOC_FLAGS ~__IOMMU_DOMAIN_DMA_FQ
 /*
  * This are the possible domain-types
  *
Signature · 1 line
2.39.2.101.g768bb238c484.dirty
Reviewed-by on the v1 series
#4 lore raw
In reply to Robin Murphy
Quoted 5 lines · Robin Murphy

Hi all,

Since it came up in discussion on the default domain series, it seemed pertinent to dig this idea up, whcih I started a while ago, and finish it off.

I was using a flag in the ops since no driver need to make a dynamic decision in code, but this works well enough too.

It is a good step forward, so

Jason

Reviewed-by on the v1 series
#5 lore raw
In reply to Robin Murphy
Quoted 14 lines · Robin Murphy

Hi all,

Since it came up in discussion on the default domain series, it seemed pertinent to dig this idea up, whcih I started a while ago, and finish it off.

Cheers,
Robin.

Robin Murphy (2):
iommu: Add a capability for flush queue support
iommu: Use flush queue capability

Quoted 12 lines
 drivers/iommu/amd/iommu.c                   | 2 ++
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 2 +-
 drivers/iommu/arm/arm-smmu/arm-smmu.c       | 4 ++--
 drivers/iommu/dma-iommu.c                   | 3 ++-
 drivers/iommu/intel/iommu.c                 | 2 +-
 drivers/iommu/iommu.c                       | 3 ++-
 include/linux/iommu.h                       | 6 ++++++
 7 files changed, 16 insertions(+), 6 deletions(-)
Signature · 1 line
2.39.2.101.g768bb238c484.dirty
#6 lore raw
Quoted 24 lines · Jerry Snitselaar
Quoted 23 lines · Robin Murphy

Hi all,

Since it came up in discussion on the default domain series, it seemed pertinent to dig this idea up, whcih I started a while ago, and finish it off.

Cheers,
Robin.

Robin Murphy (2):
iommu: Add a capability for flush queue support
iommu: Use flush queue capability

 drivers/iommu/amd/iommu.c                   | 2 ++
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 2 +-
 drivers/iommu/arm/arm-smmu/arm-smmu.c       | 4 ++--
 drivers/iommu/dma-iommu.c                   | 3 ++-
 drivers/iommu/intel/iommu.c                 | 2 +-
 drivers/iommu/iommu.c                       | 3 ++-
 include/linux/iommu.h                       | 6 ++++++
 7 files changed, 16 insertions(+), 6 deletions(-)
Quoted 4 lines
Quoted 3 lines
Signature · 1 line
2.39.2.101.g768bb238c484.dirty
Quoted 2 lines
maintainer Applied
#7 lore raw
In reply to Robin Murphy
Quoted 3 lines · Robin Murphy

Robin Murphy (2):
iommu: Add a capability for flush queue support
iommu: Use flush queue capability

Applied, thanks Robin.