fault.c 9.0 KB

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