fault.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. if (!radix_enabled()) {
  120. /*
  121. * update_mmu_cache() will not have loaded the hash since current->trap
  122. * is not a 0x400 or 0x300, so just call hash_page_mm() here.
  123. */
  124. access = _PAGE_PRESENT | _PAGE_READ;
  125. if (dsisr & CXL_PSL_DSISR_An_S)
  126. access |= _PAGE_WRITE;
  127. access |= _PAGE_PRIVILEGED;
  128. if ((!ctx->kernel) || (REGION_ID(dar) == USER_REGION_ID))
  129. access &= ~_PAGE_PRIVILEGED;
  130. if (dsisr & DSISR_NOHPTE)
  131. inv_flags |= HPTE_NOHPTE_UPDATE;
  132. local_irq_save(flags);
  133. hash_page_mm(mm, dar, access, 0x300, inv_flags);
  134. local_irq_restore(flags);
  135. }
  136. pr_devel("Page fault successfully handled for pe: %i!\n", ctx->pe);
  137. cxl_ops->ack_irq(ctx, CXL_PSL_TFC_An_R, 0);
  138. }
  139. /*
  140. * Returns the mm_struct corresponding to the context ctx.
  141. * mm_users == 0, the context may be in the process of being closed.
  142. */
  143. static struct mm_struct *get_mem_context(struct cxl_context *ctx)
  144. {
  145. if (ctx->mm == NULL)
  146. return NULL;
  147. if (!atomic_inc_not_zero(&ctx->mm->mm_users))
  148. return NULL;
  149. return ctx->mm;
  150. }
  151. static bool cxl_is_segment_miss(struct cxl_context *ctx, u64 dsisr)
  152. {
  153. if ((cxl_is_psl8(ctx->afu)) && (dsisr & CXL_PSL_DSISR_An_DS))
  154. return true;
  155. return false;
  156. }
  157. static bool cxl_is_page_fault(struct cxl_context *ctx, u64 dsisr)
  158. {
  159. if ((cxl_is_psl8(ctx->afu)) && (dsisr & CXL_PSL_DSISR_An_DM))
  160. return true;
  161. if ((cxl_is_psl9(ctx->afu)) &&
  162. ((dsisr & CXL_PSL9_DSISR_An_CO_MASK) &
  163. (CXL_PSL9_DSISR_An_PF_SLR | CXL_PSL9_DSISR_An_PF_RGC |
  164. CXL_PSL9_DSISR_An_PF_RGP | CXL_PSL9_DSISR_An_PF_HRH |
  165. CXL_PSL9_DSISR_An_PF_STEG)))
  166. return true;
  167. return false;
  168. }
  169. void cxl_handle_fault(struct work_struct *fault_work)
  170. {
  171. struct cxl_context *ctx =
  172. container_of(fault_work, struct cxl_context, fault_work);
  173. u64 dsisr = ctx->dsisr;
  174. u64 dar = ctx->dar;
  175. struct mm_struct *mm = NULL;
  176. if (cpu_has_feature(CPU_FTR_HVMODE)) {
  177. if (cxl_p2n_read(ctx->afu, CXL_PSL_DSISR_An) != dsisr ||
  178. cxl_p2n_read(ctx->afu, CXL_PSL_DAR_An) != dar ||
  179. cxl_p2n_read(ctx->afu, CXL_PSL_PEHandle_An) != ctx->pe) {
  180. /* Most likely explanation is harmless - a dedicated
  181. * process has detached and these were cleared by the
  182. * PSL purge, but warn about it just in case
  183. */
  184. dev_notice(&ctx->afu->dev, "cxl_handle_fault: Translation fault regs changed\n");
  185. return;
  186. }
  187. }
  188. /* Early return if the context is being / has been detached */
  189. if (ctx->status == CLOSED) {
  190. cxl_ack_ae(ctx);
  191. return;
  192. }
  193. pr_devel("CXL BOTTOM HALF handling fault for afu pe: %i. "
  194. "DSISR: %#llx DAR: %#llx\n", ctx->pe, dsisr, dar);
  195. if (!ctx->kernel) {
  196. mm = get_mem_context(ctx);
  197. if (mm == NULL) {
  198. pr_devel("%s: unable to get mm for pe=%d pid=%i\n",
  199. __func__, ctx->pe, pid_nr(ctx->pid));
  200. cxl_ack_ae(ctx);
  201. return;
  202. } else {
  203. pr_devel("Handling page fault for pe=%d pid=%i\n",
  204. ctx->pe, pid_nr(ctx->pid));
  205. }
  206. }
  207. if (cxl_is_segment_miss(ctx, dsisr))
  208. cxl_handle_segment_miss(ctx, mm, dar);
  209. else if (cxl_is_page_fault(ctx, dsisr))
  210. cxl_handle_page_fault(ctx, mm, dsisr, dar);
  211. else
  212. WARN(1, "cxl_handle_fault has nothing to handle\n");
  213. if (mm)
  214. mmput(mm);
  215. }
  216. static void cxl_prefault_one(struct cxl_context *ctx, u64 ea)
  217. {
  218. struct mm_struct *mm;
  219. mm = get_mem_context(ctx);
  220. if (mm == NULL) {
  221. pr_devel("cxl_prefault_one unable to get mm %i\n",
  222. pid_nr(ctx->pid));
  223. return;
  224. }
  225. cxl_fault_segment(ctx, mm, ea);
  226. mmput(mm);
  227. }
  228. static u64 next_segment(u64 ea, u64 vsid)
  229. {
  230. if (vsid & SLB_VSID_B_1T)
  231. ea |= (1ULL << 40) - 1;
  232. else
  233. ea |= (1ULL << 28) - 1;
  234. return ea + 1;
  235. }
  236. static void cxl_prefault_vma(struct cxl_context *ctx)
  237. {
  238. u64 ea, last_esid = 0;
  239. struct copro_slb slb;
  240. struct vm_area_struct *vma;
  241. int rc;
  242. struct mm_struct *mm;
  243. mm = get_mem_context(ctx);
  244. if (mm == NULL) {
  245. pr_devel("cxl_prefault_vm unable to get mm %i\n",
  246. pid_nr(ctx->pid));
  247. return;
  248. }
  249. down_read(&mm->mmap_sem);
  250. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  251. for (ea = vma->vm_start; ea < vma->vm_end;
  252. ea = next_segment(ea, slb.vsid)) {
  253. rc = copro_calculate_slb(mm, ea, &slb);
  254. if (rc)
  255. continue;
  256. if (last_esid == slb.esid)
  257. continue;
  258. cxl_load_segment(ctx, &slb);
  259. last_esid = slb.esid;
  260. }
  261. }
  262. up_read(&mm->mmap_sem);
  263. mmput(mm);
  264. }
  265. void cxl_prefault(struct cxl_context *ctx, u64 wed)
  266. {
  267. switch (ctx->afu->prefault_mode) {
  268. case CXL_PREFAULT_WED:
  269. cxl_prefault_one(ctx, wed);
  270. break;
  271. case CXL_PREFAULT_ALL:
  272. cxl_prefault_vma(ctx);
  273. break;
  274. default:
  275. break;
  276. }
  277. }