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