]> Git Repo - qemu.git/blame - hw/i386/acpi-build.c
pc: export applesmc IO port/len
[qemu.git] / hw / i386 / acpi-build.c
CommitLineData
72c194f7
MT
1/* Support for generating ACPI tables and passing them to Guests
2 *
3 * Copyright (C) 2008-2010 Kevin O'Connor <[email protected]>
4 * Copyright (C) 2006 Fabrice Bellard
5 * Copyright (C) 2013 Red Hat Inc
6 *
7 * Author: Michael S. Tsirkin <[email protected]>
8 *
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.
13
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.
18
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/>.
21 */
22
23#include "acpi-build.h"
24#include <stddef.h>
25#include <glib.h>
26#include "qemu-common.h"
27#include "qemu/bitmap.h"
07fb6176 28#include "qemu/osdep.h"
72c194f7 29#include "qemu/range.h"
07fb6176 30#include "qemu/error-report.h"
72c194f7
MT
31#include "hw/pci/pci.h"
32#include "qom/cpu.h"
33#include "hw/i386/pc.h"
34#include "target-i386/cpu.h"
35#include "hw/timer/hpet.h"
36#include "hw/i386/acpi-defs.h"
37#include "hw/acpi/acpi.h"
38#include "hw/nvram/fw_cfg.h"
0058ae1d 39#include "hw/acpi/bios-linker-loader.h"
72c194f7 40#include "hw/loader.h"
15bce1b7 41#include "hw/isa/isa.h"
bef3492d 42#include "hw/acpi/memory_hotplug.h"
711b20b4
SB
43#include "sysemu/tpm.h"
44#include "hw/acpi/tpm.h"
72c194f7
MT
45
46/* Supported chipsets: */
47#include "hw/acpi/piix4.h"
99fd437d 48#include "hw/acpi/pcihp.h"
72c194f7
MT
49#include "hw/i386/ich9.h"
50#include "hw/pci/pci_bus.h"
51#include "hw/pci-host/q35.h"
d4eb9119 52#include "hw/i386/intel_iommu.h"
72c194f7
MT
53
54#include "hw/i386/q35-acpi-dsdt.hex"
55#include "hw/i386/acpi-dsdt.hex"
56
19934e0e
IM
57#include "hw/acpi/aml-build.h"
58
72c194f7
MT
59#include "qapi/qmp/qint.h"
60#include "qom/qom-qobject.h"
ad5b88b1 61#include "exec/ram_addr.h"
72c194f7 62
07fb6176
PB
63/* These are used to size the ACPI tables for -M pc-i440fx-1.7 and
64 * -M pc-i440fx-2.0. Even if the actual amount of AML generated grows
65 * a little bit, there should be plenty of free space since the DSDT
66 * shrunk by ~1.5k between QEMU 2.0 and QEMU 2.1.
67 */
68#define ACPI_BUILD_LEGACY_CPU_AML_SIZE 97
69#define ACPI_BUILD_ALIGN_SIZE 0x1000
70
868270f2 71#define ACPI_BUILD_TABLE_SIZE 0x20000
18045fb9 72
a1666142
MT
73/* Reserve RAM space for tables: add another order of magnitude. */
74#define ACPI_BUILD_TABLE_MAX_SIZE 0x200000
75
8b310fc4
GA
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)
80#else
81#define ACPI_BUILD_DPRINTF(fmt, ...)
82#endif
83
72c194f7 84typedef struct AcpiCpuInfo {
798325ed 85 DECLARE_BITMAP(found_cpus, ACPI_CPU_HOTPLUG_ID_LIMIT);
72c194f7
MT
86} AcpiCpuInfo;
87
88typedef struct AcpiMcfgInfo {
89 uint64_t mcfg_base;
90 uint32_t mcfg_size;
91} AcpiMcfgInfo;
92
93typedef struct AcpiPmInfo {
94 bool s3_disabled;
95 bool s4_disabled;
133a2da4 96 bool pcihp_bridge_en;
72c194f7
MT
97 uint8_t s4_val;
98 uint16_t sci_int;
99 uint8_t acpi_enable_cmd;
100 uint8_t acpi_disable_cmd;
101 uint32_t gpe0_blk;
102 uint32_t gpe0_blk_len;
103 uint32_t io_base;
ddf1ec2f
IM
104 uint16_t cpu_hp_io_base;
105 uint16_t cpu_hp_io_len;
2c6b94d8
IM
106 uint16_t mem_hp_io_base;
107 uint16_t mem_hp_io_len;
500b11ea
IM
108 uint16_t pcihp_io_base;
109 uint16_t pcihp_io_len;
72c194f7
MT
110} AcpiPmInfo;
111
112typedef struct AcpiMiscInfo {
113 bool has_hpet;
711b20b4 114 bool has_tpm;
72c194f7
MT
115 DECLARE_BITMAP(slot_hotplug_enable, PCI_SLOT_MAX);
116 const unsigned char *dsdt_code;
117 unsigned dsdt_size;
118 uint16_t pvpanic_port;
119} AcpiMiscInfo;
120
99fd437d
MT
121typedef struct AcpiBuildPciBusHotplugState {
122 GArray *device_table;
123 GArray *notify_table;
124 struct AcpiBuildPciBusHotplugState *parent;
133a2da4 125 bool pcihp_bridge_en;
99fd437d
MT
126} AcpiBuildPciBusHotplugState;
127
72c194f7
MT
128static void acpi_get_dsdt(AcpiMiscInfo *info)
129{
8977557a 130 uint16_t *applesmc_sta;
72c194f7
MT
131 Object *piix = piix4_pm_find();
132 Object *lpc = ich9_lpc_find();
133 assert(!!piix != !!lpc);
134
135 if (piix) {
136 info->dsdt_code = AcpiDsdtAmlCode;
137 info->dsdt_size = sizeof AcpiDsdtAmlCode;
8977557a 138 applesmc_sta = piix_dsdt_applesmc_sta;
72c194f7
MT
139 }
140 if (lpc) {
141 info->dsdt_code = Q35AcpiDsdtAmlCode;
142 info->dsdt_size = sizeof Q35AcpiDsdtAmlCode;
8977557a 143 applesmc_sta = q35_dsdt_applesmc_sta;
72c194f7 144 }
15bce1b7
GS
145
146 /* Patch in appropriate value for AppleSMC _STA */
8977557a 147 *(uint8_t *)(info->dsdt_code + *applesmc_sta) =
1142e45f 148 applesmc_port() ? 0x0b : 0x00;
72c194f7
MT
149}
150
151static
152int acpi_add_cpu_info(Object *o, void *opaque)
153{
154 AcpiCpuInfo *cpu = opaque;
155 uint64_t apic_id;
156
157 if (object_dynamic_cast(o, TYPE_CPU)) {
158 apic_id = object_property_get_int(o, "apic-id", NULL);
798325ed 159 assert(apic_id < ACPI_CPU_HOTPLUG_ID_LIMIT);
72c194f7
MT
160
161 set_bit(apic_id, cpu->found_cpus);
162 }
163
164 object_child_foreach(o, acpi_add_cpu_info, opaque);
165 return 0;
166}
167
168static void acpi_get_cpu_info(AcpiCpuInfo *cpu)
169{
170 Object *root = object_get_root();
171
172 memset(cpu->found_cpus, 0, sizeof cpu->found_cpus);
173 object_child_foreach(root, acpi_add_cpu_info, cpu);
174}
175
176static void acpi_get_pm_info(AcpiPmInfo *pm)
177{
178 Object *piix = piix4_pm_find();
179 Object *lpc = ich9_lpc_find();
180 Object *obj = NULL;
181 QObject *o;
182
500b11ea
IM
183 pm->pcihp_io_base = 0;
184 pm->pcihp_io_len = 0;
72c194f7
MT
185 if (piix) {
186 obj = piix;
ddf1ec2f 187 pm->cpu_hp_io_base = PIIX4_CPU_HOTPLUG_IO_BASE;
500b11ea
IM
188 pm->pcihp_io_base =
189 object_property_get_int(obj, ACPI_PCIHP_IO_BASE_PROP, NULL);
190 pm->pcihp_io_len =
191 object_property_get_int(obj, ACPI_PCIHP_IO_LEN_PROP, NULL);
72c194f7
MT
192 }
193 if (lpc) {
194 obj = lpc;
ddf1ec2f 195 pm->cpu_hp_io_base = ICH9_CPU_HOTPLUG_IO_BASE;
72c194f7
MT
196 }
197 assert(obj);
198
ddf1ec2f 199 pm->cpu_hp_io_len = ACPI_GPE_PROC_LEN;
2c6b94d8
IM
200 pm->mem_hp_io_base = ACPI_MEMORY_HOTPLUG_BASE;
201 pm->mem_hp_io_len = ACPI_MEMORY_HOTPLUG_IO_LEN;
202
72c194f7
MT
203 /* Fill in optional s3/s4 related properties */
204 o = object_property_get_qobject(obj, ACPI_PM_PROP_S3_DISABLED, NULL);
205 if (o) {
206 pm->s3_disabled = qint_get_int(qobject_to_qint(o));
207 } else {
208 pm->s3_disabled = false;
209 }
097a97a6 210 qobject_decref(o);
72c194f7
MT
211 o = object_property_get_qobject(obj, ACPI_PM_PROP_S4_DISABLED, NULL);
212 if (o) {
213 pm->s4_disabled = qint_get_int(qobject_to_qint(o));
214 } else {
215 pm->s4_disabled = false;
216 }
097a97a6 217 qobject_decref(o);
72c194f7
MT
218 o = object_property_get_qobject(obj, ACPI_PM_PROP_S4_VAL, NULL);
219 if (o) {
220 pm->s4_val = qint_get_int(qobject_to_qint(o));
221 } else {
222 pm->s4_val = false;
223 }
097a97a6 224 qobject_decref(o);
72c194f7
MT
225
226 /* Fill in mandatory properties */
227 pm->sci_int = object_property_get_int(obj, ACPI_PM_PROP_SCI_INT, NULL);
228
229 pm->acpi_enable_cmd = object_property_get_int(obj,
230 ACPI_PM_PROP_ACPI_ENABLE_CMD,
231 NULL);
232 pm->acpi_disable_cmd = object_property_get_int(obj,
233 ACPI_PM_PROP_ACPI_DISABLE_CMD,
234 NULL);
235 pm->io_base = object_property_get_int(obj, ACPI_PM_PROP_PM_IO_BASE,
236 NULL);
237 pm->gpe0_blk = object_property_get_int(obj, ACPI_PM_PROP_GPE0_BLK,
238 NULL);
239 pm->gpe0_blk_len = object_property_get_int(obj, ACPI_PM_PROP_GPE0_BLK_LEN,
240 NULL);
133a2da4
IM
241 pm->pcihp_bridge_en =
242 object_property_get_bool(obj, "acpi-pci-hotplug-with-bridge-support",
243 NULL);
72c194f7
MT
244}
245
72c194f7
MT
246static void acpi_get_misc_info(AcpiMiscInfo *info)
247{
248 info->has_hpet = hpet_find();
711b20b4 249 info->has_tpm = tpm_find();
72c194f7
MT
250 info->pvpanic_port = pvpanic_port();
251}
252
253static void acpi_get_pci_info(PcPciInfo *info)
254{
255 Object *pci_host;
256 bool ambiguous;
257
258 pci_host = object_resolve_path_type("", TYPE_PCI_HOST_BRIDGE, &ambiguous);
259 g_assert(!ambiguous);
260 g_assert(pci_host);
261
262 info->w32.begin = object_property_get_int(pci_host,
263 PCI_HOST_PROP_PCI_HOLE_START,
264 NULL);
265 info->w32.end = object_property_get_int(pci_host,
266 PCI_HOST_PROP_PCI_HOLE_END,
267 NULL);
268 info->w64.begin = object_property_get_int(pci_host,
269 PCI_HOST_PROP_PCI_HOLE64_START,
270 NULL);
271 info->w64.end = object_property_get_int(pci_host,
272 PCI_HOST_PROP_PCI_HOLE64_END,
273 NULL);
274}
275
276#define ACPI_BUILD_APPNAME "Bochs"
277#define ACPI_BUILD_APPNAME6 "BOCHS "
278#define ACPI_BUILD_APPNAME4 "BXPC"
279
72c194f7
MT
280#define ACPI_BUILD_TABLE_FILE "etc/acpi/tables"
281#define ACPI_BUILD_RSDP_FILE "etc/acpi/rsdp"
42a5b308 282#define ACPI_BUILD_TPMLOG_FILE "etc/tpm/log"
72c194f7
MT
283
284static void
285build_header(GArray *linker, GArray *table_data,
821e3227 286 AcpiTableHeader *h, const char *sig, int len, uint8_t rev)
72c194f7 287{
821e3227 288 memcpy(&h->signature, sig, 4);
72c194f7
MT
289 h->length = cpu_to_le32(len);
290 h->revision = rev;
291 memcpy(h->oem_id, ACPI_BUILD_APPNAME6, 6);
292 memcpy(h->oem_table_id, ACPI_BUILD_APPNAME4, 4);
821e3227 293 memcpy(h->oem_table_id + 4, sig, 4);
72c194f7
MT
294 h->oem_revision = cpu_to_le32(1);
295 memcpy(h->asl_compiler_id, ACPI_BUILD_APPNAME4, 4);
296 h->asl_compiler_revision = cpu_to_le32(1);
297 h->checksum = 0;
298 /* Checksum to be filled in by Guest linker */
299 bios_linker_loader_add_checksum(linker, ACPI_BUILD_TABLE_FILE,
300 table_data->data, h, len, &h->checksum);
301}
302
99fd437d
MT
303static GArray *build_alloc_method(const char *name, uint8_t arg_count)
304{
305 GArray *method = build_alloc_array();
306
eae8bded 307 build_append_namestring(method, "%s", name);
99fd437d
MT
308 build_append_byte(method, arg_count); /* MethodFlags: ArgCount */
309
310 return method;
311}
312
313static void build_append_and_cleanup_method(GArray *device, GArray *method)
314{
315 uint8_t op = 0x14; /* MethodOp */
316
661875e9 317 build_package(method, op);
99fd437d
MT
318
319 build_append_array(device, method);
320 build_free_array(method);
321}
322
99fd437d 323/* End here */
72c194f7
MT
324#define ACPI_PORT_SMI_CMD 0x00b2 /* TODO: this is APM_CNT_IOPORT */
325
326static inline void *acpi_data_push(GArray *table_data, unsigned size)
327{
328 unsigned off = table_data->len;
329 g_array_set_size(table_data, off + size);
330 return table_data->data + off;
331}
332
333static unsigned acpi_data_len(GArray *table)
334{
134d42d6 335#if GLIB_CHECK_VERSION(2, 22, 0)
b15654c2
MT
336 assert(g_array_get_element_size(table) == 1);
337#endif
338 return table->len;
72c194f7
MT
339}
340
341static void acpi_align_size(GArray *blob, unsigned align)
342{
343 /* Align size to multiple of given size. This reduces the chance
344 * we need to change size in the future (breaking cross version migration).
345 */
134d42d6 346 g_array_set_size(blob, ROUND_UP(acpi_data_len(blob), align));
72c194f7
MT
347}
348
72c194f7
MT
349static inline void acpi_add_table(GArray *table_offsets, GArray *table_data)
350{
351 uint32_t offset = cpu_to_le32(table_data->len);
352 g_array_append_val(table_offsets, offset);
353}
354
355/* FACS */
356static void
357build_facs(GArray *table_data, GArray *linker, PcGuestInfo *guest_info)
358{
359 AcpiFacsDescriptorRev1 *facs = acpi_data_push(table_data, sizeof *facs);
821e3227 360 memcpy(&facs->signature, "FACS", 4);
72c194f7
MT
361 facs->length = cpu_to_le32(sizeof(*facs));
362}
363
364/* Load chipset information in FADT */
365static void fadt_setup(AcpiFadtDescriptorRev1 *fadt, AcpiPmInfo *pm)
366{
367 fadt->model = 1;
368 fadt->reserved1 = 0;
369 fadt->sci_int = cpu_to_le16(pm->sci_int);
370 fadt->smi_cmd = cpu_to_le32(ACPI_PORT_SMI_CMD);
371 fadt->acpi_enable = pm->acpi_enable_cmd;
372 fadt->acpi_disable = pm->acpi_disable_cmd;
373 /* EVT, CNT, TMR offset matches hw/acpi/core.c */
374 fadt->pm1a_evt_blk = cpu_to_le32(pm->io_base);
375 fadt->pm1a_cnt_blk = cpu_to_le32(pm->io_base + 0x04);
376 fadt->pm_tmr_blk = cpu_to_le32(pm->io_base + 0x08);
377 fadt->gpe0_blk = cpu_to_le32(pm->gpe0_blk);
378 /* EVT, CNT, TMR length matches hw/acpi/core.c */
379 fadt->pm1_evt_len = 4;
380 fadt->pm1_cnt_len = 2;
381 fadt->pm_tmr_len = 4;
382 fadt->gpe0_blk_len = pm->gpe0_blk_len;
383 fadt->plvl2_lat = cpu_to_le16(0xfff); /* C2 state not supported */
384 fadt->plvl3_lat = cpu_to_le16(0xfff); /* C3 state not supported */
385 fadt->flags = cpu_to_le32((1 << ACPI_FADT_F_WBINVD) |
386 (1 << ACPI_FADT_F_PROC_C1) |
387 (1 << ACPI_FADT_F_SLP_BUTTON) |
388 (1 << ACPI_FADT_F_RTC_S4));
389 fadt->flags |= cpu_to_le32(1 << ACPI_FADT_F_USE_PLATFORM_CLOCK);
07b81ed9
HZ
390 /* APIC destination mode ("Flat Logical") has an upper limit of 8 CPUs
391 * For more than 8 CPUs, "Clustered Logical" mode has to be used
392 */
393 if (max_cpus > 8) {
394 fadt->flags |= cpu_to_le32(1 << ACPI_FADT_F_FORCE_APIC_CLUSTER_MODEL);
395 }
72c194f7
MT
396}
397
398
399/* FADT */
400static void
401build_fadt(GArray *table_data, GArray *linker, AcpiPmInfo *pm,
402 unsigned facs, unsigned dsdt)
403{
404 AcpiFadtDescriptorRev1 *fadt = acpi_data_push(table_data, sizeof(*fadt));
405
406 fadt->firmware_ctrl = cpu_to_le32(facs);
407 /* FACS address to be filled by Guest linker */
408 bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
409 ACPI_BUILD_TABLE_FILE,
410 table_data, &fadt->firmware_ctrl,
411 sizeof fadt->firmware_ctrl);
412
413 fadt->dsdt = cpu_to_le32(dsdt);
414 /* DSDT address to be filled by Guest linker */
415 bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
416 ACPI_BUILD_TABLE_FILE,
417 table_data, &fadt->dsdt,
418 sizeof fadt->dsdt);
419
420 fadt_setup(fadt, pm);
421
422 build_header(linker, table_data,
821e3227 423 (void *)fadt, "FACP", sizeof(*fadt), 1);
72c194f7
MT
424}
425
426static void
427build_madt(GArray *table_data, GArray *linker, AcpiCpuInfo *cpu,
428 PcGuestInfo *guest_info)
429{
430 int madt_start = table_data->len;
431
432 AcpiMultipleApicTable *madt;
433 AcpiMadtIoApic *io_apic;
434 AcpiMadtIntsrcovr *intsrcovr;
435 AcpiMadtLocalNmi *local_nmi;
436 int i;
437
438 madt = acpi_data_push(table_data, sizeof *madt);
439 madt->local_apic_address = cpu_to_le32(APIC_DEFAULT_ADDRESS);
440 madt->flags = cpu_to_le32(1);
441
442 for (i = 0; i < guest_info->apic_id_limit; i++) {
443 AcpiMadtProcessorApic *apic = acpi_data_push(table_data, sizeof *apic);
444 apic->type = ACPI_APIC_PROCESSOR;
445 apic->length = sizeof(*apic);
446 apic->processor_id = i;
447 apic->local_apic_id = i;
448 if (test_bit(i, cpu->found_cpus)) {
449 apic->flags = cpu_to_le32(1);
450 } else {
451 apic->flags = cpu_to_le32(0);
452 }
453 }
454 io_apic = acpi_data_push(table_data, sizeof *io_apic);
455 io_apic->type = ACPI_APIC_IO;
456 io_apic->length = sizeof(*io_apic);
457#define ACPI_BUILD_IOAPIC_ID 0x0
458 io_apic->io_apic_id = ACPI_BUILD_IOAPIC_ID;
459 io_apic->address = cpu_to_le32(IO_APIC_DEFAULT_ADDRESS);
460 io_apic->interrupt = cpu_to_le32(0);
461
462 if (guest_info->apic_xrupt_override) {
463 intsrcovr = acpi_data_push(table_data, sizeof *intsrcovr);
464 intsrcovr->type = ACPI_APIC_XRUPT_OVERRIDE;
465 intsrcovr->length = sizeof(*intsrcovr);
466 intsrcovr->source = 0;
467 intsrcovr->gsi = cpu_to_le32(2);
468 intsrcovr->flags = cpu_to_le16(0); /* conforms to bus specifications */
469 }
470 for (i = 1; i < 16; i++) {
471#define ACPI_BUILD_PCI_IRQS ((1<<5) | (1<<9) | (1<<10) | (1<<11))
472 if (!(ACPI_BUILD_PCI_IRQS & (1 << i))) {
473 /* No need for a INT source override structure. */
474 continue;
475 }
476 intsrcovr = acpi_data_push(table_data, sizeof *intsrcovr);
477 intsrcovr->type = ACPI_APIC_XRUPT_OVERRIDE;
478 intsrcovr->length = sizeof(*intsrcovr);
479 intsrcovr->source = i;
480 intsrcovr->gsi = cpu_to_le32(i);
481 intsrcovr->flags = cpu_to_le16(0xd); /* active high, level triggered */
482 }
483
484 local_nmi = acpi_data_push(table_data, sizeof *local_nmi);
485 local_nmi->type = ACPI_APIC_LOCAL_NMI;
486 local_nmi->length = sizeof(*local_nmi);
487 local_nmi->processor_id = 0xff; /* all processors */
488 local_nmi->flags = cpu_to_le16(0);
489 local_nmi->lint = 1; /* ACPI_LINT1 */
490
491 build_header(linker, table_data,
821e3227 492 (void *)(table_data->data + madt_start), "APIC",
72c194f7
MT
493 table_data->len - madt_start, 1);
494}
495
496/* Encode a hex value */
497static inline char acpi_get_hex(uint32_t val)
498{
499 val &= 0x0f;
500 return (val <= 9) ? ('0' + val) : ('A' + val - 10);
501}
502
72c194f7
MT
503/* 0x5B 0x82 DeviceOp PkgLength NameString */
504#define ACPI_PCIHP_OFFSET_HEX (*ssdt_pcihp_name - *ssdt_pcihp_start + 1)
505#define ACPI_PCIHP_OFFSET_ID (*ssdt_pcihp_id - *ssdt_pcihp_start)
506#define ACPI_PCIHP_OFFSET_ADR (*ssdt_pcihp_adr - *ssdt_pcihp_start)
507#define ACPI_PCIHP_OFFSET_EJ0 (*ssdt_pcihp_ej0 - *ssdt_pcihp_start)
508#define ACPI_PCIHP_SIZEOF (*ssdt_pcihp_end - *ssdt_pcihp_start)
509#define ACPI_PCIHP_AML (ssdp_pcihp_aml + *ssdt_pcihp_start)
510
8dcf525a
MT
511#define ACPI_PCINOHP_OFFSET_HEX (*ssdt_pcinohp_name - *ssdt_pcinohp_start + 1)
512#define ACPI_PCINOHP_OFFSET_ADR (*ssdt_pcinohp_adr - *ssdt_pcinohp_start)
513#define ACPI_PCINOHP_SIZEOF (*ssdt_pcinohp_end - *ssdt_pcinohp_start)
514#define ACPI_PCINOHP_AML (ssdp_pcihp_aml + *ssdt_pcinohp_start)
515
516#define ACPI_PCIVGA_OFFSET_HEX (*ssdt_pcivga_name - *ssdt_pcivga_start + 1)
517#define ACPI_PCIVGA_OFFSET_ADR (*ssdt_pcivga_adr - *ssdt_pcivga_start)
518#define ACPI_PCIVGA_SIZEOF (*ssdt_pcivga_end - *ssdt_pcivga_start)
519#define ACPI_PCIVGA_AML (ssdp_pcihp_aml + *ssdt_pcivga_start)
520
521#define ACPI_PCIQXL_OFFSET_HEX (*ssdt_pciqxl_name - *ssdt_pciqxl_start + 1)
522#define ACPI_PCIQXL_OFFSET_ADR (*ssdt_pciqxl_adr - *ssdt_pciqxl_start)
523#define ACPI_PCIQXL_SIZEOF (*ssdt_pciqxl_end - *ssdt_pciqxl_start)
524#define ACPI_PCIQXL_AML (ssdp_pcihp_aml + *ssdt_pciqxl_start)
525
72c194f7
MT
526#define ACPI_SSDT_SIGNATURE 0x54445353 /* SSDT */
527#define ACPI_SSDT_HEADER_LENGTH 36
528
72c194f7 529#include "hw/i386/ssdt-pcihp.hex"
711b20b4 530#include "hw/i386/ssdt-tpm.hex"
72c194f7 531
99fd437d 532static void patch_pcihp(int slot, uint8_t *ssdt_ptr)
72c194f7 533{
99fd437d
MT
534 unsigned devfn = PCI_DEVFN(slot, 0);
535
536 ssdt_ptr[ACPI_PCIHP_OFFSET_HEX] = acpi_get_hex(devfn >> 4);
537 ssdt_ptr[ACPI_PCIHP_OFFSET_HEX + 1] = acpi_get_hex(devfn);
72c194f7
MT
538 ssdt_ptr[ACPI_PCIHP_OFFSET_ID] = slot;
539 ssdt_ptr[ACPI_PCIHP_OFFSET_ADR + 2] = slot;
99fd437d
MT
540}
541
8dcf525a
MT
542static void patch_pcinohp(int slot, uint8_t *ssdt_ptr)
543{
544 unsigned devfn = PCI_DEVFN(slot, 0);
545
546 ssdt_ptr[ACPI_PCINOHP_OFFSET_HEX] = acpi_get_hex(devfn >> 4);
547 ssdt_ptr[ACPI_PCINOHP_OFFSET_HEX + 1] = acpi_get_hex(devfn);
548 ssdt_ptr[ACPI_PCINOHP_OFFSET_ADR + 2] = slot;
549}
550
551static void patch_pcivga(int slot, uint8_t *ssdt_ptr)
552{
553 unsigned devfn = PCI_DEVFN(slot, 0);
554
555 ssdt_ptr[ACPI_PCIVGA_OFFSET_HEX] = acpi_get_hex(devfn >> 4);
556 ssdt_ptr[ACPI_PCIVGA_OFFSET_HEX + 1] = acpi_get_hex(devfn);
557 ssdt_ptr[ACPI_PCIVGA_OFFSET_ADR + 2] = slot;
558}
559
560static void patch_pciqxl(int slot, uint8_t *ssdt_ptr)
561{
562 unsigned devfn = PCI_DEVFN(slot, 0);
563
564 ssdt_ptr[ACPI_PCIQXL_OFFSET_HEX] = acpi_get_hex(devfn >> 4);
565 ssdt_ptr[ACPI_PCIQXL_OFFSET_HEX + 1] = acpi_get_hex(devfn);
566 ssdt_ptr[ACPI_PCIQXL_OFFSET_ADR + 2] = slot;
567}
568
99fd437d
MT
569/* Assign BSEL property to all buses. In the future, this can be changed
570 * to only assign to buses that support hotplug.
571 */
572static void *acpi_set_bsel(PCIBus *bus, void *opaque)
573{
574 unsigned *bsel_alloc = opaque;
575 unsigned *bus_bsel;
576
39b888bd 577 if (qbus_is_hotpluggable(BUS(bus))) {
99fd437d
MT
578 bus_bsel = g_malloc(sizeof *bus_bsel);
579
580 *bus_bsel = (*bsel_alloc)++;
581 object_property_add_uint32_ptr(OBJECT(bus), ACPI_PCIHP_PROP_BSEL,
582 bus_bsel, NULL);
583 }
584
585 return bsel_alloc;
586}
587
588static void acpi_set_pci_info(void)
589{
590 PCIBus *bus = find_i440fx(); /* TODO: Q35 support */
591 unsigned bsel_alloc = 0;
592
593 if (bus) {
594 /* Scan all PCI buses. Set property to enable acpi based hotplug. */
595 pci_for_each_bus_depth_first(bus, acpi_set_bsel, NULL, &bsel_alloc);
596 }
597}
598
599static void build_pci_bus_state_init(AcpiBuildPciBusHotplugState *state,
133a2da4
IM
600 AcpiBuildPciBusHotplugState *parent,
601 bool pcihp_bridge_en)
99fd437d
MT
602{
603 state->parent = parent;
604 state->device_table = build_alloc_array();
605 state->notify_table = build_alloc_array();
133a2da4 606 state->pcihp_bridge_en = pcihp_bridge_en;
99fd437d
MT
607}
608
609static void build_pci_bus_state_cleanup(AcpiBuildPciBusHotplugState *state)
610{
611 build_free_array(state->device_table);
612 build_free_array(state->notify_table);
613}
614
615static void *build_pci_bus_begin(PCIBus *bus, void *parent_state)
616{
617 AcpiBuildPciBusHotplugState *parent = parent_state;
618 AcpiBuildPciBusHotplugState *child = g_malloc(sizeof *child);
619
133a2da4 620 build_pci_bus_state_init(child, parent, parent->pcihp_bridge_en);
99fd437d
MT
621
622 return child;
623}
624
625static void build_pci_bus_end(PCIBus *bus, void *bus_state)
626{
627 AcpiBuildPciBusHotplugState *child = bus_state;
628 AcpiBuildPciBusHotplugState *parent = child->parent;
629 GArray *bus_table = build_alloc_array();
630 DECLARE_BITMAP(slot_hotplug_enable, PCI_SLOT_MAX);
8dcf525a
MT
631 DECLARE_BITMAP(slot_device_present, PCI_SLOT_MAX);
632 DECLARE_BITMAP(slot_device_system, PCI_SLOT_MAX);
633 DECLARE_BITMAP(slot_device_vga, PCI_SLOT_MAX);
634 DECLARE_BITMAP(slot_device_qxl, PCI_SLOT_MAX);
99fd437d
MT
635 uint8_t op;
636 int i;
637 QObject *bsel;
638 GArray *method;
639 bool bus_hotplug_support = false;
640
133a2da4 641 /*
093a35e5
MT
642 * Skip bridge subtree creation if bridge hotplug is disabled
643 * to make acpi tables compatible with legacy machine types.
a20275fa 644 * Skip creation for hotplugged bridges as well.
093a35e5 645 */
a20275fa
MT
646 if (bus->parent_dev && (!child->pcihp_bridge_en ||
647 DEVICE(bus->parent_dev)->hotplugged)) {
16771613
MT
648 build_free_array(bus_table);
649 build_pci_bus_state_cleanup(child);
650 g_free(child);
133a2da4
IM
651 return;
652 }
653
99fd437d
MT
654 if (bus->parent_dev) {
655 op = 0x82; /* DeviceOp */
eae8bded 656 build_append_namestring(bus_table, "S%.02X",
99fd437d
MT
657 bus->parent_dev->devfn);
658 build_append_byte(bus_table, 0x08); /* NameOp */
eae8bded 659 build_append_namestring(bus_table, "_SUN");
295a515d 660 build_append_int(bus_table, PCI_SLOT(bus->parent_dev->devfn));
99fd437d 661 build_append_byte(bus_table, 0x08); /* NameOp */
eae8bded 662 build_append_namestring(bus_table, "_ADR");
295a515d
IM
663 build_append_int(bus_table, (PCI_SLOT(bus->parent_dev->devfn) << 16) |
664 PCI_FUNC(bus->parent_dev->devfn));
99fd437d
MT
665 } else {
666 op = 0x10; /* ScopeOp */;
eae8bded 667 build_append_namestring(bus_table, "PCI0");
99fd437d 668 }
72c194f7 669
99fd437d
MT
670 bsel = object_property_get_qobject(OBJECT(bus), ACPI_PCIHP_PROP_BSEL, NULL);
671 if (bsel) {
672 build_append_byte(bus_table, 0x08); /* NameOp */
eae8bded 673 build_append_namestring(bus_table, "BSEL");
99fd437d 674 build_append_int(bus_table, qint_get_int(qobject_to_qint(bsel)));
99fd437d 675 memset(slot_hotplug_enable, 0xff, sizeof slot_hotplug_enable);
8dcf525a
MT
676 } else {
677 /* No bsel - no slots are hot-pluggable */
678 memset(slot_hotplug_enable, 0x00, sizeof slot_hotplug_enable);
679 }
99fd437d 680
8dcf525a
MT
681 memset(slot_device_present, 0x00, sizeof slot_device_present);
682 memset(slot_device_system, 0x00, sizeof slot_device_present);
683 memset(slot_device_vga, 0x00, sizeof slot_device_vga);
684 memset(slot_device_qxl, 0x00, sizeof slot_device_qxl);
99fd437d 685
8dcf525a
MT
686 for (i = 0; i < ARRAY_SIZE(bus->devices); i += PCI_FUNC_MAX) {
687 DeviceClass *dc;
688 PCIDeviceClass *pc;
689 PCIDevice *pdev = bus->devices[i];
690 int slot = PCI_SLOT(i);
093a35e5 691 bool bridge_in_acpi;
99fd437d 692
8dcf525a
MT
693 if (!pdev) {
694 continue;
695 }
99fd437d 696
8dcf525a
MT
697 set_bit(slot, slot_device_present);
698 pc = PCI_DEVICE_GET_CLASS(pdev);
699 dc = DEVICE_GET_CLASS(pdev);
99fd437d 700
093a35e5
MT
701 /* When hotplug for bridges is enabled, bridges are
702 * described in ACPI separately (see build_pci_bus_end).
703 * In this case they aren't themselves hot-pluggable.
a20275fa 704 * Hotplugged bridges *are* hot-pluggable.
093a35e5 705 */
a20275fa
MT
706 bridge_in_acpi = pc->is_bridge && child->pcihp_bridge_en &&
707 !DEVICE(pdev)->hotplugged;
093a35e5
MT
708
709 if (pc->class_id == PCI_CLASS_BRIDGE_ISA || bridge_in_acpi) {
8dcf525a 710 set_bit(slot, slot_device_system);
99fd437d
MT
711 }
712
8dcf525a
MT
713 if (pc->class_id == PCI_CLASS_DISPLAY_VGA) {
714 set_bit(slot, slot_device_vga);
715
716 if (object_dynamic_cast(OBJECT(pdev), "qxl-vga")) {
717 set_bit(slot, slot_device_qxl);
99fd437d
MT
718 }
719 }
720
093a35e5 721 if (!dc->hotpluggable || bridge_in_acpi) {
8dcf525a
MT
722 clear_bit(slot, slot_hotplug_enable);
723 }
724 }
725
726 /* Append Device object for each slot */
727 for (i = 0; i < PCI_SLOT_MAX; i++) {
728 bool can_eject = test_bit(i, slot_hotplug_enable);
729 bool present = test_bit(i, slot_device_present);
730 bool vga = test_bit(i, slot_device_vga);
731 bool qxl = test_bit(i, slot_device_qxl);
732 bool system = test_bit(i, slot_device_system);
733 if (can_eject) {
734 void *pcihp = acpi_data_push(bus_table,
735 ACPI_PCIHP_SIZEOF);
736 memcpy(pcihp, ACPI_PCIHP_AML, ACPI_PCIHP_SIZEOF);
737 patch_pcihp(i, pcihp);
738 bus_hotplug_support = true;
739 } else if (qxl) {
740 void *pcihp = acpi_data_push(bus_table,
741 ACPI_PCIQXL_SIZEOF);
742 memcpy(pcihp, ACPI_PCIQXL_AML, ACPI_PCIQXL_SIZEOF);
743 patch_pciqxl(i, pcihp);
744 } else if (vga) {
745 void *pcihp = acpi_data_push(bus_table,
746 ACPI_PCIVGA_SIZEOF);
747 memcpy(pcihp, ACPI_PCIVGA_AML, ACPI_PCIVGA_SIZEOF);
748 patch_pcivga(i, pcihp);
749 } else if (system) {
b89834f4 750 /* Nothing to do: system devices are in DSDT or in SSDT above. */
8dcf525a
MT
751 } else if (present) {
752 void *pcihp = acpi_data_push(bus_table,
753 ACPI_PCINOHP_SIZEOF);
754 memcpy(pcihp, ACPI_PCINOHP_AML, ACPI_PCINOHP_SIZEOF);
755 patch_pcinohp(i, pcihp);
756 }
757 }
758
759 if (bsel) {
99fd437d
MT
760 method = build_alloc_method("DVNT", 2);
761
762 for (i = 0; i < PCI_SLOT_MAX; i++) {
763 GArray *notify;
764 uint8_t op;
765
766 if (!test_bit(i, slot_hotplug_enable)) {
767 continue;
768 }
769
770 notify = build_alloc_array();
771 op = 0xA0; /* IfOp */
772
773 build_append_byte(notify, 0x7B); /* AndOp */
774 build_append_byte(notify, 0x68); /* Arg0Op */
d9631b90 775 build_append_int(notify, 0x1U << i);
99fd437d
MT
776 build_append_byte(notify, 0x00); /* NullName */
777 build_append_byte(notify, 0x86); /* NotifyOp */
eae8bded 778 build_append_namestring(notify, "S%.02X", PCI_DEVFN(i, 0));
99fd437d
MT
779 build_append_byte(notify, 0x69); /* Arg1Op */
780
781 /* Pack it up */
661875e9 782 build_package(notify, op);
99fd437d
MT
783
784 build_append_array(method, notify);
785
786 build_free_array(notify);
787 }
788
789 build_append_and_cleanup_method(bus_table, method);
790 }
791
792 /* Append PCNT method to notify about events on local and child buses.
793 * Add unconditionally for root since DSDT expects it.
72c194f7 794 */
99fd437d
MT
795 if (bus_hotplug_support || child->notify_table->len || !bus->parent_dev) {
796 method = build_alloc_method("PCNT", 0);
797
798 /* If bus supports hotplug select it and notify about local events */
799 if (bsel) {
800 build_append_byte(method, 0x70); /* StoreOp */
801 build_append_int(method, qint_get_int(qobject_to_qint(bsel)));
eae8bded
IM
802 build_append_namestring(method, "BNUM");
803 build_append_namestring(method, "DVNT");
804 build_append_namestring(method, "PCIU");
99fd437d 805 build_append_int(method, 1); /* Device Check */
eae8bded
IM
806 build_append_namestring(method, "DVNT");
807 build_append_namestring(method, "PCID");
99fd437d
MT
808 build_append_int(method, 3); /* Eject Request */
809 }
810
811 /* Notify about child bus events in any case */
812 build_append_array(method, child->notify_table);
813
814 build_append_and_cleanup_method(bus_table, method);
815
816 /* Append description of child buses */
817 build_append_array(bus_table, child->device_table);
818
819 /* Pack it up */
820 if (bus->parent_dev) {
821 build_extop_package(bus_table, op);
822 } else {
661875e9 823 build_package(bus_table, op);
99fd437d 824 }
72c194f7 825
99fd437d
MT
826 /* Append our bus description to parent table */
827 build_append_array(parent->device_table, bus_table);
828
829 /* Also tell parent how to notify us, invoking PCNT method.
830 * At the moment this is not needed for root as we have a single root.
831 */
832 if (bus->parent_dev) {
eae8bded
IM
833 build_append_namestring(parent->notify_table, "^PCNT.S%.02X",
834 bus->parent_dev->devfn);
99fd437d 835 }
72c194f7 836 }
99fd437d 837
097a97a6 838 qobject_decref(bsel);
99fd437d
MT
839 build_free_array(bus_table);
840 build_pci_bus_state_cleanup(child);
841 g_free(child);
72c194f7
MT
842}
843
72c194f7
MT
844static void
845build_ssdt(GArray *table_data, GArray *linker,
846 AcpiCpuInfo *cpu, AcpiPmInfo *pm, AcpiMiscInfo *misc,
847 PcPciInfo *pci, PcGuestInfo *guest_info)
848{
bef3492d
IM
849 MachineState *machine = MACHINE(qdev_get_machine());
850 uint32_t nr_mem = machine->ram_slots;
2fd71f1b 851 unsigned acpi_cpus = guest_info->apic_id_limit;
20843d16 852 Aml *ssdt, *sb_scope, *scope, *pkg, *dev, *method, *crs, *field, *ifctx;
72c194f7
MT
853 int i;
854
011bb749 855 ssdt = init_aml_allocator();
2fd71f1b
LE
856 /* The current AML generator can cover the APIC ID range [0..255],
857 * inclusive, for VCPU hotplug. */
858 QEMU_BUILD_BUG_ON(ACPI_CPU_HOTPLUG_ID_LIMIT > 256);
859 g_assert(acpi_cpus <= ACPI_CPU_HOTPLUG_ID_LIMIT);
860
4ec8d2b3
IM
861 /* Reserve space for header */
862 acpi_data_push(ssdt->buf, sizeof(AcpiTableHeader));
72c194f7 863
500b11ea 864 scope = aml_scope("\\_SB.PCI0");
60efd429
IM
865 /* build PCI0._CRS */
866 crs = aml_resource_template();
867 aml_append(crs,
868 aml_word_bus_number(aml_min_fixed, aml_max_fixed, aml_pos_decode,
869 0x0000, 0x0000, 0x00FF, 0x0000, 0x0100));
870 aml_append(crs, aml_io(aml_decode16, 0x0CF8, 0x0CF8, 0x01, 0x08));
871
872 aml_append(crs,
873 aml_word_io(aml_min_fixed, aml_max_fixed,
874 aml_pos_decode, aml_entire_range,
875 0x0000, 0x0000, 0x0CF7, 0x0000, 0x0CF8));
d31c909e
IM
876 aml_append(crs,
877 aml_word_io(aml_min_fixed, aml_max_fixed,
878 aml_pos_decode, aml_entire_range,
879 0x0000, 0x0D00, 0xFFFF, 0x0000, 0xF300));
60efd429
IM
880 aml_append(crs,
881 aml_dword_memory(aml_pos_decode, aml_min_fixed, aml_max_fixed,
882 aml_cacheable, aml_ReadWrite,
883 0, 0x000A0000, 0x000BFFFF, 0, 0x00020000));
884 aml_append(crs,
885 aml_dword_memory(aml_pos_decode, aml_min_fixed, aml_max_fixed,
886 aml_non_cacheable, aml_ReadWrite,
887 0, pci->w32.begin, pci->w32.end - 1, 0,
888 pci->w32.end - pci->w32.begin));
889 if (pci->w64.begin) {
890 aml_append(crs,
891 aml_qword_memory(aml_pos_decode, aml_min_fixed, aml_max_fixed,
892 aml_cacheable, aml_ReadWrite,
893 0, pci->w64.begin, pci->w64.end - 1, 0,
894 pci->w64.end - pci->w64.begin));
895 }
896 aml_append(scope, aml_name_decl("_CRS", crs));
897
d31c909e
IM
898 /* reserve GPE0 block resources */
899 dev = aml_device("GPE0");
900 aml_append(dev, aml_name_decl("_HID", aml_string("PNP0A06")));
901 aml_append(dev, aml_name_decl("_UID", aml_string("GPE0 resources")));
902 /* device present, functioning, decoding, not shown in UI */
903 aml_append(dev, aml_name_decl("_STA", aml_int(0xB)));
904 crs = aml_resource_template();
905 aml_append(crs,
906 aml_io(aml_decode16, pm->gpe0_blk, pm->gpe0_blk, 1, pm->gpe0_blk_len)
907 );
908 aml_append(dev, aml_name_decl("_CRS", crs));
909 aml_append(scope, dev);
910
500b11ea
IM
911 /* reserve PCIHP resources */
912 if (pm->pcihp_io_len) {
913 dev = aml_device("PHPR");
914 aml_append(dev, aml_name_decl("_HID", aml_string("PNP0A06")));
915 aml_append(dev,
916 aml_name_decl("_UID", aml_string("PCI Hotplug resources")));
917 /* device present, functioning, decoding, not shown in UI */
918 aml_append(dev, aml_name_decl("_STA", aml_int(0xB)));
919 crs = aml_resource_template();
920 aml_append(crs,
921 aml_io(aml_decode16, pm->pcihp_io_base, pm->pcihp_io_base, 1,
922 pm->pcihp_io_len)
923 );
924 aml_append(dev, aml_name_decl("_CRS", crs));
925 aml_append(scope, dev);
926 }
927 aml_append(ssdt, scope);
928
ebc3028f
IM
929 /* create S3_ / S4_ / S5_ packages if necessary */
930 scope = aml_scope("\\");
931 if (!pm->s3_disabled) {
932 pkg = aml_package(4);
933 aml_append(pkg, aml_int(1)); /* PM1a_CNT.SLP_TYP */
934 aml_append(pkg, aml_int(1)); /* PM1b_CNT.SLP_TYP, FIXME: not impl. */
935 aml_append(pkg, aml_int(0)); /* reserved */
936 aml_append(pkg, aml_int(0)); /* reserved */
937 aml_append(scope, aml_name_decl("_S3", pkg));
938 }
939
940 if (!pm->s4_disabled) {
941 pkg = aml_package(4);
942 aml_append(pkg, aml_int(pm->s4_val)); /* PM1a_CNT.SLP_TYP */
943 /* PM1b_CNT.SLP_TYP, FIXME: not impl. */
944 aml_append(pkg, aml_int(pm->s4_val));
945 aml_append(pkg, aml_int(0)); /* reserved */
946 aml_append(pkg, aml_int(0)); /* reserved */
947 aml_append(scope, aml_name_decl("_S4", pkg));
948 }
949
950 pkg = aml_package(4);
951 aml_append(pkg, aml_int(0)); /* PM1a_CNT.SLP_TYP */
952 aml_append(pkg, aml_int(0)); /* PM1b_CNT.SLP_TYP not impl. */
953 aml_append(pkg, aml_int(0)); /* reserved */
954 aml_append(pkg, aml_int(0)); /* reserved */
955 aml_append(scope, aml_name_decl("_S5", pkg));
956 aml_append(ssdt, scope);
957
cd61cb2e
IM
958 if (misc->pvpanic_port) {
959 scope = aml_scope("\\_SB.PCI0.ISA");
960
961 dev = aml_device("PEVR");
962 aml_append(dev, aml_name_decl("_HID", aml_string("QEMU0002")));
963
964 crs = aml_resource_template();
965 aml_append(crs,
966 aml_io(aml_decode16, misc->pvpanic_port, misc->pvpanic_port, 1, 1)
967 );
968 aml_append(dev, aml_name_decl("_CRS", crs));
969
970 aml_append(dev, aml_operation_region("PEOR", aml_system_io,
971 misc->pvpanic_port, 1));
972 field = aml_field("PEOR", aml_byte_acc);
973 aml_append(field, aml_named_field("PEPT", 8));
974 aml_append(dev, field);
975
976 method = aml_method("RDPT", 0);
977 aml_append(method, aml_store(aml_name("PEPT"), aml_local(0)));
978 aml_append(method, aml_return(aml_local(0)));
979 aml_append(dev, method);
980
981 method = aml_method("WRPT", 1);
982 aml_append(method, aml_store(aml_arg(0), aml_name("PEPT")));
983 aml_append(dev, method);
984
985 aml_append(scope, dev);
986 aml_append(ssdt, scope);
987 }
988
011bb749 989 sb_scope = aml_scope("_SB");
72c194f7 990 {
ddf1ec2f
IM
991 /* create PCI0.PRES device and its _CRS to reserve CPU hotplug MMIO */
992 dev = aml_device("PCI0." stringify(CPU_HOTPLUG_RESOURCE_DEVICE));
993 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A06")));
994 aml_append(dev,
995 aml_name_decl("_UID", aml_string("CPU Hotplug resources"))
996 );
997 /* device present, functioning, decoding, not shown in UI */
998 aml_append(dev, aml_name_decl("_STA", aml_int(0xB)));
999 crs = aml_resource_template();
1000 aml_append(crs,
1001 aml_io(aml_decode16, pm->cpu_hp_io_base, pm->cpu_hp_io_base, 1,
1002 pm->cpu_hp_io_len)
1003 );
1004 aml_append(dev, aml_name_decl("_CRS", crs));
1005 aml_append(sb_scope, dev);
1006 /* declare CPU hotplug MMIO region and PRS field to access it */
1007 aml_append(sb_scope, aml_operation_region(
1008 "PRST", aml_system_io, pm->cpu_hp_io_base, pm->cpu_hp_io_len));
1009 field = aml_field("PRST", aml_byte_acc);
1010 aml_append(field, aml_named_field("PRS", 256));
1011 aml_append(sb_scope, field);
1012
72c194f7
MT
1013 /* build Processor object for each processor */
1014 for (i = 0; i < acpi_cpus; i++) {
20843d16
IM
1015 dev = aml_processor(i, 0, 0, "CP%.02X", i);
1016
1017 method = aml_method("_MAT", 0);
1018 aml_append(method, aml_return(aml_call1("CPMA", aml_int(i))));
1019 aml_append(dev, method);
1020
1021 method = aml_method("_STA", 0);
1022 aml_append(method, aml_return(aml_call1("CPST", aml_int(i))));
1023 aml_append(dev, method);
1024
1025 method = aml_method("_EJ0", 1);
1026 aml_append(method,
1027 aml_return(aml_call2("CPEJ", aml_int(i), aml_arg(0)))
1028 );
1029 aml_append(dev, method);
1030
1031 aml_append(sb_scope, dev);
72c194f7
MT
1032 }
1033
1034 /* build this code:
1035 * Method(NTFY, 2) {If (LEqual(Arg0, 0x00)) {Notify(CP00, Arg1)} ...}
1036 */
1037 /* Arg0 = Processor ID = APIC ID */
20843d16
IM
1038 method = aml_method("NTFY", 2);
1039 for (i = 0; i < acpi_cpus; i++) {
1040 ifctx = aml_if(aml_equal(aml_arg(0), aml_int(i)));
1041 aml_append(ifctx,
1042 aml_notify(aml_name("CP%.02X", i), aml_arg(1))
1043 );
1044 aml_append(method, ifctx);
1045 }
1046 aml_append(sb_scope, method);
1047
1048 /* build "Name(CPON, Package() { One, One, ..., Zero, Zero, ... })"
1049 *
1050 * Note: The ability to create variable-sized packages was first
1051 * ntroduced in ACPI 2.0. ACPI 1.0 only allowed fixed-size packages
1052 * ith up to 255 elements. Windows guests up to win2k8 fail when
1053 * VarPackageOp is used.
1054 */
1055 pkg = acpi_cpus <= 255 ? aml_package(acpi_cpus) :
1056 aml_varpackage(acpi_cpus);
72c194f7 1057
20843d16
IM
1058 for (i = 0; i < acpi_cpus; i++) {
1059 uint8_t b = test_bit(i, cpu->found_cpus) ? 0x01 : 0x00;
1060 aml_append(pkg, aml_int(b));
72c194f7 1061 }
20843d16 1062 aml_append(sb_scope, aml_name_decl("CPON", pkg));
72c194f7 1063
8698c0c0
IM
1064 /* build memory devices */
1065 assert(nr_mem <= ACPI_MAX_RAM_SLOTS);
2c6b94d8
IM
1066 scope = aml_scope("\\_SB.PCI0." stringify(MEMORY_HOTPLUG_DEVICE));
1067 aml_append(scope,
1068 aml_name_decl(stringify(MEMORY_SLOTS_NUMBER), aml_int(nr_mem))
1069 );
1070
1071 crs = aml_resource_template();
1072 aml_append(crs,
1073 aml_io(aml_decode16, pm->mem_hp_io_base, pm->mem_hp_io_base, 0,
1074 pm->mem_hp_io_len)
1075 );
1076 aml_append(scope, aml_name_decl("_CRS", crs));
1077
1078 aml_append(scope, aml_operation_region(
1079 stringify(MEMORY_HOTPLUG_IO_REGION), aml_system_io,
1080 pm->mem_hp_io_base, pm->mem_hp_io_len)
1081 );
1082
1083 field = aml_field(stringify(MEMORY_HOTPLUG_IO_REGION), aml_dword_acc);
1084 aml_append(field, /* read only */
1085 aml_named_field(stringify(MEMORY_SLOT_ADDR_LOW), 32));
1086 aml_append(field, /* read only */
1087 aml_named_field(stringify(MEMORY_SLOT_ADDR_HIGH), 32));
1088 aml_append(field, /* read only */
1089 aml_named_field(stringify(MEMORY_SLOT_SIZE_LOW), 32));
1090 aml_append(field, /* read only */
1091 aml_named_field(stringify(MEMORY_SLOT_SIZE_HIGH), 32));
1092 aml_append(field, /* read only */
1093 aml_named_field(stringify(MEMORY_SLOT_PROXIMITY), 32));
1094 aml_append(scope, field);
1095
1096 field = aml_field(stringify(MEMORY_HOTPLUG_IO_REGION), aml_byte_acc);
1097 aml_append(field, aml_reserved_field(160 /* bits, Offset(20) */));
1098 aml_append(field, /* 1 if enabled, read only */
1099 aml_named_field(stringify(MEMORY_SLOT_ENABLED), 1));
1100 aml_append(field,
1101 /*(read) 1 if has a insert event. (write) 1 to clear event */
1102 aml_named_field(stringify(MEMORY_SLOT_INSERT_EVENT), 1));
1103 aml_append(scope, field);
1104
1105 field = aml_field(stringify(MEMORY_HOTPLUG_IO_REGION), aml_dword_acc);
1106 aml_append(field, /* DIMM selector, write only */
1107 aml_named_field(stringify(MEMORY_SLOT_SLECTOR), 32));
1108 aml_append(field, /* _OST event code, write only */
1109 aml_named_field(stringify(MEMORY_SLOT_OST_EVENT), 32));
1110 aml_append(field, /* _OST status code, write only */
1111 aml_named_field(stringify(MEMORY_SLOT_OST_STATUS), 32));
1112 aml_append(scope, field);
1113
1114 aml_append(sb_scope, scope);
8698c0c0
IM
1115
1116 for (i = 0; i < nr_mem; i++) {
1117 #define BASEPATH "\\_SB.PCI0." stringify(MEMORY_HOTPLUG_DEVICE) "."
1118 const char *s;
1119
1120 dev = aml_device("MP%02X", i);
1121 aml_append(dev, aml_name_decl("_UID", aml_string("0x%02X", i)));
1122 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C80")));
bef3492d 1123
8698c0c0
IM
1124 method = aml_method("_CRS", 0);
1125 s = BASEPATH stringify(MEMORY_SLOT_CRS_METHOD);
1126 aml_append(method, aml_return(aml_call1(s, aml_name("_UID"))));
1127 aml_append(dev, method);
1128
1129 method = aml_method("_STA", 0);
1130 s = BASEPATH stringify(MEMORY_SLOT_STATUS_METHOD);
1131 aml_append(method, aml_return(aml_call1(s, aml_name("_UID"))));
1132 aml_append(dev, method);
1133
1134 method = aml_method("_PXM", 0);
1135 s = BASEPATH stringify(MEMORY_SLOT_PROXIMITY_METHOD);
1136 aml_append(method, aml_return(aml_call1(s, aml_name("_UID"))));
1137 aml_append(dev, method);
1138
1139 method = aml_method("_OST", 3);
1140 s = BASEPATH stringify(MEMORY_SLOT_OST_METHOD);
1141 aml_append(method, aml_return(aml_call4(
1142 s, aml_name("_UID"), aml_arg(0), aml_arg(1), aml_arg(2)
1143 )));
1144 aml_append(dev, method);
1145
1146 aml_append(sb_scope, dev);
bef3492d
IM
1147 }
1148
8698c0c0
IM
1149 /* build Method(MEMORY_SLOT_NOTIFY_METHOD, 2) {
1150 * If (LEqual(Arg0, 0x00)) {Notify(MP00, Arg1)} ...
1151 */
1152 method = aml_method(stringify(MEMORY_SLOT_NOTIFY_METHOD), 2);
1153 for (i = 0; i < nr_mem; i++) {
1154 ifctx = aml_if(aml_equal(aml_arg(0), aml_int(i)));
1155 aml_append(ifctx,
1156 aml_notify(aml_name("MP%.02X", i), aml_arg(1))
1157 );
1158 aml_append(method, ifctx);
1159 }
1160 aml_append(sb_scope, method);
1161
72c194f7 1162 {
99fd437d 1163 AcpiBuildPciBusHotplugState hotplug_state;
8dcf525a
MT
1164 Object *pci_host;
1165 PCIBus *bus = NULL;
1166 bool ambiguous;
1167
1168 pci_host = object_resolve_path_type("", TYPE_PCI_HOST_BRIDGE, &ambiguous);
1169 if (!ambiguous && pci_host) {
1170 bus = PCI_HOST_BRIDGE(pci_host)->bus;
1171 }
72c194f7 1172
133a2da4 1173 build_pci_bus_state_init(&hotplug_state, NULL, pm->pcihp_bridge_en);
72c194f7 1174
99fd437d
MT
1175 if (bus) {
1176 /* Scan all PCI buses. Generate tables to support hotplug. */
1177 pci_for_each_bus_depth_first(bus, build_pci_bus_begin,
1178 build_pci_bus_end, &hotplug_state);
72c194f7
MT
1179 }
1180
011bb749 1181 build_append_array(sb_scope->buf, hotplug_state.device_table);
99fd437d 1182 build_pci_bus_state_cleanup(&hotplug_state);
72c194f7 1183 }
011bb749 1184 aml_append(ssdt, sb_scope);
72c194f7
MT
1185 }
1186
011bb749
IM
1187 /* copy AML table into ACPI tables blob and patch header there */
1188 g_array_append_vals(table_data, ssdt->buf->data, ssdt->buf->len);
72c194f7 1189 build_header(linker, table_data,
011bb749
IM
1190 (void *)(table_data->data + table_data->len - ssdt->buf->len),
1191 "SSDT", ssdt->buf->len, 1);
1192 free_aml_allocator();
72c194f7
MT
1193}
1194
1195static void
1196build_hpet(GArray *table_data, GArray *linker)
1197{
1198 Acpi20Hpet *hpet;
1199
1200 hpet = acpi_data_push(table_data, sizeof(*hpet));
1201 /* Note timer_block_id value must be kept in sync with value advertised by
1202 * emulated hpet
1203 */
1204 hpet->timer_block_id = cpu_to_le32(0x8086a201);
1205 hpet->addr.address = cpu_to_le64(HPET_BASE);
1206 build_header(linker, table_data,
821e3227 1207 (void *)hpet, "HPET", sizeof(*hpet), 1);
72c194f7
MT
1208}
1209
711b20b4 1210static void
42a5b308 1211build_tpm_tcpa(GArray *table_data, GArray *linker, GArray *tcpalog)
711b20b4
SB
1212{
1213 Acpi20Tcpa *tcpa = acpi_data_push(table_data, sizeof *tcpa);
42a5b308 1214 uint64_t log_area_start_address = acpi_data_len(tcpalog);
711b20b4
SB
1215
1216 tcpa->platform_class = cpu_to_le16(TPM_TCPA_ACPI_CLASS_CLIENT);
1217 tcpa->log_area_minimum_length = cpu_to_le32(TPM_LOG_AREA_MINIMUM_SIZE);
1218 tcpa->log_area_start_address = cpu_to_le64(log_area_start_address);
1219
42a5b308
SB
1220 bios_linker_loader_alloc(linker, ACPI_BUILD_TPMLOG_FILE, 1,
1221 false /* high memory */);
1222
711b20b4
SB
1223 /* log area start address to be filled by Guest linker */
1224 bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
42a5b308 1225 ACPI_BUILD_TPMLOG_FILE,
711b20b4
SB
1226 table_data, &tcpa->log_area_start_address,
1227 sizeof(tcpa->log_area_start_address));
1228
1229 build_header(linker, table_data,
1230 (void *)tcpa, "TCPA", sizeof(*tcpa), 2);
1231
42a5b308 1232 acpi_data_push(tcpalog, TPM_LOG_AREA_MINIMUM_SIZE);
711b20b4
SB
1233}
1234
1235static void
1236build_tpm_ssdt(GArray *table_data, GArray *linker)
1237{
1238 void *tpm_ptr;
1239
1240 tpm_ptr = acpi_data_push(table_data, sizeof(ssdt_tpm_aml));
1241 memcpy(tpm_ptr, ssdt_tpm_aml, sizeof(ssdt_tpm_aml));
1242}
1243
04ed3ea8
IM
1244typedef enum {
1245 MEM_AFFINITY_NOFLAGS = 0,
1246 MEM_AFFINITY_ENABLED = (1 << 0),
1247 MEM_AFFINITY_HOTPLUGGABLE = (1 << 1),
1248 MEM_AFFINITY_NON_VOLATILE = (1 << 2),
1249} MemoryAffinityFlags;
1250
72c194f7 1251static void
04ed3ea8
IM
1252acpi_build_srat_memory(AcpiSratMemoryAffinity *numamem, uint64_t base,
1253 uint64_t len, int node, MemoryAffinityFlags flags)
72c194f7
MT
1254{
1255 numamem->type = ACPI_SRAT_MEMORY;
1256 numamem->length = sizeof(*numamem);
1257 memset(numamem->proximity, 0, 4);
1258 numamem->proximity[0] = node;
04ed3ea8 1259 numamem->flags = cpu_to_le32(flags);
72c194f7
MT
1260 numamem->base_addr = cpu_to_le64(base);
1261 numamem->range_length = cpu_to_le64(len);
1262}
1263
1264static void
dd0247e0 1265build_srat(GArray *table_data, GArray *linker, PcGuestInfo *guest_info)
72c194f7
MT
1266{
1267 AcpiSystemResourceAffinityTable *srat;
1268 AcpiSratProcessorAffinity *core;
1269 AcpiSratMemoryAffinity *numamem;
1270
1271 int i;
1272 uint64_t curnode;
1273 int srat_start, numa_start, slots;
1274 uint64_t mem_len, mem_base, next_base;
cec65193
IM
1275 PCMachineState *pcms = PC_MACHINE(qdev_get_machine());
1276 ram_addr_t hotplugabble_address_space_size =
1277 object_property_get_int(OBJECT(pcms), PC_MACHINE_MEMHP_REGION_SIZE,
1278 NULL);
72c194f7
MT
1279
1280 srat_start = table_data->len;
1281
1282 srat = acpi_data_push(table_data, sizeof *srat);
1283 srat->reserved1 = cpu_to_le32(1);
1284 core = (void *)(srat + 1);
1285
1286 for (i = 0; i < guest_info->apic_id_limit; ++i) {
1287 core = acpi_data_push(table_data, sizeof *core);
1288 core->type = ACPI_SRAT_PROCESSOR;
1289 core->length = sizeof(*core);
1290 core->local_apic_id = i;
1291 curnode = guest_info->node_cpu[i];
1292 core->proximity_lo = curnode;
1293 memset(core->proximity_hi, 0, 3);
1294 core->local_sapic_eid = 0;
dd0247e0 1295 core->flags = cpu_to_le32(1);
72c194f7
MT
1296 }
1297
1298
1299 /* the memory map is a bit tricky, it contains at least one hole
1300 * from 640k-1M and possibly another one from 3.5G-4G.
1301 */
1302 next_base = 0;
1303 numa_start = table_data->len;
1304
1305 numamem = acpi_data_push(table_data, sizeof *numamem);
04ed3ea8 1306 acpi_build_srat_memory(numamem, 0, 640*1024, 0, MEM_AFFINITY_ENABLED);
72c194f7
MT
1307 next_base = 1024 * 1024;
1308 for (i = 1; i < guest_info->numa_nodes + 1; ++i) {
1309 mem_base = next_base;
1310 mem_len = guest_info->node_mem[i - 1];
1311 if (i == 1) {
1312 mem_len -= 1024 * 1024;
1313 }
1314 next_base = mem_base + mem_len;
1315
1316 /* Cut out the ACPI_PCI hole */
4c8a949b
EH
1317 if (mem_base <= guest_info->ram_size_below_4g &&
1318 next_base > guest_info->ram_size_below_4g) {
1319 mem_len -= next_base - guest_info->ram_size_below_4g;
72c194f7
MT
1320 if (mem_len > 0) {
1321 numamem = acpi_data_push(table_data, sizeof *numamem);
04ed3ea8
IM
1322 acpi_build_srat_memory(numamem, mem_base, mem_len, i - 1,
1323 MEM_AFFINITY_ENABLED);
72c194f7
MT
1324 }
1325 mem_base = 1ULL << 32;
4c8a949b
EH
1326 mem_len = next_base - guest_info->ram_size_below_4g;
1327 next_base += (1ULL << 32) - guest_info->ram_size_below_4g;
72c194f7
MT
1328 }
1329 numamem = acpi_data_push(table_data, sizeof *numamem);
04ed3ea8
IM
1330 acpi_build_srat_memory(numamem, mem_base, mem_len, i - 1,
1331 MEM_AFFINITY_ENABLED);
72c194f7
MT
1332 }
1333 slots = (table_data->len - numa_start) / sizeof *numamem;
1334 for (; slots < guest_info->numa_nodes + 2; slots++) {
1335 numamem = acpi_data_push(table_data, sizeof *numamem);
04ed3ea8 1336 acpi_build_srat_memory(numamem, 0, 0, 0, MEM_AFFINITY_NOFLAGS);
72c194f7
MT
1337 }
1338
cec65193
IM
1339 /*
1340 * Entry is required for Windows to enable memory hotplug in OS.
1341 * Memory devices may override proximity set by this entry,
1342 * providing _PXM method if necessary.
1343 */
1344 if (hotplugabble_address_space_size) {
1345 numamem = acpi_data_push(table_data, sizeof *numamem);
1346 acpi_build_srat_memory(numamem, pcms->hotplug_memory_base,
1347 hotplugabble_address_space_size, 0,
1348 MEM_AFFINITY_HOTPLUGGABLE |
1349 MEM_AFFINITY_ENABLED);
1350 }
1351
72c194f7
MT
1352 build_header(linker, table_data,
1353 (void *)(table_data->data + srat_start),
821e3227 1354 "SRAT",
72c194f7
MT
1355 table_data->len - srat_start, 1);
1356}
1357
1358static void
1359build_mcfg_q35(GArray *table_data, GArray *linker, AcpiMcfgInfo *info)
1360{
1361 AcpiTableMcfg *mcfg;
821e3227 1362 const char *sig;
72c194f7
MT
1363 int len = sizeof(*mcfg) + 1 * sizeof(mcfg->allocation[0]);
1364
1365 mcfg = acpi_data_push(table_data, len);
1366 mcfg->allocation[0].address = cpu_to_le64(info->mcfg_base);
1367 /* Only a single allocation so no need to play with segments */
1368 mcfg->allocation[0].pci_segment = cpu_to_le16(0);
1369 mcfg->allocation[0].start_bus_number = 0;
1370 mcfg->allocation[0].end_bus_number = PCIE_MMCFG_BUS(info->mcfg_size - 1);
1371
1372 /* MCFG is used for ECAM which can be enabled or disabled by guest.
1373 * To avoid table size changes (which create migration issues),
1374 * always create the table even if there are no allocations,
1375 * but set the signature to a reserved value in this case.
1376 * ACPI spec requires OSPMs to ignore such tables.
1377 */
1378 if (info->mcfg_base == PCIE_BASE_ADDR_UNMAPPED) {
821e3227
MT
1379 /* Reserved signature: ignored by OSPM */
1380 sig = "QEMU";
72c194f7 1381 } else {
821e3227 1382 sig = "MCFG";
72c194f7
MT
1383 }
1384 build_header(linker, table_data, (void *)mcfg, sig, len, 1);
1385}
1386
d4eb9119
LT
1387static void
1388build_dmar_q35(GArray *table_data, GArray *linker)
1389{
1390 int dmar_start = table_data->len;
1391
1392 AcpiTableDmar *dmar;
1393 AcpiDmarHardwareUnit *drhd;
1394
1395 dmar = acpi_data_push(table_data, sizeof(*dmar));
1396 dmar->host_address_width = VTD_HOST_ADDRESS_WIDTH - 1;
1397 dmar->flags = 0; /* No intr_remap for now */
1398
1399 /* DMAR Remapping Hardware Unit Definition structure */
1400 drhd = acpi_data_push(table_data, sizeof(*drhd));
1401 drhd->type = cpu_to_le16(ACPI_DMAR_TYPE_HARDWARE_UNIT);
1402 drhd->length = cpu_to_le16(sizeof(*drhd)); /* No device scope now */
1403 drhd->flags = ACPI_DMAR_INCLUDE_PCI_ALL;
1404 drhd->pci_segment = cpu_to_le16(0);
1405 drhd->address = cpu_to_le64(Q35_HOST_BRIDGE_IOMMU_ADDR);
1406
1407 build_header(linker, table_data, (void *)(table_data->data + dmar_start),
1408 "DMAR", table_data->len - dmar_start, 1);
1409}
1410
72c194f7
MT
1411static void
1412build_dsdt(GArray *table_data, GArray *linker, AcpiMiscInfo *misc)
1413{
53db092a
MT
1414 AcpiTableHeader *dsdt;
1415
72c194f7 1416 assert(misc->dsdt_code && misc->dsdt_size);
53db092a 1417
72c194f7
MT
1418 dsdt = acpi_data_push(table_data, misc->dsdt_size);
1419 memcpy(dsdt, misc->dsdt_code, misc->dsdt_size);
53db092a
MT
1420
1421 memset(dsdt, 0, sizeof *dsdt);
821e3227 1422 build_header(linker, table_data, dsdt, "DSDT",
53db092a 1423 misc->dsdt_size, 1);
72c194f7
MT
1424}
1425
1426/* Build final rsdt table */
1427static void
1428build_rsdt(GArray *table_data, GArray *linker, GArray *table_offsets)
1429{
1430 AcpiRsdtDescriptorRev1 *rsdt;
1431 size_t rsdt_len;
1432 int i;
1433
1434 rsdt_len = sizeof(*rsdt) + sizeof(uint32_t) * table_offsets->len;
1435 rsdt = acpi_data_push(table_data, rsdt_len);
1436 memcpy(rsdt->table_offset_entry, table_offsets->data,
1437 sizeof(uint32_t) * table_offsets->len);
1438 for (i = 0; i < table_offsets->len; ++i) {
1439 /* rsdt->table_offset_entry to be filled by Guest linker */
1440 bios_linker_loader_add_pointer(linker,
1441 ACPI_BUILD_TABLE_FILE,
1442 ACPI_BUILD_TABLE_FILE,
1443 table_data, &rsdt->table_offset_entry[i],
1444 sizeof(uint32_t));
1445 }
1446 build_header(linker, table_data,
821e3227 1447 (void *)rsdt, "RSDT", rsdt_len, 1);
72c194f7
MT
1448}
1449
1450static GArray *
1451build_rsdp(GArray *rsdp_table, GArray *linker, unsigned rsdt)
1452{
1453 AcpiRsdpDescriptor *rsdp = acpi_data_push(rsdp_table, sizeof *rsdp);
1454
d67aadcc 1455 bios_linker_loader_alloc(linker, ACPI_BUILD_RSDP_FILE, 16,
72c194f7
MT
1456 true /* fseg memory */);
1457
821e3227 1458 memcpy(&rsdp->signature, "RSD PTR ", 8);
72c194f7
MT
1459 memcpy(rsdp->oem_id, ACPI_BUILD_APPNAME6, 6);
1460 rsdp->rsdt_physical_address = cpu_to_le32(rsdt);
1461 /* Address to be filled by Guest linker */
1462 bios_linker_loader_add_pointer(linker, ACPI_BUILD_RSDP_FILE,
1463 ACPI_BUILD_TABLE_FILE,
1464 rsdp_table, &rsdp->rsdt_physical_address,
1465 sizeof rsdp->rsdt_physical_address);
1466 rsdp->checksum = 0;
1467 /* Checksum to be filled by Guest linker */
1468 bios_linker_loader_add_checksum(linker, ACPI_BUILD_RSDP_FILE,
1469 rsdp, rsdp, sizeof *rsdp, &rsdp->checksum);
1470
1471 return rsdp_table;
1472}
1473
1474typedef
1475struct AcpiBuildTables {
1476 GArray *table_data;
1477 GArray *rsdp;
42a5b308 1478 GArray *tcpalog;
72c194f7
MT
1479 GArray *linker;
1480} AcpiBuildTables;
1481
1482static inline void acpi_build_tables_init(AcpiBuildTables *tables)
1483{
1484 tables->rsdp = g_array_new(false, true /* clear */, 1);
1485 tables->table_data = g_array_new(false, true /* clear */, 1);
42a5b308 1486 tables->tcpalog = g_array_new(false, true /* clear */, 1);
72c194f7
MT
1487 tables->linker = bios_linker_loader_init();
1488}
1489
1490static inline void acpi_build_tables_cleanup(AcpiBuildTables *tables, bool mfre)
1491{
1492 void *linker_data = bios_linker_loader_cleanup(tables->linker);
ac369a77 1493 g_free(linker_data);
afaa2e4b 1494 g_array_free(tables->rsdp, true);
ac369a77 1495 g_array_free(tables->table_data, true);
42a5b308 1496 g_array_free(tables->tcpalog, mfre);
72c194f7
MT
1497}
1498
1499typedef
1500struct AcpiBuildState {
1501 /* Copy of table in RAM (for patching). */
ad5b88b1 1502 ram_addr_t table_ram;
72c194f7
MT
1503 /* Is table patched? */
1504 uint8_t patched;
1505 PcGuestInfo *guest_info;
d70414a5 1506 void *rsdp;
42d85900 1507 ram_addr_t rsdp_ram;
6e00619b 1508 ram_addr_t linker_ram;
72c194f7
MT
1509} AcpiBuildState;
1510
1511static bool acpi_get_mcfg(AcpiMcfgInfo *mcfg)
1512{
1513 Object *pci_host;
1514 QObject *o;
1515 bool ambiguous;
1516
1517 pci_host = object_resolve_path_type("", TYPE_PCI_HOST_BRIDGE, &ambiguous);
1518 g_assert(!ambiguous);
1519 g_assert(pci_host);
1520
1521 o = object_property_get_qobject(pci_host, PCIE_HOST_MCFG_BASE, NULL);
1522 if (!o) {
1523 return false;
1524 }
1525 mcfg->mcfg_base = qint_get_int(qobject_to_qint(o));
097a97a6 1526 qobject_decref(o);
72c194f7
MT
1527
1528 o = object_property_get_qobject(pci_host, PCIE_HOST_MCFG_SIZE, NULL);
1529 assert(o);
1530 mcfg->mcfg_size = qint_get_int(qobject_to_qint(o));
097a97a6 1531 qobject_decref(o);
72c194f7
MT
1532 return true;
1533}
1534
d4eb9119
LT
1535static bool acpi_has_iommu(void)
1536{
1537 bool ambiguous;
1538 Object *intel_iommu;
1539
1540 intel_iommu = object_resolve_path_type("", TYPE_INTEL_IOMMU_DEVICE,
1541 &ambiguous);
1542 return intel_iommu && !ambiguous;
1543}
1544
72c194f7
MT
1545static
1546void acpi_build(PcGuestInfo *guest_info, AcpiBuildTables *tables)
1547{
1548 GArray *table_offsets;
07fb6176 1549 unsigned facs, ssdt, dsdt, rsdt;
72c194f7
MT
1550 AcpiCpuInfo cpu;
1551 AcpiPmInfo pm;
1552 AcpiMiscInfo misc;
1553 AcpiMcfgInfo mcfg;
1554 PcPciInfo pci;
1555 uint8_t *u;
07fb6176 1556 size_t aml_len = 0;
7c2c1fa5 1557 GArray *tables_blob = tables->table_data;
72c194f7
MT
1558
1559 acpi_get_cpu_info(&cpu);
1560 acpi_get_pm_info(&pm);
1561 acpi_get_dsdt(&misc);
72c194f7
MT
1562 acpi_get_misc_info(&misc);
1563 acpi_get_pci_info(&pci);
1564
1565 table_offsets = g_array_new(false, true /* clear */,
1566 sizeof(uint32_t));
8b310fc4 1567 ACPI_BUILD_DPRINTF("init ACPI tables\n");
72c194f7
MT
1568
1569 bios_linker_loader_alloc(tables->linker, ACPI_BUILD_TABLE_FILE,
1570 64 /* Ensure FACS is aligned */,
1571 false /* high memory */);
1572
1573 /*
1574 * FACS is pointed to by FADT.
1575 * We place it first since it's the only table that has alignment
1576 * requirements.
1577 */
7c2c1fa5
IM
1578 facs = tables_blob->len;
1579 build_facs(tables_blob, tables->linker, guest_info);
72c194f7
MT
1580
1581 /* DSDT is pointed to by FADT */
7c2c1fa5
IM
1582 dsdt = tables_blob->len;
1583 build_dsdt(tables_blob, tables->linker, &misc);
72c194f7 1584
07fb6176
PB
1585 /* Count the size of the DSDT and SSDT, we will need it for legacy
1586 * sizing of ACPI tables.
1587 */
7c2c1fa5 1588 aml_len += tables_blob->len - dsdt;
07fb6176 1589
72c194f7 1590 /* ACPI tables pointed to by RSDT */
7c2c1fa5
IM
1591 acpi_add_table(table_offsets, tables_blob);
1592 build_fadt(tables_blob, tables->linker, &pm, facs, dsdt);
72c194f7 1593
7c2c1fa5
IM
1594 ssdt = tables_blob->len;
1595 acpi_add_table(table_offsets, tables_blob);
1596 build_ssdt(tables_blob, tables->linker, &cpu, &pm, &misc, &pci,
72c194f7 1597 guest_info);
7c2c1fa5 1598 aml_len += tables_blob->len - ssdt;
72c194f7 1599
7c2c1fa5
IM
1600 acpi_add_table(table_offsets, tables_blob);
1601 build_madt(tables_blob, tables->linker, &cpu, guest_info);
9ac1c4c0 1602
72c194f7 1603 if (misc.has_hpet) {
7c2c1fa5
IM
1604 acpi_add_table(table_offsets, tables_blob);
1605 build_hpet(tables_blob, tables->linker);
711b20b4
SB
1606 }
1607 if (misc.has_tpm) {
7c2c1fa5
IM
1608 acpi_add_table(table_offsets, tables_blob);
1609 build_tpm_tcpa(tables_blob, tables->linker, tables->tcpalog);
711b20b4 1610
7c2c1fa5
IM
1611 acpi_add_table(table_offsets, tables_blob);
1612 build_tpm_ssdt(tables_blob, tables->linker);
72c194f7
MT
1613 }
1614 if (guest_info->numa_nodes) {
7c2c1fa5
IM
1615 acpi_add_table(table_offsets, tables_blob);
1616 build_srat(tables_blob, tables->linker, guest_info);
72c194f7
MT
1617 }
1618 if (acpi_get_mcfg(&mcfg)) {
7c2c1fa5
IM
1619 acpi_add_table(table_offsets, tables_blob);
1620 build_mcfg_q35(tables_blob, tables->linker, &mcfg);
72c194f7 1621 }
d4eb9119 1622 if (acpi_has_iommu()) {
7c2c1fa5
IM
1623 acpi_add_table(table_offsets, tables_blob);
1624 build_dmar_q35(tables_blob, tables->linker);
d4eb9119 1625 }
72c194f7
MT
1626
1627 /* Add tables supplied by user (if any) */
1628 for (u = acpi_table_first(); u; u = acpi_table_next(u)) {
1629 unsigned len = acpi_table_len(u);
1630
7c2c1fa5
IM
1631 acpi_add_table(table_offsets, tables_blob);
1632 g_array_append_vals(tables_blob, u, len);
72c194f7
MT
1633 }
1634
1635 /* RSDT is pointed to by RSDP */
7c2c1fa5
IM
1636 rsdt = tables_blob->len;
1637 build_rsdt(tables_blob, tables->linker, table_offsets);
72c194f7
MT
1638
1639 /* RSDP is in FSEG memory, so allocate it separately */
1640 build_rsdp(tables->rsdp, tables->linker, rsdt);
1641
07fb6176 1642 /* We'll expose it all to Guest so we want to reduce
72c194f7 1643 * chance of size changes.
07fb6176
PB
1644 *
1645 * We used to align the tables to 4k, but of course this would
1646 * too simple to be enough. 4k turned out to be too small an
1647 * alignment very soon, and in fact it is almost impossible to
1648 * keep the table size stable for all (max_cpus, max_memory_slots)
1649 * combinations. So the table size is always 64k for pc-i440fx-2.1
1650 * and we give an error if the table grows beyond that limit.
1651 *
1652 * We still have the problem of migrating from "-M pc-i440fx-2.0". For
1653 * that, we exploit the fact that QEMU 2.1 generates _smaller_ tables
1654 * than 2.0 and we can always pad the smaller tables with zeros. We can
1655 * then use the exact size of the 2.0 tables.
1656 *
1657 * All this is for PIIX4, since QEMU 2.0 didn't support Q35 migration.
72c194f7 1658 */
07fb6176
PB
1659 if (guest_info->legacy_acpi_table_size) {
1660 /* Subtracting aml_len gives the size of fixed tables. Then add the
1661 * size of the PIIX4 DSDT/SSDT in QEMU 2.0.
1662 */
1663 int legacy_aml_len =
1664 guest_info->legacy_acpi_table_size +
1665 ACPI_BUILD_LEGACY_CPU_AML_SIZE * max_cpus;
1666 int legacy_table_size =
7c2c1fa5 1667 ROUND_UP(tables_blob->len - aml_len + legacy_aml_len,
07fb6176 1668 ACPI_BUILD_ALIGN_SIZE);
7c2c1fa5 1669 if (tables_blob->len > legacy_table_size) {
07fb6176 1670 /* Should happen only with PCI bridges and -M pc-i440fx-2.0. */
868270f2 1671 error_report("Warning: migration may not work.");
07fb6176 1672 }
7c2c1fa5 1673 g_array_set_size(tables_blob, legacy_table_size);
07fb6176 1674 } else {
868270f2 1675 /* Make sure we have a buffer in case we need to resize the tables. */
7c2c1fa5 1676 if (tables_blob->len > ACPI_BUILD_TABLE_SIZE / 2) {
18045fb9 1677 /* As of QEMU 2.1, this fires with 160 VCPUs and 255 memory slots. */
868270f2
MT
1678 error_report("Warning: ACPI tables are larger than 64k.");
1679 error_report("Warning: migration may not work.");
1680 error_report("Warning: please remove CPUs, NUMA nodes, "
1681 "memory slots or PCI bridges.");
18045fb9 1682 }
7c2c1fa5 1683 acpi_align_size(tables_blob, ACPI_BUILD_TABLE_SIZE);
07fb6176 1684 }
72c194f7 1685
07fb6176 1686 acpi_align_size(tables->linker, ACPI_BUILD_ALIGN_SIZE);
72c194f7
MT
1687
1688 /* Cleanup memory that's no longer used. */
1689 g_array_free(table_offsets, true);
1690}
1691
42d85900
MT
1692static void acpi_ram_update(ram_addr_t ram, GArray *data)
1693{
1694 uint32_t size = acpi_data_len(data);
1695
1696 /* Make sure RAM size is correct - in case it got changed e.g. by migration */
1697 qemu_ram_resize(ram, size, &error_abort);
1698
1699 memcpy(qemu_get_ram_ptr(ram), data->data, size);
1700 cpu_physical_memory_set_dirty_range_nocode(ram, size);
1701}
1702
72c194f7
MT
1703static void acpi_build_update(void *build_opaque, uint32_t offset)
1704{
1705 AcpiBuildState *build_state = build_opaque;
1706 AcpiBuildTables tables;
1707
1708 /* No state to update or already patched? Nothing to do. */
1709 if (!build_state || build_state->patched) {
1710 return;
1711 }
1712 build_state->patched = 1;
1713
1714 acpi_build_tables_init(&tables);
1715
1716 acpi_build(build_state->guest_info, &tables);
1717
42d85900 1718 acpi_ram_update(build_state->table_ram, tables.table_data);
a1666142 1719
42d85900
MT
1720 if (build_state->rsdp) {
1721 memcpy(build_state->rsdp, tables.rsdp->data, acpi_data_len(tables.rsdp));
1722 } else {
1723 acpi_ram_update(build_state->rsdp_ram, tables.rsdp);
1724 }
ad5b88b1 1725
42d85900 1726 acpi_ram_update(build_state->linker_ram, tables.linker);
72c194f7
MT
1727 acpi_build_tables_cleanup(&tables, true);
1728}
1729
1730static void acpi_build_reset(void *build_opaque)
1731{
1732 AcpiBuildState *build_state = build_opaque;
1733 build_state->patched = 0;
1734}
1735
ad5b88b1 1736static ram_addr_t acpi_add_rom_blob(AcpiBuildState *build_state, GArray *blob,
a1666142 1737 const char *name, uint64_t max_size)
72c194f7 1738{
a1666142
MT
1739 return rom_add_blob(name, blob->data, acpi_data_len(blob), max_size, -1,
1740 name, acpi_build_update, build_state);
72c194f7
MT
1741}
1742
1743static const VMStateDescription vmstate_acpi_build = {
1744 .name = "acpi_build",
1745 .version_id = 1,
1746 .minimum_version_id = 1,
d49805ae 1747 .fields = (VMStateField[]) {
72c194f7
MT
1748 VMSTATE_UINT8(patched, AcpiBuildState),
1749 VMSTATE_END_OF_LIST()
1750 },
1751};
1752
1753void acpi_setup(PcGuestInfo *guest_info)
1754{
1755 AcpiBuildTables tables;
1756 AcpiBuildState *build_state;
1757
1758 if (!guest_info->fw_cfg) {
8b310fc4 1759 ACPI_BUILD_DPRINTF("No fw cfg. Bailing out.\n");
72c194f7
MT
1760 return;
1761 }
1762
1763 if (!guest_info->has_acpi_build) {
8b310fc4 1764 ACPI_BUILD_DPRINTF("ACPI build disabled. Bailing out.\n");
72c194f7
MT
1765 return;
1766 }
1767
81adc513 1768 if (!acpi_enabled) {
8b310fc4 1769 ACPI_BUILD_DPRINTF("ACPI disabled. Bailing out.\n");
81adc513
MT
1770 return;
1771 }
1772
72c194f7
MT
1773 build_state = g_malloc0(sizeof *build_state);
1774
1775 build_state->guest_info = guest_info;
1776
99fd437d
MT
1777 acpi_set_pci_info();
1778
72c194f7
MT
1779 acpi_build_tables_init(&tables);
1780 acpi_build(build_state->guest_info, &tables);
1781
1782 /* Now expose it all to Guest */
1783 build_state->table_ram = acpi_add_rom_blob(build_state, tables.table_data,
a1666142
MT
1784 ACPI_BUILD_TABLE_FILE,
1785 ACPI_BUILD_TABLE_MAX_SIZE);
ad5b88b1 1786 assert(build_state->table_ram != RAM_ADDR_MAX);
72c194f7 1787
6e00619b
IM
1788 build_state->linker_ram =
1789 acpi_add_rom_blob(build_state, tables.linker, "etc/table-loader", 0);
72c194f7 1790
42a5b308
SB
1791 fw_cfg_add_file(guest_info->fw_cfg, ACPI_BUILD_TPMLOG_FILE,
1792 tables.tcpalog->data, acpi_data_len(tables.tcpalog));
1793
384fb32e 1794 if (!guest_info->rsdp_in_ram) {
358774d7
IM
1795 /*
1796 * Keep for compatibility with old machine types.
1797 * Though RSDP is small, its contents isn't immutable, so
afaa2e4b 1798 * we'll update it along with the rest of tables on guest access.
358774d7 1799 */
afaa2e4b
MT
1800 uint32_t rsdp_size = acpi_data_len(tables.rsdp);
1801
1802 build_state->rsdp = g_memdup(tables.rsdp->data, rsdp_size);
358774d7
IM
1803 fw_cfg_add_file_callback(guest_info->fw_cfg, ACPI_BUILD_RSDP_FILE,
1804 acpi_build_update, build_state,
afaa2e4b 1805 build_state->rsdp, rsdp_size);
42d85900 1806 build_state->rsdp_ram = (ram_addr_t)-1;
358774d7 1807 } else {
42d85900
MT
1808 build_state->rsdp = NULL;
1809 build_state->rsdp_ram = acpi_add_rom_blob(build_state, tables.rsdp,
1810 ACPI_BUILD_RSDP_FILE, 0);
358774d7 1811 }
72c194f7
MT
1812
1813 qemu_register_reset(acpi_build_reset, build_state);
1814 acpi_build_reset(build_state);
1815 vmstate_register(NULL, 0, &vmstate_acpi_build, build_state);
1816
1817 /* Cleanup tables but don't free the memory: we track it
1818 * in build_state.
1819 */
1820 acpi_build_tables_cleanup(&tables, false);
1821}
This page took 0.375616 seconds and 4 git commands to generate.