]> git.proxmox.com Git - mirror_qemu.git/blame - hw/arm/virt-acpi-build.c
hw/arm/virt-acpi-build: Generate GTDT 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
ee246400
SZ
154/* GTDT */
155static void
156build_gtdt(GArray *table_data, GArray *linker)
157{
158 int gtdt_start = table_data->len;
159 AcpiGenericTimerTable *gtdt;
160
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;
165
166 gtdt->non_secure_el1_interrupt = ARCH_TIMER_NS_EL1_IRQ + 16;
167 gtdt->non_secure_el1_flags = ACPI_EDGE_SENSITIVE;
168
169 gtdt->virtual_timer_interrupt = ARCH_TIMER_VIRT_IRQ + 16;
170 gtdt->virtual_timer_flags = ACPI_EDGE_SENSITIVE;
171
172 gtdt->non_secure_el2_interrupt = ARCH_TIMER_NS_EL2_IRQ + 16;
173 gtdt->non_secure_el2_flags = ACPI_EDGE_SENSITIVE;
174
175 build_header(linker, table_data,
176 (void *)(table_data->data + gtdt_start), "GTDT",
177 table_data->len - gtdt_start, 5);
178}
179
982d06c5
SZ
180/* MADT */
181static void
182build_madt(GArray *table_data, GArray *linker, VirtGuestInfo *guest_info,
183 VirtAcpiCpuInfo *cpuinfo)
184{
185 int madt_start = table_data->len;
186 const MemMapEntry *memmap = guest_info->memmap;
187 AcpiMultipleApicTable *madt;
188 AcpiMadtGenericDistributor *gicd;
189 int i;
190
191 madt = acpi_data_push(table_data, sizeof *madt);
192
193 for (i = 0; i < guest_info->smp_cpus; i++) {
194 AcpiMadtGenericInterrupt *gicc = acpi_data_push(table_data,
195 sizeof *gicc);
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;
200 gicc->arm_mpidr = i;
201 gicc->uid = i;
202 if (test_bit(i, cpuinfo->found_cpus)) {
203 gicc->flags = cpu_to_le32(ACPI_GICC_ENABLED);
204 }
205 }
206
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;
211
212 build_header(linker, table_data,
213 (void *)(table_data->data + madt_start), "APIC",
214 table_data->len - madt_start, 5);
215}
216
c2f7c0c3
SZ
217/* FADT */
218static void
219build_fadt(GArray *table_data, GArray *linker, unsigned dsdt)
220{
221 AcpiFadtDescriptorRev5_1 *fadt = acpi_data_push(table_data, sizeof(*fadt));
222
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));
227
228 /* ACPI v5.1 (fadt->revision.fadt->minor_revision) */
229 fadt->minor_revision = 0x1;
230
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,
236 sizeof fadt->dsdt);
237
238 build_header(linker, table_data,
239 (void *)fadt, "FACP", sizeof(*fadt), 5);
240}
241
dfccd8cf
SZ
242/* DSDT */
243static void
244build_dsdt(GArray *table_data, GArray *linker, VirtGuestInfo *guest_info)
245{
246 Aml *scope, *dsdt;
247 const MemMapEntry *memmap = guest_info->memmap;
248 const int *irqmap = guest_info->irqmap;
249
250 dsdt = init_aml_allocator();
251 /* Reserve space for header */
252 acpi_data_push(dsdt->buf, sizeof(AcpiTableHeader));
253
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);
264
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();
271}
272
f5d8c8cd
SZ
273typedef
274struct 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? */
280 bool patched;
281 VirtGuestInfo *guest_info;
282} AcpiBuildState;
283
284static
285void virt_acpi_build(VirtGuestInfo *guest_info, AcpiBuildTables *tables)
286{
287 GArray *table_offsets;
c2f7c0c3 288 unsigned dsdt;
982d06c5 289 VirtAcpiCpuInfo cpuinfo;
dfccd8cf 290 GArray *tables_blob = tables->table_data;
f5d8c8cd 291
982d06c5
SZ
292 virt_acpi_get_cpu_info(&cpuinfo);
293
f5d8c8cd
SZ
294 table_offsets = g_array_new(false, true /* clear */,
295 sizeof(uint32_t));
296
297 bios_linker_loader_alloc(tables->linker, ACPI_BUILD_TABLE_FILE,
298 64, false /* high memory */);
299
300 /*
301 * The ACPI v5.1 tables for Hardware-reduced ACPI platform are:
302 * RSDP
303 * RSDT
304 * FADT
305 * GTDT
306 * MADT
307 * DSDT
308 */
309
dfccd8cf 310 /* DSDT is pointed to by FADT */
c2f7c0c3 311 dsdt = tables_blob->len;
dfccd8cf
SZ
312 build_dsdt(tables_blob, tables->linker, guest_info);
313
c2f7c0c3
SZ
314 /* FADT MADT GTDT pointed to by RSDT */
315 acpi_add_table(table_offsets, tables_blob);
316 build_fadt(tables_blob, tables->linker, dsdt);
317
982d06c5
SZ
318 acpi_add_table(table_offsets, tables_blob);
319 build_madt(tables_blob, tables->linker, guest_info, &cpuinfo);
320
ee246400
SZ
321 acpi_add_table(table_offsets, tables_blob);
322 build_gtdt(tables_blob, tables->linker);
323
f5d8c8cd
SZ
324 /* Cleanup memory that's no longer used. */
325 g_array_free(table_offsets, true);
326}
327
328static void acpi_ram_update(MemoryRegion *mr, GArray *data)
329{
330 uint32_t size = acpi_data_len(data);
331
332 /* Make sure RAM size is correct - in case it got changed
333 * e.g. by migration */
334 memory_region_ram_resize(mr, size, &error_abort);
335
336 memcpy(memory_region_get_ram_ptr(mr), data->data, size);
337 memory_region_set_dirty(mr, 0, size);
338}
339
340static void virt_acpi_build_update(void *build_opaque, uint32_t offset)
341{
342 AcpiBuildState *build_state = build_opaque;
343 AcpiBuildTables tables;
344
345 /* No state to update or already patched? Nothing to do. */
346 if (!build_state || build_state->patched) {
347 return;
348 }
349 build_state->patched = true;
350
351 acpi_build_tables_init(&tables);
352
353 virt_acpi_build(build_state->guest_info, &tables);
354
355 acpi_ram_update(build_state->table_mr, tables.table_data);
356 acpi_ram_update(build_state->rsdp_mr, tables.rsdp);
357 acpi_ram_update(build_state->linker_mr, tables.linker);
358
359
360 acpi_build_tables_cleanup(&tables, true);
361}
362
363static void virt_acpi_build_reset(void *build_opaque)
364{
365 AcpiBuildState *build_state = build_opaque;
366 build_state->patched = false;
367}
368
369static MemoryRegion *acpi_add_rom_blob(AcpiBuildState *build_state,
370 GArray *blob, const char *name,
371 uint64_t max_size)
372{
373 return rom_add_blob(name, blob->data, acpi_data_len(blob), max_size, -1,
374 name, virt_acpi_build_update, build_state);
375}
376
377static const VMStateDescription vmstate_virt_acpi_build = {
378 .name = "virt_acpi_build",
379 .version_id = 1,
380 .minimum_version_id = 1,
381 .fields = (VMStateField[]) {
382 VMSTATE_BOOL(patched, AcpiBuildState),
383 VMSTATE_END_OF_LIST()
384 },
385};
386
387void virt_acpi_setup(VirtGuestInfo *guest_info)
388{
389 AcpiBuildTables tables;
390 AcpiBuildState *build_state;
391
392 if (!guest_info->fw_cfg) {
393 trace_virt_acpi_setup();
394 return;
395 }
396
397 if (!acpi_enabled) {
398 trace_virt_acpi_setup();
399 return;
400 }
401
402 build_state = g_malloc0(sizeof *build_state);
403 build_state->guest_info = guest_info;
404
405 acpi_build_tables_init(&tables);
406 virt_acpi_build(build_state->guest_info, &tables);
407
408 /* Now expose it all to Guest */
409 build_state->table_mr = acpi_add_rom_blob(build_state, tables.table_data,
410 ACPI_BUILD_TABLE_FILE,
411 ACPI_BUILD_TABLE_MAX_SIZE);
412 assert(build_state->table_mr != NULL);
413
414 build_state->linker_mr =
415 acpi_add_rom_blob(build_state, tables.linker, "etc/table-loader", 0);
416
417 fw_cfg_add_file(guest_info->fw_cfg, ACPI_BUILD_TPMLOG_FILE,
418 tables.tcpalog->data, acpi_data_len(tables.tcpalog));
419
420 build_state->rsdp_mr = acpi_add_rom_blob(build_state, tables.rsdp,
421 ACPI_BUILD_RSDP_FILE, 0);
422
423 qemu_register_reset(virt_acpi_build_reset, build_state);
424 virt_acpi_build_reset(build_state);
425 vmstate_register(NULL, 0, &vmstate_virt_acpi_build, build_state);
426
427 /* Cleanup tables but don't free the memory: we track it
428 * in build_state.
429 */
430 acpi_build_tables_cleanup(&tables, false);
431}