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