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-common.h" 33 #include "qemu/units.h" 34 #include "qemu/option.h" 35 #include "monitor/qdev.h" 36 #include "qapi/error.h" 37 #include "hw/sysbus.h" 38 #include "hw/boards.h" 39 #include "hw/arm/boot.h" 40 #include "hw/arm/primecell.h" 41 #include "hw/arm/virt.h" 42 #include "hw/block/flash.h" 43 #include "hw/vfio/vfio-calxeda-xgmac.h" 44 #include "hw/vfio/vfio-amd-xgbe.h" 45 #include "hw/display/ramfb.h" 46 #include "net/net.h" 47 #include "sysemu/device_tree.h" 48 #include "sysemu/numa.h" 49 #include "sysemu/runstate.h" 50 #include "sysemu/sysemu.h" 51 #include "sysemu/kvm.h" 52 #include "hw/loader.h" 53 #include "exec/address-spaces.h" 54 #include "qemu/bitops.h" 55 #include "qemu/error-report.h" 56 #include "qemu/module.h" 57 #include "hw/pci-host/gpex.h" 58 #include "hw/virtio/virtio-pci.h" 59 #include "hw/arm/sysbus-fdt.h" 60 #include "hw/platform-bus.h" 61 #include "hw/qdev-properties.h" 62 #include "hw/arm/fdt.h" 63 #include "hw/intc/arm_gic.h" 64 #include "hw/intc/arm_gicv3_common.h" 65 #include "hw/irq.h" 66 #include "kvm_arm.h" 67 #include "hw/firmware/smbios.h" 68 #include "qapi/visitor.h" 69 #include "standard-headers/linux/input.h" 70 #include "hw/arm/smmuv3.h" 71 #include "hw/acpi/acpi.h" 72 #include "target/arm/internals.h" 73 #include "hw/mem/pc-dimm.h" 74 #include "hw/mem/nvdimm.h" 75 #include "hw/acpi/generic_event_device.h" 76 #include "hw/virtio/virtio-iommu.h" 77 #include "hw/char/pl011.h" 78 79 #define DEFINE_VIRT_MACHINE_LATEST(major, minor, latest) \ 80 static void virt_##major##_##minor##_class_init(ObjectClass *oc, \ 81 void *data) \ 82 { \ 83 MachineClass *mc = MACHINE_CLASS(oc); \ 84 virt_machine_##major##_##minor##_options(mc); \ 85 mc->desc = "QEMU " # major "." # minor " ARM Virtual Machine"; \ 86 if (latest) { \ 87 mc->alias = "virt"; \ 88 } \ 89 } \ 90 static const TypeInfo machvirt_##major##_##minor##_info = { \ 91 .name = MACHINE_TYPE_NAME("virt-" # major "." # minor), \ 92 .parent = TYPE_VIRT_MACHINE, \ 93 .class_init = virt_##major##_##minor##_class_init, \ 94 }; \ 95 static void machvirt_machine_##major##_##minor##_init(void) \ 96 { \ 97 type_register_static(&machvirt_##major##_##minor##_info); \ 98 } \ 99 type_init(machvirt_machine_##major##_##minor##_init); 100 101 #define DEFINE_VIRT_MACHINE_AS_LATEST(major, minor) \ 102 DEFINE_VIRT_MACHINE_LATEST(major, minor, true) 103 #define DEFINE_VIRT_MACHINE(major, minor) \ 104 DEFINE_VIRT_MACHINE_LATEST(major, minor, false) 105 106 107 /* Number of external interrupt lines to configure the GIC with */ 108 #define NUM_IRQS 256 109 110 #define PLATFORM_BUS_NUM_IRQS 64 111 112 /* Legacy RAM limit in GB (< version 4.0) */ 113 #define LEGACY_RAMLIMIT_GB 255 114 #define LEGACY_RAMLIMIT_BYTES (LEGACY_RAMLIMIT_GB * GiB) 115 116 /* Addresses and sizes of our components. 117 * 0..128MB is space for a flash device so we can run bootrom code such as UEFI. 118 * 128MB..256MB is used for miscellaneous device I/O. 119 * 256MB..1GB is reserved for possible future PCI support (ie where the 120 * PCI memory window will go if we add a PCI host controller). 121 * 1GB and up is RAM (which may happily spill over into the 122 * high memory region beyond 4GB). 123 * This represents a compromise between how much RAM can be given to 124 * a 32 bit VM and leaving space for expansion and in particular for PCI. 125 * Note that devices should generally be placed at multiples of 0x10000, 126 * to accommodate guests using 64K pages. 127 */ 128 static const MemMapEntry base_memmap[] = { 129 /* Space up to 0x8000000 is reserved for a boot ROM */ 130 [VIRT_FLASH] = { 0, 0x08000000 }, 131 [VIRT_CPUPERIPHS] = { 0x08000000, 0x00020000 }, 132 /* GIC distributor and CPU interfaces sit inside the CPU peripheral space */ 133 [VIRT_GIC_DIST] = { 0x08000000, 0x00010000 }, 134 [VIRT_GIC_CPU] = { 0x08010000, 0x00010000 }, 135 [VIRT_GIC_V2M] = { 0x08020000, 0x00001000 }, 136 [VIRT_GIC_HYP] = { 0x08030000, 0x00010000 }, 137 [VIRT_GIC_VCPU] = { 0x08040000, 0x00010000 }, 138 /* The space in between here is reserved for GICv3 CPU/vCPU/HYP */ 139 [VIRT_GIC_ITS] = { 0x08080000, 0x00020000 }, 140 /* This redistributor space allows up to 2*64kB*123 CPUs */ 141 [VIRT_GIC_REDIST] = { 0x080A0000, 0x00F60000 }, 142 [VIRT_UART] = { 0x09000000, 0x00001000 }, 143 [VIRT_RTC] = { 0x09010000, 0x00001000 }, 144 [VIRT_FW_CFG] = { 0x09020000, 0x00000018 }, 145 [VIRT_GPIO] = { 0x09030000, 0x00001000 }, 146 [VIRT_SECURE_UART] = { 0x09040000, 0x00001000 }, 147 [VIRT_SMMU] = { 0x09050000, 0x00020000 }, 148 [VIRT_PCDIMM_ACPI] = { 0x09070000, MEMORY_HOTPLUG_IO_LEN }, 149 [VIRT_ACPI_GED] = { 0x09080000, ACPI_GED_EVT_SEL_LEN }, 150 [VIRT_MMIO] = { 0x0a000000, 0x00000200 }, 151 /* ...repeating for a total of NUM_VIRTIO_TRANSPORTS, each of that size */ 152 [VIRT_PLATFORM_BUS] = { 0x0c000000, 0x02000000 }, 153 [VIRT_SECURE_MEM] = { 0x0e000000, 0x01000000 }, 154 [VIRT_PCIE_MMIO] = { 0x10000000, 0x2eff0000 }, 155 [VIRT_PCIE_PIO] = { 0x3eff0000, 0x00010000 }, 156 [VIRT_PCIE_ECAM] = { 0x3f000000, 0x01000000 }, 157 /* Actual RAM size depends on initial RAM and device memory settings */ 158 [VIRT_MEM] = { GiB, LEGACY_RAMLIMIT_BYTES }, 159 }; 160 161 /* 162 * Highmem IO Regions: This memory map is floating, located after the RAM. 163 * Each MemMapEntry base (GPA) will be dynamically computed, depending on the 164 * top of the RAM, so that its base get the same alignment as the size, 165 * ie. a 512GiB entry will be aligned on a 512GiB boundary. If there is 166 * less than 256GiB of RAM, the floating area starts at the 256GiB mark. 167 * Note the extended_memmap is sized so that it eventually also includes the 168 * base_memmap entries (VIRT_HIGH_GIC_REDIST2 index is greater than the last 169 * index of base_memmap). 170 */ 171 static MemMapEntry extended_memmap[] = { 172 /* Additional 64 MB redist region (can contain up to 512 redistributors) */ 173 [VIRT_HIGH_GIC_REDIST2] = { 0x0, 64 * MiB }, 174 [VIRT_HIGH_PCIE_ECAM] = { 0x0, 256 * MiB }, 175 /* Second PCIe window */ 176 [VIRT_HIGH_PCIE_MMIO] = { 0x0, 512 * GiB }, 177 }; 178 179 static const int a15irqmap[] = { 180 [VIRT_UART] = 1, 181 [VIRT_RTC] = 2, 182 [VIRT_PCIE] = 3, /* ... to 6 */ 183 [VIRT_GPIO] = 7, 184 [VIRT_SECURE_UART] = 8, 185 [VIRT_ACPI_GED] = 9, 186 [VIRT_MMIO] = 16, /* ...to 16 + NUM_VIRTIO_TRANSPORTS - 1 */ 187 [VIRT_GIC_V2M] = 48, /* ...to 48 + NUM_GICV2M_SPIS - 1 */ 188 [VIRT_SMMU] = 74, /* ...to 74 + NUM_SMMU_IRQS - 1 */ 189 [VIRT_PLATFORM_BUS] = 112, /* ...to 112 + PLATFORM_BUS_NUM_IRQS -1 */ 190 }; 191 192 static const char *valid_cpus[] = { 193 ARM_CPU_TYPE_NAME("cortex-a7"), 194 ARM_CPU_TYPE_NAME("cortex-a15"), 195 ARM_CPU_TYPE_NAME("cortex-a53"), 196 ARM_CPU_TYPE_NAME("cortex-a57"), 197 ARM_CPU_TYPE_NAME("cortex-a72"), 198 ARM_CPU_TYPE_NAME("host"), 199 ARM_CPU_TYPE_NAME("max"), 200 }; 201 202 static bool cpu_type_valid(const char *cpu) 203 { 204 int i; 205 206 for (i = 0; i < ARRAY_SIZE(valid_cpus); i++) { 207 if (strcmp(cpu, valid_cpus[i]) == 0) { 208 return true; 209 } 210 } 211 return false; 212 } 213 214 static void create_fdt(VirtMachineState *vms) 215 { 216 MachineState *ms = MACHINE(vms); 217 int nb_numa_nodes = ms->numa_state->num_nodes; 218 void *fdt = create_device_tree(&vms->fdt_size); 219 220 if (!fdt) { 221 error_report("create_device_tree() failed"); 222 exit(1); 223 } 224 225 vms->fdt = fdt; 226 227 /* Header */ 228 qemu_fdt_setprop_string(fdt, "/", "compatible", "linux,dummy-virt"); 229 qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2); 230 qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2); 231 232 /* /chosen must exist for load_dtb to fill in necessary properties later */ 233 qemu_fdt_add_subnode(fdt, "/chosen"); 234 235 /* Clock node, for the benefit of the UART. The kernel device tree 236 * binding documentation claims the PL011 node clock properties are 237 * optional but in practice if you omit them the kernel refuses to 238 * probe for the device. 239 */ 240 vms->clock_phandle = qemu_fdt_alloc_phandle(fdt); 241 qemu_fdt_add_subnode(fdt, "/apb-pclk"); 242 qemu_fdt_setprop_string(fdt, "/apb-pclk", "compatible", "fixed-clock"); 243 qemu_fdt_setprop_cell(fdt, "/apb-pclk", "#clock-cells", 0x0); 244 qemu_fdt_setprop_cell(fdt, "/apb-pclk", "clock-frequency", 24000000); 245 qemu_fdt_setprop_string(fdt, "/apb-pclk", "clock-output-names", 246 "clk24mhz"); 247 qemu_fdt_setprop_cell(fdt, "/apb-pclk", "phandle", vms->clock_phandle); 248 249 if (nb_numa_nodes > 0 && ms->numa_state->have_numa_distance) { 250 int size = nb_numa_nodes * nb_numa_nodes * 3 * sizeof(uint32_t); 251 uint32_t *matrix = g_malloc0(size); 252 int idx, i, j; 253 254 for (i = 0; i < nb_numa_nodes; i++) { 255 for (j = 0; j < nb_numa_nodes; j++) { 256 idx = (i * nb_numa_nodes + j) * 3; 257 matrix[idx + 0] = cpu_to_be32(i); 258 matrix[idx + 1] = cpu_to_be32(j); 259 matrix[idx + 2] = 260 cpu_to_be32(ms->numa_state->nodes[i].distance[j]); 261 } 262 } 263 264 qemu_fdt_add_subnode(fdt, "/distance-map"); 265 qemu_fdt_setprop_string(fdt, "/distance-map", "compatible", 266 "numa-distance-map-v1"); 267 qemu_fdt_setprop(fdt, "/distance-map", "distance-matrix", 268 matrix, size); 269 g_free(matrix); 270 } 271 } 272 273 static void fdt_add_timer_nodes(const VirtMachineState *vms) 274 { 275 /* On real hardware these interrupts are level-triggered. 276 * On KVM they were edge-triggered before host kernel version 4.4, 277 * and level-triggered afterwards. 278 * On emulated QEMU they are level-triggered. 279 * 280 * Getting the DTB info about them wrong is awkward for some 281 * guest kernels: 282 * pre-4.8 ignore the DT and leave the interrupt configured 283 * with whatever the GIC reset value (or the bootloader) left it at 284 * 4.8 before rc6 honour the incorrect data by programming it back 285 * into the GIC, causing problems 286 * 4.8rc6 and later ignore the DT and always write "level triggered" 287 * into the GIC 288 * 289 * For backwards-compatibility, virt-2.8 and earlier will continue 290 * to say these are edge-triggered, but later machines will report 291 * the correct information. 292 */ 293 ARMCPU *armcpu; 294 VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms); 295 uint32_t irqflags = GIC_FDT_IRQ_FLAGS_LEVEL_HI; 296 297 if (vmc->claim_edge_triggered_timers) { 298 irqflags = GIC_FDT_IRQ_FLAGS_EDGE_LO_HI; 299 } 300 301 if (vms->gic_version == 2) { 302 irqflags = deposit32(irqflags, GIC_FDT_IRQ_PPI_CPU_START, 303 GIC_FDT_IRQ_PPI_CPU_WIDTH, 304 (1 << vms->smp_cpus) - 1); 305 } 306 307 qemu_fdt_add_subnode(vms->fdt, "/timer"); 308 309 armcpu = ARM_CPU(qemu_get_cpu(0)); 310 if (arm_feature(&armcpu->env, ARM_FEATURE_V8)) { 311 const char compat[] = "arm,armv8-timer\0arm,armv7-timer"; 312 qemu_fdt_setprop(vms->fdt, "/timer", "compatible", 313 compat, sizeof(compat)); 314 } else { 315 qemu_fdt_setprop_string(vms->fdt, "/timer", "compatible", 316 "arm,armv7-timer"); 317 } 318 qemu_fdt_setprop(vms->fdt, "/timer", "always-on", NULL, 0); 319 qemu_fdt_setprop_cells(vms->fdt, "/timer", "interrupts", 320 GIC_FDT_IRQ_TYPE_PPI, ARCH_TIMER_S_EL1_IRQ, irqflags, 321 GIC_FDT_IRQ_TYPE_PPI, ARCH_TIMER_NS_EL1_IRQ, irqflags, 322 GIC_FDT_IRQ_TYPE_PPI, ARCH_TIMER_VIRT_IRQ, irqflags, 323 GIC_FDT_IRQ_TYPE_PPI, ARCH_TIMER_NS_EL2_IRQ, irqflags); 324 } 325 326 static void fdt_add_cpu_nodes(const VirtMachineState *vms) 327 { 328 int cpu; 329 int addr_cells = 1; 330 const MachineState *ms = MACHINE(vms); 331 332 /* 333 * From Documentation/devicetree/bindings/arm/cpus.txt 334 * On ARM v8 64-bit systems value should be set to 2, 335 * that corresponds to the MPIDR_EL1 register size. 336 * If MPIDR_EL1[63:32] value is equal to 0 on all CPUs 337 * in the system, #address-cells can be set to 1, since 338 * MPIDR_EL1[63:32] bits are not used for CPUs 339 * identification. 340 * 341 * Here we actually don't know whether our system is 32- or 64-bit one. 342 * The simplest way to go is to examine affinity IDs of all our CPUs. If 343 * at least one of them has Aff3 populated, we set #address-cells to 2. 344 */ 345 for (cpu = 0; cpu < vms->smp_cpus; cpu++) { 346 ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(cpu)); 347 348 if (armcpu->mp_affinity & ARM_AFF3_MASK) { 349 addr_cells = 2; 350 break; 351 } 352 } 353 354 qemu_fdt_add_subnode(vms->fdt, "/cpus"); 355 qemu_fdt_setprop_cell(vms->fdt, "/cpus", "#address-cells", addr_cells); 356 qemu_fdt_setprop_cell(vms->fdt, "/cpus", "#size-cells", 0x0); 357 358 for (cpu = vms->smp_cpus - 1; cpu >= 0; cpu--) { 359 char *nodename = g_strdup_printf("/cpus/cpu@%d", cpu); 360 ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(cpu)); 361 CPUState *cs = CPU(armcpu); 362 363 qemu_fdt_add_subnode(vms->fdt, nodename); 364 qemu_fdt_setprop_string(vms->fdt, nodename, "device_type", "cpu"); 365 qemu_fdt_setprop_string(vms->fdt, nodename, "compatible", 366 armcpu->dtb_compatible); 367 368 if (vms->psci_conduit != QEMU_PSCI_CONDUIT_DISABLED 369 && vms->smp_cpus > 1) { 370 qemu_fdt_setprop_string(vms->fdt, nodename, 371 "enable-method", "psci"); 372 } 373 374 if (addr_cells == 2) { 375 qemu_fdt_setprop_u64(vms->fdt, nodename, "reg", 376 armcpu->mp_affinity); 377 } else { 378 qemu_fdt_setprop_cell(vms->fdt, nodename, "reg", 379 armcpu->mp_affinity); 380 } 381 382 if (ms->possible_cpus->cpus[cs->cpu_index].props.has_node_id) { 383 qemu_fdt_setprop_cell(vms->fdt, nodename, "numa-node-id", 384 ms->possible_cpus->cpus[cs->cpu_index].props.node_id); 385 } 386 387 g_free(nodename); 388 } 389 } 390 391 static void fdt_add_its_gic_node(VirtMachineState *vms) 392 { 393 char *nodename; 394 395 vms->msi_phandle = qemu_fdt_alloc_phandle(vms->fdt); 396 nodename = g_strdup_printf("/intc/its@%" PRIx64, 397 vms->memmap[VIRT_GIC_ITS].base); 398 qemu_fdt_add_subnode(vms->fdt, nodename); 399 qemu_fdt_setprop_string(vms->fdt, nodename, "compatible", 400 "arm,gic-v3-its"); 401 qemu_fdt_setprop(vms->fdt, nodename, "msi-controller", NULL, 0); 402 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg", 403 2, vms->memmap[VIRT_GIC_ITS].base, 404 2, vms->memmap[VIRT_GIC_ITS].size); 405 qemu_fdt_setprop_cell(vms->fdt, nodename, "phandle", vms->msi_phandle); 406 g_free(nodename); 407 } 408 409 static void fdt_add_v2m_gic_node(VirtMachineState *vms) 410 { 411 char *nodename; 412 413 nodename = g_strdup_printf("/intc/v2m@%" PRIx64, 414 vms->memmap[VIRT_GIC_V2M].base); 415 vms->msi_phandle = qemu_fdt_alloc_phandle(vms->fdt); 416 qemu_fdt_add_subnode(vms->fdt, nodename); 417 qemu_fdt_setprop_string(vms->fdt, nodename, "compatible", 418 "arm,gic-v2m-frame"); 419 qemu_fdt_setprop(vms->fdt, nodename, "msi-controller", NULL, 0); 420 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg", 421 2, vms->memmap[VIRT_GIC_V2M].base, 422 2, vms->memmap[VIRT_GIC_V2M].size); 423 qemu_fdt_setprop_cell(vms->fdt, nodename, "phandle", vms->msi_phandle); 424 g_free(nodename); 425 } 426 427 static void fdt_add_gic_node(VirtMachineState *vms) 428 { 429 char *nodename; 430 431 vms->gic_phandle = qemu_fdt_alloc_phandle(vms->fdt); 432 qemu_fdt_setprop_cell(vms->fdt, "/", "interrupt-parent", vms->gic_phandle); 433 434 nodename = g_strdup_printf("/intc@%" PRIx64, 435 vms->memmap[VIRT_GIC_DIST].base); 436 qemu_fdt_add_subnode(vms->fdt, nodename); 437 qemu_fdt_setprop_cell(vms->fdt, nodename, "#interrupt-cells", 3); 438 qemu_fdt_setprop(vms->fdt, nodename, "interrupt-controller", NULL, 0); 439 qemu_fdt_setprop_cell(vms->fdt, nodename, "#address-cells", 0x2); 440 qemu_fdt_setprop_cell(vms->fdt, nodename, "#size-cells", 0x2); 441 qemu_fdt_setprop(vms->fdt, nodename, "ranges", NULL, 0); 442 if (vms->gic_version == 3) { 443 int nb_redist_regions = virt_gicv3_redist_region_count(vms); 444 445 qemu_fdt_setprop_string(vms->fdt, nodename, "compatible", 446 "arm,gic-v3"); 447 448 qemu_fdt_setprop_cell(vms->fdt, nodename, 449 "#redistributor-regions", nb_redist_regions); 450 451 if (nb_redist_regions == 1) { 452 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg", 453 2, vms->memmap[VIRT_GIC_DIST].base, 454 2, vms->memmap[VIRT_GIC_DIST].size, 455 2, vms->memmap[VIRT_GIC_REDIST].base, 456 2, vms->memmap[VIRT_GIC_REDIST].size); 457 } else { 458 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg", 459 2, vms->memmap[VIRT_GIC_DIST].base, 460 2, vms->memmap[VIRT_GIC_DIST].size, 461 2, vms->memmap[VIRT_GIC_REDIST].base, 462 2, vms->memmap[VIRT_GIC_REDIST].size, 463 2, vms->memmap[VIRT_HIGH_GIC_REDIST2].base, 464 2, vms->memmap[VIRT_HIGH_GIC_REDIST2].size); 465 } 466 467 if (vms->virt) { 468 qemu_fdt_setprop_cells(vms->fdt, nodename, "interrupts", 469 GIC_FDT_IRQ_TYPE_PPI, ARCH_GIC_MAINT_IRQ, 470 GIC_FDT_IRQ_FLAGS_LEVEL_HI); 471 } 472 } else { 473 /* 'cortex-a15-gic' means 'GIC v2' */ 474 qemu_fdt_setprop_string(vms->fdt, nodename, "compatible", 475 "arm,cortex-a15-gic"); 476 if (!vms->virt) { 477 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg", 478 2, vms->memmap[VIRT_GIC_DIST].base, 479 2, vms->memmap[VIRT_GIC_DIST].size, 480 2, vms->memmap[VIRT_GIC_CPU].base, 481 2, vms->memmap[VIRT_GIC_CPU].size); 482 } else { 483 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg", 484 2, vms->memmap[VIRT_GIC_DIST].base, 485 2, vms->memmap[VIRT_GIC_DIST].size, 486 2, vms->memmap[VIRT_GIC_CPU].base, 487 2, vms->memmap[VIRT_GIC_CPU].size, 488 2, vms->memmap[VIRT_GIC_HYP].base, 489 2, vms->memmap[VIRT_GIC_HYP].size, 490 2, vms->memmap[VIRT_GIC_VCPU].base, 491 2, vms->memmap[VIRT_GIC_VCPU].size); 492 qemu_fdt_setprop_cells(vms->fdt, nodename, "interrupts", 493 GIC_FDT_IRQ_TYPE_PPI, ARCH_GIC_MAINT_IRQ, 494 GIC_FDT_IRQ_FLAGS_LEVEL_HI); 495 } 496 } 497 498 qemu_fdt_setprop_cell(vms->fdt, nodename, "phandle", vms->gic_phandle); 499 g_free(nodename); 500 } 501 502 static void fdt_add_pmu_nodes(const VirtMachineState *vms) 503 { 504 CPUState *cpu; 505 ARMCPU *armcpu; 506 uint32_t irqflags = GIC_FDT_IRQ_FLAGS_LEVEL_HI; 507 508 CPU_FOREACH(cpu) { 509 armcpu = ARM_CPU(cpu); 510 if (!arm_feature(&armcpu->env, ARM_FEATURE_PMU)) { 511 return; 512 } 513 if (kvm_enabled()) { 514 if (kvm_irqchip_in_kernel()) { 515 kvm_arm_pmu_set_irq(cpu, PPI(VIRTUAL_PMU_IRQ)); 516 } 517 kvm_arm_pmu_init(cpu); 518 } 519 } 520 521 if (vms->gic_version == 2) { 522 irqflags = deposit32(irqflags, GIC_FDT_IRQ_PPI_CPU_START, 523 GIC_FDT_IRQ_PPI_CPU_WIDTH, 524 (1 << vms->smp_cpus) - 1); 525 } 526 527 armcpu = ARM_CPU(qemu_get_cpu(0)); 528 qemu_fdt_add_subnode(vms->fdt, "/pmu"); 529 if (arm_feature(&armcpu->env, ARM_FEATURE_V8)) { 530 const char compat[] = "arm,armv8-pmuv3"; 531 qemu_fdt_setprop(vms->fdt, "/pmu", "compatible", 532 compat, sizeof(compat)); 533 qemu_fdt_setprop_cells(vms->fdt, "/pmu", "interrupts", 534 GIC_FDT_IRQ_TYPE_PPI, VIRTUAL_PMU_IRQ, irqflags); 535 } 536 } 537 538 static inline DeviceState *create_acpi_ged(VirtMachineState *vms) 539 { 540 DeviceState *dev; 541 MachineState *ms = MACHINE(vms); 542 int irq = vms->irqmap[VIRT_ACPI_GED]; 543 uint32_t event = ACPI_GED_PWR_DOWN_EVT; 544 545 if (ms->ram_slots) { 546 event |= ACPI_GED_MEM_HOTPLUG_EVT; 547 } 548 549 dev = qdev_create(NULL, TYPE_ACPI_GED); 550 qdev_prop_set_uint32(dev, "ged-event", event); 551 552 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, vms->memmap[VIRT_ACPI_GED].base); 553 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 1, vms->memmap[VIRT_PCDIMM_ACPI].base); 554 sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0, qdev_get_gpio_in(vms->gic, irq)); 555 556 qdev_init_nofail(dev); 557 558 return dev; 559 } 560 561 static void create_its(VirtMachineState *vms) 562 { 563 const char *itsclass = its_class_name(); 564 DeviceState *dev; 565 566 if (!itsclass) { 567 /* Do nothing if not supported */ 568 return; 569 } 570 571 dev = qdev_create(NULL, itsclass); 572 573 object_property_set_link(OBJECT(dev), OBJECT(vms->gic), "parent-gicv3", 574 &error_abort); 575 qdev_init_nofail(dev); 576 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, vms->memmap[VIRT_GIC_ITS].base); 577 578 fdt_add_its_gic_node(vms); 579 } 580 581 static void create_v2m(VirtMachineState *vms) 582 { 583 int i; 584 int irq = vms->irqmap[VIRT_GIC_V2M]; 585 DeviceState *dev; 586 587 dev = qdev_create(NULL, "arm-gicv2m"); 588 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, vms->memmap[VIRT_GIC_V2M].base); 589 qdev_prop_set_uint32(dev, "base-spi", irq); 590 qdev_prop_set_uint32(dev, "num-spi", NUM_GICV2M_SPIS); 591 qdev_init_nofail(dev); 592 593 for (i = 0; i < NUM_GICV2M_SPIS; i++) { 594 sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, 595 qdev_get_gpio_in(vms->gic, irq + i)); 596 } 597 598 fdt_add_v2m_gic_node(vms); 599 } 600 601 static void create_gic(VirtMachineState *vms) 602 { 603 MachineState *ms = MACHINE(vms); 604 /* We create a standalone GIC */ 605 SysBusDevice *gicbusdev; 606 const char *gictype; 607 int type = vms->gic_version, i; 608 unsigned int smp_cpus = ms->smp.cpus; 609 uint32_t nb_redist_regions = 0; 610 611 gictype = (type == 3) ? gicv3_class_name() : gic_class_name(); 612 613 vms->gic = qdev_create(NULL, gictype); 614 qdev_prop_set_uint32(vms->gic, "revision", type); 615 qdev_prop_set_uint32(vms->gic, "num-cpu", smp_cpus); 616 /* Note that the num-irq property counts both internal and external 617 * interrupts; there are always 32 of the former (mandated by GIC spec). 618 */ 619 qdev_prop_set_uint32(vms->gic, "num-irq", NUM_IRQS + 32); 620 if (!kvm_irqchip_in_kernel()) { 621 qdev_prop_set_bit(vms->gic, "has-security-extensions", vms->secure); 622 } 623 624 if (type == 3) { 625 uint32_t redist0_capacity = 626 vms->memmap[VIRT_GIC_REDIST].size / GICV3_REDIST_SIZE; 627 uint32_t redist0_count = MIN(smp_cpus, redist0_capacity); 628 629 nb_redist_regions = virt_gicv3_redist_region_count(vms); 630 631 qdev_prop_set_uint32(vms->gic, "len-redist-region-count", 632 nb_redist_regions); 633 qdev_prop_set_uint32(vms->gic, "redist-region-count[0]", redist0_count); 634 635 if (nb_redist_regions == 2) { 636 uint32_t redist1_capacity = 637 vms->memmap[VIRT_HIGH_GIC_REDIST2].size / GICV3_REDIST_SIZE; 638 639 qdev_prop_set_uint32(vms->gic, "redist-region-count[1]", 640 MIN(smp_cpus - redist0_count, redist1_capacity)); 641 } 642 } else { 643 if (!kvm_irqchip_in_kernel()) { 644 qdev_prop_set_bit(vms->gic, "has-virtualization-extensions", 645 vms->virt); 646 } 647 } 648 qdev_init_nofail(vms->gic); 649 gicbusdev = SYS_BUS_DEVICE(vms->gic); 650 sysbus_mmio_map(gicbusdev, 0, vms->memmap[VIRT_GIC_DIST].base); 651 if (type == 3) { 652 sysbus_mmio_map(gicbusdev, 1, vms->memmap[VIRT_GIC_REDIST].base); 653 if (nb_redist_regions == 2) { 654 sysbus_mmio_map(gicbusdev, 2, 655 vms->memmap[VIRT_HIGH_GIC_REDIST2].base); 656 } 657 } else { 658 sysbus_mmio_map(gicbusdev, 1, vms->memmap[VIRT_GIC_CPU].base); 659 if (vms->virt) { 660 sysbus_mmio_map(gicbusdev, 2, vms->memmap[VIRT_GIC_HYP].base); 661 sysbus_mmio_map(gicbusdev, 3, vms->memmap[VIRT_GIC_VCPU].base); 662 } 663 } 664 665 /* Wire the outputs from each CPU's generic timer and the GICv3 666 * maintenance interrupt signal to the appropriate GIC PPI inputs, 667 * and the GIC's IRQ/FIQ/VIRQ/VFIQ interrupt outputs to the CPU's inputs. 668 */ 669 for (i = 0; i < smp_cpus; i++) { 670 DeviceState *cpudev = DEVICE(qemu_get_cpu(i)); 671 int ppibase = NUM_IRQS + i * GIC_INTERNAL + GIC_NR_SGIS; 672 int irq; 673 /* Mapping from the output timer irq lines from the CPU to the 674 * GIC PPI inputs we use for the virt board. 675 */ 676 const int timer_irq[] = { 677 [GTIMER_PHYS] = ARCH_TIMER_NS_EL1_IRQ, 678 [GTIMER_VIRT] = ARCH_TIMER_VIRT_IRQ, 679 [GTIMER_HYP] = ARCH_TIMER_NS_EL2_IRQ, 680 [GTIMER_SEC] = ARCH_TIMER_S_EL1_IRQ, 681 }; 682 683 for (irq = 0; irq < ARRAY_SIZE(timer_irq); irq++) { 684 qdev_connect_gpio_out(cpudev, irq, 685 qdev_get_gpio_in(vms->gic, 686 ppibase + timer_irq[irq])); 687 } 688 689 if (type == 3) { 690 qemu_irq irq = qdev_get_gpio_in(vms->gic, 691 ppibase + ARCH_GIC_MAINT_IRQ); 692 qdev_connect_gpio_out_named(cpudev, "gicv3-maintenance-interrupt", 693 0, irq); 694 } else if (vms->virt) { 695 qemu_irq irq = qdev_get_gpio_in(vms->gic, 696 ppibase + ARCH_GIC_MAINT_IRQ); 697 sysbus_connect_irq(gicbusdev, i + 4 * smp_cpus, irq); 698 } 699 700 qdev_connect_gpio_out_named(cpudev, "pmu-interrupt", 0, 701 qdev_get_gpio_in(vms->gic, ppibase 702 + VIRTUAL_PMU_IRQ)); 703 704 sysbus_connect_irq(gicbusdev, i, qdev_get_gpio_in(cpudev, ARM_CPU_IRQ)); 705 sysbus_connect_irq(gicbusdev, i + smp_cpus, 706 qdev_get_gpio_in(cpudev, ARM_CPU_FIQ)); 707 sysbus_connect_irq(gicbusdev, i + 2 * smp_cpus, 708 qdev_get_gpio_in(cpudev, ARM_CPU_VIRQ)); 709 sysbus_connect_irq(gicbusdev, i + 3 * smp_cpus, 710 qdev_get_gpio_in(cpudev, ARM_CPU_VFIQ)); 711 } 712 713 fdt_add_gic_node(vms); 714 715 if (type == 3 && vms->its) { 716 create_its(vms); 717 } else if (type == 2) { 718 create_v2m(vms); 719 } 720 } 721 722 static void create_uart(const VirtMachineState *vms, int uart, 723 MemoryRegion *mem, Chardev *chr) 724 { 725 char *nodename; 726 hwaddr base = vms->memmap[uart].base; 727 hwaddr size = vms->memmap[uart].size; 728 int irq = vms->irqmap[uart]; 729 const char compat[] = "arm,pl011\0arm,primecell"; 730 const char clocknames[] = "uartclk\0apb_pclk"; 731 DeviceState *dev = qdev_create(NULL, TYPE_PL011); 732 SysBusDevice *s = SYS_BUS_DEVICE(dev); 733 734 qdev_prop_set_chr(dev, "chardev", chr); 735 qdev_init_nofail(dev); 736 memory_region_add_subregion(mem, base, 737 sysbus_mmio_get_region(s, 0)); 738 sysbus_connect_irq(s, 0, qdev_get_gpio_in(vms->gic, irq)); 739 740 nodename = g_strdup_printf("/pl011@%" PRIx64, base); 741 qemu_fdt_add_subnode(vms->fdt, nodename); 742 /* Note that we can't use setprop_string because of the embedded NUL */ 743 qemu_fdt_setprop(vms->fdt, nodename, "compatible", 744 compat, sizeof(compat)); 745 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg", 746 2, base, 2, size); 747 qemu_fdt_setprop_cells(vms->fdt, nodename, "interrupts", 748 GIC_FDT_IRQ_TYPE_SPI, irq, 749 GIC_FDT_IRQ_FLAGS_LEVEL_HI); 750 qemu_fdt_setprop_cells(vms->fdt, nodename, "clocks", 751 vms->clock_phandle, vms->clock_phandle); 752 qemu_fdt_setprop(vms->fdt, nodename, "clock-names", 753 clocknames, sizeof(clocknames)); 754 755 if (uart == VIRT_UART) { 756 qemu_fdt_setprop_string(vms->fdt, "/chosen", "stdout-path", nodename); 757 } else { 758 /* Mark as not usable by the normal world */ 759 qemu_fdt_setprop_string(vms->fdt, nodename, "status", "disabled"); 760 qemu_fdt_setprop_string(vms->fdt, nodename, "secure-status", "okay"); 761 762 qemu_fdt_add_subnode(vms->fdt, "/secure-chosen"); 763 qemu_fdt_setprop_string(vms->fdt, "/secure-chosen", "stdout-path", 764 nodename); 765 } 766 767 g_free(nodename); 768 } 769 770 static void create_rtc(const VirtMachineState *vms) 771 { 772 char *nodename; 773 hwaddr base = vms->memmap[VIRT_RTC].base; 774 hwaddr size = vms->memmap[VIRT_RTC].size; 775 int irq = vms->irqmap[VIRT_RTC]; 776 const char compat[] = "arm,pl031\0arm,primecell"; 777 778 sysbus_create_simple("pl031", base, qdev_get_gpio_in(vms->gic, irq)); 779 780 nodename = g_strdup_printf("/pl031@%" PRIx64, base); 781 qemu_fdt_add_subnode(vms->fdt, nodename); 782 qemu_fdt_setprop(vms->fdt, nodename, "compatible", compat, sizeof(compat)); 783 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg", 784 2, base, 2, size); 785 qemu_fdt_setprop_cells(vms->fdt, nodename, "interrupts", 786 GIC_FDT_IRQ_TYPE_SPI, irq, 787 GIC_FDT_IRQ_FLAGS_LEVEL_HI); 788 qemu_fdt_setprop_cell(vms->fdt, nodename, "clocks", vms->clock_phandle); 789 qemu_fdt_setprop_string(vms->fdt, nodename, "clock-names", "apb_pclk"); 790 g_free(nodename); 791 } 792 793 static DeviceState *gpio_key_dev; 794 static void virt_powerdown_req(Notifier *n, void *opaque) 795 { 796 VirtMachineState *s = container_of(n, VirtMachineState, powerdown_notifier); 797 798 if (s->acpi_dev) { 799 acpi_send_event(s->acpi_dev, ACPI_POWER_DOWN_STATUS); 800 } else { 801 /* use gpio Pin 3 for power button event */ 802 qemu_set_irq(qdev_get_gpio_in(gpio_key_dev, 0), 1); 803 } 804 } 805 806 static void create_gpio(const VirtMachineState *vms) 807 { 808 char *nodename; 809 DeviceState *pl061_dev; 810 hwaddr base = vms->memmap[VIRT_GPIO].base; 811 hwaddr size = vms->memmap[VIRT_GPIO].size; 812 int irq = vms->irqmap[VIRT_GPIO]; 813 const char compat[] = "arm,pl061\0arm,primecell"; 814 815 pl061_dev = sysbus_create_simple("pl061", base, 816 qdev_get_gpio_in(vms->gic, irq)); 817 818 uint32_t phandle = qemu_fdt_alloc_phandle(vms->fdt); 819 nodename = g_strdup_printf("/pl061@%" PRIx64, base); 820 qemu_fdt_add_subnode(vms->fdt, nodename); 821 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg", 822 2, base, 2, size); 823 qemu_fdt_setprop(vms->fdt, nodename, "compatible", compat, sizeof(compat)); 824 qemu_fdt_setprop_cell(vms->fdt, nodename, "#gpio-cells", 2); 825 qemu_fdt_setprop(vms->fdt, nodename, "gpio-controller", NULL, 0); 826 qemu_fdt_setprop_cells(vms->fdt, nodename, "interrupts", 827 GIC_FDT_IRQ_TYPE_SPI, irq, 828 GIC_FDT_IRQ_FLAGS_LEVEL_HI); 829 qemu_fdt_setprop_cell(vms->fdt, nodename, "clocks", vms->clock_phandle); 830 qemu_fdt_setprop_string(vms->fdt, nodename, "clock-names", "apb_pclk"); 831 qemu_fdt_setprop_cell(vms->fdt, nodename, "phandle", phandle); 832 833 gpio_key_dev = sysbus_create_simple("gpio-key", -1, 834 qdev_get_gpio_in(pl061_dev, 3)); 835 qemu_fdt_add_subnode(vms->fdt, "/gpio-keys"); 836 qemu_fdt_setprop_string(vms->fdt, "/gpio-keys", "compatible", "gpio-keys"); 837 qemu_fdt_setprop_cell(vms->fdt, "/gpio-keys", "#size-cells", 0); 838 qemu_fdt_setprop_cell(vms->fdt, "/gpio-keys", "#address-cells", 1); 839 840 qemu_fdt_add_subnode(vms->fdt, "/gpio-keys/poweroff"); 841 qemu_fdt_setprop_string(vms->fdt, "/gpio-keys/poweroff", 842 "label", "GPIO Key Poweroff"); 843 qemu_fdt_setprop_cell(vms->fdt, "/gpio-keys/poweroff", "linux,code", 844 KEY_POWER); 845 qemu_fdt_setprop_cells(vms->fdt, "/gpio-keys/poweroff", 846 "gpios", phandle, 3, 0); 847 g_free(nodename); 848 } 849 850 static void create_virtio_devices(const VirtMachineState *vms) 851 { 852 int i; 853 hwaddr size = vms->memmap[VIRT_MMIO].size; 854 855 /* We create the transports in forwards order. Since qbus_realize() 856 * prepends (not appends) new child buses, the incrementing loop below will 857 * create a list of virtio-mmio buses with decreasing base addresses. 858 * 859 * When a -device option is processed from the command line, 860 * qbus_find_recursive() picks the next free virtio-mmio bus in forwards 861 * order. The upshot is that -device options in increasing command line 862 * order are mapped to virtio-mmio buses with decreasing base addresses. 863 * 864 * When this code was originally written, that arrangement ensured that the 865 * guest Linux kernel would give the lowest "name" (/dev/vda, eth0, etc) to 866 * the first -device on the command line. (The end-to-end order is a 867 * function of this loop, qbus_realize(), qbus_find_recursive(), and the 868 * guest kernel's name-to-address assignment strategy.) 869 * 870 * Meanwhile, the kernel's traversal seems to have been reversed; see eg. 871 * the message, if not necessarily the code, of commit 70161ff336. 872 * Therefore the loop now establishes the inverse of the original intent. 873 * 874 * Unfortunately, we can't counteract the kernel change by reversing the 875 * loop; it would break existing command lines. 876 * 877 * In any case, the kernel makes no guarantee about the stability of 878 * enumeration order of virtio devices (as demonstrated by it changing 879 * between kernel versions). For reliable and stable identification 880 * of disks users must use UUIDs or similar mechanisms. 881 */ 882 for (i = 0; i < NUM_VIRTIO_TRANSPORTS; i++) { 883 int irq = vms->irqmap[VIRT_MMIO] + i; 884 hwaddr base = vms->memmap[VIRT_MMIO].base + i * size; 885 886 sysbus_create_simple("virtio-mmio", base, 887 qdev_get_gpio_in(vms->gic, irq)); 888 } 889 890 /* We add dtb nodes in reverse order so that they appear in the finished 891 * device tree lowest address first. 892 * 893 * Note that this mapping is independent of the loop above. The previous 894 * loop influences virtio device to virtio transport assignment, whereas 895 * this loop controls how virtio transports are laid out in the dtb. 896 */ 897 for (i = NUM_VIRTIO_TRANSPORTS - 1; i >= 0; i--) { 898 char *nodename; 899 int irq = vms->irqmap[VIRT_MMIO] + i; 900 hwaddr base = vms->memmap[VIRT_MMIO].base + i * size; 901 902 nodename = g_strdup_printf("/virtio_mmio@%" PRIx64, base); 903 qemu_fdt_add_subnode(vms->fdt, nodename); 904 qemu_fdt_setprop_string(vms->fdt, nodename, 905 "compatible", "virtio,mmio"); 906 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg", 907 2, base, 2, size); 908 qemu_fdt_setprop_cells(vms->fdt, nodename, "interrupts", 909 GIC_FDT_IRQ_TYPE_SPI, irq, 910 GIC_FDT_IRQ_FLAGS_EDGE_LO_HI); 911 qemu_fdt_setprop(vms->fdt, nodename, "dma-coherent", NULL, 0); 912 g_free(nodename); 913 } 914 } 915 916 #define VIRT_FLASH_SECTOR_SIZE (256 * KiB) 917 918 static PFlashCFI01 *virt_flash_create1(VirtMachineState *vms, 919 const char *name, 920 const char *alias_prop_name) 921 { 922 /* 923 * Create a single flash device. We use the same parameters as 924 * the flash devices on the Versatile Express board. 925 */ 926 DeviceState *dev = qdev_create(NULL, TYPE_PFLASH_CFI01); 927 928 qdev_prop_set_uint64(dev, "sector-length", VIRT_FLASH_SECTOR_SIZE); 929 qdev_prop_set_uint8(dev, "width", 4); 930 qdev_prop_set_uint8(dev, "device-width", 2); 931 qdev_prop_set_bit(dev, "big-endian", false); 932 qdev_prop_set_uint16(dev, "id0", 0x89); 933 qdev_prop_set_uint16(dev, "id1", 0x18); 934 qdev_prop_set_uint16(dev, "id2", 0x00); 935 qdev_prop_set_uint16(dev, "id3", 0x00); 936 qdev_prop_set_string(dev, "name", name); 937 object_property_add_child(OBJECT(vms), name, OBJECT(dev), 938 &error_abort); 939 object_property_add_alias(OBJECT(vms), alias_prop_name, 940 OBJECT(dev), "drive", &error_abort); 941 return PFLASH_CFI01(dev); 942 } 943 944 static void virt_flash_create(VirtMachineState *vms) 945 { 946 vms->flash[0] = virt_flash_create1(vms, "virt.flash0", "pflash0"); 947 vms->flash[1] = virt_flash_create1(vms, "virt.flash1", "pflash1"); 948 } 949 950 static void virt_flash_map1(PFlashCFI01 *flash, 951 hwaddr base, hwaddr size, 952 MemoryRegion *sysmem) 953 { 954 DeviceState *dev = DEVICE(flash); 955 956 assert(size % VIRT_FLASH_SECTOR_SIZE == 0); 957 assert(size / VIRT_FLASH_SECTOR_SIZE <= UINT32_MAX); 958 qdev_prop_set_uint32(dev, "num-blocks", size / VIRT_FLASH_SECTOR_SIZE); 959 qdev_init_nofail(dev); 960 961 memory_region_add_subregion(sysmem, base, 962 sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 963 0)); 964 } 965 966 static void virt_flash_map(VirtMachineState *vms, 967 MemoryRegion *sysmem, 968 MemoryRegion *secure_sysmem) 969 { 970 /* 971 * Map two flash devices to fill the VIRT_FLASH space in the memmap. 972 * sysmem is the system memory space. secure_sysmem is the secure view 973 * of the system, and the first flash device should be made visible only 974 * there. The second flash device is visible to both secure and nonsecure. 975 * If sysmem == secure_sysmem this means there is no separate Secure 976 * address space and both flash devices are generally visible. 977 */ 978 hwaddr flashsize = vms->memmap[VIRT_FLASH].size / 2; 979 hwaddr flashbase = vms->memmap[VIRT_FLASH].base; 980 981 virt_flash_map1(vms->flash[0], flashbase, flashsize, 982 secure_sysmem); 983 virt_flash_map1(vms->flash[1], flashbase + flashsize, flashsize, 984 sysmem); 985 } 986 987 static void virt_flash_fdt(VirtMachineState *vms, 988 MemoryRegion *sysmem, 989 MemoryRegion *secure_sysmem) 990 { 991 hwaddr flashsize = vms->memmap[VIRT_FLASH].size / 2; 992 hwaddr flashbase = vms->memmap[VIRT_FLASH].base; 993 char *nodename; 994 995 if (sysmem == secure_sysmem) { 996 /* Report both flash devices as a single node in the DT */ 997 nodename = g_strdup_printf("/flash@%" PRIx64, flashbase); 998 qemu_fdt_add_subnode(vms->fdt, nodename); 999 qemu_fdt_setprop_string(vms->fdt, nodename, "compatible", "cfi-flash"); 1000 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg", 1001 2, flashbase, 2, flashsize, 1002 2, flashbase + flashsize, 2, flashsize); 1003 qemu_fdt_setprop_cell(vms->fdt, nodename, "bank-width", 4); 1004 g_free(nodename); 1005 } else { 1006 /* 1007 * Report the devices as separate nodes so we can mark one as 1008 * only visible to the secure world. 1009 */ 1010 nodename = g_strdup_printf("/secflash@%" PRIx64, flashbase); 1011 qemu_fdt_add_subnode(vms->fdt, nodename); 1012 qemu_fdt_setprop_string(vms->fdt, nodename, "compatible", "cfi-flash"); 1013 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg", 1014 2, flashbase, 2, flashsize); 1015 qemu_fdt_setprop_cell(vms->fdt, nodename, "bank-width", 4); 1016 qemu_fdt_setprop_string(vms->fdt, nodename, "status", "disabled"); 1017 qemu_fdt_setprop_string(vms->fdt, nodename, "secure-status", "okay"); 1018 g_free(nodename); 1019 1020 nodename = g_strdup_printf("/flash@%" PRIx64, flashbase); 1021 qemu_fdt_add_subnode(vms->fdt, nodename); 1022 qemu_fdt_setprop_string(vms->fdt, nodename, "compatible", "cfi-flash"); 1023 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg", 1024 2, flashbase + flashsize, 2, flashsize); 1025 qemu_fdt_setprop_cell(vms->fdt, nodename, "bank-width", 4); 1026 g_free(nodename); 1027 } 1028 } 1029 1030 static bool virt_firmware_init(VirtMachineState *vms, 1031 MemoryRegion *sysmem, 1032 MemoryRegion *secure_sysmem) 1033 { 1034 int i; 1035 BlockBackend *pflash_blk0; 1036 1037 /* Map legacy -drive if=pflash to machine properties */ 1038 for (i = 0; i < ARRAY_SIZE(vms->flash); i++) { 1039 pflash_cfi01_legacy_drive(vms->flash[i], 1040 drive_get(IF_PFLASH, 0, i)); 1041 } 1042 1043 virt_flash_map(vms, sysmem, secure_sysmem); 1044 1045 pflash_blk0 = pflash_cfi01_get_blk(vms->flash[0]); 1046 1047 if (bios_name) { 1048 char *fname; 1049 MemoryRegion *mr; 1050 int image_size; 1051 1052 if (pflash_blk0) { 1053 error_report("The contents of the first flash device may be " 1054 "specified with -bios or with -drive if=pflash... " 1055 "but you cannot use both options at once"); 1056 exit(1); 1057 } 1058 1059 /* Fall back to -bios */ 1060 1061 fname = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name); 1062 if (!fname) { 1063 error_report("Could not find ROM image '%s'", bios_name); 1064 exit(1); 1065 } 1066 mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(vms->flash[0]), 0); 1067 image_size = load_image_mr(fname, mr); 1068 g_free(fname); 1069 if (image_size < 0) { 1070 error_report("Could not load ROM image '%s'", bios_name); 1071 exit(1); 1072 } 1073 } 1074 1075 return pflash_blk0 || bios_name; 1076 } 1077 1078 static FWCfgState *create_fw_cfg(const VirtMachineState *vms, AddressSpace *as) 1079 { 1080 MachineState *ms = MACHINE(vms); 1081 hwaddr base = vms->memmap[VIRT_FW_CFG].base; 1082 hwaddr size = vms->memmap[VIRT_FW_CFG].size; 1083 FWCfgState *fw_cfg; 1084 char *nodename; 1085 1086 fw_cfg = fw_cfg_init_mem_wide(base + 8, base, 8, base + 16, as); 1087 fw_cfg_add_i16(fw_cfg, FW_CFG_NB_CPUS, (uint16_t)ms->smp.cpus); 1088 1089 nodename = g_strdup_printf("/fw-cfg@%" PRIx64, base); 1090 qemu_fdt_add_subnode(vms->fdt, nodename); 1091 qemu_fdt_setprop_string(vms->fdt, nodename, 1092 "compatible", "qemu,fw-cfg-mmio"); 1093 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg", 1094 2, base, 2, size); 1095 qemu_fdt_setprop(vms->fdt, nodename, "dma-coherent", NULL, 0); 1096 g_free(nodename); 1097 return fw_cfg; 1098 } 1099 1100 static void create_pcie_irq_map(const VirtMachineState *vms, 1101 uint32_t gic_phandle, 1102 int first_irq, const char *nodename) 1103 { 1104 int devfn, pin; 1105 uint32_t full_irq_map[4 * 4 * 10] = { 0 }; 1106 uint32_t *irq_map = full_irq_map; 1107 1108 for (devfn = 0; devfn <= 0x18; devfn += 0x8) { 1109 for (pin = 0; pin < 4; pin++) { 1110 int irq_type = GIC_FDT_IRQ_TYPE_SPI; 1111 int irq_nr = first_irq + ((pin + PCI_SLOT(devfn)) % PCI_NUM_PINS); 1112 int irq_level = GIC_FDT_IRQ_FLAGS_LEVEL_HI; 1113 int i; 1114 1115 uint32_t map[] = { 1116 devfn << 8, 0, 0, /* devfn */ 1117 pin + 1, /* PCI pin */ 1118 gic_phandle, 0, 0, irq_type, irq_nr, irq_level }; /* GIC irq */ 1119 1120 /* Convert map to big endian */ 1121 for (i = 0; i < 10; i++) { 1122 irq_map[i] = cpu_to_be32(map[i]); 1123 } 1124 irq_map += 10; 1125 } 1126 } 1127 1128 qemu_fdt_setprop(vms->fdt, nodename, "interrupt-map", 1129 full_irq_map, sizeof(full_irq_map)); 1130 1131 qemu_fdt_setprop_cells(vms->fdt, nodename, "interrupt-map-mask", 1132 0x1800, 0, 0, /* devfn (PCI_SLOT(3)) */ 1133 0x7 /* PCI irq */); 1134 } 1135 1136 static void create_smmu(const VirtMachineState *vms, 1137 PCIBus *bus) 1138 { 1139 char *node; 1140 const char compat[] = "arm,smmu-v3"; 1141 int irq = vms->irqmap[VIRT_SMMU]; 1142 int i; 1143 hwaddr base = vms->memmap[VIRT_SMMU].base; 1144 hwaddr size = vms->memmap[VIRT_SMMU].size; 1145 const char irq_names[] = "eventq\0priq\0cmdq-sync\0gerror"; 1146 DeviceState *dev; 1147 1148 if (vms->iommu != VIRT_IOMMU_SMMUV3 || !vms->iommu_phandle) { 1149 return; 1150 } 1151 1152 dev = qdev_create(NULL, "arm-smmuv3"); 1153 1154 object_property_set_link(OBJECT(dev), OBJECT(bus), "primary-bus", 1155 &error_abort); 1156 qdev_init_nofail(dev); 1157 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base); 1158 for (i = 0; i < NUM_SMMU_IRQS; i++) { 1159 sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, 1160 qdev_get_gpio_in(vms->gic, irq + i)); 1161 } 1162 1163 node = g_strdup_printf("/smmuv3@%" PRIx64, base); 1164 qemu_fdt_add_subnode(vms->fdt, node); 1165 qemu_fdt_setprop(vms->fdt, node, "compatible", compat, sizeof(compat)); 1166 qemu_fdt_setprop_sized_cells(vms->fdt, node, "reg", 2, base, 2, size); 1167 1168 qemu_fdt_setprop_cells(vms->fdt, node, "interrupts", 1169 GIC_FDT_IRQ_TYPE_SPI, irq , GIC_FDT_IRQ_FLAGS_EDGE_LO_HI, 1170 GIC_FDT_IRQ_TYPE_SPI, irq + 1, GIC_FDT_IRQ_FLAGS_EDGE_LO_HI, 1171 GIC_FDT_IRQ_TYPE_SPI, irq + 2, GIC_FDT_IRQ_FLAGS_EDGE_LO_HI, 1172 GIC_FDT_IRQ_TYPE_SPI, irq + 3, GIC_FDT_IRQ_FLAGS_EDGE_LO_HI); 1173 1174 qemu_fdt_setprop(vms->fdt, node, "interrupt-names", irq_names, 1175 sizeof(irq_names)); 1176 1177 qemu_fdt_setprop_cell(vms->fdt, node, "clocks", vms->clock_phandle); 1178 qemu_fdt_setprop_string(vms->fdt, node, "clock-names", "apb_pclk"); 1179 qemu_fdt_setprop(vms->fdt, node, "dma-coherent", NULL, 0); 1180 1181 qemu_fdt_setprop_cell(vms->fdt, node, "#iommu-cells", 1); 1182 1183 qemu_fdt_setprop_cell(vms->fdt, node, "phandle", vms->iommu_phandle); 1184 g_free(node); 1185 } 1186 1187 static void create_virtio_iommu_dt_bindings(VirtMachineState *vms, Error **errp) 1188 { 1189 const char compat[] = "virtio,pci-iommu"; 1190 uint16_t bdf = vms->virtio_iommu_bdf; 1191 char *node; 1192 1193 vms->iommu_phandle = qemu_fdt_alloc_phandle(vms->fdt); 1194 1195 node = g_strdup_printf("%s/virtio_iommu@%d", vms->pciehb_nodename, bdf); 1196 qemu_fdt_add_subnode(vms->fdt, node); 1197 qemu_fdt_setprop(vms->fdt, node, "compatible", compat, sizeof(compat)); 1198 qemu_fdt_setprop_sized_cells(vms->fdt, node, "reg", 1199 1, bdf << 8, 1, 0, 1, 0, 1200 1, 0, 1, 0); 1201 1202 qemu_fdt_setprop_cell(vms->fdt, node, "#iommu-cells", 1); 1203 qemu_fdt_setprop_cell(vms->fdt, node, "phandle", vms->iommu_phandle); 1204 g_free(node); 1205 1206 qemu_fdt_setprop_cells(vms->fdt, vms->pciehb_nodename, "iommu-map", 1207 0x0, vms->iommu_phandle, 0x0, bdf, 1208 bdf + 1, vms->iommu_phandle, bdf + 1, 0xffff - bdf); 1209 } 1210 1211 static void create_pcie(VirtMachineState *vms) 1212 { 1213 hwaddr base_mmio = vms->memmap[VIRT_PCIE_MMIO].base; 1214 hwaddr size_mmio = vms->memmap[VIRT_PCIE_MMIO].size; 1215 hwaddr base_mmio_high = vms->memmap[VIRT_HIGH_PCIE_MMIO].base; 1216 hwaddr size_mmio_high = vms->memmap[VIRT_HIGH_PCIE_MMIO].size; 1217 hwaddr base_pio = vms->memmap[VIRT_PCIE_PIO].base; 1218 hwaddr size_pio = vms->memmap[VIRT_PCIE_PIO].size; 1219 hwaddr base_ecam, size_ecam; 1220 hwaddr base = base_mmio; 1221 int nr_pcie_buses; 1222 int irq = vms->irqmap[VIRT_PCIE]; 1223 MemoryRegion *mmio_alias; 1224 MemoryRegion *mmio_reg; 1225 MemoryRegion *ecam_alias; 1226 MemoryRegion *ecam_reg; 1227 DeviceState *dev; 1228 char *nodename; 1229 int i, ecam_id; 1230 PCIHostState *pci; 1231 1232 dev = qdev_create(NULL, TYPE_GPEX_HOST); 1233 qdev_init_nofail(dev); 1234 1235 ecam_id = VIRT_ECAM_ID(vms->highmem_ecam); 1236 base_ecam = vms->memmap[ecam_id].base; 1237 size_ecam = vms->memmap[ecam_id].size; 1238 nr_pcie_buses = size_ecam / PCIE_MMCFG_SIZE_MIN; 1239 /* Map only the first size_ecam bytes of ECAM space */ 1240 ecam_alias = g_new0(MemoryRegion, 1); 1241 ecam_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0); 1242 memory_region_init_alias(ecam_alias, OBJECT(dev), "pcie-ecam", 1243 ecam_reg, 0, size_ecam); 1244 memory_region_add_subregion(get_system_memory(), base_ecam, ecam_alias); 1245 1246 /* Map the MMIO window into system address space so as to expose 1247 * the section of PCI MMIO space which starts at the same base address 1248 * (ie 1:1 mapping for that part of PCI MMIO space visible through 1249 * the window). 1250 */ 1251 mmio_alias = g_new0(MemoryRegion, 1); 1252 mmio_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 1); 1253 memory_region_init_alias(mmio_alias, OBJECT(dev), "pcie-mmio", 1254 mmio_reg, base_mmio, size_mmio); 1255 memory_region_add_subregion(get_system_memory(), base_mmio, mmio_alias); 1256 1257 if (vms->highmem) { 1258 /* Map high MMIO space */ 1259 MemoryRegion *high_mmio_alias = g_new0(MemoryRegion, 1); 1260 1261 memory_region_init_alias(high_mmio_alias, OBJECT(dev), "pcie-mmio-high", 1262 mmio_reg, base_mmio_high, size_mmio_high); 1263 memory_region_add_subregion(get_system_memory(), base_mmio_high, 1264 high_mmio_alias); 1265 } 1266 1267 /* Map IO port space */ 1268 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 2, base_pio); 1269 1270 for (i = 0; i < GPEX_NUM_IRQS; i++) { 1271 sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, 1272 qdev_get_gpio_in(vms->gic, irq + i)); 1273 gpex_set_irq_num(GPEX_HOST(dev), i, irq + i); 1274 } 1275 1276 pci = PCI_HOST_BRIDGE(dev); 1277 if (pci->bus) { 1278 for (i = 0; i < nb_nics; i++) { 1279 NICInfo *nd = &nd_table[i]; 1280 1281 if (!nd->model) { 1282 nd->model = g_strdup("virtio"); 1283 } 1284 1285 pci_nic_init_nofail(nd, pci->bus, nd->model, NULL); 1286 } 1287 } 1288 1289 nodename = vms->pciehb_nodename = g_strdup_printf("/pcie@%" PRIx64, base); 1290 qemu_fdt_add_subnode(vms->fdt, nodename); 1291 qemu_fdt_setprop_string(vms->fdt, nodename, 1292 "compatible", "pci-host-ecam-generic"); 1293 qemu_fdt_setprop_string(vms->fdt, nodename, "device_type", "pci"); 1294 qemu_fdt_setprop_cell(vms->fdt, nodename, "#address-cells", 3); 1295 qemu_fdt_setprop_cell(vms->fdt, nodename, "#size-cells", 2); 1296 qemu_fdt_setprop_cell(vms->fdt, nodename, "linux,pci-domain", 0); 1297 qemu_fdt_setprop_cells(vms->fdt, nodename, "bus-range", 0, 1298 nr_pcie_buses - 1); 1299 qemu_fdt_setprop(vms->fdt, nodename, "dma-coherent", NULL, 0); 1300 1301 if (vms->msi_phandle) { 1302 qemu_fdt_setprop_cells(vms->fdt, nodename, "msi-parent", 1303 vms->msi_phandle); 1304 } 1305 1306 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg", 1307 2, base_ecam, 2, size_ecam); 1308 1309 if (vms->highmem) { 1310 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "ranges", 1311 1, FDT_PCI_RANGE_IOPORT, 2, 0, 1312 2, base_pio, 2, size_pio, 1313 1, FDT_PCI_RANGE_MMIO, 2, base_mmio, 1314 2, base_mmio, 2, size_mmio, 1315 1, FDT_PCI_RANGE_MMIO_64BIT, 1316 2, base_mmio_high, 1317 2, base_mmio_high, 2, size_mmio_high); 1318 } else { 1319 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "ranges", 1320 1, FDT_PCI_RANGE_IOPORT, 2, 0, 1321 2, base_pio, 2, size_pio, 1322 1, FDT_PCI_RANGE_MMIO, 2, base_mmio, 1323 2, base_mmio, 2, size_mmio); 1324 } 1325 1326 qemu_fdt_setprop_cell(vms->fdt, nodename, "#interrupt-cells", 1); 1327 create_pcie_irq_map(vms, vms->gic_phandle, irq, nodename); 1328 1329 if (vms->iommu) { 1330 vms->iommu_phandle = qemu_fdt_alloc_phandle(vms->fdt); 1331 1332 switch (vms->iommu) { 1333 case VIRT_IOMMU_SMMUV3: 1334 create_smmu(vms, pci->bus); 1335 qemu_fdt_setprop_cells(vms->fdt, nodename, "iommu-map", 1336 0x0, vms->iommu_phandle, 0x0, 0x10000); 1337 break; 1338 default: 1339 g_assert_not_reached(); 1340 } 1341 } 1342 } 1343 1344 static void create_platform_bus(VirtMachineState *vms) 1345 { 1346 DeviceState *dev; 1347 SysBusDevice *s; 1348 int i; 1349 MemoryRegion *sysmem = get_system_memory(); 1350 1351 dev = qdev_create(NULL, TYPE_PLATFORM_BUS_DEVICE); 1352 dev->id = TYPE_PLATFORM_BUS_DEVICE; 1353 qdev_prop_set_uint32(dev, "num_irqs", PLATFORM_BUS_NUM_IRQS); 1354 qdev_prop_set_uint32(dev, "mmio_size", vms->memmap[VIRT_PLATFORM_BUS].size); 1355 qdev_init_nofail(dev); 1356 vms->platform_bus_dev = dev; 1357 1358 s = SYS_BUS_DEVICE(dev); 1359 for (i = 0; i < PLATFORM_BUS_NUM_IRQS; i++) { 1360 int irq = vms->irqmap[VIRT_PLATFORM_BUS] + i; 1361 sysbus_connect_irq(s, i, qdev_get_gpio_in(vms->gic, irq)); 1362 } 1363 1364 memory_region_add_subregion(sysmem, 1365 vms->memmap[VIRT_PLATFORM_BUS].base, 1366 sysbus_mmio_get_region(s, 0)); 1367 } 1368 1369 static void create_secure_ram(VirtMachineState *vms, 1370 MemoryRegion *secure_sysmem) 1371 { 1372 MemoryRegion *secram = g_new(MemoryRegion, 1); 1373 char *nodename; 1374 hwaddr base = vms->memmap[VIRT_SECURE_MEM].base; 1375 hwaddr size = vms->memmap[VIRT_SECURE_MEM].size; 1376 1377 memory_region_init_ram(secram, NULL, "virt.secure-ram", size, 1378 &error_fatal); 1379 memory_region_add_subregion(secure_sysmem, base, secram); 1380 1381 nodename = g_strdup_printf("/secram@%" PRIx64, base); 1382 qemu_fdt_add_subnode(vms->fdt, nodename); 1383 qemu_fdt_setprop_string(vms->fdt, nodename, "device_type", "memory"); 1384 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg", 2, base, 2, size); 1385 qemu_fdt_setprop_string(vms->fdt, nodename, "status", "disabled"); 1386 qemu_fdt_setprop_string(vms->fdt, nodename, "secure-status", "okay"); 1387 1388 g_free(nodename); 1389 } 1390 1391 static void *machvirt_dtb(const struct arm_boot_info *binfo, int *fdt_size) 1392 { 1393 const VirtMachineState *board = container_of(binfo, VirtMachineState, 1394 bootinfo); 1395 1396 *fdt_size = board->fdt_size; 1397 return board->fdt; 1398 } 1399 1400 static void virt_build_smbios(VirtMachineState *vms) 1401 { 1402 MachineClass *mc = MACHINE_GET_CLASS(vms); 1403 VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms); 1404 uint8_t *smbios_tables, *smbios_anchor; 1405 size_t smbios_tables_len, smbios_anchor_len; 1406 const char *product = "QEMU Virtual Machine"; 1407 1408 if (kvm_enabled()) { 1409 product = "KVM Virtual Machine"; 1410 } 1411 1412 smbios_set_defaults("QEMU", product, 1413 vmc->smbios_old_sys_ver ? "1.0" : mc->name, false, 1414 true, SMBIOS_ENTRY_POINT_30); 1415 1416 smbios_get_tables(MACHINE(vms), NULL, 0, &smbios_tables, &smbios_tables_len, 1417 &smbios_anchor, &smbios_anchor_len); 1418 1419 if (smbios_anchor) { 1420 fw_cfg_add_file(vms->fw_cfg, "etc/smbios/smbios-tables", 1421 smbios_tables, smbios_tables_len); 1422 fw_cfg_add_file(vms->fw_cfg, "etc/smbios/smbios-anchor", 1423 smbios_anchor, smbios_anchor_len); 1424 } 1425 } 1426 1427 static 1428 void virt_machine_done(Notifier *notifier, void *data) 1429 { 1430 VirtMachineState *vms = container_of(notifier, VirtMachineState, 1431 machine_done); 1432 MachineState *ms = MACHINE(vms); 1433 ARMCPU *cpu = ARM_CPU(first_cpu); 1434 struct arm_boot_info *info = &vms->bootinfo; 1435 AddressSpace *as = arm_boot_address_space(cpu, info); 1436 1437 /* 1438 * If the user provided a dtb, we assume the dynamic sysbus nodes 1439 * already are integrated there. This corresponds to a use case where 1440 * the dynamic sysbus nodes are complex and their generation is not yet 1441 * supported. In that case the user can take charge of the guest dt 1442 * while qemu takes charge of the qom stuff. 1443 */ 1444 if (info->dtb_filename == NULL) { 1445 platform_bus_add_all_fdt_nodes(vms->fdt, "/intc", 1446 vms->memmap[VIRT_PLATFORM_BUS].base, 1447 vms->memmap[VIRT_PLATFORM_BUS].size, 1448 vms->irqmap[VIRT_PLATFORM_BUS]); 1449 } 1450 if (arm_load_dtb(info->dtb_start, info, info->dtb_limit, as, ms) < 0) { 1451 exit(1); 1452 } 1453 1454 virt_acpi_setup(vms); 1455 virt_build_smbios(vms); 1456 } 1457 1458 static uint64_t virt_cpu_mp_affinity(VirtMachineState *vms, int idx) 1459 { 1460 uint8_t clustersz = ARM_DEFAULT_CPUS_PER_CLUSTER; 1461 VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms); 1462 1463 if (!vmc->disallow_affinity_adjustment) { 1464 /* Adjust MPIDR like 64-bit KVM hosts, which incorporate the 1465 * GIC's target-list limitations. 32-bit KVM hosts currently 1466 * always create clusters of 4 CPUs, but that is expected to 1467 * change when they gain support for gicv3. When KVM is enabled 1468 * it will override the changes we make here, therefore our 1469 * purposes are to make TCG consistent (with 64-bit KVM hosts) 1470 * and to improve SGI efficiency. 1471 */ 1472 if (vms->gic_version == 3) { 1473 clustersz = GICV3_TARGETLIST_BITS; 1474 } else { 1475 clustersz = GIC_TARGETLIST_BITS; 1476 } 1477 } 1478 return arm_cpu_mp_affinity(idx, clustersz); 1479 } 1480 1481 static void virt_set_memmap(VirtMachineState *vms) 1482 { 1483 MachineState *ms = MACHINE(vms); 1484 hwaddr base, device_memory_base, device_memory_size; 1485 int i; 1486 1487 vms->memmap = extended_memmap; 1488 1489 for (i = 0; i < ARRAY_SIZE(base_memmap); i++) { 1490 vms->memmap[i] = base_memmap[i]; 1491 } 1492 1493 if (ms->ram_slots > ACPI_MAX_RAM_SLOTS) { 1494 error_report("unsupported number of memory slots: %"PRIu64, 1495 ms->ram_slots); 1496 exit(EXIT_FAILURE); 1497 } 1498 1499 /* 1500 * We compute the base of the high IO region depending on the 1501 * amount of initial and device memory. The device memory start/size 1502 * is aligned on 1GiB. We never put the high IO region below 256GiB 1503 * so that if maxram_size is < 255GiB we keep the legacy memory map. 1504 * The device region size assumes 1GiB page max alignment per slot. 1505 */ 1506 device_memory_base = 1507 ROUND_UP(vms->memmap[VIRT_MEM].base + ms->ram_size, GiB); 1508 device_memory_size = ms->maxram_size - ms->ram_size + ms->ram_slots * GiB; 1509 1510 /* Base address of the high IO region */ 1511 base = device_memory_base + ROUND_UP(device_memory_size, GiB); 1512 if (base < device_memory_base) { 1513 error_report("maxmem/slots too huge"); 1514 exit(EXIT_FAILURE); 1515 } 1516 if (base < vms->memmap[VIRT_MEM].base + LEGACY_RAMLIMIT_BYTES) { 1517 base = vms->memmap[VIRT_MEM].base + LEGACY_RAMLIMIT_BYTES; 1518 } 1519 1520 for (i = VIRT_LOWMEMMAP_LAST; i < ARRAY_SIZE(extended_memmap); i++) { 1521 hwaddr size = extended_memmap[i].size; 1522 1523 base = ROUND_UP(base, size); 1524 vms->memmap[i].base = base; 1525 vms->memmap[i].size = size; 1526 base += size; 1527 } 1528 vms->highest_gpa = base - 1; 1529 if (device_memory_size > 0) { 1530 ms->device_memory = g_malloc0(sizeof(*ms->device_memory)); 1531 ms->device_memory->base = device_memory_base; 1532 memory_region_init(&ms->device_memory->mr, OBJECT(vms), 1533 "device-memory", device_memory_size); 1534 } 1535 } 1536 1537 static void machvirt_init(MachineState *machine) 1538 { 1539 VirtMachineState *vms = VIRT_MACHINE(machine); 1540 VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(machine); 1541 MachineClass *mc = MACHINE_GET_CLASS(machine); 1542 const CPUArchIdList *possible_cpus; 1543 MemoryRegion *sysmem = get_system_memory(); 1544 MemoryRegion *secure_sysmem = NULL; 1545 int n, virt_max_cpus; 1546 bool firmware_loaded; 1547 bool aarch64 = true; 1548 bool has_ged = !vmc->no_ged; 1549 unsigned int smp_cpus = machine->smp.cpus; 1550 unsigned int max_cpus = machine->smp.max_cpus; 1551 1552 /* 1553 * In accelerated mode, the memory map is computed earlier in kvm_type() 1554 * to create a VM with the right number of IPA bits. 1555 */ 1556 if (!vms->memmap) { 1557 virt_set_memmap(vms); 1558 } 1559 1560 /* We can probe only here because during property set 1561 * KVM is not available yet 1562 */ 1563 if (vms->gic_version <= 0) { 1564 /* "host" or "max" */ 1565 if (!kvm_enabled()) { 1566 if (vms->gic_version == 0) { 1567 error_report("gic-version=host requires KVM"); 1568 exit(1); 1569 } else { 1570 /* "max": currently means 3 for TCG */ 1571 vms->gic_version = 3; 1572 } 1573 } else { 1574 vms->gic_version = kvm_arm_vgic_probe(); 1575 if (!vms->gic_version) { 1576 error_report( 1577 "Unable to determine GIC version supported by host"); 1578 exit(1); 1579 } 1580 } 1581 } 1582 1583 if (!cpu_type_valid(machine->cpu_type)) { 1584 error_report("mach-virt: CPU type %s not supported", machine->cpu_type); 1585 exit(1); 1586 } 1587 1588 if (vms->secure) { 1589 if (kvm_enabled()) { 1590 error_report("mach-virt: KVM does not support Security extensions"); 1591 exit(1); 1592 } 1593 1594 /* 1595 * The Secure view of the world is the same as the NonSecure, 1596 * but with a few extra devices. Create it as a container region 1597 * containing the system memory at low priority; any secure-only 1598 * devices go in at higher priority and take precedence. 1599 */ 1600 secure_sysmem = g_new(MemoryRegion, 1); 1601 memory_region_init(secure_sysmem, OBJECT(machine), "secure-memory", 1602 UINT64_MAX); 1603 memory_region_add_subregion_overlap(secure_sysmem, 0, sysmem, -1); 1604 } 1605 1606 firmware_loaded = virt_firmware_init(vms, sysmem, 1607 secure_sysmem ?: sysmem); 1608 1609 /* If we have an EL3 boot ROM then the assumption is that it will 1610 * implement PSCI itself, so disable QEMU's internal implementation 1611 * so it doesn't get in the way. Instead of starting secondary 1612 * CPUs in PSCI powerdown state we will start them all running and 1613 * let the boot ROM sort them out. 1614 * The usual case is that we do use QEMU's PSCI implementation; 1615 * if the guest has EL2 then we will use SMC as the conduit, 1616 * and otherwise we will use HVC (for backwards compatibility and 1617 * because if we're using KVM then we must use HVC). 1618 */ 1619 if (vms->secure && firmware_loaded) { 1620 vms->psci_conduit = QEMU_PSCI_CONDUIT_DISABLED; 1621 } else if (vms->virt) { 1622 vms->psci_conduit = QEMU_PSCI_CONDUIT_SMC; 1623 } else { 1624 vms->psci_conduit = QEMU_PSCI_CONDUIT_HVC; 1625 } 1626 1627 /* The maximum number of CPUs depends on the GIC version, or on how 1628 * many redistributors we can fit into the memory map. 1629 */ 1630 if (vms->gic_version == 3) { 1631 virt_max_cpus = 1632 vms->memmap[VIRT_GIC_REDIST].size / GICV3_REDIST_SIZE; 1633 virt_max_cpus += 1634 vms->memmap[VIRT_HIGH_GIC_REDIST2].size / GICV3_REDIST_SIZE; 1635 } else { 1636 virt_max_cpus = GIC_NCPU; 1637 } 1638 1639 if (max_cpus > virt_max_cpus) { 1640 error_report("Number of SMP CPUs requested (%d) exceeds max CPUs " 1641 "supported by machine 'mach-virt' (%d)", 1642 max_cpus, virt_max_cpus); 1643 exit(1); 1644 } 1645 1646 vms->smp_cpus = smp_cpus; 1647 1648 if (vms->virt && kvm_enabled()) { 1649 error_report("mach-virt: KVM does not support providing " 1650 "Virtualization extensions to the guest CPU"); 1651 exit(1); 1652 } 1653 1654 create_fdt(vms); 1655 1656 possible_cpus = mc->possible_cpu_arch_ids(machine); 1657 for (n = 0; n < possible_cpus->len; n++) { 1658 Object *cpuobj; 1659 CPUState *cs; 1660 1661 if (n >= smp_cpus) { 1662 break; 1663 } 1664 1665 cpuobj = object_new(possible_cpus->cpus[n].type); 1666 object_property_set_int(cpuobj, possible_cpus->cpus[n].arch_id, 1667 "mp-affinity", NULL); 1668 1669 cs = CPU(cpuobj); 1670 cs->cpu_index = n; 1671 1672 numa_cpu_pre_plug(&possible_cpus->cpus[cs->cpu_index], DEVICE(cpuobj), 1673 &error_fatal); 1674 1675 aarch64 &= object_property_get_bool(cpuobj, "aarch64", NULL); 1676 1677 if (!vms->secure) { 1678 object_property_set_bool(cpuobj, false, "has_el3", NULL); 1679 } 1680 1681 if (!vms->virt && object_property_find(cpuobj, "has_el2", NULL)) { 1682 object_property_set_bool(cpuobj, false, "has_el2", NULL); 1683 } 1684 1685 if (vms->psci_conduit != QEMU_PSCI_CONDUIT_DISABLED) { 1686 object_property_set_int(cpuobj, vms->psci_conduit, 1687 "psci-conduit", NULL); 1688 1689 /* Secondary CPUs start in PSCI powered-down state */ 1690 if (n > 0) { 1691 object_property_set_bool(cpuobj, true, 1692 "start-powered-off", NULL); 1693 } 1694 } 1695 1696 if (vmc->kvm_no_adjvtime && 1697 object_property_find(cpuobj, "kvm-no-adjvtime", NULL)) { 1698 object_property_set_bool(cpuobj, true, "kvm-no-adjvtime", NULL); 1699 } 1700 1701 if (vmc->no_pmu && object_property_find(cpuobj, "pmu", NULL)) { 1702 object_property_set_bool(cpuobj, false, "pmu", NULL); 1703 } 1704 1705 if (object_property_find(cpuobj, "reset-cbar", NULL)) { 1706 object_property_set_int(cpuobj, vms->memmap[VIRT_CPUPERIPHS].base, 1707 "reset-cbar", &error_abort); 1708 } 1709 1710 object_property_set_link(cpuobj, OBJECT(sysmem), "memory", 1711 &error_abort); 1712 if (vms->secure) { 1713 object_property_set_link(cpuobj, OBJECT(secure_sysmem), 1714 "secure-memory", &error_abort); 1715 } 1716 1717 object_property_set_bool(cpuobj, true, "realized", &error_fatal); 1718 object_unref(cpuobj); 1719 } 1720 fdt_add_timer_nodes(vms); 1721 fdt_add_cpu_nodes(vms); 1722 1723 if (!kvm_enabled()) { 1724 ARMCPU *cpu = ARM_CPU(first_cpu); 1725 bool aarch64 = object_property_get_bool(OBJECT(cpu), "aarch64", NULL); 1726 1727 if (aarch64 && vms->highmem) { 1728 int requested_pa_size, pamax = arm_pamax(cpu); 1729 1730 requested_pa_size = 64 - clz64(vms->highest_gpa); 1731 if (pamax < requested_pa_size) { 1732 error_report("VCPU supports less PA bits (%d) than requested " 1733 "by the memory map (%d)", pamax, requested_pa_size); 1734 exit(1); 1735 } 1736 } 1737 } 1738 1739 memory_region_add_subregion(sysmem, vms->memmap[VIRT_MEM].base, 1740 machine->ram); 1741 if (machine->device_memory) { 1742 memory_region_add_subregion(sysmem, machine->device_memory->base, 1743 &machine->device_memory->mr); 1744 } 1745 1746 virt_flash_fdt(vms, sysmem, secure_sysmem ?: sysmem); 1747 1748 create_gic(vms); 1749 1750 fdt_add_pmu_nodes(vms); 1751 1752 create_uart(vms, VIRT_UART, sysmem, serial_hd(0)); 1753 1754 if (vms->secure) { 1755 create_secure_ram(vms, secure_sysmem); 1756 create_uart(vms, VIRT_SECURE_UART, secure_sysmem, serial_hd(1)); 1757 } 1758 1759 vms->highmem_ecam &= vms->highmem && (!firmware_loaded || aarch64); 1760 1761 create_rtc(vms); 1762 1763 create_pcie(vms); 1764 1765 if (has_ged && aarch64 && firmware_loaded && acpi_enabled) { 1766 vms->acpi_dev = create_acpi_ged(vms); 1767 } else { 1768 create_gpio(vms); 1769 } 1770 1771 /* connect powerdown request */ 1772 vms->powerdown_notifier.notify = virt_powerdown_req; 1773 qemu_register_powerdown_notifier(&vms->powerdown_notifier); 1774 1775 /* Create mmio transports, so the user can create virtio backends 1776 * (which will be automatically plugged in to the transports). If 1777 * no backend is created the transport will just sit harmlessly idle. 1778 */ 1779 create_virtio_devices(vms); 1780 1781 vms->fw_cfg = create_fw_cfg(vms, &address_space_memory); 1782 rom_set_fw(vms->fw_cfg); 1783 1784 create_platform_bus(vms); 1785 1786 vms->bootinfo.ram_size = machine->ram_size; 1787 vms->bootinfo.nb_cpus = smp_cpus; 1788 vms->bootinfo.board_id = -1; 1789 vms->bootinfo.loader_start = vms->memmap[VIRT_MEM].base; 1790 vms->bootinfo.get_dtb = machvirt_dtb; 1791 vms->bootinfo.skip_dtb_autoload = true; 1792 vms->bootinfo.firmware_loaded = firmware_loaded; 1793 arm_load_kernel(ARM_CPU(first_cpu), machine, &vms->bootinfo); 1794 1795 vms->machine_done.notify = virt_machine_done; 1796 qemu_add_machine_init_done_notifier(&vms->machine_done); 1797 } 1798 1799 static bool virt_get_secure(Object *obj, Error **errp) 1800 { 1801 VirtMachineState *vms = VIRT_MACHINE(obj); 1802 1803 return vms->secure; 1804 } 1805 1806 static void virt_set_secure(Object *obj, bool value, Error **errp) 1807 { 1808 VirtMachineState *vms = VIRT_MACHINE(obj); 1809 1810 vms->secure = value; 1811 } 1812 1813 static bool virt_get_virt(Object *obj, Error **errp) 1814 { 1815 VirtMachineState *vms = VIRT_MACHINE(obj); 1816 1817 return vms->virt; 1818 } 1819 1820 static void virt_set_virt(Object *obj, bool value, Error **errp) 1821 { 1822 VirtMachineState *vms = VIRT_MACHINE(obj); 1823 1824 vms->virt = value; 1825 } 1826 1827 static bool virt_get_highmem(Object *obj, Error **errp) 1828 { 1829 VirtMachineState *vms = VIRT_MACHINE(obj); 1830 1831 return vms->highmem; 1832 } 1833 1834 static void virt_set_highmem(Object *obj, bool value, Error **errp) 1835 { 1836 VirtMachineState *vms = VIRT_MACHINE(obj); 1837 1838 vms->highmem = value; 1839 } 1840 1841 static bool virt_get_its(Object *obj, Error **errp) 1842 { 1843 VirtMachineState *vms = VIRT_MACHINE(obj); 1844 1845 return vms->its; 1846 } 1847 1848 static void virt_set_its(Object *obj, bool value, Error **errp) 1849 { 1850 VirtMachineState *vms = VIRT_MACHINE(obj); 1851 1852 vms->its = value; 1853 } 1854 1855 static char *virt_get_gic_version(Object *obj, Error **errp) 1856 { 1857 VirtMachineState *vms = VIRT_MACHINE(obj); 1858 const char *val = vms->gic_version == 3 ? "3" : "2"; 1859 1860 return g_strdup(val); 1861 } 1862 1863 static void virt_set_gic_version(Object *obj, const char *value, Error **errp) 1864 { 1865 VirtMachineState *vms = VIRT_MACHINE(obj); 1866 1867 if (!strcmp(value, "3")) { 1868 vms->gic_version = 3; 1869 } else if (!strcmp(value, "2")) { 1870 vms->gic_version = 2; 1871 } else if (!strcmp(value, "host")) { 1872 vms->gic_version = 0; /* Will probe later */ 1873 } else if (!strcmp(value, "max")) { 1874 vms->gic_version = -1; /* Will probe later */ 1875 } else { 1876 error_setg(errp, "Invalid gic-version value"); 1877 error_append_hint(errp, "Valid values are 3, 2, host, max.\n"); 1878 } 1879 } 1880 1881 static char *virt_get_iommu(Object *obj, Error **errp) 1882 { 1883 VirtMachineState *vms = VIRT_MACHINE(obj); 1884 1885 switch (vms->iommu) { 1886 case VIRT_IOMMU_NONE: 1887 return g_strdup("none"); 1888 case VIRT_IOMMU_SMMUV3: 1889 return g_strdup("smmuv3"); 1890 default: 1891 g_assert_not_reached(); 1892 } 1893 } 1894 1895 static void virt_set_iommu(Object *obj, const char *value, Error **errp) 1896 { 1897 VirtMachineState *vms = VIRT_MACHINE(obj); 1898 1899 if (!strcmp(value, "smmuv3")) { 1900 vms->iommu = VIRT_IOMMU_SMMUV3; 1901 } else if (!strcmp(value, "none")) { 1902 vms->iommu = VIRT_IOMMU_NONE; 1903 } else { 1904 error_setg(errp, "Invalid iommu value"); 1905 error_append_hint(errp, "Valid values are none, smmuv3.\n"); 1906 } 1907 } 1908 1909 static CpuInstanceProperties 1910 virt_cpu_index_to_props(MachineState *ms, unsigned cpu_index) 1911 { 1912 MachineClass *mc = MACHINE_GET_CLASS(ms); 1913 const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(ms); 1914 1915 assert(cpu_index < possible_cpus->len); 1916 return possible_cpus->cpus[cpu_index].props; 1917 } 1918 1919 static int64_t virt_get_default_cpu_node_id(const MachineState *ms, int idx) 1920 { 1921 return idx % ms->numa_state->num_nodes; 1922 } 1923 1924 static const CPUArchIdList *virt_possible_cpu_arch_ids(MachineState *ms) 1925 { 1926 int n; 1927 unsigned int max_cpus = ms->smp.max_cpus; 1928 VirtMachineState *vms = VIRT_MACHINE(ms); 1929 1930 if (ms->possible_cpus) { 1931 assert(ms->possible_cpus->len == max_cpus); 1932 return ms->possible_cpus; 1933 } 1934 1935 ms->possible_cpus = g_malloc0(sizeof(CPUArchIdList) + 1936 sizeof(CPUArchId) * max_cpus); 1937 ms->possible_cpus->len = max_cpus; 1938 for (n = 0; n < ms->possible_cpus->len; n++) { 1939 ms->possible_cpus->cpus[n].type = ms->cpu_type; 1940 ms->possible_cpus->cpus[n].arch_id = 1941 virt_cpu_mp_affinity(vms, n); 1942 ms->possible_cpus->cpus[n].props.has_thread_id = true; 1943 ms->possible_cpus->cpus[n].props.thread_id = n; 1944 } 1945 return ms->possible_cpus; 1946 } 1947 1948 static void virt_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev, 1949 Error **errp) 1950 { 1951 VirtMachineState *vms = VIRT_MACHINE(hotplug_dev); 1952 const bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM); 1953 1954 if (is_nvdimm) { 1955 error_setg(errp, "nvdimm is not yet supported"); 1956 return; 1957 } 1958 1959 if (!vms->acpi_dev) { 1960 error_setg(errp, 1961 "memory hotplug is not enabled: missing acpi-ged device"); 1962 return; 1963 } 1964 1965 pc_dimm_pre_plug(PC_DIMM(dev), MACHINE(hotplug_dev), NULL, errp); 1966 } 1967 1968 static void virt_memory_plug(HotplugHandler *hotplug_dev, 1969 DeviceState *dev, Error **errp) 1970 { 1971 VirtMachineState *vms = VIRT_MACHINE(hotplug_dev); 1972 Error *local_err = NULL; 1973 1974 pc_dimm_plug(PC_DIMM(dev), MACHINE(vms), &local_err); 1975 if (local_err) { 1976 goto out; 1977 } 1978 1979 hotplug_handler_plug(HOTPLUG_HANDLER(vms->acpi_dev), 1980 dev, &error_abort); 1981 1982 out: 1983 error_propagate(errp, local_err); 1984 } 1985 1986 static void virt_machine_device_pre_plug_cb(HotplugHandler *hotplug_dev, 1987 DeviceState *dev, Error **errp) 1988 { 1989 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) { 1990 virt_memory_pre_plug(hotplug_dev, dev, errp); 1991 } 1992 } 1993 1994 static void virt_machine_device_plug_cb(HotplugHandler *hotplug_dev, 1995 DeviceState *dev, Error **errp) 1996 { 1997 VirtMachineState *vms = VIRT_MACHINE(hotplug_dev); 1998 1999 if (vms->platform_bus_dev) { 2000 if (object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE)) { 2001 platform_bus_link_device(PLATFORM_BUS_DEVICE(vms->platform_bus_dev), 2002 SYS_BUS_DEVICE(dev)); 2003 } 2004 } 2005 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) { 2006 virt_memory_plug(hotplug_dev, dev, errp); 2007 } 2008 if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_IOMMU_PCI)) { 2009 PCIDevice *pdev = PCI_DEVICE(dev); 2010 2011 vms->iommu = VIRT_IOMMU_VIRTIO; 2012 vms->virtio_iommu_bdf = pci_get_bdf(pdev); 2013 create_virtio_iommu_dt_bindings(vms, errp); 2014 } 2015 } 2016 2017 static void virt_machine_device_unplug_request_cb(HotplugHandler *hotplug_dev, 2018 DeviceState *dev, Error **errp) 2019 { 2020 error_setg(errp, "device unplug request for unsupported device" 2021 " type: %s", object_get_typename(OBJECT(dev))); 2022 } 2023 2024 static HotplugHandler *virt_machine_get_hotplug_handler(MachineState *machine, 2025 DeviceState *dev) 2026 { 2027 if (object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE) || 2028 (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM))) { 2029 return HOTPLUG_HANDLER(machine); 2030 } 2031 if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_IOMMU_PCI)) { 2032 VirtMachineState *vms = VIRT_MACHINE(machine); 2033 2034 if (!vms->bootinfo.firmware_loaded || !acpi_enabled) { 2035 return HOTPLUG_HANDLER(machine); 2036 } 2037 } 2038 return NULL; 2039 } 2040 2041 /* 2042 * for arm64 kvm_type [7-0] encodes the requested number of bits 2043 * in the IPA address space 2044 */ 2045 static int virt_kvm_type(MachineState *ms, const char *type_str) 2046 { 2047 VirtMachineState *vms = VIRT_MACHINE(ms); 2048 int max_vm_pa_size = kvm_arm_get_max_vm_ipa_size(ms); 2049 int requested_pa_size; 2050 2051 /* we freeze the memory map to compute the highest gpa */ 2052 virt_set_memmap(vms); 2053 2054 requested_pa_size = 64 - clz64(vms->highest_gpa); 2055 2056 if (requested_pa_size > max_vm_pa_size) { 2057 error_report("-m and ,maxmem option values " 2058 "require an IPA range (%d bits) larger than " 2059 "the one supported by the host (%d bits)", 2060 requested_pa_size, max_vm_pa_size); 2061 exit(1); 2062 } 2063 /* 2064 * By default we return 0 which corresponds to an implicit legacy 2065 * 40b IPA setting. Otherwise we return the actual requested PA 2066 * logsize 2067 */ 2068 return requested_pa_size > 40 ? requested_pa_size : 0; 2069 } 2070 2071 static void virt_machine_class_init(ObjectClass *oc, void *data) 2072 { 2073 MachineClass *mc = MACHINE_CLASS(oc); 2074 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc); 2075 2076 mc->init = machvirt_init; 2077 /* Start with max_cpus set to 512, which is the maximum supported by KVM. 2078 * The value may be reduced later when we have more information about the 2079 * configuration of the particular instance. 2080 */ 2081 mc->max_cpus = 512; 2082 machine_class_allow_dynamic_sysbus_dev(mc, TYPE_VFIO_CALXEDA_XGMAC); 2083 machine_class_allow_dynamic_sysbus_dev(mc, TYPE_VFIO_AMD_XGBE); 2084 machine_class_allow_dynamic_sysbus_dev(mc, TYPE_RAMFB_DEVICE); 2085 machine_class_allow_dynamic_sysbus_dev(mc, TYPE_VFIO_PLATFORM); 2086 mc->block_default_type = IF_VIRTIO; 2087 mc->no_cdrom = 1; 2088 mc->pci_allow_0_address = true; 2089 /* We know we will never create a pre-ARMv7 CPU which needs 1K pages */ 2090 mc->minimum_page_bits = 12; 2091 mc->possible_cpu_arch_ids = virt_possible_cpu_arch_ids; 2092 mc->cpu_index_to_instance_props = virt_cpu_index_to_props; 2093 mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-a15"); 2094 mc->get_default_cpu_node_id = virt_get_default_cpu_node_id; 2095 mc->kvm_type = virt_kvm_type; 2096 assert(!mc->get_hotplug_handler); 2097 mc->get_hotplug_handler = virt_machine_get_hotplug_handler; 2098 hc->pre_plug = virt_machine_device_pre_plug_cb; 2099 hc->plug = virt_machine_device_plug_cb; 2100 hc->unplug_request = virt_machine_device_unplug_request_cb; 2101 mc->numa_mem_supported = true; 2102 mc->auto_enable_numa_with_memhp = true; 2103 mc->default_ram_id = "mach-virt.ram"; 2104 } 2105 2106 static void virt_instance_init(Object *obj) 2107 { 2108 VirtMachineState *vms = VIRT_MACHINE(obj); 2109 VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms); 2110 2111 /* EL3 is disabled by default on virt: this makes us consistent 2112 * between KVM and TCG for this board, and it also allows us to 2113 * boot UEFI blobs which assume no TrustZone support. 2114 */ 2115 vms->secure = false; 2116 object_property_add_bool(obj, "secure", virt_get_secure, 2117 virt_set_secure, NULL); 2118 object_property_set_description(obj, "secure", 2119 "Set on/off to enable/disable the ARM " 2120 "Security Extensions (TrustZone)", 2121 NULL); 2122 2123 /* EL2 is also disabled by default, for similar reasons */ 2124 vms->virt = false; 2125 object_property_add_bool(obj, "virtualization", virt_get_virt, 2126 virt_set_virt, NULL); 2127 object_property_set_description(obj, "virtualization", 2128 "Set on/off to enable/disable emulating a " 2129 "guest CPU which implements the ARM " 2130 "Virtualization Extensions", 2131 NULL); 2132 2133 /* High memory is enabled by default */ 2134 vms->highmem = true; 2135 object_property_add_bool(obj, "highmem", virt_get_highmem, 2136 virt_set_highmem, NULL); 2137 object_property_set_description(obj, "highmem", 2138 "Set on/off to enable/disable using " 2139 "physical address space above 32 bits", 2140 NULL); 2141 /* Default GIC type is v2 */ 2142 vms->gic_version = 2; 2143 object_property_add_str(obj, "gic-version", virt_get_gic_version, 2144 virt_set_gic_version, NULL); 2145 object_property_set_description(obj, "gic-version", 2146 "Set GIC version. " 2147 "Valid values are 2, 3 and host", NULL); 2148 2149 vms->highmem_ecam = !vmc->no_highmem_ecam; 2150 2151 if (vmc->no_its) { 2152 vms->its = false; 2153 } else { 2154 /* Default allows ITS instantiation */ 2155 vms->its = true; 2156 object_property_add_bool(obj, "its", virt_get_its, 2157 virt_set_its, NULL); 2158 object_property_set_description(obj, "its", 2159 "Set on/off to enable/disable " 2160 "ITS instantiation", 2161 NULL); 2162 } 2163 2164 /* Default disallows iommu instantiation */ 2165 vms->iommu = VIRT_IOMMU_NONE; 2166 object_property_add_str(obj, "iommu", virt_get_iommu, virt_set_iommu, NULL); 2167 object_property_set_description(obj, "iommu", 2168 "Set the IOMMU type. " 2169 "Valid values are none and smmuv3", 2170 NULL); 2171 2172 vms->irqmap = a15irqmap; 2173 2174 virt_flash_create(vms); 2175 } 2176 2177 static const TypeInfo virt_machine_info = { 2178 .name = TYPE_VIRT_MACHINE, 2179 .parent = TYPE_MACHINE, 2180 .abstract = true, 2181 .instance_size = sizeof(VirtMachineState), 2182 .class_size = sizeof(VirtMachineClass), 2183 .class_init = virt_machine_class_init, 2184 .instance_init = virt_instance_init, 2185 .interfaces = (InterfaceInfo[]) { 2186 { TYPE_HOTPLUG_HANDLER }, 2187 { } 2188 }, 2189 }; 2190 2191 static void machvirt_machine_init(void) 2192 { 2193 type_register_static(&virt_machine_info); 2194 } 2195 type_init(machvirt_machine_init); 2196 2197 static void virt_machine_5_0_options(MachineClass *mc) 2198 { 2199 } 2200 DEFINE_VIRT_MACHINE_AS_LATEST(5, 0) 2201 2202 static void virt_machine_4_2_options(MachineClass *mc) 2203 { 2204 VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc)); 2205 2206 virt_machine_5_0_options(mc); 2207 compat_props_add(mc->compat_props, hw_compat_4_2, hw_compat_4_2_len); 2208 vmc->kvm_no_adjvtime = true; 2209 } 2210 DEFINE_VIRT_MACHINE(4, 2) 2211 2212 static void virt_machine_4_1_options(MachineClass *mc) 2213 { 2214 VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc)); 2215 2216 virt_machine_4_2_options(mc); 2217 compat_props_add(mc->compat_props, hw_compat_4_1, hw_compat_4_1_len); 2218 vmc->no_ged = true; 2219 mc->auto_enable_numa_with_memhp = false; 2220 } 2221 DEFINE_VIRT_MACHINE(4, 1) 2222 2223 static void virt_machine_4_0_options(MachineClass *mc) 2224 { 2225 virt_machine_4_1_options(mc); 2226 compat_props_add(mc->compat_props, hw_compat_4_0, hw_compat_4_0_len); 2227 } 2228 DEFINE_VIRT_MACHINE(4, 0) 2229 2230 static void virt_machine_3_1_options(MachineClass *mc) 2231 { 2232 virt_machine_4_0_options(mc); 2233 compat_props_add(mc->compat_props, hw_compat_3_1, hw_compat_3_1_len); 2234 } 2235 DEFINE_VIRT_MACHINE(3, 1) 2236 2237 static void virt_machine_3_0_options(MachineClass *mc) 2238 { 2239 virt_machine_3_1_options(mc); 2240 compat_props_add(mc->compat_props, hw_compat_3_0, hw_compat_3_0_len); 2241 } 2242 DEFINE_VIRT_MACHINE(3, 0) 2243 2244 static void virt_machine_2_12_options(MachineClass *mc) 2245 { 2246 VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc)); 2247 2248 virt_machine_3_0_options(mc); 2249 compat_props_add(mc->compat_props, hw_compat_2_12, hw_compat_2_12_len); 2250 vmc->no_highmem_ecam = true; 2251 mc->max_cpus = 255; 2252 } 2253 DEFINE_VIRT_MACHINE(2, 12) 2254 2255 static void virt_machine_2_11_options(MachineClass *mc) 2256 { 2257 VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc)); 2258 2259 virt_machine_2_12_options(mc); 2260 compat_props_add(mc->compat_props, hw_compat_2_11, hw_compat_2_11_len); 2261 vmc->smbios_old_sys_ver = true; 2262 } 2263 DEFINE_VIRT_MACHINE(2, 11) 2264 2265 static void virt_machine_2_10_options(MachineClass *mc) 2266 { 2267 virt_machine_2_11_options(mc); 2268 compat_props_add(mc->compat_props, hw_compat_2_10, hw_compat_2_10_len); 2269 /* before 2.11 we never faulted accesses to bad addresses */ 2270 mc->ignore_memory_transaction_failures = true; 2271 } 2272 DEFINE_VIRT_MACHINE(2, 10) 2273 2274 static void virt_machine_2_9_options(MachineClass *mc) 2275 { 2276 virt_machine_2_10_options(mc); 2277 compat_props_add(mc->compat_props, hw_compat_2_9, hw_compat_2_9_len); 2278 } 2279 DEFINE_VIRT_MACHINE(2, 9) 2280 2281 static void virt_machine_2_8_options(MachineClass *mc) 2282 { 2283 VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc)); 2284 2285 virt_machine_2_9_options(mc); 2286 compat_props_add(mc->compat_props, hw_compat_2_8, hw_compat_2_8_len); 2287 /* For 2.8 and earlier we falsely claimed in the DT that 2288 * our timers were edge-triggered, not level-triggered. 2289 */ 2290 vmc->claim_edge_triggered_timers = true; 2291 } 2292 DEFINE_VIRT_MACHINE(2, 8) 2293 2294 static void virt_machine_2_7_options(MachineClass *mc) 2295 { 2296 VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc)); 2297 2298 virt_machine_2_8_options(mc); 2299 compat_props_add(mc->compat_props, hw_compat_2_7, hw_compat_2_7_len); 2300 /* ITS was introduced with 2.8 */ 2301 vmc->no_its = true; 2302 /* Stick with 1K pages for migration compatibility */ 2303 mc->minimum_page_bits = 0; 2304 } 2305 DEFINE_VIRT_MACHINE(2, 7) 2306 2307 static void virt_machine_2_6_options(MachineClass *mc) 2308 { 2309 VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc)); 2310 2311 virt_machine_2_7_options(mc); 2312 compat_props_add(mc->compat_props, hw_compat_2_6, hw_compat_2_6_len); 2313 vmc->disallow_affinity_adjustment = true; 2314 /* Disable PMU for 2.6 as PMU support was first introduced in 2.7 */ 2315 vmc->no_pmu = true; 2316 } 2317 DEFINE_VIRT_MACHINE(2, 6) 2318