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