context.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. // SPDX-License-Identifier: GPL-2.0+
  2. // Copyright 2017 IBM Corp.
  3. #include <linux/sched/mm.h>
  4. #include "trace.h"
  5. #include "ocxl_internal.h"
  6. int ocxl_context_alloc(struct ocxl_context **context, struct ocxl_afu *afu,
  7. struct address_space *mapping)
  8. {
  9. int pasid;
  10. struct ocxl_context *ctx;
  11. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  12. if (!ctx)
  13. return -ENOMEM;
  14. ctx->afu = afu;
  15. mutex_lock(&afu->contexts_lock);
  16. pasid = idr_alloc(&afu->contexts_idr, ctx, afu->pasid_base,
  17. afu->pasid_base + afu->pasid_max, GFP_KERNEL);
  18. if (pasid < 0) {
  19. mutex_unlock(&afu->contexts_lock);
  20. kfree(ctx);
  21. return pasid;
  22. }
  23. afu->pasid_count++;
  24. mutex_unlock(&afu->contexts_lock);
  25. ctx->pasid = pasid;
  26. ctx->status = OPENED;
  27. mutex_init(&ctx->status_mutex);
  28. ctx->mapping = mapping;
  29. mutex_init(&ctx->mapping_lock);
  30. init_waitqueue_head(&ctx->events_wq);
  31. mutex_init(&ctx->xsl_error_lock);
  32. mutex_init(&ctx->irq_lock);
  33. idr_init(&ctx->irq_idr);
  34. ctx->tidr = 0;
  35. /*
  36. * Keep a reference on the AFU to make sure it's valid for the
  37. * duration of the life of the context
  38. */
  39. ocxl_afu_get(afu);
  40. *context = ctx;
  41. return 0;
  42. }
  43. EXPORT_SYMBOL_GPL(ocxl_context_alloc);
  44. /*
  45. * Callback for when a translation fault triggers an error
  46. * data: a pointer to the context which triggered the fault
  47. * addr: the address that triggered the error
  48. * dsisr: the value of the PPC64 dsisr register
  49. */
  50. static void xsl_fault_error(void *data, u64 addr, u64 dsisr)
  51. {
  52. struct ocxl_context *ctx = (struct ocxl_context *) data;
  53. mutex_lock(&ctx->xsl_error_lock);
  54. ctx->xsl_error.addr = addr;
  55. ctx->xsl_error.dsisr = dsisr;
  56. ctx->xsl_error.count++;
  57. mutex_unlock(&ctx->xsl_error_lock);
  58. wake_up_all(&ctx->events_wq);
  59. }
  60. int ocxl_context_attach(struct ocxl_context *ctx, u64 amr, struct mm_struct *mm)
  61. {
  62. int rc;
  63. unsigned long pidr = 0;
  64. struct pci_dev *dev;
  65. // Locks both status & tidr
  66. mutex_lock(&ctx->status_mutex);
  67. if (ctx->status != OPENED) {
  68. rc = -EIO;
  69. goto out;
  70. }
  71. if (mm)
  72. pidr = mm->context.id;
  73. dev = to_pci_dev(ctx->afu->fn->dev.parent);
  74. rc = ocxl_link_add_pe(ctx->afu->fn->link, ctx->pasid, pidr, ctx->tidr,
  75. amr, pci_dev_id(dev), mm, xsl_fault_error, ctx);
  76. if (rc)
  77. goto out;
  78. ctx->status = ATTACHED;
  79. out:
  80. mutex_unlock(&ctx->status_mutex);
  81. return rc;
  82. }
  83. EXPORT_SYMBOL_GPL(ocxl_context_attach);
  84. static vm_fault_t map_afu_irq(struct vm_area_struct *vma, unsigned long address,
  85. u64 offset, struct ocxl_context *ctx)
  86. {
  87. u64 trigger_addr;
  88. int irq_id = ocxl_irq_offset_to_id(ctx, offset);
  89. trigger_addr = ocxl_afu_irq_get_addr(ctx, irq_id);
  90. if (!trigger_addr)
  91. return VM_FAULT_SIGBUS;
  92. return vmf_insert_pfn(vma, address, trigger_addr >> PAGE_SHIFT);
  93. }
  94. static vm_fault_t map_pp_mmio(struct vm_area_struct *vma, unsigned long address,
  95. u64 offset, struct ocxl_context *ctx)
  96. {
  97. u64 pp_mmio_addr;
  98. int pasid_off;
  99. vm_fault_t ret;
  100. if (offset >= ctx->afu->config.pp_mmio_stride)
  101. return VM_FAULT_SIGBUS;
  102. mutex_lock(&ctx->status_mutex);
  103. if (ctx->status != ATTACHED) {
  104. mutex_unlock(&ctx->status_mutex);
  105. pr_debug("%s: Context not attached, failing mmio mmap\n",
  106. __func__);
  107. return VM_FAULT_SIGBUS;
  108. }
  109. pasid_off = ctx->pasid - ctx->afu->pasid_base;
  110. pp_mmio_addr = ctx->afu->pp_mmio_start +
  111. pasid_off * ctx->afu->config.pp_mmio_stride +
  112. offset;
  113. ret = vmf_insert_pfn(vma, address, pp_mmio_addr >> PAGE_SHIFT);
  114. mutex_unlock(&ctx->status_mutex);
  115. return ret;
  116. }
  117. static vm_fault_t ocxl_mmap_fault(struct vm_fault *vmf)
  118. {
  119. struct vm_area_struct *vma = vmf->vma;
  120. struct ocxl_context *ctx = vma->vm_file->private_data;
  121. u64 offset;
  122. vm_fault_t ret;
  123. offset = vmf->pgoff << PAGE_SHIFT;
  124. pr_debug("%s: pasid %d address 0x%lx offset 0x%llx\n", __func__,
  125. ctx->pasid, vmf->address, offset);
  126. if (offset < ctx->afu->irq_base_offset)
  127. ret = map_pp_mmio(vma, vmf->address, offset, ctx);
  128. else
  129. ret = map_afu_irq(vma, vmf->address, offset, ctx);
  130. return ret;
  131. }
  132. static const struct vm_operations_struct ocxl_vmops = {
  133. .fault = ocxl_mmap_fault,
  134. };
  135. static int check_mmap_afu_irq(struct ocxl_context *ctx,
  136. struct vm_area_struct *vma)
  137. {
  138. int irq_id = ocxl_irq_offset_to_id(ctx, vma->vm_pgoff << PAGE_SHIFT);
  139. /* only one page */
  140. if (vma_pages(vma) != 1)
  141. return -EINVAL;
  142. /* check offset validty */
  143. if (!ocxl_afu_irq_get_addr(ctx, irq_id))
  144. return -EINVAL;
  145. /*
  146. * trigger page should only be accessible in write mode.
  147. *
  148. * It's a bit theoretical, as a page mmaped with only
  149. * PROT_WRITE is currently readable, but it doesn't hurt.
  150. */
  151. if ((vma->vm_flags & VM_READ) || (vma->vm_flags & VM_EXEC) ||
  152. !(vma->vm_flags & VM_WRITE))
  153. return -EINVAL;
  154. vm_flags_clear(vma, VM_MAYREAD | VM_MAYEXEC);
  155. return 0;
  156. }
  157. static int check_mmap_mmio(struct ocxl_context *ctx,
  158. struct vm_area_struct *vma)
  159. {
  160. if ((vma_pages(vma) + vma->vm_pgoff) >
  161. (ctx->afu->config.pp_mmio_stride >> PAGE_SHIFT))
  162. return -EINVAL;
  163. return 0;
  164. }
  165. int ocxl_context_mmap(struct ocxl_context *ctx, struct vm_area_struct *vma)
  166. {
  167. int rc;
  168. if ((vma->vm_pgoff << PAGE_SHIFT) < ctx->afu->irq_base_offset)
  169. rc = check_mmap_mmio(ctx, vma);
  170. else
  171. rc = check_mmap_afu_irq(ctx, vma);
  172. if (rc)
  173. return rc;
  174. vm_flags_set(vma, VM_IO | VM_PFNMAP);
  175. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  176. vma->vm_ops = &ocxl_vmops;
  177. return 0;
  178. }
  179. int ocxl_context_detach(struct ocxl_context *ctx)
  180. {
  181. struct pci_dev *dev;
  182. int afu_control_pos;
  183. enum ocxl_context_status status;
  184. int rc;
  185. mutex_lock(&ctx->status_mutex);
  186. status = ctx->status;
  187. ctx->status = CLOSED;
  188. mutex_unlock(&ctx->status_mutex);
  189. if (status != ATTACHED)
  190. return 0;
  191. dev = to_pci_dev(ctx->afu->fn->dev.parent);
  192. afu_control_pos = ctx->afu->config.dvsec_afu_control_pos;
  193. mutex_lock(&ctx->afu->afu_control_lock);
  194. rc = ocxl_config_terminate_pasid(dev, afu_control_pos, ctx->pasid);
  195. mutex_unlock(&ctx->afu->afu_control_lock);
  196. trace_ocxl_terminate_pasid(ctx->pasid, rc);
  197. if (rc) {
  198. /*
  199. * If we timeout waiting for the AFU to terminate the
  200. * pasid, then it's dangerous to clean up the Process
  201. * Element entry in the SPA, as it may be referenced
  202. * in the future by the AFU. In which case, we would
  203. * checkstop because of an invalid PE access (FIR
  204. * register 2, bit 42). So leave the PE
  205. * defined. Caller shouldn't free the context so that
  206. * PASID remains allocated.
  207. *
  208. * A link reset will be required to cleanup the AFU
  209. * and the SPA.
  210. */
  211. if (rc == -EBUSY)
  212. return rc;
  213. }
  214. rc = ocxl_link_remove_pe(ctx->afu->fn->link, ctx->pasid);
  215. if (rc) {
  216. dev_warn(&dev->dev,
  217. "Couldn't remove PE entry cleanly: %d\n", rc);
  218. }
  219. return 0;
  220. }
  221. EXPORT_SYMBOL_GPL(ocxl_context_detach);
  222. void ocxl_context_detach_all(struct ocxl_afu *afu)
  223. {
  224. struct ocxl_context *ctx;
  225. int tmp;
  226. mutex_lock(&afu->contexts_lock);
  227. idr_for_each_entry(&afu->contexts_idr, ctx, tmp) {
  228. ocxl_context_detach(ctx);
  229. /*
  230. * We are force detaching - remove any active mmio
  231. * mappings so userspace cannot interfere with the
  232. * card if it comes back. Easiest way to exercise
  233. * this is to unbind and rebind the driver via sysfs
  234. * while it is in use.
  235. */
  236. mutex_lock(&ctx->mapping_lock);
  237. if (ctx->mapping)
  238. unmap_mapping_range(ctx->mapping, 0, 0, 1);
  239. mutex_unlock(&ctx->mapping_lock);
  240. }
  241. mutex_unlock(&afu->contexts_lock);
  242. }
  243. void ocxl_context_free(struct ocxl_context *ctx)
  244. {
  245. mutex_lock(&ctx->afu->contexts_lock);
  246. ctx->afu->pasid_count--;
  247. idr_remove(&ctx->afu->contexts_idr, ctx->pasid);
  248. mutex_unlock(&ctx->afu->contexts_lock);
  249. ocxl_afu_irq_free_all(ctx);
  250. idr_destroy(&ctx->irq_idr);
  251. /* reference to the AFU taken in ocxl_context_alloc() */
  252. ocxl_afu_put(ctx->afu);
  253. kfree(ctx);
  254. }
  255. EXPORT_SYMBOL_GPL(ocxl_context_free);