iommu-sva.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Helpers for IOMMU drivers implementing SVA
  4. */
  5. #include <linux/mmu_context.h>
  6. #include <linux/mutex.h>
  7. #include <linux/sched/mm.h>
  8. #include <linux/iommu.h>
  9. #include "iommu-priv.h"
  10. static DEFINE_MUTEX(iommu_sva_lock);
  11. static struct iommu_domain *iommu_sva_domain_alloc(struct device *dev,
  12. struct mm_struct *mm);
  13. /* Allocate a PASID for the mm within range (inclusive) */
  14. static struct iommu_mm_data *iommu_alloc_mm_data(struct mm_struct *mm, struct device *dev)
  15. {
  16. struct iommu_mm_data *iommu_mm;
  17. ioasid_t pasid;
  18. lockdep_assert_held(&iommu_sva_lock);
  19. if (!arch_pgtable_dma_compat(mm))
  20. return ERR_PTR(-EBUSY);
  21. iommu_mm = mm->iommu_mm;
  22. /* Is a PASID already associated with this mm? */
  23. if (iommu_mm) {
  24. if (iommu_mm->pasid >= dev->iommu->max_pasids)
  25. return ERR_PTR(-EOVERFLOW);
  26. return iommu_mm;
  27. }
  28. iommu_mm = kzalloc(sizeof(struct iommu_mm_data), GFP_KERNEL);
  29. if (!iommu_mm)
  30. return ERR_PTR(-ENOMEM);
  31. pasid = iommu_alloc_global_pasid(dev);
  32. if (pasid == IOMMU_PASID_INVALID) {
  33. kfree(iommu_mm);
  34. return ERR_PTR(-ENOSPC);
  35. }
  36. iommu_mm->pasid = pasid;
  37. INIT_LIST_HEAD(&iommu_mm->sva_domains);
  38. /*
  39. * Make sure the write to mm->iommu_mm is not reordered in front of
  40. * initialization to iommu_mm fields. If it does, readers may see a
  41. * valid iommu_mm with uninitialized values.
  42. */
  43. smp_store_release(&mm->iommu_mm, iommu_mm);
  44. return iommu_mm;
  45. }
  46. /**
  47. * iommu_sva_bind_device() - Bind a process address space to a device
  48. * @dev: the device
  49. * @mm: the mm to bind, caller must hold a reference to mm_users
  50. *
  51. * Create a bond between device and address space, allowing the device to
  52. * access the mm using the PASID returned by iommu_sva_get_pasid(). If a
  53. * bond already exists between @device and @mm, an additional internal
  54. * reference is taken. Caller must call iommu_sva_unbind_device()
  55. * to release each reference.
  56. *
  57. * iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA) must be called first, to
  58. * initialize the required SVA features.
  59. *
  60. * On error, returns an ERR_PTR value.
  61. */
  62. struct iommu_sva *iommu_sva_bind_device(struct device *dev, struct mm_struct *mm)
  63. {
  64. struct iommu_group *group = dev->iommu_group;
  65. struct iommu_attach_handle *attach_handle;
  66. struct iommu_mm_data *iommu_mm;
  67. struct iommu_domain *domain;
  68. struct iommu_sva *handle;
  69. int ret;
  70. if (!group)
  71. return ERR_PTR(-ENODEV);
  72. mutex_lock(&iommu_sva_lock);
  73. /* Allocate mm->pasid if necessary. */
  74. iommu_mm = iommu_alloc_mm_data(mm, dev);
  75. if (IS_ERR(iommu_mm)) {
  76. ret = PTR_ERR(iommu_mm);
  77. goto out_unlock;
  78. }
  79. /* A bond already exists, just take a reference`. */
  80. attach_handle = iommu_attach_handle_get(group, iommu_mm->pasid, IOMMU_DOMAIN_SVA);
  81. if (!IS_ERR(attach_handle)) {
  82. handle = container_of(attach_handle, struct iommu_sva, handle);
  83. if (attach_handle->domain->mm != mm) {
  84. ret = -EBUSY;
  85. goto out_unlock;
  86. }
  87. refcount_inc(&handle->users);
  88. mutex_unlock(&iommu_sva_lock);
  89. return handle;
  90. }
  91. if (PTR_ERR(attach_handle) != -ENOENT) {
  92. ret = PTR_ERR(attach_handle);
  93. goto out_unlock;
  94. }
  95. handle = kzalloc(sizeof(*handle), GFP_KERNEL);
  96. if (!handle) {
  97. ret = -ENOMEM;
  98. goto out_unlock;
  99. }
  100. /* Search for an existing domain. */
  101. list_for_each_entry(domain, &mm->iommu_mm->sva_domains, next) {
  102. ret = iommu_attach_device_pasid(domain, dev, iommu_mm->pasid,
  103. &handle->handle);
  104. if (!ret) {
  105. domain->users++;
  106. goto out;
  107. }
  108. }
  109. /* Allocate a new domain and set it on device pasid. */
  110. domain = iommu_sva_domain_alloc(dev, mm);
  111. if (IS_ERR(domain)) {
  112. ret = PTR_ERR(domain);
  113. goto out_free_handle;
  114. }
  115. ret = iommu_attach_device_pasid(domain, dev, iommu_mm->pasid,
  116. &handle->handle);
  117. if (ret)
  118. goto out_free_domain;
  119. domain->users = 1;
  120. list_add(&domain->next, &mm->iommu_mm->sva_domains);
  121. out:
  122. refcount_set(&handle->users, 1);
  123. mutex_unlock(&iommu_sva_lock);
  124. handle->dev = dev;
  125. return handle;
  126. out_free_domain:
  127. iommu_domain_free(domain);
  128. out_free_handle:
  129. kfree(handle);
  130. out_unlock:
  131. mutex_unlock(&iommu_sva_lock);
  132. return ERR_PTR(ret);
  133. }
  134. EXPORT_SYMBOL_GPL(iommu_sva_bind_device);
  135. /**
  136. * iommu_sva_unbind_device() - Remove a bond created with iommu_sva_bind_device
  137. * @handle: the handle returned by iommu_sva_bind_device()
  138. *
  139. * Put reference to a bond between device and address space. The device should
  140. * not be issuing any more transaction for this PASID. All outstanding page
  141. * requests for this PASID must have been flushed to the IOMMU.
  142. */
  143. void iommu_sva_unbind_device(struct iommu_sva *handle)
  144. {
  145. struct iommu_domain *domain = handle->handle.domain;
  146. struct iommu_mm_data *iommu_mm = domain->mm->iommu_mm;
  147. struct device *dev = handle->dev;
  148. mutex_lock(&iommu_sva_lock);
  149. if (!refcount_dec_and_test(&handle->users)) {
  150. mutex_unlock(&iommu_sva_lock);
  151. return;
  152. }
  153. iommu_detach_device_pasid(domain, dev, iommu_mm->pasid);
  154. if (--domain->users == 0) {
  155. list_del(&domain->next);
  156. iommu_domain_free(domain);
  157. }
  158. mutex_unlock(&iommu_sva_lock);
  159. kfree(handle);
  160. }
  161. EXPORT_SYMBOL_GPL(iommu_sva_unbind_device);
  162. u32 iommu_sva_get_pasid(struct iommu_sva *handle)
  163. {
  164. struct iommu_domain *domain = handle->handle.domain;
  165. return mm_get_enqcmd_pasid(domain->mm);
  166. }
  167. EXPORT_SYMBOL_GPL(iommu_sva_get_pasid);
  168. void mm_pasid_drop(struct mm_struct *mm)
  169. {
  170. struct iommu_mm_data *iommu_mm = mm->iommu_mm;
  171. if (!iommu_mm)
  172. return;
  173. iommu_free_global_pasid(iommu_mm->pasid);
  174. kfree(iommu_mm);
  175. }
  176. /*
  177. * I/O page fault handler for SVA
  178. */
  179. static enum iommu_page_response_code
  180. iommu_sva_handle_mm(struct iommu_fault *fault, struct mm_struct *mm)
  181. {
  182. vm_fault_t ret;
  183. struct vm_area_struct *vma;
  184. unsigned int access_flags = 0;
  185. unsigned int fault_flags = FAULT_FLAG_REMOTE;
  186. struct iommu_fault_page_request *prm = &fault->prm;
  187. enum iommu_page_response_code status = IOMMU_PAGE_RESP_INVALID;
  188. if (!(prm->flags & IOMMU_FAULT_PAGE_REQUEST_PASID_VALID))
  189. return status;
  190. if (!mmget_not_zero(mm))
  191. return status;
  192. mmap_read_lock(mm);
  193. vma = vma_lookup(mm, prm->addr);
  194. if (!vma)
  195. /* Unmapped area */
  196. goto out_put_mm;
  197. if (prm->perm & IOMMU_FAULT_PERM_READ)
  198. access_flags |= VM_READ;
  199. if (prm->perm & IOMMU_FAULT_PERM_WRITE) {
  200. access_flags |= VM_WRITE;
  201. fault_flags |= FAULT_FLAG_WRITE;
  202. }
  203. if (prm->perm & IOMMU_FAULT_PERM_EXEC) {
  204. access_flags |= VM_EXEC;
  205. fault_flags |= FAULT_FLAG_INSTRUCTION;
  206. }
  207. if (!(prm->perm & IOMMU_FAULT_PERM_PRIV))
  208. fault_flags |= FAULT_FLAG_USER;
  209. if (access_flags & ~vma->vm_flags)
  210. /* Access fault */
  211. goto out_put_mm;
  212. ret = handle_mm_fault(vma, prm->addr, fault_flags, NULL);
  213. status = ret & VM_FAULT_ERROR ? IOMMU_PAGE_RESP_INVALID :
  214. IOMMU_PAGE_RESP_SUCCESS;
  215. out_put_mm:
  216. mmap_read_unlock(mm);
  217. mmput(mm);
  218. return status;
  219. }
  220. static void iommu_sva_handle_iopf(struct work_struct *work)
  221. {
  222. struct iopf_fault *iopf;
  223. struct iopf_group *group;
  224. enum iommu_page_response_code status = IOMMU_PAGE_RESP_SUCCESS;
  225. group = container_of(work, struct iopf_group, work);
  226. list_for_each_entry(iopf, &group->faults, list) {
  227. /*
  228. * For the moment, errors are sticky: don't handle subsequent
  229. * faults in the group if there is an error.
  230. */
  231. if (status != IOMMU_PAGE_RESP_SUCCESS)
  232. break;
  233. status = iommu_sva_handle_mm(&iopf->fault,
  234. group->attach_handle->domain->mm);
  235. }
  236. iopf_group_response(group, status);
  237. iopf_free_group(group);
  238. }
  239. static int iommu_sva_iopf_handler(struct iopf_group *group)
  240. {
  241. struct iommu_fault_param *fault_param = group->fault_param;
  242. INIT_WORK(&group->work, iommu_sva_handle_iopf);
  243. if (!queue_work(fault_param->queue->wq, &group->work))
  244. return -EBUSY;
  245. return 0;
  246. }
  247. static struct iommu_domain *iommu_sva_domain_alloc(struct device *dev,
  248. struct mm_struct *mm)
  249. {
  250. const struct iommu_ops *ops = dev_iommu_ops(dev);
  251. struct iommu_domain *domain;
  252. if (ops->domain_alloc_sva) {
  253. domain = ops->domain_alloc_sva(dev, mm);
  254. if (IS_ERR(domain))
  255. return domain;
  256. } else {
  257. domain = ops->domain_alloc(IOMMU_DOMAIN_SVA);
  258. if (!domain)
  259. return ERR_PTR(-ENOMEM);
  260. }
  261. domain->type = IOMMU_DOMAIN_SVA;
  262. mmgrab(mm);
  263. domain->mm = mm;
  264. domain->owner = ops;
  265. domain->iopf_handler = iommu_sva_iopf_handler;
  266. return domain;
  267. }