2 * Copyright 2012 Michael Ellerman, IBM Corporation.
3 * Copyright 2012 Benjamin Herrenschmidt, IBM Corporation
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.
10 #include <linux/kernel.h>
11 #include <linux/kvm_host.h>
12 #include <linux/err.h>
13 #include <linux/kernel_stat.h>
15 #include <asm/kvm_book3s.h>
16 #include <asm/kvm_ppc.h>
17 #include <asm/hvcall.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>
28 #include "book3s_xics.h"
32 int h_ipi_redirect = 1;
33 EXPORT_SYMBOL(h_ipi_redirect);
34 int kvm_irq_bypass = 1;
35 EXPORT_SYMBOL(kvm_irq_bypass);
37 static void icp_rm_deliver_irq(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
39 static int xics_opal_rm_set_server(unsigned int hw_irq, int server_cpu);
41 /* -- ICS routines -- */
42 static void ics_rm_check_resend(struct kvmppc_xics *xics,
43 struct kvmppc_ics *ics, struct kvmppc_icp *icp)
47 arch_spin_lock(&ics->lock);
49 for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
50 struct ics_irq_state *state = &ics->irq_state[i];
55 arch_spin_unlock(&ics->lock);
56 icp_rm_deliver_irq(xics, icp, state->number);
57 arch_spin_lock(&ics->lock);
60 arch_spin_unlock(&ics->lock);
63 /* -- ICP routines -- */
66 static inline void icp_send_hcore_msg(int hcore, struct kvm_vcpu *vcpu)
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);
76 static inline void icp_send_hcore_msg(int hcore, struct kvm_vcpu *vcpu) { }
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.
85 * In the future, could consider using a fairer algorithm (one
86 * that distributes the IPIs better)
88 * Returns -1, if no CPU could be found in the host
89 * Else, returns a CPU Id which has been reserved for use
91 static inline int grab_next_hostcore(int start,
92 struct kvmppc_host_rm_core *rm_core, int max, int action)
96 union kvmppc_rm_state old, new;
98 for (core = start + 1; core < max; core++) {
99 old = new = READ_ONCE(rm_core[core].rm_state);
101 if (!old.in_host || old.rm_action)
104 /* Try to grab this host core if not taken already. */
105 new.rm_action = action;
107 success = cmpxchg64(&rm_core[core].rm_state.raw,
108 old.raw, new.raw) == old.raw;
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
124 static inline int find_available_hostcore(int action)
127 int my_core = smp_processor_id() >> threads_shift;
128 struct kvmppc_host_rm_core *rm_core = kvmppc_host_rm_ops_hv->rm_core;
130 core = grab_next_hostcore(my_core, rm_core, cpu_nr_cores(), action);
132 core = grab_next_hostcore(core, rm_core, my_core, action);
137 static void icp_rm_set_vcpu_irq(struct kvm_vcpu *vcpu,
138 struct kvm_vcpu *this_vcpu)
140 struct kvmppc_icp *this_icp = this_vcpu->arch.icp;
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);
148 /* Kick self ? Just set MER and return */
149 if (vcpu == this_vcpu) {
150 mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) | LPCR_MER);
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.
159 cpu = vcpu->arch.thread_cpu;
160 if (cpu < 0 || cpu >= nr_cpu_ids) {
162 if (kvmppc_host_rm_ops_hv && h_ipi_redirect)
163 hcore = find_available_hostcore(XICS_RM_KICK_VCPU);
165 icp_send_hcore_msg(hcore, vcpu);
167 this_icp->rm_action |= XICS_RM_KICK_VCPU;
168 this_icp->rm_kick_target = vcpu;
174 kvmhv_rm_send_ipi(cpu);
177 static void icp_rm_clr_vcpu_irq(struct kvm_vcpu *vcpu)
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);
185 static inline bool icp_rm_try_update(struct kvmppc_icp *icp,
186 union kvmppc_icp_state old,
187 union kvmppc_icp_state new)
189 struct kvm_vcpu *this_vcpu = local_paca->kvm_hstate.kvm_vcpu;
192 /* Calculate new output value */
193 new.out_ee = (new.xisr && (new.pending_pri < new.cppr));
195 /* Attempt atomic update */
196 success = cmpxchg64(&icp->state.raw, old.raw, new.raw) == old.raw;
201 * Check for output state update
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).
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.
216 icp_rm_set_vcpu_irq(icp->vcpu, this_vcpu);
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;
226 static inline int check_too_hard(struct kvmppc_xics *xics,
227 struct kvmppc_icp *icp)
229 return (xics->real_mode_dbg || icp->rm_action) ? H_TOO_HARD : H_SUCCESS;
232 static void icp_rm_check_resend(struct kvmppc_xics *xics,
233 struct kvmppc_icp *icp)
237 /* Order this load with the test for need_resend in the caller */
239 for_each_set_bit(icsid, icp->resend_map, xics->max_icsid + 1) {
240 struct kvmppc_ics *ics = xics->ics[icsid];
242 if (!test_and_clear_bit(icsid, icp->resend_map))
246 ics_rm_check_resend(xics, ics, icp);
250 static bool icp_rm_try_to_deliver(struct kvmppc_icp *icp, u32 irq, u8 priority,
253 union kvmppc_icp_state old_state, new_state;
257 old_state = new_state = READ_ONCE(icp->state);
261 /* See if we can deliver */
262 success = new_state.cppr > priority &&
263 new_state.mfrr > priority &&
264 new_state.pending_pri > priority;
267 * If we can, check for a rejection and perform the
271 *reject = new_state.xisr;
272 new_state.xisr = irq;
273 new_state.pending_pri = priority;
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.
280 new_state.need_resend = true;
283 } while (!icp_rm_try_update(icp, old_state, new_state));
288 static void icp_rm_deliver_irq(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
291 struct ics_irq_state *state;
292 struct kvmppc_ics *ics;
297 * This is used both for initial delivery of an interrupt and
298 * for subsequent rejection.
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.
304 * So we need to retry the delivery. Essentially the reject path
305 * boils down to a failed delivery. Always.
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
312 /* Get the ICS state and lock it */
313 ics = kvmppc_xics_find_ics(xics, new_irq, &src);
315 /* Unsafe increment, but this does not need to be accurate */
319 state = &ics->irq_state[src];
321 /* Get a lock on the ICS */
322 arch_spin_lock(&ics->lock);
325 if (!icp || state->server != icp->server_num) {
326 icp = kvmppc_xics_find_server(xics->kvm, state->server);
328 /* Unsafe increment again*/
334 /* Clear the resend bit of that interrupt */
338 * If masked, bail out
340 * Note: PAPR doesn't mention anything about masked pending
341 * when doing a resend, only when doing a delivery.
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.
348 * I don't differentiate normal deliveries and resends, this
349 * implementation will differ from PAPR and not lose such
352 if (state->priority == MASKED) {
353 state->masked_pending = 1;
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.
362 * Note that if successful, the new delivery might have itself
363 * rejected an interrupt that was "delivered" before we took the
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.
373 if (icp_rm_try_to_deliver(icp, new_irq, state->priority, &reject)) {
375 * Delivery was successful, did we reject somebody else ?
377 if (reject && reject != XICS_IPI) {
378 arch_spin_unlock(&ics->lock);
385 * We failed to deliver the interrupt we need to set the
386 * resend map bit and mark the ICS state as needing a resend
388 set_bit(ics->icsid, icp->resend_map);
392 * If the need_resend flag got cleared in the ICP some time
393 * between icp_rm_try_to_deliver() atomic update and now, then
394 * we know it might have missed the resend_map bit. So we
398 if (!icp->state.need_resend) {
399 arch_spin_unlock(&ics->lock);
404 arch_spin_unlock(&ics->lock);
407 static void icp_rm_down_cppr(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
410 union kvmppc_icp_state old_state, new_state;
414 * This handles several related states in one operation:
416 * ICP State: Down_CPPR
418 * Load CPPR with new value and if the XISR is 0
419 * then check for resends:
423 * If MFRR is more favored than CPPR, check for IPIs
424 * and notify ICS of a potential resend. This is done
425 * asynchronously (when used in real mode, we will have
428 * We do not handle the complete Check_IPI as documented
429 * here. In the PAPR, this state will be used for both
430 * Set_MFRR and Down_CPPR. However, we know that we aren't
431 * changing the MFRR state here so we don't need to handle
432 * the case of an MFRR causing a reject of a pending irq,
433 * this will have been handled when the MFRR was set in the
436 * Thus we don't have to handle rejects, only resends.
438 * When implementing real mode for HV KVM, resend will lead to
439 * a H_TOO_HARD return and the whole transaction will be handled
443 old_state = new_state = READ_ONCE(icp->state);
446 new_state.cppr = new_cppr;
449 * Cut down Resend / Check_IPI / IPI
451 * The logic is that we cannot have a pending interrupt
452 * trumped by an IPI at this point (see above), so we
453 * know that either the pending interrupt is already an
454 * IPI (in which case we don't care to override it) or
455 * it's either more favored than us or non existent
457 if (new_state.mfrr < new_cppr &&
458 new_state.mfrr <= new_state.pending_pri) {
459 new_state.pending_pri = new_state.mfrr;
460 new_state.xisr = XICS_IPI;
463 /* Latch/clear resend bit */
464 resend = new_state.need_resend;
465 new_state.need_resend = 0;
467 } while (!icp_rm_try_update(icp, old_state, new_state));
470 * Now handle resend checks. Those are asynchronous to the ICP
471 * state update in HW (ie bus transactions) so we can handle them
472 * separately here as well.
475 icp->n_check_resend++;
476 icp_rm_check_resend(xics, icp);
481 unsigned long kvmppc_rm_h_xirr(struct kvm_vcpu *vcpu)
483 union kvmppc_icp_state old_state, new_state;
484 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
485 struct kvmppc_icp *icp = vcpu->arch.icp;
488 if (!xics || !xics->real_mode)
491 /* First clear the interrupt */
492 icp_rm_clr_vcpu_irq(icp->vcpu);
495 * ICP State: Accept_Interrupt
497 * Return the pending interrupt (if any) along with the
498 * current CPPR, then clear the XISR & set CPPR to the
502 old_state = new_state = READ_ONCE(icp->state);
504 xirr = old_state.xisr | (((u32)old_state.cppr) << 24);
507 new_state.cppr = new_state.pending_pri;
508 new_state.pending_pri = 0xff;
511 } while (!icp_rm_try_update(icp, old_state, new_state));
513 /* Return the result in GPR4 */
514 vcpu->arch.gpr[4] = xirr;
516 return check_too_hard(xics, icp);
519 int kvmppc_rm_h_ipi(struct kvm_vcpu *vcpu, unsigned long server,
522 union kvmppc_icp_state old_state, new_state;
523 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
524 struct kvmppc_icp *icp, *this_icp = vcpu->arch.icp;
529 if (!xics || !xics->real_mode)
532 local = this_icp->server_num == server;
536 icp = kvmppc_xics_find_server(vcpu->kvm, server);
541 * ICP state: Set_MFRR
543 * If the CPPR is more favored than the new MFRR, then
544 * nothing needs to be done as there can be no XISR to
547 * ICP state: Check_IPI
549 * If the CPPR is less favored, then we might be replacing
550 * an interrupt, and thus need to possibly reject it.
554 * Besides rejecting any pending interrupts, we also
555 * update XISR and pending_pri to mark IPI as pending.
557 * PAPR does not describe this state, but if the MFRR is being
558 * made less favored than its earlier value, there might be
559 * a previously-rejected interrupt needing to be resent.
560 * Ideally, we would want to resend only if
561 * prio(pending_interrupt) < mfrr &&
562 * prio(pending_interrupt) < cppr
563 * where pending interrupt is the one that was rejected. But
564 * we don't have that state, so we simply trigger a resend
565 * whenever the MFRR is made less favored.
568 old_state = new_state = READ_ONCE(icp->state);
571 new_state.mfrr = mfrr;
576 if (mfrr < new_state.cppr) {
577 /* Reject a pending interrupt if not an IPI */
578 if (mfrr <= new_state.pending_pri) {
579 reject = new_state.xisr;
580 new_state.pending_pri = mfrr;
581 new_state.xisr = XICS_IPI;
585 if (mfrr > old_state.mfrr) {
586 resend = new_state.need_resend;
587 new_state.need_resend = 0;
589 } while (!icp_rm_try_update(icp, old_state, new_state));
591 /* Handle reject in real mode */
592 if (reject && reject != XICS_IPI) {
593 this_icp->n_reject++;
594 icp_rm_deliver_irq(xics, icp, reject);
597 /* Handle resends in real mode */
599 this_icp->n_check_resend++;
600 icp_rm_check_resend(xics, icp);
603 return check_too_hard(xics, this_icp);
606 int kvmppc_rm_h_cppr(struct kvm_vcpu *vcpu, unsigned long cppr)
608 union kvmppc_icp_state old_state, new_state;
609 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
610 struct kvmppc_icp *icp = vcpu->arch.icp;
613 if (!xics || !xics->real_mode)
617 * ICP State: Set_CPPR
619 * We can safely compare the new value with the current
620 * value outside of the transaction as the CPPR is only
621 * ever changed by the processor on itself
623 if (cppr > icp->state.cppr) {
624 icp_rm_down_cppr(xics, icp, cppr);
626 } else if (cppr == icp->state.cppr)
632 * The processor is raising its priority, this can result
633 * in a rejection of a pending interrupt:
635 * ICP State: Reject_Current
637 * We can remove EE from the current processor, the update
638 * transaction will set it again if needed
640 icp_rm_clr_vcpu_irq(icp->vcpu);
643 old_state = new_state = READ_ONCE(icp->state);
646 new_state.cppr = cppr;
648 if (cppr <= new_state.pending_pri) {
649 reject = new_state.xisr;
651 new_state.pending_pri = 0xff;
654 } while (!icp_rm_try_update(icp, old_state, new_state));
657 * Check for rejects. They are handled by doing a new delivery
658 * attempt (see comments in icp_rm_deliver_irq).
660 if (reject && reject != XICS_IPI) {
662 icp_rm_deliver_irq(xics, icp, reject);
665 return check_too_hard(xics, icp);
668 int kvmppc_rm_h_eoi(struct kvm_vcpu *vcpu, unsigned long xirr)
670 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
671 struct kvmppc_icp *icp = vcpu->arch.icp;
672 struct kvmppc_ics *ics;
673 struct ics_irq_state *state;
674 u32 irq = xirr & 0x00ffffff;
677 if (!xics || !xics->real_mode)
683 * Note: If EOI is incorrectly used by SW to lower the CPPR
684 * value (ie more favored), we do not check for rejection of
685 * a pending interrupt, this is a SW error and PAPR sepcifies
686 * that we don't have to deal with it.
688 * The sending of an EOI to the ICS is handled after the
691 * ICP State: Down_CPPR which we handle
692 * in a separate function as it's shared with H_CPPR.
694 icp_rm_down_cppr(xics, icp, xirr >> 24);
696 /* IPIs have no EOI */
700 * EOI handling: If the interrupt is still asserted, we need to
701 * resend it. We can take a lockless "peek" at the ICS state here.
703 * "Message" interrupts will never have "asserted" set
705 ics = kvmppc_xics_find_ics(xics, irq, &src);
708 state = &ics->irq_state[src];
710 /* Still asserted, resend it */
712 icp_rm_deliver_irq(xics, icp, irq);
714 if (!hlist_empty(&vcpu->kvm->irq_ack_notifier_list)) {
715 icp->rm_action |= XICS_RM_NOTIFY_EOI;
716 icp->rm_eoied_irq = irq;
719 if (state->host_irq) {
720 ++vcpu->stat.pthru_all;
721 if (state->intr_cpu != -1) {
722 int pcpu = raw_smp_processor_id();
724 pcpu = cpu_first_thread_sibling(pcpu);
725 ++vcpu->stat.pthru_host;
726 if (state->intr_cpu != pcpu) {
727 ++vcpu->stat.pthru_bad_aff;
728 xics_opal_rm_set_server(state->host_irq, pcpu);
730 state->intr_cpu = -1;
734 return check_too_hard(xics, icp);
737 unsigned long eoi_rc;
739 static void icp_eoi(struct irq_chip *c, u32 hwirq, u32 xirr)
741 unsigned long xics_phys;
744 rc = pnv_opal_pci_msi_eoi(c, hwirq);
752 xics_phys = local_paca->kvm_hstate.xics_phys;
753 _stwcix(xics_phys + XICS_XIRR, xirr);
756 static int xics_opal_rm_set_server(unsigned int hw_irq, int server_cpu)
758 unsigned int mangle_cpu = get_hard_smp_processor_id(server_cpu) << 2;
760 return opal_rm_set_xive(hw_irq, mangle_cpu, DEFAULT_PRIORITY);
764 * Increment a per-CPU 32-bit unsigned integer variable.
765 * Safe to call in real-mode. Handles vmalloc'ed addresses
767 * ToDo: Make this work for any integral type
770 static inline void this_cpu_inc_rm(unsigned int __percpu *addr)
774 int cpu = smp_processor_id();
776 raddr = per_cpu_ptr(addr, cpu);
777 l = (unsigned long)raddr;
779 if (REGION_ID(l) == VMALLOC_REGION_ID) {
780 l = vmalloc_to_phys(raddr);
781 raddr = (unsigned int *)l;
787 * We don't try to update the flags in the irq_desc 'istate' field in
788 * here as would happen in the normal IRQ handling path for several reasons:
789 * - state flags represent internal IRQ state and are not expected to be
790 * updated outside the IRQ subsystem
791 * - more importantly, these are useful for edge triggered interrupts,
792 * IRQ probing, etc., but we are only handling MSI/MSIx interrupts here
793 * and these states shouldn't apply to us.
795 * However, we do update irq_stats - we somewhat duplicate the code in
796 * kstat_incr_irqs_this_cpu() for this since this function is defined
797 * in irq/internal.h which we don't want to include here.
798 * The only difference is that desc->kstat_irqs is an allocated per CPU
799 * variable and could have been vmalloc'ed, so we can't directly
800 * call __this_cpu_inc() on it. The kstat structure is a static
801 * per CPU variable and it should be accessible by real-mode KVM.
804 static void kvmppc_rm_handle_irq_desc(struct irq_desc *desc)
806 this_cpu_inc_rm(desc->kstat_irqs);
807 __this_cpu_inc(kstat.irqs_sum);
810 long kvmppc_deliver_irq_passthru(struct kvm_vcpu *vcpu,
812 struct kvmppc_irq_map *irq_map,
813 struct kvmppc_passthru_irqmap *pimap)
815 struct kvmppc_xics *xics;
816 struct kvmppc_icp *icp;
819 irq = irq_map->v_hwirq;
820 xics = vcpu->kvm->arch.xics;
821 icp = vcpu->arch.icp;
823 kvmppc_rm_handle_irq_desc(irq_map->desc);
824 icp_rm_deliver_irq(xics, icp, irq);
826 /* EOI the interrupt */
827 icp_eoi(irq_desc_get_chip(irq_map->desc), irq_map->r_hwirq, xirr);
829 if (check_too_hard(xics, icp) == H_TOO_HARD)
835 /* --- Non-real mode XICS-related built-in routines --- */
838 * Host Operations poked by RM KVM
840 static void rm_host_ipi_action(int action, void *data)
843 case XICS_RM_KICK_VCPU:
844 kvmppc_host_rm_ops_hv->vcpu_kick(data);
847 WARN(1, "Unexpected rm_action=%d data=%p\n", action, data);
853 void kvmppc_xics_ipi_action(void)
856 unsigned int cpu = smp_processor_id();
857 struct kvmppc_host_rm_core *rm_corep;
859 core = cpu >> threads_shift;
860 rm_corep = &kvmppc_host_rm_ops_hv->rm_core[core];
862 if (rm_corep->rm_data) {
863 rm_host_ipi_action(rm_corep->rm_state.rm_action,
865 /* Order these stores against the real mode KVM */
866 rm_corep->rm_data = NULL;
868 rm_corep->rm_state.rm_action = 0;