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"
26 #include "exec/memory.h"
27 #include "hw/acpi/acpi.h"
28 #include "hw/acpi/aml-build.h"
29 #include "hw/acpi/bios-linker-loader.h"
30 #include "hw/acpi/generic_event_device.h"
31 #include "hw/acpi/utils.h"
32 #include "hw/boards.h"
33 #include "hw/i386/fw_cfg.h"
34 #include "hw/i386/microvm.h"
36 #include "acpi-common.h"
37 #include "acpi-microvm.h"
40 build_dsdt_microvm(GArray
*table_data
, BIOSLinker
*linker
,
41 MicrovmMachineState
*mms
)
43 X86MachineState
*x86ms
= X86_MACHINE(mms
);
44 Aml
*dsdt
, *sb_scope
, *scope
, *pkg
;
48 isabus
= object_resolve_path_type("", TYPE_ISA_BUS
, &ambiguous
);
52 dsdt
= init_aml_allocator();
54 /* Reserve space for header */
55 acpi_data_push(dsdt
->buf
, sizeof(AcpiTableHeader
));
57 sb_scope
= aml_scope("_SB");
58 fw_cfg_add_acpi_dsdt(sb_scope
, x86ms
->fw_cfg
);
59 isa_build_aml(ISA_BUS(isabus
), sb_scope
);
60 build_ged_aml(sb_scope
, GED_DEVICE
, HOTPLUG_HANDLER(mms
->acpi_dev
),
61 GED_MMIO_IRQ
, AML_SYSTEM_MEMORY
, GED_MMIO_BASE
);
62 acpi_dsdt_add_power_button(sb_scope
);
63 aml_append(dsdt
, sb_scope
);
65 /* ACPI 5.0: Table 7-209 System State Package */
66 scope
= aml_scope("\\");
68 aml_append(pkg
, aml_int(ACPI_GED_SLP_TYP_S5
));
69 aml_append(pkg
, aml_int(0)); /* ignored */
70 aml_append(pkg
, aml_int(0)); /* reserved */
71 aml_append(pkg
, aml_int(0)); /* reserved */
72 aml_append(scope
, aml_name_decl("_S5", pkg
));
73 aml_append(dsdt
, scope
);
75 /* copy AML table into ACPI tables blob and patch header there */
76 g_array_append_vals(table_data
, dsdt
->buf
->data
, dsdt
->buf
->len
);
77 build_header(linker
, table_data
,
78 (void *)(table_data
->data
+ table_data
->len
- dsdt
->buf
->len
),
79 "DSDT", dsdt
->buf
->len
, 2, NULL
, NULL
);
83 static void acpi_build_microvm(AcpiBuildTables
*tables
,
84 MicrovmMachineState
*mms
)
86 MachineState
*machine
= MACHINE(mms
);
87 GArray
*table_offsets
;
88 GArray
*tables_blob
= tables
->table_data
;
90 AcpiFadtData pmfadt
= {
91 /* ACPI 5.0: 4.1 Hardware-Reduced ACPI */
93 .flags
= ((1 << ACPI_FADT_F_HW_REDUCED_ACPI
) |
94 (1 << ACPI_FADT_F_RESET_REG_SUP
)),
96 /* ACPI 5.0: 4.8.3.7 Sleep Control and Status Registers */
98 .space_id
= AML_AS_SYSTEM_MEMORY
,
100 .address
= GED_MMIO_BASE_REGS
+ ACPI_GED_REG_SLEEP_CTL
,
103 .space_id
= AML_AS_SYSTEM_MEMORY
,
105 .address
= GED_MMIO_BASE_REGS
+ ACPI_GED_REG_SLEEP_STS
,
108 /* ACPI 5.0: 4.8.3.6 Reset Register */
110 .space_id
= AML_AS_SYSTEM_MEMORY
,
112 .address
= GED_MMIO_BASE_REGS
+ ACPI_GED_REG_RESET
,
114 .reset_val
= ACPI_GED_RESET_VALUE
,
117 table_offsets
= g_array_new(false, true /* clear */,
119 bios_linker_loader_alloc(tables
->linker
,
120 ACPI_BUILD_TABLE_FILE
, tables_blob
,
121 64 /* Ensure FACS is aligned */,
122 false /* high memory */);
124 dsdt
= tables_blob
->len
;
125 build_dsdt_microvm(tables_blob
, tables
->linker
, mms
);
127 pmfadt
.dsdt_tbl_offset
= &dsdt
;
128 pmfadt
.xdsdt_tbl_offset
= &dsdt
;
129 acpi_add_table(table_offsets
, tables_blob
);
130 build_fadt(tables_blob
, tables
->linker
, &pmfadt
, NULL
, NULL
);
132 acpi_add_table(table_offsets
, tables_blob
);
133 acpi_build_madt(tables_blob
, tables
->linker
, X86_MACHINE(machine
),
134 mms
->acpi_dev
, false);
136 xsdt
= tables_blob
->len
;
137 build_xsdt(tables_blob
, tables
->linker
, table_offsets
, NULL
, NULL
);
139 /* RSDP is in FSEG memory, so allocate it separately */
141 AcpiRsdpData rsdp_data
= {
142 /* ACPI 2.0: 5.2.4.3 RSDP Structure */
143 .revision
= 2, /* xsdt needs v2 */
144 .oem_id
= ACPI_BUILD_APPNAME6
,
145 .xsdt_tbl_offset
= &xsdt
,
146 .rsdt_tbl_offset
= NULL
,
148 build_rsdp(tables
->rsdp
, tables
->linker
, &rsdp_data
);
151 /* Cleanup memory that's no longer used. */
152 g_array_free(table_offsets
, true);
155 static void acpi_build_no_update(void *build_opaque
)
157 /* nothing, microvm tables don't change at runtime */
160 void acpi_setup_microvm(MicrovmMachineState
*mms
)
162 X86MachineState
*x86ms
= X86_MACHINE(mms
);
163 AcpiBuildTables tables
;
165 assert(x86ms
->fw_cfg
);
167 if (!x86_machine_is_acpi_enabled(x86ms
)) {
171 acpi_build_tables_init(&tables
);
172 acpi_build_microvm(&tables
, mms
);
174 /* Now expose it all to Guest */
175 acpi_add_rom_blob(acpi_build_no_update
, NULL
,
177 ACPI_BUILD_TABLE_FILE
,
178 ACPI_BUILD_TABLE_MAX_SIZE
);
179 acpi_add_rom_blob(acpi_build_no_update
, NULL
,
180 tables
.linker
->cmd_blob
,
181 "etc/table-loader", 0);
182 acpi_add_rom_blob(acpi_build_no_update
, NULL
,
184 ACPI_BUILD_RSDP_FILE
, 0);
186 acpi_build_tables_cleanup(&tables
, false);