xref: /openbmc/qemu/target/arm/kvm_arm.h (revision ea8ae47bdd2024dc2596f16b27f27fd4dcc08776)
1fcf5ef2aSThomas Huth /*
2fcf5ef2aSThomas Huth  * QEMU KVM support -- ARM specific functions.
3fcf5ef2aSThomas Huth  *
4fcf5ef2aSThomas Huth  * Copyright (c) 2012 Linaro Limited
5fcf5ef2aSThomas Huth  *
6fcf5ef2aSThomas Huth  * This work is licensed under the terms of the GNU GPL, version 2 or later.
7fcf5ef2aSThomas Huth  * See the COPYING file in the top-level directory.
8fcf5ef2aSThomas Huth  *
9fcf5ef2aSThomas Huth  */
10fcf5ef2aSThomas Huth 
11fcf5ef2aSThomas Huth #ifndef QEMU_KVM_ARM_H
12fcf5ef2aSThomas Huth #define QEMU_KVM_ARM_H
13fcf5ef2aSThomas Huth 
14fcf5ef2aSThomas Huth #include "sysemu/kvm.h"
15fcf5ef2aSThomas Huth 
16d45efe47SEric Auger #define KVM_ARM_VGIC_V2   (1 << 0)
17d45efe47SEric Auger #define KVM_ARM_VGIC_V3   (1 << 1)
18d45efe47SEric Auger 
19fcf5ef2aSThomas Huth /**
20fcf5ef2aSThomas Huth  * kvm_arm_register_device:
21fcf5ef2aSThomas Huth  * @mr: memory region for this device
22fcf5ef2aSThomas Huth  * @devid: the KVM device ID
23fcf5ef2aSThomas Huth  * @group: device control API group for setting addresses
24fcf5ef2aSThomas Huth  * @attr: device control API address type
25*84f298eaSPaolo Bonzini  * @dev_fd: device control device file descriptor
2619d1bd0bSEric Auger  * @addr_ormask: value to be OR'ed with resolved address
27fcf5ef2aSThomas Huth  *
28*84f298eaSPaolo Bonzini  * Remember the memory region @mr, and when it is mapped by the machine
29*84f298eaSPaolo Bonzini  * model, tell the kernel that base address using the device control API.
30*84f298eaSPaolo Bonzini  * @devid should be the ID of the device as defined by  the arm-vgic device
31*84f298eaSPaolo Bonzini  * in the device control API.  The machine model may map and unmap the device
32*84f298eaSPaolo Bonzini  * multiple times; the kernel will only be told the final address at the
33*84f298eaSPaolo Bonzini  * point where machine init is complete.
34fcf5ef2aSThomas Huth  */
35fcf5ef2aSThomas Huth void kvm_arm_register_device(MemoryRegion *mr, uint64_t devid, uint64_t group,
3619d1bd0bSEric Auger                              uint64_t attr, int dev_fd, uint64_t addr_ormask);
37fcf5ef2aSThomas Huth 
38fcf5ef2aSThomas Huth /**
39fcf5ef2aSThomas Huth  * write_list_to_kvmstate:
40fcf5ef2aSThomas Huth  * @cpu: ARMCPU
41fcf5ef2aSThomas Huth  * @level: the state level to sync
42fcf5ef2aSThomas Huth  *
43fcf5ef2aSThomas Huth  * For each register listed in the ARMCPU cpreg_indexes list, write
44fcf5ef2aSThomas Huth  * its value from the cpreg_values list into the kernel (via ioctl).
45fcf5ef2aSThomas Huth  * This updates KVM's working data structures from TCG data or
46fcf5ef2aSThomas Huth  * from incoming migration state.
47fcf5ef2aSThomas Huth  *
48fcf5ef2aSThomas Huth  * Returns: true if all register values were updated correctly,
49fcf5ef2aSThomas Huth  * false if some register was unknown to the kernel or could not
50fcf5ef2aSThomas Huth  * be written (eg constant register with the wrong value).
51fcf5ef2aSThomas Huth  * Note that we do not stop early on failure -- we will attempt
52fcf5ef2aSThomas Huth  * writing all registers in the list.
53fcf5ef2aSThomas Huth  */
54fcf5ef2aSThomas Huth bool write_list_to_kvmstate(ARMCPU *cpu, int level);
55fcf5ef2aSThomas Huth 
56fcf5ef2aSThomas Huth /**
57fcf5ef2aSThomas Huth  * write_kvmstate_to_list:
58fcf5ef2aSThomas Huth  * @cpu: ARMCPU
59fcf5ef2aSThomas Huth  *
60fcf5ef2aSThomas Huth  * For each register listed in the ARMCPU cpreg_indexes list, write
61fcf5ef2aSThomas Huth  * its value from the kernel into the cpreg_values list. This is used to
62fcf5ef2aSThomas Huth  * copy info from KVM's working data structures into TCG or
63fcf5ef2aSThomas Huth  * for outbound migration.
64fcf5ef2aSThomas Huth  *
65fcf5ef2aSThomas Huth  * Returns: true if all register values were read correctly,
66fcf5ef2aSThomas Huth  * false if some register was unknown or could not be read.
67fcf5ef2aSThomas Huth  * Note that we do not stop early on failure -- we will attempt
68fcf5ef2aSThomas Huth  * reading all registers in the list.
69fcf5ef2aSThomas Huth  */
70fcf5ef2aSThomas Huth bool write_kvmstate_to_list(ARMCPU *cpu);
71fcf5ef2aSThomas Huth 
72fcf5ef2aSThomas Huth /**
73e5ac4200SAndrew Jones  * kvm_arm_cpu_pre_save:
74e5ac4200SAndrew Jones  * @cpu: ARMCPU
75e5ac4200SAndrew Jones  *
76e5ac4200SAndrew Jones  * Called after write_kvmstate_to_list() from cpu_pre_save() to update
77e5ac4200SAndrew Jones  * the cpreg list with KVM CPU state.
78e5ac4200SAndrew Jones  */
79e5ac4200SAndrew Jones void kvm_arm_cpu_pre_save(ARMCPU *cpu);
80e5ac4200SAndrew Jones 
81e5ac4200SAndrew Jones /**
82e5ac4200SAndrew Jones  * kvm_arm_cpu_post_load:
83e5ac4200SAndrew Jones  * @cpu: ARMCPU
84e5ac4200SAndrew Jones  *
85e5ac4200SAndrew Jones  * Called from cpu_post_load() to update KVM CPU state from the cpreg list.
86e5ac4200SAndrew Jones  */
87e5ac4200SAndrew Jones void kvm_arm_cpu_post_load(ARMCPU *cpu);
88e5ac4200SAndrew Jones 
89e5ac4200SAndrew Jones /**
90fcf5ef2aSThomas Huth  * kvm_arm_reset_vcpu:
91fcf5ef2aSThomas Huth  * @cpu: ARMCPU
92fcf5ef2aSThomas Huth  *
93fcf5ef2aSThomas Huth  * Called at reset time to kernel registers to their initial values.
94fcf5ef2aSThomas Huth  */
95fcf5ef2aSThomas Huth void kvm_arm_reset_vcpu(ARMCPU *cpu);
96fcf5ef2aSThomas Huth 
97fcf5ef2aSThomas Huth #ifdef CONFIG_KVM
98fcf5ef2aSThomas Huth /**
99fcf5ef2aSThomas Huth  * kvm_arm_create_scratch_host_vcpu:
100fcf5ef2aSThomas Huth  * @cpus_to_try: array of QEMU_KVM_ARM_TARGET_* values (terminated with
101fcf5ef2aSThomas Huth  * QEMU_KVM_ARM_TARGET_NONE) to try as fallback if the kernel does not
102fcf5ef2aSThomas Huth  * know the PREFERRED_TARGET ioctl. Passing NULL is the same as passing
103fcf5ef2aSThomas Huth  * an empty array.
104fcf5ef2aSThomas Huth  * @fdarray: filled in with kvmfd, vmfd, cpufd file descriptors in that order
105fcf5ef2aSThomas Huth  * @init: filled in with the necessary values for creating a host
106fcf5ef2aSThomas Huth  * vcpu. If NULL is provided, will not init the vCPU (though the cpufd
107fcf5ef2aSThomas Huth  * will still be set up).
108fcf5ef2aSThomas Huth  *
109fcf5ef2aSThomas Huth  * Create a scratch vcpu in its own VM of the type preferred by the host
110fcf5ef2aSThomas Huth  * kernel (as would be used for '-cpu host'), for purposes of probing it
111fcf5ef2aSThomas Huth  * for capabilities.
112fcf5ef2aSThomas Huth  *
113fcf5ef2aSThomas Huth  * Returns: true on success (and fdarray and init are filled in),
114fcf5ef2aSThomas Huth  * false on failure (and fdarray and init are not valid).
115fcf5ef2aSThomas Huth  */
116fcf5ef2aSThomas Huth bool kvm_arm_create_scratch_host_vcpu(const uint32_t *cpus_to_try,
117fcf5ef2aSThomas Huth                                       int *fdarray,
118fcf5ef2aSThomas Huth                                       struct kvm_vcpu_init *init);
119fcf5ef2aSThomas Huth 
120fcf5ef2aSThomas Huth /**
121fcf5ef2aSThomas Huth  * kvm_arm_destroy_scratch_host_vcpu:
122fcf5ef2aSThomas Huth  * @fdarray: array of fds as set up by kvm_arm_create_scratch_host_vcpu
123fcf5ef2aSThomas Huth  *
124fcf5ef2aSThomas Huth  * Tear down the scratch vcpu created by kvm_arm_create_scratch_host_vcpu.
125fcf5ef2aSThomas Huth  */
126fcf5ef2aSThomas Huth void kvm_arm_destroy_scratch_host_vcpu(int *fdarray);
127fcf5ef2aSThomas Huth 
128c4487d76SPeter Maydell /**
1296fa8a379SAndrew Jones  * kvm_arm_sve_get_vls:
130d6339282SPhilippe Mathieu-Daudé  * @cpu: ARMCPU
1316fa8a379SAndrew Jones  *
1326fa8a379SAndrew Jones  * Get all the SVE vector lengths supported by the KVM host, setting
1336fa8a379SAndrew Jones  * the bits corresponding to their length in quadwords minus one
134886902ecSRichard Henderson  * (vq - 1) up to ARM_MAX_VQ.  Return the resulting map.
1356fa8a379SAndrew Jones  */
136d6339282SPhilippe Mathieu-Daudé uint32_t kvm_arm_sve_get_vls(ARMCPU *cpu);
1376fa8a379SAndrew Jones 
1386fa8a379SAndrew Jones /**
139c4487d76SPeter Maydell  * kvm_arm_set_cpu_features_from_host:
140c4487d76SPeter Maydell  * @cpu: ARMCPU to set the features for
141c4487d76SPeter Maydell  *
142c4487d76SPeter Maydell  * Set up the ARMCPU struct fields up to match the information probed
143c4487d76SPeter Maydell  * from the host CPU.
144c4487d76SPeter Maydell  */
145c4487d76SPeter Maydell void kvm_arm_set_cpu_features_from_host(ARMCPU *cpu);
146fcf5ef2aSThomas Huth 
147fcf5ef2aSThomas Huth /**
148dea101a1SAndrew Jones  * kvm_arm_add_vcpu_properties:
149cac675b5SPhilippe Mathieu-Daudé  * @cpu: The CPU object to add the properties to
150dea101a1SAndrew Jones  *
151dea101a1SAndrew Jones  * Add all KVM specific CPU properties to the CPU object. These
152dea101a1SAndrew Jones  * are the CPU properties with "kvm-" prefixed names.
153dea101a1SAndrew Jones  */
154cac675b5SPhilippe Mathieu-Daudé void kvm_arm_add_vcpu_properties(ARMCPU *cpu);
155dea101a1SAndrew Jones 
156dea101a1SAndrew Jones /**
15768970d1eSAndrew Jones  * kvm_arm_steal_time_finalize:
15868970d1eSAndrew Jones  * @cpu: ARMCPU for which to finalize kvm-steal-time
15968970d1eSAndrew Jones  * @errp: Pointer to Error* for error propagation
16068970d1eSAndrew Jones  *
16168970d1eSAndrew Jones  * Validate the kvm-steal-time property selection and set its default
16268970d1eSAndrew Jones  * based on KVM support and guest configuration.
16368970d1eSAndrew Jones  */
16468970d1eSAndrew Jones void kvm_arm_steal_time_finalize(ARMCPU *cpu, Error **errp);
16568970d1eSAndrew Jones 
16668970d1eSAndrew Jones /**
167b9e758f0SAndrew Jones  * kvm_arm_aarch32_supported:
168b9e758f0SAndrew Jones  *
1697d20e681SPhilippe Mathieu-Daudé  * Returns: true if KVM can enable AArch32 mode
170b9e758f0SAndrew Jones  * and false otherwise.
171b9e758f0SAndrew Jones  */
1727d20e681SPhilippe Mathieu-Daudé bool kvm_arm_aarch32_supported(void);
173b9e758f0SAndrew Jones 
174b9e758f0SAndrew Jones /**
175d1ebbc9dSAndrew Jones  * kvm_arm_pmu_supported:
176ae502508SAndrew Jones  *
1777d20e681SPhilippe Mathieu-Daudé  * Returns: true if KVM can enable the PMU
178ae502508SAndrew Jones  * and false otherwise.
179ae502508SAndrew Jones  */
1807d20e681SPhilippe Mathieu-Daudé bool kvm_arm_pmu_supported(void);
181ae502508SAndrew Jones 
182ae502508SAndrew Jones /**
183d1ebbc9dSAndrew Jones  * kvm_arm_sve_supported:
18414e99e0fSAndrew Jones  *
1857d20e681SPhilippe Mathieu-Daudé  * Returns true if KVM can enable SVE and false otherwise.
18614e99e0fSAndrew Jones  */
1877d20e681SPhilippe Mathieu-Daudé bool kvm_arm_sve_supported(void);
18814e99e0fSAndrew Jones 
18914e99e0fSAndrew Jones /**
190918d0de0SCornelia Huck  * kvm_arm_mte_supported:
191918d0de0SCornelia Huck  *
192918d0de0SCornelia Huck  * Returns: true if KVM can enable MTE, and false otherwise.
193918d0de0SCornelia Huck  */
194918d0de0SCornelia Huck bool kvm_arm_mte_supported(void);
195918d0de0SCornelia Huck 
196918d0de0SCornelia Huck /**
197d1ebbc9dSAndrew Jones  * kvm_arm_get_max_vm_ipa_size:
198a27382e2SEric Auger  * @ms: Machine state handle
199bcb902a1SAndrew Jones  * @fixed_ipa: True when the IPA limit is fixed at 40. This is the case
200bcb902a1SAndrew Jones  * for legacy KVM.
201d1ebbc9dSAndrew Jones  *
202d1ebbc9dSAndrew Jones  * Returns the number of bits in the IPA address space supported by KVM
203a27382e2SEric Auger  */
204bcb902a1SAndrew Jones int kvm_arm_get_max_vm_ipa_size(MachineState *ms, bool *fixed_ipa);
205a27382e2SEric Auger 
206fcf5ef2aSThomas Huth int kvm_arm_vgic_probe(void);
207fcf5ef2aSThomas Huth 
208d344f5baSPhilippe Mathieu-Daudé void kvm_arm_pmu_init(ARMCPU *cpu);
2095ed84f3bSPhilippe Mathieu-Daudé void kvm_arm_pmu_set_irq(ARMCPU *cpu, int irq);
21068970d1eSAndrew Jones 
21168970d1eSAndrew Jones /**
21268970d1eSAndrew Jones  * kvm_arm_pvtime_init:
21355503372SPhilippe Mathieu-Daudé  * @cpu: ARMCPU
21468970d1eSAndrew Jones  * @ipa: Per-vcpu guest physical base address of the pvtime structures
21568970d1eSAndrew Jones  *
21668970d1eSAndrew Jones  * Initializes PVTIME for the VCPU, setting the PVTIME IPA to @ipa.
21768970d1eSAndrew Jones  */
21855503372SPhilippe Mathieu-Daudé void kvm_arm_pvtime_init(ARMCPU *cpu, uint64_t ipa);
21968970d1eSAndrew Jones 
220f6530926SEric Auger int kvm_arm_set_irq(int cpu, int irqtype, int irq, int level);
221fcf5ef2aSThomas Huth 
222918d0de0SCornelia Huck void kvm_arm_enable_mte(Object *cpuobj, Error **errp);
223918d0de0SCornelia Huck 
224fcf5ef2aSThomas Huth #else
225fcf5ef2aSThomas Huth 
226d1ebbc9dSAndrew Jones /*
227281a3c33SAndrew Jones  * It's safe to call these functions without KVM support.
228281a3c33SAndrew Jones  * They should either do nothing or return "not supported".
229c4487d76SPeter Maydell  */
kvm_arm_aarch32_supported(void)2307d20e681SPhilippe Mathieu-Daudé static inline bool kvm_arm_aarch32_supported(void)
231b9e758f0SAndrew Jones {
232b9e758f0SAndrew Jones     return false;
233b9e758f0SAndrew Jones }
234b9e758f0SAndrew Jones 
kvm_arm_pmu_supported(void)2357d20e681SPhilippe Mathieu-Daudé static inline bool kvm_arm_pmu_supported(void)
236ae502508SAndrew Jones {
237ae502508SAndrew Jones     return false;
238ae502508SAndrew Jones }
239ae502508SAndrew Jones 
kvm_arm_sve_supported(void)2407d20e681SPhilippe Mathieu-Daudé static inline bool kvm_arm_sve_supported(void)
24114e99e0fSAndrew Jones {
24214e99e0fSAndrew Jones     return false;
24314e99e0fSAndrew Jones }
24414e99e0fSAndrew Jones 
kvm_arm_mte_supported(void)245918d0de0SCornelia Huck static inline bool kvm_arm_mte_supported(void)
246918d0de0SCornelia Huck {
247918d0de0SCornelia Huck     return false;
248918d0de0SCornelia Huck }
249918d0de0SCornelia Huck 
250281a3c33SAndrew Jones /*
251281a3c33SAndrew Jones  * These functions should never actually be called without KVM support.
252281a3c33SAndrew Jones  */
kvm_arm_set_cpu_features_from_host(ARMCPU * cpu)253281a3c33SAndrew Jones static inline void kvm_arm_set_cpu_features_from_host(ARMCPU *cpu)
254281a3c33SAndrew Jones {
255281a3c33SAndrew Jones     g_assert_not_reached();
256281a3c33SAndrew Jones }
257281a3c33SAndrew Jones 
kvm_arm_add_vcpu_properties(ARMCPU * cpu)258cac675b5SPhilippe Mathieu-Daudé static inline void kvm_arm_add_vcpu_properties(ARMCPU *cpu)
259281a3c33SAndrew Jones {
260281a3c33SAndrew Jones     g_assert_not_reached();
261281a3c33SAndrew Jones }
262281a3c33SAndrew Jones 
kvm_arm_get_max_vm_ipa_size(MachineState * ms,bool * fixed_ipa)263bcb902a1SAndrew Jones static inline int kvm_arm_get_max_vm_ipa_size(MachineState *ms, bool *fixed_ipa)
264a27382e2SEric Auger {
265281a3c33SAndrew Jones     g_assert_not_reached();
266a27382e2SEric Auger }
267a27382e2SEric Auger 
kvm_arm_vgic_probe(void)268fcf5ef2aSThomas Huth static inline int kvm_arm_vgic_probe(void)
269fcf5ef2aSThomas Huth {
270281a3c33SAndrew Jones     g_assert_not_reached();
271fcf5ef2aSThomas Huth }
272fcf5ef2aSThomas Huth 
kvm_arm_pmu_set_irq(ARMCPU * cpu,int irq)2735ed84f3bSPhilippe Mathieu-Daudé static inline void kvm_arm_pmu_set_irq(ARMCPU *cpu, int irq)
274281a3c33SAndrew Jones {
275281a3c33SAndrew Jones     g_assert_not_reached();
276281a3c33SAndrew Jones }
277fcf5ef2aSThomas Huth 
kvm_arm_pmu_init(ARMCPU * cpu)278d344f5baSPhilippe Mathieu-Daudé static inline void kvm_arm_pmu_init(ARMCPU *cpu)
279281a3c33SAndrew Jones {
280281a3c33SAndrew Jones     g_assert_not_reached();
281281a3c33SAndrew Jones }
282e5ac4200SAndrew Jones 
kvm_arm_pvtime_init(ARMCPU * cpu,uint64_t ipa)28355503372SPhilippe Mathieu-Daudé static inline void kvm_arm_pvtime_init(ARMCPU *cpu, uint64_t ipa)
28468970d1eSAndrew Jones {
28568970d1eSAndrew Jones     g_assert_not_reached();
28668970d1eSAndrew Jones }
28768970d1eSAndrew Jones 
kvm_arm_steal_time_finalize(ARMCPU * cpu,Error ** errp)28868970d1eSAndrew Jones static inline void kvm_arm_steal_time_finalize(ARMCPU *cpu, Error **errp)
28968970d1eSAndrew Jones {
29068970d1eSAndrew Jones     g_assert_not_reached();
29168970d1eSAndrew Jones }
29268970d1eSAndrew Jones 
kvm_arm_sve_get_vls(ARMCPU * cpu)293d6339282SPhilippe Mathieu-Daudé static inline uint32_t kvm_arm_sve_get_vls(ARMCPU *cpu)
294281a3c33SAndrew Jones {
295281a3c33SAndrew Jones     g_assert_not_reached();
296281a3c33SAndrew Jones }
297281a3c33SAndrew Jones 
kvm_arm_enable_mte(Object * cpuobj,Error ** errp)298918d0de0SCornelia Huck static inline void kvm_arm_enable_mte(Object *cpuobj, Error **errp)
299918d0de0SCornelia Huck {
300918d0de0SCornelia Huck     g_assert_not_reached();
301918d0de0SCornelia Huck }
302918d0de0SCornelia Huck 
303fcf5ef2aSThomas Huth #endif
304fcf5ef2aSThomas Huth 
305fcf5ef2aSThomas Huth #endif
306