1 // SPDX-License-Identifier: GPL-2.0
3 * Helpers for IOMMU drivers implementing SVA
5 #include <linux/mmu_context.h>
6 #include <linux/mutex.h>
7 #include <linux/sched/mm.h>
8 #include <linux/iommu.h>
10 #include "iommu-sva.h"
12 static DEFINE_MUTEX(iommu_sva_lock);
13 static DEFINE_IDA(iommu_global_pasid_ida);
15 /* Allocate a PASID for the mm within range (inclusive) */
16 static int iommu_sva_alloc_pasid(struct mm_struct *mm, ioasid_t min, ioasid_t max)
20 if (min == IOMMU_PASID_INVALID ||
21 max == IOMMU_PASID_INVALID ||
22 min == 0 || max < min)
25 if (!arch_pgtable_dma_compat(mm))
28 mutex_lock(&iommu_sva_lock);
29 /* Is a PASID already associated with this mm? */
30 if (mm_valid_pasid(mm)) {
31 if (mm->pasid < min || mm->pasid > max)
36 ret = ida_alloc_range(&iommu_global_pasid_ida, min, max, GFP_KERNEL);
43 mutex_unlock(&iommu_sva_lock);
48 * iommu_sva_bind_device() - Bind a process address space to a device
50 * @mm: the mm to bind, caller must hold a reference to mm_users
52 * Create a bond between device and address space, allowing the device to
53 * access the mm using the PASID returned by iommu_sva_get_pasid(). If a
54 * bond already exists between @device and @mm, an additional internal
55 * reference is taken. Caller must call iommu_sva_unbind_device()
56 * to release each reference.
58 * iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA) must be called first, to
59 * initialize the required SVA features.
61 * On error, returns an ERR_PTR value.
63 struct iommu_sva *iommu_sva_bind_device(struct device *dev, struct mm_struct *mm)
65 struct iommu_domain *domain;
66 struct iommu_sva *handle;
70 max_pasids = dev->iommu->max_pasids;
72 return ERR_PTR(-EOPNOTSUPP);
74 /* Allocate mm->pasid if necessary. */
75 ret = iommu_sva_alloc_pasid(mm, 1, max_pasids - 1);
79 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
81 return ERR_PTR(-ENOMEM);
83 mutex_lock(&iommu_sva_lock);
84 /* Search for an existing domain. */
85 domain = iommu_get_domain_for_dev_pasid(dev, mm->pasid,
88 ret = PTR_ERR(domain);
97 /* Allocate a new domain and set it on device pasid. */
98 domain = iommu_sva_domain_alloc(dev, mm);
104 ret = iommu_attach_device_pasid(domain, dev, mm->pasid);
106 goto out_free_domain;
109 mutex_unlock(&iommu_sva_lock);
111 handle->domain = domain;
116 iommu_domain_free(domain);
118 mutex_unlock(&iommu_sva_lock);
123 EXPORT_SYMBOL_GPL(iommu_sva_bind_device);
126 * iommu_sva_unbind_device() - Remove a bond created with iommu_sva_bind_device
127 * @handle: the handle returned by iommu_sva_bind_device()
129 * Put reference to a bond between device and address space. The device should
130 * not be issuing any more transaction for this PASID. All outstanding page
131 * requests for this PASID must have been flushed to the IOMMU.
133 void iommu_sva_unbind_device(struct iommu_sva *handle)
135 struct iommu_domain *domain = handle->domain;
136 ioasid_t pasid = domain->mm->pasid;
137 struct device *dev = handle->dev;
139 mutex_lock(&iommu_sva_lock);
140 if (--domain->users == 0) {
141 iommu_detach_device_pasid(domain, dev, pasid);
142 iommu_domain_free(domain);
144 mutex_unlock(&iommu_sva_lock);
147 EXPORT_SYMBOL_GPL(iommu_sva_unbind_device);
149 u32 iommu_sva_get_pasid(struct iommu_sva *handle)
151 struct iommu_domain *domain = handle->domain;
153 return domain->mm->pasid;
155 EXPORT_SYMBOL_GPL(iommu_sva_get_pasid);
158 * I/O page fault handler for SVA
160 enum iommu_page_response_code
161 iommu_sva_handle_iopf(struct iommu_fault *fault, void *data)
164 struct vm_area_struct *vma;
165 struct mm_struct *mm = data;
166 unsigned int access_flags = 0;
167 unsigned int fault_flags = FAULT_FLAG_REMOTE;
168 struct iommu_fault_page_request *prm = &fault->prm;
169 enum iommu_page_response_code status = IOMMU_PAGE_RESP_INVALID;
171 if (!(prm->flags & IOMMU_FAULT_PAGE_REQUEST_PASID_VALID))
174 if (!mmget_not_zero(mm))
179 vma = vma_lookup(mm, prm->addr);
184 if (prm->perm & IOMMU_FAULT_PERM_READ)
185 access_flags |= VM_READ;
187 if (prm->perm & IOMMU_FAULT_PERM_WRITE) {
188 access_flags |= VM_WRITE;
189 fault_flags |= FAULT_FLAG_WRITE;
192 if (prm->perm & IOMMU_FAULT_PERM_EXEC) {
193 access_flags |= VM_EXEC;
194 fault_flags |= FAULT_FLAG_INSTRUCTION;
197 if (!(prm->perm & IOMMU_FAULT_PERM_PRIV))
198 fault_flags |= FAULT_FLAG_USER;
200 if (access_flags & ~vma->vm_flags)
204 ret = handle_mm_fault(vma, prm->addr, fault_flags, NULL);
205 status = ret & VM_FAULT_ERROR ? IOMMU_PAGE_RESP_INVALID :
206 IOMMU_PAGE_RESP_SUCCESS;
209 mmap_read_unlock(mm);
215 void mm_pasid_drop(struct mm_struct *mm)
217 if (likely(!mm_valid_pasid(mm)))
220 ida_free(&iommu_global_pasid_ida, mm->pasid);