xen/pt: Use xen_host_pci_get_[byte|word] instead of dev.config
[qemu/ar7.git] / hw / arm / virt.c
blob91e45e04a18238a9ca71b7e1f3ecc74cad20c7da
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 "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)
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 qdev_init_nofail(gicdev);
417 gicbusdev = SYS_BUS_DEVICE(gicdev);
418 sysbus_mmio_map(gicbusdev, 0, vbi->memmap[VIRT_GIC_DIST].base);
419 sysbus_mmio_map(gicbusdev, 1, vbi->memmap[VIRT_GIC_CPU].base);
421 /* Wire the outputs from each CPU's generic timer to the
422 * appropriate GIC PPI inputs, and the GIC's IRQ output to
423 * the CPU's IRQ input.
425 for (i = 0; i < smp_cpus; i++) {
426 DeviceState *cpudev = DEVICE(qemu_get_cpu(i));
427 int ppibase = NUM_IRQS + i * GIC_INTERNAL + GIC_NR_SGIS;
428 int irq;
429 /* Mapping from the output timer irq lines from the CPU to the
430 * GIC PPI inputs we use for the virt board.
432 const int timer_irq[] = {
433 [GTIMER_PHYS] = ARCH_TIMER_NS_EL1_IRQ,
434 [GTIMER_VIRT] = ARCH_TIMER_VIRT_IRQ,
435 [GTIMER_HYP] = ARCH_TIMER_NS_EL2_IRQ,
436 [GTIMER_SEC] = ARCH_TIMER_S_EL1_IRQ,
439 for (irq = 0; irq < ARRAY_SIZE(timer_irq); irq++) {
440 qdev_connect_gpio_out(cpudev, irq,
441 qdev_get_gpio_in(gicdev,
442 ppibase + timer_irq[irq]));
445 sysbus_connect_irq(gicbusdev, i, qdev_get_gpio_in(cpudev, ARM_CPU_IRQ));
446 sysbus_connect_irq(gicbusdev, i + smp_cpus,
447 qdev_get_gpio_in(cpudev, ARM_CPU_FIQ));
450 for (i = 0; i < NUM_IRQS; i++) {
451 pic[i] = qdev_get_gpio_in(gicdev, i);
454 fdt_add_gic_node(vbi);
456 create_v2m(vbi, pic);
459 static void create_uart(const VirtBoardInfo *vbi, qemu_irq *pic)
461 char *nodename;
462 hwaddr base = vbi->memmap[VIRT_UART].base;
463 hwaddr size = vbi->memmap[VIRT_UART].size;
464 int irq = vbi->irqmap[VIRT_UART];
465 const char compat[] = "arm,pl011\0arm,primecell";
466 const char clocknames[] = "uartclk\0apb_pclk";
468 sysbus_create_simple("pl011", base, pic[irq]);
470 nodename = g_strdup_printf("/pl011@%" PRIx64, base);
471 qemu_fdt_add_subnode(vbi->fdt, nodename);
472 /* Note that we can't use setprop_string because of the embedded NUL */
473 qemu_fdt_setprop(vbi->fdt, nodename, "compatible",
474 compat, sizeof(compat));
475 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg",
476 2, base, 2, size);
477 qemu_fdt_setprop_cells(vbi->fdt, nodename, "interrupts",
478 GIC_FDT_IRQ_TYPE_SPI, irq,
479 GIC_FDT_IRQ_FLAGS_LEVEL_HI);
480 qemu_fdt_setprop_cells(vbi->fdt, nodename, "clocks",
481 vbi->clock_phandle, vbi->clock_phandle);
482 qemu_fdt_setprop(vbi->fdt, nodename, "clock-names",
483 clocknames, sizeof(clocknames));
485 qemu_fdt_setprop_string(vbi->fdt, "/chosen", "stdout-path", nodename);
486 g_free(nodename);
489 static void create_rtc(const VirtBoardInfo *vbi, qemu_irq *pic)
491 char *nodename;
492 hwaddr base = vbi->memmap[VIRT_RTC].base;
493 hwaddr size = vbi->memmap[VIRT_RTC].size;
494 int irq = vbi->irqmap[VIRT_RTC];
495 const char compat[] = "arm,pl031\0arm,primecell";
497 sysbus_create_simple("pl031", base, pic[irq]);
499 nodename = g_strdup_printf("/pl031@%" PRIx64, base);
500 qemu_fdt_add_subnode(vbi->fdt, nodename);
501 qemu_fdt_setprop(vbi->fdt, nodename, "compatible", compat, sizeof(compat));
502 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg",
503 2, base, 2, size);
504 qemu_fdt_setprop_cells(vbi->fdt, nodename, "interrupts",
505 GIC_FDT_IRQ_TYPE_SPI, irq,
506 GIC_FDT_IRQ_FLAGS_LEVEL_HI);
507 qemu_fdt_setprop_cell(vbi->fdt, nodename, "clocks", vbi->clock_phandle);
508 qemu_fdt_setprop_string(vbi->fdt, nodename, "clock-names", "apb_pclk");
509 g_free(nodename);
512 static void create_virtio_devices(const VirtBoardInfo *vbi, qemu_irq *pic)
514 int i;
515 hwaddr size = vbi->memmap[VIRT_MMIO].size;
517 /* We create the transports in forwards order. Since qbus_realize()
518 * prepends (not appends) new child buses, the incrementing loop below will
519 * create a list of virtio-mmio buses with decreasing base addresses.
521 * When a -device option is processed from the command line,
522 * qbus_find_recursive() picks the next free virtio-mmio bus in forwards
523 * order. The upshot is that -device options in increasing command line
524 * order are mapped to virtio-mmio buses with decreasing base addresses.
526 * When this code was originally written, that arrangement ensured that the
527 * guest Linux kernel would give the lowest "name" (/dev/vda, eth0, etc) to
528 * the first -device on the command line. (The end-to-end order is a
529 * function of this loop, qbus_realize(), qbus_find_recursive(), and the
530 * guest kernel's name-to-address assignment strategy.)
532 * Meanwhile, the kernel's traversal seems to have been reversed; see eg.
533 * the message, if not necessarily the code, of commit 70161ff336.
534 * Therefore the loop now establishes the inverse of the original intent.
536 * Unfortunately, we can't counteract the kernel change by reversing the
537 * loop; it would break existing command lines.
539 * In any case, the kernel makes no guarantee about the stability of
540 * enumeration order of virtio devices (as demonstrated by it changing
541 * between kernel versions). For reliable and stable identification
542 * of disks users must use UUIDs or similar mechanisms.
544 for (i = 0; i < NUM_VIRTIO_TRANSPORTS; i++) {
545 int irq = vbi->irqmap[VIRT_MMIO] + i;
546 hwaddr base = vbi->memmap[VIRT_MMIO].base + i * size;
548 sysbus_create_simple("virtio-mmio", base, pic[irq]);
551 /* We add dtb nodes in reverse order so that they appear in the finished
552 * device tree lowest address first.
554 * Note that this mapping is independent of the loop above. The previous
555 * loop influences virtio device to virtio transport assignment, whereas
556 * this loop controls how virtio transports are laid out in the dtb.
558 for (i = NUM_VIRTIO_TRANSPORTS - 1; i >= 0; i--) {
559 char *nodename;
560 int irq = vbi->irqmap[VIRT_MMIO] + i;
561 hwaddr base = vbi->memmap[VIRT_MMIO].base + i * size;
563 nodename = g_strdup_printf("/virtio_mmio@%" PRIx64, base);
564 qemu_fdt_add_subnode(vbi->fdt, nodename);
565 qemu_fdt_setprop_string(vbi->fdt, nodename,
566 "compatible", "virtio,mmio");
567 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg",
568 2, base, 2, size);
569 qemu_fdt_setprop_cells(vbi->fdt, nodename, "interrupts",
570 GIC_FDT_IRQ_TYPE_SPI, irq,
571 GIC_FDT_IRQ_FLAGS_EDGE_LO_HI);
572 g_free(nodename);
576 static void create_one_flash(const char *name, hwaddr flashbase,
577 hwaddr flashsize)
579 /* Create and map a single flash device. We use the same
580 * parameters as the flash devices on the Versatile Express board.
582 DriveInfo *dinfo = drive_get_next(IF_PFLASH);
583 DeviceState *dev = qdev_create(NULL, "cfi.pflash01");
584 const uint64_t sectorlength = 256 * 1024;
586 if (dinfo) {
587 qdev_prop_set_drive(dev, "drive", blk_by_legacy_dinfo(dinfo),
588 &error_abort);
591 qdev_prop_set_uint32(dev, "num-blocks", flashsize / sectorlength);
592 qdev_prop_set_uint64(dev, "sector-length", sectorlength);
593 qdev_prop_set_uint8(dev, "width", 4);
594 qdev_prop_set_uint8(dev, "device-width", 2);
595 qdev_prop_set_bit(dev, "big-endian", false);
596 qdev_prop_set_uint16(dev, "id0", 0x89);
597 qdev_prop_set_uint16(dev, "id1", 0x18);
598 qdev_prop_set_uint16(dev, "id2", 0x00);
599 qdev_prop_set_uint16(dev, "id3", 0x00);
600 qdev_prop_set_string(dev, "name", name);
601 qdev_init_nofail(dev);
603 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, flashbase);
606 static void create_flash(const VirtBoardInfo *vbi)
608 /* Create two flash devices to fill the VIRT_FLASH space in the memmap.
609 * Any file passed via -bios goes in the first of these.
611 hwaddr flashsize = vbi->memmap[VIRT_FLASH].size / 2;
612 hwaddr flashbase = vbi->memmap[VIRT_FLASH].base;
613 char *nodename;
615 if (bios_name) {
616 char *fn;
617 int image_size;
619 if (drive_get(IF_PFLASH, 0, 0)) {
620 error_report("The contents of the first flash device may be "
621 "specified with -bios or with -drive if=pflash... "
622 "but you cannot use both options at once");
623 exit(1);
625 fn = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
626 if (!fn) {
627 error_report("Could not find ROM image '%s'", bios_name);
628 exit(1);
630 image_size = load_image_targphys(fn, flashbase, flashsize);
631 g_free(fn);
632 if (image_size < 0) {
633 error_report("Could not load ROM image '%s'", bios_name);
634 exit(1);
638 create_one_flash("virt.flash0", flashbase, flashsize);
639 create_one_flash("virt.flash1", flashbase + flashsize, flashsize);
641 nodename = g_strdup_printf("/flash@%" PRIx64, flashbase);
642 qemu_fdt_add_subnode(vbi->fdt, nodename);
643 qemu_fdt_setprop_string(vbi->fdt, nodename, "compatible", "cfi-flash");
644 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg",
645 2, flashbase, 2, flashsize,
646 2, flashbase + flashsize, 2, flashsize);
647 qemu_fdt_setprop_cell(vbi->fdt, nodename, "bank-width", 4);
648 g_free(nodename);
651 static void create_fw_cfg(const VirtBoardInfo *vbi)
653 hwaddr base = vbi->memmap[VIRT_FW_CFG].base;
654 hwaddr size = vbi->memmap[VIRT_FW_CFG].size;
655 char *nodename;
657 fw_cfg_init_mem_wide(base + 8, base, 8);
659 nodename = g_strdup_printf("/fw-cfg@%" PRIx64, base);
660 qemu_fdt_add_subnode(vbi->fdt, nodename);
661 qemu_fdt_setprop_string(vbi->fdt, nodename,
662 "compatible", "qemu,fw-cfg-mmio");
663 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg",
664 2, base, 2, size);
665 g_free(nodename);
668 static void create_pcie_irq_map(const VirtBoardInfo *vbi, uint32_t gic_phandle,
669 int first_irq, const char *nodename)
671 int devfn, pin;
672 uint32_t full_irq_map[4 * 4 * 10] = { 0 };
673 uint32_t *irq_map = full_irq_map;
675 for (devfn = 0; devfn <= 0x18; devfn += 0x8) {
676 for (pin = 0; pin < 4; pin++) {
677 int irq_type = GIC_FDT_IRQ_TYPE_SPI;
678 int irq_nr = first_irq + ((pin + PCI_SLOT(devfn)) % PCI_NUM_PINS);
679 int irq_level = GIC_FDT_IRQ_FLAGS_LEVEL_HI;
680 int i;
682 uint32_t map[] = {
683 devfn << 8, 0, 0, /* devfn */
684 pin + 1, /* PCI pin */
685 gic_phandle, 0, 0, irq_type, irq_nr, irq_level }; /* GIC irq */
687 /* Convert map to big endian */
688 for (i = 0; i < 10; i++) {
689 irq_map[i] = cpu_to_be32(map[i]);
691 irq_map += 10;
695 qemu_fdt_setprop(vbi->fdt, nodename, "interrupt-map",
696 full_irq_map, sizeof(full_irq_map));
698 qemu_fdt_setprop_cells(vbi->fdt, nodename, "interrupt-map-mask",
699 0x1800, 0, 0, /* devfn (PCI_SLOT(3)) */
700 0x7 /* PCI irq */);
703 static void create_pcie(const VirtBoardInfo *vbi, qemu_irq *pic,
704 bool use_highmem)
706 hwaddr base_mmio = vbi->memmap[VIRT_PCIE_MMIO].base;
707 hwaddr size_mmio = vbi->memmap[VIRT_PCIE_MMIO].size;
708 hwaddr base_mmio_high = vbi->memmap[VIRT_PCIE_MMIO_HIGH].base;
709 hwaddr size_mmio_high = vbi->memmap[VIRT_PCIE_MMIO_HIGH].size;
710 hwaddr base_pio = vbi->memmap[VIRT_PCIE_PIO].base;
711 hwaddr size_pio = vbi->memmap[VIRT_PCIE_PIO].size;
712 hwaddr base_ecam = vbi->memmap[VIRT_PCIE_ECAM].base;
713 hwaddr size_ecam = vbi->memmap[VIRT_PCIE_ECAM].size;
714 hwaddr base = base_mmio;
715 int nr_pcie_buses = size_ecam / PCIE_MMCFG_SIZE_MIN;
716 int irq = vbi->irqmap[VIRT_PCIE];
717 MemoryRegion *mmio_alias;
718 MemoryRegion *mmio_reg;
719 MemoryRegion *ecam_alias;
720 MemoryRegion *ecam_reg;
721 DeviceState *dev;
722 char *nodename;
723 int i;
725 dev = qdev_create(NULL, TYPE_GPEX_HOST);
726 qdev_init_nofail(dev);
728 /* Map only the first size_ecam bytes of ECAM space */
729 ecam_alias = g_new0(MemoryRegion, 1);
730 ecam_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0);
731 memory_region_init_alias(ecam_alias, OBJECT(dev), "pcie-ecam",
732 ecam_reg, 0, size_ecam);
733 memory_region_add_subregion(get_system_memory(), base_ecam, ecam_alias);
735 /* Map the MMIO window into system address space so as to expose
736 * the section of PCI MMIO space which starts at the same base address
737 * (ie 1:1 mapping for that part of PCI MMIO space visible through
738 * the window).
740 mmio_alias = g_new0(MemoryRegion, 1);
741 mmio_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 1);
742 memory_region_init_alias(mmio_alias, OBJECT(dev), "pcie-mmio",
743 mmio_reg, base_mmio, size_mmio);
744 memory_region_add_subregion(get_system_memory(), base_mmio, mmio_alias);
746 if (use_highmem) {
747 /* Map high MMIO space */
748 MemoryRegion *high_mmio_alias = g_new0(MemoryRegion, 1);
750 memory_region_init_alias(high_mmio_alias, OBJECT(dev), "pcie-mmio-high",
751 mmio_reg, base_mmio_high, size_mmio_high);
752 memory_region_add_subregion(get_system_memory(), base_mmio_high,
753 high_mmio_alias);
756 /* Map IO port space */
757 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 2, base_pio);
759 for (i = 0; i < GPEX_NUM_IRQS; i++) {
760 sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, pic[irq + i]);
763 nodename = g_strdup_printf("/pcie@%" PRIx64, base);
764 qemu_fdt_add_subnode(vbi->fdt, nodename);
765 qemu_fdt_setprop_string(vbi->fdt, nodename,
766 "compatible", "pci-host-ecam-generic");
767 qemu_fdt_setprop_string(vbi->fdt, nodename, "device_type", "pci");
768 qemu_fdt_setprop_cell(vbi->fdt, nodename, "#address-cells", 3);
769 qemu_fdt_setprop_cell(vbi->fdt, nodename, "#size-cells", 2);
770 qemu_fdt_setprop_cells(vbi->fdt, nodename, "bus-range", 0,
771 nr_pcie_buses - 1);
773 qemu_fdt_setprop_cells(vbi->fdt, nodename, "msi-parent", vbi->v2m_phandle);
775 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg",
776 2, base_ecam, 2, size_ecam);
778 if (use_highmem) {
779 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "ranges",
780 1, FDT_PCI_RANGE_IOPORT, 2, 0,
781 2, base_pio, 2, size_pio,
782 1, FDT_PCI_RANGE_MMIO, 2, base_mmio,
783 2, base_mmio, 2, size_mmio,
784 1, FDT_PCI_RANGE_MMIO_64BIT,
785 2, base_mmio_high,
786 2, base_mmio_high, 2, size_mmio_high);
787 } else {
788 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "ranges",
789 1, FDT_PCI_RANGE_IOPORT, 2, 0,
790 2, base_pio, 2, size_pio,
791 1, FDT_PCI_RANGE_MMIO, 2, base_mmio,
792 2, base_mmio, 2, size_mmio);
795 qemu_fdt_setprop_cell(vbi->fdt, nodename, "#interrupt-cells", 1);
796 create_pcie_irq_map(vbi, vbi->gic_phandle, irq, nodename);
798 g_free(nodename);
801 static void create_platform_bus(VirtBoardInfo *vbi, qemu_irq *pic)
803 DeviceState *dev;
804 SysBusDevice *s;
805 int i;
806 ARMPlatformBusFDTParams *fdt_params = g_new(ARMPlatformBusFDTParams, 1);
807 MemoryRegion *sysmem = get_system_memory();
809 platform_bus_params.platform_bus_base = vbi->memmap[VIRT_PLATFORM_BUS].base;
810 platform_bus_params.platform_bus_size = vbi->memmap[VIRT_PLATFORM_BUS].size;
811 platform_bus_params.platform_bus_first_irq = vbi->irqmap[VIRT_PLATFORM_BUS];
812 platform_bus_params.platform_bus_num_irqs = PLATFORM_BUS_NUM_IRQS;
814 fdt_params->system_params = &platform_bus_params;
815 fdt_params->binfo = &vbi->bootinfo;
816 fdt_params->intc = "/intc";
818 * register a machine init done notifier that creates the device tree
819 * nodes of the platform bus and its children dynamic sysbus devices
821 arm_register_platform_bus_fdt_creator(fdt_params);
823 dev = qdev_create(NULL, TYPE_PLATFORM_BUS_DEVICE);
824 dev->id = TYPE_PLATFORM_BUS_DEVICE;
825 qdev_prop_set_uint32(dev, "num_irqs",
826 platform_bus_params.platform_bus_num_irqs);
827 qdev_prop_set_uint32(dev, "mmio_size",
828 platform_bus_params.platform_bus_size);
829 qdev_init_nofail(dev);
830 s = SYS_BUS_DEVICE(dev);
832 for (i = 0; i < platform_bus_params.platform_bus_num_irqs; i++) {
833 int irqn = platform_bus_params.platform_bus_first_irq + i;
834 sysbus_connect_irq(s, i, pic[irqn]);
837 memory_region_add_subregion(sysmem,
838 platform_bus_params.platform_bus_base,
839 sysbus_mmio_get_region(s, 0));
842 static void *machvirt_dtb(const struct arm_boot_info *binfo, int *fdt_size)
844 const VirtBoardInfo *board = (const VirtBoardInfo *)binfo;
846 *fdt_size = board->fdt_size;
847 return board->fdt;
850 static void virt_build_smbios(VirtGuestInfo *guest_info)
852 FWCfgState *fw_cfg = guest_info->fw_cfg;
853 uint8_t *smbios_tables, *smbios_anchor;
854 size_t smbios_tables_len, smbios_anchor_len;
856 if (!fw_cfg) {
857 return;
860 smbios_set_defaults("QEMU", "QEMU Virtual Machine",
861 "1.0", false, true, SMBIOS_ENTRY_POINT_30);
863 smbios_get_tables(NULL, 0, &smbios_tables, &smbios_tables_len,
864 &smbios_anchor, &smbios_anchor_len);
866 if (smbios_anchor) {
867 fw_cfg_add_file(fw_cfg, "etc/smbios/smbios-tables",
868 smbios_tables, smbios_tables_len);
869 fw_cfg_add_file(fw_cfg, "etc/smbios/smbios-anchor",
870 smbios_anchor, smbios_anchor_len);
874 static
875 void virt_guest_info_machine_done(Notifier *notifier, void *data)
877 VirtGuestInfoState *guest_info_state = container_of(notifier,
878 VirtGuestInfoState, machine_done);
879 virt_acpi_setup(&guest_info_state->info);
880 virt_build_smbios(&guest_info_state->info);
883 static void machvirt_init(MachineState *machine)
885 VirtMachineState *vms = VIRT_MACHINE(machine);
886 qemu_irq pic[NUM_IRQS];
887 MemoryRegion *sysmem = get_system_memory();
888 int n;
889 MemoryRegion *ram = g_new(MemoryRegion, 1);
890 const char *cpu_model = machine->cpu_model;
891 VirtBoardInfo *vbi;
892 VirtGuestInfoState *guest_info_state = g_malloc0(sizeof *guest_info_state);
893 VirtGuestInfo *guest_info = &guest_info_state->info;
894 char **cpustr;
896 if (!cpu_model) {
897 cpu_model = "cortex-a15";
900 /* Separate the actual CPU model name from any appended features */
901 cpustr = g_strsplit(cpu_model, ",", 2);
903 vbi = find_machine_info(cpustr[0]);
905 if (!vbi) {
906 error_report("mach-virt: CPU %s not supported", cpustr[0]);
907 exit(1);
910 vbi->smp_cpus = smp_cpus;
912 if (machine->ram_size > vbi->memmap[VIRT_MEM].size) {
913 error_report("mach-virt: cannot model more than 30GB RAM");
914 exit(1);
917 create_fdt(vbi);
919 for (n = 0; n < smp_cpus; n++) {
920 ObjectClass *oc = cpu_class_by_name(TYPE_ARM_CPU, cpustr[0]);
921 CPUClass *cc = CPU_CLASS(oc);
922 Object *cpuobj;
923 Error *err = NULL;
924 char *cpuopts = g_strdup(cpustr[1]);
926 if (!oc) {
927 fprintf(stderr, "Unable to find CPU definition\n");
928 exit(1);
930 cpuobj = object_new(object_class_get_name(oc));
932 /* Handle any CPU options specified by the user */
933 cc->parse_features(CPU(cpuobj), cpuopts, &err);
934 g_free(cpuopts);
935 if (err) {
936 error_report_err(err);
937 exit(1);
940 if (!vms->secure) {
941 object_property_set_bool(cpuobj, false, "has_el3", NULL);
944 object_property_set_int(cpuobj, QEMU_PSCI_CONDUIT_HVC, "psci-conduit",
945 NULL);
947 /* Secondary CPUs start in PSCI powered-down state */
948 if (n > 0) {
949 object_property_set_bool(cpuobj, true, "start-powered-off", NULL);
952 if (object_property_find(cpuobj, "reset-cbar", NULL)) {
953 object_property_set_int(cpuobj, vbi->memmap[VIRT_CPUPERIPHS].base,
954 "reset-cbar", &error_abort);
957 object_property_set_bool(cpuobj, true, "realized", NULL);
959 g_strfreev(cpustr);
960 fdt_add_timer_nodes(vbi);
961 fdt_add_cpu_nodes(vbi);
962 fdt_add_psci_node(vbi);
964 memory_region_allocate_system_memory(ram, NULL, "mach-virt.ram",
965 machine->ram_size);
966 memory_region_add_subregion(sysmem, vbi->memmap[VIRT_MEM].base, ram);
968 create_flash(vbi);
970 create_gic(vbi, pic);
972 create_uart(vbi, pic);
974 create_rtc(vbi, pic);
976 create_pcie(vbi, pic, vms->highmem);
978 /* Create mmio transports, so the user can create virtio backends
979 * (which will be automatically plugged in to the transports). If
980 * no backend is created the transport will just sit harmlessly idle.
982 create_virtio_devices(vbi, pic);
984 create_fw_cfg(vbi);
985 rom_set_fw(fw_cfg_find());
987 guest_info->smp_cpus = smp_cpus;
988 guest_info->fw_cfg = fw_cfg_find();
989 guest_info->memmap = vbi->memmap;
990 guest_info->irqmap = vbi->irqmap;
991 guest_info->use_highmem = vms->highmem;
992 guest_info_state->machine_done.notify = virt_guest_info_machine_done;
993 qemu_add_machine_init_done_notifier(&guest_info_state->machine_done);
995 vbi->bootinfo.ram_size = machine->ram_size;
996 vbi->bootinfo.kernel_filename = machine->kernel_filename;
997 vbi->bootinfo.kernel_cmdline = machine->kernel_cmdline;
998 vbi->bootinfo.initrd_filename = machine->initrd_filename;
999 vbi->bootinfo.nb_cpus = smp_cpus;
1000 vbi->bootinfo.board_id = -1;
1001 vbi->bootinfo.loader_start = vbi->memmap[VIRT_MEM].base;
1002 vbi->bootinfo.get_dtb = machvirt_dtb;
1003 vbi->bootinfo.firmware_loaded = bios_name || drive_get(IF_PFLASH, 0, 0);
1004 arm_load_kernel(ARM_CPU(first_cpu), &vbi->bootinfo);
1007 * arm_load_kernel machine init done notifier registration must
1008 * happen before the platform_bus_create call. In this latter,
1009 * another notifier is registered which adds platform bus nodes.
1010 * Notifiers are executed in registration reverse order.
1012 create_platform_bus(vbi, pic);
1015 static bool virt_get_secure(Object *obj, Error **errp)
1017 VirtMachineState *vms = VIRT_MACHINE(obj);
1019 return vms->secure;
1022 static void virt_set_secure(Object *obj, bool value, Error **errp)
1024 VirtMachineState *vms = VIRT_MACHINE(obj);
1026 vms->secure = value;
1029 static bool virt_get_highmem(Object *obj, Error **errp)
1031 VirtMachineState *vms = VIRT_MACHINE(obj);
1033 return vms->highmem;
1036 static void virt_set_highmem(Object *obj, bool value, Error **errp)
1038 VirtMachineState *vms = VIRT_MACHINE(obj);
1040 vms->highmem = value;
1043 static void virt_instance_init(Object *obj)
1045 VirtMachineState *vms = VIRT_MACHINE(obj);
1047 /* EL3 is enabled by default on virt */
1048 vms->secure = true;
1049 object_property_add_bool(obj, "secure", virt_get_secure,
1050 virt_set_secure, NULL);
1051 object_property_set_description(obj, "secure",
1052 "Set on/off to enable/disable the ARM "
1053 "Security Extensions (TrustZone)",
1054 NULL);
1056 /* High memory is enabled by default */
1057 vms->highmem = true;
1058 object_property_add_bool(obj, "highmem", virt_get_highmem,
1059 virt_set_highmem, NULL);
1060 object_property_set_description(obj, "highmem",
1061 "Set on/off to enable/disable using "
1062 "physical address space above 32 bits",
1063 NULL);
1066 static void virt_class_init(ObjectClass *oc, void *data)
1068 MachineClass *mc = MACHINE_CLASS(oc);
1070 mc->name = TYPE_VIRT_MACHINE;
1071 mc->desc = "ARM Virtual Machine",
1072 mc->init = machvirt_init;
1073 mc->max_cpus = 8;
1074 mc->has_dynamic_sysbus = true;
1075 mc->block_default_type = IF_VIRTIO;
1076 mc->no_cdrom = 1;
1079 static const TypeInfo machvirt_info = {
1080 .name = TYPE_VIRT_MACHINE,
1081 .parent = TYPE_MACHINE,
1082 .instance_size = sizeof(VirtMachineState),
1083 .instance_init = virt_instance_init,
1084 .class_size = sizeof(VirtMachineClass),
1085 .class_init = virt_class_init,
1088 static void machvirt_machine_init(void)
1090 type_register_static(&machvirt_info);
1093 machine_init(machvirt_machine_init);