]> git.proxmox.com Git - mirror_qemu.git/blame - hw/i386/acpi-build.c
Do not include hw/boards.h if it's not really necessary
[mirror_qemu.git] / hw / i386 / acpi-build.c
CommitLineData
72c194f7
MT
1/* 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
b6a0aa05 23#include "qemu/osdep.h"
da34e65c 24#include "qapi/error.h"
15280c36 25#include "qapi/qmp/qnum.h"
72c194f7 26#include "acpi-build.h"
eb66ffab 27#include "acpi-common.h"
72c194f7 28#include "qemu/bitmap.h"
07fb6176 29#include "qemu/error-report.h"
72c194f7 30#include "hw/pci/pci.h"
2e5b09fd 31#include "hw/core/cpu.h"
fcf5ef2a 32#include "target/i386/cpu.h"
0d5d8a3a 33#include "hw/misc/pvpanic.h"
72c194f7 34#include "hw/timer/hpet.h"
395e5fb4 35#include "hw/acpi/acpi-defs.h"
72c194f7 36#include "hw/acpi/acpi.h"
679dd1a9 37#include "hw/acpi/cpu.h"
72c194f7 38#include "hw/nvram/fw_cfg.h"
0058ae1d 39#include "hw/acpi/bios-linker-loader.h"
15bce1b7 40#include "hw/isa/isa.h"
27b9fc54 41#include "hw/block/fdc.h"
bef3492d 42#include "hw/acpi/memory_hotplug.h"
711b20b4
SB
43#include "sysemu/tpm.h"
44#include "hw/acpi/tpm.h"
d03637bc 45#include "hw/acpi/vmgenid.h"
5cb18b3d 46#include "sysemu/tpm_backend.h"
bcdb9064 47#include "hw/rtc/mc146818rtc_regs.h"
d6454270 48#include "migration/vmstate.h"
2cc0e2e8 49#include "hw/mem/memory-device.h"
4b997690 50#include "hw/mem/nvdimm.h"
1f3aba37 51#include "sysemu/numa.h"
71e8a915 52#include "sysemu/reset.h"
6775d15d 53#include "hw/hyperv/vmbus-bridge.h"
72c194f7
MT
54
55/* Supported chipsets: */
fff123b8 56#include "hw/southbridge/piix.h"
99fd437d 57#include "hw/acpi/pcihp.h"
89a289c7 58#include "hw/i386/fw_cfg.h"
72c194f7
MT
59#include "hw/i386/ich9.h"
60#include "hw/pci/pci_bus.h"
61#include "hw/pci-host/q35.h"
1cf5fd57 62#include "hw/i386/x86-iommu.h"
72c194f7 63
19934e0e 64#include "hw/acpi/aml-build.h"
82f76c67 65#include "hw/acpi/utils.h"
48cefd94 66#include "hw/acpi/pci.h"
19934e0e 67
72c194f7 68#include "qom/qom-qobject.h"
fb9f5926
DK
69#include "hw/i386/amd_iommu.h"
70#include "hw/i386/intel_iommu.h"
72c194f7 71
86e91dd7 72#include "hw/acpi/ipmi.h"
e6f123c3 73#include "hw/acpi/hmat.h"
86e91dd7 74
07fb6176
PB
75/* These are used to size the ACPI tables for -M pc-i440fx-1.7 and
76 * -M pc-i440fx-2.0. Even if the actual amount of AML generated grows
77 * a little bit, there should be plenty of free space since the DSDT
78 * shrunk by ~1.5k between QEMU 2.0 and QEMU 2.1.
79 */
80#define ACPI_BUILD_LEGACY_CPU_AML_SIZE 97
81#define ACPI_BUILD_ALIGN_SIZE 0x1000
82
868270f2 83#define ACPI_BUILD_TABLE_SIZE 0x20000
18045fb9 84
8b310fc4
GA
85/* #define DEBUG_ACPI_BUILD */
86#ifdef DEBUG_ACPI_BUILD
87#define ACPI_BUILD_DPRINTF(fmt, ...) \
88 do {printf("ACPI_BUILD: " fmt, ## __VA_ARGS__); } while (0)
89#else
90#define ACPI_BUILD_DPRINTF(fmt, ...)
91#endif
92
72c194f7
MT
93typedef struct AcpiPmInfo {
94 bool s3_disabled;
95 bool s4_disabled;
133a2da4 96 bool pcihp_bridge_en;
6d837f1f 97 bool smi_on_cpuhp;
892aae74 98 bool smi_on_cpu_unplug;
df4008c9 99 bool pcihp_root_en;
72c194f7 100 uint8_t s4_val;
937d1b58 101 AcpiFadtData fadt;
ddf1ec2f 102 uint16_t cpu_hp_io_base;
500b11ea
IM
103 uint16_t pcihp_io_base;
104 uint16_t pcihp_io_len;
72c194f7
MT
105} AcpiPmInfo;
106
107typedef struct AcpiMiscInfo {
e4db2798 108 bool is_piix4;
72c194f7 109 bool has_hpet;
5cb18b3d 110 TPMVersion tpm_version;
72c194f7
MT
111 const unsigned char *dsdt_code;
112 unsigned dsdt_size;
113 uint16_t pvpanic_port;
8ac6f7a6 114 uint16_t applesmc_io_base;
72c194f7
MT
115} AcpiMiscInfo;
116
99fd437d
MT
117typedef struct AcpiBuildPciBusHotplugState {
118 GArray *device_table;
119 GArray *notify_table;
120 struct AcpiBuildPciBusHotplugState *parent;
133a2da4 121 bool pcihp_bridge_en;
99fd437d
MT
122} AcpiBuildPciBusHotplugState;
123
0fe24669
SB
124typedef struct FwCfgTPMConfig {
125 uint32_t tpmppi_address;
126 uint8_t tpm_version;
127 uint8_t tpmppi_version;
128} QEMU_PACKED FwCfgTPMConfig;
129
4a441836
GH
130static bool acpi_get_mcfg(AcpiMcfgInfo *mcfg);
131
5c94b826
KL
132const struct AcpiGenericAddress x86_nvdimm_acpi_dsmio = {
133 .space_id = AML_AS_SYSTEM_IO,
134 .address = NVDIMM_ACPI_IO_BASE,
135 .bit_width = NVDIMM_ACPI_IO_LEN << 3
136};
137
0e11fc69
LX
138static void init_common_fadt_data(MachineState *ms, Object *o,
139 AcpiFadtData *data)
937d1b58 140{
33b44fda
IY
141 X86MachineState *x86ms = X86_MACHINE(ms);
142 /*
143 * "ICH9-LPC" or "PIIX4_PM" has "smm-compat" property to keep the old
144 * behavior for compatibility irrelevant to smm_enabled, which doesn't
145 * comforms to ACPI spec.
146 */
147 bool smm_enabled = object_property_get_bool(o, "smm-compat", NULL) ?
148 true : x86_machine_is_smm_enabled(x86ms);
937d1b58
IM
149 uint32_t io = object_property_get_uint(o, ACPI_PM_PROP_PM_IO_BASE, NULL);
150 AmlAddressSpace as = AML_AS_SYSTEM_IO;
151 AcpiFadtData fadt = {
152 .rev = 3,
153 .flags =
154 (1 << ACPI_FADT_F_WBINVD) |
155 (1 << ACPI_FADT_F_PROC_C1) |
156 (1 << ACPI_FADT_F_SLP_BUTTON) |
157 (1 << ACPI_FADT_F_RTC_S4) |
158 (1 << ACPI_FADT_F_USE_PLATFORM_CLOCK) |
159 /* APIC destination mode ("Flat Logical") has an upper limit of 8
160 * CPUs for more than 8 CPUs, "Clustered Logical" mode has to be
161 * used
162 */
0e11fc69
LX
163 ((ms->smp.max_cpus > 8) ?
164 (1 << ACPI_FADT_F_FORCE_APIC_CLUSTER_MODEL) : 0),
937d1b58
IM
165 .int_model = 1 /* Multiple APIC */,
166 .rtc_century = RTC_CENTURY,
167 .plvl2_lat = 0xfff /* C2 state not supported */,
168 .plvl3_lat = 0xfff /* C3 state not supported */,
33b44fda 169 .smi_cmd = smm_enabled ? ACPI_PORT_SMI_CMD : 0,
937d1b58
IM
170 .sci_int = object_property_get_uint(o, ACPI_PM_PROP_SCI_INT, NULL),
171 .acpi_enable_cmd =
33b44fda
IY
172 smm_enabled ?
173 object_property_get_uint(o, ACPI_PM_PROP_ACPI_ENABLE_CMD, NULL) :
174 0,
937d1b58 175 .acpi_disable_cmd =
33b44fda
IY
176 smm_enabled ?
177 object_property_get_uint(o, ACPI_PM_PROP_ACPI_DISABLE_CMD, NULL) :
178 0,
937d1b58
IM
179 .pm1a_evt = { .space_id = as, .bit_width = 4 * 8, .address = io },
180 .pm1a_cnt = { .space_id = as, .bit_width = 2 * 8,
181 .address = io + 0x04 },
182 .pm_tmr = { .space_id = as, .bit_width = 4 * 8, .address = io + 0x08 },
183 .gpe0_blk = { .space_id = as, .bit_width =
184 object_property_get_uint(o, ACPI_PM_PROP_GPE0_BLK_LEN, NULL) * 8,
185 .address = object_property_get_uint(o, ACPI_PM_PROP_GPE0_BLK, NULL)
186 },
187 };
188 *data = fadt;
189}
190
81c48dd7
PMD
191static Object *object_resolve_type_unambiguous(const char *typename)
192{
193 bool ambig;
194 Object *o = object_resolve_path_type("", typename, &ambig);
195
196 if (ambig || !o) {
197 return NULL;
198 }
199 return o;
200}
201
0e11fc69 202static void acpi_get_pm_info(MachineState *machine, AcpiPmInfo *pm)
72c194f7 203{
81c48dd7
PMD
204 Object *piix = object_resolve_type_unambiguous(TYPE_PIIX4_PM);
205 Object *lpc = object_resolve_type_unambiguous(TYPE_ICH9_LPC_DEVICE);
697155cd 206 Object *obj = piix ? piix : lpc;
72c194f7 207 QObject *o;
94aaca64 208 pm->cpu_hp_io_base = 0;
500b11ea
IM
209 pm->pcihp_io_base = 0;
210 pm->pcihp_io_len = 0;
6d837f1f 211 pm->smi_on_cpuhp = false;
892aae74 212 pm->smi_on_cpu_unplug = false;
937d1b58 213
6fa5171f 214 assert(obj);
a0628599 215 init_common_fadt_data(machine, obj, &pm->fadt);
72c194f7 216 if (piix) {
3a3fcc75 217 /* w2k requires FADT(rev1) or it won't boot, keep PC compatible */
937d1b58 218 pm->fadt.rev = 1;
ddf1ec2f 219 pm->cpu_hp_io_base = PIIX4_CPU_HOTPLUG_IO_BASE;
500b11ea 220 pm->pcihp_io_base =
35f91e50 221 object_property_get_uint(obj, ACPI_PCIHP_IO_BASE_PROP, NULL);
500b11ea 222 pm->pcihp_io_len =
35f91e50 223 object_property_get_uint(obj, ACPI_PCIHP_IO_LEN_PROP, NULL);
72c194f7
MT
224 }
225 if (lpc) {
6d837f1f
IM
226 uint64_t smi_features = object_property_get_uint(lpc,
227 ICH9_LPC_SMI_NEGOTIATED_FEAT_PROP, NULL);
937d1b58
IM
228 struct AcpiGenericAddress r = { .space_id = AML_AS_SYSTEM_IO,
229 .bit_width = 8, .address = ICH9_RST_CNT_IOPORT };
230 pm->fadt.reset_reg = r;
231 pm->fadt.reset_val = 0xf;
232 pm->fadt.flags |= 1 << ACPI_FADT_F_RESET_REG_SUP;
ddf1ec2f 233 pm->cpu_hp_io_base = ICH9_CPU_HOTPLUG_IO_BASE;
6d837f1f
IM
234 pm->smi_on_cpuhp =
235 !!(smi_features & BIT_ULL(ICH9_LPC_SMI_F_CPU_HOTPLUG_BIT));
892aae74
IM
236 pm->smi_on_cpu_unplug =
237 !!(smi_features & BIT_ULL(ICH9_LPC_SMI_F_CPU_HOT_UNPLUG_BIT));
72c194f7 238 }
72c194f7 239
937d1b58
IM
240 /* The above need not be conditional on machine type because the reset port
241 * happens to be the same on PIIX (pc) and ICH9 (q35). */
0063454a 242 QEMU_BUILD_BUG_ON(ICH9_RST_CNT_IOPORT != PIIX_RCR_IOPORT);
937d1b58 243
72c194f7
MT
244 /* Fill in optional s3/s4 related properties */
245 o = object_property_get_qobject(obj, ACPI_PM_PROP_S3_DISABLED, NULL);
246 if (o) {
7dc847eb 247 pm->s3_disabled = qnum_get_uint(qobject_to(QNum, o));
72c194f7
MT
248 } else {
249 pm->s3_disabled = false;
250 }
cb3e7f08 251 qobject_unref(o);
72c194f7
MT
252 o = object_property_get_qobject(obj, ACPI_PM_PROP_S4_DISABLED, NULL);
253 if (o) {
7dc847eb 254 pm->s4_disabled = qnum_get_uint(qobject_to(QNum, o));
72c194f7
MT
255 } else {
256 pm->s4_disabled = false;
257 }
cb3e7f08 258 qobject_unref(o);
72c194f7
MT
259 o = object_property_get_qobject(obj, ACPI_PM_PROP_S4_VAL, NULL);
260 if (o) {
7dc847eb 261 pm->s4_val = qnum_get_uint(qobject_to(QNum, o));
72c194f7
MT
262 } else {
263 pm->s4_val = false;
264 }
cb3e7f08 265 qobject_unref(o);
72c194f7 266
133a2da4
IM
267 pm->pcihp_bridge_en =
268 object_property_get_bool(obj, "acpi-pci-hotplug-with-bridge-support",
269 NULL);
df4008c9
AS
270 pm->pcihp_root_en =
271 object_property_get_bool(obj, "acpi-root-pci-hotplug",
272 NULL);
72c194f7
MT
273}
274
72c194f7
MT
275static void acpi_get_misc_info(AcpiMiscInfo *info)
276{
81c48dd7
PMD
277 Object *piix = object_resolve_type_unambiguous(TYPE_PIIX4_PM);
278 Object *lpc = object_resolve_type_unambiguous(TYPE_ICH9_LPC_DEVICE);
3db119da
IM
279 assert(!!piix != !!lpc);
280
281 if (piix) {
282 info->is_piix4 = true;
283 }
284 if (lpc) {
285 info->is_piix4 = false;
286 }
287
72c194f7 288 info->has_hpet = hpet_find();
3dfd5a2a 289 info->tpm_version = tpm_get_version(tpm_find());
72c194f7 290 info->pvpanic_port = pvpanic_port();
8ac6f7a6 291 info->applesmc_io_base = applesmc_port();
72c194f7
MT
292}
293
ca6c1855
MA
294/*
295 * Because of the PXB hosts we cannot simply query TYPE_PCI_HOST_BRIDGE.
296 * On i386 arch we only have two pci hosts, so we can look only for them.
297 */
298static Object *acpi_get_i386_pci_host(void)
299{
300 PCIHostState *host;
301
302 host = OBJECT_CHECK(PCIHostState,
303 object_resolve_path("/machine/i440fx", NULL),
304 TYPE_PCI_HOST_BRIDGE);
305 if (!host) {
306 host = OBJECT_CHECK(PCIHostState,
307 object_resolve_path("/machine/q35", NULL),
308 TYPE_PCI_HOST_BRIDGE);
309 }
310
311 return OBJECT(host);
312}
313
01c9742d 314static void acpi_get_pci_holes(Range *hole, Range *hole64)
72c194f7
MT
315{
316 Object *pci_host;
72c194f7 317
ca6c1855 318 pci_host = acpi_get_i386_pci_host();
72c194f7
MT
319 g_assert(pci_host);
320
a0efbf16 321 range_set_bounds1(hole,
60555365
MAL
322 object_property_get_uint(pci_host,
323 PCI_HOST_PROP_PCI_HOLE_START,
324 NULL),
325 object_property_get_uint(pci_host,
326 PCI_HOST_PROP_PCI_HOLE_END,
327 NULL));
a0efbf16 328 range_set_bounds1(hole64,
60555365
MAL
329 object_property_get_uint(pci_host,
330 PCI_HOST_PROP_PCI_HOLE64_START,
331 NULL),
332 object_property_get_uint(pci_host,
333 PCI_HOST_PROP_PCI_HOLE64_END,
334 NULL));
72c194f7
MT
335}
336
72c194f7
MT
337static void acpi_align_size(GArray *blob, unsigned align)
338{
339 /* Align size to multiple of given size. This reduces the chance
340 * we need to change size in the future (breaking cross version migration).
341 */
134d42d6 342 g_array_set_size(blob, ROUND_UP(acpi_data_len(blob), align));
72c194f7
MT
343}
344
72c194f7
MT
345/* FACS */
346static void
009180bd 347build_facs(GArray *table_data)
72c194f7
MT
348{
349 AcpiFacsDescriptorRev1 *facs = acpi_data_push(table_data, sizeof *facs);
821e3227 350 memcpy(&facs->signature, "FACS", 4);
72c194f7
MT
351 facs->length = cpu_to_le32(sizeof(*facs));
352}
353
62b52c26 354static void build_append_pcihp_notify_entry(Aml *method, int slot)
99fd437d 355{
62b52c26
IM
356 Aml *if_ctx;
357 int32_t devfn = PCI_DEVFN(slot, 0);
358
5530427f 359 if_ctx = aml_if(aml_and(aml_arg(0), aml_int(0x1U << slot), NULL));
62b52c26
IM
360 aml_append(if_ctx, aml_notify(aml_name("S%.02X", devfn), aml_arg(1)));
361 aml_append(method, if_ctx);
99fd437d
MT
362}
363
62b52c26 364static void build_append_pci_bus_devices(Aml *parent_scope, PCIBus *bus,
b23046ab 365 bool pcihp_bridge_en)
99fd437d 366{
7dc847eb 367 Aml *dev, *notify_method = NULL, *method;
99fd437d 368 QObject *bsel;
b23046ab
IM
369 PCIBus *sec;
370 int i;
133a2da4 371
99fd437d
MT
372 bsel = object_property_get_qobject(OBJECT(bus), ACPI_PCIHP_PROP_BSEL, NULL);
373 if (bsel) {
7dc847eb 374 uint64_t bsel_val = qnum_get_uint(qobject_to(QNum, bsel));
62b52c26
IM
375
376 aml_append(parent_scope, aml_name_decl("BSEL", aml_int(bsel_val)));
4dbfc881 377 notify_method = aml_method("DVNT", 2, AML_NOTSERIALIZED);
8dcf525a 378 }
99fd437d 379
8dcf525a
MT
380 for (i = 0; i < ARRAY_SIZE(bus->devices); i += PCI_FUNC_MAX) {
381 DeviceClass *dc;
382 PCIDeviceClass *pc;
383 PCIDevice *pdev = bus->devices[i];
384 int slot = PCI_SLOT(i);
b23046ab 385 bool hotplug_enabled_dev;
093a35e5 386 bool bridge_in_acpi;
15a5b254 387 bool cold_plugged_bridge;
99fd437d 388
8dcf525a 389 if (!pdev) {
b23046ab 390 if (bsel) { /* add hotplug slots for non present devices */
62b52c26
IM
391 dev = aml_device("S%.02X", PCI_DEVFN(slot, 0));
392 aml_append(dev, aml_name_decl("_SUN", aml_int(slot)));
393 aml_append(dev, aml_name_decl("_ADR", aml_int(slot << 16)));
4dbfc881 394 method = aml_method("_EJ0", 1, AML_NOTSERIALIZED);
62b52c26
IM
395 aml_append(method,
396 aml_call2("PCEJ", aml_name("BSEL"), aml_name("_SUN"))
397 );
398 aml_append(dev, method);
b7f23f62
IM
399 method = aml_method("_DSM", 4, AML_SERIALIZED);
400 aml_append(method,
401 aml_return(aml_call6("PDSM", aml_arg(0), aml_arg(1),
402 aml_arg(2), aml_arg(3),
403 aml_name("BSEL"), aml_name("_SUN")))
404 );
405 aml_append(dev, method);
62b52c26
IM
406 aml_append(parent_scope, dev);
407
408 build_append_pcihp_notify_entry(notify_method, slot);
b23046ab 409 }
8dcf525a
MT
410 continue;
411 }
99fd437d 412
8dcf525a
MT
413 pc = PCI_DEVICE_GET_CLASS(pdev);
414 dc = DEVICE_GET_CLASS(pdev);
99fd437d 415
15a5b254
AS
416 /*
417 * Cold plugged bridges aren't themselves hot-pluggable.
a20275fa 418 * Hotplugged bridges *are* hot-pluggable.
093a35e5 419 */
15a5b254
AS
420 cold_plugged_bridge = pc->is_bridge && !DEVICE(pdev)->hotplugged;
421 bridge_in_acpi = cold_plugged_bridge && pcihp_bridge_en;
b23046ab 422
15a5b254 423 hotplug_enabled_dev = bsel && dc->hotpluggable && !cold_plugged_bridge;
093a35e5 424
b23046ab
IM
425 if (pc->class_id == PCI_CLASS_BRIDGE_ISA) {
426 continue;
99fd437d
MT
427 }
428
62b52c26
IM
429 /* start to compose PCI slot descriptor */
430 dev = aml_device("S%.02X", PCI_DEVFN(slot, 0));
431 aml_append(dev, aml_name_decl("_ADR", aml_int(slot << 16)));
432
b7f23f62
IM
433 if (bsel) {
434 aml_append(dev, aml_name_decl("_SUN", aml_int(slot)));
435 method = aml_method("_DSM", 4, AML_SERIALIZED);
436 aml_append(method, aml_return(
437 aml_call6("PDSM", aml_arg(0), aml_arg(1), aml_arg(2),
438 aml_arg(3), aml_name("BSEL"), aml_name("_SUN"))
439 ));
440 aml_append(dev, method);
441 }
442
8dcf525a 443 if (pc->class_id == PCI_CLASS_DISPLAY_VGA) {
62b52c26
IM
444 /* add VGA specific AML methods */
445 int s3d;
446
8dcf525a 447 if (object_dynamic_cast(OBJECT(pdev), "qxl-vga")) {
62b52c26 448 s3d = 3;
b23046ab 449 } else {
62b52c26 450 s3d = 0;
99fd437d 451 }
62b52c26 452
4dbfc881 453 method = aml_method("_S1D", 0, AML_NOTSERIALIZED);
62b52c26
IM
454 aml_append(method, aml_return(aml_int(0)));
455 aml_append(dev, method);
456
4dbfc881 457 method = aml_method("_S2D", 0, AML_NOTSERIALIZED);
62b52c26
IM
458 aml_append(method, aml_return(aml_int(0)));
459 aml_append(dev, method);
460
4dbfc881 461 method = aml_method("_S3D", 0, AML_NOTSERIALIZED);
62b52c26
IM
462 aml_append(method, aml_return(aml_int(s3d)));
463 aml_append(dev, method);
b23046ab 464 } else if (hotplug_enabled_dev) {
b7f23f62 465 /* add _EJ0 to make slot hotpluggable */
4dbfc881 466 method = aml_method("_EJ0", 1, AML_NOTSERIALIZED);
62b52c26
IM
467 aml_append(method,
468 aml_call2("PCEJ", aml_name("BSEL"), aml_name("_SUN"))
469 );
470 aml_append(dev, method);
471
472 if (bsel) {
473 build_append_pcihp_notify_entry(notify_method, slot);
474 }
b23046ab 475 } else if (bridge_in_acpi) {
62b52c26
IM
476 /*
477 * device is coldplugged bridge,
478 * add child device descriptions into its scope
479 */
b23046ab 480 PCIBus *sec_bus = pci_bridge_get_sec_bus(PCI_BRIDGE(pdev));
b23046ab 481
62b52c26 482 build_append_pci_bus_devices(dev, sec_bus, pcihp_bridge_en);
8dcf525a 483 }
62b52c26
IM
484 /* slot descriptor has been composed, add it into parent context */
485 aml_append(parent_scope, dev);
8dcf525a
MT
486 }
487
488 if (bsel) {
62b52c26 489 aml_append(parent_scope, notify_method);
99fd437d
MT
490 }
491
492 /* Append PCNT method to notify about events on local and child buses.
df4008c9
AS
493 * Add this method for root bus only when hotplug is enabled since DSDT
494 * expects it.
72c194f7 495 */
df4008c9
AS
496 if (bsel || pcihp_bridge_en) {
497 method = aml_method("PCNT", 0, AML_NOTSERIALIZED);
01b2ffce 498
811c74fb
PMD
499 /* If bus supports hotplug select it and notify about local events */
500 if (bsel) {
501 uint64_t bsel_val = qnum_get_uint(qobject_to(QNum, bsel));
502
503 aml_append(method, aml_store(aml_int(bsel_val), aml_name("BNUM")));
504 aml_append(method, aml_call2("DVNT", aml_name("PCIU"),
505 aml_int(1))); /* Device Check */
506 aml_append(method, aml_call2("DVNT", aml_name("PCID"),
507 aml_int(3))); /* Eject Request */
508 }
99fd437d 509
811c74fb
PMD
510 /* Notify about child bus events in any case */
511 if (pcihp_bridge_en) {
512 QLIST_FOREACH(sec, &bus->child, sibling) {
513 int32_t devfn = sec->parent_dev->devfn;
62b52c26 514
811c74fb
PMD
515 if (pci_bus_is_root(sec) || pci_bus_is_express(sec)) {
516 continue;
517 }
c99cb18e 518
811c74fb
PMD
519 aml_append(method, aml_name("^S%.02X.PCNT", devfn));
520 }
99fd437d 521 }
df4008c9 522
df4008c9
AS
523 aml_append(parent_scope, method);
524 }
cb3e7f08 525 qobject_unref(bsel);
72c194f7
MT
526}
527
b7f23f62
IM
528Aml *aml_pci_device_dsm(void)
529{
530 Aml *method, *UUID, *ifctx, *ifctx1, *ifctx2, *ifctx3, *elsectx;
531 Aml *acpi_index = aml_local(0);
532 Aml *zero = aml_int(0);
533 Aml *bnum = aml_arg(4);
534 Aml *func = aml_arg(2);
535 Aml *rev = aml_arg(1);
536 Aml *sun = aml_arg(5);
537
538 method = aml_method("PDSM", 6, AML_SERIALIZED);
539
540 /*
541 * PCI Firmware Specification 3.1
542 * 4.6. _DSM Definitions for PCI
543 */
544 UUID = aml_touuid("E5C937D0-3553-4D7A-9117-EA4D19C3434D");
545 ifctx = aml_if(aml_equal(aml_arg(0), UUID));
546 {
547 aml_append(ifctx, aml_store(aml_call2("AIDX", bnum, sun), acpi_index));
548 ifctx1 = aml_if(aml_equal(func, zero));
549 {
550 uint8_t byte_list[1];
551
552 ifctx2 = aml_if(aml_equal(rev, aml_int(2)));
553 {
554 /*
555 * advertise function 7 if device has acpi-index
556 * acpi_index values:
557 * 0: not present (default value)
558 * FFFFFFFF: not supported (old QEMU without PIDX reg)
559 * other: device's acpi-index
560 */
561 ifctx3 = aml_if(aml_lnot(
562 aml_or(aml_equal(acpi_index, zero),
563 aml_equal(acpi_index, aml_int(0xFFFFFFFF)), NULL)
564 ));
565 {
566 byte_list[0] =
567 1 /* have supported functions */ |
568 1 << 7 /* support for function 7 */
569 ;
570 aml_append(ifctx3, aml_return(aml_buffer(1, byte_list)));
571 }
572 aml_append(ifctx2, ifctx3);
573 }
574 aml_append(ifctx1, ifctx2);
575
576 byte_list[0] = 0; /* nothing supported */
577 aml_append(ifctx1, aml_return(aml_buffer(1, byte_list)));
578 }
579 aml_append(ifctx, ifctx1);
580 elsectx = aml_else();
581 /*
582 * PCI Firmware Specification 3.1
583 * 4.6.7. _DSM for Naming a PCI or PCI Express Device Under
584 * Operating Systems
585 */
586 ifctx1 = aml_if(aml_equal(func, aml_int(7)));
587 {
588 Aml *pkg = aml_package(2);
589 Aml *ret = aml_local(1);
590
591 aml_append(pkg, zero);
592 /*
593 * optional, if not impl. should return null string
594 */
595 aml_append(pkg, aml_string("%s", ""));
596 aml_append(ifctx1, aml_store(pkg, ret));
597 /*
598 * update acpi-index to actual value
599 */
600 aml_append(ifctx1, aml_store(acpi_index, aml_index(ret, zero)));
601 aml_append(ifctx1, aml_return(ret));
602 }
603 aml_append(elsectx, ifctx1);
604 aml_append(ifctx, elsectx);
605 }
606 aml_append(method, ifctx);
607 return method;
608}
609
196e2137
IM
610/**
611 * build_prt_entry:
612 * @link_name: link name for PCI route entry
613 *
614 * build AML package containing a PCI route entry for @link_name
615 */
616static Aml *build_prt_entry(const char *link_name)
617{
618 Aml *a_zero = aml_int(0);
619 Aml *pkg = aml_package(4);
620 aml_append(pkg, a_zero);
621 aml_append(pkg, a_zero);
622 aml_append(pkg, aml_name("%s", link_name));
623 aml_append(pkg, a_zero);
624 return pkg;
625}
626
0d8935e3
MA
627/*
628 * initialize_route - Initialize the interrupt routing rule
629 * through a specific LINK:
630 * if (lnk_idx == idx)
631 * route using link 'link_name'
632 */
633static Aml *initialize_route(Aml *route, const char *link_name,
634 Aml *lnk_idx, int idx)
635{
636 Aml *if_ctx = aml_if(aml_equal(lnk_idx, aml_int(idx)));
196e2137 637 Aml *pkg = build_prt_entry(link_name);
0d8935e3 638
0d8935e3
MA
639 aml_append(if_ctx, aml_store(pkg, route));
640
641 return if_ctx;
642}
643
644/*
645 * build_prt - Define interrupt rounting rules
646 *
647 * Returns an array of 128 routes, one for each device,
648 * based on device location.
649 * The main goal is to equaly distribute the interrupts
650 * over the 4 existing ACPI links (works only for i440fx).
651 * The hash function is (slot + pin) & 3 -> "LNK[D|A|B|C]".
652 *
653 */
196e2137 654static Aml *build_prt(bool is_pci0_prt)
0d8935e3
MA
655{
656 Aml *method, *while_ctx, *pin, *res;
657
4dbfc881 658 method = aml_method("_PRT", 0, AML_NOTSERIALIZED);
0d8935e3
MA
659 res = aml_local(0);
660 pin = aml_local(1);
661 aml_append(method, aml_store(aml_package(128), res));
662 aml_append(method, aml_store(aml_int(0), pin));
663
664 /* while (pin < 128) */
665 while_ctx = aml_while(aml_lless(pin, aml_int(128)));
666 {
667 Aml *slot = aml_local(2);
668 Aml *lnk_idx = aml_local(3);
669 Aml *route = aml_local(4);
670
671 /* slot = pin >> 2 */
672 aml_append(while_ctx,
c360639a 673 aml_store(aml_shiftright(pin, aml_int(2), NULL), slot));
0d8935e3
MA
674 /* lnk_idx = (slot + pin) & 3 */
675 aml_append(while_ctx,
5530427f
IM
676 aml_store(aml_and(aml_add(pin, slot, NULL), aml_int(3), NULL),
677 lnk_idx));
0d8935e3
MA
678
679 /* route[2] = "LNK[D|A|B|C]", selection based on pin % 3 */
680 aml_append(while_ctx, initialize_route(route, "LNKD", lnk_idx, 0));
196e2137
IM
681 if (is_pci0_prt) {
682 Aml *if_device_1, *if_pin_4, *else_pin_4;
683
684 /* device 1 is the power-management device, needs SCI */
685 if_device_1 = aml_if(aml_equal(lnk_idx, aml_int(1)));
686 {
687 if_pin_4 = aml_if(aml_equal(pin, aml_int(4)));
688 {
689 aml_append(if_pin_4,
690 aml_store(build_prt_entry("LNKS"), route));
691 }
692 aml_append(if_device_1, if_pin_4);
693 else_pin_4 = aml_else();
694 {
695 aml_append(else_pin_4,
696 aml_store(build_prt_entry("LNKA"), route));
697 }
698 aml_append(if_device_1, else_pin_4);
699 }
700 aml_append(while_ctx, if_device_1);
701 } else {
702 aml_append(while_ctx, initialize_route(route, "LNKA", lnk_idx, 1));
703 }
0d8935e3
MA
704 aml_append(while_ctx, initialize_route(route, "LNKB", lnk_idx, 2));
705 aml_append(while_ctx, initialize_route(route, "LNKC", lnk_idx, 3));
706
707 /* route[0] = 0x[slot]FFFF */
708 aml_append(while_ctx,
ca3df95d
IM
709 aml_store(aml_or(aml_shiftleft(slot, aml_int(16)), aml_int(0xFFFF),
710 NULL),
0d8935e3
MA
711 aml_index(route, aml_int(0))));
712 /* route[1] = pin & 3 */
713 aml_append(while_ctx,
5530427f
IM
714 aml_store(aml_and(pin, aml_int(3), NULL),
715 aml_index(route, aml_int(1))));
0d8935e3
MA
716 /* res[pin] = route */
717 aml_append(while_ctx, aml_store(route, aml_index(res, pin)));
718 /* pin++ */
719 aml_append(while_ctx, aml_increment(pin));
720 }
721 aml_append(method, while_ctx);
722 /* return res*/
723 aml_append(method, aml_return(res));
724
725 return method;
726}
727
a57d708d
IM
728static void build_hpet_aml(Aml *table)
729{
730 Aml *crs;
731 Aml *field;
732 Aml *method;
733 Aml *if_ctx;
734 Aml *scope = aml_scope("_SB");
735 Aml *dev = aml_device("HPET");
736 Aml *zero = aml_int(0);
737 Aml *id = aml_local(0);
738 Aml *period = aml_local(1);
739
740 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0103")));
741 aml_append(dev, aml_name_decl("_UID", zero));
742
743 aml_append(dev,
3f3009c0
XG
744 aml_operation_region("HPTM", AML_SYSTEM_MEMORY, aml_int(HPET_BASE),
745 HPET_LEN));
a57d708d
IM
746 field = aml_field("HPTM", AML_DWORD_ACC, AML_LOCK, AML_PRESERVE);
747 aml_append(field, aml_named_field("VEND", 32));
748 aml_append(field, aml_named_field("PRD", 32));
749 aml_append(dev, field);
750
751 method = aml_method("_STA", 0, AML_NOTSERIALIZED);
752 aml_append(method, aml_store(aml_name("VEND"), id));
753 aml_append(method, aml_store(aml_name("PRD"), period));
754 aml_append(method, aml_shiftright(id, aml_int(16), id));
755 if_ctx = aml_if(aml_lor(aml_equal(id, zero),
756 aml_equal(id, aml_int(0xffff))));
757 {
758 aml_append(if_ctx, aml_return(zero));
759 }
760 aml_append(method, if_ctx);
761
762 if_ctx = aml_if(aml_lor(aml_equal(period, zero),
763 aml_lgreater(period, aml_int(100000000))));
764 {
765 aml_append(if_ctx, aml_return(zero));
766 }
767 aml_append(method, if_ctx);
768
769 aml_append(method, aml_return(aml_int(0x0F)));
770 aml_append(dev, method);
771
772 crs = aml_resource_template();
773 aml_append(crs, aml_memory32_fixed(HPET_BASE, HPET_LEN, AML_READ_ONLY));
774 aml_append(dev, aml_name_decl("_CRS", crs));
775
776 aml_append(scope, dev);
777 aml_append(table, scope);
778}
779
6775d15d
JD
780static Aml *build_vmbus_device_aml(VMBusBridge *vmbus_bridge)
781{
782 Aml *dev;
783 Aml *method;
784 Aml *crs;
785
786 dev = aml_device("VMBS");
787 aml_append(dev, aml_name_decl("STA", aml_int(0xF)));
788 aml_append(dev, aml_name_decl("_HID", aml_string("VMBus")));
789 aml_append(dev, aml_name_decl("_UID", aml_int(0x0)));
790 aml_append(dev, aml_name_decl("_DDN", aml_string("VMBUS")));
791
792 method = aml_method("_DIS", 0, AML_NOTSERIALIZED);
793 aml_append(method, aml_store(aml_and(aml_name("STA"), aml_int(0xD), NULL),
794 aml_name("STA")));
795 aml_append(dev, method);
796
797 method = aml_method("_PS0", 0, AML_NOTSERIALIZED);
798 aml_append(method, aml_store(aml_or(aml_name("STA"), aml_int(0xF), NULL),
799 aml_name("STA")));
800 aml_append(dev, method);
801
802 method = aml_method("_STA", 0, AML_NOTSERIALIZED);
803 aml_append(method, aml_return(aml_name("STA")));
804 aml_append(dev, method);
805
806 aml_append(dev, aml_name_decl("_PS3", aml_int(0x0)));
807
808 crs = aml_resource_template();
8f06f22f 809 aml_append(crs, aml_irq_no_flags(vmbus_bridge->irq));
6775d15d
JD
810 aml_append(dev, aml_name_decl("_CRS", crs));
811
812 return dev;
813}
814
ee135849
IM
815static void build_isa_devices_aml(Aml *table)
816{
86e91dd7 817 bool ambiguous;
86e91dd7 818 Object *obj = object_resolve_path_type("", TYPE_ISA_BUS, &ambiguous);
13371f9b 819 Aml *scope;
ee135849 820
13371f9b
GH
821 assert(obj && !ambiguous);
822
823 scope = aml_scope("_SB.PCI0.ISA");
824 build_acpi_ipmi_devices(scope, BUS(obj), "\\_SB.PCI0.ISA");
825 isa_build_aml(ISA_BUS(obj), scope);
86e91dd7 826
ee135849
IM
827 aml_append(table, scope);
828}
829
3892a2b7
IM
830static void build_dbg_aml(Aml *table)
831{
832 Aml *field;
833 Aml *method;
834 Aml *while_ctx;
835 Aml *scope = aml_scope("\\");
836 Aml *buf = aml_local(0);
837 Aml *len = aml_local(1);
838 Aml *idx = aml_local(2);
839
840 aml_append(scope,
3f3009c0 841 aml_operation_region("DBG", AML_SYSTEM_IO, aml_int(0x0402), 0x01));
3892a2b7
IM
842 field = aml_field("DBG", AML_BYTE_ACC, AML_NOLOCK, AML_PRESERVE);
843 aml_append(field, aml_named_field("DBGB", 8));
844 aml_append(scope, field);
845
846 method = aml_method("DBUG", 1, AML_NOTSERIALIZED);
847
848 aml_append(method, aml_to_hexstring(aml_arg(0), buf));
849 aml_append(method, aml_to_buffer(buf, buf));
850 aml_append(method, aml_subtract(aml_sizeof(buf), aml_int(1), len));
851 aml_append(method, aml_store(aml_int(0), idx));
852
853 while_ctx = aml_while(aml_lless(idx, len));
854 aml_append(while_ctx,
855 aml_store(aml_derefof(aml_index(buf, idx)), aml_name("DBGB")));
856 aml_append(while_ctx, aml_increment(idx));
857 aml_append(method, while_ctx);
858
859 aml_append(method, aml_store(aml_int(0x0A), aml_name("DBGB")));
860 aml_append(scope, method);
861
862 aml_append(table, scope);
863}
864
c35b6e80
IM
865static Aml *build_link_dev(const char *name, uint8_t uid, Aml *reg)
866{
867 Aml *dev;
868 Aml *crs;
869 Aml *method;
870 uint32_t irqs[] = {5, 10, 11};
871
872 dev = aml_device("%s", name);
873 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C0F")));
874 aml_append(dev, aml_name_decl("_UID", aml_int(uid)));
875
876 crs = aml_resource_template();
877 aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL, AML_ACTIVE_HIGH,
878 AML_SHARED, irqs, ARRAY_SIZE(irqs)));
879 aml_append(dev, aml_name_decl("_PRS", crs));
880
881 method = aml_method("_STA", 0, AML_NOTSERIALIZED);
882 aml_append(method, aml_return(aml_call1("IQST", reg)));
883 aml_append(dev, method);
884
885 method = aml_method("_DIS", 0, AML_NOTSERIALIZED);
886 aml_append(method, aml_or(reg, aml_int(0x80), reg));
887 aml_append(dev, method);
888
889 method = aml_method("_CRS", 0, AML_NOTSERIALIZED);
890 aml_append(method, aml_return(aml_call1("IQCR", reg)));
891 aml_append(dev, method);
892
893 method = aml_method("_SRS", 1, AML_NOTSERIALIZED);
894 aml_append(method, aml_create_dword_field(aml_arg(0), aml_int(5), "PRRI"));
895 aml_append(method, aml_store(aml_name("PRRI"), reg));
896 aml_append(dev, method);
897
898 return dev;
899 }
900
80b32df5
IM
901static Aml *build_gsi_link_dev(const char *name, uint8_t uid, uint8_t gsi)
902{
903 Aml *dev;
904 Aml *crs;
905 Aml *method;
906 uint32_t irqs;
907
908 dev = aml_device("%s", name);
909 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C0F")));
910 aml_append(dev, aml_name_decl("_UID", aml_int(uid)));
911
912 crs = aml_resource_template();
913 irqs = gsi;
914 aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL, AML_ACTIVE_HIGH,
915 AML_SHARED, &irqs, 1));
916 aml_append(dev, aml_name_decl("_PRS", crs));
917
918 aml_append(dev, aml_name_decl("_CRS", crs));
919
c82f503d
MA
920 /*
921 * _DIS can be no-op because the interrupt cannot be disabled.
922 */
923 method = aml_method("_DIS", 0, AML_NOTSERIALIZED);
924 aml_append(dev, method);
925
80b32df5
IM
926 method = aml_method("_SRS", 1, AML_NOTSERIALIZED);
927 aml_append(dev, method);
928
929 return dev;
930}
931
16682a9d
IM
932/* _CRS method - get current settings */
933static Aml *build_iqcr_method(bool is_piix4)
934{
935 Aml *if_ctx;
936 uint32_t irqs;
937 Aml *method = aml_method("IQCR", 1, AML_SERIALIZED);
938 Aml *crs = aml_resource_template();
939
940 irqs = 0;
941 aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL,
942 AML_ACTIVE_HIGH, AML_SHARED, &irqs, 1));
943 aml_append(method, aml_name_decl("PRR0", crs));
944
945 aml_append(method,
946 aml_create_dword_field(aml_name("PRR0"), aml_int(5), "PRRI"));
947
948 if (is_piix4) {
949 if_ctx = aml_if(aml_lless(aml_arg(0), aml_int(0x80)));
950 aml_append(if_ctx, aml_store(aml_arg(0), aml_name("PRRI")));
951 aml_append(method, if_ctx);
952 } else {
953 aml_append(method,
954 aml_store(aml_and(aml_arg(0), aml_int(0xF), NULL),
955 aml_name("PRRI")));
956 }
957
958 aml_append(method, aml_return(aml_name("PRR0")));
959 return method;
960}
961
78e1ad05
IM
962/* _STA method - get status */
963static Aml *build_irq_status_method(void)
964{
965 Aml *if_ctx;
966 Aml *method = aml_method("IQST", 1, AML_NOTSERIALIZED);
967
968 if_ctx = aml_if(aml_and(aml_int(0x80), aml_arg(0), NULL));
969 aml_append(if_ctx, aml_return(aml_int(0x09)));
970 aml_append(method, if_ctx);
971 aml_append(method, aml_return(aml_int(0x0B)));
972 return method;
973}
974
e4db2798
IM
975static void build_piix4_pci0_int(Aml *table)
976{
c35b6e80
IM
977 Aml *dev;
978 Aml *crs;
e4db2798 979 Aml *field;
c35b6e80
IM
980 Aml *method;
981 uint32_t irqs;
e4db2798 982 Aml *sb_scope = aml_scope("_SB");
196e2137
IM
983 Aml *pci0_scope = aml_scope("PCI0");
984
985 aml_append(pci0_scope, build_prt(true));
986 aml_append(sb_scope, pci0_scope);
e4db2798
IM
987
988 field = aml_field("PCI0.ISA.P40C", AML_BYTE_ACC, AML_NOLOCK, AML_PRESERVE);
989 aml_append(field, aml_named_field("PRQ0", 8));
990 aml_append(field, aml_named_field("PRQ1", 8));
991 aml_append(field, aml_named_field("PRQ2", 8));
992 aml_append(field, aml_named_field("PRQ3", 8));
993 aml_append(sb_scope, field);
994
78e1ad05 995 aml_append(sb_scope, build_irq_status_method());
16682a9d 996 aml_append(sb_scope, build_iqcr_method(true));
100681cc 997
c35b6e80
IM
998 aml_append(sb_scope, build_link_dev("LNKA", 0, aml_name("PRQ0")));
999 aml_append(sb_scope, build_link_dev("LNKB", 1, aml_name("PRQ1")));
1000 aml_append(sb_scope, build_link_dev("LNKC", 2, aml_name("PRQ2")));
1001 aml_append(sb_scope, build_link_dev("LNKD", 3, aml_name("PRQ3")));
1002
1003 dev = aml_device("LNKS");
1004 {
1005 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C0F")));
1006 aml_append(dev, aml_name_decl("_UID", aml_int(4)));
1007
1008 crs = aml_resource_template();
1009 irqs = 9;
1010 aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL,
1011 AML_ACTIVE_HIGH, AML_SHARED,
1012 &irqs, 1));
1013 aml_append(dev, aml_name_decl("_PRS", crs));
1014
1015 /* The SCI cannot be disabled and is always attached to GSI 9,
1016 * so these are no-ops. We only need this link to override the
1017 * polarity to active high and match the content of the MADT.
1018 */
1019 method = aml_method("_STA", 0, AML_NOTSERIALIZED);
1020 aml_append(method, aml_return(aml_int(0x0b)));
1021 aml_append(dev, method);
1022
1023 method = aml_method("_DIS", 0, AML_NOTSERIALIZED);
1024 aml_append(dev, method);
1025
1026 method = aml_method("_CRS", 0, AML_NOTSERIALIZED);
1027 aml_append(method, aml_return(aml_name("_PRS")));
1028 aml_append(dev, method);
1029
1030 method = aml_method("_SRS", 1, AML_NOTSERIALIZED);
1031 aml_append(dev, method);
1032 }
1033 aml_append(sb_scope, dev);
1034
e4db2798
IM
1035 aml_append(table, sb_scope);
1036}
1037
22b5b8bf
IM
1038static void append_q35_prt_entry(Aml *ctx, uint32_t nr, const char *name)
1039{
1040 int i;
1041 int head;
1042 Aml *pkg;
1043 char base = name[3] < 'E' ? 'A' : 'E';
1044 char *s = g_strdup(name);
1045 Aml *a_nr = aml_int((nr << 16) | 0xffff);
1046
1047 assert(strlen(s) == 4);
1048
1049 head = name[3] - base;
1050 for (i = 0; i < 4; i++) {
1051 if (head + i > 3) {
1052 head = i * -1;
1053 }
1054 s[3] = base + head + i;
1055 pkg = aml_package(4);
1056 aml_append(pkg, a_nr);
1057 aml_append(pkg, aml_int(i));
1058 aml_append(pkg, aml_name("%s", s));
1059 aml_append(pkg, aml_int(0));
1060 aml_append(ctx, pkg);
1061 }
1062 g_free(s);
1063}
1064
1065static Aml *build_q35_routing_table(const char *str)
1066{
1067 int i;
1068 Aml *pkg;
1069 char *name = g_strdup_printf("%s ", str);
1070
1071 pkg = aml_package(128);
1072 for (i = 0; i < 0x18; i++) {
1073 name[3] = 'E' + (i & 0x3);
1074 append_q35_prt_entry(pkg, i, name);
1075 }
1076
1077 name[3] = 'E';
1078 append_q35_prt_entry(pkg, 0x18, name);
1079
1080 /* INTA -> PIRQA for slot 25 - 31, see the default value of D<N>IR */
1081 for (i = 0x0019; i < 0x1e; i++) {
1082 name[3] = 'A';
1083 append_q35_prt_entry(pkg, i, name);
1084 }
1085
1086 /* PCIe->PCI bridge. use PIRQ[E-H] */
1087 name[3] = 'E';
1088 append_q35_prt_entry(pkg, 0x1e, name);
1089 name[3] = 'A';
1090 append_q35_prt_entry(pkg, 0x1f, name);
1091
1092 g_free(name);
1093 return pkg;
1094}
1095
80b32df5
IM
1096static void build_q35_pci0_int(Aml *table)
1097{
41f95a52 1098 Aml *field;
0dafe3b3 1099 Aml *method;
80b32df5 1100 Aml *sb_scope = aml_scope("_SB");
0dafe3b3
IM
1101 Aml *pci0_scope = aml_scope("PCI0");
1102
e9fce798
IM
1103 /* Zero => PIC mode, One => APIC Mode */
1104 aml_append(table, aml_name_decl("PICF", aml_int(0)));
1105 method = aml_method("_PIC", 1, AML_NOTSERIALIZED);
1106 {
1107 aml_append(method, aml_store(aml_arg(0), aml_name("PICF")));
1108 }
1109 aml_append(table, method);
1110
65aef4de
IM
1111 aml_append(pci0_scope,
1112 aml_name_decl("PRTP", build_q35_routing_table("LNK")));
22b5b8bf
IM
1113 aml_append(pci0_scope,
1114 aml_name_decl("PRTA", build_q35_routing_table("GSI")));
1115
0dafe3b3
IM
1116 method = aml_method("_PRT", 0, AML_NOTSERIALIZED);
1117 {
1118 Aml *if_ctx;
1119 Aml *else_ctx;
1120
1121 /* PCI IRQ routing table, example from ACPI 2.0a specification,
1122 section 6.2.8.1 */
1123 /* Note: we provide the same info as the PCI routing
1124 table of the Bochs BIOS */
1125 if_ctx = aml_if(aml_equal(aml_name("PICF"), aml_int(0)));
1126 aml_append(if_ctx, aml_return(aml_name("PRTP")));
1127 aml_append(method, if_ctx);
1128 else_ctx = aml_else();
1129 aml_append(else_ctx, aml_return(aml_name("PRTA")));
1130 aml_append(method, else_ctx);
1131 }
1132 aml_append(pci0_scope, method);
1133 aml_append(sb_scope, pci0_scope);
80b32df5 1134
41f95a52
IM
1135 field = aml_field("PCI0.ISA.PIRQ", AML_BYTE_ACC, AML_NOLOCK, AML_PRESERVE);
1136 aml_append(field, aml_named_field("PRQA", 8));
1137 aml_append(field, aml_named_field("PRQB", 8));
1138 aml_append(field, aml_named_field("PRQC", 8));
1139 aml_append(field, aml_named_field("PRQD", 8));
1140 aml_append(field, aml_reserved_field(0x20));
1141 aml_append(field, aml_named_field("PRQE", 8));
1142 aml_append(field, aml_named_field("PRQF", 8));
1143 aml_append(field, aml_named_field("PRQG", 8));
1144 aml_append(field, aml_named_field("PRQH", 8));
1145 aml_append(sb_scope, field);
1146
78e1ad05 1147 aml_append(sb_scope, build_irq_status_method());
16682a9d
IM
1148 aml_append(sb_scope, build_iqcr_method(false));
1149
12e3b1f7
IM
1150 aml_append(sb_scope, build_link_dev("LNKA", 0, aml_name("PRQA")));
1151 aml_append(sb_scope, build_link_dev("LNKB", 1, aml_name("PRQB")));
1152 aml_append(sb_scope, build_link_dev("LNKC", 2, aml_name("PRQC")));
1153 aml_append(sb_scope, build_link_dev("LNKD", 3, aml_name("PRQD")));
1154 aml_append(sb_scope, build_link_dev("LNKE", 4, aml_name("PRQE")));
1155 aml_append(sb_scope, build_link_dev("LNKF", 5, aml_name("PRQF")));
1156 aml_append(sb_scope, build_link_dev("LNKG", 6, aml_name("PRQG")));
1157 aml_append(sb_scope, build_link_dev("LNKH", 7, aml_name("PRQH")));
1158
6a991e07
MA
1159 aml_append(sb_scope, build_gsi_link_dev("GSIA", 0x10, 0x10));
1160 aml_append(sb_scope, build_gsi_link_dev("GSIB", 0x11, 0x11));
1161 aml_append(sb_scope, build_gsi_link_dev("GSIC", 0x12, 0x12));
1162 aml_append(sb_scope, build_gsi_link_dev("GSID", 0x13, 0x13));
1163 aml_append(sb_scope, build_gsi_link_dev("GSIE", 0x14, 0x14));
1164 aml_append(sb_scope, build_gsi_link_dev("GSIF", 0x15, 0x15));
1165 aml_append(sb_scope, build_gsi_link_dev("GSIG", 0x16, 0x16));
1166 aml_append(sb_scope, build_gsi_link_dev("GSIH", 0x17, 0x17));
80b32df5
IM
1167
1168 aml_append(table, sb_scope);
1169}
1170
e3fb55f0
IY
1171static Aml *build_q35_dram_controller(const AcpiMcfgInfo *mcfg)
1172{
1173 Aml *dev;
1174 Aml *resource_template;
1175
1176 /* DRAM controller */
1177 dev = aml_device("DRAC");
1178 aml_append(dev, aml_name_decl("_HID", aml_string("PNP0C01")));
1179
1180 resource_template = aml_resource_template();
1181 if (mcfg->base + mcfg->size - 1 >= (1ULL << 32)) {
1182 aml_append(resource_template,
1183 aml_qword_memory(AML_POS_DECODE,
1184 AML_MIN_FIXED,
1185 AML_MAX_FIXED,
1186 AML_NON_CACHEABLE,
1187 AML_READ_WRITE,
1188 0x0000000000000000,
1189 mcfg->base,
1190 mcfg->base + mcfg->size - 1,
1191 0x0000000000000000,
1192 mcfg->size));
1193 } else {
1194 aml_append(resource_template,
1195 aml_dword_memory(AML_POS_DECODE,
1196 AML_MIN_FIXED,
1197 AML_MAX_FIXED,
1198 AML_NON_CACHEABLE,
1199 AML_READ_WRITE,
1200 0x0000000000000000,
1201 mcfg->base,
1202 mcfg->base + mcfg->size - 1,
1203 0x0000000000000000,
1204 mcfg->size));
1205 }
1206 aml_append(dev, aml_name_decl("_CRS", resource_template));
1207
1208 return dev;
1209}
1210
41f95a52
IM
1211static void build_q35_isa_bridge(Aml *table)
1212{
1213 Aml *dev;
1214 Aml *scope;
41f95a52
IM
1215
1216 scope = aml_scope("_SB.PCI0");
1217 dev = aml_device("ISA");
1218 aml_append(dev, aml_name_decl("_ADR", aml_int(0x001F0000)));
1219
1220 /* ICH9 PCI to ISA irq remapping */
1221 aml_append(dev, aml_operation_region("PIRQ", AML_PCI_CONFIG,
3f3009c0 1222 aml_int(0x60), 0x0C));
41f95a52 1223
41f95a52
IM
1224 aml_append(scope, dev);
1225 aml_append(table, scope);
1226}
1227
e4db2798
IM
1228static void build_piix4_isa_bridge(Aml *table)
1229{
1230 Aml *dev;
1231 Aml *scope;
e4db2798
IM
1232
1233 scope = aml_scope("_SB.PCI0");
1234 dev = aml_device("ISA");
1235 aml_append(dev, aml_name_decl("_ADR", aml_int(0x00010000)));
1236
1237 /* PIIX PCI to ISA irq remapping */
1238 aml_append(dev, aml_operation_region("P40C", AML_PCI_CONFIG,
3f3009c0 1239 aml_int(0x60), 0x04));
e4db2798
IM
1240
1241 aml_append(scope, dev);
1242 aml_append(table, scope);
1243}
1244
b616ec4d
IM
1245static void build_piix4_pci_hotplug(Aml *table)
1246{
1247 Aml *scope;
1248 Aml *field;
1249 Aml *method;
1250
1251 scope = aml_scope("_SB.PCI0");
1252
1253 aml_append(scope,
3f3009c0 1254 aml_operation_region("PCST", AML_SYSTEM_IO, aml_int(0xae00), 0x08));
b616ec4d
IM
1255 field = aml_field("PCST", AML_DWORD_ACC, AML_NOLOCK, AML_WRITE_AS_ZEROS);
1256 aml_append(field, aml_named_field("PCIU", 32));
1257 aml_append(field, aml_named_field("PCID", 32));
1258 aml_append(scope, field);
1259
1260 aml_append(scope,
3f3009c0 1261 aml_operation_region("SEJ", AML_SYSTEM_IO, aml_int(0xae08), 0x04));
b616ec4d
IM
1262 field = aml_field("SEJ", AML_DWORD_ACC, AML_NOLOCK, AML_WRITE_AS_ZEROS);
1263 aml_append(field, aml_named_field("B0EJ", 32));
1264 aml_append(scope, field);
1265
1266 aml_append(scope,
b32bd763 1267 aml_operation_region("BNMR", AML_SYSTEM_IO, aml_int(0xae10), 0x08));
b616ec4d
IM
1268 field = aml_field("BNMR", AML_DWORD_ACC, AML_NOLOCK, AML_WRITE_AS_ZEROS);
1269 aml_append(field, aml_named_field("BNUM", 32));
b32bd763 1270 aml_append(field, aml_named_field("PIDX", 32));
b616ec4d
IM
1271 aml_append(scope, field);
1272
1273 aml_append(scope, aml_mutex("BLCK", 0));
1274
1275 method = aml_method("PCEJ", 2, AML_NOTSERIALIZED);
1276 aml_append(method, aml_acquire(aml_name("BLCK"), 0xFFFF));
1277 aml_append(method, aml_store(aml_arg(0), aml_name("BNUM")));
1278 aml_append(method,
1279 aml_store(aml_shiftleft(aml_int(1), aml_arg(1)), aml_name("B0EJ")));
1280 aml_append(method, aml_release(aml_name("BLCK")));
1281 aml_append(method, aml_return(aml_int(0)));
1282 aml_append(scope, method);
1283
b32bd763
IM
1284 method = aml_method("AIDX", 2, AML_NOTSERIALIZED);
1285 aml_append(method, aml_acquire(aml_name("BLCK"), 0xFFFF));
1286 aml_append(method, aml_store(aml_arg(0), aml_name("BNUM")));
1287 aml_append(method,
1288 aml_store(aml_shiftleft(aml_int(1), aml_arg(1)), aml_name("PIDX")));
1289 aml_append(method, aml_store(aml_name("PIDX"), aml_local(0)));
1290 aml_append(method, aml_release(aml_name("BLCK")));
1291 aml_append(method, aml_return(aml_local(0)));
1292 aml_append(scope, method);
1293
b7f23f62
IM
1294 aml_append(scope, aml_pci_device_dsm());
1295
b616ec4d
IM
1296 aml_append(table, scope);
1297}
1298
f97a88a8
IM
1299static Aml *build_q35_osc_method(void)
1300{
1301 Aml *if_ctx;
1302 Aml *if_ctx2;
1303 Aml *else_ctx;
1304 Aml *method;
1305 Aml *a_cwd1 = aml_name("CDW1");
b3c782db 1306 Aml *a_ctrl = aml_local(0);
f97a88a8
IM
1307
1308 method = aml_method("_OSC", 4, AML_NOTSERIALIZED);
1309 aml_append(method, aml_create_dword_field(aml_arg(3), aml_int(0), "CDW1"));
1310
1311 if_ctx = aml_if(aml_equal(
1312 aml_arg(0), aml_touuid("33DB4D5B-1FF7-401C-9657-7441C03DD766")));
1313 aml_append(if_ctx, aml_create_dword_field(aml_arg(3), aml_int(4), "CDW2"));
1314 aml_append(if_ctx, aml_create_dword_field(aml_arg(3), aml_int(8), "CDW3"));
1315
f97a88a8
IM
1316 aml_append(if_ctx, aml_store(aml_name("CDW3"), a_ctrl));
1317
1318 /*
1319 * Always allow native PME, AER (no dependencies)
a41c78c1 1320 * Allow SHPC (PCI bridges can have SHPC controller)
f97a88a8 1321 */
a41c78c1 1322 aml_append(if_ctx, aml_and(a_ctrl, aml_int(0x1F), a_ctrl));
f97a88a8
IM
1323
1324 if_ctx2 = aml_if(aml_lnot(aml_equal(aml_arg(1), aml_int(1))));
1325 /* Unknown revision */
1326 aml_append(if_ctx2, aml_or(a_cwd1, aml_int(0x08), a_cwd1));
1327 aml_append(if_ctx, if_ctx2);
1328
1329 if_ctx2 = aml_if(aml_lnot(aml_equal(aml_name("CDW3"), a_ctrl)));
1330 /* Capabilities bits were masked */
1331 aml_append(if_ctx2, aml_or(a_cwd1, aml_int(0x10), a_cwd1));
1332 aml_append(if_ctx, if_ctx2);
1333
1334 /* Update DWORD3 in the buffer */
1335 aml_append(if_ctx, aml_store(a_ctrl, aml_name("CDW3")));
1336 aml_append(method, if_ctx);
1337
1338 else_ctx = aml_else();
1339 /* Unrecognized UUID */
1340 aml_append(else_ctx, aml_or(a_cwd1, aml_int(4), a_cwd1));
1341 aml_append(method, else_ctx);
1342
1343 aml_append(method, aml_return(aml_arg(3)));
1344 return method;
1345}
b616ec4d 1346
ebe15582
CM
1347static void build_smb0(Aml *table, I2CBus *smbus, int devnr, int func)
1348{
1349 Aml *scope = aml_scope("_SB.PCI0");
1350 Aml *dev = aml_device("SMB0");
1351
ebe15582
CM
1352 aml_append(dev, aml_name_decl("_ADR", aml_int(devnr << 16 | func)));
1353 build_acpi_ipmi_devices(dev, BUS(smbus), "\\_SB.PCI0.SMB0");
1354 aml_append(scope, dev);
1355 aml_append(table, scope);
1356}
1357
72c194f7 1358static void
0e9b9eda 1359build_dsdt(GArray *table_data, BIOSLinker *linker,
adcb89d5 1360 AcpiPmInfo *pm, AcpiMiscInfo *misc,
01c9742d 1361 Range *pci_hole, Range *pci_hole64, MachineState *machine)
72c194f7 1362{
41fa5c04
IM
1363 CrsRangeEntry *entry;
1364 Aml *dsdt, *sb_scope, *scope, *dev, *method, *field, *pkg, *crs;
2df5a7b5 1365 CrsRangeSet crs_range_set;
fb306ffe 1366 PCMachineState *pcms = PC_MACHINE(machine);
679dd1a9 1367 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(machine);
f0bb276b 1368 X86MachineState *x86ms = X86_MACHINE(machine);
4a441836 1369 AcpiMcfgInfo mcfg;
e3fb55f0 1370 bool mcfg_valid = !!acpi_get_mcfg(&mcfg);
bef3492d 1371 uint32_t nr_mem = machine->ram_slots;
dcdca296 1372 int root_bus_limit = 0xFF;
41fa5c04 1373 PCIBus *bus = NULL;
ac6dd31e 1374 TPMIf *tpm = tpm_find();
72c194f7 1375 int i;
8f814ea1 1376 VMBusBridge *vmbus_bridge = vmbus_bridge_find();
72c194f7 1377
41fa5c04 1378 dsdt = init_aml_allocator();
2fd71f1b 1379
4ec8d2b3 1380 /* Reserve space for header */
41fa5c04
IM
1381 acpi_data_push(dsdt->buf, sizeof(AcpiTableHeader));
1382
1383 build_dbg_aml(dsdt);
1384 if (misc->is_piix4) {
1385 sb_scope = aml_scope("_SB");
1386 dev = aml_device("PCI0");
1387 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A03")));
1388 aml_append(dev, aml_name_decl("_ADR", aml_int(0)));
0a343a5a 1389 aml_append(dev, aml_name_decl("_UID", aml_int(pcmc->pci_root_uid)));
41fa5c04
IM
1390 aml_append(sb_scope, dev);
1391 aml_append(dsdt, sb_scope);
1392
51124bbf
SC
1393 if (misc->has_hpet) {
1394 build_hpet_aml(dsdt);
1395 }
41fa5c04
IM
1396 build_piix4_isa_bridge(dsdt);
1397 build_isa_devices_aml(dsdt);
df4008c9
AS
1398 if (pm->pcihp_bridge_en || pm->pcihp_root_en) {
1399 build_piix4_pci_hotplug(dsdt);
1400 }
41fa5c04
IM
1401 build_piix4_pci0_int(dsdt);
1402 } else {
41fa5c04
IM
1403 sb_scope = aml_scope("_SB");
1404 dev = aml_device("PCI0");
1405 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A08")));
1406 aml_append(dev, aml_name_decl("_CID", aml_eisaid("PNP0A03")));
1407 aml_append(dev, aml_name_decl("_ADR", aml_int(0)));
0a343a5a 1408 aml_append(dev, aml_name_decl("_UID", aml_int(pcmc->pci_root_uid)));
41fa5c04
IM
1409 aml_append(dev, build_q35_osc_method());
1410 aml_append(sb_scope, dev);
e3fb55f0
IY
1411 if (mcfg_valid) {
1412 aml_append(sb_scope, build_q35_dram_controller(&mcfg));
1413 }
998ba950
IM
1414
1415 if (pm->smi_on_cpuhp) {
1416 /* reserve SMI block resources, IO ports 0xB2, 0xB3 */
1417 dev = aml_device("PCI0.SMI0");
1418 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A06")));
1419 aml_append(dev, aml_name_decl("_UID", aml_string("SMI resources")));
1420 crs = aml_resource_template();
1421 aml_append(crs,
1422 aml_io(
1423 AML_DECODE16,
1424 ACPI_PORT_SMI_CMD,
1425 ACPI_PORT_SMI_CMD,
1426 1,
1427 2)
1428 );
1429 aml_append(dev, aml_name_decl("_CRS", crs));
1430 aml_append(dev, aml_operation_region("SMIR", AML_SYSTEM_IO,
1431 aml_int(ACPI_PORT_SMI_CMD), 2));
1432 field = aml_field("SMIR", AML_BYTE_ACC, AML_NOLOCK,
1433 AML_WRITE_AS_ZEROS);
1434 aml_append(field, aml_named_field("SMIC", 8));
1435 aml_append(field, aml_reserved_field(8));
1436 aml_append(dev, field);
1437 aml_append(sb_scope, dev);
1438 }
1439
41fa5c04
IM
1440 aml_append(dsdt, sb_scope);
1441
51124bbf
SC
1442 if (misc->has_hpet) {
1443 build_hpet_aml(dsdt);
1444 }
41fa5c04
IM
1445 build_q35_isa_bridge(dsdt);
1446 build_isa_devices_aml(dsdt);
1447 build_q35_pci0_int(dsdt);
ebe15582
CM
1448 if (pcms->smbus && !pcmc->do_not_add_smb_acpi) {
1449 build_smb0(dsdt, pcms->smbus, ICH9_SMB_DEV, ICH9_SMB_FUNC);
1450 }
41fa5c04
IM
1451 }
1452
8f814ea1
JD
1453 if (vmbus_bridge) {
1454 sb_scope = aml_scope("_SB");
1455 aml_append(sb_scope, build_vmbus_device_aml(vmbus_bridge));
1456 aml_append(dsdt, sb_scope);
1457 }
1458
679dd1a9
IM
1459 if (pcmc->legacy_cpu_hotplug) {
1460 build_legacy_cpu_hotplug_aml(dsdt, machine, pm->cpu_hp_io_base);
1461 } else {
1462 CPUHotplugFeatures opts = {
998ba950
IM
1463 .acpi_1_compatible = true, .has_legacy_cphp = true,
1464 .smi_path = pm->smi_on_cpuhp ? "\\_SB.PCI0.SMI0.SMIC" : NULL,
69dea9d6 1465 .fw_unplugs_cpu = pm->smi_on_cpu_unplug,
679dd1a9
IM
1466 };
1467 build_cpus_aml(dsdt, machine, opts, pm->cpu_hp_io_base,
1468 "\\_SB.PCI0", "\\_GPE._E02");
1469 }
091c466e
SK
1470
1471 if (pcms->memhp_io_base && nr_mem) {
1472 build_memory_hotplug_aml(dsdt, nr_mem, "\\_SB.PCI0",
1473 "\\_GPE._E03", AML_SYSTEM_IO,
1474 pcms->memhp_io_base);
1475 }
41fa5c04
IM
1476
1477 scope = aml_scope("_GPE");
1478 {
1479 aml_append(scope, aml_name_decl("_HID", aml_string("ACPI0006")));
1480
df4008c9 1481 if (misc->is_piix4 && (pm->pcihp_bridge_en || pm->pcihp_root_en)) {
41fa5c04
IM
1482 method = aml_method("_E01", 0, AML_NOTSERIALIZED);
1483 aml_append(method,
1484 aml_acquire(aml_name("\\_SB.PCI0.BLCK"), 0xFFFF));
1485 aml_append(method, aml_call0("\\_SB.PCI0.PCNT"));
1486 aml_append(method, aml_release(aml_name("\\_SB.PCI0.BLCK")));
1487 aml_append(scope, method);
41fa5c04
IM
1488 }
1489
f6a0d06b 1490 if (machine->nvdimms_state->is_enabled) {
b097cc52
XG
1491 method = aml_method("_E04", 0, AML_NOTSERIALIZED);
1492 aml_append(method, aml_notify(aml_name("\\_SB.NVDR"),
1493 aml_int(0x80)));
1494 aml_append(scope, method);
1495 }
41fa5c04
IM
1496 }
1497 aml_append(dsdt, scope);
72c194f7 1498
2df5a7b5 1499 crs_range_set_init(&crs_range_set);
81ed6482 1500 bus = PC_MACHINE(machine)->bus;
a4894206
MA
1501 if (bus) {
1502 QLIST_FOREACH(bus, &bus->child, sibling) {
1503 uint8_t bus_num = pci_bus_num(bus);
0e79e51a 1504 uint8_t numa_node = pci_bus_numa_node(bus);
a4894206
MA
1505
1506 /* look only for expander root buses */
1507 if (!pci_bus_is_root(bus)) {
1508 continue;
1509 }
1510
dcdca296
MA
1511 if (bus_num < root_bus_limit) {
1512 root_bus_limit = bus_num - 1;
1513 }
1514
a4894206
MA
1515 scope = aml_scope("\\_SB");
1516 dev = aml_device("PC%.02X", bus_num);
c96d9286 1517 aml_append(dev, aml_name_decl("_UID", aml_int(bus_num)));
a4894206 1518 aml_append(dev, aml_name_decl("_BBN", aml_int(bus_num)));
077dd742 1519 if (pci_bus_is_express(bus)) {
ee4b0c86
EY
1520 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A08")));
1521 aml_append(dev, aml_name_decl("_CID", aml_eisaid("PNP0A03")));
077dd742 1522 aml_append(dev, build_q35_osc_method());
ee4b0c86
EY
1523 } else {
1524 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A03")));
077dd742 1525 }
0e79e51a
MA
1526
1527 if (numa_node != NUMA_NODE_UNASSIGNED) {
1528 aml_append(dev, aml_name_decl("_PXM", aml_int(numa_node)));
1529 }
1530
196e2137 1531 aml_append(dev, build_prt(false));
e41ee855
JC
1532 crs = build_crs(PCI_HOST_BRIDGE(BUS(bus)->parent), &crs_range_set,
1533 0, 0, 0, 0);
a43c6e27 1534 aml_append(dev, aml_name_decl("_CRS", crs));
a4894206 1535 aml_append(scope, dev);
41fa5c04 1536 aml_append(dsdt, scope);
a4894206
MA
1537 }
1538 }
1539
4a441836
GH
1540 /*
1541 * At this point crs_range_set has all the ranges used by pci
1542 * busses *other* than PCI0. These ranges will be excluded from
1543 * the PCI0._CRS. Add mmconfig to the set so it will be excluded
1544 * too.
1545 */
e3fb55f0 1546 if (mcfg_valid) {
4a441836
GH
1547 crs_range_insert(crs_range_set.mem_ranges,
1548 mcfg.base, mcfg.base + mcfg.size - 1);
1549 }
1550
500b11ea 1551 scope = aml_scope("\\_SB.PCI0");
60efd429
IM
1552 /* build PCI0._CRS */
1553 crs = aml_resource_template();
1554 aml_append(crs,
ff80dc7f 1555 aml_word_bus_number(AML_MIN_FIXED, AML_MAX_FIXED, AML_POS_DECODE,
dcdca296
MA
1556 0x0000, 0x0, root_bus_limit,
1557 0x0000, root_bus_limit + 1));
ff80dc7f 1558 aml_append(crs, aml_io(AML_DECODE16, 0x0CF8, 0x0CF8, 0x01, 0x08));
60efd429
IM
1559
1560 aml_append(crs,
ff80dc7f
SZ
1561 aml_word_io(AML_MIN_FIXED, AML_MAX_FIXED,
1562 AML_POS_DECODE, AML_ENTIRE_RANGE,
60efd429 1563 0x0000, 0x0000, 0x0CF7, 0x0000, 0x0CF8));
dcdca296 1564
2df5a7b5
MA
1565 crs_replace_with_free_ranges(crs_range_set.io_ranges, 0x0D00, 0xFFFF);
1566 for (i = 0; i < crs_range_set.io_ranges->len; i++) {
1567 entry = g_ptr_array_index(crs_range_set.io_ranges, i);
dcdca296
MA
1568 aml_append(crs,
1569 aml_word_io(AML_MIN_FIXED, AML_MAX_FIXED,
1570 AML_POS_DECODE, AML_ENTIRE_RANGE,
1571 0x0000, entry->base, entry->limit,
1572 0x0000, entry->limit - entry->base + 1));
1573 }
1574
60efd429 1575 aml_append(crs,
ff80dc7f
SZ
1576 aml_dword_memory(AML_POS_DECODE, AML_MIN_FIXED, AML_MAX_FIXED,
1577 AML_CACHEABLE, AML_READ_WRITE,
60efd429 1578 0, 0x000A0000, 0x000BFFFF, 0, 0x00020000));
dcdca296 1579
2df5a7b5 1580 crs_replace_with_free_ranges(crs_range_set.mem_ranges,
a0efbf16
MA
1581 range_lob(pci_hole),
1582 range_upb(pci_hole));
2df5a7b5
MA
1583 for (i = 0; i < crs_range_set.mem_ranges->len; i++) {
1584 entry = g_ptr_array_index(crs_range_set.mem_ranges, i);
dcdca296
MA
1585 aml_append(crs,
1586 aml_dword_memory(AML_POS_DECODE, AML_MIN_FIXED, AML_MAX_FIXED,
1587 AML_NON_CACHEABLE, AML_READ_WRITE,
1588 0, entry->base, entry->limit,
1589 0, entry->limit - entry->base + 1));
1590 }
1591
a0efbf16 1592 if (!range_is_empty(pci_hole64)) {
16de88a4
MA
1593 crs_replace_with_free_ranges(crs_range_set.mem_64bit_ranges,
1594 range_lob(pci_hole64),
1595 range_upb(pci_hole64));
1596 for (i = 0; i < crs_range_set.mem_64bit_ranges->len; i++) {
1597 entry = g_ptr_array_index(crs_range_set.mem_64bit_ranges, i);
1598 aml_append(crs,
1599 aml_qword_memory(AML_POS_DECODE, AML_MIN_FIXED,
1600 AML_MAX_FIXED,
1601 AML_CACHEABLE, AML_READ_WRITE,
1602 0, entry->base, entry->limit,
1603 0, entry->limit - entry->base + 1));
1604 }
60efd429 1605 }
2b1c2e8e 1606
43bc7f84 1607 if (TPM_IS_TIS_ISA(tpm_find())) {
2b1c2e8e
IM
1608 aml_append(crs, aml_memory32_fixed(TPM_TIS_ADDR_BASE,
1609 TPM_TIS_ADDR_SIZE, AML_READ_WRITE));
1610 }
60efd429
IM
1611 aml_append(scope, aml_name_decl("_CRS", crs));
1612
d31c909e
IM
1613 /* reserve GPE0 block resources */
1614 dev = aml_device("GPE0");
1615 aml_append(dev, aml_name_decl("_HID", aml_string("PNP0A06")));
1616 aml_append(dev, aml_name_decl("_UID", aml_string("GPE0 resources")));
1617 /* device present, functioning, decoding, not shown in UI */
1618 aml_append(dev, aml_name_decl("_STA", aml_int(0xB)));
1619 crs = aml_resource_template();
1620 aml_append(crs,
937d1b58
IM
1621 aml_io(
1622 AML_DECODE16,
1623 pm->fadt.gpe0_blk.address,
1624 pm->fadt.gpe0_blk.address,
1625 1,
1626 pm->fadt.gpe0_blk.bit_width / 8)
d31c909e
IM
1627 );
1628 aml_append(dev, aml_name_decl("_CRS", crs));
1629 aml_append(scope, dev);
1630
2df5a7b5 1631 crs_range_set_free(&crs_range_set);
dcdca296 1632
500b11ea 1633 /* reserve PCIHP resources */
df4008c9 1634 if (pm->pcihp_io_len && (pm->pcihp_bridge_en || pm->pcihp_root_en)) {
500b11ea
IM
1635 dev = aml_device("PHPR");
1636 aml_append(dev, aml_name_decl("_HID", aml_string("PNP0A06")));
1637 aml_append(dev,
1638 aml_name_decl("_UID", aml_string("PCI Hotplug resources")));
1639 /* device present, functioning, decoding, not shown in UI */
1640 aml_append(dev, aml_name_decl("_STA", aml_int(0xB)));
1641 crs = aml_resource_template();
1642 aml_append(crs,
ff80dc7f 1643 aml_io(AML_DECODE16, pm->pcihp_io_base, pm->pcihp_io_base, 1,
500b11ea
IM
1644 pm->pcihp_io_len)
1645 );
1646 aml_append(dev, aml_name_decl("_CRS", crs));
1647 aml_append(scope, dev);
1648 }
41fa5c04 1649 aml_append(dsdt, scope);
500b11ea 1650
ebc3028f
IM
1651 /* create S3_ / S4_ / S5_ packages if necessary */
1652 scope = aml_scope("\\");
1653 if (!pm->s3_disabled) {
1654 pkg = aml_package(4);
1655 aml_append(pkg, aml_int(1)); /* PM1a_CNT.SLP_TYP */
1656 aml_append(pkg, aml_int(1)); /* PM1b_CNT.SLP_TYP, FIXME: not impl. */
1657 aml_append(pkg, aml_int(0)); /* reserved */
1658 aml_append(pkg, aml_int(0)); /* reserved */
1659 aml_append(scope, aml_name_decl("_S3", pkg));
1660 }
1661
1662 if (!pm->s4_disabled) {
1663 pkg = aml_package(4);
1664 aml_append(pkg, aml_int(pm->s4_val)); /* PM1a_CNT.SLP_TYP */
1665 /* PM1b_CNT.SLP_TYP, FIXME: not impl. */
1666 aml_append(pkg, aml_int(pm->s4_val));
1667 aml_append(pkg, aml_int(0)); /* reserved */
1668 aml_append(pkg, aml_int(0)); /* reserved */
1669 aml_append(scope, aml_name_decl("_S4", pkg));
1670 }
1671
1672 pkg = aml_package(4);
1673 aml_append(pkg, aml_int(0)); /* PM1a_CNT.SLP_TYP */
1674 aml_append(pkg, aml_int(0)); /* PM1b_CNT.SLP_TYP not impl. */
1675 aml_append(pkg, aml_int(0)); /* reserved */
1676 aml_append(pkg, aml_int(0)); /* reserved */
1677 aml_append(scope, aml_name_decl("_S5", pkg));
41fa5c04 1678 aml_append(dsdt, scope);
ebc3028f 1679
e2ec7568
GS
1680 /* create fw_cfg node, unconditionally */
1681 {
e2ec7568 1682 scope = aml_scope("\\_SB.PCI0");
0575c2fd 1683 fw_cfg_add_acpi_dsdt(scope, x86ms->fw_cfg);
e2ec7568
GS
1684 aml_append(dsdt, scope);
1685 }
1686
8ac6f7a6
IM
1687 if (misc->applesmc_io_base) {
1688 scope = aml_scope("\\_SB.PCI0.ISA");
1689 dev = aml_device("SMC");
1690
1691 aml_append(dev, aml_name_decl("_HID", aml_eisaid("APP0001")));
1692 /* device present, functioning, decoding, not shown in UI */
1693 aml_append(dev, aml_name_decl("_STA", aml_int(0xB)));
1694
1695 crs = aml_resource_template();
1696 aml_append(crs,
ff80dc7f 1697 aml_io(AML_DECODE16, misc->applesmc_io_base, misc->applesmc_io_base,
8ac6f7a6
IM
1698 0x01, APPLESMC_MAX_DATA_LENGTH)
1699 );
1700 aml_append(crs, aml_irq_no_flags(6));
1701 aml_append(dev, aml_name_decl("_CRS", crs));
1702
1703 aml_append(scope, dev);
41fa5c04 1704 aml_append(dsdt, scope);
8ac6f7a6
IM
1705 }
1706
cd61cb2e
IM
1707 if (misc->pvpanic_port) {
1708 scope = aml_scope("\\_SB.PCI0.ISA");
1709
2332333c 1710 dev = aml_device("PEVT");
e65bef69 1711 aml_append(dev, aml_name_decl("_HID", aml_string("QEMU0001")));
cd61cb2e
IM
1712
1713 crs = aml_resource_template();
1714 aml_append(crs,
ff80dc7f 1715 aml_io(AML_DECODE16, misc->pvpanic_port, misc->pvpanic_port, 1, 1)
cd61cb2e
IM
1716 );
1717 aml_append(dev, aml_name_decl("_CRS", crs));
1718
ff80dc7f 1719 aml_append(dev, aml_operation_region("PEOR", AML_SYSTEM_IO,
3f3009c0 1720 aml_int(misc->pvpanic_port), 1));
36de884a 1721 field = aml_field("PEOR", AML_BYTE_ACC, AML_NOLOCK, AML_PRESERVE);
cd61cb2e
IM
1722 aml_append(field, aml_named_field("PEPT", 8));
1723 aml_append(dev, field);
1724
8ef3ea25
GH
1725 /* device present, functioning, decoding, shown in UI */
1726 aml_append(dev, aml_name_decl("_STA", aml_int(0xF)));
2332333c 1727
4dbfc881 1728 method = aml_method("RDPT", 0, AML_NOTSERIALIZED);
cd61cb2e
IM
1729 aml_append(method, aml_store(aml_name("PEPT"), aml_local(0)));
1730 aml_append(method, aml_return(aml_local(0)));
1731 aml_append(dev, method);
1732
4dbfc881 1733 method = aml_method("WRPT", 1, AML_NOTSERIALIZED);
cd61cb2e
IM
1734 aml_append(method, aml_store(aml_arg(0), aml_name("PEPT")));
1735 aml_append(dev, method);
1736
1737 aml_append(scope, dev);
41fa5c04 1738 aml_append(dsdt, scope);
cd61cb2e
IM
1739 }
1740
7824df38 1741 sb_scope = aml_scope("\\_SB");
72c194f7 1742 {
8b35ab27
IM
1743 Object *pci_host;
1744 PCIBus *bus = NULL;
8698c0c0 1745
8b35ab27
IM
1746 pci_host = acpi_get_i386_pci_host();
1747 if (pci_host) {
1748 bus = PCI_HOST_BRIDGE(pci_host)->bus;
1749 }
8dcf525a 1750
8b35ab27
IM
1751 if (bus) {
1752 Aml *scope = aml_scope("PCI0");
1753 /* Scan all PCI buses. Generate tables to support hotplug. */
1754 build_append_pci_bus_devices(scope, bus, pm->pcihp_bridge_en);
1755
43bc7f84 1756 if (TPM_IS_TIS_ISA(tpm)) {
24cf5413
SB
1757 if (misc->tpm_version == TPM_VERSION_2_0) {
1758 dev = aml_device("TPM");
1759 aml_append(dev, aml_name_decl("_HID",
1760 aml_string("MSFT0101")));
1761 } else {
1762 dev = aml_device("ISA.TPM");
1763 aml_append(dev, aml_name_decl("_HID",
1764 aml_eisaid("PNP0C31")));
1765 }
1766
8b35ab27
IM
1767 aml_append(dev, aml_name_decl("_STA", aml_int(0xF)));
1768 crs = aml_resource_template();
1769 aml_append(crs, aml_memory32_fixed(TPM_TIS_ADDR_BASE,
1770 TPM_TIS_ADDR_SIZE, AML_READ_WRITE));
1771 /*
1772 FIXME: TPM_TIS_IRQ=5 conflicts with PNP0C0F irqs,
1773 Rewrite to take IRQ from TPM device model and
1774 fix default IRQ value there to use some unused IRQ
1775 */
1776 /* aml_append(crs, aml_irq_no_flags(TPM_TIS_IRQ)); */
1777 aml_append(dev, aml_name_decl("_CRS", crs));
ac6dd31e
SB
1778
1779 tpm_build_ppi_acpi(tpm, dev);
1780
8b35ab27 1781 aml_append(scope, dev);
8dcf525a 1782 }
72c194f7 1783
8b35ab27 1784 aml_append(sb_scope, scope);
72c194f7 1785 }
72c194f7 1786 }
4ab6cb4c 1787
ac6dd31e 1788 if (TPM_IS_CRB(tpm)) {
4ab6cb4c
MAL
1789 dev = aml_device("TPM");
1790 aml_append(dev, aml_name_decl("_HID", aml_string("MSFT0101")));
1791 crs = aml_resource_template();
1792 aml_append(crs, aml_memory32_fixed(TPM_CRB_ADDR_BASE,
1793 TPM_CRB_ADDR_SIZE, AML_READ_WRITE));
1794 aml_append(dev, aml_name_decl("_CRS", crs));
1795
88b3648f 1796 aml_append(dev, aml_name_decl("_STA", aml_int(0xf)));
4ab6cb4c 1797
ac6dd31e
SB
1798 tpm_build_ppi_acpi(tpm, dev);
1799
4ab6cb4c
MAL
1800 aml_append(sb_scope, dev);
1801 }
1802
8b35ab27 1803 aml_append(dsdt, sb_scope);
72c194f7 1804
011bb749 1805 /* copy AML table into ACPI tables blob and patch header there */
41fa5c04 1806 g_array_append_vals(table_data, dsdt->buf->data, dsdt->buf->len);
72c194f7 1807 build_header(linker, table_data,
41fa5c04 1808 (void *)(table_data->data + table_data->len - dsdt->buf->len),
d07b2286 1809 "DSDT", dsdt->buf->len, 1, x86ms->oem_id, x86ms->oem_table_id);
011bb749 1810 free_aml_allocator();
72c194f7
MT
1811}
1812
1813static void
602b4582
MP
1814build_hpet(GArray *table_data, BIOSLinker *linker, const char *oem_id,
1815 const char *oem_table_id)
72c194f7
MT
1816{
1817 Acpi20Hpet *hpet;
1818
1819 hpet = acpi_data_push(table_data, sizeof(*hpet));
1820 /* Note timer_block_id value must be kept in sync with value advertised by
1821 * emulated hpet
1822 */
1823 hpet->timer_block_id = cpu_to_le32(0x8086a201);
1824 hpet->addr.address = cpu_to_le64(HPET_BASE);
1825 build_header(linker, table_data,
602b4582 1826 (void *)hpet, "HPET", sizeof(*hpet), 1, oem_id, oem_table_id);
72c194f7
MT
1827}
1828
711b20b4 1829static void
602b4582
MP
1830build_tpm_tcpa(GArray *table_data, BIOSLinker *linker, GArray *tcpalog,
1831 const char *oem_id, const char *oem_table_id)
711b20b4
SB
1832{
1833 Acpi20Tcpa *tcpa = acpi_data_push(table_data, sizeof *tcpa);
4678124b
IM
1834 unsigned log_addr_size = sizeof(tcpa->log_area_start_address);
1835 unsigned log_addr_offset =
1836 (char *)&tcpa->log_area_start_address - table_data->data;
711b20b4
SB
1837
1838 tcpa->platform_class = cpu_to_le16(TPM_TCPA_ACPI_CLASS_CLIENT);
1839 tcpa->log_area_minimum_length = cpu_to_le32(TPM_LOG_AREA_MINIMUM_SIZE);
9774ccf7 1840 acpi_data_push(tcpalog, le32_to_cpu(tcpa->log_area_minimum_length));
711b20b4 1841
ad9671b8 1842 bios_linker_loader_alloc(linker, ACPI_BUILD_TPMLOG_FILE, tcpalog, 1,
42a5b308
SB
1843 false /* high memory */);
1844
711b20b4 1845 /* log area start address to be filled by Guest linker */
4678124b
IM
1846 bios_linker_loader_add_pointer(linker,
1847 ACPI_BUILD_TABLE_FILE, log_addr_offset, log_addr_size,
1848 ACPI_BUILD_TPMLOG_FILE, 0);
711b20b4
SB
1849
1850 build_header(linker, table_data,
602b4582 1851 (void *)tcpa, "TCPA", sizeof(*tcpa), 2, oem_id, oem_table_id);
711b20b4
SB
1852}
1853
d471bf3e
PB
1854#define HOLE_640K_START (640 * KiB)
1855#define HOLE_640K_END (1 * MiB)
4926403c 1856
72c194f7 1857static void
0e9b9eda 1858build_srat(GArray *table_data, BIOSLinker *linker, MachineState *machine)
72c194f7
MT
1859{
1860 AcpiSystemResourceAffinityTable *srat;
72c194f7
MT
1861 AcpiSratMemoryAffinity *numamem;
1862
1863 int i;
72c194f7
MT
1864 int srat_start, numa_start, slots;
1865 uint64_t mem_len, mem_base, next_base;
5803fce3 1866 MachineClass *mc = MACHINE_GET_CLASS(machine);
f0bb276b 1867 X86MachineState *x86ms = X86_MACHINE(machine);
80e5db30 1868 const CPUArchIdList *apic_ids = mc->possible_cpu_arch_ids(machine);
3d3ebcad 1869 PCMachineState *pcms = PC_MACHINE(machine);
cec65193 1870 ram_addr_t hotplugabble_address_space_size =
f2ffbe2b 1871 object_property_get_int(OBJECT(pcms), PC_MACHINE_DEVMEM_REGION_SIZE,
cec65193 1872 NULL);
72c194f7
MT
1873
1874 srat_start = table_data->len;
1875
1876 srat = acpi_data_push(table_data, sizeof *srat);
1877 srat->reserved1 = cpu_to_le32(1);
72c194f7 1878
5803fce3 1879 for (i = 0; i < apic_ids->len; i++) {
d41f3e75 1880 int node_id = apic_ids->cpus[i].props.node_id;
5eff33a2 1881 uint32_t apic_id = apic_ids->cpus[i].arch_id;
5803fce3 1882
5eff33a2
IM
1883 if (apic_id < 255) {
1884 AcpiSratProcessorAffinity *core;
1885
1886 core = acpi_data_push(table_data, sizeof *core);
1887 core->type = ACPI_SRAT_PROCESSOR_APIC;
1888 core->length = sizeof(*core);
1889 core->local_apic_id = apic_id;
ea265072 1890 core->proximity_lo = node_id;
5eff33a2
IM
1891 memset(core->proximity_hi, 0, 3);
1892 core->local_sapic_eid = 0;
1893 core->flags = cpu_to_le32(1);
1894 } else {
1895 AcpiSratProcessorX2ApicAffinity *core;
1896
1897 core = acpi_data_push(table_data, sizeof *core);
1898 core->type = ACPI_SRAT_PROCESSOR_x2APIC;
1899 core->length = sizeof(*core);
1900 core->x2apic_id = cpu_to_le32(apic_id);
ea265072 1901 core->proximity_domain = cpu_to_le32(node_id);
5eff33a2 1902 core->flags = cpu_to_le32(1);
1f3aba37 1903 }
72c194f7
MT
1904 }
1905
1906
1907 /* the memory map is a bit tricky, it contains at least one hole
1908 * from 640k-1M and possibly another one from 3.5G-4G.
1909 */
1910 next_base = 0;
1911 numa_start = table_data->len;
1912
dd4c2f01 1913 for (i = 1; i < pcms->numa_nodes + 1; ++i) {
72c194f7 1914 mem_base = next_base;
dd4c2f01 1915 mem_len = pcms->node_mem[i - 1];
72c194f7
MT
1916 next_base = mem_base + mem_len;
1917
4926403c
EH
1918 /* Cut out the 640K hole */
1919 if (mem_base <= HOLE_640K_START &&
1920 next_base > HOLE_640K_START) {
1921 mem_len -= next_base - HOLE_640K_START;
1922 if (mem_len > 0) {
1923 numamem = acpi_data_push(table_data, sizeof *numamem);
1924 build_srat_memory(numamem, mem_base, mem_len, i - 1,
1925 MEM_AFFINITY_ENABLED);
1926 }
1927
1928 /* Check for the rare case: 640K < RAM < 1M */
1929 if (next_base <= HOLE_640K_END) {
1930 next_base = HOLE_640K_END;
1931 continue;
1932 }
1933 mem_base = HOLE_640K_END;
1934 mem_len = next_base - HOLE_640K_END;
1935 }
1936
72c194f7 1937 /* Cut out the ACPI_PCI hole */
f0bb276b
PB
1938 if (mem_base <= x86ms->below_4g_mem_size &&
1939 next_base > x86ms->below_4g_mem_size) {
1940 mem_len -= next_base - x86ms->below_4g_mem_size;
72c194f7
MT
1941 if (mem_len > 0) {
1942 numamem = acpi_data_push(table_data, sizeof *numamem);
64b83136
SZ
1943 build_srat_memory(numamem, mem_base, mem_len, i - 1,
1944 MEM_AFFINITY_ENABLED);
72c194f7
MT
1945 }
1946 mem_base = 1ULL << 32;
f0bb276b 1947 mem_len = next_base - x86ms->below_4g_mem_size;
6cf6fe39 1948 next_base = mem_base + mem_len;
72c194f7 1949 }
16b42263
DL
1950
1951 if (mem_len > 0) {
1952 numamem = acpi_data_push(table_data, sizeof *numamem);
1953 build_srat_memory(numamem, mem_base, mem_len, i - 1,
1954 MEM_AFFINITY_ENABLED);
1955 }
72c194f7 1956 }
c3b0cf6e
VV
1957
1958 if (machine->nvdimms_state->is_enabled) {
1959 nvdimm_build_srat(table_data);
1960 }
1961
72c194f7 1962 slots = (table_data->len - numa_start) / sizeof *numamem;
dd4c2f01 1963 for (; slots < pcms->numa_nodes + 2; slots++) {
72c194f7 1964 numamem = acpi_data_push(table_data, sizeof *numamem);
64b83136 1965 build_srat_memory(numamem, 0, 0, 0, MEM_AFFINITY_NOFLAGS);
72c194f7
MT
1966 }
1967
dbb6da8b
IM
1968 /*
1969 * Entry is required for Windows to enable memory hotplug in OS
1970 * and for Linux to enable SWIOTLB when booted with less than
1971 * 4G of RAM. Windows works better if the entry sets proximity
1972 * to the highest NUMA node in the machine.
1973 * Memory devices may override proximity set by this entry,
1974 * providing _PXM method if necessary.
1975 */
cec65193 1976 if (hotplugabble_address_space_size) {
dbb6da8b
IM
1977 numamem = acpi_data_push(table_data, sizeof *numamem);
1978 build_srat_memory(numamem, machine->device_memory->base,
1979 hotplugabble_address_space_size, pcms->numa_nodes - 1,
1980 MEM_AFFINITY_HOTPLUGGABLE | MEM_AFFINITY_ENABLED);
cec65193
IM
1981 }
1982
72c194f7
MT
1983 build_header(linker, table_data,
1984 (void *)(table_data->data + srat_start),
821e3227 1985 "SRAT",
d07b2286
MP
1986 table_data->len - srat_start, 1, x86ms->oem_id,
1987 x86ms->oem_table_id);
72c194f7
MT
1988}
1989
d46114f9
PX
1990/*
1991 * VT-d spec 8.1 DMA Remapping Reporting Structure
1992 * (version Oct. 2014 or later)
1993 */
d4eb9119 1994static void
602b4582
MP
1995build_dmar_q35(GArray *table_data, BIOSLinker *linker, const char *oem_id,
1996 const char *oem_table_id)
d4eb9119
LT
1997{
1998 int dmar_start = table_data->len;
1999
2000 AcpiTableDmar *dmar;
2001 AcpiDmarHardwareUnit *drhd;
bd2baacc 2002 AcpiDmarRootPortATS *atsr;
d46114f9
PX
2003 uint8_t dmar_flags = 0;
2004 X86IOMMUState *iommu = x86_iommu_get_default();
cfc13df4
PX
2005 AcpiDmarDeviceScope *scope = NULL;
2006 /* Root complex IOAPIC use one path[0] only */
2007 size_t ioapic_scope_size = sizeof(*scope) + sizeof(scope->path[0]);
37f51384 2008 IntelIOMMUState *intel_iommu = INTEL_IOMMU_DEVICE(iommu);
d46114f9
PX
2009
2010 assert(iommu);
a924b3d8 2011 if (x86_iommu_ir_supported(iommu)) {
d46114f9
PX
2012 dmar_flags |= 0x1; /* Flags: 0x1: INT_REMAP */
2013 }
d4eb9119
LT
2014
2015 dmar = acpi_data_push(table_data, sizeof(*dmar));
37f51384 2016 dmar->host_address_width = intel_iommu->aw_bits - 1;
d46114f9 2017 dmar->flags = dmar_flags;
d4eb9119
LT
2018
2019 /* DMAR Remapping Hardware Unit Definition structure */
cfc13df4 2020 drhd = acpi_data_push(table_data, sizeof(*drhd) + ioapic_scope_size);
d4eb9119 2021 drhd->type = cpu_to_le16(ACPI_DMAR_TYPE_HARDWARE_UNIT);
cfc13df4 2022 drhd->length = cpu_to_le16(sizeof(*drhd) + ioapic_scope_size);
d4eb9119
LT
2023 drhd->flags = ACPI_DMAR_INCLUDE_PCI_ALL;
2024 drhd->pci_segment = cpu_to_le16(0);
2025 drhd->address = cpu_to_le64(Q35_HOST_BRIDGE_IOMMU_ADDR);
2026
cfc13df4
PX
2027 /* Scope definition for the root-complex IOAPIC. See VT-d spec
2028 * 8.3.1 (version Oct. 2014 or later). */
2029 scope = &drhd->scope[0];
2030 scope->entry_type = 0x03; /* Type: 0x03 for IOAPIC */
2031 scope->length = ioapic_scope_size;
2032 scope->enumeration_id = ACPI_BUILD_IOAPIC_ID;
2033 scope->bus = Q35_PSEUDO_BUS_PLATFORM;
1b39bc1c
PX
2034 scope->path[0].device = PCI_SLOT(Q35_PSEUDO_DEVFN_IOAPIC);
2035 scope->path[0].function = PCI_FUNC(Q35_PSEUDO_DEVFN_IOAPIC);
cfc13df4 2036
bd2baacc
JW
2037 if (iommu->dt_supported) {
2038 atsr = acpi_data_push(table_data, sizeof(*atsr));
2039 atsr->type = cpu_to_le16(ACPI_DMAR_TYPE_ATSR);
2040 atsr->length = cpu_to_le16(sizeof(*atsr));
2041 atsr->flags = ACPI_DMAR_ATSR_ALL_PORTS;
2042 atsr->pci_segment = cpu_to_le16(0);
2043 }
2044
d4eb9119 2045 build_header(linker, table_data, (void *)(table_data->data + dmar_start),
602b4582 2046 "DMAR", table_data->len - dmar_start, 1, oem_id, oem_table_id);
d4eb9119 2047}
14cda350
LA
2048
2049/*
2050 * Windows ACPI Emulated Devices Table
2051 * (Version 1.0 - April 6, 2009)
2052 * Spec: http://download.microsoft.com/download/7/E/7/7E7662CF-CBEA-470B-A97E-CE7CE0D98DC2/WAET.docx
2053 *
2054 * Helpful to speedup Windows guests and ignored by others.
2055 */
2056static void
602b4582
MP
2057build_waet(GArray *table_data, BIOSLinker *linker, const char *oem_id,
2058 const char *oem_table_id)
14cda350
LA
2059{
2060 int waet_start = table_data->len;
2061
2062 /* WAET header */
2063 acpi_data_push(table_data, sizeof(AcpiTableHeader));
2064 /*
2065 * Set "ACPI PM timer good" flag.
2066 *
2067 * Tells Windows guests that our ACPI PM timer is reliable in the
2068 * sense that guest can read it only once to obtain a reliable value.
2069 * Which avoids costly VMExits caused by guest re-reading it unnecessarily.
2070 */
2071 build_append_int_noprefix(table_data, 1 << 1 /* ACPI PM timer good */, 4);
2072
2073 build_header(linker, table_data, (void *)(table_data->data + waet_start),
602b4582 2074 "WAET", table_data->len - waet_start, 1, oem_id, oem_table_id);
14cda350
LA
2075}
2076
fb9f5926
DK
2077/*
2078 * IVRS table as specified in AMD IOMMU Specification v2.62, Section 5.2
2079 * accessible here http://support.amd.com/TechDocs/48882_IOMMU.pdf
2080 */
c028818d
BS
2081#define IOAPIC_SB_DEVID (uint64_t)PCI_BUILD_BDF(0, PCI_DEVFN(0x14, 0))
2082
977aff10
AW
2083/*
2084 * Insert IVHD entry for device and recurse, insert alias, or insert range as
2085 * necessary for the PCI topology.
2086 */
2087static void
2088insert_ivhd(PCIBus *bus, PCIDevice *dev, void *opaque)
2089{
2090 GArray *table_data = opaque;
2091 uint32_t entry;
2092
2093 /* "Select" IVHD entry, type 0x2 */
2094 entry = PCI_BUILD_BDF(pci_bus_num(bus), dev->devfn) << 8 | 0x2;
2095 build_append_int_noprefix(table_data, entry, 4);
2096
2097 if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_BRIDGE)) {
2098 PCIBus *sec_bus = pci_bridge_get_sec_bus(PCI_BRIDGE(dev));
2099 uint8_t sec = pci_bus_num(sec_bus);
2100 uint8_t sub = dev->config[PCI_SUBORDINATE_BUS];
2101
2102 if (pci_bus_is_express(sec_bus)) {
2103 /*
2104 * Walk the bus if there are subordinates, otherwise use a range
2105 * to cover an entire leaf bus. We could potentially also use a
2106 * range for traversed buses, but we'd need to take care not to
2107 * create both Select and Range entries covering the same device.
2108 * This is easier and potentially more compact.
2109 *
2110 * An example bare metal system seems to use Select entries for
2111 * root ports without a slot (ie. built-ins) and Range entries
2112 * when there is a slot. The same system also only hard-codes
2113 * the alias range for an onboard PCIe-to-PCI bridge, apparently
2114 * making no effort to support nested bridges. We attempt to
2115 * be more thorough here.
2116 */
2117 if (sec == sub) { /* leaf bus */
2118 /* "Start of Range" IVHD entry, type 0x3 */
2119 entry = PCI_BUILD_BDF(sec, PCI_DEVFN(0, 0)) << 8 | 0x3;
2120 build_append_int_noprefix(table_data, entry, 4);
2121 /* "End of Range" IVHD entry, type 0x4 */
2122 entry = PCI_BUILD_BDF(sub, PCI_DEVFN(31, 7)) << 8 | 0x4;
2123 build_append_int_noprefix(table_data, entry, 4);
2124 } else {
2125 pci_for_each_device(sec_bus, sec, insert_ivhd, table_data);
2126 }
2127 } else {
2128 /*
2129 * If the secondary bus is conventional, then we need to create an
2130 * Alias range for everything downstream. The range covers the
2131 * first devfn on the secondary bus to the last devfn on the
2132 * subordinate bus. The alias target depends on legacy versus
2133 * express bridges, just as in pci_device_iommu_address_space().
2134 * DeviceIDa vs DeviceIDb as per the AMD IOMMU spec.
2135 */
2136 uint16_t dev_id_a, dev_id_b;
2137
2138 dev_id_a = PCI_BUILD_BDF(sec, PCI_DEVFN(0, 0));
2139
2140 if (pci_is_express(dev) &&
2141 pcie_cap_get_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE) {
2142 dev_id_b = dev_id_a;
2143 } else {
2144 dev_id_b = PCI_BUILD_BDF(pci_bus_num(bus), dev->devfn);
2145 }
2146
2147 /* "Alias Start of Range" IVHD entry, type 0x43, 8 bytes */
2148 build_append_int_noprefix(table_data, dev_id_a << 8 | 0x43, 4);
2149 build_append_int_noprefix(table_data, dev_id_b << 8 | 0x0, 4);
2150
2151 /* "End of Range" IVHD entry, type 0x4 */
2152 entry = PCI_BUILD_BDF(sub, PCI_DEVFN(31, 7)) << 8 | 0x4;
2153 build_append_int_noprefix(table_data, entry, 4);
2154 }
2155 }
2156}
2157
2158/* For all PCI host bridges, walk and insert IVHD entries */
2159static int
2160ivrs_host_bridges(Object *obj, void *opaque)
2161{
2162 GArray *ivhd_blob = opaque;
2163
2164 if (object_dynamic_cast(obj, TYPE_PCI_HOST_BRIDGE)) {
2165 PCIBus *bus = PCI_HOST_BRIDGE(obj)->bus;
2166
2167 if (bus) {
2168 pci_for_each_device(bus, pci_bus_num(bus), insert_ivhd, ivhd_blob);
2169 }
2170 }
2171
2172 return 0;
2173}
2174
fb9f5926 2175static void
602b4582
MP
2176build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,
2177 const char *oem_table_id)
fb9f5926 2178{
977aff10 2179 int ivhd_table_len = 24;
fb9f5926
DK
2180 int iommu_start = table_data->len;
2181 AMDVIState *s = AMD_IOMMU_DEVICE(x86_iommu_get_default());
977aff10 2182 GArray *ivhd_blob = g_array_new(false, true, 1);
fb9f5926
DK
2183
2184 /* IVRS header */
2185 acpi_data_push(table_data, sizeof(AcpiTableHeader));
2186 /* IVinfo - IO virtualization information common to all
2187 * IOMMU units in a system
2188 */
2189 build_append_int_noprefix(table_data, 40UL << 8/* PASize */, 4);
2190 /* reserved */
2191 build_append_int_noprefix(table_data, 0, 8);
2192
2193 /* IVHD definition - type 10h */
2194 build_append_int_noprefix(table_data, 0x10, 1);
2195 /* virtualization flags */
2196 build_append_int_noprefix(table_data,
2197 (1UL << 0) | /* HtTunEn */
2198 (1UL << 4) | /* iotblSup */
2199 (1UL << 6) | /* PrefSup */
2200 (1UL << 7), /* PPRSup */
2201 1);
c028818d 2202
977aff10
AW
2203 /*
2204 * A PCI bus walk, for each PCI host bridge, is necessary to create a
2205 * complete set of IVHD entries. Do this into a separate blob so that we
2206 * can calculate the total IVRS table length here and then append the new
2207 * blob further below. Fall back to an entry covering all devices, which
2208 * is sufficient when no aliases are present.
2209 */
2210 object_child_foreach_recursive(object_get_root(),
2211 ivrs_host_bridges, ivhd_blob);
2212
2213 if (!ivhd_blob->len) {
2214 /*
2215 * Type 1 device entry reporting all devices
2216 * These are 4-byte device entries currently reporting the range of
2217 * Refer to Spec - Table 95:IVHD Device Entry Type Codes(4-byte)
2218 */
2219 build_append_int_noprefix(ivhd_blob, 0x0000001, 4);
2220 }
2221
2222 ivhd_table_len += ivhd_blob->len;
2223
c028818d
BS
2224 /*
2225 * When interrupt remapping is supported, we add a special IVHD device
2226 * for type IO-APIC.
2227 */
a924b3d8 2228 if (x86_iommu_ir_supported(x86_iommu_get_default())) {
c028818d
BS
2229 ivhd_table_len += 8;
2230 }
977aff10 2231
fb9f5926 2232 /* IVHD length */
c028818d 2233 build_append_int_noprefix(table_data, ivhd_table_len, 2);
fb9f5926
DK
2234 /* DeviceID */
2235 build_append_int_noprefix(table_data, s->devid, 2);
2236 /* Capability offset */
2237 build_append_int_noprefix(table_data, s->capab_offset, 2);
2238 /* IOMMU base address */
2239 build_append_int_noprefix(table_data, s->mmio.addr, 8);
2240 /* PCI Segment Group */
2241 build_append_int_noprefix(table_data, 0, 2);
2242 /* IOMMU info */
2243 build_append_int_noprefix(table_data, 0, 2);
2244 /* IOMMU Feature Reporting */
2245 build_append_int_noprefix(table_data,
2246 (48UL << 30) | /* HATS */
2247 (48UL << 28) | /* GATS */
12499b23
BS
2248 (1UL << 2) | /* GTSup */
2249 (1UL << 6), /* GASup */
fb9f5926 2250 4);
977aff10
AW
2251
2252 /* IVHD entries as found above */
2253 g_array_append_vals(table_data, ivhd_blob->data, ivhd_blob->len);
2254 g_array_free(ivhd_blob, TRUE);
fb9f5926 2255
c028818d
BS
2256 /*
2257 * Add a special IVHD device type.
2258 * Refer to spec - Table 95: IVHD device entry type codes
2259 *
2260 * Linux IOMMU driver checks for the special IVHD device (type IO-APIC).
2261 * See Linux kernel commit 'c2ff5cf5294bcbd7fa50f7d860e90a66db7e5059'
2262 */
a924b3d8 2263 if (x86_iommu_ir_supported(x86_iommu_get_default())) {
c028818d
BS
2264 build_append_int_noprefix(table_data,
2265 (0x1ull << 56) | /* type IOAPIC */
2266 (IOAPIC_SB_DEVID << 40) | /* IOAPIC devid */
2267 0x48, /* special device */
2268 8);
2269 }
2270
fb9f5926 2271 build_header(linker, table_data, (void *)(table_data->data + iommu_start),
602b4582
MP
2272 "IVRS", table_data->len - iommu_start, 1, oem_id,
2273 oem_table_id);
fb9f5926 2274}
d4eb9119 2275
72c194f7
MT
2276typedef
2277struct AcpiBuildState {
2278 /* Copy of table in RAM (for patching). */
339240b5 2279 MemoryRegion *table_mr;
72c194f7
MT
2280 /* Is table patched? */
2281 uint8_t patched;
d70414a5 2282 void *rsdp;
339240b5
PB
2283 MemoryRegion *rsdp_mr;
2284 MemoryRegion *linker_mr;
72c194f7
MT
2285} AcpiBuildState;
2286
2287static bool acpi_get_mcfg(AcpiMcfgInfo *mcfg)
2288{
2289 Object *pci_host;
2290 QObject *o;
72c194f7 2291
ca6c1855 2292 pci_host = acpi_get_i386_pci_host();
72c194f7
MT
2293 g_assert(pci_host);
2294
2295 o = object_property_get_qobject(pci_host, PCIE_HOST_MCFG_BASE, NULL);
2296 if (!o) {
2297 return false;
2298 }
c309434e 2299 mcfg->base = qnum_get_uint(qobject_to(QNum, o));
cb3e7f08 2300 qobject_unref(o);
c309434e 2301 if (mcfg->base == PCIE_BASE_ADDR_UNMAPPED) {
fe4970ad
IM
2302 return false;
2303 }
72c194f7
MT
2304
2305 o = object_property_get_qobject(pci_host, PCIE_HOST_MCFG_SIZE, NULL);
2306 assert(o);
c309434e 2307 mcfg->size = qnum_get_uint(qobject_to(QNum, o));
cb3e7f08 2308 qobject_unref(o);
72c194f7
MT
2309 return true;
2310}
2311
2312static
3d3ebcad 2313void acpi_build(AcpiBuildTables *tables, MachineState *machine)
72c194f7 2314{
3d3ebcad 2315 PCMachineState *pcms = PC_MACHINE(machine);
bb292f5a 2316 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
f0bb276b 2317 X86MachineState *x86ms = X86_MACHINE(machine);
72c194f7 2318 GArray *table_offsets;
41fa5c04 2319 unsigned facs, dsdt, rsdt, fadt;
72c194f7
MT
2320 AcpiPmInfo pm;
2321 AcpiMiscInfo misc;
2322 AcpiMcfgInfo mcfg;
01c9742d 2323 Range pci_hole, pci_hole64;
72c194f7 2324 uint8_t *u;
07fb6176 2325 size_t aml_len = 0;
7c2c1fa5 2326 GArray *tables_blob = tables->table_data;
ae123749 2327 AcpiSlicOem slic_oem = { .id = NULL, .table_id = NULL };
d03637bc 2328 Object *vmgenid_dev;
602b4582
MP
2329 char *oem_id;
2330 char *oem_table_id;
72c194f7 2331
0e11fc69 2332 acpi_get_pm_info(machine, &pm);
72c194f7 2333 acpi_get_misc_info(&misc);
01c9742d 2334 acpi_get_pci_holes(&pci_hole, &pci_hole64);
ae123749 2335 acpi_get_slic_oem(&slic_oem);
72c194f7 2336
602b4582
MP
2337 if (slic_oem.id) {
2338 oem_id = slic_oem.id;
2339 } else {
d07b2286 2340 oem_id = x86ms->oem_id;
602b4582
MP
2341 }
2342
2343 if (slic_oem.table_id) {
2344 oem_table_id = slic_oem.table_id;
2345 } else {
d07b2286 2346 oem_table_id = x86ms->oem_table_id;
602b4582
MP
2347 }
2348
72c194f7
MT
2349 table_offsets = g_array_new(false, true /* clear */,
2350 sizeof(uint32_t));
8b310fc4 2351 ACPI_BUILD_DPRINTF("init ACPI tables\n");
72c194f7 2352
ad9671b8
IM
2353 bios_linker_loader_alloc(tables->linker,
2354 ACPI_BUILD_TABLE_FILE, tables_blob,
72c194f7
MT
2355 64 /* Ensure FACS is aligned */,
2356 false /* high memory */);
2357
2358 /*
2359 * FACS is pointed to by FADT.
2360 * We place it first since it's the only table that has alignment
2361 * requirements.
2362 */
7c2c1fa5 2363 facs = tables_blob->len;
009180bd 2364 build_facs(tables_blob);
72c194f7
MT
2365
2366 /* DSDT is pointed to by FADT */
7c2c1fa5 2367 dsdt = tables_blob->len;
01c9742d
MA
2368 build_dsdt(tables_blob, tables->linker, &pm, &misc,
2369 &pci_hole, &pci_hole64, machine);
72c194f7 2370
07fb6176
PB
2371 /* Count the size of the DSDT and SSDT, we will need it for legacy
2372 * sizing of ACPI tables.
2373 */
7c2c1fa5 2374 aml_len += tables_blob->len - dsdt;
07fb6176 2375
72c194f7 2376 /* ACPI tables pointed to by RSDT */
41fa5c04 2377 fadt = tables_blob->len;
7c2c1fa5 2378 acpi_add_table(table_offsets, tables_blob);
937d1b58
IM
2379 pm.fadt.facs_tbl_offset = &facs;
2380 pm.fadt.dsdt_tbl_offset = &dsdt;
2381 pm.fadt.xdsdt_tbl_offset = &dsdt;
602b4582 2382 build_fadt(tables_blob, tables->linker, &pm.fadt, oem_id, oem_table_id);
41fa5c04 2383 aml_len += tables_blob->len - fadt;
72c194f7 2384
7c2c1fa5 2385 acpi_add_table(table_offsets, tables_blob);
eb66ffab 2386 acpi_build_madt(tables_blob, tables->linker, x86ms,
d07b2286
MP
2387 ACPI_DEVICE_IF(x86ms->acpi_dev), x86ms->oem_id,
2388 x86ms->oem_table_id);
9ac1c4c0 2389
d03637bc
BW
2390 vmgenid_dev = find_vmgenid_dev();
2391 if (vmgenid_dev) {
2392 acpi_add_table(table_offsets, tables_blob);
2393 vmgenid_build_acpi(VMGENID(vmgenid_dev), tables_blob,
d07b2286 2394 tables->vmgenid, tables->linker, x86ms->oem_id);
d03637bc
BW
2395 }
2396
72c194f7 2397 if (misc.has_hpet) {
7c2c1fa5 2398 acpi_add_table(table_offsets, tables_blob);
d07b2286
MP
2399 build_hpet(tables_blob, tables->linker, x86ms->oem_id,
2400 x86ms->oem_table_id);
711b20b4 2401 }
5cb18b3d 2402 if (misc.tpm_version != TPM_VERSION_UNSPEC) {
7e7c1b84
SB
2403 if (misc.tpm_version == TPM_VERSION_1_2) {
2404 acpi_add_table(table_offsets, tables_blob);
602b4582 2405 build_tpm_tcpa(tables_blob, tables->linker, tables->tcpalog,
d07b2286 2406 x86ms->oem_id, x86ms->oem_table_id);
7e7c1b84 2407 } else { /* TPM_VERSION_2_0 */
72d97b3a 2408 acpi_add_table(table_offsets, tables_blob);
602b4582 2409 build_tpm2(tables_blob, tables->linker, tables->tcpalog,
d07b2286 2410 x86ms->oem_id, x86ms->oem_table_id);
5cb18b3d 2411 }
72c194f7 2412 }
dd4c2f01 2413 if (pcms->numa_nodes) {
7c2c1fa5 2414 acpi_add_table(table_offsets, tables_blob);
3d3ebcad 2415 build_srat(tables_blob, tables->linker, machine);
118154b7 2416 if (machine->numa_state->have_numa_distance) {
0f203430 2417 acpi_add_table(table_offsets, tables_blob);
d07b2286
MP
2418 build_slit(tables_blob, tables->linker, machine, x86ms->oem_id,
2419 x86ms->oem_table_id);
0f203430 2420 }
e6f123c3
LJ
2421 if (machine->numa_state->hmat_enabled) {
2422 acpi_add_table(table_offsets, tables_blob);
602b4582 2423 build_hmat(tables_blob, tables->linker, machine->numa_state,
d07b2286 2424 x86ms->oem_id, x86ms->oem_table_id);
e6f123c3 2425 }
72c194f7
MT
2426 }
2427 if (acpi_get_mcfg(&mcfg)) {
7c2c1fa5 2428 acpi_add_table(table_offsets, tables_blob);
d07b2286
MP
2429 build_mcfg(tables_blob, tables->linker, &mcfg, x86ms->oem_id,
2430 x86ms->oem_table_id);
72c194f7 2431 }
fb9f5926
DK
2432 if (x86_iommu_get_default()) {
2433 IommuType IOMMUType = x86_iommu_get_type();
2434 if (IOMMUType == TYPE_AMD) {
2435 acpi_add_table(table_offsets, tables_blob);
d07b2286
MP
2436 build_amd_iommu(tables_blob, tables->linker, x86ms->oem_id,
2437 x86ms->oem_table_id);
fb9f5926
DK
2438 } else if (IOMMUType == TYPE_INTEL) {
2439 acpi_add_table(table_offsets, tables_blob);
d07b2286
MP
2440 build_dmar_q35(tables_blob, tables->linker, x86ms->oem_id,
2441 x86ms->oem_table_id);
fb9f5926 2442 }
d4eb9119 2443 }
f6a0d06b 2444 if (machine->nvdimms_state->is_enabled) {
ad9671b8 2445 nvdimm_build_acpi(table_offsets, tables_blob, tables->linker,
602b4582 2446 machine->nvdimms_state, machine->ram_slots,
d07b2286 2447 x86ms->oem_id, x86ms->oem_table_id);
87252e1b
XG
2448 }
2449
14cda350 2450 acpi_add_table(table_offsets, tables_blob);
d07b2286 2451 build_waet(tables_blob, tables->linker, x86ms->oem_id, x86ms->oem_table_id);
14cda350 2452
72c194f7
MT
2453 /* Add tables supplied by user (if any) */
2454 for (u = acpi_table_first(); u; u = acpi_table_next(u)) {
2455 unsigned len = acpi_table_len(u);
2456
7c2c1fa5
IM
2457 acpi_add_table(table_offsets, tables_blob);
2458 g_array_append_vals(tables_blob, u, len);
72c194f7
MT
2459 }
2460
2461 /* RSDT is pointed to by RSDP */
7c2c1fa5 2462 rsdt = tables_blob->len;
ae123749 2463 build_rsdt(tables_blob, tables->linker, table_offsets,
602b4582 2464 oem_id, oem_table_id);
72c194f7
MT
2465
2466 /* RSDP is in FSEG memory, so allocate it separately */
a46ce1c2
SO
2467 {
2468 AcpiRsdpData rsdp_data = {
2469 .revision = 0,
d07b2286 2470 .oem_id = x86ms->oem_id,
a46ce1c2
SO
2471 .xsdt_tbl_offset = NULL,
2472 .rsdt_tbl_offset = &rsdt,
2473 };
2474 build_rsdp(tables->rsdp, tables->linker, &rsdp_data);
2475 if (!pcmc->rsdp_in_ram) {
2476 /* We used to allocate some extra space for RSDP revision 2 but
2477 * only used the RSDP revision 0 space. The extra bytes were
2478 * zeroed out and not used.
2479 * Here we continue wasting those extra 16 bytes to make sure we
2480 * don't break migration for machine types 2.2 and older due to
2481 * RSDP blob size mismatch.
2482 */
2483 build_append_int_noprefix(tables->rsdp, 0, 16);
2484 }
2485 }
72c194f7 2486
07fb6176 2487 /* We'll expose it all to Guest so we want to reduce
72c194f7 2488 * chance of size changes.
07fb6176
PB
2489 *
2490 * We used to align the tables to 4k, but of course this would
2491 * too simple to be enough. 4k turned out to be too small an
2492 * alignment very soon, and in fact it is almost impossible to
2493 * keep the table size stable for all (max_cpus, max_memory_slots)
2494 * combinations. So the table size is always 64k for pc-i440fx-2.1
2495 * and we give an error if the table grows beyond that limit.
2496 *
2497 * We still have the problem of migrating from "-M pc-i440fx-2.0". For
2498 * that, we exploit the fact that QEMU 2.1 generates _smaller_ tables
2499 * than 2.0 and we can always pad the smaller tables with zeros. We can
2500 * then use the exact size of the 2.0 tables.
2501 *
2502 * All this is for PIIX4, since QEMU 2.0 didn't support Q35 migration.
72c194f7 2503 */
bb292f5a 2504 if (pcmc->legacy_acpi_table_size) {
07fb6176
PB
2505 /* Subtracting aml_len gives the size of fixed tables. Then add the
2506 * size of the PIIX4 DSDT/SSDT in QEMU 2.0.
2507 */
2508 int legacy_aml_len =
bb292f5a 2509 pcmc->legacy_acpi_table_size +
f0bb276b 2510 ACPI_BUILD_LEGACY_CPU_AML_SIZE * x86ms->apic_id_limit;
07fb6176 2511 int legacy_table_size =
7c2c1fa5 2512 ROUND_UP(tables_blob->len - aml_len + legacy_aml_len,
07fb6176 2513 ACPI_BUILD_ALIGN_SIZE);
7c2c1fa5 2514 if (tables_blob->len > legacy_table_size) {
07fb6176 2515 /* Should happen only with PCI bridges and -M pc-i440fx-2.0. */
9e5d2c52
AF
2516 warn_report("ACPI table size %u exceeds %d bytes,"
2517 " migration may not work",
2518 tables_blob->len, legacy_table_size);
2519 error_printf("Try removing CPUs, NUMA nodes, memory slots"
2520 " or PCI bridges.");
07fb6176 2521 }
7c2c1fa5 2522 g_array_set_size(tables_blob, legacy_table_size);
07fb6176 2523 } else {
868270f2 2524 /* Make sure we have a buffer in case we need to resize the tables. */
7c2c1fa5 2525 if (tables_blob->len > ACPI_BUILD_TABLE_SIZE / 2) {
18045fb9 2526 /* As of QEMU 2.1, this fires with 160 VCPUs and 255 memory slots. */
9e5d2c52
AF
2527 warn_report("ACPI table size %u exceeds %d bytes,"
2528 " migration may not work",
2529 tables_blob->len, ACPI_BUILD_TABLE_SIZE / 2);
2530 error_printf("Try removing CPUs, NUMA nodes, memory slots"
2531 " or PCI bridges.");
18045fb9 2532 }
7c2c1fa5 2533 acpi_align_size(tables_blob, ACPI_BUILD_TABLE_SIZE);
07fb6176 2534 }
72c194f7 2535
0e9b9eda 2536 acpi_align_size(tables->linker->cmd_blob, ACPI_BUILD_ALIGN_SIZE);
72c194f7
MT
2537
2538 /* Cleanup memory that's no longer used. */
2539 g_array_free(table_offsets, true);
2540}
2541
339240b5 2542static void acpi_ram_update(MemoryRegion *mr, GArray *data)
42d85900
MT
2543{
2544 uint32_t size = acpi_data_len(data);
2545
2546 /* Make sure RAM size is correct - in case it got changed e.g. by migration */
339240b5 2547 memory_region_ram_resize(mr, size, &error_abort);
42d85900 2548
339240b5
PB
2549 memcpy(memory_region_get_ram_ptr(mr), data->data, size);
2550 memory_region_set_dirty(mr, 0, size);
42d85900
MT
2551}
2552
3f8752b4 2553static void acpi_build_update(void *build_opaque)
72c194f7
MT
2554{
2555 AcpiBuildState *build_state = build_opaque;
2556 AcpiBuildTables tables;
2557
2558 /* No state to update or already patched? Nothing to do. */
2559 if (!build_state || build_state->patched) {
2560 return;
2561 }
2562 build_state->patched = 1;
2563
2564 acpi_build_tables_init(&tables);
2565
3d3ebcad 2566 acpi_build(&tables, MACHINE(qdev_get_machine()));
72c194f7 2567
339240b5 2568 acpi_ram_update(build_state->table_mr, tables.table_data);
a1666142 2569
42d85900
MT
2570 if (build_state->rsdp) {
2571 memcpy(build_state->rsdp, tables.rsdp->data, acpi_data_len(tables.rsdp));
2572 } else {
339240b5 2573 acpi_ram_update(build_state->rsdp_mr, tables.rsdp);
42d85900 2574 }
ad5b88b1 2575
0e9b9eda 2576 acpi_ram_update(build_state->linker_mr, tables.linker->cmd_blob);
72c194f7
MT
2577 acpi_build_tables_cleanup(&tables, true);
2578}
2579
2580static void acpi_build_reset(void *build_opaque)
2581{
2582 AcpiBuildState *build_state = build_opaque;
2583 build_state->patched = 0;
2584}
2585
72c194f7
MT
2586static const VMStateDescription vmstate_acpi_build = {
2587 .name = "acpi_build",
2588 .version_id = 1,
2589 .minimum_version_id = 1,
d49805ae 2590 .fields = (VMStateField[]) {
72c194f7
MT
2591 VMSTATE_UINT8(patched, AcpiBuildState),
2592 VMSTATE_END_OF_LIST()
2593 },
2594};
2595
fb306ffe 2596void acpi_setup(void)
72c194f7 2597{
fb306ffe 2598 PCMachineState *pcms = PC_MACHINE(qdev_get_machine());
bb292f5a 2599 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
f0bb276b 2600 X86MachineState *x86ms = X86_MACHINE(pcms);
72c194f7
MT
2601 AcpiBuildTables tables;
2602 AcpiBuildState *build_state;
d03637bc 2603 Object *vmgenid_dev;
0fe24669
SB
2604 TPMIf *tpm;
2605 static FwCfgTPMConfig tpm_config;
72c194f7 2606
f0bb276b 2607 if (!x86ms->fw_cfg) {
8b310fc4 2608 ACPI_BUILD_DPRINTF("No fw cfg. Bailing out.\n");
72c194f7
MT
2609 return;
2610 }
2611
021746c1 2612 if (!pcms->acpi_build_enabled) {
8b310fc4 2613 ACPI_BUILD_DPRINTF("ACPI build disabled. Bailing out.\n");
72c194f7
MT
2614 return;
2615 }
2616
17e89077 2617 if (!x86_machine_is_acpi_enabled(X86_MACHINE(pcms))) {
8b310fc4 2618 ACPI_BUILD_DPRINTF("ACPI disabled. Bailing out.\n");
81adc513
MT
2619 return;
2620 }
2621
72c194f7
MT
2622 build_state = g_malloc0(sizeof *build_state);
2623
72c194f7 2624 acpi_build_tables_init(&tables);
3d3ebcad 2625 acpi_build(&tables, MACHINE(pcms));
72c194f7
MT
2626
2627 /* Now expose it all to Guest */
82f76c67
WY
2628 build_state->table_mr = acpi_add_rom_blob(acpi_build_update,
2629 build_state, tables.table_data,
6930ba0d 2630 ACPI_BUILD_TABLE_FILE);
339240b5 2631 assert(build_state->table_mr != NULL);
72c194f7 2632
339240b5 2633 build_state->linker_mr =
82f76c67 2634 acpi_add_rom_blob(acpi_build_update, build_state,
6930ba0d 2635 tables.linker->cmd_blob, ACPI_BUILD_LOADER_FILE);
72c194f7 2636
f0bb276b 2637 fw_cfg_add_file(x86ms->fw_cfg, ACPI_BUILD_TPMLOG_FILE,
42a5b308
SB
2638 tables.tcpalog->data, acpi_data_len(tables.tcpalog));
2639
0fe24669
SB
2640 tpm = tpm_find();
2641 if (tpm && object_property_get_bool(OBJECT(tpm), "ppi", &error_abort)) {
2642 tpm_config = (FwCfgTPMConfig) {
2643 .tpmppi_address = cpu_to_le32(TPM_PPI_ADDR_BASE),
2644 .tpm_version = tpm_get_version(tpm),
ac6dd31e 2645 .tpmppi_version = TPM_PPI_VERSION_1_30
0fe24669 2646 };
f0bb276b 2647 fw_cfg_add_file(x86ms->fw_cfg, "etc/tpm/config",
0fe24669
SB
2648 &tpm_config, sizeof tpm_config);
2649 }
2650
d03637bc
BW
2651 vmgenid_dev = find_vmgenid_dev();
2652 if (vmgenid_dev) {
f0bb276b 2653 vmgenid_add_fw_cfg(VMGENID(vmgenid_dev), x86ms->fw_cfg,
d03637bc
BW
2654 tables.vmgenid);
2655 }
2656
bb292f5a 2657 if (!pcmc->rsdp_in_ram) {
358774d7
IM
2658 /*
2659 * Keep for compatibility with old machine types.
2660 * Though RSDP is small, its contents isn't immutable, so
afaa2e4b 2661 * we'll update it along with the rest of tables on guest access.
358774d7 2662 */
afaa2e4b
MT
2663 uint32_t rsdp_size = acpi_data_len(tables.rsdp);
2664
2665 build_state->rsdp = g_memdup(tables.rsdp->data, rsdp_size);
f0bb276b 2666 fw_cfg_add_file_callback(x86ms->fw_cfg, ACPI_BUILD_RSDP_FILE,
5f9252f7 2667 acpi_build_update, NULL, build_state,
baf2d5bf 2668 build_state->rsdp, rsdp_size, true);
339240b5 2669 build_state->rsdp_mr = NULL;
358774d7 2670 } else {
42d85900 2671 build_state->rsdp = NULL;
82f76c67
WY
2672 build_state->rsdp_mr = acpi_add_rom_blob(acpi_build_update,
2673 build_state, tables.rsdp,
6930ba0d 2674 ACPI_BUILD_RSDP_FILE);
358774d7 2675 }
72c194f7
MT
2676
2677 qemu_register_reset(acpi_build_reset, build_state);
2678 acpi_build_reset(build_state);
2679 vmstate_register(NULL, 0, &vmstate_acpi_build, build_state);
2680
2681 /* Cleanup tables but don't free the memory: we track it
2682 * in build_state.
2683 */
2684 acpi_build_tables_cleanup(&tables, false);
2685}