1 /* Support for generating ACPI tables and passing them to Guests
3 * ARM virt ACPI generation
5 * Copyright (C) 2008-2010 Kevin O'Connor <kevin@koconnor.net>
6 * Copyright (C) 2006 Fabrice Bellard
7 * Copyright (C) 2013 Red Hat Inc
9 * Author: Michael S. Tsirkin <mst@redhat.com>
11 * Copyright (c) 2015 HUAWEI TECHNOLOGIES CO.,LTD.
13 * Author: Shannon Zhao <zhaoshenglong@huawei.com>
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, see <http://www.gnu.org/licenses/>.
29 #include "qemu/osdep.h"
30 #include "qapi/error.h"
31 #include "qemu/bitmap.h"
33 #include "hw/core/cpu.h"
34 #include "target/arm/cpu.h"
35 #include "hw/acpi/acpi-defs.h"
36 #include "hw/acpi/acpi.h"
37 #include "hw/nvram/fw_cfg.h"
38 #include "hw/acpi/bios-linker-loader.h"
39 #include "hw/acpi/aml-build.h"
40 #include "hw/acpi/utils.h"
41 #include "hw/acpi/pci.h"
42 #include "hw/acpi/memory_hotplug.h"
43 #include "hw/acpi/generic_event_device.h"
44 #include "hw/acpi/tpm.h"
45 #include "hw/pci/pcie_host.h"
46 #include "hw/pci/pci.h"
47 #include "hw/pci-host/gpex.h"
48 #include "hw/arm/virt.h"
49 #include "hw/mem/nvdimm.h"
50 #include "hw/platform-bus.h"
51 #include "sysemu/numa.h"
52 #include "sysemu/reset.h"
53 #include "sysemu/tpm.h"
55 #include "migration/vmstate.h"
56 #include "hw/acpi/ghes.h"
58 #define ARM_SPI_BASE 32
60 #define ACPI_BUILD_TABLE_SIZE 0x20000
62 static void acpi_dsdt_add_cpus(Aml
*scope
, VirtMachineState
*vms
)
64 MachineState
*ms
= MACHINE(vms
);
67 for (i
= 0; i
< ms
->smp
.cpus
; i
++) {
68 Aml
*dev
= aml_device("C%.03X", i
);
69 aml_append(dev
, aml_name_decl("_HID", aml_string("ACPI0007")));
70 aml_append(dev
, aml_name_decl("_UID", aml_int(i
)));
71 aml_append(scope
, dev
);
75 static void acpi_dsdt_add_uart(Aml
*scope
, const MemMapEntry
*uart_memmap
,
78 Aml
*dev
= aml_device("COM0");
79 aml_append(dev
, aml_name_decl("_HID", aml_string("ARMH0011")));
80 aml_append(dev
, aml_name_decl("_UID", aml_int(0)));
82 Aml
*crs
= aml_resource_template();
83 aml_append(crs
, aml_memory32_fixed(uart_memmap
->base
,
84 uart_memmap
->size
, AML_READ_WRITE
));
86 aml_interrupt(AML_CONSUMER
, AML_LEVEL
, AML_ACTIVE_HIGH
,
87 AML_EXCLUSIVE
, &uart_irq
, 1));
88 aml_append(dev
, aml_name_decl("_CRS", crs
));
90 aml_append(scope
, dev
);
93 static void acpi_dsdt_add_fw_cfg(Aml
*scope
, const MemMapEntry
*fw_cfg_memmap
)
95 Aml
*dev
= aml_device("FWCF");
96 aml_append(dev
, aml_name_decl("_HID", aml_string("QEMU0002")));
97 /* device present, functioning, decoding, not shown in UI */
98 aml_append(dev
, aml_name_decl("_STA", aml_int(0xB)));
99 aml_append(dev
, aml_name_decl("_CCA", aml_int(1)));
101 Aml
*crs
= aml_resource_template();
102 aml_append(crs
, aml_memory32_fixed(fw_cfg_memmap
->base
,
103 fw_cfg_memmap
->size
, AML_READ_WRITE
));
104 aml_append(dev
, aml_name_decl("_CRS", crs
));
105 aml_append(scope
, dev
);
108 static void acpi_dsdt_add_flash(Aml
*scope
, const MemMapEntry
*flash_memmap
)
111 hwaddr base
= flash_memmap
->base
;
112 hwaddr size
= flash_memmap
->size
/ 2;
114 dev
= aml_device("FLS0");
115 aml_append(dev
, aml_name_decl("_HID", aml_string("LNRO0015")));
116 aml_append(dev
, aml_name_decl("_UID", aml_int(0)));
118 crs
= aml_resource_template();
119 aml_append(crs
, aml_memory32_fixed(base
, size
, AML_READ_WRITE
));
120 aml_append(dev
, aml_name_decl("_CRS", crs
));
121 aml_append(scope
, dev
);
123 dev
= aml_device("FLS1");
124 aml_append(dev
, aml_name_decl("_HID", aml_string("LNRO0015")));
125 aml_append(dev
, aml_name_decl("_UID", aml_int(1)));
126 crs
= aml_resource_template();
127 aml_append(crs
, aml_memory32_fixed(base
+ size
, size
, AML_READ_WRITE
));
128 aml_append(dev
, aml_name_decl("_CRS", crs
));
129 aml_append(scope
, dev
);
132 static void acpi_dsdt_add_virtio(Aml
*scope
,
133 const MemMapEntry
*virtio_mmio_memmap
,
134 uint32_t mmio_irq
, int num
)
136 hwaddr base
= virtio_mmio_memmap
->base
;
137 hwaddr size
= virtio_mmio_memmap
->size
;
140 for (i
= 0; i
< num
; i
++) {
141 uint32_t irq
= mmio_irq
+ i
;
142 Aml
*dev
= aml_device("VR%02u", i
);
143 aml_append(dev
, aml_name_decl("_HID", aml_string("LNRO0005")));
144 aml_append(dev
, aml_name_decl("_UID", aml_int(i
)));
145 aml_append(dev
, aml_name_decl("_CCA", aml_int(1)));
147 Aml
*crs
= aml_resource_template();
148 aml_append(crs
, aml_memory32_fixed(base
, size
, AML_READ_WRITE
));
150 aml_interrupt(AML_CONSUMER
, AML_LEVEL
, AML_ACTIVE_HIGH
,
151 AML_EXCLUSIVE
, &irq
, 1));
152 aml_append(dev
, aml_name_decl("_CRS", crs
));
153 aml_append(scope
, dev
);
158 static void acpi_dsdt_add_pci(Aml
*scope
, const MemMapEntry
*memmap
,
159 uint32_t irq
, bool use_highmem
, bool highmem_ecam
,
160 VirtMachineState
*vms
)
162 int ecam_id
= VIRT_ECAM_ID(highmem_ecam
);
163 struct GPEXConfig cfg
= {
164 .mmio32
= memmap
[VIRT_PCIE_MMIO
],
165 .pio
= memmap
[VIRT_PCIE_PIO
],
166 .ecam
= memmap
[ecam_id
],
172 cfg
.mmio64
= memmap
[VIRT_HIGH_PCIE_MMIO
];
175 acpi_dsdt_add_gpex(scope
, &cfg
);
178 static void acpi_dsdt_add_gpio(Aml
*scope
, const MemMapEntry
*gpio_memmap
,
181 Aml
*dev
= aml_device("GPO0");
182 aml_append(dev
, aml_name_decl("_HID", aml_string("ARMH0061")));
183 aml_append(dev
, aml_name_decl("_UID", aml_int(0)));
185 Aml
*crs
= aml_resource_template();
186 aml_append(crs
, aml_memory32_fixed(gpio_memmap
->base
, gpio_memmap
->size
,
188 aml_append(crs
, aml_interrupt(AML_CONSUMER
, AML_LEVEL
, AML_ACTIVE_HIGH
,
189 AML_EXCLUSIVE
, &gpio_irq
, 1));
190 aml_append(dev
, aml_name_decl("_CRS", crs
));
192 Aml
*aei
= aml_resource_template();
193 /* Pin 3 for power button */
194 const uint32_t pin_list
[1] = {3};
195 aml_append(aei
, aml_gpio_int(AML_CONSUMER
, AML_EDGE
, AML_ACTIVE_HIGH
,
196 AML_EXCLUSIVE
, AML_PULL_UP
, 0, pin_list
, 1,
198 aml_append(dev
, aml_name_decl("_AEI", aei
));
200 /* _E03 is handle for power button */
201 Aml
*method
= aml_method("_E03", 0, AML_NOTSERIALIZED
);
202 aml_append(method
, aml_notify(aml_name(ACPI_POWER_BUTTON_DEVICE
),
204 aml_append(dev
, method
);
205 aml_append(scope
, dev
);
208 static void acpi_dsdt_add_tpm(Aml
*scope
, VirtMachineState
*vms
)
210 PlatformBusDevice
*pbus
= PLATFORM_BUS_DEVICE(vms
->platform_bus_dev
);
211 hwaddr pbus_base
= vms
->memmap
[VIRT_PLATFORM_BUS
].base
;
212 SysBusDevice
*sbdev
= SYS_BUS_DEVICE(tpm_find());
213 MemoryRegion
*sbdev_mr
;
220 tpm_base
= platform_bus_get_mmio_addr(pbus
, sbdev
, 0);
221 assert(tpm_base
!= -1);
223 tpm_base
+= pbus_base
;
225 sbdev_mr
= sysbus_mmio_get_region(sbdev
, 0);
227 Aml
*dev
= aml_device("TPM0");
228 aml_append(dev
, aml_name_decl("_HID", aml_string("MSFT0101")));
229 aml_append(dev
, aml_name_decl("_UID", aml_int(0)));
231 Aml
*crs
= aml_resource_template();
233 aml_memory32_fixed(tpm_base
,
234 (uint32_t)memory_region_size(sbdev_mr
),
236 aml_append(dev
, aml_name_decl("_CRS", crs
));
237 aml_append(scope
, dev
);
241 build_iort(GArray
*table_data
, BIOSLinker
*linker
, VirtMachineState
*vms
)
243 int nb_nodes
, iort_start
= table_data
->len
;
244 AcpiIortIdMapping
*idmap
;
245 AcpiIortItsGroup
*its
;
248 size_t node_size
, iort_node_offset
, iort_length
, smmu_offset
= 0;
251 iort
= acpi_data_push(table_data
, sizeof(*iort
));
253 if (vms
->iommu
== VIRT_IOMMU_SMMUV3
) {
254 nb_nodes
= 3; /* RC, ITS, SMMUv3 */
256 nb_nodes
= 2; /* RC, ITS */
259 iort_length
= sizeof(*iort
);
260 iort
->node_count
= cpu_to_le32(nb_nodes
);
262 * Use a copy in case table_data->data moves during acpi_data_push
265 iort_node_offset
= sizeof(*iort
);
266 iort
->node_offset
= cpu_to_le32(iort_node_offset
);
269 node_size
= sizeof(*its
) + sizeof(uint32_t);
270 iort_length
+= node_size
;
271 its
= acpi_data_push(table_data
, node_size
);
273 its
->type
= ACPI_IORT_NODE_ITS_GROUP
;
274 its
->length
= cpu_to_le16(node_size
);
275 its
->its_count
= cpu_to_le32(1);
276 its
->identifiers
[0] = 0; /* MADT translation_id */
278 if (vms
->iommu
== VIRT_IOMMU_SMMUV3
) {
279 int irq
= vms
->irqmap
[VIRT_SMMU
] + ARM_SPI_BASE
;
282 smmu_offset
= iort_node_offset
+ node_size
;
283 node_size
= sizeof(*smmu
) + sizeof(*idmap
);
284 iort_length
+= node_size
;
285 smmu
= acpi_data_push(table_data
, node_size
);
287 smmu
->type
= ACPI_IORT_NODE_SMMU_V3
;
288 smmu
->length
= cpu_to_le16(node_size
);
289 smmu
->mapping_count
= cpu_to_le32(1);
290 smmu
->mapping_offset
= cpu_to_le32(sizeof(*smmu
));
291 smmu
->base_address
= cpu_to_le64(vms
->memmap
[VIRT_SMMU
].base
);
292 smmu
->flags
= cpu_to_le32(ACPI_IORT_SMMU_V3_COHACC_OVERRIDE
);
293 smmu
->event_gsiv
= cpu_to_le32(irq
);
294 smmu
->pri_gsiv
= cpu_to_le32(irq
+ 1);
295 smmu
->gerr_gsiv
= cpu_to_le32(irq
+ 2);
296 smmu
->sync_gsiv
= cpu_to_le32(irq
+ 3);
298 /* Identity RID mapping covering the whole input RID range */
299 idmap
= &smmu
->id_mapping_array
[0];
300 idmap
->input_base
= 0;
301 idmap
->id_count
= cpu_to_le32(0xFFFF);
302 idmap
->output_base
= 0;
303 /* output IORT node is the ITS group node (the first node) */
304 idmap
->output_reference
= cpu_to_le32(iort_node_offset
);
307 /* Root Complex Node */
308 node_size
= sizeof(*rc
) + sizeof(*idmap
);
309 iort_length
+= node_size
;
310 rc
= acpi_data_push(table_data
, node_size
);
312 rc
->type
= ACPI_IORT_NODE_PCI_ROOT_COMPLEX
;
313 rc
->length
= cpu_to_le16(node_size
);
314 rc
->mapping_count
= cpu_to_le32(1);
315 rc
->mapping_offset
= cpu_to_le32(sizeof(*rc
));
317 /* fully coherent device */
318 rc
->memory_properties
.cache_coherency
= cpu_to_le32(1);
319 rc
->memory_properties
.memory_flags
= 0x3; /* CCA = CPM = DCAS = 1 */
320 rc
->pci_segment_number
= 0; /* MCFG pci_segment */
322 /* Identity RID mapping covering the whole input RID range */
323 idmap
= &rc
->id_mapping_array
[0];
324 idmap
->input_base
= 0;
325 idmap
->id_count
= cpu_to_le32(0xFFFF);
326 idmap
->output_base
= 0;
328 if (vms
->iommu
== VIRT_IOMMU_SMMUV3
) {
329 /* output IORT node is the smmuv3 node */
330 idmap
->output_reference
= cpu_to_le32(smmu_offset
);
332 /* output IORT node is the ITS group node (the first node) */
333 idmap
->output_reference
= cpu_to_le32(iort_node_offset
);
337 * Update the pointer address in case table_data->data moves during above
338 * acpi_data_push operations.
340 iort
= (AcpiIortTable
*)(table_data
->data
+ iort_start
);
341 iort
->length
= cpu_to_le32(iort_length
);
343 build_header(linker
, table_data
, (void *)(table_data
->data
+ iort_start
),
344 "IORT", table_data
->len
- iort_start
, 0, NULL
, NULL
);
348 build_spcr(GArray
*table_data
, BIOSLinker
*linker
, VirtMachineState
*vms
)
350 AcpiSerialPortConsoleRedirection
*spcr
;
351 const MemMapEntry
*uart_memmap
= &vms
->memmap
[VIRT_UART
];
352 int irq
= vms
->irqmap
[VIRT_UART
] + ARM_SPI_BASE
;
353 int spcr_start
= table_data
->len
;
355 spcr
= acpi_data_push(table_data
, sizeof(*spcr
));
357 spcr
->interface_type
= 0x3; /* ARM PL011 UART */
359 spcr
->base_address
.space_id
= AML_SYSTEM_MEMORY
;
360 spcr
->base_address
.bit_width
= 8;
361 spcr
->base_address
.bit_offset
= 0;
362 spcr
->base_address
.access_width
= 1;
363 spcr
->base_address
.address
= cpu_to_le64(uart_memmap
->base
);
365 spcr
->interrupt_types
= (1 << 3); /* Bit[3] ARMH GIC interrupt */
366 spcr
->gsi
= cpu_to_le32(irq
); /* Global System Interrupt */
368 spcr
->baud
= 3; /* Baud Rate: 3 = 9600 */
369 spcr
->parity
= 0; /* No Parity */
370 spcr
->stopbits
= 1; /* 1 Stop bit */
371 spcr
->flowctrl
= (1 << 1); /* Bit[1] = RTS/CTS hardware flow control */
372 spcr
->term_type
= 0; /* Terminal Type: 0 = VT100 */
374 spcr
->pci_device_id
= 0xffff; /* PCI Device ID: not a PCI device */
375 spcr
->pci_vendor_id
= 0xffff; /* PCI Vendor ID: not a PCI device */
377 build_header(linker
, table_data
, (void *)(table_data
->data
+ spcr_start
),
378 "SPCR", table_data
->len
- spcr_start
, 2, NULL
, NULL
);
382 build_srat(GArray
*table_data
, BIOSLinker
*linker
, VirtMachineState
*vms
)
384 AcpiSystemResourceAffinityTable
*srat
;
385 AcpiSratProcessorGiccAffinity
*core
;
386 AcpiSratMemoryAffinity
*numamem
;
389 MachineClass
*mc
= MACHINE_GET_CLASS(vms
);
390 MachineState
*ms
= MACHINE(vms
);
391 const CPUArchIdList
*cpu_list
= mc
->possible_cpu_arch_ids(ms
);
393 srat_start
= table_data
->len
;
394 srat
= acpi_data_push(table_data
, sizeof(*srat
));
395 srat
->reserved1
= cpu_to_le32(1);
397 for (i
= 0; i
< cpu_list
->len
; ++i
) {
398 core
= acpi_data_push(table_data
, sizeof(*core
));
399 core
->type
= ACPI_SRAT_PROCESSOR_GICC
;
400 core
->length
= sizeof(*core
);
401 core
->proximity
= cpu_to_le32(cpu_list
->cpus
[i
].props
.node_id
);
402 core
->acpi_processor_uid
= cpu_to_le32(i
);
403 core
->flags
= cpu_to_le32(1);
406 mem_base
= vms
->memmap
[VIRT_MEM
].base
;
407 for (i
= 0; i
< ms
->numa_state
->num_nodes
; ++i
) {
408 if (ms
->numa_state
->nodes
[i
].node_mem
> 0) {
409 numamem
= acpi_data_push(table_data
, sizeof(*numamem
));
410 build_srat_memory(numamem
, mem_base
,
411 ms
->numa_state
->nodes
[i
].node_mem
, i
,
412 MEM_AFFINITY_ENABLED
);
413 mem_base
+= ms
->numa_state
->nodes
[i
].node_mem
;
417 if (ms
->nvdimms_state
->is_enabled
) {
418 nvdimm_build_srat(table_data
);
421 if (ms
->device_memory
) {
422 numamem
= acpi_data_push(table_data
, sizeof *numamem
);
423 build_srat_memory(numamem
, ms
->device_memory
->base
,
424 memory_region_size(&ms
->device_memory
->mr
),
425 ms
->numa_state
->num_nodes
- 1,
426 MEM_AFFINITY_HOTPLUGGABLE
| MEM_AFFINITY_ENABLED
);
429 build_header(linker
, table_data
, (void *)(table_data
->data
+ srat_start
),
430 "SRAT", table_data
->len
- srat_start
, 3, NULL
, NULL
);
435 build_gtdt(GArray
*table_data
, BIOSLinker
*linker
, VirtMachineState
*vms
)
437 VirtMachineClass
*vmc
= VIRT_MACHINE_GET_CLASS(vms
);
438 int gtdt_start
= table_data
->len
;
439 AcpiGenericTimerTable
*gtdt
;
442 if (vmc
->claim_edge_triggered_timers
) {
443 irqflags
= ACPI_GTDT_INTERRUPT_MODE_EDGE
;
445 irqflags
= ACPI_GTDT_INTERRUPT_MODE_LEVEL
;
448 gtdt
= acpi_data_push(table_data
, sizeof *gtdt
);
449 /* The interrupt values are the same with the device tree when adding 16 */
450 gtdt
->secure_el1_interrupt
= cpu_to_le32(ARCH_TIMER_S_EL1_IRQ
+ 16);
451 gtdt
->secure_el1_flags
= cpu_to_le32(irqflags
);
453 gtdt
->non_secure_el1_interrupt
= cpu_to_le32(ARCH_TIMER_NS_EL1_IRQ
+ 16);
454 gtdt
->non_secure_el1_flags
= cpu_to_le32(irqflags
|
455 ACPI_GTDT_CAP_ALWAYS_ON
);
457 gtdt
->virtual_timer_interrupt
= cpu_to_le32(ARCH_TIMER_VIRT_IRQ
+ 16);
458 gtdt
->virtual_timer_flags
= cpu_to_le32(irqflags
);
460 gtdt
->non_secure_el2_interrupt
= cpu_to_le32(ARCH_TIMER_NS_EL2_IRQ
+ 16);
461 gtdt
->non_secure_el2_flags
= cpu_to_le32(irqflags
);
463 build_header(linker
, table_data
,
464 (void *)(table_data
->data
+ gtdt_start
), "GTDT",
465 table_data
->len
- gtdt_start
, 2, NULL
, NULL
);
470 build_madt(GArray
*table_data
, BIOSLinker
*linker
, VirtMachineState
*vms
)
472 VirtMachineClass
*vmc
= VIRT_MACHINE_GET_CLASS(vms
);
473 int madt_start
= table_data
->len
;
474 const MemMapEntry
*memmap
= vms
->memmap
;
475 const int *irqmap
= vms
->irqmap
;
476 AcpiMadtGenericDistributor
*gicd
;
477 AcpiMadtGenericMsiFrame
*gic_msi
;
480 acpi_data_push(table_data
, sizeof(AcpiMultipleApicTable
));
482 gicd
= acpi_data_push(table_data
, sizeof *gicd
);
483 gicd
->type
= ACPI_APIC_GENERIC_DISTRIBUTOR
;
484 gicd
->length
= sizeof(*gicd
);
485 gicd
->base_address
= cpu_to_le64(memmap
[VIRT_GIC_DIST
].base
);
486 gicd
->version
= vms
->gic_version
;
488 for (i
= 0; i
< MACHINE(vms
)->smp
.cpus
; i
++) {
489 AcpiMadtGenericCpuInterface
*gicc
= acpi_data_push(table_data
,
491 ARMCPU
*armcpu
= ARM_CPU(qemu_get_cpu(i
));
493 gicc
->type
= ACPI_APIC_GENERIC_CPU_INTERFACE
;
494 gicc
->length
= sizeof(*gicc
);
495 if (vms
->gic_version
== 2) {
496 gicc
->base_address
= cpu_to_le64(memmap
[VIRT_GIC_CPU
].base
);
497 gicc
->gich_base_address
= cpu_to_le64(memmap
[VIRT_GIC_HYP
].base
);
498 gicc
->gicv_base_address
= cpu_to_le64(memmap
[VIRT_GIC_VCPU
].base
);
500 gicc
->cpu_interface_number
= cpu_to_le32(i
);
501 gicc
->arm_mpidr
= cpu_to_le64(armcpu
->mp_affinity
);
502 gicc
->uid
= cpu_to_le32(i
);
503 gicc
->flags
= cpu_to_le32(ACPI_MADT_GICC_ENABLED
);
505 if (arm_feature(&armcpu
->env
, ARM_FEATURE_PMU
)) {
506 gicc
->performance_interrupt
= cpu_to_le32(PPI(VIRTUAL_PMU_IRQ
));
509 gicc
->vgic_interrupt
= cpu_to_le32(PPI(ARCH_GIC_MAINT_IRQ
));
513 if (vms
->gic_version
== 3) {
514 AcpiMadtGenericTranslator
*gic_its
;
515 int nb_redist_regions
= virt_gicv3_redist_region_count(vms
);
516 AcpiMadtGenericRedistributor
*gicr
= acpi_data_push(table_data
,
519 gicr
->type
= ACPI_APIC_GENERIC_REDISTRIBUTOR
;
520 gicr
->length
= sizeof(*gicr
);
521 gicr
->base_address
= cpu_to_le64(memmap
[VIRT_GIC_REDIST
].base
);
522 gicr
->range_length
= cpu_to_le32(memmap
[VIRT_GIC_REDIST
].size
);
524 if (nb_redist_regions
== 2) {
525 gicr
= acpi_data_push(table_data
, sizeof(*gicr
));
526 gicr
->type
= ACPI_APIC_GENERIC_REDISTRIBUTOR
;
527 gicr
->length
= sizeof(*gicr
);
529 cpu_to_le64(memmap
[VIRT_HIGH_GIC_REDIST2
].base
);
531 cpu_to_le32(memmap
[VIRT_HIGH_GIC_REDIST2
].size
);
534 if (its_class_name() && !vmc
->no_its
) {
535 gic_its
= acpi_data_push(table_data
, sizeof *gic_its
);
536 gic_its
->type
= ACPI_APIC_GENERIC_TRANSLATOR
;
537 gic_its
->length
= sizeof(*gic_its
);
538 gic_its
->translation_id
= 0;
539 gic_its
->base_address
= cpu_to_le64(memmap
[VIRT_GIC_ITS
].base
);
542 gic_msi
= acpi_data_push(table_data
, sizeof *gic_msi
);
543 gic_msi
->type
= ACPI_APIC_GENERIC_MSI_FRAME
;
544 gic_msi
->length
= sizeof(*gic_msi
);
545 gic_msi
->gic_msi_frame_id
= 0;
546 gic_msi
->base_address
= cpu_to_le64(memmap
[VIRT_GIC_V2M
].base
);
547 gic_msi
->flags
= cpu_to_le32(1);
548 gic_msi
->spi_count
= cpu_to_le16(NUM_GICV2M_SPIS
);
549 gic_msi
->spi_base
= cpu_to_le16(irqmap
[VIRT_GIC_V2M
] + ARM_SPI_BASE
);
552 build_header(linker
, table_data
,
553 (void *)(table_data
->data
+ madt_start
), "APIC",
554 table_data
->len
- madt_start
, 3, NULL
, NULL
);
558 static void build_fadt_rev5(GArray
*table_data
, BIOSLinker
*linker
,
559 VirtMachineState
*vms
, unsigned dsdt_tbl_offset
)
562 AcpiFadtData fadt
= {
565 .flags
= 1 << ACPI_FADT_F_HW_REDUCED_ACPI
,
566 .xdsdt_tbl_offset
= &dsdt_tbl_offset
,
569 switch (vms
->psci_conduit
) {
570 case QEMU_PSCI_CONDUIT_DISABLED
:
571 fadt
.arm_boot_arch
= 0;
573 case QEMU_PSCI_CONDUIT_HVC
:
574 fadt
.arm_boot_arch
= ACPI_FADT_ARM_PSCI_COMPLIANT
|
575 ACPI_FADT_ARM_PSCI_USE_HVC
;
577 case QEMU_PSCI_CONDUIT_SMC
:
578 fadt
.arm_boot_arch
= ACPI_FADT_ARM_PSCI_COMPLIANT
;
581 g_assert_not_reached();
584 build_fadt(table_data
, linker
, &fadt
, NULL
, NULL
);
589 build_dsdt(GArray
*table_data
, BIOSLinker
*linker
, VirtMachineState
*vms
)
591 VirtMachineClass
*vmc
= VIRT_MACHINE_GET_CLASS(vms
);
593 MachineState
*ms
= MACHINE(vms
);
594 const MemMapEntry
*memmap
= vms
->memmap
;
595 const int *irqmap
= vms
->irqmap
;
597 dsdt
= init_aml_allocator();
598 /* Reserve space for header */
599 acpi_data_push(dsdt
->buf
, sizeof(AcpiTableHeader
));
601 /* When booting the VM with UEFI, UEFI takes ownership of the RTC hardware.
602 * While UEFI can use libfdt to disable the RTC device node in the DTB that
603 * it passes to the OS, it cannot modify AML. Therefore, we won't generate
604 * the RTC ACPI device at all when using UEFI.
606 scope
= aml_scope("\\_SB");
607 acpi_dsdt_add_cpus(scope
, vms
);
608 acpi_dsdt_add_uart(scope
, &memmap
[VIRT_UART
],
609 (irqmap
[VIRT_UART
] + ARM_SPI_BASE
));
610 if (vmc
->acpi_expose_flash
) {
611 acpi_dsdt_add_flash(scope
, &memmap
[VIRT_FLASH
]);
613 acpi_dsdt_add_fw_cfg(scope
, &memmap
[VIRT_FW_CFG
]);
614 acpi_dsdt_add_virtio(scope
, &memmap
[VIRT_MMIO
],
615 (irqmap
[VIRT_MMIO
] + ARM_SPI_BASE
), NUM_VIRTIO_TRANSPORTS
);
616 acpi_dsdt_add_pci(scope
, memmap
, (irqmap
[VIRT_PCIE
] + ARM_SPI_BASE
),
617 vms
->highmem
, vms
->highmem_ecam
, vms
);
619 build_ged_aml(scope
, "\\_SB."GED_DEVICE
,
620 HOTPLUG_HANDLER(vms
->acpi_dev
),
621 irqmap
[VIRT_ACPI_GED
] + ARM_SPI_BASE
, AML_SYSTEM_MEMORY
,
622 memmap
[VIRT_ACPI_GED
].base
);
624 acpi_dsdt_add_gpio(scope
, &memmap
[VIRT_GPIO
],
625 (irqmap
[VIRT_GPIO
] + ARM_SPI_BASE
));
629 uint32_t event
= object_property_get_uint(OBJECT(vms
->acpi_dev
),
630 "ged-event", &error_abort
);
632 if (event
& ACPI_GED_MEM_HOTPLUG_EVT
) {
633 build_memory_hotplug_aml(scope
, ms
->ram_slots
, "\\_SB", NULL
,
635 memmap
[VIRT_PCDIMM_ACPI
].base
);
639 acpi_dsdt_add_power_button(scope
);
640 acpi_dsdt_add_tpm(scope
, vms
);
642 aml_append(dsdt
, scope
);
644 /* copy AML table into ACPI tables blob and patch header there */
645 g_array_append_vals(table_data
, dsdt
->buf
->data
, dsdt
->buf
->len
);
646 build_header(linker
, table_data
,
647 (void *)(table_data
->data
+ table_data
->len
- dsdt
->buf
->len
),
648 "DSDT", dsdt
->buf
->len
, 2, NULL
, NULL
);
649 free_aml_allocator();
653 struct AcpiBuildState
{
654 /* Copy of table in RAM (for patching). */
655 MemoryRegion
*table_mr
;
656 MemoryRegion
*rsdp_mr
;
657 MemoryRegion
*linker_mr
;
658 /* Is table patched? */
662 static void acpi_align_size(GArray
*blob
, unsigned align
)
665 * Align size to multiple of given size. This reduces the chance
666 * we need to change size in the future (breaking cross version migration).
668 g_array_set_size(blob
, ROUND_UP(acpi_data_len(blob
), align
));
672 void virt_acpi_build(VirtMachineState
*vms
, AcpiBuildTables
*tables
)
674 VirtMachineClass
*vmc
= VIRT_MACHINE_GET_CLASS(vms
);
675 GArray
*table_offsets
;
677 GArray
*tables_blob
= tables
->table_data
;
678 MachineState
*ms
= MACHINE(vms
);
680 table_offsets
= g_array_new(false, true /* clear */,
683 bios_linker_loader_alloc(tables
->linker
,
684 ACPI_BUILD_TABLE_FILE
, tables_blob
,
685 64, false /* high memory */);
687 /* DSDT is pointed to by FADT */
688 dsdt
= tables_blob
->len
;
689 build_dsdt(tables_blob
, tables
->linker
, vms
);
691 /* FADT MADT GTDT MCFG SPCR pointed to by RSDT */
692 acpi_add_table(table_offsets
, tables_blob
);
693 build_fadt_rev5(tables_blob
, tables
->linker
, vms
, dsdt
);
695 acpi_add_table(table_offsets
, tables_blob
);
696 build_madt(tables_blob
, tables
->linker
, vms
);
698 acpi_add_table(table_offsets
, tables_blob
);
699 build_gtdt(tables_blob
, tables
->linker
, vms
);
701 acpi_add_table(table_offsets
, tables_blob
);
703 AcpiMcfgInfo mcfg
= {
704 .base
= vms
->memmap
[VIRT_ECAM_ID(vms
->highmem_ecam
)].base
,
705 .size
= vms
->memmap
[VIRT_ECAM_ID(vms
->highmem_ecam
)].size
,
707 build_mcfg(tables_blob
, tables
->linker
, &mcfg
);
710 acpi_add_table(table_offsets
, tables_blob
);
711 build_spcr(tables_blob
, tables
->linker
, vms
);
714 build_ghes_error_table(tables
->hardware_errors
, tables
->linker
);
715 acpi_add_table(table_offsets
, tables_blob
);
716 acpi_build_hest(tables_blob
, tables
->linker
);
719 if (ms
->numa_state
->num_nodes
> 0) {
720 acpi_add_table(table_offsets
, tables_blob
);
721 build_srat(tables_blob
, tables
->linker
, vms
);
722 if (ms
->numa_state
->have_numa_distance
) {
723 acpi_add_table(table_offsets
, tables_blob
);
724 build_slit(tables_blob
, tables
->linker
, ms
);
728 if (ms
->nvdimms_state
->is_enabled
) {
729 nvdimm_build_acpi(table_offsets
, tables_blob
, tables
->linker
,
730 ms
->nvdimms_state
, ms
->ram_slots
);
733 if (its_class_name() && !vmc
->no_its
) {
734 acpi_add_table(table_offsets
, tables_blob
);
735 build_iort(tables_blob
, tables
->linker
, vms
);
738 if (tpm_get_version(tpm_find()) == TPM_VERSION_2_0
) {
739 acpi_add_table(table_offsets
, tables_blob
);
740 build_tpm2(tables_blob
, tables
->linker
, tables
->tcpalog
);
743 /* XSDT is pointed to by RSDP */
744 xsdt
= tables_blob
->len
;
745 build_xsdt(tables_blob
, tables
->linker
, table_offsets
, NULL
, NULL
);
747 /* RSDP is in FSEG memory, so allocate it separately */
749 AcpiRsdpData rsdp_data
= {
751 .oem_id
= ACPI_BUILD_APPNAME6
,
752 .xsdt_tbl_offset
= &xsdt
,
753 .rsdt_tbl_offset
= NULL
,
755 build_rsdp(tables
->rsdp
, tables
->linker
, &rsdp_data
);
759 * The align size is 128, warn if 64k is not enough therefore
760 * the align size could be resized.
762 if (tables_blob
->len
> ACPI_BUILD_TABLE_SIZE
/ 2) {
763 warn_report("ACPI table size %u exceeds %d bytes,"
764 " migration may not work",
765 tables_blob
->len
, ACPI_BUILD_TABLE_SIZE
/ 2);
766 error_printf("Try removing CPUs, NUMA nodes, memory slots"
769 acpi_align_size(tables_blob
, ACPI_BUILD_TABLE_SIZE
);
772 /* Cleanup memory that's no longer used. */
773 g_array_free(table_offsets
, true);
776 static void acpi_ram_update(MemoryRegion
*mr
, GArray
*data
)
778 uint32_t size
= acpi_data_len(data
);
780 /* Make sure RAM size is correct - in case it got changed
781 * e.g. by migration */
782 memory_region_ram_resize(mr
, size
, &error_abort
);
784 memcpy(memory_region_get_ram_ptr(mr
), data
->data
, size
);
785 memory_region_set_dirty(mr
, 0, size
);
788 static void virt_acpi_build_update(void *build_opaque
)
790 AcpiBuildState
*build_state
= build_opaque
;
791 AcpiBuildTables tables
;
793 /* No state to update or already patched? Nothing to do. */
794 if (!build_state
|| build_state
->patched
) {
797 build_state
->patched
= true;
799 acpi_build_tables_init(&tables
);
801 virt_acpi_build(VIRT_MACHINE(qdev_get_machine()), &tables
);
803 acpi_ram_update(build_state
->table_mr
, tables
.table_data
);
804 acpi_ram_update(build_state
->rsdp_mr
, tables
.rsdp
);
805 acpi_ram_update(build_state
->linker_mr
, tables
.linker
->cmd_blob
);
807 acpi_build_tables_cleanup(&tables
, true);
810 static void virt_acpi_build_reset(void *build_opaque
)
812 AcpiBuildState
*build_state
= build_opaque
;
813 build_state
->patched
= false;
816 static const VMStateDescription vmstate_virt_acpi_build
= {
817 .name
= "virt_acpi_build",
819 .minimum_version_id
= 1,
820 .fields
= (VMStateField
[]) {
821 VMSTATE_BOOL(patched
, AcpiBuildState
),
822 VMSTATE_END_OF_LIST()
826 void virt_acpi_setup(VirtMachineState
*vms
)
828 AcpiBuildTables tables
;
829 AcpiBuildState
*build_state
;
830 AcpiGedState
*acpi_ged_state
;
833 trace_virt_acpi_setup();
837 if (!virt_is_acpi_enabled(vms
)) {
838 trace_virt_acpi_setup();
842 build_state
= g_malloc0(sizeof *build_state
);
844 acpi_build_tables_init(&tables
);
845 virt_acpi_build(vms
, &tables
);
847 /* Now expose it all to Guest */
848 build_state
->table_mr
= acpi_add_rom_blob(virt_acpi_build_update
,
849 build_state
, tables
.table_data
,
850 ACPI_BUILD_TABLE_FILE
,
851 ACPI_BUILD_TABLE_MAX_SIZE
);
852 assert(build_state
->table_mr
!= NULL
);
854 build_state
->linker_mr
=
855 acpi_add_rom_blob(virt_acpi_build_update
, build_state
,
856 tables
.linker
->cmd_blob
, ACPI_BUILD_LOADER_FILE
, 0);
858 fw_cfg_add_file(vms
->fw_cfg
, ACPI_BUILD_TPMLOG_FILE
, tables
.tcpalog
->data
,
859 acpi_data_len(tables
.tcpalog
));
862 assert(vms
->acpi_dev
);
863 acpi_ged_state
= ACPI_GED(vms
->acpi_dev
);
864 acpi_ghes_add_fw_cfg(&acpi_ged_state
->ghes_state
,
865 vms
->fw_cfg
, tables
.hardware_errors
);
868 build_state
->rsdp_mr
= acpi_add_rom_blob(virt_acpi_build_update
,
869 build_state
, tables
.rsdp
,
870 ACPI_BUILD_RSDP_FILE
, 0);
872 qemu_register_reset(virt_acpi_build_reset
, build_state
);
873 virt_acpi_build_reset(build_state
);
874 vmstate_register(NULL
, 0, &vmstate_virt_acpi_build
, build_state
);
876 /* Cleanup tables but don't free the memory: we track it
879 acpi_build_tables_cleanup(&tables
, false);