1 /* 2 * ARM mach-virt emulation 3 * 4 * Copyright (c) 2013 Linaro Limited 5 * 6 * This program is free software; you can redistribute it and/or modify it 7 * under the terms and conditions of the GNU General Public License, 8 * version 2 or later, as published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 13 * more details. 14 * 15 * You should have received a copy of the GNU General Public License along with 16 * this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 * Emulate a virtual board which works by passing Linux all the information 19 * it needs about what devices are present via the device tree. 20 * There are some restrictions about what we can do here: 21 * + we can only present devices whose Linux drivers will work based 22 * purely on the device tree with no platform data at all 23 * + we want to present a very stripped-down minimalist platform, 24 * both because this reduces the security attack surface from the guest 25 * and also because it reduces our exposure to being broken when 26 * the kernel updates its device tree bindings and requires further 27 * information in a device binding that we aren't providing. 28 * This is essentially the same approach kvmtool uses. 29 */ 30 31 #include "qemu/osdep.h" 32 #include "qemu/datadir.h" 33 #include "qemu/units.h" 34 #include "qemu/option.h" 35 #include "monitor/qdev.h" 36 #include "hw/sysbus.h" 37 #include "hw/arm/boot.h" 38 #include "hw/arm/primecell.h" 39 #include "hw/arm/virt.h" 40 #include "hw/block/flash.h" 41 #include "hw/vfio/vfio-calxeda-xgmac.h" 42 #include "hw/vfio/vfio-amd-xgbe.h" 43 #include "hw/display/ramfb.h" 44 #include "net/net.h" 45 #include "sysemu/device_tree.h" 46 #include "sysemu/numa.h" 47 #include "sysemu/runstate.h" 48 #include "sysemu/tpm.h" 49 #include "sysemu/tcg.h" 50 #include "sysemu/kvm.h" 51 #include "sysemu/hvf.h" 52 #include "sysemu/qtest.h" 53 #include "hw/loader.h" 54 #include "qapi/error.h" 55 #include "qemu/bitops.h" 56 #include "qemu/error-report.h" 57 #include "qemu/module.h" 58 #include "hw/pci-host/gpex.h" 59 #include "hw/virtio/virtio-pci.h" 60 #include "hw/core/sysbus-fdt.h" 61 #include "hw/platform-bus.h" 62 #include "hw/qdev-properties.h" 63 #include "hw/arm/fdt.h" 64 #include "hw/intc/arm_gic.h" 65 #include "hw/intc/arm_gicv3_common.h" 66 #include "hw/intc/arm_gicv3_its_common.h" 67 #include "hw/irq.h" 68 #include "kvm_arm.h" 69 #include "hw/firmware/smbios.h" 70 #include "qapi/visitor.h" 71 #include "qapi/qapi-visit-common.h" 72 #include "standard-headers/linux/input.h" 73 #include "hw/arm/smmuv3.h" 74 #include "hw/acpi/acpi.h" 75 #include "target/arm/internals.h" 76 #include "hw/mem/memory-device.h" 77 #include "hw/mem/pc-dimm.h" 78 #include "hw/mem/nvdimm.h" 79 #include "hw/acpi/generic_event_device.h" 80 #include "hw/virtio/virtio-mem-pci.h" 81 #include "hw/virtio/virtio-iommu.h" 82 #include "hw/char/pl011.h" 83 #include "qemu/guest-random.h" 84 85 #define DEFINE_VIRT_MACHINE_LATEST(major, minor, latest) \ 86 static void virt_##major##_##minor##_class_init(ObjectClass *oc, \ 87 void *data) \ 88 { \ 89 MachineClass *mc = MACHINE_CLASS(oc); \ 90 virt_machine_##major##_##minor##_options(mc); \ 91 mc->desc = "QEMU " # major "." # minor " ARM Virtual Machine"; \ 92 if (latest) { \ 93 mc->alias = "virt"; \ 94 } \ 95 } \ 96 static const TypeInfo machvirt_##major##_##minor##_info = { \ 97 .name = MACHINE_TYPE_NAME("virt-" # major "." # minor), \ 98 .parent = TYPE_VIRT_MACHINE, \ 99 .class_init = virt_##major##_##minor##_class_init, \ 100 }; \ 101 static void machvirt_machine_##major##_##minor##_init(void) \ 102 { \ 103 type_register_static(&machvirt_##major##_##minor##_info); \ 104 } \ 105 type_init(machvirt_machine_##major##_##minor##_init); 106 107 #define DEFINE_VIRT_MACHINE_AS_LATEST(major, minor) \ 108 DEFINE_VIRT_MACHINE_LATEST(major, minor, true) 109 #define DEFINE_VIRT_MACHINE(major, minor) \ 110 DEFINE_VIRT_MACHINE_LATEST(major, minor, false) 111 112 113 /* Number of external interrupt lines to configure the GIC with */ 114 #define NUM_IRQS 256 115 116 #define PLATFORM_BUS_NUM_IRQS 64 117 118 /* Legacy RAM limit in GB (< version 4.0) */ 119 #define LEGACY_RAMLIMIT_GB 255 120 #define LEGACY_RAMLIMIT_BYTES (LEGACY_RAMLIMIT_GB * GiB) 121 122 /* Addresses and sizes of our components. 123 * 0..128MB is space for a flash device so we can run bootrom code such as UEFI. 124 * 128MB..256MB is used for miscellaneous device I/O. 125 * 256MB..1GB is reserved for possible future PCI support (ie where the 126 * PCI memory window will go if we add a PCI host controller). 127 * 1GB and up is RAM (which may happily spill over into the 128 * high memory region beyond 4GB). 129 * This represents a compromise between how much RAM can be given to 130 * a 32 bit VM and leaving space for expansion and in particular for PCI. 131 * Note that devices should generally be placed at multiples of 0x10000, 132 * to accommodate guests using 64K pages. 133 */ 134 static const MemMapEntry base_memmap[] = { 135 /* Space up to 0x8000000 is reserved for a boot ROM */ 136 [VIRT_FLASH] = { 0, 0x08000000 }, 137 [VIRT_CPUPERIPHS] = { 0x08000000, 0x00020000 }, 138 /* GIC distributor and CPU interfaces sit inside the CPU peripheral space */ 139 [VIRT_GIC_DIST] = { 0x08000000, 0x00010000 }, 140 [VIRT_GIC_CPU] = { 0x08010000, 0x00010000 }, 141 [VIRT_GIC_V2M] = { 0x08020000, 0x00001000 }, 142 [VIRT_GIC_HYP] = { 0x08030000, 0x00010000 }, 143 [VIRT_GIC_VCPU] = { 0x08040000, 0x00010000 }, 144 /* The space in between here is reserved for GICv3 CPU/vCPU/HYP */ 145 [VIRT_GIC_ITS] = { 0x08080000, 0x00020000 }, 146 /* This redistributor space allows up to 2*64kB*123 CPUs */ 147 [VIRT_GIC_REDIST] = { 0x080A0000, 0x00F60000 }, 148 [VIRT_UART] = { 0x09000000, 0x00001000 }, 149 [VIRT_RTC] = { 0x09010000, 0x00001000 }, 150 [VIRT_FW_CFG] = { 0x09020000, 0x00000018 }, 151 [VIRT_GPIO] = { 0x09030000, 0x00001000 }, 152 [VIRT_SECURE_UART] = { 0x09040000, 0x00001000 }, 153 [VIRT_SMMU] = { 0x09050000, 0x00020000 }, 154 [VIRT_PCDIMM_ACPI] = { 0x09070000, MEMORY_HOTPLUG_IO_LEN }, 155 [VIRT_ACPI_GED] = { 0x09080000, ACPI_GED_EVT_SEL_LEN }, 156 [VIRT_NVDIMM_ACPI] = { 0x09090000, NVDIMM_ACPI_IO_LEN}, 157 [VIRT_PVTIME] = { 0x090a0000, 0x00010000 }, 158 [VIRT_SECURE_GPIO] = { 0x090b0000, 0x00001000 }, 159 [VIRT_MMIO] = { 0x0a000000, 0x00000200 }, 160 /* ...repeating for a total of NUM_VIRTIO_TRANSPORTS, each of that size */ 161 [VIRT_PLATFORM_BUS] = { 0x0c000000, 0x02000000 }, 162 [VIRT_SECURE_MEM] = { 0x0e000000, 0x01000000 }, 163 [VIRT_PCIE_MMIO] = { 0x10000000, 0x2eff0000 }, 164 [VIRT_PCIE_PIO] = { 0x3eff0000, 0x00010000 }, 165 [VIRT_PCIE_ECAM] = { 0x3f000000, 0x01000000 }, 166 /* Actual RAM size depends on initial RAM and device memory settings */ 167 [VIRT_MEM] = { GiB, LEGACY_RAMLIMIT_BYTES }, 168 }; 169 170 /* 171 * Highmem IO Regions: This memory map is floating, located after the RAM. 172 * Each MemMapEntry base (GPA) will be dynamically computed, depending on the 173 * top of the RAM, so that its base get the same alignment as the size, 174 * ie. a 512GiB entry will be aligned on a 512GiB boundary. If there is 175 * less than 256GiB of RAM, the floating area starts at the 256GiB mark. 176 * Note the extended_memmap is sized so that it eventually also includes the 177 * base_memmap entries (VIRT_HIGH_GIC_REDIST2 index is greater than the last 178 * index of base_memmap). 179 * 180 * The memory map for these Highmem IO Regions can be in legacy or compact 181 * layout, depending on 'compact-highmem' property. With legacy layout, the 182 * PA space for one specific region is always reserved, even if the region 183 * has been disabled or doesn't fit into the PA space. However, the PA space 184 * for the region won't be reserved in these circumstances with compact layout. 185 */ 186 static MemMapEntry extended_memmap[] = { 187 /* Additional 64 MB redist region (can contain up to 512 redistributors) */ 188 [VIRT_HIGH_GIC_REDIST2] = { 0x0, 64 * MiB }, 189 [VIRT_HIGH_PCIE_ECAM] = { 0x0, 256 * MiB }, 190 /* Second PCIe window */ 191 [VIRT_HIGH_PCIE_MMIO] = { 0x0, 512 * GiB }, 192 }; 193 194 static const int a15irqmap[] = { 195 [VIRT_UART] = 1, 196 [VIRT_RTC] = 2, 197 [VIRT_PCIE] = 3, /* ... to 6 */ 198 [VIRT_GPIO] = 7, 199 [VIRT_SECURE_UART] = 8, 200 [VIRT_ACPI_GED] = 9, 201 [VIRT_MMIO] = 16, /* ...to 16 + NUM_VIRTIO_TRANSPORTS - 1 */ 202 [VIRT_GIC_V2M] = 48, /* ...to 48 + NUM_GICV2M_SPIS - 1 */ 203 [VIRT_SMMU] = 74, /* ...to 74 + NUM_SMMU_IRQS - 1 */ 204 [VIRT_PLATFORM_BUS] = 112, /* ...to 112 + PLATFORM_BUS_NUM_IRQS -1 */ 205 }; 206 207 static const char *valid_cpus[] = { 208 #ifdef CONFIG_TCG 209 ARM_CPU_TYPE_NAME("cortex-a7"), 210 ARM_CPU_TYPE_NAME("cortex-a15"), 211 ARM_CPU_TYPE_NAME("cortex-a35"), 212 ARM_CPU_TYPE_NAME("cortex-a55"), 213 ARM_CPU_TYPE_NAME("cortex-a72"), 214 ARM_CPU_TYPE_NAME("cortex-a76"), 215 ARM_CPU_TYPE_NAME("a64fx"), 216 ARM_CPU_TYPE_NAME("neoverse-n1"), 217 ARM_CPU_TYPE_NAME("neoverse-v1"), 218 #endif 219 ARM_CPU_TYPE_NAME("cortex-a53"), 220 ARM_CPU_TYPE_NAME("cortex-a57"), 221 ARM_CPU_TYPE_NAME("host"), 222 ARM_CPU_TYPE_NAME("max"), 223 }; 224 225 static bool cpu_type_valid(const char *cpu) 226 { 227 int i; 228 229 for (i = 0; i < ARRAY_SIZE(valid_cpus); i++) { 230 if (strcmp(cpu, valid_cpus[i]) == 0) { 231 return true; 232 } 233 } 234 return false; 235 } 236 237 static void create_randomness(MachineState *ms, const char *node) 238 { 239 struct { 240 uint64_t kaslr; 241 uint8_t rng[32]; 242 } seed; 243 244 if (qemu_guest_getrandom(&seed, sizeof(seed), NULL)) { 245 return; 246 } 247 qemu_fdt_setprop_u64(ms->fdt, node, "kaslr-seed", seed.kaslr); 248 qemu_fdt_setprop(ms->fdt, node, "rng-seed", seed.rng, sizeof(seed.rng)); 249 } 250 251 static void create_fdt(VirtMachineState *vms) 252 { 253 MachineState *ms = MACHINE(vms); 254 int nb_numa_nodes = ms->numa_state->num_nodes; 255 void *fdt = create_device_tree(&vms->fdt_size); 256 257 if (!fdt) { 258 error_report("create_device_tree() failed"); 259 exit(1); 260 } 261 262 ms->fdt = fdt; 263 264 /* Header */ 265 qemu_fdt_setprop_string(fdt, "/", "compatible", "linux,dummy-virt"); 266 qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2); 267 qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2); 268 qemu_fdt_setprop_string(fdt, "/", "model", "linux,dummy-virt"); 269 270 /* /chosen must exist for load_dtb to fill in necessary properties later */ 271 qemu_fdt_add_subnode(fdt, "/chosen"); 272 if (vms->dtb_randomness) { 273 create_randomness(ms, "/chosen"); 274 } 275 276 if (vms->secure) { 277 qemu_fdt_add_subnode(fdt, "/secure-chosen"); 278 if (vms->dtb_randomness) { 279 create_randomness(ms, "/secure-chosen"); 280 } 281 } 282 283 /* Clock node, for the benefit of the UART. The kernel device tree 284 * binding documentation claims the PL011 node clock properties are 285 * optional but in practice if you omit them the kernel refuses to 286 * probe for the device. 287 */ 288 vms->clock_phandle = qemu_fdt_alloc_phandle(fdt); 289 qemu_fdt_add_subnode(fdt, "/apb-pclk"); 290 qemu_fdt_setprop_string(fdt, "/apb-pclk", "compatible", "fixed-clock"); 291 qemu_fdt_setprop_cell(fdt, "/apb-pclk", "#clock-cells", 0x0); 292 qemu_fdt_setprop_cell(fdt, "/apb-pclk", "clock-frequency", 24000000); 293 qemu_fdt_setprop_string(fdt, "/apb-pclk", "clock-output-names", 294 "clk24mhz"); 295 qemu_fdt_setprop_cell(fdt, "/apb-pclk", "phandle", vms->clock_phandle); 296 297 if (nb_numa_nodes > 0 && ms->numa_state->have_numa_distance) { 298 int size = nb_numa_nodes * nb_numa_nodes * 3 * sizeof(uint32_t); 299 uint32_t *matrix = g_malloc0(size); 300 int idx, i, j; 301 302 for (i = 0; i < nb_numa_nodes; i++) { 303 for (j = 0; j < nb_numa_nodes; j++) { 304 idx = (i * nb_numa_nodes + j) * 3; 305 matrix[idx + 0] = cpu_to_be32(i); 306 matrix[idx + 1] = cpu_to_be32(j); 307 matrix[idx + 2] = 308 cpu_to_be32(ms->numa_state->nodes[i].distance[j]); 309 } 310 } 311 312 qemu_fdt_add_subnode(fdt, "/distance-map"); 313 qemu_fdt_setprop_string(fdt, "/distance-map", "compatible", 314 "numa-distance-map-v1"); 315 qemu_fdt_setprop(fdt, "/distance-map", "distance-matrix", 316 matrix, size); 317 g_free(matrix); 318 } 319 } 320 321 static void fdt_add_timer_nodes(const VirtMachineState *vms) 322 { 323 /* On real hardware these interrupts are level-triggered. 324 * On KVM they were edge-triggered before host kernel version 4.4, 325 * and level-triggered afterwards. 326 * On emulated QEMU they are level-triggered. 327 * 328 * Getting the DTB info about them wrong is awkward for some 329 * guest kernels: 330 * pre-4.8 ignore the DT and leave the interrupt configured 331 * with whatever the GIC reset value (or the bootloader) left it at 332 * 4.8 before rc6 honour the incorrect data by programming it back 333 * into the GIC, causing problems 334 * 4.8rc6 and later ignore the DT and always write "level triggered" 335 * into the GIC 336 * 337 * For backwards-compatibility, virt-2.8 and earlier will continue 338 * to say these are edge-triggered, but later machines will report 339 * the correct information. 340 */ 341 ARMCPU *armcpu; 342 VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms); 343 uint32_t irqflags = GIC_FDT_IRQ_FLAGS_LEVEL_HI; 344 MachineState *ms = MACHINE(vms); 345 346 if (vmc->claim_edge_triggered_timers) { 347 irqflags = GIC_FDT_IRQ_FLAGS_EDGE_LO_HI; 348 } 349 350 if (vms->gic_version == VIRT_GIC_VERSION_2) { 351 irqflags = deposit32(irqflags, GIC_FDT_IRQ_PPI_CPU_START, 352 GIC_FDT_IRQ_PPI_CPU_WIDTH, 353 (1 << MACHINE(vms)->smp.cpus) - 1); 354 } 355 356 qemu_fdt_add_subnode(ms->fdt, "/timer"); 357 358 armcpu = ARM_CPU(qemu_get_cpu(0)); 359 if (arm_feature(&armcpu->env, ARM_FEATURE_V8)) { 360 const char compat[] = "arm,armv8-timer\0arm,armv7-timer"; 361 qemu_fdt_setprop(ms->fdt, "/timer", "compatible", 362 compat, sizeof(compat)); 363 } else { 364 qemu_fdt_setprop_string(ms->fdt, "/timer", "compatible", 365 "arm,armv7-timer"); 366 } 367 qemu_fdt_setprop(ms->fdt, "/timer", "always-on", NULL, 0); 368 qemu_fdt_setprop_cells(ms->fdt, "/timer", "interrupts", 369 GIC_FDT_IRQ_TYPE_PPI, ARCH_TIMER_S_EL1_IRQ, irqflags, 370 GIC_FDT_IRQ_TYPE_PPI, ARCH_TIMER_NS_EL1_IRQ, irqflags, 371 GIC_FDT_IRQ_TYPE_PPI, ARCH_TIMER_VIRT_IRQ, irqflags, 372 GIC_FDT_IRQ_TYPE_PPI, ARCH_TIMER_NS_EL2_IRQ, irqflags); 373 } 374 375 static void fdt_add_cpu_nodes(const VirtMachineState *vms) 376 { 377 int cpu; 378 int addr_cells = 1; 379 const MachineState *ms = MACHINE(vms); 380 const VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms); 381 int smp_cpus = ms->smp.cpus; 382 383 /* 384 * See Linux Documentation/devicetree/bindings/arm/cpus.yaml 385 * On ARM v8 64-bit systems value should be set to 2, 386 * that corresponds to the MPIDR_EL1 register size. 387 * If MPIDR_EL1[63:32] value is equal to 0 on all CPUs 388 * in the system, #address-cells can be set to 1, since 389 * MPIDR_EL1[63:32] bits are not used for CPUs 390 * identification. 391 * 392 * Here we actually don't know whether our system is 32- or 64-bit one. 393 * The simplest way to go is to examine affinity IDs of all our CPUs. If 394 * at least one of them has Aff3 populated, we set #address-cells to 2. 395 */ 396 for (cpu = 0; cpu < smp_cpus; cpu++) { 397 ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(cpu)); 398 399 if (armcpu->mp_affinity & ARM_AFF3_MASK) { 400 addr_cells = 2; 401 break; 402 } 403 } 404 405 qemu_fdt_add_subnode(ms->fdt, "/cpus"); 406 qemu_fdt_setprop_cell(ms->fdt, "/cpus", "#address-cells", addr_cells); 407 qemu_fdt_setprop_cell(ms->fdt, "/cpus", "#size-cells", 0x0); 408 409 for (cpu = smp_cpus - 1; cpu >= 0; cpu--) { 410 char *nodename = g_strdup_printf("/cpus/cpu@%d", cpu); 411 ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(cpu)); 412 CPUState *cs = CPU(armcpu); 413 414 qemu_fdt_add_subnode(ms->fdt, nodename); 415 qemu_fdt_setprop_string(ms->fdt, nodename, "device_type", "cpu"); 416 qemu_fdt_setprop_string(ms->fdt, nodename, "compatible", 417 armcpu->dtb_compatible); 418 419 if (vms->psci_conduit != QEMU_PSCI_CONDUIT_DISABLED && smp_cpus > 1) { 420 qemu_fdt_setprop_string(ms->fdt, nodename, 421 "enable-method", "psci"); 422 } 423 424 if (addr_cells == 2) { 425 qemu_fdt_setprop_u64(ms->fdt, nodename, "reg", 426 armcpu->mp_affinity); 427 } else { 428 qemu_fdt_setprop_cell(ms->fdt, nodename, "reg", 429 armcpu->mp_affinity); 430 } 431 432 if (ms->possible_cpus->cpus[cs->cpu_index].props.has_node_id) { 433 qemu_fdt_setprop_cell(ms->fdt, nodename, "numa-node-id", 434 ms->possible_cpus->cpus[cs->cpu_index].props.node_id); 435 } 436 437 if (!vmc->no_cpu_topology) { 438 qemu_fdt_setprop_cell(ms->fdt, nodename, "phandle", 439 qemu_fdt_alloc_phandle(ms->fdt)); 440 } 441 442 g_free(nodename); 443 } 444 445 if (!vmc->no_cpu_topology) { 446 /* 447 * Add vCPU topology description through fdt node cpu-map. 448 * 449 * See Linux Documentation/devicetree/bindings/cpu/cpu-topology.txt 450 * In a SMP system, the hierarchy of CPUs can be defined through 451 * four entities that are used to describe the layout of CPUs in 452 * the system: socket/cluster/core/thread. 453 * 454 * A socket node represents the boundary of system physical package 455 * and its child nodes must be one or more cluster nodes. A system 456 * can contain several layers of clustering within a single physical 457 * package and cluster nodes can be contained in parent cluster nodes. 458 * 459 * Note: currently we only support one layer of clustering within 460 * each physical package. 461 */ 462 qemu_fdt_add_subnode(ms->fdt, "/cpus/cpu-map"); 463 464 for (cpu = smp_cpus - 1; cpu >= 0; cpu--) { 465 char *cpu_path = g_strdup_printf("/cpus/cpu@%d", cpu); 466 char *map_path; 467 468 if (ms->smp.threads > 1) { 469 map_path = g_strdup_printf( 470 "/cpus/cpu-map/socket%d/cluster%d/core%d/thread%d", 471 cpu / (ms->smp.clusters * ms->smp.cores * ms->smp.threads), 472 (cpu / (ms->smp.cores * ms->smp.threads)) % ms->smp.clusters, 473 (cpu / ms->smp.threads) % ms->smp.cores, 474 cpu % ms->smp.threads); 475 } else { 476 map_path = g_strdup_printf( 477 "/cpus/cpu-map/socket%d/cluster%d/core%d", 478 cpu / (ms->smp.clusters * ms->smp.cores), 479 (cpu / ms->smp.cores) % ms->smp.clusters, 480 cpu % ms->smp.cores); 481 } 482 qemu_fdt_add_path(ms->fdt, map_path); 483 qemu_fdt_setprop_phandle(ms->fdt, map_path, "cpu", cpu_path); 484 485 g_free(map_path); 486 g_free(cpu_path); 487 } 488 } 489 } 490 491 static void fdt_add_its_gic_node(VirtMachineState *vms) 492 { 493 char *nodename; 494 MachineState *ms = MACHINE(vms); 495 496 vms->msi_phandle = qemu_fdt_alloc_phandle(ms->fdt); 497 nodename = g_strdup_printf("/intc/its@%" PRIx64, 498 vms->memmap[VIRT_GIC_ITS].base); 499 qemu_fdt_add_subnode(ms->fdt, nodename); 500 qemu_fdt_setprop_string(ms->fdt, nodename, "compatible", 501 "arm,gic-v3-its"); 502 qemu_fdt_setprop(ms->fdt, nodename, "msi-controller", NULL, 0); 503 qemu_fdt_setprop_cell(ms->fdt, nodename, "#msi-cells", 1); 504 qemu_fdt_setprop_sized_cells(ms->fdt, nodename, "reg", 505 2, vms->memmap[VIRT_GIC_ITS].base, 506 2, vms->memmap[VIRT_GIC_ITS].size); 507 qemu_fdt_setprop_cell(ms->fdt, nodename, "phandle", vms->msi_phandle); 508 g_free(nodename); 509 } 510 511 static void fdt_add_v2m_gic_node(VirtMachineState *vms) 512 { 513 MachineState *ms = MACHINE(vms); 514 char *nodename; 515 516 nodename = g_strdup_printf("/intc/v2m@%" PRIx64, 517 vms->memmap[VIRT_GIC_V2M].base); 518 vms->msi_phandle = qemu_fdt_alloc_phandle(ms->fdt); 519 qemu_fdt_add_subnode(ms->fdt, nodename); 520 qemu_fdt_setprop_string(ms->fdt, nodename, "compatible", 521 "arm,gic-v2m-frame"); 522 qemu_fdt_setprop(ms->fdt, nodename, "msi-controller", NULL, 0); 523 qemu_fdt_setprop_sized_cells(ms->fdt, nodename, "reg", 524 2, vms->memmap[VIRT_GIC_V2M].base, 525 2, vms->memmap[VIRT_GIC_V2M].size); 526 qemu_fdt_setprop_cell(ms->fdt, nodename, "phandle", vms->msi_phandle); 527 g_free(nodename); 528 } 529 530 static void fdt_add_gic_node(VirtMachineState *vms) 531 { 532 MachineState *ms = MACHINE(vms); 533 char *nodename; 534 535 vms->gic_phandle = qemu_fdt_alloc_phandle(ms->fdt); 536 qemu_fdt_setprop_cell(ms->fdt, "/", "interrupt-parent", vms->gic_phandle); 537 538 nodename = g_strdup_printf("/intc@%" PRIx64, 539 vms->memmap[VIRT_GIC_DIST].base); 540 qemu_fdt_add_subnode(ms->fdt, nodename); 541 qemu_fdt_setprop_cell(ms->fdt, nodename, "#interrupt-cells", 3); 542 qemu_fdt_setprop(ms->fdt, nodename, "interrupt-controller", NULL, 0); 543 qemu_fdt_setprop_cell(ms->fdt, nodename, "#address-cells", 0x2); 544 qemu_fdt_setprop_cell(ms->fdt, nodename, "#size-cells", 0x2); 545 qemu_fdt_setprop(ms->fdt, nodename, "ranges", NULL, 0); 546 if (vms->gic_version != VIRT_GIC_VERSION_2) { 547 int nb_redist_regions = virt_gicv3_redist_region_count(vms); 548 549 qemu_fdt_setprop_string(ms->fdt, nodename, "compatible", 550 "arm,gic-v3"); 551 552 qemu_fdt_setprop_cell(ms->fdt, nodename, 553 "#redistributor-regions", nb_redist_regions); 554 555 if (nb_redist_regions == 1) { 556 qemu_fdt_setprop_sized_cells(ms->fdt, nodename, "reg", 557 2, vms->memmap[VIRT_GIC_DIST].base, 558 2, vms->memmap[VIRT_GIC_DIST].size, 559 2, vms->memmap[VIRT_GIC_REDIST].base, 560 2, vms->memmap[VIRT_GIC_REDIST].size); 561 } else { 562 qemu_fdt_setprop_sized_cells(ms->fdt, nodename, "reg", 563 2, vms->memmap[VIRT_GIC_DIST].base, 564 2, vms->memmap[VIRT_GIC_DIST].size, 565 2, vms->memmap[VIRT_GIC_REDIST].base, 566 2, vms->memmap[VIRT_GIC_REDIST].size, 567 2, vms->memmap[VIRT_HIGH_GIC_REDIST2].base, 568 2, vms->memmap[VIRT_HIGH_GIC_REDIST2].size); 569 } 570 571 if (vms->virt) { 572 qemu_fdt_setprop_cells(ms->fdt, nodename, "interrupts", 573 GIC_FDT_IRQ_TYPE_PPI, ARCH_GIC_MAINT_IRQ, 574 GIC_FDT_IRQ_FLAGS_LEVEL_HI); 575 } 576 } else { 577 /* 'cortex-a15-gic' means 'GIC v2' */ 578 qemu_fdt_setprop_string(ms->fdt, nodename, "compatible", 579 "arm,cortex-a15-gic"); 580 if (!vms->virt) { 581 qemu_fdt_setprop_sized_cells(ms->fdt, nodename, "reg", 582 2, vms->memmap[VIRT_GIC_DIST].base, 583 2, vms->memmap[VIRT_GIC_DIST].size, 584 2, vms->memmap[VIRT_GIC_CPU].base, 585 2, vms->memmap[VIRT_GIC_CPU].size); 586 } else { 587 qemu_fdt_setprop_sized_cells(ms->fdt, nodename, "reg", 588 2, vms->memmap[VIRT_GIC_DIST].base, 589 2, vms->memmap[VIRT_GIC_DIST].size, 590 2, vms->memmap[VIRT_GIC_CPU].base, 591 2, vms->memmap[VIRT_GIC_CPU].size, 592 2, vms->memmap[VIRT_GIC_HYP].base, 593 2, vms->memmap[VIRT_GIC_HYP].size, 594 2, vms->memmap[VIRT_GIC_VCPU].base, 595 2, vms->memmap[VIRT_GIC_VCPU].size); 596 qemu_fdt_setprop_cells(ms->fdt, nodename, "interrupts", 597 GIC_FDT_IRQ_TYPE_PPI, ARCH_GIC_MAINT_IRQ, 598 GIC_FDT_IRQ_FLAGS_LEVEL_HI); 599 } 600 } 601 602 qemu_fdt_setprop_cell(ms->fdt, nodename, "phandle", vms->gic_phandle); 603 g_free(nodename); 604 } 605 606 static void fdt_add_pmu_nodes(const VirtMachineState *vms) 607 { 608 ARMCPU *armcpu = ARM_CPU(first_cpu); 609 uint32_t irqflags = GIC_FDT_IRQ_FLAGS_LEVEL_HI; 610 MachineState *ms = MACHINE(vms); 611 612 if (!arm_feature(&armcpu->env, ARM_FEATURE_PMU)) { 613 assert(!object_property_get_bool(OBJECT(armcpu), "pmu", NULL)); 614 return; 615 } 616 617 if (vms->gic_version == VIRT_GIC_VERSION_2) { 618 irqflags = deposit32(irqflags, GIC_FDT_IRQ_PPI_CPU_START, 619 GIC_FDT_IRQ_PPI_CPU_WIDTH, 620 (1 << MACHINE(vms)->smp.cpus) - 1); 621 } 622 623 qemu_fdt_add_subnode(ms->fdt, "/pmu"); 624 if (arm_feature(&armcpu->env, ARM_FEATURE_V8)) { 625 const char compat[] = "arm,armv8-pmuv3"; 626 qemu_fdt_setprop(ms->fdt, "/pmu", "compatible", 627 compat, sizeof(compat)); 628 qemu_fdt_setprop_cells(ms->fdt, "/pmu", "interrupts", 629 GIC_FDT_IRQ_TYPE_PPI, VIRTUAL_PMU_IRQ, irqflags); 630 } 631 } 632 633 static inline DeviceState *create_acpi_ged(VirtMachineState *vms) 634 { 635 DeviceState *dev; 636 MachineState *ms = MACHINE(vms); 637 int irq = vms->irqmap[VIRT_ACPI_GED]; 638 uint32_t event = ACPI_GED_PWR_DOWN_EVT; 639 640 if (ms->ram_slots) { 641 event |= ACPI_GED_MEM_HOTPLUG_EVT; 642 } 643 644 if (ms->nvdimms_state->is_enabled) { 645 event |= ACPI_GED_NVDIMM_HOTPLUG_EVT; 646 } 647 648 dev = qdev_new(TYPE_ACPI_GED); 649 qdev_prop_set_uint32(dev, "ged-event", event); 650 651 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, vms->memmap[VIRT_ACPI_GED].base); 652 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 1, vms->memmap[VIRT_PCDIMM_ACPI].base); 653 sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0, qdev_get_gpio_in(vms->gic, irq)); 654 655 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal); 656 657 return dev; 658 } 659 660 static void create_its(VirtMachineState *vms) 661 { 662 const char *itsclass = its_class_name(); 663 DeviceState *dev; 664 665 if (!strcmp(itsclass, "arm-gicv3-its")) { 666 if (!vms->tcg_its) { 667 itsclass = NULL; 668 } 669 } 670 671 if (!itsclass) { 672 /* Do nothing if not supported */ 673 return; 674 } 675 676 dev = qdev_new(itsclass); 677 678 object_property_set_link(OBJECT(dev), "parent-gicv3", OBJECT(vms->gic), 679 &error_abort); 680 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal); 681 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, vms->memmap[VIRT_GIC_ITS].base); 682 683 fdt_add_its_gic_node(vms); 684 vms->msi_controller = VIRT_MSI_CTRL_ITS; 685 } 686 687 static void create_v2m(VirtMachineState *vms) 688 { 689 int i; 690 int irq = vms->irqmap[VIRT_GIC_V2M]; 691 DeviceState *dev; 692 693 dev = qdev_new("arm-gicv2m"); 694 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, vms->memmap[VIRT_GIC_V2M].base); 695 qdev_prop_set_uint32(dev, "base-spi", irq); 696 qdev_prop_set_uint32(dev, "num-spi", NUM_GICV2M_SPIS); 697 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal); 698 699 for (i = 0; i < NUM_GICV2M_SPIS; i++) { 700 sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, 701 qdev_get_gpio_in(vms->gic, irq + i)); 702 } 703 704 fdt_add_v2m_gic_node(vms); 705 vms->msi_controller = VIRT_MSI_CTRL_GICV2M; 706 } 707 708 static void create_gic(VirtMachineState *vms, MemoryRegion *mem) 709 { 710 MachineState *ms = MACHINE(vms); 711 /* We create a standalone GIC */ 712 SysBusDevice *gicbusdev; 713 const char *gictype; 714 int i; 715 unsigned int smp_cpus = ms->smp.cpus; 716 uint32_t nb_redist_regions = 0; 717 int revision; 718 719 if (vms->gic_version == VIRT_GIC_VERSION_2) { 720 gictype = gic_class_name(); 721 } else { 722 gictype = gicv3_class_name(); 723 } 724 725 switch (vms->gic_version) { 726 case VIRT_GIC_VERSION_2: 727 revision = 2; 728 break; 729 case VIRT_GIC_VERSION_3: 730 revision = 3; 731 break; 732 case VIRT_GIC_VERSION_4: 733 revision = 4; 734 break; 735 default: 736 g_assert_not_reached(); 737 } 738 vms->gic = qdev_new(gictype); 739 qdev_prop_set_uint32(vms->gic, "revision", revision); 740 qdev_prop_set_uint32(vms->gic, "num-cpu", smp_cpus); 741 /* Note that the num-irq property counts both internal and external 742 * interrupts; there are always 32 of the former (mandated by GIC spec). 743 */ 744 qdev_prop_set_uint32(vms->gic, "num-irq", NUM_IRQS + 32); 745 if (!kvm_irqchip_in_kernel()) { 746 qdev_prop_set_bit(vms->gic, "has-security-extensions", vms->secure); 747 } 748 749 if (vms->gic_version != VIRT_GIC_VERSION_2) { 750 uint32_t redist0_capacity = virt_redist_capacity(vms, VIRT_GIC_REDIST); 751 uint32_t redist0_count = MIN(smp_cpus, redist0_capacity); 752 753 nb_redist_regions = virt_gicv3_redist_region_count(vms); 754 755 qdev_prop_set_uint32(vms->gic, "len-redist-region-count", 756 nb_redist_regions); 757 qdev_prop_set_uint32(vms->gic, "redist-region-count[0]", redist0_count); 758 759 if (!kvm_irqchip_in_kernel()) { 760 if (vms->tcg_its) { 761 object_property_set_link(OBJECT(vms->gic), "sysmem", 762 OBJECT(mem), &error_fatal); 763 qdev_prop_set_bit(vms->gic, "has-lpi", true); 764 } 765 } 766 767 if (nb_redist_regions == 2) { 768 uint32_t redist1_capacity = 769 virt_redist_capacity(vms, VIRT_HIGH_GIC_REDIST2); 770 771 qdev_prop_set_uint32(vms->gic, "redist-region-count[1]", 772 MIN(smp_cpus - redist0_count, redist1_capacity)); 773 } 774 } else { 775 if (!kvm_irqchip_in_kernel()) { 776 qdev_prop_set_bit(vms->gic, "has-virtualization-extensions", 777 vms->virt); 778 } 779 } 780 gicbusdev = SYS_BUS_DEVICE(vms->gic); 781 sysbus_realize_and_unref(gicbusdev, &error_fatal); 782 sysbus_mmio_map(gicbusdev, 0, vms->memmap[VIRT_GIC_DIST].base); 783 if (vms->gic_version != VIRT_GIC_VERSION_2) { 784 sysbus_mmio_map(gicbusdev, 1, vms->memmap[VIRT_GIC_REDIST].base); 785 if (nb_redist_regions == 2) { 786 sysbus_mmio_map(gicbusdev, 2, 787 vms->memmap[VIRT_HIGH_GIC_REDIST2].base); 788 } 789 } else { 790 sysbus_mmio_map(gicbusdev, 1, vms->memmap[VIRT_GIC_CPU].base); 791 if (vms->virt) { 792 sysbus_mmio_map(gicbusdev, 2, vms->memmap[VIRT_GIC_HYP].base); 793 sysbus_mmio_map(gicbusdev, 3, vms->memmap[VIRT_GIC_VCPU].base); 794 } 795 } 796 797 /* Wire the outputs from each CPU's generic timer and the GICv3 798 * maintenance interrupt signal to the appropriate GIC PPI inputs, 799 * and the GIC's IRQ/FIQ/VIRQ/VFIQ interrupt outputs to the CPU's inputs. 800 */ 801 for (i = 0; i < smp_cpus; i++) { 802 DeviceState *cpudev = DEVICE(qemu_get_cpu(i)); 803 int ppibase = NUM_IRQS + i * GIC_INTERNAL + GIC_NR_SGIS; 804 int irq; 805 /* Mapping from the output timer irq lines from the CPU to the 806 * GIC PPI inputs we use for the virt board. 807 */ 808 const int timer_irq[] = { 809 [GTIMER_PHYS] = ARCH_TIMER_NS_EL1_IRQ, 810 [GTIMER_VIRT] = ARCH_TIMER_VIRT_IRQ, 811 [GTIMER_HYP] = ARCH_TIMER_NS_EL2_IRQ, 812 [GTIMER_SEC] = ARCH_TIMER_S_EL1_IRQ, 813 }; 814 815 for (irq = 0; irq < ARRAY_SIZE(timer_irq); irq++) { 816 qdev_connect_gpio_out(cpudev, irq, 817 qdev_get_gpio_in(vms->gic, 818 ppibase + timer_irq[irq])); 819 } 820 821 if (vms->gic_version != VIRT_GIC_VERSION_2) { 822 qemu_irq irq = qdev_get_gpio_in(vms->gic, 823 ppibase + ARCH_GIC_MAINT_IRQ); 824 qdev_connect_gpio_out_named(cpudev, "gicv3-maintenance-interrupt", 825 0, irq); 826 } else if (vms->virt) { 827 qemu_irq irq = qdev_get_gpio_in(vms->gic, 828 ppibase + ARCH_GIC_MAINT_IRQ); 829 sysbus_connect_irq(gicbusdev, i + 4 * smp_cpus, irq); 830 } 831 832 qdev_connect_gpio_out_named(cpudev, "pmu-interrupt", 0, 833 qdev_get_gpio_in(vms->gic, ppibase 834 + VIRTUAL_PMU_IRQ)); 835 836 sysbus_connect_irq(gicbusdev, i, qdev_get_gpio_in(cpudev, ARM_CPU_IRQ)); 837 sysbus_connect_irq(gicbusdev, i + smp_cpus, 838 qdev_get_gpio_in(cpudev, ARM_CPU_FIQ)); 839 sysbus_connect_irq(gicbusdev, i + 2 * smp_cpus, 840 qdev_get_gpio_in(cpudev, ARM_CPU_VIRQ)); 841 sysbus_connect_irq(gicbusdev, i + 3 * smp_cpus, 842 qdev_get_gpio_in(cpudev, ARM_CPU_VFIQ)); 843 } 844 845 fdt_add_gic_node(vms); 846 847 if (vms->gic_version != VIRT_GIC_VERSION_2 && vms->its) { 848 create_its(vms); 849 } else if (vms->gic_version == VIRT_GIC_VERSION_2) { 850 create_v2m(vms); 851 } 852 } 853 854 static void create_uart(const VirtMachineState *vms, int uart, 855 MemoryRegion *mem, Chardev *chr) 856 { 857 char *nodename; 858 hwaddr base = vms->memmap[uart].base; 859 hwaddr size = vms->memmap[uart].size; 860 int irq = vms->irqmap[uart]; 861 const char compat[] = "arm,pl011\0arm,primecell"; 862 const char clocknames[] = "uartclk\0apb_pclk"; 863 DeviceState *dev = qdev_new(TYPE_PL011); 864 SysBusDevice *s = SYS_BUS_DEVICE(dev); 865 MachineState *ms = MACHINE(vms); 866 867 qdev_prop_set_chr(dev, "chardev", chr); 868 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal); 869 memory_region_add_subregion(mem, base, 870 sysbus_mmio_get_region(s, 0)); 871 sysbus_connect_irq(s, 0, qdev_get_gpio_in(vms->gic, irq)); 872 873 nodename = g_strdup_printf("/pl011@%" PRIx64, base); 874 qemu_fdt_add_subnode(ms->fdt, nodename); 875 /* Note that we can't use setprop_string because of the embedded NUL */ 876 qemu_fdt_setprop(ms->fdt, nodename, "compatible", 877 compat, sizeof(compat)); 878 qemu_fdt_setprop_sized_cells(ms->fdt, nodename, "reg", 879 2, base, 2, size); 880 qemu_fdt_setprop_cells(ms->fdt, nodename, "interrupts", 881 GIC_FDT_IRQ_TYPE_SPI, irq, 882 GIC_FDT_IRQ_FLAGS_LEVEL_HI); 883 qemu_fdt_setprop_cells(ms->fdt, nodename, "clocks", 884 vms->clock_phandle, vms->clock_phandle); 885 qemu_fdt_setprop(ms->fdt, nodename, "clock-names", 886 clocknames, sizeof(clocknames)); 887 888 if (uart == VIRT_UART) { 889 qemu_fdt_setprop_string(ms->fdt, "/chosen", "stdout-path", nodename); 890 } else { 891 /* Mark as not usable by the normal world */ 892 qemu_fdt_setprop_string(ms->fdt, nodename, "status", "disabled"); 893 qemu_fdt_setprop_string(ms->fdt, nodename, "secure-status", "okay"); 894 895 qemu_fdt_setprop_string(ms->fdt, "/secure-chosen", "stdout-path", 896 nodename); 897 } 898 899 g_free(nodename); 900 } 901 902 static void create_rtc(const VirtMachineState *vms) 903 { 904 char *nodename; 905 hwaddr base = vms->memmap[VIRT_RTC].base; 906 hwaddr size = vms->memmap[VIRT_RTC].size; 907 int irq = vms->irqmap[VIRT_RTC]; 908 const char compat[] = "arm,pl031\0arm,primecell"; 909 MachineState *ms = MACHINE(vms); 910 911 sysbus_create_simple("pl031", base, qdev_get_gpio_in(vms->gic, irq)); 912 913 nodename = g_strdup_printf("/pl031@%" PRIx64, base); 914 qemu_fdt_add_subnode(ms->fdt, nodename); 915 qemu_fdt_setprop(ms->fdt, nodename, "compatible", compat, sizeof(compat)); 916 qemu_fdt_setprop_sized_cells(ms->fdt, nodename, "reg", 917 2, base, 2, size); 918 qemu_fdt_setprop_cells(ms->fdt, nodename, "interrupts", 919 GIC_FDT_IRQ_TYPE_SPI, irq, 920 GIC_FDT_IRQ_FLAGS_LEVEL_HI); 921 qemu_fdt_setprop_cell(ms->fdt, nodename, "clocks", vms->clock_phandle); 922 qemu_fdt_setprop_string(ms->fdt, nodename, "clock-names", "apb_pclk"); 923 g_free(nodename); 924 } 925 926 static DeviceState *gpio_key_dev; 927 static void virt_powerdown_req(Notifier *n, void *opaque) 928 { 929 VirtMachineState *s = container_of(n, VirtMachineState, powerdown_notifier); 930 931 if (s->acpi_dev) { 932 acpi_send_event(s->acpi_dev, ACPI_POWER_DOWN_STATUS); 933 } else { 934 /* use gpio Pin 3 for power button event */ 935 qemu_set_irq(qdev_get_gpio_in(gpio_key_dev, 0), 1); 936 } 937 } 938 939 static void create_gpio_keys(char *fdt, DeviceState *pl061_dev, 940 uint32_t phandle) 941 { 942 gpio_key_dev = sysbus_create_simple("gpio-key", -1, 943 qdev_get_gpio_in(pl061_dev, 3)); 944 945 qemu_fdt_add_subnode(fdt, "/gpio-keys"); 946 qemu_fdt_setprop_string(fdt, "/gpio-keys", "compatible", "gpio-keys"); 947 948 qemu_fdt_add_subnode(fdt, "/gpio-keys/poweroff"); 949 qemu_fdt_setprop_string(fdt, "/gpio-keys/poweroff", 950 "label", "GPIO Key Poweroff"); 951 qemu_fdt_setprop_cell(fdt, "/gpio-keys/poweroff", "linux,code", 952 KEY_POWER); 953 qemu_fdt_setprop_cells(fdt, "/gpio-keys/poweroff", 954 "gpios", phandle, 3, 0); 955 } 956 957 #define SECURE_GPIO_POWEROFF 0 958 #define SECURE_GPIO_RESET 1 959 960 static void create_secure_gpio_pwr(char *fdt, DeviceState *pl061_dev, 961 uint32_t phandle) 962 { 963 DeviceState *gpio_pwr_dev; 964 965 /* gpio-pwr */ 966 gpio_pwr_dev = sysbus_create_simple("gpio-pwr", -1, NULL); 967 968 /* connect secure pl061 to gpio-pwr */ 969 qdev_connect_gpio_out(pl061_dev, SECURE_GPIO_RESET, 970 qdev_get_gpio_in_named(gpio_pwr_dev, "reset", 0)); 971 qdev_connect_gpio_out(pl061_dev, SECURE_GPIO_POWEROFF, 972 qdev_get_gpio_in_named(gpio_pwr_dev, "shutdown", 0)); 973 974 qemu_fdt_add_subnode(fdt, "/gpio-poweroff"); 975 qemu_fdt_setprop_string(fdt, "/gpio-poweroff", "compatible", 976 "gpio-poweroff"); 977 qemu_fdt_setprop_cells(fdt, "/gpio-poweroff", 978 "gpios", phandle, SECURE_GPIO_POWEROFF, 0); 979 qemu_fdt_setprop_string(fdt, "/gpio-poweroff", "status", "disabled"); 980 qemu_fdt_setprop_string(fdt, "/gpio-poweroff", "secure-status", 981 "okay"); 982 983 qemu_fdt_add_subnode(fdt, "/gpio-restart"); 984 qemu_fdt_setprop_string(fdt, "/gpio-restart", "compatible", 985 "gpio-restart"); 986 qemu_fdt_setprop_cells(fdt, "/gpio-restart", 987 "gpios", phandle, SECURE_GPIO_RESET, 0); 988 qemu_fdt_setprop_string(fdt, "/gpio-restart", "status", "disabled"); 989 qemu_fdt_setprop_string(fdt, "/gpio-restart", "secure-status", 990 "okay"); 991 } 992 993 static void create_gpio_devices(const VirtMachineState *vms, int gpio, 994 MemoryRegion *mem) 995 { 996 char *nodename; 997 DeviceState *pl061_dev; 998 hwaddr base = vms->memmap[gpio].base; 999 hwaddr size = vms->memmap[gpio].size; 1000 int irq = vms->irqmap[gpio]; 1001 const char compat[] = "arm,pl061\0arm,primecell"; 1002 SysBusDevice *s; 1003 MachineState *ms = MACHINE(vms); 1004 1005 pl061_dev = qdev_new("pl061"); 1006 /* Pull lines down to 0 if not driven by the PL061 */ 1007 qdev_prop_set_uint32(pl061_dev, "pullups", 0); 1008 qdev_prop_set_uint32(pl061_dev, "pulldowns", 0xff); 1009 s = SYS_BUS_DEVICE(pl061_dev); 1010 sysbus_realize_and_unref(s, &error_fatal); 1011 memory_region_add_subregion(mem, base, sysbus_mmio_get_region(s, 0)); 1012 sysbus_connect_irq(s, 0, qdev_get_gpio_in(vms->gic, irq)); 1013 1014 uint32_t phandle = qemu_fdt_alloc_phandle(ms->fdt); 1015 nodename = g_strdup_printf("/pl061@%" PRIx64, base); 1016 qemu_fdt_add_subnode(ms->fdt, nodename); 1017 qemu_fdt_setprop_sized_cells(ms->fdt, nodename, "reg", 1018 2, base, 2, size); 1019 qemu_fdt_setprop(ms->fdt, nodename, "compatible", compat, sizeof(compat)); 1020 qemu_fdt_setprop_cell(ms->fdt, nodename, "#gpio-cells", 2); 1021 qemu_fdt_setprop(ms->fdt, nodename, "gpio-controller", NULL, 0); 1022 qemu_fdt_setprop_cells(ms->fdt, nodename, "interrupts", 1023 GIC_FDT_IRQ_TYPE_SPI, irq, 1024 GIC_FDT_IRQ_FLAGS_LEVEL_HI); 1025 qemu_fdt_setprop_cell(ms->fdt, nodename, "clocks", vms->clock_phandle); 1026 qemu_fdt_setprop_string(ms->fdt, nodename, "clock-names", "apb_pclk"); 1027 qemu_fdt_setprop_cell(ms->fdt, nodename, "phandle", phandle); 1028 1029 if (gpio != VIRT_GPIO) { 1030 /* Mark as not usable by the normal world */ 1031 qemu_fdt_setprop_string(ms->fdt, nodename, "status", "disabled"); 1032 qemu_fdt_setprop_string(ms->fdt, nodename, "secure-status", "okay"); 1033 } 1034 g_free(nodename); 1035 1036 /* Child gpio devices */ 1037 if (gpio == VIRT_GPIO) { 1038 create_gpio_keys(ms->fdt, pl061_dev, phandle); 1039 } else { 1040 create_secure_gpio_pwr(ms->fdt, pl061_dev, phandle); 1041 } 1042 } 1043 1044 static void create_virtio_devices(const VirtMachineState *vms) 1045 { 1046 int i; 1047 hwaddr size = vms->memmap[VIRT_MMIO].size; 1048 MachineState *ms = MACHINE(vms); 1049 1050 /* We create the transports in forwards order. Since qbus_realize() 1051 * prepends (not appends) new child buses, the incrementing loop below will 1052 * create a list of virtio-mmio buses with decreasing base addresses. 1053 * 1054 * When a -device option is processed from the command line, 1055 * qbus_find_recursive() picks the next free virtio-mmio bus in forwards 1056 * order. The upshot is that -device options in increasing command line 1057 * order are mapped to virtio-mmio buses with decreasing base addresses. 1058 * 1059 * When this code was originally written, that arrangement ensured that the 1060 * guest Linux kernel would give the lowest "name" (/dev/vda, eth0, etc) to 1061 * the first -device on the command line. (The end-to-end order is a 1062 * function of this loop, qbus_realize(), qbus_find_recursive(), and the 1063 * guest kernel's name-to-address assignment strategy.) 1064 * 1065 * Meanwhile, the kernel's traversal seems to have been reversed; see eg. 1066 * the message, if not necessarily the code, of commit 70161ff336. 1067 * Therefore the loop now establishes the inverse of the original intent. 1068 * 1069 * Unfortunately, we can't counteract the kernel change by reversing the 1070 * loop; it would break existing command lines. 1071 * 1072 * In any case, the kernel makes no guarantee about the stability of 1073 * enumeration order of virtio devices (as demonstrated by it changing 1074 * between kernel versions). For reliable and stable identification 1075 * of disks users must use UUIDs or similar mechanisms. 1076 */ 1077 for (i = 0; i < NUM_VIRTIO_TRANSPORTS; i++) { 1078 int irq = vms->irqmap[VIRT_MMIO] + i; 1079 hwaddr base = vms->memmap[VIRT_MMIO].base + i * size; 1080 1081 sysbus_create_simple("virtio-mmio", base, 1082 qdev_get_gpio_in(vms->gic, irq)); 1083 } 1084 1085 /* We add dtb nodes in reverse order so that they appear in the finished 1086 * device tree lowest address first. 1087 * 1088 * Note that this mapping is independent of the loop above. The previous 1089 * loop influences virtio device to virtio transport assignment, whereas 1090 * this loop controls how virtio transports are laid out in the dtb. 1091 */ 1092 for (i = NUM_VIRTIO_TRANSPORTS - 1; i >= 0; i--) { 1093 char *nodename; 1094 int irq = vms->irqmap[VIRT_MMIO] + i; 1095 hwaddr base = vms->memmap[VIRT_MMIO].base + i * size; 1096 1097 nodename = g_strdup_printf("/virtio_mmio@%" PRIx64, base); 1098 qemu_fdt_add_subnode(ms->fdt, nodename); 1099 qemu_fdt_setprop_string(ms->fdt, nodename, 1100 "compatible", "virtio,mmio"); 1101 qemu_fdt_setprop_sized_cells(ms->fdt, nodename, "reg", 1102 2, base, 2, size); 1103 qemu_fdt_setprop_cells(ms->fdt, nodename, "interrupts", 1104 GIC_FDT_IRQ_TYPE_SPI, irq, 1105 GIC_FDT_IRQ_FLAGS_EDGE_LO_HI); 1106 qemu_fdt_setprop(ms->fdt, nodename, "dma-coherent", NULL, 0); 1107 g_free(nodename); 1108 } 1109 } 1110 1111 #define VIRT_FLASH_SECTOR_SIZE (256 * KiB) 1112 1113 static PFlashCFI01 *virt_flash_create1(VirtMachineState *vms, 1114 const char *name, 1115 const char *alias_prop_name) 1116 { 1117 /* 1118 * Create a single flash device. We use the same parameters as 1119 * the flash devices on the Versatile Express board. 1120 */ 1121 DeviceState *dev = qdev_new(TYPE_PFLASH_CFI01); 1122 1123 qdev_prop_set_uint64(dev, "sector-length", VIRT_FLASH_SECTOR_SIZE); 1124 qdev_prop_set_uint8(dev, "width", 4); 1125 qdev_prop_set_uint8(dev, "device-width", 2); 1126 qdev_prop_set_bit(dev, "big-endian", false); 1127 qdev_prop_set_uint16(dev, "id0", 0x89); 1128 qdev_prop_set_uint16(dev, "id1", 0x18); 1129 qdev_prop_set_uint16(dev, "id2", 0x00); 1130 qdev_prop_set_uint16(dev, "id3", 0x00); 1131 qdev_prop_set_string(dev, "name", name); 1132 object_property_add_child(OBJECT(vms), name, OBJECT(dev)); 1133 object_property_add_alias(OBJECT(vms), alias_prop_name, 1134 OBJECT(dev), "drive"); 1135 return PFLASH_CFI01(dev); 1136 } 1137 1138 static void virt_flash_create(VirtMachineState *vms) 1139 { 1140 vms->flash[0] = virt_flash_create1(vms, "virt.flash0", "pflash0"); 1141 vms->flash[1] = virt_flash_create1(vms, "virt.flash1", "pflash1"); 1142 } 1143 1144 static void virt_flash_map1(PFlashCFI01 *flash, 1145 hwaddr base, hwaddr size, 1146 MemoryRegion *sysmem) 1147 { 1148 DeviceState *dev = DEVICE(flash); 1149 1150 assert(QEMU_IS_ALIGNED(size, VIRT_FLASH_SECTOR_SIZE)); 1151 assert(size / VIRT_FLASH_SECTOR_SIZE <= UINT32_MAX); 1152 qdev_prop_set_uint32(dev, "num-blocks", size / VIRT_FLASH_SECTOR_SIZE); 1153 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal); 1154 1155 memory_region_add_subregion(sysmem, base, 1156 sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 1157 0)); 1158 } 1159 1160 static void virt_flash_map(VirtMachineState *vms, 1161 MemoryRegion *sysmem, 1162 MemoryRegion *secure_sysmem) 1163 { 1164 /* 1165 * Map two flash devices to fill the VIRT_FLASH space in the memmap. 1166 * sysmem is the system memory space. secure_sysmem is the secure view 1167 * of the system, and the first flash device should be made visible only 1168 * there. The second flash device is visible to both secure and nonsecure. 1169 * If sysmem == secure_sysmem this means there is no separate Secure 1170 * address space and both flash devices are generally visible. 1171 */ 1172 hwaddr flashsize = vms->memmap[VIRT_FLASH].size / 2; 1173 hwaddr flashbase = vms->memmap[VIRT_FLASH].base; 1174 1175 virt_flash_map1(vms->flash[0], flashbase, flashsize, 1176 secure_sysmem); 1177 virt_flash_map1(vms->flash[1], flashbase + flashsize, flashsize, 1178 sysmem); 1179 } 1180 1181 static void virt_flash_fdt(VirtMachineState *vms, 1182 MemoryRegion *sysmem, 1183 MemoryRegion *secure_sysmem) 1184 { 1185 hwaddr flashsize = vms->memmap[VIRT_FLASH].size / 2; 1186 hwaddr flashbase = vms->memmap[VIRT_FLASH].base; 1187 MachineState *ms = MACHINE(vms); 1188 char *nodename; 1189 1190 if (sysmem == secure_sysmem) { 1191 /* Report both flash devices as a single node in the DT */ 1192 nodename = g_strdup_printf("/flash@%" PRIx64, flashbase); 1193 qemu_fdt_add_subnode(ms->fdt, nodename); 1194 qemu_fdt_setprop_string(ms->fdt, nodename, "compatible", "cfi-flash"); 1195 qemu_fdt_setprop_sized_cells(ms->fdt, nodename, "reg", 1196 2, flashbase, 2, flashsize, 1197 2, flashbase + flashsize, 2, flashsize); 1198 qemu_fdt_setprop_cell(ms->fdt, nodename, "bank-width", 4); 1199 g_free(nodename); 1200 } else { 1201 /* 1202 * Report the devices as separate nodes so we can mark one as 1203 * only visible to the secure world. 1204 */ 1205 nodename = g_strdup_printf("/secflash@%" PRIx64, flashbase); 1206 qemu_fdt_add_subnode(ms->fdt, nodename); 1207 qemu_fdt_setprop_string(ms->fdt, nodename, "compatible", "cfi-flash"); 1208 qemu_fdt_setprop_sized_cells(ms->fdt, nodename, "reg", 1209 2, flashbase, 2, flashsize); 1210 qemu_fdt_setprop_cell(ms->fdt, nodename, "bank-width", 4); 1211 qemu_fdt_setprop_string(ms->fdt, nodename, "status", "disabled"); 1212 qemu_fdt_setprop_string(ms->fdt, nodename, "secure-status", "okay"); 1213 g_free(nodename); 1214 1215 nodename = g_strdup_printf("/flash@%" PRIx64, flashbase + flashsize); 1216 qemu_fdt_add_subnode(ms->fdt, nodename); 1217 qemu_fdt_setprop_string(ms->fdt, nodename, "compatible", "cfi-flash"); 1218 qemu_fdt_setprop_sized_cells(ms->fdt, nodename, "reg", 1219 2, flashbase + flashsize, 2, flashsize); 1220 qemu_fdt_setprop_cell(ms->fdt, nodename, "bank-width", 4); 1221 g_free(nodename); 1222 } 1223 } 1224 1225 static bool virt_firmware_init(VirtMachineState *vms, 1226 MemoryRegion *sysmem, 1227 MemoryRegion *secure_sysmem) 1228 { 1229 int i; 1230 const char *bios_name; 1231 BlockBackend *pflash_blk0; 1232 1233 /* Map legacy -drive if=pflash to machine properties */ 1234 for (i = 0; i < ARRAY_SIZE(vms->flash); i++) { 1235 pflash_cfi01_legacy_drive(vms->flash[i], 1236 drive_get(IF_PFLASH, 0, i)); 1237 } 1238 1239 virt_flash_map(vms, sysmem, secure_sysmem); 1240 1241 pflash_blk0 = pflash_cfi01_get_blk(vms->flash[0]); 1242 1243 bios_name = MACHINE(vms)->firmware; 1244 if (bios_name) { 1245 char *fname; 1246 MemoryRegion *mr; 1247 int image_size; 1248 1249 if (pflash_blk0) { 1250 error_report("The contents of the first flash device may be " 1251 "specified with -bios or with -drive if=pflash... " 1252 "but you cannot use both options at once"); 1253 exit(1); 1254 } 1255 1256 /* Fall back to -bios */ 1257 1258 fname = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name); 1259 if (!fname) { 1260 error_report("Could not find ROM image '%s'", bios_name); 1261 exit(1); 1262 } 1263 mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(vms->flash[0]), 0); 1264 image_size = load_image_mr(fname, mr); 1265 g_free(fname); 1266 if (image_size < 0) { 1267 error_report("Could not load ROM image '%s'", bios_name); 1268 exit(1); 1269 } 1270 } 1271 1272 return pflash_blk0 || bios_name; 1273 } 1274 1275 static FWCfgState *create_fw_cfg(const VirtMachineState *vms, AddressSpace *as) 1276 { 1277 MachineState *ms = MACHINE(vms); 1278 hwaddr base = vms->memmap[VIRT_FW_CFG].base; 1279 hwaddr size = vms->memmap[VIRT_FW_CFG].size; 1280 FWCfgState *fw_cfg; 1281 char *nodename; 1282 1283 fw_cfg = fw_cfg_init_mem_wide(base + 8, base, 8, base + 16, as); 1284 fw_cfg_add_i16(fw_cfg, FW_CFG_NB_CPUS, (uint16_t)ms->smp.cpus); 1285 1286 nodename = g_strdup_printf("/fw-cfg@%" PRIx64, base); 1287 qemu_fdt_add_subnode(ms->fdt, nodename); 1288 qemu_fdt_setprop_string(ms->fdt, nodename, 1289 "compatible", "qemu,fw-cfg-mmio"); 1290 qemu_fdt_setprop_sized_cells(ms->fdt, nodename, "reg", 1291 2, base, 2, size); 1292 qemu_fdt_setprop(ms->fdt, nodename, "dma-coherent", NULL, 0); 1293 g_free(nodename); 1294 return fw_cfg; 1295 } 1296 1297 static void create_pcie_irq_map(const MachineState *ms, 1298 uint32_t gic_phandle, 1299 int first_irq, const char *nodename) 1300 { 1301 int devfn, pin; 1302 uint32_t full_irq_map[4 * 4 * 10] = { 0 }; 1303 uint32_t *irq_map = full_irq_map; 1304 1305 for (devfn = 0; devfn <= 0x18; devfn += 0x8) { 1306 for (pin = 0; pin < 4; pin++) { 1307 int irq_type = GIC_FDT_IRQ_TYPE_SPI; 1308 int irq_nr = first_irq + ((pin + PCI_SLOT(devfn)) % PCI_NUM_PINS); 1309 int irq_level = GIC_FDT_IRQ_FLAGS_LEVEL_HI; 1310 int i; 1311 1312 uint32_t map[] = { 1313 devfn << 8, 0, 0, /* devfn */ 1314 pin + 1, /* PCI pin */ 1315 gic_phandle, 0, 0, irq_type, irq_nr, irq_level }; /* GIC irq */ 1316 1317 /* Convert map to big endian */ 1318 for (i = 0; i < 10; i++) { 1319 irq_map[i] = cpu_to_be32(map[i]); 1320 } 1321 irq_map += 10; 1322 } 1323 } 1324 1325 qemu_fdt_setprop(ms->fdt, nodename, "interrupt-map", 1326 full_irq_map, sizeof(full_irq_map)); 1327 1328 qemu_fdt_setprop_cells(ms->fdt, nodename, "interrupt-map-mask", 1329 cpu_to_be16(PCI_DEVFN(3, 0)), /* Slot 3 */ 1330 0, 0, 1331 0x7 /* PCI irq */); 1332 } 1333 1334 static void create_smmu(const VirtMachineState *vms, 1335 PCIBus *bus) 1336 { 1337 char *node; 1338 const char compat[] = "arm,smmu-v3"; 1339 int irq = vms->irqmap[VIRT_SMMU]; 1340 int i; 1341 hwaddr base = vms->memmap[VIRT_SMMU].base; 1342 hwaddr size = vms->memmap[VIRT_SMMU].size; 1343 const char irq_names[] = "eventq\0priq\0cmdq-sync\0gerror"; 1344 DeviceState *dev; 1345 MachineState *ms = MACHINE(vms); 1346 1347 if (vms->iommu != VIRT_IOMMU_SMMUV3 || !vms->iommu_phandle) { 1348 return; 1349 } 1350 1351 dev = qdev_new(TYPE_ARM_SMMUV3); 1352 1353 object_property_set_link(OBJECT(dev), "primary-bus", OBJECT(bus), 1354 &error_abort); 1355 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal); 1356 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base); 1357 for (i = 0; i < NUM_SMMU_IRQS; i++) { 1358 sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, 1359 qdev_get_gpio_in(vms->gic, irq + i)); 1360 } 1361 1362 node = g_strdup_printf("/smmuv3@%" PRIx64, base); 1363 qemu_fdt_add_subnode(ms->fdt, node); 1364 qemu_fdt_setprop(ms->fdt, node, "compatible", compat, sizeof(compat)); 1365 qemu_fdt_setprop_sized_cells(ms->fdt, node, "reg", 2, base, 2, size); 1366 1367 qemu_fdt_setprop_cells(ms->fdt, node, "interrupts", 1368 GIC_FDT_IRQ_TYPE_SPI, irq , GIC_FDT_IRQ_FLAGS_EDGE_LO_HI, 1369 GIC_FDT_IRQ_TYPE_SPI, irq + 1, GIC_FDT_IRQ_FLAGS_EDGE_LO_HI, 1370 GIC_FDT_IRQ_TYPE_SPI, irq + 2, GIC_FDT_IRQ_FLAGS_EDGE_LO_HI, 1371 GIC_FDT_IRQ_TYPE_SPI, irq + 3, GIC_FDT_IRQ_FLAGS_EDGE_LO_HI); 1372 1373 qemu_fdt_setprop(ms->fdt, node, "interrupt-names", irq_names, 1374 sizeof(irq_names)); 1375 1376 qemu_fdt_setprop(ms->fdt, node, "dma-coherent", NULL, 0); 1377 1378 qemu_fdt_setprop_cell(ms->fdt, node, "#iommu-cells", 1); 1379 1380 qemu_fdt_setprop_cell(ms->fdt, node, "phandle", vms->iommu_phandle); 1381 g_free(node); 1382 } 1383 1384 static void create_virtio_iommu_dt_bindings(VirtMachineState *vms) 1385 { 1386 const char compat[] = "virtio,pci-iommu\0pci1af4,1057"; 1387 uint16_t bdf = vms->virtio_iommu_bdf; 1388 MachineState *ms = MACHINE(vms); 1389 char *node; 1390 1391 vms->iommu_phandle = qemu_fdt_alloc_phandle(ms->fdt); 1392 1393 node = g_strdup_printf("%s/virtio_iommu@%x,%x", vms->pciehb_nodename, 1394 PCI_SLOT(bdf), PCI_FUNC(bdf)); 1395 qemu_fdt_add_subnode(ms->fdt, node); 1396 qemu_fdt_setprop(ms->fdt, node, "compatible", compat, sizeof(compat)); 1397 qemu_fdt_setprop_sized_cells(ms->fdt, node, "reg", 1398 1, bdf << 8, 1, 0, 1, 0, 1399 1, 0, 1, 0); 1400 1401 qemu_fdt_setprop_cell(ms->fdt, node, "#iommu-cells", 1); 1402 qemu_fdt_setprop_cell(ms->fdt, node, "phandle", vms->iommu_phandle); 1403 g_free(node); 1404 1405 qemu_fdt_setprop_cells(ms->fdt, vms->pciehb_nodename, "iommu-map", 1406 0x0, vms->iommu_phandle, 0x0, bdf, 1407 bdf + 1, vms->iommu_phandle, bdf + 1, 0xffff - bdf); 1408 } 1409 1410 static void create_pcie(VirtMachineState *vms) 1411 { 1412 hwaddr base_mmio = vms->memmap[VIRT_PCIE_MMIO].base; 1413 hwaddr size_mmio = vms->memmap[VIRT_PCIE_MMIO].size; 1414 hwaddr base_mmio_high = vms->memmap[VIRT_HIGH_PCIE_MMIO].base; 1415 hwaddr size_mmio_high = vms->memmap[VIRT_HIGH_PCIE_MMIO].size; 1416 hwaddr base_pio = vms->memmap[VIRT_PCIE_PIO].base; 1417 hwaddr size_pio = vms->memmap[VIRT_PCIE_PIO].size; 1418 hwaddr base_ecam, size_ecam; 1419 hwaddr base = base_mmio; 1420 int nr_pcie_buses; 1421 int irq = vms->irqmap[VIRT_PCIE]; 1422 MemoryRegion *mmio_alias; 1423 MemoryRegion *mmio_reg; 1424 MemoryRegion *ecam_alias; 1425 MemoryRegion *ecam_reg; 1426 DeviceState *dev; 1427 char *nodename; 1428 int i, ecam_id; 1429 PCIHostState *pci; 1430 MachineState *ms = MACHINE(vms); 1431 MachineClass *mc = MACHINE_GET_CLASS(ms); 1432 1433 dev = qdev_new(TYPE_GPEX_HOST); 1434 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal); 1435 1436 ecam_id = VIRT_ECAM_ID(vms->highmem_ecam); 1437 base_ecam = vms->memmap[ecam_id].base; 1438 size_ecam = vms->memmap[ecam_id].size; 1439 nr_pcie_buses = size_ecam / PCIE_MMCFG_SIZE_MIN; 1440 /* Map only the first size_ecam bytes of ECAM space */ 1441 ecam_alias = g_new0(MemoryRegion, 1); 1442 ecam_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0); 1443 memory_region_init_alias(ecam_alias, OBJECT(dev), "pcie-ecam", 1444 ecam_reg, 0, size_ecam); 1445 memory_region_add_subregion(get_system_memory(), base_ecam, ecam_alias); 1446 1447 /* Map the MMIO window into system address space so as to expose 1448 * the section of PCI MMIO space which starts at the same base address 1449 * (ie 1:1 mapping for that part of PCI MMIO space visible through 1450 * the window). 1451 */ 1452 mmio_alias = g_new0(MemoryRegion, 1); 1453 mmio_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 1); 1454 memory_region_init_alias(mmio_alias, OBJECT(dev), "pcie-mmio", 1455 mmio_reg, base_mmio, size_mmio); 1456 memory_region_add_subregion(get_system_memory(), base_mmio, mmio_alias); 1457 1458 if (vms->highmem_mmio) { 1459 /* Map high MMIO space */ 1460 MemoryRegion *high_mmio_alias = g_new0(MemoryRegion, 1); 1461 1462 memory_region_init_alias(high_mmio_alias, OBJECT(dev), "pcie-mmio-high", 1463 mmio_reg, base_mmio_high, size_mmio_high); 1464 memory_region_add_subregion(get_system_memory(), base_mmio_high, 1465 high_mmio_alias); 1466 } 1467 1468 /* Map IO port space */ 1469 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 2, base_pio); 1470 1471 for (i = 0; i < GPEX_NUM_IRQS; i++) { 1472 sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, 1473 qdev_get_gpio_in(vms->gic, irq + i)); 1474 gpex_set_irq_num(GPEX_HOST(dev), i, irq + i); 1475 } 1476 1477 pci = PCI_HOST_BRIDGE(dev); 1478 pci->bypass_iommu = vms->default_bus_bypass_iommu; 1479 vms->bus = pci->bus; 1480 if (vms->bus) { 1481 for (i = 0; i < nb_nics; i++) { 1482 NICInfo *nd = &nd_table[i]; 1483 1484 if (!nd->model) { 1485 nd->model = g_strdup(mc->default_nic); 1486 } 1487 1488 pci_nic_init_nofail(nd, pci->bus, nd->model, NULL); 1489 } 1490 } 1491 1492 nodename = vms->pciehb_nodename = g_strdup_printf("/pcie@%" PRIx64, base); 1493 qemu_fdt_add_subnode(ms->fdt, nodename); 1494 qemu_fdt_setprop_string(ms->fdt, nodename, 1495 "compatible", "pci-host-ecam-generic"); 1496 qemu_fdt_setprop_string(ms->fdt, nodename, "device_type", "pci"); 1497 qemu_fdt_setprop_cell(ms->fdt, nodename, "#address-cells", 3); 1498 qemu_fdt_setprop_cell(ms->fdt, nodename, "#size-cells", 2); 1499 qemu_fdt_setprop_cell(ms->fdt, nodename, "linux,pci-domain", 0); 1500 qemu_fdt_setprop_cells(ms->fdt, nodename, "bus-range", 0, 1501 nr_pcie_buses - 1); 1502 qemu_fdt_setprop(ms->fdt, nodename, "dma-coherent", NULL, 0); 1503 1504 if (vms->msi_phandle) { 1505 qemu_fdt_setprop_cells(ms->fdt, nodename, "msi-map", 1506 0, vms->msi_phandle, 0, 0x10000); 1507 } 1508 1509 qemu_fdt_setprop_sized_cells(ms->fdt, nodename, "reg", 1510 2, base_ecam, 2, size_ecam); 1511 1512 if (vms->highmem_mmio) { 1513 qemu_fdt_setprop_sized_cells(ms->fdt, nodename, "ranges", 1514 1, FDT_PCI_RANGE_IOPORT, 2, 0, 1515 2, base_pio, 2, size_pio, 1516 1, FDT_PCI_RANGE_MMIO, 2, base_mmio, 1517 2, base_mmio, 2, size_mmio, 1518 1, FDT_PCI_RANGE_MMIO_64BIT, 1519 2, base_mmio_high, 1520 2, base_mmio_high, 2, size_mmio_high); 1521 } else { 1522 qemu_fdt_setprop_sized_cells(ms->fdt, nodename, "ranges", 1523 1, FDT_PCI_RANGE_IOPORT, 2, 0, 1524 2, base_pio, 2, size_pio, 1525 1, FDT_PCI_RANGE_MMIO, 2, base_mmio, 1526 2, base_mmio, 2, size_mmio); 1527 } 1528 1529 qemu_fdt_setprop_cell(ms->fdt, nodename, "#interrupt-cells", 1); 1530 create_pcie_irq_map(ms, vms->gic_phandle, irq, nodename); 1531 1532 if (vms->iommu) { 1533 vms->iommu_phandle = qemu_fdt_alloc_phandle(ms->fdt); 1534 1535 switch (vms->iommu) { 1536 case VIRT_IOMMU_SMMUV3: 1537 create_smmu(vms, vms->bus); 1538 qemu_fdt_setprop_cells(ms->fdt, nodename, "iommu-map", 1539 0x0, vms->iommu_phandle, 0x0, 0x10000); 1540 break; 1541 default: 1542 g_assert_not_reached(); 1543 } 1544 } 1545 } 1546 1547 static void create_platform_bus(VirtMachineState *vms) 1548 { 1549 DeviceState *dev; 1550 SysBusDevice *s; 1551 int i; 1552 MemoryRegion *sysmem = get_system_memory(); 1553 1554 dev = qdev_new(TYPE_PLATFORM_BUS_DEVICE); 1555 dev->id = g_strdup(TYPE_PLATFORM_BUS_DEVICE); 1556 qdev_prop_set_uint32(dev, "num_irqs", PLATFORM_BUS_NUM_IRQS); 1557 qdev_prop_set_uint32(dev, "mmio_size", vms->memmap[VIRT_PLATFORM_BUS].size); 1558 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal); 1559 vms->platform_bus_dev = dev; 1560 1561 s = SYS_BUS_DEVICE(dev); 1562 for (i = 0; i < PLATFORM_BUS_NUM_IRQS; i++) { 1563 int irq = vms->irqmap[VIRT_PLATFORM_BUS] + i; 1564 sysbus_connect_irq(s, i, qdev_get_gpio_in(vms->gic, irq)); 1565 } 1566 1567 memory_region_add_subregion(sysmem, 1568 vms->memmap[VIRT_PLATFORM_BUS].base, 1569 sysbus_mmio_get_region(s, 0)); 1570 } 1571 1572 static void create_tag_ram(MemoryRegion *tag_sysmem, 1573 hwaddr base, hwaddr size, 1574 const char *name) 1575 { 1576 MemoryRegion *tagram = g_new(MemoryRegion, 1); 1577 1578 memory_region_init_ram(tagram, NULL, name, size / 32, &error_fatal); 1579 memory_region_add_subregion(tag_sysmem, base / 32, tagram); 1580 } 1581 1582 static void create_secure_ram(VirtMachineState *vms, 1583 MemoryRegion *secure_sysmem, 1584 MemoryRegion *secure_tag_sysmem) 1585 { 1586 MemoryRegion *secram = g_new(MemoryRegion, 1); 1587 char *nodename; 1588 hwaddr base = vms->memmap[VIRT_SECURE_MEM].base; 1589 hwaddr size = vms->memmap[VIRT_SECURE_MEM].size; 1590 MachineState *ms = MACHINE(vms); 1591 1592 memory_region_init_ram(secram, NULL, "virt.secure-ram", size, 1593 &error_fatal); 1594 memory_region_add_subregion(secure_sysmem, base, secram); 1595 1596 nodename = g_strdup_printf("/secram@%" PRIx64, base); 1597 qemu_fdt_add_subnode(ms->fdt, nodename); 1598 qemu_fdt_setprop_string(ms->fdt, nodename, "device_type", "memory"); 1599 qemu_fdt_setprop_sized_cells(ms->fdt, nodename, "reg", 2, base, 2, size); 1600 qemu_fdt_setprop_string(ms->fdt, nodename, "status", "disabled"); 1601 qemu_fdt_setprop_string(ms->fdt, nodename, "secure-status", "okay"); 1602 1603 if (secure_tag_sysmem) { 1604 create_tag_ram(secure_tag_sysmem, base, size, "mach-virt.secure-tag"); 1605 } 1606 1607 g_free(nodename); 1608 } 1609 1610 static void *machvirt_dtb(const struct arm_boot_info *binfo, int *fdt_size) 1611 { 1612 const VirtMachineState *board = container_of(binfo, VirtMachineState, 1613 bootinfo); 1614 MachineState *ms = MACHINE(board); 1615 1616 1617 *fdt_size = board->fdt_size; 1618 return ms->fdt; 1619 } 1620 1621 static void virt_build_smbios(VirtMachineState *vms) 1622 { 1623 MachineClass *mc = MACHINE_GET_CLASS(vms); 1624 MachineState *ms = MACHINE(vms); 1625 VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms); 1626 uint8_t *smbios_tables, *smbios_anchor; 1627 size_t smbios_tables_len, smbios_anchor_len; 1628 struct smbios_phys_mem_area mem_array; 1629 const char *product = "QEMU Virtual Machine"; 1630 1631 if (kvm_enabled()) { 1632 product = "KVM Virtual Machine"; 1633 } 1634 1635 smbios_set_defaults("QEMU", product, 1636 vmc->smbios_old_sys_ver ? "1.0" : mc->name, false, 1637 true, SMBIOS_ENTRY_POINT_TYPE_64); 1638 1639 /* build the array of physical mem area from base_memmap */ 1640 mem_array.address = vms->memmap[VIRT_MEM].base; 1641 mem_array.length = ms->ram_size; 1642 1643 smbios_get_tables(ms, &mem_array, 1, 1644 &smbios_tables, &smbios_tables_len, 1645 &smbios_anchor, &smbios_anchor_len, 1646 &error_fatal); 1647 1648 if (smbios_anchor) { 1649 fw_cfg_add_file(vms->fw_cfg, "etc/smbios/smbios-tables", 1650 smbios_tables, smbios_tables_len); 1651 fw_cfg_add_file(vms->fw_cfg, "etc/smbios/smbios-anchor", 1652 smbios_anchor, smbios_anchor_len); 1653 } 1654 } 1655 1656 static 1657 void virt_machine_done(Notifier *notifier, void *data) 1658 { 1659 VirtMachineState *vms = container_of(notifier, VirtMachineState, 1660 machine_done); 1661 MachineState *ms = MACHINE(vms); 1662 ARMCPU *cpu = ARM_CPU(first_cpu); 1663 struct arm_boot_info *info = &vms->bootinfo; 1664 AddressSpace *as = arm_boot_address_space(cpu, info); 1665 1666 /* 1667 * If the user provided a dtb, we assume the dynamic sysbus nodes 1668 * already are integrated there. This corresponds to a use case where 1669 * the dynamic sysbus nodes are complex and their generation is not yet 1670 * supported. In that case the user can take charge of the guest dt 1671 * while qemu takes charge of the qom stuff. 1672 */ 1673 if (info->dtb_filename == NULL) { 1674 platform_bus_add_all_fdt_nodes(ms->fdt, "/intc", 1675 vms->memmap[VIRT_PLATFORM_BUS].base, 1676 vms->memmap[VIRT_PLATFORM_BUS].size, 1677 vms->irqmap[VIRT_PLATFORM_BUS]); 1678 } 1679 if (arm_load_dtb(info->dtb_start, info, info->dtb_limit, as, ms) < 0) { 1680 exit(1); 1681 } 1682 1683 fw_cfg_add_extra_pci_roots(vms->bus, vms->fw_cfg); 1684 1685 virt_acpi_setup(vms); 1686 virt_build_smbios(vms); 1687 } 1688 1689 static uint64_t virt_cpu_mp_affinity(VirtMachineState *vms, int idx) 1690 { 1691 uint8_t clustersz = ARM_DEFAULT_CPUS_PER_CLUSTER; 1692 VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms); 1693 1694 if (!vmc->disallow_affinity_adjustment) { 1695 /* Adjust MPIDR like 64-bit KVM hosts, which incorporate the 1696 * GIC's target-list limitations. 32-bit KVM hosts currently 1697 * always create clusters of 4 CPUs, but that is expected to 1698 * change when they gain support for gicv3. When KVM is enabled 1699 * it will override the changes we make here, therefore our 1700 * purposes are to make TCG consistent (with 64-bit KVM hosts) 1701 * and to improve SGI efficiency. 1702 */ 1703 if (vms->gic_version == VIRT_GIC_VERSION_2) { 1704 clustersz = GIC_TARGETLIST_BITS; 1705 } else { 1706 clustersz = GICV3_TARGETLIST_BITS; 1707 } 1708 } 1709 return arm_cpu_mp_affinity(idx, clustersz); 1710 } 1711 1712 static inline bool *virt_get_high_memmap_enabled(VirtMachineState *vms, 1713 int index) 1714 { 1715 bool *enabled_array[] = { 1716 &vms->highmem_redists, 1717 &vms->highmem_ecam, 1718 &vms->highmem_mmio, 1719 }; 1720 1721 assert(ARRAY_SIZE(extended_memmap) - VIRT_LOWMEMMAP_LAST == 1722 ARRAY_SIZE(enabled_array)); 1723 assert(index - VIRT_LOWMEMMAP_LAST < ARRAY_SIZE(enabled_array)); 1724 1725 return enabled_array[index - VIRT_LOWMEMMAP_LAST]; 1726 } 1727 1728 static void virt_set_high_memmap(VirtMachineState *vms, 1729 hwaddr base, int pa_bits) 1730 { 1731 hwaddr region_base, region_size; 1732 bool *region_enabled, fits; 1733 int i; 1734 1735 for (i = VIRT_LOWMEMMAP_LAST; i < ARRAY_SIZE(extended_memmap); i++) { 1736 region_enabled = virt_get_high_memmap_enabled(vms, i); 1737 region_base = ROUND_UP(base, extended_memmap[i].size); 1738 region_size = extended_memmap[i].size; 1739 1740 vms->memmap[i].base = region_base; 1741 vms->memmap[i].size = region_size; 1742 1743 /* 1744 * Check each device to see if it fits in the PA space, 1745 * moving highest_gpa as we go. For compatibility, move 1746 * highest_gpa for disabled fitting devices as well, if 1747 * the compact layout has been disabled. 1748 * 1749 * For each device that doesn't fit, disable it. 1750 */ 1751 fits = (region_base + region_size) <= BIT_ULL(pa_bits); 1752 *region_enabled &= fits; 1753 if (vms->highmem_compact && !*region_enabled) { 1754 continue; 1755 } 1756 1757 base = region_base + region_size; 1758 if (fits) { 1759 vms->highest_gpa = base - 1; 1760 } 1761 } 1762 } 1763 1764 static void virt_set_memmap(VirtMachineState *vms, int pa_bits) 1765 { 1766 MachineState *ms = MACHINE(vms); 1767 hwaddr base, device_memory_base, device_memory_size, memtop; 1768 int i; 1769 1770 vms->memmap = extended_memmap; 1771 1772 for (i = 0; i < ARRAY_SIZE(base_memmap); i++) { 1773 vms->memmap[i] = base_memmap[i]; 1774 } 1775 1776 if (ms->ram_slots > ACPI_MAX_RAM_SLOTS) { 1777 error_report("unsupported number of memory slots: %"PRIu64, 1778 ms->ram_slots); 1779 exit(EXIT_FAILURE); 1780 } 1781 1782 /* 1783 * !highmem is exactly the same as limiting the PA space to 32bit, 1784 * irrespective of the underlying capabilities of the HW. 1785 */ 1786 if (!vms->highmem) { 1787 pa_bits = 32; 1788 } 1789 1790 /* 1791 * We compute the base of the high IO region depending on the 1792 * amount of initial and device memory. The device memory start/size 1793 * is aligned on 1GiB. We never put the high IO region below 256GiB 1794 * so that if maxram_size is < 255GiB we keep the legacy memory map. 1795 * The device region size assumes 1GiB page max alignment per slot. 1796 */ 1797 device_memory_base = 1798 ROUND_UP(vms->memmap[VIRT_MEM].base + ms->ram_size, GiB); 1799 device_memory_size = ms->maxram_size - ms->ram_size + ms->ram_slots * GiB; 1800 1801 /* Base address of the high IO region */ 1802 memtop = base = device_memory_base + ROUND_UP(device_memory_size, GiB); 1803 if (memtop > BIT_ULL(pa_bits)) { 1804 error_report("Addressing limited to %d bits, but memory exceeds it by %llu bytes\n", 1805 pa_bits, memtop - BIT_ULL(pa_bits)); 1806 exit(EXIT_FAILURE); 1807 } 1808 if (base < device_memory_base) { 1809 error_report("maxmem/slots too huge"); 1810 exit(EXIT_FAILURE); 1811 } 1812 if (base < vms->memmap[VIRT_MEM].base + LEGACY_RAMLIMIT_BYTES) { 1813 base = vms->memmap[VIRT_MEM].base + LEGACY_RAMLIMIT_BYTES; 1814 } 1815 1816 /* We know for sure that at least the memory fits in the PA space */ 1817 vms->highest_gpa = memtop - 1; 1818 1819 virt_set_high_memmap(vms, base, pa_bits); 1820 1821 if (device_memory_size > 0) { 1822 ms->device_memory = g_malloc0(sizeof(*ms->device_memory)); 1823 ms->device_memory->base = device_memory_base; 1824 memory_region_init(&ms->device_memory->mr, OBJECT(vms), 1825 "device-memory", device_memory_size); 1826 } 1827 } 1828 1829 static VirtGICType finalize_gic_version_do(const char *accel_name, 1830 VirtGICType gic_version, 1831 int gics_supported, 1832 unsigned int max_cpus) 1833 { 1834 /* Convert host/max/nosel to GIC version number */ 1835 switch (gic_version) { 1836 case VIRT_GIC_VERSION_HOST: 1837 if (!kvm_enabled()) { 1838 error_report("gic-version=host requires KVM"); 1839 exit(1); 1840 } 1841 1842 /* For KVM, gic-version=host means gic-version=max */ 1843 return finalize_gic_version_do(accel_name, VIRT_GIC_VERSION_MAX, 1844 gics_supported, max_cpus); 1845 case VIRT_GIC_VERSION_MAX: 1846 if (gics_supported & VIRT_GIC_VERSION_4_MASK) { 1847 gic_version = VIRT_GIC_VERSION_4; 1848 } else if (gics_supported & VIRT_GIC_VERSION_3_MASK) { 1849 gic_version = VIRT_GIC_VERSION_3; 1850 } else { 1851 gic_version = VIRT_GIC_VERSION_2; 1852 } 1853 break; 1854 case VIRT_GIC_VERSION_NOSEL: 1855 if ((gics_supported & VIRT_GIC_VERSION_2_MASK) && 1856 max_cpus <= GIC_NCPU) { 1857 gic_version = VIRT_GIC_VERSION_2; 1858 } else if (gics_supported & VIRT_GIC_VERSION_3_MASK) { 1859 /* 1860 * in case the host does not support v2 emulation or 1861 * the end-user requested more than 8 VCPUs we now default 1862 * to v3. In any case defaulting to v2 would be broken. 1863 */ 1864 gic_version = VIRT_GIC_VERSION_3; 1865 } else if (max_cpus > GIC_NCPU) { 1866 error_report("%s only supports GICv2 emulation but more than 8 " 1867 "vcpus are requested", accel_name); 1868 exit(1); 1869 } 1870 break; 1871 case VIRT_GIC_VERSION_2: 1872 case VIRT_GIC_VERSION_3: 1873 case VIRT_GIC_VERSION_4: 1874 break; 1875 } 1876 1877 /* Check chosen version is effectively supported */ 1878 switch (gic_version) { 1879 case VIRT_GIC_VERSION_2: 1880 if (!(gics_supported & VIRT_GIC_VERSION_2_MASK)) { 1881 error_report("%s does not support GICv2 emulation", accel_name); 1882 exit(1); 1883 } 1884 break; 1885 case VIRT_GIC_VERSION_3: 1886 if (!(gics_supported & VIRT_GIC_VERSION_3_MASK)) { 1887 error_report("%s does not support GICv3 emulation", accel_name); 1888 exit(1); 1889 } 1890 break; 1891 case VIRT_GIC_VERSION_4: 1892 if (!(gics_supported & VIRT_GIC_VERSION_4_MASK)) { 1893 error_report("%s does not support GICv4 emulation, is virtualization=on?", 1894 accel_name); 1895 exit(1); 1896 } 1897 break; 1898 default: 1899 error_report("logic error in finalize_gic_version"); 1900 exit(1); 1901 break; 1902 } 1903 1904 return gic_version; 1905 } 1906 1907 /* 1908 * finalize_gic_version - Determines the final gic_version 1909 * according to the gic-version property 1910 * 1911 * Default GIC type is v2 1912 */ 1913 static void finalize_gic_version(VirtMachineState *vms) 1914 { 1915 const char *accel_name = current_accel_name(); 1916 unsigned int max_cpus = MACHINE(vms)->smp.max_cpus; 1917 int gics_supported = 0; 1918 1919 /* Determine which GIC versions the current environment supports */ 1920 if (kvm_enabled() && kvm_irqchip_in_kernel()) { 1921 int probe_bitmap = kvm_arm_vgic_probe(); 1922 1923 if (!probe_bitmap) { 1924 error_report("Unable to determine GIC version supported by host"); 1925 exit(1); 1926 } 1927 1928 if (probe_bitmap & KVM_ARM_VGIC_V2) { 1929 gics_supported |= VIRT_GIC_VERSION_2_MASK; 1930 } 1931 if (probe_bitmap & KVM_ARM_VGIC_V3) { 1932 gics_supported |= VIRT_GIC_VERSION_3_MASK; 1933 } 1934 } else if (kvm_enabled() && !kvm_irqchip_in_kernel()) { 1935 /* KVM w/o kernel irqchip can only deal with GICv2 */ 1936 gics_supported |= VIRT_GIC_VERSION_2_MASK; 1937 accel_name = "KVM with kernel-irqchip=off"; 1938 } else if (tcg_enabled() || hvf_enabled() || qtest_enabled()) { 1939 gics_supported |= VIRT_GIC_VERSION_2_MASK; 1940 if (module_object_class_by_name("arm-gicv3")) { 1941 gics_supported |= VIRT_GIC_VERSION_3_MASK; 1942 if (vms->virt) { 1943 /* GICv4 only makes sense if CPU has EL2 */ 1944 gics_supported |= VIRT_GIC_VERSION_4_MASK; 1945 } 1946 } 1947 } else { 1948 error_report("Unsupported accelerator, can not determine GIC support"); 1949 exit(1); 1950 } 1951 1952 /* 1953 * Then convert helpers like host/max to concrete GIC versions and ensure 1954 * the desired version is supported 1955 */ 1956 vms->gic_version = finalize_gic_version_do(accel_name, vms->gic_version, 1957 gics_supported, max_cpus); 1958 } 1959 1960 /* 1961 * virt_cpu_post_init() must be called after the CPUs have 1962 * been realized and the GIC has been created. 1963 */ 1964 static void virt_cpu_post_init(VirtMachineState *vms, MemoryRegion *sysmem) 1965 { 1966 int max_cpus = MACHINE(vms)->smp.max_cpus; 1967 bool aarch64, pmu, steal_time; 1968 CPUState *cpu; 1969 1970 aarch64 = object_property_get_bool(OBJECT(first_cpu), "aarch64", NULL); 1971 pmu = object_property_get_bool(OBJECT(first_cpu), "pmu", NULL); 1972 steal_time = object_property_get_bool(OBJECT(first_cpu), 1973 "kvm-steal-time", NULL); 1974 1975 if (kvm_enabled()) { 1976 hwaddr pvtime_reg_base = vms->memmap[VIRT_PVTIME].base; 1977 hwaddr pvtime_reg_size = vms->memmap[VIRT_PVTIME].size; 1978 1979 if (steal_time) { 1980 MemoryRegion *pvtime = g_new(MemoryRegion, 1); 1981 hwaddr pvtime_size = max_cpus * PVTIME_SIZE_PER_CPU; 1982 1983 /* The memory region size must be a multiple of host page size. */ 1984 pvtime_size = REAL_HOST_PAGE_ALIGN(pvtime_size); 1985 1986 if (pvtime_size > pvtime_reg_size) { 1987 error_report("pvtime requires a %" HWADDR_PRId 1988 " byte memory region for %d CPUs," 1989 " but only %" HWADDR_PRId " has been reserved", 1990 pvtime_size, max_cpus, pvtime_reg_size); 1991 exit(1); 1992 } 1993 1994 memory_region_init_ram(pvtime, NULL, "pvtime", pvtime_size, NULL); 1995 memory_region_add_subregion(sysmem, pvtime_reg_base, pvtime); 1996 } 1997 1998 CPU_FOREACH(cpu) { 1999 if (pmu) { 2000 assert(arm_feature(&ARM_CPU(cpu)->env, ARM_FEATURE_PMU)); 2001 if (kvm_irqchip_in_kernel()) { 2002 kvm_arm_pmu_set_irq(cpu, PPI(VIRTUAL_PMU_IRQ)); 2003 } 2004 kvm_arm_pmu_init(cpu); 2005 } 2006 if (steal_time) { 2007 kvm_arm_pvtime_init(cpu, pvtime_reg_base + 2008 cpu->cpu_index * PVTIME_SIZE_PER_CPU); 2009 } 2010 } 2011 } else { 2012 if (aarch64 && vms->highmem) { 2013 int requested_pa_size = 64 - clz64(vms->highest_gpa); 2014 int pamax = arm_pamax(ARM_CPU(first_cpu)); 2015 2016 if (pamax < requested_pa_size) { 2017 error_report("VCPU supports less PA bits (%d) than " 2018 "requested by the memory map (%d)", 2019 pamax, requested_pa_size); 2020 exit(1); 2021 } 2022 } 2023 } 2024 } 2025 2026 static void machvirt_init(MachineState *machine) 2027 { 2028 VirtMachineState *vms = VIRT_MACHINE(machine); 2029 VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(machine); 2030 MachineClass *mc = MACHINE_GET_CLASS(machine); 2031 const CPUArchIdList *possible_cpus; 2032 MemoryRegion *sysmem = get_system_memory(); 2033 MemoryRegion *secure_sysmem = NULL; 2034 MemoryRegion *tag_sysmem = NULL; 2035 MemoryRegion *secure_tag_sysmem = NULL; 2036 int n, virt_max_cpus; 2037 bool firmware_loaded; 2038 bool aarch64 = true; 2039 bool has_ged = !vmc->no_ged; 2040 unsigned int smp_cpus = machine->smp.cpus; 2041 unsigned int max_cpus = machine->smp.max_cpus; 2042 2043 if (!cpu_type_valid(machine->cpu_type)) { 2044 error_report("mach-virt: CPU type %s not supported", machine->cpu_type); 2045 exit(1); 2046 } 2047 2048 possible_cpus = mc->possible_cpu_arch_ids(machine); 2049 2050 /* 2051 * In accelerated mode, the memory map is computed earlier in kvm_type() 2052 * to create a VM with the right number of IPA bits. 2053 */ 2054 if (!vms->memmap) { 2055 Object *cpuobj; 2056 ARMCPU *armcpu; 2057 int pa_bits; 2058 2059 /* 2060 * Instantiate a temporary CPU object to find out about what 2061 * we are about to deal with. Once this is done, get rid of 2062 * the object. 2063 */ 2064 cpuobj = object_new(possible_cpus->cpus[0].type); 2065 armcpu = ARM_CPU(cpuobj); 2066 2067 pa_bits = arm_pamax(armcpu); 2068 2069 object_unref(cpuobj); 2070 2071 virt_set_memmap(vms, pa_bits); 2072 } 2073 2074 /* We can probe only here because during property set 2075 * KVM is not available yet 2076 */ 2077 finalize_gic_version(vms); 2078 2079 if (vms->secure) { 2080 /* 2081 * The Secure view of the world is the same as the NonSecure, 2082 * but with a few extra devices. Create it as a container region 2083 * containing the system memory at low priority; any secure-only 2084 * devices go in at higher priority and take precedence. 2085 */ 2086 secure_sysmem = g_new(MemoryRegion, 1); 2087 memory_region_init(secure_sysmem, OBJECT(machine), "secure-memory", 2088 UINT64_MAX); 2089 memory_region_add_subregion_overlap(secure_sysmem, 0, sysmem, -1); 2090 } 2091 2092 firmware_loaded = virt_firmware_init(vms, sysmem, 2093 secure_sysmem ?: sysmem); 2094 2095 /* If we have an EL3 boot ROM then the assumption is that it will 2096 * implement PSCI itself, so disable QEMU's internal implementation 2097 * so it doesn't get in the way. Instead of starting secondary 2098 * CPUs in PSCI powerdown state we will start them all running and 2099 * let the boot ROM sort them out. 2100 * The usual case is that we do use QEMU's PSCI implementation; 2101 * if the guest has EL2 then we will use SMC as the conduit, 2102 * and otherwise we will use HVC (for backwards compatibility and 2103 * because if we're using KVM then we must use HVC). 2104 */ 2105 if (vms->secure && firmware_loaded) { 2106 vms->psci_conduit = QEMU_PSCI_CONDUIT_DISABLED; 2107 } else if (vms->virt) { 2108 vms->psci_conduit = QEMU_PSCI_CONDUIT_SMC; 2109 } else { 2110 vms->psci_conduit = QEMU_PSCI_CONDUIT_HVC; 2111 } 2112 2113 /* 2114 * The maximum number of CPUs depends on the GIC version, or on how 2115 * many redistributors we can fit into the memory map (which in turn 2116 * depends on whether this is a GICv3 or v4). 2117 */ 2118 if (vms->gic_version == VIRT_GIC_VERSION_2) { 2119 virt_max_cpus = GIC_NCPU; 2120 } else { 2121 virt_max_cpus = virt_redist_capacity(vms, VIRT_GIC_REDIST); 2122 if (vms->highmem_redists) { 2123 virt_max_cpus += virt_redist_capacity(vms, VIRT_HIGH_GIC_REDIST2); 2124 } 2125 } 2126 2127 if (max_cpus > virt_max_cpus) { 2128 error_report("Number of SMP CPUs requested (%d) exceeds max CPUs " 2129 "supported by machine 'mach-virt' (%d)", 2130 max_cpus, virt_max_cpus); 2131 if (vms->gic_version != VIRT_GIC_VERSION_2 && !vms->highmem_redists) { 2132 error_printf("Try 'highmem-redists=on' for more CPUs\n"); 2133 } 2134 2135 exit(1); 2136 } 2137 2138 if (vms->secure && (kvm_enabled() || hvf_enabled())) { 2139 error_report("mach-virt: %s does not support providing " 2140 "Security extensions (TrustZone) to the guest CPU", 2141 current_accel_name()); 2142 exit(1); 2143 } 2144 2145 if (vms->virt && (kvm_enabled() || hvf_enabled())) { 2146 error_report("mach-virt: %s does not support providing " 2147 "Virtualization extensions to the guest CPU", 2148 current_accel_name()); 2149 exit(1); 2150 } 2151 2152 if (vms->mte && (kvm_enabled() || hvf_enabled())) { 2153 error_report("mach-virt: %s does not support providing " 2154 "MTE to the guest CPU", 2155 current_accel_name()); 2156 exit(1); 2157 } 2158 2159 create_fdt(vms); 2160 2161 assert(possible_cpus->len == max_cpus); 2162 for (n = 0; n < possible_cpus->len; n++) { 2163 Object *cpuobj; 2164 CPUState *cs; 2165 2166 if (n >= smp_cpus) { 2167 break; 2168 } 2169 2170 cpuobj = object_new(possible_cpus->cpus[n].type); 2171 object_property_set_int(cpuobj, "mp-affinity", 2172 possible_cpus->cpus[n].arch_id, NULL); 2173 2174 cs = CPU(cpuobj); 2175 cs->cpu_index = n; 2176 2177 numa_cpu_pre_plug(&possible_cpus->cpus[cs->cpu_index], DEVICE(cpuobj), 2178 &error_fatal); 2179 2180 aarch64 &= object_property_get_bool(cpuobj, "aarch64", NULL); 2181 2182 if (!vms->secure) { 2183 object_property_set_bool(cpuobj, "has_el3", false, NULL); 2184 } 2185 2186 if (!vms->virt && object_property_find(cpuobj, "has_el2")) { 2187 object_property_set_bool(cpuobj, "has_el2", false, NULL); 2188 } 2189 2190 if (vmc->kvm_no_adjvtime && 2191 object_property_find(cpuobj, "kvm-no-adjvtime")) { 2192 object_property_set_bool(cpuobj, "kvm-no-adjvtime", true, NULL); 2193 } 2194 2195 if (vmc->no_kvm_steal_time && 2196 object_property_find(cpuobj, "kvm-steal-time")) { 2197 object_property_set_bool(cpuobj, "kvm-steal-time", false, NULL); 2198 } 2199 2200 if (vmc->no_pmu && object_property_find(cpuobj, "pmu")) { 2201 object_property_set_bool(cpuobj, "pmu", false, NULL); 2202 } 2203 2204 if (vmc->no_tcg_lpa2 && object_property_find(cpuobj, "lpa2")) { 2205 object_property_set_bool(cpuobj, "lpa2", false, NULL); 2206 } 2207 2208 if (object_property_find(cpuobj, "reset-cbar")) { 2209 object_property_set_int(cpuobj, "reset-cbar", 2210 vms->memmap[VIRT_CPUPERIPHS].base, 2211 &error_abort); 2212 } 2213 2214 object_property_set_link(cpuobj, "memory", OBJECT(sysmem), 2215 &error_abort); 2216 if (vms->secure) { 2217 object_property_set_link(cpuobj, "secure-memory", 2218 OBJECT(secure_sysmem), &error_abort); 2219 } 2220 2221 if (vms->mte) { 2222 /* Create the memory region only once, but link to all cpus. */ 2223 if (!tag_sysmem) { 2224 /* 2225 * The property exists only if MemTag is supported. 2226 * If it is, we must allocate the ram to back that up. 2227 */ 2228 if (!object_property_find(cpuobj, "tag-memory")) { 2229 error_report("MTE requested, but not supported " 2230 "by the guest CPU"); 2231 exit(1); 2232 } 2233 2234 tag_sysmem = g_new(MemoryRegion, 1); 2235 memory_region_init(tag_sysmem, OBJECT(machine), 2236 "tag-memory", UINT64_MAX / 32); 2237 2238 if (vms->secure) { 2239 secure_tag_sysmem = g_new(MemoryRegion, 1); 2240 memory_region_init(secure_tag_sysmem, OBJECT(machine), 2241 "secure-tag-memory", UINT64_MAX / 32); 2242 2243 /* As with ram, secure-tag takes precedence over tag. */ 2244 memory_region_add_subregion_overlap(secure_tag_sysmem, 0, 2245 tag_sysmem, -1); 2246 } 2247 } 2248 2249 object_property_set_link(cpuobj, "tag-memory", OBJECT(tag_sysmem), 2250 &error_abort); 2251 if (vms->secure) { 2252 object_property_set_link(cpuobj, "secure-tag-memory", 2253 OBJECT(secure_tag_sysmem), 2254 &error_abort); 2255 } 2256 } 2257 2258 qdev_realize(DEVICE(cpuobj), NULL, &error_fatal); 2259 object_unref(cpuobj); 2260 } 2261 fdt_add_timer_nodes(vms); 2262 fdt_add_cpu_nodes(vms); 2263 2264 memory_region_add_subregion(sysmem, vms->memmap[VIRT_MEM].base, 2265 machine->ram); 2266 if (machine->device_memory) { 2267 memory_region_add_subregion(sysmem, machine->device_memory->base, 2268 &machine->device_memory->mr); 2269 } 2270 2271 virt_flash_fdt(vms, sysmem, secure_sysmem ?: sysmem); 2272 2273 create_gic(vms, sysmem); 2274 2275 virt_cpu_post_init(vms, sysmem); 2276 2277 fdt_add_pmu_nodes(vms); 2278 2279 create_uart(vms, VIRT_UART, sysmem, serial_hd(0)); 2280 2281 if (vms->secure) { 2282 create_secure_ram(vms, secure_sysmem, secure_tag_sysmem); 2283 create_uart(vms, VIRT_SECURE_UART, secure_sysmem, serial_hd(1)); 2284 } 2285 2286 if (tag_sysmem) { 2287 create_tag_ram(tag_sysmem, vms->memmap[VIRT_MEM].base, 2288 machine->ram_size, "mach-virt.tag"); 2289 } 2290 2291 vms->highmem_ecam &= (!firmware_loaded || aarch64); 2292 2293 create_rtc(vms); 2294 2295 create_pcie(vms); 2296 2297 if (has_ged && aarch64 && firmware_loaded && virt_is_acpi_enabled(vms)) { 2298 vms->acpi_dev = create_acpi_ged(vms); 2299 } else { 2300 create_gpio_devices(vms, VIRT_GPIO, sysmem); 2301 } 2302 2303 if (vms->secure && !vmc->no_secure_gpio) { 2304 create_gpio_devices(vms, VIRT_SECURE_GPIO, secure_sysmem); 2305 } 2306 2307 /* connect powerdown request */ 2308 vms->powerdown_notifier.notify = virt_powerdown_req; 2309 qemu_register_powerdown_notifier(&vms->powerdown_notifier); 2310 2311 /* Create mmio transports, so the user can create virtio backends 2312 * (which will be automatically plugged in to the transports). If 2313 * no backend is created the transport will just sit harmlessly idle. 2314 */ 2315 create_virtio_devices(vms); 2316 2317 vms->fw_cfg = create_fw_cfg(vms, &address_space_memory); 2318 rom_set_fw(vms->fw_cfg); 2319 2320 create_platform_bus(vms); 2321 2322 if (machine->nvdimms_state->is_enabled) { 2323 const struct AcpiGenericAddress arm_virt_nvdimm_acpi_dsmio = { 2324 .space_id = AML_AS_SYSTEM_MEMORY, 2325 .address = vms->memmap[VIRT_NVDIMM_ACPI].base, 2326 .bit_width = NVDIMM_ACPI_IO_LEN << 3 2327 }; 2328 2329 nvdimm_init_acpi_state(machine->nvdimms_state, sysmem, 2330 arm_virt_nvdimm_acpi_dsmio, 2331 vms->fw_cfg, OBJECT(vms)); 2332 } 2333 2334 vms->bootinfo.ram_size = machine->ram_size; 2335 vms->bootinfo.board_id = -1; 2336 vms->bootinfo.loader_start = vms->memmap[VIRT_MEM].base; 2337 vms->bootinfo.get_dtb = machvirt_dtb; 2338 vms->bootinfo.skip_dtb_autoload = true; 2339 vms->bootinfo.firmware_loaded = firmware_loaded; 2340 vms->bootinfo.psci_conduit = vms->psci_conduit; 2341 arm_load_kernel(ARM_CPU(first_cpu), machine, &vms->bootinfo); 2342 2343 vms->machine_done.notify = virt_machine_done; 2344 qemu_add_machine_init_done_notifier(&vms->machine_done); 2345 } 2346 2347 static bool virt_get_secure(Object *obj, Error **errp) 2348 { 2349 VirtMachineState *vms = VIRT_MACHINE(obj); 2350 2351 return vms->secure; 2352 } 2353 2354 static void virt_set_secure(Object *obj, bool value, Error **errp) 2355 { 2356 VirtMachineState *vms = VIRT_MACHINE(obj); 2357 2358 vms->secure = value; 2359 } 2360 2361 static bool virt_get_virt(Object *obj, Error **errp) 2362 { 2363 VirtMachineState *vms = VIRT_MACHINE(obj); 2364 2365 return vms->virt; 2366 } 2367 2368 static void virt_set_virt(Object *obj, bool value, Error **errp) 2369 { 2370 VirtMachineState *vms = VIRT_MACHINE(obj); 2371 2372 vms->virt = value; 2373 } 2374 2375 static bool virt_get_highmem(Object *obj, Error **errp) 2376 { 2377 VirtMachineState *vms = VIRT_MACHINE(obj); 2378 2379 return vms->highmem; 2380 } 2381 2382 static void virt_set_highmem(Object *obj, bool value, Error **errp) 2383 { 2384 VirtMachineState *vms = VIRT_MACHINE(obj); 2385 2386 vms->highmem = value; 2387 } 2388 2389 static bool virt_get_compact_highmem(Object *obj, Error **errp) 2390 { 2391 VirtMachineState *vms = VIRT_MACHINE(obj); 2392 2393 return vms->highmem_compact; 2394 } 2395 2396 static void virt_set_compact_highmem(Object *obj, bool value, Error **errp) 2397 { 2398 VirtMachineState *vms = VIRT_MACHINE(obj); 2399 2400 vms->highmem_compact = value; 2401 } 2402 2403 static bool virt_get_highmem_redists(Object *obj, Error **errp) 2404 { 2405 VirtMachineState *vms = VIRT_MACHINE(obj); 2406 2407 return vms->highmem_redists; 2408 } 2409 2410 static void virt_set_highmem_redists(Object *obj, bool value, Error **errp) 2411 { 2412 VirtMachineState *vms = VIRT_MACHINE(obj); 2413 2414 vms->highmem_redists = value; 2415 } 2416 2417 static bool virt_get_highmem_ecam(Object *obj, Error **errp) 2418 { 2419 VirtMachineState *vms = VIRT_MACHINE(obj); 2420 2421 return vms->highmem_ecam; 2422 } 2423 2424 static void virt_set_highmem_ecam(Object *obj, bool value, Error **errp) 2425 { 2426 VirtMachineState *vms = VIRT_MACHINE(obj); 2427 2428 vms->highmem_ecam = value; 2429 } 2430 2431 static bool virt_get_highmem_mmio(Object *obj, Error **errp) 2432 { 2433 VirtMachineState *vms = VIRT_MACHINE(obj); 2434 2435 return vms->highmem_mmio; 2436 } 2437 2438 static void virt_set_highmem_mmio(Object *obj, bool value, Error **errp) 2439 { 2440 VirtMachineState *vms = VIRT_MACHINE(obj); 2441 2442 vms->highmem_mmio = value; 2443 } 2444 2445 2446 static bool virt_get_its(Object *obj, Error **errp) 2447 { 2448 VirtMachineState *vms = VIRT_MACHINE(obj); 2449 2450 return vms->its; 2451 } 2452 2453 static void virt_set_its(Object *obj, bool value, Error **errp) 2454 { 2455 VirtMachineState *vms = VIRT_MACHINE(obj); 2456 2457 vms->its = value; 2458 } 2459 2460 static bool virt_get_dtb_randomness(Object *obj, Error **errp) 2461 { 2462 VirtMachineState *vms = VIRT_MACHINE(obj); 2463 2464 return vms->dtb_randomness; 2465 } 2466 2467 static void virt_set_dtb_randomness(Object *obj, bool value, Error **errp) 2468 { 2469 VirtMachineState *vms = VIRT_MACHINE(obj); 2470 2471 vms->dtb_randomness = value; 2472 } 2473 2474 static char *virt_get_oem_id(Object *obj, Error **errp) 2475 { 2476 VirtMachineState *vms = VIRT_MACHINE(obj); 2477 2478 return g_strdup(vms->oem_id); 2479 } 2480 2481 static void virt_set_oem_id(Object *obj, const char *value, Error **errp) 2482 { 2483 VirtMachineState *vms = VIRT_MACHINE(obj); 2484 size_t len = strlen(value); 2485 2486 if (len > 6) { 2487 error_setg(errp, 2488 "User specified oem-id value is bigger than 6 bytes in size"); 2489 return; 2490 } 2491 2492 strncpy(vms->oem_id, value, 6); 2493 } 2494 2495 static char *virt_get_oem_table_id(Object *obj, Error **errp) 2496 { 2497 VirtMachineState *vms = VIRT_MACHINE(obj); 2498 2499 return g_strdup(vms->oem_table_id); 2500 } 2501 2502 static void virt_set_oem_table_id(Object *obj, const char *value, 2503 Error **errp) 2504 { 2505 VirtMachineState *vms = VIRT_MACHINE(obj); 2506 size_t len = strlen(value); 2507 2508 if (len > 8) { 2509 error_setg(errp, 2510 "User specified oem-table-id value is bigger than 8 bytes in size"); 2511 return; 2512 } 2513 strncpy(vms->oem_table_id, value, 8); 2514 } 2515 2516 2517 bool virt_is_acpi_enabled(VirtMachineState *vms) 2518 { 2519 if (vms->acpi == ON_OFF_AUTO_OFF) { 2520 return false; 2521 } 2522 return true; 2523 } 2524 2525 static void virt_get_acpi(Object *obj, Visitor *v, const char *name, 2526 void *opaque, Error **errp) 2527 { 2528 VirtMachineState *vms = VIRT_MACHINE(obj); 2529 OnOffAuto acpi = vms->acpi; 2530 2531 visit_type_OnOffAuto(v, name, &acpi, errp); 2532 } 2533 2534 static void virt_set_acpi(Object *obj, Visitor *v, const char *name, 2535 void *opaque, Error **errp) 2536 { 2537 VirtMachineState *vms = VIRT_MACHINE(obj); 2538 2539 visit_type_OnOffAuto(v, name, &vms->acpi, errp); 2540 } 2541 2542 static bool virt_get_ras(Object *obj, Error **errp) 2543 { 2544 VirtMachineState *vms = VIRT_MACHINE(obj); 2545 2546 return vms->ras; 2547 } 2548 2549 static void virt_set_ras(Object *obj, bool value, Error **errp) 2550 { 2551 VirtMachineState *vms = VIRT_MACHINE(obj); 2552 2553 vms->ras = value; 2554 } 2555 2556 static bool virt_get_mte(Object *obj, Error **errp) 2557 { 2558 VirtMachineState *vms = VIRT_MACHINE(obj); 2559 2560 return vms->mte; 2561 } 2562 2563 static void virt_set_mte(Object *obj, bool value, Error **errp) 2564 { 2565 VirtMachineState *vms = VIRT_MACHINE(obj); 2566 2567 vms->mte = value; 2568 } 2569 2570 static char *virt_get_gic_version(Object *obj, Error **errp) 2571 { 2572 VirtMachineState *vms = VIRT_MACHINE(obj); 2573 const char *val; 2574 2575 switch (vms->gic_version) { 2576 case VIRT_GIC_VERSION_4: 2577 val = "4"; 2578 break; 2579 case VIRT_GIC_VERSION_3: 2580 val = "3"; 2581 break; 2582 default: 2583 val = "2"; 2584 break; 2585 } 2586 return g_strdup(val); 2587 } 2588 2589 static void virt_set_gic_version(Object *obj, const char *value, Error **errp) 2590 { 2591 VirtMachineState *vms = VIRT_MACHINE(obj); 2592 2593 if (!strcmp(value, "4")) { 2594 vms->gic_version = VIRT_GIC_VERSION_4; 2595 } else if (!strcmp(value, "3")) { 2596 vms->gic_version = VIRT_GIC_VERSION_3; 2597 } else if (!strcmp(value, "2")) { 2598 vms->gic_version = VIRT_GIC_VERSION_2; 2599 } else if (!strcmp(value, "host")) { 2600 vms->gic_version = VIRT_GIC_VERSION_HOST; /* Will probe later */ 2601 } else if (!strcmp(value, "max")) { 2602 vms->gic_version = VIRT_GIC_VERSION_MAX; /* Will probe later */ 2603 } else { 2604 error_setg(errp, "Invalid gic-version value"); 2605 error_append_hint(errp, "Valid values are 3, 2, host, max.\n"); 2606 } 2607 } 2608 2609 static char *virt_get_iommu(Object *obj, Error **errp) 2610 { 2611 VirtMachineState *vms = VIRT_MACHINE(obj); 2612 2613 switch (vms->iommu) { 2614 case VIRT_IOMMU_NONE: 2615 return g_strdup("none"); 2616 case VIRT_IOMMU_SMMUV3: 2617 return g_strdup("smmuv3"); 2618 default: 2619 g_assert_not_reached(); 2620 } 2621 } 2622 2623 static void virt_set_iommu(Object *obj, const char *value, Error **errp) 2624 { 2625 VirtMachineState *vms = VIRT_MACHINE(obj); 2626 2627 if (!strcmp(value, "smmuv3")) { 2628 vms->iommu = VIRT_IOMMU_SMMUV3; 2629 } else if (!strcmp(value, "none")) { 2630 vms->iommu = VIRT_IOMMU_NONE; 2631 } else { 2632 error_setg(errp, "Invalid iommu value"); 2633 error_append_hint(errp, "Valid values are none, smmuv3.\n"); 2634 } 2635 } 2636 2637 static bool virt_get_default_bus_bypass_iommu(Object *obj, Error **errp) 2638 { 2639 VirtMachineState *vms = VIRT_MACHINE(obj); 2640 2641 return vms->default_bus_bypass_iommu; 2642 } 2643 2644 static void virt_set_default_bus_bypass_iommu(Object *obj, bool value, 2645 Error **errp) 2646 { 2647 VirtMachineState *vms = VIRT_MACHINE(obj); 2648 2649 vms->default_bus_bypass_iommu = value; 2650 } 2651 2652 static CpuInstanceProperties 2653 virt_cpu_index_to_props(MachineState *ms, unsigned cpu_index) 2654 { 2655 MachineClass *mc = MACHINE_GET_CLASS(ms); 2656 const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(ms); 2657 2658 assert(cpu_index < possible_cpus->len); 2659 return possible_cpus->cpus[cpu_index].props; 2660 } 2661 2662 static int64_t virt_get_default_cpu_node_id(const MachineState *ms, int idx) 2663 { 2664 int64_t socket_id = ms->possible_cpus->cpus[idx].props.socket_id; 2665 2666 return socket_id % ms->numa_state->num_nodes; 2667 } 2668 2669 static const CPUArchIdList *virt_possible_cpu_arch_ids(MachineState *ms) 2670 { 2671 int n; 2672 unsigned int max_cpus = ms->smp.max_cpus; 2673 VirtMachineState *vms = VIRT_MACHINE(ms); 2674 MachineClass *mc = MACHINE_GET_CLASS(vms); 2675 2676 if (ms->possible_cpus) { 2677 assert(ms->possible_cpus->len == max_cpus); 2678 return ms->possible_cpus; 2679 } 2680 2681 ms->possible_cpus = g_malloc0(sizeof(CPUArchIdList) + 2682 sizeof(CPUArchId) * max_cpus); 2683 ms->possible_cpus->len = max_cpus; 2684 for (n = 0; n < ms->possible_cpus->len; n++) { 2685 ms->possible_cpus->cpus[n].type = ms->cpu_type; 2686 ms->possible_cpus->cpus[n].arch_id = 2687 virt_cpu_mp_affinity(vms, n); 2688 2689 assert(!mc->smp_props.dies_supported); 2690 ms->possible_cpus->cpus[n].props.has_socket_id = true; 2691 ms->possible_cpus->cpus[n].props.socket_id = 2692 n / (ms->smp.clusters * ms->smp.cores * ms->smp.threads); 2693 ms->possible_cpus->cpus[n].props.has_cluster_id = true; 2694 ms->possible_cpus->cpus[n].props.cluster_id = 2695 (n / (ms->smp.cores * ms->smp.threads)) % ms->smp.clusters; 2696 ms->possible_cpus->cpus[n].props.has_core_id = true; 2697 ms->possible_cpus->cpus[n].props.core_id = 2698 (n / ms->smp.threads) % ms->smp.cores; 2699 ms->possible_cpus->cpus[n].props.has_thread_id = true; 2700 ms->possible_cpus->cpus[n].props.thread_id = 2701 n % ms->smp.threads; 2702 } 2703 return ms->possible_cpus; 2704 } 2705 2706 static void virt_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev, 2707 Error **errp) 2708 { 2709 VirtMachineState *vms = VIRT_MACHINE(hotplug_dev); 2710 const MachineState *ms = MACHINE(hotplug_dev); 2711 const bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM); 2712 2713 if (!vms->acpi_dev) { 2714 error_setg(errp, 2715 "memory hotplug is not enabled: missing acpi-ged device"); 2716 return; 2717 } 2718 2719 if (vms->mte) { 2720 error_setg(errp, "memory hotplug is not enabled: MTE is enabled"); 2721 return; 2722 } 2723 2724 if (is_nvdimm && !ms->nvdimms_state->is_enabled) { 2725 error_setg(errp, "nvdimm is not enabled: add 'nvdimm=on' to '-M'"); 2726 return; 2727 } 2728 2729 pc_dimm_pre_plug(PC_DIMM(dev), MACHINE(hotplug_dev), NULL, errp); 2730 } 2731 2732 static void virt_memory_plug(HotplugHandler *hotplug_dev, 2733 DeviceState *dev, Error **errp) 2734 { 2735 VirtMachineState *vms = VIRT_MACHINE(hotplug_dev); 2736 MachineState *ms = MACHINE(hotplug_dev); 2737 bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM); 2738 2739 pc_dimm_plug(PC_DIMM(dev), MACHINE(vms)); 2740 2741 if (is_nvdimm) { 2742 nvdimm_plug(ms->nvdimms_state); 2743 } 2744 2745 hotplug_handler_plug(HOTPLUG_HANDLER(vms->acpi_dev), 2746 dev, &error_abort); 2747 } 2748 2749 static void virt_virtio_md_pci_pre_plug(HotplugHandler *hotplug_dev, 2750 DeviceState *dev, Error **errp) 2751 { 2752 HotplugHandler *hotplug_dev2 = qdev_get_bus_hotplug_handler(dev); 2753 Error *local_err = NULL; 2754 2755 if (!hotplug_dev2 && dev->hotplugged) { 2756 /* 2757 * Without a bus hotplug handler, we cannot control the plug/unplug 2758 * order. We should never reach this point when hotplugging on ARM. 2759 * However, it's nice to add a safety net, similar to what we have 2760 * on x86. 2761 */ 2762 error_setg(errp, "hotplug of virtio based memory devices not supported" 2763 " on this bus."); 2764 return; 2765 } 2766 /* 2767 * First, see if we can plug this memory device at all. If that 2768 * succeeds, branch of to the actual hotplug handler. 2769 */ 2770 memory_device_pre_plug(MEMORY_DEVICE(dev), MACHINE(hotplug_dev), NULL, 2771 &local_err); 2772 if (!local_err && hotplug_dev2) { 2773 hotplug_handler_pre_plug(hotplug_dev2, dev, &local_err); 2774 } 2775 error_propagate(errp, local_err); 2776 } 2777 2778 static void virt_virtio_md_pci_plug(HotplugHandler *hotplug_dev, 2779 DeviceState *dev, Error **errp) 2780 { 2781 HotplugHandler *hotplug_dev2 = qdev_get_bus_hotplug_handler(dev); 2782 Error *local_err = NULL; 2783 2784 /* 2785 * Plug the memory device first and then branch off to the actual 2786 * hotplug handler. If that one fails, we can easily undo the memory 2787 * device bits. 2788 */ 2789 memory_device_plug(MEMORY_DEVICE(dev), MACHINE(hotplug_dev)); 2790 if (hotplug_dev2) { 2791 hotplug_handler_plug(hotplug_dev2, dev, &local_err); 2792 if (local_err) { 2793 memory_device_unplug(MEMORY_DEVICE(dev), MACHINE(hotplug_dev)); 2794 } 2795 } 2796 error_propagate(errp, local_err); 2797 } 2798 2799 static void virt_virtio_md_pci_unplug_request(HotplugHandler *hotplug_dev, 2800 DeviceState *dev, Error **errp) 2801 { 2802 /* We don't support hot unplug of virtio based memory devices */ 2803 error_setg(errp, "virtio based memory devices cannot be unplugged."); 2804 } 2805 2806 2807 static void virt_machine_device_pre_plug_cb(HotplugHandler *hotplug_dev, 2808 DeviceState *dev, Error **errp) 2809 { 2810 VirtMachineState *vms = VIRT_MACHINE(hotplug_dev); 2811 2812 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) { 2813 virt_memory_pre_plug(hotplug_dev, dev, errp); 2814 } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MEM_PCI)) { 2815 virt_virtio_md_pci_pre_plug(hotplug_dev, dev, errp); 2816 } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_IOMMU_PCI)) { 2817 hwaddr db_start = 0, db_end = 0; 2818 char *resv_prop_str; 2819 2820 if (vms->iommu != VIRT_IOMMU_NONE) { 2821 error_setg(errp, "virt machine does not support multiple IOMMUs"); 2822 return; 2823 } 2824 2825 switch (vms->msi_controller) { 2826 case VIRT_MSI_CTRL_NONE: 2827 return; 2828 case VIRT_MSI_CTRL_ITS: 2829 /* GITS_TRANSLATER page */ 2830 db_start = base_memmap[VIRT_GIC_ITS].base + 0x10000; 2831 db_end = base_memmap[VIRT_GIC_ITS].base + 2832 base_memmap[VIRT_GIC_ITS].size - 1; 2833 break; 2834 case VIRT_MSI_CTRL_GICV2M: 2835 /* MSI_SETSPI_NS page */ 2836 db_start = base_memmap[VIRT_GIC_V2M].base; 2837 db_end = db_start + base_memmap[VIRT_GIC_V2M].size - 1; 2838 break; 2839 } 2840 resv_prop_str = g_strdup_printf("0x%"PRIx64":0x%"PRIx64":%u", 2841 db_start, db_end, 2842 VIRTIO_IOMMU_RESV_MEM_T_MSI); 2843 2844 object_property_set_uint(OBJECT(dev), "len-reserved-regions", 1, errp); 2845 object_property_set_str(OBJECT(dev), "reserved-regions[0]", 2846 resv_prop_str, errp); 2847 g_free(resv_prop_str); 2848 } 2849 } 2850 2851 static void virt_machine_device_plug_cb(HotplugHandler *hotplug_dev, 2852 DeviceState *dev, Error **errp) 2853 { 2854 VirtMachineState *vms = VIRT_MACHINE(hotplug_dev); 2855 2856 if (vms->platform_bus_dev) { 2857 MachineClass *mc = MACHINE_GET_CLASS(vms); 2858 2859 if (device_is_dynamic_sysbus(mc, dev)) { 2860 platform_bus_link_device(PLATFORM_BUS_DEVICE(vms->platform_bus_dev), 2861 SYS_BUS_DEVICE(dev)); 2862 } 2863 } 2864 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) { 2865 virt_memory_plug(hotplug_dev, dev, errp); 2866 } 2867 2868 if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MEM_PCI)) { 2869 virt_virtio_md_pci_plug(hotplug_dev, dev, errp); 2870 } 2871 2872 if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_IOMMU_PCI)) { 2873 PCIDevice *pdev = PCI_DEVICE(dev); 2874 2875 vms->iommu = VIRT_IOMMU_VIRTIO; 2876 vms->virtio_iommu_bdf = pci_get_bdf(pdev); 2877 create_virtio_iommu_dt_bindings(vms); 2878 } 2879 } 2880 2881 static void virt_dimm_unplug_request(HotplugHandler *hotplug_dev, 2882 DeviceState *dev, Error **errp) 2883 { 2884 VirtMachineState *vms = VIRT_MACHINE(hotplug_dev); 2885 2886 if (!vms->acpi_dev) { 2887 error_setg(errp, 2888 "memory hotplug is not enabled: missing acpi-ged device"); 2889 return; 2890 } 2891 2892 if (object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM)) { 2893 error_setg(errp, "nvdimm device hot unplug is not supported yet."); 2894 return; 2895 } 2896 2897 hotplug_handler_unplug_request(HOTPLUG_HANDLER(vms->acpi_dev), dev, 2898 errp); 2899 } 2900 2901 static void virt_dimm_unplug(HotplugHandler *hotplug_dev, 2902 DeviceState *dev, Error **errp) 2903 { 2904 VirtMachineState *vms = VIRT_MACHINE(hotplug_dev); 2905 Error *local_err = NULL; 2906 2907 hotplug_handler_unplug(HOTPLUG_HANDLER(vms->acpi_dev), dev, &local_err); 2908 if (local_err) { 2909 goto out; 2910 } 2911 2912 pc_dimm_unplug(PC_DIMM(dev), MACHINE(vms)); 2913 qdev_unrealize(dev); 2914 2915 out: 2916 error_propagate(errp, local_err); 2917 } 2918 2919 static void virt_machine_device_unplug_request_cb(HotplugHandler *hotplug_dev, 2920 DeviceState *dev, Error **errp) 2921 { 2922 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) { 2923 virt_dimm_unplug_request(hotplug_dev, dev, errp); 2924 } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MEM_PCI)) { 2925 virt_virtio_md_pci_unplug_request(hotplug_dev, dev, errp); 2926 } else { 2927 error_setg(errp, "device unplug request for unsupported device" 2928 " type: %s", object_get_typename(OBJECT(dev))); 2929 } 2930 } 2931 2932 static void virt_machine_device_unplug_cb(HotplugHandler *hotplug_dev, 2933 DeviceState *dev, Error **errp) 2934 { 2935 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) { 2936 virt_dimm_unplug(hotplug_dev, dev, errp); 2937 } else { 2938 error_setg(errp, "virt: device unplug for unsupported device" 2939 " type: %s", object_get_typename(OBJECT(dev))); 2940 } 2941 } 2942 2943 static HotplugHandler *virt_machine_get_hotplug_handler(MachineState *machine, 2944 DeviceState *dev) 2945 { 2946 MachineClass *mc = MACHINE_GET_CLASS(machine); 2947 2948 if (device_is_dynamic_sysbus(mc, dev) || 2949 object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM) || 2950 object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MEM_PCI) || 2951 object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_IOMMU_PCI)) { 2952 return HOTPLUG_HANDLER(machine); 2953 } 2954 return NULL; 2955 } 2956 2957 /* 2958 * for arm64 kvm_type [7-0] encodes the requested number of bits 2959 * in the IPA address space 2960 */ 2961 static int virt_kvm_type(MachineState *ms, const char *type_str) 2962 { 2963 VirtMachineState *vms = VIRT_MACHINE(ms); 2964 int max_vm_pa_size, requested_pa_size; 2965 bool fixed_ipa; 2966 2967 max_vm_pa_size = kvm_arm_get_max_vm_ipa_size(ms, &fixed_ipa); 2968 2969 /* we freeze the memory map to compute the highest gpa */ 2970 virt_set_memmap(vms, max_vm_pa_size); 2971 2972 requested_pa_size = 64 - clz64(vms->highest_gpa); 2973 2974 /* 2975 * KVM requires the IPA size to be at least 32 bits. 2976 */ 2977 if (requested_pa_size < 32) { 2978 requested_pa_size = 32; 2979 } 2980 2981 if (requested_pa_size > max_vm_pa_size) { 2982 error_report("-m and ,maxmem option values " 2983 "require an IPA range (%d bits) larger than " 2984 "the one supported by the host (%d bits)", 2985 requested_pa_size, max_vm_pa_size); 2986 exit(1); 2987 } 2988 /* 2989 * We return the requested PA log size, unless KVM only supports 2990 * the implicit legacy 40b IPA setting, in which case the kvm_type 2991 * must be 0. 2992 */ 2993 return fixed_ipa ? 0 : requested_pa_size; 2994 } 2995 2996 static void virt_machine_class_init(ObjectClass *oc, void *data) 2997 { 2998 MachineClass *mc = MACHINE_CLASS(oc); 2999 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc); 3000 3001 mc->init = machvirt_init; 3002 /* Start with max_cpus set to 512, which is the maximum supported by KVM. 3003 * The value may be reduced later when we have more information about the 3004 * configuration of the particular instance. 3005 */ 3006 mc->max_cpus = 512; 3007 machine_class_allow_dynamic_sysbus_dev(mc, TYPE_VFIO_CALXEDA_XGMAC); 3008 machine_class_allow_dynamic_sysbus_dev(mc, TYPE_VFIO_AMD_XGBE); 3009 machine_class_allow_dynamic_sysbus_dev(mc, TYPE_RAMFB_DEVICE); 3010 machine_class_allow_dynamic_sysbus_dev(mc, TYPE_VFIO_PLATFORM); 3011 #ifdef CONFIG_TPM 3012 machine_class_allow_dynamic_sysbus_dev(mc, TYPE_TPM_TIS_SYSBUS); 3013 #endif 3014 mc->block_default_type = IF_VIRTIO; 3015 mc->no_cdrom = 1; 3016 mc->pci_allow_0_address = true; 3017 /* We know we will never create a pre-ARMv7 CPU which needs 1K pages */ 3018 mc->minimum_page_bits = 12; 3019 mc->possible_cpu_arch_ids = virt_possible_cpu_arch_ids; 3020 mc->cpu_index_to_instance_props = virt_cpu_index_to_props; 3021 #ifdef CONFIG_TCG 3022 mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-a15"); 3023 #else 3024 mc->default_cpu_type = ARM_CPU_TYPE_NAME("max"); 3025 #endif 3026 mc->get_default_cpu_node_id = virt_get_default_cpu_node_id; 3027 mc->kvm_type = virt_kvm_type; 3028 assert(!mc->get_hotplug_handler); 3029 mc->get_hotplug_handler = virt_machine_get_hotplug_handler; 3030 hc->pre_plug = virt_machine_device_pre_plug_cb; 3031 hc->plug = virt_machine_device_plug_cb; 3032 hc->unplug_request = virt_machine_device_unplug_request_cb; 3033 hc->unplug = virt_machine_device_unplug_cb; 3034 mc->nvdimm_supported = true; 3035 mc->smp_props.clusters_supported = true; 3036 mc->auto_enable_numa_with_memhp = true; 3037 mc->auto_enable_numa_with_memdev = true; 3038 /* platform instead of architectural choice */ 3039 mc->cpu_cluster_has_numa_boundary = true; 3040 mc->default_ram_id = "mach-virt.ram"; 3041 mc->default_nic = "virtio-net-pci"; 3042 3043 object_class_property_add(oc, "acpi", "OnOffAuto", 3044 virt_get_acpi, virt_set_acpi, 3045 NULL, NULL); 3046 object_class_property_set_description(oc, "acpi", 3047 "Enable ACPI"); 3048 object_class_property_add_bool(oc, "secure", virt_get_secure, 3049 virt_set_secure); 3050 object_class_property_set_description(oc, "secure", 3051 "Set on/off to enable/disable the ARM " 3052 "Security Extensions (TrustZone)"); 3053 3054 object_class_property_add_bool(oc, "virtualization", virt_get_virt, 3055 virt_set_virt); 3056 object_class_property_set_description(oc, "virtualization", 3057 "Set on/off to enable/disable emulating a " 3058 "guest CPU which implements the ARM " 3059 "Virtualization Extensions"); 3060 3061 object_class_property_add_bool(oc, "highmem", virt_get_highmem, 3062 virt_set_highmem); 3063 object_class_property_set_description(oc, "highmem", 3064 "Set on/off to enable/disable using " 3065 "physical address space above 32 bits"); 3066 3067 object_class_property_add_bool(oc, "compact-highmem", 3068 virt_get_compact_highmem, 3069 virt_set_compact_highmem); 3070 object_class_property_set_description(oc, "compact-highmem", 3071 "Set on/off to enable/disable compact " 3072 "layout for high memory regions"); 3073 3074 object_class_property_add_bool(oc, "highmem-redists", 3075 virt_get_highmem_redists, 3076 virt_set_highmem_redists); 3077 object_class_property_set_description(oc, "highmem-redists", 3078 "Set on/off to enable/disable high " 3079 "memory region for GICv3 or GICv4 " 3080 "redistributor"); 3081 3082 object_class_property_add_bool(oc, "highmem-ecam", 3083 virt_get_highmem_ecam, 3084 virt_set_highmem_ecam); 3085 object_class_property_set_description(oc, "highmem-ecam", 3086 "Set on/off to enable/disable high " 3087 "memory region for PCI ECAM"); 3088 3089 object_class_property_add_bool(oc, "highmem-mmio", 3090 virt_get_highmem_mmio, 3091 virt_set_highmem_mmio); 3092 object_class_property_set_description(oc, "highmem-mmio", 3093 "Set on/off to enable/disable high " 3094 "memory region for PCI MMIO"); 3095 3096 object_class_property_add_str(oc, "gic-version", virt_get_gic_version, 3097 virt_set_gic_version); 3098 object_class_property_set_description(oc, "gic-version", 3099 "Set GIC version. " 3100 "Valid values are 2, 3, 4, host and max"); 3101 3102 object_class_property_add_str(oc, "iommu", virt_get_iommu, virt_set_iommu); 3103 object_class_property_set_description(oc, "iommu", 3104 "Set the IOMMU type. " 3105 "Valid values are none and smmuv3"); 3106 3107 object_class_property_add_bool(oc, "default-bus-bypass-iommu", 3108 virt_get_default_bus_bypass_iommu, 3109 virt_set_default_bus_bypass_iommu); 3110 object_class_property_set_description(oc, "default-bus-bypass-iommu", 3111 "Set on/off to enable/disable " 3112 "bypass_iommu for default root bus"); 3113 3114 object_class_property_add_bool(oc, "ras", virt_get_ras, 3115 virt_set_ras); 3116 object_class_property_set_description(oc, "ras", 3117 "Set on/off to enable/disable reporting host memory errors " 3118 "to a KVM guest using ACPI and guest external abort exceptions"); 3119 3120 object_class_property_add_bool(oc, "mte", virt_get_mte, virt_set_mte); 3121 object_class_property_set_description(oc, "mte", 3122 "Set on/off to enable/disable emulating a " 3123 "guest CPU which implements the ARM " 3124 "Memory Tagging Extension"); 3125 3126 object_class_property_add_bool(oc, "its", virt_get_its, 3127 virt_set_its); 3128 object_class_property_set_description(oc, "its", 3129 "Set on/off to enable/disable " 3130 "ITS instantiation"); 3131 3132 object_class_property_add_bool(oc, "dtb-randomness", 3133 virt_get_dtb_randomness, 3134 virt_set_dtb_randomness); 3135 object_class_property_set_description(oc, "dtb-randomness", 3136 "Set off to disable passing random or " 3137 "non-deterministic dtb nodes to guest"); 3138 3139 object_class_property_add_bool(oc, "dtb-kaslr-seed", 3140 virt_get_dtb_randomness, 3141 virt_set_dtb_randomness); 3142 object_class_property_set_description(oc, "dtb-kaslr-seed", 3143 "Deprecated synonym of dtb-randomness"); 3144 3145 object_class_property_add_str(oc, "x-oem-id", 3146 virt_get_oem_id, 3147 virt_set_oem_id); 3148 object_class_property_set_description(oc, "x-oem-id", 3149 "Override the default value of field OEMID " 3150 "in ACPI table header." 3151 "The string may be up to 6 bytes in size"); 3152 3153 3154 object_class_property_add_str(oc, "x-oem-table-id", 3155 virt_get_oem_table_id, 3156 virt_set_oem_table_id); 3157 object_class_property_set_description(oc, "x-oem-table-id", 3158 "Override the default value of field OEM Table ID " 3159 "in ACPI table header." 3160 "The string may be up to 8 bytes in size"); 3161 3162 } 3163 3164 static void virt_instance_init(Object *obj) 3165 { 3166 VirtMachineState *vms = VIRT_MACHINE(obj); 3167 VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms); 3168 3169 /* EL3 is disabled by default on virt: this makes us consistent 3170 * between KVM and TCG for this board, and it also allows us to 3171 * boot UEFI blobs which assume no TrustZone support. 3172 */ 3173 vms->secure = false; 3174 3175 /* EL2 is also disabled by default, for similar reasons */ 3176 vms->virt = false; 3177 3178 /* High memory is enabled by default */ 3179 vms->highmem = true; 3180 vms->highmem_compact = !vmc->no_highmem_compact; 3181 vms->gic_version = VIRT_GIC_VERSION_NOSEL; 3182 3183 vms->highmem_ecam = !vmc->no_highmem_ecam; 3184 vms->highmem_mmio = true; 3185 vms->highmem_redists = true; 3186 3187 if (vmc->no_its) { 3188 vms->its = false; 3189 } else { 3190 /* Default allows ITS instantiation */ 3191 vms->its = true; 3192 3193 if (vmc->no_tcg_its) { 3194 vms->tcg_its = false; 3195 } else { 3196 vms->tcg_its = true; 3197 } 3198 } 3199 3200 /* Default disallows iommu instantiation */ 3201 vms->iommu = VIRT_IOMMU_NONE; 3202 3203 /* The default root bus is attached to iommu by default */ 3204 vms->default_bus_bypass_iommu = false; 3205 3206 /* Default disallows RAS instantiation */ 3207 vms->ras = false; 3208 3209 /* MTE is disabled by default. */ 3210 vms->mte = false; 3211 3212 /* Supply kaslr-seed and rng-seed by default */ 3213 vms->dtb_randomness = true; 3214 3215 vms->irqmap = a15irqmap; 3216 3217 virt_flash_create(vms); 3218 3219 vms->oem_id = g_strndup(ACPI_BUILD_APPNAME6, 6); 3220 vms->oem_table_id = g_strndup(ACPI_BUILD_APPNAME8, 8); 3221 } 3222 3223 static const TypeInfo virt_machine_info = { 3224 .name = TYPE_VIRT_MACHINE, 3225 .parent = TYPE_MACHINE, 3226 .abstract = true, 3227 .instance_size = sizeof(VirtMachineState), 3228 .class_size = sizeof(VirtMachineClass), 3229 .class_init = virt_machine_class_init, 3230 .instance_init = virt_instance_init, 3231 .interfaces = (InterfaceInfo[]) { 3232 { TYPE_HOTPLUG_HANDLER }, 3233 { } 3234 }, 3235 }; 3236 3237 static void machvirt_machine_init(void) 3238 { 3239 type_register_static(&virt_machine_info); 3240 } 3241 type_init(machvirt_machine_init); 3242 3243 static void virt_machine_8_1_options(MachineClass *mc) 3244 { 3245 } 3246 DEFINE_VIRT_MACHINE_AS_LATEST(8, 1) 3247 3248 static void virt_machine_8_0_options(MachineClass *mc) 3249 { 3250 virt_machine_8_1_options(mc); 3251 compat_props_add(mc->compat_props, hw_compat_8_0, hw_compat_8_0_len); 3252 } 3253 DEFINE_VIRT_MACHINE(8, 0) 3254 3255 static void virt_machine_7_2_options(MachineClass *mc) 3256 { 3257 virt_machine_8_0_options(mc); 3258 compat_props_add(mc->compat_props, hw_compat_7_2, hw_compat_7_2_len); 3259 } 3260 DEFINE_VIRT_MACHINE(7, 2) 3261 3262 static void virt_machine_7_1_options(MachineClass *mc) 3263 { 3264 VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc)); 3265 3266 virt_machine_7_2_options(mc); 3267 compat_props_add(mc->compat_props, hw_compat_7_1, hw_compat_7_1_len); 3268 /* Compact layout for high memory regions was introduced with 7.2 */ 3269 vmc->no_highmem_compact = true; 3270 } 3271 DEFINE_VIRT_MACHINE(7, 1) 3272 3273 static void virt_machine_7_0_options(MachineClass *mc) 3274 { 3275 virt_machine_7_1_options(mc); 3276 compat_props_add(mc->compat_props, hw_compat_7_0, hw_compat_7_0_len); 3277 } 3278 DEFINE_VIRT_MACHINE(7, 0) 3279 3280 static void virt_machine_6_2_options(MachineClass *mc) 3281 { 3282 VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc)); 3283 3284 virt_machine_7_0_options(mc); 3285 compat_props_add(mc->compat_props, hw_compat_6_2, hw_compat_6_2_len); 3286 vmc->no_tcg_lpa2 = true; 3287 } 3288 DEFINE_VIRT_MACHINE(6, 2) 3289 3290 static void virt_machine_6_1_options(MachineClass *mc) 3291 { 3292 VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc)); 3293 3294 virt_machine_6_2_options(mc); 3295 compat_props_add(mc->compat_props, hw_compat_6_1, hw_compat_6_1_len); 3296 mc->smp_props.prefer_sockets = true; 3297 vmc->no_cpu_topology = true; 3298 3299 /* qemu ITS was introduced with 6.2 */ 3300 vmc->no_tcg_its = true; 3301 } 3302 DEFINE_VIRT_MACHINE(6, 1) 3303 3304 static void virt_machine_6_0_options(MachineClass *mc) 3305 { 3306 virt_machine_6_1_options(mc); 3307 compat_props_add(mc->compat_props, hw_compat_6_0, hw_compat_6_0_len); 3308 } 3309 DEFINE_VIRT_MACHINE(6, 0) 3310 3311 static void virt_machine_5_2_options(MachineClass *mc) 3312 { 3313 VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc)); 3314 3315 virt_machine_6_0_options(mc); 3316 compat_props_add(mc->compat_props, hw_compat_5_2, hw_compat_5_2_len); 3317 vmc->no_secure_gpio = true; 3318 } 3319 DEFINE_VIRT_MACHINE(5, 2) 3320 3321 static void virt_machine_5_1_options(MachineClass *mc) 3322 { 3323 VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc)); 3324 3325 virt_machine_5_2_options(mc); 3326 compat_props_add(mc->compat_props, hw_compat_5_1, hw_compat_5_1_len); 3327 vmc->no_kvm_steal_time = true; 3328 } 3329 DEFINE_VIRT_MACHINE(5, 1) 3330 3331 static void virt_machine_5_0_options(MachineClass *mc) 3332 { 3333 VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc)); 3334 3335 virt_machine_5_1_options(mc); 3336 compat_props_add(mc->compat_props, hw_compat_5_0, hw_compat_5_0_len); 3337 mc->numa_mem_supported = true; 3338 vmc->acpi_expose_flash = true; 3339 mc->auto_enable_numa_with_memdev = false; 3340 } 3341 DEFINE_VIRT_MACHINE(5, 0) 3342 3343 static void virt_machine_4_2_options(MachineClass *mc) 3344 { 3345 VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc)); 3346 3347 virt_machine_5_0_options(mc); 3348 compat_props_add(mc->compat_props, hw_compat_4_2, hw_compat_4_2_len); 3349 vmc->kvm_no_adjvtime = true; 3350 } 3351 DEFINE_VIRT_MACHINE(4, 2) 3352 3353 static void virt_machine_4_1_options(MachineClass *mc) 3354 { 3355 VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc)); 3356 3357 virt_machine_4_2_options(mc); 3358 compat_props_add(mc->compat_props, hw_compat_4_1, hw_compat_4_1_len); 3359 vmc->no_ged = true; 3360 mc->auto_enable_numa_with_memhp = false; 3361 } 3362 DEFINE_VIRT_MACHINE(4, 1) 3363 3364 static void virt_machine_4_0_options(MachineClass *mc) 3365 { 3366 virt_machine_4_1_options(mc); 3367 compat_props_add(mc->compat_props, hw_compat_4_0, hw_compat_4_0_len); 3368 } 3369 DEFINE_VIRT_MACHINE(4, 0) 3370 3371 static void virt_machine_3_1_options(MachineClass *mc) 3372 { 3373 virt_machine_4_0_options(mc); 3374 compat_props_add(mc->compat_props, hw_compat_3_1, hw_compat_3_1_len); 3375 } 3376 DEFINE_VIRT_MACHINE(3, 1) 3377 3378 static void virt_machine_3_0_options(MachineClass *mc) 3379 { 3380 virt_machine_3_1_options(mc); 3381 compat_props_add(mc->compat_props, hw_compat_3_0, hw_compat_3_0_len); 3382 } 3383 DEFINE_VIRT_MACHINE(3, 0) 3384 3385 static void virt_machine_2_12_options(MachineClass *mc) 3386 { 3387 VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc)); 3388 3389 virt_machine_3_0_options(mc); 3390 compat_props_add(mc->compat_props, hw_compat_2_12, hw_compat_2_12_len); 3391 vmc->no_highmem_ecam = true; 3392 mc->max_cpus = 255; 3393 } 3394 DEFINE_VIRT_MACHINE(2, 12) 3395 3396 static void virt_machine_2_11_options(MachineClass *mc) 3397 { 3398 VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc)); 3399 3400 virt_machine_2_12_options(mc); 3401 compat_props_add(mc->compat_props, hw_compat_2_11, hw_compat_2_11_len); 3402 vmc->smbios_old_sys_ver = true; 3403 } 3404 DEFINE_VIRT_MACHINE(2, 11) 3405 3406 static void virt_machine_2_10_options(MachineClass *mc) 3407 { 3408 virt_machine_2_11_options(mc); 3409 compat_props_add(mc->compat_props, hw_compat_2_10, hw_compat_2_10_len); 3410 /* before 2.11 we never faulted accesses to bad addresses */ 3411 mc->ignore_memory_transaction_failures = true; 3412 } 3413 DEFINE_VIRT_MACHINE(2, 10) 3414 3415 static void virt_machine_2_9_options(MachineClass *mc) 3416 { 3417 virt_machine_2_10_options(mc); 3418 compat_props_add(mc->compat_props, hw_compat_2_9, hw_compat_2_9_len); 3419 } 3420 DEFINE_VIRT_MACHINE(2, 9) 3421 3422 static void virt_machine_2_8_options(MachineClass *mc) 3423 { 3424 VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc)); 3425 3426 virt_machine_2_9_options(mc); 3427 compat_props_add(mc->compat_props, hw_compat_2_8, hw_compat_2_8_len); 3428 /* For 2.8 and earlier we falsely claimed in the DT that 3429 * our timers were edge-triggered, not level-triggered. 3430 */ 3431 vmc->claim_edge_triggered_timers = true; 3432 } 3433 DEFINE_VIRT_MACHINE(2, 8) 3434 3435 static void virt_machine_2_7_options(MachineClass *mc) 3436 { 3437 VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc)); 3438 3439 virt_machine_2_8_options(mc); 3440 compat_props_add(mc->compat_props, hw_compat_2_7, hw_compat_2_7_len); 3441 /* ITS was introduced with 2.8 */ 3442 vmc->no_its = true; 3443 /* Stick with 1K pages for migration compatibility */ 3444 mc->minimum_page_bits = 0; 3445 } 3446 DEFINE_VIRT_MACHINE(2, 7) 3447 3448 static void virt_machine_2_6_options(MachineClass *mc) 3449 { 3450 VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc)); 3451 3452 virt_machine_2_7_options(mc); 3453 compat_props_add(mc->compat_props, hw_compat_2_6, hw_compat_2_6_len); 3454 vmc->disallow_affinity_adjustment = true; 3455 /* Disable PMU for 2.6 as PMU support was first introduced in 2.7 */ 3456 vmc->no_pmu = true; 3457 } 3458 DEFINE_VIRT_MACHINE(2, 6) 3459