1f5d8c8cdSShannon Zhao /* Support for generating ACPI tables and passing them to Guests
2f5d8c8cdSShannon Zhao *
3f5d8c8cdSShannon Zhao * ARM virt ACPI generation
4f5d8c8cdSShannon Zhao *
5f5d8c8cdSShannon Zhao * Copyright (C) 2008-2010 Kevin O'Connor <kevin@koconnor.net>
6f5d8c8cdSShannon Zhao * Copyright (C) 2006 Fabrice Bellard
7f5d8c8cdSShannon Zhao * Copyright (C) 2013 Red Hat Inc
8f5d8c8cdSShannon Zhao *
9f5d8c8cdSShannon Zhao * Author: Michael S. Tsirkin <mst@redhat.com>
10f5d8c8cdSShannon Zhao *
11f5d8c8cdSShannon Zhao * Copyright (c) 2015 HUAWEI TECHNOLOGIES CO.,LTD.
12f5d8c8cdSShannon Zhao *
13f5d8c8cdSShannon Zhao * Author: Shannon Zhao <zhaoshenglong@huawei.com>
14f5d8c8cdSShannon Zhao *
15f5d8c8cdSShannon Zhao * This program is free software; you can redistribute it and/or modify
16f5d8c8cdSShannon Zhao * it under the terms of the GNU General Public License as published by
17f5d8c8cdSShannon Zhao * the Free Software Foundation; either version 2 of the License, or
18f5d8c8cdSShannon Zhao * (at your option) any later version.
19f5d8c8cdSShannon Zhao
20f5d8c8cdSShannon Zhao * This program is distributed in the hope that it will be useful,
21f5d8c8cdSShannon Zhao * but WITHOUT ANY WARRANTY; without even the implied warranty of
22f5d8c8cdSShannon Zhao * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23f5d8c8cdSShannon Zhao * GNU General Public License for more details.
24f5d8c8cdSShannon Zhao
25f5d8c8cdSShannon Zhao * You should have received a copy of the GNU General Public License along
26f5d8c8cdSShannon Zhao * with this program; if not, see <http://www.gnu.org/licenses/>.
27f5d8c8cdSShannon Zhao */
28f5d8c8cdSShannon Zhao
2912b16722SPeter Maydell #include "qemu/osdep.h"
30da34e65cSMarkus Armbruster #include "qapi/error.h"
31f5d8c8cdSShannon Zhao #include "qemu/bitmap.h"
3213a63743SPeng Liang #include "qemu/error-report.h"
33f5d8c8cdSShannon Zhao #include "trace.h"
342e5b09fdSMarkus Armbruster #include "hw/core/cpu.h"
35f5d8c8cdSShannon Zhao #include "hw/acpi/acpi-defs.h"
36f5d8c8cdSShannon Zhao #include "hw/acpi/acpi.h"
374c7f4f4fSSunil V L #include "hw/nvram/fw_cfg_acpi.h"
38f5d8c8cdSShannon Zhao #include "hw/acpi/bios-linker-loader.h"
39f5d8c8cdSShannon Zhao #include "hw/acpi/aml-build.h"
4082f76c67SWei Yang #include "hw/acpi/utils.h"
4148cefd94SWei Yang #include "hw/acpi/pci.h"
42cff51ac9SShameer Kolothum #include "hw/acpi/memory_hotplug.h"
43cff51ac9SShameer Kolothum #include "hw/acpi/generic_event_device.h"
4480bde693SEric Auger #include "hw/acpi/tpm.h"
457cbd3fd3SXiang Chen #include "hw/acpi/hmat.h"
4684344884SShannon Zhao #include "hw/pci/pcie_host.h"
47d4e5de1aSShannon Zhao #include "hw/pci/pci.h"
4842e0f050SXingang Wang #include "hw/pci/pci_bus.h"
4906d2dd49SGerd Hoffmann #include "hw/pci-host/gpex.h"
50d05fdab4SAndrew Jones #include "hw/arm/virt.h"
510c40daf0SPhilippe Mathieu-Daudé #include "hw/intc/arm_gicv3_its_common.h"
52b5a60beeSKwangwoo Lee #include "hw/mem/nvdimm.h"
535ab540e9SEric Auger #include "hw/platform-bus.h"
542b302e1eSShannon Zhao #include "sysemu/numa.h"
5571e8a915SMarkus Armbruster #include "sysemu/reset.h"
5680bde693SEric Auger #include "sysemu/tpm.h"
57d6454270SMarkus Armbruster #include "migration/vmstate.h"
58aa16508fSDongjiu Geng #include "hw/acpi/ghes.h"
59cf1a5cc9SJean-Philippe Brucker #include "hw/acpi/viot.h"
6057ba8436SSunil V L #include "hw/virtio/virtio-acpi.h"
61e2d8cf9bSPhilippe Mathieu-Daudé #include "target/arm/multiprocessing.h"
62f5d8c8cdSShannon Zhao
63dfccd8cfSShannon Zhao #define ARM_SPI_BASE 32
64dfccd8cfSShannon Zhao
65451b1570SYubo Miao #define ACPI_BUILD_TABLE_SIZE 0x20000
66451b1570SYubo Miao
acpi_dsdt_add_cpus(Aml * scope,VirtMachineState * vms)679cd07db9SAndrew Jones static void acpi_dsdt_add_cpus(Aml *scope, VirtMachineState *vms)
68dfccd8cfSShannon Zhao {
699cd07db9SAndrew Jones MachineState *ms = MACHINE(vms);
70dfccd8cfSShannon Zhao uint16_t i;
71dfccd8cfSShannon Zhao
729cd07db9SAndrew Jones for (i = 0; i < ms->smp.cpus; i++) {
73f460be43SWei Huang Aml *dev = aml_device("C%.03X", i);
74dfccd8cfSShannon Zhao aml_append(dev, aml_name_decl("_HID", aml_string("ACPI0007")));
75dfccd8cfSShannon Zhao aml_append(dev, aml_name_decl("_UID", aml_int(i)));
76dfccd8cfSShannon Zhao aml_append(scope, dev);
77dfccd8cfSShannon Zhao }
78dfccd8cfSShannon Zhao }
79dfccd8cfSShannon Zhao
acpi_dsdt_add_uart(Aml * scope,const MemMapEntry * uart_memmap,uint32_t uart_irq,int uartidx)80dfccd8cfSShannon Zhao static void acpi_dsdt_add_uart(Aml *scope, const MemMapEntry *uart_memmap,
81e7100972SPeter Maydell uint32_t uart_irq, int uartidx)
82dfccd8cfSShannon Zhao {
83e7100972SPeter Maydell Aml *dev = aml_device("COM%d", uartidx);
84dfccd8cfSShannon Zhao aml_append(dev, aml_name_decl("_HID", aml_string("ARMH0011")));
85e7100972SPeter Maydell aml_append(dev, aml_name_decl("_UID", aml_int(uartidx)));
86dfccd8cfSShannon Zhao
87dfccd8cfSShannon Zhao Aml *crs = aml_resource_template();
88dfccd8cfSShannon Zhao aml_append(crs, aml_memory32_fixed(uart_memmap->base,
89dfccd8cfSShannon Zhao uart_memmap->size, AML_READ_WRITE));
90dfccd8cfSShannon Zhao aml_append(crs,
91dfccd8cfSShannon Zhao aml_interrupt(AML_CONSUMER, AML_LEVEL, AML_ACTIVE_HIGH,
9245fcf539SIgor Mammedov AML_EXCLUSIVE, &uart_irq, 1));
93dfccd8cfSShannon Zhao aml_append(dev, aml_name_decl("_CRS", crs));
94f264d51dSAndrew Jones
95dfccd8cfSShannon Zhao aml_append(scope, dev);
96dfccd8cfSShannon Zhao }
97dfccd8cfSShannon Zhao
acpi_dsdt_add_flash(Aml * scope,const MemMapEntry * flash_memmap)98dfccd8cfSShannon Zhao static void acpi_dsdt_add_flash(Aml *scope, const MemMapEntry *flash_memmap)
99dfccd8cfSShannon Zhao {
100dfccd8cfSShannon Zhao Aml *dev, *crs;
101dfccd8cfSShannon Zhao hwaddr base = flash_memmap->base;
102cd37aaf8SShannon Zhao hwaddr size = flash_memmap->size / 2;
103dfccd8cfSShannon Zhao
104dfccd8cfSShannon Zhao dev = aml_device("FLS0");
105dfccd8cfSShannon Zhao aml_append(dev, aml_name_decl("_HID", aml_string("LNRO0015")));
106dfccd8cfSShannon Zhao aml_append(dev, aml_name_decl("_UID", aml_int(0)));
107dfccd8cfSShannon Zhao
108dfccd8cfSShannon Zhao crs = aml_resource_template();
109dfccd8cfSShannon Zhao aml_append(crs, aml_memory32_fixed(base, size, AML_READ_WRITE));
110dfccd8cfSShannon Zhao aml_append(dev, aml_name_decl("_CRS", crs));
111dfccd8cfSShannon Zhao aml_append(scope, dev);
112dfccd8cfSShannon Zhao
113dfccd8cfSShannon Zhao dev = aml_device("FLS1");
114dfccd8cfSShannon Zhao aml_append(dev, aml_name_decl("_HID", aml_string("LNRO0015")));
115dfccd8cfSShannon Zhao aml_append(dev, aml_name_decl("_UID", aml_int(1)));
116dfccd8cfSShannon Zhao crs = aml_resource_template();
117dfccd8cfSShannon Zhao aml_append(crs, aml_memory32_fixed(base + size, size, AML_READ_WRITE));
118dfccd8cfSShannon Zhao aml_append(dev, aml_name_decl("_CRS", crs));
119dfccd8cfSShannon Zhao aml_append(scope, dev);
120dfccd8cfSShannon Zhao }
121dfccd8cfSShannon Zhao
acpi_dsdt_add_pci(Aml * scope,const MemMapEntry * memmap,uint32_t irq,VirtMachineState * vms)12245fcf539SIgor Mammedov static void acpi_dsdt_add_pci(Aml *scope, const MemMapEntry *memmap,
123c8f008c4SMarc Zyngier uint32_t irq, VirtMachineState *vms)
124d4e5de1aSShannon Zhao {
125c8f008c4SMarc Zyngier int ecam_id = VIRT_ECAM_ID(vms->highmem_ecam);
12606d2dd49SGerd Hoffmann struct GPEXConfig cfg = {
12706d2dd49SGerd Hoffmann .mmio32 = memmap[VIRT_PCIE_MMIO],
12806d2dd49SGerd Hoffmann .pio = memmap[VIRT_PCIE_PIO],
12906d2dd49SGerd Hoffmann .ecam = memmap[ecam_id],
13006d2dd49SGerd Hoffmann .irq = irq,
1316f9765fbSYubo Miao .bus = vms->bus,
13206d2dd49SGerd Hoffmann };
133d4e5de1aSShannon Zhao
134c8f008c4SMarc Zyngier if (vms->highmem_mmio) {
13506d2dd49SGerd Hoffmann cfg.mmio64 = memmap[VIRT_HIGH_PCIE_MMIO];
1365125f9cdSPavel Fedin }
1375125f9cdSPavel Fedin
13806d2dd49SGerd Hoffmann acpi_dsdt_add_gpex(scope, &cfg);
139d4e5de1aSShannon Zhao }
140d4e5de1aSShannon Zhao
acpi_dsdt_add_gpio(Aml * scope,const MemMapEntry * gpio_memmap,uint32_t gpio_irq)141aeb1a36dSShannon Zhao static void acpi_dsdt_add_gpio(Aml *scope, const MemMapEntry *gpio_memmap,
142aeb1a36dSShannon Zhao uint32_t gpio_irq)
143aeb1a36dSShannon Zhao {
144aeb1a36dSShannon Zhao Aml *dev = aml_device("GPO0");
145aeb1a36dSShannon Zhao aml_append(dev, aml_name_decl("_HID", aml_string("ARMH0061")));
146aeb1a36dSShannon Zhao aml_append(dev, aml_name_decl("_UID", aml_int(0)));
147aeb1a36dSShannon Zhao
148aeb1a36dSShannon Zhao Aml *crs = aml_resource_template();
149aeb1a36dSShannon Zhao aml_append(crs, aml_memory32_fixed(gpio_memmap->base, gpio_memmap->size,
150aeb1a36dSShannon Zhao AML_READ_WRITE));
151aeb1a36dSShannon Zhao aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL, AML_ACTIVE_HIGH,
152aeb1a36dSShannon Zhao AML_EXCLUSIVE, &gpio_irq, 1));
153aeb1a36dSShannon Zhao aml_append(dev, aml_name_decl("_CRS", crs));
154c1a158b7SShannon Zhao
155c1a158b7SShannon Zhao Aml *aei = aml_resource_template();
156ed5031adSMauro Carvalho Chehab
157ed5031adSMauro Carvalho Chehab const uint32_t pin = GPIO_PIN_POWER_BUTTON;
158c1a158b7SShannon Zhao aml_append(aei, aml_gpio_int(AML_CONSUMER, AML_EDGE, AML_ACTIVE_HIGH,
159ed5031adSMauro Carvalho Chehab AML_EXCLUSIVE, AML_PULL_UP, 0, &pin, 1,
160c1a158b7SShannon Zhao "GPO0", NULL, 0));
161c1a158b7SShannon Zhao aml_append(dev, aml_name_decl("_AEI", aei));
162c1a158b7SShannon Zhao
163c1a158b7SShannon Zhao /* _E03 is handle for power button */
164c1a158b7SShannon Zhao Aml *method = aml_method("_E03", 0, AML_NOTSERIALIZED);
165c1a158b7SShannon Zhao aml_append(method, aml_notify(aml_name(ACPI_POWER_BUTTON_DEVICE),
166c1a158b7SShannon Zhao aml_int(0x80)));
167c1a158b7SShannon Zhao aml_append(dev, method);
168aeb1a36dSShannon Zhao aml_append(scope, dev);
169aeb1a36dSShannon Zhao }
170aeb1a36dSShannon Zhao
171f50be48aSStefan Berger #ifdef CONFIG_TPM
acpi_dsdt_add_tpm(Aml * scope,VirtMachineState * vms)1725ab540e9SEric Auger static void acpi_dsdt_add_tpm(Aml *scope, VirtMachineState *vms)
1735ab540e9SEric Auger {
1745ab540e9SEric Auger PlatformBusDevice *pbus = PLATFORM_BUS_DEVICE(vms->platform_bus_dev);
1755ab540e9SEric Auger hwaddr pbus_base = vms->memmap[VIRT_PLATFORM_BUS].base;
1765ab540e9SEric Auger SysBusDevice *sbdev = SYS_BUS_DEVICE(tpm_find());
1775ab540e9SEric Auger MemoryRegion *sbdev_mr;
1785ab540e9SEric Auger hwaddr tpm_base;
1795ab540e9SEric Auger
1805ab540e9SEric Auger if (!sbdev) {
1815ab540e9SEric Auger return;
1825ab540e9SEric Auger }
1835ab540e9SEric Auger
1845ab540e9SEric Auger tpm_base = platform_bus_get_mmio_addr(pbus, sbdev, 0);
1855ab540e9SEric Auger assert(tpm_base != -1);
1865ab540e9SEric Auger
1875ab540e9SEric Auger tpm_base += pbus_base;
1885ab540e9SEric Auger
1895ab540e9SEric Auger sbdev_mr = sysbus_mmio_get_region(sbdev, 0);
1905ab540e9SEric Auger
1915ab540e9SEric Auger Aml *dev = aml_device("TPM0");
1925ab540e9SEric Auger aml_append(dev, aml_name_decl("_HID", aml_string("MSFT0101")));
1935903646dSStefan Berger aml_append(dev, aml_name_decl("_STR", aml_string("TPM 2.0 Device")));
1945ab540e9SEric Auger aml_append(dev, aml_name_decl("_UID", aml_int(0)));
1955ab540e9SEric Auger
1965ab540e9SEric Auger Aml *crs = aml_resource_template();
1975ab540e9SEric Auger aml_append(crs,
1985ab540e9SEric Auger aml_memory32_fixed(tpm_base,
1995ab540e9SEric Auger (uint32_t)memory_region_size(sbdev_mr),
2005ab540e9SEric Auger AML_READ_WRITE));
2015ab540e9SEric Auger aml_append(dev, aml_name_decl("_CRS", crs));
2025ab540e9SEric Auger aml_append(scope, dev);
2035ab540e9SEric Auger }
204f50be48aSStefan Berger #endif
2055ab540e9SEric Auger
206271cbb2fSIgor Mammedov #define ID_MAPPING_ENTRY_SIZE 20
2071c2cb7e0SEric Auger #define SMMU_V3_ENTRY_SIZE 68
2081c2cb7e0SEric Auger #define ROOT_COMPLEX_ENTRY_SIZE 36
209271cbb2fSIgor Mammedov #define IORT_NODE_OFFSET 48
210271cbb2fSIgor Mammedov
2115786827fSNicolin Chen /*
2125786827fSNicolin Chen * Append an ID mapping entry as described by "Table 4 ID mapping format" in
2135786827fSNicolin Chen * "IO Remapping Table System Software on ARM Platforms", Chapter 3.
2145786827fSNicolin Chen * Document number: ARM DEN 0049E.f, Apr 2024
2155786827fSNicolin Chen *
2165786827fSNicolin Chen * Note that @id_count gets internally subtracted by one, following the spec.
2175786827fSNicolin Chen */
build_iort_id_mapping(GArray * table_data,uint32_t input_base,uint32_t id_count,uint32_t out_ref)218271cbb2fSIgor Mammedov static void build_iort_id_mapping(GArray *table_data, uint32_t input_base,
219271cbb2fSIgor Mammedov uint32_t id_count, uint32_t out_ref)
220271cbb2fSIgor Mammedov {
221271cbb2fSIgor Mammedov build_append_int_noprefix(table_data, input_base, 4); /* Input base */
2225786827fSNicolin Chen /* Number of IDs - The number of IDs in the range minus one */
2235786827fSNicolin Chen build_append_int_noprefix(table_data, id_count - 1, 4);
224271cbb2fSIgor Mammedov build_append_int_noprefix(table_data, input_base, 4); /* Output base */
225271cbb2fSIgor Mammedov build_append_int_noprefix(table_data, out_ref, 4); /* Output Reference */
2261c2cb7e0SEric Auger /* Flags */
2271c2cb7e0SEric Auger build_append_int_noprefix(table_data, 0 /* Single mapping (disabled) */, 4);
228271cbb2fSIgor Mammedov }
229271cbb2fSIgor Mammedov
230271cbb2fSIgor Mammedov struct AcpiIortIdMapping {
231271cbb2fSIgor Mammedov uint32_t input_base;
232271cbb2fSIgor Mammedov uint32_t id_count;
233271cbb2fSIgor Mammedov };
234271cbb2fSIgor Mammedov typedef struct AcpiIortIdMapping AcpiIortIdMapping;
235271cbb2fSIgor Mammedov
23642e0f050SXingang Wang /* Build the iort ID mapping to SMMUv3 for a given PCI host bridge */
23742e0f050SXingang Wang static int
iort_host_bridges(Object * obj,void * opaque)23842e0f050SXingang Wang iort_host_bridges(Object *obj, void *opaque)
23942e0f050SXingang Wang {
24042e0f050SXingang Wang GArray *idmap_blob = opaque;
24142e0f050SXingang Wang
24242e0f050SXingang Wang if (object_dynamic_cast(obj, TYPE_PCI_HOST_BRIDGE)) {
24342e0f050SXingang Wang PCIBus *bus = PCI_HOST_BRIDGE(obj)->bus;
24442e0f050SXingang Wang
24542e0f050SXingang Wang if (bus && !pci_bus_bypass_iommu(bus)) {
24642e0f050SXingang Wang int min_bus, max_bus;
24742e0f050SXingang Wang
24842e0f050SXingang Wang pci_bus_range(bus, &min_bus, &max_bus);
24942e0f050SXingang Wang
25042e0f050SXingang Wang AcpiIortIdMapping idmap = {
25142e0f050SXingang Wang .input_base = min_bus << 8,
25242e0f050SXingang Wang .id_count = (max_bus - min_bus + 1) << 8,
25342e0f050SXingang Wang };
25442e0f050SXingang Wang g_array_append_val(idmap_blob, idmap);
25542e0f050SXingang Wang }
25642e0f050SXingang Wang }
25742e0f050SXingang Wang
25842e0f050SXingang Wang return 0;
25942e0f050SXingang Wang }
26042e0f050SXingang Wang
iort_idmap_compare(gconstpointer a,gconstpointer b)26142e0f050SXingang Wang static int iort_idmap_compare(gconstpointer a, gconstpointer b)
26242e0f050SXingang Wang {
26342e0f050SXingang Wang AcpiIortIdMapping *idmap_a = (AcpiIortIdMapping *)a;
26442e0f050SXingang Wang AcpiIortIdMapping *idmap_b = (AcpiIortIdMapping *)b;
26542e0f050SXingang Wang
26642e0f050SXingang Wang return idmap_a->input_base - idmap_b->input_base;
26742e0f050SXingang Wang }
26842e0f050SXingang Wang
2693548494eSIgor Mammedov /*
2703548494eSIgor Mammedov * Input Output Remapping Table (IORT)
2713548494eSIgor Mammedov * Conforms to "IO Remapping Table System Software on ARM Platforms",
2721c2cb7e0SEric Auger * Document number: ARM DEN 0049E.b, Feb 2021
2733548494eSIgor Mammedov */
27484344884SShannon Zhao static void
build_iort(GArray * table_data,BIOSLinker * linker,VirtMachineState * vms)275a703b4f6SPrem Mallappa build_iort(GArray *table_data, BIOSLinker *linker, VirtMachineState *vms)
276e78f1222SPrem Mallappa {
2773548494eSIgor Mammedov int i, nb_nodes, rc_mapping_count;
2783548494eSIgor Mammedov size_t node_size, smmu_offset = 0;
279271cbb2fSIgor Mammedov AcpiIortIdMapping *idmap;
2801c2cb7e0SEric Auger uint32_t id = 0;
28142e0f050SXingang Wang GArray *smmu_idmaps = g_array_new(false, true, sizeof(AcpiIortIdMapping));
28242e0f050SXingang Wang GArray *its_idmaps = g_array_new(false, true, sizeof(AcpiIortIdMapping));
283e78f1222SPrem Mallappa
2841c2cb7e0SEric Auger AcpiTable table = { .sig = "IORT", .rev = 3, .oem_id = vms->oem_id,
2853548494eSIgor Mammedov .oem_table_id = vms->oem_table_id };
286271cbb2fSIgor Mammedov /* Table 2 The IORT */
287271cbb2fSIgor Mammedov acpi_table_begin(&table, table_data);
288e78f1222SPrem Mallappa
289a703b4f6SPrem Mallappa if (vms->iommu == VIRT_IOMMU_SMMUV3) {
29042e0f050SXingang Wang AcpiIortIdMapping next_range = {0};
29142e0f050SXingang Wang
29242e0f050SXingang Wang object_child_foreach_recursive(object_get_root(),
29342e0f050SXingang Wang iort_host_bridges, smmu_idmaps);
29442e0f050SXingang Wang
29542e0f050SXingang Wang /* Sort the smmu idmap by input_base */
29642e0f050SXingang Wang g_array_sort(smmu_idmaps, iort_idmap_compare);
29742e0f050SXingang Wang
29842e0f050SXingang Wang /*
29942e0f050SXingang Wang * Split the whole RIDs by mapping from RC to SMMU,
30042e0f050SXingang Wang * build the ID mapping from RC to ITS directly.
30142e0f050SXingang Wang */
30242e0f050SXingang Wang for (i = 0; i < smmu_idmaps->len; i++) {
30342e0f050SXingang Wang idmap = &g_array_index(smmu_idmaps, AcpiIortIdMapping, i);
30442e0f050SXingang Wang
30542e0f050SXingang Wang if (next_range.input_base < idmap->input_base) {
30642e0f050SXingang Wang next_range.id_count = idmap->input_base - next_range.input_base;
30742e0f050SXingang Wang g_array_append_val(its_idmaps, next_range);
30842e0f050SXingang Wang }
30942e0f050SXingang Wang
31042e0f050SXingang Wang next_range.input_base = idmap->input_base + idmap->id_count;
31142e0f050SXingang Wang }
31242e0f050SXingang Wang
31342e0f050SXingang Wang /* Append the last RC -> ITS ID mapping */
3145786827fSNicolin Chen if (next_range.input_base < 0x10000) {
3155786827fSNicolin Chen next_range.id_count = 0x10000 - next_range.input_base;
31642e0f050SXingang Wang g_array_append_val(its_idmaps, next_range);
31742e0f050SXingang Wang }
31842e0f050SXingang Wang
319a703b4f6SPrem Mallappa nb_nodes = 3; /* RC, ITS, SMMUv3 */
32042e0f050SXingang Wang rc_mapping_count = smmu_idmaps->len + its_idmaps->len;
321a703b4f6SPrem Mallappa } else {
322a703b4f6SPrem Mallappa nb_nodes = 2; /* RC, ITS */
32342e0f050SXingang Wang rc_mapping_count = 1;
324a703b4f6SPrem Mallappa }
3253548494eSIgor Mammedov /* Number of IORT Nodes */
3263548494eSIgor Mammedov build_append_int_noprefix(table_data, nb_nodes, 4);
327271cbb2fSIgor Mammedov
3283548494eSIgor Mammedov /* Offset to Array of IORT Nodes */
329271cbb2fSIgor Mammedov build_append_int_noprefix(table_data, IORT_NODE_OFFSET, 4);
3303548494eSIgor Mammedov build_append_int_noprefix(table_data, 0, 4); /* Reserved */
331e78f1222SPrem Mallappa
3321c2cb7e0SEric Auger /* Table 12 ITS Group Format */
333271cbb2fSIgor Mammedov build_append_int_noprefix(table_data, 0 /* ITS Group */, 1); /* Type */
334271cbb2fSIgor Mammedov node_size = 20 /* fixed header size */ + 4 /* 1 GIC ITS Identifier */;
335271cbb2fSIgor Mammedov build_append_int_noprefix(table_data, node_size, 2); /* Length */
3361c2cb7e0SEric Auger build_append_int_noprefix(table_data, 1, 1); /* Revision */
3371c2cb7e0SEric Auger build_append_int_noprefix(table_data, id++, 4); /* Identifier */
338271cbb2fSIgor Mammedov build_append_int_noprefix(table_data, 0, 4); /* Number of ID mappings */
339271cbb2fSIgor Mammedov build_append_int_noprefix(table_data, 0, 4); /* Reference to ID Array */
340271cbb2fSIgor Mammedov build_append_int_noprefix(table_data, 1, 4); /* Number of ITSs */
341271cbb2fSIgor Mammedov /* GIC ITS Identifier Array */
342271cbb2fSIgor Mammedov build_append_int_noprefix(table_data, 0 /* MADT translation_id */, 4);
343e78f1222SPrem Mallappa
344a703b4f6SPrem Mallappa if (vms->iommu == VIRT_IOMMU_SMMUV3) {
34541c4fb94SEric Auger int irq = vms->irqmap[VIRT_SMMU] + ARM_SPI_BASE;
346a703b4f6SPrem Mallappa
347271cbb2fSIgor Mammedov smmu_offset = table_data->len - table.table_offset;
3481c2cb7e0SEric Auger /* Table 9 SMMUv3 Format */
349271cbb2fSIgor Mammedov build_append_int_noprefix(table_data, 4 /* SMMUv3 */, 1); /* Type */
350271cbb2fSIgor Mammedov node_size = SMMU_V3_ENTRY_SIZE + ID_MAPPING_ENTRY_SIZE;
351271cbb2fSIgor Mammedov build_append_int_noprefix(table_data, node_size, 2); /* Length */
3521c2cb7e0SEric Auger build_append_int_noprefix(table_data, 4, 1); /* Revision */
3531c2cb7e0SEric Auger build_append_int_noprefix(table_data, id++, 4); /* Identifier */
354271cbb2fSIgor Mammedov build_append_int_noprefix(table_data, 1, 4); /* Number of ID mappings */
355271cbb2fSIgor Mammedov /* Reference to ID Array */
356271cbb2fSIgor Mammedov build_append_int_noprefix(table_data, SMMU_V3_ENTRY_SIZE, 4);
357271cbb2fSIgor Mammedov /* Base address */
358271cbb2fSIgor Mammedov build_append_int_noprefix(table_data, vms->memmap[VIRT_SMMU].base, 8);
359271cbb2fSIgor Mammedov /* Flags */
3601c2cb7e0SEric Auger build_append_int_noprefix(table_data, 1 /* COHACC Override */, 4);
361271cbb2fSIgor Mammedov build_append_int_noprefix(table_data, 0, 4); /* Reserved */
362271cbb2fSIgor Mammedov build_append_int_noprefix(table_data, 0, 8); /* VATOS address */
363271cbb2fSIgor Mammedov /* Model */
364271cbb2fSIgor Mammedov build_append_int_noprefix(table_data, 0 /* Generic SMMU-v3 */, 4);
365271cbb2fSIgor Mammedov build_append_int_noprefix(table_data, irq, 4); /* Event */
366271cbb2fSIgor Mammedov build_append_int_noprefix(table_data, irq + 1, 4); /* PRI */
367271cbb2fSIgor Mammedov build_append_int_noprefix(table_data, irq + 3, 4); /* GERR */
368271cbb2fSIgor Mammedov build_append_int_noprefix(table_data, irq + 2, 4); /* Sync */
3691c2cb7e0SEric Auger build_append_int_noprefix(table_data, 0, 4); /* Proximity domain */
3701c2cb7e0SEric Auger /* DeviceID mapping index (ignored since interrupts are GSIV based) */
3711c2cb7e0SEric Auger build_append_int_noprefix(table_data, 0, 4);
372a703b4f6SPrem Mallappa
373a703b4f6SPrem Mallappa /* output IORT node is the ITS group node (the first node) */
3745786827fSNicolin Chen build_iort_id_mapping(table_data, 0, 0x10000, IORT_NODE_OFFSET);
375a703b4f6SPrem Mallappa }
376a703b4f6SPrem Mallappa
3771c2cb7e0SEric Auger /* Table 17 Root Complex Node */
378271cbb2fSIgor Mammedov build_append_int_noprefix(table_data, 2 /* Root complex */, 1); /* Type */
379271cbb2fSIgor Mammedov node_size = ROOT_COMPLEX_ENTRY_SIZE +
380271cbb2fSIgor Mammedov ID_MAPPING_ENTRY_SIZE * rc_mapping_count;
381271cbb2fSIgor Mammedov build_append_int_noprefix(table_data, node_size, 2); /* Length */
3821c2cb7e0SEric Auger build_append_int_noprefix(table_data, 3, 1); /* Revision */
3831c2cb7e0SEric Auger build_append_int_noprefix(table_data, id++, 4); /* Identifier */
384271cbb2fSIgor Mammedov /* Number of ID mappings */
385271cbb2fSIgor Mammedov build_append_int_noprefix(table_data, rc_mapping_count, 4);
386271cbb2fSIgor Mammedov /* Reference to ID Array */
387271cbb2fSIgor Mammedov build_append_int_noprefix(table_data, ROOT_COMPLEX_ENTRY_SIZE, 4);
388e78f1222SPrem Mallappa
3891c2cb7e0SEric Auger /* Table 14 Memory access properties */
390271cbb2fSIgor Mammedov /* CCA: Cache Coherent Attribute */
391271cbb2fSIgor Mammedov build_append_int_noprefix(table_data, 1 /* fully coherent */, 4);
392271cbb2fSIgor Mammedov build_append_int_noprefix(table_data, 0, 1); /* AH: Note Allocation Hints */
393271cbb2fSIgor Mammedov build_append_int_noprefix(table_data, 0, 2); /* Reserved */
3941c2cb7e0SEric Auger /* Table 15 Memory Access Flags */
3951c2cb7e0SEric Auger build_append_int_noprefix(table_data, 0x3 /* CCA = CPM = DACS = 1 */, 1);
396e78f1222SPrem Mallappa
397271cbb2fSIgor Mammedov build_append_int_noprefix(table_data, 0, 4); /* ATS Attribute */
398271cbb2fSIgor Mammedov /* MCFG pci_segment */
399271cbb2fSIgor Mammedov build_append_int_noprefix(table_data, 0, 4); /* PCI Segment number */
400e78f1222SPrem Mallappa
4011c2cb7e0SEric Auger /* Memory address size limit */
4021c2cb7e0SEric Auger build_append_int_noprefix(table_data, 64, 1);
4031c2cb7e0SEric Auger
4041c2cb7e0SEric Auger build_append_int_noprefix(table_data, 0, 3); /* Reserved */
4051c2cb7e0SEric Auger
406271cbb2fSIgor Mammedov /* Output Reference */
407a703b4f6SPrem Mallappa if (vms->iommu == VIRT_IOMMU_SMMUV3) {
40842e0f050SXingang Wang AcpiIortIdMapping *range;
40942e0f050SXingang Wang
41042e0f050SXingang Wang /* translated RIDs connect to SMMUv3 node: RC -> SMMUv3 -> ITS */
41142e0f050SXingang Wang for (i = 0; i < smmu_idmaps->len; i++) {
41242e0f050SXingang Wang range = &g_array_index(smmu_idmaps, AcpiIortIdMapping, i);
413a703b4f6SPrem Mallappa /* output IORT node is the smmuv3 node */
414271cbb2fSIgor Mammedov build_iort_id_mapping(table_data, range->input_base,
415271cbb2fSIgor Mammedov range->id_count, smmu_offset);
41642e0f050SXingang Wang }
41742e0f050SXingang Wang
41842e0f050SXingang Wang /* bypassed RIDs connect to ITS group node directly: RC -> ITS */
41942e0f050SXingang Wang for (i = 0; i < its_idmaps->len; i++) {
42042e0f050SXingang Wang range = &g_array_index(its_idmaps, AcpiIortIdMapping, i);
421e78f1222SPrem Mallappa /* output IORT node is the ITS group node (the first node) */
422271cbb2fSIgor Mammedov build_iort_id_mapping(table_data, range->input_base,
423e9fd8277SNicolin Chen range->id_count, IORT_NODE_OFFSET);
424a703b4f6SPrem Mallappa }
42542e0f050SXingang Wang } else {
42642e0f050SXingang Wang /* output IORT node is the ITS group node (the first node) */
4275786827fSNicolin Chen build_iort_id_mapping(table_data, 0, 0x10000, IORT_NODE_OFFSET);
42842e0f050SXingang Wang }
42942e0f050SXingang Wang
4303548494eSIgor Mammedov acpi_table_end(linker, &table);
43142e0f050SXingang Wang g_array_free(smmu_idmaps, true);
43242e0f050SXingang Wang g_array_free(its_idmaps, true);
433e78f1222SPrem Mallappa }
434e78f1222SPrem Mallappa
435a86d86acSIgor Mammedov /*
436a86d86acSIgor Mammedov * Serial Port Console Redirection Table (SPCR)
437a86d86acSIgor Mammedov * Rev: 1.07
438a86d86acSIgor Mammedov */
439e78f1222SPrem Mallappa static void
spcr_setup(GArray * table_data,BIOSLinker * linker,VirtMachineState * vms)4407dd0b070SSia Jee Heng spcr_setup(GArray *table_data, BIOSLinker *linker, VirtMachineState *vms)
441f264d51dSAndrew Jones {
4427dd0b070SSia Jee Heng AcpiSpcrData serial = {
4437dd0b070SSia Jee Heng .interface_type = 3, /* ARM PL011 UART */
4447dd0b070SSia Jee Heng .base_addr.id = AML_AS_SYSTEM_MEMORY,
4457dd0b070SSia Jee Heng .base_addr.width = 32,
4467dd0b070SSia Jee Heng .base_addr.offset = 0,
4477dd0b070SSia Jee Heng .base_addr.size = 3,
448fe22cba9SPeter Maydell .base_addr.addr = vms->memmap[VIRT_UART0].base,
4497dd0b070SSia Jee Heng .interrupt_type = (1 << 3),/* Bit[3] ARMH GIC interrupt*/
4507dd0b070SSia Jee Heng .pc_interrupt = 0, /* IRQ */
451fe22cba9SPeter Maydell .interrupt = (vms->irqmap[VIRT_UART0] + ARM_SPI_BASE),
4527dd0b070SSia Jee Heng .baud_rate = 3, /* 9600 */
4537dd0b070SSia Jee Heng .parity = 0, /* No Parity */
4547dd0b070SSia Jee Heng .stop_bits = 1, /* 1 Stop bit */
4557dd0b070SSia Jee Heng .flow_control = 1 << 1, /* RTS/CTS hardware flow control */
4567dd0b070SSia Jee Heng .terminal_type = 0, /* VT100 */
4577dd0b070SSia Jee Heng .language = 0, /* Language */
4587dd0b070SSia Jee Heng .pci_device_id = 0xffff, /* not a PCI device*/
4597dd0b070SSia Jee Heng .pci_vendor_id = 0xffff, /* not a PCI device*/
4607dd0b070SSia Jee Heng .pci_bus = 0,
4617dd0b070SSia Jee Heng .pci_device = 0,
4627dd0b070SSia Jee Heng .pci_function = 0,
4637dd0b070SSia Jee Heng .pci_flags = 0,
4647dd0b070SSia Jee Heng .pci_segment = 0,
4657dd0b070SSia Jee Heng };
466f264d51dSAndrew Jones
4677dd0b070SSia Jee Heng build_spcr(table_data, linker, &serial, 2, vms->oem_id, vms->oem_table_id);
468f264d51dSAndrew Jones }
469f264d51dSAndrew Jones
470e5b6d55aSIgor Mammedov /*
471e5b6d55aSIgor Mammedov * ACPI spec, Revision 5.1
472e5b6d55aSIgor Mammedov * 5.2.16 System Resource Affinity Table (SRAT)
473e5b6d55aSIgor Mammedov */
474f264d51dSAndrew Jones static void
build_srat(GArray * table_data,BIOSLinker * linker,VirtMachineState * vms)475da4f09a7SAndrew Jones build_srat(GArray *table_data, BIOSLinker *linker, VirtMachineState *vms)
4762b302e1eSShannon Zhao {
477255bf20fSIgor Mammedov int i;
4782b302e1eSShannon Zhao uint64_t mem_base;
4794ccf5826SIgor Mammedov MachineClass *mc = MACHINE_GET_CLASS(vms);
480aa570207STao Xu MachineState *ms = MACHINE(vms);
481aa570207STao Xu const CPUArchIdList *cpu_list = mc->possible_cpu_arch_ids(ms);
482255bf20fSIgor Mammedov AcpiTable table = { .sig = "SRAT", .rev = 3, .oem_id = vms->oem_id,
483255bf20fSIgor Mammedov .oem_table_id = vms->oem_table_id };
4842b302e1eSShannon Zhao
485255bf20fSIgor Mammedov acpi_table_begin(&table, table_data);
486255bf20fSIgor Mammedov build_append_int_noprefix(table_data, 1, 4); /* Reserved */
487255bf20fSIgor Mammedov build_append_int_noprefix(table_data, 0, 8); /* Reserved */
4882b302e1eSShannon Zhao
4894ccf5826SIgor Mammedov for (i = 0; i < cpu_list->len; ++i) {
490e5b6d55aSIgor Mammedov uint32_t nodeid = cpu_list->cpus[i].props.node_id;
491e5b6d55aSIgor Mammedov /*
492e5b6d55aSIgor Mammedov * 5.2.16.4 GICC Affinity Structure
493e5b6d55aSIgor Mammedov */
494e5b6d55aSIgor Mammedov build_append_int_noprefix(table_data, 3, 1); /* Type */
495e5b6d55aSIgor Mammedov build_append_int_noprefix(table_data, 18, 1); /* Length */
496e5b6d55aSIgor Mammedov build_append_int_noprefix(table_data, nodeid, 4); /* Proximity Domain */
497e5b6d55aSIgor Mammedov build_append_int_noprefix(table_data, i, 4); /* ACPI Processor UID */
498e5b6d55aSIgor Mammedov /* Flags, Table 5-76 */
499e5b6d55aSIgor Mammedov build_append_int_noprefix(table_data, 1 /* Enabled */, 4);
500e5b6d55aSIgor Mammedov build_append_int_noprefix(table_data, 0, 4); /* Clock Domain */
5012b302e1eSShannon Zhao }
5022b302e1eSShannon Zhao
503da4f09a7SAndrew Jones mem_base = vms->memmap[VIRT_MEM].base;
504aa570207STao Xu for (i = 0; i < ms->numa_state->num_nodes; ++i) {
5057e721e7bSTao Xu if (ms->numa_state->nodes[i].node_mem > 0) {
506e5b6d55aSIgor Mammedov build_srat_memory(table_data, mem_base,
5077e721e7bSTao Xu ms->numa_state->nodes[i].node_mem, i,
5082b302e1eSShannon Zhao MEM_AFFINITY_ENABLED);
5097e721e7bSTao Xu mem_base += ms->numa_state->nodes[i].node_mem;
5102b302e1eSShannon Zhao }
51166c353ceSShannon Zhao }
5122b302e1eSShannon Zhao
513*a82fe829SJonathan Cameron build_srat_generic_affinity_structures(table_data);
5140a5b5acdSAnkit Agrawal
515c3b0cf6eSVishal Verma if (ms->nvdimms_state->is_enabled) {
516c3b0cf6eSVishal Verma nvdimm_build_srat(table_data);
517c3b0cf6eSVishal Verma }
518c3b0cf6eSVishal Verma
519442da7dcSShameer Kolothum if (ms->device_memory) {
520e5b6d55aSIgor Mammedov build_srat_memory(table_data, ms->device_memory->base,
521442da7dcSShameer Kolothum memory_region_size(&ms->device_memory->mr),
522442da7dcSShameer Kolothum ms->numa_state->num_nodes - 1,
523442da7dcSShameer Kolothum MEM_AFFINITY_HOTPLUGGABLE | MEM_AFFINITY_ENABLED);
524442da7dcSShameer Kolothum }
525442da7dcSShameer Kolothum
526255bf20fSIgor Mammedov acpi_table_end(linker, &table);
5272b302e1eSShannon Zhao }
5282b302e1eSShannon Zhao
52941041e57SIgor Mammedov /*
5301ec896feSPeter Maydell * ACPI spec, Revision 6.5
5311ec896feSPeter Maydell * 5.2.25 Generic Timer Description Table (GTDT)
53241041e57SIgor Mammedov */
533ee246400SShannon Zhao static void
build_gtdt(GArray * table_data,BIOSLinker * linker,VirtMachineState * vms)5348dd845d3SAndrew Jones build_gtdt(GArray *table_data, BIOSLinker *linker, VirtMachineState *vms)
535ee246400SShannon Zhao {
5368dd845d3SAndrew Jones VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms);
53741041e57SIgor Mammedov /*
53841041e57SIgor Mammedov * Table 5-117 Flag Definitions
53941041e57SIgor Mammedov * set only "Timer interrupt Mode" and assume "Timer Interrupt
54041041e57SIgor Mammedov * polarity" bit as '0: Interrupt is Active high'
54141041e57SIgor Mammedov */
54241041e57SIgor Mammedov uint32_t irqflags = vmc->claim_edge_triggered_timers ?
54341041e57SIgor Mammedov 1 : /* Interrupt is Edge triggered */
54441041e57SIgor Mammedov 0; /* Interrupt is Level triggered */
5451ec896feSPeter Maydell AcpiTable table = { .sig = "GTDT", .rev = 3, .oem_id = vms->oem_id,
54641041e57SIgor Mammedov .oem_table_id = vms->oem_table_id };
5478dd845d3SAndrew Jones
54841041e57SIgor Mammedov acpi_table_begin(&table, table_data);
549ee246400SShannon Zhao
55041041e57SIgor Mammedov /* CntControlBase Physical Address */
5515dbc9a27SMiguel Luis build_append_int_noprefix(table_data, 0xFFFFFFFFFFFFFFFF, 8);
55241041e57SIgor Mammedov build_append_int_noprefix(table_data, 0, 4); /* Reserved */
55341041e57SIgor Mammedov /*
55441041e57SIgor Mammedov * FIXME: clarify comment:
55541041e57SIgor Mammedov * The interrupt values are the same with the device tree when adding 16
55641041e57SIgor Mammedov */
55741041e57SIgor Mammedov /* Secure EL1 timer GSIV */
5589036e917SLeif Lindholm build_append_int_noprefix(table_data, ARCH_TIMER_S_EL1_IRQ, 4);
55941041e57SIgor Mammedov /* Secure EL1 timer Flags */
56041041e57SIgor Mammedov build_append_int_noprefix(table_data, irqflags, 4);
56141041e57SIgor Mammedov /* Non-Secure EL1 timer GSIV */
5629036e917SLeif Lindholm build_append_int_noprefix(table_data, ARCH_TIMER_NS_EL1_IRQ, 4);
56341041e57SIgor Mammedov /* Non-Secure EL1 timer Flags */
56441041e57SIgor Mammedov build_append_int_noprefix(table_data, irqflags |
56541041e57SIgor Mammedov 1UL << 2, /* Always-on Capability */
56641041e57SIgor Mammedov 4);
56741041e57SIgor Mammedov /* Virtual timer GSIV */
5689036e917SLeif Lindholm build_append_int_noprefix(table_data, ARCH_TIMER_VIRT_IRQ, 4);
56941041e57SIgor Mammedov /* Virtual Timer Flags */
57041041e57SIgor Mammedov build_append_int_noprefix(table_data, irqflags, 4);
57141041e57SIgor Mammedov /* Non-Secure EL2 timer GSIV */
5729036e917SLeif Lindholm build_append_int_noprefix(table_data, ARCH_TIMER_NS_EL2_IRQ, 4);
57341041e57SIgor Mammedov /* Non-Secure EL2 timer Flags */
57441041e57SIgor Mammedov build_append_int_noprefix(table_data, irqflags, 4);
57541041e57SIgor Mammedov /* CntReadBase Physical address */
5765dbc9a27SMiguel Luis build_append_int_noprefix(table_data, 0xFFFFFFFFFFFFFFFF, 8);
57741041e57SIgor Mammedov /* Platform Timer Count */
57841041e57SIgor Mammedov build_append_int_noprefix(table_data, 0, 4);
57941041e57SIgor Mammedov /* Platform Timer Offset */
58041041e57SIgor Mammedov build_append_int_noprefix(table_data, 0, 4);
5811ec896feSPeter Maydell if (vms->ns_el2_virt_timer_irq) {
5821ec896feSPeter Maydell /* Virtual EL2 Timer GSIV */
5831ec896feSPeter Maydell build_append_int_noprefix(table_data, ARCH_TIMER_NS_EL2_VIRT_IRQ, 4);
5841ec896feSPeter Maydell /* Virtual EL2 Timer Flags */
5851ec896feSPeter Maydell build_append_int_noprefix(table_data, irqflags, 4);
5861ec896feSPeter Maydell } else {
5871ec896feSPeter Maydell build_append_int_noprefix(table_data, 0, 4);
5881ec896feSPeter Maydell build_append_int_noprefix(table_data, 0, 4);
5891ec896feSPeter Maydell }
59041041e57SIgor Mammedov acpi_table_end(linker, &table);
591ee246400SShannon Zhao }
592ee246400SShannon Zhao
593f0dc9a5dSEric Auger /* Debug Port Table 2 (DBG2) */
594f0dc9a5dSEric Auger static void
build_dbg2(GArray * table_data,BIOSLinker * linker,VirtMachineState * vms)595f0dc9a5dSEric Auger build_dbg2(GArray *table_data, BIOSLinker *linker, VirtMachineState *vms)
596f0dc9a5dSEric Auger {
597f0dc9a5dSEric Auger AcpiTable table = { .sig = "DBG2", .rev = 0, .oem_id = vms->oem_id,
598f0dc9a5dSEric Auger .oem_table_id = vms->oem_table_id };
599f0dc9a5dSEric Auger int dbg2devicelength;
600f0dc9a5dSEric Auger const char name[] = "COM0";
601f0dc9a5dSEric Auger const int namespace_length = sizeof(name);
602f0dc9a5dSEric Auger
603f0dc9a5dSEric Auger acpi_table_begin(&table, table_data);
604f0dc9a5dSEric Auger
605f0dc9a5dSEric Auger dbg2devicelength = 22 + /* BaseAddressRegister[] offset */
606f0dc9a5dSEric Auger 12 + /* BaseAddressRegister[] */
607f0dc9a5dSEric Auger 4 + /* AddressSize[] */
608f0dc9a5dSEric Auger namespace_length /* NamespaceString[] */;
609f0dc9a5dSEric Auger
610f0dc9a5dSEric Auger /* OffsetDbgDeviceInfo */
611f0dc9a5dSEric Auger build_append_int_noprefix(table_data, 44, 4);
612f0dc9a5dSEric Auger /* NumberDbgDeviceInfo */
613f0dc9a5dSEric Auger build_append_int_noprefix(table_data, 1, 4);
614f0dc9a5dSEric Auger
615f0dc9a5dSEric Auger /* Table 2. Debug Device Information structure format */
616f0dc9a5dSEric Auger build_append_int_noprefix(table_data, 0, 1); /* Revision */
617f0dc9a5dSEric Auger build_append_int_noprefix(table_data, dbg2devicelength, 2); /* Length */
618f0dc9a5dSEric Auger /* NumberofGenericAddressRegisters */
619f0dc9a5dSEric Auger build_append_int_noprefix(table_data, 1, 1);
620f0dc9a5dSEric Auger /* NameSpaceStringLength */
621f0dc9a5dSEric Auger build_append_int_noprefix(table_data, namespace_length, 2);
622f0dc9a5dSEric Auger build_append_int_noprefix(table_data, 38, 2); /* NameSpaceStringOffset */
623f0dc9a5dSEric Auger build_append_int_noprefix(table_data, 0, 2); /* OemDataLength */
624f0dc9a5dSEric Auger /* OemDataOffset (0 means no OEM data) */
625f0dc9a5dSEric Auger build_append_int_noprefix(table_data, 0, 2);
626f0dc9a5dSEric Auger
627f0dc9a5dSEric Auger /* Port Type */
628f0dc9a5dSEric Auger build_append_int_noprefix(table_data, 0x8000 /* Serial */, 2);
629f0dc9a5dSEric Auger /* Port Subtype */
630f0dc9a5dSEric Auger build_append_int_noprefix(table_data, 0x3 /* ARM PL011 UART */, 2);
631f0dc9a5dSEric Auger build_append_int_noprefix(table_data, 0, 2); /* Reserved */
632f0dc9a5dSEric Auger /* BaseAddressRegisterOffset */
633f0dc9a5dSEric Auger build_append_int_noprefix(table_data, 22, 2);
634f0dc9a5dSEric Auger /* AddressSizeOffset */
635f0dc9a5dSEric Auger build_append_int_noprefix(table_data, 34, 2);
636f0dc9a5dSEric Auger
637f0dc9a5dSEric Auger /* BaseAddressRegister[] */
63841f7b58bSUdo Steinberg build_append_gas(table_data, AML_AS_SYSTEM_MEMORY, 32, 0, 3,
639fe22cba9SPeter Maydell vms->memmap[VIRT_UART0].base);
640f0dc9a5dSEric Auger
641f0dc9a5dSEric Auger /* AddressSize[] */
642f0dc9a5dSEric Auger build_append_int_noprefix(table_data,
643fe22cba9SPeter Maydell vms->memmap[VIRT_UART0].size, 4);
644f0dc9a5dSEric Auger
645f0dc9a5dSEric Auger /* NamespaceString[] */
646f0dc9a5dSEric Auger g_array_append_vals(table_data, name, namespace_length);
647f0dc9a5dSEric Auger
648f0dc9a5dSEric Auger acpi_table_end(linker, &table);
649f0dc9a5dSEric Auger };
650f0dc9a5dSEric Auger
65199a7545fSIgor Mammedov /*
6527fe4c35cSMiguel Luis * ACPI spec, Revision 6.0 Errata A
65399a7545fSIgor Mammedov * 5.2.12 Multiple APIC Description Table (MADT)
65499a7545fSIgor Mammedov */
build_append_gicr(GArray * table_data,uint64_t base,uint32_t size)65537f33084SIgor Mammedov static void build_append_gicr(GArray *table_data, uint64_t base, uint32_t size)
65637f33084SIgor Mammedov {
65737f33084SIgor Mammedov build_append_int_noprefix(table_data, 0xE, 1); /* Type */
65837f33084SIgor Mammedov build_append_int_noprefix(table_data, 16, 1); /* Length */
65937f33084SIgor Mammedov build_append_int_noprefix(table_data, 0, 2); /* Reserved */
660b3db996fSStefan Weil /* Discovery Range Base Address */
66137f33084SIgor Mammedov build_append_int_noprefix(table_data, base, 8);
66237f33084SIgor Mammedov build_append_int_noprefix(table_data, size, 4); /* Discovery Range Length */
66337f33084SIgor Mammedov }
66437f33084SIgor Mammedov
665982d06c5SShannon Zhao static void
build_madt(GArray * table_data,BIOSLinker * linker,VirtMachineState * vms)666da4f09a7SAndrew Jones build_madt(GArray *table_data, BIOSLinker *linker, VirtMachineState *vms)
667982d06c5SShannon Zhao {
66837f33084SIgor Mammedov int i;
669da4f09a7SAndrew Jones VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms);
670da4f09a7SAndrew Jones const MemMapEntry *memmap = vms->memmap;
6717fe4c35cSMiguel Luis AcpiTable table = { .sig = "APIC", .rev = 4, .oem_id = vms->oem_id,
67299a7545fSIgor Mammedov .oem_table_id = vms->oem_table_id };
673982d06c5SShannon Zhao
67499a7545fSIgor Mammedov acpi_table_begin(&table, table_data);
67599a7545fSIgor Mammedov /* Local Interrupt Controller Address */
67699a7545fSIgor Mammedov build_append_int_noprefix(table_data, 0, 4);
67799a7545fSIgor Mammedov build_append_int_noprefix(table_data, 0, 4); /* Flags */
678982d06c5SShannon Zhao
67937f33084SIgor Mammedov /* 5.2.12.15 GIC Distributor Structure */
68037f33084SIgor Mammedov build_append_int_noprefix(table_data, 0xC, 1); /* Type */
68137f33084SIgor Mammedov build_append_int_noprefix(table_data, 24, 1); /* Length */
68237f33084SIgor Mammedov build_append_int_noprefix(table_data, 0, 2); /* Reserved */
68337f33084SIgor Mammedov build_append_int_noprefix(table_data, 0, 4); /* GIC ID */
68437f33084SIgor Mammedov /* Physical Base Address */
68537f33084SIgor Mammedov build_append_int_noprefix(table_data, memmap[VIRT_GIC_DIST].base, 8);
68637f33084SIgor Mammedov build_append_int_noprefix(table_data, 0, 4); /* System Vector Base */
68737f33084SIgor Mammedov /* GIC version */
68837f33084SIgor Mammedov build_append_int_noprefix(table_data, vms->gic_version, 1);
68937f33084SIgor Mammedov build_append_int_noprefix(table_data, 0, 3); /* Reserved */
690b92ad394SPavel Fedin
6919cd07db9SAndrew Jones for (i = 0; i < MACHINE(vms)->smp.cpus; i++) {
6925d9c1756SShannon Zhao ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(i));
69337f33084SIgor Mammedov uint64_t physical_base_address = 0, gich = 0, gicv = 0;
6949036e917SLeif Lindholm uint32_t vgic_interrupt = vms->virt ? ARCH_GIC_MAINT_IRQ : 0;
69537f33084SIgor Mammedov uint32_t pmu_interrupt = arm_feature(&armcpu->env, ARM_FEATURE_PMU) ?
6969036e917SLeif Lindholm VIRTUAL_PMU_IRQ : 0;
6975d9c1756SShannon Zhao
698e1f04578SZenghui Yu if (vms->gic_version == VIRT_GIC_VERSION_2) {
69937f33084SIgor Mammedov physical_base_address = memmap[VIRT_GIC_CPU].base;
70037f33084SIgor Mammedov gicv = memmap[VIRT_GIC_VCPU].base;
70137f33084SIgor Mammedov gich = memmap[VIRT_GIC_HYP].base;
702f2fbfaceSShannon Zhao }
7038433dee0SShannon Zhao
70437f33084SIgor Mammedov /* 5.2.12.14 GIC Structure */
70537f33084SIgor Mammedov build_append_int_noprefix(table_data, 0xB, 1); /* Type */
7067fe4c35cSMiguel Luis build_append_int_noprefix(table_data, 80, 1); /* Length */
70737f33084SIgor Mammedov build_append_int_noprefix(table_data, 0, 2); /* Reserved */
70837f33084SIgor Mammedov build_append_int_noprefix(table_data, i, 4); /* GIC ID */
70937f33084SIgor Mammedov build_append_int_noprefix(table_data, i, 4); /* ACPI Processor UID */
71037f33084SIgor Mammedov /* Flags */
71137f33084SIgor Mammedov build_append_int_noprefix(table_data, 1, 4); /* Enabled */
71237f33084SIgor Mammedov /* Parking Protocol Version */
71337f33084SIgor Mammedov build_append_int_noprefix(table_data, 0, 4);
71437f33084SIgor Mammedov /* Performance Interrupt GSIV */
71537f33084SIgor Mammedov build_append_int_noprefix(table_data, pmu_interrupt, 4);
71637f33084SIgor Mammedov build_append_int_noprefix(table_data, 0, 8); /* Parked Address */
71737f33084SIgor Mammedov /* Physical Base Address */
71837f33084SIgor Mammedov build_append_int_noprefix(table_data, physical_base_address, 8);
71937f33084SIgor Mammedov build_append_int_noprefix(table_data, gicv, 8); /* GICV */
72037f33084SIgor Mammedov build_append_int_noprefix(table_data, gich, 8); /* GICH */
72137f33084SIgor Mammedov /* VGIC Maintenance interrupt */
72237f33084SIgor Mammedov build_append_int_noprefix(table_data, vgic_interrupt, 4);
72337f33084SIgor Mammedov build_append_int_noprefix(table_data, 0, 8); /* GICR Base Address*/
72437f33084SIgor Mammedov /* MPIDR */
725c4380f7bSRichard Henderson build_append_int_noprefix(table_data, arm_cpu_mp_affinity(armcpu), 8);
7267fe4c35cSMiguel Luis /* Processor Power Efficiency Class */
7277fe4c35cSMiguel Luis build_append_int_noprefix(table_data, 0, 1);
7287fe4c35cSMiguel Luis /* Reserved */
7297fe4c35cSMiguel Luis build_append_int_noprefix(table_data, 0, 3);
730f2fbfaceSShannon Zhao }
731f2fbfaceSShannon Zhao
732e1f04578SZenghui Yu if (vms->gic_version != VIRT_GIC_VERSION_2) {
73337f33084SIgor Mammedov build_append_gicr(table_data, memmap[VIRT_GIC_REDIST].base,
73437f33084SIgor Mammedov memmap[VIRT_GIC_REDIST].size);
73537f33084SIgor Mammedov if (virt_gicv3_redist_region_count(vms) == 2) {
73637f33084SIgor Mammedov build_append_gicr(table_data, memmap[VIRT_HIGH_GIC_REDIST2].base,
73737f33084SIgor Mammedov memmap[VIRT_HIGH_GIC_REDIST2].size);
738a1de312fSEric Auger }
739a1de312fSEric Auger
740da4f09a7SAndrew Jones if (its_class_name() && !vmc->no_its) {
74137f33084SIgor Mammedov /*
74237f33084SIgor Mammedov * ACPI spec, Revision 6.0 Errata A
74337f33084SIgor Mammedov * (original 6.0 definition has invalid Length)
74437f33084SIgor Mammedov * 5.2.12.18 GIC ITS Structure
74537f33084SIgor Mammedov */
74637f33084SIgor Mammedov build_append_int_noprefix(table_data, 0xF, 1); /* Type */
74737f33084SIgor Mammedov build_append_int_noprefix(table_data, 20, 1); /* Length */
74837f33084SIgor Mammedov build_append_int_noprefix(table_data, 0, 2); /* Reserved */
74937f33084SIgor Mammedov build_append_int_noprefix(table_data, 0, 4); /* GIC ITS ID */
75037f33084SIgor Mammedov /* Physical Base Address */
75137f33084SIgor Mammedov build_append_int_noprefix(table_data, memmap[VIRT_GIC_ITS].base, 8);
75237f33084SIgor Mammedov build_append_int_noprefix(table_data, 0, 4); /* Reserved */
75313cda487SAndrew Jones }
754b92ad394SPavel Fedin } else {
75537f33084SIgor Mammedov const uint16_t spi_base = vms->irqmap[VIRT_GIC_V2M] + ARM_SPI_BASE;
75637f33084SIgor Mammedov
75737f33084SIgor Mammedov /* 5.2.12.16 GIC MSI Frame Structure */
75837f33084SIgor Mammedov build_append_int_noprefix(table_data, 0xD, 1); /* Type */
75937f33084SIgor Mammedov build_append_int_noprefix(table_data, 24, 1); /* Length */
76037f33084SIgor Mammedov build_append_int_noprefix(table_data, 0, 2); /* Reserved */
76137f33084SIgor Mammedov build_append_int_noprefix(table_data, 0, 4); /* GIC MSI Frame ID */
76237f33084SIgor Mammedov /* Physical Base Address */
76337f33084SIgor Mammedov build_append_int_noprefix(table_data, memmap[VIRT_GIC_V2M].base, 8);
76437f33084SIgor Mammedov build_append_int_noprefix(table_data, 1, 4); /* Flags */
76537f33084SIgor Mammedov /* SPI Count */
76637f33084SIgor Mammedov build_append_int_noprefix(table_data, NUM_GICV2M_SPIS, 2);
76737f33084SIgor Mammedov build_append_int_noprefix(table_data, spi_base, 2); /* SPI Base */
768b92ad394SPavel Fedin }
76999a7545fSIgor Mammedov acpi_table_end(linker, &table);
770982d06c5SShannon Zhao }
771982d06c5SShannon Zhao
772c2f7c0c3SShannon Zhao /* FADT */
build_fadt_rev6(GArray * table_data,BIOSLinker * linker,VirtMachineState * vms,unsigned dsdt_tbl_offset)7734496d1d3SMiguel Luis static void build_fadt_rev6(GArray *table_data, BIOSLinker *linker,
77479e993a0SAndrew Jones VirtMachineState *vms, unsigned dsdt_tbl_offset)
775c2f7c0c3SShannon Zhao {
7761ec896feSPeter Maydell /* ACPI v6.3 */
777dd1b2037SIgor Mammedov AcpiFadtData fadt = {
7784496d1d3SMiguel Luis .rev = 6,
7791ec896feSPeter Maydell .minor_ver = 3,
780dd1b2037SIgor Mammedov .flags = 1 << ACPI_FADT_F_HW_REDUCED_ACPI,
781dd1b2037SIgor Mammedov .xdsdt_tbl_offset = &dsdt_tbl_offset,
782dd1b2037SIgor Mammedov };
783c2f7c0c3SShannon Zhao
78479e993a0SAndrew Jones switch (vms->psci_conduit) {
78579e993a0SAndrew Jones case QEMU_PSCI_CONDUIT_DISABLED:
786dd1b2037SIgor Mammedov fadt.arm_boot_arch = 0;
78779e993a0SAndrew Jones break;
78879e993a0SAndrew Jones case QEMU_PSCI_CONDUIT_HVC:
789dd1b2037SIgor Mammedov fadt.arm_boot_arch = ACPI_FADT_ARM_PSCI_COMPLIANT |
790dd1b2037SIgor Mammedov ACPI_FADT_ARM_PSCI_USE_HVC;
79179e993a0SAndrew Jones break;
79279e993a0SAndrew Jones case QEMU_PSCI_CONDUIT_SMC:
793dd1b2037SIgor Mammedov fadt.arm_boot_arch = ACPI_FADT_ARM_PSCI_COMPLIANT;
79479e993a0SAndrew Jones break;
79579e993a0SAndrew Jones default:
79679e993a0SAndrew Jones g_assert_not_reached();
79779e993a0SAndrew Jones }
79879e993a0SAndrew Jones
799602b4582SMarian Postevca build_fadt(table_data, linker, &fadt, vms->oem_id, vms->oem_table_id);
800c2f7c0c3SShannon Zhao }
801c2f7c0c3SShannon Zhao
802dfccd8cfSShannon Zhao /* DSDT */
803dfccd8cfSShannon Zhao static void
build_dsdt(GArray * table_data,BIOSLinker * linker,VirtMachineState * vms)804da4f09a7SAndrew Jones build_dsdt(GArray *table_data, BIOSLinker *linker, VirtMachineState *vms)
805dfccd8cfSShannon Zhao {
8062c1fb4d5SAndrew Jones VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms);
807dfccd8cfSShannon Zhao Aml *scope, *dsdt;
808cff51ac9SShameer Kolothum MachineState *ms = MACHINE(vms);
809da4f09a7SAndrew Jones const MemMapEntry *memmap = vms->memmap;
810da4f09a7SAndrew Jones const int *irqmap = vms->irqmap;
811fc02b869SIgor Mammedov AcpiTable table = { .sig = "DSDT", .rev = 2, .oem_id = vms->oem_id,
812fc02b869SIgor Mammedov .oem_table_id = vms->oem_table_id };
813dfccd8cfSShannon Zhao
814fc02b869SIgor Mammedov acpi_table_begin(&table, table_data);
815dfccd8cfSShannon Zhao dsdt = init_aml_allocator();
816dfccd8cfSShannon Zhao
81767736a25SShannon Zhao /* When booting the VM with UEFI, UEFI takes ownership of the RTC hardware.
81867736a25SShannon Zhao * While UEFI can use libfdt to disable the RTC device node in the DTB that
81967736a25SShannon Zhao * it passes to the OS, it cannot modify AML. Therefore, we won't generate
82067736a25SShannon Zhao * the RTC ACPI device at all when using UEFI.
82167736a25SShannon Zhao */
822dfccd8cfSShannon Zhao scope = aml_scope("\\_SB");
8239cd07db9SAndrew Jones acpi_dsdt_add_cpus(scope, vms);
824fe22cba9SPeter Maydell acpi_dsdt_add_uart(scope, &memmap[VIRT_UART0],
825e7100972SPeter Maydell (irqmap[VIRT_UART0] + ARM_SPI_BASE), 0);
826e7100972SPeter Maydell if (vms->second_ns_uart_present) {
827e7100972SPeter Maydell acpi_dsdt_add_uart(scope, &memmap[VIRT_UART1],
828e7100972SPeter Maydell (irqmap[VIRT_UART1] + ARM_SPI_BASE), 1);
829e7100972SPeter Maydell }
8302c1fb4d5SAndrew Jones if (vmc->acpi_expose_flash) {
831dfccd8cfSShannon Zhao acpi_dsdt_add_flash(scope, &memmap[VIRT_FLASH]);
8322c1fb4d5SAndrew Jones }
8334c7f4f4fSSunil V L fw_cfg_acpi_dsdt_add(scope, &memmap[VIRT_FW_CFG]);
83457ba8436SSunil V L virtio_acpi_dsdt_add(scope, memmap[VIRT_MMIO].base, memmap[VIRT_MMIO].size,
83557ba8436SSunil V L (irqmap[VIRT_MMIO] + ARM_SPI_BASE),
83657ba8436SSunil V L 0, NUM_VIRTIO_TRANSPORTS);
837c8f008c4SMarc Zyngier acpi_dsdt_add_pci(scope, memmap, irqmap[VIRT_PCIE] + ARM_SPI_BASE, vms);
838cff51ac9SShameer Kolothum if (vms->acpi_dev) {
839cff51ac9SShameer Kolothum build_ged_aml(scope, "\\_SB."GED_DEVICE,
840cff51ac9SShameer Kolothum HOTPLUG_HANDLER(vms->acpi_dev),
841cff51ac9SShameer Kolothum irqmap[VIRT_ACPI_GED] + ARM_SPI_BASE, AML_SYSTEM_MEMORY,
842cff51ac9SShameer Kolothum memmap[VIRT_ACPI_GED].base);
8431962f31bSShameer Kolothum } else {
8441962f31bSShameer Kolothum acpi_dsdt_add_gpio(scope, &memmap[VIRT_GPIO],
8451962f31bSShameer Kolothum (irqmap[VIRT_GPIO] + ARM_SPI_BASE));
846cff51ac9SShameer Kolothum }
847cff51ac9SShameer Kolothum
848cff51ac9SShameer Kolothum if (vms->acpi_dev) {
849cff51ac9SShameer Kolothum uint32_t event = object_property_get_uint(OBJECT(vms->acpi_dev),
850cff51ac9SShameer Kolothum "ged-event", &error_abort);
851cff51ac9SShameer Kolothum
852cff51ac9SShameer Kolothum if (event & ACPI_GED_MEM_HOTPLUG_EVT) {
853cff51ac9SShameer Kolothum build_memory_hotplug_aml(scope, ms->ram_slots, "\\_SB", NULL,
854cff51ac9SShameer Kolothum AML_SYSTEM_MEMORY,
855cff51ac9SShameer Kolothum memmap[VIRT_PCDIMM_ACPI].base);
856cff51ac9SShameer Kolothum }
857cff51ac9SShameer Kolothum }
858cff51ac9SShameer Kolothum
859ac6aa59aSShannon Zhao acpi_dsdt_add_power_button(scope);
860f50be48aSStefan Berger #ifdef CONFIG_TPM
8615ab540e9SEric Auger acpi_dsdt_add_tpm(scope, vms);
862f50be48aSStefan Berger #endif
863d4e5de1aSShannon Zhao
864dfccd8cfSShannon Zhao aml_append(dsdt, scope);
865dfccd8cfSShannon Zhao
866fc02b869SIgor Mammedov /* copy AML table into ACPI tables blob */
867dfccd8cfSShannon Zhao g_array_append_vals(table_data, dsdt->buf->data, dsdt->buf->len);
868fc02b869SIgor Mammedov
869fc02b869SIgor Mammedov acpi_table_end(linker, &table);
870dfccd8cfSShannon Zhao free_aml_allocator();
871dfccd8cfSShannon Zhao }
872dfccd8cfSShannon Zhao
873f5d8c8cdSShannon Zhao typedef
874f5d8c8cdSShannon Zhao struct AcpiBuildState {
875f5d8c8cdSShannon Zhao /* Copy of table in RAM (for patching). */
876f5d8c8cdSShannon Zhao MemoryRegion *table_mr;
877f5d8c8cdSShannon Zhao MemoryRegion *rsdp_mr;
878f5d8c8cdSShannon Zhao MemoryRegion *linker_mr;
879f5d8c8cdSShannon Zhao /* Is table patched? */
880f5d8c8cdSShannon Zhao bool patched;
881f5d8c8cdSShannon Zhao } AcpiBuildState;
882f5d8c8cdSShannon Zhao
acpi_align_size(GArray * blob,unsigned align)883451b1570SYubo Miao static void acpi_align_size(GArray *blob, unsigned align)
884451b1570SYubo Miao {
885451b1570SYubo Miao /*
886451b1570SYubo Miao * Align size to multiple of given size. This reduces the chance
887451b1570SYubo Miao * we need to change size in the future (breaking cross version migration).
888451b1570SYubo Miao */
889451b1570SYubo Miao g_array_set_size(blob, ROUND_UP(acpi_data_len(blob), align));
890451b1570SYubo Miao }
891451b1570SYubo Miao
892f5d8c8cdSShannon Zhao static
virt_acpi_build(VirtMachineState * vms,AcpiBuildTables * tables)893da4f09a7SAndrew Jones void virt_acpi_build(VirtMachineState *vms, AcpiBuildTables *tables)
894f5d8c8cdSShannon Zhao {
895da4f09a7SAndrew Jones VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms);
896f5d8c8cdSShannon Zhao GArray *table_offsets;
897cb51ac2fSArd Biesheuvel unsigned dsdt, xsdt;
898dfccd8cfSShannon Zhao GArray *tables_blob = tables->table_data;
899aa570207STao Xu MachineState *ms = MACHINE(vms);
900f5d8c8cdSShannon Zhao
901f5d8c8cdSShannon Zhao table_offsets = g_array_new(false, true /* clear */,
902f5d8c8cdSShannon Zhao sizeof(uint32_t));
903f5d8c8cdSShannon Zhao
904ad9671b8SIgor Mammedov bios_linker_loader_alloc(tables->linker,
905ad9671b8SIgor Mammedov ACPI_BUILD_TABLE_FILE, tables_blob,
906f5d8c8cdSShannon Zhao 64, false /* high memory */);
907f5d8c8cdSShannon Zhao
908dfccd8cfSShannon Zhao /* DSDT is pointed to by FADT */
909c2f7c0c3SShannon Zhao dsdt = tables_blob->len;
910da4f09a7SAndrew Jones build_dsdt(tables_blob, tables->linker, vms);
911dfccd8cfSShannon Zhao
91270d23ed5SYanan Wang /* FADT MADT PPTT GTDT MCFG SPCR DBG2 pointed to by RSDT */
913c2f7c0c3SShannon Zhao acpi_add_table(table_offsets, tables_blob);
9144496d1d3SMiguel Luis build_fadt_rev6(tables_blob, tables->linker, vms, dsdt);
915c2f7c0c3SShannon Zhao
916982d06c5SShannon Zhao acpi_add_table(table_offsets, tables_blob);
917da4f09a7SAndrew Jones build_madt(tables_blob, tables->linker, vms);
918982d06c5SShannon Zhao
91970d23ed5SYanan Wang if (!vmc->no_cpu_topology) {
92070d23ed5SYanan Wang acpi_add_table(table_offsets, tables_blob);
92170d23ed5SYanan Wang build_pptt(tables_blob, tables->linker, ms,
92270d23ed5SYanan Wang vms->oem_id, vms->oem_table_id);
92370d23ed5SYanan Wang }
92470d23ed5SYanan Wang
925ee246400SShannon Zhao acpi_add_table(table_offsets, tables_blob);
9268dd845d3SAndrew Jones build_gtdt(tables_blob, tables->linker, vms);
927ee246400SShannon Zhao
92884344884SShannon Zhao acpi_add_table(table_offsets, tables_blob);
92948cefd94SWei Yang {
93048cefd94SWei Yang AcpiMcfgInfo mcfg = {
93148cefd94SWei Yang .base = vms->memmap[VIRT_ECAM_ID(vms->highmem_ecam)].base,
93248cefd94SWei Yang .size = vms->memmap[VIRT_ECAM_ID(vms->highmem_ecam)].size,
93348cefd94SWei Yang };
934602b4582SMarian Postevca build_mcfg(tables_blob, tables->linker, &mcfg, vms->oem_id,
935602b4582SMarian Postevca vms->oem_table_id);
93648cefd94SWei Yang }
93784344884SShannon Zhao
938f264d51dSAndrew Jones acpi_add_table(table_offsets, tables_blob);
9397dd0b070SSia Jee Heng spcr_setup(tables_blob, tables->linker, vms);
940f264d51dSAndrew Jones
941f0dc9a5dSEric Auger acpi_add_table(table_offsets, tables_blob);
942f0dc9a5dSEric Auger build_dbg2(tables_blob, tables->linker, vms);
943f0dc9a5dSEric Auger
944aa16508fSDongjiu Geng if (vms->ras) {
945aa16508fSDongjiu Geng build_ghes_error_table(tables->hardware_errors, tables->linker);
946205cc75dSDongjiu Geng acpi_add_table(table_offsets, tables_blob);
947602b4582SMarian Postevca acpi_build_hest(tables_blob, tables->linker, vms->oem_id,
948602b4582SMarian Postevca vms->oem_table_id);
949aa16508fSDongjiu Geng }
950aa16508fSDongjiu Geng
951aa570207STao Xu if (ms->numa_state->num_nodes > 0) {
9522b302e1eSShannon Zhao acpi_add_table(table_offsets, tables_blob);
953da4f09a7SAndrew Jones build_srat(tables_blob, tables->linker, vms);
954118154b7STao Xu if (ms->numa_state->have_numa_distance) {
95594a66456SAndrew Jones acpi_add_table(table_offsets, tables_blob);
956602b4582SMarian Postevca build_slit(tables_blob, tables->linker, ms, vms->oem_id,
957602b4582SMarian Postevca vms->oem_table_id);
95894a66456SAndrew Jones }
9597cbd3fd3SXiang Chen
9607cbd3fd3SXiang Chen if (ms->numa_state->hmat_enabled) {
9617cbd3fd3SXiang Chen acpi_add_table(table_offsets, tables_blob);
9627cbd3fd3SXiang Chen build_hmat(tables_blob, tables->linker, ms->numa_state,
9637cbd3fd3SXiang Chen vms->oem_id, vms->oem_table_id);
9647cbd3fd3SXiang Chen }
9652b302e1eSShannon Zhao }
9662b302e1eSShannon Zhao
967b5a60beeSKwangwoo Lee if (ms->nvdimms_state->is_enabled) {
968b5a60beeSKwangwoo Lee nvdimm_build_acpi(table_offsets, tables_blob, tables->linker,
969602b4582SMarian Postevca ms->nvdimms_state, ms->ram_slots, vms->oem_id,
970602b4582SMarian Postevca vms->oem_table_id);
971b5a60beeSKwangwoo Lee }
972b5a60beeSKwangwoo Lee
973da4f09a7SAndrew Jones if (its_class_name() && !vmc->no_its) {
974e78f1222SPrem Mallappa acpi_add_table(table_offsets, tables_blob);
975a703b4f6SPrem Mallappa build_iort(tables_blob, tables->linker, vms);
976e78f1222SPrem Mallappa }
977e78f1222SPrem Mallappa
978f50be48aSStefan Berger #ifdef CONFIG_TPM
97980bde693SEric Auger if (tpm_get_version(tpm_find()) == TPM_VERSION_2_0) {
98080bde693SEric Auger acpi_add_table(table_offsets, tables_blob);
981602b4582SMarian Postevca build_tpm2(tables_blob, tables->linker, tables->tcpalog, vms->oem_id,
982602b4582SMarian Postevca vms->oem_table_id);
98380bde693SEric Auger }
984f50be48aSStefan Berger #endif
98580bde693SEric Auger
986cf1a5cc9SJean-Philippe Brucker if (vms->iommu == VIRT_IOMMU_VIRTIO) {
987cf1a5cc9SJean-Philippe Brucker acpi_add_table(table_offsets, tables_blob);
988cf1a5cc9SJean-Philippe Brucker build_viot(ms, tables_blob, tables->linker, vms->virtio_iommu_bdf,
989cf1a5cc9SJean-Philippe Brucker vms->oem_id, vms->oem_table_id);
990cf1a5cc9SJean-Philippe Brucker }
991cf1a5cc9SJean-Philippe Brucker
992cb51ac2fSArd Biesheuvel /* XSDT is pointed to by RSDP */
993cb51ac2fSArd Biesheuvel xsdt = tables_blob->len;
994602b4582SMarian Postevca build_xsdt(tables_blob, tables->linker, table_offsets, vms->oem_id,
995602b4582SMarian Postevca vms->oem_table_id);
996243bdb79SShannon Zhao
997d4bec5d8SShannon Zhao /* RSDP is in FSEG memory, so allocate it separately */
9985c5fce1aSSamuel Ortiz {
9995c5fce1aSSamuel Ortiz AcpiRsdpData rsdp_data = {
10005c5fce1aSSamuel Ortiz .revision = 2,
1001602b4582SMarian Postevca .oem_id = vms->oem_id,
10025c5fce1aSSamuel Ortiz .xsdt_tbl_offset = &xsdt,
10035c5fce1aSSamuel Ortiz .rsdt_tbl_offset = NULL,
10045c5fce1aSSamuel Ortiz };
10055c5fce1aSSamuel Ortiz build_rsdp(tables->rsdp, tables->linker, &rsdp_data);
10065c5fce1aSSamuel Ortiz }
1007d4bec5d8SShannon Zhao
1008451b1570SYubo Miao /*
1009451b1570SYubo Miao * The align size is 128, warn if 64k is not enough therefore
1010451b1570SYubo Miao * the align size could be resized.
1011451b1570SYubo Miao */
1012451b1570SYubo Miao if (tables_blob->len > ACPI_BUILD_TABLE_SIZE / 2) {
1013451b1570SYubo Miao warn_report("ACPI table size %u exceeds %d bytes,"
1014451b1570SYubo Miao " migration may not work",
1015451b1570SYubo Miao tables_blob->len, ACPI_BUILD_TABLE_SIZE / 2);
1016451b1570SYubo Miao error_printf("Try removing CPUs, NUMA nodes, memory slots"
1017cd25f5d3SGreg Kurz " or PCI bridges.\n");
1018451b1570SYubo Miao }
1019451b1570SYubo Miao acpi_align_size(tables_blob, ACPI_BUILD_TABLE_SIZE);
1020451b1570SYubo Miao
1021451b1570SYubo Miao
1022f5d8c8cdSShannon Zhao /* Cleanup memory that's no longer used. */
1023f5d8c8cdSShannon Zhao g_array_free(table_offsets, true);
1024f5d8c8cdSShannon Zhao }
1025f5d8c8cdSShannon Zhao
acpi_ram_update(MemoryRegion * mr,GArray * data)1026f5d8c8cdSShannon Zhao static void acpi_ram_update(MemoryRegion *mr, GArray *data)
1027f5d8c8cdSShannon Zhao {
1028f5d8c8cdSShannon Zhao uint32_t size = acpi_data_len(data);
1029f5d8c8cdSShannon Zhao
1030f5d8c8cdSShannon Zhao /* Make sure RAM size is correct - in case it got changed
1031f5d8c8cdSShannon Zhao * e.g. by migration */
1032f5d8c8cdSShannon Zhao memory_region_ram_resize(mr, size, &error_abort);
1033f5d8c8cdSShannon Zhao
1034f5d8c8cdSShannon Zhao memcpy(memory_region_get_ram_ptr(mr), data->data, size);
1035f5d8c8cdSShannon Zhao memory_region_set_dirty(mr, 0, size);
1036f5d8c8cdSShannon Zhao }
1037f5d8c8cdSShannon Zhao
virt_acpi_build_update(void * build_opaque)10383f8752b4SGabriel L. Somlo static void virt_acpi_build_update(void *build_opaque)
1039f5d8c8cdSShannon Zhao {
1040f5d8c8cdSShannon Zhao AcpiBuildState *build_state = build_opaque;
1041f5d8c8cdSShannon Zhao AcpiBuildTables tables;
1042f5d8c8cdSShannon Zhao
1043f5d8c8cdSShannon Zhao /* No state to update or already patched? Nothing to do. */
1044f5d8c8cdSShannon Zhao if (!build_state || build_state->patched) {
1045f5d8c8cdSShannon Zhao return;
1046f5d8c8cdSShannon Zhao }
1047f5d8c8cdSShannon Zhao build_state->patched = true;
1048f5d8c8cdSShannon Zhao
1049f5d8c8cdSShannon Zhao acpi_build_tables_init(&tables);
1050f5d8c8cdSShannon Zhao
10514dad9e74SAndrew Jones virt_acpi_build(VIRT_MACHINE(qdev_get_machine()), &tables);
1052f5d8c8cdSShannon Zhao
1053f5d8c8cdSShannon Zhao acpi_ram_update(build_state->table_mr, tables.table_data);
1054f5d8c8cdSShannon Zhao acpi_ram_update(build_state->rsdp_mr, tables.rsdp);
10550e9b9edaSIgor Mammedov acpi_ram_update(build_state->linker_mr, tables.linker->cmd_blob);
1056f5d8c8cdSShannon Zhao
1057f5d8c8cdSShannon Zhao acpi_build_tables_cleanup(&tables, true);
1058f5d8c8cdSShannon Zhao }
1059f5d8c8cdSShannon Zhao
virt_acpi_build_reset(void * build_opaque)1060f5d8c8cdSShannon Zhao static void virt_acpi_build_reset(void *build_opaque)
1061f5d8c8cdSShannon Zhao {
1062f5d8c8cdSShannon Zhao AcpiBuildState *build_state = build_opaque;
1063f5d8c8cdSShannon Zhao build_state->patched = false;
1064f5d8c8cdSShannon Zhao }
1065f5d8c8cdSShannon Zhao
1066f5d8c8cdSShannon Zhao static const VMStateDescription vmstate_virt_acpi_build = {
1067f5d8c8cdSShannon Zhao .name = "virt_acpi_build",
1068f5d8c8cdSShannon Zhao .version_id = 1,
1069f5d8c8cdSShannon Zhao .minimum_version_id = 1,
1070607ef570SRichard Henderson .fields = (const VMStateField[]) {
1071f5d8c8cdSShannon Zhao VMSTATE_BOOL(patched, AcpiBuildState),
1072f5d8c8cdSShannon Zhao VMSTATE_END_OF_LIST()
1073f5d8c8cdSShannon Zhao },
1074f5d8c8cdSShannon Zhao };
1075f5d8c8cdSShannon Zhao
virt_acpi_setup(VirtMachineState * vms)1076e9a8e474SAndrew Jones void virt_acpi_setup(VirtMachineState *vms)
1077f5d8c8cdSShannon Zhao {
1078f5d8c8cdSShannon Zhao AcpiBuildTables tables;
1079f5d8c8cdSShannon Zhao AcpiBuildState *build_state;
1080a08a6462SDongjiu Geng AcpiGedState *acpi_ged_state;
1081f5d8c8cdSShannon Zhao
1082af1f60a4SAndrew Jones if (!vms->fw_cfg) {
1083f5d8c8cdSShannon Zhao trace_virt_acpi_setup();
1084f5d8c8cdSShannon Zhao return;
1085f5d8c8cdSShannon Zhao }
1086f5d8c8cdSShannon Zhao
108717e89077SGerd Hoffmann if (!virt_is_acpi_enabled(vms)) {
1088f5d8c8cdSShannon Zhao trace_virt_acpi_setup();
1089f5d8c8cdSShannon Zhao return;
1090f5d8c8cdSShannon Zhao }
1091f5d8c8cdSShannon Zhao
1092f5d8c8cdSShannon Zhao build_state = g_malloc0(sizeof *build_state);
1093f5d8c8cdSShannon Zhao
1094f5d8c8cdSShannon Zhao acpi_build_tables_init(&tables);
1095da4f09a7SAndrew Jones virt_acpi_build(vms, &tables);
1096f5d8c8cdSShannon Zhao
1097f5d8c8cdSShannon Zhao /* Now expose it all to Guest */
109882f76c67SWei Yang build_state->table_mr = acpi_add_rom_blob(virt_acpi_build_update,
109982f76c67SWei Yang build_state, tables.table_data,
11006930ba0dSDavid Hildenbrand ACPI_BUILD_TABLE_FILE);
1101f5d8c8cdSShannon Zhao assert(build_state->table_mr != NULL);
1102f5d8c8cdSShannon Zhao
11036930ba0dSDavid Hildenbrand build_state->linker_mr = acpi_add_rom_blob(virt_acpi_build_update,
11046930ba0dSDavid Hildenbrand build_state,
11056930ba0dSDavid Hildenbrand tables.linker->cmd_blob,
11066930ba0dSDavid Hildenbrand ACPI_BUILD_LOADER_FILE);
1107f5d8c8cdSShannon Zhao
1108af1f60a4SAndrew Jones fw_cfg_add_file(vms->fw_cfg, ACPI_BUILD_TPMLOG_FILE, tables.tcpalog->data,
1109af1f60a4SAndrew Jones acpi_data_len(tables.tcpalog));
1110f5d8c8cdSShannon Zhao
1111a08a6462SDongjiu Geng if (vms->ras) {
1112a08a6462SDongjiu Geng assert(vms->acpi_dev);
1113a08a6462SDongjiu Geng acpi_ged_state = ACPI_GED(vms->acpi_dev);
1114a08a6462SDongjiu Geng acpi_ghes_add_fw_cfg(&acpi_ged_state->ghes_state,
1115a08a6462SDongjiu Geng vms->fw_cfg, tables.hardware_errors);
1116a08a6462SDongjiu Geng }
1117a08a6462SDongjiu Geng
111882f76c67SWei Yang build_state->rsdp_mr = acpi_add_rom_blob(virt_acpi_build_update,
111982f76c67SWei Yang build_state, tables.rsdp,
11206930ba0dSDavid Hildenbrand ACPI_BUILD_RSDP_FILE);
1121f5d8c8cdSShannon Zhao
1122f5d8c8cdSShannon Zhao qemu_register_reset(virt_acpi_build_reset, build_state);
1123f5d8c8cdSShannon Zhao virt_acpi_build_reset(build_state);
1124f5d8c8cdSShannon Zhao vmstate_register(NULL, 0, &vmstate_virt_acpi_build, build_state);
1125f5d8c8cdSShannon Zhao
1126f5d8c8cdSShannon Zhao /* Cleanup tables but don't free the memory: we track it
1127f5d8c8cdSShannon Zhao * in build_state.
1128f5d8c8cdSShannon Zhao */
1129f5d8c8cdSShannon Zhao acpi_build_tables_cleanup(&tables, false);
1130f5d8c8cdSShannon Zhao }
1131