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