Skip to contentSkip to conversation

iommu/vt-d: Refactor PCI PRI enabling/disabling callbacks

PATCH In review Lu Baolu 3 participants 5 messages Jun 6, 2024 – Jun 27, 2024

Subsystems (2)
lore
Share & tools
lore

Lu Baolu

author maintainer
#0 lore raw

PATCH 1/1 iommu/vt-d: Refactor PCI PRI enabling/disabling callbacks

Commit 0095bf83554f8 ("iommu: Improve iopf_queue_remove_device()") specified the flow for disabling the PRI on a device. Refactor the PRI callbacks in the intel iommu driver to better manage PRI enabling and disabling and align it with the device queue interfaces in the iommu core.

---
 drivers/iommu/intel/iommu.h |  9 +++++
 drivers/iommu/intel/iommu.c | 77 ++++++++++++++++++++++++++++++++++---
 drivers/iommu/intel/pasid.c |  2 -
 3 files changed, 81 insertions(+), 7 deletions(-)
drivers/iommu/intel/iommu.h +9 -0
index eaf015b4353b..3d5d8f786906 100644
@@ -1047,6 +1047,15 @@ static inline void context_set_sm_pre(struct context_entry *context)
 	context->lo |= BIT_ULL(4);
 }
 
+/*
+ * Clear the PRE(Page Request Enable) field of a scalable mode context
+ * entry.
+ */
+static inline void context_clear_sm_pre(struct context_entry *context)
+{
+	context->lo &= ~BIT_ULL(4);
+}
+
 /* Returns a number of VTD pages, but aligned to MM page size */
 static inline unsigned long aligned_nrpages(unsigned long host_addr, size_t size)
 {
drivers/iommu/intel/iommu.c +72 -5

106 lines not on this page · raw message

drivers/iommu/intel/pasid.c +0 -2
index abce19e2ad6f..286a8a7d66f5 100644
@@ -768,8 +768,6 @@ static int context_entry_set_pasid_table(struct context_entry *context,
 
 	if (info->ats_supported)
 		context_set_sm_dte(context);
-	if (info->pri_supported)
-		context_set_sm_pre(context);
 	if (info->pasid_supported)
 		context_set_pasid(context);
 
Signature · 1 line
2.34.1
#1 lore raw
Quoted 19 lines

From: Lu Baolu <[email protected]>
Sent: Thursday, June 6, 2024 11:40 AM

drivers/iommu/intel/iommu.c +10 -0
+/*
+ * Invalidate the caches for a present-to-present change in a context
+ * table entry according to the Spec 6.5.3.3 (Guidance to Software for
+ * Invalidations).
+ *
+ * Since context entry is not encoded by domain-id when operating in
+ * scalable-mode (refer Section 6.2.1), this performs coarser
+ * invalidation than the domain-selective granularity requested.
+ */
+static void invalidate_present_context_change(struct device_domain_info

*info)

drivers/iommu/intel/iommu.c +4 -0
+{
+	struct intel_iommu *iommu = info->iommu;
+
+	iommu->flush.flush_context(iommu, 0, 0, 0,

DMA_CCMD_GLOBAL_INVL);

drivers/iommu/intel/iommu.c static int intel_iommu_enable_sva(struct device *dev) · line 4235

Quoted 6 lines
drivers/iommu/intel/iommu.c +6 -0
+	if (sm_supported(iommu))
+		qi_flush_pasid_cache(iommu, 0, QI_PC_GLOBAL, 0);
+	iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
+	__iommu_flush_dev_iotlb(info, 0, MAX_AGAW_PFN_WIDTH);
+}
+

this invalidates the entire cache/iotlb for all devices behind this iommu just due to a PRI enable/disable operation on a single device.

No that's way too much. If there is a burden to identify all active DIDs used by this device then pay it and penalize only that device.

btw in concept PRI will not be enabled/disabled when there are PASIDs of this device being actively attached. So at this point there should only be RID with attached domain then we only need to find that DID out and use it to invalidate related caches.

author maintainer
#2 lore raw
Quoted 30 lines · Kevin
Quoted 19 lines

From: Lu Baolu <[email protected]>
Sent: Thursday, June 6, 2024 11:40 AM

drivers/iommu/intel/iommu.c +10 -0
+/*
+ * Invalidate the caches for a present-to-present change in a context
+ * table entry according to the Spec 6.5.3.3 (Guidance to Software for
+ * Invalidations).
+ *
+ * Since context entry is not encoded by domain-id when operating in
+ * scalable-mode (refer Section 6.2.1), this performs coarser
+ * invalidation than the domain-selective granularity requested.
+ */
+static void invalidate_present_context_change(struct device_domain_info

*info)

drivers/iommu/intel/iommu.c +4 -0
+{
+	struct intel_iommu *iommu = info->iommu;
+
+	iommu->flush.flush_context(iommu, 0, 0, 0,

DMA_CCMD_GLOBAL_INVL);

drivers/iommu/intel/iommu.c static int intel_iommu_enable_sva(struct device *dev) · line 4235

Quoted 6 lines
drivers/iommu/intel/iommu.c +6 -0
+	if (sm_supported(iommu))
+		qi_flush_pasid_cache(iommu, 0, QI_PC_GLOBAL, 0);
+	iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
+	__iommu_flush_dev_iotlb(info, 0, MAX_AGAW_PFN_WIDTH);
+}
+

this invalidates the entire cache/iotlb for all devices behind this iommu just due to a PRI enable/disable operation on a single device.

Quoted 2 lines

No that's way too much. If there is a burden to identify all active DIDs used by this device then pay it and penalize only that device.

You are right. We should not simplify the flow like this.

Quoted 5 lines

btw in concept PRI will not be enabled/disabled when there are PASIDs of this device being actively attached. So at this point there should only be RID with attached domain then we only need to find that DID out and use it to invalidate related caches.

The assumption of "PRI will not be enabled/disabled when there are PASIDs of this device being actively attached" is not always correct. Both the pasid domain attachment and PRI are controlled by the device driver and there is no order rules for the drivers.

For example, the idxd driver attaches the default domain to a PASID and use it for kernel ENQCMD and use other PASIDs for SVA usage.

I am considering working out a generic helper to handle caches after change to a context entry what was present. How do you like below code (compiled but not tested)?

/*
* Cache invalidations after change in a context table entry that was
present
* according to the Spec 6.5.3.3 (Guidance to Software for
Invalidations). If
* IOMMU is in scalable mode and all PASID table entries of the device were
* non-present, set affect_domains to true. Otherwise, false. */
void intel_context_flush_present(struct device_domain_info *info,
struct context_entry *context, bool affect_domains)

{
	struct intel_iommu *iommu = info->iommu;
	u16 did = context_domain_id(context);
	struct pasid_entry *pte;
	int i;

	assert_spin_locked(&iommu->lock);

	/*
	 * Device-selective context-cache invalidation. The Domain-ID field
	 * of the Context-cache Invalidate Descriptor is ignored by hardware
	 * when operating in scalable mode. Therefore the @did value doesn't
	 * matter in scalable mode.
	 */
	iommu->flush.flush_context(iommu, did, PCI_DEVID(info->bus, info->devfn),
				   DMA_CCMD_MASK_NOBIT, DMA_CCMD_DEVICE_INVL);

	/*
	 * For legacy mode:
	 * - Domain-selective IOTLB invalidation
	 * - Global Device-TLB invalidation to all affected functions
	 */
	if (!sm_supported(iommu)) {
		iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH);
		__iommu_flush_dev_iotlb(info, 0, MAX_AGAW_PFN_WIDTH);

		return;
	}

	/*
	 * For scalable mode:
	 * - Domain-selective PASID-cache invalidation to affected domains
	 * - Domain-selective IOTLB invalidation to affected domains
	 * - Global Device-TLB invalidation to affected functions
	 */
	if (affect_domains) {
		for (i = 0; i < info->pasid_table->max_pasid; i++) {
			pte = intel_pasid_get_entry(info->dev, i);
			if (!pte || !pasid_pte_is_present(pte))
				continue;

			did = pasid_get_domain_id(pte);
			qi_flush_pasid_cache(iommu, did, QI_PC_ALL_PASIDS, 0);
			iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH);
		}
	}

	__iommu_flush_dev_iotlb(info, 0, MAX_AGAW_PFN_WIDTH);
}

