2 * ARM mach-virt emulation
4 * Copyright (c) 2013 Linaro Limited
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.
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
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/>.
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.
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"
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"
49 /* Number of external interrupt lines to configure the GIC with */
52 #define GIC_FDT_IRQ_TYPE_SPI 0
53 #define GIC_FDT_IRQ_TYPE_PPI 1
55 #define GIC_FDT_IRQ_FLAGS_EDGE_LO_HI 1
56 #define GIC_FDT_IRQ_FLAGS_EDGE_HI_LO 2
57 #define GIC_FDT_IRQ_FLAGS_LEVEL_HI 4
58 #define GIC_FDT_IRQ_FLAGS_LEVEL_LO 8
60 #define GIC_FDT_IRQ_PPI_CPU_START 8
61 #define GIC_FDT_IRQ_PPI_CPU_WIDTH 8
63 typedef struct VirtBoardInfo
{
64 struct arm_boot_info bootinfo
;
65 const char *cpu_model
;
66 const MemMapEntry
*memmap
;
71 uint32_t clock_phandle
;
76 VirtBoardInfo
*daughterboard
;
84 #define TYPE_VIRT_MACHINE "virt"
85 #define VIRT_MACHINE(obj) \
86 OBJECT_CHECK(VirtMachineState, (obj), TYPE_VIRT_MACHINE)
87 #define VIRT_MACHINE_GET_CLASS(obj) \
88 OBJECT_GET_CLASS(VirtMachineClass, obj, TYPE_VIRT_MACHINE)
89 #define VIRT_MACHINE_CLASS(klass) \
90 OBJECT_CLASS_CHECK(VirtMachineClass, klass, TYPE_VIRT_MACHINE)
92 /* Addresses and sizes of our components.
93 * 0..128MB is space for a flash device so we can run bootrom code such as UEFI.
94 * 128MB..256MB is used for miscellaneous device I/O.
95 * 256MB..1GB is reserved for possible future PCI support (ie where the
96 * PCI memory window will go if we add a PCI host controller).
97 * 1GB and up is RAM (which may happily spill over into the
98 * high memory region beyond 4GB).
99 * This represents a compromise between how much RAM can be given to
100 * a 32 bit VM and leaving space for expansion and in particular for PCI.
101 * Note that devices should generally be placed at multiples of 0x10000,
102 * to accommodate guests using 64K pages.
104 static const MemMapEntry a15memmap
[] = {
105 /* Space up to 0x8000000 is reserved for a boot ROM */
106 [VIRT_FLASH
] = { 0, 0x08000000 },
107 [VIRT_CPUPERIPHS
] = { 0x08000000, 0x00020000 },
108 /* GIC distributor and CPU interfaces sit inside the CPU peripheral space */
109 [VIRT_GIC_DIST
] = { 0x08000000, 0x00010000 },
110 [VIRT_GIC_CPU
] = { 0x08010000, 0x00010000 },
111 [VIRT_UART
] = { 0x09000000, 0x00001000 },
112 [VIRT_RTC
] = { 0x09010000, 0x00001000 },
113 [VIRT_FW_CFG
] = { 0x09020000, 0x0000000a },
114 [VIRT_MMIO
] = { 0x0a000000, 0x00000200 },
115 /* ...repeating for a total of NUM_VIRTIO_TRANSPORTS, each of that size */
116 [VIRT_PCIE_MMIO
] = { 0x10000000, 0x2eff0000 },
117 [VIRT_PCIE_PIO
] = { 0x3eff0000, 0x00010000 },
118 [VIRT_PCIE_ECAM
] = { 0x3f000000, 0x01000000 },
119 [VIRT_MEM
] = { 0x40000000, 30ULL * 1024 * 1024 * 1024 },
122 static const int a15irqmap
[] = {
125 [VIRT_PCIE
] = 3, /* ... to 6 */
126 [VIRT_MMIO
] = 16, /* ...to 16 + NUM_VIRTIO_TRANSPORTS - 1 */
129 static VirtBoardInfo machines
[] = {
131 .cpu_model
= "cortex-a15",
136 .cpu_model
= "cortex-a57",
147 static VirtBoardInfo
*find_machine_info(const char *cpu
)
151 for (i
= 0; i
< ARRAY_SIZE(machines
); i
++) {
152 if (strcmp(cpu
, machines
[i
].cpu_model
) == 0) {
159 static void create_fdt(VirtBoardInfo
*vbi
)
161 void *fdt
= create_device_tree(&vbi
->fdt_size
);
164 error_report("create_device_tree() failed");
171 qemu_fdt_setprop_string(fdt
, "/", "compatible", "linux,dummy-virt");
172 qemu_fdt_setprop_cell(fdt
, "/", "#address-cells", 0x2);
173 qemu_fdt_setprop_cell(fdt
, "/", "#size-cells", 0x2);
176 * /chosen and /memory nodes must exist for load_dtb
177 * to fill in necessary properties later
179 qemu_fdt_add_subnode(fdt
, "/chosen");
180 qemu_fdt_add_subnode(fdt
, "/memory");
181 qemu_fdt_setprop_string(fdt
, "/memory", "device_type", "memory");
183 /* Clock node, for the benefit of the UART. The kernel device tree
184 * binding documentation claims the PL011 node clock properties are
185 * optional but in practice if you omit them the kernel refuses to
186 * probe for the device.
188 vbi
->clock_phandle
= qemu_fdt_alloc_phandle(fdt
);
189 qemu_fdt_add_subnode(fdt
, "/apb-pclk");
190 qemu_fdt_setprop_string(fdt
, "/apb-pclk", "compatible", "fixed-clock");
191 qemu_fdt_setprop_cell(fdt
, "/apb-pclk", "#clock-cells", 0x0);
192 qemu_fdt_setprop_cell(fdt
, "/apb-pclk", "clock-frequency", 24000000);
193 qemu_fdt_setprop_string(fdt
, "/apb-pclk", "clock-output-names",
195 qemu_fdt_setprop_cell(fdt
, "/apb-pclk", "phandle", vbi
->clock_phandle
);
199 static void fdt_add_psci_node(const VirtBoardInfo
*vbi
)
201 uint32_t cpu_suspend_fn
;
205 void *fdt
= vbi
->fdt
;
206 ARMCPU
*armcpu
= ARM_CPU(qemu_get_cpu(0));
208 qemu_fdt_add_subnode(fdt
, "/psci");
209 if (armcpu
->psci_version
== 2) {
210 const char comp
[] = "arm,psci-0.2\0arm,psci";
211 qemu_fdt_setprop(fdt
, "/psci", "compatible", comp
, sizeof(comp
));
213 cpu_off_fn
= QEMU_PSCI_0_2_FN_CPU_OFF
;
214 if (arm_feature(&armcpu
->env
, ARM_FEATURE_AARCH64
)) {
215 cpu_suspend_fn
= QEMU_PSCI_0_2_FN64_CPU_SUSPEND
;
216 cpu_on_fn
= QEMU_PSCI_0_2_FN64_CPU_ON
;
217 migrate_fn
= QEMU_PSCI_0_2_FN64_MIGRATE
;
219 cpu_suspend_fn
= QEMU_PSCI_0_2_FN_CPU_SUSPEND
;
220 cpu_on_fn
= QEMU_PSCI_0_2_FN_CPU_ON
;
221 migrate_fn
= QEMU_PSCI_0_2_FN_MIGRATE
;
224 qemu_fdt_setprop_string(fdt
, "/psci", "compatible", "arm,psci");
226 cpu_suspend_fn
= QEMU_PSCI_0_1_FN_CPU_SUSPEND
;
227 cpu_off_fn
= QEMU_PSCI_0_1_FN_CPU_OFF
;
228 cpu_on_fn
= QEMU_PSCI_0_1_FN_CPU_ON
;
229 migrate_fn
= QEMU_PSCI_0_1_FN_MIGRATE
;
232 /* We adopt the PSCI spec's nomenclature, and use 'conduit' to refer
233 * to the instruction that should be used to invoke PSCI functions.
234 * However, the device tree binding uses 'method' instead, so that is
235 * what we should use here.
237 qemu_fdt_setprop_string(fdt
, "/psci", "method", "hvc");
239 qemu_fdt_setprop_cell(fdt
, "/psci", "cpu_suspend", cpu_suspend_fn
);
240 qemu_fdt_setprop_cell(fdt
, "/psci", "cpu_off", cpu_off_fn
);
241 qemu_fdt_setprop_cell(fdt
, "/psci", "cpu_on", cpu_on_fn
);
242 qemu_fdt_setprop_cell(fdt
, "/psci", "migrate", migrate_fn
);
245 static void fdt_add_timer_nodes(const VirtBoardInfo
*vbi
)
247 /* Note that on A15 h/w these interrupts are level-triggered,
248 * but for the GIC implementation provided by both QEMU and KVM
249 * they are edge-triggered.
252 uint32_t irqflags
= GIC_FDT_IRQ_FLAGS_EDGE_LO_HI
;
254 irqflags
= deposit32(irqflags
, GIC_FDT_IRQ_PPI_CPU_START
,
255 GIC_FDT_IRQ_PPI_CPU_WIDTH
, (1 << vbi
->smp_cpus
) - 1);
257 qemu_fdt_add_subnode(vbi
->fdt
, "/timer");
259 armcpu
= ARM_CPU(qemu_get_cpu(0));
260 if (arm_feature(&armcpu
->env
, ARM_FEATURE_V8
)) {
261 const char compat
[] = "arm,armv8-timer\0arm,armv7-timer";
262 qemu_fdt_setprop(vbi
->fdt
, "/timer", "compatible",
263 compat
, sizeof(compat
));
265 qemu_fdt_setprop_string(vbi
->fdt
, "/timer", "compatible",
268 qemu_fdt_setprop_cells(vbi
->fdt
, "/timer", "interrupts",
269 GIC_FDT_IRQ_TYPE_PPI
, ARCH_TIMER_S_EL1_IRQ
, irqflags
,
270 GIC_FDT_IRQ_TYPE_PPI
, ARCH_TIMER_NS_EL1_IRQ
, irqflags
,
271 GIC_FDT_IRQ_TYPE_PPI
, ARCH_TIMER_VIRT_IRQ
, irqflags
,
272 GIC_FDT_IRQ_TYPE_PPI
, ARCH_TIMER_NS_EL2_IRQ
, irqflags
);
275 static void fdt_add_cpu_nodes(const VirtBoardInfo
*vbi
)
279 qemu_fdt_add_subnode(vbi
->fdt
, "/cpus");
280 qemu_fdt_setprop_cell(vbi
->fdt
, "/cpus", "#address-cells", 0x1);
281 qemu_fdt_setprop_cell(vbi
->fdt
, "/cpus", "#size-cells", 0x0);
283 for (cpu
= vbi
->smp_cpus
- 1; cpu
>= 0; cpu
--) {
284 char *nodename
= g_strdup_printf("/cpus/cpu@%d", cpu
);
285 ARMCPU
*armcpu
= ARM_CPU(qemu_get_cpu(cpu
));
287 qemu_fdt_add_subnode(vbi
->fdt
, nodename
);
288 qemu_fdt_setprop_string(vbi
->fdt
, nodename
, "device_type", "cpu");
289 qemu_fdt_setprop_string(vbi
->fdt
, nodename
, "compatible",
290 armcpu
->dtb_compatible
);
292 if (vbi
->smp_cpus
> 1) {
293 qemu_fdt_setprop_string(vbi
->fdt
, nodename
,
294 "enable-method", "psci");
297 qemu_fdt_setprop_cell(vbi
->fdt
, nodename
, "reg", cpu
);
302 static uint32_t fdt_add_gic_node(const VirtBoardInfo
*vbi
)
304 uint32_t gic_phandle
;
306 gic_phandle
= qemu_fdt_alloc_phandle(vbi
->fdt
);
307 qemu_fdt_setprop_cell(vbi
->fdt
, "/", "interrupt-parent", gic_phandle
);
309 qemu_fdt_add_subnode(vbi
->fdt
, "/intc");
310 /* 'cortex-a15-gic' means 'GIC v2' */
311 qemu_fdt_setprop_string(vbi
->fdt
, "/intc", "compatible",
312 "arm,cortex-a15-gic");
313 qemu_fdt_setprop_cell(vbi
->fdt
, "/intc", "#interrupt-cells", 3);
314 qemu_fdt_setprop(vbi
->fdt
, "/intc", "interrupt-controller", NULL
, 0);
315 qemu_fdt_setprop_sized_cells(vbi
->fdt
, "/intc", "reg",
316 2, vbi
->memmap
[VIRT_GIC_DIST
].base
,
317 2, vbi
->memmap
[VIRT_GIC_DIST
].size
,
318 2, vbi
->memmap
[VIRT_GIC_CPU
].base
,
319 2, vbi
->memmap
[VIRT_GIC_CPU
].size
);
320 qemu_fdt_setprop_cell(vbi
->fdt
, "/intc", "phandle", gic_phandle
);
325 static uint32_t create_gic(const VirtBoardInfo
*vbi
, qemu_irq
*pic
)
327 /* We create a standalone GIC v2 */
329 SysBusDevice
*gicbusdev
;
330 const char *gictype
= "arm_gic";
333 if (kvm_irqchip_in_kernel()) {
334 gictype
= "kvm-arm-gic";
337 gicdev
= qdev_create(NULL
, gictype
);
338 qdev_prop_set_uint32(gicdev
, "revision", 2);
339 qdev_prop_set_uint32(gicdev
, "num-cpu", smp_cpus
);
340 /* Note that the num-irq property counts both internal and external
341 * interrupts; there are always 32 of the former (mandated by GIC spec).
343 qdev_prop_set_uint32(gicdev
, "num-irq", NUM_IRQS
+ 32);
344 qdev_init_nofail(gicdev
);
345 gicbusdev
= SYS_BUS_DEVICE(gicdev
);
346 sysbus_mmio_map(gicbusdev
, 0, vbi
->memmap
[VIRT_GIC_DIST
].base
);
347 sysbus_mmio_map(gicbusdev
, 1, vbi
->memmap
[VIRT_GIC_CPU
].base
);
349 /* Wire the outputs from each CPU's generic timer to the
350 * appropriate GIC PPI inputs, and the GIC's IRQ output to
351 * the CPU's IRQ input.
353 for (i
= 0; i
< smp_cpus
; i
++) {
354 DeviceState
*cpudev
= DEVICE(qemu_get_cpu(i
));
355 int ppibase
= NUM_IRQS
+ i
* 32;
356 /* physical timer; we wire it up to the non-secure timer's ID,
357 * since a real A15 always has TrustZone but QEMU doesn't.
359 qdev_connect_gpio_out(cpudev
, 0,
360 qdev_get_gpio_in(gicdev
, ppibase
+ 30));
362 qdev_connect_gpio_out(cpudev
, 1,
363 qdev_get_gpio_in(gicdev
, ppibase
+ 27));
365 sysbus_connect_irq(gicbusdev
, i
, qdev_get_gpio_in(cpudev
, ARM_CPU_IRQ
));
366 sysbus_connect_irq(gicbusdev
, i
+ smp_cpus
,
367 qdev_get_gpio_in(cpudev
, ARM_CPU_FIQ
));
370 for (i
= 0; i
< NUM_IRQS
; i
++) {
371 pic
[i
] = qdev_get_gpio_in(gicdev
, i
);
374 return fdt_add_gic_node(vbi
);
377 static void create_uart(const VirtBoardInfo
*vbi
, qemu_irq
*pic
)
380 hwaddr base
= vbi
->memmap
[VIRT_UART
].base
;
381 hwaddr size
= vbi
->memmap
[VIRT_UART
].size
;
382 int irq
= vbi
->irqmap
[VIRT_UART
];
383 const char compat
[] = "arm,pl011\0arm,primecell";
384 const char clocknames
[] = "uartclk\0apb_pclk";
386 sysbus_create_simple("pl011", base
, pic
[irq
]);
388 nodename
= g_strdup_printf("/pl011@%" PRIx64
, base
);
389 qemu_fdt_add_subnode(vbi
->fdt
, nodename
);
390 /* Note that we can't use setprop_string because of the embedded NUL */
391 qemu_fdt_setprop(vbi
->fdt
, nodename
, "compatible",
392 compat
, sizeof(compat
));
393 qemu_fdt_setprop_sized_cells(vbi
->fdt
, nodename
, "reg",
395 qemu_fdt_setprop_cells(vbi
->fdt
, nodename
, "interrupts",
396 GIC_FDT_IRQ_TYPE_SPI
, irq
,
397 GIC_FDT_IRQ_FLAGS_LEVEL_HI
);
398 qemu_fdt_setprop_cells(vbi
->fdt
, nodename
, "clocks",
399 vbi
->clock_phandle
, vbi
->clock_phandle
);
400 qemu_fdt_setprop(vbi
->fdt
, nodename
, "clock-names",
401 clocknames
, sizeof(clocknames
));
403 qemu_fdt_setprop_string(vbi
->fdt
, "/chosen", "stdout-path", nodename
);
407 static void create_rtc(const VirtBoardInfo
*vbi
, qemu_irq
*pic
)
410 hwaddr base
= vbi
->memmap
[VIRT_RTC
].base
;
411 hwaddr size
= vbi
->memmap
[VIRT_RTC
].size
;
412 int irq
= vbi
->irqmap
[VIRT_RTC
];
413 const char compat
[] = "arm,pl031\0arm,primecell";
415 sysbus_create_simple("pl031", base
, pic
[irq
]);
417 nodename
= g_strdup_printf("/pl031@%" PRIx64
, base
);
418 qemu_fdt_add_subnode(vbi
->fdt
, nodename
);
419 qemu_fdt_setprop(vbi
->fdt
, nodename
, "compatible", compat
, sizeof(compat
));
420 qemu_fdt_setprop_sized_cells(vbi
->fdt
, nodename
, "reg",
422 qemu_fdt_setprop_cells(vbi
->fdt
, nodename
, "interrupts",
423 GIC_FDT_IRQ_TYPE_SPI
, irq
,
424 GIC_FDT_IRQ_FLAGS_LEVEL_HI
);
425 qemu_fdt_setprop_cell(vbi
->fdt
, nodename
, "clocks", vbi
->clock_phandle
);
426 qemu_fdt_setprop_string(vbi
->fdt
, nodename
, "clock-names", "apb_pclk");
430 static void create_virtio_devices(const VirtBoardInfo
*vbi
, qemu_irq
*pic
)
433 hwaddr size
= vbi
->memmap
[VIRT_MMIO
].size
;
435 /* We create the transports in forwards order. Since qbus_realize()
436 * prepends (not appends) new child buses, the incrementing loop below will
437 * create a list of virtio-mmio buses with decreasing base addresses.
439 * When a -device option is processed from the command line,
440 * qbus_find_recursive() picks the next free virtio-mmio bus in forwards
441 * order. The upshot is that -device options in increasing command line
442 * order are mapped to virtio-mmio buses with decreasing base addresses.
444 * When this code was originally written, that arrangement ensured that the
445 * guest Linux kernel would give the lowest "name" (/dev/vda, eth0, etc) to
446 * the first -device on the command line. (The end-to-end order is a
447 * function of this loop, qbus_realize(), qbus_find_recursive(), and the
448 * guest kernel's name-to-address assignment strategy.)
450 * Meanwhile, the kernel's traversal seems to have been reversed; see eg.
451 * the message, if not necessarily the code, of commit 70161ff336.
452 * Therefore the loop now establishes the inverse of the original intent.
454 * Unfortunately, we can't counteract the kernel change by reversing the
455 * loop; it would break existing command lines.
457 * In any case, the kernel makes no guarantee about the stability of
458 * enumeration order of virtio devices (as demonstrated by it changing
459 * between kernel versions). For reliable and stable identification
460 * of disks users must use UUIDs or similar mechanisms.
462 for (i
= 0; i
< NUM_VIRTIO_TRANSPORTS
; i
++) {
463 int irq
= vbi
->irqmap
[VIRT_MMIO
] + i
;
464 hwaddr base
= vbi
->memmap
[VIRT_MMIO
].base
+ i
* size
;
466 sysbus_create_simple("virtio-mmio", base
, pic
[irq
]);
469 /* We add dtb nodes in reverse order so that they appear in the finished
470 * device tree lowest address first.
472 * Note that this mapping is independent of the loop above. The previous
473 * loop influences virtio device to virtio transport assignment, whereas
474 * this loop controls how virtio transports are laid out in the dtb.
476 for (i
= NUM_VIRTIO_TRANSPORTS
- 1; i
>= 0; i
--) {
478 int irq
= vbi
->irqmap
[VIRT_MMIO
] + i
;
479 hwaddr base
= vbi
->memmap
[VIRT_MMIO
].base
+ i
* size
;
481 nodename
= g_strdup_printf("/virtio_mmio@%" PRIx64
, base
);
482 qemu_fdt_add_subnode(vbi
->fdt
, nodename
);
483 qemu_fdt_setprop_string(vbi
->fdt
, nodename
,
484 "compatible", "virtio,mmio");
485 qemu_fdt_setprop_sized_cells(vbi
->fdt
, nodename
, "reg",
487 qemu_fdt_setprop_cells(vbi
->fdt
, nodename
, "interrupts",
488 GIC_FDT_IRQ_TYPE_SPI
, irq
,
489 GIC_FDT_IRQ_FLAGS_EDGE_LO_HI
);
494 static void create_one_flash(const char *name
, hwaddr flashbase
,
497 /* Create and map a single flash device. We use the same
498 * parameters as the flash devices on the Versatile Express board.
500 DriveInfo
*dinfo
= drive_get_next(IF_PFLASH
);
501 DeviceState
*dev
= qdev_create(NULL
, "cfi.pflash01");
502 const uint64_t sectorlength
= 256 * 1024;
505 qdev_prop_set_drive(dev
, "drive", blk_by_legacy_dinfo(dinfo
),
509 qdev_prop_set_uint32(dev
, "num-blocks", flashsize
/ sectorlength
);
510 qdev_prop_set_uint64(dev
, "sector-length", sectorlength
);
511 qdev_prop_set_uint8(dev
, "width", 4);
512 qdev_prop_set_uint8(dev
, "device-width", 2);
513 qdev_prop_set_uint8(dev
, "big-endian", 0);
514 qdev_prop_set_uint16(dev
, "id0", 0x89);
515 qdev_prop_set_uint16(dev
, "id1", 0x18);
516 qdev_prop_set_uint16(dev
, "id2", 0x00);
517 qdev_prop_set_uint16(dev
, "id3", 0x00);
518 qdev_prop_set_string(dev
, "name", name
);
519 qdev_init_nofail(dev
);
521 sysbus_mmio_map(SYS_BUS_DEVICE(dev
), 0, flashbase
);
524 static void create_flash(const VirtBoardInfo
*vbi
)
526 /* Create two flash devices to fill the VIRT_FLASH space in the memmap.
527 * Any file passed via -bios goes in the first of these.
529 hwaddr flashsize
= vbi
->memmap
[VIRT_FLASH
].size
/ 2;
530 hwaddr flashbase
= vbi
->memmap
[VIRT_FLASH
].base
;
537 if (drive_get(IF_PFLASH
, 0, 0)) {
538 error_report("The contents of the first flash device may be "
539 "specified with -bios or with -drive if=pflash... "
540 "but you cannot use both options at once");
543 fn
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, bios_name
);
545 error_report("Could not find ROM image '%s'", bios_name
);
548 image_size
= load_image_targphys(fn
, flashbase
, flashsize
);
550 if (image_size
< 0) {
551 error_report("Could not load ROM image '%s'", bios_name
);
556 create_one_flash("virt.flash0", flashbase
, flashsize
);
557 create_one_flash("virt.flash1", flashbase
+ flashsize
, flashsize
);
559 nodename
= g_strdup_printf("/flash@%" PRIx64
, flashbase
);
560 qemu_fdt_add_subnode(vbi
->fdt
, nodename
);
561 qemu_fdt_setprop_string(vbi
->fdt
, nodename
, "compatible", "cfi-flash");
562 qemu_fdt_setprop_sized_cells(vbi
->fdt
, nodename
, "reg",
563 2, flashbase
, 2, flashsize
,
564 2, flashbase
+ flashsize
, 2, flashsize
);
565 qemu_fdt_setprop_cell(vbi
->fdt
, nodename
, "bank-width", 4);
569 static void create_fw_cfg(const VirtBoardInfo
*vbi
)
571 hwaddr base
= vbi
->memmap
[VIRT_FW_CFG
].base
;
572 hwaddr size
= vbi
->memmap
[VIRT_FW_CFG
].size
;
575 fw_cfg_init_mem_wide(base
+ 8, base
, 8);
577 nodename
= g_strdup_printf("/fw-cfg@%" PRIx64
, base
);
578 qemu_fdt_add_subnode(vbi
->fdt
, nodename
);
579 qemu_fdt_setprop_string(vbi
->fdt
, nodename
,
580 "compatible", "qemu,fw-cfg-mmio");
581 qemu_fdt_setprop_sized_cells(vbi
->fdt
, nodename
, "reg",
586 static void create_pcie_irq_map(const VirtBoardInfo
*vbi
, uint32_t gic_phandle
,
587 int first_irq
, const char *nodename
)
590 uint32_t full_irq_map
[4 * 4 * 8] = { 0 };
591 uint32_t *irq_map
= full_irq_map
;
593 for (devfn
= 0; devfn
<= 0x18; devfn
+= 0x8) {
594 for (pin
= 0; pin
< 4; pin
++) {
595 int irq_type
= GIC_FDT_IRQ_TYPE_SPI
;
596 int irq_nr
= first_irq
+ ((pin
+ PCI_SLOT(devfn
)) % PCI_NUM_PINS
);
597 int irq_level
= GIC_FDT_IRQ_FLAGS_LEVEL_HI
;
601 devfn
<< 8, 0, 0, /* devfn */
602 pin
+ 1, /* PCI pin */
603 gic_phandle
, irq_type
, irq_nr
, irq_level
}; /* GIC irq */
605 /* Convert map to big endian */
606 for (i
= 0; i
< 8; i
++) {
607 irq_map
[i
] = cpu_to_be32(map
[i
]);
613 qemu_fdt_setprop(vbi
->fdt
, nodename
, "interrupt-map",
614 full_irq_map
, sizeof(full_irq_map
));
616 qemu_fdt_setprop_cells(vbi
->fdt
, nodename
, "interrupt-map-mask",
617 0x1800, 0, 0, /* devfn (PCI_SLOT(3)) */
621 static void create_pcie(const VirtBoardInfo
*vbi
, qemu_irq
*pic
,
622 uint32_t gic_phandle
)
624 hwaddr base_mmio
= vbi
->memmap
[VIRT_PCIE_MMIO
].base
;
625 hwaddr size_mmio
= vbi
->memmap
[VIRT_PCIE_MMIO
].size
;
626 hwaddr base_pio
= vbi
->memmap
[VIRT_PCIE_PIO
].base
;
627 hwaddr size_pio
= vbi
->memmap
[VIRT_PCIE_PIO
].size
;
628 hwaddr base_ecam
= vbi
->memmap
[VIRT_PCIE_ECAM
].base
;
629 hwaddr size_ecam
= vbi
->memmap
[VIRT_PCIE_ECAM
].size
;
630 hwaddr base
= base_mmio
;
631 int nr_pcie_buses
= size_ecam
/ PCIE_MMCFG_SIZE_MIN
;
632 int irq
= vbi
->irqmap
[VIRT_PCIE
];
633 MemoryRegion
*mmio_alias
;
634 MemoryRegion
*mmio_reg
;
635 MemoryRegion
*ecam_alias
;
636 MemoryRegion
*ecam_reg
;
641 dev
= qdev_create(NULL
, TYPE_GPEX_HOST
);
642 qdev_init_nofail(dev
);
644 /* Map only the first size_ecam bytes of ECAM space */
645 ecam_alias
= g_new0(MemoryRegion
, 1);
646 ecam_reg
= sysbus_mmio_get_region(SYS_BUS_DEVICE(dev
), 0);
647 memory_region_init_alias(ecam_alias
, OBJECT(dev
), "pcie-ecam",
648 ecam_reg
, 0, size_ecam
);
649 memory_region_add_subregion(get_system_memory(), base_ecam
, ecam_alias
);
651 /* Map the MMIO window into system address space so as to expose
652 * the section of PCI MMIO space which starts at the same base address
653 * (ie 1:1 mapping for that part of PCI MMIO space visible through
656 mmio_alias
= g_new0(MemoryRegion
, 1);
657 mmio_reg
= sysbus_mmio_get_region(SYS_BUS_DEVICE(dev
), 1);
658 memory_region_init_alias(mmio_alias
, OBJECT(dev
), "pcie-mmio",
659 mmio_reg
, base_mmio
, size_mmio
);
660 memory_region_add_subregion(get_system_memory(), base_mmio
, mmio_alias
);
662 /* Map IO port space */
663 sysbus_mmio_map(SYS_BUS_DEVICE(dev
), 2, base_pio
);
665 for (i
= 0; i
< GPEX_NUM_IRQS
; i
++) {
666 sysbus_connect_irq(SYS_BUS_DEVICE(dev
), i
, pic
[irq
+ i
]);
669 nodename
= g_strdup_printf("/pcie@%" PRIx64
, base
);
670 qemu_fdt_add_subnode(vbi
->fdt
, nodename
);
671 qemu_fdt_setprop_string(vbi
->fdt
, nodename
,
672 "compatible", "pci-host-ecam-generic");
673 qemu_fdt_setprop_string(vbi
->fdt
, nodename
, "device_type", "pci");
674 qemu_fdt_setprop_cell(vbi
->fdt
, nodename
, "#address-cells", 3);
675 qemu_fdt_setprop_cell(vbi
->fdt
, nodename
, "#size-cells", 2);
676 qemu_fdt_setprop_cells(vbi
->fdt
, nodename
, "bus-range", 0,
679 qemu_fdt_setprop_sized_cells(vbi
->fdt
, nodename
, "reg",
680 2, base_ecam
, 2, size_ecam
);
681 qemu_fdt_setprop_sized_cells(vbi
->fdt
, nodename
, "ranges",
682 1, FDT_PCI_RANGE_IOPORT
, 2, 0,
683 2, base_pio
, 2, size_pio
,
684 1, FDT_PCI_RANGE_MMIO
, 2, base_mmio
,
685 2, base_mmio
, 2, size_mmio
);
687 qemu_fdt_setprop_cell(vbi
->fdt
, nodename
, "#interrupt-cells", 1);
688 create_pcie_irq_map(vbi
, gic_phandle
, irq
, nodename
);
693 static void *machvirt_dtb(const struct arm_boot_info
*binfo
, int *fdt_size
)
695 const VirtBoardInfo
*board
= (const VirtBoardInfo
*)binfo
;
697 *fdt_size
= board
->fdt_size
;
702 void virt_guest_info_machine_done(Notifier
*notifier
, void *data
)
704 VirtGuestInfoState
*guest_info_state
= container_of(notifier
,
705 VirtGuestInfoState
, machine_done
);
706 virt_acpi_setup(&guest_info_state
->info
);
709 static void machvirt_init(MachineState
*machine
)
711 VirtMachineState
*vms
= VIRT_MACHINE(machine
);
712 qemu_irq pic
[NUM_IRQS
];
713 MemoryRegion
*sysmem
= get_system_memory();
715 MemoryRegion
*ram
= g_new(MemoryRegion
, 1);
716 const char *cpu_model
= machine
->cpu_model
;
718 VirtGuestInfoState
*guest_info_state
= g_malloc0(sizeof *guest_info_state
);
719 VirtGuestInfo
*guest_info
= &guest_info_state
->info
;
720 uint32_t gic_phandle
;
724 cpu_model
= "cortex-a15";
727 /* Separate the actual CPU model name from any appended features */
728 cpustr
= g_strsplit(cpu_model
, ",", 2);
730 vbi
= find_machine_info(cpustr
[0]);
733 error_report("mach-virt: CPU %s not supported", cpustr
[0]);
737 vbi
->smp_cpus
= smp_cpus
;
739 if (machine
->ram_size
> vbi
->memmap
[VIRT_MEM
].size
) {
740 error_report("mach-virt: cannot model more than 30GB RAM");
746 for (n
= 0; n
< smp_cpus
; n
++) {
747 ObjectClass
*oc
= cpu_class_by_name(TYPE_ARM_CPU
, cpustr
[0]);
748 CPUClass
*cc
= CPU_CLASS(oc
);
751 char *cpuopts
= g_strdup(cpustr
[1]);
754 fprintf(stderr
, "Unable to find CPU definition\n");
757 cpuobj
= object_new(object_class_get_name(oc
));
759 /* Handle any CPU options specified by the user */
760 cc
->parse_features(CPU(cpuobj
), cpuopts
, &err
);
763 error_report_err(err
);
768 object_property_set_bool(cpuobj
, false, "has_el3", NULL
);
771 object_property_set_int(cpuobj
, QEMU_PSCI_CONDUIT_HVC
, "psci-conduit",
774 /* Secondary CPUs start in PSCI powered-down state */
776 object_property_set_bool(cpuobj
, true, "start-powered-off", NULL
);
779 if (object_property_find(cpuobj
, "reset-cbar", NULL
)) {
780 object_property_set_int(cpuobj
, vbi
->memmap
[VIRT_CPUPERIPHS
].base
,
781 "reset-cbar", &error_abort
);
784 object_property_set_bool(cpuobj
, true, "realized", NULL
);
787 fdt_add_timer_nodes(vbi
);
788 fdt_add_cpu_nodes(vbi
);
789 fdt_add_psci_node(vbi
);
791 memory_region_allocate_system_memory(ram
, NULL
, "mach-virt.ram",
793 memory_region_add_subregion(sysmem
, vbi
->memmap
[VIRT_MEM
].base
, ram
);
797 gic_phandle
= create_gic(vbi
, pic
);
799 create_uart(vbi
, pic
);
801 create_rtc(vbi
, pic
);
803 create_pcie(vbi
, pic
, gic_phandle
);
805 /* Create mmio transports, so the user can create virtio backends
806 * (which will be automatically plugged in to the transports). If
807 * no backend is created the transport will just sit harmlessly idle.
809 create_virtio_devices(vbi
, pic
);
812 rom_set_fw(fw_cfg_find());
814 guest_info
->smp_cpus
= smp_cpus
;
815 guest_info
->fw_cfg
= fw_cfg_find();
816 guest_info
->memmap
= vbi
->memmap
;
817 guest_info
->irqmap
= vbi
->irqmap
;
818 guest_info_state
->machine_done
.notify
= virt_guest_info_machine_done
;
819 qemu_add_machine_init_done_notifier(&guest_info_state
->machine_done
);
821 vbi
->bootinfo
.ram_size
= machine
->ram_size
;
822 vbi
->bootinfo
.kernel_filename
= machine
->kernel_filename
;
823 vbi
->bootinfo
.kernel_cmdline
= machine
->kernel_cmdline
;
824 vbi
->bootinfo
.initrd_filename
= machine
->initrd_filename
;
825 vbi
->bootinfo
.nb_cpus
= smp_cpus
;
826 vbi
->bootinfo
.board_id
= -1;
827 vbi
->bootinfo
.loader_start
= vbi
->memmap
[VIRT_MEM
].base
;
828 vbi
->bootinfo
.get_dtb
= machvirt_dtb
;
829 vbi
->bootinfo
.firmware_loaded
= bios_name
|| drive_get(IF_PFLASH
, 0, 0);
830 arm_load_kernel(ARM_CPU(first_cpu
), &vbi
->bootinfo
);
833 static bool virt_get_secure(Object
*obj
, Error
**errp
)
835 VirtMachineState
*vms
= VIRT_MACHINE(obj
);
840 static void virt_set_secure(Object
*obj
, bool value
, Error
**errp
)
842 VirtMachineState
*vms
= VIRT_MACHINE(obj
);
847 static void virt_instance_init(Object
*obj
)
849 VirtMachineState
*vms
= VIRT_MACHINE(obj
);
851 /* EL3 is enabled by default on virt */
853 object_property_add_bool(obj
, "secure", virt_get_secure
,
854 virt_set_secure
, NULL
);
855 object_property_set_description(obj
, "secure",
856 "Set on/off to enable/disable the ARM "
857 "Security Extensions (TrustZone)",
861 static void virt_class_init(ObjectClass
*oc
, void *data
)
863 MachineClass
*mc
= MACHINE_CLASS(oc
);
865 mc
->name
= TYPE_VIRT_MACHINE
;
866 mc
->desc
= "ARM Virtual Machine",
867 mc
->init
= machvirt_init
;
871 static const TypeInfo machvirt_info
= {
872 .name
= TYPE_VIRT_MACHINE
,
873 .parent
= TYPE_MACHINE
,
874 .instance_size
= sizeof(VirtMachineState
),
875 .instance_init
= virt_instance_init
,
876 .class_size
= sizeof(VirtMachineClass
),
877 .class_init
= virt_class_init
,
880 static void machvirt_machine_init(void)
882 type_register_static(&machvirt_info
);
885 machine_init(machvirt_machine_init
);