irq.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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. /* XXX: This is implementation specific */
  20. static irqreturn_t handle_psl_slice_error(struct cxl_context *ctx, u64 dsisr, u64 errstat)
  21. {
  22. u64 fir1, fir2, fir_slice, serr, afu_debug;
  23. fir1 = cxl_p1_read(ctx->afu->adapter, CXL_PSL_FIR1);
  24. fir2 = cxl_p1_read(ctx->afu->adapter, CXL_PSL_FIR2);
  25. fir_slice = cxl_p1n_read(ctx->afu, CXL_PSL_FIR_SLICE_An);
  26. serr = cxl_p1n_read(ctx->afu, CXL_PSL_SERR_An);
  27. afu_debug = cxl_p1n_read(ctx->afu, CXL_AFU_DEBUG_An);
  28. dev_crit(&ctx->afu->dev, "PSL ERROR STATUS: 0x%016llx\n", errstat);
  29. dev_crit(&ctx->afu->dev, "PSL_FIR1: 0x%016llx\n", fir1);
  30. dev_crit(&ctx->afu->dev, "PSL_FIR2: 0x%016llx\n", fir2);
  31. dev_crit(&ctx->afu->dev, "PSL_SERR_An: 0x%016llx\n", serr);
  32. dev_crit(&ctx->afu->dev, "PSL_FIR_SLICE_An: 0x%016llx\n", fir_slice);
  33. dev_crit(&ctx->afu->dev, "CXL_PSL_AFU_DEBUG_An: 0x%016llx\n", afu_debug);
  34. dev_crit(&ctx->afu->dev, "STOPPING CXL TRACE\n");
  35. cxl_stop_trace(ctx->afu->adapter);
  36. return cxl_ack_irq(ctx, 0, errstat);
  37. }
  38. irqreturn_t cxl_slice_irq_err(int irq, void *data)
  39. {
  40. struct cxl_afu *afu = data;
  41. u64 fir_slice, errstat, serr, afu_debug;
  42. WARN(irq, "CXL SLICE ERROR interrupt %i\n", irq);
  43. serr = cxl_p1n_read(afu, CXL_PSL_SERR_An);
  44. fir_slice = cxl_p1n_read(afu, CXL_PSL_FIR_SLICE_An);
  45. errstat = cxl_p2n_read(afu, CXL_PSL_ErrStat_An);
  46. afu_debug = cxl_p1n_read(afu, CXL_AFU_DEBUG_An);
  47. dev_crit(&afu->dev, "PSL_SERR_An: 0x%016llx\n", serr);
  48. dev_crit(&afu->dev, "PSL_FIR_SLICE_An: 0x%016llx\n", fir_slice);
  49. dev_crit(&afu->dev, "CXL_PSL_ErrStat_An: 0x%016llx\n", errstat);
  50. dev_crit(&afu->dev, "CXL_PSL_AFU_DEBUG_An: 0x%016llx\n", afu_debug);
  51. cxl_p1n_write(afu, CXL_PSL_SERR_An, serr);
  52. return IRQ_HANDLED;
  53. }
  54. static irqreturn_t cxl_irq_err(int irq, void *data)
  55. {
  56. struct cxl *adapter = data;
  57. u64 fir1, fir2, err_ivte;
  58. WARN(1, "CXL ERROR interrupt %i\n", irq);
  59. err_ivte = cxl_p1_read(adapter, CXL_PSL_ErrIVTE);
  60. dev_crit(&adapter->dev, "PSL_ErrIVTE: 0x%016llx\n", err_ivte);
  61. dev_crit(&adapter->dev, "STOPPING CXL TRACE\n");
  62. cxl_stop_trace(adapter);
  63. fir1 = cxl_p1_read(adapter, CXL_PSL_FIR1);
  64. fir2 = cxl_p1_read(adapter, CXL_PSL_FIR2);
  65. dev_crit(&adapter->dev, "PSL_FIR1: 0x%016llx\nPSL_FIR2: 0x%016llx\n", fir1, fir2);
  66. return IRQ_HANDLED;
  67. }
  68. static irqreturn_t schedule_cxl_fault(struct cxl_context *ctx, u64 dsisr, u64 dar)
  69. {
  70. ctx->dsisr = dsisr;
  71. ctx->dar = dar;
  72. schedule_work(&ctx->fault_work);
  73. return IRQ_HANDLED;
  74. }
  75. static irqreturn_t cxl_irq(int irq, void *data, struct cxl_irq_info *irq_info)
  76. {
  77. struct cxl_context *ctx = data;
  78. u64 dsisr, dar;
  79. dsisr = irq_info->dsisr;
  80. dar = irq_info->dar;
  81. trace_cxl_psl_irq(ctx, irq, dsisr, dar);
  82. pr_devel("CXL interrupt %i for afu pe: %i DSISR: %#llx DAR: %#llx\n", irq, ctx->pe, dsisr, dar);
  83. if (dsisr & CXL_PSL_DSISR_An_DS) {
  84. /*
  85. * We don't inherently need to sleep to handle this, but we do
  86. * need to get a ref to the task's mm, which we can't do from
  87. * irq context without the potential for a deadlock since it
  88. * takes the task_lock. An alternate option would be to keep a
  89. * reference to the task's mm the entire time it has cxl open,
  90. * but to do that we need to solve the issue where we hold a
  91. * ref to the mm, but the mm can hold a ref to the fd after an
  92. * mmap preventing anything from being cleaned up.
  93. */
  94. pr_devel("Scheduling segment miss handling for later pe: %i\n", ctx->pe);
  95. return schedule_cxl_fault(ctx, dsisr, dar);
  96. }
  97. if (dsisr & CXL_PSL_DSISR_An_M)
  98. pr_devel("CXL interrupt: PTE not found\n");
  99. if (dsisr & CXL_PSL_DSISR_An_P)
  100. pr_devel("CXL interrupt: Storage protection violation\n");
  101. if (dsisr & CXL_PSL_DSISR_An_A)
  102. pr_devel("CXL interrupt: AFU lock access to write through or cache inhibited storage\n");
  103. if (dsisr & CXL_PSL_DSISR_An_S)
  104. pr_devel("CXL interrupt: Access was afu_wr or afu_zero\n");
  105. if (dsisr & CXL_PSL_DSISR_An_K)
  106. pr_devel("CXL interrupt: Access not permitted by virtual page class key protection\n");
  107. if (dsisr & CXL_PSL_DSISR_An_DM) {
  108. /*
  109. * In some cases we might be able to handle the fault
  110. * immediately if hash_page would succeed, but we still need
  111. * the task's mm, which as above we can't get without a lock
  112. */
  113. pr_devel("Scheduling page fault handling for later pe: %i\n", ctx->pe);
  114. return schedule_cxl_fault(ctx, dsisr, dar);
  115. }
  116. if (dsisr & CXL_PSL_DSISR_An_ST)
  117. WARN(1, "CXL interrupt: Segment Table PTE not found\n");
  118. if (dsisr & CXL_PSL_DSISR_An_UR)
  119. pr_devel("CXL interrupt: AURP PTE not found\n");
  120. if (dsisr & CXL_PSL_DSISR_An_PE)
  121. return handle_psl_slice_error(ctx, dsisr, irq_info->errstat);
  122. if (dsisr & CXL_PSL_DSISR_An_AE) {
  123. pr_devel("CXL interrupt: AFU Error 0x%016llx\n", irq_info->afu_err);
  124. if (ctx->pending_afu_err) {
  125. /*
  126. * This shouldn't happen - the PSL treats these errors
  127. * as fatal and will have reset the AFU, so there's not
  128. * much point buffering multiple AFU errors.
  129. * OTOH if we DO ever see a storm of these come in it's
  130. * probably best that we log them somewhere:
  131. */
  132. dev_err_ratelimited(&ctx->afu->dev, "CXL AFU Error "
  133. "undelivered to pe %i: 0x%016llx\n",
  134. ctx->pe, irq_info->afu_err);
  135. } else {
  136. spin_lock(&ctx->lock);
  137. ctx->afu_err = irq_info->afu_err;
  138. ctx->pending_afu_err = 1;
  139. spin_unlock(&ctx->lock);
  140. wake_up_all(&ctx->wq);
  141. }
  142. cxl_ack_irq(ctx, CXL_PSL_TFC_An_A, 0);
  143. return IRQ_HANDLED;
  144. }
  145. if (dsisr & CXL_PSL_DSISR_An_OC)
  146. pr_devel("CXL interrupt: OS Context Warning\n");
  147. WARN(1, "Unhandled CXL PSL IRQ\n");
  148. return IRQ_HANDLED;
  149. }
  150. static irqreturn_t fail_psl_irq(struct cxl_afu *afu, struct cxl_irq_info *irq_info)
  151. {
  152. if (irq_info->dsisr & CXL_PSL_DSISR_TRANS)
  153. cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_AE);
  154. else
  155. cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_A);
  156. return IRQ_HANDLED;
  157. }
  158. static irqreturn_t cxl_irq_multiplexed(int irq, void *data)
  159. {
  160. struct cxl_afu *afu = data;
  161. struct cxl_context *ctx;
  162. struct cxl_irq_info irq_info;
  163. int ph = cxl_p2n_read(afu, CXL_PSL_PEHandle_An) & 0xffff;
  164. int ret;
  165. if ((ret = cxl_get_irq(afu, &irq_info))) {
  166. WARN(1, "Unable to get CXL IRQ Info: %i\n", ret);
  167. return fail_psl_irq(afu, &irq_info);
  168. }
  169. rcu_read_lock();
  170. ctx = idr_find(&afu->contexts_idr, ph);
  171. if (ctx) {
  172. ret = cxl_irq(irq, ctx, &irq_info);
  173. rcu_read_unlock();
  174. return ret;
  175. }
  176. rcu_read_unlock();
  177. WARN(1, "Unable to demultiplex CXL PSL IRQ for PE %i DSISR %016llx DAR"
  178. " %016llx\n(Possible AFU HW issue - was a term/remove acked"
  179. " with outstanding transactions?)\n", ph, irq_info.dsisr,
  180. irq_info.dar);
  181. return fail_psl_irq(afu, &irq_info);
  182. }
  183. static irqreturn_t cxl_irq_afu(int irq, void *data)
  184. {
  185. struct cxl_context *ctx = data;
  186. irq_hw_number_t hwirq = irqd_to_hwirq(irq_get_irq_data(irq));
  187. int irq_off, afu_irq = 1;
  188. __u16 range;
  189. int r;
  190. for (r = 1; r < CXL_IRQ_RANGES; r++) {
  191. irq_off = hwirq - ctx->irqs.offset[r];
  192. range = ctx->irqs.range[r];
  193. if (irq_off >= 0 && irq_off < range) {
  194. afu_irq += irq_off;
  195. break;
  196. }
  197. afu_irq += range;
  198. }
  199. if (unlikely(r >= CXL_IRQ_RANGES)) {
  200. WARN(1, "Recieved AFU IRQ out of range for pe %i (virq %i hwirq %lx)\n",
  201. ctx->pe, irq, hwirq);
  202. return IRQ_HANDLED;
  203. }
  204. trace_cxl_afu_irq(ctx, afu_irq, irq, hwirq);
  205. pr_devel("Received AFU interrupt %i for pe: %i (virq %i hwirq %lx)\n",
  206. afu_irq, ctx->pe, irq, hwirq);
  207. if (unlikely(!ctx->irq_bitmap)) {
  208. WARN(1, "Recieved AFU IRQ for context with no IRQ bitmap\n");
  209. return IRQ_HANDLED;
  210. }
  211. spin_lock(&ctx->lock);
  212. set_bit(afu_irq - 1, ctx->irq_bitmap);
  213. ctx->pending_irq = true;
  214. spin_unlock(&ctx->lock);
  215. wake_up_all(&ctx->wq);
  216. return IRQ_HANDLED;
  217. }
  218. unsigned int cxl_map_irq(struct cxl *adapter, irq_hw_number_t hwirq,
  219. irq_handler_t handler, void *cookie, const char *name)
  220. {
  221. unsigned int virq;
  222. int result;
  223. /* IRQ Domain? */
  224. virq = irq_create_mapping(NULL, hwirq);
  225. if (!virq) {
  226. dev_warn(&adapter->dev, "cxl_map_irq: irq_create_mapping failed\n");
  227. return 0;
  228. }
  229. cxl_setup_irq(adapter, hwirq, virq);
  230. pr_devel("hwirq %#lx mapped to virq %u\n", hwirq, virq);
  231. result = request_irq(virq, handler, 0, name, cookie);
  232. if (result) {
  233. dev_warn(&adapter->dev, "cxl_map_irq: request_irq failed: %i\n", result);
  234. return 0;
  235. }
  236. return virq;
  237. }
  238. void cxl_unmap_irq(unsigned int virq, void *cookie)
  239. {
  240. free_irq(virq, cookie);
  241. }
  242. static int cxl_register_one_irq(struct cxl *adapter,
  243. irq_handler_t handler,
  244. void *cookie,
  245. irq_hw_number_t *dest_hwirq,
  246. unsigned int *dest_virq,
  247. const char *name)
  248. {
  249. int hwirq, virq;
  250. if ((hwirq = cxl_alloc_one_irq(adapter)) < 0)
  251. return hwirq;
  252. if (!(virq = cxl_map_irq(adapter, hwirq, handler, cookie, name)))
  253. goto err;
  254. *dest_hwirq = hwirq;
  255. *dest_virq = virq;
  256. return 0;
  257. err:
  258. cxl_release_one_irq(adapter, hwirq);
  259. return -ENOMEM;
  260. }
  261. int cxl_register_psl_err_irq(struct cxl *adapter)
  262. {
  263. int rc;
  264. adapter->irq_name = kasprintf(GFP_KERNEL, "cxl-%s-err",
  265. dev_name(&adapter->dev));
  266. if (!adapter->irq_name)
  267. return -ENOMEM;
  268. if ((rc = cxl_register_one_irq(adapter, cxl_irq_err, adapter,
  269. &adapter->err_hwirq,
  270. &adapter->err_virq,
  271. adapter->irq_name))) {
  272. kfree(adapter->irq_name);
  273. adapter->irq_name = NULL;
  274. return rc;
  275. }
  276. cxl_p1_write(adapter, CXL_PSL_ErrIVTE, adapter->err_hwirq & 0xffff);
  277. return 0;
  278. }
  279. void cxl_release_psl_err_irq(struct cxl *adapter)
  280. {
  281. if (adapter->err_virq != irq_find_mapping(NULL, adapter->err_hwirq))
  282. return;
  283. cxl_p1_write(adapter, CXL_PSL_ErrIVTE, 0x0000000000000000);
  284. cxl_unmap_irq(adapter->err_virq, adapter);
  285. cxl_release_one_irq(adapter, adapter->err_hwirq);
  286. kfree(adapter->irq_name);
  287. }
  288. int cxl_register_serr_irq(struct cxl_afu *afu)
  289. {
  290. u64 serr;
  291. int rc;
  292. afu->err_irq_name = kasprintf(GFP_KERNEL, "cxl-%s-err",
  293. dev_name(&afu->dev));
  294. if (!afu->err_irq_name)
  295. return -ENOMEM;
  296. if ((rc = cxl_register_one_irq(afu->adapter, cxl_slice_irq_err, afu,
  297. &afu->serr_hwirq,
  298. &afu->serr_virq, afu->err_irq_name))) {
  299. kfree(afu->err_irq_name);
  300. afu->err_irq_name = NULL;
  301. return rc;
  302. }
  303. serr = cxl_p1n_read(afu, CXL_PSL_SERR_An);
  304. serr = (serr & 0x00ffffffffff0000ULL) | (afu->serr_hwirq & 0xffff);
  305. cxl_p1n_write(afu, CXL_PSL_SERR_An, serr);
  306. return 0;
  307. }
  308. void cxl_release_serr_irq(struct cxl_afu *afu)
  309. {
  310. if (afu->serr_virq != irq_find_mapping(NULL, afu->serr_hwirq))
  311. return;
  312. cxl_p1n_write(afu, CXL_PSL_SERR_An, 0x0000000000000000);
  313. cxl_unmap_irq(afu->serr_virq, afu);
  314. cxl_release_one_irq(afu->adapter, afu->serr_hwirq);
  315. kfree(afu->err_irq_name);
  316. }
  317. int cxl_register_psl_irq(struct cxl_afu *afu)
  318. {
  319. int rc;
  320. afu->psl_irq_name = kasprintf(GFP_KERNEL, "cxl-%s",
  321. dev_name(&afu->dev));
  322. if (!afu->psl_irq_name)
  323. return -ENOMEM;
  324. if ((rc = cxl_register_one_irq(afu->adapter, cxl_irq_multiplexed, afu,
  325. &afu->psl_hwirq, &afu->psl_virq,
  326. afu->psl_irq_name))) {
  327. kfree(afu->psl_irq_name);
  328. afu->psl_irq_name = NULL;
  329. }
  330. return rc;
  331. }
  332. void cxl_release_psl_irq(struct cxl_afu *afu)
  333. {
  334. if (afu->psl_virq != irq_find_mapping(NULL, afu->psl_hwirq))
  335. return;
  336. cxl_unmap_irq(afu->psl_virq, afu);
  337. cxl_release_one_irq(afu->adapter, afu->psl_hwirq);
  338. kfree(afu->psl_irq_name);
  339. }
  340. void afu_irq_name_free(struct cxl_context *ctx)
  341. {
  342. struct cxl_irq_name *irq_name, *tmp;
  343. list_for_each_entry_safe(irq_name, tmp, &ctx->irq_names, list) {
  344. kfree(irq_name->name);
  345. list_del(&irq_name->list);
  346. kfree(irq_name);
  347. }
  348. }
  349. int afu_allocate_irqs(struct cxl_context *ctx, u32 count)
  350. {
  351. int rc, r, i, j = 1;
  352. struct cxl_irq_name *irq_name;
  353. /* Initialize the list head to hold irq names */
  354. INIT_LIST_HEAD(&ctx->irq_names);
  355. if ((rc = cxl_alloc_irq_ranges(&ctx->irqs, ctx->afu->adapter, count)))
  356. return rc;
  357. /* Multiplexed PSL Interrupt */
  358. ctx->irqs.offset[0] = ctx->afu->psl_hwirq;
  359. ctx->irqs.range[0] = 1;
  360. ctx->irq_count = count;
  361. ctx->irq_bitmap = kcalloc(BITS_TO_LONGS(count),
  362. sizeof(*ctx->irq_bitmap), GFP_KERNEL);
  363. if (!ctx->irq_bitmap)
  364. goto out;
  365. /*
  366. * Allocate names first. If any fail, bail out before allocating
  367. * actual hardware IRQs.
  368. */
  369. for (r = 1; r < CXL_IRQ_RANGES; r++) {
  370. for (i = 0; i < ctx->irqs.range[r]; i++) {
  371. irq_name = kmalloc(sizeof(struct cxl_irq_name),
  372. GFP_KERNEL);
  373. if (!irq_name)
  374. goto out;
  375. irq_name->name = kasprintf(GFP_KERNEL, "cxl-%s-pe%i-%i",
  376. dev_name(&ctx->afu->dev),
  377. ctx->pe, j);
  378. if (!irq_name->name) {
  379. kfree(irq_name);
  380. goto out;
  381. }
  382. /* Add to tail so next look get the correct order */
  383. list_add_tail(&irq_name->list, &ctx->irq_names);
  384. j++;
  385. }
  386. }
  387. return 0;
  388. out:
  389. cxl_release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
  390. afu_irq_name_free(ctx);
  391. return -ENOMEM;
  392. }
  393. static void afu_register_hwirqs(struct cxl_context *ctx)
  394. {
  395. irq_hw_number_t hwirq;
  396. struct cxl_irq_name *irq_name;
  397. int r,i;
  398. /* We've allocated all memory now, so let's do the irq allocations */
  399. irq_name = list_first_entry(&ctx->irq_names, struct cxl_irq_name, list);
  400. for (r = 1; r < CXL_IRQ_RANGES; r++) {
  401. hwirq = ctx->irqs.offset[r];
  402. for (i = 0; i < ctx->irqs.range[r]; hwirq++, i++) {
  403. cxl_map_irq(ctx->afu->adapter, hwirq,
  404. cxl_irq_afu, ctx, irq_name->name);
  405. irq_name = list_next_entry(irq_name, list);
  406. }
  407. }
  408. }
  409. int afu_register_irqs(struct cxl_context *ctx, u32 count)
  410. {
  411. int rc;
  412. rc = afu_allocate_irqs(ctx, count);
  413. if (rc)
  414. return rc;
  415. afu_register_hwirqs(ctx);
  416. return 0;
  417. }
  418. void afu_release_irqs(struct cxl_context *ctx, void *cookie)
  419. {
  420. irq_hw_number_t hwirq;
  421. unsigned int virq;
  422. int r, i;
  423. for (r = 1; r < CXL_IRQ_RANGES; r++) {
  424. hwirq = ctx->irqs.offset[r];
  425. for (i = 0; i < ctx->irqs.range[r]; hwirq++, i++) {
  426. virq = irq_find_mapping(NULL, hwirq);
  427. if (virq)
  428. cxl_unmap_irq(virq, cookie);
  429. }
  430. }
  431. afu_irq_name_free(ctx);
  432. cxl_release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
  433. ctx->irq_count = 0;
  434. }