1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright 2012 Michael Ellerman, IBM Corporation.
4 * Copyright 2012 Benjamin Herrenschmidt, IBM Corporation
7 #ifndef _KVM_PPC_BOOK3S_XICS_H
8 #define _KVM_PPC_BOOK3S_XICS_H
10 #ifdef CONFIG_KVM_XICS
12 * We use a two-level tree to store interrupt source information.
13 * There are up to 1024 ICS nodes, each of which can represent
16 #define KVMPPC_XICS_MAX_ICS_ID 1023
17 #define KVMPPC_XICS_ICS_SHIFT 10
18 #define KVMPPC_XICS_IRQ_PER_ICS (1 << KVMPPC_XICS_ICS_SHIFT)
19 #define KVMPPC_XICS_SRC_MASK (KVMPPC_XICS_IRQ_PER_ICS - 1)
22 * Interrupt source numbers below this are reserved, for example
23 * 0 is "no interrupt", and 2 is used for IPIs.
25 #define KVMPPC_XICS_FIRST_IRQ 16
26 #define KVMPPC_XICS_NR_IRQS ((KVMPPC_XICS_MAX_ICS_ID + 1) * \
27 KVMPPC_XICS_IRQ_PER_ICS)
29 /* Priority value to use for disabling an interrupt */
32 #define PQ_PRESENTED 1
35 /* State for one irq source */
36 struct ics_irq_state {
44 u8 lsi; /* level-sensitive interrupt */
50 /* Atomic ICP state, updated with a single compare & swap */
51 union kvmppc_icp_state {
64 #define ICP_RESEND_MAP_SIZE (KVMPPC_XICS_MAX_ICS_ID / BITS_PER_LONG + 1)
67 struct kvm_vcpu *vcpu;
68 unsigned long server_num;
69 union kvmppc_icp_state state;
70 unsigned long resend_map[ICP_RESEND_MAP_SIZE];
72 /* Real mode might find something too hard, here's the action
73 * it might request from virtual mode
75 #define XICS_RM_KICK_VCPU 0x1
76 #define XICS_RM_CHECK_RESEND 0x2
77 #define XICS_RM_NOTIFY_EOI 0x8
79 struct kvm_vcpu *rm_kick_target;
80 struct kvmppc_icp *rm_resend_icp;
84 /* Counters for each reason we exited real mode */
85 unsigned long n_rm_kick_vcpu;
86 unsigned long n_rm_check_resend;
87 unsigned long n_rm_notify_eoi;
88 /* Counters for handling ICP processing in real mode */
89 unsigned long n_check_resend;
90 unsigned long n_reject;
92 /* Debug stuff for real mode */
93 union kvmppc_icp_state rm_dbgstate;
94 struct kvm_vcpu *rm_dbgtgt;
100 struct ics_irq_state irq_state[KVMPPC_XICS_IRQ_PER_ICS];
105 struct kvm_device *dev;
106 struct dentry *dentry;
112 struct kvmppc_ics *ics[KVMPPC_XICS_MAX_ICS_ID + 1];
115 static inline struct kvmppc_icp *kvmppc_xics_find_server(struct kvm *kvm,
118 struct kvm_vcpu *vcpu = NULL;
121 kvm_for_each_vcpu(i, vcpu, kvm) {
122 if (vcpu->arch.icp && nr == vcpu->arch.icp->server_num)
123 return vcpu->arch.icp;
128 static inline struct kvmppc_ics *kvmppc_xics_find_ics(struct kvmppc_xics *xics,
129 u32 irq, u16 *source)
131 u32 icsid = irq >> KVMPPC_XICS_ICS_SHIFT;
132 u16 src = irq & KVMPPC_XICS_SRC_MASK;
133 struct kvmppc_ics *ics;
137 if (icsid > KVMPPC_XICS_MAX_ICS_ID)
139 ics = xics->ics[icsid];
145 extern unsigned long xics_rm_h_xirr(struct kvm_vcpu *vcpu);
146 extern int xics_rm_h_ipi(struct kvm_vcpu *vcpu, unsigned long server,
148 extern int xics_rm_h_cppr(struct kvm_vcpu *vcpu, unsigned long cppr);
149 extern int xics_rm_h_eoi(struct kvm_vcpu *vcpu, unsigned long xirr);
151 #endif /* CONFIG_KVM_XICS */
152 #endif /* _KVM_PPC_BOOK3S_XICS_H */