context.c 7.0 KB

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