hw/arm/virt-acpi-build: Generate MCFG table
[qemu/cris-port.git] / hw / arm / virt-acpi-build.c
blob95c83eeb85847a8e1c378c93ae178d39d712faee
1 /* Support for generating ACPI tables and passing them to Guests
3 * ARM virt ACPI generation
5 * Copyright (C) 2008-2010 Kevin O'Connor <kevin@koconnor.net>
6 * Copyright (C) 2006 Fabrice Bellard
7 * Copyright (C) 2013 Red Hat Inc
9 * Author: Michael S. Tsirkin <mst@redhat.com>
11 * Copyright (c) 2015 HUAWEI TECHNOLOGIES CO.,LTD.
13 * Author: Shannon Zhao <zhaoshenglong@huawei.com>
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.
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.
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/>.
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 #include "hw/pci/pcie_host.h"
44 #define ARM_SPI_BASE 32
46 typedef struct VirtAcpiCpuInfo {
47 DECLARE_BITMAP(found_cpus, VIRT_ACPI_CPU_ID_LIMIT);
48 } VirtAcpiCpuInfo;
50 static void virt_acpi_get_cpu_info(VirtAcpiCpuInfo *cpuinfo)
52 CPUState *cpu;
54 memset(cpuinfo->found_cpus, 0, sizeof cpuinfo->found_cpus);
55 CPU_FOREACH(cpu) {
56 set_bit(cpu->cpu_index, cpuinfo->found_cpus);
60 static void acpi_dsdt_add_cpus(Aml *scope, int smp_cpus)
62 uint16_t i;
64 for (i = 0; i < smp_cpus; i++) {
65 Aml *dev = aml_device("C%03x", i);
66 aml_append(dev, aml_name_decl("_HID", aml_string("ACPI0007")));
67 aml_append(dev, aml_name_decl("_UID", aml_int(i)));
68 aml_append(scope, dev);
72 static void acpi_dsdt_add_uart(Aml *scope, const MemMapEntry *uart_memmap,
73 int uart_irq)
75 Aml *dev = aml_device("COM0");
76 aml_append(dev, aml_name_decl("_HID", aml_string("ARMH0011")));
77 aml_append(dev, aml_name_decl("_UID", aml_int(0)));
79 Aml *crs = aml_resource_template();
80 aml_append(crs, aml_memory32_fixed(uart_memmap->base,
81 uart_memmap->size, AML_READ_WRITE));
82 aml_append(crs,
83 aml_interrupt(AML_CONSUMER, AML_LEVEL, AML_ACTIVE_HIGH,
84 AML_EXCLUSIVE, uart_irq));
85 aml_append(dev, aml_name_decl("_CRS", crs));
86 aml_append(scope, dev);
89 static void acpi_dsdt_add_rtc(Aml *scope, const MemMapEntry *rtc_memmap,
90 int rtc_irq)
92 Aml *dev = aml_device("RTC0");
93 aml_append(dev, aml_name_decl("_HID", aml_string("LNRO0013")));
94 aml_append(dev, aml_name_decl("_UID", aml_int(0)));
96 Aml *crs = aml_resource_template();
97 aml_append(crs, aml_memory32_fixed(rtc_memmap->base,
98 rtc_memmap->size, AML_READ_WRITE));
99 aml_append(crs,
100 aml_interrupt(AML_CONSUMER, AML_LEVEL, AML_ACTIVE_HIGH,
101 AML_EXCLUSIVE, rtc_irq));
102 aml_append(dev, aml_name_decl("_CRS", crs));
103 aml_append(scope, dev);
106 static void acpi_dsdt_add_flash(Aml *scope, const MemMapEntry *flash_memmap)
108 Aml *dev, *crs;
109 hwaddr base = flash_memmap->base;
110 hwaddr size = flash_memmap->size;
112 dev = aml_device("FLS0");
113 aml_append(dev, aml_name_decl("_HID", aml_string("LNRO0015")));
114 aml_append(dev, aml_name_decl("_UID", aml_int(0)));
116 crs = aml_resource_template();
117 aml_append(crs, aml_memory32_fixed(base, size, AML_READ_WRITE));
118 aml_append(dev, aml_name_decl("_CRS", crs));
119 aml_append(scope, dev);
121 dev = aml_device("FLS1");
122 aml_append(dev, aml_name_decl("_HID", aml_string("LNRO0015")));
123 aml_append(dev, aml_name_decl("_UID", aml_int(1)));
124 crs = aml_resource_template();
125 aml_append(crs, aml_memory32_fixed(base + size, size, AML_READ_WRITE));
126 aml_append(dev, aml_name_decl("_CRS", crs));
127 aml_append(scope, dev);
130 static void acpi_dsdt_add_virtio(Aml *scope,
131 const MemMapEntry *virtio_mmio_memmap,
132 int mmio_irq, int num)
134 hwaddr base = virtio_mmio_memmap->base;
135 hwaddr size = virtio_mmio_memmap->size;
136 int irq = mmio_irq;
137 int i;
139 for (i = 0; i < num; i++) {
140 Aml *dev = aml_device("VR%02u", i);
141 aml_append(dev, aml_name_decl("_HID", aml_string("LNRO0005")));
142 aml_append(dev, aml_name_decl("_UID", aml_int(i)));
144 Aml *crs = aml_resource_template();
145 aml_append(crs, aml_memory32_fixed(base, size, AML_READ_WRITE));
146 aml_append(crs,
147 aml_interrupt(AML_CONSUMER, AML_LEVEL, AML_ACTIVE_HIGH,
148 AML_EXCLUSIVE, irq + i));
149 aml_append(dev, aml_name_decl("_CRS", crs));
150 aml_append(scope, dev);
151 base += size;
155 /* RSDP */
156 static GArray *
157 build_rsdp(GArray *rsdp_table, GArray *linker, unsigned rsdt)
159 AcpiRsdpDescriptor *rsdp = acpi_data_push(rsdp_table, sizeof *rsdp);
161 bios_linker_loader_alloc(linker, ACPI_BUILD_RSDP_FILE, 16,
162 true /* fseg memory */);
164 memcpy(&rsdp->signature, "RSD PTR ", sizeof(rsdp->signature));
165 memcpy(rsdp->oem_id, ACPI_BUILD_APPNAME6, sizeof(rsdp->oem_id));
166 rsdp->length = cpu_to_le32(sizeof(*rsdp));
167 rsdp->revision = 0x02;
169 /* Point to RSDT */
170 rsdp->rsdt_physical_address = cpu_to_le32(rsdt);
171 /* Address to be filled by Guest linker */
172 bios_linker_loader_add_pointer(linker, ACPI_BUILD_RSDP_FILE,
173 ACPI_BUILD_TABLE_FILE,
174 rsdp_table, &rsdp->rsdt_physical_address,
175 sizeof rsdp->rsdt_physical_address);
176 rsdp->checksum = 0;
177 /* Checksum to be filled by Guest linker */
178 bios_linker_loader_add_checksum(linker, ACPI_BUILD_RSDP_FILE,
179 rsdp, rsdp, sizeof *rsdp, &rsdp->checksum);
181 return rsdp_table;
184 static void
185 build_mcfg(GArray *table_data, GArray *linker, VirtGuestInfo *guest_info)
187 AcpiTableMcfg *mcfg;
188 const MemMapEntry *memmap = guest_info->memmap;
189 int len = sizeof(*mcfg) + sizeof(mcfg->allocation[0]);
191 mcfg = acpi_data_push(table_data, len);
192 mcfg->allocation[0].address = cpu_to_le64(memmap[VIRT_PCIE_ECAM].base);
194 /* Only a single allocation so no need to play with segments */
195 mcfg->allocation[0].pci_segment = cpu_to_le16(0);
196 mcfg->allocation[0].start_bus_number = 0;
197 mcfg->allocation[0].end_bus_number = (memmap[VIRT_PCIE_ECAM].size
198 / PCIE_MMCFG_SIZE_MIN) - 1;
200 build_header(linker, table_data, (void *)mcfg, "MCFG", len, 5);
203 /* GTDT */
204 static void
205 build_gtdt(GArray *table_data, GArray *linker)
207 int gtdt_start = table_data->len;
208 AcpiGenericTimerTable *gtdt;
210 gtdt = acpi_data_push(table_data, sizeof *gtdt);
211 /* The interrupt values are the same with the device tree when adding 16 */
212 gtdt->secure_el1_interrupt = ARCH_TIMER_S_EL1_IRQ + 16;
213 gtdt->secure_el1_flags = ACPI_EDGE_SENSITIVE;
215 gtdt->non_secure_el1_interrupt = ARCH_TIMER_NS_EL1_IRQ + 16;
216 gtdt->non_secure_el1_flags = ACPI_EDGE_SENSITIVE;
218 gtdt->virtual_timer_interrupt = ARCH_TIMER_VIRT_IRQ + 16;
219 gtdt->virtual_timer_flags = ACPI_EDGE_SENSITIVE;
221 gtdt->non_secure_el2_interrupt = ARCH_TIMER_NS_EL2_IRQ + 16;
222 gtdt->non_secure_el2_flags = ACPI_EDGE_SENSITIVE;
224 build_header(linker, table_data,
225 (void *)(table_data->data + gtdt_start), "GTDT",
226 table_data->len - gtdt_start, 5);
229 /* MADT */
230 static void
231 build_madt(GArray *table_data, GArray *linker, VirtGuestInfo *guest_info,
232 VirtAcpiCpuInfo *cpuinfo)
234 int madt_start = table_data->len;
235 const MemMapEntry *memmap = guest_info->memmap;
236 AcpiMultipleApicTable *madt;
237 AcpiMadtGenericDistributor *gicd;
238 int i;
240 madt = acpi_data_push(table_data, sizeof *madt);
242 for (i = 0; i < guest_info->smp_cpus; i++) {
243 AcpiMadtGenericInterrupt *gicc = acpi_data_push(table_data,
244 sizeof *gicc);
245 gicc->type = ACPI_APIC_GENERIC_INTERRUPT;
246 gicc->length = sizeof(*gicc);
247 gicc->base_address = memmap[VIRT_GIC_CPU].base;
248 gicc->cpu_interface_number = i;
249 gicc->arm_mpidr = i;
250 gicc->uid = i;
251 if (test_bit(i, cpuinfo->found_cpus)) {
252 gicc->flags = cpu_to_le32(ACPI_GICC_ENABLED);
256 gicd = acpi_data_push(table_data, sizeof *gicd);
257 gicd->type = ACPI_APIC_GENERIC_DISTRIBUTOR;
258 gicd->length = sizeof(*gicd);
259 gicd->base_address = memmap[VIRT_GIC_DIST].base;
261 build_header(linker, table_data,
262 (void *)(table_data->data + madt_start), "APIC",
263 table_data->len - madt_start, 5);
266 /* FADT */
267 static void
268 build_fadt(GArray *table_data, GArray *linker, unsigned dsdt)
270 AcpiFadtDescriptorRev5_1 *fadt = acpi_data_push(table_data, sizeof(*fadt));
272 /* Hardware Reduced = 1 and use PSCI 0.2+ and with HVC */
273 fadt->flags = cpu_to_le32(1 << ACPI_FADT_F_HW_REDUCED_ACPI);
274 fadt->arm_boot_flags = cpu_to_le16((1 << ACPI_FADT_ARM_USE_PSCI_G_0_2) |
275 (1 << ACPI_FADT_ARM_PSCI_USE_HVC));
277 /* ACPI v5.1 (fadt->revision.fadt->minor_revision) */
278 fadt->minor_revision = 0x1;
280 fadt->dsdt = cpu_to_le32(dsdt);
281 /* DSDT address to be filled by Guest linker */
282 bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
283 ACPI_BUILD_TABLE_FILE,
284 table_data, &fadt->dsdt,
285 sizeof fadt->dsdt);
287 build_header(linker, table_data,
288 (void *)fadt, "FACP", sizeof(*fadt), 5);
291 /* DSDT */
292 static void
293 build_dsdt(GArray *table_data, GArray *linker, VirtGuestInfo *guest_info)
295 Aml *scope, *dsdt;
296 const MemMapEntry *memmap = guest_info->memmap;
297 const int *irqmap = guest_info->irqmap;
299 dsdt = init_aml_allocator();
300 /* Reserve space for header */
301 acpi_data_push(dsdt->buf, sizeof(AcpiTableHeader));
303 scope = aml_scope("\\_SB");
304 acpi_dsdt_add_cpus(scope, guest_info->smp_cpus);
305 acpi_dsdt_add_uart(scope, &memmap[VIRT_UART],
306 (irqmap[VIRT_UART] + ARM_SPI_BASE));
307 acpi_dsdt_add_rtc(scope, &memmap[VIRT_RTC],
308 (irqmap[VIRT_RTC] + ARM_SPI_BASE));
309 acpi_dsdt_add_flash(scope, &memmap[VIRT_FLASH]);
310 acpi_dsdt_add_virtio(scope, &memmap[VIRT_MMIO],
311 (irqmap[VIRT_MMIO] + ARM_SPI_BASE), NUM_VIRTIO_TRANSPORTS);
312 aml_append(dsdt, scope);
314 /* copy AML table into ACPI tables blob and patch header there */
315 g_array_append_vals(table_data, dsdt->buf->data, dsdt->buf->len);
316 build_header(linker, table_data,
317 (void *)(table_data->data + table_data->len - dsdt->buf->len),
318 "DSDT", dsdt->buf->len, 5);
319 free_aml_allocator();
322 typedef
323 struct AcpiBuildState {
324 /* Copy of table in RAM (for patching). */
325 MemoryRegion *table_mr;
326 MemoryRegion *rsdp_mr;
327 MemoryRegion *linker_mr;
328 /* Is table patched? */
329 bool patched;
330 VirtGuestInfo *guest_info;
331 } AcpiBuildState;
333 static
334 void virt_acpi_build(VirtGuestInfo *guest_info, AcpiBuildTables *tables)
336 GArray *table_offsets;
337 unsigned dsdt, rsdt;
338 VirtAcpiCpuInfo cpuinfo;
339 GArray *tables_blob = tables->table_data;
341 virt_acpi_get_cpu_info(&cpuinfo);
343 table_offsets = g_array_new(false, true /* clear */,
344 sizeof(uint32_t));
346 bios_linker_loader_alloc(tables->linker, ACPI_BUILD_TABLE_FILE,
347 64, false /* high memory */);
350 * The ACPI v5.1 tables for Hardware-reduced ACPI platform are:
351 * RSDP
352 * RSDT
353 * FADT
354 * GTDT
355 * MADT
356 * DSDT
359 /* DSDT is pointed to by FADT */
360 dsdt = tables_blob->len;
361 build_dsdt(tables_blob, tables->linker, guest_info);
363 /* FADT MADT GTDT pointed to by RSDT */
364 acpi_add_table(table_offsets, tables_blob);
365 build_fadt(tables_blob, tables->linker, dsdt);
367 acpi_add_table(table_offsets, tables_blob);
368 build_madt(tables_blob, tables->linker, guest_info, &cpuinfo);
370 acpi_add_table(table_offsets, tables_blob);
371 build_gtdt(tables_blob, tables->linker);
373 acpi_add_table(table_offsets, tables_blob);
374 build_mcfg(tables_blob, tables->linker, guest_info);
376 /* RSDT is pointed to by RSDP */
377 rsdt = tables_blob->len;
378 build_rsdt(tables_blob, tables->linker, table_offsets);
380 /* RSDP is in FSEG memory, so allocate it separately */
381 build_rsdp(tables->rsdp, tables->linker, rsdt);
383 /* Cleanup memory that's no longer used. */
384 g_array_free(table_offsets, true);
387 static void acpi_ram_update(MemoryRegion *mr, GArray *data)
389 uint32_t size = acpi_data_len(data);
391 /* Make sure RAM size is correct - in case it got changed
392 * e.g. by migration */
393 memory_region_ram_resize(mr, size, &error_abort);
395 memcpy(memory_region_get_ram_ptr(mr), data->data, size);
396 memory_region_set_dirty(mr, 0, size);
399 static void virt_acpi_build_update(void *build_opaque, uint32_t offset)
401 AcpiBuildState *build_state = build_opaque;
402 AcpiBuildTables tables;
404 /* No state to update or already patched? Nothing to do. */
405 if (!build_state || build_state->patched) {
406 return;
408 build_state->patched = true;
410 acpi_build_tables_init(&tables);
412 virt_acpi_build(build_state->guest_info, &tables);
414 acpi_ram_update(build_state->table_mr, tables.table_data);
415 acpi_ram_update(build_state->rsdp_mr, tables.rsdp);
416 acpi_ram_update(build_state->linker_mr, tables.linker);
419 acpi_build_tables_cleanup(&tables, true);
422 static void virt_acpi_build_reset(void *build_opaque)
424 AcpiBuildState *build_state = build_opaque;
425 build_state->patched = false;
428 static MemoryRegion *acpi_add_rom_blob(AcpiBuildState *build_state,
429 GArray *blob, const char *name,
430 uint64_t max_size)
432 return rom_add_blob(name, blob->data, acpi_data_len(blob), max_size, -1,
433 name, virt_acpi_build_update, build_state);
436 static const VMStateDescription vmstate_virt_acpi_build = {
437 .name = "virt_acpi_build",
438 .version_id = 1,
439 .minimum_version_id = 1,
440 .fields = (VMStateField[]) {
441 VMSTATE_BOOL(patched, AcpiBuildState),
442 VMSTATE_END_OF_LIST()
446 void virt_acpi_setup(VirtGuestInfo *guest_info)
448 AcpiBuildTables tables;
449 AcpiBuildState *build_state;
451 if (!guest_info->fw_cfg) {
452 trace_virt_acpi_setup();
453 return;
456 if (!acpi_enabled) {
457 trace_virt_acpi_setup();
458 return;
461 build_state = g_malloc0(sizeof *build_state);
462 build_state->guest_info = guest_info;
464 acpi_build_tables_init(&tables);
465 virt_acpi_build(build_state->guest_info, &tables);
467 /* Now expose it all to Guest */
468 build_state->table_mr = acpi_add_rom_blob(build_state, tables.table_data,
469 ACPI_BUILD_TABLE_FILE,
470 ACPI_BUILD_TABLE_MAX_SIZE);
471 assert(build_state->table_mr != NULL);
473 build_state->linker_mr =
474 acpi_add_rom_blob(build_state, tables.linker, "etc/table-loader", 0);
476 fw_cfg_add_file(guest_info->fw_cfg, ACPI_BUILD_TPMLOG_FILE,
477 tables.tcpalog->data, acpi_data_len(tables.tcpalog));
479 build_state->rsdp_mr = acpi_add_rom_blob(build_state, tables.rsdp,
480 ACPI_BUILD_RSDP_FILE, 0);
482 qemu_register_reset(virt_acpi_build_reset, build_state);
483 virt_acpi_build_reset(build_state);
484 vmstate_register(NULL, 0, &vmstate_virt_acpi_build, build_state);
486 /* Cleanup tables but don't free the memory: we track it
487 * in build_state.
489 acpi_build_tables_cleanup(&tables, false);