vas-fault.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * VAS Fault handling.
  4. * Copyright 2019, IBM Corporation
  5. */
  6. #define pr_fmt(fmt) "vas: " fmt
  7. #include <linux/kernel.h>
  8. #include <linux/types.h>
  9. #include <linux/slab.h>
  10. #include <linux/uaccess.h>
  11. #include <linux/kthread.h>
  12. #include <linux/sched/signal.h>
  13. #include <linux/mmu_context.h>
  14. #include <asm/icswx.h>
  15. #include "vas.h"
  16. /*
  17. * The maximum FIFO size for fault window can be 8MB
  18. * (VAS_RX_FIFO_SIZE_MAX). Using 4MB FIFO since each VAS
  19. * instance will be having fault window.
  20. * 8MB FIFO can be used if expects more faults for each VAS
  21. * instance.
  22. */
  23. #define VAS_FAULT_WIN_FIFO_SIZE (4 << 20)
  24. static void dump_fifo(struct vas_instance *vinst, void *entry)
  25. {
  26. unsigned long *end = vinst->fault_fifo + vinst->fault_fifo_size;
  27. unsigned long *fifo = entry;
  28. int i;
  29. pr_err("Fault fifo size %d, Max crbs %d\n", vinst->fault_fifo_size,
  30. vinst->fault_fifo_size / CRB_SIZE);
  31. /* Dump 10 CRB entries or until end of FIFO */
  32. pr_err("Fault FIFO Dump:\n");
  33. for (i = 0; i < 10*(CRB_SIZE/8) && fifo < end; i += 4, fifo += 4) {
  34. pr_err("[%.3d, %p]: 0x%.16lx 0x%.16lx 0x%.16lx 0x%.16lx\n",
  35. i, fifo, *fifo, *(fifo+1), *(fifo+2), *(fifo+3));
  36. }
  37. }
  38. /*
  39. * Process valid CRBs in fault FIFO.
  40. * NX process user space requests, return credit and update the status
  41. * in CRB. If it encounters transalation error when accessing CRB or
  42. * request buffers, raises interrupt on the CPU to handle the fault.
  43. * It takes credit on fault window, updates nx_fault_stamp in CRB with
  44. * the following information and pastes CRB in fault FIFO.
  45. *
  46. * pswid - window ID of the window on which the request is sent.
  47. * fault_storage_addr - fault address
  48. *
  49. * It can raise a single interrupt for multiple faults. Expects OS to
  50. * process all valid faults and return credit for each fault on user
  51. * space and fault windows. This fault FIFO control will be done with
  52. * credit mechanism. NX can continuously paste CRBs until credits are not
  53. * available on fault window. Otherwise, returns with RMA_reject.
  54. *
  55. * Total credits available on fault window: FIFO_SIZE(4MB)/CRBS_SIZE(128)
  56. *
  57. */
  58. irqreturn_t vas_fault_thread_fn(int irq, void *data)
  59. {
  60. struct vas_instance *vinst = data;
  61. struct coprocessor_request_block *crb, *entry;
  62. struct coprocessor_request_block buf;
  63. struct pnv_vas_window *window;
  64. unsigned long flags;
  65. void *fifo;
  66. crb = &buf;
  67. /*
  68. * VAS can interrupt with multiple page faults. So process all
  69. * valid CRBs within fault FIFO until reaches invalid CRB.
  70. * We use CCW[0] and pswid to validate CRBs:
  71. *
  72. * CCW[0] Reserved bit. When NX pastes CRB, CCW[0]=0
  73. * OS sets this bit to 1 after reading CRB.
  74. * pswid NX assigns window ID. Set pswid to -1 after
  75. * reading CRB from fault FIFO.
  76. *
  77. * We exit this function if no valid CRBs are available to process.
  78. * So acquire fault_lock and reset fifo_in_progress to 0 before
  79. * exit.
  80. * In case kernel receives another interrupt with different page
  81. * fault, interrupt handler returns with IRQ_HANDLED if
  82. * fifo_in_progress is set. Means these new faults will be
  83. * handled by the current thread. Otherwise set fifo_in_progress
  84. * and return IRQ_WAKE_THREAD to wake up thread.
  85. */
  86. while (true) {
  87. spin_lock_irqsave(&vinst->fault_lock, flags);
  88. /*
  89. * Advance the fault fifo pointer to next CRB.
  90. * Use CRB_SIZE rather than sizeof(*crb) since the latter is
  91. * aligned to CRB_ALIGN (256) but the CRB written to by VAS is
  92. * only CRB_SIZE in len.
  93. */
  94. fifo = vinst->fault_fifo + (vinst->fault_crbs * CRB_SIZE);
  95. entry = fifo;
  96. if ((entry->stamp.nx.pswid == cpu_to_be32(FIFO_INVALID_ENTRY))
  97. || (entry->ccw & cpu_to_be32(CCW0_INVALID))) {
  98. vinst->fifo_in_progress = 0;
  99. spin_unlock_irqrestore(&vinst->fault_lock, flags);
  100. return IRQ_HANDLED;
  101. }
  102. spin_unlock_irqrestore(&vinst->fault_lock, flags);
  103. vinst->fault_crbs++;
  104. if (vinst->fault_crbs == (vinst->fault_fifo_size / CRB_SIZE))
  105. vinst->fault_crbs = 0;
  106. memcpy(crb, fifo, CRB_SIZE);
  107. entry->stamp.nx.pswid = cpu_to_be32(FIFO_INVALID_ENTRY);
  108. entry->ccw |= cpu_to_be32(CCW0_INVALID);
  109. /*
  110. * Return credit for the fault window.
  111. */
  112. vas_return_credit(vinst->fault_win, false);
  113. pr_devel("VAS[%d] fault_fifo %p, fifo %p, fault_crbs %d\n",
  114. vinst->vas_id, vinst->fault_fifo, fifo,
  115. vinst->fault_crbs);
  116. vas_dump_crb(crb);
  117. window = vas_pswid_to_window(vinst,
  118. be32_to_cpu(crb->stamp.nx.pswid));
  119. if (IS_ERR(window)) {
  120. /*
  121. * We got an interrupt about a specific send
  122. * window but we can't find that window and we can't
  123. * even clean it up (return credit on user space
  124. * window).
  125. * But we should not get here.
  126. * TODO: Disable IRQ.
  127. */
  128. dump_fifo(vinst, (void *)entry);
  129. pr_err("VAS[%d] fault_fifo %p, fifo %p, pswid 0x%x, fault_crbs %d bad CRB?\n",
  130. vinst->vas_id, vinst->fault_fifo, fifo,
  131. be32_to_cpu(crb->stamp.nx.pswid),
  132. vinst->fault_crbs);
  133. WARN_ON_ONCE(1);
  134. } else {
  135. /*
  136. * NX sees faults only with user space windows.
  137. */
  138. if (window->user_win)
  139. vas_update_csb(crb, &window->vas_win.task_ref);
  140. else
  141. WARN_ON_ONCE(!window->user_win);
  142. /*
  143. * Return credit for send window after processing
  144. * fault CRB.
  145. */
  146. vas_return_credit(window, true);
  147. }
  148. }
  149. }
  150. irqreturn_t vas_fault_handler(int irq, void *dev_id)
  151. {
  152. struct vas_instance *vinst = dev_id;
  153. irqreturn_t ret = IRQ_WAKE_THREAD;
  154. unsigned long flags;
  155. /*
  156. * NX can generate an interrupt for multiple faults. So the
  157. * fault handler thread process all CRBs until finds invalid
  158. * entry. In case if NX sees continuous faults, it is possible
  159. * that the thread function entered with the first interrupt
  160. * can execute and process all valid CRBs.
  161. * So wake up thread only if the fault thread is not in progress.
  162. */
  163. spin_lock_irqsave(&vinst->fault_lock, flags);
  164. if (vinst->fifo_in_progress)
  165. ret = IRQ_HANDLED;
  166. else
  167. vinst->fifo_in_progress = 1;
  168. spin_unlock_irqrestore(&vinst->fault_lock, flags);
  169. return ret;
  170. }
  171. /*
  172. * Fault window is opened per VAS instance. NX pastes fault CRB in fault
  173. * FIFO upon page faults.
  174. */
  175. int vas_setup_fault_window(struct vas_instance *vinst)
  176. {
  177. struct vas_rx_win_attr attr;
  178. struct vas_window *win;
  179. vinst->fault_fifo_size = VAS_FAULT_WIN_FIFO_SIZE;
  180. vinst->fault_fifo = kzalloc(vinst->fault_fifo_size, GFP_KERNEL);
  181. if (!vinst->fault_fifo) {
  182. pr_err("Unable to alloc %d bytes for fault_fifo\n",
  183. vinst->fault_fifo_size);
  184. return -ENOMEM;
  185. }
  186. /*
  187. * Invalidate all CRB entries. NX pastes valid entry for each fault.
  188. */
  189. memset(vinst->fault_fifo, FIFO_INVALID_ENTRY, vinst->fault_fifo_size);
  190. vas_init_rx_win_attr(&attr, VAS_COP_TYPE_FAULT);
  191. attr.rx_fifo_size = vinst->fault_fifo_size;
  192. attr.rx_fifo = __pa(vinst->fault_fifo);
  193. /*
  194. * Max creds is based on number of CRBs can fit in the FIFO.
  195. * (fault_fifo_size/CRB_SIZE). If 8MB FIFO is used, max creds
  196. * will be 0xffff since the receive creds field is 16bits wide.
  197. */
  198. attr.wcreds_max = vinst->fault_fifo_size / CRB_SIZE;
  199. attr.lnotify_lpid = 0;
  200. attr.lnotify_pid = mfspr(SPRN_PID);
  201. attr.lnotify_tid = mfspr(SPRN_PID);
  202. win = vas_rx_win_open(vinst->vas_id, VAS_COP_TYPE_FAULT, &attr);
  203. if (IS_ERR(win)) {
  204. pr_err("VAS: Error %ld opening FaultWin\n", PTR_ERR(win));
  205. kfree(vinst->fault_fifo);
  206. return PTR_ERR(win);
  207. }
  208. vinst->fault_win = container_of(win, struct pnv_vas_window, vas_win);
  209. pr_devel("VAS: Created FaultWin %d, LPID/PID/TID [%d/%d/%d]\n",
  210. vinst->fault_win->vas_win.winid, attr.lnotify_lpid,
  211. attr.lnotify_pid, attr.lnotify_tid);
  212. return 0;
  213. }