fault.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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/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 via ctx->pid
  139. * In case the task has exited we use the task group leader accessible
  140. * via ctx->glpid to find the next task in the thread group that has a
  141. * valid mm_struct associated with it. If a task with valid mm_struct
  142. * is found the ctx->pid is updated to use the task struct for subsequent
  143. * translations. In case no valid mm_struct is found in the task group to
  144. * service the fault a NULL is returned.
  145. */
  146. static struct mm_struct *get_mem_context(struct cxl_context *ctx)
  147. {
  148. struct task_struct *task = NULL;
  149. struct mm_struct *mm = NULL;
  150. struct pid *old_pid = ctx->pid;
  151. if (old_pid == NULL) {
  152. pr_warn("%s: Invalid context for pe=%d\n",
  153. __func__, ctx->pe);
  154. return NULL;
  155. }
  156. task = get_pid_task(old_pid, PIDTYPE_PID);
  157. /*
  158. * pid_alive may look racy but this saves us from costly
  159. * get_task_mm when the task is a zombie. In worst case
  160. * we may think a task is alive, which is about to die
  161. * but get_task_mm will return NULL.
  162. */
  163. if (task != NULL && pid_alive(task))
  164. mm = get_task_mm(task);
  165. /* release the task struct that was taken earlier */
  166. if (task)
  167. put_task_struct(task);
  168. else
  169. pr_devel("%s: Context owning pid=%i for pe=%i dead\n",
  170. __func__, pid_nr(old_pid), ctx->pe);
  171. /*
  172. * If we couldn't find the mm context then use the group
  173. * leader to iterate over the task group and find a task
  174. * that gives us mm_struct.
  175. */
  176. if (unlikely(mm == NULL && ctx->glpid != NULL)) {
  177. rcu_read_lock();
  178. task = pid_task(ctx->glpid, PIDTYPE_PID);
  179. if (task)
  180. do {
  181. mm = get_task_mm(task);
  182. if (mm) {
  183. ctx->pid = get_task_pid(task,
  184. PIDTYPE_PID);
  185. break;
  186. }
  187. task = next_thread(task);
  188. } while (task && !thread_group_leader(task));
  189. rcu_read_unlock();
  190. /* check if we switched pid */
  191. if (ctx->pid != old_pid) {
  192. if (mm)
  193. pr_devel("%s:pe=%i switch pid %i->%i\n",
  194. __func__, ctx->pe, pid_nr(old_pid),
  195. pid_nr(ctx->pid));
  196. else
  197. pr_devel("%s:Cannot find mm for pid=%i\n",
  198. __func__, pid_nr(old_pid));
  199. /* drop the reference to older pid */
  200. put_pid(old_pid);
  201. }
  202. }
  203. return mm;
  204. }
  205. void cxl_handle_fault(struct work_struct *fault_work)
  206. {
  207. struct cxl_context *ctx =
  208. container_of(fault_work, struct cxl_context, fault_work);
  209. u64 dsisr = ctx->dsisr;
  210. u64 dar = ctx->dar;
  211. struct mm_struct *mm = NULL;
  212. if (cpu_has_feature(CPU_FTR_HVMODE)) {
  213. if (cxl_p2n_read(ctx->afu, CXL_PSL_DSISR_An) != dsisr ||
  214. cxl_p2n_read(ctx->afu, CXL_PSL_DAR_An) != dar ||
  215. cxl_p2n_read(ctx->afu, CXL_PSL_PEHandle_An) != ctx->pe) {
  216. /* Most likely explanation is harmless - a dedicated
  217. * process has detached and these were cleared by the
  218. * PSL purge, but warn about it just in case
  219. */
  220. dev_notice(&ctx->afu->dev, "cxl_handle_fault: Translation fault regs changed\n");
  221. return;
  222. }
  223. }
  224. /* Early return if the context is being / has been detached */
  225. if (ctx->status == CLOSED) {
  226. cxl_ack_ae(ctx);
  227. return;
  228. }
  229. pr_devel("CXL BOTTOM HALF handling fault for afu pe: %i. "
  230. "DSISR: %#llx DAR: %#llx\n", ctx->pe, dsisr, dar);
  231. if (!ctx->kernel) {
  232. mm = get_mem_context(ctx);
  233. /* indicates all the thread in task group have exited */
  234. if (mm == NULL) {
  235. pr_devel("%s: unable to get mm for pe=%d pid=%i\n",
  236. __func__, ctx->pe, pid_nr(ctx->pid));
  237. cxl_ack_ae(ctx);
  238. return;
  239. } else {
  240. pr_devel("Handling page fault for pe=%d pid=%i\n",
  241. ctx->pe, pid_nr(ctx->pid));
  242. }
  243. }
  244. if (dsisr & CXL_PSL_DSISR_An_DS)
  245. cxl_handle_segment_miss(ctx, mm, dar);
  246. else if (dsisr & CXL_PSL_DSISR_An_DM)
  247. cxl_handle_page_fault(ctx, mm, dsisr, dar);
  248. else
  249. WARN(1, "cxl_handle_fault has nothing to handle\n");
  250. if (mm)
  251. mmput(mm);
  252. }
  253. static void cxl_prefault_one(struct cxl_context *ctx, u64 ea)
  254. {
  255. struct mm_struct *mm;
  256. mm = get_mem_context(ctx);
  257. if (mm == NULL) {
  258. pr_devel("cxl_prefault_one unable to get mm %i\n",
  259. pid_nr(ctx->pid));
  260. return;
  261. }
  262. cxl_fault_segment(ctx, mm, ea);
  263. mmput(mm);
  264. }
  265. static u64 next_segment(u64 ea, u64 vsid)
  266. {
  267. if (vsid & SLB_VSID_B_1T)
  268. ea |= (1ULL << 40) - 1;
  269. else
  270. ea |= (1ULL << 28) - 1;
  271. return ea + 1;
  272. }
  273. static void cxl_prefault_vma(struct cxl_context *ctx)
  274. {
  275. u64 ea, last_esid = 0;
  276. struct copro_slb slb;
  277. struct vm_area_struct *vma;
  278. int rc;
  279. struct mm_struct *mm;
  280. mm = get_mem_context(ctx);
  281. if (mm == NULL) {
  282. pr_devel("cxl_prefault_vm unable to get mm %i\n",
  283. pid_nr(ctx->pid));
  284. return;
  285. }
  286. down_read(&mm->mmap_sem);
  287. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  288. for (ea = vma->vm_start; ea < vma->vm_end;
  289. ea = next_segment(ea, slb.vsid)) {
  290. rc = copro_calculate_slb(mm, ea, &slb);
  291. if (rc)
  292. continue;
  293. if (last_esid == slb.esid)
  294. continue;
  295. cxl_load_segment(ctx, &slb);
  296. last_esid = slb.esid;
  297. }
  298. }
  299. up_read(&mm->mmap_sem);
  300. mmput(mm);
  301. }
  302. void cxl_prefault(struct cxl_context *ctx, u64 wed)
  303. {
  304. switch (ctx->afu->prefault_mode) {
  305. case CXL_PREFAULT_WED:
  306. cxl_prefault_one(ctx, wed);
  307. break;
  308. case CXL_PREFAULT_ALL:
  309. cxl_prefault_vma(ctx);
  310. break;
  311. default:
  312. break;
  313. }
  314. }