]> git.proxmox.com Git - mirror_qemu.git/blame - hw/arm/virt.c
qom: Use return values to check for error where that's simpler
[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"
a8d25326 32#include "qemu-common.h"
350a9c9e 33#include "qemu/units.h"
e0561e60 34#include "qemu/option.h"
70e89132 35#include "monitor/qdev.h"
da34e65c 36#include "qapi/error.h"
f5fdcd6e 37#include "hw/sysbus.h"
12e9493d 38#include "hw/boards.h"
12ec8bd5 39#include "hw/arm/boot.h"
f5fdcd6e 40#include "hw/arm/primecell.h"
afe0b380 41#include "hw/arm/virt.h"
81c7db72 42#include "hw/block/flash.h"
6f2062b9
EH
43#include "hw/vfio/vfio-calxeda-xgmac.h"
44#include "hw/vfio/vfio-amd-xgbe.h"
94692dcd 45#include "hw/display/ramfb.h"
f5fdcd6e
PM
46#include "net/net.h"
47#include "sysemu/device_tree.h"
9695200a 48#include "sysemu/numa.h"
54d31236 49#include "sysemu/runstate.h"
f5fdcd6e 50#include "sysemu/sysemu.h"
c294ac32 51#include "sysemu/tpm.h"
f5fdcd6e 52#include "sysemu/kvm.h"
acf82361 53#include "hw/loader.h"
f5fdcd6e
PM
54#include "exec/address-spaces.h"
55#include "qemu/bitops.h"
56#include "qemu/error-report.h"
0b8fa32f 57#include "qemu/module.h"
4ab29b82 58#include "hw/pci-host/gpex.h"
70e89132 59#include "hw/virtio/virtio-pci.h"
5f7a5a0e
EA
60#include "hw/arm/sysbus-fdt.h"
61#include "hw/platform-bus.h"
a27bd6c7 62#include "hw/qdev-properties.h"
decf4f80 63#include "hw/arm/fdt.h"
95eb49c8
AJ
64#include "hw/intc/arm_gic.h"
65#include "hw/intc/arm_gicv3_common.h"
64552b6b 66#include "hw/irq.h"
e6fbcbc4 67#include "kvm_arm.h"
a2eb5c0c 68#include "hw/firmware/smbios.h"
b92ad394 69#include "qapi/visitor.h"
17e89077 70#include "qapi/qapi-visit-common.h"
3e6ebb64 71#include "standard-headers/linux/input.h"
584105ea 72#include "hw/arm/smmuv3.h"
957e32cf 73#include "hw/acpi/acpi.h"
2ba956cc 74#include "target/arm/internals.h"
1f283ae1
EA
75#include "hw/mem/pc-dimm.h"
76#include "hw/mem/nvdimm.h"
cff51ac9 77#include "hw/acpi/generic_event_device.h"
70e89132 78#include "hw/virtio/virtio-iommu.h"
d8f6d15f 79#include "hw/char/pl011.h"
60592cfe 80#include "qemu/guest-random.h"
f5fdcd6e 81
3356ebce 82#define DEFINE_VIRT_MACHINE_LATEST(major, minor, latest) \
ab093c3c
AJ
83 static void virt_##major##_##minor##_class_init(ObjectClass *oc, \
84 void *data) \
85 { \
86 MachineClass *mc = MACHINE_CLASS(oc); \
87 virt_machine_##major##_##minor##_options(mc); \
88 mc->desc = "QEMU " # major "." # minor " ARM Virtual Machine"; \
3356ebce
AJ
89 if (latest) { \
90 mc->alias = "virt"; \
91 } \
ab093c3c
AJ
92 } \
93 static const TypeInfo machvirt_##major##_##minor##_info = { \
94 .name = MACHINE_TYPE_NAME("virt-" # major "." # minor), \
95 .parent = TYPE_VIRT_MACHINE, \
ab093c3c
AJ
96 .class_init = virt_##major##_##minor##_class_init, \
97 }; \
98 static void machvirt_machine_##major##_##minor##_init(void) \
99 { \
100 type_register_static(&machvirt_##major##_##minor##_info); \
101 } \
102 type_init(machvirt_machine_##major##_##minor##_init);
103
3356ebce
AJ
104#define DEFINE_VIRT_MACHINE_AS_LATEST(major, minor) \
105 DEFINE_VIRT_MACHINE_LATEST(major, minor, true)
106#define DEFINE_VIRT_MACHINE(major, minor) \
107 DEFINE_VIRT_MACHINE_LATEST(major, minor, false)
108
ab093c3c 109
a72d4363
AJ
110/* Number of external interrupt lines to configure the GIC with */
111#define NUM_IRQS 256
112
113#define PLATFORM_BUS_NUM_IRQS 64
114
50a17297 115/* Legacy RAM limit in GB (< version 4.0) */
957e32cf
EA
116#define LEGACY_RAMLIMIT_GB 255
117#define LEGACY_RAMLIMIT_BYTES (LEGACY_RAMLIMIT_GB * GiB)
71c27684 118
f5fdcd6e
PM
119/* Addresses and sizes of our components.
120 * 0..128MB is space for a flash device so we can run bootrom code such as UEFI.
121 * 128MB..256MB is used for miscellaneous device I/O.
122 * 256MB..1GB is reserved for possible future PCI support (ie where the
123 * PCI memory window will go if we add a PCI host controller).
124 * 1GB and up is RAM (which may happily spill over into the
125 * high memory region beyond 4GB).
126 * This represents a compromise between how much RAM can be given to
127 * a 32 bit VM and leaving space for expansion and in particular for PCI.
6e411af9
PM
128 * Note that devices should generally be placed at multiples of 0x10000,
129 * to accommodate guests using 64K pages.
f5fdcd6e 130 */
350a9c9e 131static const MemMapEntry base_memmap[] = {
f5fdcd6e 132 /* Space up to 0x8000000 is reserved for a boot ROM */
94edf02c
EA
133 [VIRT_FLASH] = { 0, 0x08000000 },
134 [VIRT_CPUPERIPHS] = { 0x08000000, 0x00020000 },
f5fdcd6e 135 /* GIC distributor and CPU interfaces sit inside the CPU peripheral space */
94edf02c
EA
136 [VIRT_GIC_DIST] = { 0x08000000, 0x00010000 },
137 [VIRT_GIC_CPU] = { 0x08010000, 0x00010000 },
138 [VIRT_GIC_V2M] = { 0x08020000, 0x00001000 },
55ef3233
LM
139 [VIRT_GIC_HYP] = { 0x08030000, 0x00010000 },
140 [VIRT_GIC_VCPU] = { 0x08040000, 0x00010000 },
b92ad394
PF
141 /* The space in between here is reserved for GICv3 CPU/vCPU/HYP */
142 [VIRT_GIC_ITS] = { 0x08080000, 0x00020000 },
143 /* This redistributor space allows up to 2*64kB*123 CPUs */
144 [VIRT_GIC_REDIST] = { 0x080A0000, 0x00F60000 },
94edf02c
EA
145 [VIRT_UART] = { 0x09000000, 0x00001000 },
146 [VIRT_RTC] = { 0x09010000, 0x00001000 },
0b341a85 147 [VIRT_FW_CFG] = { 0x09020000, 0x00000018 },
b0a3721e 148 [VIRT_GPIO] = { 0x09030000, 0x00001000 },
3df708eb 149 [VIRT_SECURE_UART] = { 0x09040000, 0x00001000 },
584105ea 150 [VIRT_SMMU] = { 0x09050000, 0x00020000 },
cff51ac9
SK
151 [VIRT_PCDIMM_ACPI] = { 0x09070000, MEMORY_HOTPLUG_IO_LEN },
152 [VIRT_ACPI_GED] = { 0x09080000, ACPI_GED_EVT_SEL_LEN },
b5a60bee 153 [VIRT_NVDIMM_ACPI] = { 0x09090000, NVDIMM_ACPI_IO_LEN},
94edf02c 154 [VIRT_MMIO] = { 0x0a000000, 0x00000200 },
f5fdcd6e 155 /* ...repeating for a total of NUM_VIRTIO_TRANSPORTS, each of that size */
94edf02c 156 [VIRT_PLATFORM_BUS] = { 0x0c000000, 0x02000000 },
83ec1923 157 [VIRT_SECURE_MEM] = { 0x0e000000, 0x01000000 },
94edf02c
EA
158 [VIRT_PCIE_MMIO] = { 0x10000000, 0x2eff0000 },
159 [VIRT_PCIE_PIO] = { 0x3eff0000, 0x00010000 },
160 [VIRT_PCIE_ECAM] = { 0x3f000000, 0x01000000 },
957e32cf
EA
161 /* Actual RAM size depends on initial RAM and device memory settings */
162 [VIRT_MEM] = { GiB, LEGACY_RAMLIMIT_BYTES },
350a9c9e
EA
163};
164
165/*
166 * Highmem IO Regions: This memory map is floating, located after the RAM.
167 * Each MemMapEntry base (GPA) will be dynamically computed, depending on the
168 * top of the RAM, so that its base get the same alignment as the size,
169 * ie. a 512GiB entry will be aligned on a 512GiB boundary. If there is
170 * less than 256GiB of RAM, the floating area starts at the 256GiB mark.
171 * Note the extended_memmap is sized so that it eventually also includes the
172 * base_memmap entries (VIRT_HIGH_GIC_REDIST2 index is greater than the last
173 * index of base_memmap).
174 */
175static MemMapEntry extended_memmap[] = {
f90747c4 176 /* Additional 64 MB redist region (can contain up to 512 redistributors) */
350a9c9e
EA
177 [VIRT_HIGH_GIC_REDIST2] = { 0x0, 64 * MiB },
178 [VIRT_HIGH_PCIE_ECAM] = { 0x0, 256 * MiB },
179 /* Second PCIe window */
180 [VIRT_HIGH_PCIE_MMIO] = { 0x0, 512 * GiB },
f5fdcd6e
PM
181};
182
183static const int a15irqmap[] = {
184 [VIRT_UART] = 1,
6e411af9 185 [VIRT_RTC] = 2,
4ab29b82 186 [VIRT_PCIE] = 3, /* ... to 6 */
b0a3721e 187 [VIRT_GPIO] = 7,
3df708eb 188 [VIRT_SECURE_UART] = 8,
cff51ac9 189 [VIRT_ACPI_GED] = 9,
f5fdcd6e 190 [VIRT_MMIO] = 16, /* ...to 16 + NUM_VIRTIO_TRANSPORTS - 1 */
bd204e63 191 [VIRT_GIC_V2M] = 48, /* ...to 48 + NUM_GICV2M_SPIS - 1 */
584105ea 192 [VIRT_SMMU] = 74, /* ...to 74 + NUM_SMMU_IRQS - 1 */
5f7a5a0e 193 [VIRT_PLATFORM_BUS] = 112, /* ...to 112 + PLATFORM_BUS_NUM_IRQS -1 */
f5fdcd6e
PM
194};
195
9ac4ef77 196static const char *valid_cpus[] = {
4414942e 197 ARM_CPU_TYPE_NAME("cortex-a7"),
ba1ba5cc
IM
198 ARM_CPU_TYPE_NAME("cortex-a15"),
199 ARM_CPU_TYPE_NAME("cortex-a53"),
200 ARM_CPU_TYPE_NAME("cortex-a57"),
2264faa5 201 ARM_CPU_TYPE_NAME("cortex-a72"),
ba1ba5cc 202 ARM_CPU_TYPE_NAME("host"),
9076ddb3 203 ARM_CPU_TYPE_NAME("max"),
f5fdcd6e
PM
204};
205
ba1ba5cc 206static bool cpu_type_valid(const char *cpu)
f5fdcd6e
PM
207{
208 int i;
209
9ac4ef77
PM
210 for (i = 0; i < ARRAY_SIZE(valid_cpus); i++) {
211 if (strcmp(cpu, valid_cpus[i]) == 0) {
212 return true;
f5fdcd6e
PM
213 }
214 }
9ac4ef77 215 return false;
f5fdcd6e
PM
216}
217
60592cfe
JF
218static void create_kaslr_seed(VirtMachineState *vms, const char *node)
219{
60592cfe
JF
220 uint64_t seed;
221
9261ef5e 222 if (qemu_guest_getrandom(&seed, sizeof(seed), NULL)) {
60592cfe
JF
223 return;
224 }
225 qemu_fdt_setprop_u64(vms->fdt, node, "kaslr-seed", seed);
226}
227
c8ef2bda 228static void create_fdt(VirtMachineState *vms)
f5fdcd6e 229{
aa570207
TX
230 MachineState *ms = MACHINE(vms);
231 int nb_numa_nodes = ms->numa_state->num_nodes;
c8ef2bda 232 void *fdt = create_device_tree(&vms->fdt_size);
f5fdcd6e
PM
233
234 if (!fdt) {
235 error_report("create_device_tree() failed");
236 exit(1);
237 }
238
c8ef2bda 239 vms->fdt = fdt;
f5fdcd6e
PM
240
241 /* Header */
5a4348d1
PC
242 qemu_fdt_setprop_string(fdt, "/", "compatible", "linux,dummy-virt");
243 qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2);
244 qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2);
f5fdcd6e 245
e2eb3d29 246 /* /chosen must exist for load_dtb to fill in necessary properties later */
5a4348d1 247 qemu_fdt_add_subnode(fdt, "/chosen");
60592cfe 248 create_kaslr_seed(vms, "/chosen");
f5fdcd6e 249
ef6a5c71
JF
250 if (vms->secure) {
251 qemu_fdt_add_subnode(fdt, "/secure-chosen");
60592cfe 252 create_kaslr_seed(vms, "/secure-chosen");
ef6a5c71
JF
253 }
254
f5fdcd6e
PM
255 /* Clock node, for the benefit of the UART. The kernel device tree
256 * binding documentation claims the PL011 node clock properties are
257 * optional but in practice if you omit them the kernel refuses to
258 * probe for the device.
259 */
c8ef2bda 260 vms->clock_phandle = qemu_fdt_alloc_phandle(fdt);
5a4348d1
PC
261 qemu_fdt_add_subnode(fdt, "/apb-pclk");
262 qemu_fdt_setprop_string(fdt, "/apb-pclk", "compatible", "fixed-clock");
263 qemu_fdt_setprop_cell(fdt, "/apb-pclk", "#clock-cells", 0x0);
264 qemu_fdt_setprop_cell(fdt, "/apb-pclk", "clock-frequency", 24000000);
265 qemu_fdt_setprop_string(fdt, "/apb-pclk", "clock-output-names",
f5fdcd6e 266 "clk24mhz");
c8ef2bda 267 qemu_fdt_setprop_cell(fdt, "/apb-pclk", "phandle", vms->clock_phandle);
f5fdcd6e 268
118154b7 269 if (nb_numa_nodes > 0 && ms->numa_state->have_numa_distance) {
c7637c04
AJ
270 int size = nb_numa_nodes * nb_numa_nodes * 3 * sizeof(uint32_t);
271 uint32_t *matrix = g_malloc0(size);
272 int idx, i, j;
273
274 for (i = 0; i < nb_numa_nodes; i++) {
275 for (j = 0; j < nb_numa_nodes; j++) {
276 idx = (i * nb_numa_nodes + j) * 3;
277 matrix[idx + 0] = cpu_to_be32(i);
278 matrix[idx + 1] = cpu_to_be32(j);
7e721e7b
TX
279 matrix[idx + 2] =
280 cpu_to_be32(ms->numa_state->nodes[i].distance[j]);
c7637c04
AJ
281 }
282 }
283
284 qemu_fdt_add_subnode(fdt, "/distance-map");
285 qemu_fdt_setprop_string(fdt, "/distance-map", "compatible",
286 "numa-distance-map-v1");
287 qemu_fdt_setprop(fdt, "/distance-map", "distance-matrix",
288 matrix, size);
289 g_free(matrix);
290 }
06955739
PS
291}
292
055a7f2b 293static void fdt_add_timer_nodes(const VirtMachineState *vms)
f5fdcd6e 294{
156bc9a5
PM
295 /* On real hardware these interrupts are level-triggered.
296 * On KVM they were edge-triggered before host kernel version 4.4,
297 * and level-triggered afterwards.
298 * On emulated QEMU they are level-triggered.
299 *
300 * Getting the DTB info about them wrong is awkward for some
301 * guest kernels:
302 * pre-4.8 ignore the DT and leave the interrupt configured
303 * with whatever the GIC reset value (or the bootloader) left it at
304 * 4.8 before rc6 honour the incorrect data by programming it back
305 * into the GIC, causing problems
306 * 4.8rc6 and later ignore the DT and always write "level triggered"
307 * into the GIC
308 *
309 * For backwards-compatibility, virt-2.8 and earlier will continue
310 * to say these are edge-triggered, but later machines will report
311 * the correct information.
f5fdcd6e 312 */
b32a9509 313 ARMCPU *armcpu;
156bc9a5
PM
314 VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms);
315 uint32_t irqflags = GIC_FDT_IRQ_FLAGS_LEVEL_HI;
316
317 if (vmc->claim_edge_triggered_timers) {
318 irqflags = GIC_FDT_IRQ_FLAGS_EDGE_LO_HI;
319 }
f5fdcd6e 320
d04460e5 321 if (vms->gic_version == VIRT_GIC_VERSION_2) {
b92ad394
PF
322 irqflags = deposit32(irqflags, GIC_FDT_IRQ_PPI_CPU_START,
323 GIC_FDT_IRQ_PPI_CPU_WIDTH,
c8ef2bda 324 (1 << vms->smp_cpus) - 1);
b92ad394 325 }
f5fdcd6e 326
c8ef2bda 327 qemu_fdt_add_subnode(vms->fdt, "/timer");
b32a9509
CF
328
329 armcpu = ARM_CPU(qemu_get_cpu(0));
330 if (arm_feature(&armcpu->env, ARM_FEATURE_V8)) {
331 const char compat[] = "arm,armv8-timer\0arm,armv7-timer";
c8ef2bda 332 qemu_fdt_setprop(vms->fdt, "/timer", "compatible",
b32a9509
CF
333 compat, sizeof(compat));
334 } else {
c8ef2bda 335 qemu_fdt_setprop_string(vms->fdt, "/timer", "compatible",
b32a9509
CF
336 "arm,armv7-timer");
337 }
c8ef2bda
PM
338 qemu_fdt_setprop(vms->fdt, "/timer", "always-on", NULL, 0);
339 qemu_fdt_setprop_cells(vms->fdt, "/timer", "interrupts",
ee246400
SZ
340 GIC_FDT_IRQ_TYPE_PPI, ARCH_TIMER_S_EL1_IRQ, irqflags,
341 GIC_FDT_IRQ_TYPE_PPI, ARCH_TIMER_NS_EL1_IRQ, irqflags,
342 GIC_FDT_IRQ_TYPE_PPI, ARCH_TIMER_VIRT_IRQ, irqflags,
343 GIC_FDT_IRQ_TYPE_PPI, ARCH_TIMER_NS_EL2_IRQ, irqflags);
f5fdcd6e
PM
344}
345
c8ef2bda 346static void fdt_add_cpu_nodes(const VirtMachineState *vms)
f5fdcd6e
PM
347{
348 int cpu;
8d45c54d 349 int addr_cells = 1;
4ccf5826 350 const MachineState *ms = MACHINE(vms);
8d45c54d
PF
351
352 /*
353 * From Documentation/devicetree/bindings/arm/cpus.txt
354 * On ARM v8 64-bit systems value should be set to 2,
355 * that corresponds to the MPIDR_EL1 register size.
356 * If MPIDR_EL1[63:32] value is equal to 0 on all CPUs
357 * in the system, #address-cells can be set to 1, since
358 * MPIDR_EL1[63:32] bits are not used for CPUs
359 * identification.
360 *
361 * Here we actually don't know whether our system is 32- or 64-bit one.
362 * The simplest way to go is to examine affinity IDs of all our CPUs. If
363 * at least one of them has Aff3 populated, we set #address-cells to 2.
364 */
c8ef2bda 365 for (cpu = 0; cpu < vms->smp_cpus; cpu++) {
8d45c54d
PF
366 ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(cpu));
367
368 if (armcpu->mp_affinity & ARM_AFF3_MASK) {
369 addr_cells = 2;
370 break;
371 }
372 }
f5fdcd6e 373
c8ef2bda
PM
374 qemu_fdt_add_subnode(vms->fdt, "/cpus");
375 qemu_fdt_setprop_cell(vms->fdt, "/cpus", "#address-cells", addr_cells);
376 qemu_fdt_setprop_cell(vms->fdt, "/cpus", "#size-cells", 0x0);
f5fdcd6e 377
c8ef2bda 378 for (cpu = vms->smp_cpus - 1; cpu >= 0; cpu--) {
f5fdcd6e
PM
379 char *nodename = g_strdup_printf("/cpus/cpu@%d", cpu);
380 ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(cpu));
4ccf5826 381 CPUState *cs = CPU(armcpu);
f5fdcd6e 382
c8ef2bda
PM
383 qemu_fdt_add_subnode(vms->fdt, nodename);
384 qemu_fdt_setprop_string(vms->fdt, nodename, "device_type", "cpu");
385 qemu_fdt_setprop_string(vms->fdt, nodename, "compatible",
f5fdcd6e
PM
386 armcpu->dtb_compatible);
387
2013c566
PM
388 if (vms->psci_conduit != QEMU_PSCI_CONDUIT_DISABLED
389 && vms->smp_cpus > 1) {
c8ef2bda 390 qemu_fdt_setprop_string(vms->fdt, nodename,
f5fdcd6e
PM
391 "enable-method", "psci");
392 }
393
8d45c54d 394 if (addr_cells == 2) {
c8ef2bda 395 qemu_fdt_setprop_u64(vms->fdt, nodename, "reg",
8d45c54d
PF
396 armcpu->mp_affinity);
397 } else {
c8ef2bda 398 qemu_fdt_setprop_cell(vms->fdt, nodename, "reg",
8d45c54d
PF
399 armcpu->mp_affinity);
400 }
401
4ccf5826
IM
402 if (ms->possible_cpus->cpus[cs->cpu_index].props.has_node_id) {
403 qemu_fdt_setprop_cell(vms->fdt, nodename, "numa-node-id",
404 ms->possible_cpus->cpus[cs->cpu_index].props.node_id);
9695200a
SZ
405 }
406
f5fdcd6e
PM
407 g_free(nodename);
408 }
409}
410
c8ef2bda 411static void fdt_add_its_gic_node(VirtMachineState *vms)
02f98731 412{
bb2a3348
EA
413 char *nodename;
414
c8ef2bda 415 vms->msi_phandle = qemu_fdt_alloc_phandle(vms->fdt);
bb2a3348
EA
416 nodename = g_strdup_printf("/intc/its@%" PRIx64,
417 vms->memmap[VIRT_GIC_ITS].base);
418 qemu_fdt_add_subnode(vms->fdt, nodename);
419 qemu_fdt_setprop_string(vms->fdt, nodename, "compatible",
02f98731 420 "arm,gic-v3-its");
bb2a3348
EA
421 qemu_fdt_setprop(vms->fdt, nodename, "msi-controller", NULL, 0);
422 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
c8ef2bda
PM
423 2, vms->memmap[VIRT_GIC_ITS].base,
424 2, vms->memmap[VIRT_GIC_ITS].size);
bb2a3348
EA
425 qemu_fdt_setprop_cell(vms->fdt, nodename, "phandle", vms->msi_phandle);
426 g_free(nodename);
02f98731
PF
427}
428
c8ef2bda 429static void fdt_add_v2m_gic_node(VirtMachineState *vms)
f5fdcd6e 430{
bb2a3348
EA
431 char *nodename;
432
433 nodename = g_strdup_printf("/intc/v2m@%" PRIx64,
434 vms->memmap[VIRT_GIC_V2M].base);
c8ef2bda 435 vms->msi_phandle = qemu_fdt_alloc_phandle(vms->fdt);
bb2a3348
EA
436 qemu_fdt_add_subnode(vms->fdt, nodename);
437 qemu_fdt_setprop_string(vms->fdt, nodename, "compatible",
bd204e63 438 "arm,gic-v2m-frame");
bb2a3348
EA
439 qemu_fdt_setprop(vms->fdt, nodename, "msi-controller", NULL, 0);
440 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
c8ef2bda
PM
441 2, vms->memmap[VIRT_GIC_V2M].base,
442 2, vms->memmap[VIRT_GIC_V2M].size);
bb2a3348
EA
443 qemu_fdt_setprop_cell(vms->fdt, nodename, "phandle", vms->msi_phandle);
444 g_free(nodename);
bd204e63 445}
f5fdcd6e 446
055a7f2b 447static void fdt_add_gic_node(VirtMachineState *vms)
bd204e63 448{
bb2a3348
EA
449 char *nodename;
450
c8ef2bda
PM
451 vms->gic_phandle = qemu_fdt_alloc_phandle(vms->fdt);
452 qemu_fdt_setprop_cell(vms->fdt, "/", "interrupt-parent", vms->gic_phandle);
453
bb2a3348
EA
454 nodename = g_strdup_printf("/intc@%" PRIx64,
455 vms->memmap[VIRT_GIC_DIST].base);
456 qemu_fdt_add_subnode(vms->fdt, nodename);
457 qemu_fdt_setprop_cell(vms->fdt, nodename, "#interrupt-cells", 3);
458 qemu_fdt_setprop(vms->fdt, nodename, "interrupt-controller", NULL, 0);
459 qemu_fdt_setprop_cell(vms->fdt, nodename, "#address-cells", 0x2);
460 qemu_fdt_setprop_cell(vms->fdt, nodename, "#size-cells", 0x2);
461 qemu_fdt_setprop(vms->fdt, nodename, "ranges", NULL, 0);
d04460e5 462 if (vms->gic_version == VIRT_GIC_VERSION_3) {
f90747c4
EA
463 int nb_redist_regions = virt_gicv3_redist_region_count(vms);
464
bb2a3348 465 qemu_fdt_setprop_string(vms->fdt, nodename, "compatible",
b92ad394 466 "arm,gic-v3");
f90747c4 467
bb2a3348 468 qemu_fdt_setprop_cell(vms->fdt, nodename,
f90747c4
EA
469 "#redistributor-regions", nb_redist_regions);
470
471 if (nb_redist_regions == 1) {
bb2a3348 472 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
f90747c4
EA
473 2, vms->memmap[VIRT_GIC_DIST].base,
474 2, vms->memmap[VIRT_GIC_DIST].size,
475 2, vms->memmap[VIRT_GIC_REDIST].base,
476 2, vms->memmap[VIRT_GIC_REDIST].size);
477 } else {
bb2a3348 478 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
bf424a12
EA
479 2, vms->memmap[VIRT_GIC_DIST].base,
480 2, vms->memmap[VIRT_GIC_DIST].size,
481 2, vms->memmap[VIRT_GIC_REDIST].base,
482 2, vms->memmap[VIRT_GIC_REDIST].size,
483 2, vms->memmap[VIRT_HIGH_GIC_REDIST2].base,
484 2, vms->memmap[VIRT_HIGH_GIC_REDIST2].size);
f90747c4
EA
485 }
486
f29cacfb 487 if (vms->virt) {
bb2a3348 488 qemu_fdt_setprop_cells(vms->fdt, nodename, "interrupts",
55ef3233 489 GIC_FDT_IRQ_TYPE_PPI, ARCH_GIC_MAINT_IRQ,
f29cacfb
PM
490 GIC_FDT_IRQ_FLAGS_LEVEL_HI);
491 }
b92ad394
PF
492 } else {
493 /* 'cortex-a15-gic' means 'GIC v2' */
bb2a3348 494 qemu_fdt_setprop_string(vms->fdt, nodename, "compatible",
b92ad394 495 "arm,cortex-a15-gic");
55ef3233
LM
496 if (!vms->virt) {
497 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
498 2, vms->memmap[VIRT_GIC_DIST].base,
499 2, vms->memmap[VIRT_GIC_DIST].size,
500 2, vms->memmap[VIRT_GIC_CPU].base,
501 2, vms->memmap[VIRT_GIC_CPU].size);
502 } else {
503 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
504 2, vms->memmap[VIRT_GIC_DIST].base,
505 2, vms->memmap[VIRT_GIC_DIST].size,
506 2, vms->memmap[VIRT_GIC_CPU].base,
507 2, vms->memmap[VIRT_GIC_CPU].size,
508 2, vms->memmap[VIRT_GIC_HYP].base,
509 2, vms->memmap[VIRT_GIC_HYP].size,
510 2, vms->memmap[VIRT_GIC_VCPU].base,
511 2, vms->memmap[VIRT_GIC_VCPU].size);
512 qemu_fdt_setprop_cells(vms->fdt, nodename, "interrupts",
513 GIC_FDT_IRQ_TYPE_PPI, ARCH_GIC_MAINT_IRQ,
514 GIC_FDT_IRQ_FLAGS_LEVEL_HI);
515 }
b92ad394
PF
516 }
517
bb2a3348
EA
518 qemu_fdt_setprop_cell(vms->fdt, nodename, "phandle", vms->gic_phandle);
519 g_free(nodename);
f5fdcd6e
PM
520}
521
055a7f2b 522static void fdt_add_pmu_nodes(const VirtMachineState *vms)
01fe6b60
SZ
523{
524 CPUState *cpu;
525 ARMCPU *armcpu;
526 uint32_t irqflags = GIC_FDT_IRQ_FLAGS_LEVEL_HI;
527
528 CPU_FOREACH(cpu) {
529 armcpu = ARM_CPU(cpu);
3f07cb2a 530 if (!arm_feature(&armcpu->env, ARM_FEATURE_PMU)) {
01fe6b60
SZ
531 return;
532 }
3f07cb2a 533 if (kvm_enabled()) {
b2bfe9f7
AJ
534 if (kvm_irqchip_in_kernel()) {
535 kvm_arm_pmu_set_irq(cpu, PPI(VIRTUAL_PMU_IRQ));
3f07cb2a 536 }
b2bfe9f7 537 kvm_arm_pmu_init(cpu);
3f07cb2a 538 }
01fe6b60
SZ
539 }
540
d04460e5 541 if (vms->gic_version == VIRT_GIC_VERSION_2) {
01fe6b60
SZ
542 irqflags = deposit32(irqflags, GIC_FDT_IRQ_PPI_CPU_START,
543 GIC_FDT_IRQ_PPI_CPU_WIDTH,
c8ef2bda 544 (1 << vms->smp_cpus) - 1);
01fe6b60
SZ
545 }
546
547 armcpu = ARM_CPU(qemu_get_cpu(0));
c8ef2bda 548 qemu_fdt_add_subnode(vms->fdt, "/pmu");
01fe6b60
SZ
549 if (arm_feature(&armcpu->env, ARM_FEATURE_V8)) {
550 const char compat[] = "arm,armv8-pmuv3";
c8ef2bda 551 qemu_fdt_setprop(vms->fdt, "/pmu", "compatible",
01fe6b60 552 compat, sizeof(compat));
c8ef2bda 553 qemu_fdt_setprop_cells(vms->fdt, "/pmu", "interrupts",
01fe6b60
SZ
554 GIC_FDT_IRQ_TYPE_PPI, VIRTUAL_PMU_IRQ, irqflags);
555 }
556}
557
b8b69f4c 558static inline DeviceState *create_acpi_ged(VirtMachineState *vms)
cff51ac9
SK
559{
560 DeviceState *dev;
561 MachineState *ms = MACHINE(vms);
562 int irq = vms->irqmap[VIRT_ACPI_GED];
1962f31b 563 uint32_t event = ACPI_GED_PWR_DOWN_EVT;
cff51ac9
SK
564
565 if (ms->ram_slots) {
1962f31b 566 event |= ACPI_GED_MEM_HOTPLUG_EVT;
cff51ac9
SK
567 }
568
c2505d1c
SK
569 if (ms->nvdimms_state->is_enabled) {
570 event |= ACPI_GED_NVDIMM_HOTPLUG_EVT;
571 }
572
3e80f690 573 dev = qdev_new(TYPE_ACPI_GED);
cff51ac9
SK
574 qdev_prop_set_uint32(dev, "ged-event", event);
575
576 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, vms->memmap[VIRT_ACPI_GED].base);
577 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 1, vms->memmap[VIRT_PCDIMM_ACPI].base);
b8b69f4c 578 sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0, qdev_get_gpio_in(vms->gic, irq));
cff51ac9 579
3c6ef471 580 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
cff51ac9
SK
581
582 return dev;
583}
584
b8b69f4c 585static void create_its(VirtMachineState *vms)
02f98731
PF
586{
587 const char *itsclass = its_class_name();
588 DeviceState *dev;
589
590 if (!itsclass) {
591 /* Do nothing if not supported */
592 return;
593 }
594
3e80f690 595 dev = qdev_new(itsclass);
02f98731 596
b8b69f4c 597 object_property_set_link(OBJECT(dev), OBJECT(vms->gic), "parent-gicv3",
02f98731 598 &error_abort);
3c6ef471 599 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
c8ef2bda 600 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, vms->memmap[VIRT_GIC_ITS].base);
02f98731 601
c8ef2bda 602 fdt_add_its_gic_node(vms);
1b6f99d8 603 vms->msi_controller = VIRT_MSI_CTRL_ITS;
02f98731
PF
604}
605
b8b69f4c 606static void create_v2m(VirtMachineState *vms)
bd204e63
CD
607{
608 int i;
c8ef2bda 609 int irq = vms->irqmap[VIRT_GIC_V2M];
bd204e63
CD
610 DeviceState *dev;
611
3e80f690 612 dev = qdev_new("arm-gicv2m");
c8ef2bda 613 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, vms->memmap[VIRT_GIC_V2M].base);
bd204e63
CD
614 qdev_prop_set_uint32(dev, "base-spi", irq);
615 qdev_prop_set_uint32(dev, "num-spi", NUM_GICV2M_SPIS);
3c6ef471 616 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
bd204e63
CD
617
618 for (i = 0; i < NUM_GICV2M_SPIS; i++) {
b8b69f4c
PMD
619 sysbus_connect_irq(SYS_BUS_DEVICE(dev), i,
620 qdev_get_gpio_in(vms->gic, irq + i));
bd204e63
CD
621 }
622
c8ef2bda 623 fdt_add_v2m_gic_node(vms);
1b6f99d8 624 vms->msi_controller = VIRT_MSI_CTRL_GICV2M;
bd204e63
CD
625}
626
b8b69f4c 627static void create_gic(VirtMachineState *vms)
64204743 628{
cc7d44c2 629 MachineState *ms = MACHINE(vms);
b92ad394 630 /* We create a standalone GIC */
64204743 631 SysBusDevice *gicbusdev;
e6fbcbc4 632 const char *gictype;
055a7f2b 633 int type = vms->gic_version, i;
cc7d44c2 634 unsigned int smp_cpus = ms->smp.cpus;
03d72fa1 635 uint32_t nb_redist_regions = 0;
64204743 636
b92ad394 637 gictype = (type == 3) ? gicv3_class_name() : gic_class_name();
64204743 638
3e80f690 639 vms->gic = qdev_new(gictype);
b8b69f4c
PMD
640 qdev_prop_set_uint32(vms->gic, "revision", type);
641 qdev_prop_set_uint32(vms->gic, "num-cpu", smp_cpus);
64204743
PM
642 /* Note that the num-irq property counts both internal and external
643 * interrupts; there are always 32 of the former (mandated by GIC spec).
644 */
b8b69f4c 645 qdev_prop_set_uint32(vms->gic, "num-irq", NUM_IRQS + 32);
0e21f183 646 if (!kvm_irqchip_in_kernel()) {
b8b69f4c 647 qdev_prop_set_bit(vms->gic, "has-security-extensions", vms->secure);
0e21f183 648 }
1e575b66
EA
649
650 if (type == 3) {
651 uint32_t redist0_capacity =
652 vms->memmap[VIRT_GIC_REDIST].size / GICV3_REDIST_SIZE;
653 uint32_t redist0_count = MIN(smp_cpus, redist0_capacity);
654
03d72fa1
EA
655 nb_redist_regions = virt_gicv3_redist_region_count(vms);
656
b8b69f4c 657 qdev_prop_set_uint32(vms->gic, "len-redist-region-count",
03d72fa1 658 nb_redist_regions);
b8b69f4c 659 qdev_prop_set_uint32(vms->gic, "redist-region-count[0]", redist0_count);
03d72fa1
EA
660
661 if (nb_redist_regions == 2) {
662 uint32_t redist1_capacity =
bf424a12 663 vms->memmap[VIRT_HIGH_GIC_REDIST2].size / GICV3_REDIST_SIZE;
03d72fa1 664
b8b69f4c 665 qdev_prop_set_uint32(vms->gic, "redist-region-count[1]",
03d72fa1
EA
666 MIN(smp_cpus - redist0_count, redist1_capacity));
667 }
55ef3233
LM
668 } else {
669 if (!kvm_irqchip_in_kernel()) {
b8b69f4c 670 qdev_prop_set_bit(vms->gic, "has-virtualization-extensions",
55ef3233
LM
671 vms->virt);
672 }
1e575b66 673 }
b8b69f4c 674 gicbusdev = SYS_BUS_DEVICE(vms->gic);
3c6ef471 675 sysbus_realize_and_unref(gicbusdev, &error_fatal);
c8ef2bda 676 sysbus_mmio_map(gicbusdev, 0, vms->memmap[VIRT_GIC_DIST].base);
b92ad394 677 if (type == 3) {
c8ef2bda 678 sysbus_mmio_map(gicbusdev, 1, vms->memmap[VIRT_GIC_REDIST].base);
03d72fa1 679 if (nb_redist_regions == 2) {
bf424a12
EA
680 sysbus_mmio_map(gicbusdev, 2,
681 vms->memmap[VIRT_HIGH_GIC_REDIST2].base);
03d72fa1 682 }
b92ad394 683 } else {
c8ef2bda 684 sysbus_mmio_map(gicbusdev, 1, vms->memmap[VIRT_GIC_CPU].base);
55ef3233
LM
685 if (vms->virt) {
686 sysbus_mmio_map(gicbusdev, 2, vms->memmap[VIRT_GIC_HYP].base);
687 sysbus_mmio_map(gicbusdev, 3, vms->memmap[VIRT_GIC_VCPU].base);
688 }
b92ad394 689 }
64204743 690
5454006a
PM
691 /* Wire the outputs from each CPU's generic timer and the GICv3
692 * maintenance interrupt signal to the appropriate GIC PPI inputs,
693 * and the GIC's IRQ/FIQ/VIRQ/VFIQ interrupt outputs to the CPU's inputs.
64204743
PM
694 */
695 for (i = 0; i < smp_cpus; i++) {
696 DeviceState *cpudev = DEVICE(qemu_get_cpu(i));
0e3e858f 697 int ppibase = NUM_IRQS + i * GIC_INTERNAL + GIC_NR_SGIS;
a007b1f8
PM
698 int irq;
699 /* Mapping from the output timer irq lines from the CPU to the
700 * GIC PPI inputs we use for the virt board.
64204743 701 */
a007b1f8
PM
702 const int timer_irq[] = {
703 [GTIMER_PHYS] = ARCH_TIMER_NS_EL1_IRQ,
704 [GTIMER_VIRT] = ARCH_TIMER_VIRT_IRQ,
705 [GTIMER_HYP] = ARCH_TIMER_NS_EL2_IRQ,
706 [GTIMER_SEC] = ARCH_TIMER_S_EL1_IRQ,
707 };
708
709 for (irq = 0; irq < ARRAY_SIZE(timer_irq); irq++) {
710 qdev_connect_gpio_out(cpudev, irq,
b8b69f4c 711 qdev_get_gpio_in(vms->gic,
a007b1f8
PM
712 ppibase + timer_irq[irq]));
713 }
64204743 714
55ef3233 715 if (type == 3) {
b8b69f4c 716 qemu_irq irq = qdev_get_gpio_in(vms->gic,
55ef3233
LM
717 ppibase + ARCH_GIC_MAINT_IRQ);
718 qdev_connect_gpio_out_named(cpudev, "gicv3-maintenance-interrupt",
719 0, irq);
720 } else if (vms->virt) {
b8b69f4c 721 qemu_irq irq = qdev_get_gpio_in(vms->gic,
55ef3233
LM
722 ppibase + ARCH_GIC_MAINT_IRQ);
723 sysbus_connect_irq(gicbusdev, i + 4 * smp_cpus, irq);
724 }
725
07f48730 726 qdev_connect_gpio_out_named(cpudev, "pmu-interrupt", 0,
b8b69f4c 727 qdev_get_gpio_in(vms->gic, ppibase
07f48730 728 + VIRTUAL_PMU_IRQ));
5454006a 729
64204743 730 sysbus_connect_irq(gicbusdev, i, qdev_get_gpio_in(cpudev, ARM_CPU_IRQ));
8e7b4ca0
GB
731 sysbus_connect_irq(gicbusdev, i + smp_cpus,
732 qdev_get_gpio_in(cpudev, ARM_CPU_FIQ));
5454006a
PM
733 sysbus_connect_irq(gicbusdev, i + 2 * smp_cpus,
734 qdev_get_gpio_in(cpudev, ARM_CPU_VIRQ));
735 sysbus_connect_irq(gicbusdev, i + 3 * smp_cpus,
736 qdev_get_gpio_in(cpudev, ARM_CPU_VFIQ));
64204743
PM
737 }
738
055a7f2b 739 fdt_add_gic_node(vms);
bd204e63 740
ccc11b02 741 if (type == 3 && vms->its) {
b8b69f4c 742 create_its(vms);
2231f69b 743 } else if (type == 2) {
b8b69f4c 744 create_v2m(vms);
b92ad394 745 }
64204743
PM
746}
747
b8b69f4c 748static void create_uart(const VirtMachineState *vms, int uart,
0ec7b3e7 749 MemoryRegion *mem, Chardev *chr)
f5fdcd6e
PM
750{
751 char *nodename;
c8ef2bda
PM
752 hwaddr base = vms->memmap[uart].base;
753 hwaddr size = vms->memmap[uart].size;
754 int irq = vms->irqmap[uart];
f5fdcd6e
PM
755 const char compat[] = "arm,pl011\0arm,primecell";
756 const char clocknames[] = "uartclk\0apb_pclk";
3e80f690 757 DeviceState *dev = qdev_new(TYPE_PL011);
3df708eb 758 SysBusDevice *s = SYS_BUS_DEVICE(dev);
f5fdcd6e 759
9bbbf649 760 qdev_prop_set_chr(dev, "chardev", chr);
3c6ef471 761 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
3df708eb
PM
762 memory_region_add_subregion(mem, base,
763 sysbus_mmio_get_region(s, 0));
b8b69f4c 764 sysbus_connect_irq(s, 0, qdev_get_gpio_in(vms->gic, irq));
f5fdcd6e
PM
765
766 nodename = g_strdup_printf("/pl011@%" PRIx64, base);
c8ef2bda 767 qemu_fdt_add_subnode(vms->fdt, nodename);
f5fdcd6e 768 /* Note that we can't use setprop_string because of the embedded NUL */
c8ef2bda 769 qemu_fdt_setprop(vms->fdt, nodename, "compatible",
f5fdcd6e 770 compat, sizeof(compat));
c8ef2bda 771 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
f5fdcd6e 772 2, base, 2, size);
c8ef2bda 773 qemu_fdt_setprop_cells(vms->fdt, nodename, "interrupts",
f5fdcd6e 774 GIC_FDT_IRQ_TYPE_SPI, irq,
0be969a2 775 GIC_FDT_IRQ_FLAGS_LEVEL_HI);
c8ef2bda
PM
776 qemu_fdt_setprop_cells(vms->fdt, nodename, "clocks",
777 vms->clock_phandle, vms->clock_phandle);
778 qemu_fdt_setprop(vms->fdt, nodename, "clock-names",
f5fdcd6e 779 clocknames, sizeof(clocknames));
f022b8e9 780
3df708eb 781 if (uart == VIRT_UART) {
c8ef2bda 782 qemu_fdt_setprop_string(vms->fdt, "/chosen", "stdout-path", nodename);
3df708eb
PM
783 } else {
784 /* Mark as not usable by the normal world */
c8ef2bda
PM
785 qemu_fdt_setprop_string(vms->fdt, nodename, "status", "disabled");
786 qemu_fdt_setprop_string(vms->fdt, nodename, "secure-status", "okay");
fb23d693 787
fb23d693
JF
788 qemu_fdt_setprop_string(vms->fdt, "/secure-chosen", "stdout-path",
789 nodename);
3df708eb
PM
790 }
791
f5fdcd6e
PM
792 g_free(nodename);
793}
794
b8b69f4c 795static void create_rtc(const VirtMachineState *vms)
6e411af9
PM
796{
797 char *nodename;
c8ef2bda
PM
798 hwaddr base = vms->memmap[VIRT_RTC].base;
799 hwaddr size = vms->memmap[VIRT_RTC].size;
800 int irq = vms->irqmap[VIRT_RTC];
6e411af9
PM
801 const char compat[] = "arm,pl031\0arm,primecell";
802
b8b69f4c 803 sysbus_create_simple("pl031", base, qdev_get_gpio_in(vms->gic, irq));
6e411af9
PM
804
805 nodename = g_strdup_printf("/pl031@%" PRIx64, base);
c8ef2bda
PM
806 qemu_fdt_add_subnode(vms->fdt, nodename);
807 qemu_fdt_setprop(vms->fdt, nodename, "compatible", compat, sizeof(compat));
808 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
6e411af9 809 2, base, 2, size);
c8ef2bda 810 qemu_fdt_setprop_cells(vms->fdt, nodename, "interrupts",
6e411af9 811 GIC_FDT_IRQ_TYPE_SPI, irq,
0be969a2 812 GIC_FDT_IRQ_FLAGS_LEVEL_HI);
c8ef2bda
PM
813 qemu_fdt_setprop_cell(vms->fdt, nodename, "clocks", vms->clock_phandle);
814 qemu_fdt_setprop_string(vms->fdt, nodename, "clock-names", "apb_pclk");
6e411af9
PM
815 g_free(nodename);
816}
817
94f02c5e 818static DeviceState *gpio_key_dev;
4bedd849
SZ
819static void virt_powerdown_req(Notifier *n, void *opaque)
820{
1962f31b
SK
821 VirtMachineState *s = container_of(n, VirtMachineState, powerdown_notifier);
822
823 if (s->acpi_dev) {
824 acpi_send_event(s->acpi_dev, ACPI_POWER_DOWN_STATUS);
825 } else {
826 /* use gpio Pin 3 for power button event */
827 qemu_set_irq(qdev_get_gpio_in(gpio_key_dev, 0), 1);
828 }
4bedd849
SZ
829}
830
b8b69f4c 831static void create_gpio(const VirtMachineState *vms)
b0a3721e
SZ
832{
833 char *nodename;
94f02c5e 834 DeviceState *pl061_dev;
c8ef2bda
PM
835 hwaddr base = vms->memmap[VIRT_GPIO].base;
836 hwaddr size = vms->memmap[VIRT_GPIO].size;
837 int irq = vms->irqmap[VIRT_GPIO];
b0a3721e
SZ
838 const char compat[] = "arm,pl061\0arm,primecell";
839
b8b69f4c
PMD
840 pl061_dev = sysbus_create_simple("pl061", base,
841 qdev_get_gpio_in(vms->gic, irq));
b0a3721e 842
c8ef2bda 843 uint32_t phandle = qemu_fdt_alloc_phandle(vms->fdt);
b0a3721e 844 nodename = g_strdup_printf("/pl061@%" PRIx64, base);
c8ef2bda
PM
845 qemu_fdt_add_subnode(vms->fdt, nodename);
846 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
b0a3721e 847 2, base, 2, size);
c8ef2bda
PM
848 qemu_fdt_setprop(vms->fdt, nodename, "compatible", compat, sizeof(compat));
849 qemu_fdt_setprop_cell(vms->fdt, nodename, "#gpio-cells", 2);
850 qemu_fdt_setprop(vms->fdt, nodename, "gpio-controller", NULL, 0);
851 qemu_fdt_setprop_cells(vms->fdt, nodename, "interrupts",
b0a3721e
SZ
852 GIC_FDT_IRQ_TYPE_SPI, irq,
853 GIC_FDT_IRQ_FLAGS_LEVEL_HI);
c8ef2bda
PM
854 qemu_fdt_setprop_cell(vms->fdt, nodename, "clocks", vms->clock_phandle);
855 qemu_fdt_setprop_string(vms->fdt, nodename, "clock-names", "apb_pclk");
856 qemu_fdt_setprop_cell(vms->fdt, nodename, "phandle", phandle);
3e6ebb64 857
94f02c5e
SZ
858 gpio_key_dev = sysbus_create_simple("gpio-key", -1,
859 qdev_get_gpio_in(pl061_dev, 3));
c8ef2bda
PM
860 qemu_fdt_add_subnode(vms->fdt, "/gpio-keys");
861 qemu_fdt_setprop_string(vms->fdt, "/gpio-keys", "compatible", "gpio-keys");
862 qemu_fdt_setprop_cell(vms->fdt, "/gpio-keys", "#size-cells", 0);
863 qemu_fdt_setprop_cell(vms->fdt, "/gpio-keys", "#address-cells", 1);
3e6ebb64 864
c8ef2bda
PM
865 qemu_fdt_add_subnode(vms->fdt, "/gpio-keys/poweroff");
866 qemu_fdt_setprop_string(vms->fdt, "/gpio-keys/poweroff",
3e6ebb64 867 "label", "GPIO Key Poweroff");
c8ef2bda 868 qemu_fdt_setprop_cell(vms->fdt, "/gpio-keys/poweroff", "linux,code",
3e6ebb64 869 KEY_POWER);
c8ef2bda 870 qemu_fdt_setprop_cells(vms->fdt, "/gpio-keys/poweroff",
3e6ebb64 871 "gpios", phandle, 3, 0);
b0a3721e
SZ
872 g_free(nodename);
873}
874
b8b69f4c 875static void create_virtio_devices(const VirtMachineState *vms)
f5fdcd6e
PM
876{
877 int i;
c8ef2bda 878 hwaddr size = vms->memmap[VIRT_MMIO].size;
f5fdcd6e 879
587078f0
LE
880 /* We create the transports in forwards order. Since qbus_realize()
881 * prepends (not appends) new child buses, the incrementing loop below will
882 * create a list of virtio-mmio buses with decreasing base addresses.
883 *
884 * When a -device option is processed from the command line,
885 * qbus_find_recursive() picks the next free virtio-mmio bus in forwards
886 * order. The upshot is that -device options in increasing command line
887 * order are mapped to virtio-mmio buses with decreasing base addresses.
888 *
889 * When this code was originally written, that arrangement ensured that the
890 * guest Linux kernel would give the lowest "name" (/dev/vda, eth0, etc) to
891 * the first -device on the command line. (The end-to-end order is a
892 * function of this loop, qbus_realize(), qbus_find_recursive(), and the
893 * guest kernel's name-to-address assignment strategy.)
894 *
895 * Meanwhile, the kernel's traversal seems to have been reversed; see eg.
896 * the message, if not necessarily the code, of commit 70161ff336.
897 * Therefore the loop now establishes the inverse of the original intent.
898 *
899 * Unfortunately, we can't counteract the kernel change by reversing the
900 * loop; it would break existing command lines.
901 *
902 * In any case, the kernel makes no guarantee about the stability of
903 * enumeration order of virtio devices (as demonstrated by it changing
904 * between kernel versions). For reliable and stable identification
905 * of disks users must use UUIDs or similar mechanisms.
f5fdcd6e
PM
906 */
907 for (i = 0; i < NUM_VIRTIO_TRANSPORTS; i++) {
c8ef2bda
PM
908 int irq = vms->irqmap[VIRT_MMIO] + i;
909 hwaddr base = vms->memmap[VIRT_MMIO].base + i * size;
f5fdcd6e 910
b8b69f4c
PMD
911 sysbus_create_simple("virtio-mmio", base,
912 qdev_get_gpio_in(vms->gic, irq));
f5fdcd6e
PM
913 }
914
587078f0
LE
915 /* We add dtb nodes in reverse order so that they appear in the finished
916 * device tree lowest address first.
917 *
918 * Note that this mapping is independent of the loop above. The previous
919 * loop influences virtio device to virtio transport assignment, whereas
920 * this loop controls how virtio transports are laid out in the dtb.
921 */
f5fdcd6e
PM
922 for (i = NUM_VIRTIO_TRANSPORTS - 1; i >= 0; i--) {
923 char *nodename;
c8ef2bda
PM
924 int irq = vms->irqmap[VIRT_MMIO] + i;
925 hwaddr base = vms->memmap[VIRT_MMIO].base + i * size;
f5fdcd6e
PM
926
927 nodename = g_strdup_printf("/virtio_mmio@%" PRIx64, base);
c8ef2bda
PM
928 qemu_fdt_add_subnode(vms->fdt, nodename);
929 qemu_fdt_setprop_string(vms->fdt, nodename,
5a4348d1 930 "compatible", "virtio,mmio");
c8ef2bda 931 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
5a4348d1 932 2, base, 2, size);
c8ef2bda 933 qemu_fdt_setprop_cells(vms->fdt, nodename, "interrupts",
5a4348d1
PC
934 GIC_FDT_IRQ_TYPE_SPI, irq,
935 GIC_FDT_IRQ_FLAGS_EDGE_LO_HI);
054bb7b2 936 qemu_fdt_setprop(vms->fdt, nodename, "dma-coherent", NULL, 0);
f5fdcd6e
PM
937 g_free(nodename);
938 }
939}
940
e0561e60
MA
941#define VIRT_FLASH_SECTOR_SIZE (256 * KiB)
942
943static PFlashCFI01 *virt_flash_create1(VirtMachineState *vms,
944 const char *name,
945 const char *alias_prop_name)
acf82361 946{
e0561e60
MA
947 /*
948 * Create a single flash device. We use the same parameters as
949 * the flash devices on the Versatile Express board.
acf82361 950 */
df707969 951 DeviceState *dev = qdev_new(TYPE_PFLASH_CFI01);
acf82361 952
e0561e60 953 qdev_prop_set_uint64(dev, "sector-length", VIRT_FLASH_SECTOR_SIZE);
acf82361
PM
954 qdev_prop_set_uint8(dev, "width", 4);
955 qdev_prop_set_uint8(dev, "device-width", 2);
e9809422 956 qdev_prop_set_bit(dev, "big-endian", false);
acf82361
PM
957 qdev_prop_set_uint16(dev, "id0", 0x89);
958 qdev_prop_set_uint16(dev, "id1", 0x18);
959 qdev_prop_set_uint16(dev, "id2", 0x00);
960 qdev_prop_set_uint16(dev, "id3", 0x00);
961 qdev_prop_set_string(dev, "name", name);
d2623129 962 object_property_add_child(OBJECT(vms), name, OBJECT(dev));
e0561e60 963 object_property_add_alias(OBJECT(vms), alias_prop_name,
d2623129 964 OBJECT(dev), "drive");
e0561e60
MA
965 return PFLASH_CFI01(dev);
966}
acf82361 967
e0561e60
MA
968static void virt_flash_create(VirtMachineState *vms)
969{
970 vms->flash[0] = virt_flash_create1(vms, "virt.flash0", "pflash0");
971 vms->flash[1] = virt_flash_create1(vms, "virt.flash1", "pflash1");
972}
acf82361 973
e0561e60
MA
974static void virt_flash_map1(PFlashCFI01 *flash,
975 hwaddr base, hwaddr size,
976 MemoryRegion *sysmem)
977{
978 DeviceState *dev = DEVICE(flash);
acf82361 979
4cdd0a77 980 assert(QEMU_IS_ALIGNED(size, VIRT_FLASH_SECTOR_SIZE));
e0561e60
MA
981 assert(size / VIRT_FLASH_SECTOR_SIZE <= UINT32_MAX);
982 qdev_prop_set_uint32(dev, "num-blocks", size / VIRT_FLASH_SECTOR_SIZE);
3c6ef471 983 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
e0561e60
MA
984
985 memory_region_add_subregion(sysmem, base,
986 sysbus_mmio_get_region(SYS_BUS_DEVICE(dev),
987 0));
16f4a8dc
PM
988}
989
e0561e60
MA
990static void virt_flash_map(VirtMachineState *vms,
991 MemoryRegion *sysmem,
992 MemoryRegion *secure_sysmem)
16f4a8dc 993{
e0561e60
MA
994 /*
995 * Map two flash devices to fill the VIRT_FLASH space in the memmap.
738a5d9f
PM
996 * sysmem is the system memory space. secure_sysmem is the secure view
997 * of the system, and the first flash device should be made visible only
998 * there. The second flash device is visible to both secure and nonsecure.
999 * If sysmem == secure_sysmem this means there is no separate Secure
1000 * address space and both flash devices are generally visible.
16f4a8dc 1001 */
c8ef2bda
PM
1002 hwaddr flashsize = vms->memmap[VIRT_FLASH].size / 2;
1003 hwaddr flashbase = vms->memmap[VIRT_FLASH].base;
acf82361 1004
e0561e60
MA
1005 virt_flash_map1(vms->flash[0], flashbase, flashsize,
1006 secure_sysmem);
1007 virt_flash_map1(vms->flash[1], flashbase + flashsize, flashsize,
1008 sysmem);
1009}
1010
1011static void virt_flash_fdt(VirtMachineState *vms,
1012 MemoryRegion *sysmem,
1013 MemoryRegion *secure_sysmem)
1014{
1015 hwaddr flashsize = vms->memmap[VIRT_FLASH].size / 2;
1016 hwaddr flashbase = vms->memmap[VIRT_FLASH].base;
1017 char *nodename;
acf82361 1018
738a5d9f
PM
1019 if (sysmem == secure_sysmem) {
1020 /* Report both flash devices as a single node in the DT */
1021 nodename = g_strdup_printf("/flash@%" PRIx64, flashbase);
c8ef2bda
PM
1022 qemu_fdt_add_subnode(vms->fdt, nodename);
1023 qemu_fdt_setprop_string(vms->fdt, nodename, "compatible", "cfi-flash");
1024 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
738a5d9f
PM
1025 2, flashbase, 2, flashsize,
1026 2, flashbase + flashsize, 2, flashsize);
c8ef2bda 1027 qemu_fdt_setprop_cell(vms->fdt, nodename, "bank-width", 4);
738a5d9f
PM
1028 g_free(nodename);
1029 } else {
e0561e60
MA
1030 /*
1031 * Report the devices as separate nodes so we can mark one as
738a5d9f
PM
1032 * only visible to the secure world.
1033 */
1034 nodename = g_strdup_printf("/secflash@%" PRIx64, flashbase);
c8ef2bda
PM
1035 qemu_fdt_add_subnode(vms->fdt, nodename);
1036 qemu_fdt_setprop_string(vms->fdt, nodename, "compatible", "cfi-flash");
1037 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
738a5d9f 1038 2, flashbase, 2, flashsize);
c8ef2bda
PM
1039 qemu_fdt_setprop_cell(vms->fdt, nodename, "bank-width", 4);
1040 qemu_fdt_setprop_string(vms->fdt, nodename, "status", "disabled");
1041 qemu_fdt_setprop_string(vms->fdt, nodename, "secure-status", "okay");
738a5d9f
PM
1042 g_free(nodename);
1043
1044 nodename = g_strdup_printf("/flash@%" PRIx64, flashbase);
c8ef2bda
PM
1045 qemu_fdt_add_subnode(vms->fdt, nodename);
1046 qemu_fdt_setprop_string(vms->fdt, nodename, "compatible", "cfi-flash");
1047 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
738a5d9f 1048 2, flashbase + flashsize, 2, flashsize);
c8ef2bda 1049 qemu_fdt_setprop_cell(vms->fdt, nodename, "bank-width", 4);
738a5d9f
PM
1050 g_free(nodename);
1051 }
acf82361
PM
1052}
1053
e0561e60
MA
1054static bool virt_firmware_init(VirtMachineState *vms,
1055 MemoryRegion *sysmem,
1056 MemoryRegion *secure_sysmem)
1057{
1058 int i;
1059 BlockBackend *pflash_blk0;
1060
1061 /* Map legacy -drive if=pflash to machine properties */
1062 for (i = 0; i < ARRAY_SIZE(vms->flash); i++) {
1063 pflash_cfi01_legacy_drive(vms->flash[i],
1064 drive_get(IF_PFLASH, 0, i));
1065 }
1066
1067 virt_flash_map(vms, sysmem, secure_sysmem);
1068
1069 pflash_blk0 = pflash_cfi01_get_blk(vms->flash[0]);
1070
1071 if (bios_name) {
1072 char *fname;
1073 MemoryRegion *mr;
1074 int image_size;
1075
1076 if (pflash_blk0) {
1077 error_report("The contents of the first flash device may be "
1078 "specified with -bios or with -drive if=pflash... "
1079 "but you cannot use both options at once");
1080 exit(1);
1081 }
1082
1083 /* Fall back to -bios */
1084
1085 fname = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
1086 if (!fname) {
1087 error_report("Could not find ROM image '%s'", bios_name);
1088 exit(1);
1089 }
1090 mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(vms->flash[0]), 0);
1091 image_size = load_image_mr(fname, mr);
1092 g_free(fname);
1093 if (image_size < 0) {
1094 error_report("Could not load ROM image '%s'", bios_name);
1095 exit(1);
1096 }
1097 }
1098
1099 return pflash_blk0 || bios_name;
1100}
1101
af1f60a4 1102static FWCfgState *create_fw_cfg(const VirtMachineState *vms, AddressSpace *as)
578f3c7b 1103{
cc7d44c2 1104 MachineState *ms = MACHINE(vms);
c8ef2bda
PM
1105 hwaddr base = vms->memmap[VIRT_FW_CFG].base;
1106 hwaddr size = vms->memmap[VIRT_FW_CFG].size;
5836d168 1107 FWCfgState *fw_cfg;
578f3c7b
LE
1108 char *nodename;
1109
5836d168 1110 fw_cfg = fw_cfg_init_mem_wide(base + 8, base, 8, base + 16, as);
cc7d44c2 1111 fw_cfg_add_i16(fw_cfg, FW_CFG_NB_CPUS, (uint16_t)ms->smp.cpus);
578f3c7b
LE
1112
1113 nodename = g_strdup_printf("/fw-cfg@%" PRIx64, base);
c8ef2bda
PM
1114 qemu_fdt_add_subnode(vms->fdt, nodename);
1115 qemu_fdt_setprop_string(vms->fdt, nodename,
578f3c7b 1116 "compatible", "qemu,fw-cfg-mmio");
c8ef2bda 1117 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
578f3c7b 1118 2, base, 2, size);
14efdb5c 1119 qemu_fdt_setprop(vms->fdt, nodename, "dma-coherent", NULL, 0);
578f3c7b 1120 g_free(nodename);
af1f60a4 1121 return fw_cfg;
578f3c7b
LE
1122}
1123
c8ef2bda 1124static void create_pcie_irq_map(const VirtMachineState *vms,
9ac4ef77 1125 uint32_t gic_phandle,
4ab29b82
AG
1126 int first_irq, const char *nodename)
1127{
1128 int devfn, pin;
dfd90a87 1129 uint32_t full_irq_map[4 * 4 * 10] = { 0 };
4ab29b82
AG
1130 uint32_t *irq_map = full_irq_map;
1131
1132 for (devfn = 0; devfn <= 0x18; devfn += 0x8) {
1133 for (pin = 0; pin < 4; pin++) {
1134 int irq_type = GIC_FDT_IRQ_TYPE_SPI;
1135 int irq_nr = first_irq + ((pin + PCI_SLOT(devfn)) % PCI_NUM_PINS);
1136 int irq_level = GIC_FDT_IRQ_FLAGS_LEVEL_HI;
1137 int i;
1138
1139 uint32_t map[] = {
1140 devfn << 8, 0, 0, /* devfn */
1141 pin + 1, /* PCI pin */
dfd90a87 1142 gic_phandle, 0, 0, irq_type, irq_nr, irq_level }; /* GIC irq */
4ab29b82
AG
1143
1144 /* Convert map to big endian */
dfd90a87 1145 for (i = 0; i < 10; i++) {
4ab29b82
AG
1146 irq_map[i] = cpu_to_be32(map[i]);
1147 }
dfd90a87 1148 irq_map += 10;
4ab29b82
AG
1149 }
1150 }
1151
c8ef2bda 1152 qemu_fdt_setprop(vms->fdt, nodename, "interrupt-map",
4ab29b82
AG
1153 full_irq_map, sizeof(full_irq_map));
1154
c8ef2bda 1155 qemu_fdt_setprop_cells(vms->fdt, nodename, "interrupt-map-mask",
4ab29b82
AG
1156 0x1800, 0, 0, /* devfn (PCI_SLOT(3)) */
1157 0x7 /* PCI irq */);
1158}
1159
b8b69f4c 1160static void create_smmu(const VirtMachineState *vms,
584105ea
PM
1161 PCIBus *bus)
1162{
1163 char *node;
1164 const char compat[] = "arm,smmu-v3";
1165 int irq = vms->irqmap[VIRT_SMMU];
1166 int i;
1167 hwaddr base = vms->memmap[VIRT_SMMU].base;
1168 hwaddr size = vms->memmap[VIRT_SMMU].size;
1169 const char irq_names[] = "eventq\0priq\0cmdq-sync\0gerror";
1170 DeviceState *dev;
1171
1172 if (vms->iommu != VIRT_IOMMU_SMMUV3 || !vms->iommu_phandle) {
1173 return;
1174 }
1175
3e80f690 1176 dev = qdev_new("arm-smmuv3");
584105ea
PM
1177
1178 object_property_set_link(OBJECT(dev), OBJECT(bus), "primary-bus",
1179 &error_abort);
3c6ef471 1180 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
584105ea
PM
1181 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base);
1182 for (i = 0; i < NUM_SMMU_IRQS; i++) {
b8b69f4c
PMD
1183 sysbus_connect_irq(SYS_BUS_DEVICE(dev), i,
1184 qdev_get_gpio_in(vms->gic, irq + i));
584105ea
PM
1185 }
1186
1187 node = g_strdup_printf("/smmuv3@%" PRIx64, base);
1188 qemu_fdt_add_subnode(vms->fdt, node);
1189 qemu_fdt_setprop(vms->fdt, node, "compatible", compat, sizeof(compat));
1190 qemu_fdt_setprop_sized_cells(vms->fdt, node, "reg", 2, base, 2, size);
1191
1192 qemu_fdt_setprop_cells(vms->fdt, node, "interrupts",
1193 GIC_FDT_IRQ_TYPE_SPI, irq , GIC_FDT_IRQ_FLAGS_EDGE_LO_HI,
1194 GIC_FDT_IRQ_TYPE_SPI, irq + 1, GIC_FDT_IRQ_FLAGS_EDGE_LO_HI,
1195 GIC_FDT_IRQ_TYPE_SPI, irq + 2, GIC_FDT_IRQ_FLAGS_EDGE_LO_HI,
1196 GIC_FDT_IRQ_TYPE_SPI, irq + 3, GIC_FDT_IRQ_FLAGS_EDGE_LO_HI);
1197
1198 qemu_fdt_setprop(vms->fdt, node, "interrupt-names", irq_names,
1199 sizeof(irq_names));
1200
1201 qemu_fdt_setprop_cell(vms->fdt, node, "clocks", vms->clock_phandle);
1202 qemu_fdt_setprop_string(vms->fdt, node, "clock-names", "apb_pclk");
1203 qemu_fdt_setprop(vms->fdt, node, "dma-coherent", NULL, 0);
1204
1205 qemu_fdt_setprop_cell(vms->fdt, node, "#iommu-cells", 1);
1206
1207 qemu_fdt_setprop_cell(vms->fdt, node, "phandle", vms->iommu_phandle);
1208 g_free(node);
1209}
1210
0fbddcec 1211static void create_virtio_iommu_dt_bindings(VirtMachineState *vms)
70e89132
EA
1212{
1213 const char compat[] = "virtio,pci-iommu";
1214 uint16_t bdf = vms->virtio_iommu_bdf;
1215 char *node;
1216
1217 vms->iommu_phandle = qemu_fdt_alloc_phandle(vms->fdt);
1218
1219 node = g_strdup_printf("%s/virtio_iommu@%d", vms->pciehb_nodename, bdf);
1220 qemu_fdt_add_subnode(vms->fdt, node);
1221 qemu_fdt_setprop(vms->fdt, node, "compatible", compat, sizeof(compat));
1222 qemu_fdt_setprop_sized_cells(vms->fdt, node, "reg",
1223 1, bdf << 8, 1, 0, 1, 0,
1224 1, 0, 1, 0);
1225
1226 qemu_fdt_setprop_cell(vms->fdt, node, "#iommu-cells", 1);
1227 qemu_fdt_setprop_cell(vms->fdt, node, "phandle", vms->iommu_phandle);
1228 g_free(node);
1229
1230 qemu_fdt_setprop_cells(vms->fdt, vms->pciehb_nodename, "iommu-map",
1231 0x0, vms->iommu_phandle, 0x0, bdf,
1232 bdf + 1, vms->iommu_phandle, bdf + 1, 0xffff - bdf);
1233}
1234
b8b69f4c 1235static void create_pcie(VirtMachineState *vms)
4ab29b82 1236{
c8ef2bda
PM
1237 hwaddr base_mmio = vms->memmap[VIRT_PCIE_MMIO].base;
1238 hwaddr size_mmio = vms->memmap[VIRT_PCIE_MMIO].size;
bf424a12
EA
1239 hwaddr base_mmio_high = vms->memmap[VIRT_HIGH_PCIE_MMIO].base;
1240 hwaddr size_mmio_high = vms->memmap[VIRT_HIGH_PCIE_MMIO].size;
c8ef2bda
PM
1241 hwaddr base_pio = vms->memmap[VIRT_PCIE_PIO].base;
1242 hwaddr size_pio = vms->memmap[VIRT_PCIE_PIO].size;
601d626d 1243 hwaddr base_ecam, size_ecam;
6a1f001b 1244 hwaddr base = base_mmio;
601d626d 1245 int nr_pcie_buses;
c8ef2bda 1246 int irq = vms->irqmap[VIRT_PCIE];
4ab29b82
AG
1247 MemoryRegion *mmio_alias;
1248 MemoryRegion *mmio_reg;
1249 MemoryRegion *ecam_alias;
1250 MemoryRegion *ecam_reg;
1251 DeviceState *dev;
1252 char *nodename;
601d626d 1253 int i, ecam_id;
fea9b3ca 1254 PCIHostState *pci;
4ab29b82 1255
3e80f690 1256 dev = qdev_new(TYPE_GPEX_HOST);
3c6ef471 1257 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
4ab29b82 1258
601d626d
EA
1259 ecam_id = VIRT_ECAM_ID(vms->highmem_ecam);
1260 base_ecam = vms->memmap[ecam_id].base;
1261 size_ecam = vms->memmap[ecam_id].size;
1262 nr_pcie_buses = size_ecam / PCIE_MMCFG_SIZE_MIN;
4ab29b82
AG
1263 /* Map only the first size_ecam bytes of ECAM space */
1264 ecam_alias = g_new0(MemoryRegion, 1);
1265 ecam_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0);
1266 memory_region_init_alias(ecam_alias, OBJECT(dev), "pcie-ecam",
1267 ecam_reg, 0, size_ecam);
1268 memory_region_add_subregion(get_system_memory(), base_ecam, ecam_alias);
1269
1270 /* Map the MMIO window into system address space so as to expose
1271 * the section of PCI MMIO space which starts at the same base address
1272 * (ie 1:1 mapping for that part of PCI MMIO space visible through
1273 * the window).
1274 */
1275 mmio_alias = g_new0(MemoryRegion, 1);
1276 mmio_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 1);
1277 memory_region_init_alias(mmio_alias, OBJECT(dev), "pcie-mmio",
1278 mmio_reg, base_mmio, size_mmio);
1279 memory_region_add_subregion(get_system_memory(), base_mmio, mmio_alias);
1280
0127937b 1281 if (vms->highmem) {
5125f9cd
PF
1282 /* Map high MMIO space */
1283 MemoryRegion *high_mmio_alias = g_new0(MemoryRegion, 1);
1284
1285 memory_region_init_alias(high_mmio_alias, OBJECT(dev), "pcie-mmio-high",
1286 mmio_reg, base_mmio_high, size_mmio_high);
1287 memory_region_add_subregion(get_system_memory(), base_mmio_high,
1288 high_mmio_alias);
1289 }
1290
4ab29b82 1291 /* Map IO port space */
6a1f001b 1292 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 2, base_pio);
4ab29b82
AG
1293
1294 for (i = 0; i < GPEX_NUM_IRQS; i++) {
b8b69f4c
PMD
1295 sysbus_connect_irq(SYS_BUS_DEVICE(dev), i,
1296 qdev_get_gpio_in(vms->gic, irq + i));
c9bb8e16 1297 gpex_set_irq_num(GPEX_HOST(dev), i, irq + i);
4ab29b82
AG
1298 }
1299
fea9b3ca
AK
1300 pci = PCI_HOST_BRIDGE(dev);
1301 if (pci->bus) {
1302 for (i = 0; i < nb_nics; i++) {
1303 NICInfo *nd = &nd_table[i];
1304
1305 if (!nd->model) {
1306 nd->model = g_strdup("virtio");
1307 }
1308
1309 pci_nic_init_nofail(nd, pci->bus, nd->model, NULL);
1310 }
1311 }
1312
70e89132 1313 nodename = vms->pciehb_nodename = g_strdup_printf("/pcie@%" PRIx64, base);
c8ef2bda
PM
1314 qemu_fdt_add_subnode(vms->fdt, nodename);
1315 qemu_fdt_setprop_string(vms->fdt, nodename,
4ab29b82 1316 "compatible", "pci-host-ecam-generic");
c8ef2bda
PM
1317 qemu_fdt_setprop_string(vms->fdt, nodename, "device_type", "pci");
1318 qemu_fdt_setprop_cell(vms->fdt, nodename, "#address-cells", 3);
1319 qemu_fdt_setprop_cell(vms->fdt, nodename, "#size-cells", 2);
6d9c1b8d 1320 qemu_fdt_setprop_cell(vms->fdt, nodename, "linux,pci-domain", 0);
c8ef2bda 1321 qemu_fdt_setprop_cells(vms->fdt, nodename, "bus-range", 0,
4ab29b82 1322 nr_pcie_buses - 1);
c8ef2bda 1323 qemu_fdt_setprop(vms->fdt, nodename, "dma-coherent", NULL, 0);
4ab29b82 1324
c8ef2bda
PM
1325 if (vms->msi_phandle) {
1326 qemu_fdt_setprop_cells(vms->fdt, nodename, "msi-parent",
1327 vms->msi_phandle);
b92ad394 1328 }
bd204e63 1329
c8ef2bda 1330 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
4ab29b82 1331 2, base_ecam, 2, size_ecam);
5125f9cd 1332
0127937b 1333 if (vms->highmem) {
c8ef2bda 1334 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "ranges",
5125f9cd
PF
1335 1, FDT_PCI_RANGE_IOPORT, 2, 0,
1336 2, base_pio, 2, size_pio,
1337 1, FDT_PCI_RANGE_MMIO, 2, base_mmio,
1338 2, base_mmio, 2, size_mmio,
1339 1, FDT_PCI_RANGE_MMIO_64BIT,
1340 2, base_mmio_high,
1341 2, base_mmio_high, 2, size_mmio_high);
1342 } else {
c8ef2bda 1343 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "ranges",
5125f9cd
PF
1344 1, FDT_PCI_RANGE_IOPORT, 2, 0,
1345 2, base_pio, 2, size_pio,
1346 1, FDT_PCI_RANGE_MMIO, 2, base_mmio,
1347 2, base_mmio, 2, size_mmio);
1348 }
4ab29b82 1349
c8ef2bda
PM
1350 qemu_fdt_setprop_cell(vms->fdt, nodename, "#interrupt-cells", 1);
1351 create_pcie_irq_map(vms, vms->gic_phandle, irq, nodename);
4ab29b82 1352
584105ea
PM
1353 if (vms->iommu) {
1354 vms->iommu_phandle = qemu_fdt_alloc_phandle(vms->fdt);
1355
70e89132
EA
1356 switch (vms->iommu) {
1357 case VIRT_IOMMU_SMMUV3:
1358 create_smmu(vms, pci->bus);
1359 qemu_fdt_setprop_cells(vms->fdt, nodename, "iommu-map",
1360 0x0, vms->iommu_phandle, 0x0, 0x10000);
1361 break;
1362 default:
1363 g_assert_not_reached();
1364 }
584105ea 1365 }
4ab29b82
AG
1366}
1367
b8b69f4c 1368static void create_platform_bus(VirtMachineState *vms)
5f7a5a0e
EA
1369{
1370 DeviceState *dev;
1371 SysBusDevice *s;
1372 int i;
5f7a5a0e
EA
1373 MemoryRegion *sysmem = get_system_memory();
1374
3e80f690 1375 dev = qdev_new(TYPE_PLATFORM_BUS_DEVICE);
5f7a5a0e 1376 dev->id = TYPE_PLATFORM_BUS_DEVICE;
3b77f6c3
IM
1377 qdev_prop_set_uint32(dev, "num_irqs", PLATFORM_BUS_NUM_IRQS);
1378 qdev_prop_set_uint32(dev, "mmio_size", vms->memmap[VIRT_PLATFORM_BUS].size);
3c6ef471 1379 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
a3fc8396 1380 vms->platform_bus_dev = dev;
5f7a5a0e 1381
3b77f6c3
IM
1382 s = SYS_BUS_DEVICE(dev);
1383 for (i = 0; i < PLATFORM_BUS_NUM_IRQS; i++) {
b8b69f4c
PMD
1384 int irq = vms->irqmap[VIRT_PLATFORM_BUS] + i;
1385 sysbus_connect_irq(s, i, qdev_get_gpio_in(vms->gic, irq));
5f7a5a0e
EA
1386 }
1387
1388 memory_region_add_subregion(sysmem,
3b77f6c3 1389 vms->memmap[VIRT_PLATFORM_BUS].base,
5f7a5a0e
EA
1390 sysbus_mmio_get_region(s, 0));
1391}
1392
8bce44a2
RH
1393static void create_tag_ram(MemoryRegion *tag_sysmem,
1394 hwaddr base, hwaddr size,
1395 const char *name)
1396{
1397 MemoryRegion *tagram = g_new(MemoryRegion, 1);
1398
1399 memory_region_init_ram(tagram, NULL, name, size / 32, &error_fatal);
1400 memory_region_add_subregion(tag_sysmem, base / 32, tagram);
1401}
1402
c8ef2bda 1403static void create_secure_ram(VirtMachineState *vms,
8bce44a2
RH
1404 MemoryRegion *secure_sysmem,
1405 MemoryRegion *secure_tag_sysmem)
83ec1923
PM
1406{
1407 MemoryRegion *secram = g_new(MemoryRegion, 1);
1408 char *nodename;
c8ef2bda
PM
1409 hwaddr base = vms->memmap[VIRT_SECURE_MEM].base;
1410 hwaddr size = vms->memmap[VIRT_SECURE_MEM].size;
83ec1923 1411
98a99ce0
PM
1412 memory_region_init_ram(secram, NULL, "virt.secure-ram", size,
1413 &error_fatal);
83ec1923
PM
1414 memory_region_add_subregion(secure_sysmem, base, secram);
1415
1416 nodename = g_strdup_printf("/secram@%" PRIx64, base);
c8ef2bda
PM
1417 qemu_fdt_add_subnode(vms->fdt, nodename);
1418 qemu_fdt_setprop_string(vms->fdt, nodename, "device_type", "memory");
1419 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg", 2, base, 2, size);
1420 qemu_fdt_setprop_string(vms->fdt, nodename, "status", "disabled");
1421 qemu_fdt_setprop_string(vms->fdt, nodename, "secure-status", "okay");
83ec1923 1422
8bce44a2
RH
1423 if (secure_tag_sysmem) {
1424 create_tag_ram(secure_tag_sysmem, base, size, "mach-virt.secure-tag");
1425 }
1426
83ec1923
PM
1427 g_free(nodename);
1428}
1429
f5fdcd6e
PM
1430static void *machvirt_dtb(const struct arm_boot_info *binfo, int *fdt_size)
1431{
9ac4ef77
PM
1432 const VirtMachineState *board = container_of(binfo, VirtMachineState,
1433 bootinfo);
f5fdcd6e
PM
1434
1435 *fdt_size = board->fdt_size;
1436 return board->fdt;
1437}
1438
e9a8e474 1439static void virt_build_smbios(VirtMachineState *vms)
c30e1565 1440{
dfadc3bf
WH
1441 MachineClass *mc = MACHINE_GET_CLASS(vms);
1442 VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms);
c30e1565
WH
1443 uint8_t *smbios_tables, *smbios_anchor;
1444 size_t smbios_tables_len, smbios_anchor_len;
bab27ea2 1445 const char *product = "QEMU Virtual Machine";
c30e1565 1446
bab27ea2
AJ
1447 if (kvm_enabled()) {
1448 product = "KVM Virtual Machine";
1449 }
1450
1451 smbios_set_defaults("QEMU", product,
dfadc3bf
WH
1452 vmc->smbios_old_sys_ver ? "1.0" : mc->name, false,
1453 true, SMBIOS_ENTRY_POINT_30);
c30e1565 1454
a0628599 1455 smbios_get_tables(MACHINE(vms), NULL, 0, &smbios_tables, &smbios_tables_len,
c30e1565
WH
1456 &smbios_anchor, &smbios_anchor_len);
1457
1458 if (smbios_anchor) {
af1f60a4 1459 fw_cfg_add_file(vms->fw_cfg, "etc/smbios/smbios-tables",
c30e1565 1460 smbios_tables, smbios_tables_len);
af1f60a4 1461 fw_cfg_add_file(vms->fw_cfg, "etc/smbios/smbios-anchor",
c30e1565
WH
1462 smbios_anchor, smbios_anchor_len);
1463 }
1464}
1465
d7c2e2db 1466static
054f4dc9 1467void virt_machine_done(Notifier *notifier, void *data)
d7c2e2db 1468{
054f4dc9
AJ
1469 VirtMachineState *vms = container_of(notifier, VirtMachineState,
1470 machine_done);
2744ece8 1471 MachineState *ms = MACHINE(vms);
3b77f6c3
IM
1472 ARMCPU *cpu = ARM_CPU(first_cpu);
1473 struct arm_boot_info *info = &vms->bootinfo;
1474 AddressSpace *as = arm_boot_address_space(cpu, info);
1475
1476 /*
1477 * If the user provided a dtb, we assume the dynamic sysbus nodes
1478 * already are integrated there. This corresponds to a use case where
1479 * the dynamic sysbus nodes are complex and their generation is not yet
1480 * supported. In that case the user can take charge of the guest dt
1481 * while qemu takes charge of the qom stuff.
1482 */
1483 if (info->dtb_filename == NULL) {
1484 platform_bus_add_all_fdt_nodes(vms->fdt, "/intc",
1485 vms->memmap[VIRT_PLATFORM_BUS].base,
1486 vms->memmap[VIRT_PLATFORM_BUS].size,
1487 vms->irqmap[VIRT_PLATFORM_BUS]);
1488 }
2744ece8 1489 if (arm_load_dtb(info->dtb_start, info, info->dtb_limit, as, ms) < 0) {
3b77f6c3
IM
1490 exit(1);
1491 }
054f4dc9 1492
e9a8e474
AJ
1493 virt_acpi_setup(vms);
1494 virt_build_smbios(vms);
d7c2e2db
SZ
1495}
1496
46de5913
IM
1497static uint64_t virt_cpu_mp_affinity(VirtMachineState *vms, int idx)
1498{
1499 uint8_t clustersz = ARM_DEFAULT_CPUS_PER_CLUSTER;
1500 VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms);
1501
1502 if (!vmc->disallow_affinity_adjustment) {
1503 /* Adjust MPIDR like 64-bit KVM hosts, which incorporate the
1504 * GIC's target-list limitations. 32-bit KVM hosts currently
1505 * always create clusters of 4 CPUs, but that is expected to
1506 * change when they gain support for gicv3. When KVM is enabled
1507 * it will override the changes we make here, therefore our
1508 * purposes are to make TCG consistent (with 64-bit KVM hosts)
1509 * and to improve SGI efficiency.
1510 */
d04460e5 1511 if (vms->gic_version == VIRT_GIC_VERSION_3) {
46de5913
IM
1512 clustersz = GICV3_TARGETLIST_BITS;
1513 } else {
1514 clustersz = GIC_TARGETLIST_BITS;
1515 }
1516 }
1517 return arm_cpu_mp_affinity(idx, clustersz);
1518}
1519
350a9c9e
EA
1520static void virt_set_memmap(VirtMachineState *vms)
1521{
957e32cf
EA
1522 MachineState *ms = MACHINE(vms);
1523 hwaddr base, device_memory_base, device_memory_size;
350a9c9e
EA
1524 int i;
1525
1526 vms->memmap = extended_memmap;
1527
1528 for (i = 0; i < ARRAY_SIZE(base_memmap); i++) {
1529 vms->memmap[i] = base_memmap[i];
1530 }
1531
957e32cf
EA
1532 if (ms->ram_slots > ACPI_MAX_RAM_SLOTS) {
1533 error_report("unsupported number of memory slots: %"PRIu64,
1534 ms->ram_slots);
1535 exit(EXIT_FAILURE);
1536 }
1537
1538 /*
1539 * We compute the base of the high IO region depending on the
1540 * amount of initial and device memory. The device memory start/size
1541 * is aligned on 1GiB. We never put the high IO region below 256GiB
1542 * so that if maxram_size is < 255GiB we keep the legacy memory map.
1543 * The device region size assumes 1GiB page max alignment per slot.
1544 */
1545 device_memory_base =
1546 ROUND_UP(vms->memmap[VIRT_MEM].base + ms->ram_size, GiB);
1547 device_memory_size = ms->maxram_size - ms->ram_size + ms->ram_slots * GiB;
1548
1549 /* Base address of the high IO region */
1550 base = device_memory_base + ROUND_UP(device_memory_size, GiB);
1551 if (base < device_memory_base) {
1552 error_report("maxmem/slots too huge");
1553 exit(EXIT_FAILURE);
1554 }
1555 if (base < vms->memmap[VIRT_MEM].base + LEGACY_RAMLIMIT_BYTES) {
1556 base = vms->memmap[VIRT_MEM].base + LEGACY_RAMLIMIT_BYTES;
1557 }
350a9c9e
EA
1558
1559 for (i = VIRT_LOWMEMMAP_LAST; i < ARRAY_SIZE(extended_memmap); i++) {
1560 hwaddr size = extended_memmap[i].size;
1561
1562 base = ROUND_UP(base, size);
1563 vms->memmap[i].base = base;
1564 vms->memmap[i].size = size;
1565 base += size;
1566 }
957e32cf
EA
1567 vms->highest_gpa = base - 1;
1568 if (device_memory_size > 0) {
1569 ms->device_memory = g_malloc0(sizeof(*ms->device_memory));
1570 ms->device_memory->base = device_memory_base;
1571 memory_region_init(&ms->device_memory->mr, OBJECT(vms),
1572 "device-memory", device_memory_size);
1573 }
350a9c9e
EA
1574}
1575
36bf4ec8
EA
1576/*
1577 * finalize_gic_version - Determines the final gic_version
1578 * according to the gic-version property
1579 *
1580 * Default GIC type is v2
1581 */
1582static void finalize_gic_version(VirtMachineState *vms)
1583{
6785aee0
EA
1584 unsigned int max_cpus = MACHINE(vms)->smp.max_cpus;
1585
97b4c918
EA
1586 if (kvm_enabled()) {
1587 int probe_bitmap;
d45efe47 1588
97b4c918
EA
1589 if (!kvm_irqchip_in_kernel()) {
1590 switch (vms->gic_version) {
1591 case VIRT_GIC_VERSION_HOST:
1592 warn_report(
1593 "gic-version=host not relevant with kernel-irqchip=off "
1594 "as only userspace GICv2 is supported. Using v2 ...");
1595 return;
1596 case VIRT_GIC_VERSION_MAX:
1597 case VIRT_GIC_VERSION_NOSEL:
1598 vms->gic_version = VIRT_GIC_VERSION_2;
1599 return;
1600 case VIRT_GIC_VERSION_2:
1601 return;
1602 case VIRT_GIC_VERSION_3:
36bf4ec8 1603 error_report(
97b4c918 1604 "gic-version=3 is not supported with kernel-irqchip=off");
36bf4ec8 1605 exit(1);
97b4c918
EA
1606 }
1607 }
1608
1609 probe_bitmap = kvm_arm_vgic_probe();
1610 if (!probe_bitmap) {
1611 error_report("Unable to determine GIC version supported by host");
1612 exit(1);
1613 }
1614
1615 switch (vms->gic_version) {
1616 case VIRT_GIC_VERSION_HOST:
1617 case VIRT_GIC_VERSION_MAX:
1618 if (probe_bitmap & KVM_ARM_VGIC_V3) {
1619 vms->gic_version = VIRT_GIC_VERSION_3;
d45efe47 1620 } else {
97b4c918 1621 vms->gic_version = VIRT_GIC_VERSION_2;
36bf4ec8 1622 }
97b4c918
EA
1623 return;
1624 case VIRT_GIC_VERSION_NOSEL:
6785aee0
EA
1625 if ((probe_bitmap & KVM_ARM_VGIC_V2) && max_cpus <= GIC_NCPU) {
1626 vms->gic_version = VIRT_GIC_VERSION_2;
1627 } else if (probe_bitmap & KVM_ARM_VGIC_V3) {
1628 /*
1629 * in case the host does not support v2 in-kernel emulation or
1630 * the end-user requested more than 8 VCPUs we now default
1631 * to v3. In any case defaulting to v2 would be broken.
1632 */
1633 vms->gic_version = VIRT_GIC_VERSION_3;
1634 } else if (max_cpus > GIC_NCPU) {
1635 error_report("host only supports in-kernel GICv2 emulation "
1636 "but more than 8 vcpus are requested");
1637 exit(1);
1638 }
97b4c918
EA
1639 break;
1640 case VIRT_GIC_VERSION_2:
1641 case VIRT_GIC_VERSION_3:
1642 break;
1643 }
1644
1645 /* Check chosen version is effectively supported by the host */
1646 if (vms->gic_version == VIRT_GIC_VERSION_2 &&
1647 !(probe_bitmap & KVM_ARM_VGIC_V2)) {
1648 error_report("host does not support in-kernel GICv2 emulation");
1649 exit(1);
1650 } else if (vms->gic_version == VIRT_GIC_VERSION_3 &&
1651 !(probe_bitmap & KVM_ARM_VGIC_V3)) {
1652 error_report("host does not support in-kernel GICv3 emulation");
1653 exit(1);
36bf4ec8 1654 }
97b4c918
EA
1655 return;
1656 }
1657
1658 /* TCG mode */
1659 switch (vms->gic_version) {
1660 case VIRT_GIC_VERSION_NOSEL:
36bf4ec8 1661 vms->gic_version = VIRT_GIC_VERSION_2;
97b4c918
EA
1662 break;
1663 case VIRT_GIC_VERSION_MAX:
1664 vms->gic_version = VIRT_GIC_VERSION_3;
1665 break;
1666 case VIRT_GIC_VERSION_HOST:
1667 error_report("gic-version=host requires KVM");
1668 exit(1);
1669 case VIRT_GIC_VERSION_2:
1670 case VIRT_GIC_VERSION_3:
1671 break;
36bf4ec8
EA
1672 }
1673}
1674
3ef96221 1675static void machvirt_init(MachineState *machine)
f5fdcd6e 1676{
e5a5604f 1677 VirtMachineState *vms = VIRT_MACHINE(machine);
95eb49c8 1678 VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(machine);
17d3d0e2
IM
1679 MachineClass *mc = MACHINE_GET_CLASS(machine);
1680 const CPUArchIdList *possible_cpus;
f5fdcd6e 1681 MemoryRegion *sysmem = get_system_memory();
3df708eb 1682 MemoryRegion *secure_sysmem = NULL;
8bce44a2
RH
1683 MemoryRegion *tag_sysmem = NULL;
1684 MemoryRegion *secure_tag_sysmem = NULL;
7ea686f5 1685 int n, virt_max_cpus;
e0561e60 1686 bool firmware_loaded;
17ec075a 1687 bool aarch64 = true;
cff51ac9 1688 bool has_ged = !vmc->no_ged;
cc7d44c2
LX
1689 unsigned int smp_cpus = machine->smp.cpus;
1690 unsigned int max_cpus = machine->smp.max_cpus;
f5fdcd6e 1691
c9650222
EA
1692 /*
1693 * In accelerated mode, the memory map is computed earlier in kvm_type()
1694 * to create a VM with the right number of IPA bits.
1695 */
1696 if (!vms->memmap) {
1697 virt_set_memmap(vms);
1698 }
350a9c9e 1699
b92ad394
PF
1700 /* We can probe only here because during property set
1701 * KVM is not available yet
1702 */
36bf4ec8 1703 finalize_gic_version(vms);
b92ad394 1704
ba1ba5cc
IM
1705 if (!cpu_type_valid(machine->cpu_type)) {
1706 error_report("mach-virt: CPU type %s not supported", machine->cpu_type);
f5fdcd6e
PM
1707 exit(1);
1708 }
1709
e0561e60
MA
1710 if (vms->secure) {
1711 if (kvm_enabled()) {
1712 error_report("mach-virt: KVM does not support Security extensions");
1713 exit(1);
1714 }
1715
1716 /*
1717 * The Secure view of the world is the same as the NonSecure,
1718 * but with a few extra devices. Create it as a container region
1719 * containing the system memory at low priority; any secure-only
1720 * devices go in at higher priority and take precedence.
1721 */
1722 secure_sysmem = g_new(MemoryRegion, 1);
1723 memory_region_init(secure_sysmem, OBJECT(machine), "secure-memory",
1724 UINT64_MAX);
1725 memory_region_add_subregion_overlap(secure_sysmem, 0, sysmem, -1);
1726 }
1727
1728 firmware_loaded = virt_firmware_init(vms, sysmem,
1729 secure_sysmem ?: sysmem);
1730
4824a61a
PM
1731 /* If we have an EL3 boot ROM then the assumption is that it will
1732 * implement PSCI itself, so disable QEMU's internal implementation
1733 * so it doesn't get in the way. Instead of starting secondary
1734 * CPUs in PSCI powerdown state we will start them all running and
1735 * let the boot ROM sort them out.
f29cacfb
PM
1736 * The usual case is that we do use QEMU's PSCI implementation;
1737 * if the guest has EL2 then we will use SMC as the conduit,
1738 * and otherwise we will use HVC (for backwards compatibility and
1739 * because if we're using KVM then we must use HVC).
4824a61a 1740 */
2013c566
PM
1741 if (vms->secure && firmware_loaded) {
1742 vms->psci_conduit = QEMU_PSCI_CONDUIT_DISABLED;
f29cacfb
PM
1743 } else if (vms->virt) {
1744 vms->psci_conduit = QEMU_PSCI_CONDUIT_SMC;
2013c566
PM
1745 } else {
1746 vms->psci_conduit = QEMU_PSCI_CONDUIT_HVC;
1747 }
4824a61a 1748
4b280b72
AJ
1749 /* The maximum number of CPUs depends on the GIC version, or on how
1750 * many redistributors we can fit into the memory map.
1751 */
d04460e5 1752 if (vms->gic_version == VIRT_GIC_VERSION_3) {
bf424a12
EA
1753 virt_max_cpus =
1754 vms->memmap[VIRT_GIC_REDIST].size / GICV3_REDIST_SIZE;
1755 virt_max_cpus +=
1756 vms->memmap[VIRT_HIGH_GIC_REDIST2].size / GICV3_REDIST_SIZE;
4b280b72 1757 } else {
7ea686f5 1758 virt_max_cpus = GIC_NCPU;
4b280b72
AJ
1759 }
1760
7ea686f5 1761 if (max_cpus > virt_max_cpus) {
4b280b72
AJ
1762 error_report("Number of SMP CPUs requested (%d) exceeds max CPUs "
1763 "supported by machine 'mach-virt' (%d)",
7ea686f5 1764 max_cpus, virt_max_cpus);
4b280b72
AJ
1765 exit(1);
1766 }
1767
c8ef2bda 1768 vms->smp_cpus = smp_cpus;
f5fdcd6e 1769
f29cacfb
PM
1770 if (vms->virt && kvm_enabled()) {
1771 error_report("mach-virt: KVM does not support providing "
1772 "Virtualization extensions to the guest CPU");
1773 exit(1);
1774 }
1775
c8ef2bda 1776 create_fdt(vms);
f5fdcd6e 1777
17d3d0e2
IM
1778 possible_cpus = mc->possible_cpu_arch_ids(machine);
1779 for (n = 0; n < possible_cpus->len; n++) {
1780 Object *cpuobj;
d9c34f9c 1781 CPUState *cs;
46de5913 1782
17d3d0e2
IM
1783 if (n >= smp_cpus) {
1784 break;
1785 }
1786
d342eb76 1787 cpuobj = object_new(possible_cpus->cpus[n].type);
17d3d0e2 1788 object_property_set_int(cpuobj, possible_cpus->cpus[n].arch_id,
46de5913 1789 "mp-affinity", NULL);
f313369f 1790
d9c34f9c
IM
1791 cs = CPU(cpuobj);
1792 cs->cpu_index = n;
1793
a0ceb640
IM
1794 numa_cpu_pre_plug(&possible_cpus->cpus[cs->cpu_index], DEVICE(cpuobj),
1795 &error_fatal);
bd4c1bfe 1796
17ec075a
EA
1797 aarch64 &= object_property_get_bool(cpuobj, "aarch64", NULL);
1798
e5a5604f
GB
1799 if (!vms->secure) {
1800 object_property_set_bool(cpuobj, false, "has_el3", NULL);
1801 }
1802
f29cacfb 1803 if (!vms->virt && object_property_find(cpuobj, "has_el2", NULL)) {
c25bd18a
PM
1804 object_property_set_bool(cpuobj, false, "has_el2", NULL);
1805 }
1806
2013c566
PM
1807 if (vms->psci_conduit != QEMU_PSCI_CONDUIT_DISABLED) {
1808 object_property_set_int(cpuobj, vms->psci_conduit,
4824a61a 1809 "psci-conduit", NULL);
211b0169 1810
4824a61a
PM
1811 /* Secondary CPUs start in PSCI powered-down state */
1812 if (n > 0) {
1813 object_property_set_bool(cpuobj, true,
1814 "start-powered-off", NULL);
1815 }
f5fdcd6e 1816 }
ba750085 1817
dea101a1
AJ
1818 if (vmc->kvm_no_adjvtime &&
1819 object_property_find(cpuobj, "kvm-no-adjvtime", NULL)) {
1820 object_property_set_bool(cpuobj, true, "kvm-no-adjvtime", NULL);
1821 }
1822
1141d1eb
WH
1823 if (vmc->no_pmu && object_property_find(cpuobj, "pmu", NULL)) {
1824 object_property_set_bool(cpuobj, false, "pmu", NULL);
1825 }
1826
ba750085 1827 if (object_property_find(cpuobj, "reset-cbar", NULL)) {
c8ef2bda 1828 object_property_set_int(cpuobj, vms->memmap[VIRT_CPUPERIPHS].base,
ba750085
PM
1829 "reset-cbar", &error_abort);
1830 }
1831
1d939a68
PM
1832 object_property_set_link(cpuobj, OBJECT(sysmem), "memory",
1833 &error_abort);
3df708eb
PM
1834 if (vms->secure) {
1835 object_property_set_link(cpuobj, OBJECT(secure_sysmem),
1836 "secure-memory", &error_abort);
1837 }
1d939a68 1838
8bce44a2
RH
1839 /*
1840 * The cpu adds the property if and only if MemTag is supported.
1841 * If it is, we must allocate the ram to back that up.
1842 */
1843 if (object_property_find(cpuobj, "tag-memory", NULL)) {
1844 if (!tag_sysmem) {
1845 tag_sysmem = g_new(MemoryRegion, 1);
1846 memory_region_init(tag_sysmem, OBJECT(machine),
1847 "tag-memory", UINT64_MAX / 32);
1848
1849 if (vms->secure) {
1850 secure_tag_sysmem = g_new(MemoryRegion, 1);
1851 memory_region_init(secure_tag_sysmem, OBJECT(machine),
1852 "secure-tag-memory", UINT64_MAX / 32);
1853
1854 /* As with ram, secure-tag takes precedence over tag. */
1855 memory_region_add_subregion_overlap(secure_tag_sysmem, 0,
1856 tag_sysmem, -1);
1857 }
1858 }
1859
1860 object_property_set_link(cpuobj, OBJECT(tag_sysmem),
1861 "tag-memory", &error_abort);
1862 if (vms->secure) {
1863 object_property_set_link(cpuobj, OBJECT(secure_tag_sysmem),
1864 "secure-tag-memory", &error_abort);
1865 }
1866 }
1867
ce189ab2 1868 qdev_realize(DEVICE(cpuobj), NULL, &error_fatal);
dbb74759 1869 object_unref(cpuobj);
f5fdcd6e 1870 }
055a7f2b 1871 fdt_add_timer_nodes(vms);
c8ef2bda 1872 fdt_add_cpu_nodes(vms);
f5fdcd6e 1873
2ba956cc
EA
1874 if (!kvm_enabled()) {
1875 ARMCPU *cpu = ARM_CPU(first_cpu);
1876 bool aarch64 = object_property_get_bool(OBJECT(cpu), "aarch64", NULL);
1877
1878 if (aarch64 && vms->highmem) {
1879 int requested_pa_size, pamax = arm_pamax(cpu);
1880
1881 requested_pa_size = 64 - clz64(vms->highest_gpa);
1882 if (pamax < requested_pa_size) {
1883 error_report("VCPU supports less PA bits (%d) than requested "
1884 "by the memory map (%d)", pamax, requested_pa_size);
1885 exit(1);
1886 }
1887 }
1888 }
1889
a72f6805
IM
1890 memory_region_add_subregion(sysmem, vms->memmap[VIRT_MEM].base,
1891 machine->ram);
957e32cf
EA
1892 if (machine->device_memory) {
1893 memory_region_add_subregion(sysmem, machine->device_memory->base,
1894 &machine->device_memory->mr);
1895 }
f5fdcd6e 1896
80734cbd 1897 virt_flash_fdt(vms, sysmem, secure_sysmem ?: sysmem);
acf82361 1898
b8b69f4c 1899 create_gic(vms);
f5fdcd6e 1900
055a7f2b 1901 fdt_add_pmu_nodes(vms);
01fe6b60 1902
b8b69f4c 1903 create_uart(vms, VIRT_UART, sysmem, serial_hd(0));
3df708eb
PM
1904
1905 if (vms->secure) {
8bce44a2 1906 create_secure_ram(vms, secure_sysmem, secure_tag_sysmem);
b8b69f4c 1907 create_uart(vms, VIRT_SECURE_UART, secure_sysmem, serial_hd(1));
3df708eb 1908 }
f5fdcd6e 1909
8bce44a2
RH
1910 if (tag_sysmem) {
1911 create_tag_ram(tag_sysmem, vms->memmap[VIRT_MEM].base,
1912 machine->ram_size, "mach-virt.tag");
1913 }
1914
17ec075a
EA
1915 vms->highmem_ecam &= vms->highmem && (!firmware_loaded || aarch64);
1916
b8b69f4c 1917 create_rtc(vms);
6e411af9 1918
b8b69f4c 1919 create_pcie(vms);
4ab29b82 1920
17e89077 1921 if (has_ged && aarch64 && firmware_loaded && virt_is_acpi_enabled(vms)) {
b8b69f4c 1922 vms->acpi_dev = create_acpi_ged(vms);
1962f31b 1923 } else {
b8b69f4c 1924 create_gpio(vms);
cff51ac9
SK
1925 }
1926
c345680c
SK
1927 /* connect powerdown request */
1928 vms->powerdown_notifier.notify = virt_powerdown_req;
1929 qemu_register_powerdown_notifier(&vms->powerdown_notifier);
1930
f5fdcd6e
PM
1931 /* Create mmio transports, so the user can create virtio backends
1932 * (which will be automatically plugged in to the transports). If
1933 * no backend is created the transport will just sit harmlessly idle.
1934 */
b8b69f4c 1935 create_virtio_devices(vms);
f5fdcd6e 1936
af1f60a4
AJ
1937 vms->fw_cfg = create_fw_cfg(vms, &address_space_memory);
1938 rom_set_fw(vms->fw_cfg);
d7c2e2db 1939
b8b69f4c 1940 create_platform_bus(vms);
578f3c7b 1941
b5a60bee
KL
1942 if (machine->nvdimms_state->is_enabled) {
1943 const struct AcpiGenericAddress arm_virt_nvdimm_acpi_dsmio = {
1944 .space_id = AML_AS_SYSTEM_MEMORY,
1945 .address = vms->memmap[VIRT_NVDIMM_ACPI].base,
1946 .bit_width = NVDIMM_ACPI_IO_LEN << 3
1947 };
1948
1949 nvdimm_init_acpi_state(machine->nvdimms_state, sysmem,
1950 arm_virt_nvdimm_acpi_dsmio,
1951 vms->fw_cfg, OBJECT(vms));
1952 }
1953
c8ef2bda 1954 vms->bootinfo.ram_size = machine->ram_size;
c8ef2bda
PM
1955 vms->bootinfo.nb_cpus = smp_cpus;
1956 vms->bootinfo.board_id = -1;
1957 vms->bootinfo.loader_start = vms->memmap[VIRT_MEM].base;
1958 vms->bootinfo.get_dtb = machvirt_dtb;
3b77f6c3 1959 vms->bootinfo.skip_dtb_autoload = true;
c8ef2bda 1960 vms->bootinfo.firmware_loaded = firmware_loaded;
2744ece8 1961 arm_load_kernel(ARM_CPU(first_cpu), machine, &vms->bootinfo);
5f7a5a0e 1962
3b77f6c3
IM
1963 vms->machine_done.notify = virt_machine_done;
1964 qemu_add_machine_init_done_notifier(&vms->machine_done);
f5fdcd6e
PM
1965}
1966
083a5890
GB
1967static bool virt_get_secure(Object *obj, Error **errp)
1968{
1969 VirtMachineState *vms = VIRT_MACHINE(obj);
1970
1971 return vms->secure;
1972}
1973
1974static void virt_set_secure(Object *obj, bool value, Error **errp)
1975{
1976 VirtMachineState *vms = VIRT_MACHINE(obj);
1977
1978 vms->secure = value;
1979}
1980
f29cacfb
PM
1981static bool virt_get_virt(Object *obj, Error **errp)
1982{
1983 VirtMachineState *vms = VIRT_MACHINE(obj);
1984
1985 return vms->virt;
1986}
1987
1988static void virt_set_virt(Object *obj, bool value, Error **errp)
1989{
1990 VirtMachineState *vms = VIRT_MACHINE(obj);
1991
1992 vms->virt = value;
1993}
1994
5125f9cd
PF
1995static bool virt_get_highmem(Object *obj, Error **errp)
1996{
1997 VirtMachineState *vms = VIRT_MACHINE(obj);
1998
1999 return vms->highmem;
2000}
2001
2002static void virt_set_highmem(Object *obj, bool value, Error **errp)
2003{
2004 VirtMachineState *vms = VIRT_MACHINE(obj);
2005
2006 vms->highmem = value;
2007}
2008
ccc11b02
EA
2009static bool virt_get_its(Object *obj, Error **errp)
2010{
2011 VirtMachineState *vms = VIRT_MACHINE(obj);
2012
2013 return vms->its;
2014}
2015
2016static void virt_set_its(Object *obj, bool value, Error **errp)
2017{
2018 VirtMachineState *vms = VIRT_MACHINE(obj);
2019
2020 vms->its = value;
2021}
2022
17e89077
GH
2023bool virt_is_acpi_enabled(VirtMachineState *vms)
2024{
2025 if (vms->acpi == ON_OFF_AUTO_OFF) {
2026 return false;
2027 }
2028 return true;
2029}
2030
2031static void virt_get_acpi(Object *obj, Visitor *v, const char *name,
2032 void *opaque, Error **errp)
2033{
2034 VirtMachineState *vms = VIRT_MACHINE(obj);
2035 OnOffAuto acpi = vms->acpi;
2036
2037 visit_type_OnOffAuto(v, name, &acpi, errp);
2038}
2039
2040static void virt_set_acpi(Object *obj, Visitor *v, const char *name,
2041 void *opaque, Error **errp)
2042{
2043 VirtMachineState *vms = VIRT_MACHINE(obj);
2044
2045 visit_type_OnOffAuto(v, name, &vms->acpi, errp);
2046}
2047
2afa8c85
DG
2048static bool virt_get_ras(Object *obj, Error **errp)
2049{
2050 VirtMachineState *vms = VIRT_MACHINE(obj);
2051
2052 return vms->ras;
2053}
2054
2055static void virt_set_ras(Object *obj, bool value, Error **errp)
2056{
2057 VirtMachineState *vms = VIRT_MACHINE(obj);
2058
2059 vms->ras = value;
2060}
2061
b92ad394
PF
2062static char *virt_get_gic_version(Object *obj, Error **errp)
2063{
2064 VirtMachineState *vms = VIRT_MACHINE(obj);
d04460e5 2065 const char *val = vms->gic_version == VIRT_GIC_VERSION_3 ? "3" : "2";
b92ad394
PF
2066
2067 return g_strdup(val);
2068}
2069
2070static void virt_set_gic_version(Object *obj, const char *value, Error **errp)
2071{
2072 VirtMachineState *vms = VIRT_MACHINE(obj);
2073
2074 if (!strcmp(value, "3")) {
d04460e5 2075 vms->gic_version = VIRT_GIC_VERSION_3;
b92ad394 2076 } else if (!strcmp(value, "2")) {
d04460e5 2077 vms->gic_version = VIRT_GIC_VERSION_2;
b92ad394 2078 } else if (!strcmp(value, "host")) {
d04460e5 2079 vms->gic_version = VIRT_GIC_VERSION_HOST; /* Will probe later */
dc16538a 2080 } else if (!strcmp(value, "max")) {
d04460e5 2081 vms->gic_version = VIRT_GIC_VERSION_MAX; /* Will probe later */
b92ad394 2082 } else {
7b55044f 2083 error_setg(errp, "Invalid gic-version value");
dc16538a 2084 error_append_hint(errp, "Valid values are 3, 2, host, max.\n");
b92ad394
PF
2085 }
2086}
2087
e24e3454
EA
2088static char *virt_get_iommu(Object *obj, Error **errp)
2089{
2090 VirtMachineState *vms = VIRT_MACHINE(obj);
2091
2092 switch (vms->iommu) {
2093 case VIRT_IOMMU_NONE:
2094 return g_strdup("none");
2095 case VIRT_IOMMU_SMMUV3:
2096 return g_strdup("smmuv3");
2097 default:
2098 g_assert_not_reached();
2099 }
2100}
2101
2102static void virt_set_iommu(Object *obj, const char *value, Error **errp)
2103{
2104 VirtMachineState *vms = VIRT_MACHINE(obj);
2105
2106 if (!strcmp(value, "smmuv3")) {
2107 vms->iommu = VIRT_IOMMU_SMMUV3;
2108 } else if (!strcmp(value, "none")) {
2109 vms->iommu = VIRT_IOMMU_NONE;
2110 } else {
2111 error_setg(errp, "Invalid iommu value");
2112 error_append_hint(errp, "Valid values are none, smmuv3.\n");
2113 }
2114}
2115
ea089eeb
IM
2116static CpuInstanceProperties
2117virt_cpu_index_to_props(MachineState *ms, unsigned cpu_index)
2118{
2119 MachineClass *mc = MACHINE_GET_CLASS(ms);
2120 const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(ms);
2121
2122 assert(cpu_index < possible_cpus->len);
2123 return possible_cpus->cpus[cpu_index].props;
2124}
2125
79e07936
IM
2126static int64_t virt_get_default_cpu_node_id(const MachineState *ms, int idx)
2127{
aa570207 2128 return idx % ms->numa_state->num_nodes;
79e07936
IM
2129}
2130
17d3d0e2
IM
2131static const CPUArchIdList *virt_possible_cpu_arch_ids(MachineState *ms)
2132{
2133 int n;
cc7d44c2 2134 unsigned int max_cpus = ms->smp.max_cpus;
17d3d0e2
IM
2135 VirtMachineState *vms = VIRT_MACHINE(ms);
2136
2137 if (ms->possible_cpus) {
2138 assert(ms->possible_cpus->len == max_cpus);
2139 return ms->possible_cpus;
2140 }
2141
2142 ms->possible_cpus = g_malloc0(sizeof(CPUArchIdList) +
2143 sizeof(CPUArchId) * max_cpus);
2144 ms->possible_cpus->len = max_cpus;
2145 for (n = 0; n < ms->possible_cpus->len; n++) {
d342eb76 2146 ms->possible_cpus->cpus[n].type = ms->cpu_type;
17d3d0e2
IM
2147 ms->possible_cpus->cpus[n].arch_id =
2148 virt_cpu_mp_affinity(vms, n);
2149 ms->possible_cpus->cpus[n].props.has_thread_id = true;
2150 ms->possible_cpus->cpus[n].props.thread_id = n;
17d3d0e2
IM
2151 }
2152 return ms->possible_cpus;
2153}
2154
1f283ae1
EA
2155static void virt_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
2156 Error **errp)
2157{
cff51ac9 2158 VirtMachineState *vms = VIRT_MACHINE(hotplug_dev);
c2505d1c 2159 const MachineState *ms = MACHINE(hotplug_dev);
cff51ac9 2160 const bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
1f283ae1 2161
cff51ac9
SK
2162 if (!vms->acpi_dev) {
2163 error_setg(errp,
2164 "memory hotplug is not enabled: missing acpi-ged device");
1f283ae1
EA
2165 return;
2166 }
2167
c2505d1c
SK
2168 if (is_nvdimm && !ms->nvdimms_state->is_enabled) {
2169 error_setg(errp, "nvdimm is not enabled: add 'nvdimm=on' to '-M'");
2170 return;
2171 }
2172
1f283ae1
EA
2173 pc_dimm_pre_plug(PC_DIMM(dev), MACHINE(hotplug_dev), NULL, errp);
2174}
2175
2176static void virt_memory_plug(HotplugHandler *hotplug_dev,
2177 DeviceState *dev, Error **errp)
2178{
2179 VirtMachineState *vms = VIRT_MACHINE(hotplug_dev);
b5a60bee
KL
2180 MachineState *ms = MACHINE(hotplug_dev);
2181 bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
1f283ae1
EA
2182 Error *local_err = NULL;
2183
2184 pc_dimm_plug(PC_DIMM(dev), MACHINE(vms), &local_err);
cff51ac9
SK
2185 if (local_err) {
2186 goto out;
2187 }
1f283ae1 2188
b5a60bee
KL
2189 if (is_nvdimm) {
2190 nvdimm_plug(ms->nvdimms_state);
2191 }
2192
53eccc70
KZ
2193 hotplug_handler_plug(HOTPLUG_HANDLER(vms->acpi_dev),
2194 dev, &error_abort);
2195
cff51ac9 2196out:
1f283ae1
EA
2197 error_propagate(errp, local_err);
2198}
2199
2200static void virt_machine_device_pre_plug_cb(HotplugHandler *hotplug_dev,
2201 DeviceState *dev, Error **errp)
2202{
1b6f99d8
EA
2203 VirtMachineState *vms = VIRT_MACHINE(hotplug_dev);
2204
1f283ae1
EA
2205 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
2206 virt_memory_pre_plug(hotplug_dev, dev, errp);
1b6f99d8
EA
2207 } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_IOMMU_PCI)) {
2208 hwaddr db_start = 0, db_end = 0;
2209 char *resv_prop_str;
2210
2211 switch (vms->msi_controller) {
2212 case VIRT_MSI_CTRL_NONE:
2213 return;
2214 case VIRT_MSI_CTRL_ITS:
2215 /* GITS_TRANSLATER page */
2216 db_start = base_memmap[VIRT_GIC_ITS].base + 0x10000;
2217 db_end = base_memmap[VIRT_GIC_ITS].base +
2218 base_memmap[VIRT_GIC_ITS].size - 1;
2219 break;
2220 case VIRT_MSI_CTRL_GICV2M:
2221 /* MSI_SETSPI_NS page */
2222 db_start = base_memmap[VIRT_GIC_V2M].base;
2223 db_end = db_start + base_memmap[VIRT_GIC_V2M].size - 1;
2224 break;
2225 }
2226 resv_prop_str = g_strdup_printf("0x%"PRIx64":0x%"PRIx64":%u",
2227 db_start, db_end,
2228 VIRTIO_IOMMU_RESV_MEM_T_MSI);
2229
2230 qdev_prop_set_uint32(dev, "len-reserved-regions", 1);
2231 qdev_prop_set_string(dev, "reserved-regions[0]", resv_prop_str);
2232 g_free(resv_prop_str);
1f283ae1
EA
2233 }
2234}
2235
a3fc8396
IM
2236static void virt_machine_device_plug_cb(HotplugHandler *hotplug_dev,
2237 DeviceState *dev, Error **errp)
2238{
2239 VirtMachineState *vms = VIRT_MACHINE(hotplug_dev);
2240
2241 if (vms->platform_bus_dev) {
2242 if (object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE)) {
2243 platform_bus_link_device(PLATFORM_BUS_DEVICE(vms->platform_bus_dev),
2244 SYS_BUS_DEVICE(dev));
2245 }
2246 }
1f283ae1
EA
2247 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
2248 virt_memory_plug(hotplug_dev, dev, errp);
2249 }
70e89132
EA
2250 if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_IOMMU_PCI)) {
2251 PCIDevice *pdev = PCI_DEVICE(dev);
2252
2253 vms->iommu = VIRT_IOMMU_VIRTIO;
2254 vms->virtio_iommu_bdf = pci_get_bdf(pdev);
0fbddcec 2255 create_virtio_iommu_dt_bindings(vms);
70e89132 2256 }
1f283ae1
EA
2257}
2258
539533b8
SK
2259static void virt_dimm_unplug_request(HotplugHandler *hotplug_dev,
2260 DeviceState *dev, Error **errp)
2261{
2262 VirtMachineState *vms = VIRT_MACHINE(hotplug_dev);
2263 Error *local_err = NULL;
2264
2265 if (!vms->acpi_dev) {
2266 error_setg(&local_err,
2267 "memory hotplug is not enabled: missing acpi-ged device");
2268 goto out;
2269 }
2270
2271 if (object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM)) {
2272 error_setg(&local_err,
2273 "nvdimm device hot unplug is not supported yet.");
2274 goto out;
2275 }
2276
2277 hotplug_handler_unplug_request(HOTPLUG_HANDLER(vms->acpi_dev), dev,
2278 &local_err);
2279out:
2280 error_propagate(errp, local_err);
2281}
2282
2283static void virt_dimm_unplug(HotplugHandler *hotplug_dev,
2284 DeviceState *dev, Error **errp)
2285{
2286 VirtMachineState *vms = VIRT_MACHINE(hotplug_dev);
2287 Error *local_err = NULL;
2288
2289 hotplug_handler_unplug(HOTPLUG_HANDLER(vms->acpi_dev), dev, &local_err);
2290 if (local_err) {
2291 goto out;
2292 }
2293
2294 pc_dimm_unplug(PC_DIMM(dev), MACHINE(vms));
2295 qdev_unrealize(dev);
2296
2297out:
2298 error_propagate(errp, local_err);
2299}
2300
1f283ae1
EA
2301static void virt_machine_device_unplug_request_cb(HotplugHandler *hotplug_dev,
2302 DeviceState *dev, Error **errp)
2303{
539533b8
SK
2304 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
2305 virt_dimm_unplug_request(hotplug_dev, dev, errp);
2306 } else {
2307 error_setg(errp, "device unplug request for unsupported device"
2308 " type: %s", object_get_typename(OBJECT(dev)));
2309 }
2310}
2311
2312static void virt_machine_device_unplug_cb(HotplugHandler *hotplug_dev,
2313 DeviceState *dev, Error **errp)
2314{
2315 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
2316 virt_dimm_unplug(hotplug_dev, dev, errp);
2317 } else {
2318 error_setg(errp, "virt: device unplug for unsupported device"
2319 " type: %s", object_get_typename(OBJECT(dev)));
2320 }
a3fc8396
IM
2321}
2322
2323static HotplugHandler *virt_machine_get_hotplug_handler(MachineState *machine,
2324 DeviceState *dev)
2325{
1f283ae1
EA
2326 if (object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE) ||
2327 (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM))) {
a3fc8396
IM
2328 return HOTPLUG_HANDLER(machine);
2329 }
70e89132
EA
2330 if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_IOMMU_PCI)) {
2331 VirtMachineState *vms = VIRT_MACHINE(machine);
a3fc8396 2332
17e89077 2333 if (!vms->bootinfo.firmware_loaded || !virt_is_acpi_enabled(vms)) {
70e89132
EA
2334 return HOTPLUG_HANDLER(machine);
2335 }
2336 }
a3fc8396
IM
2337 return NULL;
2338}
2339
c9650222
EA
2340/*
2341 * for arm64 kvm_type [7-0] encodes the requested number of bits
2342 * in the IPA address space
2343 */
2344static int virt_kvm_type(MachineState *ms, const char *type_str)
2345{
2346 VirtMachineState *vms = VIRT_MACHINE(ms);
2347 int max_vm_pa_size = kvm_arm_get_max_vm_ipa_size(ms);
2348 int requested_pa_size;
2349
2350 /* we freeze the memory map to compute the highest gpa */
2351 virt_set_memmap(vms);
2352
2353 requested_pa_size = 64 - clz64(vms->highest_gpa);
2354
2355 if (requested_pa_size > max_vm_pa_size) {
2356 error_report("-m and ,maxmem option values "
2357 "require an IPA range (%d bits) larger than "
2358 "the one supported by the host (%d bits)",
2359 requested_pa_size, max_vm_pa_size);
2360 exit(1);
2361 }
2362 /*
2363 * By default we return 0 which corresponds to an implicit legacy
2364 * 40b IPA setting. Otherwise we return the actual requested PA
2365 * logsize
2366 */
2367 return requested_pa_size > 40 ? requested_pa_size : 0;
2368}
2369
ed796373
WH
2370static void virt_machine_class_init(ObjectClass *oc, void *data)
2371{
9c94d8e6 2372 MachineClass *mc = MACHINE_CLASS(oc);
a3fc8396 2373 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
9c94d8e6
WH
2374
2375 mc->init = machvirt_init;
b10fbd53
EA
2376 /* Start with max_cpus set to 512, which is the maximum supported by KVM.
2377 * The value may be reduced later when we have more information about the
9c94d8e6
WH
2378 * configuration of the particular instance.
2379 */
b10fbd53 2380 mc->max_cpus = 512;
6f2062b9
EH
2381 machine_class_allow_dynamic_sysbus_dev(mc, TYPE_VFIO_CALXEDA_XGMAC);
2382 machine_class_allow_dynamic_sysbus_dev(mc, TYPE_VFIO_AMD_XGBE);
94692dcd 2383 machine_class_allow_dynamic_sysbus_dev(mc, TYPE_RAMFB_DEVICE);
4ebc0b61 2384 machine_class_allow_dynamic_sysbus_dev(mc, TYPE_VFIO_PLATFORM);
c294ac32 2385 machine_class_allow_dynamic_sysbus_dev(mc, TYPE_TPM_TIS_SYSBUS);
9c94d8e6
WH
2386 mc->block_default_type = IF_VIRTIO;
2387 mc->no_cdrom = 1;
2388 mc->pci_allow_0_address = true;
a2519ad1
PM
2389 /* We know we will never create a pre-ARMv7 CPU which needs 1K pages */
2390 mc->minimum_page_bits = 12;
17d3d0e2 2391 mc->possible_cpu_arch_ids = virt_possible_cpu_arch_ids;
ea089eeb 2392 mc->cpu_index_to_instance_props = virt_cpu_index_to_props;
ba1ba5cc 2393 mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-a15");
79e07936 2394 mc->get_default_cpu_node_id = virt_get_default_cpu_node_id;
c9650222 2395 mc->kvm_type = virt_kvm_type;
debbdc00 2396 assert(!mc->get_hotplug_handler);
a3fc8396 2397 mc->get_hotplug_handler = virt_machine_get_hotplug_handler;
1f283ae1 2398 hc->pre_plug = virt_machine_device_pre_plug_cb;
a3fc8396 2399 hc->plug = virt_machine_device_plug_cb;
1f283ae1 2400 hc->unplug_request = virt_machine_device_unplug_request_cb;
539533b8 2401 hc->unplug = virt_machine_device_unplug_cb;
c2505d1c 2402 mc->nvdimm_supported = true;
442da7dc 2403 mc->auto_enable_numa_with_memhp = true;
195784a0 2404 mc->auto_enable_numa_with_memdev = true;
a72f6805 2405 mc->default_ram_id = "mach-virt.ram";
17e89077
GH
2406
2407 object_class_property_add(oc, "acpi", "OnOffAuto",
2408 virt_get_acpi, virt_set_acpi,
d2623129 2409 NULL, NULL);
17e89077 2410 object_class_property_set_description(oc, "acpi",
7eecec7d 2411 "Enable ACPI");
ed796373
WH
2412}
2413
95159760 2414static void virt_instance_init(Object *obj)
083a5890
GB
2415{
2416 VirtMachineState *vms = VIRT_MACHINE(obj);
ccc11b02 2417 VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms);
083a5890 2418
2d710006
PM
2419 /* EL3 is disabled by default on virt: this makes us consistent
2420 * between KVM and TCG for this board, and it also allows us to
2421 * boot UEFI blobs which assume no TrustZone support.
2422 */
2423 vms->secure = false;
083a5890 2424 object_property_add_bool(obj, "secure", virt_get_secure,
d2623129 2425 virt_set_secure);
083a5890
GB
2426 object_property_set_description(obj, "secure",
2427 "Set on/off to enable/disable the ARM "
7eecec7d 2428 "Security Extensions (TrustZone)");
5125f9cd 2429
f29cacfb
PM
2430 /* EL2 is also disabled by default, for similar reasons */
2431 vms->virt = false;
2432 object_property_add_bool(obj, "virtualization", virt_get_virt,
d2623129 2433 virt_set_virt);
f29cacfb
PM
2434 object_property_set_description(obj, "virtualization",
2435 "Set on/off to enable/disable emulating a "
2436 "guest CPU which implements the ARM "
7eecec7d 2437 "Virtualization Extensions");
f29cacfb 2438
5125f9cd
PF
2439 /* High memory is enabled by default */
2440 vms->highmem = true;
2441 object_property_add_bool(obj, "highmem", virt_get_highmem,
d2623129 2442 virt_set_highmem);
5125f9cd
PF
2443 object_property_set_description(obj, "highmem",
2444 "Set on/off to enable/disable using "
7eecec7d 2445 "physical address space above 32 bits");
36bf4ec8 2446 vms->gic_version = VIRT_GIC_VERSION_NOSEL;
b92ad394 2447 object_property_add_str(obj, "gic-version", virt_get_gic_version,
d2623129 2448 virt_set_gic_version);
b92ad394
PF
2449 object_property_set_description(obj, "gic-version",
2450 "Set GIC version. "
7eecec7d 2451 "Valid values are 2, 3, host and max");
9ac4ef77 2452
17ec075a
EA
2453 vms->highmem_ecam = !vmc->no_highmem_ecam;
2454
ccc11b02
EA
2455 if (vmc->no_its) {
2456 vms->its = false;
2457 } else {
2458 /* Default allows ITS instantiation */
2459 vms->its = true;
2460 object_property_add_bool(obj, "its", virt_get_its,
d2623129 2461 virt_set_its);
ccc11b02
EA
2462 object_property_set_description(obj, "its",
2463 "Set on/off to enable/disable "
7eecec7d 2464 "ITS instantiation");
ccc11b02
EA
2465 }
2466
e24e3454
EA
2467 /* Default disallows iommu instantiation */
2468 vms->iommu = VIRT_IOMMU_NONE;
d2623129 2469 object_property_add_str(obj, "iommu", virt_get_iommu, virt_set_iommu);
e24e3454
EA
2470 object_property_set_description(obj, "iommu",
2471 "Set the IOMMU type. "
7eecec7d 2472 "Valid values are none and smmuv3");
e24e3454 2473
2afa8c85
DG
2474 /* Default disallows RAS instantiation */
2475 vms->ras = false;
2476 object_property_add_bool(obj, "ras", virt_get_ras,
d2623129 2477 virt_set_ras);
2afa8c85
DG
2478 object_property_set_description(obj, "ras",
2479 "Set on/off to enable/disable reporting host memory errors "
7eecec7d 2480 "to a KVM guest using ACPI and guest external abort exceptions");
2afa8c85 2481
9ac4ef77 2482 vms->irqmap = a15irqmap;
e0561e60
MA
2483
2484 virt_flash_create(vms);
083a5890
GB
2485}
2486
95159760
EH
2487static const TypeInfo virt_machine_info = {
2488 .name = TYPE_VIRT_MACHINE,
2489 .parent = TYPE_MACHINE,
2490 .abstract = true,
2491 .instance_size = sizeof(VirtMachineState),
2492 .class_size = sizeof(VirtMachineClass),
2493 .class_init = virt_machine_class_init,
bbac02f1 2494 .instance_init = virt_instance_init,
95159760
EH
2495 .interfaces = (InterfaceInfo[]) {
2496 { TYPE_HOTPLUG_HANDLER },
2497 { }
2498 },
2499};
2500
2501static void machvirt_machine_init(void)
2502{
2503 type_register_static(&virt_machine_info);
2504}
2505type_init(machvirt_machine_init);
2506
541aaa1d
CH
2507static void virt_machine_5_1_options(MachineClass *mc)
2508{
2509}
2510DEFINE_VIRT_MACHINE_AS_LATEST(5, 1)
2511
3eb74d20
CH
2512static void virt_machine_5_0_options(MachineClass *mc)
2513{
2c1fb4d5
AJ
2514 VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
2515
541aaa1d 2516 virt_machine_5_1_options(mc);
c6228807 2517 compat_props_add(mc->compat_props, hw_compat_5_0, hw_compat_5_0_len);
32a354dc 2518 mc->numa_mem_supported = true;
2c1fb4d5 2519 vmc->acpi_expose_flash = true;
195784a0 2520 mc->auto_enable_numa_with_memdev = false;
3eb74d20 2521}
541aaa1d 2522DEFINE_VIRT_MACHINE(5, 0)
3eb74d20 2523
9aec2e52
CH
2524static void virt_machine_4_2_options(MachineClass *mc)
2525{
dea101a1
AJ
2526 VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
2527
fa7c8e92 2528 virt_machine_5_0_options(mc);
5f258577 2529 compat_props_add(mc->compat_props, hw_compat_4_2, hw_compat_4_2_len);
dea101a1 2530 vmc->kvm_no_adjvtime = true;
9aec2e52 2531}
3eb74d20 2532DEFINE_VIRT_MACHINE(4, 2)
9aec2e52 2533
9bf2650b
CH
2534static void virt_machine_4_1_options(MachineClass *mc)
2535{
cff51ac9
SK
2536 VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
2537
9aec2e52
CH
2538 virt_machine_4_2_options(mc);
2539 compat_props_add(mc->compat_props, hw_compat_4_1, hw_compat_4_1_len);
cff51ac9 2540 vmc->no_ged = true;
442da7dc 2541 mc->auto_enable_numa_with_memhp = false;
9bf2650b 2542}
9aec2e52 2543DEFINE_VIRT_MACHINE(4, 1)
9bf2650b 2544
84e060bf
AW
2545static void virt_machine_4_0_options(MachineClass *mc)
2546{
9bf2650b
CH
2547 virt_machine_4_1_options(mc);
2548 compat_props_add(mc->compat_props, hw_compat_4_0, hw_compat_4_0_len);
84e060bf 2549}
9bf2650b 2550DEFINE_VIRT_MACHINE(4, 0)
84e060bf 2551
22907d2b
AJ
2552static void virt_machine_3_1_options(MachineClass *mc)
2553{
84e060bf 2554 virt_machine_4_0_options(mc);
abd93cc7 2555 compat_props_add(mc->compat_props, hw_compat_3_1, hw_compat_3_1_len);
22907d2b 2556}
84e060bf 2557DEFINE_VIRT_MACHINE(3, 1)
22907d2b 2558
8ae9a1ca
EA
2559static void virt_machine_3_0_options(MachineClass *mc)
2560{
22907d2b 2561 virt_machine_3_1_options(mc);
ddb3235d 2562 compat_props_add(mc->compat_props, hw_compat_3_0, hw_compat_3_0_len);
8ae9a1ca 2563}
22907d2b
AJ
2564DEFINE_VIRT_MACHINE(3, 0)
2565
a2a05159
PM
2566static void virt_machine_2_12_options(MachineClass *mc)
2567{
17ec075a
EA
2568 VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
2569
8ae9a1ca 2570 virt_machine_3_0_options(mc);
0d47310b 2571 compat_props_add(mc->compat_props, hw_compat_2_12, hw_compat_2_12_len);
17ec075a 2572 vmc->no_highmem_ecam = true;
b10fbd53 2573 mc->max_cpus = 255;
a2a05159 2574}
8ae9a1ca 2575DEFINE_VIRT_MACHINE(2, 12)
a2a05159 2576
79283dda
EA
2577static void virt_machine_2_11_options(MachineClass *mc)
2578{
dfadc3bf
WH
2579 VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
2580
a2a05159 2581 virt_machine_2_12_options(mc);
43df70a9 2582 compat_props_add(mc->compat_props, hw_compat_2_11, hw_compat_2_11_len);
dfadc3bf 2583 vmc->smbios_old_sys_ver = true;
79283dda 2584}
a2a05159 2585DEFINE_VIRT_MACHINE(2, 11)
79283dda 2586
f22ab6cb
EA
2587static void virt_machine_2_10_options(MachineClass *mc)
2588{
79283dda 2589 virt_machine_2_11_options(mc);
503224f4 2590 compat_props_add(mc->compat_props, hw_compat_2_10, hw_compat_2_10_len);
846690de
PM
2591 /* before 2.11 we never faulted accesses to bad addresses */
2592 mc->ignore_memory_transaction_failures = true;
f22ab6cb 2593}
79283dda 2594DEFINE_VIRT_MACHINE(2, 10)
f22ab6cb 2595
e353aac5
PM
2596static void virt_machine_2_9_options(MachineClass *mc)
2597{
f22ab6cb 2598 virt_machine_2_10_options(mc);
3e803152 2599 compat_props_add(mc->compat_props, hw_compat_2_9, hw_compat_2_9_len);
e353aac5 2600}
f22ab6cb 2601DEFINE_VIRT_MACHINE(2, 9)
e353aac5 2602
96b0439b
AJ
2603static void virt_machine_2_8_options(MachineClass *mc)
2604{
156bc9a5
PM
2605 VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
2606
e353aac5 2607 virt_machine_2_9_options(mc);
edc24ccd 2608 compat_props_add(mc->compat_props, hw_compat_2_8, hw_compat_2_8_len);
156bc9a5
PM
2609 /* For 2.8 and earlier we falsely claimed in the DT that
2610 * our timers were edge-triggered, not level-triggered.
2611 */
2612 vmc->claim_edge_triggered_timers = true;
96b0439b 2613}
e353aac5 2614DEFINE_VIRT_MACHINE(2, 8)
96b0439b 2615
1287f2b3
AJ
2616static void virt_machine_2_7_options(MachineClass *mc)
2617{
2231f69b
AJ
2618 VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
2619
96b0439b 2620 virt_machine_2_8_options(mc);
5a995064 2621 compat_props_add(mc->compat_props, hw_compat_2_7, hw_compat_2_7_len);
2231f69b
AJ
2622 /* ITS was introduced with 2.8 */
2623 vmc->no_its = true;
a2519ad1
PM
2624 /* Stick with 1K pages for migration compatibility */
2625 mc->minimum_page_bits = 0;
1287f2b3 2626}
96b0439b 2627DEFINE_VIRT_MACHINE(2, 7)
1287f2b3 2628
ab093c3c 2629static void virt_machine_2_6_options(MachineClass *mc)
c2919690 2630{
95eb49c8
AJ
2631 VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
2632
1287f2b3 2633 virt_machine_2_7_options(mc);
ff8f261f 2634 compat_props_add(mc->compat_props, hw_compat_2_6, hw_compat_2_6_len);
95eb49c8 2635 vmc->disallow_affinity_adjustment = true;
1141d1eb
WH
2636 /* Disable PMU for 2.6 as PMU support was first introduced in 2.7 */
2637 vmc->no_pmu = true;
c2919690 2638}
1287f2b3 2639DEFINE_VIRT_MACHINE(2, 6)