context.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 = 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. /*
  61. * When we have to destroy all contexts in cxl_context_detach_all() we
  62. * end up with afu_release_irqs() called from inside a
  63. * idr_for_each_entry(). Hence we need to make sure that anything
  64. * dereferenced from this IDR is ok before we allocate the IDR here.
  65. * This clears out the IRQ ranges to ensure this.
  66. */
  67. for (i = 0; i < CXL_IRQ_RANGES; i++)
  68. ctx->irqs.range[i] = 0;
  69. mutex_init(&ctx->status_mutex);
  70. ctx->status = OPENED;
  71. /*
  72. * Allocating IDR! We better make sure everything's setup that
  73. * dereferences from it.
  74. */
  75. mutex_lock(&afu->contexts_lock);
  76. idr_preload(GFP_KERNEL);
  77. i = idr_alloc(&ctx->afu->contexts_idr, ctx, 0,
  78. ctx->afu->num_procs, GFP_NOWAIT);
  79. idr_preload_end();
  80. mutex_unlock(&afu->contexts_lock);
  81. if (i < 0)
  82. return i;
  83. ctx->pe = i;
  84. ctx->elem = &ctx->afu->spa[i];
  85. ctx->pe_inserted = false;
  86. return 0;
  87. }
  88. /*
  89. * Map a per-context mmio space into the given vma.
  90. */
  91. int cxl_context_iomap(struct cxl_context *ctx, struct vm_area_struct *vma)
  92. {
  93. u64 len = vma->vm_end - vma->vm_start;
  94. len = min(len, ctx->psn_size);
  95. if (ctx->afu->current_mode == CXL_MODE_DEDICATED) {
  96. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  97. return vm_iomap_memory(vma, ctx->afu->psn_phys, ctx->afu->adapter->ps_size);
  98. }
  99. /* make sure there is a valid per process space for this AFU */
  100. if ((ctx->master && !ctx->afu->psa) || (!ctx->afu->pp_psa)) {
  101. pr_devel("AFU doesn't support mmio space\n");
  102. return -EINVAL;
  103. }
  104. /* Can't mmap until the AFU is enabled */
  105. if (!ctx->afu->enabled)
  106. return -EBUSY;
  107. pr_devel("%s: mmio physical: %llx pe: %i master:%i\n", __func__,
  108. ctx->psn_phys, ctx->pe , ctx->master);
  109. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  110. return vm_iomap_memory(vma, ctx->psn_phys, len);
  111. }
  112. /*
  113. * Detach a context from the hardware. This disables interrupts and doesn't
  114. * return until all outstanding interrupts for this context have completed. The
  115. * hardware should no longer access *ctx after this has returned.
  116. */
  117. static void __detach_context(struct cxl_context *ctx)
  118. {
  119. enum cxl_context_status status;
  120. mutex_lock(&ctx->status_mutex);
  121. status = ctx->status;
  122. ctx->status = CLOSED;
  123. mutex_unlock(&ctx->status_mutex);
  124. if (status != STARTED)
  125. return;
  126. WARN_ON(cxl_detach_process(ctx));
  127. afu_release_irqs(ctx);
  128. flush_work(&ctx->fault_work); /* Only needed for dedicated process */
  129. wake_up_all(&ctx->wq);
  130. /* Release Problem State Area mapping */
  131. mutex_lock(&ctx->mapping_lock);
  132. if (ctx->mapping)
  133. unmap_mapping_range(ctx->mapping, 0, 0, 1);
  134. mutex_unlock(&ctx->mapping_lock);
  135. }
  136. /*
  137. * Detach the given context from the AFU. This doesn't actually
  138. * free the context but it should stop the context running in hardware
  139. * (ie. prevent this context from generating any further interrupts
  140. * so that it can be freed).
  141. */
  142. void cxl_context_detach(struct cxl_context *ctx)
  143. {
  144. __detach_context(ctx);
  145. }
  146. /*
  147. * Detach all contexts on the given AFU.
  148. */
  149. void cxl_context_detach_all(struct cxl_afu *afu)
  150. {
  151. struct cxl_context *ctx;
  152. int tmp;
  153. mutex_lock(&afu->contexts_lock);
  154. idr_for_each_entry(&afu->contexts_idr, ctx, tmp) {
  155. /*
  156. * Anything done in here needs to be setup before the IDR is
  157. * created and torn down after the IDR removed
  158. */
  159. __detach_context(ctx);
  160. }
  161. mutex_unlock(&afu->contexts_lock);
  162. }
  163. void cxl_context_free(struct cxl_context *ctx)
  164. {
  165. mutex_lock(&ctx->afu->contexts_lock);
  166. idr_remove(&ctx->afu->contexts_idr, ctx->pe);
  167. mutex_unlock(&ctx->afu->contexts_lock);
  168. synchronize_rcu();
  169. free_page((u64)ctx->sstp);
  170. ctx->sstp = NULL;
  171. put_pid(ctx->pid);
  172. kfree(ctx);
  173. }