]> git.proxmox.com Git - mirror_qemu.git/blame - hw/arm/virt-acpi-build.c
hw/arm/virt-acpi-build: Generate MADT 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
982d06c5
SZ
154/* MADT */
155static void
156build_madt(GArray *table_data, GArray *linker, VirtGuestInfo *guest_info,
157 VirtAcpiCpuInfo *cpuinfo)
158{
159 int madt_start = table_data->len;
160 const MemMapEntry *memmap = guest_info->memmap;
161 AcpiMultipleApicTable *madt;
162 AcpiMadtGenericDistributor *gicd;
163 int i;
164
165 madt = acpi_data_push(table_data, sizeof *madt);
166
167 for (i = 0; i < guest_info->smp_cpus; i++) {
168 AcpiMadtGenericInterrupt *gicc = acpi_data_push(table_data,
169 sizeof *gicc);
170 gicc->type = ACPI_APIC_GENERIC_INTERRUPT;
171 gicc->length = sizeof(*gicc);
172 gicc->base_address = memmap[VIRT_GIC_CPU].base;
173 gicc->cpu_interface_number = i;
174 gicc->arm_mpidr = i;
175 gicc->uid = i;
176 if (test_bit(i, cpuinfo->found_cpus)) {
177 gicc->flags = cpu_to_le32(ACPI_GICC_ENABLED);
178 }
179 }
180
181 gicd = acpi_data_push(table_data, sizeof *gicd);
182 gicd->type = ACPI_APIC_GENERIC_DISTRIBUTOR;
183 gicd->length = sizeof(*gicd);
184 gicd->base_address = memmap[VIRT_GIC_DIST].base;
185
186 build_header(linker, table_data,
187 (void *)(table_data->data + madt_start), "APIC",
188 table_data->len - madt_start, 5);
189}
190
c2f7c0c3
SZ
191/* FADT */
192static void
193build_fadt(GArray *table_data, GArray *linker, unsigned dsdt)
194{
195 AcpiFadtDescriptorRev5_1 *fadt = acpi_data_push(table_data, sizeof(*fadt));
196
197 /* Hardware Reduced = 1 and use PSCI 0.2+ and with HVC */
198 fadt->flags = cpu_to_le32(1 << ACPI_FADT_F_HW_REDUCED_ACPI);
199 fadt->arm_boot_flags = cpu_to_le16((1 << ACPI_FADT_ARM_USE_PSCI_G_0_2) |
200 (1 << ACPI_FADT_ARM_PSCI_USE_HVC));
201
202 /* ACPI v5.1 (fadt->revision.fadt->minor_revision) */
203 fadt->minor_revision = 0x1;
204
205 fadt->dsdt = cpu_to_le32(dsdt);
206 /* DSDT address to be filled by Guest linker */
207 bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
208 ACPI_BUILD_TABLE_FILE,
209 table_data, &fadt->dsdt,
210 sizeof fadt->dsdt);
211
212 build_header(linker, table_data,
213 (void *)fadt, "FACP", sizeof(*fadt), 5);
214}
215
dfccd8cf
SZ
216/* DSDT */
217static void
218build_dsdt(GArray *table_data, GArray *linker, VirtGuestInfo *guest_info)
219{
220 Aml *scope, *dsdt;
221 const MemMapEntry *memmap = guest_info->memmap;
222 const int *irqmap = guest_info->irqmap;
223
224 dsdt = init_aml_allocator();
225 /* Reserve space for header */
226 acpi_data_push(dsdt->buf, sizeof(AcpiTableHeader));
227
228 scope = aml_scope("\\_SB");
229 acpi_dsdt_add_cpus(scope, guest_info->smp_cpus);
230 acpi_dsdt_add_uart(scope, &memmap[VIRT_UART],
231 (irqmap[VIRT_UART] + ARM_SPI_BASE));
232 acpi_dsdt_add_rtc(scope, &memmap[VIRT_RTC],
233 (irqmap[VIRT_RTC] + ARM_SPI_BASE));
234 acpi_dsdt_add_flash(scope, &memmap[VIRT_FLASH]);
235 acpi_dsdt_add_virtio(scope, &memmap[VIRT_MMIO],
236 (irqmap[VIRT_MMIO] + ARM_SPI_BASE), NUM_VIRTIO_TRANSPORTS);
237 aml_append(dsdt, scope);
238
239 /* copy AML table into ACPI tables blob and patch header there */
240 g_array_append_vals(table_data, dsdt->buf->data, dsdt->buf->len);
241 build_header(linker, table_data,
242 (void *)(table_data->data + table_data->len - dsdt->buf->len),
243 "DSDT", dsdt->buf->len, 5);
244 free_aml_allocator();
245}
246
f5d8c8cd
SZ
247typedef
248struct AcpiBuildState {
249 /* Copy of table in RAM (for patching). */
250 MemoryRegion *table_mr;
251 MemoryRegion *rsdp_mr;
252 MemoryRegion *linker_mr;
253 /* Is table patched? */
254 bool patched;
255 VirtGuestInfo *guest_info;
256} AcpiBuildState;
257
258static
259void virt_acpi_build(VirtGuestInfo *guest_info, AcpiBuildTables *tables)
260{
261 GArray *table_offsets;
c2f7c0c3 262 unsigned dsdt;
982d06c5 263 VirtAcpiCpuInfo cpuinfo;
dfccd8cf 264 GArray *tables_blob = tables->table_data;
f5d8c8cd 265
982d06c5
SZ
266 virt_acpi_get_cpu_info(&cpuinfo);
267
f5d8c8cd
SZ
268 table_offsets = g_array_new(false, true /* clear */,
269 sizeof(uint32_t));
270
271 bios_linker_loader_alloc(tables->linker, ACPI_BUILD_TABLE_FILE,
272 64, false /* high memory */);
273
274 /*
275 * The ACPI v5.1 tables for Hardware-reduced ACPI platform are:
276 * RSDP
277 * RSDT
278 * FADT
279 * GTDT
280 * MADT
281 * DSDT
282 */
283
dfccd8cf 284 /* DSDT is pointed to by FADT */
c2f7c0c3 285 dsdt = tables_blob->len;
dfccd8cf
SZ
286 build_dsdt(tables_blob, tables->linker, guest_info);
287
c2f7c0c3
SZ
288 /* FADT MADT GTDT pointed to by RSDT */
289 acpi_add_table(table_offsets, tables_blob);
290 build_fadt(tables_blob, tables->linker, dsdt);
291
982d06c5
SZ
292 acpi_add_table(table_offsets, tables_blob);
293 build_madt(tables_blob, tables->linker, guest_info, &cpuinfo);
294
f5d8c8cd
SZ
295 /* Cleanup memory that's no longer used. */
296 g_array_free(table_offsets, true);
297}
298
299static void acpi_ram_update(MemoryRegion *mr, GArray *data)
300{
301 uint32_t size = acpi_data_len(data);
302
303 /* Make sure RAM size is correct - in case it got changed
304 * e.g. by migration */
305 memory_region_ram_resize(mr, size, &error_abort);
306
307 memcpy(memory_region_get_ram_ptr(mr), data->data, size);
308 memory_region_set_dirty(mr, 0, size);
309}
310
311static void virt_acpi_build_update(void *build_opaque, uint32_t offset)
312{
313 AcpiBuildState *build_state = build_opaque;
314 AcpiBuildTables tables;
315
316 /* No state to update or already patched? Nothing to do. */
317 if (!build_state || build_state->patched) {
318 return;
319 }
320 build_state->patched = true;
321
322 acpi_build_tables_init(&tables);
323
324 virt_acpi_build(build_state->guest_info, &tables);
325
326 acpi_ram_update(build_state->table_mr, tables.table_data);
327 acpi_ram_update(build_state->rsdp_mr, tables.rsdp);
328 acpi_ram_update(build_state->linker_mr, tables.linker);
329
330
331 acpi_build_tables_cleanup(&tables, true);
332}
333
334static void virt_acpi_build_reset(void *build_opaque)
335{
336 AcpiBuildState *build_state = build_opaque;
337 build_state->patched = false;
338}
339
340static MemoryRegion *acpi_add_rom_blob(AcpiBuildState *build_state,
341 GArray *blob, const char *name,
342 uint64_t max_size)
343{
344 return rom_add_blob(name, blob->data, acpi_data_len(blob), max_size, -1,
345 name, virt_acpi_build_update, build_state);
346}
347
348static const VMStateDescription vmstate_virt_acpi_build = {
349 .name = "virt_acpi_build",
350 .version_id = 1,
351 .minimum_version_id = 1,
352 .fields = (VMStateField[]) {
353 VMSTATE_BOOL(patched, AcpiBuildState),
354 VMSTATE_END_OF_LIST()
355 },
356};
357
358void virt_acpi_setup(VirtGuestInfo *guest_info)
359{
360 AcpiBuildTables tables;
361 AcpiBuildState *build_state;
362
363 if (!guest_info->fw_cfg) {
364 trace_virt_acpi_setup();
365 return;
366 }
367
368 if (!acpi_enabled) {
369 trace_virt_acpi_setup();
370 return;
371 }
372
373 build_state = g_malloc0(sizeof *build_state);
374 build_state->guest_info = guest_info;
375
376 acpi_build_tables_init(&tables);
377 virt_acpi_build(build_state->guest_info, &tables);
378
379 /* Now expose it all to Guest */
380 build_state->table_mr = acpi_add_rom_blob(build_state, tables.table_data,
381 ACPI_BUILD_TABLE_FILE,
382 ACPI_BUILD_TABLE_MAX_SIZE);
383 assert(build_state->table_mr != NULL);
384
385 build_state->linker_mr =
386 acpi_add_rom_blob(build_state, tables.linker, "etc/table-loader", 0);
387
388 fw_cfg_add_file(guest_info->fw_cfg, ACPI_BUILD_TPMLOG_FILE,
389 tables.tcpalog->data, acpi_data_len(tables.tcpalog));
390
391 build_state->rsdp_mr = acpi_add_rom_blob(build_state, tables.rsdp,
392 ACPI_BUILD_RSDP_FILE, 0);
393
394 qemu_register_reset(virt_acpi_build_reset, build_state);
395 virt_acpi_build_reset(build_state);
396 vmstate_register(NULL, 0, &vmstate_virt_acpi_build, build_state);
397
398 /* Cleanup tables but don't free the memory: we track it
399 * in build_state.
400 */
401 acpi_build_tables_cleanup(&tables, false);
402}