blob: 15242b9096b5fc1434a514a79a6855056329c3ba [file] [log] [blame]
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001/* Support for generating ACPI tables and passing them to Guests
2 *
3 * Copyright (C) 2008-2010 Kevin O'Connor <kevin@koconnor.net>
4 * Copyright (C) 2006 Fabrice Bellard
5 * Copyright (C) 2013 Red Hat Inc
6 *
7 * Author: Michael S. Tsirkin <mst@redhat.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, see <http://www.gnu.org/licenses/>.
21 */
22
Peter Maydellb6a0aa02016-01-26 18:17:03 +000023#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010024#include "qapi/error.h"
Markus Armbruster15280c32018-02-01 12:18:36 +010025#include "qapi/qmp/qnum.h"
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +030026#include "acpi-build.h"
Gerd Hoffmanneb66ffa2020-05-20 15:19:47 +020027#include "acpi-common.h"
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +030028#include "qemu/bitmap.h"
Paolo Bonzini07fb6172014-07-28 17:34:15 +020029#include "qemu/error-report.h"
Markus Armbruster674b0a52022-12-22 11:03:24 +010030#include "hw/pci/pci_bridge.h"
Ben Widawsky6e4e3ae2022-04-29 15:40:48 +010031#include "hw/cxl/cxl.h"
Markus Armbruster2e5b09f2019-07-09 17:20:52 +020032#include "hw/core/cpu.h"
Thomas Huthfcf5ef22016-10-11 08:56:52 +020033#include "target/i386/cpu.h"
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +030034#include "hw/timer/hpet.h"
Shannon Zhao395e5fb2015-04-03 18:03:33 +080035#include "hw/acpi/acpi-defs.h"
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +030036#include "hw/acpi/acpi.h"
Igor Mammedov679dd1a2016-06-15 11:25:23 +020037#include "hw/acpi/cpu.h"
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +030038#include "hw/nvram/fw_cfg.h"
Michael S. Tsirkin0058ae12015-01-19 23:58:55 +020039#include "hw/acpi/bios-linker-loader.h"
Igor Mammedov5876d9b2022-06-08 09:53:21 -040040#include "hw/acpi/acpi_aml_interface.h"
Liav Albani5334bf52022-03-04 21:10:30 +053041#include "hw/input/i8042.h"
Igor Mammedovbef34922014-06-02 15:25:26 +020042#include "hw/acpi/memory_hotplug.h"
Stefan Berger711b20b2014-08-11 16:33:36 -040043#include "sysemu/tpm.h"
44#include "hw/acpi/tpm.h"
Ben Warrend03637b2017-02-16 15:15:36 -080045#include "hw/acpi/vmgenid.h"
Eric DeVolder8486f122022-01-28 15:38:06 -050046#include "hw/acpi/erst.h"
Mark Cave-Ayland2bfd0842022-05-28 10:02:11 +010047#include "hw/acpi/piix4.h"
Stefan Berger5cb18b32015-05-26 16:51:07 -040048#include "sysemu/tpm_backend.h"
Philippe Mathieu-Daudébcdb9062019-10-04 01:03:53 +020049#include "hw/rtc/mc146818rtc_regs.h"
Markus Armbrusterd6454272019-08-12 07:23:45 +020050#include "migration/vmstate.h"
David Hildenbrand2cc0e2e2018-04-23 18:51:16 +020051#include "hw/mem/memory-device.h"
Philippe Mathieu-Daudé4b997692020-02-28 12:46:47 +010052#include "hw/mem/nvdimm.h"
Igor Mammedov1f3aba32016-06-16 14:23:48 +020053#include "sysemu/numa.h"
Markus Armbruster71e8a912019-08-12 07:23:38 +020054#include "sysemu/reset.h"
Jon Doron6775d152020-04-24 15:34:43 +030055#include "hw/hyperv/vmbus-bridge.h"
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +030056
57/* Supported chipsets: */
Bernhard Beschow1a6981b2023-02-13 18:30:33 +010058#include "hw/southbridge/ich9.h"
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +030059#include "hw/acpi/pcihp.h"
Paolo Bonzini89a289c2019-12-12 14:14:40 +010060#include "hw/i386/fw_cfg.h"
Bernhard Beschow71671812023-02-13 18:30:32 +010061#include "hw/i386/pc.h"
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +030062#include "hw/pci/pci_bus.h"
Bernhard Beschowb496a172022-10-28 12:34:19 +020063#include "hw/pci-host/i440fx.h"
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +030064#include "hw/pci-host/q35.h"
Peter Xu1cf5fd52016-07-14 13:56:12 +080065#include "hw/i386/x86-iommu.h"
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +030066
Igor Mammedov19934e02015-01-30 13:29:36 +000067#include "hw/acpi/aml-build.h"
Wei Yang82f76c62019-06-10 09:18:30 +080068#include "hw/acpi/utils.h"
Wei Yang48cefd92019-04-19 08:30:51 +080069#include "hw/acpi/pci.h"
Ben Widawsky2a3282c2022-04-29 15:40:49 +010070#include "hw/acpi/cxl.h"
Igor Mammedov19934e02015-01-30 13:29:36 +000071
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +030072#include "qom/qom-qobject.h"
David Kiariefb9f5922016-09-20 18:42:34 +030073#include "hw/i386/amd_iommu.h"
74#include "hw/i386/intel_iommu.h"
Jean-Philippe Brucker36efa252021-10-26 19:20:23 +010075#include "hw/virtio/virtio-iommu.h"
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +030076
Liu Jingqie6f123c2019-12-13 09:19:25 +080077#include "hw/acpi/hmat.h"
Jean-Philippe Brucker36efa252021-10-26 19:20:23 +010078#include "hw/acpi/viot.h"
Corey Minyard86e91dd2016-06-10 04:15:42 -050079
Eric DeVolder8486f122022-01-28 15:38:06 -050080#include CONFIG_DEVICES
81
Paolo Bonzini07fb6172014-07-28 17:34:15 +020082/* These are used to size the ACPI tables for -M pc-i440fx-1.7 and
83 * -M pc-i440fx-2.0. Even if the actual amount of AML generated grows
84 * a little bit, there should be plenty of free space since the DSDT
85 * shrunk by ~1.5k between QEMU 2.0 and QEMU 2.1.
86 */
87#define ACPI_BUILD_LEGACY_CPU_AML_SIZE 97
88#define ACPI_BUILD_ALIGN_SIZE 0x1000
89
Michael S. Tsirkin868270f2014-07-28 23:07:11 +020090#define ACPI_BUILD_TABLE_SIZE 0x20000
Paolo Bonzini18045fb2014-07-28 17:34:16 +020091
Gonglei8b310fc2014-11-13 10:59:37 +080092/* #define DEBUG_ACPI_BUILD */
93#ifdef DEBUG_ACPI_BUILD
94#define ACPI_BUILD_DPRINTF(fmt, ...) \
95 do {printf("ACPI_BUILD: " fmt, ## __VA_ARGS__); } while (0)
96#else
97#define ACPI_BUILD_DPRINTF(fmt, ...)
98#endif
99
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300100typedef struct AcpiPmInfo {
101 bool s3_disabled;
102 bool s4_disabled;
Igor Mammedov133a2da2014-07-28 17:34:18 +0200103 bool pcihp_bridge_en;
Igor Mammedov6d837f12020-09-23 05:46:46 -0400104 bool smi_on_cpuhp;
Igor Mammedov892aae72020-12-07 09:07:34 -0500105 bool smi_on_cpu_unplug;
Ani Sinhadf4008c2020-09-18 14:11:08 +0530106 bool pcihp_root_en;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300107 uint8_t s4_val;
Igor Mammedov937d1b52018-02-28 15:23:51 +0100108 AcpiFadtData fadt;
Igor Mammedovddf1ec22015-02-18 19:14:44 +0000109 uint16_t cpu_hp_io_base;
Igor Mammedov500b11e2015-02-18 19:14:50 +0000110 uint16_t pcihp_io_base;
111 uint16_t pcihp_io_len;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300112} AcpiPmInfo;
113
114typedef struct AcpiMiscInfo {
115 bool has_hpet;
Stefan Berger11fb99e2021-06-15 16:21:17 +0200116#ifdef CONFIG_TPM
Stefan Berger5cb18b32015-05-26 16:51:07 -0400117 TPMVersion tpm_version;
Stefan Berger11fb99e2021-06-15 16:21:17 +0200118#endif
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300119} AcpiMiscInfo;
120
Stefan Berger0fe24662019-01-15 02:27:51 +0400121typedef struct FwCfgTPMConfig {
122 uint32_t tpmppi_address;
123 uint8_t tpm_version;
124 uint8_t tpmppi_version;
125} QEMU_PACKED FwCfgTPMConfig;
126
Gerd Hoffmann4a441832019-06-07 09:34:29 +0200127static bool acpi_get_mcfg(AcpiMcfgInfo *mcfg);
128
Kwangwoo Lee5c94b822020-04-21 13:59:29 +0100129const struct AcpiGenericAddress x86_nvdimm_acpi_dsmio = {
130 .space_id = AML_AS_SYSTEM_IO,
131 .address = NVDIMM_ACPI_IO_BASE,
132 .bit_width = NVDIMM_ACPI_IO_LEN << 3
133};
134
Like Xu0e11fc62019-05-19 04:54:25 +0800135static void init_common_fadt_data(MachineState *ms, Object *o,
136 AcpiFadtData *data)
Igor Mammedov937d1b52018-02-28 15:23:51 +0100137{
Isaku Yamahata33b44fd2021-02-17 21:51:13 -0800138 X86MachineState *x86ms = X86_MACHINE(ms);
139 /*
140 * "ICH9-LPC" or "PIIX4_PM" has "smm-compat" property to keep the old
141 * behavior for compatibility irrelevant to smm_enabled, which doesn't
142 * comforms to ACPI spec.
143 */
144 bool smm_enabled = object_property_get_bool(o, "smm-compat", NULL) ?
145 true : x86_machine_is_smm_enabled(x86ms);
Igor Mammedov937d1b52018-02-28 15:23:51 +0100146 uint32_t io = object_property_get_uint(o, ACPI_PM_PROP_PM_IO_BASE, NULL);
147 AmlAddressSpace as = AML_AS_SYSTEM_IO;
148 AcpiFadtData fadt = {
149 .rev = 3,
150 .flags =
151 (1 << ACPI_FADT_F_WBINVD) |
152 (1 << ACPI_FADT_F_PROC_C1) |
153 (1 << ACPI_FADT_F_SLP_BUTTON) |
154 (1 << ACPI_FADT_F_RTC_S4) |
155 (1 << ACPI_FADT_F_USE_PLATFORM_CLOCK) |
156 /* APIC destination mode ("Flat Logical") has an upper limit of 8
157 * CPUs for more than 8 CPUs, "Clustered Logical" mode has to be
158 * used
159 */
Like Xu0e11fc62019-05-19 04:54:25 +0800160 ((ms->smp.max_cpus > 8) ?
161 (1 << ACPI_FADT_F_FORCE_APIC_CLUSTER_MODEL) : 0),
Igor Mammedov937d1b52018-02-28 15:23:51 +0100162 .int_model = 1 /* Multiple APIC */,
163 .rtc_century = RTC_CENTURY,
164 .plvl2_lat = 0xfff /* C2 state not supported */,
165 .plvl3_lat = 0xfff /* C3 state not supported */,
Isaku Yamahata33b44fd2021-02-17 21:51:13 -0800166 .smi_cmd = smm_enabled ? ACPI_PORT_SMI_CMD : 0,
Igor Mammedov937d1b52018-02-28 15:23:51 +0100167 .sci_int = object_property_get_uint(o, ACPI_PM_PROP_SCI_INT, NULL),
168 .acpi_enable_cmd =
Isaku Yamahata33b44fd2021-02-17 21:51:13 -0800169 smm_enabled ?
170 object_property_get_uint(o, ACPI_PM_PROP_ACPI_ENABLE_CMD, NULL) :
171 0,
Igor Mammedov937d1b52018-02-28 15:23:51 +0100172 .acpi_disable_cmd =
Isaku Yamahata33b44fd2021-02-17 21:51:13 -0800173 smm_enabled ?
174 object_property_get_uint(o, ACPI_PM_PROP_ACPI_DISABLE_CMD, NULL) :
175 0,
Igor Mammedov937d1b52018-02-28 15:23:51 +0100176 .pm1a_evt = { .space_id = as, .bit_width = 4 * 8, .address = io },
177 .pm1a_cnt = { .space_id = as, .bit_width = 2 * 8,
178 .address = io + 0x04 },
179 .pm_tmr = { .space_id = as, .bit_width = 4 * 8, .address = io + 0x08 },
180 .gpe0_blk = { .space_id = as, .bit_width =
181 object_property_get_uint(o, ACPI_PM_PROP_GPE0_BLK_LEN, NULL) * 8,
182 .address = object_property_get_uint(o, ACPI_PM_PROP_GPE0_BLK, NULL)
183 },
184 };
Liav Albani5334bf52022-03-04 21:10:30 +0530185
186 /*
187 * ACPI v2, Table 5-10 - Fixed ACPI Description Table Boot Architecture
188 * Flags, bit offset 1 - 8042.
189 */
190 fadt.iapc_boot_arch = iapc_boot_arch_8042();
191
Igor Mammedov937d1b52018-02-28 15:23:51 +0100192 *data = fadt;
193}
194
Like Xu0e11fc62019-05-19 04:54:25 +0800195static void acpi_get_pm_info(MachineState *machine, AcpiPmInfo *pm)
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300196{
Paolo Bonziniee3b34c2024-02-23 13:43:57 +0100197 Object *piix = object_resolve_type_unambiguous(TYPE_PIIX4_PM, NULL);
198 Object *lpc = object_resolve_type_unambiguous(TYPE_ICH9_LPC_DEVICE, NULL);
Igor Mammedov697155c2018-02-28 15:23:47 +0100199 Object *obj = piix ? piix : lpc;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300200 QObject *o;
Daniel P. Berrange94aaca62015-07-31 11:14:35 +0100201 pm->cpu_hp_io_base = 0;
Igor Mammedov500b11e2015-02-18 19:14:50 +0000202 pm->pcihp_io_base = 0;
203 pm->pcihp_io_len = 0;
Igor Mammedov6d837f12020-09-23 05:46:46 -0400204 pm->smi_on_cpuhp = false;
Igor Mammedov892aae72020-12-07 09:07:34 -0500205 pm->smi_on_cpu_unplug = false;
Igor Mammedov937d1b52018-02-28 15:23:51 +0100206
Philippe Mathieu-Daudé6fa51712019-04-27 16:40:25 +0200207 assert(obj);
Like Xua0628592019-05-19 04:54:20 +0800208 init_common_fadt_data(machine, obj, &pm->fadt);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300209 if (piix) {
Igor Mammedov3a3fcc72017-07-24 15:50:20 +0200210 /* w2k requires FADT(rev1) or it won't boot, keep PC compatible */
Igor Mammedov937d1b52018-02-28 15:23:51 +0100211 pm->fadt.rev = 1;
Igor Mammedovddf1ec22015-02-18 19:14:44 +0000212 pm->cpu_hp_io_base = PIIX4_CPU_HOTPLUG_IO_BASE;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300213 }
214 if (lpc) {
Igor Mammedov6d837f12020-09-23 05:46:46 -0400215 uint64_t smi_features = object_property_get_uint(lpc,
216 ICH9_LPC_SMI_NEGOTIATED_FEAT_PROP, NULL);
Igor Mammedov937d1b52018-02-28 15:23:51 +0100217 struct AcpiGenericAddress r = { .space_id = AML_AS_SYSTEM_IO,
218 .bit_width = 8, .address = ICH9_RST_CNT_IOPORT };
219 pm->fadt.reset_reg = r;
220 pm->fadt.reset_val = 0xf;
221 pm->fadt.flags |= 1 << ACPI_FADT_F_RESET_REG_SUP;
Igor Mammedovddf1ec22015-02-18 19:14:44 +0000222 pm->cpu_hp_io_base = ICH9_CPU_HOTPLUG_IO_BASE;
Igor Mammedov6d837f12020-09-23 05:46:46 -0400223 pm->smi_on_cpuhp =
224 !!(smi_features & BIT_ULL(ICH9_LPC_SMI_F_CPU_HOTPLUG_BIT));
Igor Mammedov892aae72020-12-07 09:07:34 -0500225 pm->smi_on_cpu_unplug =
226 !!(smi_features & BIT_ULL(ICH9_LPC_SMI_F_CPU_HOT_UNPLUG_BIT));
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300227 }
Julia Suvorovacaf108b2021-07-13 02:42:00 +0200228 pm->pcihp_io_base =
229 object_property_get_uint(obj, ACPI_PCIHP_IO_BASE_PROP, NULL);
230 pm->pcihp_io_len =
231 object_property_get_uint(obj, ACPI_PCIHP_IO_LEN_PROP, NULL);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300232
233 /* Fill in optional s3/s4 related properties */
234 o = object_property_get_qobject(obj, ACPI_PM_PROP_S3_DISABLED, NULL);
235 if (o) {
Max Reitz7dc847e2018-02-24 16:40:29 +0100236 pm->s3_disabled = qnum_get_uint(qobject_to(QNum, o));
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300237 } else {
238 pm->s3_disabled = false;
239 }
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200240 qobject_unref(o);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300241 o = object_property_get_qobject(obj, ACPI_PM_PROP_S4_DISABLED, NULL);
242 if (o) {
Max Reitz7dc847e2018-02-24 16:40:29 +0100243 pm->s4_disabled = qnum_get_uint(qobject_to(QNum, o));
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300244 } else {
245 pm->s4_disabled = false;
246 }
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200247 qobject_unref(o);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300248 o = object_property_get_qobject(obj, ACPI_PM_PROP_S4_VAL, NULL);
249 if (o) {
Max Reitz7dc847e2018-02-24 16:40:29 +0100250 pm->s4_val = qnum_get_uint(qobject_to(QNum, o));
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300251 } else {
252 pm->s4_val = false;
253 }
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200254 qobject_unref(o);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300255
Igor Mammedov133a2da2014-07-28 17:34:18 +0200256 pm->pcihp_bridge_en =
Ani Sinhaaa294662021-08-16 14:02:14 +0530257 object_property_get_bool(obj, ACPI_PM_PROP_ACPI_PCIHP_BRIDGE,
Igor Mammedov133a2da2014-07-28 17:34:18 +0200258 NULL);
Ani Sinhadf4008c2020-09-18 14:11:08 +0530259 pm->pcihp_root_en =
Ani Sinhaaa294662021-08-16 14:02:14 +0530260 object_property_get_bool(obj, ACPI_PM_PROP_ACPI_PCI_ROOTHP,
Ani Sinhadf4008c2020-09-18 14:11:08 +0530261 NULL);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300262}
263
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300264static void acpi_get_misc_info(AcpiMiscInfo *info)
265{
266 info->has_hpet = hpet_find();
Stefan Berger11fb99e2021-06-15 16:21:17 +0200267#ifdef CONFIG_TPM
Marc-André Lureau3dfd5a22017-11-06 19:39:15 +0100268 info->tpm_version = tpm_get_version(tpm_find());
Stefan Berger11fb99e2021-06-15 16:21:17 +0200269#endif
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300270}
271
Marcel Apfelbaumca6c1852015-06-02 14:22:59 +0300272/*
273 * Because of the PXB hosts we cannot simply query TYPE_PCI_HOST_BRIDGE.
274 * On i386 arch we only have two pci hosts, so we can look only for them.
275 */
Julia Suvorovac0e427d2021-07-13 02:42:01 +0200276Object *acpi_get_i386_pci_host(void)
Marcel Apfelbaumca6c1852015-06-02 14:22:59 +0300277{
278 PCIHostState *host;
279
Eduardo Habkostb914e742021-08-05 15:34:31 -0400280 host = PCI_HOST_BRIDGE(object_resolve_path("/machine/i440fx", NULL));
Marcel Apfelbaumca6c1852015-06-02 14:22:59 +0300281 if (!host) {
Eduardo Habkostb914e742021-08-05 15:34:31 -0400282 host = PCI_HOST_BRIDGE(object_resolve_path("/machine/q35", NULL));
Marcel Apfelbaumca6c1852015-06-02 14:22:59 +0300283 }
284
285 return OBJECT(host);
286}
287
Markus Armbruster01c97422016-06-15 19:56:31 +0200288static void acpi_get_pci_holes(Range *hole, Range *hole64)
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300289{
290 Object *pci_host;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300291
Marcel Apfelbaumca6c1852015-06-02 14:22:59 +0300292 pci_host = acpi_get_i386_pci_host();
Julia Suvorovac0e427d2021-07-13 02:42:01 +0200293
294 if (!pci_host) {
295 return;
296 }
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300297
Markus Armbrustera0efbf12016-07-01 13:47:47 +0200298 range_set_bounds1(hole,
Marc-André Lureau60555362017-06-07 20:36:21 +0400299 object_property_get_uint(pci_host,
300 PCI_HOST_PROP_PCI_HOLE_START,
301 NULL),
302 object_property_get_uint(pci_host,
303 PCI_HOST_PROP_PCI_HOLE_END,
304 NULL));
Markus Armbrustera0efbf12016-07-01 13:47:47 +0200305 range_set_bounds1(hole64,
Marc-André Lureau60555362017-06-07 20:36:21 +0400306 object_property_get_uint(pci_host,
307 PCI_HOST_PROP_PCI_HOLE64_START,
308 NULL),
309 object_property_get_uint(pci_host,
310 PCI_HOST_PROP_PCI_HOLE64_END,
311 NULL));
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300312}
313
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300314static void acpi_align_size(GArray *blob, unsigned align)
315{
316 /* Align size to multiple of given size. This reduces the chance
317 * we need to change size in the future (breaking cross version migration).
318 */
Michael S. Tsirkin134d42d2013-11-26 00:00:39 +0200319 g_array_set_size(blob, ROUND_UP(acpi_data_len(blob), align));
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300320}
321
Igor Mammedovcf684102021-09-24 08:28:00 -0400322/*
323 * ACPI spec 1.0b,
324 * 5.2.6 Firmware ACPI Control Structure
325 */
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300326static void
Wei Yang009180b2019-01-30 11:02:07 +0800327build_facs(GArray *table_data)
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300328{
Igor Mammedovcf684102021-09-24 08:28:00 -0400329 const char *sig = "FACS";
330 const uint8_t reserved[40] = {};
331
332 g_array_append_vals(table_data, sig, 4); /* Signature */
333 build_append_int_noprefix(table_data, 64, 4); /* Length */
334 build_append_int_noprefix(table_data, 0, 4); /* Hardware Signature */
335 build_append_int_noprefix(table_data, 0, 4); /* Firmware Waking Vector */
336 build_append_int_noprefix(table_data, 0, 4); /* Global Lock */
337 build_append_int_noprefix(table_data, 0, 4); /* Flags */
338 g_array_append_vals(table_data, reserved, 40); /* Reserved */
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300339}
340
Igor Mammedov5840a162022-07-01 09:35:07 -0400341Aml *aml_pci_device_dsm(void)
342{
343 Aml *method;
344
345 method = aml_method("_DSM", 4, AML_SERIALIZED);
346 {
347 Aml *params = aml_local(0);
348 Aml *pkg = aml_package(2);
Igor Mammedov44d975e2023-07-20 15:38:54 +0200349 aml_append(pkg, aml_int(0));
350 aml_append(pkg, aml_int(0));
Igor Mammedov5840a162022-07-01 09:35:07 -0400351 aml_append(method, aml_store(pkg, params));
352 aml_append(method,
Igor Mammedov44d975e2023-07-20 15:38:54 +0200353 aml_store(aml_name("BSEL"), aml_index(params, aml_int(0))));
354 aml_append(method,
355 aml_store(aml_name("ASUN"), aml_index(params, aml_int(1))));
356 aml_append(method,
Igor Mammedov5840a162022-07-01 09:35:07 -0400357 aml_return(aml_call5("PDSM", aml_arg(0), aml_arg(1),
358 aml_arg(2), aml_arg(3), params))
359 );
360 }
361 return method;
362}
363
Igor Mammedov0a4584f2023-03-02 17:15:23 +0100364static void build_append_pci_dsm_func0_common(Aml *ctx, Aml *retvar)
365{
366 Aml *UUID, *ifctx1;
367 uint8_t byte_list[1] = { 0 }; /* nothing supported yet */
368
369 aml_append(ctx, aml_store(aml_buffer(1, byte_list), retvar));
370 /*
371 * PCI Firmware Specification 3.1
372 * 4.6. _DSM Definitions for PCI
373 */
374 UUID = aml_touuid("E5C937D0-3553-4D7A-9117-EA4D19C3434D");
375 ifctx1 = aml_if(aml_lnot(aml_equal(aml_arg(0), UUID)));
376 {
377 /* call is for unsupported UUID, bail out */
378 aml_append(ifctx1, aml_return(retvar));
379 }
380 aml_append(ctx, ifctx1);
381
382 ifctx1 = aml_if(aml_lless(aml_arg(1), aml_int(2)));
383 {
384 /* call is for unsupported REV, bail out */
385 aml_append(ifctx1, aml_return(retvar));
386 }
387 aml_append(ctx, ifctx1);
388}
389
Igor Mammedovfe0d5f52023-03-02 17:15:26 +0100390static Aml *aml_pci_edsm(void)
391{
392 Aml *method, *ifctx;
393 Aml *zero = aml_int(0);
394 Aml *func = aml_arg(2);
395 Aml *ret = aml_local(0);
396 Aml *aidx = aml_local(1);
397 Aml *params = aml_arg(4);
398
399 method = aml_method("EDSM", 5, AML_SERIALIZED);
400
401 /* get supported functions */
402 ifctx = aml_if(aml_equal(func, zero));
403 {
404 /* 1: have supported functions */
405 /* 7: support for function 7 */
406 const uint8_t caps = 1 | BIT(7);
407 build_append_pci_dsm_func0_common(ifctx, ret);
408 aml_append(ifctx, aml_store(aml_int(caps), aml_index(ret, zero)));
409 aml_append(ifctx, aml_return(ret));
410 }
411 aml_append(method, ifctx);
412
413 /* handle specific functions requests */
414 /*
415 * PCI Firmware Specification 3.1
416 * 4.6.7. _DSM for Naming a PCI or PCI Express Device Under
417 * Operating Systems
418 */
419 ifctx = aml_if(aml_equal(func, aml_int(7)));
420 {
421 Aml *pkg = aml_package(2);
422 aml_append(pkg, zero);
423 /* optional, if not impl. should return null string */
424 aml_append(pkg, aml_string("%s", ""));
425 aml_append(ifctx, aml_store(pkg, ret));
426
427 /*
428 * IASL is fine when initializing Package with computational data,
429 * however it makes guest unhappy /it fails to process such AML/.
430 * So use runtime assignment to set acpi-index after initializer
431 * to make OSPM happy.
432 */
433 aml_append(ifctx,
434 aml_store(aml_derefof(aml_index(params, aml_int(0))), aidx));
435 aml_append(ifctx, aml_store(aidx, aml_index(ret, zero)));
436 aml_append(ifctx, aml_return(ret));
437 }
438 aml_append(method, ifctx);
439
440 return method;
441}
Igor Mammedov0a4584f2023-03-02 17:15:23 +0100442
Igor Mammedov7fb1d732023-03-02 17:15:30 +0100443static Aml *aml_pci_static_endpoint_dsm(PCIDevice *pdev)
444{
445 Aml *method;
446
447 g_assert(pdev->acpi_index != 0);
448 method = aml_method("_DSM", 4, AML_SERIALIZED);
449 {
450 Aml *params = aml_local(0);
451 Aml *pkg = aml_package(1);
452 aml_append(pkg, aml_int(pdev->acpi_index));
453 aml_append(method, aml_store(pkg, params));
454 aml_append(method,
455 aml_return(aml_call5("EDSM", aml_arg(0), aml_arg(1),
456 aml_arg(2), aml_arg(3), params))
457 );
458 }
459 return method;
460}
461
Igor Mammedov62b52c22015-02-20 18:22:18 +0000462static void build_append_pcihp_notify_entry(Aml *method, int slot)
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300463{
Igor Mammedov62b52c22015-02-20 18:22:18 +0000464 Aml *if_ctx;
465 int32_t devfn = PCI_DEVFN(slot, 0);
Igor Mammedovb23046a2015-02-20 18:22:16 +0000466
Igor Mammedov55304272015-12-10 00:41:17 +0100467 if_ctx = aml_if(aml_and(aml_arg(0), aml_int(0x1U << slot), NULL));
Igor Mammedov62b52c22015-02-20 18:22:18 +0000468 aml_append(if_ctx, aml_notify(aml_name("S%.02X", devfn), aml_arg(1)));
469 aml_append(method, if_ctx);
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300470}
471
Igor Mammedov6fe55182023-01-12 15:03:05 +0100472static bool is_devfn_ignored_generic(const int devfn, const PCIBus *bus)
Igor Mammedova06c15a2023-01-12 15:03:03 +0100473{
474 const PCIDevice *pdev = bus->devices[devfn];
475
Igor Mammedov6fe55182023-01-12 15:03:05 +0100476 if (PCI_FUNC(devfn)) {
477 if (IS_PCI_BRIDGE(pdev)) {
478 /*
479 * Ignore only hotplugged PCI bridges on !0 functions, but
480 * allow describing cold plugged bridges on all functions
481 */
482 if (DEVICE(pdev)->hotplugged) {
Igor Mammedova06c15a2023-01-12 15:03:03 +0100483 return true;
484 }
485 }
Igor Mammedov6fe55182023-01-12 15:03:05 +0100486 }
487 return false;
488}
489
490static bool is_devfn_ignored_hotplug(const int devfn, const PCIBus *bus)
491{
Igor Mammedov64a55102023-01-12 15:03:08 +0100492 PCIDevice *pdev = bus->devices[devfn];
493 if (pdev) {
494 return is_devfn_ignored_generic(devfn, bus) ||
Igor Mammedov17f4ced2023-01-12 15:03:11 +0100495 !DEVICE_GET_CLASS(pdev)->hotpluggable ||
Igor Mammedov64a55102023-01-12 15:03:08 +0100496 /* Cold plugged bridges aren't themselves hot-pluggable */
497 (IS_PCI_BRIDGE(pdev) && !DEVICE(pdev)->hotplugged);
Igor Mammedova06c15a2023-01-12 15:03:03 +0100498 } else { /* non populated slots */
Igor Mammedov6fe55182023-01-12 15:03:05 +0100499 /*
Igor Mammedova06c15a2023-01-12 15:03:03 +0100500 * hotplug is supported only for non-multifunction device
501 * so generate device description only for function 0
502 */
Igor Mammedov6fe55182023-01-12 15:03:05 +0100503 if (PCI_FUNC(devfn) ||
Igor Mammedova06c15a2023-01-12 15:03:03 +0100504 (pci_bus_is_express(bus) && PCI_SLOT(devfn) > 0)) {
505 return true;
506 }
507 }
508 return false;
509}
510
Igor Mammedov02c10612023-03-02 17:15:41 +0100511void build_append_pcihp_slots(Aml *parent_scope, PCIBus *bus)
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300512{
Igor Mammedovd7346e62021-07-23 05:04:24 -0400513 int devfn;
Igor Mammedov6fe55182023-01-12 15:03:05 +0100514 Aml *dev, *notify_method = NULL, *method;
Igor Mammedov62dd55f2023-03-02 17:15:40 +0100515 QObject *bsel = object_property_get_qobject(OBJECT(bus),
516 ACPI_PCIHP_PROP_BSEL, NULL);
Igor Mammedov6fe55182023-01-12 15:03:05 +0100517 uint64_t bsel_val = qnum_get_uint(qobject_to(QNum, bsel));
Igor Mammedov62dd55f2023-03-02 17:15:40 +0100518 qobject_unref(bsel);
Igor Mammedov133a2da2014-07-28 17:34:18 +0200519
Igor Mammedov6fe55182023-01-12 15:03:05 +0100520 aml_append(parent_scope, aml_name_decl("BSEL", aml_int(bsel_val)));
521 notify_method = aml_method("DVNT", 2, AML_NOTSERIALIZED);
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300522
Igor Mammedovd7346e62021-07-23 05:04:24 -0400523 for (devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
Igor Mammedovd7346e62021-07-23 05:04:24 -0400524 int slot = PCI_SLOT(devfn);
Igor Mammedov6fe55182023-01-12 15:03:05 +0100525 int adr = slot << 16 | PCI_FUNC(devfn);
Igor Mammedova06c15a2023-01-12 15:03:03 +0100526
Igor Mammedov6fe55182023-01-12 15:03:05 +0100527 if (is_devfn_ignored_hotplug(devfn, bus)) {
Igor Mammedova06c15a2023-01-12 15:03:03 +0100528 continue;
529 }
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300530
Igor Mammedov17f4ced2023-01-12 15:03:11 +0100531 if (bus->devices[devfn]) {
Igor Mammedov6fe55182023-01-12 15:03:05 +0100532 dev = aml_scope("S%.02X", devfn);
533 } else {
534 dev = aml_device("S%.02X", devfn);
535 aml_append(dev, aml_name_decl("_ADR", aml_int(adr)));
Igor Mammedovd7346e62021-07-23 05:04:24 -0400536 }
537
Igor Mammedov6fe55182023-01-12 15:03:05 +0100538 /*
539 * Can't declare _SUN here for every device as it changes 'slot'
540 * enumeration order in linux kernel, so use another variable for it
541 */
542 aml_append(dev, aml_name_decl("ASUN", aml_int(slot)));
543 aml_append(dev, aml_pci_device_dsm());
Igor Mammedov62b52c22015-02-20 18:22:18 +0000544
Igor Mammedov17f4ced2023-01-12 15:03:11 +0100545 aml_append(dev, aml_name_decl("_SUN", aml_int(slot)));
546 /* add _EJ0 to make slot hotpluggable */
547 method = aml_method("_EJ0", 1, AML_NOTSERIALIZED);
548 aml_append(method,
549 aml_call2("PCEJ", aml_name("BSEL"), aml_name("_SUN"))
550 );
551 aml_append(dev, method);
Igor Mammedov3216ab22022-07-01 09:35:15 -0400552
Igor Mammedov17f4ced2023-01-12 15:03:11 +0100553 build_append_pcihp_notify_entry(notify_method, slot);
Igor Mammedov3216ab22022-07-01 09:35:15 -0400554
Igor Mammedovd7346e62021-07-23 05:04:24 -0400555 /* device descriptor has been composed, add it into parent context */
Igor Mammedov62b52c22015-02-20 18:22:18 +0000556 aml_append(parent_scope, dev);
Michael S. Tsirkin8dcf5252014-02-04 17:43:47 +0200557 }
Igor Mammedov6fe55182023-01-12 15:03:05 +0100558 aml_append(parent_scope, notify_method);
559}
560
561void build_append_pci_bus_devices(Aml *parent_scope, PCIBus *bus)
562{
Igor Mammedov6fe55182023-01-12 15:03:05 +0100563 int devfn;
564 Aml *dev;
565
Igor Mammedov6fe55182023-01-12 15:03:05 +0100566 for (devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
567 /* ACPI spec: 1.0b: Table 6-2 _ADR Object Bus Types, PCI type */
568 int adr = PCI_SLOT(devfn) << 16 | PCI_FUNC(devfn);
Igor Mammedov17f4ced2023-01-12 15:03:11 +0100569 PCIDevice *pdev = bus->devices[devfn];
Igor Mammedov6fe55182023-01-12 15:03:05 +0100570
Igor Mammedov17f4ced2023-01-12 15:03:11 +0100571 if (!pdev || is_devfn_ignored_generic(devfn, bus)) {
Igor Mammedov6fe55182023-01-12 15:03:05 +0100572 continue;
573 }
574
575 /* start to compose PCI device descriptor */
576 dev = aml_device("S%.02X", devfn);
577 aml_append(dev, aml_name_decl("_ADR", aml_int(adr)));
578
579 call_dev_aml_func(DEVICE(bus->devices[devfn]), dev);
Igor Mammedov7fb1d732023-03-02 17:15:30 +0100580 /* add _DSM if device has acpi-index set */
Igor Mammedov419233b2023-03-02 17:15:39 +0100581 if (pdev->acpi_index &&
Igor Mammedov7fb1d732023-03-02 17:15:30 +0100582 !object_property_get_bool(OBJECT(pdev), "hotpluggable",
583 &error_abort)) {
584 aml_append(dev, aml_pci_static_endpoint_dsm(pdev));
585 }
Igor Mammedov6fe55182023-01-12 15:03:05 +0100586
587 /* device descriptor has been composed, add it into parent context */
588 aml_append(parent_scope, dev);
589 }
Igor Mammedovddab4d32023-01-12 15:02:53 +0100590}
591
Igor Mammedov219e6382023-01-12 15:02:54 +0100592static bool build_append_notfication_callback(Aml *parent_scope,
Igor Mammedovddab4d32023-01-12 15:02:53 +0100593 const PCIBus *bus)
594{
595 Aml *method;
596 PCIBus *sec;
597 QObject *bsel;
Igor Mammedov219e6382023-01-12 15:02:54 +0100598 int nr_notifiers = 0;
Igor Mammedov11215a32023-03-02 17:15:16 +0100599 GQueue *pcnt_bus_list = g_queue_new();
Igor Mammedovddab4d32023-01-12 15:02:53 +0100600
601 QLIST_FOREACH(sec, &bus->child, sibling) {
602 Aml *br_scope = aml_scope("S%.02X", sec->parent_dev->devfn);
Igor Mammedov11215a32023-03-02 17:15:16 +0100603 if (pci_bus_is_root(sec)) {
Igor Mammedovddab4d32023-01-12 15:02:53 +0100604 continue;
605 }
Igor Mammedov219e6382023-01-12 15:02:54 +0100606 nr_notifiers = nr_notifiers +
607 build_append_notfication_callback(br_scope, sec);
Igor Mammedov11215a32023-03-02 17:15:16 +0100608 /*
609 * add new child scope to parent
610 * and keep track of bus that have PCNT,
611 * bus list is used later to call children PCNTs from this level PCNT
612 */
613 if (nr_notifiers) {
614 g_queue_push_tail(pcnt_bus_list, sec);
615 aml_append(parent_scope, br_scope);
616 }
Igor Mammedovddab4d32023-01-12 15:02:53 +0100617 }
618
Igor Mammedov19f50522023-01-12 15:02:50 +0100619 /*
620 * Append PCNT method to notify about events on local and child buses.
Igor Mammedovddab4d32023-01-12 15:02:53 +0100621 * ps: hostbridge might not have hotplug (bsel) enabled but might have
622 * child bridges that do have bsel.
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300623 */
Igor Mammedov19f50522023-01-12 15:02:50 +0100624 method = aml_method("PCNT", 0, AML_NOTSERIALIZED);
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400625
Igor Mammedov19f50522023-01-12 15:02:50 +0100626 /* If bus supports hotplug select it and notify about local events */
Igor Mammedovddab4d32023-01-12 15:02:53 +0100627 bsel = object_property_get_qobject(OBJECT(bus), ACPI_PCIHP_PROP_BSEL, NULL);
Igor Mammedov19f50522023-01-12 15:02:50 +0100628 if (bsel) {
629 uint64_t bsel_val = qnum_get_uint(qobject_to(QNum, bsel));
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300630
Igor Mammedov19f50522023-01-12 15:02:50 +0100631 aml_append(method, aml_store(aml_int(bsel_val), aml_name("BNUM")));
632 aml_append(method, aml_call2("DVNT", aml_name("PCIU"),
633 aml_int(1))); /* Device Check */
634 aml_append(method, aml_call2("DVNT", aml_name("PCID"),
635 aml_int(3))); /* Eject Request */
Igor Mammedov219e6382023-01-12 15:02:54 +0100636 nr_notifiers++;
Ani Sinhadf4008c2020-09-18 14:11:08 +0530637 }
Igor Mammedov19f50522023-01-12 15:02:50 +0100638
639 /* Notify about child bus events in any case */
Igor Mammedov11215a32023-03-02 17:15:16 +0100640 while ((sec = g_queue_pop_head(pcnt_bus_list))) {
Igor Mammedov19f50522023-01-12 15:02:50 +0100641 aml_append(method, aml_name("^S%.02X.PCNT", sec->parent_dev->devfn));
642 }
643
644 aml_append(parent_scope, method);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200645 qobject_unref(bsel);
Igor Mammedov11215a32023-03-02 17:15:16 +0100646 g_queue_free(pcnt_bus_list);
Igor Mammedov219e6382023-01-12 15:02:54 +0100647 return !!nr_notifiers;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300648}
649
Igor Mammedov5840a162022-07-01 09:35:07 -0400650static Aml *aml_pci_pdsm(void)
Igor Mammedovb7f23f62021-03-15 14:01:01 -0400651{
Igor Mammedov0a4584f2023-03-02 17:15:23 +0100652 Aml *method, *ifctx, *ifctx1;
Igor Mammedova12cf692022-07-01 09:35:03 -0400653 Aml *ret = aml_local(0);
654 Aml *caps = aml_local(1);
655 Aml *acpi_index = aml_local(2);
Igor Mammedovb7f23f62021-03-15 14:01:01 -0400656 Aml *zero = aml_int(0);
Igor Mammedova12cf692022-07-01 09:35:03 -0400657 Aml *one = aml_int(1);
Igor Mammedovb7f23f62021-03-15 14:01:01 -0400658 Aml *func = aml_arg(2);
Igor Mammedov467d0992022-07-01 09:35:04 -0400659 Aml *params = aml_arg(4);
660 Aml *bnum = aml_derefof(aml_index(params, aml_int(0)));
661 Aml *sunum = aml_derefof(aml_index(params, aml_int(1)));
Igor Mammedovb7f23f62021-03-15 14:01:01 -0400662
Igor Mammedov467d0992022-07-01 09:35:04 -0400663 method = aml_method("PDSM", 5, AML_SERIALIZED);
Igor Mammedovb7f23f62021-03-15 14:01:01 -0400664
Igor Mammedova12cf692022-07-01 09:35:03 -0400665 /* get supported functions */
666 ifctx = aml_if(aml_equal(func, zero));
Igor Mammedovb7f23f62021-03-15 14:01:01 -0400667 {
Igor Mammedov0a4584f2023-03-02 17:15:23 +0100668 build_append_pci_dsm_func0_common(ifctx, ret);
669
Igor Mammedova12cf692022-07-01 09:35:03 -0400670 aml_append(ifctx, aml_store(zero, caps));
Igor Mammedova12cf692022-07-01 09:35:03 -0400671 aml_append(ifctx,
672 aml_store(aml_call2("AIDX", bnum, sunum), acpi_index));
673 /*
674 * advertise function 7 if device has acpi-index
675 * acpi_index values:
676 * 0: not present (default value)
677 * FFFFFFFF: not supported (old QEMU without PIDX reg)
678 * other: device's acpi-index
679 */
680 ifctx1 = aml_if(aml_lnot(
681 aml_or(aml_equal(acpi_index, zero),
682 aml_equal(acpi_index, aml_int(0xFFFFFFFF)), NULL)
683 ));
684 {
685 /* have supported functions */
686 aml_append(ifctx1, aml_or(caps, one, caps));
687 /* support for function 7 */
688 aml_append(ifctx1,
689 aml_or(caps, aml_shiftleft(one, aml_int(7)), caps));
690 }
691 aml_append(ifctx, ifctx1);
Igor Mammedovb7f23f62021-03-15 14:01:01 -0400692
Igor Mammedova12cf692022-07-01 09:35:03 -0400693 aml_append(ifctx, aml_store(caps, aml_index(ret, zero)));
694 aml_append(ifctx, aml_return(ret));
Igor Mammedovb7f23f62021-03-15 14:01:01 -0400695 }
696 aml_append(method, ifctx);
Igor Mammedova12cf692022-07-01 09:35:03 -0400697
698 /* handle specific functions requests */
699 /*
700 * PCI Firmware Specification 3.1
701 * 4.6.7. _DSM for Naming a PCI or PCI Express Device Under
702 * Operating Systems
703 */
704 ifctx = aml_if(aml_equal(func, aml_int(7)));
705 {
706 Aml *pkg = aml_package(2);
707
708 aml_append(pkg, zero);
709 /*
710 * optional, if not impl. should return null string
711 */
712 aml_append(pkg, aml_string("%s", ""));
713 aml_append(ifctx, aml_store(pkg, ret));
714
715 aml_append(ifctx, aml_store(aml_call2("AIDX", bnum, sunum), acpi_index));
716 /*
717 * update acpi-index to actual value
718 */
719 aml_append(ifctx, aml_store(acpi_index, aml_index(ret, zero)));
Igor Mammedov467d0992022-07-01 09:35:04 -0400720 aml_append(ifctx, aml_return(ret));
Igor Mammedova12cf692022-07-01 09:35:03 -0400721 }
722
723 aml_append(method, ifctx);
Igor Mammedovb7f23f62021-03-15 14:01:01 -0400724 return method;
725}
726
Igor Mammedov196e2132015-12-28 18:02:42 +0100727/**
728 * build_prt_entry:
729 * @link_name: link name for PCI route entry
730 *
731 * build AML package containing a PCI route entry for @link_name
732 */
733static Aml *build_prt_entry(const char *link_name)
734{
735 Aml *a_zero = aml_int(0);
736 Aml *pkg = aml_package(4);
737 aml_append(pkg, a_zero);
738 aml_append(pkg, a_zero);
739 aml_append(pkg, aml_name("%s", link_name));
740 aml_append(pkg, a_zero);
741 return pkg;
742}
743
Marcel Apfelbaum0d8935e2015-06-02 14:23:02 +0300744/*
745 * initialize_route - Initialize the interrupt routing rule
746 * through a specific LINK:
747 * if (lnk_idx == idx)
748 * route using link 'link_name'
749 */
750static Aml *initialize_route(Aml *route, const char *link_name,
751 Aml *lnk_idx, int idx)
752{
753 Aml *if_ctx = aml_if(aml_equal(lnk_idx, aml_int(idx)));
Igor Mammedov196e2132015-12-28 18:02:42 +0100754 Aml *pkg = build_prt_entry(link_name);
Marcel Apfelbaum0d8935e2015-06-02 14:23:02 +0300755
Marcel Apfelbaum0d8935e2015-06-02 14:23:02 +0300756 aml_append(if_ctx, aml_store(pkg, route));
757
758 return if_ctx;
759}
760
761/*
762 * build_prt - Define interrupt rounting rules
763 *
764 * Returns an array of 128 routes, one for each device,
765 * based on device location.
Michael Tokarevbad5cfc2023-07-14 14:16:12 +0300766 * The main goal is to equally distribute the interrupts
Marcel Apfelbaum0d8935e2015-06-02 14:23:02 +0300767 * over the 4 existing ACPI links (works only for i440fx).
768 * The hash function is (slot + pin) & 3 -> "LNK[D|A|B|C]".
769 *
770 */
Igor Mammedov196e2132015-12-28 18:02:42 +0100771static Aml *build_prt(bool is_pci0_prt)
Marcel Apfelbaum0d8935e2015-06-02 14:23:02 +0300772{
773 Aml *method, *while_ctx, *pin, *res;
774
Xiao Guangrong4dbfc882015-12-17 13:37:13 +0000775 method = aml_method("_PRT", 0, AML_NOTSERIALIZED);
Marcel Apfelbaum0d8935e2015-06-02 14:23:02 +0300776 res = aml_local(0);
777 pin = aml_local(1);
778 aml_append(method, aml_store(aml_package(128), res));
779 aml_append(method, aml_store(aml_int(0), pin));
780
781 /* while (pin < 128) */
782 while_ctx = aml_while(aml_lless(pin, aml_int(128)));
783 {
784 Aml *slot = aml_local(2);
785 Aml *lnk_idx = aml_local(3);
786 Aml *route = aml_local(4);
787
788 /* slot = pin >> 2 */
789 aml_append(while_ctx,
Igor Mammedovc3606392015-12-10 00:41:06 +0100790 aml_store(aml_shiftright(pin, aml_int(2), NULL), slot));
Marcel Apfelbaum0d8935e2015-06-02 14:23:02 +0300791 /* lnk_idx = (slot + pin) & 3 */
792 aml_append(while_ctx,
Igor Mammedov55304272015-12-10 00:41:17 +0100793 aml_store(aml_and(aml_add(pin, slot, NULL), aml_int(3), NULL),
794 lnk_idx));
Marcel Apfelbaum0d8935e2015-06-02 14:23:02 +0300795
796 /* route[2] = "LNK[D|A|B|C]", selection based on pin % 3 */
797 aml_append(while_ctx, initialize_route(route, "LNKD", lnk_idx, 0));
Igor Mammedov196e2132015-12-28 18:02:42 +0100798 if (is_pci0_prt) {
799 Aml *if_device_1, *if_pin_4, *else_pin_4;
800
801 /* device 1 is the power-management device, needs SCI */
802 if_device_1 = aml_if(aml_equal(lnk_idx, aml_int(1)));
803 {
804 if_pin_4 = aml_if(aml_equal(pin, aml_int(4)));
805 {
806 aml_append(if_pin_4,
807 aml_store(build_prt_entry("LNKS"), route));
808 }
809 aml_append(if_device_1, if_pin_4);
810 else_pin_4 = aml_else();
811 {
812 aml_append(else_pin_4,
813 aml_store(build_prt_entry("LNKA"), route));
814 }
815 aml_append(if_device_1, else_pin_4);
816 }
817 aml_append(while_ctx, if_device_1);
818 } else {
819 aml_append(while_ctx, initialize_route(route, "LNKA", lnk_idx, 1));
820 }
Marcel Apfelbaum0d8935e2015-06-02 14:23:02 +0300821 aml_append(while_ctx, initialize_route(route, "LNKB", lnk_idx, 2));
822 aml_append(while_ctx, initialize_route(route, "LNKC", lnk_idx, 3));
823
824 /* route[0] = 0x[slot]FFFF */
825 aml_append(while_ctx,
Igor Mammedovca3df952015-12-10 00:41:16 +0100826 aml_store(aml_or(aml_shiftleft(slot, aml_int(16)), aml_int(0xFFFF),
827 NULL),
Marcel Apfelbaum0d8935e2015-06-02 14:23:02 +0300828 aml_index(route, aml_int(0))));
829 /* route[1] = pin & 3 */
830 aml_append(while_ctx,
Igor Mammedov55304272015-12-10 00:41:17 +0100831 aml_store(aml_and(pin, aml_int(3), NULL),
832 aml_index(route, aml_int(1))));
Marcel Apfelbaum0d8935e2015-06-02 14:23:02 +0300833 /* res[pin] = route */
834 aml_append(while_ctx, aml_store(route, aml_index(res, pin)));
835 /* pin++ */
836 aml_append(while_ctx, aml_increment(pin));
837 }
838 aml_append(method, while_ctx);
839 /* return res*/
840 aml_append(method, aml_return(res));
841
842 return method;
843}
844
Igor Mammedova57d7082015-12-28 18:02:29 +0100845static void build_hpet_aml(Aml *table)
846{
847 Aml *crs;
848 Aml *field;
849 Aml *method;
850 Aml *if_ctx;
851 Aml *scope = aml_scope("_SB");
852 Aml *dev = aml_device("HPET");
853 Aml *zero = aml_int(0);
854 Aml *id = aml_local(0);
855 Aml *period = aml_local(1);
856
857 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0103")));
858 aml_append(dev, aml_name_decl("_UID", zero));
859
860 aml_append(dev,
Xiao Guangrong3f3009c2016-03-01 18:56:05 +0800861 aml_operation_region("HPTM", AML_SYSTEM_MEMORY, aml_int(HPET_BASE),
862 HPET_LEN));
Igor Mammedova57d7082015-12-28 18:02:29 +0100863 field = aml_field("HPTM", AML_DWORD_ACC, AML_LOCK, AML_PRESERVE);
864 aml_append(field, aml_named_field("VEND", 32));
865 aml_append(field, aml_named_field("PRD", 32));
866 aml_append(dev, field);
867
868 method = aml_method("_STA", 0, AML_NOTSERIALIZED);
869 aml_append(method, aml_store(aml_name("VEND"), id));
870 aml_append(method, aml_store(aml_name("PRD"), period));
871 aml_append(method, aml_shiftright(id, aml_int(16), id));
872 if_ctx = aml_if(aml_lor(aml_equal(id, zero),
873 aml_equal(id, aml_int(0xffff))));
874 {
875 aml_append(if_ctx, aml_return(zero));
876 }
877 aml_append(method, if_ctx);
878
879 if_ctx = aml_if(aml_lor(aml_equal(period, zero),
880 aml_lgreater(period, aml_int(100000000))));
881 {
882 aml_append(if_ctx, aml_return(zero));
883 }
884 aml_append(method, if_ctx);
885
886 aml_append(method, aml_return(aml_int(0x0F)));
887 aml_append(dev, method);
888
889 crs = aml_resource_template();
890 aml_append(crs, aml_memory32_fixed(HPET_BASE, HPET_LEN, AML_READ_ONLY));
891 aml_append(dev, aml_name_decl("_CRS", crs));
892
893 aml_append(scope, dev);
894 aml_append(table, scope);
895}
896
Jon Doron6775d152020-04-24 15:34:43 +0300897static Aml *build_vmbus_device_aml(VMBusBridge *vmbus_bridge)
898{
899 Aml *dev;
900 Aml *method;
901 Aml *crs;
902
903 dev = aml_device("VMBS");
904 aml_append(dev, aml_name_decl("STA", aml_int(0xF)));
905 aml_append(dev, aml_name_decl("_HID", aml_string("VMBus")));
906 aml_append(dev, aml_name_decl("_UID", aml_int(0x0)));
907 aml_append(dev, aml_name_decl("_DDN", aml_string("VMBUS")));
908
909 method = aml_method("_DIS", 0, AML_NOTSERIALIZED);
910 aml_append(method, aml_store(aml_and(aml_name("STA"), aml_int(0xD), NULL),
911 aml_name("STA")));
912 aml_append(dev, method);
913
914 method = aml_method("_PS0", 0, AML_NOTSERIALIZED);
915 aml_append(method, aml_store(aml_or(aml_name("STA"), aml_int(0xF), NULL),
916 aml_name("STA")));
917 aml_append(dev, method);
918
919 method = aml_method("_STA", 0, AML_NOTSERIALIZED);
920 aml_append(method, aml_return(aml_name("STA")));
921 aml_append(dev, method);
922
923 aml_append(dev, aml_name_decl("_PS3", aml_int(0x0)));
924
925 crs = aml_resource_template();
Jon Doron8f06f222020-06-17 19:09:02 +0300926 aml_append(crs, aml_irq_no_flags(vmbus_bridge->irq));
Jon Doron6775d152020-04-24 15:34:43 +0300927 aml_append(dev, aml_name_decl("_CRS", crs));
928
929 return dev;
930}
931
Igor Mammedov3892a2b2015-12-28 18:02:30 +0100932static void build_dbg_aml(Aml *table)
933{
934 Aml *field;
935 Aml *method;
936 Aml *while_ctx;
937 Aml *scope = aml_scope("\\");
938 Aml *buf = aml_local(0);
939 Aml *len = aml_local(1);
940 Aml *idx = aml_local(2);
941
942 aml_append(scope,
Xiao Guangrong3f3009c2016-03-01 18:56:05 +0800943 aml_operation_region("DBG", AML_SYSTEM_IO, aml_int(0x0402), 0x01));
Igor Mammedov3892a2b2015-12-28 18:02:30 +0100944 field = aml_field("DBG", AML_BYTE_ACC, AML_NOLOCK, AML_PRESERVE);
945 aml_append(field, aml_named_field("DBGB", 8));
946 aml_append(scope, field);
947
948 method = aml_method("DBUG", 1, AML_NOTSERIALIZED);
949
950 aml_append(method, aml_to_hexstring(aml_arg(0), buf));
951 aml_append(method, aml_to_buffer(buf, buf));
952 aml_append(method, aml_subtract(aml_sizeof(buf), aml_int(1), len));
953 aml_append(method, aml_store(aml_int(0), idx));
954
955 while_ctx = aml_while(aml_lless(idx, len));
956 aml_append(while_ctx,
957 aml_store(aml_derefof(aml_index(buf, idx)), aml_name("DBGB")));
958 aml_append(while_ctx, aml_increment(idx));
959 aml_append(method, while_ctx);
960
961 aml_append(method, aml_store(aml_int(0x0A), aml_name("DBGB")));
962 aml_append(scope, method);
963
964 aml_append(table, scope);
965}
966
Igor Mammedovc35b6e82015-12-28 18:02:39 +0100967static Aml *build_link_dev(const char *name, uint8_t uid, Aml *reg)
968{
969 Aml *dev;
970 Aml *crs;
971 Aml *method;
972 uint32_t irqs[] = {5, 10, 11};
973
974 dev = aml_device("%s", name);
975 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C0F")));
976 aml_append(dev, aml_name_decl("_UID", aml_int(uid)));
977
978 crs = aml_resource_template();
979 aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL, AML_ACTIVE_HIGH,
980 AML_SHARED, irqs, ARRAY_SIZE(irqs)));
981 aml_append(dev, aml_name_decl("_PRS", crs));
982
983 method = aml_method("_STA", 0, AML_NOTSERIALIZED);
984 aml_append(method, aml_return(aml_call1("IQST", reg)));
985 aml_append(dev, method);
986
987 method = aml_method("_DIS", 0, AML_NOTSERIALIZED);
988 aml_append(method, aml_or(reg, aml_int(0x80), reg));
989 aml_append(dev, method);
990
991 method = aml_method("_CRS", 0, AML_NOTSERIALIZED);
992 aml_append(method, aml_return(aml_call1("IQCR", reg)));
993 aml_append(dev, method);
994
995 method = aml_method("_SRS", 1, AML_NOTSERIALIZED);
996 aml_append(method, aml_create_dword_field(aml_arg(0), aml_int(5), "PRRI"));
997 aml_append(method, aml_store(aml_name("PRRI"), reg));
998 aml_append(dev, method);
999
1000 return dev;
1001 }
1002
Igor Mammedov80b32df2015-12-28 18:02:45 +01001003static Aml *build_gsi_link_dev(const char *name, uint8_t uid, uint8_t gsi)
1004{
1005 Aml *dev;
1006 Aml *crs;
1007 Aml *method;
1008 uint32_t irqs;
1009
1010 dev = aml_device("%s", name);
1011 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C0F")));
1012 aml_append(dev, aml_name_decl("_UID", aml_int(uid)));
1013
1014 crs = aml_resource_template();
1015 irqs = gsi;
1016 aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL, AML_ACTIVE_HIGH,
1017 AML_SHARED, &irqs, 1));
1018 aml_append(dev, aml_name_decl("_PRS", crs));
1019
1020 aml_append(dev, aml_name_decl("_CRS", crs));
1021
Marcel Apfelbaumc82f5032016-03-07 21:14:37 +02001022 /*
1023 * _DIS can be no-op because the interrupt cannot be disabled.
1024 */
1025 method = aml_method("_DIS", 0, AML_NOTSERIALIZED);
1026 aml_append(dev, method);
1027
Igor Mammedov80b32df2015-12-28 18:02:45 +01001028 method = aml_method("_SRS", 1, AML_NOTSERIALIZED);
1029 aml_append(dev, method);
1030
1031 return dev;
1032}
1033
Igor Mammedov16682a92015-12-28 18:02:47 +01001034/* _CRS method - get current settings */
1035static Aml *build_iqcr_method(bool is_piix4)
1036{
1037 Aml *if_ctx;
1038 uint32_t irqs;
1039 Aml *method = aml_method("IQCR", 1, AML_SERIALIZED);
1040 Aml *crs = aml_resource_template();
1041
1042 irqs = 0;
1043 aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL,
1044 AML_ACTIVE_HIGH, AML_SHARED, &irqs, 1));
1045 aml_append(method, aml_name_decl("PRR0", crs));
1046
1047 aml_append(method,
1048 aml_create_dword_field(aml_name("PRR0"), aml_int(5), "PRRI"));
1049
1050 if (is_piix4) {
1051 if_ctx = aml_if(aml_lless(aml_arg(0), aml_int(0x80)));
1052 aml_append(if_ctx, aml_store(aml_arg(0), aml_name("PRRI")));
1053 aml_append(method, if_ctx);
1054 } else {
1055 aml_append(method,
1056 aml_store(aml_and(aml_arg(0), aml_int(0xF), NULL),
1057 aml_name("PRRI")));
1058 }
1059
1060 aml_append(method, aml_return(aml_name("PRR0")));
1061 return method;
1062}
1063
Igor Mammedov78e1ad02015-12-28 18:02:48 +01001064/* _STA method - get status */
1065static Aml *build_irq_status_method(void)
1066{
1067 Aml *if_ctx;
1068 Aml *method = aml_method("IQST", 1, AML_NOTSERIALIZED);
1069
1070 if_ctx = aml_if(aml_and(aml_int(0x80), aml_arg(0), NULL));
1071 aml_append(if_ctx, aml_return(aml_int(0x09)));
1072 aml_append(method, if_ctx);
1073 aml_append(method, aml_return(aml_int(0x0B)));
1074 return method;
1075}
1076
Igor Mammedove4db2792015-12-28 18:02:37 +01001077static void build_piix4_pci0_int(Aml *table)
1078{
Igor Mammedovc35b6e82015-12-28 18:02:39 +01001079 Aml *dev;
1080 Aml *crs;
Igor Mammedovc35b6e82015-12-28 18:02:39 +01001081 Aml *method;
1082 uint32_t irqs;
Igor Mammedove4db2792015-12-28 18:02:37 +01001083 Aml *sb_scope = aml_scope("_SB");
Igor Mammedov196e2132015-12-28 18:02:42 +01001084 Aml *pci0_scope = aml_scope("PCI0");
1085
1086 aml_append(pci0_scope, build_prt(true));
1087 aml_append(sb_scope, pci0_scope);
Igor Mammedove4db2792015-12-28 18:02:37 +01001088
Igor Mammedov78e1ad02015-12-28 18:02:48 +01001089 aml_append(sb_scope, build_irq_status_method());
Igor Mammedov16682a92015-12-28 18:02:47 +01001090 aml_append(sb_scope, build_iqcr_method(true));
Igor Mammedov100681c2015-12-28 18:02:40 +01001091
Igor Mammedovc35b6e82015-12-28 18:02:39 +01001092 aml_append(sb_scope, build_link_dev("LNKA", 0, aml_name("PRQ0")));
1093 aml_append(sb_scope, build_link_dev("LNKB", 1, aml_name("PRQ1")));
1094 aml_append(sb_scope, build_link_dev("LNKC", 2, aml_name("PRQ2")));
1095 aml_append(sb_scope, build_link_dev("LNKD", 3, aml_name("PRQ3")));
1096
1097 dev = aml_device("LNKS");
1098 {
1099 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C0F")));
1100 aml_append(dev, aml_name_decl("_UID", aml_int(4)));
1101
1102 crs = aml_resource_template();
1103 irqs = 9;
1104 aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL,
1105 AML_ACTIVE_HIGH, AML_SHARED,
1106 &irqs, 1));
1107 aml_append(dev, aml_name_decl("_PRS", crs));
1108
1109 /* The SCI cannot be disabled and is always attached to GSI 9,
1110 * so these are no-ops. We only need this link to override the
1111 * polarity to active high and match the content of the MADT.
1112 */
1113 method = aml_method("_STA", 0, AML_NOTSERIALIZED);
1114 aml_append(method, aml_return(aml_int(0x0b)));
1115 aml_append(dev, method);
1116
1117 method = aml_method("_DIS", 0, AML_NOTSERIALIZED);
1118 aml_append(dev, method);
1119
1120 method = aml_method("_CRS", 0, AML_NOTSERIALIZED);
1121 aml_append(method, aml_return(aml_name("_PRS")));
1122 aml_append(dev, method);
1123
1124 method = aml_method("_SRS", 1, AML_NOTSERIALIZED);
1125 aml_append(dev, method);
1126 }
1127 aml_append(sb_scope, dev);
1128
Igor Mammedove4db2792015-12-28 18:02:37 +01001129 aml_append(table, sb_scope);
1130}
1131
Igor Mammedov22b5b8b2015-12-28 18:02:51 +01001132static void append_q35_prt_entry(Aml *ctx, uint32_t nr, const char *name)
1133{
1134 int i;
1135 int head;
1136 Aml *pkg;
1137 char base = name[3] < 'E' ? 'A' : 'E';
1138 char *s = g_strdup(name);
1139 Aml *a_nr = aml_int((nr << 16) | 0xffff);
1140
1141 assert(strlen(s) == 4);
1142
1143 head = name[3] - base;
1144 for (i = 0; i < 4; i++) {
1145 if (head + i > 3) {
1146 head = i * -1;
1147 }
1148 s[3] = base + head + i;
1149 pkg = aml_package(4);
1150 aml_append(pkg, a_nr);
1151 aml_append(pkg, aml_int(i));
1152 aml_append(pkg, aml_name("%s", s));
1153 aml_append(pkg, aml_int(0));
1154 aml_append(ctx, pkg);
1155 }
1156 g_free(s);
1157}
1158
1159static Aml *build_q35_routing_table(const char *str)
1160{
1161 int i;
1162 Aml *pkg;
1163 char *name = g_strdup_printf("%s ", str);
1164
1165 pkg = aml_package(128);
1166 for (i = 0; i < 0x18; i++) {
1167 name[3] = 'E' + (i & 0x3);
1168 append_q35_prt_entry(pkg, i, name);
1169 }
1170
1171 name[3] = 'E';
1172 append_q35_prt_entry(pkg, 0x18, name);
1173
1174 /* INTA -> PIRQA for slot 25 - 31, see the default value of D<N>IR */
1175 for (i = 0x0019; i < 0x1e; i++) {
1176 name[3] = 'A';
1177 append_q35_prt_entry(pkg, i, name);
1178 }
1179
1180 /* PCIe->PCI bridge. use PIRQ[E-H] */
1181 name[3] = 'E';
1182 append_q35_prt_entry(pkg, 0x1e, name);
1183 name[3] = 'A';
1184 append_q35_prt_entry(pkg, 0x1f, name);
1185
1186 g_free(name);
1187 return pkg;
1188}
1189
Igor Mammedov80b32df2015-12-28 18:02:45 +01001190static void build_q35_pci0_int(Aml *table)
1191{
Igor Mammedov0dafe3b2015-12-28 18:02:50 +01001192 Aml *method;
Igor Mammedov80b32df2015-12-28 18:02:45 +01001193 Aml *sb_scope = aml_scope("_SB");
Igor Mammedov0dafe3b2015-12-28 18:02:50 +01001194 Aml *pci0_scope = aml_scope("PCI0");
1195
Igor Mammedove9fce792015-12-28 18:02:53 +01001196 /* Zero => PIC mode, One => APIC Mode */
1197 aml_append(table, aml_name_decl("PICF", aml_int(0)));
1198 method = aml_method("_PIC", 1, AML_NOTSERIALIZED);
1199 {
1200 aml_append(method, aml_store(aml_arg(0), aml_name("PICF")));
1201 }
1202 aml_append(table, method);
1203
Igor Mammedov22b5b8b2015-12-28 18:02:51 +01001204 aml_append(pci0_scope,
Igor Mammedov65aef4d2015-12-28 18:02:52 +01001205 aml_name_decl("PRTP", build_q35_routing_table("LNK")));
1206 aml_append(pci0_scope,
Igor Mammedov22b5b8b2015-12-28 18:02:51 +01001207 aml_name_decl("PRTA", build_q35_routing_table("GSI")));
1208
Igor Mammedov0dafe3b2015-12-28 18:02:50 +01001209 method = aml_method("_PRT", 0, AML_NOTSERIALIZED);
1210 {
1211 Aml *if_ctx;
1212 Aml *else_ctx;
1213
1214 /* PCI IRQ routing table, example from ACPI 2.0a specification,
1215 section 6.2.8.1 */
1216 /* Note: we provide the same info as the PCI routing
1217 table of the Bochs BIOS */
1218 if_ctx = aml_if(aml_equal(aml_name("PICF"), aml_int(0)));
1219 aml_append(if_ctx, aml_return(aml_name("PRTP")));
1220 aml_append(method, if_ctx);
1221 else_ctx = aml_else();
1222 aml_append(else_ctx, aml_return(aml_name("PRTA")));
1223 aml_append(method, else_ctx);
1224 }
1225 aml_append(pci0_scope, method);
1226 aml_append(sb_scope, pci0_scope);
Igor Mammedov80b32df2015-12-28 18:02:45 +01001227
Igor Mammedov78e1ad02015-12-28 18:02:48 +01001228 aml_append(sb_scope, build_irq_status_method());
Igor Mammedov16682a92015-12-28 18:02:47 +01001229 aml_append(sb_scope, build_iqcr_method(false));
1230
Igor Mammedov12e3b1f2015-12-28 18:02:46 +01001231 aml_append(sb_scope, build_link_dev("LNKA", 0, aml_name("PRQA")));
1232 aml_append(sb_scope, build_link_dev("LNKB", 1, aml_name("PRQB")));
1233 aml_append(sb_scope, build_link_dev("LNKC", 2, aml_name("PRQC")));
1234 aml_append(sb_scope, build_link_dev("LNKD", 3, aml_name("PRQD")));
1235 aml_append(sb_scope, build_link_dev("LNKE", 4, aml_name("PRQE")));
1236 aml_append(sb_scope, build_link_dev("LNKF", 5, aml_name("PRQF")));
1237 aml_append(sb_scope, build_link_dev("LNKG", 6, aml_name("PRQG")));
1238 aml_append(sb_scope, build_link_dev("LNKH", 7, aml_name("PRQH")));
1239
Marcel Apfelbaum6a991e02016-03-13 13:40:29 +02001240 aml_append(sb_scope, build_gsi_link_dev("GSIA", 0x10, 0x10));
1241 aml_append(sb_scope, build_gsi_link_dev("GSIB", 0x11, 0x11));
1242 aml_append(sb_scope, build_gsi_link_dev("GSIC", 0x12, 0x12));
1243 aml_append(sb_scope, build_gsi_link_dev("GSID", 0x13, 0x13));
1244 aml_append(sb_scope, build_gsi_link_dev("GSIE", 0x14, 0x14));
1245 aml_append(sb_scope, build_gsi_link_dev("GSIF", 0x15, 0x15));
1246 aml_append(sb_scope, build_gsi_link_dev("GSIG", 0x16, 0x16));
1247 aml_append(sb_scope, build_gsi_link_dev("GSIH", 0x17, 0x17));
Igor Mammedov80b32df2015-12-28 18:02:45 +01001248
1249 aml_append(table, sb_scope);
1250}
1251
Isaku Yamahatae3fb55f2021-02-17 21:51:15 -08001252static Aml *build_q35_dram_controller(const AcpiMcfgInfo *mcfg)
1253{
1254 Aml *dev;
1255 Aml *resource_template;
1256
1257 /* DRAM controller */
1258 dev = aml_device("DRAC");
1259 aml_append(dev, aml_name_decl("_HID", aml_string("PNP0C01")));
1260
1261 resource_template = aml_resource_template();
1262 if (mcfg->base + mcfg->size - 1 >= (1ULL << 32)) {
1263 aml_append(resource_template,
1264 aml_qword_memory(AML_POS_DECODE,
1265 AML_MIN_FIXED,
1266 AML_MAX_FIXED,
1267 AML_NON_CACHEABLE,
1268 AML_READ_WRITE,
1269 0x0000000000000000,
1270 mcfg->base,
1271 mcfg->base + mcfg->size - 1,
1272 0x0000000000000000,
1273 mcfg->size));
1274 } else {
1275 aml_append(resource_template,
1276 aml_dword_memory(AML_POS_DECODE,
1277 AML_MIN_FIXED,
1278 AML_MAX_FIXED,
1279 AML_NON_CACHEABLE,
1280 AML_READ_WRITE,
1281 0x0000000000000000,
1282 mcfg->base,
1283 mcfg->base + mcfg->size - 1,
1284 0x0000000000000000,
1285 mcfg->size));
1286 }
1287 aml_append(dev, aml_name_decl("_CRS", resource_template));
1288
1289 return dev;
1290}
1291
Julia Suvorovacaf108b2021-07-13 02:42:00 +02001292static void build_x86_acpi_pci_hotplug(Aml *table, uint64_t pcihp_addr)
Igor Mammedovb616ec42015-12-28 18:02:43 +01001293{
1294 Aml *scope;
1295 Aml *field;
1296 Aml *method;
1297
1298 scope = aml_scope("_SB.PCI0");
1299
1300 aml_append(scope,
Julia Suvorovacaf108b2021-07-13 02:42:00 +02001301 aml_operation_region("PCST", AML_SYSTEM_IO, aml_int(pcihp_addr), 0x08));
Igor Mammedovb616ec42015-12-28 18:02:43 +01001302 field = aml_field("PCST", AML_DWORD_ACC, AML_NOLOCK, AML_WRITE_AS_ZEROS);
1303 aml_append(field, aml_named_field("PCIU", 32));
1304 aml_append(field, aml_named_field("PCID", 32));
1305 aml_append(scope, field);
1306
1307 aml_append(scope,
Julia Suvorovacaf108b2021-07-13 02:42:00 +02001308 aml_operation_region("SEJ", AML_SYSTEM_IO,
1309 aml_int(pcihp_addr + ACPI_PCIHP_SEJ_BASE), 0x04));
Igor Mammedovb616ec42015-12-28 18:02:43 +01001310 field = aml_field("SEJ", AML_DWORD_ACC, AML_NOLOCK, AML_WRITE_AS_ZEROS);
1311 aml_append(field, aml_named_field("B0EJ", 32));
1312 aml_append(scope, field);
1313
1314 aml_append(scope,
Julia Suvorovacaf108b2021-07-13 02:42:00 +02001315 aml_operation_region("BNMR", AML_SYSTEM_IO,
1316 aml_int(pcihp_addr + ACPI_PCIHP_BNMR_BASE), 0x08));
Igor Mammedovb616ec42015-12-28 18:02:43 +01001317 field = aml_field("BNMR", AML_DWORD_ACC, AML_NOLOCK, AML_WRITE_AS_ZEROS);
1318 aml_append(field, aml_named_field("BNUM", 32));
Igor Mammedovb32bd762021-03-15 14:00:58 -04001319 aml_append(field, aml_named_field("PIDX", 32));
Igor Mammedovb616ec42015-12-28 18:02:43 +01001320 aml_append(scope, field);
1321
1322 aml_append(scope, aml_mutex("BLCK", 0));
1323
1324 method = aml_method("PCEJ", 2, AML_NOTSERIALIZED);
1325 aml_append(method, aml_acquire(aml_name("BLCK"), 0xFFFF));
1326 aml_append(method, aml_store(aml_arg(0), aml_name("BNUM")));
1327 aml_append(method,
1328 aml_store(aml_shiftleft(aml_int(1), aml_arg(1)), aml_name("B0EJ")));
1329 aml_append(method, aml_release(aml_name("BLCK")));
1330 aml_append(method, aml_return(aml_int(0)));
1331 aml_append(scope, method);
1332
Igor Mammedovb32bd762021-03-15 14:00:58 -04001333 method = aml_method("AIDX", 2, AML_NOTSERIALIZED);
1334 aml_append(method, aml_acquire(aml_name("BLCK"), 0xFFFF));
1335 aml_append(method, aml_store(aml_arg(0), aml_name("BNUM")));
1336 aml_append(method,
1337 aml_store(aml_shiftleft(aml_int(1), aml_arg(1)), aml_name("PIDX")));
1338 aml_append(method, aml_store(aml_name("PIDX"), aml_local(0)));
1339 aml_append(method, aml_release(aml_name("BLCK")));
1340 aml_append(method, aml_return(aml_local(0)));
1341 aml_append(scope, method);
1342
Igor Mammedov5840a162022-07-01 09:35:07 -04001343 aml_append(scope, aml_pci_pdsm());
Igor Mammedovb7f23f62021-03-15 14:01:01 -04001344
Igor Mammedovb616ec42015-12-28 18:02:43 +01001345 aml_append(table, scope);
1346}
1347
Julia Suvorova211afe52021-11-12 06:08:56 -05001348static Aml *build_q35_osc_method(bool enable_native_pcie_hotplug)
Igor Mammedovf97a88a2015-12-28 18:02:54 +01001349{
1350 Aml *if_ctx;
1351 Aml *if_ctx2;
1352 Aml *else_ctx;
1353 Aml *method;
1354 Aml *a_cwd1 = aml_name("CDW1");
Michael S. Tsirkinb3c782d2017-02-28 16:13:28 +02001355 Aml *a_ctrl = aml_local(0);
Igor Mammedovf97a88a2015-12-28 18:02:54 +01001356
1357 method = aml_method("_OSC", 4, AML_NOTSERIALIZED);
1358 aml_append(method, aml_create_dword_field(aml_arg(3), aml_int(0), "CDW1"));
1359
1360 if_ctx = aml_if(aml_equal(
1361 aml_arg(0), aml_touuid("33DB4D5B-1FF7-401C-9657-7441C03DD766")));
1362 aml_append(if_ctx, aml_create_dword_field(aml_arg(3), aml_int(4), "CDW2"));
1363 aml_append(if_ctx, aml_create_dword_field(aml_arg(3), aml_int(8), "CDW3"));
1364
Igor Mammedovf97a88a2015-12-28 18:02:54 +01001365 aml_append(if_ctx, aml_store(aml_name("CDW3"), a_ctrl));
1366
1367 /*
1368 * Always allow native PME, AER (no dependencies)
Aleksandr Bezzubikova41c78c2017-07-29 02:37:49 +03001369 * Allow SHPC (PCI bridges can have SHPC controller)
Julia Suvorova211afe52021-11-12 06:08:56 -05001370 * Disable PCIe Native Hot-plug if ACPI PCI Hot-plug is enabled.
Igor Mammedovf97a88a2015-12-28 18:02:54 +01001371 */
Julia Suvorova211afe52021-11-12 06:08:56 -05001372 aml_append(if_ctx, aml_and(a_ctrl,
1373 aml_int(0x1E | (enable_native_pcie_hotplug ? 0x1 : 0x0)), a_ctrl));
Igor Mammedovf97a88a2015-12-28 18:02:54 +01001374
1375 if_ctx2 = aml_if(aml_lnot(aml_equal(aml_arg(1), aml_int(1))));
1376 /* Unknown revision */
1377 aml_append(if_ctx2, aml_or(a_cwd1, aml_int(0x08), a_cwd1));
1378 aml_append(if_ctx, if_ctx2);
1379
1380 if_ctx2 = aml_if(aml_lnot(aml_equal(aml_name("CDW3"), a_ctrl)));
1381 /* Capabilities bits were masked */
1382 aml_append(if_ctx2, aml_or(a_cwd1, aml_int(0x10), a_cwd1));
1383 aml_append(if_ctx, if_ctx2);
1384
1385 /* Update DWORD3 in the buffer */
1386 aml_append(if_ctx, aml_store(a_ctrl, aml_name("CDW3")));
1387 aml_append(method, if_ctx);
1388
1389 else_ctx = aml_else();
1390 /* Unrecognized UUID */
1391 aml_append(else_ctx, aml_or(a_cwd1, aml_int(4), a_cwd1));
1392 aml_append(method, else_ctx);
1393
1394 aml_append(method, aml_return(aml_arg(3)));
1395 return method;
1396}
Igor Mammedovb616ec42015-12-28 18:02:43 +01001397
Ben Widawsky3d6a69b2022-04-29 15:40:50 +01001398static void build_acpi0017(Aml *table)
1399{
1400 Aml *dev, *scope, *method;
1401
1402 scope = aml_scope("_SB");
1403 dev = aml_device("CXLM");
1404 aml_append(dev, aml_name_decl("_HID", aml_string("ACPI0017")));
1405
1406 method = aml_method("_STA", 0, AML_NOTSERIALIZED);
Jonathan Camerond9ae5802024-01-26 12:01:31 +00001407 aml_append(method, aml_return(aml_int(0x0B)));
Ben Widawsky3d6a69b2022-04-29 15:40:50 +01001408 aml_append(dev, method);
Dave Jiang6cdd46f2023-10-12 13:56:22 +01001409 build_cxl_dsm_method(dev);
Ben Widawsky3d6a69b2022-04-29 15:40:50 +01001410
1411 aml_append(scope, dev);
1412 aml_append(table, scope);
1413}
1414
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001415static void
Igor Mammedov0e9b9ed2016-05-19 15:19:25 +02001416build_dsdt(GArray *table_data, BIOSLinker *linker,
Igor Mammedovadcb89d2016-02-26 14:59:26 +01001417 AcpiPmInfo *pm, AcpiMiscInfo *misc,
Markus Armbruster01c97422016-06-15 19:56:31 +02001418 Range *pci_hole, Range *pci_hole64, MachineState *machine)
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001419{
Paolo Bonziniee3b34c2024-02-23 13:43:57 +01001420 Object *i440fx = object_resolve_type_unambiguous(TYPE_I440FX_PCI_HOST_BRIDGE,
1421 NULL);
1422 Object *q35 = object_resolve_type_unambiguous(TYPE_Q35_HOST_DEVICE, NULL);
Igor Mammedov41fa5c02016-01-22 15:36:06 +01001423 CrsRangeEntry *entry;
1424 Aml *dsdt, *sb_scope, *scope, *dev, *method, *field, *pkg, *crs;
Marcel Apfelbaum2df5a7b2016-07-17 19:53:12 +03001425 CrsRangeSet crs_range_set;
Eduardo Habkostfb306ff2015-12-11 16:42:26 -02001426 PCMachineState *pcms = PC_MACHINE(machine);
Igor Mammedov679dd1a2016-06-15 11:25:23 +02001427 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(machine);
Paolo Bonzinif0bb2762019-10-22 09:39:50 +02001428 X86MachineState *x86ms = X86_MACHINE(machine);
Gerd Hoffmann4a441832019-06-07 09:34:29 +02001429 AcpiMcfgInfo mcfg;
Isaku Yamahatae3fb55f2021-02-17 21:51:15 -08001430 bool mcfg_valid = !!acpi_get_mcfg(&mcfg);
Igor Mammedovbef34922014-06-02 15:25:26 +02001431 uint32_t nr_mem = machine->ram_slots;
Marcel Apfelbaumdcdca292015-06-02 14:23:04 +03001432 int root_bus_limit = 0xFF;
Igor Mammedov41fa5c02016-01-22 15:36:06 +01001433 PCIBus *bus = NULL;
Stefan Berger11fb99e2021-06-15 16:21:17 +02001434#ifdef CONFIG_TPM
Stefan Bergerac6dd312019-01-15 02:27:52 +04001435 TPMIf *tpm = tpm_find();
Stefan Berger11fb99e2021-06-15 16:21:17 +02001436#endif
Ben Widawsky3d6a69b2022-04-29 15:40:50 +01001437 bool cxl_present = false;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001438 int i;
Jon Doron8f814ea2020-07-15 11:43:25 +03001439 VMBusBridge *vmbus_bridge = vmbus_bridge_find();
Igor Mammedov5c142bc2021-09-24 08:27:40 -04001440 AcpiTable table = { .sig = "DSDT", .rev = 1, .oem_id = x86ms->oem_id,
1441 .oem_table_id = x86ms->oem_table_id };
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001442
Bernhard Beschowb496a172022-10-28 12:34:19 +02001443 assert(!!i440fx != !!q35);
Bernhard Beschowbbaa5c42022-10-28 12:34:18 +02001444
Igor Mammedov5c142bc2021-09-24 08:27:40 -04001445 acpi_table_begin(&table, table_data);
Igor Mammedov41fa5c02016-01-22 15:36:06 +01001446 dsdt = init_aml_allocator();
Laszlo Ersek2fd71f12014-03-17 17:05:17 +01001447
Igor Mammedov41fa5c02016-01-22 15:36:06 +01001448 build_dbg_aml(dsdt);
Bernhard Beschowb496a172022-10-28 12:34:19 +02001449 if (i440fx) {
Igor Mammedov41fa5c02016-01-22 15:36:06 +01001450 sb_scope = aml_scope("_SB");
1451 dev = aml_device("PCI0");
1452 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A03")));
Vitaly Cheptsov0a343a52021-03-01 22:59:18 +03001453 aml_append(dev, aml_name_decl("_UID", aml_int(pcmc->pci_root_uid)));
Igor Mammedovfe0d5f52023-03-02 17:15:26 +01001454 aml_append(dev, aml_pci_edsm());
Igor Mammedov41fa5c02016-01-22 15:36:06 +01001455 aml_append(sb_scope, dev);
1456 aml_append(dsdt, sb_scope);
1457
Ani Sinhadf4008c2020-09-18 14:11:08 +05301458 if (pm->pcihp_bridge_en || pm->pcihp_root_en) {
Julia Suvorovacaf108b2021-07-13 02:42:00 +02001459 build_x86_acpi_pci_hotplug(dsdt, pm->pcihp_io_base);
Ani Sinhadf4008c2020-09-18 14:11:08 +05301460 }
Igor Mammedov41fa5c02016-01-22 15:36:06 +01001461 build_piix4_pci0_int(dsdt);
Bernhard Beschowb496a172022-10-28 12:34:19 +02001462 } else if (q35) {
Igor Mammedov41fa5c02016-01-22 15:36:06 +01001463 sb_scope = aml_scope("_SB");
Igor Mammedov41fa5c02016-01-22 15:36:06 +01001464 dev = aml_device("PCI0");
1465 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A08")));
1466 aml_append(dev, aml_name_decl("_CID", aml_eisaid("PNP0A03")));
Vitaly Cheptsov0a343a52021-03-01 22:59:18 +03001467 aml_append(dev, aml_name_decl("_UID", aml_int(pcmc->pci_root_uid)));
Julia Suvorova211afe52021-11-12 06:08:56 -05001468 aml_append(dev, build_q35_osc_method(!pm->pcihp_bridge_en));
Igor Mammedovfe0d5f52023-03-02 17:15:26 +01001469 aml_append(dev, aml_pci_edsm());
Igor Mammedov41fa5c02016-01-22 15:36:06 +01001470 aml_append(sb_scope, dev);
Isaku Yamahatae3fb55f2021-02-17 21:51:15 -08001471 if (mcfg_valid) {
1472 aml_append(sb_scope, build_q35_dram_controller(&mcfg));
1473 }
Igor Mammedov998ba952020-09-23 05:46:47 -04001474
1475 if (pm->smi_on_cpuhp) {
1476 /* reserve SMI block resources, IO ports 0xB2, 0xB3 */
1477 dev = aml_device("PCI0.SMI0");
1478 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A06")));
1479 aml_append(dev, aml_name_decl("_UID", aml_string("SMI resources")));
1480 crs = aml_resource_template();
1481 aml_append(crs,
1482 aml_io(
1483 AML_DECODE16,
Bernhard Beschow5cdb6392023-09-08 10:42:32 +02001484 pm->fadt.smi_cmd,
1485 pm->fadt.smi_cmd,
Igor Mammedov998ba952020-09-23 05:46:47 -04001486 1,
1487 2)
1488 );
1489 aml_append(dev, aml_name_decl("_CRS", crs));
1490 aml_append(dev, aml_operation_region("SMIR", AML_SYSTEM_IO,
Bernhard Beschow5cdb6392023-09-08 10:42:32 +02001491 aml_int(pm->fadt.smi_cmd), 2));
Igor Mammedov998ba952020-09-23 05:46:47 -04001492 field = aml_field("SMIR", AML_BYTE_ACC, AML_NOLOCK,
1493 AML_WRITE_AS_ZEROS);
1494 aml_append(field, aml_named_field("SMIC", 8));
1495 aml_append(field, aml_reserved_field(8));
1496 aml_append(dev, field);
1497 aml_append(sb_scope, dev);
1498 }
1499
Igor Mammedov41fa5c02016-01-22 15:36:06 +01001500 aml_append(dsdt, sb_scope);
1501
Julia Suvorovacaf108b2021-07-13 02:42:00 +02001502 if (pm->pcihp_bridge_en) {
1503 build_x86_acpi_pci_hotplug(dsdt, pm->pcihp_io_base);
1504 }
Igor Mammedov41fa5c02016-01-22 15:36:06 +01001505 build_q35_pci0_int(dsdt);
1506 }
1507
Igor Mammedove05acc32022-07-01 09:35:00 -04001508 if (misc->has_hpet) {
1509 build_hpet_aml(dsdt);
1510 }
1511
Jon Doron8f814ea2020-07-15 11:43:25 +03001512 if (vmbus_bridge) {
1513 sb_scope = aml_scope("_SB");
1514 aml_append(sb_scope, build_vmbus_device_aml(vmbus_bridge));
1515 aml_append(dsdt, sb_scope);
1516 }
1517
Igor Mammedovd12dbd42022-10-17 12:21:45 +02001518 scope = aml_scope("_GPE");
1519 {
1520 aml_append(scope, aml_name_decl("_HID", aml_string("ACPI0006")));
1521 if (machine->nvdimms_state->is_enabled) {
1522 method = aml_method("_E04", 0, AML_NOTSERIALIZED);
1523 aml_append(method, aml_notify(aml_name("\\_SB.NVDR"),
1524 aml_int(0x80)));
1525 aml_append(scope, method);
1526 }
1527 }
1528 aml_append(dsdt, scope);
1529
Igor Mammedov679dd1a2016-06-15 11:25:23 +02001530 if (pcmc->legacy_cpu_hotplug) {
1531 build_legacy_cpu_hotplug_aml(dsdt, machine, pm->cpu_hp_io_base);
1532 } else {
1533 CPUHotplugFeatures opts = {
Igor Mammedov998ba952020-09-23 05:46:47 -04001534 .acpi_1_compatible = true, .has_legacy_cphp = true,
1535 .smi_path = pm->smi_on_cpuhp ? "\\_SB.PCI0.SMI0.SMIC" : NULL,
Igor Mammedov69dea9d2020-12-07 09:07:36 -05001536 .fw_unplugs_cpu = pm->smi_on_cpu_unplug,
Igor Mammedov679dd1a2016-06-15 11:25:23 +02001537 };
Bernhard Beschow9a4fedc2023-09-08 10:42:28 +02001538 build_cpus_aml(dsdt, machine, opts, pc_madt_cpu_entry,
1539 pm->cpu_hp_io_base, "\\_SB.PCI0", "\\_GPE._E02");
Igor Mammedov679dd1a2016-06-15 11:25:23 +02001540 }
Shameer Kolothum091c4662019-09-18 14:06:23 +01001541
1542 if (pcms->memhp_io_base && nr_mem) {
1543 build_memory_hotplug_aml(dsdt, nr_mem, "\\_SB.PCI0",
1544 "\\_GPE._E03", AML_SYSTEM_IO,
1545 pcms->memhp_io_base);
1546 }
Igor Mammedov41fa5c02016-01-22 15:36:06 +01001547
Marcel Apfelbaum2df5a7b2016-07-17 19:53:12 +03001548 crs_range_set_init(&crs_range_set);
Bernhard Beschowb54a9d42024-02-24 14:58:47 +01001549 bus = PC_MACHINE(machine)->pcibus;
Marcel Apfelbauma4894202015-06-02 14:23:01 +03001550 if (bus) {
1551 QLIST_FOREACH(bus, &bus->child, sibling) {
1552 uint8_t bus_num = pci_bus_num(bus);
Marcel Apfelbaum0e79e512015-06-02 14:23:10 +03001553 uint8_t numa_node = pci_bus_numa_node(bus);
Marcel Apfelbauma4894202015-06-02 14:23:01 +03001554
1555 /* look only for expander root buses */
1556 if (!pci_bus_is_root(bus)) {
1557 continue;
1558 }
1559
Marcel Apfelbaumdcdca292015-06-02 14:23:04 +03001560 if (bus_num < root_bus_limit) {
1561 root_bus_limit = bus_num - 1;
1562 }
1563
Marcel Apfelbauma4894202015-06-02 14:23:01 +03001564 scope = aml_scope("\\_SB");
Ben Widawsky6e4e3ae2022-04-29 15:40:48 +01001565
1566 if (pci_bus_is_cxl(bus)) {
1567 dev = aml_device("CL%.02X", bus_num);
1568 } else {
1569 dev = aml_device("PC%.02X", bus_num);
1570 }
Laszlo Ersekc96d9282015-06-11 02:37:58 +02001571 aml_append(dev, aml_name_decl("_UID", aml_int(bus_num)));
Marcel Apfelbauma4894202015-06-02 14:23:01 +03001572 aml_append(dev, aml_name_decl("_BBN", aml_int(bus_num)));
Ben Widawsky6e4e3ae2022-04-29 15:40:48 +01001573 if (pci_bus_is_cxl(bus)) {
Ani Sinha7b393b72023-09-22 18:12:02 +05301574 struct Aml *aml_pkg = aml_package(2);
Ben Widawsky6e4e3ae2022-04-29 15:40:48 +01001575
Ben Widawsky2a3282c2022-04-29 15:40:49 +01001576 aml_append(dev, aml_name_decl("_HID", aml_string("ACPI0016")));
Ani Sinha7b393b72023-09-22 18:12:02 +05301577 aml_append(aml_pkg, aml_eisaid("PNP0A08"));
1578 aml_append(aml_pkg, aml_eisaid("PNP0A03"));
1579 aml_append(dev, aml_name_decl("_CID", aml_pkg));
Ben Widawsky2a3282c2022-04-29 15:40:49 +01001580 build_cxl_osc_method(dev);
Ben Widawsky6e4e3ae2022-04-29 15:40:48 +01001581 } else if (pci_bus_is_express(bus)) {
Evgeny Yakovlevee4b0c82019-07-19 11:54:29 +03001582 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A08")));
1583 aml_append(dev, aml_name_decl("_CID", aml_eisaid("PNP0A03")));
Julia Suvorova211afe52021-11-12 06:08:56 -05001584
1585 /* Expander bridges do not have ACPI PCI Hot-plug enabled */
1586 aml_append(dev, build_q35_osc_method(true));
Evgeny Yakovlevee4b0c82019-07-19 11:54:29 +03001587 } else {
1588 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A03")));
Marcel Apfelbaum077dd742017-02-28 16:13:29 +02001589 }
Marcel Apfelbaum0e79e512015-06-02 14:23:10 +03001590
1591 if (numa_node != NUMA_NODE_UNASSIGNED) {
1592 aml_append(dev, aml_name_decl("_PXM", aml_int(numa_node)));
1593 }
1594
Igor Mammedov196e2132015-12-28 18:02:42 +01001595 aml_append(dev, build_prt(false));
Jiahui Cene41ee852021-01-14 18:06:38 +08001596 crs = build_crs(PCI_HOST_BRIDGE(BUS(bus)->parent), &crs_range_set,
1597 0, 0, 0, 0);
Marcel Apfelbauma43c6e22015-06-02 14:23:03 +03001598 aml_append(dev, aml_name_decl("_CRS", crs));
Marcel Apfelbauma4894202015-06-02 14:23:01 +03001599 aml_append(scope, dev);
Igor Mammedov41fa5c02016-01-22 15:36:06 +01001600 aml_append(dsdt, scope);
Ben Widawsky6e4e3ae2022-04-29 15:40:48 +01001601
1602 /* Handle the ranges for the PXB expanders */
1603 if (pci_bus_is_cxl(bus)) {
Jonathan Cameron1ebf9002022-06-08 15:54:39 +01001604 MemoryRegion *mr = &pcms->cxl_devices_state.host_mr;
Ben Widawsky6e4e3ae2022-04-29 15:40:48 +01001605 uint64_t base = mr->addr;
1606
Ben Widawsky3d6a69b2022-04-29 15:40:50 +01001607 cxl_present = true;
Ben Widawsky6e4e3ae2022-04-29 15:40:48 +01001608 crs_range_insert(crs_range_set.mem_ranges, base,
1609 base + memory_region_size(mr) - 1);
1610 }
Marcel Apfelbauma4894202015-06-02 14:23:01 +03001611 }
1612 }
1613
Ben Widawsky3d6a69b2022-04-29 15:40:50 +01001614 if (cxl_present) {
1615 build_acpi0017(dsdt);
1616 }
1617
Gerd Hoffmann4a441832019-06-07 09:34:29 +02001618 /*
1619 * At this point crs_range_set has all the ranges used by pci
1620 * busses *other* than PCI0. These ranges will be excluded from
1621 * the PCI0._CRS. Add mmconfig to the set so it will be excluded
1622 * too.
1623 */
Isaku Yamahatae3fb55f2021-02-17 21:51:15 -08001624 if (mcfg_valid) {
Gerd Hoffmann4a441832019-06-07 09:34:29 +02001625 crs_range_insert(crs_range_set.mem_ranges,
1626 mcfg.base, mcfg.base + mcfg.size - 1);
1627 }
1628
Igor Mammedov500b11e2015-02-18 19:14:50 +00001629 scope = aml_scope("\\_SB.PCI0");
Igor Mammedov60efd422015-02-20 18:22:05 +00001630 /* build PCI0._CRS */
1631 crs = aml_resource_template();
1632 aml_append(crs,
Shannon Zhaoff80dc72015-05-29 11:28:54 +01001633 aml_word_bus_number(AML_MIN_FIXED, AML_MAX_FIXED, AML_POS_DECODE,
Marcel Apfelbaumdcdca292015-06-02 14:23:04 +03001634 0x0000, 0x0, root_bus_limit,
1635 0x0000, root_bus_limit + 1));
Shannon Zhaoff80dc72015-05-29 11:28:54 +01001636 aml_append(crs, aml_io(AML_DECODE16, 0x0CF8, 0x0CF8, 0x01, 0x08));
Igor Mammedov60efd422015-02-20 18:22:05 +00001637
1638 aml_append(crs,
Shannon Zhaoff80dc72015-05-29 11:28:54 +01001639 aml_word_io(AML_MIN_FIXED, AML_MAX_FIXED,
1640 AML_POS_DECODE, AML_ENTIRE_RANGE,
Igor Mammedov60efd422015-02-20 18:22:05 +00001641 0x0000, 0x0000, 0x0CF7, 0x0000, 0x0CF8));
Marcel Apfelbaumdcdca292015-06-02 14:23:04 +03001642
Marcel Apfelbaum2df5a7b2016-07-17 19:53:12 +03001643 crs_replace_with_free_ranges(crs_range_set.io_ranges, 0x0D00, 0xFFFF);
1644 for (i = 0; i < crs_range_set.io_ranges->len; i++) {
1645 entry = g_ptr_array_index(crs_range_set.io_ranges, i);
Marcel Apfelbaumdcdca292015-06-02 14:23:04 +03001646 aml_append(crs,
1647 aml_word_io(AML_MIN_FIXED, AML_MAX_FIXED,
1648 AML_POS_DECODE, AML_ENTIRE_RANGE,
1649 0x0000, entry->base, entry->limit,
1650 0x0000, entry->limit - entry->base + 1));
1651 }
1652
Igor Mammedov60efd422015-02-20 18:22:05 +00001653 aml_append(crs,
Shannon Zhaoff80dc72015-05-29 11:28:54 +01001654 aml_dword_memory(AML_POS_DECODE, AML_MIN_FIXED, AML_MAX_FIXED,
1655 AML_CACHEABLE, AML_READ_WRITE,
Igor Mammedov60efd422015-02-20 18:22:05 +00001656 0, 0x000A0000, 0x000BFFFF, 0, 0x00020000));
Marcel Apfelbaumdcdca292015-06-02 14:23:04 +03001657
Marcel Apfelbaum2df5a7b2016-07-17 19:53:12 +03001658 crs_replace_with_free_ranges(crs_range_set.mem_ranges,
Markus Armbrustera0efbf12016-07-01 13:47:47 +02001659 range_lob(pci_hole),
1660 range_upb(pci_hole));
Marcel Apfelbaum2df5a7b2016-07-17 19:53:12 +03001661 for (i = 0; i < crs_range_set.mem_ranges->len; i++) {
1662 entry = g_ptr_array_index(crs_range_set.mem_ranges, i);
Marcel Apfelbaumdcdca292015-06-02 14:23:04 +03001663 aml_append(crs,
1664 aml_dword_memory(AML_POS_DECODE, AML_MIN_FIXED, AML_MAX_FIXED,
1665 AML_NON_CACHEABLE, AML_READ_WRITE,
1666 0, entry->base, entry->limit,
1667 0, entry->limit - entry->base + 1));
1668 }
1669
Markus Armbrustera0efbf12016-07-01 13:47:47 +02001670 if (!range_is_empty(pci_hole64)) {
Marcel Apfelbaum16de88a2016-07-17 19:53:13 +03001671 crs_replace_with_free_ranges(crs_range_set.mem_64bit_ranges,
1672 range_lob(pci_hole64),
1673 range_upb(pci_hole64));
1674 for (i = 0; i < crs_range_set.mem_64bit_ranges->len; i++) {
1675 entry = g_ptr_array_index(crs_range_set.mem_64bit_ranges, i);
1676 aml_append(crs,
1677 aml_qword_memory(AML_POS_DECODE, AML_MIN_FIXED,
1678 AML_MAX_FIXED,
1679 AML_CACHEABLE, AML_READ_WRITE,
1680 0, entry->base, entry->limit,
1681 0, entry->limit - entry->base + 1));
1682 }
Igor Mammedov60efd422015-02-20 18:22:05 +00001683 }
Igor Mammedov2b1c2e82016-04-08 13:23:13 +02001684
Stefan Berger11fb99e2021-06-15 16:21:17 +02001685#ifdef CONFIG_TPM
Eric Auger43bc7f82020-03-05 17:51:40 +01001686 if (TPM_IS_TIS_ISA(tpm_find())) {
Igor Mammedov2b1c2e82016-04-08 13:23:13 +02001687 aml_append(crs, aml_memory32_fixed(TPM_TIS_ADDR_BASE,
1688 TPM_TIS_ADDR_SIZE, AML_READ_WRITE));
1689 }
Stefan Berger11fb99e2021-06-15 16:21:17 +02001690#endif
Igor Mammedov60efd422015-02-20 18:22:05 +00001691 aml_append(scope, aml_name_decl("_CRS", crs));
1692
Igor Mammedovd31c9092015-02-20 18:22:08 +00001693 /* reserve GPE0 block resources */
1694 dev = aml_device("GPE0");
1695 aml_append(dev, aml_name_decl("_HID", aml_string("PNP0A06")));
1696 aml_append(dev, aml_name_decl("_UID", aml_string("GPE0 resources")));
1697 /* device present, functioning, decoding, not shown in UI */
1698 aml_append(dev, aml_name_decl("_STA", aml_int(0xB)));
1699 crs = aml_resource_template();
1700 aml_append(crs,
Igor Mammedov937d1b52018-02-28 15:23:51 +01001701 aml_io(
1702 AML_DECODE16,
1703 pm->fadt.gpe0_blk.address,
1704 pm->fadt.gpe0_blk.address,
1705 1,
1706 pm->fadt.gpe0_blk.bit_width / 8)
Igor Mammedovd31c9092015-02-20 18:22:08 +00001707 );
1708 aml_append(dev, aml_name_decl("_CRS", crs));
1709 aml_append(scope, dev);
1710
Marcel Apfelbaum2df5a7b2016-07-17 19:53:12 +03001711 crs_range_set_free(&crs_range_set);
Marcel Apfelbaumdcdca292015-06-02 14:23:04 +03001712
Igor Mammedov500b11e2015-02-18 19:14:50 +00001713 /* reserve PCIHP resources */
Ani Sinhadf4008c2020-09-18 14:11:08 +05301714 if (pm->pcihp_io_len && (pm->pcihp_bridge_en || pm->pcihp_root_en)) {
Igor Mammedov500b11e2015-02-18 19:14:50 +00001715 dev = aml_device("PHPR");
1716 aml_append(dev, aml_name_decl("_HID", aml_string("PNP0A06")));
1717 aml_append(dev,
1718 aml_name_decl("_UID", aml_string("PCI Hotplug resources")));
1719 /* device present, functioning, decoding, not shown in UI */
1720 aml_append(dev, aml_name_decl("_STA", aml_int(0xB)));
1721 crs = aml_resource_template();
1722 aml_append(crs,
Shannon Zhaoff80dc72015-05-29 11:28:54 +01001723 aml_io(AML_DECODE16, pm->pcihp_io_base, pm->pcihp_io_base, 1,
Igor Mammedov500b11e2015-02-18 19:14:50 +00001724 pm->pcihp_io_len)
1725 );
1726 aml_append(dev, aml_name_decl("_CRS", crs));
1727 aml_append(scope, dev);
1728 }
Igor Mammedov41fa5c02016-01-22 15:36:06 +01001729 aml_append(dsdt, scope);
Igor Mammedov500b11e2015-02-18 19:14:50 +00001730
Igor Mammedovebc30282015-02-18 19:14:29 +00001731 /* create S3_ / S4_ / S5_ packages if necessary */
1732 scope = aml_scope("\\");
1733 if (!pm->s3_disabled) {
1734 pkg = aml_package(4);
1735 aml_append(pkg, aml_int(1)); /* PM1a_CNT.SLP_TYP */
1736 aml_append(pkg, aml_int(1)); /* PM1b_CNT.SLP_TYP, FIXME: not impl. */
1737 aml_append(pkg, aml_int(0)); /* reserved */
1738 aml_append(pkg, aml_int(0)); /* reserved */
1739 aml_append(scope, aml_name_decl("_S3", pkg));
1740 }
1741
1742 if (!pm->s4_disabled) {
1743 pkg = aml_package(4);
1744 aml_append(pkg, aml_int(pm->s4_val)); /* PM1a_CNT.SLP_TYP */
1745 /* PM1b_CNT.SLP_TYP, FIXME: not impl. */
1746 aml_append(pkg, aml_int(pm->s4_val));
1747 aml_append(pkg, aml_int(0)); /* reserved */
1748 aml_append(pkg, aml_int(0)); /* reserved */
1749 aml_append(scope, aml_name_decl("_S4", pkg));
1750 }
1751
1752 pkg = aml_package(4);
1753 aml_append(pkg, aml_int(0)); /* PM1a_CNT.SLP_TYP */
1754 aml_append(pkg, aml_int(0)); /* PM1b_CNT.SLP_TYP not impl. */
1755 aml_append(pkg, aml_int(0)); /* reserved */
1756 aml_append(pkg, aml_int(0)); /* reserved */
1757 aml_append(scope, aml_name_decl("_S5", pkg));
Igor Mammedov41fa5c02016-01-22 15:36:06 +01001758 aml_append(dsdt, scope);
Igor Mammedovebc30282015-02-18 19:14:29 +00001759
Gabriel L. Somloe2ec7562016-02-19 13:20:27 -05001760 /* create fw_cfg node, unconditionally */
1761 {
Gabriel L. Somloe2ec7562016-02-19 13:20:27 -05001762 scope = aml_scope("\\_SB.PCI0");
Gerd Hoffmann0575c2f2020-06-19 11:19:00 +02001763 fw_cfg_add_acpi_dsdt(scope, x86ms->fw_cfg);
Gabriel L. Somloe2ec7562016-02-19 13:20:27 -05001764 aml_append(dsdt, scope);
1765 }
1766
Gal Hammer7824df32015-04-21 11:26:12 +03001767 sb_scope = aml_scope("\\_SB");
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001768 {
Igor Mammedovd3ecb222022-06-08 09:53:37 -04001769 Object *pci_host = acpi_get_i386_pci_host();
Julia Suvorovac0e427d2021-07-13 02:42:01 +02001770
Igor Mammedov8b35ab22016-12-06 00:32:25 +01001771 if (pci_host) {
Ani Sinha7b393b72023-09-22 18:12:02 +05301772 PCIBus *pbus = PCI_HOST_BRIDGE(pci_host)->bus;
1773 Aml *ascope = aml_scope("PCI0");
Igor Mammedov8b35ab22016-12-06 00:32:25 +01001774 /* Scan all PCI buses. Generate tables to support hotplug. */
Ani Sinha7b393b72023-09-22 18:12:02 +05301775 build_append_pci_bus_devices(ascope, pbus);
1776 if (object_property_find(OBJECT(pbus), ACPI_PCIHP_PROP_BSEL)) {
1777 build_append_pcihp_slots(ascope, pbus);
Igor Mammedov02c10612023-03-02 17:15:41 +01001778 }
Ani Sinha7b393b72023-09-22 18:12:02 +05301779 aml_append(sb_scope, ascope);
Igor Mammedov8b35ab22016-12-06 00:32:25 +01001780 }
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001781 }
Marc-André Lureau4ab6cb42018-01-29 19:33:07 +01001782
Stefan Berger11fb99e2021-06-15 16:21:17 +02001783#ifdef CONFIG_TPM
Stefan Bergerac6dd312019-01-15 02:27:52 +04001784 if (TPM_IS_CRB(tpm)) {
Marc-André Lureau4ab6cb42018-01-29 19:33:07 +01001785 dev = aml_device("TPM");
1786 aml_append(dev, aml_name_decl("_HID", aml_string("MSFT0101")));
Stefan Berger59036462022-01-04 12:58:05 -05001787 aml_append(dev, aml_name_decl("_STR",
1788 aml_string("TPM 2.0 Device")));
Marc-André Lureau4ab6cb42018-01-29 19:33:07 +01001789 crs = aml_resource_template();
1790 aml_append(crs, aml_memory32_fixed(TPM_CRB_ADDR_BASE,
1791 TPM_CRB_ADDR_SIZE, AML_READ_WRITE));
1792 aml_append(dev, aml_name_decl("_CRS", crs));
1793
Gerd Hoffmann88b36482020-04-29 15:59:52 +02001794 aml_append(dev, aml_name_decl("_STA", aml_int(0xf)));
Stefan Berger59036462022-01-04 12:58:05 -05001795 aml_append(dev, aml_name_decl("_UID", aml_int(1)));
Marc-André Lureau4ab6cb42018-01-29 19:33:07 +01001796
Stefan Bergerac6dd312019-01-15 02:27:52 +04001797 tpm_build_ppi_acpi(tpm, dev);
1798
Marc-André Lureau4ab6cb42018-01-29 19:33:07 +01001799 aml_append(sb_scope, dev);
1800 }
Stefan Berger11fb99e2021-06-15 16:21:17 +02001801#endif
Marc-André Lureau4ab6cb42018-01-29 19:33:07 +01001802
Sean Christophersonc8a98992021-07-19 19:21:22 +08001803 if (pcms->sgx_epc.size != 0) {
1804 uint64_t epc_base = pcms->sgx_epc.base;
1805 uint64_t epc_size = pcms->sgx_epc.size;
1806
1807 dev = aml_device("EPC");
1808 aml_append(dev, aml_name_decl("_HID", aml_eisaid("INT0E0C")));
1809 aml_append(dev, aml_name_decl("_STR",
1810 aml_unicode("Enclave Page Cache 1.0")));
1811 crs = aml_resource_template();
1812 aml_append(crs,
1813 aml_qword_memory(AML_POS_DECODE, AML_MIN_FIXED,
1814 AML_MAX_FIXED, AML_NON_CACHEABLE,
1815 AML_READ_WRITE, 0, epc_base,
1816 epc_base + epc_size - 1, 0, epc_size));
1817 aml_append(dev, aml_name_decl("_CRS", crs));
1818
1819 method = aml_method("_STA", 0, AML_NOTSERIALIZED);
1820 aml_append(method, aml_return(aml_int(0x0f)));
1821 aml_append(dev, method);
1822
1823 aml_append(sb_scope, dev);
1824 }
Igor Mammedov8b35ab22016-12-06 00:32:25 +01001825 aml_append(dsdt, sb_scope);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001826
Igor Mammedovd12dbd42022-10-17 12:21:45 +02001827 if (pm->pcihp_bridge_en || pm->pcihp_root_en) {
Igor Mammedov219e6382023-01-12 15:02:54 +01001828 bool has_pcnt;
1829
Igor Mammedovddab4d32023-01-12 15:02:53 +01001830 Object *pci_host = acpi_get_i386_pci_host();
Ani Sinha7b393b72023-09-22 18:12:02 +05301831 PCIBus *b = PCI_HOST_BRIDGE(pci_host)->bus;
Igor Mammedovddab4d32023-01-12 15:02:53 +01001832
1833 scope = aml_scope("\\_SB.PCI0");
Ani Sinha7b393b72023-09-22 18:12:02 +05301834 has_pcnt = build_append_notfication_callback(scope, b);
Igor Mammedov219e6382023-01-12 15:02:54 +01001835 if (has_pcnt) {
1836 aml_append(dsdt, scope);
1837 }
Igor Mammedovddab4d32023-01-12 15:02:53 +01001838
Igor Mammedovd12dbd42022-10-17 12:21:45 +02001839 scope = aml_scope("_GPE");
1840 {
1841 method = aml_method("_E01", 0, AML_NOTSERIALIZED);
Igor Mammedov219e6382023-01-12 15:02:54 +01001842 if (has_pcnt) {
1843 aml_append(method,
1844 aml_acquire(aml_name("\\_SB.PCI0.BLCK"), 0xFFFF));
1845 aml_append(method, aml_call0("\\_SB.PCI0.PCNT"));
1846 aml_append(method, aml_release(aml_name("\\_SB.PCI0.BLCK")));
1847 }
Igor Mammedovd12dbd42022-10-17 12:21:45 +02001848 aml_append(scope, method);
1849 }
1850 aml_append(dsdt, scope);
1851 }
1852
Igor Mammedov011bb742015-02-18 19:14:16 +00001853 /* copy AML table into ACPI tables blob and patch header there */
Igor Mammedov41fa5c02016-01-22 15:36:06 +01001854 g_array_append_vals(table_data, dsdt->buf->data, dsdt->buf->len);
Igor Mammedov5c142bc2021-09-24 08:27:40 -04001855 acpi_table_end(linker, &table);
Igor Mammedov011bb742015-02-18 19:14:16 +00001856 free_aml_allocator();
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001857}
1858
Igor Mammedov43dde172021-09-24 08:27:41 -04001859/*
1860 * IA-PC HPET (High Precision Event Timers) Specification (Revision: 1.0a)
1861 * 3.2.4The ACPI 2.0 HPET Description Table (HPET)
1862 */
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001863static void
Marian Postevca602b4582021-01-19 02:32:13 +02001864build_hpet(GArray *table_data, BIOSLinker *linker, const char *oem_id,
1865 const char *oem_table_id)
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001866{
Igor Mammedov43dde172021-09-24 08:27:41 -04001867 AcpiTable table = { .sig = "HPET", .rev = 1,
1868 .oem_id = oem_id, .oem_table_id = oem_table_id };
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001869
Igor Mammedov43dde172021-09-24 08:27:41 -04001870 acpi_table_begin(&table, table_data);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001871 /* Note timer_block_id value must be kept in sync with value advertised by
1872 * emulated hpet
1873 */
Igor Mammedov43dde172021-09-24 08:27:41 -04001874 /* Event Timer Block ID */
1875 build_append_int_noprefix(table_data, 0x8086a201, 4);
1876 /* BASE_ADDRESS */
1877 build_append_gas(table_data, AML_AS_SYSTEM_MEMORY, 0, 0, 0, HPET_BASE);
1878 /* HPET Number */
1879 build_append_int_noprefix(table_data, 0, 1);
1880 /* Main Counter Minimum Clock_tick in Periodic Mode */
1881 build_append_int_noprefix(table_data, 0, 2);
1882 /* Page Protection And OEM Attribute */
1883 build_append_int_noprefix(table_data, 0, 1);
1884 acpi_table_end(linker, &table);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001885}
1886
Stefan Berger11fb99e2021-06-15 16:21:17 +02001887#ifdef CONFIG_TPM
Igor Mammedov57cb8cf2021-09-24 08:27:42 -04001888/*
1889 * TCPA Description Table
1890 *
1891 * Following Level 00, Rev 00.37 of specs:
1892 * http://www.trustedcomputinggroup.org/resources/tcg_acpi_specification
1893 * 7.1.2 ACPI Table Layout
1894 */
Stefan Berger711b20b2014-08-11 16:33:36 -04001895static void
Marian Postevca602b4582021-01-19 02:32:13 +02001896build_tpm_tcpa(GArray *table_data, BIOSLinker *linker, GArray *tcpalog,
1897 const char *oem_id, const char *oem_table_id)
Stefan Berger711b20b2014-08-11 16:33:36 -04001898{
Igor Mammedov57cb8cf2021-09-24 08:27:42 -04001899 unsigned log_addr_offset;
1900 AcpiTable table = { .sig = "TCPA", .rev = 2,
1901 .oem_id = oem_id, .oem_table_id = oem_table_id };
Stefan Berger711b20b2014-08-11 16:33:36 -04001902
Igor Mammedov57cb8cf2021-09-24 08:27:42 -04001903 acpi_table_begin(&table, table_data);
1904 /* Platform Class */
1905 build_append_int_noprefix(table_data, TPM_TCPA_ACPI_CLASS_CLIENT, 2);
1906 /* Log Area Minimum Length (LAML) */
1907 build_append_int_noprefix(table_data, TPM_LOG_AREA_MINIMUM_SIZE, 4);
1908 /* Log Area Start Address (LASA) */
1909 log_addr_offset = table_data->len;
1910 build_append_int_noprefix(table_data, 0, 8);
Stefan Berger711b20b2014-08-11 16:33:36 -04001911
Igor Mammedov57cb8cf2021-09-24 08:27:42 -04001912 /* allocate/reserve space for TPM log area */
1913 acpi_data_push(tcpalog, TPM_LOG_AREA_MINIMUM_SIZE);
Igor Mammedovad9671b2016-05-19 15:19:26 +02001914 bios_linker_loader_alloc(linker, ACPI_BUILD_TPMLOG_FILE, tcpalog, 1,
Stefan Berger42a5b302014-10-24 13:21:04 -04001915 false /* high memory */);
Stefan Berger711b20b2014-08-11 16:33:36 -04001916 /* log area start address to be filled by Guest linker */
Igor Mammedov57cb8cf2021-09-24 08:27:42 -04001917 bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
1918 log_addr_offset, 8, ACPI_BUILD_TPMLOG_FILE, 0);
Stefan Berger711b20b2014-08-11 16:33:36 -04001919
Igor Mammedov57cb8cf2021-09-24 08:27:42 -04001920 acpi_table_end(linker, &table);
Stefan Berger711b20b2014-08-11 16:33:36 -04001921}
Stefan Berger11fb99e2021-06-15 16:21:17 +02001922#endif
Stefan Berger711b20b2014-08-11 16:33:36 -04001923
Paolo Bonzinid471bf32018-06-29 16:22:13 +02001924#define HOLE_640K_START (640 * KiB)
1925#define HOLE_640K_END (1 * MiB)
Eduardo Habkost49264032017-09-01 10:10:02 +08001926
Igor Mammedove5b6d552021-09-24 08:27:44 -04001927/*
1928 * ACPI spec, Revision 3.0
1929 * 5.2.15 System Resource Affinity Table (SRAT)
1930 */
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001931static void
Igor Mammedov0e9b9ed2016-05-19 15:19:25 +02001932build_srat(GArray *table_data, BIOSLinker *linker, MachineState *machine)
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001933{
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001934 int i;
Igor Mammedove5b6d552021-09-24 08:27:44 -04001935 int numa_mem_start, slots;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001936 uint64_t mem_len, mem_base, next_base;
Igor Mammedov5803fce2016-02-26 14:59:23 +01001937 MachineClass *mc = MACHINE_GET_CLASS(machine);
Paolo Bonzinif0bb2762019-10-22 09:39:50 +02001938 X86MachineState *x86ms = X86_MACHINE(machine);
Igor Mammedov80e5db32017-01-18 18:13:20 +01001939 const CPUArchIdList *apic_ids = mc->possible_cpu_arch_ids(machine);
Jingqi Liue77af212021-08-23 09:12:54 +08001940 int nb_numa_nodes = machine->numa_state->num_nodes;
1941 NodeInfo *numa_info = machine->numa_state->nodes;
Igor Mammedov255bf202021-09-24 08:27:43 -04001942 AcpiTable table = { .sig = "SRAT", .rev = 1, .oem_id = x86ms->oem_id,
1943 .oem_table_id = x86ms->oem_table_id };
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001944
Igor Mammedov255bf202021-09-24 08:27:43 -04001945 acpi_table_begin(&table, table_data);
1946 build_append_int_noprefix(table_data, 1, 4); /* Reserved */
1947 build_append_int_noprefix(table_data, 0, 8); /* Reserved */
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001948
Igor Mammedov5803fce2016-02-26 14:59:23 +01001949 for (i = 0; i < apic_ids->len; i++) {
Igor Mammedovd41f3e72017-05-30 18:23:58 +02001950 int node_id = apic_ids->cpus[i].props.node_id;
Igor Mammedov5eff33a2016-10-19 14:05:32 +02001951 uint32_t apic_id = apic_ids->cpus[i].arch_id;
Igor Mammedov5803fce2016-02-26 14:59:23 +01001952
Igor Mammedov5eff33a2016-10-19 14:05:32 +02001953 if (apic_id < 255) {
Igor Mammedove5b6d552021-09-24 08:27:44 -04001954 /* 5.2.15.1 Processor Local APIC/SAPIC Affinity Structure */
1955 build_append_int_noprefix(table_data, 0, 1); /* Type */
1956 build_append_int_noprefix(table_data, 16, 1); /* Length */
1957 /* Proximity Domain [7:0] */
1958 build_append_int_noprefix(table_data, node_id, 1);
1959 build_append_int_noprefix(table_data, apic_id, 1); /* APIC ID */
1960 /* Flags, Table 5-36 */
1961 build_append_int_noprefix(table_data, 1, 4);
1962 build_append_int_noprefix(table_data, 0, 1); /* Local SAPIC EID */
1963 /* Proximity Domain [31:8] */
1964 build_append_int_noprefix(table_data, 0, 3);
1965 build_append_int_noprefix(table_data, 0, 4); /* Reserved */
Igor Mammedov5eff33a2016-10-19 14:05:32 +02001966 } else {
Igor Mammedove5b6d552021-09-24 08:27:44 -04001967 /*
1968 * ACPI spec, Revision 4.0
1969 * 5.2.16.3 Processor Local x2APIC Affinity Structure
1970 */
1971 build_append_int_noprefix(table_data, 2, 1); /* Type */
1972 build_append_int_noprefix(table_data, 24, 1); /* Length */
1973 build_append_int_noprefix(table_data, 0, 2); /* Reserved */
1974 /* Proximity Domain */
1975 build_append_int_noprefix(table_data, node_id, 4);
1976 build_append_int_noprefix(table_data, apic_id, 4); /* X2APIC ID */
1977 /* Flags, Table 5-39 */
1978 build_append_int_noprefix(table_data, 1 /* Enabled */, 4);
1979 build_append_int_noprefix(table_data, 0, 4); /* Clock Domain */
1980 build_append_int_noprefix(table_data, 0, 4); /* Reserved */
Igor Mammedov1f3aba32016-06-16 14:23:48 +02001981 }
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001982 }
1983
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001984 /* the memory map is a bit tricky, it contains at least one hole
1985 * from 640k-1M and possibly another one from 3.5G-4G.
1986 */
1987 next_base = 0;
Igor Mammedove5b6d552021-09-24 08:27:44 -04001988 numa_mem_start = table_data->len;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001989
Jingqi Liue77af212021-08-23 09:12:54 +08001990 for (i = 1; i < nb_numa_nodes + 1; ++i) {
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001991 mem_base = next_base;
Jingqi Liue77af212021-08-23 09:12:54 +08001992 mem_len = numa_info[i - 1].node_mem;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001993 next_base = mem_base + mem_len;
1994
Eduardo Habkost49264032017-09-01 10:10:02 +08001995 /* Cut out the 640K hole */
1996 if (mem_base <= HOLE_640K_START &&
1997 next_base > HOLE_640K_START) {
1998 mem_len -= next_base - HOLE_640K_START;
1999 if (mem_len > 0) {
Igor Mammedove5b6d552021-09-24 08:27:44 -04002000 build_srat_memory(table_data, mem_base, mem_len, i - 1,
Eduardo Habkost49264032017-09-01 10:10:02 +08002001 MEM_AFFINITY_ENABLED);
2002 }
2003
2004 /* Check for the rare case: 640K < RAM < 1M */
2005 if (next_base <= HOLE_640K_END) {
2006 next_base = HOLE_640K_END;
2007 continue;
2008 }
2009 mem_base = HOLE_640K_END;
2010 mem_len = next_base - HOLE_640K_END;
2011 }
2012
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002013 /* Cut out the ACPI_PCI hole */
Paolo Bonzinif0bb2762019-10-22 09:39:50 +02002014 if (mem_base <= x86ms->below_4g_mem_size &&
2015 next_base > x86ms->below_4g_mem_size) {
2016 mem_len -= next_base - x86ms->below_4g_mem_size;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002017 if (mem_len > 0) {
Igor Mammedove5b6d552021-09-24 08:27:44 -04002018 build_srat_memory(table_data, mem_base, mem_len, i - 1,
Shannon Zhao64b83132016-05-12 13:22:28 +01002019 MEM_AFFINITY_ENABLED);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002020 }
Joao Martins4ab4c332022-07-19 18:00:04 +01002021 mem_base = x86ms->above_4g_mem_start;
Paolo Bonzinif0bb2762019-10-22 09:39:50 +02002022 mem_len = next_base - x86ms->below_4g_mem_size;
Dou Liyang6cf6fe32017-12-14 12:08:55 +08002023 next_base = mem_base + mem_len;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002024 }
Dou Liyang16b42262018-07-10 16:58:01 +08002025
2026 if (mem_len > 0) {
Igor Mammedove5b6d552021-09-24 08:27:44 -04002027 build_srat_memory(table_data, mem_base, mem_len, i - 1,
Dou Liyang16b42262018-07-10 16:58:01 +08002028 MEM_AFFINITY_ENABLED);
2029 }
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002030 }
Vishal Vermac3b0cf62020-06-05 18:09:10 -06002031
2032 if (machine->nvdimms_state->is_enabled) {
2033 nvdimm_build_srat(table_data);
2034 }
2035
Yang Zhong11058122021-11-01 12:20:05 -04002036 sgx_epc_build_srat(table_data);
2037
Igor Mammedove5b6d552021-09-24 08:27:44 -04002038 /*
2039 * TODO: this part is not in ACPI spec and current linux kernel boots fine
2040 * without these entries. But I recall there were issues the last time I
2041 * tried to remove it with some ancient guest OS, however I can't remember
2042 * what that was so keep this around for now
2043 */
2044 slots = (table_data->len - numa_mem_start) / 40 /* mem affinity len */;
Jingqi Liue77af212021-08-23 09:12:54 +08002045 for (; slots < nb_numa_nodes + 2; slots++) {
Igor Mammedove5b6d552021-09-24 08:27:44 -04002046 build_srat_memory(table_data, 0, 0, 0, MEM_AFFINITY_NOFLAGS);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002047 }
2048
Igor Mammedovdbb6da82018-08-22 11:46:44 +02002049 /*
2050 * Entry is required for Windows to enable memory hotplug in OS
2051 * and for Linux to enable SWIOTLB when booted with less than
2052 * 4G of RAM. Windows works better if the entry sets proximity
2053 * to the highest NUMA node in the machine.
2054 * Memory devices may override proximity set by this entry,
2055 * providing _PXM method if necessary.
2056 */
David Hildenbrand75d5f342023-06-23 14:45:50 +02002057 if (machine->device_memory) {
Igor Mammedove5b6d552021-09-24 08:27:44 -04002058 build_srat_memory(table_data, machine->device_memory->base,
David Hildenbrand75d5f342023-06-23 14:45:50 +02002059 memory_region_size(&machine->device_memory->mr),
2060 nb_numa_nodes - 1,
Igor Mammedovdbb6da82018-08-22 11:46:44 +02002061 MEM_AFFINITY_HOTPLUGGABLE | MEM_AFFINITY_ENABLED);
Igor Mammedovcec65192014-06-02 15:25:28 +02002062 }
2063
Igor Mammedov255bf202021-09-24 08:27:43 -04002064 acpi_table_end(linker, &table);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002065}
2066
Peter Xud46114f2016-07-14 13:56:14 +08002067/*
Michael Tokarevbad5cfc2023-07-14 14:16:12 +03002068 * Insert DMAR scope for PCI bridges and endpoint devices
Xingang Wang26863362021-07-08 12:55:17 +00002069 */
2070static void
2071insert_scope(PCIBus *bus, PCIDevice *dev, void *opaque)
2072{
Igor Mammedov91a6b972021-09-24 08:27:45 -04002073 const size_t device_scope_size = 6 /* device scope structure */ +
2074 2 /* 1 path entry */;
Xingang Wang26863362021-07-08 12:55:17 +00002075 GArray *scope_blob = opaque;
Xingang Wang26863362021-07-08 12:55:17 +00002076
2077 if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_BRIDGE)) {
2078 /* Dmar Scope Type: 0x02 for PCI Bridge */
2079 build_append_int_noprefix(scope_blob, 0x02, 1);
2080 } else {
2081 /* Dmar Scope Type: 0x01 for PCI Endpoint Device */
2082 build_append_int_noprefix(scope_blob, 0x01, 1);
2083 }
2084
2085 /* length */
Igor Mammedov91a6b972021-09-24 08:27:45 -04002086 build_append_int_noprefix(scope_blob, device_scope_size, 1);
Xingang Wang26863362021-07-08 12:55:17 +00002087 /* reserved */
2088 build_append_int_noprefix(scope_blob, 0, 2);
2089 /* enumeration_id */
2090 build_append_int_noprefix(scope_blob, 0, 1);
2091 /* bus */
2092 build_append_int_noprefix(scope_blob, pci_bus_num(bus), 1);
2093 /* device */
2094 build_append_int_noprefix(scope_blob, PCI_SLOT(dev->devfn), 1);
2095 /* function */
2096 build_append_int_noprefix(scope_blob, PCI_FUNC(dev->devfn), 1);
2097}
2098
2099/* For a given PCI host bridge, walk and insert DMAR scope */
2100static int
2101dmar_host_bridges(Object *obj, void *opaque)
2102{
2103 GArray *scope_blob = opaque;
2104
2105 if (object_dynamic_cast(obj, TYPE_PCI_HOST_BRIDGE)) {
2106 PCIBus *bus = PCI_HOST_BRIDGE(obj)->bus;
2107
2108 if (bus && !pci_bus_bypass_iommu(bus)) {
Peter Xu2914fc62021-10-28 12:31:26 +08002109 pci_for_each_device_under_bus(bus, insert_scope, scope_blob);
Xingang Wang26863362021-07-08 12:55:17 +00002110 }
2111 }
2112
2113 return 0;
2114}
2115
2116/*
Igor Mammedov91a6b972021-09-24 08:27:45 -04002117 * Intel ® Virtualization Technology for Directed I/O
2118 * Architecture Specification. Revision 3.3
2119 * 8.1 DMA Remapping Reporting Structure
Peter Xud46114f2016-07-14 13:56:14 +08002120 */
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002121static void
Marian Postevca602b4582021-01-19 02:32:13 +02002122build_dmar_q35(GArray *table_data, BIOSLinker *linker, const char *oem_id,
2123 const char *oem_table_id)
Le Tand4eb9112014-08-16 13:55:39 +08002124{
Peter Xud46114f2016-07-14 13:56:14 +08002125 uint8_t dmar_flags = 0;
Igor Mammedov91a6b972021-09-24 08:27:45 -04002126 uint8_t rsvd10[10] = {};
2127 /* Root complex IOAPIC uses one path only */
2128 const size_t ioapic_scope_size = 6 /* device scope structure */ +
2129 2 /* 1 path entry */;
Peter Xud46114f2016-07-14 13:56:14 +08002130 X86IOMMUState *iommu = x86_iommu_get_default();
Prasad Singamsetty37f51382017-11-14 18:13:50 -05002131 IntelIOMMUState *intel_iommu = INTEL_IOMMU_DEVICE(iommu);
Xingang Wang26863362021-07-08 12:55:17 +00002132 GArray *scope_blob = g_array_new(false, true, 1);
2133
Igor Mammedov91a6b972021-09-24 08:27:45 -04002134 AcpiTable table = { .sig = "DMAR", .rev = 1, .oem_id = oem_id,
2135 .oem_table_id = oem_table_id };
2136
Xingang Wang26863362021-07-08 12:55:17 +00002137 /*
2138 * A PCI bus walk, for each PCI host bridge.
2139 * Insert scope for each PCI bridge and endpoint device which
2140 * is attached to a bus with iommu enabled.
2141 */
2142 object_child_foreach_recursive(object_get_root(),
2143 dmar_host_bridges, scope_blob);
Peter Xud46114f2016-07-14 13:56:14 +08002144
2145 assert(iommu);
Peter Xua924b3d2018-12-20 13:40:36 +08002146 if (x86_iommu_ir_supported(iommu)) {
Peter Xud46114f2016-07-14 13:56:14 +08002147 dmar_flags |= 0x1; /* Flags: 0x1: INT_REMAP */
2148 }
Le Tand4eb9112014-08-16 13:55:39 +08002149
Igor Mammedov91a6b972021-09-24 08:27:45 -04002150 acpi_table_begin(&table, table_data);
2151 /* Host Address Width */
2152 build_append_int_noprefix(table_data, intel_iommu->aw_bits - 1, 1);
2153 build_append_int_noprefix(table_data, dmar_flags, 1); /* Flags */
2154 g_array_append_vals(table_data, rsvd10, sizeof(rsvd10)); /* Reserved */
Le Tand4eb9112014-08-16 13:55:39 +08002155
Igor Mammedov91a6b972021-09-24 08:27:45 -04002156 /* 8.3 DMAR Remapping Hardware Unit Definition structure */
2157 build_append_int_noprefix(table_data, 0, 2); /* Type */
2158 /* Length */
2159 build_append_int_noprefix(table_data,
2160 16 + ioapic_scope_size + scope_blob->len, 2);
2161 /* Flags */
2162 build_append_int_noprefix(table_data, 0 /* Don't include all pci device */ ,
2163 1);
2164 build_append_int_noprefix(table_data, 0 , 1); /* Reserved */
2165 build_append_int_noprefix(table_data, 0 , 2); /* Segment Number */
2166 /* Register Base Address */
2167 build_append_int_noprefix(table_data, Q35_HOST_BRIDGE_IOMMU_ADDR , 8);
Le Tand4eb9112014-08-16 13:55:39 +08002168
Peter Xucfc13df2016-07-14 13:56:17 +08002169 /* Scope definition for the root-complex IOAPIC. See VT-d spec
2170 * 8.3.1 (version Oct. 2014 or later). */
Igor Mammedov91a6b972021-09-24 08:27:45 -04002171 build_append_int_noprefix(table_data, 0x03 /* IOAPIC */, 1); /* Type */
2172 build_append_int_noprefix(table_data, ioapic_scope_size, 1); /* Length */
2173 build_append_int_noprefix(table_data, 0, 2); /* Reserved */
2174 /* Enumeration ID */
2175 build_append_int_noprefix(table_data, ACPI_BUILD_IOAPIC_ID, 1);
2176 /* Start Bus Number */
2177 build_append_int_noprefix(table_data, Q35_PSEUDO_BUS_PLATFORM, 1);
2178 /* Path, {Device, Function} pair */
2179 build_append_int_noprefix(table_data, PCI_SLOT(Q35_PSEUDO_DEVFN_IOAPIC), 1);
2180 build_append_int_noprefix(table_data, PCI_FUNC(Q35_PSEUDO_DEVFN_IOAPIC), 1);
Peter Xucfc13df2016-07-14 13:56:17 +08002181
Xingang Wang26863362021-07-08 12:55:17 +00002182 /* Add scope found above */
2183 g_array_append_vals(table_data, scope_blob->data, scope_blob->len);
2184 g_array_free(scope_blob, true);
2185
Jason Wangbd2baac2016-12-30 18:09:16 +08002186 if (iommu->dt_supported) {
Igor Mammedov91a6b972021-09-24 08:27:45 -04002187 /* 8.5 Root Port ATS Capability Reporting Structure */
2188 build_append_int_noprefix(table_data, 2, 2); /* Type */
2189 build_append_int_noprefix(table_data, 8, 2); /* Length */
2190 build_append_int_noprefix(table_data, 1 /* ALL_PORTS */, 1); /* Flags */
2191 build_append_int_noprefix(table_data, 0, 1); /* Reserved */
2192 build_append_int_noprefix(table_data, 0, 2); /* Segment Number */
Jason Wangbd2baac2016-12-30 18:09:16 +08002193 }
2194
Igor Mammedov91a6b972021-09-24 08:27:45 -04002195 acpi_table_end(linker, &table);
Le Tand4eb9112014-08-16 13:55:39 +08002196}
Liran Alon14cda352020-03-13 16:50:08 +02002197
2198/*
2199 * Windows ACPI Emulated Devices Table
2200 * (Version 1.0 - April 6, 2009)
2201 * Spec: http://download.microsoft.com/download/7/E/7/7E7662CF-CBEA-470B-A97E-CE7CE0D98DC2/WAET.docx
2202 *
2203 * Helpful to speedup Windows guests and ignored by others.
2204 */
2205static void
Marian Postevca602b4582021-01-19 02:32:13 +02002206build_waet(GArray *table_data, BIOSLinker *linker, const char *oem_id,
2207 const char *oem_table_id)
Liran Alon14cda352020-03-13 16:50:08 +02002208{
Igor Mammedoveaa50762021-09-24 08:27:46 -04002209 AcpiTable table = { .sig = "WAET", .rev = 1, .oem_id = oem_id,
2210 .oem_table_id = oem_table_id };
Liran Alon14cda352020-03-13 16:50:08 +02002211
Igor Mammedoveaa50762021-09-24 08:27:46 -04002212 acpi_table_begin(&table, table_data);
Liran Alon14cda352020-03-13 16:50:08 +02002213 /*
2214 * Set "ACPI PM timer good" flag.
2215 *
2216 * Tells Windows guests that our ACPI PM timer is reliable in the
2217 * sense that guest can read it only once to obtain a reliable value.
2218 * Which avoids costly VMExits caused by guest re-reading it unnecessarily.
2219 */
2220 build_append_int_noprefix(table_data, 1 << 1 /* ACPI PM timer good */, 4);
Igor Mammedoveaa50762021-09-24 08:27:46 -04002221 acpi_table_end(linker, &table);
Liran Alon14cda352020-03-13 16:50:08 +02002222}
2223
David Kiariefb9f5922016-09-20 18:42:34 +03002224/*
2225 * IVRS table as specified in AMD IOMMU Specification v2.62, Section 5.2
2226 * accessible here http://support.amd.com/TechDocs/48882_IOMMU.pdf
2227 */
Singh, Brijeshc0288182018-10-01 19:44:41 +00002228#define IOAPIC_SB_DEVID (uint64_t)PCI_BUILD_BDF(0, PCI_DEVFN(0x14, 0))
2229
Alex Williamson977aff12019-10-23 16:47:28 -06002230/*
2231 * Insert IVHD entry for device and recurse, insert alias, or insert range as
2232 * necessary for the PCI topology.
2233 */
2234static void
2235insert_ivhd(PCIBus *bus, PCIDevice *dev, void *opaque)
2236{
2237 GArray *table_data = opaque;
2238 uint32_t entry;
2239
2240 /* "Select" IVHD entry, type 0x2 */
2241 entry = PCI_BUILD_BDF(pci_bus_num(bus), dev->devfn) << 8 | 0x2;
2242 build_append_int_noprefix(table_data, entry, 4);
2243
2244 if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_BRIDGE)) {
2245 PCIBus *sec_bus = pci_bridge_get_sec_bus(PCI_BRIDGE(dev));
2246 uint8_t sec = pci_bus_num(sec_bus);
2247 uint8_t sub = dev->config[PCI_SUBORDINATE_BUS];
2248
2249 if (pci_bus_is_express(sec_bus)) {
2250 /*
2251 * Walk the bus if there are subordinates, otherwise use a range
2252 * to cover an entire leaf bus. We could potentially also use a
2253 * range for traversed buses, but we'd need to take care not to
2254 * create both Select and Range entries covering the same device.
2255 * This is easier and potentially more compact.
2256 *
2257 * An example bare metal system seems to use Select entries for
2258 * root ports without a slot (ie. built-ins) and Range entries
2259 * when there is a slot. The same system also only hard-codes
2260 * the alias range for an onboard PCIe-to-PCI bridge, apparently
2261 * making no effort to support nested bridges. We attempt to
2262 * be more thorough here.
2263 */
2264 if (sec == sub) { /* leaf bus */
2265 /* "Start of Range" IVHD entry, type 0x3 */
2266 entry = PCI_BUILD_BDF(sec, PCI_DEVFN(0, 0)) << 8 | 0x3;
2267 build_append_int_noprefix(table_data, entry, 4);
2268 /* "End of Range" IVHD entry, type 0x4 */
2269 entry = PCI_BUILD_BDF(sub, PCI_DEVFN(31, 7)) << 8 | 0x4;
2270 build_append_int_noprefix(table_data, entry, 4);
2271 } else {
2272 pci_for_each_device(sec_bus, sec, insert_ivhd, table_data);
2273 }
2274 } else {
2275 /*
2276 * If the secondary bus is conventional, then we need to create an
2277 * Alias range for everything downstream. The range covers the
2278 * first devfn on the secondary bus to the last devfn on the
2279 * subordinate bus. The alias target depends on legacy versus
2280 * express bridges, just as in pci_device_iommu_address_space().
2281 * DeviceIDa vs DeviceIDb as per the AMD IOMMU spec.
2282 */
2283 uint16_t dev_id_a, dev_id_b;
2284
2285 dev_id_a = PCI_BUILD_BDF(sec, PCI_DEVFN(0, 0));
2286
2287 if (pci_is_express(dev) &&
2288 pcie_cap_get_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE) {
2289 dev_id_b = dev_id_a;
2290 } else {
2291 dev_id_b = PCI_BUILD_BDF(pci_bus_num(bus), dev->devfn);
2292 }
2293
2294 /* "Alias Start of Range" IVHD entry, type 0x43, 8 bytes */
2295 build_append_int_noprefix(table_data, dev_id_a << 8 | 0x43, 4);
2296 build_append_int_noprefix(table_data, dev_id_b << 8 | 0x0, 4);
2297
2298 /* "End of Range" IVHD entry, type 0x4 */
2299 entry = PCI_BUILD_BDF(sub, PCI_DEVFN(31, 7)) << 8 | 0x4;
2300 build_append_int_noprefix(table_data, entry, 4);
2301 }
2302 }
2303}
2304
2305/* For all PCI host bridges, walk and insert IVHD entries */
2306static int
2307ivrs_host_bridges(Object *obj, void *opaque)
2308{
2309 GArray *ivhd_blob = opaque;
2310
2311 if (object_dynamic_cast(obj, TYPE_PCI_HOST_BRIDGE)) {
2312 PCIBus *bus = PCI_HOST_BRIDGE(obj)->bus;
2313
Xingang Wangdec2f562021-07-08 12:55:18 +00002314 if (bus && !pci_bus_bypass_iommu(bus)) {
Peter Xu2914fc62021-10-28 12:31:26 +08002315 pci_for_each_device_under_bus(bus, insert_ivhd, ivhd_blob);
Alex Williamson977aff12019-10-23 16:47:28 -06002316 }
2317 }
2318
2319 return 0;
2320}
2321
David Kiariefb9f5922016-09-20 18:42:34 +03002322static void
Marian Postevca602b4582021-01-19 02:32:13 +02002323build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,
2324 const char *oem_table_id)
David Kiariefb9f5922016-09-20 18:42:34 +03002325{
David Kiariefb9f5922016-09-20 18:42:34 +03002326 AMDVIState *s = AMD_IOMMU_DEVICE(x86_iommu_get_default());
Alex Williamson977aff12019-10-23 16:47:28 -06002327 GArray *ivhd_blob = g_array_new(false, true, 1);
Igor Mammedovb0a45ff2021-09-24 08:27:47 -04002328 AcpiTable table = { .sig = "IVRS", .rev = 1, .oem_id = oem_id,
2329 .oem_table_id = oem_table_id };
Bui Quang Minh328a11a2024-01-11 22:44:03 +07002330 uint64_t feature_report;
David Kiariefb9f5922016-09-20 18:42:34 +03002331
Igor Mammedovb0a45ff2021-09-24 08:27:47 -04002332 acpi_table_begin(&table, table_data);
David Kiariefb9f5922016-09-20 18:42:34 +03002333 /* IVinfo - IO virtualization information common to all
2334 * IOMMU units in a system
2335 */
Bui Quang Minh328a11a2024-01-11 22:44:03 +07002336 build_append_int_noprefix(table_data,
2337 (1UL << 0) | /* EFRSup */
2338 (40UL << 8), /* PASize */
2339 4);
David Kiariefb9f5922016-09-20 18:42:34 +03002340 /* reserved */
2341 build_append_int_noprefix(table_data, 0, 8);
2342
Singh, Brijeshc0288182018-10-01 19:44:41 +00002343 /*
Alex Williamson977aff12019-10-23 16:47:28 -06002344 * A PCI bus walk, for each PCI host bridge, is necessary to create a
2345 * complete set of IVHD entries. Do this into a separate blob so that we
2346 * can calculate the total IVRS table length here and then append the new
2347 * blob further below. Fall back to an entry covering all devices, which
2348 * is sufficient when no aliases are present.
2349 */
2350 object_child_foreach_recursive(object_get_root(),
2351 ivrs_host_bridges, ivhd_blob);
2352
2353 if (!ivhd_blob->len) {
2354 /*
2355 * Type 1 device entry reporting all devices
2356 * These are 4-byte device entries currently reporting the range of
2357 * Refer to Spec - Table 95:IVHD Device Entry Type Codes(4-byte)
2358 */
2359 build_append_int_noprefix(ivhd_blob, 0x0000001, 4);
2360 }
2361
Alex Williamson977aff12019-10-23 16:47:28 -06002362 /*
Singh, Brijeshc0288182018-10-01 19:44:41 +00002363 * When interrupt remapping is supported, we add a special IVHD device
Bui Quang Minh328a11a2024-01-11 22:44:03 +07002364 * for type IO-APIC
2365 * Refer to spec - Table 95: IVHD device entry type codes
2366 *
2367 * Linux IOMMU driver checks for the special IVHD device (type IO-APIC).
2368 * See Linux kernel commit 'c2ff5cf5294bcbd7fa50f7d860e90a66db7e5059'
Singh, Brijeshc0288182018-10-01 19:44:41 +00002369 */
Peter Xua924b3d2018-12-20 13:40:36 +08002370 if (x86_iommu_ir_supported(x86_iommu_get_default())) {
Bui Quang Minh328a11a2024-01-11 22:44:03 +07002371 build_append_int_noprefix(ivhd_blob,
2372 (0x1ull << 56) | /* type IOAPIC */
2373 (IOAPIC_SB_DEVID << 40) | /* IOAPIC devid */
2374 0x48, /* special device */
2375 8);
Singh, Brijeshc0288182018-10-01 19:44:41 +00002376 }
Alex Williamson977aff12019-10-23 16:47:28 -06002377
Bui Quang Minh328a11a2024-01-11 22:44:03 +07002378 /* IVHD definition - type 10h */
2379 build_append_int_noprefix(table_data, 0x10, 1);
2380 /* virtualization flags */
2381 build_append_int_noprefix(table_data,
2382 (1UL << 0) | /* HtTunEn */
2383 (1UL << 4) | /* iotblSup */
2384 (1UL << 6) | /* PrefSup */
2385 (1UL << 7), /* PPRSup */
2386 1);
2387
David Kiariefb9f5922016-09-20 18:42:34 +03002388 /* IVHD length */
Bui Quang Minh328a11a2024-01-11 22:44:03 +07002389 build_append_int_noprefix(table_data, ivhd_blob->len + 24, 2);
David Kiariefb9f5922016-09-20 18:42:34 +03002390 /* DeviceID */
Philippe Mathieu-Daudé531f50a2023-03-13 16:30:28 +01002391 build_append_int_noprefix(table_data,
2392 object_property_get_int(OBJECT(&s->pci), "addr",
2393 &error_abort), 2);
David Kiariefb9f5922016-09-20 18:42:34 +03002394 /* Capability offset */
Philippe Mathieu-Daudéae097d82023-03-13 16:30:29 +01002395 build_append_int_noprefix(table_data, s->pci.capab_offset, 2);
David Kiariefb9f5922016-09-20 18:42:34 +03002396 /* IOMMU base address */
2397 build_append_int_noprefix(table_data, s->mmio.addr, 8);
2398 /* PCI Segment Group */
2399 build_append_int_noprefix(table_data, 0, 2);
2400 /* IOMMU info */
2401 build_append_int_noprefix(table_data, 0, 2);
2402 /* IOMMU Feature Reporting */
Bui Quang Minh328a11a2024-01-11 22:44:03 +07002403 feature_report = (48UL << 30) | /* HATS */
2404 (48UL << 28) | /* GATS */
2405 (1UL << 2) | /* GTSup */
2406 (1UL << 6); /* GASup */
2407 if (s->xtsup) {
2408 feature_report |= (1UL << 0); /* XTSup */
2409 }
2410 build_append_int_noprefix(table_data, feature_report, 4);
Alex Williamson977aff12019-10-23 16:47:28 -06002411
2412 /* IVHD entries as found above */
2413 g_array_append_vals(table_data, ivhd_blob->data, ivhd_blob->len);
David Kiariefb9f5922016-09-20 18:42:34 +03002414
Bui Quang Minh328a11a2024-01-11 22:44:03 +07002415 /* IVHD definition - type 11h */
2416 build_append_int_noprefix(table_data, 0x11, 1);
2417 /* virtualization flags */
2418 build_append_int_noprefix(table_data,
2419 (1UL << 0) | /* HtTunEn */
2420 (1UL << 4), /* iotblSup */
2421 1);
2422
2423 /* IVHD length */
2424 build_append_int_noprefix(table_data, ivhd_blob->len + 40, 2);
2425 /* DeviceID */
2426 build_append_int_noprefix(table_data,
2427 object_property_get_int(OBJECT(&s->pci), "addr",
2428 &error_abort), 2);
2429 /* Capability offset */
2430 build_append_int_noprefix(table_data, s->pci.capab_offset, 2);
2431 /* IOMMU base address */
2432 build_append_int_noprefix(table_data, s->mmio.addr, 8);
2433 /* PCI Segment Group */
2434 build_append_int_noprefix(table_data, 0, 2);
2435 /* IOMMU info */
2436 build_append_int_noprefix(table_data, 0, 2);
2437 /* IOMMU Attributes */
2438 build_append_int_noprefix(table_data, 0, 4);
2439 /* EFR Register Image */
2440 build_append_int_noprefix(table_data,
2441 amdvi_extended_feature_register(s),
2442 8);
2443 /* EFR Register Image 2 */
2444 build_append_int_noprefix(table_data, 0, 8);
2445
2446 /* IVHD entries as found above */
2447 g_array_append_vals(table_data, ivhd_blob->data, ivhd_blob->len);
2448
2449 g_array_free(ivhd_blob, TRUE);
Igor Mammedovb0a45ff2021-09-24 08:27:47 -04002450 acpi_table_end(linker, &table);
David Kiariefb9f5922016-09-20 18:42:34 +03002451}
Le Tand4eb9112014-08-16 13:55:39 +08002452
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002453typedef
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002454struct AcpiBuildState {
2455 /* Copy of table in RAM (for patching). */
Paolo Bonzini339240b2015-03-23 10:24:16 +01002456 MemoryRegion *table_mr;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002457 /* Is table patched? */
2458 uint8_t patched;
Michael S. Tsirkind70414a2015-02-09 13:59:53 +00002459 void *rsdp;
Paolo Bonzini339240b2015-03-23 10:24:16 +01002460 MemoryRegion *rsdp_mr;
2461 MemoryRegion *linker_mr;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002462} AcpiBuildState;
2463
2464static bool acpi_get_mcfg(AcpiMcfgInfo *mcfg)
2465{
2466 Object *pci_host;
2467 QObject *o;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002468
Marcel Apfelbaumca6c1852015-06-02 14:22:59 +03002469 pci_host = acpi_get_i386_pci_host();
Julia Suvorovac0e427d2021-07-13 02:42:01 +02002470 if (!pci_host) {
2471 return false;
2472 }
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002473
2474 o = object_property_get_qobject(pci_host, PCIE_HOST_MCFG_BASE, NULL);
2475 if (!o) {
2476 return false;
2477 }
Wei Yangc3094342019-04-19 08:30:50 +08002478 mcfg->base = qnum_get_uint(qobject_to(QNum, o));
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02002479 qobject_unref(o);
Wei Yangc3094342019-04-19 08:30:50 +08002480 if (mcfg->base == PCIE_BASE_ADDR_UNMAPPED) {
Igor Mammedovfe4970a2019-04-09 17:00:37 +02002481 return false;
2482 }
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002483
2484 o = object_property_get_qobject(pci_host, PCIE_HOST_MCFG_SIZE, NULL);
2485 assert(o);
Wei Yangc3094342019-04-19 08:30:50 +08002486 mcfg->size = qnum_get_uint(qobject_to(QNum, o));
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02002487 qobject_unref(o);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002488 return true;
2489}
2490
2491static
Igor Mammedov3d3ebca2016-02-26 14:59:22 +01002492void acpi_build(AcpiBuildTables *tables, MachineState *machine)
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002493{
Igor Mammedov3d3ebca2016-02-26 14:59:22 +01002494 PCMachineState *pcms = PC_MACHINE(machine);
Eduardo Habkostbb292f52015-12-11 16:42:28 -02002495 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
Paolo Bonzinif0bb2762019-10-22 09:39:50 +02002496 X86MachineState *x86ms = X86_MACHINE(machine);
Jean-Philippe Brucker36efa252021-10-26 19:20:23 +01002497 DeviceState *iommu = pcms->iommu;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002498 GArray *table_offsets;
Igor Mammedov41fa5c02016-01-22 15:36:06 +01002499 unsigned facs, dsdt, rsdt, fadt;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002500 AcpiPmInfo pm;
2501 AcpiMiscInfo misc;
2502 AcpiMcfgInfo mcfg;
Julia Suvorovac0e427d2021-07-13 02:42:01 +02002503 Range pci_hole = {}, pci_hole64 = {};
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002504 uint8_t *u;
Paolo Bonzini07fb6172014-07-28 17:34:15 +02002505 size_t aml_len = 0;
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002506 GArray *tables_blob = tables->table_data;
Laszlo Ersekae123742016-01-18 15:12:13 +01002507 AcpiSlicOem slic_oem = { .id = NULL, .table_id = NULL };
Ben Warrend03637b2017-02-16 15:15:36 -08002508 Object *vmgenid_dev;
Marian Postevca602b4582021-01-19 02:32:13 +02002509 char *oem_id;
2510 char *oem_table_id;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002511
Like Xu0e11fc62019-05-19 04:54:25 +08002512 acpi_get_pm_info(machine, &pm);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002513 acpi_get_misc_info(&misc);
Markus Armbruster01c97422016-06-15 19:56:31 +02002514 acpi_get_pci_holes(&pci_hole, &pci_hole64);
Laszlo Ersekae123742016-01-18 15:12:13 +01002515 acpi_get_slic_oem(&slic_oem);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002516
Marian Postevca602b4582021-01-19 02:32:13 +02002517 if (slic_oem.id) {
2518 oem_id = slic_oem.id;
2519 } else {
Marian Postevcad07b2282021-02-21 02:17:36 +02002520 oem_id = x86ms->oem_id;
Marian Postevca602b4582021-01-19 02:32:13 +02002521 }
2522
2523 if (slic_oem.table_id) {
2524 oem_table_id = slic_oem.table_id;
2525 } else {
Marian Postevcad07b2282021-02-21 02:17:36 +02002526 oem_table_id = x86ms->oem_table_id;
Marian Postevca602b4582021-01-19 02:32:13 +02002527 }
2528
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002529 table_offsets = g_array_new(false, true /* clear */,
2530 sizeof(uint32_t));
Gonglei8b310fc2014-11-13 10:59:37 +08002531 ACPI_BUILD_DPRINTF("init ACPI tables\n");
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002532
Igor Mammedovad9671b2016-05-19 15:19:26 +02002533 bios_linker_loader_alloc(tables->linker,
2534 ACPI_BUILD_TABLE_FILE, tables_blob,
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002535 64 /* Ensure FACS is aligned */,
2536 false /* high memory */);
2537
2538 /*
2539 * FACS is pointed to by FADT.
2540 * We place it first since it's the only table that has alignment
2541 * requirements.
2542 */
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002543 facs = tables_blob->len;
Wei Yang009180b2019-01-30 11:02:07 +08002544 build_facs(tables_blob);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002545
2546 /* DSDT is pointed to by FADT */
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002547 dsdt = tables_blob->len;
Markus Armbruster01c97422016-06-15 19:56:31 +02002548 build_dsdt(tables_blob, tables->linker, &pm, &misc,
2549 &pci_hole, &pci_hole64, machine);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002550
Paolo Bonzini07fb6172014-07-28 17:34:15 +02002551 /* Count the size of the DSDT and SSDT, we will need it for legacy
2552 * sizing of ACPI tables.
2553 */
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002554 aml_len += tables_blob->len - dsdt;
Paolo Bonzini07fb6172014-07-28 17:34:15 +02002555
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002556 /* ACPI tables pointed to by RSDT */
Igor Mammedov41fa5c02016-01-22 15:36:06 +01002557 fadt = tables_blob->len;
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002558 acpi_add_table(table_offsets, tables_blob);
Igor Mammedov937d1b52018-02-28 15:23:51 +01002559 pm.fadt.facs_tbl_offset = &facs;
2560 pm.fadt.dsdt_tbl_offset = &dsdt;
2561 pm.fadt.xdsdt_tbl_offset = &dsdt;
Marian Postevca602b4582021-01-19 02:32:13 +02002562 build_fadt(tables_blob, tables->linker, &pm.fadt, oem_id, oem_table_id);
Igor Mammedov41fa5c02016-01-22 15:36:06 +01002563 aml_len += tables_blob->len - fadt;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002564
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002565 acpi_add_table(table_offsets, tables_blob);
Gerd Hoffmanneb66ffa2020-05-20 15:19:47 +02002566 acpi_build_madt(tables_blob, tables->linker, x86ms,
Bernhard Beschowf4a06e52023-09-08 10:42:27 +02002567 x86ms->oem_id, x86ms->oem_table_id);
Michael S. Tsirkin9ac1c4c2014-04-28 08:15:32 +03002568
Eric DeVolder8486f122022-01-28 15:38:06 -05002569#ifdef CONFIG_ACPI_ERST
2570 {
2571 Object *erst_dev;
2572 erst_dev = find_erst_dev();
2573 if (erst_dev) {
2574 acpi_add_table(table_offsets, tables_blob);
2575 build_erst(tables_blob, tables->linker, erst_dev,
2576 x86ms->oem_id, x86ms->oem_table_id);
2577 }
2578 }
2579#endif
2580
Ben Warrend03637b2017-02-16 15:15:36 -08002581 vmgenid_dev = find_vmgenid_dev();
2582 if (vmgenid_dev) {
2583 acpi_add_table(table_offsets, tables_blob);
2584 vmgenid_build_acpi(VMGENID(vmgenid_dev), tables_blob,
Marian Postevcad07b2282021-02-21 02:17:36 +02002585 tables->vmgenid, tables->linker, x86ms->oem_id);
Ben Warrend03637b2017-02-16 15:15:36 -08002586 }
2587
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002588 if (misc.has_hpet) {
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002589 acpi_add_table(table_offsets, tables_blob);
Marian Postevcad07b2282021-02-21 02:17:36 +02002590 build_hpet(tables_blob, tables->linker, x86ms->oem_id,
2591 x86ms->oem_table_id);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002592 }
Stefan Berger11fb99e2021-06-15 16:21:17 +02002593#ifdef CONFIG_TPM
Stefan Berger5cb18b32015-05-26 16:51:07 -04002594 if (misc.tpm_version != TPM_VERSION_UNSPEC) {
Stefan Berger7e7c1b82020-05-29 15:28:40 -04002595 if (misc.tpm_version == TPM_VERSION_1_2) {
2596 acpi_add_table(table_offsets, tables_blob);
Marian Postevca602b4582021-01-19 02:32:13 +02002597 build_tpm_tcpa(tables_blob, tables->linker, tables->tcpalog,
Marian Postevcad07b2282021-02-21 02:17:36 +02002598 x86ms->oem_id, x86ms->oem_table_id);
Stefan Berger7e7c1b82020-05-29 15:28:40 -04002599 } else { /* TPM_VERSION_2_0 */
Igor Mammedov72d97b32015-06-09 05:31:53 +02002600 acpi_add_table(table_offsets, tables_blob);
Marian Postevca602b4582021-01-19 02:32:13 +02002601 build_tpm2(tables_blob, tables->linker, tables->tcpalog,
Marian Postevcad07b2282021-02-21 02:17:36 +02002602 x86ms->oem_id, x86ms->oem_table_id);
Stefan Berger5cb18b32015-05-26 16:51:07 -04002603 }
Stefan Berger711b20b2014-08-11 16:33:36 -04002604 }
Stefan Berger11fb99e2021-06-15 16:21:17 +02002605#endif
Jingqi Liue77af212021-08-23 09:12:54 +08002606 if (machine->numa_state->num_nodes) {
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002607 acpi_add_table(table_offsets, tables_blob);
Igor Mammedov3d3ebca2016-02-26 14:59:22 +01002608 build_srat(tables_blob, tables->linker, machine);
Tao Xu118154b2019-08-09 14:57:23 +08002609 if (machine->numa_state->have_numa_distance) {
He Chen0f203432017-04-27 10:35:58 +08002610 acpi_add_table(table_offsets, tables_blob);
Marian Postevcad07b2282021-02-21 02:17:36 +02002611 build_slit(tables_blob, tables->linker, machine, x86ms->oem_id,
2612 x86ms->oem_table_id);
He Chen0f203432017-04-27 10:35:58 +08002613 }
Liu Jingqie6f123c2019-12-13 09:19:25 +08002614 if (machine->numa_state->hmat_enabled) {
2615 acpi_add_table(table_offsets, tables_blob);
Marian Postevca602b4582021-01-19 02:32:13 +02002616 build_hmat(tables_blob, tables->linker, machine->numa_state,
Marian Postevcad07b2282021-02-21 02:17:36 +02002617 x86ms->oem_id, x86ms->oem_table_id);
Liu Jingqie6f123c2019-12-13 09:19:25 +08002618 }
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002619 }
2620 if (acpi_get_mcfg(&mcfg)) {
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002621 acpi_add_table(table_offsets, tables_blob);
Marian Postevcad07b2282021-02-21 02:17:36 +02002622 build_mcfg(tables_blob, tables->linker, &mcfg, x86ms->oem_id,
2623 x86ms->oem_table_id);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002624 }
Jean-Philippe Brucker867e9c92021-10-26 19:20:21 +01002625 if (object_dynamic_cast(OBJECT(iommu), TYPE_AMD_IOMMU_DEVICE)) {
2626 acpi_add_table(table_offsets, tables_blob);
2627 build_amd_iommu(tables_blob, tables->linker, x86ms->oem_id,
2628 x86ms->oem_table_id);
2629 } else if (object_dynamic_cast(OBJECT(iommu), TYPE_INTEL_IOMMU_DEVICE)) {
2630 acpi_add_table(table_offsets, tables_blob);
2631 build_dmar_q35(tables_blob, tables->linker, x86ms->oem_id,
2632 x86ms->oem_table_id);
Jean-Philippe Brucker36efa252021-10-26 19:20:23 +01002633 } else if (object_dynamic_cast(OBJECT(iommu), TYPE_VIRTIO_IOMMU_PCI)) {
2634 PCIDevice *pdev = PCI_DEVICE(iommu);
2635
2636 acpi_add_table(table_offsets, tables_blob);
2637 build_viot(machine, tables_blob, tables->linker, pci_get_bdf(pdev),
2638 x86ms->oem_id, x86ms->oem_table_id);
Le Tand4eb9112014-08-16 13:55:39 +08002639 }
Eric Augerf6a0d062019-03-08 19:20:53 +01002640 if (machine->nvdimms_state->is_enabled) {
Igor Mammedovad9671b2016-05-19 15:19:26 +02002641 nvdimm_build_acpi(table_offsets, tables_blob, tables->linker,
Marian Postevca602b4582021-01-19 02:32:13 +02002642 machine->nvdimms_state, machine->ram_slots,
Marian Postevcad07b2282021-02-21 02:17:36 +02002643 x86ms->oem_id, x86ms->oem_table_id);
Xiao Guangrong87252e12015-12-02 15:20:58 +08002644 }
Jonathan Cameron1ebf9002022-06-08 15:54:39 +01002645 if (pcms->cxl_devices_state.is_enabled) {
Jonathan Cameron51359802022-06-08 15:54:34 +01002646 cxl_build_cedt(table_offsets, tables_blob, tables->linker,
Jonathan Cameron1ebf9002022-06-08 15:54:39 +01002647 x86ms->oem_id, x86ms->oem_table_id, &pcms->cxl_devices_state);
Ben Widawsky3d6a69b2022-04-29 15:40:50 +01002648 }
Xiao Guangrong87252e12015-12-02 15:20:58 +08002649
Liran Alon14cda352020-03-13 16:50:08 +02002650 acpi_add_table(table_offsets, tables_blob);
Marian Postevcad07b2282021-02-21 02:17:36 +02002651 build_waet(tables_blob, tables->linker, x86ms->oem_id, x86ms->oem_table_id);
Liran Alon14cda352020-03-13 16:50:08 +02002652
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002653 /* Add tables supplied by user (if any) */
2654 for (u = acpi_table_first(); u; u = acpi_table_next(u)) {
2655 unsigned len = acpi_table_len(u);
2656
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002657 acpi_add_table(table_offsets, tables_blob);
2658 g_array_append_vals(tables_blob, u, len);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002659 }
2660
2661 /* RSDT is pointed to by RSDP */
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002662 rsdt = tables_blob->len;
Laszlo Ersekae123742016-01-18 15:12:13 +01002663 build_rsdt(tables_blob, tables->linker, table_offsets,
Marian Postevca602b4582021-01-19 02:32:13 +02002664 oem_id, oem_table_id);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002665
2666 /* RSDP is in FSEG memory, so allocate it separately */
Samuel Ortiza46ce1c2018-12-17 16:34:48 +01002667 {
2668 AcpiRsdpData rsdp_data = {
2669 .revision = 0,
Marian Postevcad07b2282021-02-21 02:17:36 +02002670 .oem_id = x86ms->oem_id,
Samuel Ortiza46ce1c2018-12-17 16:34:48 +01002671 .xsdt_tbl_offset = NULL,
2672 .rsdt_tbl_offset = &rsdt,
2673 };
2674 build_rsdp(tables->rsdp, tables->linker, &rsdp_data);
2675 if (!pcmc->rsdp_in_ram) {
2676 /* We used to allocate some extra space for RSDP revision 2 but
2677 * only used the RSDP revision 0 space. The extra bytes were
2678 * zeroed out and not used.
2679 * Here we continue wasting those extra 16 bytes to make sure we
2680 * don't break migration for machine types 2.2 and older due to
2681 * RSDP blob size mismatch.
2682 */
2683 build_append_int_noprefix(tables->rsdp, 0, 16);
2684 }
2685 }
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002686
Paolo Bonzini07fb6172014-07-28 17:34:15 +02002687 /* We'll expose it all to Guest so we want to reduce
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002688 * chance of size changes.
Paolo Bonzini07fb6172014-07-28 17:34:15 +02002689 *
2690 * We used to align the tables to 4k, but of course this would
2691 * too simple to be enough. 4k turned out to be too small an
2692 * alignment very soon, and in fact it is almost impossible to
2693 * keep the table size stable for all (max_cpus, max_memory_slots)
2694 * combinations. So the table size is always 64k for pc-i440fx-2.1
2695 * and we give an error if the table grows beyond that limit.
2696 *
2697 * We still have the problem of migrating from "-M pc-i440fx-2.0". For
2698 * that, we exploit the fact that QEMU 2.1 generates _smaller_ tables
2699 * than 2.0 and we can always pad the smaller tables with zeros. We can
2700 * then use the exact size of the 2.0 tables.
2701 *
2702 * All this is for PIIX4, since QEMU 2.0 didn't support Q35 migration.
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002703 */
Eduardo Habkostbb292f52015-12-11 16:42:28 -02002704 if (pcmc->legacy_acpi_table_size) {
Paolo Bonzini07fb6172014-07-28 17:34:15 +02002705 /* Subtracting aml_len gives the size of fixed tables. Then add the
2706 * size of the PIIX4 DSDT/SSDT in QEMU 2.0.
2707 */
2708 int legacy_aml_len =
Eduardo Habkostbb292f52015-12-11 16:42:28 -02002709 pcmc->legacy_acpi_table_size +
Paolo Bonzinif0bb2762019-10-22 09:39:50 +02002710 ACPI_BUILD_LEGACY_CPU_AML_SIZE * x86ms->apic_id_limit;
Paolo Bonzini07fb6172014-07-28 17:34:15 +02002711 int legacy_table_size =
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002712 ROUND_UP(tables_blob->len - aml_len + legacy_aml_len,
Paolo Bonzini07fb6172014-07-28 17:34:15 +02002713 ACPI_BUILD_ALIGN_SIZE);
Ani Sinha1af50772023-03-29 10:27:26 +05302714 if ((tables_blob->len > legacy_table_size) &&
2715 !pcmc->resizable_acpi_blob) {
Paolo Bonzini07fb6172014-07-28 17:34:15 +02002716 /* Should happen only with PCI bridges and -M pc-i440fx-2.0. */
Alistair Francis9e5d2c52017-09-11 12:52:43 -07002717 warn_report("ACPI table size %u exceeds %d bytes,"
2718 " migration may not work",
2719 tables_blob->len, legacy_table_size);
2720 error_printf("Try removing CPUs, NUMA nodes, memory slots"
Greg Kurzdfa1aa82024-01-30 17:37:39 +01002721 " or PCI bridges.\n");
Paolo Bonzini07fb6172014-07-28 17:34:15 +02002722 }
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002723 g_array_set_size(tables_blob, legacy_table_size);
Paolo Bonzini07fb6172014-07-28 17:34:15 +02002724 } else {
Michael S. Tsirkin868270f2014-07-28 23:07:11 +02002725 /* Make sure we have a buffer in case we need to resize the tables. */
Ani Sinha1af50772023-03-29 10:27:26 +05302726 if ((tables_blob->len > ACPI_BUILD_TABLE_SIZE / 2) &&
2727 !pcmc->resizable_acpi_blob) {
Paolo Bonzini18045fb2014-07-28 17:34:16 +02002728 /* As of QEMU 2.1, this fires with 160 VCPUs and 255 memory slots. */
Alistair Francis9e5d2c52017-09-11 12:52:43 -07002729 warn_report("ACPI table size %u exceeds %d bytes,"
2730 " migration may not work",
2731 tables_blob->len, ACPI_BUILD_TABLE_SIZE / 2);
2732 error_printf("Try removing CPUs, NUMA nodes, memory slots"
Greg Kurzdfa1aa82024-01-30 17:37:39 +01002733 " or PCI bridges.\n");
Paolo Bonzini18045fb2014-07-28 17:34:16 +02002734 }
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002735 acpi_align_size(tables_blob, ACPI_BUILD_TABLE_SIZE);
Paolo Bonzini07fb6172014-07-28 17:34:15 +02002736 }
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002737
Igor Mammedov0e9b9ed2016-05-19 15:19:25 +02002738 acpi_align_size(tables->linker->cmd_blob, ACPI_BUILD_ALIGN_SIZE);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002739
2740 /* Cleanup memory that's no longer used. */
2741 g_array_free(table_offsets, true);
Igor Mammedov8cdb99a2021-12-27 14:31:17 -05002742 g_free(slic_oem.id);
2743 g_free(slic_oem.table_id);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002744}
2745
Paolo Bonzini339240b2015-03-23 10:24:16 +01002746static void acpi_ram_update(MemoryRegion *mr, GArray *data)
Michael S. Tsirkin42d85902015-02-15 17:12:11 +01002747{
2748 uint32_t size = acpi_data_len(data);
2749
2750 /* Make sure RAM size is correct - in case it got changed e.g. by migration */
Paolo Bonzini339240b2015-03-23 10:24:16 +01002751 memory_region_ram_resize(mr, size, &error_abort);
Michael S. Tsirkin42d85902015-02-15 17:12:11 +01002752
Paolo Bonzini339240b2015-03-23 10:24:16 +01002753 memcpy(memory_region_get_ram_ptr(mr), data->data, size);
2754 memory_region_set_dirty(mr, 0, size);
Michael S. Tsirkin42d85902015-02-15 17:12:11 +01002755}
2756
Gabriel L. Somlo3f8752b2015-11-05 09:32:49 -05002757static void acpi_build_update(void *build_opaque)
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002758{
2759 AcpiBuildState *build_state = build_opaque;
2760 AcpiBuildTables tables;
2761
2762 /* No state to update or already patched? Nothing to do. */
2763 if (!build_state || build_state->patched) {
2764 return;
2765 }
2766 build_state->patched = 1;
2767
2768 acpi_build_tables_init(&tables);
2769
Igor Mammedov3d3ebca2016-02-26 14:59:22 +01002770 acpi_build(&tables, MACHINE(qdev_get_machine()));
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002771
Paolo Bonzini339240b2015-03-23 10:24:16 +01002772 acpi_ram_update(build_state->table_mr, tables.table_data);
Michael S. Tsirkina1666142014-11-17 07:51:50 +02002773
Michael S. Tsirkin42d85902015-02-15 17:12:11 +01002774 if (build_state->rsdp) {
2775 memcpy(build_state->rsdp, tables.rsdp->data, acpi_data_len(tables.rsdp));
2776 } else {
Paolo Bonzini339240b2015-03-23 10:24:16 +01002777 acpi_ram_update(build_state->rsdp_mr, tables.rsdp);
Michael S. Tsirkin42d85902015-02-15 17:12:11 +01002778 }
Michael S. Tsirkina1666142014-11-17 07:51:50 +02002779
Igor Mammedov0e9b9ed2016-05-19 15:19:25 +02002780 acpi_ram_update(build_state->linker_mr, tables.linker->cmd_blob);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002781 acpi_build_tables_cleanup(&tables, true);
2782}
2783
2784static void acpi_build_reset(void *build_opaque)
2785{
2786 AcpiBuildState *build_state = build_opaque;
2787 build_state->patched = 0;
2788}
2789
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002790static const VMStateDescription vmstate_acpi_build = {
2791 .name = "acpi_build",
2792 .version_id = 1,
2793 .minimum_version_id = 1,
Richard Henderson9231a012023-12-21 14:16:12 +11002794 .fields = (const VMStateField[]) {
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002795 VMSTATE_UINT8(patched, AcpiBuildState),
2796 VMSTATE_END_OF_LIST()
2797 },
2798};
2799
Eduardo Habkostfb306ff2015-12-11 16:42:26 -02002800void acpi_setup(void)
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002801{
Eduardo Habkostfb306ff2015-12-11 16:42:26 -02002802 PCMachineState *pcms = PC_MACHINE(qdev_get_machine());
Eduardo Habkostbb292f52015-12-11 16:42:28 -02002803 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
Paolo Bonzinif0bb2762019-10-22 09:39:50 +02002804 X86MachineState *x86ms = X86_MACHINE(pcms);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002805 AcpiBuildTables tables;
2806 AcpiBuildState *build_state;
Ben Warrend03637b2017-02-16 15:15:36 -08002807 Object *vmgenid_dev;
Stefan Berger11fb99e2021-06-15 16:21:17 +02002808#ifdef CONFIG_TPM
Stefan Berger0fe24662019-01-15 02:27:51 +04002809 TPMIf *tpm;
2810 static FwCfgTPMConfig tpm_config;
Stefan Berger11fb99e2021-06-15 16:21:17 +02002811#endif
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002812
Paolo Bonzinif0bb2762019-10-22 09:39:50 +02002813 if (!x86ms->fw_cfg) {
Gonglei8b310fc2014-11-13 10:59:37 +08002814 ACPI_BUILD_DPRINTF("No fw cfg. Bailing out.\n");
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002815 return;
2816 }
2817
Wei Liu021746c2016-11-01 17:44:16 +00002818 if (!pcms->acpi_build_enabled) {
Gonglei8b310fc2014-11-13 10:59:37 +08002819 ACPI_BUILD_DPRINTF("ACPI build disabled. Bailing out.\n");
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002820 return;
2821 }
2822
Gerd Hoffmann17e89072020-03-20 11:01:36 +01002823 if (!x86_machine_is_acpi_enabled(X86_MACHINE(pcms))) {
Gonglei8b310fc2014-11-13 10:59:37 +08002824 ACPI_BUILD_DPRINTF("ACPI disabled. Bailing out.\n");
Michael S. Tsirkin81adc512013-11-07 14:12:05 +02002825 return;
2826 }
2827
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002828 build_state = g_malloc0(sizeof *build_state);
2829
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002830 acpi_build_tables_init(&tables);
Igor Mammedov3d3ebca2016-02-26 14:59:22 +01002831 acpi_build(&tables, MACHINE(pcms));
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002832
2833 /* Now expose it all to Guest */
Wei Yang82f76c62019-06-10 09:18:30 +08002834 build_state->table_mr = acpi_add_rom_blob(acpi_build_update,
2835 build_state, tables.table_data,
David Hildenbrand6930ba02021-03-04 11:55:53 +01002836 ACPI_BUILD_TABLE_FILE);
Paolo Bonzini339240b2015-03-23 10:24:16 +01002837 assert(build_state->table_mr != NULL);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002838
Paolo Bonzini339240b2015-03-23 10:24:16 +01002839 build_state->linker_mr =
Wei Yang82f76c62019-06-10 09:18:30 +08002840 acpi_add_rom_blob(acpi_build_update, build_state,
David Hildenbrand6930ba02021-03-04 11:55:53 +01002841 tables.linker->cmd_blob, ACPI_BUILD_LOADER_FILE);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002842
Stefan Berger11fb99e2021-06-15 16:21:17 +02002843#ifdef CONFIG_TPM
Paolo Bonzinif0bb2762019-10-22 09:39:50 +02002844 fw_cfg_add_file(x86ms->fw_cfg, ACPI_BUILD_TPMLOG_FILE,
Stefan Berger42a5b302014-10-24 13:21:04 -04002845 tables.tcpalog->data, acpi_data_len(tables.tcpalog));
2846
Stefan Berger0fe24662019-01-15 02:27:51 +04002847 tpm = tpm_find();
2848 if (tpm && object_property_get_bool(OBJECT(tpm), "ppi", &error_abort)) {
2849 tpm_config = (FwCfgTPMConfig) {
2850 .tpmppi_address = cpu_to_le32(TPM_PPI_ADDR_BASE),
2851 .tpm_version = tpm_get_version(tpm),
Stefan Bergerac6dd312019-01-15 02:27:52 +04002852 .tpmppi_version = TPM_PPI_VERSION_1_30
Stefan Berger0fe24662019-01-15 02:27:51 +04002853 };
Paolo Bonzinif0bb2762019-10-22 09:39:50 +02002854 fw_cfg_add_file(x86ms->fw_cfg, "etc/tpm/config",
Stefan Berger0fe24662019-01-15 02:27:51 +04002855 &tpm_config, sizeof tpm_config);
2856 }
Stefan Berger11fb99e2021-06-15 16:21:17 +02002857#endif
Stefan Berger0fe24662019-01-15 02:27:51 +04002858
Ben Warrend03637b2017-02-16 15:15:36 -08002859 vmgenid_dev = find_vmgenid_dev();
2860 if (vmgenid_dev) {
Paolo Bonzinif0bb2762019-10-22 09:39:50 +02002861 vmgenid_add_fw_cfg(VMGENID(vmgenid_dev), x86ms->fw_cfg,
Ben Warrend03637b2017-02-16 15:15:36 -08002862 tables.vmgenid);
2863 }
2864
Eduardo Habkostbb292f52015-12-11 16:42:28 -02002865 if (!pcmc->rsdp_in_ram) {
Igor Mammedov358774d2015-02-09 13:59:55 +00002866 /*
2867 * Keep for compatibility with old machine types.
2868 * Though RSDP is small, its contents isn't immutable, so
Michael S. Tsirkinafaa2e42015-02-17 10:40:30 +01002869 * we'll update it along with the rest of tables on guest access.
Igor Mammedov358774d2015-02-09 13:59:55 +00002870 */
Michael S. Tsirkinafaa2e42015-02-17 10:40:30 +01002871 uint32_t rsdp_size = acpi_data_len(tables.rsdp);
2872
2873 build_state->rsdp = g_memdup(tables.rsdp->data, rsdp_size);
Paolo Bonzinif0bb2762019-10-22 09:39:50 +02002874 fw_cfg_add_file_callback(x86ms->fw_cfg, ACPI_BUILD_RSDP_FILE,
Marc-André Lureau5f9252f2017-09-11 18:59:23 +02002875 acpi_build_update, NULL, build_state,
Michael S. Tsirkinbaf2d5b2017-01-12 19:24:14 +01002876 build_state->rsdp, rsdp_size, true);
Paolo Bonzini339240b2015-03-23 10:24:16 +01002877 build_state->rsdp_mr = NULL;
Igor Mammedov358774d2015-02-09 13:59:55 +00002878 } else {
Michael S. Tsirkin42d85902015-02-15 17:12:11 +01002879 build_state->rsdp = NULL;
Wei Yang82f76c62019-06-10 09:18:30 +08002880 build_state->rsdp_mr = acpi_add_rom_blob(acpi_build_update,
2881 build_state, tables.rsdp,
David Hildenbrand6930ba02021-03-04 11:55:53 +01002882 ACPI_BUILD_RSDP_FILE);
Igor Mammedov358774d2015-02-09 13:59:55 +00002883 }
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002884
2885 qemu_register_reset(acpi_build_reset, build_state);
2886 acpi_build_reset(build_state);
2887 vmstate_register(NULL, 0, &vmstate_acpi_build, build_state);
2888
2889 /* Cleanup tables but don't free the memory: we track it
2890 * in build_state.
2891 */
2892 acpi_build_tables_cleanup(&tables, false);
2893}