]> git.proxmox.com Git - mirror_qemu.git/blame - hw/arm/virt.c
sdhci: Implement DeviceClass reset
[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
12b16722 31#include "qemu/osdep.h"
f5fdcd6e
PM
32#include "hw/sysbus.h"
33#include "hw/arm/arm.h"
34#include "hw/arm/primecell.h"
afe0b380 35#include "hw/arm/virt.h"
f5fdcd6e
PM
36#include "hw/devices.h"
37#include "net/net.h"
fa1d36df 38#include "sysemu/block-backend.h"
f5fdcd6e
PM
39#include "sysemu/device_tree.h"
40#include "sysemu/sysemu.h"
41#include "sysemu/kvm.h"
42#include "hw/boards.h"
acf82361 43#include "hw/loader.h"
f5fdcd6e
PM
44#include "exec/address-spaces.h"
45#include "qemu/bitops.h"
46#include "qemu/error-report.h"
4ab29b82 47#include "hw/pci-host/gpex.h"
d7c2e2db 48#include "hw/arm/virt-acpi-build.h"
5f7a5a0e
EA
49#include "hw/arm/sysbus-fdt.h"
50#include "hw/platform-bus.h"
decf4f80 51#include "hw/arm/fdt.h"
0e3e858f 52#include "hw/intc/arm_gic_common.h"
e6fbcbc4 53#include "kvm_arm.h"
c30e1565 54#include "hw/smbios/smbios.h"
b92ad394 55#include "qapi/visitor.h"
3e6ebb64 56#include "standard-headers/linux/input.h"
f5fdcd6e 57
f5fdcd6e 58/* Number of external interrupt lines to configure the GIC with */
5f7a5a0e 59#define NUM_IRQS 256
f5fdcd6e 60
5f7a5a0e
EA
61#define PLATFORM_BUS_NUM_IRQS 64
62
63static ARMPlatformBusSystemParams platform_bus_params;
64
f5fdcd6e
PM
65typedef struct VirtBoardInfo {
66 struct arm_boot_info bootinfo;
67 const char *cpu_model;
f5fdcd6e
PM
68 const MemMapEntry *memmap;
69 const int *irqmap;
70 int smp_cpus;
71 void *fdt;
72 int fdt_size;
73 uint32_t clock_phandle;
747d009d 74 uint32_t gic_phandle;
bd204e63 75 uint32_t v2m_phandle;
f5fdcd6e
PM
76} VirtBoardInfo;
77
c2919690
GB
78typedef struct {
79 MachineClass parent;
80 VirtBoardInfo *daughterboard;
81} VirtMachineClass;
82
83typedef struct {
84 MachineState parent;
083a5890 85 bool secure;
5125f9cd 86 bool highmem;
b92ad394 87 int32_t gic_version;
c2919690
GB
88} VirtMachineState;
89
98cec76a 90#define TYPE_VIRT_MACHINE MACHINE_TYPE_NAME("virt")
c2919690
GB
91#define VIRT_MACHINE(obj) \
92 OBJECT_CHECK(VirtMachineState, (obj), TYPE_VIRT_MACHINE)
93#define VIRT_MACHINE_GET_CLASS(obj) \
94 OBJECT_GET_CLASS(VirtMachineClass, obj, TYPE_VIRT_MACHINE)
95#define VIRT_MACHINE_CLASS(klass) \
96 OBJECT_CLASS_CHECK(VirtMachineClass, klass, TYPE_VIRT_MACHINE)
97
71c27684
PM
98/* RAM limit in GB. Since VIRT_MEM starts at the 1GB mark, this means
99 * RAM can go up to the 256GB mark, leaving 256GB of the physical
100 * address space unallocated and free for future use between 256G and 512G.
101 * If we need to provide more RAM to VMs in the future then we need to:
102 * * allocate a second bank of RAM starting at 2TB and working up
103 * * fix the DT and ACPI table generation code in QEMU to correctly
104 * report two split lumps of RAM to the guest
105 * * fix KVM in the host kernel to allow guests with >40 bit address spaces
106 * (We don't want to fill all the way up to 512GB with RAM because
107 * we might want it for non-RAM purposes later. Conversely it seems
108 * reasonable to assume that anybody configuring a VM with a quarter
109 * of a terabyte of RAM will be doing it on a host with more than a
110 * terabyte of physical address space.)
111 */
112#define RAMLIMIT_GB 255
113#define RAMLIMIT_BYTES (RAMLIMIT_GB * 1024ULL * 1024 * 1024)
114
f5fdcd6e
PM
115/* Addresses and sizes of our components.
116 * 0..128MB is space for a flash device so we can run bootrom code such as UEFI.
117 * 128MB..256MB is used for miscellaneous device I/O.
118 * 256MB..1GB is reserved for possible future PCI support (ie where the
119 * PCI memory window will go if we add a PCI host controller).
120 * 1GB and up is RAM (which may happily spill over into the
121 * high memory region beyond 4GB).
122 * This represents a compromise between how much RAM can be given to
123 * a 32 bit VM and leaving space for expansion and in particular for PCI.
6e411af9
PM
124 * Note that devices should generally be placed at multiples of 0x10000,
125 * to accommodate guests using 64K pages.
f5fdcd6e
PM
126 */
127static const MemMapEntry a15memmap[] = {
128 /* Space up to 0x8000000 is reserved for a boot ROM */
94edf02c
EA
129 [VIRT_FLASH] = { 0, 0x08000000 },
130 [VIRT_CPUPERIPHS] = { 0x08000000, 0x00020000 },
f5fdcd6e 131 /* GIC distributor and CPU interfaces sit inside the CPU peripheral space */
94edf02c
EA
132 [VIRT_GIC_DIST] = { 0x08000000, 0x00010000 },
133 [VIRT_GIC_CPU] = { 0x08010000, 0x00010000 },
134 [VIRT_GIC_V2M] = { 0x08020000, 0x00001000 },
b92ad394
PF
135 /* The space in between here is reserved for GICv3 CPU/vCPU/HYP */
136 [VIRT_GIC_ITS] = { 0x08080000, 0x00020000 },
137 /* This redistributor space allows up to 2*64kB*123 CPUs */
138 [VIRT_GIC_REDIST] = { 0x080A0000, 0x00F60000 },
94edf02c
EA
139 [VIRT_UART] = { 0x09000000, 0x00001000 },
140 [VIRT_RTC] = { 0x09010000, 0x00001000 },
0b341a85 141 [VIRT_FW_CFG] = { 0x09020000, 0x00000018 },
b0a3721e 142 [VIRT_GPIO] = { 0x09030000, 0x00001000 },
3df708eb 143 [VIRT_SECURE_UART] = { 0x09040000, 0x00001000 },
94edf02c 144 [VIRT_MMIO] = { 0x0a000000, 0x00000200 },
f5fdcd6e 145 /* ...repeating for a total of NUM_VIRTIO_TRANSPORTS, each of that size */
94edf02c
EA
146 [VIRT_PLATFORM_BUS] = { 0x0c000000, 0x02000000 },
147 [VIRT_PCIE_MMIO] = { 0x10000000, 0x2eff0000 },
148 [VIRT_PCIE_PIO] = { 0x3eff0000, 0x00010000 },
149 [VIRT_PCIE_ECAM] = { 0x3f000000, 0x01000000 },
71c27684 150 [VIRT_MEM] = { 0x40000000, RAMLIMIT_BYTES },
5125f9cd
PF
151 /* Second PCIe window, 512GB wide at the 512GB boundary */
152 [VIRT_PCIE_MMIO_HIGH] = { 0x8000000000ULL, 0x8000000000ULL },
f5fdcd6e
PM
153};
154
155static const int a15irqmap[] = {
156 [VIRT_UART] = 1,
6e411af9 157 [VIRT_RTC] = 2,
4ab29b82 158 [VIRT_PCIE] = 3, /* ... to 6 */
b0a3721e 159 [VIRT_GPIO] = 7,
3df708eb 160 [VIRT_SECURE_UART] = 8,
f5fdcd6e 161 [VIRT_MMIO] = 16, /* ...to 16 + NUM_VIRTIO_TRANSPORTS - 1 */
bd204e63 162 [VIRT_GIC_V2M] = 48, /* ...to 48 + NUM_GICV2M_SPIS - 1 */
5f7a5a0e 163 [VIRT_PLATFORM_BUS] = 112, /* ...to 112 + PLATFORM_BUS_NUM_IRQS -1 */
f5fdcd6e
PM
164};
165
166static VirtBoardInfo machines[] = {
167 {
168 .cpu_model = "cortex-a15",
f5fdcd6e
PM
169 .memmap = a15memmap,
170 .irqmap = a15irqmap,
171 },
8772de2c
SZ
172 {
173 .cpu_model = "cortex-a53",
174 .memmap = a15memmap,
175 .irqmap = a15irqmap,
176 },
f42c5c8e
PM
177 {
178 .cpu_model = "cortex-a57",
179 .memmap = a15memmap,
180 .irqmap = a15irqmap,
181 },
198aa064
PM
182 {
183 .cpu_model = "host",
198aa064
PM
184 .memmap = a15memmap,
185 .irqmap = a15irqmap,
186 },
f5fdcd6e
PM
187};
188
189static VirtBoardInfo *find_machine_info(const char *cpu)
190{
191 int i;
192
193 for (i = 0; i < ARRAY_SIZE(machines); i++) {
194 if (strcmp(cpu, machines[i].cpu_model) == 0) {
195 return &machines[i];
196 }
197 }
198 return NULL;
199}
200
201static void create_fdt(VirtBoardInfo *vbi)
202{
203 void *fdt = create_device_tree(&vbi->fdt_size);
204
205 if (!fdt) {
206 error_report("create_device_tree() failed");
207 exit(1);
208 }
209
210 vbi->fdt = fdt;
211
212 /* Header */
5a4348d1
PC
213 qemu_fdt_setprop_string(fdt, "/", "compatible", "linux,dummy-virt");
214 qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2);
215 qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2);
f5fdcd6e
PM
216
217 /*
218 * /chosen and /memory nodes must exist for load_dtb
219 * to fill in necessary properties later
220 */
5a4348d1
PC
221 qemu_fdt_add_subnode(fdt, "/chosen");
222 qemu_fdt_add_subnode(fdt, "/memory");
223 qemu_fdt_setprop_string(fdt, "/memory", "device_type", "memory");
f5fdcd6e
PM
224
225 /* Clock node, for the benefit of the UART. The kernel device tree
226 * binding documentation claims the PL011 node clock properties are
227 * optional but in practice if you omit them the kernel refuses to
228 * probe for the device.
229 */
5a4348d1
PC
230 vbi->clock_phandle = qemu_fdt_alloc_phandle(fdt);
231 qemu_fdt_add_subnode(fdt, "/apb-pclk");
232 qemu_fdt_setprop_string(fdt, "/apb-pclk", "compatible", "fixed-clock");
233 qemu_fdt_setprop_cell(fdt, "/apb-pclk", "#clock-cells", 0x0);
234 qemu_fdt_setprop_cell(fdt, "/apb-pclk", "clock-frequency", 24000000);
235 qemu_fdt_setprop_string(fdt, "/apb-pclk", "clock-output-names",
f5fdcd6e 236 "clk24mhz");
5a4348d1 237 qemu_fdt_setprop_cell(fdt, "/apb-pclk", "phandle", vbi->clock_phandle);
f5fdcd6e 238
06955739
PS
239}
240
241static void fdt_add_psci_node(const VirtBoardInfo *vbi)
242{
211b0169
RH
243 uint32_t cpu_suspend_fn;
244 uint32_t cpu_off_fn;
245 uint32_t cpu_on_fn;
246 uint32_t migrate_fn;
06955739
PS
247 void *fdt = vbi->fdt;
248 ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(0));
249
211b0169
RH
250 qemu_fdt_add_subnode(fdt, "/psci");
251 if (armcpu->psci_version == 2) {
252 const char comp[] = "arm,psci-0.2\0arm,psci";
253 qemu_fdt_setprop(fdt, "/psci", "compatible", comp, sizeof(comp));
863714ba 254
211b0169
RH
255 cpu_off_fn = QEMU_PSCI_0_2_FN_CPU_OFF;
256 if (arm_feature(&armcpu->env, ARM_FEATURE_AARCH64)) {
257 cpu_suspend_fn = QEMU_PSCI_0_2_FN64_CPU_SUSPEND;
258 cpu_on_fn = QEMU_PSCI_0_2_FN64_CPU_ON;
259 migrate_fn = QEMU_PSCI_0_2_FN64_MIGRATE;
260 } else {
261 cpu_suspend_fn = QEMU_PSCI_0_2_FN_CPU_SUSPEND;
262 cpu_on_fn = QEMU_PSCI_0_2_FN_CPU_ON;
263 migrate_fn = QEMU_PSCI_0_2_FN_MIGRATE;
06955739 264 }
211b0169
RH
265 } else {
266 qemu_fdt_setprop_string(fdt, "/psci", "compatible", "arm,psci");
06955739 267
211b0169
RH
268 cpu_suspend_fn = QEMU_PSCI_0_1_FN_CPU_SUSPEND;
269 cpu_off_fn = QEMU_PSCI_0_1_FN_CPU_OFF;
270 cpu_on_fn = QEMU_PSCI_0_1_FN_CPU_ON;
271 migrate_fn = QEMU_PSCI_0_1_FN_MIGRATE;
f5fdcd6e 272 }
211b0169
RH
273
274 /* We adopt the PSCI spec's nomenclature, and use 'conduit' to refer
275 * to the instruction that should be used to invoke PSCI functions.
276 * However, the device tree binding uses 'method' instead, so that is
277 * what we should use here.
278 */
279 qemu_fdt_setprop_string(fdt, "/psci", "method", "hvc");
280
281 qemu_fdt_setprop_cell(fdt, "/psci", "cpu_suspend", cpu_suspend_fn);
282 qemu_fdt_setprop_cell(fdt, "/psci", "cpu_off", cpu_off_fn);
283 qemu_fdt_setprop_cell(fdt, "/psci", "cpu_on", cpu_on_fn);
284 qemu_fdt_setprop_cell(fdt, "/psci", "migrate", migrate_fn);
f5fdcd6e
PM
285}
286
b92ad394 287static void fdt_add_timer_nodes(const VirtBoardInfo *vbi, int gictype)
f5fdcd6e
PM
288{
289 /* Note that on A15 h/w these interrupts are level-triggered,
290 * but for the GIC implementation provided by both QEMU and KVM
291 * they are edge-triggered.
292 */
b32a9509 293 ARMCPU *armcpu;
f5fdcd6e
PM
294 uint32_t irqflags = GIC_FDT_IRQ_FLAGS_EDGE_LO_HI;
295
b92ad394
PF
296 if (gictype == 2) {
297 irqflags = deposit32(irqflags, GIC_FDT_IRQ_PPI_CPU_START,
298 GIC_FDT_IRQ_PPI_CPU_WIDTH,
299 (1 << vbi->smp_cpus) - 1);
300 }
f5fdcd6e 301
5a4348d1 302 qemu_fdt_add_subnode(vbi->fdt, "/timer");
b32a9509
CF
303
304 armcpu = ARM_CPU(qemu_get_cpu(0));
305 if (arm_feature(&armcpu->env, ARM_FEATURE_V8)) {
306 const char compat[] = "arm,armv8-timer\0arm,armv7-timer";
307 qemu_fdt_setprop(vbi->fdt, "/timer", "compatible",
308 compat, sizeof(compat));
309 } else {
310 qemu_fdt_setprop_string(vbi->fdt, "/timer", "compatible",
311 "arm,armv7-timer");
312 }
caa49adb 313 qemu_fdt_setprop(vbi->fdt, "/timer", "always-on", NULL, 0);
5a4348d1 314 qemu_fdt_setprop_cells(vbi->fdt, "/timer", "interrupts",
ee246400
SZ
315 GIC_FDT_IRQ_TYPE_PPI, ARCH_TIMER_S_EL1_IRQ, irqflags,
316 GIC_FDT_IRQ_TYPE_PPI, ARCH_TIMER_NS_EL1_IRQ, irqflags,
317 GIC_FDT_IRQ_TYPE_PPI, ARCH_TIMER_VIRT_IRQ, irqflags,
318 GIC_FDT_IRQ_TYPE_PPI, ARCH_TIMER_NS_EL2_IRQ, irqflags);
f5fdcd6e
PM
319}
320
321static void fdt_add_cpu_nodes(const VirtBoardInfo *vbi)
322{
323 int cpu;
8d45c54d
PF
324 int addr_cells = 1;
325
326 /*
327 * From Documentation/devicetree/bindings/arm/cpus.txt
328 * On ARM v8 64-bit systems value should be set to 2,
329 * that corresponds to the MPIDR_EL1 register size.
330 * If MPIDR_EL1[63:32] value is equal to 0 on all CPUs
331 * in the system, #address-cells can be set to 1, since
332 * MPIDR_EL1[63:32] bits are not used for CPUs
333 * identification.
334 *
335 * Here we actually don't know whether our system is 32- or 64-bit one.
336 * The simplest way to go is to examine affinity IDs of all our CPUs. If
337 * at least one of them has Aff3 populated, we set #address-cells to 2.
338 */
339 for (cpu = 0; cpu < vbi->smp_cpus; cpu++) {
340 ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(cpu));
341
342 if (armcpu->mp_affinity & ARM_AFF3_MASK) {
343 addr_cells = 2;
344 break;
345 }
346 }
f5fdcd6e 347
5a4348d1 348 qemu_fdt_add_subnode(vbi->fdt, "/cpus");
8d45c54d 349 qemu_fdt_setprop_cell(vbi->fdt, "/cpus", "#address-cells", addr_cells);
5a4348d1 350 qemu_fdt_setprop_cell(vbi->fdt, "/cpus", "#size-cells", 0x0);
f5fdcd6e
PM
351
352 for (cpu = vbi->smp_cpus - 1; cpu >= 0; cpu--) {
353 char *nodename = g_strdup_printf("/cpus/cpu@%d", cpu);
354 ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(cpu));
355
5a4348d1
PC
356 qemu_fdt_add_subnode(vbi->fdt, nodename);
357 qemu_fdt_setprop_string(vbi->fdt, nodename, "device_type", "cpu");
358 qemu_fdt_setprop_string(vbi->fdt, nodename, "compatible",
f5fdcd6e
PM
359 armcpu->dtb_compatible);
360
361 if (vbi->smp_cpus > 1) {
5a4348d1 362 qemu_fdt_setprop_string(vbi->fdt, nodename,
f5fdcd6e
PM
363 "enable-method", "psci");
364 }
365
8d45c54d
PF
366 if (addr_cells == 2) {
367 qemu_fdt_setprop_u64(vbi->fdt, nodename, "reg",
368 armcpu->mp_affinity);
369 } else {
370 qemu_fdt_setprop_cell(vbi->fdt, nodename, "reg",
371 armcpu->mp_affinity);
372 }
373
f5fdcd6e
PM
374 g_free(nodename);
375 }
376}
377
bd204e63 378static void fdt_add_v2m_gic_node(VirtBoardInfo *vbi)
f5fdcd6e 379{
bd204e63
CD
380 vbi->v2m_phandle = qemu_fdt_alloc_phandle(vbi->fdt);
381 qemu_fdt_add_subnode(vbi->fdt, "/intc/v2m");
382 qemu_fdt_setprop_string(vbi->fdt, "/intc/v2m", "compatible",
383 "arm,gic-v2m-frame");
384 qemu_fdt_setprop(vbi->fdt, "/intc/v2m", "msi-controller", NULL, 0);
385 qemu_fdt_setprop_sized_cells(vbi->fdt, "/intc/v2m", "reg",
386 2, vbi->memmap[VIRT_GIC_V2M].base,
387 2, vbi->memmap[VIRT_GIC_V2M].size);
388 qemu_fdt_setprop_cell(vbi->fdt, "/intc/v2m", "phandle", vbi->v2m_phandle);
389}
f5fdcd6e 390
b92ad394 391static void fdt_add_gic_node(VirtBoardInfo *vbi, int type)
bd204e63 392{
747d009d
CD
393 vbi->gic_phandle = qemu_fdt_alloc_phandle(vbi->fdt);
394 qemu_fdt_setprop_cell(vbi->fdt, "/", "interrupt-parent", vbi->gic_phandle);
f5fdcd6e 395
5a4348d1 396 qemu_fdt_add_subnode(vbi->fdt, "/intc");
5a4348d1
PC
397 qemu_fdt_setprop_cell(vbi->fdt, "/intc", "#interrupt-cells", 3);
398 qemu_fdt_setprop(vbi->fdt, "/intc", "interrupt-controller", NULL, 0);
dfd90a87
CD
399 qemu_fdt_setprop_cell(vbi->fdt, "/intc", "#address-cells", 0x2);
400 qemu_fdt_setprop_cell(vbi->fdt, "/intc", "#size-cells", 0x2);
401 qemu_fdt_setprop(vbi->fdt, "/intc", "ranges", NULL, 0);
b92ad394
PF
402 if (type == 3) {
403 qemu_fdt_setprop_string(vbi->fdt, "/intc", "compatible",
404 "arm,gic-v3");
405 qemu_fdt_setprop_sized_cells(vbi->fdt, "/intc", "reg",
406 2, vbi->memmap[VIRT_GIC_DIST].base,
407 2, vbi->memmap[VIRT_GIC_DIST].size,
408 2, vbi->memmap[VIRT_GIC_REDIST].base,
409 2, vbi->memmap[VIRT_GIC_REDIST].size);
410 } else {
411 /* 'cortex-a15-gic' means 'GIC v2' */
412 qemu_fdt_setprop_string(vbi->fdt, "/intc", "compatible",
413 "arm,cortex-a15-gic");
414 qemu_fdt_setprop_sized_cells(vbi->fdt, "/intc", "reg",
415 2, vbi->memmap[VIRT_GIC_DIST].base,
416 2, vbi->memmap[VIRT_GIC_DIST].size,
417 2, vbi->memmap[VIRT_GIC_CPU].base,
418 2, vbi->memmap[VIRT_GIC_CPU].size);
419 }
420
747d009d 421 qemu_fdt_setprop_cell(vbi->fdt, "/intc", "phandle", vbi->gic_phandle);
f5fdcd6e
PM
422}
423
bd204e63
CD
424static void create_v2m(VirtBoardInfo *vbi, qemu_irq *pic)
425{
426 int i;
427 int irq = vbi->irqmap[VIRT_GIC_V2M];
428 DeviceState *dev;
429
430 dev = qdev_create(NULL, "arm-gicv2m");
431 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, vbi->memmap[VIRT_GIC_V2M].base);
432 qdev_prop_set_uint32(dev, "base-spi", irq);
433 qdev_prop_set_uint32(dev, "num-spi", NUM_GICV2M_SPIS);
434 qdev_init_nofail(dev);
435
436 for (i = 0; i < NUM_GICV2M_SPIS; i++) {
437 sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, pic[irq + i]);
438 }
439
440 fdt_add_v2m_gic_node(vbi);
441}
442
b92ad394 443static void create_gic(VirtBoardInfo *vbi, qemu_irq *pic, int type, bool secure)
64204743 444{
b92ad394 445 /* We create a standalone GIC */
64204743
PM
446 DeviceState *gicdev;
447 SysBusDevice *gicbusdev;
e6fbcbc4 448 const char *gictype;
64204743
PM
449 int i;
450
b92ad394 451 gictype = (type == 3) ? gicv3_class_name() : gic_class_name();
64204743
PM
452
453 gicdev = qdev_create(NULL, gictype);
b92ad394 454 qdev_prop_set_uint32(gicdev, "revision", type);
64204743
PM
455 qdev_prop_set_uint32(gicdev, "num-cpu", smp_cpus);
456 /* Note that the num-irq property counts both internal and external
457 * interrupts; there are always 32 of the former (mandated by GIC spec).
458 */
459 qdev_prop_set_uint32(gicdev, "num-irq", NUM_IRQS + 32);
0e21f183
PM
460 if (!kvm_irqchip_in_kernel()) {
461 qdev_prop_set_bit(gicdev, "has-security-extensions", secure);
462 }
64204743
PM
463 qdev_init_nofail(gicdev);
464 gicbusdev = SYS_BUS_DEVICE(gicdev);
465 sysbus_mmio_map(gicbusdev, 0, vbi->memmap[VIRT_GIC_DIST].base);
b92ad394
PF
466 if (type == 3) {
467 sysbus_mmio_map(gicbusdev, 1, vbi->memmap[VIRT_GIC_REDIST].base);
468 } else {
469 sysbus_mmio_map(gicbusdev, 1, vbi->memmap[VIRT_GIC_CPU].base);
470 }
64204743
PM
471
472 /* Wire the outputs from each CPU's generic timer to the
473 * appropriate GIC PPI inputs, and the GIC's IRQ output to
474 * the CPU's IRQ input.
475 */
476 for (i = 0; i < smp_cpus; i++) {
477 DeviceState *cpudev = DEVICE(qemu_get_cpu(i));
0e3e858f 478 int ppibase = NUM_IRQS + i * GIC_INTERNAL + GIC_NR_SGIS;
a007b1f8
PM
479 int irq;
480 /* Mapping from the output timer irq lines from the CPU to the
481 * GIC PPI inputs we use for the virt board.
64204743 482 */
a007b1f8
PM
483 const int timer_irq[] = {
484 [GTIMER_PHYS] = ARCH_TIMER_NS_EL1_IRQ,
485 [GTIMER_VIRT] = ARCH_TIMER_VIRT_IRQ,
486 [GTIMER_HYP] = ARCH_TIMER_NS_EL2_IRQ,
487 [GTIMER_SEC] = ARCH_TIMER_S_EL1_IRQ,
488 };
489
490 for (irq = 0; irq < ARRAY_SIZE(timer_irq); irq++) {
491 qdev_connect_gpio_out(cpudev, irq,
492 qdev_get_gpio_in(gicdev,
493 ppibase + timer_irq[irq]));
494 }
64204743
PM
495
496 sysbus_connect_irq(gicbusdev, i, qdev_get_gpio_in(cpudev, ARM_CPU_IRQ));
8e7b4ca0
GB
497 sysbus_connect_irq(gicbusdev, i + smp_cpus,
498 qdev_get_gpio_in(cpudev, ARM_CPU_FIQ));
64204743
PM
499 }
500
501 for (i = 0; i < NUM_IRQS; i++) {
502 pic[i] = qdev_get_gpio_in(gicdev, i);
503 }
504
b92ad394 505 fdt_add_gic_node(vbi, type);
bd204e63 506
b92ad394
PF
507 if (type == 2) {
508 create_v2m(vbi, pic);
509 }
64204743
PM
510}
511
3df708eb
PM
512static void create_uart(const VirtBoardInfo *vbi, qemu_irq *pic, int uart,
513 MemoryRegion *mem)
f5fdcd6e
PM
514{
515 char *nodename;
3df708eb
PM
516 hwaddr base = vbi->memmap[uart].base;
517 hwaddr size = vbi->memmap[uart].size;
518 int irq = vbi->irqmap[uart];
f5fdcd6e
PM
519 const char compat[] = "arm,pl011\0arm,primecell";
520 const char clocknames[] = "uartclk\0apb_pclk";
3df708eb
PM
521 DeviceState *dev = qdev_create(NULL, "pl011");
522 SysBusDevice *s = SYS_BUS_DEVICE(dev);
f5fdcd6e 523
3df708eb
PM
524 qdev_init_nofail(dev);
525 memory_region_add_subregion(mem, base,
526 sysbus_mmio_get_region(s, 0));
527 sysbus_connect_irq(s, 0, pic[irq]);
f5fdcd6e
PM
528
529 nodename = g_strdup_printf("/pl011@%" PRIx64, base);
5a4348d1 530 qemu_fdt_add_subnode(vbi->fdt, nodename);
f5fdcd6e 531 /* Note that we can't use setprop_string because of the embedded NUL */
5a4348d1 532 qemu_fdt_setprop(vbi->fdt, nodename, "compatible",
f5fdcd6e 533 compat, sizeof(compat));
5a4348d1 534 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg",
f5fdcd6e 535 2, base, 2, size);
5a4348d1 536 qemu_fdt_setprop_cells(vbi->fdt, nodename, "interrupts",
f5fdcd6e 537 GIC_FDT_IRQ_TYPE_SPI, irq,
0be969a2 538 GIC_FDT_IRQ_FLAGS_LEVEL_HI);
5a4348d1 539 qemu_fdt_setprop_cells(vbi->fdt, nodename, "clocks",
f5fdcd6e 540 vbi->clock_phandle, vbi->clock_phandle);
5a4348d1 541 qemu_fdt_setprop(vbi->fdt, nodename, "clock-names",
f5fdcd6e 542 clocknames, sizeof(clocknames));
f022b8e9 543
3df708eb
PM
544 if (uart == VIRT_UART) {
545 qemu_fdt_setprop_string(vbi->fdt, "/chosen", "stdout-path", nodename);
546 } else {
547 /* Mark as not usable by the normal world */
548 qemu_fdt_setprop_string(vbi->fdt, nodename, "status", "disabled");
549 qemu_fdt_setprop_string(vbi->fdt, nodename, "secure-status", "okay");
550 }
551
f5fdcd6e
PM
552 g_free(nodename);
553}
554
6e411af9
PM
555static void create_rtc(const VirtBoardInfo *vbi, qemu_irq *pic)
556{
557 char *nodename;
558 hwaddr base = vbi->memmap[VIRT_RTC].base;
559 hwaddr size = vbi->memmap[VIRT_RTC].size;
560 int irq = vbi->irqmap[VIRT_RTC];
561 const char compat[] = "arm,pl031\0arm,primecell";
562
563 sysbus_create_simple("pl031", base, pic[irq]);
564
565 nodename = g_strdup_printf("/pl031@%" PRIx64, base);
566 qemu_fdt_add_subnode(vbi->fdt, nodename);
567 qemu_fdt_setprop(vbi->fdt, nodename, "compatible", compat, sizeof(compat));
568 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg",
569 2, base, 2, size);
570 qemu_fdt_setprop_cells(vbi->fdt, nodename, "interrupts",
571 GIC_FDT_IRQ_TYPE_SPI, irq,
0be969a2 572 GIC_FDT_IRQ_FLAGS_LEVEL_HI);
6e411af9
PM
573 qemu_fdt_setprop_cell(vbi->fdt, nodename, "clocks", vbi->clock_phandle);
574 qemu_fdt_setprop_string(vbi->fdt, nodename, "clock-names", "apb_pclk");
575 g_free(nodename);
576}
577
4bedd849
SZ
578static DeviceState *pl061_dev;
579static void virt_powerdown_req(Notifier *n, void *opaque)
580{
581 /* use gpio Pin 3 for power button event */
582 qemu_set_irq(qdev_get_gpio_in(pl061_dev, 3), 1);
583}
584
585static Notifier virt_system_powerdown_notifier = {
586 .notify = virt_powerdown_req
587};
588
b0a3721e
SZ
589static void create_gpio(const VirtBoardInfo *vbi, qemu_irq *pic)
590{
591 char *nodename;
592 hwaddr base = vbi->memmap[VIRT_GPIO].base;
593 hwaddr size = vbi->memmap[VIRT_GPIO].size;
594 int irq = vbi->irqmap[VIRT_GPIO];
595 const char compat[] = "arm,pl061\0arm,primecell";
596
4bedd849 597 pl061_dev = sysbus_create_simple("pl061", base, pic[irq]);
b0a3721e 598
3e6ebb64 599 uint32_t phandle = qemu_fdt_alloc_phandle(vbi->fdt);
b0a3721e
SZ
600 nodename = g_strdup_printf("/pl061@%" PRIx64, base);
601 qemu_fdt_add_subnode(vbi->fdt, nodename);
602 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg",
603 2, base, 2, size);
604 qemu_fdt_setprop(vbi->fdt, nodename, "compatible", compat, sizeof(compat));
605 qemu_fdt_setprop_cell(vbi->fdt, nodename, "#gpio-cells", 2);
606 qemu_fdt_setprop(vbi->fdt, nodename, "gpio-controller", NULL, 0);
607 qemu_fdt_setprop_cells(vbi->fdt, nodename, "interrupts",
608 GIC_FDT_IRQ_TYPE_SPI, irq,
609 GIC_FDT_IRQ_FLAGS_LEVEL_HI);
610 qemu_fdt_setprop_cell(vbi->fdt, nodename, "clocks", vbi->clock_phandle);
611 qemu_fdt_setprop_string(vbi->fdt, nodename, "clock-names", "apb_pclk");
3e6ebb64
SZ
612 qemu_fdt_setprop_cell(vbi->fdt, nodename, "phandle", phandle);
613
614 qemu_fdt_add_subnode(vbi->fdt, "/gpio-keys");
615 qemu_fdt_setprop_string(vbi->fdt, "/gpio-keys", "compatible", "gpio-keys");
616 qemu_fdt_setprop_cell(vbi->fdt, "/gpio-keys", "#size-cells", 0);
617 qemu_fdt_setprop_cell(vbi->fdt, "/gpio-keys", "#address-cells", 1);
618
619 qemu_fdt_add_subnode(vbi->fdt, "/gpio-keys/poweroff");
620 qemu_fdt_setprop_string(vbi->fdt, "/gpio-keys/poweroff",
621 "label", "GPIO Key Poweroff");
622 qemu_fdt_setprop_cell(vbi->fdt, "/gpio-keys/poweroff", "linux,code",
623 KEY_POWER);
624 qemu_fdt_setprop_cells(vbi->fdt, "/gpio-keys/poweroff",
625 "gpios", phandle, 3, 0);
b0a3721e 626
4bedd849
SZ
627 /* connect powerdown request */
628 qemu_register_powerdown_notifier(&virt_system_powerdown_notifier);
629
b0a3721e
SZ
630 g_free(nodename);
631}
632
f5fdcd6e
PM
633static void create_virtio_devices(const VirtBoardInfo *vbi, qemu_irq *pic)
634{
635 int i;
636 hwaddr size = vbi->memmap[VIRT_MMIO].size;
637
587078f0
LE
638 /* We create the transports in forwards order. Since qbus_realize()
639 * prepends (not appends) new child buses, the incrementing loop below will
640 * create a list of virtio-mmio buses with decreasing base addresses.
641 *
642 * When a -device option is processed from the command line,
643 * qbus_find_recursive() picks the next free virtio-mmio bus in forwards
644 * order. The upshot is that -device options in increasing command line
645 * order are mapped to virtio-mmio buses with decreasing base addresses.
646 *
647 * When this code was originally written, that arrangement ensured that the
648 * guest Linux kernel would give the lowest "name" (/dev/vda, eth0, etc) to
649 * the first -device on the command line. (The end-to-end order is a
650 * function of this loop, qbus_realize(), qbus_find_recursive(), and the
651 * guest kernel's name-to-address assignment strategy.)
652 *
653 * Meanwhile, the kernel's traversal seems to have been reversed; see eg.
654 * the message, if not necessarily the code, of commit 70161ff336.
655 * Therefore the loop now establishes the inverse of the original intent.
656 *
657 * Unfortunately, we can't counteract the kernel change by reversing the
658 * loop; it would break existing command lines.
659 *
660 * In any case, the kernel makes no guarantee about the stability of
661 * enumeration order of virtio devices (as demonstrated by it changing
662 * between kernel versions). For reliable and stable identification
663 * of disks users must use UUIDs or similar mechanisms.
f5fdcd6e
PM
664 */
665 for (i = 0; i < NUM_VIRTIO_TRANSPORTS; i++) {
666 int irq = vbi->irqmap[VIRT_MMIO] + i;
667 hwaddr base = vbi->memmap[VIRT_MMIO].base + i * size;
668
669 sysbus_create_simple("virtio-mmio", base, pic[irq]);
670 }
671
587078f0
LE
672 /* We add dtb nodes in reverse order so that they appear in the finished
673 * device tree lowest address first.
674 *
675 * Note that this mapping is independent of the loop above. The previous
676 * loop influences virtio device to virtio transport assignment, whereas
677 * this loop controls how virtio transports are laid out in the dtb.
678 */
f5fdcd6e
PM
679 for (i = NUM_VIRTIO_TRANSPORTS - 1; i >= 0; i--) {
680 char *nodename;
681 int irq = vbi->irqmap[VIRT_MMIO] + i;
682 hwaddr base = vbi->memmap[VIRT_MMIO].base + i * size;
683
684 nodename = g_strdup_printf("/virtio_mmio@%" PRIx64, base);
5a4348d1
PC
685 qemu_fdt_add_subnode(vbi->fdt, nodename);
686 qemu_fdt_setprop_string(vbi->fdt, nodename,
687 "compatible", "virtio,mmio");
688 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg",
689 2, base, 2, size);
690 qemu_fdt_setprop_cells(vbi->fdt, nodename, "interrupts",
691 GIC_FDT_IRQ_TYPE_SPI, irq,
692 GIC_FDT_IRQ_FLAGS_EDGE_LO_HI);
f5fdcd6e
PM
693 g_free(nodename);
694 }
695}
696
acf82361
PM
697static void create_one_flash(const char *name, hwaddr flashbase,
698 hwaddr flashsize)
699{
700 /* Create and map a single flash device. We use the same
701 * parameters as the flash devices on the Versatile Express board.
702 */
703 DriveInfo *dinfo = drive_get_next(IF_PFLASH);
704 DeviceState *dev = qdev_create(NULL, "cfi.pflash01");
705 const uint64_t sectorlength = 256 * 1024;
706
9b3d111a
MA
707 if (dinfo) {
708 qdev_prop_set_drive(dev, "drive", blk_by_legacy_dinfo(dinfo),
709 &error_abort);
acf82361
PM
710 }
711
712 qdev_prop_set_uint32(dev, "num-blocks", flashsize / sectorlength);
713 qdev_prop_set_uint64(dev, "sector-length", sectorlength);
714 qdev_prop_set_uint8(dev, "width", 4);
715 qdev_prop_set_uint8(dev, "device-width", 2);
e9809422 716 qdev_prop_set_bit(dev, "big-endian", false);
acf82361
PM
717 qdev_prop_set_uint16(dev, "id0", 0x89);
718 qdev_prop_set_uint16(dev, "id1", 0x18);
719 qdev_prop_set_uint16(dev, "id2", 0x00);
720 qdev_prop_set_uint16(dev, "id3", 0x00);
721 qdev_prop_set_string(dev, "name", name);
722 qdev_init_nofail(dev);
723
724 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, flashbase);
725}
726
727static void create_flash(const VirtBoardInfo *vbi)
728{
729 /* Create two flash devices to fill the VIRT_FLASH space in the memmap.
730 * Any file passed via -bios goes in the first of these.
731 */
732 hwaddr flashsize = vbi->memmap[VIRT_FLASH].size / 2;
733 hwaddr flashbase = vbi->memmap[VIRT_FLASH].base;
734 char *nodename;
735
736 if (bios_name) {
6e05a12f 737 char *fn;
4de9a883 738 int image_size;
acf82361
PM
739
740 if (drive_get(IF_PFLASH, 0, 0)) {
741 error_report("The contents of the first flash device may be "
742 "specified with -bios or with -drive if=pflash... "
743 "but you cannot use both options at once");
744 exit(1);
745 }
746 fn = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
4de9a883
SW
747 if (!fn) {
748 error_report("Could not find ROM image '%s'", bios_name);
749 exit(1);
750 }
751 image_size = load_image_targphys(fn, flashbase, flashsize);
752 g_free(fn);
753 if (image_size < 0) {
acf82361
PM
754 error_report("Could not load ROM image '%s'", bios_name);
755 exit(1);
756 }
757 }
758
759 create_one_flash("virt.flash0", flashbase, flashsize);
760 create_one_flash("virt.flash1", flashbase + flashsize, flashsize);
761
762 nodename = g_strdup_printf("/flash@%" PRIx64, flashbase);
763 qemu_fdt_add_subnode(vbi->fdt, nodename);
764 qemu_fdt_setprop_string(vbi->fdt, nodename, "compatible", "cfi-flash");
765 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg",
766 2, flashbase, 2, flashsize,
767 2, flashbase + flashsize, 2, flashsize);
768 qemu_fdt_setprop_cell(vbi->fdt, nodename, "bank-width", 4);
769 g_free(nodename);
770}
771
0b341a85 772static void create_fw_cfg(const VirtBoardInfo *vbi, AddressSpace *as)
578f3c7b
LE
773{
774 hwaddr base = vbi->memmap[VIRT_FW_CFG].base;
775 hwaddr size = vbi->memmap[VIRT_FW_CFG].size;
776 char *nodename;
777
0b341a85 778 fw_cfg_init_mem_wide(base + 8, base, 8, base + 16, as);
578f3c7b
LE
779
780 nodename = g_strdup_printf("/fw-cfg@%" PRIx64, base);
781 qemu_fdt_add_subnode(vbi->fdt, nodename);
782 qemu_fdt_setprop_string(vbi->fdt, nodename,
783 "compatible", "qemu,fw-cfg-mmio");
784 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg",
785 2, base, 2, size);
786 g_free(nodename);
787}
788
4ab29b82
AG
789static void create_pcie_irq_map(const VirtBoardInfo *vbi, uint32_t gic_phandle,
790 int first_irq, const char *nodename)
791{
792 int devfn, pin;
dfd90a87 793 uint32_t full_irq_map[4 * 4 * 10] = { 0 };
4ab29b82
AG
794 uint32_t *irq_map = full_irq_map;
795
796 for (devfn = 0; devfn <= 0x18; devfn += 0x8) {
797 for (pin = 0; pin < 4; pin++) {
798 int irq_type = GIC_FDT_IRQ_TYPE_SPI;
799 int irq_nr = first_irq + ((pin + PCI_SLOT(devfn)) % PCI_NUM_PINS);
800 int irq_level = GIC_FDT_IRQ_FLAGS_LEVEL_HI;
801 int i;
802
803 uint32_t map[] = {
804 devfn << 8, 0, 0, /* devfn */
805 pin + 1, /* PCI pin */
dfd90a87 806 gic_phandle, 0, 0, irq_type, irq_nr, irq_level }; /* GIC irq */
4ab29b82
AG
807
808 /* Convert map to big endian */
dfd90a87 809 for (i = 0; i < 10; i++) {
4ab29b82
AG
810 irq_map[i] = cpu_to_be32(map[i]);
811 }
dfd90a87 812 irq_map += 10;
4ab29b82
AG
813 }
814 }
815
816 qemu_fdt_setprop(vbi->fdt, nodename, "interrupt-map",
817 full_irq_map, sizeof(full_irq_map));
818
819 qemu_fdt_setprop_cells(vbi->fdt, nodename, "interrupt-map-mask",
820 0x1800, 0, 0, /* devfn (PCI_SLOT(3)) */
821 0x7 /* PCI irq */);
822}
823
5125f9cd
PF
824static void create_pcie(const VirtBoardInfo *vbi, qemu_irq *pic,
825 bool use_highmem)
4ab29b82 826{
6a1f001b
SZ
827 hwaddr base_mmio = vbi->memmap[VIRT_PCIE_MMIO].base;
828 hwaddr size_mmio = vbi->memmap[VIRT_PCIE_MMIO].size;
5125f9cd
PF
829 hwaddr base_mmio_high = vbi->memmap[VIRT_PCIE_MMIO_HIGH].base;
830 hwaddr size_mmio_high = vbi->memmap[VIRT_PCIE_MMIO_HIGH].size;
6a1f001b
SZ
831 hwaddr base_pio = vbi->memmap[VIRT_PCIE_PIO].base;
832 hwaddr size_pio = vbi->memmap[VIRT_PCIE_PIO].size;
833 hwaddr base_ecam = vbi->memmap[VIRT_PCIE_ECAM].base;
834 hwaddr size_ecam = vbi->memmap[VIRT_PCIE_ECAM].size;
835 hwaddr base = base_mmio;
836 int nr_pcie_buses = size_ecam / PCIE_MMCFG_SIZE_MIN;
4ab29b82
AG
837 int irq = vbi->irqmap[VIRT_PCIE];
838 MemoryRegion *mmio_alias;
839 MemoryRegion *mmio_reg;
840 MemoryRegion *ecam_alias;
841 MemoryRegion *ecam_reg;
842 DeviceState *dev;
843 char *nodename;
844 int i;
fea9b3ca 845 PCIHostState *pci;
4ab29b82 846
4ab29b82
AG
847 dev = qdev_create(NULL, TYPE_GPEX_HOST);
848 qdev_init_nofail(dev);
849
850 /* Map only the first size_ecam bytes of ECAM space */
851 ecam_alias = g_new0(MemoryRegion, 1);
852 ecam_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0);
853 memory_region_init_alias(ecam_alias, OBJECT(dev), "pcie-ecam",
854 ecam_reg, 0, size_ecam);
855 memory_region_add_subregion(get_system_memory(), base_ecam, ecam_alias);
856
857 /* Map the MMIO window into system address space so as to expose
858 * the section of PCI MMIO space which starts at the same base address
859 * (ie 1:1 mapping for that part of PCI MMIO space visible through
860 * the window).
861 */
862 mmio_alias = g_new0(MemoryRegion, 1);
863 mmio_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 1);
864 memory_region_init_alias(mmio_alias, OBJECT(dev), "pcie-mmio",
865 mmio_reg, base_mmio, size_mmio);
866 memory_region_add_subregion(get_system_memory(), base_mmio, mmio_alias);
867
5125f9cd
PF
868 if (use_highmem) {
869 /* Map high MMIO space */
870 MemoryRegion *high_mmio_alias = g_new0(MemoryRegion, 1);
871
872 memory_region_init_alias(high_mmio_alias, OBJECT(dev), "pcie-mmio-high",
873 mmio_reg, base_mmio_high, size_mmio_high);
874 memory_region_add_subregion(get_system_memory(), base_mmio_high,
875 high_mmio_alias);
876 }
877
4ab29b82 878 /* Map IO port space */
6a1f001b 879 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 2, base_pio);
4ab29b82
AG
880
881 for (i = 0; i < GPEX_NUM_IRQS; i++) {
882 sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, pic[irq + i]);
883 }
884
fea9b3ca
AK
885 pci = PCI_HOST_BRIDGE(dev);
886 if (pci->bus) {
887 for (i = 0; i < nb_nics; i++) {
888 NICInfo *nd = &nd_table[i];
889
890 if (!nd->model) {
891 nd->model = g_strdup("virtio");
892 }
893
894 pci_nic_init_nofail(nd, pci->bus, nd->model, NULL);
895 }
896 }
897
4ab29b82
AG
898 nodename = g_strdup_printf("/pcie@%" PRIx64, base);
899 qemu_fdt_add_subnode(vbi->fdt, nodename);
900 qemu_fdt_setprop_string(vbi->fdt, nodename,
901 "compatible", "pci-host-ecam-generic");
902 qemu_fdt_setprop_string(vbi->fdt, nodename, "device_type", "pci");
903 qemu_fdt_setprop_cell(vbi->fdt, nodename, "#address-cells", 3);
904 qemu_fdt_setprop_cell(vbi->fdt, nodename, "#size-cells", 2);
905 qemu_fdt_setprop_cells(vbi->fdt, nodename, "bus-range", 0,
906 nr_pcie_buses - 1);
907
b92ad394
PF
908 if (vbi->v2m_phandle) {
909 qemu_fdt_setprop_cells(vbi->fdt, nodename, "msi-parent",
910 vbi->v2m_phandle);
911 }
bd204e63 912
4ab29b82
AG
913 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg",
914 2, base_ecam, 2, size_ecam);
5125f9cd
PF
915
916 if (use_highmem) {
917 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "ranges",
918 1, FDT_PCI_RANGE_IOPORT, 2, 0,
919 2, base_pio, 2, size_pio,
920 1, FDT_PCI_RANGE_MMIO, 2, base_mmio,
921 2, base_mmio, 2, size_mmio,
922 1, FDT_PCI_RANGE_MMIO_64BIT,
923 2, base_mmio_high,
924 2, base_mmio_high, 2, size_mmio_high);
925 } else {
926 qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "ranges",
927 1, FDT_PCI_RANGE_IOPORT, 2, 0,
928 2, base_pio, 2, size_pio,
929 1, FDT_PCI_RANGE_MMIO, 2, base_mmio,
930 2, base_mmio, 2, size_mmio);
931 }
4ab29b82
AG
932
933 qemu_fdt_setprop_cell(vbi->fdt, nodename, "#interrupt-cells", 1);
747d009d 934 create_pcie_irq_map(vbi, vbi->gic_phandle, irq, nodename);
4ab29b82
AG
935
936 g_free(nodename);
937}
938
5f7a5a0e
EA
939static void create_platform_bus(VirtBoardInfo *vbi, qemu_irq *pic)
940{
941 DeviceState *dev;
942 SysBusDevice *s;
943 int i;
944 ARMPlatformBusFDTParams *fdt_params = g_new(ARMPlatformBusFDTParams, 1);
945 MemoryRegion *sysmem = get_system_memory();
946
947 platform_bus_params.platform_bus_base = vbi->memmap[VIRT_PLATFORM_BUS].base;
948 platform_bus_params.platform_bus_size = vbi->memmap[VIRT_PLATFORM_BUS].size;
949 platform_bus_params.platform_bus_first_irq = vbi->irqmap[VIRT_PLATFORM_BUS];
950 platform_bus_params.platform_bus_num_irqs = PLATFORM_BUS_NUM_IRQS;
951
952 fdt_params->system_params = &platform_bus_params;
953 fdt_params->binfo = &vbi->bootinfo;
954 fdt_params->intc = "/intc";
955 /*
956 * register a machine init done notifier that creates the device tree
957 * nodes of the platform bus and its children dynamic sysbus devices
958 */
959 arm_register_platform_bus_fdt_creator(fdt_params);
960
961 dev = qdev_create(NULL, TYPE_PLATFORM_BUS_DEVICE);
962 dev->id = TYPE_PLATFORM_BUS_DEVICE;
963 qdev_prop_set_uint32(dev, "num_irqs",
964 platform_bus_params.platform_bus_num_irqs);
965 qdev_prop_set_uint32(dev, "mmio_size",
966 platform_bus_params.platform_bus_size);
967 qdev_init_nofail(dev);
968 s = SYS_BUS_DEVICE(dev);
969
970 for (i = 0; i < platform_bus_params.platform_bus_num_irqs; i++) {
971 int irqn = platform_bus_params.platform_bus_first_irq + i;
972 sysbus_connect_irq(s, i, pic[irqn]);
973 }
974
975 memory_region_add_subregion(sysmem,
976 platform_bus_params.platform_bus_base,
977 sysbus_mmio_get_region(s, 0));
978}
979
f5fdcd6e
PM
980static void *machvirt_dtb(const struct arm_boot_info *binfo, int *fdt_size)
981{
982 const VirtBoardInfo *board = (const VirtBoardInfo *)binfo;
983
984 *fdt_size = board->fdt_size;
985 return board->fdt;
986}
987
c30e1565
WH
988static void virt_build_smbios(VirtGuestInfo *guest_info)
989{
990 FWCfgState *fw_cfg = guest_info->fw_cfg;
991 uint8_t *smbios_tables, *smbios_anchor;
992 size_t smbios_tables_len, smbios_anchor_len;
bab27ea2 993 const char *product = "QEMU Virtual Machine";
c30e1565
WH
994
995 if (!fw_cfg) {
996 return;
997 }
998
bab27ea2
AJ
999 if (kvm_enabled()) {
1000 product = "KVM Virtual Machine";
1001 }
1002
1003 smbios_set_defaults("QEMU", product,
c30e1565
WH
1004 "1.0", false, true, SMBIOS_ENTRY_POINT_30);
1005
1006 smbios_get_tables(NULL, 0, &smbios_tables, &smbios_tables_len,
1007 &smbios_anchor, &smbios_anchor_len);
1008
1009 if (smbios_anchor) {
1010 fw_cfg_add_file(fw_cfg, "etc/smbios/smbios-tables",
1011 smbios_tables, smbios_tables_len);
1012 fw_cfg_add_file(fw_cfg, "etc/smbios/smbios-anchor",
1013 smbios_anchor, smbios_anchor_len);
1014 }
1015}
1016
d7c2e2db
SZ
1017static
1018void virt_guest_info_machine_done(Notifier *notifier, void *data)
1019{
1020 VirtGuestInfoState *guest_info_state = container_of(notifier,
1021 VirtGuestInfoState, machine_done);
1022 virt_acpi_setup(&guest_info_state->info);
c30e1565 1023 virt_build_smbios(&guest_info_state->info);
d7c2e2db
SZ
1024}
1025
3ef96221 1026static void machvirt_init(MachineState *machine)
f5fdcd6e 1027{
e5a5604f 1028 VirtMachineState *vms = VIRT_MACHINE(machine);
f5fdcd6e
PM
1029 qemu_irq pic[NUM_IRQS];
1030 MemoryRegion *sysmem = get_system_memory();
3df708eb 1031 MemoryRegion *secure_sysmem = NULL;
b92ad394 1032 int gic_version = vms->gic_version;
7ea686f5 1033 int n, virt_max_cpus;
f5fdcd6e 1034 MemoryRegion *ram = g_new(MemoryRegion, 1);
3ef96221 1035 const char *cpu_model = machine->cpu_model;
f5fdcd6e 1036 VirtBoardInfo *vbi;
d7c2e2db
SZ
1037 VirtGuestInfoState *guest_info_state = g_malloc0(sizeof *guest_info_state);
1038 VirtGuestInfo *guest_info = &guest_info_state->info;
f313369f 1039 char **cpustr;
f5fdcd6e
PM
1040
1041 if (!cpu_model) {
1042 cpu_model = "cortex-a15";
1043 }
1044
b92ad394
PF
1045 /* We can probe only here because during property set
1046 * KVM is not available yet
1047 */
1048 if (!gic_version) {
1049 gic_version = kvm_arm_vgic_probe();
1050 if (!gic_version) {
faa811f6
AJ
1051 error_report("Unable to determine GIC version supported by host");
1052 error_printf("KVM acceleration is probably not supported\n");
b92ad394
PF
1053 exit(1);
1054 }
1055 }
1056
f313369f
GB
1057 /* Separate the actual CPU model name from any appended features */
1058 cpustr = g_strsplit(cpu_model, ",", 2);
1059
1060 vbi = find_machine_info(cpustr[0]);
f5fdcd6e
PM
1061
1062 if (!vbi) {
f313369f 1063 error_report("mach-virt: CPU %s not supported", cpustr[0]);
f5fdcd6e
PM
1064 exit(1);
1065 }
1066
4b280b72
AJ
1067 /* The maximum number of CPUs depends on the GIC version, or on how
1068 * many redistributors we can fit into the memory map.
1069 */
1070 if (gic_version == 3) {
7ea686f5 1071 virt_max_cpus = vbi->memmap[VIRT_GIC_REDIST].size / 0x20000;
4b280b72 1072 } else {
7ea686f5 1073 virt_max_cpus = GIC_NCPU;
4b280b72
AJ
1074 }
1075
7ea686f5 1076 if (max_cpus > virt_max_cpus) {
4b280b72
AJ
1077 error_report("Number of SMP CPUs requested (%d) exceeds max CPUs "
1078 "supported by machine 'mach-virt' (%d)",
7ea686f5 1079 max_cpus, virt_max_cpus);
4b280b72
AJ
1080 exit(1);
1081 }
1082
f5fdcd6e
PM
1083 vbi->smp_cpus = smp_cpus;
1084
3ef96221 1085 if (machine->ram_size > vbi->memmap[VIRT_MEM].size) {
71c27684 1086 error_report("mach-virt: cannot model more than %dGB RAM", RAMLIMIT_GB);
f5fdcd6e
PM
1087 exit(1);
1088 }
1089
3df708eb
PM
1090 if (vms->secure) {
1091 if (kvm_enabled()) {
1092 error_report("mach-virt: KVM does not support Security extensions");
1093 exit(1);
1094 }
1095
1096 /* The Secure view of the world is the same as the NonSecure,
1097 * but with a few extra devices. Create it as a container region
1098 * containing the system memory at low priority; any secure-only
1099 * devices go in at higher priority and take precedence.
1100 */
1101 secure_sysmem = g_new(MemoryRegion, 1);
1102 memory_region_init(secure_sysmem, OBJECT(machine), "secure-memory",
1103 UINT64_MAX);
1104 memory_region_add_subregion_overlap(secure_sysmem, 0, sysmem, -1);
1105 }
1106
f5fdcd6e 1107 create_fdt(vbi);
f5fdcd6e
PM
1108
1109 for (n = 0; n < smp_cpus; n++) {
f313369f
GB
1110 ObjectClass *oc = cpu_class_by_name(TYPE_ARM_CPU, cpustr[0]);
1111 CPUClass *cc = CPU_CLASS(oc);
f5fdcd6e 1112 Object *cpuobj;
f313369f 1113 Error *err = NULL;
886bc7a0 1114 char *cpuopts = g_strdup(cpustr[1]);
f5fdcd6e
PM
1115
1116 if (!oc) {
faa811f6 1117 error_report("Unable to find CPU definition");
f5fdcd6e
PM
1118 exit(1);
1119 }
1120 cpuobj = object_new(object_class_get_name(oc));
1121
f313369f 1122 /* Handle any CPU options specified by the user */
886bc7a0
AB
1123 cc->parse_features(CPU(cpuobj), cpuopts, &err);
1124 g_free(cpuopts);
f313369f 1125 if (err) {
19867549 1126 error_report_err(err);
f313369f
GB
1127 exit(1);
1128 }
1129
e5a5604f
GB
1130 if (!vms->secure) {
1131 object_property_set_bool(cpuobj, false, "has_el3", NULL);
1132 }
1133
211b0169
RH
1134 object_property_set_int(cpuobj, QEMU_PSCI_CONDUIT_HVC, "psci-conduit",
1135 NULL);
1136
f5fdcd6e
PM
1137 /* Secondary CPUs start in PSCI powered-down state */
1138 if (n > 0) {
1139 object_property_set_bool(cpuobj, true, "start-powered-off", NULL);
1140 }
ba750085
PM
1141
1142 if (object_property_find(cpuobj, "reset-cbar", NULL)) {
1143 object_property_set_int(cpuobj, vbi->memmap[VIRT_CPUPERIPHS].base,
1144 "reset-cbar", &error_abort);
1145 }
1146
1d939a68
PM
1147 object_property_set_link(cpuobj, OBJECT(sysmem), "memory",
1148 &error_abort);
3df708eb
PM
1149 if (vms->secure) {
1150 object_property_set_link(cpuobj, OBJECT(secure_sysmem),
1151 "secure-memory", &error_abort);
1152 }
1d939a68 1153
f5fdcd6e
PM
1154 object_property_set_bool(cpuobj, true, "realized", NULL);
1155 }
f313369f 1156 g_strfreev(cpustr);
b92ad394 1157 fdt_add_timer_nodes(vbi, gic_version);
f5fdcd6e 1158 fdt_add_cpu_nodes(vbi);
06955739 1159 fdt_add_psci_node(vbi);
f5fdcd6e 1160
c8623c02
DM
1161 memory_region_allocate_system_memory(ram, NULL, "mach-virt.ram",
1162 machine->ram_size);
f5fdcd6e
PM
1163 memory_region_add_subregion(sysmem, vbi->memmap[VIRT_MEM].base, ram);
1164
acf82361
PM
1165 create_flash(vbi);
1166
b92ad394 1167 create_gic(vbi, pic, gic_version, vms->secure);
f5fdcd6e 1168
3df708eb
PM
1169 create_uart(vbi, pic, VIRT_UART, sysmem);
1170
1171 if (vms->secure) {
1172 create_uart(vbi, pic, VIRT_SECURE_UART, secure_sysmem);
1173 }
f5fdcd6e 1174
6e411af9
PM
1175 create_rtc(vbi, pic);
1176
5125f9cd 1177 create_pcie(vbi, pic, vms->highmem);
4ab29b82 1178
b0a3721e
SZ
1179 create_gpio(vbi, pic);
1180
f5fdcd6e
PM
1181 /* Create mmio transports, so the user can create virtio backends
1182 * (which will be automatically plugged in to the transports). If
1183 * no backend is created the transport will just sit harmlessly idle.
1184 */
1185 create_virtio_devices(vbi, pic);
1186
0b341a85 1187 create_fw_cfg(vbi, &address_space_memory);
d7c2e2db
SZ
1188 rom_set_fw(fw_cfg_find());
1189
1190 guest_info->smp_cpus = smp_cpus;
1191 guest_info->fw_cfg = fw_cfg_find();
1192 guest_info->memmap = vbi->memmap;
1193 guest_info->irqmap = vbi->irqmap;
5125f9cd 1194 guest_info->use_highmem = vms->highmem;
b92ad394 1195 guest_info->gic_version = gic_version;
d7c2e2db
SZ
1196 guest_info_state->machine_done.notify = virt_guest_info_machine_done;
1197 qemu_add_machine_init_done_notifier(&guest_info_state->machine_done);
578f3c7b 1198
3ef96221
MA
1199 vbi->bootinfo.ram_size = machine->ram_size;
1200 vbi->bootinfo.kernel_filename = machine->kernel_filename;
1201 vbi->bootinfo.kernel_cmdline = machine->kernel_cmdline;
1202 vbi->bootinfo.initrd_filename = machine->initrd_filename;
f5fdcd6e
PM
1203 vbi->bootinfo.nb_cpus = smp_cpus;
1204 vbi->bootinfo.board_id = -1;
1205 vbi->bootinfo.loader_start = vbi->memmap[VIRT_MEM].base;
1206 vbi->bootinfo.get_dtb = machvirt_dtb;
aa351061 1207 vbi->bootinfo.firmware_loaded = bios_name || drive_get(IF_PFLASH, 0, 0);
f5fdcd6e 1208 arm_load_kernel(ARM_CPU(first_cpu), &vbi->bootinfo);
5f7a5a0e
EA
1209
1210 /*
1211 * arm_load_kernel machine init done notifier registration must
1212 * happen before the platform_bus_create call. In this latter,
1213 * another notifier is registered which adds platform bus nodes.
1214 * Notifiers are executed in registration reverse order.
1215 */
1216 create_platform_bus(vbi, pic);
f5fdcd6e
PM
1217}
1218
083a5890
GB
1219static bool virt_get_secure(Object *obj, Error **errp)
1220{
1221 VirtMachineState *vms = VIRT_MACHINE(obj);
1222
1223 return vms->secure;
1224}
1225
1226static void virt_set_secure(Object *obj, bool value, Error **errp)
1227{
1228 VirtMachineState *vms = VIRT_MACHINE(obj);
1229
1230 vms->secure = value;
1231}
1232
5125f9cd
PF
1233static bool virt_get_highmem(Object *obj, Error **errp)
1234{
1235 VirtMachineState *vms = VIRT_MACHINE(obj);
1236
1237 return vms->highmem;
1238}
1239
1240static void virt_set_highmem(Object *obj, bool value, Error **errp)
1241{
1242 VirtMachineState *vms = VIRT_MACHINE(obj);
1243
1244 vms->highmem = value;
1245}
1246
b92ad394
PF
1247static char *virt_get_gic_version(Object *obj, Error **errp)
1248{
1249 VirtMachineState *vms = VIRT_MACHINE(obj);
1250 const char *val = vms->gic_version == 3 ? "3" : "2";
1251
1252 return g_strdup(val);
1253}
1254
1255static void virt_set_gic_version(Object *obj, const char *value, Error **errp)
1256{
1257 VirtMachineState *vms = VIRT_MACHINE(obj);
1258
1259 if (!strcmp(value, "3")) {
1260 vms->gic_version = 3;
1261 } else if (!strcmp(value, "2")) {
1262 vms->gic_version = 2;
1263 } else if (!strcmp(value, "host")) {
1264 vms->gic_version = 0; /* Will probe later */
1265 } else {
7b55044f
MA
1266 error_setg(errp, "Invalid gic-version value");
1267 error_append_hint(errp, "Valid values are 3, 2, host.\n");
b92ad394
PF
1268 }
1269}
1270
083a5890
GB
1271static void virt_instance_init(Object *obj)
1272{
1273 VirtMachineState *vms = VIRT_MACHINE(obj);
1274
2d710006
PM
1275 /* EL3 is disabled by default on virt: this makes us consistent
1276 * between KVM and TCG for this board, and it also allows us to
1277 * boot UEFI blobs which assume no TrustZone support.
1278 */
1279 vms->secure = false;
083a5890
GB
1280 object_property_add_bool(obj, "secure", virt_get_secure,
1281 virt_set_secure, NULL);
1282 object_property_set_description(obj, "secure",
1283 "Set on/off to enable/disable the ARM "
1284 "Security Extensions (TrustZone)",
1285 NULL);
5125f9cd
PF
1286
1287 /* High memory is enabled by default */
1288 vms->highmem = true;
1289 object_property_add_bool(obj, "highmem", virt_get_highmem,
1290 virt_set_highmem, NULL);
1291 object_property_set_description(obj, "highmem",
1292 "Set on/off to enable/disable using "
1293 "physical address space above 32 bits",
1294 NULL);
b92ad394
PF
1295 /* Default GIC type is v2 */
1296 vms->gic_version = 2;
1297 object_property_add_str(obj, "gic-version", virt_get_gic_version,
1298 virt_set_gic_version, NULL);
1299 object_property_set_description(obj, "gic-version",
1300 "Set GIC version. "
1301 "Valid values are 2, 3 and host", NULL);
083a5890
GB
1302}
1303
c2919690
GB
1304static void virt_class_init(ObjectClass *oc, void *data)
1305{
1306 MachineClass *mc = MACHINE_CLASS(oc);
1307
c2919690
GB
1308 mc->desc = "ARM Virtual Machine",
1309 mc->init = machvirt_init;
4b280b72
AJ
1310 /* Start max_cpus at the maximum QEMU supports. We'll further restrict
1311 * it later in machvirt_init, where we have more information about the
1312 * configuration of the particular instance.
b92ad394 1313 */
4b280b72 1314 mc->max_cpus = MAX_CPUMASK_BITS;
5f7a5a0e 1315 mc->has_dynamic_sysbus = true;
4e2c0b2a
PM
1316 mc->block_default_type = IF_VIRTIO;
1317 mc->no_cdrom = 1;
74de8c35 1318 mc->pci_allow_0_address = true;
c2919690
GB
1319}
1320
1321static const TypeInfo machvirt_info = {
1322 .name = TYPE_VIRT_MACHINE,
1323 .parent = TYPE_MACHINE,
1324 .instance_size = sizeof(VirtMachineState),
083a5890 1325 .instance_init = virt_instance_init,
c2919690
GB
1326 .class_size = sizeof(VirtMachineClass),
1327 .class_init = virt_class_init,
f5fdcd6e
PM
1328};
1329
1330static void machvirt_machine_init(void)
1331{
c2919690 1332 type_register_static(&machvirt_info);
f5fdcd6e
PM
1333}
1334
1335machine_init(machvirt_machine_init);