context.c 4.9 KB

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