]> git.proxmox.com Git - mirror_qemu.git/blame - hw/arm/virt-acpi-build.c
hw/arm/virt-acpi-build: Generate RSDP table
[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
982d06c5
SZ
45typedef struct VirtAcpiCpuInfo {
46 DECLARE_BITMAP(found_cpus, VIRT_ACPI_CPU_ID_LIMIT);
47} VirtAcpiCpuInfo;
48
49static void virt_acpi_get_cpu_info(VirtAcpiCpuInfo *cpuinfo)
50{
51 CPUState *cpu;
52
53 memset(cpuinfo->found_cpus, 0, sizeof cpuinfo->found_cpus);
54 CPU_FOREACH(cpu) {
55 set_bit(cpu->cpu_index, cpuinfo->found_cpus);
56 }
57}
58
dfccd8cf
SZ
59static void acpi_dsdt_add_cpus(Aml *scope, int smp_cpus)
60{
61 uint16_t i;
62
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);
68 }
69}
70
71static void acpi_dsdt_add_uart(Aml *scope, const MemMapEntry *uart_memmap,
72 int uart_irq)
73{
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)));
77
78 Aml *crs = aml_resource_template();
79 aml_append(crs, aml_memory32_fixed(uart_memmap->base,
80 uart_memmap->size, AML_READ_WRITE));
81 aml_append(crs,
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);
86}
87
88static void acpi_dsdt_add_rtc(Aml *scope, const MemMapEntry *rtc_memmap,
89 int rtc_irq)
90{
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)));
94
95 Aml *crs = aml_resource_template();
96 aml_append(crs, aml_memory32_fixed(rtc_memmap->base,
97 rtc_memmap->size, AML_READ_WRITE));
98 aml_append(crs,
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);
103}
104
105static void acpi_dsdt_add_flash(Aml *scope, const MemMapEntry *flash_memmap)
106{
107 Aml *dev, *crs;
108 hwaddr base = flash_memmap->base;
109 hwaddr size = flash_memmap->size;
110
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)));
114
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);
119
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);
127}
128
129static void acpi_dsdt_add_virtio(Aml *scope,
130 const MemMapEntry *virtio_mmio_memmap,
131 int mmio_irq, int num)
132{
133 hwaddr base = virtio_mmio_memmap->base;
134 hwaddr size = virtio_mmio_memmap->size;
135 int irq = mmio_irq;
136 int i;
137
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)));
142
143 Aml *crs = aml_resource_template();
144 aml_append(crs, aml_memory32_fixed(base, size, AML_READ_WRITE));
145 aml_append(crs,
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);
150 base += size;
151 }
152}
153
d4bec5d8
SZ
154/* RSDP */
155static GArray *
156build_rsdp(GArray *rsdp_table, GArray *linker, unsigned rsdt)
157{
158 AcpiRsdpDescriptor *rsdp = acpi_data_push(rsdp_table, sizeof *rsdp);
159
160 bios_linker_loader_alloc(linker, ACPI_BUILD_RSDP_FILE, 16,
161 true /* fseg memory */);
162
163 memcpy(&rsdp->signature, "RSD PTR ", sizeof(rsdp->signature));
164 memcpy(rsdp->oem_id, ACPI_BUILD_APPNAME6, sizeof(rsdp->oem_id));
165 rsdp->length = cpu_to_le32(sizeof(*rsdp));
166 rsdp->revision = 0x02;
167
168 /* Point to RSDT */
169 rsdp->rsdt_physical_address = cpu_to_le32(rsdt);
170 /* Address to be filled by Guest linker */
171 bios_linker_loader_add_pointer(linker, ACPI_BUILD_RSDP_FILE,
172 ACPI_BUILD_TABLE_FILE,
173 rsdp_table, &rsdp->rsdt_physical_address,
174 sizeof rsdp->rsdt_physical_address);
175 rsdp->checksum = 0;
176 /* Checksum to be filled by Guest linker */
177 bios_linker_loader_add_checksum(linker, ACPI_BUILD_RSDP_FILE,
178 rsdp, rsdp, sizeof *rsdp, &rsdp->checksum);
179
180 return rsdp_table;
181}
182
ee246400
SZ
183/* GTDT */
184static void
185build_gtdt(GArray *table_data, GArray *linker)
186{
187 int gtdt_start = table_data->len;
188 AcpiGenericTimerTable *gtdt;
189
190 gtdt = acpi_data_push(table_data, sizeof *gtdt);
191 /* The interrupt values are the same with the device tree when adding 16 */
192 gtdt->secure_el1_interrupt = ARCH_TIMER_S_EL1_IRQ + 16;
193 gtdt->secure_el1_flags = ACPI_EDGE_SENSITIVE;
194
195 gtdt->non_secure_el1_interrupt = ARCH_TIMER_NS_EL1_IRQ + 16;
196 gtdt->non_secure_el1_flags = ACPI_EDGE_SENSITIVE;
197
198 gtdt->virtual_timer_interrupt = ARCH_TIMER_VIRT_IRQ + 16;
199 gtdt->virtual_timer_flags = ACPI_EDGE_SENSITIVE;
200
201 gtdt->non_secure_el2_interrupt = ARCH_TIMER_NS_EL2_IRQ + 16;
202 gtdt->non_secure_el2_flags = ACPI_EDGE_SENSITIVE;
203
204 build_header(linker, table_data,
205 (void *)(table_data->data + gtdt_start), "GTDT",
206 table_data->len - gtdt_start, 5);
207}
208
982d06c5
SZ
209/* MADT */
210static void
211build_madt(GArray *table_data, GArray *linker, VirtGuestInfo *guest_info,
212 VirtAcpiCpuInfo *cpuinfo)
213{
214 int madt_start = table_data->len;
215 const MemMapEntry *memmap = guest_info->memmap;
216 AcpiMultipleApicTable *madt;
217 AcpiMadtGenericDistributor *gicd;
218 int i;
219
220 madt = acpi_data_push(table_data, sizeof *madt);
221
222 for (i = 0; i < guest_info->smp_cpus; i++) {
223 AcpiMadtGenericInterrupt *gicc = acpi_data_push(table_data,
224 sizeof *gicc);
225 gicc->type = ACPI_APIC_GENERIC_INTERRUPT;
226 gicc->length = sizeof(*gicc);
227 gicc->base_address = memmap[VIRT_GIC_CPU].base;
228 gicc->cpu_interface_number = i;
229 gicc->arm_mpidr = i;
230 gicc->uid = i;
231 if (test_bit(i, cpuinfo->found_cpus)) {
232 gicc->flags = cpu_to_le32(ACPI_GICC_ENABLED);
233 }
234 }
235
236 gicd = acpi_data_push(table_data, sizeof *gicd);
237 gicd->type = ACPI_APIC_GENERIC_DISTRIBUTOR;
238 gicd->length = sizeof(*gicd);
239 gicd->base_address = memmap[VIRT_GIC_DIST].base;
240
241 build_header(linker, table_data,
242 (void *)(table_data->data + madt_start), "APIC",
243 table_data->len - madt_start, 5);
244}
245
c2f7c0c3
SZ
246/* FADT */
247static void
248build_fadt(GArray *table_data, GArray *linker, unsigned dsdt)
249{
250 AcpiFadtDescriptorRev5_1 *fadt = acpi_data_push(table_data, sizeof(*fadt));
251
252 /* Hardware Reduced = 1 and use PSCI 0.2+ and with HVC */
253 fadt->flags = cpu_to_le32(1 << ACPI_FADT_F_HW_REDUCED_ACPI);
254 fadt->arm_boot_flags = cpu_to_le16((1 << ACPI_FADT_ARM_USE_PSCI_G_0_2) |
255 (1 << ACPI_FADT_ARM_PSCI_USE_HVC));
256
257 /* ACPI v5.1 (fadt->revision.fadt->minor_revision) */
258 fadt->minor_revision = 0x1;
259
260 fadt->dsdt = cpu_to_le32(dsdt);
261 /* DSDT address to be filled by Guest linker */
262 bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
263 ACPI_BUILD_TABLE_FILE,
264 table_data, &fadt->dsdt,
265 sizeof fadt->dsdt);
266
267 build_header(linker, table_data,
268 (void *)fadt, "FACP", sizeof(*fadt), 5);
269}
270
dfccd8cf
SZ
271/* DSDT */
272static void
273build_dsdt(GArray *table_data, GArray *linker, VirtGuestInfo *guest_info)
274{
275 Aml *scope, *dsdt;
276 const MemMapEntry *memmap = guest_info->memmap;
277 const int *irqmap = guest_info->irqmap;
278
279 dsdt = init_aml_allocator();
280 /* Reserve space for header */
281 acpi_data_push(dsdt->buf, sizeof(AcpiTableHeader));
282
283 scope = aml_scope("\\_SB");
284 acpi_dsdt_add_cpus(scope, guest_info->smp_cpus);
285 acpi_dsdt_add_uart(scope, &memmap[VIRT_UART],
286 (irqmap[VIRT_UART] + ARM_SPI_BASE));
287 acpi_dsdt_add_rtc(scope, &memmap[VIRT_RTC],
288 (irqmap[VIRT_RTC] + ARM_SPI_BASE));
289 acpi_dsdt_add_flash(scope, &memmap[VIRT_FLASH]);
290 acpi_dsdt_add_virtio(scope, &memmap[VIRT_MMIO],
291 (irqmap[VIRT_MMIO] + ARM_SPI_BASE), NUM_VIRTIO_TRANSPORTS);
292 aml_append(dsdt, scope);
293
294 /* copy AML table into ACPI tables blob and patch header there */
295 g_array_append_vals(table_data, dsdt->buf->data, dsdt->buf->len);
296 build_header(linker, table_data,
297 (void *)(table_data->data + table_data->len - dsdt->buf->len),
298 "DSDT", dsdt->buf->len, 5);
299 free_aml_allocator();
300}
301
f5d8c8cd
SZ
302typedef
303struct AcpiBuildState {
304 /* Copy of table in RAM (for patching). */
305 MemoryRegion *table_mr;
306 MemoryRegion *rsdp_mr;
307 MemoryRegion *linker_mr;
308 /* Is table patched? */
309 bool patched;
310 VirtGuestInfo *guest_info;
311} AcpiBuildState;
312
313static
314void virt_acpi_build(VirtGuestInfo *guest_info, AcpiBuildTables *tables)
315{
316 GArray *table_offsets;
d4bec5d8 317 unsigned dsdt, rsdt;
982d06c5 318 VirtAcpiCpuInfo cpuinfo;
dfccd8cf 319 GArray *tables_blob = tables->table_data;
f5d8c8cd 320
982d06c5
SZ
321 virt_acpi_get_cpu_info(&cpuinfo);
322
f5d8c8cd
SZ
323 table_offsets = g_array_new(false, true /* clear */,
324 sizeof(uint32_t));
325
326 bios_linker_loader_alloc(tables->linker, ACPI_BUILD_TABLE_FILE,
327 64, false /* high memory */);
328
329 /*
330 * The ACPI v5.1 tables for Hardware-reduced ACPI platform are:
331 * RSDP
332 * RSDT
333 * FADT
334 * GTDT
335 * MADT
336 * DSDT
337 */
338
dfccd8cf 339 /* DSDT is pointed to by FADT */
c2f7c0c3 340 dsdt = tables_blob->len;
dfccd8cf
SZ
341 build_dsdt(tables_blob, tables->linker, guest_info);
342
c2f7c0c3
SZ
343 /* FADT MADT GTDT pointed to by RSDT */
344 acpi_add_table(table_offsets, tables_blob);
345 build_fadt(tables_blob, tables->linker, dsdt);
346
982d06c5
SZ
347 acpi_add_table(table_offsets, tables_blob);
348 build_madt(tables_blob, tables->linker, guest_info, &cpuinfo);
349
ee246400
SZ
350 acpi_add_table(table_offsets, tables_blob);
351 build_gtdt(tables_blob, tables->linker);
352
243bdb79 353 /* RSDT is pointed to by RSDP */
d4bec5d8 354 rsdt = tables_blob->len;
243bdb79
SZ
355 build_rsdt(tables_blob, tables->linker, table_offsets);
356
d4bec5d8
SZ
357 /* RSDP is in FSEG memory, so allocate it separately */
358 build_rsdp(tables->rsdp, tables->linker, rsdt);
359
f5d8c8cd
SZ
360 /* Cleanup memory that's no longer used. */
361 g_array_free(table_offsets, true);
362}
363
364static void acpi_ram_update(MemoryRegion *mr, GArray *data)
365{
366 uint32_t size = acpi_data_len(data);
367
368 /* Make sure RAM size is correct - in case it got changed
369 * e.g. by migration */
370 memory_region_ram_resize(mr, size, &error_abort);
371
372 memcpy(memory_region_get_ram_ptr(mr), data->data, size);
373 memory_region_set_dirty(mr, 0, size);
374}
375
376static void virt_acpi_build_update(void *build_opaque, uint32_t offset)
377{
378 AcpiBuildState *build_state = build_opaque;
379 AcpiBuildTables tables;
380
381 /* No state to update or already patched? Nothing to do. */
382 if (!build_state || build_state->patched) {
383 return;
384 }
385 build_state->patched = true;
386
387 acpi_build_tables_init(&tables);
388
389 virt_acpi_build(build_state->guest_info, &tables);
390
391 acpi_ram_update(build_state->table_mr, tables.table_data);
392 acpi_ram_update(build_state->rsdp_mr, tables.rsdp);
393 acpi_ram_update(build_state->linker_mr, tables.linker);
394
395
396 acpi_build_tables_cleanup(&tables, true);
397}
398
399static void virt_acpi_build_reset(void *build_opaque)
400{
401 AcpiBuildState *build_state = build_opaque;
402 build_state->patched = false;
403}
404
405static MemoryRegion *acpi_add_rom_blob(AcpiBuildState *build_state,
406 GArray *blob, const char *name,
407 uint64_t max_size)
408{
409 return rom_add_blob(name, blob->data, acpi_data_len(blob), max_size, -1,
410 name, virt_acpi_build_update, build_state);
411}
412
413static const VMStateDescription vmstate_virt_acpi_build = {
414 .name = "virt_acpi_build",
415 .version_id = 1,
416 .minimum_version_id = 1,
417 .fields = (VMStateField[]) {
418 VMSTATE_BOOL(patched, AcpiBuildState),
419 VMSTATE_END_OF_LIST()
420 },
421};
422
423void virt_acpi_setup(VirtGuestInfo *guest_info)
424{
425 AcpiBuildTables tables;
426 AcpiBuildState *build_state;
427
428 if (!guest_info->fw_cfg) {
429 trace_virt_acpi_setup();
430 return;
431 }
432
433 if (!acpi_enabled) {
434 trace_virt_acpi_setup();
435 return;
436 }
437
438 build_state = g_malloc0(sizeof *build_state);
439 build_state->guest_info = guest_info;
440
441 acpi_build_tables_init(&tables);
442 virt_acpi_build(build_state->guest_info, &tables);
443
444 /* Now expose it all to Guest */
445 build_state->table_mr = acpi_add_rom_blob(build_state, tables.table_data,
446 ACPI_BUILD_TABLE_FILE,
447 ACPI_BUILD_TABLE_MAX_SIZE);
448 assert(build_state->table_mr != NULL);
449
450 build_state->linker_mr =
451 acpi_add_rom_blob(build_state, tables.linker, "etc/table-loader", 0);
452
453 fw_cfg_add_file(guest_info->fw_cfg, ACPI_BUILD_TPMLOG_FILE,
454 tables.tcpalog->data, acpi_data_len(tables.tcpalog));
455
456 build_state->rsdp_mr = acpi_add_rom_blob(build_state, tables.rsdp,
457 ACPI_BUILD_RSDP_FILE, 0);
458
459 qemu_register_reset(virt_acpi_build_reset, build_state);
460 virt_acpi_build_reset(build_state);
461 vmstate_register(NULL, 0, &vmstate_virt_acpi_build, build_state);
462
463 /* Cleanup tables but don't free the memory: we track it
464 * in build_state.
465 */
466 acpi_build_tables_cleanup(&tables, false);
467}