blob: ae26fe653d98952b229bc413028e4c913027ea31 [file] [log] [blame]
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001/*
2 * Copyright 2017 Benjamin Herrenschmidt, IBM Corporation
3 *
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.
7 */
8
9#ifndef _KVM_PPC_BOOK3S_XIVE_H
10#define _KVM_PPC_BOOK3S_XIVE_H
11
12#ifdef CONFIG_KVM_XICS
13#include "book3s_xics.h"
14
15/*
Cédric Le Goater4131f832019-04-18 12:39:29 +020016 * The XIVE Interrupt source numbers are within the range 0 to
17 * KVMPPC_XICS_NR_IRQS.
18 */
19#define KVMPPC_XIVE_FIRST_IRQ 0
20#define KVMPPC_XIVE_NR_IRQS KVMPPC_XICS_NR_IRQS
21
22/*
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +100023 * State for one guest irq source.
24 *
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
30 * emulated one.
31 *
32 * This state structure is very similar to the XICS one with
33 * additional XIVE specific tracking.
34 */
35struct kvmppc_xive_irq_state {
36 bool valid; /* Interrupt entry is valid */
37
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 */
43
44 /* Targetting as set by guest */
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +100045 u8 guest_priority; /* Guest set priority */
46 u8 saved_priority; /* Saved priority when masking */
47
48 /* Actual targetting */
49 u32 act_server; /* Actual server */
50 u8 act_priority; /* Actual priority */
51
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 */
58
59 /* Saved for migration state */
60 bool in_queue;
61 bool saved_p;
62 bool saved_q;
63 u8 saved_scan_prio;
Cédric Le Goatere8676ce2019-04-18 12:39:30 +020064
65 /* Xive native */
66 u32 eisn; /* Guest Effective IRQ number */
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +100067};
68
69/* Select the "right" interrupt (IPI vs. passthrough) */
70static inline void kvmppc_xive_select_irq(struct kvmppc_xive_irq_state *state,
71 u32 *out_hw_irq,
72 struct xive_irq_data **out_xd)
73{
74 if (state->pt_number) {
75 if (out_hw_irq)
76 *out_hw_irq = state->pt_number;
77 if (out_xd)
78 *out_xd = state->pt_data;
79 } else {
80 if (out_hw_irq)
81 *out_hw_irq = state->ipi_number;
82 if (out_xd)
83 *out_xd = &state->ipi_data;
84 }
85}
86
87/*
88 * This corresponds to an "ICS" in XICS terminology, we use it
89 * as a mean to break up source information into multiple structures.
90 */
91struct kvmppc_xive_src_block {
92 arch_spinlock_t lock;
93 u16 id;
94 struct kvmppc_xive_irq_state irq_state[KVMPPC_XICS_IRQ_PER_ICS];
95};
96
97
98struct kvmppc_xive {
99 struct kvm *kvm;
100 struct kvm_device *dev;
101 struct dentry *dentry;
102
103 /* VP block associated with the VM */
104 u32 vp_base;
105
106 /* Blocks of sources */
107 struct kvmppc_xive_src_block *src_blocks[KVMPPC_XICS_MAX_ICS_ID + 1];
108 u32 max_sbid;
109
110 /*
111 * For state save, we lazily scan the queues on the first interrupt
112 * being migrated. We don't have a clean way to reset that flags
113 * so we keep track of the number of valid sources and how many of
114 * them were migrated so we can reset when all of them have been
115 * processed.
116 */
117 u32 src_count;
118 u32 saved_src_count;
119
120 /*
121 * Some irqs are delayed on restore until the source is created,
122 * keep track here of how many of them
123 */
124 u32 delayed_irqs;
125
126 /* Which queues (priorities) are in use by the guest */
127 u8 qmap;
128
129 /* Queue orders */
130 u32 q_order;
131 u32 q_page_order;
132
Benjamin Herrenschmidtbf4159da2018-01-12 13:37:12 +1100133 /* Flags */
134 u8 single_escalation;
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000135};
136
137#define KVMPPC_XIVE_Q_COUNT 8
138
139struct kvmppc_xive_vcpu {
140 struct kvmppc_xive *xive;
141 struct kvm_vcpu *vcpu;
142 bool valid;
143
144 /* Server number. This is the HW CPU ID from a guest perspective */
145 u32 server_num;
146
147 /*
148 * HW VP corresponding to this VCPU. This is the base of the VP
149 * block plus the server number.
150 */
151 u32 vp_id;
152 u32 vp_chip_id;
153 u32 vp_cam;
154
155 /* IPI used for sending ... IPIs */
156 u32 vp_ipi;
157 struct xive_irq_data vp_ipi_data;
158
159 /* Local emulation state */
160 uint8_t cppr; /* guest CPPR */
161 uint8_t hw_cppr;/* Hardware CPPR */
162 uint8_t mfrr;
163 uint8_t pending;
164
165 /* Each VP has 8 queues though we only provision some */
166 struct xive_q queues[KVMPPC_XIVE_Q_COUNT];
167 u32 esc_virq[KVMPPC_XIVE_Q_COUNT];
168 char *esc_virq_names[KVMPPC_XIVE_Q_COUNT];
169
170 /* Stash a delayed irq on restore from migration (see set_icp) */
171 u32 delayed_irq;
172
173 /* Stats */
174 u64 stat_rm_h_xirr;
175 u64 stat_rm_h_ipoll;
176 u64 stat_rm_h_cppr;
177 u64 stat_rm_h_eoi;
178 u64 stat_rm_h_ipi;
179 u64 stat_vm_h_xirr;
180 u64 stat_vm_h_ipoll;
181 u64 stat_vm_h_cppr;
182 u64 stat_vm_h_eoi;
183 u64 stat_vm_h_ipi;
184};
185
186static inline struct kvm_vcpu *kvmppc_xive_find_server(struct kvm *kvm, u32 nr)
187{
188 struct kvm_vcpu *vcpu = NULL;
189 int i;
190
191 kvm_for_each_vcpu(i, vcpu, kvm) {
192 if (vcpu->arch.xive_vcpu && nr == vcpu->arch.xive_vcpu->server_num)
193 return vcpu;
194 }
195 return NULL;
196}
197
198static inline struct kvmppc_xive_src_block *kvmppc_xive_find_source(struct kvmppc_xive *xive,
199 u32 irq, u16 *source)
200{
201 u32 bid = irq >> KVMPPC_XICS_ICS_SHIFT;
202 u16 src = irq & KVMPPC_XICS_SRC_MASK;
203
204 if (source)
205 *source = src;
206 if (bid > KVMPPC_XICS_MAX_ICS_ID)
207 return NULL;
208 return xive->src_blocks[bid];
209}
210
Cédric Le Goatereacc56b2019-04-18 12:39:28 +0200211static inline u32 kvmppc_xive_vp(struct kvmppc_xive *xive, u32 server)
212{
213 return xive->vp_base + kvmppc_pack_vcpu_id(xive->kvm, server);
214}
215
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000216/*
217 * Mapping between guest priorities and host priorities
218 * is as follow.
219 *
220 * Guest request for 0...6 are honored. Guest request for anything
Benjamin Herrenschmidtbf4159da2018-01-12 13:37:12 +1100221 * higher results in a priority of 6 being applied.
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000222 *
223 * Similar mapping is done for CPPR values
224 */
225static inline u8 xive_prio_from_guest(u8 prio)
226{
Benjamin Herrenschmidtbf4159da2018-01-12 13:37:12 +1100227 if (prio == 0xff || prio < 6)
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000228 return prio;
Benjamin Herrenschmidtbf4159da2018-01-12 13:37:12 +1100229 return 6;
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000230}
231
232static inline u8 xive_prio_to_guest(u8 prio)
233{
Benjamin Herrenschmidtbf4159da2018-01-12 13:37:12 +1100234 return prio;
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000235}
236
237static inline u32 __xive_read_eq(__be32 *qpage, u32 msk, u32 *idx, u32 *toggle)
238{
239 u32 cur;
240
241 if (!qpage)
242 return 0;
243 cur = be32_to_cpup(qpage + *idx);
244 if ((cur >> 31) == *toggle)
245 return 0;
246 *idx = (*idx + 1) & msk;
247 if (*idx == 0)
248 (*toggle) ^= 1;
249 return cur & 0x7fffffff;
250}
251
252extern unsigned long xive_rm_h_xirr(struct kvm_vcpu *vcpu);
253extern unsigned long xive_rm_h_ipoll(struct kvm_vcpu *vcpu, unsigned long server);
254extern int xive_rm_h_ipi(struct kvm_vcpu *vcpu, unsigned long server,
255 unsigned long mfrr);
256extern int xive_rm_h_cppr(struct kvm_vcpu *vcpu, unsigned long cppr);
257extern int xive_rm_h_eoi(struct kvm_vcpu *vcpu, unsigned long xirr);
258
259extern unsigned long (*__xive_vm_h_xirr)(struct kvm_vcpu *vcpu);
260extern unsigned long (*__xive_vm_h_ipoll)(struct kvm_vcpu *vcpu, unsigned long server);
261extern int (*__xive_vm_h_ipi)(struct kvm_vcpu *vcpu, unsigned long server,
262 unsigned long mfrr);
263extern int (*__xive_vm_h_cppr)(struct kvm_vcpu *vcpu, unsigned long cppr);
264extern int (*__xive_vm_h_eoi)(struct kvm_vcpu *vcpu, unsigned long xirr);
265
Cédric Le Goatereacc56b2019-04-18 12:39:28 +0200266/*
267 * Common Xive routines for XICS-over-XIVE and XIVE native
268 */
269void kvmppc_xive_disable_vcpu_interrupts(struct kvm_vcpu *vcpu);
270int kvmppc_xive_debug_show_queues(struct seq_file *m, struct kvm_vcpu *vcpu);
Cédric Le Goater4131f832019-04-18 12:39:29 +0200271struct kvmppc_xive_src_block *kvmppc_xive_create_src_block(
272 struct kvmppc_xive *xive, int irq);
273void kvmppc_xive_free_sources(struct kvmppc_xive_src_block *sb);
Cédric Le Goatere8676ce2019-04-18 12:39:30 +0200274int kvmppc_xive_select_target(struct kvm *kvm, u32 *server, u8 prio);
Cédric Le Goatereacc56b2019-04-18 12:39:28 +0200275
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000276#endif /* CONFIG_KVM_XICS */
277#endif /* _KVM_PPC_BOOK3S_XICS_H */