Skip to contentSkip to conversation

IOMMU SVA device driver interface

PATCH Applied Jean-Philippe Brucker 2 participants 6 messages Apr 10, 2019 – Apr 12, 2019 IOMMU

lore
Share & tools
lore
#0 lore raw

v3 cover IOMMU SVA device driver interface

This is the device driver API for SVA (Shared Virtual Addressing). Since v2 I removed the set_sva_ops() IOMMU operation as suggested by Joerg, instead doing the sva_ops assignment in iommu.c.

The four dev_feature functions are implemented by Lu Baolu's IOMMU-aware mdev series [2].

	iommu_dev_has_feature(dev, IOMMU_DEV_FEAT_SVA) -> true/false
	iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA) -> 0/err
	iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA) -> 0/err
	iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_SVA) -> true/false

Patch 1/1 adds the next four functions, once the SVA feature is enabled:

	iommu_sva_bind(dev, mm, drvdata) -> handle
	iommu_sva_set_ops(handle, iommu_sva_ops) -> 0/err
	iommu_sva_get_pasid(handle) -> pasid/invalid
	iommu_sva_unbind(handle)

Generic implementation and SMMUv3 support can be found at [3].

[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/
[3] git://linux-arm.org/linux-jpb.git sva/current
http://www.linux-arm.org/git?p=linux-jpb.git;a=shortlog;h=refs/heads/sva/current

Jean-Philippe Brucker (1):
iommu: Bind process address spaces to devices

 drivers/iommu/iommu.c | 104 ++++++++++++++++++++++++++++++++++++++++++
 include/linux/iommu.h |  70 ++++++++++++++++++++++++++++
 2 files changed, 174 insertions(+)
Signature · 1 line
2.21.0
#1 lore raw

PATCH v3 1/1 iommu: Bind process address spaces to devices

Add bind() and unbind() operations to the IOMMU API. iommu_sva_bind_device() binds a device to an mm, and returns a handle to the bond, which is released by calling iommu_sva_unbind_device().

Each mm bound to devices gets a PASID (by convention, a 20-bit system-wide ID representing the address space), which can be retrieved with iommu_sva_get_pasid(). When programming DMA addresses, device drivers include this PASID in a device-specific manner, to let the device access the given address space. Since the process memory may be paged out, device and IOMMU must support I/O page faults (e.g. PCI PRI).

Using iommu_sva_set_ops(), device drivers provide an mm_exit() callback that is called by the IOMMU driver if the process exits before the device driver called unbind(). In mm_exit(), device driver should disable DMA from the given context, so that the core IOMMU can reallocate the PASID. Whether the process exited or nor, the device driver should always release the handle with unbind().

To use these functions, device driver must first enable the IOMMU_DEV_FEAT_SVA device feature with iommu_dev_enable_feature().

---
 drivers/iommu/iommu.c | 104 ++++++++++++++++++++++++++++++++++++++++++
 include/linux/iommu.h |  70 ++++++++++++++++++++++++++++
 2 files changed, 174 insertions(+)
drivers/iommu/iommu.c +104 -0
index d3403d84b20c..65e3b2f06e22 100644
@@ -2357,3 +2357,107 @@ int iommu_aux_get_pasid(struct iommu_domain *domain, struct device *dev)
 	return ret;
 }
 EXPORT_SYMBOL_GPL(iommu_aux_get_pasid);
+
+/**
+ * iommu_sva_bind_device() - Bind a process address space to a device
+ * @dev: the device
+ * @mm: the mm to bind, caller must hold a reference to it
+ *
+ * Create a bond between device and address space, allowing the device to access
+ * the mm using the returned PASID. If a bond already exists between @device and
+ * @mm, it is returned and an additional reference is taken. Caller must call
+ * iommu_sva_unbind_device() to release each reference.
+ *
+ * iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA) must be called first, to
+ * initialize the required SVA features.
+ *
+ * On error, returns an ERR_PTR value.
+ */
+struct iommu_sva *
+iommu_sva_bind_device(struct device *dev, struct mm_struct *mm, void *drvdata)
+{
+	struct iommu_group *group;
+	struct iommu_sva *handle = ERR_PTR(-EINVAL);
+	const struct iommu_ops *ops = dev->bus->iommu_ops;
+
+	if (!ops || !ops->sva_bind)
+		return ERR_PTR(-ENODEV);
+
+	group = iommu_group_get(dev);
+	if (!group)
+		return ERR_PTR(-ENODEV);
+
+	/* Ensure device count and domain don't change while we're binding */
+	mutex_lock(&group->mutex);
+
+	/*
+	 * To keep things simple, SVA currently doesn't support IOMMU groups
+	 * with more than one device. Existing SVA-capable systems are not
+	 * affected by the problems that required IOMMU groups (lack of ACS
+	 * isolation, device ID aliasing and other hardware issues).
+	 */
+	if (iommu_group_device_count(group) != 1)
+		goto out_unlock;
+
+	handle = ops->sva_bind(dev, mm, drvdata);
+
+out_unlock:
+	mutex_unlock(&group->mutex);
+	iommu_group_put(group);
+
+	return handle;
+}
+EXPORT_SYMBOL_GPL(iommu_sva_bind_device);
+
+/**
+ * iommu_sva_unbind_device() - Remove a bond created with iommu_sva_bind_device
+ * @handle: the handle returned by iommu_sva_bind_device()
+ *
+ * Put reference to a bond between device and address space. The device should
+ * not be issuing any more transaction for this PASID. All outstanding page
+ * requests for this PASID must have been flushed to the IOMMU.
+ *
+ * Returns 0 on success, or an error value
+ */
+void iommu_sva_unbind_device(struct iommu_sva *handle)
+{
+	struct iommu_group *group;
+	struct device *dev = handle->dev;
+	const struct iommu_ops *ops = dev->bus->iommu_ops;
+
+	if (!ops || !ops->sva_unbind)
+		return;
+
+	group = iommu_group_get(dev);
+	if (!group)
+		return;
+
+	mutex_lock(&group->mutex);
+	ops->sva_unbind(handle);
+	mutex_unlock(&group->mutex);
+
+	iommu_group_put(group);
+}
+EXPORT_SYMBOL_GPL(iommu_sva_unbind_device);
+
+int iommu_sva_set_ops(struct iommu_sva *handle,
+		      const struct iommu_sva_ops *sva_ops)
+{
+	if (handle->ops && handle->ops != sva_ops)
+		return -EEXIST;
+
+	handle->ops = sva_ops;
+	return 0;
+}
+EXPORT_SYMBOL_GPL(iommu_sva_set_ops);
+
+int iommu_sva_get_pasid(struct iommu_sva *handle)
+{
+	const struct iommu_ops *ops = handle->dev->bus->iommu_ops;
+
+	if (!ops || !ops->sva_get_pasid)
+		return IOMMU_PASID_INVALID;
+
+	return ops->sva_get_pasid(handle);
+}
+EXPORT_SYMBOL_GPL(iommu_sva_get_pasid);
include/linux/iommu.h +70 -0

