1 // SPDX-License-Identifier: GPL-2.0
3 * Helpers for IOMMU drivers implementing SVA
5 #include <linux/mutex.h>
6 #include <linux/sched/mm.h>
7 #include <linux/iommu.h>
11 static DEFINE_MUTEX(iommu_sva_lock);
12 static DECLARE_IOASID_SET(iommu_sva_pasid);
15 * iommu_sva_alloc_pasid - Allocate a PASID for the mm
17 * @min: minimum PASID value (inclusive)
18 * @max: maximum PASID value (inclusive)
20 * Try to allocate a PASID for this mm, or take a reference to the existing one
21 * provided it fits within the [@min, @max] range. On success the PASID is
22 * available in mm->pasid and will be available for the lifetime of the mm.
24 * Returns 0 on success and < 0 on error.
26 int iommu_sva_alloc_pasid(struct mm_struct *mm, ioasid_t min, ioasid_t max)
31 if (min == INVALID_IOASID || max == INVALID_IOASID ||
32 min == 0 || max < min)
35 mutex_lock(&iommu_sva_lock);
36 /* Is a PASID already associated with this mm? */
37 if (pasid_valid(mm->pasid)) {
38 if (mm->pasid < min || mm->pasid >= max)
43 pasid = ioasid_alloc(&iommu_sva_pasid, min, max, mm);
44 if (!pasid_valid(pasid))
47 mm_pasid_set(mm, pasid);
49 mutex_unlock(&iommu_sva_lock);
52 EXPORT_SYMBOL_GPL(iommu_sva_alloc_pasid);
54 /* ioasid_find getter() requires a void * argument */
55 static bool __mmget_not_zero(void *mm)
57 return mmget_not_zero(mm);
61 * iommu_sva_find() - Find mm associated to the given PASID
62 * @pasid: Process Address Space ID assigned to the mm
64 * On success a reference to the mm is taken, and must be released with mmput().
66 * Returns the mm corresponding to this PASID, or an error if not found.
68 struct mm_struct *iommu_sva_find(ioasid_t pasid)
70 return ioasid_find(&iommu_sva_pasid, pasid, __mmget_not_zero);
72 EXPORT_SYMBOL_GPL(iommu_sva_find);
75 * iommu_sva_bind_device() - Bind a process address space to a device
77 * @mm: the mm to bind, caller must hold a reference to mm_users
79 * Create a bond between device and address space, allowing the device to
80 * access the mm using the PASID returned by iommu_sva_get_pasid(). If a
81 * bond already exists between @device and @mm, an additional internal
82 * reference is taken. Caller must call iommu_sva_unbind_device()
83 * to release each reference.
85 * iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA) must be called first, to
86 * initialize the required SVA features.
88 * On error, returns an ERR_PTR value.
90 struct iommu_sva *iommu_sva_bind_device(struct device *dev, struct mm_struct *mm)
92 struct iommu_domain *domain;
93 struct iommu_sva *handle;
97 max_pasids = dev->iommu->max_pasids;
99 return ERR_PTR(-EOPNOTSUPP);
101 /* Allocate mm->pasid if necessary. */
102 ret = iommu_sva_alloc_pasid(mm, 1, max_pasids - 1);
106 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
108 return ERR_PTR(-ENOMEM);
110 mutex_lock(&iommu_sva_lock);
111 /* Search for an existing domain. */
112 domain = iommu_get_domain_for_dev_pasid(dev, mm->pasid,
114 if (IS_ERR(domain)) {
115 ret = PTR_ERR(domain);
124 /* Allocate a new domain and set it on device pasid. */
125 domain = iommu_sva_domain_alloc(dev, mm);
131 ret = iommu_attach_device_pasid(domain, dev, mm->pasid);
133 goto out_free_domain;
136 mutex_unlock(&iommu_sva_lock);
138 handle->domain = domain;
143 iommu_domain_free(domain);
145 mutex_unlock(&iommu_sva_lock);
150 EXPORT_SYMBOL_GPL(iommu_sva_bind_device);
153 * iommu_sva_unbind_device() - Remove a bond created with iommu_sva_bind_device
154 * @handle: the handle returned by iommu_sva_bind_device()
156 * Put reference to a bond between device and address space. The device should
157 * not be issuing any more transaction for this PASID. All outstanding page
158 * requests for this PASID must have been flushed to the IOMMU.
160 void iommu_sva_unbind_device(struct iommu_sva *handle)
162 struct iommu_domain *domain = handle->domain;
163 ioasid_t pasid = domain->mm->pasid;
164 struct device *dev = handle->dev;
166 mutex_lock(&iommu_sva_lock);
167 if (--domain->users == 0) {
168 iommu_detach_device_pasid(domain, dev, pasid);
169 iommu_domain_free(domain);
171 mutex_unlock(&iommu_sva_lock);
174 EXPORT_SYMBOL_GPL(iommu_sva_unbind_device);
176 u32 iommu_sva_get_pasid(struct iommu_sva *handle)
178 struct iommu_domain *domain = handle->domain;
180 return domain->mm->pasid;
182 EXPORT_SYMBOL_GPL(iommu_sva_get_pasid);
185 * I/O page fault handler for SVA
187 enum iommu_page_response_code
188 iommu_sva_handle_iopf(struct iommu_fault *fault, void *data)
191 struct vm_area_struct *vma;
192 struct mm_struct *mm = data;
193 unsigned int access_flags = 0;
194 unsigned int fault_flags = FAULT_FLAG_REMOTE;
195 struct iommu_fault_page_request *prm = &fault->prm;
196 enum iommu_page_response_code status = IOMMU_PAGE_RESP_INVALID;
198 if (!(prm->flags & IOMMU_FAULT_PAGE_REQUEST_PASID_VALID))
201 if (!mmget_not_zero(mm))
206 vma = find_extend_vma(mm, prm->addr);
211 if (prm->perm & IOMMU_FAULT_PERM_READ)
212 access_flags |= VM_READ;
214 if (prm->perm & IOMMU_FAULT_PERM_WRITE) {
215 access_flags |= VM_WRITE;
216 fault_flags |= FAULT_FLAG_WRITE;
219 if (prm->perm & IOMMU_FAULT_PERM_EXEC) {
220 access_flags |= VM_EXEC;
221 fault_flags |= FAULT_FLAG_INSTRUCTION;
224 if (!(prm->perm & IOMMU_FAULT_PERM_PRIV))
225 fault_flags |= FAULT_FLAG_USER;
227 if (access_flags & ~vma->vm_flags)
231 ret = handle_mm_fault(vma, prm->addr, fault_flags, NULL);
232 status = ret & VM_FAULT_ERROR ? IOMMU_PAGE_RESP_INVALID :
233 IOMMU_PAGE_RESP_SUCCESS;
236 mmap_read_unlock(mm);