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/devices.h" 35 #include "net/net.h" 36 #include "sysemu/block-backend.h" 37 #include "sysemu/device_tree.h" 38 #include "sysemu/sysemu.h" 39 #include "sysemu/kvm.h" 40 #include "hw/boards.h" 41 #include "hw/loader.h" 42 #include "exec/address-spaces.h" 43 #include "qemu/bitops.h" 44 #include "qemu/error-report.h" 45 46 #define NUM_VIRTIO_TRANSPORTS 32 47 48 /* Number of external interrupt lines to configure the GIC with */ 49 #define NUM_IRQS 128 50 51 #define GIC_FDT_IRQ_TYPE_SPI 0 52 #define GIC_FDT_IRQ_TYPE_PPI 1 53 54 #define GIC_FDT_IRQ_FLAGS_EDGE_LO_HI 1 55 #define GIC_FDT_IRQ_FLAGS_EDGE_HI_LO 2 56 #define GIC_FDT_IRQ_FLAGS_LEVEL_HI 4 57 #define GIC_FDT_IRQ_FLAGS_LEVEL_LO 8 58 59 #define GIC_FDT_IRQ_PPI_CPU_START 8 60 #define GIC_FDT_IRQ_PPI_CPU_WIDTH 8 61 62 enum { 63 VIRT_FLASH, 64 VIRT_MEM, 65 VIRT_CPUPERIPHS, 66 VIRT_GIC_DIST, 67 VIRT_GIC_CPU, 68 VIRT_UART, 69 VIRT_MMIO, 70 VIRT_RTC, 71 }; 72 73 typedef struct MemMapEntry { 74 hwaddr base; 75 hwaddr size; 76 } MemMapEntry; 77 78 typedef struct VirtBoardInfo { 79 struct arm_boot_info bootinfo; 80 const char *cpu_model; 81 const MemMapEntry *memmap; 82 const int *irqmap; 83 int smp_cpus; 84 void *fdt; 85 int fdt_size; 86 uint32_t clock_phandle; 87 } VirtBoardInfo; 88 89 typedef struct { 90 MachineClass parent; 91 VirtBoardInfo *daughterboard; 92 } VirtMachineClass; 93 94 typedef struct { 95 MachineState parent; 96 bool secure; 97 } VirtMachineState; 98 99 #define TYPE_VIRT_MACHINE "virt" 100 #define VIRT_MACHINE(obj) \ 101 OBJECT_CHECK(VirtMachineState, (obj), TYPE_VIRT_MACHINE) 102 #define VIRT_MACHINE_GET_CLASS(obj) \ 103 OBJECT_GET_CLASS(VirtMachineClass, obj, TYPE_VIRT_MACHINE) 104 #define VIRT_MACHINE_CLASS(klass) \ 105 OBJECT_CLASS_CHECK(VirtMachineClass, klass, TYPE_VIRT_MACHINE) 106 107 /* Addresses and sizes of our components. 108 * 0..128MB is space for a flash device so we can run bootrom code such as UEFI. 109 * 128MB..256MB is used for miscellaneous device I/O. 110 * 256MB..1GB is reserved for possible future PCI support (ie where the 111 * PCI memory window will go if we add a PCI host controller). 112 * 1GB and up is RAM (which may happily spill over into the 113 * high memory region beyond 4GB). 114 * This represents a compromise between how much RAM can be given to 115 * a 32 bit VM and leaving space for expansion and in particular for PCI. 116 * Note that devices should generally be placed at multiples of 0x10000, 117 * to accommodate guests using 64K pages. 118 */ 119 static const MemMapEntry a15memmap[] = { 120 /* Space up to 0x8000000 is reserved for a boot ROM */ 121 [VIRT_FLASH] = { 0, 0x08000000 }, 122 [VIRT_CPUPERIPHS] = { 0x08000000, 0x00020000 }, 123 /* GIC distributor and CPU interfaces sit inside the CPU peripheral space */ 124 [VIRT_GIC_DIST] = { 0x08000000, 0x00010000 }, 125 [VIRT_GIC_CPU] = { 0x08010000, 0x00010000 }, 126 [VIRT_UART] = { 0x09000000, 0x00001000 }, 127 [VIRT_RTC] = { 0x09010000, 0x00001000 }, 128 [VIRT_MMIO] = { 0x0a000000, 0x00000200 }, 129 /* ...repeating for a total of NUM_VIRTIO_TRANSPORTS, each of that size */ 130 /* 0x10000000 .. 0x40000000 reserved for PCI */ 131 [VIRT_MEM] = { 0x40000000, 30ULL * 1024 * 1024 * 1024 }, 132 }; 133 134 static const int a15irqmap[] = { 135 [VIRT_UART] = 1, 136 [VIRT_RTC] = 2, 137 [VIRT_MMIO] = 16, /* ...to 16 + NUM_VIRTIO_TRANSPORTS - 1 */ 138 }; 139 140 static VirtBoardInfo machines[] = { 141 { 142 .cpu_model = "cortex-a15", 143 .memmap = a15memmap, 144 .irqmap = a15irqmap, 145 }, 146 { 147 .cpu_model = "cortex-a57", 148 .memmap = a15memmap, 149 .irqmap = a15irqmap, 150 }, 151 { 152 .cpu_model = "host", 153 .memmap = a15memmap, 154 .irqmap = a15irqmap, 155 }, 156 }; 157 158 static VirtBoardInfo *find_machine_info(const char *cpu) 159 { 160 int i; 161 162 for (i = 0; i < ARRAY_SIZE(machines); i++) { 163 if (strcmp(cpu, machines[i].cpu_model) == 0) { 164 return &machines[i]; 165 } 166 } 167 return NULL; 168 } 169 170 static void create_fdt(VirtBoardInfo *vbi) 171 { 172 void *fdt = create_device_tree(&vbi->fdt_size); 173 174 if (!fdt) { 175 error_report("create_device_tree() failed"); 176 exit(1); 177 } 178 179 vbi->fdt = fdt; 180 181 /* Header */ 182 qemu_fdt_setprop_string(fdt, "/", "compatible", "linux,dummy-virt"); 183 qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2); 184 qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2); 185 186 /* 187 * /chosen and /memory nodes must exist for load_dtb 188 * to fill in necessary properties later 189 */ 190 qemu_fdt_add_subnode(fdt, "/chosen"); 191 qemu_fdt_add_subnode(fdt, "/memory"); 192 qemu_fdt_setprop_string(fdt, "/memory", "device_type", "memory"); 193 194 /* Clock node, for the benefit of the UART. The kernel device tree 195 * binding documentation claims the PL011 node clock properties are 196 * optional but in practice if you omit them the kernel refuses to 197 * probe for the device. 198 */ 199 vbi->clock_phandle = qemu_fdt_alloc_phandle(fdt); 200 qemu_fdt_add_subnode(fdt, "/apb-pclk"); 201 qemu_fdt_setprop_string(fdt, "/apb-pclk", "compatible", "fixed-clock"); 202 qemu_fdt_setprop_cell(fdt, "/apb-pclk", "#clock-cells", 0x0); 203 qemu_fdt_setprop_cell(fdt, "/apb-pclk", "clock-frequency", 24000000); 204 qemu_fdt_setprop_string(fdt, "/apb-pclk", "clock-output-names", 205 "clk24mhz"); 206 qemu_fdt_setprop_cell(fdt, "/apb-pclk", "phandle", vbi->clock_phandle); 207 208 } 209 210 static void fdt_add_psci_node(const VirtBoardInfo *vbi) 211 { 212 uint32_t cpu_suspend_fn; 213 uint32_t cpu_off_fn; 214 uint32_t cpu_on_fn; 215 uint32_t migrate_fn; 216 void *fdt = vbi->fdt; 217 ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(0)); 218 219 qemu_fdt_add_subnode(fdt, "/psci"); 220 if (armcpu->psci_version == 2) { 221 const char comp[] = "arm,psci-0.2\0arm,psci"; 222 qemu_fdt_setprop(fdt, "/psci", "compatible", comp, sizeof(comp)); 223 224 cpu_off_fn = QEMU_PSCI_0_2_FN_CPU_OFF; 225 if (arm_feature(&armcpu->env, ARM_FEATURE_AARCH64)) { 226 cpu_suspend_fn = QEMU_PSCI_0_2_FN64_CPU_SUSPEND; 227 cpu_on_fn = QEMU_PSCI_0_2_FN64_CPU_ON; 228 migrate_fn = QEMU_PSCI_0_2_FN64_MIGRATE; 229 } else { 230 cpu_suspend_fn = QEMU_PSCI_0_2_FN_CPU_SUSPEND; 231 cpu_on_fn = QEMU_PSCI_0_2_FN_CPU_ON; 232 migrate_fn = QEMU_PSCI_0_2_FN_MIGRATE; 233 } 234 } else { 235 qemu_fdt_setprop_string(fdt, "/psci", "compatible", "arm,psci"); 236 237 cpu_suspend_fn = QEMU_PSCI_0_1_FN_CPU_SUSPEND; 238 cpu_off_fn = QEMU_PSCI_0_1_FN_CPU_OFF; 239 cpu_on_fn = QEMU_PSCI_0_1_FN_CPU_ON; 240 migrate_fn = QEMU_PSCI_0_1_FN_MIGRATE; 241 } 242 243 /* We adopt the PSCI spec's nomenclature, and use 'conduit' to refer 244 * to the instruction that should be used to invoke PSCI functions. 245 * However, the device tree binding uses 'method' instead, so that is 246 * what we should use here. 247 */ 248 qemu_fdt_setprop_string(fdt, "/psci", "method", "hvc"); 249 250 qemu_fdt_setprop_cell(fdt, "/psci", "cpu_suspend", cpu_suspend_fn); 251 qemu_fdt_setprop_cell(fdt, "/psci", "cpu_off", cpu_off_fn); 252 qemu_fdt_setprop_cell(fdt, "/psci", "cpu_on", cpu_on_fn); 253 qemu_fdt_setprop_cell(fdt, "/psci", "migrate", migrate_fn); 254 } 255 256 static void fdt_add_timer_nodes(const VirtBoardInfo *vbi) 257 { 258 /* Note that on A15 h/w these interrupts are level-triggered, 259 * but for the GIC implementation provided by both QEMU and KVM 260 * they are edge-triggered. 261 */ 262 ARMCPU *armcpu; 263 uint32_t irqflags = GIC_FDT_IRQ_FLAGS_EDGE_LO_HI; 264 265 irqflags = deposit32(irqflags, GIC_FDT_IRQ_PPI_CPU_START, 266 GIC_FDT_IRQ_PPI_CPU_WIDTH, (1 << vbi->smp_cpus) - 1); 267 268 qemu_fdt_add_subnode(vbi->fdt, "/timer"); 269 270 armcpu = ARM_CPU(qemu_get_cpu(0)); 271 if (arm_feature(&armcpu->env, ARM_FEATURE_V8)) { 272 const char compat[] = "arm,armv8-timer\0arm,armv7-timer"; 273 qemu_fdt_setprop(vbi->fdt, "/timer", "compatible", 274 compat, sizeof(compat)); 275 } else { 276 qemu_fdt_setprop_string(vbi->fdt, "/timer", "compatible", 277 "arm,armv7-timer"); 278 } 279 qemu_fdt_setprop_cells(vbi->fdt, "/timer", "interrupts", 280 GIC_FDT_IRQ_TYPE_PPI, 13, irqflags, 281 GIC_FDT_IRQ_TYPE_PPI, 14, irqflags, 282 GIC_FDT_IRQ_TYPE_PPI, 11, irqflags, 283 GIC_FDT_IRQ_TYPE_PPI, 10, irqflags); 284 } 285 286 static void fdt_add_cpu_nodes(const VirtBoardInfo *vbi) 287 { 288 int cpu; 289 290 qemu_fdt_add_subnode(vbi->fdt, "/cpus"); 291 qemu_fdt_setprop_cell(vbi->fdt, "/cpus", "#address-cells", 0x1); 292 qemu_fdt_setprop_cell(vbi->fdt, "/cpus", "#size-cells", 0x0); 293 294 for (cpu = vbi->smp_cpus - 1; cpu >= 0; cpu--) { 295 char *nodename = g_strdup_printf("/cpus/cpu@%d", cpu); 296 ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(cpu)); 297 298 qemu_fdt_add_subnode(vbi->fdt, nodename); 299 qemu_fdt_setprop_string(vbi->fdt, nodename, "device_type", "cpu"); 300 qemu_fdt_setprop_string(vbi->fdt, nodename, "compatible", 301 armcpu->dtb_compatible); 302 303 if (vbi->smp_cpus > 1) { 304 qemu_fdt_setprop_string(vbi->fdt, nodename, 305 "enable-method", "psci"); 306 } 307 308 qemu_fdt_setprop_cell(vbi->fdt, nodename, "reg", cpu); 309 g_free(nodename); 310 } 311 } 312 313 static void fdt_add_gic_node(const VirtBoardInfo *vbi) 314 { 315 uint32_t gic_phandle; 316 317 gic_phandle = qemu_fdt_alloc_phandle(vbi->fdt); 318 qemu_fdt_setprop_cell(vbi->fdt, "/", "interrupt-parent", gic_phandle); 319 320 qemu_fdt_add_subnode(vbi->fdt, "/intc"); 321 /* 'cortex-a15-gic' means 'GIC v2' */ 322 qemu_fdt_setprop_string(vbi->fdt, "/intc", "compatible", 323 "arm,cortex-a15-gic"); 324 qemu_fdt_setprop_cell(vbi->fdt, "/intc", "#interrupt-cells", 3); 325 qemu_fdt_setprop(vbi->fdt, "/intc", "interrupt-controller", NULL, 0); 326 qemu_fdt_setprop_sized_cells(vbi->fdt, "/intc", "reg", 327 2, vbi->memmap[VIRT_GIC_DIST].base, 328 2, vbi->memmap[VIRT_GIC_DIST].size, 329 2, vbi->memmap[VIRT_GIC_CPU].base, 330 2, vbi->memmap[VIRT_GIC_CPU].size); 331 qemu_fdt_setprop_cell(vbi->fdt, "/intc", "phandle", gic_phandle); 332 } 333 334 static void create_gic(const VirtBoardInfo *vbi, qemu_irq *pic) 335 { 336 /* We create a standalone GIC v2 */ 337 DeviceState *gicdev; 338 SysBusDevice *gicbusdev; 339 const char *gictype = "arm_gic"; 340 int i; 341 342 if (kvm_irqchip_in_kernel()) { 343 gictype = "kvm-arm-gic"; 344 } 345 346 gicdev = qdev_create(NULL, gictype); 347 qdev_prop_set_uint32(gicdev, "revision", 2); 348 qdev_prop_set_uint32(gicdev, "num-cpu", smp_cpus); 349 /* Note that the num-irq property counts both internal and external 350 * interrupts; there are always 32 of the former (mandated by GIC spec). 351 */ 352 qdev_prop_set_uint32(gicdev, "num-irq", NUM_IRQS + 32); 353 qdev_init_nofail(gicdev); 354 gicbusdev = SYS_BUS_DEVICE(gicdev); 355 sysbus_mmio_map(gicbusdev, 0, vbi->memmap[VIRT_GIC_DIST].base); 356 sysbus_mmio_map(gicbusdev, 1, vbi->memmap[VIRT_GIC_CPU].base); 357 358 /* Wire the outputs from each CPU's generic timer to the 359 * appropriate GIC PPI inputs, and the GIC's IRQ output to 360 * the CPU's IRQ input. 361 */ 362 for (i = 0; i < smp_cpus; i++) { 363 DeviceState *cpudev = DEVICE(qemu_get_cpu(i)); 364 int ppibase = NUM_IRQS + i * 32; 365 /* physical timer; we wire it up to the non-secure timer's ID, 366 * since a real A15 always has TrustZone but QEMU doesn't. 367 */ 368 qdev_connect_gpio_out(cpudev, 0, 369 qdev_get_gpio_in(gicdev, ppibase + 30)); 370 /* virtual timer */ 371 qdev_connect_gpio_out(cpudev, 1, 372 qdev_get_gpio_in(gicdev, ppibase + 27)); 373 374 sysbus_connect_irq(gicbusdev, i, qdev_get_gpio_in(cpudev, ARM_CPU_IRQ)); 375 } 376 377 for (i = 0; i < NUM_IRQS; i++) { 378 pic[i] = qdev_get_gpio_in(gicdev, i); 379 } 380 381 fdt_add_gic_node(vbi); 382 } 383 384 static void create_uart(const VirtBoardInfo *vbi, qemu_irq *pic) 385 { 386 char *nodename; 387 hwaddr base = vbi->memmap[VIRT_UART].base; 388 hwaddr size = vbi->memmap[VIRT_UART].size; 389 int irq = vbi->irqmap[VIRT_UART]; 390 const char compat[] = "arm,pl011\0arm,primecell"; 391 const char clocknames[] = "uartclk\0apb_pclk"; 392 393 sysbus_create_simple("pl011", base, pic[irq]); 394 395 nodename = g_strdup_printf("/pl011@%" PRIx64, base); 396 qemu_fdt_add_subnode(vbi->fdt, nodename); 397 /* Note that we can't use setprop_string because of the embedded NUL */ 398 qemu_fdt_setprop(vbi->fdt, nodename, "compatible", 399 compat, sizeof(compat)); 400 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg", 401 2, base, 2, size); 402 qemu_fdt_setprop_cells(vbi->fdt, nodename, "interrupts", 403 GIC_FDT_IRQ_TYPE_SPI, irq, 404 GIC_FDT_IRQ_FLAGS_LEVEL_HI); 405 qemu_fdt_setprop_cells(vbi->fdt, nodename, "clocks", 406 vbi->clock_phandle, vbi->clock_phandle); 407 qemu_fdt_setprop(vbi->fdt, nodename, "clock-names", 408 clocknames, sizeof(clocknames)); 409 410 qemu_fdt_setprop_string(vbi->fdt, "/chosen", "stdout-path", nodename); 411 g_free(nodename); 412 } 413 414 static void create_rtc(const VirtBoardInfo *vbi, qemu_irq *pic) 415 { 416 char *nodename; 417 hwaddr base = vbi->memmap[VIRT_RTC].base; 418 hwaddr size = vbi->memmap[VIRT_RTC].size; 419 int irq = vbi->irqmap[VIRT_RTC]; 420 const char compat[] = "arm,pl031\0arm,primecell"; 421 422 sysbus_create_simple("pl031", base, pic[irq]); 423 424 nodename = g_strdup_printf("/pl031@%" PRIx64, base); 425 qemu_fdt_add_subnode(vbi->fdt, nodename); 426 qemu_fdt_setprop(vbi->fdt, nodename, "compatible", compat, sizeof(compat)); 427 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg", 428 2, base, 2, size); 429 qemu_fdt_setprop_cells(vbi->fdt, nodename, "interrupts", 430 GIC_FDT_IRQ_TYPE_SPI, irq, 431 GIC_FDT_IRQ_FLAGS_LEVEL_HI); 432 qemu_fdt_setprop_cell(vbi->fdt, nodename, "clocks", vbi->clock_phandle); 433 qemu_fdt_setprop_string(vbi->fdt, nodename, "clock-names", "apb_pclk"); 434 g_free(nodename); 435 } 436 437 static void create_virtio_devices(const VirtBoardInfo *vbi, qemu_irq *pic) 438 { 439 int i; 440 hwaddr size = vbi->memmap[VIRT_MMIO].size; 441 442 /* Note that we have to create the transports in forwards order 443 * so that command line devices are inserted lowest address first, 444 * and then add dtb nodes in reverse order so that they appear in 445 * the finished device tree lowest address first. 446 */ 447 for (i = 0; i < NUM_VIRTIO_TRANSPORTS; i++) { 448 int irq = vbi->irqmap[VIRT_MMIO] + i; 449 hwaddr base = vbi->memmap[VIRT_MMIO].base + i * size; 450 451 sysbus_create_simple("virtio-mmio", base, pic[irq]); 452 } 453 454 for (i = NUM_VIRTIO_TRANSPORTS - 1; i >= 0; i--) { 455 char *nodename; 456 int irq = vbi->irqmap[VIRT_MMIO] + i; 457 hwaddr base = vbi->memmap[VIRT_MMIO].base + i * size; 458 459 nodename = g_strdup_printf("/virtio_mmio@%" PRIx64, base); 460 qemu_fdt_add_subnode(vbi->fdt, nodename); 461 qemu_fdt_setprop_string(vbi->fdt, nodename, 462 "compatible", "virtio,mmio"); 463 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg", 464 2, base, 2, size); 465 qemu_fdt_setprop_cells(vbi->fdt, nodename, "interrupts", 466 GIC_FDT_IRQ_TYPE_SPI, irq, 467 GIC_FDT_IRQ_FLAGS_EDGE_LO_HI); 468 g_free(nodename); 469 } 470 } 471 472 static void create_one_flash(const char *name, hwaddr flashbase, 473 hwaddr flashsize) 474 { 475 /* Create and map a single flash device. We use the same 476 * parameters as the flash devices on the Versatile Express board. 477 */ 478 DriveInfo *dinfo = drive_get_next(IF_PFLASH); 479 DeviceState *dev = qdev_create(NULL, "cfi.pflash01"); 480 const uint64_t sectorlength = 256 * 1024; 481 482 if (dinfo && qdev_prop_set_drive(dev, "drive", 483 blk_by_legacy_dinfo(dinfo))) { 484 abort(); 485 } 486 487 qdev_prop_set_uint32(dev, "num-blocks", flashsize / sectorlength); 488 qdev_prop_set_uint64(dev, "sector-length", sectorlength); 489 qdev_prop_set_uint8(dev, "width", 4); 490 qdev_prop_set_uint8(dev, "device-width", 2); 491 qdev_prop_set_uint8(dev, "big-endian", 0); 492 qdev_prop_set_uint16(dev, "id0", 0x89); 493 qdev_prop_set_uint16(dev, "id1", 0x18); 494 qdev_prop_set_uint16(dev, "id2", 0x00); 495 qdev_prop_set_uint16(dev, "id3", 0x00); 496 qdev_prop_set_string(dev, "name", name); 497 qdev_init_nofail(dev); 498 499 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, flashbase); 500 } 501 502 static void create_flash(const VirtBoardInfo *vbi) 503 { 504 /* Create two flash devices to fill the VIRT_FLASH space in the memmap. 505 * Any file passed via -bios goes in the first of these. 506 */ 507 hwaddr flashsize = vbi->memmap[VIRT_FLASH].size / 2; 508 hwaddr flashbase = vbi->memmap[VIRT_FLASH].base; 509 char *nodename; 510 511 if (bios_name) { 512 const char *fn; 513 514 if (drive_get(IF_PFLASH, 0, 0)) { 515 error_report("The contents of the first flash device may be " 516 "specified with -bios or with -drive if=pflash... " 517 "but you cannot use both options at once"); 518 exit(1); 519 } 520 fn = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name); 521 if (!fn || load_image_targphys(fn, flashbase, flashsize) < 0) { 522 error_report("Could not load ROM image '%s'", bios_name); 523 exit(1); 524 } 525 } 526 527 create_one_flash("virt.flash0", flashbase, flashsize); 528 create_one_flash("virt.flash1", flashbase + flashsize, flashsize); 529 530 nodename = g_strdup_printf("/flash@%" PRIx64, flashbase); 531 qemu_fdt_add_subnode(vbi->fdt, nodename); 532 qemu_fdt_setprop_string(vbi->fdt, nodename, "compatible", "cfi-flash"); 533 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg", 534 2, flashbase, 2, flashsize, 535 2, flashbase + flashsize, 2, flashsize); 536 qemu_fdt_setprop_cell(vbi->fdt, nodename, "bank-width", 4); 537 g_free(nodename); 538 } 539 540 static void *machvirt_dtb(const struct arm_boot_info *binfo, int *fdt_size) 541 { 542 const VirtBoardInfo *board = (const VirtBoardInfo *)binfo; 543 544 *fdt_size = board->fdt_size; 545 return board->fdt; 546 } 547 548 static void machvirt_init(MachineState *machine) 549 { 550 VirtMachineState *vms = VIRT_MACHINE(machine); 551 qemu_irq pic[NUM_IRQS]; 552 MemoryRegion *sysmem = get_system_memory(); 553 int n; 554 MemoryRegion *ram = g_new(MemoryRegion, 1); 555 const char *cpu_model = machine->cpu_model; 556 VirtBoardInfo *vbi; 557 558 if (!cpu_model) { 559 cpu_model = "cortex-a15"; 560 } 561 562 vbi = find_machine_info(cpu_model); 563 564 if (!vbi) { 565 error_report("mach-virt: CPU %s not supported", cpu_model); 566 exit(1); 567 } 568 569 vbi->smp_cpus = smp_cpus; 570 571 if (machine->ram_size > vbi->memmap[VIRT_MEM].size) { 572 error_report("mach-virt: cannot model more than 30GB RAM"); 573 exit(1); 574 } 575 576 create_fdt(vbi); 577 578 for (n = 0; n < smp_cpus; n++) { 579 ObjectClass *oc = cpu_class_by_name(TYPE_ARM_CPU, cpu_model); 580 Object *cpuobj; 581 582 if (!oc) { 583 fprintf(stderr, "Unable to find CPU definition\n"); 584 exit(1); 585 } 586 cpuobj = object_new(object_class_get_name(oc)); 587 588 if (!vms->secure) { 589 object_property_set_bool(cpuobj, false, "has_el3", NULL); 590 } 591 592 object_property_set_int(cpuobj, QEMU_PSCI_CONDUIT_HVC, "psci-conduit", 593 NULL); 594 595 /* Secondary CPUs start in PSCI powered-down state */ 596 if (n > 0) { 597 object_property_set_bool(cpuobj, true, "start-powered-off", NULL); 598 } 599 600 if (object_property_find(cpuobj, "reset-cbar", NULL)) { 601 object_property_set_int(cpuobj, vbi->memmap[VIRT_CPUPERIPHS].base, 602 "reset-cbar", &error_abort); 603 } 604 605 object_property_set_bool(cpuobj, true, "realized", NULL); 606 } 607 fdt_add_timer_nodes(vbi); 608 fdt_add_cpu_nodes(vbi); 609 fdt_add_psci_node(vbi); 610 611 memory_region_init_ram(ram, NULL, "mach-virt.ram", machine->ram_size, 612 &error_abort); 613 vmstate_register_ram_global(ram); 614 memory_region_add_subregion(sysmem, vbi->memmap[VIRT_MEM].base, ram); 615 616 create_flash(vbi); 617 618 create_gic(vbi, pic); 619 620 create_uart(vbi, pic); 621 622 create_rtc(vbi, pic); 623 624 /* Create mmio transports, so the user can create virtio backends 625 * (which will be automatically plugged in to the transports). If 626 * no backend is created the transport will just sit harmlessly idle. 627 */ 628 create_virtio_devices(vbi, pic); 629 630 vbi->bootinfo.ram_size = machine->ram_size; 631 vbi->bootinfo.kernel_filename = machine->kernel_filename; 632 vbi->bootinfo.kernel_cmdline = machine->kernel_cmdline; 633 vbi->bootinfo.initrd_filename = machine->initrd_filename; 634 vbi->bootinfo.nb_cpus = smp_cpus; 635 vbi->bootinfo.board_id = -1; 636 vbi->bootinfo.loader_start = vbi->memmap[VIRT_MEM].base; 637 vbi->bootinfo.get_dtb = machvirt_dtb; 638 arm_load_kernel(ARM_CPU(first_cpu), &vbi->bootinfo); 639 } 640 641 static bool virt_get_secure(Object *obj, Error **errp) 642 { 643 VirtMachineState *vms = VIRT_MACHINE(obj); 644 645 return vms->secure; 646 } 647 648 static void virt_set_secure(Object *obj, bool value, Error **errp) 649 { 650 VirtMachineState *vms = VIRT_MACHINE(obj); 651 652 vms->secure = value; 653 } 654 655 static void virt_instance_init(Object *obj) 656 { 657 VirtMachineState *vms = VIRT_MACHINE(obj); 658 659 /* EL3 is enabled by default on virt */ 660 vms->secure = true; 661 object_property_add_bool(obj, "secure", virt_get_secure, 662 virt_set_secure, NULL); 663 object_property_set_description(obj, "secure", 664 "Set on/off to enable/disable the ARM " 665 "Security Extensions (TrustZone)", 666 NULL); 667 } 668 669 static void virt_class_init(ObjectClass *oc, void *data) 670 { 671 MachineClass *mc = MACHINE_CLASS(oc); 672 673 mc->name = TYPE_VIRT_MACHINE; 674 mc->desc = "ARM Virtual Machine", 675 mc->init = machvirt_init; 676 mc->max_cpus = 8; 677 } 678 679 static const TypeInfo machvirt_info = { 680 .name = TYPE_VIRT_MACHINE, 681 .parent = TYPE_MACHINE, 682 .instance_size = sizeof(VirtMachineState), 683 .instance_init = virt_instance_init, 684 .class_size = sizeof(VirtMachineClass), 685 .class_init = virt_class_init, 686 }; 687 688 static void machvirt_machine_init(void) 689 { 690 type_register_static(&machvirt_info); 691 } 692 693 machine_init(machvirt_machine_init); 694