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 "acpi-build.h"
26 #include "qemu-common.h"
27 #include "qemu/bitmap.h"
28 #include "qemu/error-report.h"
29 #include "hw/pci/pci.h"
31 #include "hw/i386/pc.h"
32 #include "target/i386/cpu.h"
33 #include "hw/timer/hpet.h"
34 #include "hw/acpi/acpi-defs.h"
35 #include "hw/acpi/acpi.h"
36 #include "hw/acpi/cpu.h"
37 #include "hw/nvram/fw_cfg.h"
38 #include "hw/acpi/bios-linker-loader.h"
39 #include "hw/loader.h"
40 #include "hw/isa/isa.h"
41 #include "hw/block/fdc.h"
42 #include "hw/acpi/memory_hotplug.h"
43 #include "sysemu/tpm.h"
44 #include "hw/acpi/tpm.h"
45 #include "hw/acpi/vmgenid.h"
46 #include "sysemu/tpm_backend.h"
47 #include "hw/timer/mc146818rtc_regs.h"
48 #include "sysemu/numa.h"
50 /* Supported chipsets: */
51 #include "hw/acpi/piix4.h"
52 #include "hw/acpi/pcihp.h"
53 #include "hw/i386/ich9.h"
54 #include "hw/pci/pci_bus.h"
55 #include "hw/pci-host/q35.h"
56 #include "hw/i386/x86-iommu.h"
58 #include "hw/acpi/aml-build.h"
60 #include "qom/qom-qobject.h"
61 #include "hw/i386/amd_iommu.h"
62 #include "hw/i386/intel_iommu.h"
64 #include "hw/acpi/ipmi.h"
66 /* These are used to size the ACPI tables for -M pc-i440fx-1.7 and
67 * -M pc-i440fx-2.0. Even if the actual amount of AML generated grows
68 * a little bit, there should be plenty of free space since the DSDT
69 * shrunk by ~1.5k between QEMU 2.0 and QEMU 2.1.
71 #define ACPI_BUILD_LEGACY_CPU_AML_SIZE 97
72 #define ACPI_BUILD_ALIGN_SIZE 0x1000
74 #define ACPI_BUILD_TABLE_SIZE 0x20000
76 /* #define DEBUG_ACPI_BUILD */
77 #ifdef DEBUG_ACPI_BUILD
78 #define ACPI_BUILD_DPRINTF(fmt, ...) \
79 do {printf("ACPI_BUILD: " fmt, ## __VA_ARGS__); } while (0)
81 #define ACPI_BUILD_DPRINTF(fmt, ...)
84 /* Default IOAPIC ID */
85 #define ACPI_BUILD_IOAPIC_ID 0x0
87 typedef struct AcpiMcfgInfo
{
92 typedef struct AcpiPmInfo
{
98 uint8_t acpi_enable_cmd
;
99 uint8_t acpi_disable_cmd
;
101 uint32_t gpe0_blk_len
;
103 uint16_t cpu_hp_io_base
;
104 uint16_t pcihp_io_base
;
105 uint16_t pcihp_io_len
;
108 typedef struct AcpiMiscInfo
{
111 TPMVersion tpm_version
;
112 const unsigned char *dsdt_code
;
114 uint16_t pvpanic_port
;
115 uint16_t applesmc_io_base
;
118 typedef struct AcpiBuildPciBusHotplugState
{
119 GArray
*device_table
;
120 GArray
*notify_table
;
121 struct AcpiBuildPciBusHotplugState
*parent
;
122 bool pcihp_bridge_en
;
123 } AcpiBuildPciBusHotplugState
;
125 static void acpi_get_pm_info(AcpiPmInfo
*pm
)
127 Object
*piix
= piix4_pm_find();
128 Object
*lpc
= ich9_lpc_find();
132 pm
->cpu_hp_io_base
= 0;
133 pm
->pcihp_io_base
= 0;
134 pm
->pcihp_io_len
= 0;
137 pm
->cpu_hp_io_base
= PIIX4_CPU_HOTPLUG_IO_BASE
;
139 object_property_get_uint(obj
, ACPI_PCIHP_IO_BASE_PROP
, NULL
);
141 object_property_get_uint(obj
, ACPI_PCIHP_IO_LEN_PROP
, NULL
);
145 pm
->cpu_hp_io_base
= ICH9_CPU_HOTPLUG_IO_BASE
;
149 /* Fill in optional s3/s4 related properties */
150 o
= object_property_get_qobject(obj
, ACPI_PM_PROP_S3_DISABLED
, NULL
);
152 pm
->s3_disabled
= qnum_get_uint(qobject_to_qnum(o
));
154 pm
->s3_disabled
= false;
157 o
= object_property_get_qobject(obj
, ACPI_PM_PROP_S4_DISABLED
, NULL
);
159 pm
->s4_disabled
= qnum_get_uint(qobject_to_qnum(o
));
161 pm
->s4_disabled
= false;
164 o
= object_property_get_qobject(obj
, ACPI_PM_PROP_S4_VAL
, NULL
);
166 pm
->s4_val
= qnum_get_uint(qobject_to_qnum(o
));
172 /* Fill in mandatory properties */
173 pm
->sci_int
= object_property_get_uint(obj
, ACPI_PM_PROP_SCI_INT
, NULL
);
175 pm
->acpi_enable_cmd
= object_property_get_uint(obj
,
176 ACPI_PM_PROP_ACPI_ENABLE_CMD
,
178 pm
->acpi_disable_cmd
=
179 object_property_get_uint(obj
,
180 ACPI_PM_PROP_ACPI_DISABLE_CMD
,
182 pm
->io_base
= object_property_get_uint(obj
, ACPI_PM_PROP_PM_IO_BASE
,
184 pm
->gpe0_blk
= object_property_get_uint(obj
, ACPI_PM_PROP_GPE0_BLK
,
186 pm
->gpe0_blk_len
= object_property_get_uint(obj
, ACPI_PM_PROP_GPE0_BLK_LEN
,
188 pm
->pcihp_bridge_en
=
189 object_property_get_bool(obj
, "acpi-pci-hotplug-with-bridge-support",
193 static void acpi_get_misc_info(AcpiMiscInfo
*info
)
195 Object
*piix
= piix4_pm_find();
196 Object
*lpc
= ich9_lpc_find();
197 assert(!!piix
!= !!lpc
);
200 info
->is_piix4
= true;
203 info
->is_piix4
= false;
206 info
->has_hpet
= hpet_find();
207 info
->tpm_version
= tpm_get_version();
208 info
->pvpanic_port
= pvpanic_port();
209 info
->applesmc_io_base
= applesmc_port();
213 * Because of the PXB hosts we cannot simply query TYPE_PCI_HOST_BRIDGE.
214 * On i386 arch we only have two pci hosts, so we can look only for them.
216 static Object
*acpi_get_i386_pci_host(void)
220 host
= OBJECT_CHECK(PCIHostState
,
221 object_resolve_path("/machine/i440fx", NULL
),
222 TYPE_PCI_HOST_BRIDGE
);
224 host
= OBJECT_CHECK(PCIHostState
,
225 object_resolve_path("/machine/q35", NULL
),
226 TYPE_PCI_HOST_BRIDGE
);
232 static void acpi_get_pci_holes(Range
*hole
, Range
*hole64
)
236 pci_host
= acpi_get_i386_pci_host();
239 range_set_bounds1(hole
,
240 object_property_get_uint(pci_host
,
241 PCI_HOST_PROP_PCI_HOLE_START
,
243 object_property_get_uint(pci_host
,
244 PCI_HOST_PROP_PCI_HOLE_END
,
246 range_set_bounds1(hole64
,
247 object_property_get_uint(pci_host
,
248 PCI_HOST_PROP_PCI_HOLE64_START
,
250 object_property_get_uint(pci_host
,
251 PCI_HOST_PROP_PCI_HOLE64_END
,
255 #define ACPI_PORT_SMI_CMD 0x00b2 /* TODO: this is APM_CNT_IOPORT */
257 static void acpi_align_size(GArray
*blob
, unsigned align
)
259 /* Align size to multiple of given size. This reduces the chance
260 * we need to change size in the future (breaking cross version migration).
262 g_array_set_size(blob
, ROUND_UP(acpi_data_len(blob
), align
));
267 build_facs(GArray
*table_data
, BIOSLinker
*linker
)
269 AcpiFacsDescriptorRev1
*facs
= acpi_data_push(table_data
, sizeof *facs
);
270 memcpy(&facs
->signature
, "FACS", 4);
271 facs
->length
= cpu_to_le32(sizeof(*facs
));
274 /* Load chipset information in FADT */
275 static void fadt_setup(AcpiFadtDescriptorRev3
*fadt
, AcpiPmInfo
*pm
)
279 fadt
->sci_int
= cpu_to_le16(pm
->sci_int
);
280 fadt
->smi_cmd
= cpu_to_le32(ACPI_PORT_SMI_CMD
);
281 fadt
->acpi_enable
= pm
->acpi_enable_cmd
;
282 fadt
->acpi_disable
= pm
->acpi_disable_cmd
;
283 /* EVT, CNT, TMR offset matches hw/acpi/core.c */
284 fadt
->pm1a_evt_blk
= cpu_to_le32(pm
->io_base
);
285 fadt
->pm1a_cnt_blk
= cpu_to_le32(pm
->io_base
+ 0x04);
286 fadt
->pm_tmr_blk
= cpu_to_le32(pm
->io_base
+ 0x08);
287 fadt
->gpe0_blk
= cpu_to_le32(pm
->gpe0_blk
);
288 /* EVT, CNT, TMR length matches hw/acpi/core.c */
289 fadt
->pm1_evt_len
= 4;
290 fadt
->pm1_cnt_len
= 2;
291 fadt
->pm_tmr_len
= 4;
292 fadt
->gpe0_blk_len
= pm
->gpe0_blk_len
;
293 fadt
->plvl2_lat
= cpu_to_le16(0xfff); /* C2 state not supported */
294 fadt
->plvl3_lat
= cpu_to_le16(0xfff); /* C3 state not supported */
295 fadt
->flags
= cpu_to_le32((1 << ACPI_FADT_F_WBINVD
) |
296 (1 << ACPI_FADT_F_PROC_C1
) |
297 (1 << ACPI_FADT_F_SLP_BUTTON
) |
298 (1 << ACPI_FADT_F_RTC_S4
));
299 fadt
->flags
|= cpu_to_le32(1 << ACPI_FADT_F_USE_PLATFORM_CLOCK
);
300 /* APIC destination mode ("Flat Logical") has an upper limit of 8 CPUs
301 * For more than 8 CPUs, "Clustered Logical" mode has to be used
304 fadt
->flags
|= cpu_to_le32(1 << ACPI_FADT_F_FORCE_APIC_CLUSTER_MODEL
);
306 fadt
->century
= RTC_CENTURY
;
308 fadt
->flags
|= cpu_to_le32(1 << ACPI_FADT_F_RESET_REG_SUP
);
309 fadt
->reset_value
= 0xf;
310 fadt
->reset_register
.space_id
= AML_SYSTEM_IO
;
311 fadt
->reset_register
.bit_width
= 8;
312 fadt
->reset_register
.address
= cpu_to_le64(ICH9_RST_CNT_IOPORT
);
313 /* The above need not be conditional on machine type because the reset port
314 * happens to be the same on PIIX (pc) and ICH9 (q35). */
315 QEMU_BUILD_BUG_ON(ICH9_RST_CNT_IOPORT
!= RCR_IOPORT
);
317 fadt
->xpm1a_event_block
.space_id
= AML_SYSTEM_IO
;
318 fadt
->xpm1a_event_block
.bit_width
= fadt
->pm1_evt_len
* 8;
319 fadt
->xpm1a_event_block
.address
= cpu_to_le64(pm
->io_base
);
321 fadt
->xpm1a_control_block
.space_id
= AML_SYSTEM_IO
;
322 fadt
->xpm1a_control_block
.bit_width
= fadt
->pm1_cnt_len
* 8;
323 fadt
->xpm1a_control_block
.address
= cpu_to_le64(pm
->io_base
+ 0x4);
325 fadt
->xpm_timer_block
.space_id
= AML_SYSTEM_IO
;
326 fadt
->xpm_timer_block
.bit_width
= fadt
->pm_tmr_len
* 8;
327 fadt
->xpm_timer_block
.address
= cpu_to_le64(pm
->io_base
+ 0x8);
329 fadt
->xgpe0_block
.space_id
= AML_SYSTEM_IO
;
330 fadt
->xgpe0_block
.bit_width
= pm
->gpe0_blk_len
* 8;
331 fadt
->xgpe0_block
.address
= cpu_to_le64(pm
->gpe0_blk
);
337 build_fadt(GArray
*table_data
, BIOSLinker
*linker
, AcpiPmInfo
*pm
,
338 unsigned facs_tbl_offset
, unsigned dsdt_tbl_offset
,
339 const char *oem_id
, const char *oem_table_id
)
341 AcpiFadtDescriptorRev3
*fadt
= acpi_data_push(table_data
, sizeof(*fadt
));
342 unsigned fw_ctrl_offset
= (char *)&fadt
->firmware_ctrl
- table_data
->data
;
343 unsigned dsdt_entry_offset
= (char *)&fadt
->dsdt
- table_data
->data
;
344 unsigned xdsdt_entry_offset
= (char *)&fadt
->x_dsdt
- table_data
->data
;
346 /* FACS address to be filled by Guest linker */
347 bios_linker_loader_add_pointer(linker
,
348 ACPI_BUILD_TABLE_FILE
, fw_ctrl_offset
, sizeof(fadt
->firmware_ctrl
),
349 ACPI_BUILD_TABLE_FILE
, facs_tbl_offset
);
351 /* DSDT address to be filled by Guest linker */
352 fadt_setup(fadt
, pm
);
353 bios_linker_loader_add_pointer(linker
,
354 ACPI_BUILD_TABLE_FILE
, dsdt_entry_offset
, sizeof(fadt
->dsdt
),
355 ACPI_BUILD_TABLE_FILE
, dsdt_tbl_offset
);
356 bios_linker_loader_add_pointer(linker
,
357 ACPI_BUILD_TABLE_FILE
, xdsdt_entry_offset
, sizeof(fadt
->x_dsdt
),
358 ACPI_BUILD_TABLE_FILE
, dsdt_tbl_offset
);
360 build_header(linker
, table_data
,
361 (void *)fadt
, "FACP", sizeof(*fadt
), 3, oem_id
, oem_table_id
);
364 void pc_madt_cpu_entry(AcpiDeviceIf
*adev
, int uid
,
365 const CPUArchIdList
*apic_ids
, GArray
*entry
)
367 uint32_t apic_id
= apic_ids
->cpus
[uid
].arch_id
;
369 /* ACPI spec says that LAPIC entry for non present
370 * CPU may be omitted from MADT or it must be marked
371 * as disabled. However omitting non present CPU from
372 * MADT breaks hotplug on linux. So possible CPUs
373 * should be put in MADT but kept disabled.
376 AcpiMadtProcessorApic
*apic
= acpi_data_push(entry
, sizeof *apic
);
378 apic
->type
= ACPI_APIC_PROCESSOR
;
379 apic
->length
= sizeof(*apic
);
380 apic
->processor_id
= uid
;
381 apic
->local_apic_id
= apic_id
;
382 if (apic_ids
->cpus
[uid
].cpu
!= NULL
) {
383 apic
->flags
= cpu_to_le32(1);
385 apic
->flags
= cpu_to_le32(0);
388 AcpiMadtProcessorX2Apic
*apic
= acpi_data_push(entry
, sizeof *apic
);
390 apic
->type
= ACPI_APIC_LOCAL_X2APIC
;
391 apic
->length
= sizeof(*apic
);
392 apic
->uid
= cpu_to_le32(uid
);
393 apic
->x2apic_id
= cpu_to_le32(apic_id
);
394 if (apic_ids
->cpus
[uid
].cpu
!= NULL
) {
395 apic
->flags
= cpu_to_le32(1);
397 apic
->flags
= cpu_to_le32(0);
403 build_madt(GArray
*table_data
, BIOSLinker
*linker
, PCMachineState
*pcms
)
405 MachineClass
*mc
= MACHINE_GET_CLASS(pcms
);
406 const CPUArchIdList
*apic_ids
= mc
->possible_cpu_arch_ids(MACHINE(pcms
));
407 int madt_start
= table_data
->len
;
408 AcpiDeviceIfClass
*adevc
= ACPI_DEVICE_IF_GET_CLASS(pcms
->acpi_dev
);
409 AcpiDeviceIf
*adev
= ACPI_DEVICE_IF(pcms
->acpi_dev
);
410 bool x2apic_mode
= false;
412 AcpiMultipleApicTable
*madt
;
413 AcpiMadtIoApic
*io_apic
;
414 AcpiMadtIntsrcovr
*intsrcovr
;
417 madt
= acpi_data_push(table_data
, sizeof *madt
);
418 madt
->local_apic_address
= cpu_to_le32(APIC_DEFAULT_ADDRESS
);
419 madt
->flags
= cpu_to_le32(1);
421 for (i
= 0; i
< apic_ids
->len
; i
++) {
422 adevc
->madt_cpu(adev
, i
, apic_ids
, table_data
);
423 if (apic_ids
->cpus
[i
].arch_id
> 254) {
428 io_apic
= acpi_data_push(table_data
, sizeof *io_apic
);
429 io_apic
->type
= ACPI_APIC_IO
;
430 io_apic
->length
= sizeof(*io_apic
);
431 io_apic
->io_apic_id
= ACPI_BUILD_IOAPIC_ID
;
432 io_apic
->address
= cpu_to_le32(IO_APIC_DEFAULT_ADDRESS
);
433 io_apic
->interrupt
= cpu_to_le32(0);
435 if (pcms
->apic_xrupt_override
) {
436 intsrcovr
= acpi_data_push(table_data
, sizeof *intsrcovr
);
437 intsrcovr
->type
= ACPI_APIC_XRUPT_OVERRIDE
;
438 intsrcovr
->length
= sizeof(*intsrcovr
);
439 intsrcovr
->source
= 0;
440 intsrcovr
->gsi
= cpu_to_le32(2);
441 intsrcovr
->flags
= cpu_to_le16(0); /* conforms to bus specifications */
443 for (i
= 1; i
< 16; i
++) {
444 #define ACPI_BUILD_PCI_IRQS ((1<<5) | (1<<9) | (1<<10) | (1<<11))
445 if (!(ACPI_BUILD_PCI_IRQS
& (1 << i
))) {
446 /* No need for a INT source override structure. */
449 intsrcovr
= acpi_data_push(table_data
, sizeof *intsrcovr
);
450 intsrcovr
->type
= ACPI_APIC_XRUPT_OVERRIDE
;
451 intsrcovr
->length
= sizeof(*intsrcovr
);
452 intsrcovr
->source
= i
;
453 intsrcovr
->gsi
= cpu_to_le32(i
);
454 intsrcovr
->flags
= cpu_to_le16(0xd); /* active high, level triggered */
458 AcpiMadtLocalX2ApicNmi
*local_nmi
;
460 local_nmi
= acpi_data_push(table_data
, sizeof *local_nmi
);
461 local_nmi
->type
= ACPI_APIC_LOCAL_X2APIC_NMI
;
462 local_nmi
->length
= sizeof(*local_nmi
);
463 local_nmi
->uid
= 0xFFFFFFFF; /* all processors */
464 local_nmi
->flags
= cpu_to_le16(0);
465 local_nmi
->lint
= 1; /* ACPI_LINT1 */
467 AcpiMadtLocalNmi
*local_nmi
;
469 local_nmi
= acpi_data_push(table_data
, sizeof *local_nmi
);
470 local_nmi
->type
= ACPI_APIC_LOCAL_NMI
;
471 local_nmi
->length
= sizeof(*local_nmi
);
472 local_nmi
->processor_id
= 0xff; /* all processors */
473 local_nmi
->flags
= cpu_to_le16(0);
474 local_nmi
->lint
= 1; /* ACPI_LINT1 */
477 build_header(linker
, table_data
,
478 (void *)(table_data
->data
+ madt_start
), "APIC",
479 table_data
->len
- madt_start
, 1, NULL
, NULL
);
482 /* Assign BSEL property to all buses. In the future, this can be changed
483 * to only assign to buses that support hotplug.
485 static void *acpi_set_bsel(PCIBus
*bus
, void *opaque
)
487 unsigned *bsel_alloc
= opaque
;
490 if (qbus_is_hotpluggable(BUS(bus
))) {
491 bus_bsel
= g_malloc(sizeof *bus_bsel
);
493 *bus_bsel
= (*bsel_alloc
)++;
494 object_property_add_uint32_ptr(OBJECT(bus
), ACPI_PCIHP_PROP_BSEL
,
495 bus_bsel
, &error_abort
);
501 static void acpi_set_pci_info(void)
503 PCIBus
*bus
= find_i440fx(); /* TODO: Q35 support */
504 unsigned bsel_alloc
= ACPI_PCIHP_BSEL_DEFAULT
;
507 /* Scan all PCI buses. Set property to enable acpi based hotplug. */
508 pci_for_each_bus_depth_first(bus
, acpi_set_bsel
, NULL
, &bsel_alloc
);
512 static void build_append_pcihp_notify_entry(Aml
*method
, int slot
)
515 int32_t devfn
= PCI_DEVFN(slot
, 0);
517 if_ctx
= aml_if(aml_and(aml_arg(0), aml_int(0x1U
<< slot
), NULL
));
518 aml_append(if_ctx
, aml_notify(aml_name("S%.02X", devfn
), aml_arg(1)));
519 aml_append(method
, if_ctx
);
522 static void build_append_pci_bus_devices(Aml
*parent_scope
, PCIBus
*bus
,
523 bool pcihp_bridge_en
)
525 Aml
*dev
, *notify_method
, *method
;
530 bsel
= object_property_get_qobject(OBJECT(bus
), ACPI_PCIHP_PROP_BSEL
, NULL
);
532 uint64_t bsel_val
= qnum_get_uint(qobject_to_qnum(bsel
));
534 aml_append(parent_scope
, aml_name_decl("BSEL", aml_int(bsel_val
)));
535 notify_method
= aml_method("DVNT", 2, AML_NOTSERIALIZED
);
538 for (i
= 0; i
< ARRAY_SIZE(bus
->devices
); i
+= PCI_FUNC_MAX
) {
541 PCIDevice
*pdev
= bus
->devices
[i
];
542 int slot
= PCI_SLOT(i
);
543 bool hotplug_enabled_dev
;
547 if (bsel
) { /* add hotplug slots for non present devices */
548 dev
= aml_device("S%.02X", PCI_DEVFN(slot
, 0));
549 aml_append(dev
, aml_name_decl("_SUN", aml_int(slot
)));
550 aml_append(dev
, aml_name_decl("_ADR", aml_int(slot
<< 16)));
551 method
= aml_method("_EJ0", 1, AML_NOTSERIALIZED
);
553 aml_call2("PCEJ", aml_name("BSEL"), aml_name("_SUN"))
555 aml_append(dev
, method
);
556 aml_append(parent_scope
, dev
);
558 build_append_pcihp_notify_entry(notify_method
, slot
);
563 pc
= PCI_DEVICE_GET_CLASS(pdev
);
564 dc
= DEVICE_GET_CLASS(pdev
);
566 /* When hotplug for bridges is enabled, bridges are
567 * described in ACPI separately (see build_pci_bus_end).
568 * In this case they aren't themselves hot-pluggable.
569 * Hotplugged bridges *are* hot-pluggable.
571 bridge_in_acpi
= pc
->is_bridge
&& pcihp_bridge_en
&&
572 !DEVICE(pdev
)->hotplugged
;
574 hotplug_enabled_dev
= bsel
&& dc
->hotpluggable
&& !bridge_in_acpi
;
576 if (pc
->class_id
== PCI_CLASS_BRIDGE_ISA
) {
580 /* start to compose PCI slot descriptor */
581 dev
= aml_device("S%.02X", PCI_DEVFN(slot
, 0));
582 aml_append(dev
, aml_name_decl("_ADR", aml_int(slot
<< 16)));
584 if (pc
->class_id
== PCI_CLASS_DISPLAY_VGA
) {
585 /* add VGA specific AML methods */
588 if (object_dynamic_cast(OBJECT(pdev
), "qxl-vga")) {
594 method
= aml_method("_S1D", 0, AML_NOTSERIALIZED
);
595 aml_append(method
, aml_return(aml_int(0)));
596 aml_append(dev
, method
);
598 method
= aml_method("_S2D", 0, AML_NOTSERIALIZED
);
599 aml_append(method
, aml_return(aml_int(0)));
600 aml_append(dev
, method
);
602 method
= aml_method("_S3D", 0, AML_NOTSERIALIZED
);
603 aml_append(method
, aml_return(aml_int(s3d
)));
604 aml_append(dev
, method
);
605 } else if (hotplug_enabled_dev
) {
606 /* add _SUN/_EJ0 to make slot hotpluggable */
607 aml_append(dev
, aml_name_decl("_SUN", aml_int(slot
)));
609 method
= aml_method("_EJ0", 1, AML_NOTSERIALIZED
);
611 aml_call2("PCEJ", aml_name("BSEL"), aml_name("_SUN"))
613 aml_append(dev
, method
);
616 build_append_pcihp_notify_entry(notify_method
, slot
);
618 } else if (bridge_in_acpi
) {
620 * device is coldplugged bridge,
621 * add child device descriptions into its scope
623 PCIBus
*sec_bus
= pci_bridge_get_sec_bus(PCI_BRIDGE(pdev
));
625 build_append_pci_bus_devices(dev
, sec_bus
, pcihp_bridge_en
);
627 /* slot descriptor has been composed, add it into parent context */
628 aml_append(parent_scope
, dev
);
632 aml_append(parent_scope
, notify_method
);
635 /* Append PCNT method to notify about events on local and child buses.
636 * Add unconditionally for root since DSDT expects it.
638 method
= aml_method("PCNT", 0, AML_NOTSERIALIZED
);
640 /* If bus supports hotplug select it and notify about local events */
642 uint64_t bsel_val
= qnum_get_uint(qobject_to_qnum(bsel
));
644 aml_append(method
, aml_store(aml_int(bsel_val
), aml_name("BNUM")));
646 aml_call2("DVNT", aml_name("PCIU"), aml_int(1) /* Device Check */)
649 aml_call2("DVNT", aml_name("PCID"), aml_int(3)/* Eject Request */)
653 /* Notify about child bus events in any case */
654 if (pcihp_bridge_en
) {
655 QLIST_FOREACH(sec
, &bus
->child
, sibling
) {
656 int32_t devfn
= sec
->parent_dev
->devfn
;
658 if (pci_bus_is_root(sec
) || pci_bus_is_express(sec
)) {
662 aml_append(method
, aml_name("^S%.02X.PCNT", devfn
));
665 aml_append(parent_scope
, method
);
666 qobject_decref(bsel
);
671 * @link_name: link name for PCI route entry
673 * build AML package containing a PCI route entry for @link_name
675 static Aml
*build_prt_entry(const char *link_name
)
677 Aml
*a_zero
= aml_int(0);
678 Aml
*pkg
= aml_package(4);
679 aml_append(pkg
, a_zero
);
680 aml_append(pkg
, a_zero
);
681 aml_append(pkg
, aml_name("%s", link_name
));
682 aml_append(pkg
, a_zero
);
687 * initialize_route - Initialize the interrupt routing rule
688 * through a specific LINK:
689 * if (lnk_idx == idx)
690 * route using link 'link_name'
692 static Aml
*initialize_route(Aml
*route
, const char *link_name
,
693 Aml
*lnk_idx
, int idx
)
695 Aml
*if_ctx
= aml_if(aml_equal(lnk_idx
, aml_int(idx
)));
696 Aml
*pkg
= build_prt_entry(link_name
);
698 aml_append(if_ctx
, aml_store(pkg
, route
));
704 * build_prt - Define interrupt rounting rules
706 * Returns an array of 128 routes, one for each device,
707 * based on device location.
708 * The main goal is to equaly distribute the interrupts
709 * over the 4 existing ACPI links (works only for i440fx).
710 * The hash function is (slot + pin) & 3 -> "LNK[D|A|B|C]".
713 static Aml
*build_prt(bool is_pci0_prt
)
715 Aml
*method
, *while_ctx
, *pin
, *res
;
717 method
= aml_method("_PRT", 0, AML_NOTSERIALIZED
);
720 aml_append(method
, aml_store(aml_package(128), res
));
721 aml_append(method
, aml_store(aml_int(0), pin
));
723 /* while (pin < 128) */
724 while_ctx
= aml_while(aml_lless(pin
, aml_int(128)));
726 Aml
*slot
= aml_local(2);
727 Aml
*lnk_idx
= aml_local(3);
728 Aml
*route
= aml_local(4);
730 /* slot = pin >> 2 */
731 aml_append(while_ctx
,
732 aml_store(aml_shiftright(pin
, aml_int(2), NULL
), slot
));
733 /* lnk_idx = (slot + pin) & 3 */
734 aml_append(while_ctx
,
735 aml_store(aml_and(aml_add(pin
, slot
, NULL
), aml_int(3), NULL
),
738 /* route[2] = "LNK[D|A|B|C]", selection based on pin % 3 */
739 aml_append(while_ctx
, initialize_route(route
, "LNKD", lnk_idx
, 0));
741 Aml
*if_device_1
, *if_pin_4
, *else_pin_4
;
743 /* device 1 is the power-management device, needs SCI */
744 if_device_1
= aml_if(aml_equal(lnk_idx
, aml_int(1)));
746 if_pin_4
= aml_if(aml_equal(pin
, aml_int(4)));
749 aml_store(build_prt_entry("LNKS"), route
));
751 aml_append(if_device_1
, if_pin_4
);
752 else_pin_4
= aml_else();
754 aml_append(else_pin_4
,
755 aml_store(build_prt_entry("LNKA"), route
));
757 aml_append(if_device_1
, else_pin_4
);
759 aml_append(while_ctx
, if_device_1
);
761 aml_append(while_ctx
, initialize_route(route
, "LNKA", lnk_idx
, 1));
763 aml_append(while_ctx
, initialize_route(route
, "LNKB", lnk_idx
, 2));
764 aml_append(while_ctx
, initialize_route(route
, "LNKC", lnk_idx
, 3));
766 /* route[0] = 0x[slot]FFFF */
767 aml_append(while_ctx
,
768 aml_store(aml_or(aml_shiftleft(slot
, aml_int(16)), aml_int(0xFFFF),
770 aml_index(route
, aml_int(0))));
771 /* route[1] = pin & 3 */
772 aml_append(while_ctx
,
773 aml_store(aml_and(pin
, aml_int(3), NULL
),
774 aml_index(route
, aml_int(1))));
775 /* res[pin] = route */
776 aml_append(while_ctx
, aml_store(route
, aml_index(res
, pin
)));
778 aml_append(while_ctx
, aml_increment(pin
));
780 aml_append(method
, while_ctx
);
782 aml_append(method
, aml_return(res
));
787 typedef struct CrsRangeEntry
{
792 static void crs_range_insert(GPtrArray
*ranges
, uint64_t base
, uint64_t limit
)
794 CrsRangeEntry
*entry
;
796 entry
= g_malloc(sizeof(*entry
));
798 entry
->limit
= limit
;
800 g_ptr_array_add(ranges
, entry
);
803 static void crs_range_free(gpointer data
)
805 CrsRangeEntry
*entry
= (CrsRangeEntry
*)data
;
809 typedef struct CrsRangeSet
{
810 GPtrArray
*io_ranges
;
811 GPtrArray
*mem_ranges
;
812 GPtrArray
*mem_64bit_ranges
;
815 static void crs_range_set_init(CrsRangeSet
*range_set
)
817 range_set
->io_ranges
= g_ptr_array_new_with_free_func(crs_range_free
);
818 range_set
->mem_ranges
= g_ptr_array_new_with_free_func(crs_range_free
);
819 range_set
->mem_64bit_ranges
=
820 g_ptr_array_new_with_free_func(crs_range_free
);
823 static void crs_range_set_free(CrsRangeSet
*range_set
)
825 g_ptr_array_free(range_set
->io_ranges
, true);
826 g_ptr_array_free(range_set
->mem_ranges
, true);
827 g_ptr_array_free(range_set
->mem_64bit_ranges
, true);
830 static gint
crs_range_compare(gconstpointer a
, gconstpointer b
)
832 CrsRangeEntry
*entry_a
= *(CrsRangeEntry
**)a
;
833 CrsRangeEntry
*entry_b
= *(CrsRangeEntry
**)b
;
835 return (int64_t)entry_a
->base
- (int64_t)entry_b
->base
;
839 * crs_replace_with_free_ranges - given the 'used' ranges within [start - end]
840 * interval, computes the 'free' ranges from the same interval.
841 * Example: If the input array is { [a1 - a2],[b1 - b2] }, the function
842 * will return { [base - a1], [a2 - b1], [b2 - limit] }.
844 static void crs_replace_with_free_ranges(GPtrArray
*ranges
,
845 uint64_t start
, uint64_t end
)
847 GPtrArray
*free_ranges
= g_ptr_array_new();
848 uint64_t free_base
= start
;
851 g_ptr_array_sort(ranges
, crs_range_compare
);
852 for (i
= 0; i
< ranges
->len
; i
++) {
853 CrsRangeEntry
*used
= g_ptr_array_index(ranges
, i
);
855 if (free_base
< used
->base
) {
856 crs_range_insert(free_ranges
, free_base
, used
->base
- 1);
859 free_base
= used
->limit
+ 1;
862 if (free_base
< end
) {
863 crs_range_insert(free_ranges
, free_base
, end
);
866 g_ptr_array_set_size(ranges
, 0);
867 for (i
= 0; i
< free_ranges
->len
; i
++) {
868 g_ptr_array_add(ranges
, g_ptr_array_index(free_ranges
, i
));
871 g_ptr_array_free(free_ranges
, true);
875 * crs_range_merge - merges adjacent ranges in the given array.
876 * Array elements are deleted and replaced with the merged ranges.
878 static void crs_range_merge(GPtrArray
*range
)
880 GPtrArray
*tmp
= g_ptr_array_new_with_free_func(crs_range_free
);
881 CrsRangeEntry
*entry
;
882 uint64_t range_base
, range_limit
;
889 g_ptr_array_sort(range
, crs_range_compare
);
891 entry
= g_ptr_array_index(range
, 0);
892 range_base
= entry
->base
;
893 range_limit
= entry
->limit
;
894 for (i
= 1; i
< range
->len
; i
++) {
895 entry
= g_ptr_array_index(range
, i
);
896 if (entry
->base
- 1 == range_limit
) {
897 range_limit
= entry
->limit
;
899 crs_range_insert(tmp
, range_base
, range_limit
);
900 range_base
= entry
->base
;
901 range_limit
= entry
->limit
;
904 crs_range_insert(tmp
, range_base
, range_limit
);
906 g_ptr_array_set_size(range
, 0);
907 for (i
= 0; i
< tmp
->len
; i
++) {
908 entry
= g_ptr_array_index(tmp
, i
);
909 crs_range_insert(range
, entry
->base
, entry
->limit
);
911 g_ptr_array_free(tmp
, true);
914 static Aml
*build_crs(PCIHostState
*host
, CrsRangeSet
*range_set
)
916 Aml
*crs
= aml_resource_template();
917 CrsRangeSet temp_range_set
;
918 CrsRangeEntry
*entry
;
919 uint8_t max_bus
= pci_bus_num(host
->bus
);
924 crs_range_set_init(&temp_range_set
);
925 for (devfn
= 0; devfn
< ARRAY_SIZE(host
->bus
->devices
); devfn
++) {
926 uint64_t range_base
, range_limit
;
927 PCIDevice
*dev
= host
->bus
->devices
[devfn
];
933 for (i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
934 PCIIORegion
*r
= &dev
->io_regions
[i
];
936 range_base
= r
->addr
;
937 range_limit
= r
->addr
+ r
->size
- 1;
940 * Work-around for old bioses
941 * that do not support multiple root buses
943 if (!range_base
|| range_base
> range_limit
) {
947 if (r
->type
& PCI_BASE_ADDRESS_SPACE_IO
) {
948 crs_range_insert(temp_range_set
.io_ranges
,
949 range_base
, range_limit
);
950 } else { /* "memory" */
951 crs_range_insert(temp_range_set
.mem_ranges
,
952 range_base
, range_limit
);
956 type
= dev
->config
[PCI_HEADER_TYPE
] & ~PCI_HEADER_TYPE_MULTI_FUNCTION
;
957 if (type
== PCI_HEADER_TYPE_BRIDGE
) {
958 uint8_t subordinate
= dev
->config
[PCI_SUBORDINATE_BUS
];
959 if (subordinate
> max_bus
) {
960 max_bus
= subordinate
;
963 range_base
= pci_bridge_get_base(dev
, PCI_BASE_ADDRESS_SPACE_IO
);
964 range_limit
= pci_bridge_get_limit(dev
, PCI_BASE_ADDRESS_SPACE_IO
);
967 * Work-around for old bioses
968 * that do not support multiple root buses
970 if (range_base
&& range_base
<= range_limit
) {
971 crs_range_insert(temp_range_set
.io_ranges
,
972 range_base
, range_limit
);
976 pci_bridge_get_base(dev
, PCI_BASE_ADDRESS_SPACE_MEMORY
);
978 pci_bridge_get_limit(dev
, PCI_BASE_ADDRESS_SPACE_MEMORY
);
981 * Work-around for old bioses
982 * that do not support multiple root buses
984 if (range_base
&& range_base
<= range_limit
) {
985 uint64_t length
= range_limit
- range_base
+ 1;
986 if (range_limit
<= UINT32_MAX
&& length
<= UINT32_MAX
) {
987 crs_range_insert(temp_range_set
.mem_ranges
,
988 range_base
, range_limit
);
990 crs_range_insert(temp_range_set
.mem_64bit_ranges
,
991 range_base
, range_limit
);
996 pci_bridge_get_base(dev
, PCI_BASE_ADDRESS_MEM_PREFETCH
);
998 pci_bridge_get_limit(dev
, PCI_BASE_ADDRESS_MEM_PREFETCH
);
1001 * Work-around for old bioses
1002 * that do not support multiple root buses
1004 if (range_base
&& range_base
<= range_limit
) {
1005 uint64_t length
= range_limit
- range_base
+ 1;
1006 if (range_limit
<= UINT32_MAX
&& length
<= UINT32_MAX
) {
1007 crs_range_insert(temp_range_set
.mem_ranges
,
1008 range_base
, range_limit
);
1010 crs_range_insert(temp_range_set
.mem_64bit_ranges
,
1011 range_base
, range_limit
);
1017 crs_range_merge(temp_range_set
.io_ranges
);
1018 for (i
= 0; i
< temp_range_set
.io_ranges
->len
; i
++) {
1019 entry
= g_ptr_array_index(temp_range_set
.io_ranges
, i
);
1021 aml_word_io(AML_MIN_FIXED
, AML_MAX_FIXED
,
1022 AML_POS_DECODE
, AML_ENTIRE_RANGE
,
1023 0, entry
->base
, entry
->limit
, 0,
1024 entry
->limit
- entry
->base
+ 1));
1025 crs_range_insert(range_set
->io_ranges
, entry
->base
, entry
->limit
);
1028 crs_range_merge(temp_range_set
.mem_ranges
);
1029 for (i
= 0; i
< temp_range_set
.mem_ranges
->len
; i
++) {
1030 entry
= g_ptr_array_index(temp_range_set
.mem_ranges
, i
);
1032 aml_dword_memory(AML_POS_DECODE
, AML_MIN_FIXED
,
1033 AML_MAX_FIXED
, AML_NON_CACHEABLE
,
1035 0, entry
->base
, entry
->limit
, 0,
1036 entry
->limit
- entry
->base
+ 1));
1037 crs_range_insert(range_set
->mem_ranges
, entry
->base
, entry
->limit
);
1040 crs_range_merge(temp_range_set
.mem_64bit_ranges
);
1041 for (i
= 0; i
< temp_range_set
.mem_64bit_ranges
->len
; i
++) {
1042 entry
= g_ptr_array_index(temp_range_set
.mem_64bit_ranges
, i
);
1044 aml_qword_memory(AML_POS_DECODE
, AML_MIN_FIXED
,
1045 AML_MAX_FIXED
, AML_NON_CACHEABLE
,
1047 0, entry
->base
, entry
->limit
, 0,
1048 entry
->limit
- entry
->base
+ 1));
1049 crs_range_insert(range_set
->mem_64bit_ranges
,
1050 entry
->base
, entry
->limit
);
1053 crs_range_set_free(&temp_range_set
);
1056 aml_word_bus_number(AML_MIN_FIXED
, AML_MAX_FIXED
, AML_POS_DECODE
,
1058 pci_bus_num(host
->bus
),
1061 max_bus
- pci_bus_num(host
->bus
) + 1));
1066 static void build_hpet_aml(Aml
*table
)
1072 Aml
*scope
= aml_scope("_SB");
1073 Aml
*dev
= aml_device("HPET");
1074 Aml
*zero
= aml_int(0);
1075 Aml
*id
= aml_local(0);
1076 Aml
*period
= aml_local(1);
1078 aml_append(dev
, aml_name_decl("_HID", aml_eisaid("PNP0103")));
1079 aml_append(dev
, aml_name_decl("_UID", zero
));
1082 aml_operation_region("HPTM", AML_SYSTEM_MEMORY
, aml_int(HPET_BASE
),
1084 field
= aml_field("HPTM", AML_DWORD_ACC
, AML_LOCK
, AML_PRESERVE
);
1085 aml_append(field
, aml_named_field("VEND", 32));
1086 aml_append(field
, aml_named_field("PRD", 32));
1087 aml_append(dev
, field
);
1089 method
= aml_method("_STA", 0, AML_NOTSERIALIZED
);
1090 aml_append(method
, aml_store(aml_name("VEND"), id
));
1091 aml_append(method
, aml_store(aml_name("PRD"), period
));
1092 aml_append(method
, aml_shiftright(id
, aml_int(16), id
));
1093 if_ctx
= aml_if(aml_lor(aml_equal(id
, zero
),
1094 aml_equal(id
, aml_int(0xffff))));
1096 aml_append(if_ctx
, aml_return(zero
));
1098 aml_append(method
, if_ctx
);
1100 if_ctx
= aml_if(aml_lor(aml_equal(period
, zero
),
1101 aml_lgreater(period
, aml_int(100000000))));
1103 aml_append(if_ctx
, aml_return(zero
));
1105 aml_append(method
, if_ctx
);
1107 aml_append(method
, aml_return(aml_int(0x0F)));
1108 aml_append(dev
, method
);
1110 crs
= aml_resource_template();
1111 aml_append(crs
, aml_memory32_fixed(HPET_BASE
, HPET_LEN
, AML_READ_ONLY
));
1112 aml_append(dev
, aml_name_decl("_CRS", crs
));
1114 aml_append(scope
, dev
);
1115 aml_append(table
, scope
);
1118 static Aml
*build_fdinfo_aml(int idx
, FloppyDriveType type
)
1121 uint8_t maxc
, maxh
, maxs
;
1123 isa_fdc_get_drive_max_chs(type
, &maxc
, &maxh
, &maxs
);
1125 dev
= aml_device("FLP%c", 'A' + idx
);
1127 aml_append(dev
, aml_name_decl("_ADR", aml_int(idx
)));
1129 fdi
= aml_package(16);
1130 aml_append(fdi
, aml_int(idx
)); /* Drive Number */
1132 aml_int(cmos_get_fd_drive_type(type
))); /* Device Type */
1134 * the values below are the limits of the drive, and are thus independent
1135 * of the inserted media
1137 aml_append(fdi
, aml_int(maxc
)); /* Maximum Cylinder Number */
1138 aml_append(fdi
, aml_int(maxs
)); /* Maximum Sector Number */
1139 aml_append(fdi
, aml_int(maxh
)); /* Maximum Head Number */
1141 * SeaBIOS returns the below values for int 0x13 func 0x08 regardless of
1142 * the drive type, so shall we
1144 aml_append(fdi
, aml_int(0xAF)); /* disk_specify_1 */
1145 aml_append(fdi
, aml_int(0x02)); /* disk_specify_2 */
1146 aml_append(fdi
, aml_int(0x25)); /* disk_motor_wait */
1147 aml_append(fdi
, aml_int(0x02)); /* disk_sector_siz */
1148 aml_append(fdi
, aml_int(0x12)); /* disk_eot */
1149 aml_append(fdi
, aml_int(0x1B)); /* disk_rw_gap */
1150 aml_append(fdi
, aml_int(0xFF)); /* disk_dtl */
1151 aml_append(fdi
, aml_int(0x6C)); /* disk_formt_gap */
1152 aml_append(fdi
, aml_int(0xF6)); /* disk_fill */
1153 aml_append(fdi
, aml_int(0x0F)); /* disk_head_sttl */
1154 aml_append(fdi
, aml_int(0x08)); /* disk_motor_strt */
1156 aml_append(dev
, aml_name_decl("_FDI", fdi
));
1160 static Aml
*build_fdc_device_aml(ISADevice
*fdc
)
1166 #define ACPI_FDE_MAX_FD 4
1167 uint32_t fde_buf
[5] = {
1168 0, 0, 0, 0, /* presence of floppy drives #0 - #3 */
1169 cpu_to_le32(2) /* tape presence (2 == never present) */
1172 dev
= aml_device("FDC0");
1173 aml_append(dev
, aml_name_decl("_HID", aml_eisaid("PNP0700")));
1175 crs
= aml_resource_template();
1176 aml_append(crs
, aml_io(AML_DECODE16
, 0x03F2, 0x03F2, 0x00, 0x04));
1177 aml_append(crs
, aml_io(AML_DECODE16
, 0x03F7, 0x03F7, 0x00, 0x01));
1178 aml_append(crs
, aml_irq_no_flags(6));
1180 aml_dma(AML_COMPATIBILITY
, AML_NOTBUSMASTER
, AML_TRANSFER8
, 2));
1181 aml_append(dev
, aml_name_decl("_CRS", crs
));
1183 for (i
= 0; i
< MIN(MAX_FD
, ACPI_FDE_MAX_FD
); i
++) {
1184 FloppyDriveType type
= isa_fdc_get_drive_type(fdc
, i
);
1186 if (type
< FLOPPY_DRIVE_TYPE_NONE
) {
1187 fde_buf
[i
] = cpu_to_le32(1); /* drive present */
1188 aml_append(dev
, build_fdinfo_aml(i
, type
));
1191 aml_append(dev
, aml_name_decl("_FDE",
1192 aml_buffer(sizeof(fde_buf
), (uint8_t *)fde_buf
)));
1197 static Aml
*build_rtc_device_aml(void)
1202 dev
= aml_device("RTC");
1203 aml_append(dev
, aml_name_decl("_HID", aml_eisaid("PNP0B00")));
1204 crs
= aml_resource_template();
1205 aml_append(crs
, aml_io(AML_DECODE16
, 0x0070, 0x0070, 0x10, 0x02));
1206 aml_append(crs
, aml_irq_no_flags(8));
1207 aml_append(crs
, aml_io(AML_DECODE16
, 0x0072, 0x0072, 0x02, 0x06));
1208 aml_append(dev
, aml_name_decl("_CRS", crs
));
1213 static Aml
*build_kbd_device_aml(void)
1219 dev
= aml_device("KBD");
1220 aml_append(dev
, aml_name_decl("_HID", aml_eisaid("PNP0303")));
1222 method
= aml_method("_STA", 0, AML_NOTSERIALIZED
);
1223 aml_append(method
, aml_return(aml_int(0x0f)));
1224 aml_append(dev
, method
);
1226 crs
= aml_resource_template();
1227 aml_append(crs
, aml_io(AML_DECODE16
, 0x0060, 0x0060, 0x01, 0x01));
1228 aml_append(crs
, aml_io(AML_DECODE16
, 0x0064, 0x0064, 0x01, 0x01));
1229 aml_append(crs
, aml_irq_no_flags(1));
1230 aml_append(dev
, aml_name_decl("_CRS", crs
));
1235 static Aml
*build_mouse_device_aml(void)
1241 dev
= aml_device("MOU");
1242 aml_append(dev
, aml_name_decl("_HID", aml_eisaid("PNP0F13")));
1244 method
= aml_method("_STA", 0, AML_NOTSERIALIZED
);
1245 aml_append(method
, aml_return(aml_int(0x0f)));
1246 aml_append(dev
, method
);
1248 crs
= aml_resource_template();
1249 aml_append(crs
, aml_irq_no_flags(12));
1250 aml_append(dev
, aml_name_decl("_CRS", crs
));
1255 static Aml
*build_lpt_device_aml(void)
1262 Aml
*zero
= aml_int(0);
1263 Aml
*is_present
= aml_local(0);
1265 dev
= aml_device("LPT");
1266 aml_append(dev
, aml_name_decl("_HID", aml_eisaid("PNP0400")));
1268 method
= aml_method("_STA", 0, AML_NOTSERIALIZED
);
1269 aml_append(method
, aml_store(aml_name("LPEN"), is_present
));
1270 if_ctx
= aml_if(aml_equal(is_present
, zero
));
1272 aml_append(if_ctx
, aml_return(aml_int(0x00)));
1274 aml_append(method
, if_ctx
);
1275 else_ctx
= aml_else();
1277 aml_append(else_ctx
, aml_return(aml_int(0x0f)));
1279 aml_append(method
, else_ctx
);
1280 aml_append(dev
, method
);
1282 crs
= aml_resource_template();
1283 aml_append(crs
, aml_io(AML_DECODE16
, 0x0378, 0x0378, 0x08, 0x08));
1284 aml_append(crs
, aml_irq_no_flags(7));
1285 aml_append(dev
, aml_name_decl("_CRS", crs
));
1290 static Aml
*build_com_device_aml(uint8_t uid
)
1297 Aml
*zero
= aml_int(0);
1298 Aml
*is_present
= aml_local(0);
1299 const char *enabled_field
= "CAEN";
1301 uint16_t io_port
= 0x03F8;
1303 assert(uid
== 1 || uid
== 2);
1305 enabled_field
= "CBEN";
1310 dev
= aml_device("COM%d", uid
);
1311 aml_append(dev
, aml_name_decl("_HID", aml_eisaid("PNP0501")));
1312 aml_append(dev
, aml_name_decl("_UID", aml_int(uid
)));
1314 method
= aml_method("_STA", 0, AML_NOTSERIALIZED
);
1315 aml_append(method
, aml_store(aml_name("%s", enabled_field
), is_present
));
1316 if_ctx
= aml_if(aml_equal(is_present
, zero
));
1318 aml_append(if_ctx
, aml_return(aml_int(0x00)));
1320 aml_append(method
, if_ctx
);
1321 else_ctx
= aml_else();
1323 aml_append(else_ctx
, aml_return(aml_int(0x0f)));
1325 aml_append(method
, else_ctx
);
1326 aml_append(dev
, method
);
1328 crs
= aml_resource_template();
1329 aml_append(crs
, aml_io(AML_DECODE16
, io_port
, io_port
, 0x00, 0x08));
1330 aml_append(crs
, aml_irq_no_flags(irq
));
1331 aml_append(dev
, aml_name_decl("_CRS", crs
));
1336 static void build_isa_devices_aml(Aml
*table
)
1338 ISADevice
*fdc
= pc_find_fdc0();
1341 Aml
*scope
= aml_scope("_SB.PCI0.ISA");
1342 Object
*obj
= object_resolve_path_type("", TYPE_ISA_BUS
, &ambiguous
);
1344 aml_append(scope
, build_rtc_device_aml());
1345 aml_append(scope
, build_kbd_device_aml());
1346 aml_append(scope
, build_mouse_device_aml());
1348 aml_append(scope
, build_fdc_device_aml(fdc
));
1350 aml_append(scope
, build_lpt_device_aml());
1351 aml_append(scope
, build_com_device_aml(1));
1352 aml_append(scope
, build_com_device_aml(2));
1355 error_report("Multiple ISA busses, unable to define IPMI ACPI data");
1357 error_report("No ISA bus, unable to define IPMI ACPI data");
1359 build_acpi_ipmi_devices(scope
, BUS(obj
));
1362 aml_append(table
, scope
);
1365 static void build_dbg_aml(Aml
*table
)
1370 Aml
*scope
= aml_scope("\\");
1371 Aml
*buf
= aml_local(0);
1372 Aml
*len
= aml_local(1);
1373 Aml
*idx
= aml_local(2);
1376 aml_operation_region("DBG", AML_SYSTEM_IO
, aml_int(0x0402), 0x01));
1377 field
= aml_field("DBG", AML_BYTE_ACC
, AML_NOLOCK
, AML_PRESERVE
);
1378 aml_append(field
, aml_named_field("DBGB", 8));
1379 aml_append(scope
, field
);
1381 method
= aml_method("DBUG", 1, AML_NOTSERIALIZED
);
1383 aml_append(method
, aml_to_hexstring(aml_arg(0), buf
));
1384 aml_append(method
, aml_to_buffer(buf
, buf
));
1385 aml_append(method
, aml_subtract(aml_sizeof(buf
), aml_int(1), len
));
1386 aml_append(method
, aml_store(aml_int(0), idx
));
1388 while_ctx
= aml_while(aml_lless(idx
, len
));
1389 aml_append(while_ctx
,
1390 aml_store(aml_derefof(aml_index(buf
, idx
)), aml_name("DBGB")));
1391 aml_append(while_ctx
, aml_increment(idx
));
1392 aml_append(method
, while_ctx
);
1394 aml_append(method
, aml_store(aml_int(0x0A), aml_name("DBGB")));
1395 aml_append(scope
, method
);
1397 aml_append(table
, scope
);
1400 static Aml
*build_link_dev(const char *name
, uint8_t uid
, Aml
*reg
)
1405 uint32_t irqs
[] = {5, 10, 11};
1407 dev
= aml_device("%s", name
);
1408 aml_append(dev
, aml_name_decl("_HID", aml_eisaid("PNP0C0F")));
1409 aml_append(dev
, aml_name_decl("_UID", aml_int(uid
)));
1411 crs
= aml_resource_template();
1412 aml_append(crs
, aml_interrupt(AML_CONSUMER
, AML_LEVEL
, AML_ACTIVE_HIGH
,
1413 AML_SHARED
, irqs
, ARRAY_SIZE(irqs
)));
1414 aml_append(dev
, aml_name_decl("_PRS", crs
));
1416 method
= aml_method("_STA", 0, AML_NOTSERIALIZED
);
1417 aml_append(method
, aml_return(aml_call1("IQST", reg
)));
1418 aml_append(dev
, method
);
1420 method
= aml_method("_DIS", 0, AML_NOTSERIALIZED
);
1421 aml_append(method
, aml_or(reg
, aml_int(0x80), reg
));
1422 aml_append(dev
, method
);
1424 method
= aml_method("_CRS", 0, AML_NOTSERIALIZED
);
1425 aml_append(method
, aml_return(aml_call1("IQCR", reg
)));
1426 aml_append(dev
, method
);
1428 method
= aml_method("_SRS", 1, AML_NOTSERIALIZED
);
1429 aml_append(method
, aml_create_dword_field(aml_arg(0), aml_int(5), "PRRI"));
1430 aml_append(method
, aml_store(aml_name("PRRI"), reg
));
1431 aml_append(dev
, method
);
1436 static Aml
*build_gsi_link_dev(const char *name
, uint8_t uid
, uint8_t gsi
)
1443 dev
= aml_device("%s", name
);
1444 aml_append(dev
, aml_name_decl("_HID", aml_eisaid("PNP0C0F")));
1445 aml_append(dev
, aml_name_decl("_UID", aml_int(uid
)));
1447 crs
= aml_resource_template();
1449 aml_append(crs
, aml_interrupt(AML_CONSUMER
, AML_LEVEL
, AML_ACTIVE_HIGH
,
1450 AML_SHARED
, &irqs
, 1));
1451 aml_append(dev
, aml_name_decl("_PRS", crs
));
1453 aml_append(dev
, aml_name_decl("_CRS", crs
));
1456 * _DIS can be no-op because the interrupt cannot be disabled.
1458 method
= aml_method("_DIS", 0, AML_NOTSERIALIZED
);
1459 aml_append(dev
, method
);
1461 method
= aml_method("_SRS", 1, AML_NOTSERIALIZED
);
1462 aml_append(dev
, method
);
1467 /* _CRS method - get current settings */
1468 static Aml
*build_iqcr_method(bool is_piix4
)
1472 Aml
*method
= aml_method("IQCR", 1, AML_SERIALIZED
);
1473 Aml
*crs
= aml_resource_template();
1476 aml_append(crs
, aml_interrupt(AML_CONSUMER
, AML_LEVEL
,
1477 AML_ACTIVE_HIGH
, AML_SHARED
, &irqs
, 1));
1478 aml_append(method
, aml_name_decl("PRR0", crs
));
1481 aml_create_dword_field(aml_name("PRR0"), aml_int(5), "PRRI"));
1484 if_ctx
= aml_if(aml_lless(aml_arg(0), aml_int(0x80)));
1485 aml_append(if_ctx
, aml_store(aml_arg(0), aml_name("PRRI")));
1486 aml_append(method
, if_ctx
);
1489 aml_store(aml_and(aml_arg(0), aml_int(0xF), NULL
),
1493 aml_append(method
, aml_return(aml_name("PRR0")));
1497 /* _STA method - get status */
1498 static Aml
*build_irq_status_method(void)
1501 Aml
*method
= aml_method("IQST", 1, AML_NOTSERIALIZED
);
1503 if_ctx
= aml_if(aml_and(aml_int(0x80), aml_arg(0), NULL
));
1504 aml_append(if_ctx
, aml_return(aml_int(0x09)));
1505 aml_append(method
, if_ctx
);
1506 aml_append(method
, aml_return(aml_int(0x0B)));
1510 static void build_piix4_pci0_int(Aml
*table
)
1517 Aml
*sb_scope
= aml_scope("_SB");
1518 Aml
*pci0_scope
= aml_scope("PCI0");
1520 aml_append(pci0_scope
, build_prt(true));
1521 aml_append(sb_scope
, pci0_scope
);
1523 field
= aml_field("PCI0.ISA.P40C", AML_BYTE_ACC
, AML_NOLOCK
, AML_PRESERVE
);
1524 aml_append(field
, aml_named_field("PRQ0", 8));
1525 aml_append(field
, aml_named_field("PRQ1", 8));
1526 aml_append(field
, aml_named_field("PRQ2", 8));
1527 aml_append(field
, aml_named_field("PRQ3", 8));
1528 aml_append(sb_scope
, field
);
1530 aml_append(sb_scope
, build_irq_status_method());
1531 aml_append(sb_scope
, build_iqcr_method(true));
1533 aml_append(sb_scope
, build_link_dev("LNKA", 0, aml_name("PRQ0")));
1534 aml_append(sb_scope
, build_link_dev("LNKB", 1, aml_name("PRQ1")));
1535 aml_append(sb_scope
, build_link_dev("LNKC", 2, aml_name("PRQ2")));
1536 aml_append(sb_scope
, build_link_dev("LNKD", 3, aml_name("PRQ3")));
1538 dev
= aml_device("LNKS");
1540 aml_append(dev
, aml_name_decl("_HID", aml_eisaid("PNP0C0F")));
1541 aml_append(dev
, aml_name_decl("_UID", aml_int(4)));
1543 crs
= aml_resource_template();
1545 aml_append(crs
, aml_interrupt(AML_CONSUMER
, AML_LEVEL
,
1546 AML_ACTIVE_HIGH
, AML_SHARED
,
1548 aml_append(dev
, aml_name_decl("_PRS", crs
));
1550 /* The SCI cannot be disabled and is always attached to GSI 9,
1551 * so these are no-ops. We only need this link to override the
1552 * polarity to active high and match the content of the MADT.
1554 method
= aml_method("_STA", 0, AML_NOTSERIALIZED
);
1555 aml_append(method
, aml_return(aml_int(0x0b)));
1556 aml_append(dev
, method
);
1558 method
= aml_method("_DIS", 0, AML_NOTSERIALIZED
);
1559 aml_append(dev
, method
);
1561 method
= aml_method("_CRS", 0, AML_NOTSERIALIZED
);
1562 aml_append(method
, aml_return(aml_name("_PRS")));
1563 aml_append(dev
, method
);
1565 method
= aml_method("_SRS", 1, AML_NOTSERIALIZED
);
1566 aml_append(dev
, method
);
1568 aml_append(sb_scope
, dev
);
1570 aml_append(table
, sb_scope
);
1573 static void append_q35_prt_entry(Aml
*ctx
, uint32_t nr
, const char *name
)
1578 char base
= name
[3] < 'E' ? 'A' : 'E';
1579 char *s
= g_strdup(name
);
1580 Aml
*a_nr
= aml_int((nr
<< 16) | 0xffff);
1582 assert(strlen(s
) == 4);
1584 head
= name
[3] - base
;
1585 for (i
= 0; i
< 4; i
++) {
1589 s
[3] = base
+ head
+ i
;
1590 pkg
= aml_package(4);
1591 aml_append(pkg
, a_nr
);
1592 aml_append(pkg
, aml_int(i
));
1593 aml_append(pkg
, aml_name("%s", s
));
1594 aml_append(pkg
, aml_int(0));
1595 aml_append(ctx
, pkg
);
1600 static Aml
*build_q35_routing_table(const char *str
)
1604 char *name
= g_strdup_printf("%s ", str
);
1606 pkg
= aml_package(128);
1607 for (i
= 0; i
< 0x18; i
++) {
1608 name
[3] = 'E' + (i
& 0x3);
1609 append_q35_prt_entry(pkg
, i
, name
);
1613 append_q35_prt_entry(pkg
, 0x18, name
);
1615 /* INTA -> PIRQA for slot 25 - 31, see the default value of D<N>IR */
1616 for (i
= 0x0019; i
< 0x1e; i
++) {
1618 append_q35_prt_entry(pkg
, i
, name
);
1621 /* PCIe->PCI bridge. use PIRQ[E-H] */
1623 append_q35_prt_entry(pkg
, 0x1e, name
);
1625 append_q35_prt_entry(pkg
, 0x1f, name
);
1631 static void build_q35_pci0_int(Aml
*table
)
1635 Aml
*sb_scope
= aml_scope("_SB");
1636 Aml
*pci0_scope
= aml_scope("PCI0");
1638 /* Zero => PIC mode, One => APIC Mode */
1639 aml_append(table
, aml_name_decl("PICF", aml_int(0)));
1640 method
= aml_method("_PIC", 1, AML_NOTSERIALIZED
);
1642 aml_append(method
, aml_store(aml_arg(0), aml_name("PICF")));
1644 aml_append(table
, method
);
1646 aml_append(pci0_scope
,
1647 aml_name_decl("PRTP", build_q35_routing_table("LNK")));
1648 aml_append(pci0_scope
,
1649 aml_name_decl("PRTA", build_q35_routing_table("GSI")));
1651 method
= aml_method("_PRT", 0, AML_NOTSERIALIZED
);
1656 /* PCI IRQ routing table, example from ACPI 2.0a specification,
1658 /* Note: we provide the same info as the PCI routing
1659 table of the Bochs BIOS */
1660 if_ctx
= aml_if(aml_equal(aml_name("PICF"), aml_int(0)));
1661 aml_append(if_ctx
, aml_return(aml_name("PRTP")));
1662 aml_append(method
, if_ctx
);
1663 else_ctx
= aml_else();
1664 aml_append(else_ctx
, aml_return(aml_name("PRTA")));
1665 aml_append(method
, else_ctx
);
1667 aml_append(pci0_scope
, method
);
1668 aml_append(sb_scope
, pci0_scope
);
1670 field
= aml_field("PCI0.ISA.PIRQ", AML_BYTE_ACC
, AML_NOLOCK
, AML_PRESERVE
);
1671 aml_append(field
, aml_named_field("PRQA", 8));
1672 aml_append(field
, aml_named_field("PRQB", 8));
1673 aml_append(field
, aml_named_field("PRQC", 8));
1674 aml_append(field
, aml_named_field("PRQD", 8));
1675 aml_append(field
, aml_reserved_field(0x20));
1676 aml_append(field
, aml_named_field("PRQE", 8));
1677 aml_append(field
, aml_named_field("PRQF", 8));
1678 aml_append(field
, aml_named_field("PRQG", 8));
1679 aml_append(field
, aml_named_field("PRQH", 8));
1680 aml_append(sb_scope
, field
);
1682 aml_append(sb_scope
, build_irq_status_method());
1683 aml_append(sb_scope
, build_iqcr_method(false));
1685 aml_append(sb_scope
, build_link_dev("LNKA", 0, aml_name("PRQA")));
1686 aml_append(sb_scope
, build_link_dev("LNKB", 1, aml_name("PRQB")));
1687 aml_append(sb_scope
, build_link_dev("LNKC", 2, aml_name("PRQC")));
1688 aml_append(sb_scope
, build_link_dev("LNKD", 3, aml_name("PRQD")));
1689 aml_append(sb_scope
, build_link_dev("LNKE", 4, aml_name("PRQE")));
1690 aml_append(sb_scope
, build_link_dev("LNKF", 5, aml_name("PRQF")));
1691 aml_append(sb_scope
, build_link_dev("LNKG", 6, aml_name("PRQG")));
1692 aml_append(sb_scope
, build_link_dev("LNKH", 7, aml_name("PRQH")));
1694 aml_append(sb_scope
, build_gsi_link_dev("GSIA", 0x10, 0x10));
1695 aml_append(sb_scope
, build_gsi_link_dev("GSIB", 0x11, 0x11));
1696 aml_append(sb_scope
, build_gsi_link_dev("GSIC", 0x12, 0x12));
1697 aml_append(sb_scope
, build_gsi_link_dev("GSID", 0x13, 0x13));
1698 aml_append(sb_scope
, build_gsi_link_dev("GSIE", 0x14, 0x14));
1699 aml_append(sb_scope
, build_gsi_link_dev("GSIF", 0x15, 0x15));
1700 aml_append(sb_scope
, build_gsi_link_dev("GSIG", 0x16, 0x16));
1701 aml_append(sb_scope
, build_gsi_link_dev("GSIH", 0x17, 0x17));
1703 aml_append(table
, sb_scope
);
1706 static void build_q35_isa_bridge(Aml
*table
)
1712 scope
= aml_scope("_SB.PCI0");
1713 dev
= aml_device("ISA");
1714 aml_append(dev
, aml_name_decl("_ADR", aml_int(0x001F0000)));
1716 /* ICH9 PCI to ISA irq remapping */
1717 aml_append(dev
, aml_operation_region("PIRQ", AML_PCI_CONFIG
,
1718 aml_int(0x60), 0x0C));
1720 aml_append(dev
, aml_operation_region("LPCD", AML_PCI_CONFIG
,
1721 aml_int(0x80), 0x02));
1722 field
= aml_field("LPCD", AML_ANY_ACC
, AML_NOLOCK
, AML_PRESERVE
);
1723 aml_append(field
, aml_named_field("COMA", 3));
1724 aml_append(field
, aml_reserved_field(1));
1725 aml_append(field
, aml_named_field("COMB", 3));
1726 aml_append(field
, aml_reserved_field(1));
1727 aml_append(field
, aml_named_field("LPTD", 2));
1728 aml_append(dev
, field
);
1730 aml_append(dev
, aml_operation_region("LPCE", AML_PCI_CONFIG
,
1731 aml_int(0x82), 0x02));
1733 field
= aml_field("LPCE", AML_ANY_ACC
, AML_NOLOCK
, AML_PRESERVE
);
1734 aml_append(field
, aml_named_field("CAEN", 1));
1735 aml_append(field
, aml_named_field("CBEN", 1));
1736 aml_append(field
, aml_named_field("LPEN", 1));
1737 aml_append(dev
, field
);
1739 aml_append(scope
, dev
);
1740 aml_append(table
, scope
);
1743 static void build_piix4_pm(Aml
*table
)
1748 scope
= aml_scope("_SB.PCI0");
1749 dev
= aml_device("PX13");
1750 aml_append(dev
, aml_name_decl("_ADR", aml_int(0x00010003)));
1752 aml_append(dev
, aml_operation_region("P13C", AML_PCI_CONFIG
,
1753 aml_int(0x00), 0xff));
1754 aml_append(scope
, dev
);
1755 aml_append(table
, scope
);
1758 static void build_piix4_isa_bridge(Aml
*table
)
1764 scope
= aml_scope("_SB.PCI0");
1765 dev
= aml_device("ISA");
1766 aml_append(dev
, aml_name_decl("_ADR", aml_int(0x00010000)));
1768 /* PIIX PCI to ISA irq remapping */
1769 aml_append(dev
, aml_operation_region("P40C", AML_PCI_CONFIG
,
1770 aml_int(0x60), 0x04));
1772 field
= aml_field("^PX13.P13C", AML_ANY_ACC
, AML_NOLOCK
, AML_PRESERVE
);
1773 /* Offset(0x5f),, 7, */
1774 aml_append(field
, aml_reserved_field(0x2f8));
1775 aml_append(field
, aml_reserved_field(7));
1776 aml_append(field
, aml_named_field("LPEN", 1));
1777 /* Offset(0x67),, 3, */
1778 aml_append(field
, aml_reserved_field(0x38));
1779 aml_append(field
, aml_reserved_field(3));
1780 aml_append(field
, aml_named_field("CAEN", 1));
1781 aml_append(field
, aml_reserved_field(3));
1782 aml_append(field
, aml_named_field("CBEN", 1));
1783 aml_append(dev
, field
);
1785 aml_append(scope
, dev
);
1786 aml_append(table
, scope
);
1789 static void build_piix4_pci_hotplug(Aml
*table
)
1795 scope
= aml_scope("_SB.PCI0");
1798 aml_operation_region("PCST", AML_SYSTEM_IO
, aml_int(0xae00), 0x08));
1799 field
= aml_field("PCST", AML_DWORD_ACC
, AML_NOLOCK
, AML_WRITE_AS_ZEROS
);
1800 aml_append(field
, aml_named_field("PCIU", 32));
1801 aml_append(field
, aml_named_field("PCID", 32));
1802 aml_append(scope
, field
);
1805 aml_operation_region("SEJ", AML_SYSTEM_IO
, aml_int(0xae08), 0x04));
1806 field
= aml_field("SEJ", AML_DWORD_ACC
, AML_NOLOCK
, AML_WRITE_AS_ZEROS
);
1807 aml_append(field
, aml_named_field("B0EJ", 32));
1808 aml_append(scope
, field
);
1811 aml_operation_region("BNMR", AML_SYSTEM_IO
, aml_int(0xae10), 0x04));
1812 field
= aml_field("BNMR", AML_DWORD_ACC
, AML_NOLOCK
, AML_WRITE_AS_ZEROS
);
1813 aml_append(field
, aml_named_field("BNUM", 32));
1814 aml_append(scope
, field
);
1816 aml_append(scope
, aml_mutex("BLCK", 0));
1818 method
= aml_method("PCEJ", 2, AML_NOTSERIALIZED
);
1819 aml_append(method
, aml_acquire(aml_name("BLCK"), 0xFFFF));
1820 aml_append(method
, aml_store(aml_arg(0), aml_name("BNUM")));
1822 aml_store(aml_shiftleft(aml_int(1), aml_arg(1)), aml_name("B0EJ")));
1823 aml_append(method
, aml_release(aml_name("BLCK")));
1824 aml_append(method
, aml_return(aml_int(0)));
1825 aml_append(scope
, method
);
1827 aml_append(table
, scope
);
1830 static Aml
*build_q35_osc_method(void)
1836 Aml
*a_cwd1
= aml_name("CDW1");
1837 Aml
*a_ctrl
= aml_local(0);
1839 method
= aml_method("_OSC", 4, AML_NOTSERIALIZED
);
1840 aml_append(method
, aml_create_dword_field(aml_arg(3), aml_int(0), "CDW1"));
1842 if_ctx
= aml_if(aml_equal(
1843 aml_arg(0), aml_touuid("33DB4D5B-1FF7-401C-9657-7441C03DD766")));
1844 aml_append(if_ctx
, aml_create_dword_field(aml_arg(3), aml_int(4), "CDW2"));
1845 aml_append(if_ctx
, aml_create_dword_field(aml_arg(3), aml_int(8), "CDW3"));
1847 aml_append(if_ctx
, aml_store(aml_name("CDW3"), a_ctrl
));
1850 * Always allow native PME, AER (no dependencies)
1851 * Never allow SHPC (no SHPC controller in this system)
1853 aml_append(if_ctx
, aml_and(a_ctrl
, aml_int(0x1D), a_ctrl
));
1855 if_ctx2
= aml_if(aml_lnot(aml_equal(aml_arg(1), aml_int(1))));
1856 /* Unknown revision */
1857 aml_append(if_ctx2
, aml_or(a_cwd1
, aml_int(0x08), a_cwd1
));
1858 aml_append(if_ctx
, if_ctx2
);
1860 if_ctx2
= aml_if(aml_lnot(aml_equal(aml_name("CDW3"), a_ctrl
)));
1861 /* Capabilities bits were masked */
1862 aml_append(if_ctx2
, aml_or(a_cwd1
, aml_int(0x10), a_cwd1
));
1863 aml_append(if_ctx
, if_ctx2
);
1865 /* Update DWORD3 in the buffer */
1866 aml_append(if_ctx
, aml_store(a_ctrl
, aml_name("CDW3")));
1867 aml_append(method
, if_ctx
);
1869 else_ctx
= aml_else();
1870 /* Unrecognized UUID */
1871 aml_append(else_ctx
, aml_or(a_cwd1
, aml_int(4), a_cwd1
));
1872 aml_append(method
, else_ctx
);
1874 aml_append(method
, aml_return(aml_arg(3)));
1879 build_dsdt(GArray
*table_data
, BIOSLinker
*linker
,
1880 AcpiPmInfo
*pm
, AcpiMiscInfo
*misc
,
1881 Range
*pci_hole
, Range
*pci_hole64
, MachineState
*machine
)
1883 CrsRangeEntry
*entry
;
1884 Aml
*dsdt
, *sb_scope
, *scope
, *dev
, *method
, *field
, *pkg
, *crs
;
1885 CrsRangeSet crs_range_set
;
1886 PCMachineState
*pcms
= PC_MACHINE(machine
);
1887 PCMachineClass
*pcmc
= PC_MACHINE_GET_CLASS(machine
);
1888 uint32_t nr_mem
= machine
->ram_slots
;
1889 int root_bus_limit
= 0xFF;
1893 dsdt
= init_aml_allocator();
1895 /* Reserve space for header */
1896 acpi_data_push(dsdt
->buf
, sizeof(AcpiTableHeader
));
1898 build_dbg_aml(dsdt
);
1899 if (misc
->is_piix4
) {
1900 sb_scope
= aml_scope("_SB");
1901 dev
= aml_device("PCI0");
1902 aml_append(dev
, aml_name_decl("_HID", aml_eisaid("PNP0A03")));
1903 aml_append(dev
, aml_name_decl("_ADR", aml_int(0)));
1904 aml_append(dev
, aml_name_decl("_UID", aml_int(1)));
1905 aml_append(sb_scope
, dev
);
1906 aml_append(dsdt
, sb_scope
);
1908 build_hpet_aml(dsdt
);
1909 build_piix4_pm(dsdt
);
1910 build_piix4_isa_bridge(dsdt
);
1911 build_isa_devices_aml(dsdt
);
1912 build_piix4_pci_hotplug(dsdt
);
1913 build_piix4_pci0_int(dsdt
);
1915 sb_scope
= aml_scope("_SB");
1916 dev
= aml_device("PCI0");
1917 aml_append(dev
, aml_name_decl("_HID", aml_eisaid("PNP0A08")));
1918 aml_append(dev
, aml_name_decl("_CID", aml_eisaid("PNP0A03")));
1919 aml_append(dev
, aml_name_decl("_ADR", aml_int(0)));
1920 aml_append(dev
, aml_name_decl("_UID", aml_int(1)));
1921 aml_append(dev
, build_q35_osc_method());
1922 aml_append(sb_scope
, dev
);
1923 aml_append(dsdt
, sb_scope
);
1925 build_hpet_aml(dsdt
);
1926 build_q35_isa_bridge(dsdt
);
1927 build_isa_devices_aml(dsdt
);
1928 build_q35_pci0_int(dsdt
);
1931 if (pcmc
->legacy_cpu_hotplug
) {
1932 build_legacy_cpu_hotplug_aml(dsdt
, machine
, pm
->cpu_hp_io_base
);
1934 CPUHotplugFeatures opts
= {
1935 .apci_1_compatible
= true, .has_legacy_cphp
= true
1937 build_cpus_aml(dsdt
, machine
, opts
, pm
->cpu_hp_io_base
,
1938 "\\_SB.PCI0", "\\_GPE._E02");
1940 build_memory_hotplug_aml(dsdt
, nr_mem
, "\\_SB.PCI0", "\\_GPE._E03");
1942 scope
= aml_scope("_GPE");
1944 aml_append(scope
, aml_name_decl("_HID", aml_string("ACPI0006")));
1946 if (misc
->is_piix4
) {
1947 method
= aml_method("_E01", 0, AML_NOTSERIALIZED
);
1949 aml_acquire(aml_name("\\_SB.PCI0.BLCK"), 0xFFFF));
1950 aml_append(method
, aml_call0("\\_SB.PCI0.PCNT"));
1951 aml_append(method
, aml_release(aml_name("\\_SB.PCI0.BLCK")));
1952 aml_append(scope
, method
);
1955 if (pcms
->acpi_nvdimm_state
.is_enabled
) {
1956 method
= aml_method("_E04", 0, AML_NOTSERIALIZED
);
1957 aml_append(method
, aml_notify(aml_name("\\_SB.NVDR"),
1959 aml_append(scope
, method
);
1962 aml_append(dsdt
, scope
);
1964 crs_range_set_init(&crs_range_set
);
1965 bus
= PC_MACHINE(machine
)->bus
;
1967 QLIST_FOREACH(bus
, &bus
->child
, sibling
) {
1968 uint8_t bus_num
= pci_bus_num(bus
);
1969 uint8_t numa_node
= pci_bus_numa_node(bus
);
1971 /* look only for expander root buses */
1972 if (!pci_bus_is_root(bus
)) {
1976 if (bus_num
< root_bus_limit
) {
1977 root_bus_limit
= bus_num
- 1;
1980 scope
= aml_scope("\\_SB");
1981 dev
= aml_device("PC%.02X", bus_num
);
1982 aml_append(dev
, aml_name_decl("_UID", aml_int(bus_num
)));
1983 aml_append(dev
, aml_name_decl("_HID", aml_eisaid("PNP0A03")));
1984 aml_append(dev
, aml_name_decl("_BBN", aml_int(bus_num
)));
1985 if (pci_bus_is_express(bus
)) {
1986 aml_append(dev
, build_q35_osc_method());
1989 if (numa_node
!= NUMA_NODE_UNASSIGNED
) {
1990 aml_append(dev
, aml_name_decl("_PXM", aml_int(numa_node
)));
1993 aml_append(dev
, build_prt(false));
1994 crs
= build_crs(PCI_HOST_BRIDGE(BUS(bus
)->parent
), &crs_range_set
);
1995 aml_append(dev
, aml_name_decl("_CRS", crs
));
1996 aml_append(scope
, dev
);
1997 aml_append(dsdt
, scope
);
2001 scope
= aml_scope("\\_SB.PCI0");
2002 /* build PCI0._CRS */
2003 crs
= aml_resource_template();
2005 aml_word_bus_number(AML_MIN_FIXED
, AML_MAX_FIXED
, AML_POS_DECODE
,
2006 0x0000, 0x0, root_bus_limit
,
2007 0x0000, root_bus_limit
+ 1));
2008 aml_append(crs
, aml_io(AML_DECODE16
, 0x0CF8, 0x0CF8, 0x01, 0x08));
2011 aml_word_io(AML_MIN_FIXED
, AML_MAX_FIXED
,
2012 AML_POS_DECODE
, AML_ENTIRE_RANGE
,
2013 0x0000, 0x0000, 0x0CF7, 0x0000, 0x0CF8));
2015 crs_replace_with_free_ranges(crs_range_set
.io_ranges
, 0x0D00, 0xFFFF);
2016 for (i
= 0; i
< crs_range_set
.io_ranges
->len
; i
++) {
2017 entry
= g_ptr_array_index(crs_range_set
.io_ranges
, i
);
2019 aml_word_io(AML_MIN_FIXED
, AML_MAX_FIXED
,
2020 AML_POS_DECODE
, AML_ENTIRE_RANGE
,
2021 0x0000, entry
->base
, entry
->limit
,
2022 0x0000, entry
->limit
- entry
->base
+ 1));
2026 aml_dword_memory(AML_POS_DECODE
, AML_MIN_FIXED
, AML_MAX_FIXED
,
2027 AML_CACHEABLE
, AML_READ_WRITE
,
2028 0, 0x000A0000, 0x000BFFFF, 0, 0x00020000));
2030 crs_replace_with_free_ranges(crs_range_set
.mem_ranges
,
2031 range_lob(pci_hole
),
2032 range_upb(pci_hole
));
2033 for (i
= 0; i
< crs_range_set
.mem_ranges
->len
; i
++) {
2034 entry
= g_ptr_array_index(crs_range_set
.mem_ranges
, i
);
2036 aml_dword_memory(AML_POS_DECODE
, AML_MIN_FIXED
, AML_MAX_FIXED
,
2037 AML_NON_CACHEABLE
, AML_READ_WRITE
,
2038 0, entry
->base
, entry
->limit
,
2039 0, entry
->limit
- entry
->base
+ 1));
2042 if (!range_is_empty(pci_hole64
)) {
2043 crs_replace_with_free_ranges(crs_range_set
.mem_64bit_ranges
,
2044 range_lob(pci_hole64
),
2045 range_upb(pci_hole64
));
2046 for (i
= 0; i
< crs_range_set
.mem_64bit_ranges
->len
; i
++) {
2047 entry
= g_ptr_array_index(crs_range_set
.mem_64bit_ranges
, i
);
2049 aml_qword_memory(AML_POS_DECODE
, AML_MIN_FIXED
,
2051 AML_CACHEABLE
, AML_READ_WRITE
,
2052 0, entry
->base
, entry
->limit
,
2053 0, entry
->limit
- entry
->base
+ 1));
2057 if (misc
->tpm_version
!= TPM_VERSION_UNSPEC
) {
2058 aml_append(crs
, aml_memory32_fixed(TPM_TIS_ADDR_BASE
,
2059 TPM_TIS_ADDR_SIZE
, AML_READ_WRITE
));
2061 aml_append(scope
, aml_name_decl("_CRS", crs
));
2063 /* reserve GPE0 block resources */
2064 dev
= aml_device("GPE0");
2065 aml_append(dev
, aml_name_decl("_HID", aml_string("PNP0A06")));
2066 aml_append(dev
, aml_name_decl("_UID", aml_string("GPE0 resources")));
2067 /* device present, functioning, decoding, not shown in UI */
2068 aml_append(dev
, aml_name_decl("_STA", aml_int(0xB)));
2069 crs
= aml_resource_template();
2071 aml_io(AML_DECODE16
, pm
->gpe0_blk
, pm
->gpe0_blk
, 1, pm
->gpe0_blk_len
)
2073 aml_append(dev
, aml_name_decl("_CRS", crs
));
2074 aml_append(scope
, dev
);
2076 crs_range_set_free(&crs_range_set
);
2078 /* reserve PCIHP resources */
2079 if (pm
->pcihp_io_len
) {
2080 dev
= aml_device("PHPR");
2081 aml_append(dev
, aml_name_decl("_HID", aml_string("PNP0A06")));
2083 aml_name_decl("_UID", aml_string("PCI Hotplug resources")));
2084 /* device present, functioning, decoding, not shown in UI */
2085 aml_append(dev
, aml_name_decl("_STA", aml_int(0xB)));
2086 crs
= aml_resource_template();
2088 aml_io(AML_DECODE16
, pm
->pcihp_io_base
, pm
->pcihp_io_base
, 1,
2091 aml_append(dev
, aml_name_decl("_CRS", crs
));
2092 aml_append(scope
, dev
);
2094 aml_append(dsdt
, scope
);
2096 /* create S3_ / S4_ / S5_ packages if necessary */
2097 scope
= aml_scope("\\");
2098 if (!pm
->s3_disabled
) {
2099 pkg
= aml_package(4);
2100 aml_append(pkg
, aml_int(1)); /* PM1a_CNT.SLP_TYP */
2101 aml_append(pkg
, aml_int(1)); /* PM1b_CNT.SLP_TYP, FIXME: not impl. */
2102 aml_append(pkg
, aml_int(0)); /* reserved */
2103 aml_append(pkg
, aml_int(0)); /* reserved */
2104 aml_append(scope
, aml_name_decl("_S3", pkg
));
2107 if (!pm
->s4_disabled
) {
2108 pkg
= aml_package(4);
2109 aml_append(pkg
, aml_int(pm
->s4_val
)); /* PM1a_CNT.SLP_TYP */
2110 /* PM1b_CNT.SLP_TYP, FIXME: not impl. */
2111 aml_append(pkg
, aml_int(pm
->s4_val
));
2112 aml_append(pkg
, aml_int(0)); /* reserved */
2113 aml_append(pkg
, aml_int(0)); /* reserved */
2114 aml_append(scope
, aml_name_decl("_S4", pkg
));
2117 pkg
= aml_package(4);
2118 aml_append(pkg
, aml_int(0)); /* PM1a_CNT.SLP_TYP */
2119 aml_append(pkg
, aml_int(0)); /* PM1b_CNT.SLP_TYP not impl. */
2120 aml_append(pkg
, aml_int(0)); /* reserved */
2121 aml_append(pkg
, aml_int(0)); /* reserved */
2122 aml_append(scope
, aml_name_decl("_S5", pkg
));
2123 aml_append(dsdt
, scope
);
2125 /* create fw_cfg node, unconditionally */
2127 /* when using port i/o, the 8-bit data register *always* overlaps
2128 * with half of the 16-bit control register. Hence, the total size
2129 * of the i/o region used is FW_CFG_CTL_SIZE; when using DMA, the
2130 * DMA control register is located at FW_CFG_DMA_IO_BASE + 4 */
2131 uint8_t io_size
= object_property_get_bool(OBJECT(pcms
->fw_cfg
),
2132 "dma_enabled", NULL
) ?
2133 ROUND_UP(FW_CFG_CTL_SIZE
, 4) + sizeof(dma_addr_t
) :
2136 scope
= aml_scope("\\_SB.PCI0");
2137 dev
= aml_device("FWCF");
2139 aml_append(dev
, aml_name_decl("_HID", aml_string("QEMU0002")));
2141 /* device present, functioning, decoding, not shown in UI */
2142 aml_append(dev
, aml_name_decl("_STA", aml_int(0xB)));
2144 crs
= aml_resource_template();
2146 aml_io(AML_DECODE16
, FW_CFG_IO_BASE
, FW_CFG_IO_BASE
, 0x01, io_size
)
2148 aml_append(dev
, aml_name_decl("_CRS", crs
));
2150 aml_append(scope
, dev
);
2151 aml_append(dsdt
, scope
);
2154 if (misc
->applesmc_io_base
) {
2155 scope
= aml_scope("\\_SB.PCI0.ISA");
2156 dev
= aml_device("SMC");
2158 aml_append(dev
, aml_name_decl("_HID", aml_eisaid("APP0001")));
2159 /* device present, functioning, decoding, not shown in UI */
2160 aml_append(dev
, aml_name_decl("_STA", aml_int(0xB)));
2162 crs
= aml_resource_template();
2164 aml_io(AML_DECODE16
, misc
->applesmc_io_base
, misc
->applesmc_io_base
,
2165 0x01, APPLESMC_MAX_DATA_LENGTH
)
2167 aml_append(crs
, aml_irq_no_flags(6));
2168 aml_append(dev
, aml_name_decl("_CRS", crs
));
2170 aml_append(scope
, dev
);
2171 aml_append(dsdt
, scope
);
2174 if (misc
->pvpanic_port
) {
2175 scope
= aml_scope("\\_SB.PCI0.ISA");
2177 dev
= aml_device("PEVT");
2178 aml_append(dev
, aml_name_decl("_HID", aml_string("QEMU0001")));
2180 crs
= aml_resource_template();
2182 aml_io(AML_DECODE16
, misc
->pvpanic_port
, misc
->pvpanic_port
, 1, 1)
2184 aml_append(dev
, aml_name_decl("_CRS", crs
));
2186 aml_append(dev
, aml_operation_region("PEOR", AML_SYSTEM_IO
,
2187 aml_int(misc
->pvpanic_port
), 1));
2188 field
= aml_field("PEOR", AML_BYTE_ACC
, AML_NOLOCK
, AML_PRESERVE
);
2189 aml_append(field
, aml_named_field("PEPT", 8));
2190 aml_append(dev
, field
);
2192 /* device present, functioning, decoding, shown in UI */
2193 aml_append(dev
, aml_name_decl("_STA", aml_int(0xF)));
2195 method
= aml_method("RDPT", 0, AML_NOTSERIALIZED
);
2196 aml_append(method
, aml_store(aml_name("PEPT"), aml_local(0)));
2197 aml_append(method
, aml_return(aml_local(0)));
2198 aml_append(dev
, method
);
2200 method
= aml_method("WRPT", 1, AML_NOTSERIALIZED
);
2201 aml_append(method
, aml_store(aml_arg(0), aml_name("PEPT")));
2202 aml_append(dev
, method
);
2204 aml_append(scope
, dev
);
2205 aml_append(dsdt
, scope
);
2208 sb_scope
= aml_scope("\\_SB");
2213 pci_host
= acpi_get_i386_pci_host();
2215 bus
= PCI_HOST_BRIDGE(pci_host
)->bus
;
2219 Aml
*scope
= aml_scope("PCI0");
2220 /* Scan all PCI buses. Generate tables to support hotplug. */
2221 build_append_pci_bus_devices(scope
, bus
, pm
->pcihp_bridge_en
);
2223 if (misc
->tpm_version
!= TPM_VERSION_UNSPEC
) {
2224 dev
= aml_device("ISA.TPM");
2225 aml_append(dev
, aml_name_decl("_HID", aml_eisaid("PNP0C31")));
2226 aml_append(dev
, aml_name_decl("_STA", aml_int(0xF)));
2227 crs
= aml_resource_template();
2228 aml_append(crs
, aml_memory32_fixed(TPM_TIS_ADDR_BASE
,
2229 TPM_TIS_ADDR_SIZE
, AML_READ_WRITE
));
2231 FIXME: TPM_TIS_IRQ=5 conflicts with PNP0C0F irqs,
2232 Rewrite to take IRQ from TPM device model and
2233 fix default IRQ value there to use some unused IRQ
2235 /* aml_append(crs, aml_irq_no_flags(TPM_TIS_IRQ)); */
2236 aml_append(dev
, aml_name_decl("_CRS", crs
));
2237 aml_append(scope
, dev
);
2240 aml_append(sb_scope
, scope
);
2243 aml_append(dsdt
, sb_scope
);
2245 /* copy AML table into ACPI tables blob and patch header there */
2246 g_array_append_vals(table_data
, dsdt
->buf
->data
, dsdt
->buf
->len
);
2247 build_header(linker
, table_data
,
2248 (void *)(table_data
->data
+ table_data
->len
- dsdt
->buf
->len
),
2249 "DSDT", dsdt
->buf
->len
, 1, NULL
, NULL
);
2250 free_aml_allocator();
2254 build_hpet(GArray
*table_data
, BIOSLinker
*linker
)
2258 hpet
= acpi_data_push(table_data
, sizeof(*hpet
));
2259 /* Note timer_block_id value must be kept in sync with value advertised by
2262 hpet
->timer_block_id
= cpu_to_le32(0x8086a201);
2263 hpet
->addr
.address
= cpu_to_le64(HPET_BASE
);
2264 build_header(linker
, table_data
,
2265 (void *)hpet
, "HPET", sizeof(*hpet
), 1, NULL
, NULL
);
2269 build_tpm_tcpa(GArray
*table_data
, BIOSLinker
*linker
, GArray
*tcpalog
)
2271 Acpi20Tcpa
*tcpa
= acpi_data_push(table_data
, sizeof *tcpa
);
2272 unsigned log_addr_size
= sizeof(tcpa
->log_area_start_address
);
2273 unsigned log_addr_offset
=
2274 (char *)&tcpa
->log_area_start_address
- table_data
->data
;
2276 tcpa
->platform_class
= cpu_to_le16(TPM_TCPA_ACPI_CLASS_CLIENT
);
2277 tcpa
->log_area_minimum_length
= cpu_to_le32(TPM_LOG_AREA_MINIMUM_SIZE
);
2278 acpi_data_push(tcpalog
, le32_to_cpu(tcpa
->log_area_minimum_length
));
2280 bios_linker_loader_alloc(linker
, ACPI_BUILD_TPMLOG_FILE
, tcpalog
, 1,
2281 false /* high memory */);
2283 /* log area start address to be filled by Guest linker */
2284 bios_linker_loader_add_pointer(linker
,
2285 ACPI_BUILD_TABLE_FILE
, log_addr_offset
, log_addr_size
,
2286 ACPI_BUILD_TPMLOG_FILE
, 0);
2288 build_header(linker
, table_data
,
2289 (void *)tcpa
, "TCPA", sizeof(*tcpa
), 2, NULL
, NULL
);
2293 build_tpm2(GArray
*table_data
, BIOSLinker
*linker
)
2295 Acpi20TPM2
*tpm2_ptr
;
2297 tpm2_ptr
= acpi_data_push(table_data
, sizeof *tpm2_ptr
);
2299 tpm2_ptr
->platform_class
= cpu_to_le16(TPM2_ACPI_CLASS_CLIENT
);
2300 tpm2_ptr
->control_area_address
= cpu_to_le64(0);
2301 tpm2_ptr
->start_method
= cpu_to_le32(TPM2_START_METHOD_MMIO
);
2303 build_header(linker
, table_data
,
2304 (void *)tpm2_ptr
, "TPM2", sizeof(*tpm2_ptr
), 4, NULL
, NULL
);
2308 build_srat(GArray
*table_data
, BIOSLinker
*linker
, MachineState
*machine
)
2310 AcpiSystemResourceAffinityTable
*srat
;
2311 AcpiSratMemoryAffinity
*numamem
;
2314 int srat_start
, numa_start
, slots
;
2315 uint64_t mem_len
, mem_base
, next_base
;
2316 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
2317 const CPUArchIdList
*apic_ids
= mc
->possible_cpu_arch_ids(machine
);
2318 PCMachineState
*pcms
= PC_MACHINE(machine
);
2319 ram_addr_t hotplugabble_address_space_size
=
2320 object_property_get_int(OBJECT(pcms
), PC_MACHINE_MEMHP_REGION_SIZE
,
2323 srat_start
= table_data
->len
;
2325 srat
= acpi_data_push(table_data
, sizeof *srat
);
2326 srat
->reserved1
= cpu_to_le32(1);
2328 for (i
= 0; i
< apic_ids
->len
; i
++) {
2329 int node_id
= apic_ids
->cpus
[i
].props
.node_id
;
2330 uint32_t apic_id
= apic_ids
->cpus
[i
].arch_id
;
2332 if (apic_id
< 255) {
2333 AcpiSratProcessorAffinity
*core
;
2335 core
= acpi_data_push(table_data
, sizeof *core
);
2336 core
->type
= ACPI_SRAT_PROCESSOR_APIC
;
2337 core
->length
= sizeof(*core
);
2338 core
->local_apic_id
= apic_id
;
2339 core
->proximity_lo
= node_id
;
2340 memset(core
->proximity_hi
, 0, 3);
2341 core
->local_sapic_eid
= 0;
2342 core
->flags
= cpu_to_le32(1);
2344 AcpiSratProcessorX2ApicAffinity
*core
;
2346 core
= acpi_data_push(table_data
, sizeof *core
);
2347 core
->type
= ACPI_SRAT_PROCESSOR_x2APIC
;
2348 core
->length
= sizeof(*core
);
2349 core
->x2apic_id
= cpu_to_le32(apic_id
);
2350 core
->proximity_domain
= cpu_to_le32(node_id
);
2351 core
->flags
= cpu_to_le32(1);
2356 /* the memory map is a bit tricky, it contains at least one hole
2357 * from 640k-1M and possibly another one from 3.5G-4G.
2360 numa_start
= table_data
->len
;
2362 numamem
= acpi_data_push(table_data
, sizeof *numamem
);
2363 build_srat_memory(numamem
, 0, 640 * 1024, 0, MEM_AFFINITY_ENABLED
);
2364 next_base
= 1024 * 1024;
2365 for (i
= 1; i
< pcms
->numa_nodes
+ 1; ++i
) {
2366 mem_base
= next_base
;
2367 mem_len
= pcms
->node_mem
[i
- 1];
2369 mem_len
-= 1024 * 1024;
2371 next_base
= mem_base
+ mem_len
;
2373 /* Cut out the ACPI_PCI hole */
2374 if (mem_base
<= pcms
->below_4g_mem_size
&&
2375 next_base
> pcms
->below_4g_mem_size
) {
2376 mem_len
-= next_base
- pcms
->below_4g_mem_size
;
2378 numamem
= acpi_data_push(table_data
, sizeof *numamem
);
2379 build_srat_memory(numamem
, mem_base
, mem_len
, i
- 1,
2380 MEM_AFFINITY_ENABLED
);
2382 mem_base
= 1ULL << 32;
2383 mem_len
= next_base
- pcms
->below_4g_mem_size
;
2384 next_base
+= (1ULL << 32) - 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 slots
= (table_data
->len
- numa_start
) / sizeof *numamem
;
2391 for (; slots
< pcms
->numa_nodes
+ 2; slots
++) {
2392 numamem
= acpi_data_push(table_data
, sizeof *numamem
);
2393 build_srat_memory(numamem
, 0, 0, 0, MEM_AFFINITY_NOFLAGS
);
2397 * Entry is required for Windows to enable memory hotplug in OS
2398 * and for Linux to enable SWIOTLB when booted with less than
2399 * 4G of RAM. Windows works better if the entry sets proximity
2400 * to the highest NUMA node in the machine.
2401 * Memory devices may override proximity set by this entry,
2402 * providing _PXM method if necessary.
2404 if (hotplugabble_address_space_size
) {
2405 numamem
= acpi_data_push(table_data
, sizeof *numamem
);
2406 build_srat_memory(numamem
, pcms
->hotplug_memory
.base
,
2407 hotplugabble_address_space_size
, pcms
->numa_nodes
- 1,
2408 MEM_AFFINITY_HOTPLUGGABLE
| MEM_AFFINITY_ENABLED
);
2411 build_header(linker
, table_data
,
2412 (void *)(table_data
->data
+ srat_start
),
2414 table_data
->len
- srat_start
, 1, NULL
, NULL
);
2418 build_mcfg_q35(GArray
*table_data
, BIOSLinker
*linker
, AcpiMcfgInfo
*info
)
2420 AcpiTableMcfg
*mcfg
;
2422 int len
= sizeof(*mcfg
) + 1 * sizeof(mcfg
->allocation
[0]);
2424 mcfg
= acpi_data_push(table_data
, len
);
2425 mcfg
->allocation
[0].address
= cpu_to_le64(info
->mcfg_base
);
2426 /* Only a single allocation so no need to play with segments */
2427 mcfg
->allocation
[0].pci_segment
= cpu_to_le16(0);
2428 mcfg
->allocation
[0].start_bus_number
= 0;
2429 mcfg
->allocation
[0].end_bus_number
= PCIE_MMCFG_BUS(info
->mcfg_size
- 1);
2431 /* MCFG is used for ECAM which can be enabled or disabled by guest.
2432 * To avoid table size changes (which create migration issues),
2433 * always create the table even if there are no allocations,
2434 * but set the signature to a reserved value in this case.
2435 * ACPI spec requires OSPMs to ignore such tables.
2437 if (info
->mcfg_base
== PCIE_BASE_ADDR_UNMAPPED
) {
2438 /* Reserved signature: ignored by OSPM */
2443 build_header(linker
, table_data
, (void *)mcfg
, sig
, len
, 1, NULL
, NULL
);
2447 * VT-d spec 8.1 DMA Remapping Reporting Structure
2448 * (version Oct. 2014 or later)
2451 build_dmar_q35(GArray
*table_data
, BIOSLinker
*linker
)
2453 int dmar_start
= table_data
->len
;
2455 AcpiTableDmar
*dmar
;
2456 AcpiDmarHardwareUnit
*drhd
;
2457 AcpiDmarRootPortATS
*atsr
;
2458 uint8_t dmar_flags
= 0;
2459 X86IOMMUState
*iommu
= x86_iommu_get_default();
2460 AcpiDmarDeviceScope
*scope
= NULL
;
2461 /* Root complex IOAPIC use one path[0] only */
2462 size_t ioapic_scope_size
= sizeof(*scope
) + sizeof(scope
->path
[0]);
2465 if (iommu
->intr_supported
) {
2466 dmar_flags
|= 0x1; /* Flags: 0x1: INT_REMAP */
2469 dmar
= acpi_data_push(table_data
, sizeof(*dmar
));
2470 dmar
->host_address_width
= VTD_HOST_ADDRESS_WIDTH
- 1;
2471 dmar
->flags
= dmar_flags
;
2473 /* DMAR Remapping Hardware Unit Definition structure */
2474 drhd
= acpi_data_push(table_data
, sizeof(*drhd
) + ioapic_scope_size
);
2475 drhd
->type
= cpu_to_le16(ACPI_DMAR_TYPE_HARDWARE_UNIT
);
2476 drhd
->length
= cpu_to_le16(sizeof(*drhd
) + ioapic_scope_size
);
2477 drhd
->flags
= ACPI_DMAR_INCLUDE_PCI_ALL
;
2478 drhd
->pci_segment
= cpu_to_le16(0);
2479 drhd
->address
= cpu_to_le64(Q35_HOST_BRIDGE_IOMMU_ADDR
);
2481 /* Scope definition for the root-complex IOAPIC. See VT-d spec
2482 * 8.3.1 (version Oct. 2014 or later). */
2483 scope
= &drhd
->scope
[0];
2484 scope
->entry_type
= 0x03; /* Type: 0x03 for IOAPIC */
2485 scope
->length
= ioapic_scope_size
;
2486 scope
->enumeration_id
= ACPI_BUILD_IOAPIC_ID
;
2487 scope
->bus
= Q35_PSEUDO_BUS_PLATFORM
;
2488 scope
->path
[0].device
= PCI_SLOT(Q35_PSEUDO_DEVFN_IOAPIC
);
2489 scope
->path
[0].function
= PCI_FUNC(Q35_PSEUDO_DEVFN_IOAPIC
);
2491 if (iommu
->dt_supported
) {
2492 atsr
= acpi_data_push(table_data
, sizeof(*atsr
));
2493 atsr
->type
= cpu_to_le16(ACPI_DMAR_TYPE_ATSR
);
2494 atsr
->length
= cpu_to_le16(sizeof(*atsr
));
2495 atsr
->flags
= ACPI_DMAR_ATSR_ALL_PORTS
;
2496 atsr
->pci_segment
= cpu_to_le16(0);
2499 build_header(linker
, table_data
, (void *)(table_data
->data
+ dmar_start
),
2500 "DMAR", table_data
->len
- dmar_start
, 1, NULL
, NULL
);
2503 * IVRS table as specified in AMD IOMMU Specification v2.62, Section 5.2
2504 * accessible here http://support.amd.com/TechDocs/48882_IOMMU.pdf
2507 build_amd_iommu(GArray
*table_data
, BIOSLinker
*linker
)
2509 int iommu_start
= table_data
->len
;
2510 AMDVIState
*s
= AMD_IOMMU_DEVICE(x86_iommu_get_default());
2513 acpi_data_push(table_data
, sizeof(AcpiTableHeader
));
2514 /* IVinfo - IO virtualization information common to all
2515 * IOMMU units in a system
2517 build_append_int_noprefix(table_data
, 40UL << 8/* PASize */, 4);
2519 build_append_int_noprefix(table_data
, 0, 8);
2521 /* IVHD definition - type 10h */
2522 build_append_int_noprefix(table_data
, 0x10, 1);
2523 /* virtualization flags */
2524 build_append_int_noprefix(table_data
,
2525 (1UL << 0) | /* HtTunEn */
2526 (1UL << 4) | /* iotblSup */
2527 (1UL << 6) | /* PrefSup */
2528 (1UL << 7), /* PPRSup */
2531 build_append_int_noprefix(table_data
, 0x24, 2);
2533 build_append_int_noprefix(table_data
, s
->devid
, 2);
2534 /* Capability offset */
2535 build_append_int_noprefix(table_data
, s
->capab_offset
, 2);
2536 /* IOMMU base address */
2537 build_append_int_noprefix(table_data
, s
->mmio
.addr
, 8);
2538 /* PCI Segment Group */
2539 build_append_int_noprefix(table_data
, 0, 2);
2541 build_append_int_noprefix(table_data
, 0, 2);
2542 /* IOMMU Feature Reporting */
2543 build_append_int_noprefix(table_data
,
2544 (48UL << 30) | /* HATS */
2545 (48UL << 28) | /* GATS */
2546 (1UL << 2), /* GTSup */
2549 * Type 1 device entry reporting all devices
2550 * These are 4-byte device entries currently reporting the range of
2551 * Refer to Spec - Table 95:IVHD Device Entry Type Codes(4-byte)
2553 build_append_int_noprefix(table_data
, 0x0000001, 4);
2555 build_header(linker
, table_data
, (void *)(table_data
->data
+ iommu_start
),
2556 "IVRS", table_data
->len
- iommu_start
, 1, NULL
, NULL
);
2560 build_rsdp(GArray
*rsdp_table
, BIOSLinker
*linker
, unsigned rsdt_tbl_offset
)
2562 AcpiRsdpDescriptor
*rsdp
= acpi_data_push(rsdp_table
, sizeof *rsdp
);
2563 unsigned rsdt_pa_size
= sizeof(rsdp
->rsdt_physical_address
);
2564 unsigned rsdt_pa_offset
=
2565 (char *)&rsdp
->rsdt_physical_address
- rsdp_table
->data
;
2567 bios_linker_loader_alloc(linker
, ACPI_BUILD_RSDP_FILE
, rsdp_table
, 16,
2568 true /* fseg memory */);
2570 memcpy(&rsdp
->signature
, "RSD PTR ", 8);
2571 memcpy(rsdp
->oem_id
, ACPI_BUILD_APPNAME6
, 6);
2572 /* Address to be filled by Guest linker */
2573 bios_linker_loader_add_pointer(linker
,
2574 ACPI_BUILD_RSDP_FILE
, rsdt_pa_offset
, rsdt_pa_size
,
2575 ACPI_BUILD_TABLE_FILE
, rsdt_tbl_offset
);
2577 /* Checksum to be filled by Guest linker */
2578 bios_linker_loader_add_checksum(linker
, ACPI_BUILD_RSDP_FILE
,
2579 (char *)rsdp
- rsdp_table
->data
, sizeof *rsdp
,
2580 (char *)&rsdp
->checksum
- rsdp_table
->data
);
2586 struct AcpiBuildState
{
2587 /* Copy of table in RAM (for patching). */
2588 MemoryRegion
*table_mr
;
2589 /* Is table patched? */
2592 MemoryRegion
*rsdp_mr
;
2593 MemoryRegion
*linker_mr
;
2596 static bool acpi_get_mcfg(AcpiMcfgInfo
*mcfg
)
2601 pci_host
= acpi_get_i386_pci_host();
2604 o
= object_property_get_qobject(pci_host
, PCIE_HOST_MCFG_BASE
, NULL
);
2608 mcfg
->mcfg_base
= qnum_get_uint(qobject_to_qnum(o
));
2611 o
= object_property_get_qobject(pci_host
, PCIE_HOST_MCFG_SIZE
, NULL
);
2613 mcfg
->mcfg_size
= qnum_get_uint(qobject_to_qnum(o
));
2619 void acpi_build(AcpiBuildTables
*tables
, MachineState
*machine
)
2621 PCMachineState
*pcms
= PC_MACHINE(machine
);
2622 PCMachineClass
*pcmc
= PC_MACHINE_GET_CLASS(pcms
);
2623 GArray
*table_offsets
;
2624 unsigned facs
, dsdt
, rsdt
, fadt
;
2628 Range pci_hole
, pci_hole64
;
2631 GArray
*tables_blob
= tables
->table_data
;
2632 AcpiSlicOem slic_oem
= { .id
= NULL
, .table_id
= NULL
};
2633 Object
*vmgenid_dev
;
2635 acpi_get_pm_info(&pm
);
2636 acpi_get_misc_info(&misc
);
2637 acpi_get_pci_holes(&pci_hole
, &pci_hole64
);
2638 acpi_get_slic_oem(&slic_oem
);
2640 table_offsets
= g_array_new(false, true /* clear */,
2642 ACPI_BUILD_DPRINTF("init ACPI tables\n");
2644 bios_linker_loader_alloc(tables
->linker
,
2645 ACPI_BUILD_TABLE_FILE
, tables_blob
,
2646 64 /* Ensure FACS is aligned */,
2647 false /* high memory */);
2650 * FACS is pointed to by FADT.
2651 * We place it first since it's the only table that has alignment
2654 facs
= tables_blob
->len
;
2655 build_facs(tables_blob
, tables
->linker
);
2657 /* DSDT is pointed to by FADT */
2658 dsdt
= tables_blob
->len
;
2659 build_dsdt(tables_blob
, tables
->linker
, &pm
, &misc
,
2660 &pci_hole
, &pci_hole64
, machine
);
2662 /* Count the size of the DSDT and SSDT, we will need it for legacy
2663 * sizing of ACPI tables.
2665 aml_len
+= tables_blob
->len
- dsdt
;
2667 /* ACPI tables pointed to by RSDT */
2668 fadt
= tables_blob
->len
;
2669 acpi_add_table(table_offsets
, tables_blob
);
2670 build_fadt(tables_blob
, tables
->linker
, &pm
, facs
, dsdt
,
2671 slic_oem
.id
, slic_oem
.table_id
);
2672 aml_len
+= tables_blob
->len
- fadt
;
2674 acpi_add_table(table_offsets
, tables_blob
);
2675 build_madt(tables_blob
, tables
->linker
, pcms
);
2677 vmgenid_dev
= find_vmgenid_dev();
2679 acpi_add_table(table_offsets
, tables_blob
);
2680 vmgenid_build_acpi(VMGENID(vmgenid_dev
), tables_blob
,
2681 tables
->vmgenid
, tables
->linker
);
2684 if (misc
.has_hpet
) {
2685 acpi_add_table(table_offsets
, tables_blob
);
2686 build_hpet(tables_blob
, tables
->linker
);
2688 if (misc
.tpm_version
!= TPM_VERSION_UNSPEC
) {
2689 acpi_add_table(table_offsets
, tables_blob
);
2690 build_tpm_tcpa(tables_blob
, tables
->linker
, tables
->tcpalog
);
2692 if (misc
.tpm_version
== TPM_VERSION_2_0
) {
2693 acpi_add_table(table_offsets
, tables_blob
);
2694 build_tpm2(tables_blob
, tables
->linker
);
2697 if (pcms
->numa_nodes
) {
2698 acpi_add_table(table_offsets
, tables_blob
);
2699 build_srat(tables_blob
, tables
->linker
, machine
);
2700 if (have_numa_distance
) {
2701 acpi_add_table(table_offsets
, tables_blob
);
2702 build_slit(tables_blob
, tables
->linker
);
2705 if (acpi_get_mcfg(&mcfg
)) {
2706 acpi_add_table(table_offsets
, tables_blob
);
2707 build_mcfg_q35(tables_blob
, tables
->linker
, &mcfg
);
2709 if (x86_iommu_get_default()) {
2710 IommuType IOMMUType
= x86_iommu_get_type();
2711 if (IOMMUType
== TYPE_AMD
) {
2712 acpi_add_table(table_offsets
, tables_blob
);
2713 build_amd_iommu(tables_blob
, tables
->linker
);
2714 } else if (IOMMUType
== TYPE_INTEL
) {
2715 acpi_add_table(table_offsets
, tables_blob
);
2716 build_dmar_q35(tables_blob
, tables
->linker
);
2719 if (pcms
->acpi_nvdimm_state
.is_enabled
) {
2720 nvdimm_build_acpi(table_offsets
, tables_blob
, tables
->linker
,
2721 &pcms
->acpi_nvdimm_state
, machine
->ram_slots
);
2724 /* Add tables supplied by user (if any) */
2725 for (u
= acpi_table_first(); u
; u
= acpi_table_next(u
)) {
2726 unsigned len
= acpi_table_len(u
);
2728 acpi_add_table(table_offsets
, tables_blob
);
2729 g_array_append_vals(tables_blob
, u
, len
);
2732 /* RSDT is pointed to by RSDP */
2733 rsdt
= tables_blob
->len
;
2734 build_rsdt(tables_blob
, tables
->linker
, table_offsets
,
2735 slic_oem
.id
, slic_oem
.table_id
);
2737 /* RSDP is in FSEG memory, so allocate it separately */
2738 build_rsdp(tables
->rsdp
, tables
->linker
, rsdt
);
2740 /* We'll expose it all to Guest so we want to reduce
2741 * chance of size changes.
2743 * We used to align the tables to 4k, but of course this would
2744 * too simple to be enough. 4k turned out to be too small an
2745 * alignment very soon, and in fact it is almost impossible to
2746 * keep the table size stable for all (max_cpus, max_memory_slots)
2747 * combinations. So the table size is always 64k for pc-i440fx-2.1
2748 * and we give an error if the table grows beyond that limit.
2750 * We still have the problem of migrating from "-M pc-i440fx-2.0". For
2751 * that, we exploit the fact that QEMU 2.1 generates _smaller_ tables
2752 * than 2.0 and we can always pad the smaller tables with zeros. We can
2753 * then use the exact size of the 2.0 tables.
2755 * All this is for PIIX4, since QEMU 2.0 didn't support Q35 migration.
2757 if (pcmc
->legacy_acpi_table_size
) {
2758 /* Subtracting aml_len gives the size of fixed tables. Then add the
2759 * size of the PIIX4 DSDT/SSDT in QEMU 2.0.
2761 int legacy_aml_len
=
2762 pcmc
->legacy_acpi_table_size
+
2763 ACPI_BUILD_LEGACY_CPU_AML_SIZE
* pcms
->apic_id_limit
;
2764 int legacy_table_size
=
2765 ROUND_UP(tables_blob
->len
- aml_len
+ legacy_aml_len
,
2766 ACPI_BUILD_ALIGN_SIZE
);
2767 if (tables_blob
->len
> legacy_table_size
) {
2768 /* Should happen only with PCI bridges and -M pc-i440fx-2.0. */
2769 error_report("Warning: migration may not work.");
2771 g_array_set_size(tables_blob
, legacy_table_size
);
2773 /* Make sure we have a buffer in case we need to resize the tables. */
2774 if (tables_blob
->len
> ACPI_BUILD_TABLE_SIZE
/ 2) {
2775 /* As of QEMU 2.1, this fires with 160 VCPUs and 255 memory slots. */
2776 error_report("Warning: ACPI tables are larger than 64k.");
2777 error_report("Warning: migration may not work.");
2778 error_report("Warning: please remove CPUs, NUMA nodes, "
2779 "memory slots or PCI bridges.");
2781 acpi_align_size(tables_blob
, ACPI_BUILD_TABLE_SIZE
);
2784 acpi_align_size(tables
->linker
->cmd_blob
, ACPI_BUILD_ALIGN_SIZE
);
2786 /* Cleanup memory that's no longer used. */
2787 g_array_free(table_offsets
, true);
2790 static void acpi_ram_update(MemoryRegion
*mr
, GArray
*data
)
2792 uint32_t size
= acpi_data_len(data
);
2794 /* Make sure RAM size is correct - in case it got changed e.g. by migration */
2795 memory_region_ram_resize(mr
, size
, &error_abort
);
2797 memcpy(memory_region_get_ram_ptr(mr
), data
->data
, size
);
2798 memory_region_set_dirty(mr
, 0, size
);
2801 static void acpi_build_update(void *build_opaque
)
2803 AcpiBuildState
*build_state
= build_opaque
;
2804 AcpiBuildTables tables
;
2806 /* No state to update or already patched? Nothing to do. */
2807 if (!build_state
|| build_state
->patched
) {
2810 build_state
->patched
= 1;
2812 acpi_build_tables_init(&tables
);
2814 acpi_build(&tables
, MACHINE(qdev_get_machine()));
2816 acpi_ram_update(build_state
->table_mr
, tables
.table_data
);
2818 if (build_state
->rsdp
) {
2819 memcpy(build_state
->rsdp
, tables
.rsdp
->data
, acpi_data_len(tables
.rsdp
));
2821 acpi_ram_update(build_state
->rsdp_mr
, tables
.rsdp
);
2824 acpi_ram_update(build_state
->linker_mr
, tables
.linker
->cmd_blob
);
2825 acpi_build_tables_cleanup(&tables
, true);
2828 static void acpi_build_reset(void *build_opaque
)
2830 AcpiBuildState
*build_state
= build_opaque
;
2831 build_state
->patched
= 0;
2834 static MemoryRegion
*acpi_add_rom_blob(AcpiBuildState
*build_state
,
2835 GArray
*blob
, const char *name
,
2838 return rom_add_blob(name
, blob
->data
, acpi_data_len(blob
), max_size
, -1,
2839 name
, acpi_build_update
, build_state
, NULL
, true);
2842 static const VMStateDescription vmstate_acpi_build
= {
2843 .name
= "acpi_build",
2845 .minimum_version_id
= 1,
2846 .fields
= (VMStateField
[]) {
2847 VMSTATE_UINT8(patched
, AcpiBuildState
),
2848 VMSTATE_END_OF_LIST()
2852 void acpi_setup(void)
2854 PCMachineState
*pcms
= PC_MACHINE(qdev_get_machine());
2855 PCMachineClass
*pcmc
= PC_MACHINE_GET_CLASS(pcms
);
2856 AcpiBuildTables tables
;
2857 AcpiBuildState
*build_state
;
2858 Object
*vmgenid_dev
;
2860 if (!pcms
->fw_cfg
) {
2861 ACPI_BUILD_DPRINTF("No fw cfg. Bailing out.\n");
2865 if (!pcms
->acpi_build_enabled
) {
2866 ACPI_BUILD_DPRINTF("ACPI build disabled. Bailing out.\n");
2870 if (!acpi_enabled
) {
2871 ACPI_BUILD_DPRINTF("ACPI disabled. Bailing out.\n");
2875 build_state
= g_malloc0(sizeof *build_state
);
2877 acpi_set_pci_info();
2879 acpi_build_tables_init(&tables
);
2880 acpi_build(&tables
, MACHINE(pcms
));
2882 /* Now expose it all to Guest */
2883 build_state
->table_mr
= acpi_add_rom_blob(build_state
, tables
.table_data
,
2884 ACPI_BUILD_TABLE_FILE
,
2885 ACPI_BUILD_TABLE_MAX_SIZE
);
2886 assert(build_state
->table_mr
!= NULL
);
2888 build_state
->linker_mr
=
2889 acpi_add_rom_blob(build_state
, tables
.linker
->cmd_blob
,
2890 "etc/table-loader", 0);
2892 fw_cfg_add_file(pcms
->fw_cfg
, ACPI_BUILD_TPMLOG_FILE
,
2893 tables
.tcpalog
->data
, acpi_data_len(tables
.tcpalog
));
2895 vmgenid_dev
= find_vmgenid_dev();
2897 vmgenid_add_fw_cfg(VMGENID(vmgenid_dev
), pcms
->fw_cfg
,
2901 if (!pcmc
->rsdp_in_ram
) {
2903 * Keep for compatibility with old machine types.
2904 * Though RSDP is small, its contents isn't immutable, so
2905 * we'll update it along with the rest of tables on guest access.
2907 uint32_t rsdp_size
= acpi_data_len(tables
.rsdp
);
2909 build_state
->rsdp
= g_memdup(tables
.rsdp
->data
, rsdp_size
);
2910 fw_cfg_add_file_callback(pcms
->fw_cfg
, ACPI_BUILD_RSDP_FILE
,
2911 acpi_build_update
, build_state
,
2912 build_state
->rsdp
, rsdp_size
, true);
2913 build_state
->rsdp_mr
= NULL
;
2915 build_state
->rsdp
= NULL
;
2916 build_state
->rsdp_mr
= acpi_add_rom_blob(build_state
, tables
.rsdp
,
2917 ACPI_BUILD_RSDP_FILE
, 0);
2920 qemu_register_reset(acpi_build_reset
, build_state
);
2921 acpi_build_reset(build_state
);
2922 vmstate_register(NULL
, 0, &vmstate_acpi_build
, build_state
);
2924 /* Cleanup tables but don't free the memory: we track it
2927 acpi_build_tables_cleanup(&tables
, false);