1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2012 Michael Ellerman, IBM Corporation.
4 * Copyright 2012 Benjamin Herrenschmidt, IBM Corporation.
7 #include <linux/kernel.h>
8 #include <linux/kvm_host.h>
10 #include <linux/gfp.h>
11 #include <linux/anon_inodes.h>
12 #include <linux/spinlock.h>
13 #include <linux/debugfs.h>
14 #include <linux/uaccess.h>
16 #include <asm/kvm_book3s.h>
17 #include <asm/kvm_ppc.h>
18 #include <asm/hvcall.h>
22 #include <linux/seq_file.h>
24 #include "book3s_xics.h"
27 #define XICS_DBG(fmt...) do { } while (0)
29 #define XICS_DBG(fmt...) trace_printk(fmt)
32 #define ENABLE_REALMODE true
33 #define DEBUG_REALMODE false
39 * Each ICS has a spin lock protecting the information about the IRQ
40 * sources and avoiding simultaneous deliveries of the same interrupt.
42 * ICP operations are done via a single compare & swap transaction
43 * (most ICP state fits in the union kvmppc_icp_state)
50 * - To speed up resends, keep a bitmap of "resend" set bits in the
53 * - Speed up server# -> ICP lookup (array ? hash table ?)
55 * - Make ICS lockless as well, or at least a per-interrupt lock or hashed
56 * locks array to improve scalability
59 /* -- ICS routines -- */
61 static void icp_deliver_irq(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
62 u32 new_irq, bool check_resend);
65 * Return value ideally indicates how the interrupt was handled, but no
66 * callers look at it (given that we don't implement KVM_IRQ_LINE_STATUS),
69 static int ics_deliver_irq(struct kvmppc_xics *xics, u32 irq, u32 level)
71 struct ics_irq_state *state;
72 struct kvmppc_ics *ics;
76 XICS_DBG("ics deliver %#x (level: %d)\n", irq, level);
78 ics = kvmppc_xics_find_ics(xics, irq, &src);
80 XICS_DBG("ics_deliver_irq: IRQ 0x%06x not found !\n", irq);
83 state = &ics->irq_state[src];
87 if (level == KVM_INTERRUPT_SET_LEVEL || level == KVM_INTERRUPT_SET)
89 else if (level == KVM_INTERRUPT_UNSET)
92 * Take other values the same as 1, consistent with original code.
96 if (!state->lsi && level == 0) /* noop for MSI */
100 pq_old = state->pq_state;
103 if (pq_old & PQ_PRESENTED)
104 /* Setting already set LSI ... */
107 pq_new = PQ_PRESENTED;
111 pq_new = ((pq_old << 1) & 3) | PQ_PRESENTED;
112 } while (cmpxchg(&state->pq_state, pq_old, pq_new) != pq_old);
114 /* Test P=1, Q=0, this is the only case where we present */
115 if (pq_new == PQ_PRESENTED)
116 icp_deliver_irq(xics, NULL, irq, false);
118 /* Record which CPU this arrived on for passed-through interrupts */
120 state->intr_cpu = raw_smp_processor_id();
125 static void ics_check_resend(struct kvmppc_xics *xics, struct kvmppc_ics *ics,
126 struct kvmppc_icp *icp)
130 for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
131 struct ics_irq_state *state = &ics->irq_state[i];
133 XICS_DBG("resend %#x prio %#x\n", state->number,
135 icp_deliver_irq(xics, icp, state->number, true);
140 static bool write_xive(struct kvmppc_xics *xics, struct kvmppc_ics *ics,
141 struct ics_irq_state *state,
142 u32 server, u32 priority, u32 saved_priority)
147 local_irq_save(flags);
148 arch_spin_lock(&ics->lock);
150 state->server = server;
151 state->priority = priority;
152 state->saved_priority = saved_priority;
154 if ((state->masked_pending || state->resend) && priority != MASKED) {
155 state->masked_pending = 0;
160 arch_spin_unlock(&ics->lock);
161 local_irq_restore(flags);
166 int kvmppc_xics_set_xive(struct kvm *kvm, u32 irq, u32 server, u32 priority)
168 struct kvmppc_xics *xics = kvm->arch.xics;
169 struct kvmppc_icp *icp;
170 struct kvmppc_ics *ics;
171 struct ics_irq_state *state;
177 ics = kvmppc_xics_find_ics(xics, irq, &src);
180 state = &ics->irq_state[src];
182 icp = kvmppc_xics_find_server(kvm, server);
186 XICS_DBG("set_xive %#x server %#x prio %#x MP:%d RS:%d\n",
187 irq, server, priority,
188 state->masked_pending, state->resend);
190 if (write_xive(xics, ics, state, server, priority, priority))
191 icp_deliver_irq(xics, icp, irq, false);
196 int kvmppc_xics_get_xive(struct kvm *kvm, u32 irq, u32 *server, u32 *priority)
198 struct kvmppc_xics *xics = kvm->arch.xics;
199 struct kvmppc_ics *ics;
200 struct ics_irq_state *state;
207 ics = kvmppc_xics_find_ics(xics, irq, &src);
210 state = &ics->irq_state[src];
212 local_irq_save(flags);
213 arch_spin_lock(&ics->lock);
214 *server = state->server;
215 *priority = state->priority;
216 arch_spin_unlock(&ics->lock);
217 local_irq_restore(flags);
222 int kvmppc_xics_int_on(struct kvm *kvm, u32 irq)
224 struct kvmppc_xics *xics = kvm->arch.xics;
225 struct kvmppc_icp *icp;
226 struct kvmppc_ics *ics;
227 struct ics_irq_state *state;
233 ics = kvmppc_xics_find_ics(xics, irq, &src);
236 state = &ics->irq_state[src];
238 icp = kvmppc_xics_find_server(kvm, state->server);
242 if (write_xive(xics, ics, state, state->server, state->saved_priority,
243 state->saved_priority))
244 icp_deliver_irq(xics, icp, irq, false);
249 int kvmppc_xics_int_off(struct kvm *kvm, u32 irq)
251 struct kvmppc_xics *xics = kvm->arch.xics;
252 struct kvmppc_ics *ics;
253 struct ics_irq_state *state;
259 ics = kvmppc_xics_find_ics(xics, irq, &src);
262 state = &ics->irq_state[src];
264 write_xive(xics, ics, state, state->server, MASKED, state->priority);
269 /* -- ICP routines, including hcalls -- */
271 static inline bool icp_try_update(struct kvmppc_icp *icp,
272 union kvmppc_icp_state old,
273 union kvmppc_icp_state new,
278 /* Calculate new output value */
279 new.out_ee = (new.xisr && (new.pending_pri < new.cppr));
281 /* Attempt atomic update */
282 success = cmpxchg64(&icp->state.raw, old.raw, new.raw) == old.raw;
286 XICS_DBG("UPD [%04lx] - C:%02x M:%02x PP: %02x PI:%06x R:%d O:%d\n",
288 old.cppr, old.mfrr, old.pending_pri, old.xisr,
289 old.need_resend, old.out_ee);
290 XICS_DBG("UPD - C:%02x M:%02x PP: %02x PI:%06x R:%d O:%d\n",
291 new.cppr, new.mfrr, new.pending_pri, new.xisr,
292 new.need_resend, new.out_ee);
294 * Check for output state update
296 * Note that this is racy since another processor could be updating
297 * the state already. This is why we never clear the interrupt output
298 * here, we only ever set it. The clear only happens prior to doing
299 * an update and only by the processor itself. Currently we do it
300 * in Accept (H_XIRR) and Up_Cppr (H_XPPR).
302 * We also do not try to figure out whether the EE state has changed,
303 * we unconditionally set it if the new state calls for it. The reason
304 * for that is that we opportunistically remove the pending interrupt
305 * flag when raising CPPR, so we need to set it back here if an
306 * interrupt is still pending.
309 kvmppc_book3s_queue_irqprio(icp->vcpu,
310 BOOK3S_INTERRUPT_EXTERNAL);
312 kvmppc_fast_vcpu_kick(icp->vcpu);
318 static void icp_check_resend(struct kvmppc_xics *xics,
319 struct kvmppc_icp *icp)
323 /* Order this load with the test for need_resend in the caller */
325 for_each_set_bit(icsid, icp->resend_map, xics->max_icsid + 1) {
326 struct kvmppc_ics *ics = xics->ics[icsid];
328 if (!test_and_clear_bit(icsid, icp->resend_map))
332 ics_check_resend(xics, ics, icp);
336 static bool icp_try_to_deliver(struct kvmppc_icp *icp, u32 irq, u8 priority,
339 union kvmppc_icp_state old_state, new_state;
342 XICS_DBG("try deliver %#x(P:%#x) to server %#lx\n", irq, priority,
346 old_state = new_state = READ_ONCE(icp->state);
350 /* See if we can deliver */
351 success = new_state.cppr > priority &&
352 new_state.mfrr > priority &&
353 new_state.pending_pri > priority;
356 * If we can, check for a rejection and perform the
360 *reject = new_state.xisr;
361 new_state.xisr = irq;
362 new_state.pending_pri = priority;
365 * If we failed to deliver we set need_resend
366 * so a subsequent CPPR state change causes us
367 * to try a new delivery.
369 new_state.need_resend = true;
372 } while (!icp_try_update(icp, old_state, new_state, false));
377 static void icp_deliver_irq(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
378 u32 new_irq, bool check_resend)
380 struct ics_irq_state *state;
381 struct kvmppc_ics *ics;
387 * This is used both for initial delivery of an interrupt and
388 * for subsequent rejection.
390 * Rejection can be racy vs. resends. We have evaluated the
391 * rejection in an atomic ICP transaction which is now complete,
392 * so potentially the ICP can already accept the interrupt again.
394 * So we need to retry the delivery. Essentially the reject path
395 * boils down to a failed delivery. Always.
397 * Now the interrupt could also have moved to a different target,
398 * thus we may need to re-do the ICP lookup as well
402 /* Get the ICS state and lock it */
403 ics = kvmppc_xics_find_ics(xics, new_irq, &src);
405 XICS_DBG("icp_deliver_irq: IRQ 0x%06x not found !\n", new_irq);
408 state = &ics->irq_state[src];
410 /* Get a lock on the ICS */
411 local_irq_save(flags);
412 arch_spin_lock(&ics->lock);
415 if (!icp || state->server != icp->server_num) {
416 icp = kvmppc_xics_find_server(xics->kvm, state->server);
418 pr_warn("icp_deliver_irq: IRQ 0x%06x server 0x%x not found !\n",
419 new_irq, state->server);
428 /* Clear the resend bit of that interrupt */
432 * If masked, bail out
434 * Note: PAPR doesn't mention anything about masked pending
435 * when doing a resend, only when doing a delivery.
437 * However that would have the effect of losing a masked
438 * interrupt that was rejected and isn't consistent with
439 * the whole masked_pending business which is about not
440 * losing interrupts that occur while masked.
442 * I don't differentiate normal deliveries and resends, this
443 * implementation will differ from PAPR and not lose such
446 if (state->priority == MASKED) {
447 XICS_DBG("irq %#x masked pending\n", new_irq);
448 state->masked_pending = 1;
453 * Try the delivery, this will set the need_resend flag
454 * in the ICP as part of the atomic transaction if the
455 * delivery is not possible.
457 * Note that if successful, the new delivery might have itself
458 * rejected an interrupt that was "delivered" before we took the
461 * In this case we do the whole sequence all over again for the
462 * new guy. We cannot assume that the rejected interrupt is less
463 * favored than the new one, and thus doesn't need to be delivered,
464 * because by the time we exit icp_try_to_deliver() the target
465 * processor may well have alrady consumed & completed it, and thus
466 * the rejected interrupt might actually be already acceptable.
468 if (icp_try_to_deliver(icp, new_irq, state->priority, &reject)) {
470 * Delivery was successful, did we reject somebody else ?
472 if (reject && reject != XICS_IPI) {
473 arch_spin_unlock(&ics->lock);
474 local_irq_restore(flags);
476 check_resend = false;
481 * We failed to deliver the interrupt we need to set the
482 * resend map bit and mark the ICS state as needing a resend
487 * Make sure when checking resend, we don't miss the resend
488 * if resend_map bit is seen and cleared.
491 set_bit(ics->icsid, icp->resend_map);
494 * If the need_resend flag got cleared in the ICP some time
495 * between icp_try_to_deliver() atomic update and now, then
496 * we know it might have missed the resend_map bit. So we
500 if (!icp->state.need_resend) {
502 arch_spin_unlock(&ics->lock);
503 local_irq_restore(flags);
504 check_resend = false;
509 arch_spin_unlock(&ics->lock);
510 local_irq_restore(flags);
513 static void icp_down_cppr(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
516 union kvmppc_icp_state old_state, new_state;
520 * This handles several related states in one operation:
522 * ICP State: Down_CPPR
524 * Load CPPR with new value and if the XISR is 0
525 * then check for resends:
529 * If MFRR is more favored than CPPR, check for IPIs
530 * and notify ICS of a potential resend. This is done
531 * asynchronously (when used in real mode, we will have
534 * We do not handle the complete Check_IPI as documented
535 * here. In the PAPR, this state will be used for both
536 * Set_MFRR and Down_CPPR. However, we know that we aren't
537 * changing the MFRR state here so we don't need to handle
538 * the case of an MFRR causing a reject of a pending irq,
539 * this will have been handled when the MFRR was set in the
542 * Thus we don't have to handle rejects, only resends.
544 * When implementing real mode for HV KVM, resend will lead to
545 * a H_TOO_HARD return and the whole transaction will be handled
549 old_state = new_state = READ_ONCE(icp->state);
552 new_state.cppr = new_cppr;
555 * Cut down Resend / Check_IPI / IPI
557 * The logic is that we cannot have a pending interrupt
558 * trumped by an IPI at this point (see above), so we
559 * know that either the pending interrupt is already an
560 * IPI (in which case we don't care to override it) or
561 * it's either more favored than us or non existent
563 if (new_state.mfrr < new_cppr &&
564 new_state.mfrr <= new_state.pending_pri) {
565 WARN_ON(new_state.xisr != XICS_IPI &&
566 new_state.xisr != 0);
567 new_state.pending_pri = new_state.mfrr;
568 new_state.xisr = XICS_IPI;
571 /* Latch/clear resend bit */
572 resend = new_state.need_resend;
573 new_state.need_resend = 0;
575 } while (!icp_try_update(icp, old_state, new_state, true));
578 * Now handle resend checks. Those are asynchronous to the ICP
579 * state update in HW (ie bus transactions) so we can handle them
580 * separately here too
583 icp_check_resend(xics, icp);
586 static noinline unsigned long kvmppc_h_xirr(struct kvm_vcpu *vcpu)
588 union kvmppc_icp_state old_state, new_state;
589 struct kvmppc_icp *icp = vcpu->arch.icp;
592 /* First, remove EE from the processor */
593 kvmppc_book3s_dequeue_irqprio(icp->vcpu, BOOK3S_INTERRUPT_EXTERNAL);
596 * ICP State: Accept_Interrupt
598 * Return the pending interrupt (if any) along with the
599 * current CPPR, then clear the XISR & set CPPR to the
603 old_state = new_state = READ_ONCE(icp->state);
605 xirr = old_state.xisr | (((u32)old_state.cppr) << 24);
608 new_state.cppr = new_state.pending_pri;
609 new_state.pending_pri = 0xff;
612 } while (!icp_try_update(icp, old_state, new_state, true));
614 XICS_DBG("h_xirr vcpu %d xirr %#x\n", vcpu->vcpu_id, xirr);
619 static noinline int kvmppc_h_ipi(struct kvm_vcpu *vcpu, unsigned long server,
622 union kvmppc_icp_state old_state, new_state;
623 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
624 struct kvmppc_icp *icp;
629 XICS_DBG("h_ipi vcpu %d to server %lu mfrr %#lx\n",
630 vcpu->vcpu_id, server, mfrr);
632 icp = vcpu->arch.icp;
633 local = icp->server_num == server;
635 icp = kvmppc_xics_find_server(vcpu->kvm, server);
641 * ICP state: Set_MFRR
643 * If the CPPR is more favored than the new MFRR, then
644 * nothing needs to be rejected as there can be no XISR to
645 * reject. If the MFRR is being made less favored then
646 * there might be a previously-rejected interrupt needing
649 * ICP state: Check_IPI
651 * If the CPPR is less favored, then we might be replacing
652 * an interrupt, and thus need to possibly reject it.
656 * Besides rejecting any pending interrupts, we also
657 * update XISR and pending_pri to mark IPI as pending.
659 * PAPR does not describe this state, but if the MFRR is being
660 * made less favored than its earlier value, there might be
661 * a previously-rejected interrupt needing to be resent.
662 * Ideally, we would want to resend only if
663 * prio(pending_interrupt) < mfrr &&
664 * prio(pending_interrupt) < cppr
665 * where pending interrupt is the one that was rejected. But
666 * we don't have that state, so we simply trigger a resend
667 * whenever the MFRR is made less favored.
670 old_state = new_state = READ_ONCE(icp->state);
673 new_state.mfrr = mfrr;
678 if (mfrr < new_state.cppr) {
679 /* Reject a pending interrupt if not an IPI */
680 if (mfrr <= new_state.pending_pri) {
681 reject = new_state.xisr;
682 new_state.pending_pri = mfrr;
683 new_state.xisr = XICS_IPI;
687 if (mfrr > old_state.mfrr) {
688 resend = new_state.need_resend;
689 new_state.need_resend = 0;
691 } while (!icp_try_update(icp, old_state, new_state, local));
694 if (reject && reject != XICS_IPI)
695 icp_deliver_irq(xics, icp, reject, false);
699 icp_check_resend(xics, icp);
704 static int kvmppc_h_ipoll(struct kvm_vcpu *vcpu, unsigned long server)
706 union kvmppc_icp_state state;
707 struct kvmppc_icp *icp;
709 icp = vcpu->arch.icp;
710 if (icp->server_num != server) {
711 icp = kvmppc_xics_find_server(vcpu->kvm, server);
715 state = READ_ONCE(icp->state);
716 kvmppc_set_gpr(vcpu, 4, ((u32)state.cppr << 24) | state.xisr);
717 kvmppc_set_gpr(vcpu, 5, state.mfrr);
721 static noinline void kvmppc_h_cppr(struct kvm_vcpu *vcpu, unsigned long cppr)
723 union kvmppc_icp_state old_state, new_state;
724 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
725 struct kvmppc_icp *icp = vcpu->arch.icp;
728 XICS_DBG("h_cppr vcpu %d cppr %#lx\n", vcpu->vcpu_id, cppr);
731 * ICP State: Set_CPPR
733 * We can safely compare the new value with the current
734 * value outside of the transaction as the CPPR is only
735 * ever changed by the processor on itself
737 if (cppr > icp->state.cppr)
738 icp_down_cppr(xics, icp, cppr);
739 else if (cppr == icp->state.cppr)
745 * The processor is raising its priority, this can result
746 * in a rejection of a pending interrupt:
748 * ICP State: Reject_Current
750 * We can remove EE from the current processor, the update
751 * transaction will set it again if needed
753 kvmppc_book3s_dequeue_irqprio(icp->vcpu, BOOK3S_INTERRUPT_EXTERNAL);
756 old_state = new_state = READ_ONCE(icp->state);
759 new_state.cppr = cppr;
761 if (cppr <= new_state.pending_pri) {
762 reject = new_state.xisr;
764 new_state.pending_pri = 0xff;
767 } while (!icp_try_update(icp, old_state, new_state, true));
770 * Check for rejects. They are handled by doing a new delivery
771 * attempt (see comments in icp_deliver_irq).
773 if (reject && reject != XICS_IPI)
774 icp_deliver_irq(xics, icp, reject, false);
777 static int ics_eoi(struct kvm_vcpu *vcpu, u32 irq)
779 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
780 struct kvmppc_icp *icp = vcpu->arch.icp;
781 struct kvmppc_ics *ics;
782 struct ics_irq_state *state;
787 * ICS EOI handling: For LSI, if P bit is still set, we need to
790 * For MSI, we move Q bit into P (and clear Q). If it is set,
794 ics = kvmppc_xics_find_ics(xics, irq, &src);
796 XICS_DBG("ios_eoi: IRQ 0x%06x not found !\n", irq);
799 state = &ics->irq_state[src];
802 pq_new = state->pq_state;
805 pq_old = state->pq_state;
806 pq_new = pq_old >> 1;
807 } while (cmpxchg(&state->pq_state, pq_old, pq_new) != pq_old);
809 if (pq_new & PQ_PRESENTED)
810 icp_deliver_irq(xics, icp, irq, false);
812 kvm_notify_acked_irq(vcpu->kvm, 0, irq);
817 static noinline int kvmppc_h_eoi(struct kvm_vcpu *vcpu, unsigned long xirr)
819 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
820 struct kvmppc_icp *icp = vcpu->arch.icp;
821 u32 irq = xirr & 0x00ffffff;
823 XICS_DBG("h_eoi vcpu %d eoi %#lx\n", vcpu->vcpu_id, xirr);
828 * Note: If EOI is incorrectly used by SW to lower the CPPR
829 * value (ie more favored), we do not check for rejection of
830 * a pending interrupt, this is a SW error and PAPR specifies
831 * that we don't have to deal with it.
833 * The sending of an EOI to the ICS is handled after the
836 * ICP State: Down_CPPR which we handle
837 * in a separate function as it's shared with H_CPPR.
839 icp_down_cppr(xics, icp, xirr >> 24);
841 /* IPIs have no EOI */
845 return ics_eoi(vcpu, irq);
848 int kvmppc_xics_rm_complete(struct kvm_vcpu *vcpu, u32 hcall)
850 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
851 struct kvmppc_icp *icp = vcpu->arch.icp;
853 XICS_DBG("XICS_RM: H_%x completing, act: %x state: %lx tgt: %p\n",
854 hcall, icp->rm_action, icp->rm_dbgstate.raw, icp->rm_dbgtgt);
856 if (icp->rm_action & XICS_RM_KICK_VCPU) {
857 icp->n_rm_kick_vcpu++;
858 kvmppc_fast_vcpu_kick(icp->rm_kick_target);
860 if (icp->rm_action & XICS_RM_CHECK_RESEND) {
861 icp->n_rm_check_resend++;
862 icp_check_resend(xics, icp->rm_resend_icp);
864 if (icp->rm_action & XICS_RM_NOTIFY_EOI) {
865 icp->n_rm_notify_eoi++;
866 kvm_notify_acked_irq(vcpu->kvm, 0, icp->rm_eoied_irq);
873 EXPORT_SYMBOL_GPL(kvmppc_xics_rm_complete);
875 int kvmppc_xics_hcall(struct kvm_vcpu *vcpu, u32 req)
877 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
881 /* Check if we have an ICP */
882 if (!xics || !vcpu->arch.icp)
885 /* These requests don't have real-mode implementations at present */
888 res = kvmppc_h_xirr(vcpu);
889 kvmppc_set_gpr(vcpu, 4, res);
890 kvmppc_set_gpr(vcpu, 5, get_tb());
893 rc = kvmppc_h_ipoll(vcpu, kvmppc_get_gpr(vcpu, 4));
897 /* Check for real mode returning too hard */
898 if (xics->real_mode && is_kvmppc_hv_enabled(vcpu->kvm))
899 return kvmppc_xics_rm_complete(vcpu, req);
903 res = kvmppc_h_xirr(vcpu);
904 kvmppc_set_gpr(vcpu, 4, res);
907 kvmppc_h_cppr(vcpu, kvmppc_get_gpr(vcpu, 4));
910 rc = kvmppc_h_eoi(vcpu, kvmppc_get_gpr(vcpu, 4));
913 rc = kvmppc_h_ipi(vcpu, kvmppc_get_gpr(vcpu, 4),
914 kvmppc_get_gpr(vcpu, 5));
920 EXPORT_SYMBOL_GPL(kvmppc_xics_hcall);
923 /* -- Initialisation code etc. -- */
925 static void xics_debugfs_irqmap(struct seq_file *m,
926 struct kvmppc_passthru_irqmap *pimap)
932 seq_printf(m, "========\nPIRQ mappings: %d maps\n===========\n",
934 for (i = 0; i < pimap->n_mapped; i++) {
935 seq_printf(m, "r_hwirq=%x, v_hwirq=%x\n",
936 pimap->mapped[i].r_hwirq, pimap->mapped[i].v_hwirq);
940 static int xics_debug_show(struct seq_file *m, void *private)
942 struct kvmppc_xics *xics = m->private;
943 struct kvm *kvm = xics->kvm;
944 struct kvm_vcpu *vcpu;
947 unsigned long t_rm_kick_vcpu, t_rm_check_resend;
948 unsigned long t_rm_notify_eoi;
949 unsigned long t_reject, t_check_resend;
956 t_rm_check_resend = 0;
960 xics_debugfs_irqmap(m, kvm->arch.pimap);
962 seq_printf(m, "=========\nICP state\n=========\n");
964 kvm_for_each_vcpu(i, vcpu, kvm) {
965 struct kvmppc_icp *icp = vcpu->arch.icp;
966 union kvmppc_icp_state state;
971 state.raw = READ_ONCE(icp->state.raw);
972 seq_printf(m, "cpu server %#lx XIRR:%#x PPRI:%#x CPPR:%#x MFRR:%#x OUT:%d NR:%d\n",
973 icp->server_num, state.xisr,
974 state.pending_pri, state.cppr, state.mfrr,
975 state.out_ee, state.need_resend);
976 t_rm_kick_vcpu += icp->n_rm_kick_vcpu;
977 t_rm_notify_eoi += icp->n_rm_notify_eoi;
978 t_rm_check_resend += icp->n_rm_check_resend;
979 t_check_resend += icp->n_check_resend;
980 t_reject += icp->n_reject;
983 seq_printf(m, "ICP Guest->Host totals: kick_vcpu=%lu check_resend=%lu notify_eoi=%lu\n",
984 t_rm_kick_vcpu, t_rm_check_resend,
986 seq_printf(m, "ICP Real Mode totals: check_resend=%lu resend=%lu\n",
987 t_check_resend, t_reject);
988 for (icsid = 0; icsid <= KVMPPC_XICS_MAX_ICS_ID; icsid++) {
989 struct kvmppc_ics *ics = xics->ics[icsid];
994 seq_printf(m, "=========\nICS state for ICS 0x%x\n=========\n",
997 local_irq_save(flags);
998 arch_spin_lock(&ics->lock);
1000 for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
1001 struct ics_irq_state *irq = &ics->irq_state[i];
1003 seq_printf(m, "irq 0x%06x: server %#x prio %#x save prio %#x pq_state %d resend %d masked pending %d\n",
1004 irq->number, irq->server, irq->priority,
1005 irq->saved_priority, irq->pq_state,
1006 irq->resend, irq->masked_pending);
1009 arch_spin_unlock(&ics->lock);
1010 local_irq_restore(flags);
1015 DEFINE_SHOW_ATTRIBUTE(xics_debug);
1017 static void xics_debugfs_init(struct kvmppc_xics *xics)
1021 name = kasprintf(GFP_KERNEL, "kvm-xics-%p", xics);
1023 pr_err("%s: no memory for name\n", __func__);
1027 xics->dentry = debugfs_create_file(name, 0444, arch_debugfs_dir,
1028 xics, &xics_debug_fops);
1030 pr_debug("%s: created %s\n", __func__, name);
1034 static struct kvmppc_ics *kvmppc_xics_create_ics(struct kvm *kvm,
1035 struct kvmppc_xics *xics, int irq)
1037 struct kvmppc_ics *ics;
1040 icsid = irq >> KVMPPC_XICS_ICS_SHIFT;
1042 mutex_lock(&kvm->lock);
1044 /* ICS already exists - somebody else got here first */
1045 if (xics->ics[icsid])
1048 /* Create the ICS */
1049 ics = kzalloc(sizeof(struct kvmppc_ics), GFP_KERNEL);
1055 for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
1056 ics->irq_state[i].number = (icsid << KVMPPC_XICS_ICS_SHIFT) | i;
1057 ics->irq_state[i].priority = MASKED;
1058 ics->irq_state[i].saved_priority = MASKED;
1061 xics->ics[icsid] = ics;
1063 if (icsid > xics->max_icsid)
1064 xics->max_icsid = icsid;
1067 mutex_unlock(&kvm->lock);
1068 return xics->ics[icsid];
1071 static int kvmppc_xics_create_icp(struct kvm_vcpu *vcpu, unsigned long server_num)
1073 struct kvmppc_icp *icp;
1075 if (!vcpu->kvm->arch.xics)
1078 if (kvmppc_xics_find_server(vcpu->kvm, server_num))
1081 icp = kzalloc(sizeof(struct kvmppc_icp), GFP_KERNEL);
1086 icp->server_num = server_num;
1087 icp->state.mfrr = MASKED;
1088 icp->state.pending_pri = MASKED;
1089 vcpu->arch.icp = icp;
1091 XICS_DBG("created server for vcpu %d\n", vcpu->vcpu_id);
1096 u64 kvmppc_xics_get_icp(struct kvm_vcpu *vcpu)
1098 struct kvmppc_icp *icp = vcpu->arch.icp;
1099 union kvmppc_icp_state state;
1104 return ((u64)state.cppr << KVM_REG_PPC_ICP_CPPR_SHIFT) |
1105 ((u64)state.xisr << KVM_REG_PPC_ICP_XISR_SHIFT) |
1106 ((u64)state.mfrr << KVM_REG_PPC_ICP_MFRR_SHIFT) |
1107 ((u64)state.pending_pri << KVM_REG_PPC_ICP_PPRI_SHIFT);
1110 int kvmppc_xics_set_icp(struct kvm_vcpu *vcpu, u64 icpval)
1112 struct kvmppc_icp *icp = vcpu->arch.icp;
1113 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
1114 union kvmppc_icp_state old_state, new_state;
1115 struct kvmppc_ics *ics;
1116 u8 cppr, mfrr, pending_pri;
1124 cppr = icpval >> KVM_REG_PPC_ICP_CPPR_SHIFT;
1125 xisr = (icpval >> KVM_REG_PPC_ICP_XISR_SHIFT) &
1126 KVM_REG_PPC_ICP_XISR_MASK;
1127 mfrr = icpval >> KVM_REG_PPC_ICP_MFRR_SHIFT;
1128 pending_pri = icpval >> KVM_REG_PPC_ICP_PPRI_SHIFT;
1130 /* Require the new state to be internally consistent */
1132 if (pending_pri != 0xff)
1134 } else if (xisr == XICS_IPI) {
1135 if (pending_pri != mfrr || pending_pri >= cppr)
1138 if (pending_pri >= mfrr || pending_pri >= cppr)
1140 ics = kvmppc_xics_find_ics(xics, xisr, &src);
1146 new_state.cppr = cppr;
1147 new_state.xisr = xisr;
1148 new_state.mfrr = mfrr;
1149 new_state.pending_pri = pending_pri;
1152 * Deassert the CPU interrupt request.
1153 * icp_try_update will reassert it if necessary.
1155 kvmppc_book3s_dequeue_irqprio(icp->vcpu, BOOK3S_INTERRUPT_EXTERNAL);
1158 * Note that if we displace an interrupt from old_state.xisr,
1159 * we don't mark it as rejected. We expect userspace to set
1160 * the state of the interrupt sources to be consistent with
1161 * the ICP states (either before or afterwards, which doesn't
1162 * matter). We do handle resends due to CPPR becoming less
1163 * favoured because that is necessary to end up with a
1164 * consistent state in the situation where userspace restores
1165 * the ICS states before the ICP states.
1168 old_state = READ_ONCE(icp->state);
1170 if (new_state.mfrr <= old_state.mfrr) {
1172 new_state.need_resend = old_state.need_resend;
1174 resend = old_state.need_resend;
1175 new_state.need_resend = 0;
1177 } while (!icp_try_update(icp, old_state, new_state, false));
1180 icp_check_resend(xics, icp);
1185 static int xics_get_source(struct kvmppc_xics *xics, long irq, u64 addr)
1188 struct kvmppc_ics *ics;
1189 struct ics_irq_state *irqp;
1190 u64 __user *ubufp = (u64 __user *) addr;
1193 unsigned long flags;
1195 ics = kvmppc_xics_find_ics(xics, irq, &idx);
1199 irqp = &ics->irq_state[idx];
1200 local_irq_save(flags);
1201 arch_spin_lock(&ics->lock);
1205 prio = irqp->priority;
1206 if (prio == MASKED) {
1207 val |= KVM_XICS_MASKED;
1208 prio = irqp->saved_priority;
1210 val |= prio << KVM_XICS_PRIORITY_SHIFT;
1212 val |= KVM_XICS_LEVEL_SENSITIVE;
1213 if (irqp->pq_state & PQ_PRESENTED)
1214 val |= KVM_XICS_PENDING;
1215 } else if (irqp->masked_pending || irqp->resend)
1216 val |= KVM_XICS_PENDING;
1218 if (irqp->pq_state & PQ_PRESENTED)
1219 val |= KVM_XICS_PRESENTED;
1221 if (irqp->pq_state & PQ_QUEUED)
1222 val |= KVM_XICS_QUEUED;
1226 arch_spin_unlock(&ics->lock);
1227 local_irq_restore(flags);
1229 if (!ret && put_user(val, ubufp))
1235 static int xics_set_source(struct kvmppc_xics *xics, long irq, u64 addr)
1237 struct kvmppc_ics *ics;
1238 struct ics_irq_state *irqp;
1239 u64 __user *ubufp = (u64 __user *) addr;
1244 unsigned long flags;
1246 if (irq < KVMPPC_XICS_FIRST_IRQ || irq >= KVMPPC_XICS_NR_IRQS)
1249 ics = kvmppc_xics_find_ics(xics, irq, &idx);
1251 ics = kvmppc_xics_create_ics(xics->kvm, xics, irq);
1255 irqp = &ics->irq_state[idx];
1256 if (get_user(val, ubufp))
1259 server = val & KVM_XICS_DESTINATION_MASK;
1260 prio = val >> KVM_XICS_PRIORITY_SHIFT;
1261 if (prio != MASKED &&
1262 kvmppc_xics_find_server(xics->kvm, server) == NULL)
1265 local_irq_save(flags);
1266 arch_spin_lock(&ics->lock);
1267 irqp->server = server;
1268 irqp->saved_priority = prio;
1269 if (val & KVM_XICS_MASKED)
1271 irqp->priority = prio;
1273 irqp->masked_pending = 0;
1276 if (val & KVM_XICS_LEVEL_SENSITIVE)
1278 /* If PENDING, set P in case P is not saved because of old code */
1279 if (val & KVM_XICS_PRESENTED || val & KVM_XICS_PENDING)
1280 irqp->pq_state |= PQ_PRESENTED;
1281 if (val & KVM_XICS_QUEUED)
1282 irqp->pq_state |= PQ_QUEUED;
1284 arch_spin_unlock(&ics->lock);
1285 local_irq_restore(flags);
1287 if (val & KVM_XICS_PENDING)
1288 icp_deliver_irq(xics, NULL, irqp->number, false);
1293 int kvmppc_xics_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level,
1296 struct kvmppc_xics *xics = kvm->arch.xics;
1300 return ics_deliver_irq(xics, irq, level);
1303 static int xics_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1305 struct kvmppc_xics *xics = dev->private;
1307 switch (attr->group) {
1308 case KVM_DEV_XICS_GRP_SOURCES:
1309 return xics_set_source(xics, attr->attr, attr->addr);
1314 static int xics_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1316 struct kvmppc_xics *xics = dev->private;
1318 switch (attr->group) {
1319 case KVM_DEV_XICS_GRP_SOURCES:
1320 return xics_get_source(xics, attr->attr, attr->addr);
1325 static int xics_has_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1327 switch (attr->group) {
1328 case KVM_DEV_XICS_GRP_SOURCES:
1329 if (attr->attr >= KVMPPC_XICS_FIRST_IRQ &&
1330 attr->attr < KVMPPC_XICS_NR_IRQS)
1338 * Called when device fd is closed. kvm->lock is held.
1340 static void kvmppc_xics_release(struct kvm_device *dev)
1342 struct kvmppc_xics *xics = dev->private;
1344 struct kvm *kvm = xics->kvm;
1345 struct kvm_vcpu *vcpu;
1347 pr_devel("Releasing xics device\n");
1350 * Since this is the device release function, we know that
1351 * userspace does not have any open fd referring to the
1352 * device. Therefore there can not be any of the device
1353 * attribute set/get functions being executed concurrently,
1354 * and similarly, the connect_vcpu and set/clr_mapped
1355 * functions also cannot be being executed.
1358 debugfs_remove(xics->dentry);
1361 * We should clean up the vCPU interrupt presenters first.
1363 kvm_for_each_vcpu(i, vcpu, kvm) {
1365 * Take vcpu->mutex to ensure that no one_reg get/set ioctl
1366 * (i.e. kvmppc_xics_[gs]et_icp) can be done concurrently.
1367 * Holding the vcpu->mutex also means that execution is
1368 * excluded for the vcpu until the ICP was freed. When the vcpu
1369 * can execute again, vcpu->arch.icp and vcpu->arch.irq_type
1370 * have been cleared and the vcpu will not be going into the
1371 * XICS code anymore.
1373 mutex_lock(&vcpu->mutex);
1374 kvmppc_xics_free_icp(vcpu);
1375 mutex_unlock(&vcpu->mutex);
1379 kvm->arch.xics = NULL;
1381 for (i = 0; i <= xics->max_icsid; i++) {
1382 kfree(xics->ics[i]);
1383 xics->ics[i] = NULL;
1386 * A reference of the kvmppc_xics pointer is now kept under
1387 * the xics_device pointer of the machine for reuse. It is
1388 * freed when the VM is destroyed for now until we fix all the
1394 static struct kvmppc_xics *kvmppc_xics_get_device(struct kvm *kvm)
1396 struct kvmppc_xics **kvm_xics_device = &kvm->arch.xics_device;
1397 struct kvmppc_xics *xics = *kvm_xics_device;
1400 xics = kzalloc(sizeof(*xics), GFP_KERNEL);
1401 *kvm_xics_device = xics;
1403 memset(xics, 0, sizeof(*xics));
1409 static int kvmppc_xics_create(struct kvm_device *dev, u32 type)
1411 struct kvmppc_xics *xics;
1412 struct kvm *kvm = dev->kvm;
1414 pr_devel("Creating xics for partition\n");
1416 /* Already there ? */
1420 xics = kvmppc_xics_get_device(kvm);
1424 dev->private = xics;
1427 kvm->arch.xics = xics;
1429 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
1430 if (cpu_has_feature(CPU_FTR_ARCH_206) &&
1431 cpu_has_feature(CPU_FTR_HVMODE)) {
1432 /* Enable real mode support */
1433 xics->real_mode = ENABLE_REALMODE;
1434 xics->real_mode_dbg = DEBUG_REALMODE;
1436 #endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
1441 static void kvmppc_xics_init(struct kvm_device *dev)
1443 struct kvmppc_xics *xics = (struct kvmppc_xics *)dev->private;
1445 xics_debugfs_init(xics);
1448 struct kvm_device_ops kvm_xics_ops = {
1450 .create = kvmppc_xics_create,
1451 .init = kvmppc_xics_init,
1452 .release = kvmppc_xics_release,
1453 .set_attr = xics_set_attr,
1454 .get_attr = xics_get_attr,
1455 .has_attr = xics_has_attr,
1458 int kvmppc_xics_connect_vcpu(struct kvm_device *dev, struct kvm_vcpu *vcpu,
1461 struct kvmppc_xics *xics = dev->private;
1464 if (dev->ops != &kvm_xics_ops)
1466 if (xics->kvm != vcpu->kvm)
1468 if (vcpu->arch.irq_type != KVMPPC_IRQ_DEFAULT)
1471 r = kvmppc_xics_create_icp(vcpu, xcpu);
1473 vcpu->arch.irq_type = KVMPPC_IRQ_XICS;
1478 void kvmppc_xics_free_icp(struct kvm_vcpu *vcpu)
1480 if (!vcpu->arch.icp)
1482 kfree(vcpu->arch.icp);
1483 vcpu->arch.icp = NULL;
1484 vcpu->arch.irq_type = KVMPPC_IRQ_DEFAULT;
1487 void kvmppc_xics_set_mapped(struct kvm *kvm, unsigned long irq,
1488 unsigned long host_irq)
1490 struct kvmppc_xics *xics = kvm->arch.xics;
1491 struct kvmppc_ics *ics;
1494 ics = kvmppc_xics_find_ics(xics, irq, &idx);
1498 ics->irq_state[idx].host_irq = host_irq;
1499 ics->irq_state[idx].intr_cpu = -1;
1501 EXPORT_SYMBOL_GPL(kvmppc_xics_set_mapped);
1503 void kvmppc_xics_clr_mapped(struct kvm *kvm, unsigned long irq,
1504 unsigned long host_irq)
1506 struct kvmppc_xics *xics = kvm->arch.xics;
1507 struct kvmppc_ics *ics;
1510 ics = kvmppc_xics_find_ics(xics, irq, &idx);
1514 ics->irq_state[idx].host_irq = 0;
1516 EXPORT_SYMBOL_GPL(kvmppc_xics_clr_mapped);