History

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().

Jean-Philippe Brucker committed 7 years ago

Commit 26b25a21 parent a3a19592 files · +174 −0Browse files

Showing the changes to include/linux (1 of 2 files). Show all

Unified · Split

include/linux/iommu.h+70−0
@@ -48,6 +48,7 @@ struct bus_type; · 47 unmodified lines
4848 struct device;
4949 struct iommu_domain;
5050 struct notifier_block;
51+struct iommu_sva;
5152
5253 /* iommu fault flags */
5354 #define IOMMU_FAULT_READ 0x0
@@ -55,6 +56,8 @@ struct notifier_block; · 1 unmodified line
5556
5657 typedef int (*iommu_fault_handler_t)(struct iommu_domain *,
5758 struct device *, unsigned long, int, void *);
59+typedef int (*iommu_mm_exit_handler_t)(struct device *dev, struct iommu_sva *,
60+ void *);
5861
5962 struct iommu_domain_geometry {
6063 dma_addr_t aperture_start; /* First address that can be mapped */
@@ -159,6 +162,28 @@ struct iommu_resv_region { · 98 unmodified lines
159162 /* Per device IOMMU features */
160163 enum iommu_dev_features {
161164 IOMMU_DEV_FEAT_AUX, /* Aux-domain feature */
165+ IOMMU_DEV_FEAT_SVA, /* Shared Virtual Addresses */
166+};
167+
168+#define IOMMU_PASID_INVALID (-1U)
169+
170+/**
171+ * struct iommu_sva_ops - device driver callbacks for an SVA context
172+ *
173+ * @mm_exit: called when the mm is about to be torn down by exit_mmap. After
174+ * @mm_exit returns, the device must not issue any more transaction
175+ * with the PASID given as argument.
176+ *
177+ * The @mm_exit handler is allowed to sleep. Be careful about the
178+ * locks taken in @mm_exit, because they might lead to deadlocks if
179+ * they are also held when dropping references to the mm. Consider the
180+ * following call chain:
181+ * mutex_lock(A); mmput(mm) -> exit_mm() -> @mm_exit() -> mutex_lock(A)
182+ * Using mmput_async() prevents this scenario.
183+ *
184+ */
185+struct iommu_sva_ops {
186+ iommu_mm_exit_handler_t mm_exit;
162187 };
163188
164189 #ifdef CONFIG_IOMMU_API
@@ -196,6 +221,9 @@ enum iommu_dev_features { · 31 unmodified lines
196221 * @dev_feat_enabled: check enabled feature
197222 * @aux_attach/detach_dev: aux-domain specific attach/detach entries.
198223 * @aux_get_pasid: get the pasid given an aux-domain
224+ * @sva_bind: Bind process address space to device
225+ * @sva_unbind: Unbind process address space from device
226+ * @sva_get_pasid: Get PASID associated to a SVA handle
199227 * @pgsize_bitmap: bitmap of all possible supported page sizes
200228 */
201229 struct iommu_ops {
@@ -251,6 +279,11 @@ struct iommu_ops { · 49 unmodified lines
251279 void (*aux_detach_dev)(struct iommu_domain *domain, struct device *dev);
252280 int (*aux_get_pasid)(struct iommu_domain *domain, struct device *dev);
253281
282+ struct iommu_sva *(*sva_bind)(struct device *dev, struct mm_struct *mm,
283+ void *drvdata);
284+ void (*sva_unbind)(struct iommu_sva *handle);
285+ int (*sva_get_pasid)(struct iommu_sva *handle);
286+
254287 unsigned long pgsize_bitmap;
255288 };
256289
@@ -417,6 +450,14 @@ struct iommu_fwspec { · 160 unmodified lines
417450 u32 ids[1];
418451 };
419452
453+/**
454+ * struct iommu_sva - handle to a device-mm bond
455+ */
456+struct iommu_sva {
457+ struct device *dev;
458+ const struct iommu_sva_ops *ops;
459+};
460+
420461 int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
421462 const struct iommu_ops *ops);
422463 void iommu_fwspec_free(struct device *dev);
@@ -445,6 +486,14 @@ int iommu_aux_attach_device(struct iommu_domain *domain, struct device *dev); · 22 unmodified lines
445486 void iommu_aux_detach_device(struct iommu_domain *domain, struct device *dev);
446487 int iommu_aux_get_pasid(struct iommu_domain *domain, struct device *dev);
447488
489+struct iommu_sva *iommu_sva_bind_device(struct device *dev,
490+ struct mm_struct *mm,
491+ void *drvdata);
492+void iommu_sva_unbind_device(struct iommu_sva *handle);
493+int iommu_sva_set_ops(struct iommu_sva *handle,
494+ const struct iommu_sva_ops *ops);
495+int iommu_sva_get_pasid(struct iommu_sva *handle);
496+
448497 #else /* CONFIG_IOMMU_API */
449498
450499 struct iommu_ops {};
@@ -770,6 +819,27 @@ iommu_aux_get_pasid(struct iommu_domain *domain, struct device *dev) · 319 unmodified lines
770819 return -ENODEV;
771820 }
772821
822+static inline struct iommu_sva *
823+iommu_sva_bind_device(struct device *dev, struct mm_struct *mm, void *drvdata)
824+{
825+ return NULL;
826+}
827+
828+static inline void iommu_sva_unbind_device(struct iommu_sva *handle)
829+{
830+}
831+
832+static inline int iommu_sva_set_ops(struct iommu_sva *handle,
833+ const struct iommu_sva_ops *ops)
834+{
835+ return -EINVAL;
836+}
837+
838+static inline int iommu_sva_get_pasid(struct iommu_sva *handle)
839+{
840+ return IOMMU_PASID_INVALID;
841+}
842+
773843 #endif /* CONFIG_IOMMU_API */
774844
775845 #ifdef CONFIG_IOMMU_DEBUGFS

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t