irq.c 13 KB

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