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