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