IOMMU SVA device driver interface
v2 cover IOMMU SVA device driver interface
This is the device driver API for SVA (Shared Virtual Addressing). Changes since v1 [1]:
* Following comments, return a handle rather than a PASID. I agree that it makes things easier for device drivers as well, because they don't need to worry about the unbind()/mm_exit() race as much. Previously they couldn't issue an unbind() if the mm exited since the PASID could have been reallocated.
* If a handle already exists, reuse it and take a reference instead of returning EEXIST. As noted by Zhangfei and myself during development, it can make things a bit easier for device drivers that need to handle multiple threads in a process issuing bind() for the same dev and mm.
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 /* * Only exit_mm() for now, but later callbacks to deal with * unhandled PRI faults and possible others: */ iommu_sva_set_ops(handle, iommu_sva_ops) -> 0/err iommu_sva_get_pasid(handle) -> pasid/invalid iommu_sva_unbind(handle)
Full support for the SMMUv3 can be found at [3]
[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/T/
[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 | 105 ++++++++++++++++++++++++++++++++++++++++++ include/linux/iommu.h | 71 ++++++++++++++++++++++++++++ 2 files changed, 176 insertions(+)
Signature · 1 line
2.21.0
PATCH v2 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 | 105 ++++++++++++++++++++++++++++++++++++++++++ include/linux/iommu.h | 71 ++++++++++++++++++++++++++++ 2 files changed, 176 insertions(+)
drivers/iommu/iommu.c +105 -0
@@ -2355,3 +2355,108 @@ 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) +{ +const struct iommu_ops *ops = handle->dev->bus->iommu_ops; + +if (!ops || !ops->sva_set_ops) +return -ENODEV ; + +return ops->sva_set_ops (handle, sva_ops); +} +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 +71 -0
117 lines not on this page · raw message
Signature · 1 line
2.21.0
Joerg Roedel
maintainerRe: [PATCH v2 1/1] iommu: Bind process address spaces to devices
Hey Jean-Philippe,
thanks for the patch, I think we are on the finish line with this interface. Just one small question below.
drivers/iommu/iommu.c int iommu_aux_get_pasid(struct iommu_domain *domain, struct device *dev) · line 2450
Quoted 10 lines · Jean-Philippe Brucker
drivers/iommu/iommu.c +10
-0+int iommu_sva_set_ops (struct iommu_sva *handle, +const struct iommu_sva_ops *sva_ops) +{ +const struct iommu_ops *ops = handle->dev->bus->iommu_ops; + +if (!ops || !ops->sva_set_ops) +return -ENODEV ; + +return ops->sva_set_ops (handle, sva_ops); +}
What is the purpose of the sva_set_ops call-back in iommu-ops? Is the IOMMU driver supposed to do some extra setup work with the provided ops? Otherwise we can just store the pointer in 'struct iommu_sva' without calling into the iommu driver.
Regards,
Joerg
Re: [PATCH v2 1/1] iommu: Bind process address spaces to devices
Hi Joerg,
Quoted 6 lines · Joerg Roedel
Hey Jean-Philippe,
thanks for the patch, I think we are on the finish line with this interface. Just one small question below.
On Wed, Mar 20, 2019 at 03:02:58PM +0000, Jean-Philippe Brucker wrote:
Quoted 15 lines
drivers/iommu/iommu.c int iommu_aux_get_pasid(struct iommu_domain *domain, struct device *dev) · line 2450
Quoted 10 lines
drivers/iommu/iommu.c +10
-0+int iommu_sva_set_ops (struct iommu_sva *handle, +const struct iommu_sva_ops *sva_ops) +{ +const struct iommu_ops *ops = handle->dev->bus->iommu_ops; + +if (!ops || !ops->sva_set_ops) +return -ENODEV ; + +return ops->sva_set_ops (handle, sva_ops); +}What is the purpose of the
sva_set_opscall-back in iommu-ops? Is the IOMMU driver supposed to do some extra setup work with the provided ops? Otherwise we can just store the pointer in 'structiommu_sva' without calling into the iommu driver.
Right that would be simpler. I added the sva_set_ops callback so that IOMMU driver can use the same lock in unbind() and sva_set_ops(). But I think it's safe to assume that device drivers don't call unbind() before setting the ops.
Thanks,
Jean