irq.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. irq_dispose_mapping(virq);
  179. }
  180. int cxl_register_one_irq(struct cxl *adapter,
  181. irq_handler_t handler,
  182. void *cookie,
  183. irq_hw_number_t *dest_hwirq,
  184. unsigned int *dest_virq,
  185. const char *name)
  186. {
  187. int hwirq, virq;
  188. if ((hwirq = cxl_ops->alloc_one_irq(adapter)) < 0)
  189. return hwirq;
  190. if (!(virq = cxl_map_irq(adapter, hwirq, handler, cookie, name)))
  191. goto err;
  192. *dest_hwirq = hwirq;
  193. *dest_virq = virq;
  194. return 0;
  195. err:
  196. cxl_ops->release_one_irq(adapter, hwirq);
  197. return -ENOMEM;
  198. }
  199. void afu_irq_name_free(struct cxl_context *ctx)
  200. {
  201. struct cxl_irq_name *irq_name, *tmp;
  202. list_for_each_entry_safe(irq_name, tmp, &ctx->irq_names, list) {
  203. kfree(irq_name->name);
  204. list_del(&irq_name->list);
  205. kfree(irq_name);
  206. }
  207. }
  208. int afu_allocate_irqs(struct cxl_context *ctx, u32 count)
  209. {
  210. int rc, r, i, j = 1;
  211. struct cxl_irq_name *irq_name;
  212. int alloc_count;
  213. /*
  214. * In native mode, range 0 is reserved for the multiplexed
  215. * PSL interrupt. It has been allocated when the AFU was initialized.
  216. *
  217. * In a guest, the PSL interrupt is not mutliplexed, but per-context,
  218. * and is the first interrupt from range 0. It still needs to be
  219. * allocated, so bump the count by one.
  220. */
  221. if (cpu_has_feature(CPU_FTR_HVMODE))
  222. alloc_count = count;
  223. else
  224. alloc_count = count + 1;
  225. /* Initialize the list head to hold irq names */
  226. INIT_LIST_HEAD(&ctx->irq_names);
  227. if ((rc = cxl_ops->alloc_irq_ranges(&ctx->irqs, ctx->afu->adapter,
  228. alloc_count)))
  229. return rc;
  230. if (cpu_has_feature(CPU_FTR_HVMODE)) {
  231. /* Multiplexed PSL Interrupt */
  232. ctx->irqs.offset[0] = ctx->afu->native->psl_hwirq;
  233. ctx->irqs.range[0] = 1;
  234. }
  235. ctx->irq_count = count;
  236. ctx->irq_bitmap = kcalloc(BITS_TO_LONGS(count),
  237. sizeof(*ctx->irq_bitmap), GFP_KERNEL);
  238. if (!ctx->irq_bitmap)
  239. goto out;
  240. /*
  241. * Allocate names first. If any fail, bail out before allocating
  242. * actual hardware IRQs.
  243. */
  244. for (r = afu_irq_range_start(); r < CXL_IRQ_RANGES; r++) {
  245. for (i = 0; i < ctx->irqs.range[r]; i++) {
  246. irq_name = kmalloc(sizeof(struct cxl_irq_name),
  247. GFP_KERNEL);
  248. if (!irq_name)
  249. goto out;
  250. irq_name->name = kasprintf(GFP_KERNEL, "cxl-%s-pe%i-%i",
  251. dev_name(&ctx->afu->dev),
  252. ctx->pe, j);
  253. if (!irq_name->name) {
  254. kfree(irq_name);
  255. goto out;
  256. }
  257. /* Add to tail so next look get the correct order */
  258. list_add_tail(&irq_name->list, &ctx->irq_names);
  259. j++;
  260. }
  261. }
  262. return 0;
  263. out:
  264. cxl_ops->release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
  265. afu_irq_name_free(ctx);
  266. return -ENOMEM;
  267. }
  268. static void afu_register_hwirqs(struct cxl_context *ctx)
  269. {
  270. irq_hw_number_t hwirq;
  271. struct cxl_irq_name *irq_name;
  272. int r, i;
  273. irqreturn_t (*handler)(int irq, void *data);
  274. /* We've allocated all memory now, so let's do the irq allocations */
  275. irq_name = list_first_entry(&ctx->irq_names, struct cxl_irq_name, list);
  276. for (r = afu_irq_range_start(); r < CXL_IRQ_RANGES; r++) {
  277. hwirq = ctx->irqs.offset[r];
  278. for (i = 0; i < ctx->irqs.range[r]; hwirq++, i++) {
  279. if (r == 0 && i == 0)
  280. /*
  281. * The very first interrupt of range 0 is
  282. * always the PSL interrupt, but we only
  283. * need to connect a handler for guests,
  284. * because there's one PSL interrupt per
  285. * context.
  286. * On bare-metal, the PSL interrupt is
  287. * multiplexed and was setup when the AFU
  288. * was configured.
  289. */
  290. handler = cxl_ops->psl_interrupt;
  291. else
  292. handler = cxl_irq_afu;
  293. cxl_map_irq(ctx->afu->adapter, hwirq, handler, ctx,
  294. irq_name->name);
  295. irq_name = list_next_entry(irq_name, list);
  296. }
  297. }
  298. }
  299. int afu_register_irqs(struct cxl_context *ctx, u32 count)
  300. {
  301. int rc;
  302. rc = afu_allocate_irqs(ctx, count);
  303. if (rc)
  304. return rc;
  305. afu_register_hwirqs(ctx);
  306. return 0;
  307. }
  308. void afu_release_irqs(struct cxl_context *ctx, void *cookie)
  309. {
  310. irq_hw_number_t hwirq;
  311. unsigned int virq;
  312. int r, i;
  313. for (r = afu_irq_range_start(); r < CXL_IRQ_RANGES; r++) {
  314. hwirq = ctx->irqs.offset[r];
  315. for (i = 0; i < ctx->irqs.range[r]; hwirq++, i++) {
  316. virq = irq_find_mapping(NULL, hwirq);
  317. if (virq)
  318. cxl_unmap_irq(virq, cookie);
  319. }
  320. }
  321. afu_irq_name_free(ctx);
  322. cxl_ops->release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
  323. ctx->irq_count = 0;
  324. }