hw/arm/virt: Check the VCPU PA range in TCG mode
[qemu/kevin.git] / hw / arm / virt.c
blobd725daefdf83d70683b0455bf63dc1a312275923
1 /*
2 * ARM mach-virt emulation
4 * Copyright (c) 2013 Linaro Limited
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.
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.
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/>.
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.
31 #include "qemu/osdep.h"
32 #include "qemu/units.h"
33 #include "qapi/error.h"
34 #include "hw/sysbus.h"
35 #include "hw/arm/arm.h"
36 #include "hw/arm/primecell.h"
37 #include "hw/arm/virt.h"
38 #include "hw/vfio/vfio-calxeda-xgmac.h"
39 #include "hw/vfio/vfio-amd-xgbe.h"
40 #include "hw/display/ramfb.h"
41 #include "hw/devices.h"
42 #include "net/net.h"
43 #include "sysemu/device_tree.h"
44 #include "sysemu/numa.h"
45 #include "sysemu/sysemu.h"
46 #include "sysemu/kvm.h"
47 #include "hw/loader.h"
48 #include "exec/address-spaces.h"
49 #include "qemu/bitops.h"
50 #include "qemu/error-report.h"
51 #include "hw/pci-host/gpex.h"
52 #include "hw/arm/sysbus-fdt.h"
53 #include "hw/platform-bus.h"
54 #include "hw/arm/fdt.h"
55 #include "hw/intc/arm_gic.h"
56 #include "hw/intc/arm_gicv3_common.h"
57 #include "kvm_arm.h"
58 #include "hw/firmware/smbios.h"
59 #include "qapi/visitor.h"
60 #include "standard-headers/linux/input.h"
61 #include "hw/arm/smmuv3.h"
62 #include "hw/acpi/acpi.h"
63 #include "target/arm/internals.h"
65 #define DEFINE_VIRT_MACHINE_LATEST(major, minor, latest) \
66 static void virt_##major##_##minor##_class_init(ObjectClass *oc, \
67 void *data) \
68 { \
69 MachineClass *mc = MACHINE_CLASS(oc); \
70 virt_machine_##major##_##minor##_options(mc); \
71 mc->desc = "QEMU " # major "." # minor " ARM Virtual Machine"; \
72 if (latest) { \
73 mc->alias = "virt"; \
74 } \
75 } \
76 static const TypeInfo machvirt_##major##_##minor##_info = { \
77 .name = MACHINE_TYPE_NAME("virt-" # major "." # minor), \
78 .parent = TYPE_VIRT_MACHINE, \
79 .class_init = virt_##major##_##minor##_class_init, \
80 }; \
81 static void machvirt_machine_##major##_##minor##_init(void) \
82 { \
83 type_register_static(&machvirt_##major##_##minor##_info); \
84 } \
85 type_init(machvirt_machine_##major##_##minor##_init);
87 #define DEFINE_VIRT_MACHINE_AS_LATEST(major, minor) \
88 DEFINE_VIRT_MACHINE_LATEST(major, minor, true)
89 #define DEFINE_VIRT_MACHINE(major, minor) \
90 DEFINE_VIRT_MACHINE_LATEST(major, minor, false)
93 /* Number of external interrupt lines to configure the GIC with */
94 #define NUM_IRQS 256
96 #define PLATFORM_BUS_NUM_IRQS 64
98 /* RAM limit in GB. Since VIRT_MEM starts at the 1GB mark, this means
99 * RAM can go up to the 256GB mark, leaving 256GB of the physical
100 * address space unallocated and free for future use between 256G and 512G.
101 * If we need to provide more RAM to VMs in the future then we need to:
102 * * allocate a second bank of RAM starting at 2TB and working up
103 * * fix the DT and ACPI table generation code in QEMU to correctly
104 * report two split lumps of RAM to the guest
105 * * fix KVM in the host kernel to allow guests with >40 bit address spaces
106 * (We don't want to fill all the way up to 512GB with RAM because
107 * we might want it for non-RAM purposes later. Conversely it seems
108 * reasonable to assume that anybody configuring a VM with a quarter
109 * of a terabyte of RAM will be doing it on a host with more than a
110 * terabyte of physical address space.)
112 #define LEGACY_RAMLIMIT_GB 255
113 #define LEGACY_RAMLIMIT_BYTES (LEGACY_RAMLIMIT_GB * GiB)
115 /* Addresses and sizes of our components.
116 * 0..128MB is space for a flash device so we can run bootrom code such as UEFI.
117 * 128MB..256MB is used for miscellaneous device I/O.
118 * 256MB..1GB is reserved for possible future PCI support (ie where the
119 * PCI memory window will go if we add a PCI host controller).
120 * 1GB and up is RAM (which may happily spill over into the
121 * high memory region beyond 4GB).
122 * This represents a compromise between how much RAM can be given to
123 * a 32 bit VM and leaving space for expansion and in particular for PCI.
124 * Note that devices should generally be placed at multiples of 0x10000,
125 * to accommodate guests using 64K pages.
127 static const MemMapEntry base_memmap[] = {
128 /* Space up to 0x8000000 is reserved for a boot ROM */
129 [VIRT_FLASH] = { 0, 0x08000000 },
130 [VIRT_CPUPERIPHS] = { 0x08000000, 0x00020000 },
131 /* GIC distributor and CPU interfaces sit inside the CPU peripheral space */
132 [VIRT_GIC_DIST] = { 0x08000000, 0x00010000 },
133 [VIRT_GIC_CPU] = { 0x08010000, 0x00010000 },
134 [VIRT_GIC_V2M] = { 0x08020000, 0x00001000 },
135 [VIRT_GIC_HYP] = { 0x08030000, 0x00010000 },
136 [VIRT_GIC_VCPU] = { 0x08040000, 0x00010000 },
137 /* The space in between here is reserved for GICv3 CPU/vCPU/HYP */
138 [VIRT_GIC_ITS] = { 0x08080000, 0x00020000 },
139 /* This redistributor space allows up to 2*64kB*123 CPUs */
140 [VIRT_GIC_REDIST] = { 0x080A0000, 0x00F60000 },
141 [VIRT_UART] = { 0x09000000, 0x00001000 },
142 [VIRT_RTC] = { 0x09010000, 0x00001000 },
143 [VIRT_FW_CFG] = { 0x09020000, 0x00000018 },
144 [VIRT_GPIO] = { 0x09030000, 0x00001000 },
145 [VIRT_SECURE_UART] = { 0x09040000, 0x00001000 },
146 [VIRT_SMMU] = { 0x09050000, 0x00020000 },
147 [VIRT_MMIO] = { 0x0a000000, 0x00000200 },
148 /* ...repeating for a total of NUM_VIRTIO_TRANSPORTS, each of that size */
149 [VIRT_PLATFORM_BUS] = { 0x0c000000, 0x02000000 },
150 [VIRT_SECURE_MEM] = { 0x0e000000, 0x01000000 },
151 [VIRT_PCIE_MMIO] = { 0x10000000, 0x2eff0000 },
152 [VIRT_PCIE_PIO] = { 0x3eff0000, 0x00010000 },
153 [VIRT_PCIE_ECAM] = { 0x3f000000, 0x01000000 },
154 /* Actual RAM size depends on initial RAM and device memory settings */
155 [VIRT_MEM] = { GiB, LEGACY_RAMLIMIT_BYTES },
159 * Highmem IO Regions: This memory map is floating, located after the RAM.
160 * Each MemMapEntry base (GPA) will be dynamically computed, depending on the
161 * top of the RAM, so that its base get the same alignment as the size,
162 * ie. a 512GiB entry will be aligned on a 512GiB boundary. If there is
163 * less than 256GiB of RAM, the floating area starts at the 256GiB mark.
164 * Note the extended_memmap is sized so that it eventually also includes the
165 * base_memmap entries (VIRT_HIGH_GIC_REDIST2 index is greater than the last
166 * index of base_memmap).
168 static MemMapEntry extended_memmap[] = {
169 /* Additional 64 MB redist region (can contain up to 512 redistributors) */
170 [VIRT_HIGH_GIC_REDIST2] = { 0x0, 64 * MiB },
171 [VIRT_HIGH_PCIE_ECAM] = { 0x0, 256 * MiB },
172 /* Second PCIe window */
173 [VIRT_HIGH_PCIE_MMIO] = { 0x0, 512 * GiB },
176 static const int a15irqmap[] = {
177 [VIRT_UART] = 1,
178 [VIRT_RTC] = 2,
179 [VIRT_PCIE] = 3, /* ... to 6 */
180 [VIRT_GPIO] = 7,
181 [VIRT_SECURE_UART] = 8,
182 [VIRT_MMIO] = 16, /* ...to 16 + NUM_VIRTIO_TRANSPORTS - 1 */
183 [VIRT_GIC_V2M] = 48, /* ...to 48 + NUM_GICV2M_SPIS - 1 */
184 [VIRT_SMMU] = 74, /* ...to 74 + NUM_SMMU_IRQS - 1 */
185 [VIRT_PLATFORM_BUS] = 112, /* ...to 112 + PLATFORM_BUS_NUM_IRQS -1 */
188 static const char *valid_cpus[] = {
189 ARM_CPU_TYPE_NAME("cortex-a15"),
190 ARM_CPU_TYPE_NAME("cortex-a53"),
191 ARM_CPU_TYPE_NAME("cortex-a57"),
192 ARM_CPU_TYPE_NAME("cortex-a72"),
193 ARM_CPU_TYPE_NAME("host"),
194 ARM_CPU_TYPE_NAME("max"),
197 static bool cpu_type_valid(const char *cpu)
199 int i;
201 for (i = 0; i < ARRAY_SIZE(valid_cpus); i++) {
202 if (strcmp(cpu, valid_cpus[i]) == 0) {
203 return true;
206 return false;
209 static void create_fdt(VirtMachineState *vms)
211 void *fdt = create_device_tree(&vms->fdt_size);
213 if (!fdt) {
214 error_report("create_device_tree() failed");
215 exit(1);
218 vms->fdt = fdt;
220 /* Header */
221 qemu_fdt_setprop_string(fdt, "/", "compatible", "linux,dummy-virt");
222 qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2);
223 qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2);
225 /* /chosen must exist for load_dtb to fill in necessary properties later */
226 qemu_fdt_add_subnode(fdt, "/chosen");
228 /* Clock node, for the benefit of the UART. The kernel device tree
229 * binding documentation claims the PL011 node clock properties are
230 * optional but in practice if you omit them the kernel refuses to
231 * probe for the device.
233 vms->clock_phandle = qemu_fdt_alloc_phandle(fdt);
234 qemu_fdt_add_subnode(fdt, "/apb-pclk");
235 qemu_fdt_setprop_string(fdt, "/apb-pclk", "compatible", "fixed-clock");
236 qemu_fdt_setprop_cell(fdt, "/apb-pclk", "#clock-cells", 0x0);
237 qemu_fdt_setprop_cell(fdt, "/apb-pclk", "clock-frequency", 24000000);
238 qemu_fdt_setprop_string(fdt, "/apb-pclk", "clock-output-names",
239 "clk24mhz");
240 qemu_fdt_setprop_cell(fdt, "/apb-pclk", "phandle", vms->clock_phandle);
242 if (have_numa_distance) {
243 int size = nb_numa_nodes * nb_numa_nodes * 3 * sizeof(uint32_t);
244 uint32_t *matrix = g_malloc0(size);
245 int idx, i, j;
247 for (i = 0; i < nb_numa_nodes; i++) {
248 for (j = 0; j < nb_numa_nodes; j++) {
249 idx = (i * nb_numa_nodes + j) * 3;
250 matrix[idx + 0] = cpu_to_be32(i);
251 matrix[idx + 1] = cpu_to_be32(j);
252 matrix[idx + 2] = cpu_to_be32(numa_info[i].distance[j]);
256 qemu_fdt_add_subnode(fdt, "/distance-map");
257 qemu_fdt_setprop_string(fdt, "/distance-map", "compatible",
258 "numa-distance-map-v1");
259 qemu_fdt_setprop(fdt, "/distance-map", "distance-matrix",
260 matrix, size);
261 g_free(matrix);
265 static void fdt_add_timer_nodes(const VirtMachineState *vms)
267 /* On real hardware these interrupts are level-triggered.
268 * On KVM they were edge-triggered before host kernel version 4.4,
269 * and level-triggered afterwards.
270 * On emulated QEMU they are level-triggered.
272 * Getting the DTB info about them wrong is awkward for some
273 * guest kernels:
274 * pre-4.8 ignore the DT and leave the interrupt configured
275 * with whatever the GIC reset value (or the bootloader) left it at
276 * 4.8 before rc6 honour the incorrect data by programming it back
277 * into the GIC, causing problems
278 * 4.8rc6 and later ignore the DT and always write "level triggered"
279 * into the GIC
281 * For backwards-compatibility, virt-2.8 and earlier will continue
282 * to say these are edge-triggered, but later machines will report
283 * the correct information.
285 ARMCPU *armcpu;
286 VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms);
287 uint32_t irqflags = GIC_FDT_IRQ_FLAGS_LEVEL_HI;
289 if (vmc->claim_edge_triggered_timers) {
290 irqflags = GIC_FDT_IRQ_FLAGS_EDGE_LO_HI;
293 if (vms->gic_version == 2) {
294 irqflags = deposit32(irqflags, GIC_FDT_IRQ_PPI_CPU_START,
295 GIC_FDT_IRQ_PPI_CPU_WIDTH,
296 (1 << vms->smp_cpus) - 1);
299 qemu_fdt_add_subnode(vms->fdt, "/timer");
301 armcpu = ARM_CPU(qemu_get_cpu(0));
302 if (arm_feature(&armcpu->env, ARM_FEATURE_V8)) {
303 const char compat[] = "arm,armv8-timer\0arm,armv7-timer";
304 qemu_fdt_setprop(vms->fdt, "/timer", "compatible",
305 compat, sizeof(compat));
306 } else {
307 qemu_fdt_setprop_string(vms->fdt, "/timer", "compatible",
308 "arm,armv7-timer");
310 qemu_fdt_setprop(vms->fdt, "/timer", "always-on", NULL, 0);
311 qemu_fdt_setprop_cells(vms->fdt, "/timer", "interrupts",
312 GIC_FDT_IRQ_TYPE_PPI, ARCH_TIMER_S_EL1_IRQ, irqflags,
313 GIC_FDT_IRQ_TYPE_PPI, ARCH_TIMER_NS_EL1_IRQ, irqflags,
314 GIC_FDT_IRQ_TYPE_PPI, ARCH_TIMER_VIRT_IRQ, irqflags,
315 GIC_FDT_IRQ_TYPE_PPI, ARCH_TIMER_NS_EL2_IRQ, irqflags);
318 static void fdt_add_cpu_nodes(const VirtMachineState *vms)
320 int cpu;
321 int addr_cells = 1;
322 const MachineState *ms = MACHINE(vms);
325 * From Documentation/devicetree/bindings/arm/cpus.txt
326 * On ARM v8 64-bit systems value should be set to 2,
327 * that corresponds to the MPIDR_EL1 register size.
328 * If MPIDR_EL1[63:32] value is equal to 0 on all CPUs
329 * in the system, #address-cells can be set to 1, since
330 * MPIDR_EL1[63:32] bits are not used for CPUs
331 * identification.
333 * Here we actually don't know whether our system is 32- or 64-bit one.
334 * The simplest way to go is to examine affinity IDs of all our CPUs. If
335 * at least one of them has Aff3 populated, we set #address-cells to 2.
337 for (cpu = 0; cpu < vms->smp_cpus; cpu++) {
338 ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(cpu));
340 if (armcpu->mp_affinity & ARM_AFF3_MASK) {
341 addr_cells = 2;
342 break;
346 qemu_fdt_add_subnode(vms->fdt, "/cpus");
347 qemu_fdt_setprop_cell(vms->fdt, "/cpus", "#address-cells", addr_cells);
348 qemu_fdt_setprop_cell(vms->fdt, "/cpus", "#size-cells", 0x0);
350 for (cpu = vms->smp_cpus - 1; cpu >= 0; cpu--) {
351 char *nodename = g_strdup_printf("/cpus/cpu@%d", cpu);
352 ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(cpu));
353 CPUState *cs = CPU(armcpu);
355 qemu_fdt_add_subnode(vms->fdt, nodename);
356 qemu_fdt_setprop_string(vms->fdt, nodename, "device_type", "cpu");
357 qemu_fdt_setprop_string(vms->fdt, nodename, "compatible",
358 armcpu->dtb_compatible);
360 if (vms->psci_conduit != QEMU_PSCI_CONDUIT_DISABLED
361 && vms->smp_cpus > 1) {
362 qemu_fdt_setprop_string(vms->fdt, nodename,
363 "enable-method", "psci");
366 if (addr_cells == 2) {
367 qemu_fdt_setprop_u64(vms->fdt, nodename, "reg",
368 armcpu->mp_affinity);
369 } else {
370 qemu_fdt_setprop_cell(vms->fdt, nodename, "reg",
371 armcpu->mp_affinity);
374 if (ms->possible_cpus->cpus[cs->cpu_index].props.has_node_id) {
375 qemu_fdt_setprop_cell(vms->fdt, nodename, "numa-node-id",
376 ms->possible_cpus->cpus[cs->cpu_index].props.node_id);
379 g_free(nodename);
383 static void fdt_add_its_gic_node(VirtMachineState *vms)
385 char *nodename;
387 vms->msi_phandle = qemu_fdt_alloc_phandle(vms->fdt);
388 nodename = g_strdup_printf("/intc/its@%" PRIx64,
389 vms->memmap[VIRT_GIC_ITS].base);
390 qemu_fdt_add_subnode(vms->fdt, nodename);
391 qemu_fdt_setprop_string(vms->fdt, nodename, "compatible",
392 "arm,gic-v3-its");
393 qemu_fdt_setprop(vms->fdt, nodename, "msi-controller", NULL, 0);
394 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
395 2, vms->memmap[VIRT_GIC_ITS].base,
396 2, vms->memmap[VIRT_GIC_ITS].size);
397 qemu_fdt_setprop_cell(vms->fdt, nodename, "phandle", vms->msi_phandle);
398 g_free(nodename);
401 static void fdt_add_v2m_gic_node(VirtMachineState *vms)
403 char *nodename;
405 nodename = g_strdup_printf("/intc/v2m@%" PRIx64,
406 vms->memmap[VIRT_GIC_V2M].base);
407 vms->msi_phandle = qemu_fdt_alloc_phandle(vms->fdt);
408 qemu_fdt_add_subnode(vms->fdt, nodename);
409 qemu_fdt_setprop_string(vms->fdt, nodename, "compatible",
410 "arm,gic-v2m-frame");
411 qemu_fdt_setprop(vms->fdt, nodename, "msi-controller", NULL, 0);
412 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
413 2, vms->memmap[VIRT_GIC_V2M].base,
414 2, vms->memmap[VIRT_GIC_V2M].size);
415 qemu_fdt_setprop_cell(vms->fdt, nodename, "phandle", vms->msi_phandle);
416 g_free(nodename);
419 static void fdt_add_gic_node(VirtMachineState *vms)
421 char *nodename;
423 vms->gic_phandle = qemu_fdt_alloc_phandle(vms->fdt);
424 qemu_fdt_setprop_cell(vms->fdt, "/", "interrupt-parent", vms->gic_phandle);
426 nodename = g_strdup_printf("/intc@%" PRIx64,
427 vms->memmap[VIRT_GIC_DIST].base);
428 qemu_fdt_add_subnode(vms->fdt, nodename);
429 qemu_fdt_setprop_cell(vms->fdt, nodename, "#interrupt-cells", 3);
430 qemu_fdt_setprop(vms->fdt, nodename, "interrupt-controller", NULL, 0);
431 qemu_fdt_setprop_cell(vms->fdt, nodename, "#address-cells", 0x2);
432 qemu_fdt_setprop_cell(vms->fdt, nodename, "#size-cells", 0x2);
433 qemu_fdt_setprop(vms->fdt, nodename, "ranges", NULL, 0);
434 if (vms->gic_version == 3) {
435 int nb_redist_regions = virt_gicv3_redist_region_count(vms);
437 qemu_fdt_setprop_string(vms->fdt, nodename, "compatible",
438 "arm,gic-v3");
440 qemu_fdt_setprop_cell(vms->fdt, nodename,
441 "#redistributor-regions", nb_redist_regions);
443 if (nb_redist_regions == 1) {
444 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
445 2, vms->memmap[VIRT_GIC_DIST].base,
446 2, vms->memmap[VIRT_GIC_DIST].size,
447 2, vms->memmap[VIRT_GIC_REDIST].base,
448 2, vms->memmap[VIRT_GIC_REDIST].size);
449 } else {
450 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
451 2, vms->memmap[VIRT_GIC_DIST].base,
452 2, vms->memmap[VIRT_GIC_DIST].size,
453 2, vms->memmap[VIRT_GIC_REDIST].base,
454 2, vms->memmap[VIRT_GIC_REDIST].size,
455 2, vms->memmap[VIRT_HIGH_GIC_REDIST2].base,
456 2, vms->memmap[VIRT_HIGH_GIC_REDIST2].size);
459 if (vms->virt) {
460 qemu_fdt_setprop_cells(vms->fdt, nodename, "interrupts",
461 GIC_FDT_IRQ_TYPE_PPI, ARCH_GIC_MAINT_IRQ,
462 GIC_FDT_IRQ_FLAGS_LEVEL_HI);
464 } else {
465 /* 'cortex-a15-gic' means 'GIC v2' */
466 qemu_fdt_setprop_string(vms->fdt, nodename, "compatible",
467 "arm,cortex-a15-gic");
468 if (!vms->virt) {
469 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
470 2, vms->memmap[VIRT_GIC_DIST].base,
471 2, vms->memmap[VIRT_GIC_DIST].size,
472 2, vms->memmap[VIRT_GIC_CPU].base,
473 2, vms->memmap[VIRT_GIC_CPU].size);
474 } else {
475 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
476 2, vms->memmap[VIRT_GIC_DIST].base,
477 2, vms->memmap[VIRT_GIC_DIST].size,
478 2, vms->memmap[VIRT_GIC_CPU].base,
479 2, vms->memmap[VIRT_GIC_CPU].size,
480 2, vms->memmap[VIRT_GIC_HYP].base,
481 2, vms->memmap[VIRT_GIC_HYP].size,
482 2, vms->memmap[VIRT_GIC_VCPU].base,
483 2, vms->memmap[VIRT_GIC_VCPU].size);
484 qemu_fdt_setprop_cells(vms->fdt, nodename, "interrupts",
485 GIC_FDT_IRQ_TYPE_PPI, ARCH_GIC_MAINT_IRQ,
486 GIC_FDT_IRQ_FLAGS_LEVEL_HI);
490 qemu_fdt_setprop_cell(vms->fdt, nodename, "phandle", vms->gic_phandle);
491 g_free(nodename);
494 static void fdt_add_pmu_nodes(const VirtMachineState *vms)
496 CPUState *cpu;
497 ARMCPU *armcpu;
498 uint32_t irqflags = GIC_FDT_IRQ_FLAGS_LEVEL_HI;
500 CPU_FOREACH(cpu) {
501 armcpu = ARM_CPU(cpu);
502 if (!arm_feature(&armcpu->env, ARM_FEATURE_PMU)) {
503 return;
505 if (kvm_enabled()) {
506 if (kvm_irqchip_in_kernel()) {
507 kvm_arm_pmu_set_irq(cpu, PPI(VIRTUAL_PMU_IRQ));
509 kvm_arm_pmu_init(cpu);
513 if (vms->gic_version == 2) {
514 irqflags = deposit32(irqflags, GIC_FDT_IRQ_PPI_CPU_START,
515 GIC_FDT_IRQ_PPI_CPU_WIDTH,
516 (1 << vms->smp_cpus) - 1);
519 armcpu = ARM_CPU(qemu_get_cpu(0));
520 qemu_fdt_add_subnode(vms->fdt, "/pmu");
521 if (arm_feature(&armcpu->env, ARM_FEATURE_V8)) {
522 const char compat[] = "arm,armv8-pmuv3";
523 qemu_fdt_setprop(vms->fdt, "/pmu", "compatible",
524 compat, sizeof(compat));
525 qemu_fdt_setprop_cells(vms->fdt, "/pmu", "interrupts",
526 GIC_FDT_IRQ_TYPE_PPI, VIRTUAL_PMU_IRQ, irqflags);
530 static void create_its(VirtMachineState *vms, DeviceState *gicdev)
532 const char *itsclass = its_class_name();
533 DeviceState *dev;
535 if (!itsclass) {
536 /* Do nothing if not supported */
537 return;
540 dev = qdev_create(NULL, itsclass);
542 object_property_set_link(OBJECT(dev), OBJECT(gicdev), "parent-gicv3",
543 &error_abort);
544 qdev_init_nofail(dev);
545 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, vms->memmap[VIRT_GIC_ITS].base);
547 fdt_add_its_gic_node(vms);
550 static void create_v2m(VirtMachineState *vms, qemu_irq *pic)
552 int i;
553 int irq = vms->irqmap[VIRT_GIC_V2M];
554 DeviceState *dev;
556 dev = qdev_create(NULL, "arm-gicv2m");
557 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, vms->memmap[VIRT_GIC_V2M].base);
558 qdev_prop_set_uint32(dev, "base-spi", irq);
559 qdev_prop_set_uint32(dev, "num-spi", NUM_GICV2M_SPIS);
560 qdev_init_nofail(dev);
562 for (i = 0; i < NUM_GICV2M_SPIS; i++) {
563 sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, pic[irq + i]);
566 fdt_add_v2m_gic_node(vms);
569 static void create_gic(VirtMachineState *vms, qemu_irq *pic)
571 /* We create a standalone GIC */
572 DeviceState *gicdev;
573 SysBusDevice *gicbusdev;
574 const char *gictype;
575 int type = vms->gic_version, i;
576 uint32_t nb_redist_regions = 0;
578 gictype = (type == 3) ? gicv3_class_name() : gic_class_name();
580 gicdev = qdev_create(NULL, gictype);
581 qdev_prop_set_uint32(gicdev, "revision", type);
582 qdev_prop_set_uint32(gicdev, "num-cpu", smp_cpus);
583 /* Note that the num-irq property counts both internal and external
584 * interrupts; there are always 32 of the former (mandated by GIC spec).
586 qdev_prop_set_uint32(gicdev, "num-irq", NUM_IRQS + 32);
587 if (!kvm_irqchip_in_kernel()) {
588 qdev_prop_set_bit(gicdev, "has-security-extensions", vms->secure);
591 if (type == 3) {
592 uint32_t redist0_capacity =
593 vms->memmap[VIRT_GIC_REDIST].size / GICV3_REDIST_SIZE;
594 uint32_t redist0_count = MIN(smp_cpus, redist0_capacity);
596 nb_redist_regions = virt_gicv3_redist_region_count(vms);
598 qdev_prop_set_uint32(gicdev, "len-redist-region-count",
599 nb_redist_regions);
600 qdev_prop_set_uint32(gicdev, "redist-region-count[0]", redist0_count);
602 if (nb_redist_regions == 2) {
603 uint32_t redist1_capacity =
604 vms->memmap[VIRT_HIGH_GIC_REDIST2].size / GICV3_REDIST_SIZE;
606 qdev_prop_set_uint32(gicdev, "redist-region-count[1]",
607 MIN(smp_cpus - redist0_count, redist1_capacity));
609 } else {
610 if (!kvm_irqchip_in_kernel()) {
611 qdev_prop_set_bit(gicdev, "has-virtualization-extensions",
612 vms->virt);
615 qdev_init_nofail(gicdev);
616 gicbusdev = SYS_BUS_DEVICE(gicdev);
617 sysbus_mmio_map(gicbusdev, 0, vms->memmap[VIRT_GIC_DIST].base);
618 if (type == 3) {
619 sysbus_mmio_map(gicbusdev, 1, vms->memmap[VIRT_GIC_REDIST].base);
620 if (nb_redist_regions == 2) {
621 sysbus_mmio_map(gicbusdev, 2,
622 vms->memmap[VIRT_HIGH_GIC_REDIST2].base);
624 } else {
625 sysbus_mmio_map(gicbusdev, 1, vms->memmap[VIRT_GIC_CPU].base);
626 if (vms->virt) {
627 sysbus_mmio_map(gicbusdev, 2, vms->memmap[VIRT_GIC_HYP].base);
628 sysbus_mmio_map(gicbusdev, 3, vms->memmap[VIRT_GIC_VCPU].base);
632 /* Wire the outputs from each CPU's generic timer and the GICv3
633 * maintenance interrupt signal to the appropriate GIC PPI inputs,
634 * and the GIC's IRQ/FIQ/VIRQ/VFIQ interrupt outputs to the CPU's inputs.
636 for (i = 0; i < smp_cpus; i++) {
637 DeviceState *cpudev = DEVICE(qemu_get_cpu(i));
638 int ppibase = NUM_IRQS + i * GIC_INTERNAL + GIC_NR_SGIS;
639 int irq;
640 /* Mapping from the output timer irq lines from the CPU to the
641 * GIC PPI inputs we use for the virt board.
643 const int timer_irq[] = {
644 [GTIMER_PHYS] = ARCH_TIMER_NS_EL1_IRQ,
645 [GTIMER_VIRT] = ARCH_TIMER_VIRT_IRQ,
646 [GTIMER_HYP] = ARCH_TIMER_NS_EL2_IRQ,
647 [GTIMER_SEC] = ARCH_TIMER_S_EL1_IRQ,
650 for (irq = 0; irq < ARRAY_SIZE(timer_irq); irq++) {
651 qdev_connect_gpio_out(cpudev, irq,
652 qdev_get_gpio_in(gicdev,
653 ppibase + timer_irq[irq]));
656 if (type == 3) {
657 qemu_irq irq = qdev_get_gpio_in(gicdev,
658 ppibase + ARCH_GIC_MAINT_IRQ);
659 qdev_connect_gpio_out_named(cpudev, "gicv3-maintenance-interrupt",
660 0, irq);
661 } else if (vms->virt) {
662 qemu_irq irq = qdev_get_gpio_in(gicdev,
663 ppibase + ARCH_GIC_MAINT_IRQ);
664 sysbus_connect_irq(gicbusdev, i + 4 * smp_cpus, irq);
667 qdev_connect_gpio_out_named(cpudev, "pmu-interrupt", 0,
668 qdev_get_gpio_in(gicdev, ppibase
669 + VIRTUAL_PMU_IRQ));
671 sysbus_connect_irq(gicbusdev, i, qdev_get_gpio_in(cpudev, ARM_CPU_IRQ));
672 sysbus_connect_irq(gicbusdev, i + smp_cpus,
673 qdev_get_gpio_in(cpudev, ARM_CPU_FIQ));
674 sysbus_connect_irq(gicbusdev, i + 2 * smp_cpus,
675 qdev_get_gpio_in(cpudev, ARM_CPU_VIRQ));
676 sysbus_connect_irq(gicbusdev, i + 3 * smp_cpus,
677 qdev_get_gpio_in(cpudev, ARM_CPU_VFIQ));
680 for (i = 0; i < NUM_IRQS; i++) {
681 pic[i] = qdev_get_gpio_in(gicdev, i);
684 fdt_add_gic_node(vms);
686 if (type == 3 && vms->its) {
687 create_its(vms, gicdev);
688 } else if (type == 2) {
689 create_v2m(vms, pic);
693 static void create_uart(const VirtMachineState *vms, qemu_irq *pic, int uart,
694 MemoryRegion *mem, Chardev *chr)
696 char *nodename;
697 hwaddr base = vms->memmap[uart].base;
698 hwaddr size = vms->memmap[uart].size;
699 int irq = vms->irqmap[uart];
700 const char compat[] = "arm,pl011\0arm,primecell";
701 const char clocknames[] = "uartclk\0apb_pclk";
702 DeviceState *dev = qdev_create(NULL, "pl011");
703 SysBusDevice *s = SYS_BUS_DEVICE(dev);
705 qdev_prop_set_chr(dev, "chardev", chr);
706 qdev_init_nofail(dev);
707 memory_region_add_subregion(mem, base,
708 sysbus_mmio_get_region(s, 0));
709 sysbus_connect_irq(s, 0, pic[irq]);
711 nodename = g_strdup_printf("/pl011@%" PRIx64, base);
712 qemu_fdt_add_subnode(vms->fdt, nodename);
713 /* Note that we can't use setprop_string because of the embedded NUL */
714 qemu_fdt_setprop(vms->fdt, nodename, "compatible",
715 compat, sizeof(compat));
716 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
717 2, base, 2, size);
718 qemu_fdt_setprop_cells(vms->fdt, nodename, "interrupts",
719 GIC_FDT_IRQ_TYPE_SPI, irq,
720 GIC_FDT_IRQ_FLAGS_LEVEL_HI);
721 qemu_fdt_setprop_cells(vms->fdt, nodename, "clocks",
722 vms->clock_phandle, vms->clock_phandle);
723 qemu_fdt_setprop(vms->fdt, nodename, "clock-names",
724 clocknames, sizeof(clocknames));
726 if (uart == VIRT_UART) {
727 qemu_fdt_setprop_string(vms->fdt, "/chosen", "stdout-path", nodename);
728 } else {
729 /* Mark as not usable by the normal world */
730 qemu_fdt_setprop_string(vms->fdt, nodename, "status", "disabled");
731 qemu_fdt_setprop_string(vms->fdt, nodename, "secure-status", "okay");
733 qemu_fdt_add_subnode(vms->fdt, "/secure-chosen");
734 qemu_fdt_setprop_string(vms->fdt, "/secure-chosen", "stdout-path",
735 nodename);
738 g_free(nodename);
741 static void create_rtc(const VirtMachineState *vms, qemu_irq *pic)
743 char *nodename;
744 hwaddr base = vms->memmap[VIRT_RTC].base;
745 hwaddr size = vms->memmap[VIRT_RTC].size;
746 int irq = vms->irqmap[VIRT_RTC];
747 const char compat[] = "arm,pl031\0arm,primecell";
749 sysbus_create_simple("pl031", base, pic[irq]);
751 nodename = g_strdup_printf("/pl031@%" PRIx64, base);
752 qemu_fdt_add_subnode(vms->fdt, nodename);
753 qemu_fdt_setprop(vms->fdt, nodename, "compatible", compat, sizeof(compat));
754 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
755 2, base, 2, size);
756 qemu_fdt_setprop_cells(vms->fdt, nodename, "interrupts",
757 GIC_FDT_IRQ_TYPE_SPI, irq,
758 GIC_FDT_IRQ_FLAGS_LEVEL_HI);
759 qemu_fdt_setprop_cell(vms->fdt, nodename, "clocks", vms->clock_phandle);
760 qemu_fdt_setprop_string(vms->fdt, nodename, "clock-names", "apb_pclk");
761 g_free(nodename);
764 static DeviceState *gpio_key_dev;
765 static void virt_powerdown_req(Notifier *n, void *opaque)
767 /* use gpio Pin 3 for power button event */
768 qemu_set_irq(qdev_get_gpio_in(gpio_key_dev, 0), 1);
771 static Notifier virt_system_powerdown_notifier = {
772 .notify = virt_powerdown_req
775 static void create_gpio(const VirtMachineState *vms, qemu_irq *pic)
777 char *nodename;
778 DeviceState *pl061_dev;
779 hwaddr base = vms->memmap[VIRT_GPIO].base;
780 hwaddr size = vms->memmap[VIRT_GPIO].size;
781 int irq = vms->irqmap[VIRT_GPIO];
782 const char compat[] = "arm,pl061\0arm,primecell";
784 pl061_dev = sysbus_create_simple("pl061", base, pic[irq]);
786 uint32_t phandle = qemu_fdt_alloc_phandle(vms->fdt);
787 nodename = g_strdup_printf("/pl061@%" PRIx64, base);
788 qemu_fdt_add_subnode(vms->fdt, nodename);
789 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
790 2, base, 2, size);
791 qemu_fdt_setprop(vms->fdt, nodename, "compatible", compat, sizeof(compat));
792 qemu_fdt_setprop_cell(vms->fdt, nodename, "#gpio-cells", 2);
793 qemu_fdt_setprop(vms->fdt, nodename, "gpio-controller", NULL, 0);
794 qemu_fdt_setprop_cells(vms->fdt, nodename, "interrupts",
795 GIC_FDT_IRQ_TYPE_SPI, irq,
796 GIC_FDT_IRQ_FLAGS_LEVEL_HI);
797 qemu_fdt_setprop_cell(vms->fdt, nodename, "clocks", vms->clock_phandle);
798 qemu_fdt_setprop_string(vms->fdt, nodename, "clock-names", "apb_pclk");
799 qemu_fdt_setprop_cell(vms->fdt, nodename, "phandle", phandle);
801 gpio_key_dev = sysbus_create_simple("gpio-key", -1,
802 qdev_get_gpio_in(pl061_dev, 3));
803 qemu_fdt_add_subnode(vms->fdt, "/gpio-keys");
804 qemu_fdt_setprop_string(vms->fdt, "/gpio-keys", "compatible", "gpio-keys");
805 qemu_fdt_setprop_cell(vms->fdt, "/gpio-keys", "#size-cells", 0);
806 qemu_fdt_setprop_cell(vms->fdt, "/gpio-keys", "#address-cells", 1);
808 qemu_fdt_add_subnode(vms->fdt, "/gpio-keys/poweroff");
809 qemu_fdt_setprop_string(vms->fdt, "/gpio-keys/poweroff",
810 "label", "GPIO Key Poweroff");
811 qemu_fdt_setprop_cell(vms->fdt, "/gpio-keys/poweroff", "linux,code",
812 KEY_POWER);
813 qemu_fdt_setprop_cells(vms->fdt, "/gpio-keys/poweroff",
814 "gpios", phandle, 3, 0);
816 /* connect powerdown request */
817 qemu_register_powerdown_notifier(&virt_system_powerdown_notifier);
819 g_free(nodename);
822 static void create_virtio_devices(const VirtMachineState *vms, qemu_irq *pic)
824 int i;
825 hwaddr size = vms->memmap[VIRT_MMIO].size;
827 /* We create the transports in forwards order. Since qbus_realize()
828 * prepends (not appends) new child buses, the incrementing loop below will
829 * create a list of virtio-mmio buses with decreasing base addresses.
831 * When a -device option is processed from the command line,
832 * qbus_find_recursive() picks the next free virtio-mmio bus in forwards
833 * order. The upshot is that -device options in increasing command line
834 * order are mapped to virtio-mmio buses with decreasing base addresses.
836 * When this code was originally written, that arrangement ensured that the
837 * guest Linux kernel would give the lowest "name" (/dev/vda, eth0, etc) to
838 * the first -device on the command line. (The end-to-end order is a
839 * function of this loop, qbus_realize(), qbus_find_recursive(), and the
840 * guest kernel's name-to-address assignment strategy.)
842 * Meanwhile, the kernel's traversal seems to have been reversed; see eg.
843 * the message, if not necessarily the code, of commit 70161ff336.
844 * Therefore the loop now establishes the inverse of the original intent.
846 * Unfortunately, we can't counteract the kernel change by reversing the
847 * loop; it would break existing command lines.
849 * In any case, the kernel makes no guarantee about the stability of
850 * enumeration order of virtio devices (as demonstrated by it changing
851 * between kernel versions). For reliable and stable identification
852 * of disks users must use UUIDs or similar mechanisms.
854 for (i = 0; i < NUM_VIRTIO_TRANSPORTS; i++) {
855 int irq = vms->irqmap[VIRT_MMIO] + i;
856 hwaddr base = vms->memmap[VIRT_MMIO].base + i * size;
858 sysbus_create_simple("virtio-mmio", base, pic[irq]);
861 /* We add dtb nodes in reverse order so that they appear in the finished
862 * device tree lowest address first.
864 * Note that this mapping is independent of the loop above. The previous
865 * loop influences virtio device to virtio transport assignment, whereas
866 * this loop controls how virtio transports are laid out in the dtb.
868 for (i = NUM_VIRTIO_TRANSPORTS - 1; i >= 0; i--) {
869 char *nodename;
870 int irq = vms->irqmap[VIRT_MMIO] + i;
871 hwaddr base = vms->memmap[VIRT_MMIO].base + i * size;
873 nodename = g_strdup_printf("/virtio_mmio@%" PRIx64, base);
874 qemu_fdt_add_subnode(vms->fdt, nodename);
875 qemu_fdt_setprop_string(vms->fdt, nodename,
876 "compatible", "virtio,mmio");
877 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
878 2, base, 2, size);
879 qemu_fdt_setprop_cells(vms->fdt, nodename, "interrupts",
880 GIC_FDT_IRQ_TYPE_SPI, irq,
881 GIC_FDT_IRQ_FLAGS_EDGE_LO_HI);
882 qemu_fdt_setprop(vms->fdt, nodename, "dma-coherent", NULL, 0);
883 g_free(nodename);
887 static void create_one_flash(const char *name, hwaddr flashbase,
888 hwaddr flashsize, const char *file,
889 MemoryRegion *sysmem)
891 /* Create and map a single flash device. We use the same
892 * parameters as the flash devices on the Versatile Express board.
894 DriveInfo *dinfo = drive_get_next(IF_PFLASH);
895 DeviceState *dev = qdev_create(NULL, "cfi.pflash01");
896 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
897 const uint64_t sectorlength = 256 * 1024;
899 if (dinfo) {
900 qdev_prop_set_drive(dev, "drive", blk_by_legacy_dinfo(dinfo),
901 &error_abort);
904 qdev_prop_set_uint32(dev, "num-blocks", flashsize / sectorlength);
905 qdev_prop_set_uint64(dev, "sector-length", sectorlength);
906 qdev_prop_set_uint8(dev, "width", 4);
907 qdev_prop_set_uint8(dev, "device-width", 2);
908 qdev_prop_set_bit(dev, "big-endian", false);
909 qdev_prop_set_uint16(dev, "id0", 0x89);
910 qdev_prop_set_uint16(dev, "id1", 0x18);
911 qdev_prop_set_uint16(dev, "id2", 0x00);
912 qdev_prop_set_uint16(dev, "id3", 0x00);
913 qdev_prop_set_string(dev, "name", name);
914 qdev_init_nofail(dev);
916 memory_region_add_subregion(sysmem, flashbase,
917 sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0));
919 if (file) {
920 char *fn;
921 int image_size;
923 if (drive_get(IF_PFLASH, 0, 0)) {
924 error_report("The contents of the first flash device may be "
925 "specified with -bios or with -drive if=pflash... "
926 "but you cannot use both options at once");
927 exit(1);
929 fn = qemu_find_file(QEMU_FILE_TYPE_BIOS, file);
930 if (!fn) {
931 error_report("Could not find ROM image '%s'", file);
932 exit(1);
934 image_size = load_image_mr(fn, sysbus_mmio_get_region(sbd, 0));
935 g_free(fn);
936 if (image_size < 0) {
937 error_report("Could not load ROM image '%s'", file);
938 exit(1);
943 static void create_flash(const VirtMachineState *vms,
944 MemoryRegion *sysmem,
945 MemoryRegion *secure_sysmem)
947 /* Create two flash devices to fill the VIRT_FLASH space in the memmap.
948 * Any file passed via -bios goes in the first of these.
949 * sysmem is the system memory space. secure_sysmem is the secure view
950 * of the system, and the first flash device should be made visible only
951 * there. The second flash device is visible to both secure and nonsecure.
952 * If sysmem == secure_sysmem this means there is no separate Secure
953 * address space and both flash devices are generally visible.
955 hwaddr flashsize = vms->memmap[VIRT_FLASH].size / 2;
956 hwaddr flashbase = vms->memmap[VIRT_FLASH].base;
957 char *nodename;
959 create_one_flash("virt.flash0", flashbase, flashsize,
960 bios_name, secure_sysmem);
961 create_one_flash("virt.flash1", flashbase + flashsize, flashsize,
962 NULL, sysmem);
964 if (sysmem == secure_sysmem) {
965 /* Report both flash devices as a single node in the DT */
966 nodename = g_strdup_printf("/flash@%" PRIx64, flashbase);
967 qemu_fdt_add_subnode(vms->fdt, nodename);
968 qemu_fdt_setprop_string(vms->fdt, nodename, "compatible", "cfi-flash");
969 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
970 2, flashbase, 2, flashsize,
971 2, flashbase + flashsize, 2, flashsize);
972 qemu_fdt_setprop_cell(vms->fdt, nodename, "bank-width", 4);
973 g_free(nodename);
974 } else {
975 /* Report the devices as separate nodes so we can mark one as
976 * only visible to the secure world.
978 nodename = g_strdup_printf("/secflash@%" PRIx64, flashbase);
979 qemu_fdt_add_subnode(vms->fdt, nodename);
980 qemu_fdt_setprop_string(vms->fdt, nodename, "compatible", "cfi-flash");
981 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
982 2, flashbase, 2, flashsize);
983 qemu_fdt_setprop_cell(vms->fdt, nodename, "bank-width", 4);
984 qemu_fdt_setprop_string(vms->fdt, nodename, "status", "disabled");
985 qemu_fdt_setprop_string(vms->fdt, nodename, "secure-status", "okay");
986 g_free(nodename);
988 nodename = g_strdup_printf("/flash@%" PRIx64, flashbase);
989 qemu_fdt_add_subnode(vms->fdt, nodename);
990 qemu_fdt_setprop_string(vms->fdt, nodename, "compatible", "cfi-flash");
991 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
992 2, flashbase + flashsize, 2, flashsize);
993 qemu_fdt_setprop_cell(vms->fdt, nodename, "bank-width", 4);
994 g_free(nodename);
998 static FWCfgState *create_fw_cfg(const VirtMachineState *vms, AddressSpace *as)
1000 hwaddr base = vms->memmap[VIRT_FW_CFG].base;
1001 hwaddr size = vms->memmap[VIRT_FW_CFG].size;
1002 FWCfgState *fw_cfg;
1003 char *nodename;
1005 fw_cfg = fw_cfg_init_mem_wide(base + 8, base, 8, base + 16, as);
1006 fw_cfg_add_i16(fw_cfg, FW_CFG_NB_CPUS, (uint16_t)smp_cpus);
1008 nodename = g_strdup_printf("/fw-cfg@%" PRIx64, base);
1009 qemu_fdt_add_subnode(vms->fdt, nodename);
1010 qemu_fdt_setprop_string(vms->fdt, nodename,
1011 "compatible", "qemu,fw-cfg-mmio");
1012 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
1013 2, base, 2, size);
1014 qemu_fdt_setprop(vms->fdt, nodename, "dma-coherent", NULL, 0);
1015 g_free(nodename);
1016 return fw_cfg;
1019 static void create_pcie_irq_map(const VirtMachineState *vms,
1020 uint32_t gic_phandle,
1021 int first_irq, const char *nodename)
1023 int devfn, pin;
1024 uint32_t full_irq_map[4 * 4 * 10] = { 0 };
1025 uint32_t *irq_map = full_irq_map;
1027 for (devfn = 0; devfn <= 0x18; devfn += 0x8) {
1028 for (pin = 0; pin < 4; pin++) {
1029 int irq_type = GIC_FDT_IRQ_TYPE_SPI;
1030 int irq_nr = first_irq + ((pin + PCI_SLOT(devfn)) % PCI_NUM_PINS);
1031 int irq_level = GIC_FDT_IRQ_FLAGS_LEVEL_HI;
1032 int i;
1034 uint32_t map[] = {
1035 devfn << 8, 0, 0, /* devfn */
1036 pin + 1, /* PCI pin */
1037 gic_phandle, 0, 0, irq_type, irq_nr, irq_level }; /* GIC irq */
1039 /* Convert map to big endian */
1040 for (i = 0; i < 10; i++) {
1041 irq_map[i] = cpu_to_be32(map[i]);
1043 irq_map += 10;
1047 qemu_fdt_setprop(vms->fdt, nodename, "interrupt-map",
1048 full_irq_map, sizeof(full_irq_map));
1050 qemu_fdt_setprop_cells(vms->fdt, nodename, "interrupt-map-mask",
1051 0x1800, 0, 0, /* devfn (PCI_SLOT(3)) */
1052 0x7 /* PCI irq */);
1055 static void create_smmu(const VirtMachineState *vms, qemu_irq *pic,
1056 PCIBus *bus)
1058 char *node;
1059 const char compat[] = "arm,smmu-v3";
1060 int irq = vms->irqmap[VIRT_SMMU];
1061 int i;
1062 hwaddr base = vms->memmap[VIRT_SMMU].base;
1063 hwaddr size = vms->memmap[VIRT_SMMU].size;
1064 const char irq_names[] = "eventq\0priq\0cmdq-sync\0gerror";
1065 DeviceState *dev;
1067 if (vms->iommu != VIRT_IOMMU_SMMUV3 || !vms->iommu_phandle) {
1068 return;
1071 dev = qdev_create(NULL, "arm-smmuv3");
1073 object_property_set_link(OBJECT(dev), OBJECT(bus), "primary-bus",
1074 &error_abort);
1075 qdev_init_nofail(dev);
1076 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base);
1077 for (i = 0; i < NUM_SMMU_IRQS; i++) {
1078 sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, pic[irq + i]);
1081 node = g_strdup_printf("/smmuv3@%" PRIx64, base);
1082 qemu_fdt_add_subnode(vms->fdt, node);
1083 qemu_fdt_setprop(vms->fdt, node, "compatible", compat, sizeof(compat));
1084 qemu_fdt_setprop_sized_cells(vms->fdt, node, "reg", 2, base, 2, size);
1086 qemu_fdt_setprop_cells(vms->fdt, node, "interrupts",
1087 GIC_FDT_IRQ_TYPE_SPI, irq , GIC_FDT_IRQ_FLAGS_EDGE_LO_HI,
1088 GIC_FDT_IRQ_TYPE_SPI, irq + 1, GIC_FDT_IRQ_FLAGS_EDGE_LO_HI,
1089 GIC_FDT_IRQ_TYPE_SPI, irq + 2, GIC_FDT_IRQ_FLAGS_EDGE_LO_HI,
1090 GIC_FDT_IRQ_TYPE_SPI, irq + 3, GIC_FDT_IRQ_FLAGS_EDGE_LO_HI);
1092 qemu_fdt_setprop(vms->fdt, node, "interrupt-names", irq_names,
1093 sizeof(irq_names));
1095 qemu_fdt_setprop_cell(vms->fdt, node, "clocks", vms->clock_phandle);
1096 qemu_fdt_setprop_string(vms->fdt, node, "clock-names", "apb_pclk");
1097 qemu_fdt_setprop(vms->fdt, node, "dma-coherent", NULL, 0);
1099 qemu_fdt_setprop_cell(vms->fdt, node, "#iommu-cells", 1);
1101 qemu_fdt_setprop_cell(vms->fdt, node, "phandle", vms->iommu_phandle);
1102 g_free(node);
1105 static void create_pcie(VirtMachineState *vms, qemu_irq *pic)
1107 hwaddr base_mmio = vms->memmap[VIRT_PCIE_MMIO].base;
1108 hwaddr size_mmio = vms->memmap[VIRT_PCIE_MMIO].size;
1109 hwaddr base_mmio_high = vms->memmap[VIRT_HIGH_PCIE_MMIO].base;
1110 hwaddr size_mmio_high = vms->memmap[VIRT_HIGH_PCIE_MMIO].size;
1111 hwaddr base_pio = vms->memmap[VIRT_PCIE_PIO].base;
1112 hwaddr size_pio = vms->memmap[VIRT_PCIE_PIO].size;
1113 hwaddr base_ecam, size_ecam;
1114 hwaddr base = base_mmio;
1115 int nr_pcie_buses;
1116 int irq = vms->irqmap[VIRT_PCIE];
1117 MemoryRegion *mmio_alias;
1118 MemoryRegion *mmio_reg;
1119 MemoryRegion *ecam_alias;
1120 MemoryRegion *ecam_reg;
1121 DeviceState *dev;
1122 char *nodename;
1123 int i, ecam_id;
1124 PCIHostState *pci;
1126 dev = qdev_create(NULL, TYPE_GPEX_HOST);
1127 qdev_init_nofail(dev);
1129 ecam_id = VIRT_ECAM_ID(vms->highmem_ecam);
1130 base_ecam = vms->memmap[ecam_id].base;
1131 size_ecam = vms->memmap[ecam_id].size;
1132 nr_pcie_buses = size_ecam / PCIE_MMCFG_SIZE_MIN;
1133 /* Map only the first size_ecam bytes of ECAM space */
1134 ecam_alias = g_new0(MemoryRegion, 1);
1135 ecam_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0);
1136 memory_region_init_alias(ecam_alias, OBJECT(dev), "pcie-ecam",
1137 ecam_reg, 0, size_ecam);
1138 memory_region_add_subregion(get_system_memory(), base_ecam, ecam_alias);
1140 /* Map the MMIO window into system address space so as to expose
1141 * the section of PCI MMIO space which starts at the same base address
1142 * (ie 1:1 mapping for that part of PCI MMIO space visible through
1143 * the window).
1145 mmio_alias = g_new0(MemoryRegion, 1);
1146 mmio_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 1);
1147 memory_region_init_alias(mmio_alias, OBJECT(dev), "pcie-mmio",
1148 mmio_reg, base_mmio, size_mmio);
1149 memory_region_add_subregion(get_system_memory(), base_mmio, mmio_alias);
1151 if (vms->highmem) {
1152 /* Map high MMIO space */
1153 MemoryRegion *high_mmio_alias = g_new0(MemoryRegion, 1);
1155 memory_region_init_alias(high_mmio_alias, OBJECT(dev), "pcie-mmio-high",
1156 mmio_reg, base_mmio_high, size_mmio_high);
1157 memory_region_add_subregion(get_system_memory(), base_mmio_high,
1158 high_mmio_alias);
1161 /* Map IO port space */
1162 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 2, base_pio);
1164 for (i = 0; i < GPEX_NUM_IRQS; i++) {
1165 sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, pic[irq + i]);
1166 gpex_set_irq_num(GPEX_HOST(dev), i, irq + i);
1169 pci = PCI_HOST_BRIDGE(dev);
1170 if (pci->bus) {
1171 for (i = 0; i < nb_nics; i++) {
1172 NICInfo *nd = &nd_table[i];
1174 if (!nd->model) {
1175 nd->model = g_strdup("virtio");
1178 pci_nic_init_nofail(nd, pci->bus, nd->model, NULL);
1182 nodename = g_strdup_printf("/pcie@%" PRIx64, base);
1183 qemu_fdt_add_subnode(vms->fdt, nodename);
1184 qemu_fdt_setprop_string(vms->fdt, nodename,
1185 "compatible", "pci-host-ecam-generic");
1186 qemu_fdt_setprop_string(vms->fdt, nodename, "device_type", "pci");
1187 qemu_fdt_setprop_cell(vms->fdt, nodename, "#address-cells", 3);
1188 qemu_fdt_setprop_cell(vms->fdt, nodename, "#size-cells", 2);
1189 qemu_fdt_setprop_cell(vms->fdt, nodename, "linux,pci-domain", 0);
1190 qemu_fdt_setprop_cells(vms->fdt, nodename, "bus-range", 0,
1191 nr_pcie_buses - 1);
1192 qemu_fdt_setprop(vms->fdt, nodename, "dma-coherent", NULL, 0);
1194 if (vms->msi_phandle) {
1195 qemu_fdt_setprop_cells(vms->fdt, nodename, "msi-parent",
1196 vms->msi_phandle);
1199 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
1200 2, base_ecam, 2, size_ecam);
1202 if (vms->highmem) {
1203 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "ranges",
1204 1, FDT_PCI_RANGE_IOPORT, 2, 0,
1205 2, base_pio, 2, size_pio,
1206 1, FDT_PCI_RANGE_MMIO, 2, base_mmio,
1207 2, base_mmio, 2, size_mmio,
1208 1, FDT_PCI_RANGE_MMIO_64BIT,
1209 2, base_mmio_high,
1210 2, base_mmio_high, 2, size_mmio_high);
1211 } else {
1212 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "ranges",
1213 1, FDT_PCI_RANGE_IOPORT, 2, 0,
1214 2, base_pio, 2, size_pio,
1215 1, FDT_PCI_RANGE_MMIO, 2, base_mmio,
1216 2, base_mmio, 2, size_mmio);
1219 qemu_fdt_setprop_cell(vms->fdt, nodename, "#interrupt-cells", 1);
1220 create_pcie_irq_map(vms, vms->gic_phandle, irq, nodename);
1222 if (vms->iommu) {
1223 vms->iommu_phandle = qemu_fdt_alloc_phandle(vms->fdt);
1225 create_smmu(vms, pic, pci->bus);
1227 qemu_fdt_setprop_cells(vms->fdt, nodename, "iommu-map",
1228 0x0, vms->iommu_phandle, 0x0, 0x10000);
1231 g_free(nodename);
1234 static void create_platform_bus(VirtMachineState *vms, qemu_irq *pic)
1236 DeviceState *dev;
1237 SysBusDevice *s;
1238 int i;
1239 MemoryRegion *sysmem = get_system_memory();
1241 dev = qdev_create(NULL, TYPE_PLATFORM_BUS_DEVICE);
1242 dev->id = TYPE_PLATFORM_BUS_DEVICE;
1243 qdev_prop_set_uint32(dev, "num_irqs", PLATFORM_BUS_NUM_IRQS);
1244 qdev_prop_set_uint32(dev, "mmio_size", vms->memmap[VIRT_PLATFORM_BUS].size);
1245 qdev_init_nofail(dev);
1246 vms->platform_bus_dev = dev;
1248 s = SYS_BUS_DEVICE(dev);
1249 for (i = 0; i < PLATFORM_BUS_NUM_IRQS; i++) {
1250 int irqn = vms->irqmap[VIRT_PLATFORM_BUS] + i;
1251 sysbus_connect_irq(s, i, pic[irqn]);
1254 memory_region_add_subregion(sysmem,
1255 vms->memmap[VIRT_PLATFORM_BUS].base,
1256 sysbus_mmio_get_region(s, 0));
1259 static void create_secure_ram(VirtMachineState *vms,
1260 MemoryRegion *secure_sysmem)
1262 MemoryRegion *secram = g_new(MemoryRegion, 1);
1263 char *nodename;
1264 hwaddr base = vms->memmap[VIRT_SECURE_MEM].base;
1265 hwaddr size = vms->memmap[VIRT_SECURE_MEM].size;
1267 memory_region_init_ram(secram, NULL, "virt.secure-ram", size,
1268 &error_fatal);
1269 memory_region_add_subregion(secure_sysmem, base, secram);
1271 nodename = g_strdup_printf("/secram@%" PRIx64, base);
1272 qemu_fdt_add_subnode(vms->fdt, nodename);
1273 qemu_fdt_setprop_string(vms->fdt, nodename, "device_type", "memory");
1274 qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg", 2, base, 2, size);
1275 qemu_fdt_setprop_string(vms->fdt, nodename, "status", "disabled");
1276 qemu_fdt_setprop_string(vms->fdt, nodename, "secure-status", "okay");
1278 g_free(nodename);
1281 static void *machvirt_dtb(const struct arm_boot_info *binfo, int *fdt_size)
1283 const VirtMachineState *board = container_of(binfo, VirtMachineState,
1284 bootinfo);
1286 *fdt_size = board->fdt_size;
1287 return board->fdt;
1290 static void virt_build_smbios(VirtMachineState *vms)
1292 MachineClass *mc = MACHINE_GET_CLASS(vms);
1293 VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms);
1294 uint8_t *smbios_tables, *smbios_anchor;
1295 size_t smbios_tables_len, smbios_anchor_len;
1296 const char *product = "QEMU Virtual Machine";
1298 if (!vms->fw_cfg) {
1299 return;
1302 if (kvm_enabled()) {
1303 product = "KVM Virtual Machine";
1306 smbios_set_defaults("QEMU", product,
1307 vmc->smbios_old_sys_ver ? "1.0" : mc->name, false,
1308 true, SMBIOS_ENTRY_POINT_30);
1310 smbios_get_tables(NULL, 0, &smbios_tables, &smbios_tables_len,
1311 &smbios_anchor, &smbios_anchor_len);
1313 if (smbios_anchor) {
1314 fw_cfg_add_file(vms->fw_cfg, "etc/smbios/smbios-tables",
1315 smbios_tables, smbios_tables_len);
1316 fw_cfg_add_file(vms->fw_cfg, "etc/smbios/smbios-anchor",
1317 smbios_anchor, smbios_anchor_len);
1321 static
1322 void virt_machine_done(Notifier *notifier, void *data)
1324 VirtMachineState *vms = container_of(notifier, VirtMachineState,
1325 machine_done);
1326 ARMCPU *cpu = ARM_CPU(first_cpu);
1327 struct arm_boot_info *info = &vms->bootinfo;
1328 AddressSpace *as = arm_boot_address_space(cpu, info);
1331 * If the user provided a dtb, we assume the dynamic sysbus nodes
1332 * already are integrated there. This corresponds to a use case where
1333 * the dynamic sysbus nodes are complex and their generation is not yet
1334 * supported. In that case the user can take charge of the guest dt
1335 * while qemu takes charge of the qom stuff.
1337 if (info->dtb_filename == NULL) {
1338 platform_bus_add_all_fdt_nodes(vms->fdt, "/intc",
1339 vms->memmap[VIRT_PLATFORM_BUS].base,
1340 vms->memmap[VIRT_PLATFORM_BUS].size,
1341 vms->irqmap[VIRT_PLATFORM_BUS]);
1343 if (arm_load_dtb(info->dtb_start, info, info->dtb_limit, as) < 0) {
1344 exit(1);
1347 virt_acpi_setup(vms);
1348 virt_build_smbios(vms);
1351 static uint64_t virt_cpu_mp_affinity(VirtMachineState *vms, int idx)
1353 uint8_t clustersz = ARM_DEFAULT_CPUS_PER_CLUSTER;
1354 VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms);
1356 if (!vmc->disallow_affinity_adjustment) {
1357 /* Adjust MPIDR like 64-bit KVM hosts, which incorporate the
1358 * GIC's target-list limitations. 32-bit KVM hosts currently
1359 * always create clusters of 4 CPUs, but that is expected to
1360 * change when they gain support for gicv3. When KVM is enabled
1361 * it will override the changes we make here, therefore our
1362 * purposes are to make TCG consistent (with 64-bit KVM hosts)
1363 * and to improve SGI efficiency.
1365 if (vms->gic_version == 3) {
1366 clustersz = GICV3_TARGETLIST_BITS;
1367 } else {
1368 clustersz = GIC_TARGETLIST_BITS;
1371 return arm_cpu_mp_affinity(idx, clustersz);
1374 static void virt_set_memmap(VirtMachineState *vms)
1376 MachineState *ms = MACHINE(vms);
1377 hwaddr base, device_memory_base, device_memory_size;
1378 int i;
1380 vms->memmap = extended_memmap;
1382 for (i = 0; i < ARRAY_SIZE(base_memmap); i++) {
1383 vms->memmap[i] = base_memmap[i];
1386 if (ms->ram_slots > ACPI_MAX_RAM_SLOTS) {
1387 error_report("unsupported number of memory slots: %"PRIu64,
1388 ms->ram_slots);
1389 exit(EXIT_FAILURE);
1393 * We compute the base of the high IO region depending on the
1394 * amount of initial and device memory. The device memory start/size
1395 * is aligned on 1GiB. We never put the high IO region below 256GiB
1396 * so that if maxram_size is < 255GiB we keep the legacy memory map.
1397 * The device region size assumes 1GiB page max alignment per slot.
1399 device_memory_base =
1400 ROUND_UP(vms->memmap[VIRT_MEM].base + ms->ram_size, GiB);
1401 device_memory_size = ms->maxram_size - ms->ram_size + ms->ram_slots * GiB;
1403 /* Base address of the high IO region */
1404 base = device_memory_base + ROUND_UP(device_memory_size, GiB);
1405 if (base < device_memory_base) {
1406 error_report("maxmem/slots too huge");
1407 exit(EXIT_FAILURE);
1409 if (base < vms->memmap[VIRT_MEM].base + LEGACY_RAMLIMIT_BYTES) {
1410 base = vms->memmap[VIRT_MEM].base + LEGACY_RAMLIMIT_BYTES;
1413 for (i = VIRT_LOWMEMMAP_LAST; i < ARRAY_SIZE(extended_memmap); i++) {
1414 hwaddr size = extended_memmap[i].size;
1416 base = ROUND_UP(base, size);
1417 vms->memmap[i].base = base;
1418 vms->memmap[i].size = size;
1419 base += size;
1421 vms->highest_gpa = base - 1;
1422 if (device_memory_size > 0) {
1423 ms->device_memory = g_malloc0(sizeof(*ms->device_memory));
1424 ms->device_memory->base = device_memory_base;
1425 memory_region_init(&ms->device_memory->mr, OBJECT(vms),
1426 "device-memory", device_memory_size);
1430 static void machvirt_init(MachineState *machine)
1432 VirtMachineState *vms = VIRT_MACHINE(machine);
1433 VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(machine);
1434 MachineClass *mc = MACHINE_GET_CLASS(machine);
1435 const CPUArchIdList *possible_cpus;
1436 qemu_irq pic[NUM_IRQS];
1437 MemoryRegion *sysmem = get_system_memory();
1438 MemoryRegion *secure_sysmem = NULL;
1439 int n, virt_max_cpus;
1440 MemoryRegion *ram = g_new(MemoryRegion, 1);
1441 bool firmware_loaded = bios_name || drive_get(IF_PFLASH, 0, 0);
1442 bool aarch64 = true;
1445 * In accelerated mode, the memory map is computed earlier in kvm_type()
1446 * to create a VM with the right number of IPA bits.
1448 if (!vms->memmap) {
1449 virt_set_memmap(vms);
1452 /* We can probe only here because during property set
1453 * KVM is not available yet
1455 if (vms->gic_version <= 0) {
1456 /* "host" or "max" */
1457 if (!kvm_enabled()) {
1458 if (vms->gic_version == 0) {
1459 error_report("gic-version=host requires KVM");
1460 exit(1);
1461 } else {
1462 /* "max": currently means 3 for TCG */
1463 vms->gic_version = 3;
1465 } else {
1466 vms->gic_version = kvm_arm_vgic_probe();
1467 if (!vms->gic_version) {
1468 error_report(
1469 "Unable to determine GIC version supported by host");
1470 exit(1);
1475 if (!cpu_type_valid(machine->cpu_type)) {
1476 error_report("mach-virt: CPU type %s not supported", machine->cpu_type);
1477 exit(1);
1480 /* If we have an EL3 boot ROM then the assumption is that it will
1481 * implement PSCI itself, so disable QEMU's internal implementation
1482 * so it doesn't get in the way. Instead of starting secondary
1483 * CPUs in PSCI powerdown state we will start them all running and
1484 * let the boot ROM sort them out.
1485 * The usual case is that we do use QEMU's PSCI implementation;
1486 * if the guest has EL2 then we will use SMC as the conduit,
1487 * and otherwise we will use HVC (for backwards compatibility and
1488 * because if we're using KVM then we must use HVC).
1490 if (vms->secure && firmware_loaded) {
1491 vms->psci_conduit = QEMU_PSCI_CONDUIT_DISABLED;
1492 } else if (vms->virt) {
1493 vms->psci_conduit = QEMU_PSCI_CONDUIT_SMC;
1494 } else {
1495 vms->psci_conduit = QEMU_PSCI_CONDUIT_HVC;
1498 /* The maximum number of CPUs depends on the GIC version, or on how
1499 * many redistributors we can fit into the memory map.
1501 if (vms->gic_version == 3) {
1502 virt_max_cpus =
1503 vms->memmap[VIRT_GIC_REDIST].size / GICV3_REDIST_SIZE;
1504 virt_max_cpus +=
1505 vms->memmap[VIRT_HIGH_GIC_REDIST2].size / GICV3_REDIST_SIZE;
1506 } else {
1507 virt_max_cpus = GIC_NCPU;
1510 if (max_cpus > virt_max_cpus) {
1511 error_report("Number of SMP CPUs requested (%d) exceeds max CPUs "
1512 "supported by machine 'mach-virt' (%d)",
1513 max_cpus, virt_max_cpus);
1514 exit(1);
1517 vms->smp_cpus = smp_cpus;
1519 if (machine->ram_size > vms->memmap[VIRT_MEM].size) {
1520 error_report("mach-virt: cannot model more than %dGB RAM",
1521 LEGACY_RAMLIMIT_GB);
1522 exit(1);
1525 if (vms->virt && kvm_enabled()) {
1526 error_report("mach-virt: KVM does not support providing "
1527 "Virtualization extensions to the guest CPU");
1528 exit(1);
1531 if (vms->secure) {
1532 if (kvm_enabled()) {
1533 error_report("mach-virt: KVM does not support Security extensions");
1534 exit(1);
1537 /* The Secure view of the world is the same as the NonSecure,
1538 * but with a few extra devices. Create it as a container region
1539 * containing the system memory at low priority; any secure-only
1540 * devices go in at higher priority and take precedence.
1542 secure_sysmem = g_new(MemoryRegion, 1);
1543 memory_region_init(secure_sysmem, OBJECT(machine), "secure-memory",
1544 UINT64_MAX);
1545 memory_region_add_subregion_overlap(secure_sysmem, 0, sysmem, -1);
1548 create_fdt(vms);
1550 possible_cpus = mc->possible_cpu_arch_ids(machine);
1551 for (n = 0; n < possible_cpus->len; n++) {
1552 Object *cpuobj;
1553 CPUState *cs;
1555 if (n >= smp_cpus) {
1556 break;
1559 cpuobj = object_new(possible_cpus->cpus[n].type);
1560 object_property_set_int(cpuobj, possible_cpus->cpus[n].arch_id,
1561 "mp-affinity", NULL);
1563 cs = CPU(cpuobj);
1564 cs->cpu_index = n;
1566 numa_cpu_pre_plug(&possible_cpus->cpus[cs->cpu_index], DEVICE(cpuobj),
1567 &error_fatal);
1569 aarch64 &= object_property_get_bool(cpuobj, "aarch64", NULL);
1571 if (!vms->secure) {
1572 object_property_set_bool(cpuobj, false, "has_el3", NULL);
1575 if (!vms->virt && object_property_find(cpuobj, "has_el2", NULL)) {
1576 object_property_set_bool(cpuobj, false, "has_el2", NULL);
1579 if (vms->psci_conduit != QEMU_PSCI_CONDUIT_DISABLED) {
1580 object_property_set_int(cpuobj, vms->psci_conduit,
1581 "psci-conduit", NULL);
1583 /* Secondary CPUs start in PSCI powered-down state */
1584 if (n > 0) {
1585 object_property_set_bool(cpuobj, true,
1586 "start-powered-off", NULL);
1590 if (vmc->no_pmu && object_property_find(cpuobj, "pmu", NULL)) {
1591 object_property_set_bool(cpuobj, false, "pmu", NULL);
1594 if (object_property_find(cpuobj, "reset-cbar", NULL)) {
1595 object_property_set_int(cpuobj, vms->memmap[VIRT_CPUPERIPHS].base,
1596 "reset-cbar", &error_abort);
1599 object_property_set_link(cpuobj, OBJECT(sysmem), "memory",
1600 &error_abort);
1601 if (vms->secure) {
1602 object_property_set_link(cpuobj, OBJECT(secure_sysmem),
1603 "secure-memory", &error_abort);
1606 object_property_set_bool(cpuobj, true, "realized", &error_fatal);
1607 object_unref(cpuobj);
1609 fdt_add_timer_nodes(vms);
1610 fdt_add_cpu_nodes(vms);
1612 if (!kvm_enabled()) {
1613 ARMCPU *cpu = ARM_CPU(first_cpu);
1614 bool aarch64 = object_property_get_bool(OBJECT(cpu), "aarch64", NULL);
1616 if (aarch64 && vms->highmem) {
1617 int requested_pa_size, pamax = arm_pamax(cpu);
1619 requested_pa_size = 64 - clz64(vms->highest_gpa);
1620 if (pamax < requested_pa_size) {
1621 error_report("VCPU supports less PA bits (%d) than requested "
1622 "by the memory map (%d)", pamax, requested_pa_size);
1623 exit(1);
1628 memory_region_allocate_system_memory(ram, NULL, "mach-virt.ram",
1629 machine->ram_size);
1630 memory_region_add_subregion(sysmem, vms->memmap[VIRT_MEM].base, ram);
1631 if (machine->device_memory) {
1632 memory_region_add_subregion(sysmem, machine->device_memory->base,
1633 &machine->device_memory->mr);
1636 create_flash(vms, sysmem, secure_sysmem ? secure_sysmem : sysmem);
1638 create_gic(vms, pic);
1640 fdt_add_pmu_nodes(vms);
1642 create_uart(vms, pic, VIRT_UART, sysmem, serial_hd(0));
1644 if (vms->secure) {
1645 create_secure_ram(vms, secure_sysmem);
1646 create_uart(vms, pic, VIRT_SECURE_UART, secure_sysmem, serial_hd(1));
1649 vms->highmem_ecam &= vms->highmem && (!firmware_loaded || aarch64);
1651 create_rtc(vms, pic);
1653 create_pcie(vms, pic);
1655 create_gpio(vms, pic);
1657 /* Create mmio transports, so the user can create virtio backends
1658 * (which will be automatically plugged in to the transports). If
1659 * no backend is created the transport will just sit harmlessly idle.
1661 create_virtio_devices(vms, pic);
1663 vms->fw_cfg = create_fw_cfg(vms, &address_space_memory);
1664 rom_set_fw(vms->fw_cfg);
1666 create_platform_bus(vms, pic);
1668 vms->bootinfo.ram_size = machine->ram_size;
1669 vms->bootinfo.kernel_filename = machine->kernel_filename;
1670 vms->bootinfo.kernel_cmdline = machine->kernel_cmdline;
1671 vms->bootinfo.initrd_filename = machine->initrd_filename;
1672 vms->bootinfo.nb_cpus = smp_cpus;
1673 vms->bootinfo.board_id = -1;
1674 vms->bootinfo.loader_start = vms->memmap[VIRT_MEM].base;
1675 vms->bootinfo.get_dtb = machvirt_dtb;
1676 vms->bootinfo.skip_dtb_autoload = true;
1677 vms->bootinfo.firmware_loaded = firmware_loaded;
1678 arm_load_kernel(ARM_CPU(first_cpu), &vms->bootinfo);
1680 vms->machine_done.notify = virt_machine_done;
1681 qemu_add_machine_init_done_notifier(&vms->machine_done);
1684 static bool virt_get_secure(Object *obj, Error **errp)
1686 VirtMachineState *vms = VIRT_MACHINE(obj);
1688 return vms->secure;
1691 static void virt_set_secure(Object *obj, bool value, Error **errp)
1693 VirtMachineState *vms = VIRT_MACHINE(obj);
1695 vms->secure = value;
1698 static bool virt_get_virt(Object *obj, Error **errp)
1700 VirtMachineState *vms = VIRT_MACHINE(obj);
1702 return vms->virt;
1705 static void virt_set_virt(Object *obj, bool value, Error **errp)
1707 VirtMachineState *vms = VIRT_MACHINE(obj);
1709 vms->virt = value;
1712 static bool virt_get_highmem(Object *obj, Error **errp)
1714 VirtMachineState *vms = VIRT_MACHINE(obj);
1716 return vms->highmem;
1719 static void virt_set_highmem(Object *obj, bool value, Error **errp)
1721 VirtMachineState *vms = VIRT_MACHINE(obj);
1723 vms->highmem = value;
1726 static bool virt_get_its(Object *obj, Error **errp)
1728 VirtMachineState *vms = VIRT_MACHINE(obj);
1730 return vms->its;
1733 static void virt_set_its(Object *obj, bool value, Error **errp)
1735 VirtMachineState *vms = VIRT_MACHINE(obj);
1737 vms->its = value;
1740 static char *virt_get_gic_version(Object *obj, Error **errp)
1742 VirtMachineState *vms = VIRT_MACHINE(obj);
1743 const char *val = vms->gic_version == 3 ? "3" : "2";
1745 return g_strdup(val);
1748 static void virt_set_gic_version(Object *obj, const char *value, Error **errp)
1750 VirtMachineState *vms = VIRT_MACHINE(obj);
1752 if (!strcmp(value, "3")) {
1753 vms->gic_version = 3;
1754 } else if (!strcmp(value, "2")) {
1755 vms->gic_version = 2;
1756 } else if (!strcmp(value, "host")) {
1757 vms->gic_version = 0; /* Will probe later */
1758 } else if (!strcmp(value, "max")) {
1759 vms->gic_version = -1; /* Will probe later */
1760 } else {
1761 error_setg(errp, "Invalid gic-version value");
1762 error_append_hint(errp, "Valid values are 3, 2, host, max.\n");
1766 static char *virt_get_iommu(Object *obj, Error **errp)
1768 VirtMachineState *vms = VIRT_MACHINE(obj);
1770 switch (vms->iommu) {
1771 case VIRT_IOMMU_NONE:
1772 return g_strdup("none");
1773 case VIRT_IOMMU_SMMUV3:
1774 return g_strdup("smmuv3");
1775 default:
1776 g_assert_not_reached();
1780 static void virt_set_iommu(Object *obj, const char *value, Error **errp)
1782 VirtMachineState *vms = VIRT_MACHINE(obj);
1784 if (!strcmp(value, "smmuv3")) {
1785 vms->iommu = VIRT_IOMMU_SMMUV3;
1786 } else if (!strcmp(value, "none")) {
1787 vms->iommu = VIRT_IOMMU_NONE;
1788 } else {
1789 error_setg(errp, "Invalid iommu value");
1790 error_append_hint(errp, "Valid values are none, smmuv3.\n");
1794 static CpuInstanceProperties
1795 virt_cpu_index_to_props(MachineState *ms, unsigned cpu_index)
1797 MachineClass *mc = MACHINE_GET_CLASS(ms);
1798 const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(ms);
1800 assert(cpu_index < possible_cpus->len);
1801 return possible_cpus->cpus[cpu_index].props;
1804 static int64_t virt_get_default_cpu_node_id(const MachineState *ms, int idx)
1806 return idx % nb_numa_nodes;
1809 static const CPUArchIdList *virt_possible_cpu_arch_ids(MachineState *ms)
1811 int n;
1812 VirtMachineState *vms = VIRT_MACHINE(ms);
1814 if (ms->possible_cpus) {
1815 assert(ms->possible_cpus->len == max_cpus);
1816 return ms->possible_cpus;
1819 ms->possible_cpus = g_malloc0(sizeof(CPUArchIdList) +
1820 sizeof(CPUArchId) * max_cpus);
1821 ms->possible_cpus->len = max_cpus;
1822 for (n = 0; n < ms->possible_cpus->len; n++) {
1823 ms->possible_cpus->cpus[n].type = ms->cpu_type;
1824 ms->possible_cpus->cpus[n].arch_id =
1825 virt_cpu_mp_affinity(vms, n);
1826 ms->possible_cpus->cpus[n].props.has_thread_id = true;
1827 ms->possible_cpus->cpus[n].props.thread_id = n;
1829 return ms->possible_cpus;
1832 static void virt_machine_device_plug_cb(HotplugHandler *hotplug_dev,
1833 DeviceState *dev, Error **errp)
1835 VirtMachineState *vms = VIRT_MACHINE(hotplug_dev);
1837 if (vms->platform_bus_dev) {
1838 if (object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE)) {
1839 platform_bus_link_device(PLATFORM_BUS_DEVICE(vms->platform_bus_dev),
1840 SYS_BUS_DEVICE(dev));
1845 static HotplugHandler *virt_machine_get_hotplug_handler(MachineState *machine,
1846 DeviceState *dev)
1848 if (object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE)) {
1849 return HOTPLUG_HANDLER(machine);
1852 return NULL;
1856 * for arm64 kvm_type [7-0] encodes the requested number of bits
1857 * in the IPA address space
1859 static int virt_kvm_type(MachineState *ms, const char *type_str)
1861 VirtMachineState *vms = VIRT_MACHINE(ms);
1862 int max_vm_pa_size = kvm_arm_get_max_vm_ipa_size(ms);
1863 int requested_pa_size;
1865 /* we freeze the memory map to compute the highest gpa */
1866 virt_set_memmap(vms);
1868 requested_pa_size = 64 - clz64(vms->highest_gpa);
1870 if (requested_pa_size > max_vm_pa_size) {
1871 error_report("-m and ,maxmem option values "
1872 "require an IPA range (%d bits) larger than "
1873 "the one supported by the host (%d bits)",
1874 requested_pa_size, max_vm_pa_size);
1875 exit(1);
1878 * By default we return 0 which corresponds to an implicit legacy
1879 * 40b IPA setting. Otherwise we return the actual requested PA
1880 * logsize
1882 return requested_pa_size > 40 ? requested_pa_size : 0;
1885 static void virt_machine_class_init(ObjectClass *oc, void *data)
1887 MachineClass *mc = MACHINE_CLASS(oc);
1888 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
1890 mc->init = machvirt_init;
1891 /* Start with max_cpus set to 512, which is the maximum supported by KVM.
1892 * The value may be reduced later when we have more information about the
1893 * configuration of the particular instance.
1895 mc->max_cpus = 512;
1896 machine_class_allow_dynamic_sysbus_dev(mc, TYPE_VFIO_CALXEDA_XGMAC);
1897 machine_class_allow_dynamic_sysbus_dev(mc, TYPE_VFIO_AMD_XGBE);
1898 machine_class_allow_dynamic_sysbus_dev(mc, TYPE_RAMFB_DEVICE);
1899 machine_class_allow_dynamic_sysbus_dev(mc, TYPE_VFIO_PLATFORM);
1900 mc->block_default_type = IF_VIRTIO;
1901 mc->no_cdrom = 1;
1902 mc->pci_allow_0_address = true;
1903 /* We know we will never create a pre-ARMv7 CPU which needs 1K pages */
1904 mc->minimum_page_bits = 12;
1905 mc->possible_cpu_arch_ids = virt_possible_cpu_arch_ids;
1906 mc->cpu_index_to_instance_props = virt_cpu_index_to_props;
1907 mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-a15");
1908 mc->get_default_cpu_node_id = virt_get_default_cpu_node_id;
1909 mc->kvm_type = virt_kvm_type;
1910 assert(!mc->get_hotplug_handler);
1911 mc->get_hotplug_handler = virt_machine_get_hotplug_handler;
1912 hc->plug = virt_machine_device_plug_cb;
1915 static void virt_instance_init(Object *obj)
1917 VirtMachineState *vms = VIRT_MACHINE(obj);
1918 VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms);
1920 /* EL3 is disabled by default on virt: this makes us consistent
1921 * between KVM and TCG for this board, and it also allows us to
1922 * boot UEFI blobs which assume no TrustZone support.
1924 vms->secure = false;
1925 object_property_add_bool(obj, "secure", virt_get_secure,
1926 virt_set_secure, NULL);
1927 object_property_set_description(obj, "secure",
1928 "Set on/off to enable/disable the ARM "
1929 "Security Extensions (TrustZone)",
1930 NULL);
1932 /* EL2 is also disabled by default, for similar reasons */
1933 vms->virt = false;
1934 object_property_add_bool(obj, "virtualization", virt_get_virt,
1935 virt_set_virt, NULL);
1936 object_property_set_description(obj, "virtualization",
1937 "Set on/off to enable/disable emulating a "
1938 "guest CPU which implements the ARM "
1939 "Virtualization Extensions",
1940 NULL);
1942 /* High memory is enabled by default */
1943 vms->highmem = true;
1944 object_property_add_bool(obj, "highmem", virt_get_highmem,
1945 virt_set_highmem, NULL);
1946 object_property_set_description(obj, "highmem",
1947 "Set on/off to enable/disable using "
1948 "physical address space above 32 bits",
1949 NULL);
1950 /* Default GIC type is v2 */
1951 vms->gic_version = 2;
1952 object_property_add_str(obj, "gic-version", virt_get_gic_version,
1953 virt_set_gic_version, NULL);
1954 object_property_set_description(obj, "gic-version",
1955 "Set GIC version. "
1956 "Valid values are 2, 3 and host", NULL);
1958 vms->highmem_ecam = !vmc->no_highmem_ecam;
1960 if (vmc->no_its) {
1961 vms->its = false;
1962 } else {
1963 /* Default allows ITS instantiation */
1964 vms->its = true;
1965 object_property_add_bool(obj, "its", virt_get_its,
1966 virt_set_its, NULL);
1967 object_property_set_description(obj, "its",
1968 "Set on/off to enable/disable "
1969 "ITS instantiation",
1970 NULL);
1973 /* Default disallows iommu instantiation */
1974 vms->iommu = VIRT_IOMMU_NONE;
1975 object_property_add_str(obj, "iommu", virt_get_iommu, virt_set_iommu, NULL);
1976 object_property_set_description(obj, "iommu",
1977 "Set the IOMMU type. "
1978 "Valid values are none and smmuv3",
1979 NULL);
1981 vms->irqmap = a15irqmap;
1984 static const TypeInfo virt_machine_info = {
1985 .name = TYPE_VIRT_MACHINE,
1986 .parent = TYPE_MACHINE,
1987 .abstract = true,
1988 .instance_size = sizeof(VirtMachineState),
1989 .class_size = sizeof(VirtMachineClass),
1990 .class_init = virt_machine_class_init,
1991 .instance_init = virt_instance_init,
1992 .interfaces = (InterfaceInfo[]) {
1993 { TYPE_HOTPLUG_HANDLER },
1998 static void machvirt_machine_init(void)
2000 type_register_static(&virt_machine_info);
2002 type_init(machvirt_machine_init);
2004 static void virt_machine_4_0_options(MachineClass *mc)
2007 DEFINE_VIRT_MACHINE_AS_LATEST(4, 0)
2009 static void virt_machine_3_1_options(MachineClass *mc)
2011 virt_machine_4_0_options(mc);
2012 compat_props_add(mc->compat_props, hw_compat_3_1, hw_compat_3_1_len);
2014 DEFINE_VIRT_MACHINE(3, 1)
2016 static void virt_machine_3_0_options(MachineClass *mc)
2018 virt_machine_3_1_options(mc);
2019 compat_props_add(mc->compat_props, hw_compat_3_0, hw_compat_3_0_len);
2021 DEFINE_VIRT_MACHINE(3, 0)
2023 static void virt_machine_2_12_options(MachineClass *mc)
2025 VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
2027 virt_machine_3_0_options(mc);
2028 compat_props_add(mc->compat_props, hw_compat_2_12, hw_compat_2_12_len);
2029 vmc->no_highmem_ecam = true;
2030 mc->max_cpus = 255;
2032 DEFINE_VIRT_MACHINE(2, 12)
2034 static void virt_machine_2_11_options(MachineClass *mc)
2036 VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
2038 virt_machine_2_12_options(mc);
2039 compat_props_add(mc->compat_props, hw_compat_2_11, hw_compat_2_11_len);
2040 vmc->smbios_old_sys_ver = true;
2042 DEFINE_VIRT_MACHINE(2, 11)
2044 static void virt_machine_2_10_options(MachineClass *mc)
2046 virt_machine_2_11_options(mc);
2047 compat_props_add(mc->compat_props, hw_compat_2_10, hw_compat_2_10_len);
2048 /* before 2.11 we never faulted accesses to bad addresses */
2049 mc->ignore_memory_transaction_failures = true;
2051 DEFINE_VIRT_MACHINE(2, 10)
2053 static void virt_machine_2_9_options(MachineClass *mc)
2055 virt_machine_2_10_options(mc);
2056 compat_props_add(mc->compat_props, hw_compat_2_9, hw_compat_2_9_len);
2058 DEFINE_VIRT_MACHINE(2, 9)
2060 static void virt_machine_2_8_options(MachineClass *mc)
2062 VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
2064 virt_machine_2_9_options(mc);
2065 compat_props_add(mc->compat_props, hw_compat_2_8, hw_compat_2_8_len);
2066 /* For 2.8 and earlier we falsely claimed in the DT that
2067 * our timers were edge-triggered, not level-triggered.
2069 vmc->claim_edge_triggered_timers = true;
2071 DEFINE_VIRT_MACHINE(2, 8)
2073 static void virt_machine_2_7_options(MachineClass *mc)
2075 VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
2077 virt_machine_2_8_options(mc);
2078 compat_props_add(mc->compat_props, hw_compat_2_7, hw_compat_2_7_len);
2079 /* ITS was introduced with 2.8 */
2080 vmc->no_its = true;
2081 /* Stick with 1K pages for migration compatibility */
2082 mc->minimum_page_bits = 0;
2084 DEFINE_VIRT_MACHINE(2, 7)
2086 static void virt_machine_2_6_options(MachineClass *mc)
2088 VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
2090 virt_machine_2_7_options(mc);
2091 compat_props_add(mc->compat_props, hw_compat_2_6, hw_compat_2_6_len);
2092 vmc->disallow_affinity_adjustment = true;
2093 /* Disable PMU for 2.6 as PMU support was first introduced in 2.7 */
2094 vmc->no_pmu = true;
2096 DEFINE_VIRT_MACHINE(2, 6)