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 "acpi-build.h"
26 #include "qemu-common.h"
27 #include "qemu/bitmap.h"
28 #include "qemu/range.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/i386/acpi-defs.h"
35 #include "hw/acpi/acpi.h"
36 #include "hw/nvram/fw_cfg.h"
37 #include "bios-linker-loader.h"
38 #include "hw/loader.h"
39 #include "hw/isa/isa.h"
41 /* Supported chipsets: */
42 #include "hw/acpi/piix4.h"
43 #include "hw/i386/ich9.h"
44 #include "hw/pci/pci_bus.h"
45 #include "hw/pci-host/q35.h"
47 #include "hw/i386/q35-acpi-dsdt.hex"
48 #include "hw/i386/acpi-dsdt.hex"
50 #include "qapi/qmp/qint.h"
51 #include "qom/qom-qobject.h"
53 typedef struct AcpiCpuInfo
{
54 DECLARE_BITMAP(found_cpus
, MAX_CPUMASK_BITS
+ 1);
57 typedef struct AcpiMcfgInfo
{
62 typedef struct AcpiPmInfo
{
67 uint8_t acpi_enable_cmd
;
68 uint8_t acpi_disable_cmd
;
70 uint32_t gpe0_blk_len
;
74 typedef struct AcpiMiscInfo
{
76 DECLARE_BITMAP(slot_hotplug_enable
, PCI_SLOT_MAX
);
77 const unsigned char *dsdt_code
;
79 uint16_t pvpanic_port
;
82 static void acpi_get_dsdt(AcpiMiscInfo
*info
)
84 unsigned short applesmc_sta_val
, *applesmc_sta_off
;
85 Object
*piix
= piix4_pm_find();
86 Object
*lpc
= ich9_lpc_find();
87 assert(!!piix
!= !!lpc
);
90 info
->dsdt_code
= AcpiDsdtAmlCode
;
91 info
->dsdt_size
= sizeof AcpiDsdtAmlCode
;
92 applesmc_sta_off
= piix_dsdt_applesmc_sta
;
95 info
->dsdt_code
= Q35AcpiDsdtAmlCode
;
96 info
->dsdt_size
= sizeof Q35AcpiDsdtAmlCode
;
97 applesmc_sta_off
= q35_dsdt_applesmc_sta
;
100 /* Patch in appropriate value for AppleSMC _STA */
101 applesmc_sta_val
= applesmc_find() ? 0x0b : 0x00;
102 *(uint16_t *)(info
->dsdt_code
+ *applesmc_sta_off
) =
103 cpu_to_le16(applesmc_sta_val
);
107 int acpi_add_cpu_info(Object
*o
, void *opaque
)
109 AcpiCpuInfo
*cpu
= opaque
;
112 if (object_dynamic_cast(o
, TYPE_CPU
)) {
113 apic_id
= object_property_get_int(o
, "apic-id", NULL
);
114 assert(apic_id
<= MAX_CPUMASK_BITS
);
116 set_bit(apic_id
, cpu
->found_cpus
);
119 object_child_foreach(o
, acpi_add_cpu_info
, opaque
);
123 static void acpi_get_cpu_info(AcpiCpuInfo
*cpu
)
125 Object
*root
= object_get_root();
127 memset(cpu
->found_cpus
, 0, sizeof cpu
->found_cpus
);
128 object_child_foreach(root
, acpi_add_cpu_info
, cpu
);
131 static void acpi_get_pm_info(AcpiPmInfo
*pm
)
133 Object
*piix
= piix4_pm_find();
134 Object
*lpc
= ich9_lpc_find();
146 /* Fill in optional s3/s4 related properties */
147 o
= object_property_get_qobject(obj
, ACPI_PM_PROP_S3_DISABLED
, NULL
);
149 pm
->s3_disabled
= qint_get_int(qobject_to_qint(o
));
151 pm
->s3_disabled
= false;
153 o
= object_property_get_qobject(obj
, ACPI_PM_PROP_S4_DISABLED
, NULL
);
155 pm
->s4_disabled
= qint_get_int(qobject_to_qint(o
));
157 pm
->s4_disabled
= false;
159 o
= object_property_get_qobject(obj
, ACPI_PM_PROP_S4_VAL
, NULL
);
161 pm
->s4_val
= qint_get_int(qobject_to_qint(o
));
166 /* Fill in mandatory properties */
167 pm
->sci_int
= object_property_get_int(obj
, ACPI_PM_PROP_SCI_INT
, NULL
);
169 pm
->acpi_enable_cmd
= object_property_get_int(obj
,
170 ACPI_PM_PROP_ACPI_ENABLE_CMD
,
172 pm
->acpi_disable_cmd
= object_property_get_int(obj
,
173 ACPI_PM_PROP_ACPI_DISABLE_CMD
,
175 pm
->io_base
= object_property_get_int(obj
, ACPI_PM_PROP_PM_IO_BASE
,
177 pm
->gpe0_blk
= object_property_get_int(obj
, ACPI_PM_PROP_GPE0_BLK
,
179 pm
->gpe0_blk_len
= object_property_get_int(obj
, ACPI_PM_PROP_GPE0_BLK_LEN
,
183 static void acpi_get_hotplug_info(AcpiMiscInfo
*misc
)
186 PCIBus
*bus
= find_i440fx();
189 /* Only PIIX supports ACPI hotplug */
190 memset(misc
->slot_hotplug_enable
, 0, sizeof misc
->slot_hotplug_enable
);
194 memset(misc
->slot_hotplug_enable
, 0xff,
195 DIV_ROUND_UP(PCI_SLOT_MAX
, BITS_PER_BYTE
));
197 for (i
= 0; i
< ARRAY_SIZE(bus
->devices
); ++i
) {
199 PCIDevice
*pdev
= bus
->devices
[i
];
205 pc
= PCI_DEVICE_GET_CLASS(pdev
);
207 if (pc
->no_hotplug
) {
208 int slot
= PCI_SLOT(i
);
210 clear_bit(slot
, misc
->slot_hotplug_enable
);
215 static void acpi_get_misc_info(AcpiMiscInfo
*info
)
217 info
->has_hpet
= hpet_find();
218 info
->pvpanic_port
= pvpanic_port();
221 static void acpi_get_pci_info(PcPciInfo
*info
)
226 pci_host
= object_resolve_path_type("", TYPE_PCI_HOST_BRIDGE
, &ambiguous
);
227 g_assert(!ambiguous
);
230 info
->w32
.begin
= object_property_get_int(pci_host
,
231 PCI_HOST_PROP_PCI_HOLE_START
,
233 info
->w32
.end
= object_property_get_int(pci_host
,
234 PCI_HOST_PROP_PCI_HOLE_END
,
236 info
->w64
.begin
= object_property_get_int(pci_host
,
237 PCI_HOST_PROP_PCI_HOLE64_START
,
239 info
->w64
.end
= object_property_get_int(pci_host
,
240 PCI_HOST_PROP_PCI_HOLE64_END
,
244 #define ACPI_BUILD_APPNAME "Bochs"
245 #define ACPI_BUILD_APPNAME6 "BOCHS "
246 #define ACPI_BUILD_APPNAME4 "BXPC"
248 #define ACPI_BUILD_DPRINTF(level, fmt, ...) do {} while (0)
250 #define ACPI_BUILD_TABLE_FILE "etc/acpi/tables"
251 #define ACPI_BUILD_RSDP_FILE "etc/acpi/rsdp"
254 build_header(GArray
*linker
, GArray
*table_data
,
255 AcpiTableHeader
*h
, uint32_t sig
, int len
, uint8_t rev
)
257 h
->signature
= cpu_to_le32(sig
);
258 h
->length
= cpu_to_le32(len
);
260 memcpy(h
->oem_id
, ACPI_BUILD_APPNAME6
, 6);
261 memcpy(h
->oem_table_id
, ACPI_BUILD_APPNAME4
, 4);
262 memcpy(h
->oem_table_id
+ 4, (void *)&sig
, 4);
263 h
->oem_revision
= cpu_to_le32(1);
264 memcpy(h
->asl_compiler_id
, ACPI_BUILD_APPNAME4
, 4);
265 h
->asl_compiler_revision
= cpu_to_le32(1);
267 /* Checksum to be filled in by Guest linker */
268 bios_linker_loader_add_checksum(linker
, ACPI_BUILD_TABLE_FILE
,
269 table_data
->data
, h
, len
, &h
->checksum
);
272 static inline GArray
*build_alloc_array(void)
274 return g_array_new(false, true /* clear */, 1);
277 static inline void build_free_array(GArray
*array
)
279 g_array_free(array
, true);
282 static inline void build_prepend_byte(GArray
*array
, uint8_t val
)
284 g_array_prepend_val(array
, val
);
287 static inline void build_append_byte(GArray
*array
, uint8_t val
)
289 g_array_append_val(array
, val
);
292 static inline void build_append_array(GArray
*array
, GArray
*val
)
294 g_array_append_vals(array
, val
->data
, val
->len
);
297 static void GCC_FMT_ATTR(2, 3)
298 build_append_nameseg(GArray
*array
, const char *format
, ...)
300 /* It would be nicer to use g_string_vprintf but it's only there in 2.22 */
305 va_start(args
, format
);
306 len
= vsnprintf(s
, sizeof s
, format
, args
);
310 g_array_append_vals(array
, s
, len
);
313 /* 5.4 Definition Block Encoding */
315 PACKAGE_LENGTH_1BYTE_SHIFT
= 6, /* Up to 63 - use extra 2 bits. */
316 PACKAGE_LENGTH_2BYTE_SHIFT
= 4,
317 PACKAGE_LENGTH_3BYTE_SHIFT
= 12,
318 PACKAGE_LENGTH_4BYTE_SHIFT
= 20,
321 static void build_prepend_package_length(GArray
*package
, unsigned min_bytes
)
324 unsigned length
= package
->len
;
325 unsigned length_bytes
;
327 if (length
+ 1 < (1 << PACKAGE_LENGTH_1BYTE_SHIFT
)) {
329 } else if (length
+ 2 < (1 << PACKAGE_LENGTH_3BYTE_SHIFT
)) {
331 } else if (length
+ 3 < (1 << PACKAGE_LENGTH_4BYTE_SHIFT
)) {
337 /* Force length to at least min_bytes.
338 * This wastes memory but that's how bios did it.
340 length_bytes
= MAX(length_bytes
, min_bytes
);
342 /* PkgLength is the length of the inclusive length of the data. */
343 length
+= length_bytes
;
345 switch (length_bytes
) {
348 build_prepend_byte(package
, byte
);
351 byte
= length
>> PACKAGE_LENGTH_4BYTE_SHIFT
;
352 build_prepend_byte(package
, byte
);
353 length
&= (1 << PACKAGE_LENGTH_4BYTE_SHIFT
) - 1;
356 byte
= length
>> PACKAGE_LENGTH_3BYTE_SHIFT
;
357 build_prepend_byte(package
, byte
);
358 length
&= (1 << PACKAGE_LENGTH_3BYTE_SHIFT
) - 1;
361 byte
= length
>> PACKAGE_LENGTH_2BYTE_SHIFT
;
362 build_prepend_byte(package
, byte
);
363 length
&= (1 << PACKAGE_LENGTH_2BYTE_SHIFT
) - 1;
367 * Most significant two bits of byte zero indicate how many following bytes
368 * are in PkgLength encoding.
370 byte
= ((length_bytes
- 1) << PACKAGE_LENGTH_1BYTE_SHIFT
) | length
;
371 build_prepend_byte(package
, byte
);
374 static void build_package(GArray
*package
, uint8_t op
, unsigned min_bytes
)
376 build_prepend_package_length(package
, min_bytes
);
377 build_prepend_byte(package
, op
);
380 static void build_append_value(GArray
*table
, uint32_t value
, int size
)
387 prefix
= 0x0A; /* BytePrefix */
390 prefix
= 0x0B; /* WordPrefix */
393 prefix
= 0x0C; /* DWordPrefix */
399 build_append_byte(table
, prefix
);
400 for (i
= 0; i
< size
; ++i
) {
401 build_append_byte(table
, value
& 0xFF);
406 static void build_append_notify_target(GArray
*method
, GArray
*target_name
,
407 uint32_t value
, int size
)
409 GArray
*notify
= build_alloc_array();
410 uint8_t op
= 0xA0; /* IfOp */
412 build_append_byte(notify
, 0x93); /* LEqualOp */
413 build_append_byte(notify
, 0x68); /* Arg0Op */
414 build_append_value(notify
, value
, size
);
415 build_append_byte(notify
, 0x86); /* NotifyOp */
416 build_append_array(notify
, target_name
);
417 build_append_byte(notify
, 0x69); /* Arg1Op */
420 build_package(notify
, op
, 1);
422 build_append_array(method
, notify
);
424 build_free_array(notify
);
427 #define ACPI_PORT_SMI_CMD 0x00b2 /* TODO: this is APM_CNT_IOPORT */
429 static inline void *acpi_data_push(GArray
*table_data
, unsigned size
)
431 unsigned off
= table_data
->len
;
432 g_array_set_size(table_data
, off
+ size
);
433 return table_data
->data
+ off
;
436 static unsigned acpi_data_len(GArray
*table
)
438 #if GLIB_CHECK_VERSION(2, 22, 0)
439 assert(g_array_get_element_size(table
) == 1);
444 static void acpi_align_size(GArray
*blob
, unsigned align
)
446 /* Align size to multiple of given size. This reduces the chance
447 * we need to change size in the future (breaking cross version migration).
449 g_array_set_size(blob
, ROUND_UP(acpi_data_len(blob
), align
));
452 /* Get pointer within table in a safe manner */
453 #define ACPI_BUILD_PTR(table, size, off, type) \
454 ((type *)(acpi_data_get_ptr(table, size, off, sizeof(type))))
456 static inline void *acpi_data_get_ptr(uint8_t *table_data
, unsigned table_size
,
457 unsigned off
, unsigned size
)
459 assert(off
+ size
> off
);
460 assert(off
+ size
<= table_size
);
461 return table_data
+ off
;
464 static inline void acpi_add_table(GArray
*table_offsets
, GArray
*table_data
)
466 uint32_t offset
= cpu_to_le32(table_data
->len
);
467 g_array_append_val(table_offsets
, offset
);
472 build_facs(GArray
*table_data
, GArray
*linker
, PcGuestInfo
*guest_info
)
474 AcpiFacsDescriptorRev1
*facs
= acpi_data_push(table_data
, sizeof *facs
);
475 facs
->signature
= cpu_to_le32(ACPI_FACS_SIGNATURE
);
476 facs
->length
= cpu_to_le32(sizeof(*facs
));
479 /* Load chipset information in FADT */
480 static void fadt_setup(AcpiFadtDescriptorRev1
*fadt
, AcpiPmInfo
*pm
)
484 fadt
->sci_int
= cpu_to_le16(pm
->sci_int
);
485 fadt
->smi_cmd
= cpu_to_le32(ACPI_PORT_SMI_CMD
);
486 fadt
->acpi_enable
= pm
->acpi_enable_cmd
;
487 fadt
->acpi_disable
= pm
->acpi_disable_cmd
;
488 /* EVT, CNT, TMR offset matches hw/acpi/core.c */
489 fadt
->pm1a_evt_blk
= cpu_to_le32(pm
->io_base
);
490 fadt
->pm1a_cnt_blk
= cpu_to_le32(pm
->io_base
+ 0x04);
491 fadt
->pm_tmr_blk
= cpu_to_le32(pm
->io_base
+ 0x08);
492 fadt
->gpe0_blk
= cpu_to_le32(pm
->gpe0_blk
);
493 /* EVT, CNT, TMR length matches hw/acpi/core.c */
494 fadt
->pm1_evt_len
= 4;
495 fadt
->pm1_cnt_len
= 2;
496 fadt
->pm_tmr_len
= 4;
497 fadt
->gpe0_blk_len
= pm
->gpe0_blk_len
;
498 fadt
->plvl2_lat
= cpu_to_le16(0xfff); /* C2 state not supported */
499 fadt
->plvl3_lat
= cpu_to_le16(0xfff); /* C3 state not supported */
500 fadt
->flags
= cpu_to_le32((1 << ACPI_FADT_F_WBINVD
) |
501 (1 << ACPI_FADT_F_PROC_C1
) |
502 (1 << ACPI_FADT_F_SLP_BUTTON
) |
503 (1 << ACPI_FADT_F_RTC_S4
));
504 fadt
->flags
|= cpu_to_le32(1 << ACPI_FADT_F_USE_PLATFORM_CLOCK
);
510 build_fadt(GArray
*table_data
, GArray
*linker
, AcpiPmInfo
*pm
,
511 unsigned facs
, unsigned dsdt
)
513 AcpiFadtDescriptorRev1
*fadt
= acpi_data_push(table_data
, sizeof(*fadt
));
515 fadt
->firmware_ctrl
= cpu_to_le32(facs
);
516 /* FACS address to be filled by Guest linker */
517 bios_linker_loader_add_pointer(linker
, ACPI_BUILD_TABLE_FILE
,
518 ACPI_BUILD_TABLE_FILE
,
519 table_data
, &fadt
->firmware_ctrl
,
520 sizeof fadt
->firmware_ctrl
);
522 fadt
->dsdt
= cpu_to_le32(dsdt
);
523 /* DSDT address to be filled by Guest linker */
524 bios_linker_loader_add_pointer(linker
, ACPI_BUILD_TABLE_FILE
,
525 ACPI_BUILD_TABLE_FILE
,
526 table_data
, &fadt
->dsdt
,
529 fadt_setup(fadt
, pm
);
531 build_header(linker
, table_data
,
532 (void *)fadt
, ACPI_FACP_SIGNATURE
, sizeof(*fadt
), 1);
536 build_madt(GArray
*table_data
, GArray
*linker
, AcpiCpuInfo
*cpu
,
537 PcGuestInfo
*guest_info
)
539 int madt_start
= table_data
->len
;
541 AcpiMultipleApicTable
*madt
;
542 AcpiMadtIoApic
*io_apic
;
543 AcpiMadtIntsrcovr
*intsrcovr
;
544 AcpiMadtLocalNmi
*local_nmi
;
547 madt
= acpi_data_push(table_data
, sizeof *madt
);
548 madt
->local_apic_address
= cpu_to_le32(APIC_DEFAULT_ADDRESS
);
549 madt
->flags
= cpu_to_le32(1);
551 for (i
= 0; i
< guest_info
->apic_id_limit
; i
++) {
552 AcpiMadtProcessorApic
*apic
= acpi_data_push(table_data
, sizeof *apic
);
553 apic
->type
= ACPI_APIC_PROCESSOR
;
554 apic
->length
= sizeof(*apic
);
555 apic
->processor_id
= i
;
556 apic
->local_apic_id
= i
;
557 if (test_bit(i
, cpu
->found_cpus
)) {
558 apic
->flags
= cpu_to_le32(1);
560 apic
->flags
= cpu_to_le32(0);
563 io_apic
= acpi_data_push(table_data
, sizeof *io_apic
);
564 io_apic
->type
= ACPI_APIC_IO
;
565 io_apic
->length
= sizeof(*io_apic
);
566 #define ACPI_BUILD_IOAPIC_ID 0x0
567 io_apic
->io_apic_id
= ACPI_BUILD_IOAPIC_ID
;
568 io_apic
->address
= cpu_to_le32(IO_APIC_DEFAULT_ADDRESS
);
569 io_apic
->interrupt
= cpu_to_le32(0);
571 if (guest_info
->apic_xrupt_override
) {
572 intsrcovr
= acpi_data_push(table_data
, sizeof *intsrcovr
);
573 intsrcovr
->type
= ACPI_APIC_XRUPT_OVERRIDE
;
574 intsrcovr
->length
= sizeof(*intsrcovr
);
575 intsrcovr
->source
= 0;
576 intsrcovr
->gsi
= cpu_to_le32(2);
577 intsrcovr
->flags
= cpu_to_le16(0); /* conforms to bus specifications */
579 for (i
= 1; i
< 16; i
++) {
580 #define ACPI_BUILD_PCI_IRQS ((1<<5) | (1<<9) | (1<<10) | (1<<11))
581 if (!(ACPI_BUILD_PCI_IRQS
& (1 << i
))) {
582 /* No need for a INT source override structure. */
585 intsrcovr
= acpi_data_push(table_data
, sizeof *intsrcovr
);
586 intsrcovr
->type
= ACPI_APIC_XRUPT_OVERRIDE
;
587 intsrcovr
->length
= sizeof(*intsrcovr
);
588 intsrcovr
->source
= i
;
589 intsrcovr
->gsi
= cpu_to_le32(i
);
590 intsrcovr
->flags
= cpu_to_le16(0xd); /* active high, level triggered */
593 local_nmi
= acpi_data_push(table_data
, sizeof *local_nmi
);
594 local_nmi
->type
= ACPI_APIC_LOCAL_NMI
;
595 local_nmi
->length
= sizeof(*local_nmi
);
596 local_nmi
->processor_id
= 0xff; /* all processors */
597 local_nmi
->flags
= cpu_to_le16(0);
598 local_nmi
->lint
= 1; /* ACPI_LINT1 */
600 build_header(linker
, table_data
,
601 (void *)(table_data
->data
+ madt_start
), ACPI_APIC_SIGNATURE
,
602 table_data
->len
- madt_start
, 1);
605 /* Encode a hex value */
606 static inline char acpi_get_hex(uint32_t val
)
609 return (val
<= 9) ? ('0' + val
) : ('A' + val
- 10);
612 #include "hw/i386/ssdt-proc.hex"
614 /* 0x5B 0x83 ProcessorOp PkgLength NameString ProcID */
615 #define ACPI_PROC_OFFSET_CPUHEX (*ssdt_proc_name - *ssdt_proc_start + 2)
616 #define ACPI_PROC_OFFSET_CPUID1 (*ssdt_proc_name - *ssdt_proc_start + 4)
617 #define ACPI_PROC_OFFSET_CPUID2 (*ssdt_proc_id - *ssdt_proc_start)
618 #define ACPI_PROC_SIZEOF (*ssdt_proc_end - *ssdt_proc_start)
619 #define ACPI_PROC_AML (ssdp_proc_aml + *ssdt_proc_start)
621 /* 0x5B 0x82 DeviceOp PkgLength NameString */
622 #define ACPI_PCIHP_OFFSET_HEX (*ssdt_pcihp_name - *ssdt_pcihp_start + 1)
623 #define ACPI_PCIHP_OFFSET_ID (*ssdt_pcihp_id - *ssdt_pcihp_start)
624 #define ACPI_PCIHP_OFFSET_ADR (*ssdt_pcihp_adr - *ssdt_pcihp_start)
625 #define ACPI_PCIHP_OFFSET_EJ0 (*ssdt_pcihp_ej0 - *ssdt_pcihp_start)
626 #define ACPI_PCIHP_SIZEOF (*ssdt_pcihp_end - *ssdt_pcihp_start)
627 #define ACPI_PCIHP_AML (ssdp_pcihp_aml + *ssdt_pcihp_start)
629 #define ACPI_SSDT_SIGNATURE 0x54445353 /* SSDT */
630 #define ACPI_SSDT_HEADER_LENGTH 36
632 #include "hw/i386/ssdt-misc.hex"
633 #include "hw/i386/ssdt-pcihp.hex"
636 build_append_notify(GArray
*device
, const char *name
,
637 const char *format
, int skip
, int count
)
640 GArray
*method
= build_alloc_array();
641 uint8_t op
= 0x14; /* MethodOp */
643 build_append_nameseg(method
, "%s", name
);
644 build_append_byte(method
, 0x02); /* MethodFlags: ArgCount */
645 for (i
= skip
; i
< count
; i
++) {
646 GArray
*target
= build_alloc_array();
647 build_append_nameseg(target
, format
, i
);
648 assert(i
< 256); /* Fits in 1 byte */
649 build_append_notify_target(method
, target
, i
, 1);
650 build_free_array(target
);
652 build_package(method
, op
, 2);
654 build_append_array(device
, method
);
655 build_free_array(method
);
658 static void patch_pcihp(int slot
, uint8_t *ssdt_ptr
, uint32_t eject
)
660 ssdt_ptr
[ACPI_PCIHP_OFFSET_HEX
] = acpi_get_hex(slot
>> 4);
661 ssdt_ptr
[ACPI_PCIHP_OFFSET_HEX
+ 1] = acpi_get_hex(slot
);
662 ssdt_ptr
[ACPI_PCIHP_OFFSET_ID
] = slot
;
663 ssdt_ptr
[ACPI_PCIHP_OFFSET_ADR
+ 2] = slot
;
665 /* Runtime patching of ACPI_EJ0: to disable hotplug for a slot,
666 * replace the method name: _EJ0 by ACPI_EJ0_.
669 assert(!memcmp(ssdt_ptr
+ ACPI_PCIHP_OFFSET_EJ0
, "_EJ0", 4));
672 memcpy(ssdt_ptr
+ ACPI_PCIHP_OFFSET_EJ0
, "EJ0_", 4);
676 static void patch_pci_windows(PcPciInfo
*pci
, uint8_t *start
, unsigned size
)
678 *ACPI_BUILD_PTR(start
, size
, acpi_pci32_start
[0], uint32_t) =
679 cpu_to_le32(pci
->w32
.begin
);
681 *ACPI_BUILD_PTR(start
, size
, acpi_pci32_end
[0], uint32_t) =
682 cpu_to_le32(pci
->w32
.end
- 1);
684 if (pci
->w64
.end
|| pci
->w64
.begin
) {
685 *ACPI_BUILD_PTR(start
, size
, acpi_pci64_valid
[0], uint8_t) = 1;
686 *ACPI_BUILD_PTR(start
, size
, acpi_pci64_start
[0], uint64_t) =
687 cpu_to_le64(pci
->w64
.begin
);
688 *ACPI_BUILD_PTR(start
, size
, acpi_pci64_end
[0], uint64_t) =
689 cpu_to_le64(pci
->w64
.end
- 1);
690 *ACPI_BUILD_PTR(start
, size
, acpi_pci64_length
[0], uint64_t) =
691 cpu_to_le64(pci
->w64
.end
- pci
->w64
.begin
);
693 *ACPI_BUILD_PTR(start
, size
, acpi_pci64_valid
[0], uint8_t) = 0;
698 build_ssdt(GArray
*table_data
, GArray
*linker
,
699 AcpiCpuInfo
*cpu
, AcpiPmInfo
*pm
, AcpiMiscInfo
*misc
,
700 PcPciInfo
*pci
, PcGuestInfo
*guest_info
)
702 int acpi_cpus
= MIN(0xff, guest_info
->apic_id_limit
);
703 int ssdt_start
= table_data
->len
;
707 /* Copy header and patch values in the S3_ / S4_ / S5_ packages */
708 ssdt_ptr
= acpi_data_push(table_data
, sizeof(ssdp_misc_aml
));
709 memcpy(ssdt_ptr
, ssdp_misc_aml
, sizeof(ssdp_misc_aml
));
710 if (pm
->s3_disabled
) {
711 ssdt_ptr
[acpi_s3_name
[0]] = 'X';
713 if (pm
->s4_disabled
) {
714 ssdt_ptr
[acpi_s4_name
[0]] = 'X';
716 ssdt_ptr
[acpi_s4_pkg
[0] + 1] = ssdt_ptr
[acpi_s4_pkg
[0] + 3] =
720 patch_pci_windows(pci
, ssdt_ptr
, sizeof(ssdp_misc_aml
));
722 *(uint16_t *)(ssdt_ptr
+ *ssdt_isa_pest
) =
723 cpu_to_le16(misc
->pvpanic_port
);
726 GArray
*sb_scope
= build_alloc_array();
727 uint8_t op
= 0x10; /* ScopeOp */
729 build_append_nameseg(sb_scope
, "_SB_");
731 /* build Processor object for each processor */
732 for (i
= 0; i
< acpi_cpus
; i
++) {
733 uint8_t *proc
= acpi_data_push(sb_scope
, ACPI_PROC_SIZEOF
);
734 memcpy(proc
, ACPI_PROC_AML
, ACPI_PROC_SIZEOF
);
735 proc
[ACPI_PROC_OFFSET_CPUHEX
] = acpi_get_hex(i
>> 4);
736 proc
[ACPI_PROC_OFFSET_CPUHEX
+1] = acpi_get_hex(i
);
737 proc
[ACPI_PROC_OFFSET_CPUID1
] = i
;
738 proc
[ACPI_PROC_OFFSET_CPUID2
] = i
;
742 * Method(NTFY, 2) {If (LEqual(Arg0, 0x00)) {Notify(CP00, Arg1)} ...}
744 /* Arg0 = Processor ID = APIC ID */
745 build_append_notify(sb_scope
, "NTFY", "CP%0.02X", 0, acpi_cpus
);
747 /* build "Name(CPON, Package() { One, One, ..., Zero, Zero, ... })" */
748 build_append_byte(sb_scope
, 0x08); /* NameOp */
749 build_append_nameseg(sb_scope
, "CPON");
752 GArray
*package
= build_alloc_array();
753 uint8_t op
= 0x12; /* PackageOp */
755 build_append_byte(package
, acpi_cpus
); /* NumElements */
756 for (i
= 0; i
< acpi_cpus
; i
++) {
757 uint8_t b
= test_bit(i
, cpu
->found_cpus
) ? 0x01 : 0x00;
758 build_append_byte(package
, b
);
761 build_package(package
, op
, 2);
762 build_append_array(sb_scope
, package
);
763 build_free_array(package
);
767 GArray
*pci0
= build_alloc_array();
768 uint8_t op
= 0x10; /* ScopeOp */;
770 build_append_nameseg(pci0
, "PCI0");
772 /* build Device object for each slot */
773 for (i
= 1; i
< PCI_SLOT_MAX
; i
++) {
774 bool eject
= test_bit(i
, misc
->slot_hotplug_enable
);
775 void *pcihp
= acpi_data_push(pci0
, ACPI_PCIHP_SIZEOF
);
777 memcpy(pcihp
, ACPI_PCIHP_AML
, ACPI_PCIHP_SIZEOF
);
778 patch_pcihp(i
, pcihp
, eject
);
781 build_append_notify(pci0
, "PCNT", "S%0.02X_", 1, PCI_SLOT_MAX
);
782 build_package(pci0
, op
, 3);
783 build_append_array(sb_scope
, pci0
);
784 build_free_array(pci0
);
787 build_package(sb_scope
, op
, 3);
788 build_append_array(table_data
, sb_scope
);
789 build_free_array(sb_scope
);
792 build_header(linker
, table_data
,
793 (void *)(table_data
->data
+ ssdt_start
),
794 ACPI_SSDT_SIGNATURE
, table_data
->len
- ssdt_start
, 1);
798 build_hpet(GArray
*table_data
, GArray
*linker
)
802 hpet
= acpi_data_push(table_data
, sizeof(*hpet
));
803 /* Note timer_block_id value must be kept in sync with value advertised by
806 hpet
->timer_block_id
= cpu_to_le32(0x8086a201);
807 hpet
->addr
.address
= cpu_to_le64(HPET_BASE
);
808 build_header(linker
, table_data
,
809 (void *)hpet
, ACPI_HPET_SIGNATURE
, sizeof(*hpet
), 1);
813 acpi_build_srat_memory(AcpiSratMemoryAffinity
*numamem
,
814 uint64_t base
, uint64_t len
, int node
, int enabled
)
816 numamem
->type
= ACPI_SRAT_MEMORY
;
817 numamem
->length
= sizeof(*numamem
);
818 memset(numamem
->proximity
, 0, 4);
819 numamem
->proximity
[0] = node
;
820 numamem
->flags
= cpu_to_le32(!!enabled
);
821 numamem
->base_addr
= cpu_to_le64(base
);
822 numamem
->range_length
= cpu_to_le64(len
);
826 build_srat(GArray
*table_data
, GArray
*linker
,
827 AcpiCpuInfo
*cpu
, PcGuestInfo
*guest_info
)
829 AcpiSystemResourceAffinityTable
*srat
;
830 AcpiSratProcessorAffinity
*core
;
831 AcpiSratMemoryAffinity
*numamem
;
835 int srat_start
, numa_start
, slots
;
836 uint64_t mem_len
, mem_base
, next_base
;
838 srat_start
= table_data
->len
;
840 srat
= acpi_data_push(table_data
, sizeof *srat
);
841 srat
->reserved1
= cpu_to_le32(1);
842 core
= (void *)(srat
+ 1);
844 for (i
= 0; i
< guest_info
->apic_id_limit
; ++i
) {
845 core
= acpi_data_push(table_data
, sizeof *core
);
846 core
->type
= ACPI_SRAT_PROCESSOR
;
847 core
->length
= sizeof(*core
);
848 core
->local_apic_id
= i
;
849 curnode
= guest_info
->node_cpu
[i
];
850 core
->proximity_lo
= curnode
;
851 memset(core
->proximity_hi
, 0, 3);
852 core
->local_sapic_eid
= 0;
853 if (test_bit(i
, cpu
->found_cpus
)) {
854 core
->flags
= cpu_to_le32(1);
856 core
->flags
= cpu_to_le32(0);
861 /* the memory map is a bit tricky, it contains at least one hole
862 * from 640k-1M and possibly another one from 3.5G-4G.
865 numa_start
= table_data
->len
;
867 numamem
= acpi_data_push(table_data
, sizeof *numamem
);
868 acpi_build_srat_memory(numamem
, 0, 640*1024, 0, 1);
869 next_base
= 1024 * 1024;
870 for (i
= 1; i
< guest_info
->numa_nodes
+ 1; ++i
) {
871 mem_base
= next_base
;
872 mem_len
= guest_info
->node_mem
[i
- 1];
874 mem_len
-= 1024 * 1024;
876 next_base
= mem_base
+ mem_len
;
878 /* Cut out the ACPI_PCI hole */
879 if (mem_base
<= guest_info
->ram_size
&&
880 next_base
> guest_info
->ram_size
) {
881 mem_len
-= next_base
- guest_info
->ram_size
;
883 numamem
= acpi_data_push(table_data
, sizeof *numamem
);
884 acpi_build_srat_memory(numamem
, mem_base
, mem_len
, i
-1, 1);
886 mem_base
= 1ULL << 32;
887 mem_len
= next_base
- guest_info
->ram_size
;
888 next_base
+= (1ULL << 32) - guest_info
->ram_size
;
890 numamem
= acpi_data_push(table_data
, sizeof *numamem
);
891 acpi_build_srat_memory(numamem
, mem_base
, mem_len
, i
- 1, 1);
893 slots
= (table_data
->len
- numa_start
) / sizeof *numamem
;
894 for (; slots
< guest_info
->numa_nodes
+ 2; slots
++) {
895 numamem
= acpi_data_push(table_data
, sizeof *numamem
);
896 acpi_build_srat_memory(numamem
, 0, 0, 0, 0);
899 build_header(linker
, table_data
,
900 (void *)(table_data
->data
+ srat_start
),
902 table_data
->len
- srat_start
, 1);
906 build_mcfg_q35(GArray
*table_data
, GArray
*linker
, AcpiMcfgInfo
*info
)
910 int len
= sizeof(*mcfg
) + 1 * sizeof(mcfg
->allocation
[0]);
912 mcfg
= acpi_data_push(table_data
, len
);
913 mcfg
->allocation
[0].address
= cpu_to_le64(info
->mcfg_base
);
914 /* Only a single allocation so no need to play with segments */
915 mcfg
->allocation
[0].pci_segment
= cpu_to_le16(0);
916 mcfg
->allocation
[0].start_bus_number
= 0;
917 mcfg
->allocation
[0].end_bus_number
= PCIE_MMCFG_BUS(info
->mcfg_size
- 1);
919 /* MCFG is used for ECAM which can be enabled or disabled by guest.
920 * To avoid table size changes (which create migration issues),
921 * always create the table even if there are no allocations,
922 * but set the signature to a reserved value in this case.
923 * ACPI spec requires OSPMs to ignore such tables.
925 if (info
->mcfg_base
== PCIE_BASE_ADDR_UNMAPPED
) {
926 sig
= ACPI_RSRV_SIGNATURE
;
928 sig
= ACPI_MCFG_SIGNATURE
;
930 build_header(linker
, table_data
, (void *)mcfg
, sig
, len
, 1);
934 build_dsdt(GArray
*table_data
, GArray
*linker
, AcpiMiscInfo
*misc
)
936 AcpiTableHeader
*dsdt
;
938 assert(misc
->dsdt_code
&& misc
->dsdt_size
);
940 dsdt
= acpi_data_push(table_data
, misc
->dsdt_size
);
941 memcpy(dsdt
, misc
->dsdt_code
, misc
->dsdt_size
);
943 memset(dsdt
, 0, sizeof *dsdt
);
944 build_header(linker
, table_data
, dsdt
, ACPI_DSDT_SIGNATURE
,
948 /* Build final rsdt table */
950 build_rsdt(GArray
*table_data
, GArray
*linker
, GArray
*table_offsets
)
952 AcpiRsdtDescriptorRev1
*rsdt
;
956 rsdt_len
= sizeof(*rsdt
) + sizeof(uint32_t) * table_offsets
->len
;
957 rsdt
= acpi_data_push(table_data
, rsdt_len
);
958 memcpy(rsdt
->table_offset_entry
, table_offsets
->data
,
959 sizeof(uint32_t) * table_offsets
->len
);
960 for (i
= 0; i
< table_offsets
->len
; ++i
) {
961 /* rsdt->table_offset_entry to be filled by Guest linker */
962 bios_linker_loader_add_pointer(linker
,
963 ACPI_BUILD_TABLE_FILE
,
964 ACPI_BUILD_TABLE_FILE
,
965 table_data
, &rsdt
->table_offset_entry
[i
],
968 build_header(linker
, table_data
,
969 (void *)rsdt
, ACPI_RSDT_SIGNATURE
, rsdt_len
, 1);
973 build_rsdp(GArray
*rsdp_table
, GArray
*linker
, unsigned rsdt
)
975 AcpiRsdpDescriptor
*rsdp
= acpi_data_push(rsdp_table
, sizeof *rsdp
);
977 bios_linker_loader_alloc(linker
, ACPI_BUILD_RSDP_FILE
, 1,
978 true /* fseg memory */);
980 rsdp
->signature
= cpu_to_le64(ACPI_RSDP_SIGNATURE
);
981 memcpy(rsdp
->oem_id
, ACPI_BUILD_APPNAME6
, 6);
982 rsdp
->rsdt_physical_address
= cpu_to_le32(rsdt
);
983 /* Address to be filled by Guest linker */
984 bios_linker_loader_add_pointer(linker
, ACPI_BUILD_RSDP_FILE
,
985 ACPI_BUILD_TABLE_FILE
,
986 rsdp_table
, &rsdp
->rsdt_physical_address
,
987 sizeof rsdp
->rsdt_physical_address
);
989 /* Checksum to be filled by Guest linker */
990 bios_linker_loader_add_checksum(linker
, ACPI_BUILD_RSDP_FILE
,
991 rsdp
, rsdp
, sizeof *rsdp
, &rsdp
->checksum
);
997 struct AcpiBuildTables
{
1003 static inline void acpi_build_tables_init(AcpiBuildTables
*tables
)
1005 tables
->rsdp
= g_array_new(false, true /* clear */, 1);
1006 tables
->table_data
= g_array_new(false, true /* clear */, 1);
1007 tables
->linker
= bios_linker_loader_init();
1010 static inline void acpi_build_tables_cleanup(AcpiBuildTables
*tables
, bool mfre
)
1012 void *linker_data
= bios_linker_loader_cleanup(tables
->linker
);
1014 g_free(linker_data
);
1016 g_array_free(tables
->rsdp
, mfre
);
1017 g_array_free(tables
->table_data
, mfre
);
1021 struct AcpiBuildState
{
1022 /* Copy of table in RAM (for patching). */
1024 uint32_t table_size
;
1025 /* Is table patched? */
1027 PcGuestInfo
*guest_info
;
1030 static bool acpi_get_mcfg(AcpiMcfgInfo
*mcfg
)
1036 pci_host
= object_resolve_path_type("", TYPE_PCI_HOST_BRIDGE
, &ambiguous
);
1037 g_assert(!ambiguous
);
1040 o
= object_property_get_qobject(pci_host
, PCIE_HOST_MCFG_BASE
, NULL
);
1044 mcfg
->mcfg_base
= qint_get_int(qobject_to_qint(o
));
1046 o
= object_property_get_qobject(pci_host
, PCIE_HOST_MCFG_SIZE
, NULL
);
1048 mcfg
->mcfg_size
= qint_get_int(qobject_to_qint(o
));
1053 void acpi_build(PcGuestInfo
*guest_info
, AcpiBuildTables
*tables
)
1055 GArray
*table_offsets
;
1056 unsigned facs
, dsdt
, rsdt
;
1064 acpi_get_cpu_info(&cpu
);
1065 acpi_get_pm_info(&pm
);
1066 acpi_get_dsdt(&misc
);
1067 acpi_get_hotplug_info(&misc
);
1068 acpi_get_misc_info(&misc
);
1069 acpi_get_pci_info(&pci
);
1071 table_offsets
= g_array_new(false, true /* clear */,
1073 ACPI_BUILD_DPRINTF(3, "init ACPI tables\n");
1075 bios_linker_loader_alloc(tables
->linker
, ACPI_BUILD_TABLE_FILE
,
1076 64 /* Ensure FACS is aligned */,
1077 false /* high memory */);
1080 * FACS is pointed to by FADT.
1081 * We place it first since it's the only table that has alignment
1084 facs
= tables
->table_data
->len
;
1085 build_facs(tables
->table_data
, tables
->linker
, guest_info
);
1087 /* DSDT is pointed to by FADT */
1088 dsdt
= tables
->table_data
->len
;
1089 build_dsdt(tables
->table_data
, tables
->linker
, &misc
);
1091 /* ACPI tables pointed to by RSDT */
1092 acpi_add_table(table_offsets
, tables
->table_data
);
1093 build_fadt(tables
->table_data
, tables
->linker
, &pm
, facs
, dsdt
);
1094 acpi_add_table(table_offsets
, tables
->table_data
);
1096 build_ssdt(tables
->table_data
, tables
->linker
, &cpu
, &pm
, &misc
, &pci
,
1098 acpi_add_table(table_offsets
, tables
->table_data
);
1100 build_madt(tables
->table_data
, tables
->linker
, &cpu
, guest_info
);
1101 acpi_add_table(table_offsets
, tables
->table_data
);
1102 if (misc
.has_hpet
) {
1103 build_hpet(tables
->table_data
, tables
->linker
);
1105 if (guest_info
->numa_nodes
) {
1106 acpi_add_table(table_offsets
, tables
->table_data
);
1107 build_srat(tables
->table_data
, tables
->linker
, &cpu
, guest_info
);
1109 if (acpi_get_mcfg(&mcfg
)) {
1110 acpi_add_table(table_offsets
, tables
->table_data
);
1111 build_mcfg_q35(tables
->table_data
, tables
->linker
, &mcfg
);
1114 /* Add tables supplied by user (if any) */
1115 for (u
= acpi_table_first(); u
; u
= acpi_table_next(u
)) {
1116 unsigned len
= acpi_table_len(u
);
1118 acpi_add_table(table_offsets
, tables
->table_data
);
1119 g_array_append_vals(tables
->table_data
, u
, len
);
1122 /* RSDT is pointed to by RSDP */
1123 rsdt
= tables
->table_data
->len
;
1124 build_rsdt(tables
->table_data
, tables
->linker
, table_offsets
);
1126 /* RSDP is in FSEG memory, so allocate it separately */
1127 build_rsdp(tables
->rsdp
, tables
->linker
, rsdt
);
1129 /* We'll expose it all to Guest so align size to reduce
1130 * chance of size changes.
1131 * RSDP is small so it's easy to keep it immutable, no need to
1132 * bother with alignment.
1134 acpi_align_size(tables
->table_data
, 0x1000);
1136 acpi_align_size(tables
->linker
, 0x1000);
1138 /* Cleanup memory that's no longer used. */
1139 g_array_free(table_offsets
, true);
1142 static void acpi_build_update(void *build_opaque
, uint32_t offset
)
1144 AcpiBuildState
*build_state
= build_opaque
;
1145 AcpiBuildTables tables
;
1147 /* No state to update or already patched? Nothing to do. */
1148 if (!build_state
|| build_state
->patched
) {
1151 build_state
->patched
= 1;
1153 acpi_build_tables_init(&tables
);
1155 acpi_build(build_state
->guest_info
, &tables
);
1157 assert(acpi_data_len(tables
.table_data
) == build_state
->table_size
);
1158 memcpy(build_state
->table_ram
, tables
.table_data
->data
,
1159 build_state
->table_size
);
1161 acpi_build_tables_cleanup(&tables
, true);
1164 static void acpi_build_reset(void *build_opaque
)
1166 AcpiBuildState
*build_state
= build_opaque
;
1167 build_state
->patched
= 0;
1170 static void *acpi_add_rom_blob(AcpiBuildState
*build_state
, GArray
*blob
,
1173 return rom_add_blob(name
, blob
->data
, acpi_data_len(blob
), -1, name
,
1174 acpi_build_update
, build_state
);
1177 static const VMStateDescription vmstate_acpi_build
= {
1178 .name
= "acpi_build",
1180 .minimum_version_id
= 1,
1181 .minimum_version_id_old
= 1,
1182 .fields
= (VMStateField
[]) {
1183 VMSTATE_UINT8(patched
, AcpiBuildState
),
1184 VMSTATE_END_OF_LIST()
1188 void acpi_setup(PcGuestInfo
*guest_info
)
1190 AcpiBuildTables tables
;
1191 AcpiBuildState
*build_state
;
1193 if (!guest_info
->fw_cfg
) {
1194 ACPI_BUILD_DPRINTF(3, "No fw cfg. Bailing out.\n");
1198 if (!guest_info
->has_acpi_build
) {
1199 ACPI_BUILD_DPRINTF(3, "ACPI build disabled. Bailing out.\n");
1203 if (!acpi_enabled
) {
1204 ACPI_BUILD_DPRINTF(3, "ACPI disabled. Bailing out.\n");
1208 build_state
= g_malloc0(sizeof *build_state
);
1210 build_state
->guest_info
= guest_info
;
1212 acpi_build_tables_init(&tables
);
1213 acpi_build(build_state
->guest_info
, &tables
);
1215 /* Now expose it all to Guest */
1216 build_state
->table_ram
= acpi_add_rom_blob(build_state
, tables
.table_data
,
1217 ACPI_BUILD_TABLE_FILE
);
1218 build_state
->table_size
= acpi_data_len(tables
.table_data
);
1220 acpi_add_rom_blob(NULL
, tables
.linker
, "etc/table-loader");
1223 * RSDP is small so it's easy to keep it immutable, no need to
1224 * bother with ROM blobs.
1226 fw_cfg_add_file(guest_info
->fw_cfg
, ACPI_BUILD_RSDP_FILE
,
1227 tables
.rsdp
->data
, acpi_data_len(tables
.rsdp
));
1229 qemu_register_reset(acpi_build_reset
, build_state
);
1230 acpi_build_reset(build_state
);
1231 vmstate_register(NULL
, 0, &vmstate_acpi_build
, build_state
);
1233 /* Cleanup tables but don't free the memory: we track it
1236 acpi_build_tables_cleanup(&tables
, false);