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