Best regards,
baolu

#3 lore raw
Quoted 46 lines

From: Baolu Lu <[email protected]>
Sent: Wednesday, June 26, 2024 9:05 PM

Quoted 13 lines · Kevin

drivers/iommu/intel/iommu.c static int intel_iommu_enable_sva(struct device *dev)

Quoted 13 lines

From: Lu Baolu <[email protected]>
Sent: Thursday, June 6, 2024 11:40 AM

drivers/iommu/intel/iommu.c +10 -0
+/*
+ * Invalidate the caches for a present-to-present change in a context
+ * table entry according to the Spec 6.5.3.3 (Guidance to Software for
+ * Invalidations).
+ *
+ * Since context entry is not encoded by domain-id when operating in
+ * scalable-mode (refer Section 6.2.1), this performs coarser
+ * invalidation than the domain-selective granularity requested.
+ */
+static void invalidate_present_context_change(struct

device_domain_info

Quoted 17 lines

drivers/iommu/intel/iommu.c static int intel_iommu_enable_sva(struct device *dev) · line 4235

Quoted 12 lines

*info)

drivers/iommu/intel/iommu.c +4 -0
+{
+	struct intel_iommu *iommu = info->iommu;
+
+	iommu->flush.flush_context(iommu, 0, 0, 0,

DMA_CCMD_GLOBAL_INVL);

drivers/iommu/intel/iommu.c +6 -0
+	if (sm_supported(iommu))
+		qi_flush_pasid_cache(iommu, 0, QI_PC_GLOBAL, 0);
+	iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
+	__iommu_flush_dev_iotlb(info, 0, MAX_AGAW_PFN_WIDTH);
+}
+

