fault.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. static bool sste_matches(struct cxl_sste *sste, struct copro_slb *slb)
  21. {
  22. return ((sste->vsid_data == cpu_to_be64(slb->vsid)) &&
  23. (sste->esid_data == cpu_to_be64(slb->esid)));
  24. }
  25. /*
  26. * This finds a free SSTE for the given SLB, or returns NULL if it's already in
  27. * the segment table.
  28. */
  29. static struct cxl_sste* find_free_sste(struct cxl_context *ctx,
  30. struct copro_slb *slb)
  31. {
  32. struct cxl_sste *primary, *sste, *ret = NULL;
  33. unsigned int mask = (ctx->sst_size >> 7) - 1; /* SSTP0[SegTableSize] */
  34. unsigned int entry;
  35. unsigned int hash;
  36. if (slb->vsid & SLB_VSID_B_1T)
  37. hash = (slb->esid >> SID_SHIFT_1T) & mask;
  38. else /* 256M */
  39. hash = (slb->esid >> SID_SHIFT) & mask;
  40. primary = ctx->sstp + (hash << 3);
  41. for (entry = 0, sste = primary; entry < 8; entry++, sste++) {
  42. if (!ret && !(be64_to_cpu(sste->esid_data) & SLB_ESID_V))
  43. ret = sste;
  44. if (sste_matches(sste, slb))
  45. return NULL;
  46. }
  47. if (ret)
  48. return ret;
  49. /* Nothing free, select an entry to cast out */
  50. ret = primary + ctx->sst_lru;
  51. ctx->sst_lru = (ctx->sst_lru + 1) & 0x7;
  52. return ret;
  53. }
  54. static void cxl_load_segment(struct cxl_context *ctx, struct copro_slb *slb)
  55. {
  56. /* mask is the group index, we search primary and secondary here. */
  57. struct cxl_sste *sste;
  58. unsigned long flags;
  59. spin_lock_irqsave(&ctx->sste_lock, flags);
  60. sste = find_free_sste(ctx, slb);
  61. if (!sste)
  62. goto out_unlock;
  63. pr_devel("CXL Populating SST[%li]: %#llx %#llx\n",
  64. sste - ctx->sstp, slb->vsid, slb->esid);
  65. sste->vsid_data = cpu_to_be64(slb->vsid);
  66. sste->esid_data = cpu_to_be64(slb->esid);
  67. out_unlock:
  68. spin_unlock_irqrestore(&ctx->sste_lock, flags);
  69. }
  70. static int cxl_fault_segment(struct cxl_context *ctx, struct mm_struct *mm,
  71. u64 ea)
  72. {
  73. struct copro_slb slb = {0,0};
  74. int rc;
  75. if (!(rc = copro_calculate_slb(mm, ea, &slb))) {
  76. cxl_load_segment(ctx, &slb);
  77. }
  78. return rc;
  79. }
  80. static void cxl_ack_ae(struct cxl_context *ctx)
  81. {
  82. unsigned long flags;
  83. cxl_ack_irq(ctx, CXL_PSL_TFC_An_AE, 0);
  84. spin_lock_irqsave(&ctx->lock, flags);
  85. ctx->pending_fault = true;
  86. ctx->fault_addr = ctx->dar;
  87. ctx->fault_dsisr = ctx->dsisr;
  88. spin_unlock_irqrestore(&ctx->lock, flags);
  89. wake_up_all(&ctx->wq);
  90. }
  91. static int cxl_handle_segment_miss(struct cxl_context *ctx,
  92. struct mm_struct *mm, u64 ea)
  93. {
  94. int rc;
  95. pr_devel("CXL interrupt: Segment fault pe: %i ea: %#llx\n", ctx->pe, ea);
  96. if ((rc = cxl_fault_segment(ctx, mm, ea)))
  97. cxl_ack_ae(ctx);
  98. else {
  99. mb(); /* Order seg table write to TFC MMIO write */
  100. cxl_ack_irq(ctx, CXL_PSL_TFC_An_R, 0);
  101. }
  102. return IRQ_HANDLED;
  103. }
  104. static void cxl_handle_page_fault(struct cxl_context *ctx,
  105. struct mm_struct *mm, u64 dsisr, u64 dar)
  106. {
  107. unsigned flt = 0;
  108. int result;
  109. unsigned long access, flags, inv_flags = 0;
  110. if ((result = copro_handle_mm_fault(mm, dar, dsisr, &flt))) {
  111. pr_devel("copro_handle_mm_fault failed: %#x\n", result);
  112. return cxl_ack_ae(ctx);
  113. }
  114. /*
  115. * update_mmu_cache() will not have loaded the hash since current->trap
  116. * is not a 0x400 or 0x300, so just call hash_page_mm() here.
  117. */
  118. access = _PAGE_PRESENT;
  119. if (dsisr & CXL_PSL_DSISR_An_S)
  120. access |= _PAGE_RW;
  121. if ((!ctx->kernel) || ~(dar & (1ULL << 63)))
  122. access |= _PAGE_USER;
  123. if (dsisr & DSISR_NOHPTE)
  124. inv_flags |= HPTE_NOHPTE_UPDATE;
  125. local_irq_save(flags);
  126. hash_page_mm(mm, dar, access, 0x300, inv_flags);
  127. local_irq_restore(flags);
  128. pr_devel("Page fault successfully handled for pe: %i!\n", ctx->pe);
  129. cxl_ack_irq(ctx, CXL_PSL_TFC_An_R, 0);
  130. }
  131. void cxl_handle_fault(struct work_struct *fault_work)
  132. {
  133. struct cxl_context *ctx =
  134. container_of(fault_work, struct cxl_context, fault_work);
  135. u64 dsisr = ctx->dsisr;
  136. u64 dar = ctx->dar;
  137. struct task_struct *task;
  138. struct mm_struct *mm;
  139. if (cxl_p2n_read(ctx->afu, CXL_PSL_DSISR_An) != dsisr ||
  140. cxl_p2n_read(ctx->afu, CXL_PSL_DAR_An) != dar ||
  141. cxl_p2n_read(ctx->afu, CXL_PSL_PEHandle_An) != ctx->pe) {
  142. /* Most likely explanation is harmless - a dedicated process
  143. * has detached and these were cleared by the PSL purge, but
  144. * warn about it just in case */
  145. dev_notice(&ctx->afu->dev, "cxl_handle_fault: Translation fault regs changed\n");
  146. return;
  147. }
  148. pr_devel("CXL BOTTOM HALF handling fault for afu pe: %i. "
  149. "DSISR: %#llx DAR: %#llx\n", ctx->pe, dsisr, dar);
  150. if (!(task = get_pid_task(ctx->pid, PIDTYPE_PID))) {
  151. pr_devel("cxl_handle_fault unable to get task %i\n",
  152. pid_nr(ctx->pid));
  153. cxl_ack_ae(ctx);
  154. return;
  155. }
  156. if (!(mm = get_task_mm(task))) {
  157. pr_devel("cxl_handle_fault unable to get mm %i\n",
  158. pid_nr(ctx->pid));
  159. cxl_ack_ae(ctx);
  160. goto out;
  161. }
  162. if (dsisr & CXL_PSL_DSISR_An_DS)
  163. cxl_handle_segment_miss(ctx, mm, dar);
  164. else if (dsisr & CXL_PSL_DSISR_An_DM)
  165. cxl_handle_page_fault(ctx, mm, dsisr, dar);
  166. else
  167. WARN(1, "cxl_handle_fault has nothing to handle\n");
  168. mmput(mm);
  169. out:
  170. put_task_struct(task);
  171. }
  172. static void cxl_prefault_one(struct cxl_context *ctx, u64 ea)
  173. {
  174. int rc;
  175. struct task_struct *task;
  176. struct mm_struct *mm;
  177. if (!(task = get_pid_task(ctx->pid, PIDTYPE_PID))) {
  178. pr_devel("cxl_prefault_one unable to get task %i\n",
  179. pid_nr(ctx->pid));
  180. return;
  181. }
  182. if (!(mm = get_task_mm(task))) {
  183. pr_devel("cxl_prefault_one unable to get mm %i\n",
  184. pid_nr(ctx->pid));
  185. put_task_struct(task);
  186. return;
  187. }
  188. rc = cxl_fault_segment(ctx, mm, ea);
  189. mmput(mm);
  190. put_task_struct(task);
  191. }
  192. static u64 next_segment(u64 ea, u64 vsid)
  193. {
  194. if (vsid & SLB_VSID_B_1T)
  195. ea |= (1ULL << 40) - 1;
  196. else
  197. ea |= (1ULL << 28) - 1;
  198. return ea + 1;
  199. }
  200. static void cxl_prefault_vma(struct cxl_context *ctx)
  201. {
  202. u64 ea, last_esid = 0;
  203. struct copro_slb slb;
  204. struct vm_area_struct *vma;
  205. int rc;
  206. struct task_struct *task;
  207. struct mm_struct *mm;
  208. if (!(task = get_pid_task(ctx->pid, PIDTYPE_PID))) {
  209. pr_devel("cxl_prefault_vma unable to get task %i\n",
  210. pid_nr(ctx->pid));
  211. return;
  212. }
  213. if (!(mm = get_task_mm(task))) {
  214. pr_devel("cxl_prefault_vm unable to get mm %i\n",
  215. pid_nr(ctx->pid));
  216. goto out1;
  217. }
  218. down_read(&mm->mmap_sem);
  219. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  220. for (ea = vma->vm_start; ea < vma->vm_end;
  221. ea = next_segment(ea, slb.vsid)) {
  222. rc = copro_calculate_slb(mm, ea, &slb);
  223. if (rc)
  224. continue;
  225. if (last_esid == slb.esid)
  226. continue;
  227. cxl_load_segment(ctx, &slb);
  228. last_esid = slb.esid;
  229. }
  230. }
  231. up_read(&mm->mmap_sem);
  232. mmput(mm);
  233. out1:
  234. put_task_struct(task);
  235. }
  236. void cxl_prefault(struct cxl_context *ctx, u64 wed)
  237. {
  238. switch (ctx->afu->prefault_mode) {
  239. case CXL_PREFAULT_WED:
  240. cxl_prefault_one(ctx, wed);
  241. break;
  242. case CXL_PREFAULT_ALL:
  243. cxl_prefault_vma(ctx);
  244. break;
  245. default:
  246. break;
  247. }
  248. }