172c194f7SMichael S. Tsirkin /* Support for generating ACPI tables and passing them to Guests
272c194f7SMichael S. Tsirkin *
372c194f7SMichael S. Tsirkin * Copyright (C) 2008-2010 Kevin O'Connor <kevin@koconnor.net>
472c194f7SMichael S. Tsirkin * Copyright (C) 2006 Fabrice Bellard
572c194f7SMichael S. Tsirkin * Copyright (C) 2013 Red Hat Inc
672c194f7SMichael S. Tsirkin *
772c194f7SMichael S. Tsirkin * Author: Michael S. Tsirkin <mst@redhat.com>
872c194f7SMichael S. Tsirkin *
972c194f7SMichael S. Tsirkin * This program is free software; you can redistribute it and/or modify
1072c194f7SMichael S. Tsirkin * it under the terms of the GNU General Public License as published by
1172c194f7SMichael S. Tsirkin * the Free Software Foundation; either version 2 of the License, or
1272c194f7SMichael S. Tsirkin * (at your option) any later version.
1372c194f7SMichael S. Tsirkin
1472c194f7SMichael S. Tsirkin * This program is distributed in the hope that it will be useful,
1572c194f7SMichael S. Tsirkin * but WITHOUT ANY WARRANTY; without even the implied warranty of
1672c194f7SMichael S. Tsirkin * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1772c194f7SMichael S. Tsirkin * GNU General Public License for more details.
1872c194f7SMichael S. Tsirkin
1972c194f7SMichael S. Tsirkin * You should have received a copy of the GNU General Public License along
2072c194f7SMichael S. Tsirkin * with this program; if not, see <http://www.gnu.org/licenses/>.
2172c194f7SMichael S. Tsirkin */
2272c194f7SMichael S. Tsirkin
23b6a0aa05SPeter Maydell #include "qemu/osdep.h"
24da34e65cSMarkus Armbruster #include "qapi/error.h"
2515280c36SMarkus Armbruster #include "qapi/qmp/qnum.h"
2672c194f7SMichael S. Tsirkin #include "acpi-build.h"
27eb66ffabSGerd Hoffmann #include "acpi-common.h"
2872c194f7SMichael S. Tsirkin #include "qemu/bitmap.h"
2907fb6176SPaolo Bonzini #include "qemu/error-report.h"
30674b0a57SMarkus Armbruster #include "hw/pci/pci_bridge.h"
316e4e3ae9SBen Widawsky #include "hw/cxl/cxl.h"
322e5b09fdSMarkus Armbruster #include "hw/core/cpu.h"
33fcf5ef2aSThomas Huth #include "target/i386/cpu.h"
3472c194f7SMichael S. Tsirkin #include "hw/timer/hpet.h"
35395e5fb4SShannon Zhao #include "hw/acpi/acpi-defs.h"
3672c194f7SMichael S. Tsirkin #include "hw/acpi/acpi.h"
37679dd1a9SIgor Mammedov #include "hw/acpi/cpu.h"
3872c194f7SMichael S. Tsirkin #include "hw/nvram/fw_cfg.h"
390058ae1dSMichael S. Tsirkin #include "hw/acpi/bios-linker-loader.h"
405876d9b5SIgor Mammedov #include "hw/acpi/acpi_aml_interface.h"
415334bf57SLiav Albani #include "hw/input/i8042.h"
42bef3492dSIgor Mammedov #include "hw/acpi/memory_hotplug.h"
43711b20b4SStefan Berger #include "sysemu/tpm.h"
44711b20b4SStefan Berger #include "hw/acpi/tpm.h"
45d03637bcSBen Warren #include "hw/acpi/vmgenid.h"
468486f12fSEric DeVolder #include "hw/acpi/erst.h"
472bfd0845SMark Cave-Ayland #include "hw/acpi/piix4.h"
485cb18b3dSStefan Berger #include "sysemu/tpm_backend.h"
49bcdb9064SPhilippe Mathieu-Daudé #include "hw/rtc/mc146818rtc_regs.h"
50d6454270SMarkus Armbruster #include "migration/vmstate.h"
512cc0e2e8SDavid Hildenbrand #include "hw/mem/memory-device.h"
524b997690SPhilippe Mathieu-Daudé #include "hw/mem/nvdimm.h"
531f3aba37SIgor Mammedov #include "sysemu/numa.h"
5471e8a915SMarkus Armbruster #include "sysemu/reset.h"
556775d15dSJon Doron #include "hw/hyperv/vmbus-bridge.h"
5672c194f7SMichael S. Tsirkin
5772c194f7SMichael S. Tsirkin /* Supported chipsets: */
581a6981bbSBernhard Beschow #include "hw/southbridge/ich9.h"
5999fd437dSMichael S. Tsirkin #include "hw/acpi/pcihp.h"
6089a289c7SPaolo Bonzini #include "hw/i386/fw_cfg.h"
6171671814SBernhard Beschow #include "hw/i386/pc.h"
6272c194f7SMichael S. Tsirkin #include "hw/pci/pci_bus.h"
63b496a17dSBernhard Beschow #include "hw/pci-host/i440fx.h"
6472c194f7SMichael S. Tsirkin #include "hw/pci-host/q35.h"
651cf5fd57SPeter Xu #include "hw/i386/x86-iommu.h"
6672c194f7SMichael S. Tsirkin
6719934e0eSIgor Mammedov #include "hw/acpi/aml-build.h"
6882f76c67SWei Yang #include "hw/acpi/utils.h"
6948cefd94SWei Yang #include "hw/acpi/pci.h"
702a3282c6SBen Widawsky #include "hw/acpi/cxl.h"
7119934e0eSIgor Mammedov
7272c194f7SMichael S. Tsirkin #include "qom/qom-qobject.h"
73fb9f5926SDavid Kiarie #include "hw/i386/amd_iommu.h"
74fb9f5926SDavid Kiarie #include "hw/i386/intel_iommu.h"
7536efa250SJean-Philippe Brucker #include "hw/virtio/virtio-iommu.h"
7672c194f7SMichael S. Tsirkin
77e6f123c3SLiu Jingqi #include "hw/acpi/hmat.h"
7836efa250SJean-Philippe Brucker #include "hw/acpi/viot.h"
7986e91dd7SCorey Minyard
808486f12fSEric DeVolder #include CONFIG_DEVICES
818486f12fSEric DeVolder
8207fb6176SPaolo Bonzini /* These are used to size the ACPI tables for -M pc-i440fx-1.7 and
8307fb6176SPaolo Bonzini * -M pc-i440fx-2.0. Even if the actual amount of AML generated grows
8407fb6176SPaolo Bonzini * a little bit, there should be plenty of free space since the DSDT
8507fb6176SPaolo Bonzini * shrunk by ~1.5k between QEMU 2.0 and QEMU 2.1.
8607fb6176SPaolo Bonzini */
8707fb6176SPaolo Bonzini #define ACPI_BUILD_ALIGN_SIZE 0x1000
8807fb6176SPaolo Bonzini
89868270f2SMichael S. Tsirkin #define ACPI_BUILD_TABLE_SIZE 0x20000
9018045fb9SPaolo Bonzini
918b310fc4SGonglei /* #define DEBUG_ACPI_BUILD */
928b310fc4SGonglei #ifdef DEBUG_ACPI_BUILD
938b310fc4SGonglei #define ACPI_BUILD_DPRINTF(fmt, ...) \
948b310fc4SGonglei do {printf("ACPI_BUILD: " fmt, ## __VA_ARGS__); } while (0)
958b310fc4SGonglei #else
968b310fc4SGonglei #define ACPI_BUILD_DPRINTF(fmt, ...)
978b310fc4SGonglei #endif
988b310fc4SGonglei
9972c194f7SMichael S. Tsirkin typedef struct AcpiPmInfo {
10072c194f7SMichael S. Tsirkin bool s3_disabled;
10172c194f7SMichael S. Tsirkin bool s4_disabled;
102133a2da4SIgor Mammedov bool pcihp_bridge_en;
1036d837f1fSIgor Mammedov bool smi_on_cpuhp;
104892aae74SIgor Mammedov bool smi_on_cpu_unplug;
105df4008c9SAni Sinha bool pcihp_root_en;
10672c194f7SMichael S. Tsirkin uint8_t s4_val;
107937d1b58SIgor Mammedov AcpiFadtData fadt;
108ddf1ec2fSIgor Mammedov uint16_t cpu_hp_io_base;
109500b11eaSIgor Mammedov uint16_t pcihp_io_base;
110500b11eaSIgor Mammedov uint16_t pcihp_io_len;
11172c194f7SMichael S. Tsirkin } AcpiPmInfo;
11272c194f7SMichael S. Tsirkin
11372c194f7SMichael S. Tsirkin typedef struct AcpiMiscInfo {
11472c194f7SMichael S. Tsirkin bool has_hpet;
11511fb99e6SStefan Berger #ifdef CONFIG_TPM
1165cb18b3dSStefan Berger TPMVersion tpm_version;
11711fb99e6SStefan Berger #endif
11872c194f7SMichael S. Tsirkin } AcpiMiscInfo;
11972c194f7SMichael S. Tsirkin
1200fe24669SStefan Berger typedef struct FwCfgTPMConfig {
1210fe24669SStefan Berger uint32_t tpmppi_address;
1220fe24669SStefan Berger uint8_t tpm_version;
1230fe24669SStefan Berger uint8_t tpmppi_version;
1240fe24669SStefan Berger } QEMU_PACKED FwCfgTPMConfig;
1250fe24669SStefan Berger
1264a441836SGerd Hoffmann static bool acpi_get_mcfg(AcpiMcfgInfo *mcfg);
1274a441836SGerd Hoffmann
1285c94b826SKwangwoo Lee const struct AcpiGenericAddress x86_nvdimm_acpi_dsmio = {
1295c94b826SKwangwoo Lee .space_id = AML_AS_SYSTEM_IO,
1305c94b826SKwangwoo Lee .address = NVDIMM_ACPI_IO_BASE,
1315c94b826SKwangwoo Lee .bit_width = NVDIMM_ACPI_IO_LEN << 3
1325c94b826SKwangwoo Lee };
1335c94b826SKwangwoo Lee
init_common_fadt_data(MachineState * ms,Object * o,AcpiFadtData * data)1340e11fc69SLike Xu static void init_common_fadt_data(MachineState *ms, Object *o,
1350e11fc69SLike Xu AcpiFadtData *data)
136937d1b58SIgor Mammedov {
13733b44fdaSIsaku Yamahata X86MachineState *x86ms = X86_MACHINE(ms);
13833b44fdaSIsaku Yamahata /*
13933b44fdaSIsaku Yamahata * "ICH9-LPC" or "PIIX4_PM" has "smm-compat" property to keep the old
14033b44fdaSIsaku Yamahata * behavior for compatibility irrelevant to smm_enabled, which doesn't
14133b44fdaSIsaku Yamahata * comforms to ACPI spec.
14233b44fdaSIsaku Yamahata */
14333b44fdaSIsaku Yamahata bool smm_enabled = object_property_get_bool(o, "smm-compat", NULL) ?
14433b44fdaSIsaku Yamahata true : x86_machine_is_smm_enabled(x86ms);
145937d1b58SIgor Mammedov uint32_t io = object_property_get_uint(o, ACPI_PM_PROP_PM_IO_BASE, NULL);
146937d1b58SIgor Mammedov AmlAddressSpace as = AML_AS_SYSTEM_IO;
147937d1b58SIgor Mammedov AcpiFadtData fadt = {
148937d1b58SIgor Mammedov .rev = 3,
149937d1b58SIgor Mammedov .flags =
150937d1b58SIgor Mammedov (1 << ACPI_FADT_F_WBINVD) |
151937d1b58SIgor Mammedov (1 << ACPI_FADT_F_PROC_C1) |
152937d1b58SIgor Mammedov (1 << ACPI_FADT_F_SLP_BUTTON) |
153937d1b58SIgor Mammedov (1 << ACPI_FADT_F_RTC_S4) |
154937d1b58SIgor Mammedov (1 << ACPI_FADT_F_USE_PLATFORM_CLOCK) |
155937d1b58SIgor Mammedov /* APIC destination mode ("Flat Logical") has an upper limit of 8
156937d1b58SIgor Mammedov * CPUs for more than 8 CPUs, "Clustered Logical" mode has to be
157937d1b58SIgor Mammedov * used
158937d1b58SIgor Mammedov */
1590e11fc69SLike Xu ((ms->smp.max_cpus > 8) ?
1600e11fc69SLike Xu (1 << ACPI_FADT_F_FORCE_APIC_CLUSTER_MODEL) : 0),
161937d1b58SIgor Mammedov .int_model = 1 /* Multiple APIC */,
162937d1b58SIgor Mammedov .rtc_century = RTC_CENTURY,
163937d1b58SIgor Mammedov .plvl2_lat = 0xfff /* C2 state not supported */,
164937d1b58SIgor Mammedov .plvl3_lat = 0xfff /* C3 state not supported */,
16533b44fdaSIsaku Yamahata .smi_cmd = smm_enabled ? ACPI_PORT_SMI_CMD : 0,
166937d1b58SIgor Mammedov .sci_int = object_property_get_uint(o, ACPI_PM_PROP_SCI_INT, NULL),
167937d1b58SIgor Mammedov .acpi_enable_cmd =
16833b44fdaSIsaku Yamahata smm_enabled ?
16933b44fdaSIsaku Yamahata object_property_get_uint(o, ACPI_PM_PROP_ACPI_ENABLE_CMD, NULL) :
17033b44fdaSIsaku Yamahata 0,
171937d1b58SIgor Mammedov .acpi_disable_cmd =
17233b44fdaSIsaku Yamahata smm_enabled ?
17333b44fdaSIsaku Yamahata object_property_get_uint(o, ACPI_PM_PROP_ACPI_DISABLE_CMD, NULL) :
17433b44fdaSIsaku Yamahata 0,
175937d1b58SIgor Mammedov .pm1a_evt = { .space_id = as, .bit_width = 4 * 8, .address = io },
176937d1b58SIgor Mammedov .pm1a_cnt = { .space_id = as, .bit_width = 2 * 8,
177937d1b58SIgor Mammedov .address = io + 0x04 },
178937d1b58SIgor Mammedov .pm_tmr = { .space_id = as, .bit_width = 4 * 8, .address = io + 0x08 },
179937d1b58SIgor Mammedov .gpe0_blk = { .space_id = as, .bit_width =
180937d1b58SIgor Mammedov object_property_get_uint(o, ACPI_PM_PROP_GPE0_BLK_LEN, NULL) * 8,
181937d1b58SIgor Mammedov .address = object_property_get_uint(o, ACPI_PM_PROP_GPE0_BLK, NULL)
182937d1b58SIgor Mammedov },
183937d1b58SIgor Mammedov };
1845334bf57SLiav Albani
1855334bf57SLiav Albani /*
1865334bf57SLiav Albani * ACPI v2, Table 5-10 - Fixed ACPI Description Table Boot Architecture
1875334bf57SLiav Albani * Flags, bit offset 1 - 8042.
1885334bf57SLiav Albani */
1895334bf57SLiav Albani fadt.iapc_boot_arch = iapc_boot_arch_8042();
1905334bf57SLiav Albani
191937d1b58SIgor Mammedov *data = fadt;
192937d1b58SIgor Mammedov }
193937d1b58SIgor Mammedov
acpi_get_pm_info(MachineState * machine,AcpiPmInfo * pm)1940e11fc69SLike Xu static void acpi_get_pm_info(MachineState *machine, AcpiPmInfo *pm)
19572c194f7SMichael S. Tsirkin {
196ee3b34cdSPaolo Bonzini Object *piix = object_resolve_type_unambiguous(TYPE_PIIX4_PM, NULL);
197ee3b34cdSPaolo Bonzini Object *lpc = object_resolve_type_unambiguous(TYPE_ICH9_LPC_DEVICE, NULL);
198697155cdSIgor Mammedov Object *obj = piix ? piix : lpc;
19972c194f7SMichael S. Tsirkin QObject *o;
20094aaca64SDaniel P. Berrange pm->cpu_hp_io_base = 0;
201500b11eaSIgor Mammedov pm->pcihp_io_base = 0;
202500b11eaSIgor Mammedov pm->pcihp_io_len = 0;
2036d837f1fSIgor Mammedov pm->smi_on_cpuhp = false;
204892aae74SIgor Mammedov pm->smi_on_cpu_unplug = false;
205937d1b58SIgor Mammedov
2066fa5171fSPhilippe Mathieu-Daudé assert(obj);
207a0628599SLike Xu init_common_fadt_data(machine, obj, &pm->fadt);
20872c194f7SMichael S. Tsirkin if (piix) {
2093a3fcc75SIgor Mammedov /* w2k requires FADT(rev1) or it won't boot, keep PC compatible */
210937d1b58SIgor Mammedov pm->fadt.rev = 1;
211ddf1ec2fSIgor Mammedov pm->cpu_hp_io_base = PIIX4_CPU_HOTPLUG_IO_BASE;
21272c194f7SMichael S. Tsirkin }
21372c194f7SMichael S. Tsirkin if (lpc) {
2146d837f1fSIgor Mammedov uint64_t smi_features = object_property_get_uint(lpc,
2156d837f1fSIgor Mammedov ICH9_LPC_SMI_NEGOTIATED_FEAT_PROP, NULL);
216937d1b58SIgor Mammedov struct AcpiGenericAddress r = { .space_id = AML_AS_SYSTEM_IO,
217937d1b58SIgor Mammedov .bit_width = 8, .address = ICH9_RST_CNT_IOPORT };
218937d1b58SIgor Mammedov pm->fadt.reset_reg = r;
219937d1b58SIgor Mammedov pm->fadt.reset_val = 0xf;
220937d1b58SIgor Mammedov pm->fadt.flags |= 1 << ACPI_FADT_F_RESET_REG_SUP;
221ddf1ec2fSIgor Mammedov pm->cpu_hp_io_base = ICH9_CPU_HOTPLUG_IO_BASE;
2226d837f1fSIgor Mammedov pm->smi_on_cpuhp =
2236d837f1fSIgor Mammedov !!(smi_features & BIT_ULL(ICH9_LPC_SMI_F_CPU_HOTPLUG_BIT));
224892aae74SIgor Mammedov pm->smi_on_cpu_unplug =
225892aae74SIgor Mammedov !!(smi_features & BIT_ULL(ICH9_LPC_SMI_F_CPU_HOT_UNPLUG_BIT));
22672c194f7SMichael S. Tsirkin }
227caf108bcSJulia Suvorova pm->pcihp_io_base =
228caf108bcSJulia Suvorova object_property_get_uint(obj, ACPI_PCIHP_IO_BASE_PROP, NULL);
229caf108bcSJulia Suvorova pm->pcihp_io_len =
230caf108bcSJulia Suvorova object_property_get_uint(obj, ACPI_PCIHP_IO_LEN_PROP, NULL);
23172c194f7SMichael S. Tsirkin
23272c194f7SMichael S. Tsirkin /* Fill in optional s3/s4 related properties */
23372c194f7SMichael S. Tsirkin o = object_property_get_qobject(obj, ACPI_PM_PROP_S3_DISABLED, NULL);
23472c194f7SMichael S. Tsirkin if (o) {
2357dc847ebSMax Reitz pm->s3_disabled = qnum_get_uint(qobject_to(QNum, o));
23672c194f7SMichael S. Tsirkin } else {
23772c194f7SMichael S. Tsirkin pm->s3_disabled = false;
23872c194f7SMichael S. Tsirkin }
239cb3e7f08SMarc-André Lureau qobject_unref(o);
24072c194f7SMichael S. Tsirkin o = object_property_get_qobject(obj, ACPI_PM_PROP_S4_DISABLED, NULL);
24172c194f7SMichael S. Tsirkin if (o) {
2427dc847ebSMax Reitz pm->s4_disabled = qnum_get_uint(qobject_to(QNum, o));
24372c194f7SMichael S. Tsirkin } else {
24472c194f7SMichael S. Tsirkin pm->s4_disabled = false;
24572c194f7SMichael S. Tsirkin }
246cb3e7f08SMarc-André Lureau qobject_unref(o);
24772c194f7SMichael S. Tsirkin o = object_property_get_qobject(obj, ACPI_PM_PROP_S4_VAL, NULL);
24872c194f7SMichael S. Tsirkin if (o) {
2497dc847ebSMax Reitz pm->s4_val = qnum_get_uint(qobject_to(QNum, o));
25072c194f7SMichael S. Tsirkin } else {
25172c194f7SMichael S. Tsirkin pm->s4_val = false;
25272c194f7SMichael S. Tsirkin }
253cb3e7f08SMarc-André Lureau qobject_unref(o);
25472c194f7SMichael S. Tsirkin
255133a2da4SIgor Mammedov pm->pcihp_bridge_en =
256aa29466bSAni Sinha object_property_get_bool(obj, ACPI_PM_PROP_ACPI_PCIHP_BRIDGE,
257133a2da4SIgor Mammedov NULL);
258df4008c9SAni Sinha pm->pcihp_root_en =
259aa29466bSAni Sinha object_property_get_bool(obj, ACPI_PM_PROP_ACPI_PCI_ROOTHP,
260df4008c9SAni Sinha NULL);
26172c194f7SMichael S. Tsirkin }
26272c194f7SMichael S. Tsirkin
acpi_get_misc_info(AcpiMiscInfo * info)26372c194f7SMichael S. Tsirkin static void acpi_get_misc_info(AcpiMiscInfo *info)
26472c194f7SMichael S. Tsirkin {
26572c194f7SMichael S. Tsirkin info->has_hpet = hpet_find();
26611fb99e6SStefan Berger #ifdef CONFIG_TPM
2673dfd5a2aSMarc-André Lureau info->tpm_version = tpm_get_version(tpm_find());
26811fb99e6SStefan Berger #endif
26972c194f7SMichael S. Tsirkin }
27072c194f7SMichael S. Tsirkin
271ca6c1855SMarcel Apfelbaum /*
272ca6c1855SMarcel Apfelbaum * Because of the PXB hosts we cannot simply query TYPE_PCI_HOST_BRIDGE.
273ca6c1855SMarcel Apfelbaum * On i386 arch we only have two pci hosts, so we can look only for them.
274ca6c1855SMarcel Apfelbaum */
acpi_get_i386_pci_host(void)275c0e427d6SJulia Suvorova Object *acpi_get_i386_pci_host(void)
276ca6c1855SMarcel Apfelbaum {
277ca6c1855SMarcel Apfelbaum PCIHostState *host;
278ca6c1855SMarcel Apfelbaum
279b914e741SEduardo Habkost host = PCI_HOST_BRIDGE(object_resolve_path("/machine/i440fx", NULL));
280ca6c1855SMarcel Apfelbaum if (!host) {
281b914e741SEduardo Habkost host = PCI_HOST_BRIDGE(object_resolve_path("/machine/q35", NULL));
282ca6c1855SMarcel Apfelbaum }
283ca6c1855SMarcel Apfelbaum
284ca6c1855SMarcel Apfelbaum return OBJECT(host);
285ca6c1855SMarcel Apfelbaum }
286ca6c1855SMarcel Apfelbaum
acpi_get_pci_holes(Range * hole,Range * hole64)28701c9742dSMarkus Armbruster static void acpi_get_pci_holes(Range *hole, Range *hole64)
28872c194f7SMichael S. Tsirkin {
28972c194f7SMichael S. Tsirkin Object *pci_host;
29072c194f7SMichael S. Tsirkin
291ca6c1855SMarcel Apfelbaum pci_host = acpi_get_i386_pci_host();
292c0e427d6SJulia Suvorova
293c0e427d6SJulia Suvorova if (!pci_host) {
294c0e427d6SJulia Suvorova return;
295c0e427d6SJulia Suvorova }
29672c194f7SMichael S. Tsirkin
297a0efbf16SMarkus Armbruster range_set_bounds1(hole,
29860555365SMarc-André Lureau object_property_get_uint(pci_host,
29972c194f7SMichael S. Tsirkin PCI_HOST_PROP_PCI_HOLE_START,
300a0efbf16SMarkus Armbruster NULL),
30160555365SMarc-André Lureau object_property_get_uint(pci_host,
30272c194f7SMichael S. Tsirkin PCI_HOST_PROP_PCI_HOLE_END,
303a0efbf16SMarkus Armbruster NULL));
304a0efbf16SMarkus Armbruster range_set_bounds1(hole64,
30560555365SMarc-André Lureau object_property_get_uint(pci_host,
30672c194f7SMichael S. Tsirkin PCI_HOST_PROP_PCI_HOLE64_START,
307a0efbf16SMarkus Armbruster NULL),
30860555365SMarc-André Lureau object_property_get_uint(pci_host,
30972c194f7SMichael S. Tsirkin PCI_HOST_PROP_PCI_HOLE64_END,
310a0efbf16SMarkus Armbruster NULL));
31172c194f7SMichael S. Tsirkin }
31272c194f7SMichael S. Tsirkin
acpi_align_size(GArray * blob,unsigned align)31372c194f7SMichael S. Tsirkin static void acpi_align_size(GArray *blob, unsigned align)
31472c194f7SMichael S. Tsirkin {
31572c194f7SMichael S. Tsirkin /* Align size to multiple of given size. This reduces the chance
31672c194f7SMichael S. Tsirkin * we need to change size in the future (breaking cross version migration).
31772c194f7SMichael S. Tsirkin */
318134d42d6SMichael S. Tsirkin g_array_set_size(blob, ROUND_UP(acpi_data_len(blob), align));
31972c194f7SMichael S. Tsirkin }
32072c194f7SMichael S. Tsirkin
321cf68410bSIgor Mammedov /*
322cf68410bSIgor Mammedov * ACPI spec 1.0b,
323cf68410bSIgor Mammedov * 5.2.6 Firmware ACPI Control Structure
324cf68410bSIgor Mammedov */
32572c194f7SMichael S. Tsirkin static void
build_facs(GArray * table_data)326009180bdSWei Yang build_facs(GArray *table_data)
32772c194f7SMichael S. Tsirkin {
328cf68410bSIgor Mammedov const char *sig = "FACS";
329cf68410bSIgor Mammedov const uint8_t reserved[40] = {};
330cf68410bSIgor Mammedov
331cf68410bSIgor Mammedov g_array_append_vals(table_data, sig, 4); /* Signature */
332cf68410bSIgor Mammedov build_append_int_noprefix(table_data, 64, 4); /* Length */
333cf68410bSIgor Mammedov build_append_int_noprefix(table_data, 0, 4); /* Hardware Signature */
334cf68410bSIgor Mammedov build_append_int_noprefix(table_data, 0, 4); /* Firmware Waking Vector */
335cf68410bSIgor Mammedov build_append_int_noprefix(table_data, 0, 4); /* Global Lock */
336cf68410bSIgor Mammedov build_append_int_noprefix(table_data, 0, 4); /* Flags */
337cf68410bSIgor Mammedov g_array_append_vals(table_data, reserved, 40); /* Reserved */
33872c194f7SMichael S. Tsirkin }
33972c194f7SMichael S. Tsirkin
aml_pci_device_dsm(void)3405840a163SIgor Mammedov Aml *aml_pci_device_dsm(void)
3415840a163SIgor Mammedov {
3425840a163SIgor Mammedov Aml *method;
3435840a163SIgor Mammedov
3445840a163SIgor Mammedov method = aml_method("_DSM", 4, AML_SERIALIZED);
3455840a163SIgor Mammedov {
3465840a163SIgor Mammedov Aml *params = aml_local(0);
3475840a163SIgor Mammedov Aml *pkg = aml_package(2);
34844d975efSIgor Mammedov aml_append(pkg, aml_int(0));
34944d975efSIgor Mammedov aml_append(pkg, aml_int(0));
3505840a163SIgor Mammedov aml_append(method, aml_store(pkg, params));
3515840a163SIgor Mammedov aml_append(method,
35244d975efSIgor Mammedov aml_store(aml_name("BSEL"), aml_index(params, aml_int(0))));
35344d975efSIgor Mammedov aml_append(method,
35444d975efSIgor Mammedov aml_store(aml_name("ASUN"), aml_index(params, aml_int(1))));
35544d975efSIgor Mammedov aml_append(method,
3565840a163SIgor Mammedov aml_return(aml_call5("PDSM", aml_arg(0), aml_arg(1),
3575840a163SIgor Mammedov aml_arg(2), aml_arg(3), params))
3585840a163SIgor Mammedov );
3595840a163SIgor Mammedov }
3605840a163SIgor Mammedov return method;
3615840a163SIgor Mammedov }
3625840a163SIgor Mammedov
build_append_pci_dsm_func0_common(Aml * ctx,Aml * retvar)3630a4584fcSIgor Mammedov static void build_append_pci_dsm_func0_common(Aml *ctx, Aml *retvar)
3640a4584fcSIgor Mammedov {
3650a4584fcSIgor Mammedov Aml *UUID, *ifctx1;
3660a4584fcSIgor Mammedov uint8_t byte_list[1] = { 0 }; /* nothing supported yet */
3670a4584fcSIgor Mammedov
3680a4584fcSIgor Mammedov aml_append(ctx, aml_store(aml_buffer(1, byte_list), retvar));
3690a4584fcSIgor Mammedov /*
3700a4584fcSIgor Mammedov * PCI Firmware Specification 3.1
3710a4584fcSIgor Mammedov * 4.6. _DSM Definitions for PCI
3720a4584fcSIgor Mammedov */
3730a4584fcSIgor Mammedov UUID = aml_touuid("E5C937D0-3553-4D7A-9117-EA4D19C3434D");
3740a4584fcSIgor Mammedov ifctx1 = aml_if(aml_lnot(aml_equal(aml_arg(0), UUID)));
3750a4584fcSIgor Mammedov {
3760a4584fcSIgor Mammedov /* call is for unsupported UUID, bail out */
3770a4584fcSIgor Mammedov aml_append(ifctx1, aml_return(retvar));
3780a4584fcSIgor Mammedov }
3790a4584fcSIgor Mammedov aml_append(ctx, ifctx1);
3800a4584fcSIgor Mammedov
3810a4584fcSIgor Mammedov ifctx1 = aml_if(aml_lless(aml_arg(1), aml_int(2)));
3820a4584fcSIgor Mammedov {
3830a4584fcSIgor Mammedov /* call is for unsupported REV, bail out */
3840a4584fcSIgor Mammedov aml_append(ifctx1, aml_return(retvar));
3850a4584fcSIgor Mammedov }
3860a4584fcSIgor Mammedov aml_append(ctx, ifctx1);
3870a4584fcSIgor Mammedov }
3880a4584fcSIgor Mammedov
aml_pci_edsm(void)389fe0d5f53SIgor Mammedov static Aml *aml_pci_edsm(void)
390fe0d5f53SIgor Mammedov {
391fe0d5f53SIgor Mammedov Aml *method, *ifctx;
392fe0d5f53SIgor Mammedov Aml *zero = aml_int(0);
393fe0d5f53SIgor Mammedov Aml *func = aml_arg(2);
394fe0d5f53SIgor Mammedov Aml *ret = aml_local(0);
395fe0d5f53SIgor Mammedov Aml *aidx = aml_local(1);
396fe0d5f53SIgor Mammedov Aml *params = aml_arg(4);
397fe0d5f53SIgor Mammedov
398fe0d5f53SIgor Mammedov method = aml_method("EDSM", 5, AML_SERIALIZED);
399fe0d5f53SIgor Mammedov
400fe0d5f53SIgor Mammedov /* get supported functions */
401fe0d5f53SIgor Mammedov ifctx = aml_if(aml_equal(func, zero));
402fe0d5f53SIgor Mammedov {
403fe0d5f53SIgor Mammedov /* 1: have supported functions */
404fe0d5f53SIgor Mammedov /* 7: support for function 7 */
405fe0d5f53SIgor Mammedov const uint8_t caps = 1 | BIT(7);
406fe0d5f53SIgor Mammedov build_append_pci_dsm_func0_common(ifctx, ret);
407fe0d5f53SIgor Mammedov aml_append(ifctx, aml_store(aml_int(caps), aml_index(ret, zero)));
408fe0d5f53SIgor Mammedov aml_append(ifctx, aml_return(ret));
409fe0d5f53SIgor Mammedov }
410fe0d5f53SIgor Mammedov aml_append(method, ifctx);
411fe0d5f53SIgor Mammedov
412fe0d5f53SIgor Mammedov /* handle specific functions requests */
413fe0d5f53SIgor Mammedov /*
414fe0d5f53SIgor Mammedov * PCI Firmware Specification 3.1
415fe0d5f53SIgor Mammedov * 4.6.7. _DSM for Naming a PCI or PCI Express Device Under
416fe0d5f53SIgor Mammedov * Operating Systems
417fe0d5f53SIgor Mammedov */
418fe0d5f53SIgor Mammedov ifctx = aml_if(aml_equal(func, aml_int(7)));
419fe0d5f53SIgor Mammedov {
420fe0d5f53SIgor Mammedov Aml *pkg = aml_package(2);
421fe0d5f53SIgor Mammedov aml_append(pkg, zero);
422fe0d5f53SIgor Mammedov /* optional, if not impl. should return null string */
423fe0d5f53SIgor Mammedov aml_append(pkg, aml_string("%s", ""));
424fe0d5f53SIgor Mammedov aml_append(ifctx, aml_store(pkg, ret));
425fe0d5f53SIgor Mammedov
426fe0d5f53SIgor Mammedov /*
427fe0d5f53SIgor Mammedov * IASL is fine when initializing Package with computational data,
428fe0d5f53SIgor Mammedov * however it makes guest unhappy /it fails to process such AML/.
429fe0d5f53SIgor Mammedov * So use runtime assignment to set acpi-index after initializer
430fe0d5f53SIgor Mammedov * to make OSPM happy.
431fe0d5f53SIgor Mammedov */
432fe0d5f53SIgor Mammedov aml_append(ifctx,
433fe0d5f53SIgor Mammedov aml_store(aml_derefof(aml_index(params, aml_int(0))), aidx));
434fe0d5f53SIgor Mammedov aml_append(ifctx, aml_store(aidx, aml_index(ret, zero)));
435fe0d5f53SIgor Mammedov aml_append(ifctx, aml_return(ret));
436fe0d5f53SIgor Mammedov }
437fe0d5f53SIgor Mammedov aml_append(method, ifctx);
438fe0d5f53SIgor Mammedov
439fe0d5f53SIgor Mammedov return method;
440fe0d5f53SIgor Mammedov }
4410a4584fcSIgor Mammedov
aml_pci_static_endpoint_dsm(PCIDevice * pdev)4427fb1d738SIgor Mammedov static Aml *aml_pci_static_endpoint_dsm(PCIDevice *pdev)
4437fb1d738SIgor Mammedov {
4447fb1d738SIgor Mammedov Aml *method;
4457fb1d738SIgor Mammedov
4467fb1d738SIgor Mammedov g_assert(pdev->acpi_index != 0);
4477fb1d738SIgor Mammedov method = aml_method("_DSM", 4, AML_SERIALIZED);
4487fb1d738SIgor Mammedov {
4497fb1d738SIgor Mammedov Aml *params = aml_local(0);
4507fb1d738SIgor Mammedov Aml *pkg = aml_package(1);
4517fb1d738SIgor Mammedov aml_append(pkg, aml_int(pdev->acpi_index));
4527fb1d738SIgor Mammedov aml_append(method, aml_store(pkg, params));
4537fb1d738SIgor Mammedov aml_append(method,
4547fb1d738SIgor Mammedov aml_return(aml_call5("EDSM", aml_arg(0), aml_arg(1),
4557fb1d738SIgor Mammedov aml_arg(2), aml_arg(3), params))
4567fb1d738SIgor Mammedov );
4577fb1d738SIgor Mammedov }
4587fb1d738SIgor Mammedov return method;
4597fb1d738SIgor Mammedov }
4607fb1d738SIgor Mammedov
build_append_pcihp_notify_entry(Aml * method,int slot)46162b52c26SIgor Mammedov static void build_append_pcihp_notify_entry(Aml *method, int slot)
462b23046abSIgor Mammedov {
46362b52c26SIgor Mammedov Aml *if_ctx;
46462b52c26SIgor Mammedov int32_t devfn = PCI_DEVFN(slot, 0);
465b23046abSIgor Mammedov
4665530427fSIgor Mammedov if_ctx = aml_if(aml_and(aml_arg(0), aml_int(0x1U << slot), NULL));
46762b52c26SIgor Mammedov aml_append(if_ctx, aml_notify(aml_name("S%.02X", devfn), aml_arg(1)));
46862b52c26SIgor Mammedov aml_append(method, if_ctx);
469b23046abSIgor Mammedov }
470b23046abSIgor Mammedov
is_devfn_ignored_generic(const int devfn,const PCIBus * bus)4716fe5518eSIgor Mammedov static bool is_devfn_ignored_generic(const int devfn, const PCIBus *bus)
472a06c15a3SIgor Mammedov {
473a06c15a3SIgor Mammedov const PCIDevice *pdev = bus->devices[devfn];
474a06c15a3SIgor Mammedov
475a06c15a3SIgor Mammedov if (PCI_FUNC(devfn)) {
476a06c15a3SIgor Mammedov if (IS_PCI_BRIDGE(pdev)) {
477a06c15a3SIgor Mammedov /*
478a06c15a3SIgor Mammedov * Ignore only hotplugged PCI bridges on !0 functions, but
479a06c15a3SIgor Mammedov * allow describing cold plugged bridges on all functions
480a06c15a3SIgor Mammedov */
481a06c15a3SIgor Mammedov if (DEVICE(pdev)->hotplugged) {
482a06c15a3SIgor Mammedov return true;
483a06c15a3SIgor Mammedov }
484a06c15a3SIgor Mammedov }
485a06c15a3SIgor Mammedov }
4866fe5518eSIgor Mammedov return false;
4876fe5518eSIgor Mammedov }
4886fe5518eSIgor Mammedov
is_devfn_ignored_hotplug(const int devfn,const PCIBus * bus)4896fe5518eSIgor Mammedov static bool is_devfn_ignored_hotplug(const int devfn, const PCIBus *bus)
4906fe5518eSIgor Mammedov {
49164a55106SIgor Mammedov PCIDevice *pdev = bus->devices[devfn];
49264a55106SIgor Mammedov if (pdev) {
49364a55106SIgor Mammedov return is_devfn_ignored_generic(devfn, bus) ||
49417f4cedbSIgor Mammedov !DEVICE_GET_CLASS(pdev)->hotpluggable ||
49564a55106SIgor Mammedov /* Cold plugged bridges aren't themselves hot-pluggable */
49664a55106SIgor Mammedov (IS_PCI_BRIDGE(pdev) && !DEVICE(pdev)->hotplugged);
497a06c15a3SIgor Mammedov } else { /* non populated slots */
498a06c15a3SIgor Mammedov /*
499a06c15a3SIgor Mammedov * hotplug is supported only for non-multifunction device
500a06c15a3SIgor Mammedov * so generate device description only for function 0
501a06c15a3SIgor Mammedov */
5026fe5518eSIgor Mammedov if (PCI_FUNC(devfn) ||
503a06c15a3SIgor Mammedov (pci_bus_is_express(bus) && PCI_SLOT(devfn) > 0)) {
504a06c15a3SIgor Mammedov return true;
505a06c15a3SIgor Mammedov }
506a06c15a3SIgor Mammedov }
507a06c15a3SIgor Mammedov return false;
508a06c15a3SIgor Mammedov }
509a06c15a3SIgor Mammedov
build_append_pcihp_slots(Aml * parent_scope,PCIBus * bus)51002c10613SIgor Mammedov void build_append_pcihp_slots(Aml *parent_scope, PCIBus *bus)
51199fd437dSMichael S. Tsirkin {
512d7346e61SIgor Mammedov int devfn;
5136fe5518eSIgor Mammedov Aml *dev, *notify_method = NULL, *method;
51462dd55fcSIgor Mammedov QObject *bsel = object_property_get_qobject(OBJECT(bus),
51562dd55fcSIgor Mammedov ACPI_PCIHP_PROP_BSEL, NULL);
5167dc847ebSMax Reitz uint64_t bsel_val = qnum_get_uint(qobject_to(QNum, bsel));
51762dd55fcSIgor Mammedov qobject_unref(bsel);
51862b52c26SIgor Mammedov
51962b52c26SIgor Mammedov aml_append(parent_scope, aml_name_decl("BSEL", aml_int(bsel_val)));
5204dbfc881SXiao Guangrong notify_method = aml_method("DVNT", 2, AML_NOTSERIALIZED);
52199fd437dSMichael S. Tsirkin
522d7346e61SIgor Mammedov for (devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
523d7346e61SIgor Mammedov int slot = PCI_SLOT(devfn);
5246fe5518eSIgor Mammedov int adr = slot << 16 | PCI_FUNC(devfn);
525a06c15a3SIgor Mammedov
5266fe5518eSIgor Mammedov if (is_devfn_ignored_hotplug(devfn, bus)) {
527a06c15a3SIgor Mammedov continue;
528a06c15a3SIgor Mammedov }
52999fd437dSMichael S. Tsirkin
53017f4cedbSIgor Mammedov if (bus->devices[devfn]) {
5316fe5518eSIgor Mammedov dev = aml_scope("S%.02X", devfn);
5326fe5518eSIgor Mammedov } else {
533d7346e61SIgor Mammedov dev = aml_device("S%.02X", devfn);
534d7346e61SIgor Mammedov aml_append(dev, aml_name_decl("_ADR", aml_int(adr)));
5356fe5518eSIgor Mammedov }
53662b52c26SIgor Mammedov
5377193d7cdSIgor Mammedov /*
5387193d7cdSIgor Mammedov * Can't declare _SUN here for every device as it changes 'slot'
5397193d7cdSIgor Mammedov * enumeration order in linux kernel, so use another variable for it
5407193d7cdSIgor Mammedov */
5417193d7cdSIgor Mammedov aml_append(dev, aml_name_decl("ASUN", aml_int(slot)));
5425840a163SIgor Mammedov aml_append(dev, aml_pci_device_dsm());
5433216ab2aSIgor Mammedov
5443216ab2aSIgor Mammedov aml_append(dev, aml_name_decl("_SUN", aml_int(slot)));
5453216ab2aSIgor Mammedov /* add _EJ0 to make slot hotpluggable */
5463216ab2aSIgor Mammedov method = aml_method("_EJ0", 1, AML_NOTSERIALIZED);
5473216ab2aSIgor Mammedov aml_append(method,
5483216ab2aSIgor Mammedov aml_call2("PCEJ", aml_name("BSEL"), aml_name("_SUN"))
5493216ab2aSIgor Mammedov );
5503216ab2aSIgor Mammedov aml_append(dev, method);
5513216ab2aSIgor Mammedov
5523216ab2aSIgor Mammedov build_append_pcihp_notify_entry(notify_method, slot);
5533216ab2aSIgor Mammedov
554d7346e61SIgor Mammedov /* device descriptor has been composed, add it into parent context */
55562b52c26SIgor Mammedov aml_append(parent_scope, dev);
55699fd437dSMichael S. Tsirkin }
5576fe5518eSIgor Mammedov aml_append(parent_scope, notify_method);
5586fe5518eSIgor Mammedov }
5596fe5518eSIgor Mammedov
build_append_pci_bus_devices(Aml * parent_scope,PCIBus * bus)5606fe5518eSIgor Mammedov void build_append_pci_bus_devices(Aml *parent_scope, PCIBus *bus)
5616fe5518eSIgor Mammedov {
5626fe5518eSIgor Mammedov int devfn;
5636fe5518eSIgor Mammedov Aml *dev;
5646fe5518eSIgor Mammedov
5656fe5518eSIgor Mammedov for (devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
5666fe5518eSIgor Mammedov /* ACPI spec: 1.0b: Table 6-2 _ADR Object Bus Types, PCI type */
5676fe5518eSIgor Mammedov int adr = PCI_SLOT(devfn) << 16 | PCI_FUNC(devfn);
56817f4cedbSIgor Mammedov PCIDevice *pdev = bus->devices[devfn];
5696fe5518eSIgor Mammedov
57017f4cedbSIgor Mammedov if (!pdev || is_devfn_ignored_generic(devfn, bus)) {
5716fe5518eSIgor Mammedov continue;
5726fe5518eSIgor Mammedov }
5736fe5518eSIgor Mammedov
5746fe5518eSIgor Mammedov /* start to compose PCI device descriptor */
5756fe5518eSIgor Mammedov dev = aml_device("S%.02X", devfn);
5766fe5518eSIgor Mammedov aml_append(dev, aml_name_decl("_ADR", aml_int(adr)));
5776fe5518eSIgor Mammedov
5786fe5518eSIgor Mammedov call_dev_aml_func(DEVICE(bus->devices[devfn]), dev);
5797fb1d738SIgor Mammedov /* add _DSM if device has acpi-index set */
580419233b2SIgor Mammedov if (pdev->acpi_index &&
5817fb1d738SIgor Mammedov !object_property_get_bool(OBJECT(pdev), "hotpluggable",
5827fb1d738SIgor Mammedov &error_abort)) {
5837fb1d738SIgor Mammedov aml_append(dev, aml_pci_static_endpoint_dsm(pdev));
5847fb1d738SIgor Mammedov }
5856fe5518eSIgor Mammedov
5866fe5518eSIgor Mammedov /* device descriptor has been composed, add it into parent context */
5876fe5518eSIgor Mammedov aml_append(parent_scope, dev);
5886fe5518eSIgor Mammedov }
589ddab4d3fSIgor Mammedov }
590ddab4d3fSIgor Mammedov
build_append_notfication_callback(Aml * parent_scope,const PCIBus * bus)591219e638fSIgor Mammedov static bool build_append_notfication_callback(Aml *parent_scope,
592ddab4d3fSIgor Mammedov const PCIBus *bus)
593ddab4d3fSIgor Mammedov {
594ddab4d3fSIgor Mammedov Aml *method;
595ddab4d3fSIgor Mammedov PCIBus *sec;
596ddab4d3fSIgor Mammedov QObject *bsel;
597219e638fSIgor Mammedov int nr_notifiers = 0;
59811215a34SIgor Mammedov GQueue *pcnt_bus_list = g_queue_new();
599ddab4d3fSIgor Mammedov
600ddab4d3fSIgor Mammedov QLIST_FOREACH(sec, &bus->child, sibling) {
601ddab4d3fSIgor Mammedov Aml *br_scope = aml_scope("S%.02X", sec->parent_dev->devfn);
60211215a34SIgor Mammedov if (pci_bus_is_root(sec)) {
603ddab4d3fSIgor Mammedov continue;
604ddab4d3fSIgor Mammedov }
605219e638fSIgor Mammedov nr_notifiers = nr_notifiers +
606ddab4d3fSIgor Mammedov build_append_notfication_callback(br_scope, sec);
60711215a34SIgor Mammedov /*
60811215a34SIgor Mammedov * add new child scope to parent
60911215a34SIgor Mammedov * and keep track of bus that have PCNT,
61011215a34SIgor Mammedov * bus list is used later to call children PCNTs from this level PCNT
61111215a34SIgor Mammedov */
61211215a34SIgor Mammedov if (nr_notifiers) {
61311215a34SIgor Mammedov g_queue_push_tail(pcnt_bus_list, sec);
614ddab4d3fSIgor Mammedov aml_append(parent_scope, br_scope);
615ddab4d3fSIgor Mammedov }
61611215a34SIgor Mammedov }
617ddab4d3fSIgor Mammedov
61819f5052cSIgor Mammedov /*
61919f5052cSIgor Mammedov * Append PCNT method to notify about events on local and child buses.
620ddab4d3fSIgor Mammedov * ps: hostbridge might not have hotplug (bsel) enabled but might have
621ddab4d3fSIgor Mammedov * child bridges that do have bsel.
62299fd437dSMichael S. Tsirkin */
6234dbfc881SXiao Guangrong method = aml_method("PCNT", 0, AML_NOTSERIALIZED);
624811c74fbSPhilippe Mathieu-Daudé
62599fd437dSMichael S. Tsirkin /* If bus supports hotplug select it and notify about local events */
626ddab4d3fSIgor Mammedov bsel = object_property_get_qobject(OBJECT(bus), ACPI_PCIHP_PROP_BSEL, NULL);
62799fd437dSMichael S. Tsirkin if (bsel) {
6287dc847ebSMax Reitz uint64_t bsel_val = qnum_get_uint(qobject_to(QNum, bsel));
62901b2ffceSMarc-André Lureau
63062b52c26SIgor Mammedov aml_append(method, aml_store(aml_int(bsel_val), aml_name("BNUM")));
631811c74fbSPhilippe Mathieu-Daudé aml_append(method, aml_call2("DVNT", aml_name("PCIU"),
632811c74fbSPhilippe Mathieu-Daudé aml_int(1))); /* Device Check */
633811c74fbSPhilippe Mathieu-Daudé aml_append(method, aml_call2("DVNT", aml_name("PCID"),
634811c74fbSPhilippe Mathieu-Daudé aml_int(3))); /* Eject Request */
635219e638fSIgor Mammedov nr_notifiers++;
63699fd437dSMichael S. Tsirkin }
63799fd437dSMichael S. Tsirkin
63899fd437dSMichael S. Tsirkin /* Notify about child bus events in any case */
63911215a34SIgor Mammedov while ((sec = g_queue_pop_head(pcnt_bus_list))) {
64019f5052cSIgor Mammedov aml_append(method, aml_name("^S%.02X.PCNT", sec->parent_dev->devfn));
641b23046abSIgor Mammedov }
642df4008c9SAni Sinha
64362b52c26SIgor Mammedov aml_append(parent_scope, method);
644cb3e7f08SMarc-André Lureau qobject_unref(bsel);
64511215a34SIgor Mammedov g_queue_free(pcnt_bus_list);
646219e638fSIgor Mammedov return !!nr_notifiers;
64772c194f7SMichael S. Tsirkin }
64872c194f7SMichael S. Tsirkin
aml_pci_pdsm(void)6495840a163SIgor Mammedov static Aml *aml_pci_pdsm(void)
650b7f23f62SIgor Mammedov {
6510a4584fcSIgor Mammedov Aml *method, *ifctx, *ifctx1;
652a12cf692SIgor Mammedov Aml *ret = aml_local(0);
653a12cf692SIgor Mammedov Aml *caps = aml_local(1);
654a12cf692SIgor Mammedov Aml *acpi_index = aml_local(2);
655b7f23f62SIgor Mammedov Aml *zero = aml_int(0);
656a12cf692SIgor Mammedov Aml *one = aml_int(1);
657*7170aa66SIgor Mammedov Aml *not_supp = aml_int(0xFFFFFFFF);
658b7f23f62SIgor Mammedov Aml *func = aml_arg(2);
659467d099aSIgor Mammedov Aml *params = aml_arg(4);
660467d099aSIgor Mammedov Aml *bnum = aml_derefof(aml_index(params, aml_int(0)));
661467d099aSIgor Mammedov Aml *sunum = aml_derefof(aml_index(params, aml_int(1)));
662b7f23f62SIgor Mammedov
663467d099aSIgor Mammedov method = aml_method("PDSM", 5, AML_SERIALIZED);
664b7f23f62SIgor Mammedov
665a12cf692SIgor Mammedov /* get supported functions */
666a12cf692SIgor Mammedov ifctx = aml_if(aml_equal(func, zero));
667a12cf692SIgor Mammedov {
6680a4584fcSIgor Mammedov build_append_pci_dsm_func0_common(ifctx, ret);
6690a4584fcSIgor Mammedov
670a12cf692SIgor Mammedov aml_append(ifctx, aml_store(zero, caps));
671a12cf692SIgor Mammedov aml_append(ifctx,
672a12cf692SIgor Mammedov aml_store(aml_call2("AIDX", bnum, sunum), acpi_index));
673b7f23f62SIgor Mammedov /*
674b7f23f62SIgor Mammedov * advertise function 7 if device has acpi-index
675b7f23f62SIgor Mammedov * acpi_index values:
676b7f23f62SIgor Mammedov * 0: not present (default value)
677b7f23f62SIgor Mammedov * FFFFFFFF: not supported (old QEMU without PIDX reg)
678b7f23f62SIgor Mammedov * other: device's acpi-index
679b7f23f62SIgor Mammedov */
680a12cf692SIgor Mammedov ifctx1 = aml_if(aml_lnot(
681b7f23f62SIgor Mammedov aml_or(aml_equal(acpi_index, zero),
682*7170aa66SIgor Mammedov aml_equal(acpi_index, not_supp), NULL)
683b7f23f62SIgor Mammedov ));
684b7f23f62SIgor Mammedov {
685a12cf692SIgor Mammedov /* have supported functions */
686a12cf692SIgor Mammedov aml_append(ifctx1, aml_or(caps, one, caps));
687a12cf692SIgor Mammedov /* support for function 7 */
688a12cf692SIgor Mammedov aml_append(ifctx1,
689a12cf692SIgor Mammedov aml_or(caps, aml_shiftleft(one, aml_int(7)), caps));
690b7f23f62SIgor Mammedov }
691b7f23f62SIgor Mammedov aml_append(ifctx, ifctx1);
692a12cf692SIgor Mammedov
693a12cf692SIgor Mammedov aml_append(ifctx, aml_store(caps, aml_index(ret, zero)));
694a12cf692SIgor Mammedov aml_append(ifctx, aml_return(ret));
695a12cf692SIgor Mammedov }
696a12cf692SIgor Mammedov aml_append(method, ifctx);
697a12cf692SIgor Mammedov
698a12cf692SIgor Mammedov /* handle specific functions requests */
699b7f23f62SIgor Mammedov /*
700b7f23f62SIgor Mammedov * PCI Firmware Specification 3.1
701b7f23f62SIgor Mammedov * 4.6.7. _DSM for Naming a PCI or PCI Express Device Under
702b7f23f62SIgor Mammedov * Operating Systems
703b7f23f62SIgor Mammedov */
704a12cf692SIgor Mammedov ifctx = aml_if(aml_equal(func, aml_int(7)));
705b7f23f62SIgor Mammedov {
706b7f23f62SIgor Mammedov Aml *pkg = aml_package(2);
707b7f23f62SIgor Mammedov
708*7170aa66SIgor Mammedov aml_append(ifctx, aml_store(aml_call2("AIDX", bnum, sunum), acpi_index));
709*7170aa66SIgor Mammedov aml_append(ifctx, aml_store(pkg, ret));
710*7170aa66SIgor Mammedov /*
711*7170aa66SIgor Mammedov * Windows calls func=7 without checking if it's available,
712*7170aa66SIgor Mammedov * as workaround Microsoft has suggested to return invalid for func7
713*7170aa66SIgor Mammedov * Package, so return 2 elements package but only initialize elements
714*7170aa66SIgor Mammedov * when acpi_index is supported and leave them uninitialized, which
715*7170aa66SIgor Mammedov * leads elements to being Uninitialized ObjectType and should trip
716*7170aa66SIgor Mammedov * Windows into discarding result as an unexpected and prevent setting
717*7170aa66SIgor Mammedov * bogus 'PCI Label' on the device.
718*7170aa66SIgor Mammedov */
719*7170aa66SIgor Mammedov ifctx1 = aml_if(aml_lnot(aml_lor(
720*7170aa66SIgor Mammedov aml_equal(acpi_index, zero), aml_equal(acpi_index, not_supp)
721*7170aa66SIgor Mammedov )));
722*7170aa66SIgor Mammedov {
723*7170aa66SIgor Mammedov aml_append(ifctx1, aml_store(acpi_index, aml_index(ret, zero)));
724b7f23f62SIgor Mammedov /*
725b7f23f62SIgor Mammedov * optional, if not impl. should return null string
726b7f23f62SIgor Mammedov */
727*7170aa66SIgor Mammedov aml_append(ifctx1, aml_store(aml_string("%s", ""),
728*7170aa66SIgor Mammedov aml_index(ret, one)));
729*7170aa66SIgor Mammedov }
730*7170aa66SIgor Mammedov aml_append(ifctx, ifctx1);
731a12cf692SIgor Mammedov
732467d099aSIgor Mammedov aml_append(ifctx, aml_return(ret));
733b7f23f62SIgor Mammedov }
734a12cf692SIgor Mammedov
735b7f23f62SIgor Mammedov aml_append(method, ifctx);
736b7f23f62SIgor Mammedov return method;
737b7f23f62SIgor Mammedov }
738b7f23f62SIgor Mammedov
7390d8935e3SMarcel Apfelbaum /*
74099cb2c6cSRicardo Ribalda * build_prt - Define interrupt routing rules
7410d8935e3SMarcel Apfelbaum *
7420d8935e3SMarcel Apfelbaum * Returns an array of 128 routes, one for each device,
7430d8935e3SMarcel Apfelbaum * based on device location.
744bad5cfcdSMichael Tokarev * The main goal is to equally distribute the interrupts
7450d8935e3SMarcel Apfelbaum * over the 4 existing ACPI links (works only for i440fx).
74699cb2c6cSRicardo Ribalda * The hash function is: (slot + pin) & 3 -> "LNK[D|A|B|C]".
7470d8935e3SMarcel Apfelbaum *
7480d8935e3SMarcel Apfelbaum */
build_prt(bool is_pci0_prt)749196e2137SIgor Mammedov static Aml *build_prt(bool is_pci0_prt)
7500d8935e3SMarcel Apfelbaum {
75199cb2c6cSRicardo Ribalda const int nroutes = 128;
75299cb2c6cSRicardo Ribalda Aml *rt_pkg, *method;
75399cb2c6cSRicardo Ribalda int pin;
7540d8935e3SMarcel Apfelbaum
7554dbfc881SXiao Guangrong method = aml_method("_PRT", 0, AML_NOTSERIALIZED);
7567916bb54SRicardo Ribalda assert(nroutes < 256);
7577916bb54SRicardo Ribalda rt_pkg = aml_package(nroutes);
7580d8935e3SMarcel Apfelbaum
75999cb2c6cSRicardo Ribalda for (pin = 0; pin < nroutes; pin++) {
76099cb2c6cSRicardo Ribalda Aml *pkg = aml_package(4);
76199cb2c6cSRicardo Ribalda int slot = pin >> 2;
7620d8935e3SMarcel Apfelbaum
76399cb2c6cSRicardo Ribalda aml_append(pkg, aml_int((slot << 16) | 0xFFFF));
76499cb2c6cSRicardo Ribalda aml_append(pkg, aml_int(pin & 3));
765196e2137SIgor Mammedov /* device 1 is the power-management device, needs SCI */
76699cb2c6cSRicardo Ribalda if (is_pci0_prt && pin == 4) {
76799cb2c6cSRicardo Ribalda aml_append(pkg, aml_name("%s", "LNKS"));
768196e2137SIgor Mammedov } else {
76999cb2c6cSRicardo Ribalda static const char link_name[][5] = {"LNKD", "LNKA", "LNKB", "LNKC"};
77099cb2c6cSRicardo Ribalda int hash = (slot + pin) & 3;
77199cb2c6cSRicardo Ribalda aml_append(pkg, aml_name("%s", link_name[hash]));
772196e2137SIgor Mammedov }
77399cb2c6cSRicardo Ribalda aml_append(pkg, aml_int(0));
77499cb2c6cSRicardo Ribalda aml_append(rt_pkg, pkg);
77599cb2c6cSRicardo Ribalda }
7760d8935e3SMarcel Apfelbaum
77799cb2c6cSRicardo Ribalda aml_append(method, aml_return(rt_pkg));
7780d8935e3SMarcel Apfelbaum
7790d8935e3SMarcel Apfelbaum return method;
7800d8935e3SMarcel Apfelbaum }
7810d8935e3SMarcel Apfelbaum
build_hpet_aml(Aml * table)782a57d708dSIgor Mammedov static void build_hpet_aml(Aml *table)
783a57d708dSIgor Mammedov {
784a57d708dSIgor Mammedov Aml *crs;
785a57d708dSIgor Mammedov Aml *field;
786a57d708dSIgor Mammedov Aml *method;
787a57d708dSIgor Mammedov Aml *if_ctx;
788a57d708dSIgor Mammedov Aml *scope = aml_scope("_SB");
789a57d708dSIgor Mammedov Aml *dev = aml_device("HPET");
790a57d708dSIgor Mammedov Aml *zero = aml_int(0);
791a57d708dSIgor Mammedov Aml *id = aml_local(0);
792a57d708dSIgor Mammedov Aml *period = aml_local(1);
793a57d708dSIgor Mammedov
794a57d708dSIgor Mammedov aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0103")));
795a57d708dSIgor Mammedov aml_append(dev, aml_name_decl("_UID", zero));
796a57d708dSIgor Mammedov
797a57d708dSIgor Mammedov aml_append(dev,
7983f3009c0SXiao Guangrong aml_operation_region("HPTM", AML_SYSTEM_MEMORY, aml_int(HPET_BASE),
7993f3009c0SXiao Guangrong HPET_LEN));
800a57d708dSIgor Mammedov field = aml_field("HPTM", AML_DWORD_ACC, AML_LOCK, AML_PRESERVE);
801a57d708dSIgor Mammedov aml_append(field, aml_named_field("VEND", 32));
802a57d708dSIgor Mammedov aml_append(field, aml_named_field("PRD", 32));
803a57d708dSIgor Mammedov aml_append(dev, field);
804a57d708dSIgor Mammedov
805a57d708dSIgor Mammedov method = aml_method("_STA", 0, AML_NOTSERIALIZED);
806a57d708dSIgor Mammedov aml_append(method, aml_store(aml_name("VEND"), id));
807a57d708dSIgor Mammedov aml_append(method, aml_store(aml_name("PRD"), period));
808a57d708dSIgor Mammedov aml_append(method, aml_shiftright(id, aml_int(16), id));
809a57d708dSIgor Mammedov if_ctx = aml_if(aml_lor(aml_equal(id, zero),
810a57d708dSIgor Mammedov aml_equal(id, aml_int(0xffff))));
811a57d708dSIgor Mammedov {
812a57d708dSIgor Mammedov aml_append(if_ctx, aml_return(zero));
813a57d708dSIgor Mammedov }
814a57d708dSIgor Mammedov aml_append(method, if_ctx);
815a57d708dSIgor Mammedov
816a57d708dSIgor Mammedov if_ctx = aml_if(aml_lor(aml_equal(period, zero),
817a57d708dSIgor Mammedov aml_lgreater(period, aml_int(100000000))));
818a57d708dSIgor Mammedov {
819a57d708dSIgor Mammedov aml_append(if_ctx, aml_return(zero));
820a57d708dSIgor Mammedov }
821a57d708dSIgor Mammedov aml_append(method, if_ctx);
822a57d708dSIgor Mammedov
823a57d708dSIgor Mammedov aml_append(method, aml_return(aml_int(0x0F)));
824a57d708dSIgor Mammedov aml_append(dev, method);
825a57d708dSIgor Mammedov
826a57d708dSIgor Mammedov crs = aml_resource_template();
827a57d708dSIgor Mammedov aml_append(crs, aml_memory32_fixed(HPET_BASE, HPET_LEN, AML_READ_ONLY));
828a57d708dSIgor Mammedov aml_append(dev, aml_name_decl("_CRS", crs));
829a57d708dSIgor Mammedov
830a57d708dSIgor Mammedov aml_append(scope, dev);
831a57d708dSIgor Mammedov aml_append(table, scope);
832a57d708dSIgor Mammedov }
833a57d708dSIgor Mammedov
build_vmbus_device_aml(VMBusBridge * vmbus_bridge)8346775d15dSJon Doron static Aml *build_vmbus_device_aml(VMBusBridge *vmbus_bridge)
8356775d15dSJon Doron {
8366775d15dSJon Doron Aml *dev;
8376775d15dSJon Doron Aml *method;
8386775d15dSJon Doron Aml *crs;
8396775d15dSJon Doron
8406775d15dSJon Doron dev = aml_device("VMBS");
8416775d15dSJon Doron aml_append(dev, aml_name_decl("STA", aml_int(0xF)));
8426775d15dSJon Doron aml_append(dev, aml_name_decl("_HID", aml_string("VMBus")));
8436775d15dSJon Doron aml_append(dev, aml_name_decl("_UID", aml_int(0x0)));
8446775d15dSJon Doron aml_append(dev, aml_name_decl("_DDN", aml_string("VMBUS")));
8456775d15dSJon Doron
8466775d15dSJon Doron method = aml_method("_DIS", 0, AML_NOTSERIALIZED);
8476775d15dSJon Doron aml_append(method, aml_store(aml_and(aml_name("STA"), aml_int(0xD), NULL),
8486775d15dSJon Doron aml_name("STA")));
8496775d15dSJon Doron aml_append(dev, method);
8506775d15dSJon Doron
8516775d15dSJon Doron method = aml_method("_PS0", 0, AML_NOTSERIALIZED);
8526775d15dSJon Doron aml_append(method, aml_store(aml_or(aml_name("STA"), aml_int(0xF), NULL),
8536775d15dSJon Doron aml_name("STA")));
8546775d15dSJon Doron aml_append(dev, method);
8556775d15dSJon Doron
8566775d15dSJon Doron method = aml_method("_STA", 0, AML_NOTSERIALIZED);
8576775d15dSJon Doron aml_append(method, aml_return(aml_name("STA")));
8586775d15dSJon Doron aml_append(dev, method);
8596775d15dSJon Doron
8606775d15dSJon Doron aml_append(dev, aml_name_decl("_PS3", aml_int(0x0)));
8616775d15dSJon Doron
8626775d15dSJon Doron crs = aml_resource_template();
8638f06f22fSJon Doron aml_append(crs, aml_irq_no_flags(vmbus_bridge->irq));
8646775d15dSJon Doron aml_append(dev, aml_name_decl("_CRS", crs));
8656775d15dSJon Doron
8666775d15dSJon Doron return dev;
8676775d15dSJon Doron }
8686775d15dSJon Doron
build_dbg_aml(Aml * table)8693892a2b7SIgor Mammedov static void build_dbg_aml(Aml *table)
8703892a2b7SIgor Mammedov {
8713892a2b7SIgor Mammedov Aml *field;
8723892a2b7SIgor Mammedov Aml *method;
8733892a2b7SIgor Mammedov Aml *while_ctx;
8743892a2b7SIgor Mammedov Aml *scope = aml_scope("\\");
8753892a2b7SIgor Mammedov Aml *buf = aml_local(0);
8763892a2b7SIgor Mammedov Aml *len = aml_local(1);
8773892a2b7SIgor Mammedov Aml *idx = aml_local(2);
8783892a2b7SIgor Mammedov
8793892a2b7SIgor Mammedov aml_append(scope,
8803f3009c0SXiao Guangrong aml_operation_region("DBG", AML_SYSTEM_IO, aml_int(0x0402), 0x01));
8813892a2b7SIgor Mammedov field = aml_field("DBG", AML_BYTE_ACC, AML_NOLOCK, AML_PRESERVE);
8823892a2b7SIgor Mammedov aml_append(field, aml_named_field("DBGB", 8));
8833892a2b7SIgor Mammedov aml_append(scope, field);
8843892a2b7SIgor Mammedov
8853892a2b7SIgor Mammedov method = aml_method("DBUG", 1, AML_NOTSERIALIZED);
8863892a2b7SIgor Mammedov
8873892a2b7SIgor Mammedov aml_append(method, aml_to_hexstring(aml_arg(0), buf));
8883892a2b7SIgor Mammedov aml_append(method, aml_to_buffer(buf, buf));
8893892a2b7SIgor Mammedov aml_append(method, aml_subtract(aml_sizeof(buf), aml_int(1), len));
8903892a2b7SIgor Mammedov aml_append(method, aml_store(aml_int(0), idx));
8913892a2b7SIgor Mammedov
8923892a2b7SIgor Mammedov while_ctx = aml_while(aml_lless(idx, len));
8933892a2b7SIgor Mammedov aml_append(while_ctx,
8943892a2b7SIgor Mammedov aml_store(aml_derefof(aml_index(buf, idx)), aml_name("DBGB")));
8953892a2b7SIgor Mammedov aml_append(while_ctx, aml_increment(idx));
8963892a2b7SIgor Mammedov aml_append(method, while_ctx);
8973892a2b7SIgor Mammedov
8983892a2b7SIgor Mammedov aml_append(method, aml_store(aml_int(0x0A), aml_name("DBGB")));
8993892a2b7SIgor Mammedov aml_append(scope, method);
9003892a2b7SIgor Mammedov
9013892a2b7SIgor Mammedov aml_append(table, scope);
9023892a2b7SIgor Mammedov }
9033892a2b7SIgor Mammedov
build_link_dev(const char * name,uint8_t uid,Aml * reg)904c35b6e80SIgor Mammedov static Aml *build_link_dev(const char *name, uint8_t uid, Aml *reg)
905c35b6e80SIgor Mammedov {
906c35b6e80SIgor Mammedov Aml *dev;
907c35b6e80SIgor Mammedov Aml *crs;
908c35b6e80SIgor Mammedov Aml *method;
909c35b6e80SIgor Mammedov uint32_t irqs[] = {5, 10, 11};
910c35b6e80SIgor Mammedov
911c35b6e80SIgor Mammedov dev = aml_device("%s", name);
912c35b6e80SIgor Mammedov aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C0F")));
913c35b6e80SIgor Mammedov aml_append(dev, aml_name_decl("_UID", aml_int(uid)));
914c35b6e80SIgor Mammedov
915c35b6e80SIgor Mammedov crs = aml_resource_template();
916c35b6e80SIgor Mammedov aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL, AML_ACTIVE_HIGH,
917c35b6e80SIgor Mammedov AML_SHARED, irqs, ARRAY_SIZE(irqs)));
918c35b6e80SIgor Mammedov aml_append(dev, aml_name_decl("_PRS", crs));
919c35b6e80SIgor Mammedov
920c35b6e80SIgor Mammedov method = aml_method("_STA", 0, AML_NOTSERIALIZED);
921c35b6e80SIgor Mammedov aml_append(method, aml_return(aml_call1("IQST", reg)));
922c35b6e80SIgor Mammedov aml_append(dev, method);
923c35b6e80SIgor Mammedov
924c35b6e80SIgor Mammedov method = aml_method("_DIS", 0, AML_NOTSERIALIZED);
925c35b6e80SIgor Mammedov aml_append(method, aml_or(reg, aml_int(0x80), reg));
926c35b6e80SIgor Mammedov aml_append(dev, method);
927c35b6e80SIgor Mammedov
928c35b6e80SIgor Mammedov method = aml_method("_CRS", 0, AML_NOTSERIALIZED);
929c35b6e80SIgor Mammedov aml_append(method, aml_return(aml_call1("IQCR", reg)));
930c35b6e80SIgor Mammedov aml_append(dev, method);
931c35b6e80SIgor Mammedov
932c35b6e80SIgor Mammedov method = aml_method("_SRS", 1, AML_NOTSERIALIZED);
933c35b6e80SIgor Mammedov aml_append(method, aml_create_dword_field(aml_arg(0), aml_int(5), "PRRI"));
934c35b6e80SIgor Mammedov aml_append(method, aml_store(aml_name("PRRI"), reg));
935c35b6e80SIgor Mammedov aml_append(dev, method);
936c35b6e80SIgor Mammedov
937c35b6e80SIgor Mammedov return dev;
938c35b6e80SIgor Mammedov }
939c35b6e80SIgor Mammedov
build_gsi_link_dev(const char * name,uint8_t uid,uint8_t gsi)94080b32df5SIgor Mammedov static Aml *build_gsi_link_dev(const char *name, uint8_t uid, uint8_t gsi)
94180b32df5SIgor Mammedov {
94280b32df5SIgor Mammedov Aml *dev;
94380b32df5SIgor Mammedov Aml *crs;
94480b32df5SIgor Mammedov Aml *method;
94580b32df5SIgor Mammedov uint32_t irqs;
94680b32df5SIgor Mammedov
94780b32df5SIgor Mammedov dev = aml_device("%s", name);
94880b32df5SIgor Mammedov aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C0F")));
94980b32df5SIgor Mammedov aml_append(dev, aml_name_decl("_UID", aml_int(uid)));
95080b32df5SIgor Mammedov
95180b32df5SIgor Mammedov crs = aml_resource_template();
95280b32df5SIgor Mammedov irqs = gsi;
95380b32df5SIgor Mammedov aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL, AML_ACTIVE_HIGH,
95480b32df5SIgor Mammedov AML_SHARED, &irqs, 1));
95580b32df5SIgor Mammedov aml_append(dev, aml_name_decl("_PRS", crs));
95680b32df5SIgor Mammedov
95780b32df5SIgor Mammedov aml_append(dev, aml_name_decl("_CRS", crs));
95880b32df5SIgor Mammedov
959c82f503dSMarcel Apfelbaum /*
960c82f503dSMarcel Apfelbaum * _DIS can be no-op because the interrupt cannot be disabled.
961c82f503dSMarcel Apfelbaum */
962c82f503dSMarcel Apfelbaum method = aml_method("_DIS", 0, AML_NOTSERIALIZED);
963c82f503dSMarcel Apfelbaum aml_append(dev, method);
964c82f503dSMarcel Apfelbaum
96580b32df5SIgor Mammedov method = aml_method("_SRS", 1, AML_NOTSERIALIZED);
96680b32df5SIgor Mammedov aml_append(dev, method);
96780b32df5SIgor Mammedov
96880b32df5SIgor Mammedov return dev;
96980b32df5SIgor Mammedov }
97080b32df5SIgor Mammedov
97116682a9dSIgor Mammedov /* _CRS method - get current settings */
build_iqcr_method(bool is_piix4)97216682a9dSIgor Mammedov static Aml *build_iqcr_method(bool is_piix4)
97316682a9dSIgor Mammedov {
97416682a9dSIgor Mammedov Aml *if_ctx;
97516682a9dSIgor Mammedov uint32_t irqs;
97616682a9dSIgor Mammedov Aml *method = aml_method("IQCR", 1, AML_SERIALIZED);
97716682a9dSIgor Mammedov Aml *crs = aml_resource_template();
97816682a9dSIgor Mammedov
97916682a9dSIgor Mammedov irqs = 0;
98016682a9dSIgor Mammedov aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL,
98116682a9dSIgor Mammedov AML_ACTIVE_HIGH, AML_SHARED, &irqs, 1));
98216682a9dSIgor Mammedov aml_append(method, aml_name_decl("PRR0", crs));
98316682a9dSIgor Mammedov
98416682a9dSIgor Mammedov aml_append(method,
98516682a9dSIgor Mammedov aml_create_dword_field(aml_name("PRR0"), aml_int(5), "PRRI"));
98616682a9dSIgor Mammedov
98716682a9dSIgor Mammedov if (is_piix4) {
98816682a9dSIgor Mammedov if_ctx = aml_if(aml_lless(aml_arg(0), aml_int(0x80)));
98916682a9dSIgor Mammedov aml_append(if_ctx, aml_store(aml_arg(0), aml_name("PRRI")));
99016682a9dSIgor Mammedov aml_append(method, if_ctx);
99116682a9dSIgor Mammedov } else {
99216682a9dSIgor Mammedov aml_append(method,
99316682a9dSIgor Mammedov aml_store(aml_and(aml_arg(0), aml_int(0xF), NULL),
99416682a9dSIgor Mammedov aml_name("PRRI")));
99516682a9dSIgor Mammedov }
99616682a9dSIgor Mammedov
99716682a9dSIgor Mammedov aml_append(method, aml_return(aml_name("PRR0")));
99816682a9dSIgor Mammedov return method;
99916682a9dSIgor Mammedov }
100016682a9dSIgor Mammedov
100178e1ad05SIgor Mammedov /* _STA method - get status */
build_irq_status_method(void)100278e1ad05SIgor Mammedov static Aml *build_irq_status_method(void)
100378e1ad05SIgor Mammedov {
100478e1ad05SIgor Mammedov Aml *if_ctx;
100578e1ad05SIgor Mammedov Aml *method = aml_method("IQST", 1, AML_NOTSERIALIZED);
100678e1ad05SIgor Mammedov
100778e1ad05SIgor Mammedov if_ctx = aml_if(aml_and(aml_int(0x80), aml_arg(0), NULL));
100878e1ad05SIgor Mammedov aml_append(if_ctx, aml_return(aml_int(0x09)));
100978e1ad05SIgor Mammedov aml_append(method, if_ctx);
101078e1ad05SIgor Mammedov aml_append(method, aml_return(aml_int(0x0B)));
101178e1ad05SIgor Mammedov return method;
101278e1ad05SIgor Mammedov }
101378e1ad05SIgor Mammedov
build_piix4_pci0_int(Aml * table)1014e4db2798SIgor Mammedov static void build_piix4_pci0_int(Aml *table)
1015e4db2798SIgor Mammedov {
1016c35b6e80SIgor Mammedov Aml *dev;
1017c35b6e80SIgor Mammedov Aml *crs;
1018c35b6e80SIgor Mammedov Aml *method;
1019c35b6e80SIgor Mammedov uint32_t irqs;
1020e4db2798SIgor Mammedov Aml *sb_scope = aml_scope("_SB");
1021196e2137SIgor Mammedov Aml *pci0_scope = aml_scope("PCI0");
1022196e2137SIgor Mammedov
1023196e2137SIgor Mammedov aml_append(pci0_scope, build_prt(true));
1024196e2137SIgor Mammedov aml_append(sb_scope, pci0_scope);
1025e4db2798SIgor Mammedov
102678e1ad05SIgor Mammedov aml_append(sb_scope, build_irq_status_method());
102716682a9dSIgor Mammedov aml_append(sb_scope, build_iqcr_method(true));
1028100681ccSIgor Mammedov
1029c35b6e80SIgor Mammedov aml_append(sb_scope, build_link_dev("LNKA", 0, aml_name("PRQ0")));
1030c35b6e80SIgor Mammedov aml_append(sb_scope, build_link_dev("LNKB", 1, aml_name("PRQ1")));
1031c35b6e80SIgor Mammedov aml_append(sb_scope, build_link_dev("LNKC", 2, aml_name("PRQ2")));
1032c35b6e80SIgor Mammedov aml_append(sb_scope, build_link_dev("LNKD", 3, aml_name("PRQ3")));
1033c35b6e80SIgor Mammedov
1034c35b6e80SIgor Mammedov dev = aml_device("LNKS");
1035c35b6e80SIgor Mammedov {
1036c35b6e80SIgor Mammedov aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C0F")));
1037c35b6e80SIgor Mammedov aml_append(dev, aml_name_decl("_UID", aml_int(4)));
1038c35b6e80SIgor Mammedov
1039c35b6e80SIgor Mammedov crs = aml_resource_template();
1040c35b6e80SIgor Mammedov irqs = 9;
1041c35b6e80SIgor Mammedov aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL,
1042c35b6e80SIgor Mammedov AML_ACTIVE_HIGH, AML_SHARED,
1043c35b6e80SIgor Mammedov &irqs, 1));
1044c35b6e80SIgor Mammedov aml_append(dev, aml_name_decl("_PRS", crs));
1045c35b6e80SIgor Mammedov
1046c35b6e80SIgor Mammedov /* The SCI cannot be disabled and is always attached to GSI 9,
1047c35b6e80SIgor Mammedov * so these are no-ops. We only need this link to override the
1048c35b6e80SIgor Mammedov * polarity to active high and match the content of the MADT.
1049c35b6e80SIgor Mammedov */
1050c35b6e80SIgor Mammedov method = aml_method("_STA", 0, AML_NOTSERIALIZED);
1051c35b6e80SIgor Mammedov aml_append(method, aml_return(aml_int(0x0b)));
1052c35b6e80SIgor Mammedov aml_append(dev, method);
1053c35b6e80SIgor Mammedov
1054c35b6e80SIgor Mammedov method = aml_method("_DIS", 0, AML_NOTSERIALIZED);
1055c35b6e80SIgor Mammedov aml_append(dev, method);
1056c35b6e80SIgor Mammedov
1057c35b6e80SIgor Mammedov method = aml_method("_CRS", 0, AML_NOTSERIALIZED);
1058c35b6e80SIgor Mammedov aml_append(method, aml_return(aml_name("_PRS")));
1059c35b6e80SIgor Mammedov aml_append(dev, method);
1060c35b6e80SIgor Mammedov
1061c35b6e80SIgor Mammedov method = aml_method("_SRS", 1, AML_NOTSERIALIZED);
1062c35b6e80SIgor Mammedov aml_append(dev, method);
1063c35b6e80SIgor Mammedov }
1064c35b6e80SIgor Mammedov aml_append(sb_scope, dev);
1065c35b6e80SIgor Mammedov
1066e4db2798SIgor Mammedov aml_append(table, sb_scope);
1067e4db2798SIgor Mammedov }
1068e4db2798SIgor Mammedov
append_q35_prt_entry(Aml * ctx,uint32_t nr,const char * name)106922b5b8bfSIgor Mammedov static void append_q35_prt_entry(Aml *ctx, uint32_t nr, const char *name)
107022b5b8bfSIgor Mammedov {
107122b5b8bfSIgor Mammedov int i;
107222b5b8bfSIgor Mammedov int head;
107322b5b8bfSIgor Mammedov Aml *pkg;
107422b5b8bfSIgor Mammedov char base = name[3] < 'E' ? 'A' : 'E';
107522b5b8bfSIgor Mammedov char *s = g_strdup(name);
107622b5b8bfSIgor Mammedov Aml *a_nr = aml_int((nr << 16) | 0xffff);
107722b5b8bfSIgor Mammedov
107822b5b8bfSIgor Mammedov assert(strlen(s) == 4);
107922b5b8bfSIgor Mammedov
108022b5b8bfSIgor Mammedov head = name[3] - base;
108122b5b8bfSIgor Mammedov for (i = 0; i < 4; i++) {
108222b5b8bfSIgor Mammedov if (head + i > 3) {
108322b5b8bfSIgor Mammedov head = i * -1;
108422b5b8bfSIgor Mammedov }
108522b5b8bfSIgor Mammedov s[3] = base + head + i;
108622b5b8bfSIgor Mammedov pkg = aml_package(4);
108722b5b8bfSIgor Mammedov aml_append(pkg, a_nr);
108822b5b8bfSIgor Mammedov aml_append(pkg, aml_int(i));
108922b5b8bfSIgor Mammedov aml_append(pkg, aml_name("%s", s));
109022b5b8bfSIgor Mammedov aml_append(pkg, aml_int(0));
109122b5b8bfSIgor Mammedov aml_append(ctx, pkg);
109222b5b8bfSIgor Mammedov }
109322b5b8bfSIgor Mammedov g_free(s);
109422b5b8bfSIgor Mammedov }
109522b5b8bfSIgor Mammedov
build_q35_routing_table(const char * str)109622b5b8bfSIgor Mammedov static Aml *build_q35_routing_table(const char *str)
109722b5b8bfSIgor Mammedov {
109822b5b8bfSIgor Mammedov int i;
109922b5b8bfSIgor Mammedov Aml *pkg;
110022b5b8bfSIgor Mammedov char *name = g_strdup_printf("%s ", str);
110122b5b8bfSIgor Mammedov
110222b5b8bfSIgor Mammedov pkg = aml_package(128);
110322b5b8bfSIgor Mammedov for (i = 0; i < 0x18; i++) {
110422b5b8bfSIgor Mammedov name[3] = 'E' + (i & 0x3);
110522b5b8bfSIgor Mammedov append_q35_prt_entry(pkg, i, name);
110622b5b8bfSIgor Mammedov }
110722b5b8bfSIgor Mammedov
110822b5b8bfSIgor Mammedov name[3] = 'E';
110922b5b8bfSIgor Mammedov append_q35_prt_entry(pkg, 0x18, name);
111022b5b8bfSIgor Mammedov
111122b5b8bfSIgor Mammedov /* INTA -> PIRQA for slot 25 - 31, see the default value of D<N>IR */
111222b5b8bfSIgor Mammedov for (i = 0x0019; i < 0x1e; i++) {
111322b5b8bfSIgor Mammedov name[3] = 'A';
111422b5b8bfSIgor Mammedov append_q35_prt_entry(pkg, i, name);
111522b5b8bfSIgor Mammedov }
111622b5b8bfSIgor Mammedov
111722b5b8bfSIgor Mammedov /* PCIe->PCI bridge. use PIRQ[E-H] */
111822b5b8bfSIgor Mammedov name[3] = 'E';
111922b5b8bfSIgor Mammedov append_q35_prt_entry(pkg, 0x1e, name);
112022b5b8bfSIgor Mammedov name[3] = 'A';
112122b5b8bfSIgor Mammedov append_q35_prt_entry(pkg, 0x1f, name);
112222b5b8bfSIgor Mammedov
112322b5b8bfSIgor Mammedov g_free(name);
112422b5b8bfSIgor Mammedov return pkg;
112522b5b8bfSIgor Mammedov }
112622b5b8bfSIgor Mammedov
build_q35_pci0_int(Aml * table)112780b32df5SIgor Mammedov static void build_q35_pci0_int(Aml *table)
112880b32df5SIgor Mammedov {
11290dafe3b3SIgor Mammedov Aml *method;
113080b32df5SIgor Mammedov Aml *sb_scope = aml_scope("_SB");
11310dafe3b3SIgor Mammedov Aml *pci0_scope = aml_scope("PCI0");
11320dafe3b3SIgor Mammedov
1133e9fce798SIgor Mammedov /* Zero => PIC mode, One => APIC Mode */
1134e9fce798SIgor Mammedov aml_append(table, aml_name_decl("PICF", aml_int(0)));
1135e9fce798SIgor Mammedov method = aml_method("_PIC", 1, AML_NOTSERIALIZED);
1136e9fce798SIgor Mammedov {
1137e9fce798SIgor Mammedov aml_append(method, aml_store(aml_arg(0), aml_name("PICF")));
1138e9fce798SIgor Mammedov }
1139e9fce798SIgor Mammedov aml_append(table, method);
1140e9fce798SIgor Mammedov
114122b5b8bfSIgor Mammedov aml_append(pci0_scope,
114265aef4deSIgor Mammedov aml_name_decl("PRTP", build_q35_routing_table("LNK")));
114365aef4deSIgor Mammedov aml_append(pci0_scope,
114422b5b8bfSIgor Mammedov aml_name_decl("PRTA", build_q35_routing_table("GSI")));
114522b5b8bfSIgor Mammedov
11460dafe3b3SIgor Mammedov method = aml_method("_PRT", 0, AML_NOTSERIALIZED);
11470dafe3b3SIgor Mammedov {
11480dafe3b3SIgor Mammedov Aml *if_ctx;
11490dafe3b3SIgor Mammedov Aml *else_ctx;
11500dafe3b3SIgor Mammedov
11510dafe3b3SIgor Mammedov /* PCI IRQ routing table, example from ACPI 2.0a specification,
11520dafe3b3SIgor Mammedov section 6.2.8.1 */
11530dafe3b3SIgor Mammedov /* Note: we provide the same info as the PCI routing
11540dafe3b3SIgor Mammedov table of the Bochs BIOS */
11550dafe3b3SIgor Mammedov if_ctx = aml_if(aml_equal(aml_name("PICF"), aml_int(0)));
11560dafe3b3SIgor Mammedov aml_append(if_ctx, aml_return(aml_name("PRTP")));
11570dafe3b3SIgor Mammedov aml_append(method, if_ctx);
11580dafe3b3SIgor Mammedov else_ctx = aml_else();
11590dafe3b3SIgor Mammedov aml_append(else_ctx, aml_return(aml_name("PRTA")));
11600dafe3b3SIgor Mammedov aml_append(method, else_ctx);
11610dafe3b3SIgor Mammedov }
11620dafe3b3SIgor Mammedov aml_append(pci0_scope, method);
11630dafe3b3SIgor Mammedov aml_append(sb_scope, pci0_scope);
116480b32df5SIgor Mammedov
116578e1ad05SIgor Mammedov aml_append(sb_scope, build_irq_status_method());
116616682a9dSIgor Mammedov aml_append(sb_scope, build_iqcr_method(false));
116716682a9dSIgor Mammedov
116812e3b1f7SIgor Mammedov aml_append(sb_scope, build_link_dev("LNKA", 0, aml_name("PRQA")));
116912e3b1f7SIgor Mammedov aml_append(sb_scope, build_link_dev("LNKB", 1, aml_name("PRQB")));
117012e3b1f7SIgor Mammedov aml_append(sb_scope, build_link_dev("LNKC", 2, aml_name("PRQC")));
117112e3b1f7SIgor Mammedov aml_append(sb_scope, build_link_dev("LNKD", 3, aml_name("PRQD")));
117212e3b1f7SIgor Mammedov aml_append(sb_scope, build_link_dev("LNKE", 4, aml_name("PRQE")));
117312e3b1f7SIgor Mammedov aml_append(sb_scope, build_link_dev("LNKF", 5, aml_name("PRQF")));
117412e3b1f7SIgor Mammedov aml_append(sb_scope, build_link_dev("LNKG", 6, aml_name("PRQG")));
117512e3b1f7SIgor Mammedov aml_append(sb_scope, build_link_dev("LNKH", 7, aml_name("PRQH")));
117612e3b1f7SIgor Mammedov
11776a991e07SMarcel Apfelbaum aml_append(sb_scope, build_gsi_link_dev("GSIA", 0x10, 0x10));
11786a991e07SMarcel Apfelbaum aml_append(sb_scope, build_gsi_link_dev("GSIB", 0x11, 0x11));
11796a991e07SMarcel Apfelbaum aml_append(sb_scope, build_gsi_link_dev("GSIC", 0x12, 0x12));
11806a991e07SMarcel Apfelbaum aml_append(sb_scope, build_gsi_link_dev("GSID", 0x13, 0x13));
11816a991e07SMarcel Apfelbaum aml_append(sb_scope, build_gsi_link_dev("GSIE", 0x14, 0x14));
11826a991e07SMarcel Apfelbaum aml_append(sb_scope, build_gsi_link_dev("GSIF", 0x15, 0x15));
11836a991e07SMarcel Apfelbaum aml_append(sb_scope, build_gsi_link_dev("GSIG", 0x16, 0x16));
11846a991e07SMarcel Apfelbaum aml_append(sb_scope, build_gsi_link_dev("GSIH", 0x17, 0x17));
118580b32df5SIgor Mammedov
118680b32df5SIgor Mammedov aml_append(table, sb_scope);
118780b32df5SIgor Mammedov }
118880b32df5SIgor Mammedov
build_q35_dram_controller(const AcpiMcfgInfo * mcfg)1189e3fb55f0SIsaku Yamahata static Aml *build_q35_dram_controller(const AcpiMcfgInfo *mcfg)
1190e3fb55f0SIsaku Yamahata {
1191e3fb55f0SIsaku Yamahata Aml *dev;
1192e3fb55f0SIsaku Yamahata Aml *resource_template;
1193e3fb55f0SIsaku Yamahata
1194e3fb55f0SIsaku Yamahata /* DRAM controller */
1195e3fb55f0SIsaku Yamahata dev = aml_device("DRAC");
1196e3fb55f0SIsaku Yamahata aml_append(dev, aml_name_decl("_HID", aml_string("PNP0C01")));
1197e3fb55f0SIsaku Yamahata
1198e3fb55f0SIsaku Yamahata resource_template = aml_resource_template();
1199e3fb55f0SIsaku Yamahata if (mcfg->base + mcfg->size - 1 >= (1ULL << 32)) {
1200e3fb55f0SIsaku Yamahata aml_append(resource_template,
1201e3fb55f0SIsaku Yamahata aml_qword_memory(AML_POS_DECODE,
1202e3fb55f0SIsaku Yamahata AML_MIN_FIXED,
1203e3fb55f0SIsaku Yamahata AML_MAX_FIXED,
1204e3fb55f0SIsaku Yamahata AML_NON_CACHEABLE,
1205e3fb55f0SIsaku Yamahata AML_READ_WRITE,
1206e3fb55f0SIsaku Yamahata 0x0000000000000000,
1207e3fb55f0SIsaku Yamahata mcfg->base,
1208e3fb55f0SIsaku Yamahata mcfg->base + mcfg->size - 1,
1209e3fb55f0SIsaku Yamahata 0x0000000000000000,
1210e3fb55f0SIsaku Yamahata mcfg->size));
1211e3fb55f0SIsaku Yamahata } else {
1212e3fb55f0SIsaku Yamahata aml_append(resource_template,
1213e3fb55f0SIsaku Yamahata aml_dword_memory(AML_POS_DECODE,
1214e3fb55f0SIsaku Yamahata AML_MIN_FIXED,
1215e3fb55f0SIsaku Yamahata AML_MAX_FIXED,
1216e3fb55f0SIsaku Yamahata AML_NON_CACHEABLE,
1217e3fb55f0SIsaku Yamahata AML_READ_WRITE,
1218e3fb55f0SIsaku Yamahata 0x0000000000000000,
1219e3fb55f0SIsaku Yamahata mcfg->base,
1220e3fb55f0SIsaku Yamahata mcfg->base + mcfg->size - 1,
1221e3fb55f0SIsaku Yamahata 0x0000000000000000,
1222e3fb55f0SIsaku Yamahata mcfg->size));
1223e3fb55f0SIsaku Yamahata }
1224e3fb55f0SIsaku Yamahata aml_append(dev, aml_name_decl("_CRS", resource_template));
1225e3fb55f0SIsaku Yamahata
1226e3fb55f0SIsaku Yamahata return dev;
1227e3fb55f0SIsaku Yamahata }
1228e3fb55f0SIsaku Yamahata
build_x86_acpi_pci_hotplug(Aml * table,uint64_t pcihp_addr)1229caf108bcSJulia Suvorova static void build_x86_acpi_pci_hotplug(Aml *table, uint64_t pcihp_addr)
1230b616ec4dSIgor Mammedov {
1231b616ec4dSIgor Mammedov Aml *scope;
1232b616ec4dSIgor Mammedov Aml *field;
1233b616ec4dSIgor Mammedov Aml *method;
1234b616ec4dSIgor Mammedov
1235b616ec4dSIgor Mammedov scope = aml_scope("_SB.PCI0");
1236b616ec4dSIgor Mammedov
1237b616ec4dSIgor Mammedov aml_append(scope,
1238caf108bcSJulia Suvorova aml_operation_region("PCST", AML_SYSTEM_IO, aml_int(pcihp_addr), 0x08));
1239b616ec4dSIgor Mammedov field = aml_field("PCST", AML_DWORD_ACC, AML_NOLOCK, AML_WRITE_AS_ZEROS);
1240b616ec4dSIgor Mammedov aml_append(field, aml_named_field("PCIU", 32));
1241b616ec4dSIgor Mammedov aml_append(field, aml_named_field("PCID", 32));
1242b616ec4dSIgor Mammedov aml_append(scope, field);
1243b616ec4dSIgor Mammedov
1244b616ec4dSIgor Mammedov aml_append(scope,
1245caf108bcSJulia Suvorova aml_operation_region("SEJ", AML_SYSTEM_IO,
1246caf108bcSJulia Suvorova aml_int(pcihp_addr + ACPI_PCIHP_SEJ_BASE), 0x04));
1247b616ec4dSIgor Mammedov field = aml_field("SEJ", AML_DWORD_ACC, AML_NOLOCK, AML_WRITE_AS_ZEROS);
1248b616ec4dSIgor Mammedov aml_append(field, aml_named_field("B0EJ", 32));
1249b616ec4dSIgor Mammedov aml_append(scope, field);
1250b616ec4dSIgor Mammedov
1251b616ec4dSIgor Mammedov aml_append(scope,
1252caf108bcSJulia Suvorova aml_operation_region("BNMR", AML_SYSTEM_IO,
1253caf108bcSJulia Suvorova aml_int(pcihp_addr + ACPI_PCIHP_BNMR_BASE), 0x08));
1254b616ec4dSIgor Mammedov field = aml_field("BNMR", AML_DWORD_ACC, AML_NOLOCK, AML_WRITE_AS_ZEROS);
1255b616ec4dSIgor Mammedov aml_append(field, aml_named_field("BNUM", 32));
1256b32bd763SIgor Mammedov aml_append(field, aml_named_field("PIDX", 32));
1257b616ec4dSIgor Mammedov aml_append(scope, field);
1258b616ec4dSIgor Mammedov
1259b616ec4dSIgor Mammedov aml_append(scope, aml_mutex("BLCK", 0));
1260b616ec4dSIgor Mammedov
1261b616ec4dSIgor Mammedov method = aml_method("PCEJ", 2, AML_NOTSERIALIZED);
1262b616ec4dSIgor Mammedov aml_append(method, aml_acquire(aml_name("BLCK"), 0xFFFF));
1263b616ec4dSIgor Mammedov aml_append(method, aml_store(aml_arg(0), aml_name("BNUM")));
1264b616ec4dSIgor Mammedov aml_append(method,
1265b616ec4dSIgor Mammedov aml_store(aml_shiftleft(aml_int(1), aml_arg(1)), aml_name("B0EJ")));
1266b616ec4dSIgor Mammedov aml_append(method, aml_release(aml_name("BLCK")));
1267b616ec4dSIgor Mammedov aml_append(method, aml_return(aml_int(0)));
1268b616ec4dSIgor Mammedov aml_append(scope, method);
1269b616ec4dSIgor Mammedov
1270b32bd763SIgor Mammedov method = aml_method("AIDX", 2, AML_NOTSERIALIZED);
1271b32bd763SIgor Mammedov aml_append(method, aml_acquire(aml_name("BLCK"), 0xFFFF));
1272b32bd763SIgor Mammedov aml_append(method, aml_store(aml_arg(0), aml_name("BNUM")));
1273b32bd763SIgor Mammedov aml_append(method,
1274b32bd763SIgor Mammedov aml_store(aml_shiftleft(aml_int(1), aml_arg(1)), aml_name("PIDX")));
1275b32bd763SIgor Mammedov aml_append(method, aml_store(aml_name("PIDX"), aml_local(0)));
1276b32bd763SIgor Mammedov aml_append(method, aml_release(aml_name("BLCK")));
1277b32bd763SIgor Mammedov aml_append(method, aml_return(aml_local(0)));
1278b32bd763SIgor Mammedov aml_append(scope, method);
1279b32bd763SIgor Mammedov
12805840a163SIgor Mammedov aml_append(scope, aml_pci_pdsm());
1281b7f23f62SIgor Mammedov
1282b616ec4dSIgor Mammedov aml_append(table, scope);
1283b616ec4dSIgor Mammedov }
1284b616ec4dSIgor Mammedov
build_q35_osc_method(bool enable_native_pcie_hotplug)1285211afe5cSJulia Suvorova static Aml *build_q35_osc_method(bool enable_native_pcie_hotplug)
1286f97a88a8SIgor Mammedov {
1287f97a88a8SIgor Mammedov Aml *if_ctx;
1288f97a88a8SIgor Mammedov Aml *if_ctx2;
1289f97a88a8SIgor Mammedov Aml *else_ctx;
1290f97a88a8SIgor Mammedov Aml *method;
1291f97a88a8SIgor Mammedov Aml *a_cwd1 = aml_name("CDW1");
1292b3c782dbSMichael S. Tsirkin Aml *a_ctrl = aml_local(0);
1293f97a88a8SIgor Mammedov
1294f97a88a8SIgor Mammedov method = aml_method("_OSC", 4, AML_NOTSERIALIZED);
1295f97a88a8SIgor Mammedov aml_append(method, aml_create_dword_field(aml_arg(3), aml_int(0), "CDW1"));
1296f97a88a8SIgor Mammedov
1297f97a88a8SIgor Mammedov if_ctx = aml_if(aml_equal(
1298f97a88a8SIgor Mammedov aml_arg(0), aml_touuid("33DB4D5B-1FF7-401C-9657-7441C03DD766")));
1299f97a88a8SIgor Mammedov aml_append(if_ctx, aml_create_dword_field(aml_arg(3), aml_int(4), "CDW2"));
1300f97a88a8SIgor Mammedov aml_append(if_ctx, aml_create_dword_field(aml_arg(3), aml_int(8), "CDW3"));
1301f97a88a8SIgor Mammedov
1302f97a88a8SIgor Mammedov aml_append(if_ctx, aml_store(aml_name("CDW3"), a_ctrl));
1303f97a88a8SIgor Mammedov
1304f97a88a8SIgor Mammedov /*
1305f97a88a8SIgor Mammedov * Always allow native PME, AER (no dependencies)
1306a41c78c1SAleksandr Bezzubikov * Allow SHPC (PCI bridges can have SHPC controller)
1307211afe5cSJulia Suvorova * Disable PCIe Native Hot-plug if ACPI PCI Hot-plug is enabled.
1308f97a88a8SIgor Mammedov */
1309211afe5cSJulia Suvorova aml_append(if_ctx, aml_and(a_ctrl,
1310211afe5cSJulia Suvorova aml_int(0x1E | (enable_native_pcie_hotplug ? 0x1 : 0x0)), a_ctrl));
1311f97a88a8SIgor Mammedov
1312f97a88a8SIgor Mammedov if_ctx2 = aml_if(aml_lnot(aml_equal(aml_arg(1), aml_int(1))));
1313f97a88a8SIgor Mammedov /* Unknown revision */
1314f97a88a8SIgor Mammedov aml_append(if_ctx2, aml_or(a_cwd1, aml_int(0x08), a_cwd1));
1315f97a88a8SIgor Mammedov aml_append(if_ctx, if_ctx2);
1316f97a88a8SIgor Mammedov
1317f97a88a8SIgor Mammedov if_ctx2 = aml_if(aml_lnot(aml_equal(aml_name("CDW3"), a_ctrl)));
1318f97a88a8SIgor Mammedov /* Capabilities bits were masked */
1319f97a88a8SIgor Mammedov aml_append(if_ctx2, aml_or(a_cwd1, aml_int(0x10), a_cwd1));
1320f97a88a8SIgor Mammedov aml_append(if_ctx, if_ctx2);
1321f97a88a8SIgor Mammedov
1322f97a88a8SIgor Mammedov /* Update DWORD3 in the buffer */
1323f97a88a8SIgor Mammedov aml_append(if_ctx, aml_store(a_ctrl, aml_name("CDW3")));
1324f97a88a8SIgor Mammedov aml_append(method, if_ctx);
1325f97a88a8SIgor Mammedov
1326f97a88a8SIgor Mammedov else_ctx = aml_else();
1327f97a88a8SIgor Mammedov /* Unrecognized UUID */
1328f97a88a8SIgor Mammedov aml_append(else_ctx, aml_or(a_cwd1, aml_int(4), a_cwd1));
1329f97a88a8SIgor Mammedov aml_append(method, else_ctx);
1330f97a88a8SIgor Mammedov
1331f97a88a8SIgor Mammedov aml_append(method, aml_return(aml_arg(3)));
1332f97a88a8SIgor Mammedov return method;
1333f97a88a8SIgor Mammedov }
1334b616ec4dSIgor Mammedov
build_acpi0017(Aml * table)13353d6a69b6SBen Widawsky static void build_acpi0017(Aml *table)
13363d6a69b6SBen Widawsky {
13373d6a69b6SBen Widawsky Aml *dev, *scope, *method;
13383d6a69b6SBen Widawsky
13393d6a69b6SBen Widawsky scope = aml_scope("_SB");
13403d6a69b6SBen Widawsky dev = aml_device("CXLM");
13413d6a69b6SBen Widawsky aml_append(dev, aml_name_decl("_HID", aml_string("ACPI0017")));
13423d6a69b6SBen Widawsky
13433d6a69b6SBen Widawsky method = aml_method("_STA", 0, AML_NOTSERIALIZED);
1344d9ae5802SJonathan Cameron aml_append(method, aml_return(aml_int(0x0B)));
13453d6a69b6SBen Widawsky aml_append(dev, method);
13466cdd46f6SDave Jiang build_cxl_dsm_method(dev);
13473d6a69b6SBen Widawsky
13483d6a69b6SBen Widawsky aml_append(scope, dev);
13493d6a69b6SBen Widawsky aml_append(table, scope);
13503d6a69b6SBen Widawsky }
13513d6a69b6SBen Widawsky
135272c194f7SMichael S. Tsirkin static void
build_dsdt(GArray * table_data,BIOSLinker * linker,AcpiPmInfo * pm,AcpiMiscInfo * misc,Range * pci_hole,Range * pci_hole64,MachineState * machine)13530e9b9edaSIgor Mammedov build_dsdt(GArray *table_data, BIOSLinker *linker,
1354adcb89d5SIgor Mammedov AcpiPmInfo *pm, AcpiMiscInfo *misc,
135501c9742dSMarkus Armbruster Range *pci_hole, Range *pci_hole64, MachineState *machine)
135672c194f7SMichael S. Tsirkin {
1357ee3b34cdSPaolo Bonzini Object *i440fx = object_resolve_type_unambiguous(TYPE_I440FX_PCI_HOST_BRIDGE,
1358ee3b34cdSPaolo Bonzini NULL);
1359ee3b34cdSPaolo Bonzini Object *q35 = object_resolve_type_unambiguous(TYPE_Q35_HOST_DEVICE, NULL);
136041fa5c04SIgor Mammedov CrsRangeEntry *entry;
136141fa5c04SIgor Mammedov Aml *dsdt, *sb_scope, *scope, *dev, *method, *field, *pkg, *crs;
13622df5a7b5SMarcel Apfelbaum CrsRangeSet crs_range_set;
1363fb306ffeSEduardo Habkost PCMachineState *pcms = PC_MACHINE(machine);
1364679dd1a9SIgor Mammedov PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(machine);
1365f0bb276bSPaolo Bonzini X86MachineState *x86ms = X86_MACHINE(machine);
13664a441836SGerd Hoffmann AcpiMcfgInfo mcfg;
1367e3fb55f0SIsaku Yamahata bool mcfg_valid = !!acpi_get_mcfg(&mcfg);
1368bef3492dSIgor Mammedov uint32_t nr_mem = machine->ram_slots;
1369dcdca296SMarcel Apfelbaum int root_bus_limit = 0xFF;
137041fa5c04SIgor Mammedov PCIBus *bus = NULL;
137111fb99e6SStefan Berger #ifdef CONFIG_TPM
1372ac6dd31eSStefan Berger TPMIf *tpm = tpm_find();
137311fb99e6SStefan Berger #endif
13743d6a69b6SBen Widawsky bool cxl_present = false;
137572c194f7SMichael S. Tsirkin int i;
13768f814ea1SJon Doron VMBusBridge *vmbus_bridge = vmbus_bridge_find();
13775c142bc4SIgor Mammedov AcpiTable table = { .sig = "DSDT", .rev = 1, .oem_id = x86ms->oem_id,
13785c142bc4SIgor Mammedov .oem_table_id = x86ms->oem_table_id };
137972c194f7SMichael S. Tsirkin
1380b496a17dSBernhard Beschow assert(!!i440fx != !!q35);
1381bbaa5c41SBernhard Beschow
13825c142bc4SIgor Mammedov acpi_table_begin(&table, table_data);
138341fa5c04SIgor Mammedov dsdt = init_aml_allocator();
13842fd71f1bSLaszlo Ersek
138541fa5c04SIgor Mammedov build_dbg_aml(dsdt);
1386b496a17dSBernhard Beschow if (i440fx) {
138741fa5c04SIgor Mammedov sb_scope = aml_scope("_SB");
138841fa5c04SIgor Mammedov dev = aml_device("PCI0");
138941fa5c04SIgor Mammedov aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A03")));
13900a343a5aSVitaly Cheptsov aml_append(dev, aml_name_decl("_UID", aml_int(pcmc->pci_root_uid)));
1391fe0d5f53SIgor Mammedov aml_append(dev, aml_pci_edsm());
139241fa5c04SIgor Mammedov aml_append(sb_scope, dev);
139341fa5c04SIgor Mammedov aml_append(dsdt, sb_scope);
139441fa5c04SIgor Mammedov
1395df4008c9SAni Sinha if (pm->pcihp_bridge_en || pm->pcihp_root_en) {
1396caf108bcSJulia Suvorova build_x86_acpi_pci_hotplug(dsdt, pm->pcihp_io_base);
1397df4008c9SAni Sinha }
139841fa5c04SIgor Mammedov build_piix4_pci0_int(dsdt);
1399b496a17dSBernhard Beschow } else if (q35) {
140041fa5c04SIgor Mammedov sb_scope = aml_scope("_SB");
140141fa5c04SIgor Mammedov dev = aml_device("PCI0");
140241fa5c04SIgor Mammedov aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A08")));
140341fa5c04SIgor Mammedov aml_append(dev, aml_name_decl("_CID", aml_eisaid("PNP0A03")));
14040a343a5aSVitaly Cheptsov aml_append(dev, aml_name_decl("_UID", aml_int(pcmc->pci_root_uid)));
1405211afe5cSJulia Suvorova aml_append(dev, build_q35_osc_method(!pm->pcihp_bridge_en));
1406fe0d5f53SIgor Mammedov aml_append(dev, aml_pci_edsm());
140741fa5c04SIgor Mammedov aml_append(sb_scope, dev);
1408e3fb55f0SIsaku Yamahata if (mcfg_valid) {
1409e3fb55f0SIsaku Yamahata aml_append(sb_scope, build_q35_dram_controller(&mcfg));
1410e3fb55f0SIsaku Yamahata }
1411998ba950SIgor Mammedov
1412998ba950SIgor Mammedov if (pm->smi_on_cpuhp) {
1413998ba950SIgor Mammedov /* reserve SMI block resources, IO ports 0xB2, 0xB3 */
1414998ba950SIgor Mammedov dev = aml_device("PCI0.SMI0");
1415998ba950SIgor Mammedov aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A06")));
1416998ba950SIgor Mammedov aml_append(dev, aml_name_decl("_UID", aml_string("SMI resources")));
1417998ba950SIgor Mammedov crs = aml_resource_template();
1418998ba950SIgor Mammedov aml_append(crs,
1419998ba950SIgor Mammedov aml_io(
1420998ba950SIgor Mammedov AML_DECODE16,
14215cdb639dSBernhard Beschow pm->fadt.smi_cmd,
14225cdb639dSBernhard Beschow pm->fadt.smi_cmd,
1423998ba950SIgor Mammedov 1,
1424998ba950SIgor Mammedov 2)
1425998ba950SIgor Mammedov );
1426998ba950SIgor Mammedov aml_append(dev, aml_name_decl("_CRS", crs));
1427998ba950SIgor Mammedov aml_append(dev, aml_operation_region("SMIR", AML_SYSTEM_IO,
14285cdb639dSBernhard Beschow aml_int(pm->fadt.smi_cmd), 2));
1429998ba950SIgor Mammedov field = aml_field("SMIR", AML_BYTE_ACC, AML_NOLOCK,
1430998ba950SIgor Mammedov AML_WRITE_AS_ZEROS);
1431998ba950SIgor Mammedov aml_append(field, aml_named_field("SMIC", 8));
1432998ba950SIgor Mammedov aml_append(field, aml_reserved_field(8));
1433998ba950SIgor Mammedov aml_append(dev, field);
1434998ba950SIgor Mammedov aml_append(sb_scope, dev);
1435998ba950SIgor Mammedov }
1436998ba950SIgor Mammedov
143741fa5c04SIgor Mammedov aml_append(dsdt, sb_scope);
143841fa5c04SIgor Mammedov
1439caf108bcSJulia Suvorova if (pm->pcihp_bridge_en) {
1440caf108bcSJulia Suvorova build_x86_acpi_pci_hotplug(dsdt, pm->pcihp_io_base);
1441caf108bcSJulia Suvorova }
144241fa5c04SIgor Mammedov build_q35_pci0_int(dsdt);
144341fa5c04SIgor Mammedov }
144441fa5c04SIgor Mammedov
1445e05acc36SIgor Mammedov if (misc->has_hpet) {
1446e05acc36SIgor Mammedov build_hpet_aml(dsdt);
1447e05acc36SIgor Mammedov }
1448e05acc36SIgor Mammedov
14498f814ea1SJon Doron if (vmbus_bridge) {
14508f814ea1SJon Doron sb_scope = aml_scope("_SB");
14518f814ea1SJon Doron aml_append(sb_scope, build_vmbus_device_aml(vmbus_bridge));
14528f814ea1SJon Doron aml_append(dsdt, sb_scope);
14538f814ea1SJon Doron }
14548f814ea1SJon Doron
1455d12dbd44SIgor Mammedov scope = aml_scope("_GPE");
1456d12dbd44SIgor Mammedov {
1457d12dbd44SIgor Mammedov aml_append(scope, aml_name_decl("_HID", aml_string("ACPI0006")));
1458d12dbd44SIgor Mammedov if (machine->nvdimms_state->is_enabled) {
1459d12dbd44SIgor Mammedov method = aml_method("_E04", 0, AML_NOTSERIALIZED);
1460d12dbd44SIgor Mammedov aml_append(method, aml_notify(aml_name("\\_SB.NVDR"),
1461d12dbd44SIgor Mammedov aml_int(0x80)));
1462d12dbd44SIgor Mammedov aml_append(scope, method);
1463d12dbd44SIgor Mammedov }
1464d12dbd44SIgor Mammedov }
1465d12dbd44SIgor Mammedov aml_append(dsdt, scope);
1466d12dbd44SIgor Mammedov
1467679dd1a9SIgor Mammedov if (pcmc->legacy_cpu_hotplug) {
1468ebd8ea82SIgor Mammedov build_legacy_cpu_hotplug_aml(dsdt, machine, pm->cpu_hp_io_base);
1469679dd1a9SIgor Mammedov } else {
1470679dd1a9SIgor Mammedov CPUHotplugFeatures opts = {
1471998ba950SIgor Mammedov .acpi_1_compatible = true, .has_legacy_cphp = true,
1472998ba950SIgor Mammedov .smi_path = pm->smi_on_cpuhp ? "\\_SB.PCI0.SMI0.SMIC" : NULL,
147369dea9d6SIgor Mammedov .fw_unplugs_cpu = pm->smi_on_cpu_unplug,
1474679dd1a9SIgor Mammedov };
14759a4fedcfSBernhard Beschow build_cpus_aml(dsdt, machine, opts, pc_madt_cpu_entry,
1476efdb43b8SSalil Mehta pm->cpu_hp_io_base, "\\_SB.PCI0", "\\_GPE._E02",
1477efdb43b8SSalil Mehta AML_SYSTEM_IO);
1478679dd1a9SIgor Mammedov }
1479091c466eSShameer Kolothum
1480091c466eSShameer Kolothum if (pcms->memhp_io_base && nr_mem) {
1481091c466eSShameer Kolothum build_memory_hotplug_aml(dsdt, nr_mem, "\\_SB.PCI0",
1482091c466eSShameer Kolothum "\\_GPE._E03", AML_SYSTEM_IO,
1483091c466eSShameer Kolothum pcms->memhp_io_base);
1484091c466eSShameer Kolothum }
148541fa5c04SIgor Mammedov
14862df5a7b5SMarcel Apfelbaum crs_range_set_init(&crs_range_set);
1487b54a9d46SBernhard Beschow bus = PC_MACHINE(machine)->pcibus;
1488a4894206SMarcel Apfelbaum if (bus) {
1489a4894206SMarcel Apfelbaum QLIST_FOREACH(bus, &bus->child, sibling) {
1490a4894206SMarcel Apfelbaum uint8_t bus_num = pci_bus_num(bus);
14910e79e51aSMarcel Apfelbaum uint8_t numa_node = pci_bus_numa_node(bus);
1492dc907b5cSJonathan Cameron uint32_t uid;
1493a4894206SMarcel Apfelbaum
1494a4894206SMarcel Apfelbaum /* look only for expander root buses */
1495a4894206SMarcel Apfelbaum if (!pci_bus_is_root(bus)) {
1496a4894206SMarcel Apfelbaum continue;
1497a4894206SMarcel Apfelbaum }
1498a4894206SMarcel Apfelbaum
1499dcdca296SMarcel Apfelbaum if (bus_num < root_bus_limit) {
1500dcdca296SMarcel Apfelbaum root_bus_limit = bus_num - 1;
1501dcdca296SMarcel Apfelbaum }
1502dcdca296SMarcel Apfelbaum
1503dc907b5cSJonathan Cameron uid = object_property_get_uint(OBJECT(bus), "acpi_uid",
1504dc907b5cSJonathan Cameron &error_fatal);
1505a4894206SMarcel Apfelbaum scope = aml_scope("\\_SB");
15066e4e3ae9SBen Widawsky
15076e4e3ae9SBen Widawsky if (pci_bus_is_cxl(bus)) {
15086e4e3ae9SBen Widawsky dev = aml_device("CL%.02X", bus_num);
15096e4e3ae9SBen Widawsky } else {
1510a4894206SMarcel Apfelbaum dev = aml_device("PC%.02X", bus_num);
15116e4e3ae9SBen Widawsky }
1512dc907b5cSJonathan Cameron aml_append(dev, aml_name_decl("_UID", aml_int(uid)));
1513a4894206SMarcel Apfelbaum aml_append(dev, aml_name_decl("_BBN", aml_int(bus_num)));
15146e4e3ae9SBen Widawsky if (pci_bus_is_cxl(bus)) {
15157b393b71SAni Sinha struct Aml *aml_pkg = aml_package(2);
15166e4e3ae9SBen Widawsky
15172a3282c6SBen Widawsky aml_append(dev, aml_name_decl("_HID", aml_string("ACPI0016")));
15187b393b71SAni Sinha aml_append(aml_pkg, aml_eisaid("PNP0A08"));
15197b393b71SAni Sinha aml_append(aml_pkg, aml_eisaid("PNP0A03"));
15207b393b71SAni Sinha aml_append(dev, aml_name_decl("_CID", aml_pkg));
15212a3282c6SBen Widawsky build_cxl_osc_method(dev);
15226e4e3ae9SBen Widawsky } else if (pci_bus_is_express(bus)) {
1523ee4b0c86SEvgeny Yakovlev aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A08")));
1524ee4b0c86SEvgeny Yakovlev aml_append(dev, aml_name_decl("_CID", aml_eisaid("PNP0A03")));
1525211afe5cSJulia Suvorova
1526211afe5cSJulia Suvorova /* Expander bridges do not have ACPI PCI Hot-plug enabled */
1527211afe5cSJulia Suvorova aml_append(dev, build_q35_osc_method(true));
1528ee4b0c86SEvgeny Yakovlev } else {
1529ee4b0c86SEvgeny Yakovlev aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A03")));
1530077dd742SMarcel Apfelbaum }
15310e79e51aSMarcel Apfelbaum
15320e79e51aSMarcel Apfelbaum if (numa_node != NUMA_NODE_UNASSIGNED) {
15330e79e51aSMarcel Apfelbaum aml_append(dev, aml_name_decl("_PXM", aml_int(numa_node)));
15340e79e51aSMarcel Apfelbaum }
15350e79e51aSMarcel Apfelbaum
1536196e2137SIgor Mammedov aml_append(dev, build_prt(false));
1537e41ee855SJiahui Cen crs = build_crs(PCI_HOST_BRIDGE(BUS(bus)->parent), &crs_range_set,
1538e41ee855SJiahui Cen 0, 0, 0, 0);
1539a43c6e27SMarcel Apfelbaum aml_append(dev, aml_name_decl("_CRS", crs));
1540a4894206SMarcel Apfelbaum aml_append(scope, dev);
154141fa5c04SIgor Mammedov aml_append(dsdt, scope);
15426e4e3ae9SBen Widawsky
15436e4e3ae9SBen Widawsky /* Handle the ranges for the PXB expanders */
15446e4e3ae9SBen Widawsky if (pci_bus_is_cxl(bus)) {
15451ebf9001SJonathan Cameron MemoryRegion *mr = &pcms->cxl_devices_state.host_mr;
15466e4e3ae9SBen Widawsky uint64_t base = mr->addr;
15476e4e3ae9SBen Widawsky
15483d6a69b6SBen Widawsky cxl_present = true;
15496e4e3ae9SBen Widawsky crs_range_insert(crs_range_set.mem_ranges, base,
15506e4e3ae9SBen Widawsky base + memory_region_size(mr) - 1);
15516e4e3ae9SBen Widawsky }
1552a4894206SMarcel Apfelbaum }
1553a4894206SMarcel Apfelbaum }
1554a4894206SMarcel Apfelbaum
15553d6a69b6SBen Widawsky if (cxl_present) {
15563d6a69b6SBen Widawsky build_acpi0017(dsdt);
15573d6a69b6SBen Widawsky }
15583d6a69b6SBen Widawsky
15594a441836SGerd Hoffmann /*
15604a441836SGerd Hoffmann * At this point crs_range_set has all the ranges used by pci
15614a441836SGerd Hoffmann * busses *other* than PCI0. These ranges will be excluded from
15624a441836SGerd Hoffmann * the PCI0._CRS. Add mmconfig to the set so it will be excluded
15634a441836SGerd Hoffmann * too.
15644a441836SGerd Hoffmann */
1565e3fb55f0SIsaku Yamahata if (mcfg_valid) {
15664a441836SGerd Hoffmann crs_range_insert(crs_range_set.mem_ranges,
15674a441836SGerd Hoffmann mcfg.base, mcfg.base + mcfg.size - 1);
15684a441836SGerd Hoffmann }
15694a441836SGerd Hoffmann
1570500b11eaSIgor Mammedov scope = aml_scope("\\_SB.PCI0");
157160efd429SIgor Mammedov /* build PCI0._CRS */
157260efd429SIgor Mammedov crs = aml_resource_template();
157360efd429SIgor Mammedov aml_append(crs,
1574ff80dc7fSShannon Zhao aml_word_bus_number(AML_MIN_FIXED, AML_MAX_FIXED, AML_POS_DECODE,
1575dcdca296SMarcel Apfelbaum 0x0000, 0x0, root_bus_limit,
1576dcdca296SMarcel Apfelbaum 0x0000, root_bus_limit + 1));
1577ff80dc7fSShannon Zhao aml_append(crs, aml_io(AML_DECODE16, 0x0CF8, 0x0CF8, 0x01, 0x08));
157860efd429SIgor Mammedov
157960efd429SIgor Mammedov aml_append(crs,
1580ff80dc7fSShannon Zhao aml_word_io(AML_MIN_FIXED, AML_MAX_FIXED,
1581ff80dc7fSShannon Zhao AML_POS_DECODE, AML_ENTIRE_RANGE,
158260efd429SIgor Mammedov 0x0000, 0x0000, 0x0CF7, 0x0000, 0x0CF8));
1583dcdca296SMarcel Apfelbaum
15842df5a7b5SMarcel Apfelbaum crs_replace_with_free_ranges(crs_range_set.io_ranges, 0x0D00, 0xFFFF);
15852df5a7b5SMarcel Apfelbaum for (i = 0; i < crs_range_set.io_ranges->len; i++) {
15862df5a7b5SMarcel Apfelbaum entry = g_ptr_array_index(crs_range_set.io_ranges, i);
158760efd429SIgor Mammedov aml_append(crs,
1588ff80dc7fSShannon Zhao aml_word_io(AML_MIN_FIXED, AML_MAX_FIXED,
1589ff80dc7fSShannon Zhao AML_POS_DECODE, AML_ENTIRE_RANGE,
1590dcdca296SMarcel Apfelbaum 0x0000, entry->base, entry->limit,
1591dcdca296SMarcel Apfelbaum 0x0000, entry->limit - entry->base + 1));
1592dcdca296SMarcel Apfelbaum }
1593dcdca296SMarcel Apfelbaum
159460efd429SIgor Mammedov aml_append(crs,
1595ff80dc7fSShannon Zhao aml_dword_memory(AML_POS_DECODE, AML_MIN_FIXED, AML_MAX_FIXED,
1596ff80dc7fSShannon Zhao AML_CACHEABLE, AML_READ_WRITE,
159760efd429SIgor Mammedov 0, 0x000A0000, 0x000BFFFF, 0, 0x00020000));
1598dcdca296SMarcel Apfelbaum
15992df5a7b5SMarcel Apfelbaum crs_replace_with_free_ranges(crs_range_set.mem_ranges,
1600a0efbf16SMarkus Armbruster range_lob(pci_hole),
1601a0efbf16SMarkus Armbruster range_upb(pci_hole));
16022df5a7b5SMarcel Apfelbaum for (i = 0; i < crs_range_set.mem_ranges->len; i++) {
16032df5a7b5SMarcel Apfelbaum entry = g_ptr_array_index(crs_range_set.mem_ranges, i);
160460efd429SIgor Mammedov aml_append(crs,
1605ff80dc7fSShannon Zhao aml_dword_memory(AML_POS_DECODE, AML_MIN_FIXED, AML_MAX_FIXED,
1606ff80dc7fSShannon Zhao AML_NON_CACHEABLE, AML_READ_WRITE,
1607dcdca296SMarcel Apfelbaum 0, entry->base, entry->limit,
1608dcdca296SMarcel Apfelbaum 0, entry->limit - entry->base + 1));
1609dcdca296SMarcel Apfelbaum }
1610dcdca296SMarcel Apfelbaum
1611a0efbf16SMarkus Armbruster if (!range_is_empty(pci_hole64)) {
161216de88a4SMarcel Apfelbaum crs_replace_with_free_ranges(crs_range_set.mem_64bit_ranges,
161316de88a4SMarcel Apfelbaum range_lob(pci_hole64),
161416de88a4SMarcel Apfelbaum range_upb(pci_hole64));
161516de88a4SMarcel Apfelbaum for (i = 0; i < crs_range_set.mem_64bit_ranges->len; i++) {
161616de88a4SMarcel Apfelbaum entry = g_ptr_array_index(crs_range_set.mem_64bit_ranges, i);
161760efd429SIgor Mammedov aml_append(crs,
161816de88a4SMarcel Apfelbaum aml_qword_memory(AML_POS_DECODE, AML_MIN_FIXED,
161916de88a4SMarcel Apfelbaum AML_MAX_FIXED,
1620ff80dc7fSShannon Zhao AML_CACHEABLE, AML_READ_WRITE,
162116de88a4SMarcel Apfelbaum 0, entry->base, entry->limit,
162216de88a4SMarcel Apfelbaum 0, entry->limit - entry->base + 1));
162316de88a4SMarcel Apfelbaum }
162460efd429SIgor Mammedov }
16252b1c2e8eSIgor Mammedov
162611fb99e6SStefan Berger #ifdef CONFIG_TPM
162743bc7f84SEric Auger if (TPM_IS_TIS_ISA(tpm_find())) {
16282b1c2e8eSIgor Mammedov aml_append(crs, aml_memory32_fixed(TPM_TIS_ADDR_BASE,
16292b1c2e8eSIgor Mammedov TPM_TIS_ADDR_SIZE, AML_READ_WRITE));
16302b1c2e8eSIgor Mammedov }
163111fb99e6SStefan Berger #endif
163260efd429SIgor Mammedov aml_append(scope, aml_name_decl("_CRS", crs));
163360efd429SIgor Mammedov
1634d31c909eSIgor Mammedov /* reserve GPE0 block resources */
1635d31c909eSIgor Mammedov dev = aml_device("GPE0");
1636d31c909eSIgor Mammedov aml_append(dev, aml_name_decl("_HID", aml_string("PNP0A06")));
1637d31c909eSIgor Mammedov aml_append(dev, aml_name_decl("_UID", aml_string("GPE0 resources")));
1638d31c909eSIgor Mammedov /* device present, functioning, decoding, not shown in UI */
1639d31c909eSIgor Mammedov aml_append(dev, aml_name_decl("_STA", aml_int(0xB)));
1640d31c909eSIgor Mammedov crs = aml_resource_template();
1641d31c909eSIgor Mammedov aml_append(crs,
1642937d1b58SIgor Mammedov aml_io(
1643937d1b58SIgor Mammedov AML_DECODE16,
1644937d1b58SIgor Mammedov pm->fadt.gpe0_blk.address,
1645937d1b58SIgor Mammedov pm->fadt.gpe0_blk.address,
1646937d1b58SIgor Mammedov 1,
1647937d1b58SIgor Mammedov pm->fadt.gpe0_blk.bit_width / 8)
1648d31c909eSIgor Mammedov );
1649d31c909eSIgor Mammedov aml_append(dev, aml_name_decl("_CRS", crs));
1650d31c909eSIgor Mammedov aml_append(scope, dev);
1651d31c909eSIgor Mammedov
16522df5a7b5SMarcel Apfelbaum crs_range_set_free(&crs_range_set);
1653dcdca296SMarcel Apfelbaum
1654500b11eaSIgor Mammedov /* reserve PCIHP resources */
1655df4008c9SAni Sinha if (pm->pcihp_io_len && (pm->pcihp_bridge_en || pm->pcihp_root_en)) {
1656500b11eaSIgor Mammedov dev = aml_device("PHPR");
1657500b11eaSIgor Mammedov aml_append(dev, aml_name_decl("_HID", aml_string("PNP0A06")));
1658500b11eaSIgor Mammedov aml_append(dev,
1659500b11eaSIgor Mammedov aml_name_decl("_UID", aml_string("PCI Hotplug resources")));
1660500b11eaSIgor Mammedov /* device present, functioning, decoding, not shown in UI */
1661500b11eaSIgor Mammedov aml_append(dev, aml_name_decl("_STA", aml_int(0xB)));
1662500b11eaSIgor Mammedov crs = aml_resource_template();
1663500b11eaSIgor Mammedov aml_append(crs,
1664ff80dc7fSShannon Zhao aml_io(AML_DECODE16, pm->pcihp_io_base, pm->pcihp_io_base, 1,
1665500b11eaSIgor Mammedov pm->pcihp_io_len)
1666500b11eaSIgor Mammedov );
1667500b11eaSIgor Mammedov aml_append(dev, aml_name_decl("_CRS", crs));
1668500b11eaSIgor Mammedov aml_append(scope, dev);
1669500b11eaSIgor Mammedov }
167041fa5c04SIgor Mammedov aml_append(dsdt, scope);
1671500b11eaSIgor Mammedov
1672ebc3028fSIgor Mammedov /* create S3_ / S4_ / S5_ packages if necessary */
1673ebc3028fSIgor Mammedov scope = aml_scope("\\");
1674ebc3028fSIgor Mammedov if (!pm->s3_disabled) {
1675ebc3028fSIgor Mammedov pkg = aml_package(4);
1676ebc3028fSIgor Mammedov aml_append(pkg, aml_int(1)); /* PM1a_CNT.SLP_TYP */
1677ebc3028fSIgor Mammedov aml_append(pkg, aml_int(1)); /* PM1b_CNT.SLP_TYP, FIXME: not impl. */
1678ebc3028fSIgor Mammedov aml_append(pkg, aml_int(0)); /* reserved */
1679ebc3028fSIgor Mammedov aml_append(pkg, aml_int(0)); /* reserved */
1680ebc3028fSIgor Mammedov aml_append(scope, aml_name_decl("_S3", pkg));
1681ebc3028fSIgor Mammedov }
1682ebc3028fSIgor Mammedov
1683ebc3028fSIgor Mammedov if (!pm->s4_disabled) {
1684ebc3028fSIgor Mammedov pkg = aml_package(4);
1685ebc3028fSIgor Mammedov aml_append(pkg, aml_int(pm->s4_val)); /* PM1a_CNT.SLP_TYP */
1686ebc3028fSIgor Mammedov /* PM1b_CNT.SLP_TYP, FIXME: not impl. */
1687ebc3028fSIgor Mammedov aml_append(pkg, aml_int(pm->s4_val));
1688ebc3028fSIgor Mammedov aml_append(pkg, aml_int(0)); /* reserved */
1689ebc3028fSIgor Mammedov aml_append(pkg, aml_int(0)); /* reserved */
1690ebc3028fSIgor Mammedov aml_append(scope, aml_name_decl("_S4", pkg));
1691ebc3028fSIgor Mammedov }
1692ebc3028fSIgor Mammedov
1693ebc3028fSIgor Mammedov pkg = aml_package(4);
1694ebc3028fSIgor Mammedov aml_append(pkg, aml_int(0)); /* PM1a_CNT.SLP_TYP */
1695ebc3028fSIgor Mammedov aml_append(pkg, aml_int(0)); /* PM1b_CNT.SLP_TYP not impl. */
1696ebc3028fSIgor Mammedov aml_append(pkg, aml_int(0)); /* reserved */
1697ebc3028fSIgor Mammedov aml_append(pkg, aml_int(0)); /* reserved */
1698ebc3028fSIgor Mammedov aml_append(scope, aml_name_decl("_S5", pkg));
169941fa5c04SIgor Mammedov aml_append(dsdt, scope);
1700ebc3028fSIgor Mammedov
1701e2ec7568SGabriel L. Somlo /* create fw_cfg node, unconditionally */
1702e2ec7568SGabriel L. Somlo {
1703e2ec7568SGabriel L. Somlo scope = aml_scope("\\_SB.PCI0");
17040575c2fdSGerd Hoffmann fw_cfg_add_acpi_dsdt(scope, x86ms->fw_cfg);
1705e2ec7568SGabriel L. Somlo aml_append(dsdt, scope);
1706e2ec7568SGabriel L. Somlo }
1707e2ec7568SGabriel L. Somlo
17087824df38SGal Hammer sb_scope = aml_scope("\\_SB");
170972c194f7SMichael S. Tsirkin {
1710d3ecb22cSIgor Mammedov Object *pci_host = acpi_get_i386_pci_host();
1711c0e427d6SJulia Suvorova
1712ca6c1855SMarcel Apfelbaum if (pci_host) {
17137b393b71SAni Sinha PCIBus *pbus = PCI_HOST_BRIDGE(pci_host)->bus;
17147b393b71SAni Sinha Aml *ascope = aml_scope("PCI0");
171599fd437dSMichael S. Tsirkin /* Scan all PCI buses. Generate tables to support hotplug. */
17167b393b71SAni Sinha build_append_pci_bus_devices(ascope, pbus);
17177b393b71SAni Sinha if (object_property_find(OBJECT(pbus), ACPI_PCIHP_PROP_BSEL)) {
17187b393b71SAni Sinha build_append_pcihp_slots(ascope, pbus);
171902c10613SIgor Mammedov }
17207b393b71SAni Sinha aml_append(sb_scope, ascope);
172172c194f7SMichael S. Tsirkin }
172272c194f7SMichael S. Tsirkin }
17234ab6cb4cSMarc-André Lureau
172411fb99e6SStefan Berger #ifdef CONFIG_TPM
1725ac6dd31eSStefan Berger if (TPM_IS_CRB(tpm)) {
17264ab6cb4cSMarc-André Lureau dev = aml_device("TPM");
17274ab6cb4cSMarc-André Lureau aml_append(dev, aml_name_decl("_HID", aml_string("MSFT0101")));
17285903646dSStefan Berger aml_append(dev, aml_name_decl("_STR",
17295903646dSStefan Berger aml_string("TPM 2.0 Device")));
17304ab6cb4cSMarc-André Lureau crs = aml_resource_template();
17314ab6cb4cSMarc-André Lureau aml_append(crs, aml_memory32_fixed(TPM_CRB_ADDR_BASE,
17324ab6cb4cSMarc-André Lureau TPM_CRB_ADDR_SIZE, AML_READ_WRITE));
17334ab6cb4cSMarc-André Lureau aml_append(dev, aml_name_decl("_CRS", crs));
17344ab6cb4cSMarc-André Lureau
173588b3648fSGerd Hoffmann aml_append(dev, aml_name_decl("_STA", aml_int(0xf)));
17365903646dSStefan Berger aml_append(dev, aml_name_decl("_UID", aml_int(1)));
17374ab6cb4cSMarc-André Lureau
1738ac6dd31eSStefan Berger tpm_build_ppi_acpi(tpm, dev);
1739ac6dd31eSStefan Berger
17404ab6cb4cSMarc-André Lureau aml_append(sb_scope, dev);
17414ab6cb4cSMarc-André Lureau }
174211fb99e6SStefan Berger #endif
17434ab6cb4cSMarc-André Lureau
1744c8a9899cSSean Christopherson if (pcms->sgx_epc.size != 0) {
1745c8a9899cSSean Christopherson uint64_t epc_base = pcms->sgx_epc.base;
1746c8a9899cSSean Christopherson uint64_t epc_size = pcms->sgx_epc.size;
1747c8a9899cSSean Christopherson
1748c8a9899cSSean Christopherson dev = aml_device("EPC");
1749c8a9899cSSean Christopherson aml_append(dev, aml_name_decl("_HID", aml_eisaid("INT0E0C")));
1750c8a9899cSSean Christopherson aml_append(dev, aml_name_decl("_STR",
1751c8a9899cSSean Christopherson aml_unicode("Enclave Page Cache 1.0")));
1752c8a9899cSSean Christopherson crs = aml_resource_template();
1753c8a9899cSSean Christopherson aml_append(crs,
1754c8a9899cSSean Christopherson aml_qword_memory(AML_POS_DECODE, AML_MIN_FIXED,
1755c8a9899cSSean Christopherson AML_MAX_FIXED, AML_NON_CACHEABLE,
1756c8a9899cSSean Christopherson AML_READ_WRITE, 0, epc_base,
1757c8a9899cSSean Christopherson epc_base + epc_size - 1, 0, epc_size));
1758c8a9899cSSean Christopherson aml_append(dev, aml_name_decl("_CRS", crs));
1759c8a9899cSSean Christopherson
1760c8a9899cSSean Christopherson method = aml_method("_STA", 0, AML_NOTSERIALIZED);
1761c8a9899cSSean Christopherson aml_append(method, aml_return(aml_int(0x0f)));
1762c8a9899cSSean Christopherson aml_append(dev, method);
1763c8a9899cSSean Christopherson
1764c8a9899cSSean Christopherson aml_append(sb_scope, dev);
1765c8a9899cSSean Christopherson }
176641fa5c04SIgor Mammedov aml_append(dsdt, sb_scope);
176772c194f7SMichael S. Tsirkin
1768d12dbd44SIgor Mammedov if (pm->pcihp_bridge_en || pm->pcihp_root_en) {
1769219e638fSIgor Mammedov bool has_pcnt;
1770219e638fSIgor Mammedov
1771ddab4d3fSIgor Mammedov Object *pci_host = acpi_get_i386_pci_host();
17727b393b71SAni Sinha PCIBus *b = PCI_HOST_BRIDGE(pci_host)->bus;
1773ddab4d3fSIgor Mammedov
1774ddab4d3fSIgor Mammedov scope = aml_scope("\\_SB.PCI0");
17757b393b71SAni Sinha has_pcnt = build_append_notfication_callback(scope, b);
1776219e638fSIgor Mammedov if (has_pcnt) {
1777ddab4d3fSIgor Mammedov aml_append(dsdt, scope);
1778219e638fSIgor Mammedov }
1779ddab4d3fSIgor Mammedov
1780d12dbd44SIgor Mammedov scope = aml_scope("_GPE");
1781d12dbd44SIgor Mammedov {
1782d12dbd44SIgor Mammedov method = aml_method("_E01", 0, AML_NOTSERIALIZED);
1783219e638fSIgor Mammedov if (has_pcnt) {
1784d12dbd44SIgor Mammedov aml_append(method,
1785d12dbd44SIgor Mammedov aml_acquire(aml_name("\\_SB.PCI0.BLCK"), 0xFFFF));
1786d12dbd44SIgor Mammedov aml_append(method, aml_call0("\\_SB.PCI0.PCNT"));
1787d12dbd44SIgor Mammedov aml_append(method, aml_release(aml_name("\\_SB.PCI0.BLCK")));
1788219e638fSIgor Mammedov }
1789d12dbd44SIgor Mammedov aml_append(scope, method);
1790d12dbd44SIgor Mammedov }
1791d12dbd44SIgor Mammedov aml_append(dsdt, scope);
1792d12dbd44SIgor Mammedov }
1793d12dbd44SIgor Mammedov
1794011bb749SIgor Mammedov /* copy AML table into ACPI tables blob and patch header there */
179541fa5c04SIgor Mammedov g_array_append_vals(table_data, dsdt->buf->data, dsdt->buf->len);
17965c142bc4SIgor Mammedov acpi_table_end(linker, &table);
1797011bb749SIgor Mammedov free_aml_allocator();
179872c194f7SMichael S. Tsirkin }
179972c194f7SMichael S. Tsirkin
180043dde170SIgor Mammedov /*
180143dde170SIgor Mammedov * IA-PC HPET (High Precision Event Timers) Specification (Revision: 1.0a)
180243dde170SIgor Mammedov * 3.2.4The ACPI 2.0 HPET Description Table (HPET)
180343dde170SIgor Mammedov */
180472c194f7SMichael S. Tsirkin static void
build_hpet(GArray * table_data,BIOSLinker * linker,const char * oem_id,const char * oem_table_id)1805602b4582SMarian Postevca build_hpet(GArray *table_data, BIOSLinker *linker, const char *oem_id,
1806602b4582SMarian Postevca const char *oem_table_id)
180772c194f7SMichael S. Tsirkin {
180843dde170SIgor Mammedov AcpiTable table = { .sig = "HPET", .rev = 1,
180943dde170SIgor Mammedov .oem_id = oem_id, .oem_table_id = oem_table_id };
181072c194f7SMichael S. Tsirkin
181143dde170SIgor Mammedov acpi_table_begin(&table, table_data);
181272c194f7SMichael S. Tsirkin /* Note timer_block_id value must be kept in sync with value advertised by
181372c194f7SMichael S. Tsirkin * emulated hpet
181472c194f7SMichael S. Tsirkin */
181543dde170SIgor Mammedov /* Event Timer Block ID */
181643dde170SIgor Mammedov build_append_int_noprefix(table_data, 0x8086a201, 4);
181743dde170SIgor Mammedov /* BASE_ADDRESS */
181843dde170SIgor Mammedov build_append_gas(table_data, AML_AS_SYSTEM_MEMORY, 0, 0, 0, HPET_BASE);
181943dde170SIgor Mammedov /* HPET Number */
182043dde170SIgor Mammedov build_append_int_noprefix(table_data, 0, 1);
182143dde170SIgor Mammedov /* Main Counter Minimum Clock_tick in Periodic Mode */
182243dde170SIgor Mammedov build_append_int_noprefix(table_data, 0, 2);
182343dde170SIgor Mammedov /* Page Protection And OEM Attribute */
182443dde170SIgor Mammedov build_append_int_noprefix(table_data, 0, 1);
182543dde170SIgor Mammedov acpi_table_end(linker, &table);
182672c194f7SMichael S. Tsirkin }
182772c194f7SMichael S. Tsirkin
182811fb99e6SStefan Berger #ifdef CONFIG_TPM
182957cb8cfbSIgor Mammedov /*
183057cb8cfbSIgor Mammedov * TCPA Description Table
183157cb8cfbSIgor Mammedov *
183257cb8cfbSIgor Mammedov * Following Level 00, Rev 00.37 of specs:
183357cb8cfbSIgor Mammedov * http://www.trustedcomputinggroup.org/resources/tcg_acpi_specification
183457cb8cfbSIgor Mammedov * 7.1.2 ACPI Table Layout
183557cb8cfbSIgor Mammedov */
1836711b20b4SStefan Berger static void
build_tpm_tcpa(GArray * table_data,BIOSLinker * linker,GArray * tcpalog,const char * oem_id,const char * oem_table_id)1837602b4582SMarian Postevca build_tpm_tcpa(GArray *table_data, BIOSLinker *linker, GArray *tcpalog,
1838602b4582SMarian Postevca const char *oem_id, const char *oem_table_id)
1839711b20b4SStefan Berger {
184057cb8cfbSIgor Mammedov unsigned log_addr_offset;
184157cb8cfbSIgor Mammedov AcpiTable table = { .sig = "TCPA", .rev = 2,
184257cb8cfbSIgor Mammedov .oem_id = oem_id, .oem_table_id = oem_table_id };
1843711b20b4SStefan Berger
184457cb8cfbSIgor Mammedov acpi_table_begin(&table, table_data);
184557cb8cfbSIgor Mammedov /* Platform Class */
184657cb8cfbSIgor Mammedov build_append_int_noprefix(table_data, TPM_TCPA_ACPI_CLASS_CLIENT, 2);
184757cb8cfbSIgor Mammedov /* Log Area Minimum Length (LAML) */
184857cb8cfbSIgor Mammedov build_append_int_noprefix(table_data, TPM_LOG_AREA_MINIMUM_SIZE, 4);
184957cb8cfbSIgor Mammedov /* Log Area Start Address (LASA) */
185057cb8cfbSIgor Mammedov log_addr_offset = table_data->len;
185157cb8cfbSIgor Mammedov build_append_int_noprefix(table_data, 0, 8);
1852711b20b4SStefan Berger
185357cb8cfbSIgor Mammedov /* allocate/reserve space for TPM log area */
185457cb8cfbSIgor Mammedov acpi_data_push(tcpalog, TPM_LOG_AREA_MINIMUM_SIZE);
1855ad9671b8SIgor Mammedov bios_linker_loader_alloc(linker, ACPI_BUILD_TPMLOG_FILE, tcpalog, 1,
185642a5b308SStefan Berger false /* high memory */);
1857711b20b4SStefan Berger /* log area start address to be filled by Guest linker */
185857cb8cfbSIgor Mammedov bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
185957cb8cfbSIgor Mammedov log_addr_offset, 8, ACPI_BUILD_TPMLOG_FILE, 0);
1860711b20b4SStefan Berger
186157cb8cfbSIgor Mammedov acpi_table_end(linker, &table);
1862711b20b4SStefan Berger }
186311fb99e6SStefan Berger #endif
1864711b20b4SStefan Berger
1865d471bf3eSPaolo Bonzini #define HOLE_640K_START (640 * KiB)
1866d471bf3eSPaolo Bonzini #define HOLE_640K_END (1 * MiB)
18674926403cSEduardo Habkost
1868e5b6d55aSIgor Mammedov /*
1869e5b6d55aSIgor Mammedov * ACPI spec, Revision 3.0
1870e5b6d55aSIgor Mammedov * 5.2.15 System Resource Affinity Table (SRAT)
1871e5b6d55aSIgor Mammedov */
187272c194f7SMichael S. Tsirkin static void
build_srat(GArray * table_data,BIOSLinker * linker,MachineState * machine)18730e9b9edaSIgor Mammedov build_srat(GArray *table_data, BIOSLinker *linker, MachineState *machine)
187472c194f7SMichael S. Tsirkin {
187572c194f7SMichael S. Tsirkin int i;
1876e5b6d55aSIgor Mammedov int numa_mem_start, slots;
187772c194f7SMichael S. Tsirkin uint64_t mem_len, mem_base, next_base;
18785803fce3SIgor Mammedov MachineClass *mc = MACHINE_GET_CLASS(machine);
1879f0bb276bSPaolo Bonzini X86MachineState *x86ms = X86_MACHINE(machine);
188080e5db30SIgor Mammedov const CPUArchIdList *apic_ids = mc->possible_cpu_arch_ids(machine);
1881e77af21aSJingqi Liu int nb_numa_nodes = machine->numa_state->num_nodes;
1882e77af21aSJingqi Liu NodeInfo *numa_info = machine->numa_state->nodes;
1883255bf20fSIgor Mammedov AcpiTable table = { .sig = "SRAT", .rev = 1, .oem_id = x86ms->oem_id,
1884255bf20fSIgor Mammedov .oem_table_id = x86ms->oem_table_id };
188572c194f7SMichael S. Tsirkin
1886255bf20fSIgor Mammedov acpi_table_begin(&table, table_data);
1887255bf20fSIgor Mammedov build_append_int_noprefix(table_data, 1, 4); /* Reserved */
1888255bf20fSIgor Mammedov build_append_int_noprefix(table_data, 0, 8); /* Reserved */
188972c194f7SMichael S. Tsirkin
18905803fce3SIgor Mammedov for (i = 0; i < apic_ids->len; i++) {
1891d41f3e75SIgor Mammedov int node_id = apic_ids->cpus[i].props.node_id;
18925eff33a2SIgor Mammedov uint32_t apic_id = apic_ids->cpus[i].arch_id;
18935eff33a2SIgor Mammedov
18945eff33a2SIgor Mammedov if (apic_id < 255) {
1895e5b6d55aSIgor Mammedov /* 5.2.15.1 Processor Local APIC/SAPIC Affinity Structure */
1896e5b6d55aSIgor Mammedov build_append_int_noprefix(table_data, 0, 1); /* Type */
1897e5b6d55aSIgor Mammedov build_append_int_noprefix(table_data, 16, 1); /* Length */
1898e5b6d55aSIgor Mammedov /* Proximity Domain [7:0] */
1899e5b6d55aSIgor Mammedov build_append_int_noprefix(table_data, node_id, 1);
1900e5b6d55aSIgor Mammedov build_append_int_noprefix(table_data, apic_id, 1); /* APIC ID */
1901e5b6d55aSIgor Mammedov /* Flags, Table 5-36 */
1902e5b6d55aSIgor Mammedov build_append_int_noprefix(table_data, 1, 4);
1903e5b6d55aSIgor Mammedov build_append_int_noprefix(table_data, 0, 1); /* Local SAPIC EID */
1904e5b6d55aSIgor Mammedov /* Proximity Domain [31:8] */
1905e5b6d55aSIgor Mammedov build_append_int_noprefix(table_data, 0, 3);
1906e5b6d55aSIgor Mammedov build_append_int_noprefix(table_data, 0, 4); /* Reserved */
19075eff33a2SIgor Mammedov } else {
1908e5b6d55aSIgor Mammedov /*
1909e5b6d55aSIgor Mammedov * ACPI spec, Revision 4.0
1910e5b6d55aSIgor Mammedov * 5.2.16.3 Processor Local x2APIC Affinity Structure
1911e5b6d55aSIgor Mammedov */
1912e5b6d55aSIgor Mammedov build_append_int_noprefix(table_data, 2, 1); /* Type */
1913e5b6d55aSIgor Mammedov build_append_int_noprefix(table_data, 24, 1); /* Length */
1914e5b6d55aSIgor Mammedov build_append_int_noprefix(table_data, 0, 2); /* Reserved */
1915e5b6d55aSIgor Mammedov /* Proximity Domain */
1916e5b6d55aSIgor Mammedov build_append_int_noprefix(table_data, node_id, 4);
1917e5b6d55aSIgor Mammedov build_append_int_noprefix(table_data, apic_id, 4); /* X2APIC ID */
1918e5b6d55aSIgor Mammedov /* Flags, Table 5-39 */
1919e5b6d55aSIgor Mammedov build_append_int_noprefix(table_data, 1 /* Enabled */, 4);
1920e5b6d55aSIgor Mammedov build_append_int_noprefix(table_data, 0, 4); /* Clock Domain */
1921e5b6d55aSIgor Mammedov build_append_int_noprefix(table_data, 0, 4); /* Reserved */
19225eff33a2SIgor Mammedov }
192372c194f7SMichael S. Tsirkin }
192472c194f7SMichael S. Tsirkin
192572c194f7SMichael S. Tsirkin /* the memory map is a bit tricky, it contains at least one hole
192672c194f7SMichael S. Tsirkin * from 640k-1M and possibly another one from 3.5G-4G.
192772c194f7SMichael S. Tsirkin */
192872c194f7SMichael S. Tsirkin next_base = 0;
1929e5b6d55aSIgor Mammedov numa_mem_start = table_data->len;
193072c194f7SMichael S. Tsirkin
1931e77af21aSJingqi Liu for (i = 1; i < nb_numa_nodes + 1; ++i) {
193272c194f7SMichael S. Tsirkin mem_base = next_base;
1933e77af21aSJingqi Liu mem_len = numa_info[i - 1].node_mem;
193472c194f7SMichael S. Tsirkin next_base = mem_base + mem_len;
193572c194f7SMichael S. Tsirkin
19364926403cSEduardo Habkost /* Cut out the 640K hole */
19374926403cSEduardo Habkost if (mem_base <= HOLE_640K_START &&
19384926403cSEduardo Habkost next_base > HOLE_640K_START) {
19394926403cSEduardo Habkost mem_len -= next_base - HOLE_640K_START;
19404926403cSEduardo Habkost if (mem_len > 0) {
1941e5b6d55aSIgor Mammedov build_srat_memory(table_data, mem_base, mem_len, i - 1,
19424926403cSEduardo Habkost MEM_AFFINITY_ENABLED);
19434926403cSEduardo Habkost }
19444926403cSEduardo Habkost
19454926403cSEduardo Habkost /* Check for the rare case: 640K < RAM < 1M */
19464926403cSEduardo Habkost if (next_base <= HOLE_640K_END) {
19474926403cSEduardo Habkost next_base = HOLE_640K_END;
19484926403cSEduardo Habkost continue;
19494926403cSEduardo Habkost }
19504926403cSEduardo Habkost mem_base = HOLE_640K_END;
19514926403cSEduardo Habkost mem_len = next_base - HOLE_640K_END;
19524926403cSEduardo Habkost }
19534926403cSEduardo Habkost
195472c194f7SMichael S. Tsirkin /* Cut out the ACPI_PCI hole */
1955f0bb276bSPaolo Bonzini if (mem_base <= x86ms->below_4g_mem_size &&
1956f0bb276bSPaolo Bonzini next_base > x86ms->below_4g_mem_size) {
1957f0bb276bSPaolo Bonzini mem_len -= next_base - x86ms->below_4g_mem_size;
195872c194f7SMichael S. Tsirkin if (mem_len > 0) {
1959e5b6d55aSIgor Mammedov build_srat_memory(table_data, mem_base, mem_len, i - 1,
196004ed3ea8SIgor Mammedov MEM_AFFINITY_ENABLED);
196172c194f7SMichael S. Tsirkin }
19624ab4c330SJoao Martins mem_base = x86ms->above_4g_mem_start;
1963f0bb276bSPaolo Bonzini mem_len = next_base - x86ms->below_4g_mem_size;
19646cf6fe39SDou Liyang next_base = mem_base + mem_len;
196572c194f7SMichael S. Tsirkin }
196616b42263SDou Liyang
196716b42263SDou Liyang if (mem_len > 0) {
1968e5b6d55aSIgor Mammedov build_srat_memory(table_data, mem_base, mem_len, i - 1,
196904ed3ea8SIgor Mammedov MEM_AFFINITY_ENABLED);
197072c194f7SMichael S. Tsirkin }
197116b42263SDou Liyang }
1972c3b0cf6eSVishal Verma
1973c3b0cf6eSVishal Verma if (machine->nvdimms_state->is_enabled) {
1974c3b0cf6eSVishal Verma nvdimm_build_srat(table_data);
1975c3b0cf6eSVishal Verma }
1976c3b0cf6eSVishal Verma
197711058123SYang Zhong sgx_epc_build_srat(table_data);
197811058123SYang Zhong
1979e5b6d55aSIgor Mammedov /*
1980e5b6d55aSIgor Mammedov * TODO: this part is not in ACPI spec and current linux kernel boots fine
1981e5b6d55aSIgor Mammedov * without these entries. But I recall there were issues the last time I
1982e5b6d55aSIgor Mammedov * tried to remove it with some ancient guest OS, however I can't remember
1983e5b6d55aSIgor Mammedov * what that was so keep this around for now
1984e5b6d55aSIgor Mammedov */
1985e5b6d55aSIgor Mammedov slots = (table_data->len - numa_mem_start) / 40 /* mem affinity len */;
1986e77af21aSJingqi Liu for (; slots < nb_numa_nodes + 2; slots++) {
1987e5b6d55aSIgor Mammedov build_srat_memory(table_data, 0, 0, 0, MEM_AFFINITY_NOFLAGS);
198872c194f7SMichael S. Tsirkin }
198972c194f7SMichael S. Tsirkin
1990a82fe829SJonathan Cameron build_srat_generic_affinity_structures(table_data);
19915deced6aSAnkit Agrawal
1992dbb6da8bSIgor Mammedov /*
1993dbb6da8bSIgor Mammedov * Entry is required for Windows to enable memory hotplug in OS
1994dbb6da8bSIgor Mammedov * and for Linux to enable SWIOTLB when booted with less than
1995dbb6da8bSIgor Mammedov * 4G of RAM. Windows works better if the entry sets proximity
1996dbb6da8bSIgor Mammedov * to the highest NUMA node in the machine.
1997dbb6da8bSIgor Mammedov * Memory devices may override proximity set by this entry,
1998dbb6da8bSIgor Mammedov * providing _PXM method if necessary.
1999dbb6da8bSIgor Mammedov */
200075d5f343SDavid Hildenbrand if (machine->device_memory) {
2001e5b6d55aSIgor Mammedov build_srat_memory(table_data, machine->device_memory->base,
200275d5f343SDavid Hildenbrand memory_region_size(&machine->device_memory->mr),
200375d5f343SDavid Hildenbrand nb_numa_nodes - 1,
2004dbb6da8bSIgor Mammedov MEM_AFFINITY_HOTPLUGGABLE | MEM_AFFINITY_ENABLED);
2005cec65193SIgor Mammedov }
2006cec65193SIgor Mammedov
2007255bf20fSIgor Mammedov acpi_table_end(linker, &table);
200872c194f7SMichael S. Tsirkin }
200972c194f7SMichael S. Tsirkin
2010d46114f9SPeter Xu /*
2011bad5cfcdSMichael Tokarev * Insert DMAR scope for PCI bridges and endpoint devices
201226863366SXingang Wang */
201326863366SXingang Wang static void
insert_scope(PCIBus * bus,PCIDevice * dev,void * opaque)201426863366SXingang Wang insert_scope(PCIBus *bus, PCIDevice *dev, void *opaque)
201526863366SXingang Wang {
201691a6b975SIgor Mammedov const size_t device_scope_size = 6 /* device scope structure */ +
201791a6b975SIgor Mammedov 2 /* 1 path entry */;
201826863366SXingang Wang GArray *scope_blob = opaque;
201926863366SXingang Wang
202026863366SXingang Wang if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_BRIDGE)) {
202126863366SXingang Wang /* Dmar Scope Type: 0x02 for PCI Bridge */
202226863366SXingang Wang build_append_int_noprefix(scope_blob, 0x02, 1);
202326863366SXingang Wang } else {
202426863366SXingang Wang /* Dmar Scope Type: 0x01 for PCI Endpoint Device */
202526863366SXingang Wang build_append_int_noprefix(scope_blob, 0x01, 1);
202626863366SXingang Wang }
202726863366SXingang Wang
202826863366SXingang Wang /* length */
202991a6b975SIgor Mammedov build_append_int_noprefix(scope_blob, device_scope_size, 1);
203026863366SXingang Wang /* reserved */
203126863366SXingang Wang build_append_int_noprefix(scope_blob, 0, 2);
203226863366SXingang Wang /* enumeration_id */
203326863366SXingang Wang build_append_int_noprefix(scope_blob, 0, 1);
203426863366SXingang Wang /* bus */
203526863366SXingang Wang build_append_int_noprefix(scope_blob, pci_bus_num(bus), 1);
203626863366SXingang Wang /* device */
203726863366SXingang Wang build_append_int_noprefix(scope_blob, PCI_SLOT(dev->devfn), 1);
203826863366SXingang Wang /* function */
203926863366SXingang Wang build_append_int_noprefix(scope_blob, PCI_FUNC(dev->devfn), 1);
204026863366SXingang Wang }
204126863366SXingang Wang
204226863366SXingang Wang /* For a given PCI host bridge, walk and insert DMAR scope */
204326863366SXingang Wang static int
dmar_host_bridges(Object * obj,void * opaque)204426863366SXingang Wang dmar_host_bridges(Object *obj, void *opaque)
204526863366SXingang Wang {
204626863366SXingang Wang GArray *scope_blob = opaque;
204726863366SXingang Wang
204826863366SXingang Wang if (object_dynamic_cast(obj, TYPE_PCI_HOST_BRIDGE)) {
204926863366SXingang Wang PCIBus *bus = PCI_HOST_BRIDGE(obj)->bus;
205026863366SXingang Wang
205126863366SXingang Wang if (bus && !pci_bus_bypass_iommu(bus)) {
20522914fc61SPeter Xu pci_for_each_device_under_bus(bus, insert_scope, scope_blob);
205326863366SXingang Wang }
205426863366SXingang Wang }
205526863366SXingang Wang
205626863366SXingang Wang return 0;
205726863366SXingang Wang }
205826863366SXingang Wang
205926863366SXingang Wang /*
206091a6b975SIgor Mammedov * Intel ® Virtualization Technology for Directed I/O
206191a6b975SIgor Mammedov * Architecture Specification. Revision 3.3
206291a6b975SIgor Mammedov * 8.1 DMA Remapping Reporting Structure
2063d46114f9SPeter Xu */
206472c194f7SMichael S. Tsirkin static void
build_dmar_q35(GArray * table_data,BIOSLinker * linker,const char * oem_id,const char * oem_table_id)2065602b4582SMarian Postevca build_dmar_q35(GArray *table_data, BIOSLinker *linker, const char *oem_id,
2066602b4582SMarian Postevca const char *oem_table_id)
2067d4eb9119SLe Tan {
2068d46114f9SPeter Xu uint8_t dmar_flags = 0;
206991a6b975SIgor Mammedov uint8_t rsvd10[10] = {};
207091a6b975SIgor Mammedov /* Root complex IOAPIC uses one path only */
207191a6b975SIgor Mammedov const size_t ioapic_scope_size = 6 /* device scope structure */ +
207291a6b975SIgor Mammedov 2 /* 1 path entry */;
2073d46114f9SPeter Xu X86IOMMUState *iommu = x86_iommu_get_default();
207437f51384SPrasad Singamsetty IntelIOMMUState *intel_iommu = INTEL_IOMMU_DEVICE(iommu);
207526863366SXingang Wang GArray *scope_blob = g_array_new(false, true, 1);
207626863366SXingang Wang
207791a6b975SIgor Mammedov AcpiTable table = { .sig = "DMAR", .rev = 1, .oem_id = oem_id,
207891a6b975SIgor Mammedov .oem_table_id = oem_table_id };
207991a6b975SIgor Mammedov
208026863366SXingang Wang /*
208126863366SXingang Wang * A PCI bus walk, for each PCI host bridge.
208226863366SXingang Wang * Insert scope for each PCI bridge and endpoint device which
208326863366SXingang Wang * is attached to a bus with iommu enabled.
208426863366SXingang Wang */
208526863366SXingang Wang object_child_foreach_recursive(object_get_root(),
208626863366SXingang Wang dmar_host_bridges, scope_blob);
2087d46114f9SPeter Xu
2088d46114f9SPeter Xu assert(iommu);
2089a924b3d8SPeter Xu if (x86_iommu_ir_supported(iommu)) {
2090d46114f9SPeter Xu dmar_flags |= 0x1; /* Flags: 0x1: INT_REMAP */
2091d46114f9SPeter Xu }
2092d4eb9119SLe Tan
209391a6b975SIgor Mammedov acpi_table_begin(&table, table_data);
209491a6b975SIgor Mammedov /* Host Address Width */
209591a6b975SIgor Mammedov build_append_int_noprefix(table_data, intel_iommu->aw_bits - 1, 1);
209691a6b975SIgor Mammedov build_append_int_noprefix(table_data, dmar_flags, 1); /* Flags */
209791a6b975SIgor Mammedov g_array_append_vals(table_data, rsvd10, sizeof(rsvd10)); /* Reserved */
2098d4eb9119SLe Tan
209991a6b975SIgor Mammedov /* 8.3 DMAR Remapping Hardware Unit Definition structure */
210091a6b975SIgor Mammedov build_append_int_noprefix(table_data, 0, 2); /* Type */
210191a6b975SIgor Mammedov /* Length */
210291a6b975SIgor Mammedov build_append_int_noprefix(table_data,
210391a6b975SIgor Mammedov 16 + ioapic_scope_size + scope_blob->len, 2);
210491a6b975SIgor Mammedov /* Flags */
210591a6b975SIgor Mammedov build_append_int_noprefix(table_data, 0 /* Don't include all pci device */ ,
210691a6b975SIgor Mammedov 1);
210791a6b975SIgor Mammedov build_append_int_noprefix(table_data, 0 , 1); /* Reserved */
210891a6b975SIgor Mammedov build_append_int_noprefix(table_data, 0 , 2); /* Segment Number */
210991a6b975SIgor Mammedov /* Register Base Address */
211091a6b975SIgor Mammedov build_append_int_noprefix(table_data, Q35_HOST_BRIDGE_IOMMU_ADDR , 8);
2111d4eb9119SLe Tan
2112cfc13df4SPeter Xu /* Scope definition for the root-complex IOAPIC. See VT-d spec
2113cfc13df4SPeter Xu * 8.3.1 (version Oct. 2014 or later). */
211491a6b975SIgor Mammedov build_append_int_noprefix(table_data, 0x03 /* IOAPIC */, 1); /* Type */
211591a6b975SIgor Mammedov build_append_int_noprefix(table_data, ioapic_scope_size, 1); /* Length */
211691a6b975SIgor Mammedov build_append_int_noprefix(table_data, 0, 2); /* Reserved */
211791a6b975SIgor Mammedov /* Enumeration ID */
211891a6b975SIgor Mammedov build_append_int_noprefix(table_data, ACPI_BUILD_IOAPIC_ID, 1);
211991a6b975SIgor Mammedov /* Start Bus Number */
212091a6b975SIgor Mammedov build_append_int_noprefix(table_data, Q35_PSEUDO_BUS_PLATFORM, 1);
212191a6b975SIgor Mammedov /* Path, {Device, Function} pair */
212291a6b975SIgor Mammedov build_append_int_noprefix(table_data, PCI_SLOT(Q35_PSEUDO_DEVFN_IOAPIC), 1);
212391a6b975SIgor Mammedov build_append_int_noprefix(table_data, PCI_FUNC(Q35_PSEUDO_DEVFN_IOAPIC), 1);
2124cfc13df4SPeter Xu
212526863366SXingang Wang /* Add scope found above */
212626863366SXingang Wang g_array_append_vals(table_data, scope_blob->data, scope_blob->len);
212726863366SXingang Wang g_array_free(scope_blob, true);
212826863366SXingang Wang
2129bd2baaccSJason Wang if (iommu->dt_supported) {
213091a6b975SIgor Mammedov /* 8.5 Root Port ATS Capability Reporting Structure */
213191a6b975SIgor Mammedov build_append_int_noprefix(table_data, 2, 2); /* Type */
213291a6b975SIgor Mammedov build_append_int_noprefix(table_data, 8, 2); /* Length */
213391a6b975SIgor Mammedov build_append_int_noprefix(table_data, 1 /* ALL_PORTS */, 1); /* Flags */
213491a6b975SIgor Mammedov build_append_int_noprefix(table_data, 0, 1); /* Reserved */
213591a6b975SIgor Mammedov build_append_int_noprefix(table_data, 0, 2); /* Segment Number */
2136bd2baaccSJason Wang }
2137bd2baaccSJason Wang
213891a6b975SIgor Mammedov acpi_table_end(linker, &table);
2139d4eb9119SLe Tan }
214014cda350SLiran Alon
214114cda350SLiran Alon /*
214214cda350SLiran Alon * Windows ACPI Emulated Devices Table
214314cda350SLiran Alon * (Version 1.0 - April 6, 2009)
214414cda350SLiran Alon * Spec: http://download.microsoft.com/download/7/E/7/7E7662CF-CBEA-470B-A97E-CE7CE0D98DC2/WAET.docx
214514cda350SLiran Alon *
214614cda350SLiran Alon * Helpful to speedup Windows guests and ignored by others.
214714cda350SLiran Alon */
214814cda350SLiran Alon static void
build_waet(GArray * table_data,BIOSLinker * linker,const char * oem_id,const char * oem_table_id)2149602b4582SMarian Postevca build_waet(GArray *table_data, BIOSLinker *linker, const char *oem_id,
2150602b4582SMarian Postevca const char *oem_table_id)
215114cda350SLiran Alon {
2152eaa50764SIgor Mammedov AcpiTable table = { .sig = "WAET", .rev = 1, .oem_id = oem_id,
2153eaa50764SIgor Mammedov .oem_table_id = oem_table_id };
215414cda350SLiran Alon
2155eaa50764SIgor Mammedov acpi_table_begin(&table, table_data);
215614cda350SLiran Alon /*
215714cda350SLiran Alon * Set "ACPI PM timer good" flag.
215814cda350SLiran Alon *
215914cda350SLiran Alon * Tells Windows guests that our ACPI PM timer is reliable in the
216014cda350SLiran Alon * sense that guest can read it only once to obtain a reliable value.
216114cda350SLiran Alon * Which avoids costly VMExits caused by guest re-reading it unnecessarily.
216214cda350SLiran Alon */
216314cda350SLiran Alon build_append_int_noprefix(table_data, 1 << 1 /* ACPI PM timer good */, 4);
2164eaa50764SIgor Mammedov acpi_table_end(linker, &table);
216514cda350SLiran Alon }
216614cda350SLiran Alon
2167fb9f5926SDavid Kiarie /*
2168fb9f5926SDavid Kiarie * IVRS table as specified in AMD IOMMU Specification v2.62, Section 5.2
2169fb9f5926SDavid Kiarie * accessible here http://support.amd.com/TechDocs/48882_IOMMU.pdf
2170fb9f5926SDavid Kiarie */
2171c028818dSSingh, Brijesh #define IOAPIC_SB_DEVID (uint64_t)PCI_BUILD_BDF(0, PCI_DEVFN(0x14, 0))
2172c028818dSSingh, Brijesh
2173977aff10SAlex Williamson /*
2174977aff10SAlex Williamson * Insert IVHD entry for device and recurse, insert alias, or insert range as
2175977aff10SAlex Williamson * necessary for the PCI topology.
2176977aff10SAlex Williamson */
2177977aff10SAlex Williamson static void
insert_ivhd(PCIBus * bus,PCIDevice * dev,void * opaque)2178977aff10SAlex Williamson insert_ivhd(PCIBus *bus, PCIDevice *dev, void *opaque)
2179977aff10SAlex Williamson {
2180977aff10SAlex Williamson GArray *table_data = opaque;
2181977aff10SAlex Williamson uint32_t entry;
2182977aff10SAlex Williamson
2183977aff10SAlex Williamson /* "Select" IVHD entry, type 0x2 */
2184977aff10SAlex Williamson entry = PCI_BUILD_BDF(pci_bus_num(bus), dev->devfn) << 8 | 0x2;
2185977aff10SAlex Williamson build_append_int_noprefix(table_data, entry, 4);
2186977aff10SAlex Williamson
2187977aff10SAlex Williamson if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_BRIDGE)) {
2188977aff10SAlex Williamson PCIBus *sec_bus = pci_bridge_get_sec_bus(PCI_BRIDGE(dev));
2189977aff10SAlex Williamson uint8_t sec = pci_bus_num(sec_bus);
2190977aff10SAlex Williamson uint8_t sub = dev->config[PCI_SUBORDINATE_BUS];
2191977aff10SAlex Williamson
2192977aff10SAlex Williamson if (pci_bus_is_express(sec_bus)) {
2193977aff10SAlex Williamson /*
2194977aff10SAlex Williamson * Walk the bus if there are subordinates, otherwise use a range
2195977aff10SAlex Williamson * to cover an entire leaf bus. We could potentially also use a
2196977aff10SAlex Williamson * range for traversed buses, but we'd need to take care not to
2197977aff10SAlex Williamson * create both Select and Range entries covering the same device.
2198977aff10SAlex Williamson * This is easier and potentially more compact.
2199977aff10SAlex Williamson *
2200977aff10SAlex Williamson * An example bare metal system seems to use Select entries for
2201977aff10SAlex Williamson * root ports without a slot (ie. built-ins) and Range entries
2202977aff10SAlex Williamson * when there is a slot. The same system also only hard-codes
2203977aff10SAlex Williamson * the alias range for an onboard PCIe-to-PCI bridge, apparently
2204977aff10SAlex Williamson * making no effort to support nested bridges. We attempt to
2205977aff10SAlex Williamson * be more thorough here.
2206977aff10SAlex Williamson */
2207977aff10SAlex Williamson if (sec == sub) { /* leaf bus */
2208977aff10SAlex Williamson /* "Start of Range" IVHD entry, type 0x3 */
2209977aff10SAlex Williamson entry = PCI_BUILD_BDF(sec, PCI_DEVFN(0, 0)) << 8 | 0x3;
2210977aff10SAlex Williamson build_append_int_noprefix(table_data, entry, 4);
2211977aff10SAlex Williamson /* "End of Range" IVHD entry, type 0x4 */
2212977aff10SAlex Williamson entry = PCI_BUILD_BDF(sub, PCI_DEVFN(31, 7)) << 8 | 0x4;
2213977aff10SAlex Williamson build_append_int_noprefix(table_data, entry, 4);
2214977aff10SAlex Williamson } else {
2215977aff10SAlex Williamson pci_for_each_device(sec_bus, sec, insert_ivhd, table_data);
2216977aff10SAlex Williamson }
2217977aff10SAlex Williamson } else {
2218977aff10SAlex Williamson /*
2219977aff10SAlex Williamson * If the secondary bus is conventional, then we need to create an
2220977aff10SAlex Williamson * Alias range for everything downstream. The range covers the
2221977aff10SAlex Williamson * first devfn on the secondary bus to the last devfn on the
2222977aff10SAlex Williamson * subordinate bus. The alias target depends on legacy versus
2223977aff10SAlex Williamson * express bridges, just as in pci_device_iommu_address_space().
2224977aff10SAlex Williamson * DeviceIDa vs DeviceIDb as per the AMD IOMMU spec.
2225977aff10SAlex Williamson */
2226977aff10SAlex Williamson uint16_t dev_id_a, dev_id_b;
2227977aff10SAlex Williamson
2228977aff10SAlex Williamson dev_id_a = PCI_BUILD_BDF(sec, PCI_DEVFN(0, 0));
2229977aff10SAlex Williamson
2230977aff10SAlex Williamson if (pci_is_express(dev) &&
2231977aff10SAlex Williamson pcie_cap_get_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE) {
2232977aff10SAlex Williamson dev_id_b = dev_id_a;
2233977aff10SAlex Williamson } else {
2234977aff10SAlex Williamson dev_id_b = PCI_BUILD_BDF(pci_bus_num(bus), dev->devfn);
2235977aff10SAlex Williamson }
2236977aff10SAlex Williamson
2237977aff10SAlex Williamson /* "Alias Start of Range" IVHD entry, type 0x43, 8 bytes */
2238977aff10SAlex Williamson build_append_int_noprefix(table_data, dev_id_a << 8 | 0x43, 4);
2239977aff10SAlex Williamson build_append_int_noprefix(table_data, dev_id_b << 8 | 0x0, 4);
2240977aff10SAlex Williamson
2241977aff10SAlex Williamson /* "End of Range" IVHD entry, type 0x4 */
2242977aff10SAlex Williamson entry = PCI_BUILD_BDF(sub, PCI_DEVFN(31, 7)) << 8 | 0x4;
2243977aff10SAlex Williamson build_append_int_noprefix(table_data, entry, 4);
2244977aff10SAlex Williamson }
2245977aff10SAlex Williamson }
2246977aff10SAlex Williamson }
2247977aff10SAlex Williamson
2248977aff10SAlex Williamson /* For all PCI host bridges, walk and insert IVHD entries */
2249977aff10SAlex Williamson static int
ivrs_host_bridges(Object * obj,void * opaque)2250977aff10SAlex Williamson ivrs_host_bridges(Object *obj, void *opaque)
2251977aff10SAlex Williamson {
2252977aff10SAlex Williamson GArray *ivhd_blob = opaque;
2253977aff10SAlex Williamson
2254977aff10SAlex Williamson if (object_dynamic_cast(obj, TYPE_PCI_HOST_BRIDGE)) {
2255977aff10SAlex Williamson PCIBus *bus = PCI_HOST_BRIDGE(obj)->bus;
2256977aff10SAlex Williamson
2257dec2f563SXingang Wang if (bus && !pci_bus_bypass_iommu(bus)) {
22582914fc61SPeter Xu pci_for_each_device_under_bus(bus, insert_ivhd, ivhd_blob);
2259977aff10SAlex Williamson }
2260977aff10SAlex Williamson }
2261977aff10SAlex Williamson
2262977aff10SAlex Williamson return 0;
2263977aff10SAlex Williamson }
2264977aff10SAlex Williamson
2265fb9f5926SDavid Kiarie static void
build_amd_iommu(GArray * table_data,BIOSLinker * linker,const char * oem_id,const char * oem_table_id)2266602b4582SMarian Postevca build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,
2267602b4582SMarian Postevca const char *oem_table_id)
2268fb9f5926SDavid Kiarie {
2269fb9f5926SDavid Kiarie AMDVIState *s = AMD_IOMMU_DEVICE(x86_iommu_get_default());
2270977aff10SAlex Williamson GArray *ivhd_blob = g_array_new(false, true, 1);
2271b0a45ff6SIgor Mammedov AcpiTable table = { .sig = "IVRS", .rev = 1, .oem_id = oem_id,
2272b0a45ff6SIgor Mammedov .oem_table_id = oem_table_id };
2273328a11a0SBui Quang Minh uint64_t feature_report;
2274fb9f5926SDavid Kiarie
2275b0a45ff6SIgor Mammedov acpi_table_begin(&table, table_data);
2276fb9f5926SDavid Kiarie /* IVinfo - IO virtualization information common to all
2277fb9f5926SDavid Kiarie * IOMMU units in a system
2278fb9f5926SDavid Kiarie */
2279328a11a0SBui Quang Minh build_append_int_noprefix(table_data,
2280328a11a0SBui Quang Minh (1UL << 0) | /* EFRSup */
2281328a11a0SBui Quang Minh (40UL << 8), /* PASize */
2282328a11a0SBui Quang Minh 4);
2283fb9f5926SDavid Kiarie /* reserved */
2284fb9f5926SDavid Kiarie build_append_int_noprefix(table_data, 0, 8);
2285fb9f5926SDavid Kiarie
2286c028818dSSingh, Brijesh /*
2287977aff10SAlex Williamson * A PCI bus walk, for each PCI host bridge, is necessary to create a
2288977aff10SAlex Williamson * complete set of IVHD entries. Do this into a separate blob so that we
2289977aff10SAlex Williamson * can calculate the total IVRS table length here and then append the new
2290977aff10SAlex Williamson * blob further below. Fall back to an entry covering all devices, which
2291977aff10SAlex Williamson * is sufficient when no aliases are present.
2292977aff10SAlex Williamson */
2293977aff10SAlex Williamson object_child_foreach_recursive(object_get_root(),
2294977aff10SAlex Williamson ivrs_host_bridges, ivhd_blob);
2295977aff10SAlex Williamson
2296977aff10SAlex Williamson if (!ivhd_blob->len) {
2297977aff10SAlex Williamson /*
2298977aff10SAlex Williamson * Type 1 device entry reporting all devices
2299977aff10SAlex Williamson * These are 4-byte device entries currently reporting the range of
2300977aff10SAlex Williamson * Refer to Spec - Table 95:IVHD Device Entry Type Codes(4-byte)
2301977aff10SAlex Williamson */
2302977aff10SAlex Williamson build_append_int_noprefix(ivhd_blob, 0x0000001, 4);
2303977aff10SAlex Williamson }
2304977aff10SAlex Williamson
2305977aff10SAlex Williamson /*
2306c028818dSSingh, Brijesh * When interrupt remapping is supported, we add a special IVHD device
2307328a11a0SBui Quang Minh * for type IO-APIC
2308328a11a0SBui Quang Minh * Refer to spec - Table 95: IVHD device entry type codes
2309328a11a0SBui Quang Minh *
2310328a11a0SBui Quang Minh * Linux IOMMU driver checks for the special IVHD device (type IO-APIC).
2311328a11a0SBui Quang Minh * See Linux kernel commit 'c2ff5cf5294bcbd7fa50f7d860e90a66db7e5059'
2312c028818dSSingh, Brijesh */
2313a924b3d8SPeter Xu if (x86_iommu_ir_supported(x86_iommu_get_default())) {
2314328a11a0SBui Quang Minh build_append_int_noprefix(ivhd_blob,
2315328a11a0SBui Quang Minh (0x1ull << 56) | /* type IOAPIC */
2316328a11a0SBui Quang Minh (IOAPIC_SB_DEVID << 40) | /* IOAPIC devid */
2317328a11a0SBui Quang Minh 0x48, /* special device */
2318328a11a0SBui Quang Minh 8);
2319c028818dSSingh, Brijesh }
2320977aff10SAlex Williamson
2321328a11a0SBui Quang Minh /* IVHD definition - type 10h */
2322328a11a0SBui Quang Minh build_append_int_noprefix(table_data, 0x10, 1);
2323328a11a0SBui Quang Minh /* virtualization flags */
2324328a11a0SBui Quang Minh build_append_int_noprefix(table_data,
2325328a11a0SBui Quang Minh (1UL << 0) | /* HtTunEn */
2326328a11a0SBui Quang Minh (1UL << 4) | /* iotblSup */
2327328a11a0SBui Quang Minh (1UL << 6) | /* PrefSup */
2328328a11a0SBui Quang Minh (1UL << 7), /* PPRSup */
2329328a11a0SBui Quang Minh 1);
2330328a11a0SBui Quang Minh
2331fb9f5926SDavid Kiarie /* IVHD length */
2332328a11a0SBui Quang Minh build_append_int_noprefix(table_data, ivhd_blob->len + 24, 2);
2333fb9f5926SDavid Kiarie /* DeviceID */
2334531f50abSPhilippe Mathieu-Daudé build_append_int_noprefix(table_data,
2335531f50abSPhilippe Mathieu-Daudé object_property_get_int(OBJECT(&s->pci), "addr",
2336531f50abSPhilippe Mathieu-Daudé &error_abort), 2);
2337fb9f5926SDavid Kiarie /* Capability offset */
2338ae097d8fSPhilippe Mathieu-Daudé build_append_int_noprefix(table_data, s->pci.capab_offset, 2);
2339fb9f5926SDavid Kiarie /* IOMMU base address */
23402e6f051cSSuravee Suthikulpanit build_append_int_noprefix(table_data, s->mr_mmio.addr, 8);
2341fb9f5926SDavid Kiarie /* PCI Segment Group */
2342fb9f5926SDavid Kiarie build_append_int_noprefix(table_data, 0, 2);
2343fb9f5926SDavid Kiarie /* IOMMU info */
2344fb9f5926SDavid Kiarie build_append_int_noprefix(table_data, 0, 2);
2345fb9f5926SDavid Kiarie /* IOMMU Feature Reporting */
2346328a11a0SBui Quang Minh feature_report = (48UL << 30) | /* HATS */
2347fb9f5926SDavid Kiarie (48UL << 28) | /* GATS */
234812499b23SSingh, Brijesh (1UL << 2) | /* GTSup */
2349328a11a0SBui Quang Minh (1UL << 6); /* GASup */
2350328a11a0SBui Quang Minh if (s->xtsup) {
2351328a11a0SBui Quang Minh feature_report |= (1UL << 0); /* XTSup */
2352328a11a0SBui Quang Minh }
2353328a11a0SBui Quang Minh build_append_int_noprefix(table_data, feature_report, 4);
2354977aff10SAlex Williamson
2355977aff10SAlex Williamson /* IVHD entries as found above */
2356977aff10SAlex Williamson g_array_append_vals(table_data, ivhd_blob->data, ivhd_blob->len);
2357fb9f5926SDavid Kiarie
2358328a11a0SBui Quang Minh /* IVHD definition - type 11h */
2359328a11a0SBui Quang Minh build_append_int_noprefix(table_data, 0x11, 1);
2360328a11a0SBui Quang Minh /* virtualization flags */
2361c028818dSSingh, Brijesh build_append_int_noprefix(table_data,
2362328a11a0SBui Quang Minh (1UL << 0) | /* HtTunEn */
2363328a11a0SBui Quang Minh (1UL << 4), /* iotblSup */
2364328a11a0SBui Quang Minh 1);
2365328a11a0SBui Quang Minh
2366328a11a0SBui Quang Minh /* IVHD length */
2367328a11a0SBui Quang Minh build_append_int_noprefix(table_data, ivhd_blob->len + 40, 2);
2368328a11a0SBui Quang Minh /* DeviceID */
2369328a11a0SBui Quang Minh build_append_int_noprefix(table_data,
2370328a11a0SBui Quang Minh object_property_get_int(OBJECT(&s->pci), "addr",
2371328a11a0SBui Quang Minh &error_abort), 2);
2372328a11a0SBui Quang Minh /* Capability offset */
2373328a11a0SBui Quang Minh build_append_int_noprefix(table_data, s->pci.capab_offset, 2);
2374328a11a0SBui Quang Minh /* IOMMU base address */
23752e6f051cSSuravee Suthikulpanit build_append_int_noprefix(table_data, s->mr_mmio.addr, 8);
2376328a11a0SBui Quang Minh /* PCI Segment Group */
2377328a11a0SBui Quang Minh build_append_int_noprefix(table_data, 0, 2);
2378328a11a0SBui Quang Minh /* IOMMU info */
2379328a11a0SBui Quang Minh build_append_int_noprefix(table_data, 0, 2);
2380328a11a0SBui Quang Minh /* IOMMU Attributes */
2381328a11a0SBui Quang Minh build_append_int_noprefix(table_data, 0, 4);
2382328a11a0SBui Quang Minh /* EFR Register Image */
2383328a11a0SBui Quang Minh build_append_int_noprefix(table_data,
2384328a11a0SBui Quang Minh amdvi_extended_feature_register(s),
2385c028818dSSingh, Brijesh 8);
2386328a11a0SBui Quang Minh /* EFR Register Image 2 */
2387328a11a0SBui Quang Minh build_append_int_noprefix(table_data, 0, 8);
2388328a11a0SBui Quang Minh
2389328a11a0SBui Quang Minh /* IVHD entries as found above */
2390328a11a0SBui Quang Minh g_array_append_vals(table_data, ivhd_blob->data, ivhd_blob->len);
2391328a11a0SBui Quang Minh
2392328a11a0SBui Quang Minh g_array_free(ivhd_blob, TRUE);
2393b0a45ff6SIgor Mammedov acpi_table_end(linker, &table);
2394fb9f5926SDavid Kiarie }
2395d4eb9119SLe Tan
239672c194f7SMichael S. Tsirkin typedef
239772c194f7SMichael S. Tsirkin struct AcpiBuildState {
239872c194f7SMichael S. Tsirkin /* Copy of table in RAM (for patching). */
2399339240b5SPaolo Bonzini MemoryRegion *table_mr;
240072c194f7SMichael S. Tsirkin /* Is table patched? */
240172c194f7SMichael S. Tsirkin uint8_t patched;
2402339240b5SPaolo Bonzini MemoryRegion *rsdp_mr;
2403339240b5SPaolo Bonzini MemoryRegion *linker_mr;
240472c194f7SMichael S. Tsirkin } AcpiBuildState;
240572c194f7SMichael S. Tsirkin
acpi_get_mcfg(AcpiMcfgInfo * mcfg)240672c194f7SMichael S. Tsirkin static bool acpi_get_mcfg(AcpiMcfgInfo *mcfg)
240772c194f7SMichael S. Tsirkin {
240872c194f7SMichael S. Tsirkin Object *pci_host;
240972c194f7SMichael S. Tsirkin QObject *o;
241072c194f7SMichael S. Tsirkin
2411ca6c1855SMarcel Apfelbaum pci_host = acpi_get_i386_pci_host();
2412c0e427d6SJulia Suvorova if (!pci_host) {
2413c0e427d6SJulia Suvorova return false;
2414c0e427d6SJulia Suvorova }
241572c194f7SMichael S. Tsirkin
241672c194f7SMichael S. Tsirkin o = object_property_get_qobject(pci_host, PCIE_HOST_MCFG_BASE, NULL);
241772c194f7SMichael S. Tsirkin if (!o) {
241872c194f7SMichael S. Tsirkin return false;
241972c194f7SMichael S. Tsirkin }
2420c309434eSWei Yang mcfg->base = qnum_get_uint(qobject_to(QNum, o));
2421cb3e7f08SMarc-André Lureau qobject_unref(o);
2422c309434eSWei Yang if (mcfg->base == PCIE_BASE_ADDR_UNMAPPED) {
2423fe4970adSIgor Mammedov return false;
2424fe4970adSIgor Mammedov }
242572c194f7SMichael S. Tsirkin
242672c194f7SMichael S. Tsirkin o = object_property_get_qobject(pci_host, PCIE_HOST_MCFG_SIZE, NULL);
242772c194f7SMichael S. Tsirkin assert(o);
2428c309434eSWei Yang mcfg->size = qnum_get_uint(qobject_to(QNum, o));
2429cb3e7f08SMarc-André Lureau qobject_unref(o);
243072c194f7SMichael S. Tsirkin return true;
243172c194f7SMichael S. Tsirkin }
243272c194f7SMichael S. Tsirkin
243372c194f7SMichael S. Tsirkin static
acpi_build(AcpiBuildTables * tables,MachineState * machine)24343d3ebcadSIgor Mammedov void acpi_build(AcpiBuildTables *tables, MachineState *machine)
243572c194f7SMichael S. Tsirkin {
24363d3ebcadSIgor Mammedov PCMachineState *pcms = PC_MACHINE(machine);
2437f0bb276bSPaolo Bonzini X86MachineState *x86ms = X86_MACHINE(machine);
243836efa250SJean-Philippe Brucker DeviceState *iommu = pcms->iommu;
243972c194f7SMichael S. Tsirkin GArray *table_offsets;
2440ea7a74abSPhilippe Mathieu-Daudé unsigned facs, dsdt, rsdt;
244172c194f7SMichael S. Tsirkin AcpiPmInfo pm;
244272c194f7SMichael S. Tsirkin AcpiMiscInfo misc;
244372c194f7SMichael S. Tsirkin AcpiMcfgInfo mcfg;
2444c0e427d6SJulia Suvorova Range pci_hole = {}, pci_hole64 = {};
244572c194f7SMichael S. Tsirkin uint8_t *u;
24467c2c1fa5SIgor Mammedov GArray *tables_blob = tables->table_data;
2447ae123749SLaszlo Ersek AcpiSlicOem slic_oem = { .id = NULL, .table_id = NULL };
2448d03637bcSBen Warren Object *vmgenid_dev;
2449602b4582SMarian Postevca char *oem_id;
2450602b4582SMarian Postevca char *oem_table_id;
245172c194f7SMichael S. Tsirkin
24520e11fc69SLike Xu acpi_get_pm_info(machine, &pm);
245372c194f7SMichael S. Tsirkin acpi_get_misc_info(&misc);
245401c9742dSMarkus Armbruster acpi_get_pci_holes(&pci_hole, &pci_hole64);
2455ae123749SLaszlo Ersek acpi_get_slic_oem(&slic_oem);
245672c194f7SMichael S. Tsirkin
2457602b4582SMarian Postevca if (slic_oem.id) {
2458602b4582SMarian Postevca oem_id = slic_oem.id;
2459602b4582SMarian Postevca } else {
2460d07b2286SMarian Postevca oem_id = x86ms->oem_id;
2461602b4582SMarian Postevca }
2462602b4582SMarian Postevca
2463602b4582SMarian Postevca if (slic_oem.table_id) {
2464602b4582SMarian Postevca oem_table_id = slic_oem.table_id;
2465602b4582SMarian Postevca } else {
2466d07b2286SMarian Postevca oem_table_id = x86ms->oem_table_id;
2467602b4582SMarian Postevca }
2468602b4582SMarian Postevca
246972c194f7SMichael S. Tsirkin table_offsets = g_array_new(false, true /* clear */,
247072c194f7SMichael S. Tsirkin sizeof(uint32_t));
24718b310fc4SGonglei ACPI_BUILD_DPRINTF("init ACPI tables\n");
247272c194f7SMichael S. Tsirkin
2473ad9671b8SIgor Mammedov bios_linker_loader_alloc(tables->linker,
2474ad9671b8SIgor Mammedov ACPI_BUILD_TABLE_FILE, tables_blob,
247572c194f7SMichael S. Tsirkin 64 /* Ensure FACS is aligned */,
247672c194f7SMichael S. Tsirkin false /* high memory */);
247772c194f7SMichael S. Tsirkin
247872c194f7SMichael S. Tsirkin /*
247972c194f7SMichael S. Tsirkin * FACS is pointed to by FADT.
248072c194f7SMichael S. Tsirkin * We place it first since it's the only table that has alignment
248172c194f7SMichael S. Tsirkin * requirements.
248272c194f7SMichael S. Tsirkin */
24837c2c1fa5SIgor Mammedov facs = tables_blob->len;
2484009180bdSWei Yang build_facs(tables_blob);
248572c194f7SMichael S. Tsirkin
248672c194f7SMichael S. Tsirkin /* DSDT is pointed to by FADT */
24877c2c1fa5SIgor Mammedov dsdt = tables_blob->len;
248801c9742dSMarkus Armbruster build_dsdt(tables_blob, tables->linker, &pm, &misc,
248901c9742dSMarkus Armbruster &pci_hole, &pci_hole64, machine);
249072c194f7SMichael S. Tsirkin
249172c194f7SMichael S. Tsirkin /* ACPI tables pointed to by RSDT */
24927c2c1fa5SIgor Mammedov acpi_add_table(table_offsets, tables_blob);
2493937d1b58SIgor Mammedov pm.fadt.facs_tbl_offset = &facs;
2494937d1b58SIgor Mammedov pm.fadt.dsdt_tbl_offset = &dsdt;
2495937d1b58SIgor Mammedov pm.fadt.xdsdt_tbl_offset = &dsdt;
2496602b4582SMarian Postevca build_fadt(tables_blob, tables->linker, &pm.fadt, oem_id, oem_table_id);
249772c194f7SMichael S. Tsirkin
24987c2c1fa5SIgor Mammedov acpi_add_table(table_offsets, tables_blob);
2499eb66ffabSGerd Hoffmann acpi_build_madt(tables_blob, tables->linker, x86ms,
2500f4a06e59SBernhard Beschow x86ms->oem_id, x86ms->oem_table_id);
25019ac1c4c0SMichael S. Tsirkin
25028486f12fSEric DeVolder #ifdef CONFIG_ACPI_ERST
25038486f12fSEric DeVolder {
25048486f12fSEric DeVolder Object *erst_dev;
25058486f12fSEric DeVolder erst_dev = find_erst_dev();
25068486f12fSEric DeVolder if (erst_dev) {
25078486f12fSEric DeVolder acpi_add_table(table_offsets, tables_blob);
25088486f12fSEric DeVolder build_erst(tables_blob, tables->linker, erst_dev,
25098486f12fSEric DeVolder x86ms->oem_id, x86ms->oem_table_id);
25108486f12fSEric DeVolder }
25118486f12fSEric DeVolder }
25128486f12fSEric DeVolder #endif
25138486f12fSEric DeVolder
2514d03637bcSBen Warren vmgenid_dev = find_vmgenid_dev();
2515d03637bcSBen Warren if (vmgenid_dev) {
2516d03637bcSBen Warren acpi_add_table(table_offsets, tables_blob);
2517d03637bcSBen Warren vmgenid_build_acpi(VMGENID(vmgenid_dev), tables_blob,
2518d07b2286SMarian Postevca tables->vmgenid, tables->linker, x86ms->oem_id);
2519d03637bcSBen Warren }
2520d03637bcSBen Warren
252172c194f7SMichael S. Tsirkin if (misc.has_hpet) {
25227c2c1fa5SIgor Mammedov acpi_add_table(table_offsets, tables_blob);
2523d07b2286SMarian Postevca build_hpet(tables_blob, tables->linker, x86ms->oem_id,
2524d07b2286SMarian Postevca x86ms->oem_table_id);
252572c194f7SMichael S. Tsirkin }
252611fb99e6SStefan Berger #ifdef CONFIG_TPM
25275cb18b3dSStefan Berger if (misc.tpm_version != TPM_VERSION_UNSPEC) {
25287e7c1b84SStefan Berger if (misc.tpm_version == TPM_VERSION_1_2) {
25297c2c1fa5SIgor Mammedov acpi_add_table(table_offsets, tables_blob);
2530602b4582SMarian Postevca build_tpm_tcpa(tables_blob, tables->linker, tables->tcpalog,
2531d07b2286SMarian Postevca x86ms->oem_id, x86ms->oem_table_id);
25327e7c1b84SStefan Berger } else { /* TPM_VERSION_2_0 */
25337c2c1fa5SIgor Mammedov acpi_add_table(table_offsets, tables_blob);
2534602b4582SMarian Postevca build_tpm2(tables_blob, tables->linker, tables->tcpalog,
2535d07b2286SMarian Postevca x86ms->oem_id, x86ms->oem_table_id);
25365cb18b3dSStefan Berger }
2537711b20b4SStefan Berger }
253811fb99e6SStefan Berger #endif
2539e77af21aSJingqi Liu if (machine->numa_state->num_nodes) {
25407c2c1fa5SIgor Mammedov acpi_add_table(table_offsets, tables_blob);
25413d3ebcadSIgor Mammedov build_srat(tables_blob, tables->linker, machine);
2542118154b7STao Xu if (machine->numa_state->have_numa_distance) {
25430f203430SHe Chen acpi_add_table(table_offsets, tables_blob);
2544d07b2286SMarian Postevca build_slit(tables_blob, tables->linker, machine, x86ms->oem_id,
2545d07b2286SMarian Postevca x86ms->oem_table_id);
25460f203430SHe Chen }
2547e6f123c3SLiu Jingqi if (machine->numa_state->hmat_enabled) {
2548e6f123c3SLiu Jingqi acpi_add_table(table_offsets, tables_blob);
2549602b4582SMarian Postevca build_hmat(tables_blob, tables->linker, machine->numa_state,
2550d07b2286SMarian Postevca x86ms->oem_id, x86ms->oem_table_id);
2551e6f123c3SLiu Jingqi }
255272c194f7SMichael S. Tsirkin }
255372c194f7SMichael S. Tsirkin if (acpi_get_mcfg(&mcfg)) {
25547c2c1fa5SIgor Mammedov acpi_add_table(table_offsets, tables_blob);
2555d07b2286SMarian Postevca build_mcfg(tables_blob, tables->linker, &mcfg, x86ms->oem_id,
2556d07b2286SMarian Postevca x86ms->oem_table_id);
255772c194f7SMichael S. Tsirkin }
2558867e9c9fSJean-Philippe Brucker if (object_dynamic_cast(OBJECT(iommu), TYPE_AMD_IOMMU_DEVICE)) {
2559fb9f5926SDavid Kiarie acpi_add_table(table_offsets, tables_blob);
2560d07b2286SMarian Postevca build_amd_iommu(tables_blob, tables->linker, x86ms->oem_id,
2561d07b2286SMarian Postevca x86ms->oem_table_id);
2562867e9c9fSJean-Philippe Brucker } else if (object_dynamic_cast(OBJECT(iommu), TYPE_INTEL_IOMMU_DEVICE)) {
25637c2c1fa5SIgor Mammedov acpi_add_table(table_offsets, tables_blob);
2564d07b2286SMarian Postevca build_dmar_q35(tables_blob, tables->linker, x86ms->oem_id,
2565d07b2286SMarian Postevca x86ms->oem_table_id);
256636efa250SJean-Philippe Brucker } else if (object_dynamic_cast(OBJECT(iommu), TYPE_VIRTIO_IOMMU_PCI)) {
256736efa250SJean-Philippe Brucker PCIDevice *pdev = PCI_DEVICE(iommu);
256836efa250SJean-Philippe Brucker
256936efa250SJean-Philippe Brucker acpi_add_table(table_offsets, tables_blob);
257036efa250SJean-Philippe Brucker build_viot(machine, tables_blob, tables->linker, pci_get_bdf(pdev),
257136efa250SJean-Philippe Brucker x86ms->oem_id, x86ms->oem_table_id);
2572d4eb9119SLe Tan }
2573f6a0d06bSEric Auger if (machine->nvdimms_state->is_enabled) {
2574ad9671b8SIgor Mammedov nvdimm_build_acpi(table_offsets, tables_blob, tables->linker,
2575602b4582SMarian Postevca machine->nvdimms_state, machine->ram_slots,
2576d07b2286SMarian Postevca x86ms->oem_id, x86ms->oem_table_id);
257787252e1bSXiao Guangrong }
25781ebf9001SJonathan Cameron if (pcms->cxl_devices_state.is_enabled) {
257951359805SJonathan Cameron cxl_build_cedt(table_offsets, tables_blob, tables->linker,
25801ebf9001SJonathan Cameron x86ms->oem_id, x86ms->oem_table_id, &pcms->cxl_devices_state);
25813d6a69b6SBen Widawsky }
258287252e1bSXiao Guangrong
258314cda350SLiran Alon acpi_add_table(table_offsets, tables_blob);
2584d07b2286SMarian Postevca build_waet(tables_blob, tables->linker, x86ms->oem_id, x86ms->oem_table_id);
258514cda350SLiran Alon
258672c194f7SMichael S. Tsirkin /* Add tables supplied by user (if any) */
258772c194f7SMichael S. Tsirkin for (u = acpi_table_first(); u; u = acpi_table_next(u)) {
258872c194f7SMichael S. Tsirkin unsigned len = acpi_table_len(u);
258972c194f7SMichael S. Tsirkin
25907c2c1fa5SIgor Mammedov acpi_add_table(table_offsets, tables_blob);
25917c2c1fa5SIgor Mammedov g_array_append_vals(tables_blob, u, len);
259272c194f7SMichael S. Tsirkin }
259372c194f7SMichael S. Tsirkin
259472c194f7SMichael S. Tsirkin /* RSDT is pointed to by RSDP */
25957c2c1fa5SIgor Mammedov rsdt = tables_blob->len;
2596ae123749SLaszlo Ersek build_rsdt(tables_blob, tables->linker, table_offsets,
2597602b4582SMarian Postevca oem_id, oem_table_id);
259872c194f7SMichael S. Tsirkin
259972c194f7SMichael S. Tsirkin /* RSDP is in FSEG memory, so allocate it separately */
2600a46ce1c2SSamuel Ortiz {
2601a46ce1c2SSamuel Ortiz AcpiRsdpData rsdp_data = {
2602a46ce1c2SSamuel Ortiz .revision = 0,
2603d07b2286SMarian Postevca .oem_id = x86ms->oem_id,
2604a46ce1c2SSamuel Ortiz .xsdt_tbl_offset = NULL,
2605a46ce1c2SSamuel Ortiz .rsdt_tbl_offset = &rsdt,
2606a46ce1c2SSamuel Ortiz };
2607a46ce1c2SSamuel Ortiz build_rsdp(tables->rsdp, tables->linker, &rsdp_data);
2608a46ce1c2SSamuel Ortiz }
260972c194f7SMichael S. Tsirkin
261007fb6176SPaolo Bonzini /* We'll expose it all to Guest so we want to reduce
261172c194f7SMichael S. Tsirkin * chance of size changes.
261207fb6176SPaolo Bonzini *
261307fb6176SPaolo Bonzini * We used to align the tables to 4k, but of course this would
261407fb6176SPaolo Bonzini * too simple to be enough. 4k turned out to be too small an
261507fb6176SPaolo Bonzini * alignment very soon, and in fact it is almost impossible to
261607fb6176SPaolo Bonzini * keep the table size stable for all (max_cpus, max_memory_slots)
2617ea7a74abSPhilippe Mathieu-Daudé * combinations.
261872c194f7SMichael S. Tsirkin */
26197c2c1fa5SIgor Mammedov acpi_align_size(tables_blob, ACPI_BUILD_TABLE_SIZE);
262072c194f7SMichael S. Tsirkin
26210e9b9edaSIgor Mammedov acpi_align_size(tables->linker->cmd_blob, ACPI_BUILD_ALIGN_SIZE);
262272c194f7SMichael S. Tsirkin
262372c194f7SMichael S. Tsirkin /* Cleanup memory that's no longer used. */
262472c194f7SMichael S. Tsirkin g_array_free(table_offsets, true);
26258cdb99afSIgor Mammedov g_free(slic_oem.id);
26268cdb99afSIgor Mammedov g_free(slic_oem.table_id);
262772c194f7SMichael S. Tsirkin }
262872c194f7SMichael S. Tsirkin
acpi_ram_update(MemoryRegion * mr,GArray * data)2629339240b5SPaolo Bonzini static void acpi_ram_update(MemoryRegion *mr, GArray *data)
263042d85900SMichael S. Tsirkin {
263142d85900SMichael S. Tsirkin uint32_t size = acpi_data_len(data);
263242d85900SMichael S. Tsirkin
263342d85900SMichael S. Tsirkin /* Make sure RAM size is correct - in case it got changed e.g. by migration */
2634339240b5SPaolo Bonzini memory_region_ram_resize(mr, size, &error_abort);
263542d85900SMichael S. Tsirkin
2636339240b5SPaolo Bonzini memcpy(memory_region_get_ram_ptr(mr), data->data, size);
2637339240b5SPaolo Bonzini memory_region_set_dirty(mr, 0, size);
263842d85900SMichael S. Tsirkin }
263942d85900SMichael S. Tsirkin
acpi_build_update(void * build_opaque)26403f8752b4SGabriel L. Somlo static void acpi_build_update(void *build_opaque)
264172c194f7SMichael S. Tsirkin {
264272c194f7SMichael S. Tsirkin AcpiBuildState *build_state = build_opaque;
264372c194f7SMichael S. Tsirkin AcpiBuildTables tables;
264472c194f7SMichael S. Tsirkin
264572c194f7SMichael S. Tsirkin /* No state to update or already patched? Nothing to do. */
264672c194f7SMichael S. Tsirkin if (!build_state || build_state->patched) {
264772c194f7SMichael S. Tsirkin return;
264872c194f7SMichael S. Tsirkin }
264972c194f7SMichael S. Tsirkin build_state->patched = 1;
265072c194f7SMichael S. Tsirkin
265172c194f7SMichael S. Tsirkin acpi_build_tables_init(&tables);
265272c194f7SMichael S. Tsirkin
26533d3ebcadSIgor Mammedov acpi_build(&tables, MACHINE(qdev_get_machine()));
265472c194f7SMichael S. Tsirkin
2655339240b5SPaolo Bonzini acpi_ram_update(build_state->table_mr, tables.table_data);
2656a1666142SMichael S. Tsirkin
2657339240b5SPaolo Bonzini acpi_ram_update(build_state->rsdp_mr, tables.rsdp);
2658ad5b88b1SMichael S. Tsirkin
26590e9b9edaSIgor Mammedov acpi_ram_update(build_state->linker_mr, tables.linker->cmd_blob);
266072c194f7SMichael S. Tsirkin acpi_build_tables_cleanup(&tables, true);
266172c194f7SMichael S. Tsirkin }
266272c194f7SMichael S. Tsirkin
acpi_build_reset(void * build_opaque)266372c194f7SMichael S. Tsirkin static void acpi_build_reset(void *build_opaque)
266472c194f7SMichael S. Tsirkin {
266572c194f7SMichael S. Tsirkin AcpiBuildState *build_state = build_opaque;
266672c194f7SMichael S. Tsirkin build_state->patched = 0;
266772c194f7SMichael S. Tsirkin }
266872c194f7SMichael S. Tsirkin
266972c194f7SMichael S. Tsirkin static const VMStateDescription vmstate_acpi_build = {
267072c194f7SMichael S. Tsirkin .name = "acpi_build",
267172c194f7SMichael S. Tsirkin .version_id = 1,
267272c194f7SMichael S. Tsirkin .minimum_version_id = 1,
26739231a017SRichard Henderson .fields = (const VMStateField[]) {
267472c194f7SMichael S. Tsirkin VMSTATE_UINT8(patched, AcpiBuildState),
267572c194f7SMichael S. Tsirkin VMSTATE_END_OF_LIST()
267672c194f7SMichael S. Tsirkin },
267772c194f7SMichael S. Tsirkin };
267872c194f7SMichael S. Tsirkin
acpi_setup(void)2679fb306ffeSEduardo Habkost void acpi_setup(void)
268072c194f7SMichael S. Tsirkin {
2681fb306ffeSEduardo Habkost PCMachineState *pcms = PC_MACHINE(qdev_get_machine());
2682f0bb276bSPaolo Bonzini X86MachineState *x86ms = X86_MACHINE(pcms);
268372c194f7SMichael S. Tsirkin AcpiBuildTables tables;
268472c194f7SMichael S. Tsirkin AcpiBuildState *build_state;
2685d03637bcSBen Warren Object *vmgenid_dev;
268611fb99e6SStefan Berger #ifdef CONFIG_TPM
26870fe24669SStefan Berger TPMIf *tpm;
26880fe24669SStefan Berger static FwCfgTPMConfig tpm_config;
268911fb99e6SStefan Berger #endif
269072c194f7SMichael S. Tsirkin
2691f0bb276bSPaolo Bonzini if (!x86ms->fw_cfg) {
26928b310fc4SGonglei ACPI_BUILD_DPRINTF("No fw cfg. Bailing out.\n");
269372c194f7SMichael S. Tsirkin return;
269472c194f7SMichael S. Tsirkin }
269572c194f7SMichael S. Tsirkin
2696021746c1SWei Liu if (!pcms->acpi_build_enabled) {
26978b310fc4SGonglei ACPI_BUILD_DPRINTF("ACPI build disabled. Bailing out.\n");
269872c194f7SMichael S. Tsirkin return;
269972c194f7SMichael S. Tsirkin }
270072c194f7SMichael S. Tsirkin
270117e89077SGerd Hoffmann if (!x86_machine_is_acpi_enabled(X86_MACHINE(pcms))) {
27028b310fc4SGonglei ACPI_BUILD_DPRINTF("ACPI disabled. Bailing out.\n");
270381adc513SMichael S. Tsirkin return;
270481adc513SMichael S. Tsirkin }
270581adc513SMichael S. Tsirkin
270672c194f7SMichael S. Tsirkin build_state = g_malloc0(sizeof *build_state);
270772c194f7SMichael S. Tsirkin
270872c194f7SMichael S. Tsirkin acpi_build_tables_init(&tables);
27093d3ebcadSIgor Mammedov acpi_build(&tables, MACHINE(pcms));
271072c194f7SMichael S. Tsirkin
271172c194f7SMichael S. Tsirkin /* Now expose it all to Guest */
271282f76c67SWei Yang build_state->table_mr = acpi_add_rom_blob(acpi_build_update,
271382f76c67SWei Yang build_state, tables.table_data,
27146930ba0dSDavid Hildenbrand ACPI_BUILD_TABLE_FILE);
2715339240b5SPaolo Bonzini assert(build_state->table_mr != NULL);
271672c194f7SMichael S. Tsirkin
2717339240b5SPaolo Bonzini build_state->linker_mr =
271882f76c67SWei Yang acpi_add_rom_blob(acpi_build_update, build_state,
27196930ba0dSDavid Hildenbrand tables.linker->cmd_blob, ACPI_BUILD_LOADER_FILE);
272072c194f7SMichael S. Tsirkin
272111fb99e6SStefan Berger #ifdef CONFIG_TPM
2722f0bb276bSPaolo Bonzini fw_cfg_add_file(x86ms->fw_cfg, ACPI_BUILD_TPMLOG_FILE,
272342a5b308SStefan Berger tables.tcpalog->data, acpi_data_len(tables.tcpalog));
272442a5b308SStefan Berger
27250fe24669SStefan Berger tpm = tpm_find();
27260fe24669SStefan Berger if (tpm && object_property_get_bool(OBJECT(tpm), "ppi", &error_abort)) {
27270fe24669SStefan Berger tpm_config = (FwCfgTPMConfig) {
27280fe24669SStefan Berger .tpmppi_address = cpu_to_le32(TPM_PPI_ADDR_BASE),
27290fe24669SStefan Berger .tpm_version = tpm_get_version(tpm),
2730ac6dd31eSStefan Berger .tpmppi_version = TPM_PPI_VERSION_1_30
27310fe24669SStefan Berger };
2732f0bb276bSPaolo Bonzini fw_cfg_add_file(x86ms->fw_cfg, "etc/tpm/config",
27330fe24669SStefan Berger &tpm_config, sizeof tpm_config);
27340fe24669SStefan Berger }
273511fb99e6SStefan Berger #endif
27360fe24669SStefan Berger
2737d03637bcSBen Warren vmgenid_dev = find_vmgenid_dev();
2738d03637bcSBen Warren if (vmgenid_dev) {
2739f0bb276bSPaolo Bonzini vmgenid_add_fw_cfg(VMGENID(vmgenid_dev), x86ms->fw_cfg,
2740d03637bcSBen Warren tables.vmgenid);
2741d03637bcSBen Warren }
2742d03637bcSBen Warren
274382f76c67SWei Yang build_state->rsdp_mr = acpi_add_rom_blob(acpi_build_update,
274482f76c67SWei Yang build_state, tables.rsdp,
27456930ba0dSDavid Hildenbrand ACPI_BUILD_RSDP_FILE);
2746d70414a5SMichael S. Tsirkin
274772c194f7SMichael S. Tsirkin qemu_register_reset(acpi_build_reset, build_state);
274872c194f7SMichael S. Tsirkin acpi_build_reset(build_state);
274972c194f7SMichael S. Tsirkin vmstate_register(NULL, 0, &vmstate_acpi_build, build_state);
275072c194f7SMichael S. Tsirkin
275172c194f7SMichael S. Tsirkin /* Cleanup tables but don't free the memory: we track it
275272c194f7SMichael S. Tsirkin * in build_state.
275372c194f7SMichael S. Tsirkin */
275472c194f7SMichael S. Tsirkin acpi_build_tables_cleanup(&tables, false);
275572c194f7SMichael S. Tsirkin }
2756