fault.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. int cxl_handle_mm_fault(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. /*
  114. * Add the fault handling cpu to task mm cpumask so that we
  115. * can do a safe lockless page table walk when inserting the
  116. * hash page table entry. This function get called with a
  117. * valid mm for user space addresses. Hence using the if (mm)
  118. * check is sufficient here.
  119. */
  120. if (mm && !cpumask_test_cpu(smp_processor_id(), mm_cpumask(mm))) {
  121. cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
  122. /*
  123. * We need to make sure we walk the table only after
  124. * we update the cpumask. The other side of the barrier
  125. * is explained in serialize_against_pte_lookup()
  126. */
  127. smp_mb();
  128. }
  129. if ((result = copro_handle_mm_fault(mm, dar, dsisr, &flt))) {
  130. pr_devel("copro_handle_mm_fault failed: %#x\n", result);
  131. return result;
  132. }
  133. if (!radix_enabled()) {
  134. /*
  135. * update_mmu_cache() will not have loaded the hash since current->trap
  136. * is not a 0x400 or 0x300, so just call hash_page_mm() here.
  137. */
  138. access = _PAGE_PRESENT | _PAGE_READ;
  139. if (dsisr & CXL_PSL_DSISR_An_S)
  140. access |= _PAGE_WRITE;
  141. if (!mm && (REGION_ID(dar) != USER_REGION_ID))
  142. access |= _PAGE_PRIVILEGED;
  143. if (dsisr & DSISR_NOHPTE)
  144. inv_flags |= HPTE_NOHPTE_UPDATE;
  145. local_irq_save(flags);
  146. hash_page_mm(mm, dar, access, 0x300, inv_flags);
  147. local_irq_restore(flags);
  148. }
  149. return 0;
  150. }
  151. static void cxl_handle_page_fault(struct cxl_context *ctx,
  152. struct mm_struct *mm,
  153. u64 dsisr, u64 dar)
  154. {
  155. trace_cxl_pte_miss(ctx, dsisr, dar);
  156. if (cxl_handle_mm_fault(mm, dsisr, dar)) {
  157. cxl_ack_ae(ctx);
  158. } else {
  159. pr_devel("Page fault successfully handled for pe: %i!\n", ctx->pe);
  160. cxl_ops->ack_irq(ctx, CXL_PSL_TFC_An_R, 0);
  161. }
  162. }
  163. /*
  164. * Returns the mm_struct corresponding to the context ctx.
  165. * mm_users == 0, the context may be in the process of being closed.
  166. */
  167. static struct mm_struct *get_mem_context(struct cxl_context *ctx)
  168. {
  169. if (ctx->mm == NULL)
  170. return NULL;
  171. if (!atomic_inc_not_zero(&ctx->mm->mm_users))
  172. return NULL;
  173. return ctx->mm;
  174. }
  175. static bool cxl_is_segment_miss(struct cxl_context *ctx, u64 dsisr)
  176. {
  177. if ((cxl_is_power8() && (dsisr & CXL_PSL_DSISR_An_DS)))
  178. return true;
  179. return false;
  180. }
  181. static bool cxl_is_page_fault(struct cxl_context *ctx, u64 dsisr)
  182. {
  183. u64 crs; /* Translation Checkout Response Status */
  184. if ((cxl_is_power8()) && (dsisr & CXL_PSL_DSISR_An_DM))
  185. return true;
  186. if (cxl_is_power9()) {
  187. crs = (dsisr & CXL_PSL9_DSISR_An_CO_MASK);
  188. if ((crs == CXL_PSL9_DSISR_An_PF_SLR) ||
  189. (crs == CXL_PSL9_DSISR_An_PF_RGC) ||
  190. (crs == CXL_PSL9_DSISR_An_PF_RGP) ||
  191. (crs == CXL_PSL9_DSISR_An_PF_HRH) ||
  192. (crs == CXL_PSL9_DSISR_An_PF_STEG) ||
  193. (crs == CXL_PSL9_DSISR_An_URTCH)) {
  194. return true;
  195. }
  196. }
  197. return false;
  198. }
  199. void cxl_handle_fault(struct work_struct *fault_work)
  200. {
  201. struct cxl_context *ctx =
  202. container_of(fault_work, struct cxl_context, fault_work);
  203. u64 dsisr = ctx->dsisr;
  204. u64 dar = ctx->dar;
  205. struct mm_struct *mm = NULL;
  206. if (cpu_has_feature(CPU_FTR_HVMODE)) {
  207. if (cxl_p2n_read(ctx->afu, CXL_PSL_DSISR_An) != dsisr ||
  208. cxl_p2n_read(ctx->afu, CXL_PSL_DAR_An) != dar ||
  209. cxl_p2n_read(ctx->afu, CXL_PSL_PEHandle_An) != ctx->pe) {
  210. /* Most likely explanation is harmless - a dedicated
  211. * process has detached and these were cleared by the
  212. * PSL purge, but warn about it just in case
  213. */
  214. dev_notice(&ctx->afu->dev, "cxl_handle_fault: Translation fault regs changed\n");
  215. return;
  216. }
  217. }
  218. /* Early return if the context is being / has been detached */
  219. if (ctx->status == CLOSED) {
  220. cxl_ack_ae(ctx);
  221. return;
  222. }
  223. pr_devel("CXL BOTTOM HALF handling fault for afu pe: %i. "
  224. "DSISR: %#llx DAR: %#llx\n", ctx->pe, dsisr, dar);
  225. if (!ctx->kernel) {
  226. mm = get_mem_context(ctx);
  227. if (mm == NULL) {
  228. pr_devel("%s: unable to get mm for pe=%d pid=%i\n",
  229. __func__, ctx->pe, pid_nr(ctx->pid));
  230. cxl_ack_ae(ctx);
  231. return;
  232. } else {
  233. pr_devel("Handling page fault for pe=%d pid=%i\n",
  234. ctx->pe, pid_nr(ctx->pid));
  235. }
  236. }
  237. if (cxl_is_segment_miss(ctx, dsisr))
  238. cxl_handle_segment_miss(ctx, mm, dar);
  239. else if (cxl_is_page_fault(ctx, dsisr))
  240. cxl_handle_page_fault(ctx, mm, dsisr, dar);
  241. else
  242. WARN(1, "cxl_handle_fault has nothing to handle\n");
  243. if (mm)
  244. mmput(mm);
  245. }
  246. static void cxl_prefault_one(struct cxl_context *ctx, u64 ea)
  247. {
  248. struct mm_struct *mm;
  249. mm = get_mem_context(ctx);
  250. if (mm == NULL) {
  251. pr_devel("cxl_prefault_one unable to get mm %i\n",
  252. pid_nr(ctx->pid));
  253. return;
  254. }
  255. cxl_fault_segment(ctx, mm, ea);
  256. mmput(mm);
  257. }
  258. static u64 next_segment(u64 ea, u64 vsid)
  259. {
  260. if (vsid & SLB_VSID_B_1T)
  261. ea |= (1ULL << 40) - 1;
  262. else
  263. ea |= (1ULL << 28) - 1;
  264. return ea + 1;
  265. }
  266. static void cxl_prefault_vma(struct cxl_context *ctx)
  267. {
  268. u64 ea, last_esid = 0;
  269. struct copro_slb slb;
  270. struct vm_area_struct *vma;
  271. int rc;
  272. struct mm_struct *mm;
  273. mm = get_mem_context(ctx);
  274. if (mm == NULL) {
  275. pr_devel("cxl_prefault_vm unable to get mm %i\n",
  276. pid_nr(ctx->pid));
  277. return;
  278. }
  279. down_read(&mm->mmap_sem);
  280. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  281. for (ea = vma->vm_start; ea < vma->vm_end;
  282. ea = next_segment(ea, slb.vsid)) {
  283. rc = copro_calculate_slb(mm, ea, &slb);
  284. if (rc)
  285. continue;
  286. if (last_esid == slb.esid)
  287. continue;
  288. cxl_load_segment(ctx, &slb);
  289. last_esid = slb.esid;
  290. }
  291. }
  292. up_read(&mm->mmap_sem);
  293. mmput(mm);
  294. }
  295. void cxl_prefault(struct cxl_context *ctx, u64 wed)
  296. {
  297. switch (ctx->afu->prefault_mode) {
  298. case CXL_PREFAULT_WED:
  299. cxl_prefault_one(ctx, wed);
  300. break;
  301. case CXL_PREFAULT_ALL:
  302. cxl_prefault_vma(ctx);
  303. break;
  304. default:
  305. break;
  306. }
  307. }