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