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 "hw/sysbus.h" 32 #include "hw/arm/arm.h" 33 #include "hw/arm/primecell.h" 34 #include "hw/arm/virt.h" 35 #include "hw/devices.h" 36 #include "net/net.h" 37 #include "sysemu/block-backend.h" 38 #include "sysemu/device_tree.h" 39 #include "sysemu/sysemu.h" 40 #include "sysemu/kvm.h" 41 #include "hw/boards.h" 42 #include "hw/loader.h" 43 #include "exec/address-spaces.h" 44 #include "qemu/bitops.h" 45 #include "qemu/error-report.h" 46 #include "hw/pci-host/gpex.h" 47 #include "hw/arm/virt-acpi-build.h" 48 #include "hw/arm/sysbus-fdt.h" 49 #include "hw/platform-bus.h" 50 51 /* Number of external interrupt lines to configure the GIC with */ 52 #define NUM_IRQS 256 53 54 #define GIC_FDT_IRQ_TYPE_SPI 0 55 #define GIC_FDT_IRQ_TYPE_PPI 1 56 57 #define GIC_FDT_IRQ_FLAGS_EDGE_LO_HI 1 58 #define GIC_FDT_IRQ_FLAGS_EDGE_HI_LO 2 59 #define GIC_FDT_IRQ_FLAGS_LEVEL_HI 4 60 #define GIC_FDT_IRQ_FLAGS_LEVEL_LO 8 61 62 #define GIC_FDT_IRQ_PPI_CPU_START 8 63 #define GIC_FDT_IRQ_PPI_CPU_WIDTH 8 64 65 #define PLATFORM_BUS_NUM_IRQS 64 66 67 static ARMPlatformBusSystemParams platform_bus_params; 68 69 typedef struct VirtBoardInfo { 70 struct arm_boot_info bootinfo; 71 const char *cpu_model; 72 const MemMapEntry *memmap; 73 const int *irqmap; 74 int smp_cpus; 75 void *fdt; 76 int fdt_size; 77 uint32_t clock_phandle; 78 uint32_t gic_phandle; 79 uint32_t v2m_phandle; 80 } VirtBoardInfo; 81 82 typedef struct { 83 MachineClass parent; 84 VirtBoardInfo *daughterboard; 85 } VirtMachineClass; 86 87 typedef struct { 88 MachineState parent; 89 bool secure; 90 } VirtMachineState; 91 92 #define TYPE_VIRT_MACHINE "virt" 93 #define VIRT_MACHINE(obj) \ 94 OBJECT_CHECK(VirtMachineState, (obj), TYPE_VIRT_MACHINE) 95 #define VIRT_MACHINE_GET_CLASS(obj) \ 96 OBJECT_GET_CLASS(VirtMachineClass, obj, TYPE_VIRT_MACHINE) 97 #define VIRT_MACHINE_CLASS(klass) \ 98 OBJECT_CLASS_CHECK(VirtMachineClass, klass, TYPE_VIRT_MACHINE) 99 100 /* Addresses and sizes of our components. 101 * 0..128MB is space for a flash device so we can run bootrom code such as UEFI. 102 * 128MB..256MB is used for miscellaneous device I/O. 103 * 256MB..1GB is reserved for possible future PCI support (ie where the 104 * PCI memory window will go if we add a PCI host controller). 105 * 1GB and up is RAM (which may happily spill over into the 106 * high memory region beyond 4GB). 107 * This represents a compromise between how much RAM can be given to 108 * a 32 bit VM and leaving space for expansion and in particular for PCI. 109 * Note that devices should generally be placed at multiples of 0x10000, 110 * to accommodate guests using 64K pages. 111 */ 112 static const MemMapEntry a15memmap[] = { 113 /* Space up to 0x8000000 is reserved for a boot ROM */ 114 [VIRT_FLASH] = { 0, 0x08000000 }, 115 [VIRT_CPUPERIPHS] = { 0x08000000, 0x00020000 }, 116 /* GIC distributor and CPU interfaces sit inside the CPU peripheral space */ 117 [VIRT_GIC_DIST] = { 0x08000000, 0x00010000 }, 118 [VIRT_GIC_CPU] = { 0x08010000, 0x00010000 }, 119 [VIRT_GIC_V2M] = { 0x08020000, 0x00001000 }, 120 [VIRT_UART] = { 0x09000000, 0x00001000 }, 121 [VIRT_RTC] = { 0x09010000, 0x00001000 }, 122 [VIRT_FW_CFG] = { 0x09020000, 0x0000000a }, 123 [VIRT_MMIO] = { 0x0a000000, 0x00000200 }, 124 /* ...repeating for a total of NUM_VIRTIO_TRANSPORTS, each of that size */ 125 [VIRT_PLATFORM_BUS] = { 0x0c000000, 0x02000000 }, 126 [VIRT_PCIE_MMIO] = { 0x10000000, 0x2eff0000 }, 127 [VIRT_PCIE_PIO] = { 0x3eff0000, 0x00010000 }, 128 [VIRT_PCIE_ECAM] = { 0x3f000000, 0x01000000 }, 129 [VIRT_MEM] = { 0x40000000, 30ULL * 1024 * 1024 * 1024 }, 130 }; 131 132 static const int a15irqmap[] = { 133 [VIRT_UART] = 1, 134 [VIRT_RTC] = 2, 135 [VIRT_PCIE] = 3, /* ... to 6 */ 136 [VIRT_MMIO] = 16, /* ...to 16 + NUM_VIRTIO_TRANSPORTS - 1 */ 137 [VIRT_GIC_V2M] = 48, /* ...to 48 + NUM_GICV2M_SPIS - 1 */ 138 [VIRT_PLATFORM_BUS] = 112, /* ...to 112 + PLATFORM_BUS_NUM_IRQS -1 */ 139 }; 140 141 static VirtBoardInfo machines[] = { 142 { 143 .cpu_model = "cortex-a15", 144 .memmap = a15memmap, 145 .irqmap = a15irqmap, 146 }, 147 { 148 .cpu_model = "cortex-a53", 149 .memmap = a15memmap, 150 .irqmap = a15irqmap, 151 }, 152 { 153 .cpu_model = "cortex-a57", 154 .memmap = a15memmap, 155 .irqmap = a15irqmap, 156 }, 157 { 158 .cpu_model = "host", 159 .memmap = a15memmap, 160 .irqmap = a15irqmap, 161 }, 162 }; 163 164 static VirtBoardInfo *find_machine_info(const char *cpu) 165 { 166 int i; 167 168 for (i = 0; i < ARRAY_SIZE(machines); i++) { 169 if (strcmp(cpu, machines[i].cpu_model) == 0) { 170 return &machines[i]; 171 } 172 } 173 return NULL; 174 } 175 176 static void create_fdt(VirtBoardInfo *vbi) 177 { 178 void *fdt = create_device_tree(&vbi->fdt_size); 179 180 if (!fdt) { 181 error_report("create_device_tree() failed"); 182 exit(1); 183 } 184 185 vbi->fdt = fdt; 186 187 /* Header */ 188 qemu_fdt_setprop_string(fdt, "/", "compatible", "linux,dummy-virt"); 189 qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2); 190 qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2); 191 192 /* 193 * /chosen and /memory nodes must exist for load_dtb 194 * to fill in necessary properties later 195 */ 196 qemu_fdt_add_subnode(fdt, "/chosen"); 197 qemu_fdt_add_subnode(fdt, "/memory"); 198 qemu_fdt_setprop_string(fdt, "/memory", "device_type", "memory"); 199 200 /* Clock node, for the benefit of the UART. The kernel device tree 201 * binding documentation claims the PL011 node clock properties are 202 * optional but in practice if you omit them the kernel refuses to 203 * probe for the device. 204 */ 205 vbi->clock_phandle = qemu_fdt_alloc_phandle(fdt); 206 qemu_fdt_add_subnode(fdt, "/apb-pclk"); 207 qemu_fdt_setprop_string(fdt, "/apb-pclk", "compatible", "fixed-clock"); 208 qemu_fdt_setprop_cell(fdt, "/apb-pclk", "#clock-cells", 0x0); 209 qemu_fdt_setprop_cell(fdt, "/apb-pclk", "clock-frequency", 24000000); 210 qemu_fdt_setprop_string(fdt, "/apb-pclk", "clock-output-names", 211 "clk24mhz"); 212 qemu_fdt_setprop_cell(fdt, "/apb-pclk", "phandle", vbi->clock_phandle); 213 214 } 215 216 static void fdt_add_psci_node(const VirtBoardInfo *vbi) 217 { 218 uint32_t cpu_suspend_fn; 219 uint32_t cpu_off_fn; 220 uint32_t cpu_on_fn; 221 uint32_t migrate_fn; 222 void *fdt = vbi->fdt; 223 ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(0)); 224 225 qemu_fdt_add_subnode(fdt, "/psci"); 226 if (armcpu->psci_version == 2) { 227 const char comp[] = "arm,psci-0.2\0arm,psci"; 228 qemu_fdt_setprop(fdt, "/psci", "compatible", comp, sizeof(comp)); 229 230 cpu_off_fn = QEMU_PSCI_0_2_FN_CPU_OFF; 231 if (arm_feature(&armcpu->env, ARM_FEATURE_AARCH64)) { 232 cpu_suspend_fn = QEMU_PSCI_0_2_FN64_CPU_SUSPEND; 233 cpu_on_fn = QEMU_PSCI_0_2_FN64_CPU_ON; 234 migrate_fn = QEMU_PSCI_0_2_FN64_MIGRATE; 235 } else { 236 cpu_suspend_fn = QEMU_PSCI_0_2_FN_CPU_SUSPEND; 237 cpu_on_fn = QEMU_PSCI_0_2_FN_CPU_ON; 238 migrate_fn = QEMU_PSCI_0_2_FN_MIGRATE; 239 } 240 } else { 241 qemu_fdt_setprop_string(fdt, "/psci", "compatible", "arm,psci"); 242 243 cpu_suspend_fn = QEMU_PSCI_0_1_FN_CPU_SUSPEND; 244 cpu_off_fn = QEMU_PSCI_0_1_FN_CPU_OFF; 245 cpu_on_fn = QEMU_PSCI_0_1_FN_CPU_ON; 246 migrate_fn = QEMU_PSCI_0_1_FN_MIGRATE; 247 } 248 249 /* We adopt the PSCI spec's nomenclature, and use 'conduit' to refer 250 * to the instruction that should be used to invoke PSCI functions. 251 * However, the device tree binding uses 'method' instead, so that is 252 * what we should use here. 253 */ 254 qemu_fdt_setprop_string(fdt, "/psci", "method", "hvc"); 255 256 qemu_fdt_setprop_cell(fdt, "/psci", "cpu_suspend", cpu_suspend_fn); 257 qemu_fdt_setprop_cell(fdt, "/psci", "cpu_off", cpu_off_fn); 258 qemu_fdt_setprop_cell(fdt, "/psci", "cpu_on", cpu_on_fn); 259 qemu_fdt_setprop_cell(fdt, "/psci", "migrate", migrate_fn); 260 } 261 262 static void fdt_add_timer_nodes(const VirtBoardInfo *vbi) 263 { 264 /* Note that on A15 h/w these interrupts are level-triggered, 265 * but for the GIC implementation provided by both QEMU and KVM 266 * they are edge-triggered. 267 */ 268 ARMCPU *armcpu; 269 uint32_t irqflags = GIC_FDT_IRQ_FLAGS_EDGE_LO_HI; 270 271 irqflags = deposit32(irqflags, GIC_FDT_IRQ_PPI_CPU_START, 272 GIC_FDT_IRQ_PPI_CPU_WIDTH, (1 << vbi->smp_cpus) - 1); 273 274 qemu_fdt_add_subnode(vbi->fdt, "/timer"); 275 276 armcpu = ARM_CPU(qemu_get_cpu(0)); 277 if (arm_feature(&armcpu->env, ARM_FEATURE_V8)) { 278 const char compat[] = "arm,armv8-timer\0arm,armv7-timer"; 279 qemu_fdt_setprop(vbi->fdt, "/timer", "compatible", 280 compat, sizeof(compat)); 281 } else { 282 qemu_fdt_setprop_string(vbi->fdt, "/timer", "compatible", 283 "arm,armv7-timer"); 284 } 285 qemu_fdt_setprop_cells(vbi->fdt, "/timer", "interrupts", 286 GIC_FDT_IRQ_TYPE_PPI, ARCH_TIMER_S_EL1_IRQ, irqflags, 287 GIC_FDT_IRQ_TYPE_PPI, ARCH_TIMER_NS_EL1_IRQ, irqflags, 288 GIC_FDT_IRQ_TYPE_PPI, ARCH_TIMER_VIRT_IRQ, irqflags, 289 GIC_FDT_IRQ_TYPE_PPI, ARCH_TIMER_NS_EL2_IRQ, irqflags); 290 } 291 292 static void fdt_add_cpu_nodes(const VirtBoardInfo *vbi) 293 { 294 int cpu; 295 296 qemu_fdt_add_subnode(vbi->fdt, "/cpus"); 297 qemu_fdt_setprop_cell(vbi->fdt, "/cpus", "#address-cells", 0x1); 298 qemu_fdt_setprop_cell(vbi->fdt, "/cpus", "#size-cells", 0x0); 299 300 for (cpu = vbi->smp_cpus - 1; cpu >= 0; cpu--) { 301 char *nodename = g_strdup_printf("/cpus/cpu@%d", cpu); 302 ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(cpu)); 303 304 qemu_fdt_add_subnode(vbi->fdt, nodename); 305 qemu_fdt_setprop_string(vbi->fdt, nodename, "device_type", "cpu"); 306 qemu_fdt_setprop_string(vbi->fdt, nodename, "compatible", 307 armcpu->dtb_compatible); 308 309 if (vbi->smp_cpus > 1) { 310 qemu_fdt_setprop_string(vbi->fdt, nodename, 311 "enable-method", "psci"); 312 } 313 314 qemu_fdt_setprop_cell(vbi->fdt, nodename, "reg", armcpu->mp_affinity); 315 g_free(nodename); 316 } 317 } 318 319 static void fdt_add_v2m_gic_node(VirtBoardInfo *vbi) 320 { 321 vbi->v2m_phandle = qemu_fdt_alloc_phandle(vbi->fdt); 322 qemu_fdt_add_subnode(vbi->fdt, "/intc/v2m"); 323 qemu_fdt_setprop_string(vbi->fdt, "/intc/v2m", "compatible", 324 "arm,gic-v2m-frame"); 325 qemu_fdt_setprop(vbi->fdt, "/intc/v2m", "msi-controller", NULL, 0); 326 qemu_fdt_setprop_sized_cells(vbi->fdt, "/intc/v2m", "reg", 327 2, vbi->memmap[VIRT_GIC_V2M].base, 328 2, vbi->memmap[VIRT_GIC_V2M].size); 329 qemu_fdt_setprop_cell(vbi->fdt, "/intc/v2m", "phandle", vbi->v2m_phandle); 330 } 331 332 static void fdt_add_gic_node(VirtBoardInfo *vbi) 333 { 334 vbi->gic_phandle = qemu_fdt_alloc_phandle(vbi->fdt); 335 qemu_fdt_setprop_cell(vbi->fdt, "/", "interrupt-parent", vbi->gic_phandle); 336 337 qemu_fdt_add_subnode(vbi->fdt, "/intc"); 338 /* 'cortex-a15-gic' means 'GIC v2' */ 339 qemu_fdt_setprop_string(vbi->fdt, "/intc", "compatible", 340 "arm,cortex-a15-gic"); 341 qemu_fdt_setprop_cell(vbi->fdt, "/intc", "#interrupt-cells", 3); 342 qemu_fdt_setprop(vbi->fdt, "/intc", "interrupt-controller", NULL, 0); 343 qemu_fdt_setprop_sized_cells(vbi->fdt, "/intc", "reg", 344 2, vbi->memmap[VIRT_GIC_DIST].base, 345 2, vbi->memmap[VIRT_GIC_DIST].size, 346 2, vbi->memmap[VIRT_GIC_CPU].base, 347 2, vbi->memmap[VIRT_GIC_CPU].size); 348 qemu_fdt_setprop_cell(vbi->fdt, "/intc", "#address-cells", 0x2); 349 qemu_fdt_setprop_cell(vbi->fdt, "/intc", "#size-cells", 0x2); 350 qemu_fdt_setprop(vbi->fdt, "/intc", "ranges", NULL, 0); 351 qemu_fdt_setprop_cell(vbi->fdt, "/intc", "phandle", vbi->gic_phandle); 352 } 353 354 static void create_v2m(VirtBoardInfo *vbi, qemu_irq *pic) 355 { 356 int i; 357 int irq = vbi->irqmap[VIRT_GIC_V2M]; 358 DeviceState *dev; 359 360 dev = qdev_create(NULL, "arm-gicv2m"); 361 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, vbi->memmap[VIRT_GIC_V2M].base); 362 qdev_prop_set_uint32(dev, "base-spi", irq); 363 qdev_prop_set_uint32(dev, "num-spi", NUM_GICV2M_SPIS); 364 qdev_init_nofail(dev); 365 366 for (i = 0; i < NUM_GICV2M_SPIS; i++) { 367 sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, pic[irq + i]); 368 } 369 370 fdt_add_v2m_gic_node(vbi); 371 } 372 373 static void create_gic(VirtBoardInfo *vbi, qemu_irq *pic) 374 { 375 /* We create a standalone GIC v2 */ 376 DeviceState *gicdev; 377 SysBusDevice *gicbusdev; 378 const char *gictype = "arm_gic"; 379 int i; 380 381 if (kvm_irqchip_in_kernel()) { 382 gictype = "kvm-arm-gic"; 383 } 384 385 gicdev = qdev_create(NULL, gictype); 386 qdev_prop_set_uint32(gicdev, "revision", 2); 387 qdev_prop_set_uint32(gicdev, "num-cpu", smp_cpus); 388 /* Note that the num-irq property counts both internal and external 389 * interrupts; there are always 32 of the former (mandated by GIC spec). 390 */ 391 qdev_prop_set_uint32(gicdev, "num-irq", NUM_IRQS + 32); 392 qdev_init_nofail(gicdev); 393 gicbusdev = SYS_BUS_DEVICE(gicdev); 394 sysbus_mmio_map(gicbusdev, 0, vbi->memmap[VIRT_GIC_DIST].base); 395 sysbus_mmio_map(gicbusdev, 1, vbi->memmap[VIRT_GIC_CPU].base); 396 397 /* Wire the outputs from each CPU's generic timer to the 398 * appropriate GIC PPI inputs, and the GIC's IRQ output to 399 * the CPU's IRQ input. 400 */ 401 for (i = 0; i < smp_cpus; i++) { 402 DeviceState *cpudev = DEVICE(qemu_get_cpu(i)); 403 int ppibase = NUM_IRQS + i * 32; 404 /* physical timer; we wire it up to the non-secure timer's ID, 405 * since a real A15 always has TrustZone but QEMU doesn't. 406 */ 407 qdev_connect_gpio_out(cpudev, 0, 408 qdev_get_gpio_in(gicdev, ppibase + 30)); 409 /* virtual timer */ 410 qdev_connect_gpio_out(cpudev, 1, 411 qdev_get_gpio_in(gicdev, ppibase + 27)); 412 413 sysbus_connect_irq(gicbusdev, i, qdev_get_gpio_in(cpudev, ARM_CPU_IRQ)); 414 sysbus_connect_irq(gicbusdev, i + smp_cpus, 415 qdev_get_gpio_in(cpudev, ARM_CPU_FIQ)); 416 } 417 418 for (i = 0; i < NUM_IRQS; i++) { 419 pic[i] = qdev_get_gpio_in(gicdev, i); 420 } 421 422 fdt_add_gic_node(vbi); 423 424 create_v2m(vbi, pic); 425 } 426 427 static void create_uart(const VirtBoardInfo *vbi, qemu_irq *pic) 428 { 429 char *nodename; 430 hwaddr base = vbi->memmap[VIRT_UART].base; 431 hwaddr size = vbi->memmap[VIRT_UART].size; 432 int irq = vbi->irqmap[VIRT_UART]; 433 const char compat[] = "arm,pl011\0arm,primecell"; 434 const char clocknames[] = "uartclk\0apb_pclk"; 435 436 sysbus_create_simple("pl011", base, pic[irq]); 437 438 nodename = g_strdup_printf("/pl011@%" PRIx64, base); 439 qemu_fdt_add_subnode(vbi->fdt, nodename); 440 /* Note that we can't use setprop_string because of the embedded NUL */ 441 qemu_fdt_setprop(vbi->fdt, nodename, "compatible", 442 compat, sizeof(compat)); 443 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg", 444 2, base, 2, size); 445 qemu_fdt_setprop_cells(vbi->fdt, nodename, "interrupts", 446 GIC_FDT_IRQ_TYPE_SPI, irq, 447 GIC_FDT_IRQ_FLAGS_LEVEL_HI); 448 qemu_fdt_setprop_cells(vbi->fdt, nodename, "clocks", 449 vbi->clock_phandle, vbi->clock_phandle); 450 qemu_fdt_setprop(vbi->fdt, nodename, "clock-names", 451 clocknames, sizeof(clocknames)); 452 453 qemu_fdt_setprop_string(vbi->fdt, "/chosen", "stdout-path", nodename); 454 g_free(nodename); 455 } 456 457 static void create_rtc(const VirtBoardInfo *vbi, qemu_irq *pic) 458 { 459 char *nodename; 460 hwaddr base = vbi->memmap[VIRT_RTC].base; 461 hwaddr size = vbi->memmap[VIRT_RTC].size; 462 int irq = vbi->irqmap[VIRT_RTC]; 463 const char compat[] = "arm,pl031\0arm,primecell"; 464 465 sysbus_create_simple("pl031", base, pic[irq]); 466 467 nodename = g_strdup_printf("/pl031@%" PRIx64, base); 468 qemu_fdt_add_subnode(vbi->fdt, nodename); 469 qemu_fdt_setprop(vbi->fdt, nodename, "compatible", compat, sizeof(compat)); 470 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg", 471 2, base, 2, size); 472 qemu_fdt_setprop_cells(vbi->fdt, nodename, "interrupts", 473 GIC_FDT_IRQ_TYPE_SPI, irq, 474 GIC_FDT_IRQ_FLAGS_LEVEL_HI); 475 qemu_fdt_setprop_cell(vbi->fdt, nodename, "clocks", vbi->clock_phandle); 476 qemu_fdt_setprop_string(vbi->fdt, nodename, "clock-names", "apb_pclk"); 477 g_free(nodename); 478 } 479 480 static void create_virtio_devices(const VirtBoardInfo *vbi, qemu_irq *pic) 481 { 482 int i; 483 hwaddr size = vbi->memmap[VIRT_MMIO].size; 484 485 /* We create the transports in forwards order. Since qbus_realize() 486 * prepends (not appends) new child buses, the incrementing loop below will 487 * create a list of virtio-mmio buses with decreasing base addresses. 488 * 489 * When a -device option is processed from the command line, 490 * qbus_find_recursive() picks the next free virtio-mmio bus in forwards 491 * order. The upshot is that -device options in increasing command line 492 * order are mapped to virtio-mmio buses with decreasing base addresses. 493 * 494 * When this code was originally written, that arrangement ensured that the 495 * guest Linux kernel would give the lowest "name" (/dev/vda, eth0, etc) to 496 * the first -device on the command line. (The end-to-end order is a 497 * function of this loop, qbus_realize(), qbus_find_recursive(), and the 498 * guest kernel's name-to-address assignment strategy.) 499 * 500 * Meanwhile, the kernel's traversal seems to have been reversed; see eg. 501 * the message, if not necessarily the code, of commit 70161ff336. 502 * Therefore the loop now establishes the inverse of the original intent. 503 * 504 * Unfortunately, we can't counteract the kernel change by reversing the 505 * loop; it would break existing command lines. 506 * 507 * In any case, the kernel makes no guarantee about the stability of 508 * enumeration order of virtio devices (as demonstrated by it changing 509 * between kernel versions). For reliable and stable identification 510 * of disks users must use UUIDs or similar mechanisms. 511 */ 512 for (i = 0; i < NUM_VIRTIO_TRANSPORTS; i++) { 513 int irq = vbi->irqmap[VIRT_MMIO] + i; 514 hwaddr base = vbi->memmap[VIRT_MMIO].base + i * size; 515 516 sysbus_create_simple("virtio-mmio", base, pic[irq]); 517 } 518 519 /* We add dtb nodes in reverse order so that they appear in the finished 520 * device tree lowest address first. 521 * 522 * Note that this mapping is independent of the loop above. The previous 523 * loop influences virtio device to virtio transport assignment, whereas 524 * this loop controls how virtio transports are laid out in the dtb. 525 */ 526 for (i = NUM_VIRTIO_TRANSPORTS - 1; i >= 0; i--) { 527 char *nodename; 528 int irq = vbi->irqmap[VIRT_MMIO] + i; 529 hwaddr base = vbi->memmap[VIRT_MMIO].base + i * size; 530 531 nodename = g_strdup_printf("/virtio_mmio@%" PRIx64, base); 532 qemu_fdt_add_subnode(vbi->fdt, nodename); 533 qemu_fdt_setprop_string(vbi->fdt, nodename, 534 "compatible", "virtio,mmio"); 535 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg", 536 2, base, 2, size); 537 qemu_fdt_setprop_cells(vbi->fdt, nodename, "interrupts", 538 GIC_FDT_IRQ_TYPE_SPI, irq, 539 GIC_FDT_IRQ_FLAGS_EDGE_LO_HI); 540 g_free(nodename); 541 } 542 } 543 544 static void create_one_flash(const char *name, hwaddr flashbase, 545 hwaddr flashsize) 546 { 547 /* Create and map a single flash device. We use the same 548 * parameters as the flash devices on the Versatile Express board. 549 */ 550 DriveInfo *dinfo = drive_get_next(IF_PFLASH); 551 DeviceState *dev = qdev_create(NULL, "cfi.pflash01"); 552 const uint64_t sectorlength = 256 * 1024; 553 554 if (dinfo) { 555 qdev_prop_set_drive(dev, "drive", blk_by_legacy_dinfo(dinfo), 556 &error_abort); 557 } 558 559 qdev_prop_set_uint32(dev, "num-blocks", flashsize / sectorlength); 560 qdev_prop_set_uint64(dev, "sector-length", sectorlength); 561 qdev_prop_set_uint8(dev, "width", 4); 562 qdev_prop_set_uint8(dev, "device-width", 2); 563 qdev_prop_set_bit(dev, "big-endian", false); 564 qdev_prop_set_uint16(dev, "id0", 0x89); 565 qdev_prop_set_uint16(dev, "id1", 0x18); 566 qdev_prop_set_uint16(dev, "id2", 0x00); 567 qdev_prop_set_uint16(dev, "id3", 0x00); 568 qdev_prop_set_string(dev, "name", name); 569 qdev_init_nofail(dev); 570 571 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, flashbase); 572 } 573 574 static void create_flash(const VirtBoardInfo *vbi) 575 { 576 /* Create two flash devices to fill the VIRT_FLASH space in the memmap. 577 * Any file passed via -bios goes in the first of these. 578 */ 579 hwaddr flashsize = vbi->memmap[VIRT_FLASH].size / 2; 580 hwaddr flashbase = vbi->memmap[VIRT_FLASH].base; 581 char *nodename; 582 583 if (bios_name) { 584 char *fn; 585 int image_size; 586 587 if (drive_get(IF_PFLASH, 0, 0)) { 588 error_report("The contents of the first flash device may be " 589 "specified with -bios or with -drive if=pflash... " 590 "but you cannot use both options at once"); 591 exit(1); 592 } 593 fn = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name); 594 if (!fn) { 595 error_report("Could not find ROM image '%s'", bios_name); 596 exit(1); 597 } 598 image_size = load_image_targphys(fn, flashbase, flashsize); 599 g_free(fn); 600 if (image_size < 0) { 601 error_report("Could not load ROM image '%s'", bios_name); 602 exit(1); 603 } 604 } 605 606 create_one_flash("virt.flash0", flashbase, flashsize); 607 create_one_flash("virt.flash1", flashbase + flashsize, flashsize); 608 609 nodename = g_strdup_printf("/flash@%" PRIx64, flashbase); 610 qemu_fdt_add_subnode(vbi->fdt, nodename); 611 qemu_fdt_setprop_string(vbi->fdt, nodename, "compatible", "cfi-flash"); 612 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg", 613 2, flashbase, 2, flashsize, 614 2, flashbase + flashsize, 2, flashsize); 615 qemu_fdt_setprop_cell(vbi->fdt, nodename, "bank-width", 4); 616 g_free(nodename); 617 } 618 619 static void create_fw_cfg(const VirtBoardInfo *vbi) 620 { 621 hwaddr base = vbi->memmap[VIRT_FW_CFG].base; 622 hwaddr size = vbi->memmap[VIRT_FW_CFG].size; 623 char *nodename; 624 625 fw_cfg_init_mem_wide(base + 8, base, 8); 626 627 nodename = g_strdup_printf("/fw-cfg@%" PRIx64, base); 628 qemu_fdt_add_subnode(vbi->fdt, nodename); 629 qemu_fdt_setprop_string(vbi->fdt, nodename, 630 "compatible", "qemu,fw-cfg-mmio"); 631 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg", 632 2, base, 2, size); 633 g_free(nodename); 634 } 635 636 static void create_pcie_irq_map(const VirtBoardInfo *vbi, uint32_t gic_phandle, 637 int first_irq, const char *nodename) 638 { 639 int devfn, pin; 640 uint32_t full_irq_map[4 * 4 * 10] = { 0 }; 641 uint32_t *irq_map = full_irq_map; 642 643 for (devfn = 0; devfn <= 0x18; devfn += 0x8) { 644 for (pin = 0; pin < 4; pin++) { 645 int irq_type = GIC_FDT_IRQ_TYPE_SPI; 646 int irq_nr = first_irq + ((pin + PCI_SLOT(devfn)) % PCI_NUM_PINS); 647 int irq_level = GIC_FDT_IRQ_FLAGS_LEVEL_HI; 648 int i; 649 650 uint32_t map[] = { 651 devfn << 8, 0, 0, /* devfn */ 652 pin + 1, /* PCI pin */ 653 gic_phandle, 0, 0, irq_type, irq_nr, irq_level }; /* GIC irq */ 654 655 /* Convert map to big endian */ 656 for (i = 0; i < 10; i++) { 657 irq_map[i] = cpu_to_be32(map[i]); 658 } 659 irq_map += 10; 660 } 661 } 662 663 qemu_fdt_setprop(vbi->fdt, nodename, "interrupt-map", 664 full_irq_map, sizeof(full_irq_map)); 665 666 qemu_fdt_setprop_cells(vbi->fdt, nodename, "interrupt-map-mask", 667 0x1800, 0, 0, /* devfn (PCI_SLOT(3)) */ 668 0x7 /* PCI irq */); 669 } 670 671 static void create_pcie(const VirtBoardInfo *vbi, qemu_irq *pic) 672 { 673 hwaddr base_mmio = vbi->memmap[VIRT_PCIE_MMIO].base; 674 hwaddr size_mmio = vbi->memmap[VIRT_PCIE_MMIO].size; 675 hwaddr base_pio = vbi->memmap[VIRT_PCIE_PIO].base; 676 hwaddr size_pio = vbi->memmap[VIRT_PCIE_PIO].size; 677 hwaddr base_ecam = vbi->memmap[VIRT_PCIE_ECAM].base; 678 hwaddr size_ecam = vbi->memmap[VIRT_PCIE_ECAM].size; 679 hwaddr base = base_mmio; 680 int nr_pcie_buses = size_ecam / PCIE_MMCFG_SIZE_MIN; 681 int irq = vbi->irqmap[VIRT_PCIE]; 682 MemoryRegion *mmio_alias; 683 MemoryRegion *mmio_reg; 684 MemoryRegion *ecam_alias; 685 MemoryRegion *ecam_reg; 686 DeviceState *dev; 687 char *nodename; 688 int i; 689 690 dev = qdev_create(NULL, TYPE_GPEX_HOST); 691 qdev_init_nofail(dev); 692 693 /* Map only the first size_ecam bytes of ECAM space */ 694 ecam_alias = g_new0(MemoryRegion, 1); 695 ecam_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0); 696 memory_region_init_alias(ecam_alias, OBJECT(dev), "pcie-ecam", 697 ecam_reg, 0, size_ecam); 698 memory_region_add_subregion(get_system_memory(), base_ecam, ecam_alias); 699 700 /* Map the MMIO window into system address space so as to expose 701 * the section of PCI MMIO space which starts at the same base address 702 * (ie 1:1 mapping for that part of PCI MMIO space visible through 703 * the window). 704 */ 705 mmio_alias = g_new0(MemoryRegion, 1); 706 mmio_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 1); 707 memory_region_init_alias(mmio_alias, OBJECT(dev), "pcie-mmio", 708 mmio_reg, base_mmio, size_mmio); 709 memory_region_add_subregion(get_system_memory(), base_mmio, mmio_alias); 710 711 /* Map IO port space */ 712 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 2, base_pio); 713 714 for (i = 0; i < GPEX_NUM_IRQS; i++) { 715 sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, pic[irq + i]); 716 } 717 718 nodename = g_strdup_printf("/pcie@%" PRIx64, base); 719 qemu_fdt_add_subnode(vbi->fdt, nodename); 720 qemu_fdt_setprop_string(vbi->fdt, nodename, 721 "compatible", "pci-host-ecam-generic"); 722 qemu_fdt_setprop_string(vbi->fdt, nodename, "device_type", "pci"); 723 qemu_fdt_setprop_cell(vbi->fdt, nodename, "#address-cells", 3); 724 qemu_fdt_setprop_cell(vbi->fdt, nodename, "#size-cells", 2); 725 qemu_fdt_setprop_cells(vbi->fdt, nodename, "bus-range", 0, 726 nr_pcie_buses - 1); 727 728 qemu_fdt_setprop_cells(vbi->fdt, nodename, "msi-parent", vbi->v2m_phandle); 729 730 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg", 731 2, base_ecam, 2, size_ecam); 732 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "ranges", 733 1, FDT_PCI_RANGE_IOPORT, 2, 0, 734 2, base_pio, 2, size_pio, 735 1, FDT_PCI_RANGE_MMIO, 2, base_mmio, 736 2, base_mmio, 2, size_mmio); 737 738 qemu_fdt_setprop_cell(vbi->fdt, nodename, "#interrupt-cells", 1); 739 create_pcie_irq_map(vbi, vbi->gic_phandle, irq, nodename); 740 741 g_free(nodename); 742 } 743 744 static void create_platform_bus(VirtBoardInfo *vbi, qemu_irq *pic) 745 { 746 DeviceState *dev; 747 SysBusDevice *s; 748 int i; 749 ARMPlatformBusFDTParams *fdt_params = g_new(ARMPlatformBusFDTParams, 1); 750 MemoryRegion *sysmem = get_system_memory(); 751 752 platform_bus_params.platform_bus_base = vbi->memmap[VIRT_PLATFORM_BUS].base; 753 platform_bus_params.platform_bus_size = vbi->memmap[VIRT_PLATFORM_BUS].size; 754 platform_bus_params.platform_bus_first_irq = vbi->irqmap[VIRT_PLATFORM_BUS]; 755 platform_bus_params.platform_bus_num_irqs = PLATFORM_BUS_NUM_IRQS; 756 757 fdt_params->system_params = &platform_bus_params; 758 fdt_params->binfo = &vbi->bootinfo; 759 fdt_params->intc = "/intc"; 760 /* 761 * register a machine init done notifier that creates the device tree 762 * nodes of the platform bus and its children dynamic sysbus devices 763 */ 764 arm_register_platform_bus_fdt_creator(fdt_params); 765 766 dev = qdev_create(NULL, TYPE_PLATFORM_BUS_DEVICE); 767 dev->id = TYPE_PLATFORM_BUS_DEVICE; 768 qdev_prop_set_uint32(dev, "num_irqs", 769 platform_bus_params.platform_bus_num_irqs); 770 qdev_prop_set_uint32(dev, "mmio_size", 771 platform_bus_params.platform_bus_size); 772 qdev_init_nofail(dev); 773 s = SYS_BUS_DEVICE(dev); 774 775 for (i = 0; i < platform_bus_params.platform_bus_num_irqs; i++) { 776 int irqn = platform_bus_params.platform_bus_first_irq + i; 777 sysbus_connect_irq(s, i, pic[irqn]); 778 } 779 780 memory_region_add_subregion(sysmem, 781 platform_bus_params.platform_bus_base, 782 sysbus_mmio_get_region(s, 0)); 783 } 784 785 static void *machvirt_dtb(const struct arm_boot_info *binfo, int *fdt_size) 786 { 787 const VirtBoardInfo *board = (const VirtBoardInfo *)binfo; 788 789 *fdt_size = board->fdt_size; 790 return board->fdt; 791 } 792 793 static 794 void virt_guest_info_machine_done(Notifier *notifier, void *data) 795 { 796 VirtGuestInfoState *guest_info_state = container_of(notifier, 797 VirtGuestInfoState, machine_done); 798 virt_acpi_setup(&guest_info_state->info); 799 } 800 801 static void machvirt_init(MachineState *machine) 802 { 803 VirtMachineState *vms = VIRT_MACHINE(machine); 804 qemu_irq pic[NUM_IRQS]; 805 MemoryRegion *sysmem = get_system_memory(); 806 int n; 807 MemoryRegion *ram = g_new(MemoryRegion, 1); 808 const char *cpu_model = machine->cpu_model; 809 VirtBoardInfo *vbi; 810 VirtGuestInfoState *guest_info_state = g_malloc0(sizeof *guest_info_state); 811 VirtGuestInfo *guest_info = &guest_info_state->info; 812 char **cpustr; 813 814 if (!cpu_model) { 815 cpu_model = "cortex-a15"; 816 } 817 818 /* Separate the actual CPU model name from any appended features */ 819 cpustr = g_strsplit(cpu_model, ",", 2); 820 821 vbi = find_machine_info(cpustr[0]); 822 823 if (!vbi) { 824 error_report("mach-virt: CPU %s not supported", cpustr[0]); 825 exit(1); 826 } 827 828 vbi->smp_cpus = smp_cpus; 829 830 if (machine->ram_size > vbi->memmap[VIRT_MEM].size) { 831 error_report("mach-virt: cannot model more than 30GB RAM"); 832 exit(1); 833 } 834 835 create_fdt(vbi); 836 837 for (n = 0; n < smp_cpus; n++) { 838 ObjectClass *oc = cpu_class_by_name(TYPE_ARM_CPU, cpustr[0]); 839 CPUClass *cc = CPU_CLASS(oc); 840 Object *cpuobj; 841 Error *err = NULL; 842 char *cpuopts = g_strdup(cpustr[1]); 843 844 if (!oc) { 845 fprintf(stderr, "Unable to find CPU definition\n"); 846 exit(1); 847 } 848 cpuobj = object_new(object_class_get_name(oc)); 849 850 /* Handle any CPU options specified by the user */ 851 cc->parse_features(CPU(cpuobj), cpuopts, &err); 852 g_free(cpuopts); 853 if (err) { 854 error_report_err(err); 855 exit(1); 856 } 857 858 if (!vms->secure) { 859 object_property_set_bool(cpuobj, false, "has_el3", NULL); 860 } 861 862 object_property_set_int(cpuobj, QEMU_PSCI_CONDUIT_HVC, "psci-conduit", 863 NULL); 864 865 /* Secondary CPUs start in PSCI powered-down state */ 866 if (n > 0) { 867 object_property_set_bool(cpuobj, true, "start-powered-off", NULL); 868 } 869 870 if (object_property_find(cpuobj, "reset-cbar", NULL)) { 871 object_property_set_int(cpuobj, vbi->memmap[VIRT_CPUPERIPHS].base, 872 "reset-cbar", &error_abort); 873 } 874 875 object_property_set_bool(cpuobj, true, "realized", NULL); 876 } 877 g_strfreev(cpustr); 878 fdt_add_timer_nodes(vbi); 879 fdt_add_cpu_nodes(vbi); 880 fdt_add_psci_node(vbi); 881 882 memory_region_allocate_system_memory(ram, NULL, "mach-virt.ram", 883 machine->ram_size); 884 memory_region_add_subregion(sysmem, vbi->memmap[VIRT_MEM].base, ram); 885 886 create_flash(vbi); 887 888 create_gic(vbi, pic); 889 890 create_uart(vbi, pic); 891 892 create_rtc(vbi, pic); 893 894 create_pcie(vbi, pic); 895 896 /* Create mmio transports, so the user can create virtio backends 897 * (which will be automatically plugged in to the transports). If 898 * no backend is created the transport will just sit harmlessly idle. 899 */ 900 create_virtio_devices(vbi, pic); 901 902 create_fw_cfg(vbi); 903 rom_set_fw(fw_cfg_find()); 904 905 guest_info->smp_cpus = smp_cpus; 906 guest_info->fw_cfg = fw_cfg_find(); 907 guest_info->memmap = vbi->memmap; 908 guest_info->irqmap = vbi->irqmap; 909 guest_info_state->machine_done.notify = virt_guest_info_machine_done; 910 qemu_add_machine_init_done_notifier(&guest_info_state->machine_done); 911 912 vbi->bootinfo.ram_size = machine->ram_size; 913 vbi->bootinfo.kernel_filename = machine->kernel_filename; 914 vbi->bootinfo.kernel_cmdline = machine->kernel_cmdline; 915 vbi->bootinfo.initrd_filename = machine->initrd_filename; 916 vbi->bootinfo.nb_cpus = smp_cpus; 917 vbi->bootinfo.board_id = -1; 918 vbi->bootinfo.loader_start = vbi->memmap[VIRT_MEM].base; 919 vbi->bootinfo.get_dtb = machvirt_dtb; 920 vbi->bootinfo.firmware_loaded = bios_name || drive_get(IF_PFLASH, 0, 0); 921 arm_load_kernel(ARM_CPU(first_cpu), &vbi->bootinfo); 922 923 /* 924 * arm_load_kernel machine init done notifier registration must 925 * happen before the platform_bus_create call. In this latter, 926 * another notifier is registered which adds platform bus nodes. 927 * Notifiers are executed in registration reverse order. 928 */ 929 create_platform_bus(vbi, pic); 930 } 931 932 static bool virt_get_secure(Object *obj, Error **errp) 933 { 934 VirtMachineState *vms = VIRT_MACHINE(obj); 935 936 return vms->secure; 937 } 938 939 static void virt_set_secure(Object *obj, bool value, Error **errp) 940 { 941 VirtMachineState *vms = VIRT_MACHINE(obj); 942 943 vms->secure = value; 944 } 945 946 static void virt_instance_init(Object *obj) 947 { 948 VirtMachineState *vms = VIRT_MACHINE(obj); 949 950 /* EL3 is enabled by default on virt */ 951 vms->secure = true; 952 object_property_add_bool(obj, "secure", virt_get_secure, 953 virt_set_secure, NULL); 954 object_property_set_description(obj, "secure", 955 "Set on/off to enable/disable the ARM " 956 "Security Extensions (TrustZone)", 957 NULL); 958 } 959 960 static void virt_class_init(ObjectClass *oc, void *data) 961 { 962 MachineClass *mc = MACHINE_CLASS(oc); 963 964 mc->name = TYPE_VIRT_MACHINE; 965 mc->desc = "ARM Virtual Machine", 966 mc->init = machvirt_init; 967 mc->max_cpus = 8; 968 mc->has_dynamic_sysbus = true; 969 } 970 971 static const TypeInfo machvirt_info = { 972 .name = TYPE_VIRT_MACHINE, 973 .parent = TYPE_MACHINE, 974 .instance_size = sizeof(VirtMachineState), 975 .instance_init = virt_instance_init, 976 .class_size = sizeof(VirtMachineClass), 977 .class_init = virt_class_init, 978 }; 979 980 static void machvirt_machine_init(void) 981 { 982 type_register_static(&machvirt_info); 983 } 984 985 machine_init(machvirt_machine_init); 986