]> git.proxmox.com Git - mirror_qemu.git/blame - hw/arm/virt-acpi-build.c
hw/arm/virt-acpi-build: Generation of DSDT table for virt devices
[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
140/* DSDT */
141static void
142build_dsdt(GArray *table_data, GArray *linker, VirtGuestInfo *guest_info)
143{
144 Aml *scope, *dsdt;
145 const MemMapEntry *memmap = guest_info->memmap;
146 const int *irqmap = guest_info->irqmap;
147
148 dsdt = init_aml_allocator();
149 /* Reserve space for header */
150 acpi_data_push(dsdt->buf, sizeof(AcpiTableHeader));
151
152 scope = aml_scope("\\_SB");
153 acpi_dsdt_add_cpus(scope, guest_info->smp_cpus);
154 acpi_dsdt_add_uart(scope, &memmap[VIRT_UART],
155 (irqmap[VIRT_UART] + ARM_SPI_BASE));
156 acpi_dsdt_add_rtc(scope, &memmap[VIRT_RTC],
157 (irqmap[VIRT_RTC] + ARM_SPI_BASE));
158 acpi_dsdt_add_flash(scope, &memmap[VIRT_FLASH]);
159 acpi_dsdt_add_virtio(scope, &memmap[VIRT_MMIO],
160 (irqmap[VIRT_MMIO] + ARM_SPI_BASE), NUM_VIRTIO_TRANSPORTS);
161 aml_append(dsdt, scope);
162
163 /* copy AML table into ACPI tables blob and patch header there */
164 g_array_append_vals(table_data, dsdt->buf->data, dsdt->buf->len);
165 build_header(linker, table_data,
166 (void *)(table_data->data + table_data->len - dsdt->buf->len),
167 "DSDT", dsdt->buf->len, 5);
168 free_aml_allocator();
169}
170
f5d8c8cd
SZ
171typedef
172struct AcpiBuildState {
173 /* Copy of table in RAM (for patching). */
174 MemoryRegion *table_mr;
175 MemoryRegion *rsdp_mr;
176 MemoryRegion *linker_mr;
177 /* Is table patched? */
178 bool patched;
179 VirtGuestInfo *guest_info;
180} AcpiBuildState;
181
182static
183void virt_acpi_build(VirtGuestInfo *guest_info, AcpiBuildTables *tables)
184{
185 GArray *table_offsets;
dfccd8cf 186 GArray *tables_blob = tables->table_data;
f5d8c8cd
SZ
187
188 table_offsets = g_array_new(false, true /* clear */,
189 sizeof(uint32_t));
190
191 bios_linker_loader_alloc(tables->linker, ACPI_BUILD_TABLE_FILE,
192 64, false /* high memory */);
193
194 /*
195 * The ACPI v5.1 tables for Hardware-reduced ACPI platform are:
196 * RSDP
197 * RSDT
198 * FADT
199 * GTDT
200 * MADT
201 * DSDT
202 */
203
dfccd8cf
SZ
204 /* DSDT is pointed to by FADT */
205 build_dsdt(tables_blob, tables->linker, guest_info);
206
f5d8c8cd
SZ
207 /* Cleanup memory that's no longer used. */
208 g_array_free(table_offsets, true);
209}
210
211static void acpi_ram_update(MemoryRegion *mr, GArray *data)
212{
213 uint32_t size = acpi_data_len(data);
214
215 /* Make sure RAM size is correct - in case it got changed
216 * e.g. by migration */
217 memory_region_ram_resize(mr, size, &error_abort);
218
219 memcpy(memory_region_get_ram_ptr(mr), data->data, size);
220 memory_region_set_dirty(mr, 0, size);
221}
222
223static void virt_acpi_build_update(void *build_opaque, uint32_t offset)
224{
225 AcpiBuildState *build_state = build_opaque;
226 AcpiBuildTables tables;
227
228 /* No state to update or already patched? Nothing to do. */
229 if (!build_state || build_state->patched) {
230 return;
231 }
232 build_state->patched = true;
233
234 acpi_build_tables_init(&tables);
235
236 virt_acpi_build(build_state->guest_info, &tables);
237
238 acpi_ram_update(build_state->table_mr, tables.table_data);
239 acpi_ram_update(build_state->rsdp_mr, tables.rsdp);
240 acpi_ram_update(build_state->linker_mr, tables.linker);
241
242
243 acpi_build_tables_cleanup(&tables, true);
244}
245
246static void virt_acpi_build_reset(void *build_opaque)
247{
248 AcpiBuildState *build_state = build_opaque;
249 build_state->patched = false;
250}
251
252static MemoryRegion *acpi_add_rom_blob(AcpiBuildState *build_state,
253 GArray *blob, const char *name,
254 uint64_t max_size)
255{
256 return rom_add_blob(name, blob->data, acpi_data_len(blob), max_size, -1,
257 name, virt_acpi_build_update, build_state);
258}
259
260static const VMStateDescription vmstate_virt_acpi_build = {
261 .name = "virt_acpi_build",
262 .version_id = 1,
263 .minimum_version_id = 1,
264 .fields = (VMStateField[]) {
265 VMSTATE_BOOL(patched, AcpiBuildState),
266 VMSTATE_END_OF_LIST()
267 },
268};
269
270void virt_acpi_setup(VirtGuestInfo *guest_info)
271{
272 AcpiBuildTables tables;
273 AcpiBuildState *build_state;
274
275 if (!guest_info->fw_cfg) {
276 trace_virt_acpi_setup();
277 return;
278 }
279
280 if (!acpi_enabled) {
281 trace_virt_acpi_setup();
282 return;
283 }
284
285 build_state = g_malloc0(sizeof *build_state);
286 build_state->guest_info = guest_info;
287
288 acpi_build_tables_init(&tables);
289 virt_acpi_build(build_state->guest_info, &tables);
290
291 /* Now expose it all to Guest */
292 build_state->table_mr = acpi_add_rom_blob(build_state, tables.table_data,
293 ACPI_BUILD_TABLE_FILE,
294 ACPI_BUILD_TABLE_MAX_SIZE);
295 assert(build_state->table_mr != NULL);
296
297 build_state->linker_mr =
298 acpi_add_rom_blob(build_state, tables.linker, "etc/table-loader", 0);
299
300 fw_cfg_add_file(guest_info->fw_cfg, ACPI_BUILD_TPMLOG_FILE,
301 tables.tcpalog->data, acpi_data_len(tables.tcpalog));
302
303 build_state->rsdp_mr = acpi_add_rom_blob(build_state, tables.rsdp,
304 ACPI_BUILD_RSDP_FILE, 0);
305
306 qemu_register_reset(virt_acpi_build_reset, build_state);
307 virt_acpi_build_reset(build_state);
308 vmstate_register(NULL, 0, &vmstate_virt_acpi_build, build_state);
309
310 /* Cleanup tables but don't free the memory: we track it
311 * in build_state.
312 */
313 acpi_build_tables_cleanup(&tables, false);
314}