1 /* Support for generating ACPI tables and passing them to Guests
4 * Copyright (C) 2006 Fabrice Bellard
5 * Copyright (C) 2013 Red Hat Inc
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 "qemu-common.h"
28 #include "qemu/bitmap.h"
29 #include "qemu/error-report.h"
30 #include "hw/pci/pci.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/loader.h"
41 #include "hw/isa/isa.h"
42 #include "hw/block/fdc.h"
43 #include "hw/acpi/memory_hotplug.h"
44 #include "sysemu/tpm.h"
45 #include "hw/acpi/tpm.h"
46 #include "hw/acpi/vmgenid.h"
47 #include "sysemu/tpm_backend.h"
48 #include "hw/timer/mc146818rtc_regs.h"
49 #include "hw/mem/memory-device.h"
50 #include "sysemu/numa.h"
52 /* Supported chipsets: */
53 #include "hw/acpi/piix4.h"
54 #include "hw/acpi/pcihp.h"
55 #include "hw/i386/ich9.h"
56 #include "hw/pci/pci_bus.h"
57 #include "hw/pci-host/q35.h"
58 #include "hw/i386/x86-iommu.h"
60 #include "hw/acpi/aml-build.h"
62 #include "qom/qom-qobject.h"
63 #include "hw/i386/amd_iommu.h"
64 #include "hw/i386/intel_iommu.h"
66 #include "hw/acpi/ipmi.h"
68 /* These are used to size the ACPI tables for -M pc-i440fx-1.7 and
69 * -M pc-i440fx-2.0. Even if the actual amount of AML generated grows
70 * a little bit, there should be plenty of free space since the DSDT
71 * shrunk by ~1.5k between QEMU 2.0 and QEMU 2.1.
73 #define ACPI_BUILD_LEGACY_CPU_AML_SIZE 97
74 #define ACPI_BUILD_ALIGN_SIZE 0x1000
76 #define ACPI_BUILD_TABLE_SIZE 0x20000
78 /* #define DEBUG_ACPI_BUILD */
79 #ifdef DEBUG_ACPI_BUILD
80 #define ACPI_BUILD_DPRINTF(fmt, ...) \
81 do {printf("ACPI_BUILD: " fmt, ## __VA_ARGS__); } while (0)
83 #define ACPI_BUILD_DPRINTF(fmt, ...)
86 /* Default IOAPIC ID */
87 #define ACPI_BUILD_IOAPIC_ID 0x0
89 typedef struct AcpiMcfgInfo {
94 typedef struct AcpiPmInfo {
100 uint16_t cpu_hp_io_base;
101 uint16_t pcihp_io_base;
102 uint16_t pcihp_io_len;
105 typedef struct AcpiMiscInfo {
108 TPMVersion tpm_version;
109 const unsigned char *dsdt_code;
111 uint16_t pvpanic_port;
112 uint16_t applesmc_io_base;
115 typedef struct AcpiBuildPciBusHotplugState {
116 GArray *device_table;
117 GArray *notify_table;
118 struct AcpiBuildPciBusHotplugState *parent;
119 bool pcihp_bridge_en;
120 } AcpiBuildPciBusHotplugState;
122 typedef struct FwCfgTPMConfig {
123 uint32_t tpmppi_address;
125 uint8_t tpmppi_version;
126 } QEMU_PACKED FwCfgTPMConfig;
128 static void init_common_fadt_data(Object *o, AcpiFadtData *data)
130 uint32_t io = object_property_get_uint(o, ACPI_PM_PROP_PM_IO_BASE, NULL);
131 AmlAddressSpace as = AML_AS_SYSTEM_IO;
132 AcpiFadtData fadt = {
135 (1 << ACPI_FADT_F_WBINVD) |
136 (1 << ACPI_FADT_F_PROC_C1) |
137 (1 << ACPI_FADT_F_SLP_BUTTON) |
138 (1 << ACPI_FADT_F_RTC_S4) |
139 (1 << ACPI_FADT_F_USE_PLATFORM_CLOCK) |
140 /* APIC destination mode ("Flat Logical") has an upper limit of 8
141 * CPUs for more than 8 CPUs, "Clustered Logical" mode has to be
144 ((max_cpus > 8) ? (1 << ACPI_FADT_F_FORCE_APIC_CLUSTER_MODEL) : 0),
145 .int_model = 1 /* Multiple APIC */,
146 .rtc_century = RTC_CENTURY,
147 .plvl2_lat = 0xfff /* C2 state not supported */,
148 .plvl3_lat = 0xfff /* C3 state not supported */,
149 .smi_cmd = ACPI_PORT_SMI_CMD,
150 .sci_int = object_property_get_uint(o, ACPI_PM_PROP_SCI_INT, NULL),
152 object_property_get_uint(o, ACPI_PM_PROP_ACPI_ENABLE_CMD, NULL),
154 object_property_get_uint(o, ACPI_PM_PROP_ACPI_DISABLE_CMD, NULL),
155 .pm1a_evt = { .space_id = as, .bit_width = 4 * 8, .address = io },
156 .pm1a_cnt = { .space_id = as, .bit_width = 2 * 8,
157 .address = io + 0x04 },
158 .pm_tmr = { .space_id = as, .bit_width = 4 * 8, .address = io + 0x08 },
159 .gpe0_blk = { .space_id = as, .bit_width =
160 object_property_get_uint(o, ACPI_PM_PROP_GPE0_BLK_LEN, NULL) * 8,
161 .address = object_property_get_uint(o, ACPI_PM_PROP_GPE0_BLK, NULL)
167 static void acpi_get_pm_info(AcpiPmInfo *pm)
169 Object *piix = piix4_pm_find();
170 Object *lpc = ich9_lpc_find();
171 Object *obj = piix ? piix : lpc;
173 pm->cpu_hp_io_base = 0;
174 pm->pcihp_io_base = 0;
175 pm->pcihp_io_len = 0;
177 init_common_fadt_data(obj, &pm->fadt);
179 /* w2k requires FADT(rev1) or it won't boot, keep PC compatible */
181 pm->cpu_hp_io_base = PIIX4_CPU_HOTPLUG_IO_BASE;
183 object_property_get_uint(obj, ACPI_PCIHP_IO_BASE_PROP, NULL);
185 object_property_get_uint(obj, ACPI_PCIHP_IO_LEN_PROP, NULL);
188 struct AcpiGenericAddress r = { .space_id = AML_AS_SYSTEM_IO,
189 .bit_width = 8, .address = ICH9_RST_CNT_IOPORT };
190 pm->fadt.reset_reg = r;
191 pm->fadt.reset_val = 0xf;
192 pm->fadt.flags |= 1 << ACPI_FADT_F_RESET_REG_SUP;
193 pm->cpu_hp_io_base = ICH9_CPU_HOTPLUG_IO_BASE;
197 /* The above need not be conditional on machine type because the reset port
198 * happens to be the same on PIIX (pc) and ICH9 (q35). */
199 QEMU_BUILD_BUG_ON(ICH9_RST_CNT_IOPORT != RCR_IOPORT);
201 /* Fill in optional s3/s4 related properties */
202 o = object_property_get_qobject(obj, ACPI_PM_PROP_S3_DISABLED, NULL);
204 pm->s3_disabled = qnum_get_uint(qobject_to(QNum, o));
206 pm->s3_disabled = false;
209 o = object_property_get_qobject(obj, ACPI_PM_PROP_S4_DISABLED, NULL);
211 pm->s4_disabled = qnum_get_uint(qobject_to(QNum, o));
213 pm->s4_disabled = false;
216 o = object_property_get_qobject(obj, ACPI_PM_PROP_S4_VAL, NULL);
218 pm->s4_val = qnum_get_uint(qobject_to(QNum, o));
224 pm->pcihp_bridge_en =
225 object_property_get_bool(obj, "acpi-pci-hotplug-with-bridge-support",
229 static void acpi_get_misc_info(AcpiMiscInfo *info)
231 Object *piix = piix4_pm_find();
232 Object *lpc = ich9_lpc_find();
233 assert(!!piix != !!lpc);
236 info->is_piix4 = true;
239 info->is_piix4 = false;
242 info->has_hpet = hpet_find();
243 info->tpm_version = tpm_get_version(tpm_find());
244 info->pvpanic_port = pvpanic_port();
245 info->applesmc_io_base = applesmc_port();
249 * Because of the PXB hosts we cannot simply query TYPE_PCI_HOST_BRIDGE.
250 * On i386 arch we only have two pci hosts, so we can look only for them.
252 static Object *acpi_get_i386_pci_host(void)
256 host = OBJECT_CHECK(PCIHostState,
257 object_resolve_path("/machine/i440fx", NULL),
258 TYPE_PCI_HOST_BRIDGE);
260 host = OBJECT_CHECK(PCIHostState,
261 object_resolve_path("/machine/q35", NULL),
262 TYPE_PCI_HOST_BRIDGE);
268 static void acpi_get_pci_holes(Range *hole, Range *hole64)
272 pci_host = acpi_get_i386_pci_host();
275 range_set_bounds1(hole,
276 object_property_get_uint(pci_host,
277 PCI_HOST_PROP_PCI_HOLE_START,
279 object_property_get_uint(pci_host,
280 PCI_HOST_PROP_PCI_HOLE_END,
282 range_set_bounds1(hole64,
283 object_property_get_uint(pci_host,
284 PCI_HOST_PROP_PCI_HOLE64_START,
286 object_property_get_uint(pci_host,
287 PCI_HOST_PROP_PCI_HOLE64_END,
291 static void acpi_align_size(GArray *blob, unsigned align)
293 /* Align size to multiple of given size. This reduces the chance
294 * we need to change size in the future (breaking cross version migration).
296 g_array_set_size(blob, ROUND_UP(acpi_data_len(blob), align));
301 build_facs(GArray *table_data, BIOSLinker *linker)
303 AcpiFacsDescriptorRev1 *facs = acpi_data_push(table_data, sizeof *facs);
304 memcpy(&facs->signature, "FACS", 4);
305 facs->length = cpu_to_le32(sizeof(*facs));
308 void pc_madt_cpu_entry(AcpiDeviceIf *adev, int uid,
309 const CPUArchIdList *apic_ids, GArray *entry)
311 uint32_t apic_id = apic_ids->cpus[uid].arch_id;
313 /* ACPI spec says that LAPIC entry for non present
314 * CPU may be omitted from MADT or it must be marked
315 * as disabled. However omitting non present CPU from
316 * MADT breaks hotplug on linux. So possible CPUs
317 * should be put in MADT but kept disabled.
320 AcpiMadtProcessorApic *apic = acpi_data_push(entry, sizeof *apic);
322 apic->type = ACPI_APIC_PROCESSOR;
323 apic->length = sizeof(*apic);
324 apic->processor_id = uid;
325 apic->local_apic_id = apic_id;
326 if (apic_ids->cpus[uid].cpu != NULL) {
327 apic->flags = cpu_to_le32(1);
329 apic->flags = cpu_to_le32(0);
332 AcpiMadtProcessorX2Apic *apic = acpi_data_push(entry, sizeof *apic);
334 apic->type = ACPI_APIC_LOCAL_X2APIC;
335 apic->length = sizeof(*apic);
336 apic->uid = cpu_to_le32(uid);
337 apic->x2apic_id = cpu_to_le32(apic_id);
338 if (apic_ids->cpus[uid].cpu != NULL) {
339 apic->flags = cpu_to_le32(1);
341 apic->flags = cpu_to_le32(0);
347 build_madt(GArray *table_data, BIOSLinker *linker, PCMachineState *pcms)
349 MachineClass *mc = MACHINE_GET_CLASS(pcms);
350 const CPUArchIdList *apic_ids = mc->possible_cpu_arch_ids(MACHINE(pcms));
351 int madt_start = table_data->len;
352 AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(pcms->acpi_dev);
353 AcpiDeviceIf *adev = ACPI_DEVICE_IF(pcms->acpi_dev);
354 bool x2apic_mode = false;
356 AcpiMultipleApicTable *madt;
357 AcpiMadtIoApic *io_apic;
358 AcpiMadtIntsrcovr *intsrcovr;
361 madt = acpi_data_push(table_data, sizeof *madt);
362 madt->local_apic_address = cpu_to_le32(APIC_DEFAULT_ADDRESS);
363 madt->flags = cpu_to_le32(1);
365 for (i = 0; i < apic_ids->len; i++) {
366 adevc->madt_cpu(adev, i, apic_ids, table_data);
367 if (apic_ids->cpus[i].arch_id > 254) {
372 io_apic = acpi_data_push(table_data, sizeof *io_apic);
373 io_apic->type = ACPI_APIC_IO;
374 io_apic->length = sizeof(*io_apic);
375 io_apic->io_apic_id = ACPI_BUILD_IOAPIC_ID;
376 io_apic->address = cpu_to_le32(IO_APIC_DEFAULT_ADDRESS);
377 io_apic->interrupt = cpu_to_le32(0);
379 if (pcms->apic_xrupt_override) {
380 intsrcovr = acpi_data_push(table_data, sizeof *intsrcovr);
381 intsrcovr->type = ACPI_APIC_XRUPT_OVERRIDE;
382 intsrcovr->length = sizeof(*intsrcovr);
383 intsrcovr->source = 0;
384 intsrcovr->gsi = cpu_to_le32(2);
385 intsrcovr->flags = cpu_to_le16(0); /* conforms to bus specifications */
387 for (i = 1; i < 16; i++) {
388 #define ACPI_BUILD_PCI_IRQS ((1<<5) | (1<<9) | (1<<10) | (1<<11))
389 if (!(ACPI_BUILD_PCI_IRQS & (1 << i))) {
390 /* No need for a INT source override structure. */
393 intsrcovr = acpi_data_push(table_data, sizeof *intsrcovr);
394 intsrcovr->type = ACPI_APIC_XRUPT_OVERRIDE;
395 intsrcovr->length = sizeof(*intsrcovr);
396 intsrcovr->source = i;
397 intsrcovr->gsi = cpu_to_le32(i);
398 intsrcovr->flags = cpu_to_le16(0xd); /* active high, level triggered */
402 AcpiMadtLocalX2ApicNmi *local_nmi;
404 local_nmi = acpi_data_push(table_data, sizeof *local_nmi);
405 local_nmi->type = ACPI_APIC_LOCAL_X2APIC_NMI;
406 local_nmi->length = sizeof(*local_nmi);
407 local_nmi->uid = 0xFFFFFFFF; /* all processors */
408 local_nmi->flags = cpu_to_le16(0);
409 local_nmi->lint = 1; /* ACPI_LINT1 */
411 AcpiMadtLocalNmi *local_nmi;
413 local_nmi = acpi_data_push(table_data, sizeof *local_nmi);
414 local_nmi->type = ACPI_APIC_LOCAL_NMI;
415 local_nmi->length = sizeof(*local_nmi);
416 local_nmi->processor_id = 0xff; /* all processors */
417 local_nmi->flags = cpu_to_le16(0);
418 local_nmi->lint = 1; /* ACPI_LINT1 */
421 build_header(linker, table_data,
422 (void *)(table_data->data + madt_start), "APIC",
423 table_data->len - madt_start, 1, NULL, NULL);
426 static void build_append_pcihp_notify_entry(Aml *method, int slot)
429 int32_t devfn = PCI_DEVFN(slot, 0);
431 if_ctx = aml_if(aml_and(aml_arg(0), aml_int(0x1U << slot), NULL));
432 aml_append(if_ctx, aml_notify(aml_name("S%.02X", devfn), aml_arg(1)));
433 aml_append(method, if_ctx);
436 static void build_append_pci_bus_devices(Aml *parent_scope, PCIBus *bus,
437 bool pcihp_bridge_en)
439 Aml *dev, *notify_method = NULL, *method;
444 bsel = object_property_get_qobject(OBJECT(bus), ACPI_PCIHP_PROP_BSEL, NULL);
446 uint64_t bsel_val = qnum_get_uint(qobject_to(QNum, bsel));
448 aml_append(parent_scope, aml_name_decl("BSEL", aml_int(bsel_val)));
449 notify_method = aml_method("DVNT", 2, AML_NOTSERIALIZED);
452 for (i = 0; i < ARRAY_SIZE(bus->devices); i += PCI_FUNC_MAX) {
455 PCIDevice *pdev = bus->devices[i];
456 int slot = PCI_SLOT(i);
457 bool hotplug_enabled_dev;
461 if (bsel) { /* add hotplug slots for non present devices */
462 dev = aml_device("S%.02X", PCI_DEVFN(slot, 0));
463 aml_append(dev, aml_name_decl("_SUN", aml_int(slot)));
464 aml_append(dev, aml_name_decl("_ADR", aml_int(slot << 16)));
465 method = aml_method("_EJ0", 1, AML_NOTSERIALIZED);
467 aml_call2("PCEJ", aml_name("BSEL"), aml_name("_SUN"))
469 aml_append(dev, method);
470 aml_append(parent_scope, dev);
472 build_append_pcihp_notify_entry(notify_method, slot);
477 pc = PCI_DEVICE_GET_CLASS(pdev);
478 dc = DEVICE_GET_CLASS(pdev);
480 /* When hotplug for bridges is enabled, bridges are
481 * described in ACPI separately (see build_pci_bus_end).
482 * In this case they aren't themselves hot-pluggable.
483 * Hotplugged bridges *are* hot-pluggable.
485 bridge_in_acpi = pc->is_bridge && pcihp_bridge_en &&
486 !DEVICE(pdev)->hotplugged;
488 hotplug_enabled_dev = bsel && dc->hotpluggable && !bridge_in_acpi;
490 if (pc->class_id == PCI_CLASS_BRIDGE_ISA) {
494 /* start to compose PCI slot descriptor */
495 dev = aml_device("S%.02X", PCI_DEVFN(slot, 0));
496 aml_append(dev, aml_name_decl("_ADR", aml_int(slot << 16)));
498 if (pc->class_id == PCI_CLASS_DISPLAY_VGA) {
499 /* add VGA specific AML methods */
502 if (object_dynamic_cast(OBJECT(pdev), "qxl-vga")) {
508 method = aml_method("_S1D", 0, AML_NOTSERIALIZED);
509 aml_append(method, aml_return(aml_int(0)));
510 aml_append(dev, method);
512 method = aml_method("_S2D", 0, AML_NOTSERIALIZED);
513 aml_append(method, aml_return(aml_int(0)));
514 aml_append(dev, method);
516 method = aml_method("_S3D", 0, AML_NOTSERIALIZED);
517 aml_append(method, aml_return(aml_int(s3d)));
518 aml_append(dev, method);
519 } else if (hotplug_enabled_dev) {
520 /* add _SUN/_EJ0 to make slot hotpluggable */
521 aml_append(dev, aml_name_decl("_SUN", aml_int(slot)));
523 method = aml_method("_EJ0", 1, AML_NOTSERIALIZED);
525 aml_call2("PCEJ", aml_name("BSEL"), aml_name("_SUN"))
527 aml_append(dev, method);
530 build_append_pcihp_notify_entry(notify_method, slot);
532 } else if (bridge_in_acpi) {
534 * device is coldplugged bridge,
535 * add child device descriptions into its scope
537 PCIBus *sec_bus = pci_bridge_get_sec_bus(PCI_BRIDGE(pdev));
539 build_append_pci_bus_devices(dev, sec_bus, pcihp_bridge_en);
541 /* slot descriptor has been composed, add it into parent context */
542 aml_append(parent_scope, dev);
546 aml_append(parent_scope, notify_method);
549 /* Append PCNT method to notify about events on local and child buses.
550 * Add unconditionally for root since DSDT expects it.
552 method = aml_method("PCNT", 0, AML_NOTSERIALIZED);
554 /* If bus supports hotplug select it and notify about local events */
556 uint64_t bsel_val = qnum_get_uint(qobject_to(QNum, bsel));
558 aml_append(method, aml_store(aml_int(bsel_val), aml_name("BNUM")));
560 aml_call2("DVNT", aml_name("PCIU"), aml_int(1) /* Device Check */)
563 aml_call2("DVNT", aml_name("PCID"), aml_int(3)/* Eject Request */)
567 /* Notify about child bus events in any case */
568 if (pcihp_bridge_en) {
569 QLIST_FOREACH(sec, &bus->child, sibling) {
570 int32_t devfn = sec->parent_dev->devfn;
572 if (pci_bus_is_root(sec) || pci_bus_is_express(sec)) {
576 aml_append(method, aml_name("^S%.02X.PCNT", devfn));
579 aml_append(parent_scope, method);
585 * @link_name: link name for PCI route entry
587 * build AML package containing a PCI route entry for @link_name
589 static Aml *build_prt_entry(const char *link_name)
591 Aml *a_zero = aml_int(0);
592 Aml *pkg = aml_package(4);
593 aml_append(pkg, a_zero);
594 aml_append(pkg, a_zero);
595 aml_append(pkg, aml_name("%s", link_name));
596 aml_append(pkg, a_zero);
601 * initialize_route - Initialize the interrupt routing rule
602 * through a specific LINK:
603 * if (lnk_idx == idx)
604 * route using link 'link_name'
606 static Aml *initialize_route(Aml *route, const char *link_name,
607 Aml *lnk_idx, int idx)
609 Aml *if_ctx = aml_if(aml_equal(lnk_idx, aml_int(idx)));
610 Aml *pkg = build_prt_entry(link_name);
612 aml_append(if_ctx, aml_store(pkg, route));
618 * build_prt - Define interrupt rounting rules
620 * Returns an array of 128 routes, one for each device,
621 * based on device location.
622 * The main goal is to equaly distribute the interrupts
623 * over the 4 existing ACPI links (works only for i440fx).
624 * The hash function is (slot + pin) & 3 -> "LNK[D|A|B|C]".
627 static Aml *build_prt(bool is_pci0_prt)
629 Aml *method, *while_ctx, *pin, *res;
631 method = aml_method("_PRT", 0, AML_NOTSERIALIZED);
634 aml_append(method, aml_store(aml_package(128), res));
635 aml_append(method, aml_store(aml_int(0), pin));
637 /* while (pin < 128) */
638 while_ctx = aml_while(aml_lless(pin, aml_int(128)));
640 Aml *slot = aml_local(2);
641 Aml *lnk_idx = aml_local(3);
642 Aml *route = aml_local(4);
644 /* slot = pin >> 2 */
645 aml_append(while_ctx,
646 aml_store(aml_shiftright(pin, aml_int(2), NULL), slot));
647 /* lnk_idx = (slot + pin) & 3 */
648 aml_append(while_ctx,
649 aml_store(aml_and(aml_add(pin, slot, NULL), aml_int(3), NULL),
652 /* route[2] = "LNK[D|A|B|C]", selection based on pin % 3 */
653 aml_append(while_ctx, initialize_route(route, "LNKD", lnk_idx, 0));
655 Aml *if_device_1, *if_pin_4, *else_pin_4;
657 /* device 1 is the power-management device, needs SCI */
658 if_device_1 = aml_if(aml_equal(lnk_idx, aml_int(1)));
660 if_pin_4 = aml_if(aml_equal(pin, aml_int(4)));
663 aml_store(build_prt_entry("LNKS"), route));
665 aml_append(if_device_1, if_pin_4);
666 else_pin_4 = aml_else();
668 aml_append(else_pin_4,
669 aml_store(build_prt_entry("LNKA"), route));
671 aml_append(if_device_1, else_pin_4);
673 aml_append(while_ctx, if_device_1);
675 aml_append(while_ctx, initialize_route(route, "LNKA", lnk_idx, 1));
677 aml_append(while_ctx, initialize_route(route, "LNKB", lnk_idx, 2));
678 aml_append(while_ctx, initialize_route(route, "LNKC", lnk_idx, 3));
680 /* route[0] = 0x[slot]FFFF */
681 aml_append(while_ctx,
682 aml_store(aml_or(aml_shiftleft(slot, aml_int(16)), aml_int(0xFFFF),
684 aml_index(route, aml_int(0))));
685 /* route[1] = pin & 3 */
686 aml_append(while_ctx,
687 aml_store(aml_and(pin, aml_int(3), NULL),
688 aml_index(route, aml_int(1))));
689 /* res[pin] = route */
690 aml_append(while_ctx, aml_store(route, aml_index(res, pin)));
692 aml_append(while_ctx, aml_increment(pin));
694 aml_append(method, while_ctx);
696 aml_append(method, aml_return(res));
701 typedef struct CrsRangeEntry {
706 static void crs_range_insert(GPtrArray *ranges, uint64_t base, uint64_t limit)
708 CrsRangeEntry *entry;
710 entry = g_malloc(sizeof(*entry));
712 entry->limit = limit;
714 g_ptr_array_add(ranges, entry);
717 static void crs_range_free(gpointer data)
719 CrsRangeEntry *entry = (CrsRangeEntry *)data;
723 typedef struct CrsRangeSet {
724 GPtrArray *io_ranges;
725 GPtrArray *mem_ranges;
726 GPtrArray *mem_64bit_ranges;
729 static void crs_range_set_init(CrsRangeSet *range_set)
731 range_set->io_ranges = g_ptr_array_new_with_free_func(crs_range_free);
732 range_set->mem_ranges = g_ptr_array_new_with_free_func(crs_range_free);
733 range_set->mem_64bit_ranges =
734 g_ptr_array_new_with_free_func(crs_range_free);
737 static void crs_range_set_free(CrsRangeSet *range_set)
739 g_ptr_array_free(range_set->io_ranges, true);
740 g_ptr_array_free(range_set->mem_ranges, true);
741 g_ptr_array_free(range_set->mem_64bit_ranges, true);
744 static gint crs_range_compare(gconstpointer a, gconstpointer b)
746 CrsRangeEntry *entry_a = *(CrsRangeEntry **)a;
747 CrsRangeEntry *entry_b = *(CrsRangeEntry **)b;
749 return (int64_t)entry_a->base - (int64_t)entry_b->base;
753 * crs_replace_with_free_ranges - given the 'used' ranges within [start - end]
754 * interval, computes the 'free' ranges from the same interval.
755 * Example: If the input array is { [a1 - a2],[b1 - b2] }, the function
756 * will return { [base - a1], [a2 - b1], [b2 - limit] }.
758 static void crs_replace_with_free_ranges(GPtrArray *ranges,
759 uint64_t start, uint64_t end)
761 GPtrArray *free_ranges = g_ptr_array_new();
762 uint64_t free_base = start;
765 g_ptr_array_sort(ranges, crs_range_compare);
766 for (i = 0; i < ranges->len; i++) {
767 CrsRangeEntry *used = g_ptr_array_index(ranges, i);
769 if (free_base < used->base) {
770 crs_range_insert(free_ranges, free_base, used->base - 1);
773 free_base = used->limit + 1;
776 if (free_base < end) {
777 crs_range_insert(free_ranges, free_base, end);
780 g_ptr_array_set_size(ranges, 0);
781 for (i = 0; i < free_ranges->len; i++) {
782 g_ptr_array_add(ranges, g_ptr_array_index(free_ranges, i));
785 g_ptr_array_free(free_ranges, true);
789 * crs_range_merge - merges adjacent ranges in the given array.
790 * Array elements are deleted and replaced with the merged ranges.
792 static void crs_range_merge(GPtrArray *range)
794 GPtrArray *tmp = g_ptr_array_new_with_free_func(crs_range_free);
795 CrsRangeEntry *entry;
796 uint64_t range_base, range_limit;
803 g_ptr_array_sort(range, crs_range_compare);
805 entry = g_ptr_array_index(range, 0);
806 range_base = entry->base;
807 range_limit = entry->limit;
808 for (i = 1; i < range->len; i++) {
809 entry = g_ptr_array_index(range, i);
810 if (entry->base - 1 == range_limit) {
811 range_limit = entry->limit;
813 crs_range_insert(tmp, range_base, range_limit);
814 range_base = entry->base;
815 range_limit = entry->limit;
818 crs_range_insert(tmp, range_base, range_limit);
820 g_ptr_array_set_size(range, 0);
821 for (i = 0; i < tmp->len; i++) {
822 entry = g_ptr_array_index(tmp, i);
823 crs_range_insert(range, entry->base, entry->limit);
825 g_ptr_array_free(tmp, true);
828 static Aml *build_crs(PCIHostState *host, CrsRangeSet *range_set)
830 Aml *crs = aml_resource_template();
831 CrsRangeSet temp_range_set;
832 CrsRangeEntry *entry;
833 uint8_t max_bus = pci_bus_num(host->bus);
838 crs_range_set_init(&temp_range_set);
839 for (devfn = 0; devfn < ARRAY_SIZE(host->bus->devices); devfn++) {
840 uint64_t range_base, range_limit;
841 PCIDevice *dev = host->bus->devices[devfn];
847 for (i = 0; i < PCI_NUM_REGIONS; i++) {
848 PCIIORegion *r = &dev->io_regions[i];
850 range_base = r->addr;
851 range_limit = r->addr + r->size - 1;
854 * Work-around for old bioses
855 * that do not support multiple root buses
857 if (!range_base || range_base > range_limit) {
861 if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
862 crs_range_insert(temp_range_set.io_ranges,
863 range_base, range_limit);
864 } else { /* "memory" */
865 crs_range_insert(temp_range_set.mem_ranges,
866 range_base, range_limit);
870 type = dev->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION;
871 if (type == PCI_HEADER_TYPE_BRIDGE) {
872 uint8_t subordinate = dev->config[PCI_SUBORDINATE_BUS];
873 if (subordinate > max_bus) {
874 max_bus = subordinate;
877 range_base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_IO);
878 range_limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_IO);
881 * Work-around for old bioses
882 * that do not support multiple root buses
884 if (range_base && range_base <= range_limit) {
885 crs_range_insert(temp_range_set.io_ranges,
886 range_base, range_limit);
890 pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_MEMORY);
892 pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_MEMORY);
895 * Work-around for old bioses
896 * that do not support multiple root buses
898 if (range_base && range_base <= range_limit) {
899 uint64_t length = range_limit - range_base + 1;
900 if (range_limit <= UINT32_MAX && length <= UINT32_MAX) {
901 crs_range_insert(temp_range_set.mem_ranges,
902 range_base, range_limit);
904 crs_range_insert(temp_range_set.mem_64bit_ranges,
905 range_base, range_limit);
910 pci_bridge_get_base(dev, PCI_BASE_ADDRESS_MEM_PREFETCH);
912 pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_MEM_PREFETCH);
915 * Work-around for old bioses
916 * that do not support multiple root buses
918 if (range_base && range_base <= range_limit) {
919 uint64_t length = range_limit - range_base + 1;
920 if (range_limit <= UINT32_MAX && length <= UINT32_MAX) {
921 crs_range_insert(temp_range_set.mem_ranges,
922 range_base, range_limit);
924 crs_range_insert(temp_range_set.mem_64bit_ranges,
925 range_base, range_limit);
931 crs_range_merge(temp_range_set.io_ranges);
932 for (i = 0; i < temp_range_set.io_ranges->len; i++) {
933 entry = g_ptr_array_index(temp_range_set.io_ranges, i);
935 aml_word_io(AML_MIN_FIXED, AML_MAX_FIXED,
936 AML_POS_DECODE, AML_ENTIRE_RANGE,
937 0, entry->base, entry->limit, 0,
938 entry->limit - entry->base + 1));
939 crs_range_insert(range_set->io_ranges, entry->base, entry->limit);
942 crs_range_merge(temp_range_set.mem_ranges);
943 for (i = 0; i < temp_range_set.mem_ranges->len; i++) {
944 entry = g_ptr_array_index(temp_range_set.mem_ranges, i);
946 aml_dword_memory(AML_POS_DECODE, AML_MIN_FIXED,
947 AML_MAX_FIXED, AML_NON_CACHEABLE,
949 0, entry->base, entry->limit, 0,
950 entry->limit - entry->base + 1));
951 crs_range_insert(range_set->mem_ranges, entry->base, entry->limit);
954 crs_range_merge(temp_range_set.mem_64bit_ranges);
955 for (i = 0; i < temp_range_set.mem_64bit_ranges->len; i++) {
956 entry = g_ptr_array_index(temp_range_set.mem_64bit_ranges, i);
958 aml_qword_memory(AML_POS_DECODE, AML_MIN_FIXED,
959 AML_MAX_FIXED, AML_NON_CACHEABLE,
961 0, entry->base, entry->limit, 0,
962 entry->limit - entry->base + 1));
963 crs_range_insert(range_set->mem_64bit_ranges,
964 entry->base, entry->limit);
967 crs_range_set_free(&temp_range_set);
970 aml_word_bus_number(AML_MIN_FIXED, AML_MAX_FIXED, AML_POS_DECODE,
972 pci_bus_num(host->bus),
975 max_bus - pci_bus_num(host->bus) + 1));
980 static void build_hpet_aml(Aml *table)
986 Aml *scope = aml_scope("_SB");
987 Aml *dev = aml_device("HPET");
988 Aml *zero = aml_int(0);
989 Aml *id = aml_local(0);
990 Aml *period = aml_local(1);
992 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0103")));
993 aml_append(dev, aml_name_decl("_UID", zero));
996 aml_operation_region("HPTM", AML_SYSTEM_MEMORY, aml_int(HPET_BASE),
998 field = aml_field("HPTM", AML_DWORD_ACC, AML_LOCK, AML_PRESERVE);
999 aml_append(field, aml_named_field("VEND", 32));
1000 aml_append(field, aml_named_field("PRD", 32));
1001 aml_append(dev, field);
1003 method = aml_method("_STA", 0, AML_NOTSERIALIZED);
1004 aml_append(method, aml_store(aml_name("VEND"), id));
1005 aml_append(method, aml_store(aml_name("PRD"), period));
1006 aml_append(method, aml_shiftright(id, aml_int(16), id));
1007 if_ctx = aml_if(aml_lor(aml_equal(id, zero),
1008 aml_equal(id, aml_int(0xffff))));
1010 aml_append(if_ctx, aml_return(zero));
1012 aml_append(method, if_ctx);
1014 if_ctx = aml_if(aml_lor(aml_equal(period, zero),
1015 aml_lgreater(period, aml_int(100000000))));
1017 aml_append(if_ctx, aml_return(zero));
1019 aml_append(method, if_ctx);
1021 aml_append(method, aml_return(aml_int(0x0F)));
1022 aml_append(dev, method);
1024 crs = aml_resource_template();
1025 aml_append(crs, aml_memory32_fixed(HPET_BASE, HPET_LEN, AML_READ_ONLY));
1026 aml_append(dev, aml_name_decl("_CRS", crs));
1028 aml_append(scope, dev);
1029 aml_append(table, scope);
1032 static Aml *build_fdinfo_aml(int idx, FloppyDriveType type)
1035 uint8_t maxc, maxh, maxs;
1037 isa_fdc_get_drive_max_chs(type, &maxc, &maxh, &maxs);
1039 dev = aml_device("FLP%c", 'A' + idx);
1041 aml_append(dev, aml_name_decl("_ADR", aml_int(idx)));
1043 fdi = aml_package(16);
1044 aml_append(fdi, aml_int(idx)); /* Drive Number */
1046 aml_int(cmos_get_fd_drive_type(type))); /* Device Type */
1048 * the values below are the limits of the drive, and are thus independent
1049 * of the inserted media
1051 aml_append(fdi, aml_int(maxc)); /* Maximum Cylinder Number */
1052 aml_append(fdi, aml_int(maxs)); /* Maximum Sector Number */
1053 aml_append(fdi, aml_int(maxh)); /* Maximum Head Number */
1055 * SeaBIOS returns the below values for int 0x13 func 0x08 regardless of
1056 * the drive type, so shall we
1058 aml_append(fdi, aml_int(0xAF)); /* disk_specify_1 */
1059 aml_append(fdi, aml_int(0x02)); /* disk_specify_2 */
1060 aml_append(fdi, aml_int(0x25)); /* disk_motor_wait */
1061 aml_append(fdi, aml_int(0x02)); /* disk_sector_siz */
1062 aml_append(fdi, aml_int(0x12)); /* disk_eot */
1063 aml_append(fdi, aml_int(0x1B)); /* disk_rw_gap */
1064 aml_append(fdi, aml_int(0xFF)); /* disk_dtl */
1065 aml_append(fdi, aml_int(0x6C)); /* disk_formt_gap */
1066 aml_append(fdi, aml_int(0xF6)); /* disk_fill */
1067 aml_append(fdi, aml_int(0x0F)); /* disk_head_sttl */
1068 aml_append(fdi, aml_int(0x08)); /* disk_motor_strt */
1070 aml_append(dev, aml_name_decl("_FDI", fdi));
1074 static Aml *build_fdc_device_aml(ISADevice *fdc)
1080 #define ACPI_FDE_MAX_FD 4
1081 uint32_t fde_buf[5] = {
1082 0, 0, 0, 0, /* presence of floppy drives #0 - #3 */
1083 cpu_to_le32(2) /* tape presence (2 == never present) */
1086 dev = aml_device("FDC0");
1087 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0700")));
1089 crs = aml_resource_template();
1090 aml_append(crs, aml_io(AML_DECODE16, 0x03F2, 0x03F2, 0x00, 0x04));
1091 aml_append(crs, aml_io(AML_DECODE16, 0x03F7, 0x03F7, 0x00, 0x01));
1092 aml_append(crs, aml_irq_no_flags(6));
1094 aml_dma(AML_COMPATIBILITY, AML_NOTBUSMASTER, AML_TRANSFER8, 2));
1095 aml_append(dev, aml_name_decl("_CRS", crs));
1097 for (i = 0; i < MIN(MAX_FD, ACPI_FDE_MAX_FD); i++) {
1098 FloppyDriveType type = isa_fdc_get_drive_type(fdc, i);
1100 if (type < FLOPPY_DRIVE_TYPE_NONE) {
1101 fde_buf[i] = cpu_to_le32(1); /* drive present */
1102 aml_append(dev, build_fdinfo_aml(i, type));
1105 aml_append(dev, aml_name_decl("_FDE",
1106 aml_buffer(sizeof(fde_buf), (uint8_t *)fde_buf)));
1111 static Aml *build_rtc_device_aml(void)
1116 dev = aml_device("RTC");
1117 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0B00")));
1118 crs = aml_resource_template();
1119 aml_append(crs, aml_io(AML_DECODE16, 0x0070, 0x0070, 0x10, 0x02));
1120 aml_append(crs, aml_irq_no_flags(8));
1121 aml_append(crs, aml_io(AML_DECODE16, 0x0072, 0x0072, 0x02, 0x06));
1122 aml_append(dev, aml_name_decl("_CRS", crs));
1127 static Aml *build_kbd_device_aml(void)
1133 dev = aml_device("KBD");
1134 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0303")));
1136 method = aml_method("_STA", 0, AML_NOTSERIALIZED);
1137 aml_append(method, aml_return(aml_int(0x0f)));
1138 aml_append(dev, method);
1140 crs = aml_resource_template();
1141 aml_append(crs, aml_io(AML_DECODE16, 0x0060, 0x0060, 0x01, 0x01));
1142 aml_append(crs, aml_io(AML_DECODE16, 0x0064, 0x0064, 0x01, 0x01));
1143 aml_append(crs, aml_irq_no_flags(1));
1144 aml_append(dev, aml_name_decl("_CRS", crs));
1149 static Aml *build_mouse_device_aml(void)
1155 dev = aml_device("MOU");
1156 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0F13")));
1158 method = aml_method("_STA", 0, AML_NOTSERIALIZED);
1159 aml_append(method, aml_return(aml_int(0x0f)));
1160 aml_append(dev, method);
1162 crs = aml_resource_template();
1163 aml_append(crs, aml_irq_no_flags(12));
1164 aml_append(dev, aml_name_decl("_CRS", crs));
1169 static Aml *build_lpt_device_aml(void)
1176 Aml *zero = aml_int(0);
1177 Aml *is_present = aml_local(0);
1179 dev = aml_device("LPT");
1180 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0400")));
1182 method = aml_method("_STA", 0, AML_NOTSERIALIZED);
1183 aml_append(method, aml_store(aml_name("LPEN"), is_present));
1184 if_ctx = aml_if(aml_equal(is_present, zero));
1186 aml_append(if_ctx, aml_return(aml_int(0x00)));
1188 aml_append(method, if_ctx);
1189 else_ctx = aml_else();
1191 aml_append(else_ctx, aml_return(aml_int(0x0f)));
1193 aml_append(method, else_ctx);
1194 aml_append(dev, method);
1196 crs = aml_resource_template();
1197 aml_append(crs, aml_io(AML_DECODE16, 0x0378, 0x0378, 0x08, 0x08));
1198 aml_append(crs, aml_irq_no_flags(7));
1199 aml_append(dev, aml_name_decl("_CRS", crs));
1204 static Aml *build_com_device_aml(uint8_t uid)
1211 Aml *zero = aml_int(0);
1212 Aml *is_present = aml_local(0);
1213 const char *enabled_field = "CAEN";
1215 uint16_t io_port = 0x03F8;
1217 assert(uid == 1 || uid == 2);
1219 enabled_field = "CBEN";
1224 dev = aml_device("COM%d", uid);
1225 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0501")));
1226 aml_append(dev, aml_name_decl("_UID", aml_int(uid)));
1228 method = aml_method("_STA", 0, AML_NOTSERIALIZED);
1229 aml_append(method, aml_store(aml_name("%s", enabled_field), is_present));
1230 if_ctx = aml_if(aml_equal(is_present, zero));
1232 aml_append(if_ctx, aml_return(aml_int(0x00)));
1234 aml_append(method, if_ctx);
1235 else_ctx = aml_else();
1237 aml_append(else_ctx, aml_return(aml_int(0x0f)));
1239 aml_append(method, else_ctx);
1240 aml_append(dev, method);
1242 crs = aml_resource_template();
1243 aml_append(crs, aml_io(AML_DECODE16, io_port, io_port, 0x00, 0x08));
1244 aml_append(crs, aml_irq_no_flags(irq));
1245 aml_append(dev, aml_name_decl("_CRS", crs));
1250 static void build_isa_devices_aml(Aml *table)
1252 ISADevice *fdc = pc_find_fdc0();
1255 Aml *scope = aml_scope("_SB.PCI0.ISA");
1256 Object *obj = object_resolve_path_type("", TYPE_ISA_BUS, &ambiguous);
1258 aml_append(scope, build_rtc_device_aml());
1259 aml_append(scope, build_kbd_device_aml());
1260 aml_append(scope, build_mouse_device_aml());
1262 aml_append(scope, build_fdc_device_aml(fdc));
1264 aml_append(scope, build_lpt_device_aml());
1265 aml_append(scope, build_com_device_aml(1));
1266 aml_append(scope, build_com_device_aml(2));
1269 error_report("Multiple ISA busses, unable to define IPMI ACPI data");
1271 error_report("No ISA bus, unable to define IPMI ACPI data");
1273 build_acpi_ipmi_devices(scope, BUS(obj));
1276 aml_append(table, scope);
1279 static void build_dbg_aml(Aml *table)
1284 Aml *scope = aml_scope("\\");
1285 Aml *buf = aml_local(0);
1286 Aml *len = aml_local(1);
1287 Aml *idx = aml_local(2);
1290 aml_operation_region("DBG", AML_SYSTEM_IO, aml_int(0x0402), 0x01));
1291 field = aml_field("DBG", AML_BYTE_ACC, AML_NOLOCK, AML_PRESERVE);
1292 aml_append(field, aml_named_field("DBGB", 8));
1293 aml_append(scope, field);
1295 method = aml_method("DBUG", 1, AML_NOTSERIALIZED);
1297 aml_append(method, aml_to_hexstring(aml_arg(0), buf));
1298 aml_append(method, aml_to_buffer(buf, buf));
1299 aml_append(method, aml_subtract(aml_sizeof(buf), aml_int(1), len));
1300 aml_append(method, aml_store(aml_int(0), idx));
1302 while_ctx = aml_while(aml_lless(idx, len));
1303 aml_append(while_ctx,
1304 aml_store(aml_derefof(aml_index(buf, idx)), aml_name("DBGB")));
1305 aml_append(while_ctx, aml_increment(idx));
1306 aml_append(method, while_ctx);
1308 aml_append(method, aml_store(aml_int(0x0A), aml_name("DBGB")));
1309 aml_append(scope, method);
1311 aml_append(table, scope);
1314 static Aml *build_link_dev(const char *name, uint8_t uid, Aml *reg)
1319 uint32_t irqs[] = {5, 10, 11};
1321 dev = aml_device("%s", name);
1322 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C0F")));
1323 aml_append(dev, aml_name_decl("_UID", aml_int(uid)));
1325 crs = aml_resource_template();
1326 aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL, AML_ACTIVE_HIGH,
1327 AML_SHARED, irqs, ARRAY_SIZE(irqs)));
1328 aml_append(dev, aml_name_decl("_PRS", crs));
1330 method = aml_method("_STA", 0, AML_NOTSERIALIZED);
1331 aml_append(method, aml_return(aml_call1("IQST", reg)));
1332 aml_append(dev, method);
1334 method = aml_method("_DIS", 0, AML_NOTSERIALIZED);
1335 aml_append(method, aml_or(reg, aml_int(0x80), reg));
1336 aml_append(dev, method);
1338 method = aml_method("_CRS", 0, AML_NOTSERIALIZED);
1339 aml_append(method, aml_return(aml_call1("IQCR", reg)));
1340 aml_append(dev, method);
1342 method = aml_method("_SRS", 1, AML_NOTSERIALIZED);
1343 aml_append(method, aml_create_dword_field(aml_arg(0), aml_int(5), "PRRI"));
1344 aml_append(method, aml_store(aml_name("PRRI"), reg));
1345 aml_append(dev, method);
1350 static Aml *build_gsi_link_dev(const char *name, uint8_t uid, uint8_t gsi)
1357 dev = aml_device("%s", name);
1358 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C0F")));
1359 aml_append(dev, aml_name_decl("_UID", aml_int(uid)));
1361 crs = aml_resource_template();
1363 aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL, AML_ACTIVE_HIGH,
1364 AML_SHARED, &irqs, 1));
1365 aml_append(dev, aml_name_decl("_PRS", crs));
1367 aml_append(dev, aml_name_decl("_CRS", crs));
1370 * _DIS can be no-op because the interrupt cannot be disabled.
1372 method = aml_method("_DIS", 0, AML_NOTSERIALIZED);
1373 aml_append(dev, method);
1375 method = aml_method("_SRS", 1, AML_NOTSERIALIZED);
1376 aml_append(dev, method);
1381 /* _CRS method - get current settings */
1382 static Aml *build_iqcr_method(bool is_piix4)
1386 Aml *method = aml_method("IQCR", 1, AML_SERIALIZED);
1387 Aml *crs = aml_resource_template();
1390 aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL,
1391 AML_ACTIVE_HIGH, AML_SHARED, &irqs, 1));
1392 aml_append(method, aml_name_decl("PRR0", crs));
1395 aml_create_dword_field(aml_name("PRR0"), aml_int(5), "PRRI"));
1398 if_ctx = aml_if(aml_lless(aml_arg(0), aml_int(0x80)));
1399 aml_append(if_ctx, aml_store(aml_arg(0), aml_name("PRRI")));
1400 aml_append(method, if_ctx);
1403 aml_store(aml_and(aml_arg(0), aml_int(0xF), NULL),
1407 aml_append(method, aml_return(aml_name("PRR0")));
1411 /* _STA method - get status */
1412 static Aml *build_irq_status_method(void)
1415 Aml *method = aml_method("IQST", 1, AML_NOTSERIALIZED);
1417 if_ctx = aml_if(aml_and(aml_int(0x80), aml_arg(0), NULL));
1418 aml_append(if_ctx, aml_return(aml_int(0x09)));
1419 aml_append(method, if_ctx);
1420 aml_append(method, aml_return(aml_int(0x0B)));
1424 static void build_piix4_pci0_int(Aml *table)
1431 Aml *sb_scope = aml_scope("_SB");
1432 Aml *pci0_scope = aml_scope("PCI0");
1434 aml_append(pci0_scope, build_prt(true));
1435 aml_append(sb_scope, pci0_scope);
1437 field = aml_field("PCI0.ISA.P40C", AML_BYTE_ACC, AML_NOLOCK, AML_PRESERVE);
1438 aml_append(field, aml_named_field("PRQ0", 8));
1439 aml_append(field, aml_named_field("PRQ1", 8));
1440 aml_append(field, aml_named_field("PRQ2", 8));
1441 aml_append(field, aml_named_field("PRQ3", 8));
1442 aml_append(sb_scope, field);
1444 aml_append(sb_scope, build_irq_status_method());
1445 aml_append(sb_scope, build_iqcr_method(true));
1447 aml_append(sb_scope, build_link_dev("LNKA", 0, aml_name("PRQ0")));
1448 aml_append(sb_scope, build_link_dev("LNKB", 1, aml_name("PRQ1")));
1449 aml_append(sb_scope, build_link_dev("LNKC", 2, aml_name("PRQ2")));
1450 aml_append(sb_scope, build_link_dev("LNKD", 3, aml_name("PRQ3")));
1452 dev = aml_device("LNKS");
1454 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C0F")));
1455 aml_append(dev, aml_name_decl("_UID", aml_int(4)));
1457 crs = aml_resource_template();
1459 aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL,
1460 AML_ACTIVE_HIGH, AML_SHARED,
1462 aml_append(dev, aml_name_decl("_PRS", crs));
1464 /* The SCI cannot be disabled and is always attached to GSI 9,
1465 * so these are no-ops. We only need this link to override the
1466 * polarity to active high and match the content of the MADT.
1468 method = aml_method("_STA", 0, AML_NOTSERIALIZED);
1469 aml_append(method, aml_return(aml_int(0x0b)));
1470 aml_append(dev, method);
1472 method = aml_method("_DIS", 0, AML_NOTSERIALIZED);
1473 aml_append(dev, method);
1475 method = aml_method("_CRS", 0, AML_NOTSERIALIZED);
1476 aml_append(method, aml_return(aml_name("_PRS")));
1477 aml_append(dev, method);
1479 method = aml_method("_SRS", 1, AML_NOTSERIALIZED);
1480 aml_append(dev, method);
1482 aml_append(sb_scope, dev);
1484 aml_append(table, sb_scope);
1487 static void append_q35_prt_entry(Aml *ctx, uint32_t nr, const char *name)
1492 char base = name[3] < 'E' ? 'A' : 'E';
1493 char *s = g_strdup(name);
1494 Aml *a_nr = aml_int((nr << 16) | 0xffff);
1496 assert(strlen(s) == 4);
1498 head = name[3] - base;
1499 for (i = 0; i < 4; i++) {
1503 s[3] = base + head + i;
1504 pkg = aml_package(4);
1505 aml_append(pkg, a_nr);
1506 aml_append(pkg, aml_int(i));
1507 aml_append(pkg, aml_name("%s", s));
1508 aml_append(pkg, aml_int(0));
1509 aml_append(ctx, pkg);
1514 static Aml *build_q35_routing_table(const char *str)
1518 char *name = g_strdup_printf("%s ", str);
1520 pkg = aml_package(128);
1521 for (i = 0; i < 0x18; i++) {
1522 name[3] = 'E' + (i & 0x3);
1523 append_q35_prt_entry(pkg, i, name);
1527 append_q35_prt_entry(pkg, 0x18, name);
1529 /* INTA -> PIRQA for slot 25 - 31, see the default value of D<N>IR */
1530 for (i = 0x0019; i < 0x1e; i++) {
1532 append_q35_prt_entry(pkg, i, name);
1535 /* PCIe->PCI bridge. use PIRQ[E-H] */
1537 append_q35_prt_entry(pkg, 0x1e, name);
1539 append_q35_prt_entry(pkg, 0x1f, name);
1545 static void build_q35_pci0_int(Aml *table)
1549 Aml *sb_scope = aml_scope("_SB");
1550 Aml *pci0_scope = aml_scope("PCI0");
1552 /* Zero => PIC mode, One => APIC Mode */
1553 aml_append(table, aml_name_decl("PICF", aml_int(0)));
1554 method = aml_method("_PIC", 1, AML_NOTSERIALIZED);
1556 aml_append(method, aml_store(aml_arg(0), aml_name("PICF")));
1558 aml_append(table, method);
1560 aml_append(pci0_scope,
1561 aml_name_decl("PRTP", build_q35_routing_table("LNK")));
1562 aml_append(pci0_scope,
1563 aml_name_decl("PRTA", build_q35_routing_table("GSI")));
1565 method = aml_method("_PRT", 0, AML_NOTSERIALIZED);
1570 /* PCI IRQ routing table, example from ACPI 2.0a specification,
1572 /* Note: we provide the same info as the PCI routing
1573 table of the Bochs BIOS */
1574 if_ctx = aml_if(aml_equal(aml_name("PICF"), aml_int(0)));
1575 aml_append(if_ctx, aml_return(aml_name("PRTP")));
1576 aml_append(method, if_ctx);
1577 else_ctx = aml_else();
1578 aml_append(else_ctx, aml_return(aml_name("PRTA")));
1579 aml_append(method, else_ctx);
1581 aml_append(pci0_scope, method);
1582 aml_append(sb_scope, pci0_scope);
1584 field = aml_field("PCI0.ISA.PIRQ", AML_BYTE_ACC, AML_NOLOCK, AML_PRESERVE);
1585 aml_append(field, aml_named_field("PRQA", 8));
1586 aml_append(field, aml_named_field("PRQB", 8));
1587 aml_append(field, aml_named_field("PRQC", 8));
1588 aml_append(field, aml_named_field("PRQD", 8));
1589 aml_append(field, aml_reserved_field(0x20));
1590 aml_append(field, aml_named_field("PRQE", 8));
1591 aml_append(field, aml_named_field("PRQF", 8));
1592 aml_append(field, aml_named_field("PRQG", 8));
1593 aml_append(field, aml_named_field("PRQH", 8));
1594 aml_append(sb_scope, field);
1596 aml_append(sb_scope, build_irq_status_method());
1597 aml_append(sb_scope, build_iqcr_method(false));
1599 aml_append(sb_scope, build_link_dev("LNKA", 0, aml_name("PRQA")));
1600 aml_append(sb_scope, build_link_dev("LNKB", 1, aml_name("PRQB")));
1601 aml_append(sb_scope, build_link_dev("LNKC", 2, aml_name("PRQC")));
1602 aml_append(sb_scope, build_link_dev("LNKD", 3, aml_name("PRQD")));
1603 aml_append(sb_scope, build_link_dev("LNKE", 4, aml_name("PRQE")));
1604 aml_append(sb_scope, build_link_dev("LNKF", 5, aml_name("PRQF")));
1605 aml_append(sb_scope, build_link_dev("LNKG", 6, aml_name("PRQG")));
1606 aml_append(sb_scope, build_link_dev("LNKH", 7, aml_name("PRQH")));
1608 aml_append(sb_scope, build_gsi_link_dev("GSIA", 0x10, 0x10));
1609 aml_append(sb_scope, build_gsi_link_dev("GSIB", 0x11, 0x11));
1610 aml_append(sb_scope, build_gsi_link_dev("GSIC", 0x12, 0x12));
1611 aml_append(sb_scope, build_gsi_link_dev("GSID", 0x13, 0x13));
1612 aml_append(sb_scope, build_gsi_link_dev("GSIE", 0x14, 0x14));
1613 aml_append(sb_scope, build_gsi_link_dev("GSIF", 0x15, 0x15));
1614 aml_append(sb_scope, build_gsi_link_dev("GSIG", 0x16, 0x16));
1615 aml_append(sb_scope, build_gsi_link_dev("GSIH", 0x17, 0x17));
1617 aml_append(table, sb_scope);
1620 static void build_q35_isa_bridge(Aml *table)
1626 scope = aml_scope("_SB.PCI0");
1627 dev = aml_device("ISA");
1628 aml_append(dev, aml_name_decl("_ADR", aml_int(0x001F0000)));
1630 /* ICH9 PCI to ISA irq remapping */
1631 aml_append(dev, aml_operation_region("PIRQ", AML_PCI_CONFIG,
1632 aml_int(0x60), 0x0C));
1634 aml_append(dev, aml_operation_region("LPCD", AML_PCI_CONFIG,
1635 aml_int(0x80), 0x02));
1636 field = aml_field("LPCD", AML_ANY_ACC, AML_NOLOCK, AML_PRESERVE);
1637 aml_append(field, aml_named_field("COMA", 3));
1638 aml_append(field, aml_reserved_field(1));
1639 aml_append(field, aml_named_field("COMB", 3));
1640 aml_append(field, aml_reserved_field(1));
1641 aml_append(field, aml_named_field("LPTD", 2));
1642 aml_append(dev, field);
1644 aml_append(dev, aml_operation_region("LPCE", AML_PCI_CONFIG,
1645 aml_int(0x82), 0x02));
1647 field = aml_field("LPCE", AML_ANY_ACC, AML_NOLOCK, AML_PRESERVE);
1648 aml_append(field, aml_named_field("CAEN", 1));
1649 aml_append(field, aml_named_field("CBEN", 1));
1650 aml_append(field, aml_named_field("LPEN", 1));
1651 aml_append(dev, field);
1653 aml_append(scope, dev);
1654 aml_append(table, scope);
1657 static void build_piix4_pm(Aml *table)
1662 scope = aml_scope("_SB.PCI0");
1663 dev = aml_device("PX13");
1664 aml_append(dev, aml_name_decl("_ADR", aml_int(0x00010003)));
1666 aml_append(dev, aml_operation_region("P13C", AML_PCI_CONFIG,
1667 aml_int(0x00), 0xff));
1668 aml_append(scope, dev);
1669 aml_append(table, scope);
1672 static void build_piix4_isa_bridge(Aml *table)
1678 scope = aml_scope("_SB.PCI0");
1679 dev = aml_device("ISA");
1680 aml_append(dev, aml_name_decl("_ADR", aml_int(0x00010000)));
1682 /* PIIX PCI to ISA irq remapping */
1683 aml_append(dev, aml_operation_region("P40C", AML_PCI_CONFIG,
1684 aml_int(0x60), 0x04));
1686 field = aml_field("^PX13.P13C", AML_ANY_ACC, AML_NOLOCK, AML_PRESERVE);
1687 /* Offset(0x5f),, 7, */
1688 aml_append(field, aml_reserved_field(0x2f8));
1689 aml_append(field, aml_reserved_field(7));
1690 aml_append(field, aml_named_field("LPEN", 1));
1691 /* Offset(0x67),, 3, */
1692 aml_append(field, aml_reserved_field(0x38));
1693 aml_append(field, aml_reserved_field(3));
1694 aml_append(field, aml_named_field("CAEN", 1));
1695 aml_append(field, aml_reserved_field(3));
1696 aml_append(field, aml_named_field("CBEN", 1));
1697 aml_append(dev, field);
1699 aml_append(scope, dev);
1700 aml_append(table, scope);
1703 static void build_piix4_pci_hotplug(Aml *table)
1709 scope = aml_scope("_SB.PCI0");
1712 aml_operation_region("PCST", AML_SYSTEM_IO, aml_int(0xae00), 0x08));
1713 field = aml_field("PCST", AML_DWORD_ACC, AML_NOLOCK, AML_WRITE_AS_ZEROS);
1714 aml_append(field, aml_named_field("PCIU", 32));
1715 aml_append(field, aml_named_field("PCID", 32));
1716 aml_append(scope, field);
1719 aml_operation_region("SEJ", AML_SYSTEM_IO, aml_int(0xae08), 0x04));
1720 field = aml_field("SEJ", AML_DWORD_ACC, AML_NOLOCK, AML_WRITE_AS_ZEROS);
1721 aml_append(field, aml_named_field("B0EJ", 32));
1722 aml_append(scope, field);
1725 aml_operation_region("BNMR", AML_SYSTEM_IO, aml_int(0xae10), 0x04));
1726 field = aml_field("BNMR", AML_DWORD_ACC, AML_NOLOCK, AML_WRITE_AS_ZEROS);
1727 aml_append(field, aml_named_field("BNUM", 32));
1728 aml_append(scope, field);
1730 aml_append(scope, aml_mutex("BLCK", 0));
1732 method = aml_method("PCEJ", 2, AML_NOTSERIALIZED);
1733 aml_append(method, aml_acquire(aml_name("BLCK"), 0xFFFF));
1734 aml_append(method, aml_store(aml_arg(0), aml_name("BNUM")));
1736 aml_store(aml_shiftleft(aml_int(1), aml_arg(1)), aml_name("B0EJ")));
1737 aml_append(method, aml_release(aml_name("BLCK")));
1738 aml_append(method, aml_return(aml_int(0)));
1739 aml_append(scope, method);
1741 aml_append(table, scope);
1744 static Aml *build_q35_osc_method(void)
1750 Aml *a_cwd1 = aml_name("CDW1");
1751 Aml *a_ctrl = aml_local(0);
1753 method = aml_method("_OSC", 4, AML_NOTSERIALIZED);
1754 aml_append(method, aml_create_dword_field(aml_arg(3), aml_int(0), "CDW1"));
1756 if_ctx = aml_if(aml_equal(
1757 aml_arg(0), aml_touuid("33DB4D5B-1FF7-401C-9657-7441C03DD766")));
1758 aml_append(if_ctx, aml_create_dword_field(aml_arg(3), aml_int(4), "CDW2"));
1759 aml_append(if_ctx, aml_create_dword_field(aml_arg(3), aml_int(8), "CDW3"));
1761 aml_append(if_ctx, aml_store(aml_name("CDW3"), a_ctrl));
1764 * Always allow native PME, AER (no dependencies)
1765 * Allow SHPC (PCI bridges can have SHPC controller)
1767 aml_append(if_ctx, aml_and(a_ctrl, aml_int(0x1F), a_ctrl));
1769 if_ctx2 = aml_if(aml_lnot(aml_equal(aml_arg(1), aml_int(1))));
1770 /* Unknown revision */
1771 aml_append(if_ctx2, aml_or(a_cwd1, aml_int(0x08), a_cwd1));
1772 aml_append(if_ctx, if_ctx2);
1774 if_ctx2 = aml_if(aml_lnot(aml_equal(aml_name("CDW3"), a_ctrl)));
1775 /* Capabilities bits were masked */
1776 aml_append(if_ctx2, aml_or(a_cwd1, aml_int(0x10), a_cwd1));
1777 aml_append(if_ctx, if_ctx2);
1779 /* Update DWORD3 in the buffer */
1780 aml_append(if_ctx, aml_store(a_ctrl, aml_name("CDW3")));
1781 aml_append(method, if_ctx);
1783 else_ctx = aml_else();
1784 /* Unrecognized UUID */
1785 aml_append(else_ctx, aml_or(a_cwd1, aml_int(4), a_cwd1));
1786 aml_append(method, else_ctx);
1788 aml_append(method, aml_return(aml_arg(3)));
1793 build_dsdt(GArray *table_data, BIOSLinker *linker,
1794 AcpiPmInfo *pm, AcpiMiscInfo *misc,
1795 Range *pci_hole, Range *pci_hole64, MachineState *machine)
1797 CrsRangeEntry *entry;
1798 Aml *dsdt, *sb_scope, *scope, *dev, *method, *field, *pkg, *crs;
1799 CrsRangeSet crs_range_set;
1800 PCMachineState *pcms = PC_MACHINE(machine);
1801 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(machine);
1802 uint32_t nr_mem = machine->ram_slots;
1803 int root_bus_limit = 0xFF;
1805 TPMIf *tpm = tpm_find();
1808 dsdt = init_aml_allocator();
1810 /* Reserve space for header */
1811 acpi_data_push(dsdt->buf, sizeof(AcpiTableHeader));
1813 build_dbg_aml(dsdt);
1814 if (misc->is_piix4) {
1815 sb_scope = aml_scope("_SB");
1816 dev = aml_device("PCI0");
1817 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A03")));
1818 aml_append(dev, aml_name_decl("_ADR", aml_int(0)));
1819 aml_append(dev, aml_name_decl("_UID", aml_int(1)));
1820 aml_append(sb_scope, dev);
1821 aml_append(dsdt, sb_scope);
1823 build_hpet_aml(dsdt);
1824 build_piix4_pm(dsdt);
1825 build_piix4_isa_bridge(dsdt);
1826 build_isa_devices_aml(dsdt);
1827 build_piix4_pci_hotplug(dsdt);
1828 build_piix4_pci0_int(dsdt);
1830 sb_scope = aml_scope("_SB");
1831 dev = aml_device("PCI0");
1832 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A08")));
1833 aml_append(dev, aml_name_decl("_CID", aml_eisaid("PNP0A03")));
1834 aml_append(dev, aml_name_decl("_ADR", aml_int(0)));
1835 aml_append(dev, aml_name_decl("_UID", aml_int(1)));
1836 aml_append(dev, build_q35_osc_method());
1837 aml_append(sb_scope, dev);
1838 aml_append(dsdt, sb_scope);
1840 build_hpet_aml(dsdt);
1841 build_q35_isa_bridge(dsdt);
1842 build_isa_devices_aml(dsdt);
1843 build_q35_pci0_int(dsdt);
1846 if (pcmc->legacy_cpu_hotplug) {
1847 build_legacy_cpu_hotplug_aml(dsdt, machine, pm->cpu_hp_io_base);
1849 CPUHotplugFeatures opts = {
1850 .apci_1_compatible = true, .has_legacy_cphp = true
1852 build_cpus_aml(dsdt, machine, opts, pm->cpu_hp_io_base,
1853 "\\_SB.PCI0", "\\_GPE._E02");
1855 build_memory_hotplug_aml(dsdt, nr_mem, "\\_SB.PCI0", "\\_GPE._E03");
1857 scope = aml_scope("_GPE");
1859 aml_append(scope, aml_name_decl("_HID", aml_string("ACPI0006")));
1861 if (misc->is_piix4) {
1862 method = aml_method("_E01", 0, AML_NOTSERIALIZED);
1864 aml_acquire(aml_name("\\_SB.PCI0.BLCK"), 0xFFFF));
1865 aml_append(method, aml_call0("\\_SB.PCI0.PCNT"));
1866 aml_append(method, aml_release(aml_name("\\_SB.PCI0.BLCK")));
1867 aml_append(scope, method);
1870 if (pcms->acpi_nvdimm_state.is_enabled) {
1871 method = aml_method("_E04", 0, AML_NOTSERIALIZED);
1872 aml_append(method, aml_notify(aml_name("\\_SB.NVDR"),
1874 aml_append(scope, method);
1877 aml_append(dsdt, scope);
1879 crs_range_set_init(&crs_range_set);
1880 bus = PC_MACHINE(machine)->bus;
1882 QLIST_FOREACH(bus, &bus->child, sibling) {
1883 uint8_t bus_num = pci_bus_num(bus);
1884 uint8_t numa_node = pci_bus_numa_node(bus);
1886 /* look only for expander root buses */
1887 if (!pci_bus_is_root(bus)) {
1891 if (bus_num < root_bus_limit) {
1892 root_bus_limit = bus_num - 1;
1895 scope = aml_scope("\\_SB");
1896 dev = aml_device("PC%.02X", bus_num);
1897 aml_append(dev, aml_name_decl("_UID", aml_int(bus_num)));
1898 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A03")));
1899 aml_append(dev, aml_name_decl("_BBN", aml_int(bus_num)));
1900 if (pci_bus_is_express(bus)) {
1901 aml_append(dev, build_q35_osc_method());
1904 if (numa_node != NUMA_NODE_UNASSIGNED) {
1905 aml_append(dev, aml_name_decl("_PXM", aml_int(numa_node)));
1908 aml_append(dev, build_prt(false));
1909 crs = build_crs(PCI_HOST_BRIDGE(BUS(bus)->parent), &crs_range_set);
1910 aml_append(dev, aml_name_decl("_CRS", crs));
1911 aml_append(scope, dev);
1912 aml_append(dsdt, scope);
1916 scope = aml_scope("\\_SB.PCI0");
1917 /* build PCI0._CRS */
1918 crs = aml_resource_template();
1920 aml_word_bus_number(AML_MIN_FIXED, AML_MAX_FIXED, AML_POS_DECODE,
1921 0x0000, 0x0, root_bus_limit,
1922 0x0000, root_bus_limit + 1));
1923 aml_append(crs, aml_io(AML_DECODE16, 0x0CF8, 0x0CF8, 0x01, 0x08));
1926 aml_word_io(AML_MIN_FIXED, AML_MAX_FIXED,
1927 AML_POS_DECODE, AML_ENTIRE_RANGE,
1928 0x0000, 0x0000, 0x0CF7, 0x0000, 0x0CF8));
1930 crs_replace_with_free_ranges(crs_range_set.io_ranges, 0x0D00, 0xFFFF);
1931 for (i = 0; i < crs_range_set.io_ranges->len; i++) {
1932 entry = g_ptr_array_index(crs_range_set.io_ranges, i);
1934 aml_word_io(AML_MIN_FIXED, AML_MAX_FIXED,
1935 AML_POS_DECODE, AML_ENTIRE_RANGE,
1936 0x0000, entry->base, entry->limit,
1937 0x0000, entry->limit - entry->base + 1));
1941 aml_dword_memory(AML_POS_DECODE, AML_MIN_FIXED, AML_MAX_FIXED,
1942 AML_CACHEABLE, AML_READ_WRITE,
1943 0, 0x000A0000, 0x000BFFFF, 0, 0x00020000));
1945 crs_replace_with_free_ranges(crs_range_set.mem_ranges,
1946 range_lob(pci_hole),
1947 range_upb(pci_hole));
1948 for (i = 0; i < crs_range_set.mem_ranges->len; i++) {
1949 entry = g_ptr_array_index(crs_range_set.mem_ranges, i);
1951 aml_dword_memory(AML_POS_DECODE, AML_MIN_FIXED, AML_MAX_FIXED,
1952 AML_NON_CACHEABLE, AML_READ_WRITE,
1953 0, entry->base, entry->limit,
1954 0, entry->limit - entry->base + 1));
1957 if (!range_is_empty(pci_hole64)) {
1958 crs_replace_with_free_ranges(crs_range_set.mem_64bit_ranges,
1959 range_lob(pci_hole64),
1960 range_upb(pci_hole64));
1961 for (i = 0; i < crs_range_set.mem_64bit_ranges->len; i++) {
1962 entry = g_ptr_array_index(crs_range_set.mem_64bit_ranges, i);
1964 aml_qword_memory(AML_POS_DECODE, AML_MIN_FIXED,
1966 AML_CACHEABLE, AML_READ_WRITE,
1967 0, entry->base, entry->limit,
1968 0, entry->limit - entry->base + 1));
1972 if (TPM_IS_TIS(tpm_find())) {
1973 aml_append(crs, aml_memory32_fixed(TPM_TIS_ADDR_BASE,
1974 TPM_TIS_ADDR_SIZE, AML_READ_WRITE));
1976 aml_append(scope, aml_name_decl("_CRS", crs));
1978 /* reserve GPE0 block resources */
1979 dev = aml_device("GPE0");
1980 aml_append(dev, aml_name_decl("_HID", aml_string("PNP0A06")));
1981 aml_append(dev, aml_name_decl("_UID", aml_string("GPE0 resources")));
1982 /* device present, functioning, decoding, not shown in UI */
1983 aml_append(dev, aml_name_decl("_STA", aml_int(0xB)));
1984 crs = aml_resource_template();
1988 pm->fadt.gpe0_blk.address,
1989 pm->fadt.gpe0_blk.address,
1991 pm->fadt.gpe0_blk.bit_width / 8)
1993 aml_append(dev, aml_name_decl("_CRS", crs));
1994 aml_append(scope, dev);
1996 crs_range_set_free(&crs_range_set);
1998 /* reserve PCIHP resources */
1999 if (pm->pcihp_io_len) {
2000 dev = aml_device("PHPR");
2001 aml_append(dev, aml_name_decl("_HID", aml_string("PNP0A06")));
2003 aml_name_decl("_UID", aml_string("PCI Hotplug resources")));
2004 /* device present, functioning, decoding, not shown in UI */
2005 aml_append(dev, aml_name_decl("_STA", aml_int(0xB)));
2006 crs = aml_resource_template();
2008 aml_io(AML_DECODE16, pm->pcihp_io_base, pm->pcihp_io_base, 1,
2011 aml_append(dev, aml_name_decl("_CRS", crs));
2012 aml_append(scope, dev);
2014 aml_append(dsdt, scope);
2016 /* create S3_ / S4_ / S5_ packages if necessary */
2017 scope = aml_scope("\\");
2018 if (!pm->s3_disabled) {
2019 pkg = aml_package(4);
2020 aml_append(pkg, aml_int(1)); /* PM1a_CNT.SLP_TYP */
2021 aml_append(pkg, aml_int(1)); /* PM1b_CNT.SLP_TYP, FIXME: not impl. */
2022 aml_append(pkg, aml_int(0)); /* reserved */
2023 aml_append(pkg, aml_int(0)); /* reserved */
2024 aml_append(scope, aml_name_decl("_S3", pkg));
2027 if (!pm->s4_disabled) {
2028 pkg = aml_package(4);
2029 aml_append(pkg, aml_int(pm->s4_val)); /* PM1a_CNT.SLP_TYP */
2030 /* PM1b_CNT.SLP_TYP, FIXME: not impl. */
2031 aml_append(pkg, aml_int(pm->s4_val));
2032 aml_append(pkg, aml_int(0)); /* reserved */
2033 aml_append(pkg, aml_int(0)); /* reserved */
2034 aml_append(scope, aml_name_decl("_S4", pkg));
2037 pkg = aml_package(4);
2038 aml_append(pkg, aml_int(0)); /* PM1a_CNT.SLP_TYP */
2039 aml_append(pkg, aml_int(0)); /* PM1b_CNT.SLP_TYP not impl. */
2040 aml_append(pkg, aml_int(0)); /* reserved */
2041 aml_append(pkg, aml_int(0)); /* reserved */
2042 aml_append(scope, aml_name_decl("_S5", pkg));
2043 aml_append(dsdt, scope);
2045 /* create fw_cfg node, unconditionally */
2047 /* when using port i/o, the 8-bit data register *always* overlaps
2048 * with half of the 16-bit control register. Hence, the total size
2049 * of the i/o region used is FW_CFG_CTL_SIZE; when using DMA, the
2050 * DMA control register is located at FW_CFG_DMA_IO_BASE + 4 */
2051 uint8_t io_size = object_property_get_bool(OBJECT(pcms->fw_cfg),
2052 "dma_enabled", NULL) ?
2053 ROUND_UP(FW_CFG_CTL_SIZE, 4) + sizeof(dma_addr_t) :
2056 scope = aml_scope("\\_SB.PCI0");
2057 dev = aml_device("FWCF");
2059 aml_append(dev, aml_name_decl("_HID", aml_string("QEMU0002")));
2061 /* device present, functioning, decoding, not shown in UI */
2062 aml_append(dev, aml_name_decl("_STA", aml_int(0xB)));
2064 crs = aml_resource_template();
2066 aml_io(AML_DECODE16, FW_CFG_IO_BASE, FW_CFG_IO_BASE, 0x01, io_size)
2068 aml_append(dev, aml_name_decl("_CRS", crs));
2070 aml_append(scope, dev);
2071 aml_append(dsdt, scope);
2074 if (misc->applesmc_io_base) {
2075 scope = aml_scope("\\_SB.PCI0.ISA");
2076 dev = aml_device("SMC");
2078 aml_append(dev, aml_name_decl("_HID", aml_eisaid("APP0001")));
2079 /* device present, functioning, decoding, not shown in UI */
2080 aml_append(dev, aml_name_decl("_STA", aml_int(0xB)));
2082 crs = aml_resource_template();
2084 aml_io(AML_DECODE16, misc->applesmc_io_base, misc->applesmc_io_base,
2085 0x01, APPLESMC_MAX_DATA_LENGTH)
2087 aml_append(crs, aml_irq_no_flags(6));
2088 aml_append(dev, aml_name_decl("_CRS", crs));
2090 aml_append(scope, dev);
2091 aml_append(dsdt, scope);
2094 if (misc->pvpanic_port) {
2095 scope = aml_scope("\\_SB.PCI0.ISA");
2097 dev = aml_device("PEVT");
2098 aml_append(dev, aml_name_decl("_HID", aml_string("QEMU0001")));
2100 crs = aml_resource_template();
2102 aml_io(AML_DECODE16, misc->pvpanic_port, misc->pvpanic_port, 1, 1)
2104 aml_append(dev, aml_name_decl("_CRS", crs));
2106 aml_append(dev, aml_operation_region("PEOR", AML_SYSTEM_IO,
2107 aml_int(misc->pvpanic_port), 1));
2108 field = aml_field("PEOR", AML_BYTE_ACC, AML_NOLOCK, AML_PRESERVE);
2109 aml_append(field, aml_named_field("PEPT", 8));
2110 aml_append(dev, field);
2112 /* device present, functioning, decoding, shown in UI */
2113 aml_append(dev, aml_name_decl("_STA", aml_int(0xF)));
2115 method = aml_method("RDPT", 0, AML_NOTSERIALIZED);
2116 aml_append(method, aml_store(aml_name("PEPT"), aml_local(0)));
2117 aml_append(method, aml_return(aml_local(0)));
2118 aml_append(dev, method);
2120 method = aml_method("WRPT", 1, AML_NOTSERIALIZED);
2121 aml_append(method, aml_store(aml_arg(0), aml_name("PEPT")));
2122 aml_append(dev, method);
2124 aml_append(scope, dev);
2125 aml_append(dsdt, scope);
2128 sb_scope = aml_scope("\\_SB");
2133 pci_host = acpi_get_i386_pci_host();
2135 bus = PCI_HOST_BRIDGE(pci_host)->bus;
2139 Aml *scope = aml_scope("PCI0");
2140 /* Scan all PCI buses. Generate tables to support hotplug. */
2141 build_append_pci_bus_devices(scope, bus, pm->pcihp_bridge_en);
2143 if (TPM_IS_TIS(tpm)) {
2144 dev = aml_device("ISA.TPM");
2145 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C31")));
2146 aml_append(dev, aml_name_decl("_STA", aml_int(0xF)));
2147 crs = aml_resource_template();
2148 aml_append(crs, aml_memory32_fixed(TPM_TIS_ADDR_BASE,
2149 TPM_TIS_ADDR_SIZE, AML_READ_WRITE));
2151 FIXME: TPM_TIS_IRQ=5 conflicts with PNP0C0F irqs,
2152 Rewrite to take IRQ from TPM device model and
2153 fix default IRQ value there to use some unused IRQ
2155 /* aml_append(crs, aml_irq_no_flags(TPM_TIS_IRQ)); */
2156 aml_append(dev, aml_name_decl("_CRS", crs));
2158 tpm_build_ppi_acpi(tpm, dev);
2160 aml_append(scope, dev);
2163 aml_append(sb_scope, scope);
2167 if (TPM_IS_CRB(tpm)) {
2168 dev = aml_device("TPM");
2169 aml_append(dev, aml_name_decl("_HID", aml_string("MSFT0101")));
2170 crs = aml_resource_template();
2171 aml_append(crs, aml_memory32_fixed(TPM_CRB_ADDR_BASE,
2172 TPM_CRB_ADDR_SIZE, AML_READ_WRITE));
2173 aml_append(dev, aml_name_decl("_CRS", crs));
2175 method = aml_method("_STA", 0, AML_NOTSERIALIZED);
2176 aml_append(method, aml_return(aml_int(0x0f)));
2177 aml_append(dev, method);
2179 tpm_build_ppi_acpi(tpm, dev);
2181 aml_append(sb_scope, dev);
2184 aml_append(dsdt, sb_scope);
2186 /* copy AML table into ACPI tables blob and patch header there */
2187 g_array_append_vals(table_data, dsdt->buf->data, dsdt->buf->len);
2188 build_header(linker, table_data,
2189 (void *)(table_data->data + table_data->len - dsdt->buf->len),
2190 "DSDT", dsdt->buf->len, 1, NULL, NULL);
2191 free_aml_allocator();
2195 build_hpet(GArray *table_data, BIOSLinker *linker)
2199 hpet = acpi_data_push(table_data, sizeof(*hpet));
2200 /* Note timer_block_id value must be kept in sync with value advertised by
2203 hpet->timer_block_id = cpu_to_le32(0x8086a201);
2204 hpet->addr.address = cpu_to_le64(HPET_BASE);
2205 build_header(linker, table_data,
2206 (void *)hpet, "HPET", sizeof(*hpet), 1, NULL, NULL);
2210 build_tpm_tcpa(GArray *table_data, BIOSLinker *linker, GArray *tcpalog)
2212 Acpi20Tcpa *tcpa = acpi_data_push(table_data, sizeof *tcpa);
2213 unsigned log_addr_size = sizeof(tcpa->log_area_start_address);
2214 unsigned log_addr_offset =
2215 (char *)&tcpa->log_area_start_address - table_data->data;
2217 tcpa->platform_class = cpu_to_le16(TPM_TCPA_ACPI_CLASS_CLIENT);
2218 tcpa->log_area_minimum_length = cpu_to_le32(TPM_LOG_AREA_MINIMUM_SIZE);
2219 acpi_data_push(tcpalog, le32_to_cpu(tcpa->log_area_minimum_length));
2221 bios_linker_loader_alloc(linker, ACPI_BUILD_TPMLOG_FILE, tcpalog, 1,
2222 false /* high memory */);
2224 /* log area start address to be filled by Guest linker */
2225 bios_linker_loader_add_pointer(linker,
2226 ACPI_BUILD_TABLE_FILE, log_addr_offset, log_addr_size,
2227 ACPI_BUILD_TPMLOG_FILE, 0);
2229 build_header(linker, table_data,
2230 (void *)tcpa, "TCPA", sizeof(*tcpa), 2, NULL, NULL);
2234 build_tpm2(GArray *table_data, BIOSLinker *linker, GArray *tcpalog)
2236 Acpi20TPM2 *tpm2_ptr = acpi_data_push(table_data, sizeof *tpm2_ptr);
2237 unsigned log_addr_size = sizeof(tpm2_ptr->log_area_start_address);
2238 unsigned log_addr_offset =
2239 (char *)&tpm2_ptr->log_area_start_address - table_data->data;
2241 tpm2_ptr->platform_class = cpu_to_le16(TPM2_ACPI_CLASS_CLIENT);
2242 if (TPM_IS_TIS(tpm_find())) {
2243 tpm2_ptr->control_area_address = cpu_to_le64(0);
2244 tpm2_ptr->start_method = cpu_to_le32(TPM2_START_METHOD_MMIO);
2245 } else if (TPM_IS_CRB(tpm_find())) {
2246 tpm2_ptr->control_area_address = cpu_to_le64(TPM_CRB_ADDR_CTRL);
2247 tpm2_ptr->start_method = cpu_to_le32(TPM2_START_METHOD_CRB);
2249 g_warn_if_reached();
2252 tpm2_ptr->log_area_minimum_length =
2253 cpu_to_le32(TPM_LOG_AREA_MINIMUM_SIZE);
2255 /* log area start address to be filled by Guest linker */
2256 bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
2257 log_addr_offset, log_addr_size,
2258 ACPI_BUILD_TPMLOG_FILE, 0);
2259 build_header(linker, table_data,
2260 (void *)tpm2_ptr, "TPM2", sizeof(*tpm2_ptr), 4, NULL, NULL);
2263 #define HOLE_640K_START (640 * KiB)
2264 #define HOLE_640K_END (1 * MiB)
2267 build_srat(GArray *table_data, BIOSLinker *linker, MachineState *machine)
2269 AcpiSystemResourceAffinityTable *srat;
2270 AcpiSratMemoryAffinity *numamem;
2273 int srat_start, numa_start, slots;
2274 uint64_t mem_len, mem_base, next_base;
2275 MachineClass *mc = MACHINE_GET_CLASS(machine);
2276 const CPUArchIdList *apic_ids = mc->possible_cpu_arch_ids(machine);
2277 PCMachineState *pcms = PC_MACHINE(machine);
2278 ram_addr_t hotplugabble_address_space_size =
2279 object_property_get_int(OBJECT(pcms), PC_MACHINE_DEVMEM_REGION_SIZE,
2282 srat_start = table_data->len;
2284 srat = acpi_data_push(table_data, sizeof *srat);
2285 srat->reserved1 = cpu_to_le32(1);
2287 for (i = 0; i < apic_ids->len; i++) {
2288 int node_id = apic_ids->cpus[i].props.node_id;
2289 uint32_t apic_id = apic_ids->cpus[i].arch_id;
2291 if (apic_id < 255) {
2292 AcpiSratProcessorAffinity *core;
2294 core = acpi_data_push(table_data, sizeof *core);
2295 core->type = ACPI_SRAT_PROCESSOR_APIC;
2296 core->length = sizeof(*core);
2297 core->local_apic_id = apic_id;
2298 core->proximity_lo = node_id;
2299 memset(core->proximity_hi, 0, 3);
2300 core->local_sapic_eid = 0;
2301 core->flags = cpu_to_le32(1);
2303 AcpiSratProcessorX2ApicAffinity *core;
2305 core = acpi_data_push(table_data, sizeof *core);
2306 core->type = ACPI_SRAT_PROCESSOR_x2APIC;
2307 core->length = sizeof(*core);
2308 core->x2apic_id = cpu_to_le32(apic_id);
2309 core->proximity_domain = cpu_to_le32(node_id);
2310 core->flags = cpu_to_le32(1);
2315 /* the memory map is a bit tricky, it contains at least one hole
2316 * from 640k-1M and possibly another one from 3.5G-4G.
2319 numa_start = table_data->len;
2321 for (i = 1; i < pcms->numa_nodes + 1; ++i) {
2322 mem_base = next_base;
2323 mem_len = pcms->node_mem[i - 1];
2324 next_base = mem_base + mem_len;
2326 /* Cut out the 640K hole */
2327 if (mem_base <= HOLE_640K_START &&
2328 next_base > HOLE_640K_START) {
2329 mem_len -= next_base - HOLE_640K_START;
2331 numamem = acpi_data_push(table_data, sizeof *numamem);
2332 build_srat_memory(numamem, mem_base, mem_len, i - 1,
2333 MEM_AFFINITY_ENABLED);
2336 /* Check for the rare case: 640K < RAM < 1M */
2337 if (next_base <= HOLE_640K_END) {
2338 next_base = HOLE_640K_END;
2341 mem_base = HOLE_640K_END;
2342 mem_len = next_base - HOLE_640K_END;
2345 /* Cut out the ACPI_PCI hole */
2346 if (mem_base <= pcms->below_4g_mem_size &&
2347 next_base > pcms->below_4g_mem_size) {
2348 mem_len -= next_base - pcms->below_4g_mem_size;
2350 numamem = acpi_data_push(table_data, sizeof *numamem);
2351 build_srat_memory(numamem, mem_base, mem_len, i - 1,
2352 MEM_AFFINITY_ENABLED);
2354 mem_base = 1ULL << 32;
2355 mem_len = next_base - pcms->below_4g_mem_size;
2356 next_base = mem_base + mem_len;
2360 numamem = acpi_data_push(table_data, sizeof *numamem);
2361 build_srat_memory(numamem, mem_base, mem_len, i - 1,
2362 MEM_AFFINITY_ENABLED);
2365 slots = (table_data->len - numa_start) / sizeof *numamem;
2366 for (; slots < pcms->numa_nodes + 2; slots++) {
2367 numamem = acpi_data_push(table_data, sizeof *numamem);
2368 build_srat_memory(numamem, 0, 0, 0, MEM_AFFINITY_NOFLAGS);
2372 * Entry is required for Windows to enable memory hotplug in OS
2373 * and for Linux to enable SWIOTLB when booted with less than
2374 * 4G of RAM. Windows works better if the entry sets proximity
2375 * to the highest NUMA node in the machine.
2376 * Memory devices may override proximity set by this entry,
2377 * providing _PXM method if necessary.
2379 if (hotplugabble_address_space_size) {
2380 numamem = acpi_data_push(table_data, sizeof *numamem);
2381 build_srat_memory(numamem, machine->device_memory->base,
2382 hotplugabble_address_space_size, pcms->numa_nodes - 1,
2383 MEM_AFFINITY_HOTPLUGGABLE | MEM_AFFINITY_ENABLED);
2386 build_header(linker, table_data,
2387 (void *)(table_data->data + srat_start),
2389 table_data->len - srat_start, 1, NULL, NULL);
2393 build_mcfg_q35(GArray *table_data, BIOSLinker *linker, AcpiMcfgInfo *info)
2395 AcpiTableMcfg *mcfg;
2397 int len = sizeof(*mcfg) + 1 * sizeof(mcfg->allocation[0]);
2399 mcfg = acpi_data_push(table_data, len);
2400 mcfg->allocation[0].address = cpu_to_le64(info->mcfg_base);
2401 /* Only a single allocation so no need to play with segments */
2402 mcfg->allocation[0].pci_segment = cpu_to_le16(0);
2403 mcfg->allocation[0].start_bus_number = 0;
2404 mcfg->allocation[0].end_bus_number = PCIE_MMCFG_BUS(info->mcfg_size - 1);
2406 /* MCFG is used for ECAM which can be enabled or disabled by guest.
2407 * To avoid table size changes (which create migration issues),
2408 * always create the table even if there are no allocations,
2409 * but set the signature to a reserved value in this case.
2410 * ACPI spec requires OSPMs to ignore such tables.
2412 if (info->mcfg_base == PCIE_BASE_ADDR_UNMAPPED) {
2413 /* Reserved signature: ignored by OSPM */
2418 build_header(linker, table_data, (void *)mcfg, sig, len, 1, NULL, NULL);
2422 * VT-d spec 8.1 DMA Remapping Reporting Structure
2423 * (version Oct. 2014 or later)
2426 build_dmar_q35(GArray *table_data, BIOSLinker *linker)
2428 int dmar_start = table_data->len;
2430 AcpiTableDmar *dmar;
2431 AcpiDmarHardwareUnit *drhd;
2432 AcpiDmarRootPortATS *atsr;
2433 uint8_t dmar_flags = 0;
2434 X86IOMMUState *iommu = x86_iommu_get_default();
2435 AcpiDmarDeviceScope *scope = NULL;
2436 /* Root complex IOAPIC use one path[0] only */
2437 size_t ioapic_scope_size = sizeof(*scope) + sizeof(scope->path[0]);
2438 IntelIOMMUState *intel_iommu = INTEL_IOMMU_DEVICE(iommu);
2441 if (x86_iommu_ir_supported(iommu)) {
2442 dmar_flags |= 0x1; /* Flags: 0x1: INT_REMAP */
2445 dmar = acpi_data_push(table_data, sizeof(*dmar));
2446 dmar->host_address_width = intel_iommu->aw_bits - 1;
2447 dmar->flags = dmar_flags;
2449 /* DMAR Remapping Hardware Unit Definition structure */
2450 drhd = acpi_data_push(table_data, sizeof(*drhd) + ioapic_scope_size);
2451 drhd->type = cpu_to_le16(ACPI_DMAR_TYPE_HARDWARE_UNIT);
2452 drhd->length = cpu_to_le16(sizeof(*drhd) + ioapic_scope_size);
2453 drhd->flags = ACPI_DMAR_INCLUDE_PCI_ALL;
2454 drhd->pci_segment = cpu_to_le16(0);
2455 drhd->address = cpu_to_le64(Q35_HOST_BRIDGE_IOMMU_ADDR);
2457 /* Scope definition for the root-complex IOAPIC. See VT-d spec
2458 * 8.3.1 (version Oct. 2014 or later). */
2459 scope = &drhd->scope[0];
2460 scope->entry_type = 0x03; /* Type: 0x03 for IOAPIC */
2461 scope->length = ioapic_scope_size;
2462 scope->enumeration_id = ACPI_BUILD_IOAPIC_ID;
2463 scope->bus = Q35_PSEUDO_BUS_PLATFORM;
2464 scope->path[0].device = PCI_SLOT(Q35_PSEUDO_DEVFN_IOAPIC);
2465 scope->path[0].function = PCI_FUNC(Q35_PSEUDO_DEVFN_IOAPIC);
2467 if (iommu->dt_supported) {
2468 atsr = acpi_data_push(table_data, sizeof(*atsr));
2469 atsr->type = cpu_to_le16(ACPI_DMAR_TYPE_ATSR);
2470 atsr->length = cpu_to_le16(sizeof(*atsr));
2471 atsr->flags = ACPI_DMAR_ATSR_ALL_PORTS;
2472 atsr->pci_segment = cpu_to_le16(0);
2475 build_header(linker, table_data, (void *)(table_data->data + dmar_start),
2476 "DMAR", table_data->len - dmar_start, 1, NULL, NULL);
2479 * IVRS table as specified in AMD IOMMU Specification v2.62, Section 5.2
2480 * accessible here http://support.amd.com/TechDocs/48882_IOMMU.pdf
2482 #define IOAPIC_SB_DEVID (uint64_t)PCI_BUILD_BDF(0, PCI_DEVFN(0x14, 0))
2485 build_amd_iommu(GArray *table_data, BIOSLinker *linker)
2487 int ivhd_table_len = 28;
2488 int iommu_start = table_data->len;
2489 AMDVIState *s = AMD_IOMMU_DEVICE(x86_iommu_get_default());
2492 acpi_data_push(table_data, sizeof(AcpiTableHeader));
2493 /* IVinfo - IO virtualization information common to all
2494 * IOMMU units in a system
2496 build_append_int_noprefix(table_data, 40UL << 8/* PASize */, 4);
2498 build_append_int_noprefix(table_data, 0, 8);
2500 /* IVHD definition - type 10h */
2501 build_append_int_noprefix(table_data, 0x10, 1);
2502 /* virtualization flags */
2503 build_append_int_noprefix(table_data,
2504 (1UL << 0) | /* HtTunEn */
2505 (1UL << 4) | /* iotblSup */
2506 (1UL << 6) | /* PrefSup */
2507 (1UL << 7), /* PPRSup */
2511 * When interrupt remapping is supported, we add a special IVHD device
2514 if (x86_iommu_ir_supported(x86_iommu_get_default())) {
2515 ivhd_table_len += 8;
2518 build_append_int_noprefix(table_data, ivhd_table_len, 2);
2520 build_append_int_noprefix(table_data, s->devid, 2);
2521 /* Capability offset */
2522 build_append_int_noprefix(table_data, s->capab_offset, 2);
2523 /* IOMMU base address */
2524 build_append_int_noprefix(table_data, s->mmio.addr, 8);
2525 /* PCI Segment Group */
2526 build_append_int_noprefix(table_data, 0, 2);
2528 build_append_int_noprefix(table_data, 0, 2);
2529 /* IOMMU Feature Reporting */
2530 build_append_int_noprefix(table_data,
2531 (48UL << 30) | /* HATS */
2532 (48UL << 28) | /* GATS */
2533 (1UL << 2) | /* GTSup */
2534 (1UL << 6), /* GASup */
2537 * Type 1 device entry reporting all devices
2538 * These are 4-byte device entries currently reporting the range of
2539 * Refer to Spec - Table 95:IVHD Device Entry Type Codes(4-byte)
2541 build_append_int_noprefix(table_data, 0x0000001, 4);
2544 * Add a special IVHD device type.
2545 * Refer to spec - Table 95: IVHD device entry type codes
2547 * Linux IOMMU driver checks for the special IVHD device (type IO-APIC).
2548 * See Linux kernel commit 'c2ff5cf5294bcbd7fa50f7d860e90a66db7e5059'
2550 if (x86_iommu_ir_supported(x86_iommu_get_default())) {
2551 build_append_int_noprefix(table_data,
2552 (0x1ull << 56) | /* type IOAPIC */
2553 (IOAPIC_SB_DEVID << 40) | /* IOAPIC devid */
2554 0x48, /* special device */
2558 build_header(linker, table_data, (void *)(table_data->data + iommu_start),
2559 "IVRS", table_data->len - iommu_start, 1, NULL, NULL);
2563 struct AcpiBuildState {
2564 /* Copy of table in RAM (for patching). */
2565 MemoryRegion *table_mr;
2566 /* Is table patched? */
2569 MemoryRegion *rsdp_mr;
2570 MemoryRegion *linker_mr;
2573 static bool acpi_get_mcfg(AcpiMcfgInfo *mcfg)
2578 pci_host = acpi_get_i386_pci_host();
2581 o = object_property_get_qobject(pci_host, PCIE_HOST_MCFG_BASE, NULL);
2585 mcfg->mcfg_base = qnum_get_uint(qobject_to(QNum, o));
2588 o = object_property_get_qobject(pci_host, PCIE_HOST_MCFG_SIZE, NULL);
2590 mcfg->mcfg_size = qnum_get_uint(qobject_to(QNum, o));
2596 void acpi_build(AcpiBuildTables *tables, MachineState *machine)
2598 PCMachineState *pcms = PC_MACHINE(machine);
2599 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
2600 GArray *table_offsets;
2601 unsigned facs, dsdt, rsdt, fadt;
2605 Range pci_hole, pci_hole64;
2608 GArray *tables_blob = tables->table_data;
2609 AcpiSlicOem slic_oem = { .id = NULL, .table_id = NULL };
2610 Object *vmgenid_dev;
2612 acpi_get_pm_info(&pm);
2613 acpi_get_misc_info(&misc);
2614 acpi_get_pci_holes(&pci_hole, &pci_hole64);
2615 acpi_get_slic_oem(&slic_oem);
2617 table_offsets = g_array_new(false, true /* clear */,
2619 ACPI_BUILD_DPRINTF("init ACPI tables\n");
2621 bios_linker_loader_alloc(tables->linker,
2622 ACPI_BUILD_TABLE_FILE, tables_blob,
2623 64 /* Ensure FACS is aligned */,
2624 false /* high memory */);
2627 * FACS is pointed to by FADT.
2628 * We place it first since it's the only table that has alignment
2631 facs = tables_blob->len;
2632 build_facs(tables_blob, tables->linker);
2634 /* DSDT is pointed to by FADT */
2635 dsdt = tables_blob->len;
2636 build_dsdt(tables_blob, tables->linker, &pm, &misc,
2637 &pci_hole, &pci_hole64, machine);
2639 /* Count the size of the DSDT and SSDT, we will need it for legacy
2640 * sizing of ACPI tables.
2642 aml_len += tables_blob->len - dsdt;
2644 /* ACPI tables pointed to by RSDT */
2645 fadt = tables_blob->len;
2646 acpi_add_table(table_offsets, tables_blob);
2647 pm.fadt.facs_tbl_offset = &facs;
2648 pm.fadt.dsdt_tbl_offset = &dsdt;
2649 pm.fadt.xdsdt_tbl_offset = &dsdt;
2650 build_fadt(tables_blob, tables->linker, &pm.fadt,
2651 slic_oem.id, slic_oem.table_id);
2652 aml_len += tables_blob->len - fadt;
2654 acpi_add_table(table_offsets, tables_blob);
2655 build_madt(tables_blob, tables->linker, pcms);
2657 vmgenid_dev = find_vmgenid_dev();
2659 acpi_add_table(table_offsets, tables_blob);
2660 vmgenid_build_acpi(VMGENID(vmgenid_dev), tables_blob,
2661 tables->vmgenid, tables->linker);
2664 if (misc.has_hpet) {
2665 acpi_add_table(table_offsets, tables_blob);
2666 build_hpet(tables_blob, tables->linker);
2668 if (misc.tpm_version != TPM_VERSION_UNSPEC) {
2669 acpi_add_table(table_offsets, tables_blob);
2670 build_tpm_tcpa(tables_blob, tables->linker, tables->tcpalog);
2672 if (misc.tpm_version == TPM_VERSION_2_0) {
2673 acpi_add_table(table_offsets, tables_blob);
2674 build_tpm2(tables_blob, tables->linker, tables->tcpalog);
2677 if (pcms->numa_nodes) {
2678 acpi_add_table(table_offsets, tables_blob);
2679 build_srat(tables_blob, tables->linker, machine);
2680 if (have_numa_distance) {
2681 acpi_add_table(table_offsets, tables_blob);
2682 build_slit(tables_blob, tables->linker);
2685 if (acpi_get_mcfg(&mcfg)) {
2686 acpi_add_table(table_offsets, tables_blob);
2687 build_mcfg_q35(tables_blob, tables->linker, &mcfg);
2689 if (x86_iommu_get_default()) {
2690 IommuType IOMMUType = x86_iommu_get_type();
2691 if (IOMMUType == TYPE_AMD) {
2692 acpi_add_table(table_offsets, tables_blob);
2693 build_amd_iommu(tables_blob, tables->linker);
2694 } else if (IOMMUType == TYPE_INTEL) {
2695 acpi_add_table(table_offsets, tables_blob);
2696 build_dmar_q35(tables_blob, tables->linker);
2699 if (pcms->acpi_nvdimm_state.is_enabled) {
2700 nvdimm_build_acpi(table_offsets, tables_blob, tables->linker,
2701 &pcms->acpi_nvdimm_state, machine->ram_slots);
2704 /* Add tables supplied by user (if any) */
2705 for (u = acpi_table_first(); u; u = acpi_table_next(u)) {
2706 unsigned len = acpi_table_len(u);
2708 acpi_add_table(table_offsets, tables_blob);
2709 g_array_append_vals(tables_blob, u, len);
2712 /* RSDT is pointed to by RSDP */
2713 rsdt = tables_blob->len;
2714 build_rsdt(tables_blob, tables->linker, table_offsets,
2715 slic_oem.id, slic_oem.table_id);
2717 /* RSDP is in FSEG memory, so allocate it separately */
2719 AcpiRsdpData rsdp_data = {
2721 .oem_id = ACPI_BUILD_APPNAME6,
2722 .xsdt_tbl_offset = NULL,
2723 .rsdt_tbl_offset = &rsdt,
2725 build_rsdp(tables->rsdp, tables->linker, &rsdp_data);
2726 if (!pcmc->rsdp_in_ram) {
2727 /* We used to allocate some extra space for RSDP revision 2 but
2728 * only used the RSDP revision 0 space. The extra bytes were
2729 * zeroed out and not used.
2730 * Here we continue wasting those extra 16 bytes to make sure we
2731 * don't break migration for machine types 2.2 and older due to
2732 * RSDP blob size mismatch.
2734 build_append_int_noprefix(tables->rsdp, 0, 16);
2738 /* We'll expose it all to Guest so we want to reduce
2739 * chance of size changes.
2741 * We used to align the tables to 4k, but of course this would
2742 * too simple to be enough. 4k turned out to be too small an
2743 * alignment very soon, and in fact it is almost impossible to
2744 * keep the table size stable for all (max_cpus, max_memory_slots)
2745 * combinations. So the table size is always 64k for pc-i440fx-2.1
2746 * and we give an error if the table grows beyond that limit.
2748 * We still have the problem of migrating from "-M pc-i440fx-2.0". For
2749 * that, we exploit the fact that QEMU 2.1 generates _smaller_ tables
2750 * than 2.0 and we can always pad the smaller tables with zeros. We can
2751 * then use the exact size of the 2.0 tables.
2753 * All this is for PIIX4, since QEMU 2.0 didn't support Q35 migration.
2755 if (pcmc->legacy_acpi_table_size) {
2756 /* Subtracting aml_len gives the size of fixed tables. Then add the
2757 * size of the PIIX4 DSDT/SSDT in QEMU 2.0.
2759 int legacy_aml_len =
2760 pcmc->legacy_acpi_table_size +
2761 ACPI_BUILD_LEGACY_CPU_AML_SIZE * pcms->apic_id_limit;
2762 int legacy_table_size =
2763 ROUND_UP(tables_blob->len - aml_len + legacy_aml_len,
2764 ACPI_BUILD_ALIGN_SIZE);
2765 if (tables_blob->len > legacy_table_size) {
2766 /* Should happen only with PCI bridges and -M pc-i440fx-2.0. */
2767 warn_report("ACPI table size %u exceeds %d bytes,"
2768 " migration may not work",
2769 tables_blob->len, legacy_table_size);
2770 error_printf("Try removing CPUs, NUMA nodes, memory slots"
2771 " or PCI bridges.");
2773 g_array_set_size(tables_blob, legacy_table_size);
2775 /* Make sure we have a buffer in case we need to resize the tables. */
2776 if (tables_blob->len > ACPI_BUILD_TABLE_SIZE / 2) {
2777 /* As of QEMU 2.1, this fires with 160 VCPUs and 255 memory slots. */
2778 warn_report("ACPI table size %u exceeds %d bytes,"
2779 " migration may not work",
2780 tables_blob->len, ACPI_BUILD_TABLE_SIZE / 2);
2781 error_printf("Try removing CPUs, NUMA nodes, memory slots"
2782 " or PCI bridges.");
2784 acpi_align_size(tables_blob, ACPI_BUILD_TABLE_SIZE);
2787 acpi_align_size(tables->linker->cmd_blob, ACPI_BUILD_ALIGN_SIZE);
2789 /* Cleanup memory that's no longer used. */
2790 g_array_free(table_offsets, true);
2793 static void acpi_ram_update(MemoryRegion *mr, GArray *data)
2795 uint32_t size = acpi_data_len(data);
2797 /* Make sure RAM size is correct - in case it got changed e.g. by migration */
2798 memory_region_ram_resize(mr, size, &error_abort);
2800 memcpy(memory_region_get_ram_ptr(mr), data->data, size);
2801 memory_region_set_dirty(mr, 0, size);
2804 static void acpi_build_update(void *build_opaque)
2806 AcpiBuildState *build_state = build_opaque;
2807 AcpiBuildTables tables;
2809 /* No state to update or already patched? Nothing to do. */
2810 if (!build_state || build_state->patched) {
2813 build_state->patched = 1;
2815 acpi_build_tables_init(&tables);
2817 acpi_build(&tables, MACHINE(qdev_get_machine()));
2819 acpi_ram_update(build_state->table_mr, tables.table_data);
2821 if (build_state->rsdp) {
2822 memcpy(build_state->rsdp, tables.rsdp->data, acpi_data_len(tables.rsdp));
2824 acpi_ram_update(build_state->rsdp_mr, tables.rsdp);
2827 acpi_ram_update(build_state->linker_mr, tables.linker->cmd_blob);
2828 acpi_build_tables_cleanup(&tables, true);
2831 static void acpi_build_reset(void *build_opaque)
2833 AcpiBuildState *build_state = build_opaque;
2834 build_state->patched = 0;
2837 static MemoryRegion *acpi_add_rom_blob(AcpiBuildState *build_state,
2838 GArray *blob, const char *name,
2841 return rom_add_blob(name, blob->data, acpi_data_len(blob), max_size, -1,
2842 name, acpi_build_update, build_state, NULL, true);
2845 static const VMStateDescription vmstate_acpi_build = {
2846 .name = "acpi_build",
2848 .minimum_version_id = 1,
2849 .fields = (VMStateField[]) {
2850 VMSTATE_UINT8(patched, AcpiBuildState),
2851 VMSTATE_END_OF_LIST()
2855 void acpi_setup(void)
2857 PCMachineState *pcms = PC_MACHINE(qdev_get_machine());
2858 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
2859 AcpiBuildTables tables;
2860 AcpiBuildState *build_state;
2861 Object *vmgenid_dev;
2863 static FwCfgTPMConfig tpm_config;
2865 if (!pcms->fw_cfg) {
2866 ACPI_BUILD_DPRINTF("No fw cfg. Bailing out.\n");
2870 if (!pcms->acpi_build_enabled) {
2871 ACPI_BUILD_DPRINTF("ACPI build disabled. Bailing out.\n");
2875 if (!acpi_enabled) {
2876 ACPI_BUILD_DPRINTF("ACPI disabled. Bailing out.\n");
2880 build_state = g_malloc0(sizeof *build_state);
2882 acpi_build_tables_init(&tables);
2883 acpi_build(&tables, MACHINE(pcms));
2885 /* Now expose it all to Guest */
2886 build_state->table_mr = acpi_add_rom_blob(build_state, tables.table_data,
2887 ACPI_BUILD_TABLE_FILE,
2888 ACPI_BUILD_TABLE_MAX_SIZE);
2889 assert(build_state->table_mr != NULL);
2891 build_state->linker_mr =
2892 acpi_add_rom_blob(build_state, tables.linker->cmd_blob,
2893 "etc/table-loader", 0);
2895 fw_cfg_add_file(pcms->fw_cfg, ACPI_BUILD_TPMLOG_FILE,
2896 tables.tcpalog->data, acpi_data_len(tables.tcpalog));
2899 if (tpm && object_property_get_bool(OBJECT(tpm), "ppi", &error_abort)) {
2900 tpm_config = (FwCfgTPMConfig) {
2901 .tpmppi_address = cpu_to_le32(TPM_PPI_ADDR_BASE),
2902 .tpm_version = tpm_get_version(tpm),
2903 .tpmppi_version = TPM_PPI_VERSION_1_30
2905 fw_cfg_add_file(pcms->fw_cfg, "etc/tpm/config",
2906 &tpm_config, sizeof tpm_config);
2909 vmgenid_dev = find_vmgenid_dev();
2911 vmgenid_add_fw_cfg(VMGENID(vmgenid_dev), pcms->fw_cfg,
2915 if (!pcmc->rsdp_in_ram) {
2917 * Keep for compatibility with old machine types.
2918 * Though RSDP is small, its contents isn't immutable, so
2919 * we'll update it along with the rest of tables on guest access.
2921 uint32_t rsdp_size = acpi_data_len(tables.rsdp);
2923 build_state->rsdp = g_memdup(tables.rsdp->data, rsdp_size);
2924 fw_cfg_add_file_callback(pcms->fw_cfg, ACPI_BUILD_RSDP_FILE,
2925 acpi_build_update, NULL, build_state,
2926 build_state->rsdp, rsdp_size, true);
2927 build_state->rsdp_mr = NULL;
2929 build_state->rsdp = NULL;
2930 build_state->rsdp_mr = acpi_add_rom_blob(build_state, tables.rsdp,
2931 ACPI_BUILD_RSDP_FILE, 0);
2934 qemu_register_reset(acpi_build_reset, build_state);
2935 acpi_build_reset(build_state);
2936 vmstate_register(NULL, 0, &vmstate_acpi_build, build_state);
2938 /* Cleanup tables but don't free the memory: we track it
2941 acpi_build_tables_cleanup(&tables, false);