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