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