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