hw/arm/virt-acpi-build: Add GICC ACPI subtable for GICv3
[qemu/ar7.git] / hw / arm / virt-acpi-build.c
blobb170dfa3e2d983faeaa8f1e34811b492b295e3e4
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"
43 #include "hw/pci/pci.h"
45 #define ARM_SPI_BASE 32
47 typedef struct VirtAcpiCpuInfo {
48 DECLARE_BITMAP(found_cpus, VIRT_ACPI_CPU_ID_LIMIT);
49 } VirtAcpiCpuInfo;
51 static void virt_acpi_get_cpu_info(VirtAcpiCpuInfo *cpuinfo)
53 CPUState *cpu;
55 memset(cpuinfo->found_cpus, 0, sizeof cpuinfo->found_cpus);
56 CPU_FOREACH(cpu) {
57 set_bit(cpu->cpu_index, cpuinfo->found_cpus);
61 static void acpi_dsdt_add_cpus(Aml *scope, int smp_cpus)
63 uint16_t i;
65 for (i = 0; i < smp_cpus; i++) {
66 Aml *dev = aml_device("C%03x", i);
67 aml_append(dev, aml_name_decl("_HID", aml_string("ACPI0007")));
68 aml_append(dev, aml_name_decl("_UID", aml_int(i)));
69 aml_append(scope, dev);
73 static void acpi_dsdt_add_uart(Aml *scope, const MemMapEntry *uart_memmap,
74 int uart_irq)
76 Aml *dev = aml_device("COM0");
77 aml_append(dev, aml_name_decl("_HID", aml_string("ARMH0011")));
78 aml_append(dev, aml_name_decl("_UID", aml_int(0)));
80 Aml *crs = aml_resource_template();
81 aml_append(crs, aml_memory32_fixed(uart_memmap->base,
82 uart_memmap->size, AML_READ_WRITE));
83 aml_append(crs,
84 aml_interrupt(AML_CONSUMER, AML_LEVEL, AML_ACTIVE_HIGH,
85 AML_EXCLUSIVE, uart_irq));
86 aml_append(dev, aml_name_decl("_CRS", crs));
88 /* The _ADR entry is used to link this device to the UART described
89 * in the SPCR table, i.e. SPCR.base_address.address == _ADR.
91 aml_append(dev, aml_name_decl("_ADR", aml_int(uart_memmap->base)));
93 aml_append(scope, dev);
96 static void acpi_dsdt_add_rtc(Aml *scope, const MemMapEntry *rtc_memmap,
97 int rtc_irq)
99 Aml *dev = aml_device("RTC0");
100 aml_append(dev, aml_name_decl("_HID", aml_string("LNRO0013")));
101 aml_append(dev, aml_name_decl("_UID", aml_int(0)));
103 Aml *crs = aml_resource_template();
104 aml_append(crs, aml_memory32_fixed(rtc_memmap->base,
105 rtc_memmap->size, AML_READ_WRITE));
106 aml_append(crs,
107 aml_interrupt(AML_CONSUMER, AML_LEVEL, AML_ACTIVE_HIGH,
108 AML_EXCLUSIVE, rtc_irq));
109 aml_append(dev, aml_name_decl("_CRS", crs));
110 aml_append(scope, dev);
113 static void acpi_dsdt_add_flash(Aml *scope, const MemMapEntry *flash_memmap)
115 Aml *dev, *crs;
116 hwaddr base = flash_memmap->base;
117 hwaddr size = flash_memmap->size / 2;
119 dev = aml_device("FLS0");
120 aml_append(dev, aml_name_decl("_HID", aml_string("LNRO0015")));
121 aml_append(dev, aml_name_decl("_UID", aml_int(0)));
123 crs = aml_resource_template();
124 aml_append(crs, aml_memory32_fixed(base, size, AML_READ_WRITE));
125 aml_append(dev, aml_name_decl("_CRS", crs));
126 aml_append(scope, dev);
128 dev = aml_device("FLS1");
129 aml_append(dev, aml_name_decl("_HID", aml_string("LNRO0015")));
130 aml_append(dev, aml_name_decl("_UID", aml_int(1)));
131 crs = aml_resource_template();
132 aml_append(crs, aml_memory32_fixed(base + size, size, AML_READ_WRITE));
133 aml_append(dev, aml_name_decl("_CRS", crs));
134 aml_append(scope, dev);
137 static void acpi_dsdt_add_virtio(Aml *scope,
138 const MemMapEntry *virtio_mmio_memmap,
139 int mmio_irq, int num)
141 hwaddr base = virtio_mmio_memmap->base;
142 hwaddr size = virtio_mmio_memmap->size;
143 int irq = mmio_irq;
144 int i;
146 for (i = 0; i < num; i++) {
147 Aml *dev = aml_device("VR%02u", i);
148 aml_append(dev, aml_name_decl("_HID", aml_string("LNRO0005")));
149 aml_append(dev, aml_name_decl("_UID", aml_int(i)));
151 Aml *crs = aml_resource_template();
152 aml_append(crs, aml_memory32_fixed(base, size, AML_READ_WRITE));
153 aml_append(crs,
154 aml_interrupt(AML_CONSUMER, AML_LEVEL, AML_ACTIVE_HIGH,
155 AML_EXCLUSIVE, irq + i));
156 aml_append(dev, aml_name_decl("_CRS", crs));
157 aml_append(scope, dev);
158 base += size;
162 static void acpi_dsdt_add_pci(Aml *scope, const MemMapEntry *memmap, int irq,
163 bool use_highmem)
165 Aml *method, *crs, *ifctx, *UUID, *ifctx1, *elsectx, *buf;
166 int i, bus_no;
167 hwaddr base_mmio = memmap[VIRT_PCIE_MMIO].base;
168 hwaddr size_mmio = memmap[VIRT_PCIE_MMIO].size;
169 hwaddr base_pio = memmap[VIRT_PCIE_PIO].base;
170 hwaddr size_pio = memmap[VIRT_PCIE_PIO].size;
171 hwaddr base_ecam = memmap[VIRT_PCIE_ECAM].base;
172 hwaddr size_ecam = memmap[VIRT_PCIE_ECAM].size;
173 int nr_pcie_buses = size_ecam / PCIE_MMCFG_SIZE_MIN;
175 Aml *dev = aml_device("%s", "PCI0");
176 aml_append(dev, aml_name_decl("_HID", aml_string("PNP0A08")));
177 aml_append(dev, aml_name_decl("_CID", aml_string("PNP0A03")));
178 aml_append(dev, aml_name_decl("_SEG", aml_int(0)));
179 aml_append(dev, aml_name_decl("_BBN", aml_int(0)));
180 aml_append(dev, aml_name_decl("_ADR", aml_int(0)));
181 aml_append(dev, aml_name_decl("_UID", aml_string("PCI0")));
182 aml_append(dev, aml_name_decl("_STR", aml_unicode("PCIe 0 Device")));
183 aml_append(dev, aml_name_decl("_CCA", aml_int(1)));
185 /* Declare the PCI Routing Table. */
186 Aml *rt_pkg = aml_package(nr_pcie_buses * PCI_NUM_PINS);
187 for (bus_no = 0; bus_no < nr_pcie_buses; bus_no++) {
188 for (i = 0; i < PCI_NUM_PINS; i++) {
189 int gsi = (i + bus_no) % PCI_NUM_PINS;
190 Aml *pkg = aml_package(4);
191 aml_append(pkg, aml_int((bus_no << 16) | 0xFFFF));
192 aml_append(pkg, aml_int(i));
193 aml_append(pkg, aml_name("GSI%d", gsi));
194 aml_append(pkg, aml_int(0));
195 aml_append(rt_pkg, pkg);
198 aml_append(dev, aml_name_decl("_PRT", rt_pkg));
200 /* Create GSI link device */
201 for (i = 0; i < PCI_NUM_PINS; i++) {
202 Aml *dev_gsi = aml_device("GSI%d", i);
203 aml_append(dev_gsi, aml_name_decl("_HID", aml_string("PNP0C0F")));
204 aml_append(dev_gsi, aml_name_decl("_UID", aml_int(0)));
205 crs = aml_resource_template();
206 aml_append(crs,
207 aml_interrupt(AML_CONSUMER, AML_LEVEL, AML_ACTIVE_HIGH,
208 AML_EXCLUSIVE, irq + i));
209 aml_append(dev_gsi, aml_name_decl("_PRS", crs));
210 crs = aml_resource_template();
211 aml_append(crs,
212 aml_interrupt(AML_CONSUMER, AML_LEVEL, AML_ACTIVE_HIGH,
213 AML_EXCLUSIVE, irq + i));
214 aml_append(dev_gsi, aml_name_decl("_CRS", crs));
215 method = aml_method("_SRS", 1);
216 aml_append(dev_gsi, method);
217 aml_append(dev, dev_gsi);
220 method = aml_method("_CBA", 0);
221 aml_append(method, aml_return(aml_int(base_ecam)));
222 aml_append(dev, method);
224 method = aml_method("_CRS", 0);
225 Aml *rbuf = aml_resource_template();
226 aml_append(rbuf,
227 aml_word_bus_number(AML_MIN_FIXED, AML_MAX_FIXED, AML_POS_DECODE,
228 0x0000, 0x0000, nr_pcie_buses - 1, 0x0000,
229 nr_pcie_buses));
230 aml_append(rbuf,
231 aml_dword_memory(AML_POS_DECODE, AML_MIN_FIXED, AML_MAX_FIXED,
232 AML_NON_CACHEABLE, AML_READ_WRITE, 0x0000, base_mmio,
233 base_mmio + size_mmio - 1, 0x0000, size_mmio));
234 aml_append(rbuf,
235 aml_dword_io(AML_MIN_FIXED, AML_MAX_FIXED, AML_POS_DECODE,
236 AML_ENTIRE_RANGE, 0x0000, 0x0000, size_pio - 1, base_pio,
237 size_pio));
239 if (use_highmem) {
240 hwaddr base_mmio_high = memmap[VIRT_PCIE_MMIO_HIGH].base;
241 hwaddr size_mmio_high = memmap[VIRT_PCIE_MMIO_HIGH].size;
243 aml_append(rbuf,
244 aml_qword_memory(AML_POS_DECODE, AML_MIN_FIXED, AML_MAX_FIXED,
245 AML_NON_CACHEABLE, AML_READ_WRITE, 0x0000,
246 base_mmio_high, base_mmio_high, 0x0000,
247 size_mmio_high));
250 aml_append(method, aml_name_decl("RBUF", rbuf));
251 aml_append(method, aml_return(rbuf));
252 aml_append(dev, method);
254 /* Declare an _OSC (OS Control Handoff) method */
255 aml_append(dev, aml_name_decl("SUPP", aml_int(0)));
256 aml_append(dev, aml_name_decl("CTRL", aml_int(0)));
257 method = aml_method("_OSC", 4);
258 aml_append(method,
259 aml_create_dword_field(aml_arg(3), aml_int(0), "CDW1"));
261 /* PCI Firmware Specification 3.0
262 * 4.5.1. _OSC Interface for PCI Host Bridge Devices
263 * The _OSC interface for a PCI/PCI-X/PCI Express hierarchy is
264 * identified by the Universal Unique IDentifier (UUID)
265 * 33DB4D5B-1FF7-401C-9657-7441C03DD766
267 UUID = aml_touuid("33DB4D5B-1FF7-401C-9657-7441C03DD766");
268 ifctx = aml_if(aml_equal(aml_arg(0), UUID));
269 aml_append(ifctx,
270 aml_create_dword_field(aml_arg(3), aml_int(4), "CDW2"));
271 aml_append(ifctx,
272 aml_create_dword_field(aml_arg(3), aml_int(8), "CDW3"));
273 aml_append(ifctx, aml_store(aml_name("CDW2"), aml_name("SUPP")));
274 aml_append(ifctx, aml_store(aml_name("CDW3"), aml_name("CTRL")));
275 aml_append(ifctx, aml_store(aml_and(aml_name("CTRL"), aml_int(0x1D)),
276 aml_name("CTRL")));
278 ifctx1 = aml_if(aml_lnot(aml_equal(aml_arg(1), aml_int(0x1))));
279 aml_append(ifctx1, aml_store(aml_or(aml_name("CDW1"), aml_int(0x08)),
280 aml_name("CDW1")));
281 aml_append(ifctx, ifctx1);
283 ifctx1 = aml_if(aml_lnot(aml_equal(aml_name("CDW3"), aml_name("CTRL"))));
284 aml_append(ifctx1, aml_store(aml_or(aml_name("CDW1"), aml_int(0x10)),
285 aml_name("CDW1")));
286 aml_append(ifctx, ifctx1);
288 aml_append(ifctx, aml_store(aml_name("CTRL"), aml_name("CDW3")));
289 aml_append(ifctx, aml_return(aml_arg(3)));
290 aml_append(method, ifctx);
292 elsectx = aml_else();
293 aml_append(elsectx, aml_store(aml_or(aml_name("CDW1"), aml_int(4)),
294 aml_name("CDW1")));
295 aml_append(elsectx, aml_return(aml_arg(3)));
296 aml_append(method, elsectx);
297 aml_append(dev, method);
299 method = aml_method("_DSM", 4);
301 /* PCI Firmware Specification 3.0
302 * 4.6.1. _DSM for PCI Express Slot Information
303 * The UUID in _DSM in this context is
304 * {E5C937D0-3553-4D7A-9117-EA4D19C3434D}
306 UUID = aml_touuid("E5C937D0-3553-4D7A-9117-EA4D19C3434D");
307 ifctx = aml_if(aml_equal(aml_arg(0), UUID));
308 ifctx1 = aml_if(aml_equal(aml_arg(2), aml_int(0)));
309 uint8_t byte_list[1] = {1};
310 buf = aml_buffer(1, byte_list);
311 aml_append(ifctx1, aml_return(buf));
312 aml_append(ifctx, ifctx1);
313 aml_append(method, ifctx);
315 byte_list[0] = 0;
316 buf = aml_buffer(1, byte_list);
317 aml_append(method, aml_return(buf));
318 aml_append(dev, method);
320 Aml *dev_rp0 = aml_device("%s", "RP0");
321 aml_append(dev_rp0, aml_name_decl("_ADR", aml_int(0)));
322 aml_append(dev, dev_rp0);
323 aml_append(scope, dev);
326 /* RSDP */
327 static GArray *
328 build_rsdp(GArray *rsdp_table, GArray *linker, unsigned rsdt)
330 AcpiRsdpDescriptor *rsdp = acpi_data_push(rsdp_table, sizeof *rsdp);
332 bios_linker_loader_alloc(linker, ACPI_BUILD_RSDP_FILE, 16,
333 true /* fseg memory */);
335 memcpy(&rsdp->signature, "RSD PTR ", sizeof(rsdp->signature));
336 memcpy(rsdp->oem_id, ACPI_BUILD_APPNAME6, sizeof(rsdp->oem_id));
337 rsdp->length = cpu_to_le32(sizeof(*rsdp));
338 rsdp->revision = 0x02;
340 /* Point to RSDT */
341 rsdp->rsdt_physical_address = cpu_to_le32(rsdt);
342 /* Address to be filled by Guest linker */
343 bios_linker_loader_add_pointer(linker, ACPI_BUILD_RSDP_FILE,
344 ACPI_BUILD_TABLE_FILE,
345 rsdp_table, &rsdp->rsdt_physical_address,
346 sizeof rsdp->rsdt_physical_address);
347 rsdp->checksum = 0;
348 /* Checksum to be filled by Guest linker */
349 bios_linker_loader_add_checksum(linker, ACPI_BUILD_RSDP_FILE,
350 rsdp, rsdp, sizeof *rsdp, &rsdp->checksum);
352 return rsdp_table;
355 static void
356 build_spcr(GArray *table_data, GArray *linker, VirtGuestInfo *guest_info)
358 AcpiSerialPortConsoleRedirection *spcr;
359 const MemMapEntry *uart_memmap = &guest_info->memmap[VIRT_UART];
360 int irq = guest_info->irqmap[VIRT_UART] + ARM_SPI_BASE;
362 spcr = acpi_data_push(table_data, sizeof(*spcr));
364 spcr->interface_type = 0x3; /* ARM PL011 UART */
366 spcr->base_address.space_id = AML_SYSTEM_MEMORY;
367 spcr->base_address.bit_width = 8;
368 spcr->base_address.bit_offset = 0;
369 spcr->base_address.access_width = 1;
370 spcr->base_address.address = cpu_to_le64(uart_memmap->base);
372 spcr->interrupt_types = (1 << 3); /* Bit[3] ARMH GIC interrupt */
373 spcr->gsi = cpu_to_le32(irq); /* Global System Interrupt */
375 spcr->baud = 3; /* Baud Rate: 3 = 9600 */
376 spcr->parity = 0; /* No Parity */
377 spcr->stopbits = 1; /* 1 Stop bit */
378 spcr->flowctrl = (1 << 1); /* Bit[1] = RTS/CTS hardware flow control */
379 spcr->term_type = 0; /* Terminal Type: 0 = VT100 */
381 spcr->pci_device_id = 0xffff; /* PCI Device ID: not a PCI device */
382 spcr->pci_vendor_id = 0xffff; /* PCI Vendor ID: not a PCI device */
384 build_header(linker, table_data, (void *)spcr, "SPCR", sizeof(*spcr), 2);
387 static void
388 build_mcfg(GArray *table_data, GArray *linker, VirtGuestInfo *guest_info)
390 AcpiTableMcfg *mcfg;
391 const MemMapEntry *memmap = guest_info->memmap;
392 int len = sizeof(*mcfg) + sizeof(mcfg->allocation[0]);
394 mcfg = acpi_data_push(table_data, len);
395 mcfg->allocation[0].address = cpu_to_le64(memmap[VIRT_PCIE_ECAM].base);
397 /* Only a single allocation so no need to play with segments */
398 mcfg->allocation[0].pci_segment = cpu_to_le16(0);
399 mcfg->allocation[0].start_bus_number = 0;
400 mcfg->allocation[0].end_bus_number = (memmap[VIRT_PCIE_ECAM].size
401 / PCIE_MMCFG_SIZE_MIN) - 1;
403 build_header(linker, table_data, (void *)mcfg, "MCFG", len, 1);
406 /* GTDT */
407 static void
408 build_gtdt(GArray *table_data, GArray *linker)
410 int gtdt_start = table_data->len;
411 AcpiGenericTimerTable *gtdt;
413 gtdt = acpi_data_push(table_data, sizeof *gtdt);
414 /* The interrupt values are the same with the device tree when adding 16 */
415 gtdt->secure_el1_interrupt = ARCH_TIMER_S_EL1_IRQ + 16;
416 gtdt->secure_el1_flags = ACPI_EDGE_SENSITIVE;
418 gtdt->non_secure_el1_interrupt = ARCH_TIMER_NS_EL1_IRQ + 16;
419 gtdt->non_secure_el1_flags = ACPI_EDGE_SENSITIVE;
421 gtdt->virtual_timer_interrupt = ARCH_TIMER_VIRT_IRQ + 16;
422 gtdt->virtual_timer_flags = ACPI_EDGE_SENSITIVE;
424 gtdt->non_secure_el2_interrupt = ARCH_TIMER_NS_EL2_IRQ + 16;
425 gtdt->non_secure_el2_flags = ACPI_EDGE_SENSITIVE;
427 build_header(linker, table_data,
428 (void *)(table_data->data + gtdt_start), "GTDT",
429 table_data->len - gtdt_start, 2);
432 /* MADT */
433 static void
434 build_madt(GArray *table_data, GArray *linker, VirtGuestInfo *guest_info,
435 VirtAcpiCpuInfo *cpuinfo)
437 int madt_start = table_data->len;
438 const MemMapEntry *memmap = guest_info->memmap;
439 const int *irqmap = guest_info->irqmap;
440 AcpiMultipleApicTable *madt;
441 AcpiMadtGenericDistributor *gicd;
442 AcpiMadtGenericMsiFrame *gic_msi;
443 int i;
445 madt = acpi_data_push(table_data, sizeof *madt);
447 gicd = acpi_data_push(table_data, sizeof *gicd);
448 gicd->type = ACPI_APIC_GENERIC_DISTRIBUTOR;
449 gicd->length = sizeof(*gicd);
450 gicd->base_address = memmap[VIRT_GIC_DIST].base;
452 for (i = 0; i < guest_info->smp_cpus; i++) {
453 AcpiMadtGenericInterrupt *gicc = acpi_data_push(table_data,
454 sizeof *gicc);
455 gicc->type = ACPI_APIC_GENERIC_INTERRUPT;
456 gicc->length = sizeof(*gicc);
457 if (guest_info->gic_version == 2) {
458 gicc->base_address = memmap[VIRT_GIC_CPU].base;
460 gicc->cpu_interface_number = i;
461 gicc->arm_mpidr = i;
462 gicc->uid = i;
463 if (test_bit(i, cpuinfo->found_cpus)) {
464 gicc->flags = cpu_to_le32(ACPI_GICC_ENABLED);
468 if (guest_info->gic_version == 3) {
469 AcpiMadtGenericRedistributor *gicr = acpi_data_push(table_data,
470 sizeof *gicr);
472 gicr->type = ACPI_APIC_GENERIC_REDISTRIBUTOR;
473 gicr->length = sizeof(*gicr);
474 gicr->base_address = cpu_to_le64(memmap[VIRT_GIC_REDIST].base);
475 gicr->range_length = cpu_to_le32(memmap[VIRT_GIC_REDIST].size);
476 } else {
477 gic_msi = acpi_data_push(table_data, sizeof *gic_msi);
478 gic_msi->type = ACPI_APIC_GENERIC_MSI_FRAME;
479 gic_msi->length = sizeof(*gic_msi);
480 gic_msi->gic_msi_frame_id = 0;
481 gic_msi->base_address = cpu_to_le64(memmap[VIRT_GIC_V2M].base);
482 gic_msi->flags = cpu_to_le32(1);
483 gic_msi->spi_count = cpu_to_le16(NUM_GICV2M_SPIS);
484 gic_msi->spi_base = cpu_to_le16(irqmap[VIRT_GIC_V2M] + ARM_SPI_BASE);
487 build_header(linker, table_data,
488 (void *)(table_data->data + madt_start), "APIC",
489 table_data->len - madt_start, 3);
492 /* FADT */
493 static void
494 build_fadt(GArray *table_data, GArray *linker, unsigned dsdt)
496 AcpiFadtDescriptorRev5_1 *fadt = acpi_data_push(table_data, sizeof(*fadt));
498 /* Hardware Reduced = 1 and use PSCI 0.2+ and with HVC */
499 fadt->flags = cpu_to_le32(1 << ACPI_FADT_F_HW_REDUCED_ACPI);
500 fadt->arm_boot_flags = cpu_to_le16((1 << ACPI_FADT_ARM_USE_PSCI_G_0_2) |
501 (1 << ACPI_FADT_ARM_PSCI_USE_HVC));
503 /* ACPI v5.1 (fadt->revision.fadt->minor_revision) */
504 fadt->minor_revision = 0x1;
506 fadt->dsdt = cpu_to_le32(dsdt);
507 /* DSDT address to be filled by Guest linker */
508 bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
509 ACPI_BUILD_TABLE_FILE,
510 table_data, &fadt->dsdt,
511 sizeof fadt->dsdt);
513 build_header(linker, table_data,
514 (void *)fadt, "FACP", sizeof(*fadt), 5);
517 /* DSDT */
518 static void
519 build_dsdt(GArray *table_data, GArray *linker, VirtGuestInfo *guest_info)
521 Aml *scope, *dsdt;
522 const MemMapEntry *memmap = guest_info->memmap;
523 const int *irqmap = guest_info->irqmap;
525 dsdt = init_aml_allocator();
526 /* Reserve space for header */
527 acpi_data_push(dsdt->buf, sizeof(AcpiTableHeader));
529 scope = aml_scope("\\_SB");
530 acpi_dsdt_add_cpus(scope, guest_info->smp_cpus);
531 acpi_dsdt_add_uart(scope, &memmap[VIRT_UART],
532 (irqmap[VIRT_UART] + ARM_SPI_BASE));
533 acpi_dsdt_add_rtc(scope, &memmap[VIRT_RTC],
534 (irqmap[VIRT_RTC] + ARM_SPI_BASE));
535 acpi_dsdt_add_flash(scope, &memmap[VIRT_FLASH]);
536 acpi_dsdt_add_virtio(scope, &memmap[VIRT_MMIO],
537 (irqmap[VIRT_MMIO] + ARM_SPI_BASE), NUM_VIRTIO_TRANSPORTS);
538 acpi_dsdt_add_pci(scope, memmap, (irqmap[VIRT_PCIE] + ARM_SPI_BASE),
539 guest_info->use_highmem);
541 aml_append(dsdt, scope);
543 /* copy AML table into ACPI tables blob and patch header there */
544 g_array_append_vals(table_data, dsdt->buf->data, dsdt->buf->len);
545 build_header(linker, table_data,
546 (void *)(table_data->data + table_data->len - dsdt->buf->len),
547 "DSDT", dsdt->buf->len, 2);
548 free_aml_allocator();
551 typedef
552 struct AcpiBuildState {
553 /* Copy of table in RAM (for patching). */
554 MemoryRegion *table_mr;
555 MemoryRegion *rsdp_mr;
556 MemoryRegion *linker_mr;
557 /* Is table patched? */
558 bool patched;
559 VirtGuestInfo *guest_info;
560 } AcpiBuildState;
562 static
563 void virt_acpi_build(VirtGuestInfo *guest_info, AcpiBuildTables *tables)
565 GArray *table_offsets;
566 unsigned dsdt, rsdt;
567 VirtAcpiCpuInfo cpuinfo;
568 GArray *tables_blob = tables->table_data;
570 virt_acpi_get_cpu_info(&cpuinfo);
572 table_offsets = g_array_new(false, true /* clear */,
573 sizeof(uint32_t));
575 bios_linker_loader_alloc(tables->linker, ACPI_BUILD_TABLE_FILE,
576 64, false /* high memory */);
579 * The ACPI v5.1 tables for Hardware-reduced ACPI platform are:
580 * RSDP
581 * RSDT
582 * FADT
583 * GTDT
584 * MADT
585 * MCFG
586 * DSDT
589 /* DSDT is pointed to by FADT */
590 dsdt = tables_blob->len;
591 build_dsdt(tables_blob, tables->linker, guest_info);
593 /* FADT MADT GTDT MCFG SPCR pointed to by RSDT */
594 acpi_add_table(table_offsets, tables_blob);
595 build_fadt(tables_blob, tables->linker, dsdt);
597 acpi_add_table(table_offsets, tables_blob);
598 build_madt(tables_blob, tables->linker, guest_info, &cpuinfo);
600 acpi_add_table(table_offsets, tables_blob);
601 build_gtdt(tables_blob, tables->linker);
603 acpi_add_table(table_offsets, tables_blob);
604 build_mcfg(tables_blob, tables->linker, guest_info);
606 acpi_add_table(table_offsets, tables_blob);
607 build_spcr(tables_blob, tables->linker, guest_info);
609 /* RSDT is pointed to by RSDP */
610 rsdt = tables_blob->len;
611 build_rsdt(tables_blob, tables->linker, table_offsets);
613 /* RSDP is in FSEG memory, so allocate it separately */
614 build_rsdp(tables->rsdp, tables->linker, rsdt);
616 /* Cleanup memory that's no longer used. */
617 g_array_free(table_offsets, true);
620 static void acpi_ram_update(MemoryRegion *mr, GArray *data)
622 uint32_t size = acpi_data_len(data);
624 /* Make sure RAM size is correct - in case it got changed
625 * e.g. by migration */
626 memory_region_ram_resize(mr, size, &error_abort);
628 memcpy(memory_region_get_ram_ptr(mr), data->data, size);
629 memory_region_set_dirty(mr, 0, size);
632 static void virt_acpi_build_update(void *build_opaque, uint32_t offset)
634 AcpiBuildState *build_state = build_opaque;
635 AcpiBuildTables tables;
637 /* No state to update or already patched? Nothing to do. */
638 if (!build_state || build_state->patched) {
639 return;
641 build_state->patched = true;
643 acpi_build_tables_init(&tables);
645 virt_acpi_build(build_state->guest_info, &tables);
647 acpi_ram_update(build_state->table_mr, tables.table_data);
648 acpi_ram_update(build_state->rsdp_mr, tables.rsdp);
649 acpi_ram_update(build_state->linker_mr, tables.linker);
652 acpi_build_tables_cleanup(&tables, true);
655 static void virt_acpi_build_reset(void *build_opaque)
657 AcpiBuildState *build_state = build_opaque;
658 build_state->patched = false;
661 static MemoryRegion *acpi_add_rom_blob(AcpiBuildState *build_state,
662 GArray *blob, const char *name,
663 uint64_t max_size)
665 return rom_add_blob(name, blob->data, acpi_data_len(blob), max_size, -1,
666 name, virt_acpi_build_update, build_state);
669 static const VMStateDescription vmstate_virt_acpi_build = {
670 .name = "virt_acpi_build",
671 .version_id = 1,
672 .minimum_version_id = 1,
673 .fields = (VMStateField[]) {
674 VMSTATE_BOOL(patched, AcpiBuildState),
675 VMSTATE_END_OF_LIST()
679 void virt_acpi_setup(VirtGuestInfo *guest_info)
681 AcpiBuildTables tables;
682 AcpiBuildState *build_state;
684 if (!guest_info->fw_cfg) {
685 trace_virt_acpi_setup();
686 return;
689 if (!acpi_enabled) {
690 trace_virt_acpi_setup();
691 return;
694 build_state = g_malloc0(sizeof *build_state);
695 build_state->guest_info = guest_info;
697 acpi_build_tables_init(&tables);
698 virt_acpi_build(build_state->guest_info, &tables);
700 /* Now expose it all to Guest */
701 build_state->table_mr = acpi_add_rom_blob(build_state, tables.table_data,
702 ACPI_BUILD_TABLE_FILE,
703 ACPI_BUILD_TABLE_MAX_SIZE);
704 assert(build_state->table_mr != NULL);
706 build_state->linker_mr =
707 acpi_add_rom_blob(build_state, tables.linker, "etc/table-loader", 0);
709 fw_cfg_add_file(guest_info->fw_cfg, ACPI_BUILD_TPMLOG_FILE,
710 tables.tcpalog->data, acpi_data_len(tables.tcpalog));
712 build_state->rsdp_mr = acpi_add_rom_blob(build_state, tables.rsdp,
713 ACPI_BUILD_RSDP_FILE, 0);
715 qemu_register_reset(virt_acpi_build_reset, build_state);
716 virt_acpi_build_reset(build_state);
717 vmstate_register(NULL, 0, &vmstate_virt_acpi_build, build_state);
719 /* Cleanup tables but don't free the memory: we track it
720 * in build_state.
722 acpi_build_tables_cleanup(&tables, false);