irq.c 13 KB

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