xref: /OK3568_Linux_fs/kernel/arch/powerpc/kvm/book3s_xive_template.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright 2017 Benjamin Herrenschmidt, IBM Corporation
4*4882a593Smuzhiyun  */
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun /* File to be included by other .c files */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #define XGLUE(a,b) a##b
9*4882a593Smuzhiyun #define GLUE(a,b) XGLUE(a,b)
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun /* Dummy interrupt used when taking interrupts out of a queue in H_CPPR */
12*4882a593Smuzhiyun #define XICS_DUMMY	1
13*4882a593Smuzhiyun 
GLUE(X_PFX,ack_pending)14*4882a593Smuzhiyun static void GLUE(X_PFX,ack_pending)(struct kvmppc_xive_vcpu *xc)
15*4882a593Smuzhiyun {
16*4882a593Smuzhiyun 	u8 cppr;
17*4882a593Smuzhiyun 	u16 ack;
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun 	/*
20*4882a593Smuzhiyun 	 * Ensure any previous store to CPPR is ordered vs.
21*4882a593Smuzhiyun 	 * the subsequent loads from PIPR or ACK.
22*4882a593Smuzhiyun 	 */
23*4882a593Smuzhiyun 	eieio();
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun 	/* Perform the acknowledge OS to register cycle. */
26*4882a593Smuzhiyun 	ack = be16_to_cpu(__x_readw(__x_tima + TM_SPC_ACK_OS_REG));
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun 	/* Synchronize subsequent queue accesses */
29*4882a593Smuzhiyun 	mb();
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun 	/* XXX Check grouping level */
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun 	/* Anything ? */
34*4882a593Smuzhiyun 	if (!((ack >> 8) & TM_QW1_NSR_EO))
35*4882a593Smuzhiyun 		return;
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun 	/* Grab CPPR of the most favored pending interrupt */
38*4882a593Smuzhiyun 	cppr = ack & 0xff;
39*4882a593Smuzhiyun 	if (cppr < 8)
40*4882a593Smuzhiyun 		xc->pending |= 1 << cppr;
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun #ifdef XIVE_RUNTIME_CHECKS
43*4882a593Smuzhiyun 	/* Check consistency */
44*4882a593Smuzhiyun 	if (cppr >= xc->hw_cppr)
45*4882a593Smuzhiyun 		pr_warn("KVM-XIVE: CPU %d odd ack CPPR, got %d at %d\n",
46*4882a593Smuzhiyun 			smp_processor_id(), cppr, xc->hw_cppr);
47*4882a593Smuzhiyun #endif
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	/*
50*4882a593Smuzhiyun 	 * Update our image of the HW CPPR. We don't yet modify
51*4882a593Smuzhiyun 	 * xc->cppr, this will be done as we scan for interrupts
52*4882a593Smuzhiyun 	 * in the queues.
53*4882a593Smuzhiyun 	 */
54*4882a593Smuzhiyun 	xc->hw_cppr = cppr;
55*4882a593Smuzhiyun }
56*4882a593Smuzhiyun 
GLUE(X_PFX,esb_load)57*4882a593Smuzhiyun static u8 GLUE(X_PFX,esb_load)(struct xive_irq_data *xd, u32 offset)
58*4882a593Smuzhiyun {
59*4882a593Smuzhiyun 	u64 val;
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 	if (offset == XIVE_ESB_SET_PQ_10 && xd->flags & XIVE_IRQ_FLAG_STORE_EOI)
62*4882a593Smuzhiyun 		offset |= XIVE_ESB_LD_ST_MO;
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 	if (xd->flags & XIVE_IRQ_FLAG_SHIFT_BUG)
65*4882a593Smuzhiyun 		offset |= offset << 4;
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	val =__x_readq(__x_eoi_page(xd) + offset);
68*4882a593Smuzhiyun #ifdef __LITTLE_ENDIAN__
69*4882a593Smuzhiyun 	val >>= 64-8;
70*4882a593Smuzhiyun #endif
71*4882a593Smuzhiyun 	return (u8)val;
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 
GLUE(X_PFX,source_eoi)75*4882a593Smuzhiyun static void GLUE(X_PFX,source_eoi)(u32 hw_irq, struct xive_irq_data *xd)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun 	/* If the XIVE supports the new "store EOI facility, use it */
78*4882a593Smuzhiyun 	if (xd->flags & XIVE_IRQ_FLAG_STORE_EOI)
79*4882a593Smuzhiyun 		__x_writeq(0, __x_eoi_page(xd) + XIVE_ESB_STORE_EOI);
80*4882a593Smuzhiyun 	else if (hw_irq && xd->flags & XIVE_IRQ_FLAG_EOI_FW)
81*4882a593Smuzhiyun 		opal_int_eoi(hw_irq);
82*4882a593Smuzhiyun 	else if (xd->flags & XIVE_IRQ_FLAG_LSI) {
83*4882a593Smuzhiyun 		/*
84*4882a593Smuzhiyun 		 * For LSIs the HW EOI cycle is used rather than PQ bits,
85*4882a593Smuzhiyun 		 * as they are automatically re-triggred in HW when still
86*4882a593Smuzhiyun 		 * pending.
87*4882a593Smuzhiyun 		 */
88*4882a593Smuzhiyun 		__x_readq(__x_eoi_page(xd) + XIVE_ESB_LOAD_EOI);
89*4882a593Smuzhiyun 	} else {
90*4882a593Smuzhiyun 		uint64_t eoi_val;
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 		/*
93*4882a593Smuzhiyun 		 * Otherwise for EOI, we use the special MMIO that does
94*4882a593Smuzhiyun 		 * a clear of both P and Q and returns the old Q,
95*4882a593Smuzhiyun 		 * except for LSIs where we use the "EOI cycle" special
96*4882a593Smuzhiyun 		 * load.
97*4882a593Smuzhiyun 		 *
98*4882a593Smuzhiyun 		 * This allows us to then do a re-trigger if Q was set
99*4882a593Smuzhiyun 		 * rather than synthetizing an interrupt in software
100*4882a593Smuzhiyun 		 */
101*4882a593Smuzhiyun 		eoi_val = GLUE(X_PFX,esb_load)(xd, XIVE_ESB_SET_PQ_00);
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 		/* Re-trigger if needed */
104*4882a593Smuzhiyun 		if ((eoi_val & 1) && __x_trig_page(xd))
105*4882a593Smuzhiyun 			__x_writeq(0, __x_trig_page(xd));
106*4882a593Smuzhiyun 	}
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun enum {
110*4882a593Smuzhiyun 	scan_fetch,
111*4882a593Smuzhiyun 	scan_poll,
112*4882a593Smuzhiyun 	scan_eoi,
113*4882a593Smuzhiyun };
114*4882a593Smuzhiyun 
GLUE(X_PFX,scan_interrupts)115*4882a593Smuzhiyun static u32 GLUE(X_PFX,scan_interrupts)(struct kvmppc_xive_vcpu *xc,
116*4882a593Smuzhiyun 				       u8 pending, int scan_type)
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun 	u32 hirq = 0;
119*4882a593Smuzhiyun 	u8 prio = 0xff;
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	/* Find highest pending priority */
122*4882a593Smuzhiyun 	while ((xc->mfrr != 0xff || pending != 0) && hirq == 0) {
123*4882a593Smuzhiyun 		struct xive_q *q;
124*4882a593Smuzhiyun 		u32 idx, toggle;
125*4882a593Smuzhiyun 		__be32 *qpage;
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 		/*
128*4882a593Smuzhiyun 		 * If pending is 0 this will return 0xff which is what
129*4882a593Smuzhiyun 		 * we want
130*4882a593Smuzhiyun 		 */
131*4882a593Smuzhiyun 		prio = ffs(pending) - 1;
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun 		/* Don't scan past the guest cppr */
134*4882a593Smuzhiyun 		if (prio >= xc->cppr || prio > 7) {
135*4882a593Smuzhiyun 			if (xc->mfrr < xc->cppr) {
136*4882a593Smuzhiyun 				prio = xc->mfrr;
137*4882a593Smuzhiyun 				hirq = XICS_IPI;
138*4882a593Smuzhiyun 			}
139*4882a593Smuzhiyun 			break;
140*4882a593Smuzhiyun 		}
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 		/* Grab queue and pointers */
143*4882a593Smuzhiyun 		q = &xc->queues[prio];
144*4882a593Smuzhiyun 		idx = q->idx;
145*4882a593Smuzhiyun 		toggle = q->toggle;
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 		/*
148*4882a593Smuzhiyun 		 * Snapshot the queue page. The test further down for EOI
149*4882a593Smuzhiyun 		 * must use the same "copy" that was used by __xive_read_eq
150*4882a593Smuzhiyun 		 * since qpage can be set concurrently and we don't want
151*4882a593Smuzhiyun 		 * to miss an EOI.
152*4882a593Smuzhiyun 		 */
153*4882a593Smuzhiyun 		qpage = READ_ONCE(q->qpage);
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun skip_ipi:
156*4882a593Smuzhiyun 		/*
157*4882a593Smuzhiyun 		 * Try to fetch from the queue. Will return 0 for a
158*4882a593Smuzhiyun 		 * non-queueing priority (ie, qpage = 0).
159*4882a593Smuzhiyun 		 */
160*4882a593Smuzhiyun 		hirq = __xive_read_eq(qpage, q->msk, &idx, &toggle);
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 		/*
163*4882a593Smuzhiyun 		 * If this was a signal for an MFFR change done by
164*4882a593Smuzhiyun 		 * H_IPI we skip it. Additionally, if we were fetching
165*4882a593Smuzhiyun 		 * we EOI it now, thus re-enabling reception of a new
166*4882a593Smuzhiyun 		 * such signal.
167*4882a593Smuzhiyun 		 *
168*4882a593Smuzhiyun 		 * We also need to do that if prio is 0 and we had no
169*4882a593Smuzhiyun 		 * page for the queue. In this case, we have non-queued
170*4882a593Smuzhiyun 		 * IPI that needs to be EOId.
171*4882a593Smuzhiyun 		 *
172*4882a593Smuzhiyun 		 * This is safe because if we have another pending MFRR
173*4882a593Smuzhiyun 		 * change that wasn't observed above, the Q bit will have
174*4882a593Smuzhiyun 		 * been set and another occurrence of the IPI will trigger.
175*4882a593Smuzhiyun 		 */
176*4882a593Smuzhiyun 		if (hirq == XICS_IPI || (prio == 0 && !qpage)) {
177*4882a593Smuzhiyun 			if (scan_type == scan_fetch) {
178*4882a593Smuzhiyun 				GLUE(X_PFX,source_eoi)(xc->vp_ipi,
179*4882a593Smuzhiyun 						       &xc->vp_ipi_data);
180*4882a593Smuzhiyun 				q->idx = idx;
181*4882a593Smuzhiyun 				q->toggle = toggle;
182*4882a593Smuzhiyun 			}
183*4882a593Smuzhiyun 			/* Loop back on same queue with updated idx/toggle */
184*4882a593Smuzhiyun #ifdef XIVE_RUNTIME_CHECKS
185*4882a593Smuzhiyun 			WARN_ON(hirq && hirq != XICS_IPI);
186*4882a593Smuzhiyun #endif
187*4882a593Smuzhiyun 			if (hirq)
188*4882a593Smuzhiyun 				goto skip_ipi;
189*4882a593Smuzhiyun 		}
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 		/* If it's the dummy interrupt, continue searching */
192*4882a593Smuzhiyun 		if (hirq == XICS_DUMMY)
193*4882a593Smuzhiyun 			goto skip_ipi;
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 		/* Clear the pending bit if the queue is now empty */
196*4882a593Smuzhiyun 		if (!hirq) {
197*4882a593Smuzhiyun 			pending &= ~(1 << prio);
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 			/*
200*4882a593Smuzhiyun 			 * Check if the queue count needs adjusting due to
201*4882a593Smuzhiyun 			 * interrupts being moved away.
202*4882a593Smuzhiyun 			 */
203*4882a593Smuzhiyun 			if (atomic_read(&q->pending_count)) {
204*4882a593Smuzhiyun 				int p = atomic_xchg(&q->pending_count, 0);
205*4882a593Smuzhiyun 				if (p) {
206*4882a593Smuzhiyun #ifdef XIVE_RUNTIME_CHECKS
207*4882a593Smuzhiyun 					WARN_ON(p > atomic_read(&q->count));
208*4882a593Smuzhiyun #endif
209*4882a593Smuzhiyun 					atomic_sub(p, &q->count);
210*4882a593Smuzhiyun 				}
211*4882a593Smuzhiyun 			}
212*4882a593Smuzhiyun 		}
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 		/*
215*4882a593Smuzhiyun 		 * If the most favoured prio we found pending is less
216*4882a593Smuzhiyun 		 * favored (or equal) than a pending IPI, we return
217*4882a593Smuzhiyun 		 * the IPI instead.
218*4882a593Smuzhiyun 		 */
219*4882a593Smuzhiyun 		if (prio >= xc->mfrr && xc->mfrr < xc->cppr) {
220*4882a593Smuzhiyun 			prio = xc->mfrr;
221*4882a593Smuzhiyun 			hirq = XICS_IPI;
222*4882a593Smuzhiyun 			break;
223*4882a593Smuzhiyun 		}
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 		/* If fetching, update queue pointers */
226*4882a593Smuzhiyun 		if (scan_type == scan_fetch) {
227*4882a593Smuzhiyun 			q->idx = idx;
228*4882a593Smuzhiyun 			q->toggle = toggle;
229*4882a593Smuzhiyun 		}
230*4882a593Smuzhiyun 	}
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun 	/* If we are just taking a "peek", do nothing else */
233*4882a593Smuzhiyun 	if (scan_type == scan_poll)
234*4882a593Smuzhiyun 		return hirq;
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 	/* Update the pending bits */
237*4882a593Smuzhiyun 	xc->pending = pending;
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	/*
240*4882a593Smuzhiyun 	 * If this is an EOI that's it, no CPPR adjustment done here,
241*4882a593Smuzhiyun 	 * all we needed was cleanup the stale pending bits and check
242*4882a593Smuzhiyun 	 * if there's anything left.
243*4882a593Smuzhiyun 	 */
244*4882a593Smuzhiyun 	if (scan_type == scan_eoi)
245*4882a593Smuzhiyun 		return hirq;
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 	/*
248*4882a593Smuzhiyun 	 * If we found an interrupt, adjust what the guest CPPR should
249*4882a593Smuzhiyun 	 * be as if we had just fetched that interrupt from HW.
250*4882a593Smuzhiyun 	 *
251*4882a593Smuzhiyun 	 * Note: This can only make xc->cppr smaller as the previous
252*4882a593Smuzhiyun 	 * loop will only exit with hirq != 0 if prio is lower than
253*4882a593Smuzhiyun 	 * the current xc->cppr. Thus we don't need to re-check xc->mfrr
254*4882a593Smuzhiyun 	 * for pending IPIs.
255*4882a593Smuzhiyun 	 */
256*4882a593Smuzhiyun 	if (hirq)
257*4882a593Smuzhiyun 		xc->cppr = prio;
258*4882a593Smuzhiyun 	/*
259*4882a593Smuzhiyun 	 * If it was an IPI the HW CPPR might have been lowered too much
260*4882a593Smuzhiyun 	 * as the HW interrupt we use for IPIs is routed to priority 0.
261*4882a593Smuzhiyun 	 *
262*4882a593Smuzhiyun 	 * We re-sync it here.
263*4882a593Smuzhiyun 	 */
264*4882a593Smuzhiyun 	if (xc->cppr != xc->hw_cppr) {
265*4882a593Smuzhiyun 		xc->hw_cppr = xc->cppr;
266*4882a593Smuzhiyun 		__x_writeb(xc->cppr, __x_tima + TM_QW1_OS + TM_CPPR);
267*4882a593Smuzhiyun 	}
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	return hirq;
270*4882a593Smuzhiyun }
271*4882a593Smuzhiyun 
GLUE(X_PFX,h_xirr)272*4882a593Smuzhiyun X_STATIC unsigned long GLUE(X_PFX,h_xirr)(struct kvm_vcpu *vcpu)
273*4882a593Smuzhiyun {
274*4882a593Smuzhiyun 	struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
275*4882a593Smuzhiyun 	u8 old_cppr;
276*4882a593Smuzhiyun 	u32 hirq;
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun 	pr_devel("H_XIRR\n");
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 	xc->GLUE(X_STAT_PFX,h_xirr)++;
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun 	/* First collect pending bits from HW */
283*4882a593Smuzhiyun 	GLUE(X_PFX,ack_pending)(xc);
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun 	pr_devel(" new pending=0x%02x hw_cppr=%d cppr=%d\n",
286*4882a593Smuzhiyun 		 xc->pending, xc->hw_cppr, xc->cppr);
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun 	/* Grab previous CPPR and reverse map it */
289*4882a593Smuzhiyun 	old_cppr = xive_prio_to_guest(xc->cppr);
290*4882a593Smuzhiyun 
291*4882a593Smuzhiyun 	/* Scan for actual interrupts */
292*4882a593Smuzhiyun 	hirq = GLUE(X_PFX,scan_interrupts)(xc, xc->pending, scan_fetch);
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 	pr_devel(" got hirq=0x%x hw_cppr=%d cppr=%d\n",
295*4882a593Smuzhiyun 		 hirq, xc->hw_cppr, xc->cppr);
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun #ifdef XIVE_RUNTIME_CHECKS
298*4882a593Smuzhiyun 	/* That should never hit */
299*4882a593Smuzhiyun 	if (hirq & 0xff000000)
300*4882a593Smuzhiyun 		pr_warn("XIVE: Weird guest interrupt number 0x%08x\n", hirq);
301*4882a593Smuzhiyun #endif
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 	/*
304*4882a593Smuzhiyun 	 * XXX We could check if the interrupt is masked here and
305*4882a593Smuzhiyun 	 * filter it. If we chose to do so, we would need to do:
306*4882a593Smuzhiyun 	 *
307*4882a593Smuzhiyun 	 *    if (masked) {
308*4882a593Smuzhiyun 	 *        lock();
309*4882a593Smuzhiyun 	 *        if (masked) {
310*4882a593Smuzhiyun 	 *            old_Q = true;
311*4882a593Smuzhiyun 	 *            hirq = 0;
312*4882a593Smuzhiyun 	 *        }
313*4882a593Smuzhiyun 	 *        unlock();
314*4882a593Smuzhiyun 	 *    }
315*4882a593Smuzhiyun 	 */
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun 	/* Return interrupt and old CPPR in GPR4 */
318*4882a593Smuzhiyun 	vcpu->arch.regs.gpr[4] = hirq | (old_cppr << 24);
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun 	return H_SUCCESS;
321*4882a593Smuzhiyun }
322*4882a593Smuzhiyun 
GLUE(X_PFX,h_ipoll)323*4882a593Smuzhiyun X_STATIC unsigned long GLUE(X_PFX,h_ipoll)(struct kvm_vcpu *vcpu, unsigned long server)
324*4882a593Smuzhiyun {
325*4882a593Smuzhiyun 	struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
326*4882a593Smuzhiyun 	u8 pending = xc->pending;
327*4882a593Smuzhiyun 	u32 hirq;
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun 	pr_devel("H_IPOLL(server=%ld)\n", server);
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun 	xc->GLUE(X_STAT_PFX,h_ipoll)++;
332*4882a593Smuzhiyun 
333*4882a593Smuzhiyun 	/* Grab the target VCPU if not the current one */
334*4882a593Smuzhiyun 	if (xc->server_num != server) {
335*4882a593Smuzhiyun 		vcpu = kvmppc_xive_find_server(vcpu->kvm, server);
336*4882a593Smuzhiyun 		if (!vcpu)
337*4882a593Smuzhiyun 			return H_PARAMETER;
338*4882a593Smuzhiyun 		xc = vcpu->arch.xive_vcpu;
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun 		/* Scan all priorities */
341*4882a593Smuzhiyun 		pending = 0xff;
342*4882a593Smuzhiyun 	} else {
343*4882a593Smuzhiyun 		/* Grab pending interrupt if any */
344*4882a593Smuzhiyun 		__be64 qw1 = __x_readq(__x_tima + TM_QW1_OS);
345*4882a593Smuzhiyun 		u8 pipr = be64_to_cpu(qw1) & 0xff;
346*4882a593Smuzhiyun 		if (pipr < 8)
347*4882a593Smuzhiyun 			pending |= 1 << pipr;
348*4882a593Smuzhiyun 	}
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun 	hirq = GLUE(X_PFX,scan_interrupts)(xc, pending, scan_poll);
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun 	/* Return interrupt and old CPPR in GPR4 */
353*4882a593Smuzhiyun 	vcpu->arch.regs.gpr[4] = hirq | (xc->cppr << 24);
354*4882a593Smuzhiyun 
355*4882a593Smuzhiyun 	return H_SUCCESS;
356*4882a593Smuzhiyun }
357*4882a593Smuzhiyun 
GLUE(X_PFX,push_pending_to_hw)358*4882a593Smuzhiyun static void GLUE(X_PFX,push_pending_to_hw)(struct kvmppc_xive_vcpu *xc)
359*4882a593Smuzhiyun {
360*4882a593Smuzhiyun 	u8 pending, prio;
361*4882a593Smuzhiyun 
362*4882a593Smuzhiyun 	pending = xc->pending;
363*4882a593Smuzhiyun 	if (xc->mfrr != 0xff) {
364*4882a593Smuzhiyun 		if (xc->mfrr < 8)
365*4882a593Smuzhiyun 			pending |= 1 << xc->mfrr;
366*4882a593Smuzhiyun 		else
367*4882a593Smuzhiyun 			pending |= 0x80;
368*4882a593Smuzhiyun 	}
369*4882a593Smuzhiyun 	if (!pending)
370*4882a593Smuzhiyun 		return;
371*4882a593Smuzhiyun 	prio = ffs(pending) - 1;
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun 	__x_writeb(prio, __x_tima + TM_SPC_SET_OS_PENDING);
374*4882a593Smuzhiyun }
375*4882a593Smuzhiyun 
GLUE(X_PFX,scan_for_rerouted_irqs)376*4882a593Smuzhiyun static void GLUE(X_PFX,scan_for_rerouted_irqs)(struct kvmppc_xive *xive,
377*4882a593Smuzhiyun 					       struct kvmppc_xive_vcpu *xc)
378*4882a593Smuzhiyun {
379*4882a593Smuzhiyun 	unsigned int prio;
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun 	/* For each priority that is now masked */
382*4882a593Smuzhiyun 	for (prio = xc->cppr; prio < KVMPPC_XIVE_Q_COUNT; prio++) {
383*4882a593Smuzhiyun 		struct xive_q *q = &xc->queues[prio];
384*4882a593Smuzhiyun 		struct kvmppc_xive_irq_state *state;
385*4882a593Smuzhiyun 		struct kvmppc_xive_src_block *sb;
386*4882a593Smuzhiyun 		u32 idx, toggle, entry, irq, hw_num;
387*4882a593Smuzhiyun 		struct xive_irq_data *xd;
388*4882a593Smuzhiyun 		__be32 *qpage;
389*4882a593Smuzhiyun 		u16 src;
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun 		idx = q->idx;
392*4882a593Smuzhiyun 		toggle = q->toggle;
393*4882a593Smuzhiyun 		qpage = READ_ONCE(q->qpage);
394*4882a593Smuzhiyun 		if (!qpage)
395*4882a593Smuzhiyun 			continue;
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 		/* For each interrupt in the queue */
398*4882a593Smuzhiyun 		for (;;) {
399*4882a593Smuzhiyun 			entry = be32_to_cpup(qpage + idx);
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun 			/* No more ? */
402*4882a593Smuzhiyun 			if ((entry >> 31) == toggle)
403*4882a593Smuzhiyun 				break;
404*4882a593Smuzhiyun 			irq = entry & 0x7fffffff;
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun 			/* Skip dummies and IPIs */
407*4882a593Smuzhiyun 			if (irq == XICS_DUMMY || irq == XICS_IPI)
408*4882a593Smuzhiyun 				goto next;
409*4882a593Smuzhiyun 			sb = kvmppc_xive_find_source(xive, irq, &src);
410*4882a593Smuzhiyun 			if (!sb)
411*4882a593Smuzhiyun 				goto next;
412*4882a593Smuzhiyun 			state = &sb->irq_state[src];
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun 			/* Has it been rerouted ? */
415*4882a593Smuzhiyun 			if (xc->server_num == state->act_server)
416*4882a593Smuzhiyun 				goto next;
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 			/*
419*4882a593Smuzhiyun 			 * Allright, it *has* been re-routed, kill it from
420*4882a593Smuzhiyun 			 * the queue.
421*4882a593Smuzhiyun 			 */
422*4882a593Smuzhiyun 			qpage[idx] = cpu_to_be32((entry & 0x80000000) | XICS_DUMMY);
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun 			/* Find the HW interrupt */
425*4882a593Smuzhiyun 			kvmppc_xive_select_irq(state, &hw_num, &xd);
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun 			/* If it's not an LSI, set PQ to 11 the EOI will force a resend */
428*4882a593Smuzhiyun 			if (!(xd->flags & XIVE_IRQ_FLAG_LSI))
429*4882a593Smuzhiyun 				GLUE(X_PFX,esb_load)(xd, XIVE_ESB_SET_PQ_11);
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun 			/* EOI the source */
432*4882a593Smuzhiyun 			GLUE(X_PFX,source_eoi)(hw_num, xd);
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun 		next:
435*4882a593Smuzhiyun 			idx = (idx + 1) & q->msk;
436*4882a593Smuzhiyun 			if (idx == 0)
437*4882a593Smuzhiyun 				toggle ^= 1;
438*4882a593Smuzhiyun 		}
439*4882a593Smuzhiyun 	}
440*4882a593Smuzhiyun }
441*4882a593Smuzhiyun 
GLUE(X_PFX,h_cppr)442*4882a593Smuzhiyun X_STATIC int GLUE(X_PFX,h_cppr)(struct kvm_vcpu *vcpu, unsigned long cppr)
443*4882a593Smuzhiyun {
444*4882a593Smuzhiyun 	struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
445*4882a593Smuzhiyun 	struct kvmppc_xive *xive = vcpu->kvm->arch.xive;
446*4882a593Smuzhiyun 	u8 old_cppr;
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun 	pr_devel("H_CPPR(cppr=%ld)\n", cppr);
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun 	xc->GLUE(X_STAT_PFX,h_cppr)++;
451*4882a593Smuzhiyun 
452*4882a593Smuzhiyun 	/* Map CPPR */
453*4882a593Smuzhiyun 	cppr = xive_prio_from_guest(cppr);
454*4882a593Smuzhiyun 
455*4882a593Smuzhiyun 	/* Remember old and update SW state */
456*4882a593Smuzhiyun 	old_cppr = xc->cppr;
457*4882a593Smuzhiyun 	xc->cppr = cppr;
458*4882a593Smuzhiyun 
459*4882a593Smuzhiyun 	/*
460*4882a593Smuzhiyun 	 * Order the above update of xc->cppr with the subsequent
461*4882a593Smuzhiyun 	 * read of xc->mfrr inside push_pending_to_hw()
462*4882a593Smuzhiyun 	 */
463*4882a593Smuzhiyun 	smp_mb();
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun 	if (cppr > old_cppr) {
466*4882a593Smuzhiyun 		/*
467*4882a593Smuzhiyun 		 * We are masking less, we need to look for pending things
468*4882a593Smuzhiyun 		 * to deliver and set VP pending bits accordingly to trigger
469*4882a593Smuzhiyun 		 * a new interrupt otherwise we might miss MFRR changes for
470*4882a593Smuzhiyun 		 * which we have optimized out sending an IPI signal.
471*4882a593Smuzhiyun 		 */
472*4882a593Smuzhiyun 		GLUE(X_PFX,push_pending_to_hw)(xc);
473*4882a593Smuzhiyun 	} else {
474*4882a593Smuzhiyun 		/*
475*4882a593Smuzhiyun 		 * We are masking more, we need to check the queue for any
476*4882a593Smuzhiyun 		 * interrupt that has been routed to another CPU, take
477*4882a593Smuzhiyun 		 * it out (replace it with the dummy) and retrigger it.
478*4882a593Smuzhiyun 		 *
479*4882a593Smuzhiyun 		 * This is necessary since those interrupts may otherwise
480*4882a593Smuzhiyun 		 * never be processed, at least not until this CPU restores
481*4882a593Smuzhiyun 		 * its CPPR.
482*4882a593Smuzhiyun 		 *
483*4882a593Smuzhiyun 		 * This is in theory racy vs. HW adding new interrupts to
484*4882a593Smuzhiyun 		 * the queue. In practice this works because the interesting
485*4882a593Smuzhiyun 		 * cases are when the guest has done a set_xive() to move the
486*4882a593Smuzhiyun 		 * interrupt away, which flushes the xive, followed by the
487*4882a593Smuzhiyun 		 * target CPU doing a H_CPPR. So any new interrupt coming into
488*4882a593Smuzhiyun 		 * the queue must still be routed to us and isn't a source
489*4882a593Smuzhiyun 		 * of concern.
490*4882a593Smuzhiyun 		 */
491*4882a593Smuzhiyun 		GLUE(X_PFX,scan_for_rerouted_irqs)(xive, xc);
492*4882a593Smuzhiyun 	}
493*4882a593Smuzhiyun 
494*4882a593Smuzhiyun 	/* Apply new CPPR */
495*4882a593Smuzhiyun 	xc->hw_cppr = cppr;
496*4882a593Smuzhiyun 	__x_writeb(cppr, __x_tima + TM_QW1_OS + TM_CPPR);
497*4882a593Smuzhiyun 
498*4882a593Smuzhiyun 	return H_SUCCESS;
499*4882a593Smuzhiyun }
500*4882a593Smuzhiyun 
GLUE(X_PFX,h_eoi)501*4882a593Smuzhiyun X_STATIC int GLUE(X_PFX,h_eoi)(struct kvm_vcpu *vcpu, unsigned long xirr)
502*4882a593Smuzhiyun {
503*4882a593Smuzhiyun 	struct kvmppc_xive *xive = vcpu->kvm->arch.xive;
504*4882a593Smuzhiyun 	struct kvmppc_xive_src_block *sb;
505*4882a593Smuzhiyun 	struct kvmppc_xive_irq_state *state;
506*4882a593Smuzhiyun 	struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
507*4882a593Smuzhiyun 	struct xive_irq_data *xd;
508*4882a593Smuzhiyun 	u8 new_cppr = xirr >> 24;
509*4882a593Smuzhiyun 	u32 irq = xirr & 0x00ffffff, hw_num;
510*4882a593Smuzhiyun 	u16 src;
511*4882a593Smuzhiyun 	int rc = 0;
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun 	pr_devel("H_EOI(xirr=%08lx)\n", xirr);
514*4882a593Smuzhiyun 
515*4882a593Smuzhiyun 	xc->GLUE(X_STAT_PFX,h_eoi)++;
516*4882a593Smuzhiyun 
517*4882a593Smuzhiyun 	xc->cppr = xive_prio_from_guest(new_cppr);
518*4882a593Smuzhiyun 
519*4882a593Smuzhiyun 	/*
520*4882a593Smuzhiyun 	 * IPIs are synthetized from MFRR and thus don't need
521*4882a593Smuzhiyun 	 * any special EOI handling. The underlying interrupt
522*4882a593Smuzhiyun 	 * used to signal MFRR changes is EOId when fetched from
523*4882a593Smuzhiyun 	 * the queue.
524*4882a593Smuzhiyun 	 */
525*4882a593Smuzhiyun 	if (irq == XICS_IPI || irq == 0) {
526*4882a593Smuzhiyun 		/*
527*4882a593Smuzhiyun 		 * This barrier orders the setting of xc->cppr vs.
528*4882a593Smuzhiyun 		 * subsquent test of xc->mfrr done inside
529*4882a593Smuzhiyun 		 * scan_interrupts and push_pending_to_hw
530*4882a593Smuzhiyun 		 */
531*4882a593Smuzhiyun 		smp_mb();
532*4882a593Smuzhiyun 		goto bail;
533*4882a593Smuzhiyun 	}
534*4882a593Smuzhiyun 
535*4882a593Smuzhiyun 	/* Find interrupt source */
536*4882a593Smuzhiyun 	sb = kvmppc_xive_find_source(xive, irq, &src);
537*4882a593Smuzhiyun 	if (!sb) {
538*4882a593Smuzhiyun 		pr_devel(" source not found !\n");
539*4882a593Smuzhiyun 		rc = H_PARAMETER;
540*4882a593Smuzhiyun 		/* Same as above */
541*4882a593Smuzhiyun 		smp_mb();
542*4882a593Smuzhiyun 		goto bail;
543*4882a593Smuzhiyun 	}
544*4882a593Smuzhiyun 	state = &sb->irq_state[src];
545*4882a593Smuzhiyun 	kvmppc_xive_select_irq(state, &hw_num, &xd);
546*4882a593Smuzhiyun 
547*4882a593Smuzhiyun 	state->in_eoi = true;
548*4882a593Smuzhiyun 
549*4882a593Smuzhiyun 	/*
550*4882a593Smuzhiyun 	 * This barrier orders both setting of in_eoi above vs,
551*4882a593Smuzhiyun 	 * subsequent test of guest_priority, and the setting
552*4882a593Smuzhiyun 	 * of xc->cppr vs. subsquent test of xc->mfrr done inside
553*4882a593Smuzhiyun 	 * scan_interrupts and push_pending_to_hw
554*4882a593Smuzhiyun 	 */
555*4882a593Smuzhiyun 	smp_mb();
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun again:
558*4882a593Smuzhiyun 	if (state->guest_priority == MASKED) {
559*4882a593Smuzhiyun 		arch_spin_lock(&sb->lock);
560*4882a593Smuzhiyun 		if (state->guest_priority != MASKED) {
561*4882a593Smuzhiyun 			arch_spin_unlock(&sb->lock);
562*4882a593Smuzhiyun 			goto again;
563*4882a593Smuzhiyun 		}
564*4882a593Smuzhiyun 		pr_devel(" EOI on saved P...\n");
565*4882a593Smuzhiyun 
566*4882a593Smuzhiyun 		/* Clear old_p, that will cause unmask to perform an EOI */
567*4882a593Smuzhiyun 		state->old_p = false;
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun 		arch_spin_unlock(&sb->lock);
570*4882a593Smuzhiyun 	} else {
571*4882a593Smuzhiyun 		pr_devel(" EOI on source...\n");
572*4882a593Smuzhiyun 
573*4882a593Smuzhiyun 		/* Perform EOI on the source */
574*4882a593Smuzhiyun 		GLUE(X_PFX,source_eoi)(hw_num, xd);
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun 		/* If it's an emulated LSI, check level and resend */
577*4882a593Smuzhiyun 		if (state->lsi && state->asserted)
578*4882a593Smuzhiyun 			__x_writeq(0, __x_trig_page(xd));
579*4882a593Smuzhiyun 
580*4882a593Smuzhiyun 	}
581*4882a593Smuzhiyun 
582*4882a593Smuzhiyun 	/*
583*4882a593Smuzhiyun 	 * This barrier orders the above guest_priority check
584*4882a593Smuzhiyun 	 * and spin_lock/unlock with clearing in_eoi below.
585*4882a593Smuzhiyun 	 *
586*4882a593Smuzhiyun 	 * It also has to be a full mb() as it must ensure
587*4882a593Smuzhiyun 	 * the MMIOs done in source_eoi() are completed before
588*4882a593Smuzhiyun 	 * state->in_eoi is visible.
589*4882a593Smuzhiyun 	 */
590*4882a593Smuzhiyun 	mb();
591*4882a593Smuzhiyun 	state->in_eoi = false;
592*4882a593Smuzhiyun bail:
593*4882a593Smuzhiyun 
594*4882a593Smuzhiyun 	/* Re-evaluate pending IRQs and update HW */
595*4882a593Smuzhiyun 	GLUE(X_PFX,scan_interrupts)(xc, xc->pending, scan_eoi);
596*4882a593Smuzhiyun 	GLUE(X_PFX,push_pending_to_hw)(xc);
597*4882a593Smuzhiyun 	pr_devel(" after scan pending=%02x\n", xc->pending);
598*4882a593Smuzhiyun 
599*4882a593Smuzhiyun 	/* Apply new CPPR */
600*4882a593Smuzhiyun 	xc->hw_cppr = xc->cppr;
601*4882a593Smuzhiyun 	__x_writeb(xc->cppr, __x_tima + TM_QW1_OS + TM_CPPR);
602*4882a593Smuzhiyun 
603*4882a593Smuzhiyun 	return rc;
604*4882a593Smuzhiyun }
605*4882a593Smuzhiyun 
GLUE(X_PFX,h_ipi)606*4882a593Smuzhiyun X_STATIC int GLUE(X_PFX,h_ipi)(struct kvm_vcpu *vcpu, unsigned long server,
607*4882a593Smuzhiyun 			       unsigned long mfrr)
608*4882a593Smuzhiyun {
609*4882a593Smuzhiyun 	struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
610*4882a593Smuzhiyun 
611*4882a593Smuzhiyun 	pr_devel("H_IPI(server=%08lx,mfrr=%ld)\n", server, mfrr);
612*4882a593Smuzhiyun 
613*4882a593Smuzhiyun 	xc->GLUE(X_STAT_PFX,h_ipi)++;
614*4882a593Smuzhiyun 
615*4882a593Smuzhiyun 	/* Find target */
616*4882a593Smuzhiyun 	vcpu = kvmppc_xive_find_server(vcpu->kvm, server);
617*4882a593Smuzhiyun 	if (!vcpu)
618*4882a593Smuzhiyun 		return H_PARAMETER;
619*4882a593Smuzhiyun 	xc = vcpu->arch.xive_vcpu;
620*4882a593Smuzhiyun 
621*4882a593Smuzhiyun 	/* Locklessly write over MFRR */
622*4882a593Smuzhiyun 	xc->mfrr = mfrr;
623*4882a593Smuzhiyun 
624*4882a593Smuzhiyun 	/*
625*4882a593Smuzhiyun 	 * The load of xc->cppr below and the subsequent MMIO store
626*4882a593Smuzhiyun 	 * to the IPI must happen after the above mfrr update is
627*4882a593Smuzhiyun 	 * globally visible so that:
628*4882a593Smuzhiyun 	 *
629*4882a593Smuzhiyun 	 * - Synchronize with another CPU doing an H_EOI or a H_CPPR
630*4882a593Smuzhiyun 	 *   updating xc->cppr then reading xc->mfrr.
631*4882a593Smuzhiyun 	 *
632*4882a593Smuzhiyun 	 * - The target of the IPI sees the xc->mfrr update
633*4882a593Smuzhiyun 	 */
634*4882a593Smuzhiyun 	mb();
635*4882a593Smuzhiyun 
636*4882a593Smuzhiyun 	/* Shoot the IPI if most favored than target cppr */
637*4882a593Smuzhiyun 	if (mfrr < xc->cppr)
638*4882a593Smuzhiyun 		__x_writeq(0, __x_trig_page(&xc->vp_ipi_data));
639*4882a593Smuzhiyun 
640*4882a593Smuzhiyun 	return H_SUCCESS;
641*4882a593Smuzhiyun }
642