context.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 = ctx->glpid = 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. /*
  87. * take a ref on the afu so that it stays alive at-least till
  88. * this context is reclaimed inside reclaim_ctx.
  89. */
  90. cxl_afu_get(afu);
  91. return 0;
  92. }
  93. static int cxl_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  94. {
  95. struct cxl_context *ctx = vma->vm_file->private_data;
  96. unsigned long address = (unsigned long)vmf->virtual_address;
  97. u64 area, offset;
  98. offset = vmf->pgoff << PAGE_SHIFT;
  99. pr_devel("%s: pe: %i address: 0x%lx offset: 0x%llx\n",
  100. __func__, ctx->pe, address, offset);
  101. if (ctx->afu->current_mode == CXL_MODE_DEDICATED) {
  102. area = ctx->afu->psn_phys;
  103. if (offset >= ctx->afu->adapter->ps_size)
  104. return VM_FAULT_SIGBUS;
  105. } else {
  106. area = ctx->psn_phys;
  107. if (offset >= ctx->psn_size)
  108. return VM_FAULT_SIGBUS;
  109. }
  110. mutex_lock(&ctx->status_mutex);
  111. if (ctx->status != STARTED) {
  112. mutex_unlock(&ctx->status_mutex);
  113. pr_devel("%s: Context not started, failing problem state access\n", __func__);
  114. if (ctx->mmio_err_ff) {
  115. if (!ctx->ff_page) {
  116. ctx->ff_page = alloc_page(GFP_USER);
  117. if (!ctx->ff_page)
  118. return VM_FAULT_OOM;
  119. memset(page_address(ctx->ff_page), 0xff, PAGE_SIZE);
  120. }
  121. get_page(ctx->ff_page);
  122. vmf->page = ctx->ff_page;
  123. vma->vm_page_prot = pgprot_cached(vma->vm_page_prot);
  124. return 0;
  125. }
  126. return VM_FAULT_SIGBUS;
  127. }
  128. vm_insert_pfn(vma, address, (area + offset) >> PAGE_SHIFT);
  129. mutex_unlock(&ctx->status_mutex);
  130. return VM_FAULT_NOPAGE;
  131. }
  132. static const struct vm_operations_struct cxl_mmap_vmops = {
  133. .fault = cxl_mmap_fault,
  134. };
  135. /*
  136. * Map a per-context mmio space into the given vma.
  137. */
  138. int cxl_context_iomap(struct cxl_context *ctx, struct vm_area_struct *vma)
  139. {
  140. u64 start = vma->vm_pgoff << PAGE_SHIFT;
  141. u64 len = vma->vm_end - vma->vm_start;
  142. if (ctx->afu->current_mode == CXL_MODE_DEDICATED) {
  143. if (start + len > ctx->afu->adapter->ps_size)
  144. return -EINVAL;
  145. } else {
  146. if (start + len > ctx->psn_size)
  147. return -EINVAL;
  148. }
  149. if (ctx->afu->current_mode != CXL_MODE_DEDICATED) {
  150. /* make sure there is a valid per process space for this AFU */
  151. if ((ctx->master && !ctx->afu->psa) || (!ctx->afu->pp_psa)) {
  152. pr_devel("AFU doesn't support mmio space\n");
  153. return -EINVAL;
  154. }
  155. /* Can't mmap until the AFU is enabled */
  156. if (!ctx->afu->enabled)
  157. return -EBUSY;
  158. }
  159. pr_devel("%s: mmio physical: %llx pe: %i master:%i\n", __func__,
  160. ctx->psn_phys, ctx->pe , ctx->master);
  161. vma->vm_flags |= VM_IO | VM_PFNMAP;
  162. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  163. vma->vm_ops = &cxl_mmap_vmops;
  164. return 0;
  165. }
  166. /*
  167. * Detach a context from the hardware. This disables interrupts and doesn't
  168. * return until all outstanding interrupts for this context have completed. The
  169. * hardware should no longer access *ctx after this has returned.
  170. */
  171. int __detach_context(struct cxl_context *ctx)
  172. {
  173. enum cxl_context_status status;
  174. mutex_lock(&ctx->status_mutex);
  175. status = ctx->status;
  176. ctx->status = CLOSED;
  177. mutex_unlock(&ctx->status_mutex);
  178. if (status != STARTED)
  179. return -EBUSY;
  180. /* Only warn if we detached while the link was OK.
  181. * If detach fails when hw is down, we don't care.
  182. */
  183. WARN_ON(cxl_detach_process(ctx) &&
  184. cxl_adapter_link_ok(ctx->afu->adapter));
  185. flush_work(&ctx->fault_work); /* Only needed for dedicated process */
  186. /* release the reference to the group leader and mm handling pid */
  187. put_pid(ctx->pid);
  188. put_pid(ctx->glpid);
  189. cxl_ctx_put();
  190. return 0;
  191. }
  192. /*
  193. * Detach the given context from the AFU. This doesn't actually
  194. * free the context but it should stop the context running in hardware
  195. * (ie. prevent this context from generating any further interrupts
  196. * so that it can be freed).
  197. */
  198. void cxl_context_detach(struct cxl_context *ctx)
  199. {
  200. int rc;
  201. rc = __detach_context(ctx);
  202. if (rc)
  203. return;
  204. afu_release_irqs(ctx, ctx);
  205. wake_up_all(&ctx->wq);
  206. }
  207. /*
  208. * Detach all contexts on the given AFU.
  209. */
  210. void cxl_context_detach_all(struct cxl_afu *afu)
  211. {
  212. struct cxl_context *ctx;
  213. int tmp;
  214. mutex_lock(&afu->contexts_lock);
  215. idr_for_each_entry(&afu->contexts_idr, ctx, tmp) {
  216. /*
  217. * Anything done in here needs to be setup before the IDR is
  218. * created and torn down after the IDR removed
  219. */
  220. cxl_context_detach(ctx);
  221. /*
  222. * We are force detaching - remove any active PSA mappings so
  223. * userspace cannot interfere with the card if it comes back.
  224. * Easiest way to exercise this is to unbind and rebind the
  225. * driver via sysfs while it is in use.
  226. */
  227. mutex_lock(&ctx->mapping_lock);
  228. if (ctx->mapping)
  229. unmap_mapping_range(ctx->mapping, 0, 0, 1);
  230. mutex_unlock(&ctx->mapping_lock);
  231. }
  232. mutex_unlock(&afu->contexts_lock);
  233. }
  234. static void reclaim_ctx(struct rcu_head *rcu)
  235. {
  236. struct cxl_context *ctx = container_of(rcu, struct cxl_context, rcu);
  237. free_page((u64)ctx->sstp);
  238. if (ctx->ff_page)
  239. __free_page(ctx->ff_page);
  240. ctx->sstp = NULL;
  241. if (ctx->kernelapi)
  242. kfree(ctx->mapping);
  243. if (ctx->irq_bitmap)
  244. kfree(ctx->irq_bitmap);
  245. /* Drop ref to the afu device taken during cxl_context_init */
  246. cxl_afu_put(ctx->afu);
  247. kfree(ctx);
  248. }
  249. void cxl_context_free(struct cxl_context *ctx)
  250. {
  251. mutex_lock(&ctx->afu->contexts_lock);
  252. idr_remove(&ctx->afu->contexts_idr, ctx->pe);
  253. mutex_unlock(&ctx->afu->contexts_lock);
  254. call_rcu(&ctx->rcu, reclaim_ctx);
  255. }