2 * Copyright 2017 Benjamin Herrenschmidt, IBM Corporation
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2, as
6 * published by the Free Software Foundation.
9 #ifndef _KVM_PPC_BOOK3S_XIVE_H
10 #define _KVM_PPC_BOOK3S_XIVE_H
12 #ifdef CONFIG_KVM_XICS
13 #include "book3s_xics.h"
16 * State for one guest irq source.
18 * For each guest source we allocate a HW interrupt in the XIVE
19 * which we use for all SW triggers. It will be unused for
20 * pass-through but it's easier to keep around as the same
21 * guest interrupt can alternatively be emulated or pass-through
22 * if a physical device is hot unplugged and replaced with an
25 * This state structure is very similar to the XICS one with
26 * additional XIVE specific tracking.
28 struct kvmppc_xive_irq_state {
29 bool valid; /* Interrupt entry is valid */
31 u32 number; /* Guest IRQ number */
32 u32 ipi_number; /* XIVE IPI HW number */
33 struct xive_irq_data ipi_data; /* XIVE IPI associated data */
34 u32 pt_number; /* XIVE Pass-through number if any */
35 struct xive_irq_data *pt_data; /* XIVE Pass-through associated data */
37 /* Targetting as set by guest */
38 u8 guest_priority; /* Guest set priority */
39 u8 saved_priority; /* Saved priority when masking */
41 /* Actual targetting */
42 u32 act_server; /* Actual server */
43 u8 act_priority; /* Actual priority */
45 /* Various state bits */
46 bool in_eoi; /* Synchronize with H_EOI */
47 bool old_p; /* P bit state when masking */
48 bool old_q; /* Q bit state when masking */
49 bool lsi; /* level-sensitive interrupt */
50 bool asserted; /* Only for emulated LSI: current state */
52 /* Saved for migration state */
59 /* Select the "right" interrupt (IPI vs. passthrough) */
60 static inline void kvmppc_xive_select_irq(struct kvmppc_xive_irq_state *state,
62 struct xive_irq_data **out_xd)
64 if (state->pt_number) {
66 *out_hw_irq = state->pt_number;
68 *out_xd = state->pt_data;
71 *out_hw_irq = state->ipi_number;
73 *out_xd = &state->ipi_data;
78 * This corresponds to an "ICS" in XICS terminology, we use it
79 * as a mean to break up source information into multiple structures.
81 struct kvmppc_xive_src_block {
84 struct kvmppc_xive_irq_state irq_state[KVMPPC_XICS_IRQ_PER_ICS];
90 struct kvm_device *dev;
91 struct dentry *dentry;
93 /* VP block associated with the VM */
96 /* Blocks of sources */
97 struct kvmppc_xive_src_block *src_blocks[KVMPPC_XICS_MAX_ICS_ID + 1];
101 * For state save, we lazily scan the queues on the first interrupt
102 * being migrated. We don't have a clean way to reset that flags
103 * so we keep track of the number of valid sources and how many of
104 * them were migrated so we can reset when all of them have been
111 * Some irqs are delayed on restore until the source is created,
112 * keep track here of how many of them
116 /* Which queues (priorities) are in use by the guest */
125 #define KVMPPC_XIVE_Q_COUNT 8
127 struct kvmppc_xive_vcpu {
128 struct kvmppc_xive *xive;
129 struct kvm_vcpu *vcpu;
132 /* Server number. This is the HW CPU ID from a guest perspective */
136 * HW VP corresponding to this VCPU. This is the base of the VP
137 * block plus the server number.
143 /* IPI used for sending ... IPIs */
145 struct xive_irq_data vp_ipi_data;
147 /* Local emulation state */
148 uint8_t cppr; /* guest CPPR */
149 uint8_t hw_cppr;/* Hardware CPPR */
153 /* Each VP has 8 queues though we only provision some */
154 struct xive_q queues[KVMPPC_XIVE_Q_COUNT];
155 u32 esc_virq[KVMPPC_XIVE_Q_COUNT];
156 char *esc_virq_names[KVMPPC_XIVE_Q_COUNT];
158 /* Stash a delayed irq on restore from migration (see set_icp) */
174 static inline struct kvm_vcpu *kvmppc_xive_find_server(struct kvm *kvm, u32 nr)
176 struct kvm_vcpu *vcpu = NULL;
179 kvm_for_each_vcpu(i, vcpu, kvm) {
180 if (vcpu->arch.xive_vcpu && nr == vcpu->arch.xive_vcpu->server_num)
186 static inline struct kvmppc_xive_src_block *kvmppc_xive_find_source(struct kvmppc_xive *xive,
187 u32 irq, u16 *source)
189 u32 bid = irq >> KVMPPC_XICS_ICS_SHIFT;
190 u16 src = irq & KVMPPC_XICS_SRC_MASK;
194 if (bid > KVMPPC_XICS_MAX_ICS_ID)
196 return xive->src_blocks[bid];
200 * Mapping between guest priorities and host priorities
203 * Guest request for 0...6 are honored. Guest request for anything
204 * higher results in a priority of 7 being applied.
206 * However, when XIRR is returned via H_XIRR, 7 is translated to 0xb
207 * in order to match AIX expectations
209 * Similar mapping is done for CPPR values
211 static inline u8 xive_prio_from_guest(u8 prio)
213 if (prio == 0xff || prio < 8)
218 static inline u8 xive_prio_to_guest(u8 prio)
220 if (prio == 0xff || prio < 7)
225 static inline u32 __xive_read_eq(__be32 *qpage, u32 msk, u32 *idx, u32 *toggle)
231 cur = be32_to_cpup(qpage + *idx);
232 if ((cur >> 31) == *toggle)
234 *idx = (*idx + 1) & msk;
237 return cur & 0x7fffffff;
240 extern unsigned long xive_rm_h_xirr(struct kvm_vcpu *vcpu);
241 extern unsigned long xive_rm_h_ipoll(struct kvm_vcpu *vcpu, unsigned long server);
242 extern int xive_rm_h_ipi(struct kvm_vcpu *vcpu, unsigned long server,
244 extern int xive_rm_h_cppr(struct kvm_vcpu *vcpu, unsigned long cppr);
245 extern int xive_rm_h_eoi(struct kvm_vcpu *vcpu, unsigned long xirr);
247 extern unsigned long (*__xive_vm_h_xirr)(struct kvm_vcpu *vcpu);
248 extern unsigned long (*__xive_vm_h_ipoll)(struct kvm_vcpu *vcpu, unsigned long server);
249 extern int (*__xive_vm_h_ipi)(struct kvm_vcpu *vcpu, unsigned long server,
251 extern int (*__xive_vm_h_cppr)(struct kvm_vcpu *vcpu, unsigned long cppr);
252 extern int (*__xive_vm_h_eoi)(struct kvm_vcpu *vcpu, unsigned long xirr);
254 #endif /* CONFIG_KVM_XICS */
255 #endif /* _KVM_PPC_BOOK3S_XICS_H */