xref: /openbmc/linux/drivers/misc/cxl/irq.c (revision 9bcf28cdb28e6a793c4e59f0a42c66fe241993a8)
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>
17f204e0b8SIan Munsie #include <misc/cxl.h>
18f204e0b8SIan Munsie 
19f204e0b8SIan Munsie #include "cxl.h"
20*9bcf28cdSIan 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 
33f204e0b8SIan Munsie 	dev_crit(&ctx->afu->dev, "PSL ERROR STATUS: 0x%.16llx\n", errstat);
34f204e0b8SIan Munsie 	dev_crit(&ctx->afu->dev, "PSL_FIR1: 0x%.16llx\n", fir1);
35f204e0b8SIan Munsie 	dev_crit(&ctx->afu->dev, "PSL_FIR2: 0x%.16llx\n", fir2);
36f204e0b8SIan Munsie 	dev_crit(&ctx->afu->dev, "PSL_SERR_An: 0x%.16llx\n", serr);
37f204e0b8SIan Munsie 	dev_crit(&ctx->afu->dev, "PSL_FIR_SLICE_An: 0x%.16llx\n", fir_slice);
38f204e0b8SIan Munsie 	dev_crit(&ctx->afu->dev, "CXL_PSL_AFU_DEBUG_An: 0x%.16llx\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);
57f204e0b8SIan Munsie 	dev_crit(&afu->dev, "PSL_SERR_An: 0x%.16llx\n", serr);
58f204e0b8SIan Munsie 	dev_crit(&afu->dev, "PSL_FIR_SLICE_An: 0x%.16llx\n", fir_slice);
59f204e0b8SIan Munsie 	dev_crit(&afu->dev, "CXL_PSL_ErrStat_An: 0x%.16llx\n", errstat);
60f204e0b8SIan Munsie 	dev_crit(&afu->dev, "CXL_PSL_AFU_DEBUG_An: 0x%.16llx\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);
75f204e0b8SIan Munsie 	dev_crit(&adapter->dev, "PSL_ErrIVTE: 0x%.16llx\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 
83f204e0b8SIan Munsie 	dev_crit(&adapter->dev, "PSL_FIR1: 0x%.16llx\nPSL_FIR2: 0x%.16llx\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 
104*9bcf28cdSIan Munsie 	trace_cxl_psl_irq(ctx, irq, dsisr, dar);
105*9bcf28cdSIan 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) {
150bc78b05bSIan Munsie 		pr_devel("CXL interrupt: AFU Error %.llx\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 "
161f204e0b8SIan Munsie 					    "undelivered to pe %i: %.llx\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);
173f204e0b8SIan Munsie 	}
174f204e0b8SIan Munsie 	if (dsisr & CXL_PSL_DSISR_An_OC)
175f204e0b8SIan Munsie 		pr_devel("CXL interrupt: OS Context Warning\n");
176f204e0b8SIan Munsie 
177f204e0b8SIan Munsie 	WARN(1, "Unhandled CXL PSL IRQ\n");
178f204e0b8SIan Munsie 	return IRQ_HANDLED;
179f204e0b8SIan Munsie }
180f204e0b8SIan Munsie 
181bc78b05bSIan Munsie static irqreturn_t fail_psl_irq(struct cxl_afu *afu, struct cxl_irq_info *irq_info)
182bc78b05bSIan Munsie {
183bc78b05bSIan Munsie 	if (irq_info->dsisr & CXL_PSL_DSISR_TRANS)
184bc78b05bSIan Munsie 		cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_AE);
185bc78b05bSIan Munsie 	else
186bc78b05bSIan Munsie 		cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_A);
187bc78b05bSIan Munsie 
188bc78b05bSIan Munsie 	return IRQ_HANDLED;
189bc78b05bSIan Munsie }
190bc78b05bSIan Munsie 
191f204e0b8SIan Munsie static irqreturn_t cxl_irq_multiplexed(int irq, void *data)
192f204e0b8SIan Munsie {
193f204e0b8SIan Munsie 	struct cxl_afu *afu = data;
194f204e0b8SIan Munsie 	struct cxl_context *ctx;
195bc78b05bSIan Munsie 	struct cxl_irq_info irq_info;
196f204e0b8SIan Munsie 	int ph = cxl_p2n_read(afu, CXL_PSL_PEHandle_An) & 0xffff;
197f204e0b8SIan Munsie 	int ret;
198f204e0b8SIan Munsie 
199bc78b05bSIan Munsie 	if ((ret = cxl_get_irq(afu, &irq_info))) {
200bc78b05bSIan Munsie 		WARN(1, "Unable to get CXL IRQ Info: %i\n", ret);
201bc78b05bSIan Munsie 		return fail_psl_irq(afu, &irq_info);
202bc78b05bSIan Munsie 	}
203bc78b05bSIan Munsie 
204f204e0b8SIan Munsie 	rcu_read_lock();
205f204e0b8SIan Munsie 	ctx = idr_find(&afu->contexts_idr, ph);
206f204e0b8SIan Munsie 	if (ctx) {
207bc78b05bSIan Munsie 		ret = cxl_irq(irq, ctx, &irq_info);
208f204e0b8SIan Munsie 		rcu_read_unlock();
209f204e0b8SIan Munsie 		return ret;
210f204e0b8SIan Munsie 	}
211f204e0b8SIan Munsie 	rcu_read_unlock();
212f204e0b8SIan Munsie 
213bc78b05bSIan Munsie 	WARN(1, "Unable to demultiplex CXL PSL IRQ for PE %i DSISR %.16llx DAR"
214bc78b05bSIan Munsie 		" %.16llx\n(Possible AFU HW issue - was a term/remove acked"
215bc78b05bSIan Munsie 		" with outstanding transactions?)\n", ph, irq_info.dsisr,
216bc78b05bSIan Munsie 		irq_info.dar);
217bc78b05bSIan Munsie 	return fail_psl_irq(afu, &irq_info);
218f204e0b8SIan Munsie }
219f204e0b8SIan Munsie 
220f204e0b8SIan Munsie static irqreturn_t cxl_irq_afu(int irq, void *data)
221f204e0b8SIan Munsie {
222f204e0b8SIan Munsie 	struct cxl_context *ctx = data;
223f204e0b8SIan Munsie 	irq_hw_number_t hwirq = irqd_to_hwirq(irq_get_irq_data(irq));
224f204e0b8SIan Munsie 	int irq_off, afu_irq = 1;
225f204e0b8SIan Munsie 	__u16 range;
226f204e0b8SIan Munsie 	int r;
227f204e0b8SIan Munsie 
228f204e0b8SIan Munsie 	for (r = 1; r < CXL_IRQ_RANGES; r++) {
229f204e0b8SIan Munsie 		irq_off = hwirq - ctx->irqs.offset[r];
230f204e0b8SIan Munsie 		range = ctx->irqs.range[r];
231f204e0b8SIan Munsie 		if (irq_off >= 0 && irq_off < range) {
232f204e0b8SIan Munsie 			afu_irq += irq_off;
233f204e0b8SIan Munsie 			break;
234f204e0b8SIan Munsie 		}
235f204e0b8SIan Munsie 		afu_irq += range;
236f204e0b8SIan Munsie 	}
237f204e0b8SIan Munsie 	if (unlikely(r >= CXL_IRQ_RANGES)) {
238f204e0b8SIan Munsie 		WARN(1, "Recieved AFU IRQ out of range for pe %i (virq %i hwirq %lx)\n",
239f204e0b8SIan Munsie 		     ctx->pe, irq, hwirq);
240f204e0b8SIan Munsie 		return IRQ_HANDLED;
241f204e0b8SIan Munsie 	}
242f204e0b8SIan Munsie 
243*9bcf28cdSIan Munsie 	trace_cxl_afu_irq(ctx, afu_irq, irq, hwirq);
244f204e0b8SIan Munsie 	pr_devel("Received AFU interrupt %i for pe: %i (virq %i hwirq %lx)\n",
245f204e0b8SIan Munsie 	       afu_irq, ctx->pe, irq, hwirq);
246f204e0b8SIan Munsie 
247f204e0b8SIan Munsie 	if (unlikely(!ctx->irq_bitmap)) {
248f204e0b8SIan Munsie 		WARN(1, "Recieved AFU IRQ for context with no IRQ bitmap\n");
249f204e0b8SIan Munsie 		return IRQ_HANDLED;
250f204e0b8SIan Munsie 	}
251f204e0b8SIan Munsie 	spin_lock(&ctx->lock);
252f204e0b8SIan Munsie 	set_bit(afu_irq - 1, ctx->irq_bitmap);
253f204e0b8SIan Munsie 	ctx->pending_irq = true;
254f204e0b8SIan Munsie 	spin_unlock(&ctx->lock);
255f204e0b8SIan Munsie 
256f204e0b8SIan Munsie 	wake_up_all(&ctx->wq);
257f204e0b8SIan Munsie 
258f204e0b8SIan Munsie 	return IRQ_HANDLED;
259f204e0b8SIan Munsie }
260f204e0b8SIan Munsie 
261f204e0b8SIan Munsie unsigned int cxl_map_irq(struct cxl *adapter, irq_hw_number_t hwirq,
26280fa93fcSMichael Neuling 			 irq_handler_t handler, void *cookie, const char *name)
263f204e0b8SIan Munsie {
264f204e0b8SIan Munsie 	unsigned int virq;
265f204e0b8SIan Munsie 	int result;
266f204e0b8SIan Munsie 
267f204e0b8SIan Munsie 	/* IRQ Domain? */
268f204e0b8SIan Munsie 	virq = irq_create_mapping(NULL, hwirq);
269f204e0b8SIan Munsie 	if (!virq) {
270f204e0b8SIan Munsie 		dev_warn(&adapter->dev, "cxl_map_irq: irq_create_mapping failed\n");
271f204e0b8SIan Munsie 		return 0;
272f204e0b8SIan Munsie 	}
273f204e0b8SIan Munsie 
274f204e0b8SIan Munsie 	cxl_setup_irq(adapter, hwirq, virq);
275f204e0b8SIan Munsie 
276f204e0b8SIan Munsie 	pr_devel("hwirq %#lx mapped to virq %u\n", hwirq, virq);
277f204e0b8SIan Munsie 
27880fa93fcSMichael Neuling 	result = request_irq(virq, handler, 0, name, cookie);
279f204e0b8SIan Munsie 	if (result) {
280f204e0b8SIan Munsie 		dev_warn(&adapter->dev, "cxl_map_irq: request_irq failed: %i\n", result);
281f204e0b8SIan Munsie 		return 0;
282f204e0b8SIan Munsie 	}
283f204e0b8SIan Munsie 
284f204e0b8SIan Munsie 	return virq;
285f204e0b8SIan Munsie }
286f204e0b8SIan Munsie 
287f204e0b8SIan Munsie void cxl_unmap_irq(unsigned int virq, void *cookie)
288f204e0b8SIan Munsie {
289f204e0b8SIan Munsie 	free_irq(virq, cookie);
290f204e0b8SIan Munsie 	irq_dispose_mapping(virq);
291f204e0b8SIan Munsie }
292f204e0b8SIan Munsie 
293f204e0b8SIan Munsie static int cxl_register_one_irq(struct cxl *adapter,
294f204e0b8SIan Munsie 				irq_handler_t handler,
295f204e0b8SIan Munsie 				void *cookie,
296f204e0b8SIan Munsie 				irq_hw_number_t *dest_hwirq,
29780fa93fcSMichael Neuling 				unsigned int *dest_virq,
29880fa93fcSMichael Neuling 				const char *name)
299f204e0b8SIan Munsie {
300f204e0b8SIan Munsie 	int hwirq, virq;
301f204e0b8SIan Munsie 
302f204e0b8SIan Munsie 	if ((hwirq = cxl_alloc_one_irq(adapter)) < 0)
303f204e0b8SIan Munsie 		return hwirq;
304f204e0b8SIan Munsie 
30580fa93fcSMichael Neuling 	if (!(virq = cxl_map_irq(adapter, hwirq, handler, cookie, name)))
306f204e0b8SIan Munsie 		goto err;
307f204e0b8SIan Munsie 
308f204e0b8SIan Munsie 	*dest_hwirq = hwirq;
309f204e0b8SIan Munsie 	*dest_virq = virq;
310f204e0b8SIan Munsie 
311f204e0b8SIan Munsie 	return 0;
312f204e0b8SIan Munsie 
313f204e0b8SIan Munsie err:
314f204e0b8SIan Munsie 	cxl_release_one_irq(adapter, hwirq);
315f204e0b8SIan Munsie 	return -ENOMEM;
316f204e0b8SIan Munsie }
317f204e0b8SIan Munsie 
318f204e0b8SIan Munsie int cxl_register_psl_err_irq(struct cxl *adapter)
319f204e0b8SIan Munsie {
320f204e0b8SIan Munsie 	int rc;
321f204e0b8SIan Munsie 
32280fa93fcSMichael Neuling 	adapter->irq_name = kasprintf(GFP_KERNEL, "cxl-%s-err",
32380fa93fcSMichael Neuling 				      dev_name(&adapter->dev));
32480fa93fcSMichael Neuling 	if (!adapter->irq_name)
32580fa93fcSMichael Neuling 		return -ENOMEM;
32680fa93fcSMichael Neuling 
327f204e0b8SIan Munsie 	if ((rc = cxl_register_one_irq(adapter, cxl_irq_err, adapter,
328f204e0b8SIan Munsie 				       &adapter->err_hwirq,
32980fa93fcSMichael Neuling 				       &adapter->err_virq,
33080fa93fcSMichael Neuling 				       adapter->irq_name))) {
33180fa93fcSMichael Neuling 		kfree(adapter->irq_name);
33280fa93fcSMichael Neuling 		adapter->irq_name = NULL;
333f204e0b8SIan Munsie 		return rc;
33480fa93fcSMichael Neuling 	}
335f204e0b8SIan Munsie 
336f204e0b8SIan Munsie 	cxl_p1_write(adapter, CXL_PSL_ErrIVTE, adapter->err_hwirq & 0xffff);
337f204e0b8SIan Munsie 
338f204e0b8SIan Munsie 	return 0;
339f204e0b8SIan Munsie }
340f204e0b8SIan Munsie 
341f204e0b8SIan Munsie void cxl_release_psl_err_irq(struct cxl *adapter)
342f204e0b8SIan Munsie {
343f204e0b8SIan Munsie 	cxl_p1_write(adapter, CXL_PSL_ErrIVTE, 0x0000000000000000);
344f204e0b8SIan Munsie 	cxl_unmap_irq(adapter->err_virq, adapter);
345f204e0b8SIan Munsie 	cxl_release_one_irq(adapter, adapter->err_hwirq);
34680fa93fcSMichael Neuling 	kfree(adapter->irq_name);
347f204e0b8SIan Munsie }
348f204e0b8SIan Munsie 
349f204e0b8SIan Munsie int cxl_register_serr_irq(struct cxl_afu *afu)
350f204e0b8SIan Munsie {
351f204e0b8SIan Munsie 	u64 serr;
352f204e0b8SIan Munsie 	int rc;
353f204e0b8SIan Munsie 
35480fa93fcSMichael Neuling 	afu->err_irq_name = kasprintf(GFP_KERNEL, "cxl-%s-err",
35580fa93fcSMichael Neuling 				      dev_name(&afu->dev));
35680fa93fcSMichael Neuling 	if (!afu->err_irq_name)
35780fa93fcSMichael Neuling 		return -ENOMEM;
35880fa93fcSMichael Neuling 
359f204e0b8SIan Munsie 	if ((rc = cxl_register_one_irq(afu->adapter, cxl_slice_irq_err, afu,
360f204e0b8SIan Munsie 				       &afu->serr_hwirq,
36180fa93fcSMichael Neuling 				       &afu->serr_virq, afu->err_irq_name))) {
36280fa93fcSMichael Neuling 		kfree(afu->err_irq_name);
36380fa93fcSMichael Neuling 		afu->err_irq_name = NULL;
364f204e0b8SIan Munsie 		return rc;
36580fa93fcSMichael Neuling 	}
366f204e0b8SIan Munsie 
367f204e0b8SIan Munsie 	serr = cxl_p1n_read(afu, CXL_PSL_SERR_An);
368f204e0b8SIan Munsie 	serr = (serr & 0x00ffffffffff0000ULL) | (afu->serr_hwirq & 0xffff);
369f204e0b8SIan Munsie 	cxl_p1n_write(afu, CXL_PSL_SERR_An, serr);
370f204e0b8SIan Munsie 
371f204e0b8SIan Munsie 	return 0;
372f204e0b8SIan Munsie }
373f204e0b8SIan Munsie 
374f204e0b8SIan Munsie void cxl_release_serr_irq(struct cxl_afu *afu)
375f204e0b8SIan Munsie {
376f204e0b8SIan Munsie 	cxl_p1n_write(afu, CXL_PSL_SERR_An, 0x0000000000000000);
377f204e0b8SIan Munsie 	cxl_unmap_irq(afu->serr_virq, afu);
378f204e0b8SIan Munsie 	cxl_release_one_irq(afu->adapter, afu->serr_hwirq);
37980fa93fcSMichael Neuling 	kfree(afu->err_irq_name);
380f204e0b8SIan Munsie }
381f204e0b8SIan Munsie 
382f204e0b8SIan Munsie int cxl_register_psl_irq(struct cxl_afu *afu)
383f204e0b8SIan Munsie {
38480fa93fcSMichael Neuling 	int rc;
38580fa93fcSMichael Neuling 
38680fa93fcSMichael Neuling 	afu->psl_irq_name = kasprintf(GFP_KERNEL, "cxl-%s",
38780fa93fcSMichael Neuling 				      dev_name(&afu->dev));
38880fa93fcSMichael Neuling 	if (!afu->psl_irq_name)
38980fa93fcSMichael Neuling 		return -ENOMEM;
39080fa93fcSMichael Neuling 
39180fa93fcSMichael Neuling 	if ((rc = cxl_register_one_irq(afu->adapter, cxl_irq_multiplexed, afu,
39280fa93fcSMichael Neuling 				    &afu->psl_hwirq, &afu->psl_virq,
39380fa93fcSMichael Neuling 				    afu->psl_irq_name))) {
39480fa93fcSMichael Neuling 		kfree(afu->psl_irq_name);
39580fa93fcSMichael Neuling 		afu->psl_irq_name = NULL;
39680fa93fcSMichael Neuling 	}
39780fa93fcSMichael Neuling 	return rc;
398f204e0b8SIan Munsie }
399f204e0b8SIan Munsie 
400f204e0b8SIan Munsie void cxl_release_psl_irq(struct cxl_afu *afu)
401f204e0b8SIan Munsie {
402f204e0b8SIan Munsie 	cxl_unmap_irq(afu->psl_virq, afu);
403f204e0b8SIan Munsie 	cxl_release_one_irq(afu->adapter, afu->psl_hwirq);
40480fa93fcSMichael Neuling 	kfree(afu->psl_irq_name);
40580fa93fcSMichael Neuling }
40680fa93fcSMichael Neuling 
40780fa93fcSMichael Neuling void afu_irq_name_free(struct cxl_context *ctx)
40880fa93fcSMichael Neuling {
40980fa93fcSMichael Neuling 	struct cxl_irq_name *irq_name, *tmp;
41080fa93fcSMichael Neuling 
41180fa93fcSMichael Neuling 	list_for_each_entry_safe(irq_name, tmp, &ctx->irq_names, list) {
41280fa93fcSMichael Neuling 		kfree(irq_name->name);
41380fa93fcSMichael Neuling 		list_del(&irq_name->list);
41480fa93fcSMichael Neuling 		kfree(irq_name);
41580fa93fcSMichael Neuling 	}
416f204e0b8SIan Munsie }
417f204e0b8SIan Munsie 
418f204e0b8SIan Munsie int afu_register_irqs(struct cxl_context *ctx, u32 count)
419f204e0b8SIan Munsie {
420f204e0b8SIan Munsie 	irq_hw_number_t hwirq;
42180fa93fcSMichael Neuling 	int rc, r, i, j = 1;
42280fa93fcSMichael Neuling 	struct cxl_irq_name *irq_name;
423f204e0b8SIan Munsie 
424f204e0b8SIan Munsie 	if ((rc = cxl_alloc_irq_ranges(&ctx->irqs, ctx->afu->adapter, count)))
425f204e0b8SIan Munsie 		return rc;
426f204e0b8SIan Munsie 
427f204e0b8SIan Munsie 	/* Multiplexed PSL Interrupt */
428f204e0b8SIan Munsie 	ctx->irqs.offset[0] = ctx->afu->psl_hwirq;
429f204e0b8SIan Munsie 	ctx->irqs.range[0] = 1;
430f204e0b8SIan Munsie 
431f204e0b8SIan Munsie 	ctx->irq_count = count;
432f204e0b8SIan Munsie 	ctx->irq_bitmap = kcalloc(BITS_TO_LONGS(count),
433f204e0b8SIan Munsie 				  sizeof(*ctx->irq_bitmap), GFP_KERNEL);
434f204e0b8SIan Munsie 	if (!ctx->irq_bitmap)
435f204e0b8SIan Munsie 		return -ENOMEM;
43680fa93fcSMichael Neuling 
43780fa93fcSMichael Neuling 	/*
43880fa93fcSMichael Neuling 	 * Allocate names first.  If any fail, bail out before allocating
43980fa93fcSMichael Neuling 	 * actual hardware IRQs.
44080fa93fcSMichael Neuling 	 */
44180fa93fcSMichael Neuling 	INIT_LIST_HEAD(&ctx->irq_names);
44280fa93fcSMichael Neuling 	for (r = 1; r < CXL_IRQ_RANGES; r++) {
443d3383aaaSColin Ian King 		for (i = 0; i < ctx->irqs.range[r]; i++) {
44480fa93fcSMichael Neuling 			irq_name = kmalloc(sizeof(struct cxl_irq_name),
44580fa93fcSMichael Neuling 					   GFP_KERNEL);
44680fa93fcSMichael Neuling 			if (!irq_name)
44780fa93fcSMichael Neuling 				goto out;
44880fa93fcSMichael Neuling 			irq_name->name = kasprintf(GFP_KERNEL, "cxl-%s-pe%i-%i",
44980fa93fcSMichael Neuling 						   dev_name(&ctx->afu->dev),
45080fa93fcSMichael Neuling 						   ctx->pe, j);
45180fa93fcSMichael Neuling 			if (!irq_name->name) {
45280fa93fcSMichael Neuling 				kfree(irq_name);
45380fa93fcSMichael Neuling 				goto out;
45480fa93fcSMichael Neuling 			}
45580fa93fcSMichael Neuling 			/* Add to tail so next look get the correct order */
45680fa93fcSMichael Neuling 			list_add_tail(&irq_name->list, &ctx->irq_names);
45780fa93fcSMichael Neuling 			j++;
45880fa93fcSMichael Neuling 		}
45980fa93fcSMichael Neuling 	}
46080fa93fcSMichael Neuling 
46180fa93fcSMichael Neuling 	/* We've allocated all memory now, so let's do the irq allocations */
46280fa93fcSMichael Neuling 	irq_name = list_first_entry(&ctx->irq_names, struct cxl_irq_name, list);
463f204e0b8SIan Munsie 	for (r = 1; r < CXL_IRQ_RANGES; r++) {
464f204e0b8SIan Munsie 		hwirq = ctx->irqs.offset[r];
465f204e0b8SIan Munsie 		for (i = 0; i < ctx->irqs.range[r]; hwirq++, i++) {
466f204e0b8SIan Munsie 			cxl_map_irq(ctx->afu->adapter, hwirq,
46780fa93fcSMichael Neuling 				    cxl_irq_afu, ctx, irq_name->name);
46880fa93fcSMichael Neuling 			irq_name = list_next_entry(irq_name, list);
469f204e0b8SIan Munsie 		}
470f204e0b8SIan Munsie 	}
471f204e0b8SIan Munsie 
472f204e0b8SIan Munsie 	return 0;
47380fa93fcSMichael Neuling 
47480fa93fcSMichael Neuling out:
47580fa93fcSMichael Neuling 	afu_irq_name_free(ctx);
47680fa93fcSMichael Neuling 	return -ENOMEM;
477f204e0b8SIan Munsie }
478f204e0b8SIan Munsie 
479f204e0b8SIan Munsie void afu_release_irqs(struct cxl_context *ctx)
480f204e0b8SIan Munsie {
481f204e0b8SIan Munsie 	irq_hw_number_t hwirq;
482f204e0b8SIan Munsie 	unsigned int virq;
483f204e0b8SIan Munsie 	int r, i;
484f204e0b8SIan Munsie 
485f204e0b8SIan Munsie 	for (r = 1; r < CXL_IRQ_RANGES; r++) {
486f204e0b8SIan Munsie 		hwirq = ctx->irqs.offset[r];
487f204e0b8SIan Munsie 		for (i = 0; i < ctx->irqs.range[r]; hwirq++, i++) {
488f204e0b8SIan Munsie 			virq = irq_find_mapping(NULL, hwirq);
489f204e0b8SIan Munsie 			if (virq)
490f204e0b8SIan Munsie 				cxl_unmap_irq(virq, ctx);
491f204e0b8SIan Munsie 		}
492f204e0b8SIan Munsie 	}
493f204e0b8SIan Munsie 
49480fa93fcSMichael Neuling 	afu_irq_name_free(ctx);
495f204e0b8SIan Munsie 	cxl_release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
496f204e0b8SIan Munsie }
497