context.c 8.9 KB

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