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