context.c 8.2 KB

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