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 * The XIVE Interrupt source numbers are within the range 0 to
17 * KVMPPC_XICS_NR_IRQS.
19 #define KVMPPC_XIVE_FIRST_IRQ 0
20 #define KVMPPC_XIVE_NR_IRQS KVMPPC_XICS_NR_IRQS
23 * State for one guest irq source.
25 * For each guest source we allocate a HW interrupt in the XIVE
26 * which we use for all SW triggers. It will be unused for
27 * pass-through but it's easier to keep around as the same
28 * guest interrupt can alternatively be emulated or pass-through
29 * if a physical device is hot unplugged and replaced with an
32 * This state structure is very similar to the XICS one with
33 * additional XIVE specific tracking.
35 struct kvmppc_xive_irq_state {
36 bool valid; /* Interrupt entry is valid */
38 u32 number; /* Guest IRQ number */
39 u32 ipi_number; /* XIVE IPI HW number */
40 struct xive_irq_data ipi_data; /* XIVE IPI associated data */
41 u32 pt_number; /* XIVE Pass-through number if any */
42 struct xive_irq_data *pt_data; /* XIVE Pass-through associated data */
44 /* Targetting as set by guest */
45 u8 guest_priority; /* Guest set priority */
46 u8 saved_priority; /* Saved priority when masking */
48 /* Actual targetting */
49 u32 act_server; /* Actual server */
50 u8 act_priority; /* Actual priority */
52 /* Various state bits */
53 bool in_eoi; /* Synchronize with H_EOI */
54 bool old_p; /* P bit state when masking */
55 bool old_q; /* Q bit state when masking */
56 bool lsi; /* level-sensitive interrupt */
57 bool asserted; /* Only for emulated LSI: current state */
59 /* Saved for migration state */
66 u32 eisn; /* Guest Effective IRQ number */
69 /* Select the "right" interrupt (IPI vs. passthrough) */
70 static inline void kvmppc_xive_select_irq(struct kvmppc_xive_irq_state *state,
72 struct xive_irq_data **out_xd)
74 if (state->pt_number) {
76 *out_hw_irq = state->pt_number;
78 *out_xd = state->pt_data;
81 *out_hw_irq = state->ipi_number;
83 *out_xd = &state->ipi_data;
88 * This corresponds to an "ICS" in XICS terminology, we use it
89 * as a mean to break up source information into multiple structures.
91 struct kvmppc_xive_src_block {
94 struct kvmppc_xive_irq_state irq_state[KVMPPC_XICS_IRQ_PER_ICS];
99 struct kvmppc_xive_ops {
100 int (*reset_mapped)(struct kvm *kvm, unsigned long guest_irq);
105 struct kvm_device *dev;
106 struct dentry *dentry;
108 /* VP block associated with the VM */
111 /* Blocks of sources */
112 struct kvmppc_xive_src_block *src_blocks[KVMPPC_XICS_MAX_ICS_ID + 1];
116 * For state save, we lazily scan the queues on the first interrupt
117 * being migrated. We don't have a clean way to reset that flags
118 * so we keep track of the number of valid sources and how many of
119 * them were migrated so we can reset when all of them have been
126 * Some irqs are delayed on restore until the source is created,
127 * keep track here of how many of them
131 /* Which queues (priorities) are in use by the guest */
139 u8 single_escalation;
141 struct kvmppc_xive_ops *ops;
142 struct address_space *mapping;
143 struct mutex mapping_lock;
146 #define KVMPPC_XIVE_Q_COUNT 8
148 struct kvmppc_xive_vcpu {
149 struct kvmppc_xive *xive;
150 struct kvm_vcpu *vcpu;
153 /* Server number. This is the HW CPU ID from a guest perspective */
157 * HW VP corresponding to this VCPU. This is the base of the VP
158 * block plus the server number.
164 /* IPI used for sending ... IPIs */
166 struct xive_irq_data vp_ipi_data;
168 /* Local emulation state */
169 uint8_t cppr; /* guest CPPR */
170 uint8_t hw_cppr;/* Hardware CPPR */
174 /* Each VP has 8 queues though we only provision some */
175 struct xive_q queues[KVMPPC_XIVE_Q_COUNT];
176 u32 esc_virq[KVMPPC_XIVE_Q_COUNT];
177 char *esc_virq_names[KVMPPC_XIVE_Q_COUNT];
179 /* Stash a delayed irq on restore from migration (see set_icp) */
195 static inline struct kvm_vcpu *kvmppc_xive_find_server(struct kvm *kvm, u32 nr)
197 struct kvm_vcpu *vcpu = NULL;
200 kvm_for_each_vcpu(i, vcpu, kvm) {
201 if (vcpu->arch.xive_vcpu && nr == vcpu->arch.xive_vcpu->server_num)
207 static inline struct kvmppc_xive_src_block *kvmppc_xive_find_source(struct kvmppc_xive *xive,
208 u32 irq, u16 *source)
210 u32 bid = irq >> KVMPPC_XICS_ICS_SHIFT;
211 u16 src = irq & KVMPPC_XICS_SRC_MASK;
215 if (bid > KVMPPC_XICS_MAX_ICS_ID)
217 return xive->src_blocks[bid];
220 static inline u32 kvmppc_xive_vp(struct kvmppc_xive *xive, u32 server)
222 return xive->vp_base + kvmppc_pack_vcpu_id(xive->kvm, server);
226 * Mapping between guest priorities and host priorities
229 * Guest request for 0...6 are honored. Guest request for anything
230 * higher results in a priority of 6 being applied.
232 * Similar mapping is done for CPPR values
234 static inline u8 xive_prio_from_guest(u8 prio)
236 if (prio == 0xff || prio < 6)
241 static inline u8 xive_prio_to_guest(u8 prio)
246 static inline u32 __xive_read_eq(__be32 *qpage, u32 msk, u32 *idx, u32 *toggle)
252 cur = be32_to_cpup(qpage + *idx);
253 if ((cur >> 31) == *toggle)
255 *idx = (*idx + 1) & msk;
258 return cur & 0x7fffffff;
261 extern unsigned long xive_rm_h_xirr(struct kvm_vcpu *vcpu);
262 extern unsigned long xive_rm_h_ipoll(struct kvm_vcpu *vcpu, unsigned long server);
263 extern int xive_rm_h_ipi(struct kvm_vcpu *vcpu, unsigned long server,
265 extern int xive_rm_h_cppr(struct kvm_vcpu *vcpu, unsigned long cppr);
266 extern int xive_rm_h_eoi(struct kvm_vcpu *vcpu, unsigned long xirr);
268 extern unsigned long (*__xive_vm_h_xirr)(struct kvm_vcpu *vcpu);
269 extern unsigned long (*__xive_vm_h_ipoll)(struct kvm_vcpu *vcpu, unsigned long server);
270 extern int (*__xive_vm_h_ipi)(struct kvm_vcpu *vcpu, unsigned long server,
272 extern int (*__xive_vm_h_cppr)(struct kvm_vcpu *vcpu, unsigned long cppr);
273 extern int (*__xive_vm_h_eoi)(struct kvm_vcpu *vcpu, unsigned long xirr);
276 * Common Xive routines for XICS-over-XIVE and XIVE native
278 void kvmppc_xive_disable_vcpu_interrupts(struct kvm_vcpu *vcpu);
279 int kvmppc_xive_debug_show_queues(struct seq_file *m, struct kvm_vcpu *vcpu);
280 struct kvmppc_xive_src_block *kvmppc_xive_create_src_block(
281 struct kvmppc_xive *xive, int irq);
282 void kvmppc_xive_free_sources(struct kvmppc_xive_src_block *sb);
283 int kvmppc_xive_select_target(struct kvm *kvm, u32 *server, u8 prio);
284 int kvmppc_xive_attach_escalation(struct kvm_vcpu *vcpu, u8 prio,
285 bool single_escalation);
286 struct kvmppc_xive *kvmppc_xive_get_device(struct kvm *kvm, u32 type);
288 #endif /* CONFIG_KVM_XICS */
289 #endif /* _KVM_PPC_BOOK3S_XICS_H */