this invalidates the entire cache/iotlb for all devices behind this iommu just due to a PRI enable/disable operation on a single device.

Quoted 2 lines

No that's way too much. If there is a burden to identify all active DIDs used by this device then pay it and penalize only that device.

You are right. We should not simplify the flow like this.

Quoted 5 lines

btw in concept PRI will not be enabled/disabled when there are PASIDs of this device being actively attached. So at this point there should only be RID with attached domain then we only need to find that DID out and use it to invalidate related caches.

Quoted 4 lines

The assumption of "PRI will not be enabled/disabled when there are PASIDs of this device being actively attached" is not always correct. Both the pasid domain attachment and PRI are controlled by the device driver and there is no order rules for the drivers.

Yeah. Not sure how I got it wrong in previous reply. 😊

Quoted 66 lines

For example, the idxd driver attaches the default domain to a PASID and use it for kernel ENQCMD and use other PASIDs for SVA usage.

I am considering working out a generic helper to handle caches after change to a context entry what was present. How do you like below code (compiled but not tested)?

/*
* Cache invalidations after change in a context table entry that was
present
* according to the Spec 6.5.3.3 (Guidance to Software for
Invalidations). If
* IOMMU is in scalable mode and all PASID table entries of the device were
* non-present, set affect_domains to true. Otherwise, false. */
void intel_context_flush_present(struct device_domain_info *info,
struct context_entry *context, bool affect_domains)

{
	struct intel_iommu *iommu = info->iommu;
	u16 did = context_domain_id(context);
	struct pasid_entry *pte;
	int i;

	assert_spin_locked(&iommu->lock);

	/*
	 * Device-selective context-cache invalidation. The Domain-ID field
	 * of the Context-cache Invalidate Descriptor is ignored by hardware
	 * when operating in scalable mode. Therefore the @did value
doesn't
	 * matter in scalable mode.
	 */
	iommu->flush.flush_context(iommu, did, PCI_DEVID(info->bus, info-
Quoted 1 line

devfn),

DMA_CCMD_MASK_NOBIT,
DMA_CCMD_DEVICE_INVL);

	/*
	 * For legacy mode:
	 * - Domain-selective IOTLB invalidation
	 * - Global Device-TLB invalidation to all affected functions
	 */
	if (!sm_supported(iommu)) {
		iommu->flush.flush_iotlb(iommu, did, 0, 0,
DMA_TLB_DSI_FLUSH);
		__iommu_flush_dev_iotlb(info, 0, MAX_AGAW_PFN_WIDTH);

		return;
	}

	/*
	 * For scalable mode:
	 * - Domain-selective PASID-cache invalidation to affected domains
	 * - Domain-selective IOTLB invalidation to affected domains
	 * - Global Device-TLB invalidation to affected functions
	 */
	if (affect_domains) {
		for (i = 0; i < info->pasid_table->max_pasid; i++) {
			pte = intel_pasid_get_entry(info->dev, i);
			if (!pte || !pasid_pte_is_present(pte))
				continue;

			did = pasid_get_domain_id(pte);
			qi_flush_pasid_cache(iommu, did,
Quoted 9 lines
QI_PC_ALL_PASIDS, 0);
			iommu->flush.flush_iotlb(iommu, did, 0, 0,
DMA_TLB_DSI_FLUSH);
		}
	}

	__iommu_flush_dev_iotlb(info, 0, MAX_AGAW_PFN_WIDTH);
}

looks good to me.

#4 lore raw
In reply to Tian, Kevin
Quoted 2 lines · Kevin

btw in concept PRI will not be enabled/disabled when there are PASIDs of this device being actively attached.

Please don't bake such an assumption in.

non-PRI paging domains can be attached to a PASID and should not enable PRI.

PRI is enabled when the first PRI domain is attached to a RID/PASID and disabled when the last PRI domain is detached.

Jason