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