acpi: use build_append_int_noprefix() API to compose SRAT table
[qemu.git] / hw / i386 / acpi-build.c
blob078097a060b0eb552e4f65c6fd29899a4840090b
1 /* Support for generating ACPI tables and passing them to Guests
3 * Copyright (C) 2008-2010 Kevin O'Connor <kevin@koconnor.net>
4 * Copyright (C) 2006 Fabrice Bellard
5 * Copyright (C) 2013 Red Hat Inc
7 * Author: Michael S. Tsirkin <mst@redhat.com>
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.
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.
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/>.
23 #include "qemu/osdep.h"
24 #include "qapi/error.h"
25 #include "qapi/qmp/qnum.h"
26 #include "acpi-build.h"
27 #include "acpi-common.h"
28 #include "qemu/bitmap.h"
29 #include "qemu/error-report.h"
30 #include "hw/pci/pci.h"
31 #include "hw/core/cpu.h"
32 #include "target/i386/cpu.h"
33 #include "hw/misc/pvpanic.h"
34 #include "hw/timer/hpet.h"
35 #include "hw/acpi/acpi-defs.h"
36 #include "hw/acpi/acpi.h"
37 #include "hw/acpi/cpu.h"
38 #include "hw/nvram/fw_cfg.h"
39 #include "hw/acpi/bios-linker-loader.h"
40 #include "hw/isa/isa.h"
41 #include "hw/block/fdc.h"
42 #include "hw/acpi/memory_hotplug.h"
43 #include "sysemu/tpm.h"
44 #include "hw/acpi/tpm.h"
45 #include "hw/acpi/vmgenid.h"
46 #include "sysemu/tpm_backend.h"
47 #include "hw/rtc/mc146818rtc_regs.h"
48 #include "migration/vmstate.h"
49 #include "hw/mem/memory-device.h"
50 #include "hw/mem/nvdimm.h"
51 #include "sysemu/numa.h"
52 #include "sysemu/reset.h"
53 #include "hw/hyperv/vmbus-bridge.h"
55 /* Supported chipsets: */
56 #include "hw/southbridge/piix.h"
57 #include "hw/acpi/pcihp.h"
58 #include "hw/i386/fw_cfg.h"
59 #include "hw/i386/ich9.h"
60 #include "hw/pci/pci_bus.h"
61 #include "hw/pci-host/q35.h"
62 #include "hw/i386/x86-iommu.h"
64 #include "hw/acpi/aml-build.h"
65 #include "hw/acpi/utils.h"
66 #include "hw/acpi/pci.h"
68 #include "qom/qom-qobject.h"
69 #include "hw/i386/amd_iommu.h"
70 #include "hw/i386/intel_iommu.h"
72 #include "hw/acpi/ipmi.h"
73 #include "hw/acpi/hmat.h"
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.
80 #define ACPI_BUILD_LEGACY_CPU_AML_SIZE 97
81 #define ACPI_BUILD_ALIGN_SIZE 0x1000
83 #define ACPI_BUILD_TABLE_SIZE 0x20000
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
93 typedef struct AcpiPmInfo {
94 bool s3_disabled;
95 bool s4_disabled;
96 bool pcihp_bridge_en;
97 bool smi_on_cpuhp;
98 bool smi_on_cpu_unplug;
99 bool pcihp_root_en;
100 uint8_t s4_val;
101 AcpiFadtData fadt;
102 uint16_t cpu_hp_io_base;
103 uint16_t pcihp_io_base;
104 uint16_t pcihp_io_len;
105 } AcpiPmInfo;
107 typedef struct AcpiMiscInfo {
108 bool is_piix4;
109 bool has_hpet;
110 #ifdef CONFIG_TPM
111 TPMVersion tpm_version;
112 #endif
113 const unsigned char *dsdt_code;
114 unsigned dsdt_size;
115 uint16_t pvpanic_port;
116 uint16_t applesmc_io_base;
117 } AcpiMiscInfo;
119 typedef struct AcpiBuildPciBusHotplugState {
120 GArray *device_table;
121 GArray *notify_table;
122 struct AcpiBuildPciBusHotplugState *parent;
123 bool pcihp_bridge_en;
124 } AcpiBuildPciBusHotplugState;
126 typedef struct FwCfgTPMConfig {
127 uint32_t tpmppi_address;
128 uint8_t tpm_version;
129 uint8_t tpmppi_version;
130 } QEMU_PACKED FwCfgTPMConfig;
132 static bool acpi_get_mcfg(AcpiMcfgInfo *mcfg);
134 const struct AcpiGenericAddress x86_nvdimm_acpi_dsmio = {
135 .space_id = AML_AS_SYSTEM_IO,
136 .address = NVDIMM_ACPI_IO_BASE,
137 .bit_width = NVDIMM_ACPI_IO_LEN << 3
140 static void init_common_fadt_data(MachineState *ms, Object *o,
141 AcpiFadtData *data)
143 X86MachineState *x86ms = X86_MACHINE(ms);
145 * "ICH9-LPC" or "PIIX4_PM" has "smm-compat" property to keep the old
146 * behavior for compatibility irrelevant to smm_enabled, which doesn't
147 * comforms to ACPI spec.
149 bool smm_enabled = object_property_get_bool(o, "smm-compat", NULL) ?
150 true : x86_machine_is_smm_enabled(x86ms);
151 uint32_t io = object_property_get_uint(o, ACPI_PM_PROP_PM_IO_BASE, NULL);
152 AmlAddressSpace as = AML_AS_SYSTEM_IO;
153 AcpiFadtData fadt = {
154 .rev = 3,
155 .flags =
156 (1 << ACPI_FADT_F_WBINVD) |
157 (1 << ACPI_FADT_F_PROC_C1) |
158 (1 << ACPI_FADT_F_SLP_BUTTON) |
159 (1 << ACPI_FADT_F_RTC_S4) |
160 (1 << ACPI_FADT_F_USE_PLATFORM_CLOCK) |
161 /* APIC destination mode ("Flat Logical") has an upper limit of 8
162 * CPUs for more than 8 CPUs, "Clustered Logical" mode has to be
163 * used
165 ((ms->smp.max_cpus > 8) ?
166 (1 << ACPI_FADT_F_FORCE_APIC_CLUSTER_MODEL) : 0),
167 .int_model = 1 /* Multiple APIC */,
168 .rtc_century = RTC_CENTURY,
169 .plvl2_lat = 0xfff /* C2 state not supported */,
170 .plvl3_lat = 0xfff /* C3 state not supported */,
171 .smi_cmd = smm_enabled ? ACPI_PORT_SMI_CMD : 0,
172 .sci_int = object_property_get_uint(o, ACPI_PM_PROP_SCI_INT, NULL),
173 .acpi_enable_cmd =
174 smm_enabled ?
175 object_property_get_uint(o, ACPI_PM_PROP_ACPI_ENABLE_CMD, NULL) :
177 .acpi_disable_cmd =
178 smm_enabled ?
179 object_property_get_uint(o, ACPI_PM_PROP_ACPI_DISABLE_CMD, NULL) :
181 .pm1a_evt = { .space_id = as, .bit_width = 4 * 8, .address = io },
182 .pm1a_cnt = { .space_id = as, .bit_width = 2 * 8,
183 .address = io + 0x04 },
184 .pm_tmr = { .space_id = as, .bit_width = 4 * 8, .address = io + 0x08 },
185 .gpe0_blk = { .space_id = as, .bit_width =
186 object_property_get_uint(o, ACPI_PM_PROP_GPE0_BLK_LEN, NULL) * 8,
187 .address = object_property_get_uint(o, ACPI_PM_PROP_GPE0_BLK, NULL)
190 *data = fadt;
193 static Object *object_resolve_type_unambiguous(const char *typename)
195 bool ambig;
196 Object *o = object_resolve_path_type("", typename, &ambig);
198 if (ambig || !o) {
199 return NULL;
201 return o;
204 static void acpi_get_pm_info(MachineState *machine, AcpiPmInfo *pm)
206 Object *piix = object_resolve_type_unambiguous(TYPE_PIIX4_PM);
207 Object *lpc = object_resolve_type_unambiguous(TYPE_ICH9_LPC_DEVICE);
208 Object *obj = piix ? piix : lpc;
209 QObject *o;
210 pm->cpu_hp_io_base = 0;
211 pm->pcihp_io_base = 0;
212 pm->pcihp_io_len = 0;
213 pm->smi_on_cpuhp = false;
214 pm->smi_on_cpu_unplug = false;
216 assert(obj);
217 init_common_fadt_data(machine, obj, &pm->fadt);
218 if (piix) {
219 /* w2k requires FADT(rev1) or it won't boot, keep PC compatible */
220 pm->fadt.rev = 1;
221 pm->cpu_hp_io_base = PIIX4_CPU_HOTPLUG_IO_BASE;
223 if (lpc) {
224 uint64_t smi_features = object_property_get_uint(lpc,
225 ICH9_LPC_SMI_NEGOTIATED_FEAT_PROP, NULL);
226 struct AcpiGenericAddress r = { .space_id = AML_AS_SYSTEM_IO,
227 .bit_width = 8, .address = ICH9_RST_CNT_IOPORT };
228 pm->fadt.reset_reg = r;
229 pm->fadt.reset_val = 0xf;
230 pm->fadt.flags |= 1 << ACPI_FADT_F_RESET_REG_SUP;
231 pm->cpu_hp_io_base = ICH9_CPU_HOTPLUG_IO_BASE;
232 pm->smi_on_cpuhp =
233 !!(smi_features & BIT_ULL(ICH9_LPC_SMI_F_CPU_HOTPLUG_BIT));
234 pm->smi_on_cpu_unplug =
235 !!(smi_features & BIT_ULL(ICH9_LPC_SMI_F_CPU_HOT_UNPLUG_BIT));
237 pm->pcihp_io_base =
238 object_property_get_uint(obj, ACPI_PCIHP_IO_BASE_PROP, NULL);
239 pm->pcihp_io_len =
240 object_property_get_uint(obj, ACPI_PCIHP_IO_LEN_PROP, NULL);
242 /* The above need not be conditional on machine type because the reset port
243 * happens to be the same on PIIX (pc) and ICH9 (q35). */
244 QEMU_BUILD_BUG_ON(ICH9_RST_CNT_IOPORT != PIIX_RCR_IOPORT);
246 /* Fill in optional s3/s4 related properties */
247 o = object_property_get_qobject(obj, ACPI_PM_PROP_S3_DISABLED, NULL);
248 if (o) {
249 pm->s3_disabled = qnum_get_uint(qobject_to(QNum, o));
250 } else {
251 pm->s3_disabled = false;
253 qobject_unref(o);
254 o = object_property_get_qobject(obj, ACPI_PM_PROP_S4_DISABLED, NULL);
255 if (o) {
256 pm->s4_disabled = qnum_get_uint(qobject_to(QNum, o));
257 } else {
258 pm->s4_disabled = false;
260 qobject_unref(o);
261 o = object_property_get_qobject(obj, ACPI_PM_PROP_S4_VAL, NULL);
262 if (o) {
263 pm->s4_val = qnum_get_uint(qobject_to(QNum, o));
264 } else {
265 pm->s4_val = false;
267 qobject_unref(o);
269 pm->pcihp_bridge_en =
270 object_property_get_bool(obj, ACPI_PM_PROP_ACPI_PCIHP_BRIDGE,
271 NULL);
272 pm->pcihp_root_en =
273 object_property_get_bool(obj, ACPI_PM_PROP_ACPI_PCI_ROOTHP,
274 NULL);
277 static void acpi_get_misc_info(AcpiMiscInfo *info)
279 Object *piix = object_resolve_type_unambiguous(TYPE_PIIX4_PM);
280 Object *lpc = object_resolve_type_unambiguous(TYPE_ICH9_LPC_DEVICE);
281 assert(!!piix != !!lpc);
283 if (piix) {
284 info->is_piix4 = true;
286 if (lpc) {
287 info->is_piix4 = false;
290 info->has_hpet = hpet_find();
291 #ifdef CONFIG_TPM
292 info->tpm_version = tpm_get_version(tpm_find());
293 #endif
294 info->pvpanic_port = pvpanic_port();
295 info->applesmc_io_base = applesmc_port();
299 * Because of the PXB hosts we cannot simply query TYPE_PCI_HOST_BRIDGE.
300 * On i386 arch we only have two pci hosts, so we can look only for them.
302 Object *acpi_get_i386_pci_host(void)
304 PCIHostState *host;
306 host = PCI_HOST_BRIDGE(object_resolve_path("/machine/i440fx", NULL));
307 if (!host) {
308 host = PCI_HOST_BRIDGE(object_resolve_path("/machine/q35", NULL));
311 return OBJECT(host);
314 static void acpi_get_pci_holes(Range *hole, Range *hole64)
316 Object *pci_host;
318 pci_host = acpi_get_i386_pci_host();
320 if (!pci_host) {
321 return;
324 range_set_bounds1(hole,
325 object_property_get_uint(pci_host,
326 PCI_HOST_PROP_PCI_HOLE_START,
327 NULL),
328 object_property_get_uint(pci_host,
329 PCI_HOST_PROP_PCI_HOLE_END,
330 NULL));
331 range_set_bounds1(hole64,
332 object_property_get_uint(pci_host,
333 PCI_HOST_PROP_PCI_HOLE64_START,
334 NULL),
335 object_property_get_uint(pci_host,
336 PCI_HOST_PROP_PCI_HOLE64_END,
337 NULL));
340 static void acpi_align_size(GArray *blob, unsigned align)
342 /* Align size to multiple of given size. This reduces the chance
343 * we need to change size in the future (breaking cross version migration).
345 g_array_set_size(blob, ROUND_UP(acpi_data_len(blob), align));
348 /* FACS */
349 static void
350 build_facs(GArray *table_data)
352 AcpiFacsDescriptorRev1 *facs = acpi_data_push(table_data, sizeof *facs);
353 memcpy(&facs->signature, "FACS", 4);
354 facs->length = cpu_to_le32(sizeof(*facs));
357 static void build_append_pcihp_notify_entry(Aml *method, int slot)
359 Aml *if_ctx;
360 int32_t devfn = PCI_DEVFN(slot, 0);
362 if_ctx = aml_if(aml_and(aml_arg(0), aml_int(0x1U << slot), NULL));
363 aml_append(if_ctx, aml_notify(aml_name("S%.02X", devfn), aml_arg(1)));
364 aml_append(method, if_ctx);
367 static void build_append_pci_bus_devices(Aml *parent_scope, PCIBus *bus,
368 bool pcihp_bridge_en)
370 Aml *dev, *notify_method = NULL, *method;
371 QObject *bsel;
372 PCIBus *sec;
373 int devfn;
375 bsel = object_property_get_qobject(OBJECT(bus), ACPI_PCIHP_PROP_BSEL, NULL);
376 if (bsel) {
377 uint64_t bsel_val = qnum_get_uint(qobject_to(QNum, bsel));
379 aml_append(parent_scope, aml_name_decl("BSEL", aml_int(bsel_val)));
380 notify_method = aml_method("DVNT", 2, AML_NOTSERIALIZED);
383 for (devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
384 DeviceClass *dc;
385 PCIDeviceClass *pc;
386 PCIDevice *pdev = bus->devices[devfn];
387 int slot = PCI_SLOT(devfn);
388 int func = PCI_FUNC(devfn);
389 /* ACPI spec: 1.0b: Table 6-2 _ADR Object Bus Types, PCI type */
390 int adr = slot << 16 | func;
391 bool hotplug_enabled_dev;
392 bool bridge_in_acpi;
393 bool cold_plugged_bridge;
395 if (!pdev) {
397 * add hotplug slots for non present devices.
398 * hotplug is supported only for non-multifunction device
399 * so generate device description only for function 0
401 if (bsel && !func) {
402 if (pci_bus_is_express(bus) && slot > 0) {
403 break;
405 dev = aml_device("S%.02X", devfn);
406 aml_append(dev, aml_name_decl("_SUN", aml_int(slot)));
407 aml_append(dev, aml_name_decl("_ADR", aml_int(adr)));
408 method = aml_method("_EJ0", 1, AML_NOTSERIALIZED);
409 aml_append(method,
410 aml_call2("PCEJ", aml_name("BSEL"), aml_name("_SUN"))
412 aml_append(dev, method);
413 method = aml_method("_DSM", 4, AML_SERIALIZED);
414 aml_append(method,
415 aml_return(aml_call6("PDSM", aml_arg(0), aml_arg(1),
416 aml_arg(2), aml_arg(3),
417 aml_name("BSEL"), aml_name("_SUN")))
419 aml_append(dev, method);
420 aml_append(parent_scope, dev);
422 build_append_pcihp_notify_entry(notify_method, slot);
424 continue;
427 pc = PCI_DEVICE_GET_CLASS(pdev);
428 dc = DEVICE_GET_CLASS(pdev);
431 * Cold plugged bridges aren't themselves hot-pluggable.
432 * Hotplugged bridges *are* hot-pluggable.
434 cold_plugged_bridge = pc->is_bridge && !DEVICE(pdev)->hotplugged;
435 bridge_in_acpi = cold_plugged_bridge && pcihp_bridge_en;
437 hotplug_enabled_dev = bsel && dc->hotpluggable && !cold_plugged_bridge;
439 if (pc->class_id == PCI_CLASS_BRIDGE_ISA) {
440 continue;
444 * allow describing coldplugged bridges in ACPI even if they are not
445 * on function 0, as they are not unpluggable, for all other devices
446 * generate description only for function 0 per slot
448 if (func && !bridge_in_acpi) {
449 continue;
452 /* start to compose PCI device descriptor */
453 dev = aml_device("S%.02X", devfn);
454 aml_append(dev, aml_name_decl("_ADR", aml_int(adr)));
456 if (bsel) {
458 * Can't declare _SUN here for every device as it changes 'slot'
459 * enumeration order in linux kernel, so use another variable for it
461 aml_append(dev, aml_name_decl("ASUN", aml_int(slot)));
462 method = aml_method("_DSM", 4, AML_SERIALIZED);
463 aml_append(method, aml_return(
464 aml_call6("PDSM", aml_arg(0), aml_arg(1), aml_arg(2),
465 aml_arg(3), aml_name("BSEL"), aml_name("ASUN"))
467 aml_append(dev, method);
470 if (pc->class_id == PCI_CLASS_DISPLAY_VGA) {
471 /* add VGA specific AML methods */
472 int s3d;
474 if (object_dynamic_cast(OBJECT(pdev), "qxl-vga")) {
475 s3d = 3;
476 } else {
477 s3d = 0;
480 method = aml_method("_S1D", 0, AML_NOTSERIALIZED);
481 aml_append(method, aml_return(aml_int(0)));
482 aml_append(dev, method);
484 method = aml_method("_S2D", 0, AML_NOTSERIALIZED);
485 aml_append(method, aml_return(aml_int(0)));
486 aml_append(dev, method);
488 method = aml_method("_S3D", 0, AML_NOTSERIALIZED);
489 aml_append(method, aml_return(aml_int(s3d)));
490 aml_append(dev, method);
491 } else if (hotplug_enabled_dev) {
492 aml_append(dev, aml_name_decl("_SUN", aml_int(slot)));
493 /* add _EJ0 to make slot hotpluggable */
494 method = aml_method("_EJ0", 1, AML_NOTSERIALIZED);
495 aml_append(method,
496 aml_call2("PCEJ", aml_name("BSEL"), aml_name("_SUN"))
498 aml_append(dev, method);
500 if (bsel) {
501 build_append_pcihp_notify_entry(notify_method, slot);
503 } else if (bridge_in_acpi) {
505 * device is coldplugged bridge,
506 * add child device descriptions into its scope
508 PCIBus *sec_bus = pci_bridge_get_sec_bus(PCI_BRIDGE(pdev));
510 build_append_pci_bus_devices(dev, sec_bus, pcihp_bridge_en);
512 /* device descriptor has been composed, add it into parent context */
513 aml_append(parent_scope, dev);
516 if (bsel) {
517 aml_append(parent_scope, notify_method);
520 /* Append PCNT method to notify about events on local and child buses.
521 * Add this method for root bus only when hotplug is enabled since DSDT
522 * expects it.
524 if (bsel || pcihp_bridge_en) {
525 method = aml_method("PCNT", 0, AML_NOTSERIALIZED);
527 /* If bus supports hotplug select it and notify about local events */
528 if (bsel) {
529 uint64_t bsel_val = qnum_get_uint(qobject_to(QNum, bsel));
531 aml_append(method, aml_store(aml_int(bsel_val), aml_name("BNUM")));
532 aml_append(method, aml_call2("DVNT", aml_name("PCIU"),
533 aml_int(1))); /* Device Check */
534 aml_append(method, aml_call2("DVNT", aml_name("PCID"),
535 aml_int(3))); /* Eject Request */
538 /* Notify about child bus events in any case */
539 if (pcihp_bridge_en) {
540 QLIST_FOREACH(sec, &bus->child, sibling) {
541 if (pci_bus_is_root(sec)) {
542 continue;
545 aml_append(method, aml_name("^S%.02X.PCNT",
546 sec->parent_dev->devfn));
550 aml_append(parent_scope, method);
552 qobject_unref(bsel);
555 Aml *aml_pci_device_dsm(void)
557 Aml *method, *UUID, *ifctx, *ifctx1, *ifctx2, *ifctx3, *elsectx;
558 Aml *acpi_index = aml_local(0);
559 Aml *zero = aml_int(0);
560 Aml *bnum = aml_arg(4);
561 Aml *func = aml_arg(2);
562 Aml *rev = aml_arg(1);
563 Aml *sun = aml_arg(5);
565 method = aml_method("PDSM", 6, AML_SERIALIZED);
568 * PCI Firmware Specification 3.1
569 * 4.6. _DSM Definitions for PCI
571 UUID = aml_touuid("E5C937D0-3553-4D7A-9117-EA4D19C3434D");
572 ifctx = aml_if(aml_equal(aml_arg(0), UUID));
574 aml_append(ifctx, aml_store(aml_call2("AIDX", bnum, sun), acpi_index));
575 ifctx1 = aml_if(aml_equal(func, zero));
577 uint8_t byte_list[1];
579 ifctx2 = aml_if(aml_equal(rev, aml_int(2)));
582 * advertise function 7 if device has acpi-index
583 * acpi_index values:
584 * 0: not present (default value)
585 * FFFFFFFF: not supported (old QEMU without PIDX reg)
586 * other: device's acpi-index
588 ifctx3 = aml_if(aml_lnot(
589 aml_or(aml_equal(acpi_index, zero),
590 aml_equal(acpi_index, aml_int(0xFFFFFFFF)), NULL)
593 byte_list[0] =
594 1 /* have supported functions */ |
595 1 << 7 /* support for function 7 */
597 aml_append(ifctx3, aml_return(aml_buffer(1, byte_list)));
599 aml_append(ifctx2, ifctx3);
601 aml_append(ifctx1, ifctx2);
603 byte_list[0] = 0; /* nothing supported */
604 aml_append(ifctx1, aml_return(aml_buffer(1, byte_list)));
606 aml_append(ifctx, ifctx1);
607 elsectx = aml_else();
609 * PCI Firmware Specification 3.1
610 * 4.6.7. _DSM for Naming a PCI or PCI Express Device Under
611 * Operating Systems
613 ifctx1 = aml_if(aml_equal(func, aml_int(7)));
615 Aml *pkg = aml_package(2);
616 Aml *ret = aml_local(1);
618 aml_append(pkg, zero);
620 * optional, if not impl. should return null string
622 aml_append(pkg, aml_string("%s", ""));
623 aml_append(ifctx1, aml_store(pkg, ret));
625 * update acpi-index to actual value
627 aml_append(ifctx1, aml_store(acpi_index, aml_index(ret, zero)));
628 aml_append(ifctx1, aml_return(ret));
630 aml_append(elsectx, ifctx1);
631 aml_append(ifctx, elsectx);
633 aml_append(method, ifctx);
634 return method;
638 * build_prt_entry:
639 * @link_name: link name for PCI route entry
641 * build AML package containing a PCI route entry for @link_name
643 static Aml *build_prt_entry(const char *link_name)
645 Aml *a_zero = aml_int(0);
646 Aml *pkg = aml_package(4);
647 aml_append(pkg, a_zero);
648 aml_append(pkg, a_zero);
649 aml_append(pkg, aml_name("%s", link_name));
650 aml_append(pkg, a_zero);
651 return pkg;
655 * initialize_route - Initialize the interrupt routing rule
656 * through a specific LINK:
657 * if (lnk_idx == idx)
658 * route using link 'link_name'
660 static Aml *initialize_route(Aml *route, const char *link_name,
661 Aml *lnk_idx, int idx)
663 Aml *if_ctx = aml_if(aml_equal(lnk_idx, aml_int(idx)));
664 Aml *pkg = build_prt_entry(link_name);
666 aml_append(if_ctx, aml_store(pkg, route));
668 return if_ctx;
672 * build_prt - Define interrupt rounting rules
674 * Returns an array of 128 routes, one for each device,
675 * based on device location.
676 * The main goal is to equaly distribute the interrupts
677 * over the 4 existing ACPI links (works only for i440fx).
678 * The hash function is (slot + pin) & 3 -> "LNK[D|A|B|C]".
681 static Aml *build_prt(bool is_pci0_prt)
683 Aml *method, *while_ctx, *pin, *res;
685 method = aml_method("_PRT", 0, AML_NOTSERIALIZED);
686 res = aml_local(0);
687 pin = aml_local(1);
688 aml_append(method, aml_store(aml_package(128), res));
689 aml_append(method, aml_store(aml_int(0), pin));
691 /* while (pin < 128) */
692 while_ctx = aml_while(aml_lless(pin, aml_int(128)));
694 Aml *slot = aml_local(2);
695 Aml *lnk_idx = aml_local(3);
696 Aml *route = aml_local(4);
698 /* slot = pin >> 2 */
699 aml_append(while_ctx,
700 aml_store(aml_shiftright(pin, aml_int(2), NULL), slot));
701 /* lnk_idx = (slot + pin) & 3 */
702 aml_append(while_ctx,
703 aml_store(aml_and(aml_add(pin, slot, NULL), aml_int(3), NULL),
704 lnk_idx));
706 /* route[2] = "LNK[D|A|B|C]", selection based on pin % 3 */
707 aml_append(while_ctx, initialize_route(route, "LNKD", lnk_idx, 0));
708 if (is_pci0_prt) {
709 Aml *if_device_1, *if_pin_4, *else_pin_4;
711 /* device 1 is the power-management device, needs SCI */
712 if_device_1 = aml_if(aml_equal(lnk_idx, aml_int(1)));
714 if_pin_4 = aml_if(aml_equal(pin, aml_int(4)));
716 aml_append(if_pin_4,
717 aml_store(build_prt_entry("LNKS"), route));
719 aml_append(if_device_1, if_pin_4);
720 else_pin_4 = aml_else();
722 aml_append(else_pin_4,
723 aml_store(build_prt_entry("LNKA"), route));
725 aml_append(if_device_1, else_pin_4);
727 aml_append(while_ctx, if_device_1);
728 } else {
729 aml_append(while_ctx, initialize_route(route, "LNKA", lnk_idx, 1));
731 aml_append(while_ctx, initialize_route(route, "LNKB", lnk_idx, 2));
732 aml_append(while_ctx, initialize_route(route, "LNKC", lnk_idx, 3));
734 /* route[0] = 0x[slot]FFFF */
735 aml_append(while_ctx,
736 aml_store(aml_or(aml_shiftleft(slot, aml_int(16)), aml_int(0xFFFF),
737 NULL),
738 aml_index(route, aml_int(0))));
739 /* route[1] = pin & 3 */
740 aml_append(while_ctx,
741 aml_store(aml_and(pin, aml_int(3), NULL),
742 aml_index(route, aml_int(1))));
743 /* res[pin] = route */
744 aml_append(while_ctx, aml_store(route, aml_index(res, pin)));
745 /* pin++ */
746 aml_append(while_ctx, aml_increment(pin));
748 aml_append(method, while_ctx);
749 /* return res*/
750 aml_append(method, aml_return(res));
752 return method;
755 static void build_hpet_aml(Aml *table)
757 Aml *crs;
758 Aml *field;
759 Aml *method;
760 Aml *if_ctx;
761 Aml *scope = aml_scope("_SB");
762 Aml *dev = aml_device("HPET");
763 Aml *zero = aml_int(0);
764 Aml *id = aml_local(0);
765 Aml *period = aml_local(1);
767 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0103")));
768 aml_append(dev, aml_name_decl("_UID", zero));
770 aml_append(dev,
771 aml_operation_region("HPTM", AML_SYSTEM_MEMORY, aml_int(HPET_BASE),
772 HPET_LEN));
773 field = aml_field("HPTM", AML_DWORD_ACC, AML_LOCK, AML_PRESERVE);
774 aml_append(field, aml_named_field("VEND", 32));
775 aml_append(field, aml_named_field("PRD", 32));
776 aml_append(dev, field);
778 method = aml_method("_STA", 0, AML_NOTSERIALIZED);
779 aml_append(method, aml_store(aml_name("VEND"), id));
780 aml_append(method, aml_store(aml_name("PRD"), period));
781 aml_append(method, aml_shiftright(id, aml_int(16), id));
782 if_ctx = aml_if(aml_lor(aml_equal(id, zero),
783 aml_equal(id, aml_int(0xffff))));
785 aml_append(if_ctx, aml_return(zero));
787 aml_append(method, if_ctx);
789 if_ctx = aml_if(aml_lor(aml_equal(period, zero),
790 aml_lgreater(period, aml_int(100000000))));
792 aml_append(if_ctx, aml_return(zero));
794 aml_append(method, if_ctx);
796 aml_append(method, aml_return(aml_int(0x0F)));
797 aml_append(dev, method);
799 crs = aml_resource_template();
800 aml_append(crs, aml_memory32_fixed(HPET_BASE, HPET_LEN, AML_READ_ONLY));
801 aml_append(dev, aml_name_decl("_CRS", crs));
803 aml_append(scope, dev);
804 aml_append(table, scope);
807 static Aml *build_vmbus_device_aml(VMBusBridge *vmbus_bridge)
809 Aml *dev;
810 Aml *method;
811 Aml *crs;
813 dev = aml_device("VMBS");
814 aml_append(dev, aml_name_decl("STA", aml_int(0xF)));
815 aml_append(dev, aml_name_decl("_HID", aml_string("VMBus")));
816 aml_append(dev, aml_name_decl("_UID", aml_int(0x0)));
817 aml_append(dev, aml_name_decl("_DDN", aml_string("VMBUS")));
819 method = aml_method("_DIS", 0, AML_NOTSERIALIZED);
820 aml_append(method, aml_store(aml_and(aml_name("STA"), aml_int(0xD), NULL),
821 aml_name("STA")));
822 aml_append(dev, method);
824 method = aml_method("_PS0", 0, AML_NOTSERIALIZED);
825 aml_append(method, aml_store(aml_or(aml_name("STA"), aml_int(0xF), NULL),
826 aml_name("STA")));
827 aml_append(dev, method);
829 method = aml_method("_STA", 0, AML_NOTSERIALIZED);
830 aml_append(method, aml_return(aml_name("STA")));
831 aml_append(dev, method);
833 aml_append(dev, aml_name_decl("_PS3", aml_int(0x0)));
835 crs = aml_resource_template();
836 aml_append(crs, aml_irq_no_flags(vmbus_bridge->irq));
837 aml_append(dev, aml_name_decl("_CRS", crs));
839 return dev;
842 static void build_isa_devices_aml(Aml *table)
844 bool ambiguous;
845 Object *obj = object_resolve_path_type("", TYPE_ISA_BUS, &ambiguous);
846 Aml *scope;
848 assert(obj && !ambiguous);
850 scope = aml_scope("_SB.PCI0.ISA");
851 build_acpi_ipmi_devices(scope, BUS(obj), "\\_SB.PCI0.ISA");
852 isa_build_aml(ISA_BUS(obj), scope);
854 aml_append(table, scope);
857 static void build_dbg_aml(Aml *table)
859 Aml *field;
860 Aml *method;
861 Aml *while_ctx;
862 Aml *scope = aml_scope("\\");
863 Aml *buf = aml_local(0);
864 Aml *len = aml_local(1);
865 Aml *idx = aml_local(2);
867 aml_append(scope,
868 aml_operation_region("DBG", AML_SYSTEM_IO, aml_int(0x0402), 0x01));
869 field = aml_field("DBG", AML_BYTE_ACC, AML_NOLOCK, AML_PRESERVE);
870 aml_append(field, aml_named_field("DBGB", 8));
871 aml_append(scope, field);
873 method = aml_method("DBUG", 1, AML_NOTSERIALIZED);
875 aml_append(method, aml_to_hexstring(aml_arg(0), buf));
876 aml_append(method, aml_to_buffer(buf, buf));
877 aml_append(method, aml_subtract(aml_sizeof(buf), aml_int(1), len));
878 aml_append(method, aml_store(aml_int(0), idx));
880 while_ctx = aml_while(aml_lless(idx, len));
881 aml_append(while_ctx,
882 aml_store(aml_derefof(aml_index(buf, idx)), aml_name("DBGB")));
883 aml_append(while_ctx, aml_increment(idx));
884 aml_append(method, while_ctx);
886 aml_append(method, aml_store(aml_int(0x0A), aml_name("DBGB")));
887 aml_append(scope, method);
889 aml_append(table, scope);
892 static Aml *build_link_dev(const char *name, uint8_t uid, Aml *reg)
894 Aml *dev;
895 Aml *crs;
896 Aml *method;
897 uint32_t irqs[] = {5, 10, 11};
899 dev = aml_device("%s", name);
900 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C0F")));
901 aml_append(dev, aml_name_decl("_UID", aml_int(uid)));
903 crs = aml_resource_template();
904 aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL, AML_ACTIVE_HIGH,
905 AML_SHARED, irqs, ARRAY_SIZE(irqs)));
906 aml_append(dev, aml_name_decl("_PRS", crs));
908 method = aml_method("_STA", 0, AML_NOTSERIALIZED);
909 aml_append(method, aml_return(aml_call1("IQST", reg)));
910 aml_append(dev, method);
912 method = aml_method("_DIS", 0, AML_NOTSERIALIZED);
913 aml_append(method, aml_or(reg, aml_int(0x80), reg));
914 aml_append(dev, method);
916 method = aml_method("_CRS", 0, AML_NOTSERIALIZED);
917 aml_append(method, aml_return(aml_call1("IQCR", reg)));
918 aml_append(dev, method);
920 method = aml_method("_SRS", 1, AML_NOTSERIALIZED);
921 aml_append(method, aml_create_dword_field(aml_arg(0), aml_int(5), "PRRI"));
922 aml_append(method, aml_store(aml_name("PRRI"), reg));
923 aml_append(dev, method);
925 return dev;
928 static Aml *build_gsi_link_dev(const char *name, uint8_t uid, uint8_t gsi)
930 Aml *dev;
931 Aml *crs;
932 Aml *method;
933 uint32_t irqs;
935 dev = aml_device("%s", name);
936 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C0F")));
937 aml_append(dev, aml_name_decl("_UID", aml_int(uid)));
939 crs = aml_resource_template();
940 irqs = gsi;
941 aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL, AML_ACTIVE_HIGH,
942 AML_SHARED, &irqs, 1));
943 aml_append(dev, aml_name_decl("_PRS", crs));
945 aml_append(dev, aml_name_decl("_CRS", crs));
948 * _DIS can be no-op because the interrupt cannot be disabled.
950 method = aml_method("_DIS", 0, AML_NOTSERIALIZED);
951 aml_append(dev, method);
953 method = aml_method("_SRS", 1, AML_NOTSERIALIZED);
954 aml_append(dev, method);
956 return dev;
959 /* _CRS method - get current settings */
960 static Aml *build_iqcr_method(bool is_piix4)
962 Aml *if_ctx;
963 uint32_t irqs;
964 Aml *method = aml_method("IQCR", 1, AML_SERIALIZED);
965 Aml *crs = aml_resource_template();
967 irqs = 0;
968 aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL,
969 AML_ACTIVE_HIGH, AML_SHARED, &irqs, 1));
970 aml_append(method, aml_name_decl("PRR0", crs));
972 aml_append(method,
973 aml_create_dword_field(aml_name("PRR0"), aml_int(5), "PRRI"));
975 if (is_piix4) {
976 if_ctx = aml_if(aml_lless(aml_arg(0), aml_int(0x80)));
977 aml_append(if_ctx, aml_store(aml_arg(0), aml_name("PRRI")));
978 aml_append(method, if_ctx);
979 } else {
980 aml_append(method,
981 aml_store(aml_and(aml_arg(0), aml_int(0xF), NULL),
982 aml_name("PRRI")));
985 aml_append(method, aml_return(aml_name("PRR0")));
986 return method;
989 /* _STA method - get status */
990 static Aml *build_irq_status_method(void)
992 Aml *if_ctx;
993 Aml *method = aml_method("IQST", 1, AML_NOTSERIALIZED);
995 if_ctx = aml_if(aml_and(aml_int(0x80), aml_arg(0), NULL));
996 aml_append(if_ctx, aml_return(aml_int(0x09)));
997 aml_append(method, if_ctx);
998 aml_append(method, aml_return(aml_int(0x0B)));
999 return method;
1002 static void build_piix4_pci0_int(Aml *table)
1004 Aml *dev;
1005 Aml *crs;
1006 Aml *field;
1007 Aml *method;
1008 uint32_t irqs;
1009 Aml *sb_scope = aml_scope("_SB");
1010 Aml *pci0_scope = aml_scope("PCI0");
1012 aml_append(pci0_scope, build_prt(true));
1013 aml_append(sb_scope, pci0_scope);
1015 field = aml_field("PCI0.ISA.P40C", AML_BYTE_ACC, AML_NOLOCK, AML_PRESERVE);
1016 aml_append(field, aml_named_field("PRQ0", 8));
1017 aml_append(field, aml_named_field("PRQ1", 8));
1018 aml_append(field, aml_named_field("PRQ2", 8));
1019 aml_append(field, aml_named_field("PRQ3", 8));
1020 aml_append(sb_scope, field);
1022 aml_append(sb_scope, build_irq_status_method());
1023 aml_append(sb_scope, build_iqcr_method(true));
1025 aml_append(sb_scope, build_link_dev("LNKA", 0, aml_name("PRQ0")));
1026 aml_append(sb_scope, build_link_dev("LNKB", 1, aml_name("PRQ1")));
1027 aml_append(sb_scope, build_link_dev("LNKC", 2, aml_name("PRQ2")));
1028 aml_append(sb_scope, build_link_dev("LNKD", 3, aml_name("PRQ3")));
1030 dev = aml_device("LNKS");
1032 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C0F")));
1033 aml_append(dev, aml_name_decl("_UID", aml_int(4)));
1035 crs = aml_resource_template();
1036 irqs = 9;
1037 aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL,
1038 AML_ACTIVE_HIGH, AML_SHARED,
1039 &irqs, 1));
1040 aml_append(dev, aml_name_decl("_PRS", crs));
1042 /* The SCI cannot be disabled and is always attached to GSI 9,
1043 * so these are no-ops. We only need this link to override the
1044 * polarity to active high and match the content of the MADT.
1046 method = aml_method("_STA", 0, AML_NOTSERIALIZED);
1047 aml_append(method, aml_return(aml_int(0x0b)));
1048 aml_append(dev, method);
1050 method = aml_method("_DIS", 0, AML_NOTSERIALIZED);
1051 aml_append(dev, method);
1053 method = aml_method("_CRS", 0, AML_NOTSERIALIZED);
1054 aml_append(method, aml_return(aml_name("_PRS")));
1055 aml_append(dev, method);
1057 method = aml_method("_SRS", 1, AML_NOTSERIALIZED);
1058 aml_append(dev, method);
1060 aml_append(sb_scope, dev);
1062 aml_append(table, sb_scope);
1065 static void append_q35_prt_entry(Aml *ctx, uint32_t nr, const char *name)
1067 int i;
1068 int head;
1069 Aml *pkg;
1070 char base = name[3] < 'E' ? 'A' : 'E';
1071 char *s = g_strdup(name);
1072 Aml *a_nr = aml_int((nr << 16) | 0xffff);
1074 assert(strlen(s) == 4);
1076 head = name[3] - base;
1077 for (i = 0; i < 4; i++) {
1078 if (head + i > 3) {
1079 head = i * -1;
1081 s[3] = base + head + i;
1082 pkg = aml_package(4);
1083 aml_append(pkg, a_nr);
1084 aml_append(pkg, aml_int(i));
1085 aml_append(pkg, aml_name("%s", s));
1086 aml_append(pkg, aml_int(0));
1087 aml_append(ctx, pkg);
1089 g_free(s);
1092 static Aml *build_q35_routing_table(const char *str)
1094 int i;
1095 Aml *pkg;
1096 char *name = g_strdup_printf("%s ", str);
1098 pkg = aml_package(128);
1099 for (i = 0; i < 0x18; i++) {
1100 name[3] = 'E' + (i & 0x3);
1101 append_q35_prt_entry(pkg, i, name);
1104 name[3] = 'E';
1105 append_q35_prt_entry(pkg, 0x18, name);
1107 /* INTA -> PIRQA for slot 25 - 31, see the default value of D<N>IR */
1108 for (i = 0x0019; i < 0x1e; i++) {
1109 name[3] = 'A';
1110 append_q35_prt_entry(pkg, i, name);
1113 /* PCIe->PCI bridge. use PIRQ[E-H] */
1114 name[3] = 'E';
1115 append_q35_prt_entry(pkg, 0x1e, name);
1116 name[3] = 'A';
1117 append_q35_prt_entry(pkg, 0x1f, name);
1119 g_free(name);
1120 return pkg;
1123 static void build_q35_pci0_int(Aml *table)
1125 Aml *field;
1126 Aml *method;
1127 Aml *sb_scope = aml_scope("_SB");
1128 Aml *pci0_scope = aml_scope("PCI0");
1130 /* Zero => PIC mode, One => APIC Mode */
1131 aml_append(table, aml_name_decl("PICF", aml_int(0)));
1132 method = aml_method("_PIC", 1, AML_NOTSERIALIZED);
1134 aml_append(method, aml_store(aml_arg(0), aml_name("PICF")));
1136 aml_append(table, method);
1138 aml_append(pci0_scope,
1139 aml_name_decl("PRTP", build_q35_routing_table("LNK")));
1140 aml_append(pci0_scope,
1141 aml_name_decl("PRTA", build_q35_routing_table("GSI")));
1143 method = aml_method("_PRT", 0, AML_NOTSERIALIZED);
1145 Aml *if_ctx;
1146 Aml *else_ctx;
1148 /* PCI IRQ routing table, example from ACPI 2.0a specification,
1149 section 6.2.8.1 */
1150 /* Note: we provide the same info as the PCI routing
1151 table of the Bochs BIOS */
1152 if_ctx = aml_if(aml_equal(aml_name("PICF"), aml_int(0)));
1153 aml_append(if_ctx, aml_return(aml_name("PRTP")));
1154 aml_append(method, if_ctx);
1155 else_ctx = aml_else();
1156 aml_append(else_ctx, aml_return(aml_name("PRTA")));
1157 aml_append(method, else_ctx);
1159 aml_append(pci0_scope, method);
1160 aml_append(sb_scope, pci0_scope);
1162 field = aml_field("PCI0.ISA.PIRQ", AML_BYTE_ACC, AML_NOLOCK, AML_PRESERVE);
1163 aml_append(field, aml_named_field("PRQA", 8));
1164 aml_append(field, aml_named_field("PRQB", 8));
1165 aml_append(field, aml_named_field("PRQC", 8));
1166 aml_append(field, aml_named_field("PRQD", 8));
1167 aml_append(field, aml_reserved_field(0x20));
1168 aml_append(field, aml_named_field("PRQE", 8));
1169 aml_append(field, aml_named_field("PRQF", 8));
1170 aml_append(field, aml_named_field("PRQG", 8));
1171 aml_append(field, aml_named_field("PRQH", 8));
1172 aml_append(sb_scope, field);
1174 aml_append(sb_scope, build_irq_status_method());
1175 aml_append(sb_scope, build_iqcr_method(false));
1177 aml_append(sb_scope, build_link_dev("LNKA", 0, aml_name("PRQA")));
1178 aml_append(sb_scope, build_link_dev("LNKB", 1, aml_name("PRQB")));
1179 aml_append(sb_scope, build_link_dev("LNKC", 2, aml_name("PRQC")));
1180 aml_append(sb_scope, build_link_dev("LNKD", 3, aml_name("PRQD")));
1181 aml_append(sb_scope, build_link_dev("LNKE", 4, aml_name("PRQE")));
1182 aml_append(sb_scope, build_link_dev("LNKF", 5, aml_name("PRQF")));
1183 aml_append(sb_scope, build_link_dev("LNKG", 6, aml_name("PRQG")));
1184 aml_append(sb_scope, build_link_dev("LNKH", 7, aml_name("PRQH")));
1186 aml_append(sb_scope, build_gsi_link_dev("GSIA", 0x10, 0x10));
1187 aml_append(sb_scope, build_gsi_link_dev("GSIB", 0x11, 0x11));
1188 aml_append(sb_scope, build_gsi_link_dev("GSIC", 0x12, 0x12));
1189 aml_append(sb_scope, build_gsi_link_dev("GSID", 0x13, 0x13));
1190 aml_append(sb_scope, build_gsi_link_dev("GSIE", 0x14, 0x14));
1191 aml_append(sb_scope, build_gsi_link_dev("GSIF", 0x15, 0x15));
1192 aml_append(sb_scope, build_gsi_link_dev("GSIG", 0x16, 0x16));
1193 aml_append(sb_scope, build_gsi_link_dev("GSIH", 0x17, 0x17));
1195 aml_append(table, sb_scope);
1198 static Aml *build_q35_dram_controller(const AcpiMcfgInfo *mcfg)
1200 Aml *dev;
1201 Aml *resource_template;
1203 /* DRAM controller */
1204 dev = aml_device("DRAC");
1205 aml_append(dev, aml_name_decl("_HID", aml_string("PNP0C01")));
1207 resource_template = aml_resource_template();
1208 if (mcfg->base + mcfg->size - 1 >= (1ULL << 32)) {
1209 aml_append(resource_template,
1210 aml_qword_memory(AML_POS_DECODE,
1211 AML_MIN_FIXED,
1212 AML_MAX_FIXED,
1213 AML_NON_CACHEABLE,
1214 AML_READ_WRITE,
1215 0x0000000000000000,
1216 mcfg->base,
1217 mcfg->base + mcfg->size - 1,
1218 0x0000000000000000,
1219 mcfg->size));
1220 } else {
1221 aml_append(resource_template,
1222 aml_dword_memory(AML_POS_DECODE,
1223 AML_MIN_FIXED,
1224 AML_MAX_FIXED,
1225 AML_NON_CACHEABLE,
1226 AML_READ_WRITE,
1227 0x0000000000000000,
1228 mcfg->base,
1229 mcfg->base + mcfg->size - 1,
1230 0x0000000000000000,
1231 mcfg->size));
1233 aml_append(dev, aml_name_decl("_CRS", resource_template));
1235 return dev;
1238 static void build_q35_isa_bridge(Aml *table)
1240 Aml *dev;
1241 Aml *scope;
1243 scope = aml_scope("_SB.PCI0");
1244 dev = aml_device("ISA");
1245 aml_append(dev, aml_name_decl("_ADR", aml_int(0x001F0000)));
1247 /* ICH9 PCI to ISA irq remapping */
1248 aml_append(dev, aml_operation_region("PIRQ", AML_PCI_CONFIG,
1249 aml_int(0x60), 0x0C));
1251 aml_append(scope, dev);
1252 aml_append(table, scope);
1255 static void build_piix4_isa_bridge(Aml *table)
1257 Aml *dev;
1258 Aml *scope;
1260 scope = aml_scope("_SB.PCI0");
1261 dev = aml_device("ISA");
1262 aml_append(dev, aml_name_decl("_ADR", aml_int(0x00010000)));
1264 /* PIIX PCI to ISA irq remapping */
1265 aml_append(dev, aml_operation_region("P40C", AML_PCI_CONFIG,
1266 aml_int(0x60), 0x04));
1268 aml_append(scope, dev);
1269 aml_append(table, scope);
1272 static void build_x86_acpi_pci_hotplug(Aml *table, uint64_t pcihp_addr)
1274 Aml *scope;
1275 Aml *field;
1276 Aml *method;
1278 scope = aml_scope("_SB.PCI0");
1280 aml_append(scope,
1281 aml_operation_region("PCST", AML_SYSTEM_IO, aml_int(pcihp_addr), 0x08));
1282 field = aml_field("PCST", AML_DWORD_ACC, AML_NOLOCK, AML_WRITE_AS_ZEROS);
1283 aml_append(field, aml_named_field("PCIU", 32));
1284 aml_append(field, aml_named_field("PCID", 32));
1285 aml_append(scope, field);
1287 aml_append(scope,
1288 aml_operation_region("SEJ", AML_SYSTEM_IO,
1289 aml_int(pcihp_addr + ACPI_PCIHP_SEJ_BASE), 0x04));
1290 field = aml_field("SEJ", AML_DWORD_ACC, AML_NOLOCK, AML_WRITE_AS_ZEROS);
1291 aml_append(field, aml_named_field("B0EJ", 32));
1292 aml_append(scope, field);
1294 aml_append(scope,
1295 aml_operation_region("BNMR", AML_SYSTEM_IO,
1296 aml_int(pcihp_addr + ACPI_PCIHP_BNMR_BASE), 0x08));
1297 field = aml_field("BNMR", AML_DWORD_ACC, AML_NOLOCK, AML_WRITE_AS_ZEROS);
1298 aml_append(field, aml_named_field("BNUM", 32));
1299 aml_append(field, aml_named_field("PIDX", 32));
1300 aml_append(scope, field);
1302 aml_append(scope, aml_mutex("BLCK", 0));
1304 method = aml_method("PCEJ", 2, AML_NOTSERIALIZED);
1305 aml_append(method, aml_acquire(aml_name("BLCK"), 0xFFFF));
1306 aml_append(method, aml_store(aml_arg(0), aml_name("BNUM")));
1307 aml_append(method,
1308 aml_store(aml_shiftleft(aml_int(1), aml_arg(1)), aml_name("B0EJ")));
1309 aml_append(method, aml_release(aml_name("BLCK")));
1310 aml_append(method, aml_return(aml_int(0)));
1311 aml_append(scope, method);
1313 method = aml_method("AIDX", 2, AML_NOTSERIALIZED);
1314 aml_append(method, aml_acquire(aml_name("BLCK"), 0xFFFF));
1315 aml_append(method, aml_store(aml_arg(0), aml_name("BNUM")));
1316 aml_append(method,
1317 aml_store(aml_shiftleft(aml_int(1), aml_arg(1)), aml_name("PIDX")));
1318 aml_append(method, aml_store(aml_name("PIDX"), aml_local(0)));
1319 aml_append(method, aml_release(aml_name("BLCK")));
1320 aml_append(method, aml_return(aml_local(0)));
1321 aml_append(scope, method);
1323 aml_append(scope, aml_pci_device_dsm());
1325 aml_append(table, scope);
1328 static Aml *build_q35_osc_method(void)
1330 Aml *if_ctx;
1331 Aml *if_ctx2;
1332 Aml *else_ctx;
1333 Aml *method;
1334 Aml *a_cwd1 = aml_name("CDW1");
1335 Aml *a_ctrl = aml_local(0);
1337 method = aml_method("_OSC", 4, AML_NOTSERIALIZED);
1338 aml_append(method, aml_create_dword_field(aml_arg(3), aml_int(0), "CDW1"));
1340 if_ctx = aml_if(aml_equal(
1341 aml_arg(0), aml_touuid("33DB4D5B-1FF7-401C-9657-7441C03DD766")));
1342 aml_append(if_ctx, aml_create_dword_field(aml_arg(3), aml_int(4), "CDW2"));
1343 aml_append(if_ctx, aml_create_dword_field(aml_arg(3), aml_int(8), "CDW3"));
1345 aml_append(if_ctx, aml_store(aml_name("CDW3"), a_ctrl));
1348 * Always allow native PME, AER (no dependencies)
1349 * Allow SHPC (PCI bridges can have SHPC controller)
1351 aml_append(if_ctx, aml_and(a_ctrl, aml_int(0x1F), a_ctrl));
1353 if_ctx2 = aml_if(aml_lnot(aml_equal(aml_arg(1), aml_int(1))));
1354 /* Unknown revision */
1355 aml_append(if_ctx2, aml_or(a_cwd1, aml_int(0x08), a_cwd1));
1356 aml_append(if_ctx, if_ctx2);
1358 if_ctx2 = aml_if(aml_lnot(aml_equal(aml_name("CDW3"), a_ctrl)));
1359 /* Capabilities bits were masked */
1360 aml_append(if_ctx2, aml_or(a_cwd1, aml_int(0x10), a_cwd1));
1361 aml_append(if_ctx, if_ctx2);
1363 /* Update DWORD3 in the buffer */
1364 aml_append(if_ctx, aml_store(a_ctrl, aml_name("CDW3")));
1365 aml_append(method, if_ctx);
1367 else_ctx = aml_else();
1368 /* Unrecognized UUID */
1369 aml_append(else_ctx, aml_or(a_cwd1, aml_int(4), a_cwd1));
1370 aml_append(method, else_ctx);
1372 aml_append(method, aml_return(aml_arg(3)));
1373 return method;
1376 static void build_smb0(Aml *table, I2CBus *smbus, int devnr, int func)
1378 Aml *scope = aml_scope("_SB.PCI0");
1379 Aml *dev = aml_device("SMB0");
1381 aml_append(dev, aml_name_decl("_ADR", aml_int(devnr << 16 | func)));
1382 build_acpi_ipmi_devices(dev, BUS(smbus), "\\_SB.PCI0.SMB0");
1383 aml_append(scope, dev);
1384 aml_append(table, scope);
1387 static void
1388 build_dsdt(GArray *table_data, BIOSLinker *linker,
1389 AcpiPmInfo *pm, AcpiMiscInfo *misc,
1390 Range *pci_hole, Range *pci_hole64, MachineState *machine)
1392 CrsRangeEntry *entry;
1393 Aml *dsdt, *sb_scope, *scope, *dev, *method, *field, *pkg, *crs;
1394 CrsRangeSet crs_range_set;
1395 PCMachineState *pcms = PC_MACHINE(machine);
1396 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(machine);
1397 X86MachineState *x86ms = X86_MACHINE(machine);
1398 AcpiMcfgInfo mcfg;
1399 bool mcfg_valid = !!acpi_get_mcfg(&mcfg);
1400 uint32_t nr_mem = machine->ram_slots;
1401 int root_bus_limit = 0xFF;
1402 PCIBus *bus = NULL;
1403 #ifdef CONFIG_TPM
1404 TPMIf *tpm = tpm_find();
1405 #endif
1406 int i;
1407 VMBusBridge *vmbus_bridge = vmbus_bridge_find();
1408 AcpiTable table = { .sig = "DSDT", .rev = 1, .oem_id = x86ms->oem_id,
1409 .oem_table_id = x86ms->oem_table_id };
1411 acpi_table_begin(&table, table_data);
1412 dsdt = init_aml_allocator();
1414 build_dbg_aml(dsdt);
1415 if (misc->is_piix4) {
1416 sb_scope = aml_scope("_SB");
1417 dev = aml_device("PCI0");
1418 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A03")));
1419 aml_append(dev, aml_name_decl("_ADR", aml_int(0)));
1420 aml_append(dev, aml_name_decl("_UID", aml_int(pcmc->pci_root_uid)));
1421 aml_append(sb_scope, dev);
1422 aml_append(dsdt, sb_scope);
1424 if (misc->has_hpet) {
1425 build_hpet_aml(dsdt);
1427 build_piix4_isa_bridge(dsdt);
1428 build_isa_devices_aml(dsdt);
1429 if (pm->pcihp_bridge_en || pm->pcihp_root_en) {
1430 build_x86_acpi_pci_hotplug(dsdt, pm->pcihp_io_base);
1432 build_piix4_pci0_int(dsdt);
1433 } else {
1434 sb_scope = aml_scope("_SB");
1435 dev = aml_device("PCI0");
1436 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A08")));
1437 aml_append(dev, aml_name_decl("_CID", aml_eisaid("PNP0A03")));
1438 aml_append(dev, aml_name_decl("_ADR", aml_int(0)));
1439 aml_append(dev, aml_name_decl("_UID", aml_int(pcmc->pci_root_uid)));
1440 aml_append(dev, build_q35_osc_method());
1441 aml_append(sb_scope, dev);
1442 if (mcfg_valid) {
1443 aml_append(sb_scope, build_q35_dram_controller(&mcfg));
1446 if (pm->smi_on_cpuhp) {
1447 /* reserve SMI block resources, IO ports 0xB2, 0xB3 */
1448 dev = aml_device("PCI0.SMI0");
1449 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A06")));
1450 aml_append(dev, aml_name_decl("_UID", aml_string("SMI resources")));
1451 crs = aml_resource_template();
1452 aml_append(crs,
1453 aml_io(
1454 AML_DECODE16,
1455 ACPI_PORT_SMI_CMD,
1456 ACPI_PORT_SMI_CMD,
1460 aml_append(dev, aml_name_decl("_CRS", crs));
1461 aml_append(dev, aml_operation_region("SMIR", AML_SYSTEM_IO,
1462 aml_int(ACPI_PORT_SMI_CMD), 2));
1463 field = aml_field("SMIR", AML_BYTE_ACC, AML_NOLOCK,
1464 AML_WRITE_AS_ZEROS);
1465 aml_append(field, aml_named_field("SMIC", 8));
1466 aml_append(field, aml_reserved_field(8));
1467 aml_append(dev, field);
1468 aml_append(sb_scope, dev);
1471 aml_append(dsdt, sb_scope);
1473 if (misc->has_hpet) {
1474 build_hpet_aml(dsdt);
1476 build_q35_isa_bridge(dsdt);
1477 build_isa_devices_aml(dsdt);
1478 if (pm->pcihp_bridge_en) {
1479 build_x86_acpi_pci_hotplug(dsdt, pm->pcihp_io_base);
1481 build_q35_pci0_int(dsdt);
1482 if (pcms->smbus && !pcmc->do_not_add_smb_acpi) {
1483 build_smb0(dsdt, pcms->smbus, ICH9_SMB_DEV, ICH9_SMB_FUNC);
1487 if (vmbus_bridge) {
1488 sb_scope = aml_scope("_SB");
1489 aml_append(sb_scope, build_vmbus_device_aml(vmbus_bridge));
1490 aml_append(dsdt, sb_scope);
1493 if (pcmc->legacy_cpu_hotplug) {
1494 build_legacy_cpu_hotplug_aml(dsdt, machine, pm->cpu_hp_io_base);
1495 } else {
1496 CPUHotplugFeatures opts = {
1497 .acpi_1_compatible = true, .has_legacy_cphp = true,
1498 .smi_path = pm->smi_on_cpuhp ? "\\_SB.PCI0.SMI0.SMIC" : NULL,
1499 .fw_unplugs_cpu = pm->smi_on_cpu_unplug,
1501 build_cpus_aml(dsdt, machine, opts, pm->cpu_hp_io_base,
1502 "\\_SB.PCI0", "\\_GPE._E02");
1505 if (pcms->memhp_io_base && nr_mem) {
1506 build_memory_hotplug_aml(dsdt, nr_mem, "\\_SB.PCI0",
1507 "\\_GPE._E03", AML_SYSTEM_IO,
1508 pcms->memhp_io_base);
1511 scope = aml_scope("_GPE");
1513 aml_append(scope, aml_name_decl("_HID", aml_string("ACPI0006")));
1515 if (pm->pcihp_bridge_en || pm->pcihp_root_en) {
1516 method = aml_method("_E01", 0, AML_NOTSERIALIZED);
1517 aml_append(method,
1518 aml_acquire(aml_name("\\_SB.PCI0.BLCK"), 0xFFFF));
1519 aml_append(method, aml_call0("\\_SB.PCI0.PCNT"));
1520 aml_append(method, aml_release(aml_name("\\_SB.PCI0.BLCK")));
1521 aml_append(scope, method);
1524 if (machine->nvdimms_state->is_enabled) {
1525 method = aml_method("_E04", 0, AML_NOTSERIALIZED);
1526 aml_append(method, aml_notify(aml_name("\\_SB.NVDR"),
1527 aml_int(0x80)));
1528 aml_append(scope, method);
1531 aml_append(dsdt, scope);
1533 crs_range_set_init(&crs_range_set);
1534 bus = PC_MACHINE(machine)->bus;
1535 if (bus) {
1536 QLIST_FOREACH(bus, &bus->child, sibling) {
1537 uint8_t bus_num = pci_bus_num(bus);
1538 uint8_t numa_node = pci_bus_numa_node(bus);
1540 /* look only for expander root buses */
1541 if (!pci_bus_is_root(bus)) {
1542 continue;
1545 if (bus_num < root_bus_limit) {
1546 root_bus_limit = bus_num - 1;
1549 scope = aml_scope("\\_SB");
1550 dev = aml_device("PC%.02X", bus_num);
1551 aml_append(dev, aml_name_decl("_UID", aml_int(bus_num)));
1552 aml_append(dev, aml_name_decl("_BBN", aml_int(bus_num)));
1553 if (pci_bus_is_express(bus)) {
1554 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A08")));
1555 aml_append(dev, aml_name_decl("_CID", aml_eisaid("PNP0A03")));
1556 aml_append(dev, build_q35_osc_method());
1557 } else {
1558 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A03")));
1561 if (numa_node != NUMA_NODE_UNASSIGNED) {
1562 aml_append(dev, aml_name_decl("_PXM", aml_int(numa_node)));
1565 aml_append(dev, build_prt(false));
1566 crs = build_crs(PCI_HOST_BRIDGE(BUS(bus)->parent), &crs_range_set,
1567 0, 0, 0, 0);
1568 aml_append(dev, aml_name_decl("_CRS", crs));
1569 aml_append(scope, dev);
1570 aml_append(dsdt, scope);
1575 * At this point crs_range_set has all the ranges used by pci
1576 * busses *other* than PCI0. These ranges will be excluded from
1577 * the PCI0._CRS. Add mmconfig to the set so it will be excluded
1578 * too.
1580 if (mcfg_valid) {
1581 crs_range_insert(crs_range_set.mem_ranges,
1582 mcfg.base, mcfg.base + mcfg.size - 1);
1585 scope = aml_scope("\\_SB.PCI0");
1586 /* build PCI0._CRS */
1587 crs = aml_resource_template();
1588 aml_append(crs,
1589 aml_word_bus_number(AML_MIN_FIXED, AML_MAX_FIXED, AML_POS_DECODE,
1590 0x0000, 0x0, root_bus_limit,
1591 0x0000, root_bus_limit + 1));
1592 aml_append(crs, aml_io(AML_DECODE16, 0x0CF8, 0x0CF8, 0x01, 0x08));
1594 aml_append(crs,
1595 aml_word_io(AML_MIN_FIXED, AML_MAX_FIXED,
1596 AML_POS_DECODE, AML_ENTIRE_RANGE,
1597 0x0000, 0x0000, 0x0CF7, 0x0000, 0x0CF8));
1599 crs_replace_with_free_ranges(crs_range_set.io_ranges, 0x0D00, 0xFFFF);
1600 for (i = 0; i < crs_range_set.io_ranges->len; i++) {
1601 entry = g_ptr_array_index(crs_range_set.io_ranges, i);
1602 aml_append(crs,
1603 aml_word_io(AML_MIN_FIXED, AML_MAX_FIXED,
1604 AML_POS_DECODE, AML_ENTIRE_RANGE,
1605 0x0000, entry->base, entry->limit,
1606 0x0000, entry->limit - entry->base + 1));
1609 aml_append(crs,
1610 aml_dword_memory(AML_POS_DECODE, AML_MIN_FIXED, AML_MAX_FIXED,
1611 AML_CACHEABLE, AML_READ_WRITE,
1612 0, 0x000A0000, 0x000BFFFF, 0, 0x00020000));
1614 crs_replace_with_free_ranges(crs_range_set.mem_ranges,
1615 range_lob(pci_hole),
1616 range_upb(pci_hole));
1617 for (i = 0; i < crs_range_set.mem_ranges->len; i++) {
1618 entry = g_ptr_array_index(crs_range_set.mem_ranges, i);
1619 aml_append(crs,
1620 aml_dword_memory(AML_POS_DECODE, AML_MIN_FIXED, AML_MAX_FIXED,
1621 AML_NON_CACHEABLE, AML_READ_WRITE,
1622 0, entry->base, entry->limit,
1623 0, entry->limit - entry->base + 1));
1626 if (!range_is_empty(pci_hole64)) {
1627 crs_replace_with_free_ranges(crs_range_set.mem_64bit_ranges,
1628 range_lob(pci_hole64),
1629 range_upb(pci_hole64));
1630 for (i = 0; i < crs_range_set.mem_64bit_ranges->len; i++) {
1631 entry = g_ptr_array_index(crs_range_set.mem_64bit_ranges, i);
1632 aml_append(crs,
1633 aml_qword_memory(AML_POS_DECODE, AML_MIN_FIXED,
1634 AML_MAX_FIXED,
1635 AML_CACHEABLE, AML_READ_WRITE,
1636 0, entry->base, entry->limit,
1637 0, entry->limit - entry->base + 1));
1641 #ifdef CONFIG_TPM
1642 if (TPM_IS_TIS_ISA(tpm_find())) {
1643 aml_append(crs, aml_memory32_fixed(TPM_TIS_ADDR_BASE,
1644 TPM_TIS_ADDR_SIZE, AML_READ_WRITE));
1646 #endif
1647 aml_append(scope, aml_name_decl("_CRS", crs));
1649 /* reserve GPE0 block resources */
1650 dev = aml_device("GPE0");
1651 aml_append(dev, aml_name_decl("_HID", aml_string("PNP0A06")));
1652 aml_append(dev, aml_name_decl("_UID", aml_string("GPE0 resources")));
1653 /* device present, functioning, decoding, not shown in UI */
1654 aml_append(dev, aml_name_decl("_STA", aml_int(0xB)));
1655 crs = aml_resource_template();
1656 aml_append(crs,
1657 aml_io(
1658 AML_DECODE16,
1659 pm->fadt.gpe0_blk.address,
1660 pm->fadt.gpe0_blk.address,
1662 pm->fadt.gpe0_blk.bit_width / 8)
1664 aml_append(dev, aml_name_decl("_CRS", crs));
1665 aml_append(scope, dev);
1667 crs_range_set_free(&crs_range_set);
1669 /* reserve PCIHP resources */
1670 if (pm->pcihp_io_len && (pm->pcihp_bridge_en || pm->pcihp_root_en)) {
1671 dev = aml_device("PHPR");
1672 aml_append(dev, aml_name_decl("_HID", aml_string("PNP0A06")));
1673 aml_append(dev,
1674 aml_name_decl("_UID", aml_string("PCI Hotplug resources")));
1675 /* device present, functioning, decoding, not shown in UI */
1676 aml_append(dev, aml_name_decl("_STA", aml_int(0xB)));
1677 crs = aml_resource_template();
1678 aml_append(crs,
1679 aml_io(AML_DECODE16, pm->pcihp_io_base, pm->pcihp_io_base, 1,
1680 pm->pcihp_io_len)
1682 aml_append(dev, aml_name_decl("_CRS", crs));
1683 aml_append(scope, dev);
1685 aml_append(dsdt, scope);
1687 /* create S3_ / S4_ / S5_ packages if necessary */
1688 scope = aml_scope("\\");
1689 if (!pm->s3_disabled) {
1690 pkg = aml_package(4);
1691 aml_append(pkg, aml_int(1)); /* PM1a_CNT.SLP_TYP */
1692 aml_append(pkg, aml_int(1)); /* PM1b_CNT.SLP_TYP, FIXME: not impl. */
1693 aml_append(pkg, aml_int(0)); /* reserved */
1694 aml_append(pkg, aml_int(0)); /* reserved */
1695 aml_append(scope, aml_name_decl("_S3", pkg));
1698 if (!pm->s4_disabled) {
1699 pkg = aml_package(4);
1700 aml_append(pkg, aml_int(pm->s4_val)); /* PM1a_CNT.SLP_TYP */
1701 /* PM1b_CNT.SLP_TYP, FIXME: not impl. */
1702 aml_append(pkg, aml_int(pm->s4_val));
1703 aml_append(pkg, aml_int(0)); /* reserved */
1704 aml_append(pkg, aml_int(0)); /* reserved */
1705 aml_append(scope, aml_name_decl("_S4", pkg));
1708 pkg = aml_package(4);
1709 aml_append(pkg, aml_int(0)); /* PM1a_CNT.SLP_TYP */
1710 aml_append(pkg, aml_int(0)); /* PM1b_CNT.SLP_TYP not impl. */
1711 aml_append(pkg, aml_int(0)); /* reserved */
1712 aml_append(pkg, aml_int(0)); /* reserved */
1713 aml_append(scope, aml_name_decl("_S5", pkg));
1714 aml_append(dsdt, scope);
1716 /* create fw_cfg node, unconditionally */
1718 scope = aml_scope("\\_SB.PCI0");
1719 fw_cfg_add_acpi_dsdt(scope, x86ms->fw_cfg);
1720 aml_append(dsdt, scope);
1723 if (misc->applesmc_io_base) {
1724 scope = aml_scope("\\_SB.PCI0.ISA");
1725 dev = aml_device("SMC");
1727 aml_append(dev, aml_name_decl("_HID", aml_eisaid("APP0001")));
1728 /* device present, functioning, decoding, not shown in UI */
1729 aml_append(dev, aml_name_decl("_STA", aml_int(0xB)));
1731 crs = aml_resource_template();
1732 aml_append(crs,
1733 aml_io(AML_DECODE16, misc->applesmc_io_base, misc->applesmc_io_base,
1734 0x01, APPLESMC_MAX_DATA_LENGTH)
1736 aml_append(crs, aml_irq_no_flags(6));
1737 aml_append(dev, aml_name_decl("_CRS", crs));
1739 aml_append(scope, dev);
1740 aml_append(dsdt, scope);
1743 if (misc->pvpanic_port) {
1744 scope = aml_scope("\\_SB.PCI0.ISA");
1746 dev = aml_device("PEVT");
1747 aml_append(dev, aml_name_decl("_HID", aml_string("QEMU0001")));
1749 crs = aml_resource_template();
1750 aml_append(crs,
1751 aml_io(AML_DECODE16, misc->pvpanic_port, misc->pvpanic_port, 1, 1)
1753 aml_append(dev, aml_name_decl("_CRS", crs));
1755 aml_append(dev, aml_operation_region("PEOR", AML_SYSTEM_IO,
1756 aml_int(misc->pvpanic_port), 1));
1757 field = aml_field("PEOR", AML_BYTE_ACC, AML_NOLOCK, AML_PRESERVE);
1758 aml_append(field, aml_named_field("PEPT", 8));
1759 aml_append(dev, field);
1761 /* device present, functioning, decoding, shown in UI */
1762 aml_append(dev, aml_name_decl("_STA", aml_int(0xF)));
1764 method = aml_method("RDPT", 0, AML_NOTSERIALIZED);
1765 aml_append(method, aml_store(aml_name("PEPT"), aml_local(0)));
1766 aml_append(method, aml_return(aml_local(0)));
1767 aml_append(dev, method);
1769 method = aml_method("WRPT", 1, AML_NOTSERIALIZED);
1770 aml_append(method, aml_store(aml_arg(0), aml_name("PEPT")));
1771 aml_append(dev, method);
1773 aml_append(scope, dev);
1774 aml_append(dsdt, scope);
1777 sb_scope = aml_scope("\\_SB");
1779 Object *pci_host;
1780 PCIBus *bus = NULL;
1782 pci_host = acpi_get_i386_pci_host();
1784 if (pci_host) {
1785 bus = PCI_HOST_BRIDGE(pci_host)->bus;
1788 if (bus) {
1789 Aml *scope = aml_scope("PCI0");
1790 /* Scan all PCI buses. Generate tables to support hotplug. */
1791 build_append_pci_bus_devices(scope, bus, pm->pcihp_bridge_en);
1793 #ifdef CONFIG_TPM
1794 if (TPM_IS_TIS_ISA(tpm)) {
1795 if (misc->tpm_version == TPM_VERSION_2_0) {
1796 dev = aml_device("TPM");
1797 aml_append(dev, aml_name_decl("_HID",
1798 aml_string("MSFT0101")));
1799 } else {
1800 dev = aml_device("ISA.TPM");
1801 aml_append(dev, aml_name_decl("_HID",
1802 aml_eisaid("PNP0C31")));
1805 aml_append(dev, aml_name_decl("_STA", aml_int(0xF)));
1806 crs = aml_resource_template();
1807 aml_append(crs, aml_memory32_fixed(TPM_TIS_ADDR_BASE,
1808 TPM_TIS_ADDR_SIZE, AML_READ_WRITE));
1810 FIXME: TPM_TIS_IRQ=5 conflicts with PNP0C0F irqs,
1811 Rewrite to take IRQ from TPM device model and
1812 fix default IRQ value there to use some unused IRQ
1814 /* aml_append(crs, aml_irq_no_flags(TPM_TIS_IRQ)); */
1815 aml_append(dev, aml_name_decl("_CRS", crs));
1817 tpm_build_ppi_acpi(tpm, dev);
1819 aml_append(scope, dev);
1821 #endif
1823 aml_append(sb_scope, scope);
1827 #ifdef CONFIG_TPM
1828 if (TPM_IS_CRB(tpm)) {
1829 dev = aml_device("TPM");
1830 aml_append(dev, aml_name_decl("_HID", aml_string("MSFT0101")));
1831 crs = aml_resource_template();
1832 aml_append(crs, aml_memory32_fixed(TPM_CRB_ADDR_BASE,
1833 TPM_CRB_ADDR_SIZE, AML_READ_WRITE));
1834 aml_append(dev, aml_name_decl("_CRS", crs));
1836 aml_append(dev, aml_name_decl("_STA", aml_int(0xf)));
1838 tpm_build_ppi_acpi(tpm, dev);
1840 aml_append(sb_scope, dev);
1842 #endif
1844 if (pcms->sgx_epc.size != 0) {
1845 uint64_t epc_base = pcms->sgx_epc.base;
1846 uint64_t epc_size = pcms->sgx_epc.size;
1848 dev = aml_device("EPC");
1849 aml_append(dev, aml_name_decl("_HID", aml_eisaid("INT0E0C")));
1850 aml_append(dev, aml_name_decl("_STR",
1851 aml_unicode("Enclave Page Cache 1.0")));
1852 crs = aml_resource_template();
1853 aml_append(crs,
1854 aml_qword_memory(AML_POS_DECODE, AML_MIN_FIXED,
1855 AML_MAX_FIXED, AML_NON_CACHEABLE,
1856 AML_READ_WRITE, 0, epc_base,
1857 epc_base + epc_size - 1, 0, epc_size));
1858 aml_append(dev, aml_name_decl("_CRS", crs));
1860 method = aml_method("_STA", 0, AML_NOTSERIALIZED);
1861 aml_append(method, aml_return(aml_int(0x0f)));
1862 aml_append(dev, method);
1864 aml_append(sb_scope, dev);
1866 aml_append(dsdt, sb_scope);
1868 /* copy AML table into ACPI tables blob and patch header there */
1869 g_array_append_vals(table_data, dsdt->buf->data, dsdt->buf->len);
1870 acpi_table_end(linker, &table);
1871 free_aml_allocator();
1875 * IA-PC HPET (High Precision Event Timers) Specification (Revision: 1.0a)
1876 * 3.2.4The ACPI 2.0 HPET Description Table (HPET)
1878 static void
1879 build_hpet(GArray *table_data, BIOSLinker *linker, const char *oem_id,
1880 const char *oem_table_id)
1882 AcpiTable table = { .sig = "HPET", .rev = 1,
1883 .oem_id = oem_id, .oem_table_id = oem_table_id };
1885 acpi_table_begin(&table, table_data);
1886 /* Note timer_block_id value must be kept in sync with value advertised by
1887 * emulated hpet
1889 /* Event Timer Block ID */
1890 build_append_int_noprefix(table_data, 0x8086a201, 4);
1891 /* BASE_ADDRESS */
1892 build_append_gas(table_data, AML_AS_SYSTEM_MEMORY, 0, 0, 0, HPET_BASE);
1893 /* HPET Number */
1894 build_append_int_noprefix(table_data, 0, 1);
1895 /* Main Counter Minimum Clock_tick in Periodic Mode */
1896 build_append_int_noprefix(table_data, 0, 2);
1897 /* Page Protection And OEM Attribute */
1898 build_append_int_noprefix(table_data, 0, 1);
1899 acpi_table_end(linker, &table);
1902 #ifdef CONFIG_TPM
1904 * TCPA Description Table
1906 * Following Level 00, Rev 00.37 of specs:
1907 * http://www.trustedcomputinggroup.org/resources/tcg_acpi_specification
1908 * 7.1.2 ACPI Table Layout
1910 static void
1911 build_tpm_tcpa(GArray *table_data, BIOSLinker *linker, GArray *tcpalog,
1912 const char *oem_id, const char *oem_table_id)
1914 unsigned log_addr_offset;
1915 AcpiTable table = { .sig = "TCPA", .rev = 2,
1916 .oem_id = oem_id, .oem_table_id = oem_table_id };
1918 acpi_table_begin(&table, table_data);
1919 /* Platform Class */
1920 build_append_int_noprefix(table_data, TPM_TCPA_ACPI_CLASS_CLIENT, 2);
1921 /* Log Area Minimum Length (LAML) */
1922 build_append_int_noprefix(table_data, TPM_LOG_AREA_MINIMUM_SIZE, 4);
1923 /* Log Area Start Address (LASA) */
1924 log_addr_offset = table_data->len;
1925 build_append_int_noprefix(table_data, 0, 8);
1927 /* allocate/reserve space for TPM log area */
1928 acpi_data_push(tcpalog, TPM_LOG_AREA_MINIMUM_SIZE);
1929 bios_linker_loader_alloc(linker, ACPI_BUILD_TPMLOG_FILE, tcpalog, 1,
1930 false /* high memory */);
1931 /* log area start address to be filled by Guest linker */
1932 bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
1933 log_addr_offset, 8, ACPI_BUILD_TPMLOG_FILE, 0);
1935 acpi_table_end(linker, &table);
1937 #endif
1939 #define HOLE_640K_START (640 * KiB)
1940 #define HOLE_640K_END (1 * MiB)
1943 * ACPI spec, Revision 3.0
1944 * 5.2.15 System Resource Affinity Table (SRAT)
1946 static void
1947 build_srat(GArray *table_data, BIOSLinker *linker, MachineState *machine)
1949 int i;
1950 int numa_mem_start, slots;
1951 uint64_t mem_len, mem_base, next_base;
1952 MachineClass *mc = MACHINE_GET_CLASS(machine);
1953 X86MachineState *x86ms = X86_MACHINE(machine);
1954 const CPUArchIdList *apic_ids = mc->possible_cpu_arch_ids(machine);
1955 PCMachineState *pcms = PC_MACHINE(machine);
1956 int nb_numa_nodes = machine->numa_state->num_nodes;
1957 NodeInfo *numa_info = machine->numa_state->nodes;
1958 ram_addr_t hotpluggable_address_space_size =
1959 object_property_get_int(OBJECT(pcms), PC_MACHINE_DEVMEM_REGION_SIZE,
1960 NULL);
1961 AcpiTable table = { .sig = "SRAT", .rev = 1, .oem_id = x86ms->oem_id,
1962 .oem_table_id = x86ms->oem_table_id };
1964 acpi_table_begin(&table, table_data);
1965 build_append_int_noprefix(table_data, 1, 4); /* Reserved */
1966 build_append_int_noprefix(table_data, 0, 8); /* Reserved */
1968 for (i = 0; i < apic_ids->len; i++) {
1969 int node_id = apic_ids->cpus[i].props.node_id;
1970 uint32_t apic_id = apic_ids->cpus[i].arch_id;
1972 if (apic_id < 255) {
1973 /* 5.2.15.1 Processor Local APIC/SAPIC Affinity Structure */
1974 build_append_int_noprefix(table_data, 0, 1); /* Type */
1975 build_append_int_noprefix(table_data, 16, 1); /* Length */
1976 /* Proximity Domain [7:0] */
1977 build_append_int_noprefix(table_data, node_id, 1);
1978 build_append_int_noprefix(table_data, apic_id, 1); /* APIC ID */
1979 /* Flags, Table 5-36 */
1980 build_append_int_noprefix(table_data, 1, 4);
1981 build_append_int_noprefix(table_data, 0, 1); /* Local SAPIC EID */
1982 /* Proximity Domain [31:8] */
1983 build_append_int_noprefix(table_data, 0, 3);
1984 build_append_int_noprefix(table_data, 0, 4); /* Reserved */
1985 } else {
1987 * ACPI spec, Revision 4.0
1988 * 5.2.16.3 Processor Local x2APIC Affinity Structure
1990 build_append_int_noprefix(table_data, 2, 1); /* Type */
1991 build_append_int_noprefix(table_data, 24, 1); /* Length */
1992 build_append_int_noprefix(table_data, 0, 2); /* Reserved */
1993 /* Proximity Domain */
1994 build_append_int_noprefix(table_data, node_id, 4);
1995 build_append_int_noprefix(table_data, apic_id, 4); /* X2APIC ID */
1996 /* Flags, Table 5-39 */
1997 build_append_int_noprefix(table_data, 1 /* Enabled */, 4);
1998 build_append_int_noprefix(table_data, 0, 4); /* Clock Domain */
1999 build_append_int_noprefix(table_data, 0, 4); /* Reserved */
2003 /* the memory map is a bit tricky, it contains at least one hole
2004 * from 640k-1M and possibly another one from 3.5G-4G.
2006 next_base = 0;
2007 numa_mem_start = table_data->len;
2009 for (i = 1; i < nb_numa_nodes + 1; ++i) {
2010 mem_base = next_base;
2011 mem_len = numa_info[i - 1].node_mem;
2012 next_base = mem_base + mem_len;
2014 /* Cut out the 640K hole */
2015 if (mem_base <= HOLE_640K_START &&
2016 next_base > HOLE_640K_START) {
2017 mem_len -= next_base - HOLE_640K_START;
2018 if (mem_len > 0) {
2019 build_srat_memory(table_data, mem_base, mem_len, i - 1,
2020 MEM_AFFINITY_ENABLED);
2023 /* Check for the rare case: 640K < RAM < 1M */
2024 if (next_base <= HOLE_640K_END) {
2025 next_base = HOLE_640K_END;
2026 continue;
2028 mem_base = HOLE_640K_END;
2029 mem_len = next_base - HOLE_640K_END;
2032 /* Cut out the ACPI_PCI hole */
2033 if (mem_base <= x86ms->below_4g_mem_size &&
2034 next_base > x86ms->below_4g_mem_size) {
2035 mem_len -= next_base - x86ms->below_4g_mem_size;
2036 if (mem_len > 0) {
2037 build_srat_memory(table_data, mem_base, mem_len, i - 1,
2038 MEM_AFFINITY_ENABLED);
2040 mem_base = 1ULL << 32;
2041 mem_len = next_base - x86ms->below_4g_mem_size;
2042 next_base = mem_base + mem_len;
2045 if (mem_len > 0) {
2046 build_srat_memory(table_data, mem_base, mem_len, i - 1,
2047 MEM_AFFINITY_ENABLED);
2051 if (machine->nvdimms_state->is_enabled) {
2052 nvdimm_build_srat(table_data);
2056 * TODO: this part is not in ACPI spec and current linux kernel boots fine
2057 * without these entries. But I recall there were issues the last time I
2058 * tried to remove it with some ancient guest OS, however I can't remember
2059 * what that was so keep this around for now
2061 slots = (table_data->len - numa_mem_start) / 40 /* mem affinity len */;
2062 for (; slots < nb_numa_nodes + 2; slots++) {
2063 build_srat_memory(table_data, 0, 0, 0, MEM_AFFINITY_NOFLAGS);
2067 * Entry is required for Windows to enable memory hotplug in OS
2068 * and for Linux to enable SWIOTLB when booted with less than
2069 * 4G of RAM. Windows works better if the entry sets proximity
2070 * to the highest NUMA node in the machine.
2071 * Memory devices may override proximity set by this entry,
2072 * providing _PXM method if necessary.
2074 if (hotpluggable_address_space_size) {
2075 build_srat_memory(table_data, machine->device_memory->base,
2076 hotpluggable_address_space_size, nb_numa_nodes - 1,
2077 MEM_AFFINITY_HOTPLUGGABLE | MEM_AFFINITY_ENABLED);
2080 acpi_table_end(linker, &table);
2084 * Insert DMAR scope for PCI bridges and endpoint devcie
2086 static void
2087 insert_scope(PCIBus *bus, PCIDevice *dev, void *opaque)
2089 GArray *scope_blob = opaque;
2090 AcpiDmarDeviceScope *scope = NULL;
2092 if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_BRIDGE)) {
2093 /* Dmar Scope Type: 0x02 for PCI Bridge */
2094 build_append_int_noprefix(scope_blob, 0x02, 1);
2095 } else {
2096 /* Dmar Scope Type: 0x01 for PCI Endpoint Device */
2097 build_append_int_noprefix(scope_blob, 0x01, 1);
2100 /* length */
2101 build_append_int_noprefix(scope_blob,
2102 sizeof(*scope) + sizeof(scope->path[0]), 1);
2103 /* reserved */
2104 build_append_int_noprefix(scope_blob, 0, 2);
2105 /* enumeration_id */
2106 build_append_int_noprefix(scope_blob, 0, 1);
2107 /* bus */
2108 build_append_int_noprefix(scope_blob, pci_bus_num(bus), 1);
2109 /* device */
2110 build_append_int_noprefix(scope_blob, PCI_SLOT(dev->devfn), 1);
2111 /* function */
2112 build_append_int_noprefix(scope_blob, PCI_FUNC(dev->devfn), 1);
2115 /* For a given PCI host bridge, walk and insert DMAR scope */
2116 static int
2117 dmar_host_bridges(Object *obj, void *opaque)
2119 GArray *scope_blob = opaque;
2121 if (object_dynamic_cast(obj, TYPE_PCI_HOST_BRIDGE)) {
2122 PCIBus *bus = PCI_HOST_BRIDGE(obj)->bus;
2124 if (bus && !pci_bus_bypass_iommu(bus)) {
2125 pci_for_each_device(bus, pci_bus_num(bus), insert_scope,
2126 scope_blob);
2130 return 0;
2134 * VT-d spec 8.1 DMA Remapping Reporting Structure
2135 * (version Oct. 2014 or later)
2137 static void
2138 build_dmar_q35(GArray *table_data, BIOSLinker *linker, const char *oem_id,
2139 const char *oem_table_id)
2141 int dmar_start = table_data->len;
2143 AcpiTableDmar *dmar;
2144 AcpiDmarHardwareUnit *drhd;
2145 AcpiDmarRootPortATS *atsr;
2146 uint8_t dmar_flags = 0;
2147 X86IOMMUState *iommu = x86_iommu_get_default();
2148 AcpiDmarDeviceScope *scope = NULL;
2149 /* Root complex IOAPIC use one path[0] only */
2150 size_t ioapic_scope_size = sizeof(*scope) + sizeof(scope->path[0]);
2151 IntelIOMMUState *intel_iommu = INTEL_IOMMU_DEVICE(iommu);
2152 GArray *scope_blob = g_array_new(false, true, 1);
2155 * A PCI bus walk, for each PCI host bridge.
2156 * Insert scope for each PCI bridge and endpoint device which
2157 * is attached to a bus with iommu enabled.
2159 object_child_foreach_recursive(object_get_root(),
2160 dmar_host_bridges, scope_blob);
2162 assert(iommu);
2163 if (x86_iommu_ir_supported(iommu)) {
2164 dmar_flags |= 0x1; /* Flags: 0x1: INT_REMAP */
2167 dmar = acpi_data_push(table_data, sizeof(*dmar));
2168 dmar->host_address_width = intel_iommu->aw_bits - 1;
2169 dmar->flags = dmar_flags;
2171 /* DMAR Remapping Hardware Unit Definition structure */
2172 drhd = acpi_data_push(table_data, sizeof(*drhd) + ioapic_scope_size);
2173 drhd->type = cpu_to_le16(ACPI_DMAR_TYPE_HARDWARE_UNIT);
2174 drhd->length =
2175 cpu_to_le16(sizeof(*drhd) + ioapic_scope_size + scope_blob->len);
2176 drhd->flags = 0; /* Don't include all pci device */
2177 drhd->pci_segment = cpu_to_le16(0);
2178 drhd->address = cpu_to_le64(Q35_HOST_BRIDGE_IOMMU_ADDR);
2180 /* Scope definition for the root-complex IOAPIC. See VT-d spec
2181 * 8.3.1 (version Oct. 2014 or later). */
2182 scope = &drhd->scope[0];
2183 scope->entry_type = 0x03; /* Type: 0x03 for IOAPIC */
2184 scope->length = ioapic_scope_size;
2185 scope->enumeration_id = ACPI_BUILD_IOAPIC_ID;
2186 scope->bus = Q35_PSEUDO_BUS_PLATFORM;
2187 scope->path[0].device = PCI_SLOT(Q35_PSEUDO_DEVFN_IOAPIC);
2188 scope->path[0].function = PCI_FUNC(Q35_PSEUDO_DEVFN_IOAPIC);
2190 /* Add scope found above */
2191 g_array_append_vals(table_data, scope_blob->data, scope_blob->len);
2192 g_array_free(scope_blob, true);
2194 if (iommu->dt_supported) {
2195 atsr = acpi_data_push(table_data, sizeof(*atsr));
2196 atsr->type = cpu_to_le16(ACPI_DMAR_TYPE_ATSR);
2197 atsr->length = cpu_to_le16(sizeof(*atsr));
2198 atsr->flags = ACPI_DMAR_ATSR_ALL_PORTS;
2199 atsr->pci_segment = cpu_to_le16(0);
2202 build_header(linker, table_data, (void *)(table_data->data + dmar_start),
2203 "DMAR", table_data->len - dmar_start, 1, oem_id, oem_table_id);
2207 * Windows ACPI Emulated Devices Table
2208 * (Version 1.0 - April 6, 2009)
2209 * Spec: http://download.microsoft.com/download/7/E/7/7E7662CF-CBEA-470B-A97E-CE7CE0D98DC2/WAET.docx
2211 * Helpful to speedup Windows guests and ignored by others.
2213 static void
2214 build_waet(GArray *table_data, BIOSLinker *linker, const char *oem_id,
2215 const char *oem_table_id)
2217 int waet_start = table_data->len;
2219 /* WAET header */
2220 acpi_data_push(table_data, sizeof(AcpiTableHeader));
2222 * Set "ACPI PM timer good" flag.
2224 * Tells Windows guests that our ACPI PM timer is reliable in the
2225 * sense that guest can read it only once to obtain a reliable value.
2226 * Which avoids costly VMExits caused by guest re-reading it unnecessarily.
2228 build_append_int_noprefix(table_data, 1 << 1 /* ACPI PM timer good */, 4);
2230 build_header(linker, table_data, (void *)(table_data->data + waet_start),
2231 "WAET", table_data->len - waet_start, 1, oem_id, oem_table_id);
2235 * IVRS table as specified in AMD IOMMU Specification v2.62, Section 5.2
2236 * accessible here http://support.amd.com/TechDocs/48882_IOMMU.pdf
2238 #define IOAPIC_SB_DEVID (uint64_t)PCI_BUILD_BDF(0, PCI_DEVFN(0x14, 0))
2241 * Insert IVHD entry for device and recurse, insert alias, or insert range as
2242 * necessary for the PCI topology.
2244 static void
2245 insert_ivhd(PCIBus *bus, PCIDevice *dev, void *opaque)
2247 GArray *table_data = opaque;
2248 uint32_t entry;
2250 /* "Select" IVHD entry, type 0x2 */
2251 entry = PCI_BUILD_BDF(pci_bus_num(bus), dev->devfn) << 8 | 0x2;
2252 build_append_int_noprefix(table_data, entry, 4);
2254 if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_BRIDGE)) {
2255 PCIBus *sec_bus = pci_bridge_get_sec_bus(PCI_BRIDGE(dev));
2256 uint8_t sec = pci_bus_num(sec_bus);
2257 uint8_t sub = dev->config[PCI_SUBORDINATE_BUS];
2259 if (pci_bus_is_express(sec_bus)) {
2261 * Walk the bus if there are subordinates, otherwise use a range
2262 * to cover an entire leaf bus. We could potentially also use a
2263 * range for traversed buses, but we'd need to take care not to
2264 * create both Select and Range entries covering the same device.
2265 * This is easier and potentially more compact.
2267 * An example bare metal system seems to use Select entries for
2268 * root ports without a slot (ie. built-ins) and Range entries
2269 * when there is a slot. The same system also only hard-codes
2270 * the alias range for an onboard PCIe-to-PCI bridge, apparently
2271 * making no effort to support nested bridges. We attempt to
2272 * be more thorough here.
2274 if (sec == sub) { /* leaf bus */
2275 /* "Start of Range" IVHD entry, type 0x3 */
2276 entry = PCI_BUILD_BDF(sec, PCI_DEVFN(0, 0)) << 8 | 0x3;
2277 build_append_int_noprefix(table_data, entry, 4);
2278 /* "End of Range" IVHD entry, type 0x4 */
2279 entry = PCI_BUILD_BDF(sub, PCI_DEVFN(31, 7)) << 8 | 0x4;
2280 build_append_int_noprefix(table_data, entry, 4);
2281 } else {
2282 pci_for_each_device(sec_bus, sec, insert_ivhd, table_data);
2284 } else {
2286 * If the secondary bus is conventional, then we need to create an
2287 * Alias range for everything downstream. The range covers the
2288 * first devfn on the secondary bus to the last devfn on the
2289 * subordinate bus. The alias target depends on legacy versus
2290 * express bridges, just as in pci_device_iommu_address_space().
2291 * DeviceIDa vs DeviceIDb as per the AMD IOMMU spec.
2293 uint16_t dev_id_a, dev_id_b;
2295 dev_id_a = PCI_BUILD_BDF(sec, PCI_DEVFN(0, 0));
2297 if (pci_is_express(dev) &&
2298 pcie_cap_get_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE) {
2299 dev_id_b = dev_id_a;
2300 } else {
2301 dev_id_b = PCI_BUILD_BDF(pci_bus_num(bus), dev->devfn);
2304 /* "Alias Start of Range" IVHD entry, type 0x43, 8 bytes */
2305 build_append_int_noprefix(table_data, dev_id_a << 8 | 0x43, 4);
2306 build_append_int_noprefix(table_data, dev_id_b << 8 | 0x0, 4);
2308 /* "End of Range" IVHD entry, type 0x4 */
2309 entry = PCI_BUILD_BDF(sub, PCI_DEVFN(31, 7)) << 8 | 0x4;
2310 build_append_int_noprefix(table_data, entry, 4);
2315 /* For all PCI host bridges, walk and insert IVHD entries */
2316 static int
2317 ivrs_host_bridges(Object *obj, void *opaque)
2319 GArray *ivhd_blob = opaque;
2321 if (object_dynamic_cast(obj, TYPE_PCI_HOST_BRIDGE)) {
2322 PCIBus *bus = PCI_HOST_BRIDGE(obj)->bus;
2324 if (bus && !pci_bus_bypass_iommu(bus)) {
2325 pci_for_each_device(bus, pci_bus_num(bus), insert_ivhd, ivhd_blob);
2329 return 0;
2332 static void
2333 build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,
2334 const char *oem_table_id)
2336 int ivhd_table_len = 24;
2337 int iommu_start = table_data->len;
2338 AMDVIState *s = AMD_IOMMU_DEVICE(x86_iommu_get_default());
2339 GArray *ivhd_blob = g_array_new(false, true, 1);
2341 /* IVRS header */
2342 acpi_data_push(table_data, sizeof(AcpiTableHeader));
2343 /* IVinfo - IO virtualization information common to all
2344 * IOMMU units in a system
2346 build_append_int_noprefix(table_data, 40UL << 8/* PASize */, 4);
2347 /* reserved */
2348 build_append_int_noprefix(table_data, 0, 8);
2350 /* IVHD definition - type 10h */
2351 build_append_int_noprefix(table_data, 0x10, 1);
2352 /* virtualization flags */
2353 build_append_int_noprefix(table_data,
2354 (1UL << 0) | /* HtTunEn */
2355 (1UL << 4) | /* iotblSup */
2356 (1UL << 6) | /* PrefSup */
2357 (1UL << 7), /* PPRSup */
2361 * A PCI bus walk, for each PCI host bridge, is necessary to create a
2362 * complete set of IVHD entries. Do this into a separate blob so that we
2363 * can calculate the total IVRS table length here and then append the new
2364 * blob further below. Fall back to an entry covering all devices, which
2365 * is sufficient when no aliases are present.
2367 object_child_foreach_recursive(object_get_root(),
2368 ivrs_host_bridges, ivhd_blob);
2370 if (!ivhd_blob->len) {
2372 * Type 1 device entry reporting all devices
2373 * These are 4-byte device entries currently reporting the range of
2374 * Refer to Spec - Table 95:IVHD Device Entry Type Codes(4-byte)
2376 build_append_int_noprefix(ivhd_blob, 0x0000001, 4);
2379 ivhd_table_len += ivhd_blob->len;
2382 * When interrupt remapping is supported, we add a special IVHD device
2383 * for type IO-APIC.
2385 if (x86_iommu_ir_supported(x86_iommu_get_default())) {
2386 ivhd_table_len += 8;
2389 /* IVHD length */
2390 build_append_int_noprefix(table_data, ivhd_table_len, 2);
2391 /* DeviceID */
2392 build_append_int_noprefix(table_data, s->devid, 2);
2393 /* Capability offset */
2394 build_append_int_noprefix(table_data, s->capab_offset, 2);
2395 /* IOMMU base address */
2396 build_append_int_noprefix(table_data, s->mmio.addr, 8);
2397 /* PCI Segment Group */
2398 build_append_int_noprefix(table_data, 0, 2);
2399 /* IOMMU info */
2400 build_append_int_noprefix(table_data, 0, 2);
2401 /* IOMMU Feature Reporting */
2402 build_append_int_noprefix(table_data,
2403 (48UL << 30) | /* HATS */
2404 (48UL << 28) | /* GATS */
2405 (1UL << 2) | /* GTSup */
2406 (1UL << 6), /* GASup */
2409 /* IVHD entries as found above */
2410 g_array_append_vals(table_data, ivhd_blob->data, ivhd_blob->len);
2411 g_array_free(ivhd_blob, TRUE);
2414 * Add a special IVHD device type.
2415 * Refer to spec - Table 95: IVHD device entry type codes
2417 * Linux IOMMU driver checks for the special IVHD device (type IO-APIC).
2418 * See Linux kernel commit 'c2ff5cf5294bcbd7fa50f7d860e90a66db7e5059'
2420 if (x86_iommu_ir_supported(x86_iommu_get_default())) {
2421 build_append_int_noprefix(table_data,
2422 (0x1ull << 56) | /* type IOAPIC */
2423 (IOAPIC_SB_DEVID << 40) | /* IOAPIC devid */
2424 0x48, /* special device */
2428 build_header(linker, table_data, (void *)(table_data->data + iommu_start),
2429 "IVRS", table_data->len - iommu_start, 1, oem_id,
2430 oem_table_id);
2433 typedef
2434 struct AcpiBuildState {
2435 /* Copy of table in RAM (for patching). */
2436 MemoryRegion *table_mr;
2437 /* Is table patched? */
2438 uint8_t patched;
2439 void *rsdp;
2440 MemoryRegion *rsdp_mr;
2441 MemoryRegion *linker_mr;
2442 } AcpiBuildState;
2444 static bool acpi_get_mcfg(AcpiMcfgInfo *mcfg)
2446 Object *pci_host;
2447 QObject *o;
2449 pci_host = acpi_get_i386_pci_host();
2450 if (!pci_host) {
2451 return false;
2454 o = object_property_get_qobject(pci_host, PCIE_HOST_MCFG_BASE, NULL);
2455 if (!o) {
2456 return false;
2458 mcfg->base = qnum_get_uint(qobject_to(QNum, o));
2459 qobject_unref(o);
2460 if (mcfg->base == PCIE_BASE_ADDR_UNMAPPED) {
2461 return false;
2464 o = object_property_get_qobject(pci_host, PCIE_HOST_MCFG_SIZE, NULL);
2465 assert(o);
2466 mcfg->size = qnum_get_uint(qobject_to(QNum, o));
2467 qobject_unref(o);
2468 return true;
2471 static
2472 void acpi_build(AcpiBuildTables *tables, MachineState *machine)
2474 PCMachineState *pcms = PC_MACHINE(machine);
2475 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
2476 X86MachineState *x86ms = X86_MACHINE(machine);
2477 GArray *table_offsets;
2478 unsigned facs, dsdt, rsdt, fadt;
2479 AcpiPmInfo pm;
2480 AcpiMiscInfo misc;
2481 AcpiMcfgInfo mcfg;
2482 Range pci_hole = {}, pci_hole64 = {};
2483 uint8_t *u;
2484 size_t aml_len = 0;
2485 GArray *tables_blob = tables->table_data;
2486 AcpiSlicOem slic_oem = { .id = NULL, .table_id = NULL };
2487 Object *vmgenid_dev;
2488 char *oem_id;
2489 char *oem_table_id;
2491 acpi_get_pm_info(machine, &pm);
2492 acpi_get_misc_info(&misc);
2493 acpi_get_pci_holes(&pci_hole, &pci_hole64);
2494 acpi_get_slic_oem(&slic_oem);
2496 if (slic_oem.id) {
2497 oem_id = slic_oem.id;
2498 } else {
2499 oem_id = x86ms->oem_id;
2502 if (slic_oem.table_id) {
2503 oem_table_id = slic_oem.table_id;
2504 } else {
2505 oem_table_id = x86ms->oem_table_id;
2508 table_offsets = g_array_new(false, true /* clear */,
2509 sizeof(uint32_t));
2510 ACPI_BUILD_DPRINTF("init ACPI tables\n");
2512 bios_linker_loader_alloc(tables->linker,
2513 ACPI_BUILD_TABLE_FILE, tables_blob,
2514 64 /* Ensure FACS is aligned */,
2515 false /* high memory */);
2518 * FACS is pointed to by FADT.
2519 * We place it first since it's the only table that has alignment
2520 * requirements.
2522 facs = tables_blob->len;
2523 build_facs(tables_blob);
2525 /* DSDT is pointed to by FADT */
2526 dsdt = tables_blob->len;
2527 build_dsdt(tables_blob, tables->linker, &pm, &misc,
2528 &pci_hole, &pci_hole64, machine);
2530 /* Count the size of the DSDT and SSDT, we will need it for legacy
2531 * sizing of ACPI tables.
2533 aml_len += tables_blob->len - dsdt;
2535 /* ACPI tables pointed to by RSDT */
2536 fadt = tables_blob->len;
2537 acpi_add_table(table_offsets, tables_blob);
2538 pm.fadt.facs_tbl_offset = &facs;
2539 pm.fadt.dsdt_tbl_offset = &dsdt;
2540 pm.fadt.xdsdt_tbl_offset = &dsdt;
2541 build_fadt(tables_blob, tables->linker, &pm.fadt, oem_id, oem_table_id);
2542 aml_len += tables_blob->len - fadt;
2544 acpi_add_table(table_offsets, tables_blob);
2545 acpi_build_madt(tables_blob, tables->linker, x86ms,
2546 ACPI_DEVICE_IF(x86ms->acpi_dev), x86ms->oem_id,
2547 x86ms->oem_table_id);
2549 vmgenid_dev = find_vmgenid_dev();
2550 if (vmgenid_dev) {
2551 acpi_add_table(table_offsets, tables_blob);
2552 vmgenid_build_acpi(VMGENID(vmgenid_dev), tables_blob,
2553 tables->vmgenid, tables->linker, x86ms->oem_id);
2556 if (misc.has_hpet) {
2557 acpi_add_table(table_offsets, tables_blob);
2558 build_hpet(tables_blob, tables->linker, x86ms->oem_id,
2559 x86ms->oem_table_id);
2561 #ifdef CONFIG_TPM
2562 if (misc.tpm_version != TPM_VERSION_UNSPEC) {
2563 if (misc.tpm_version == TPM_VERSION_1_2) {
2564 acpi_add_table(table_offsets, tables_blob);
2565 build_tpm_tcpa(tables_blob, tables->linker, tables->tcpalog,
2566 x86ms->oem_id, x86ms->oem_table_id);
2567 } else { /* TPM_VERSION_2_0 */
2568 acpi_add_table(table_offsets, tables_blob);
2569 build_tpm2(tables_blob, tables->linker, tables->tcpalog,
2570 x86ms->oem_id, x86ms->oem_table_id);
2573 #endif
2574 if (machine->numa_state->num_nodes) {
2575 acpi_add_table(table_offsets, tables_blob);
2576 build_srat(tables_blob, tables->linker, machine);
2577 if (machine->numa_state->have_numa_distance) {
2578 acpi_add_table(table_offsets, tables_blob);
2579 build_slit(tables_blob, tables->linker, machine, x86ms->oem_id,
2580 x86ms->oem_table_id);
2582 if (machine->numa_state->hmat_enabled) {
2583 acpi_add_table(table_offsets, tables_blob);
2584 build_hmat(tables_blob, tables->linker, machine->numa_state,
2585 x86ms->oem_id, x86ms->oem_table_id);
2588 if (acpi_get_mcfg(&mcfg)) {
2589 acpi_add_table(table_offsets, tables_blob);
2590 build_mcfg(tables_blob, tables->linker, &mcfg, x86ms->oem_id,
2591 x86ms->oem_table_id);
2593 if (x86_iommu_get_default()) {
2594 IommuType IOMMUType = x86_iommu_get_type();
2595 if (IOMMUType == TYPE_AMD) {
2596 acpi_add_table(table_offsets, tables_blob);
2597 build_amd_iommu(tables_blob, tables->linker, x86ms->oem_id,
2598 x86ms->oem_table_id);
2599 } else if (IOMMUType == TYPE_INTEL) {
2600 acpi_add_table(table_offsets, tables_blob);
2601 build_dmar_q35(tables_blob, tables->linker, x86ms->oem_id,
2602 x86ms->oem_table_id);
2605 if (machine->nvdimms_state->is_enabled) {
2606 nvdimm_build_acpi(table_offsets, tables_blob, tables->linker,
2607 machine->nvdimms_state, machine->ram_slots,
2608 x86ms->oem_id, x86ms->oem_table_id);
2611 acpi_add_table(table_offsets, tables_blob);
2612 build_waet(tables_blob, tables->linker, x86ms->oem_id, x86ms->oem_table_id);
2614 /* Add tables supplied by user (if any) */
2615 for (u = acpi_table_first(); u; u = acpi_table_next(u)) {
2616 unsigned len = acpi_table_len(u);
2618 acpi_add_table(table_offsets, tables_blob);
2619 g_array_append_vals(tables_blob, u, len);
2622 /* RSDT is pointed to by RSDP */
2623 rsdt = tables_blob->len;
2624 build_rsdt(tables_blob, tables->linker, table_offsets,
2625 oem_id, oem_table_id);
2627 /* RSDP is in FSEG memory, so allocate it separately */
2629 AcpiRsdpData rsdp_data = {
2630 .revision = 0,
2631 .oem_id = x86ms->oem_id,
2632 .xsdt_tbl_offset = NULL,
2633 .rsdt_tbl_offset = &rsdt,
2635 build_rsdp(tables->rsdp, tables->linker, &rsdp_data);
2636 if (!pcmc->rsdp_in_ram) {
2637 /* We used to allocate some extra space for RSDP revision 2 but
2638 * only used the RSDP revision 0 space. The extra bytes were
2639 * zeroed out and not used.
2640 * Here we continue wasting those extra 16 bytes to make sure we
2641 * don't break migration for machine types 2.2 and older due to
2642 * RSDP blob size mismatch.
2644 build_append_int_noprefix(tables->rsdp, 0, 16);
2648 /* We'll expose it all to Guest so we want to reduce
2649 * chance of size changes.
2651 * We used to align the tables to 4k, but of course this would
2652 * too simple to be enough. 4k turned out to be too small an
2653 * alignment very soon, and in fact it is almost impossible to
2654 * keep the table size stable for all (max_cpus, max_memory_slots)
2655 * combinations. So the table size is always 64k for pc-i440fx-2.1
2656 * and we give an error if the table grows beyond that limit.
2658 * We still have the problem of migrating from "-M pc-i440fx-2.0". For
2659 * that, we exploit the fact that QEMU 2.1 generates _smaller_ tables
2660 * than 2.0 and we can always pad the smaller tables with zeros. We can
2661 * then use the exact size of the 2.0 tables.
2663 * All this is for PIIX4, since QEMU 2.0 didn't support Q35 migration.
2665 if (pcmc->legacy_acpi_table_size) {
2666 /* Subtracting aml_len gives the size of fixed tables. Then add the
2667 * size of the PIIX4 DSDT/SSDT in QEMU 2.0.
2669 int legacy_aml_len =
2670 pcmc->legacy_acpi_table_size +
2671 ACPI_BUILD_LEGACY_CPU_AML_SIZE * x86ms->apic_id_limit;
2672 int legacy_table_size =
2673 ROUND_UP(tables_blob->len - aml_len + legacy_aml_len,
2674 ACPI_BUILD_ALIGN_SIZE);
2675 if (tables_blob->len > legacy_table_size) {
2676 /* Should happen only with PCI bridges and -M pc-i440fx-2.0. */
2677 warn_report("ACPI table size %u exceeds %d bytes,"
2678 " migration may not work",
2679 tables_blob->len, legacy_table_size);
2680 error_printf("Try removing CPUs, NUMA nodes, memory slots"
2681 " or PCI bridges.");
2683 g_array_set_size(tables_blob, legacy_table_size);
2684 } else {
2685 /* Make sure we have a buffer in case we need to resize the tables. */
2686 if (tables_blob->len > ACPI_BUILD_TABLE_SIZE / 2) {
2687 /* As of QEMU 2.1, this fires with 160 VCPUs and 255 memory slots. */
2688 warn_report("ACPI table size %u exceeds %d bytes,"
2689 " migration may not work",
2690 tables_blob->len, ACPI_BUILD_TABLE_SIZE / 2);
2691 error_printf("Try removing CPUs, NUMA nodes, memory slots"
2692 " or PCI bridges.");
2694 acpi_align_size(tables_blob, ACPI_BUILD_TABLE_SIZE);
2697 acpi_align_size(tables->linker->cmd_blob, ACPI_BUILD_ALIGN_SIZE);
2699 /* Cleanup memory that's no longer used. */
2700 g_array_free(table_offsets, true);
2703 static void acpi_ram_update(MemoryRegion *mr, GArray *data)
2705 uint32_t size = acpi_data_len(data);
2707 /* Make sure RAM size is correct - in case it got changed e.g. by migration */
2708 memory_region_ram_resize(mr, size, &error_abort);
2710 memcpy(memory_region_get_ram_ptr(mr), data->data, size);
2711 memory_region_set_dirty(mr, 0, size);
2714 static void acpi_build_update(void *build_opaque)
2716 AcpiBuildState *build_state = build_opaque;
2717 AcpiBuildTables tables;
2719 /* No state to update or already patched? Nothing to do. */
2720 if (!build_state || build_state->patched) {
2721 return;
2723 build_state->patched = 1;
2725 acpi_build_tables_init(&tables);
2727 acpi_build(&tables, MACHINE(qdev_get_machine()));
2729 acpi_ram_update(build_state->table_mr, tables.table_data);
2731 if (build_state->rsdp) {
2732 memcpy(build_state->rsdp, tables.rsdp->data, acpi_data_len(tables.rsdp));
2733 } else {
2734 acpi_ram_update(build_state->rsdp_mr, tables.rsdp);
2737 acpi_ram_update(build_state->linker_mr, tables.linker->cmd_blob);
2738 acpi_build_tables_cleanup(&tables, true);
2741 static void acpi_build_reset(void *build_opaque)
2743 AcpiBuildState *build_state = build_opaque;
2744 build_state->patched = 0;
2747 static const VMStateDescription vmstate_acpi_build = {
2748 .name = "acpi_build",
2749 .version_id = 1,
2750 .minimum_version_id = 1,
2751 .fields = (VMStateField[]) {
2752 VMSTATE_UINT8(patched, AcpiBuildState),
2753 VMSTATE_END_OF_LIST()
2757 void acpi_setup(void)
2759 PCMachineState *pcms = PC_MACHINE(qdev_get_machine());
2760 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
2761 X86MachineState *x86ms = X86_MACHINE(pcms);
2762 AcpiBuildTables tables;
2763 AcpiBuildState *build_state;
2764 Object *vmgenid_dev;
2765 #ifdef CONFIG_TPM
2766 TPMIf *tpm;
2767 static FwCfgTPMConfig tpm_config;
2768 #endif
2770 if (!x86ms->fw_cfg) {
2771 ACPI_BUILD_DPRINTF("No fw cfg. Bailing out.\n");
2772 return;
2775 if (!pcms->acpi_build_enabled) {
2776 ACPI_BUILD_DPRINTF("ACPI build disabled. Bailing out.\n");
2777 return;
2780 if (!x86_machine_is_acpi_enabled(X86_MACHINE(pcms))) {
2781 ACPI_BUILD_DPRINTF("ACPI disabled. Bailing out.\n");
2782 return;
2785 build_state = g_malloc0(sizeof *build_state);
2787 acpi_build_tables_init(&tables);
2788 acpi_build(&tables, MACHINE(pcms));
2790 /* Now expose it all to Guest */
2791 build_state->table_mr = acpi_add_rom_blob(acpi_build_update,
2792 build_state, tables.table_data,
2793 ACPI_BUILD_TABLE_FILE);
2794 assert(build_state->table_mr != NULL);
2796 build_state->linker_mr =
2797 acpi_add_rom_blob(acpi_build_update, build_state,
2798 tables.linker->cmd_blob, ACPI_BUILD_LOADER_FILE);
2800 #ifdef CONFIG_TPM
2801 fw_cfg_add_file(x86ms->fw_cfg, ACPI_BUILD_TPMLOG_FILE,
2802 tables.tcpalog->data, acpi_data_len(tables.tcpalog));
2804 tpm = tpm_find();
2805 if (tpm && object_property_get_bool(OBJECT(tpm), "ppi", &error_abort)) {
2806 tpm_config = (FwCfgTPMConfig) {
2807 .tpmppi_address = cpu_to_le32(TPM_PPI_ADDR_BASE),
2808 .tpm_version = tpm_get_version(tpm),
2809 .tpmppi_version = TPM_PPI_VERSION_1_30
2811 fw_cfg_add_file(x86ms->fw_cfg, "etc/tpm/config",
2812 &tpm_config, sizeof tpm_config);
2814 #endif
2816 vmgenid_dev = find_vmgenid_dev();
2817 if (vmgenid_dev) {
2818 vmgenid_add_fw_cfg(VMGENID(vmgenid_dev), x86ms->fw_cfg,
2819 tables.vmgenid);
2822 if (!pcmc->rsdp_in_ram) {
2824 * Keep for compatibility with old machine types.
2825 * Though RSDP is small, its contents isn't immutable, so
2826 * we'll update it along with the rest of tables on guest access.
2828 uint32_t rsdp_size = acpi_data_len(tables.rsdp);
2830 build_state->rsdp = g_memdup(tables.rsdp->data, rsdp_size);
2831 fw_cfg_add_file_callback(x86ms->fw_cfg, ACPI_BUILD_RSDP_FILE,
2832 acpi_build_update, NULL, build_state,
2833 build_state->rsdp, rsdp_size, true);
2834 build_state->rsdp_mr = NULL;
2835 } else {
2836 build_state->rsdp = NULL;
2837 build_state->rsdp_mr = acpi_add_rom_blob(acpi_build_update,
2838 build_state, tables.rsdp,
2839 ACPI_BUILD_RSDP_FILE);
2842 qemu_register_reset(acpi_build_reset, build_state);
2843 acpi_build_reset(build_state);
2844 vmstate_register(NULL, 0, &vmstate_acpi_build, build_state);
2846 /* Cleanup tables but don't free the memory: we track it
2847 * in build_state.
2849 acpi_build_tables_cleanup(&tables, false);