context.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Copyright 2014 IBM Corp.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/kernel.h>
  11. #include <linux/bitmap.h>
  12. #include <linux/sched.h>
  13. #include <linux/pid.h>
  14. #include <linux/fs.h>
  15. #include <linux/mm.h>
  16. #include <linux/debugfs.h>
  17. #include <linux/slab.h>
  18. #include <linux/idr.h>
  19. #include <asm/cputable.h>
  20. #include <asm/current.h>
  21. #include <asm/copro.h>
  22. #include "cxl.h"
  23. /*
  24. * Allocates space for a CXL context.
  25. */
  26. struct cxl_context *cxl_context_alloc(void)
  27. {
  28. return kzalloc(sizeof(struct cxl_context), GFP_KERNEL);
  29. }
  30. /*
  31. * Initialises a CXL context.
  32. */
  33. int cxl_context_init(struct cxl_context *ctx, struct cxl_afu *afu, bool master,
  34. struct address_space *mapping)
  35. {
  36. int i;
  37. spin_lock_init(&ctx->sste_lock);
  38. ctx->afu = afu;
  39. ctx->master = master;
  40. ctx->pid = NULL; /* Set in start work ioctl */
  41. mutex_init(&ctx->mapping_lock);
  42. ctx->mapping = mapping;
  43. /*
  44. * Allocate the segment table before we put it in the IDR so that we
  45. * can always access it when dereferenced from IDR. For the same
  46. * reason, the segment table is only destroyed after the context is
  47. * removed from the IDR. Access to this in the IOCTL is protected by
  48. * Linux filesytem symantics (can't IOCTL until open is complete).
  49. */
  50. i = cxl_alloc_sst(ctx);
  51. if (i)
  52. return i;
  53. INIT_WORK(&ctx->fault_work, cxl_handle_fault);
  54. init_waitqueue_head(&ctx->wq);
  55. spin_lock_init(&ctx->lock);
  56. ctx->irq_bitmap = NULL;
  57. ctx->pending_irq = false;
  58. ctx->pending_fault = false;
  59. ctx->pending_afu_err = false;
  60. /*
  61. * When we have to destroy all contexts in cxl_context_detach_all() we
  62. * end up with afu_release_irqs() called from inside a
  63. * idr_for_each_entry(). Hence we need to make sure that anything
  64. * dereferenced from this IDR is ok before we allocate the IDR here.
  65. * This clears out the IRQ ranges to ensure this.
  66. */
  67. for (i = 0; i < CXL_IRQ_RANGES; i++)
  68. ctx->irqs.range[i] = 0;
  69. mutex_init(&ctx->status_mutex);
  70. ctx->status = OPENED;
  71. /*
  72. * Allocating IDR! We better make sure everything's setup that
  73. * dereferences from it.
  74. */
  75. mutex_lock(&afu->contexts_lock);
  76. idr_preload(GFP_KERNEL);
  77. i = idr_alloc(&ctx->afu->contexts_idr, ctx, 0,
  78. ctx->afu->num_procs, GFP_NOWAIT);
  79. idr_preload_end();
  80. mutex_unlock(&afu->contexts_lock);
  81. if (i < 0)
  82. return i;
  83. ctx->pe = i;
  84. ctx->elem = &ctx->afu->spa[i];
  85. ctx->pe_inserted = false;
  86. return 0;
  87. }
  88. static int cxl_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  89. {
  90. struct cxl_context *ctx = vma->vm_file->private_data;
  91. unsigned long address = (unsigned long)vmf->virtual_address;
  92. u64 area, offset;
  93. offset = vmf->pgoff << PAGE_SHIFT;
  94. pr_devel("%s: pe: %i address: 0x%lx offset: 0x%llx\n",
  95. __func__, ctx->pe, address, offset);
  96. if (ctx->afu->current_mode == CXL_MODE_DEDICATED) {
  97. area = ctx->afu->psn_phys;
  98. if (offset > ctx->afu->adapter->ps_size)
  99. return VM_FAULT_SIGBUS;
  100. } else {
  101. area = ctx->psn_phys;
  102. if (offset > ctx->psn_size)
  103. return VM_FAULT_SIGBUS;
  104. }
  105. mutex_lock(&ctx->status_mutex);
  106. if (ctx->status != STARTED) {
  107. mutex_unlock(&ctx->status_mutex);
  108. pr_devel("%s: Context not started, failing problem state access\n", __func__);
  109. return VM_FAULT_SIGBUS;
  110. }
  111. vm_insert_pfn(vma, address, (area + offset) >> PAGE_SHIFT);
  112. mutex_unlock(&ctx->status_mutex);
  113. return VM_FAULT_NOPAGE;
  114. }
  115. static const struct vm_operations_struct cxl_mmap_vmops = {
  116. .fault = cxl_mmap_fault,
  117. };
  118. /*
  119. * Map a per-context mmio space into the given vma.
  120. */
  121. int cxl_context_iomap(struct cxl_context *ctx, struct vm_area_struct *vma)
  122. {
  123. u64 len = vma->vm_end - vma->vm_start;
  124. len = min(len, ctx->psn_size);
  125. if (ctx->afu->current_mode != CXL_MODE_DEDICATED) {
  126. /* make sure there is a valid per process space for this AFU */
  127. if ((ctx->master && !ctx->afu->psa) || (!ctx->afu->pp_psa)) {
  128. pr_devel("AFU doesn't support mmio space\n");
  129. return -EINVAL;
  130. }
  131. /* Can't mmap until the AFU is enabled */
  132. if (!ctx->afu->enabled)
  133. return -EBUSY;
  134. }
  135. pr_devel("%s: mmio physical: %llx pe: %i master:%i\n", __func__,
  136. ctx->psn_phys, ctx->pe , ctx->master);
  137. vma->vm_flags |= VM_IO | VM_PFNMAP;
  138. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  139. vma->vm_ops = &cxl_mmap_vmops;
  140. return 0;
  141. }
  142. /*
  143. * Detach a context from the hardware. This disables interrupts and doesn't
  144. * return until all outstanding interrupts for this context have completed. The
  145. * hardware should no longer access *ctx after this has returned.
  146. */
  147. int __detach_context(struct cxl_context *ctx)
  148. {
  149. enum cxl_context_status status;
  150. mutex_lock(&ctx->status_mutex);
  151. status = ctx->status;
  152. ctx->status = CLOSED;
  153. mutex_unlock(&ctx->status_mutex);
  154. if (status != STARTED)
  155. return -EBUSY;
  156. WARN_ON(cxl_detach_process(ctx));
  157. flush_work(&ctx->fault_work); /* Only needed for dedicated process */
  158. put_pid(ctx->pid);
  159. cxl_ctx_put();
  160. return 0;
  161. }
  162. /*
  163. * Detach the given context from the AFU. This doesn't actually
  164. * free the context but it should stop the context running in hardware
  165. * (ie. prevent this context from generating any further interrupts
  166. * so that it can be freed).
  167. */
  168. void cxl_context_detach(struct cxl_context *ctx)
  169. {
  170. int rc;
  171. rc = __detach_context(ctx);
  172. if (rc)
  173. return;
  174. afu_release_irqs(ctx, ctx);
  175. wake_up_all(&ctx->wq);
  176. }
  177. /*
  178. * Detach all contexts on the given AFU.
  179. */
  180. void cxl_context_detach_all(struct cxl_afu *afu)
  181. {
  182. struct cxl_context *ctx;
  183. int tmp;
  184. mutex_lock(&afu->contexts_lock);
  185. idr_for_each_entry(&afu->contexts_idr, ctx, tmp) {
  186. /*
  187. * Anything done in here needs to be setup before the IDR is
  188. * created and torn down after the IDR removed
  189. */
  190. cxl_context_detach(ctx);
  191. /*
  192. * We are force detaching - remove any active PSA mappings so
  193. * userspace cannot interfere with the card if it comes back.
  194. * Easiest way to exercise this is to unbind and rebind the
  195. * driver via sysfs while it is in use.
  196. */
  197. mutex_lock(&ctx->mapping_lock);
  198. if (ctx->mapping)
  199. unmap_mapping_range(ctx->mapping, 0, 0, 1);
  200. mutex_unlock(&ctx->mapping_lock);
  201. }
  202. mutex_unlock(&afu->contexts_lock);
  203. }
  204. static void reclaim_ctx(struct rcu_head *rcu)
  205. {
  206. struct cxl_context *ctx = container_of(rcu, struct cxl_context, rcu);
  207. free_page((u64)ctx->sstp);
  208. ctx->sstp = NULL;
  209. kfree(ctx);
  210. }
  211. void cxl_context_free(struct cxl_context *ctx)
  212. {
  213. mutex_lock(&ctx->afu->contexts_lock);
  214. idr_remove(&ctx->afu->contexts_idr, ctx->pe);
  215. mutex_unlock(&ctx->afu->contexts_lock);
  216. call_rcu(&ctx->rcu, reclaim_ctx);
  217. }