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