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-common.h"
30 #include "hw/arm/virt-acpi-build.h"
31 #include "qemu/bitmap.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/loader.h"
41 #include "hw/acpi/aml-build.h"
43 #define ARM_SPI_BASE 32
45 typedef struct VirtAcpiCpuInfo
{
46 DECLARE_BITMAP(found_cpus
, VIRT_ACPI_CPU_ID_LIMIT
);
49 static void virt_acpi_get_cpu_info(VirtAcpiCpuInfo
*cpuinfo
)
53 memset(cpuinfo
->found_cpus
, 0, sizeof cpuinfo
->found_cpus
);
55 set_bit(cpu
->cpu_index
, cpuinfo
->found_cpus
);
59 static void acpi_dsdt_add_cpus(Aml
*scope
, int smp_cpus
)
63 for (i
= 0; i
< smp_cpus
; i
++) {
64 Aml
*dev
= aml_device("C%03x", i
);
65 aml_append(dev
, aml_name_decl("_HID", aml_string("ACPI0007")));
66 aml_append(dev
, aml_name_decl("_UID", aml_int(i
)));
67 aml_append(scope
, dev
);
71 static void acpi_dsdt_add_uart(Aml
*scope
, const MemMapEntry
*uart_memmap
,
74 Aml
*dev
= aml_device("COM0");
75 aml_append(dev
, aml_name_decl("_HID", aml_string("ARMH0011")));
76 aml_append(dev
, aml_name_decl("_UID", aml_int(0)));
78 Aml
*crs
= aml_resource_template();
79 aml_append(crs
, aml_memory32_fixed(uart_memmap
->base
,
80 uart_memmap
->size
, AML_READ_WRITE
));
82 aml_interrupt(AML_CONSUMER
, AML_LEVEL
, AML_ACTIVE_HIGH
,
83 AML_EXCLUSIVE
, uart_irq
));
84 aml_append(dev
, aml_name_decl("_CRS", crs
));
85 aml_append(scope
, dev
);
88 static void acpi_dsdt_add_rtc(Aml
*scope
, const MemMapEntry
*rtc_memmap
,
91 Aml
*dev
= aml_device("RTC0");
92 aml_append(dev
, aml_name_decl("_HID", aml_string("LNRO0013")));
93 aml_append(dev
, aml_name_decl("_UID", aml_int(0)));
95 Aml
*crs
= aml_resource_template();
96 aml_append(crs
, aml_memory32_fixed(rtc_memmap
->base
,
97 rtc_memmap
->size
, AML_READ_WRITE
));
99 aml_interrupt(AML_CONSUMER
, AML_LEVEL
, AML_ACTIVE_HIGH
,
100 AML_EXCLUSIVE
, rtc_irq
));
101 aml_append(dev
, aml_name_decl("_CRS", crs
));
102 aml_append(scope
, dev
);
105 static void acpi_dsdt_add_flash(Aml
*scope
, const MemMapEntry
*flash_memmap
)
108 hwaddr base
= flash_memmap
->base
;
109 hwaddr size
= flash_memmap
->size
;
111 dev
= aml_device("FLS0");
112 aml_append(dev
, aml_name_decl("_HID", aml_string("LNRO0015")));
113 aml_append(dev
, aml_name_decl("_UID", aml_int(0)));
115 crs
= aml_resource_template();
116 aml_append(crs
, aml_memory32_fixed(base
, size
, AML_READ_WRITE
));
117 aml_append(dev
, aml_name_decl("_CRS", crs
));
118 aml_append(scope
, dev
);
120 dev
= aml_device("FLS1");
121 aml_append(dev
, aml_name_decl("_HID", aml_string("LNRO0015")));
122 aml_append(dev
, aml_name_decl("_UID", aml_int(1)));
123 crs
= aml_resource_template();
124 aml_append(crs
, aml_memory32_fixed(base
+ size
, size
, AML_READ_WRITE
));
125 aml_append(dev
, aml_name_decl("_CRS", crs
));
126 aml_append(scope
, dev
);
129 static void acpi_dsdt_add_virtio(Aml
*scope
,
130 const MemMapEntry
*virtio_mmio_memmap
,
131 int mmio_irq
, int num
)
133 hwaddr base
= virtio_mmio_memmap
->base
;
134 hwaddr size
= virtio_mmio_memmap
->size
;
138 for (i
= 0; i
< num
; i
++) {
139 Aml
*dev
= aml_device("VR%02u", i
);
140 aml_append(dev
, aml_name_decl("_HID", aml_string("LNRO0005")));
141 aml_append(dev
, aml_name_decl("_UID", aml_int(i
)));
143 Aml
*crs
= aml_resource_template();
144 aml_append(crs
, aml_memory32_fixed(base
, size
, AML_READ_WRITE
));
146 aml_interrupt(AML_CONSUMER
, AML_LEVEL
, AML_ACTIVE_HIGH
,
147 AML_EXCLUSIVE
, irq
+ i
));
148 aml_append(dev
, aml_name_decl("_CRS", crs
));
149 aml_append(scope
, dev
);
156 build_gtdt(GArray
*table_data
, GArray
*linker
)
158 int gtdt_start
= table_data
->len
;
159 AcpiGenericTimerTable
*gtdt
;
161 gtdt
= acpi_data_push(table_data
, sizeof *gtdt
);
162 /* The interrupt values are the same with the device tree when adding 16 */
163 gtdt
->secure_el1_interrupt
= ARCH_TIMER_S_EL1_IRQ
+ 16;
164 gtdt
->secure_el1_flags
= ACPI_EDGE_SENSITIVE
;
166 gtdt
->non_secure_el1_interrupt
= ARCH_TIMER_NS_EL1_IRQ
+ 16;
167 gtdt
->non_secure_el1_flags
= ACPI_EDGE_SENSITIVE
;
169 gtdt
->virtual_timer_interrupt
= ARCH_TIMER_VIRT_IRQ
+ 16;
170 gtdt
->virtual_timer_flags
= ACPI_EDGE_SENSITIVE
;
172 gtdt
->non_secure_el2_interrupt
= ARCH_TIMER_NS_EL2_IRQ
+ 16;
173 gtdt
->non_secure_el2_flags
= ACPI_EDGE_SENSITIVE
;
175 build_header(linker
, table_data
,
176 (void *)(table_data
->data
+ gtdt_start
), "GTDT",
177 table_data
->len
- gtdt_start
, 5);
182 build_madt(GArray
*table_data
, GArray
*linker
, VirtGuestInfo
*guest_info
,
183 VirtAcpiCpuInfo
*cpuinfo
)
185 int madt_start
= table_data
->len
;
186 const MemMapEntry
*memmap
= guest_info
->memmap
;
187 AcpiMultipleApicTable
*madt
;
188 AcpiMadtGenericDistributor
*gicd
;
191 madt
= acpi_data_push(table_data
, sizeof *madt
);
193 for (i
= 0; i
< guest_info
->smp_cpus
; i
++) {
194 AcpiMadtGenericInterrupt
*gicc
= acpi_data_push(table_data
,
196 gicc
->type
= ACPI_APIC_GENERIC_INTERRUPT
;
197 gicc
->length
= sizeof(*gicc
);
198 gicc
->base_address
= memmap
[VIRT_GIC_CPU
].base
;
199 gicc
->cpu_interface_number
= i
;
202 if (test_bit(i
, cpuinfo
->found_cpus
)) {
203 gicc
->flags
= cpu_to_le32(ACPI_GICC_ENABLED
);
207 gicd
= acpi_data_push(table_data
, sizeof *gicd
);
208 gicd
->type
= ACPI_APIC_GENERIC_DISTRIBUTOR
;
209 gicd
->length
= sizeof(*gicd
);
210 gicd
->base_address
= memmap
[VIRT_GIC_DIST
].base
;
212 build_header(linker
, table_data
,
213 (void *)(table_data
->data
+ madt_start
), "APIC",
214 table_data
->len
- madt_start
, 5);
219 build_fadt(GArray
*table_data
, GArray
*linker
, unsigned dsdt
)
221 AcpiFadtDescriptorRev5_1
*fadt
= acpi_data_push(table_data
, sizeof(*fadt
));
223 /* Hardware Reduced = 1 and use PSCI 0.2+ and with HVC */
224 fadt
->flags
= cpu_to_le32(1 << ACPI_FADT_F_HW_REDUCED_ACPI
);
225 fadt
->arm_boot_flags
= cpu_to_le16((1 << ACPI_FADT_ARM_USE_PSCI_G_0_2
) |
226 (1 << ACPI_FADT_ARM_PSCI_USE_HVC
));
228 /* ACPI v5.1 (fadt->revision.fadt->minor_revision) */
229 fadt
->minor_revision
= 0x1;
231 fadt
->dsdt
= cpu_to_le32(dsdt
);
232 /* DSDT address to be filled by Guest linker */
233 bios_linker_loader_add_pointer(linker
, ACPI_BUILD_TABLE_FILE
,
234 ACPI_BUILD_TABLE_FILE
,
235 table_data
, &fadt
->dsdt
,
238 build_header(linker
, table_data
,
239 (void *)fadt
, "FACP", sizeof(*fadt
), 5);
244 build_dsdt(GArray
*table_data
, GArray
*linker
, VirtGuestInfo
*guest_info
)
247 const MemMapEntry
*memmap
= guest_info
->memmap
;
248 const int *irqmap
= guest_info
->irqmap
;
250 dsdt
= init_aml_allocator();
251 /* Reserve space for header */
252 acpi_data_push(dsdt
->buf
, sizeof(AcpiTableHeader
));
254 scope
= aml_scope("\\_SB");
255 acpi_dsdt_add_cpus(scope
, guest_info
->smp_cpus
);
256 acpi_dsdt_add_uart(scope
, &memmap
[VIRT_UART
],
257 (irqmap
[VIRT_UART
] + ARM_SPI_BASE
));
258 acpi_dsdt_add_rtc(scope
, &memmap
[VIRT_RTC
],
259 (irqmap
[VIRT_RTC
] + ARM_SPI_BASE
));
260 acpi_dsdt_add_flash(scope
, &memmap
[VIRT_FLASH
]);
261 acpi_dsdt_add_virtio(scope
, &memmap
[VIRT_MMIO
],
262 (irqmap
[VIRT_MMIO
] + ARM_SPI_BASE
), NUM_VIRTIO_TRANSPORTS
);
263 aml_append(dsdt
, scope
);
265 /* copy AML table into ACPI tables blob and patch header there */
266 g_array_append_vals(table_data
, dsdt
->buf
->data
, dsdt
->buf
->len
);
267 build_header(linker
, table_data
,
268 (void *)(table_data
->data
+ table_data
->len
- dsdt
->buf
->len
),
269 "DSDT", dsdt
->buf
->len
, 5);
270 free_aml_allocator();
274 struct AcpiBuildState
{
275 /* Copy of table in RAM (for patching). */
276 MemoryRegion
*table_mr
;
277 MemoryRegion
*rsdp_mr
;
278 MemoryRegion
*linker_mr
;
279 /* Is table patched? */
281 VirtGuestInfo
*guest_info
;
285 void virt_acpi_build(VirtGuestInfo
*guest_info
, AcpiBuildTables
*tables
)
287 GArray
*table_offsets
;
289 VirtAcpiCpuInfo cpuinfo
;
290 GArray
*tables_blob
= tables
->table_data
;
292 virt_acpi_get_cpu_info(&cpuinfo
);
294 table_offsets
= g_array_new(false, true /* clear */,
297 bios_linker_loader_alloc(tables
->linker
, ACPI_BUILD_TABLE_FILE
,
298 64, false /* high memory */);
301 * The ACPI v5.1 tables for Hardware-reduced ACPI platform are:
310 /* DSDT is pointed to by FADT */
311 dsdt
= tables_blob
->len
;
312 build_dsdt(tables_blob
, tables
->linker
, guest_info
);
314 /* FADT MADT GTDT pointed to by RSDT */
315 acpi_add_table(table_offsets
, tables_blob
);
316 build_fadt(tables_blob
, tables
->linker
, dsdt
);
318 acpi_add_table(table_offsets
, tables_blob
);
319 build_madt(tables_blob
, tables
->linker
, guest_info
, &cpuinfo
);
321 acpi_add_table(table_offsets
, tables_blob
);
322 build_gtdt(tables_blob
, tables
->linker
);
324 /* RSDT is pointed to by RSDP */
325 build_rsdt(tables_blob
, tables
->linker
, table_offsets
);
327 /* Cleanup memory that's no longer used. */
328 g_array_free(table_offsets
, true);
331 static void acpi_ram_update(MemoryRegion
*mr
, GArray
*data
)
333 uint32_t size
= acpi_data_len(data
);
335 /* Make sure RAM size is correct - in case it got changed
336 * e.g. by migration */
337 memory_region_ram_resize(mr
, size
, &error_abort
);
339 memcpy(memory_region_get_ram_ptr(mr
), data
->data
, size
);
340 memory_region_set_dirty(mr
, 0, size
);
343 static void virt_acpi_build_update(void *build_opaque
, uint32_t offset
)
345 AcpiBuildState
*build_state
= build_opaque
;
346 AcpiBuildTables tables
;
348 /* No state to update or already patched? Nothing to do. */
349 if (!build_state
|| build_state
->patched
) {
352 build_state
->patched
= true;
354 acpi_build_tables_init(&tables
);
356 virt_acpi_build(build_state
->guest_info
, &tables
);
358 acpi_ram_update(build_state
->table_mr
, tables
.table_data
);
359 acpi_ram_update(build_state
->rsdp_mr
, tables
.rsdp
);
360 acpi_ram_update(build_state
->linker_mr
, tables
.linker
);
363 acpi_build_tables_cleanup(&tables
, true);
366 static void virt_acpi_build_reset(void *build_opaque
)
368 AcpiBuildState
*build_state
= build_opaque
;
369 build_state
->patched
= false;
372 static MemoryRegion
*acpi_add_rom_blob(AcpiBuildState
*build_state
,
373 GArray
*blob
, const char *name
,
376 return rom_add_blob(name
, blob
->data
, acpi_data_len(blob
), max_size
, -1,
377 name
, virt_acpi_build_update
, build_state
);
380 static const VMStateDescription vmstate_virt_acpi_build
= {
381 .name
= "virt_acpi_build",
383 .minimum_version_id
= 1,
384 .fields
= (VMStateField
[]) {
385 VMSTATE_BOOL(patched
, AcpiBuildState
),
386 VMSTATE_END_OF_LIST()
390 void virt_acpi_setup(VirtGuestInfo
*guest_info
)
392 AcpiBuildTables tables
;
393 AcpiBuildState
*build_state
;
395 if (!guest_info
->fw_cfg
) {
396 trace_virt_acpi_setup();
401 trace_virt_acpi_setup();
405 build_state
= g_malloc0(sizeof *build_state
);
406 build_state
->guest_info
= guest_info
;
408 acpi_build_tables_init(&tables
);
409 virt_acpi_build(build_state
->guest_info
, &tables
);
411 /* Now expose it all to Guest */
412 build_state
->table_mr
= acpi_add_rom_blob(build_state
, tables
.table_data
,
413 ACPI_BUILD_TABLE_FILE
,
414 ACPI_BUILD_TABLE_MAX_SIZE
);
415 assert(build_state
->table_mr
!= NULL
);
417 build_state
->linker_mr
=
418 acpi_add_rom_blob(build_state
, tables
.linker
, "etc/table-loader", 0);
420 fw_cfg_add_file(guest_info
->fw_cfg
, ACPI_BUILD_TPMLOG_FILE
,
421 tables
.tcpalog
->data
, acpi_data_len(tables
.tcpalog
));
423 build_state
->rsdp_mr
= acpi_add_rom_blob(build_state
, tables
.rsdp
,
424 ACPI_BUILD_RSDP_FILE
, 0);
426 qemu_register_reset(virt_acpi_build_reset
, build_state
);
427 virt_acpi_build_reset(build_state
);
428 vmstate_register(NULL
, 0, &vmstate_virt_acpi_build
, build_state
);
430 /* Cleanup tables but don't free the memory: we track it
433 acpi_build_tables_cleanup(&tables
, false);