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"
40 /* Supported chipsets: */
41 #include "hw/acpi/piix4.h"
42 #include "hw/i386/ich9.h"
43 #include "hw/pci/pci_bus.h"
44 #include "hw/pci-host/q35.h"
46 #include "hw/i386/q35-acpi-dsdt.hex"
47 #include "hw/i386/acpi-dsdt.hex"
49 #include "qapi/qmp/qint.h"
50 #include "qom/qom-qobject.h"
52 typedef struct AcpiCpuInfo
{
53 DECLARE_BITMAP(found_cpus
, MAX_CPUMASK_BITS
+ 1);
56 typedef struct AcpiMcfgInfo
{
61 typedef struct AcpiPmInfo
{
66 uint8_t acpi_enable_cmd
;
67 uint8_t acpi_disable_cmd
;
69 uint32_t gpe0_blk_len
;
73 typedef struct AcpiMiscInfo
{
75 DECLARE_BITMAP(slot_hotplug_enable
, PCI_SLOT_MAX
);
76 const unsigned char *dsdt_code
;
78 uint16_t pvpanic_port
;
81 static void acpi_get_dsdt(AcpiMiscInfo
*info
)
83 Object
*piix
= piix4_pm_find();
84 Object
*lpc
= ich9_lpc_find();
85 assert(!!piix
!= !!lpc
);
88 info
->dsdt_code
= AcpiDsdtAmlCode
;
89 info
->dsdt_size
= sizeof AcpiDsdtAmlCode
;
92 info
->dsdt_code
= Q35AcpiDsdtAmlCode
;
93 info
->dsdt_size
= sizeof Q35AcpiDsdtAmlCode
;
98 int acpi_add_cpu_info(Object
*o
, void *opaque
)
100 AcpiCpuInfo
*cpu
= opaque
;
103 if (object_dynamic_cast(o
, TYPE_CPU
)) {
104 apic_id
= object_property_get_int(o
, "apic-id", NULL
);
105 assert(apic_id
<= MAX_CPUMASK_BITS
);
107 set_bit(apic_id
, cpu
->found_cpus
);
110 object_child_foreach(o
, acpi_add_cpu_info
, opaque
);
114 static void acpi_get_cpu_info(AcpiCpuInfo
*cpu
)
116 Object
*root
= object_get_root();
118 memset(cpu
->found_cpus
, 0, sizeof cpu
->found_cpus
);
119 object_child_foreach(root
, acpi_add_cpu_info
, cpu
);
122 static void acpi_get_pm_info(AcpiPmInfo
*pm
)
124 Object
*piix
= piix4_pm_find();
125 Object
*lpc
= ich9_lpc_find();
137 /* Fill in optional s3/s4 related properties */
138 o
= object_property_get_qobject(obj
, ACPI_PM_PROP_S3_DISABLED
, NULL
);
140 pm
->s3_disabled
= qint_get_int(qobject_to_qint(o
));
142 pm
->s3_disabled
= false;
144 o
= object_property_get_qobject(obj
, ACPI_PM_PROP_S4_DISABLED
, NULL
);
146 pm
->s4_disabled
= qint_get_int(qobject_to_qint(o
));
148 pm
->s4_disabled
= false;
150 o
= object_property_get_qobject(obj
, ACPI_PM_PROP_S4_VAL
, NULL
);
152 pm
->s4_val
= qint_get_int(qobject_to_qint(o
));
157 /* Fill in mandatory properties */
158 pm
->sci_int
= object_property_get_int(obj
, ACPI_PM_PROP_SCI_INT
, NULL
);
160 pm
->acpi_enable_cmd
= object_property_get_int(obj
,
161 ACPI_PM_PROP_ACPI_ENABLE_CMD
,
163 pm
->acpi_disable_cmd
= object_property_get_int(obj
,
164 ACPI_PM_PROP_ACPI_DISABLE_CMD
,
166 pm
->io_base
= object_property_get_int(obj
, ACPI_PM_PROP_PM_IO_BASE
,
168 pm
->gpe0_blk
= object_property_get_int(obj
, ACPI_PM_PROP_GPE0_BLK
,
170 pm
->gpe0_blk_len
= object_property_get_int(obj
, ACPI_PM_PROP_GPE0_BLK_LEN
,
174 static void acpi_get_hotplug_info(AcpiMiscInfo
*misc
)
177 PCIBus
*bus
= find_i440fx();
180 /* Only PIIX supports ACPI hotplug */
181 memset(misc
->slot_hotplug_enable
, 0, sizeof misc
->slot_hotplug_enable
);
185 memset(misc
->slot_hotplug_enable
, 0xff,
186 DIV_ROUND_UP(PCI_SLOT_MAX
, BITS_PER_BYTE
));
188 for (i
= 0; i
< ARRAY_SIZE(bus
->devices
); ++i
) {
190 PCIDevice
*pdev
= bus
->devices
[i
];
196 pc
= PCI_DEVICE_GET_CLASS(pdev
);
198 if (pc
->no_hotplug
) {
199 int slot
= PCI_SLOT(i
);
201 clear_bit(slot
, misc
->slot_hotplug_enable
);
206 static void acpi_get_misc_info(AcpiMiscInfo
*info
)
208 info
->has_hpet
= hpet_find();
209 info
->pvpanic_port
= pvpanic_port();
212 static void acpi_get_pci_info(PcPciInfo
*info
)
217 pci_host
= object_resolve_path_type("", TYPE_PCI_HOST_BRIDGE
, &ambiguous
);
218 g_assert(!ambiguous
);
221 info
->w32
.begin
= object_property_get_int(pci_host
,
222 PCI_HOST_PROP_PCI_HOLE_START
,
224 info
->w32
.end
= object_property_get_int(pci_host
,
225 PCI_HOST_PROP_PCI_HOLE_END
,
227 info
->w64
.begin
= object_property_get_int(pci_host
,
228 PCI_HOST_PROP_PCI_HOLE64_START
,
230 info
->w64
.end
= object_property_get_int(pci_host
,
231 PCI_HOST_PROP_PCI_HOLE64_END
,
235 #define ACPI_BUILD_APPNAME "Bochs"
236 #define ACPI_BUILD_APPNAME6 "BOCHS "
237 #define ACPI_BUILD_APPNAME4 "BXPC"
239 #define ACPI_BUILD_DPRINTF(level, fmt, ...) do {} while (0)
241 #define ACPI_BUILD_TABLE_FILE "etc/acpi/tables"
242 #define ACPI_BUILD_RSDP_FILE "etc/acpi/rsdp"
245 build_header(GArray
*linker
, GArray
*table_data
,
246 AcpiTableHeader
*h
, uint32_t sig
, int len
, uint8_t rev
)
248 h
->signature
= cpu_to_le32(sig
);
249 h
->length
= cpu_to_le32(len
);
251 memcpy(h
->oem_id
, ACPI_BUILD_APPNAME6
, 6);
252 memcpy(h
->oem_table_id
, ACPI_BUILD_APPNAME4
, 4);
253 memcpy(h
->oem_table_id
+ 4, (void *)&sig
, 4);
254 h
->oem_revision
= cpu_to_le32(1);
255 memcpy(h
->asl_compiler_id
, ACPI_BUILD_APPNAME4
, 4);
256 h
->asl_compiler_revision
= cpu_to_le32(1);
258 /* Checksum to be filled in by Guest linker */
259 bios_linker_loader_add_checksum(linker
, ACPI_BUILD_TABLE_FILE
,
260 table_data
->data
, h
, len
, &h
->checksum
);
263 static inline GArray
*build_alloc_array(void)
265 return g_array_new(false, true /* clear */, 1);
268 static inline void build_free_array(GArray
*array
)
270 g_array_free(array
, true);
273 static inline void build_prepend_byte(GArray
*array
, uint8_t val
)
275 g_array_prepend_val(array
, val
);
278 static inline void build_append_byte(GArray
*array
, uint8_t val
)
280 g_array_append_val(array
, val
);
283 static inline void build_append_array(GArray
*array
, GArray
*val
)
285 g_array_append_vals(array
, val
->data
, val
->len
);
288 static void build_append_nameseg(GArray
*array
, const char *format
, ...)
290 /* It would be nicer to use g_string_vprintf but it's only there in 2.22 */
295 va_start(args
, format
);
296 len
= vsnprintf(s
, sizeof s
, format
, args
);
300 g_array_append_vals(array
, s
, len
);
303 /* 5.4 Definition Block Encoding */
305 PACKAGE_LENGTH_1BYTE_SHIFT
= 6, /* Up to 63 - use extra 2 bits. */
306 PACKAGE_LENGTH_2BYTE_SHIFT
= 4,
307 PACKAGE_LENGTH_3BYTE_SHIFT
= 12,
308 PACKAGE_LENGTH_4BYTE_SHIFT
= 20,
311 static void build_prepend_package_length(GArray
*package
, unsigned min_bytes
)
314 unsigned length
= package
->len
;
315 unsigned length_bytes
;
317 if (length
+ 1 < (1 << PACKAGE_LENGTH_1BYTE_SHIFT
)) {
319 } else if (length
+ 2 < (1 << PACKAGE_LENGTH_3BYTE_SHIFT
)) {
321 } else if (length
+ 3 < (1 << PACKAGE_LENGTH_4BYTE_SHIFT
)) {
327 /* Force length to at least min_bytes.
328 * This wastes memory but that's how bios did it.
330 length_bytes
= MAX(length_bytes
, min_bytes
);
332 /* PkgLength is the length of the inclusive length of the data. */
333 length
+= length_bytes
;
335 switch (length_bytes
) {
338 build_prepend_byte(package
, byte
);
341 byte
= length
>> PACKAGE_LENGTH_4BYTE_SHIFT
;
342 build_prepend_byte(package
, byte
);
343 length
&= (1 << PACKAGE_LENGTH_4BYTE_SHIFT
) - 1;
346 byte
= length
>> PACKAGE_LENGTH_3BYTE_SHIFT
;
347 build_prepend_byte(package
, byte
);
348 length
&= (1 << PACKAGE_LENGTH_3BYTE_SHIFT
) - 1;
351 byte
= length
>> PACKAGE_LENGTH_2BYTE_SHIFT
;
352 build_prepend_byte(package
, byte
);
353 length
&= (1 << PACKAGE_LENGTH_2BYTE_SHIFT
) - 1;
357 * Most significant two bits of byte zero indicate how many following bytes
358 * are in PkgLength encoding.
360 byte
= ((length_bytes
- 1) << PACKAGE_LENGTH_1BYTE_SHIFT
) | length
;
361 build_prepend_byte(package
, byte
);
364 static void build_package(GArray
*package
, uint8_t op
, unsigned min_bytes
)
366 build_prepend_package_length(package
, min_bytes
);
367 build_prepend_byte(package
, op
);
370 static void build_append_value(GArray
*table
, uint32_t value
, int size
)
377 prefix
= 0x0A; /* BytePrefix */
380 prefix
= 0x0B; /* WordPrefix */
383 prefix
= 0x0C; /* DWordPrefix */
389 build_append_byte(table
, prefix
);
390 for (i
= 0; i
< size
; ++i
) {
391 build_append_byte(table
, value
& 0xFF);
396 static void build_append_notify_target(GArray
*method
, GArray
*target_name
,
397 uint32_t value
, int size
)
399 GArray
*notify
= build_alloc_array();
400 uint8_t op
= 0xA0; /* IfOp */
402 build_append_byte(notify
, 0x93); /* LEqualOp */
403 build_append_byte(notify
, 0x68); /* Arg0Op */
404 build_append_value(notify
, value
, size
);
405 build_append_byte(notify
, 0x86); /* NotifyOp */
406 build_append_array(notify
, target_name
);
407 build_append_byte(notify
, 0x69); /* Arg1Op */
410 build_package(notify
, op
, 1);
412 build_append_array(method
, notify
);
414 build_free_array(notify
);
417 #define ACPI_PORT_SMI_CMD 0x00b2 /* TODO: this is APM_CNT_IOPORT */
419 static inline void *acpi_data_push(GArray
*table_data
, unsigned size
)
421 unsigned off
= table_data
->len
;
422 g_array_set_size(table_data
, off
+ size
);
423 return table_data
->data
+ off
;
426 static unsigned acpi_data_len(GArray
*table
)
428 return table
->len
* g_array_get_element_size(table
);
431 static void acpi_align_size(GArray
*blob
, unsigned align
)
433 /* Align size to multiple of given size. This reduces the chance
434 * we need to change size in the future (breaking cross version migration).
436 g_array_set_size(blob
, (ROUND_UP(acpi_data_len(blob
), align
) +
437 g_array_get_element_size(blob
) - 1) /
438 g_array_get_element_size(blob
));
441 /* Get pointer within table in a safe manner */
442 #define ACPI_BUILD_PTR(table, size, off, type) \
443 ((type *)(acpi_data_get_ptr(table, size, off, sizeof(type))))
445 static inline void *acpi_data_get_ptr(uint8_t *table_data
, unsigned table_size
,
446 unsigned off
, unsigned size
)
448 assert(off
+ size
> off
);
449 assert(off
+ size
<= table_size
);
450 return table_data
+ off
;
453 static inline void acpi_add_table(GArray
*table_offsets
, GArray
*table_data
)
455 uint32_t offset
= cpu_to_le32(table_data
->len
);
456 g_array_append_val(table_offsets
, offset
);
461 build_facs(GArray
*table_data
, GArray
*linker
, PcGuestInfo
*guest_info
)
463 AcpiFacsDescriptorRev1
*facs
= acpi_data_push(table_data
, sizeof *facs
);
464 facs
->signature
= cpu_to_le32(ACPI_FACS_SIGNATURE
);
465 facs
->length
= cpu_to_le32(sizeof(*facs
));
468 /* Load chipset information in FADT */
469 static void fadt_setup(AcpiFadtDescriptorRev1
*fadt
, AcpiPmInfo
*pm
)
473 fadt
->sci_int
= cpu_to_le16(pm
->sci_int
);
474 fadt
->smi_cmd
= cpu_to_le32(ACPI_PORT_SMI_CMD
);
475 fadt
->acpi_enable
= pm
->acpi_enable_cmd
;
476 fadt
->acpi_disable
= pm
->acpi_disable_cmd
;
477 /* EVT, CNT, TMR offset matches hw/acpi/core.c */
478 fadt
->pm1a_evt_blk
= cpu_to_le32(pm
->io_base
);
479 fadt
->pm1a_cnt_blk
= cpu_to_le32(pm
->io_base
+ 0x04);
480 fadt
->pm_tmr_blk
= cpu_to_le32(pm
->io_base
+ 0x08);
481 fadt
->gpe0_blk
= cpu_to_le32(pm
->gpe0_blk
);
482 /* EVT, CNT, TMR length matches hw/acpi/core.c */
483 fadt
->pm1_evt_len
= 4;
484 fadt
->pm1_cnt_len
= 2;
485 fadt
->pm_tmr_len
= 4;
486 fadt
->gpe0_blk_len
= pm
->gpe0_blk_len
;
487 fadt
->plvl2_lat
= cpu_to_le16(0xfff); /* C2 state not supported */
488 fadt
->plvl3_lat
= cpu_to_le16(0xfff); /* C3 state not supported */
489 fadt
->flags
= cpu_to_le32((1 << ACPI_FADT_F_WBINVD
) |
490 (1 << ACPI_FADT_F_PROC_C1
) |
491 (1 << ACPI_FADT_F_SLP_BUTTON
) |
492 (1 << ACPI_FADT_F_RTC_S4
));
493 fadt
->flags
|= cpu_to_le32(1 << ACPI_FADT_F_USE_PLATFORM_CLOCK
);
499 build_fadt(GArray
*table_data
, GArray
*linker
, AcpiPmInfo
*pm
,
500 unsigned facs
, unsigned dsdt
)
502 AcpiFadtDescriptorRev1
*fadt
= acpi_data_push(table_data
, sizeof(*fadt
));
504 fadt
->firmware_ctrl
= cpu_to_le32(facs
);
505 /* FACS address to be filled by Guest linker */
506 bios_linker_loader_add_pointer(linker
, ACPI_BUILD_TABLE_FILE
,
507 ACPI_BUILD_TABLE_FILE
,
508 table_data
, &fadt
->firmware_ctrl
,
509 sizeof fadt
->firmware_ctrl
);
511 fadt
->dsdt
= cpu_to_le32(dsdt
);
512 /* DSDT address to be filled by Guest linker */
513 bios_linker_loader_add_pointer(linker
, ACPI_BUILD_TABLE_FILE
,
514 ACPI_BUILD_TABLE_FILE
,
515 table_data
, &fadt
->dsdt
,
518 fadt_setup(fadt
, pm
);
520 build_header(linker
, table_data
,
521 (void *)fadt
, ACPI_FACP_SIGNATURE
, sizeof(*fadt
), 1);
525 build_madt(GArray
*table_data
, GArray
*linker
, AcpiCpuInfo
*cpu
,
526 PcGuestInfo
*guest_info
)
528 int madt_start
= table_data
->len
;
530 AcpiMultipleApicTable
*madt
;
531 AcpiMadtIoApic
*io_apic
;
532 AcpiMadtIntsrcovr
*intsrcovr
;
533 AcpiMadtLocalNmi
*local_nmi
;
536 madt
= acpi_data_push(table_data
, sizeof *madt
);
537 madt
->local_apic_address
= cpu_to_le32(APIC_DEFAULT_ADDRESS
);
538 madt
->flags
= cpu_to_le32(1);
540 for (i
= 0; i
< guest_info
->apic_id_limit
; i
++) {
541 AcpiMadtProcessorApic
*apic
= acpi_data_push(table_data
, sizeof *apic
);
542 apic
->type
= ACPI_APIC_PROCESSOR
;
543 apic
->length
= sizeof(*apic
);
544 apic
->processor_id
= i
;
545 apic
->local_apic_id
= i
;
546 if (test_bit(i
, cpu
->found_cpus
)) {
547 apic
->flags
= cpu_to_le32(1);
549 apic
->flags
= cpu_to_le32(0);
552 io_apic
= acpi_data_push(table_data
, sizeof *io_apic
);
553 io_apic
->type
= ACPI_APIC_IO
;
554 io_apic
->length
= sizeof(*io_apic
);
555 #define ACPI_BUILD_IOAPIC_ID 0x0
556 io_apic
->io_apic_id
= ACPI_BUILD_IOAPIC_ID
;
557 io_apic
->address
= cpu_to_le32(IO_APIC_DEFAULT_ADDRESS
);
558 io_apic
->interrupt
= cpu_to_le32(0);
560 if (guest_info
->apic_xrupt_override
) {
561 intsrcovr
= acpi_data_push(table_data
, sizeof *intsrcovr
);
562 intsrcovr
->type
= ACPI_APIC_XRUPT_OVERRIDE
;
563 intsrcovr
->length
= sizeof(*intsrcovr
);
564 intsrcovr
->source
= 0;
565 intsrcovr
->gsi
= cpu_to_le32(2);
566 intsrcovr
->flags
= cpu_to_le16(0); /* conforms to bus specifications */
568 for (i
= 1; i
< 16; i
++) {
569 #define ACPI_BUILD_PCI_IRQS ((1<<5) | (1<<9) | (1<<10) | (1<<11))
570 if (!(ACPI_BUILD_PCI_IRQS
& (1 << i
))) {
571 /* No need for a INT source override structure. */
574 intsrcovr
= acpi_data_push(table_data
, sizeof *intsrcovr
);
575 intsrcovr
->type
= ACPI_APIC_XRUPT_OVERRIDE
;
576 intsrcovr
->length
= sizeof(*intsrcovr
);
577 intsrcovr
->source
= i
;
578 intsrcovr
->gsi
= cpu_to_le32(i
);
579 intsrcovr
->flags
= cpu_to_le16(0xd); /* active high, level triggered */
582 local_nmi
= acpi_data_push(table_data
, sizeof *local_nmi
);
583 local_nmi
->type
= ACPI_APIC_LOCAL_NMI
;
584 local_nmi
->length
= sizeof(*local_nmi
);
585 local_nmi
->processor_id
= 0xff; /* all processors */
586 local_nmi
->flags
= cpu_to_le16(0);
587 local_nmi
->lint
= 1; /* ACPI_LINT1 */
589 build_header(linker
, table_data
,
590 (void *)(table_data
->data
+ madt_start
), ACPI_APIC_SIGNATURE
,
591 table_data
->len
- madt_start
, 1);
594 /* Encode a hex value */
595 static inline char acpi_get_hex(uint32_t val
)
598 return (val
<= 9) ? ('0' + val
) : ('A' + val
- 10);
601 #include "hw/i386/ssdt-proc.hex"
603 /* 0x5B 0x83 ProcessorOp PkgLength NameString ProcID */
604 #define ACPI_PROC_OFFSET_CPUHEX (*ssdt_proc_name - *ssdt_proc_start + 2)
605 #define ACPI_PROC_OFFSET_CPUID1 (*ssdt_proc_name - *ssdt_proc_start + 4)
606 #define ACPI_PROC_OFFSET_CPUID2 (*ssdt_proc_id - *ssdt_proc_start)
607 #define ACPI_PROC_SIZEOF (*ssdt_proc_end - *ssdt_proc_start)
608 #define ACPI_PROC_AML (ssdp_proc_aml + *ssdt_proc_start)
610 /* 0x5B 0x82 DeviceOp PkgLength NameString */
611 #define ACPI_PCIHP_OFFSET_HEX (*ssdt_pcihp_name - *ssdt_pcihp_start + 1)
612 #define ACPI_PCIHP_OFFSET_ID (*ssdt_pcihp_id - *ssdt_pcihp_start)
613 #define ACPI_PCIHP_OFFSET_ADR (*ssdt_pcihp_adr - *ssdt_pcihp_start)
614 #define ACPI_PCIHP_OFFSET_EJ0 (*ssdt_pcihp_ej0 - *ssdt_pcihp_start)
615 #define ACPI_PCIHP_SIZEOF (*ssdt_pcihp_end - *ssdt_pcihp_start)
616 #define ACPI_PCIHP_AML (ssdp_pcihp_aml + *ssdt_pcihp_start)
618 #define ACPI_SSDT_SIGNATURE 0x54445353 /* SSDT */
619 #define ACPI_SSDT_HEADER_LENGTH 36
621 #include "hw/i386/ssdt-misc.hex"
622 #include "hw/i386/ssdt-pcihp.hex"
625 build_append_notify(GArray
*device
, const char *name
,
626 const char *format
, int skip
, int count
)
629 GArray
*method
= build_alloc_array();
630 uint8_t op
= 0x14; /* MethodOp */
632 build_append_nameseg(method
, name
);
633 build_append_byte(method
, 0x02); /* MethodFlags: ArgCount */
634 for (i
= skip
; i
< count
; i
++) {
635 GArray
*target
= build_alloc_array();
636 build_append_nameseg(target
, format
, i
);
637 assert(i
< 256); /* Fits in 1 byte */
638 build_append_notify_target(method
, target
, i
, 1);
639 build_free_array(target
);
641 build_package(method
, op
, 2);
643 build_append_array(device
, method
);
644 build_free_array(method
);
647 static void patch_pcihp(int slot
, uint8_t *ssdt_ptr
, uint32_t eject
)
649 ssdt_ptr
[ACPI_PCIHP_OFFSET_HEX
] = acpi_get_hex(slot
>> 4);
650 ssdt_ptr
[ACPI_PCIHP_OFFSET_HEX
+ 1] = acpi_get_hex(slot
);
651 ssdt_ptr
[ACPI_PCIHP_OFFSET_ID
] = slot
;
652 ssdt_ptr
[ACPI_PCIHP_OFFSET_ADR
+ 2] = slot
;
654 /* Runtime patching of ACPI_EJ0: to disable hotplug for a slot,
655 * replace the method name: _EJ0 by ACPI_EJ0_.
658 assert(!memcmp(ssdt_ptr
+ ACPI_PCIHP_OFFSET_EJ0
, "_EJ0", 4));
661 memcpy(ssdt_ptr
+ ACPI_PCIHP_OFFSET_EJ0
, "EJ0_", 4);
665 static void patch_pci_windows(PcPciInfo
*pci
, uint8_t *start
, unsigned size
)
667 *ACPI_BUILD_PTR(start
, size
, acpi_pci32_start
[0], uint32_t) =
668 cpu_to_le32(pci
->w32
.begin
);
670 *ACPI_BUILD_PTR(start
, size
, acpi_pci32_end
[0], uint32_t) =
671 cpu_to_le32(pci
->w32
.end
- 1);
673 if (pci
->w64
.end
|| pci
->w64
.begin
) {
674 *ACPI_BUILD_PTR(start
, size
, acpi_pci64_valid
[0], uint8_t) = 1;
675 *ACPI_BUILD_PTR(start
, size
, acpi_pci64_start
[0], uint64_t) =
676 cpu_to_le64(pci
->w64
.begin
);
677 *ACPI_BUILD_PTR(start
, size
, acpi_pci64_end
[0], uint64_t) =
678 cpu_to_le64(pci
->w64
.end
- 1);
679 *ACPI_BUILD_PTR(start
, size
, acpi_pci64_length
[0], uint64_t) =
680 cpu_to_le64(pci
->w64
.end
- pci
->w64
.begin
);
682 *ACPI_BUILD_PTR(start
, size
, acpi_pci64_valid
[0], uint8_t) = 0;
687 build_ssdt(GArray
*table_data
, GArray
*linker
,
688 AcpiCpuInfo
*cpu
, AcpiPmInfo
*pm
, AcpiMiscInfo
*misc
,
689 PcPciInfo
*pci
, PcGuestInfo
*guest_info
)
691 int acpi_cpus
= MIN(0xff, guest_info
->apic_id_limit
);
692 int ssdt_start
= table_data
->len
;
696 /* Copy header and patch values in the S3_ / S4_ / S5_ packages */
697 ssdt_ptr
= acpi_data_push(table_data
, sizeof(ssdp_misc_aml
));
698 memcpy(ssdt_ptr
, ssdp_misc_aml
, sizeof(ssdp_misc_aml
));
699 if (pm
->s3_disabled
) {
700 ssdt_ptr
[acpi_s3_name
[0]] = 'X';
702 if (pm
->s4_disabled
) {
703 ssdt_ptr
[acpi_s4_name
[0]] = 'X';
705 ssdt_ptr
[acpi_s4_pkg
[0] + 1] = ssdt_ptr
[acpi_s4_pkg
[0] + 3] =
709 patch_pci_windows(pci
, ssdt_ptr
, sizeof(ssdp_misc_aml
));
711 *(uint16_t *)(ssdt_ptr
+ *ssdt_isa_pest
) =
712 cpu_to_le16(misc
->pvpanic_port
);
715 GArray
*sb_scope
= build_alloc_array();
716 uint8_t op
= 0x10; /* ScopeOp */
718 build_append_nameseg(sb_scope
, "_SB_");
720 /* build Processor object for each processor */
721 for (i
= 0; i
< acpi_cpus
; i
++) {
722 uint8_t *proc
= acpi_data_push(sb_scope
, ACPI_PROC_SIZEOF
);
723 memcpy(proc
, ACPI_PROC_AML
, ACPI_PROC_SIZEOF
);
724 proc
[ACPI_PROC_OFFSET_CPUHEX
] = acpi_get_hex(i
>> 4);
725 proc
[ACPI_PROC_OFFSET_CPUHEX
+1] = acpi_get_hex(i
);
726 proc
[ACPI_PROC_OFFSET_CPUID1
] = i
;
727 proc
[ACPI_PROC_OFFSET_CPUID2
] = i
;
731 * Method(NTFY, 2) {If (LEqual(Arg0, 0x00)) {Notify(CP00, Arg1)} ...}
733 /* Arg0 = Processor ID = APIC ID */
734 build_append_notify(sb_scope
, "NTFY", "CP%0.02X", 0, acpi_cpus
);
736 /* build "Name(CPON, Package() { One, One, ..., Zero, Zero, ... })" */
737 build_append_byte(sb_scope
, 0x08); /* NameOp */
738 build_append_nameseg(sb_scope
, "CPON");
741 GArray
*package
= build_alloc_array();
742 uint8_t op
= 0x12; /* PackageOp */
744 build_append_byte(package
, acpi_cpus
); /* NumElements */
745 for (i
= 0; i
< acpi_cpus
; i
++) {
746 uint8_t b
= test_bit(i
, cpu
->found_cpus
) ? 0x01 : 0x00;
747 build_append_byte(package
, b
);
750 build_package(package
, op
, 2);
751 build_append_array(sb_scope
, package
);
752 build_free_array(package
);
756 GArray
*pci0
= build_alloc_array();
757 uint8_t op
= 0x10; /* ScopeOp */;
759 build_append_nameseg(pci0
, "PCI0");
761 /* build Device object for each slot */
762 for (i
= 1; i
< PCI_SLOT_MAX
; i
++) {
763 bool eject
= test_bit(i
, misc
->slot_hotplug_enable
);
764 void *pcihp
= acpi_data_push(pci0
, ACPI_PCIHP_SIZEOF
);
766 memcpy(pcihp
, ACPI_PCIHP_AML
, ACPI_PCIHP_SIZEOF
);
767 patch_pcihp(i
, pcihp
, eject
);
770 build_append_notify(pci0
, "PCNT", "S%0.02X_", 1, PCI_SLOT_MAX
);
771 build_package(pci0
, op
, 3);
772 build_append_array(sb_scope
, pci0
);
773 build_free_array(pci0
);
776 build_package(sb_scope
, op
, 3);
777 build_append_array(table_data
, sb_scope
);
778 build_free_array(sb_scope
);
781 build_header(linker
, table_data
,
782 (void *)(table_data
->data
+ ssdt_start
),
783 ACPI_SSDT_SIGNATURE
, table_data
->len
- ssdt_start
, 1);
787 build_hpet(GArray
*table_data
, GArray
*linker
)
791 hpet
= acpi_data_push(table_data
, sizeof(*hpet
));
792 /* Note timer_block_id value must be kept in sync with value advertised by
795 hpet
->timer_block_id
= cpu_to_le32(0x8086a201);
796 hpet
->addr
.address
= cpu_to_le64(HPET_BASE
);
797 build_header(linker
, table_data
,
798 (void *)hpet
, ACPI_HPET_SIGNATURE
, sizeof(*hpet
), 1);
802 acpi_build_srat_memory(AcpiSratMemoryAffinity
*numamem
,
803 uint64_t base
, uint64_t len
, int node
, int enabled
)
805 numamem
->type
= ACPI_SRAT_MEMORY
;
806 numamem
->length
= sizeof(*numamem
);
807 memset(numamem
->proximity
, 0, 4);
808 numamem
->proximity
[0] = node
;
809 numamem
->flags
= cpu_to_le32(!!enabled
);
810 numamem
->base_addr
= cpu_to_le64(base
);
811 numamem
->range_length
= cpu_to_le64(len
);
815 build_srat(GArray
*table_data
, GArray
*linker
,
816 AcpiCpuInfo
*cpu
, PcGuestInfo
*guest_info
)
818 AcpiSystemResourceAffinityTable
*srat
;
819 AcpiSratProcessorAffinity
*core
;
820 AcpiSratMemoryAffinity
*numamem
;
824 int srat_start
, numa_start
, slots
;
825 uint64_t mem_len
, mem_base
, next_base
;
827 srat_start
= table_data
->len
;
829 srat
= acpi_data_push(table_data
, sizeof *srat
);
830 srat
->reserved1
= cpu_to_le32(1);
831 core
= (void *)(srat
+ 1);
833 for (i
= 0; i
< guest_info
->apic_id_limit
; ++i
) {
834 core
= acpi_data_push(table_data
, sizeof *core
);
835 core
->type
= ACPI_SRAT_PROCESSOR
;
836 core
->length
= sizeof(*core
);
837 core
->local_apic_id
= i
;
838 curnode
= guest_info
->node_cpu
[i
];
839 core
->proximity_lo
= curnode
;
840 memset(core
->proximity_hi
, 0, 3);
841 core
->local_sapic_eid
= 0;
842 if (test_bit(i
, cpu
->found_cpus
)) {
843 core
->flags
= cpu_to_le32(1);
845 core
->flags
= cpu_to_le32(0);
850 /* the memory map is a bit tricky, it contains at least one hole
851 * from 640k-1M and possibly another one from 3.5G-4G.
854 numa_start
= table_data
->len
;
856 numamem
= acpi_data_push(table_data
, sizeof *numamem
);
857 acpi_build_srat_memory(numamem
, 0, 640*1024, 0, 1);
858 next_base
= 1024 * 1024;
859 for (i
= 1; i
< guest_info
->numa_nodes
+ 1; ++i
) {
860 mem_base
= next_base
;
861 mem_len
= guest_info
->node_mem
[i
- 1];
863 mem_len
-= 1024 * 1024;
865 next_base
= mem_base
+ mem_len
;
867 /* Cut out the ACPI_PCI hole */
868 if (mem_base
<= guest_info
->ram_size
&&
869 next_base
> guest_info
->ram_size
) {
870 mem_len
-= next_base
- guest_info
->ram_size
;
872 numamem
= acpi_data_push(table_data
, sizeof *numamem
);
873 acpi_build_srat_memory(numamem
, mem_base
, mem_len
, i
-1, 1);
875 mem_base
= 1ULL << 32;
876 mem_len
= next_base
- guest_info
->ram_size
;
877 next_base
+= (1ULL << 32) - guest_info
->ram_size
;
879 numamem
= acpi_data_push(table_data
, sizeof *numamem
);
880 acpi_build_srat_memory(numamem
, mem_base
, mem_len
, i
- 1, 1);
882 slots
= (table_data
->len
- numa_start
) / sizeof *numamem
;
883 for (; slots
< guest_info
->numa_nodes
+ 2; slots
++) {
884 numamem
= acpi_data_push(table_data
, sizeof *numamem
);
885 acpi_build_srat_memory(numamem
, 0, 0, 0, 0);
888 build_header(linker
, table_data
,
889 (void *)(table_data
->data
+ srat_start
),
891 table_data
->len
- srat_start
, 1);
895 build_mcfg_q35(GArray
*table_data
, GArray
*linker
, AcpiMcfgInfo
*info
)
899 int len
= sizeof(*mcfg
) + 1 * sizeof(mcfg
->allocation
[0]);
901 mcfg
= acpi_data_push(table_data
, len
);
902 mcfg
->allocation
[0].address
= cpu_to_le64(info
->mcfg_base
);
903 /* Only a single allocation so no need to play with segments */
904 mcfg
->allocation
[0].pci_segment
= cpu_to_le16(0);
905 mcfg
->allocation
[0].start_bus_number
= 0;
906 mcfg
->allocation
[0].end_bus_number
= PCIE_MMCFG_BUS(info
->mcfg_size
- 1);
908 /* MCFG is used for ECAM which can be enabled or disabled by guest.
909 * To avoid table size changes (which create migration issues),
910 * always create the table even if there are no allocations,
911 * but set the signature to a reserved value in this case.
912 * ACPI spec requires OSPMs to ignore such tables.
914 if (info
->mcfg_base
== PCIE_BASE_ADDR_UNMAPPED
) {
915 sig
= ACPI_RSRV_SIGNATURE
;
917 sig
= ACPI_MCFG_SIGNATURE
;
919 build_header(linker
, table_data
, (void *)mcfg
, sig
, len
, 1);
923 build_dsdt(GArray
*table_data
, GArray
*linker
, AcpiMiscInfo
*misc
)
926 assert(misc
->dsdt_code
&& misc
->dsdt_size
);
927 dsdt
= acpi_data_push(table_data
, misc
->dsdt_size
);
928 memcpy(dsdt
, misc
->dsdt_code
, misc
->dsdt_size
);
931 /* Build final rsdt table */
933 build_rsdt(GArray
*table_data
, GArray
*linker
, GArray
*table_offsets
)
935 AcpiRsdtDescriptorRev1
*rsdt
;
939 rsdt_len
= sizeof(*rsdt
) + sizeof(uint32_t) * table_offsets
->len
;
940 rsdt
= acpi_data_push(table_data
, rsdt_len
);
941 memcpy(rsdt
->table_offset_entry
, table_offsets
->data
,
942 sizeof(uint32_t) * table_offsets
->len
);
943 for (i
= 0; i
< table_offsets
->len
; ++i
) {
944 /* rsdt->table_offset_entry to be filled by Guest linker */
945 bios_linker_loader_add_pointer(linker
,
946 ACPI_BUILD_TABLE_FILE
,
947 ACPI_BUILD_TABLE_FILE
,
948 table_data
, &rsdt
->table_offset_entry
[i
],
951 build_header(linker
, table_data
,
952 (void *)rsdt
, ACPI_RSDT_SIGNATURE
, rsdt_len
, 1);
956 build_rsdp(GArray
*rsdp_table
, GArray
*linker
, unsigned rsdt
)
958 AcpiRsdpDescriptor
*rsdp
= acpi_data_push(rsdp_table
, sizeof *rsdp
);
960 bios_linker_loader_alloc(linker
, ACPI_BUILD_RSDP_FILE
, 1,
961 true /* fseg memory */);
963 rsdp
->signature
= cpu_to_le64(ACPI_RSDP_SIGNATURE
);
964 memcpy(rsdp
->oem_id
, ACPI_BUILD_APPNAME6
, 6);
965 rsdp
->rsdt_physical_address
= cpu_to_le32(rsdt
);
966 /* Address to be filled by Guest linker */
967 bios_linker_loader_add_pointer(linker
, ACPI_BUILD_RSDP_FILE
,
968 ACPI_BUILD_TABLE_FILE
,
969 rsdp_table
, &rsdp
->rsdt_physical_address
,
970 sizeof rsdp
->rsdt_physical_address
);
972 /* Checksum to be filled by Guest linker */
973 bios_linker_loader_add_checksum(linker
, ACPI_BUILD_RSDP_FILE
,
974 rsdp
, rsdp
, sizeof *rsdp
, &rsdp
->checksum
);
980 struct AcpiBuildTables
{
986 static inline void acpi_build_tables_init(AcpiBuildTables
*tables
)
988 tables
->rsdp
= g_array_new(false, true /* clear */, 1);
989 tables
->table_data
= g_array_new(false, true /* clear */, 1);
990 tables
->linker
= bios_linker_loader_init();
993 static inline void acpi_build_tables_cleanup(AcpiBuildTables
*tables
, bool mfre
)
995 void *linker_data
= bios_linker_loader_cleanup(tables
->linker
);
999 g_array_free(tables
->rsdp
, mfre
);
1000 g_array_free(tables
->table_data
, mfre
);
1004 struct AcpiBuildState
{
1005 /* Copy of table in RAM (for patching). */
1007 uint32_t table_size
;
1008 /* Is table patched? */
1010 PcGuestInfo
*guest_info
;
1013 static bool acpi_get_mcfg(AcpiMcfgInfo
*mcfg
)
1019 pci_host
= object_resolve_path_type("", TYPE_PCI_HOST_BRIDGE
, &ambiguous
);
1020 g_assert(!ambiguous
);
1023 o
= object_property_get_qobject(pci_host
, PCIE_HOST_MCFG_BASE
, NULL
);
1027 mcfg
->mcfg_base
= qint_get_int(qobject_to_qint(o
));
1029 o
= object_property_get_qobject(pci_host
, PCIE_HOST_MCFG_SIZE
, NULL
);
1031 mcfg
->mcfg_size
= qint_get_int(qobject_to_qint(o
));
1036 void acpi_build(PcGuestInfo
*guest_info
, AcpiBuildTables
*tables
)
1038 GArray
*table_offsets
;
1039 unsigned facs
, dsdt
, rsdt
;
1047 acpi_get_cpu_info(&cpu
);
1048 acpi_get_pm_info(&pm
);
1049 acpi_get_dsdt(&misc
);
1050 acpi_get_hotplug_info(&misc
);
1051 acpi_get_misc_info(&misc
);
1052 acpi_get_pci_info(&pci
);
1054 table_offsets
= g_array_new(false, true /* clear */,
1056 ACPI_BUILD_DPRINTF(3, "init ACPI tables\n");
1058 bios_linker_loader_alloc(tables
->linker
, ACPI_BUILD_TABLE_FILE
,
1059 64 /* Ensure FACS is aligned */,
1060 false /* high memory */);
1063 * FACS is pointed to by FADT.
1064 * We place it first since it's the only table that has alignment
1067 facs
= tables
->table_data
->len
;
1068 build_facs(tables
->table_data
, tables
->linker
, guest_info
);
1070 /* DSDT is pointed to by FADT */
1071 dsdt
= tables
->table_data
->len
;
1072 build_dsdt(tables
->table_data
, tables
->linker
, &misc
);
1074 /* ACPI tables pointed to by RSDT */
1075 acpi_add_table(table_offsets
, tables
->table_data
);
1076 build_fadt(tables
->table_data
, tables
->linker
, &pm
, facs
, dsdt
);
1077 acpi_add_table(table_offsets
, tables
->table_data
);
1079 build_ssdt(tables
->table_data
, tables
->linker
, &cpu
, &pm
, &misc
, &pci
,
1081 acpi_add_table(table_offsets
, tables
->table_data
);
1083 build_madt(tables
->table_data
, tables
->linker
, &cpu
, guest_info
);
1084 acpi_add_table(table_offsets
, tables
->table_data
);
1085 if (misc
.has_hpet
) {
1086 build_hpet(tables
->table_data
, tables
->linker
);
1088 if (guest_info
->numa_nodes
) {
1089 acpi_add_table(table_offsets
, tables
->table_data
);
1090 build_srat(tables
->table_data
, tables
->linker
, &cpu
, guest_info
);
1092 if (acpi_get_mcfg(&mcfg
)) {
1093 acpi_add_table(table_offsets
, tables
->table_data
);
1094 build_mcfg_q35(tables
->table_data
, tables
->linker
, &mcfg
);
1097 /* Add tables supplied by user (if any) */
1098 for (u
= acpi_table_first(); u
; u
= acpi_table_next(u
)) {
1099 unsigned len
= acpi_table_len(u
);
1101 acpi_add_table(table_offsets
, tables
->table_data
);
1102 g_array_append_vals(tables
->table_data
, u
, len
);
1105 /* RSDT is pointed to by RSDP */
1106 rsdt
= tables
->table_data
->len
;
1107 build_rsdt(tables
->table_data
, tables
->linker
, table_offsets
);
1109 /* RSDP is in FSEG memory, so allocate it separately */
1110 build_rsdp(tables
->rsdp
, tables
->linker
, rsdt
);
1112 /* We'll expose it all to Guest so align size to reduce
1113 * chance of size changes.
1114 * RSDP is small so it's easy to keep it immutable, no need to
1115 * bother with alignment.
1117 acpi_align_size(tables
->table_data
, 0x1000);
1119 acpi_align_size(tables
->linker
, 0x1000);
1121 /* Cleanup memory that's no longer used. */
1122 g_array_free(table_offsets
, true);
1125 static void acpi_build_update(void *build_opaque
, uint32_t offset
)
1127 AcpiBuildState
*build_state
= build_opaque
;
1128 AcpiBuildTables tables
;
1130 /* No state to update or already patched? Nothing to do. */
1131 if (!build_state
|| build_state
->patched
) {
1134 build_state
->patched
= 1;
1136 acpi_build_tables_init(&tables
);
1138 acpi_build(build_state
->guest_info
, &tables
);
1140 assert(acpi_data_len(tables
.table_data
) == build_state
->table_size
);
1141 memcpy(build_state
->table_ram
, tables
.table_data
->data
,
1142 build_state
->table_size
);
1144 acpi_build_tables_cleanup(&tables
, true);
1147 static void acpi_build_reset(void *build_opaque
)
1149 AcpiBuildState
*build_state
= build_opaque
;
1150 build_state
->patched
= 0;
1153 static void *acpi_add_rom_blob(AcpiBuildState
*build_state
, GArray
*blob
,
1156 return rom_add_blob(name
, blob
->data
, acpi_data_len(blob
), -1, name
,
1157 acpi_build_update
, build_state
);
1160 static const VMStateDescription vmstate_acpi_build
= {
1161 .name
= "acpi_build",
1163 .minimum_version_id
= 1,
1164 .minimum_version_id_old
= 1,
1165 .fields
= (VMStateField
[]) {
1166 VMSTATE_UINT8(patched
, AcpiBuildState
),
1167 VMSTATE_END_OF_LIST()
1171 void acpi_setup(PcGuestInfo
*guest_info
)
1173 AcpiBuildTables tables
;
1174 AcpiBuildState
*build_state
;
1176 if (!guest_info
->fw_cfg
) {
1177 ACPI_BUILD_DPRINTF(3, "No fw cfg. Bailing out.\n");
1181 if (!guest_info
->has_acpi_build
) {
1182 ACPI_BUILD_DPRINTF(3, "ACPI build disabled. Bailing out.\n");
1186 if (!acpi_enabled
) {
1187 ACPI_BUILD_DPRINTF(3, "ACPI disabled. Bailing out.\n");
1191 build_state
= g_malloc0(sizeof *build_state
);
1193 build_state
->guest_info
= guest_info
;
1195 acpi_build_tables_init(&tables
);
1196 acpi_build(build_state
->guest_info
, &tables
);
1198 /* Now expose it all to Guest */
1199 build_state
->table_ram
= acpi_add_rom_blob(build_state
, tables
.table_data
,
1200 ACPI_BUILD_TABLE_FILE
);
1201 build_state
->table_size
= acpi_data_len(tables
.table_data
);
1203 acpi_add_rom_blob(NULL
, tables
.linker
, "etc/table-loader");
1206 * RSDP is small so it's easy to keep it immutable, no need to
1207 * bother with ROM blobs.
1209 fw_cfg_add_file(guest_info
->fw_cfg
, ACPI_BUILD_RSDP_FILE
,
1210 tables
.rsdp
->data
, acpi_data_len(tables
.rsdp
));
1212 qemu_register_reset(acpi_build_reset
, build_state
);
1213 acpi_build_reset(build_state
);
1214 vmstate_register(NULL
, 0, &vmstate_acpi_build
, build_state
);
1216 /* Cleanup tables but don't free the memory: we track it
1219 acpi_build_tables_cleanup(&tables
, false);