xref: /openbmc/qemu/target/arm/kvm_arm.h (revision f480f6e8)
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 /**
19  * kvm_arm_vcpu_init:
20  * @cs: CPUState
21  *
22  * Initialize (or reinitialize) the VCPU by invoking the
23  * KVM_ARM_VCPU_INIT ioctl with the CPU type and feature
24  * bitmask specified in the CPUState.
25  *
26  * Returns: 0 if success else < 0 error code
27  */
28 int kvm_arm_vcpu_init(CPUState *cs);
29 
30 /**
31  * kvm_arm_vcpu_finalize
32  * @cs: CPUState
33  * @feature: int
34  *
35  * Finalizes the configuration of the specified VCPU feature by
36  * invoking the KVM_ARM_VCPU_FINALIZE ioctl. Features requiring
37  * this are documented in the "KVM_ARM_VCPU_FINALIZE" section of
38  * KVM's API documentation.
39  *
40  * Returns: 0 if success else < 0 error code
41  */
42 int kvm_arm_vcpu_finalize(CPUState *cs, int feature);
43 
44 /**
45  * kvm_arm_register_device:
46  * @mr: memory region for this device
47  * @devid: the KVM device ID
48  * @group: device control API group for setting addresses
49  * @attr: device control API address type
50  * @dev_fd: device control device file descriptor (or -1 if not supported)
51  * @addr_ormask: value to be OR'ed with resolved address
52  *
53  * Remember the memory region @mr, and when it is mapped by the
54  * machine model, tell the kernel that base address using the
55  * KVM_ARM_SET_DEVICE_ADDRESS ioctl or the newer device control API.  @devid
56  * should be the ID of the device as defined by KVM_ARM_SET_DEVICE_ADDRESS or
57  * the arm-vgic device in the device control API.
58  * The machine model may map
59  * and unmap the device multiple times; the kernel will only be told the final
60  * address at the point where machine init is complete.
61  */
62 void kvm_arm_register_device(MemoryRegion *mr, uint64_t devid, uint64_t group,
63                              uint64_t attr, int dev_fd, uint64_t addr_ormask);
64 
65 /**
66  * kvm_arm_init_cpreg_list:
67  * @cpu: ARMCPU
68  *
69  * Initialize the ARMCPU cpreg list according to the kernel's
70  * definition of what CPU registers it knows about (and throw away
71  * the previous TCG-created cpreg list).
72  *
73  * Returns: 0 if success, else < 0 error code
74  */
75 int kvm_arm_init_cpreg_list(ARMCPU *cpu);
76 
77 /**
78  * kvm_arm_reg_syncs_via_cpreg_list
79  * regidx: KVM register index
80  *
81  * Return true if this KVM register should be synchronized via the
82  * cpreg list of arbitrary system registers, false if it is synchronized
83  * by hand using code in kvm_arch_get/put_registers().
84  */
85 bool kvm_arm_reg_syncs_via_cpreg_list(uint64_t regidx);
86 
87 /**
88  * kvm_arm_cpreg_level
89  * regidx: KVM register index
90  *
91  * Return the level of this coprocessor/system register.  Return value is
92  * either KVM_PUT_RUNTIME_STATE, KVM_PUT_RESET_STATE, or KVM_PUT_FULL_STATE.
93  */
94 int kvm_arm_cpreg_level(uint64_t regidx);
95 
96 /**
97  * write_list_to_kvmstate:
98  * @cpu: ARMCPU
99  * @level: the state level to sync
100  *
101  * For each register listed in the ARMCPU cpreg_indexes list, write
102  * its value from the cpreg_values list into the kernel (via ioctl).
103  * This updates KVM's working data structures from TCG data or
104  * from incoming migration state.
105  *
106  * Returns: true if all register values were updated correctly,
107  * false if some register was unknown to the kernel or could not
108  * be written (eg constant register with the wrong value).
109  * Note that we do not stop early on failure -- we will attempt
110  * writing all registers in the list.
111  */
112 bool write_list_to_kvmstate(ARMCPU *cpu, int level);
113 
114 /**
115  * write_kvmstate_to_list:
116  * @cpu: ARMCPU
117  *
118  * For each register listed in the ARMCPU cpreg_indexes list, write
119  * its value from the kernel into the cpreg_values list. This is used to
120  * copy info from KVM's working data structures into TCG or
121  * for outbound migration.
122  *
123  * Returns: true if all register values were read correctly,
124  * false if some register was unknown or could not be read.
125  * Note that we do not stop early on failure -- we will attempt
126  * reading all registers in the list.
127  */
128 bool write_kvmstate_to_list(ARMCPU *cpu);
129 
130 /**
131  * kvm_arm_reset_vcpu:
132  * @cpu: ARMCPU
133  *
134  * Called at reset time to kernel registers to their initial values.
135  */
136 void kvm_arm_reset_vcpu(ARMCPU *cpu);
137 
138 /**
139  * kvm_arm_init_serror_injection:
140  * @cs: CPUState
141  *
142  * Check whether KVM can set guest SError syndrome.
143  */
144 void kvm_arm_init_serror_injection(CPUState *cs);
145 
146 /**
147  * kvm_get_vcpu_events:
148  * @cpu: ARMCPU
149  *
150  * Get VCPU related state from kvm.
151  */
152 int kvm_get_vcpu_events(ARMCPU *cpu);
153 
154 /**
155  * kvm_put_vcpu_events:
156  * @cpu: ARMCPU
157  *
158  * Put VCPU related state to kvm.
159  */
160 int kvm_put_vcpu_events(ARMCPU *cpu);
161 
162 #ifdef CONFIG_KVM
163 /**
164  * kvm_arm_create_scratch_host_vcpu:
165  * @cpus_to_try: array of QEMU_KVM_ARM_TARGET_* values (terminated with
166  * QEMU_KVM_ARM_TARGET_NONE) to try as fallback if the kernel does not
167  * know the PREFERRED_TARGET ioctl. Passing NULL is the same as passing
168  * an empty array.
169  * @fdarray: filled in with kvmfd, vmfd, cpufd file descriptors in that order
170  * @init: filled in with the necessary values for creating a host
171  * vcpu. If NULL is provided, will not init the vCPU (though the cpufd
172  * will still be set up).
173  *
174  * Create a scratch vcpu in its own VM of the type preferred by the host
175  * kernel (as would be used for '-cpu host'), for purposes of probing it
176  * for capabilities.
177  *
178  * Returns: true on success (and fdarray and init are filled in),
179  * false on failure (and fdarray and init are not valid).
180  */
181 bool kvm_arm_create_scratch_host_vcpu(const uint32_t *cpus_to_try,
182                                       int *fdarray,
183                                       struct kvm_vcpu_init *init);
184 
185 /**
186  * kvm_arm_destroy_scratch_host_vcpu:
187  * @fdarray: array of fds as set up by kvm_arm_create_scratch_host_vcpu
188  *
189  * Tear down the scratch vcpu created by kvm_arm_create_scratch_host_vcpu.
190  */
191 void kvm_arm_destroy_scratch_host_vcpu(int *fdarray);
192 
193 #define TYPE_ARM_HOST_CPU "host-" TYPE_ARM_CPU
194 
195 /**
196  * ARMHostCPUFeatures: information about the host CPU (identified
197  * by asking the host kernel)
198  */
199 typedef struct ARMHostCPUFeatures {
200     ARMISARegisters isar;
201     uint64_t features;
202     uint32_t target;
203     const char *dtb_compatible;
204 } ARMHostCPUFeatures;
205 
206 /**
207  * kvm_arm_get_host_cpu_features:
208  * @ahcc: ARMHostCPUClass to fill in
209  *
210  * Probe the capabilities of the host kernel's preferred CPU and fill
211  * in the ARMHostCPUClass struct accordingly.
212  */
213 bool kvm_arm_get_host_cpu_features(ARMHostCPUFeatures *ahcf);
214 
215 /**
216  * kvm_arm_sve_get_vls:
217  * @cs: CPUState
218  * @map: bitmap to fill in
219  *
220  * Get all the SVE vector lengths supported by the KVM host, setting
221  * the bits corresponding to their length in quadwords minus one
222  * (vq - 1) in @map up to ARM_MAX_VQ.
223  */
224 void kvm_arm_sve_get_vls(CPUState *cs, unsigned long *map);
225 
226 /**
227  * kvm_arm_set_cpu_features_from_host:
228  * @cpu: ARMCPU to set the features for
229  *
230  * Set up the ARMCPU struct fields up to match the information probed
231  * from the host CPU.
232  */
233 void kvm_arm_set_cpu_features_from_host(ARMCPU *cpu);
234 
235 /**
236  * kvm_arm_aarch32_supported:
237  * @cs: CPUState
238  *
239  * Returns: true if the KVM VCPU can enable AArch32 mode
240  * and false otherwise.
241  */
242 bool kvm_arm_aarch32_supported(CPUState *cs);
243 
244 /**
245  * bool kvm_arm_pmu_supported:
246  * @cs: CPUState
247  *
248  * Returns: true if the KVM VCPU can enable its PMU
249  * and false otherwise.
250  */
251 bool kvm_arm_pmu_supported(CPUState *cs);
252 
253 /**
254  * bool kvm_arm_sve_supported:
255  * @cs: CPUState
256  *
257  * Returns true if the KVM VCPU can enable SVE and false otherwise.
258  */
259 bool kvm_arm_sve_supported(CPUState *cs);
260 
261 /**
262  * kvm_arm_get_max_vm_ipa_size - Returns the number of bits in the
263  * IPA address space supported by KVM
264  *
265  * @ms: Machine state handle
266  */
267 int kvm_arm_get_max_vm_ipa_size(MachineState *ms);
268 
269 /**
270  * kvm_arm_sync_mpstate_to_kvm
271  * @cpu: ARMCPU
272  *
273  * If supported set the KVM MP_STATE based on QEMU's model.
274  */
275 int kvm_arm_sync_mpstate_to_kvm(ARMCPU *cpu);
276 
277 /**
278  * kvm_arm_sync_mpstate_to_qemu
279  * @cpu: ARMCPU
280  *
281  * If supported get the MP_STATE from KVM and store in QEMU's model.
282  */
283 int kvm_arm_sync_mpstate_to_qemu(ARMCPU *cpu);
284 
285 int kvm_arm_vgic_probe(void);
286 
287 void kvm_arm_pmu_set_irq(CPUState *cs, int irq);
288 void kvm_arm_pmu_init(CPUState *cs);
289 int kvm_arm_set_irq(int cpu, int irqtype, int irq, int level);
290 
291 #else
292 
293 static inline void kvm_arm_set_cpu_features_from_host(ARMCPU *cpu)
294 {
295     /* This should never actually be called in the "not KVM" case,
296      * but set up the fields to indicate an error anyway.
297      */
298     cpu->kvm_target = QEMU_KVM_ARM_TARGET_NONE;
299     cpu->host_cpu_probe_failed = true;
300 }
301 
302 static inline bool kvm_arm_aarch32_supported(CPUState *cs)
303 {
304     return false;
305 }
306 
307 static inline bool kvm_arm_pmu_supported(CPUState *cs)
308 {
309     return false;
310 }
311 
312 static inline bool kvm_arm_sve_supported(CPUState *cs)
313 {
314     return false;
315 }
316 
317 static inline int kvm_arm_get_max_vm_ipa_size(MachineState *ms)
318 {
319     return -ENOENT;
320 }
321 
322 static inline int kvm_arm_vgic_probe(void)
323 {
324     return 0;
325 }
326 
327 static inline void kvm_arm_pmu_set_irq(CPUState *cs, int irq) {}
328 static inline void kvm_arm_pmu_init(CPUState *cs) {}
329 
330 static inline void kvm_arm_sve_get_vls(CPUState *cs, unsigned long *map) {}
331 #endif
332 
333 static inline const char *gic_class_name(void)
334 {
335     return kvm_irqchip_in_kernel() ? "kvm-arm-gic" : "arm_gic";
336 }
337 
338 /**
339  * gicv3_class_name
340  *
341  * Return name of GICv3 class to use depending on whether KVM acceleration is
342  * in use. May throw an error if the chosen implementation is not available.
343  *
344  * Returns: class name to use
345  */
346 static inline const char *gicv3_class_name(void)
347 {
348     if (kvm_irqchip_in_kernel()) {
349 #ifdef TARGET_AARCH64
350         return "kvm-arm-gicv3";
351 #else
352         error_report("KVM GICv3 acceleration is not supported on this "
353                      "platform");
354         exit(1);
355 #endif
356     } else {
357         if (kvm_enabled()) {
358             error_report("Userspace GICv3 is not supported with KVM");
359             exit(1);
360         }
361         return "arm-gicv3";
362     }
363 }
364 
365 /**
366  * kvm_arm_handle_debug:
367  * @cs: CPUState
368  * @debug_exit: debug part of the KVM exit structure
369  *
370  * Returns: TRUE if the debug exception was handled.
371  */
372 bool kvm_arm_handle_debug(CPUState *cs, struct kvm_debug_exit_arch *debug_exit);
373 
374 /**
375  * kvm_arm_hw_debug_active:
376  * @cs: CPU State
377  *
378  * Return: TRUE if any hardware breakpoints in use.
379  */
380 
381 bool kvm_arm_hw_debug_active(CPUState *cs);
382 
383 /**
384  * kvm_arm_copy_hw_debug_data:
385  *
386  * @ptr: kvm_guest_debug_arch structure
387  *
388  * Copy the architecture specific debug registers into the
389  * kvm_guest_debug ioctl structure.
390  */
391 struct kvm_guest_debug_arch;
392 
393 void kvm_arm_copy_hw_debug_data(struct kvm_guest_debug_arch *ptr);
394 
395 /**
396  * its_class_name
397  *
398  * Return the ITS class name to use depending on whether KVM acceleration
399  * and KVM CAP_SIGNAL_MSI are supported
400  *
401  * Returns: class name to use or NULL
402  */
403 static inline const char *its_class_name(void)
404 {
405     if (kvm_irqchip_in_kernel()) {
406         /* KVM implementation requires this capability */
407         return kvm_direct_msi_enabled() ? "arm-its-kvm" : NULL;
408     } else {
409         /* Software emulation is not implemented yet */
410         return NULL;
411     }
412 }
413 
414 #endif
415