context.c 5.6 KB

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