target/arm/cpu: Add the kvm-no-adjvtime CPU property
[qemu/ar7.git] / target / arm / kvm_arm.h
blobae9e075d75454762e45a2008b0623e89c8abee63
1 /*
2 * QEMU KVM support -- ARM specific functions.
4 * Copyright (c) 2012 Linaro Limited
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.
9 */
11 #ifndef QEMU_KVM_ARM_H
12 #define QEMU_KVM_ARM_H
14 #include "sysemu/kvm.h"
15 #include "exec/memory.h"
16 #include "qemu/error-report.h"
18 /**
19 * kvm_arm_vcpu_init:
20 * @cs: CPUState
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.
26 * Returns: 0 if success else < 0 error code
28 int kvm_arm_vcpu_init(CPUState *cs);
30 /**
31 * kvm_arm_vcpu_finalize:
32 * @cs: CPUState
33 * @feature: feature to finalize
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.
40 * Returns: 0 if success else < 0 error code
42 int kvm_arm_vcpu_finalize(CPUState *cs, int feature);
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
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.
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);
65 /**
66 * kvm_arm_init_cpreg_list:
67 * @cpu: ARMCPU
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).
73 * Returns: 0 if success, else < 0 error code
75 int kvm_arm_init_cpreg_list(ARMCPU *cpu);
77 /**
78 * kvm_arm_reg_syncs_via_cpreg_list:
79 * @regidx: KVM register index
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().
85 bool kvm_arm_reg_syncs_via_cpreg_list(uint64_t regidx);
87 /**
88 * kvm_arm_cpreg_level:
89 * @regidx: KVM register index
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.
94 int kvm_arm_cpreg_level(uint64_t regidx);
96 /**
97 * write_list_to_kvmstate:
98 * @cpu: ARMCPU
99 * @level: the state level to sync
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.
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.
112 bool write_list_to_kvmstate(ARMCPU *cpu, int level);
115 * write_kvmstate_to_list:
116 * @cpu: ARMCPU
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.
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.
128 bool write_kvmstate_to_list(ARMCPU *cpu);
131 * kvm_arm_cpu_pre_save:
132 * @cpu: ARMCPU
134 * Called after write_kvmstate_to_list() from cpu_pre_save() to update
135 * the cpreg list with KVM CPU state.
137 void kvm_arm_cpu_pre_save(ARMCPU *cpu);
140 * kvm_arm_cpu_post_load:
141 * @cpu: ARMCPU
143 * Called from cpu_post_load() to update KVM CPU state from the cpreg list.
145 void kvm_arm_cpu_post_load(ARMCPU *cpu);
148 * kvm_arm_reset_vcpu:
149 * @cpu: ARMCPU
151 * Called at reset time to kernel registers to their initial values.
153 void kvm_arm_reset_vcpu(ARMCPU *cpu);
156 * kvm_arm_init_serror_injection:
157 * @cs: CPUState
159 * Check whether KVM can set guest SError syndrome.
161 void kvm_arm_init_serror_injection(CPUState *cs);
164 * kvm_get_vcpu_events:
165 * @cpu: ARMCPU
167 * Get VCPU related state from kvm.
169 * Returns: 0 if success else < 0 error code
171 int kvm_get_vcpu_events(ARMCPU *cpu);
174 * kvm_put_vcpu_events:
175 * @cpu: ARMCPU
177 * Put VCPU related state to kvm.
179 * Returns: 0 if success else < 0 error code
181 int kvm_put_vcpu_events(ARMCPU *cpu);
183 #ifdef CONFIG_KVM
185 * kvm_arm_create_scratch_host_vcpu:
186 * @cpus_to_try: array of QEMU_KVM_ARM_TARGET_* values (terminated with
187 * QEMU_KVM_ARM_TARGET_NONE) to try as fallback if the kernel does not
188 * know the PREFERRED_TARGET ioctl. Passing NULL is the same as passing
189 * an empty array.
190 * @fdarray: filled in with kvmfd, vmfd, cpufd file descriptors in that order
191 * @init: filled in with the necessary values for creating a host
192 * vcpu. If NULL is provided, will not init the vCPU (though the cpufd
193 * will still be set up).
195 * Create a scratch vcpu in its own VM of the type preferred by the host
196 * kernel (as would be used for '-cpu host'), for purposes of probing it
197 * for capabilities.
199 * Returns: true on success (and fdarray and init are filled in),
200 * false on failure (and fdarray and init are not valid).
202 bool kvm_arm_create_scratch_host_vcpu(const uint32_t *cpus_to_try,
203 int *fdarray,
204 struct kvm_vcpu_init *init);
207 * kvm_arm_destroy_scratch_host_vcpu:
208 * @fdarray: array of fds as set up by kvm_arm_create_scratch_host_vcpu
210 * Tear down the scratch vcpu created by kvm_arm_create_scratch_host_vcpu.
212 void kvm_arm_destroy_scratch_host_vcpu(int *fdarray);
214 #define TYPE_ARM_HOST_CPU "host-" TYPE_ARM_CPU
217 * ARMHostCPUFeatures: information about the host CPU (identified
218 * by asking the host kernel)
220 typedef struct ARMHostCPUFeatures {
221 ARMISARegisters isar;
222 uint64_t features;
223 uint32_t target;
224 const char *dtb_compatible;
225 } ARMHostCPUFeatures;
228 * kvm_arm_get_host_cpu_features:
229 * @ahcf: ARMHostCPUClass to fill in
231 * Probe the capabilities of the host kernel's preferred CPU and fill
232 * in the ARMHostCPUClass struct accordingly.
234 * Returns true on success and false otherwise.
236 bool kvm_arm_get_host_cpu_features(ARMHostCPUFeatures *ahcf);
239 * kvm_arm_sve_get_vls:
240 * @cs: CPUState
241 * @map: bitmap to fill in
243 * Get all the SVE vector lengths supported by the KVM host, setting
244 * the bits corresponding to their length in quadwords minus one
245 * (vq - 1) in @map up to ARM_MAX_VQ.
247 void kvm_arm_sve_get_vls(CPUState *cs, unsigned long *map);
250 * kvm_arm_set_cpu_features_from_host:
251 * @cpu: ARMCPU to set the features for
253 * Set up the ARMCPU struct fields up to match the information probed
254 * from the host CPU.
256 void kvm_arm_set_cpu_features_from_host(ARMCPU *cpu);
259 * kvm_arm_add_vcpu_properties:
260 * @obj: The CPU object to add the properties to
262 * Add all KVM specific CPU properties to the CPU object. These
263 * are the CPU properties with "kvm-" prefixed names.
265 void kvm_arm_add_vcpu_properties(Object *obj);
268 * kvm_arm_aarch32_supported:
269 * @cs: CPUState
271 * Returns: true if the KVM VCPU can enable AArch32 mode
272 * and false otherwise.
274 bool kvm_arm_aarch32_supported(CPUState *cs);
277 * kvm_arm_pmu_supported:
278 * @cs: CPUState
280 * Returns: true if the KVM VCPU can enable its PMU
281 * and false otherwise.
283 bool kvm_arm_pmu_supported(CPUState *cs);
286 * kvm_arm_sve_supported:
287 * @cs: CPUState
289 * Returns true if the KVM VCPU can enable SVE and false otherwise.
291 bool kvm_arm_sve_supported(CPUState *cs);
294 * kvm_arm_get_max_vm_ipa_size:
295 * @ms: Machine state handle
297 * Returns the number of bits in the IPA address space supported by KVM
299 int kvm_arm_get_max_vm_ipa_size(MachineState *ms);
302 * kvm_arm_sync_mpstate_to_kvm:
303 * @cpu: ARMCPU
305 * If supported set the KVM MP_STATE based on QEMU's model.
307 * Returns 0 on success and -1 on failure.
309 int kvm_arm_sync_mpstate_to_kvm(ARMCPU *cpu);
312 * kvm_arm_sync_mpstate_to_qemu:
313 * @cpu: ARMCPU
315 * If supported get the MP_STATE from KVM and store in QEMU's model.
317 * Returns 0 on success and aborts on failure.
319 int kvm_arm_sync_mpstate_to_qemu(ARMCPU *cpu);
322 * kvm_arm_get_virtual_time:
323 * @cs: CPUState
325 * Gets the VCPU's virtual counter and stores it in the KVM CPU state.
327 void kvm_arm_get_virtual_time(CPUState *cs);
330 * kvm_arm_put_virtual_time:
331 * @cs: CPUState
333 * Sets the VCPU's virtual counter to the value stored in the KVM CPU state.
335 void kvm_arm_put_virtual_time(CPUState *cs);
337 void kvm_arm_vm_state_change(void *opaque, int running, RunState state);
339 int kvm_arm_vgic_probe(void);
341 void kvm_arm_pmu_set_irq(CPUState *cs, int irq);
342 void kvm_arm_pmu_init(CPUState *cs);
343 int kvm_arm_set_irq(int cpu, int irqtype, int irq, int level);
345 #else
347 static inline void kvm_arm_set_cpu_features_from_host(ARMCPU *cpu)
350 * This should never actually be called in the "not KVM" case,
351 * but set up the fields to indicate an error anyway.
353 cpu->kvm_target = QEMU_KVM_ARM_TARGET_NONE;
354 cpu->host_cpu_probe_failed = true;
357 static inline void kvm_arm_add_vcpu_properties(Object *obj) {}
359 static inline bool kvm_arm_aarch32_supported(CPUState *cs)
361 return false;
364 static inline bool kvm_arm_pmu_supported(CPUState *cs)
366 return false;
369 static inline bool kvm_arm_sve_supported(CPUState *cs)
371 return false;
374 static inline int kvm_arm_get_max_vm_ipa_size(MachineState *ms)
376 return -ENOENT;
379 static inline int kvm_arm_vgic_probe(void)
381 return 0;
384 static inline void kvm_arm_pmu_set_irq(CPUState *cs, int irq) {}
385 static inline void kvm_arm_pmu_init(CPUState *cs) {}
387 static inline void kvm_arm_sve_get_vls(CPUState *cs, unsigned long *map) {}
389 static inline void kvm_arm_get_virtual_time(CPUState *cs) {}
390 static inline void kvm_arm_put_virtual_time(CPUState *cs) {}
391 #endif
393 static inline const char *gic_class_name(void)
395 return kvm_irqchip_in_kernel() ? "kvm-arm-gic" : "arm_gic";
399 * gicv3_class_name
401 * Return name of GICv3 class to use depending on whether KVM acceleration is
402 * in use. May throw an error if the chosen implementation is not available.
404 * Returns: class name to use
406 static inline const char *gicv3_class_name(void)
408 if (kvm_irqchip_in_kernel()) {
409 #ifdef TARGET_AARCH64
410 return "kvm-arm-gicv3";
411 #else
412 error_report("KVM GICv3 acceleration is not supported on this "
413 "platform");
414 exit(1);
415 #endif
416 } else {
417 if (kvm_enabled()) {
418 error_report("Userspace GICv3 is not supported with KVM");
419 exit(1);
421 return "arm-gicv3";
426 * kvm_arm_handle_debug:
427 * @cs: CPUState
428 * @debug_exit: debug part of the KVM exit structure
430 * Returns: TRUE if the debug exception was handled.
432 bool kvm_arm_handle_debug(CPUState *cs, struct kvm_debug_exit_arch *debug_exit);
435 * kvm_arm_hw_debug_active:
436 * @cs: CPU State
438 * Return: TRUE if any hardware breakpoints in use.
440 bool kvm_arm_hw_debug_active(CPUState *cs);
443 * kvm_arm_copy_hw_debug_data:
444 * @ptr: kvm_guest_debug_arch structure
446 * Copy the architecture specific debug registers into the
447 * kvm_guest_debug ioctl structure.
449 struct kvm_guest_debug_arch;
450 void kvm_arm_copy_hw_debug_data(struct kvm_guest_debug_arch *ptr);
453 * its_class_name:
455 * Return the ITS class name to use depending on whether KVM acceleration
456 * and KVM CAP_SIGNAL_MSI are supported
458 * Returns: class name to use or NULL
460 static inline const char *its_class_name(void)
462 if (kvm_irqchip_in_kernel()) {
463 /* KVM implementation requires this capability */
464 return kvm_direct_msi_enabled() ? "arm-its-kvm" : NULL;
465 } else {
466 /* Software emulation is not implemented yet */
467 return NULL;
471 #endif