fault.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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/workqueue.h>
  10. #include <linux/sched/signal.h>
  11. #include <linux/sched/mm.h>
  12. #include <linux/pid.h>
  13. #include <linux/mm.h>
  14. #include <linux/moduleparam.h>
  15. #undef MODULE_PARAM_PREFIX
  16. #define MODULE_PARAM_PREFIX "cxl" "."
  17. #include <asm/current.h>
  18. #include <asm/copro.h>
  19. #include <asm/mmu.h>
  20. #include "cxl.h"
  21. #include "trace.h"
  22. static bool sste_matches(struct cxl_sste *sste, struct copro_slb *slb)
  23. {
  24. return ((sste->vsid_data == cpu_to_be64(slb->vsid)) &&
  25. (sste->esid_data == cpu_to_be64(slb->esid)));
  26. }
  27. /*
  28. * This finds a free SSTE for the given SLB, or returns NULL if it's already in
  29. * the segment table.
  30. */
  31. static struct cxl_sste* find_free_sste(struct cxl_context *ctx,
  32. struct copro_slb *slb)
  33. {
  34. struct cxl_sste *primary, *sste, *ret = NULL;
  35. unsigned int mask = (ctx->sst_size >> 7) - 1; /* SSTP0[SegTableSize] */
  36. unsigned int entry;
  37. unsigned int hash;
  38. if (slb->vsid & SLB_VSID_B_1T)
  39. hash = (slb->esid >> SID_SHIFT_1T) & mask;
  40. else /* 256M */
  41. hash = (slb->esid >> SID_SHIFT) & mask;
  42. primary = ctx->sstp + (hash << 3);
  43. for (entry = 0, sste = primary; entry < 8; entry++, sste++) {
  44. if (!ret && !(be64_to_cpu(sste->esid_data) & SLB_ESID_V))
  45. ret = sste;
  46. if (sste_matches(sste, slb))
  47. return NULL;
  48. }
  49. if (ret)
  50. return ret;
  51. /* Nothing free, select an entry to cast out */
  52. ret = primary + ctx->sst_lru;
  53. ctx->sst_lru = (ctx->sst_lru + 1) & 0x7;
  54. return ret;
  55. }
  56. static void cxl_load_segment(struct cxl_context *ctx, struct copro_slb *slb)
  57. {
  58. /* mask is the group index, we search primary and secondary here. */
  59. struct cxl_sste *sste;
  60. unsigned long flags;
  61. spin_lock_irqsave(&ctx->sste_lock, flags);
  62. sste = find_free_sste(ctx, slb);
  63. if (!sste)
  64. goto out_unlock;
  65. pr_devel("CXL Populating SST[%li]: %#llx %#llx\n",
  66. sste - ctx->sstp, slb->vsid, slb->esid);
  67. trace_cxl_ste_write(ctx, sste - ctx->sstp, slb->esid, slb->vsid);
  68. sste->vsid_data = cpu_to_be64(slb->vsid);
  69. sste->esid_data = cpu_to_be64(slb->esid);
  70. out_unlock:
  71. spin_unlock_irqrestore(&ctx->sste_lock, flags);
  72. }
  73. static int cxl_fault_segment(struct cxl_context *ctx, struct mm_struct *mm,
  74. u64 ea)
  75. {
  76. struct copro_slb slb = {0,0};
  77. int rc;
  78. if (!(rc = copro_calculate_slb(mm, ea, &slb))) {
  79. cxl_load_segment(ctx, &slb);
  80. }
  81. return rc;
  82. }
  83. static void cxl_ack_ae(struct cxl_context *ctx)
  84. {
  85. unsigned long flags;
  86. cxl_ops->ack_irq(ctx, CXL_PSL_TFC_An_AE, 0);
  87. spin_lock_irqsave(&ctx->lock, flags);
  88. ctx->pending_fault = true;
  89. ctx->fault_addr = ctx->dar;
  90. ctx->fault_dsisr = ctx->dsisr;
  91. spin_unlock_irqrestore(&ctx->lock, flags);
  92. wake_up_all(&ctx->wq);
  93. }
  94. static int cxl_handle_segment_miss(struct cxl_context *ctx,
  95. struct mm_struct *mm, u64 ea)
  96. {
  97. int rc;
  98. pr_devel("CXL interrupt: Segment fault pe: %i ea: %#llx\n", ctx->pe, ea);
  99. trace_cxl_ste_miss(ctx, ea);
  100. if ((rc = cxl_fault_segment(ctx, mm, ea)))
  101. cxl_ack_ae(ctx);
  102. else {
  103. mb(); /* Order seg table write to TFC MMIO write */
  104. cxl_ops->ack_irq(ctx, CXL_PSL_TFC_An_R, 0);
  105. }
  106. return IRQ_HANDLED;
  107. }
  108. static void cxl_handle_page_fault(struct cxl_context *ctx,
  109. struct mm_struct *mm, u64 dsisr, u64 dar)
  110. {
  111. unsigned flt = 0;
  112. int result;
  113. unsigned long access, flags, inv_flags = 0;
  114. trace_cxl_pte_miss(ctx, dsisr, dar);
  115. if ((result = copro_handle_mm_fault(mm, dar, dsisr, &flt))) {
  116. pr_devel("copro_handle_mm_fault failed: %#x\n", result);
  117. return cxl_ack_ae(ctx);
  118. }
  119. /*
  120. * update_mmu_cache() will not have loaded the hash since current->trap
  121. * is not a 0x400 or 0x300, so just call hash_page_mm() here.
  122. */
  123. access = _PAGE_PRESENT | _PAGE_READ;
  124. if (dsisr & CXL_PSL_DSISR_An_S)
  125. access |= _PAGE_WRITE;
  126. access |= _PAGE_PRIVILEGED;
  127. if ((!ctx->kernel) || (REGION_ID(dar) == USER_REGION_ID))
  128. access &= ~_PAGE_PRIVILEGED;
  129. if (dsisr & DSISR_NOHPTE)
  130. inv_flags |= HPTE_NOHPTE_UPDATE;
  131. local_irq_save(flags);
  132. hash_page_mm(mm, dar, access, 0x300, inv_flags);
  133. local_irq_restore(flags);
  134. pr_devel("Page fault successfully handled for pe: %i!\n", ctx->pe);
  135. cxl_ops->ack_irq(ctx, CXL_PSL_TFC_An_R, 0);
  136. }
  137. /*
  138. * Returns the mm_struct corresponding to the context ctx.
  139. * mm_users == 0, the context may be in the process of being closed.
  140. */
  141. static struct mm_struct *get_mem_context(struct cxl_context *ctx)
  142. {
  143. if (ctx->mm == NULL)
  144. return NULL;
  145. if (!atomic_inc_not_zero(&ctx->mm->mm_users))
  146. return NULL;
  147. return ctx->mm;
  148. }
  149. void cxl_handle_fault(struct work_struct *fault_work)
  150. {
  151. struct cxl_context *ctx =
  152. container_of(fault_work, struct cxl_context, fault_work);
  153. u64 dsisr = ctx->dsisr;
  154. u64 dar = ctx->dar;
  155. struct mm_struct *mm = NULL;
  156. if (cpu_has_feature(CPU_FTR_HVMODE)) {
  157. if (cxl_p2n_read(ctx->afu, CXL_PSL_DSISR_An) != dsisr ||
  158. cxl_p2n_read(ctx->afu, CXL_PSL_DAR_An) != dar ||
  159. cxl_p2n_read(ctx->afu, CXL_PSL_PEHandle_An) != ctx->pe) {
  160. /* Most likely explanation is harmless - a dedicated
  161. * process has detached and these were cleared by the
  162. * PSL purge, but warn about it just in case
  163. */
  164. dev_notice(&ctx->afu->dev, "cxl_handle_fault: Translation fault regs changed\n");
  165. return;
  166. }
  167. }
  168. /* Early return if the context is being / has been detached */
  169. if (ctx->status == CLOSED) {
  170. cxl_ack_ae(ctx);
  171. return;
  172. }
  173. pr_devel("CXL BOTTOM HALF handling fault for afu pe: %i. "
  174. "DSISR: %#llx DAR: %#llx\n", ctx->pe, dsisr, dar);
  175. if (!ctx->kernel) {
  176. mm = get_mem_context(ctx);
  177. if (mm == NULL) {
  178. pr_devel("%s: unable to get mm for pe=%d pid=%i\n",
  179. __func__, ctx->pe, pid_nr(ctx->pid));
  180. cxl_ack_ae(ctx);
  181. return;
  182. } else {
  183. pr_devel("Handling page fault for pe=%d pid=%i\n",
  184. ctx->pe, pid_nr(ctx->pid));
  185. }
  186. }
  187. if (dsisr & CXL_PSL_DSISR_An_DS)
  188. cxl_handle_segment_miss(ctx, mm, dar);
  189. else if (dsisr & CXL_PSL_DSISR_An_DM)
  190. cxl_handle_page_fault(ctx, mm, dsisr, dar);
  191. else
  192. WARN(1, "cxl_handle_fault has nothing to handle\n");
  193. if (mm)
  194. mmput(mm);
  195. }
  196. static void cxl_prefault_one(struct cxl_context *ctx, u64 ea)
  197. {
  198. struct mm_struct *mm;
  199. mm = get_mem_context(ctx);
  200. if (mm == NULL) {
  201. pr_devel("cxl_prefault_one unable to get mm %i\n",
  202. pid_nr(ctx->pid));
  203. return;
  204. }
  205. cxl_fault_segment(ctx, mm, ea);
  206. mmput(mm);
  207. }
  208. static u64 next_segment(u64 ea, u64 vsid)
  209. {
  210. if (vsid & SLB_VSID_B_1T)
  211. ea |= (1ULL << 40) - 1;
  212. else
  213. ea |= (1ULL << 28) - 1;
  214. return ea + 1;
  215. }
  216. static void cxl_prefault_vma(struct cxl_context *ctx)
  217. {
  218. u64 ea, last_esid = 0;
  219. struct copro_slb slb;
  220. struct vm_area_struct *vma;
  221. int rc;
  222. struct mm_struct *mm;
  223. mm = get_mem_context(ctx);
  224. if (mm == NULL) {
  225. pr_devel("cxl_prefault_vm unable to get mm %i\n",
  226. pid_nr(ctx->pid));
  227. return;
  228. }
  229. down_read(&mm->mmap_sem);
  230. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  231. for (ea = vma->vm_start; ea < vma->vm_end;
  232. ea = next_segment(ea, slb.vsid)) {
  233. rc = copro_calculate_slb(mm, ea, &slb);
  234. if (rc)
  235. continue;
  236. if (last_esid == slb.esid)
  237. continue;
  238. cxl_load_segment(ctx, &slb);
  239. last_esid = slb.esid;
  240. }
  241. }
  242. up_read(&mm->mmap_sem);
  243. mmput(mm);
  244. }
  245. void cxl_prefault(struct cxl_context *ctx, u64 wed)
  246. {
  247. switch (ctx->afu->prefault_mode) {
  248. case CXL_PREFAULT_WED:
  249. cxl_prefault_one(ctx, wed);
  250. break;
  251. case CXL_PREFAULT_ALL:
  252. cxl_prefault_vma(ctx);
  253. break;
  254. default:
  255. break;
  256. }
  257. }