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