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