context.c 8.1 KB

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