fault.c 7.3 KB

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