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 "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 "sysemu/numa.h"
51 /* Supported chipsets: */
52 #include "hw/acpi/piix4.h"
53 #include "hw/acpi/pcihp.h"
54 #include "hw/i386/ich9.h"
55 #include "hw/pci/pci_bus.h"
56 #include "hw/pci-host/q35.h"
57 #include "hw/i386/x86-iommu.h"
59 #include "hw/acpi/aml-build.h"
61 #include "qom/qom-qobject.h"
62 #include "hw/i386/amd_iommu.h"
63 #include "hw/i386/intel_iommu.h"
65 #include "hw/acpi/ipmi.h"
67 /* These are used to size the ACPI tables for -M pc-i440fx-1.7 and
68 * -M pc-i440fx-2.0. Even if the actual amount of AML generated grows
69 * a little bit, there should be plenty of free space since the DSDT
70 * shrunk by ~1.5k between QEMU 2.0 and QEMU 2.1.
72 #define ACPI_BUILD_LEGACY_CPU_AML_SIZE 97
73 #define ACPI_BUILD_ALIGN_SIZE 0x1000
75 #define ACPI_BUILD_TABLE_SIZE 0x20000
77 /* #define DEBUG_ACPI_BUILD */
78 #ifdef DEBUG_ACPI_BUILD
79 #define ACPI_BUILD_DPRINTF(fmt, ...) \
80 do {printf("ACPI_BUILD: " fmt, ## __VA_ARGS__); } while (0)
82 #define ACPI_BUILD_DPRINTF(fmt, ...)
85 /* Default IOAPIC ID */
86 #define ACPI_BUILD_IOAPIC_ID 0x0
88 typedef struct AcpiMcfgInfo
{
93 typedef struct AcpiPmInfo
{
99 uint16_t cpu_hp_io_base
;
100 uint16_t pcihp_io_base
;
101 uint16_t pcihp_io_len
;
104 typedef struct AcpiMiscInfo
{
107 TPMVersion tpm_version
;
108 const unsigned char *dsdt_code
;
110 uint16_t pvpanic_port
;
111 uint16_t applesmc_io_base
;
114 typedef struct AcpiBuildPciBusHotplugState
{
115 GArray
*device_table
;
116 GArray
*notify_table
;
117 struct AcpiBuildPciBusHotplugState
*parent
;
118 bool pcihp_bridge_en
;
119 } AcpiBuildPciBusHotplugState
;
121 static void init_common_fadt_data(Object
*o
, AcpiFadtData
*data
)
123 uint32_t io
= object_property_get_uint(o
, ACPI_PM_PROP_PM_IO_BASE
, NULL
);
124 AmlAddressSpace as
= AML_AS_SYSTEM_IO
;
125 AcpiFadtData fadt
= {
128 (1 << ACPI_FADT_F_WBINVD
) |
129 (1 << ACPI_FADT_F_PROC_C1
) |
130 (1 << ACPI_FADT_F_SLP_BUTTON
) |
131 (1 << ACPI_FADT_F_RTC_S4
) |
132 (1 << ACPI_FADT_F_USE_PLATFORM_CLOCK
) |
133 /* APIC destination mode ("Flat Logical") has an upper limit of 8
134 * CPUs for more than 8 CPUs, "Clustered Logical" mode has to be
137 ((max_cpus
> 8) ? (1 << ACPI_FADT_F_FORCE_APIC_CLUSTER_MODEL
) : 0),
138 .int_model
= 1 /* Multiple APIC */,
139 .rtc_century
= RTC_CENTURY
,
140 .plvl2_lat
= 0xfff /* C2 state not supported */,
141 .plvl3_lat
= 0xfff /* C3 state not supported */,
142 .smi_cmd
= ACPI_PORT_SMI_CMD
,
143 .sci_int
= object_property_get_uint(o
, ACPI_PM_PROP_SCI_INT
, NULL
),
145 object_property_get_uint(o
, ACPI_PM_PROP_ACPI_ENABLE_CMD
, NULL
),
147 object_property_get_uint(o
, ACPI_PM_PROP_ACPI_DISABLE_CMD
, NULL
),
148 .pm1a_evt
= { .space_id
= as
, .bit_width
= 4 * 8, .address
= io
},
149 .pm1a_cnt
= { .space_id
= as
, .bit_width
= 2 * 8,
150 .address
= io
+ 0x04 },
151 .pm_tmr
= { .space_id
= as
, .bit_width
= 4 * 8, .address
= io
+ 0x08 },
152 .gpe0_blk
= { .space_id
= as
, .bit_width
=
153 object_property_get_uint(o
, ACPI_PM_PROP_GPE0_BLK_LEN
, NULL
) * 8,
154 .address
= object_property_get_uint(o
, ACPI_PM_PROP_GPE0_BLK
, NULL
)
160 static void acpi_get_pm_info(AcpiPmInfo
*pm
)
162 Object
*piix
= piix4_pm_find();
163 Object
*lpc
= ich9_lpc_find();
164 Object
*obj
= piix
? piix
: lpc
;
166 pm
->cpu_hp_io_base
= 0;
167 pm
->pcihp_io_base
= 0;
168 pm
->pcihp_io_len
= 0;
170 init_common_fadt_data(obj
, &pm
->fadt
);
172 /* w2k requires FADT(rev1) or it won't boot, keep PC compatible */
174 pm
->cpu_hp_io_base
= PIIX4_CPU_HOTPLUG_IO_BASE
;
176 object_property_get_uint(obj
, ACPI_PCIHP_IO_BASE_PROP
, NULL
);
178 object_property_get_uint(obj
, ACPI_PCIHP_IO_LEN_PROP
, NULL
);
181 struct AcpiGenericAddress r
= { .space_id
= AML_AS_SYSTEM_IO
,
182 .bit_width
= 8, .address
= ICH9_RST_CNT_IOPORT
};
183 pm
->fadt
.reset_reg
= r
;
184 pm
->fadt
.reset_val
= 0xf;
185 pm
->fadt
.flags
|= 1 << ACPI_FADT_F_RESET_REG_SUP
;
186 pm
->cpu_hp_io_base
= ICH9_CPU_HOTPLUG_IO_BASE
;
190 /* The above need not be conditional on machine type because the reset port
191 * happens to be the same on PIIX (pc) and ICH9 (q35). */
192 QEMU_BUILD_BUG_ON(ICH9_RST_CNT_IOPORT
!= RCR_IOPORT
);
194 /* Fill in optional s3/s4 related properties */
195 o
= object_property_get_qobject(obj
, ACPI_PM_PROP_S3_DISABLED
, NULL
);
197 pm
->s3_disabled
= qnum_get_uint(qobject_to(QNum
, o
));
199 pm
->s3_disabled
= false;
202 o
= object_property_get_qobject(obj
, ACPI_PM_PROP_S4_DISABLED
, NULL
);
204 pm
->s4_disabled
= qnum_get_uint(qobject_to(QNum
, o
));
206 pm
->s4_disabled
= false;
209 o
= object_property_get_qobject(obj
, ACPI_PM_PROP_S4_VAL
, NULL
);
211 pm
->s4_val
= qnum_get_uint(qobject_to(QNum
, o
));
217 pm
->pcihp_bridge_en
=
218 object_property_get_bool(obj
, "acpi-pci-hotplug-with-bridge-support",
222 static void acpi_get_misc_info(AcpiMiscInfo
*info
)
224 Object
*piix
= piix4_pm_find();
225 Object
*lpc
= ich9_lpc_find();
226 assert(!!piix
!= !!lpc
);
229 info
->is_piix4
= true;
232 info
->is_piix4
= false;
235 info
->has_hpet
= hpet_find();
236 info
->tpm_version
= tpm_get_version(tpm_find());
237 info
->pvpanic_port
= pvpanic_port();
238 info
->applesmc_io_base
= applesmc_port();
242 * Because of the PXB hosts we cannot simply query TYPE_PCI_HOST_BRIDGE.
243 * On i386 arch we only have two pci hosts, so we can look only for them.
245 static Object
*acpi_get_i386_pci_host(void)
249 host
= OBJECT_CHECK(PCIHostState
,
250 object_resolve_path("/machine/i440fx", NULL
),
251 TYPE_PCI_HOST_BRIDGE
);
253 host
= OBJECT_CHECK(PCIHostState
,
254 object_resolve_path("/machine/q35", NULL
),
255 TYPE_PCI_HOST_BRIDGE
);
261 static void acpi_get_pci_holes(Range
*hole
, Range
*hole64
)
265 pci_host
= acpi_get_i386_pci_host();
268 range_set_bounds1(hole
,
269 object_property_get_uint(pci_host
,
270 PCI_HOST_PROP_PCI_HOLE_START
,
272 object_property_get_uint(pci_host
,
273 PCI_HOST_PROP_PCI_HOLE_END
,
275 range_set_bounds1(hole64
,
276 object_property_get_uint(pci_host
,
277 PCI_HOST_PROP_PCI_HOLE64_START
,
279 object_property_get_uint(pci_host
,
280 PCI_HOST_PROP_PCI_HOLE64_END
,
284 static void acpi_align_size(GArray
*blob
, unsigned align
)
286 /* Align size to multiple of given size. This reduces the chance
287 * we need to change size in the future (breaking cross version migration).
289 g_array_set_size(blob
, ROUND_UP(acpi_data_len(blob
), align
));
294 build_facs(GArray
*table_data
, BIOSLinker
*linker
)
296 AcpiFacsDescriptorRev1
*facs
= acpi_data_push(table_data
, sizeof *facs
);
297 memcpy(&facs
->signature
, "FACS", 4);
298 facs
->length
= cpu_to_le32(sizeof(*facs
));
301 void pc_madt_cpu_entry(AcpiDeviceIf
*adev
, int uid
,
302 const CPUArchIdList
*apic_ids
, GArray
*entry
)
304 uint32_t apic_id
= apic_ids
->cpus
[uid
].arch_id
;
306 /* ACPI spec says that LAPIC entry for non present
307 * CPU may be omitted from MADT or it must be marked
308 * as disabled. However omitting non present CPU from
309 * MADT breaks hotplug on linux. So possible CPUs
310 * should be put in MADT but kept disabled.
313 AcpiMadtProcessorApic
*apic
= acpi_data_push(entry
, sizeof *apic
);
315 apic
->type
= ACPI_APIC_PROCESSOR
;
316 apic
->length
= sizeof(*apic
);
317 apic
->processor_id
= uid
;
318 apic
->local_apic_id
= apic_id
;
319 if (apic_ids
->cpus
[uid
].cpu
!= NULL
) {
320 apic
->flags
= cpu_to_le32(1);
322 apic
->flags
= cpu_to_le32(0);
325 AcpiMadtProcessorX2Apic
*apic
= acpi_data_push(entry
, sizeof *apic
);
327 apic
->type
= ACPI_APIC_LOCAL_X2APIC
;
328 apic
->length
= sizeof(*apic
);
329 apic
->uid
= cpu_to_le32(uid
);
330 apic
->x2apic_id
= cpu_to_le32(apic_id
);
331 if (apic_ids
->cpus
[uid
].cpu
!= NULL
) {
332 apic
->flags
= cpu_to_le32(1);
334 apic
->flags
= cpu_to_le32(0);
340 build_madt(GArray
*table_data
, BIOSLinker
*linker
, PCMachineState
*pcms
)
342 MachineClass
*mc
= MACHINE_GET_CLASS(pcms
);
343 const CPUArchIdList
*apic_ids
= mc
->possible_cpu_arch_ids(MACHINE(pcms
));
344 int madt_start
= table_data
->len
;
345 AcpiDeviceIfClass
*adevc
= ACPI_DEVICE_IF_GET_CLASS(pcms
->acpi_dev
);
346 AcpiDeviceIf
*adev
= ACPI_DEVICE_IF(pcms
->acpi_dev
);
347 bool x2apic_mode
= false;
349 AcpiMultipleApicTable
*madt
;
350 AcpiMadtIoApic
*io_apic
;
351 AcpiMadtIntsrcovr
*intsrcovr
;
354 madt
= acpi_data_push(table_data
, sizeof *madt
);
355 madt
->local_apic_address
= cpu_to_le32(APIC_DEFAULT_ADDRESS
);
356 madt
->flags
= cpu_to_le32(1);
358 for (i
= 0; i
< apic_ids
->len
; i
++) {
359 adevc
->madt_cpu(adev
, i
, apic_ids
, table_data
);
360 if (apic_ids
->cpus
[i
].arch_id
> 254) {
365 io_apic
= acpi_data_push(table_data
, sizeof *io_apic
);
366 io_apic
->type
= ACPI_APIC_IO
;
367 io_apic
->length
= sizeof(*io_apic
);
368 io_apic
->io_apic_id
= ACPI_BUILD_IOAPIC_ID
;
369 io_apic
->address
= cpu_to_le32(IO_APIC_DEFAULT_ADDRESS
);
370 io_apic
->interrupt
= cpu_to_le32(0);
372 if (pcms
->apic_xrupt_override
) {
373 intsrcovr
= acpi_data_push(table_data
, sizeof *intsrcovr
);
374 intsrcovr
->type
= ACPI_APIC_XRUPT_OVERRIDE
;
375 intsrcovr
->length
= sizeof(*intsrcovr
);
376 intsrcovr
->source
= 0;
377 intsrcovr
->gsi
= cpu_to_le32(2);
378 intsrcovr
->flags
= cpu_to_le16(0); /* conforms to bus specifications */
380 for (i
= 1; i
< 16; i
++) {
381 #define ACPI_BUILD_PCI_IRQS ((1<<5) | (1<<9) | (1<<10) | (1<<11))
382 if (!(ACPI_BUILD_PCI_IRQS
& (1 << i
))) {
383 /* No need for a INT source override structure. */
386 intsrcovr
= acpi_data_push(table_data
, sizeof *intsrcovr
);
387 intsrcovr
->type
= ACPI_APIC_XRUPT_OVERRIDE
;
388 intsrcovr
->length
= sizeof(*intsrcovr
);
389 intsrcovr
->source
= i
;
390 intsrcovr
->gsi
= cpu_to_le32(i
);
391 intsrcovr
->flags
= cpu_to_le16(0xd); /* active high, level triggered */
395 AcpiMadtLocalX2ApicNmi
*local_nmi
;
397 local_nmi
= acpi_data_push(table_data
, sizeof *local_nmi
);
398 local_nmi
->type
= ACPI_APIC_LOCAL_X2APIC_NMI
;
399 local_nmi
->length
= sizeof(*local_nmi
);
400 local_nmi
->uid
= 0xFFFFFFFF; /* all processors */
401 local_nmi
->flags
= cpu_to_le16(0);
402 local_nmi
->lint
= 1; /* ACPI_LINT1 */
404 AcpiMadtLocalNmi
*local_nmi
;
406 local_nmi
= acpi_data_push(table_data
, sizeof *local_nmi
);
407 local_nmi
->type
= ACPI_APIC_LOCAL_NMI
;
408 local_nmi
->length
= sizeof(*local_nmi
);
409 local_nmi
->processor_id
= 0xff; /* all processors */
410 local_nmi
->flags
= cpu_to_le16(0);
411 local_nmi
->lint
= 1; /* ACPI_LINT1 */
414 build_header(linker
, table_data
,
415 (void *)(table_data
->data
+ madt_start
), "APIC",
416 table_data
->len
- madt_start
, 1, NULL
, NULL
);
419 static void build_append_pcihp_notify_entry(Aml
*method
, int slot
)
422 int32_t devfn
= PCI_DEVFN(slot
, 0);
424 if_ctx
= aml_if(aml_and(aml_arg(0), aml_int(0x1U
<< slot
), NULL
));
425 aml_append(if_ctx
, aml_notify(aml_name("S%.02X", devfn
), aml_arg(1)));
426 aml_append(method
, if_ctx
);
429 static void build_append_pci_bus_devices(Aml
*parent_scope
, PCIBus
*bus
,
430 bool pcihp_bridge_en
)
432 Aml
*dev
, *notify_method
= NULL
, *method
;
437 bsel
= object_property_get_qobject(OBJECT(bus
), ACPI_PCIHP_PROP_BSEL
, NULL
);
439 uint64_t bsel_val
= qnum_get_uint(qobject_to(QNum
, bsel
));
441 aml_append(parent_scope
, aml_name_decl("BSEL", aml_int(bsel_val
)));
442 notify_method
= aml_method("DVNT", 2, AML_NOTSERIALIZED
);
445 for (i
= 0; i
< ARRAY_SIZE(bus
->devices
); i
+= PCI_FUNC_MAX
) {
448 PCIDevice
*pdev
= bus
->devices
[i
];
449 int slot
= PCI_SLOT(i
);
450 bool hotplug_enabled_dev
;
454 if (bsel
) { /* add hotplug slots for non present devices */
455 dev
= aml_device("S%.02X", PCI_DEVFN(slot
, 0));
456 aml_append(dev
, aml_name_decl("_SUN", aml_int(slot
)));
457 aml_append(dev
, aml_name_decl("_ADR", aml_int(slot
<< 16)));
458 method
= aml_method("_EJ0", 1, AML_NOTSERIALIZED
);
460 aml_call2("PCEJ", aml_name("BSEL"), aml_name("_SUN"))
462 aml_append(dev
, method
);
463 aml_append(parent_scope
, dev
);
465 build_append_pcihp_notify_entry(notify_method
, slot
);
470 pc
= PCI_DEVICE_GET_CLASS(pdev
);
471 dc
= DEVICE_GET_CLASS(pdev
);
473 /* When hotplug for bridges is enabled, bridges are
474 * described in ACPI separately (see build_pci_bus_end).
475 * In this case they aren't themselves hot-pluggable.
476 * Hotplugged bridges *are* hot-pluggable.
478 bridge_in_acpi
= pc
->is_bridge
&& pcihp_bridge_en
&&
479 !DEVICE(pdev
)->hotplugged
;
481 hotplug_enabled_dev
= bsel
&& dc
->hotpluggable
&& !bridge_in_acpi
;
483 if (pc
->class_id
== PCI_CLASS_BRIDGE_ISA
) {
487 /* start to compose PCI slot descriptor */
488 dev
= aml_device("S%.02X", PCI_DEVFN(slot
, 0));
489 aml_append(dev
, aml_name_decl("_ADR", aml_int(slot
<< 16)));
491 if (pc
->class_id
== PCI_CLASS_DISPLAY_VGA
) {
492 /* add VGA specific AML methods */
495 if (object_dynamic_cast(OBJECT(pdev
), "qxl-vga")) {
501 method
= aml_method("_S1D", 0, AML_NOTSERIALIZED
);
502 aml_append(method
, aml_return(aml_int(0)));
503 aml_append(dev
, method
);
505 method
= aml_method("_S2D", 0, AML_NOTSERIALIZED
);
506 aml_append(method
, aml_return(aml_int(0)));
507 aml_append(dev
, method
);
509 method
= aml_method("_S3D", 0, AML_NOTSERIALIZED
);
510 aml_append(method
, aml_return(aml_int(s3d
)));
511 aml_append(dev
, method
);
512 } else if (hotplug_enabled_dev
) {
513 /* add _SUN/_EJ0 to make slot hotpluggable */
514 aml_append(dev
, aml_name_decl("_SUN", aml_int(slot
)));
516 method
= aml_method("_EJ0", 1, AML_NOTSERIALIZED
);
518 aml_call2("PCEJ", aml_name("BSEL"), aml_name("_SUN"))
520 aml_append(dev
, method
);
523 build_append_pcihp_notify_entry(notify_method
, slot
);
525 } else if (bridge_in_acpi
) {
527 * device is coldplugged bridge,
528 * add child device descriptions into its scope
530 PCIBus
*sec_bus
= pci_bridge_get_sec_bus(PCI_BRIDGE(pdev
));
532 build_append_pci_bus_devices(dev
, sec_bus
, pcihp_bridge_en
);
534 /* slot descriptor has been composed, add it into parent context */
535 aml_append(parent_scope
, dev
);
539 aml_append(parent_scope
, notify_method
);
542 /* Append PCNT method to notify about events on local and child buses.
543 * Add unconditionally for root since DSDT expects it.
545 method
= aml_method("PCNT", 0, AML_NOTSERIALIZED
);
547 /* If bus supports hotplug select it and notify about local events */
549 uint64_t bsel_val
= qnum_get_uint(qobject_to(QNum
, bsel
));
551 aml_append(method
, aml_store(aml_int(bsel_val
), aml_name("BNUM")));
553 aml_call2("DVNT", aml_name("PCIU"), aml_int(1) /* Device Check */)
556 aml_call2("DVNT", aml_name("PCID"), aml_int(3)/* Eject Request */)
560 /* Notify about child bus events in any case */
561 if (pcihp_bridge_en
) {
562 QLIST_FOREACH(sec
, &bus
->child
, sibling
) {
563 int32_t devfn
= sec
->parent_dev
->devfn
;
565 if (pci_bus_is_root(sec
) || pci_bus_is_express(sec
)) {
569 aml_append(method
, aml_name("^S%.02X.PCNT", devfn
));
572 aml_append(parent_scope
, method
);
578 * @link_name: link name for PCI route entry
580 * build AML package containing a PCI route entry for @link_name
582 static Aml
*build_prt_entry(const char *link_name
)
584 Aml
*a_zero
= aml_int(0);
585 Aml
*pkg
= aml_package(4);
586 aml_append(pkg
, a_zero
);
587 aml_append(pkg
, a_zero
);
588 aml_append(pkg
, aml_name("%s", link_name
));
589 aml_append(pkg
, a_zero
);
594 * initialize_route - Initialize the interrupt routing rule
595 * through a specific LINK:
596 * if (lnk_idx == idx)
597 * route using link 'link_name'
599 static Aml
*initialize_route(Aml
*route
, const char *link_name
,
600 Aml
*lnk_idx
, int idx
)
602 Aml
*if_ctx
= aml_if(aml_equal(lnk_idx
, aml_int(idx
)));
603 Aml
*pkg
= build_prt_entry(link_name
);
605 aml_append(if_ctx
, aml_store(pkg
, route
));
611 * build_prt - Define interrupt rounting rules
613 * Returns an array of 128 routes, one for each device,
614 * based on device location.
615 * The main goal is to equaly distribute the interrupts
616 * over the 4 existing ACPI links (works only for i440fx).
617 * The hash function is (slot + pin) & 3 -> "LNK[D|A|B|C]".
620 static Aml
*build_prt(bool is_pci0_prt
)
622 Aml
*method
, *while_ctx
, *pin
, *res
;
624 method
= aml_method("_PRT", 0, AML_NOTSERIALIZED
);
627 aml_append(method
, aml_store(aml_package(128), res
));
628 aml_append(method
, aml_store(aml_int(0), pin
));
630 /* while (pin < 128) */
631 while_ctx
= aml_while(aml_lless(pin
, aml_int(128)));
633 Aml
*slot
= aml_local(2);
634 Aml
*lnk_idx
= aml_local(3);
635 Aml
*route
= aml_local(4);
637 /* slot = pin >> 2 */
638 aml_append(while_ctx
,
639 aml_store(aml_shiftright(pin
, aml_int(2), NULL
), slot
));
640 /* lnk_idx = (slot + pin) & 3 */
641 aml_append(while_ctx
,
642 aml_store(aml_and(aml_add(pin
, slot
, NULL
), aml_int(3), NULL
),
645 /* route[2] = "LNK[D|A|B|C]", selection based on pin % 3 */
646 aml_append(while_ctx
, initialize_route(route
, "LNKD", lnk_idx
, 0));
648 Aml
*if_device_1
, *if_pin_4
, *else_pin_4
;
650 /* device 1 is the power-management device, needs SCI */
651 if_device_1
= aml_if(aml_equal(lnk_idx
, aml_int(1)));
653 if_pin_4
= aml_if(aml_equal(pin
, aml_int(4)));
656 aml_store(build_prt_entry("LNKS"), route
));
658 aml_append(if_device_1
, if_pin_4
);
659 else_pin_4
= aml_else();
661 aml_append(else_pin_4
,
662 aml_store(build_prt_entry("LNKA"), route
));
664 aml_append(if_device_1
, else_pin_4
);
666 aml_append(while_ctx
, if_device_1
);
668 aml_append(while_ctx
, initialize_route(route
, "LNKA", lnk_idx
, 1));
670 aml_append(while_ctx
, initialize_route(route
, "LNKB", lnk_idx
, 2));
671 aml_append(while_ctx
, initialize_route(route
, "LNKC", lnk_idx
, 3));
673 /* route[0] = 0x[slot]FFFF */
674 aml_append(while_ctx
,
675 aml_store(aml_or(aml_shiftleft(slot
, aml_int(16)), aml_int(0xFFFF),
677 aml_index(route
, aml_int(0))));
678 /* route[1] = pin & 3 */
679 aml_append(while_ctx
,
680 aml_store(aml_and(pin
, aml_int(3), NULL
),
681 aml_index(route
, aml_int(1))));
682 /* res[pin] = route */
683 aml_append(while_ctx
, aml_store(route
, aml_index(res
, pin
)));
685 aml_append(while_ctx
, aml_increment(pin
));
687 aml_append(method
, while_ctx
);
689 aml_append(method
, aml_return(res
));
694 typedef struct CrsRangeEntry
{
699 static void crs_range_insert(GPtrArray
*ranges
, uint64_t base
, uint64_t limit
)
701 CrsRangeEntry
*entry
;
703 entry
= g_malloc(sizeof(*entry
));
705 entry
->limit
= limit
;
707 g_ptr_array_add(ranges
, entry
);
710 static void crs_range_free(gpointer data
)
712 CrsRangeEntry
*entry
= (CrsRangeEntry
*)data
;
716 typedef struct CrsRangeSet
{
717 GPtrArray
*io_ranges
;
718 GPtrArray
*mem_ranges
;
719 GPtrArray
*mem_64bit_ranges
;
722 static void crs_range_set_init(CrsRangeSet
*range_set
)
724 range_set
->io_ranges
= g_ptr_array_new_with_free_func(crs_range_free
);
725 range_set
->mem_ranges
= g_ptr_array_new_with_free_func(crs_range_free
);
726 range_set
->mem_64bit_ranges
=
727 g_ptr_array_new_with_free_func(crs_range_free
);
730 static void crs_range_set_free(CrsRangeSet
*range_set
)
732 g_ptr_array_free(range_set
->io_ranges
, true);
733 g_ptr_array_free(range_set
->mem_ranges
, true);
734 g_ptr_array_free(range_set
->mem_64bit_ranges
, true);
737 static gint
crs_range_compare(gconstpointer a
, gconstpointer b
)
739 CrsRangeEntry
*entry_a
= *(CrsRangeEntry
**)a
;
740 CrsRangeEntry
*entry_b
= *(CrsRangeEntry
**)b
;
742 return (int64_t)entry_a
->base
- (int64_t)entry_b
->base
;
746 * crs_replace_with_free_ranges - given the 'used' ranges within [start - end]
747 * interval, computes the 'free' ranges from the same interval.
748 * Example: If the input array is { [a1 - a2],[b1 - b2] }, the function
749 * will return { [base - a1], [a2 - b1], [b2 - limit] }.
751 static void crs_replace_with_free_ranges(GPtrArray
*ranges
,
752 uint64_t start
, uint64_t end
)
754 GPtrArray
*free_ranges
= g_ptr_array_new();
755 uint64_t free_base
= start
;
758 g_ptr_array_sort(ranges
, crs_range_compare
);
759 for (i
= 0; i
< ranges
->len
; i
++) {
760 CrsRangeEntry
*used
= g_ptr_array_index(ranges
, i
);
762 if (free_base
< used
->base
) {
763 crs_range_insert(free_ranges
, free_base
, used
->base
- 1);
766 free_base
= used
->limit
+ 1;
769 if (free_base
< end
) {
770 crs_range_insert(free_ranges
, free_base
, end
);
773 g_ptr_array_set_size(ranges
, 0);
774 for (i
= 0; i
< free_ranges
->len
; i
++) {
775 g_ptr_array_add(ranges
, g_ptr_array_index(free_ranges
, i
));
778 g_ptr_array_free(free_ranges
, true);
782 * crs_range_merge - merges adjacent ranges in the given array.
783 * Array elements are deleted and replaced with the merged ranges.
785 static void crs_range_merge(GPtrArray
*range
)
787 GPtrArray
*tmp
= g_ptr_array_new_with_free_func(crs_range_free
);
788 CrsRangeEntry
*entry
;
789 uint64_t range_base
, range_limit
;
796 g_ptr_array_sort(range
, crs_range_compare
);
798 entry
= g_ptr_array_index(range
, 0);
799 range_base
= entry
->base
;
800 range_limit
= entry
->limit
;
801 for (i
= 1; i
< range
->len
; i
++) {
802 entry
= g_ptr_array_index(range
, i
);
803 if (entry
->base
- 1 == range_limit
) {
804 range_limit
= entry
->limit
;
806 crs_range_insert(tmp
, range_base
, range_limit
);
807 range_base
= entry
->base
;
808 range_limit
= entry
->limit
;
811 crs_range_insert(tmp
, range_base
, range_limit
);
813 g_ptr_array_set_size(range
, 0);
814 for (i
= 0; i
< tmp
->len
; i
++) {
815 entry
= g_ptr_array_index(tmp
, i
);
816 crs_range_insert(range
, entry
->base
, entry
->limit
);
818 g_ptr_array_free(tmp
, true);
821 static Aml
*build_crs(PCIHostState
*host
, CrsRangeSet
*range_set
)
823 Aml
*crs
= aml_resource_template();
824 CrsRangeSet temp_range_set
;
825 CrsRangeEntry
*entry
;
826 uint8_t max_bus
= pci_bus_num(host
->bus
);
831 crs_range_set_init(&temp_range_set
);
832 for (devfn
= 0; devfn
< ARRAY_SIZE(host
->bus
->devices
); devfn
++) {
833 uint64_t range_base
, range_limit
;
834 PCIDevice
*dev
= host
->bus
->devices
[devfn
];
840 for (i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
841 PCIIORegion
*r
= &dev
->io_regions
[i
];
843 range_base
= r
->addr
;
844 range_limit
= r
->addr
+ r
->size
- 1;
847 * Work-around for old bioses
848 * that do not support multiple root buses
850 if (!range_base
|| range_base
> range_limit
) {
854 if (r
->type
& PCI_BASE_ADDRESS_SPACE_IO
) {
855 crs_range_insert(temp_range_set
.io_ranges
,
856 range_base
, range_limit
);
857 } else { /* "memory" */
858 crs_range_insert(temp_range_set
.mem_ranges
,
859 range_base
, range_limit
);
863 type
= dev
->config
[PCI_HEADER_TYPE
] & ~PCI_HEADER_TYPE_MULTI_FUNCTION
;
864 if (type
== PCI_HEADER_TYPE_BRIDGE
) {
865 uint8_t subordinate
= dev
->config
[PCI_SUBORDINATE_BUS
];
866 if (subordinate
> max_bus
) {
867 max_bus
= subordinate
;
870 range_base
= pci_bridge_get_base(dev
, PCI_BASE_ADDRESS_SPACE_IO
);
871 range_limit
= pci_bridge_get_limit(dev
, PCI_BASE_ADDRESS_SPACE_IO
);
874 * Work-around for old bioses
875 * that do not support multiple root buses
877 if (range_base
&& range_base
<= range_limit
) {
878 crs_range_insert(temp_range_set
.io_ranges
,
879 range_base
, range_limit
);
883 pci_bridge_get_base(dev
, PCI_BASE_ADDRESS_SPACE_MEMORY
);
885 pci_bridge_get_limit(dev
, PCI_BASE_ADDRESS_SPACE_MEMORY
);
888 * Work-around for old bioses
889 * that do not support multiple root buses
891 if (range_base
&& range_base
<= range_limit
) {
892 uint64_t length
= range_limit
- range_base
+ 1;
893 if (range_limit
<= UINT32_MAX
&& length
<= UINT32_MAX
) {
894 crs_range_insert(temp_range_set
.mem_ranges
,
895 range_base
, range_limit
);
897 crs_range_insert(temp_range_set
.mem_64bit_ranges
,
898 range_base
, range_limit
);
903 pci_bridge_get_base(dev
, PCI_BASE_ADDRESS_MEM_PREFETCH
);
905 pci_bridge_get_limit(dev
, PCI_BASE_ADDRESS_MEM_PREFETCH
);
908 * Work-around for old bioses
909 * that do not support multiple root buses
911 if (range_base
&& range_base
<= range_limit
) {
912 uint64_t length
= range_limit
- range_base
+ 1;
913 if (range_limit
<= UINT32_MAX
&& length
<= UINT32_MAX
) {
914 crs_range_insert(temp_range_set
.mem_ranges
,
915 range_base
, range_limit
);
917 crs_range_insert(temp_range_set
.mem_64bit_ranges
,
918 range_base
, range_limit
);
924 crs_range_merge(temp_range_set
.io_ranges
);
925 for (i
= 0; i
< temp_range_set
.io_ranges
->len
; i
++) {
926 entry
= g_ptr_array_index(temp_range_set
.io_ranges
, i
);
928 aml_word_io(AML_MIN_FIXED
, AML_MAX_FIXED
,
929 AML_POS_DECODE
, AML_ENTIRE_RANGE
,
930 0, entry
->base
, entry
->limit
, 0,
931 entry
->limit
- entry
->base
+ 1));
932 crs_range_insert(range_set
->io_ranges
, entry
->base
, entry
->limit
);
935 crs_range_merge(temp_range_set
.mem_ranges
);
936 for (i
= 0; i
< temp_range_set
.mem_ranges
->len
; i
++) {
937 entry
= g_ptr_array_index(temp_range_set
.mem_ranges
, i
);
939 aml_dword_memory(AML_POS_DECODE
, AML_MIN_FIXED
,
940 AML_MAX_FIXED
, AML_NON_CACHEABLE
,
942 0, entry
->base
, entry
->limit
, 0,
943 entry
->limit
- entry
->base
+ 1));
944 crs_range_insert(range_set
->mem_ranges
, entry
->base
, entry
->limit
);
947 crs_range_merge(temp_range_set
.mem_64bit_ranges
);
948 for (i
= 0; i
< temp_range_set
.mem_64bit_ranges
->len
; i
++) {
949 entry
= g_ptr_array_index(temp_range_set
.mem_64bit_ranges
, i
);
951 aml_qword_memory(AML_POS_DECODE
, AML_MIN_FIXED
,
952 AML_MAX_FIXED
, AML_NON_CACHEABLE
,
954 0, entry
->base
, entry
->limit
, 0,
955 entry
->limit
- entry
->base
+ 1));
956 crs_range_insert(range_set
->mem_64bit_ranges
,
957 entry
->base
, entry
->limit
);
960 crs_range_set_free(&temp_range_set
);
963 aml_word_bus_number(AML_MIN_FIXED
, AML_MAX_FIXED
, AML_POS_DECODE
,
965 pci_bus_num(host
->bus
),
968 max_bus
- pci_bus_num(host
->bus
) + 1));
973 static void build_hpet_aml(Aml
*table
)
979 Aml
*scope
= aml_scope("_SB");
980 Aml
*dev
= aml_device("HPET");
981 Aml
*zero
= aml_int(0);
982 Aml
*id
= aml_local(0);
983 Aml
*period
= aml_local(1);
985 aml_append(dev
, aml_name_decl("_HID", aml_eisaid("PNP0103")));
986 aml_append(dev
, aml_name_decl("_UID", zero
));
989 aml_operation_region("HPTM", AML_SYSTEM_MEMORY
, aml_int(HPET_BASE
),
991 field
= aml_field("HPTM", AML_DWORD_ACC
, AML_LOCK
, AML_PRESERVE
);
992 aml_append(field
, aml_named_field("VEND", 32));
993 aml_append(field
, aml_named_field("PRD", 32));
994 aml_append(dev
, field
);
996 method
= aml_method("_STA", 0, AML_NOTSERIALIZED
);
997 aml_append(method
, aml_store(aml_name("VEND"), id
));
998 aml_append(method
, aml_store(aml_name("PRD"), period
));
999 aml_append(method
, aml_shiftright(id
, aml_int(16), id
));
1000 if_ctx
= aml_if(aml_lor(aml_equal(id
, zero
),
1001 aml_equal(id
, aml_int(0xffff))));
1003 aml_append(if_ctx
, aml_return(zero
));
1005 aml_append(method
, if_ctx
);
1007 if_ctx
= aml_if(aml_lor(aml_equal(period
, zero
),
1008 aml_lgreater(period
, aml_int(100000000))));
1010 aml_append(if_ctx
, aml_return(zero
));
1012 aml_append(method
, if_ctx
);
1014 aml_append(method
, aml_return(aml_int(0x0F)));
1015 aml_append(dev
, method
);
1017 crs
= aml_resource_template();
1018 aml_append(crs
, aml_memory32_fixed(HPET_BASE
, HPET_LEN
, AML_READ_ONLY
));
1019 aml_append(dev
, aml_name_decl("_CRS", crs
));
1021 aml_append(scope
, dev
);
1022 aml_append(table
, scope
);
1025 static Aml
*build_fdinfo_aml(int idx
, FloppyDriveType type
)
1028 uint8_t maxc
, maxh
, maxs
;
1030 isa_fdc_get_drive_max_chs(type
, &maxc
, &maxh
, &maxs
);
1032 dev
= aml_device("FLP%c", 'A' + idx
);
1034 aml_append(dev
, aml_name_decl("_ADR", aml_int(idx
)));
1036 fdi
= aml_package(16);
1037 aml_append(fdi
, aml_int(idx
)); /* Drive Number */
1039 aml_int(cmos_get_fd_drive_type(type
))); /* Device Type */
1041 * the values below are the limits of the drive, and are thus independent
1042 * of the inserted media
1044 aml_append(fdi
, aml_int(maxc
)); /* Maximum Cylinder Number */
1045 aml_append(fdi
, aml_int(maxs
)); /* Maximum Sector Number */
1046 aml_append(fdi
, aml_int(maxh
)); /* Maximum Head Number */
1048 * SeaBIOS returns the below values for int 0x13 func 0x08 regardless of
1049 * the drive type, so shall we
1051 aml_append(fdi
, aml_int(0xAF)); /* disk_specify_1 */
1052 aml_append(fdi
, aml_int(0x02)); /* disk_specify_2 */
1053 aml_append(fdi
, aml_int(0x25)); /* disk_motor_wait */
1054 aml_append(fdi
, aml_int(0x02)); /* disk_sector_siz */
1055 aml_append(fdi
, aml_int(0x12)); /* disk_eot */
1056 aml_append(fdi
, aml_int(0x1B)); /* disk_rw_gap */
1057 aml_append(fdi
, aml_int(0xFF)); /* disk_dtl */
1058 aml_append(fdi
, aml_int(0x6C)); /* disk_formt_gap */
1059 aml_append(fdi
, aml_int(0xF6)); /* disk_fill */
1060 aml_append(fdi
, aml_int(0x0F)); /* disk_head_sttl */
1061 aml_append(fdi
, aml_int(0x08)); /* disk_motor_strt */
1063 aml_append(dev
, aml_name_decl("_FDI", fdi
));
1067 static Aml
*build_fdc_device_aml(ISADevice
*fdc
)
1073 #define ACPI_FDE_MAX_FD 4
1074 uint32_t fde_buf
[5] = {
1075 0, 0, 0, 0, /* presence of floppy drives #0 - #3 */
1076 cpu_to_le32(2) /* tape presence (2 == never present) */
1079 dev
= aml_device("FDC0");
1080 aml_append(dev
, aml_name_decl("_HID", aml_eisaid("PNP0700")));
1082 crs
= aml_resource_template();
1083 aml_append(crs
, aml_io(AML_DECODE16
, 0x03F2, 0x03F2, 0x00, 0x04));
1084 aml_append(crs
, aml_io(AML_DECODE16
, 0x03F7, 0x03F7, 0x00, 0x01));
1085 aml_append(crs
, aml_irq_no_flags(6));
1087 aml_dma(AML_COMPATIBILITY
, AML_NOTBUSMASTER
, AML_TRANSFER8
, 2));
1088 aml_append(dev
, aml_name_decl("_CRS", crs
));
1090 for (i
= 0; i
< MIN(MAX_FD
, ACPI_FDE_MAX_FD
); i
++) {
1091 FloppyDriveType type
= isa_fdc_get_drive_type(fdc
, i
);
1093 if (type
< FLOPPY_DRIVE_TYPE_NONE
) {
1094 fde_buf
[i
] = cpu_to_le32(1); /* drive present */
1095 aml_append(dev
, build_fdinfo_aml(i
, type
));
1098 aml_append(dev
, aml_name_decl("_FDE",
1099 aml_buffer(sizeof(fde_buf
), (uint8_t *)fde_buf
)));
1104 static Aml
*build_rtc_device_aml(void)
1109 dev
= aml_device("RTC");
1110 aml_append(dev
, aml_name_decl("_HID", aml_eisaid("PNP0B00")));
1111 crs
= aml_resource_template();
1112 aml_append(crs
, aml_io(AML_DECODE16
, 0x0070, 0x0070, 0x10, 0x02));
1113 aml_append(crs
, aml_irq_no_flags(8));
1114 aml_append(crs
, aml_io(AML_DECODE16
, 0x0072, 0x0072, 0x02, 0x06));
1115 aml_append(dev
, aml_name_decl("_CRS", crs
));
1120 static Aml
*build_kbd_device_aml(void)
1126 dev
= aml_device("KBD");
1127 aml_append(dev
, aml_name_decl("_HID", aml_eisaid("PNP0303")));
1129 method
= aml_method("_STA", 0, AML_NOTSERIALIZED
);
1130 aml_append(method
, aml_return(aml_int(0x0f)));
1131 aml_append(dev
, method
);
1133 crs
= aml_resource_template();
1134 aml_append(crs
, aml_io(AML_DECODE16
, 0x0060, 0x0060, 0x01, 0x01));
1135 aml_append(crs
, aml_io(AML_DECODE16
, 0x0064, 0x0064, 0x01, 0x01));
1136 aml_append(crs
, aml_irq_no_flags(1));
1137 aml_append(dev
, aml_name_decl("_CRS", crs
));
1142 static Aml
*build_mouse_device_aml(void)
1148 dev
= aml_device("MOU");
1149 aml_append(dev
, aml_name_decl("_HID", aml_eisaid("PNP0F13")));
1151 method
= aml_method("_STA", 0, AML_NOTSERIALIZED
);
1152 aml_append(method
, aml_return(aml_int(0x0f)));
1153 aml_append(dev
, method
);
1155 crs
= aml_resource_template();
1156 aml_append(crs
, aml_irq_no_flags(12));
1157 aml_append(dev
, aml_name_decl("_CRS", crs
));
1162 static Aml
*build_lpt_device_aml(void)
1169 Aml
*zero
= aml_int(0);
1170 Aml
*is_present
= aml_local(0);
1172 dev
= aml_device("LPT");
1173 aml_append(dev
, aml_name_decl("_HID", aml_eisaid("PNP0400")));
1175 method
= aml_method("_STA", 0, AML_NOTSERIALIZED
);
1176 aml_append(method
, aml_store(aml_name("LPEN"), is_present
));
1177 if_ctx
= aml_if(aml_equal(is_present
, zero
));
1179 aml_append(if_ctx
, aml_return(aml_int(0x00)));
1181 aml_append(method
, if_ctx
);
1182 else_ctx
= aml_else();
1184 aml_append(else_ctx
, aml_return(aml_int(0x0f)));
1186 aml_append(method
, else_ctx
);
1187 aml_append(dev
, method
);
1189 crs
= aml_resource_template();
1190 aml_append(crs
, aml_io(AML_DECODE16
, 0x0378, 0x0378, 0x08, 0x08));
1191 aml_append(crs
, aml_irq_no_flags(7));
1192 aml_append(dev
, aml_name_decl("_CRS", crs
));
1197 static Aml
*build_com_device_aml(uint8_t uid
)
1204 Aml
*zero
= aml_int(0);
1205 Aml
*is_present
= aml_local(0);
1206 const char *enabled_field
= "CAEN";
1208 uint16_t io_port
= 0x03F8;
1210 assert(uid
== 1 || uid
== 2);
1212 enabled_field
= "CBEN";
1217 dev
= aml_device("COM%d", uid
);
1218 aml_append(dev
, aml_name_decl("_HID", aml_eisaid("PNP0501")));
1219 aml_append(dev
, aml_name_decl("_UID", aml_int(uid
)));
1221 method
= aml_method("_STA", 0, AML_NOTSERIALIZED
);
1222 aml_append(method
, aml_store(aml_name("%s", enabled_field
), is_present
));
1223 if_ctx
= aml_if(aml_equal(is_present
, zero
));
1225 aml_append(if_ctx
, aml_return(aml_int(0x00)));
1227 aml_append(method
, if_ctx
);
1228 else_ctx
= aml_else();
1230 aml_append(else_ctx
, aml_return(aml_int(0x0f)));
1232 aml_append(method
, else_ctx
);
1233 aml_append(dev
, method
);
1235 crs
= aml_resource_template();
1236 aml_append(crs
, aml_io(AML_DECODE16
, io_port
, io_port
, 0x00, 0x08));
1237 aml_append(crs
, aml_irq_no_flags(irq
));
1238 aml_append(dev
, aml_name_decl("_CRS", crs
));
1243 static void build_isa_devices_aml(Aml
*table
)
1245 ISADevice
*fdc
= pc_find_fdc0();
1248 Aml
*scope
= aml_scope("_SB.PCI0.ISA");
1249 Object
*obj
= object_resolve_path_type("", TYPE_ISA_BUS
, &ambiguous
);
1251 aml_append(scope
, build_rtc_device_aml());
1252 aml_append(scope
, build_kbd_device_aml());
1253 aml_append(scope
, build_mouse_device_aml());
1255 aml_append(scope
, build_fdc_device_aml(fdc
));
1257 aml_append(scope
, build_lpt_device_aml());
1258 aml_append(scope
, build_com_device_aml(1));
1259 aml_append(scope
, build_com_device_aml(2));
1262 error_report("Multiple ISA busses, unable to define IPMI ACPI data");
1264 error_report("No ISA bus, unable to define IPMI ACPI data");
1266 build_acpi_ipmi_devices(scope
, BUS(obj
));
1269 aml_append(table
, scope
);
1272 static void build_dbg_aml(Aml
*table
)
1277 Aml
*scope
= aml_scope("\\");
1278 Aml
*buf
= aml_local(0);
1279 Aml
*len
= aml_local(1);
1280 Aml
*idx
= aml_local(2);
1283 aml_operation_region("DBG", AML_SYSTEM_IO
, aml_int(0x0402), 0x01));
1284 field
= aml_field("DBG", AML_BYTE_ACC
, AML_NOLOCK
, AML_PRESERVE
);
1285 aml_append(field
, aml_named_field("DBGB", 8));
1286 aml_append(scope
, field
);
1288 method
= aml_method("DBUG", 1, AML_NOTSERIALIZED
);
1290 aml_append(method
, aml_to_hexstring(aml_arg(0), buf
));
1291 aml_append(method
, aml_to_buffer(buf
, buf
));
1292 aml_append(method
, aml_subtract(aml_sizeof(buf
), aml_int(1), len
));
1293 aml_append(method
, aml_store(aml_int(0), idx
));
1295 while_ctx
= aml_while(aml_lless(idx
, len
));
1296 aml_append(while_ctx
,
1297 aml_store(aml_derefof(aml_index(buf
, idx
)), aml_name("DBGB")));
1298 aml_append(while_ctx
, aml_increment(idx
));
1299 aml_append(method
, while_ctx
);
1301 aml_append(method
, aml_store(aml_int(0x0A), aml_name("DBGB")));
1302 aml_append(scope
, method
);
1304 aml_append(table
, scope
);
1307 static Aml
*build_link_dev(const char *name
, uint8_t uid
, Aml
*reg
)
1312 uint32_t irqs
[] = {5, 10, 11};
1314 dev
= aml_device("%s", name
);
1315 aml_append(dev
, aml_name_decl("_HID", aml_eisaid("PNP0C0F")));
1316 aml_append(dev
, aml_name_decl("_UID", aml_int(uid
)));
1318 crs
= aml_resource_template();
1319 aml_append(crs
, aml_interrupt(AML_CONSUMER
, AML_LEVEL
, AML_ACTIVE_HIGH
,
1320 AML_SHARED
, irqs
, ARRAY_SIZE(irqs
)));
1321 aml_append(dev
, aml_name_decl("_PRS", crs
));
1323 method
= aml_method("_STA", 0, AML_NOTSERIALIZED
);
1324 aml_append(method
, aml_return(aml_call1("IQST", reg
)));
1325 aml_append(dev
, method
);
1327 method
= aml_method("_DIS", 0, AML_NOTSERIALIZED
);
1328 aml_append(method
, aml_or(reg
, aml_int(0x80), reg
));
1329 aml_append(dev
, method
);
1331 method
= aml_method("_CRS", 0, AML_NOTSERIALIZED
);
1332 aml_append(method
, aml_return(aml_call1("IQCR", reg
)));
1333 aml_append(dev
, method
);
1335 method
= aml_method("_SRS", 1, AML_NOTSERIALIZED
);
1336 aml_append(method
, aml_create_dword_field(aml_arg(0), aml_int(5), "PRRI"));
1337 aml_append(method
, aml_store(aml_name("PRRI"), reg
));
1338 aml_append(dev
, method
);
1343 static Aml
*build_gsi_link_dev(const char *name
, uint8_t uid
, uint8_t gsi
)
1350 dev
= aml_device("%s", name
);
1351 aml_append(dev
, aml_name_decl("_HID", aml_eisaid("PNP0C0F")));
1352 aml_append(dev
, aml_name_decl("_UID", aml_int(uid
)));
1354 crs
= aml_resource_template();
1356 aml_append(crs
, aml_interrupt(AML_CONSUMER
, AML_LEVEL
, AML_ACTIVE_HIGH
,
1357 AML_SHARED
, &irqs
, 1));
1358 aml_append(dev
, aml_name_decl("_PRS", crs
));
1360 aml_append(dev
, aml_name_decl("_CRS", crs
));
1363 * _DIS can be no-op because the interrupt cannot be disabled.
1365 method
= aml_method("_DIS", 0, AML_NOTSERIALIZED
);
1366 aml_append(dev
, method
);
1368 method
= aml_method("_SRS", 1, AML_NOTSERIALIZED
);
1369 aml_append(dev
, method
);
1374 /* _CRS method - get current settings */
1375 static Aml
*build_iqcr_method(bool is_piix4
)
1379 Aml
*method
= aml_method("IQCR", 1, AML_SERIALIZED
);
1380 Aml
*crs
= aml_resource_template();
1383 aml_append(crs
, aml_interrupt(AML_CONSUMER
, AML_LEVEL
,
1384 AML_ACTIVE_HIGH
, AML_SHARED
, &irqs
, 1));
1385 aml_append(method
, aml_name_decl("PRR0", crs
));
1388 aml_create_dword_field(aml_name("PRR0"), aml_int(5), "PRRI"));
1391 if_ctx
= aml_if(aml_lless(aml_arg(0), aml_int(0x80)));
1392 aml_append(if_ctx
, aml_store(aml_arg(0), aml_name("PRRI")));
1393 aml_append(method
, if_ctx
);
1396 aml_store(aml_and(aml_arg(0), aml_int(0xF), NULL
),
1400 aml_append(method
, aml_return(aml_name("PRR0")));
1404 /* _STA method - get status */
1405 static Aml
*build_irq_status_method(void)
1408 Aml
*method
= aml_method("IQST", 1, AML_NOTSERIALIZED
);
1410 if_ctx
= aml_if(aml_and(aml_int(0x80), aml_arg(0), NULL
));
1411 aml_append(if_ctx
, aml_return(aml_int(0x09)));
1412 aml_append(method
, if_ctx
);
1413 aml_append(method
, aml_return(aml_int(0x0B)));
1417 static void build_piix4_pci0_int(Aml
*table
)
1424 Aml
*sb_scope
= aml_scope("_SB");
1425 Aml
*pci0_scope
= aml_scope("PCI0");
1427 aml_append(pci0_scope
, build_prt(true));
1428 aml_append(sb_scope
, pci0_scope
);
1430 field
= aml_field("PCI0.ISA.P40C", AML_BYTE_ACC
, AML_NOLOCK
, AML_PRESERVE
);
1431 aml_append(field
, aml_named_field("PRQ0", 8));
1432 aml_append(field
, aml_named_field("PRQ1", 8));
1433 aml_append(field
, aml_named_field("PRQ2", 8));
1434 aml_append(field
, aml_named_field("PRQ3", 8));
1435 aml_append(sb_scope
, field
);
1437 aml_append(sb_scope
, build_irq_status_method());
1438 aml_append(sb_scope
, build_iqcr_method(true));
1440 aml_append(sb_scope
, build_link_dev("LNKA", 0, aml_name("PRQ0")));
1441 aml_append(sb_scope
, build_link_dev("LNKB", 1, aml_name("PRQ1")));
1442 aml_append(sb_scope
, build_link_dev("LNKC", 2, aml_name("PRQ2")));
1443 aml_append(sb_scope
, build_link_dev("LNKD", 3, aml_name("PRQ3")));
1445 dev
= aml_device("LNKS");
1447 aml_append(dev
, aml_name_decl("_HID", aml_eisaid("PNP0C0F")));
1448 aml_append(dev
, aml_name_decl("_UID", aml_int(4)));
1450 crs
= aml_resource_template();
1452 aml_append(crs
, aml_interrupt(AML_CONSUMER
, AML_LEVEL
,
1453 AML_ACTIVE_HIGH
, AML_SHARED
,
1455 aml_append(dev
, aml_name_decl("_PRS", crs
));
1457 /* The SCI cannot be disabled and is always attached to GSI 9,
1458 * so these are no-ops. We only need this link to override the
1459 * polarity to active high and match the content of the MADT.
1461 method
= aml_method("_STA", 0, AML_NOTSERIALIZED
);
1462 aml_append(method
, aml_return(aml_int(0x0b)));
1463 aml_append(dev
, method
);
1465 method
= aml_method("_DIS", 0, AML_NOTSERIALIZED
);
1466 aml_append(dev
, method
);
1468 method
= aml_method("_CRS", 0, AML_NOTSERIALIZED
);
1469 aml_append(method
, aml_return(aml_name("_PRS")));
1470 aml_append(dev
, method
);
1472 method
= aml_method("_SRS", 1, AML_NOTSERIALIZED
);
1473 aml_append(dev
, method
);
1475 aml_append(sb_scope
, dev
);
1477 aml_append(table
, sb_scope
);
1480 static void append_q35_prt_entry(Aml
*ctx
, uint32_t nr
, const char *name
)
1485 char base
= name
[3] < 'E' ? 'A' : 'E';
1486 char *s
= g_strdup(name
);
1487 Aml
*a_nr
= aml_int((nr
<< 16) | 0xffff);
1489 assert(strlen(s
) == 4);
1491 head
= name
[3] - base
;
1492 for (i
= 0; i
< 4; i
++) {
1496 s
[3] = base
+ head
+ i
;
1497 pkg
= aml_package(4);
1498 aml_append(pkg
, a_nr
);
1499 aml_append(pkg
, aml_int(i
));
1500 aml_append(pkg
, aml_name("%s", s
));
1501 aml_append(pkg
, aml_int(0));
1502 aml_append(ctx
, pkg
);
1507 static Aml
*build_q35_routing_table(const char *str
)
1511 char *name
= g_strdup_printf("%s ", str
);
1513 pkg
= aml_package(128);
1514 for (i
= 0; i
< 0x18; i
++) {
1515 name
[3] = 'E' + (i
& 0x3);
1516 append_q35_prt_entry(pkg
, i
, name
);
1520 append_q35_prt_entry(pkg
, 0x18, name
);
1522 /* INTA -> PIRQA for slot 25 - 31, see the default value of D<N>IR */
1523 for (i
= 0x0019; i
< 0x1e; i
++) {
1525 append_q35_prt_entry(pkg
, i
, name
);
1528 /* PCIe->PCI bridge. use PIRQ[E-H] */
1530 append_q35_prt_entry(pkg
, 0x1e, name
);
1532 append_q35_prt_entry(pkg
, 0x1f, name
);
1538 static void build_q35_pci0_int(Aml
*table
)
1542 Aml
*sb_scope
= aml_scope("_SB");
1543 Aml
*pci0_scope
= aml_scope("PCI0");
1545 /* Zero => PIC mode, One => APIC Mode */
1546 aml_append(table
, aml_name_decl("PICF", aml_int(0)));
1547 method
= aml_method("_PIC", 1, AML_NOTSERIALIZED
);
1549 aml_append(method
, aml_store(aml_arg(0), aml_name("PICF")));
1551 aml_append(table
, method
);
1553 aml_append(pci0_scope
,
1554 aml_name_decl("PRTP", build_q35_routing_table("LNK")));
1555 aml_append(pci0_scope
,
1556 aml_name_decl("PRTA", build_q35_routing_table("GSI")));
1558 method
= aml_method("_PRT", 0, AML_NOTSERIALIZED
);
1563 /* PCI IRQ routing table, example from ACPI 2.0a specification,
1565 /* Note: we provide the same info as the PCI routing
1566 table of the Bochs BIOS */
1567 if_ctx
= aml_if(aml_equal(aml_name("PICF"), aml_int(0)));
1568 aml_append(if_ctx
, aml_return(aml_name("PRTP")));
1569 aml_append(method
, if_ctx
);
1570 else_ctx
= aml_else();
1571 aml_append(else_ctx
, aml_return(aml_name("PRTA")));
1572 aml_append(method
, else_ctx
);
1574 aml_append(pci0_scope
, method
);
1575 aml_append(sb_scope
, pci0_scope
);
1577 field
= aml_field("PCI0.ISA.PIRQ", AML_BYTE_ACC
, AML_NOLOCK
, AML_PRESERVE
);
1578 aml_append(field
, aml_named_field("PRQA", 8));
1579 aml_append(field
, aml_named_field("PRQB", 8));
1580 aml_append(field
, aml_named_field("PRQC", 8));
1581 aml_append(field
, aml_named_field("PRQD", 8));
1582 aml_append(field
, aml_reserved_field(0x20));
1583 aml_append(field
, aml_named_field("PRQE", 8));
1584 aml_append(field
, aml_named_field("PRQF", 8));
1585 aml_append(field
, aml_named_field("PRQG", 8));
1586 aml_append(field
, aml_named_field("PRQH", 8));
1587 aml_append(sb_scope
, field
);
1589 aml_append(sb_scope
, build_irq_status_method());
1590 aml_append(sb_scope
, build_iqcr_method(false));
1592 aml_append(sb_scope
, build_link_dev("LNKA", 0, aml_name("PRQA")));
1593 aml_append(sb_scope
, build_link_dev("LNKB", 1, aml_name("PRQB")));
1594 aml_append(sb_scope
, build_link_dev("LNKC", 2, aml_name("PRQC")));
1595 aml_append(sb_scope
, build_link_dev("LNKD", 3, aml_name("PRQD")));
1596 aml_append(sb_scope
, build_link_dev("LNKE", 4, aml_name("PRQE")));
1597 aml_append(sb_scope
, build_link_dev("LNKF", 5, aml_name("PRQF")));
1598 aml_append(sb_scope
, build_link_dev("LNKG", 6, aml_name("PRQG")));
1599 aml_append(sb_scope
, build_link_dev("LNKH", 7, aml_name("PRQH")));
1601 aml_append(sb_scope
, build_gsi_link_dev("GSIA", 0x10, 0x10));
1602 aml_append(sb_scope
, build_gsi_link_dev("GSIB", 0x11, 0x11));
1603 aml_append(sb_scope
, build_gsi_link_dev("GSIC", 0x12, 0x12));
1604 aml_append(sb_scope
, build_gsi_link_dev("GSID", 0x13, 0x13));
1605 aml_append(sb_scope
, build_gsi_link_dev("GSIE", 0x14, 0x14));
1606 aml_append(sb_scope
, build_gsi_link_dev("GSIF", 0x15, 0x15));
1607 aml_append(sb_scope
, build_gsi_link_dev("GSIG", 0x16, 0x16));
1608 aml_append(sb_scope
, build_gsi_link_dev("GSIH", 0x17, 0x17));
1610 aml_append(table
, sb_scope
);
1613 static void build_q35_isa_bridge(Aml
*table
)
1619 scope
= aml_scope("_SB.PCI0");
1620 dev
= aml_device("ISA");
1621 aml_append(dev
, aml_name_decl("_ADR", aml_int(0x001F0000)));
1623 /* ICH9 PCI to ISA irq remapping */
1624 aml_append(dev
, aml_operation_region("PIRQ", AML_PCI_CONFIG
,
1625 aml_int(0x60), 0x0C));
1627 aml_append(dev
, aml_operation_region("LPCD", AML_PCI_CONFIG
,
1628 aml_int(0x80), 0x02));
1629 field
= aml_field("LPCD", AML_ANY_ACC
, AML_NOLOCK
, AML_PRESERVE
);
1630 aml_append(field
, aml_named_field("COMA", 3));
1631 aml_append(field
, aml_reserved_field(1));
1632 aml_append(field
, aml_named_field("COMB", 3));
1633 aml_append(field
, aml_reserved_field(1));
1634 aml_append(field
, aml_named_field("LPTD", 2));
1635 aml_append(dev
, field
);
1637 aml_append(dev
, aml_operation_region("LPCE", AML_PCI_CONFIG
,
1638 aml_int(0x82), 0x02));
1640 field
= aml_field("LPCE", AML_ANY_ACC
, AML_NOLOCK
, AML_PRESERVE
);
1641 aml_append(field
, aml_named_field("CAEN", 1));
1642 aml_append(field
, aml_named_field("CBEN", 1));
1643 aml_append(field
, aml_named_field("LPEN", 1));
1644 aml_append(dev
, field
);
1646 aml_append(scope
, dev
);
1647 aml_append(table
, scope
);
1650 static void build_piix4_pm(Aml
*table
)
1655 scope
= aml_scope("_SB.PCI0");
1656 dev
= aml_device("PX13");
1657 aml_append(dev
, aml_name_decl("_ADR", aml_int(0x00010003)));
1659 aml_append(dev
, aml_operation_region("P13C", AML_PCI_CONFIG
,
1660 aml_int(0x00), 0xff));
1661 aml_append(scope
, dev
);
1662 aml_append(table
, scope
);
1665 static void build_piix4_isa_bridge(Aml
*table
)
1671 scope
= aml_scope("_SB.PCI0");
1672 dev
= aml_device("ISA");
1673 aml_append(dev
, aml_name_decl("_ADR", aml_int(0x00010000)));
1675 /* PIIX PCI to ISA irq remapping */
1676 aml_append(dev
, aml_operation_region("P40C", AML_PCI_CONFIG
,
1677 aml_int(0x60), 0x04));
1679 field
= aml_field("^PX13.P13C", AML_ANY_ACC
, AML_NOLOCK
, AML_PRESERVE
);
1680 /* Offset(0x5f),, 7, */
1681 aml_append(field
, aml_reserved_field(0x2f8));
1682 aml_append(field
, aml_reserved_field(7));
1683 aml_append(field
, aml_named_field("LPEN", 1));
1684 /* Offset(0x67),, 3, */
1685 aml_append(field
, aml_reserved_field(0x38));
1686 aml_append(field
, aml_reserved_field(3));
1687 aml_append(field
, aml_named_field("CAEN", 1));
1688 aml_append(field
, aml_reserved_field(3));
1689 aml_append(field
, aml_named_field("CBEN", 1));
1690 aml_append(dev
, field
);
1692 aml_append(scope
, dev
);
1693 aml_append(table
, scope
);
1696 static void build_piix4_pci_hotplug(Aml
*table
)
1702 scope
= aml_scope("_SB.PCI0");
1705 aml_operation_region("PCST", AML_SYSTEM_IO
, aml_int(0xae00), 0x08));
1706 field
= aml_field("PCST", AML_DWORD_ACC
, AML_NOLOCK
, AML_WRITE_AS_ZEROS
);
1707 aml_append(field
, aml_named_field("PCIU", 32));
1708 aml_append(field
, aml_named_field("PCID", 32));
1709 aml_append(scope
, field
);
1712 aml_operation_region("SEJ", AML_SYSTEM_IO
, aml_int(0xae08), 0x04));
1713 field
= aml_field("SEJ", AML_DWORD_ACC
, AML_NOLOCK
, AML_WRITE_AS_ZEROS
);
1714 aml_append(field
, aml_named_field("B0EJ", 32));
1715 aml_append(scope
, field
);
1718 aml_operation_region("BNMR", AML_SYSTEM_IO
, aml_int(0xae10), 0x04));
1719 field
= aml_field("BNMR", AML_DWORD_ACC
, AML_NOLOCK
, AML_WRITE_AS_ZEROS
);
1720 aml_append(field
, aml_named_field("BNUM", 32));
1721 aml_append(scope
, field
);
1723 aml_append(scope
, aml_mutex("BLCK", 0));
1725 method
= aml_method("PCEJ", 2, AML_NOTSERIALIZED
);
1726 aml_append(method
, aml_acquire(aml_name("BLCK"), 0xFFFF));
1727 aml_append(method
, aml_store(aml_arg(0), aml_name("BNUM")));
1729 aml_store(aml_shiftleft(aml_int(1), aml_arg(1)), aml_name("B0EJ")));
1730 aml_append(method
, aml_release(aml_name("BLCK")));
1731 aml_append(method
, aml_return(aml_int(0)));
1732 aml_append(scope
, method
);
1734 aml_append(table
, scope
);
1737 static Aml
*build_q35_osc_method(void)
1743 Aml
*a_cwd1
= aml_name("CDW1");
1744 Aml
*a_ctrl
= aml_local(0);
1746 method
= aml_method("_OSC", 4, AML_NOTSERIALIZED
);
1747 aml_append(method
, aml_create_dword_field(aml_arg(3), aml_int(0), "CDW1"));
1749 if_ctx
= aml_if(aml_equal(
1750 aml_arg(0), aml_touuid("33DB4D5B-1FF7-401C-9657-7441C03DD766")));
1751 aml_append(if_ctx
, aml_create_dword_field(aml_arg(3), aml_int(4), "CDW2"));
1752 aml_append(if_ctx
, aml_create_dword_field(aml_arg(3), aml_int(8), "CDW3"));
1754 aml_append(if_ctx
, aml_store(aml_name("CDW3"), a_ctrl
));
1757 * Always allow native PME, AER (no dependencies)
1758 * Allow SHPC (PCI bridges can have SHPC controller)
1760 aml_append(if_ctx
, aml_and(a_ctrl
, aml_int(0x1F), a_ctrl
));
1762 if_ctx2
= aml_if(aml_lnot(aml_equal(aml_arg(1), aml_int(1))));
1763 /* Unknown revision */
1764 aml_append(if_ctx2
, aml_or(a_cwd1
, aml_int(0x08), a_cwd1
));
1765 aml_append(if_ctx
, if_ctx2
);
1767 if_ctx2
= aml_if(aml_lnot(aml_equal(aml_name("CDW3"), a_ctrl
)));
1768 /* Capabilities bits were masked */
1769 aml_append(if_ctx2
, aml_or(a_cwd1
, aml_int(0x10), a_cwd1
));
1770 aml_append(if_ctx
, if_ctx2
);
1772 /* Update DWORD3 in the buffer */
1773 aml_append(if_ctx
, aml_store(a_ctrl
, aml_name("CDW3")));
1774 aml_append(method
, if_ctx
);
1776 else_ctx
= aml_else();
1777 /* Unrecognized UUID */
1778 aml_append(else_ctx
, aml_or(a_cwd1
, aml_int(4), a_cwd1
));
1779 aml_append(method
, else_ctx
);
1781 aml_append(method
, aml_return(aml_arg(3)));
1786 build_dsdt(GArray
*table_data
, BIOSLinker
*linker
,
1787 AcpiPmInfo
*pm
, AcpiMiscInfo
*misc
,
1788 Range
*pci_hole
, Range
*pci_hole64
, MachineState
*machine
)
1790 CrsRangeEntry
*entry
;
1791 Aml
*dsdt
, *sb_scope
, *scope
, *dev
, *method
, *field
, *pkg
, *crs
;
1792 CrsRangeSet crs_range_set
;
1793 PCMachineState
*pcms
= PC_MACHINE(machine
);
1794 PCMachineClass
*pcmc
= PC_MACHINE_GET_CLASS(machine
);
1795 uint32_t nr_mem
= machine
->ram_slots
;
1796 int root_bus_limit
= 0xFF;
1800 dsdt
= init_aml_allocator();
1802 /* Reserve space for header */
1803 acpi_data_push(dsdt
->buf
, sizeof(AcpiTableHeader
));
1805 build_dbg_aml(dsdt
);
1806 if (misc
->is_piix4
) {
1807 sb_scope
= aml_scope("_SB");
1808 dev
= aml_device("PCI0");
1809 aml_append(dev
, aml_name_decl("_HID", aml_eisaid("PNP0A03")));
1810 aml_append(dev
, aml_name_decl("_ADR", aml_int(0)));
1811 aml_append(dev
, aml_name_decl("_UID", aml_int(1)));
1812 aml_append(sb_scope
, dev
);
1813 aml_append(dsdt
, sb_scope
);
1815 build_hpet_aml(dsdt
);
1816 build_piix4_pm(dsdt
);
1817 build_piix4_isa_bridge(dsdt
);
1818 build_isa_devices_aml(dsdt
);
1819 build_piix4_pci_hotplug(dsdt
);
1820 build_piix4_pci0_int(dsdt
);
1822 sb_scope
= aml_scope("_SB");
1823 dev
= aml_device("PCI0");
1824 aml_append(dev
, aml_name_decl("_HID", aml_eisaid("PNP0A08")));
1825 aml_append(dev
, aml_name_decl("_CID", aml_eisaid("PNP0A03")));
1826 aml_append(dev
, aml_name_decl("_ADR", aml_int(0)));
1827 aml_append(dev
, aml_name_decl("_UID", aml_int(1)));
1828 aml_append(dev
, build_q35_osc_method());
1829 aml_append(sb_scope
, dev
);
1830 aml_append(dsdt
, sb_scope
);
1832 build_hpet_aml(dsdt
);
1833 build_q35_isa_bridge(dsdt
);
1834 build_isa_devices_aml(dsdt
);
1835 build_q35_pci0_int(dsdt
);
1838 if (pcmc
->legacy_cpu_hotplug
) {
1839 build_legacy_cpu_hotplug_aml(dsdt
, machine
, pm
->cpu_hp_io_base
);
1841 CPUHotplugFeatures opts
= {
1842 .apci_1_compatible
= true, .has_legacy_cphp
= true
1844 build_cpus_aml(dsdt
, machine
, opts
, pm
->cpu_hp_io_base
,
1845 "\\_SB.PCI0", "\\_GPE._E02");
1847 build_memory_hotplug_aml(dsdt
, nr_mem
, "\\_SB.PCI0", "\\_GPE._E03");
1849 scope
= aml_scope("_GPE");
1851 aml_append(scope
, aml_name_decl("_HID", aml_string("ACPI0006")));
1853 if (misc
->is_piix4
) {
1854 method
= aml_method("_E01", 0, AML_NOTSERIALIZED
);
1856 aml_acquire(aml_name("\\_SB.PCI0.BLCK"), 0xFFFF));
1857 aml_append(method
, aml_call0("\\_SB.PCI0.PCNT"));
1858 aml_append(method
, aml_release(aml_name("\\_SB.PCI0.BLCK")));
1859 aml_append(scope
, method
);
1862 if (pcms
->acpi_nvdimm_state
.is_enabled
) {
1863 method
= aml_method("_E04", 0, AML_NOTSERIALIZED
);
1864 aml_append(method
, aml_notify(aml_name("\\_SB.NVDR"),
1866 aml_append(scope
, method
);
1869 aml_append(dsdt
, scope
);
1871 crs_range_set_init(&crs_range_set
);
1872 bus
= PC_MACHINE(machine
)->bus
;
1874 QLIST_FOREACH(bus
, &bus
->child
, sibling
) {
1875 uint8_t bus_num
= pci_bus_num(bus
);
1876 uint8_t numa_node
= pci_bus_numa_node(bus
);
1878 /* look only for expander root buses */
1879 if (!pci_bus_is_root(bus
)) {
1883 if (bus_num
< root_bus_limit
) {
1884 root_bus_limit
= bus_num
- 1;
1887 scope
= aml_scope("\\_SB");
1888 dev
= aml_device("PC%.02X", bus_num
);
1889 aml_append(dev
, aml_name_decl("_UID", aml_int(bus_num
)));
1890 aml_append(dev
, aml_name_decl("_HID", aml_eisaid("PNP0A03")));
1891 aml_append(dev
, aml_name_decl("_BBN", aml_int(bus_num
)));
1892 if (pci_bus_is_express(bus
)) {
1893 aml_append(dev
, build_q35_osc_method());
1896 if (numa_node
!= NUMA_NODE_UNASSIGNED
) {
1897 aml_append(dev
, aml_name_decl("_PXM", aml_int(numa_node
)));
1900 aml_append(dev
, build_prt(false));
1901 crs
= build_crs(PCI_HOST_BRIDGE(BUS(bus
)->parent
), &crs_range_set
);
1902 aml_append(dev
, aml_name_decl("_CRS", crs
));
1903 aml_append(scope
, dev
);
1904 aml_append(dsdt
, scope
);
1908 scope
= aml_scope("\\_SB.PCI0");
1909 /* build PCI0._CRS */
1910 crs
= aml_resource_template();
1912 aml_word_bus_number(AML_MIN_FIXED
, AML_MAX_FIXED
, AML_POS_DECODE
,
1913 0x0000, 0x0, root_bus_limit
,
1914 0x0000, root_bus_limit
+ 1));
1915 aml_append(crs
, aml_io(AML_DECODE16
, 0x0CF8, 0x0CF8, 0x01, 0x08));
1918 aml_word_io(AML_MIN_FIXED
, AML_MAX_FIXED
,
1919 AML_POS_DECODE
, AML_ENTIRE_RANGE
,
1920 0x0000, 0x0000, 0x0CF7, 0x0000, 0x0CF8));
1922 crs_replace_with_free_ranges(crs_range_set
.io_ranges
, 0x0D00, 0xFFFF);
1923 for (i
= 0; i
< crs_range_set
.io_ranges
->len
; i
++) {
1924 entry
= g_ptr_array_index(crs_range_set
.io_ranges
, i
);
1926 aml_word_io(AML_MIN_FIXED
, AML_MAX_FIXED
,
1927 AML_POS_DECODE
, AML_ENTIRE_RANGE
,
1928 0x0000, entry
->base
, entry
->limit
,
1929 0x0000, entry
->limit
- entry
->base
+ 1));
1933 aml_dword_memory(AML_POS_DECODE
, AML_MIN_FIXED
, AML_MAX_FIXED
,
1934 AML_CACHEABLE
, AML_READ_WRITE
,
1935 0, 0x000A0000, 0x000BFFFF, 0, 0x00020000));
1937 crs_replace_with_free_ranges(crs_range_set
.mem_ranges
,
1938 range_lob(pci_hole
),
1939 range_upb(pci_hole
));
1940 for (i
= 0; i
< crs_range_set
.mem_ranges
->len
; i
++) {
1941 entry
= g_ptr_array_index(crs_range_set
.mem_ranges
, i
);
1943 aml_dword_memory(AML_POS_DECODE
, AML_MIN_FIXED
, AML_MAX_FIXED
,
1944 AML_NON_CACHEABLE
, AML_READ_WRITE
,
1945 0, entry
->base
, entry
->limit
,
1946 0, entry
->limit
- entry
->base
+ 1));
1949 if (!range_is_empty(pci_hole64
)) {
1950 crs_replace_with_free_ranges(crs_range_set
.mem_64bit_ranges
,
1951 range_lob(pci_hole64
),
1952 range_upb(pci_hole64
));
1953 for (i
= 0; i
< crs_range_set
.mem_64bit_ranges
->len
; i
++) {
1954 entry
= g_ptr_array_index(crs_range_set
.mem_64bit_ranges
, i
);
1956 aml_qword_memory(AML_POS_DECODE
, AML_MIN_FIXED
,
1958 AML_CACHEABLE
, AML_READ_WRITE
,
1959 0, entry
->base
, entry
->limit
,
1960 0, entry
->limit
- entry
->base
+ 1));
1964 if (TPM_IS_TIS(tpm_find())) {
1965 aml_append(crs
, aml_memory32_fixed(TPM_TIS_ADDR_BASE
,
1966 TPM_TIS_ADDR_SIZE
, AML_READ_WRITE
));
1968 aml_append(scope
, aml_name_decl("_CRS", crs
));
1970 /* reserve GPE0 block resources */
1971 dev
= aml_device("GPE0");
1972 aml_append(dev
, aml_name_decl("_HID", aml_string("PNP0A06")));
1973 aml_append(dev
, aml_name_decl("_UID", aml_string("GPE0 resources")));
1974 /* device present, functioning, decoding, not shown in UI */
1975 aml_append(dev
, aml_name_decl("_STA", aml_int(0xB)));
1976 crs
= aml_resource_template();
1980 pm
->fadt
.gpe0_blk
.address
,
1981 pm
->fadt
.gpe0_blk
.address
,
1983 pm
->fadt
.gpe0_blk
.bit_width
/ 8)
1985 aml_append(dev
, aml_name_decl("_CRS", crs
));
1986 aml_append(scope
, dev
);
1988 crs_range_set_free(&crs_range_set
);
1990 /* reserve PCIHP resources */
1991 if (pm
->pcihp_io_len
) {
1992 dev
= aml_device("PHPR");
1993 aml_append(dev
, aml_name_decl("_HID", aml_string("PNP0A06")));
1995 aml_name_decl("_UID", aml_string("PCI Hotplug resources")));
1996 /* device present, functioning, decoding, not shown in UI */
1997 aml_append(dev
, aml_name_decl("_STA", aml_int(0xB)));
1998 crs
= aml_resource_template();
2000 aml_io(AML_DECODE16
, pm
->pcihp_io_base
, pm
->pcihp_io_base
, 1,
2003 aml_append(dev
, aml_name_decl("_CRS", crs
));
2004 aml_append(scope
, dev
);
2006 aml_append(dsdt
, scope
);
2008 /* create S3_ / S4_ / S5_ packages if necessary */
2009 scope
= aml_scope("\\");
2010 if (!pm
->s3_disabled
) {
2011 pkg
= aml_package(4);
2012 aml_append(pkg
, aml_int(1)); /* PM1a_CNT.SLP_TYP */
2013 aml_append(pkg
, aml_int(1)); /* PM1b_CNT.SLP_TYP, FIXME: not impl. */
2014 aml_append(pkg
, aml_int(0)); /* reserved */
2015 aml_append(pkg
, aml_int(0)); /* reserved */
2016 aml_append(scope
, aml_name_decl("_S3", pkg
));
2019 if (!pm
->s4_disabled
) {
2020 pkg
= aml_package(4);
2021 aml_append(pkg
, aml_int(pm
->s4_val
)); /* PM1a_CNT.SLP_TYP */
2022 /* PM1b_CNT.SLP_TYP, FIXME: not impl. */
2023 aml_append(pkg
, aml_int(pm
->s4_val
));
2024 aml_append(pkg
, aml_int(0)); /* reserved */
2025 aml_append(pkg
, aml_int(0)); /* reserved */
2026 aml_append(scope
, aml_name_decl("_S4", pkg
));
2029 pkg
= aml_package(4);
2030 aml_append(pkg
, aml_int(0)); /* PM1a_CNT.SLP_TYP */
2031 aml_append(pkg
, aml_int(0)); /* PM1b_CNT.SLP_TYP not impl. */
2032 aml_append(pkg
, aml_int(0)); /* reserved */
2033 aml_append(pkg
, aml_int(0)); /* reserved */
2034 aml_append(scope
, aml_name_decl("_S5", pkg
));
2035 aml_append(dsdt
, scope
);
2037 /* create fw_cfg node, unconditionally */
2039 /* when using port i/o, the 8-bit data register *always* overlaps
2040 * with half of the 16-bit control register. Hence, the total size
2041 * of the i/o region used is FW_CFG_CTL_SIZE; when using DMA, the
2042 * DMA control register is located at FW_CFG_DMA_IO_BASE + 4 */
2043 uint8_t io_size
= object_property_get_bool(OBJECT(pcms
->fw_cfg
),
2044 "dma_enabled", NULL
) ?
2045 ROUND_UP(FW_CFG_CTL_SIZE
, 4) + sizeof(dma_addr_t
) :
2048 scope
= aml_scope("\\_SB.PCI0");
2049 dev
= aml_device("FWCF");
2051 aml_append(dev
, aml_name_decl("_HID", aml_string("QEMU0002")));
2053 /* device present, functioning, decoding, not shown in UI */
2054 aml_append(dev
, aml_name_decl("_STA", aml_int(0xB)));
2056 crs
= aml_resource_template();
2058 aml_io(AML_DECODE16
, FW_CFG_IO_BASE
, FW_CFG_IO_BASE
, 0x01, io_size
)
2060 aml_append(dev
, aml_name_decl("_CRS", crs
));
2062 aml_append(scope
, dev
);
2063 aml_append(dsdt
, scope
);
2066 if (misc
->applesmc_io_base
) {
2067 scope
= aml_scope("\\_SB.PCI0.ISA");
2068 dev
= aml_device("SMC");
2070 aml_append(dev
, aml_name_decl("_HID", aml_eisaid("APP0001")));
2071 /* device present, functioning, decoding, not shown in UI */
2072 aml_append(dev
, aml_name_decl("_STA", aml_int(0xB)));
2074 crs
= aml_resource_template();
2076 aml_io(AML_DECODE16
, misc
->applesmc_io_base
, misc
->applesmc_io_base
,
2077 0x01, APPLESMC_MAX_DATA_LENGTH
)
2079 aml_append(crs
, aml_irq_no_flags(6));
2080 aml_append(dev
, aml_name_decl("_CRS", crs
));
2082 aml_append(scope
, dev
);
2083 aml_append(dsdt
, scope
);
2086 if (misc
->pvpanic_port
) {
2087 scope
= aml_scope("\\_SB.PCI0.ISA");
2089 dev
= aml_device("PEVT");
2090 aml_append(dev
, aml_name_decl("_HID", aml_string("QEMU0001")));
2092 crs
= aml_resource_template();
2094 aml_io(AML_DECODE16
, misc
->pvpanic_port
, misc
->pvpanic_port
, 1, 1)
2096 aml_append(dev
, aml_name_decl("_CRS", crs
));
2098 aml_append(dev
, aml_operation_region("PEOR", AML_SYSTEM_IO
,
2099 aml_int(misc
->pvpanic_port
), 1));
2100 field
= aml_field("PEOR", AML_BYTE_ACC
, AML_NOLOCK
, AML_PRESERVE
);
2101 aml_append(field
, aml_named_field("PEPT", 8));
2102 aml_append(dev
, field
);
2104 /* device present, functioning, decoding, shown in UI */
2105 aml_append(dev
, aml_name_decl("_STA", aml_int(0xF)));
2107 method
= aml_method("RDPT", 0, AML_NOTSERIALIZED
);
2108 aml_append(method
, aml_store(aml_name("PEPT"), aml_local(0)));
2109 aml_append(method
, aml_return(aml_local(0)));
2110 aml_append(dev
, method
);
2112 method
= aml_method("WRPT", 1, AML_NOTSERIALIZED
);
2113 aml_append(method
, aml_store(aml_arg(0), aml_name("PEPT")));
2114 aml_append(dev
, method
);
2116 aml_append(scope
, dev
);
2117 aml_append(dsdt
, scope
);
2120 sb_scope
= aml_scope("\\_SB");
2125 pci_host
= acpi_get_i386_pci_host();
2127 bus
= PCI_HOST_BRIDGE(pci_host
)->bus
;
2131 Aml
*scope
= aml_scope("PCI0");
2132 /* Scan all PCI buses. Generate tables to support hotplug. */
2133 build_append_pci_bus_devices(scope
, bus
, pm
->pcihp_bridge_en
);
2135 if (TPM_IS_TIS(tpm_find())) {
2136 dev
= aml_device("ISA.TPM");
2137 aml_append(dev
, aml_name_decl("_HID", aml_eisaid("PNP0C31")));
2138 aml_append(dev
, aml_name_decl("_STA", aml_int(0xF)));
2139 crs
= aml_resource_template();
2140 aml_append(crs
, aml_memory32_fixed(TPM_TIS_ADDR_BASE
,
2141 TPM_TIS_ADDR_SIZE
, AML_READ_WRITE
));
2143 FIXME: TPM_TIS_IRQ=5 conflicts with PNP0C0F irqs,
2144 Rewrite to take IRQ from TPM device model and
2145 fix default IRQ value there to use some unused IRQ
2147 /* aml_append(crs, aml_irq_no_flags(TPM_TIS_IRQ)); */
2148 aml_append(dev
, aml_name_decl("_CRS", crs
));
2149 aml_append(scope
, dev
);
2152 aml_append(sb_scope
, scope
);
2156 if (TPM_IS_CRB(tpm_find())) {
2157 dev
= aml_device("TPM");
2158 aml_append(dev
, aml_name_decl("_HID", aml_string("MSFT0101")));
2159 crs
= aml_resource_template();
2160 aml_append(crs
, aml_memory32_fixed(TPM_CRB_ADDR_BASE
,
2161 TPM_CRB_ADDR_SIZE
, AML_READ_WRITE
));
2162 aml_append(dev
, aml_name_decl("_CRS", crs
));
2164 method
= aml_method("_STA", 0, AML_NOTSERIALIZED
);
2165 aml_append(method
, aml_return(aml_int(0x0f)));
2166 aml_append(dev
, method
);
2168 aml_append(sb_scope
, dev
);
2171 aml_append(dsdt
, sb_scope
);
2173 /* copy AML table into ACPI tables blob and patch header there */
2174 g_array_append_vals(table_data
, dsdt
->buf
->data
, dsdt
->buf
->len
);
2175 build_header(linker
, table_data
,
2176 (void *)(table_data
->data
+ table_data
->len
- dsdt
->buf
->len
),
2177 "DSDT", dsdt
->buf
->len
, 1, NULL
, NULL
);
2178 free_aml_allocator();
2182 build_hpet(GArray
*table_data
, BIOSLinker
*linker
)
2186 hpet
= acpi_data_push(table_data
, sizeof(*hpet
));
2187 /* Note timer_block_id value must be kept in sync with value advertised by
2190 hpet
->timer_block_id
= cpu_to_le32(0x8086a201);
2191 hpet
->addr
.address
= cpu_to_le64(HPET_BASE
);
2192 build_header(linker
, table_data
,
2193 (void *)hpet
, "HPET", sizeof(*hpet
), 1, NULL
, NULL
);
2197 build_tpm_tcpa(GArray
*table_data
, BIOSLinker
*linker
, GArray
*tcpalog
)
2199 Acpi20Tcpa
*tcpa
= acpi_data_push(table_data
, sizeof *tcpa
);
2200 unsigned log_addr_size
= sizeof(tcpa
->log_area_start_address
);
2201 unsigned log_addr_offset
=
2202 (char *)&tcpa
->log_area_start_address
- table_data
->data
;
2204 tcpa
->platform_class
= cpu_to_le16(TPM_TCPA_ACPI_CLASS_CLIENT
);
2205 tcpa
->log_area_minimum_length
= cpu_to_le32(TPM_LOG_AREA_MINIMUM_SIZE
);
2206 acpi_data_push(tcpalog
, le32_to_cpu(tcpa
->log_area_minimum_length
));
2208 bios_linker_loader_alloc(linker
, ACPI_BUILD_TPMLOG_FILE
, tcpalog
, 1,
2209 false /* high memory */);
2211 /* log area start address to be filled by Guest linker */
2212 bios_linker_loader_add_pointer(linker
,
2213 ACPI_BUILD_TABLE_FILE
, log_addr_offset
, log_addr_size
,
2214 ACPI_BUILD_TPMLOG_FILE
, 0);
2216 build_header(linker
, table_data
,
2217 (void *)tcpa
, "TCPA", sizeof(*tcpa
), 2, NULL
, NULL
);
2221 build_tpm2(GArray
*table_data
, BIOSLinker
*linker
, GArray
*tcpalog
)
2223 Acpi20TPM2
*tpm2_ptr
= acpi_data_push(table_data
, sizeof *tpm2_ptr
);
2224 unsigned log_addr_size
= sizeof(tpm2_ptr
->log_area_start_address
);
2225 unsigned log_addr_offset
=
2226 (char *)&tpm2_ptr
->log_area_start_address
- table_data
->data
;
2228 tpm2_ptr
->platform_class
= cpu_to_le16(TPM2_ACPI_CLASS_CLIENT
);
2229 if (TPM_IS_TIS(tpm_find())) {
2230 tpm2_ptr
->control_area_address
= cpu_to_le64(0);
2231 tpm2_ptr
->start_method
= cpu_to_le32(TPM2_START_METHOD_MMIO
);
2232 } else if (TPM_IS_CRB(tpm_find())) {
2233 tpm2_ptr
->control_area_address
= cpu_to_le64(TPM_CRB_ADDR_CTRL
);
2234 tpm2_ptr
->start_method
= cpu_to_le32(TPM2_START_METHOD_CRB
);
2236 g_warn_if_reached();
2239 tpm2_ptr
->log_area_minimum_length
=
2240 cpu_to_le32(TPM_LOG_AREA_MINIMUM_SIZE
);
2242 /* log area start address to be filled by Guest linker */
2243 bios_linker_loader_add_pointer(linker
, ACPI_BUILD_TABLE_FILE
,
2244 log_addr_offset
, log_addr_size
,
2245 ACPI_BUILD_TPMLOG_FILE
, 0);
2246 build_header(linker
, table_data
,
2247 (void *)tpm2_ptr
, "TPM2", sizeof(*tpm2_ptr
), 4, NULL
, NULL
);
2250 #define HOLE_640K_START (640 * 1024)
2251 #define HOLE_640K_END (1024 * 1024)
2253 static void build_srat_hotpluggable_memory(GArray
*table_data
, uint64_t base
,
2254 uint64_t len
, int default_node
)
2256 MemoryDeviceInfoList
*info_list
= qmp_pc_dimm_device_list();
2257 MemoryDeviceInfoList
*info
;
2258 MemoryDeviceInfo
*mi
;
2259 PCDIMMDeviceInfo
*di
;
2260 uint64_t end
= base
+ len
, cur
, size
;
2262 AcpiSratMemoryAffinity
*numamem
;
2263 MemoryAffinityFlags flags
;
2265 for (cur
= base
, info
= info_list
;
2267 cur
+= size
, info
= info
->next
) {
2268 numamem
= acpi_data_push(table_data
, sizeof *numamem
);
2271 build_srat_memory(numamem
, cur
, end
- cur
, default_node
,
2272 MEM_AFFINITY_HOTPLUGGABLE
| MEM_AFFINITY_ENABLED
);
2277 is_nvdimm
= (mi
->type
== MEMORY_DEVICE_INFO_KIND_NVDIMM
);
2278 di
= !is_nvdimm
? mi
->u
.dimm
.data
: mi
->u
.nvdimm
.data
;
2280 if (cur
< di
->addr
) {
2281 build_srat_memory(numamem
, cur
, di
->addr
- cur
, default_node
,
2282 MEM_AFFINITY_HOTPLUGGABLE
| MEM_AFFINITY_ENABLED
);
2283 numamem
= acpi_data_push(table_data
, sizeof *numamem
);
2288 flags
= MEM_AFFINITY_ENABLED
;
2289 if (di
->hotpluggable
) {
2290 flags
|= MEM_AFFINITY_HOTPLUGGABLE
;
2293 flags
|= MEM_AFFINITY_NON_VOLATILE
;
2296 build_srat_memory(numamem
, di
->addr
, size
, di
->node
, flags
);
2299 qapi_free_MemoryDeviceInfoList(info_list
);
2303 build_srat(GArray
*table_data
, BIOSLinker
*linker
, MachineState
*machine
)
2305 AcpiSystemResourceAffinityTable
*srat
;
2306 AcpiSratMemoryAffinity
*numamem
;
2309 int srat_start
, numa_start
, slots
;
2310 uint64_t mem_len
, mem_base
, next_base
;
2311 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
2312 const CPUArchIdList
*apic_ids
= mc
->possible_cpu_arch_ids(machine
);
2313 PCMachineState
*pcms
= PC_MACHINE(machine
);
2314 ram_addr_t hotplugabble_address_space_size
=
2315 object_property_get_int(OBJECT(pcms
), PC_MACHINE_MEMHP_REGION_SIZE
,
2318 srat_start
= table_data
->len
;
2320 srat
= acpi_data_push(table_data
, sizeof *srat
);
2321 srat
->reserved1
= cpu_to_le32(1);
2323 for (i
= 0; i
< apic_ids
->len
; i
++) {
2324 int node_id
= apic_ids
->cpus
[i
].props
.node_id
;
2325 uint32_t apic_id
= apic_ids
->cpus
[i
].arch_id
;
2327 if (apic_id
< 255) {
2328 AcpiSratProcessorAffinity
*core
;
2330 core
= acpi_data_push(table_data
, sizeof *core
);
2331 core
->type
= ACPI_SRAT_PROCESSOR_APIC
;
2332 core
->length
= sizeof(*core
);
2333 core
->local_apic_id
= apic_id
;
2334 core
->proximity_lo
= node_id
;
2335 memset(core
->proximity_hi
, 0, 3);
2336 core
->local_sapic_eid
= 0;
2337 core
->flags
= cpu_to_le32(1);
2339 AcpiSratProcessorX2ApicAffinity
*core
;
2341 core
= acpi_data_push(table_data
, sizeof *core
);
2342 core
->type
= ACPI_SRAT_PROCESSOR_x2APIC
;
2343 core
->length
= sizeof(*core
);
2344 core
->x2apic_id
= cpu_to_le32(apic_id
);
2345 core
->proximity_domain
= cpu_to_le32(node_id
);
2346 core
->flags
= cpu_to_le32(1);
2351 /* the memory map is a bit tricky, it contains at least one hole
2352 * from 640k-1M and possibly another one from 3.5G-4G.
2355 numa_start
= table_data
->len
;
2357 for (i
= 1; i
< pcms
->numa_nodes
+ 1; ++i
) {
2358 mem_base
= next_base
;
2359 mem_len
= pcms
->node_mem
[i
- 1];
2360 next_base
= mem_base
+ mem_len
;
2362 /* Cut out the 640K hole */
2363 if (mem_base
<= HOLE_640K_START
&&
2364 next_base
> HOLE_640K_START
) {
2365 mem_len
-= next_base
- HOLE_640K_START
;
2367 numamem
= acpi_data_push(table_data
, sizeof *numamem
);
2368 build_srat_memory(numamem
, mem_base
, mem_len
, i
- 1,
2369 MEM_AFFINITY_ENABLED
);
2372 /* Check for the rare case: 640K < RAM < 1M */
2373 if (next_base
<= HOLE_640K_END
) {
2374 next_base
= HOLE_640K_END
;
2377 mem_base
= HOLE_640K_END
;
2378 mem_len
= next_base
- HOLE_640K_END
;
2381 /* Cut out the ACPI_PCI hole */
2382 if (mem_base
<= pcms
->below_4g_mem_size
&&
2383 next_base
> pcms
->below_4g_mem_size
) {
2384 mem_len
-= next_base
- pcms
->below_4g_mem_size
;
2386 numamem
= acpi_data_push(table_data
, sizeof *numamem
);
2387 build_srat_memory(numamem
, mem_base
, mem_len
, i
- 1,
2388 MEM_AFFINITY_ENABLED
);
2390 mem_base
= 1ULL << 32;
2391 mem_len
= next_base
- pcms
->below_4g_mem_size
;
2392 next_base
= mem_base
+ mem_len
;
2394 numamem
= acpi_data_push(table_data
, sizeof *numamem
);
2395 build_srat_memory(numamem
, mem_base
, mem_len
, i
- 1,
2396 MEM_AFFINITY_ENABLED
);
2398 slots
= (table_data
->len
- numa_start
) / sizeof *numamem
;
2399 for (; slots
< pcms
->numa_nodes
+ 2; slots
++) {
2400 numamem
= acpi_data_push(table_data
, sizeof *numamem
);
2401 build_srat_memory(numamem
, 0, 0, 0, MEM_AFFINITY_NOFLAGS
);
2405 * Entry is required for Windows to enable memory hotplug in OS
2406 * and for Linux to enable SWIOTLB when booted with less than
2407 * 4G of RAM. Windows works better if the entry sets proximity
2408 * to the highest NUMA node in the machine.
2409 * Memory devices may override proximity set by this entry,
2410 * providing _PXM method if necessary.
2412 if (hotplugabble_address_space_size
) {
2413 build_srat_hotpluggable_memory(table_data
, pcms
->hotplug_memory
.base
,
2414 hotplugabble_address_space_size
,
2415 pcms
->numa_nodes
- 1);
2418 build_header(linker
, table_data
,
2419 (void *)(table_data
->data
+ srat_start
),
2421 table_data
->len
- srat_start
, 1, NULL
, NULL
);
2425 build_mcfg_q35(GArray
*table_data
, BIOSLinker
*linker
, AcpiMcfgInfo
*info
)
2427 AcpiTableMcfg
*mcfg
;
2429 int len
= sizeof(*mcfg
) + 1 * sizeof(mcfg
->allocation
[0]);
2431 mcfg
= acpi_data_push(table_data
, len
);
2432 mcfg
->allocation
[0].address
= cpu_to_le64(info
->mcfg_base
);
2433 /* Only a single allocation so no need to play with segments */
2434 mcfg
->allocation
[0].pci_segment
= cpu_to_le16(0);
2435 mcfg
->allocation
[0].start_bus_number
= 0;
2436 mcfg
->allocation
[0].end_bus_number
= PCIE_MMCFG_BUS(info
->mcfg_size
- 1);
2438 /* MCFG is used for ECAM which can be enabled or disabled by guest.
2439 * To avoid table size changes (which create migration issues),
2440 * always create the table even if there are no allocations,
2441 * but set the signature to a reserved value in this case.
2442 * ACPI spec requires OSPMs to ignore such tables.
2444 if (info
->mcfg_base
== PCIE_BASE_ADDR_UNMAPPED
) {
2445 /* Reserved signature: ignored by OSPM */
2450 build_header(linker
, table_data
, (void *)mcfg
, sig
, len
, 1, NULL
, NULL
);
2454 * VT-d spec 8.1 DMA Remapping Reporting Structure
2455 * (version Oct. 2014 or later)
2458 build_dmar_q35(GArray
*table_data
, BIOSLinker
*linker
)
2460 int dmar_start
= table_data
->len
;
2462 AcpiTableDmar
*dmar
;
2463 AcpiDmarHardwareUnit
*drhd
;
2464 AcpiDmarRootPortATS
*atsr
;
2465 uint8_t dmar_flags
= 0;
2466 X86IOMMUState
*iommu
= x86_iommu_get_default();
2467 AcpiDmarDeviceScope
*scope
= NULL
;
2468 /* Root complex IOAPIC use one path[0] only */
2469 size_t ioapic_scope_size
= sizeof(*scope
) + sizeof(scope
->path
[0]);
2470 IntelIOMMUState
*intel_iommu
= INTEL_IOMMU_DEVICE(iommu
);
2473 if (iommu
->intr_supported
) {
2474 dmar_flags
|= 0x1; /* Flags: 0x1: INT_REMAP */
2477 dmar
= acpi_data_push(table_data
, sizeof(*dmar
));
2478 dmar
->host_address_width
= intel_iommu
->aw_bits
- 1;
2479 dmar
->flags
= dmar_flags
;
2481 /* DMAR Remapping Hardware Unit Definition structure */
2482 drhd
= acpi_data_push(table_data
, sizeof(*drhd
) + ioapic_scope_size
);
2483 drhd
->type
= cpu_to_le16(ACPI_DMAR_TYPE_HARDWARE_UNIT
);
2484 drhd
->length
= cpu_to_le16(sizeof(*drhd
) + ioapic_scope_size
);
2485 drhd
->flags
= ACPI_DMAR_INCLUDE_PCI_ALL
;
2486 drhd
->pci_segment
= cpu_to_le16(0);
2487 drhd
->address
= cpu_to_le64(Q35_HOST_BRIDGE_IOMMU_ADDR
);
2489 /* Scope definition for the root-complex IOAPIC. See VT-d spec
2490 * 8.3.1 (version Oct. 2014 or later). */
2491 scope
= &drhd
->scope
[0];
2492 scope
->entry_type
= 0x03; /* Type: 0x03 for IOAPIC */
2493 scope
->length
= ioapic_scope_size
;
2494 scope
->enumeration_id
= ACPI_BUILD_IOAPIC_ID
;
2495 scope
->bus
= Q35_PSEUDO_BUS_PLATFORM
;
2496 scope
->path
[0].device
= PCI_SLOT(Q35_PSEUDO_DEVFN_IOAPIC
);
2497 scope
->path
[0].function
= PCI_FUNC(Q35_PSEUDO_DEVFN_IOAPIC
);
2499 if (iommu
->dt_supported
) {
2500 atsr
= acpi_data_push(table_data
, sizeof(*atsr
));
2501 atsr
->type
= cpu_to_le16(ACPI_DMAR_TYPE_ATSR
);
2502 atsr
->length
= cpu_to_le16(sizeof(*atsr
));
2503 atsr
->flags
= ACPI_DMAR_ATSR_ALL_PORTS
;
2504 atsr
->pci_segment
= cpu_to_le16(0);
2507 build_header(linker
, table_data
, (void *)(table_data
->data
+ dmar_start
),
2508 "DMAR", table_data
->len
- dmar_start
, 1, NULL
, NULL
);
2511 * IVRS table as specified in AMD IOMMU Specification v2.62, Section 5.2
2512 * accessible here http://support.amd.com/TechDocs/48882_IOMMU.pdf
2515 build_amd_iommu(GArray
*table_data
, BIOSLinker
*linker
)
2517 int iommu_start
= table_data
->len
;
2518 AMDVIState
*s
= AMD_IOMMU_DEVICE(x86_iommu_get_default());
2521 acpi_data_push(table_data
, sizeof(AcpiTableHeader
));
2522 /* IVinfo - IO virtualization information common to all
2523 * IOMMU units in a system
2525 build_append_int_noprefix(table_data
, 40UL << 8/* PASize */, 4);
2527 build_append_int_noprefix(table_data
, 0, 8);
2529 /* IVHD definition - type 10h */
2530 build_append_int_noprefix(table_data
, 0x10, 1);
2531 /* virtualization flags */
2532 build_append_int_noprefix(table_data
,
2533 (1UL << 0) | /* HtTunEn */
2534 (1UL << 4) | /* iotblSup */
2535 (1UL << 6) | /* PrefSup */
2536 (1UL << 7), /* PPRSup */
2539 build_append_int_noprefix(table_data
, 0x24, 2);
2541 build_append_int_noprefix(table_data
, s
->devid
, 2);
2542 /* Capability offset */
2543 build_append_int_noprefix(table_data
, s
->capab_offset
, 2);
2544 /* IOMMU base address */
2545 build_append_int_noprefix(table_data
, s
->mmio
.addr
, 8);
2546 /* PCI Segment Group */
2547 build_append_int_noprefix(table_data
, 0, 2);
2549 build_append_int_noprefix(table_data
, 0, 2);
2550 /* IOMMU Feature Reporting */
2551 build_append_int_noprefix(table_data
,
2552 (48UL << 30) | /* HATS */
2553 (48UL << 28) | /* GATS */
2554 (1UL << 2), /* GTSup */
2557 * Type 1 device entry reporting all devices
2558 * These are 4-byte device entries currently reporting the range of
2559 * Refer to Spec - Table 95:IVHD Device Entry Type Codes(4-byte)
2561 build_append_int_noprefix(table_data
, 0x0000001, 4);
2563 build_header(linker
, table_data
, (void *)(table_data
->data
+ iommu_start
),
2564 "IVRS", table_data
->len
- iommu_start
, 1, NULL
, NULL
);
2568 build_rsdp(GArray
*rsdp_table
, BIOSLinker
*linker
, unsigned rsdt_tbl_offset
)
2570 AcpiRsdpDescriptor
*rsdp
= acpi_data_push(rsdp_table
, sizeof *rsdp
);
2571 unsigned rsdt_pa_size
= sizeof(rsdp
->rsdt_physical_address
);
2572 unsigned rsdt_pa_offset
=
2573 (char *)&rsdp
->rsdt_physical_address
- rsdp_table
->data
;
2575 bios_linker_loader_alloc(linker
, ACPI_BUILD_RSDP_FILE
, rsdp_table
, 16,
2576 true /* fseg memory */);
2578 memcpy(&rsdp
->signature
, "RSD PTR ", 8);
2579 memcpy(rsdp
->oem_id
, ACPI_BUILD_APPNAME6
, 6);
2580 /* Address to be filled by Guest linker */
2581 bios_linker_loader_add_pointer(linker
,
2582 ACPI_BUILD_RSDP_FILE
, rsdt_pa_offset
, rsdt_pa_size
,
2583 ACPI_BUILD_TABLE_FILE
, rsdt_tbl_offset
);
2585 /* Checksum to be filled by Guest linker */
2586 bios_linker_loader_add_checksum(linker
, ACPI_BUILD_RSDP_FILE
,
2587 (char *)rsdp
- rsdp_table
->data
, sizeof *rsdp
,
2588 (char *)&rsdp
->checksum
- rsdp_table
->data
);
2594 struct AcpiBuildState
{
2595 /* Copy of table in RAM (for patching). */
2596 MemoryRegion
*table_mr
;
2597 /* Is table patched? */
2600 MemoryRegion
*rsdp_mr
;
2601 MemoryRegion
*linker_mr
;
2604 static bool acpi_get_mcfg(AcpiMcfgInfo
*mcfg
)
2609 pci_host
= acpi_get_i386_pci_host();
2612 o
= object_property_get_qobject(pci_host
, PCIE_HOST_MCFG_BASE
, NULL
);
2616 mcfg
->mcfg_base
= qnum_get_uint(qobject_to(QNum
, o
));
2619 o
= object_property_get_qobject(pci_host
, PCIE_HOST_MCFG_SIZE
, NULL
);
2621 mcfg
->mcfg_size
= qnum_get_uint(qobject_to(QNum
, o
));
2627 void acpi_build(AcpiBuildTables
*tables
, MachineState
*machine
)
2629 PCMachineState
*pcms
= PC_MACHINE(machine
);
2630 PCMachineClass
*pcmc
= PC_MACHINE_GET_CLASS(pcms
);
2631 GArray
*table_offsets
;
2632 unsigned facs
, dsdt
, rsdt
, fadt
;
2636 Range pci_hole
, pci_hole64
;
2639 GArray
*tables_blob
= tables
->table_data
;
2640 AcpiSlicOem slic_oem
= { .id
= NULL
, .table_id
= NULL
};
2641 Object
*vmgenid_dev
;
2643 acpi_get_pm_info(&pm
);
2644 acpi_get_misc_info(&misc
);
2645 acpi_get_pci_holes(&pci_hole
, &pci_hole64
);
2646 acpi_get_slic_oem(&slic_oem
);
2648 table_offsets
= g_array_new(false, true /* clear */,
2650 ACPI_BUILD_DPRINTF("init ACPI tables\n");
2652 bios_linker_loader_alloc(tables
->linker
,
2653 ACPI_BUILD_TABLE_FILE
, tables_blob
,
2654 64 /* Ensure FACS is aligned */,
2655 false /* high memory */);
2658 * FACS is pointed to by FADT.
2659 * We place it first since it's the only table that has alignment
2662 facs
= tables_blob
->len
;
2663 build_facs(tables_blob
, tables
->linker
);
2665 /* DSDT is pointed to by FADT */
2666 dsdt
= tables_blob
->len
;
2667 build_dsdt(tables_blob
, tables
->linker
, &pm
, &misc
,
2668 &pci_hole
, &pci_hole64
, machine
);
2670 /* Count the size of the DSDT and SSDT, we will need it for legacy
2671 * sizing of ACPI tables.
2673 aml_len
+= tables_blob
->len
- dsdt
;
2675 /* ACPI tables pointed to by RSDT */
2676 fadt
= tables_blob
->len
;
2677 acpi_add_table(table_offsets
, tables_blob
);
2678 pm
.fadt
.facs_tbl_offset
= &facs
;
2679 pm
.fadt
.dsdt_tbl_offset
= &dsdt
;
2680 pm
.fadt
.xdsdt_tbl_offset
= &dsdt
;
2681 build_fadt(tables_blob
, tables
->linker
, &pm
.fadt
,
2682 slic_oem
.id
, slic_oem
.table_id
);
2683 aml_len
+= tables_blob
->len
- fadt
;
2685 acpi_add_table(table_offsets
, tables_blob
);
2686 build_madt(tables_blob
, tables
->linker
, pcms
);
2688 vmgenid_dev
= find_vmgenid_dev();
2690 acpi_add_table(table_offsets
, tables_blob
);
2691 vmgenid_build_acpi(VMGENID(vmgenid_dev
), tables_blob
,
2692 tables
->vmgenid
, tables
->linker
);
2695 if (misc
.has_hpet
) {
2696 acpi_add_table(table_offsets
, tables_blob
);
2697 build_hpet(tables_blob
, tables
->linker
);
2699 if (misc
.tpm_version
!= TPM_VERSION_UNSPEC
) {
2700 acpi_add_table(table_offsets
, tables_blob
);
2701 build_tpm_tcpa(tables_blob
, tables
->linker
, tables
->tcpalog
);
2703 if (misc
.tpm_version
== TPM_VERSION_2_0
) {
2704 acpi_add_table(table_offsets
, tables_blob
);
2705 build_tpm2(tables_blob
, tables
->linker
, tables
->tcpalog
);
2708 if (pcms
->numa_nodes
) {
2709 acpi_add_table(table_offsets
, tables_blob
);
2710 build_srat(tables_blob
, tables
->linker
, machine
);
2711 if (have_numa_distance
) {
2712 acpi_add_table(table_offsets
, tables_blob
);
2713 build_slit(tables_blob
, tables
->linker
);
2716 if (acpi_get_mcfg(&mcfg
)) {
2717 acpi_add_table(table_offsets
, tables_blob
);
2718 build_mcfg_q35(tables_blob
, tables
->linker
, &mcfg
);
2720 if (x86_iommu_get_default()) {
2721 IommuType IOMMUType
= x86_iommu_get_type();
2722 if (IOMMUType
== TYPE_AMD
) {
2723 acpi_add_table(table_offsets
, tables_blob
);
2724 build_amd_iommu(tables_blob
, tables
->linker
);
2725 } else if (IOMMUType
== TYPE_INTEL
) {
2726 acpi_add_table(table_offsets
, tables_blob
);
2727 build_dmar_q35(tables_blob
, tables
->linker
);
2730 if (pcms
->acpi_nvdimm_state
.is_enabled
) {
2731 nvdimm_build_acpi(table_offsets
, tables_blob
, tables
->linker
,
2732 &pcms
->acpi_nvdimm_state
, machine
->ram_slots
);
2735 /* Add tables supplied by user (if any) */
2736 for (u
= acpi_table_first(); u
; u
= acpi_table_next(u
)) {
2737 unsigned len
= acpi_table_len(u
);
2739 acpi_add_table(table_offsets
, tables_blob
);
2740 g_array_append_vals(tables_blob
, u
, len
);
2743 /* RSDT is pointed to by RSDP */
2744 rsdt
= tables_blob
->len
;
2745 build_rsdt(tables_blob
, tables
->linker
, table_offsets
,
2746 slic_oem
.id
, slic_oem
.table_id
);
2748 /* RSDP is in FSEG memory, so allocate it separately */
2749 build_rsdp(tables
->rsdp
, tables
->linker
, rsdt
);
2751 /* We'll expose it all to Guest so we want to reduce
2752 * chance of size changes.
2754 * We used to align the tables to 4k, but of course this would
2755 * too simple to be enough. 4k turned out to be too small an
2756 * alignment very soon, and in fact it is almost impossible to
2757 * keep the table size stable for all (max_cpus, max_memory_slots)
2758 * combinations. So the table size is always 64k for pc-i440fx-2.1
2759 * and we give an error if the table grows beyond that limit.
2761 * We still have the problem of migrating from "-M pc-i440fx-2.0". For
2762 * that, we exploit the fact that QEMU 2.1 generates _smaller_ tables
2763 * than 2.0 and we can always pad the smaller tables with zeros. We can
2764 * then use the exact size of the 2.0 tables.
2766 * All this is for PIIX4, since QEMU 2.0 didn't support Q35 migration.
2768 if (pcmc
->legacy_acpi_table_size
) {
2769 /* Subtracting aml_len gives the size of fixed tables. Then add the
2770 * size of the PIIX4 DSDT/SSDT in QEMU 2.0.
2772 int legacy_aml_len
=
2773 pcmc
->legacy_acpi_table_size
+
2774 ACPI_BUILD_LEGACY_CPU_AML_SIZE
* pcms
->apic_id_limit
;
2775 int legacy_table_size
=
2776 ROUND_UP(tables_blob
->len
- aml_len
+ legacy_aml_len
,
2777 ACPI_BUILD_ALIGN_SIZE
);
2778 if (tables_blob
->len
> legacy_table_size
) {
2779 /* Should happen only with PCI bridges and -M pc-i440fx-2.0. */
2780 warn_report("ACPI table size %u exceeds %d bytes,"
2781 " migration may not work",
2782 tables_blob
->len
, legacy_table_size
);
2783 error_printf("Try removing CPUs, NUMA nodes, memory slots"
2784 " or PCI bridges.");
2786 g_array_set_size(tables_blob
, legacy_table_size
);
2788 /* Make sure we have a buffer in case we need to resize the tables. */
2789 if (tables_blob
->len
> ACPI_BUILD_TABLE_SIZE
/ 2) {
2790 /* As of QEMU 2.1, this fires with 160 VCPUs and 255 memory slots. */
2791 warn_report("ACPI table size %u exceeds %d bytes,"
2792 " migration may not work",
2793 tables_blob
->len
, ACPI_BUILD_TABLE_SIZE
/ 2);
2794 error_printf("Try removing CPUs, NUMA nodes, memory slots"
2795 " or PCI bridges.");
2797 acpi_align_size(tables_blob
, ACPI_BUILD_TABLE_SIZE
);
2800 acpi_align_size(tables
->linker
->cmd_blob
, ACPI_BUILD_ALIGN_SIZE
);
2802 /* Cleanup memory that's no longer used. */
2803 g_array_free(table_offsets
, true);
2806 static void acpi_ram_update(MemoryRegion
*mr
, GArray
*data
)
2808 uint32_t size
= acpi_data_len(data
);
2810 /* Make sure RAM size is correct - in case it got changed e.g. by migration */
2811 memory_region_ram_resize(mr
, size
, &error_abort
);
2813 memcpy(memory_region_get_ram_ptr(mr
), data
->data
, size
);
2814 memory_region_set_dirty(mr
, 0, size
);
2817 static void acpi_build_update(void *build_opaque
)
2819 AcpiBuildState
*build_state
= build_opaque
;
2820 AcpiBuildTables tables
;
2822 /* No state to update or already patched? Nothing to do. */
2823 if (!build_state
|| build_state
->patched
) {
2826 build_state
->patched
= 1;
2828 acpi_build_tables_init(&tables
);
2830 acpi_build(&tables
, MACHINE(qdev_get_machine()));
2832 acpi_ram_update(build_state
->table_mr
, tables
.table_data
);
2834 if (build_state
->rsdp
) {
2835 memcpy(build_state
->rsdp
, tables
.rsdp
->data
, acpi_data_len(tables
.rsdp
));
2837 acpi_ram_update(build_state
->rsdp_mr
, tables
.rsdp
);
2840 acpi_ram_update(build_state
->linker_mr
, tables
.linker
->cmd_blob
);
2841 acpi_build_tables_cleanup(&tables
, true);
2844 static void acpi_build_reset(void *build_opaque
)
2846 AcpiBuildState
*build_state
= build_opaque
;
2847 build_state
->patched
= 0;
2850 static MemoryRegion
*acpi_add_rom_blob(AcpiBuildState
*build_state
,
2851 GArray
*blob
, const char *name
,
2854 return rom_add_blob(name
, blob
->data
, acpi_data_len(blob
), max_size
, -1,
2855 name
, acpi_build_update
, build_state
, NULL
, true);
2858 static const VMStateDescription vmstate_acpi_build
= {
2859 .name
= "acpi_build",
2861 .minimum_version_id
= 1,
2862 .fields
= (VMStateField
[]) {
2863 VMSTATE_UINT8(patched
, AcpiBuildState
),
2864 VMSTATE_END_OF_LIST()
2868 void acpi_setup(void)
2870 PCMachineState
*pcms
= PC_MACHINE(qdev_get_machine());
2871 PCMachineClass
*pcmc
= PC_MACHINE_GET_CLASS(pcms
);
2872 AcpiBuildTables tables
;
2873 AcpiBuildState
*build_state
;
2874 Object
*vmgenid_dev
;
2876 if (!pcms
->fw_cfg
) {
2877 ACPI_BUILD_DPRINTF("No fw cfg. Bailing out.\n");
2881 if (!pcms
->acpi_build_enabled
) {
2882 ACPI_BUILD_DPRINTF("ACPI build disabled. Bailing out.\n");
2886 if (!acpi_enabled
) {
2887 ACPI_BUILD_DPRINTF("ACPI disabled. Bailing out.\n");
2891 build_state
= g_malloc0(sizeof *build_state
);
2893 acpi_build_tables_init(&tables
);
2894 acpi_build(&tables
, MACHINE(pcms
));
2896 /* Now expose it all to Guest */
2897 build_state
->table_mr
= acpi_add_rom_blob(build_state
, tables
.table_data
,
2898 ACPI_BUILD_TABLE_FILE
,
2899 ACPI_BUILD_TABLE_MAX_SIZE
);
2900 assert(build_state
->table_mr
!= NULL
);
2902 build_state
->linker_mr
=
2903 acpi_add_rom_blob(build_state
, tables
.linker
->cmd_blob
,
2904 "etc/table-loader", 0);
2906 fw_cfg_add_file(pcms
->fw_cfg
, ACPI_BUILD_TPMLOG_FILE
,
2907 tables
.tcpalog
->data
, acpi_data_len(tables
.tcpalog
));
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);