xref: /openbmc/linux/drivers/misc/cxl/irq.c (revision a6897f39660cc07fa78b4459d82f12b07abb50b1)
1f204e0b8SIan Munsie /*
2f204e0b8SIan Munsie  * Copyright 2014 IBM Corp.
3f204e0b8SIan Munsie  *
4f204e0b8SIan Munsie  * This program is free software; you can redistribute it and/or
5f204e0b8SIan Munsie  * modify it under the terms of the GNU General Public License
6f204e0b8SIan Munsie  * as published by the Free Software Foundation; either version
7f204e0b8SIan Munsie  * 2 of the License, or (at your option) any later version.
8f204e0b8SIan Munsie  */
9f204e0b8SIan Munsie 
10f204e0b8SIan Munsie #include <linux/interrupt.h>
11f204e0b8SIan Munsie #include <linux/workqueue.h>
12f204e0b8SIan Munsie #include <linux/sched.h>
13f204e0b8SIan Munsie #include <linux/wait.h>
14f204e0b8SIan Munsie #include <linux/slab.h>
15f204e0b8SIan Munsie #include <linux/pid.h>
16f204e0b8SIan Munsie #include <asm/cputable.h>
17ec249dd8SMichael Neuling #include <misc/cxl-base.h>
18f204e0b8SIan Munsie 
19f204e0b8SIan Munsie #include "cxl.h"
209bcf28cdSIan Munsie #include "trace.h"
21f204e0b8SIan Munsie 
22f204e0b8SIan Munsie /* XXX: This is implementation specific */
23f204e0b8SIan Munsie static irqreturn_t handle_psl_slice_error(struct cxl_context *ctx, u64 dsisr, u64 errstat)
24f204e0b8SIan Munsie {
25f204e0b8SIan Munsie 	u64 fir1, fir2, fir_slice, serr, afu_debug;
26f204e0b8SIan Munsie 
27f204e0b8SIan Munsie 	fir1 = cxl_p1_read(ctx->afu->adapter, CXL_PSL_FIR1);
28f204e0b8SIan Munsie 	fir2 = cxl_p1_read(ctx->afu->adapter, CXL_PSL_FIR2);
29f204e0b8SIan Munsie 	fir_slice = cxl_p1n_read(ctx->afu, CXL_PSL_FIR_SLICE_An);
30f204e0b8SIan Munsie 	serr = cxl_p1n_read(ctx->afu, CXL_PSL_SERR_An);
31f204e0b8SIan Munsie 	afu_debug = cxl_p1n_read(ctx->afu, CXL_AFU_DEBUG_An);
32f204e0b8SIan Munsie 
33de369538SRasmus Villemoes 	dev_crit(&ctx->afu->dev, "PSL ERROR STATUS: 0x%016llx\n", errstat);
34de369538SRasmus Villemoes 	dev_crit(&ctx->afu->dev, "PSL_FIR1: 0x%016llx\n", fir1);
35de369538SRasmus Villemoes 	dev_crit(&ctx->afu->dev, "PSL_FIR2: 0x%016llx\n", fir2);
36de369538SRasmus Villemoes 	dev_crit(&ctx->afu->dev, "PSL_SERR_An: 0x%016llx\n", serr);
37de369538SRasmus Villemoes 	dev_crit(&ctx->afu->dev, "PSL_FIR_SLICE_An: 0x%016llx\n", fir_slice);
38de369538SRasmus Villemoes 	dev_crit(&ctx->afu->dev, "CXL_PSL_AFU_DEBUG_An: 0x%016llx\n", afu_debug);
39f204e0b8SIan Munsie 
40f204e0b8SIan Munsie 	dev_crit(&ctx->afu->dev, "STOPPING CXL TRACE\n");
41f204e0b8SIan Munsie 	cxl_stop_trace(ctx->afu->adapter);
42f204e0b8SIan Munsie 
43f204e0b8SIan Munsie 	return cxl_ack_irq(ctx, 0, errstat);
44f204e0b8SIan Munsie }
45f204e0b8SIan Munsie 
46f204e0b8SIan Munsie irqreturn_t cxl_slice_irq_err(int irq, void *data)
47f204e0b8SIan Munsie {
48f204e0b8SIan Munsie 	struct cxl_afu *afu = data;
49f204e0b8SIan Munsie 	u64 fir_slice, errstat, serr, afu_debug;
50f204e0b8SIan Munsie 
51f204e0b8SIan Munsie 	WARN(irq, "CXL SLICE ERROR interrupt %i\n", irq);
52f204e0b8SIan Munsie 
53f204e0b8SIan Munsie 	serr = cxl_p1n_read(afu, CXL_PSL_SERR_An);
54f204e0b8SIan Munsie 	fir_slice = cxl_p1n_read(afu, CXL_PSL_FIR_SLICE_An);
55f204e0b8SIan Munsie 	errstat = cxl_p2n_read(afu, CXL_PSL_ErrStat_An);
56f204e0b8SIan Munsie 	afu_debug = cxl_p1n_read(afu, CXL_AFU_DEBUG_An);
57de369538SRasmus Villemoes 	dev_crit(&afu->dev, "PSL_SERR_An: 0x%016llx\n", serr);
58de369538SRasmus Villemoes 	dev_crit(&afu->dev, "PSL_FIR_SLICE_An: 0x%016llx\n", fir_slice);
59de369538SRasmus Villemoes 	dev_crit(&afu->dev, "CXL_PSL_ErrStat_An: 0x%016llx\n", errstat);
60de369538SRasmus Villemoes 	dev_crit(&afu->dev, "CXL_PSL_AFU_DEBUG_An: 0x%016llx\n", afu_debug);
61f204e0b8SIan Munsie 
62f204e0b8SIan Munsie 	cxl_p1n_write(afu, CXL_PSL_SERR_An, serr);
63f204e0b8SIan Munsie 
64f204e0b8SIan Munsie 	return IRQ_HANDLED;
65f204e0b8SIan Munsie }
66f204e0b8SIan Munsie 
67f204e0b8SIan Munsie static irqreturn_t cxl_irq_err(int irq, void *data)
68f204e0b8SIan Munsie {
69f204e0b8SIan Munsie 	struct cxl *adapter = data;
70f204e0b8SIan Munsie 	u64 fir1, fir2, err_ivte;
71f204e0b8SIan Munsie 
72f204e0b8SIan Munsie 	WARN(1, "CXL ERROR interrupt %i\n", irq);
73f204e0b8SIan Munsie 
74f204e0b8SIan Munsie 	err_ivte = cxl_p1_read(adapter, CXL_PSL_ErrIVTE);
75de369538SRasmus Villemoes 	dev_crit(&adapter->dev, "PSL_ErrIVTE: 0x%016llx\n", err_ivte);
76f204e0b8SIan Munsie 
77f204e0b8SIan Munsie 	dev_crit(&adapter->dev, "STOPPING CXL TRACE\n");
78f204e0b8SIan Munsie 	cxl_stop_trace(adapter);
79f204e0b8SIan Munsie 
80f204e0b8SIan Munsie 	fir1 = cxl_p1_read(adapter, CXL_PSL_FIR1);
81f204e0b8SIan Munsie 	fir2 = cxl_p1_read(adapter, CXL_PSL_FIR2);
82f204e0b8SIan Munsie 
83de369538SRasmus Villemoes 	dev_crit(&adapter->dev, "PSL_FIR1: 0x%016llx\nPSL_FIR2: 0x%016llx\n", fir1, fir2);
84f204e0b8SIan Munsie 
85f204e0b8SIan Munsie 	return IRQ_HANDLED;
86f204e0b8SIan Munsie }
87f204e0b8SIan Munsie 
88f204e0b8SIan Munsie static irqreturn_t schedule_cxl_fault(struct cxl_context *ctx, u64 dsisr, u64 dar)
89f204e0b8SIan Munsie {
90f204e0b8SIan Munsie 	ctx->dsisr = dsisr;
91f204e0b8SIan Munsie 	ctx->dar = dar;
92f204e0b8SIan Munsie 	schedule_work(&ctx->fault_work);
93f204e0b8SIan Munsie 	return IRQ_HANDLED;
94f204e0b8SIan Munsie }
95f204e0b8SIan Munsie 
96bc78b05bSIan Munsie static irqreturn_t cxl_irq(int irq, void *data, struct cxl_irq_info *irq_info)
97f204e0b8SIan Munsie {
98f204e0b8SIan Munsie 	struct cxl_context *ctx = data;
99f204e0b8SIan Munsie 	u64 dsisr, dar;
100f204e0b8SIan Munsie 
101bc78b05bSIan Munsie 	dsisr = irq_info->dsisr;
102bc78b05bSIan Munsie 	dar = irq_info->dar;
103f204e0b8SIan Munsie 
1049bcf28cdSIan Munsie 	trace_cxl_psl_irq(ctx, irq, dsisr, dar);
1059bcf28cdSIan Munsie 
106f204e0b8SIan Munsie 	pr_devel("CXL interrupt %i for afu pe: %i DSISR: %#llx DAR: %#llx\n", irq, ctx->pe, dsisr, dar);
107f204e0b8SIan Munsie 
108f204e0b8SIan Munsie 	if (dsisr & CXL_PSL_DSISR_An_DS) {
109f204e0b8SIan Munsie 		/*
110f204e0b8SIan Munsie 		 * We don't inherently need to sleep to handle this, but we do
111f204e0b8SIan Munsie 		 * need to get a ref to the task's mm, which we can't do from
112f204e0b8SIan Munsie 		 * irq context without the potential for a deadlock since it
113f204e0b8SIan Munsie 		 * takes the task_lock. An alternate option would be to keep a
114f204e0b8SIan Munsie 		 * reference to the task's mm the entire time it has cxl open,
115f204e0b8SIan Munsie 		 * but to do that we need to solve the issue where we hold a
116f204e0b8SIan Munsie 		 * ref to the mm, but the mm can hold a ref to the fd after an
117f204e0b8SIan Munsie 		 * mmap preventing anything from being cleaned up.
118f204e0b8SIan Munsie 		 */
119f204e0b8SIan Munsie 		pr_devel("Scheduling segment miss handling for later pe: %i\n", ctx->pe);
120f204e0b8SIan Munsie 		return schedule_cxl_fault(ctx, dsisr, dar);
121f204e0b8SIan Munsie 	}
122f204e0b8SIan Munsie 
123f204e0b8SIan Munsie 	if (dsisr & CXL_PSL_DSISR_An_M)
124f204e0b8SIan Munsie 		pr_devel("CXL interrupt: PTE not found\n");
125f204e0b8SIan Munsie 	if (dsisr & CXL_PSL_DSISR_An_P)
126f204e0b8SIan Munsie 		pr_devel("CXL interrupt: Storage protection violation\n");
127f204e0b8SIan Munsie 	if (dsisr & CXL_PSL_DSISR_An_A)
128f204e0b8SIan Munsie 		pr_devel("CXL interrupt: AFU lock access to write through or cache inhibited storage\n");
129f204e0b8SIan Munsie 	if (dsisr & CXL_PSL_DSISR_An_S)
130f204e0b8SIan Munsie 		pr_devel("CXL interrupt: Access was afu_wr or afu_zero\n");
131f204e0b8SIan Munsie 	if (dsisr & CXL_PSL_DSISR_An_K)
132f204e0b8SIan Munsie 		pr_devel("CXL interrupt: Access not permitted by virtual page class key protection\n");
133f204e0b8SIan Munsie 
134f204e0b8SIan Munsie 	if (dsisr & CXL_PSL_DSISR_An_DM) {
135f204e0b8SIan Munsie 		/*
136f204e0b8SIan Munsie 		 * In some cases we might be able to handle the fault
137f204e0b8SIan Munsie 		 * immediately if hash_page would succeed, but we still need
138f204e0b8SIan Munsie 		 * the task's mm, which as above we can't get without a lock
139f204e0b8SIan Munsie 		 */
140f204e0b8SIan Munsie 		pr_devel("Scheduling page fault handling for later pe: %i\n", ctx->pe);
141f204e0b8SIan Munsie 		return schedule_cxl_fault(ctx, dsisr, dar);
142f204e0b8SIan Munsie 	}
143f204e0b8SIan Munsie 	if (dsisr & CXL_PSL_DSISR_An_ST)
144f204e0b8SIan Munsie 		WARN(1, "CXL interrupt: Segment Table PTE not found\n");
145f204e0b8SIan Munsie 	if (dsisr & CXL_PSL_DSISR_An_UR)
146f204e0b8SIan Munsie 		pr_devel("CXL interrupt: AURP PTE not found\n");
147f204e0b8SIan Munsie 	if (dsisr & CXL_PSL_DSISR_An_PE)
148bc78b05bSIan Munsie 		return handle_psl_slice_error(ctx, dsisr, irq_info->errstat);
149f204e0b8SIan Munsie 	if (dsisr & CXL_PSL_DSISR_An_AE) {
150de369538SRasmus Villemoes 		pr_devel("CXL interrupt: AFU Error 0x%016llx\n", irq_info->afu_err);
151f204e0b8SIan Munsie 
152f204e0b8SIan Munsie 		if (ctx->pending_afu_err) {
153f204e0b8SIan Munsie 			/*
154f204e0b8SIan Munsie 			 * This shouldn't happen - the PSL treats these errors
155f204e0b8SIan Munsie 			 * as fatal and will have reset the AFU, so there's not
156f204e0b8SIan Munsie 			 * much point buffering multiple AFU errors.
157f204e0b8SIan Munsie 			 * OTOH if we DO ever see a storm of these come in it's
158f204e0b8SIan Munsie 			 * probably best that we log them somewhere:
159f204e0b8SIan Munsie 			 */
160f204e0b8SIan Munsie 			dev_err_ratelimited(&ctx->afu->dev, "CXL AFU Error "
161de369538SRasmus Villemoes 					    "undelivered to pe %i: 0x%016llx\n",
162bc78b05bSIan Munsie 					    ctx->pe, irq_info->afu_err);
163f204e0b8SIan Munsie 		} else {
164f204e0b8SIan Munsie 			spin_lock(&ctx->lock);
165bc78b05bSIan Munsie 			ctx->afu_err = irq_info->afu_err;
166f204e0b8SIan Munsie 			ctx->pending_afu_err = 1;
167f204e0b8SIan Munsie 			spin_unlock(&ctx->lock);
168f204e0b8SIan Munsie 
169f204e0b8SIan Munsie 			wake_up_all(&ctx->wq);
170f204e0b8SIan Munsie 		}
171f204e0b8SIan Munsie 
172f204e0b8SIan Munsie 		cxl_ack_irq(ctx, CXL_PSL_TFC_An_A, 0);
173a6130ed2SIan Munsie 		return IRQ_HANDLED;
174f204e0b8SIan Munsie 	}
175f204e0b8SIan Munsie 	if (dsisr & CXL_PSL_DSISR_An_OC)
176f204e0b8SIan Munsie 		pr_devel("CXL interrupt: OS Context Warning\n");
177f204e0b8SIan Munsie 
178f204e0b8SIan Munsie 	WARN(1, "Unhandled CXL PSL IRQ\n");
179f204e0b8SIan Munsie 	return IRQ_HANDLED;
180f204e0b8SIan Munsie }
181f204e0b8SIan Munsie 
182bc78b05bSIan Munsie static irqreturn_t fail_psl_irq(struct cxl_afu *afu, struct cxl_irq_info *irq_info)
183bc78b05bSIan Munsie {
184bc78b05bSIan Munsie 	if (irq_info->dsisr & CXL_PSL_DSISR_TRANS)
185bc78b05bSIan Munsie 		cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_AE);
186bc78b05bSIan Munsie 	else
187bc78b05bSIan Munsie 		cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_A);
188bc78b05bSIan Munsie 
189bc78b05bSIan Munsie 	return IRQ_HANDLED;
190bc78b05bSIan Munsie }
191bc78b05bSIan Munsie 
192f204e0b8SIan Munsie static irqreturn_t cxl_irq_multiplexed(int irq, void *data)
193f204e0b8SIan Munsie {
194f204e0b8SIan Munsie 	struct cxl_afu *afu = data;
195f204e0b8SIan Munsie 	struct cxl_context *ctx;
196bc78b05bSIan Munsie 	struct cxl_irq_info irq_info;
197f204e0b8SIan Munsie 	int ph = cxl_p2n_read(afu, CXL_PSL_PEHandle_An) & 0xffff;
198f204e0b8SIan Munsie 	int ret;
199f204e0b8SIan Munsie 
200bc78b05bSIan Munsie 	if ((ret = cxl_get_irq(afu, &irq_info))) {
201bc78b05bSIan Munsie 		WARN(1, "Unable to get CXL IRQ Info: %i\n", ret);
202bc78b05bSIan Munsie 		return fail_psl_irq(afu, &irq_info);
203bc78b05bSIan Munsie 	}
204bc78b05bSIan Munsie 
205f204e0b8SIan Munsie 	rcu_read_lock();
206f204e0b8SIan Munsie 	ctx = idr_find(&afu->contexts_idr, ph);
207f204e0b8SIan Munsie 	if (ctx) {
208bc78b05bSIan Munsie 		ret = cxl_irq(irq, ctx, &irq_info);
209f204e0b8SIan Munsie 		rcu_read_unlock();
210f204e0b8SIan Munsie 		return ret;
211f204e0b8SIan Munsie 	}
212f204e0b8SIan Munsie 	rcu_read_unlock();
213f204e0b8SIan Munsie 
214de369538SRasmus Villemoes 	WARN(1, "Unable to demultiplex CXL PSL IRQ for PE %i DSISR %016llx DAR"
215de369538SRasmus Villemoes 		" %016llx\n(Possible AFU HW issue - was a term/remove acked"
216bc78b05bSIan Munsie 		" with outstanding transactions?)\n", ph, irq_info.dsisr,
217bc78b05bSIan Munsie 		irq_info.dar);
218bc78b05bSIan Munsie 	return fail_psl_irq(afu, &irq_info);
219f204e0b8SIan Munsie }
220f204e0b8SIan Munsie 
221f204e0b8SIan Munsie static irqreturn_t cxl_irq_afu(int irq, void *data)
222f204e0b8SIan Munsie {
223f204e0b8SIan Munsie 	struct cxl_context *ctx = data;
224f204e0b8SIan Munsie 	irq_hw_number_t hwirq = irqd_to_hwirq(irq_get_irq_data(irq));
225f204e0b8SIan Munsie 	int irq_off, afu_irq = 1;
226f204e0b8SIan Munsie 	__u16 range;
227f204e0b8SIan Munsie 	int r;
228f204e0b8SIan Munsie 
229f204e0b8SIan Munsie 	for (r = 1; r < CXL_IRQ_RANGES; r++) {
230f204e0b8SIan Munsie 		irq_off = hwirq - ctx->irqs.offset[r];
231f204e0b8SIan Munsie 		range = ctx->irqs.range[r];
232f204e0b8SIan Munsie 		if (irq_off >= 0 && irq_off < range) {
233f204e0b8SIan Munsie 			afu_irq += irq_off;
234f204e0b8SIan Munsie 			break;
235f204e0b8SIan Munsie 		}
236f204e0b8SIan Munsie 		afu_irq += range;
237f204e0b8SIan Munsie 	}
238f204e0b8SIan Munsie 	if (unlikely(r >= CXL_IRQ_RANGES)) {
239f204e0b8SIan Munsie 		WARN(1, "Recieved AFU IRQ out of range for pe %i (virq %i hwirq %lx)\n",
240f204e0b8SIan Munsie 		     ctx->pe, irq, hwirq);
241f204e0b8SIan Munsie 		return IRQ_HANDLED;
242f204e0b8SIan Munsie 	}
243f204e0b8SIan Munsie 
2449bcf28cdSIan Munsie 	trace_cxl_afu_irq(ctx, afu_irq, irq, hwirq);
245f204e0b8SIan Munsie 	pr_devel("Received AFU interrupt %i for pe: %i (virq %i hwirq %lx)\n",
246f204e0b8SIan Munsie 	       afu_irq, ctx->pe, irq, hwirq);
247f204e0b8SIan Munsie 
248f204e0b8SIan Munsie 	if (unlikely(!ctx->irq_bitmap)) {
249f204e0b8SIan Munsie 		WARN(1, "Recieved AFU IRQ for context with no IRQ bitmap\n");
250f204e0b8SIan Munsie 		return IRQ_HANDLED;
251f204e0b8SIan Munsie 	}
252f204e0b8SIan Munsie 	spin_lock(&ctx->lock);
253f204e0b8SIan Munsie 	set_bit(afu_irq - 1, ctx->irq_bitmap);
254f204e0b8SIan Munsie 	ctx->pending_irq = true;
255f204e0b8SIan Munsie 	spin_unlock(&ctx->lock);
256f204e0b8SIan Munsie 
257f204e0b8SIan Munsie 	wake_up_all(&ctx->wq);
258f204e0b8SIan Munsie 
259f204e0b8SIan Munsie 	return IRQ_HANDLED;
260f204e0b8SIan Munsie }
261f204e0b8SIan Munsie 
262f204e0b8SIan Munsie unsigned int cxl_map_irq(struct cxl *adapter, irq_hw_number_t hwirq,
26380fa93fcSMichael Neuling 			 irq_handler_t handler, void *cookie, const char *name)
264f204e0b8SIan Munsie {
265f204e0b8SIan Munsie 	unsigned int virq;
266f204e0b8SIan Munsie 	int result;
267f204e0b8SIan Munsie 
268f204e0b8SIan Munsie 	/* IRQ Domain? */
269f204e0b8SIan Munsie 	virq = irq_create_mapping(NULL, hwirq);
270f204e0b8SIan Munsie 	if (!virq) {
271f204e0b8SIan Munsie 		dev_warn(&adapter->dev, "cxl_map_irq: irq_create_mapping failed\n");
272f204e0b8SIan Munsie 		return 0;
273f204e0b8SIan Munsie 	}
274f204e0b8SIan Munsie 
275f204e0b8SIan Munsie 	cxl_setup_irq(adapter, hwirq, virq);
276f204e0b8SIan Munsie 
277f204e0b8SIan Munsie 	pr_devel("hwirq %#lx mapped to virq %u\n", hwirq, virq);
278f204e0b8SIan Munsie 
27980fa93fcSMichael Neuling 	result = request_irq(virq, handler, 0, name, cookie);
280f204e0b8SIan Munsie 	if (result) {
281f204e0b8SIan Munsie 		dev_warn(&adapter->dev, "cxl_map_irq: request_irq failed: %i\n", result);
282f204e0b8SIan Munsie 		return 0;
283f204e0b8SIan Munsie 	}
284f204e0b8SIan Munsie 
285f204e0b8SIan Munsie 	return virq;
286f204e0b8SIan Munsie }
287f204e0b8SIan Munsie 
288f204e0b8SIan Munsie void cxl_unmap_irq(unsigned int virq, void *cookie)
289f204e0b8SIan Munsie {
290f204e0b8SIan Munsie 	free_irq(virq, cookie);
291f204e0b8SIan Munsie 	irq_dispose_mapping(virq);
292f204e0b8SIan Munsie }
293f204e0b8SIan Munsie 
294f204e0b8SIan Munsie static int cxl_register_one_irq(struct cxl *adapter,
295f204e0b8SIan Munsie 				irq_handler_t handler,
296f204e0b8SIan Munsie 				void *cookie,
297f204e0b8SIan Munsie 				irq_hw_number_t *dest_hwirq,
29880fa93fcSMichael Neuling 				unsigned int *dest_virq,
29980fa93fcSMichael Neuling 				const char *name)
300f204e0b8SIan Munsie {
301f204e0b8SIan Munsie 	int hwirq, virq;
302f204e0b8SIan Munsie 
303f204e0b8SIan Munsie 	if ((hwirq = cxl_alloc_one_irq(adapter)) < 0)
304f204e0b8SIan Munsie 		return hwirq;
305f204e0b8SIan Munsie 
30680fa93fcSMichael Neuling 	if (!(virq = cxl_map_irq(adapter, hwirq, handler, cookie, name)))
307f204e0b8SIan Munsie 		goto err;
308f204e0b8SIan Munsie 
309f204e0b8SIan Munsie 	*dest_hwirq = hwirq;
310f204e0b8SIan Munsie 	*dest_virq = virq;
311f204e0b8SIan Munsie 
312f204e0b8SIan Munsie 	return 0;
313f204e0b8SIan Munsie 
314f204e0b8SIan Munsie err:
315f204e0b8SIan Munsie 	cxl_release_one_irq(adapter, hwirq);
316f204e0b8SIan Munsie 	return -ENOMEM;
317f204e0b8SIan Munsie }
318f204e0b8SIan Munsie 
319f204e0b8SIan Munsie int cxl_register_psl_err_irq(struct cxl *adapter)
320f204e0b8SIan Munsie {
321f204e0b8SIan Munsie 	int rc;
322f204e0b8SIan Munsie 
32380fa93fcSMichael Neuling 	adapter->irq_name = kasprintf(GFP_KERNEL, "cxl-%s-err",
32480fa93fcSMichael Neuling 				      dev_name(&adapter->dev));
32580fa93fcSMichael Neuling 	if (!adapter->irq_name)
32680fa93fcSMichael Neuling 		return -ENOMEM;
32780fa93fcSMichael Neuling 
328f204e0b8SIan Munsie 	if ((rc = cxl_register_one_irq(adapter, cxl_irq_err, adapter,
329f204e0b8SIan Munsie 				       &adapter->err_hwirq,
33080fa93fcSMichael Neuling 				       &adapter->err_virq,
33180fa93fcSMichael Neuling 				       adapter->irq_name))) {
33280fa93fcSMichael Neuling 		kfree(adapter->irq_name);
33380fa93fcSMichael Neuling 		adapter->irq_name = NULL;
334f204e0b8SIan Munsie 		return rc;
33580fa93fcSMichael Neuling 	}
336f204e0b8SIan Munsie 
337f204e0b8SIan Munsie 	cxl_p1_write(adapter, CXL_PSL_ErrIVTE, adapter->err_hwirq & 0xffff);
338f204e0b8SIan Munsie 
339f204e0b8SIan Munsie 	return 0;
340f204e0b8SIan Munsie }
341f204e0b8SIan Munsie 
342f204e0b8SIan Munsie void cxl_release_psl_err_irq(struct cxl *adapter)
343f204e0b8SIan Munsie {
344e640d2fcSDaniel Axtens 	if (adapter->err_virq != irq_find_mapping(NULL, adapter->err_hwirq))
345e640d2fcSDaniel Axtens 		return;
346e640d2fcSDaniel Axtens 
347f204e0b8SIan Munsie 	cxl_p1_write(adapter, CXL_PSL_ErrIVTE, 0x0000000000000000);
348f204e0b8SIan Munsie 	cxl_unmap_irq(adapter->err_virq, adapter);
349f204e0b8SIan Munsie 	cxl_release_one_irq(adapter, adapter->err_hwirq);
35080fa93fcSMichael Neuling 	kfree(adapter->irq_name);
351f204e0b8SIan Munsie }
352f204e0b8SIan Munsie 
353f204e0b8SIan Munsie int cxl_register_serr_irq(struct cxl_afu *afu)
354f204e0b8SIan Munsie {
355f204e0b8SIan Munsie 	u64 serr;
356f204e0b8SIan Munsie 	int rc;
357f204e0b8SIan Munsie 
35880fa93fcSMichael Neuling 	afu->err_irq_name = kasprintf(GFP_KERNEL, "cxl-%s-err",
35980fa93fcSMichael Neuling 				      dev_name(&afu->dev));
36080fa93fcSMichael Neuling 	if (!afu->err_irq_name)
36180fa93fcSMichael Neuling 		return -ENOMEM;
36280fa93fcSMichael Neuling 
363f204e0b8SIan Munsie 	if ((rc = cxl_register_one_irq(afu->adapter, cxl_slice_irq_err, afu,
364f204e0b8SIan Munsie 				       &afu->serr_hwirq,
36580fa93fcSMichael Neuling 				       &afu->serr_virq, afu->err_irq_name))) {
36680fa93fcSMichael Neuling 		kfree(afu->err_irq_name);
36780fa93fcSMichael Neuling 		afu->err_irq_name = NULL;
368f204e0b8SIan Munsie 		return rc;
36980fa93fcSMichael Neuling 	}
370f204e0b8SIan Munsie 
371f204e0b8SIan Munsie 	serr = cxl_p1n_read(afu, CXL_PSL_SERR_An);
372f204e0b8SIan Munsie 	serr = (serr & 0x00ffffffffff0000ULL) | (afu->serr_hwirq & 0xffff);
373f204e0b8SIan Munsie 	cxl_p1n_write(afu, CXL_PSL_SERR_An, serr);
374f204e0b8SIan Munsie 
375f204e0b8SIan Munsie 	return 0;
376f204e0b8SIan Munsie }
377f204e0b8SIan Munsie 
378f204e0b8SIan Munsie void cxl_release_serr_irq(struct cxl_afu *afu)
379f204e0b8SIan Munsie {
380e640d2fcSDaniel Axtens 	if (afu->serr_virq != irq_find_mapping(NULL, afu->serr_hwirq))
381e640d2fcSDaniel Axtens 		return;
382e640d2fcSDaniel Axtens 
383f204e0b8SIan Munsie 	cxl_p1n_write(afu, CXL_PSL_SERR_An, 0x0000000000000000);
384f204e0b8SIan Munsie 	cxl_unmap_irq(afu->serr_virq, afu);
385f204e0b8SIan Munsie 	cxl_release_one_irq(afu->adapter, afu->serr_hwirq);
38680fa93fcSMichael Neuling 	kfree(afu->err_irq_name);
387f204e0b8SIan Munsie }
388f204e0b8SIan Munsie 
389f204e0b8SIan Munsie int cxl_register_psl_irq(struct cxl_afu *afu)
390f204e0b8SIan Munsie {
39180fa93fcSMichael Neuling 	int rc;
39280fa93fcSMichael Neuling 
39380fa93fcSMichael Neuling 	afu->psl_irq_name = kasprintf(GFP_KERNEL, "cxl-%s",
39480fa93fcSMichael Neuling 				      dev_name(&afu->dev));
39580fa93fcSMichael Neuling 	if (!afu->psl_irq_name)
39680fa93fcSMichael Neuling 		return -ENOMEM;
39780fa93fcSMichael Neuling 
39880fa93fcSMichael Neuling 	if ((rc = cxl_register_one_irq(afu->adapter, cxl_irq_multiplexed, afu,
39980fa93fcSMichael Neuling 				    &afu->psl_hwirq, &afu->psl_virq,
40080fa93fcSMichael Neuling 				    afu->psl_irq_name))) {
40180fa93fcSMichael Neuling 		kfree(afu->psl_irq_name);
40280fa93fcSMichael Neuling 		afu->psl_irq_name = NULL;
40380fa93fcSMichael Neuling 	}
40480fa93fcSMichael Neuling 	return rc;
405f204e0b8SIan Munsie }
406f204e0b8SIan Munsie 
407f204e0b8SIan Munsie void cxl_release_psl_irq(struct cxl_afu *afu)
408f204e0b8SIan Munsie {
409e640d2fcSDaniel Axtens 	if (afu->psl_virq != irq_find_mapping(NULL, afu->psl_hwirq))
410e640d2fcSDaniel Axtens 		return;
411e640d2fcSDaniel Axtens 
412f204e0b8SIan Munsie 	cxl_unmap_irq(afu->psl_virq, afu);
413f204e0b8SIan Munsie 	cxl_release_one_irq(afu->adapter, afu->psl_hwirq);
41480fa93fcSMichael Neuling 	kfree(afu->psl_irq_name);
41580fa93fcSMichael Neuling }
41680fa93fcSMichael Neuling 
4173d6b040eSDaniel Axtens static void afu_irq_name_free(struct cxl_context *ctx)
41880fa93fcSMichael Neuling {
41980fa93fcSMichael Neuling 	struct cxl_irq_name *irq_name, *tmp;
42080fa93fcSMichael Neuling 
42180fa93fcSMichael Neuling 	list_for_each_entry_safe(irq_name, tmp, &ctx->irq_names, list) {
42280fa93fcSMichael Neuling 		kfree(irq_name->name);
42380fa93fcSMichael Neuling 		list_del(&irq_name->list);
42480fa93fcSMichael Neuling 		kfree(irq_name);
42580fa93fcSMichael Neuling 	}
426f204e0b8SIan Munsie }
427f204e0b8SIan Munsie 
428c358d84bSMichael Neuling int afu_allocate_irqs(struct cxl_context *ctx, u32 count)
429f204e0b8SIan Munsie {
43080fa93fcSMichael Neuling 	int rc, r, i, j = 1;
43180fa93fcSMichael Neuling 	struct cxl_irq_name *irq_name;
432f204e0b8SIan Munsie 
433*a6897f39SVaibhav Jain 	/* Initialize the list head to hold irq names */
434*a6897f39SVaibhav Jain 	INIT_LIST_HEAD(&ctx->irq_names);
435*a6897f39SVaibhav Jain 
436f204e0b8SIan Munsie 	if ((rc = cxl_alloc_irq_ranges(&ctx->irqs, ctx->afu->adapter, count)))
437f204e0b8SIan Munsie 		return rc;
438f204e0b8SIan Munsie 
439f204e0b8SIan Munsie 	/* Multiplexed PSL Interrupt */
440f204e0b8SIan Munsie 	ctx->irqs.offset[0] = ctx->afu->psl_hwirq;
441f204e0b8SIan Munsie 	ctx->irqs.range[0] = 1;
442f204e0b8SIan Munsie 
443f204e0b8SIan Munsie 	ctx->irq_count = count;
444f204e0b8SIan Munsie 	ctx->irq_bitmap = kcalloc(BITS_TO_LONGS(count),
445f204e0b8SIan Munsie 				  sizeof(*ctx->irq_bitmap), GFP_KERNEL);
446f204e0b8SIan Munsie 	if (!ctx->irq_bitmap)
447*a6897f39SVaibhav Jain 		goto out;
44880fa93fcSMichael Neuling 
44980fa93fcSMichael Neuling 	/*
45080fa93fcSMichael Neuling 	 * Allocate names first.  If any fail, bail out before allocating
45180fa93fcSMichael Neuling 	 * actual hardware IRQs.
45280fa93fcSMichael Neuling 	 */
45380fa93fcSMichael Neuling 	for (r = 1; r < CXL_IRQ_RANGES; r++) {
454d3383aaaSColin Ian King 		for (i = 0; i < ctx->irqs.range[r]; i++) {
45580fa93fcSMichael Neuling 			irq_name = kmalloc(sizeof(struct cxl_irq_name),
45680fa93fcSMichael Neuling 					   GFP_KERNEL);
45780fa93fcSMichael Neuling 			if (!irq_name)
45880fa93fcSMichael Neuling 				goto out;
45980fa93fcSMichael Neuling 			irq_name->name = kasprintf(GFP_KERNEL, "cxl-%s-pe%i-%i",
46080fa93fcSMichael Neuling 						   dev_name(&ctx->afu->dev),
46180fa93fcSMichael Neuling 						   ctx->pe, j);
46280fa93fcSMichael Neuling 			if (!irq_name->name) {
46380fa93fcSMichael Neuling 				kfree(irq_name);
46480fa93fcSMichael Neuling 				goto out;
46580fa93fcSMichael Neuling 			}
46680fa93fcSMichael Neuling 			/* Add to tail so next look get the correct order */
46780fa93fcSMichael Neuling 			list_add_tail(&irq_name->list, &ctx->irq_names);
46880fa93fcSMichael Neuling 			j++;
46980fa93fcSMichael Neuling 		}
47080fa93fcSMichael Neuling 	}
471c358d84bSMichael Neuling 	return 0;
472c358d84bSMichael Neuling 
473c358d84bSMichael Neuling out:
474*a6897f39SVaibhav Jain 	cxl_release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
475c358d84bSMichael Neuling 	afu_irq_name_free(ctx);
476c358d84bSMichael Neuling 	return -ENOMEM;
477c358d84bSMichael Neuling }
478c358d84bSMichael Neuling 
4793d6b040eSDaniel Axtens static void afu_register_hwirqs(struct cxl_context *ctx)
480c358d84bSMichael Neuling {
481c358d84bSMichael Neuling 	irq_hw_number_t hwirq;
482c358d84bSMichael Neuling 	struct cxl_irq_name *irq_name;
483c358d84bSMichael Neuling 	int r,i;
48480fa93fcSMichael Neuling 
48580fa93fcSMichael Neuling 	/* We've allocated all memory now, so let's do the irq allocations */
48680fa93fcSMichael Neuling 	irq_name = list_first_entry(&ctx->irq_names, struct cxl_irq_name, list);
487f204e0b8SIan Munsie 	for (r = 1; r < CXL_IRQ_RANGES; r++) {
488f204e0b8SIan Munsie 		hwirq = ctx->irqs.offset[r];
489f204e0b8SIan Munsie 		for (i = 0; i < ctx->irqs.range[r]; hwirq++, i++) {
490f204e0b8SIan Munsie 			cxl_map_irq(ctx->afu->adapter, hwirq,
49180fa93fcSMichael Neuling 				    cxl_irq_afu, ctx, irq_name->name);
49280fa93fcSMichael Neuling 			irq_name = list_next_entry(irq_name, list);
493f204e0b8SIan Munsie 		}
494f204e0b8SIan Munsie 	}
495c358d84bSMichael Neuling }
496f204e0b8SIan Munsie 
497c358d84bSMichael Neuling int afu_register_irqs(struct cxl_context *ctx, u32 count)
498c358d84bSMichael Neuling {
499c358d84bSMichael Neuling 	int rc;
500c358d84bSMichael Neuling 
501c358d84bSMichael Neuling 	rc = afu_allocate_irqs(ctx, count);
502c358d84bSMichael Neuling 	if (rc)
503c358d84bSMichael Neuling 		return rc;
504c358d84bSMichael Neuling 
505c358d84bSMichael Neuling 	afu_register_hwirqs(ctx);
506f204e0b8SIan Munsie 	return 0;
507f204e0b8SIan Munsie  }
508f204e0b8SIan Munsie 
5096428832aSMichael Neuling void afu_release_irqs(struct cxl_context *ctx, void *cookie)
510f204e0b8SIan Munsie {
511f204e0b8SIan Munsie 	irq_hw_number_t hwirq;
512f204e0b8SIan Munsie 	unsigned int virq;
513f204e0b8SIan Munsie 	int r, i;
514f204e0b8SIan Munsie 
515f204e0b8SIan Munsie 	for (r = 1; r < CXL_IRQ_RANGES; r++) {
516f204e0b8SIan Munsie 		hwirq = ctx->irqs.offset[r];
517f204e0b8SIan Munsie 		for (i = 0; i < ctx->irqs.range[r]; hwirq++, i++) {
518f204e0b8SIan Munsie 			virq = irq_find_mapping(NULL, hwirq);
519f204e0b8SIan Munsie 			if (virq)
5206428832aSMichael Neuling 				cxl_unmap_irq(virq, cookie);
521f204e0b8SIan Munsie 		}
522f204e0b8SIan Munsie 	}
523f204e0b8SIan Munsie 
52480fa93fcSMichael Neuling 	afu_irq_name_free(ctx);
525f204e0b8SIan Munsie 	cxl_release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
5268c7dd08aSVaibhav Jain 
5278c7dd08aSVaibhav Jain 	kfree(ctx->irq_bitmap);
5288c7dd08aSVaibhav Jain 	ctx->irq_bitmap = NULL;
5298c7dd08aSVaibhav Jain 	ctx->irq_count = 0;
530f204e0b8SIan Munsie }
531