]> git.proxmox.com Git - mirror_qemu.git/blame - hw/arm/virt-acpi-build.c
hw/arm/virt-acpi-build: Generate FADT table and update ACPI headers
[mirror_qemu.git] / hw / arm / virt-acpi-build.c
CommitLineData
f5d8c8cd
SZ
1/* Support for generating ACPI tables and passing them to Guests
2 *
3 * ARM virt ACPI generation
4 *
5 * Copyright (C) 2008-2010 Kevin O'Connor <kevin@koconnor.net>
6 * Copyright (C) 2006 Fabrice Bellard
7 * Copyright (C) 2013 Red Hat Inc
8 *
9 * Author: Michael S. Tsirkin <mst@redhat.com>
10 *
11 * Copyright (c) 2015 HUAWEI TECHNOLOGIES CO.,LTD.
12 *
13 * Author: Shannon Zhao <zhaoshenglong@huawei.com>
14 *
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.
19
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.
24
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/>.
27 */
28
29#include "qemu-common.h"
30#include "hw/arm/virt-acpi-build.h"
31#include "qemu/bitmap.h"
32#include "trace.h"
33#include "qom/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/loader.h"
40#include "hw/hw.h"
41#include "hw/acpi/aml-build.h"
42
dfccd8cf
SZ
43#define ARM_SPI_BASE 32
44
45static void acpi_dsdt_add_cpus(Aml *scope, int smp_cpus)
46{
47 uint16_t i;
48
49 for (i = 0; i < smp_cpus; i++) {
50 Aml *dev = aml_device("C%03x", i);
51 aml_append(dev, aml_name_decl("_HID", aml_string("ACPI0007")));
52 aml_append(dev, aml_name_decl("_UID", aml_int(i)));
53 aml_append(scope, dev);
54 }
55}
56
57static void acpi_dsdt_add_uart(Aml *scope, const MemMapEntry *uart_memmap,
58 int uart_irq)
59{
60 Aml *dev = aml_device("COM0");
61 aml_append(dev, aml_name_decl("_HID", aml_string("ARMH0011")));
62 aml_append(dev, aml_name_decl("_UID", aml_int(0)));
63
64 Aml *crs = aml_resource_template();
65 aml_append(crs, aml_memory32_fixed(uart_memmap->base,
66 uart_memmap->size, AML_READ_WRITE));
67 aml_append(crs,
68 aml_interrupt(AML_CONSUMER, AML_LEVEL, AML_ACTIVE_HIGH,
69 AML_EXCLUSIVE, uart_irq));
70 aml_append(dev, aml_name_decl("_CRS", crs));
71 aml_append(scope, dev);
72}
73
74static void acpi_dsdt_add_rtc(Aml *scope, const MemMapEntry *rtc_memmap,
75 int rtc_irq)
76{
77 Aml *dev = aml_device("RTC0");
78 aml_append(dev, aml_name_decl("_HID", aml_string("LNRO0013")));
79 aml_append(dev, aml_name_decl("_UID", aml_int(0)));
80
81 Aml *crs = aml_resource_template();
82 aml_append(crs, aml_memory32_fixed(rtc_memmap->base,
83 rtc_memmap->size, AML_READ_WRITE));
84 aml_append(crs,
85 aml_interrupt(AML_CONSUMER, AML_LEVEL, AML_ACTIVE_HIGH,
86 AML_EXCLUSIVE, rtc_irq));
87 aml_append(dev, aml_name_decl("_CRS", crs));
88 aml_append(scope, dev);
89}
90
91static void acpi_dsdt_add_flash(Aml *scope, const MemMapEntry *flash_memmap)
92{
93 Aml *dev, *crs;
94 hwaddr base = flash_memmap->base;
95 hwaddr size = flash_memmap->size;
96
97 dev = aml_device("FLS0");
98 aml_append(dev, aml_name_decl("_HID", aml_string("LNRO0015")));
99 aml_append(dev, aml_name_decl("_UID", aml_int(0)));
100
101 crs = aml_resource_template();
102 aml_append(crs, aml_memory32_fixed(base, size, AML_READ_WRITE));
103 aml_append(dev, aml_name_decl("_CRS", crs));
104 aml_append(scope, dev);
105
106 dev = aml_device("FLS1");
107 aml_append(dev, aml_name_decl("_HID", aml_string("LNRO0015")));
108 aml_append(dev, aml_name_decl("_UID", aml_int(1)));
109 crs = aml_resource_template();
110 aml_append(crs, aml_memory32_fixed(base + size, size, AML_READ_WRITE));
111 aml_append(dev, aml_name_decl("_CRS", crs));
112 aml_append(scope, dev);
113}
114
115static void acpi_dsdt_add_virtio(Aml *scope,
116 const MemMapEntry *virtio_mmio_memmap,
117 int mmio_irq, int num)
118{
119 hwaddr base = virtio_mmio_memmap->base;
120 hwaddr size = virtio_mmio_memmap->size;
121 int irq = mmio_irq;
122 int i;
123
124 for (i = 0; i < num; i++) {
125 Aml *dev = aml_device("VR%02u", i);
126 aml_append(dev, aml_name_decl("_HID", aml_string("LNRO0005")));
127 aml_append(dev, aml_name_decl("_UID", aml_int(i)));
128
129 Aml *crs = aml_resource_template();
130 aml_append(crs, aml_memory32_fixed(base, size, AML_READ_WRITE));
131 aml_append(crs,
132 aml_interrupt(AML_CONSUMER, AML_LEVEL, AML_ACTIVE_HIGH,
133 AML_EXCLUSIVE, irq + i));
134 aml_append(dev, aml_name_decl("_CRS", crs));
135 aml_append(scope, dev);
136 base += size;
137 }
138}
139
c2f7c0c3
SZ
140/* FADT */
141static void
142build_fadt(GArray *table_data, GArray *linker, unsigned dsdt)
143{
144 AcpiFadtDescriptorRev5_1 *fadt = acpi_data_push(table_data, sizeof(*fadt));
145
146 /* Hardware Reduced = 1 and use PSCI 0.2+ and with HVC */
147 fadt->flags = cpu_to_le32(1 << ACPI_FADT_F_HW_REDUCED_ACPI);
148 fadt->arm_boot_flags = cpu_to_le16((1 << ACPI_FADT_ARM_USE_PSCI_G_0_2) |
149 (1 << ACPI_FADT_ARM_PSCI_USE_HVC));
150
151 /* ACPI v5.1 (fadt->revision.fadt->minor_revision) */
152 fadt->minor_revision = 0x1;
153
154 fadt->dsdt = cpu_to_le32(dsdt);
155 /* DSDT address to be filled by Guest linker */
156 bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
157 ACPI_BUILD_TABLE_FILE,
158 table_data, &fadt->dsdt,
159 sizeof fadt->dsdt);
160
161 build_header(linker, table_data,
162 (void *)fadt, "FACP", sizeof(*fadt), 5);
163}
164
dfccd8cf
SZ
165/* DSDT */
166static void
167build_dsdt(GArray *table_data, GArray *linker, VirtGuestInfo *guest_info)
168{
169 Aml *scope, *dsdt;
170 const MemMapEntry *memmap = guest_info->memmap;
171 const int *irqmap = guest_info->irqmap;
172
173 dsdt = init_aml_allocator();
174 /* Reserve space for header */
175 acpi_data_push(dsdt->buf, sizeof(AcpiTableHeader));
176
177 scope = aml_scope("\\_SB");
178 acpi_dsdt_add_cpus(scope, guest_info->smp_cpus);
179 acpi_dsdt_add_uart(scope, &memmap[VIRT_UART],
180 (irqmap[VIRT_UART] + ARM_SPI_BASE));
181 acpi_dsdt_add_rtc(scope, &memmap[VIRT_RTC],
182 (irqmap[VIRT_RTC] + ARM_SPI_BASE));
183 acpi_dsdt_add_flash(scope, &memmap[VIRT_FLASH]);
184 acpi_dsdt_add_virtio(scope, &memmap[VIRT_MMIO],
185 (irqmap[VIRT_MMIO] + ARM_SPI_BASE), NUM_VIRTIO_TRANSPORTS);
186 aml_append(dsdt, scope);
187
188 /* copy AML table into ACPI tables blob and patch header there */
189 g_array_append_vals(table_data, dsdt->buf->data, dsdt->buf->len);
190 build_header(linker, table_data,
191 (void *)(table_data->data + table_data->len - dsdt->buf->len),
192 "DSDT", dsdt->buf->len, 5);
193 free_aml_allocator();
194}
195
f5d8c8cd
SZ
196typedef
197struct AcpiBuildState {
198 /* Copy of table in RAM (for patching). */
199 MemoryRegion *table_mr;
200 MemoryRegion *rsdp_mr;
201 MemoryRegion *linker_mr;
202 /* Is table patched? */
203 bool patched;
204 VirtGuestInfo *guest_info;
205} AcpiBuildState;
206
207static
208void virt_acpi_build(VirtGuestInfo *guest_info, AcpiBuildTables *tables)
209{
210 GArray *table_offsets;
c2f7c0c3 211 unsigned dsdt;
dfccd8cf 212 GArray *tables_blob = tables->table_data;
f5d8c8cd
SZ
213
214 table_offsets = g_array_new(false, true /* clear */,
215 sizeof(uint32_t));
216
217 bios_linker_loader_alloc(tables->linker, ACPI_BUILD_TABLE_FILE,
218 64, false /* high memory */);
219
220 /*
221 * The ACPI v5.1 tables for Hardware-reduced ACPI platform are:
222 * RSDP
223 * RSDT
224 * FADT
225 * GTDT
226 * MADT
227 * DSDT
228 */
229
dfccd8cf 230 /* DSDT is pointed to by FADT */
c2f7c0c3 231 dsdt = tables_blob->len;
dfccd8cf
SZ
232 build_dsdt(tables_blob, tables->linker, guest_info);
233
c2f7c0c3
SZ
234 /* FADT MADT GTDT pointed to by RSDT */
235 acpi_add_table(table_offsets, tables_blob);
236 build_fadt(tables_blob, tables->linker, dsdt);
237
f5d8c8cd
SZ
238 /* Cleanup memory that's no longer used. */
239 g_array_free(table_offsets, true);
240}
241
242static void acpi_ram_update(MemoryRegion *mr, GArray *data)
243{
244 uint32_t size = acpi_data_len(data);
245
246 /* Make sure RAM size is correct - in case it got changed
247 * e.g. by migration */
248 memory_region_ram_resize(mr, size, &error_abort);
249
250 memcpy(memory_region_get_ram_ptr(mr), data->data, size);
251 memory_region_set_dirty(mr, 0, size);
252}
253
254static void virt_acpi_build_update(void *build_opaque, uint32_t offset)
255{
256 AcpiBuildState *build_state = build_opaque;
257 AcpiBuildTables tables;
258
259 /* No state to update or already patched? Nothing to do. */
260 if (!build_state || build_state->patched) {
261 return;
262 }
263 build_state->patched = true;
264
265 acpi_build_tables_init(&tables);
266
267 virt_acpi_build(build_state->guest_info, &tables);
268
269 acpi_ram_update(build_state->table_mr, tables.table_data);
270 acpi_ram_update(build_state->rsdp_mr, tables.rsdp);
271 acpi_ram_update(build_state->linker_mr, tables.linker);
272
273
274 acpi_build_tables_cleanup(&tables, true);
275}
276
277static void virt_acpi_build_reset(void *build_opaque)
278{
279 AcpiBuildState *build_state = build_opaque;
280 build_state->patched = false;
281}
282
283static MemoryRegion *acpi_add_rom_blob(AcpiBuildState *build_state,
284 GArray *blob, const char *name,
285 uint64_t max_size)
286{
287 return rom_add_blob(name, blob->data, acpi_data_len(blob), max_size, -1,
288 name, virt_acpi_build_update, build_state);
289}
290
291static const VMStateDescription vmstate_virt_acpi_build = {
292 .name = "virt_acpi_build",
293 .version_id = 1,
294 .minimum_version_id = 1,
295 .fields = (VMStateField[]) {
296 VMSTATE_BOOL(patched, AcpiBuildState),
297 VMSTATE_END_OF_LIST()
298 },
299};
300
301void virt_acpi_setup(VirtGuestInfo *guest_info)
302{
303 AcpiBuildTables tables;
304 AcpiBuildState *build_state;
305
306 if (!guest_info->fw_cfg) {
307 trace_virt_acpi_setup();
308 return;
309 }
310
311 if (!acpi_enabled) {
312 trace_virt_acpi_setup();
313 return;
314 }
315
316 build_state = g_malloc0(sizeof *build_state);
317 build_state->guest_info = guest_info;
318
319 acpi_build_tables_init(&tables);
320 virt_acpi_build(build_state->guest_info, &tables);
321
322 /* Now expose it all to Guest */
323 build_state->table_mr = acpi_add_rom_blob(build_state, tables.table_data,
324 ACPI_BUILD_TABLE_FILE,
325 ACPI_BUILD_TABLE_MAX_SIZE);
326 assert(build_state->table_mr != NULL);
327
328 build_state->linker_mr =
329 acpi_add_rom_blob(build_state, tables.linker, "etc/table-loader", 0);
330
331 fw_cfg_add_file(guest_info->fw_cfg, ACPI_BUILD_TPMLOG_FILE,
332 tables.tcpalog->data, acpi_data_len(tables.tcpalog));
333
334 build_state->rsdp_mr = acpi_add_rom_blob(build_state, tables.rsdp,
335 ACPI_BUILD_RSDP_FILE, 0);
336
337 qemu_register_reset(virt_acpi_build_reset, build_state);
338 virt_acpi_build_reset(build_state);
339 vmstate_register(NULL, 0, &vmstate_virt_acpi_build, build_state);
340
341 /* Cleanup tables but don't free the memory: we track it
342 * in build_state.
343 */
344 acpi_build_tables_cleanup(&tables, false);
345}