116 lines not on this page · raw message

Signature · 1 line
2.21.0
maintainer Applied
#2 lore raw

Re: [PATCH v3 1/1] iommu: Bind process address spaces to devices

Quoted 3 lines · Jean-Philippe Brucker
 drivers/iommu/iommu.c | 104 ++++++++++++++++++++++++++++++++++++++++++
 include/linux/iommu.h |  70 ++++++++++++++++++++++++++++
 2 files changed, 174 insertions(+)

Applied to the api-features branch for now, thanks Jean-Philippe!

'for now' because I have a question below.

include/linux/iommu.h struct device; · line 54

Quoted 9 lines
include/linux/iommu.h +1 -0
index ed6738c358ca..87fe1cddd817 100644
@@ -51,10 +51,13 @@ struct device;
 struct iommu_domain;
 struct notifier_block;
 struct iommu_fault_event;
+struct iommu_sva;

The iommu_fault_event doesn't yet exist in my tree, is this patch based on something else I need besided Lu Baolus work? I got quite some conflicts while applying the patch because of this.

include/linux/iommu.h struct page_response_msg { · line 261

Quoted 1 line
include/linux/iommu.h +1 -0
+ * @sva_set_ops: Set ops for a SVA handle

I also removed this line.

Regards,

Joerg

#3 lore raw

Re: [PATCH v3 1/1] iommu: Bind process address spaces to devices

Quoted 9 lines · Joerg Roedel
Quoted 3 lines · Jean-Philippe Brucker

  drivers/iommu/iommu.c | 104 ++++++++++++++++++++++++++++++++++++++++++ include/linux/iommu.h |  70 ++++++++++++++++++++++++++++

  2 files changed, 174 insertions(+)

Applied to the api-features branch for now, thanks Jean-Philippe!

'for now' because I have a question below.

Quoted 13 lines

include/linux/iommu.h struct device; · line 54

Quoted 9 lines
include/linux/iommu.h +1 -0
index ed6738c358ca..87fe1cddd817 100644
@@ -51,10 +51,13 @@ struct device;
   struct iommu_domain;
   struct notifier_block;
   struct iommu_fault_event;
+struct iommu_sva;

The iommu_fault_event doesn't yet exist in my tree, is this patch based on something else I need besided Lu Baolus work? I got quite some conflicts while applying the patch because of this.

Ugh, sorry about this, I forgot to rebase onto master. The only required patch is Lu Baolu's "iommu: Add APIs for multiple domains per device". The iommu_fault_event is introduced by Jacob's fault reporting work, but is independent from this patch.

There also is a small conflict with the SMMUv3 ATS work, which I'm hoping to get in v5.2 as well (but will probably require more work). I'll send next version of ATS applied on top of this patch.

I pushed a cleaner version to git://linux-arm.org/linux-jpb.git sva/api (with the below mistake fixed as well) and can resend if necessary.

Thanks,
Jean

Quoted 8 lines

include/linux/iommu.h struct page_response_msg { · line 261

Quoted 2 lines
include/linux/iommu.h +1 -0
+ * @sva_set_ops: Set ops for a SVA handle

I also removed this line.

Regards,

        Joerg

maintainer
#4 lore raw

Re: [PATCH v3 1/1] iommu: Bind process address spaces to devices

Hey Jean-Philippe,

Quoted 2 lines · Jean-Philippe Brucker

I pushed a cleaner version to git://linux-arm.org/linux-jpb.git sva/api (with the below mistake fixed as well) and can resend if necessary.

No need to do that, I solved the conflicts I've seen and compile-testing looked good so far, so I've pushed it out already. Maybe you can double-check my tree if everything is as it should be?

Regards,

Joerg

#5 lore raw

Re: [PATCH v3 1/1] iommu: Bind process address spaces to devices

Quoted 9 lines · Joerg Roedel

Hey Jean-Philippe,

Quoted 2 lines · Jean-Philippe Brucker

I pushed a cleaner version to git://linux-arm.org/linux-jpb.git sva/api (with the below mistake fixed as well) and can resend if necessary.

No need to do that, I solved the conflicts I've seen and compile-testing looked good so far, so I've pushed it out already. Maybe you can double-check my tree if everything is as it should be?

It looks fine, thanks!

Jean