blob: 7a5a8b3521b027c598a903b734edb2939cc1ecf1 [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"
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +030030#include "hw/pci/pci.h"
Markus Armbruster2e5b09f2019-07-09 17:20:52 +020031#include "hw/core/cpu.h"
Thomas Huthfcf5ef22016-10-11 08:56:52 +020032#include "target/i386/cpu.h"
Philippe Mathieu-Daudé0d5d8a32017-10-17 13:44:23 -030033#include "hw/misc/pvpanic.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"
Gabriel L. Somlo15bce1b2013-12-22 10:34:56 -050040#include "hw/isa/isa.h"
Roman Kagan27b9fc52016-02-17 21:25:33 +030041#include "hw/block/fdc.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"
Like Xu0e11fc62019-05-19 04:54:25 +080046#include "hw/boards.h"
Stefan Berger5cb18b32015-05-26 16:51:07 -040047#include "sysemu/tpm_backend.h"
Philippe Mathieu-Daudébcdb9062019-10-04 01:03:53 +020048#include "hw/rtc/mc146818rtc_regs.h"
Markus Armbrusterd6454272019-08-12 07:23:45 +020049#include "migration/vmstate.h"
David Hildenbrand2cc0e2e2018-04-23 18:51:16 +020050#include "hw/mem/memory-device.h"
Philippe Mathieu-Daudé4b997692020-02-28 12:46:47 +010051#include "hw/mem/nvdimm.h"
Igor Mammedov1f3aba32016-06-16 14:23:48 +020052#include "sysemu/numa.h"
Markus Armbruster71e8a912019-08-12 07:23:38 +020053#include "sysemu/reset.h"
Jon Doron6775d152020-04-24 15:34:43 +030054#include "hw/hyperv/vmbus-bridge.h"
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +030055
56/* Supported chipsets: */
Philippe Mathieu-Daudéfff123b2018-01-06 16:37:26 +010057#include "hw/southbridge/piix.h"
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +030058#include "hw/acpi/pcihp.h"
Paolo Bonzini89a289c2019-12-12 14:14:40 +010059#include "hw/i386/fw_cfg.h"
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +030060#include "hw/i386/ich9.h"
61#include "hw/pci/pci_bus.h"
62#include "hw/pci-host/q35.h"
Peter Xu1cf5fd52016-07-14 13:56:12 +080063#include "hw/i386/x86-iommu.h"
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +030064
Igor Mammedov19934e02015-01-30 13:29:36 +000065#include "hw/acpi/aml-build.h"
Wei Yang82f76c62019-06-10 09:18:30 +080066#include "hw/acpi/utils.h"
Wei Yang48cefd92019-04-19 08:30:51 +080067#include "hw/acpi/pci.h"
Igor Mammedov19934e02015-01-30 13:29:36 +000068
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +030069#include "qom/qom-qobject.h"
David Kiariefb9f5922016-09-20 18:42:34 +030070#include "hw/i386/amd_iommu.h"
71#include "hw/i386/intel_iommu.h"
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +030072
Corey Minyard86e91dd2016-06-10 04:15:42 -050073#include "hw/acpi/ipmi.h"
Liu Jingqie6f123c2019-12-13 09:19:25 +080074#include "hw/acpi/hmat.h"
Corey Minyard86e91dd2016-06-10 04:15:42 -050075
Paolo Bonzini07fb6172014-07-28 17:34:15 +020076/* These are used to size the ACPI tables for -M pc-i440fx-1.7 and
77 * -M pc-i440fx-2.0. Even if the actual amount of AML generated grows
78 * a little bit, there should be plenty of free space since the DSDT
79 * shrunk by ~1.5k between QEMU 2.0 and QEMU 2.1.
80 */
81#define ACPI_BUILD_LEGACY_CPU_AML_SIZE 97
82#define ACPI_BUILD_ALIGN_SIZE 0x1000
83
Michael S. Tsirkin868270f2014-07-28 23:07:11 +020084#define ACPI_BUILD_TABLE_SIZE 0x20000
Paolo Bonzini18045fb2014-07-28 17:34:16 +020085
Gonglei8b310fc2014-11-13 10:59:37 +080086/* #define DEBUG_ACPI_BUILD */
87#ifdef DEBUG_ACPI_BUILD
88#define ACPI_BUILD_DPRINTF(fmt, ...) \
89 do {printf("ACPI_BUILD: " fmt, ## __VA_ARGS__); } while (0)
90#else
91#define ACPI_BUILD_DPRINTF(fmt, ...)
92#endif
93
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +030094typedef struct AcpiPmInfo {
95 bool s3_disabled;
96 bool s4_disabled;
Igor Mammedov133a2da2014-07-28 17:34:18 +020097 bool pcihp_bridge_en;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +030098 uint8_t s4_val;
Igor Mammedov937d1b52018-02-28 15:23:51 +010099 AcpiFadtData fadt;
Igor Mammedovddf1ec22015-02-18 19:14:44 +0000100 uint16_t cpu_hp_io_base;
Igor Mammedov500b11e2015-02-18 19:14:50 +0000101 uint16_t pcihp_io_base;
102 uint16_t pcihp_io_len;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300103} AcpiPmInfo;
104
105typedef struct AcpiMiscInfo {
Igor Mammedove4db2792015-12-28 18:02:37 +0100106 bool is_piix4;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300107 bool has_hpet;
Stefan Berger5cb18b32015-05-26 16:51:07 -0400108 TPMVersion tpm_version;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300109 const unsigned char *dsdt_code;
110 unsigned dsdt_size;
111 uint16_t pvpanic_port;
Igor Mammedov8ac6f7a2015-02-20 18:22:12 +0000112 uint16_t applesmc_io_base;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300113} AcpiMiscInfo;
114
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300115typedef struct AcpiBuildPciBusHotplugState {
116 GArray *device_table;
117 GArray *notify_table;
118 struct AcpiBuildPciBusHotplugState *parent;
Igor Mammedov133a2da2014-07-28 17:34:18 +0200119 bool pcihp_bridge_en;
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300120} AcpiBuildPciBusHotplugState;
121
Stefan Berger0fe24662019-01-15 02:27:51 +0400122typedef struct FwCfgTPMConfig {
123 uint32_t tpmppi_address;
124 uint8_t tpm_version;
125 uint8_t tpmppi_version;
126} QEMU_PACKED FwCfgTPMConfig;
127
Gerd Hoffmann4a441832019-06-07 09:34:29 +0200128static bool acpi_get_mcfg(AcpiMcfgInfo *mcfg);
129
Kwangwoo Lee5c94b822020-04-21 13:59:29 +0100130const struct AcpiGenericAddress x86_nvdimm_acpi_dsmio = {
131 .space_id = AML_AS_SYSTEM_IO,
132 .address = NVDIMM_ACPI_IO_BASE,
133 .bit_width = NVDIMM_ACPI_IO_LEN << 3
134};
135
Like Xu0e11fc62019-05-19 04:54:25 +0800136static void init_common_fadt_data(MachineState *ms, Object *o,
137 AcpiFadtData *data)
Igor Mammedov937d1b52018-02-28 15:23:51 +0100138{
139 uint32_t io = object_property_get_uint(o, ACPI_PM_PROP_PM_IO_BASE, NULL);
140 AmlAddressSpace as = AML_AS_SYSTEM_IO;
141 AcpiFadtData fadt = {
142 .rev = 3,
143 .flags =
144 (1 << ACPI_FADT_F_WBINVD) |
145 (1 << ACPI_FADT_F_PROC_C1) |
146 (1 << ACPI_FADT_F_SLP_BUTTON) |
147 (1 << ACPI_FADT_F_RTC_S4) |
148 (1 << ACPI_FADT_F_USE_PLATFORM_CLOCK) |
149 /* APIC destination mode ("Flat Logical") has an upper limit of 8
150 * CPUs for more than 8 CPUs, "Clustered Logical" mode has to be
151 * used
152 */
Like Xu0e11fc62019-05-19 04:54:25 +0800153 ((ms->smp.max_cpus > 8) ?
154 (1 << ACPI_FADT_F_FORCE_APIC_CLUSTER_MODEL) : 0),
Igor Mammedov937d1b52018-02-28 15:23:51 +0100155 .int_model = 1 /* Multiple APIC */,
156 .rtc_century = RTC_CENTURY,
157 .plvl2_lat = 0xfff /* C2 state not supported */,
158 .plvl3_lat = 0xfff /* C3 state not supported */,
159 .smi_cmd = ACPI_PORT_SMI_CMD,
160 .sci_int = object_property_get_uint(o, ACPI_PM_PROP_SCI_INT, NULL),
161 .acpi_enable_cmd =
162 object_property_get_uint(o, ACPI_PM_PROP_ACPI_ENABLE_CMD, NULL),
163 .acpi_disable_cmd =
164 object_property_get_uint(o, ACPI_PM_PROP_ACPI_DISABLE_CMD, NULL),
165 .pm1a_evt = { .space_id = as, .bit_width = 4 * 8, .address = io },
166 .pm1a_cnt = { .space_id = as, .bit_width = 2 * 8,
167 .address = io + 0x04 },
168 .pm_tmr = { .space_id = as, .bit_width = 4 * 8, .address = io + 0x08 },
169 .gpe0_blk = { .space_id = as, .bit_width =
170 object_property_get_uint(o, ACPI_PM_PROP_GPE0_BLK_LEN, NULL) * 8,
171 .address = object_property_get_uint(o, ACPI_PM_PROP_GPE0_BLK, NULL)
172 },
173 };
174 *data = fadt;
175}
176
Philippe Mathieu-Daudé81c48dd2019-04-27 16:40:24 +0200177static Object *object_resolve_type_unambiguous(const char *typename)
178{
179 bool ambig;
180 Object *o = object_resolve_path_type("", typename, &ambig);
181
182 if (ambig || !o) {
183 return NULL;
184 }
185 return o;
186}
187
Like Xu0e11fc62019-05-19 04:54:25 +0800188static void acpi_get_pm_info(MachineState *machine, AcpiPmInfo *pm)
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300189{
Philippe Mathieu-Daudé81c48dd2019-04-27 16:40:24 +0200190 Object *piix = object_resolve_type_unambiguous(TYPE_PIIX4_PM);
191 Object *lpc = object_resolve_type_unambiguous(TYPE_ICH9_LPC_DEVICE);
Igor Mammedov697155c2018-02-28 15:23:47 +0100192 Object *obj = piix ? piix : lpc;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300193 QObject *o;
Daniel P. Berrange94aaca62015-07-31 11:14:35 +0100194 pm->cpu_hp_io_base = 0;
Igor Mammedov500b11e2015-02-18 19:14:50 +0000195 pm->pcihp_io_base = 0;
196 pm->pcihp_io_len = 0;
Igor Mammedov937d1b52018-02-28 15:23:51 +0100197
Philippe Mathieu-Daudé6fa51712019-04-27 16:40:25 +0200198 assert(obj);
Like Xua0628592019-05-19 04:54:20 +0800199 init_common_fadt_data(machine, obj, &pm->fadt);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300200 if (piix) {
Igor Mammedov3a3fcc72017-07-24 15:50:20 +0200201 /* w2k requires FADT(rev1) or it won't boot, keep PC compatible */
Igor Mammedov937d1b52018-02-28 15:23:51 +0100202 pm->fadt.rev = 1;
Igor Mammedovddf1ec22015-02-18 19:14:44 +0000203 pm->cpu_hp_io_base = PIIX4_CPU_HOTPLUG_IO_BASE;
Igor Mammedov500b11e2015-02-18 19:14:50 +0000204 pm->pcihp_io_base =
Marc-André Lureau35f91e52017-06-07 20:36:19 +0400205 object_property_get_uint(obj, ACPI_PCIHP_IO_BASE_PROP, NULL);
Igor Mammedov500b11e2015-02-18 19:14:50 +0000206 pm->pcihp_io_len =
Marc-André Lureau35f91e52017-06-07 20:36:19 +0400207 object_property_get_uint(obj, ACPI_PCIHP_IO_LEN_PROP, NULL);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300208 }
209 if (lpc) {
Igor Mammedov937d1b52018-02-28 15:23:51 +0100210 struct AcpiGenericAddress r = { .space_id = AML_AS_SYSTEM_IO,
211 .bit_width = 8, .address = ICH9_RST_CNT_IOPORT };
212 pm->fadt.reset_reg = r;
213 pm->fadt.reset_val = 0xf;
214 pm->fadt.flags |= 1 << ACPI_FADT_F_RESET_REG_SUP;
Igor Mammedovddf1ec22015-02-18 19:14:44 +0000215 pm->cpu_hp_io_base = ICH9_CPU_HOTPLUG_IO_BASE;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300216 }
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300217
Igor Mammedov937d1b52018-02-28 15:23:51 +0100218 /* The above need not be conditional on machine type because the reset port
219 * happens to be the same on PIIX (pc) and ICH9 (q35). */
Philippe Mathieu-Daudé00634542019-02-02 20:48:46 +0100220 QEMU_BUILD_BUG_ON(ICH9_RST_CNT_IOPORT != PIIX_RCR_IOPORT);
Igor Mammedov937d1b52018-02-28 15:23:51 +0100221
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300222 /* Fill in optional s3/s4 related properties */
223 o = object_property_get_qobject(obj, ACPI_PM_PROP_S3_DISABLED, NULL);
224 if (o) {
Max Reitz7dc847e2018-02-24 16:40:29 +0100225 pm->s3_disabled = qnum_get_uint(qobject_to(QNum, o));
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300226 } else {
227 pm->s3_disabled = false;
228 }
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200229 qobject_unref(o);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300230 o = object_property_get_qobject(obj, ACPI_PM_PROP_S4_DISABLED, NULL);
231 if (o) {
Max Reitz7dc847e2018-02-24 16:40:29 +0100232 pm->s4_disabled = qnum_get_uint(qobject_to(QNum, o));
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300233 } else {
234 pm->s4_disabled = false;
235 }
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200236 qobject_unref(o);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300237 o = object_property_get_qobject(obj, ACPI_PM_PROP_S4_VAL, NULL);
238 if (o) {
Max Reitz7dc847e2018-02-24 16:40:29 +0100239 pm->s4_val = qnum_get_uint(qobject_to(QNum, o));
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300240 } else {
241 pm->s4_val = false;
242 }
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200243 qobject_unref(o);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300244
Igor Mammedov133a2da2014-07-28 17:34:18 +0200245 pm->pcihp_bridge_en =
246 object_property_get_bool(obj, "acpi-pci-hotplug-with-bridge-support",
247 NULL);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300248}
249
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300250static void acpi_get_misc_info(AcpiMiscInfo *info)
251{
Philippe Mathieu-Daudé81c48dd2019-04-27 16:40:24 +0200252 Object *piix = object_resolve_type_unambiguous(TYPE_PIIX4_PM);
253 Object *lpc = object_resolve_type_unambiguous(TYPE_ICH9_LPC_DEVICE);
Igor Mammedov3db119d2015-12-28 18:02:57 +0100254 assert(!!piix != !!lpc);
255
256 if (piix) {
257 info->is_piix4 = true;
258 }
259 if (lpc) {
260 info->is_piix4 = false;
261 }
262
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300263 info->has_hpet = hpet_find();
Marc-André Lureau3dfd5a22017-11-06 19:39:15 +0100264 info->tpm_version = tpm_get_version(tpm_find());
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300265 info->pvpanic_port = pvpanic_port();
Igor Mammedov8ac6f7a2015-02-20 18:22:12 +0000266 info->applesmc_io_base = applesmc_port();
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300267}
268
Marcel Apfelbaumca6c1852015-06-02 14:22:59 +0300269/*
270 * Because of the PXB hosts we cannot simply query TYPE_PCI_HOST_BRIDGE.
271 * On i386 arch we only have two pci hosts, so we can look only for them.
272 */
273static Object *acpi_get_i386_pci_host(void)
274{
275 PCIHostState *host;
276
277 host = OBJECT_CHECK(PCIHostState,
278 object_resolve_path("/machine/i440fx", NULL),
279 TYPE_PCI_HOST_BRIDGE);
280 if (!host) {
281 host = OBJECT_CHECK(PCIHostState,
282 object_resolve_path("/machine/q35", NULL),
283 TYPE_PCI_HOST_BRIDGE);
284 }
285
286 return OBJECT(host);
287}
288
Markus Armbruster01c97422016-06-15 19:56:31 +0200289static void acpi_get_pci_holes(Range *hole, Range *hole64)
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300290{
291 Object *pci_host;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300292
Marcel Apfelbaumca6c1852015-06-02 14:22:59 +0300293 pci_host = acpi_get_i386_pci_host();
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300294 g_assert(pci_host);
295
Markus Armbrustera0efbf12016-07-01 13:47:47 +0200296 range_set_bounds1(hole,
Marc-André Lureau60555362017-06-07 20:36:21 +0400297 object_property_get_uint(pci_host,
298 PCI_HOST_PROP_PCI_HOLE_START,
299 NULL),
300 object_property_get_uint(pci_host,
301 PCI_HOST_PROP_PCI_HOLE_END,
302 NULL));
Markus Armbrustera0efbf12016-07-01 13:47:47 +0200303 range_set_bounds1(hole64,
Marc-André Lureau60555362017-06-07 20:36:21 +0400304 object_property_get_uint(pci_host,
305 PCI_HOST_PROP_PCI_HOLE64_START,
306 NULL),
307 object_property_get_uint(pci_host,
308 PCI_HOST_PROP_PCI_HOLE64_END,
309 NULL));
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300310}
311
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300312static void acpi_align_size(GArray *blob, unsigned align)
313{
314 /* Align size to multiple of given size. This reduces the chance
315 * we need to change size in the future (breaking cross version migration).
316 */
Michael S. Tsirkin134d42d2013-11-26 00:00:39 +0200317 g_array_set_size(blob, ROUND_UP(acpi_data_len(blob), align));
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300318}
319
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300320/* FACS */
321static void
Wei Yang009180b2019-01-30 11:02:07 +0800322build_facs(GArray *table_data)
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300323{
324 AcpiFacsDescriptorRev1 *facs = acpi_data_push(table_data, sizeof *facs);
Michael S. Tsirkin821e3222014-03-18 15:49:41 +0200325 memcpy(&facs->signature, "FACS", 4);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300326 facs->length = cpu_to_le32(sizeof(*facs));
327}
328
Igor Mammedov62b52c22015-02-20 18:22:18 +0000329static void build_append_pcihp_notify_entry(Aml *method, int slot)
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300330{
Igor Mammedov62b52c22015-02-20 18:22:18 +0000331 Aml *if_ctx;
332 int32_t devfn = PCI_DEVFN(slot, 0);
Igor Mammedovb23046a2015-02-20 18:22:16 +0000333
Igor Mammedov55304272015-12-10 00:41:17 +0100334 if_ctx = aml_if(aml_and(aml_arg(0), aml_int(0x1U << slot), NULL));
Igor Mammedov62b52c22015-02-20 18:22:18 +0000335 aml_append(if_ctx, aml_notify(aml_name("S%.02X", devfn), aml_arg(1)));
336 aml_append(method, if_ctx);
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300337}
338
Igor Mammedov62b52c22015-02-20 18:22:18 +0000339static void build_append_pci_bus_devices(Aml *parent_scope, PCIBus *bus,
Igor Mammedovb23046a2015-02-20 18:22:16 +0000340 bool pcihp_bridge_en)
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300341{
Max Reitz7dc847e2018-02-24 16:40:29 +0100342 Aml *dev, *notify_method = NULL, *method;
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300343 QObject *bsel;
Igor Mammedovb23046a2015-02-20 18:22:16 +0000344 PCIBus *sec;
345 int i;
Igor Mammedov133a2da2014-07-28 17:34:18 +0200346
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300347 bsel = object_property_get_qobject(OBJECT(bus), ACPI_PCIHP_PROP_BSEL, NULL);
348 if (bsel) {
Max Reitz7dc847e2018-02-24 16:40:29 +0100349 uint64_t bsel_val = qnum_get_uint(qobject_to(QNum, bsel));
Igor Mammedov62b52c22015-02-20 18:22:18 +0000350
351 aml_append(parent_scope, aml_name_decl("BSEL", aml_int(bsel_val)));
Xiao Guangrong4dbfc882015-12-17 13:37:13 +0000352 notify_method = aml_method("DVNT", 2, AML_NOTSERIALIZED);
Michael S. Tsirkin8dcf5252014-02-04 17:43:47 +0200353 }
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300354
Michael S. Tsirkin8dcf5252014-02-04 17:43:47 +0200355 for (i = 0; i < ARRAY_SIZE(bus->devices); i += PCI_FUNC_MAX) {
356 DeviceClass *dc;
357 PCIDeviceClass *pc;
358 PCIDevice *pdev = bus->devices[i];
359 int slot = PCI_SLOT(i);
Igor Mammedovb23046a2015-02-20 18:22:16 +0000360 bool hotplug_enabled_dev;
Michael S. Tsirkin093a35e2014-07-28 22:56:45 +0200361 bool bridge_in_acpi;
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300362
Michael S. Tsirkin8dcf5252014-02-04 17:43:47 +0200363 if (!pdev) {
Igor Mammedovb23046a2015-02-20 18:22:16 +0000364 if (bsel) { /* add hotplug slots for non present devices */
Igor Mammedov62b52c22015-02-20 18:22:18 +0000365 dev = aml_device("S%.02X", PCI_DEVFN(slot, 0));
366 aml_append(dev, aml_name_decl("_SUN", aml_int(slot)));
367 aml_append(dev, aml_name_decl("_ADR", aml_int(slot << 16)));
Xiao Guangrong4dbfc882015-12-17 13:37:13 +0000368 method = aml_method("_EJ0", 1, AML_NOTSERIALIZED);
Igor Mammedov62b52c22015-02-20 18:22:18 +0000369 aml_append(method,
370 aml_call2("PCEJ", aml_name("BSEL"), aml_name("_SUN"))
371 );
372 aml_append(dev, method);
373 aml_append(parent_scope, dev);
Igor Mammedovb23046a2015-02-20 18:22:16 +0000374
Igor Mammedov62b52c22015-02-20 18:22:18 +0000375 build_append_pcihp_notify_entry(notify_method, slot);
Igor Mammedovb23046a2015-02-20 18:22:16 +0000376 }
Michael S. Tsirkin8dcf5252014-02-04 17:43:47 +0200377 continue;
378 }
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300379
Michael S. Tsirkin8dcf5252014-02-04 17:43:47 +0200380 pc = PCI_DEVICE_GET_CLASS(pdev);
381 dc = DEVICE_GET_CLASS(pdev);
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300382
Michael S. Tsirkin093a35e2014-07-28 22:56:45 +0200383 /* When hotplug for bridges is enabled, bridges are
384 * described in ACPI separately (see build_pci_bus_end).
385 * In this case they aren't themselves hot-pluggable.
Michael S. Tsirkina20275f2015-01-28 18:30:38 +0200386 * Hotplugged bridges *are* hot-pluggable.
Michael S. Tsirkin093a35e2014-07-28 22:56:45 +0200387 */
Igor Mammedovb23046a2015-02-20 18:22:16 +0000388 bridge_in_acpi = pc->is_bridge && pcihp_bridge_en &&
389 !DEVICE(pdev)->hotplugged;
Michael S. Tsirkin093a35e2014-07-28 22:56:45 +0200390
Igor Mammedovb23046a2015-02-20 18:22:16 +0000391 hotplug_enabled_dev = bsel && dc->hotpluggable && !bridge_in_acpi;
392
393 if (pc->class_id == PCI_CLASS_BRIDGE_ISA) {
394 continue;
Michael S. Tsirkin8dcf5252014-02-04 17:43:47 +0200395 }
396
Igor Mammedov62b52c22015-02-20 18:22:18 +0000397 /* start to compose PCI slot descriptor */
398 dev = aml_device("S%.02X", PCI_DEVFN(slot, 0));
399 aml_append(dev, aml_name_decl("_ADR", aml_int(slot << 16)));
400
Michael S. Tsirkin8dcf5252014-02-04 17:43:47 +0200401 if (pc->class_id == PCI_CLASS_DISPLAY_VGA) {
Igor Mammedov62b52c22015-02-20 18:22:18 +0000402 /* add VGA specific AML methods */
403 int s3d;
404
Michael S. Tsirkin8dcf5252014-02-04 17:43:47 +0200405 if (object_dynamic_cast(OBJECT(pdev), "qxl-vga")) {
Igor Mammedov62b52c22015-02-20 18:22:18 +0000406 s3d = 3;
Igor Mammedovb23046a2015-02-20 18:22:16 +0000407 } else {
Igor Mammedov62b52c22015-02-20 18:22:18 +0000408 s3d = 0;
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300409 }
Igor Mammedov62b52c22015-02-20 18:22:18 +0000410
Xiao Guangrong4dbfc882015-12-17 13:37:13 +0000411 method = aml_method("_S1D", 0, AML_NOTSERIALIZED);
Igor Mammedov62b52c22015-02-20 18:22:18 +0000412 aml_append(method, aml_return(aml_int(0)));
413 aml_append(dev, method);
414
Xiao Guangrong4dbfc882015-12-17 13:37:13 +0000415 method = aml_method("_S2D", 0, AML_NOTSERIALIZED);
Igor Mammedov62b52c22015-02-20 18:22:18 +0000416 aml_append(method, aml_return(aml_int(0)));
417 aml_append(dev, method);
418
Xiao Guangrong4dbfc882015-12-17 13:37:13 +0000419 method = aml_method("_S3D", 0, AML_NOTSERIALIZED);
Igor Mammedov62b52c22015-02-20 18:22:18 +0000420 aml_append(method, aml_return(aml_int(s3d)));
421 aml_append(dev, method);
Igor Mammedovb23046a2015-02-20 18:22:16 +0000422 } else if (hotplug_enabled_dev) {
Igor Mammedov62b52c22015-02-20 18:22:18 +0000423 /* add _SUN/_EJ0 to make slot hotpluggable */
424 aml_append(dev, aml_name_decl("_SUN", aml_int(slot)));
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300425
Xiao Guangrong4dbfc882015-12-17 13:37:13 +0000426 method = aml_method("_EJ0", 1, AML_NOTSERIALIZED);
Igor Mammedov62b52c22015-02-20 18:22:18 +0000427 aml_append(method,
428 aml_call2("PCEJ", aml_name("BSEL"), aml_name("_SUN"))
429 );
430 aml_append(dev, method);
431
432 if (bsel) {
433 build_append_pcihp_notify_entry(notify_method, slot);
434 }
Igor Mammedovb23046a2015-02-20 18:22:16 +0000435 } else if (bridge_in_acpi) {
Igor Mammedov62b52c22015-02-20 18:22:18 +0000436 /*
437 * device is coldplugged bridge,
438 * add child device descriptions into its scope
439 */
Igor Mammedovb23046a2015-02-20 18:22:16 +0000440 PCIBus *sec_bus = pci_bridge_get_sec_bus(PCI_BRIDGE(pdev));
Igor Mammedovb23046a2015-02-20 18:22:16 +0000441
Igor Mammedov62b52c22015-02-20 18:22:18 +0000442 build_append_pci_bus_devices(dev, sec_bus, pcihp_bridge_en);
Michael S. Tsirkin8dcf5252014-02-04 17:43:47 +0200443 }
Igor Mammedov62b52c22015-02-20 18:22:18 +0000444 /* slot descriptor has been composed, add it into parent context */
445 aml_append(parent_scope, dev);
Michael S. Tsirkin8dcf5252014-02-04 17:43:47 +0200446 }
447
448 if (bsel) {
Igor Mammedov62b52c22015-02-20 18:22:18 +0000449 aml_append(parent_scope, notify_method);
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300450 }
451
452 /* Append PCNT method to notify about events on local and child buses.
453 * Add unconditionally for root since DSDT expects it.
454 */
Xiao Guangrong4dbfc882015-12-17 13:37:13 +0000455 method = aml_method("PCNT", 0, AML_NOTSERIALIZED);
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300456
Igor Mammedovb23046a2015-02-20 18:22:16 +0000457 /* If bus supports hotplug select it and notify about local events */
458 if (bsel) {
Max Reitz7dc847e2018-02-24 16:40:29 +0100459 uint64_t bsel_val = qnum_get_uint(qobject_to(QNum, bsel));
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400460
Igor Mammedov62b52c22015-02-20 18:22:18 +0000461 aml_append(method, aml_store(aml_int(bsel_val), aml_name("BNUM")));
462 aml_append(method,
463 aml_call2("DVNT", aml_name("PCIU"), aml_int(1) /* Device Check */)
464 );
465 aml_append(method,
466 aml_call2("DVNT", aml_name("PCID"), aml_int(3)/* Eject Request */)
467 );
Igor Mammedovb23046a2015-02-20 18:22:16 +0000468 }
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300469
Igor Mammedovb23046a2015-02-20 18:22:16 +0000470 /* Notify about child bus events in any case */
471 if (pcihp_bridge_en) {
472 QLIST_FOREACH(sec, &bus->child, sibling) {
Igor Mammedov62b52c22015-02-20 18:22:18 +0000473 int32_t devfn = sec->parent_dev->devfn;
474
Marcel Apfelbaumc99cb182016-07-17 19:53:11 +0300475 if (pci_bus_is_root(sec) || pci_bus_is_express(sec)) {
476 continue;
477 }
478
Igor Mammedov62b52c22015-02-20 18:22:18 +0000479 aml_append(method, aml_name("^S%.02X.PCNT", devfn));
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300480 }
481 }
Igor Mammedov62b52c22015-02-20 18:22:18 +0000482 aml_append(parent_scope, method);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200483 qobject_unref(bsel);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300484}
485
Igor Mammedov196e2132015-12-28 18:02:42 +0100486/**
487 * build_prt_entry:
488 * @link_name: link name for PCI route entry
489 *
490 * build AML package containing a PCI route entry for @link_name
491 */
492static Aml *build_prt_entry(const char *link_name)
493{
494 Aml *a_zero = aml_int(0);
495 Aml *pkg = aml_package(4);
496 aml_append(pkg, a_zero);
497 aml_append(pkg, a_zero);
498 aml_append(pkg, aml_name("%s", link_name));
499 aml_append(pkg, a_zero);
500 return pkg;
501}
502
Marcel Apfelbaum0d8935e2015-06-02 14:23:02 +0300503/*
504 * initialize_route - Initialize the interrupt routing rule
505 * through a specific LINK:
506 * if (lnk_idx == idx)
507 * route using link 'link_name'
508 */
509static Aml *initialize_route(Aml *route, const char *link_name,
510 Aml *lnk_idx, int idx)
511{
512 Aml *if_ctx = aml_if(aml_equal(lnk_idx, aml_int(idx)));
Igor Mammedov196e2132015-12-28 18:02:42 +0100513 Aml *pkg = build_prt_entry(link_name);
Marcel Apfelbaum0d8935e2015-06-02 14:23:02 +0300514
Marcel Apfelbaum0d8935e2015-06-02 14:23:02 +0300515 aml_append(if_ctx, aml_store(pkg, route));
516
517 return if_ctx;
518}
519
520/*
521 * build_prt - Define interrupt rounting rules
522 *
523 * Returns an array of 128 routes, one for each device,
524 * based on device location.
525 * The main goal is to equaly distribute the interrupts
526 * over the 4 existing ACPI links (works only for i440fx).
527 * The hash function is (slot + pin) & 3 -> "LNK[D|A|B|C]".
528 *
529 */
Igor Mammedov196e2132015-12-28 18:02:42 +0100530static Aml *build_prt(bool is_pci0_prt)
Marcel Apfelbaum0d8935e2015-06-02 14:23:02 +0300531{
532 Aml *method, *while_ctx, *pin, *res;
533
Xiao Guangrong4dbfc882015-12-17 13:37:13 +0000534 method = aml_method("_PRT", 0, AML_NOTSERIALIZED);
Marcel Apfelbaum0d8935e2015-06-02 14:23:02 +0300535 res = aml_local(0);
536 pin = aml_local(1);
537 aml_append(method, aml_store(aml_package(128), res));
538 aml_append(method, aml_store(aml_int(0), pin));
539
540 /* while (pin < 128) */
541 while_ctx = aml_while(aml_lless(pin, aml_int(128)));
542 {
543 Aml *slot = aml_local(2);
544 Aml *lnk_idx = aml_local(3);
545 Aml *route = aml_local(4);
546
547 /* slot = pin >> 2 */
548 aml_append(while_ctx,
Igor Mammedovc3606392015-12-10 00:41:06 +0100549 aml_store(aml_shiftright(pin, aml_int(2), NULL), slot));
Marcel Apfelbaum0d8935e2015-06-02 14:23:02 +0300550 /* lnk_idx = (slot + pin) & 3 */
551 aml_append(while_ctx,
Igor Mammedov55304272015-12-10 00:41:17 +0100552 aml_store(aml_and(aml_add(pin, slot, NULL), aml_int(3), NULL),
553 lnk_idx));
Marcel Apfelbaum0d8935e2015-06-02 14:23:02 +0300554
555 /* route[2] = "LNK[D|A|B|C]", selection based on pin % 3 */
556 aml_append(while_ctx, initialize_route(route, "LNKD", lnk_idx, 0));
Igor Mammedov196e2132015-12-28 18:02:42 +0100557 if (is_pci0_prt) {
558 Aml *if_device_1, *if_pin_4, *else_pin_4;
559
560 /* device 1 is the power-management device, needs SCI */
561 if_device_1 = aml_if(aml_equal(lnk_idx, aml_int(1)));
562 {
563 if_pin_4 = aml_if(aml_equal(pin, aml_int(4)));
564 {
565 aml_append(if_pin_4,
566 aml_store(build_prt_entry("LNKS"), route));
567 }
568 aml_append(if_device_1, if_pin_4);
569 else_pin_4 = aml_else();
570 {
571 aml_append(else_pin_4,
572 aml_store(build_prt_entry("LNKA"), route));
573 }
574 aml_append(if_device_1, else_pin_4);
575 }
576 aml_append(while_ctx, if_device_1);
577 } else {
578 aml_append(while_ctx, initialize_route(route, "LNKA", lnk_idx, 1));
579 }
Marcel Apfelbaum0d8935e2015-06-02 14:23:02 +0300580 aml_append(while_ctx, initialize_route(route, "LNKB", lnk_idx, 2));
581 aml_append(while_ctx, initialize_route(route, "LNKC", lnk_idx, 3));
582
583 /* route[0] = 0x[slot]FFFF */
584 aml_append(while_ctx,
Igor Mammedovca3df952015-12-10 00:41:16 +0100585 aml_store(aml_or(aml_shiftleft(slot, aml_int(16)), aml_int(0xFFFF),
586 NULL),
Marcel Apfelbaum0d8935e2015-06-02 14:23:02 +0300587 aml_index(route, aml_int(0))));
588 /* route[1] = pin & 3 */
589 aml_append(while_ctx,
Igor Mammedov55304272015-12-10 00:41:17 +0100590 aml_store(aml_and(pin, aml_int(3), NULL),
591 aml_index(route, aml_int(1))));
Marcel Apfelbaum0d8935e2015-06-02 14:23:02 +0300592 /* res[pin] = route */
593 aml_append(while_ctx, aml_store(route, aml_index(res, pin)));
594 /* pin++ */
595 aml_append(while_ctx, aml_increment(pin));
596 }
597 aml_append(method, while_ctx);
598 /* return res*/
599 aml_append(method, aml_return(res));
600
601 return method;
602}
603
Marcel Apfelbauma43c6e22015-06-02 14:23:03 +0300604typedef struct CrsRangeEntry {
605 uint64_t base;
606 uint64_t limit;
607} CrsRangeEntry;
608
609static void crs_range_insert(GPtrArray *ranges, uint64_t base, uint64_t limit)
610{
611 CrsRangeEntry *entry;
612
613 entry = g_malloc(sizeof(*entry));
614 entry->base = base;
615 entry->limit = limit;
616
617 g_ptr_array_add(ranges, entry);
618}
619
620static void crs_range_free(gpointer data)
621{
622 CrsRangeEntry *entry = (CrsRangeEntry *)data;
623 g_free(entry);
624}
625
Marcel Apfelbaum2df5a7b2016-07-17 19:53:12 +0300626typedef struct CrsRangeSet {
627 GPtrArray *io_ranges;
628 GPtrArray *mem_ranges;
Marcel Apfelbaum16de88a2016-07-17 19:53:13 +0300629 GPtrArray *mem_64bit_ranges;
Marcel Apfelbaum2df5a7b2016-07-17 19:53:12 +0300630 } CrsRangeSet;
631
632static void crs_range_set_init(CrsRangeSet *range_set)
633{
634 range_set->io_ranges = g_ptr_array_new_with_free_func(crs_range_free);
635 range_set->mem_ranges = g_ptr_array_new_with_free_func(crs_range_free);
Marcel Apfelbaum16de88a2016-07-17 19:53:13 +0300636 range_set->mem_64bit_ranges =
637 g_ptr_array_new_with_free_func(crs_range_free);
Marcel Apfelbaum2df5a7b2016-07-17 19:53:12 +0300638}
639
640static void crs_range_set_free(CrsRangeSet *range_set)
641{
642 g_ptr_array_free(range_set->io_ranges, true);
643 g_ptr_array_free(range_set->mem_ranges, true);
Marcel Apfelbaum16de88a2016-07-17 19:53:13 +0300644 g_ptr_array_free(range_set->mem_64bit_ranges, true);
Marcel Apfelbaum2df5a7b2016-07-17 19:53:12 +0300645}
646
Marcel Apfelbaumdcdca292015-06-02 14:23:04 +0300647static gint crs_range_compare(gconstpointer a, gconstpointer b)
648{
Evgeny Yakovlev21e2acd2019-07-18 19:14:23 +0300649 CrsRangeEntry *entry_a = *(CrsRangeEntry **)a;
650 CrsRangeEntry *entry_b = *(CrsRangeEntry **)b;
Marcel Apfelbaumdcdca292015-06-02 14:23:04 +0300651
Evgeny Yakovlev21e2acd2019-07-18 19:14:23 +0300652 if (entry_a->base < entry_b->base) {
653 return -1;
654 } else if (entry_a->base > entry_b->base) {
655 return 1;
656 } else {
657 return 0;
658 }
Marcel Apfelbaumdcdca292015-06-02 14:23:04 +0300659}
660
661/*
662 * crs_replace_with_free_ranges - given the 'used' ranges within [start - end]
663 * interval, computes the 'free' ranges from the same interval.
664 * Example: If the input array is { [a1 - a2],[b1 - b2] }, the function
665 * will return { [base - a1], [a2 - b1], [b2 - limit] }.
666 */
667static void crs_replace_with_free_ranges(GPtrArray *ranges,
668 uint64_t start, uint64_t end)
669{
Marc-André Lureau354fb472016-07-13 12:56:01 +0200670 GPtrArray *free_ranges = g_ptr_array_new();
Marcel Apfelbaumdcdca292015-06-02 14:23:04 +0300671 uint64_t free_base = start;
672 int i;
673
674 g_ptr_array_sort(ranges, crs_range_compare);
675 for (i = 0; i < ranges->len; i++) {
676 CrsRangeEntry *used = g_ptr_array_index(ranges, i);
677
678 if (free_base < used->base) {
679 crs_range_insert(free_ranges, free_base, used->base - 1);
680 }
681
682 free_base = used->limit + 1;
683 }
684
685 if (free_base < end) {
686 crs_range_insert(free_ranges, free_base, end);
687 }
688
689 g_ptr_array_set_size(ranges, 0);
690 for (i = 0; i < free_ranges->len; i++) {
691 g_ptr_array_add(ranges, g_ptr_array_index(free_ranges, i));
692 }
693
Marc-André Lureau354fb472016-07-13 12:56:01 +0200694 g_ptr_array_free(free_ranges, true);
Marcel Apfelbaumdcdca292015-06-02 14:23:04 +0300695}
696
Marcel Apfelbaumd7fd0e62015-11-26 18:00:26 +0200697/*
698 * crs_range_merge - merges adjacent ranges in the given array.
699 * Array elements are deleted and replaced with the merged ranges.
700 */
701static void crs_range_merge(GPtrArray *range)
702{
703 GPtrArray *tmp = g_ptr_array_new_with_free_func(crs_range_free);
704 CrsRangeEntry *entry;
705 uint64_t range_base, range_limit;
706 int i;
707
708 if (!range->len) {
709 return;
710 }
711
712 g_ptr_array_sort(range, crs_range_compare);
713
714 entry = g_ptr_array_index(range, 0);
715 range_base = entry->base;
716 range_limit = entry->limit;
717 for (i = 1; i < range->len; i++) {
718 entry = g_ptr_array_index(range, i);
719 if (entry->base - 1 == range_limit) {
720 range_limit = entry->limit;
721 } else {
722 crs_range_insert(tmp, range_base, range_limit);
723 range_base = entry->base;
724 range_limit = entry->limit;
725 }
726 }
727 crs_range_insert(tmp, range_base, range_limit);
728
729 g_ptr_array_set_size(range, 0);
730 for (i = 0; i < tmp->len; i++) {
731 entry = g_ptr_array_index(tmp, i);
732 crs_range_insert(range, entry->base, entry->limit);
733 }
734 g_ptr_array_free(tmp, true);
735}
736
Marcel Apfelbaum2df5a7b2016-07-17 19:53:12 +0300737static Aml *build_crs(PCIHostState *host, CrsRangeSet *range_set)
Marcel Apfelbauma43c6e22015-06-02 14:23:03 +0300738{
739 Aml *crs = aml_resource_template();
Marcel Apfelbaum2df5a7b2016-07-17 19:53:12 +0300740 CrsRangeSet temp_range_set;
Marcel Apfelbaumd7fd0e62015-11-26 18:00:26 +0200741 CrsRangeEntry *entry;
Marcel Apfelbauma43c6e22015-06-02 14:23:03 +0300742 uint8_t max_bus = pci_bus_num(host->bus);
743 uint8_t type;
744 int devfn;
Marcel Apfelbaumd7fd0e62015-11-26 18:00:26 +0200745 int i;
Marcel Apfelbauma43c6e22015-06-02 14:23:03 +0300746
Marcel Apfelbaum2df5a7b2016-07-17 19:53:12 +0300747 crs_range_set_init(&temp_range_set);
Marcel Apfelbauma43c6e22015-06-02 14:23:03 +0300748 for (devfn = 0; devfn < ARRAY_SIZE(host->bus->devices); devfn++) {
Marcel Apfelbauma43c6e22015-06-02 14:23:03 +0300749 uint64_t range_base, range_limit;
750 PCIDevice *dev = host->bus->devices[devfn];
751
752 if (!dev) {
753 continue;
754 }
755
756 for (i = 0; i < PCI_NUM_REGIONS; i++) {
757 PCIIORegion *r = &dev->io_regions[i];
758
759 range_base = r->addr;
760 range_limit = r->addr + r->size - 1;
761
Marcel Apfelbaum0f6dd8e2015-06-02 14:23:11 +0300762 /*
763 * Work-around for old bioses
764 * that do not support multiple root buses
765 */
766 if (!range_base || range_base > range_limit) {
767 continue;
768 }
769
Marcel Apfelbauma43c6e22015-06-02 14:23:03 +0300770 if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
Marcel Apfelbaum2df5a7b2016-07-17 19:53:12 +0300771 crs_range_insert(temp_range_set.io_ranges,
772 range_base, range_limit);
Marcel Apfelbauma43c6e22015-06-02 14:23:03 +0300773 } else { /* "memory" */
Marcel Apfelbaum2df5a7b2016-07-17 19:53:12 +0300774 crs_range_insert(temp_range_set.mem_ranges,
775 range_base, range_limit);
Marcel Apfelbauma43c6e22015-06-02 14:23:03 +0300776 }
777 }
778
779 type = dev->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION;
780 if (type == PCI_HEADER_TYPE_BRIDGE) {
781 uint8_t subordinate = dev->config[PCI_SUBORDINATE_BUS];
782 if (subordinate > max_bus) {
783 max_bus = subordinate;
784 }
785
786 range_base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_IO);
787 range_limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_IO);
Marcel Apfelbaum0f6dd8e2015-06-02 14:23:11 +0300788
789 /*
790 * Work-around for old bioses
791 * that do not support multiple root buses
792 */
Laszlo Ersek4ebc7362015-06-11 02:37:59 +0200793 if (range_base && range_base <= range_limit) {
Marcel Apfelbaum2df5a7b2016-07-17 19:53:12 +0300794 crs_range_insert(temp_range_set.io_ranges,
795 range_base, range_limit);
Marcel Apfelbaum0f6dd8e2015-06-02 14:23:11 +0300796 }
Marcel Apfelbauma43c6e22015-06-02 14:23:03 +0300797
798 range_base =
799 pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_MEMORY);
800 range_limit =
801 pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_MEMORY);
Marcel Apfelbaum0f6dd8e2015-06-02 14:23:11 +0300802
803 /*
804 * Work-around for old bioses
805 * that do not support multiple root buses
806 */
Laszlo Ersek4ebc7362015-06-11 02:37:59 +0200807 if (range_base && range_base <= range_limit) {
Marcel Apfelbaum16de88a2016-07-17 19:53:13 +0300808 uint64_t length = range_limit - range_base + 1;
809 if (range_limit <= UINT32_MAX && length <= UINT32_MAX) {
810 crs_range_insert(temp_range_set.mem_ranges,
811 range_base, range_limit);
812 } else {
813 crs_range_insert(temp_range_set.mem_64bit_ranges,
814 range_base, range_limit);
815 }
Laszlo Ersek4ebc7362015-06-11 02:37:59 +0200816 }
Marcel Apfelbauma43c6e22015-06-02 14:23:03 +0300817
818 range_base =
819 pci_bridge_get_base(dev, PCI_BASE_ADDRESS_MEM_PREFETCH);
820 range_limit =
821 pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_MEM_PREFETCH);
Marcel Apfelbaum0f6dd8e2015-06-02 14:23:11 +0300822
823 /*
824 * Work-around for old bioses
825 * that do not support multiple root buses
826 */
Laszlo Ersek4ebc7362015-06-11 02:37:59 +0200827 if (range_base && range_base <= range_limit) {
Marcel Apfelbaum16de88a2016-07-17 19:53:13 +0300828 uint64_t length = range_limit - range_base + 1;
829 if (range_limit <= UINT32_MAX && length <= UINT32_MAX) {
830 crs_range_insert(temp_range_set.mem_ranges,
831 range_base, range_limit);
832 } else {
833 crs_range_insert(temp_range_set.mem_64bit_ranges,
834 range_base, range_limit);
835 }
Marcel Apfelbaum0f6dd8e2015-06-02 14:23:11 +0300836 }
Marcel Apfelbauma43c6e22015-06-02 14:23:03 +0300837 }
838 }
839
Marcel Apfelbaum2df5a7b2016-07-17 19:53:12 +0300840 crs_range_merge(temp_range_set.io_ranges);
841 for (i = 0; i < temp_range_set.io_ranges->len; i++) {
842 entry = g_ptr_array_index(temp_range_set.io_ranges, i);
Marcel Apfelbaumd7fd0e62015-11-26 18:00:26 +0200843 aml_append(crs,
844 aml_word_io(AML_MIN_FIXED, AML_MAX_FIXED,
845 AML_POS_DECODE, AML_ENTIRE_RANGE,
846 0, entry->base, entry->limit, 0,
847 entry->limit - entry->base + 1));
Marcel Apfelbaum2df5a7b2016-07-17 19:53:12 +0300848 crs_range_insert(range_set->io_ranges, entry->base, entry->limit);
Marcel Apfelbaumd7fd0e62015-11-26 18:00:26 +0200849 }
Marcel Apfelbaumd7fd0e62015-11-26 18:00:26 +0200850
Marcel Apfelbaum2df5a7b2016-07-17 19:53:12 +0300851 crs_range_merge(temp_range_set.mem_ranges);
852 for (i = 0; i < temp_range_set.mem_ranges->len; i++) {
853 entry = g_ptr_array_index(temp_range_set.mem_ranges, i);
Marcel Apfelbaumd7fd0e62015-11-26 18:00:26 +0200854 aml_append(crs,
855 aml_dword_memory(AML_POS_DECODE, AML_MIN_FIXED,
856 AML_MAX_FIXED, AML_NON_CACHEABLE,
857 AML_READ_WRITE,
858 0, entry->base, entry->limit, 0,
859 entry->limit - entry->base + 1));
Marcel Apfelbaum2df5a7b2016-07-17 19:53:12 +0300860 crs_range_insert(range_set->mem_ranges, entry->base, entry->limit);
Marcel Apfelbaumd7fd0e62015-11-26 18:00:26 +0200861 }
Marcel Apfelbaum2df5a7b2016-07-17 19:53:12 +0300862
Marcel Apfelbaum16de88a2016-07-17 19:53:13 +0300863 crs_range_merge(temp_range_set.mem_64bit_ranges);
864 for (i = 0; i < temp_range_set.mem_64bit_ranges->len; i++) {
865 entry = g_ptr_array_index(temp_range_set.mem_64bit_ranges, i);
866 aml_append(crs,
867 aml_qword_memory(AML_POS_DECODE, AML_MIN_FIXED,
868 AML_MAX_FIXED, AML_NON_CACHEABLE,
869 AML_READ_WRITE,
870 0, entry->base, entry->limit, 0,
871 entry->limit - entry->base + 1));
872 crs_range_insert(range_set->mem_64bit_ranges,
873 entry->base, entry->limit);
874 }
875
Marcel Apfelbaum2df5a7b2016-07-17 19:53:12 +0300876 crs_range_set_free(&temp_range_set);
Marcel Apfelbaumd7fd0e62015-11-26 18:00:26 +0200877
Marcel Apfelbauma43c6e22015-06-02 14:23:03 +0300878 aml_append(crs,
Marcel Apfelbaumdcdca292015-06-02 14:23:04 +0300879 aml_word_bus_number(AML_MIN_FIXED, AML_MAX_FIXED, AML_POS_DECODE,
Marcel Apfelbauma43c6e22015-06-02 14:23:03 +0300880 0,
881 pci_bus_num(host->bus),
882 max_bus,
883 0,
884 max_bus - pci_bus_num(host->bus) + 1));
885
886 return crs;
887}
888
Igor Mammedova57d7082015-12-28 18:02:29 +0100889static void build_hpet_aml(Aml *table)
890{
891 Aml *crs;
892 Aml *field;
893 Aml *method;
894 Aml *if_ctx;
895 Aml *scope = aml_scope("_SB");
896 Aml *dev = aml_device("HPET");
897 Aml *zero = aml_int(0);
898 Aml *id = aml_local(0);
899 Aml *period = aml_local(1);
900
901 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0103")));
902 aml_append(dev, aml_name_decl("_UID", zero));
903
904 aml_append(dev,
Xiao Guangrong3f3009c2016-03-01 18:56:05 +0800905 aml_operation_region("HPTM", AML_SYSTEM_MEMORY, aml_int(HPET_BASE),
906 HPET_LEN));
Igor Mammedova57d7082015-12-28 18:02:29 +0100907 field = aml_field("HPTM", AML_DWORD_ACC, AML_LOCK, AML_PRESERVE);
908 aml_append(field, aml_named_field("VEND", 32));
909 aml_append(field, aml_named_field("PRD", 32));
910 aml_append(dev, field);
911
912 method = aml_method("_STA", 0, AML_NOTSERIALIZED);
913 aml_append(method, aml_store(aml_name("VEND"), id));
914 aml_append(method, aml_store(aml_name("PRD"), period));
915 aml_append(method, aml_shiftright(id, aml_int(16), id));
916 if_ctx = aml_if(aml_lor(aml_equal(id, zero),
917 aml_equal(id, aml_int(0xffff))));
918 {
919 aml_append(if_ctx, aml_return(zero));
920 }
921 aml_append(method, if_ctx);
922
923 if_ctx = aml_if(aml_lor(aml_equal(period, zero),
924 aml_lgreater(period, aml_int(100000000))));
925 {
926 aml_append(if_ctx, aml_return(zero));
927 }
928 aml_append(method, if_ctx);
929
930 aml_append(method, aml_return(aml_int(0x0F)));
931 aml_append(dev, method);
932
933 crs = aml_resource_template();
934 aml_append(crs, aml_memory32_fixed(HPET_BASE, HPET_LEN, AML_READ_ONLY));
935 aml_append(dev, aml_name_decl("_CRS", crs));
936
937 aml_append(scope, dev);
938 aml_append(table, scope);
939}
940
Jon Doron6775d152020-04-24 15:34:43 +0300941static Aml *build_vmbus_device_aml(VMBusBridge *vmbus_bridge)
942{
943 Aml *dev;
944 Aml *method;
945 Aml *crs;
946
947 dev = aml_device("VMBS");
948 aml_append(dev, aml_name_decl("STA", aml_int(0xF)));
949 aml_append(dev, aml_name_decl("_HID", aml_string("VMBus")));
950 aml_append(dev, aml_name_decl("_UID", aml_int(0x0)));
951 aml_append(dev, aml_name_decl("_DDN", aml_string("VMBUS")));
952
953 method = aml_method("_DIS", 0, AML_NOTSERIALIZED);
954 aml_append(method, aml_store(aml_and(aml_name("STA"), aml_int(0xD), NULL),
955 aml_name("STA")));
956 aml_append(dev, method);
957
958 method = aml_method("_PS0", 0, AML_NOTSERIALIZED);
959 aml_append(method, aml_store(aml_or(aml_name("STA"), aml_int(0xF), NULL),
960 aml_name("STA")));
961 aml_append(dev, method);
962
963 method = aml_method("_STA", 0, AML_NOTSERIALIZED);
964 aml_append(method, aml_return(aml_name("STA")));
965 aml_append(dev, method);
966
967 aml_append(dev, aml_name_decl("_PS3", aml_int(0x0)));
968
969 crs = aml_resource_template();
Jon Doron8f06f222020-06-17 19:09:02 +0300970 aml_append(crs, aml_irq_no_flags(vmbus_bridge->irq));
Jon Doron6775d152020-04-24 15:34:43 +0300971 aml_append(dev, aml_name_decl("_CRS", crs));
972
973 return dev;
974}
975
Igor Mammedovee135842015-12-28 18:02:31 +0100976static void build_isa_devices_aml(Aml *table)
977{
Jon Doron6775d152020-04-24 15:34:43 +0300978 VMBusBridge *vmbus_bridge = vmbus_bridge_find();
Corey Minyard86e91dd2016-06-10 04:15:42 -0500979 bool ambiguous;
Corey Minyard86e91dd2016-06-10 04:15:42 -0500980 Object *obj = object_resolve_path_type("", TYPE_ISA_BUS, &ambiguous);
Gerd Hoffmann13371f92020-06-19 11:19:01 +0200981 Aml *scope;
Igor Mammedovee135842015-12-28 18:02:31 +0100982
Gerd Hoffmann13371f92020-06-19 11:19:01 +0200983 assert(obj && !ambiguous);
984
985 scope = aml_scope("_SB.PCI0.ISA");
986 build_acpi_ipmi_devices(scope, BUS(obj), "\\_SB.PCI0.ISA");
987 isa_build_aml(ISA_BUS(obj), scope);
Corey Minyard86e91dd2016-06-10 04:15:42 -0500988
Jon Doron6775d152020-04-24 15:34:43 +0300989 if (vmbus_bridge) {
990 aml_append(scope, build_vmbus_device_aml(vmbus_bridge));
991 }
992
Igor Mammedovee135842015-12-28 18:02:31 +0100993 aml_append(table, scope);
994}
995
Igor Mammedov3892a2b2015-12-28 18:02:30 +0100996static void build_dbg_aml(Aml *table)
997{
998 Aml *field;
999 Aml *method;
1000 Aml *while_ctx;
1001 Aml *scope = aml_scope("\\");
1002 Aml *buf = aml_local(0);
1003 Aml *len = aml_local(1);
1004 Aml *idx = aml_local(2);
1005
1006 aml_append(scope,
Xiao Guangrong3f3009c2016-03-01 18:56:05 +08001007 aml_operation_region("DBG", AML_SYSTEM_IO, aml_int(0x0402), 0x01));
Igor Mammedov3892a2b2015-12-28 18:02:30 +01001008 field = aml_field("DBG", AML_BYTE_ACC, AML_NOLOCK, AML_PRESERVE);
1009 aml_append(field, aml_named_field("DBGB", 8));
1010 aml_append(scope, field);
1011
1012 method = aml_method("DBUG", 1, AML_NOTSERIALIZED);
1013
1014 aml_append(method, aml_to_hexstring(aml_arg(0), buf));
1015 aml_append(method, aml_to_buffer(buf, buf));
1016 aml_append(method, aml_subtract(aml_sizeof(buf), aml_int(1), len));
1017 aml_append(method, aml_store(aml_int(0), idx));
1018
1019 while_ctx = aml_while(aml_lless(idx, len));
1020 aml_append(while_ctx,
1021 aml_store(aml_derefof(aml_index(buf, idx)), aml_name("DBGB")));
1022 aml_append(while_ctx, aml_increment(idx));
1023 aml_append(method, while_ctx);
1024
1025 aml_append(method, aml_store(aml_int(0x0A), aml_name("DBGB")));
1026 aml_append(scope, method);
1027
1028 aml_append(table, scope);
1029}
1030
Igor Mammedovc35b6e82015-12-28 18:02:39 +01001031static Aml *build_link_dev(const char *name, uint8_t uid, Aml *reg)
1032{
1033 Aml *dev;
1034 Aml *crs;
1035 Aml *method;
1036 uint32_t irqs[] = {5, 10, 11};
1037
1038 dev = aml_device("%s", name);
1039 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C0F")));
1040 aml_append(dev, aml_name_decl("_UID", aml_int(uid)));
1041
1042 crs = aml_resource_template();
1043 aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL, AML_ACTIVE_HIGH,
1044 AML_SHARED, irqs, ARRAY_SIZE(irqs)));
1045 aml_append(dev, aml_name_decl("_PRS", crs));
1046
1047 method = aml_method("_STA", 0, AML_NOTSERIALIZED);
1048 aml_append(method, aml_return(aml_call1("IQST", reg)));
1049 aml_append(dev, method);
1050
1051 method = aml_method("_DIS", 0, AML_NOTSERIALIZED);
1052 aml_append(method, aml_or(reg, aml_int(0x80), reg));
1053 aml_append(dev, method);
1054
1055 method = aml_method("_CRS", 0, AML_NOTSERIALIZED);
1056 aml_append(method, aml_return(aml_call1("IQCR", reg)));
1057 aml_append(dev, method);
1058
1059 method = aml_method("_SRS", 1, AML_NOTSERIALIZED);
1060 aml_append(method, aml_create_dword_field(aml_arg(0), aml_int(5), "PRRI"));
1061 aml_append(method, aml_store(aml_name("PRRI"), reg));
1062 aml_append(dev, method);
1063
1064 return dev;
1065 }
1066
Igor Mammedov80b32df2015-12-28 18:02:45 +01001067static Aml *build_gsi_link_dev(const char *name, uint8_t uid, uint8_t gsi)
1068{
1069 Aml *dev;
1070 Aml *crs;
1071 Aml *method;
1072 uint32_t irqs;
1073
1074 dev = aml_device("%s", name);
1075 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C0F")));
1076 aml_append(dev, aml_name_decl("_UID", aml_int(uid)));
1077
1078 crs = aml_resource_template();
1079 irqs = gsi;
1080 aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL, AML_ACTIVE_HIGH,
1081 AML_SHARED, &irqs, 1));
1082 aml_append(dev, aml_name_decl("_PRS", crs));
1083
1084 aml_append(dev, aml_name_decl("_CRS", crs));
1085
Marcel Apfelbaumc82f5032016-03-07 21:14:37 +02001086 /*
1087 * _DIS can be no-op because the interrupt cannot be disabled.
1088 */
1089 method = aml_method("_DIS", 0, AML_NOTSERIALIZED);
1090 aml_append(dev, method);
1091
Igor Mammedov80b32df2015-12-28 18:02:45 +01001092 method = aml_method("_SRS", 1, AML_NOTSERIALIZED);
1093 aml_append(dev, method);
1094
1095 return dev;
1096}
1097
Igor Mammedov16682a92015-12-28 18:02:47 +01001098/* _CRS method - get current settings */
1099static Aml *build_iqcr_method(bool is_piix4)
1100{
1101 Aml *if_ctx;
1102 uint32_t irqs;
1103 Aml *method = aml_method("IQCR", 1, AML_SERIALIZED);
1104 Aml *crs = aml_resource_template();
1105
1106 irqs = 0;
1107 aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL,
1108 AML_ACTIVE_HIGH, AML_SHARED, &irqs, 1));
1109 aml_append(method, aml_name_decl("PRR0", crs));
1110
1111 aml_append(method,
1112 aml_create_dword_field(aml_name("PRR0"), aml_int(5), "PRRI"));
1113
1114 if (is_piix4) {
1115 if_ctx = aml_if(aml_lless(aml_arg(0), aml_int(0x80)));
1116 aml_append(if_ctx, aml_store(aml_arg(0), aml_name("PRRI")));
1117 aml_append(method, if_ctx);
1118 } else {
1119 aml_append(method,
1120 aml_store(aml_and(aml_arg(0), aml_int(0xF), NULL),
1121 aml_name("PRRI")));
1122 }
1123
1124 aml_append(method, aml_return(aml_name("PRR0")));
1125 return method;
1126}
1127
Igor Mammedov78e1ad02015-12-28 18:02:48 +01001128/* _STA method - get status */
1129static Aml *build_irq_status_method(void)
1130{
1131 Aml *if_ctx;
1132 Aml *method = aml_method("IQST", 1, AML_NOTSERIALIZED);
1133
1134 if_ctx = aml_if(aml_and(aml_int(0x80), aml_arg(0), NULL));
1135 aml_append(if_ctx, aml_return(aml_int(0x09)));
1136 aml_append(method, if_ctx);
1137 aml_append(method, aml_return(aml_int(0x0B)));
1138 return method;
1139}
1140
Igor Mammedove4db2792015-12-28 18:02:37 +01001141static void build_piix4_pci0_int(Aml *table)
1142{
Igor Mammedovc35b6e82015-12-28 18:02:39 +01001143 Aml *dev;
1144 Aml *crs;
Igor Mammedove4db2792015-12-28 18:02:37 +01001145 Aml *field;
Igor Mammedovc35b6e82015-12-28 18:02:39 +01001146 Aml *method;
1147 uint32_t irqs;
Igor Mammedove4db2792015-12-28 18:02:37 +01001148 Aml *sb_scope = aml_scope("_SB");
Igor Mammedov196e2132015-12-28 18:02:42 +01001149 Aml *pci0_scope = aml_scope("PCI0");
1150
1151 aml_append(pci0_scope, build_prt(true));
1152 aml_append(sb_scope, pci0_scope);
Igor Mammedove4db2792015-12-28 18:02:37 +01001153
1154 field = aml_field("PCI0.ISA.P40C", AML_BYTE_ACC, AML_NOLOCK, AML_PRESERVE);
1155 aml_append(field, aml_named_field("PRQ0", 8));
1156 aml_append(field, aml_named_field("PRQ1", 8));
1157 aml_append(field, aml_named_field("PRQ2", 8));
1158 aml_append(field, aml_named_field("PRQ3", 8));
1159 aml_append(sb_scope, field);
1160
Igor Mammedov78e1ad02015-12-28 18:02:48 +01001161 aml_append(sb_scope, build_irq_status_method());
Igor Mammedov16682a92015-12-28 18:02:47 +01001162 aml_append(sb_scope, build_iqcr_method(true));
Igor Mammedov100681c2015-12-28 18:02:40 +01001163
Igor Mammedovc35b6e82015-12-28 18:02:39 +01001164 aml_append(sb_scope, build_link_dev("LNKA", 0, aml_name("PRQ0")));
1165 aml_append(sb_scope, build_link_dev("LNKB", 1, aml_name("PRQ1")));
1166 aml_append(sb_scope, build_link_dev("LNKC", 2, aml_name("PRQ2")));
1167 aml_append(sb_scope, build_link_dev("LNKD", 3, aml_name("PRQ3")));
1168
1169 dev = aml_device("LNKS");
1170 {
1171 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C0F")));
1172 aml_append(dev, aml_name_decl("_UID", aml_int(4)));
1173
1174 crs = aml_resource_template();
1175 irqs = 9;
1176 aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL,
1177 AML_ACTIVE_HIGH, AML_SHARED,
1178 &irqs, 1));
1179 aml_append(dev, aml_name_decl("_PRS", crs));
1180
1181 /* The SCI cannot be disabled and is always attached to GSI 9,
1182 * so these are no-ops. We only need this link to override the
1183 * polarity to active high and match the content of the MADT.
1184 */
1185 method = aml_method("_STA", 0, AML_NOTSERIALIZED);
1186 aml_append(method, aml_return(aml_int(0x0b)));
1187 aml_append(dev, method);
1188
1189 method = aml_method("_DIS", 0, AML_NOTSERIALIZED);
1190 aml_append(dev, method);
1191
1192 method = aml_method("_CRS", 0, AML_NOTSERIALIZED);
1193 aml_append(method, aml_return(aml_name("_PRS")));
1194 aml_append(dev, method);
1195
1196 method = aml_method("_SRS", 1, AML_NOTSERIALIZED);
1197 aml_append(dev, method);
1198 }
1199 aml_append(sb_scope, dev);
1200
Igor Mammedove4db2792015-12-28 18:02:37 +01001201 aml_append(table, sb_scope);
1202}
1203
Igor Mammedov22b5b8b2015-12-28 18:02:51 +01001204static void append_q35_prt_entry(Aml *ctx, uint32_t nr, const char *name)
1205{
1206 int i;
1207 int head;
1208 Aml *pkg;
1209 char base = name[3] < 'E' ? 'A' : 'E';
1210 char *s = g_strdup(name);
1211 Aml *a_nr = aml_int((nr << 16) | 0xffff);
1212
1213 assert(strlen(s) == 4);
1214
1215 head = name[3] - base;
1216 for (i = 0; i < 4; i++) {
1217 if (head + i > 3) {
1218 head = i * -1;
1219 }
1220 s[3] = base + head + i;
1221 pkg = aml_package(4);
1222 aml_append(pkg, a_nr);
1223 aml_append(pkg, aml_int(i));
1224 aml_append(pkg, aml_name("%s", s));
1225 aml_append(pkg, aml_int(0));
1226 aml_append(ctx, pkg);
1227 }
1228 g_free(s);
1229}
1230
1231static Aml *build_q35_routing_table(const char *str)
1232{
1233 int i;
1234 Aml *pkg;
1235 char *name = g_strdup_printf("%s ", str);
1236
1237 pkg = aml_package(128);
1238 for (i = 0; i < 0x18; i++) {
1239 name[3] = 'E' + (i & 0x3);
1240 append_q35_prt_entry(pkg, i, name);
1241 }
1242
1243 name[3] = 'E';
1244 append_q35_prt_entry(pkg, 0x18, name);
1245
1246 /* INTA -> PIRQA for slot 25 - 31, see the default value of D<N>IR */
1247 for (i = 0x0019; i < 0x1e; i++) {
1248 name[3] = 'A';
1249 append_q35_prt_entry(pkg, i, name);
1250 }
1251
1252 /* PCIe->PCI bridge. use PIRQ[E-H] */
1253 name[3] = 'E';
1254 append_q35_prt_entry(pkg, 0x1e, name);
1255 name[3] = 'A';
1256 append_q35_prt_entry(pkg, 0x1f, name);
1257
1258 g_free(name);
1259 return pkg;
1260}
1261
Igor Mammedov80b32df2015-12-28 18:02:45 +01001262static void build_q35_pci0_int(Aml *table)
1263{
Igor Mammedov41f95a52015-12-28 18:02:49 +01001264 Aml *field;
Igor Mammedov0dafe3b2015-12-28 18:02:50 +01001265 Aml *method;
Igor Mammedov80b32df2015-12-28 18:02:45 +01001266 Aml *sb_scope = aml_scope("_SB");
Igor Mammedov0dafe3b2015-12-28 18:02:50 +01001267 Aml *pci0_scope = aml_scope("PCI0");
1268
Igor Mammedove9fce792015-12-28 18:02:53 +01001269 /* Zero => PIC mode, One => APIC Mode */
1270 aml_append(table, aml_name_decl("PICF", aml_int(0)));
1271 method = aml_method("_PIC", 1, AML_NOTSERIALIZED);
1272 {
1273 aml_append(method, aml_store(aml_arg(0), aml_name("PICF")));
1274 }
1275 aml_append(table, method);
1276
Igor Mammedov22b5b8b2015-12-28 18:02:51 +01001277 aml_append(pci0_scope,
Igor Mammedov65aef4d2015-12-28 18:02:52 +01001278 aml_name_decl("PRTP", build_q35_routing_table("LNK")));
1279 aml_append(pci0_scope,
Igor Mammedov22b5b8b2015-12-28 18:02:51 +01001280 aml_name_decl("PRTA", build_q35_routing_table("GSI")));
1281
Igor Mammedov0dafe3b2015-12-28 18:02:50 +01001282 method = aml_method("_PRT", 0, AML_NOTSERIALIZED);
1283 {
1284 Aml *if_ctx;
1285 Aml *else_ctx;
1286
1287 /* PCI IRQ routing table, example from ACPI 2.0a specification,
1288 section 6.2.8.1 */
1289 /* Note: we provide the same info as the PCI routing
1290 table of the Bochs BIOS */
1291 if_ctx = aml_if(aml_equal(aml_name("PICF"), aml_int(0)));
1292 aml_append(if_ctx, aml_return(aml_name("PRTP")));
1293 aml_append(method, if_ctx);
1294 else_ctx = aml_else();
1295 aml_append(else_ctx, aml_return(aml_name("PRTA")));
1296 aml_append(method, else_ctx);
1297 }
1298 aml_append(pci0_scope, method);
1299 aml_append(sb_scope, pci0_scope);
Igor Mammedov80b32df2015-12-28 18:02:45 +01001300
Igor Mammedov41f95a52015-12-28 18:02:49 +01001301 field = aml_field("PCI0.ISA.PIRQ", AML_BYTE_ACC, AML_NOLOCK, AML_PRESERVE);
1302 aml_append(field, aml_named_field("PRQA", 8));
1303 aml_append(field, aml_named_field("PRQB", 8));
1304 aml_append(field, aml_named_field("PRQC", 8));
1305 aml_append(field, aml_named_field("PRQD", 8));
1306 aml_append(field, aml_reserved_field(0x20));
1307 aml_append(field, aml_named_field("PRQE", 8));
1308 aml_append(field, aml_named_field("PRQF", 8));
1309 aml_append(field, aml_named_field("PRQG", 8));
1310 aml_append(field, aml_named_field("PRQH", 8));
1311 aml_append(sb_scope, field);
1312
Igor Mammedov78e1ad02015-12-28 18:02:48 +01001313 aml_append(sb_scope, build_irq_status_method());
Igor Mammedov16682a92015-12-28 18:02:47 +01001314 aml_append(sb_scope, build_iqcr_method(false));
1315
Igor Mammedov12e3b1f2015-12-28 18:02:46 +01001316 aml_append(sb_scope, build_link_dev("LNKA", 0, aml_name("PRQA")));
1317 aml_append(sb_scope, build_link_dev("LNKB", 1, aml_name("PRQB")));
1318 aml_append(sb_scope, build_link_dev("LNKC", 2, aml_name("PRQC")));
1319 aml_append(sb_scope, build_link_dev("LNKD", 3, aml_name("PRQD")));
1320 aml_append(sb_scope, build_link_dev("LNKE", 4, aml_name("PRQE")));
1321 aml_append(sb_scope, build_link_dev("LNKF", 5, aml_name("PRQF")));
1322 aml_append(sb_scope, build_link_dev("LNKG", 6, aml_name("PRQG")));
1323 aml_append(sb_scope, build_link_dev("LNKH", 7, aml_name("PRQH")));
1324
Marcel Apfelbaum6a991e02016-03-13 13:40:29 +02001325 aml_append(sb_scope, build_gsi_link_dev("GSIA", 0x10, 0x10));
1326 aml_append(sb_scope, build_gsi_link_dev("GSIB", 0x11, 0x11));
1327 aml_append(sb_scope, build_gsi_link_dev("GSIC", 0x12, 0x12));
1328 aml_append(sb_scope, build_gsi_link_dev("GSID", 0x13, 0x13));
1329 aml_append(sb_scope, build_gsi_link_dev("GSIE", 0x14, 0x14));
1330 aml_append(sb_scope, build_gsi_link_dev("GSIF", 0x15, 0x15));
1331 aml_append(sb_scope, build_gsi_link_dev("GSIG", 0x16, 0x16));
1332 aml_append(sb_scope, build_gsi_link_dev("GSIH", 0x17, 0x17));
Igor Mammedov80b32df2015-12-28 18:02:45 +01001333
1334 aml_append(table, sb_scope);
1335}
1336
Igor Mammedov41f95a52015-12-28 18:02:49 +01001337static void build_q35_isa_bridge(Aml *table)
1338{
1339 Aml *dev;
1340 Aml *scope;
Igor Mammedov41f95a52015-12-28 18:02:49 +01001341
1342 scope = aml_scope("_SB.PCI0");
1343 dev = aml_device("ISA");
1344 aml_append(dev, aml_name_decl("_ADR", aml_int(0x001F0000)));
1345
1346 /* ICH9 PCI to ISA irq remapping */
1347 aml_append(dev, aml_operation_region("PIRQ", AML_PCI_CONFIG,
Xiao Guangrong3f3009c2016-03-01 18:56:05 +08001348 aml_int(0x60), 0x0C));
Igor Mammedov41f95a52015-12-28 18:02:49 +01001349
Igor Mammedov41f95a52015-12-28 18:02:49 +01001350 aml_append(scope, dev);
1351 aml_append(table, scope);
1352}
1353
Igor Mammedove4db2792015-12-28 18:02:37 +01001354static void build_piix4_isa_bridge(Aml *table)
1355{
1356 Aml *dev;
1357 Aml *scope;
Igor Mammedove4db2792015-12-28 18:02:37 +01001358
1359 scope = aml_scope("_SB.PCI0");
1360 dev = aml_device("ISA");
1361 aml_append(dev, aml_name_decl("_ADR", aml_int(0x00010000)));
1362
1363 /* PIIX PCI to ISA irq remapping */
1364 aml_append(dev, aml_operation_region("P40C", AML_PCI_CONFIG,
Xiao Guangrong3f3009c2016-03-01 18:56:05 +08001365 aml_int(0x60), 0x04));
Igor Mammedove4db2792015-12-28 18:02:37 +01001366
1367 aml_append(scope, dev);
1368 aml_append(table, scope);
1369}
1370
Igor Mammedovb616ec42015-12-28 18:02:43 +01001371static void build_piix4_pci_hotplug(Aml *table)
1372{
1373 Aml *scope;
1374 Aml *field;
1375 Aml *method;
1376
1377 scope = aml_scope("_SB.PCI0");
1378
1379 aml_append(scope,
Xiao Guangrong3f3009c2016-03-01 18:56:05 +08001380 aml_operation_region("PCST", AML_SYSTEM_IO, aml_int(0xae00), 0x08));
Igor Mammedovb616ec42015-12-28 18:02:43 +01001381 field = aml_field("PCST", AML_DWORD_ACC, AML_NOLOCK, AML_WRITE_AS_ZEROS);
1382 aml_append(field, aml_named_field("PCIU", 32));
1383 aml_append(field, aml_named_field("PCID", 32));
1384 aml_append(scope, field);
1385
1386 aml_append(scope,
Xiao Guangrong3f3009c2016-03-01 18:56:05 +08001387 aml_operation_region("SEJ", AML_SYSTEM_IO, aml_int(0xae08), 0x04));
Igor Mammedovb616ec42015-12-28 18:02:43 +01001388 field = aml_field("SEJ", AML_DWORD_ACC, AML_NOLOCK, AML_WRITE_AS_ZEROS);
1389 aml_append(field, aml_named_field("B0EJ", 32));
1390 aml_append(scope, field);
1391
1392 aml_append(scope,
Xiao Guangrong3f3009c2016-03-01 18:56:05 +08001393 aml_operation_region("BNMR", AML_SYSTEM_IO, aml_int(0xae10), 0x04));
Igor Mammedovb616ec42015-12-28 18:02:43 +01001394 field = aml_field("BNMR", AML_DWORD_ACC, AML_NOLOCK, AML_WRITE_AS_ZEROS);
1395 aml_append(field, aml_named_field("BNUM", 32));
1396 aml_append(scope, field);
1397
1398 aml_append(scope, aml_mutex("BLCK", 0));
1399
1400 method = aml_method("PCEJ", 2, AML_NOTSERIALIZED);
1401 aml_append(method, aml_acquire(aml_name("BLCK"), 0xFFFF));
1402 aml_append(method, aml_store(aml_arg(0), aml_name("BNUM")));
1403 aml_append(method,
1404 aml_store(aml_shiftleft(aml_int(1), aml_arg(1)), aml_name("B0EJ")));
1405 aml_append(method, aml_release(aml_name("BLCK")));
1406 aml_append(method, aml_return(aml_int(0)));
1407 aml_append(scope, method);
1408
1409 aml_append(table, scope);
1410}
1411
Igor Mammedovf97a88a2015-12-28 18:02:54 +01001412static Aml *build_q35_osc_method(void)
1413{
1414 Aml *if_ctx;
1415 Aml *if_ctx2;
1416 Aml *else_ctx;
1417 Aml *method;
1418 Aml *a_cwd1 = aml_name("CDW1");
Michael S. Tsirkinb3c782d2017-02-28 16:13:28 +02001419 Aml *a_ctrl = aml_local(0);
Igor Mammedovf97a88a2015-12-28 18:02:54 +01001420
1421 method = aml_method("_OSC", 4, AML_NOTSERIALIZED);
1422 aml_append(method, aml_create_dword_field(aml_arg(3), aml_int(0), "CDW1"));
1423
1424 if_ctx = aml_if(aml_equal(
1425 aml_arg(0), aml_touuid("33DB4D5B-1FF7-401C-9657-7441C03DD766")));
1426 aml_append(if_ctx, aml_create_dword_field(aml_arg(3), aml_int(4), "CDW2"));
1427 aml_append(if_ctx, aml_create_dword_field(aml_arg(3), aml_int(8), "CDW3"));
1428
Igor Mammedovf97a88a2015-12-28 18:02:54 +01001429 aml_append(if_ctx, aml_store(aml_name("CDW3"), a_ctrl));
1430
1431 /*
1432 * Always allow native PME, AER (no dependencies)
Aleksandr Bezzubikova41c78c2017-07-29 02:37:49 +03001433 * Allow SHPC (PCI bridges can have SHPC controller)
Igor Mammedovf97a88a2015-12-28 18:02:54 +01001434 */
Aleksandr Bezzubikova41c78c2017-07-29 02:37:49 +03001435 aml_append(if_ctx, aml_and(a_ctrl, aml_int(0x1F), a_ctrl));
Igor Mammedovf97a88a2015-12-28 18:02:54 +01001436
1437 if_ctx2 = aml_if(aml_lnot(aml_equal(aml_arg(1), aml_int(1))));
1438 /* Unknown revision */
1439 aml_append(if_ctx2, aml_or(a_cwd1, aml_int(0x08), a_cwd1));
1440 aml_append(if_ctx, if_ctx2);
1441
1442 if_ctx2 = aml_if(aml_lnot(aml_equal(aml_name("CDW3"), a_ctrl)));
1443 /* Capabilities bits were masked */
1444 aml_append(if_ctx2, aml_or(a_cwd1, aml_int(0x10), a_cwd1));
1445 aml_append(if_ctx, if_ctx2);
1446
1447 /* Update DWORD3 in the buffer */
1448 aml_append(if_ctx, aml_store(a_ctrl, aml_name("CDW3")));
1449 aml_append(method, if_ctx);
1450
1451 else_ctx = aml_else();
1452 /* Unrecognized UUID */
1453 aml_append(else_ctx, aml_or(a_cwd1, aml_int(4), a_cwd1));
1454 aml_append(method, else_ctx);
1455
1456 aml_append(method, aml_return(aml_arg(3)));
1457 return method;
1458}
Igor Mammedovb616ec42015-12-28 18:02:43 +01001459
Corey Minyardebe15582016-05-12 20:43:45 -05001460static void build_smb0(Aml *table, I2CBus *smbus, int devnr, int func)
1461{
1462 Aml *scope = aml_scope("_SB.PCI0");
1463 Aml *dev = aml_device("SMB0");
1464
Corey Minyardebe15582016-05-12 20:43:45 -05001465 aml_append(dev, aml_name_decl("_ADR", aml_int(devnr << 16 | func)));
1466 build_acpi_ipmi_devices(dev, BUS(smbus), "\\_SB.PCI0.SMB0");
1467 aml_append(scope, dev);
1468 aml_append(table, scope);
1469}
1470
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001471static void
Igor Mammedov0e9b9ed2016-05-19 15:19:25 +02001472build_dsdt(GArray *table_data, BIOSLinker *linker,
Igor Mammedovadcb89d2016-02-26 14:59:26 +01001473 AcpiPmInfo *pm, AcpiMiscInfo *misc,
Markus Armbruster01c97422016-06-15 19:56:31 +02001474 Range *pci_hole, Range *pci_hole64, MachineState *machine)
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001475{
Igor Mammedov41fa5c02016-01-22 15:36:06 +01001476 CrsRangeEntry *entry;
1477 Aml *dsdt, *sb_scope, *scope, *dev, *method, *field, *pkg, *crs;
Marcel Apfelbaum2df5a7b2016-07-17 19:53:12 +03001478 CrsRangeSet crs_range_set;
Eduardo Habkostfb306ff2015-12-11 16:42:26 -02001479 PCMachineState *pcms = PC_MACHINE(machine);
Igor Mammedov679dd1a2016-06-15 11:25:23 +02001480 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(machine);
Paolo Bonzinif0bb2762019-10-22 09:39:50 +02001481 X86MachineState *x86ms = X86_MACHINE(machine);
Gerd Hoffmann4a441832019-06-07 09:34:29 +02001482 AcpiMcfgInfo mcfg;
Igor Mammedovbef34922014-06-02 15:25:26 +02001483 uint32_t nr_mem = machine->ram_slots;
Marcel Apfelbaumdcdca292015-06-02 14:23:04 +03001484 int root_bus_limit = 0xFF;
Igor Mammedov41fa5c02016-01-22 15:36:06 +01001485 PCIBus *bus = NULL;
Stefan Bergerac6dd312019-01-15 02:27:52 +04001486 TPMIf *tpm = tpm_find();
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001487 int i;
1488
Igor Mammedov41fa5c02016-01-22 15:36:06 +01001489 dsdt = init_aml_allocator();
Laszlo Ersek2fd71f12014-03-17 17:05:17 +01001490
Igor Mammedov4ec8d2b2015-02-20 18:22:09 +00001491 /* Reserve space for header */
Igor Mammedov41fa5c02016-01-22 15:36:06 +01001492 acpi_data_push(dsdt->buf, sizeof(AcpiTableHeader));
1493
1494 build_dbg_aml(dsdt);
1495 if (misc->is_piix4) {
1496 sb_scope = aml_scope("_SB");
1497 dev = aml_device("PCI0");
1498 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A03")));
1499 aml_append(dev, aml_name_decl("_ADR", aml_int(0)));
Michael S. Tsirkinaf1b80a2020-07-30 11:44:06 -04001500 aml_append(dev, aml_name_decl("_UID", aml_int(0)));
Igor Mammedov41fa5c02016-01-22 15:36:06 +01001501 aml_append(sb_scope, dev);
1502 aml_append(dsdt, sb_scope);
1503
1504 build_hpet_aml(dsdt);
Igor Mammedov41fa5c02016-01-22 15:36:06 +01001505 build_piix4_isa_bridge(dsdt);
1506 build_isa_devices_aml(dsdt);
1507 build_piix4_pci_hotplug(dsdt);
1508 build_piix4_pci0_int(dsdt);
1509 } else {
1510 sb_scope = aml_scope("_SB");
Igor Mammedov41fa5c02016-01-22 15:36:06 +01001511 dev = aml_device("PCI0");
1512 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A08")));
1513 aml_append(dev, aml_name_decl("_CID", aml_eisaid("PNP0A03")));
1514 aml_append(dev, aml_name_decl("_ADR", aml_int(0)));
Michael S. Tsirkinaf1b80a2020-07-30 11:44:06 -04001515 aml_append(dev, aml_name_decl("_UID", aml_int(0)));
Igor Mammedov41fa5c02016-01-22 15:36:06 +01001516 aml_append(dev, build_q35_osc_method());
1517 aml_append(sb_scope, dev);
1518 aml_append(dsdt, sb_scope);
1519
1520 build_hpet_aml(dsdt);
1521 build_q35_isa_bridge(dsdt);
1522 build_isa_devices_aml(dsdt);
1523 build_q35_pci0_int(dsdt);
Corey Minyardebe15582016-05-12 20:43:45 -05001524 if (pcms->smbus && !pcmc->do_not_add_smb_acpi) {
1525 build_smb0(dsdt, pcms->smbus, ICH9_SMB_DEV, ICH9_SMB_FUNC);
1526 }
Igor Mammedov41fa5c02016-01-22 15:36:06 +01001527 }
1528
Igor Mammedov679dd1a2016-06-15 11:25:23 +02001529 if (pcmc->legacy_cpu_hotplug) {
1530 build_legacy_cpu_hotplug_aml(dsdt, machine, pm->cpu_hp_io_base);
1531 } else {
1532 CPUHotplugFeatures opts = {
Dr. David Alan Gilbert89cb0c02019-01-25 09:40:46 +00001533 .acpi_1_compatible = true, .has_legacy_cphp = true
Igor Mammedov679dd1a2016-06-15 11:25:23 +02001534 };
1535 build_cpus_aml(dsdt, machine, opts, pm->cpu_hp_io_base,
1536 "\\_SB.PCI0", "\\_GPE._E02");
1537 }
Shameer Kolothum091c4662019-09-18 14:06:23 +01001538
1539 if (pcms->memhp_io_base && nr_mem) {
1540 build_memory_hotplug_aml(dsdt, nr_mem, "\\_SB.PCI0",
1541 "\\_GPE._E03", AML_SYSTEM_IO,
1542 pcms->memhp_io_base);
1543 }
Igor Mammedov41fa5c02016-01-22 15:36:06 +01001544
1545 scope = aml_scope("_GPE");
1546 {
1547 aml_append(scope, aml_name_decl("_HID", aml_string("ACPI0006")));
1548
Igor Mammedov41fa5c02016-01-22 15:36:06 +01001549 if (misc->is_piix4) {
1550 method = aml_method("_E01", 0, AML_NOTSERIALIZED);
1551 aml_append(method,
1552 aml_acquire(aml_name("\\_SB.PCI0.BLCK"), 0xFFFF));
1553 aml_append(method, aml_call0("\\_SB.PCI0.PCNT"));
1554 aml_append(method, aml_release(aml_name("\\_SB.PCI0.BLCK")));
1555 aml_append(scope, method);
Igor Mammedov41fa5c02016-01-22 15:36:06 +01001556 }
1557
Eric Augerf6a0d062019-03-08 19:20:53 +01001558 if (machine->nvdimms_state->is_enabled) {
Xiao Guangrongb097cc52016-10-29 00:35:40 +08001559 method = aml_method("_E04", 0, AML_NOTSERIALIZED);
1560 aml_append(method, aml_notify(aml_name("\\_SB.NVDR"),
1561 aml_int(0x80)));
1562 aml_append(scope, method);
1563 }
Igor Mammedov41fa5c02016-01-22 15:36:06 +01001564 }
1565 aml_append(dsdt, scope);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001566
Marcel Apfelbaum2df5a7b2016-07-17 19:53:12 +03001567 crs_range_set_init(&crs_range_set);
Marcel Apfelbaum81ed6482015-11-26 18:00:28 +02001568 bus = PC_MACHINE(machine)->bus;
Marcel Apfelbauma4894202015-06-02 14:23:01 +03001569 if (bus) {
1570 QLIST_FOREACH(bus, &bus->child, sibling) {
1571 uint8_t bus_num = pci_bus_num(bus);
Marcel Apfelbaum0e79e512015-06-02 14:23:10 +03001572 uint8_t numa_node = pci_bus_numa_node(bus);
Marcel Apfelbauma4894202015-06-02 14:23:01 +03001573
1574 /* look only for expander root buses */
1575 if (!pci_bus_is_root(bus)) {
1576 continue;
1577 }
1578
Marcel Apfelbaumdcdca292015-06-02 14:23:04 +03001579 if (bus_num < root_bus_limit) {
1580 root_bus_limit = bus_num - 1;
1581 }
1582
Marcel Apfelbauma4894202015-06-02 14:23:01 +03001583 scope = aml_scope("\\_SB");
1584 dev = aml_device("PC%.02X", bus_num);
Laszlo Ersekc96d9282015-06-11 02:37:58 +02001585 aml_append(dev, aml_name_decl("_UID", aml_int(bus_num)));
Marcel Apfelbauma4894202015-06-02 14:23:01 +03001586 aml_append(dev, aml_name_decl("_BBN", aml_int(bus_num)));
Marcel Apfelbaum077dd742017-02-28 16:13:29 +02001587 if (pci_bus_is_express(bus)) {
Evgeny Yakovlevee4b0c82019-07-19 11:54:29 +03001588 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A08")));
1589 aml_append(dev, aml_name_decl("_CID", aml_eisaid("PNP0A03")));
Marcel Apfelbaum077dd742017-02-28 16:13:29 +02001590 aml_append(dev, build_q35_osc_method());
Evgeny Yakovlevee4b0c82019-07-19 11:54:29 +03001591 } else {
1592 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A03")));
Marcel Apfelbaum077dd742017-02-28 16:13:29 +02001593 }
Marcel Apfelbaum0e79e512015-06-02 14:23:10 +03001594
1595 if (numa_node != NUMA_NODE_UNASSIGNED) {
1596 aml_append(dev, aml_name_decl("_PXM", aml_int(numa_node)));
1597 }
1598
Igor Mammedov196e2132015-12-28 18:02:42 +01001599 aml_append(dev, build_prt(false));
Marcel Apfelbaum2df5a7b2016-07-17 19:53:12 +03001600 crs = build_crs(PCI_HOST_BRIDGE(BUS(bus)->parent), &crs_range_set);
Marcel Apfelbauma43c6e22015-06-02 14:23:03 +03001601 aml_append(dev, aml_name_decl("_CRS", crs));
Marcel Apfelbauma4894202015-06-02 14:23:01 +03001602 aml_append(scope, dev);
Igor Mammedov41fa5c02016-01-22 15:36:06 +01001603 aml_append(dsdt, scope);
Marcel Apfelbauma4894202015-06-02 14:23:01 +03001604 }
1605 }
1606
Gerd Hoffmann4a441832019-06-07 09:34:29 +02001607 /*
1608 * At this point crs_range_set has all the ranges used by pci
1609 * busses *other* than PCI0. These ranges will be excluded from
1610 * the PCI0._CRS. Add mmconfig to the set so it will be excluded
1611 * too.
1612 */
1613 if (acpi_get_mcfg(&mcfg)) {
1614 crs_range_insert(crs_range_set.mem_ranges,
1615 mcfg.base, mcfg.base + mcfg.size - 1);
1616 }
1617
Igor Mammedov500b11e2015-02-18 19:14:50 +00001618 scope = aml_scope("\\_SB.PCI0");
Igor Mammedov60efd422015-02-20 18:22:05 +00001619 /* build PCI0._CRS */
1620 crs = aml_resource_template();
1621 aml_append(crs,
Shannon Zhaoff80dc72015-05-29 11:28:54 +01001622 aml_word_bus_number(AML_MIN_FIXED, AML_MAX_FIXED, AML_POS_DECODE,
Marcel Apfelbaumdcdca292015-06-02 14:23:04 +03001623 0x0000, 0x0, root_bus_limit,
1624 0x0000, root_bus_limit + 1));
Shannon Zhaoff80dc72015-05-29 11:28:54 +01001625 aml_append(crs, aml_io(AML_DECODE16, 0x0CF8, 0x0CF8, 0x01, 0x08));
Igor Mammedov60efd422015-02-20 18:22:05 +00001626
1627 aml_append(crs,
Shannon Zhaoff80dc72015-05-29 11:28:54 +01001628 aml_word_io(AML_MIN_FIXED, AML_MAX_FIXED,
1629 AML_POS_DECODE, AML_ENTIRE_RANGE,
Igor Mammedov60efd422015-02-20 18:22:05 +00001630 0x0000, 0x0000, 0x0CF7, 0x0000, 0x0CF8));
Marcel Apfelbaumdcdca292015-06-02 14:23:04 +03001631
Marcel Apfelbaum2df5a7b2016-07-17 19:53:12 +03001632 crs_replace_with_free_ranges(crs_range_set.io_ranges, 0x0D00, 0xFFFF);
1633 for (i = 0; i < crs_range_set.io_ranges->len; i++) {
1634 entry = g_ptr_array_index(crs_range_set.io_ranges, i);
Marcel Apfelbaumdcdca292015-06-02 14:23:04 +03001635 aml_append(crs,
1636 aml_word_io(AML_MIN_FIXED, AML_MAX_FIXED,
1637 AML_POS_DECODE, AML_ENTIRE_RANGE,
1638 0x0000, entry->base, entry->limit,
1639 0x0000, entry->limit - entry->base + 1));
1640 }
1641
Igor Mammedov60efd422015-02-20 18:22:05 +00001642 aml_append(crs,
Shannon Zhaoff80dc72015-05-29 11:28:54 +01001643 aml_dword_memory(AML_POS_DECODE, AML_MIN_FIXED, AML_MAX_FIXED,
1644 AML_CACHEABLE, AML_READ_WRITE,
Igor Mammedov60efd422015-02-20 18:22:05 +00001645 0, 0x000A0000, 0x000BFFFF, 0, 0x00020000));
Marcel Apfelbaumdcdca292015-06-02 14:23:04 +03001646
Marcel Apfelbaum2df5a7b2016-07-17 19:53:12 +03001647 crs_replace_with_free_ranges(crs_range_set.mem_ranges,
Markus Armbrustera0efbf12016-07-01 13:47:47 +02001648 range_lob(pci_hole),
1649 range_upb(pci_hole));
Marcel Apfelbaum2df5a7b2016-07-17 19:53:12 +03001650 for (i = 0; i < crs_range_set.mem_ranges->len; i++) {
1651 entry = g_ptr_array_index(crs_range_set.mem_ranges, i);
Marcel Apfelbaumdcdca292015-06-02 14:23:04 +03001652 aml_append(crs,
1653 aml_dword_memory(AML_POS_DECODE, AML_MIN_FIXED, AML_MAX_FIXED,
1654 AML_NON_CACHEABLE, AML_READ_WRITE,
1655 0, entry->base, entry->limit,
1656 0, entry->limit - entry->base + 1));
1657 }
1658
Markus Armbrustera0efbf12016-07-01 13:47:47 +02001659 if (!range_is_empty(pci_hole64)) {
Marcel Apfelbaum16de88a2016-07-17 19:53:13 +03001660 crs_replace_with_free_ranges(crs_range_set.mem_64bit_ranges,
1661 range_lob(pci_hole64),
1662 range_upb(pci_hole64));
1663 for (i = 0; i < crs_range_set.mem_64bit_ranges->len; i++) {
1664 entry = g_ptr_array_index(crs_range_set.mem_64bit_ranges, i);
1665 aml_append(crs,
1666 aml_qword_memory(AML_POS_DECODE, AML_MIN_FIXED,
1667 AML_MAX_FIXED,
1668 AML_CACHEABLE, AML_READ_WRITE,
1669 0, entry->base, entry->limit,
1670 0, entry->limit - entry->base + 1));
1671 }
Igor Mammedov60efd422015-02-20 18:22:05 +00001672 }
Igor Mammedov2b1c2e82016-04-08 13:23:13 +02001673
Eric Auger43bc7f82020-03-05 17:51:40 +01001674 if (TPM_IS_TIS_ISA(tpm_find())) {
Igor Mammedov2b1c2e82016-04-08 13:23:13 +02001675 aml_append(crs, aml_memory32_fixed(TPM_TIS_ADDR_BASE,
1676 TPM_TIS_ADDR_SIZE, AML_READ_WRITE));
1677 }
Igor Mammedov60efd422015-02-20 18:22:05 +00001678 aml_append(scope, aml_name_decl("_CRS", crs));
1679
Igor Mammedovd31c9092015-02-20 18:22:08 +00001680 /* reserve GPE0 block resources */
1681 dev = aml_device("GPE0");
1682 aml_append(dev, aml_name_decl("_HID", aml_string("PNP0A06")));
1683 aml_append(dev, aml_name_decl("_UID", aml_string("GPE0 resources")));
1684 /* device present, functioning, decoding, not shown in UI */
1685 aml_append(dev, aml_name_decl("_STA", aml_int(0xB)));
1686 crs = aml_resource_template();
1687 aml_append(crs,
Igor Mammedov937d1b52018-02-28 15:23:51 +01001688 aml_io(
1689 AML_DECODE16,
1690 pm->fadt.gpe0_blk.address,
1691 pm->fadt.gpe0_blk.address,
1692 1,
1693 pm->fadt.gpe0_blk.bit_width / 8)
Igor Mammedovd31c9092015-02-20 18:22:08 +00001694 );
1695 aml_append(dev, aml_name_decl("_CRS", crs));
1696 aml_append(scope, dev);
1697
Marcel Apfelbaum2df5a7b2016-07-17 19:53:12 +03001698 crs_range_set_free(&crs_range_set);
Marcel Apfelbaumdcdca292015-06-02 14:23:04 +03001699
Igor Mammedov500b11e2015-02-18 19:14:50 +00001700 /* reserve PCIHP resources */
1701 if (pm->pcihp_io_len) {
1702 dev = aml_device("PHPR");
1703 aml_append(dev, aml_name_decl("_HID", aml_string("PNP0A06")));
1704 aml_append(dev,
1705 aml_name_decl("_UID", aml_string("PCI Hotplug resources")));
1706 /* device present, functioning, decoding, not shown in UI */
1707 aml_append(dev, aml_name_decl("_STA", aml_int(0xB)));
1708 crs = aml_resource_template();
1709 aml_append(crs,
Shannon Zhaoff80dc72015-05-29 11:28:54 +01001710 aml_io(AML_DECODE16, pm->pcihp_io_base, pm->pcihp_io_base, 1,
Igor Mammedov500b11e2015-02-18 19:14:50 +00001711 pm->pcihp_io_len)
1712 );
1713 aml_append(dev, aml_name_decl("_CRS", crs));
1714 aml_append(scope, dev);
1715 }
Igor Mammedov41fa5c02016-01-22 15:36:06 +01001716 aml_append(dsdt, scope);
Igor Mammedov500b11e2015-02-18 19:14:50 +00001717
Igor Mammedovebc30282015-02-18 19:14:29 +00001718 /* create S3_ / S4_ / S5_ packages if necessary */
1719 scope = aml_scope("\\");
1720 if (!pm->s3_disabled) {
1721 pkg = aml_package(4);
1722 aml_append(pkg, aml_int(1)); /* PM1a_CNT.SLP_TYP */
1723 aml_append(pkg, aml_int(1)); /* PM1b_CNT.SLP_TYP, FIXME: not impl. */
1724 aml_append(pkg, aml_int(0)); /* reserved */
1725 aml_append(pkg, aml_int(0)); /* reserved */
1726 aml_append(scope, aml_name_decl("_S3", pkg));
1727 }
1728
1729 if (!pm->s4_disabled) {
1730 pkg = aml_package(4);
1731 aml_append(pkg, aml_int(pm->s4_val)); /* PM1a_CNT.SLP_TYP */
1732 /* PM1b_CNT.SLP_TYP, FIXME: not impl. */
1733 aml_append(pkg, aml_int(pm->s4_val));
1734 aml_append(pkg, aml_int(0)); /* reserved */
1735 aml_append(pkg, aml_int(0)); /* reserved */
1736 aml_append(scope, aml_name_decl("_S4", pkg));
1737 }
1738
1739 pkg = aml_package(4);
1740 aml_append(pkg, aml_int(0)); /* PM1a_CNT.SLP_TYP */
1741 aml_append(pkg, aml_int(0)); /* PM1b_CNT.SLP_TYP not impl. */
1742 aml_append(pkg, aml_int(0)); /* reserved */
1743 aml_append(pkg, aml_int(0)); /* reserved */
1744 aml_append(scope, aml_name_decl("_S5", pkg));
Igor Mammedov41fa5c02016-01-22 15:36:06 +01001745 aml_append(dsdt, scope);
Igor Mammedovebc30282015-02-18 19:14:29 +00001746
Gabriel L. Somloe2ec7562016-02-19 13:20:27 -05001747 /* create fw_cfg node, unconditionally */
1748 {
Gabriel L. Somloe2ec7562016-02-19 13:20:27 -05001749 scope = aml_scope("\\_SB.PCI0");
Gerd Hoffmann0575c2f2020-06-19 11:19:00 +02001750 fw_cfg_add_acpi_dsdt(scope, x86ms->fw_cfg);
Gabriel L. Somloe2ec7562016-02-19 13:20:27 -05001751 aml_append(dsdt, scope);
1752 }
1753
Igor Mammedov8ac6f7a2015-02-20 18:22:12 +00001754 if (misc->applesmc_io_base) {
1755 scope = aml_scope("\\_SB.PCI0.ISA");
1756 dev = aml_device("SMC");
1757
1758 aml_append(dev, aml_name_decl("_HID", aml_eisaid("APP0001")));
1759 /* device present, functioning, decoding, not shown in UI */
1760 aml_append(dev, aml_name_decl("_STA", aml_int(0xB)));
1761
1762 crs = aml_resource_template();
1763 aml_append(crs,
Shannon Zhaoff80dc72015-05-29 11:28:54 +01001764 aml_io(AML_DECODE16, misc->applesmc_io_base, misc->applesmc_io_base,
Igor Mammedov8ac6f7a2015-02-20 18:22:12 +00001765 0x01, APPLESMC_MAX_DATA_LENGTH)
1766 );
1767 aml_append(crs, aml_irq_no_flags(6));
1768 aml_append(dev, aml_name_decl("_CRS", crs));
1769
1770 aml_append(scope, dev);
Igor Mammedov41fa5c02016-01-22 15:36:06 +01001771 aml_append(dsdt, scope);
Igor Mammedov8ac6f7a2015-02-20 18:22:12 +00001772 }
1773
Igor Mammedovcd61cb22015-02-18 19:14:38 +00001774 if (misc->pvpanic_port) {
1775 scope = aml_scope("\\_SB.PCI0.ISA");
1776
Radim Krčmář23323332015-05-29 21:57:32 +02001777 dev = aml_device("PEVT");
Igor Mammedove65bef62015-03-30 14:18:27 +02001778 aml_append(dev, aml_name_decl("_HID", aml_string("QEMU0001")));
Igor Mammedovcd61cb22015-02-18 19:14:38 +00001779
1780 crs = aml_resource_template();
1781 aml_append(crs,
Shannon Zhaoff80dc72015-05-29 11:28:54 +01001782 aml_io(AML_DECODE16, misc->pvpanic_port, misc->pvpanic_port, 1, 1)
Igor Mammedovcd61cb22015-02-18 19:14:38 +00001783 );
1784 aml_append(dev, aml_name_decl("_CRS", crs));
1785
Shannon Zhaoff80dc72015-05-29 11:28:54 +01001786 aml_append(dev, aml_operation_region("PEOR", AML_SYSTEM_IO,
Xiao Guangrong3f3009c2016-03-01 18:56:05 +08001787 aml_int(misc->pvpanic_port), 1));
Igor Mammedov36de8842015-12-10 00:41:12 +01001788 field = aml_field("PEOR", AML_BYTE_ACC, AML_NOLOCK, AML_PRESERVE);
Igor Mammedovcd61cb22015-02-18 19:14:38 +00001789 aml_append(field, aml_named_field("PEPT", 8));
1790 aml_append(dev, field);
1791
Gal Hammer8ef3ea22015-07-26 11:00:51 +03001792 /* device present, functioning, decoding, shown in UI */
1793 aml_append(dev, aml_name_decl("_STA", aml_int(0xF)));
Radim Krčmář23323332015-05-29 21:57:32 +02001794
Xiao Guangrong4dbfc882015-12-17 13:37:13 +00001795 method = aml_method("RDPT", 0, AML_NOTSERIALIZED);
Igor Mammedovcd61cb22015-02-18 19:14:38 +00001796 aml_append(method, aml_store(aml_name("PEPT"), aml_local(0)));
1797 aml_append(method, aml_return(aml_local(0)));
1798 aml_append(dev, method);
1799
Xiao Guangrong4dbfc882015-12-17 13:37:13 +00001800 method = aml_method("WRPT", 1, AML_NOTSERIALIZED);
Igor Mammedovcd61cb22015-02-18 19:14:38 +00001801 aml_append(method, aml_store(aml_arg(0), aml_name("PEPT")));
1802 aml_append(dev, method);
1803
1804 aml_append(scope, dev);
Igor Mammedov41fa5c02016-01-22 15:36:06 +01001805 aml_append(dsdt, scope);
Igor Mammedovcd61cb22015-02-18 19:14:38 +00001806 }
1807
Gal Hammer7824df32015-04-21 11:26:12 +03001808 sb_scope = aml_scope("\\_SB");
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001809 {
Igor Mammedov8b35ab22016-12-06 00:32:25 +01001810 Object *pci_host;
1811 PCIBus *bus = NULL;
Igor Mammedov8698c0c2015-02-18 19:14:46 +00001812
Igor Mammedov8b35ab22016-12-06 00:32:25 +01001813 pci_host = acpi_get_i386_pci_host();
1814 if (pci_host) {
1815 bus = PCI_HOST_BRIDGE(pci_host)->bus;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001816 }
Igor Mammedov8b35ab22016-12-06 00:32:25 +01001817
1818 if (bus) {
1819 Aml *scope = aml_scope("PCI0");
1820 /* Scan all PCI buses. Generate tables to support hotplug. */
1821 build_append_pci_bus_devices(scope, bus, pm->pcihp_bridge_en);
1822
Eric Auger43bc7f82020-03-05 17:51:40 +01001823 if (TPM_IS_TIS_ISA(tpm)) {
Stefan Berger24cf5412019-01-25 16:00:58 -05001824 if (misc->tpm_version == TPM_VERSION_2_0) {
1825 dev = aml_device("TPM");
1826 aml_append(dev, aml_name_decl("_HID",
1827 aml_string("MSFT0101")));
1828 } else {
1829 dev = aml_device("ISA.TPM");
1830 aml_append(dev, aml_name_decl("_HID",
1831 aml_eisaid("PNP0C31")));
1832 }
1833
Igor Mammedov8b35ab22016-12-06 00:32:25 +01001834 aml_append(dev, aml_name_decl("_STA", aml_int(0xF)));
1835 crs = aml_resource_template();
1836 aml_append(crs, aml_memory32_fixed(TPM_TIS_ADDR_BASE,
1837 TPM_TIS_ADDR_SIZE, AML_READ_WRITE));
1838 /*
1839 FIXME: TPM_TIS_IRQ=5 conflicts with PNP0C0F irqs,
1840 Rewrite to take IRQ from TPM device model and
1841 fix default IRQ value there to use some unused IRQ
1842 */
1843 /* aml_append(crs, aml_irq_no_flags(TPM_TIS_IRQ)); */
1844 aml_append(dev, aml_name_decl("_CRS", crs));
Stefan Bergerac6dd312019-01-15 02:27:52 +04001845
1846 tpm_build_ppi_acpi(tpm, dev);
1847
Igor Mammedov8b35ab22016-12-06 00:32:25 +01001848 aml_append(scope, dev);
1849 }
1850
1851 aml_append(sb_scope, scope);
1852 }
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001853 }
Marc-André Lureau4ab6cb42018-01-29 19:33:07 +01001854
Stefan Bergerac6dd312019-01-15 02:27:52 +04001855 if (TPM_IS_CRB(tpm)) {
Marc-André Lureau4ab6cb42018-01-29 19:33:07 +01001856 dev = aml_device("TPM");
1857 aml_append(dev, aml_name_decl("_HID", aml_string("MSFT0101")));
1858 crs = aml_resource_template();
1859 aml_append(crs, aml_memory32_fixed(TPM_CRB_ADDR_BASE,
1860 TPM_CRB_ADDR_SIZE, AML_READ_WRITE));
1861 aml_append(dev, aml_name_decl("_CRS", crs));
1862
Gerd Hoffmann88b36482020-04-29 15:59:52 +02001863 aml_append(dev, aml_name_decl("_STA", aml_int(0xf)));
Marc-André Lureau4ab6cb42018-01-29 19:33:07 +01001864
Stefan Bergerac6dd312019-01-15 02:27:52 +04001865 tpm_build_ppi_acpi(tpm, dev);
1866
Marc-André Lureau4ab6cb42018-01-29 19:33:07 +01001867 aml_append(sb_scope, dev);
1868 }
1869
Igor Mammedov8b35ab22016-12-06 00:32:25 +01001870 aml_append(dsdt, sb_scope);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001871
Igor Mammedov011bb742015-02-18 19:14:16 +00001872 /* copy AML table into ACPI tables blob and patch header there */
Igor Mammedov41fa5c02016-01-22 15:36:06 +01001873 g_array_append_vals(table_data, dsdt->buf->data, dsdt->buf->len);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001874 build_header(linker, table_data,
Igor Mammedov41fa5c02016-01-22 15:36:06 +01001875 (void *)(table_data->data + table_data->len - dsdt->buf->len),
Laszlo Ersek37ad2232016-01-18 15:12:10 +01001876 "DSDT", dsdt->buf->len, 1, NULL, NULL);
Igor Mammedov011bb742015-02-18 19:14:16 +00001877 free_aml_allocator();
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001878}
1879
1880static void
Igor Mammedov0e9b9ed2016-05-19 15:19:25 +02001881build_hpet(GArray *table_data, BIOSLinker *linker)
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001882{
1883 Acpi20Hpet *hpet;
1884
1885 hpet = acpi_data_push(table_data, sizeof(*hpet));
1886 /* Note timer_block_id value must be kept in sync with value advertised by
1887 * emulated hpet
1888 */
1889 hpet->timer_block_id = cpu_to_le32(0x8086a201);
1890 hpet->addr.address = cpu_to_le64(HPET_BASE);
1891 build_header(linker, table_data,
Laszlo Ersek37ad2232016-01-18 15:12:10 +01001892 (void *)hpet, "HPET", sizeof(*hpet), 1, NULL, NULL);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001893}
1894
Stefan Berger711b20b2014-08-11 16:33:36 -04001895static void
Igor Mammedov0e9b9ed2016-05-19 15:19:25 +02001896build_tpm_tcpa(GArray *table_data, BIOSLinker *linker, GArray *tcpalog)
Stefan Berger711b20b2014-08-11 16:33:36 -04001897{
1898 Acpi20Tcpa *tcpa = acpi_data_push(table_data, sizeof *tcpa);
Igor Mammedov46781242016-05-19 15:19:29 +02001899 unsigned log_addr_size = sizeof(tcpa->log_area_start_address);
1900 unsigned log_addr_offset =
1901 (char *)&tcpa->log_area_start_address - table_data->data;
Stefan Berger711b20b2014-08-11 16:33:36 -04001902
1903 tcpa->platform_class = cpu_to_le16(TPM_TCPA_ACPI_CLASS_CLIENT);
1904 tcpa->log_area_minimum_length = cpu_to_le32(TPM_LOG_AREA_MINIMUM_SIZE);
Igor Mammedov9774ccf2016-05-19 15:19:28 +02001905 acpi_data_push(tcpalog, le32_to_cpu(tcpa->log_area_minimum_length));
Stefan Berger711b20b2014-08-11 16:33:36 -04001906
Igor Mammedovad9671b2016-05-19 15:19:26 +02001907 bios_linker_loader_alloc(linker, ACPI_BUILD_TPMLOG_FILE, tcpalog, 1,
Stefan Berger42a5b302014-10-24 13:21:04 -04001908 false /* high memory */);
1909
Stefan Berger711b20b2014-08-11 16:33:36 -04001910 /* log area start address to be filled by Guest linker */
Igor Mammedov46781242016-05-19 15:19:29 +02001911 bios_linker_loader_add_pointer(linker,
1912 ACPI_BUILD_TABLE_FILE, log_addr_offset, log_addr_size,
1913 ACPI_BUILD_TPMLOG_FILE, 0);
Stefan Berger711b20b2014-08-11 16:33:36 -04001914
1915 build_header(linker, table_data,
Laszlo Ersek37ad2232016-01-18 15:12:10 +01001916 (void *)tcpa, "TCPA", sizeof(*tcpa), 2, NULL, NULL);
Stefan Berger711b20b2014-08-11 16:33:36 -04001917}
1918
Paolo Bonzinid471bf32018-06-29 16:22:13 +02001919#define HOLE_640K_START (640 * KiB)
1920#define HOLE_640K_END (1 * MiB)
Eduardo Habkost49264032017-09-01 10:10:02 +08001921
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001922static void
Igor Mammedov0e9b9ed2016-05-19 15:19:25 +02001923build_srat(GArray *table_data, BIOSLinker *linker, MachineState *machine)
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001924{
1925 AcpiSystemResourceAffinityTable *srat;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001926 AcpiSratMemoryAffinity *numamem;
1927
1928 int i;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001929 int srat_start, numa_start, slots;
1930 uint64_t mem_len, mem_base, next_base;
Igor Mammedov5803fce2016-02-26 14:59:23 +01001931 MachineClass *mc = MACHINE_GET_CLASS(machine);
Paolo Bonzinif0bb2762019-10-22 09:39:50 +02001932 X86MachineState *x86ms = X86_MACHINE(machine);
Igor Mammedov80e5db32017-01-18 18:13:20 +01001933 const CPUArchIdList *apic_ids = mc->possible_cpu_arch_ids(machine);
Igor Mammedov3d3ebca2016-02-26 14:59:22 +01001934 PCMachineState *pcms = PC_MACHINE(machine);
Igor Mammedovcec65192014-06-02 15:25:28 +02001935 ram_addr_t hotplugabble_address_space_size =
David Hildenbrandf2ffbe22018-04-23 18:51:24 +02001936 object_property_get_int(OBJECT(pcms), PC_MACHINE_DEVMEM_REGION_SIZE,
Igor Mammedovcec65192014-06-02 15:25:28 +02001937 NULL);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001938
1939 srat_start = table_data->len;
1940
1941 srat = acpi_data_push(table_data, sizeof *srat);
1942 srat->reserved1 = cpu_to_le32(1);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001943
Igor Mammedov5803fce2016-02-26 14:59:23 +01001944 for (i = 0; i < apic_ids->len; i++) {
Igor Mammedovd41f3e72017-05-30 18:23:58 +02001945 int node_id = apic_ids->cpus[i].props.node_id;
Igor Mammedov5eff33a2016-10-19 14:05:32 +02001946 uint32_t apic_id = apic_ids->cpus[i].arch_id;
Igor Mammedov5803fce2016-02-26 14:59:23 +01001947
Igor Mammedov5eff33a2016-10-19 14:05:32 +02001948 if (apic_id < 255) {
1949 AcpiSratProcessorAffinity *core;
1950
1951 core = acpi_data_push(table_data, sizeof *core);
1952 core->type = ACPI_SRAT_PROCESSOR_APIC;
1953 core->length = sizeof(*core);
1954 core->local_apic_id = apic_id;
Igor Mammedovea265072017-05-10 13:29:52 +02001955 core->proximity_lo = node_id;
Igor Mammedov5eff33a2016-10-19 14:05:32 +02001956 memset(core->proximity_hi, 0, 3);
1957 core->local_sapic_eid = 0;
1958 core->flags = cpu_to_le32(1);
1959 } else {
1960 AcpiSratProcessorX2ApicAffinity *core;
1961
1962 core = acpi_data_push(table_data, sizeof *core);
1963 core->type = ACPI_SRAT_PROCESSOR_x2APIC;
1964 core->length = sizeof(*core);
1965 core->x2apic_id = cpu_to_le32(apic_id);
Igor Mammedovea265072017-05-10 13:29:52 +02001966 core->proximity_domain = cpu_to_le32(node_id);
Igor Mammedov5eff33a2016-10-19 14:05:32 +02001967 core->flags = cpu_to_le32(1);
Igor Mammedov1f3aba32016-06-16 14:23:48 +02001968 }
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001969 }
1970
1971
1972 /* the memory map is a bit tricky, it contains at least one hole
1973 * from 640k-1M and possibly another one from 3.5G-4G.
1974 */
1975 next_base = 0;
1976 numa_start = table_data->len;
1977
Eduardo Habkostdd4c2f02015-12-11 16:42:32 -02001978 for (i = 1; i < pcms->numa_nodes + 1; ++i) {
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001979 mem_base = next_base;
Eduardo Habkostdd4c2f02015-12-11 16:42:32 -02001980 mem_len = pcms->node_mem[i - 1];
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001981 next_base = mem_base + mem_len;
1982
Eduardo Habkost49264032017-09-01 10:10:02 +08001983 /* Cut out the 640K hole */
1984 if (mem_base <= HOLE_640K_START &&
1985 next_base > HOLE_640K_START) {
1986 mem_len -= next_base - HOLE_640K_START;
1987 if (mem_len > 0) {
1988 numamem = acpi_data_push(table_data, sizeof *numamem);
1989 build_srat_memory(numamem, mem_base, mem_len, i - 1,
1990 MEM_AFFINITY_ENABLED);
1991 }
1992
1993 /* Check for the rare case: 640K < RAM < 1M */
1994 if (next_base <= HOLE_640K_END) {
1995 next_base = HOLE_640K_END;
1996 continue;
1997 }
1998 mem_base = HOLE_640K_END;
1999 mem_len = next_base - HOLE_640K_END;
2000 }
2001
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002002 /* Cut out the ACPI_PCI hole */
Paolo Bonzinif0bb2762019-10-22 09:39:50 +02002003 if (mem_base <= x86ms->below_4g_mem_size &&
2004 next_base > x86ms->below_4g_mem_size) {
2005 mem_len -= next_base - x86ms->below_4g_mem_size;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002006 if (mem_len > 0) {
2007 numamem = acpi_data_push(table_data, sizeof *numamem);
Shannon Zhao64b83132016-05-12 13:22:28 +01002008 build_srat_memory(numamem, mem_base, mem_len, i - 1,
2009 MEM_AFFINITY_ENABLED);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002010 }
2011 mem_base = 1ULL << 32;
Paolo Bonzinif0bb2762019-10-22 09:39:50 +02002012 mem_len = next_base - x86ms->below_4g_mem_size;
Dou Liyang6cf6fe32017-12-14 12:08:55 +08002013 next_base = mem_base + mem_len;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002014 }
Dou Liyang16b42262018-07-10 16:58:01 +08002015
2016 if (mem_len > 0) {
2017 numamem = acpi_data_push(table_data, sizeof *numamem);
2018 build_srat_memory(numamem, mem_base, mem_len, i - 1,
2019 MEM_AFFINITY_ENABLED);
2020 }
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002021 }
Vishal Vermac3b0cf62020-06-05 18:09:10 -06002022
2023 if (machine->nvdimms_state->is_enabled) {
2024 nvdimm_build_srat(table_data);
2025 }
2026
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002027 slots = (table_data->len - numa_start) / sizeof *numamem;
Eduardo Habkostdd4c2f02015-12-11 16:42:32 -02002028 for (; slots < pcms->numa_nodes + 2; slots++) {
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002029 numamem = acpi_data_push(table_data, sizeof *numamem);
Shannon Zhao64b83132016-05-12 13:22:28 +01002030 build_srat_memory(numamem, 0, 0, 0, MEM_AFFINITY_NOFLAGS);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002031 }
2032
Igor Mammedovdbb6da82018-08-22 11:46:44 +02002033 /*
2034 * Entry is required for Windows to enable memory hotplug in OS
2035 * and for Linux to enable SWIOTLB when booted with less than
2036 * 4G of RAM. Windows works better if the entry sets proximity
2037 * to the highest NUMA node in the machine.
2038 * Memory devices may override proximity set by this entry,
2039 * providing _PXM method if necessary.
2040 */
Igor Mammedovcec65192014-06-02 15:25:28 +02002041 if (hotplugabble_address_space_size) {
Igor Mammedovdbb6da82018-08-22 11:46:44 +02002042 numamem = acpi_data_push(table_data, sizeof *numamem);
2043 build_srat_memory(numamem, machine->device_memory->base,
2044 hotplugabble_address_space_size, pcms->numa_nodes - 1,
2045 MEM_AFFINITY_HOTPLUGGABLE | MEM_AFFINITY_ENABLED);
Igor Mammedovcec65192014-06-02 15:25:28 +02002046 }
2047
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002048 build_header(linker, table_data,
2049 (void *)(table_data->data + srat_start),
Michael S. Tsirkin821e3222014-03-18 15:49:41 +02002050 "SRAT",
Laszlo Ersek37ad2232016-01-18 15:12:10 +01002051 table_data->len - srat_start, 1, NULL, NULL);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002052}
2053
Peter Xud46114f2016-07-14 13:56:14 +08002054/*
2055 * VT-d spec 8.1 DMA Remapping Reporting Structure
2056 * (version Oct. 2014 or later)
2057 */
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002058static void
Igor Mammedov0e9b9ed2016-05-19 15:19:25 +02002059build_dmar_q35(GArray *table_data, BIOSLinker *linker)
Le Tand4eb9112014-08-16 13:55:39 +08002060{
2061 int dmar_start = table_data->len;
2062
2063 AcpiTableDmar *dmar;
2064 AcpiDmarHardwareUnit *drhd;
Jason Wangbd2baac2016-12-30 18:09:16 +08002065 AcpiDmarRootPortATS *atsr;
Peter Xud46114f2016-07-14 13:56:14 +08002066 uint8_t dmar_flags = 0;
2067 X86IOMMUState *iommu = x86_iommu_get_default();
Peter Xucfc13df2016-07-14 13:56:17 +08002068 AcpiDmarDeviceScope *scope = NULL;
2069 /* Root complex IOAPIC use one path[0] only */
2070 size_t ioapic_scope_size = sizeof(*scope) + sizeof(scope->path[0]);
Prasad Singamsetty37f51382017-11-14 18:13:50 -05002071 IntelIOMMUState *intel_iommu = INTEL_IOMMU_DEVICE(iommu);
Peter Xud46114f2016-07-14 13:56:14 +08002072
2073 assert(iommu);
Peter Xua924b3d2018-12-20 13:40:36 +08002074 if (x86_iommu_ir_supported(iommu)) {
Peter Xud46114f2016-07-14 13:56:14 +08002075 dmar_flags |= 0x1; /* Flags: 0x1: INT_REMAP */
2076 }
Le Tand4eb9112014-08-16 13:55:39 +08002077
2078 dmar = acpi_data_push(table_data, sizeof(*dmar));
Prasad Singamsetty37f51382017-11-14 18:13:50 -05002079 dmar->host_address_width = intel_iommu->aw_bits - 1;
Peter Xud46114f2016-07-14 13:56:14 +08002080 dmar->flags = dmar_flags;
Le Tand4eb9112014-08-16 13:55:39 +08002081
2082 /* DMAR Remapping Hardware Unit Definition structure */
Peter Xucfc13df2016-07-14 13:56:17 +08002083 drhd = acpi_data_push(table_data, sizeof(*drhd) + ioapic_scope_size);
Le Tand4eb9112014-08-16 13:55:39 +08002084 drhd->type = cpu_to_le16(ACPI_DMAR_TYPE_HARDWARE_UNIT);
Peter Xucfc13df2016-07-14 13:56:17 +08002085 drhd->length = cpu_to_le16(sizeof(*drhd) + ioapic_scope_size);
Le Tand4eb9112014-08-16 13:55:39 +08002086 drhd->flags = ACPI_DMAR_INCLUDE_PCI_ALL;
2087 drhd->pci_segment = cpu_to_le16(0);
2088 drhd->address = cpu_to_le64(Q35_HOST_BRIDGE_IOMMU_ADDR);
2089
Peter Xucfc13df2016-07-14 13:56:17 +08002090 /* Scope definition for the root-complex IOAPIC. See VT-d spec
2091 * 8.3.1 (version Oct. 2014 or later). */
2092 scope = &drhd->scope[0];
2093 scope->entry_type = 0x03; /* Type: 0x03 for IOAPIC */
2094 scope->length = ioapic_scope_size;
2095 scope->enumeration_id = ACPI_BUILD_IOAPIC_ID;
2096 scope->bus = Q35_PSEUDO_BUS_PLATFORM;
Peter Xu1b39bc12016-10-31 15:34:39 +08002097 scope->path[0].device = PCI_SLOT(Q35_PSEUDO_DEVFN_IOAPIC);
2098 scope->path[0].function = PCI_FUNC(Q35_PSEUDO_DEVFN_IOAPIC);
Peter Xucfc13df2016-07-14 13:56:17 +08002099
Jason Wangbd2baac2016-12-30 18:09:16 +08002100 if (iommu->dt_supported) {
2101 atsr = acpi_data_push(table_data, sizeof(*atsr));
2102 atsr->type = cpu_to_le16(ACPI_DMAR_TYPE_ATSR);
2103 atsr->length = cpu_to_le16(sizeof(*atsr));
2104 atsr->flags = ACPI_DMAR_ATSR_ALL_PORTS;
2105 atsr->pci_segment = cpu_to_le16(0);
2106 }
2107
Le Tand4eb9112014-08-16 13:55:39 +08002108 build_header(linker, table_data, (void *)(table_data->data + dmar_start),
Laszlo Ersek37ad2232016-01-18 15:12:10 +01002109 "DMAR", table_data->len - dmar_start, 1, NULL, NULL);
Le Tand4eb9112014-08-16 13:55:39 +08002110}
Liran Alon14cda352020-03-13 16:50:08 +02002111
2112/*
2113 * Windows ACPI Emulated Devices Table
2114 * (Version 1.0 - April 6, 2009)
2115 * Spec: http://download.microsoft.com/download/7/E/7/7E7662CF-CBEA-470B-A97E-CE7CE0D98DC2/WAET.docx
2116 *
2117 * Helpful to speedup Windows guests and ignored by others.
2118 */
2119static void
2120build_waet(GArray *table_data, BIOSLinker *linker)
2121{
2122 int waet_start = table_data->len;
2123
2124 /* WAET header */
2125 acpi_data_push(table_data, sizeof(AcpiTableHeader));
2126 /*
2127 * Set "ACPI PM timer good" flag.
2128 *
2129 * Tells Windows guests that our ACPI PM timer is reliable in the
2130 * sense that guest can read it only once to obtain a reliable value.
2131 * Which avoids costly VMExits caused by guest re-reading it unnecessarily.
2132 */
2133 build_append_int_noprefix(table_data, 1 << 1 /* ACPI PM timer good */, 4);
2134
2135 build_header(linker, table_data, (void *)(table_data->data + waet_start),
2136 "WAET", table_data->len - waet_start, 1, NULL, NULL);
2137}
2138
David Kiariefb9f5922016-09-20 18:42:34 +03002139/*
2140 * IVRS table as specified in AMD IOMMU Specification v2.62, Section 5.2
2141 * accessible here http://support.amd.com/TechDocs/48882_IOMMU.pdf
2142 */
Singh, Brijeshc0288182018-10-01 19:44:41 +00002143#define IOAPIC_SB_DEVID (uint64_t)PCI_BUILD_BDF(0, PCI_DEVFN(0x14, 0))
2144
Alex Williamson977aff12019-10-23 16:47:28 -06002145/*
2146 * Insert IVHD entry for device and recurse, insert alias, or insert range as
2147 * necessary for the PCI topology.
2148 */
2149static void
2150insert_ivhd(PCIBus *bus, PCIDevice *dev, void *opaque)
2151{
2152 GArray *table_data = opaque;
2153 uint32_t entry;
2154
2155 /* "Select" IVHD entry, type 0x2 */
2156 entry = PCI_BUILD_BDF(pci_bus_num(bus), dev->devfn) << 8 | 0x2;
2157 build_append_int_noprefix(table_data, entry, 4);
2158
2159 if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_BRIDGE)) {
2160 PCIBus *sec_bus = pci_bridge_get_sec_bus(PCI_BRIDGE(dev));
2161 uint8_t sec = pci_bus_num(sec_bus);
2162 uint8_t sub = dev->config[PCI_SUBORDINATE_BUS];
2163
2164 if (pci_bus_is_express(sec_bus)) {
2165 /*
2166 * Walk the bus if there are subordinates, otherwise use a range
2167 * to cover an entire leaf bus. We could potentially also use a
2168 * range for traversed buses, but we'd need to take care not to
2169 * create both Select and Range entries covering the same device.
2170 * This is easier and potentially more compact.
2171 *
2172 * An example bare metal system seems to use Select entries for
2173 * root ports without a slot (ie. built-ins) and Range entries
2174 * when there is a slot. The same system also only hard-codes
2175 * the alias range for an onboard PCIe-to-PCI bridge, apparently
2176 * making no effort to support nested bridges. We attempt to
2177 * be more thorough here.
2178 */
2179 if (sec == sub) { /* leaf bus */
2180 /* "Start of Range" IVHD entry, type 0x3 */
2181 entry = PCI_BUILD_BDF(sec, PCI_DEVFN(0, 0)) << 8 | 0x3;
2182 build_append_int_noprefix(table_data, entry, 4);
2183 /* "End of Range" IVHD entry, type 0x4 */
2184 entry = PCI_BUILD_BDF(sub, PCI_DEVFN(31, 7)) << 8 | 0x4;
2185 build_append_int_noprefix(table_data, entry, 4);
2186 } else {
2187 pci_for_each_device(sec_bus, sec, insert_ivhd, table_data);
2188 }
2189 } else {
2190 /*
2191 * If the secondary bus is conventional, then we need to create an
2192 * Alias range for everything downstream. The range covers the
2193 * first devfn on the secondary bus to the last devfn on the
2194 * subordinate bus. The alias target depends on legacy versus
2195 * express bridges, just as in pci_device_iommu_address_space().
2196 * DeviceIDa vs DeviceIDb as per the AMD IOMMU spec.
2197 */
2198 uint16_t dev_id_a, dev_id_b;
2199
2200 dev_id_a = PCI_BUILD_BDF(sec, PCI_DEVFN(0, 0));
2201
2202 if (pci_is_express(dev) &&
2203 pcie_cap_get_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE) {
2204 dev_id_b = dev_id_a;
2205 } else {
2206 dev_id_b = PCI_BUILD_BDF(pci_bus_num(bus), dev->devfn);
2207 }
2208
2209 /* "Alias Start of Range" IVHD entry, type 0x43, 8 bytes */
2210 build_append_int_noprefix(table_data, dev_id_a << 8 | 0x43, 4);
2211 build_append_int_noprefix(table_data, dev_id_b << 8 | 0x0, 4);
2212
2213 /* "End of Range" IVHD entry, type 0x4 */
2214 entry = PCI_BUILD_BDF(sub, PCI_DEVFN(31, 7)) << 8 | 0x4;
2215 build_append_int_noprefix(table_data, entry, 4);
2216 }
2217 }
2218}
2219
2220/* For all PCI host bridges, walk and insert IVHD entries */
2221static int
2222ivrs_host_bridges(Object *obj, void *opaque)
2223{
2224 GArray *ivhd_blob = opaque;
2225
2226 if (object_dynamic_cast(obj, TYPE_PCI_HOST_BRIDGE)) {
2227 PCIBus *bus = PCI_HOST_BRIDGE(obj)->bus;
2228
2229 if (bus) {
2230 pci_for_each_device(bus, pci_bus_num(bus), insert_ivhd, ivhd_blob);
2231 }
2232 }
2233
2234 return 0;
2235}
2236
David Kiariefb9f5922016-09-20 18:42:34 +03002237static void
2238build_amd_iommu(GArray *table_data, BIOSLinker *linker)
2239{
Alex Williamson977aff12019-10-23 16:47:28 -06002240 int ivhd_table_len = 24;
David Kiariefb9f5922016-09-20 18:42:34 +03002241 int iommu_start = table_data->len;
2242 AMDVIState *s = AMD_IOMMU_DEVICE(x86_iommu_get_default());
Alex Williamson977aff12019-10-23 16:47:28 -06002243 GArray *ivhd_blob = g_array_new(false, true, 1);
David Kiariefb9f5922016-09-20 18:42:34 +03002244
2245 /* IVRS header */
2246 acpi_data_push(table_data, sizeof(AcpiTableHeader));
2247 /* IVinfo - IO virtualization information common to all
2248 * IOMMU units in a system
2249 */
2250 build_append_int_noprefix(table_data, 40UL << 8/* PASize */, 4);
2251 /* reserved */
2252 build_append_int_noprefix(table_data, 0, 8);
2253
2254 /* IVHD definition - type 10h */
2255 build_append_int_noprefix(table_data, 0x10, 1);
2256 /* virtualization flags */
2257 build_append_int_noprefix(table_data,
2258 (1UL << 0) | /* HtTunEn */
2259 (1UL << 4) | /* iotblSup */
2260 (1UL << 6) | /* PrefSup */
2261 (1UL << 7), /* PPRSup */
2262 1);
Singh, Brijeshc0288182018-10-01 19:44:41 +00002263
2264 /*
Alex Williamson977aff12019-10-23 16:47:28 -06002265 * A PCI bus walk, for each PCI host bridge, is necessary to create a
2266 * complete set of IVHD entries. Do this into a separate blob so that we
2267 * can calculate the total IVRS table length here and then append the new
2268 * blob further below. Fall back to an entry covering all devices, which
2269 * is sufficient when no aliases are present.
2270 */
2271 object_child_foreach_recursive(object_get_root(),
2272 ivrs_host_bridges, ivhd_blob);
2273
2274 if (!ivhd_blob->len) {
2275 /*
2276 * Type 1 device entry reporting all devices
2277 * These are 4-byte device entries currently reporting the range of
2278 * Refer to Spec - Table 95:IVHD Device Entry Type Codes(4-byte)
2279 */
2280 build_append_int_noprefix(ivhd_blob, 0x0000001, 4);
2281 }
2282
2283 ivhd_table_len += ivhd_blob->len;
2284
2285 /*
Singh, Brijeshc0288182018-10-01 19:44:41 +00002286 * When interrupt remapping is supported, we add a special IVHD device
2287 * for type IO-APIC.
2288 */
Peter Xua924b3d2018-12-20 13:40:36 +08002289 if (x86_iommu_ir_supported(x86_iommu_get_default())) {
Singh, Brijeshc0288182018-10-01 19:44:41 +00002290 ivhd_table_len += 8;
2291 }
Alex Williamson977aff12019-10-23 16:47:28 -06002292
David Kiariefb9f5922016-09-20 18:42:34 +03002293 /* IVHD length */
Singh, Brijeshc0288182018-10-01 19:44:41 +00002294 build_append_int_noprefix(table_data, ivhd_table_len, 2);
David Kiariefb9f5922016-09-20 18:42:34 +03002295 /* DeviceID */
2296 build_append_int_noprefix(table_data, s->devid, 2);
2297 /* Capability offset */
2298 build_append_int_noprefix(table_data, s->capab_offset, 2);
2299 /* IOMMU base address */
2300 build_append_int_noprefix(table_data, s->mmio.addr, 8);
2301 /* PCI Segment Group */
2302 build_append_int_noprefix(table_data, 0, 2);
2303 /* IOMMU info */
2304 build_append_int_noprefix(table_data, 0, 2);
2305 /* IOMMU Feature Reporting */
2306 build_append_int_noprefix(table_data,
2307 (48UL << 30) | /* HATS */
2308 (48UL << 28) | /* GATS */
Singh, Brijesh12499b22018-10-01 19:44:45 +00002309 (1UL << 2) | /* GTSup */
2310 (1UL << 6), /* GASup */
David Kiariefb9f5922016-09-20 18:42:34 +03002311 4);
Alex Williamson977aff12019-10-23 16:47:28 -06002312
2313 /* IVHD entries as found above */
2314 g_array_append_vals(table_data, ivhd_blob->data, ivhd_blob->len);
2315 g_array_free(ivhd_blob, TRUE);
David Kiariefb9f5922016-09-20 18:42:34 +03002316
Singh, Brijeshc0288182018-10-01 19:44:41 +00002317 /*
2318 * Add a special IVHD device type.
2319 * Refer to spec - Table 95: IVHD device entry type codes
2320 *
2321 * Linux IOMMU driver checks for the special IVHD device (type IO-APIC).
2322 * See Linux kernel commit 'c2ff5cf5294bcbd7fa50f7d860e90a66db7e5059'
2323 */
Peter Xua924b3d2018-12-20 13:40:36 +08002324 if (x86_iommu_ir_supported(x86_iommu_get_default())) {
Singh, Brijeshc0288182018-10-01 19:44:41 +00002325 build_append_int_noprefix(table_data,
2326 (0x1ull << 56) | /* type IOAPIC */
2327 (IOAPIC_SB_DEVID << 40) | /* IOAPIC devid */
2328 0x48, /* special device */
2329 8);
2330 }
2331
David Kiariefb9f5922016-09-20 18:42:34 +03002332 build_header(linker, table_data, (void *)(table_data->data + iommu_start),
2333 "IVRS", table_data->len - iommu_start, 1, NULL, NULL);
2334}
Le Tand4eb9112014-08-16 13:55:39 +08002335
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002336typedef
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002337struct AcpiBuildState {
2338 /* Copy of table in RAM (for patching). */
Paolo Bonzini339240b2015-03-23 10:24:16 +01002339 MemoryRegion *table_mr;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002340 /* Is table patched? */
2341 uint8_t patched;
Michael S. Tsirkind70414a2015-02-09 13:59:53 +00002342 void *rsdp;
Paolo Bonzini339240b2015-03-23 10:24:16 +01002343 MemoryRegion *rsdp_mr;
2344 MemoryRegion *linker_mr;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002345} AcpiBuildState;
2346
2347static bool acpi_get_mcfg(AcpiMcfgInfo *mcfg)
2348{
2349 Object *pci_host;
2350 QObject *o;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002351
Marcel Apfelbaumca6c1852015-06-02 14:22:59 +03002352 pci_host = acpi_get_i386_pci_host();
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002353 g_assert(pci_host);
2354
2355 o = object_property_get_qobject(pci_host, PCIE_HOST_MCFG_BASE, NULL);
2356 if (!o) {
2357 return false;
2358 }
Wei Yangc3094342019-04-19 08:30:50 +08002359 mcfg->base = qnum_get_uint(qobject_to(QNum, o));
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02002360 qobject_unref(o);
Wei Yangc3094342019-04-19 08:30:50 +08002361 if (mcfg->base == PCIE_BASE_ADDR_UNMAPPED) {
Igor Mammedovfe4970a2019-04-09 17:00:37 +02002362 return false;
2363 }
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002364
2365 o = object_property_get_qobject(pci_host, PCIE_HOST_MCFG_SIZE, NULL);
2366 assert(o);
Wei Yangc3094342019-04-19 08:30:50 +08002367 mcfg->size = qnum_get_uint(qobject_to(QNum, o));
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02002368 qobject_unref(o);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002369 return true;
2370}
2371
2372static
Igor Mammedov3d3ebca2016-02-26 14:59:22 +01002373void acpi_build(AcpiBuildTables *tables, MachineState *machine)
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002374{
Igor Mammedov3d3ebca2016-02-26 14:59:22 +01002375 PCMachineState *pcms = PC_MACHINE(machine);
Eduardo Habkostbb292f52015-12-11 16:42:28 -02002376 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
Paolo Bonzinif0bb2762019-10-22 09:39:50 +02002377 X86MachineState *x86ms = X86_MACHINE(machine);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002378 GArray *table_offsets;
Igor Mammedov41fa5c02016-01-22 15:36:06 +01002379 unsigned facs, dsdt, rsdt, fadt;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002380 AcpiPmInfo pm;
2381 AcpiMiscInfo misc;
2382 AcpiMcfgInfo mcfg;
Markus Armbruster01c97422016-06-15 19:56:31 +02002383 Range pci_hole, pci_hole64;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002384 uint8_t *u;
Paolo Bonzini07fb6172014-07-28 17:34:15 +02002385 size_t aml_len = 0;
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002386 GArray *tables_blob = tables->table_data;
Laszlo Ersekae123742016-01-18 15:12:13 +01002387 AcpiSlicOem slic_oem = { .id = NULL, .table_id = NULL };
Ben Warrend03637b2017-02-16 15:15:36 -08002388 Object *vmgenid_dev;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002389
Like Xu0e11fc62019-05-19 04:54:25 +08002390 acpi_get_pm_info(machine, &pm);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002391 acpi_get_misc_info(&misc);
Markus Armbruster01c97422016-06-15 19:56:31 +02002392 acpi_get_pci_holes(&pci_hole, &pci_hole64);
Laszlo Ersekae123742016-01-18 15:12:13 +01002393 acpi_get_slic_oem(&slic_oem);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002394
2395 table_offsets = g_array_new(false, true /* clear */,
2396 sizeof(uint32_t));
Gonglei8b310fc2014-11-13 10:59:37 +08002397 ACPI_BUILD_DPRINTF("init ACPI tables\n");
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002398
Igor Mammedovad9671b2016-05-19 15:19:26 +02002399 bios_linker_loader_alloc(tables->linker,
2400 ACPI_BUILD_TABLE_FILE, tables_blob,
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002401 64 /* Ensure FACS is aligned */,
2402 false /* high memory */);
2403
2404 /*
2405 * FACS is pointed to by FADT.
2406 * We place it first since it's the only table that has alignment
2407 * requirements.
2408 */
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002409 facs = tables_blob->len;
Wei Yang009180b2019-01-30 11:02:07 +08002410 build_facs(tables_blob);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002411
2412 /* DSDT is pointed to by FADT */
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002413 dsdt = tables_blob->len;
Markus Armbruster01c97422016-06-15 19:56:31 +02002414 build_dsdt(tables_blob, tables->linker, &pm, &misc,
2415 &pci_hole, &pci_hole64, machine);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002416
Paolo Bonzini07fb6172014-07-28 17:34:15 +02002417 /* Count the size of the DSDT and SSDT, we will need it for legacy
2418 * sizing of ACPI tables.
2419 */
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002420 aml_len += tables_blob->len - dsdt;
Paolo Bonzini07fb6172014-07-28 17:34:15 +02002421
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002422 /* ACPI tables pointed to by RSDT */
Igor Mammedov41fa5c02016-01-22 15:36:06 +01002423 fadt = tables_blob->len;
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002424 acpi_add_table(table_offsets, tables_blob);
Igor Mammedov937d1b52018-02-28 15:23:51 +01002425 pm.fadt.facs_tbl_offset = &facs;
2426 pm.fadt.dsdt_tbl_offset = &dsdt;
2427 pm.fadt.xdsdt_tbl_offset = &dsdt;
2428 build_fadt(tables_blob, tables->linker, &pm.fadt,
Laszlo Ersekae123742016-01-18 15:12:13 +01002429 slic_oem.id, slic_oem.table_id);
Igor Mammedov41fa5c02016-01-22 15:36:06 +01002430 aml_len += tables_blob->len - fadt;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002431
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002432 acpi_add_table(table_offsets, tables_blob);
Gerd Hoffmanneb66ffa2020-05-20 15:19:47 +02002433 acpi_build_madt(tables_blob, tables->linker, x86ms,
Gerd Hoffmann5794d342020-05-20 15:19:48 +02002434 ACPI_DEVICE_IF(pcms->acpi_dev), true);
Michael S. Tsirkin9ac1c4c2014-04-28 08:15:32 +03002435
Ben Warrend03637b2017-02-16 15:15:36 -08002436 vmgenid_dev = find_vmgenid_dev();
2437 if (vmgenid_dev) {
2438 acpi_add_table(table_offsets, tables_blob);
2439 vmgenid_build_acpi(VMGENID(vmgenid_dev), tables_blob,
2440 tables->vmgenid, tables->linker);
2441 }
2442
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002443 if (misc.has_hpet) {
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002444 acpi_add_table(table_offsets, tables_blob);
2445 build_hpet(tables_blob, tables->linker);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002446 }
Stefan Berger5cb18b32015-05-26 16:51:07 -04002447 if (misc.tpm_version != TPM_VERSION_UNSPEC) {
Stefan Berger7e7c1b82020-05-29 15:28:40 -04002448 if (misc.tpm_version == TPM_VERSION_1_2) {
2449 acpi_add_table(table_offsets, tables_blob);
2450 build_tpm_tcpa(tables_blob, tables->linker, tables->tcpalog);
2451 } else { /* TPM_VERSION_2_0 */
Igor Mammedov72d97b32015-06-09 05:31:53 +02002452 acpi_add_table(table_offsets, tables_blob);
Stefan Berger4a42fa02017-11-14 13:42:42 -05002453 build_tpm2(tables_blob, tables->linker, tables->tcpalog);
Stefan Berger5cb18b32015-05-26 16:51:07 -04002454 }
Stefan Berger711b20b2014-08-11 16:33:36 -04002455 }
Eduardo Habkostdd4c2f02015-12-11 16:42:32 -02002456 if (pcms->numa_nodes) {
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002457 acpi_add_table(table_offsets, tables_blob);
Igor Mammedov3d3ebca2016-02-26 14:59:22 +01002458 build_srat(tables_blob, tables->linker, machine);
Tao Xu118154b2019-08-09 14:57:23 +08002459 if (machine->numa_state->have_numa_distance) {
He Chen0f203432017-04-27 10:35:58 +08002460 acpi_add_table(table_offsets, tables_blob);
Tao Xuaa570202019-08-09 14:57:22 +08002461 build_slit(tables_blob, tables->linker, machine);
He Chen0f203432017-04-27 10:35:58 +08002462 }
Liu Jingqie6f123c2019-12-13 09:19:25 +08002463 if (machine->numa_state->hmat_enabled) {
2464 acpi_add_table(table_offsets, tables_blob);
2465 build_hmat(tables_blob, tables->linker, machine->numa_state);
2466 }
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002467 }
2468 if (acpi_get_mcfg(&mcfg)) {
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002469 acpi_add_table(table_offsets, tables_blob);
Wei Yangf13a9442019-05-21 14:28:35 +08002470 build_mcfg(tables_blob, tables->linker, &mcfg);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002471 }
David Kiariefb9f5922016-09-20 18:42:34 +03002472 if (x86_iommu_get_default()) {
2473 IommuType IOMMUType = x86_iommu_get_type();
2474 if (IOMMUType == TYPE_AMD) {
2475 acpi_add_table(table_offsets, tables_blob);
2476 build_amd_iommu(tables_blob, tables->linker);
2477 } else if (IOMMUType == TYPE_INTEL) {
2478 acpi_add_table(table_offsets, tables_blob);
2479 build_dmar_q35(tables_blob, tables->linker);
2480 }
Le Tand4eb9112014-08-16 13:55:39 +08002481 }
Eric Augerf6a0d062019-03-08 19:20:53 +01002482 if (machine->nvdimms_state->is_enabled) {
Igor Mammedovad9671b2016-05-19 15:19:26 +02002483 nvdimm_build_acpi(table_offsets, tables_blob, tables->linker,
Eric Augerf6a0d062019-03-08 19:20:53 +01002484 machine->nvdimms_state, machine->ram_slots);
Xiao Guangrong87252e12015-12-02 15:20:58 +08002485 }
2486
Liran Alon14cda352020-03-13 16:50:08 +02002487 acpi_add_table(table_offsets, tables_blob);
2488 build_waet(tables_blob, tables->linker);
2489
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002490 /* Add tables supplied by user (if any) */
2491 for (u = acpi_table_first(); u; u = acpi_table_next(u)) {
2492 unsigned len = acpi_table_len(u);
2493
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002494 acpi_add_table(table_offsets, tables_blob);
2495 g_array_append_vals(tables_blob, u, len);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002496 }
2497
2498 /* RSDT is pointed to by RSDP */
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002499 rsdt = tables_blob->len;
Laszlo Ersekae123742016-01-18 15:12:13 +01002500 build_rsdt(tables_blob, tables->linker, table_offsets,
2501 slic_oem.id, slic_oem.table_id);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002502
2503 /* RSDP is in FSEG memory, so allocate it separately */
Samuel Ortiza46ce1c2018-12-17 16:34:48 +01002504 {
2505 AcpiRsdpData rsdp_data = {
2506 .revision = 0,
2507 .oem_id = ACPI_BUILD_APPNAME6,
2508 .xsdt_tbl_offset = NULL,
2509 .rsdt_tbl_offset = &rsdt,
2510 };
2511 build_rsdp(tables->rsdp, tables->linker, &rsdp_data);
2512 if (!pcmc->rsdp_in_ram) {
2513 /* We used to allocate some extra space for RSDP revision 2 but
2514 * only used the RSDP revision 0 space. The extra bytes were
2515 * zeroed out and not used.
2516 * Here we continue wasting those extra 16 bytes to make sure we
2517 * don't break migration for machine types 2.2 and older due to
2518 * RSDP blob size mismatch.
2519 */
2520 build_append_int_noprefix(tables->rsdp, 0, 16);
2521 }
2522 }
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002523
Paolo Bonzini07fb6172014-07-28 17:34:15 +02002524 /* We'll expose it all to Guest so we want to reduce
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002525 * chance of size changes.
Paolo Bonzini07fb6172014-07-28 17:34:15 +02002526 *
2527 * We used to align the tables to 4k, but of course this would
2528 * too simple to be enough. 4k turned out to be too small an
2529 * alignment very soon, and in fact it is almost impossible to
2530 * keep the table size stable for all (max_cpus, max_memory_slots)
2531 * combinations. So the table size is always 64k for pc-i440fx-2.1
2532 * and we give an error if the table grows beyond that limit.
2533 *
2534 * We still have the problem of migrating from "-M pc-i440fx-2.0". For
2535 * that, we exploit the fact that QEMU 2.1 generates _smaller_ tables
2536 * than 2.0 and we can always pad the smaller tables with zeros. We can
2537 * then use the exact size of the 2.0 tables.
2538 *
2539 * All this is for PIIX4, since QEMU 2.0 didn't support Q35 migration.
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002540 */
Eduardo Habkostbb292f52015-12-11 16:42:28 -02002541 if (pcmc->legacy_acpi_table_size) {
Paolo Bonzini07fb6172014-07-28 17:34:15 +02002542 /* Subtracting aml_len gives the size of fixed tables. Then add the
2543 * size of the PIIX4 DSDT/SSDT in QEMU 2.0.
2544 */
2545 int legacy_aml_len =
Eduardo Habkostbb292f52015-12-11 16:42:28 -02002546 pcmc->legacy_acpi_table_size +
Paolo Bonzinif0bb2762019-10-22 09:39:50 +02002547 ACPI_BUILD_LEGACY_CPU_AML_SIZE * x86ms->apic_id_limit;
Paolo Bonzini07fb6172014-07-28 17:34:15 +02002548 int legacy_table_size =
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002549 ROUND_UP(tables_blob->len - aml_len + legacy_aml_len,
Paolo Bonzini07fb6172014-07-28 17:34:15 +02002550 ACPI_BUILD_ALIGN_SIZE);
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002551 if (tables_blob->len > legacy_table_size) {
Paolo Bonzini07fb6172014-07-28 17:34:15 +02002552 /* Should happen only with PCI bridges and -M pc-i440fx-2.0. */
Alistair Francis9e5d2c52017-09-11 12:52:43 -07002553 warn_report("ACPI table size %u exceeds %d bytes,"
2554 " migration may not work",
2555 tables_blob->len, legacy_table_size);
2556 error_printf("Try removing CPUs, NUMA nodes, memory slots"
2557 " or PCI bridges.");
Paolo Bonzini07fb6172014-07-28 17:34:15 +02002558 }
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002559 g_array_set_size(tables_blob, legacy_table_size);
Paolo Bonzini07fb6172014-07-28 17:34:15 +02002560 } else {
Michael S. Tsirkin868270f2014-07-28 23:07:11 +02002561 /* Make sure we have a buffer in case we need to resize the tables. */
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002562 if (tables_blob->len > ACPI_BUILD_TABLE_SIZE / 2) {
Paolo Bonzini18045fb2014-07-28 17:34:16 +02002563 /* As of QEMU 2.1, this fires with 160 VCPUs and 255 memory slots. */
Alistair Francis9e5d2c52017-09-11 12:52:43 -07002564 warn_report("ACPI table size %u exceeds %d bytes,"
2565 " migration may not work",
2566 tables_blob->len, ACPI_BUILD_TABLE_SIZE / 2);
2567 error_printf("Try removing CPUs, NUMA nodes, memory slots"
2568 " or PCI bridges.");
Paolo Bonzini18045fb2014-07-28 17:34:16 +02002569 }
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002570 acpi_align_size(tables_blob, ACPI_BUILD_TABLE_SIZE);
Paolo Bonzini07fb6172014-07-28 17:34:15 +02002571 }
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002572
Igor Mammedov0e9b9ed2016-05-19 15:19:25 +02002573 acpi_align_size(tables->linker->cmd_blob, ACPI_BUILD_ALIGN_SIZE);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002574
2575 /* Cleanup memory that's no longer used. */
2576 g_array_free(table_offsets, true);
2577}
2578
Paolo Bonzini339240b2015-03-23 10:24:16 +01002579static void acpi_ram_update(MemoryRegion *mr, GArray *data)
Michael S. Tsirkin42d85902015-02-15 17:12:11 +01002580{
2581 uint32_t size = acpi_data_len(data);
2582
2583 /* Make sure RAM size is correct - in case it got changed e.g. by migration */
Paolo Bonzini339240b2015-03-23 10:24:16 +01002584 memory_region_ram_resize(mr, size, &error_abort);
Michael S. Tsirkin42d85902015-02-15 17:12:11 +01002585
Paolo Bonzini339240b2015-03-23 10:24:16 +01002586 memcpy(memory_region_get_ram_ptr(mr), data->data, size);
2587 memory_region_set_dirty(mr, 0, size);
Michael S. Tsirkin42d85902015-02-15 17:12:11 +01002588}
2589
Gabriel L. Somlo3f8752b2015-11-05 09:32:49 -05002590static void acpi_build_update(void *build_opaque)
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002591{
2592 AcpiBuildState *build_state = build_opaque;
2593 AcpiBuildTables tables;
2594
2595 /* No state to update or already patched? Nothing to do. */
2596 if (!build_state || build_state->patched) {
2597 return;
2598 }
2599 build_state->patched = 1;
2600
2601 acpi_build_tables_init(&tables);
2602
Igor Mammedov3d3ebca2016-02-26 14:59:22 +01002603 acpi_build(&tables, MACHINE(qdev_get_machine()));
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002604
Paolo Bonzini339240b2015-03-23 10:24:16 +01002605 acpi_ram_update(build_state->table_mr, tables.table_data);
Michael S. Tsirkina1666142014-11-17 07:51:50 +02002606
Michael S. Tsirkin42d85902015-02-15 17:12:11 +01002607 if (build_state->rsdp) {
2608 memcpy(build_state->rsdp, tables.rsdp->data, acpi_data_len(tables.rsdp));
2609 } else {
Paolo Bonzini339240b2015-03-23 10:24:16 +01002610 acpi_ram_update(build_state->rsdp_mr, tables.rsdp);
Michael S. Tsirkin42d85902015-02-15 17:12:11 +01002611 }
Michael S. Tsirkina1666142014-11-17 07:51:50 +02002612
Igor Mammedov0e9b9ed2016-05-19 15:19:25 +02002613 acpi_ram_update(build_state->linker_mr, tables.linker->cmd_blob);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002614 acpi_build_tables_cleanup(&tables, true);
2615}
2616
2617static void acpi_build_reset(void *build_opaque)
2618{
2619 AcpiBuildState *build_state = build_opaque;
2620 build_state->patched = 0;
2621}
2622
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002623static const VMStateDescription vmstate_acpi_build = {
2624 .name = "acpi_build",
2625 .version_id = 1,
2626 .minimum_version_id = 1,
Juan Quintelad49805a2014-04-16 15:32:32 +02002627 .fields = (VMStateField[]) {
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002628 VMSTATE_UINT8(patched, AcpiBuildState),
2629 VMSTATE_END_OF_LIST()
2630 },
2631};
2632
Eduardo Habkostfb306ff2015-12-11 16:42:26 -02002633void acpi_setup(void)
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002634{
Eduardo Habkostfb306ff2015-12-11 16:42:26 -02002635 PCMachineState *pcms = PC_MACHINE(qdev_get_machine());
Eduardo Habkostbb292f52015-12-11 16:42:28 -02002636 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
Paolo Bonzinif0bb2762019-10-22 09:39:50 +02002637 X86MachineState *x86ms = X86_MACHINE(pcms);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002638 AcpiBuildTables tables;
2639 AcpiBuildState *build_state;
Ben Warrend03637b2017-02-16 15:15:36 -08002640 Object *vmgenid_dev;
Stefan Berger0fe24662019-01-15 02:27:51 +04002641 TPMIf *tpm;
2642 static FwCfgTPMConfig tpm_config;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002643
Paolo Bonzinif0bb2762019-10-22 09:39:50 +02002644 if (!x86ms->fw_cfg) {
Gonglei8b310fc2014-11-13 10:59:37 +08002645 ACPI_BUILD_DPRINTF("No fw cfg. Bailing out.\n");
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002646 return;
2647 }
2648
Wei Liu021746c2016-11-01 17:44:16 +00002649 if (!pcms->acpi_build_enabled) {
Gonglei8b310fc2014-11-13 10:59:37 +08002650 ACPI_BUILD_DPRINTF("ACPI build disabled. Bailing out.\n");
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002651 return;
2652 }
2653
Gerd Hoffmann17e89072020-03-20 11:01:36 +01002654 if (!x86_machine_is_acpi_enabled(X86_MACHINE(pcms))) {
Gonglei8b310fc2014-11-13 10:59:37 +08002655 ACPI_BUILD_DPRINTF("ACPI disabled. Bailing out.\n");
Michael S. Tsirkin81adc512013-11-07 14:12:05 +02002656 return;
2657 }
2658
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002659 build_state = g_malloc0(sizeof *build_state);
2660
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002661 acpi_build_tables_init(&tables);
Igor Mammedov3d3ebca2016-02-26 14:59:22 +01002662 acpi_build(&tables, MACHINE(pcms));
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002663
2664 /* Now expose it all to Guest */
Wei Yang82f76c62019-06-10 09:18:30 +08002665 build_state->table_mr = acpi_add_rom_blob(acpi_build_update,
2666 build_state, tables.table_data,
2667 ACPI_BUILD_TABLE_FILE,
2668 ACPI_BUILD_TABLE_MAX_SIZE);
Paolo Bonzini339240b2015-03-23 10:24:16 +01002669 assert(build_state->table_mr != NULL);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002670
Paolo Bonzini339240b2015-03-23 10:24:16 +01002671 build_state->linker_mr =
Wei Yang82f76c62019-06-10 09:18:30 +08002672 acpi_add_rom_blob(acpi_build_update, build_state,
Shameer Kolothumbac78f92020-04-03 11:18:25 +01002673 tables.linker->cmd_blob, ACPI_BUILD_LOADER_FILE, 0);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002674
Paolo Bonzinif0bb2762019-10-22 09:39:50 +02002675 fw_cfg_add_file(x86ms->fw_cfg, ACPI_BUILD_TPMLOG_FILE,
Stefan Berger42a5b302014-10-24 13:21:04 -04002676 tables.tcpalog->data, acpi_data_len(tables.tcpalog));
2677
Stefan Berger0fe24662019-01-15 02:27:51 +04002678 tpm = tpm_find();
2679 if (tpm && object_property_get_bool(OBJECT(tpm), "ppi", &error_abort)) {
2680 tpm_config = (FwCfgTPMConfig) {
2681 .tpmppi_address = cpu_to_le32(TPM_PPI_ADDR_BASE),
2682 .tpm_version = tpm_get_version(tpm),
Stefan Bergerac6dd312019-01-15 02:27:52 +04002683 .tpmppi_version = TPM_PPI_VERSION_1_30
Stefan Berger0fe24662019-01-15 02:27:51 +04002684 };
Paolo Bonzinif0bb2762019-10-22 09:39:50 +02002685 fw_cfg_add_file(x86ms->fw_cfg, "etc/tpm/config",
Stefan Berger0fe24662019-01-15 02:27:51 +04002686 &tpm_config, sizeof tpm_config);
2687 }
2688
Ben Warrend03637b2017-02-16 15:15:36 -08002689 vmgenid_dev = find_vmgenid_dev();
2690 if (vmgenid_dev) {
Paolo Bonzinif0bb2762019-10-22 09:39:50 +02002691 vmgenid_add_fw_cfg(VMGENID(vmgenid_dev), x86ms->fw_cfg,
Ben Warrend03637b2017-02-16 15:15:36 -08002692 tables.vmgenid);
2693 }
2694
Eduardo Habkostbb292f52015-12-11 16:42:28 -02002695 if (!pcmc->rsdp_in_ram) {
Igor Mammedov358774d2015-02-09 13:59:55 +00002696 /*
2697 * Keep for compatibility with old machine types.
2698 * Though RSDP is small, its contents isn't immutable, so
Michael S. Tsirkinafaa2e42015-02-17 10:40:30 +01002699 * we'll update it along with the rest of tables on guest access.
Igor Mammedov358774d2015-02-09 13:59:55 +00002700 */
Michael S. Tsirkinafaa2e42015-02-17 10:40:30 +01002701 uint32_t rsdp_size = acpi_data_len(tables.rsdp);
2702
2703 build_state->rsdp = g_memdup(tables.rsdp->data, rsdp_size);
Paolo Bonzinif0bb2762019-10-22 09:39:50 +02002704 fw_cfg_add_file_callback(x86ms->fw_cfg, ACPI_BUILD_RSDP_FILE,
Marc-André Lureau5f9252f2017-09-11 18:59:23 +02002705 acpi_build_update, NULL, build_state,
Michael S. Tsirkinbaf2d5b2017-01-12 19:24:14 +01002706 build_state->rsdp, rsdp_size, true);
Paolo Bonzini339240b2015-03-23 10:24:16 +01002707 build_state->rsdp_mr = NULL;
Igor Mammedov358774d2015-02-09 13:59:55 +00002708 } else {
Michael S. Tsirkin42d85902015-02-15 17:12:11 +01002709 build_state->rsdp = NULL;
Wei Yang82f76c62019-06-10 09:18:30 +08002710 build_state->rsdp_mr = acpi_add_rom_blob(acpi_build_update,
2711 build_state, tables.rsdp,
2712 ACPI_BUILD_RSDP_FILE, 0);
Igor Mammedov358774d2015-02-09 13:59:55 +00002713 }
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002714
2715 qemu_register_reset(acpi_build_reset, build_state);
2716 acpi_build_reset(build_state);
2717 vmstate_register(NULL, 0, &vmstate_acpi_build, build_state);
2718
2719 /* Cleanup tables but don't free the memory: we track it
2720 * in build_state.
2721 */
2722 acpi_build_tables_cleanup(&tables, false);
2723}