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