xref: /qemu/hw/i386/acpi-build.c (revision 55a2b1631fb343edac4a2d4596c72e58ee1372b3)
1 /* Support for generating ACPI tables and passing them to Guests
2  *
3  * Copyright (C) 2008-2010  Kevin O'Connor <kevin@koconnor.net>
4  * Copyright (C) 2006 Fabrice Bellard
5  * Copyright (C) 2013 Red Hat Inc
6  *
7  * Author: Michael S. Tsirkin <mst@redhat.com>
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"
28 #include "qemu/range.h"
29 #include "hw/pci/pci.h"
30 #include "qom/cpu.h"
31 #include "hw/i386/pc.h"
32 #include "target-i386/cpu.h"
33 #include "hw/timer/hpet.h"
34 #include "hw/i386/acpi-defs.h"
35 #include "hw/acpi/acpi.h"
36 #include "hw/nvram/fw_cfg.h"
37 #include "bios-linker-loader.h"
38 #include "hw/loader.h"
39 
40 /* Supported chipsets: */
41 #include "hw/acpi/piix4.h"
42 #include "hw/i386/ich9.h"
43 #include "hw/pci/pci_bus.h"
44 #include "hw/pci-host/q35.h"
45 
46 #include "hw/i386/q35-acpi-dsdt.hex"
47 #include "hw/i386/acpi-dsdt.hex"
48 
49 #include "qapi/qmp/qint.h"
50 #include "qom/qom-qobject.h"
51 
52 typedef struct AcpiCpuInfo {
53     DECLARE_BITMAP(found_cpus, MAX_CPUMASK_BITS + 1);
54 } AcpiCpuInfo;
55 
56 typedef struct AcpiMcfgInfo {
57     uint64_t mcfg_base;
58     uint32_t mcfg_size;
59 } AcpiMcfgInfo;
60 
61 typedef struct AcpiPmInfo {
62     bool s3_disabled;
63     bool s4_disabled;
64     uint8_t s4_val;
65     uint16_t sci_int;
66     uint8_t acpi_enable_cmd;
67     uint8_t acpi_disable_cmd;
68     uint32_t gpe0_blk;
69     uint32_t gpe0_blk_len;
70     uint32_t io_base;
71 } AcpiPmInfo;
72 
73 typedef struct AcpiMiscInfo {
74     bool has_hpet;
75     DECLARE_BITMAP(slot_hotplug_enable, PCI_SLOT_MAX);
76     const unsigned char *dsdt_code;
77     unsigned dsdt_size;
78     uint16_t pvpanic_port;
79 } AcpiMiscInfo;
80 
81 static void acpi_get_dsdt(AcpiMiscInfo *info)
82 {
83     Object *piix = piix4_pm_find();
84     Object *lpc = ich9_lpc_find();
85     assert(!!piix != !!lpc);
86 
87     if (piix) {
88         info->dsdt_code = AcpiDsdtAmlCode;
89         info->dsdt_size = sizeof AcpiDsdtAmlCode;
90     }
91     if (lpc) {
92         info->dsdt_code = Q35AcpiDsdtAmlCode;
93         info->dsdt_size = sizeof Q35AcpiDsdtAmlCode;
94     }
95 }
96 
97 static
98 int acpi_add_cpu_info(Object *o, void *opaque)
99 {
100     AcpiCpuInfo *cpu = opaque;
101     uint64_t apic_id;
102 
103     if (object_dynamic_cast(o, TYPE_CPU)) {
104         apic_id = object_property_get_int(o, "apic-id", NULL);
105         assert(apic_id <= MAX_CPUMASK_BITS);
106 
107         set_bit(apic_id, cpu->found_cpus);
108     }
109 
110     object_child_foreach(o, acpi_add_cpu_info, opaque);
111     return 0;
112 }
113 
114 static void acpi_get_cpu_info(AcpiCpuInfo *cpu)
115 {
116     Object *root = object_get_root();
117 
118     memset(cpu->found_cpus, 0, sizeof cpu->found_cpus);
119     object_child_foreach(root, acpi_add_cpu_info, cpu);
120 }
121 
122 static void acpi_get_pm_info(AcpiPmInfo *pm)
123 {
124     Object *piix = piix4_pm_find();
125     Object *lpc = ich9_lpc_find();
126     Object *obj = NULL;
127     QObject *o;
128 
129     if (piix) {
130         obj = piix;
131     }
132     if (lpc) {
133         obj = lpc;
134     }
135     assert(obj);
136 
137     /* Fill in optional s3/s4 related properties */
138     o = object_property_get_qobject(obj, ACPI_PM_PROP_S3_DISABLED, NULL);
139     if (o) {
140         pm->s3_disabled = qint_get_int(qobject_to_qint(o));
141     } else {
142         pm->s3_disabled = false;
143     }
144     o = object_property_get_qobject(obj, ACPI_PM_PROP_S4_DISABLED, NULL);
145     if (o) {
146         pm->s4_disabled = qint_get_int(qobject_to_qint(o));
147     } else {
148         pm->s4_disabled = false;
149     }
150     o = object_property_get_qobject(obj, ACPI_PM_PROP_S4_VAL, NULL);
151     if (o) {
152         pm->s4_val = qint_get_int(qobject_to_qint(o));
153     } else {
154         pm->s4_val = false;
155     }
156 
157     /* Fill in mandatory properties */
158     pm->sci_int = object_property_get_int(obj, ACPI_PM_PROP_SCI_INT, NULL);
159 
160     pm->acpi_enable_cmd = object_property_get_int(obj,
161                                                   ACPI_PM_PROP_ACPI_ENABLE_CMD,
162                                                   NULL);
163     pm->acpi_disable_cmd = object_property_get_int(obj,
164                                                   ACPI_PM_PROP_ACPI_DISABLE_CMD,
165                                                   NULL);
166     pm->io_base = object_property_get_int(obj, ACPI_PM_PROP_PM_IO_BASE,
167                                           NULL);
168     pm->gpe0_blk = object_property_get_int(obj, ACPI_PM_PROP_GPE0_BLK,
169                                            NULL);
170     pm->gpe0_blk_len = object_property_get_int(obj, ACPI_PM_PROP_GPE0_BLK_LEN,
171                                                NULL);
172 }
173 
174 static void acpi_get_hotplug_info(AcpiMiscInfo *misc)
175 {
176     int i;
177     PCIBus *bus = find_i440fx();
178 
179     if (!bus) {
180         /* Only PIIX supports ACPI hotplug */
181         memset(misc->slot_hotplug_enable, 0, sizeof misc->slot_hotplug_enable);
182         return;
183     }
184 
185     memset(misc->slot_hotplug_enable, 0xff,
186            DIV_ROUND_UP(PCI_SLOT_MAX, BITS_PER_BYTE));
187 
188     for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
189         PCIDeviceClass *pc;
190         PCIDevice *pdev = bus->devices[i];
191 
192         if (!pdev) {
193             continue;
194         }
195 
196         pc = PCI_DEVICE_GET_CLASS(pdev);
197 
198         if (pc->no_hotplug) {
199             int slot = PCI_SLOT(i);
200 
201             clear_bit(slot, misc->slot_hotplug_enable);
202         }
203     }
204 }
205 
206 static void acpi_get_misc_info(AcpiMiscInfo *info)
207 {
208     info->has_hpet = hpet_find();
209     info->pvpanic_port = pvpanic_port();
210 }
211 
212 static void acpi_get_pci_info(PcPciInfo *info)
213 {
214     Object *pci_host;
215     bool ambiguous;
216 
217     pci_host = object_resolve_path_type("", TYPE_PCI_HOST_BRIDGE, &ambiguous);
218     g_assert(!ambiguous);
219     g_assert(pci_host);
220 
221     info->w32.begin = object_property_get_int(pci_host,
222                                               PCI_HOST_PROP_PCI_HOLE_START,
223                                               NULL);
224     info->w32.end = object_property_get_int(pci_host,
225                                             PCI_HOST_PROP_PCI_HOLE_END,
226                                             NULL);
227     info->w64.begin = object_property_get_int(pci_host,
228                                               PCI_HOST_PROP_PCI_HOLE64_START,
229                                               NULL);
230     info->w64.end = object_property_get_int(pci_host,
231                                             PCI_HOST_PROP_PCI_HOLE64_END,
232                                             NULL);
233 }
234 
235 #define ACPI_BUILD_APPNAME  "Bochs"
236 #define ACPI_BUILD_APPNAME6 "BOCHS "
237 #define ACPI_BUILD_APPNAME4 "BXPC"
238 
239 #define ACPI_BUILD_DPRINTF(level, fmt, ...) do {} while (0)
240 
241 #define ACPI_BUILD_TABLE_FILE "etc/acpi/tables"
242 #define ACPI_BUILD_RSDP_FILE "etc/acpi/rsdp"
243 
244 static void
245 build_header(GArray *linker, GArray *table_data,
246              AcpiTableHeader *h, uint32_t sig, int len, uint8_t rev)
247 {
248     h->signature = cpu_to_le32(sig);
249     h->length = cpu_to_le32(len);
250     h->revision = rev;
251     memcpy(h->oem_id, ACPI_BUILD_APPNAME6, 6);
252     memcpy(h->oem_table_id, ACPI_BUILD_APPNAME4, 4);
253     memcpy(h->oem_table_id + 4, (void *)&sig, 4);
254     h->oem_revision = cpu_to_le32(1);
255     memcpy(h->asl_compiler_id, ACPI_BUILD_APPNAME4, 4);
256     h->asl_compiler_revision = cpu_to_le32(1);
257     h->checksum = 0;
258     /* Checksum to be filled in by Guest linker */
259     bios_linker_loader_add_checksum(linker, ACPI_BUILD_TABLE_FILE,
260                                     table_data->data, h, len, &h->checksum);
261 }
262 
263 static inline GArray *build_alloc_array(void)
264 {
265         return g_array_new(false, true /* clear */, 1);
266 }
267 
268 static inline void build_free_array(GArray *array)
269 {
270         g_array_free(array, true);
271 }
272 
273 static inline void build_prepend_byte(GArray *array, uint8_t val)
274 {
275     g_array_prepend_val(array, val);
276 }
277 
278 static inline void build_append_byte(GArray *array, uint8_t val)
279 {
280     g_array_append_val(array, val);
281 }
282 
283 static inline void build_append_array(GArray *array, GArray *val)
284 {
285     g_array_append_vals(array, val->data, val->len);
286 }
287 
288 static void build_append_nameseg(GArray *array, const char *format, ...)
289 {
290     /* It would be nicer to use g_string_vprintf but it's only there in 2.22 */
291     char s[] = "XXXX";
292     int len;
293     va_list args;
294 
295     va_start(args, format);
296     len = vsnprintf(s, sizeof s, format, args);
297     va_end(args);
298 
299     assert(len == 4);
300     g_array_append_vals(array, s, len);
301 }
302 
303 /* 5.4 Definition Block Encoding */
304 enum {
305     PACKAGE_LENGTH_1BYTE_SHIFT = 6, /* Up to 63 - use extra 2 bits. */
306     PACKAGE_LENGTH_2BYTE_SHIFT = 4,
307     PACKAGE_LENGTH_3BYTE_SHIFT = 12,
308     PACKAGE_LENGTH_4BYTE_SHIFT = 20,
309 };
310 
311 static void build_prepend_package_length(GArray *package, unsigned min_bytes)
312 {
313     uint8_t byte;
314     unsigned length = package->len;
315     unsigned length_bytes;
316 
317     if (length + 1 < (1 << PACKAGE_LENGTH_1BYTE_SHIFT)) {
318         length_bytes = 1;
319     } else if (length + 2 < (1 << PACKAGE_LENGTH_3BYTE_SHIFT)) {
320         length_bytes = 2;
321     } else if (length + 3 < (1 << PACKAGE_LENGTH_4BYTE_SHIFT)) {
322         length_bytes = 3;
323     } else {
324         length_bytes = 4;
325     }
326 
327     /* Force length to at least min_bytes.
328      * This wastes memory but that's how bios did it.
329      */
330     length_bytes = MAX(length_bytes, min_bytes);
331 
332     /* PkgLength is the length of the inclusive length of the data. */
333     length += length_bytes;
334 
335     switch (length_bytes) {
336     case 1:
337         byte = length;
338         build_prepend_byte(package, byte);
339         return;
340     case 4:
341         byte = length >> PACKAGE_LENGTH_4BYTE_SHIFT;
342         build_prepend_byte(package, byte);
343         length &= (1 << PACKAGE_LENGTH_4BYTE_SHIFT) - 1;
344         /* fall through */
345     case 3:
346         byte = length >> PACKAGE_LENGTH_3BYTE_SHIFT;
347         build_prepend_byte(package, byte);
348         length &= (1 << PACKAGE_LENGTH_3BYTE_SHIFT) - 1;
349         /* fall through */
350     case 2:
351         byte = length >> PACKAGE_LENGTH_2BYTE_SHIFT;
352         build_prepend_byte(package, byte);
353         length &= (1 << PACKAGE_LENGTH_2BYTE_SHIFT) - 1;
354         /* fall through */
355     }
356     /*
357      * Most significant two bits of byte zero indicate how many following bytes
358      * are in PkgLength encoding.
359      */
360     byte = ((length_bytes - 1) << PACKAGE_LENGTH_1BYTE_SHIFT) | length;
361     build_prepend_byte(package, byte);
362 }
363 
364 static void build_package(GArray *package, uint8_t op, unsigned min_bytes)
365 {
366     build_prepend_package_length(package, min_bytes);
367     build_prepend_byte(package, op);
368 }
369 
370 static void build_append_value(GArray *table, uint32_t value, int size)
371 {
372     uint8_t prefix;
373     int i;
374 
375     switch (size) {
376     case 1:
377         prefix = 0x0A; /* BytePrefix */
378         break;
379     case 2:
380         prefix = 0x0B; /* WordPrefix */
381         break;
382     case 4:
383         prefix = 0x0C; /* DWordPrefix */
384         break;
385     default:
386         assert(0);
387         return;
388     }
389     build_append_byte(table, prefix);
390     for (i = 0; i < size; ++i) {
391         build_append_byte(table, value & 0xFF);
392         value = value >> 8;
393     }
394 }
395 
396 static void build_append_notify_target(GArray *method, GArray *target_name,
397                                        uint32_t value, int size)
398 {
399     GArray *notify = build_alloc_array();
400     uint8_t op = 0xA0; /* IfOp */
401 
402     build_append_byte(notify, 0x93); /* LEqualOp */
403     build_append_byte(notify, 0x68); /* Arg0Op */
404     build_append_value(notify, value, size);
405     build_append_byte(notify, 0x86); /* NotifyOp */
406     build_append_array(notify, target_name);
407     build_append_byte(notify, 0x69); /* Arg1Op */
408 
409     /* Pack it up */
410     build_package(notify, op, 1);
411 
412     build_append_array(method, notify);
413 
414     build_free_array(notify);
415 }
416 
417 #define ACPI_PORT_SMI_CMD           0x00b2 /* TODO: this is APM_CNT_IOPORT */
418 
419 static inline void *acpi_data_push(GArray *table_data, unsigned size)
420 {
421     unsigned off = table_data->len;
422     g_array_set_size(table_data, off + size);
423     return table_data->data + off;
424 }
425 
426 static unsigned acpi_data_len(GArray *table)
427 {
428 #if GLIB_CHECK_VERSION(2, 22, 0)
429     assert(g_array_get_element_size(table) == 1);
430 #endif
431     return table->len;
432 }
433 
434 static void acpi_align_size(GArray *blob, unsigned align)
435 {
436     /* Align size to multiple of given size. This reduces the chance
437      * we need to change size in the future (breaking cross version migration).
438      */
439     g_array_set_size(blob, ROUND_UP(acpi_data_len(blob), align));
440 }
441 
442 /* Get pointer within table in a safe manner */
443 #define ACPI_BUILD_PTR(table, size, off, type) \
444     ((type *)(acpi_data_get_ptr(table, size, off, sizeof(type))))
445 
446 static inline void *acpi_data_get_ptr(uint8_t *table_data, unsigned table_size,
447                                       unsigned off, unsigned size)
448 {
449     assert(off + size > off);
450     assert(off + size <= table_size);
451     return table_data + off;
452 }
453 
454 static inline void acpi_add_table(GArray *table_offsets, GArray *table_data)
455 {
456     uint32_t offset = cpu_to_le32(table_data->len);
457     g_array_append_val(table_offsets, offset);
458 }
459 
460 /* FACS */
461 static void
462 build_facs(GArray *table_data, GArray *linker, PcGuestInfo *guest_info)
463 {
464     AcpiFacsDescriptorRev1 *facs = acpi_data_push(table_data, sizeof *facs);
465     facs->signature = cpu_to_le32(ACPI_FACS_SIGNATURE);
466     facs->length = cpu_to_le32(sizeof(*facs));
467 }
468 
469 /* Load chipset information in FADT */
470 static void fadt_setup(AcpiFadtDescriptorRev1 *fadt, AcpiPmInfo *pm)
471 {
472     fadt->model = 1;
473     fadt->reserved1 = 0;
474     fadt->sci_int = cpu_to_le16(pm->sci_int);
475     fadt->smi_cmd = cpu_to_le32(ACPI_PORT_SMI_CMD);
476     fadt->acpi_enable = pm->acpi_enable_cmd;
477     fadt->acpi_disable = pm->acpi_disable_cmd;
478     /* EVT, CNT, TMR offset matches hw/acpi/core.c */
479     fadt->pm1a_evt_blk = cpu_to_le32(pm->io_base);
480     fadt->pm1a_cnt_blk = cpu_to_le32(pm->io_base + 0x04);
481     fadt->pm_tmr_blk = cpu_to_le32(pm->io_base + 0x08);
482     fadt->gpe0_blk = cpu_to_le32(pm->gpe0_blk);
483     /* EVT, CNT, TMR length matches hw/acpi/core.c */
484     fadt->pm1_evt_len = 4;
485     fadt->pm1_cnt_len = 2;
486     fadt->pm_tmr_len = 4;
487     fadt->gpe0_blk_len = pm->gpe0_blk_len;
488     fadt->plvl2_lat = cpu_to_le16(0xfff); /* C2 state not supported */
489     fadt->plvl3_lat = cpu_to_le16(0xfff); /* C3 state not supported */
490     fadt->flags = cpu_to_le32((1 << ACPI_FADT_F_WBINVD) |
491                               (1 << ACPI_FADT_F_PROC_C1) |
492                               (1 << ACPI_FADT_F_SLP_BUTTON) |
493                               (1 << ACPI_FADT_F_RTC_S4));
494     fadt->flags |= cpu_to_le32(1 << ACPI_FADT_F_USE_PLATFORM_CLOCK);
495 }
496 
497 
498 /* FADT */
499 static void
500 build_fadt(GArray *table_data, GArray *linker, AcpiPmInfo *pm,
501            unsigned facs, unsigned dsdt)
502 {
503     AcpiFadtDescriptorRev1 *fadt = acpi_data_push(table_data, sizeof(*fadt));
504 
505     fadt->firmware_ctrl = cpu_to_le32(facs);
506     /* FACS address to be filled by Guest linker */
507     bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
508                                    ACPI_BUILD_TABLE_FILE,
509                                    table_data, &fadt->firmware_ctrl,
510                                    sizeof fadt->firmware_ctrl);
511 
512     fadt->dsdt = cpu_to_le32(dsdt);
513     /* DSDT address to be filled by Guest linker */
514     bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
515                                    ACPI_BUILD_TABLE_FILE,
516                                    table_data, &fadt->dsdt,
517                                    sizeof fadt->dsdt);
518 
519     fadt_setup(fadt, pm);
520 
521     build_header(linker, table_data,
522                  (void *)fadt, ACPI_FACP_SIGNATURE, sizeof(*fadt), 1);
523 }
524 
525 static void
526 build_madt(GArray *table_data, GArray *linker, AcpiCpuInfo *cpu,
527            PcGuestInfo *guest_info)
528 {
529     int madt_start = table_data->len;
530 
531     AcpiMultipleApicTable *madt;
532     AcpiMadtIoApic *io_apic;
533     AcpiMadtIntsrcovr *intsrcovr;
534     AcpiMadtLocalNmi *local_nmi;
535     int i;
536 
537     madt = acpi_data_push(table_data, sizeof *madt);
538     madt->local_apic_address = cpu_to_le32(APIC_DEFAULT_ADDRESS);
539     madt->flags = cpu_to_le32(1);
540 
541     for (i = 0; i < guest_info->apic_id_limit; i++) {
542         AcpiMadtProcessorApic *apic = acpi_data_push(table_data, sizeof *apic);
543         apic->type = ACPI_APIC_PROCESSOR;
544         apic->length = sizeof(*apic);
545         apic->processor_id = i;
546         apic->local_apic_id = i;
547         if (test_bit(i, cpu->found_cpus)) {
548             apic->flags = cpu_to_le32(1);
549         } else {
550             apic->flags = cpu_to_le32(0);
551         }
552     }
553     io_apic = acpi_data_push(table_data, sizeof *io_apic);
554     io_apic->type = ACPI_APIC_IO;
555     io_apic->length = sizeof(*io_apic);
556 #define ACPI_BUILD_IOAPIC_ID 0x0
557     io_apic->io_apic_id = ACPI_BUILD_IOAPIC_ID;
558     io_apic->address = cpu_to_le32(IO_APIC_DEFAULT_ADDRESS);
559     io_apic->interrupt = cpu_to_le32(0);
560 
561     if (guest_info->apic_xrupt_override) {
562         intsrcovr = acpi_data_push(table_data, sizeof *intsrcovr);
563         intsrcovr->type   = ACPI_APIC_XRUPT_OVERRIDE;
564         intsrcovr->length = sizeof(*intsrcovr);
565         intsrcovr->source = 0;
566         intsrcovr->gsi    = cpu_to_le32(2);
567         intsrcovr->flags  = cpu_to_le16(0); /* conforms to bus specifications */
568     }
569     for (i = 1; i < 16; i++) {
570 #define ACPI_BUILD_PCI_IRQS ((1<<5) | (1<<9) | (1<<10) | (1<<11))
571         if (!(ACPI_BUILD_PCI_IRQS & (1 << i))) {
572             /* No need for a INT source override structure. */
573             continue;
574         }
575         intsrcovr = acpi_data_push(table_data, sizeof *intsrcovr);
576         intsrcovr->type   = ACPI_APIC_XRUPT_OVERRIDE;
577         intsrcovr->length = sizeof(*intsrcovr);
578         intsrcovr->source = i;
579         intsrcovr->gsi    = cpu_to_le32(i);
580         intsrcovr->flags  = cpu_to_le16(0xd); /* active high, level triggered */
581     }
582 
583     local_nmi = acpi_data_push(table_data, sizeof *local_nmi);
584     local_nmi->type         = ACPI_APIC_LOCAL_NMI;
585     local_nmi->length       = sizeof(*local_nmi);
586     local_nmi->processor_id = 0xff; /* all processors */
587     local_nmi->flags        = cpu_to_le16(0);
588     local_nmi->lint         = 1; /* ACPI_LINT1 */
589 
590     build_header(linker, table_data,
591                  (void *)(table_data->data + madt_start), ACPI_APIC_SIGNATURE,
592                  table_data->len - madt_start, 1);
593 }
594 
595 /* Encode a hex value */
596 static inline char acpi_get_hex(uint32_t val)
597 {
598     val &= 0x0f;
599     return (val <= 9) ? ('0' + val) : ('A' + val - 10);
600 }
601 
602 #include "hw/i386/ssdt-proc.hex"
603 
604 /* 0x5B 0x83 ProcessorOp PkgLength NameString ProcID */
605 #define ACPI_PROC_OFFSET_CPUHEX (*ssdt_proc_name - *ssdt_proc_start + 2)
606 #define ACPI_PROC_OFFSET_CPUID1 (*ssdt_proc_name - *ssdt_proc_start + 4)
607 #define ACPI_PROC_OFFSET_CPUID2 (*ssdt_proc_id - *ssdt_proc_start)
608 #define ACPI_PROC_SIZEOF (*ssdt_proc_end - *ssdt_proc_start)
609 #define ACPI_PROC_AML (ssdp_proc_aml + *ssdt_proc_start)
610 
611 /* 0x5B 0x82 DeviceOp PkgLength NameString */
612 #define ACPI_PCIHP_OFFSET_HEX (*ssdt_pcihp_name - *ssdt_pcihp_start + 1)
613 #define ACPI_PCIHP_OFFSET_ID (*ssdt_pcihp_id - *ssdt_pcihp_start)
614 #define ACPI_PCIHP_OFFSET_ADR (*ssdt_pcihp_adr - *ssdt_pcihp_start)
615 #define ACPI_PCIHP_OFFSET_EJ0 (*ssdt_pcihp_ej0 - *ssdt_pcihp_start)
616 #define ACPI_PCIHP_SIZEOF (*ssdt_pcihp_end - *ssdt_pcihp_start)
617 #define ACPI_PCIHP_AML (ssdp_pcihp_aml + *ssdt_pcihp_start)
618 
619 #define ACPI_SSDT_SIGNATURE 0x54445353 /* SSDT */
620 #define ACPI_SSDT_HEADER_LENGTH 36
621 
622 #include "hw/i386/ssdt-misc.hex"
623 #include "hw/i386/ssdt-pcihp.hex"
624 
625 static void
626 build_append_notify(GArray *device, const char *name,
627                     const char *format, int skip, int count)
628 {
629     int i;
630     GArray *method = build_alloc_array();
631     uint8_t op = 0x14; /* MethodOp */
632 
633     build_append_nameseg(method, name);
634     build_append_byte(method, 0x02); /* MethodFlags: ArgCount */
635     for (i = skip; i < count; i++) {
636         GArray *target = build_alloc_array();
637         build_append_nameseg(target, format, i);
638         assert(i < 256); /* Fits in 1 byte */
639         build_append_notify_target(method, target, i, 1);
640         build_free_array(target);
641     }
642     build_package(method, op, 2);
643 
644     build_append_array(device, method);
645     build_free_array(method);
646 }
647 
648 static void patch_pcihp(int slot, uint8_t *ssdt_ptr, uint32_t eject)
649 {
650     ssdt_ptr[ACPI_PCIHP_OFFSET_HEX] = acpi_get_hex(slot >> 4);
651     ssdt_ptr[ACPI_PCIHP_OFFSET_HEX + 1] = acpi_get_hex(slot);
652     ssdt_ptr[ACPI_PCIHP_OFFSET_ID] = slot;
653     ssdt_ptr[ACPI_PCIHP_OFFSET_ADR + 2] = slot;
654 
655     /* Runtime patching of ACPI_EJ0: to disable hotplug for a slot,
656      * replace the method name: _EJ0 by ACPI_EJ0_.
657      */
658     /* Sanity check */
659     assert(!memcmp(ssdt_ptr + ACPI_PCIHP_OFFSET_EJ0, "_EJ0", 4));
660 
661     if (!eject) {
662         memcpy(ssdt_ptr + ACPI_PCIHP_OFFSET_EJ0, "EJ0_", 4);
663     }
664 }
665 
666 static void patch_pci_windows(PcPciInfo *pci, uint8_t *start, unsigned size)
667 {
668     *ACPI_BUILD_PTR(start, size, acpi_pci32_start[0], uint32_t) =
669         cpu_to_le32(pci->w32.begin);
670 
671     *ACPI_BUILD_PTR(start, size, acpi_pci32_end[0], uint32_t) =
672         cpu_to_le32(pci->w32.end - 1);
673 
674     if (pci->w64.end || pci->w64.begin) {
675         *ACPI_BUILD_PTR(start, size, acpi_pci64_valid[0], uint8_t) = 1;
676         *ACPI_BUILD_PTR(start, size, acpi_pci64_start[0], uint64_t) =
677             cpu_to_le64(pci->w64.begin);
678         *ACPI_BUILD_PTR(start, size, acpi_pci64_end[0], uint64_t) =
679             cpu_to_le64(pci->w64.end - 1);
680         *ACPI_BUILD_PTR(start, size, acpi_pci64_length[0], uint64_t) =
681             cpu_to_le64(pci->w64.end - pci->w64.begin);
682     } else {
683         *ACPI_BUILD_PTR(start, size, acpi_pci64_valid[0], uint8_t) = 0;
684     }
685 }
686 
687 static void
688 build_ssdt(GArray *table_data, GArray *linker,
689            AcpiCpuInfo *cpu, AcpiPmInfo *pm, AcpiMiscInfo *misc,
690            PcPciInfo *pci, PcGuestInfo *guest_info)
691 {
692     int acpi_cpus = MIN(0xff, guest_info->apic_id_limit);
693     int ssdt_start = table_data->len;
694     uint8_t *ssdt_ptr;
695     int i;
696 
697     /* Copy header and patch values in the S3_ / S4_ / S5_ packages */
698     ssdt_ptr = acpi_data_push(table_data, sizeof(ssdp_misc_aml));
699     memcpy(ssdt_ptr, ssdp_misc_aml, sizeof(ssdp_misc_aml));
700     if (pm->s3_disabled) {
701         ssdt_ptr[acpi_s3_name[0]] = 'X';
702     }
703     if (pm->s4_disabled) {
704         ssdt_ptr[acpi_s4_name[0]] = 'X';
705     } else {
706         ssdt_ptr[acpi_s4_pkg[0] + 1] = ssdt_ptr[acpi_s4_pkg[0] + 3] =
707             pm->s4_val;
708     }
709 
710     patch_pci_windows(pci, ssdt_ptr, sizeof(ssdp_misc_aml));
711 
712     *(uint16_t *)(ssdt_ptr + *ssdt_isa_pest) =
713         cpu_to_le16(misc->pvpanic_port);
714 
715     {
716         GArray *sb_scope = build_alloc_array();
717         uint8_t op = 0x10; /* ScopeOp */
718 
719         build_append_nameseg(sb_scope, "_SB_");
720 
721         /* build Processor object for each processor */
722         for (i = 0; i < acpi_cpus; i++) {
723             uint8_t *proc = acpi_data_push(sb_scope, ACPI_PROC_SIZEOF);
724             memcpy(proc, ACPI_PROC_AML, ACPI_PROC_SIZEOF);
725             proc[ACPI_PROC_OFFSET_CPUHEX] = acpi_get_hex(i >> 4);
726             proc[ACPI_PROC_OFFSET_CPUHEX+1] = acpi_get_hex(i);
727             proc[ACPI_PROC_OFFSET_CPUID1] = i;
728             proc[ACPI_PROC_OFFSET_CPUID2] = i;
729         }
730 
731         /* build this code:
732          *   Method(NTFY, 2) {If (LEqual(Arg0, 0x00)) {Notify(CP00, Arg1)} ...}
733          */
734         /* Arg0 = Processor ID = APIC ID */
735         build_append_notify(sb_scope, "NTFY", "CP%0.02X", 0, acpi_cpus);
736 
737         /* build "Name(CPON, Package() { One, One, ..., Zero, Zero, ... })" */
738         build_append_byte(sb_scope, 0x08); /* NameOp */
739         build_append_nameseg(sb_scope, "CPON");
740 
741         {
742             GArray *package = build_alloc_array();
743             uint8_t op = 0x12; /* PackageOp */
744 
745             build_append_byte(package, acpi_cpus); /* NumElements */
746             for (i = 0; i < acpi_cpus; i++) {
747                 uint8_t b = test_bit(i, cpu->found_cpus) ? 0x01 : 0x00;
748                 build_append_byte(package, b);
749             }
750 
751             build_package(package, op, 2);
752             build_append_array(sb_scope, package);
753             build_free_array(package);
754         }
755 
756         {
757             GArray *pci0 = build_alloc_array();
758             uint8_t op = 0x10; /* ScopeOp */;
759 
760             build_append_nameseg(pci0, "PCI0");
761 
762             /* build Device object for each slot */
763             for (i = 1; i < PCI_SLOT_MAX; i++) {
764                 bool eject = test_bit(i, misc->slot_hotplug_enable);
765                 void *pcihp = acpi_data_push(pci0, ACPI_PCIHP_SIZEOF);
766 
767                 memcpy(pcihp, ACPI_PCIHP_AML, ACPI_PCIHP_SIZEOF);
768                 patch_pcihp(i, pcihp, eject);
769             }
770 
771             build_append_notify(pci0, "PCNT", "S%0.02X_", 1, PCI_SLOT_MAX);
772             build_package(pci0, op, 3);
773             build_append_array(sb_scope, pci0);
774             build_free_array(pci0);
775         }
776 
777         build_package(sb_scope, op, 3);
778         build_append_array(table_data, sb_scope);
779         build_free_array(sb_scope);
780     }
781 
782     build_header(linker, table_data,
783                  (void *)(table_data->data + ssdt_start),
784                  ACPI_SSDT_SIGNATURE, table_data->len - ssdt_start, 1);
785 }
786 
787 static void
788 build_hpet(GArray *table_data, GArray *linker)
789 {
790     Acpi20Hpet *hpet;
791 
792     hpet = acpi_data_push(table_data, sizeof(*hpet));
793     /* Note timer_block_id value must be kept in sync with value advertised by
794      * emulated hpet
795      */
796     hpet->timer_block_id = cpu_to_le32(0x8086a201);
797     hpet->addr.address = cpu_to_le64(HPET_BASE);
798     build_header(linker, table_data,
799                  (void *)hpet, ACPI_HPET_SIGNATURE, sizeof(*hpet), 1);
800 }
801 
802 static void
803 acpi_build_srat_memory(AcpiSratMemoryAffinity *numamem,
804                        uint64_t base, uint64_t len, int node, int enabled)
805 {
806     numamem->type = ACPI_SRAT_MEMORY;
807     numamem->length = sizeof(*numamem);
808     memset(numamem->proximity, 0, 4);
809     numamem->proximity[0] = node;
810     numamem->flags = cpu_to_le32(!!enabled);
811     numamem->base_addr = cpu_to_le64(base);
812     numamem->range_length = cpu_to_le64(len);
813 }
814 
815 static void
816 build_srat(GArray *table_data, GArray *linker,
817            AcpiCpuInfo *cpu, PcGuestInfo *guest_info)
818 {
819     AcpiSystemResourceAffinityTable *srat;
820     AcpiSratProcessorAffinity *core;
821     AcpiSratMemoryAffinity *numamem;
822 
823     int i;
824     uint64_t curnode;
825     int srat_start, numa_start, slots;
826     uint64_t mem_len, mem_base, next_base;
827 
828     srat_start = table_data->len;
829 
830     srat = acpi_data_push(table_data, sizeof *srat);
831     srat->reserved1 = cpu_to_le32(1);
832     core = (void *)(srat + 1);
833 
834     for (i = 0; i < guest_info->apic_id_limit; ++i) {
835         core = acpi_data_push(table_data, sizeof *core);
836         core->type = ACPI_SRAT_PROCESSOR;
837         core->length = sizeof(*core);
838         core->local_apic_id = i;
839         curnode = guest_info->node_cpu[i];
840         core->proximity_lo = curnode;
841         memset(core->proximity_hi, 0, 3);
842         core->local_sapic_eid = 0;
843         if (test_bit(i, cpu->found_cpus)) {
844             core->flags = cpu_to_le32(1);
845         } else {
846             core->flags = cpu_to_le32(0);
847         }
848     }
849 
850 
851     /* the memory map is a bit tricky, it contains at least one hole
852      * from 640k-1M and possibly another one from 3.5G-4G.
853      */
854     next_base = 0;
855     numa_start = table_data->len;
856 
857     numamem = acpi_data_push(table_data, sizeof *numamem);
858     acpi_build_srat_memory(numamem, 0, 640*1024, 0, 1);
859     next_base = 1024 * 1024;
860     for (i = 1; i < guest_info->numa_nodes + 1; ++i) {
861         mem_base = next_base;
862         mem_len = guest_info->node_mem[i - 1];
863         if (i == 1) {
864             mem_len -= 1024 * 1024;
865         }
866         next_base = mem_base + mem_len;
867 
868         /* Cut out the ACPI_PCI hole */
869         if (mem_base <= guest_info->ram_size &&
870             next_base > guest_info->ram_size) {
871             mem_len -= next_base - guest_info->ram_size;
872             if (mem_len > 0) {
873                 numamem = acpi_data_push(table_data, sizeof *numamem);
874                 acpi_build_srat_memory(numamem, mem_base, mem_len, i-1, 1);
875             }
876             mem_base = 1ULL << 32;
877             mem_len = next_base - guest_info->ram_size;
878             next_base += (1ULL << 32) - guest_info->ram_size;
879         }
880         numamem = acpi_data_push(table_data, sizeof *numamem);
881         acpi_build_srat_memory(numamem, mem_base, mem_len, i - 1, 1);
882     }
883     slots = (table_data->len - numa_start) / sizeof *numamem;
884     for (; slots < guest_info->numa_nodes + 2; slots++) {
885         numamem = acpi_data_push(table_data, sizeof *numamem);
886         acpi_build_srat_memory(numamem, 0, 0, 0, 0);
887     }
888 
889     build_header(linker, table_data,
890                  (void *)(table_data->data + srat_start),
891                  ACPI_SRAT_SIGNATURE,
892                  table_data->len - srat_start, 1);
893 }
894 
895 static void
896 build_mcfg_q35(GArray *table_data, GArray *linker, AcpiMcfgInfo *info)
897 {
898     AcpiTableMcfg *mcfg;
899     uint32_t sig;
900     int len = sizeof(*mcfg) + 1 * sizeof(mcfg->allocation[0]);
901 
902     mcfg = acpi_data_push(table_data, len);
903     mcfg->allocation[0].address = cpu_to_le64(info->mcfg_base);
904     /* Only a single allocation so no need to play with segments */
905     mcfg->allocation[0].pci_segment = cpu_to_le16(0);
906     mcfg->allocation[0].start_bus_number = 0;
907     mcfg->allocation[0].end_bus_number = PCIE_MMCFG_BUS(info->mcfg_size - 1);
908 
909     /* MCFG is used for ECAM which can be enabled or disabled by guest.
910      * To avoid table size changes (which create migration issues),
911      * always create the table even if there are no allocations,
912      * but set the signature to a reserved value in this case.
913      * ACPI spec requires OSPMs to ignore such tables.
914      */
915     if (info->mcfg_base == PCIE_BASE_ADDR_UNMAPPED) {
916         sig = ACPI_RSRV_SIGNATURE;
917     } else {
918         sig = ACPI_MCFG_SIGNATURE;
919     }
920     build_header(linker, table_data, (void *)mcfg, sig, len, 1);
921 }
922 
923 static void
924 build_dsdt(GArray *table_data, GArray *linker, AcpiMiscInfo *misc)
925 {
926     void *dsdt;
927     assert(misc->dsdt_code && misc->dsdt_size);
928     dsdt = acpi_data_push(table_data, misc->dsdt_size);
929     memcpy(dsdt, misc->dsdt_code, misc->dsdt_size);
930 }
931 
932 /* Build final rsdt table */
933 static void
934 build_rsdt(GArray *table_data, GArray *linker, GArray *table_offsets)
935 {
936     AcpiRsdtDescriptorRev1 *rsdt;
937     size_t rsdt_len;
938     int i;
939 
940     rsdt_len = sizeof(*rsdt) + sizeof(uint32_t) * table_offsets->len;
941     rsdt = acpi_data_push(table_data, rsdt_len);
942     memcpy(rsdt->table_offset_entry, table_offsets->data,
943            sizeof(uint32_t) * table_offsets->len);
944     for (i = 0; i < table_offsets->len; ++i) {
945         /* rsdt->table_offset_entry to be filled by Guest linker */
946         bios_linker_loader_add_pointer(linker,
947                                        ACPI_BUILD_TABLE_FILE,
948                                        ACPI_BUILD_TABLE_FILE,
949                                        table_data, &rsdt->table_offset_entry[i],
950                                        sizeof(uint32_t));
951     }
952     build_header(linker, table_data,
953                  (void *)rsdt, ACPI_RSDT_SIGNATURE, rsdt_len, 1);
954 }
955 
956 static GArray *
957 build_rsdp(GArray *rsdp_table, GArray *linker, unsigned rsdt)
958 {
959     AcpiRsdpDescriptor *rsdp = acpi_data_push(rsdp_table, sizeof *rsdp);
960 
961     bios_linker_loader_alloc(linker, ACPI_BUILD_RSDP_FILE, 1,
962                              true /* fseg memory */);
963 
964     rsdp->signature = cpu_to_le64(ACPI_RSDP_SIGNATURE);
965     memcpy(rsdp->oem_id, ACPI_BUILD_APPNAME6, 6);
966     rsdp->rsdt_physical_address = cpu_to_le32(rsdt);
967     /* Address to be filled by Guest linker */
968     bios_linker_loader_add_pointer(linker, ACPI_BUILD_RSDP_FILE,
969                                    ACPI_BUILD_TABLE_FILE,
970                                    rsdp_table, &rsdp->rsdt_physical_address,
971                                    sizeof rsdp->rsdt_physical_address);
972     rsdp->checksum = 0;
973     /* Checksum to be filled by Guest linker */
974     bios_linker_loader_add_checksum(linker, ACPI_BUILD_RSDP_FILE,
975                                     rsdp, rsdp, sizeof *rsdp, &rsdp->checksum);
976 
977     return rsdp_table;
978 }
979 
980 typedef
981 struct AcpiBuildTables {
982     GArray *table_data;
983     GArray *rsdp;
984     GArray *linker;
985 } AcpiBuildTables;
986 
987 static inline void acpi_build_tables_init(AcpiBuildTables *tables)
988 {
989     tables->rsdp = g_array_new(false, true /* clear */, 1);
990     tables->table_data = g_array_new(false, true /* clear */, 1);
991     tables->linker = bios_linker_loader_init();
992 }
993 
994 static inline void acpi_build_tables_cleanup(AcpiBuildTables *tables, bool mfre)
995 {
996     void *linker_data = bios_linker_loader_cleanup(tables->linker);
997     if (mfre) {
998         g_free(linker_data);
999     }
1000     g_array_free(tables->rsdp, mfre);
1001     g_array_free(tables->table_data, mfre);
1002 }
1003 
1004 typedef
1005 struct AcpiBuildState {
1006     /* Copy of table in RAM (for patching). */
1007     uint8_t *table_ram;
1008     uint32_t table_size;
1009     /* Is table patched? */
1010     uint8_t patched;
1011     PcGuestInfo *guest_info;
1012 } AcpiBuildState;
1013 
1014 static bool acpi_get_mcfg(AcpiMcfgInfo *mcfg)
1015 {
1016     Object *pci_host;
1017     QObject *o;
1018     bool ambiguous;
1019 
1020     pci_host = object_resolve_path_type("", TYPE_PCI_HOST_BRIDGE, &ambiguous);
1021     g_assert(!ambiguous);
1022     g_assert(pci_host);
1023 
1024     o = object_property_get_qobject(pci_host, PCIE_HOST_MCFG_BASE, NULL);
1025     if (!o) {
1026         return false;
1027     }
1028     mcfg->mcfg_base = qint_get_int(qobject_to_qint(o));
1029 
1030     o = object_property_get_qobject(pci_host, PCIE_HOST_MCFG_SIZE, NULL);
1031     assert(o);
1032     mcfg->mcfg_size = qint_get_int(qobject_to_qint(o));
1033     return true;
1034 }
1035 
1036 static
1037 void acpi_build(PcGuestInfo *guest_info, AcpiBuildTables *tables)
1038 {
1039     GArray *table_offsets;
1040     unsigned facs, dsdt, rsdt;
1041     AcpiCpuInfo cpu;
1042     AcpiPmInfo pm;
1043     AcpiMiscInfo misc;
1044     AcpiMcfgInfo mcfg;
1045     PcPciInfo pci;
1046     uint8_t *u;
1047 
1048     acpi_get_cpu_info(&cpu);
1049     acpi_get_pm_info(&pm);
1050     acpi_get_dsdt(&misc);
1051     acpi_get_hotplug_info(&misc);
1052     acpi_get_misc_info(&misc);
1053     acpi_get_pci_info(&pci);
1054 
1055     table_offsets = g_array_new(false, true /* clear */,
1056                                         sizeof(uint32_t));
1057     ACPI_BUILD_DPRINTF(3, "init ACPI tables\n");
1058 
1059     bios_linker_loader_alloc(tables->linker, ACPI_BUILD_TABLE_FILE,
1060                              64 /* Ensure FACS is aligned */,
1061                              false /* high memory */);
1062 
1063     /*
1064      * FACS is pointed to by FADT.
1065      * We place it first since it's the only table that has alignment
1066      * requirements.
1067      */
1068     facs = tables->table_data->len;
1069     build_facs(tables->table_data, tables->linker, guest_info);
1070 
1071     /* DSDT is pointed to by FADT */
1072     dsdt = tables->table_data->len;
1073     build_dsdt(tables->table_data, tables->linker, &misc);
1074 
1075     /* ACPI tables pointed to by RSDT */
1076     acpi_add_table(table_offsets, tables->table_data);
1077     build_fadt(tables->table_data, tables->linker, &pm, facs, dsdt);
1078     acpi_add_table(table_offsets, tables->table_data);
1079 
1080     build_ssdt(tables->table_data, tables->linker, &cpu, &pm, &misc, &pci,
1081                guest_info);
1082     acpi_add_table(table_offsets, tables->table_data);
1083 
1084     build_madt(tables->table_data, tables->linker, &cpu, guest_info);
1085     acpi_add_table(table_offsets, tables->table_data);
1086     if (misc.has_hpet) {
1087         build_hpet(tables->table_data, tables->linker);
1088     }
1089     if (guest_info->numa_nodes) {
1090         acpi_add_table(table_offsets, tables->table_data);
1091         build_srat(tables->table_data, tables->linker, &cpu, guest_info);
1092     }
1093     if (acpi_get_mcfg(&mcfg)) {
1094         acpi_add_table(table_offsets, tables->table_data);
1095         build_mcfg_q35(tables->table_data, tables->linker, &mcfg);
1096     }
1097 
1098     /* Add tables supplied by user (if any) */
1099     for (u = acpi_table_first(); u; u = acpi_table_next(u)) {
1100         unsigned len = acpi_table_len(u);
1101 
1102         acpi_add_table(table_offsets, tables->table_data);
1103         g_array_append_vals(tables->table_data, u, len);
1104     }
1105 
1106     /* RSDT is pointed to by RSDP */
1107     rsdt = tables->table_data->len;
1108     build_rsdt(tables->table_data, tables->linker, table_offsets);
1109 
1110     /* RSDP is in FSEG memory, so allocate it separately */
1111     build_rsdp(tables->rsdp, tables->linker, rsdt);
1112 
1113     /* We'll expose it all to Guest so align size to reduce
1114      * chance of size changes.
1115      * RSDP is small so it's easy to keep it immutable, no need to
1116      * bother with alignment.
1117      */
1118     acpi_align_size(tables->table_data, 0x1000);
1119 
1120     acpi_align_size(tables->linker, 0x1000);
1121 
1122     /* Cleanup memory that's no longer used. */
1123     g_array_free(table_offsets, true);
1124 }
1125 
1126 static void acpi_build_update(void *build_opaque, uint32_t offset)
1127 {
1128     AcpiBuildState *build_state = build_opaque;
1129     AcpiBuildTables tables;
1130 
1131     /* No state to update or already patched? Nothing to do. */
1132     if (!build_state || build_state->patched) {
1133         return;
1134     }
1135     build_state->patched = 1;
1136 
1137     acpi_build_tables_init(&tables);
1138 
1139     acpi_build(build_state->guest_info, &tables);
1140 
1141     assert(acpi_data_len(tables.table_data) == build_state->table_size);
1142     memcpy(build_state->table_ram, tables.table_data->data,
1143            build_state->table_size);
1144 
1145     acpi_build_tables_cleanup(&tables, true);
1146 }
1147 
1148 static void acpi_build_reset(void *build_opaque)
1149 {
1150     AcpiBuildState *build_state = build_opaque;
1151     build_state->patched = 0;
1152 }
1153 
1154 static void *acpi_add_rom_blob(AcpiBuildState *build_state, GArray *blob,
1155                                const char *name)
1156 {
1157     return rom_add_blob(name, blob->data, acpi_data_len(blob), -1, name,
1158                         acpi_build_update, build_state);
1159 }
1160 
1161 static const VMStateDescription vmstate_acpi_build = {
1162     .name = "acpi_build",
1163     .version_id = 1,
1164     .minimum_version_id = 1,
1165     .minimum_version_id_old = 1,
1166     .fields      = (VMStateField[]) {
1167         VMSTATE_UINT8(patched, AcpiBuildState),
1168         VMSTATE_END_OF_LIST()
1169     },
1170 };
1171 
1172 void acpi_setup(PcGuestInfo *guest_info)
1173 {
1174     AcpiBuildTables tables;
1175     AcpiBuildState *build_state;
1176 
1177     if (!guest_info->fw_cfg) {
1178         ACPI_BUILD_DPRINTF(3, "No fw cfg. Bailing out.\n");
1179         return;
1180     }
1181 
1182     if (!guest_info->has_acpi_build) {
1183         ACPI_BUILD_DPRINTF(3, "ACPI build disabled. Bailing out.\n");
1184         return;
1185     }
1186 
1187     if (!acpi_enabled) {
1188         ACPI_BUILD_DPRINTF(3, "ACPI disabled. Bailing out.\n");
1189         return;
1190     }
1191 
1192     build_state = g_malloc0(sizeof *build_state);
1193 
1194     build_state->guest_info = guest_info;
1195 
1196     acpi_build_tables_init(&tables);
1197     acpi_build(build_state->guest_info, &tables);
1198 
1199     /* Now expose it all to Guest */
1200     build_state->table_ram = acpi_add_rom_blob(build_state, tables.table_data,
1201                                                ACPI_BUILD_TABLE_FILE);
1202     build_state->table_size = acpi_data_len(tables.table_data);
1203 
1204     acpi_add_rom_blob(NULL, tables.linker, "etc/table-loader");
1205 
1206     /*
1207      * RSDP is small so it's easy to keep it immutable, no need to
1208      * bother with ROM blobs.
1209      */
1210     fw_cfg_add_file(guest_info->fw_cfg, ACPI_BUILD_RSDP_FILE,
1211                     tables.rsdp->data, acpi_data_len(tables.rsdp));
1212 
1213     qemu_register_reset(acpi_build_reset, build_state);
1214     acpi_build_reset(build_state);
1215     vmstate_register(NULL, 0, &vmstate_acpi_build, build_state);
1216 
1217     /* Cleanup tables but don't free the memory: we track it
1218      * in build_state.
1219      */
1220     acpi_build_tables_cleanup(&tables, false);
1221 }
1222