1 /* 2 * QEMU AArch64 CPU 3 * 4 * Copyright (c) 2013 Linaro Ltd 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License 8 * as published by the Free Software Foundation; either version 2 9 * of the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, see 18 * <http://www.gnu.org/licenses/gpl-2.0.html> 19 */ 20 21 #include "qemu/osdep.h" 22 #include "qapi/error.h" 23 #include "cpu.h" 24 #include "qemu/module.h" 25 #if !defined(CONFIG_USER_ONLY) 26 #include "hw/loader.h" 27 #endif 28 #include "sysemu/kvm.h" 29 #include "kvm_arm.h" 30 #include "qapi/visitor.h" 31 #include "hw/qdev-properties.h" 32 33 34 #ifndef CONFIG_USER_ONLY 35 static uint64_t a57_a53_l2ctlr_read(CPUARMState *env, const ARMCPRegInfo *ri) 36 { 37 ARMCPU *cpu = env_archcpu(env); 38 39 /* Number of cores is in [25:24]; otherwise we RAZ */ 40 return (cpu->core_count - 1) << 24; 41 } 42 #endif 43 44 static const ARMCPRegInfo cortex_a72_a57_a53_cp_reginfo[] = { 45 #ifndef CONFIG_USER_ONLY 46 { .name = "L2CTLR_EL1", .state = ARM_CP_STATE_AA64, 47 .opc0 = 3, .opc1 = 1, .crn = 11, .crm = 0, .opc2 = 2, 48 .access = PL1_RW, .readfn = a57_a53_l2ctlr_read, 49 .writefn = arm_cp_write_ignore }, 50 { .name = "L2CTLR", 51 .cp = 15, .opc1 = 1, .crn = 9, .crm = 0, .opc2 = 2, 52 .access = PL1_RW, .readfn = a57_a53_l2ctlr_read, 53 .writefn = arm_cp_write_ignore }, 54 #endif 55 { .name = "L2ECTLR_EL1", .state = ARM_CP_STATE_AA64, 56 .opc0 = 3, .opc1 = 1, .crn = 11, .crm = 0, .opc2 = 3, 57 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 58 { .name = "L2ECTLR", 59 .cp = 15, .opc1 = 1, .crn = 9, .crm = 0, .opc2 = 3, 60 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 61 { .name = "L2ACTLR", .state = ARM_CP_STATE_BOTH, 62 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 0, .opc2 = 0, 63 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 64 { .name = "CPUACTLR_EL1", .state = ARM_CP_STATE_AA64, 65 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 2, .opc2 = 0, 66 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 67 { .name = "CPUACTLR", 68 .cp = 15, .opc1 = 0, .crm = 15, 69 .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 }, 70 { .name = "CPUECTLR_EL1", .state = ARM_CP_STATE_AA64, 71 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 2, .opc2 = 1, 72 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 73 { .name = "CPUECTLR", 74 .cp = 15, .opc1 = 1, .crm = 15, 75 .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 }, 76 { .name = "CPUMERRSR_EL1", .state = ARM_CP_STATE_AA64, 77 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 2, .opc2 = 2, 78 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 79 { .name = "CPUMERRSR", 80 .cp = 15, .opc1 = 2, .crm = 15, 81 .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 }, 82 { .name = "L2MERRSR_EL1", .state = ARM_CP_STATE_AA64, 83 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 2, .opc2 = 3, 84 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 85 { .name = "L2MERRSR", 86 .cp = 15, .opc1 = 3, .crm = 15, 87 .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 }, 88 REGINFO_SENTINEL 89 }; 90 91 static void aarch64_a57_initfn(Object *obj) 92 { 93 ARMCPU *cpu = ARM_CPU(obj); 94 95 cpu->dtb_compatible = "arm,cortex-a57"; 96 set_feature(&cpu->env, ARM_FEATURE_V8); 97 set_feature(&cpu->env, ARM_FEATURE_NEON); 98 set_feature(&cpu->env, ARM_FEATURE_GENERIC_TIMER); 99 set_feature(&cpu->env, ARM_FEATURE_AARCH64); 100 set_feature(&cpu->env, ARM_FEATURE_CBAR_RO); 101 set_feature(&cpu->env, ARM_FEATURE_EL2); 102 set_feature(&cpu->env, ARM_FEATURE_EL3); 103 set_feature(&cpu->env, ARM_FEATURE_PMU); 104 cpu->kvm_target = QEMU_KVM_ARM_TARGET_CORTEX_A57; 105 cpu->midr = 0x411fd070; 106 cpu->revidr = 0x00000000; 107 cpu->reset_fpsid = 0x41034070; 108 cpu->isar.mvfr0 = 0x10110222; 109 cpu->isar.mvfr1 = 0x12111111; 110 cpu->isar.mvfr2 = 0x00000043; 111 cpu->ctr = 0x8444c004; 112 cpu->reset_sctlr = 0x00c50838; 113 cpu->isar.id_pfr0 = 0x00000131; 114 cpu->isar.id_pfr1 = 0x00011011; 115 cpu->isar.id_dfr0 = 0x03010066; 116 cpu->id_afr0 = 0x00000000; 117 cpu->isar.id_mmfr0 = 0x10101105; 118 cpu->isar.id_mmfr1 = 0x40000000; 119 cpu->isar.id_mmfr2 = 0x01260000; 120 cpu->isar.id_mmfr3 = 0x02102211; 121 cpu->isar.id_isar0 = 0x02101110; 122 cpu->isar.id_isar1 = 0x13112111; 123 cpu->isar.id_isar2 = 0x21232042; 124 cpu->isar.id_isar3 = 0x01112131; 125 cpu->isar.id_isar4 = 0x00011142; 126 cpu->isar.id_isar5 = 0x00011121; 127 cpu->isar.id_isar6 = 0; 128 cpu->isar.id_aa64pfr0 = 0x00002222; 129 cpu->isar.id_aa64dfr0 = 0x10305106; 130 cpu->isar.id_aa64isar0 = 0x00011120; 131 cpu->isar.id_aa64mmfr0 = 0x00001124; 132 cpu->isar.dbgdidr = 0x3516d000; 133 cpu->clidr = 0x0a200023; 134 cpu->ccsidr[0] = 0x701fe00a; /* 32KB L1 dcache */ 135 cpu->ccsidr[1] = 0x201fe012; /* 48KB L1 icache */ 136 cpu->ccsidr[2] = 0x70ffe07a; /* 2048KB L2 cache */ 137 cpu->dcz_blocksize = 4; /* 64 bytes */ 138 cpu->gic_num_lrs = 4; 139 cpu->gic_vpribits = 5; 140 cpu->gic_vprebits = 5; 141 define_arm_cp_regs(cpu, cortex_a72_a57_a53_cp_reginfo); 142 } 143 144 static void aarch64_a53_initfn(Object *obj) 145 { 146 ARMCPU *cpu = ARM_CPU(obj); 147 148 cpu->dtb_compatible = "arm,cortex-a53"; 149 set_feature(&cpu->env, ARM_FEATURE_V8); 150 set_feature(&cpu->env, ARM_FEATURE_NEON); 151 set_feature(&cpu->env, ARM_FEATURE_GENERIC_TIMER); 152 set_feature(&cpu->env, ARM_FEATURE_AARCH64); 153 set_feature(&cpu->env, ARM_FEATURE_CBAR_RO); 154 set_feature(&cpu->env, ARM_FEATURE_EL2); 155 set_feature(&cpu->env, ARM_FEATURE_EL3); 156 set_feature(&cpu->env, ARM_FEATURE_PMU); 157 cpu->kvm_target = QEMU_KVM_ARM_TARGET_CORTEX_A53; 158 cpu->midr = 0x410fd034; 159 cpu->revidr = 0x00000000; 160 cpu->reset_fpsid = 0x41034070; 161 cpu->isar.mvfr0 = 0x10110222; 162 cpu->isar.mvfr1 = 0x12111111; 163 cpu->isar.mvfr2 = 0x00000043; 164 cpu->ctr = 0x84448004; /* L1Ip = VIPT */ 165 cpu->reset_sctlr = 0x00c50838; 166 cpu->isar.id_pfr0 = 0x00000131; 167 cpu->isar.id_pfr1 = 0x00011011; 168 cpu->isar.id_dfr0 = 0x03010066; 169 cpu->id_afr0 = 0x00000000; 170 cpu->isar.id_mmfr0 = 0x10101105; 171 cpu->isar.id_mmfr1 = 0x40000000; 172 cpu->isar.id_mmfr2 = 0x01260000; 173 cpu->isar.id_mmfr3 = 0x02102211; 174 cpu->isar.id_isar0 = 0x02101110; 175 cpu->isar.id_isar1 = 0x13112111; 176 cpu->isar.id_isar2 = 0x21232042; 177 cpu->isar.id_isar3 = 0x01112131; 178 cpu->isar.id_isar4 = 0x00011142; 179 cpu->isar.id_isar5 = 0x00011121; 180 cpu->isar.id_isar6 = 0; 181 cpu->isar.id_aa64pfr0 = 0x00002222; 182 cpu->isar.id_aa64dfr0 = 0x10305106; 183 cpu->isar.id_aa64isar0 = 0x00011120; 184 cpu->isar.id_aa64mmfr0 = 0x00001122; /* 40 bit physical addr */ 185 cpu->isar.dbgdidr = 0x3516d000; 186 cpu->clidr = 0x0a200023; 187 cpu->ccsidr[0] = 0x700fe01a; /* 32KB L1 dcache */ 188 cpu->ccsidr[1] = 0x201fe00a; /* 32KB L1 icache */ 189 cpu->ccsidr[2] = 0x707fe07a; /* 1024KB L2 cache */ 190 cpu->dcz_blocksize = 4; /* 64 bytes */ 191 cpu->gic_num_lrs = 4; 192 cpu->gic_vpribits = 5; 193 cpu->gic_vprebits = 5; 194 define_arm_cp_regs(cpu, cortex_a72_a57_a53_cp_reginfo); 195 } 196 197 static void aarch64_a72_initfn(Object *obj) 198 { 199 ARMCPU *cpu = ARM_CPU(obj); 200 201 cpu->dtb_compatible = "arm,cortex-a72"; 202 set_feature(&cpu->env, ARM_FEATURE_V8); 203 set_feature(&cpu->env, ARM_FEATURE_NEON); 204 set_feature(&cpu->env, ARM_FEATURE_GENERIC_TIMER); 205 set_feature(&cpu->env, ARM_FEATURE_AARCH64); 206 set_feature(&cpu->env, ARM_FEATURE_CBAR_RO); 207 set_feature(&cpu->env, ARM_FEATURE_EL2); 208 set_feature(&cpu->env, ARM_FEATURE_EL3); 209 set_feature(&cpu->env, ARM_FEATURE_PMU); 210 cpu->midr = 0x410fd083; 211 cpu->revidr = 0x00000000; 212 cpu->reset_fpsid = 0x41034080; 213 cpu->isar.mvfr0 = 0x10110222; 214 cpu->isar.mvfr1 = 0x12111111; 215 cpu->isar.mvfr2 = 0x00000043; 216 cpu->ctr = 0x8444c004; 217 cpu->reset_sctlr = 0x00c50838; 218 cpu->isar.id_pfr0 = 0x00000131; 219 cpu->isar.id_pfr1 = 0x00011011; 220 cpu->isar.id_dfr0 = 0x03010066; 221 cpu->id_afr0 = 0x00000000; 222 cpu->isar.id_mmfr0 = 0x10201105; 223 cpu->isar.id_mmfr1 = 0x40000000; 224 cpu->isar.id_mmfr2 = 0x01260000; 225 cpu->isar.id_mmfr3 = 0x02102211; 226 cpu->isar.id_isar0 = 0x02101110; 227 cpu->isar.id_isar1 = 0x13112111; 228 cpu->isar.id_isar2 = 0x21232042; 229 cpu->isar.id_isar3 = 0x01112131; 230 cpu->isar.id_isar4 = 0x00011142; 231 cpu->isar.id_isar5 = 0x00011121; 232 cpu->isar.id_aa64pfr0 = 0x00002222; 233 cpu->isar.id_aa64dfr0 = 0x10305106; 234 cpu->isar.id_aa64isar0 = 0x00011120; 235 cpu->isar.id_aa64mmfr0 = 0x00001124; 236 cpu->isar.dbgdidr = 0x3516d000; 237 cpu->clidr = 0x0a200023; 238 cpu->ccsidr[0] = 0x701fe00a; /* 32KB L1 dcache */ 239 cpu->ccsidr[1] = 0x201fe012; /* 48KB L1 icache */ 240 cpu->ccsidr[2] = 0x707fe07a; /* 1MB L2 cache */ 241 cpu->dcz_blocksize = 4; /* 64 bytes */ 242 cpu->gic_num_lrs = 4; 243 cpu->gic_vpribits = 5; 244 cpu->gic_vprebits = 5; 245 define_arm_cp_regs(cpu, cortex_a72_a57_a53_cp_reginfo); 246 } 247 248 void arm_cpu_sve_finalize(ARMCPU *cpu, Error **errp) 249 { 250 /* 251 * If any vector lengths are explicitly enabled with sve<N> properties, 252 * then all other lengths are implicitly disabled. If sve-max-vq is 253 * specified then it is the same as explicitly enabling all lengths 254 * up to and including the specified maximum, which means all larger 255 * lengths will be implicitly disabled. If no sve<N> properties 256 * are enabled and sve-max-vq is not specified, then all lengths not 257 * explicitly disabled will be enabled. Additionally, all power-of-two 258 * vector lengths less than the maximum enabled length will be 259 * automatically enabled and all vector lengths larger than the largest 260 * disabled power-of-two vector length will be automatically disabled. 261 * Errors are generated if the user provided input that interferes with 262 * any of the above. Finally, if SVE is not disabled, then at least one 263 * vector length must be enabled. 264 */ 265 DECLARE_BITMAP(kvm_supported, ARM_MAX_VQ); 266 DECLARE_BITMAP(tmp, ARM_MAX_VQ); 267 uint32_t vq, max_vq = 0; 268 269 /* Collect the set of vector lengths supported by KVM. */ 270 bitmap_zero(kvm_supported, ARM_MAX_VQ); 271 if (kvm_enabled() && kvm_arm_sve_supported()) { 272 kvm_arm_sve_get_vls(CPU(cpu), kvm_supported); 273 } else if (kvm_enabled()) { 274 assert(!cpu_isar_feature(aa64_sve, cpu)); 275 } 276 277 /* 278 * Process explicit sve<N> properties. 279 * From the properties, sve_vq_map<N> implies sve_vq_init<N>. 280 * Check first for any sve<N> enabled. 281 */ 282 if (!bitmap_empty(cpu->sve_vq_map, ARM_MAX_VQ)) { 283 max_vq = find_last_bit(cpu->sve_vq_map, ARM_MAX_VQ) + 1; 284 285 if (cpu->sve_max_vq && max_vq > cpu->sve_max_vq) { 286 error_setg(errp, "cannot enable sve%d", max_vq * 128); 287 error_append_hint(errp, "sve%d is larger than the maximum vector " 288 "length, sve-max-vq=%d (%d bits)\n", 289 max_vq * 128, cpu->sve_max_vq, 290 cpu->sve_max_vq * 128); 291 return; 292 } 293 294 if (kvm_enabled()) { 295 /* 296 * For KVM we have to automatically enable all supported unitialized 297 * lengths, even when the smaller lengths are not all powers-of-two. 298 */ 299 bitmap_andnot(tmp, kvm_supported, cpu->sve_vq_init, max_vq); 300 bitmap_or(cpu->sve_vq_map, cpu->sve_vq_map, tmp, max_vq); 301 } else { 302 /* Propagate enabled bits down through required powers-of-two. */ 303 for (vq = pow2floor(max_vq); vq >= 1; vq >>= 1) { 304 if (!test_bit(vq - 1, cpu->sve_vq_init)) { 305 set_bit(vq - 1, cpu->sve_vq_map); 306 } 307 } 308 } 309 } else if (cpu->sve_max_vq == 0) { 310 /* 311 * No explicit bits enabled, and no implicit bits from sve-max-vq. 312 */ 313 if (!cpu_isar_feature(aa64_sve, cpu)) { 314 /* SVE is disabled and so are all vector lengths. Good. */ 315 return; 316 } 317 318 if (kvm_enabled()) { 319 /* Disabling a supported length disables all larger lengths. */ 320 for (vq = 1; vq <= ARM_MAX_VQ; ++vq) { 321 if (test_bit(vq - 1, cpu->sve_vq_init) && 322 test_bit(vq - 1, kvm_supported)) { 323 break; 324 } 325 } 326 max_vq = vq <= ARM_MAX_VQ ? vq - 1 : ARM_MAX_VQ; 327 bitmap_andnot(cpu->sve_vq_map, kvm_supported, 328 cpu->sve_vq_init, max_vq); 329 if (max_vq == 0 || bitmap_empty(cpu->sve_vq_map, max_vq)) { 330 error_setg(errp, "cannot disable sve%d", vq * 128); 331 error_append_hint(errp, "Disabling sve%d results in all " 332 "vector lengths being disabled.\n", 333 vq * 128); 334 error_append_hint(errp, "With SVE enabled, at least one " 335 "vector length must be enabled.\n"); 336 return; 337 } 338 } else { 339 /* Disabling a power-of-two disables all larger lengths. */ 340 if (test_bit(0, cpu->sve_vq_init)) { 341 error_setg(errp, "cannot disable sve128"); 342 error_append_hint(errp, "Disabling sve128 results in all " 343 "vector lengths being disabled.\n"); 344 error_append_hint(errp, "With SVE enabled, at least one " 345 "vector length must be enabled.\n"); 346 return; 347 } 348 for (vq = 2; vq <= ARM_MAX_VQ; vq <<= 1) { 349 if (test_bit(vq - 1, cpu->sve_vq_init)) { 350 break; 351 } 352 } 353 max_vq = vq <= ARM_MAX_VQ ? vq - 1 : ARM_MAX_VQ; 354 bitmap_complement(cpu->sve_vq_map, cpu->sve_vq_init, max_vq); 355 } 356 357 max_vq = find_last_bit(cpu->sve_vq_map, max_vq) + 1; 358 } 359 360 /* 361 * Process the sve-max-vq property. 362 * Note that we know from the above that no bit above 363 * sve-max-vq is currently set. 364 */ 365 if (cpu->sve_max_vq != 0) { 366 max_vq = cpu->sve_max_vq; 367 368 if (!test_bit(max_vq - 1, cpu->sve_vq_map) && 369 test_bit(max_vq - 1, cpu->sve_vq_init)) { 370 error_setg(errp, "cannot disable sve%d", max_vq * 128); 371 error_append_hint(errp, "The maximum vector length must be " 372 "enabled, sve-max-vq=%d (%d bits)\n", 373 max_vq, max_vq * 128); 374 return; 375 } 376 377 /* Set all bits not explicitly set within sve-max-vq. */ 378 bitmap_complement(tmp, cpu->sve_vq_init, max_vq); 379 bitmap_or(cpu->sve_vq_map, cpu->sve_vq_map, tmp, max_vq); 380 } 381 382 /* 383 * We should know what max-vq is now. Also, as we're done 384 * manipulating sve-vq-map, we ensure any bits above max-vq 385 * are clear, just in case anybody looks. 386 */ 387 assert(max_vq != 0); 388 bitmap_clear(cpu->sve_vq_map, max_vq, ARM_MAX_VQ - max_vq); 389 390 if (kvm_enabled()) { 391 /* Ensure the set of lengths matches what KVM supports. */ 392 bitmap_xor(tmp, cpu->sve_vq_map, kvm_supported, max_vq); 393 if (!bitmap_empty(tmp, max_vq)) { 394 vq = find_last_bit(tmp, max_vq) + 1; 395 if (test_bit(vq - 1, cpu->sve_vq_map)) { 396 if (cpu->sve_max_vq) { 397 error_setg(errp, "cannot set sve-max-vq=%d", 398 cpu->sve_max_vq); 399 error_append_hint(errp, "This KVM host does not support " 400 "the vector length %d-bits.\n", 401 vq * 128); 402 error_append_hint(errp, "It may not be possible to use " 403 "sve-max-vq with this KVM host. Try " 404 "using only sve<N> properties.\n"); 405 } else { 406 error_setg(errp, "cannot enable sve%d", vq * 128); 407 error_append_hint(errp, "This KVM host does not support " 408 "the vector length %d-bits.\n", 409 vq * 128); 410 } 411 } else { 412 error_setg(errp, "cannot disable sve%d", vq * 128); 413 error_append_hint(errp, "The KVM host requires all " 414 "supported vector lengths smaller " 415 "than %d bits to also be enabled.\n", 416 max_vq * 128); 417 } 418 return; 419 } 420 } else { 421 /* Ensure all required powers-of-two are enabled. */ 422 for (vq = pow2floor(max_vq); vq >= 1; vq >>= 1) { 423 if (!test_bit(vq - 1, cpu->sve_vq_map)) { 424 error_setg(errp, "cannot disable sve%d", vq * 128); 425 error_append_hint(errp, "sve%d is required as it " 426 "is a power-of-two length smaller than " 427 "the maximum, sve%d\n", 428 vq * 128, max_vq * 128); 429 return; 430 } 431 } 432 } 433 434 /* 435 * Now that we validated all our vector lengths, the only question 436 * left to answer is if we even want SVE at all. 437 */ 438 if (!cpu_isar_feature(aa64_sve, cpu)) { 439 error_setg(errp, "cannot enable sve%d", max_vq * 128); 440 error_append_hint(errp, "SVE must be enabled to enable vector " 441 "lengths.\n"); 442 error_append_hint(errp, "Add sve=on to the CPU property list.\n"); 443 return; 444 } 445 446 /* From now on sve_max_vq is the actual maximum supported length. */ 447 cpu->sve_max_vq = max_vq; 448 } 449 450 static void cpu_max_get_sve_max_vq(Object *obj, Visitor *v, const char *name, 451 void *opaque, Error **errp) 452 { 453 ARMCPU *cpu = ARM_CPU(obj); 454 uint32_t value; 455 456 /* All vector lengths are disabled when SVE is off. */ 457 if (!cpu_isar_feature(aa64_sve, cpu)) { 458 value = 0; 459 } else { 460 value = cpu->sve_max_vq; 461 } 462 visit_type_uint32(v, name, &value, errp); 463 } 464 465 static void cpu_max_set_sve_max_vq(Object *obj, Visitor *v, const char *name, 466 void *opaque, Error **errp) 467 { 468 ARMCPU *cpu = ARM_CPU(obj); 469 uint32_t max_vq; 470 471 if (!visit_type_uint32(v, name, &max_vq, errp)) { 472 return; 473 } 474 475 if (kvm_enabled() && !kvm_arm_sve_supported()) { 476 error_setg(errp, "cannot set sve-max-vq"); 477 error_append_hint(errp, "SVE not supported by KVM on this host\n"); 478 return; 479 } 480 481 if (max_vq == 0 || max_vq > ARM_MAX_VQ) { 482 error_setg(errp, "unsupported SVE vector length"); 483 error_append_hint(errp, "Valid sve-max-vq in range [1-%d]\n", 484 ARM_MAX_VQ); 485 return; 486 } 487 488 cpu->sve_max_vq = max_vq; 489 } 490 491 static void cpu_arm_get_sve_vq(Object *obj, Visitor *v, const char *name, 492 void *opaque, Error **errp) 493 { 494 ARMCPU *cpu = ARM_CPU(obj); 495 uint32_t vq = atoi(&name[3]) / 128; 496 bool value; 497 498 /* All vector lengths are disabled when SVE is off. */ 499 if (!cpu_isar_feature(aa64_sve, cpu)) { 500 value = false; 501 } else { 502 value = test_bit(vq - 1, cpu->sve_vq_map); 503 } 504 visit_type_bool(v, name, &value, errp); 505 } 506 507 static void cpu_arm_set_sve_vq(Object *obj, Visitor *v, const char *name, 508 void *opaque, Error **errp) 509 { 510 ARMCPU *cpu = ARM_CPU(obj); 511 uint32_t vq = atoi(&name[3]) / 128; 512 bool value; 513 514 if (!visit_type_bool(v, name, &value, errp)) { 515 return; 516 } 517 518 if (value && kvm_enabled() && !kvm_arm_sve_supported()) { 519 error_setg(errp, "cannot enable %s", name); 520 error_append_hint(errp, "SVE not supported by KVM on this host\n"); 521 return; 522 } 523 524 if (value) { 525 set_bit(vq - 1, cpu->sve_vq_map); 526 } else { 527 clear_bit(vq - 1, cpu->sve_vq_map); 528 } 529 set_bit(vq - 1, cpu->sve_vq_init); 530 } 531 532 static void cpu_arm_get_sve(Object *obj, Visitor *v, const char *name, 533 void *opaque, Error **errp) 534 { 535 ARMCPU *cpu = ARM_CPU(obj); 536 bool value = cpu_isar_feature(aa64_sve, cpu); 537 538 visit_type_bool(v, name, &value, errp); 539 } 540 541 static void cpu_arm_set_sve(Object *obj, Visitor *v, const char *name, 542 void *opaque, Error **errp) 543 { 544 ARMCPU *cpu = ARM_CPU(obj); 545 bool value; 546 uint64_t t; 547 548 if (!visit_type_bool(v, name, &value, errp)) { 549 return; 550 } 551 552 if (value && kvm_enabled() && !kvm_arm_sve_supported()) { 553 error_setg(errp, "'sve' feature not supported by KVM on this host"); 554 return; 555 } 556 557 t = cpu->isar.id_aa64pfr0; 558 t = FIELD_DP64(t, ID_AA64PFR0, SVE, value); 559 cpu->isar.id_aa64pfr0 = t; 560 } 561 562 void aarch64_add_sve_properties(Object *obj) 563 { 564 uint32_t vq; 565 566 object_property_add(obj, "sve", "bool", cpu_arm_get_sve, 567 cpu_arm_set_sve, NULL, NULL); 568 569 for (vq = 1; vq <= ARM_MAX_VQ; ++vq) { 570 char name[8]; 571 sprintf(name, "sve%d", vq * 128); 572 object_property_add(obj, name, "bool", cpu_arm_get_sve_vq, 573 cpu_arm_set_sve_vq, NULL, NULL); 574 } 575 } 576 577 void arm_cpu_pauth_finalize(ARMCPU *cpu, Error **errp) 578 { 579 int arch_val = 0, impdef_val = 0; 580 uint64_t t; 581 582 /* TODO: Handle HaveEnhancedPAC, HaveEnhancedPAC2, HaveFPAC. */ 583 if (cpu->prop_pauth) { 584 if (cpu->prop_pauth_impdef) { 585 impdef_val = 1; 586 } else { 587 arch_val = 1; 588 } 589 } else if (cpu->prop_pauth_impdef) { 590 error_setg(errp, "cannot enable pauth-impdef without pauth"); 591 error_append_hint(errp, "Add pauth=on to the CPU property list.\n"); 592 } 593 594 t = cpu->isar.id_aa64isar1; 595 t = FIELD_DP64(t, ID_AA64ISAR1, APA, arch_val); 596 t = FIELD_DP64(t, ID_AA64ISAR1, GPA, arch_val); 597 t = FIELD_DP64(t, ID_AA64ISAR1, API, impdef_val); 598 t = FIELD_DP64(t, ID_AA64ISAR1, GPI, impdef_val); 599 cpu->isar.id_aa64isar1 = t; 600 } 601 602 static Property arm_cpu_pauth_property = 603 DEFINE_PROP_BOOL("pauth", ARMCPU, prop_pauth, true); 604 static Property arm_cpu_pauth_impdef_property = 605 DEFINE_PROP_BOOL("pauth-impdef", ARMCPU, prop_pauth_impdef, false); 606 607 /* -cpu max: if KVM is enabled, like -cpu host (best possible with this host); 608 * otherwise, a CPU with as many features enabled as our emulation supports. 609 * The version of '-cpu max' for qemu-system-arm is defined in cpu.c; 610 * this only needs to handle 64 bits. 611 */ 612 static void aarch64_max_initfn(Object *obj) 613 { 614 ARMCPU *cpu = ARM_CPU(obj); 615 616 if (kvm_enabled()) { 617 kvm_arm_set_cpu_features_from_host(cpu); 618 } else { 619 uint64_t t; 620 uint32_t u; 621 aarch64_a57_initfn(obj); 622 623 /* 624 * Reset MIDR so the guest doesn't mistake our 'max' CPU type for a real 625 * one and try to apply errata workarounds or use impdef features we 626 * don't provide. 627 * An IMPLEMENTER field of 0 means "reserved for software use"; 628 * ARCHITECTURE must be 0xf indicating "v7 or later, check ID registers 629 * to see which features are present"; 630 * the VARIANT, PARTNUM and REVISION fields are all implementation 631 * defined and we choose to define PARTNUM just in case guest 632 * code needs to distinguish this QEMU CPU from other software 633 * implementations, though this shouldn't be needed. 634 */ 635 t = FIELD_DP64(0, MIDR_EL1, IMPLEMENTER, 0); 636 t = FIELD_DP64(t, MIDR_EL1, ARCHITECTURE, 0xf); 637 t = FIELD_DP64(t, MIDR_EL1, PARTNUM, 'Q'); 638 t = FIELD_DP64(t, MIDR_EL1, VARIANT, 0); 639 t = FIELD_DP64(t, MIDR_EL1, REVISION, 0); 640 cpu->midr = t; 641 642 t = cpu->isar.id_aa64isar0; 643 t = FIELD_DP64(t, ID_AA64ISAR0, AES, 2); /* AES + PMULL */ 644 t = FIELD_DP64(t, ID_AA64ISAR0, SHA1, 1); 645 t = FIELD_DP64(t, ID_AA64ISAR0, SHA2, 2); /* SHA512 */ 646 t = FIELD_DP64(t, ID_AA64ISAR0, CRC32, 1); 647 t = FIELD_DP64(t, ID_AA64ISAR0, ATOMIC, 2); 648 t = FIELD_DP64(t, ID_AA64ISAR0, RDM, 1); 649 t = FIELD_DP64(t, ID_AA64ISAR0, SHA3, 1); 650 t = FIELD_DP64(t, ID_AA64ISAR0, SM3, 1); 651 t = FIELD_DP64(t, ID_AA64ISAR0, SM4, 1); 652 t = FIELD_DP64(t, ID_AA64ISAR0, DP, 1); 653 t = FIELD_DP64(t, ID_AA64ISAR0, FHM, 1); 654 t = FIELD_DP64(t, ID_AA64ISAR0, TS, 2); /* v8.5-CondM */ 655 t = FIELD_DP64(t, ID_AA64ISAR0, RNDR, 1); 656 cpu->isar.id_aa64isar0 = t; 657 658 t = cpu->isar.id_aa64isar1; 659 t = FIELD_DP64(t, ID_AA64ISAR1, DPB, 2); 660 t = FIELD_DP64(t, ID_AA64ISAR1, JSCVT, 1); 661 t = FIELD_DP64(t, ID_AA64ISAR1, FCMA, 1); 662 t = FIELD_DP64(t, ID_AA64ISAR1, SB, 1); 663 t = FIELD_DP64(t, ID_AA64ISAR1, SPECRES, 1); 664 t = FIELD_DP64(t, ID_AA64ISAR1, FRINTTS, 1); 665 t = FIELD_DP64(t, ID_AA64ISAR1, LRCPC, 2); /* ARMv8.4-RCPC */ 666 cpu->isar.id_aa64isar1 = t; 667 668 t = cpu->isar.id_aa64pfr0; 669 t = FIELD_DP64(t, ID_AA64PFR0, SVE, 1); 670 t = FIELD_DP64(t, ID_AA64PFR0, FP, 1); 671 t = FIELD_DP64(t, ID_AA64PFR0, ADVSIMD, 1); 672 cpu->isar.id_aa64pfr0 = t; 673 674 t = cpu->isar.id_aa64pfr1; 675 t = FIELD_DP64(t, ID_AA64PFR1, BT, 1); 676 /* 677 * Begin with full support for MTE. This will be downgraded to MTE=0 678 * during realize if the board provides no tag memory, much like 679 * we do for EL2 with the virtualization=on property. 680 */ 681 t = FIELD_DP64(t, ID_AA64PFR1, MTE, 2); 682 cpu->isar.id_aa64pfr1 = t; 683 684 t = cpu->isar.id_aa64mmfr0; 685 t = FIELD_DP64(t, ID_AA64MMFR0, PARANGE, 5); /* PARange: 48 bits */ 686 cpu->isar.id_aa64mmfr0 = t; 687 688 t = cpu->isar.id_aa64mmfr1; 689 t = FIELD_DP64(t, ID_AA64MMFR1, HPDS, 1); /* HPD */ 690 t = FIELD_DP64(t, ID_AA64MMFR1, LO, 1); 691 t = FIELD_DP64(t, ID_AA64MMFR1, VH, 1); 692 t = FIELD_DP64(t, ID_AA64MMFR1, PAN, 2); /* ATS1E1 */ 693 t = FIELD_DP64(t, ID_AA64MMFR1, VMIDBITS, 2); /* VMID16 */ 694 t = FIELD_DP64(t, ID_AA64MMFR1, XNX, 1); /* TTS2UXN */ 695 cpu->isar.id_aa64mmfr1 = t; 696 697 t = cpu->isar.id_aa64mmfr2; 698 t = FIELD_DP64(t, ID_AA64MMFR2, UAO, 1); 699 t = FIELD_DP64(t, ID_AA64MMFR2, CNP, 1); /* TTCNP */ 700 t = FIELD_DP64(t, ID_AA64MMFR2, ST, 1); /* TTST */ 701 cpu->isar.id_aa64mmfr2 = t; 702 703 /* Replicate the same data to the 32-bit id registers. */ 704 u = cpu->isar.id_isar5; 705 u = FIELD_DP32(u, ID_ISAR5, AES, 2); /* AES + PMULL */ 706 u = FIELD_DP32(u, ID_ISAR5, SHA1, 1); 707 u = FIELD_DP32(u, ID_ISAR5, SHA2, 1); 708 u = FIELD_DP32(u, ID_ISAR5, CRC32, 1); 709 u = FIELD_DP32(u, ID_ISAR5, RDM, 1); 710 u = FIELD_DP32(u, ID_ISAR5, VCMA, 1); 711 cpu->isar.id_isar5 = u; 712 713 u = cpu->isar.id_isar6; 714 u = FIELD_DP32(u, ID_ISAR6, JSCVT, 1); 715 u = FIELD_DP32(u, ID_ISAR6, DP, 1); 716 u = FIELD_DP32(u, ID_ISAR6, FHM, 1); 717 u = FIELD_DP32(u, ID_ISAR6, SB, 1); 718 u = FIELD_DP32(u, ID_ISAR6, SPECRES, 1); 719 cpu->isar.id_isar6 = u; 720 721 u = cpu->isar.id_mmfr3; 722 u = FIELD_DP32(u, ID_MMFR3, PAN, 2); /* ATS1E1 */ 723 cpu->isar.id_mmfr3 = u; 724 725 u = cpu->isar.id_mmfr4; 726 u = FIELD_DP32(u, ID_MMFR4, HPDS, 1); /* AA32HPD */ 727 u = FIELD_DP32(u, ID_MMFR4, AC2, 1); /* ACTLR2, HACTLR2 */ 728 u = FIELD_DP32(u, ID_MMFR4, CNP, 1); /* TTCNP */ 729 u = FIELD_DP32(u, ID_MMFR4, XNX, 1); /* TTS2UXN */ 730 cpu->isar.id_mmfr4 = u; 731 732 t = cpu->isar.id_aa64dfr0; 733 t = FIELD_DP64(t, ID_AA64DFR0, PMUVER, 5); /* v8.4-PMU */ 734 cpu->isar.id_aa64dfr0 = t; 735 736 u = cpu->isar.id_dfr0; 737 u = FIELD_DP32(u, ID_DFR0, PERFMON, 5); /* v8.4-PMU */ 738 cpu->isar.id_dfr0 = u; 739 740 u = cpu->isar.mvfr1; 741 u = FIELD_DP32(u, MVFR1, FPHP, 3); /* v8.2-FP16 */ 742 u = FIELD_DP32(u, MVFR1, SIMDHP, 2); /* v8.2-FP16 */ 743 cpu->isar.mvfr1 = u; 744 745 #ifdef CONFIG_USER_ONLY 746 /* For usermode -cpu max we can use a larger and more efficient DCZ 747 * blocksize since we don't have to follow what the hardware does. 748 */ 749 cpu->ctr = 0x80038003; /* 32 byte I and D cacheline size, VIPT icache */ 750 cpu->dcz_blocksize = 7; /* 512 bytes */ 751 #endif 752 753 /* Default to PAUTH on, with the architected algorithm. */ 754 qdev_property_add_static(DEVICE(obj), &arm_cpu_pauth_property); 755 qdev_property_add_static(DEVICE(obj), &arm_cpu_pauth_impdef_property); 756 } 757 758 aarch64_add_sve_properties(obj); 759 object_property_add(obj, "sve-max-vq", "uint32", cpu_max_get_sve_max_vq, 760 cpu_max_set_sve_max_vq, NULL, NULL); 761 } 762 763 static const ARMCPUInfo aarch64_cpus[] = { 764 { .name = "cortex-a57", .initfn = aarch64_a57_initfn }, 765 { .name = "cortex-a53", .initfn = aarch64_a53_initfn }, 766 { .name = "cortex-a72", .initfn = aarch64_a72_initfn }, 767 { .name = "max", .initfn = aarch64_max_initfn }, 768 }; 769 770 static bool aarch64_cpu_get_aarch64(Object *obj, Error **errp) 771 { 772 ARMCPU *cpu = ARM_CPU(obj); 773 774 return arm_feature(&cpu->env, ARM_FEATURE_AARCH64); 775 } 776 777 static void aarch64_cpu_set_aarch64(Object *obj, bool value, Error **errp) 778 { 779 ARMCPU *cpu = ARM_CPU(obj); 780 781 /* At this time, this property is only allowed if KVM is enabled. This 782 * restriction allows us to avoid fixing up functionality that assumes a 783 * uniform execution state like do_interrupt. 784 */ 785 if (value == false) { 786 if (!kvm_enabled() || !kvm_arm_aarch32_supported()) { 787 error_setg(errp, "'aarch64' feature cannot be disabled " 788 "unless KVM is enabled and 32-bit EL1 " 789 "is supported"); 790 return; 791 } 792 unset_feature(&cpu->env, ARM_FEATURE_AARCH64); 793 } else { 794 set_feature(&cpu->env, ARM_FEATURE_AARCH64); 795 } 796 } 797 798 static void aarch64_cpu_finalizefn(Object *obj) 799 { 800 } 801 802 static gchar *aarch64_gdb_arch_name(CPUState *cs) 803 { 804 return g_strdup("aarch64"); 805 } 806 807 static void aarch64_cpu_class_init(ObjectClass *oc, void *data) 808 { 809 CPUClass *cc = CPU_CLASS(oc); 810 811 cc->cpu_exec_interrupt = arm_cpu_exec_interrupt; 812 cc->gdb_read_register = aarch64_cpu_gdb_read_register; 813 cc->gdb_write_register = aarch64_cpu_gdb_write_register; 814 cc->gdb_num_core_regs = 34; 815 cc->gdb_core_xml_file = "aarch64-core.xml"; 816 cc->gdb_arch_name = aarch64_gdb_arch_name; 817 818 object_class_property_add_bool(oc, "aarch64", aarch64_cpu_get_aarch64, 819 aarch64_cpu_set_aarch64); 820 object_class_property_set_description(oc, "aarch64", 821 "Set on/off to enable/disable aarch64 " 822 "execution state "); 823 } 824 825 static void aarch64_cpu_instance_init(Object *obj) 826 { 827 ARMCPUClass *acc = ARM_CPU_GET_CLASS(obj); 828 829 acc->info->initfn(obj); 830 arm_cpu_post_init(obj); 831 } 832 833 static void cpu_register_class_init(ObjectClass *oc, void *data) 834 { 835 ARMCPUClass *acc = ARM_CPU_CLASS(oc); 836 837 acc->info = data; 838 } 839 840 void aarch64_cpu_register(const ARMCPUInfo *info) 841 { 842 TypeInfo type_info = { 843 .parent = TYPE_AARCH64_CPU, 844 .instance_size = sizeof(ARMCPU), 845 .instance_init = aarch64_cpu_instance_init, 846 .class_size = sizeof(ARMCPUClass), 847 .class_init = info->class_init ?: cpu_register_class_init, 848 .class_data = (void *)info, 849 }; 850 851 type_info.name = g_strdup_printf("%s-" TYPE_ARM_CPU, info->name); 852 type_register(&type_info); 853 g_free((void *)type_info.name); 854 } 855 856 static const TypeInfo aarch64_cpu_type_info = { 857 .name = TYPE_AARCH64_CPU, 858 .parent = TYPE_ARM_CPU, 859 .instance_size = sizeof(ARMCPU), 860 .instance_finalize = aarch64_cpu_finalizefn, 861 .abstract = true, 862 .class_size = sizeof(AArch64CPUClass), 863 .class_init = aarch64_cpu_class_init, 864 }; 865 866 static void aarch64_cpu_register_types(void) 867 { 868 size_t i; 869 870 type_register_static(&aarch64_cpu_type_info); 871 872 for (i = 0; i < ARRAY_SIZE(aarch64_cpus); ++i) { 873 aarch64_cpu_register(&aarch64_cpus[i]); 874 } 875 } 876 877 type_init(aarch64_cpu_register_types) 878