]> git.proxmox.com Git - mirror_qemu.git/blame - hw/arm/virt.c
target-arm: Add CPU property to disable AArch64
[mirror_qemu.git] / hw / arm / virt.c
CommitLineData
f5fdcd6e
PM
1/*
2 * ARM mach-virt emulation
3 *
4 * Copyright (c) 2013 Linaro Limited
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2 or later, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * Emulate a virtual board which works by passing Linux all the information
19 * it needs about what devices are present via the device tree.
20 * There are some restrictions about what we can do here:
21 * + we can only present devices whose Linux drivers will work based
22 * purely on the device tree with no platform data at all
23 * + we want to present a very stripped-down minimalist platform,
24 * both because this reduces the security attack surface from the guest
25 * and also because it reduces our exposure to being broken when
26 * the kernel updates its device tree bindings and requires further
27 * information in a device binding that we aren't providing.
28 * This is essentially the same approach kvmtool uses.
29 */
30
31#include "hw/sysbus.h"
32#include "hw/arm/arm.h"
33#include "hw/arm/primecell.h"
34#include "hw/devices.h"
35#include "net/net.h"
fa1d36df 36#include "sysemu/block-backend.h"
f5fdcd6e
PM
37#include "sysemu/device_tree.h"
38#include "sysemu/sysemu.h"
39#include "sysemu/kvm.h"
40#include "hw/boards.h"
acf82361 41#include "hw/loader.h"
f5fdcd6e
PM
42#include "exec/address-spaces.h"
43#include "qemu/bitops.h"
44#include "qemu/error-report.h"
4ab29b82 45#include "hw/pci-host/gpex.h"
f5fdcd6e
PM
46
47#define NUM_VIRTIO_TRANSPORTS 32
48
49/* Number of external interrupt lines to configure the GIC with */
50#define NUM_IRQS 128
51
52#define GIC_FDT_IRQ_TYPE_SPI 0
53#define GIC_FDT_IRQ_TYPE_PPI 1
54
55#define GIC_FDT_IRQ_FLAGS_EDGE_LO_HI 1
56#define GIC_FDT_IRQ_FLAGS_EDGE_HI_LO 2
57#define GIC_FDT_IRQ_FLAGS_LEVEL_HI 4
58#define GIC_FDT_IRQ_FLAGS_LEVEL_LO 8
59
60#define GIC_FDT_IRQ_PPI_CPU_START 8
61#define GIC_FDT_IRQ_PPI_CPU_WIDTH 8
62
63enum {
64 VIRT_FLASH,
65 VIRT_MEM,
66 VIRT_CPUPERIPHS,
67 VIRT_GIC_DIST,
68 VIRT_GIC_CPU,
69 VIRT_UART,
70 VIRT_MMIO,
6e411af9 71 VIRT_RTC,
578f3c7b 72 VIRT_FW_CFG,
4ab29b82 73 VIRT_PCIE,
f5fdcd6e
PM
74};
75
76typedef struct MemMapEntry {
77 hwaddr base;
78 hwaddr size;
79} MemMapEntry;
80
81typedef struct VirtBoardInfo {
82 struct arm_boot_info bootinfo;
83 const char *cpu_model;
f5fdcd6e
PM
84 const MemMapEntry *memmap;
85 const int *irqmap;
86 int smp_cpus;
87 void *fdt;
88 int fdt_size;
89 uint32_t clock_phandle;
90} VirtBoardInfo;
91
c2919690
GB
92typedef struct {
93 MachineClass parent;
94 VirtBoardInfo *daughterboard;
95} VirtMachineClass;
96
97typedef struct {
98 MachineState parent;
083a5890 99 bool secure;
c2919690
GB
100} VirtMachineState;
101
102#define TYPE_VIRT_MACHINE "virt"
103#define VIRT_MACHINE(obj) \
104 OBJECT_CHECK(VirtMachineState, (obj), TYPE_VIRT_MACHINE)
105#define VIRT_MACHINE_GET_CLASS(obj) \
106 OBJECT_GET_CLASS(VirtMachineClass, obj, TYPE_VIRT_MACHINE)
107#define VIRT_MACHINE_CLASS(klass) \
108 OBJECT_CLASS_CHECK(VirtMachineClass, klass, TYPE_VIRT_MACHINE)
109
f5fdcd6e
PM
110/* Addresses and sizes of our components.
111 * 0..128MB is space for a flash device so we can run bootrom code such as UEFI.
112 * 128MB..256MB is used for miscellaneous device I/O.
113 * 256MB..1GB is reserved for possible future PCI support (ie where the
114 * PCI memory window will go if we add a PCI host controller).
115 * 1GB and up is RAM (which may happily spill over into the
116 * high memory region beyond 4GB).
117 * This represents a compromise between how much RAM can be given to
118 * a 32 bit VM and leaving space for expansion and in particular for PCI.
6e411af9
PM
119 * Note that devices should generally be placed at multiples of 0x10000,
120 * to accommodate guests using 64K pages.
f5fdcd6e
PM
121 */
122static const MemMapEntry a15memmap[] = {
123 /* Space up to 0x8000000 is reserved for a boot ROM */
fab46932
AJ
124 [VIRT_FLASH] = { 0, 0x08000000 },
125 [VIRT_CPUPERIPHS] = { 0x08000000, 0x00020000 },
f5fdcd6e 126 /* GIC distributor and CPU interfaces sit inside the CPU peripheral space */
fab46932
AJ
127 [VIRT_GIC_DIST] = { 0x08000000, 0x00010000 },
128 [VIRT_GIC_CPU] = { 0x08010000, 0x00010000 },
129 [VIRT_UART] = { 0x09000000, 0x00001000 },
130 [VIRT_RTC] = { 0x09010000, 0x00001000 },
578f3c7b 131 [VIRT_FW_CFG] = { 0x09020000, 0x0000000a },
fab46932 132 [VIRT_MMIO] = { 0x0a000000, 0x00000200 },
f5fdcd6e 133 /* ...repeating for a total of NUM_VIRTIO_TRANSPORTS, each of that size */
4ab29b82
AG
134 /*
135 * PCIE verbose map:
136 *
137 * MMIO window { 0x10000000, 0x2eff0000 },
138 * PIO window { 0x3eff0000, 0x00010000 },
139 * ECAM { 0x3f000000, 0x01000000 },
140 */
141 [VIRT_PCIE] = { 0x10000000, 0x30000000 },
fab46932 142 [VIRT_MEM] = { 0x40000000, 30ULL * 1024 * 1024 * 1024 },
f5fdcd6e
PM
143};
144
145static const int a15irqmap[] = {
146 [VIRT_UART] = 1,
6e411af9 147 [VIRT_RTC] = 2,
4ab29b82 148 [VIRT_PCIE] = 3, /* ... to 6 */
f5fdcd6e
PM
149 [VIRT_MMIO] = 16, /* ...to 16 + NUM_VIRTIO_TRANSPORTS - 1 */
150};
151
152static VirtBoardInfo machines[] = {
153 {
154 .cpu_model = "cortex-a15",
f5fdcd6e
PM
155 .memmap = a15memmap,
156 .irqmap = a15irqmap,
157 },
f42c5c8e
PM
158 {
159 .cpu_model = "cortex-a57",
160 .memmap = a15memmap,
161 .irqmap = a15irqmap,
162 },
198aa064
PM
163 {
164 .cpu_model = "host",
198aa064
PM
165 .memmap = a15memmap,
166 .irqmap = a15irqmap,
167 },
f5fdcd6e
PM
168};
169
170static VirtBoardInfo *find_machine_info(const char *cpu)
171{
172 int i;
173
174 for (i = 0; i < ARRAY_SIZE(machines); i++) {
175 if (strcmp(cpu, machines[i].cpu_model) == 0) {
176 return &machines[i];
177 }
178 }
179 return NULL;
180}
181
182static void create_fdt(VirtBoardInfo *vbi)
183{
184 void *fdt = create_device_tree(&vbi->fdt_size);
185
186 if (!fdt) {
187 error_report("create_device_tree() failed");
188 exit(1);
189 }
190
191 vbi->fdt = fdt;
192
193 /* Header */
5a4348d1
PC
194 qemu_fdt_setprop_string(fdt, "/", "compatible", "linux,dummy-virt");
195 qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2);
196 qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2);
f5fdcd6e
PM
197
198 /*
199 * /chosen and /memory nodes must exist for load_dtb
200 * to fill in necessary properties later
201 */
5a4348d1
PC
202 qemu_fdt_add_subnode(fdt, "/chosen");
203 qemu_fdt_add_subnode(fdt, "/memory");
204 qemu_fdt_setprop_string(fdt, "/memory", "device_type", "memory");
f5fdcd6e
PM
205
206 /* Clock node, for the benefit of the UART. The kernel device tree
207 * binding documentation claims the PL011 node clock properties are
208 * optional but in practice if you omit them the kernel refuses to
209 * probe for the device.
210 */
5a4348d1
PC
211 vbi->clock_phandle = qemu_fdt_alloc_phandle(fdt);
212 qemu_fdt_add_subnode(fdt, "/apb-pclk");
213 qemu_fdt_setprop_string(fdt, "/apb-pclk", "compatible", "fixed-clock");
214 qemu_fdt_setprop_cell(fdt, "/apb-pclk", "#clock-cells", 0x0);
215 qemu_fdt_setprop_cell(fdt, "/apb-pclk", "clock-frequency", 24000000);
216 qemu_fdt_setprop_string(fdt, "/apb-pclk", "clock-output-names",
f5fdcd6e 217 "clk24mhz");
5a4348d1 218 qemu_fdt_setprop_cell(fdt, "/apb-pclk", "phandle", vbi->clock_phandle);
f5fdcd6e 219
06955739
PS
220}
221
222static void fdt_add_psci_node(const VirtBoardInfo *vbi)
223{
211b0169
RH
224 uint32_t cpu_suspend_fn;
225 uint32_t cpu_off_fn;
226 uint32_t cpu_on_fn;
227 uint32_t migrate_fn;
06955739
PS
228 void *fdt = vbi->fdt;
229 ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(0));
230
211b0169
RH
231 qemu_fdt_add_subnode(fdt, "/psci");
232 if (armcpu->psci_version == 2) {
233 const char comp[] = "arm,psci-0.2\0arm,psci";
234 qemu_fdt_setprop(fdt, "/psci", "compatible", comp, sizeof(comp));
863714ba 235
211b0169
RH
236 cpu_off_fn = QEMU_PSCI_0_2_FN_CPU_OFF;
237 if (arm_feature(&armcpu->env, ARM_FEATURE_AARCH64)) {
238 cpu_suspend_fn = QEMU_PSCI_0_2_FN64_CPU_SUSPEND;
239 cpu_on_fn = QEMU_PSCI_0_2_FN64_CPU_ON;
240 migrate_fn = QEMU_PSCI_0_2_FN64_MIGRATE;
241 } else {
242 cpu_suspend_fn = QEMU_PSCI_0_2_FN_CPU_SUSPEND;
243 cpu_on_fn = QEMU_PSCI_0_2_FN_CPU_ON;
244 migrate_fn = QEMU_PSCI_0_2_FN_MIGRATE;
06955739 245 }
211b0169
RH
246 } else {
247 qemu_fdt_setprop_string(fdt, "/psci", "compatible", "arm,psci");
06955739 248
211b0169
RH
249 cpu_suspend_fn = QEMU_PSCI_0_1_FN_CPU_SUSPEND;
250 cpu_off_fn = QEMU_PSCI_0_1_FN_CPU_OFF;
251 cpu_on_fn = QEMU_PSCI_0_1_FN_CPU_ON;
252 migrate_fn = QEMU_PSCI_0_1_FN_MIGRATE;
f5fdcd6e 253 }
211b0169
RH
254
255 /* We adopt the PSCI spec's nomenclature, and use 'conduit' to refer
256 * to the instruction that should be used to invoke PSCI functions.
257 * However, the device tree binding uses 'method' instead, so that is
258 * what we should use here.
259 */
260 qemu_fdt_setprop_string(fdt, "/psci", "method", "hvc");
261
262 qemu_fdt_setprop_cell(fdt, "/psci", "cpu_suspend", cpu_suspend_fn);
263 qemu_fdt_setprop_cell(fdt, "/psci", "cpu_off", cpu_off_fn);
264 qemu_fdt_setprop_cell(fdt, "/psci", "cpu_on", cpu_on_fn);
265 qemu_fdt_setprop_cell(fdt, "/psci", "migrate", migrate_fn);
f5fdcd6e
PM
266}
267
268static void fdt_add_timer_nodes(const VirtBoardInfo *vbi)
269{
270 /* Note that on A15 h/w these interrupts are level-triggered,
271 * but for the GIC implementation provided by both QEMU and KVM
272 * they are edge-triggered.
273 */
b32a9509 274 ARMCPU *armcpu;
f5fdcd6e
PM
275 uint32_t irqflags = GIC_FDT_IRQ_FLAGS_EDGE_LO_HI;
276
277 irqflags = deposit32(irqflags, GIC_FDT_IRQ_PPI_CPU_START,
278 GIC_FDT_IRQ_PPI_CPU_WIDTH, (1 << vbi->smp_cpus) - 1);
279
5a4348d1 280 qemu_fdt_add_subnode(vbi->fdt, "/timer");
b32a9509
CF
281
282 armcpu = ARM_CPU(qemu_get_cpu(0));
283 if (arm_feature(&armcpu->env, ARM_FEATURE_V8)) {
284 const char compat[] = "arm,armv8-timer\0arm,armv7-timer";
285 qemu_fdt_setprop(vbi->fdt, "/timer", "compatible",
286 compat, sizeof(compat));
287 } else {
288 qemu_fdt_setprop_string(vbi->fdt, "/timer", "compatible",
289 "arm,armv7-timer");
290 }
5a4348d1 291 qemu_fdt_setprop_cells(vbi->fdt, "/timer", "interrupts",
f5fdcd6e
PM
292 GIC_FDT_IRQ_TYPE_PPI, 13, irqflags,
293 GIC_FDT_IRQ_TYPE_PPI, 14, irqflags,
294 GIC_FDT_IRQ_TYPE_PPI, 11, irqflags,
295 GIC_FDT_IRQ_TYPE_PPI, 10, irqflags);
296}
297
298static void fdt_add_cpu_nodes(const VirtBoardInfo *vbi)
299{
300 int cpu;
301
5a4348d1
PC
302 qemu_fdt_add_subnode(vbi->fdt, "/cpus");
303 qemu_fdt_setprop_cell(vbi->fdt, "/cpus", "#address-cells", 0x1);
304 qemu_fdt_setprop_cell(vbi->fdt, "/cpus", "#size-cells", 0x0);
f5fdcd6e
PM
305
306 for (cpu = vbi->smp_cpus - 1; cpu >= 0; cpu--) {
307 char *nodename = g_strdup_printf("/cpus/cpu@%d", cpu);
308 ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(cpu));
309
5a4348d1
PC
310 qemu_fdt_add_subnode(vbi->fdt, nodename);
311 qemu_fdt_setprop_string(vbi->fdt, nodename, "device_type", "cpu");
312 qemu_fdt_setprop_string(vbi->fdt, nodename, "compatible",
f5fdcd6e
PM
313 armcpu->dtb_compatible);
314
315 if (vbi->smp_cpus > 1) {
5a4348d1 316 qemu_fdt_setprop_string(vbi->fdt, nodename,
f5fdcd6e
PM
317 "enable-method", "psci");
318 }
319
5a4348d1 320 qemu_fdt_setprop_cell(vbi->fdt, nodename, "reg", cpu);
f5fdcd6e
PM
321 g_free(nodename);
322 }
323}
324
4ab29b82 325static uint32_t fdt_add_gic_node(const VirtBoardInfo *vbi)
f5fdcd6e
PM
326{
327 uint32_t gic_phandle;
328
5a4348d1
PC
329 gic_phandle = qemu_fdt_alloc_phandle(vbi->fdt);
330 qemu_fdt_setprop_cell(vbi->fdt, "/", "interrupt-parent", gic_phandle);
f5fdcd6e 331
5a4348d1 332 qemu_fdt_add_subnode(vbi->fdt, "/intc");
64204743 333 /* 'cortex-a15-gic' means 'GIC v2' */
5a4348d1 334 qemu_fdt_setprop_string(vbi->fdt, "/intc", "compatible",
64204743 335 "arm,cortex-a15-gic");
5a4348d1
PC
336 qemu_fdt_setprop_cell(vbi->fdt, "/intc", "#interrupt-cells", 3);
337 qemu_fdt_setprop(vbi->fdt, "/intc", "interrupt-controller", NULL, 0);
338 qemu_fdt_setprop_sized_cells(vbi->fdt, "/intc", "reg",
f5fdcd6e
PM
339 2, vbi->memmap[VIRT_GIC_DIST].base,
340 2, vbi->memmap[VIRT_GIC_DIST].size,
341 2, vbi->memmap[VIRT_GIC_CPU].base,
342 2, vbi->memmap[VIRT_GIC_CPU].size);
5a4348d1 343 qemu_fdt_setprop_cell(vbi->fdt, "/intc", "phandle", gic_phandle);
4ab29b82
AG
344
345 return gic_phandle;
f5fdcd6e
PM
346}
347
4ab29b82 348static uint32_t create_gic(const VirtBoardInfo *vbi, qemu_irq *pic)
64204743
PM
349{
350 /* We create a standalone GIC v2 */
351 DeviceState *gicdev;
352 SysBusDevice *gicbusdev;
353 const char *gictype = "arm_gic";
354 int i;
355
356 if (kvm_irqchip_in_kernel()) {
357 gictype = "kvm-arm-gic";
358 }
359
360 gicdev = qdev_create(NULL, gictype);
361 qdev_prop_set_uint32(gicdev, "revision", 2);
362 qdev_prop_set_uint32(gicdev, "num-cpu", smp_cpus);
363 /* Note that the num-irq property counts both internal and external
364 * interrupts; there are always 32 of the former (mandated by GIC spec).
365 */
366 qdev_prop_set_uint32(gicdev, "num-irq", NUM_IRQS + 32);
367 qdev_init_nofail(gicdev);
368 gicbusdev = SYS_BUS_DEVICE(gicdev);
369 sysbus_mmio_map(gicbusdev, 0, vbi->memmap[VIRT_GIC_DIST].base);
370 sysbus_mmio_map(gicbusdev, 1, vbi->memmap[VIRT_GIC_CPU].base);
371
372 /* Wire the outputs from each CPU's generic timer to the
373 * appropriate GIC PPI inputs, and the GIC's IRQ output to
374 * the CPU's IRQ input.
375 */
376 for (i = 0; i < smp_cpus; i++) {
377 DeviceState *cpudev = DEVICE(qemu_get_cpu(i));
378 int ppibase = NUM_IRQS + i * 32;
379 /* physical timer; we wire it up to the non-secure timer's ID,
380 * since a real A15 always has TrustZone but QEMU doesn't.
381 */
382 qdev_connect_gpio_out(cpudev, 0,
383 qdev_get_gpio_in(gicdev, ppibase + 30));
384 /* virtual timer */
385 qdev_connect_gpio_out(cpudev, 1,
386 qdev_get_gpio_in(gicdev, ppibase + 27));
387
388 sysbus_connect_irq(gicbusdev, i, qdev_get_gpio_in(cpudev, ARM_CPU_IRQ));
389 }
390
391 for (i = 0; i < NUM_IRQS; i++) {
392 pic[i] = qdev_get_gpio_in(gicdev, i);
393 }
394
4ab29b82 395 return fdt_add_gic_node(vbi);
64204743
PM
396}
397
f5fdcd6e
PM
398static void create_uart(const VirtBoardInfo *vbi, qemu_irq *pic)
399{
400 char *nodename;
401 hwaddr base = vbi->memmap[VIRT_UART].base;
402 hwaddr size = vbi->memmap[VIRT_UART].size;
403 int irq = vbi->irqmap[VIRT_UART];
404 const char compat[] = "arm,pl011\0arm,primecell";
405 const char clocknames[] = "uartclk\0apb_pclk";
406
407 sysbus_create_simple("pl011", base, pic[irq]);
408
409 nodename = g_strdup_printf("/pl011@%" PRIx64, base);
5a4348d1 410 qemu_fdt_add_subnode(vbi->fdt, nodename);
f5fdcd6e 411 /* Note that we can't use setprop_string because of the embedded NUL */
5a4348d1 412 qemu_fdt_setprop(vbi->fdt, nodename, "compatible",
f5fdcd6e 413 compat, sizeof(compat));
5a4348d1 414 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg",
f5fdcd6e 415 2, base, 2, size);
5a4348d1 416 qemu_fdt_setprop_cells(vbi->fdt, nodename, "interrupts",
f5fdcd6e 417 GIC_FDT_IRQ_TYPE_SPI, irq,
0be969a2 418 GIC_FDT_IRQ_FLAGS_LEVEL_HI);
5a4348d1 419 qemu_fdt_setprop_cells(vbi->fdt, nodename, "clocks",
f5fdcd6e 420 vbi->clock_phandle, vbi->clock_phandle);
5a4348d1 421 qemu_fdt_setprop(vbi->fdt, nodename, "clock-names",
f5fdcd6e 422 clocknames, sizeof(clocknames));
f022b8e9 423
9c7074da 424 qemu_fdt_setprop_string(vbi->fdt, "/chosen", "stdout-path", nodename);
f5fdcd6e
PM
425 g_free(nodename);
426}
427
6e411af9
PM
428static void create_rtc(const VirtBoardInfo *vbi, qemu_irq *pic)
429{
430 char *nodename;
431 hwaddr base = vbi->memmap[VIRT_RTC].base;
432 hwaddr size = vbi->memmap[VIRT_RTC].size;
433 int irq = vbi->irqmap[VIRT_RTC];
434 const char compat[] = "arm,pl031\0arm,primecell";
435
436 sysbus_create_simple("pl031", base, pic[irq]);
437
438 nodename = g_strdup_printf("/pl031@%" PRIx64, base);
439 qemu_fdt_add_subnode(vbi->fdt, nodename);
440 qemu_fdt_setprop(vbi->fdt, nodename, "compatible", compat, sizeof(compat));
441 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg",
442 2, base, 2, size);
443 qemu_fdt_setprop_cells(vbi->fdt, nodename, "interrupts",
444 GIC_FDT_IRQ_TYPE_SPI, irq,
0be969a2 445 GIC_FDT_IRQ_FLAGS_LEVEL_HI);
6e411af9
PM
446 qemu_fdt_setprop_cell(vbi->fdt, nodename, "clocks", vbi->clock_phandle);
447 qemu_fdt_setprop_string(vbi->fdt, nodename, "clock-names", "apb_pclk");
448 g_free(nodename);
449}
450
f5fdcd6e
PM
451static void create_virtio_devices(const VirtBoardInfo *vbi, qemu_irq *pic)
452{
453 int i;
454 hwaddr size = vbi->memmap[VIRT_MMIO].size;
455
587078f0
LE
456 /* We create the transports in forwards order. Since qbus_realize()
457 * prepends (not appends) new child buses, the incrementing loop below will
458 * create a list of virtio-mmio buses with decreasing base addresses.
459 *
460 * When a -device option is processed from the command line,
461 * qbus_find_recursive() picks the next free virtio-mmio bus in forwards
462 * order. The upshot is that -device options in increasing command line
463 * order are mapped to virtio-mmio buses with decreasing base addresses.
464 *
465 * When this code was originally written, that arrangement ensured that the
466 * guest Linux kernel would give the lowest "name" (/dev/vda, eth0, etc) to
467 * the first -device on the command line. (The end-to-end order is a
468 * function of this loop, qbus_realize(), qbus_find_recursive(), and the
469 * guest kernel's name-to-address assignment strategy.)
470 *
471 * Meanwhile, the kernel's traversal seems to have been reversed; see eg.
472 * the message, if not necessarily the code, of commit 70161ff336.
473 * Therefore the loop now establishes the inverse of the original intent.
474 *
475 * Unfortunately, we can't counteract the kernel change by reversing the
476 * loop; it would break existing command lines.
477 *
478 * In any case, the kernel makes no guarantee about the stability of
479 * enumeration order of virtio devices (as demonstrated by it changing
480 * between kernel versions). For reliable and stable identification
481 * of disks users must use UUIDs or similar mechanisms.
f5fdcd6e
PM
482 */
483 for (i = 0; i < NUM_VIRTIO_TRANSPORTS; i++) {
484 int irq = vbi->irqmap[VIRT_MMIO] + i;
485 hwaddr base = vbi->memmap[VIRT_MMIO].base + i * size;
486
487 sysbus_create_simple("virtio-mmio", base, pic[irq]);
488 }
489
587078f0
LE
490 /* We add dtb nodes in reverse order so that they appear in the finished
491 * device tree lowest address first.
492 *
493 * Note that this mapping is independent of the loop above. The previous
494 * loop influences virtio device to virtio transport assignment, whereas
495 * this loop controls how virtio transports are laid out in the dtb.
496 */
f5fdcd6e
PM
497 for (i = NUM_VIRTIO_TRANSPORTS - 1; i >= 0; i--) {
498 char *nodename;
499 int irq = vbi->irqmap[VIRT_MMIO] + i;
500 hwaddr base = vbi->memmap[VIRT_MMIO].base + i * size;
501
502 nodename = g_strdup_printf("/virtio_mmio@%" PRIx64, base);
5a4348d1
PC
503 qemu_fdt_add_subnode(vbi->fdt, nodename);
504 qemu_fdt_setprop_string(vbi->fdt, nodename,
505 "compatible", "virtio,mmio");
506 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg",
507 2, base, 2, size);
508 qemu_fdt_setprop_cells(vbi->fdt, nodename, "interrupts",
509 GIC_FDT_IRQ_TYPE_SPI, irq,
510 GIC_FDT_IRQ_FLAGS_EDGE_LO_HI);
f5fdcd6e
PM
511 g_free(nodename);
512 }
513}
514
acf82361
PM
515static void create_one_flash(const char *name, hwaddr flashbase,
516 hwaddr flashsize)
517{
518 /* Create and map a single flash device. We use the same
519 * parameters as the flash devices on the Versatile Express board.
520 */
521 DriveInfo *dinfo = drive_get_next(IF_PFLASH);
522 DeviceState *dev = qdev_create(NULL, "cfi.pflash01");
523 const uint64_t sectorlength = 256 * 1024;
524
fa1d36df 525 if (dinfo && qdev_prop_set_drive(dev, "drive",
4be74634 526 blk_by_legacy_dinfo(dinfo))) {
acf82361
PM
527 abort();
528 }
529
530 qdev_prop_set_uint32(dev, "num-blocks", flashsize / sectorlength);
531 qdev_prop_set_uint64(dev, "sector-length", sectorlength);
532 qdev_prop_set_uint8(dev, "width", 4);
533 qdev_prop_set_uint8(dev, "device-width", 2);
534 qdev_prop_set_uint8(dev, "big-endian", 0);
535 qdev_prop_set_uint16(dev, "id0", 0x89);
536 qdev_prop_set_uint16(dev, "id1", 0x18);
537 qdev_prop_set_uint16(dev, "id2", 0x00);
538 qdev_prop_set_uint16(dev, "id3", 0x00);
539 qdev_prop_set_string(dev, "name", name);
540 qdev_init_nofail(dev);
541
542 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, flashbase);
543}
544
545static void create_flash(const VirtBoardInfo *vbi)
546{
547 /* Create two flash devices to fill the VIRT_FLASH space in the memmap.
548 * Any file passed via -bios goes in the first of these.
549 */
550 hwaddr flashsize = vbi->memmap[VIRT_FLASH].size / 2;
551 hwaddr flashbase = vbi->memmap[VIRT_FLASH].base;
552 char *nodename;
553
554 if (bios_name) {
555 const char *fn;
556
557 if (drive_get(IF_PFLASH, 0, 0)) {
558 error_report("The contents of the first flash device may be "
559 "specified with -bios or with -drive if=pflash... "
560 "but you cannot use both options at once");
561 exit(1);
562 }
563 fn = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
564 if (!fn || load_image_targphys(fn, flashbase, flashsize) < 0) {
565 error_report("Could not load ROM image '%s'", bios_name);
566 exit(1);
567 }
568 }
569
570 create_one_flash("virt.flash0", flashbase, flashsize);
571 create_one_flash("virt.flash1", flashbase + flashsize, flashsize);
572
573 nodename = g_strdup_printf("/flash@%" PRIx64, flashbase);
574 qemu_fdt_add_subnode(vbi->fdt, nodename);
575 qemu_fdt_setprop_string(vbi->fdt, nodename, "compatible", "cfi-flash");
576 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg",
577 2, flashbase, 2, flashsize,
578 2, flashbase + flashsize, 2, flashsize);
579 qemu_fdt_setprop_cell(vbi->fdt, nodename, "bank-width", 4);
580 g_free(nodename);
581}
582
578f3c7b
LE
583static void create_fw_cfg(const VirtBoardInfo *vbi)
584{
585 hwaddr base = vbi->memmap[VIRT_FW_CFG].base;
586 hwaddr size = vbi->memmap[VIRT_FW_CFG].size;
587 char *nodename;
588
589 fw_cfg_init_mem_wide(base + 8, base, 8);
590
591 nodename = g_strdup_printf("/fw-cfg@%" PRIx64, base);
592 qemu_fdt_add_subnode(vbi->fdt, nodename);
593 qemu_fdt_setprop_string(vbi->fdt, nodename,
594 "compatible", "qemu,fw-cfg-mmio");
595 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg",
596 2, base, 2, size);
597 g_free(nodename);
598}
599
4ab29b82
AG
600static void create_pcie_irq_map(const VirtBoardInfo *vbi, uint32_t gic_phandle,
601 int first_irq, const char *nodename)
602{
603 int devfn, pin;
604 uint32_t full_irq_map[4 * 4 * 8] = { 0 };
605 uint32_t *irq_map = full_irq_map;
606
607 for (devfn = 0; devfn <= 0x18; devfn += 0x8) {
608 for (pin = 0; pin < 4; pin++) {
609 int irq_type = GIC_FDT_IRQ_TYPE_SPI;
610 int irq_nr = first_irq + ((pin + PCI_SLOT(devfn)) % PCI_NUM_PINS);
611 int irq_level = GIC_FDT_IRQ_FLAGS_LEVEL_HI;
612 int i;
613
614 uint32_t map[] = {
615 devfn << 8, 0, 0, /* devfn */
616 pin + 1, /* PCI pin */
617 gic_phandle, irq_type, irq_nr, irq_level }; /* GIC irq */
618
619 /* Convert map to big endian */
620 for (i = 0; i < 8; i++) {
621 irq_map[i] = cpu_to_be32(map[i]);
622 }
623 irq_map += 8;
624 }
625 }
626
627 qemu_fdt_setprop(vbi->fdt, nodename, "interrupt-map",
628 full_irq_map, sizeof(full_irq_map));
629
630 qemu_fdt_setprop_cells(vbi->fdt, nodename, "interrupt-map-mask",
631 0x1800, 0, 0, /* devfn (PCI_SLOT(3)) */
632 0x7 /* PCI irq */);
633}
634
635static void create_pcie(const VirtBoardInfo *vbi, qemu_irq *pic,
636 uint32_t gic_phandle)
637{
638 hwaddr base = vbi->memmap[VIRT_PCIE].base;
639 hwaddr size = vbi->memmap[VIRT_PCIE].size;
640 hwaddr end = base + size;
641 hwaddr size_mmio;
642 hwaddr size_ioport = 64 * 1024;
643 int nr_pcie_buses = 16;
644 hwaddr size_ecam = PCIE_MMCFG_SIZE_MIN * nr_pcie_buses;
645 hwaddr base_mmio = base;
646 hwaddr base_ioport;
647 hwaddr base_ecam;
648 int irq = vbi->irqmap[VIRT_PCIE];
649 MemoryRegion *mmio_alias;
650 MemoryRegion *mmio_reg;
651 MemoryRegion *ecam_alias;
652 MemoryRegion *ecam_reg;
653 DeviceState *dev;
654 char *nodename;
655 int i;
656
657 base_ecam = QEMU_ALIGN_DOWN(end - size_ecam, size_ecam);
658 base_ioport = QEMU_ALIGN_DOWN(base_ecam - size_ioport, size_ioport);
659 size_mmio = base_ioport - base;
660
661 dev = qdev_create(NULL, TYPE_GPEX_HOST);
662 qdev_init_nofail(dev);
663
664 /* Map only the first size_ecam bytes of ECAM space */
665 ecam_alias = g_new0(MemoryRegion, 1);
666 ecam_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0);
667 memory_region_init_alias(ecam_alias, OBJECT(dev), "pcie-ecam",
668 ecam_reg, 0, size_ecam);
669 memory_region_add_subregion(get_system_memory(), base_ecam, ecam_alias);
670
671 /* Map the MMIO window into system address space so as to expose
672 * the section of PCI MMIO space which starts at the same base address
673 * (ie 1:1 mapping for that part of PCI MMIO space visible through
674 * the window).
675 */
676 mmio_alias = g_new0(MemoryRegion, 1);
677 mmio_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 1);
678 memory_region_init_alias(mmio_alias, OBJECT(dev), "pcie-mmio",
679 mmio_reg, base_mmio, size_mmio);
680 memory_region_add_subregion(get_system_memory(), base_mmio, mmio_alias);
681
682 /* Map IO port space */
683 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 2, base_ioport);
684
685 for (i = 0; i < GPEX_NUM_IRQS; i++) {
686 sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, pic[irq + i]);
687 }
688
689 nodename = g_strdup_printf("/pcie@%" PRIx64, base);
690 qemu_fdt_add_subnode(vbi->fdt, nodename);
691 qemu_fdt_setprop_string(vbi->fdt, nodename,
692 "compatible", "pci-host-ecam-generic");
693 qemu_fdt_setprop_string(vbi->fdt, nodename, "device_type", "pci");
694 qemu_fdt_setprop_cell(vbi->fdt, nodename, "#address-cells", 3);
695 qemu_fdt_setprop_cell(vbi->fdt, nodename, "#size-cells", 2);
696 qemu_fdt_setprop_cells(vbi->fdt, nodename, "bus-range", 0,
697 nr_pcie_buses - 1);
698
699 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg",
700 2, base_ecam, 2, size_ecam);
701 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "ranges",
702 1, FDT_PCI_RANGE_IOPORT, 2, 0,
703 2, base_ioport, 2, size_ioport,
704 1, FDT_PCI_RANGE_MMIO, 2, base_mmio,
705 2, base_mmio, 2, size_mmio);
706
707 qemu_fdt_setprop_cell(vbi->fdt, nodename, "#interrupt-cells", 1);
708 create_pcie_irq_map(vbi, gic_phandle, irq, nodename);
709
710 g_free(nodename);
711}
712
f5fdcd6e
PM
713static void *machvirt_dtb(const struct arm_boot_info *binfo, int *fdt_size)
714{
715 const VirtBoardInfo *board = (const VirtBoardInfo *)binfo;
716
717 *fdt_size = board->fdt_size;
718 return board->fdt;
719}
720
3ef96221 721static void machvirt_init(MachineState *machine)
f5fdcd6e 722{
e5a5604f 723 VirtMachineState *vms = VIRT_MACHINE(machine);
f5fdcd6e
PM
724 qemu_irq pic[NUM_IRQS];
725 MemoryRegion *sysmem = get_system_memory();
726 int n;
727 MemoryRegion *ram = g_new(MemoryRegion, 1);
3ef96221 728 const char *cpu_model = machine->cpu_model;
f5fdcd6e 729 VirtBoardInfo *vbi;
4ab29b82 730 uint32_t gic_phandle;
f5fdcd6e
PM
731
732 if (!cpu_model) {
733 cpu_model = "cortex-a15";
734 }
735
736 vbi = find_machine_info(cpu_model);
737
738 if (!vbi) {
739 error_report("mach-virt: CPU %s not supported", cpu_model);
740 exit(1);
741 }
742
743 vbi->smp_cpus = smp_cpus;
744
3ef96221 745 if (machine->ram_size > vbi->memmap[VIRT_MEM].size) {
f5fdcd6e
PM
746 error_report("mach-virt: cannot model more than 30GB RAM");
747 exit(1);
748 }
749
750 create_fdt(vbi);
f5fdcd6e
PM
751
752 for (n = 0; n < smp_cpus; n++) {
753 ObjectClass *oc = cpu_class_by_name(TYPE_ARM_CPU, cpu_model);
754 Object *cpuobj;
755
756 if (!oc) {
757 fprintf(stderr, "Unable to find CPU definition\n");
758 exit(1);
759 }
760 cpuobj = object_new(object_class_get_name(oc));
761
e5a5604f
GB
762 if (!vms->secure) {
763 object_property_set_bool(cpuobj, false, "has_el3", NULL);
764 }
765
211b0169
RH
766 object_property_set_int(cpuobj, QEMU_PSCI_CONDUIT_HVC, "psci-conduit",
767 NULL);
768
f5fdcd6e
PM
769 /* Secondary CPUs start in PSCI powered-down state */
770 if (n > 0) {
771 object_property_set_bool(cpuobj, true, "start-powered-off", NULL);
772 }
ba750085
PM
773
774 if (object_property_find(cpuobj, "reset-cbar", NULL)) {
775 object_property_set_int(cpuobj, vbi->memmap[VIRT_CPUPERIPHS].base,
776 "reset-cbar", &error_abort);
777 }
778
f5fdcd6e
PM
779 object_property_set_bool(cpuobj, true, "realized", NULL);
780 }
b32a9509 781 fdt_add_timer_nodes(vbi);
f5fdcd6e 782 fdt_add_cpu_nodes(vbi);
06955739 783 fdt_add_psci_node(vbi);
f5fdcd6e 784
49946538
HT
785 memory_region_init_ram(ram, NULL, "mach-virt.ram", machine->ram_size,
786 &error_abort);
f5fdcd6e
PM
787 vmstate_register_ram_global(ram);
788 memory_region_add_subregion(sysmem, vbi->memmap[VIRT_MEM].base, ram);
789
acf82361
PM
790 create_flash(vbi);
791
4ab29b82 792 gic_phandle = create_gic(vbi, pic);
f5fdcd6e
PM
793
794 create_uart(vbi, pic);
795
6e411af9
PM
796 create_rtc(vbi, pic);
797
4ab29b82
AG
798 create_pcie(vbi, pic, gic_phandle);
799
f5fdcd6e
PM
800 /* Create mmio transports, so the user can create virtio backends
801 * (which will be automatically plugged in to the transports). If
802 * no backend is created the transport will just sit harmlessly idle.
803 */
804 create_virtio_devices(vbi, pic);
805
578f3c7b
LE
806 create_fw_cfg(vbi);
807
3ef96221
MA
808 vbi->bootinfo.ram_size = machine->ram_size;
809 vbi->bootinfo.kernel_filename = machine->kernel_filename;
810 vbi->bootinfo.kernel_cmdline = machine->kernel_cmdline;
811 vbi->bootinfo.initrd_filename = machine->initrd_filename;
f5fdcd6e
PM
812 vbi->bootinfo.nb_cpus = smp_cpus;
813 vbi->bootinfo.board_id = -1;
814 vbi->bootinfo.loader_start = vbi->memmap[VIRT_MEM].base;
815 vbi->bootinfo.get_dtb = machvirt_dtb;
aa351061 816 vbi->bootinfo.firmware_loaded = bios_name || drive_get(IF_PFLASH, 0, 0);
f5fdcd6e
PM
817 arm_load_kernel(ARM_CPU(first_cpu), &vbi->bootinfo);
818}
819
083a5890
GB
820static bool virt_get_secure(Object *obj, Error **errp)
821{
822 VirtMachineState *vms = VIRT_MACHINE(obj);
823
824 return vms->secure;
825}
826
827static void virt_set_secure(Object *obj, bool value, Error **errp)
828{
829 VirtMachineState *vms = VIRT_MACHINE(obj);
830
831 vms->secure = value;
832}
833
834static void virt_instance_init(Object *obj)
835{
836 VirtMachineState *vms = VIRT_MACHINE(obj);
837
838 /* EL3 is enabled by default on virt */
839 vms->secure = true;
840 object_property_add_bool(obj, "secure", virt_get_secure,
841 virt_set_secure, NULL);
842 object_property_set_description(obj, "secure",
843 "Set on/off to enable/disable the ARM "
844 "Security Extensions (TrustZone)",
845 NULL);
846}
847
c2919690
GB
848static void virt_class_init(ObjectClass *oc, void *data)
849{
850 MachineClass *mc = MACHINE_CLASS(oc);
851
852 mc->name = TYPE_VIRT_MACHINE;
853 mc->desc = "ARM Virtual Machine",
854 mc->init = machvirt_init;
855 mc->max_cpus = 8;
856}
857
858static const TypeInfo machvirt_info = {
859 .name = TYPE_VIRT_MACHINE,
860 .parent = TYPE_MACHINE,
861 .instance_size = sizeof(VirtMachineState),
083a5890 862 .instance_init = virt_instance_init,
c2919690
GB
863 .class_size = sizeof(VirtMachineClass),
864 .class_init = virt_class_init,
f5fdcd6e
PM
865};
866
867static void machvirt_machine_init(void)
868{
c2919690 869 type_register_static(&machvirt_info);
f5fdcd6e
PM
870}
871
872machine_init(machvirt_machine_init);