context.c 8.2 KB

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