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