machine: Set MachineClass::name automatically
[qemu/cris-port.git] / hw / arm / virt.c
blob6bf0d6d591d6c6456b731819cb1e53eae69e4e11
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"
51 #include "hw/intc/arm_gic_common.h"
52 #include "kvm_arm.h"
53 #include "hw/smbios/smbios.h"
55 /* Number of external interrupt lines to configure the GIC with */
56 #define NUM_IRQS 256
58 #define PLATFORM_BUS_NUM_IRQS 64
60 static ARMPlatformBusSystemParams platform_bus_params;
62 typedef struct VirtBoardInfo {
63 struct arm_boot_info bootinfo;
64 const char *cpu_model;
65 const MemMapEntry *memmap;
66 const int *irqmap;
67 int smp_cpus;
68 void *fdt;
69 int fdt_size;
70 uint32_t clock_phandle;
71 uint32_t gic_phandle;
72 uint32_t v2m_phandle;
73 } VirtBoardInfo;
75 typedef struct {
76 MachineClass parent;
77 VirtBoardInfo *daughterboard;
78 } VirtMachineClass;
80 typedef struct {
81 MachineState parent;
82 bool secure;
83 bool highmem;
84 } VirtMachineState;
86 #define TYPE_VIRT_MACHINE MACHINE_TYPE_NAME("virt")
87 #define VIRT_MACHINE(obj) \
88 OBJECT_CHECK(VirtMachineState, (obj), TYPE_VIRT_MACHINE)
89 #define VIRT_MACHINE_GET_CLASS(obj) \
90 OBJECT_GET_CLASS(VirtMachineClass, obj, TYPE_VIRT_MACHINE)
91 #define VIRT_MACHINE_CLASS(klass) \
92 OBJECT_CLASS_CHECK(VirtMachineClass, klass, TYPE_VIRT_MACHINE)
94 /* Addresses and sizes of our components.
95 * 0..128MB is space for a flash device so we can run bootrom code such as UEFI.
96 * 128MB..256MB is used for miscellaneous device I/O.
97 * 256MB..1GB is reserved for possible future PCI support (ie where the
98 * PCI memory window will go if we add a PCI host controller).
99 * 1GB and up is RAM (which may happily spill over into the
100 * high memory region beyond 4GB).
101 * This represents a compromise between how much RAM can be given to
102 * a 32 bit VM and leaving space for expansion and in particular for PCI.
103 * Note that devices should generally be placed at multiples of 0x10000,
104 * to accommodate guests using 64K pages.
106 static const MemMapEntry a15memmap[] = {
107 /* Space up to 0x8000000 is reserved for a boot ROM */
108 [VIRT_FLASH] = { 0, 0x08000000 },
109 [VIRT_CPUPERIPHS] = { 0x08000000, 0x00020000 },
110 /* GIC distributor and CPU interfaces sit inside the CPU peripheral space */
111 [VIRT_GIC_DIST] = { 0x08000000, 0x00010000 },
112 [VIRT_GIC_CPU] = { 0x08010000, 0x00010000 },
113 [VIRT_GIC_V2M] = { 0x08020000, 0x00001000 },
114 [VIRT_UART] = { 0x09000000, 0x00001000 },
115 [VIRT_RTC] = { 0x09010000, 0x00001000 },
116 [VIRT_FW_CFG] = { 0x09020000, 0x0000000a },
117 [VIRT_MMIO] = { 0x0a000000, 0x00000200 },
118 /* ...repeating for a total of NUM_VIRTIO_TRANSPORTS, each of that size */
119 [VIRT_PLATFORM_BUS] = { 0x0c000000, 0x02000000 },
120 [VIRT_PCIE_MMIO] = { 0x10000000, 0x2eff0000 },
121 [VIRT_PCIE_PIO] = { 0x3eff0000, 0x00010000 },
122 [VIRT_PCIE_ECAM] = { 0x3f000000, 0x01000000 },
123 [VIRT_MEM] = { 0x40000000, 30ULL * 1024 * 1024 * 1024 },
124 /* Second PCIe window, 512GB wide at the 512GB boundary */
125 [VIRT_PCIE_MMIO_HIGH] = { 0x8000000000ULL, 0x8000000000ULL },
128 static const int a15irqmap[] = {
129 [VIRT_UART] = 1,
130 [VIRT_RTC] = 2,
131 [VIRT_PCIE] = 3, /* ... to 6 */
132 [VIRT_MMIO] = 16, /* ...to 16 + NUM_VIRTIO_TRANSPORTS - 1 */
133 [VIRT_GIC_V2M] = 48, /* ...to 48 + NUM_GICV2M_SPIS - 1 */
134 [VIRT_PLATFORM_BUS] = 112, /* ...to 112 + PLATFORM_BUS_NUM_IRQS -1 */
137 static VirtBoardInfo machines[] = {
139 .cpu_model = "cortex-a15",
140 .memmap = a15memmap,
141 .irqmap = a15irqmap,
144 .cpu_model = "cortex-a53",
145 .memmap = a15memmap,
146 .irqmap = a15irqmap,
149 .cpu_model = "cortex-a57",
150 .memmap = a15memmap,
151 .irqmap = a15irqmap,
154 .cpu_model = "host",
155 .memmap = a15memmap,
156 .irqmap = a15irqmap,
160 static VirtBoardInfo *find_machine_info(const char *cpu)
162 int i;
164 for (i = 0; i < ARRAY_SIZE(machines); i++) {
165 if (strcmp(cpu, machines[i].cpu_model) == 0) {
166 return &machines[i];
169 return NULL;
172 static void create_fdt(VirtBoardInfo *vbi)
174 void *fdt = create_device_tree(&vbi->fdt_size);
176 if (!fdt) {
177 error_report("create_device_tree() failed");
178 exit(1);
181 vbi->fdt = fdt;
183 /* Header */
184 qemu_fdt_setprop_string(fdt, "/", "compatible", "linux,dummy-virt");
185 qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2);
186 qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2);
189 * /chosen and /memory nodes must exist for load_dtb
190 * to fill in necessary properties later
192 qemu_fdt_add_subnode(fdt, "/chosen");
193 qemu_fdt_add_subnode(fdt, "/memory");
194 qemu_fdt_setprop_string(fdt, "/memory", "device_type", "memory");
196 /* Clock node, for the benefit of the UART. The kernel device tree
197 * binding documentation claims the PL011 node clock properties are
198 * optional but in practice if you omit them the kernel refuses to
199 * probe for the device.
201 vbi->clock_phandle = qemu_fdt_alloc_phandle(fdt);
202 qemu_fdt_add_subnode(fdt, "/apb-pclk");
203 qemu_fdt_setprop_string(fdt, "/apb-pclk", "compatible", "fixed-clock");
204 qemu_fdt_setprop_cell(fdt, "/apb-pclk", "#clock-cells", 0x0);
205 qemu_fdt_setprop_cell(fdt, "/apb-pclk", "clock-frequency", 24000000);
206 qemu_fdt_setprop_string(fdt, "/apb-pclk", "clock-output-names",
207 "clk24mhz");
208 qemu_fdt_setprop_cell(fdt, "/apb-pclk", "phandle", vbi->clock_phandle);
212 static void fdt_add_psci_node(const VirtBoardInfo *vbi)
214 uint32_t cpu_suspend_fn;
215 uint32_t cpu_off_fn;
216 uint32_t cpu_on_fn;
217 uint32_t migrate_fn;
218 void *fdt = vbi->fdt;
219 ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(0));
221 qemu_fdt_add_subnode(fdt, "/psci");
222 if (armcpu->psci_version == 2) {
223 const char comp[] = "arm,psci-0.2\0arm,psci";
224 qemu_fdt_setprop(fdt, "/psci", "compatible", comp, sizeof(comp));
226 cpu_off_fn = QEMU_PSCI_0_2_FN_CPU_OFF;
227 if (arm_feature(&armcpu->env, ARM_FEATURE_AARCH64)) {
228 cpu_suspend_fn = QEMU_PSCI_0_2_FN64_CPU_SUSPEND;
229 cpu_on_fn = QEMU_PSCI_0_2_FN64_CPU_ON;
230 migrate_fn = QEMU_PSCI_0_2_FN64_MIGRATE;
231 } else {
232 cpu_suspend_fn = QEMU_PSCI_0_2_FN_CPU_SUSPEND;
233 cpu_on_fn = QEMU_PSCI_0_2_FN_CPU_ON;
234 migrate_fn = QEMU_PSCI_0_2_FN_MIGRATE;
236 } else {
237 qemu_fdt_setprop_string(fdt, "/psci", "compatible", "arm,psci");
239 cpu_suspend_fn = QEMU_PSCI_0_1_FN_CPU_SUSPEND;
240 cpu_off_fn = QEMU_PSCI_0_1_FN_CPU_OFF;
241 cpu_on_fn = QEMU_PSCI_0_1_FN_CPU_ON;
242 migrate_fn = QEMU_PSCI_0_1_FN_MIGRATE;
245 /* We adopt the PSCI spec's nomenclature, and use 'conduit' to refer
246 * to the instruction that should be used to invoke PSCI functions.
247 * However, the device tree binding uses 'method' instead, so that is
248 * what we should use here.
250 qemu_fdt_setprop_string(fdt, "/psci", "method", "hvc");
252 qemu_fdt_setprop_cell(fdt, "/psci", "cpu_suspend", cpu_suspend_fn);
253 qemu_fdt_setprop_cell(fdt, "/psci", "cpu_off", cpu_off_fn);
254 qemu_fdt_setprop_cell(fdt, "/psci", "cpu_on", cpu_on_fn);
255 qemu_fdt_setprop_cell(fdt, "/psci", "migrate", migrate_fn);
258 static void fdt_add_timer_nodes(const VirtBoardInfo *vbi)
260 /* Note that on A15 h/w these interrupts are level-triggered,
261 * but for the GIC implementation provided by both QEMU and KVM
262 * they are edge-triggered.
264 ARMCPU *armcpu;
265 uint32_t irqflags = GIC_FDT_IRQ_FLAGS_EDGE_LO_HI;
267 irqflags = deposit32(irqflags, GIC_FDT_IRQ_PPI_CPU_START,
268 GIC_FDT_IRQ_PPI_CPU_WIDTH, (1 << vbi->smp_cpus) - 1);
270 qemu_fdt_add_subnode(vbi->fdt, "/timer");
272 armcpu = ARM_CPU(qemu_get_cpu(0));
273 if (arm_feature(&armcpu->env, ARM_FEATURE_V8)) {
274 const char compat[] = "arm,armv8-timer\0arm,armv7-timer";
275 qemu_fdt_setprop(vbi->fdt, "/timer", "compatible",
276 compat, sizeof(compat));
277 } else {
278 qemu_fdt_setprop_string(vbi->fdt, "/timer", "compatible",
279 "arm,armv7-timer");
281 qemu_fdt_setprop_cells(vbi->fdt, "/timer", "interrupts",
282 GIC_FDT_IRQ_TYPE_PPI, ARCH_TIMER_S_EL1_IRQ, irqflags,
283 GIC_FDT_IRQ_TYPE_PPI, ARCH_TIMER_NS_EL1_IRQ, irqflags,
284 GIC_FDT_IRQ_TYPE_PPI, ARCH_TIMER_VIRT_IRQ, irqflags,
285 GIC_FDT_IRQ_TYPE_PPI, ARCH_TIMER_NS_EL2_IRQ, irqflags);
288 static void fdt_add_cpu_nodes(const VirtBoardInfo *vbi)
290 int cpu;
291 int addr_cells = 1;
294 * From Documentation/devicetree/bindings/arm/cpus.txt
295 * On ARM v8 64-bit systems value should be set to 2,
296 * that corresponds to the MPIDR_EL1 register size.
297 * If MPIDR_EL1[63:32] value is equal to 0 on all CPUs
298 * in the system, #address-cells can be set to 1, since
299 * MPIDR_EL1[63:32] bits are not used for CPUs
300 * identification.
302 * Here we actually don't know whether our system is 32- or 64-bit one.
303 * The simplest way to go is to examine affinity IDs of all our CPUs. If
304 * at least one of them has Aff3 populated, we set #address-cells to 2.
306 for (cpu = 0; cpu < vbi->smp_cpus; cpu++) {
307 ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(cpu));
309 if (armcpu->mp_affinity & ARM_AFF3_MASK) {
310 addr_cells = 2;
311 break;
315 qemu_fdt_add_subnode(vbi->fdt, "/cpus");
316 qemu_fdt_setprop_cell(vbi->fdt, "/cpus", "#address-cells", addr_cells);
317 qemu_fdt_setprop_cell(vbi->fdt, "/cpus", "#size-cells", 0x0);
319 for (cpu = vbi->smp_cpus - 1; cpu >= 0; cpu--) {
320 char *nodename = g_strdup_printf("/cpus/cpu@%d", cpu);
321 ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(cpu));
323 qemu_fdt_add_subnode(vbi->fdt, nodename);
324 qemu_fdt_setprop_string(vbi->fdt, nodename, "device_type", "cpu");
325 qemu_fdt_setprop_string(vbi->fdt, nodename, "compatible",
326 armcpu->dtb_compatible);
328 if (vbi->smp_cpus > 1) {
329 qemu_fdt_setprop_string(vbi->fdt, nodename,
330 "enable-method", "psci");
333 if (addr_cells == 2) {
334 qemu_fdt_setprop_u64(vbi->fdt, nodename, "reg",
335 armcpu->mp_affinity);
336 } else {
337 qemu_fdt_setprop_cell(vbi->fdt, nodename, "reg",
338 armcpu->mp_affinity);
341 g_free(nodename);
345 static void fdt_add_v2m_gic_node(VirtBoardInfo *vbi)
347 vbi->v2m_phandle = qemu_fdt_alloc_phandle(vbi->fdt);
348 qemu_fdt_add_subnode(vbi->fdt, "/intc/v2m");
349 qemu_fdt_setprop_string(vbi->fdt, "/intc/v2m", "compatible",
350 "arm,gic-v2m-frame");
351 qemu_fdt_setprop(vbi->fdt, "/intc/v2m", "msi-controller", NULL, 0);
352 qemu_fdt_setprop_sized_cells(vbi->fdt, "/intc/v2m", "reg",
353 2, vbi->memmap[VIRT_GIC_V2M].base,
354 2, vbi->memmap[VIRT_GIC_V2M].size);
355 qemu_fdt_setprop_cell(vbi->fdt, "/intc/v2m", "phandle", vbi->v2m_phandle);
358 static void fdt_add_gic_node(VirtBoardInfo *vbi)
360 vbi->gic_phandle = qemu_fdt_alloc_phandle(vbi->fdt);
361 qemu_fdt_setprop_cell(vbi->fdt, "/", "interrupt-parent", vbi->gic_phandle);
363 qemu_fdt_add_subnode(vbi->fdt, "/intc");
364 /* 'cortex-a15-gic' means 'GIC v2' */
365 qemu_fdt_setprop_string(vbi->fdt, "/intc", "compatible",
366 "arm,cortex-a15-gic");
367 qemu_fdt_setprop_cell(vbi->fdt, "/intc", "#interrupt-cells", 3);
368 qemu_fdt_setprop(vbi->fdt, "/intc", "interrupt-controller", NULL, 0);
369 qemu_fdt_setprop_sized_cells(vbi->fdt, "/intc", "reg",
370 2, vbi->memmap[VIRT_GIC_DIST].base,
371 2, vbi->memmap[VIRT_GIC_DIST].size,
372 2, vbi->memmap[VIRT_GIC_CPU].base,
373 2, vbi->memmap[VIRT_GIC_CPU].size);
374 qemu_fdt_setprop_cell(vbi->fdt, "/intc", "#address-cells", 0x2);
375 qemu_fdt_setprop_cell(vbi->fdt, "/intc", "#size-cells", 0x2);
376 qemu_fdt_setprop(vbi->fdt, "/intc", "ranges", NULL, 0);
377 qemu_fdt_setprop_cell(vbi->fdt, "/intc", "phandle", vbi->gic_phandle);
380 static void create_v2m(VirtBoardInfo *vbi, qemu_irq *pic)
382 int i;
383 int irq = vbi->irqmap[VIRT_GIC_V2M];
384 DeviceState *dev;
386 dev = qdev_create(NULL, "arm-gicv2m");
387 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, vbi->memmap[VIRT_GIC_V2M].base);
388 qdev_prop_set_uint32(dev, "base-spi", irq);
389 qdev_prop_set_uint32(dev, "num-spi", NUM_GICV2M_SPIS);
390 qdev_init_nofail(dev);
392 for (i = 0; i < NUM_GICV2M_SPIS; i++) {
393 sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, pic[irq + i]);
396 fdt_add_v2m_gic_node(vbi);
399 static void create_gic(VirtBoardInfo *vbi, qemu_irq *pic, bool secure)
401 /* We create a standalone GIC v2 */
402 DeviceState *gicdev;
403 SysBusDevice *gicbusdev;
404 const char *gictype;
405 int i;
407 gictype = gic_class_name();
409 gicdev = qdev_create(NULL, gictype);
410 qdev_prop_set_uint32(gicdev, "revision", 2);
411 qdev_prop_set_uint32(gicdev, "num-cpu", smp_cpus);
412 /* Note that the num-irq property counts both internal and external
413 * interrupts; there are always 32 of the former (mandated by GIC spec).
415 qdev_prop_set_uint32(gicdev, "num-irq", NUM_IRQS + 32);
416 if (!kvm_irqchip_in_kernel()) {
417 qdev_prop_set_bit(gicdev, "has-security-extensions", secure);
419 qdev_init_nofail(gicdev);
420 gicbusdev = SYS_BUS_DEVICE(gicdev);
421 sysbus_mmio_map(gicbusdev, 0, vbi->memmap[VIRT_GIC_DIST].base);
422 sysbus_mmio_map(gicbusdev, 1, vbi->memmap[VIRT_GIC_CPU].base);
424 /* Wire the outputs from each CPU's generic timer to the
425 * appropriate GIC PPI inputs, and the GIC's IRQ output to
426 * the CPU's IRQ input.
428 for (i = 0; i < smp_cpus; i++) {
429 DeviceState *cpudev = DEVICE(qemu_get_cpu(i));
430 int ppibase = NUM_IRQS + i * GIC_INTERNAL + GIC_NR_SGIS;
431 int irq;
432 /* Mapping from the output timer irq lines from the CPU to the
433 * GIC PPI inputs we use for the virt board.
435 const int timer_irq[] = {
436 [GTIMER_PHYS] = ARCH_TIMER_NS_EL1_IRQ,
437 [GTIMER_VIRT] = ARCH_TIMER_VIRT_IRQ,
438 [GTIMER_HYP] = ARCH_TIMER_NS_EL2_IRQ,
439 [GTIMER_SEC] = ARCH_TIMER_S_EL1_IRQ,
442 for (irq = 0; irq < ARRAY_SIZE(timer_irq); irq++) {
443 qdev_connect_gpio_out(cpudev, irq,
444 qdev_get_gpio_in(gicdev,
445 ppibase + timer_irq[irq]));
448 sysbus_connect_irq(gicbusdev, i, qdev_get_gpio_in(cpudev, ARM_CPU_IRQ));
449 sysbus_connect_irq(gicbusdev, i + smp_cpus,
450 qdev_get_gpio_in(cpudev, ARM_CPU_FIQ));
453 for (i = 0; i < NUM_IRQS; i++) {
454 pic[i] = qdev_get_gpio_in(gicdev, i);
457 fdt_add_gic_node(vbi);
459 create_v2m(vbi, pic);
462 static void create_uart(const VirtBoardInfo *vbi, qemu_irq *pic)
464 char *nodename;
465 hwaddr base = vbi->memmap[VIRT_UART].base;
466 hwaddr size = vbi->memmap[VIRT_UART].size;
467 int irq = vbi->irqmap[VIRT_UART];
468 const char compat[] = "arm,pl011\0arm,primecell";
469 const char clocknames[] = "uartclk\0apb_pclk";
471 sysbus_create_simple("pl011", base, pic[irq]);
473 nodename = g_strdup_printf("/pl011@%" PRIx64, base);
474 qemu_fdt_add_subnode(vbi->fdt, nodename);
475 /* Note that we can't use setprop_string because of the embedded NUL */
476 qemu_fdt_setprop(vbi->fdt, nodename, "compatible",
477 compat, sizeof(compat));
478 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg",
479 2, base, 2, size);
480 qemu_fdt_setprop_cells(vbi->fdt, nodename, "interrupts",
481 GIC_FDT_IRQ_TYPE_SPI, irq,
482 GIC_FDT_IRQ_FLAGS_LEVEL_HI);
483 qemu_fdt_setprop_cells(vbi->fdt, nodename, "clocks",
484 vbi->clock_phandle, vbi->clock_phandle);
485 qemu_fdt_setprop(vbi->fdt, nodename, "clock-names",
486 clocknames, sizeof(clocknames));
488 qemu_fdt_setprop_string(vbi->fdt, "/chosen", "stdout-path", nodename);
489 g_free(nodename);
492 static void create_rtc(const VirtBoardInfo *vbi, qemu_irq *pic)
494 char *nodename;
495 hwaddr base = vbi->memmap[VIRT_RTC].base;
496 hwaddr size = vbi->memmap[VIRT_RTC].size;
497 int irq = vbi->irqmap[VIRT_RTC];
498 const char compat[] = "arm,pl031\0arm,primecell";
500 sysbus_create_simple("pl031", base, pic[irq]);
502 nodename = g_strdup_printf("/pl031@%" PRIx64, base);
503 qemu_fdt_add_subnode(vbi->fdt, nodename);
504 qemu_fdt_setprop(vbi->fdt, nodename, "compatible", compat, sizeof(compat));
505 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg",
506 2, base, 2, size);
507 qemu_fdt_setprop_cells(vbi->fdt, nodename, "interrupts",
508 GIC_FDT_IRQ_TYPE_SPI, irq,
509 GIC_FDT_IRQ_FLAGS_LEVEL_HI);
510 qemu_fdt_setprop_cell(vbi->fdt, nodename, "clocks", vbi->clock_phandle);
511 qemu_fdt_setprop_string(vbi->fdt, nodename, "clock-names", "apb_pclk");
512 g_free(nodename);
515 static void create_virtio_devices(const VirtBoardInfo *vbi, qemu_irq *pic)
517 int i;
518 hwaddr size = vbi->memmap[VIRT_MMIO].size;
520 /* We create the transports in forwards order. Since qbus_realize()
521 * prepends (not appends) new child buses, the incrementing loop below will
522 * create a list of virtio-mmio buses with decreasing base addresses.
524 * When a -device option is processed from the command line,
525 * qbus_find_recursive() picks the next free virtio-mmio bus in forwards
526 * order. The upshot is that -device options in increasing command line
527 * order are mapped to virtio-mmio buses with decreasing base addresses.
529 * When this code was originally written, that arrangement ensured that the
530 * guest Linux kernel would give the lowest "name" (/dev/vda, eth0, etc) to
531 * the first -device on the command line. (The end-to-end order is a
532 * function of this loop, qbus_realize(), qbus_find_recursive(), and the
533 * guest kernel's name-to-address assignment strategy.)
535 * Meanwhile, the kernel's traversal seems to have been reversed; see eg.
536 * the message, if not necessarily the code, of commit 70161ff336.
537 * Therefore the loop now establishes the inverse of the original intent.
539 * Unfortunately, we can't counteract the kernel change by reversing the
540 * loop; it would break existing command lines.
542 * In any case, the kernel makes no guarantee about the stability of
543 * enumeration order of virtio devices (as demonstrated by it changing
544 * between kernel versions). For reliable and stable identification
545 * of disks users must use UUIDs or similar mechanisms.
547 for (i = 0; i < NUM_VIRTIO_TRANSPORTS; i++) {
548 int irq = vbi->irqmap[VIRT_MMIO] + i;
549 hwaddr base = vbi->memmap[VIRT_MMIO].base + i * size;
551 sysbus_create_simple("virtio-mmio", base, pic[irq]);
554 /* We add dtb nodes in reverse order so that they appear in the finished
555 * device tree lowest address first.
557 * Note that this mapping is independent of the loop above. The previous
558 * loop influences virtio device to virtio transport assignment, whereas
559 * this loop controls how virtio transports are laid out in the dtb.
561 for (i = NUM_VIRTIO_TRANSPORTS - 1; i >= 0; i--) {
562 char *nodename;
563 int irq = vbi->irqmap[VIRT_MMIO] + i;
564 hwaddr base = vbi->memmap[VIRT_MMIO].base + i * size;
566 nodename = g_strdup_printf("/virtio_mmio@%" PRIx64, base);
567 qemu_fdt_add_subnode(vbi->fdt, nodename);
568 qemu_fdt_setprop_string(vbi->fdt, nodename,
569 "compatible", "virtio,mmio");
570 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg",
571 2, base, 2, size);
572 qemu_fdt_setprop_cells(vbi->fdt, nodename, "interrupts",
573 GIC_FDT_IRQ_TYPE_SPI, irq,
574 GIC_FDT_IRQ_FLAGS_EDGE_LO_HI);
575 g_free(nodename);
579 static void create_one_flash(const char *name, hwaddr flashbase,
580 hwaddr flashsize)
582 /* Create and map a single flash device. We use the same
583 * parameters as the flash devices on the Versatile Express board.
585 DriveInfo *dinfo = drive_get_next(IF_PFLASH);
586 DeviceState *dev = qdev_create(NULL, "cfi.pflash01");
587 const uint64_t sectorlength = 256 * 1024;
589 if (dinfo) {
590 qdev_prop_set_drive(dev, "drive", blk_by_legacy_dinfo(dinfo),
591 &error_abort);
594 qdev_prop_set_uint32(dev, "num-blocks", flashsize / sectorlength);
595 qdev_prop_set_uint64(dev, "sector-length", sectorlength);
596 qdev_prop_set_uint8(dev, "width", 4);
597 qdev_prop_set_uint8(dev, "device-width", 2);
598 qdev_prop_set_bit(dev, "big-endian", false);
599 qdev_prop_set_uint16(dev, "id0", 0x89);
600 qdev_prop_set_uint16(dev, "id1", 0x18);
601 qdev_prop_set_uint16(dev, "id2", 0x00);
602 qdev_prop_set_uint16(dev, "id3", 0x00);
603 qdev_prop_set_string(dev, "name", name);
604 qdev_init_nofail(dev);
606 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, flashbase);
609 static void create_flash(const VirtBoardInfo *vbi)
611 /* Create two flash devices to fill the VIRT_FLASH space in the memmap.
612 * Any file passed via -bios goes in the first of these.
614 hwaddr flashsize = vbi->memmap[VIRT_FLASH].size / 2;
615 hwaddr flashbase = vbi->memmap[VIRT_FLASH].base;
616 char *nodename;
618 if (bios_name) {
619 char *fn;
620 int image_size;
622 if (drive_get(IF_PFLASH, 0, 0)) {
623 error_report("The contents of the first flash device may be "
624 "specified with -bios or with -drive if=pflash... "
625 "but you cannot use both options at once");
626 exit(1);
628 fn = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
629 if (!fn) {
630 error_report("Could not find ROM image '%s'", bios_name);
631 exit(1);
633 image_size = load_image_targphys(fn, flashbase, flashsize);
634 g_free(fn);
635 if (image_size < 0) {
636 error_report("Could not load ROM image '%s'", bios_name);
637 exit(1);
641 create_one_flash("virt.flash0", flashbase, flashsize);
642 create_one_flash("virt.flash1", flashbase + flashsize, flashsize);
644 nodename = g_strdup_printf("/flash@%" PRIx64, flashbase);
645 qemu_fdt_add_subnode(vbi->fdt, nodename);
646 qemu_fdt_setprop_string(vbi->fdt, nodename, "compatible", "cfi-flash");
647 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg",
648 2, flashbase, 2, flashsize,
649 2, flashbase + flashsize, 2, flashsize);
650 qemu_fdt_setprop_cell(vbi->fdt, nodename, "bank-width", 4);
651 g_free(nodename);
654 static void create_fw_cfg(const VirtBoardInfo *vbi)
656 hwaddr base = vbi->memmap[VIRT_FW_CFG].base;
657 hwaddr size = vbi->memmap[VIRT_FW_CFG].size;
658 char *nodename;
660 fw_cfg_init_mem_wide(base + 8, base, 8);
662 nodename = g_strdup_printf("/fw-cfg@%" PRIx64, base);
663 qemu_fdt_add_subnode(vbi->fdt, nodename);
664 qemu_fdt_setprop_string(vbi->fdt, nodename,
665 "compatible", "qemu,fw-cfg-mmio");
666 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg",
667 2, base, 2, size);
668 g_free(nodename);
671 static void create_pcie_irq_map(const VirtBoardInfo *vbi, uint32_t gic_phandle,
672 int first_irq, const char *nodename)
674 int devfn, pin;
675 uint32_t full_irq_map[4 * 4 * 10] = { 0 };
676 uint32_t *irq_map = full_irq_map;
678 for (devfn = 0; devfn <= 0x18; devfn += 0x8) {
679 for (pin = 0; pin < 4; pin++) {
680 int irq_type = GIC_FDT_IRQ_TYPE_SPI;
681 int irq_nr = first_irq + ((pin + PCI_SLOT(devfn)) % PCI_NUM_PINS);
682 int irq_level = GIC_FDT_IRQ_FLAGS_LEVEL_HI;
683 int i;
685 uint32_t map[] = {
686 devfn << 8, 0, 0, /* devfn */
687 pin + 1, /* PCI pin */
688 gic_phandle, 0, 0, irq_type, irq_nr, irq_level }; /* GIC irq */
690 /* Convert map to big endian */
691 for (i = 0; i < 10; i++) {
692 irq_map[i] = cpu_to_be32(map[i]);
694 irq_map += 10;
698 qemu_fdt_setprop(vbi->fdt, nodename, "interrupt-map",
699 full_irq_map, sizeof(full_irq_map));
701 qemu_fdt_setprop_cells(vbi->fdt, nodename, "interrupt-map-mask",
702 0x1800, 0, 0, /* devfn (PCI_SLOT(3)) */
703 0x7 /* PCI irq */);
706 static void create_pcie(const VirtBoardInfo *vbi, qemu_irq *pic,
707 bool use_highmem)
709 hwaddr base_mmio = vbi->memmap[VIRT_PCIE_MMIO].base;
710 hwaddr size_mmio = vbi->memmap[VIRT_PCIE_MMIO].size;
711 hwaddr base_mmio_high = vbi->memmap[VIRT_PCIE_MMIO_HIGH].base;
712 hwaddr size_mmio_high = vbi->memmap[VIRT_PCIE_MMIO_HIGH].size;
713 hwaddr base_pio = vbi->memmap[VIRT_PCIE_PIO].base;
714 hwaddr size_pio = vbi->memmap[VIRT_PCIE_PIO].size;
715 hwaddr base_ecam = vbi->memmap[VIRT_PCIE_ECAM].base;
716 hwaddr size_ecam = vbi->memmap[VIRT_PCIE_ECAM].size;
717 hwaddr base = base_mmio;
718 int nr_pcie_buses = size_ecam / PCIE_MMCFG_SIZE_MIN;
719 int irq = vbi->irqmap[VIRT_PCIE];
720 MemoryRegion *mmio_alias;
721 MemoryRegion *mmio_reg;
722 MemoryRegion *ecam_alias;
723 MemoryRegion *ecam_reg;
724 DeviceState *dev;
725 char *nodename;
726 int i;
728 dev = qdev_create(NULL, TYPE_GPEX_HOST);
729 qdev_init_nofail(dev);
731 /* Map only the first size_ecam bytes of ECAM space */
732 ecam_alias = g_new0(MemoryRegion, 1);
733 ecam_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0);
734 memory_region_init_alias(ecam_alias, OBJECT(dev), "pcie-ecam",
735 ecam_reg, 0, size_ecam);
736 memory_region_add_subregion(get_system_memory(), base_ecam, ecam_alias);
738 /* Map the MMIO window into system address space so as to expose
739 * the section of PCI MMIO space which starts at the same base address
740 * (ie 1:1 mapping for that part of PCI MMIO space visible through
741 * the window).
743 mmio_alias = g_new0(MemoryRegion, 1);
744 mmio_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 1);
745 memory_region_init_alias(mmio_alias, OBJECT(dev), "pcie-mmio",
746 mmio_reg, base_mmio, size_mmio);
747 memory_region_add_subregion(get_system_memory(), base_mmio, mmio_alias);
749 if (use_highmem) {
750 /* Map high MMIO space */
751 MemoryRegion *high_mmio_alias = g_new0(MemoryRegion, 1);
753 memory_region_init_alias(high_mmio_alias, OBJECT(dev), "pcie-mmio-high",
754 mmio_reg, base_mmio_high, size_mmio_high);
755 memory_region_add_subregion(get_system_memory(), base_mmio_high,
756 high_mmio_alias);
759 /* Map IO port space */
760 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 2, base_pio);
762 for (i = 0; i < GPEX_NUM_IRQS; i++) {
763 sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, pic[irq + i]);
766 nodename = g_strdup_printf("/pcie@%" PRIx64, base);
767 qemu_fdt_add_subnode(vbi->fdt, nodename);
768 qemu_fdt_setprop_string(vbi->fdt, nodename,
769 "compatible", "pci-host-ecam-generic");
770 qemu_fdt_setprop_string(vbi->fdt, nodename, "device_type", "pci");
771 qemu_fdt_setprop_cell(vbi->fdt, nodename, "#address-cells", 3);
772 qemu_fdt_setprop_cell(vbi->fdt, nodename, "#size-cells", 2);
773 qemu_fdt_setprop_cells(vbi->fdt, nodename, "bus-range", 0,
774 nr_pcie_buses - 1);
776 qemu_fdt_setprop_cells(vbi->fdt, nodename, "msi-parent", vbi->v2m_phandle);
778 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg",
779 2, base_ecam, 2, size_ecam);
781 if (use_highmem) {
782 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "ranges",
783 1, FDT_PCI_RANGE_IOPORT, 2, 0,
784 2, base_pio, 2, size_pio,
785 1, FDT_PCI_RANGE_MMIO, 2, base_mmio,
786 2, base_mmio, 2, size_mmio,
787 1, FDT_PCI_RANGE_MMIO_64BIT,
788 2, base_mmio_high,
789 2, base_mmio_high, 2, size_mmio_high);
790 } else {
791 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "ranges",
792 1, FDT_PCI_RANGE_IOPORT, 2, 0,
793 2, base_pio, 2, size_pio,
794 1, FDT_PCI_RANGE_MMIO, 2, base_mmio,
795 2, base_mmio, 2, size_mmio);
798 qemu_fdt_setprop_cell(vbi->fdt, nodename, "#interrupt-cells", 1);
799 create_pcie_irq_map(vbi, vbi->gic_phandle, irq, nodename);
801 g_free(nodename);
804 static void create_platform_bus(VirtBoardInfo *vbi, qemu_irq *pic)
806 DeviceState *dev;
807 SysBusDevice *s;
808 int i;
809 ARMPlatformBusFDTParams *fdt_params = g_new(ARMPlatformBusFDTParams, 1);
810 MemoryRegion *sysmem = get_system_memory();
812 platform_bus_params.platform_bus_base = vbi->memmap[VIRT_PLATFORM_BUS].base;
813 platform_bus_params.platform_bus_size = vbi->memmap[VIRT_PLATFORM_BUS].size;
814 platform_bus_params.platform_bus_first_irq = vbi->irqmap[VIRT_PLATFORM_BUS];
815 platform_bus_params.platform_bus_num_irqs = PLATFORM_BUS_NUM_IRQS;
817 fdt_params->system_params = &platform_bus_params;
818 fdt_params->binfo = &vbi->bootinfo;
819 fdt_params->intc = "/intc";
821 * register a machine init done notifier that creates the device tree
822 * nodes of the platform bus and its children dynamic sysbus devices
824 arm_register_platform_bus_fdt_creator(fdt_params);
826 dev = qdev_create(NULL, TYPE_PLATFORM_BUS_DEVICE);
827 dev->id = TYPE_PLATFORM_BUS_DEVICE;
828 qdev_prop_set_uint32(dev, "num_irqs",
829 platform_bus_params.platform_bus_num_irqs);
830 qdev_prop_set_uint32(dev, "mmio_size",
831 platform_bus_params.platform_bus_size);
832 qdev_init_nofail(dev);
833 s = SYS_BUS_DEVICE(dev);
835 for (i = 0; i < platform_bus_params.platform_bus_num_irqs; i++) {
836 int irqn = platform_bus_params.platform_bus_first_irq + i;
837 sysbus_connect_irq(s, i, pic[irqn]);
840 memory_region_add_subregion(sysmem,
841 platform_bus_params.platform_bus_base,
842 sysbus_mmio_get_region(s, 0));
845 static void *machvirt_dtb(const struct arm_boot_info *binfo, int *fdt_size)
847 const VirtBoardInfo *board = (const VirtBoardInfo *)binfo;
849 *fdt_size = board->fdt_size;
850 return board->fdt;
853 static void virt_build_smbios(VirtGuestInfo *guest_info)
855 FWCfgState *fw_cfg = guest_info->fw_cfg;
856 uint8_t *smbios_tables, *smbios_anchor;
857 size_t smbios_tables_len, smbios_anchor_len;
859 if (!fw_cfg) {
860 return;
863 smbios_set_defaults("QEMU", "QEMU Virtual Machine",
864 "1.0", false, true, SMBIOS_ENTRY_POINT_30);
866 smbios_get_tables(NULL, 0, &smbios_tables, &smbios_tables_len,
867 &smbios_anchor, &smbios_anchor_len);
869 if (smbios_anchor) {
870 fw_cfg_add_file(fw_cfg, "etc/smbios/smbios-tables",
871 smbios_tables, smbios_tables_len);
872 fw_cfg_add_file(fw_cfg, "etc/smbios/smbios-anchor",
873 smbios_anchor, smbios_anchor_len);
877 static
878 void virt_guest_info_machine_done(Notifier *notifier, void *data)
880 VirtGuestInfoState *guest_info_state = container_of(notifier,
881 VirtGuestInfoState, machine_done);
882 virt_acpi_setup(&guest_info_state->info);
883 virt_build_smbios(&guest_info_state->info);
886 static void machvirt_init(MachineState *machine)
888 VirtMachineState *vms = VIRT_MACHINE(machine);
889 qemu_irq pic[NUM_IRQS];
890 MemoryRegion *sysmem = get_system_memory();
891 int n;
892 MemoryRegion *ram = g_new(MemoryRegion, 1);
893 const char *cpu_model = machine->cpu_model;
894 VirtBoardInfo *vbi;
895 VirtGuestInfoState *guest_info_state = g_malloc0(sizeof *guest_info_state);
896 VirtGuestInfo *guest_info = &guest_info_state->info;
897 char **cpustr;
899 if (!cpu_model) {
900 cpu_model = "cortex-a15";
903 /* Separate the actual CPU model name from any appended features */
904 cpustr = g_strsplit(cpu_model, ",", 2);
906 vbi = find_machine_info(cpustr[0]);
908 if (!vbi) {
909 error_report("mach-virt: CPU %s not supported", cpustr[0]);
910 exit(1);
913 vbi->smp_cpus = smp_cpus;
915 if (machine->ram_size > vbi->memmap[VIRT_MEM].size) {
916 error_report("mach-virt: cannot model more than 30GB RAM");
917 exit(1);
920 create_fdt(vbi);
922 for (n = 0; n < smp_cpus; n++) {
923 ObjectClass *oc = cpu_class_by_name(TYPE_ARM_CPU, cpustr[0]);
924 CPUClass *cc = CPU_CLASS(oc);
925 Object *cpuobj;
926 Error *err = NULL;
927 char *cpuopts = g_strdup(cpustr[1]);
929 if (!oc) {
930 fprintf(stderr, "Unable to find CPU definition\n");
931 exit(1);
933 cpuobj = object_new(object_class_get_name(oc));
935 /* Handle any CPU options specified by the user */
936 cc->parse_features(CPU(cpuobj), cpuopts, &err);
937 g_free(cpuopts);
938 if (err) {
939 error_report_err(err);
940 exit(1);
943 if (!vms->secure) {
944 object_property_set_bool(cpuobj, false, "has_el3", NULL);
947 object_property_set_int(cpuobj, QEMU_PSCI_CONDUIT_HVC, "psci-conduit",
948 NULL);
950 /* Secondary CPUs start in PSCI powered-down state */
951 if (n > 0) {
952 object_property_set_bool(cpuobj, true, "start-powered-off", NULL);
955 if (object_property_find(cpuobj, "reset-cbar", NULL)) {
956 object_property_set_int(cpuobj, vbi->memmap[VIRT_CPUPERIPHS].base,
957 "reset-cbar", &error_abort);
960 object_property_set_bool(cpuobj, true, "realized", NULL);
962 g_strfreev(cpustr);
963 fdt_add_timer_nodes(vbi);
964 fdt_add_cpu_nodes(vbi);
965 fdt_add_psci_node(vbi);
967 memory_region_allocate_system_memory(ram, NULL, "mach-virt.ram",
968 machine->ram_size);
969 memory_region_add_subregion(sysmem, vbi->memmap[VIRT_MEM].base, ram);
971 create_flash(vbi);
973 create_gic(vbi, pic, vms->secure);
975 create_uart(vbi, pic);
977 create_rtc(vbi, pic);
979 create_pcie(vbi, pic, vms->highmem);
981 /* Create mmio transports, so the user can create virtio backends
982 * (which will be automatically plugged in to the transports). If
983 * no backend is created the transport will just sit harmlessly idle.
985 create_virtio_devices(vbi, pic);
987 create_fw_cfg(vbi);
988 rom_set_fw(fw_cfg_find());
990 guest_info->smp_cpus = smp_cpus;
991 guest_info->fw_cfg = fw_cfg_find();
992 guest_info->memmap = vbi->memmap;
993 guest_info->irqmap = vbi->irqmap;
994 guest_info->use_highmem = vms->highmem;
995 guest_info_state->machine_done.notify = virt_guest_info_machine_done;
996 qemu_add_machine_init_done_notifier(&guest_info_state->machine_done);
998 vbi->bootinfo.ram_size = machine->ram_size;
999 vbi->bootinfo.kernel_filename = machine->kernel_filename;
1000 vbi->bootinfo.kernel_cmdline = machine->kernel_cmdline;
1001 vbi->bootinfo.initrd_filename = machine->initrd_filename;
1002 vbi->bootinfo.nb_cpus = smp_cpus;
1003 vbi->bootinfo.board_id = -1;
1004 vbi->bootinfo.loader_start = vbi->memmap[VIRT_MEM].base;
1005 vbi->bootinfo.get_dtb = machvirt_dtb;
1006 vbi->bootinfo.firmware_loaded = bios_name || drive_get(IF_PFLASH, 0, 0);
1007 arm_load_kernel(ARM_CPU(first_cpu), &vbi->bootinfo);
1010 * arm_load_kernel machine init done notifier registration must
1011 * happen before the platform_bus_create call. In this latter,
1012 * another notifier is registered which adds platform bus nodes.
1013 * Notifiers are executed in registration reverse order.
1015 create_platform_bus(vbi, pic);
1018 static bool virt_get_secure(Object *obj, Error **errp)
1020 VirtMachineState *vms = VIRT_MACHINE(obj);
1022 return vms->secure;
1025 static void virt_set_secure(Object *obj, bool value, Error **errp)
1027 VirtMachineState *vms = VIRT_MACHINE(obj);
1029 vms->secure = value;
1032 static bool virt_get_highmem(Object *obj, Error **errp)
1034 VirtMachineState *vms = VIRT_MACHINE(obj);
1036 return vms->highmem;
1039 static void virt_set_highmem(Object *obj, bool value, Error **errp)
1041 VirtMachineState *vms = VIRT_MACHINE(obj);
1043 vms->highmem = value;
1046 static void virt_instance_init(Object *obj)
1048 VirtMachineState *vms = VIRT_MACHINE(obj);
1050 /* EL3 is disabled by default on virt: this makes us consistent
1051 * between KVM and TCG for this board, and it also allows us to
1052 * boot UEFI blobs which assume no TrustZone support.
1054 vms->secure = false;
1055 object_property_add_bool(obj, "secure", virt_get_secure,
1056 virt_set_secure, NULL);
1057 object_property_set_description(obj, "secure",
1058 "Set on/off to enable/disable the ARM "
1059 "Security Extensions (TrustZone)",
1060 NULL);
1062 /* High memory is enabled by default */
1063 vms->highmem = true;
1064 object_property_add_bool(obj, "highmem", virt_get_highmem,
1065 virt_set_highmem, NULL);
1066 object_property_set_description(obj, "highmem",
1067 "Set on/off to enable/disable using "
1068 "physical address space above 32 bits",
1069 NULL);
1072 static void virt_class_init(ObjectClass *oc, void *data)
1074 MachineClass *mc = MACHINE_CLASS(oc);
1076 mc->desc = "ARM Virtual Machine",
1077 mc->init = machvirt_init;
1078 mc->max_cpus = 8;
1079 mc->has_dynamic_sysbus = true;
1080 mc->block_default_type = IF_VIRTIO;
1081 mc->no_cdrom = 1;
1084 static const TypeInfo machvirt_info = {
1085 .name = TYPE_VIRT_MACHINE,
1086 .parent = TYPE_MACHINE,
1087 .instance_size = sizeof(VirtMachineState),
1088 .instance_init = virt_instance_init,
1089 .class_size = sizeof(VirtMachineClass),
1090 .class_init = virt_class_init,
1093 static void machvirt_machine_init(void)
1095 type_register_static(&machvirt_info);
1098 machine_init(machvirt_machine_init);