xref: /openbmc/qemu/hw/arm/virt.c (revision c964b660)
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 "hw/sysbus.h"
33 #include "hw/arm/arm.h"
34 #include "hw/arm/primecell.h"
35 #include "hw/arm/virt.h"
36 #include "hw/devices.h"
37 #include "net/net.h"
38 #include "sysemu/block-backend.h"
39 #include "sysemu/device_tree.h"
40 #include "sysemu/sysemu.h"
41 #include "sysemu/kvm.h"
42 #include "hw/boards.h"
43 #include "hw/loader.h"
44 #include "exec/address-spaces.h"
45 #include "qemu/bitops.h"
46 #include "qemu/error-report.h"
47 #include "hw/pci-host/gpex.h"
48 #include "hw/arm/virt-acpi-build.h"
49 #include "hw/arm/sysbus-fdt.h"
50 #include "hw/platform-bus.h"
51 #include "hw/arm/fdt.h"
52 #include "hw/intc/arm_gic_common.h"
53 #include "kvm_arm.h"
54 #include "hw/smbios/smbios.h"
55 #include "qapi/visitor.h"
56 #include "standard-headers/linux/input.h"
57 
58 /* Number of external interrupt lines to configure the GIC with */
59 #define NUM_IRQS 256
60 
61 #define PLATFORM_BUS_NUM_IRQS 64
62 
63 static ARMPlatformBusSystemParams platform_bus_params;
64 
65 typedef struct VirtBoardInfo {
66     struct arm_boot_info bootinfo;
67     const char *cpu_model;
68     const MemMapEntry *memmap;
69     const int *irqmap;
70     int smp_cpus;
71     void *fdt;
72     int fdt_size;
73     uint32_t clock_phandle;
74     uint32_t gic_phandle;
75     uint32_t v2m_phandle;
76 } VirtBoardInfo;
77 
78 typedef struct {
79     MachineClass parent;
80     VirtBoardInfo *daughterboard;
81 } VirtMachineClass;
82 
83 typedef struct {
84     MachineState parent;
85     bool secure;
86     bool highmem;
87     int32_t gic_version;
88 } VirtMachineState;
89 
90 #define TYPE_VIRT_MACHINE   MACHINE_TYPE_NAME("virt")
91 #define VIRT_MACHINE(obj) \
92     OBJECT_CHECK(VirtMachineState, (obj), TYPE_VIRT_MACHINE)
93 #define VIRT_MACHINE_GET_CLASS(obj) \
94     OBJECT_GET_CLASS(VirtMachineClass, obj, TYPE_VIRT_MACHINE)
95 #define VIRT_MACHINE_CLASS(klass) \
96     OBJECT_CLASS_CHECK(VirtMachineClass, klass, TYPE_VIRT_MACHINE)
97 
98 /* Addresses and sizes of our components.
99  * 0..128MB is space for a flash device so we can run bootrom code such as UEFI.
100  * 128MB..256MB is used for miscellaneous device I/O.
101  * 256MB..1GB is reserved for possible future PCI support (ie where the
102  * PCI memory window will go if we add a PCI host controller).
103  * 1GB and up is RAM (which may happily spill over into the
104  * high memory region beyond 4GB).
105  * This represents a compromise between how much RAM can be given to
106  * a 32 bit VM and leaving space for expansion and in particular for PCI.
107  * Note that devices should generally be placed at multiples of 0x10000,
108  * to accommodate guests using 64K pages.
109  */
110 static const MemMapEntry a15memmap[] = {
111     /* Space up to 0x8000000 is reserved for a boot ROM */
112     [VIRT_FLASH] =              {          0, 0x08000000 },
113     [VIRT_CPUPERIPHS] =         { 0x08000000, 0x00020000 },
114     /* GIC distributor and CPU interfaces sit inside the CPU peripheral space */
115     [VIRT_GIC_DIST] =           { 0x08000000, 0x00010000 },
116     [VIRT_GIC_CPU] =            { 0x08010000, 0x00010000 },
117     [VIRT_GIC_V2M] =            { 0x08020000, 0x00001000 },
118     /* The space in between here is reserved for GICv3 CPU/vCPU/HYP */
119     [VIRT_GIC_ITS] =            { 0x08080000, 0x00020000 },
120     /* This redistributor space allows up to 2*64kB*123 CPUs */
121     [VIRT_GIC_REDIST] =         { 0x080A0000, 0x00F60000 },
122     [VIRT_UART] =               { 0x09000000, 0x00001000 },
123     [VIRT_RTC] =                { 0x09010000, 0x00001000 },
124     [VIRT_FW_CFG] =             { 0x09020000, 0x00000018 },
125     [VIRT_GPIO] =               { 0x09030000, 0x00001000 },
126     [VIRT_SECURE_UART] =        { 0x09040000, 0x00001000 },
127     [VIRT_MMIO] =               { 0x0a000000, 0x00000200 },
128     /* ...repeating for a total of NUM_VIRTIO_TRANSPORTS, each of that size */
129     [VIRT_PLATFORM_BUS] =       { 0x0c000000, 0x02000000 },
130     [VIRT_PCIE_MMIO] =          { 0x10000000, 0x2eff0000 },
131     [VIRT_PCIE_PIO] =           { 0x3eff0000, 0x00010000 },
132     [VIRT_PCIE_ECAM] =          { 0x3f000000, 0x01000000 },
133     [VIRT_MEM] =                { 0x40000000, 30ULL * 1024 * 1024 * 1024 },
134     /* Second PCIe window, 512GB wide at the 512GB boundary */
135     [VIRT_PCIE_MMIO_HIGH] =   { 0x8000000000ULL, 0x8000000000ULL },
136 };
137 
138 static const int a15irqmap[] = {
139     [VIRT_UART] = 1,
140     [VIRT_RTC] = 2,
141     [VIRT_PCIE] = 3, /* ... to 6 */
142     [VIRT_GPIO] = 7,
143     [VIRT_SECURE_UART] = 8,
144     [VIRT_MMIO] = 16, /* ...to 16 + NUM_VIRTIO_TRANSPORTS - 1 */
145     [VIRT_GIC_V2M] = 48, /* ...to 48 + NUM_GICV2M_SPIS - 1 */
146     [VIRT_PLATFORM_BUS] = 112, /* ...to 112 + PLATFORM_BUS_NUM_IRQS -1 */
147 };
148 
149 static VirtBoardInfo machines[] = {
150     {
151         .cpu_model = "cortex-a15",
152         .memmap = a15memmap,
153         .irqmap = a15irqmap,
154     },
155     {
156         .cpu_model = "cortex-a53",
157         .memmap = a15memmap,
158         .irqmap = a15irqmap,
159     },
160     {
161         .cpu_model = "cortex-a57",
162         .memmap = a15memmap,
163         .irqmap = a15irqmap,
164     },
165     {
166         .cpu_model = "host",
167         .memmap = a15memmap,
168         .irqmap = a15irqmap,
169     },
170 };
171 
172 static VirtBoardInfo *find_machine_info(const char *cpu)
173 {
174     int i;
175 
176     for (i = 0; i < ARRAY_SIZE(machines); i++) {
177         if (strcmp(cpu, machines[i].cpu_model) == 0) {
178             return &machines[i];
179         }
180     }
181     return NULL;
182 }
183 
184 static void create_fdt(VirtBoardInfo *vbi)
185 {
186     void *fdt = create_device_tree(&vbi->fdt_size);
187 
188     if (!fdt) {
189         error_report("create_device_tree() failed");
190         exit(1);
191     }
192 
193     vbi->fdt = fdt;
194 
195     /* Header */
196     qemu_fdt_setprop_string(fdt, "/", "compatible", "linux,dummy-virt");
197     qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2);
198     qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2);
199 
200     /*
201      * /chosen and /memory nodes must exist for load_dtb
202      * to fill in necessary properties later
203      */
204     qemu_fdt_add_subnode(fdt, "/chosen");
205     qemu_fdt_add_subnode(fdt, "/memory");
206     qemu_fdt_setprop_string(fdt, "/memory", "device_type", "memory");
207 
208     /* Clock node, for the benefit of the UART. The kernel device tree
209      * binding documentation claims the PL011 node clock properties are
210      * optional but in practice if you omit them the kernel refuses to
211      * probe for the device.
212      */
213     vbi->clock_phandle = qemu_fdt_alloc_phandle(fdt);
214     qemu_fdt_add_subnode(fdt, "/apb-pclk");
215     qemu_fdt_setprop_string(fdt, "/apb-pclk", "compatible", "fixed-clock");
216     qemu_fdt_setprop_cell(fdt, "/apb-pclk", "#clock-cells", 0x0);
217     qemu_fdt_setprop_cell(fdt, "/apb-pclk", "clock-frequency", 24000000);
218     qemu_fdt_setprop_string(fdt, "/apb-pclk", "clock-output-names",
219                                 "clk24mhz");
220     qemu_fdt_setprop_cell(fdt, "/apb-pclk", "phandle", vbi->clock_phandle);
221 
222 }
223 
224 static void fdt_add_psci_node(const VirtBoardInfo *vbi)
225 {
226     uint32_t cpu_suspend_fn;
227     uint32_t cpu_off_fn;
228     uint32_t cpu_on_fn;
229     uint32_t migrate_fn;
230     void *fdt = vbi->fdt;
231     ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(0));
232 
233     qemu_fdt_add_subnode(fdt, "/psci");
234     if (armcpu->psci_version == 2) {
235         const char comp[] = "arm,psci-0.2\0arm,psci";
236         qemu_fdt_setprop(fdt, "/psci", "compatible", comp, sizeof(comp));
237 
238         cpu_off_fn = QEMU_PSCI_0_2_FN_CPU_OFF;
239         if (arm_feature(&armcpu->env, ARM_FEATURE_AARCH64)) {
240             cpu_suspend_fn = QEMU_PSCI_0_2_FN64_CPU_SUSPEND;
241             cpu_on_fn = QEMU_PSCI_0_2_FN64_CPU_ON;
242             migrate_fn = QEMU_PSCI_0_2_FN64_MIGRATE;
243         } else {
244             cpu_suspend_fn = QEMU_PSCI_0_2_FN_CPU_SUSPEND;
245             cpu_on_fn = QEMU_PSCI_0_2_FN_CPU_ON;
246             migrate_fn = QEMU_PSCI_0_2_FN_MIGRATE;
247         }
248     } else {
249         qemu_fdt_setprop_string(fdt, "/psci", "compatible", "arm,psci");
250 
251         cpu_suspend_fn = QEMU_PSCI_0_1_FN_CPU_SUSPEND;
252         cpu_off_fn = QEMU_PSCI_0_1_FN_CPU_OFF;
253         cpu_on_fn = QEMU_PSCI_0_1_FN_CPU_ON;
254         migrate_fn = QEMU_PSCI_0_1_FN_MIGRATE;
255     }
256 
257     /* We adopt the PSCI spec's nomenclature, and use 'conduit' to refer
258      * to the instruction that should be used to invoke PSCI functions.
259      * However, the device tree binding uses 'method' instead, so that is
260      * what we should use here.
261      */
262     qemu_fdt_setprop_string(fdt, "/psci", "method", "hvc");
263 
264     qemu_fdt_setprop_cell(fdt, "/psci", "cpu_suspend", cpu_suspend_fn);
265     qemu_fdt_setprop_cell(fdt, "/psci", "cpu_off", cpu_off_fn);
266     qemu_fdt_setprop_cell(fdt, "/psci", "cpu_on", cpu_on_fn);
267     qemu_fdt_setprop_cell(fdt, "/psci", "migrate", migrate_fn);
268 }
269 
270 static void fdt_add_timer_nodes(const VirtBoardInfo *vbi, int gictype)
271 {
272     /* Note that on A15 h/w these interrupts are level-triggered,
273      * but for the GIC implementation provided by both QEMU and KVM
274      * they are edge-triggered.
275      */
276     ARMCPU *armcpu;
277     uint32_t irqflags = GIC_FDT_IRQ_FLAGS_EDGE_LO_HI;
278 
279     if (gictype == 2) {
280         irqflags = deposit32(irqflags, GIC_FDT_IRQ_PPI_CPU_START,
281                              GIC_FDT_IRQ_PPI_CPU_WIDTH,
282                              (1 << vbi->smp_cpus) - 1);
283     }
284 
285     qemu_fdt_add_subnode(vbi->fdt, "/timer");
286 
287     armcpu = ARM_CPU(qemu_get_cpu(0));
288     if (arm_feature(&armcpu->env, ARM_FEATURE_V8)) {
289         const char compat[] = "arm,armv8-timer\0arm,armv7-timer";
290         qemu_fdt_setprop(vbi->fdt, "/timer", "compatible",
291                          compat, sizeof(compat));
292     } else {
293         qemu_fdt_setprop_string(vbi->fdt, "/timer", "compatible",
294                                 "arm,armv7-timer");
295     }
296     qemu_fdt_setprop(vbi->fdt, "/timer", "always-on", NULL, 0);
297     qemu_fdt_setprop_cells(vbi->fdt, "/timer", "interrupts",
298                        GIC_FDT_IRQ_TYPE_PPI, ARCH_TIMER_S_EL1_IRQ, irqflags,
299                        GIC_FDT_IRQ_TYPE_PPI, ARCH_TIMER_NS_EL1_IRQ, irqflags,
300                        GIC_FDT_IRQ_TYPE_PPI, ARCH_TIMER_VIRT_IRQ, irqflags,
301                        GIC_FDT_IRQ_TYPE_PPI, ARCH_TIMER_NS_EL2_IRQ, irqflags);
302 }
303 
304 static void fdt_add_cpu_nodes(const VirtBoardInfo *vbi)
305 {
306     int cpu;
307     int addr_cells = 1;
308 
309     /*
310      * From Documentation/devicetree/bindings/arm/cpus.txt
311      *  On ARM v8 64-bit systems value should be set to 2,
312      *  that corresponds to the MPIDR_EL1 register size.
313      *  If MPIDR_EL1[63:32] value is equal to 0 on all CPUs
314      *  in the system, #address-cells can be set to 1, since
315      *  MPIDR_EL1[63:32] bits are not used for CPUs
316      *  identification.
317      *
318      *  Here we actually don't know whether our system is 32- or 64-bit one.
319      *  The simplest way to go is to examine affinity IDs of all our CPUs. If
320      *  at least one of them has Aff3 populated, we set #address-cells to 2.
321      */
322     for (cpu = 0; cpu < vbi->smp_cpus; cpu++) {
323         ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(cpu));
324 
325         if (armcpu->mp_affinity & ARM_AFF3_MASK) {
326             addr_cells = 2;
327             break;
328         }
329     }
330 
331     qemu_fdt_add_subnode(vbi->fdt, "/cpus");
332     qemu_fdt_setprop_cell(vbi->fdt, "/cpus", "#address-cells", addr_cells);
333     qemu_fdt_setprop_cell(vbi->fdt, "/cpus", "#size-cells", 0x0);
334 
335     for (cpu = vbi->smp_cpus - 1; cpu >= 0; cpu--) {
336         char *nodename = g_strdup_printf("/cpus/cpu@%d", cpu);
337         ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(cpu));
338 
339         qemu_fdt_add_subnode(vbi->fdt, nodename);
340         qemu_fdt_setprop_string(vbi->fdt, nodename, "device_type", "cpu");
341         qemu_fdt_setprop_string(vbi->fdt, nodename, "compatible",
342                                     armcpu->dtb_compatible);
343 
344         if (vbi->smp_cpus > 1) {
345             qemu_fdt_setprop_string(vbi->fdt, nodename,
346                                         "enable-method", "psci");
347         }
348 
349         if (addr_cells == 2) {
350             qemu_fdt_setprop_u64(vbi->fdt, nodename, "reg",
351                                  armcpu->mp_affinity);
352         } else {
353             qemu_fdt_setprop_cell(vbi->fdt, nodename, "reg",
354                                   armcpu->mp_affinity);
355         }
356 
357         g_free(nodename);
358     }
359 }
360 
361 static void fdt_add_v2m_gic_node(VirtBoardInfo *vbi)
362 {
363     vbi->v2m_phandle = qemu_fdt_alloc_phandle(vbi->fdt);
364     qemu_fdt_add_subnode(vbi->fdt, "/intc/v2m");
365     qemu_fdt_setprop_string(vbi->fdt, "/intc/v2m", "compatible",
366                             "arm,gic-v2m-frame");
367     qemu_fdt_setprop(vbi->fdt, "/intc/v2m", "msi-controller", NULL, 0);
368     qemu_fdt_setprop_sized_cells(vbi->fdt, "/intc/v2m", "reg",
369                                  2, vbi->memmap[VIRT_GIC_V2M].base,
370                                  2, vbi->memmap[VIRT_GIC_V2M].size);
371     qemu_fdt_setprop_cell(vbi->fdt, "/intc/v2m", "phandle", vbi->v2m_phandle);
372 }
373 
374 static void fdt_add_gic_node(VirtBoardInfo *vbi, int type)
375 {
376     vbi->gic_phandle = qemu_fdt_alloc_phandle(vbi->fdt);
377     qemu_fdt_setprop_cell(vbi->fdt, "/", "interrupt-parent", vbi->gic_phandle);
378 
379     qemu_fdt_add_subnode(vbi->fdt, "/intc");
380     qemu_fdt_setprop_cell(vbi->fdt, "/intc", "#interrupt-cells", 3);
381     qemu_fdt_setprop(vbi->fdt, "/intc", "interrupt-controller", NULL, 0);
382     qemu_fdt_setprop_cell(vbi->fdt, "/intc", "#address-cells", 0x2);
383     qemu_fdt_setprop_cell(vbi->fdt, "/intc", "#size-cells", 0x2);
384     qemu_fdt_setprop(vbi->fdt, "/intc", "ranges", NULL, 0);
385     if (type == 3) {
386         qemu_fdt_setprop_string(vbi->fdt, "/intc", "compatible",
387                                 "arm,gic-v3");
388         qemu_fdt_setprop_sized_cells(vbi->fdt, "/intc", "reg",
389                                      2, vbi->memmap[VIRT_GIC_DIST].base,
390                                      2, vbi->memmap[VIRT_GIC_DIST].size,
391                                      2, vbi->memmap[VIRT_GIC_REDIST].base,
392                                      2, vbi->memmap[VIRT_GIC_REDIST].size);
393     } else {
394         /* 'cortex-a15-gic' means 'GIC v2' */
395         qemu_fdt_setprop_string(vbi->fdt, "/intc", "compatible",
396                                 "arm,cortex-a15-gic");
397         qemu_fdt_setprop_sized_cells(vbi->fdt, "/intc", "reg",
398                                       2, vbi->memmap[VIRT_GIC_DIST].base,
399                                       2, vbi->memmap[VIRT_GIC_DIST].size,
400                                       2, vbi->memmap[VIRT_GIC_CPU].base,
401                                       2, vbi->memmap[VIRT_GIC_CPU].size);
402     }
403 
404     qemu_fdt_setprop_cell(vbi->fdt, "/intc", "phandle", vbi->gic_phandle);
405 }
406 
407 static void create_v2m(VirtBoardInfo *vbi, qemu_irq *pic)
408 {
409     int i;
410     int irq = vbi->irqmap[VIRT_GIC_V2M];
411     DeviceState *dev;
412 
413     dev = qdev_create(NULL, "arm-gicv2m");
414     sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, vbi->memmap[VIRT_GIC_V2M].base);
415     qdev_prop_set_uint32(dev, "base-spi", irq);
416     qdev_prop_set_uint32(dev, "num-spi", NUM_GICV2M_SPIS);
417     qdev_init_nofail(dev);
418 
419     for (i = 0; i < NUM_GICV2M_SPIS; i++) {
420         sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, pic[irq + i]);
421     }
422 
423     fdt_add_v2m_gic_node(vbi);
424 }
425 
426 static void create_gic(VirtBoardInfo *vbi, qemu_irq *pic, int type, bool secure)
427 {
428     /* We create a standalone GIC */
429     DeviceState *gicdev;
430     SysBusDevice *gicbusdev;
431     const char *gictype;
432     int i;
433 
434     gictype = (type == 3) ? gicv3_class_name() : gic_class_name();
435 
436     gicdev = qdev_create(NULL, gictype);
437     qdev_prop_set_uint32(gicdev, "revision", type);
438     qdev_prop_set_uint32(gicdev, "num-cpu", smp_cpus);
439     /* Note that the num-irq property counts both internal and external
440      * interrupts; there are always 32 of the former (mandated by GIC spec).
441      */
442     qdev_prop_set_uint32(gicdev, "num-irq", NUM_IRQS + 32);
443     if (!kvm_irqchip_in_kernel()) {
444         qdev_prop_set_bit(gicdev, "has-security-extensions", secure);
445     }
446     qdev_init_nofail(gicdev);
447     gicbusdev = SYS_BUS_DEVICE(gicdev);
448     sysbus_mmio_map(gicbusdev, 0, vbi->memmap[VIRT_GIC_DIST].base);
449     if (type == 3) {
450         sysbus_mmio_map(gicbusdev, 1, vbi->memmap[VIRT_GIC_REDIST].base);
451     } else {
452         sysbus_mmio_map(gicbusdev, 1, vbi->memmap[VIRT_GIC_CPU].base);
453     }
454 
455     /* Wire the outputs from each CPU's generic timer to the
456      * appropriate GIC PPI inputs, and the GIC's IRQ output to
457      * the CPU's IRQ input.
458      */
459     for (i = 0; i < smp_cpus; i++) {
460         DeviceState *cpudev = DEVICE(qemu_get_cpu(i));
461         int ppibase = NUM_IRQS + i * GIC_INTERNAL + GIC_NR_SGIS;
462         int irq;
463         /* Mapping from the output timer irq lines from the CPU to the
464          * GIC PPI inputs we use for the virt board.
465          */
466         const int timer_irq[] = {
467             [GTIMER_PHYS] = ARCH_TIMER_NS_EL1_IRQ,
468             [GTIMER_VIRT] = ARCH_TIMER_VIRT_IRQ,
469             [GTIMER_HYP]  = ARCH_TIMER_NS_EL2_IRQ,
470             [GTIMER_SEC]  = ARCH_TIMER_S_EL1_IRQ,
471         };
472 
473         for (irq = 0; irq < ARRAY_SIZE(timer_irq); irq++) {
474             qdev_connect_gpio_out(cpudev, irq,
475                                   qdev_get_gpio_in(gicdev,
476                                                    ppibase + timer_irq[irq]));
477         }
478 
479         sysbus_connect_irq(gicbusdev, i, qdev_get_gpio_in(cpudev, ARM_CPU_IRQ));
480         sysbus_connect_irq(gicbusdev, i + smp_cpus,
481                            qdev_get_gpio_in(cpudev, ARM_CPU_FIQ));
482     }
483 
484     for (i = 0; i < NUM_IRQS; i++) {
485         pic[i] = qdev_get_gpio_in(gicdev, i);
486     }
487 
488     fdt_add_gic_node(vbi, type);
489 
490     if (type == 2) {
491         create_v2m(vbi, pic);
492     }
493 }
494 
495 static void create_uart(const VirtBoardInfo *vbi, qemu_irq *pic, int uart,
496                         MemoryRegion *mem)
497 {
498     char *nodename;
499     hwaddr base = vbi->memmap[uart].base;
500     hwaddr size = vbi->memmap[uart].size;
501     int irq = vbi->irqmap[uart];
502     const char compat[] = "arm,pl011\0arm,primecell";
503     const char clocknames[] = "uartclk\0apb_pclk";
504     DeviceState *dev = qdev_create(NULL, "pl011");
505     SysBusDevice *s = SYS_BUS_DEVICE(dev);
506 
507     qdev_init_nofail(dev);
508     memory_region_add_subregion(mem, base,
509                                 sysbus_mmio_get_region(s, 0));
510     sysbus_connect_irq(s, 0, pic[irq]);
511 
512     nodename = g_strdup_printf("/pl011@%" PRIx64, base);
513     qemu_fdt_add_subnode(vbi->fdt, nodename);
514     /* Note that we can't use setprop_string because of the embedded NUL */
515     qemu_fdt_setprop(vbi->fdt, nodename, "compatible",
516                          compat, sizeof(compat));
517     qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg",
518                                      2, base, 2, size);
519     qemu_fdt_setprop_cells(vbi->fdt, nodename, "interrupts",
520                                GIC_FDT_IRQ_TYPE_SPI, irq,
521                                GIC_FDT_IRQ_FLAGS_LEVEL_HI);
522     qemu_fdt_setprop_cells(vbi->fdt, nodename, "clocks",
523                                vbi->clock_phandle, vbi->clock_phandle);
524     qemu_fdt_setprop(vbi->fdt, nodename, "clock-names",
525                          clocknames, sizeof(clocknames));
526 
527     if (uart == VIRT_UART) {
528         qemu_fdt_setprop_string(vbi->fdt, "/chosen", "stdout-path", nodename);
529     } else {
530         /* Mark as not usable by the normal world */
531         qemu_fdt_setprop_string(vbi->fdt, nodename, "status", "disabled");
532         qemu_fdt_setprop_string(vbi->fdt, nodename, "secure-status", "okay");
533     }
534 
535     g_free(nodename);
536 }
537 
538 static void create_rtc(const VirtBoardInfo *vbi, qemu_irq *pic)
539 {
540     char *nodename;
541     hwaddr base = vbi->memmap[VIRT_RTC].base;
542     hwaddr size = vbi->memmap[VIRT_RTC].size;
543     int irq = vbi->irqmap[VIRT_RTC];
544     const char compat[] = "arm,pl031\0arm,primecell";
545 
546     sysbus_create_simple("pl031", base, pic[irq]);
547 
548     nodename = g_strdup_printf("/pl031@%" PRIx64, base);
549     qemu_fdt_add_subnode(vbi->fdt, nodename);
550     qemu_fdt_setprop(vbi->fdt, nodename, "compatible", compat, sizeof(compat));
551     qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg",
552                                  2, base, 2, size);
553     qemu_fdt_setprop_cells(vbi->fdt, nodename, "interrupts",
554                            GIC_FDT_IRQ_TYPE_SPI, irq,
555                            GIC_FDT_IRQ_FLAGS_LEVEL_HI);
556     qemu_fdt_setprop_cell(vbi->fdt, nodename, "clocks", vbi->clock_phandle);
557     qemu_fdt_setprop_string(vbi->fdt, nodename, "clock-names", "apb_pclk");
558     g_free(nodename);
559 }
560 
561 static DeviceState *pl061_dev;
562 static void virt_powerdown_req(Notifier *n, void *opaque)
563 {
564     /* use gpio Pin 3 for power button event */
565     qemu_set_irq(qdev_get_gpio_in(pl061_dev, 3), 1);
566 }
567 
568 static Notifier virt_system_powerdown_notifier = {
569     .notify = virt_powerdown_req
570 };
571 
572 static void create_gpio(const VirtBoardInfo *vbi, qemu_irq *pic)
573 {
574     char *nodename;
575     hwaddr base = vbi->memmap[VIRT_GPIO].base;
576     hwaddr size = vbi->memmap[VIRT_GPIO].size;
577     int irq = vbi->irqmap[VIRT_GPIO];
578     const char compat[] = "arm,pl061\0arm,primecell";
579 
580     pl061_dev = sysbus_create_simple("pl061", base, pic[irq]);
581 
582     uint32_t phandle = qemu_fdt_alloc_phandle(vbi->fdt);
583     nodename = g_strdup_printf("/pl061@%" PRIx64, base);
584     qemu_fdt_add_subnode(vbi->fdt, nodename);
585     qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg",
586                                  2, base, 2, size);
587     qemu_fdt_setprop(vbi->fdt, nodename, "compatible", compat, sizeof(compat));
588     qemu_fdt_setprop_cell(vbi->fdt, nodename, "#gpio-cells", 2);
589     qemu_fdt_setprop(vbi->fdt, nodename, "gpio-controller", NULL, 0);
590     qemu_fdt_setprop_cells(vbi->fdt, nodename, "interrupts",
591                            GIC_FDT_IRQ_TYPE_SPI, irq,
592                            GIC_FDT_IRQ_FLAGS_LEVEL_HI);
593     qemu_fdt_setprop_cell(vbi->fdt, nodename, "clocks", vbi->clock_phandle);
594     qemu_fdt_setprop_string(vbi->fdt, nodename, "clock-names", "apb_pclk");
595     qemu_fdt_setprop_cell(vbi->fdt, nodename, "phandle", phandle);
596 
597     qemu_fdt_add_subnode(vbi->fdt, "/gpio-keys");
598     qemu_fdt_setprop_string(vbi->fdt, "/gpio-keys", "compatible", "gpio-keys");
599     qemu_fdt_setprop_cell(vbi->fdt, "/gpio-keys", "#size-cells", 0);
600     qemu_fdt_setprop_cell(vbi->fdt, "/gpio-keys", "#address-cells", 1);
601 
602     qemu_fdt_add_subnode(vbi->fdt, "/gpio-keys/poweroff");
603     qemu_fdt_setprop_string(vbi->fdt, "/gpio-keys/poweroff",
604                             "label", "GPIO Key Poweroff");
605     qemu_fdt_setprop_cell(vbi->fdt, "/gpio-keys/poweroff", "linux,code",
606                           KEY_POWER);
607     qemu_fdt_setprop_cells(vbi->fdt, "/gpio-keys/poweroff",
608                            "gpios", phandle, 3, 0);
609 
610     /* connect powerdown request */
611     qemu_register_powerdown_notifier(&virt_system_powerdown_notifier);
612 
613     g_free(nodename);
614 }
615 
616 static void create_virtio_devices(const VirtBoardInfo *vbi, qemu_irq *pic)
617 {
618     int i;
619     hwaddr size = vbi->memmap[VIRT_MMIO].size;
620 
621     /* We create the transports in forwards order. Since qbus_realize()
622      * prepends (not appends) new child buses, the incrementing loop below will
623      * create a list of virtio-mmio buses with decreasing base addresses.
624      *
625      * When a -device option is processed from the command line,
626      * qbus_find_recursive() picks the next free virtio-mmio bus in forwards
627      * order. The upshot is that -device options in increasing command line
628      * order are mapped to virtio-mmio buses with decreasing base addresses.
629      *
630      * When this code was originally written, that arrangement ensured that the
631      * guest Linux kernel would give the lowest "name" (/dev/vda, eth0, etc) to
632      * the first -device on the command line. (The end-to-end order is a
633      * function of this loop, qbus_realize(), qbus_find_recursive(), and the
634      * guest kernel's name-to-address assignment strategy.)
635      *
636      * Meanwhile, the kernel's traversal seems to have been reversed; see eg.
637      * the message, if not necessarily the code, of commit 70161ff336.
638      * Therefore the loop now establishes the inverse of the original intent.
639      *
640      * Unfortunately, we can't counteract the kernel change by reversing the
641      * loop; it would break existing command lines.
642      *
643      * In any case, the kernel makes no guarantee about the stability of
644      * enumeration order of virtio devices (as demonstrated by it changing
645      * between kernel versions). For reliable and stable identification
646      * of disks users must use UUIDs or similar mechanisms.
647      */
648     for (i = 0; i < NUM_VIRTIO_TRANSPORTS; i++) {
649         int irq = vbi->irqmap[VIRT_MMIO] + i;
650         hwaddr base = vbi->memmap[VIRT_MMIO].base + i * size;
651 
652         sysbus_create_simple("virtio-mmio", base, pic[irq]);
653     }
654 
655     /* We add dtb nodes in reverse order so that they appear in the finished
656      * device tree lowest address first.
657      *
658      * Note that this mapping is independent of the loop above. The previous
659      * loop influences virtio device to virtio transport assignment, whereas
660      * this loop controls how virtio transports are laid out in the dtb.
661      */
662     for (i = NUM_VIRTIO_TRANSPORTS - 1; i >= 0; i--) {
663         char *nodename;
664         int irq = vbi->irqmap[VIRT_MMIO] + i;
665         hwaddr base = vbi->memmap[VIRT_MMIO].base + i * size;
666 
667         nodename = g_strdup_printf("/virtio_mmio@%" PRIx64, base);
668         qemu_fdt_add_subnode(vbi->fdt, nodename);
669         qemu_fdt_setprop_string(vbi->fdt, nodename,
670                                 "compatible", "virtio,mmio");
671         qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg",
672                                      2, base, 2, size);
673         qemu_fdt_setprop_cells(vbi->fdt, nodename, "interrupts",
674                                GIC_FDT_IRQ_TYPE_SPI, irq,
675                                GIC_FDT_IRQ_FLAGS_EDGE_LO_HI);
676         g_free(nodename);
677     }
678 }
679 
680 static void create_one_flash(const char *name, hwaddr flashbase,
681                              hwaddr flashsize)
682 {
683     /* Create and map a single flash device. We use the same
684      * parameters as the flash devices on the Versatile Express board.
685      */
686     DriveInfo *dinfo = drive_get_next(IF_PFLASH);
687     DeviceState *dev = qdev_create(NULL, "cfi.pflash01");
688     const uint64_t sectorlength = 256 * 1024;
689 
690     if (dinfo) {
691         qdev_prop_set_drive(dev, "drive", blk_by_legacy_dinfo(dinfo),
692                             &error_abort);
693     }
694 
695     qdev_prop_set_uint32(dev, "num-blocks", flashsize / sectorlength);
696     qdev_prop_set_uint64(dev, "sector-length", sectorlength);
697     qdev_prop_set_uint8(dev, "width", 4);
698     qdev_prop_set_uint8(dev, "device-width", 2);
699     qdev_prop_set_bit(dev, "big-endian", false);
700     qdev_prop_set_uint16(dev, "id0", 0x89);
701     qdev_prop_set_uint16(dev, "id1", 0x18);
702     qdev_prop_set_uint16(dev, "id2", 0x00);
703     qdev_prop_set_uint16(dev, "id3", 0x00);
704     qdev_prop_set_string(dev, "name", name);
705     qdev_init_nofail(dev);
706 
707     sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, flashbase);
708 }
709 
710 static void create_flash(const VirtBoardInfo *vbi)
711 {
712     /* Create two flash devices to fill the VIRT_FLASH space in the memmap.
713      * Any file passed via -bios goes in the first of these.
714      */
715     hwaddr flashsize = vbi->memmap[VIRT_FLASH].size / 2;
716     hwaddr flashbase = vbi->memmap[VIRT_FLASH].base;
717     char *nodename;
718 
719     if (bios_name) {
720         char *fn;
721         int image_size;
722 
723         if (drive_get(IF_PFLASH, 0, 0)) {
724             error_report("The contents of the first flash device may be "
725                          "specified with -bios or with -drive if=pflash... "
726                          "but you cannot use both options at once");
727             exit(1);
728         }
729         fn = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
730         if (!fn) {
731             error_report("Could not find ROM image '%s'", bios_name);
732             exit(1);
733         }
734         image_size = load_image_targphys(fn, flashbase, flashsize);
735         g_free(fn);
736         if (image_size < 0) {
737             error_report("Could not load ROM image '%s'", bios_name);
738             exit(1);
739         }
740     }
741 
742     create_one_flash("virt.flash0", flashbase, flashsize);
743     create_one_flash("virt.flash1", flashbase + flashsize, flashsize);
744 
745     nodename = g_strdup_printf("/flash@%" PRIx64, flashbase);
746     qemu_fdt_add_subnode(vbi->fdt, nodename);
747     qemu_fdt_setprop_string(vbi->fdt, nodename, "compatible", "cfi-flash");
748     qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg",
749                                  2, flashbase, 2, flashsize,
750                                  2, flashbase + flashsize, 2, flashsize);
751     qemu_fdt_setprop_cell(vbi->fdt, nodename, "bank-width", 4);
752     g_free(nodename);
753 }
754 
755 static void create_fw_cfg(const VirtBoardInfo *vbi, AddressSpace *as)
756 {
757     hwaddr base = vbi->memmap[VIRT_FW_CFG].base;
758     hwaddr size = vbi->memmap[VIRT_FW_CFG].size;
759     char *nodename;
760 
761     fw_cfg_init_mem_wide(base + 8, base, 8, base + 16, as);
762 
763     nodename = g_strdup_printf("/fw-cfg@%" PRIx64, base);
764     qemu_fdt_add_subnode(vbi->fdt, nodename);
765     qemu_fdt_setprop_string(vbi->fdt, nodename,
766                             "compatible", "qemu,fw-cfg-mmio");
767     qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg",
768                                  2, base, 2, size);
769     g_free(nodename);
770 }
771 
772 static void create_pcie_irq_map(const VirtBoardInfo *vbi, uint32_t gic_phandle,
773                                 int first_irq, const char *nodename)
774 {
775     int devfn, pin;
776     uint32_t full_irq_map[4 * 4 * 10] = { 0 };
777     uint32_t *irq_map = full_irq_map;
778 
779     for (devfn = 0; devfn <= 0x18; devfn += 0x8) {
780         for (pin = 0; pin < 4; pin++) {
781             int irq_type = GIC_FDT_IRQ_TYPE_SPI;
782             int irq_nr = first_irq + ((pin + PCI_SLOT(devfn)) % PCI_NUM_PINS);
783             int irq_level = GIC_FDT_IRQ_FLAGS_LEVEL_HI;
784             int i;
785 
786             uint32_t map[] = {
787                 devfn << 8, 0, 0,                           /* devfn */
788                 pin + 1,                                    /* PCI pin */
789                 gic_phandle, 0, 0, irq_type, irq_nr, irq_level }; /* GIC irq */
790 
791             /* Convert map to big endian */
792             for (i = 0; i < 10; i++) {
793                 irq_map[i] = cpu_to_be32(map[i]);
794             }
795             irq_map += 10;
796         }
797     }
798 
799     qemu_fdt_setprop(vbi->fdt, nodename, "interrupt-map",
800                      full_irq_map, sizeof(full_irq_map));
801 
802     qemu_fdt_setprop_cells(vbi->fdt, nodename, "interrupt-map-mask",
803                            0x1800, 0, 0, /* devfn (PCI_SLOT(3)) */
804                            0x7           /* PCI irq */);
805 }
806 
807 static void create_pcie(const VirtBoardInfo *vbi, qemu_irq *pic,
808                         bool use_highmem)
809 {
810     hwaddr base_mmio = vbi->memmap[VIRT_PCIE_MMIO].base;
811     hwaddr size_mmio = vbi->memmap[VIRT_PCIE_MMIO].size;
812     hwaddr base_mmio_high = vbi->memmap[VIRT_PCIE_MMIO_HIGH].base;
813     hwaddr size_mmio_high = vbi->memmap[VIRT_PCIE_MMIO_HIGH].size;
814     hwaddr base_pio = vbi->memmap[VIRT_PCIE_PIO].base;
815     hwaddr size_pio = vbi->memmap[VIRT_PCIE_PIO].size;
816     hwaddr base_ecam = vbi->memmap[VIRT_PCIE_ECAM].base;
817     hwaddr size_ecam = vbi->memmap[VIRT_PCIE_ECAM].size;
818     hwaddr base = base_mmio;
819     int nr_pcie_buses = size_ecam / PCIE_MMCFG_SIZE_MIN;
820     int irq = vbi->irqmap[VIRT_PCIE];
821     MemoryRegion *mmio_alias;
822     MemoryRegion *mmio_reg;
823     MemoryRegion *ecam_alias;
824     MemoryRegion *ecam_reg;
825     DeviceState *dev;
826     char *nodename;
827     int i;
828     PCIHostState *pci;
829 
830     dev = qdev_create(NULL, TYPE_GPEX_HOST);
831     qdev_init_nofail(dev);
832 
833     /* Map only the first size_ecam bytes of ECAM space */
834     ecam_alias = g_new0(MemoryRegion, 1);
835     ecam_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0);
836     memory_region_init_alias(ecam_alias, OBJECT(dev), "pcie-ecam",
837                              ecam_reg, 0, size_ecam);
838     memory_region_add_subregion(get_system_memory(), base_ecam, ecam_alias);
839 
840     /* Map the MMIO window into system address space so as to expose
841      * the section of PCI MMIO space which starts at the same base address
842      * (ie 1:1 mapping for that part of PCI MMIO space visible through
843      * the window).
844      */
845     mmio_alias = g_new0(MemoryRegion, 1);
846     mmio_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 1);
847     memory_region_init_alias(mmio_alias, OBJECT(dev), "pcie-mmio",
848                              mmio_reg, base_mmio, size_mmio);
849     memory_region_add_subregion(get_system_memory(), base_mmio, mmio_alias);
850 
851     if (use_highmem) {
852         /* Map high MMIO space */
853         MemoryRegion *high_mmio_alias = g_new0(MemoryRegion, 1);
854 
855         memory_region_init_alias(high_mmio_alias, OBJECT(dev), "pcie-mmio-high",
856                                  mmio_reg, base_mmio_high, size_mmio_high);
857         memory_region_add_subregion(get_system_memory(), base_mmio_high,
858                                     high_mmio_alias);
859     }
860 
861     /* Map IO port space */
862     sysbus_mmio_map(SYS_BUS_DEVICE(dev), 2, base_pio);
863 
864     for (i = 0; i < GPEX_NUM_IRQS; i++) {
865         sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, pic[irq + i]);
866     }
867 
868     pci = PCI_HOST_BRIDGE(dev);
869     if (pci->bus) {
870         for (i = 0; i < nb_nics; i++) {
871             NICInfo *nd = &nd_table[i];
872 
873             if (!nd->model) {
874                 nd->model = g_strdup("virtio");
875             }
876 
877             pci_nic_init_nofail(nd, pci->bus, nd->model, NULL);
878         }
879     }
880 
881     nodename = g_strdup_printf("/pcie@%" PRIx64, base);
882     qemu_fdt_add_subnode(vbi->fdt, nodename);
883     qemu_fdt_setprop_string(vbi->fdt, nodename,
884                             "compatible", "pci-host-ecam-generic");
885     qemu_fdt_setprop_string(vbi->fdt, nodename, "device_type", "pci");
886     qemu_fdt_setprop_cell(vbi->fdt, nodename, "#address-cells", 3);
887     qemu_fdt_setprop_cell(vbi->fdt, nodename, "#size-cells", 2);
888     qemu_fdt_setprop_cells(vbi->fdt, nodename, "bus-range", 0,
889                            nr_pcie_buses - 1);
890 
891     if (vbi->v2m_phandle) {
892         qemu_fdt_setprop_cells(vbi->fdt, nodename, "msi-parent",
893                                vbi->v2m_phandle);
894     }
895 
896     qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "reg",
897                                  2, base_ecam, 2, size_ecam);
898 
899     if (use_highmem) {
900         qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "ranges",
901                                      1, FDT_PCI_RANGE_IOPORT, 2, 0,
902                                      2, base_pio, 2, size_pio,
903                                      1, FDT_PCI_RANGE_MMIO, 2, base_mmio,
904                                      2, base_mmio, 2, size_mmio,
905                                      1, FDT_PCI_RANGE_MMIO_64BIT,
906                                      2, base_mmio_high,
907                                      2, base_mmio_high, 2, size_mmio_high);
908     } else {
909         qemu_fdt_setprop_sized_cells(vbi->fdt, nodename, "ranges",
910                                      1, FDT_PCI_RANGE_IOPORT, 2, 0,
911                                      2, base_pio, 2, size_pio,
912                                      1, FDT_PCI_RANGE_MMIO, 2, base_mmio,
913                                      2, base_mmio, 2, size_mmio);
914     }
915 
916     qemu_fdt_setprop_cell(vbi->fdt, nodename, "#interrupt-cells", 1);
917     create_pcie_irq_map(vbi, vbi->gic_phandle, irq, nodename);
918 
919     g_free(nodename);
920 }
921 
922 static void create_platform_bus(VirtBoardInfo *vbi, qemu_irq *pic)
923 {
924     DeviceState *dev;
925     SysBusDevice *s;
926     int i;
927     ARMPlatformBusFDTParams *fdt_params = g_new(ARMPlatformBusFDTParams, 1);
928     MemoryRegion *sysmem = get_system_memory();
929 
930     platform_bus_params.platform_bus_base = vbi->memmap[VIRT_PLATFORM_BUS].base;
931     platform_bus_params.platform_bus_size = vbi->memmap[VIRT_PLATFORM_BUS].size;
932     platform_bus_params.platform_bus_first_irq = vbi->irqmap[VIRT_PLATFORM_BUS];
933     platform_bus_params.platform_bus_num_irqs = PLATFORM_BUS_NUM_IRQS;
934 
935     fdt_params->system_params = &platform_bus_params;
936     fdt_params->binfo = &vbi->bootinfo;
937     fdt_params->intc = "/intc";
938     /*
939      * register a machine init done notifier that creates the device tree
940      * nodes of the platform bus and its children dynamic sysbus devices
941      */
942     arm_register_platform_bus_fdt_creator(fdt_params);
943 
944     dev = qdev_create(NULL, TYPE_PLATFORM_BUS_DEVICE);
945     dev->id = TYPE_PLATFORM_BUS_DEVICE;
946     qdev_prop_set_uint32(dev, "num_irqs",
947         platform_bus_params.platform_bus_num_irqs);
948     qdev_prop_set_uint32(dev, "mmio_size",
949         platform_bus_params.platform_bus_size);
950     qdev_init_nofail(dev);
951     s = SYS_BUS_DEVICE(dev);
952 
953     for (i = 0; i < platform_bus_params.platform_bus_num_irqs; i++) {
954         int irqn = platform_bus_params.platform_bus_first_irq + i;
955         sysbus_connect_irq(s, i, pic[irqn]);
956     }
957 
958     memory_region_add_subregion(sysmem,
959                                 platform_bus_params.platform_bus_base,
960                                 sysbus_mmio_get_region(s, 0));
961 }
962 
963 static void *machvirt_dtb(const struct arm_boot_info *binfo, int *fdt_size)
964 {
965     const VirtBoardInfo *board = (const VirtBoardInfo *)binfo;
966 
967     *fdt_size = board->fdt_size;
968     return board->fdt;
969 }
970 
971 static void virt_build_smbios(VirtGuestInfo *guest_info)
972 {
973     FWCfgState *fw_cfg = guest_info->fw_cfg;
974     uint8_t *smbios_tables, *smbios_anchor;
975     size_t smbios_tables_len, smbios_anchor_len;
976     const char *product = "QEMU Virtual Machine";
977 
978     if (!fw_cfg) {
979         return;
980     }
981 
982     if (kvm_enabled()) {
983         product = "KVM Virtual Machine";
984     }
985 
986     smbios_set_defaults("QEMU", product,
987                         "1.0", false, true, SMBIOS_ENTRY_POINT_30);
988 
989     smbios_get_tables(NULL, 0, &smbios_tables, &smbios_tables_len,
990                       &smbios_anchor, &smbios_anchor_len);
991 
992     if (smbios_anchor) {
993         fw_cfg_add_file(fw_cfg, "etc/smbios/smbios-tables",
994                         smbios_tables, smbios_tables_len);
995         fw_cfg_add_file(fw_cfg, "etc/smbios/smbios-anchor",
996                         smbios_anchor, smbios_anchor_len);
997     }
998 }
999 
1000 static
1001 void virt_guest_info_machine_done(Notifier *notifier, void *data)
1002 {
1003     VirtGuestInfoState *guest_info_state = container_of(notifier,
1004                                               VirtGuestInfoState, machine_done);
1005     virt_acpi_setup(&guest_info_state->info);
1006     virt_build_smbios(&guest_info_state->info);
1007 }
1008 
1009 static void machvirt_init(MachineState *machine)
1010 {
1011     VirtMachineState *vms = VIRT_MACHINE(machine);
1012     qemu_irq pic[NUM_IRQS];
1013     MemoryRegion *sysmem = get_system_memory();
1014     MemoryRegion *secure_sysmem = NULL;
1015     int gic_version = vms->gic_version;
1016     int n, virt_max_cpus;
1017     MemoryRegion *ram = g_new(MemoryRegion, 1);
1018     const char *cpu_model = machine->cpu_model;
1019     VirtBoardInfo *vbi;
1020     VirtGuestInfoState *guest_info_state = g_malloc0(sizeof *guest_info_state);
1021     VirtGuestInfo *guest_info = &guest_info_state->info;
1022     char **cpustr;
1023 
1024     if (!cpu_model) {
1025         cpu_model = "cortex-a15";
1026     }
1027 
1028     /* We can probe only here because during property set
1029      * KVM is not available yet
1030      */
1031     if (!gic_version) {
1032         gic_version = kvm_arm_vgic_probe();
1033         if (!gic_version) {
1034             error_report("Unable to determine GIC version supported by host");
1035             error_printf("KVM acceleration is probably not supported\n");
1036             exit(1);
1037         }
1038     }
1039 
1040     /* Separate the actual CPU model name from any appended features */
1041     cpustr = g_strsplit(cpu_model, ",", 2);
1042 
1043     vbi = find_machine_info(cpustr[0]);
1044 
1045     if (!vbi) {
1046         error_report("mach-virt: CPU %s not supported", cpustr[0]);
1047         exit(1);
1048     }
1049 
1050     /* The maximum number of CPUs depends on the GIC version, or on how
1051      * many redistributors we can fit into the memory map.
1052      */
1053     if (gic_version == 3) {
1054         virt_max_cpus = vbi->memmap[VIRT_GIC_REDIST].size / 0x20000;
1055     } else {
1056         virt_max_cpus = GIC_NCPU;
1057     }
1058 
1059     if (max_cpus > virt_max_cpus) {
1060         error_report("Number of SMP CPUs requested (%d) exceeds max CPUs "
1061                      "supported by machine 'mach-virt' (%d)",
1062                      max_cpus, virt_max_cpus);
1063         exit(1);
1064     }
1065 
1066     vbi->smp_cpus = smp_cpus;
1067 
1068     if (machine->ram_size > vbi->memmap[VIRT_MEM].size) {
1069         error_report("mach-virt: cannot model more than 30GB RAM");
1070         exit(1);
1071     }
1072 
1073     if (vms->secure) {
1074         if (kvm_enabled()) {
1075             error_report("mach-virt: KVM does not support Security extensions");
1076             exit(1);
1077         }
1078 
1079         /* The Secure view of the world is the same as the NonSecure,
1080          * but with a few extra devices. Create it as a container region
1081          * containing the system memory at low priority; any secure-only
1082          * devices go in at higher priority and take precedence.
1083          */
1084         secure_sysmem = g_new(MemoryRegion, 1);
1085         memory_region_init(secure_sysmem, OBJECT(machine), "secure-memory",
1086                            UINT64_MAX);
1087         memory_region_add_subregion_overlap(secure_sysmem, 0, sysmem, -1);
1088     }
1089 
1090     create_fdt(vbi);
1091 
1092     for (n = 0; n < smp_cpus; n++) {
1093         ObjectClass *oc = cpu_class_by_name(TYPE_ARM_CPU, cpustr[0]);
1094         CPUClass *cc = CPU_CLASS(oc);
1095         Object *cpuobj;
1096         Error *err = NULL;
1097         char *cpuopts = g_strdup(cpustr[1]);
1098 
1099         if (!oc) {
1100             error_report("Unable to find CPU definition");
1101             exit(1);
1102         }
1103         cpuobj = object_new(object_class_get_name(oc));
1104 
1105         /* Handle any CPU options specified by the user */
1106         cc->parse_features(CPU(cpuobj), cpuopts, &err);
1107         g_free(cpuopts);
1108         if (err) {
1109             error_report_err(err);
1110             exit(1);
1111         }
1112 
1113         if (!vms->secure) {
1114             object_property_set_bool(cpuobj, false, "has_el3", NULL);
1115         }
1116 
1117         object_property_set_int(cpuobj, QEMU_PSCI_CONDUIT_HVC, "psci-conduit",
1118                                 NULL);
1119 
1120         /* Secondary CPUs start in PSCI powered-down state */
1121         if (n > 0) {
1122             object_property_set_bool(cpuobj, true, "start-powered-off", NULL);
1123         }
1124 
1125         if (object_property_find(cpuobj, "reset-cbar", NULL)) {
1126             object_property_set_int(cpuobj, vbi->memmap[VIRT_CPUPERIPHS].base,
1127                                     "reset-cbar", &error_abort);
1128         }
1129 
1130         object_property_set_link(cpuobj, OBJECT(sysmem), "memory",
1131                                  &error_abort);
1132         if (vms->secure) {
1133             object_property_set_link(cpuobj, OBJECT(secure_sysmem),
1134                                      "secure-memory", &error_abort);
1135         }
1136 
1137         object_property_set_bool(cpuobj, true, "realized", NULL);
1138     }
1139     g_strfreev(cpustr);
1140     fdt_add_timer_nodes(vbi, gic_version);
1141     fdt_add_cpu_nodes(vbi);
1142     fdt_add_psci_node(vbi);
1143 
1144     memory_region_allocate_system_memory(ram, NULL, "mach-virt.ram",
1145                                          machine->ram_size);
1146     memory_region_add_subregion(sysmem, vbi->memmap[VIRT_MEM].base, ram);
1147 
1148     create_flash(vbi);
1149 
1150     create_gic(vbi, pic, gic_version, vms->secure);
1151 
1152     create_uart(vbi, pic, VIRT_UART, sysmem);
1153 
1154     if (vms->secure) {
1155         create_uart(vbi, pic, VIRT_SECURE_UART, secure_sysmem);
1156     }
1157 
1158     create_rtc(vbi, pic);
1159 
1160     create_pcie(vbi, pic, vms->highmem);
1161 
1162     create_gpio(vbi, pic);
1163 
1164     /* Create mmio transports, so the user can create virtio backends
1165      * (which will be automatically plugged in to the transports). If
1166      * no backend is created the transport will just sit harmlessly idle.
1167      */
1168     create_virtio_devices(vbi, pic);
1169 
1170     create_fw_cfg(vbi, &address_space_memory);
1171     rom_set_fw(fw_cfg_find());
1172 
1173     guest_info->smp_cpus = smp_cpus;
1174     guest_info->fw_cfg = fw_cfg_find();
1175     guest_info->memmap = vbi->memmap;
1176     guest_info->irqmap = vbi->irqmap;
1177     guest_info->use_highmem = vms->highmem;
1178     guest_info->gic_version = gic_version;
1179     guest_info_state->machine_done.notify = virt_guest_info_machine_done;
1180     qemu_add_machine_init_done_notifier(&guest_info_state->machine_done);
1181 
1182     vbi->bootinfo.ram_size = machine->ram_size;
1183     vbi->bootinfo.kernel_filename = machine->kernel_filename;
1184     vbi->bootinfo.kernel_cmdline = machine->kernel_cmdline;
1185     vbi->bootinfo.initrd_filename = machine->initrd_filename;
1186     vbi->bootinfo.nb_cpus = smp_cpus;
1187     vbi->bootinfo.board_id = -1;
1188     vbi->bootinfo.loader_start = vbi->memmap[VIRT_MEM].base;
1189     vbi->bootinfo.get_dtb = machvirt_dtb;
1190     vbi->bootinfo.firmware_loaded = bios_name || drive_get(IF_PFLASH, 0, 0);
1191     arm_load_kernel(ARM_CPU(first_cpu), &vbi->bootinfo);
1192 
1193     /*
1194      * arm_load_kernel machine init done notifier registration must
1195      * happen before the platform_bus_create call. In this latter,
1196      * another notifier is registered which adds platform bus nodes.
1197      * Notifiers are executed in registration reverse order.
1198      */
1199     create_platform_bus(vbi, pic);
1200 }
1201 
1202 static bool virt_get_secure(Object *obj, Error **errp)
1203 {
1204     VirtMachineState *vms = VIRT_MACHINE(obj);
1205 
1206     return vms->secure;
1207 }
1208 
1209 static void virt_set_secure(Object *obj, bool value, Error **errp)
1210 {
1211     VirtMachineState *vms = VIRT_MACHINE(obj);
1212 
1213     vms->secure = value;
1214 }
1215 
1216 static bool virt_get_highmem(Object *obj, Error **errp)
1217 {
1218     VirtMachineState *vms = VIRT_MACHINE(obj);
1219 
1220     return vms->highmem;
1221 }
1222 
1223 static void virt_set_highmem(Object *obj, bool value, Error **errp)
1224 {
1225     VirtMachineState *vms = VIRT_MACHINE(obj);
1226 
1227     vms->highmem = value;
1228 }
1229 
1230 static char *virt_get_gic_version(Object *obj, Error **errp)
1231 {
1232     VirtMachineState *vms = VIRT_MACHINE(obj);
1233     const char *val = vms->gic_version == 3 ? "3" : "2";
1234 
1235     return g_strdup(val);
1236 }
1237 
1238 static void virt_set_gic_version(Object *obj, const char *value, Error **errp)
1239 {
1240     VirtMachineState *vms = VIRT_MACHINE(obj);
1241 
1242     if (!strcmp(value, "3")) {
1243         vms->gic_version = 3;
1244     } else if (!strcmp(value, "2")) {
1245         vms->gic_version = 2;
1246     } else if (!strcmp(value, "host")) {
1247         vms->gic_version = 0; /* Will probe later */
1248     } else {
1249         error_setg(errp, "Invalid gic-version value");
1250         error_append_hint(errp, "Valid values are 3, 2, host.\n");
1251     }
1252 }
1253 
1254 static void virt_instance_init(Object *obj)
1255 {
1256     VirtMachineState *vms = VIRT_MACHINE(obj);
1257 
1258     /* EL3 is disabled by default on virt: this makes us consistent
1259      * between KVM and TCG for this board, and it also allows us to
1260      * boot UEFI blobs which assume no TrustZone support.
1261      */
1262     vms->secure = false;
1263     object_property_add_bool(obj, "secure", virt_get_secure,
1264                              virt_set_secure, NULL);
1265     object_property_set_description(obj, "secure",
1266                                     "Set on/off to enable/disable the ARM "
1267                                     "Security Extensions (TrustZone)",
1268                                     NULL);
1269 
1270     /* High memory is enabled by default */
1271     vms->highmem = true;
1272     object_property_add_bool(obj, "highmem", virt_get_highmem,
1273                              virt_set_highmem, NULL);
1274     object_property_set_description(obj, "highmem",
1275                                     "Set on/off to enable/disable using "
1276                                     "physical address space above 32 bits",
1277                                     NULL);
1278     /* Default GIC type is v2 */
1279     vms->gic_version = 2;
1280     object_property_add_str(obj, "gic-version", virt_get_gic_version,
1281                         virt_set_gic_version, NULL);
1282     object_property_set_description(obj, "gic-version",
1283                                     "Set GIC version. "
1284                                     "Valid values are 2, 3 and host", NULL);
1285 }
1286 
1287 static void virt_class_init(ObjectClass *oc, void *data)
1288 {
1289     MachineClass *mc = MACHINE_CLASS(oc);
1290 
1291     mc->desc = "ARM Virtual Machine",
1292     mc->init = machvirt_init;
1293     /* Start max_cpus at the maximum QEMU supports. We'll further restrict
1294      * it later in machvirt_init, where we have more information about the
1295      * configuration of the particular instance.
1296      */
1297     mc->max_cpus = MAX_CPUMASK_BITS;
1298     mc->has_dynamic_sysbus = true;
1299     mc->block_default_type = IF_VIRTIO;
1300     mc->no_cdrom = 1;
1301     mc->pci_allow_0_address = true;
1302 }
1303 
1304 static const TypeInfo machvirt_info = {
1305     .name = TYPE_VIRT_MACHINE,
1306     .parent = TYPE_MACHINE,
1307     .instance_size = sizeof(VirtMachineState),
1308     .instance_init = virt_instance_init,
1309     .class_size = sizeof(VirtMachineClass),
1310     .class_init = virt_class_init,
1311 };
1312 
1313 static void machvirt_machine_init(void)
1314 {
1315     type_register_static(&machvirt_info);
1316 }
1317 
1318 machine_init(machvirt_machine_init);
1319