xref: /OK3568_Linux_fs/kernel/arch/powerpc/kvm/book3s_hv_rm_xics.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright 2012 Michael Ellerman, IBM Corporation.
4*4882a593Smuzhiyun  * Copyright 2012 Benjamin Herrenschmidt, IBM Corporation
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #include <linux/kernel.h>
8*4882a593Smuzhiyun #include <linux/kvm_host.h>
9*4882a593Smuzhiyun #include <linux/err.h>
10*4882a593Smuzhiyun #include <linux/kernel_stat.h>
11*4882a593Smuzhiyun #include <linux/pgtable.h>
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include <asm/kvm_book3s.h>
14*4882a593Smuzhiyun #include <asm/kvm_ppc.h>
15*4882a593Smuzhiyun #include <asm/hvcall.h>
16*4882a593Smuzhiyun #include <asm/xics.h>
17*4882a593Smuzhiyun #include <asm/synch.h>
18*4882a593Smuzhiyun #include <asm/cputhreads.h>
19*4882a593Smuzhiyun #include <asm/ppc-opcode.h>
20*4882a593Smuzhiyun #include <asm/pnv-pci.h>
21*4882a593Smuzhiyun #include <asm/opal.h>
22*4882a593Smuzhiyun #include <asm/smp.h>
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun #include "book3s_xics.h"
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun #define DEBUG_PASSUP
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun int h_ipi_redirect = 1;
29*4882a593Smuzhiyun EXPORT_SYMBOL(h_ipi_redirect);
30*4882a593Smuzhiyun int kvm_irq_bypass = 1;
31*4882a593Smuzhiyun EXPORT_SYMBOL(kvm_irq_bypass);
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun static void icp_rm_deliver_irq(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
34*4882a593Smuzhiyun 			    u32 new_irq, bool check_resend);
35*4882a593Smuzhiyun static int xics_opal_set_server(unsigned int hw_irq, int server_cpu);
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun /* -- ICS routines -- */
ics_rm_check_resend(struct kvmppc_xics * xics,struct kvmppc_ics * ics,struct kvmppc_icp * icp)38*4882a593Smuzhiyun static void ics_rm_check_resend(struct kvmppc_xics *xics,
39*4882a593Smuzhiyun 				struct kvmppc_ics *ics, struct kvmppc_icp *icp)
40*4882a593Smuzhiyun {
41*4882a593Smuzhiyun 	int i;
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun 	for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
44*4882a593Smuzhiyun 		struct ics_irq_state *state = &ics->irq_state[i];
45*4882a593Smuzhiyun 		if (state->resend)
46*4882a593Smuzhiyun 			icp_rm_deliver_irq(xics, icp, state->number, true);
47*4882a593Smuzhiyun 	}
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun }
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun /* -- ICP routines -- */
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun #ifdef CONFIG_SMP
icp_send_hcore_msg(int hcore,struct kvm_vcpu * vcpu)54*4882a593Smuzhiyun static inline void icp_send_hcore_msg(int hcore, struct kvm_vcpu *vcpu)
55*4882a593Smuzhiyun {
56*4882a593Smuzhiyun 	int hcpu;
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 	hcpu = hcore << threads_shift;
59*4882a593Smuzhiyun 	kvmppc_host_rm_ops_hv->rm_core[hcore].rm_data = vcpu;
60*4882a593Smuzhiyun 	smp_muxed_ipi_set_message(hcpu, PPC_MSG_RM_HOST_ACTION);
61*4882a593Smuzhiyun 	kvmppc_set_host_ipi(hcpu);
62*4882a593Smuzhiyun 	smp_mb();
63*4882a593Smuzhiyun 	kvmhv_rm_send_ipi(hcpu);
64*4882a593Smuzhiyun }
65*4882a593Smuzhiyun #else
icp_send_hcore_msg(int hcore,struct kvm_vcpu * vcpu)66*4882a593Smuzhiyun static inline void icp_send_hcore_msg(int hcore, struct kvm_vcpu *vcpu) { }
67*4882a593Smuzhiyun #endif
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun /*
70*4882a593Smuzhiyun  * We start the search from our current CPU Id in the core map
71*4882a593Smuzhiyun  * and go in a circle until we get back to our ID looking for a
72*4882a593Smuzhiyun  * core that is running in host context and that hasn't already
73*4882a593Smuzhiyun  * been targeted for another rm_host_ops.
74*4882a593Smuzhiyun  *
75*4882a593Smuzhiyun  * In the future, could consider using a fairer algorithm (one
76*4882a593Smuzhiyun  * that distributes the IPIs better)
77*4882a593Smuzhiyun  *
78*4882a593Smuzhiyun  * Returns -1, if no CPU could be found in the host
79*4882a593Smuzhiyun  * Else, returns a CPU Id which has been reserved for use
80*4882a593Smuzhiyun  */
grab_next_hostcore(int start,struct kvmppc_host_rm_core * rm_core,int max,int action)81*4882a593Smuzhiyun static inline int grab_next_hostcore(int start,
82*4882a593Smuzhiyun 		struct kvmppc_host_rm_core *rm_core, int max, int action)
83*4882a593Smuzhiyun {
84*4882a593Smuzhiyun 	bool success;
85*4882a593Smuzhiyun 	int core;
86*4882a593Smuzhiyun 	union kvmppc_rm_state old, new;
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 	for (core = start + 1; core < max; core++)  {
89*4882a593Smuzhiyun 		old = new = READ_ONCE(rm_core[core].rm_state);
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 		if (!old.in_host || old.rm_action)
92*4882a593Smuzhiyun 			continue;
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 		/* Try to grab this host core if not taken already. */
95*4882a593Smuzhiyun 		new.rm_action = action;
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 		success = cmpxchg64(&rm_core[core].rm_state.raw,
98*4882a593Smuzhiyun 						old.raw, new.raw) == old.raw;
99*4882a593Smuzhiyun 		if (success) {
100*4882a593Smuzhiyun 			/*
101*4882a593Smuzhiyun 			 * Make sure that the store to the rm_action is made
102*4882a593Smuzhiyun 			 * visible before we return to caller (and the
103*4882a593Smuzhiyun 			 * subsequent store to rm_data) to synchronize with
104*4882a593Smuzhiyun 			 * the IPI handler.
105*4882a593Smuzhiyun 			 */
106*4882a593Smuzhiyun 			smp_wmb();
107*4882a593Smuzhiyun 			return core;
108*4882a593Smuzhiyun 		}
109*4882a593Smuzhiyun 	}
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	return -1;
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun 
find_available_hostcore(int action)114*4882a593Smuzhiyun static inline int find_available_hostcore(int action)
115*4882a593Smuzhiyun {
116*4882a593Smuzhiyun 	int core;
117*4882a593Smuzhiyun 	int my_core = smp_processor_id() >> threads_shift;
118*4882a593Smuzhiyun 	struct kvmppc_host_rm_core *rm_core = kvmppc_host_rm_ops_hv->rm_core;
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	core = grab_next_hostcore(my_core, rm_core, cpu_nr_cores(), action);
121*4882a593Smuzhiyun 	if (core == -1)
122*4882a593Smuzhiyun 		core = grab_next_hostcore(core, rm_core, my_core, action);
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 	return core;
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun 
icp_rm_set_vcpu_irq(struct kvm_vcpu * vcpu,struct kvm_vcpu * this_vcpu)127*4882a593Smuzhiyun static void icp_rm_set_vcpu_irq(struct kvm_vcpu *vcpu,
128*4882a593Smuzhiyun 				struct kvm_vcpu *this_vcpu)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun 	struct kvmppc_icp *this_icp = this_vcpu->arch.icp;
131*4882a593Smuzhiyun 	int cpu;
132*4882a593Smuzhiyun 	int hcore;
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 	/* Mark the target VCPU as having an interrupt pending */
135*4882a593Smuzhiyun 	vcpu->stat.queue_intr++;
136*4882a593Smuzhiyun 	set_bit(BOOK3S_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions);
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	/* Kick self ? Just set MER and return */
139*4882a593Smuzhiyun 	if (vcpu == this_vcpu) {
140*4882a593Smuzhiyun 		mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) | LPCR_MER);
141*4882a593Smuzhiyun 		return;
142*4882a593Smuzhiyun 	}
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 	if (xive_enabled() && kvmhv_on_pseries()) {
145*4882a593Smuzhiyun 		/* No XICS access or hypercalls available, too hard */
146*4882a593Smuzhiyun 		this_icp->rm_action |= XICS_RM_KICK_VCPU;
147*4882a593Smuzhiyun 		this_icp->rm_kick_target = vcpu;
148*4882a593Smuzhiyun 		return;
149*4882a593Smuzhiyun 	}
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	/*
152*4882a593Smuzhiyun 	 * Check if the core is loaded,
153*4882a593Smuzhiyun 	 * if not, find an available host core to post to wake the VCPU,
154*4882a593Smuzhiyun 	 * if we can't find one, set up state to eventually return too hard.
155*4882a593Smuzhiyun 	 */
156*4882a593Smuzhiyun 	cpu = vcpu->arch.thread_cpu;
157*4882a593Smuzhiyun 	if (cpu < 0 || cpu >= nr_cpu_ids) {
158*4882a593Smuzhiyun 		hcore = -1;
159*4882a593Smuzhiyun 		if (kvmppc_host_rm_ops_hv && h_ipi_redirect)
160*4882a593Smuzhiyun 			hcore = find_available_hostcore(XICS_RM_KICK_VCPU);
161*4882a593Smuzhiyun 		if (hcore != -1) {
162*4882a593Smuzhiyun 			icp_send_hcore_msg(hcore, vcpu);
163*4882a593Smuzhiyun 		} else {
164*4882a593Smuzhiyun 			this_icp->rm_action |= XICS_RM_KICK_VCPU;
165*4882a593Smuzhiyun 			this_icp->rm_kick_target = vcpu;
166*4882a593Smuzhiyun 		}
167*4882a593Smuzhiyun 		return;
168*4882a593Smuzhiyun 	}
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 	smp_mb();
171*4882a593Smuzhiyun 	kvmhv_rm_send_ipi(cpu);
172*4882a593Smuzhiyun }
173*4882a593Smuzhiyun 
icp_rm_clr_vcpu_irq(struct kvm_vcpu * vcpu)174*4882a593Smuzhiyun static void icp_rm_clr_vcpu_irq(struct kvm_vcpu *vcpu)
175*4882a593Smuzhiyun {
176*4882a593Smuzhiyun 	/* Note: Only called on self ! */
177*4882a593Smuzhiyun 	clear_bit(BOOK3S_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions);
178*4882a593Smuzhiyun 	mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) & ~LPCR_MER);
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun 
icp_rm_try_update(struct kvmppc_icp * icp,union kvmppc_icp_state old,union kvmppc_icp_state new)181*4882a593Smuzhiyun static inline bool icp_rm_try_update(struct kvmppc_icp *icp,
182*4882a593Smuzhiyun 				     union kvmppc_icp_state old,
183*4882a593Smuzhiyun 				     union kvmppc_icp_state new)
184*4882a593Smuzhiyun {
185*4882a593Smuzhiyun 	struct kvm_vcpu *this_vcpu = local_paca->kvm_hstate.kvm_vcpu;
186*4882a593Smuzhiyun 	bool success;
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	/* Calculate new output value */
189*4882a593Smuzhiyun 	new.out_ee = (new.xisr && (new.pending_pri < new.cppr));
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 	/* Attempt atomic update */
192*4882a593Smuzhiyun 	success = cmpxchg64(&icp->state.raw, old.raw, new.raw) == old.raw;
193*4882a593Smuzhiyun 	if (!success)
194*4882a593Smuzhiyun 		goto bail;
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 	/*
197*4882a593Smuzhiyun 	 * Check for output state update
198*4882a593Smuzhiyun 	 *
199*4882a593Smuzhiyun 	 * Note that this is racy since another processor could be updating
200*4882a593Smuzhiyun 	 * the state already. This is why we never clear the interrupt output
201*4882a593Smuzhiyun 	 * here, we only ever set it. The clear only happens prior to doing
202*4882a593Smuzhiyun 	 * an update and only by the processor itself. Currently we do it
203*4882a593Smuzhiyun 	 * in Accept (H_XIRR) and Up_Cppr (H_XPPR).
204*4882a593Smuzhiyun 	 *
205*4882a593Smuzhiyun 	 * We also do not try to figure out whether the EE state has changed,
206*4882a593Smuzhiyun 	 * we unconditionally set it if the new state calls for it. The reason
207*4882a593Smuzhiyun 	 * for that is that we opportunistically remove the pending interrupt
208*4882a593Smuzhiyun 	 * flag when raising CPPR, so we need to set it back here if an
209*4882a593Smuzhiyun 	 * interrupt is still pending.
210*4882a593Smuzhiyun 	 */
211*4882a593Smuzhiyun 	if (new.out_ee)
212*4882a593Smuzhiyun 		icp_rm_set_vcpu_irq(icp->vcpu, this_vcpu);
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 	/* Expose the state change for debug purposes */
215*4882a593Smuzhiyun 	this_vcpu->arch.icp->rm_dbgstate = new;
216*4882a593Smuzhiyun 	this_vcpu->arch.icp->rm_dbgtgt = icp->vcpu;
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun  bail:
219*4882a593Smuzhiyun 	return success;
220*4882a593Smuzhiyun }
221*4882a593Smuzhiyun 
check_too_hard(struct kvmppc_xics * xics,struct kvmppc_icp * icp)222*4882a593Smuzhiyun static inline int check_too_hard(struct kvmppc_xics *xics,
223*4882a593Smuzhiyun 				 struct kvmppc_icp *icp)
224*4882a593Smuzhiyun {
225*4882a593Smuzhiyun 	return (xics->real_mode_dbg || icp->rm_action) ? H_TOO_HARD : H_SUCCESS;
226*4882a593Smuzhiyun }
227*4882a593Smuzhiyun 
icp_rm_check_resend(struct kvmppc_xics * xics,struct kvmppc_icp * icp)228*4882a593Smuzhiyun static void icp_rm_check_resend(struct kvmppc_xics *xics,
229*4882a593Smuzhiyun 			     struct kvmppc_icp *icp)
230*4882a593Smuzhiyun {
231*4882a593Smuzhiyun 	u32 icsid;
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 	/* Order this load with the test for need_resend in the caller */
234*4882a593Smuzhiyun 	smp_rmb();
235*4882a593Smuzhiyun 	for_each_set_bit(icsid, icp->resend_map, xics->max_icsid + 1) {
236*4882a593Smuzhiyun 		struct kvmppc_ics *ics = xics->ics[icsid];
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 		if (!test_and_clear_bit(icsid, icp->resend_map))
239*4882a593Smuzhiyun 			continue;
240*4882a593Smuzhiyun 		if (!ics)
241*4882a593Smuzhiyun 			continue;
242*4882a593Smuzhiyun 		ics_rm_check_resend(xics, ics, icp);
243*4882a593Smuzhiyun 	}
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun 
icp_rm_try_to_deliver(struct kvmppc_icp * icp,u32 irq,u8 priority,u32 * reject)246*4882a593Smuzhiyun static bool icp_rm_try_to_deliver(struct kvmppc_icp *icp, u32 irq, u8 priority,
247*4882a593Smuzhiyun 			       u32 *reject)
248*4882a593Smuzhiyun {
249*4882a593Smuzhiyun 	union kvmppc_icp_state old_state, new_state;
250*4882a593Smuzhiyun 	bool success;
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 	do {
253*4882a593Smuzhiyun 		old_state = new_state = READ_ONCE(icp->state);
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 		*reject = 0;
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 		/* See if we can deliver */
258*4882a593Smuzhiyun 		success = new_state.cppr > priority &&
259*4882a593Smuzhiyun 			new_state.mfrr > priority &&
260*4882a593Smuzhiyun 			new_state.pending_pri > priority;
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 		/*
263*4882a593Smuzhiyun 		 * If we can, check for a rejection and perform the
264*4882a593Smuzhiyun 		 * delivery
265*4882a593Smuzhiyun 		 */
266*4882a593Smuzhiyun 		if (success) {
267*4882a593Smuzhiyun 			*reject = new_state.xisr;
268*4882a593Smuzhiyun 			new_state.xisr = irq;
269*4882a593Smuzhiyun 			new_state.pending_pri = priority;
270*4882a593Smuzhiyun 		} else {
271*4882a593Smuzhiyun 			/*
272*4882a593Smuzhiyun 			 * If we failed to deliver we set need_resend
273*4882a593Smuzhiyun 			 * so a subsequent CPPR state change causes us
274*4882a593Smuzhiyun 			 * to try a new delivery.
275*4882a593Smuzhiyun 			 */
276*4882a593Smuzhiyun 			new_state.need_resend = true;
277*4882a593Smuzhiyun 		}
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun 	} while (!icp_rm_try_update(icp, old_state, new_state));
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 	return success;
282*4882a593Smuzhiyun }
283*4882a593Smuzhiyun 
icp_rm_deliver_irq(struct kvmppc_xics * xics,struct kvmppc_icp * icp,u32 new_irq,bool check_resend)284*4882a593Smuzhiyun static void icp_rm_deliver_irq(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
285*4882a593Smuzhiyun 			    u32 new_irq, bool check_resend)
286*4882a593Smuzhiyun {
287*4882a593Smuzhiyun 	struct ics_irq_state *state;
288*4882a593Smuzhiyun 	struct kvmppc_ics *ics;
289*4882a593Smuzhiyun 	u32 reject;
290*4882a593Smuzhiyun 	u16 src;
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun 	/*
293*4882a593Smuzhiyun 	 * This is used both for initial delivery of an interrupt and
294*4882a593Smuzhiyun 	 * for subsequent rejection.
295*4882a593Smuzhiyun 	 *
296*4882a593Smuzhiyun 	 * Rejection can be racy vs. resends. We have evaluated the
297*4882a593Smuzhiyun 	 * rejection in an atomic ICP transaction which is now complete,
298*4882a593Smuzhiyun 	 * so potentially the ICP can already accept the interrupt again.
299*4882a593Smuzhiyun 	 *
300*4882a593Smuzhiyun 	 * So we need to retry the delivery. Essentially the reject path
301*4882a593Smuzhiyun 	 * boils down to a failed delivery. Always.
302*4882a593Smuzhiyun 	 *
303*4882a593Smuzhiyun 	 * Now the interrupt could also have moved to a different target,
304*4882a593Smuzhiyun 	 * thus we may need to re-do the ICP lookup as well
305*4882a593Smuzhiyun 	 */
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun  again:
308*4882a593Smuzhiyun 	/* Get the ICS state and lock it */
309*4882a593Smuzhiyun 	ics = kvmppc_xics_find_ics(xics, new_irq, &src);
310*4882a593Smuzhiyun 	if (!ics) {
311*4882a593Smuzhiyun 		/* Unsafe increment, but this does not need to be accurate */
312*4882a593Smuzhiyun 		xics->err_noics++;
313*4882a593Smuzhiyun 		return;
314*4882a593Smuzhiyun 	}
315*4882a593Smuzhiyun 	state = &ics->irq_state[src];
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun 	/* Get a lock on the ICS */
318*4882a593Smuzhiyun 	arch_spin_lock(&ics->lock);
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun 	/* Get our server */
321*4882a593Smuzhiyun 	if (!icp || state->server != icp->server_num) {
322*4882a593Smuzhiyun 		icp = kvmppc_xics_find_server(xics->kvm, state->server);
323*4882a593Smuzhiyun 		if (!icp) {
324*4882a593Smuzhiyun 			/* Unsafe increment again*/
325*4882a593Smuzhiyun 			xics->err_noicp++;
326*4882a593Smuzhiyun 			goto out;
327*4882a593Smuzhiyun 		}
328*4882a593Smuzhiyun 	}
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun 	if (check_resend)
331*4882a593Smuzhiyun 		if (!state->resend)
332*4882a593Smuzhiyun 			goto out;
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun 	/* Clear the resend bit of that interrupt */
335*4882a593Smuzhiyun 	state->resend = 0;
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun 	/*
338*4882a593Smuzhiyun 	 * If masked, bail out
339*4882a593Smuzhiyun 	 *
340*4882a593Smuzhiyun 	 * Note: PAPR doesn't mention anything about masked pending
341*4882a593Smuzhiyun 	 * when doing a resend, only when doing a delivery.
342*4882a593Smuzhiyun 	 *
343*4882a593Smuzhiyun 	 * However that would have the effect of losing a masked
344*4882a593Smuzhiyun 	 * interrupt that was rejected and isn't consistent with
345*4882a593Smuzhiyun 	 * the whole masked_pending business which is about not
346*4882a593Smuzhiyun 	 * losing interrupts that occur while masked.
347*4882a593Smuzhiyun 	 *
348*4882a593Smuzhiyun 	 * I don't differentiate normal deliveries and resends, this
349*4882a593Smuzhiyun 	 * implementation will differ from PAPR and not lose such
350*4882a593Smuzhiyun 	 * interrupts.
351*4882a593Smuzhiyun 	 */
352*4882a593Smuzhiyun 	if (state->priority == MASKED) {
353*4882a593Smuzhiyun 		state->masked_pending = 1;
354*4882a593Smuzhiyun 		goto out;
355*4882a593Smuzhiyun 	}
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun 	/*
358*4882a593Smuzhiyun 	 * Try the delivery, this will set the need_resend flag
359*4882a593Smuzhiyun 	 * in the ICP as part of the atomic transaction if the
360*4882a593Smuzhiyun 	 * delivery is not possible.
361*4882a593Smuzhiyun 	 *
362*4882a593Smuzhiyun 	 * Note that if successful, the new delivery might have itself
363*4882a593Smuzhiyun 	 * rejected an interrupt that was "delivered" before we took the
364*4882a593Smuzhiyun 	 * ics spin lock.
365*4882a593Smuzhiyun 	 *
366*4882a593Smuzhiyun 	 * In this case we do the whole sequence all over again for the
367*4882a593Smuzhiyun 	 * new guy. We cannot assume that the rejected interrupt is less
368*4882a593Smuzhiyun 	 * favored than the new one, and thus doesn't need to be delivered,
369*4882a593Smuzhiyun 	 * because by the time we exit icp_rm_try_to_deliver() the target
370*4882a593Smuzhiyun 	 * processor may well have already consumed & completed it, and thus
371*4882a593Smuzhiyun 	 * the rejected interrupt might actually be already acceptable.
372*4882a593Smuzhiyun 	 */
373*4882a593Smuzhiyun 	if (icp_rm_try_to_deliver(icp, new_irq, state->priority, &reject)) {
374*4882a593Smuzhiyun 		/*
375*4882a593Smuzhiyun 		 * Delivery was successful, did we reject somebody else ?
376*4882a593Smuzhiyun 		 */
377*4882a593Smuzhiyun 		if (reject && reject != XICS_IPI) {
378*4882a593Smuzhiyun 			arch_spin_unlock(&ics->lock);
379*4882a593Smuzhiyun 			icp->n_reject++;
380*4882a593Smuzhiyun 			new_irq = reject;
381*4882a593Smuzhiyun 			check_resend = 0;
382*4882a593Smuzhiyun 			goto again;
383*4882a593Smuzhiyun 		}
384*4882a593Smuzhiyun 	} else {
385*4882a593Smuzhiyun 		/*
386*4882a593Smuzhiyun 		 * We failed to deliver the interrupt we need to set the
387*4882a593Smuzhiyun 		 * resend map bit and mark the ICS state as needing a resend
388*4882a593Smuzhiyun 		 */
389*4882a593Smuzhiyun 		state->resend = 1;
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun 		/*
392*4882a593Smuzhiyun 		 * Make sure when checking resend, we don't miss the resend
393*4882a593Smuzhiyun 		 * if resend_map bit is seen and cleared.
394*4882a593Smuzhiyun 		 */
395*4882a593Smuzhiyun 		smp_wmb();
396*4882a593Smuzhiyun 		set_bit(ics->icsid, icp->resend_map);
397*4882a593Smuzhiyun 
398*4882a593Smuzhiyun 		/*
399*4882a593Smuzhiyun 		 * If the need_resend flag got cleared in the ICP some time
400*4882a593Smuzhiyun 		 * between icp_rm_try_to_deliver() atomic update and now, then
401*4882a593Smuzhiyun 		 * we know it might have missed the resend_map bit. So we
402*4882a593Smuzhiyun 		 * retry
403*4882a593Smuzhiyun 		 */
404*4882a593Smuzhiyun 		smp_mb();
405*4882a593Smuzhiyun 		if (!icp->state.need_resend) {
406*4882a593Smuzhiyun 			state->resend = 0;
407*4882a593Smuzhiyun 			arch_spin_unlock(&ics->lock);
408*4882a593Smuzhiyun 			check_resend = 0;
409*4882a593Smuzhiyun 			goto again;
410*4882a593Smuzhiyun 		}
411*4882a593Smuzhiyun 	}
412*4882a593Smuzhiyun  out:
413*4882a593Smuzhiyun 	arch_spin_unlock(&ics->lock);
414*4882a593Smuzhiyun }
415*4882a593Smuzhiyun 
icp_rm_down_cppr(struct kvmppc_xics * xics,struct kvmppc_icp * icp,u8 new_cppr)416*4882a593Smuzhiyun static void icp_rm_down_cppr(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
417*4882a593Smuzhiyun 			     u8 new_cppr)
418*4882a593Smuzhiyun {
419*4882a593Smuzhiyun 	union kvmppc_icp_state old_state, new_state;
420*4882a593Smuzhiyun 	bool resend;
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun 	/*
423*4882a593Smuzhiyun 	 * This handles several related states in one operation:
424*4882a593Smuzhiyun 	 *
425*4882a593Smuzhiyun 	 * ICP State: Down_CPPR
426*4882a593Smuzhiyun 	 *
427*4882a593Smuzhiyun 	 * Load CPPR with new value and if the XISR is 0
428*4882a593Smuzhiyun 	 * then check for resends:
429*4882a593Smuzhiyun 	 *
430*4882a593Smuzhiyun 	 * ICP State: Resend
431*4882a593Smuzhiyun 	 *
432*4882a593Smuzhiyun 	 * If MFRR is more favored than CPPR, check for IPIs
433*4882a593Smuzhiyun 	 * and notify ICS of a potential resend. This is done
434*4882a593Smuzhiyun 	 * asynchronously (when used in real mode, we will have
435*4882a593Smuzhiyun 	 * to exit here).
436*4882a593Smuzhiyun 	 *
437*4882a593Smuzhiyun 	 * We do not handle the complete Check_IPI as documented
438*4882a593Smuzhiyun 	 * here. In the PAPR, this state will be used for both
439*4882a593Smuzhiyun 	 * Set_MFRR and Down_CPPR. However, we know that we aren't
440*4882a593Smuzhiyun 	 * changing the MFRR state here so we don't need to handle
441*4882a593Smuzhiyun 	 * the case of an MFRR causing a reject of a pending irq,
442*4882a593Smuzhiyun 	 * this will have been handled when the MFRR was set in the
443*4882a593Smuzhiyun 	 * first place.
444*4882a593Smuzhiyun 	 *
445*4882a593Smuzhiyun 	 * Thus we don't have to handle rejects, only resends.
446*4882a593Smuzhiyun 	 *
447*4882a593Smuzhiyun 	 * When implementing real mode for HV KVM, resend will lead to
448*4882a593Smuzhiyun 	 * a H_TOO_HARD return and the whole transaction will be handled
449*4882a593Smuzhiyun 	 * in virtual mode.
450*4882a593Smuzhiyun 	 */
451*4882a593Smuzhiyun 	do {
452*4882a593Smuzhiyun 		old_state = new_state = READ_ONCE(icp->state);
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun 		/* Down_CPPR */
455*4882a593Smuzhiyun 		new_state.cppr = new_cppr;
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun 		/*
458*4882a593Smuzhiyun 		 * Cut down Resend / Check_IPI / IPI
459*4882a593Smuzhiyun 		 *
460*4882a593Smuzhiyun 		 * The logic is that we cannot have a pending interrupt
461*4882a593Smuzhiyun 		 * trumped by an IPI at this point (see above), so we
462*4882a593Smuzhiyun 		 * know that either the pending interrupt is already an
463*4882a593Smuzhiyun 		 * IPI (in which case we don't care to override it) or
464*4882a593Smuzhiyun 		 * it's either more favored than us or non existent
465*4882a593Smuzhiyun 		 */
466*4882a593Smuzhiyun 		if (new_state.mfrr < new_cppr &&
467*4882a593Smuzhiyun 		    new_state.mfrr <= new_state.pending_pri) {
468*4882a593Smuzhiyun 			new_state.pending_pri = new_state.mfrr;
469*4882a593Smuzhiyun 			new_state.xisr = XICS_IPI;
470*4882a593Smuzhiyun 		}
471*4882a593Smuzhiyun 
472*4882a593Smuzhiyun 		/* Latch/clear resend bit */
473*4882a593Smuzhiyun 		resend = new_state.need_resend;
474*4882a593Smuzhiyun 		new_state.need_resend = 0;
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun 	} while (!icp_rm_try_update(icp, old_state, new_state));
477*4882a593Smuzhiyun 
478*4882a593Smuzhiyun 	/*
479*4882a593Smuzhiyun 	 * Now handle resend checks. Those are asynchronous to the ICP
480*4882a593Smuzhiyun 	 * state update in HW (ie bus transactions) so we can handle them
481*4882a593Smuzhiyun 	 * separately here as well.
482*4882a593Smuzhiyun 	 */
483*4882a593Smuzhiyun 	if (resend) {
484*4882a593Smuzhiyun 		icp->n_check_resend++;
485*4882a593Smuzhiyun 		icp_rm_check_resend(xics, icp);
486*4882a593Smuzhiyun 	}
487*4882a593Smuzhiyun }
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun 
xics_rm_h_xirr(struct kvm_vcpu * vcpu)490*4882a593Smuzhiyun unsigned long xics_rm_h_xirr(struct kvm_vcpu *vcpu)
491*4882a593Smuzhiyun {
492*4882a593Smuzhiyun 	union kvmppc_icp_state old_state, new_state;
493*4882a593Smuzhiyun 	struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
494*4882a593Smuzhiyun 	struct kvmppc_icp *icp = vcpu->arch.icp;
495*4882a593Smuzhiyun 	u32 xirr;
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun 	if (!xics || !xics->real_mode)
498*4882a593Smuzhiyun 		return H_TOO_HARD;
499*4882a593Smuzhiyun 
500*4882a593Smuzhiyun 	/* First clear the interrupt */
501*4882a593Smuzhiyun 	icp_rm_clr_vcpu_irq(icp->vcpu);
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun 	/*
504*4882a593Smuzhiyun 	 * ICP State: Accept_Interrupt
505*4882a593Smuzhiyun 	 *
506*4882a593Smuzhiyun 	 * Return the pending interrupt (if any) along with the
507*4882a593Smuzhiyun 	 * current CPPR, then clear the XISR & set CPPR to the
508*4882a593Smuzhiyun 	 * pending priority
509*4882a593Smuzhiyun 	 */
510*4882a593Smuzhiyun 	do {
511*4882a593Smuzhiyun 		old_state = new_state = READ_ONCE(icp->state);
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun 		xirr = old_state.xisr | (((u32)old_state.cppr) << 24);
514*4882a593Smuzhiyun 		if (!old_state.xisr)
515*4882a593Smuzhiyun 			break;
516*4882a593Smuzhiyun 		new_state.cppr = new_state.pending_pri;
517*4882a593Smuzhiyun 		new_state.pending_pri = 0xff;
518*4882a593Smuzhiyun 		new_state.xisr = 0;
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun 	} while (!icp_rm_try_update(icp, old_state, new_state));
521*4882a593Smuzhiyun 
522*4882a593Smuzhiyun 	/* Return the result in GPR4 */
523*4882a593Smuzhiyun 	vcpu->arch.regs.gpr[4] = xirr;
524*4882a593Smuzhiyun 
525*4882a593Smuzhiyun 	return check_too_hard(xics, icp);
526*4882a593Smuzhiyun }
527*4882a593Smuzhiyun 
xics_rm_h_ipi(struct kvm_vcpu * vcpu,unsigned long server,unsigned long mfrr)528*4882a593Smuzhiyun int xics_rm_h_ipi(struct kvm_vcpu *vcpu, unsigned long server,
529*4882a593Smuzhiyun 		  unsigned long mfrr)
530*4882a593Smuzhiyun {
531*4882a593Smuzhiyun 	union kvmppc_icp_state old_state, new_state;
532*4882a593Smuzhiyun 	struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
533*4882a593Smuzhiyun 	struct kvmppc_icp *icp, *this_icp = vcpu->arch.icp;
534*4882a593Smuzhiyun 	u32 reject;
535*4882a593Smuzhiyun 	bool resend;
536*4882a593Smuzhiyun 	bool local;
537*4882a593Smuzhiyun 
538*4882a593Smuzhiyun 	if (!xics || !xics->real_mode)
539*4882a593Smuzhiyun 		return H_TOO_HARD;
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun 	local = this_icp->server_num == server;
542*4882a593Smuzhiyun 	if (local)
543*4882a593Smuzhiyun 		icp = this_icp;
544*4882a593Smuzhiyun 	else
545*4882a593Smuzhiyun 		icp = kvmppc_xics_find_server(vcpu->kvm, server);
546*4882a593Smuzhiyun 	if (!icp)
547*4882a593Smuzhiyun 		return H_PARAMETER;
548*4882a593Smuzhiyun 
549*4882a593Smuzhiyun 	/*
550*4882a593Smuzhiyun 	 * ICP state: Set_MFRR
551*4882a593Smuzhiyun 	 *
552*4882a593Smuzhiyun 	 * If the CPPR is more favored than the new MFRR, then
553*4882a593Smuzhiyun 	 * nothing needs to be done as there can be no XISR to
554*4882a593Smuzhiyun 	 * reject.
555*4882a593Smuzhiyun 	 *
556*4882a593Smuzhiyun 	 * ICP state: Check_IPI
557*4882a593Smuzhiyun 	 *
558*4882a593Smuzhiyun 	 * If the CPPR is less favored, then we might be replacing
559*4882a593Smuzhiyun 	 * an interrupt, and thus need to possibly reject it.
560*4882a593Smuzhiyun 	 *
561*4882a593Smuzhiyun 	 * ICP State: IPI
562*4882a593Smuzhiyun 	 *
563*4882a593Smuzhiyun 	 * Besides rejecting any pending interrupts, we also
564*4882a593Smuzhiyun 	 * update XISR and pending_pri to mark IPI as pending.
565*4882a593Smuzhiyun 	 *
566*4882a593Smuzhiyun 	 * PAPR does not describe this state, but if the MFRR is being
567*4882a593Smuzhiyun 	 * made less favored than its earlier value, there might be
568*4882a593Smuzhiyun 	 * a previously-rejected interrupt needing to be resent.
569*4882a593Smuzhiyun 	 * Ideally, we would want to resend only if
570*4882a593Smuzhiyun 	 *	prio(pending_interrupt) < mfrr &&
571*4882a593Smuzhiyun 	 *	prio(pending_interrupt) < cppr
572*4882a593Smuzhiyun 	 * where pending interrupt is the one that was rejected. But
573*4882a593Smuzhiyun 	 * we don't have that state, so we simply trigger a resend
574*4882a593Smuzhiyun 	 * whenever the MFRR is made less favored.
575*4882a593Smuzhiyun 	 */
576*4882a593Smuzhiyun 	do {
577*4882a593Smuzhiyun 		old_state = new_state = READ_ONCE(icp->state);
578*4882a593Smuzhiyun 
579*4882a593Smuzhiyun 		/* Set_MFRR */
580*4882a593Smuzhiyun 		new_state.mfrr = mfrr;
581*4882a593Smuzhiyun 
582*4882a593Smuzhiyun 		/* Check_IPI */
583*4882a593Smuzhiyun 		reject = 0;
584*4882a593Smuzhiyun 		resend = false;
585*4882a593Smuzhiyun 		if (mfrr < new_state.cppr) {
586*4882a593Smuzhiyun 			/* Reject a pending interrupt if not an IPI */
587*4882a593Smuzhiyun 			if (mfrr <= new_state.pending_pri) {
588*4882a593Smuzhiyun 				reject = new_state.xisr;
589*4882a593Smuzhiyun 				new_state.pending_pri = mfrr;
590*4882a593Smuzhiyun 				new_state.xisr = XICS_IPI;
591*4882a593Smuzhiyun 			}
592*4882a593Smuzhiyun 		}
593*4882a593Smuzhiyun 
594*4882a593Smuzhiyun 		if (mfrr > old_state.mfrr) {
595*4882a593Smuzhiyun 			resend = new_state.need_resend;
596*4882a593Smuzhiyun 			new_state.need_resend = 0;
597*4882a593Smuzhiyun 		}
598*4882a593Smuzhiyun 	} while (!icp_rm_try_update(icp, old_state, new_state));
599*4882a593Smuzhiyun 
600*4882a593Smuzhiyun 	/* Handle reject in real mode */
601*4882a593Smuzhiyun 	if (reject && reject != XICS_IPI) {
602*4882a593Smuzhiyun 		this_icp->n_reject++;
603*4882a593Smuzhiyun 		icp_rm_deliver_irq(xics, icp, reject, false);
604*4882a593Smuzhiyun 	}
605*4882a593Smuzhiyun 
606*4882a593Smuzhiyun 	/* Handle resends in real mode */
607*4882a593Smuzhiyun 	if (resend) {
608*4882a593Smuzhiyun 		this_icp->n_check_resend++;
609*4882a593Smuzhiyun 		icp_rm_check_resend(xics, icp);
610*4882a593Smuzhiyun 	}
611*4882a593Smuzhiyun 
612*4882a593Smuzhiyun 	return check_too_hard(xics, this_icp);
613*4882a593Smuzhiyun }
614*4882a593Smuzhiyun 
xics_rm_h_cppr(struct kvm_vcpu * vcpu,unsigned long cppr)615*4882a593Smuzhiyun int xics_rm_h_cppr(struct kvm_vcpu *vcpu, unsigned long cppr)
616*4882a593Smuzhiyun {
617*4882a593Smuzhiyun 	union kvmppc_icp_state old_state, new_state;
618*4882a593Smuzhiyun 	struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
619*4882a593Smuzhiyun 	struct kvmppc_icp *icp = vcpu->arch.icp;
620*4882a593Smuzhiyun 	u32 reject;
621*4882a593Smuzhiyun 
622*4882a593Smuzhiyun 	if (!xics || !xics->real_mode)
623*4882a593Smuzhiyun 		return H_TOO_HARD;
624*4882a593Smuzhiyun 
625*4882a593Smuzhiyun 	/*
626*4882a593Smuzhiyun 	 * ICP State: Set_CPPR
627*4882a593Smuzhiyun 	 *
628*4882a593Smuzhiyun 	 * We can safely compare the new value with the current
629*4882a593Smuzhiyun 	 * value outside of the transaction as the CPPR is only
630*4882a593Smuzhiyun 	 * ever changed by the processor on itself
631*4882a593Smuzhiyun 	 */
632*4882a593Smuzhiyun 	if (cppr > icp->state.cppr) {
633*4882a593Smuzhiyun 		icp_rm_down_cppr(xics, icp, cppr);
634*4882a593Smuzhiyun 		goto bail;
635*4882a593Smuzhiyun 	} else if (cppr == icp->state.cppr)
636*4882a593Smuzhiyun 		return H_SUCCESS;
637*4882a593Smuzhiyun 
638*4882a593Smuzhiyun 	/*
639*4882a593Smuzhiyun 	 * ICP State: Up_CPPR
640*4882a593Smuzhiyun 	 *
641*4882a593Smuzhiyun 	 * The processor is raising its priority, this can result
642*4882a593Smuzhiyun 	 * in a rejection of a pending interrupt:
643*4882a593Smuzhiyun 	 *
644*4882a593Smuzhiyun 	 * ICP State: Reject_Current
645*4882a593Smuzhiyun 	 *
646*4882a593Smuzhiyun 	 * We can remove EE from the current processor, the update
647*4882a593Smuzhiyun 	 * transaction will set it again if needed
648*4882a593Smuzhiyun 	 */
649*4882a593Smuzhiyun 	icp_rm_clr_vcpu_irq(icp->vcpu);
650*4882a593Smuzhiyun 
651*4882a593Smuzhiyun 	do {
652*4882a593Smuzhiyun 		old_state = new_state = READ_ONCE(icp->state);
653*4882a593Smuzhiyun 
654*4882a593Smuzhiyun 		reject = 0;
655*4882a593Smuzhiyun 		new_state.cppr = cppr;
656*4882a593Smuzhiyun 
657*4882a593Smuzhiyun 		if (cppr <= new_state.pending_pri) {
658*4882a593Smuzhiyun 			reject = new_state.xisr;
659*4882a593Smuzhiyun 			new_state.xisr = 0;
660*4882a593Smuzhiyun 			new_state.pending_pri = 0xff;
661*4882a593Smuzhiyun 		}
662*4882a593Smuzhiyun 
663*4882a593Smuzhiyun 	} while (!icp_rm_try_update(icp, old_state, new_state));
664*4882a593Smuzhiyun 
665*4882a593Smuzhiyun 	/*
666*4882a593Smuzhiyun 	 * Check for rejects. They are handled by doing a new delivery
667*4882a593Smuzhiyun 	 * attempt (see comments in icp_rm_deliver_irq).
668*4882a593Smuzhiyun 	 */
669*4882a593Smuzhiyun 	if (reject && reject != XICS_IPI) {
670*4882a593Smuzhiyun 		icp->n_reject++;
671*4882a593Smuzhiyun 		icp_rm_deliver_irq(xics, icp, reject, false);
672*4882a593Smuzhiyun 	}
673*4882a593Smuzhiyun  bail:
674*4882a593Smuzhiyun 	return check_too_hard(xics, icp);
675*4882a593Smuzhiyun }
676*4882a593Smuzhiyun 
ics_rm_eoi(struct kvm_vcpu * vcpu,u32 irq)677*4882a593Smuzhiyun static int ics_rm_eoi(struct kvm_vcpu *vcpu, u32 irq)
678*4882a593Smuzhiyun {
679*4882a593Smuzhiyun 	struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
680*4882a593Smuzhiyun 	struct kvmppc_icp *icp = vcpu->arch.icp;
681*4882a593Smuzhiyun 	struct kvmppc_ics *ics;
682*4882a593Smuzhiyun 	struct ics_irq_state *state;
683*4882a593Smuzhiyun 	u16 src;
684*4882a593Smuzhiyun 	u32 pq_old, pq_new;
685*4882a593Smuzhiyun 
686*4882a593Smuzhiyun 	/*
687*4882a593Smuzhiyun 	 * ICS EOI handling: For LSI, if P bit is still set, we need to
688*4882a593Smuzhiyun 	 * resend it.
689*4882a593Smuzhiyun 	 *
690*4882a593Smuzhiyun 	 * For MSI, we move Q bit into P (and clear Q). If it is set,
691*4882a593Smuzhiyun 	 * resend it.
692*4882a593Smuzhiyun 	 */
693*4882a593Smuzhiyun 
694*4882a593Smuzhiyun 	ics = kvmppc_xics_find_ics(xics, irq, &src);
695*4882a593Smuzhiyun 	if (!ics)
696*4882a593Smuzhiyun 		goto bail;
697*4882a593Smuzhiyun 
698*4882a593Smuzhiyun 	state = &ics->irq_state[src];
699*4882a593Smuzhiyun 
700*4882a593Smuzhiyun 	if (state->lsi)
701*4882a593Smuzhiyun 		pq_new = state->pq_state;
702*4882a593Smuzhiyun 	else
703*4882a593Smuzhiyun 		do {
704*4882a593Smuzhiyun 			pq_old = state->pq_state;
705*4882a593Smuzhiyun 			pq_new = pq_old >> 1;
706*4882a593Smuzhiyun 		} while (cmpxchg(&state->pq_state, pq_old, pq_new) != pq_old);
707*4882a593Smuzhiyun 
708*4882a593Smuzhiyun 	if (pq_new & PQ_PRESENTED)
709*4882a593Smuzhiyun 		icp_rm_deliver_irq(xics, NULL, irq, false);
710*4882a593Smuzhiyun 
711*4882a593Smuzhiyun 	if (!hlist_empty(&vcpu->kvm->irq_ack_notifier_list)) {
712*4882a593Smuzhiyun 		icp->rm_action |= XICS_RM_NOTIFY_EOI;
713*4882a593Smuzhiyun 		icp->rm_eoied_irq = irq;
714*4882a593Smuzhiyun 	}
715*4882a593Smuzhiyun 
716*4882a593Smuzhiyun 	if (state->host_irq) {
717*4882a593Smuzhiyun 		++vcpu->stat.pthru_all;
718*4882a593Smuzhiyun 		if (state->intr_cpu != -1) {
719*4882a593Smuzhiyun 			int pcpu = raw_smp_processor_id();
720*4882a593Smuzhiyun 
721*4882a593Smuzhiyun 			pcpu = cpu_first_thread_sibling(pcpu);
722*4882a593Smuzhiyun 			++vcpu->stat.pthru_host;
723*4882a593Smuzhiyun 			if (state->intr_cpu != pcpu) {
724*4882a593Smuzhiyun 				++vcpu->stat.pthru_bad_aff;
725*4882a593Smuzhiyun 				xics_opal_set_server(state->host_irq, pcpu);
726*4882a593Smuzhiyun 			}
727*4882a593Smuzhiyun 			state->intr_cpu = -1;
728*4882a593Smuzhiyun 		}
729*4882a593Smuzhiyun 	}
730*4882a593Smuzhiyun 
731*4882a593Smuzhiyun  bail:
732*4882a593Smuzhiyun 	return check_too_hard(xics, icp);
733*4882a593Smuzhiyun }
734*4882a593Smuzhiyun 
xics_rm_h_eoi(struct kvm_vcpu * vcpu,unsigned long xirr)735*4882a593Smuzhiyun int xics_rm_h_eoi(struct kvm_vcpu *vcpu, unsigned long xirr)
736*4882a593Smuzhiyun {
737*4882a593Smuzhiyun 	struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
738*4882a593Smuzhiyun 	struct kvmppc_icp *icp = vcpu->arch.icp;
739*4882a593Smuzhiyun 	u32 irq = xirr & 0x00ffffff;
740*4882a593Smuzhiyun 
741*4882a593Smuzhiyun 	if (!xics || !xics->real_mode)
742*4882a593Smuzhiyun 		return H_TOO_HARD;
743*4882a593Smuzhiyun 
744*4882a593Smuzhiyun 	/*
745*4882a593Smuzhiyun 	 * ICP State: EOI
746*4882a593Smuzhiyun 	 *
747*4882a593Smuzhiyun 	 * Note: If EOI is incorrectly used by SW to lower the CPPR
748*4882a593Smuzhiyun 	 * value (ie more favored), we do not check for rejection of
749*4882a593Smuzhiyun 	 * a pending interrupt, this is a SW error and PAPR specifies
750*4882a593Smuzhiyun 	 * that we don't have to deal with it.
751*4882a593Smuzhiyun 	 *
752*4882a593Smuzhiyun 	 * The sending of an EOI to the ICS is handled after the
753*4882a593Smuzhiyun 	 * CPPR update
754*4882a593Smuzhiyun 	 *
755*4882a593Smuzhiyun 	 * ICP State: Down_CPPR which we handle
756*4882a593Smuzhiyun 	 * in a separate function as it's shared with H_CPPR.
757*4882a593Smuzhiyun 	 */
758*4882a593Smuzhiyun 	icp_rm_down_cppr(xics, icp, xirr >> 24);
759*4882a593Smuzhiyun 
760*4882a593Smuzhiyun 	/* IPIs have no EOI */
761*4882a593Smuzhiyun 	if (irq == XICS_IPI)
762*4882a593Smuzhiyun 		return check_too_hard(xics, icp);
763*4882a593Smuzhiyun 
764*4882a593Smuzhiyun 	return ics_rm_eoi(vcpu, irq);
765*4882a593Smuzhiyun }
766*4882a593Smuzhiyun 
767*4882a593Smuzhiyun static unsigned long eoi_rc;
768*4882a593Smuzhiyun 
icp_eoi(struct irq_chip * c,u32 hwirq,__be32 xirr,bool * again)769*4882a593Smuzhiyun static void icp_eoi(struct irq_chip *c, u32 hwirq, __be32 xirr, bool *again)
770*4882a593Smuzhiyun {
771*4882a593Smuzhiyun 	void __iomem *xics_phys;
772*4882a593Smuzhiyun 	int64_t rc;
773*4882a593Smuzhiyun 
774*4882a593Smuzhiyun 	if (kvmhv_on_pseries()) {
775*4882a593Smuzhiyun 		unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
776*4882a593Smuzhiyun 
777*4882a593Smuzhiyun 		iosync();
778*4882a593Smuzhiyun 		plpar_hcall_raw(H_EOI, retbuf, hwirq);
779*4882a593Smuzhiyun 		return;
780*4882a593Smuzhiyun 	}
781*4882a593Smuzhiyun 
782*4882a593Smuzhiyun 	rc = pnv_opal_pci_msi_eoi(c, hwirq);
783*4882a593Smuzhiyun 
784*4882a593Smuzhiyun 	if (rc)
785*4882a593Smuzhiyun 		eoi_rc = rc;
786*4882a593Smuzhiyun 
787*4882a593Smuzhiyun 	iosync();
788*4882a593Smuzhiyun 
789*4882a593Smuzhiyun 	/* EOI it */
790*4882a593Smuzhiyun 	xics_phys = local_paca->kvm_hstate.xics_phys;
791*4882a593Smuzhiyun 	if (xics_phys) {
792*4882a593Smuzhiyun 		__raw_rm_writel(xirr, xics_phys + XICS_XIRR);
793*4882a593Smuzhiyun 	} else {
794*4882a593Smuzhiyun 		rc = opal_int_eoi(be32_to_cpu(xirr));
795*4882a593Smuzhiyun 		*again = rc > 0;
796*4882a593Smuzhiyun 	}
797*4882a593Smuzhiyun }
798*4882a593Smuzhiyun 
xics_opal_set_server(unsigned int hw_irq,int server_cpu)799*4882a593Smuzhiyun static int xics_opal_set_server(unsigned int hw_irq, int server_cpu)
800*4882a593Smuzhiyun {
801*4882a593Smuzhiyun 	unsigned int mangle_cpu = get_hard_smp_processor_id(server_cpu) << 2;
802*4882a593Smuzhiyun 
803*4882a593Smuzhiyun 	return opal_set_xive(hw_irq, mangle_cpu, DEFAULT_PRIORITY);
804*4882a593Smuzhiyun }
805*4882a593Smuzhiyun 
806*4882a593Smuzhiyun /*
807*4882a593Smuzhiyun  * Increment a per-CPU 32-bit unsigned integer variable.
808*4882a593Smuzhiyun  * Safe to call in real-mode. Handles vmalloc'ed addresses
809*4882a593Smuzhiyun  *
810*4882a593Smuzhiyun  * ToDo: Make this work for any integral type
811*4882a593Smuzhiyun  */
812*4882a593Smuzhiyun 
this_cpu_inc_rm(unsigned int __percpu * addr)813*4882a593Smuzhiyun static inline void this_cpu_inc_rm(unsigned int __percpu *addr)
814*4882a593Smuzhiyun {
815*4882a593Smuzhiyun 	unsigned long l;
816*4882a593Smuzhiyun 	unsigned int *raddr;
817*4882a593Smuzhiyun 	int cpu = smp_processor_id();
818*4882a593Smuzhiyun 
819*4882a593Smuzhiyun 	raddr = per_cpu_ptr(addr, cpu);
820*4882a593Smuzhiyun 	l = (unsigned long)raddr;
821*4882a593Smuzhiyun 
822*4882a593Smuzhiyun 	if (get_region_id(l) == VMALLOC_REGION_ID) {
823*4882a593Smuzhiyun 		l = vmalloc_to_phys(raddr);
824*4882a593Smuzhiyun 		raddr = (unsigned int *)l;
825*4882a593Smuzhiyun 	}
826*4882a593Smuzhiyun 	++*raddr;
827*4882a593Smuzhiyun }
828*4882a593Smuzhiyun 
829*4882a593Smuzhiyun /*
830*4882a593Smuzhiyun  * We don't try to update the flags in the irq_desc 'istate' field in
831*4882a593Smuzhiyun  * here as would happen in the normal IRQ handling path for several reasons:
832*4882a593Smuzhiyun  *  - state flags represent internal IRQ state and are not expected to be
833*4882a593Smuzhiyun  *    updated outside the IRQ subsystem
834*4882a593Smuzhiyun  *  - more importantly, these are useful for edge triggered interrupts,
835*4882a593Smuzhiyun  *    IRQ probing, etc., but we are only handling MSI/MSIx interrupts here
836*4882a593Smuzhiyun  *    and these states shouldn't apply to us.
837*4882a593Smuzhiyun  *
838*4882a593Smuzhiyun  * However, we do update irq_stats - we somewhat duplicate the code in
839*4882a593Smuzhiyun  * kstat_incr_irqs_this_cpu() for this since this function is defined
840*4882a593Smuzhiyun  * in irq/internal.h which we don't want to include here.
841*4882a593Smuzhiyun  * The only difference is that desc->kstat_irqs is an allocated per CPU
842*4882a593Smuzhiyun  * variable and could have been vmalloc'ed, so we can't directly
843*4882a593Smuzhiyun  * call __this_cpu_inc() on it. The kstat structure is a static
844*4882a593Smuzhiyun  * per CPU variable and it should be accessible by real-mode KVM.
845*4882a593Smuzhiyun  *
846*4882a593Smuzhiyun  */
kvmppc_rm_handle_irq_desc(struct irq_desc * desc)847*4882a593Smuzhiyun static void kvmppc_rm_handle_irq_desc(struct irq_desc *desc)
848*4882a593Smuzhiyun {
849*4882a593Smuzhiyun 	this_cpu_inc_rm(desc->kstat_irqs);
850*4882a593Smuzhiyun 	__this_cpu_inc(kstat.irqs_sum);
851*4882a593Smuzhiyun }
852*4882a593Smuzhiyun 
kvmppc_deliver_irq_passthru(struct kvm_vcpu * vcpu,__be32 xirr,struct kvmppc_irq_map * irq_map,struct kvmppc_passthru_irqmap * pimap,bool * again)853*4882a593Smuzhiyun long kvmppc_deliver_irq_passthru(struct kvm_vcpu *vcpu,
854*4882a593Smuzhiyun 				 __be32 xirr,
855*4882a593Smuzhiyun 				 struct kvmppc_irq_map *irq_map,
856*4882a593Smuzhiyun 				 struct kvmppc_passthru_irqmap *pimap,
857*4882a593Smuzhiyun 				 bool *again)
858*4882a593Smuzhiyun {
859*4882a593Smuzhiyun 	struct kvmppc_xics *xics;
860*4882a593Smuzhiyun 	struct kvmppc_icp *icp;
861*4882a593Smuzhiyun 	struct kvmppc_ics *ics;
862*4882a593Smuzhiyun 	struct ics_irq_state *state;
863*4882a593Smuzhiyun 	u32 irq;
864*4882a593Smuzhiyun 	u16 src;
865*4882a593Smuzhiyun 	u32 pq_old, pq_new;
866*4882a593Smuzhiyun 
867*4882a593Smuzhiyun 	irq = irq_map->v_hwirq;
868*4882a593Smuzhiyun 	xics = vcpu->kvm->arch.xics;
869*4882a593Smuzhiyun 	icp = vcpu->arch.icp;
870*4882a593Smuzhiyun 
871*4882a593Smuzhiyun 	kvmppc_rm_handle_irq_desc(irq_map->desc);
872*4882a593Smuzhiyun 
873*4882a593Smuzhiyun 	ics = kvmppc_xics_find_ics(xics, irq, &src);
874*4882a593Smuzhiyun 	if (!ics)
875*4882a593Smuzhiyun 		return 2;
876*4882a593Smuzhiyun 
877*4882a593Smuzhiyun 	state = &ics->irq_state[src];
878*4882a593Smuzhiyun 
879*4882a593Smuzhiyun 	/* only MSIs register bypass producers, so it must be MSI here */
880*4882a593Smuzhiyun 	do {
881*4882a593Smuzhiyun 		pq_old = state->pq_state;
882*4882a593Smuzhiyun 		pq_new = ((pq_old << 1) & 3) | PQ_PRESENTED;
883*4882a593Smuzhiyun 	} while (cmpxchg(&state->pq_state, pq_old, pq_new) != pq_old);
884*4882a593Smuzhiyun 
885*4882a593Smuzhiyun 	/* Test P=1, Q=0, this is the only case where we present */
886*4882a593Smuzhiyun 	if (pq_new == PQ_PRESENTED)
887*4882a593Smuzhiyun 		icp_rm_deliver_irq(xics, icp, irq, false);
888*4882a593Smuzhiyun 
889*4882a593Smuzhiyun 	/* EOI the interrupt */
890*4882a593Smuzhiyun 	icp_eoi(irq_desc_get_chip(irq_map->desc), irq_map->r_hwirq, xirr,
891*4882a593Smuzhiyun 		again);
892*4882a593Smuzhiyun 
893*4882a593Smuzhiyun 	if (check_too_hard(xics, icp) == H_TOO_HARD)
894*4882a593Smuzhiyun 		return 2;
895*4882a593Smuzhiyun 	else
896*4882a593Smuzhiyun 		return -2;
897*4882a593Smuzhiyun }
898*4882a593Smuzhiyun 
899*4882a593Smuzhiyun /*  --- Non-real mode XICS-related built-in routines ---  */
900*4882a593Smuzhiyun 
901*4882a593Smuzhiyun /**
902*4882a593Smuzhiyun  * Host Operations poked by RM KVM
903*4882a593Smuzhiyun  */
rm_host_ipi_action(int action,void * data)904*4882a593Smuzhiyun static void rm_host_ipi_action(int action, void *data)
905*4882a593Smuzhiyun {
906*4882a593Smuzhiyun 	switch (action) {
907*4882a593Smuzhiyun 	case XICS_RM_KICK_VCPU:
908*4882a593Smuzhiyun 		kvmppc_host_rm_ops_hv->vcpu_kick(data);
909*4882a593Smuzhiyun 		break;
910*4882a593Smuzhiyun 	default:
911*4882a593Smuzhiyun 		WARN(1, "Unexpected rm_action=%d data=%p\n", action, data);
912*4882a593Smuzhiyun 		break;
913*4882a593Smuzhiyun 	}
914*4882a593Smuzhiyun 
915*4882a593Smuzhiyun }
916*4882a593Smuzhiyun 
kvmppc_xics_ipi_action(void)917*4882a593Smuzhiyun void kvmppc_xics_ipi_action(void)
918*4882a593Smuzhiyun {
919*4882a593Smuzhiyun 	int core;
920*4882a593Smuzhiyun 	unsigned int cpu = smp_processor_id();
921*4882a593Smuzhiyun 	struct kvmppc_host_rm_core *rm_corep;
922*4882a593Smuzhiyun 
923*4882a593Smuzhiyun 	core = cpu >> threads_shift;
924*4882a593Smuzhiyun 	rm_corep = &kvmppc_host_rm_ops_hv->rm_core[core];
925*4882a593Smuzhiyun 
926*4882a593Smuzhiyun 	if (rm_corep->rm_data) {
927*4882a593Smuzhiyun 		rm_host_ipi_action(rm_corep->rm_state.rm_action,
928*4882a593Smuzhiyun 							rm_corep->rm_data);
929*4882a593Smuzhiyun 		/* Order these stores against the real mode KVM */
930*4882a593Smuzhiyun 		rm_corep->rm_data = NULL;
931*4882a593Smuzhiyun 		smp_wmb();
932*4882a593Smuzhiyun 		rm_corep->rm_state.rm_action = 0;
933*4882a593Smuzhiyun 	}
934*4882a593Smuzhiyun }
935