xref: /OK3568_Linux_fs/kernel/arch/powerpc/sysdev/xive/common.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright 2016,2017 IBM Corporation.
4*4882a593Smuzhiyun  */
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun #define pr_fmt(fmt) "xive: " fmt
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <linux/types.h>
9*4882a593Smuzhiyun #include <linux/threads.h>
10*4882a593Smuzhiyun #include <linux/kernel.h>
11*4882a593Smuzhiyun #include <linux/irq.h>
12*4882a593Smuzhiyun #include <linux/debugfs.h>
13*4882a593Smuzhiyun #include <linux/smp.h>
14*4882a593Smuzhiyun #include <linux/interrupt.h>
15*4882a593Smuzhiyun #include <linux/seq_file.h>
16*4882a593Smuzhiyun #include <linux/init.h>
17*4882a593Smuzhiyun #include <linux/cpu.h>
18*4882a593Smuzhiyun #include <linux/of.h>
19*4882a593Smuzhiyun #include <linux/slab.h>
20*4882a593Smuzhiyun #include <linux/spinlock.h>
21*4882a593Smuzhiyun #include <linux/msi.h>
22*4882a593Smuzhiyun #include <linux/vmalloc.h>
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun #include <asm/debugfs.h>
25*4882a593Smuzhiyun #include <asm/prom.h>
26*4882a593Smuzhiyun #include <asm/io.h>
27*4882a593Smuzhiyun #include <asm/smp.h>
28*4882a593Smuzhiyun #include <asm/machdep.h>
29*4882a593Smuzhiyun #include <asm/irq.h>
30*4882a593Smuzhiyun #include <asm/errno.h>
31*4882a593Smuzhiyun #include <asm/xive.h>
32*4882a593Smuzhiyun #include <asm/xive-regs.h>
33*4882a593Smuzhiyun #include <asm/xmon.h>
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun #include "xive-internal.h"
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun #undef DEBUG_FLUSH
38*4882a593Smuzhiyun #undef DEBUG_ALL
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun #ifdef DEBUG_ALL
41*4882a593Smuzhiyun #define DBG_VERBOSE(fmt, ...)	pr_devel("cpu %d - " fmt, \
42*4882a593Smuzhiyun 					 smp_processor_id(), ## __VA_ARGS__)
43*4882a593Smuzhiyun #else
44*4882a593Smuzhiyun #define DBG_VERBOSE(fmt...)	do { } while(0)
45*4882a593Smuzhiyun #endif
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun bool __xive_enabled;
48*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(__xive_enabled);
49*4882a593Smuzhiyun bool xive_cmdline_disabled;
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun /* We use only one priority for now */
52*4882a593Smuzhiyun static u8 xive_irq_priority;
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun /* TIMA exported to KVM */
55*4882a593Smuzhiyun void __iomem *xive_tima;
56*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xive_tima);
57*4882a593Smuzhiyun u32 xive_tima_offset;
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun /* Backend ops */
60*4882a593Smuzhiyun static const struct xive_ops *xive_ops;
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun /* Our global interrupt domain */
63*4882a593Smuzhiyun static struct irq_domain *xive_irq_domain;
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun #ifdef CONFIG_SMP
66*4882a593Smuzhiyun /* The IPIs all use the same logical irq number */
67*4882a593Smuzhiyun static u32 xive_ipi_irq;
68*4882a593Smuzhiyun #endif
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun /* Xive state for each CPU */
71*4882a593Smuzhiyun static DEFINE_PER_CPU(struct xive_cpu *, xive_cpu);
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun /* An invalid CPU target */
74*4882a593Smuzhiyun #define XIVE_INVALID_TARGET	(-1)
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun /*
77*4882a593Smuzhiyun  * Read the next entry in a queue, return its content if it's valid
78*4882a593Smuzhiyun  * or 0 if there is no new entry.
79*4882a593Smuzhiyun  *
80*4882a593Smuzhiyun  * The queue pointer is moved forward unless "just_peek" is set
81*4882a593Smuzhiyun  */
xive_read_eq(struct xive_q * q,bool just_peek)82*4882a593Smuzhiyun static u32 xive_read_eq(struct xive_q *q, bool just_peek)
83*4882a593Smuzhiyun {
84*4882a593Smuzhiyun 	u32 cur;
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 	if (!q->qpage)
87*4882a593Smuzhiyun 		return 0;
88*4882a593Smuzhiyun 	cur = be32_to_cpup(q->qpage + q->idx);
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	/* Check valid bit (31) vs current toggle polarity */
91*4882a593Smuzhiyun 	if ((cur >> 31) == q->toggle)
92*4882a593Smuzhiyun 		return 0;
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	/* If consuming from the queue ... */
95*4882a593Smuzhiyun 	if (!just_peek) {
96*4882a593Smuzhiyun 		/* Next entry */
97*4882a593Smuzhiyun 		q->idx = (q->idx + 1) & q->msk;
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 		/* Wrap around: flip valid toggle */
100*4882a593Smuzhiyun 		if (q->idx == 0)
101*4882a593Smuzhiyun 			q->toggle ^= 1;
102*4882a593Smuzhiyun 	}
103*4882a593Smuzhiyun 	/* Mask out the valid bit (31) */
104*4882a593Smuzhiyun 	return cur & 0x7fffffff;
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun /*
108*4882a593Smuzhiyun  * Scans all the queue that may have interrupts in them
109*4882a593Smuzhiyun  * (based on "pending_prio") in priority order until an
110*4882a593Smuzhiyun  * interrupt is found or all the queues are empty.
111*4882a593Smuzhiyun  *
112*4882a593Smuzhiyun  * Then updates the CPPR (Current Processor Priority
113*4882a593Smuzhiyun  * Register) based on the most favored interrupt found
114*4882a593Smuzhiyun  * (0xff if none) and return what was found (0 if none).
115*4882a593Smuzhiyun  *
116*4882a593Smuzhiyun  * If just_peek is set, return the most favored pending
117*4882a593Smuzhiyun  * interrupt if any but don't update the queue pointers.
118*4882a593Smuzhiyun  *
119*4882a593Smuzhiyun  * Note: This function can operate generically on any number
120*4882a593Smuzhiyun  * of queues (up to 8). The current implementation of the XIVE
121*4882a593Smuzhiyun  * driver only uses a single queue however.
122*4882a593Smuzhiyun  *
123*4882a593Smuzhiyun  * Note2: This will also "flush" "the pending_count" of a queue
124*4882a593Smuzhiyun  * into the "count" when that queue is observed to be empty.
125*4882a593Smuzhiyun  * This is used to keep track of the amount of interrupts
126*4882a593Smuzhiyun  * targetting a queue. When an interrupt is moved away from
127*4882a593Smuzhiyun  * a queue, we only decrement that queue count once the queue
128*4882a593Smuzhiyun  * has been observed empty to avoid races.
129*4882a593Smuzhiyun  */
xive_scan_interrupts(struct xive_cpu * xc,bool just_peek)130*4882a593Smuzhiyun static u32 xive_scan_interrupts(struct xive_cpu *xc, bool just_peek)
131*4882a593Smuzhiyun {
132*4882a593Smuzhiyun 	u32 irq = 0;
133*4882a593Smuzhiyun 	u8 prio = 0;
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	/* Find highest pending priority */
136*4882a593Smuzhiyun 	while (xc->pending_prio != 0) {
137*4882a593Smuzhiyun 		struct xive_q *q;
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 		prio = ffs(xc->pending_prio) - 1;
140*4882a593Smuzhiyun 		DBG_VERBOSE("scan_irq: trying prio %d\n", prio);
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 		/* Try to fetch */
143*4882a593Smuzhiyun 		irq = xive_read_eq(&xc->queue[prio], just_peek);
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 		/* Found something ? That's it */
146*4882a593Smuzhiyun 		if (irq) {
147*4882a593Smuzhiyun 			if (just_peek || irq_to_desc(irq))
148*4882a593Smuzhiyun 				break;
149*4882a593Smuzhiyun 			/*
150*4882a593Smuzhiyun 			 * We should never get here; if we do then we must
151*4882a593Smuzhiyun 			 * have failed to synchronize the interrupt properly
152*4882a593Smuzhiyun 			 * when shutting it down.
153*4882a593Smuzhiyun 			 */
154*4882a593Smuzhiyun 			pr_crit("xive: got interrupt %d without descriptor, dropping\n",
155*4882a593Smuzhiyun 				irq);
156*4882a593Smuzhiyun 			WARN_ON(1);
157*4882a593Smuzhiyun 			continue;
158*4882a593Smuzhiyun 		}
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 		/* Clear pending bits */
161*4882a593Smuzhiyun 		xc->pending_prio &= ~(1 << prio);
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 		/*
164*4882a593Smuzhiyun 		 * Check if the queue count needs adjusting due to
165*4882a593Smuzhiyun 		 * interrupts being moved away. See description of
166*4882a593Smuzhiyun 		 * xive_dec_target_count()
167*4882a593Smuzhiyun 		 */
168*4882a593Smuzhiyun 		q = &xc->queue[prio];
169*4882a593Smuzhiyun 		if (atomic_read(&q->pending_count)) {
170*4882a593Smuzhiyun 			int p = atomic_xchg(&q->pending_count, 0);
171*4882a593Smuzhiyun 			if (p) {
172*4882a593Smuzhiyun 				WARN_ON(p > atomic_read(&q->count));
173*4882a593Smuzhiyun 				atomic_sub(p, &q->count);
174*4882a593Smuzhiyun 			}
175*4882a593Smuzhiyun 		}
176*4882a593Smuzhiyun 	}
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	/* If nothing was found, set CPPR to 0xff */
179*4882a593Smuzhiyun 	if (irq == 0)
180*4882a593Smuzhiyun 		prio = 0xff;
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	/* Update HW CPPR to match if necessary */
183*4882a593Smuzhiyun 	if (prio != xc->cppr) {
184*4882a593Smuzhiyun 		DBG_VERBOSE("scan_irq: adjusting CPPR to %d\n", prio);
185*4882a593Smuzhiyun 		xc->cppr = prio;
186*4882a593Smuzhiyun 		out_8(xive_tima + xive_tima_offset + TM_CPPR, prio);
187*4882a593Smuzhiyun 	}
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	return irq;
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun /*
193*4882a593Smuzhiyun  * This is used to perform the magic loads from an ESB
194*4882a593Smuzhiyun  * described in xive-regs.h
195*4882a593Smuzhiyun  */
xive_esb_read(struct xive_irq_data * xd,u32 offset)196*4882a593Smuzhiyun static notrace u8 xive_esb_read(struct xive_irq_data *xd, u32 offset)
197*4882a593Smuzhiyun {
198*4882a593Smuzhiyun 	u64 val;
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 	if (offset == XIVE_ESB_SET_PQ_10 && xd->flags & XIVE_IRQ_FLAG_STORE_EOI)
201*4882a593Smuzhiyun 		offset |= XIVE_ESB_LD_ST_MO;
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun 	/* Handle HW errata */
204*4882a593Smuzhiyun 	if (xd->flags & XIVE_IRQ_FLAG_SHIFT_BUG)
205*4882a593Smuzhiyun 		offset |= offset << 4;
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	if ((xd->flags & XIVE_IRQ_FLAG_H_INT_ESB) && xive_ops->esb_rw)
208*4882a593Smuzhiyun 		val = xive_ops->esb_rw(xd->hw_irq, offset, 0, 0);
209*4882a593Smuzhiyun 	else
210*4882a593Smuzhiyun 		val = in_be64(xd->eoi_mmio + offset);
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 	return (u8)val;
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun 
xive_esb_write(struct xive_irq_data * xd,u32 offset,u64 data)215*4882a593Smuzhiyun static void xive_esb_write(struct xive_irq_data *xd, u32 offset, u64 data)
216*4882a593Smuzhiyun {
217*4882a593Smuzhiyun 	/* Handle HW errata */
218*4882a593Smuzhiyun 	if (xd->flags & XIVE_IRQ_FLAG_SHIFT_BUG)
219*4882a593Smuzhiyun 		offset |= offset << 4;
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 	if ((xd->flags & XIVE_IRQ_FLAG_H_INT_ESB) && xive_ops->esb_rw)
222*4882a593Smuzhiyun 		xive_ops->esb_rw(xd->hw_irq, offset, data, 1);
223*4882a593Smuzhiyun 	else
224*4882a593Smuzhiyun 		out_be64(xd->eoi_mmio + offset, data);
225*4882a593Smuzhiyun }
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun #ifdef CONFIG_XMON
xive_dump_eq(const char * name,struct xive_q * q)228*4882a593Smuzhiyun static notrace void xive_dump_eq(const char *name, struct xive_q *q)
229*4882a593Smuzhiyun {
230*4882a593Smuzhiyun 	u32 i0, i1, idx;
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun 	if (!q->qpage)
233*4882a593Smuzhiyun 		return;
234*4882a593Smuzhiyun 	idx = q->idx;
235*4882a593Smuzhiyun 	i0 = be32_to_cpup(q->qpage + idx);
236*4882a593Smuzhiyun 	idx = (idx + 1) & q->msk;
237*4882a593Smuzhiyun 	i1 = be32_to_cpup(q->qpage + idx);
238*4882a593Smuzhiyun 	xmon_printf("%s idx=%d T=%d %08x %08x ...", name,
239*4882a593Smuzhiyun 		     q->idx, q->toggle, i0, i1);
240*4882a593Smuzhiyun }
241*4882a593Smuzhiyun 
xmon_xive_do_dump(int cpu)242*4882a593Smuzhiyun notrace void xmon_xive_do_dump(int cpu)
243*4882a593Smuzhiyun {
244*4882a593Smuzhiyun 	struct xive_cpu *xc = per_cpu(xive_cpu, cpu);
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 	xmon_printf("CPU %d:", cpu);
247*4882a593Smuzhiyun 	if (xc) {
248*4882a593Smuzhiyun 		xmon_printf("pp=%02x CPPR=%02x ", xc->pending_prio, xc->cppr);
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun #ifdef CONFIG_SMP
251*4882a593Smuzhiyun 		{
252*4882a593Smuzhiyun 			u64 val = xive_esb_read(&xc->ipi_data, XIVE_ESB_GET);
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 			xmon_printf("IPI=0x%08x PQ=%c%c ", xc->hw_ipi,
255*4882a593Smuzhiyun 				    val & XIVE_ESB_VAL_P ? 'P' : '-',
256*4882a593Smuzhiyun 				    val & XIVE_ESB_VAL_Q ? 'Q' : '-');
257*4882a593Smuzhiyun 		}
258*4882a593Smuzhiyun #endif
259*4882a593Smuzhiyun 		xive_dump_eq("EQ", &xc->queue[xive_irq_priority]);
260*4882a593Smuzhiyun 	}
261*4882a593Smuzhiyun 	xmon_printf("\n");
262*4882a593Smuzhiyun }
263*4882a593Smuzhiyun 
xive_get_irq_data(u32 hw_irq)264*4882a593Smuzhiyun static struct irq_data *xive_get_irq_data(u32 hw_irq)
265*4882a593Smuzhiyun {
266*4882a593Smuzhiyun 	unsigned int irq = irq_find_mapping(xive_irq_domain, hw_irq);
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 	return irq ? irq_get_irq_data(irq) : NULL;
269*4882a593Smuzhiyun }
270*4882a593Smuzhiyun 
xmon_xive_get_irq_config(u32 hw_irq,struct irq_data * d)271*4882a593Smuzhiyun int xmon_xive_get_irq_config(u32 hw_irq, struct irq_data *d)
272*4882a593Smuzhiyun {
273*4882a593Smuzhiyun 	int rc;
274*4882a593Smuzhiyun 	u32 target;
275*4882a593Smuzhiyun 	u8 prio;
276*4882a593Smuzhiyun 	u32 lirq;
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun 	rc = xive_ops->get_irq_config(hw_irq, &target, &prio, &lirq);
279*4882a593Smuzhiyun 	if (rc) {
280*4882a593Smuzhiyun 		xmon_printf("IRQ 0x%08x : no config rc=%d\n", hw_irq, rc);
281*4882a593Smuzhiyun 		return rc;
282*4882a593Smuzhiyun 	}
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun 	xmon_printf("IRQ 0x%08x : target=0x%x prio=%02x lirq=0x%x ",
285*4882a593Smuzhiyun 		    hw_irq, target, prio, lirq);
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun 	if (!d)
288*4882a593Smuzhiyun 		d = xive_get_irq_data(hw_irq);
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 	if (d) {
291*4882a593Smuzhiyun 		struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
292*4882a593Smuzhiyun 		u64 val = xive_esb_read(xd, XIVE_ESB_GET);
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 		xmon_printf("flags=%c%c%c PQ=%c%c",
295*4882a593Smuzhiyun 			    xd->flags & XIVE_IRQ_FLAG_STORE_EOI ? 'S' : ' ',
296*4882a593Smuzhiyun 			    xd->flags & XIVE_IRQ_FLAG_LSI ? 'L' : ' ',
297*4882a593Smuzhiyun 			    xd->flags & XIVE_IRQ_FLAG_H_INT_ESB ? 'H' : ' ',
298*4882a593Smuzhiyun 			    val & XIVE_ESB_VAL_P ? 'P' : '-',
299*4882a593Smuzhiyun 			    val & XIVE_ESB_VAL_Q ? 'Q' : '-');
300*4882a593Smuzhiyun 	}
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun 	xmon_printf("\n");
303*4882a593Smuzhiyun 	return 0;
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun #endif /* CONFIG_XMON */
307*4882a593Smuzhiyun 
xive_get_irq(void)308*4882a593Smuzhiyun static unsigned int xive_get_irq(void)
309*4882a593Smuzhiyun {
310*4882a593Smuzhiyun 	struct xive_cpu *xc = __this_cpu_read(xive_cpu);
311*4882a593Smuzhiyun 	u32 irq;
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun 	/*
314*4882a593Smuzhiyun 	 * This can be called either as a result of a HW interrupt or
315*4882a593Smuzhiyun 	 * as a "replay" because EOI decided there was still something
316*4882a593Smuzhiyun 	 * in one of the queues.
317*4882a593Smuzhiyun 	 *
318*4882a593Smuzhiyun 	 * First we perform an ACK cycle in order to update our mask
319*4882a593Smuzhiyun 	 * of pending priorities. This will also have the effect of
320*4882a593Smuzhiyun 	 * updating the CPPR to the most favored pending interrupts.
321*4882a593Smuzhiyun 	 *
322*4882a593Smuzhiyun 	 * In the future, if we have a way to differentiate a first
323*4882a593Smuzhiyun 	 * entry (on HW interrupt) from a replay triggered by EOI,
324*4882a593Smuzhiyun 	 * we could skip this on replays unless we soft-mask tells us
325*4882a593Smuzhiyun 	 * that a new HW interrupt occurred.
326*4882a593Smuzhiyun 	 */
327*4882a593Smuzhiyun 	xive_ops->update_pending(xc);
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun 	DBG_VERBOSE("get_irq: pending=%02x\n", xc->pending_prio);
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun 	/* Scan our queue(s) for interrupts */
332*4882a593Smuzhiyun 	irq = xive_scan_interrupts(xc, false);
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun 	DBG_VERBOSE("get_irq: got irq 0x%x, new pending=0x%02x\n",
335*4882a593Smuzhiyun 	    irq, xc->pending_prio);
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun 	/* Return pending interrupt if any */
338*4882a593Smuzhiyun 	if (irq == XIVE_BAD_IRQ)
339*4882a593Smuzhiyun 		return 0;
340*4882a593Smuzhiyun 	return irq;
341*4882a593Smuzhiyun }
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun /*
344*4882a593Smuzhiyun  * After EOI'ing an interrupt, we need to re-check the queue
345*4882a593Smuzhiyun  * to see if another interrupt is pending since multiple
346*4882a593Smuzhiyun  * interrupts can coalesce into a single notification to the
347*4882a593Smuzhiyun  * CPU.
348*4882a593Smuzhiyun  *
349*4882a593Smuzhiyun  * If we find that there is indeed more in there, we call
350*4882a593Smuzhiyun  * force_external_irq_replay() to make Linux synthetize an
351*4882a593Smuzhiyun  * external interrupt on the next call to local_irq_restore().
352*4882a593Smuzhiyun  */
xive_do_queue_eoi(struct xive_cpu * xc)353*4882a593Smuzhiyun static void xive_do_queue_eoi(struct xive_cpu *xc)
354*4882a593Smuzhiyun {
355*4882a593Smuzhiyun 	if (xive_scan_interrupts(xc, true) != 0) {
356*4882a593Smuzhiyun 		DBG_VERBOSE("eoi: pending=0x%02x\n", xc->pending_prio);
357*4882a593Smuzhiyun 		force_external_irq_replay();
358*4882a593Smuzhiyun 	}
359*4882a593Smuzhiyun }
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun /*
362*4882a593Smuzhiyun  * EOI an interrupt at the source. There are several methods
363*4882a593Smuzhiyun  * to do this depending on the HW version and source type
364*4882a593Smuzhiyun  */
xive_do_source_eoi(u32 hw_irq,struct xive_irq_data * xd)365*4882a593Smuzhiyun static void xive_do_source_eoi(u32 hw_irq, struct xive_irq_data *xd)
366*4882a593Smuzhiyun {
367*4882a593Smuzhiyun 	xd->stale_p = false;
368*4882a593Smuzhiyun 	/* If the XIVE supports the new "store EOI facility, use it */
369*4882a593Smuzhiyun 	if (xd->flags & XIVE_IRQ_FLAG_STORE_EOI)
370*4882a593Smuzhiyun 		xive_esb_write(xd, XIVE_ESB_STORE_EOI, 0);
371*4882a593Smuzhiyun 	else if (hw_irq && xd->flags & XIVE_IRQ_FLAG_EOI_FW) {
372*4882a593Smuzhiyun 		/*
373*4882a593Smuzhiyun 		 * The FW told us to call it. This happens for some
374*4882a593Smuzhiyun 		 * interrupt sources that need additional HW whacking
375*4882a593Smuzhiyun 		 * beyond the ESB manipulation. For example LPC interrupts
376*4882a593Smuzhiyun 		 * on P9 DD1.0 needed a latch to be clared in the LPC bridge
377*4882a593Smuzhiyun 		 * itself. The Firmware will take care of it.
378*4882a593Smuzhiyun 		 */
379*4882a593Smuzhiyun 		if (WARN_ON_ONCE(!xive_ops->eoi))
380*4882a593Smuzhiyun 			return;
381*4882a593Smuzhiyun 		xive_ops->eoi(hw_irq);
382*4882a593Smuzhiyun 	} else {
383*4882a593Smuzhiyun 		u8 eoi_val;
384*4882a593Smuzhiyun 
385*4882a593Smuzhiyun 		/*
386*4882a593Smuzhiyun 		 * Otherwise for EOI, we use the special MMIO that does
387*4882a593Smuzhiyun 		 * a clear of both P and Q and returns the old Q,
388*4882a593Smuzhiyun 		 * except for LSIs where we use the "EOI cycle" special
389*4882a593Smuzhiyun 		 * load.
390*4882a593Smuzhiyun 		 *
391*4882a593Smuzhiyun 		 * This allows us to then do a re-trigger if Q was set
392*4882a593Smuzhiyun 		 * rather than synthesizing an interrupt in software
393*4882a593Smuzhiyun 		 *
394*4882a593Smuzhiyun 		 * For LSIs the HW EOI cycle is used rather than PQ bits,
395*4882a593Smuzhiyun 		 * as they are automatically re-triggred in HW when still
396*4882a593Smuzhiyun 		 * pending.
397*4882a593Smuzhiyun 		 */
398*4882a593Smuzhiyun 		if (xd->flags & XIVE_IRQ_FLAG_LSI)
399*4882a593Smuzhiyun 			xive_esb_read(xd, XIVE_ESB_LOAD_EOI);
400*4882a593Smuzhiyun 		else {
401*4882a593Smuzhiyun 			eoi_val = xive_esb_read(xd, XIVE_ESB_SET_PQ_00);
402*4882a593Smuzhiyun 			DBG_VERBOSE("eoi_val=%x\n", eoi_val);
403*4882a593Smuzhiyun 
404*4882a593Smuzhiyun 			/* Re-trigger if needed */
405*4882a593Smuzhiyun 			if ((eoi_val & XIVE_ESB_VAL_Q) && xd->trig_mmio)
406*4882a593Smuzhiyun 				out_be64(xd->trig_mmio, 0);
407*4882a593Smuzhiyun 		}
408*4882a593Smuzhiyun 	}
409*4882a593Smuzhiyun }
410*4882a593Smuzhiyun 
411*4882a593Smuzhiyun /* irq_chip eoi callback, called with irq descriptor lock held */
xive_irq_eoi(struct irq_data * d)412*4882a593Smuzhiyun static void xive_irq_eoi(struct irq_data *d)
413*4882a593Smuzhiyun {
414*4882a593Smuzhiyun 	struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
415*4882a593Smuzhiyun 	struct xive_cpu *xc = __this_cpu_read(xive_cpu);
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun 	DBG_VERBOSE("eoi_irq: irq=%d [0x%lx] pending=%02x\n",
418*4882a593Smuzhiyun 		    d->irq, irqd_to_hwirq(d), xc->pending_prio);
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun 	/*
421*4882a593Smuzhiyun 	 * EOI the source if it hasn't been disabled and hasn't
422*4882a593Smuzhiyun 	 * been passed-through to a KVM guest
423*4882a593Smuzhiyun 	 */
424*4882a593Smuzhiyun 	if (!irqd_irq_disabled(d) && !irqd_is_forwarded_to_vcpu(d) &&
425*4882a593Smuzhiyun 	    !(xd->flags & XIVE_IRQ_NO_EOI))
426*4882a593Smuzhiyun 		xive_do_source_eoi(irqd_to_hwirq(d), xd);
427*4882a593Smuzhiyun 	else
428*4882a593Smuzhiyun 		xd->stale_p = true;
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun 	/*
431*4882a593Smuzhiyun 	 * Clear saved_p to indicate that it's no longer occupying
432*4882a593Smuzhiyun 	 * a queue slot on the target queue
433*4882a593Smuzhiyun 	 */
434*4882a593Smuzhiyun 	xd->saved_p = false;
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun 	/* Check for more work in the queue */
437*4882a593Smuzhiyun 	xive_do_queue_eoi(xc);
438*4882a593Smuzhiyun }
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun /*
441*4882a593Smuzhiyun  * Helper used to mask and unmask an interrupt source. This
442*4882a593Smuzhiyun  * is only called for normal interrupts that do not require
443*4882a593Smuzhiyun  * masking/unmasking via firmware.
444*4882a593Smuzhiyun  */
xive_do_source_set_mask(struct xive_irq_data * xd,bool mask)445*4882a593Smuzhiyun static void xive_do_source_set_mask(struct xive_irq_data *xd,
446*4882a593Smuzhiyun 				    bool mask)
447*4882a593Smuzhiyun {
448*4882a593Smuzhiyun 	u64 val;
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun 	/*
451*4882a593Smuzhiyun 	 * If the interrupt had P set, it may be in a queue.
452*4882a593Smuzhiyun 	 *
453*4882a593Smuzhiyun 	 * We need to make sure we don't re-enable it until it
454*4882a593Smuzhiyun 	 * has been fetched from that queue and EOId. We keep
455*4882a593Smuzhiyun 	 * a copy of that P state and use it to restore the
456*4882a593Smuzhiyun 	 * ESB accordingly on unmask.
457*4882a593Smuzhiyun 	 */
458*4882a593Smuzhiyun 	if (mask) {
459*4882a593Smuzhiyun 		val = xive_esb_read(xd, XIVE_ESB_SET_PQ_01);
460*4882a593Smuzhiyun 		if (!xd->stale_p && !!(val & XIVE_ESB_VAL_P))
461*4882a593Smuzhiyun 			xd->saved_p = true;
462*4882a593Smuzhiyun 		xd->stale_p = false;
463*4882a593Smuzhiyun 	} else if (xd->saved_p) {
464*4882a593Smuzhiyun 		xive_esb_read(xd, XIVE_ESB_SET_PQ_10);
465*4882a593Smuzhiyun 		xd->saved_p = false;
466*4882a593Smuzhiyun 	} else {
467*4882a593Smuzhiyun 		xive_esb_read(xd, XIVE_ESB_SET_PQ_00);
468*4882a593Smuzhiyun 		xd->stale_p = false;
469*4882a593Smuzhiyun 	}
470*4882a593Smuzhiyun }
471*4882a593Smuzhiyun 
472*4882a593Smuzhiyun /*
473*4882a593Smuzhiyun  * Try to chose "cpu" as a new interrupt target. Increments
474*4882a593Smuzhiyun  * the queue accounting for that target if it's not already
475*4882a593Smuzhiyun  * full.
476*4882a593Smuzhiyun  */
xive_try_pick_target(int cpu)477*4882a593Smuzhiyun static bool xive_try_pick_target(int cpu)
478*4882a593Smuzhiyun {
479*4882a593Smuzhiyun 	struct xive_cpu *xc = per_cpu(xive_cpu, cpu);
480*4882a593Smuzhiyun 	struct xive_q *q = &xc->queue[xive_irq_priority];
481*4882a593Smuzhiyun 	int max;
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun 	/*
484*4882a593Smuzhiyun 	 * Calculate max number of interrupts in that queue.
485*4882a593Smuzhiyun 	 *
486*4882a593Smuzhiyun 	 * We leave a gap of 1 just in case...
487*4882a593Smuzhiyun 	 */
488*4882a593Smuzhiyun 	max = (q->msk + 1) - 1;
489*4882a593Smuzhiyun 	return !!atomic_add_unless(&q->count, 1, max);
490*4882a593Smuzhiyun }
491*4882a593Smuzhiyun 
492*4882a593Smuzhiyun /*
493*4882a593Smuzhiyun  * Un-account an interrupt for a target CPU. We don't directly
494*4882a593Smuzhiyun  * decrement q->count since the interrupt might still be present
495*4882a593Smuzhiyun  * in the queue.
496*4882a593Smuzhiyun  *
497*4882a593Smuzhiyun  * Instead increment a separate counter "pending_count" which
498*4882a593Smuzhiyun  * will be substracted from "count" later when that CPU observes
499*4882a593Smuzhiyun  * the queue to be empty.
500*4882a593Smuzhiyun  */
xive_dec_target_count(int cpu)501*4882a593Smuzhiyun static void xive_dec_target_count(int cpu)
502*4882a593Smuzhiyun {
503*4882a593Smuzhiyun 	struct xive_cpu *xc = per_cpu(xive_cpu, cpu);
504*4882a593Smuzhiyun 	struct xive_q *q = &xc->queue[xive_irq_priority];
505*4882a593Smuzhiyun 
506*4882a593Smuzhiyun 	if (WARN_ON(cpu < 0 || !xc)) {
507*4882a593Smuzhiyun 		pr_err("%s: cpu=%d xc=%p\n", __func__, cpu, xc);
508*4882a593Smuzhiyun 		return;
509*4882a593Smuzhiyun 	}
510*4882a593Smuzhiyun 
511*4882a593Smuzhiyun 	/*
512*4882a593Smuzhiyun 	 * We increment the "pending count" which will be used
513*4882a593Smuzhiyun 	 * to decrement the target queue count whenever it's next
514*4882a593Smuzhiyun 	 * processed and found empty. This ensure that we don't
515*4882a593Smuzhiyun 	 * decrement while we still have the interrupt there
516*4882a593Smuzhiyun 	 * occupying a slot.
517*4882a593Smuzhiyun 	 */
518*4882a593Smuzhiyun 	atomic_inc(&q->pending_count);
519*4882a593Smuzhiyun }
520*4882a593Smuzhiyun 
521*4882a593Smuzhiyun /* Find a tentative CPU target in a CPU mask */
xive_find_target_in_mask(const struct cpumask * mask,unsigned int fuzz)522*4882a593Smuzhiyun static int xive_find_target_in_mask(const struct cpumask *mask,
523*4882a593Smuzhiyun 				    unsigned int fuzz)
524*4882a593Smuzhiyun {
525*4882a593Smuzhiyun 	int cpu, first, num, i;
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun 	/* Pick up a starting point CPU in the mask based on  fuzz */
528*4882a593Smuzhiyun 	num = min_t(int, cpumask_weight(mask), nr_cpu_ids);
529*4882a593Smuzhiyun 	first = fuzz % num;
530*4882a593Smuzhiyun 
531*4882a593Smuzhiyun 	/* Locate it */
532*4882a593Smuzhiyun 	cpu = cpumask_first(mask);
533*4882a593Smuzhiyun 	for (i = 0; i < first && cpu < nr_cpu_ids; i++)
534*4882a593Smuzhiyun 		cpu = cpumask_next(cpu, mask);
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun 	/* Sanity check */
537*4882a593Smuzhiyun 	if (WARN_ON(cpu >= nr_cpu_ids))
538*4882a593Smuzhiyun 		cpu = cpumask_first(cpu_online_mask);
539*4882a593Smuzhiyun 
540*4882a593Smuzhiyun 	/* Remember first one to handle wrap-around */
541*4882a593Smuzhiyun 	first = cpu;
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun 	/*
544*4882a593Smuzhiyun 	 * Now go through the entire mask until we find a valid
545*4882a593Smuzhiyun 	 * target.
546*4882a593Smuzhiyun 	 */
547*4882a593Smuzhiyun 	do {
548*4882a593Smuzhiyun 		/*
549*4882a593Smuzhiyun 		 * We re-check online as the fallback case passes us
550*4882a593Smuzhiyun 		 * an untested affinity mask
551*4882a593Smuzhiyun 		 */
552*4882a593Smuzhiyun 		if (cpu_online(cpu) && xive_try_pick_target(cpu))
553*4882a593Smuzhiyun 			return cpu;
554*4882a593Smuzhiyun 		cpu = cpumask_next(cpu, mask);
555*4882a593Smuzhiyun 		/* Wrap around */
556*4882a593Smuzhiyun 		if (cpu >= nr_cpu_ids)
557*4882a593Smuzhiyun 			cpu = cpumask_first(mask);
558*4882a593Smuzhiyun 	} while (cpu != first);
559*4882a593Smuzhiyun 
560*4882a593Smuzhiyun 	return -1;
561*4882a593Smuzhiyun }
562*4882a593Smuzhiyun 
563*4882a593Smuzhiyun /*
564*4882a593Smuzhiyun  * Pick a target CPU for an interrupt. This is done at
565*4882a593Smuzhiyun  * startup or if the affinity is changed in a way that
566*4882a593Smuzhiyun  * invalidates the current target.
567*4882a593Smuzhiyun  */
xive_pick_irq_target(struct irq_data * d,const struct cpumask * affinity)568*4882a593Smuzhiyun static int xive_pick_irq_target(struct irq_data *d,
569*4882a593Smuzhiyun 				const struct cpumask *affinity)
570*4882a593Smuzhiyun {
571*4882a593Smuzhiyun 	static unsigned int fuzz;
572*4882a593Smuzhiyun 	struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
573*4882a593Smuzhiyun 	cpumask_var_t mask;
574*4882a593Smuzhiyun 	int cpu = -1;
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun 	/*
577*4882a593Smuzhiyun 	 * If we have chip IDs, first we try to build a mask of
578*4882a593Smuzhiyun 	 * CPUs matching the CPU and find a target in there
579*4882a593Smuzhiyun 	 */
580*4882a593Smuzhiyun 	if (xd->src_chip != XIVE_INVALID_CHIP_ID &&
581*4882a593Smuzhiyun 		zalloc_cpumask_var(&mask, GFP_ATOMIC)) {
582*4882a593Smuzhiyun 		/* Build a mask of matching chip IDs */
583*4882a593Smuzhiyun 		for_each_cpu_and(cpu, affinity, cpu_online_mask) {
584*4882a593Smuzhiyun 			struct xive_cpu *xc = per_cpu(xive_cpu, cpu);
585*4882a593Smuzhiyun 			if (xc->chip_id == xd->src_chip)
586*4882a593Smuzhiyun 				cpumask_set_cpu(cpu, mask);
587*4882a593Smuzhiyun 		}
588*4882a593Smuzhiyun 		/* Try to find a target */
589*4882a593Smuzhiyun 		if (cpumask_empty(mask))
590*4882a593Smuzhiyun 			cpu = -1;
591*4882a593Smuzhiyun 		else
592*4882a593Smuzhiyun 			cpu = xive_find_target_in_mask(mask, fuzz++);
593*4882a593Smuzhiyun 		free_cpumask_var(mask);
594*4882a593Smuzhiyun 		if (cpu >= 0)
595*4882a593Smuzhiyun 			return cpu;
596*4882a593Smuzhiyun 		fuzz--;
597*4882a593Smuzhiyun 	}
598*4882a593Smuzhiyun 
599*4882a593Smuzhiyun 	/* No chip IDs, fallback to using the affinity mask */
600*4882a593Smuzhiyun 	return xive_find_target_in_mask(affinity, fuzz++);
601*4882a593Smuzhiyun }
602*4882a593Smuzhiyun 
xive_irq_startup(struct irq_data * d)603*4882a593Smuzhiyun static unsigned int xive_irq_startup(struct irq_data *d)
604*4882a593Smuzhiyun {
605*4882a593Smuzhiyun 	struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
606*4882a593Smuzhiyun 	unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
607*4882a593Smuzhiyun 	int target, rc;
608*4882a593Smuzhiyun 
609*4882a593Smuzhiyun 	xd->saved_p = false;
610*4882a593Smuzhiyun 	xd->stale_p = false;
611*4882a593Smuzhiyun 	pr_devel("xive_irq_startup: irq %d [0x%x] data @%p\n",
612*4882a593Smuzhiyun 		 d->irq, hw_irq, d);
613*4882a593Smuzhiyun 
614*4882a593Smuzhiyun #ifdef CONFIG_PCI_MSI
615*4882a593Smuzhiyun 	/*
616*4882a593Smuzhiyun 	 * The generic MSI code returns with the interrupt disabled on the
617*4882a593Smuzhiyun 	 * card, using the MSI mask bits. Firmware doesn't appear to unmask
618*4882a593Smuzhiyun 	 * at that level, so we do it here by hand.
619*4882a593Smuzhiyun 	 */
620*4882a593Smuzhiyun 	if (irq_data_get_msi_desc(d))
621*4882a593Smuzhiyun 		pci_msi_unmask_irq(d);
622*4882a593Smuzhiyun #endif
623*4882a593Smuzhiyun 
624*4882a593Smuzhiyun 	/* Pick a target */
625*4882a593Smuzhiyun 	target = xive_pick_irq_target(d, irq_data_get_affinity_mask(d));
626*4882a593Smuzhiyun 	if (target == XIVE_INVALID_TARGET) {
627*4882a593Smuzhiyun 		/* Try again breaking affinity */
628*4882a593Smuzhiyun 		target = xive_pick_irq_target(d, cpu_online_mask);
629*4882a593Smuzhiyun 		if (target == XIVE_INVALID_TARGET)
630*4882a593Smuzhiyun 			return -ENXIO;
631*4882a593Smuzhiyun 		pr_warn("irq %d started with broken affinity\n", d->irq);
632*4882a593Smuzhiyun 	}
633*4882a593Smuzhiyun 
634*4882a593Smuzhiyun 	/* Sanity check */
635*4882a593Smuzhiyun 	if (WARN_ON(target == XIVE_INVALID_TARGET ||
636*4882a593Smuzhiyun 		    target >= nr_cpu_ids))
637*4882a593Smuzhiyun 		target = smp_processor_id();
638*4882a593Smuzhiyun 
639*4882a593Smuzhiyun 	xd->target = target;
640*4882a593Smuzhiyun 
641*4882a593Smuzhiyun 	/*
642*4882a593Smuzhiyun 	 * Configure the logical number to be the Linux IRQ number
643*4882a593Smuzhiyun 	 * and set the target queue
644*4882a593Smuzhiyun 	 */
645*4882a593Smuzhiyun 	rc = xive_ops->configure_irq(hw_irq,
646*4882a593Smuzhiyun 				     get_hard_smp_processor_id(target),
647*4882a593Smuzhiyun 				     xive_irq_priority, d->irq);
648*4882a593Smuzhiyun 	if (rc)
649*4882a593Smuzhiyun 		return rc;
650*4882a593Smuzhiyun 
651*4882a593Smuzhiyun 	/* Unmask the ESB */
652*4882a593Smuzhiyun 	xive_do_source_set_mask(xd, false);
653*4882a593Smuzhiyun 
654*4882a593Smuzhiyun 	return 0;
655*4882a593Smuzhiyun }
656*4882a593Smuzhiyun 
657*4882a593Smuzhiyun /* called with irq descriptor lock held */
xive_irq_shutdown(struct irq_data * d)658*4882a593Smuzhiyun static void xive_irq_shutdown(struct irq_data *d)
659*4882a593Smuzhiyun {
660*4882a593Smuzhiyun 	struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
661*4882a593Smuzhiyun 	unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
662*4882a593Smuzhiyun 
663*4882a593Smuzhiyun 	pr_devel("xive_irq_shutdown: irq %d [0x%x] data @%p\n",
664*4882a593Smuzhiyun 		 d->irq, hw_irq, d);
665*4882a593Smuzhiyun 
666*4882a593Smuzhiyun 	if (WARN_ON(xd->target == XIVE_INVALID_TARGET))
667*4882a593Smuzhiyun 		return;
668*4882a593Smuzhiyun 
669*4882a593Smuzhiyun 	/* Mask the interrupt at the source */
670*4882a593Smuzhiyun 	xive_do_source_set_mask(xd, true);
671*4882a593Smuzhiyun 
672*4882a593Smuzhiyun 	/*
673*4882a593Smuzhiyun 	 * Mask the interrupt in HW in the IVT/EAS and set the number
674*4882a593Smuzhiyun 	 * to be the "bad" IRQ number
675*4882a593Smuzhiyun 	 */
676*4882a593Smuzhiyun 	xive_ops->configure_irq(hw_irq,
677*4882a593Smuzhiyun 				get_hard_smp_processor_id(xd->target),
678*4882a593Smuzhiyun 				0xff, XIVE_BAD_IRQ);
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun 	xive_dec_target_count(xd->target);
681*4882a593Smuzhiyun 	xd->target = XIVE_INVALID_TARGET;
682*4882a593Smuzhiyun }
683*4882a593Smuzhiyun 
xive_irq_unmask(struct irq_data * d)684*4882a593Smuzhiyun static void xive_irq_unmask(struct irq_data *d)
685*4882a593Smuzhiyun {
686*4882a593Smuzhiyun 	struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
687*4882a593Smuzhiyun 
688*4882a593Smuzhiyun 	pr_devel("xive_irq_unmask: irq %d data @%p\n", d->irq, xd);
689*4882a593Smuzhiyun 
690*4882a593Smuzhiyun 	/*
691*4882a593Smuzhiyun 	 * This is a workaround for PCI LSI problems on P9, for
692*4882a593Smuzhiyun 	 * these, we call FW to set the mask. The problems might
693*4882a593Smuzhiyun 	 * be fixed by P9 DD2.0, if that is the case, firmware
694*4882a593Smuzhiyun 	 * will no longer set that flag.
695*4882a593Smuzhiyun 	 */
696*4882a593Smuzhiyun 	if (xd->flags & XIVE_IRQ_FLAG_MASK_FW) {
697*4882a593Smuzhiyun 		unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
698*4882a593Smuzhiyun 		xive_ops->configure_irq(hw_irq,
699*4882a593Smuzhiyun 					get_hard_smp_processor_id(xd->target),
700*4882a593Smuzhiyun 					xive_irq_priority, d->irq);
701*4882a593Smuzhiyun 		return;
702*4882a593Smuzhiyun 	}
703*4882a593Smuzhiyun 
704*4882a593Smuzhiyun 	xive_do_source_set_mask(xd, false);
705*4882a593Smuzhiyun }
706*4882a593Smuzhiyun 
xive_irq_mask(struct irq_data * d)707*4882a593Smuzhiyun static void xive_irq_mask(struct irq_data *d)
708*4882a593Smuzhiyun {
709*4882a593Smuzhiyun 	struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
710*4882a593Smuzhiyun 
711*4882a593Smuzhiyun 	pr_devel("xive_irq_mask: irq %d data @%p\n", d->irq, xd);
712*4882a593Smuzhiyun 
713*4882a593Smuzhiyun 	/*
714*4882a593Smuzhiyun 	 * This is a workaround for PCI LSI problems on P9, for
715*4882a593Smuzhiyun 	 * these, we call OPAL to set the mask. The problems might
716*4882a593Smuzhiyun 	 * be fixed by P9 DD2.0, if that is the case, firmware
717*4882a593Smuzhiyun 	 * will no longer set that flag.
718*4882a593Smuzhiyun 	 */
719*4882a593Smuzhiyun 	if (xd->flags & XIVE_IRQ_FLAG_MASK_FW) {
720*4882a593Smuzhiyun 		unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
721*4882a593Smuzhiyun 		xive_ops->configure_irq(hw_irq,
722*4882a593Smuzhiyun 					get_hard_smp_processor_id(xd->target),
723*4882a593Smuzhiyun 					0xff, d->irq);
724*4882a593Smuzhiyun 		return;
725*4882a593Smuzhiyun 	}
726*4882a593Smuzhiyun 
727*4882a593Smuzhiyun 	xive_do_source_set_mask(xd, true);
728*4882a593Smuzhiyun }
729*4882a593Smuzhiyun 
xive_irq_set_affinity(struct irq_data * d,const struct cpumask * cpumask,bool force)730*4882a593Smuzhiyun static int xive_irq_set_affinity(struct irq_data *d,
731*4882a593Smuzhiyun 				 const struct cpumask *cpumask,
732*4882a593Smuzhiyun 				 bool force)
733*4882a593Smuzhiyun {
734*4882a593Smuzhiyun 	struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
735*4882a593Smuzhiyun 	unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
736*4882a593Smuzhiyun 	u32 target, old_target;
737*4882a593Smuzhiyun 	int rc = 0;
738*4882a593Smuzhiyun 
739*4882a593Smuzhiyun 	pr_devel("xive_irq_set_affinity: irq %d\n", d->irq);
740*4882a593Smuzhiyun 
741*4882a593Smuzhiyun 	/* Is this valid ? */
742*4882a593Smuzhiyun 	if (cpumask_any_and(cpumask, cpu_online_mask) >= nr_cpu_ids)
743*4882a593Smuzhiyun 		return -EINVAL;
744*4882a593Smuzhiyun 
745*4882a593Smuzhiyun 	/* Don't do anything if the interrupt isn't started */
746*4882a593Smuzhiyun 	if (!irqd_is_started(d))
747*4882a593Smuzhiyun 		return IRQ_SET_MASK_OK;
748*4882a593Smuzhiyun 
749*4882a593Smuzhiyun 	/*
750*4882a593Smuzhiyun 	 * If existing target is already in the new mask, and is
751*4882a593Smuzhiyun 	 * online then do nothing.
752*4882a593Smuzhiyun 	 */
753*4882a593Smuzhiyun 	if (xd->target != XIVE_INVALID_TARGET &&
754*4882a593Smuzhiyun 	    cpu_online(xd->target) &&
755*4882a593Smuzhiyun 	    cpumask_test_cpu(xd->target, cpumask))
756*4882a593Smuzhiyun 		return IRQ_SET_MASK_OK;
757*4882a593Smuzhiyun 
758*4882a593Smuzhiyun 	/* Pick a new target */
759*4882a593Smuzhiyun 	target = xive_pick_irq_target(d, cpumask);
760*4882a593Smuzhiyun 
761*4882a593Smuzhiyun 	/* No target found */
762*4882a593Smuzhiyun 	if (target == XIVE_INVALID_TARGET)
763*4882a593Smuzhiyun 		return -ENXIO;
764*4882a593Smuzhiyun 
765*4882a593Smuzhiyun 	/* Sanity check */
766*4882a593Smuzhiyun 	if (WARN_ON(target >= nr_cpu_ids))
767*4882a593Smuzhiyun 		target = smp_processor_id();
768*4882a593Smuzhiyun 
769*4882a593Smuzhiyun 	old_target = xd->target;
770*4882a593Smuzhiyun 
771*4882a593Smuzhiyun 	/*
772*4882a593Smuzhiyun 	 * Only configure the irq if it's not currently passed-through to
773*4882a593Smuzhiyun 	 * a KVM guest
774*4882a593Smuzhiyun 	 */
775*4882a593Smuzhiyun 	if (!irqd_is_forwarded_to_vcpu(d))
776*4882a593Smuzhiyun 		rc = xive_ops->configure_irq(hw_irq,
777*4882a593Smuzhiyun 					     get_hard_smp_processor_id(target),
778*4882a593Smuzhiyun 					     xive_irq_priority, d->irq);
779*4882a593Smuzhiyun 	if (rc < 0) {
780*4882a593Smuzhiyun 		pr_err("Error %d reconfiguring irq %d\n", rc, d->irq);
781*4882a593Smuzhiyun 		return rc;
782*4882a593Smuzhiyun 	}
783*4882a593Smuzhiyun 
784*4882a593Smuzhiyun 	pr_devel("  target: 0x%x\n", target);
785*4882a593Smuzhiyun 	xd->target = target;
786*4882a593Smuzhiyun 
787*4882a593Smuzhiyun 	/* Give up previous target */
788*4882a593Smuzhiyun 	if (old_target != XIVE_INVALID_TARGET)
789*4882a593Smuzhiyun 	    xive_dec_target_count(old_target);
790*4882a593Smuzhiyun 
791*4882a593Smuzhiyun 	return IRQ_SET_MASK_OK;
792*4882a593Smuzhiyun }
793*4882a593Smuzhiyun 
xive_irq_set_type(struct irq_data * d,unsigned int flow_type)794*4882a593Smuzhiyun static int xive_irq_set_type(struct irq_data *d, unsigned int flow_type)
795*4882a593Smuzhiyun {
796*4882a593Smuzhiyun 	struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
797*4882a593Smuzhiyun 
798*4882a593Smuzhiyun 	/*
799*4882a593Smuzhiyun 	 * We only support these. This has really no effect other than setting
800*4882a593Smuzhiyun 	 * the corresponding descriptor bits mind you but those will in turn
801*4882a593Smuzhiyun 	 * affect the resend function when re-enabling an edge interrupt.
802*4882a593Smuzhiyun 	 *
803*4882a593Smuzhiyun 	 * Set set the default to edge as explained in map().
804*4882a593Smuzhiyun 	 */
805*4882a593Smuzhiyun 	if (flow_type == IRQ_TYPE_DEFAULT || flow_type == IRQ_TYPE_NONE)
806*4882a593Smuzhiyun 		flow_type = IRQ_TYPE_EDGE_RISING;
807*4882a593Smuzhiyun 
808*4882a593Smuzhiyun 	if (flow_type != IRQ_TYPE_EDGE_RISING &&
809*4882a593Smuzhiyun 	    flow_type != IRQ_TYPE_LEVEL_LOW)
810*4882a593Smuzhiyun 		return -EINVAL;
811*4882a593Smuzhiyun 
812*4882a593Smuzhiyun 	irqd_set_trigger_type(d, flow_type);
813*4882a593Smuzhiyun 
814*4882a593Smuzhiyun 	/*
815*4882a593Smuzhiyun 	 * Double check it matches what the FW thinks
816*4882a593Smuzhiyun 	 *
817*4882a593Smuzhiyun 	 * NOTE: We don't know yet if the PAPR interface will provide
818*4882a593Smuzhiyun 	 * the LSI vs MSI information apart from the device-tree so
819*4882a593Smuzhiyun 	 * this check might have to move into an optional backend call
820*4882a593Smuzhiyun 	 * that is specific to the native backend
821*4882a593Smuzhiyun 	 */
822*4882a593Smuzhiyun 	if ((flow_type == IRQ_TYPE_LEVEL_LOW) !=
823*4882a593Smuzhiyun 	    !!(xd->flags & XIVE_IRQ_FLAG_LSI)) {
824*4882a593Smuzhiyun 		pr_warn("Interrupt %d (HW 0x%x) type mismatch, Linux says %s, FW says %s\n",
825*4882a593Smuzhiyun 			d->irq, (u32)irqd_to_hwirq(d),
826*4882a593Smuzhiyun 			(flow_type == IRQ_TYPE_LEVEL_LOW) ? "Level" : "Edge",
827*4882a593Smuzhiyun 			(xd->flags & XIVE_IRQ_FLAG_LSI) ? "Level" : "Edge");
828*4882a593Smuzhiyun 	}
829*4882a593Smuzhiyun 
830*4882a593Smuzhiyun 	return IRQ_SET_MASK_OK_NOCOPY;
831*4882a593Smuzhiyun }
832*4882a593Smuzhiyun 
xive_irq_retrigger(struct irq_data * d)833*4882a593Smuzhiyun static int xive_irq_retrigger(struct irq_data *d)
834*4882a593Smuzhiyun {
835*4882a593Smuzhiyun 	struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
836*4882a593Smuzhiyun 
837*4882a593Smuzhiyun 	/* This should be only for MSIs */
838*4882a593Smuzhiyun 	if (WARN_ON(xd->flags & XIVE_IRQ_FLAG_LSI))
839*4882a593Smuzhiyun 		return 0;
840*4882a593Smuzhiyun 
841*4882a593Smuzhiyun 	/*
842*4882a593Smuzhiyun 	 * To perform a retrigger, we first set the PQ bits to
843*4882a593Smuzhiyun 	 * 11, then perform an EOI.
844*4882a593Smuzhiyun 	 */
845*4882a593Smuzhiyun 	xive_esb_read(xd, XIVE_ESB_SET_PQ_11);
846*4882a593Smuzhiyun 
847*4882a593Smuzhiyun 	/*
848*4882a593Smuzhiyun 	 * Note: We pass "0" to the hw_irq argument in order to
849*4882a593Smuzhiyun 	 * avoid calling into the backend EOI code which we don't
850*4882a593Smuzhiyun 	 * want to do in the case of a re-trigger. Backends typically
851*4882a593Smuzhiyun 	 * only do EOI for LSIs anyway.
852*4882a593Smuzhiyun 	 */
853*4882a593Smuzhiyun 	xive_do_source_eoi(0, xd);
854*4882a593Smuzhiyun 
855*4882a593Smuzhiyun 	return 1;
856*4882a593Smuzhiyun }
857*4882a593Smuzhiyun 
858*4882a593Smuzhiyun /*
859*4882a593Smuzhiyun  * Caller holds the irq descriptor lock, so this won't be called
860*4882a593Smuzhiyun  * concurrently with xive_get_irqchip_state on the same interrupt.
861*4882a593Smuzhiyun  */
xive_irq_set_vcpu_affinity(struct irq_data * d,void * state)862*4882a593Smuzhiyun static int xive_irq_set_vcpu_affinity(struct irq_data *d, void *state)
863*4882a593Smuzhiyun {
864*4882a593Smuzhiyun 	struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
865*4882a593Smuzhiyun 	unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
866*4882a593Smuzhiyun 	int rc;
867*4882a593Smuzhiyun 	u8 pq;
868*4882a593Smuzhiyun 
869*4882a593Smuzhiyun 	/*
870*4882a593Smuzhiyun 	 * We only support this on interrupts that do not require
871*4882a593Smuzhiyun 	 * firmware calls for masking and unmasking
872*4882a593Smuzhiyun 	 */
873*4882a593Smuzhiyun 	if (xd->flags & XIVE_IRQ_FLAG_MASK_FW)
874*4882a593Smuzhiyun 		return -EIO;
875*4882a593Smuzhiyun 
876*4882a593Smuzhiyun 	/*
877*4882a593Smuzhiyun 	 * This is called by KVM with state non-NULL for enabling
878*4882a593Smuzhiyun 	 * pass-through or NULL for disabling it
879*4882a593Smuzhiyun 	 */
880*4882a593Smuzhiyun 	if (state) {
881*4882a593Smuzhiyun 		irqd_set_forwarded_to_vcpu(d);
882*4882a593Smuzhiyun 
883*4882a593Smuzhiyun 		/* Set it to PQ=10 state to prevent further sends */
884*4882a593Smuzhiyun 		pq = xive_esb_read(xd, XIVE_ESB_SET_PQ_10);
885*4882a593Smuzhiyun 		if (!xd->stale_p) {
886*4882a593Smuzhiyun 			xd->saved_p = !!(pq & XIVE_ESB_VAL_P);
887*4882a593Smuzhiyun 			xd->stale_p = !xd->saved_p;
888*4882a593Smuzhiyun 		}
889*4882a593Smuzhiyun 
890*4882a593Smuzhiyun 		/* No target ? nothing to do */
891*4882a593Smuzhiyun 		if (xd->target == XIVE_INVALID_TARGET) {
892*4882a593Smuzhiyun 			/*
893*4882a593Smuzhiyun 			 * An untargetted interrupt should have been
894*4882a593Smuzhiyun 			 * also masked at the source
895*4882a593Smuzhiyun 			 */
896*4882a593Smuzhiyun 			WARN_ON(xd->saved_p);
897*4882a593Smuzhiyun 
898*4882a593Smuzhiyun 			return 0;
899*4882a593Smuzhiyun 		}
900*4882a593Smuzhiyun 
901*4882a593Smuzhiyun 		/*
902*4882a593Smuzhiyun 		 * If P was set, adjust state to PQ=11 to indicate
903*4882a593Smuzhiyun 		 * that a resend is needed for the interrupt to reach
904*4882a593Smuzhiyun 		 * the guest. Also remember the value of P.
905*4882a593Smuzhiyun 		 *
906*4882a593Smuzhiyun 		 * This also tells us that it's in flight to a host queue
907*4882a593Smuzhiyun 		 * or has already been fetched but hasn't been EOIed yet
908*4882a593Smuzhiyun 		 * by the host. This it's potentially using up a host
909*4882a593Smuzhiyun 		 * queue slot. This is important to know because as long
910*4882a593Smuzhiyun 		 * as this is the case, we must not hard-unmask it when
911*4882a593Smuzhiyun 		 * "returning" that interrupt to the host.
912*4882a593Smuzhiyun 		 *
913*4882a593Smuzhiyun 		 * This saved_p is cleared by the host EOI, when we know
914*4882a593Smuzhiyun 		 * for sure the queue slot is no longer in use.
915*4882a593Smuzhiyun 		 */
916*4882a593Smuzhiyun 		if (xd->saved_p) {
917*4882a593Smuzhiyun 			xive_esb_read(xd, XIVE_ESB_SET_PQ_11);
918*4882a593Smuzhiyun 
919*4882a593Smuzhiyun 			/*
920*4882a593Smuzhiyun 			 * Sync the XIVE source HW to ensure the interrupt
921*4882a593Smuzhiyun 			 * has gone through the EAS before we change its
922*4882a593Smuzhiyun 			 * target to the guest. That should guarantee us
923*4882a593Smuzhiyun 			 * that we *will* eventually get an EOI for it on
924*4882a593Smuzhiyun 			 * the host. Otherwise there would be a small window
925*4882a593Smuzhiyun 			 * for P to be seen here but the interrupt going
926*4882a593Smuzhiyun 			 * to the guest queue.
927*4882a593Smuzhiyun 			 */
928*4882a593Smuzhiyun 			if (xive_ops->sync_source)
929*4882a593Smuzhiyun 				xive_ops->sync_source(hw_irq);
930*4882a593Smuzhiyun 		}
931*4882a593Smuzhiyun 	} else {
932*4882a593Smuzhiyun 		irqd_clr_forwarded_to_vcpu(d);
933*4882a593Smuzhiyun 
934*4882a593Smuzhiyun 		/* No host target ? hard mask and return */
935*4882a593Smuzhiyun 		if (xd->target == XIVE_INVALID_TARGET) {
936*4882a593Smuzhiyun 			xive_do_source_set_mask(xd, true);
937*4882a593Smuzhiyun 			return 0;
938*4882a593Smuzhiyun 		}
939*4882a593Smuzhiyun 
940*4882a593Smuzhiyun 		/*
941*4882a593Smuzhiyun 		 * Sync the XIVE source HW to ensure the interrupt
942*4882a593Smuzhiyun 		 * has gone through the EAS before we change its
943*4882a593Smuzhiyun 		 * target to the host.
944*4882a593Smuzhiyun 		 */
945*4882a593Smuzhiyun 		if (xive_ops->sync_source)
946*4882a593Smuzhiyun 			xive_ops->sync_source(hw_irq);
947*4882a593Smuzhiyun 
948*4882a593Smuzhiyun 		/*
949*4882a593Smuzhiyun 		 * By convention we are called with the interrupt in
950*4882a593Smuzhiyun 		 * a PQ=10 or PQ=11 state, ie, it won't fire and will
951*4882a593Smuzhiyun 		 * have latched in Q whether there's a pending HW
952*4882a593Smuzhiyun 		 * interrupt or not.
953*4882a593Smuzhiyun 		 *
954*4882a593Smuzhiyun 		 * First reconfigure the target.
955*4882a593Smuzhiyun 		 */
956*4882a593Smuzhiyun 		rc = xive_ops->configure_irq(hw_irq,
957*4882a593Smuzhiyun 					     get_hard_smp_processor_id(xd->target),
958*4882a593Smuzhiyun 					     xive_irq_priority, d->irq);
959*4882a593Smuzhiyun 		if (rc)
960*4882a593Smuzhiyun 			return rc;
961*4882a593Smuzhiyun 
962*4882a593Smuzhiyun 		/*
963*4882a593Smuzhiyun 		 * Then if saved_p is not set, effectively re-enable the
964*4882a593Smuzhiyun 		 * interrupt with an EOI. If it is set, we know there is
965*4882a593Smuzhiyun 		 * still a message in a host queue somewhere that will be
966*4882a593Smuzhiyun 		 * EOId eventually.
967*4882a593Smuzhiyun 		 *
968*4882a593Smuzhiyun 		 * Note: We don't check irqd_irq_disabled(). Effectively,
969*4882a593Smuzhiyun 		 * we *will* let the irq get through even if masked if the
970*4882a593Smuzhiyun 		 * HW is still firing it in order to deal with the whole
971*4882a593Smuzhiyun 		 * saved_p business properly. If the interrupt triggers
972*4882a593Smuzhiyun 		 * while masked, the generic code will re-mask it anyway.
973*4882a593Smuzhiyun 		 */
974*4882a593Smuzhiyun 		if (!xd->saved_p)
975*4882a593Smuzhiyun 			xive_do_source_eoi(hw_irq, xd);
976*4882a593Smuzhiyun 
977*4882a593Smuzhiyun 	}
978*4882a593Smuzhiyun 	return 0;
979*4882a593Smuzhiyun }
980*4882a593Smuzhiyun 
981*4882a593Smuzhiyun /* Called with irq descriptor lock held. */
xive_get_irqchip_state(struct irq_data * data,enum irqchip_irq_state which,bool * state)982*4882a593Smuzhiyun static int xive_get_irqchip_state(struct irq_data *data,
983*4882a593Smuzhiyun 				  enum irqchip_irq_state which, bool *state)
984*4882a593Smuzhiyun {
985*4882a593Smuzhiyun 	struct xive_irq_data *xd = irq_data_get_irq_handler_data(data);
986*4882a593Smuzhiyun 	u8 pq;
987*4882a593Smuzhiyun 
988*4882a593Smuzhiyun 	switch (which) {
989*4882a593Smuzhiyun 	case IRQCHIP_STATE_ACTIVE:
990*4882a593Smuzhiyun 		pq = xive_esb_read(xd, XIVE_ESB_GET);
991*4882a593Smuzhiyun 
992*4882a593Smuzhiyun 		/*
993*4882a593Smuzhiyun 		 * The esb value being all 1's means we couldn't get
994*4882a593Smuzhiyun 		 * the PQ state of the interrupt through mmio. It may
995*4882a593Smuzhiyun 		 * happen, for example when querying a PHB interrupt
996*4882a593Smuzhiyun 		 * while the PHB is in an error state. We consider the
997*4882a593Smuzhiyun 		 * interrupt to be inactive in that case.
998*4882a593Smuzhiyun 		 */
999*4882a593Smuzhiyun 		*state = (pq != XIVE_ESB_INVALID) && !xd->stale_p &&
1000*4882a593Smuzhiyun 			(xd->saved_p || (!!(pq & XIVE_ESB_VAL_P) &&
1001*4882a593Smuzhiyun 			 !irqd_irq_disabled(data)));
1002*4882a593Smuzhiyun 		return 0;
1003*4882a593Smuzhiyun 	default:
1004*4882a593Smuzhiyun 		return -EINVAL;
1005*4882a593Smuzhiyun 	}
1006*4882a593Smuzhiyun }
1007*4882a593Smuzhiyun 
1008*4882a593Smuzhiyun static struct irq_chip xive_irq_chip = {
1009*4882a593Smuzhiyun 	.name = "XIVE-IRQ",
1010*4882a593Smuzhiyun 	.irq_startup = xive_irq_startup,
1011*4882a593Smuzhiyun 	.irq_shutdown = xive_irq_shutdown,
1012*4882a593Smuzhiyun 	.irq_eoi = xive_irq_eoi,
1013*4882a593Smuzhiyun 	.irq_mask = xive_irq_mask,
1014*4882a593Smuzhiyun 	.irq_unmask = xive_irq_unmask,
1015*4882a593Smuzhiyun 	.irq_set_affinity = xive_irq_set_affinity,
1016*4882a593Smuzhiyun 	.irq_set_type = xive_irq_set_type,
1017*4882a593Smuzhiyun 	.irq_retrigger = xive_irq_retrigger,
1018*4882a593Smuzhiyun 	.irq_set_vcpu_affinity = xive_irq_set_vcpu_affinity,
1019*4882a593Smuzhiyun 	.irq_get_irqchip_state = xive_get_irqchip_state,
1020*4882a593Smuzhiyun };
1021*4882a593Smuzhiyun 
is_xive_irq(struct irq_chip * chip)1022*4882a593Smuzhiyun bool is_xive_irq(struct irq_chip *chip)
1023*4882a593Smuzhiyun {
1024*4882a593Smuzhiyun 	return chip == &xive_irq_chip;
1025*4882a593Smuzhiyun }
1026*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(is_xive_irq);
1027*4882a593Smuzhiyun 
xive_cleanup_irq_data(struct xive_irq_data * xd)1028*4882a593Smuzhiyun void xive_cleanup_irq_data(struct xive_irq_data *xd)
1029*4882a593Smuzhiyun {
1030*4882a593Smuzhiyun 	if (xd->eoi_mmio) {
1031*4882a593Smuzhiyun 		unmap_kernel_range((unsigned long)xd->eoi_mmio,
1032*4882a593Smuzhiyun 				   1u << xd->esb_shift);
1033*4882a593Smuzhiyun 		iounmap(xd->eoi_mmio);
1034*4882a593Smuzhiyun 		if (xd->eoi_mmio == xd->trig_mmio)
1035*4882a593Smuzhiyun 			xd->trig_mmio = NULL;
1036*4882a593Smuzhiyun 		xd->eoi_mmio = NULL;
1037*4882a593Smuzhiyun 	}
1038*4882a593Smuzhiyun 	if (xd->trig_mmio) {
1039*4882a593Smuzhiyun 		unmap_kernel_range((unsigned long)xd->trig_mmio,
1040*4882a593Smuzhiyun 				   1u << xd->esb_shift);
1041*4882a593Smuzhiyun 		iounmap(xd->trig_mmio);
1042*4882a593Smuzhiyun 		xd->trig_mmio = NULL;
1043*4882a593Smuzhiyun 	}
1044*4882a593Smuzhiyun }
1045*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xive_cleanup_irq_data);
1046*4882a593Smuzhiyun 
xive_irq_alloc_data(unsigned int virq,irq_hw_number_t hw)1047*4882a593Smuzhiyun static int xive_irq_alloc_data(unsigned int virq, irq_hw_number_t hw)
1048*4882a593Smuzhiyun {
1049*4882a593Smuzhiyun 	struct xive_irq_data *xd;
1050*4882a593Smuzhiyun 	int rc;
1051*4882a593Smuzhiyun 
1052*4882a593Smuzhiyun 	xd = kzalloc(sizeof(struct xive_irq_data), GFP_KERNEL);
1053*4882a593Smuzhiyun 	if (!xd)
1054*4882a593Smuzhiyun 		return -ENOMEM;
1055*4882a593Smuzhiyun 	rc = xive_ops->populate_irq_data(hw, xd);
1056*4882a593Smuzhiyun 	if (rc) {
1057*4882a593Smuzhiyun 		kfree(xd);
1058*4882a593Smuzhiyun 		return rc;
1059*4882a593Smuzhiyun 	}
1060*4882a593Smuzhiyun 	xd->target = XIVE_INVALID_TARGET;
1061*4882a593Smuzhiyun 	irq_set_handler_data(virq, xd);
1062*4882a593Smuzhiyun 
1063*4882a593Smuzhiyun 	/*
1064*4882a593Smuzhiyun 	 * Turn OFF by default the interrupt being mapped. A side
1065*4882a593Smuzhiyun 	 * effect of this check is the mapping the ESB page of the
1066*4882a593Smuzhiyun 	 * interrupt in the Linux address space. This prevents page
1067*4882a593Smuzhiyun 	 * fault issues in the crash handler which masks all
1068*4882a593Smuzhiyun 	 * interrupts.
1069*4882a593Smuzhiyun 	 */
1070*4882a593Smuzhiyun 	xive_esb_read(xd, XIVE_ESB_SET_PQ_01);
1071*4882a593Smuzhiyun 
1072*4882a593Smuzhiyun 	return 0;
1073*4882a593Smuzhiyun }
1074*4882a593Smuzhiyun 
xive_irq_free_data(unsigned int virq)1075*4882a593Smuzhiyun static void xive_irq_free_data(unsigned int virq)
1076*4882a593Smuzhiyun {
1077*4882a593Smuzhiyun 	struct xive_irq_data *xd = irq_get_handler_data(virq);
1078*4882a593Smuzhiyun 
1079*4882a593Smuzhiyun 	if (!xd)
1080*4882a593Smuzhiyun 		return;
1081*4882a593Smuzhiyun 	irq_set_handler_data(virq, NULL);
1082*4882a593Smuzhiyun 	xive_cleanup_irq_data(xd);
1083*4882a593Smuzhiyun 	kfree(xd);
1084*4882a593Smuzhiyun }
1085*4882a593Smuzhiyun 
1086*4882a593Smuzhiyun #ifdef CONFIG_SMP
1087*4882a593Smuzhiyun 
xive_cause_ipi(int cpu)1088*4882a593Smuzhiyun static void xive_cause_ipi(int cpu)
1089*4882a593Smuzhiyun {
1090*4882a593Smuzhiyun 	struct xive_cpu *xc;
1091*4882a593Smuzhiyun 	struct xive_irq_data *xd;
1092*4882a593Smuzhiyun 
1093*4882a593Smuzhiyun 	xc = per_cpu(xive_cpu, cpu);
1094*4882a593Smuzhiyun 
1095*4882a593Smuzhiyun 	DBG_VERBOSE("IPI CPU %d -> %d (HW IRQ 0x%x)\n",
1096*4882a593Smuzhiyun 		    smp_processor_id(), cpu, xc->hw_ipi);
1097*4882a593Smuzhiyun 
1098*4882a593Smuzhiyun 	xd = &xc->ipi_data;
1099*4882a593Smuzhiyun 	if (WARN_ON(!xd->trig_mmio))
1100*4882a593Smuzhiyun 		return;
1101*4882a593Smuzhiyun 	out_be64(xd->trig_mmio, 0);
1102*4882a593Smuzhiyun }
1103*4882a593Smuzhiyun 
xive_muxed_ipi_action(int irq,void * dev_id)1104*4882a593Smuzhiyun static irqreturn_t xive_muxed_ipi_action(int irq, void *dev_id)
1105*4882a593Smuzhiyun {
1106*4882a593Smuzhiyun 	return smp_ipi_demux();
1107*4882a593Smuzhiyun }
1108*4882a593Smuzhiyun 
xive_ipi_eoi(struct irq_data * d)1109*4882a593Smuzhiyun static void xive_ipi_eoi(struct irq_data *d)
1110*4882a593Smuzhiyun {
1111*4882a593Smuzhiyun 	struct xive_cpu *xc = __this_cpu_read(xive_cpu);
1112*4882a593Smuzhiyun 
1113*4882a593Smuzhiyun 	/* Handle possible race with unplug and drop stale IPIs */
1114*4882a593Smuzhiyun 	if (!xc)
1115*4882a593Smuzhiyun 		return;
1116*4882a593Smuzhiyun 
1117*4882a593Smuzhiyun 	DBG_VERBOSE("IPI eoi: irq=%d [0x%lx] (HW IRQ 0x%x) pending=%02x\n",
1118*4882a593Smuzhiyun 		    d->irq, irqd_to_hwirq(d), xc->hw_ipi, xc->pending_prio);
1119*4882a593Smuzhiyun 
1120*4882a593Smuzhiyun 	xive_do_source_eoi(xc->hw_ipi, &xc->ipi_data);
1121*4882a593Smuzhiyun 	xive_do_queue_eoi(xc);
1122*4882a593Smuzhiyun }
1123*4882a593Smuzhiyun 
xive_ipi_do_nothing(struct irq_data * d)1124*4882a593Smuzhiyun static void xive_ipi_do_nothing(struct irq_data *d)
1125*4882a593Smuzhiyun {
1126*4882a593Smuzhiyun 	/*
1127*4882a593Smuzhiyun 	 * Nothing to do, we never mask/unmask IPIs, but the callback
1128*4882a593Smuzhiyun 	 * has to exist for the struct irq_chip.
1129*4882a593Smuzhiyun 	 */
1130*4882a593Smuzhiyun }
1131*4882a593Smuzhiyun 
1132*4882a593Smuzhiyun static struct irq_chip xive_ipi_chip = {
1133*4882a593Smuzhiyun 	.name = "XIVE-IPI",
1134*4882a593Smuzhiyun 	.irq_eoi = xive_ipi_eoi,
1135*4882a593Smuzhiyun 	.irq_mask = xive_ipi_do_nothing,
1136*4882a593Smuzhiyun 	.irq_unmask = xive_ipi_do_nothing,
1137*4882a593Smuzhiyun };
1138*4882a593Smuzhiyun 
xive_request_ipi(void)1139*4882a593Smuzhiyun static void __init xive_request_ipi(void)
1140*4882a593Smuzhiyun {
1141*4882a593Smuzhiyun 	unsigned int virq;
1142*4882a593Smuzhiyun 
1143*4882a593Smuzhiyun 	/*
1144*4882a593Smuzhiyun 	 * Initialization failed, move on, we might manage to
1145*4882a593Smuzhiyun 	 * reach the point where we display our errors before
1146*4882a593Smuzhiyun 	 * the system falls appart
1147*4882a593Smuzhiyun 	 */
1148*4882a593Smuzhiyun 	if (!xive_irq_domain)
1149*4882a593Smuzhiyun 		return;
1150*4882a593Smuzhiyun 
1151*4882a593Smuzhiyun 	/* Initialize it */
1152*4882a593Smuzhiyun 	virq = irq_create_mapping(xive_irq_domain, 0);
1153*4882a593Smuzhiyun 	xive_ipi_irq = virq;
1154*4882a593Smuzhiyun 
1155*4882a593Smuzhiyun 	WARN_ON(request_irq(virq, xive_muxed_ipi_action,
1156*4882a593Smuzhiyun 			    IRQF_PERCPU | IRQF_NO_THREAD, "IPI", NULL));
1157*4882a593Smuzhiyun }
1158*4882a593Smuzhiyun 
xive_setup_cpu_ipi(unsigned int cpu)1159*4882a593Smuzhiyun static int xive_setup_cpu_ipi(unsigned int cpu)
1160*4882a593Smuzhiyun {
1161*4882a593Smuzhiyun 	struct xive_cpu *xc;
1162*4882a593Smuzhiyun 	int rc;
1163*4882a593Smuzhiyun 
1164*4882a593Smuzhiyun 	pr_debug("Setting up IPI for CPU %d\n", cpu);
1165*4882a593Smuzhiyun 
1166*4882a593Smuzhiyun 	xc = per_cpu(xive_cpu, cpu);
1167*4882a593Smuzhiyun 
1168*4882a593Smuzhiyun 	/* Check if we are already setup */
1169*4882a593Smuzhiyun 	if (xc->hw_ipi != XIVE_BAD_IRQ)
1170*4882a593Smuzhiyun 		return 0;
1171*4882a593Smuzhiyun 
1172*4882a593Smuzhiyun 	/* Grab an IPI from the backend, this will populate xc->hw_ipi */
1173*4882a593Smuzhiyun 	if (xive_ops->get_ipi(cpu, xc))
1174*4882a593Smuzhiyun 		return -EIO;
1175*4882a593Smuzhiyun 
1176*4882a593Smuzhiyun 	/*
1177*4882a593Smuzhiyun 	 * Populate the IRQ data in the xive_cpu structure and
1178*4882a593Smuzhiyun 	 * configure the HW / enable the IPIs.
1179*4882a593Smuzhiyun 	 */
1180*4882a593Smuzhiyun 	rc = xive_ops->populate_irq_data(xc->hw_ipi, &xc->ipi_data);
1181*4882a593Smuzhiyun 	if (rc) {
1182*4882a593Smuzhiyun 		pr_err("Failed to populate IPI data on CPU %d\n", cpu);
1183*4882a593Smuzhiyun 		return -EIO;
1184*4882a593Smuzhiyun 	}
1185*4882a593Smuzhiyun 	rc = xive_ops->configure_irq(xc->hw_ipi,
1186*4882a593Smuzhiyun 				     get_hard_smp_processor_id(cpu),
1187*4882a593Smuzhiyun 				     xive_irq_priority, xive_ipi_irq);
1188*4882a593Smuzhiyun 	if (rc) {
1189*4882a593Smuzhiyun 		pr_err("Failed to map IPI CPU %d\n", cpu);
1190*4882a593Smuzhiyun 		return -EIO;
1191*4882a593Smuzhiyun 	}
1192*4882a593Smuzhiyun 	pr_devel("CPU %d HW IPI %x, virq %d, trig_mmio=%p\n", cpu,
1193*4882a593Smuzhiyun 	    xc->hw_ipi, xive_ipi_irq, xc->ipi_data.trig_mmio);
1194*4882a593Smuzhiyun 
1195*4882a593Smuzhiyun 	/* Unmask it */
1196*4882a593Smuzhiyun 	xive_do_source_set_mask(&xc->ipi_data, false);
1197*4882a593Smuzhiyun 
1198*4882a593Smuzhiyun 	return 0;
1199*4882a593Smuzhiyun }
1200*4882a593Smuzhiyun 
xive_cleanup_cpu_ipi(unsigned int cpu,struct xive_cpu * xc)1201*4882a593Smuzhiyun static void xive_cleanup_cpu_ipi(unsigned int cpu, struct xive_cpu *xc)
1202*4882a593Smuzhiyun {
1203*4882a593Smuzhiyun 	/* Disable the IPI and free the IRQ data */
1204*4882a593Smuzhiyun 
1205*4882a593Smuzhiyun 	/* Already cleaned up ? */
1206*4882a593Smuzhiyun 	if (xc->hw_ipi == XIVE_BAD_IRQ)
1207*4882a593Smuzhiyun 		return;
1208*4882a593Smuzhiyun 
1209*4882a593Smuzhiyun 	/* Mask the IPI */
1210*4882a593Smuzhiyun 	xive_do_source_set_mask(&xc->ipi_data, true);
1211*4882a593Smuzhiyun 
1212*4882a593Smuzhiyun 	/*
1213*4882a593Smuzhiyun 	 * Note: We don't call xive_cleanup_irq_data() to free
1214*4882a593Smuzhiyun 	 * the mappings as this is called from an IPI on kexec
1215*4882a593Smuzhiyun 	 * which is not a safe environment to call iounmap()
1216*4882a593Smuzhiyun 	 */
1217*4882a593Smuzhiyun 
1218*4882a593Smuzhiyun 	/* Deconfigure/mask in the backend */
1219*4882a593Smuzhiyun 	xive_ops->configure_irq(xc->hw_ipi, hard_smp_processor_id(),
1220*4882a593Smuzhiyun 				0xff, xive_ipi_irq);
1221*4882a593Smuzhiyun 
1222*4882a593Smuzhiyun 	/* Free the IPIs in the backend */
1223*4882a593Smuzhiyun 	xive_ops->put_ipi(cpu, xc);
1224*4882a593Smuzhiyun }
1225*4882a593Smuzhiyun 
xive_smp_probe(void)1226*4882a593Smuzhiyun void __init xive_smp_probe(void)
1227*4882a593Smuzhiyun {
1228*4882a593Smuzhiyun 	smp_ops->cause_ipi = xive_cause_ipi;
1229*4882a593Smuzhiyun 
1230*4882a593Smuzhiyun 	/* Register the IPI */
1231*4882a593Smuzhiyun 	xive_request_ipi();
1232*4882a593Smuzhiyun 
1233*4882a593Smuzhiyun 	/* Allocate and setup IPI for the boot CPU */
1234*4882a593Smuzhiyun 	xive_setup_cpu_ipi(smp_processor_id());
1235*4882a593Smuzhiyun }
1236*4882a593Smuzhiyun 
1237*4882a593Smuzhiyun #endif /* CONFIG_SMP */
1238*4882a593Smuzhiyun 
xive_irq_domain_map(struct irq_domain * h,unsigned int virq,irq_hw_number_t hw)1239*4882a593Smuzhiyun static int xive_irq_domain_map(struct irq_domain *h, unsigned int virq,
1240*4882a593Smuzhiyun 			       irq_hw_number_t hw)
1241*4882a593Smuzhiyun {
1242*4882a593Smuzhiyun 	int rc;
1243*4882a593Smuzhiyun 
1244*4882a593Smuzhiyun 	/*
1245*4882a593Smuzhiyun 	 * Mark interrupts as edge sensitive by default so that resend
1246*4882a593Smuzhiyun 	 * actually works. Will fix that up below if needed.
1247*4882a593Smuzhiyun 	 */
1248*4882a593Smuzhiyun 	irq_clear_status_flags(virq, IRQ_LEVEL);
1249*4882a593Smuzhiyun 
1250*4882a593Smuzhiyun #ifdef CONFIG_SMP
1251*4882a593Smuzhiyun 	/* IPIs are special and come up with HW number 0 */
1252*4882a593Smuzhiyun 	if (hw == 0) {
1253*4882a593Smuzhiyun 		/*
1254*4882a593Smuzhiyun 		 * IPIs are marked per-cpu. We use separate HW interrupts under
1255*4882a593Smuzhiyun 		 * the hood but associated with the same "linux" interrupt
1256*4882a593Smuzhiyun 		 */
1257*4882a593Smuzhiyun 		irq_set_chip_and_handler(virq, &xive_ipi_chip,
1258*4882a593Smuzhiyun 					 handle_percpu_irq);
1259*4882a593Smuzhiyun 		return 0;
1260*4882a593Smuzhiyun 	}
1261*4882a593Smuzhiyun #endif
1262*4882a593Smuzhiyun 
1263*4882a593Smuzhiyun 	rc = xive_irq_alloc_data(virq, hw);
1264*4882a593Smuzhiyun 	if (rc)
1265*4882a593Smuzhiyun 		return rc;
1266*4882a593Smuzhiyun 
1267*4882a593Smuzhiyun 	irq_set_chip_and_handler(virq, &xive_irq_chip, handle_fasteoi_irq);
1268*4882a593Smuzhiyun 
1269*4882a593Smuzhiyun 	return 0;
1270*4882a593Smuzhiyun }
1271*4882a593Smuzhiyun 
xive_irq_domain_unmap(struct irq_domain * d,unsigned int virq)1272*4882a593Smuzhiyun static void xive_irq_domain_unmap(struct irq_domain *d, unsigned int virq)
1273*4882a593Smuzhiyun {
1274*4882a593Smuzhiyun 	struct irq_data *data = irq_get_irq_data(virq);
1275*4882a593Smuzhiyun 	unsigned int hw_irq;
1276*4882a593Smuzhiyun 
1277*4882a593Smuzhiyun 	/* XXX Assign BAD number */
1278*4882a593Smuzhiyun 	if (!data)
1279*4882a593Smuzhiyun 		return;
1280*4882a593Smuzhiyun 	hw_irq = (unsigned int)irqd_to_hwirq(data);
1281*4882a593Smuzhiyun 	if (hw_irq)
1282*4882a593Smuzhiyun 		xive_irq_free_data(virq);
1283*4882a593Smuzhiyun }
1284*4882a593Smuzhiyun 
xive_irq_domain_xlate(struct irq_domain * h,struct device_node * ct,const u32 * intspec,unsigned int intsize,irq_hw_number_t * out_hwirq,unsigned int * out_flags)1285*4882a593Smuzhiyun static int xive_irq_domain_xlate(struct irq_domain *h, struct device_node *ct,
1286*4882a593Smuzhiyun 				 const u32 *intspec, unsigned int intsize,
1287*4882a593Smuzhiyun 				 irq_hw_number_t *out_hwirq, unsigned int *out_flags)
1288*4882a593Smuzhiyun 
1289*4882a593Smuzhiyun {
1290*4882a593Smuzhiyun 	*out_hwirq = intspec[0];
1291*4882a593Smuzhiyun 
1292*4882a593Smuzhiyun 	/*
1293*4882a593Smuzhiyun 	 * If intsize is at least 2, we look for the type in the second cell,
1294*4882a593Smuzhiyun 	 * we assume the LSB indicates a level interrupt.
1295*4882a593Smuzhiyun 	 */
1296*4882a593Smuzhiyun 	if (intsize > 1) {
1297*4882a593Smuzhiyun 		if (intspec[1] & 1)
1298*4882a593Smuzhiyun 			*out_flags = IRQ_TYPE_LEVEL_LOW;
1299*4882a593Smuzhiyun 		else
1300*4882a593Smuzhiyun 			*out_flags = IRQ_TYPE_EDGE_RISING;
1301*4882a593Smuzhiyun 	} else
1302*4882a593Smuzhiyun 		*out_flags = IRQ_TYPE_LEVEL_LOW;
1303*4882a593Smuzhiyun 
1304*4882a593Smuzhiyun 	return 0;
1305*4882a593Smuzhiyun }
1306*4882a593Smuzhiyun 
xive_irq_domain_match(struct irq_domain * h,struct device_node * node,enum irq_domain_bus_token bus_token)1307*4882a593Smuzhiyun static int xive_irq_domain_match(struct irq_domain *h, struct device_node *node,
1308*4882a593Smuzhiyun 				 enum irq_domain_bus_token bus_token)
1309*4882a593Smuzhiyun {
1310*4882a593Smuzhiyun 	return xive_ops->match(node);
1311*4882a593Smuzhiyun }
1312*4882a593Smuzhiyun 
1313*4882a593Smuzhiyun static const struct irq_domain_ops xive_irq_domain_ops = {
1314*4882a593Smuzhiyun 	.match = xive_irq_domain_match,
1315*4882a593Smuzhiyun 	.map = xive_irq_domain_map,
1316*4882a593Smuzhiyun 	.unmap = xive_irq_domain_unmap,
1317*4882a593Smuzhiyun 	.xlate = xive_irq_domain_xlate,
1318*4882a593Smuzhiyun };
1319*4882a593Smuzhiyun 
xive_init_host(void)1320*4882a593Smuzhiyun static void __init xive_init_host(void)
1321*4882a593Smuzhiyun {
1322*4882a593Smuzhiyun 	xive_irq_domain = irq_domain_add_nomap(NULL, XIVE_MAX_IRQ,
1323*4882a593Smuzhiyun 					       &xive_irq_domain_ops, NULL);
1324*4882a593Smuzhiyun 	if (WARN_ON(xive_irq_domain == NULL))
1325*4882a593Smuzhiyun 		return;
1326*4882a593Smuzhiyun 	irq_set_default_host(xive_irq_domain);
1327*4882a593Smuzhiyun }
1328*4882a593Smuzhiyun 
xive_cleanup_cpu_queues(unsigned int cpu,struct xive_cpu * xc)1329*4882a593Smuzhiyun static void xive_cleanup_cpu_queues(unsigned int cpu, struct xive_cpu *xc)
1330*4882a593Smuzhiyun {
1331*4882a593Smuzhiyun 	if (xc->queue[xive_irq_priority].qpage)
1332*4882a593Smuzhiyun 		xive_ops->cleanup_queue(cpu, xc, xive_irq_priority);
1333*4882a593Smuzhiyun }
1334*4882a593Smuzhiyun 
xive_setup_cpu_queues(unsigned int cpu,struct xive_cpu * xc)1335*4882a593Smuzhiyun static int xive_setup_cpu_queues(unsigned int cpu, struct xive_cpu *xc)
1336*4882a593Smuzhiyun {
1337*4882a593Smuzhiyun 	int rc = 0;
1338*4882a593Smuzhiyun 
1339*4882a593Smuzhiyun 	/* We setup 1 queues for now with a 64k page */
1340*4882a593Smuzhiyun 	if (!xc->queue[xive_irq_priority].qpage)
1341*4882a593Smuzhiyun 		rc = xive_ops->setup_queue(cpu, xc, xive_irq_priority);
1342*4882a593Smuzhiyun 
1343*4882a593Smuzhiyun 	return rc;
1344*4882a593Smuzhiyun }
1345*4882a593Smuzhiyun 
xive_prepare_cpu(unsigned int cpu)1346*4882a593Smuzhiyun static int xive_prepare_cpu(unsigned int cpu)
1347*4882a593Smuzhiyun {
1348*4882a593Smuzhiyun 	struct xive_cpu *xc;
1349*4882a593Smuzhiyun 
1350*4882a593Smuzhiyun 	xc = per_cpu(xive_cpu, cpu);
1351*4882a593Smuzhiyun 	if (!xc) {
1352*4882a593Smuzhiyun 		struct device_node *np;
1353*4882a593Smuzhiyun 
1354*4882a593Smuzhiyun 		xc = kzalloc_node(sizeof(struct xive_cpu),
1355*4882a593Smuzhiyun 				  GFP_KERNEL, cpu_to_node(cpu));
1356*4882a593Smuzhiyun 		if (!xc)
1357*4882a593Smuzhiyun 			return -ENOMEM;
1358*4882a593Smuzhiyun 		np = of_get_cpu_node(cpu, NULL);
1359*4882a593Smuzhiyun 		if (np)
1360*4882a593Smuzhiyun 			xc->chip_id = of_get_ibm_chip_id(np);
1361*4882a593Smuzhiyun 		of_node_put(np);
1362*4882a593Smuzhiyun 		xc->hw_ipi = XIVE_BAD_IRQ;
1363*4882a593Smuzhiyun 
1364*4882a593Smuzhiyun 		per_cpu(xive_cpu, cpu) = xc;
1365*4882a593Smuzhiyun 	}
1366*4882a593Smuzhiyun 
1367*4882a593Smuzhiyun 	/* Setup EQs if not already */
1368*4882a593Smuzhiyun 	return xive_setup_cpu_queues(cpu, xc);
1369*4882a593Smuzhiyun }
1370*4882a593Smuzhiyun 
xive_setup_cpu(void)1371*4882a593Smuzhiyun static void xive_setup_cpu(void)
1372*4882a593Smuzhiyun {
1373*4882a593Smuzhiyun 	struct xive_cpu *xc = __this_cpu_read(xive_cpu);
1374*4882a593Smuzhiyun 
1375*4882a593Smuzhiyun 	/* The backend might have additional things to do */
1376*4882a593Smuzhiyun 	if (xive_ops->setup_cpu)
1377*4882a593Smuzhiyun 		xive_ops->setup_cpu(smp_processor_id(), xc);
1378*4882a593Smuzhiyun 
1379*4882a593Smuzhiyun 	/* Set CPPR to 0xff to enable flow of interrupts */
1380*4882a593Smuzhiyun 	xc->cppr = 0xff;
1381*4882a593Smuzhiyun 	out_8(xive_tima + xive_tima_offset + TM_CPPR, 0xff);
1382*4882a593Smuzhiyun }
1383*4882a593Smuzhiyun 
1384*4882a593Smuzhiyun #ifdef CONFIG_SMP
xive_smp_setup_cpu(void)1385*4882a593Smuzhiyun void xive_smp_setup_cpu(void)
1386*4882a593Smuzhiyun {
1387*4882a593Smuzhiyun 	pr_devel("SMP setup CPU %d\n", smp_processor_id());
1388*4882a593Smuzhiyun 
1389*4882a593Smuzhiyun 	/* This will have already been done on the boot CPU */
1390*4882a593Smuzhiyun 	if (smp_processor_id() != boot_cpuid)
1391*4882a593Smuzhiyun 		xive_setup_cpu();
1392*4882a593Smuzhiyun 
1393*4882a593Smuzhiyun }
1394*4882a593Smuzhiyun 
xive_smp_prepare_cpu(unsigned int cpu)1395*4882a593Smuzhiyun int xive_smp_prepare_cpu(unsigned int cpu)
1396*4882a593Smuzhiyun {
1397*4882a593Smuzhiyun 	int rc;
1398*4882a593Smuzhiyun 
1399*4882a593Smuzhiyun 	/* Allocate per-CPU data and queues */
1400*4882a593Smuzhiyun 	rc = xive_prepare_cpu(cpu);
1401*4882a593Smuzhiyun 	if (rc)
1402*4882a593Smuzhiyun 		return rc;
1403*4882a593Smuzhiyun 
1404*4882a593Smuzhiyun 	/* Allocate and setup IPI for the new CPU */
1405*4882a593Smuzhiyun 	return xive_setup_cpu_ipi(cpu);
1406*4882a593Smuzhiyun }
1407*4882a593Smuzhiyun 
1408*4882a593Smuzhiyun #ifdef CONFIG_HOTPLUG_CPU
xive_flush_cpu_queue(unsigned int cpu,struct xive_cpu * xc)1409*4882a593Smuzhiyun static void xive_flush_cpu_queue(unsigned int cpu, struct xive_cpu *xc)
1410*4882a593Smuzhiyun {
1411*4882a593Smuzhiyun 	u32 irq;
1412*4882a593Smuzhiyun 
1413*4882a593Smuzhiyun 	/* We assume local irqs are disabled */
1414*4882a593Smuzhiyun 	WARN_ON(!irqs_disabled());
1415*4882a593Smuzhiyun 
1416*4882a593Smuzhiyun 	/* Check what's already in the CPU queue */
1417*4882a593Smuzhiyun 	while ((irq = xive_scan_interrupts(xc, false)) != 0) {
1418*4882a593Smuzhiyun 		/*
1419*4882a593Smuzhiyun 		 * We need to re-route that interrupt to its new destination.
1420*4882a593Smuzhiyun 		 * First get and lock the descriptor
1421*4882a593Smuzhiyun 		 */
1422*4882a593Smuzhiyun 		struct irq_desc *desc = irq_to_desc(irq);
1423*4882a593Smuzhiyun 		struct irq_data *d = irq_desc_get_irq_data(desc);
1424*4882a593Smuzhiyun 		struct xive_irq_data *xd;
1425*4882a593Smuzhiyun 		unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
1426*4882a593Smuzhiyun 
1427*4882a593Smuzhiyun 		/*
1428*4882a593Smuzhiyun 		 * Ignore anything that isn't a XIVE irq and ignore
1429*4882a593Smuzhiyun 		 * IPIs, so can just be dropped.
1430*4882a593Smuzhiyun 		 */
1431*4882a593Smuzhiyun 		if (d->domain != xive_irq_domain || hw_irq == 0)
1432*4882a593Smuzhiyun 			continue;
1433*4882a593Smuzhiyun 
1434*4882a593Smuzhiyun 		/*
1435*4882a593Smuzhiyun 		 * The IRQ should have already been re-routed, it's just a
1436*4882a593Smuzhiyun 		 * stale in the old queue, so re-trigger it in order to make
1437*4882a593Smuzhiyun 		 * it reach is new destination.
1438*4882a593Smuzhiyun 		 */
1439*4882a593Smuzhiyun #ifdef DEBUG_FLUSH
1440*4882a593Smuzhiyun 		pr_info("CPU %d: Got irq %d while offline, re-sending...\n",
1441*4882a593Smuzhiyun 			cpu, irq);
1442*4882a593Smuzhiyun #endif
1443*4882a593Smuzhiyun 		raw_spin_lock(&desc->lock);
1444*4882a593Smuzhiyun 		xd = irq_desc_get_handler_data(desc);
1445*4882a593Smuzhiyun 
1446*4882a593Smuzhiyun 		/*
1447*4882a593Smuzhiyun 		 * Clear saved_p to indicate that it's no longer pending
1448*4882a593Smuzhiyun 		 */
1449*4882a593Smuzhiyun 		xd->saved_p = false;
1450*4882a593Smuzhiyun 
1451*4882a593Smuzhiyun 		/*
1452*4882a593Smuzhiyun 		 * For LSIs, we EOI, this will cause a resend if it's
1453*4882a593Smuzhiyun 		 * still asserted. Otherwise do an MSI retrigger.
1454*4882a593Smuzhiyun 		 */
1455*4882a593Smuzhiyun 		if (xd->flags & XIVE_IRQ_FLAG_LSI)
1456*4882a593Smuzhiyun 			xive_do_source_eoi(irqd_to_hwirq(d), xd);
1457*4882a593Smuzhiyun 		else
1458*4882a593Smuzhiyun 			xive_irq_retrigger(d);
1459*4882a593Smuzhiyun 
1460*4882a593Smuzhiyun 		raw_spin_unlock(&desc->lock);
1461*4882a593Smuzhiyun 	}
1462*4882a593Smuzhiyun }
1463*4882a593Smuzhiyun 
xive_smp_disable_cpu(void)1464*4882a593Smuzhiyun void xive_smp_disable_cpu(void)
1465*4882a593Smuzhiyun {
1466*4882a593Smuzhiyun 	struct xive_cpu *xc = __this_cpu_read(xive_cpu);
1467*4882a593Smuzhiyun 	unsigned int cpu = smp_processor_id();
1468*4882a593Smuzhiyun 
1469*4882a593Smuzhiyun 	/* Migrate interrupts away from the CPU */
1470*4882a593Smuzhiyun 	irq_migrate_all_off_this_cpu();
1471*4882a593Smuzhiyun 
1472*4882a593Smuzhiyun 	/* Set CPPR to 0 to disable flow of interrupts */
1473*4882a593Smuzhiyun 	xc->cppr = 0;
1474*4882a593Smuzhiyun 	out_8(xive_tima + xive_tima_offset + TM_CPPR, 0);
1475*4882a593Smuzhiyun 
1476*4882a593Smuzhiyun 	/* Flush everything still in the queue */
1477*4882a593Smuzhiyun 	xive_flush_cpu_queue(cpu, xc);
1478*4882a593Smuzhiyun 
1479*4882a593Smuzhiyun 	/* Re-enable CPPR  */
1480*4882a593Smuzhiyun 	xc->cppr = 0xff;
1481*4882a593Smuzhiyun 	out_8(xive_tima + xive_tima_offset + TM_CPPR, 0xff);
1482*4882a593Smuzhiyun }
1483*4882a593Smuzhiyun 
xive_flush_interrupt(void)1484*4882a593Smuzhiyun void xive_flush_interrupt(void)
1485*4882a593Smuzhiyun {
1486*4882a593Smuzhiyun 	struct xive_cpu *xc = __this_cpu_read(xive_cpu);
1487*4882a593Smuzhiyun 	unsigned int cpu = smp_processor_id();
1488*4882a593Smuzhiyun 
1489*4882a593Smuzhiyun 	/* Called if an interrupt occurs while the CPU is hot unplugged */
1490*4882a593Smuzhiyun 	xive_flush_cpu_queue(cpu, xc);
1491*4882a593Smuzhiyun }
1492*4882a593Smuzhiyun 
1493*4882a593Smuzhiyun #endif /* CONFIG_HOTPLUG_CPU */
1494*4882a593Smuzhiyun 
1495*4882a593Smuzhiyun #endif /* CONFIG_SMP */
1496*4882a593Smuzhiyun 
xive_teardown_cpu(void)1497*4882a593Smuzhiyun void xive_teardown_cpu(void)
1498*4882a593Smuzhiyun {
1499*4882a593Smuzhiyun 	struct xive_cpu *xc = __this_cpu_read(xive_cpu);
1500*4882a593Smuzhiyun 	unsigned int cpu = smp_processor_id();
1501*4882a593Smuzhiyun 
1502*4882a593Smuzhiyun 	/* Set CPPR to 0 to disable flow of interrupts */
1503*4882a593Smuzhiyun 	xc->cppr = 0;
1504*4882a593Smuzhiyun 	out_8(xive_tima + xive_tima_offset + TM_CPPR, 0);
1505*4882a593Smuzhiyun 
1506*4882a593Smuzhiyun 	if (xive_ops->teardown_cpu)
1507*4882a593Smuzhiyun 		xive_ops->teardown_cpu(cpu, xc);
1508*4882a593Smuzhiyun 
1509*4882a593Smuzhiyun #ifdef CONFIG_SMP
1510*4882a593Smuzhiyun 	/* Get rid of IPI */
1511*4882a593Smuzhiyun 	xive_cleanup_cpu_ipi(cpu, xc);
1512*4882a593Smuzhiyun #endif
1513*4882a593Smuzhiyun 
1514*4882a593Smuzhiyun 	/* Disable and free the queues */
1515*4882a593Smuzhiyun 	xive_cleanup_cpu_queues(cpu, xc);
1516*4882a593Smuzhiyun }
1517*4882a593Smuzhiyun 
xive_shutdown(void)1518*4882a593Smuzhiyun void xive_shutdown(void)
1519*4882a593Smuzhiyun {
1520*4882a593Smuzhiyun 	xive_ops->shutdown();
1521*4882a593Smuzhiyun }
1522*4882a593Smuzhiyun 
xive_core_init(const struct xive_ops * ops,void __iomem * area,u32 offset,u8 max_prio)1523*4882a593Smuzhiyun bool __init xive_core_init(const struct xive_ops *ops, void __iomem *area, u32 offset,
1524*4882a593Smuzhiyun 			   u8 max_prio)
1525*4882a593Smuzhiyun {
1526*4882a593Smuzhiyun 	xive_tima = area;
1527*4882a593Smuzhiyun 	xive_tima_offset = offset;
1528*4882a593Smuzhiyun 	xive_ops = ops;
1529*4882a593Smuzhiyun 	xive_irq_priority = max_prio;
1530*4882a593Smuzhiyun 
1531*4882a593Smuzhiyun 	ppc_md.get_irq = xive_get_irq;
1532*4882a593Smuzhiyun 	__xive_enabled = true;
1533*4882a593Smuzhiyun 
1534*4882a593Smuzhiyun 	pr_devel("Initializing host..\n");
1535*4882a593Smuzhiyun 	xive_init_host();
1536*4882a593Smuzhiyun 
1537*4882a593Smuzhiyun 	pr_devel("Initializing boot CPU..\n");
1538*4882a593Smuzhiyun 
1539*4882a593Smuzhiyun 	/* Allocate per-CPU data and queues */
1540*4882a593Smuzhiyun 	xive_prepare_cpu(smp_processor_id());
1541*4882a593Smuzhiyun 
1542*4882a593Smuzhiyun 	/* Get ready for interrupts */
1543*4882a593Smuzhiyun 	xive_setup_cpu();
1544*4882a593Smuzhiyun 
1545*4882a593Smuzhiyun 	pr_info("Interrupt handling initialized with %s backend\n",
1546*4882a593Smuzhiyun 		xive_ops->name);
1547*4882a593Smuzhiyun 	pr_info("Using priority %d for all interrupts\n", max_prio);
1548*4882a593Smuzhiyun 
1549*4882a593Smuzhiyun 	return true;
1550*4882a593Smuzhiyun }
1551*4882a593Smuzhiyun 
xive_queue_page_alloc(unsigned int cpu,u32 queue_shift)1552*4882a593Smuzhiyun __be32 *xive_queue_page_alloc(unsigned int cpu, u32 queue_shift)
1553*4882a593Smuzhiyun {
1554*4882a593Smuzhiyun 	unsigned int alloc_order;
1555*4882a593Smuzhiyun 	struct page *pages;
1556*4882a593Smuzhiyun 	__be32 *qpage;
1557*4882a593Smuzhiyun 
1558*4882a593Smuzhiyun 	alloc_order = xive_alloc_order(queue_shift);
1559*4882a593Smuzhiyun 	pages = alloc_pages_node(cpu_to_node(cpu), GFP_KERNEL, alloc_order);
1560*4882a593Smuzhiyun 	if (!pages)
1561*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
1562*4882a593Smuzhiyun 	qpage = (__be32 *)page_address(pages);
1563*4882a593Smuzhiyun 	memset(qpage, 0, 1 << queue_shift);
1564*4882a593Smuzhiyun 
1565*4882a593Smuzhiyun 	return qpage;
1566*4882a593Smuzhiyun }
1567*4882a593Smuzhiyun 
xive_off(char * arg)1568*4882a593Smuzhiyun static int __init xive_off(char *arg)
1569*4882a593Smuzhiyun {
1570*4882a593Smuzhiyun 	xive_cmdline_disabled = true;
1571*4882a593Smuzhiyun 	return 0;
1572*4882a593Smuzhiyun }
1573*4882a593Smuzhiyun __setup("xive=off", xive_off);
1574*4882a593Smuzhiyun 
xive_debug_show_cpu(struct seq_file * m,int cpu)1575*4882a593Smuzhiyun static void xive_debug_show_cpu(struct seq_file *m, int cpu)
1576*4882a593Smuzhiyun {
1577*4882a593Smuzhiyun 	struct xive_cpu *xc = per_cpu(xive_cpu, cpu);
1578*4882a593Smuzhiyun 
1579*4882a593Smuzhiyun 	seq_printf(m, "CPU %d:", cpu);
1580*4882a593Smuzhiyun 	if (xc) {
1581*4882a593Smuzhiyun 		seq_printf(m, "pp=%02x CPPR=%02x ", xc->pending_prio, xc->cppr);
1582*4882a593Smuzhiyun 
1583*4882a593Smuzhiyun #ifdef CONFIG_SMP
1584*4882a593Smuzhiyun 		{
1585*4882a593Smuzhiyun 			u64 val = xive_esb_read(&xc->ipi_data, XIVE_ESB_GET);
1586*4882a593Smuzhiyun 
1587*4882a593Smuzhiyun 			seq_printf(m, "IPI=0x%08x PQ=%c%c ", xc->hw_ipi,
1588*4882a593Smuzhiyun 				   val & XIVE_ESB_VAL_P ? 'P' : '-',
1589*4882a593Smuzhiyun 				   val & XIVE_ESB_VAL_Q ? 'Q' : '-');
1590*4882a593Smuzhiyun 		}
1591*4882a593Smuzhiyun #endif
1592*4882a593Smuzhiyun 		{
1593*4882a593Smuzhiyun 			struct xive_q *q = &xc->queue[xive_irq_priority];
1594*4882a593Smuzhiyun 			u32 i0, i1, idx;
1595*4882a593Smuzhiyun 
1596*4882a593Smuzhiyun 			if (q->qpage) {
1597*4882a593Smuzhiyun 				idx = q->idx;
1598*4882a593Smuzhiyun 				i0 = be32_to_cpup(q->qpage + idx);
1599*4882a593Smuzhiyun 				idx = (idx + 1) & q->msk;
1600*4882a593Smuzhiyun 				i1 = be32_to_cpup(q->qpage + idx);
1601*4882a593Smuzhiyun 				seq_printf(m, "EQ idx=%d T=%d %08x %08x ...",
1602*4882a593Smuzhiyun 					   q->idx, q->toggle, i0, i1);
1603*4882a593Smuzhiyun 			}
1604*4882a593Smuzhiyun 		}
1605*4882a593Smuzhiyun 	}
1606*4882a593Smuzhiyun 	seq_puts(m, "\n");
1607*4882a593Smuzhiyun }
1608*4882a593Smuzhiyun 
xive_debug_show_irq(struct seq_file * m,u32 hw_irq,struct irq_data * d)1609*4882a593Smuzhiyun static void xive_debug_show_irq(struct seq_file *m, u32 hw_irq, struct irq_data *d)
1610*4882a593Smuzhiyun {
1611*4882a593Smuzhiyun 	struct irq_chip *chip = irq_data_get_irq_chip(d);
1612*4882a593Smuzhiyun 	int rc;
1613*4882a593Smuzhiyun 	u32 target;
1614*4882a593Smuzhiyun 	u8 prio;
1615*4882a593Smuzhiyun 	u32 lirq;
1616*4882a593Smuzhiyun 	struct xive_irq_data *xd;
1617*4882a593Smuzhiyun 	u64 val;
1618*4882a593Smuzhiyun 
1619*4882a593Smuzhiyun 	if (!is_xive_irq(chip))
1620*4882a593Smuzhiyun 		return;
1621*4882a593Smuzhiyun 
1622*4882a593Smuzhiyun 	rc = xive_ops->get_irq_config(hw_irq, &target, &prio, &lirq);
1623*4882a593Smuzhiyun 	if (rc) {
1624*4882a593Smuzhiyun 		seq_printf(m, "IRQ 0x%08x : no config rc=%d\n", hw_irq, rc);
1625*4882a593Smuzhiyun 		return;
1626*4882a593Smuzhiyun 	}
1627*4882a593Smuzhiyun 
1628*4882a593Smuzhiyun 	seq_printf(m, "IRQ 0x%08x : target=0x%x prio=%02x lirq=0x%x ",
1629*4882a593Smuzhiyun 		   hw_irq, target, prio, lirq);
1630*4882a593Smuzhiyun 
1631*4882a593Smuzhiyun 	xd = irq_data_get_irq_handler_data(d);
1632*4882a593Smuzhiyun 	val = xive_esb_read(xd, XIVE_ESB_GET);
1633*4882a593Smuzhiyun 	seq_printf(m, "flags=%c%c%c PQ=%c%c",
1634*4882a593Smuzhiyun 		   xd->flags & XIVE_IRQ_FLAG_STORE_EOI ? 'S' : ' ',
1635*4882a593Smuzhiyun 		   xd->flags & XIVE_IRQ_FLAG_LSI ? 'L' : ' ',
1636*4882a593Smuzhiyun 		   xd->flags & XIVE_IRQ_FLAG_H_INT_ESB ? 'H' : ' ',
1637*4882a593Smuzhiyun 		   val & XIVE_ESB_VAL_P ? 'P' : '-',
1638*4882a593Smuzhiyun 		   val & XIVE_ESB_VAL_Q ? 'Q' : '-');
1639*4882a593Smuzhiyun 	seq_puts(m, "\n");
1640*4882a593Smuzhiyun }
1641*4882a593Smuzhiyun 
xive_core_debug_show(struct seq_file * m,void * private)1642*4882a593Smuzhiyun static int xive_core_debug_show(struct seq_file *m, void *private)
1643*4882a593Smuzhiyun {
1644*4882a593Smuzhiyun 	unsigned int i;
1645*4882a593Smuzhiyun 	struct irq_desc *desc;
1646*4882a593Smuzhiyun 	int cpu;
1647*4882a593Smuzhiyun 
1648*4882a593Smuzhiyun 	if (xive_ops->debug_show)
1649*4882a593Smuzhiyun 		xive_ops->debug_show(m, private);
1650*4882a593Smuzhiyun 
1651*4882a593Smuzhiyun 	for_each_possible_cpu(cpu)
1652*4882a593Smuzhiyun 		xive_debug_show_cpu(m, cpu);
1653*4882a593Smuzhiyun 
1654*4882a593Smuzhiyun 	for_each_irq_desc(i, desc) {
1655*4882a593Smuzhiyun 		struct irq_data *d = irq_desc_get_irq_data(desc);
1656*4882a593Smuzhiyun 		unsigned int hw_irq;
1657*4882a593Smuzhiyun 
1658*4882a593Smuzhiyun 		if (!d)
1659*4882a593Smuzhiyun 			continue;
1660*4882a593Smuzhiyun 
1661*4882a593Smuzhiyun 		hw_irq = (unsigned int)irqd_to_hwirq(d);
1662*4882a593Smuzhiyun 
1663*4882a593Smuzhiyun 		/* IPIs are special (HW number 0) */
1664*4882a593Smuzhiyun 		if (hw_irq)
1665*4882a593Smuzhiyun 			xive_debug_show_irq(m, hw_irq, d);
1666*4882a593Smuzhiyun 	}
1667*4882a593Smuzhiyun 	return 0;
1668*4882a593Smuzhiyun }
1669*4882a593Smuzhiyun DEFINE_SHOW_ATTRIBUTE(xive_core_debug);
1670*4882a593Smuzhiyun 
xive_core_debug_init(void)1671*4882a593Smuzhiyun int xive_core_debug_init(void)
1672*4882a593Smuzhiyun {
1673*4882a593Smuzhiyun 	if (xive_enabled())
1674*4882a593Smuzhiyun 		debugfs_create_file("xive", 0400, powerpc_debugfs_root,
1675*4882a593Smuzhiyun 				    NULL, &xive_core_debug_fops);
1676*4882a593Smuzhiyun 	return 0;
1677*4882a593Smuzhiyun }
1678