xref: /openbmc/qemu/target/arm/kvm_arm.h (revision 77dd098a5e790e3ede0dea5ddd5f690086fe608c)
1 /*
2  * QEMU KVM support -- ARM specific functions.
3  *
4  * Copyright (c) 2012 Linaro Limited
5  *
6  * This work is licensed under the terms of the GNU GPL, version 2 or later.
7  * See the COPYING file in the top-level directory.
8  *
9  */
10 
11 #ifndef QEMU_KVM_ARM_H
12 #define QEMU_KVM_ARM_H
13 
14 #include "sysemu/kvm.h"
15 
16 #define KVM_ARM_VGIC_V2   (1 << 0)
17 #define KVM_ARM_VGIC_V3   (1 << 1)
18 
19 /**
20  * kvm_arm_register_device:
21  * @mr: memory region for this device
22  * @devid: the KVM device ID
23  * @group: device control API group for setting addresses
24  * @attr: device control API address type
25  * @dev_fd: device control device file descriptor (or -1 if not supported)
26  * @addr_ormask: value to be OR'ed with resolved address
27  *
28  * Remember the memory region @mr, and when it is mapped by the
29  * machine model, tell the kernel that base address using the
30  * KVM_ARM_SET_DEVICE_ADDRESS ioctl or the newer device control API.  @devid
31  * should be the ID of the device as defined by KVM_ARM_SET_DEVICE_ADDRESS or
32  * the arm-vgic device in the device control API.
33  * The machine model may map
34  * and unmap the device multiple times; the kernel will only be told the final
35  * address at the point where machine init is complete.
36  */
37 void kvm_arm_register_device(MemoryRegion *mr, uint64_t devid, uint64_t group,
38                              uint64_t attr, int dev_fd, uint64_t addr_ormask);
39 
40 /**
41  * write_list_to_kvmstate:
42  * @cpu: ARMCPU
43  * @level: the state level to sync
44  *
45  * For each register listed in the ARMCPU cpreg_indexes list, write
46  * its value from the cpreg_values list into the kernel (via ioctl).
47  * This updates KVM's working data structures from TCG data or
48  * from incoming migration state.
49  *
50  * Returns: true if all register values were updated correctly,
51  * false if some register was unknown to the kernel or could not
52  * be written (eg constant register with the wrong value).
53  * Note that we do not stop early on failure -- we will attempt
54  * writing all registers in the list.
55  */
56 bool write_list_to_kvmstate(ARMCPU *cpu, int level);
57 
58 /**
59  * write_kvmstate_to_list:
60  * @cpu: ARMCPU
61  *
62  * For each register listed in the ARMCPU cpreg_indexes list, write
63  * its value from the kernel into the cpreg_values list. This is used to
64  * copy info from KVM's working data structures into TCG or
65  * for outbound migration.
66  *
67  * Returns: true if all register values were read correctly,
68  * false if some register was unknown or could not be read.
69  * Note that we do not stop early on failure -- we will attempt
70  * reading all registers in the list.
71  */
72 bool write_kvmstate_to_list(ARMCPU *cpu);
73 
74 /**
75  * kvm_arm_cpu_pre_save:
76  * @cpu: ARMCPU
77  *
78  * Called after write_kvmstate_to_list() from cpu_pre_save() to update
79  * the cpreg list with KVM CPU state.
80  */
81 void kvm_arm_cpu_pre_save(ARMCPU *cpu);
82 
83 /**
84  * kvm_arm_cpu_post_load:
85  * @cpu: ARMCPU
86  *
87  * Called from cpu_post_load() to update KVM CPU state from the cpreg list.
88  */
89 void kvm_arm_cpu_post_load(ARMCPU *cpu);
90 
91 /**
92  * kvm_arm_reset_vcpu:
93  * @cpu: ARMCPU
94  *
95  * Called at reset time to kernel registers to their initial values.
96  */
97 void kvm_arm_reset_vcpu(ARMCPU *cpu);
98 
99 #ifdef CONFIG_KVM
100 /**
101  * kvm_arm_create_scratch_host_vcpu:
102  * @cpus_to_try: array of QEMU_KVM_ARM_TARGET_* values (terminated with
103  * QEMU_KVM_ARM_TARGET_NONE) to try as fallback if the kernel does not
104  * know the PREFERRED_TARGET ioctl. Passing NULL is the same as passing
105  * an empty array.
106  * @fdarray: filled in with kvmfd, vmfd, cpufd file descriptors in that order
107  * @init: filled in with the necessary values for creating a host
108  * vcpu. If NULL is provided, will not init the vCPU (though the cpufd
109  * will still be set up).
110  *
111  * Create a scratch vcpu in its own VM of the type preferred by the host
112  * kernel (as would be used for '-cpu host'), for purposes of probing it
113  * for capabilities.
114  *
115  * Returns: true on success (and fdarray and init are filled in),
116  * false on failure (and fdarray and init are not valid).
117  */
118 bool kvm_arm_create_scratch_host_vcpu(const uint32_t *cpus_to_try,
119                                       int *fdarray,
120                                       struct kvm_vcpu_init *init);
121 
122 /**
123  * kvm_arm_destroy_scratch_host_vcpu:
124  * @fdarray: array of fds as set up by kvm_arm_create_scratch_host_vcpu
125  *
126  * Tear down the scratch vcpu created by kvm_arm_create_scratch_host_vcpu.
127  */
128 void kvm_arm_destroy_scratch_host_vcpu(int *fdarray);
129 
130 /**
131  * kvm_arm_sve_get_vls:
132  * @cpu: ARMCPU
133  *
134  * Get all the SVE vector lengths supported by the KVM host, setting
135  * the bits corresponding to their length in quadwords minus one
136  * (vq - 1) up to ARM_MAX_VQ.  Return the resulting map.
137  */
138 uint32_t kvm_arm_sve_get_vls(ARMCPU *cpu);
139 
140 /**
141  * kvm_arm_set_cpu_features_from_host:
142  * @cpu: ARMCPU to set the features for
143  *
144  * Set up the ARMCPU struct fields up to match the information probed
145  * from the host CPU.
146  */
147 void kvm_arm_set_cpu_features_from_host(ARMCPU *cpu);
148 
149 /**
150  * kvm_arm_add_vcpu_properties:
151  * @cpu: The CPU object to add the properties to
152  *
153  * Add all KVM specific CPU properties to the CPU object. These
154  * are the CPU properties with "kvm-" prefixed names.
155  */
156 void kvm_arm_add_vcpu_properties(ARMCPU *cpu);
157 
158 /**
159  * kvm_arm_steal_time_finalize:
160  * @cpu: ARMCPU for which to finalize kvm-steal-time
161  * @errp: Pointer to Error* for error propagation
162  *
163  * Validate the kvm-steal-time property selection and set its default
164  * based on KVM support and guest configuration.
165  */
166 void kvm_arm_steal_time_finalize(ARMCPU *cpu, Error **errp);
167 
168 /**
169  * kvm_arm_aarch32_supported:
170  *
171  * Returns: true if KVM can enable AArch32 mode
172  * and false otherwise.
173  */
174 bool kvm_arm_aarch32_supported(void);
175 
176 /**
177  * kvm_arm_pmu_supported:
178  *
179  * Returns: true if KVM can enable the PMU
180  * and false otherwise.
181  */
182 bool kvm_arm_pmu_supported(void);
183 
184 /**
185  * kvm_arm_sve_supported:
186  *
187  * Returns true if KVM can enable SVE and false otherwise.
188  */
189 bool kvm_arm_sve_supported(void);
190 
191 /**
192  * kvm_arm_mte_supported:
193  *
194  * Returns: true if KVM can enable MTE, and false otherwise.
195  */
196 bool kvm_arm_mte_supported(void);
197 
198 /**
199  * kvm_arm_get_max_vm_ipa_size:
200  * @ms: Machine state handle
201  * @fixed_ipa: True when the IPA limit is fixed at 40. This is the case
202  * for legacy KVM.
203  *
204  * Returns the number of bits in the IPA address space supported by KVM
205  */
206 int kvm_arm_get_max_vm_ipa_size(MachineState *ms, bool *fixed_ipa);
207 
208 int kvm_arm_vgic_probe(void);
209 
210 void kvm_arm_pmu_init(ARMCPU *cpu);
211 void kvm_arm_pmu_set_irq(ARMCPU *cpu, int irq);
212 
213 /**
214  * kvm_arm_pvtime_init:
215  * @cpu: ARMCPU
216  * @ipa: Per-vcpu guest physical base address of the pvtime structures
217  *
218  * Initializes PVTIME for the VCPU, setting the PVTIME IPA to @ipa.
219  */
220 void kvm_arm_pvtime_init(ARMCPU *cpu, uint64_t ipa);
221 
222 int kvm_arm_set_irq(int cpu, int irqtype, int irq, int level);
223 
224 void kvm_arm_enable_mte(Object *cpuobj, Error **errp);
225 
226 #else
227 
228 /*
229  * It's safe to call these functions without KVM support.
230  * They should either do nothing or return "not supported".
231  */
232 static inline bool kvm_arm_aarch32_supported(void)
233 {
234     return false;
235 }
236 
237 static inline bool kvm_arm_pmu_supported(void)
238 {
239     return false;
240 }
241 
242 static inline bool kvm_arm_sve_supported(void)
243 {
244     return false;
245 }
246 
247 static inline bool kvm_arm_mte_supported(void)
248 {
249     return false;
250 }
251 
252 /*
253  * These functions should never actually be called without KVM support.
254  */
255 static inline void kvm_arm_set_cpu_features_from_host(ARMCPU *cpu)
256 {
257     g_assert_not_reached();
258 }
259 
260 static inline void kvm_arm_add_vcpu_properties(ARMCPU *cpu)
261 {
262     g_assert_not_reached();
263 }
264 
265 static inline int kvm_arm_get_max_vm_ipa_size(MachineState *ms, bool *fixed_ipa)
266 {
267     g_assert_not_reached();
268 }
269 
270 static inline int kvm_arm_vgic_probe(void)
271 {
272     g_assert_not_reached();
273 }
274 
275 static inline void kvm_arm_pmu_set_irq(ARMCPU *cpu, int irq)
276 {
277     g_assert_not_reached();
278 }
279 
280 static inline void kvm_arm_pmu_init(ARMCPU *cpu)
281 {
282     g_assert_not_reached();
283 }
284 
285 static inline void kvm_arm_pvtime_init(ARMCPU *cpu, uint64_t ipa)
286 {
287     g_assert_not_reached();
288 }
289 
290 static inline void kvm_arm_steal_time_finalize(ARMCPU *cpu, Error **errp)
291 {
292     g_assert_not_reached();
293 }
294 
295 static inline uint32_t kvm_arm_sve_get_vls(ARMCPU *cpu)
296 {
297     g_assert_not_reached();
298 }
299 
300 static inline void kvm_arm_enable_mte(Object *cpuobj, Error **errp)
301 {
302     g_assert_not_reached();
303 }
304 
305 #endif
306 
307 #endif
308