]> Git Repo - qemu.git/blame - hw/arm/virt.c
vl: Set machine ram_size, maxram_size and ram_slots earlier
[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"
350a9c9e 32#include "qemu/units.h"
da34e65c 33#include "qapi/error.h"
f5fdcd6e
PM
34#include "hw/sysbus.h"
35#include "hw/arm/arm.h"
36#include "hw/arm/primecell.h"
afe0b380 37#include "hw/arm/virt.h"
6f2062b9
EH
38#include "hw/vfio/vfio-calxeda-xgmac.h"
39#include "hw/vfio/vfio-amd-xgbe.h"
94692dcd 40#include "hw/display/ramfb.h"
f5fdcd6e
PM
41#include "hw/devices.h"
42#include "net/net.h"
43#include "sysemu/device_tree.h"
9695200a 44#include "sysemu/numa.h"
f5fdcd6e
PM
45#include "sysemu/sysemu.h"
46#include "sysemu/kvm.h"
acf82361 47#include "hw/loader.h"
f5fdcd6e
PM
48#include "exec/address-spaces.h"
49#include "qemu/bitops.h"
50#include "qemu/error-report.h"
4ab29b82 51#include "hw/pci-host/gpex.h"
5f7a5a0e
EA
52#include "hw/arm/sysbus-fdt.h"
53#include "hw/platform-bus.h"
decf4f80 54#include "hw/arm/fdt.h"
95eb49c8
AJ
55#include "hw/intc/arm_gic.h"
56#include "hw/intc/arm_gicv3_common.h"
e6fbcbc4 57#include "kvm_arm.h"
a2eb5c0c 58#include "hw/firmware/smbios.h"
b92ad394 59#include "qapi/visitor.h"
3e6ebb64 60#include "standard-headers/linux/input.h"
584105ea 61#include "hw/arm/smmuv3.h"
f5fdcd6e 62
3356ebce 63#define DEFINE_VIRT_MACHINE_LATEST(major, minor, latest) \
ab093c3c
AJ
64 static void virt_##major##_##minor##_class_init(ObjectClass *oc, \
65 void *data) \
66 { \
67 MachineClass *mc = MACHINE_CLASS(oc); \
68 virt_machine_##major##_##minor##_options(mc); \
69 mc->desc = "QEMU " # major "." # minor " ARM Virtual Machine"; \
3356ebce
AJ
70 if (latest) { \
71 mc->alias = "virt"; \
72 } \
ab093c3c
AJ
73 } \
74 static const TypeInfo machvirt_##major##_##minor##_info = { \
75 .name = MACHINE_TYPE_NAME("virt-" # major "." # minor), \
76 .parent = TYPE_VIRT_MACHINE, \
ab093c3c
AJ
77 .class_init = virt_##major##_##minor##_class_init, \
78 }; \
79 static void machvirt_machine_##major##_##minor##_init(void) \
80 { \
81 type_register_static(&machvirt_##major##_##minor##_info); \
82 } \
83 type_init(machvirt_machine_##major##_##minor##_init);
84
3356ebce
AJ
85#define DEFINE_VIRT_MACHINE_AS_LATEST(major, minor) \
86 DEFINE_VIRT_MACHINE_LATEST(major, minor, true)
87#define DEFINE_VIRT_MACHINE(major, minor) \
88 DEFINE_VIRT_MACHINE_LATEST(major, minor, false)
89
ab093c3c 90
a72d4363
AJ
91/* Number of external interrupt lines to configure the GIC with */
92#define NUM_IRQS 256
93
94#define PLATFORM_BUS_NUM_IRQS 64
95
71c27684
PM
96/* RAM limit in GB. Since VIRT_MEM starts at the 1GB mark, this means
97 * RAM can go up to the 256GB mark, leaving 256GB of the physical
98 * address space unallocated and free for future use between 256G and 512G.
99 * If we need to provide more RAM to VMs in the future then we need to:
100 * * allocate a second bank of RAM starting at 2TB and working up
101 * * fix the DT and ACPI table generation code in QEMU to correctly
102 * report two split lumps of RAM to the guest
103 * * fix KVM in the host kernel to allow guests with >40 bit address spaces
104 * (We don't want to fill all the way up to 512GB with RAM because
105 * we might want it for non-RAM purposes later. Conversely it seems
106 * reasonable to assume that anybody configuring a VM with a quarter
107 * of a terabyte of RAM will be doing it on a host with more than a
108 * terabyte of physical address space.)
109 */
110#define RAMLIMIT_GB 255
111#define RAMLIMIT_BYTES (RAMLIMIT_GB * 1024ULL * 1024 * 1024)
112
f5fdcd6e
PM
113/* Addresses and sizes of our components.
114 * 0..128MB is space for a flash device so we can run bootrom code such as UEFI.
115 * 128MB..256MB is used for miscellaneous device I/O.
116 * 256MB..1GB is reserved for possible future PCI support (ie where the
117 * PCI memory window will go if we add a PCI host controller).
118 * 1GB and up is RAM (which may happily spill over into the
119 * high memory region beyond 4GB).
120 * This represents a compromise between how much RAM can be given to
121 * a 32 bit VM and leaving space for expansion and in particular for PCI.
6e411af9
PM
122 * Note that devices should generally be placed at multiples of 0x10000,
123 * to accommodate guests using 64K pages.
f5fdcd6e 124 */
350a9c9e 125static const MemMapEntry base_memmap[] = {
f5fdcd6e 126 /* Space up to 0x8000000 is reserved for a boot ROM */
94edf02c
EA
127 [VIRT_FLASH] = { 0, 0x08000000 },
128 [VIRT_CPUPERIPHS] = { 0x08000000, 0x00020000 },
f5fdcd6e 129 /* GIC distributor and CPU interfaces sit inside the CPU peripheral space */
94edf02c
EA
130 [VIRT_GIC_DIST] = { 0x08000000, 0x00010000 },
131 [VIRT_GIC_CPU] = { 0x08010000, 0x00010000 },
132 [VIRT_GIC_V2M] = { 0x08020000, 0x00001000 },
55ef3233
LM
133 [VIRT_GIC_HYP] = { 0x08030000, 0x00010000 },
134 [VIRT_GIC_VCPU] = { 0x08040000, 0x00010000 },
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 },
584105ea 144 [VIRT_SMMU] = { 0x09050000, 0x00020000 },
94edf02c 145 [VIRT_MMIO] = { 0x0a000000, 0x00000200 },
f5fdcd6e 146 /* ...repeating for a total of NUM_VIRTIO_TRANSPORTS, each of that size */
94edf02c 147 [VIRT_PLATFORM_BUS] = { 0x0c000000, 0x02000000 },
83ec1923 148 [VIRT_SECURE_MEM] = { 0x0e000000, 0x01000000 },
94edf02c
EA
149 [VIRT_PCIE_MMIO] = { 0x10000000, 0x2eff0000 },
150 [VIRT_PCIE_PIO] = { 0x3eff0000, 0x00010000 },
151 [VIRT_PCIE_ECAM] = { 0x3f000000, 0x01000000 },
71c27684 152 [VIRT_MEM] = { 0x40000000, RAMLIMIT_BYTES },
350a9c9e
EA
153};
154
155/*
156 * Highmem IO Regions: This memory map is floating, located after the RAM.
157 * Each MemMapEntry base (GPA) will be dynamically computed, depending on the
158 * top of the RAM, so that its base get the same alignment as the size,
159 * ie. a 512GiB entry will be aligned on a 512GiB boundary. If there is
160 * less than 256GiB of RAM, the floating area starts at the 256GiB mark.
161 * Note the extended_memmap is sized so that it eventually also includes the
162 * base_memmap entries (VIRT_HIGH_GIC_REDIST2 index is greater than the last
163 * index of base_memmap).
164 */
165static MemMapEntry extended_memmap[] = {
f90747c4 166 /* Additional 64 MB redist region (can contain up to 512 redistributors) */
350a9c9e
EA
167 [VIRT_HIGH_GIC_REDIST2] = { 0x0, 64 * MiB },
168 [VIRT_HIGH_PCIE_ECAM] = { 0x0, 256 * MiB },
169 /* Second PCIe window */
170 [VIRT_HIGH_PCIE_MMIO] = { 0x0, 512 * GiB },
f5fdcd6e
PM
171};
172
173static const int a15irqmap[] = {
174 [VIRT_UART] = 1,
6e411af9 175 [VIRT_RTC] = 2,
4ab29b82 176 [VIRT_PCIE] = 3, /* ... to 6 */
b0a3721e 177 [VIRT_GPIO] = 7,
3df708eb 178 [VIRT_SECURE_UART] = 8,
f5fdcd6e 179 [VIRT_MMIO] = 16, /* ...to 16 + NUM_VIRTIO_TRANSPORTS - 1 */
bd204e63 180 [VIRT_GIC_V2M] = 48, /* ...to 48 + NUM_GICV2M_SPIS - 1 */
584105ea 181 [VIRT_SMMU] = 74, /* ...to 74 + NUM_SMMU_IRQS - 1 */
5f7a5a0e 182 [VIRT_PLATFORM_BUS] = 112, /* ...to 112 + PLATFORM_BUS_NUM_IRQS -1 */
f5fdcd6e
PM
183};
184
9ac4ef77 185static const char *valid_cpus[] = {
ba1ba5cc
IM
186 ARM_CPU_TYPE_NAME("cortex-a15"),
187 ARM_CPU_TYPE_NAME("cortex-a53"),
188 ARM_CPU_TYPE_NAME("cortex-a57"),
2264faa5 189 ARM_CPU_TYPE_NAME("cortex-a72"),
ba1ba5cc 190 ARM_CPU_TYPE_NAME("host"),
9076ddb3 191 ARM_CPU_TYPE_NAME("max"),
f5fdcd6e
PM
192};
193
ba1ba5cc 194static bool cpu_type_valid(const char *cpu)
f5fdcd6e
PM
195{
196 int i;
197
9ac4ef77
PM
198 for (i = 0; i < ARRAY_SIZE(valid_cpus); i++) {
199 if (strcmp(cpu, valid_cpus[i]) == 0) {
200 return true;
f5fdcd6e
PM
201 }
202 }
9ac4ef77 203 return false;
f5fdcd6e
PM
204}
205
c8ef2bda 206static void create_fdt(VirtMachineState *vms)
f5fdcd6e 207{
c8ef2bda 208 void *fdt = create_device_tree(&vms->fdt_size);
f5fdcd6e
PM
209
210 if (!fdt) {
211 error_report("create_device_tree() failed");
212 exit(1);
213 }
214
c8ef2bda 215 vms->fdt = fdt;
f5fdcd6e
PM
216
217 /* Header */
5a4348d1
PC
218 qemu_fdt_setprop_string(fdt, "/", "compatible", "linux,dummy-virt");
219 qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2);
220 qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2);
f5fdcd6e 221
e2eb3d29 222 /* /chosen must exist for load_dtb to fill in necessary properties later */
5a4348d1 223 qemu_fdt_add_subnode(fdt, "/chosen");
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 */
c8ef2bda 230 vms->clock_phandle = qemu_fdt_alloc_phandle(fdt);
5a4348d1
PC
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");
c8ef2bda 237 qemu_fdt_setprop_cell(fdt, "/apb-pclk", "phandle", vms->clock_phandle);
f5fdcd6e 238
c7637c04
AJ
239 if (have_numa_distance) {
240 int size = nb_numa_nodes * nb_numa_nodes * 3 * sizeof(uint32_t);
241 uint32_t *matrix = g_malloc0(size);
242 int idx, i, j;
243
244 for (i = 0; i < nb_numa_nodes; i++) {
245 for (j = 0; j < nb_numa_nodes; j++) {
246 idx = (i * nb_numa_nodes + j) * 3;
247 matrix[idx + 0] = cpu_to_be32(i);
248 matrix[idx + 1] = cpu_to_be32(j);
249 matrix[idx + 2] = cpu_to_be32(numa_info[i].distance[j]);
250 }
251 }
252
253 qemu_fdt_add_subnode(fdt, "/distance-map");
254 qemu_fdt_setprop_string(fdt, "/distance-map", "compatible",
255 "numa-distance-map-v1");
256 qemu_fdt_setprop(fdt, "/distance-map", "distance-matrix",
257 matrix, size);
258 g_free(matrix);
259 }
06955739
PS
260}
261
055a7f2b 262static void fdt_add_timer_nodes(const VirtMachineState *vms)
f5fdcd6e 263{
156bc9a5
PM
264 /* On real hardware these interrupts are level-triggered.
265 * On KVM they were edge-triggered before host kernel version 4.4,
266 * and level-triggered afterwards.
267 * On emulated QEMU they are level-triggered.
268 *
269 * Getting the DTB info about them wrong is awkward for some
270 * guest kernels:
271 * pre-4.8 ignore the DT and leave the interrupt configured
272 * with whatever the GIC reset value (or the bootloader) left it at
273 * 4.8 before rc6 honour the incorrect data by programming it back
274 * into the GIC, causing problems
275 * 4.8rc6 and later ignore the DT and always write "level triggered"
276 * into the GIC
277 *
278 * For backwards-compatibility, virt-2.8 and earlier will continue
279 * to say these are edge-triggered, but later machines will report
280 * the correct information.
f5fdcd6e 281 */
b32a9509 282 ARMCPU *armcpu;
156bc9a5
PM
283 VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms);
284 uint32_t irqflags = GIC_FDT_IRQ_FLAGS_LEVEL_HI;
285
286 if (vmc->claim_edge_triggered_timers) {
287 irqflags = GIC_FDT_IRQ_FLAGS_EDGE_LO_HI;
288 }
f5fdcd6e 289
055a7f2b 290 if (vms->gic_version == 2) {
b92ad394
PF
291 irqflags = deposit32(irqflags, GIC_FDT_IRQ_PPI_CPU_START,
292 GIC_FDT_IRQ_PPI_CPU_WIDTH,
c8ef2bda 293 (1 << vms->smp_cpus) - 1);
b92ad394 294 }
f5fdcd6e 295
c8ef2bda 296 qemu_fdt_add_subnode(vms->fdt, "/timer");
b32a9509
CF
297
298 armcpu = ARM_CPU(qemu_get_cpu(0));
299 if (arm_feature(&armcpu->env, ARM_FEATURE_V8)) {
300 const char compat[] = "arm,armv8-timer\0arm,armv7-timer";
c8ef2bda 301 qemu_fdt_setprop(vms->fdt, "/timer", "compatible",
b32a9509
CF
302 compat, sizeof(compat));
303 } else {
c8ef2bda 304 qemu_fdt_setprop_string(vms->fdt, "/timer", "compatible",
b32a9509
CF
305 "arm,armv7-timer");
306 }
c8ef2bda
PM
307 qemu_fdt_setprop(vms->fdt, "/timer", "always-on", NULL, 0);
308 qemu_fdt_setprop_cells(vms->fdt, "/timer", "interrupts",
ee246400
SZ
309 GIC_FDT_IRQ_TYPE_PPI, ARCH_TIMER_S_EL1_IRQ, irqflags,
310 GIC_FDT_IRQ_TYPE_PPI, ARCH_TIMER_NS_EL1_IRQ, irqflags,
311 GIC_FDT_IRQ_TYPE_PPI, ARCH_TIMER_VIRT_IRQ, irqflags,
312 GIC_FDT_IRQ_TYPE_PPI, ARCH_TIMER_NS_EL2_IRQ, irqflags);
f5fdcd6e
PM
313}
314
c8ef2bda 315static void fdt_add_cpu_nodes(const VirtMachineState *vms)
f5fdcd6e
PM
316{
317 int cpu;
8d45c54d 318 int addr_cells = 1;
4ccf5826 319 const MachineState *ms = MACHINE(vms);
8d45c54d
PF
320
321 /*
322 * From Documentation/devicetree/bindings/arm/cpus.txt
323 * On ARM v8 64-bit systems value should be set to 2,
324 * that corresponds to the MPIDR_EL1 register size.
325 * If MPIDR_EL1[63:32] value is equal to 0 on all CPUs
326 * in the system, #address-cells can be set to 1, since
327 * MPIDR_EL1[63:32] bits are not used for CPUs
328 * identification.
329 *
330 * Here we actually don't know whether our system is 32- or 64-bit one.
331 * The simplest way to go is to examine affinity IDs of all our CPUs. If
332 * at least one of them has Aff3 populated, we set #address-cells to 2.
333 */
c8ef2bda 334 for (cpu = 0; cpu < vms->smp_cpus; cpu++) {
8d45c54d
PF
335 ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(cpu));
336
337 if (armcpu->mp_affinity & ARM_AFF3_MASK) {
338 addr_cells = 2;
339 break;
340 }
341 }
f5fdcd6e 342
c8ef2bda
PM
343 qemu_fdt_add_subnode(vms->fdt, "/cpus");
344 qemu_fdt_setprop_cell(vms->fdt, "/cpus", "#address-cells", addr_cells);
345 qemu_fdt_setprop_cell(vms->fdt, "/cpus", "#size-cells", 0x0);
f5fdcd6e 346
c8ef2bda 347 for (cpu = vms->smp_cpus - 1; cpu >= 0; cpu--) {
f5fdcd6e
PM
348 char *nodename = g_strdup_printf("/cpus/cpu@%d", cpu);
349 ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(cpu));
4ccf5826 350 CPUState *cs = CPU(armcpu);
f5fdcd6e 351
c8ef2bda
PM
352 qemu_fdt_add_subnode(vms->fdt, nodename);
353 qemu_fdt_setprop_string(vms->fdt, nodename, "device_type", "cpu");
354 qemu_fdt_setprop_string(vms->fdt, nodename, "compatible",
f5fdcd6e
PM
355 armcpu->dtb_compatible);
356
2013c566
PM
357 if (vms->psci_conduit != QEMU_PSCI_CONDUIT_DISABLED
358 && vms->smp_cpus > 1) {
c8ef2bda 359 qemu_fdt_setprop_string(vms->fdt, nodename,
f5fdcd6e
PM
360 "enable-method", "psci");
361 }
362
8d45c54d 363 if (addr_cells == 2) {
c8ef2bda 364 qemu_fdt_setprop_u64(vms->fdt, nodename, "reg",
8d45c54d
PF
365 armcpu->mp_affinity);
366 } else {
c8ef2bda 367 qemu_fdt_setprop_cell(vms->fdt, nodename, "reg",
8d45c54d
PF
368 armcpu->mp_affinity);
369 }
370
4ccf5826
IM
371 if (ms->possible_cpus->cpus[cs->cpu_index].props.has_node_id) {
372 qemu_fdt_setprop_cell(vms->fdt, nodename, "numa-node-id",
373 ms->possible_cpus->cpus[cs->cpu_index].props.node_id);
9695200a
SZ
374 }
375
f5fdcd6e
PM
376 g_free(nodename);
377 }
378}
379
c8ef2bda 380static void fdt_add_its_gic_node(VirtMachineState *vms)
02f98731 381{
bb2a3348
EA
382 char *nodename;
383
c8ef2bda 384 vms->msi_phandle = qemu_fdt_alloc_phandle(vms->fdt);
bb2a3348
EA
385 nodename = g_strdup_printf("/intc/its@%" PRIx64,
386 vms->memmap[VIRT_GIC_ITS].base);
387 qemu_fdt_add_subnode(vms->fdt, nodename);
388 qemu_fdt_setprop_string(vms->fdt, nodename, "compatible",
02f98731 389 "arm,gic-v3-its");
bb2a3348
EA
390 qemu_fdt_setprop(vms->fdt, nodename, "msi-controller", NULL, 0);
391 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
c8ef2bda
PM
392 2, vms->memmap[VIRT_GIC_ITS].base,
393 2, vms->memmap[VIRT_GIC_ITS].size);
bb2a3348
EA
394 qemu_fdt_setprop_cell(vms->fdt, nodename, "phandle", vms->msi_phandle);
395 g_free(nodename);
02f98731
PF
396}
397
c8ef2bda 398static void fdt_add_v2m_gic_node(VirtMachineState *vms)
f5fdcd6e 399{
bb2a3348
EA
400 char *nodename;
401
402 nodename = g_strdup_printf("/intc/v2m@%" PRIx64,
403 vms->memmap[VIRT_GIC_V2M].base);
c8ef2bda 404 vms->msi_phandle = qemu_fdt_alloc_phandle(vms->fdt);
bb2a3348
EA
405 qemu_fdt_add_subnode(vms->fdt, nodename);
406 qemu_fdt_setprop_string(vms->fdt, nodename, "compatible",
bd204e63 407 "arm,gic-v2m-frame");
bb2a3348
EA
408 qemu_fdt_setprop(vms->fdt, nodename, "msi-controller", NULL, 0);
409 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
c8ef2bda
PM
410 2, vms->memmap[VIRT_GIC_V2M].base,
411 2, vms->memmap[VIRT_GIC_V2M].size);
bb2a3348
EA
412 qemu_fdt_setprop_cell(vms->fdt, nodename, "phandle", vms->msi_phandle);
413 g_free(nodename);
bd204e63 414}
f5fdcd6e 415
055a7f2b 416static void fdt_add_gic_node(VirtMachineState *vms)
bd204e63 417{
bb2a3348
EA
418 char *nodename;
419
c8ef2bda
PM
420 vms->gic_phandle = qemu_fdt_alloc_phandle(vms->fdt);
421 qemu_fdt_setprop_cell(vms->fdt, "/", "interrupt-parent", vms->gic_phandle);
422
bb2a3348
EA
423 nodename = g_strdup_printf("/intc@%" PRIx64,
424 vms->memmap[VIRT_GIC_DIST].base);
425 qemu_fdt_add_subnode(vms->fdt, nodename);
426 qemu_fdt_setprop_cell(vms->fdt, nodename, "#interrupt-cells", 3);
427 qemu_fdt_setprop(vms->fdt, nodename, "interrupt-controller", NULL, 0);
428 qemu_fdt_setprop_cell(vms->fdt, nodename, "#address-cells", 0x2);
429 qemu_fdt_setprop_cell(vms->fdt, nodename, "#size-cells", 0x2);
430 qemu_fdt_setprop(vms->fdt, nodename, "ranges", NULL, 0);
055a7f2b 431 if (vms->gic_version == 3) {
f90747c4
EA
432 int nb_redist_regions = virt_gicv3_redist_region_count(vms);
433
bb2a3348 434 qemu_fdt_setprop_string(vms->fdt, nodename, "compatible",
b92ad394 435 "arm,gic-v3");
f90747c4 436
bb2a3348 437 qemu_fdt_setprop_cell(vms->fdt, nodename,
f90747c4
EA
438 "#redistributor-regions", nb_redist_regions);
439
440 if (nb_redist_regions == 1) {
bb2a3348 441 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
f90747c4
EA
442 2, vms->memmap[VIRT_GIC_DIST].base,
443 2, vms->memmap[VIRT_GIC_DIST].size,
444 2, vms->memmap[VIRT_GIC_REDIST].base,
445 2, vms->memmap[VIRT_GIC_REDIST].size);
446 } else {
bb2a3348 447 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
bf424a12
EA
448 2, vms->memmap[VIRT_GIC_DIST].base,
449 2, vms->memmap[VIRT_GIC_DIST].size,
450 2, vms->memmap[VIRT_GIC_REDIST].base,
451 2, vms->memmap[VIRT_GIC_REDIST].size,
452 2, vms->memmap[VIRT_HIGH_GIC_REDIST2].base,
453 2, vms->memmap[VIRT_HIGH_GIC_REDIST2].size);
f90747c4
EA
454 }
455
f29cacfb 456 if (vms->virt) {
bb2a3348 457 qemu_fdt_setprop_cells(vms->fdt, nodename, "interrupts",
55ef3233 458 GIC_FDT_IRQ_TYPE_PPI, ARCH_GIC_MAINT_IRQ,
f29cacfb
PM
459 GIC_FDT_IRQ_FLAGS_LEVEL_HI);
460 }
b92ad394
PF
461 } else {
462 /* 'cortex-a15-gic' means 'GIC v2' */
bb2a3348 463 qemu_fdt_setprop_string(vms->fdt, nodename, "compatible",
b92ad394 464 "arm,cortex-a15-gic");
55ef3233
LM
465 if (!vms->virt) {
466 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
467 2, vms->memmap[VIRT_GIC_DIST].base,
468 2, vms->memmap[VIRT_GIC_DIST].size,
469 2, vms->memmap[VIRT_GIC_CPU].base,
470 2, vms->memmap[VIRT_GIC_CPU].size);
471 } else {
472 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
473 2, vms->memmap[VIRT_GIC_DIST].base,
474 2, vms->memmap[VIRT_GIC_DIST].size,
475 2, vms->memmap[VIRT_GIC_CPU].base,
476 2, vms->memmap[VIRT_GIC_CPU].size,
477 2, vms->memmap[VIRT_GIC_HYP].base,
478 2, vms->memmap[VIRT_GIC_HYP].size,
479 2, vms->memmap[VIRT_GIC_VCPU].base,
480 2, vms->memmap[VIRT_GIC_VCPU].size);
481 qemu_fdt_setprop_cells(vms->fdt, nodename, "interrupts",
482 GIC_FDT_IRQ_TYPE_PPI, ARCH_GIC_MAINT_IRQ,
483 GIC_FDT_IRQ_FLAGS_LEVEL_HI);
484 }
b92ad394
PF
485 }
486
bb2a3348
EA
487 qemu_fdt_setprop_cell(vms->fdt, nodename, "phandle", vms->gic_phandle);
488 g_free(nodename);
f5fdcd6e
PM
489}
490
055a7f2b 491static void fdt_add_pmu_nodes(const VirtMachineState *vms)
01fe6b60
SZ
492{
493 CPUState *cpu;
494 ARMCPU *armcpu;
495 uint32_t irqflags = GIC_FDT_IRQ_FLAGS_LEVEL_HI;
496
497 CPU_FOREACH(cpu) {
498 armcpu = ARM_CPU(cpu);
3f07cb2a 499 if (!arm_feature(&armcpu->env, ARM_FEATURE_PMU)) {
01fe6b60
SZ
500 return;
501 }
3f07cb2a 502 if (kvm_enabled()) {
b2bfe9f7
AJ
503 if (kvm_irqchip_in_kernel()) {
504 kvm_arm_pmu_set_irq(cpu, PPI(VIRTUAL_PMU_IRQ));
3f07cb2a 505 }
b2bfe9f7 506 kvm_arm_pmu_init(cpu);
3f07cb2a 507 }
01fe6b60
SZ
508 }
509
055a7f2b 510 if (vms->gic_version == 2) {
01fe6b60
SZ
511 irqflags = deposit32(irqflags, GIC_FDT_IRQ_PPI_CPU_START,
512 GIC_FDT_IRQ_PPI_CPU_WIDTH,
c8ef2bda 513 (1 << vms->smp_cpus) - 1);
01fe6b60
SZ
514 }
515
516 armcpu = ARM_CPU(qemu_get_cpu(0));
c8ef2bda 517 qemu_fdt_add_subnode(vms->fdt, "/pmu");
01fe6b60
SZ
518 if (arm_feature(&armcpu->env, ARM_FEATURE_V8)) {
519 const char compat[] = "arm,armv8-pmuv3";
c8ef2bda 520 qemu_fdt_setprop(vms->fdt, "/pmu", "compatible",
01fe6b60 521 compat, sizeof(compat));
c8ef2bda 522 qemu_fdt_setprop_cells(vms->fdt, "/pmu", "interrupts",
01fe6b60
SZ
523 GIC_FDT_IRQ_TYPE_PPI, VIRTUAL_PMU_IRQ, irqflags);
524 }
525}
526
c8ef2bda 527static void create_its(VirtMachineState *vms, DeviceState *gicdev)
02f98731
PF
528{
529 const char *itsclass = its_class_name();
530 DeviceState *dev;
531
532 if (!itsclass) {
533 /* Do nothing if not supported */
534 return;
535 }
536
537 dev = qdev_create(NULL, itsclass);
538
539 object_property_set_link(OBJECT(dev), OBJECT(gicdev), "parent-gicv3",
540 &error_abort);
541 qdev_init_nofail(dev);
c8ef2bda 542 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, vms->memmap[VIRT_GIC_ITS].base);
02f98731 543
c8ef2bda 544 fdt_add_its_gic_node(vms);
02f98731
PF
545}
546
c8ef2bda 547static void create_v2m(VirtMachineState *vms, qemu_irq *pic)
bd204e63
CD
548{
549 int i;
c8ef2bda 550 int irq = vms->irqmap[VIRT_GIC_V2M];
bd204e63
CD
551 DeviceState *dev;
552
553 dev = qdev_create(NULL, "arm-gicv2m");
c8ef2bda 554 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, vms->memmap[VIRT_GIC_V2M].base);
bd204e63
CD
555 qdev_prop_set_uint32(dev, "base-spi", irq);
556 qdev_prop_set_uint32(dev, "num-spi", NUM_GICV2M_SPIS);
557 qdev_init_nofail(dev);
558
559 for (i = 0; i < NUM_GICV2M_SPIS; i++) {
560 sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, pic[irq + i]);
561 }
562
c8ef2bda 563 fdt_add_v2m_gic_node(vms);
bd204e63
CD
564}
565
055a7f2b 566static void create_gic(VirtMachineState *vms, qemu_irq *pic)
64204743 567{
b92ad394 568 /* We create a standalone GIC */
64204743
PM
569 DeviceState *gicdev;
570 SysBusDevice *gicbusdev;
e6fbcbc4 571 const char *gictype;
055a7f2b 572 int type = vms->gic_version, i;
03d72fa1 573 uint32_t nb_redist_regions = 0;
64204743 574
b92ad394 575 gictype = (type == 3) ? gicv3_class_name() : gic_class_name();
64204743
PM
576
577 gicdev = qdev_create(NULL, gictype);
b92ad394 578 qdev_prop_set_uint32(gicdev, "revision", type);
64204743
PM
579 qdev_prop_set_uint32(gicdev, "num-cpu", smp_cpus);
580 /* Note that the num-irq property counts both internal and external
581 * interrupts; there are always 32 of the former (mandated by GIC spec).
582 */
583 qdev_prop_set_uint32(gicdev, "num-irq", NUM_IRQS + 32);
0e21f183 584 if (!kvm_irqchip_in_kernel()) {
0127937b 585 qdev_prop_set_bit(gicdev, "has-security-extensions", vms->secure);
0e21f183 586 }
1e575b66
EA
587
588 if (type == 3) {
589 uint32_t redist0_capacity =
590 vms->memmap[VIRT_GIC_REDIST].size / GICV3_REDIST_SIZE;
591 uint32_t redist0_count = MIN(smp_cpus, redist0_capacity);
592
03d72fa1
EA
593 nb_redist_regions = virt_gicv3_redist_region_count(vms);
594
595 qdev_prop_set_uint32(gicdev, "len-redist-region-count",
596 nb_redist_regions);
1e575b66 597 qdev_prop_set_uint32(gicdev, "redist-region-count[0]", redist0_count);
03d72fa1
EA
598
599 if (nb_redist_regions == 2) {
600 uint32_t redist1_capacity =
bf424a12 601 vms->memmap[VIRT_HIGH_GIC_REDIST2].size / GICV3_REDIST_SIZE;
03d72fa1
EA
602
603 qdev_prop_set_uint32(gicdev, "redist-region-count[1]",
604 MIN(smp_cpus - redist0_count, redist1_capacity));
605 }
55ef3233
LM
606 } else {
607 if (!kvm_irqchip_in_kernel()) {
608 qdev_prop_set_bit(gicdev, "has-virtualization-extensions",
609 vms->virt);
610 }
1e575b66 611 }
64204743
PM
612 qdev_init_nofail(gicdev);
613 gicbusdev = SYS_BUS_DEVICE(gicdev);
c8ef2bda 614 sysbus_mmio_map(gicbusdev, 0, vms->memmap[VIRT_GIC_DIST].base);
b92ad394 615 if (type == 3) {
c8ef2bda 616 sysbus_mmio_map(gicbusdev, 1, vms->memmap[VIRT_GIC_REDIST].base);
03d72fa1 617 if (nb_redist_regions == 2) {
bf424a12
EA
618 sysbus_mmio_map(gicbusdev, 2,
619 vms->memmap[VIRT_HIGH_GIC_REDIST2].base);
03d72fa1 620 }
b92ad394 621 } else {
c8ef2bda 622 sysbus_mmio_map(gicbusdev, 1, vms->memmap[VIRT_GIC_CPU].base);
55ef3233
LM
623 if (vms->virt) {
624 sysbus_mmio_map(gicbusdev, 2, vms->memmap[VIRT_GIC_HYP].base);
625 sysbus_mmio_map(gicbusdev, 3, vms->memmap[VIRT_GIC_VCPU].base);
626 }
b92ad394 627 }
64204743 628
5454006a
PM
629 /* Wire the outputs from each CPU's generic timer and the GICv3
630 * maintenance interrupt signal to the appropriate GIC PPI inputs,
631 * and the GIC's IRQ/FIQ/VIRQ/VFIQ interrupt outputs to the CPU's inputs.
64204743
PM
632 */
633 for (i = 0; i < smp_cpus; i++) {
634 DeviceState *cpudev = DEVICE(qemu_get_cpu(i));
0e3e858f 635 int ppibase = NUM_IRQS + i * GIC_INTERNAL + GIC_NR_SGIS;
a007b1f8
PM
636 int irq;
637 /* Mapping from the output timer irq lines from the CPU to the
638 * GIC PPI inputs we use for the virt board.
64204743 639 */
a007b1f8
PM
640 const int timer_irq[] = {
641 [GTIMER_PHYS] = ARCH_TIMER_NS_EL1_IRQ,
642 [GTIMER_VIRT] = ARCH_TIMER_VIRT_IRQ,
643 [GTIMER_HYP] = ARCH_TIMER_NS_EL2_IRQ,
644 [GTIMER_SEC] = ARCH_TIMER_S_EL1_IRQ,
645 };
646
647 for (irq = 0; irq < ARRAY_SIZE(timer_irq); irq++) {
648 qdev_connect_gpio_out(cpudev, irq,
649 qdev_get_gpio_in(gicdev,
650 ppibase + timer_irq[irq]));
651 }
64204743 652
55ef3233
LM
653 if (type == 3) {
654 qemu_irq irq = qdev_get_gpio_in(gicdev,
655 ppibase + ARCH_GIC_MAINT_IRQ);
656 qdev_connect_gpio_out_named(cpudev, "gicv3-maintenance-interrupt",
657 0, irq);
658 } else if (vms->virt) {
659 qemu_irq irq = qdev_get_gpio_in(gicdev,
660 ppibase + ARCH_GIC_MAINT_IRQ);
661 sysbus_connect_irq(gicbusdev, i + 4 * smp_cpus, irq);
662 }
663
07f48730
AJ
664 qdev_connect_gpio_out_named(cpudev, "pmu-interrupt", 0,
665 qdev_get_gpio_in(gicdev, ppibase
666 + VIRTUAL_PMU_IRQ));
5454006a 667
64204743 668 sysbus_connect_irq(gicbusdev, i, qdev_get_gpio_in(cpudev, ARM_CPU_IRQ));
8e7b4ca0
GB
669 sysbus_connect_irq(gicbusdev, i + smp_cpus,
670 qdev_get_gpio_in(cpudev, ARM_CPU_FIQ));
5454006a
PM
671 sysbus_connect_irq(gicbusdev, i + 2 * smp_cpus,
672 qdev_get_gpio_in(cpudev, ARM_CPU_VIRQ));
673 sysbus_connect_irq(gicbusdev, i + 3 * smp_cpus,
674 qdev_get_gpio_in(cpudev, ARM_CPU_VFIQ));
64204743
PM
675 }
676
677 for (i = 0; i < NUM_IRQS; i++) {
678 pic[i] = qdev_get_gpio_in(gicdev, i);
679 }
680
055a7f2b 681 fdt_add_gic_node(vms);
bd204e63 682
ccc11b02 683 if (type == 3 && vms->its) {
c8ef2bda 684 create_its(vms, gicdev);
2231f69b 685 } else if (type == 2) {
c8ef2bda 686 create_v2m(vms, pic);
b92ad394 687 }
64204743
PM
688}
689
c8ef2bda 690static void create_uart(const VirtMachineState *vms, qemu_irq *pic, int uart,
0ec7b3e7 691 MemoryRegion *mem, Chardev *chr)
f5fdcd6e
PM
692{
693 char *nodename;
c8ef2bda
PM
694 hwaddr base = vms->memmap[uart].base;
695 hwaddr size = vms->memmap[uart].size;
696 int irq = vms->irqmap[uart];
f5fdcd6e
PM
697 const char compat[] = "arm,pl011\0arm,primecell";
698 const char clocknames[] = "uartclk\0apb_pclk";
3df708eb
PM
699 DeviceState *dev = qdev_create(NULL, "pl011");
700 SysBusDevice *s = SYS_BUS_DEVICE(dev);
f5fdcd6e 701
9bbbf649 702 qdev_prop_set_chr(dev, "chardev", chr);
3df708eb
PM
703 qdev_init_nofail(dev);
704 memory_region_add_subregion(mem, base,
705 sysbus_mmio_get_region(s, 0));
706 sysbus_connect_irq(s, 0, pic[irq]);
f5fdcd6e
PM
707
708 nodename = g_strdup_printf("/pl011@%" PRIx64, base);
c8ef2bda 709 qemu_fdt_add_subnode(vms->fdt, nodename);
f5fdcd6e 710 /* Note that we can't use setprop_string because of the embedded NUL */
c8ef2bda 711 qemu_fdt_setprop(vms->fdt, nodename, "compatible",
f5fdcd6e 712 compat, sizeof(compat));
c8ef2bda 713 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
f5fdcd6e 714 2, base, 2, size);
c8ef2bda 715 qemu_fdt_setprop_cells(vms->fdt, nodename, "interrupts",
f5fdcd6e 716 GIC_FDT_IRQ_TYPE_SPI, irq,
0be969a2 717 GIC_FDT_IRQ_FLAGS_LEVEL_HI);
c8ef2bda
PM
718 qemu_fdt_setprop_cells(vms->fdt, nodename, "clocks",
719 vms->clock_phandle, vms->clock_phandle);
720 qemu_fdt_setprop(vms->fdt, nodename, "clock-names",
f5fdcd6e 721 clocknames, sizeof(clocknames));
f022b8e9 722
3df708eb 723 if (uart == VIRT_UART) {
c8ef2bda 724 qemu_fdt_setprop_string(vms->fdt, "/chosen", "stdout-path", nodename);
3df708eb
PM
725 } else {
726 /* Mark as not usable by the normal world */
c8ef2bda
PM
727 qemu_fdt_setprop_string(vms->fdt, nodename, "status", "disabled");
728 qemu_fdt_setprop_string(vms->fdt, nodename, "secure-status", "okay");
fb23d693
JF
729
730 qemu_fdt_add_subnode(vms->fdt, "/secure-chosen");
731 qemu_fdt_setprop_string(vms->fdt, "/secure-chosen", "stdout-path",
732 nodename);
3df708eb
PM
733 }
734
f5fdcd6e
PM
735 g_free(nodename);
736}
737
c8ef2bda 738static void create_rtc(const VirtMachineState *vms, qemu_irq *pic)
6e411af9
PM
739{
740 char *nodename;
c8ef2bda
PM
741 hwaddr base = vms->memmap[VIRT_RTC].base;
742 hwaddr size = vms->memmap[VIRT_RTC].size;
743 int irq = vms->irqmap[VIRT_RTC];
6e411af9
PM
744 const char compat[] = "arm,pl031\0arm,primecell";
745
746 sysbus_create_simple("pl031", base, pic[irq]);
747
748 nodename = g_strdup_printf("/pl031@%" PRIx64, base);
c8ef2bda
PM
749 qemu_fdt_add_subnode(vms->fdt, nodename);
750 qemu_fdt_setprop(vms->fdt, nodename, "compatible", compat, sizeof(compat));
751 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
6e411af9 752 2, base, 2, size);
c8ef2bda 753 qemu_fdt_setprop_cells(vms->fdt, nodename, "interrupts",
6e411af9 754 GIC_FDT_IRQ_TYPE_SPI, irq,
0be969a2 755 GIC_FDT_IRQ_FLAGS_LEVEL_HI);
c8ef2bda
PM
756 qemu_fdt_setprop_cell(vms->fdt, nodename, "clocks", vms->clock_phandle);
757 qemu_fdt_setprop_string(vms->fdt, nodename, "clock-names", "apb_pclk");
6e411af9
PM
758 g_free(nodename);
759}
760
94f02c5e 761static DeviceState *gpio_key_dev;
4bedd849
SZ
762static void virt_powerdown_req(Notifier *n, void *opaque)
763{
764 /* use gpio Pin 3 for power button event */
94f02c5e 765 qemu_set_irq(qdev_get_gpio_in(gpio_key_dev, 0), 1);
4bedd849
SZ
766}
767
768static Notifier virt_system_powerdown_notifier = {
769 .notify = virt_powerdown_req
770};
771
c8ef2bda 772static void create_gpio(const VirtMachineState *vms, qemu_irq *pic)
b0a3721e
SZ
773{
774 char *nodename;
94f02c5e 775 DeviceState *pl061_dev;
c8ef2bda
PM
776 hwaddr base = vms->memmap[VIRT_GPIO].base;
777 hwaddr size = vms->memmap[VIRT_GPIO].size;
778 int irq = vms->irqmap[VIRT_GPIO];
b0a3721e
SZ
779 const char compat[] = "arm,pl061\0arm,primecell";
780
4bedd849 781 pl061_dev = sysbus_create_simple("pl061", base, pic[irq]);
b0a3721e 782
c8ef2bda 783 uint32_t phandle = qemu_fdt_alloc_phandle(vms->fdt);
b0a3721e 784 nodename = g_strdup_printf("/pl061@%" PRIx64, base);
c8ef2bda
PM
785 qemu_fdt_add_subnode(vms->fdt, nodename);
786 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
b0a3721e 787 2, base, 2, size);
c8ef2bda
PM
788 qemu_fdt_setprop(vms->fdt, nodename, "compatible", compat, sizeof(compat));
789 qemu_fdt_setprop_cell(vms->fdt, nodename, "#gpio-cells", 2);
790 qemu_fdt_setprop(vms->fdt, nodename, "gpio-controller", NULL, 0);
791 qemu_fdt_setprop_cells(vms->fdt, nodename, "interrupts",
b0a3721e
SZ
792 GIC_FDT_IRQ_TYPE_SPI, irq,
793 GIC_FDT_IRQ_FLAGS_LEVEL_HI);
c8ef2bda
PM
794 qemu_fdt_setprop_cell(vms->fdt, nodename, "clocks", vms->clock_phandle);
795 qemu_fdt_setprop_string(vms->fdt, nodename, "clock-names", "apb_pclk");
796 qemu_fdt_setprop_cell(vms->fdt, nodename, "phandle", phandle);
3e6ebb64 797
94f02c5e
SZ
798 gpio_key_dev = sysbus_create_simple("gpio-key", -1,
799 qdev_get_gpio_in(pl061_dev, 3));
c8ef2bda
PM
800 qemu_fdt_add_subnode(vms->fdt, "/gpio-keys");
801 qemu_fdt_setprop_string(vms->fdt, "/gpio-keys", "compatible", "gpio-keys");
802 qemu_fdt_setprop_cell(vms->fdt, "/gpio-keys", "#size-cells", 0);
803 qemu_fdt_setprop_cell(vms->fdt, "/gpio-keys", "#address-cells", 1);
3e6ebb64 804
c8ef2bda
PM
805 qemu_fdt_add_subnode(vms->fdt, "/gpio-keys/poweroff");
806 qemu_fdt_setprop_string(vms->fdt, "/gpio-keys/poweroff",
3e6ebb64 807 "label", "GPIO Key Poweroff");
c8ef2bda 808 qemu_fdt_setprop_cell(vms->fdt, "/gpio-keys/poweroff", "linux,code",
3e6ebb64 809 KEY_POWER);
c8ef2bda 810 qemu_fdt_setprop_cells(vms->fdt, "/gpio-keys/poweroff",
3e6ebb64 811 "gpios", phandle, 3, 0);
b0a3721e 812
4bedd849
SZ
813 /* connect powerdown request */
814 qemu_register_powerdown_notifier(&virt_system_powerdown_notifier);
815
b0a3721e
SZ
816 g_free(nodename);
817}
818
c8ef2bda 819static void create_virtio_devices(const VirtMachineState *vms, qemu_irq *pic)
f5fdcd6e
PM
820{
821 int i;
c8ef2bda 822 hwaddr size = vms->memmap[VIRT_MMIO].size;
f5fdcd6e 823
587078f0
LE
824 /* We create the transports in forwards order. Since qbus_realize()
825 * prepends (not appends) new child buses, the incrementing loop below will
826 * create a list of virtio-mmio buses with decreasing base addresses.
827 *
828 * When a -device option is processed from the command line,
829 * qbus_find_recursive() picks the next free virtio-mmio bus in forwards
830 * order. The upshot is that -device options in increasing command line
831 * order are mapped to virtio-mmio buses with decreasing base addresses.
832 *
833 * When this code was originally written, that arrangement ensured that the
834 * guest Linux kernel would give the lowest "name" (/dev/vda, eth0, etc) to
835 * the first -device on the command line. (The end-to-end order is a
836 * function of this loop, qbus_realize(), qbus_find_recursive(), and the
837 * guest kernel's name-to-address assignment strategy.)
838 *
839 * Meanwhile, the kernel's traversal seems to have been reversed; see eg.
840 * the message, if not necessarily the code, of commit 70161ff336.
841 * Therefore the loop now establishes the inverse of the original intent.
842 *
843 * Unfortunately, we can't counteract the kernel change by reversing the
844 * loop; it would break existing command lines.
845 *
846 * In any case, the kernel makes no guarantee about the stability of
847 * enumeration order of virtio devices (as demonstrated by it changing
848 * between kernel versions). For reliable and stable identification
849 * of disks users must use UUIDs or similar mechanisms.
f5fdcd6e
PM
850 */
851 for (i = 0; i < NUM_VIRTIO_TRANSPORTS; i++) {
c8ef2bda
PM
852 int irq = vms->irqmap[VIRT_MMIO] + i;
853 hwaddr base = vms->memmap[VIRT_MMIO].base + i * size;
f5fdcd6e
PM
854
855 sysbus_create_simple("virtio-mmio", base, pic[irq]);
856 }
857
587078f0
LE
858 /* We add dtb nodes in reverse order so that they appear in the finished
859 * device tree lowest address first.
860 *
861 * Note that this mapping is independent of the loop above. The previous
862 * loop influences virtio device to virtio transport assignment, whereas
863 * this loop controls how virtio transports are laid out in the dtb.
864 */
f5fdcd6e
PM
865 for (i = NUM_VIRTIO_TRANSPORTS - 1; i >= 0; i--) {
866 char *nodename;
c8ef2bda
PM
867 int irq = vms->irqmap[VIRT_MMIO] + i;
868 hwaddr base = vms->memmap[VIRT_MMIO].base + i * size;
f5fdcd6e
PM
869
870 nodename = g_strdup_printf("/virtio_mmio@%" PRIx64, base);
c8ef2bda
PM
871 qemu_fdt_add_subnode(vms->fdt, nodename);
872 qemu_fdt_setprop_string(vms->fdt, nodename,
5a4348d1 873 "compatible", "virtio,mmio");
c8ef2bda 874 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
5a4348d1 875 2, base, 2, size);
c8ef2bda 876 qemu_fdt_setprop_cells(vms->fdt, nodename, "interrupts",
5a4348d1
PC
877 GIC_FDT_IRQ_TYPE_SPI, irq,
878 GIC_FDT_IRQ_FLAGS_EDGE_LO_HI);
054bb7b2 879 qemu_fdt_setprop(vms->fdt, nodename, "dma-coherent", NULL, 0);
f5fdcd6e
PM
880 g_free(nodename);
881 }
882}
883
acf82361 884static void create_one_flash(const char *name, hwaddr flashbase,
738a5d9f
PM
885 hwaddr flashsize, const char *file,
886 MemoryRegion *sysmem)
acf82361
PM
887{
888 /* Create and map a single flash device. We use the same
889 * parameters as the flash devices on the Versatile Express board.
890 */
891 DriveInfo *dinfo = drive_get_next(IF_PFLASH);
892 DeviceState *dev = qdev_create(NULL, "cfi.pflash01");
16f4a8dc 893 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
acf82361
PM
894 const uint64_t sectorlength = 256 * 1024;
895
9b3d111a
MA
896 if (dinfo) {
897 qdev_prop_set_drive(dev, "drive", blk_by_legacy_dinfo(dinfo),
898 &error_abort);
acf82361
PM
899 }
900
901 qdev_prop_set_uint32(dev, "num-blocks", flashsize / sectorlength);
902 qdev_prop_set_uint64(dev, "sector-length", sectorlength);
903 qdev_prop_set_uint8(dev, "width", 4);
904 qdev_prop_set_uint8(dev, "device-width", 2);
e9809422 905 qdev_prop_set_bit(dev, "big-endian", false);
acf82361
PM
906 qdev_prop_set_uint16(dev, "id0", 0x89);
907 qdev_prop_set_uint16(dev, "id1", 0x18);
908 qdev_prop_set_uint16(dev, "id2", 0x00);
909 qdev_prop_set_uint16(dev, "id3", 0x00);
910 qdev_prop_set_string(dev, "name", name);
911 qdev_init_nofail(dev);
912
738a5d9f
PM
913 memory_region_add_subregion(sysmem, flashbase,
914 sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0));
acf82361 915
16f4a8dc 916 if (file) {
6e05a12f 917 char *fn;
4de9a883 918 int image_size;
acf82361
PM
919
920 if (drive_get(IF_PFLASH, 0, 0)) {
921 error_report("The contents of the first flash device may be "
922 "specified with -bios or with -drive if=pflash... "
923 "but you cannot use both options at once");
924 exit(1);
925 }
16f4a8dc 926 fn = qemu_find_file(QEMU_FILE_TYPE_BIOS, file);
4de9a883 927 if (!fn) {
16f4a8dc 928 error_report("Could not find ROM image '%s'", file);
4de9a883
SW
929 exit(1);
930 }
16f4a8dc 931 image_size = load_image_mr(fn, sysbus_mmio_get_region(sbd, 0));
4de9a883
SW
932 g_free(fn);
933 if (image_size < 0) {
16f4a8dc 934 error_report("Could not load ROM image '%s'", file);
acf82361
PM
935 exit(1);
936 }
937 }
16f4a8dc
PM
938}
939
c8ef2bda 940static void create_flash(const VirtMachineState *vms,
738a5d9f
PM
941 MemoryRegion *sysmem,
942 MemoryRegion *secure_sysmem)
16f4a8dc
PM
943{
944 /* Create two flash devices to fill the VIRT_FLASH space in the memmap.
945 * Any file passed via -bios goes in the first of these.
738a5d9f
PM
946 * sysmem is the system memory space. secure_sysmem is the secure view
947 * of the system, and the first flash device should be made visible only
948 * there. The second flash device is visible to both secure and nonsecure.
949 * If sysmem == secure_sysmem this means there is no separate Secure
950 * address space and both flash devices are generally visible.
16f4a8dc 951 */
c8ef2bda
PM
952 hwaddr flashsize = vms->memmap[VIRT_FLASH].size / 2;
953 hwaddr flashbase = vms->memmap[VIRT_FLASH].base;
16f4a8dc 954 char *nodename;
acf82361 955
738a5d9f
PM
956 create_one_flash("virt.flash0", flashbase, flashsize,
957 bios_name, secure_sysmem);
958 create_one_flash("virt.flash1", flashbase + flashsize, flashsize,
959 NULL, sysmem);
acf82361 960
738a5d9f
PM
961 if (sysmem == secure_sysmem) {
962 /* Report both flash devices as a single node in the DT */
963 nodename = g_strdup_printf("/flash@%" PRIx64, flashbase);
c8ef2bda
PM
964 qemu_fdt_add_subnode(vms->fdt, nodename);
965 qemu_fdt_setprop_string(vms->fdt, nodename, "compatible", "cfi-flash");
966 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
738a5d9f
PM
967 2, flashbase, 2, flashsize,
968 2, flashbase + flashsize, 2, flashsize);
c8ef2bda 969 qemu_fdt_setprop_cell(vms->fdt, nodename, "bank-width", 4);
738a5d9f
PM
970 g_free(nodename);
971 } else {
972 /* Report the devices as separate nodes so we can mark one as
973 * only visible to the secure world.
974 */
975 nodename = g_strdup_printf("/secflash@%" PRIx64, flashbase);
c8ef2bda
PM
976 qemu_fdt_add_subnode(vms->fdt, nodename);
977 qemu_fdt_setprop_string(vms->fdt, nodename, "compatible", "cfi-flash");
978 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
738a5d9f 979 2, flashbase, 2, flashsize);
c8ef2bda
PM
980 qemu_fdt_setprop_cell(vms->fdt, nodename, "bank-width", 4);
981 qemu_fdt_setprop_string(vms->fdt, nodename, "status", "disabled");
982 qemu_fdt_setprop_string(vms->fdt, nodename, "secure-status", "okay");
738a5d9f
PM
983 g_free(nodename);
984
985 nodename = g_strdup_printf("/flash@%" PRIx64, flashbase);
c8ef2bda
PM
986 qemu_fdt_add_subnode(vms->fdt, nodename);
987 qemu_fdt_setprop_string(vms->fdt, nodename, "compatible", "cfi-flash");
988 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
738a5d9f 989 2, flashbase + flashsize, 2, flashsize);
c8ef2bda 990 qemu_fdt_setprop_cell(vms->fdt, nodename, "bank-width", 4);
738a5d9f
PM
991 g_free(nodename);
992 }
acf82361
PM
993}
994
af1f60a4 995static FWCfgState *create_fw_cfg(const VirtMachineState *vms, AddressSpace *as)
578f3c7b 996{
c8ef2bda
PM
997 hwaddr base = vms->memmap[VIRT_FW_CFG].base;
998 hwaddr size = vms->memmap[VIRT_FW_CFG].size;
5836d168 999 FWCfgState *fw_cfg;
578f3c7b
LE
1000 char *nodename;
1001
5836d168
IM
1002 fw_cfg = fw_cfg_init_mem_wide(base + 8, base, 8, base + 16, as);
1003 fw_cfg_add_i16(fw_cfg, FW_CFG_NB_CPUS, (uint16_t)smp_cpus);
578f3c7b
LE
1004
1005 nodename = g_strdup_printf("/fw-cfg@%" PRIx64, base);
c8ef2bda
PM
1006 qemu_fdt_add_subnode(vms->fdt, nodename);
1007 qemu_fdt_setprop_string(vms->fdt, nodename,
578f3c7b 1008 "compatible", "qemu,fw-cfg-mmio");
c8ef2bda 1009 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
578f3c7b 1010 2, base, 2, size);
14efdb5c 1011 qemu_fdt_setprop(vms->fdt, nodename, "dma-coherent", NULL, 0);
578f3c7b 1012 g_free(nodename);
af1f60a4 1013 return fw_cfg;
578f3c7b
LE
1014}
1015
c8ef2bda 1016static void create_pcie_irq_map(const VirtMachineState *vms,
9ac4ef77 1017 uint32_t gic_phandle,
4ab29b82
AG
1018 int first_irq, const char *nodename)
1019{
1020 int devfn, pin;
dfd90a87 1021 uint32_t full_irq_map[4 * 4 * 10] = { 0 };
4ab29b82
AG
1022 uint32_t *irq_map = full_irq_map;
1023
1024 for (devfn = 0; devfn <= 0x18; devfn += 0x8) {
1025 for (pin = 0; pin < 4; pin++) {
1026 int irq_type = GIC_FDT_IRQ_TYPE_SPI;
1027 int irq_nr = first_irq + ((pin + PCI_SLOT(devfn)) % PCI_NUM_PINS);
1028 int irq_level = GIC_FDT_IRQ_FLAGS_LEVEL_HI;
1029 int i;
1030
1031 uint32_t map[] = {
1032 devfn << 8, 0, 0, /* devfn */
1033 pin + 1, /* PCI pin */
dfd90a87 1034 gic_phandle, 0, 0, irq_type, irq_nr, irq_level }; /* GIC irq */
4ab29b82
AG
1035
1036 /* Convert map to big endian */
dfd90a87 1037 for (i = 0; i < 10; i++) {
4ab29b82
AG
1038 irq_map[i] = cpu_to_be32(map[i]);
1039 }
dfd90a87 1040 irq_map += 10;
4ab29b82
AG
1041 }
1042 }
1043
c8ef2bda 1044 qemu_fdt_setprop(vms->fdt, nodename, "interrupt-map",
4ab29b82
AG
1045 full_irq_map, sizeof(full_irq_map));
1046
c8ef2bda 1047 qemu_fdt_setprop_cells(vms->fdt, nodename, "interrupt-map-mask",
4ab29b82
AG
1048 0x1800, 0, 0, /* devfn (PCI_SLOT(3)) */
1049 0x7 /* PCI irq */);
1050}
1051
584105ea
PM
1052static void create_smmu(const VirtMachineState *vms, qemu_irq *pic,
1053 PCIBus *bus)
1054{
1055 char *node;
1056 const char compat[] = "arm,smmu-v3";
1057 int irq = vms->irqmap[VIRT_SMMU];
1058 int i;
1059 hwaddr base = vms->memmap[VIRT_SMMU].base;
1060 hwaddr size = vms->memmap[VIRT_SMMU].size;
1061 const char irq_names[] = "eventq\0priq\0cmdq-sync\0gerror";
1062 DeviceState *dev;
1063
1064 if (vms->iommu != VIRT_IOMMU_SMMUV3 || !vms->iommu_phandle) {
1065 return;
1066 }
1067
1068 dev = qdev_create(NULL, "arm-smmuv3");
1069
1070 object_property_set_link(OBJECT(dev), OBJECT(bus), "primary-bus",
1071 &error_abort);
1072 qdev_init_nofail(dev);
1073 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base);
1074 for (i = 0; i < NUM_SMMU_IRQS; i++) {
1075 sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, pic[irq + i]);
1076 }
1077
1078 node = g_strdup_printf("/smmuv3@%" PRIx64, base);
1079 qemu_fdt_add_subnode(vms->fdt, node);
1080 qemu_fdt_setprop(vms->fdt, node, "compatible", compat, sizeof(compat));
1081 qemu_fdt_setprop_sized_cells(vms->fdt, node, "reg", 2, base, 2, size);
1082
1083 qemu_fdt_setprop_cells(vms->fdt, node, "interrupts",
1084 GIC_FDT_IRQ_TYPE_SPI, irq , GIC_FDT_IRQ_FLAGS_EDGE_LO_HI,
1085 GIC_FDT_IRQ_TYPE_SPI, irq + 1, GIC_FDT_IRQ_FLAGS_EDGE_LO_HI,
1086 GIC_FDT_IRQ_TYPE_SPI, irq + 2, GIC_FDT_IRQ_FLAGS_EDGE_LO_HI,
1087 GIC_FDT_IRQ_TYPE_SPI, irq + 3, GIC_FDT_IRQ_FLAGS_EDGE_LO_HI);
1088
1089 qemu_fdt_setprop(vms->fdt, node, "interrupt-names", irq_names,
1090 sizeof(irq_names));
1091
1092 qemu_fdt_setprop_cell(vms->fdt, node, "clocks", vms->clock_phandle);
1093 qemu_fdt_setprop_string(vms->fdt, node, "clock-names", "apb_pclk");
1094 qemu_fdt_setprop(vms->fdt, node, "dma-coherent", NULL, 0);
1095
1096 qemu_fdt_setprop_cell(vms->fdt, node, "#iommu-cells", 1);
1097
1098 qemu_fdt_setprop_cell(vms->fdt, node, "phandle", vms->iommu_phandle);
1099 g_free(node);
1100}
1101
1102static void create_pcie(VirtMachineState *vms, qemu_irq *pic)
4ab29b82 1103{
c8ef2bda
PM
1104 hwaddr base_mmio = vms->memmap[VIRT_PCIE_MMIO].base;
1105 hwaddr size_mmio = vms->memmap[VIRT_PCIE_MMIO].size;
bf424a12
EA
1106 hwaddr base_mmio_high = vms->memmap[VIRT_HIGH_PCIE_MMIO].base;
1107 hwaddr size_mmio_high = vms->memmap[VIRT_HIGH_PCIE_MMIO].size;
c8ef2bda
PM
1108 hwaddr base_pio = vms->memmap[VIRT_PCIE_PIO].base;
1109 hwaddr size_pio = vms->memmap[VIRT_PCIE_PIO].size;
601d626d 1110 hwaddr base_ecam, size_ecam;
6a1f001b 1111 hwaddr base = base_mmio;
601d626d 1112 int nr_pcie_buses;
c8ef2bda 1113 int irq = vms->irqmap[VIRT_PCIE];
4ab29b82
AG
1114 MemoryRegion *mmio_alias;
1115 MemoryRegion *mmio_reg;
1116 MemoryRegion *ecam_alias;
1117 MemoryRegion *ecam_reg;
1118 DeviceState *dev;
1119 char *nodename;
601d626d 1120 int i, ecam_id;
fea9b3ca 1121 PCIHostState *pci;
4ab29b82 1122
4ab29b82
AG
1123 dev = qdev_create(NULL, TYPE_GPEX_HOST);
1124 qdev_init_nofail(dev);
1125
601d626d
EA
1126 ecam_id = VIRT_ECAM_ID(vms->highmem_ecam);
1127 base_ecam = vms->memmap[ecam_id].base;
1128 size_ecam = vms->memmap[ecam_id].size;
1129 nr_pcie_buses = size_ecam / PCIE_MMCFG_SIZE_MIN;
4ab29b82
AG
1130 /* Map only the first size_ecam bytes of ECAM space */
1131 ecam_alias = g_new0(MemoryRegion, 1);
1132 ecam_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0);
1133 memory_region_init_alias(ecam_alias, OBJECT(dev), "pcie-ecam",
1134 ecam_reg, 0, size_ecam);
1135 memory_region_add_subregion(get_system_memory(), base_ecam, ecam_alias);
1136
1137 /* Map the MMIO window into system address space so as to expose
1138 * the section of PCI MMIO space which starts at the same base address
1139 * (ie 1:1 mapping for that part of PCI MMIO space visible through
1140 * the window).
1141 */
1142 mmio_alias = g_new0(MemoryRegion, 1);
1143 mmio_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 1);
1144 memory_region_init_alias(mmio_alias, OBJECT(dev), "pcie-mmio",
1145 mmio_reg, base_mmio, size_mmio);
1146 memory_region_add_subregion(get_system_memory(), base_mmio, mmio_alias);
1147
0127937b 1148 if (vms->highmem) {
5125f9cd
PF
1149 /* Map high MMIO space */
1150 MemoryRegion *high_mmio_alias = g_new0(MemoryRegion, 1);
1151
1152 memory_region_init_alias(high_mmio_alias, OBJECT(dev), "pcie-mmio-high",
1153 mmio_reg, base_mmio_high, size_mmio_high);
1154 memory_region_add_subregion(get_system_memory(), base_mmio_high,
1155 high_mmio_alias);
1156 }
1157
4ab29b82 1158 /* Map IO port space */
6a1f001b 1159 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 2, base_pio);
4ab29b82
AG
1160
1161 for (i = 0; i < GPEX_NUM_IRQS; i++) {
1162 sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, pic[irq + i]);
c9bb8e16 1163 gpex_set_irq_num(GPEX_HOST(dev), i, irq + i);
4ab29b82
AG
1164 }
1165
fea9b3ca
AK
1166 pci = PCI_HOST_BRIDGE(dev);
1167 if (pci->bus) {
1168 for (i = 0; i < nb_nics; i++) {
1169 NICInfo *nd = &nd_table[i];
1170
1171 if (!nd->model) {
1172 nd->model = g_strdup("virtio");
1173 }
1174
1175 pci_nic_init_nofail(nd, pci->bus, nd->model, NULL);
1176 }
1177 }
1178
4ab29b82 1179 nodename = g_strdup_printf("/pcie@%" PRIx64, base);
c8ef2bda
PM
1180 qemu_fdt_add_subnode(vms->fdt, nodename);
1181 qemu_fdt_setprop_string(vms->fdt, nodename,
4ab29b82 1182 "compatible", "pci-host-ecam-generic");
c8ef2bda
PM
1183 qemu_fdt_setprop_string(vms->fdt, nodename, "device_type", "pci");
1184 qemu_fdt_setprop_cell(vms->fdt, nodename, "#address-cells", 3);
1185 qemu_fdt_setprop_cell(vms->fdt, nodename, "#size-cells", 2);
6d9c1b8d 1186 qemu_fdt_setprop_cell(vms->fdt, nodename, "linux,pci-domain", 0);
c8ef2bda 1187 qemu_fdt_setprop_cells(vms->fdt, nodename, "bus-range", 0,
4ab29b82 1188 nr_pcie_buses - 1);
c8ef2bda 1189 qemu_fdt_setprop(vms->fdt, nodename, "dma-coherent", NULL, 0);
4ab29b82 1190
c8ef2bda
PM
1191 if (vms->msi_phandle) {
1192 qemu_fdt_setprop_cells(vms->fdt, nodename, "msi-parent",
1193 vms->msi_phandle);
b92ad394 1194 }
bd204e63 1195
c8ef2bda 1196 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
4ab29b82 1197 2, base_ecam, 2, size_ecam);
5125f9cd 1198
0127937b 1199 if (vms->highmem) {
c8ef2bda 1200 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "ranges",
5125f9cd
PF
1201 1, FDT_PCI_RANGE_IOPORT, 2, 0,
1202 2, base_pio, 2, size_pio,
1203 1, FDT_PCI_RANGE_MMIO, 2, base_mmio,
1204 2, base_mmio, 2, size_mmio,
1205 1, FDT_PCI_RANGE_MMIO_64BIT,
1206 2, base_mmio_high,
1207 2, base_mmio_high, 2, size_mmio_high);
1208 } else {
c8ef2bda 1209 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "ranges",
5125f9cd
PF
1210 1, FDT_PCI_RANGE_IOPORT, 2, 0,
1211 2, base_pio, 2, size_pio,
1212 1, FDT_PCI_RANGE_MMIO, 2, base_mmio,
1213 2, base_mmio, 2, size_mmio);
1214 }
4ab29b82 1215
c8ef2bda
PM
1216 qemu_fdt_setprop_cell(vms->fdt, nodename, "#interrupt-cells", 1);
1217 create_pcie_irq_map(vms, vms->gic_phandle, irq, nodename);
4ab29b82 1218
584105ea
PM
1219 if (vms->iommu) {
1220 vms->iommu_phandle = qemu_fdt_alloc_phandle(vms->fdt);
1221
1222 create_smmu(vms, pic, pci->bus);
1223
1224 qemu_fdt_setprop_cells(vms->fdt, nodename, "iommu-map",
1225 0x0, vms->iommu_phandle, 0x0, 0x10000);
1226 }
1227
4ab29b82
AG
1228 g_free(nodename);
1229}
1230
c8ef2bda 1231static void create_platform_bus(VirtMachineState *vms, qemu_irq *pic)
5f7a5a0e
EA
1232{
1233 DeviceState *dev;
1234 SysBusDevice *s;
1235 int i;
5f7a5a0e
EA
1236 MemoryRegion *sysmem = get_system_memory();
1237
5f7a5a0e
EA
1238 dev = qdev_create(NULL, TYPE_PLATFORM_BUS_DEVICE);
1239 dev->id = TYPE_PLATFORM_BUS_DEVICE;
3b77f6c3
IM
1240 qdev_prop_set_uint32(dev, "num_irqs", PLATFORM_BUS_NUM_IRQS);
1241 qdev_prop_set_uint32(dev, "mmio_size", vms->memmap[VIRT_PLATFORM_BUS].size);
5f7a5a0e 1242 qdev_init_nofail(dev);
a3fc8396 1243 vms->platform_bus_dev = dev;
5f7a5a0e 1244
3b77f6c3
IM
1245 s = SYS_BUS_DEVICE(dev);
1246 for (i = 0; i < PLATFORM_BUS_NUM_IRQS; i++) {
1247 int irqn = vms->irqmap[VIRT_PLATFORM_BUS] + i;
5f7a5a0e
EA
1248 sysbus_connect_irq(s, i, pic[irqn]);
1249 }
1250
1251 memory_region_add_subregion(sysmem,
3b77f6c3 1252 vms->memmap[VIRT_PLATFORM_BUS].base,
5f7a5a0e
EA
1253 sysbus_mmio_get_region(s, 0));
1254}
1255
c8ef2bda 1256static void create_secure_ram(VirtMachineState *vms,
9ac4ef77 1257 MemoryRegion *secure_sysmem)
83ec1923
PM
1258{
1259 MemoryRegion *secram = g_new(MemoryRegion, 1);
1260 char *nodename;
c8ef2bda
PM
1261 hwaddr base = vms->memmap[VIRT_SECURE_MEM].base;
1262 hwaddr size = vms->memmap[VIRT_SECURE_MEM].size;
83ec1923 1263
98a99ce0
PM
1264 memory_region_init_ram(secram, NULL, "virt.secure-ram", size,
1265 &error_fatal);
83ec1923
PM
1266 memory_region_add_subregion(secure_sysmem, base, secram);
1267
1268 nodename = g_strdup_printf("/secram@%" PRIx64, base);
c8ef2bda
PM
1269 qemu_fdt_add_subnode(vms->fdt, nodename);
1270 qemu_fdt_setprop_string(vms->fdt, nodename, "device_type", "memory");
1271 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg", 2, base, 2, size);
1272 qemu_fdt_setprop_string(vms->fdt, nodename, "status", "disabled");
1273 qemu_fdt_setprop_string(vms->fdt, nodename, "secure-status", "okay");
83ec1923
PM
1274
1275 g_free(nodename);
1276}
1277
f5fdcd6e
PM
1278static void *machvirt_dtb(const struct arm_boot_info *binfo, int *fdt_size)
1279{
9ac4ef77
PM
1280 const VirtMachineState *board = container_of(binfo, VirtMachineState,
1281 bootinfo);
f5fdcd6e
PM
1282
1283 *fdt_size = board->fdt_size;
1284 return board->fdt;
1285}
1286
e9a8e474 1287static void virt_build_smbios(VirtMachineState *vms)
c30e1565 1288{
dfadc3bf
WH
1289 MachineClass *mc = MACHINE_GET_CLASS(vms);
1290 VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms);
c30e1565
WH
1291 uint8_t *smbios_tables, *smbios_anchor;
1292 size_t smbios_tables_len, smbios_anchor_len;
bab27ea2 1293 const char *product = "QEMU Virtual Machine";
c30e1565 1294
af1f60a4 1295 if (!vms->fw_cfg) {
c30e1565
WH
1296 return;
1297 }
1298
bab27ea2
AJ
1299 if (kvm_enabled()) {
1300 product = "KVM Virtual Machine";
1301 }
1302
1303 smbios_set_defaults("QEMU", product,
dfadc3bf
WH
1304 vmc->smbios_old_sys_ver ? "1.0" : mc->name, false,
1305 true, SMBIOS_ENTRY_POINT_30);
c30e1565
WH
1306
1307 smbios_get_tables(NULL, 0, &smbios_tables, &smbios_tables_len,
1308 &smbios_anchor, &smbios_anchor_len);
1309
1310 if (smbios_anchor) {
af1f60a4 1311 fw_cfg_add_file(vms->fw_cfg, "etc/smbios/smbios-tables",
c30e1565 1312 smbios_tables, smbios_tables_len);
af1f60a4 1313 fw_cfg_add_file(vms->fw_cfg, "etc/smbios/smbios-anchor",
c30e1565
WH
1314 smbios_anchor, smbios_anchor_len);
1315 }
1316}
1317
d7c2e2db 1318static
054f4dc9 1319void virt_machine_done(Notifier *notifier, void *data)
d7c2e2db 1320{
054f4dc9
AJ
1321 VirtMachineState *vms = container_of(notifier, VirtMachineState,
1322 machine_done);
3b77f6c3
IM
1323 ARMCPU *cpu = ARM_CPU(first_cpu);
1324 struct arm_boot_info *info = &vms->bootinfo;
1325 AddressSpace *as = arm_boot_address_space(cpu, info);
1326
1327 /*
1328 * If the user provided a dtb, we assume the dynamic sysbus nodes
1329 * already are integrated there. This corresponds to a use case where
1330 * the dynamic sysbus nodes are complex and their generation is not yet
1331 * supported. In that case the user can take charge of the guest dt
1332 * while qemu takes charge of the qom stuff.
1333 */
1334 if (info->dtb_filename == NULL) {
1335 platform_bus_add_all_fdt_nodes(vms->fdt, "/intc",
1336 vms->memmap[VIRT_PLATFORM_BUS].base,
1337 vms->memmap[VIRT_PLATFORM_BUS].size,
1338 vms->irqmap[VIRT_PLATFORM_BUS]);
1339 }
1340 if (arm_load_dtb(info->dtb_start, info, info->dtb_limit, as) < 0) {
1341 exit(1);
1342 }
054f4dc9 1343
e9a8e474
AJ
1344 virt_acpi_setup(vms);
1345 virt_build_smbios(vms);
d7c2e2db
SZ
1346}
1347
46de5913
IM
1348static uint64_t virt_cpu_mp_affinity(VirtMachineState *vms, int idx)
1349{
1350 uint8_t clustersz = ARM_DEFAULT_CPUS_PER_CLUSTER;
1351 VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms);
1352
1353 if (!vmc->disallow_affinity_adjustment) {
1354 /* Adjust MPIDR like 64-bit KVM hosts, which incorporate the
1355 * GIC's target-list limitations. 32-bit KVM hosts currently
1356 * always create clusters of 4 CPUs, but that is expected to
1357 * change when they gain support for gicv3. When KVM is enabled
1358 * it will override the changes we make here, therefore our
1359 * purposes are to make TCG consistent (with 64-bit KVM hosts)
1360 * and to improve SGI efficiency.
1361 */
1362 if (vms->gic_version == 3) {
1363 clustersz = GICV3_TARGETLIST_BITS;
1364 } else {
1365 clustersz = GIC_TARGETLIST_BITS;
1366 }
1367 }
1368 return arm_cpu_mp_affinity(idx, clustersz);
1369}
1370
350a9c9e
EA
1371static void virt_set_memmap(VirtMachineState *vms)
1372{
1373 hwaddr base;
1374 int i;
1375
1376 vms->memmap = extended_memmap;
1377
1378 for (i = 0; i < ARRAY_SIZE(base_memmap); i++) {
1379 vms->memmap[i] = base_memmap[i];
1380 }
1381
1382 base = 256 * GiB; /* Top of the legacy initial RAM region */
1383
1384 for (i = VIRT_LOWMEMMAP_LAST; i < ARRAY_SIZE(extended_memmap); i++) {
1385 hwaddr size = extended_memmap[i].size;
1386
1387 base = ROUND_UP(base, size);
1388 vms->memmap[i].base = base;
1389 vms->memmap[i].size = size;
1390 base += size;
1391 }
1392}
1393
3ef96221 1394static void machvirt_init(MachineState *machine)
f5fdcd6e 1395{
e5a5604f 1396 VirtMachineState *vms = VIRT_MACHINE(machine);
95eb49c8 1397 VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(machine);
17d3d0e2
IM
1398 MachineClass *mc = MACHINE_GET_CLASS(machine);
1399 const CPUArchIdList *possible_cpus;
f5fdcd6e
PM
1400 qemu_irq pic[NUM_IRQS];
1401 MemoryRegion *sysmem = get_system_memory();
3df708eb 1402 MemoryRegion *secure_sysmem = NULL;
7ea686f5 1403 int n, virt_max_cpus;
f5fdcd6e 1404 MemoryRegion *ram = g_new(MemoryRegion, 1);
4824a61a 1405 bool firmware_loaded = bios_name || drive_get(IF_PFLASH, 0, 0);
17ec075a 1406 bool aarch64 = true;
f5fdcd6e 1407
350a9c9e
EA
1408 virt_set_memmap(vms);
1409
b92ad394
PF
1410 /* We can probe only here because during property set
1411 * KVM is not available yet
1412 */
dc16538a
PM
1413 if (vms->gic_version <= 0) {
1414 /* "host" or "max" */
0bf8039d 1415 if (!kvm_enabled()) {
dc16538a
PM
1416 if (vms->gic_version == 0) {
1417 error_report("gic-version=host requires KVM");
1418 exit(1);
1419 } else {
1420 /* "max": currently means 3 for TCG */
1421 vms->gic_version = 3;
1422 }
1423 } else {
1424 vms->gic_version = kvm_arm_vgic_probe();
1425 if (!vms->gic_version) {
1426 error_report(
1427 "Unable to determine GIC version supported by host");
1428 exit(1);
1429 }
b92ad394
PF
1430 }
1431 }
1432
ba1ba5cc
IM
1433 if (!cpu_type_valid(machine->cpu_type)) {
1434 error_report("mach-virt: CPU type %s not supported", machine->cpu_type);
f5fdcd6e
PM
1435 exit(1);
1436 }
1437
4824a61a
PM
1438 /* If we have an EL3 boot ROM then the assumption is that it will
1439 * implement PSCI itself, so disable QEMU's internal implementation
1440 * so it doesn't get in the way. Instead of starting secondary
1441 * CPUs in PSCI powerdown state we will start them all running and
1442 * let the boot ROM sort them out.
f29cacfb
PM
1443 * The usual case is that we do use QEMU's PSCI implementation;
1444 * if the guest has EL2 then we will use SMC as the conduit,
1445 * and otherwise we will use HVC (for backwards compatibility and
1446 * because if we're using KVM then we must use HVC).
4824a61a 1447 */
2013c566
PM
1448 if (vms->secure && firmware_loaded) {
1449 vms->psci_conduit = QEMU_PSCI_CONDUIT_DISABLED;
f29cacfb
PM
1450 } else if (vms->virt) {
1451 vms->psci_conduit = QEMU_PSCI_CONDUIT_SMC;
2013c566
PM
1452 } else {
1453 vms->psci_conduit = QEMU_PSCI_CONDUIT_HVC;
1454 }
4824a61a 1455
4b280b72
AJ
1456 /* The maximum number of CPUs depends on the GIC version, or on how
1457 * many redistributors we can fit into the memory map.
1458 */
055a7f2b 1459 if (vms->gic_version == 3) {
bf424a12
EA
1460 virt_max_cpus =
1461 vms->memmap[VIRT_GIC_REDIST].size / GICV3_REDIST_SIZE;
1462 virt_max_cpus +=
1463 vms->memmap[VIRT_HIGH_GIC_REDIST2].size / GICV3_REDIST_SIZE;
4b280b72 1464 } else {
7ea686f5 1465 virt_max_cpus = GIC_NCPU;
4b280b72
AJ
1466 }
1467
7ea686f5 1468 if (max_cpus > virt_max_cpus) {
4b280b72
AJ
1469 error_report("Number of SMP CPUs requested (%d) exceeds max CPUs "
1470 "supported by machine 'mach-virt' (%d)",
7ea686f5 1471 max_cpus, virt_max_cpus);
4b280b72
AJ
1472 exit(1);
1473 }
1474
c8ef2bda 1475 vms->smp_cpus = smp_cpus;
f5fdcd6e 1476
c8ef2bda 1477 if (machine->ram_size > vms->memmap[VIRT_MEM].size) {
71c27684 1478 error_report("mach-virt: cannot model more than %dGB RAM", RAMLIMIT_GB);
f5fdcd6e
PM
1479 exit(1);
1480 }
1481
f29cacfb
PM
1482 if (vms->virt && kvm_enabled()) {
1483 error_report("mach-virt: KVM does not support providing "
1484 "Virtualization extensions to the guest CPU");
1485 exit(1);
1486 }
1487
3df708eb
PM
1488 if (vms->secure) {
1489 if (kvm_enabled()) {
1490 error_report("mach-virt: KVM does not support Security extensions");
1491 exit(1);
1492 }
1493
1494 /* The Secure view of the world is the same as the NonSecure,
1495 * but with a few extra devices. Create it as a container region
1496 * containing the system memory at low priority; any secure-only
1497 * devices go in at higher priority and take precedence.
1498 */
1499 secure_sysmem = g_new(MemoryRegion, 1);
1500 memory_region_init(secure_sysmem, OBJECT(machine), "secure-memory",
1501 UINT64_MAX);
1502 memory_region_add_subregion_overlap(secure_sysmem, 0, sysmem, -1);
1503 }
1504
c8ef2bda 1505 create_fdt(vms);
f5fdcd6e 1506
17d3d0e2
IM
1507 possible_cpus = mc->possible_cpu_arch_ids(machine);
1508 for (n = 0; n < possible_cpus->len; n++) {
1509 Object *cpuobj;
d9c34f9c 1510 CPUState *cs;
46de5913 1511
17d3d0e2
IM
1512 if (n >= smp_cpus) {
1513 break;
1514 }
1515
d342eb76 1516 cpuobj = object_new(possible_cpus->cpus[n].type);
17d3d0e2 1517 object_property_set_int(cpuobj, possible_cpus->cpus[n].arch_id,
46de5913 1518 "mp-affinity", NULL);
f313369f 1519
d9c34f9c
IM
1520 cs = CPU(cpuobj);
1521 cs->cpu_index = n;
1522
a0ceb640
IM
1523 numa_cpu_pre_plug(&possible_cpus->cpus[cs->cpu_index], DEVICE(cpuobj),
1524 &error_fatal);
bd4c1bfe 1525
17ec075a
EA
1526 aarch64 &= object_property_get_bool(cpuobj, "aarch64", NULL);
1527
e5a5604f
GB
1528 if (!vms->secure) {
1529 object_property_set_bool(cpuobj, false, "has_el3", NULL);
1530 }
1531
f29cacfb 1532 if (!vms->virt && object_property_find(cpuobj, "has_el2", NULL)) {
c25bd18a
PM
1533 object_property_set_bool(cpuobj, false, "has_el2", NULL);
1534 }
1535
2013c566
PM
1536 if (vms->psci_conduit != QEMU_PSCI_CONDUIT_DISABLED) {
1537 object_property_set_int(cpuobj, vms->psci_conduit,
4824a61a 1538 "psci-conduit", NULL);
211b0169 1539
4824a61a
PM
1540 /* Secondary CPUs start in PSCI powered-down state */
1541 if (n > 0) {
1542 object_property_set_bool(cpuobj, true,
1543 "start-powered-off", NULL);
1544 }
f5fdcd6e 1545 }
ba750085 1546
1141d1eb
WH
1547 if (vmc->no_pmu && object_property_find(cpuobj, "pmu", NULL)) {
1548 object_property_set_bool(cpuobj, false, "pmu", NULL);
1549 }
1550
ba750085 1551 if (object_property_find(cpuobj, "reset-cbar", NULL)) {
c8ef2bda 1552 object_property_set_int(cpuobj, vms->memmap[VIRT_CPUPERIPHS].base,
ba750085
PM
1553 "reset-cbar", &error_abort);
1554 }
1555
1d939a68
PM
1556 object_property_set_link(cpuobj, OBJECT(sysmem), "memory",
1557 &error_abort);
3df708eb
PM
1558 if (vms->secure) {
1559 object_property_set_link(cpuobj, OBJECT(secure_sysmem),
1560 "secure-memory", &error_abort);
1561 }
1d939a68 1562
c88bc3e0 1563 object_property_set_bool(cpuobj, true, "realized", &error_fatal);
dbb74759 1564 object_unref(cpuobj);
f5fdcd6e 1565 }
055a7f2b 1566 fdt_add_timer_nodes(vms);
c8ef2bda 1567 fdt_add_cpu_nodes(vms);
f5fdcd6e 1568
c8623c02
DM
1569 memory_region_allocate_system_memory(ram, NULL, "mach-virt.ram",
1570 machine->ram_size);
c8ef2bda 1571 memory_region_add_subregion(sysmem, vms->memmap[VIRT_MEM].base, ram);
f5fdcd6e 1572
c8ef2bda 1573 create_flash(vms, sysmem, secure_sysmem ? secure_sysmem : sysmem);
acf82361 1574
055a7f2b 1575 create_gic(vms, pic);
f5fdcd6e 1576
055a7f2b 1577 fdt_add_pmu_nodes(vms);
01fe6b60 1578
9bca0edb 1579 create_uart(vms, pic, VIRT_UART, sysmem, serial_hd(0));
3df708eb
PM
1580
1581 if (vms->secure) {
c8ef2bda 1582 create_secure_ram(vms, secure_sysmem);
9bca0edb 1583 create_uart(vms, pic, VIRT_SECURE_UART, secure_sysmem, serial_hd(1));
3df708eb 1584 }
f5fdcd6e 1585
17ec075a
EA
1586 vms->highmem_ecam &= vms->highmem && (!firmware_loaded || aarch64);
1587
c8ef2bda 1588 create_rtc(vms, pic);
6e411af9 1589
0127937b 1590 create_pcie(vms, pic);
4ab29b82 1591
c8ef2bda 1592 create_gpio(vms, pic);
b0a3721e 1593
f5fdcd6e
PM
1594 /* Create mmio transports, so the user can create virtio backends
1595 * (which will be automatically plugged in to the transports). If
1596 * no backend is created the transport will just sit harmlessly idle.
1597 */
c8ef2bda 1598 create_virtio_devices(vms, pic);
f5fdcd6e 1599
af1f60a4
AJ
1600 vms->fw_cfg = create_fw_cfg(vms, &address_space_memory);
1601 rom_set_fw(vms->fw_cfg);
d7c2e2db 1602
3b77f6c3 1603 create_platform_bus(vms, pic);
578f3c7b 1604
c8ef2bda
PM
1605 vms->bootinfo.ram_size = machine->ram_size;
1606 vms->bootinfo.kernel_filename = machine->kernel_filename;
1607 vms->bootinfo.kernel_cmdline = machine->kernel_cmdline;
1608 vms->bootinfo.initrd_filename = machine->initrd_filename;
1609 vms->bootinfo.nb_cpus = smp_cpus;
1610 vms->bootinfo.board_id = -1;
1611 vms->bootinfo.loader_start = vms->memmap[VIRT_MEM].base;
1612 vms->bootinfo.get_dtb = machvirt_dtb;
3b77f6c3 1613 vms->bootinfo.skip_dtb_autoload = true;
c8ef2bda
PM
1614 vms->bootinfo.firmware_loaded = firmware_loaded;
1615 arm_load_kernel(ARM_CPU(first_cpu), &vms->bootinfo);
5f7a5a0e 1616
3b77f6c3
IM
1617 vms->machine_done.notify = virt_machine_done;
1618 qemu_add_machine_init_done_notifier(&vms->machine_done);
f5fdcd6e
PM
1619}
1620
083a5890
GB
1621static bool virt_get_secure(Object *obj, Error **errp)
1622{
1623 VirtMachineState *vms = VIRT_MACHINE(obj);
1624
1625 return vms->secure;
1626}
1627
1628static void virt_set_secure(Object *obj, bool value, Error **errp)
1629{
1630 VirtMachineState *vms = VIRT_MACHINE(obj);
1631
1632 vms->secure = value;
1633}
1634
f29cacfb
PM
1635static bool virt_get_virt(Object *obj, Error **errp)
1636{
1637 VirtMachineState *vms = VIRT_MACHINE(obj);
1638
1639 return vms->virt;
1640}
1641
1642static void virt_set_virt(Object *obj, bool value, Error **errp)
1643{
1644 VirtMachineState *vms = VIRT_MACHINE(obj);
1645
1646 vms->virt = value;
1647}
1648
5125f9cd
PF
1649static bool virt_get_highmem(Object *obj, Error **errp)
1650{
1651 VirtMachineState *vms = VIRT_MACHINE(obj);
1652
1653 return vms->highmem;
1654}
1655
1656static void virt_set_highmem(Object *obj, bool value, Error **errp)
1657{
1658 VirtMachineState *vms = VIRT_MACHINE(obj);
1659
1660 vms->highmem = value;
1661}
1662
ccc11b02
EA
1663static bool virt_get_its(Object *obj, Error **errp)
1664{
1665 VirtMachineState *vms = VIRT_MACHINE(obj);
1666
1667 return vms->its;
1668}
1669
1670static void virt_set_its(Object *obj, bool value, Error **errp)
1671{
1672 VirtMachineState *vms = VIRT_MACHINE(obj);
1673
1674 vms->its = value;
1675}
1676
b92ad394
PF
1677static char *virt_get_gic_version(Object *obj, Error **errp)
1678{
1679 VirtMachineState *vms = VIRT_MACHINE(obj);
1680 const char *val = vms->gic_version == 3 ? "3" : "2";
1681
1682 return g_strdup(val);
1683}
1684
1685static void virt_set_gic_version(Object *obj, const char *value, Error **errp)
1686{
1687 VirtMachineState *vms = VIRT_MACHINE(obj);
1688
1689 if (!strcmp(value, "3")) {
1690 vms->gic_version = 3;
1691 } else if (!strcmp(value, "2")) {
1692 vms->gic_version = 2;
1693 } else if (!strcmp(value, "host")) {
1694 vms->gic_version = 0; /* Will probe later */
dc16538a
PM
1695 } else if (!strcmp(value, "max")) {
1696 vms->gic_version = -1; /* Will probe later */
b92ad394 1697 } else {
7b55044f 1698 error_setg(errp, "Invalid gic-version value");
dc16538a 1699 error_append_hint(errp, "Valid values are 3, 2, host, max.\n");
b92ad394
PF
1700 }
1701}
1702
e24e3454
EA
1703static char *virt_get_iommu(Object *obj, Error **errp)
1704{
1705 VirtMachineState *vms = VIRT_MACHINE(obj);
1706
1707 switch (vms->iommu) {
1708 case VIRT_IOMMU_NONE:
1709 return g_strdup("none");
1710 case VIRT_IOMMU_SMMUV3:
1711 return g_strdup("smmuv3");
1712 default:
1713 g_assert_not_reached();
1714 }
1715}
1716
1717static void virt_set_iommu(Object *obj, const char *value, Error **errp)
1718{
1719 VirtMachineState *vms = VIRT_MACHINE(obj);
1720
1721 if (!strcmp(value, "smmuv3")) {
1722 vms->iommu = VIRT_IOMMU_SMMUV3;
1723 } else if (!strcmp(value, "none")) {
1724 vms->iommu = VIRT_IOMMU_NONE;
1725 } else {
1726 error_setg(errp, "Invalid iommu value");
1727 error_append_hint(errp, "Valid values are none, smmuv3.\n");
1728 }
1729}
1730
ea089eeb
IM
1731static CpuInstanceProperties
1732virt_cpu_index_to_props(MachineState *ms, unsigned cpu_index)
1733{
1734 MachineClass *mc = MACHINE_GET_CLASS(ms);
1735 const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(ms);
1736
1737 assert(cpu_index < possible_cpus->len);
1738 return possible_cpus->cpus[cpu_index].props;
1739}
1740
79e07936
IM
1741static int64_t virt_get_default_cpu_node_id(const MachineState *ms, int idx)
1742{
1743 return idx % nb_numa_nodes;
1744}
1745
17d3d0e2
IM
1746static const CPUArchIdList *virt_possible_cpu_arch_ids(MachineState *ms)
1747{
1748 int n;
1749 VirtMachineState *vms = VIRT_MACHINE(ms);
1750
1751 if (ms->possible_cpus) {
1752 assert(ms->possible_cpus->len == max_cpus);
1753 return ms->possible_cpus;
1754 }
1755
1756 ms->possible_cpus = g_malloc0(sizeof(CPUArchIdList) +
1757 sizeof(CPUArchId) * max_cpus);
1758 ms->possible_cpus->len = max_cpus;
1759 for (n = 0; n < ms->possible_cpus->len; n++) {
d342eb76 1760 ms->possible_cpus->cpus[n].type = ms->cpu_type;
17d3d0e2
IM
1761 ms->possible_cpus->cpus[n].arch_id =
1762 virt_cpu_mp_affinity(vms, n);
1763 ms->possible_cpus->cpus[n].props.has_thread_id = true;
1764 ms->possible_cpus->cpus[n].props.thread_id = n;
17d3d0e2
IM
1765 }
1766 return ms->possible_cpus;
1767}
1768
a3fc8396
IM
1769static void virt_machine_device_plug_cb(HotplugHandler *hotplug_dev,
1770 DeviceState *dev, Error **errp)
1771{
1772 VirtMachineState *vms = VIRT_MACHINE(hotplug_dev);
1773
1774 if (vms->platform_bus_dev) {
1775 if (object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE)) {
1776 platform_bus_link_device(PLATFORM_BUS_DEVICE(vms->platform_bus_dev),
1777 SYS_BUS_DEVICE(dev));
1778 }
1779 }
1780}
1781
1782static HotplugHandler *virt_machine_get_hotplug_handler(MachineState *machine,
1783 DeviceState *dev)
1784{
1785 if (object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE)) {
1786 return HOTPLUG_HANDLER(machine);
1787 }
1788
1789 return NULL;
1790}
1791
ed796373
WH
1792static void virt_machine_class_init(ObjectClass *oc, void *data)
1793{
9c94d8e6 1794 MachineClass *mc = MACHINE_CLASS(oc);
a3fc8396 1795 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
9c94d8e6
WH
1796
1797 mc->init = machvirt_init;
b10fbd53
EA
1798 /* Start with max_cpus set to 512, which is the maximum supported by KVM.
1799 * The value may be reduced later when we have more information about the
9c94d8e6
WH
1800 * configuration of the particular instance.
1801 */
b10fbd53 1802 mc->max_cpus = 512;
6f2062b9
EH
1803 machine_class_allow_dynamic_sysbus_dev(mc, TYPE_VFIO_CALXEDA_XGMAC);
1804 machine_class_allow_dynamic_sysbus_dev(mc, TYPE_VFIO_AMD_XGBE);
94692dcd 1805 machine_class_allow_dynamic_sysbus_dev(mc, TYPE_RAMFB_DEVICE);
4ebc0b61 1806 machine_class_allow_dynamic_sysbus_dev(mc, TYPE_VFIO_PLATFORM);
9c94d8e6
WH
1807 mc->block_default_type = IF_VIRTIO;
1808 mc->no_cdrom = 1;
1809 mc->pci_allow_0_address = true;
a2519ad1
PM
1810 /* We know we will never create a pre-ARMv7 CPU which needs 1K pages */
1811 mc->minimum_page_bits = 12;
17d3d0e2 1812 mc->possible_cpu_arch_ids = virt_possible_cpu_arch_ids;
ea089eeb 1813 mc->cpu_index_to_instance_props = virt_cpu_index_to_props;
ba1ba5cc 1814 mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-a15");
79e07936 1815 mc->get_default_cpu_node_id = virt_get_default_cpu_node_id;
debbdc00 1816 assert(!mc->get_hotplug_handler);
a3fc8396
IM
1817 mc->get_hotplug_handler = virt_machine_get_hotplug_handler;
1818 hc->plug = virt_machine_device_plug_cb;
ed796373
WH
1819}
1820
95159760 1821static void virt_instance_init(Object *obj)
083a5890
GB
1822{
1823 VirtMachineState *vms = VIRT_MACHINE(obj);
ccc11b02 1824 VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms);
083a5890 1825
2d710006
PM
1826 /* EL3 is disabled by default on virt: this makes us consistent
1827 * between KVM and TCG for this board, and it also allows us to
1828 * boot UEFI blobs which assume no TrustZone support.
1829 */
1830 vms->secure = false;
083a5890
GB
1831 object_property_add_bool(obj, "secure", virt_get_secure,
1832 virt_set_secure, NULL);
1833 object_property_set_description(obj, "secure",
1834 "Set on/off to enable/disable the ARM "
1835 "Security Extensions (TrustZone)",
1836 NULL);
5125f9cd 1837
f29cacfb
PM
1838 /* EL2 is also disabled by default, for similar reasons */
1839 vms->virt = false;
1840 object_property_add_bool(obj, "virtualization", virt_get_virt,
1841 virt_set_virt, NULL);
1842 object_property_set_description(obj, "virtualization",
1843 "Set on/off to enable/disable emulating a "
1844 "guest CPU which implements the ARM "
1845 "Virtualization Extensions",
1846 NULL);
1847
5125f9cd
PF
1848 /* High memory is enabled by default */
1849 vms->highmem = true;
1850 object_property_add_bool(obj, "highmem", virt_get_highmem,
1851 virt_set_highmem, NULL);
1852 object_property_set_description(obj, "highmem",
1853 "Set on/off to enable/disable using "
1854 "physical address space above 32 bits",
1855 NULL);
b92ad394
PF
1856 /* Default GIC type is v2 */
1857 vms->gic_version = 2;
1858 object_property_add_str(obj, "gic-version", virt_get_gic_version,
1859 virt_set_gic_version, NULL);
1860 object_property_set_description(obj, "gic-version",
1861 "Set GIC version. "
1862 "Valid values are 2, 3 and host", NULL);
9ac4ef77 1863
17ec075a
EA
1864 vms->highmem_ecam = !vmc->no_highmem_ecam;
1865
ccc11b02
EA
1866 if (vmc->no_its) {
1867 vms->its = false;
1868 } else {
1869 /* Default allows ITS instantiation */
1870 vms->its = true;
1871 object_property_add_bool(obj, "its", virt_get_its,
1872 virt_set_its, NULL);
1873 object_property_set_description(obj, "its",
1874 "Set on/off to enable/disable "
1875 "ITS instantiation",
1876 NULL);
1877 }
1878
e24e3454
EA
1879 /* Default disallows iommu instantiation */
1880 vms->iommu = VIRT_IOMMU_NONE;
1881 object_property_add_str(obj, "iommu", virt_get_iommu, virt_set_iommu, NULL);
1882 object_property_set_description(obj, "iommu",
1883 "Set the IOMMU type. "
1884 "Valid values are none and smmuv3",
1885 NULL);
1886
9ac4ef77 1887 vms->irqmap = a15irqmap;
083a5890
GB
1888}
1889
95159760
EH
1890static const TypeInfo virt_machine_info = {
1891 .name = TYPE_VIRT_MACHINE,
1892 .parent = TYPE_MACHINE,
1893 .abstract = true,
1894 .instance_size = sizeof(VirtMachineState),
1895 .class_size = sizeof(VirtMachineClass),
1896 .class_init = virt_machine_class_init,
bbac02f1 1897 .instance_init = virt_instance_init,
95159760
EH
1898 .interfaces = (InterfaceInfo[]) {
1899 { TYPE_HOTPLUG_HANDLER },
1900 { }
1901 },
1902};
1903
1904static void machvirt_machine_init(void)
1905{
1906 type_register_static(&virt_machine_info);
1907}
1908type_init(machvirt_machine_init);
1909
84e060bf
AW
1910static void virt_machine_4_0_options(MachineClass *mc)
1911{
1912}
1913DEFINE_VIRT_MACHINE_AS_LATEST(4, 0)
1914
22907d2b
AJ
1915static void virt_machine_3_1_options(MachineClass *mc)
1916{
84e060bf 1917 virt_machine_4_0_options(mc);
abd93cc7 1918 compat_props_add(mc->compat_props, hw_compat_3_1, hw_compat_3_1_len);
22907d2b 1919}
84e060bf 1920DEFINE_VIRT_MACHINE(3, 1)
22907d2b 1921
8ae9a1ca
EA
1922static void virt_machine_3_0_options(MachineClass *mc)
1923{
22907d2b 1924 virt_machine_3_1_options(mc);
ddb3235d 1925 compat_props_add(mc->compat_props, hw_compat_3_0, hw_compat_3_0_len);
8ae9a1ca 1926}
22907d2b
AJ
1927DEFINE_VIRT_MACHINE(3, 0)
1928
a2a05159
PM
1929static void virt_machine_2_12_options(MachineClass *mc)
1930{
17ec075a
EA
1931 VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
1932
8ae9a1ca 1933 virt_machine_3_0_options(mc);
0d47310b 1934 compat_props_add(mc->compat_props, hw_compat_2_12, hw_compat_2_12_len);
17ec075a 1935 vmc->no_highmem_ecam = true;
b10fbd53 1936 mc->max_cpus = 255;
a2a05159 1937}
8ae9a1ca 1938DEFINE_VIRT_MACHINE(2, 12)
a2a05159 1939
79283dda
EA
1940static void virt_machine_2_11_options(MachineClass *mc)
1941{
dfadc3bf
WH
1942 VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
1943
a2a05159 1944 virt_machine_2_12_options(mc);
43df70a9 1945 compat_props_add(mc->compat_props, hw_compat_2_11, hw_compat_2_11_len);
dfadc3bf 1946 vmc->smbios_old_sys_ver = true;
79283dda 1947}
a2a05159 1948DEFINE_VIRT_MACHINE(2, 11)
79283dda 1949
f22ab6cb
EA
1950static void virt_machine_2_10_options(MachineClass *mc)
1951{
79283dda 1952 virt_machine_2_11_options(mc);
503224f4 1953 compat_props_add(mc->compat_props, hw_compat_2_10, hw_compat_2_10_len);
846690de
PM
1954 /* before 2.11 we never faulted accesses to bad addresses */
1955 mc->ignore_memory_transaction_failures = true;
f22ab6cb 1956}
79283dda 1957DEFINE_VIRT_MACHINE(2, 10)
f22ab6cb 1958
e353aac5
PM
1959static void virt_machine_2_9_options(MachineClass *mc)
1960{
f22ab6cb 1961 virt_machine_2_10_options(mc);
3e803152 1962 compat_props_add(mc->compat_props, hw_compat_2_9, hw_compat_2_9_len);
e353aac5 1963}
f22ab6cb 1964DEFINE_VIRT_MACHINE(2, 9)
e353aac5 1965
96b0439b
AJ
1966static void virt_machine_2_8_options(MachineClass *mc)
1967{
156bc9a5
PM
1968 VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
1969
e353aac5 1970 virt_machine_2_9_options(mc);
edc24ccd 1971 compat_props_add(mc->compat_props, hw_compat_2_8, hw_compat_2_8_len);
156bc9a5
PM
1972 /* For 2.8 and earlier we falsely claimed in the DT that
1973 * our timers were edge-triggered, not level-triggered.
1974 */
1975 vmc->claim_edge_triggered_timers = true;
96b0439b 1976}
e353aac5 1977DEFINE_VIRT_MACHINE(2, 8)
96b0439b 1978
1287f2b3
AJ
1979static void virt_machine_2_7_options(MachineClass *mc)
1980{
2231f69b
AJ
1981 VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
1982
96b0439b 1983 virt_machine_2_8_options(mc);
5a995064 1984 compat_props_add(mc->compat_props, hw_compat_2_7, hw_compat_2_7_len);
2231f69b
AJ
1985 /* ITS was introduced with 2.8 */
1986 vmc->no_its = true;
a2519ad1
PM
1987 /* Stick with 1K pages for migration compatibility */
1988 mc->minimum_page_bits = 0;
1287f2b3 1989}
96b0439b 1990DEFINE_VIRT_MACHINE(2, 7)
1287f2b3 1991
ab093c3c 1992static void virt_machine_2_6_options(MachineClass *mc)
c2919690 1993{
95eb49c8
AJ
1994 VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
1995
1287f2b3 1996 virt_machine_2_7_options(mc);
ff8f261f 1997 compat_props_add(mc->compat_props, hw_compat_2_6, hw_compat_2_6_len);
95eb49c8 1998 vmc->disallow_affinity_adjustment = true;
1141d1eb
WH
1999 /* Disable PMU for 2.6 as PMU support was first introduced in 2.7 */
2000 vmc->no_pmu = true;
c2919690 2001}
1287f2b3 2002DEFINE_VIRT_MACHINE(2, 6)
This page took 0.643683 seconds and 4 git commands to generate.