irq.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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/interrupt.h>
  10. #include <linux/workqueue.h>
  11. #include <linux/sched.h>
  12. #include <linux/wait.h>
  13. #include <linux/slab.h>
  14. #include <linux/pid.h>
  15. #include <asm/cputable.h>
  16. #include <misc/cxl-base.h>
  17. #include "cxl.h"
  18. #include "trace.h"
  19. static int afu_irq_range_start(void)
  20. {
  21. if (cpu_has_feature(CPU_FTR_HVMODE))
  22. return 1;
  23. return 0;
  24. }
  25. static irqreturn_t schedule_cxl_fault(struct cxl_context *ctx, u64 dsisr, u64 dar)
  26. {
  27. ctx->dsisr = dsisr;
  28. ctx->dar = dar;
  29. schedule_work(&ctx->fault_work);
  30. return IRQ_HANDLED;
  31. }
  32. irqreturn_t cxl_irq(int irq, struct cxl_context *ctx, struct cxl_irq_info *irq_info)
  33. {
  34. u64 dsisr, dar;
  35. dsisr = irq_info->dsisr;
  36. dar = irq_info->dar;
  37. trace_cxl_psl_irq(ctx, irq, dsisr, dar);
  38. pr_devel("CXL interrupt %i for afu pe: %i DSISR: %#llx DAR: %#llx\n", irq, ctx->pe, dsisr, dar);
  39. if (dsisr & CXL_PSL_DSISR_An_DS) {
  40. /*
  41. * We don't inherently need to sleep to handle this, but we do
  42. * need to get a ref to the task's mm, which we can't do from
  43. * irq context without the potential for a deadlock since it
  44. * takes the task_lock. An alternate option would be to keep a
  45. * reference to the task's mm the entire time it has cxl open,
  46. * but to do that we need to solve the issue where we hold a
  47. * ref to the mm, but the mm can hold a ref to the fd after an
  48. * mmap preventing anything from being cleaned up.
  49. */
  50. pr_devel("Scheduling segment miss handling for later pe: %i\n", ctx->pe);
  51. return schedule_cxl_fault(ctx, dsisr, dar);
  52. }
  53. if (dsisr & CXL_PSL_DSISR_An_M)
  54. pr_devel("CXL interrupt: PTE not found\n");
  55. if (dsisr & CXL_PSL_DSISR_An_P)
  56. pr_devel("CXL interrupt: Storage protection violation\n");
  57. if (dsisr & CXL_PSL_DSISR_An_A)
  58. pr_devel("CXL interrupt: AFU lock access to write through or cache inhibited storage\n");
  59. if (dsisr & CXL_PSL_DSISR_An_S)
  60. pr_devel("CXL interrupt: Access was afu_wr or afu_zero\n");
  61. if (dsisr & CXL_PSL_DSISR_An_K)
  62. pr_devel("CXL interrupt: Access not permitted by virtual page class key protection\n");
  63. if (dsisr & CXL_PSL_DSISR_An_DM) {
  64. /*
  65. * In some cases we might be able to handle the fault
  66. * immediately if hash_page would succeed, but we still need
  67. * the task's mm, which as above we can't get without a lock
  68. */
  69. pr_devel("Scheduling page fault handling for later pe: %i\n", ctx->pe);
  70. return schedule_cxl_fault(ctx, dsisr, dar);
  71. }
  72. if (dsisr & CXL_PSL_DSISR_An_ST)
  73. WARN(1, "CXL interrupt: Segment Table PTE not found\n");
  74. if (dsisr & CXL_PSL_DSISR_An_UR)
  75. pr_devel("CXL interrupt: AURP PTE not found\n");
  76. if (dsisr & CXL_PSL_DSISR_An_PE)
  77. return cxl_ops->handle_psl_slice_error(ctx, dsisr,
  78. irq_info->errstat);
  79. if (dsisr & CXL_PSL_DSISR_An_AE) {
  80. pr_devel("CXL interrupt: AFU Error 0x%016llx\n", irq_info->afu_err);
  81. if (ctx->pending_afu_err) {
  82. /*
  83. * This shouldn't happen - the PSL treats these errors
  84. * as fatal and will have reset the AFU, so there's not
  85. * much point buffering multiple AFU errors.
  86. * OTOH if we DO ever see a storm of these come in it's
  87. * probably best that we log them somewhere:
  88. */
  89. dev_err_ratelimited(&ctx->afu->dev, "CXL AFU Error "
  90. "undelivered to pe %i: 0x%016llx\n",
  91. ctx->pe, irq_info->afu_err);
  92. } else {
  93. spin_lock(&ctx->lock);
  94. ctx->afu_err = irq_info->afu_err;
  95. ctx->pending_afu_err = 1;
  96. spin_unlock(&ctx->lock);
  97. wake_up_all(&ctx->wq);
  98. }
  99. cxl_ops->ack_irq(ctx, CXL_PSL_TFC_An_A, 0);
  100. return IRQ_HANDLED;
  101. }
  102. if (dsisr & CXL_PSL_DSISR_An_OC)
  103. pr_devel("CXL interrupt: OS Context Warning\n");
  104. WARN(1, "Unhandled CXL PSL IRQ\n");
  105. return IRQ_HANDLED;
  106. }
  107. static irqreturn_t cxl_irq_afu(int irq, void *data)
  108. {
  109. struct cxl_context *ctx = data;
  110. irq_hw_number_t hwirq = irqd_to_hwirq(irq_get_irq_data(irq));
  111. int irq_off, afu_irq = 0;
  112. __u16 range;
  113. int r;
  114. /*
  115. * Look for the interrupt number.
  116. * On bare-metal, we know range 0 only contains the PSL
  117. * interrupt so we could start counting at range 1 and initialize
  118. * afu_irq at 1.
  119. * In a guest, range 0 also contains AFU interrupts, so it must
  120. * be counted for. Therefore we initialize afu_irq at 0 to take into
  121. * account the PSL interrupt.
  122. *
  123. * For code-readability, it just seems easier to go over all
  124. * the ranges on bare-metal and guest. The end result is the same.
  125. */
  126. for (r = 0; r < CXL_IRQ_RANGES; r++) {
  127. irq_off = hwirq - ctx->irqs.offset[r];
  128. range = ctx->irqs.range[r];
  129. if (irq_off >= 0 && irq_off < range) {
  130. afu_irq += irq_off;
  131. break;
  132. }
  133. afu_irq += range;
  134. }
  135. if (unlikely(r >= CXL_IRQ_RANGES)) {
  136. WARN(1, "Received AFU IRQ out of range for pe %i (virq %i hwirq %lx)\n",
  137. ctx->pe, irq, hwirq);
  138. return IRQ_HANDLED;
  139. }
  140. trace_cxl_afu_irq(ctx, afu_irq, irq, hwirq);
  141. pr_devel("Received AFU interrupt %i for pe: %i (virq %i hwirq %lx)\n",
  142. afu_irq, ctx->pe, irq, hwirq);
  143. if (unlikely(!ctx->irq_bitmap)) {
  144. WARN(1, "Received AFU IRQ for context with no IRQ bitmap\n");
  145. return IRQ_HANDLED;
  146. }
  147. spin_lock(&ctx->lock);
  148. set_bit(afu_irq - 1, ctx->irq_bitmap);
  149. ctx->pending_irq = true;
  150. spin_unlock(&ctx->lock);
  151. wake_up_all(&ctx->wq);
  152. return IRQ_HANDLED;
  153. }
  154. unsigned int cxl_map_irq(struct cxl *adapter, irq_hw_number_t hwirq,
  155. irq_handler_t handler, void *cookie, const char *name)
  156. {
  157. unsigned int virq;
  158. int result;
  159. /* IRQ Domain? */
  160. virq = irq_create_mapping(NULL, hwirq);
  161. if (!virq) {
  162. dev_warn(&adapter->dev, "cxl_map_irq: irq_create_mapping failed\n");
  163. return 0;
  164. }
  165. if (cxl_ops->setup_irq)
  166. cxl_ops->setup_irq(adapter, hwirq, virq);
  167. pr_devel("hwirq %#lx mapped to virq %u\n", hwirq, virq);
  168. result = request_irq(virq, handler, 0, name, cookie);
  169. if (result) {
  170. dev_warn(&adapter->dev, "cxl_map_irq: request_irq failed: %i\n", result);
  171. return 0;
  172. }
  173. return virq;
  174. }
  175. void cxl_unmap_irq(unsigned int virq, void *cookie)
  176. {
  177. free_irq(virq, cookie);
  178. }
  179. int cxl_register_one_irq(struct cxl *adapter,
  180. irq_handler_t handler,
  181. void *cookie,
  182. irq_hw_number_t *dest_hwirq,
  183. unsigned int *dest_virq,
  184. const char *name)
  185. {
  186. int hwirq, virq;
  187. if ((hwirq = cxl_ops->alloc_one_irq(adapter)) < 0)
  188. return hwirq;
  189. if (!(virq = cxl_map_irq(adapter, hwirq, handler, cookie, name)))
  190. goto err;
  191. *dest_hwirq = hwirq;
  192. *dest_virq = virq;
  193. return 0;
  194. err:
  195. cxl_ops->release_one_irq(adapter, hwirq);
  196. return -ENOMEM;
  197. }
  198. void afu_irq_name_free(struct cxl_context *ctx)
  199. {
  200. struct cxl_irq_name *irq_name, *tmp;
  201. list_for_each_entry_safe(irq_name, tmp, &ctx->irq_names, list) {
  202. kfree(irq_name->name);
  203. list_del(&irq_name->list);
  204. kfree(irq_name);
  205. }
  206. }
  207. int afu_allocate_irqs(struct cxl_context *ctx, u32 count)
  208. {
  209. int rc, r, i, j = 1;
  210. struct cxl_irq_name *irq_name;
  211. int alloc_count;
  212. /*
  213. * In native mode, range 0 is reserved for the multiplexed
  214. * PSL interrupt. It has been allocated when the AFU was initialized.
  215. *
  216. * In a guest, the PSL interrupt is not mutliplexed, but per-context,
  217. * and is the first interrupt from range 0. It still needs to be
  218. * allocated, so bump the count by one.
  219. */
  220. if (cpu_has_feature(CPU_FTR_HVMODE))
  221. alloc_count = count;
  222. else
  223. alloc_count = count + 1;
  224. if ((rc = cxl_ops->alloc_irq_ranges(&ctx->irqs, ctx->afu->adapter,
  225. alloc_count)))
  226. return rc;
  227. if (cpu_has_feature(CPU_FTR_HVMODE)) {
  228. /* Multiplexed PSL Interrupt */
  229. ctx->irqs.offset[0] = ctx->afu->native->psl_hwirq;
  230. ctx->irqs.range[0] = 1;
  231. }
  232. ctx->irq_count = count;
  233. ctx->irq_bitmap = kcalloc(BITS_TO_LONGS(count),
  234. sizeof(*ctx->irq_bitmap), GFP_KERNEL);
  235. if (!ctx->irq_bitmap)
  236. goto out;
  237. /*
  238. * Allocate names first. If any fail, bail out before allocating
  239. * actual hardware IRQs.
  240. */
  241. for (r = afu_irq_range_start(); r < CXL_IRQ_RANGES; r++) {
  242. for (i = 0; i < ctx->irqs.range[r]; i++) {
  243. irq_name = kmalloc(sizeof(struct cxl_irq_name),
  244. GFP_KERNEL);
  245. if (!irq_name)
  246. goto out;
  247. irq_name->name = kasprintf(GFP_KERNEL, "cxl-%s-pe%i-%i",
  248. dev_name(&ctx->afu->dev),
  249. ctx->pe, j);
  250. if (!irq_name->name) {
  251. kfree(irq_name);
  252. goto out;
  253. }
  254. /* Add to tail so next look get the correct order */
  255. list_add_tail(&irq_name->list, &ctx->irq_names);
  256. j++;
  257. }
  258. }
  259. return 0;
  260. out:
  261. cxl_ops->release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
  262. afu_irq_name_free(ctx);
  263. return -ENOMEM;
  264. }
  265. static void afu_register_hwirqs(struct cxl_context *ctx)
  266. {
  267. irq_hw_number_t hwirq;
  268. struct cxl_irq_name *irq_name;
  269. int r, i;
  270. irqreturn_t (*handler)(int irq, void *data);
  271. /* We've allocated all memory now, so let's do the irq allocations */
  272. irq_name = list_first_entry(&ctx->irq_names, struct cxl_irq_name, list);
  273. for (r = afu_irq_range_start(); r < CXL_IRQ_RANGES; r++) {
  274. hwirq = ctx->irqs.offset[r];
  275. for (i = 0; i < ctx->irqs.range[r]; hwirq++, i++) {
  276. if (r == 0 && i == 0)
  277. /*
  278. * The very first interrupt of range 0 is
  279. * always the PSL interrupt, but we only
  280. * need to connect a handler for guests,
  281. * because there's one PSL interrupt per
  282. * context.
  283. * On bare-metal, the PSL interrupt is
  284. * multiplexed and was setup when the AFU
  285. * was configured.
  286. */
  287. handler = cxl_ops->psl_interrupt;
  288. else
  289. handler = cxl_irq_afu;
  290. cxl_map_irq(ctx->afu->adapter, hwirq, handler, ctx,
  291. irq_name->name);
  292. irq_name = list_next_entry(irq_name, list);
  293. }
  294. }
  295. }
  296. int afu_register_irqs(struct cxl_context *ctx, u32 count)
  297. {
  298. int rc;
  299. rc = afu_allocate_irqs(ctx, count);
  300. if (rc)
  301. return rc;
  302. afu_register_hwirqs(ctx);
  303. return 0;
  304. }
  305. void afu_release_irqs(struct cxl_context *ctx, void *cookie)
  306. {
  307. irq_hw_number_t hwirq;
  308. unsigned int virq;
  309. int r, i;
  310. for (r = afu_irq_range_start(); r < CXL_IRQ_RANGES; r++) {
  311. hwirq = ctx->irqs.offset[r];
  312. for (i = 0; i < ctx->irqs.range[r]; hwirq++, i++) {
  313. virq = irq_find_mapping(NULL, hwirq);
  314. if (virq)
  315. cxl_unmap_irq(virq, cookie);
  316. }
  317. }
  318. afu_irq_name_free(ctx);
  319. cxl_ops->release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
  320. ctx->irq_count = 0;
  321. }
  322. void cxl_afu_decode_psl_serr(struct cxl_afu *afu, u64 serr)
  323. {
  324. dev_crit(&afu->dev,
  325. "PSL Slice error received. Check AFU for root cause.\n");
  326. dev_crit(&afu->dev, "PSL_SERR_An: 0x%016llx\n", serr);
  327. if (serr & CXL_PSL_SERR_An_afuto)
  328. dev_crit(&afu->dev, "AFU MMIO Timeout\n");
  329. if (serr & CXL_PSL_SERR_An_afudis)
  330. dev_crit(&afu->dev,
  331. "MMIO targeted Accelerator that was not enabled\n");
  332. if (serr & CXL_PSL_SERR_An_afuov)
  333. dev_crit(&afu->dev, "AFU CTAG Overflow\n");
  334. if (serr & CXL_PSL_SERR_An_badsrc)
  335. dev_crit(&afu->dev, "Bad Interrupt Source\n");
  336. if (serr & CXL_PSL_SERR_An_badctx)
  337. dev_crit(&afu->dev, "Bad Context Handle\n");
  338. if (serr & CXL_PSL_SERR_An_llcmdis)
  339. dev_crit(&afu->dev, "LLCMD to Disabled AFU\n");
  340. if (serr & CXL_PSL_SERR_An_llcmdto)
  341. dev_crit(&afu->dev, "LLCMD Timeout to AFU\n");
  342. if (serr & CXL_PSL_SERR_An_afupar)
  343. dev_crit(&afu->dev, "AFU MMIO Parity Error\n");
  344. if (serr & CXL_PSL_SERR_An_afudup)
  345. dev_crit(&afu->dev, "AFU MMIO Duplicate CTAG Error\n");
  346. if (serr & CXL_PSL_SERR_An_AE)
  347. dev_crit(&afu->dev,
  348. "AFU asserted JDONE with JERROR in AFU Directed Mode\n");
  349. }