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