context.c 8.9 KB

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