irq.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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.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%.16llx\n", errstat);
  29. dev_crit(&ctx->afu->dev, "PSL_FIR1: 0x%.16llx\n", fir1);
  30. dev_crit(&ctx->afu->dev, "PSL_FIR2: 0x%.16llx\n", fir2);
  31. dev_crit(&ctx->afu->dev, "PSL_SERR_An: 0x%.16llx\n", serr);
  32. dev_crit(&ctx->afu->dev, "PSL_FIR_SLICE_An: 0x%.16llx\n", fir_slice);
  33. dev_crit(&ctx->afu->dev, "CXL_PSL_AFU_DEBUG_An: 0x%.16llx\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%.16llx\n", serr);
  48. dev_crit(&afu->dev, "PSL_FIR_SLICE_An: 0x%.16llx\n", fir_slice);
  49. dev_crit(&afu->dev, "CXL_PSL_ErrStat_An: 0x%.16llx\n", errstat);
  50. dev_crit(&afu->dev, "CXL_PSL_AFU_DEBUG_An: 0x%.16llx\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%.16llx\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%.16llx\nPSL_FIR2: 0x%.16llx\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 %.llx\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: %.llx\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. }
  144. if (dsisr & CXL_PSL_DSISR_An_OC)
  145. pr_devel("CXL interrupt: OS Context Warning\n");
  146. WARN(1, "Unhandled CXL PSL IRQ\n");
  147. return IRQ_HANDLED;
  148. }
  149. static irqreturn_t fail_psl_irq(struct cxl_afu *afu, struct cxl_irq_info *irq_info)
  150. {
  151. if (irq_info->dsisr & CXL_PSL_DSISR_TRANS)
  152. cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_AE);
  153. else
  154. cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_A);
  155. return IRQ_HANDLED;
  156. }
  157. static irqreturn_t cxl_irq_multiplexed(int irq, void *data)
  158. {
  159. struct cxl_afu *afu = data;
  160. struct cxl_context *ctx;
  161. struct cxl_irq_info irq_info;
  162. int ph = cxl_p2n_read(afu, CXL_PSL_PEHandle_An) & 0xffff;
  163. int ret;
  164. if ((ret = cxl_get_irq(afu, &irq_info))) {
  165. WARN(1, "Unable to get CXL IRQ Info: %i\n", ret);
  166. return fail_psl_irq(afu, &irq_info);
  167. }
  168. rcu_read_lock();
  169. ctx = idr_find(&afu->contexts_idr, ph);
  170. if (ctx) {
  171. ret = cxl_irq(irq, ctx, &irq_info);
  172. rcu_read_unlock();
  173. return ret;
  174. }
  175. rcu_read_unlock();
  176. WARN(1, "Unable to demultiplex CXL PSL IRQ for PE %i DSISR %.16llx DAR"
  177. " %.16llx\n(Possible AFU HW issue - was a term/remove acked"
  178. " with outstanding transactions?)\n", ph, irq_info.dsisr,
  179. irq_info.dar);
  180. return fail_psl_irq(afu, &irq_info);
  181. }
  182. static irqreturn_t cxl_irq_afu(int irq, void *data)
  183. {
  184. struct cxl_context *ctx = data;
  185. irq_hw_number_t hwirq = irqd_to_hwirq(irq_get_irq_data(irq));
  186. int irq_off, afu_irq = 1;
  187. __u16 range;
  188. int r;
  189. for (r = 1; r < CXL_IRQ_RANGES; r++) {
  190. irq_off = hwirq - ctx->irqs.offset[r];
  191. range = ctx->irqs.range[r];
  192. if (irq_off >= 0 && irq_off < range) {
  193. afu_irq += irq_off;
  194. break;
  195. }
  196. afu_irq += range;
  197. }
  198. if (unlikely(r >= CXL_IRQ_RANGES)) {
  199. WARN(1, "Recieved AFU IRQ out of range for pe %i (virq %i hwirq %lx)\n",
  200. ctx->pe, irq, hwirq);
  201. return IRQ_HANDLED;
  202. }
  203. trace_cxl_afu_irq(ctx, afu_irq, irq, hwirq);
  204. pr_devel("Received AFU interrupt %i for pe: %i (virq %i hwirq %lx)\n",
  205. afu_irq, ctx->pe, irq, hwirq);
  206. if (unlikely(!ctx->irq_bitmap)) {
  207. WARN(1, "Recieved AFU IRQ for context with no IRQ bitmap\n");
  208. return IRQ_HANDLED;
  209. }
  210. spin_lock(&ctx->lock);
  211. set_bit(afu_irq - 1, ctx->irq_bitmap);
  212. ctx->pending_irq = true;
  213. spin_unlock(&ctx->lock);
  214. wake_up_all(&ctx->wq);
  215. return IRQ_HANDLED;
  216. }
  217. unsigned int cxl_map_irq(struct cxl *adapter, irq_hw_number_t hwirq,
  218. irq_handler_t handler, void *cookie, const char *name)
  219. {
  220. unsigned int virq;
  221. int result;
  222. /* IRQ Domain? */
  223. virq = irq_create_mapping(NULL, hwirq);
  224. if (!virq) {
  225. dev_warn(&adapter->dev, "cxl_map_irq: irq_create_mapping failed\n");
  226. return 0;
  227. }
  228. cxl_setup_irq(adapter, hwirq, virq);
  229. pr_devel("hwirq %#lx mapped to virq %u\n", hwirq, virq);
  230. result = request_irq(virq, handler, 0, name, cookie);
  231. if (result) {
  232. dev_warn(&adapter->dev, "cxl_map_irq: request_irq failed: %i\n", result);
  233. return 0;
  234. }
  235. return virq;
  236. }
  237. void cxl_unmap_irq(unsigned int virq, void *cookie)
  238. {
  239. free_irq(virq, cookie);
  240. irq_dispose_mapping(virq);
  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. cxl_p1_write(adapter, CXL_PSL_ErrIVTE, 0x0000000000000000);
  282. cxl_unmap_irq(adapter->err_virq, adapter);
  283. cxl_release_one_irq(adapter, adapter->err_hwirq);
  284. kfree(adapter->irq_name);
  285. }
  286. int cxl_register_serr_irq(struct cxl_afu *afu)
  287. {
  288. u64 serr;
  289. int rc;
  290. afu->err_irq_name = kasprintf(GFP_KERNEL, "cxl-%s-err",
  291. dev_name(&afu->dev));
  292. if (!afu->err_irq_name)
  293. return -ENOMEM;
  294. if ((rc = cxl_register_one_irq(afu->adapter, cxl_slice_irq_err, afu,
  295. &afu->serr_hwirq,
  296. &afu->serr_virq, afu->err_irq_name))) {
  297. kfree(afu->err_irq_name);
  298. afu->err_irq_name = NULL;
  299. return rc;
  300. }
  301. serr = cxl_p1n_read(afu, CXL_PSL_SERR_An);
  302. serr = (serr & 0x00ffffffffff0000ULL) | (afu->serr_hwirq & 0xffff);
  303. cxl_p1n_write(afu, CXL_PSL_SERR_An, serr);
  304. return 0;
  305. }
  306. void cxl_release_serr_irq(struct cxl_afu *afu)
  307. {
  308. cxl_p1n_write(afu, CXL_PSL_SERR_An, 0x0000000000000000);
  309. cxl_unmap_irq(afu->serr_virq, afu);
  310. cxl_release_one_irq(afu->adapter, afu->serr_hwirq);
  311. kfree(afu->err_irq_name);
  312. }
  313. int cxl_register_psl_irq(struct cxl_afu *afu)
  314. {
  315. int rc;
  316. afu->psl_irq_name = kasprintf(GFP_KERNEL, "cxl-%s",
  317. dev_name(&afu->dev));
  318. if (!afu->psl_irq_name)
  319. return -ENOMEM;
  320. if ((rc = cxl_register_one_irq(afu->adapter, cxl_irq_multiplexed, afu,
  321. &afu->psl_hwirq, &afu->psl_virq,
  322. afu->psl_irq_name))) {
  323. kfree(afu->psl_irq_name);
  324. afu->psl_irq_name = NULL;
  325. }
  326. return rc;
  327. }
  328. void cxl_release_psl_irq(struct cxl_afu *afu)
  329. {
  330. cxl_unmap_irq(afu->psl_virq, afu);
  331. cxl_release_one_irq(afu->adapter, afu->psl_hwirq);
  332. kfree(afu->psl_irq_name);
  333. }
  334. void afu_irq_name_free(struct cxl_context *ctx)
  335. {
  336. struct cxl_irq_name *irq_name, *tmp;
  337. list_for_each_entry_safe(irq_name, tmp, &ctx->irq_names, list) {
  338. kfree(irq_name->name);
  339. list_del(&irq_name->list);
  340. kfree(irq_name);
  341. }
  342. }
  343. int afu_register_irqs(struct cxl_context *ctx, u32 count)
  344. {
  345. irq_hw_number_t hwirq;
  346. int rc, r, i, j = 1;
  347. struct cxl_irq_name *irq_name;
  348. if ((rc = cxl_alloc_irq_ranges(&ctx->irqs, ctx->afu->adapter, count)))
  349. return rc;
  350. /* Multiplexed PSL Interrupt */
  351. ctx->irqs.offset[0] = ctx->afu->psl_hwirq;
  352. ctx->irqs.range[0] = 1;
  353. ctx->irq_count = count;
  354. ctx->irq_bitmap = kcalloc(BITS_TO_LONGS(count),
  355. sizeof(*ctx->irq_bitmap), GFP_KERNEL);
  356. if (!ctx->irq_bitmap)
  357. return -ENOMEM;
  358. /*
  359. * Allocate names first. If any fail, bail out before allocating
  360. * actual hardware IRQs.
  361. */
  362. INIT_LIST_HEAD(&ctx->irq_names);
  363. for (r = 1; r < CXL_IRQ_RANGES; r++) {
  364. for (i = 0; i < ctx->irqs.range[r]; i++) {
  365. irq_name = kmalloc(sizeof(struct cxl_irq_name),
  366. GFP_KERNEL);
  367. if (!irq_name)
  368. goto out;
  369. irq_name->name = kasprintf(GFP_KERNEL, "cxl-%s-pe%i-%i",
  370. dev_name(&ctx->afu->dev),
  371. ctx->pe, j);
  372. if (!irq_name->name) {
  373. kfree(irq_name);
  374. goto out;
  375. }
  376. /* Add to tail so next look get the correct order */
  377. list_add_tail(&irq_name->list, &ctx->irq_names);
  378. j++;
  379. }
  380. }
  381. /* We've allocated all memory now, so let's do the irq allocations */
  382. irq_name = list_first_entry(&ctx->irq_names, struct cxl_irq_name, list);
  383. for (r = 1; r < CXL_IRQ_RANGES; r++) {
  384. hwirq = ctx->irqs.offset[r];
  385. for (i = 0; i < ctx->irqs.range[r]; hwirq++, i++) {
  386. cxl_map_irq(ctx->afu->adapter, hwirq,
  387. cxl_irq_afu, ctx, irq_name->name);
  388. irq_name = list_next_entry(irq_name, list);
  389. }
  390. }
  391. return 0;
  392. out:
  393. afu_irq_name_free(ctx);
  394. return -ENOMEM;
  395. }
  396. void afu_release_irqs(struct cxl_context *ctx)
  397. {
  398. irq_hw_number_t hwirq;
  399. unsigned int virq;
  400. int r, i;
  401. for (r = 1; r < CXL_IRQ_RANGES; r++) {
  402. hwirq = ctx->irqs.offset[r];
  403. for (i = 0; i < ctx->irqs.range[r]; hwirq++, i++) {
  404. virq = irq_find_mapping(NULL, hwirq);
  405. if (virq)
  406. cxl_unmap_irq(virq, ctx);
  407. }
  408. }
  409. afu_irq_name_free(ctx);
  410. cxl_release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
  411. }