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