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