]> Git Repo - qemu.git/blame - hw/i386/acpi-build.c
pc: acpi: move DBUG() from DSDT to SSDT
[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"
07fb6176 29#include "qemu/error-report.h"
72c194f7
MT
30#include "hw/pci/pci.h"
31#include "qom/cpu.h"
32#include "hw/i386/pc.h"
33#include "target-i386/cpu.h"
34#include "hw/timer/hpet.h"
395e5fb4 35#include "hw/acpi/acpi-defs.h"
72c194f7
MT
36#include "hw/acpi/acpi.h"
37#include "hw/nvram/fw_cfg.h"
0058ae1d 38#include "hw/acpi/bios-linker-loader.h"
72c194f7 39#include "hw/loader.h"
15bce1b7 40#include "hw/isa/isa.h"
bef3492d 41#include "hw/acpi/memory_hotplug.h"
87252e1b 42#include "hw/mem/nvdimm.h"
711b20b4
SB
43#include "sysemu/tpm.h"
44#include "hw/acpi/tpm.h"
5cb18b3d 45#include "sysemu/tpm_backend.h"
f070efa8 46#include "hw/timer/mc146818rtc_regs.h"
72c194f7
MT
47
48/* Supported chipsets: */
49#include "hw/acpi/piix4.h"
99fd437d 50#include "hw/acpi/pcihp.h"
72c194f7
MT
51#include "hw/i386/ich9.h"
52#include "hw/pci/pci_bus.h"
53#include "hw/pci-host/q35.h"
d4eb9119 54#include "hw/i386/intel_iommu.h"
a57d708d 55#include "hw/timer/hpet.h"
72c194f7
MT
56
57#include "hw/i386/q35-acpi-dsdt.hex"
58#include "hw/i386/acpi-dsdt.hex"
59
19934e0e
IM
60#include "hw/acpi/aml-build.h"
61
72c194f7
MT
62#include "qapi/qmp/qint.h"
63#include "qom/qom-qobject.h"
64
07fb6176
PB
65/* These are used to size the ACPI tables for -M pc-i440fx-1.7 and
66 * -M pc-i440fx-2.0. Even if the actual amount of AML generated grows
67 * a little bit, there should be plenty of free space since the DSDT
68 * shrunk by ~1.5k between QEMU 2.0 and QEMU 2.1.
69 */
70#define ACPI_BUILD_LEGACY_CPU_AML_SIZE 97
71#define ACPI_BUILD_ALIGN_SIZE 0x1000
72
868270f2 73#define ACPI_BUILD_TABLE_SIZE 0x20000
18045fb9 74
8b310fc4
GA
75/* #define DEBUG_ACPI_BUILD */
76#ifdef DEBUG_ACPI_BUILD
77#define ACPI_BUILD_DPRINTF(fmt, ...) \
78 do {printf("ACPI_BUILD: " fmt, ## __VA_ARGS__); } while (0)
79#else
80#define ACPI_BUILD_DPRINTF(fmt, ...)
81#endif
82
72c194f7 83typedef struct AcpiCpuInfo {
798325ed 84 DECLARE_BITMAP(found_cpus, ACPI_CPU_HOTPLUG_ID_LIMIT);
72c194f7
MT
85} AcpiCpuInfo;
86
87typedef struct AcpiMcfgInfo {
88 uint64_t mcfg_base;
89 uint32_t mcfg_size;
90} AcpiMcfgInfo;
91
92typedef struct AcpiPmInfo {
93 bool s3_disabled;
94 bool s4_disabled;
133a2da4 95 bool pcihp_bridge_en;
72c194f7
MT
96 uint8_t s4_val;
97 uint16_t sci_int;
98 uint8_t acpi_enable_cmd;
99 uint8_t acpi_disable_cmd;
100 uint32_t gpe0_blk;
101 uint32_t gpe0_blk_len;
102 uint32_t io_base;
ddf1ec2f
IM
103 uint16_t cpu_hp_io_base;
104 uint16_t cpu_hp_io_len;
2c6b94d8
IM
105 uint16_t mem_hp_io_base;
106 uint16_t mem_hp_io_len;
500b11ea
IM
107 uint16_t pcihp_io_base;
108 uint16_t pcihp_io_len;
72c194f7
MT
109} AcpiPmInfo;
110
111typedef struct AcpiMiscInfo {
112 bool has_hpet;
5cb18b3d 113 TPMVersion tpm_version;
72c194f7
MT
114 const unsigned char *dsdt_code;
115 unsigned dsdt_size;
116 uint16_t pvpanic_port;
8ac6f7a6 117 uint16_t applesmc_io_base;
72c194f7
MT
118} AcpiMiscInfo;
119
99fd437d
MT
120typedef struct AcpiBuildPciBusHotplugState {
121 GArray *device_table;
122 GArray *notify_table;
123 struct AcpiBuildPciBusHotplugState *parent;
133a2da4 124 bool pcihp_bridge_en;
99fd437d
MT
125} AcpiBuildPciBusHotplugState;
126
72c194f7
MT
127static void acpi_get_dsdt(AcpiMiscInfo *info)
128{
129 Object *piix = piix4_pm_find();
130 Object *lpc = ich9_lpc_find();
131 assert(!!piix != !!lpc);
132
133 if (piix) {
134 info->dsdt_code = AcpiDsdtAmlCode;
135 info->dsdt_size = sizeof AcpiDsdtAmlCode;
136 }
137 if (lpc) {
138 info->dsdt_code = Q35AcpiDsdtAmlCode;
139 info->dsdt_size = sizeof Q35AcpiDsdtAmlCode;
140 }
141}
142
143static
144int acpi_add_cpu_info(Object *o, void *opaque)
145{
146 AcpiCpuInfo *cpu = opaque;
147 uint64_t apic_id;
148
149 if (object_dynamic_cast(o, TYPE_CPU)) {
150 apic_id = object_property_get_int(o, "apic-id", NULL);
798325ed 151 assert(apic_id < ACPI_CPU_HOTPLUG_ID_LIMIT);
72c194f7
MT
152
153 set_bit(apic_id, cpu->found_cpus);
154 }
155
156 object_child_foreach(o, acpi_add_cpu_info, opaque);
157 return 0;
158}
159
160static void acpi_get_cpu_info(AcpiCpuInfo *cpu)
161{
162 Object *root = object_get_root();
163
164 memset(cpu->found_cpus, 0, sizeof cpu->found_cpus);
165 object_child_foreach(root, acpi_add_cpu_info, cpu);
166}
167
168static void acpi_get_pm_info(AcpiPmInfo *pm)
169{
170 Object *piix = piix4_pm_find();
171 Object *lpc = ich9_lpc_find();
172 Object *obj = NULL;
173 QObject *o;
174
94aaca64 175 pm->cpu_hp_io_base = 0;
500b11ea
IM
176 pm->pcihp_io_base = 0;
177 pm->pcihp_io_len = 0;
72c194f7
MT
178 if (piix) {
179 obj = piix;
ddf1ec2f 180 pm->cpu_hp_io_base = PIIX4_CPU_HOTPLUG_IO_BASE;
500b11ea
IM
181 pm->pcihp_io_base =
182 object_property_get_int(obj, ACPI_PCIHP_IO_BASE_PROP, NULL);
183 pm->pcihp_io_len =
184 object_property_get_int(obj, ACPI_PCIHP_IO_LEN_PROP, NULL);
72c194f7
MT
185 }
186 if (lpc) {
187 obj = lpc;
ddf1ec2f 188 pm->cpu_hp_io_base = ICH9_CPU_HOTPLUG_IO_BASE;
72c194f7
MT
189 }
190 assert(obj);
191
ddf1ec2f 192 pm->cpu_hp_io_len = ACPI_GPE_PROC_LEN;
2c6b94d8
IM
193 pm->mem_hp_io_base = ACPI_MEMORY_HOTPLUG_BASE;
194 pm->mem_hp_io_len = ACPI_MEMORY_HOTPLUG_IO_LEN;
195
72c194f7
MT
196 /* Fill in optional s3/s4 related properties */
197 o = object_property_get_qobject(obj, ACPI_PM_PROP_S3_DISABLED, NULL);
198 if (o) {
199 pm->s3_disabled = qint_get_int(qobject_to_qint(o));
200 } else {
201 pm->s3_disabled = false;
202 }
097a97a6 203 qobject_decref(o);
72c194f7
MT
204 o = object_property_get_qobject(obj, ACPI_PM_PROP_S4_DISABLED, NULL);
205 if (o) {
206 pm->s4_disabled = qint_get_int(qobject_to_qint(o));
207 } else {
208 pm->s4_disabled = false;
209 }
097a97a6 210 qobject_decref(o);
72c194f7
MT
211 o = object_property_get_qobject(obj, ACPI_PM_PROP_S4_VAL, NULL);
212 if (o) {
213 pm->s4_val = qint_get_int(qobject_to_qint(o));
214 } else {
215 pm->s4_val = false;
216 }
097a97a6 217 qobject_decref(o);
72c194f7
MT
218
219 /* Fill in mandatory properties */
220 pm->sci_int = object_property_get_int(obj, ACPI_PM_PROP_SCI_INT, NULL);
221
222 pm->acpi_enable_cmd = object_property_get_int(obj,
223 ACPI_PM_PROP_ACPI_ENABLE_CMD,
224 NULL);
225 pm->acpi_disable_cmd = object_property_get_int(obj,
226 ACPI_PM_PROP_ACPI_DISABLE_CMD,
227 NULL);
228 pm->io_base = object_property_get_int(obj, ACPI_PM_PROP_PM_IO_BASE,
229 NULL);
230 pm->gpe0_blk = object_property_get_int(obj, ACPI_PM_PROP_GPE0_BLK,
231 NULL);
232 pm->gpe0_blk_len = object_property_get_int(obj, ACPI_PM_PROP_GPE0_BLK_LEN,
233 NULL);
133a2da4
IM
234 pm->pcihp_bridge_en =
235 object_property_get_bool(obj, "acpi-pci-hotplug-with-bridge-support",
236 NULL);
72c194f7
MT
237}
238
72c194f7
MT
239static void acpi_get_misc_info(AcpiMiscInfo *info)
240{
241 info->has_hpet = hpet_find();
5cb18b3d 242 info->tpm_version = tpm_get_version();
72c194f7 243 info->pvpanic_port = pvpanic_port();
8ac6f7a6 244 info->applesmc_io_base = applesmc_port();
72c194f7
MT
245}
246
ca6c1855
MA
247/*
248 * Because of the PXB hosts we cannot simply query TYPE_PCI_HOST_BRIDGE.
249 * On i386 arch we only have two pci hosts, so we can look only for them.
250 */
251static Object *acpi_get_i386_pci_host(void)
252{
253 PCIHostState *host;
254
255 host = OBJECT_CHECK(PCIHostState,
256 object_resolve_path("/machine/i440fx", NULL),
257 TYPE_PCI_HOST_BRIDGE);
258 if (!host) {
259 host = OBJECT_CHECK(PCIHostState,
260 object_resolve_path("/machine/q35", NULL),
261 TYPE_PCI_HOST_BRIDGE);
262 }
263
264 return OBJECT(host);
265}
266
72c194f7
MT
267static void acpi_get_pci_info(PcPciInfo *info)
268{
269 Object *pci_host;
72c194f7 270
ca6c1855
MA
271
272 pci_host = acpi_get_i386_pci_host();
72c194f7
MT
273 g_assert(pci_host);
274
275 info->w32.begin = object_property_get_int(pci_host,
276 PCI_HOST_PROP_PCI_HOLE_START,
277 NULL);
278 info->w32.end = object_property_get_int(pci_host,
279 PCI_HOST_PROP_PCI_HOLE_END,
280 NULL);
281 info->w64.begin = object_property_get_int(pci_host,
282 PCI_HOST_PROP_PCI_HOLE64_START,
283 NULL);
284 info->w64.end = object_property_get_int(pci_host,
285 PCI_HOST_PROP_PCI_HOLE64_END,
286 NULL);
287}
288
72c194f7
MT
289#define ACPI_PORT_SMI_CMD 0x00b2 /* TODO: this is APM_CNT_IOPORT */
290
72c194f7
MT
291static void acpi_align_size(GArray *blob, unsigned align)
292{
293 /* Align size to multiple of given size. This reduces the chance
294 * we need to change size in the future (breaking cross version migration).
295 */
134d42d6 296 g_array_set_size(blob, ROUND_UP(acpi_data_len(blob), align));
72c194f7
MT
297}
298
72c194f7
MT
299/* FACS */
300static void
301build_facs(GArray *table_data, GArray *linker, PcGuestInfo *guest_info)
302{
303 AcpiFacsDescriptorRev1 *facs = acpi_data_push(table_data, sizeof *facs);
821e3227 304 memcpy(&facs->signature, "FACS", 4);
72c194f7
MT
305 facs->length = cpu_to_le32(sizeof(*facs));
306}
307
308/* Load chipset information in FADT */
309static void fadt_setup(AcpiFadtDescriptorRev1 *fadt, AcpiPmInfo *pm)
310{
311 fadt->model = 1;
312 fadt->reserved1 = 0;
313 fadt->sci_int = cpu_to_le16(pm->sci_int);
314 fadt->smi_cmd = cpu_to_le32(ACPI_PORT_SMI_CMD);
315 fadt->acpi_enable = pm->acpi_enable_cmd;
316 fadt->acpi_disable = pm->acpi_disable_cmd;
317 /* EVT, CNT, TMR offset matches hw/acpi/core.c */
318 fadt->pm1a_evt_blk = cpu_to_le32(pm->io_base);
319 fadt->pm1a_cnt_blk = cpu_to_le32(pm->io_base + 0x04);
320 fadt->pm_tmr_blk = cpu_to_le32(pm->io_base + 0x08);
321 fadt->gpe0_blk = cpu_to_le32(pm->gpe0_blk);
322 /* EVT, CNT, TMR length matches hw/acpi/core.c */
323 fadt->pm1_evt_len = 4;
324 fadt->pm1_cnt_len = 2;
325 fadt->pm_tmr_len = 4;
326 fadt->gpe0_blk_len = pm->gpe0_blk_len;
327 fadt->plvl2_lat = cpu_to_le16(0xfff); /* C2 state not supported */
328 fadt->plvl3_lat = cpu_to_le16(0xfff); /* C3 state not supported */
329 fadt->flags = cpu_to_le32((1 << ACPI_FADT_F_WBINVD) |
330 (1 << ACPI_FADT_F_PROC_C1) |
331 (1 << ACPI_FADT_F_SLP_BUTTON) |
332 (1 << ACPI_FADT_F_RTC_S4));
333 fadt->flags |= cpu_to_le32(1 << ACPI_FADT_F_USE_PLATFORM_CLOCK);
07b81ed9
HZ
334 /* APIC destination mode ("Flat Logical") has an upper limit of 8 CPUs
335 * For more than 8 CPUs, "Clustered Logical" mode has to be used
336 */
337 if (max_cpus > 8) {
338 fadt->flags |= cpu_to_le32(1 << ACPI_FADT_F_FORCE_APIC_CLUSTER_MODEL);
339 }
f070efa8 340 fadt->century = RTC_CENTURY;
72c194f7
MT
341}
342
343
344/* FADT */
345static void
346build_fadt(GArray *table_data, GArray *linker, AcpiPmInfo *pm,
347 unsigned facs, unsigned dsdt)
348{
349 AcpiFadtDescriptorRev1 *fadt = acpi_data_push(table_data, sizeof(*fadt));
350
351 fadt->firmware_ctrl = cpu_to_le32(facs);
352 /* FACS address to be filled by Guest linker */
353 bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
354 ACPI_BUILD_TABLE_FILE,
355 table_data, &fadt->firmware_ctrl,
356 sizeof fadt->firmware_ctrl);
357
358 fadt->dsdt = cpu_to_le32(dsdt);
359 /* DSDT address to be filled by Guest linker */
360 bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
361 ACPI_BUILD_TABLE_FILE,
362 table_data, &fadt->dsdt,
363 sizeof fadt->dsdt);
364
365 fadt_setup(fadt, pm);
366
367 build_header(linker, table_data,
8870ca0e 368 (void *)fadt, "FACP", sizeof(*fadt), 1, NULL);
72c194f7
MT
369}
370
371static void
372build_madt(GArray *table_data, GArray *linker, AcpiCpuInfo *cpu,
373 PcGuestInfo *guest_info)
374{
375 int madt_start = table_data->len;
376
377 AcpiMultipleApicTable *madt;
378 AcpiMadtIoApic *io_apic;
379 AcpiMadtIntsrcovr *intsrcovr;
380 AcpiMadtLocalNmi *local_nmi;
381 int i;
382
383 madt = acpi_data_push(table_data, sizeof *madt);
384 madt->local_apic_address = cpu_to_le32(APIC_DEFAULT_ADDRESS);
385 madt->flags = cpu_to_le32(1);
386
387 for (i = 0; i < guest_info->apic_id_limit; i++) {
388 AcpiMadtProcessorApic *apic = acpi_data_push(table_data, sizeof *apic);
389 apic->type = ACPI_APIC_PROCESSOR;
390 apic->length = sizeof(*apic);
391 apic->processor_id = i;
392 apic->local_apic_id = i;
393 if (test_bit(i, cpu->found_cpus)) {
394 apic->flags = cpu_to_le32(1);
395 } else {
396 apic->flags = cpu_to_le32(0);
397 }
398 }
399 io_apic = acpi_data_push(table_data, sizeof *io_apic);
400 io_apic->type = ACPI_APIC_IO;
401 io_apic->length = sizeof(*io_apic);
402#define ACPI_BUILD_IOAPIC_ID 0x0
403 io_apic->io_apic_id = ACPI_BUILD_IOAPIC_ID;
404 io_apic->address = cpu_to_le32(IO_APIC_DEFAULT_ADDRESS);
405 io_apic->interrupt = cpu_to_le32(0);
406
407 if (guest_info->apic_xrupt_override) {
408 intsrcovr = acpi_data_push(table_data, sizeof *intsrcovr);
409 intsrcovr->type = ACPI_APIC_XRUPT_OVERRIDE;
410 intsrcovr->length = sizeof(*intsrcovr);
411 intsrcovr->source = 0;
412 intsrcovr->gsi = cpu_to_le32(2);
413 intsrcovr->flags = cpu_to_le16(0); /* conforms to bus specifications */
414 }
415 for (i = 1; i < 16; i++) {
416#define ACPI_BUILD_PCI_IRQS ((1<<5) | (1<<9) | (1<<10) | (1<<11))
417 if (!(ACPI_BUILD_PCI_IRQS & (1 << i))) {
418 /* No need for a INT source override structure. */
419 continue;
420 }
421 intsrcovr = acpi_data_push(table_data, sizeof *intsrcovr);
422 intsrcovr->type = ACPI_APIC_XRUPT_OVERRIDE;
423 intsrcovr->length = sizeof(*intsrcovr);
424 intsrcovr->source = i;
425 intsrcovr->gsi = cpu_to_le32(i);
426 intsrcovr->flags = cpu_to_le16(0xd); /* active high, level triggered */
427 }
428
429 local_nmi = acpi_data_push(table_data, sizeof *local_nmi);
430 local_nmi->type = ACPI_APIC_LOCAL_NMI;
431 local_nmi->length = sizeof(*local_nmi);
432 local_nmi->processor_id = 0xff; /* all processors */
433 local_nmi->flags = cpu_to_le16(0);
434 local_nmi->lint = 1; /* ACPI_LINT1 */
435
436 build_header(linker, table_data,
821e3227 437 (void *)(table_data->data + madt_start), "APIC",
8870ca0e 438 table_data->len - madt_start, 1, NULL);
72c194f7
MT
439}
440
99fd437d
MT
441/* Assign BSEL property to all buses. In the future, this can be changed
442 * to only assign to buses that support hotplug.
443 */
444static void *acpi_set_bsel(PCIBus *bus, void *opaque)
445{
446 unsigned *bsel_alloc = opaque;
447 unsigned *bus_bsel;
448
39b888bd 449 if (qbus_is_hotpluggable(BUS(bus))) {
99fd437d
MT
450 bus_bsel = g_malloc(sizeof *bus_bsel);
451
452 *bus_bsel = (*bsel_alloc)++;
453 object_property_add_uint32_ptr(OBJECT(bus), ACPI_PCIHP_PROP_BSEL,
454 bus_bsel, NULL);
455 }
456
457 return bsel_alloc;
458}
459
460static void acpi_set_pci_info(void)
461{
462 PCIBus *bus = find_i440fx(); /* TODO: Q35 support */
463 unsigned bsel_alloc = 0;
464
465 if (bus) {
466 /* Scan all PCI buses. Set property to enable acpi based hotplug. */
467 pci_for_each_bus_depth_first(bus, acpi_set_bsel, NULL, &bsel_alloc);
468 }
469}
470
62b52c26 471static void build_append_pcihp_notify_entry(Aml *method, int slot)
99fd437d 472{
62b52c26
IM
473 Aml *if_ctx;
474 int32_t devfn = PCI_DEVFN(slot, 0);
475
5530427f 476 if_ctx = aml_if(aml_and(aml_arg(0), aml_int(0x1U << slot), NULL));
62b52c26
IM
477 aml_append(if_ctx, aml_notify(aml_name("S%.02X", devfn), aml_arg(1)));
478 aml_append(method, if_ctx);
99fd437d
MT
479}
480
62b52c26 481static void build_append_pci_bus_devices(Aml *parent_scope, PCIBus *bus,
b23046ab 482 bool pcihp_bridge_en)
99fd437d 483{
62b52c26 484 Aml *dev, *notify_method, *method;
99fd437d 485 QObject *bsel;
b23046ab
IM
486 PCIBus *sec;
487 int i;
133a2da4 488
99fd437d
MT
489 bsel = object_property_get_qobject(OBJECT(bus), ACPI_PCIHP_PROP_BSEL, NULL);
490 if (bsel) {
62b52c26
IM
491 int64_t bsel_val = qint_get_int(qobject_to_qint(bsel));
492
493 aml_append(parent_scope, aml_name_decl("BSEL", aml_int(bsel_val)));
4dbfc881 494 notify_method = aml_method("DVNT", 2, AML_NOTSERIALIZED);
8dcf525a 495 }
99fd437d 496
8dcf525a
MT
497 for (i = 0; i < ARRAY_SIZE(bus->devices); i += PCI_FUNC_MAX) {
498 DeviceClass *dc;
499 PCIDeviceClass *pc;
500 PCIDevice *pdev = bus->devices[i];
501 int slot = PCI_SLOT(i);
b23046ab 502 bool hotplug_enabled_dev;
093a35e5 503 bool bridge_in_acpi;
99fd437d 504
8dcf525a 505 if (!pdev) {
b23046ab 506 if (bsel) { /* add hotplug slots for non present devices */
62b52c26
IM
507 dev = aml_device("S%.02X", PCI_DEVFN(slot, 0));
508 aml_append(dev, aml_name_decl("_SUN", aml_int(slot)));
509 aml_append(dev, aml_name_decl("_ADR", aml_int(slot << 16)));
4dbfc881 510 method = aml_method("_EJ0", 1, AML_NOTSERIALIZED);
62b52c26
IM
511 aml_append(method,
512 aml_call2("PCEJ", aml_name("BSEL"), aml_name("_SUN"))
513 );
514 aml_append(dev, method);
515 aml_append(parent_scope, dev);
516
517 build_append_pcihp_notify_entry(notify_method, slot);
b23046ab 518 }
8dcf525a
MT
519 continue;
520 }
99fd437d 521
8dcf525a
MT
522 pc = PCI_DEVICE_GET_CLASS(pdev);
523 dc = DEVICE_GET_CLASS(pdev);
99fd437d 524
093a35e5
MT
525 /* When hotplug for bridges is enabled, bridges are
526 * described in ACPI separately (see build_pci_bus_end).
527 * In this case they aren't themselves hot-pluggable.
a20275fa 528 * Hotplugged bridges *are* hot-pluggable.
093a35e5 529 */
b23046ab
IM
530 bridge_in_acpi = pc->is_bridge && pcihp_bridge_en &&
531 !DEVICE(pdev)->hotplugged;
532
533 hotplug_enabled_dev = bsel && dc->hotpluggable && !bridge_in_acpi;
093a35e5 534
b23046ab
IM
535 if (pc->class_id == PCI_CLASS_BRIDGE_ISA) {
536 continue;
99fd437d
MT
537 }
538
62b52c26
IM
539 /* start to compose PCI slot descriptor */
540 dev = aml_device("S%.02X", PCI_DEVFN(slot, 0));
541 aml_append(dev, aml_name_decl("_ADR", aml_int(slot << 16)));
542
8dcf525a 543 if (pc->class_id == PCI_CLASS_DISPLAY_VGA) {
62b52c26
IM
544 /* add VGA specific AML methods */
545 int s3d;
546
8dcf525a 547 if (object_dynamic_cast(OBJECT(pdev), "qxl-vga")) {
62b52c26 548 s3d = 3;
b23046ab 549 } else {
62b52c26 550 s3d = 0;
99fd437d 551 }
62b52c26 552
4dbfc881 553 method = aml_method("_S1D", 0, AML_NOTSERIALIZED);
62b52c26
IM
554 aml_append(method, aml_return(aml_int(0)));
555 aml_append(dev, method);
556
4dbfc881 557 method = aml_method("_S2D", 0, AML_NOTSERIALIZED);
62b52c26
IM
558 aml_append(method, aml_return(aml_int(0)));
559 aml_append(dev, method);
560
4dbfc881 561 method = aml_method("_S3D", 0, AML_NOTSERIALIZED);
62b52c26
IM
562 aml_append(method, aml_return(aml_int(s3d)));
563 aml_append(dev, method);
b23046ab 564 } else if (hotplug_enabled_dev) {
62b52c26
IM
565 /* add _SUN/_EJ0 to make slot hotpluggable */
566 aml_append(dev, aml_name_decl("_SUN", aml_int(slot)));
99fd437d 567
4dbfc881 568 method = aml_method("_EJ0", 1, AML_NOTSERIALIZED);
62b52c26
IM
569 aml_append(method,
570 aml_call2("PCEJ", aml_name("BSEL"), aml_name("_SUN"))
571 );
572 aml_append(dev, method);
573
574 if (bsel) {
575 build_append_pcihp_notify_entry(notify_method, slot);
576 }
b23046ab 577 } else if (bridge_in_acpi) {
62b52c26
IM
578 /*
579 * device is coldplugged bridge,
580 * add child device descriptions into its scope
581 */
b23046ab 582 PCIBus *sec_bus = pci_bridge_get_sec_bus(PCI_BRIDGE(pdev));
b23046ab 583
62b52c26 584 build_append_pci_bus_devices(dev, sec_bus, pcihp_bridge_en);
8dcf525a 585 }
62b52c26
IM
586 /* slot descriptor has been composed, add it into parent context */
587 aml_append(parent_scope, dev);
8dcf525a
MT
588 }
589
590 if (bsel) {
62b52c26 591 aml_append(parent_scope, notify_method);
99fd437d
MT
592 }
593
594 /* Append PCNT method to notify about events on local and child buses.
595 * Add unconditionally for root since DSDT expects it.
72c194f7 596 */
4dbfc881 597 method = aml_method("PCNT", 0, AML_NOTSERIALIZED);
99fd437d 598
b23046ab
IM
599 /* If bus supports hotplug select it and notify about local events */
600 if (bsel) {
62b52c26
IM
601 int64_t bsel_val = qint_get_int(qobject_to_qint(bsel));
602 aml_append(method, aml_store(aml_int(bsel_val), aml_name("BNUM")));
603 aml_append(method,
604 aml_call2("DVNT", aml_name("PCIU"), aml_int(1) /* Device Check */)
605 );
606 aml_append(method,
607 aml_call2("DVNT", aml_name("PCID"), aml_int(3)/* Eject Request */)
608 );
b23046ab 609 }
99fd437d 610
b23046ab
IM
611 /* Notify about child bus events in any case */
612 if (pcihp_bridge_en) {
613 QLIST_FOREACH(sec, &bus->child, sibling) {
62b52c26
IM
614 int32_t devfn = sec->parent_dev->devfn;
615
616 aml_append(method, aml_name("^S%.02X.PCNT", devfn));
99fd437d 617 }
72c194f7 618 }
62b52c26 619 aml_append(parent_scope, method);
d370dfa9 620 qobject_decref(bsel);
72c194f7
MT
621}
622
0d8935e3
MA
623/*
624 * initialize_route - Initialize the interrupt routing rule
625 * through a specific LINK:
626 * if (lnk_idx == idx)
627 * route using link 'link_name'
628 */
629static Aml *initialize_route(Aml *route, const char *link_name,
630 Aml *lnk_idx, int idx)
631{
632 Aml *if_ctx = aml_if(aml_equal(lnk_idx, aml_int(idx)));
633 Aml *pkg = aml_package(4);
634
635 aml_append(pkg, aml_int(0));
636 aml_append(pkg, aml_int(0));
637 aml_append(pkg, aml_name("%s", link_name));
638 aml_append(pkg, aml_int(0));
639 aml_append(if_ctx, aml_store(pkg, route));
640
641 return if_ctx;
642}
643
644/*
645 * build_prt - Define interrupt rounting rules
646 *
647 * Returns an array of 128 routes, one for each device,
648 * based on device location.
649 * The main goal is to equaly distribute the interrupts
650 * over the 4 existing ACPI links (works only for i440fx).
651 * The hash function is (slot + pin) & 3 -> "LNK[D|A|B|C]".
652 *
653 */
654static Aml *build_prt(void)
655{
656 Aml *method, *while_ctx, *pin, *res;
657
4dbfc881 658 method = aml_method("_PRT", 0, AML_NOTSERIALIZED);
0d8935e3
MA
659 res = aml_local(0);
660 pin = aml_local(1);
661 aml_append(method, aml_store(aml_package(128), res));
662 aml_append(method, aml_store(aml_int(0), pin));
663
664 /* while (pin < 128) */
665 while_ctx = aml_while(aml_lless(pin, aml_int(128)));
666 {
667 Aml *slot = aml_local(2);
668 Aml *lnk_idx = aml_local(3);
669 Aml *route = aml_local(4);
670
671 /* slot = pin >> 2 */
672 aml_append(while_ctx,
c360639a 673 aml_store(aml_shiftright(pin, aml_int(2), NULL), slot));
0d8935e3
MA
674 /* lnk_idx = (slot + pin) & 3 */
675 aml_append(while_ctx,
5530427f
IM
676 aml_store(aml_and(aml_add(pin, slot, NULL), aml_int(3), NULL),
677 lnk_idx));
0d8935e3
MA
678
679 /* route[2] = "LNK[D|A|B|C]", selection based on pin % 3 */
680 aml_append(while_ctx, initialize_route(route, "LNKD", lnk_idx, 0));
681 aml_append(while_ctx, initialize_route(route, "LNKA", lnk_idx, 1));
682 aml_append(while_ctx, initialize_route(route, "LNKB", lnk_idx, 2));
683 aml_append(while_ctx, initialize_route(route, "LNKC", lnk_idx, 3));
684
685 /* route[0] = 0x[slot]FFFF */
686 aml_append(while_ctx,
ca3df95d
IM
687 aml_store(aml_or(aml_shiftleft(slot, aml_int(16)), aml_int(0xFFFF),
688 NULL),
0d8935e3
MA
689 aml_index(route, aml_int(0))));
690 /* route[1] = pin & 3 */
691 aml_append(while_ctx,
5530427f
IM
692 aml_store(aml_and(pin, aml_int(3), NULL),
693 aml_index(route, aml_int(1))));
0d8935e3
MA
694 /* res[pin] = route */
695 aml_append(while_ctx, aml_store(route, aml_index(res, pin)));
696 /* pin++ */
697 aml_append(while_ctx, aml_increment(pin));
698 }
699 aml_append(method, while_ctx);
700 /* return res*/
701 aml_append(method, aml_return(res));
702
703 return method;
704}
705
a43c6e27
MA
706typedef struct CrsRangeEntry {
707 uint64_t base;
708 uint64_t limit;
709} CrsRangeEntry;
710
711static void crs_range_insert(GPtrArray *ranges, uint64_t base, uint64_t limit)
712{
713 CrsRangeEntry *entry;
714
715 entry = g_malloc(sizeof(*entry));
716 entry->base = base;
717 entry->limit = limit;
718
719 g_ptr_array_add(ranges, entry);
720}
721
722static void crs_range_free(gpointer data)
723{
724 CrsRangeEntry *entry = (CrsRangeEntry *)data;
725 g_free(entry);
726}
727
dcdca296
MA
728static gint crs_range_compare(gconstpointer a, gconstpointer b)
729{
730 CrsRangeEntry *entry_a = *(CrsRangeEntry **)a;
731 CrsRangeEntry *entry_b = *(CrsRangeEntry **)b;
732
733 return (int64_t)entry_a->base - (int64_t)entry_b->base;
734}
735
736/*
737 * crs_replace_with_free_ranges - given the 'used' ranges within [start - end]
738 * interval, computes the 'free' ranges from the same interval.
739 * Example: If the input array is { [a1 - a2],[b1 - b2] }, the function
740 * will return { [base - a1], [a2 - b1], [b2 - limit] }.
741 */
742static void crs_replace_with_free_ranges(GPtrArray *ranges,
743 uint64_t start, uint64_t end)
744{
745 GPtrArray *free_ranges = g_ptr_array_new_with_free_func(crs_range_free);
746 uint64_t free_base = start;
747 int i;
748
749 g_ptr_array_sort(ranges, crs_range_compare);
750 for (i = 0; i < ranges->len; i++) {
751 CrsRangeEntry *used = g_ptr_array_index(ranges, i);
752
753 if (free_base < used->base) {
754 crs_range_insert(free_ranges, free_base, used->base - 1);
755 }
756
757 free_base = used->limit + 1;
758 }
759
760 if (free_base < end) {
761 crs_range_insert(free_ranges, free_base, end);
762 }
763
764 g_ptr_array_set_size(ranges, 0);
765 for (i = 0; i < free_ranges->len; i++) {
766 g_ptr_array_add(ranges, g_ptr_array_index(free_ranges, i));
767 }
768
769 g_ptr_array_free(free_ranges, false);
770}
771
d7fd0e69
MA
772/*
773 * crs_range_merge - merges adjacent ranges in the given array.
774 * Array elements are deleted and replaced with the merged ranges.
775 */
776static void crs_range_merge(GPtrArray *range)
777{
778 GPtrArray *tmp = g_ptr_array_new_with_free_func(crs_range_free);
779 CrsRangeEntry *entry;
780 uint64_t range_base, range_limit;
781 int i;
782
783 if (!range->len) {
784 return;
785 }
786
787 g_ptr_array_sort(range, crs_range_compare);
788
789 entry = g_ptr_array_index(range, 0);
790 range_base = entry->base;
791 range_limit = entry->limit;
792 for (i = 1; i < range->len; i++) {
793 entry = g_ptr_array_index(range, i);
794 if (entry->base - 1 == range_limit) {
795 range_limit = entry->limit;
796 } else {
797 crs_range_insert(tmp, range_base, range_limit);
798 range_base = entry->base;
799 range_limit = entry->limit;
800 }
801 }
802 crs_range_insert(tmp, range_base, range_limit);
803
804 g_ptr_array_set_size(range, 0);
805 for (i = 0; i < tmp->len; i++) {
806 entry = g_ptr_array_index(tmp, i);
807 crs_range_insert(range, entry->base, entry->limit);
808 }
809 g_ptr_array_free(tmp, true);
810}
811
a43c6e27
MA
812static Aml *build_crs(PCIHostState *host,
813 GPtrArray *io_ranges, GPtrArray *mem_ranges)
814{
815 Aml *crs = aml_resource_template();
d7fd0e69
MA
816 GPtrArray *host_io_ranges = g_ptr_array_new_with_free_func(crs_range_free);
817 GPtrArray *host_mem_ranges = g_ptr_array_new_with_free_func(crs_range_free);
818 CrsRangeEntry *entry;
a43c6e27
MA
819 uint8_t max_bus = pci_bus_num(host->bus);
820 uint8_t type;
821 int devfn;
d7fd0e69 822 int i;
a43c6e27
MA
823
824 for (devfn = 0; devfn < ARRAY_SIZE(host->bus->devices); devfn++) {
a43c6e27
MA
825 uint64_t range_base, range_limit;
826 PCIDevice *dev = host->bus->devices[devfn];
827
828 if (!dev) {
829 continue;
830 }
831
832 for (i = 0; i < PCI_NUM_REGIONS; i++) {
833 PCIIORegion *r = &dev->io_regions[i];
834
835 range_base = r->addr;
836 range_limit = r->addr + r->size - 1;
837
0f6dd8e1
MA
838 /*
839 * Work-around for old bioses
840 * that do not support multiple root buses
841 */
842 if (!range_base || range_base > range_limit) {
843 continue;
844 }
845
a43c6e27 846 if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
d7fd0e69 847 crs_range_insert(host_io_ranges, range_base, range_limit);
a43c6e27 848 } else { /* "memory" */
d7fd0e69 849 crs_range_insert(host_mem_ranges, range_base, range_limit);
a43c6e27
MA
850 }
851 }
852
853 type = dev->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION;
854 if (type == PCI_HEADER_TYPE_BRIDGE) {
855 uint8_t subordinate = dev->config[PCI_SUBORDINATE_BUS];
856 if (subordinate > max_bus) {
857 max_bus = subordinate;
858 }
859
860 range_base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_IO);
861 range_limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_IO);
0f6dd8e1
MA
862
863 /*
864 * Work-around for old bioses
865 * that do not support multiple root buses
866 */
4ebc736e 867 if (range_base && range_base <= range_limit) {
d7fd0e69 868 crs_range_insert(host_io_ranges, range_base, range_limit);
0f6dd8e1 869 }
a43c6e27
MA
870
871 range_base =
872 pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_MEMORY);
873 range_limit =
874 pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_MEMORY);
0f6dd8e1
MA
875
876 /*
877 * Work-around for old bioses
878 * that do not support multiple root buses
879 */
4ebc736e 880 if (range_base && range_base <= range_limit) {
d7fd0e69 881 crs_range_insert(host_mem_ranges, range_base, range_limit);
4ebc736e 882 }
a43c6e27
MA
883
884 range_base =
885 pci_bridge_get_base(dev, PCI_BASE_ADDRESS_MEM_PREFETCH);
886 range_limit =
887 pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_MEM_PREFETCH);
0f6dd8e1
MA
888
889 /*
890 * Work-around for old bioses
891 * that do not support multiple root buses
892 */
4ebc736e 893 if (range_base && range_base <= range_limit) {
d7fd0e69 894 crs_range_insert(host_mem_ranges, range_base, range_limit);
0f6dd8e1 895 }
a43c6e27
MA
896 }
897 }
898
d7fd0e69
MA
899 crs_range_merge(host_io_ranges);
900 for (i = 0; i < host_io_ranges->len; i++) {
901 entry = g_ptr_array_index(host_io_ranges, i);
902 aml_append(crs,
903 aml_word_io(AML_MIN_FIXED, AML_MAX_FIXED,
904 AML_POS_DECODE, AML_ENTIRE_RANGE,
905 0, entry->base, entry->limit, 0,
906 entry->limit - entry->base + 1));
907 crs_range_insert(io_ranges, entry->base, entry->limit);
908 }
909 g_ptr_array_free(host_io_ranges, true);
910
911 crs_range_merge(host_mem_ranges);
912 for (i = 0; i < host_mem_ranges->len; i++) {
913 entry = g_ptr_array_index(host_mem_ranges, i);
914 aml_append(crs,
915 aml_dword_memory(AML_POS_DECODE, AML_MIN_FIXED,
916 AML_MAX_FIXED, AML_NON_CACHEABLE,
917 AML_READ_WRITE,
918 0, entry->base, entry->limit, 0,
919 entry->limit - entry->base + 1));
920 crs_range_insert(mem_ranges, entry->base, entry->limit);
921 }
922 g_ptr_array_free(host_mem_ranges, true);
923
a43c6e27 924 aml_append(crs,
dcdca296 925 aml_word_bus_number(AML_MIN_FIXED, AML_MAX_FIXED, AML_POS_DECODE,
a43c6e27
MA
926 0,
927 pci_bus_num(host->bus),
928 max_bus,
929 0,
930 max_bus - pci_bus_num(host->bus) + 1));
931
932 return crs;
933}
934
5ca5efa4
IM
935static void build_processor_devices(Aml *sb_scope, unsigned acpi_cpus,
936 AcpiCpuInfo *cpu, AcpiPmInfo *pm)
937{
938 int i;
939 Aml *dev;
940 Aml *crs;
941 Aml *pkg;
942 Aml *field;
943 Aml *ifctx;
944 Aml *method;
945
946 /* The current AML generator can cover the APIC ID range [0..255],
947 * inclusive, for VCPU hotplug. */
948 QEMU_BUILD_BUG_ON(ACPI_CPU_HOTPLUG_ID_LIMIT > 256);
949 g_assert(acpi_cpus <= ACPI_CPU_HOTPLUG_ID_LIMIT);
950
951 /* create PCI0.PRES device and its _CRS to reserve CPU hotplug MMIO */
952 dev = aml_device("PCI0." stringify(CPU_HOTPLUG_RESOURCE_DEVICE));
953 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A06")));
954 aml_append(dev,
955 aml_name_decl("_UID", aml_string("CPU Hotplug resources"))
956 );
957 /* device present, functioning, decoding, not shown in UI */
958 aml_append(dev, aml_name_decl("_STA", aml_int(0xB)));
959 crs = aml_resource_template();
960 aml_append(crs,
961 aml_io(AML_DECODE16, pm->cpu_hp_io_base, pm->cpu_hp_io_base, 1,
962 pm->cpu_hp_io_len)
963 );
964 aml_append(dev, aml_name_decl("_CRS", crs));
965 aml_append(sb_scope, dev);
966 /* declare CPU hotplug MMIO region and PRS field to access it */
967 aml_append(sb_scope, aml_operation_region(
968 "PRST", AML_SYSTEM_IO, pm->cpu_hp_io_base, pm->cpu_hp_io_len));
969 field = aml_field("PRST", AML_BYTE_ACC, AML_NOLOCK, AML_PRESERVE);
970 aml_append(field, aml_named_field("PRS", 256));
971 aml_append(sb_scope, field);
972
973 /* build Processor object for each processor */
974 for (i = 0; i < acpi_cpus; i++) {
975 dev = aml_processor(i, 0, 0, "CP%.02X", i);
976
977 method = aml_method("_MAT", 0, AML_NOTSERIALIZED);
978 aml_append(method,
979 aml_return(aml_call1(CPU_MAT_METHOD, aml_int(i))));
980 aml_append(dev, method);
981
982 method = aml_method("_STA", 0, AML_NOTSERIALIZED);
983 aml_append(method,
984 aml_return(aml_call1(CPU_STATUS_METHOD, aml_int(i))));
985 aml_append(dev, method);
986
987 method = aml_method("_EJ0", 1, AML_NOTSERIALIZED);
988 aml_append(method,
989 aml_return(aml_call2(CPU_EJECT_METHOD, aml_int(i), aml_arg(0)))
990 );
991 aml_append(dev, method);
992
993 aml_append(sb_scope, dev);
994 }
995
996 /* build this code:
997 * Method(NTFY, 2) {If (LEqual(Arg0, 0x00)) {Notify(CP00, Arg1)} ...}
998 */
999 /* Arg0 = Processor ID = APIC ID */
1000 method = aml_method(AML_NOTIFY_METHOD, 2, AML_NOTSERIALIZED);
1001 for (i = 0; i < acpi_cpus; i++) {
1002 ifctx = aml_if(aml_equal(aml_arg(0), aml_int(i)));
1003 aml_append(ifctx,
1004 aml_notify(aml_name("CP%.02X", i), aml_arg(1))
1005 );
1006 aml_append(method, ifctx);
1007 }
1008 aml_append(sb_scope, method);
1009
1010 /* build "Name(CPON, Package() { One, One, ..., Zero, Zero, ... })"
1011 *
1012 * Note: The ability to create variable-sized packages was first
1013 * introduced in ACPI 2.0. ACPI 1.0 only allowed fixed-size packages
1014 * ith up to 255 elements. Windows guests up to win2k8 fail when
1015 * VarPackageOp is used.
1016 */
1017 pkg = acpi_cpus <= 255 ? aml_package(acpi_cpus) :
1018 aml_varpackage(acpi_cpus);
1019
1020 for (i = 0; i < acpi_cpus; i++) {
1021 uint8_t b = test_bit(i, cpu->found_cpus) ? 0x01 : 0x00;
1022 aml_append(pkg, aml_int(b));
1023 }
1024 aml_append(sb_scope, aml_name_decl(CPU_ON_BITMAP, pkg));
1025}
1026
f177d40a
IM
1027static void build_memory_devices(Aml *sb_scope, int nr_mem,
1028 uint16_t io_base, uint16_t io_len)
1029{
1030 int i;
1031 Aml *scope;
1032 Aml *crs;
1033 Aml *field;
1034 Aml *dev;
1035 Aml *method;
1036 Aml *ifctx;
1037
1038 /* build memory devices */
1039 assert(nr_mem <= ACPI_MAX_RAM_SLOTS);
f84548dd 1040 scope = aml_scope("\\_SB.PCI0." MEMORY_HOTPLUG_DEVICE);
f177d40a 1041 aml_append(scope,
f84548dd 1042 aml_name_decl(MEMORY_SLOTS_NUMBER, aml_int(nr_mem))
f177d40a
IM
1043 );
1044
1045 crs = aml_resource_template();
1046 aml_append(crs,
1047 aml_io(AML_DECODE16, io_base, io_base, 0, io_len)
1048 );
1049 aml_append(scope, aml_name_decl("_CRS", crs));
1050
1051 aml_append(scope, aml_operation_region(
f84548dd 1052 MEMORY_HOTPLUG_IO_REGION, AML_SYSTEM_IO,
f177d40a
IM
1053 io_base, io_len)
1054 );
1055
f84548dd 1056 field = aml_field(MEMORY_HOTPLUG_IO_REGION, AML_DWORD_ACC,
f177d40a
IM
1057 AML_NOLOCK, AML_PRESERVE);
1058 aml_append(field, /* read only */
f84548dd 1059 aml_named_field(MEMORY_SLOT_ADDR_LOW, 32));
f177d40a 1060 aml_append(field, /* read only */
f84548dd 1061 aml_named_field(MEMORY_SLOT_ADDR_HIGH, 32));
f177d40a 1062 aml_append(field, /* read only */
f84548dd 1063 aml_named_field(MEMORY_SLOT_SIZE_LOW, 32));
f177d40a 1064 aml_append(field, /* read only */
f84548dd 1065 aml_named_field(MEMORY_SLOT_SIZE_HIGH, 32));
f177d40a 1066 aml_append(field, /* read only */
f84548dd 1067 aml_named_field(MEMORY_SLOT_PROXIMITY, 32));
f177d40a
IM
1068 aml_append(scope, field);
1069
f84548dd 1070 field = aml_field(MEMORY_HOTPLUG_IO_REGION, AML_BYTE_ACC,
f177d40a
IM
1071 AML_NOLOCK, AML_WRITE_AS_ZEROS);
1072 aml_append(field, aml_reserved_field(160 /* bits, Offset(20) */));
1073 aml_append(field, /* 1 if enabled, read only */
f84548dd 1074 aml_named_field(MEMORY_SLOT_ENABLED, 1));
f177d40a
IM
1075 aml_append(field,
1076 /*(read) 1 if has a insert event. (write) 1 to clear event */
f84548dd 1077 aml_named_field(MEMORY_SLOT_INSERT_EVENT, 1));
f177d40a
IM
1078 aml_append(field,
1079 /* (read) 1 if has a remove event. (write) 1 to clear event */
f84548dd 1080 aml_named_field(MEMORY_SLOT_REMOVE_EVENT, 1));
f177d40a
IM
1081 aml_append(field,
1082 /* initiates device eject, write only */
f84548dd 1083 aml_named_field(MEMORY_SLOT_EJECT, 1));
f177d40a
IM
1084 aml_append(scope, field);
1085
f84548dd 1086 field = aml_field(MEMORY_HOTPLUG_IO_REGION, AML_DWORD_ACC,
f177d40a
IM
1087 AML_NOLOCK, AML_PRESERVE);
1088 aml_append(field, /* DIMM selector, write only */
f84548dd 1089 aml_named_field(MEMORY_SLOT_SLECTOR, 32));
f177d40a 1090 aml_append(field, /* _OST event code, write only */
f84548dd 1091 aml_named_field(MEMORY_SLOT_OST_EVENT, 32));
f177d40a 1092 aml_append(field, /* _OST status code, write only */
f84548dd 1093 aml_named_field(MEMORY_SLOT_OST_STATUS, 32));
f177d40a
IM
1094 aml_append(scope, field);
1095 aml_append(sb_scope, scope);
1096
1097 for (i = 0; i < nr_mem; i++) {
f84548dd 1098 #define BASEPATH "\\_SB.PCI0." MEMORY_HOTPLUG_DEVICE "."
f177d40a
IM
1099 const char *s;
1100
1101 dev = aml_device("MP%02X", i);
1102 aml_append(dev, aml_name_decl("_UID", aml_string("0x%02X", i)));
1103 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C80")));
1104
1105 method = aml_method("_CRS", 0, AML_NOTSERIALIZED);
f84548dd 1106 s = BASEPATH MEMORY_SLOT_CRS_METHOD;
f177d40a
IM
1107 aml_append(method, aml_return(aml_call1(s, aml_name("_UID"))));
1108 aml_append(dev, method);
1109
1110 method = aml_method("_STA", 0, AML_NOTSERIALIZED);
f84548dd 1111 s = BASEPATH MEMORY_SLOT_STATUS_METHOD;
f177d40a
IM
1112 aml_append(method, aml_return(aml_call1(s, aml_name("_UID"))));
1113 aml_append(dev, method);
1114
1115 method = aml_method("_PXM", 0, AML_NOTSERIALIZED);
f84548dd 1116 s = BASEPATH MEMORY_SLOT_PROXIMITY_METHOD;
f177d40a
IM
1117 aml_append(method, aml_return(aml_call1(s, aml_name("_UID"))));
1118 aml_append(dev, method);
1119
1120 method = aml_method("_OST", 3, AML_NOTSERIALIZED);
f84548dd
IM
1121 s = BASEPATH MEMORY_SLOT_OST_METHOD;
1122
f177d40a
IM
1123 aml_append(method, aml_return(aml_call4(
1124 s, aml_name("_UID"), aml_arg(0), aml_arg(1), aml_arg(2)
1125 )));
1126 aml_append(dev, method);
1127
1128 method = aml_method("_EJ0", 1, AML_NOTSERIALIZED);
f84548dd 1129 s = BASEPATH MEMORY_SLOT_EJECT_METHOD;
f177d40a
IM
1130 aml_append(method, aml_return(aml_call2(
1131 s, aml_name("_UID"), aml_arg(0))));
1132 aml_append(dev, method);
1133
1134 aml_append(sb_scope, dev);
1135 }
1136
1137 /* build Method(MEMORY_SLOT_NOTIFY_METHOD, 2) {
1138 * If (LEqual(Arg0, 0x00)) {Notify(MP00, Arg1)} ... }
1139 */
f84548dd 1140 method = aml_method(MEMORY_SLOT_NOTIFY_METHOD, 2, AML_NOTSERIALIZED);
f177d40a
IM
1141 for (i = 0; i < nr_mem; i++) {
1142 ifctx = aml_if(aml_equal(aml_arg(0), aml_int(i)));
1143 aml_append(ifctx,
1144 aml_notify(aml_name("MP%.02X", i), aml_arg(1))
1145 );
1146 aml_append(method, ifctx);
1147 }
1148 aml_append(sb_scope, method);
1149}
1150
a57d708d
IM
1151static void build_hpet_aml(Aml *table)
1152{
1153 Aml *crs;
1154 Aml *field;
1155 Aml *method;
1156 Aml *if_ctx;
1157 Aml *scope = aml_scope("_SB");
1158 Aml *dev = aml_device("HPET");
1159 Aml *zero = aml_int(0);
1160 Aml *id = aml_local(0);
1161 Aml *period = aml_local(1);
1162
1163 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0103")));
1164 aml_append(dev, aml_name_decl("_UID", zero));
1165
1166 aml_append(dev,
1167 aml_operation_region("HPTM", AML_SYSTEM_MEMORY, HPET_BASE, HPET_LEN));
1168 field = aml_field("HPTM", AML_DWORD_ACC, AML_LOCK, AML_PRESERVE);
1169 aml_append(field, aml_named_field("VEND", 32));
1170 aml_append(field, aml_named_field("PRD", 32));
1171 aml_append(dev, field);
1172
1173 method = aml_method("_STA", 0, AML_NOTSERIALIZED);
1174 aml_append(method, aml_store(aml_name("VEND"), id));
1175 aml_append(method, aml_store(aml_name("PRD"), period));
1176 aml_append(method, aml_shiftright(id, aml_int(16), id));
1177 if_ctx = aml_if(aml_lor(aml_equal(id, zero),
1178 aml_equal(id, aml_int(0xffff))));
1179 {
1180 aml_append(if_ctx, aml_return(zero));
1181 }
1182 aml_append(method, if_ctx);
1183
1184 if_ctx = aml_if(aml_lor(aml_equal(period, zero),
1185 aml_lgreater(period, aml_int(100000000))));
1186 {
1187 aml_append(if_ctx, aml_return(zero));
1188 }
1189 aml_append(method, if_ctx);
1190
1191 aml_append(method, aml_return(aml_int(0x0F)));
1192 aml_append(dev, method);
1193
1194 crs = aml_resource_template();
1195 aml_append(crs, aml_memory32_fixed(HPET_BASE, HPET_LEN, AML_READ_ONLY));
1196 aml_append(dev, aml_name_decl("_CRS", crs));
1197
1198 aml_append(scope, dev);
1199 aml_append(table, scope);
1200}
1201
3892a2b7
IM
1202static void build_dbg_aml(Aml *table)
1203{
1204 Aml *field;
1205 Aml *method;
1206 Aml *while_ctx;
1207 Aml *scope = aml_scope("\\");
1208 Aml *buf = aml_local(0);
1209 Aml *len = aml_local(1);
1210 Aml *idx = aml_local(2);
1211
1212 aml_append(scope,
1213 aml_operation_region("DBG", AML_SYSTEM_IO, 0x0402, 0x01));
1214 field = aml_field("DBG", AML_BYTE_ACC, AML_NOLOCK, AML_PRESERVE);
1215 aml_append(field, aml_named_field("DBGB", 8));
1216 aml_append(scope, field);
1217
1218 method = aml_method("DBUG", 1, AML_NOTSERIALIZED);
1219
1220 aml_append(method, aml_to_hexstring(aml_arg(0), buf));
1221 aml_append(method, aml_to_buffer(buf, buf));
1222 aml_append(method, aml_subtract(aml_sizeof(buf), aml_int(1), len));
1223 aml_append(method, aml_store(aml_int(0), idx));
1224
1225 while_ctx = aml_while(aml_lless(idx, len));
1226 aml_append(while_ctx,
1227 aml_store(aml_derefof(aml_index(buf, idx)), aml_name("DBGB")));
1228 aml_append(while_ctx, aml_increment(idx));
1229 aml_append(method, while_ctx);
1230
1231 aml_append(method, aml_store(aml_int(0x0A), aml_name("DBGB")));
1232 aml_append(scope, method);
1233
1234 aml_append(table, scope);
1235}
1236
72c194f7
MT
1237static void
1238build_ssdt(GArray *table_data, GArray *linker,
1239 AcpiCpuInfo *cpu, AcpiPmInfo *pm, AcpiMiscInfo *misc,
1240 PcPciInfo *pci, PcGuestInfo *guest_info)
1241{
bef3492d
IM
1242 MachineState *machine = MACHINE(qdev_get_machine());
1243 uint32_t nr_mem = machine->ram_slots;
5ca5efa4 1244 Aml *ssdt, *sb_scope, *scope, *pkg, *dev, *method, *crs, *field;
a4894206 1245 PCIBus *bus = NULL;
a43c6e27
MA
1246 GPtrArray *io_ranges = g_ptr_array_new_with_free_func(crs_range_free);
1247 GPtrArray *mem_ranges = g_ptr_array_new_with_free_func(crs_range_free);
dcdca296
MA
1248 CrsRangeEntry *entry;
1249 int root_bus_limit = 0xFF;
72c194f7
MT
1250 int i;
1251
011bb749 1252 ssdt = init_aml_allocator();
2fd71f1b 1253
4ec8d2b3
IM
1254 /* Reserve space for header */
1255 acpi_data_push(ssdt->buf, sizeof(AcpiTableHeader));
72c194f7 1256
3892a2b7 1257 build_dbg_aml(ssdt);
a57d708d 1258 build_hpet_aml(ssdt);
fbd7a6b8 1259 build_cpu_hotplug_aml(ssdt);
30bd0cf4
IM
1260 build_memory_hotplug_aml(ssdt, nr_mem, pm->mem_hp_io_base,
1261 pm->mem_hp_io_len);
1262
7f4495e1 1263 scope = aml_scope("\\_GPE");
6b306087
IM
1264 {
1265 method = aml_method("_E02", 0, AML_NOTSERIALIZED);
1266 aml_append(method, aml_call0("\\_SB." CPU_SCAN_METHOD));
1267 aml_append(scope, method);
1268
1269 method = aml_method("_E03", 0, AML_NOTSERIALIZED);
1270 aml_append(method, aml_call0(MEMORY_HOTPLUG_HANDLER_PATH));
1271 aml_append(scope, method);
1272 }
7f4495e1
IM
1273 aml_append(ssdt, scope);
1274
81ed6482 1275 bus = PC_MACHINE(machine)->bus;
a4894206
MA
1276 if (bus) {
1277 QLIST_FOREACH(bus, &bus->child, sibling) {
1278 uint8_t bus_num = pci_bus_num(bus);
0e79e51a 1279 uint8_t numa_node = pci_bus_numa_node(bus);
a4894206
MA
1280
1281 /* look only for expander root buses */
1282 if (!pci_bus_is_root(bus)) {
1283 continue;
1284 }
1285
dcdca296
MA
1286 if (bus_num < root_bus_limit) {
1287 root_bus_limit = bus_num - 1;
1288 }
1289
a4894206
MA
1290 scope = aml_scope("\\_SB");
1291 dev = aml_device("PC%.02X", bus_num);
c96d9286
LE
1292 aml_append(dev, aml_name_decl("_UID", aml_int(bus_num)));
1293 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A03")));
a4894206 1294 aml_append(dev, aml_name_decl("_BBN", aml_int(bus_num)));
0e79e51a
MA
1295
1296 if (numa_node != NUMA_NODE_UNASSIGNED) {
1297 aml_append(dev, aml_name_decl("_PXM", aml_int(numa_node)));
1298 }
1299
0d8935e3 1300 aml_append(dev, build_prt());
a43c6e27
MA
1301 crs = build_crs(PCI_HOST_BRIDGE(BUS(bus)->parent),
1302 io_ranges, mem_ranges);
1303 aml_append(dev, aml_name_decl("_CRS", crs));
a4894206
MA
1304 aml_append(scope, dev);
1305 aml_append(ssdt, scope);
1306 }
1307 }
1308
500b11ea 1309 scope = aml_scope("\\_SB.PCI0");
60efd429
IM
1310 /* build PCI0._CRS */
1311 crs = aml_resource_template();
1312 aml_append(crs,
ff80dc7f 1313 aml_word_bus_number(AML_MIN_FIXED, AML_MAX_FIXED, AML_POS_DECODE,
dcdca296
MA
1314 0x0000, 0x0, root_bus_limit,
1315 0x0000, root_bus_limit + 1));
ff80dc7f 1316 aml_append(crs, aml_io(AML_DECODE16, 0x0CF8, 0x0CF8, 0x01, 0x08));
60efd429
IM
1317
1318 aml_append(crs,
ff80dc7f
SZ
1319 aml_word_io(AML_MIN_FIXED, AML_MAX_FIXED,
1320 AML_POS_DECODE, AML_ENTIRE_RANGE,
60efd429 1321 0x0000, 0x0000, 0x0CF7, 0x0000, 0x0CF8));
dcdca296
MA
1322
1323 crs_replace_with_free_ranges(io_ranges, 0x0D00, 0xFFFF);
1324 for (i = 0; i < io_ranges->len; i++) {
1325 entry = g_ptr_array_index(io_ranges, i);
1326 aml_append(crs,
1327 aml_word_io(AML_MIN_FIXED, AML_MAX_FIXED,
1328 AML_POS_DECODE, AML_ENTIRE_RANGE,
1329 0x0000, entry->base, entry->limit,
1330 0x0000, entry->limit - entry->base + 1));
1331 }
1332
60efd429 1333 aml_append(crs,
ff80dc7f
SZ
1334 aml_dword_memory(AML_POS_DECODE, AML_MIN_FIXED, AML_MAX_FIXED,
1335 AML_CACHEABLE, AML_READ_WRITE,
60efd429 1336 0, 0x000A0000, 0x000BFFFF, 0, 0x00020000));
dcdca296
MA
1337
1338 crs_replace_with_free_ranges(mem_ranges, pci->w32.begin, pci->w32.end - 1);
1339 for (i = 0; i < mem_ranges->len; i++) {
1340 entry = g_ptr_array_index(mem_ranges, i);
1341 aml_append(crs,
1342 aml_dword_memory(AML_POS_DECODE, AML_MIN_FIXED, AML_MAX_FIXED,
1343 AML_NON_CACHEABLE, AML_READ_WRITE,
1344 0, entry->base, entry->limit,
1345 0, entry->limit - entry->base + 1));
1346 }
1347
60efd429
IM
1348 if (pci->w64.begin) {
1349 aml_append(crs,
ff80dc7f
SZ
1350 aml_qword_memory(AML_POS_DECODE, AML_MIN_FIXED, AML_MAX_FIXED,
1351 AML_CACHEABLE, AML_READ_WRITE,
60efd429
IM
1352 0, pci->w64.begin, pci->w64.end - 1, 0,
1353 pci->w64.end - pci->w64.begin));
1354 }
1355 aml_append(scope, aml_name_decl("_CRS", crs));
1356
d31c909e
IM
1357 /* reserve GPE0 block resources */
1358 dev = aml_device("GPE0");
1359 aml_append(dev, aml_name_decl("_HID", aml_string("PNP0A06")));
1360 aml_append(dev, aml_name_decl("_UID", aml_string("GPE0 resources")));
1361 /* device present, functioning, decoding, not shown in UI */
1362 aml_append(dev, aml_name_decl("_STA", aml_int(0xB)));
1363 crs = aml_resource_template();
1364 aml_append(crs,
ff80dc7f 1365 aml_io(AML_DECODE16, pm->gpe0_blk, pm->gpe0_blk, 1, pm->gpe0_blk_len)
d31c909e
IM
1366 );
1367 aml_append(dev, aml_name_decl("_CRS", crs));
1368 aml_append(scope, dev);
1369
dcdca296
MA
1370 g_ptr_array_free(io_ranges, true);
1371 g_ptr_array_free(mem_ranges, true);
1372
500b11ea
IM
1373 /* reserve PCIHP resources */
1374 if (pm->pcihp_io_len) {
1375 dev = aml_device("PHPR");
1376 aml_append(dev, aml_name_decl("_HID", aml_string("PNP0A06")));
1377 aml_append(dev,
1378 aml_name_decl("_UID", aml_string("PCI Hotplug resources")));
1379 /* device present, functioning, decoding, not shown in UI */
1380 aml_append(dev, aml_name_decl("_STA", aml_int(0xB)));
1381 crs = aml_resource_template();
1382 aml_append(crs,
ff80dc7f 1383 aml_io(AML_DECODE16, pm->pcihp_io_base, pm->pcihp_io_base, 1,
500b11ea
IM
1384 pm->pcihp_io_len)
1385 );
1386 aml_append(dev, aml_name_decl("_CRS", crs));
1387 aml_append(scope, dev);
1388 }
1389 aml_append(ssdt, scope);
1390
ebc3028f
IM
1391 /* create S3_ / S4_ / S5_ packages if necessary */
1392 scope = aml_scope("\\");
1393 if (!pm->s3_disabled) {
1394 pkg = aml_package(4);
1395 aml_append(pkg, aml_int(1)); /* PM1a_CNT.SLP_TYP */
1396 aml_append(pkg, aml_int(1)); /* PM1b_CNT.SLP_TYP, FIXME: not impl. */
1397 aml_append(pkg, aml_int(0)); /* reserved */
1398 aml_append(pkg, aml_int(0)); /* reserved */
1399 aml_append(scope, aml_name_decl("_S3", pkg));
1400 }
1401
1402 if (!pm->s4_disabled) {
1403 pkg = aml_package(4);
1404 aml_append(pkg, aml_int(pm->s4_val)); /* PM1a_CNT.SLP_TYP */
1405 /* PM1b_CNT.SLP_TYP, FIXME: not impl. */
1406 aml_append(pkg, aml_int(pm->s4_val));
1407 aml_append(pkg, aml_int(0)); /* reserved */
1408 aml_append(pkg, aml_int(0)); /* reserved */
1409 aml_append(scope, aml_name_decl("_S4", pkg));
1410 }
1411
1412 pkg = aml_package(4);
1413 aml_append(pkg, aml_int(0)); /* PM1a_CNT.SLP_TYP */
1414 aml_append(pkg, aml_int(0)); /* PM1b_CNT.SLP_TYP not impl. */
1415 aml_append(pkg, aml_int(0)); /* reserved */
1416 aml_append(pkg, aml_int(0)); /* reserved */
1417 aml_append(scope, aml_name_decl("_S5", pkg));
1418 aml_append(ssdt, scope);
1419
8ac6f7a6
IM
1420 if (misc->applesmc_io_base) {
1421 scope = aml_scope("\\_SB.PCI0.ISA");
1422 dev = aml_device("SMC");
1423
1424 aml_append(dev, aml_name_decl("_HID", aml_eisaid("APP0001")));
1425 /* device present, functioning, decoding, not shown in UI */
1426 aml_append(dev, aml_name_decl("_STA", aml_int(0xB)));
1427
1428 crs = aml_resource_template();
1429 aml_append(crs,
ff80dc7f 1430 aml_io(AML_DECODE16, misc->applesmc_io_base, misc->applesmc_io_base,
8ac6f7a6
IM
1431 0x01, APPLESMC_MAX_DATA_LENGTH)
1432 );
1433 aml_append(crs, aml_irq_no_flags(6));
1434 aml_append(dev, aml_name_decl("_CRS", crs));
1435
1436 aml_append(scope, dev);
1437 aml_append(ssdt, scope);
1438 }
1439
cd61cb2e
IM
1440 if (misc->pvpanic_port) {
1441 scope = aml_scope("\\_SB.PCI0.ISA");
1442
2332333c 1443 dev = aml_device("PEVT");
e65bef69 1444 aml_append(dev, aml_name_decl("_HID", aml_string("QEMU0001")));
cd61cb2e
IM
1445
1446 crs = aml_resource_template();
1447 aml_append(crs,
ff80dc7f 1448 aml_io(AML_DECODE16, misc->pvpanic_port, misc->pvpanic_port, 1, 1)
cd61cb2e
IM
1449 );
1450 aml_append(dev, aml_name_decl("_CRS", crs));
1451
ff80dc7f 1452 aml_append(dev, aml_operation_region("PEOR", AML_SYSTEM_IO,
cd61cb2e 1453 misc->pvpanic_port, 1));
36de884a 1454 field = aml_field("PEOR", AML_BYTE_ACC, AML_NOLOCK, AML_PRESERVE);
cd61cb2e
IM
1455 aml_append(field, aml_named_field("PEPT", 8));
1456 aml_append(dev, field);
1457
8ef3ea25
GH
1458 /* device present, functioning, decoding, shown in UI */
1459 aml_append(dev, aml_name_decl("_STA", aml_int(0xF)));
2332333c 1460
4dbfc881 1461 method = aml_method("RDPT", 0, AML_NOTSERIALIZED);
cd61cb2e
IM
1462 aml_append(method, aml_store(aml_name("PEPT"), aml_local(0)));
1463 aml_append(method, aml_return(aml_local(0)));
1464 aml_append(dev, method);
1465
4dbfc881 1466 method = aml_method("WRPT", 1, AML_NOTSERIALIZED);
cd61cb2e
IM
1467 aml_append(method, aml_store(aml_arg(0), aml_name("PEPT")));
1468 aml_append(dev, method);
1469
1470 aml_append(scope, dev);
1471 aml_append(ssdt, scope);
1472 }
1473
7824df38 1474 sb_scope = aml_scope("\\_SB");
72c194f7 1475 {
5ca5efa4 1476 build_processor_devices(sb_scope, guest_info->apic_id_limit, cpu, pm);
72c194f7 1477
f177d40a
IM
1478 build_memory_devices(sb_scope, nr_mem, pm->mem_hp_io_base,
1479 pm->mem_hp_io_len);
8698c0c0 1480
72c194f7 1481 {
8dcf525a
MT
1482 Object *pci_host;
1483 PCIBus *bus = NULL;
8dcf525a 1484
ca6c1855
MA
1485 pci_host = acpi_get_i386_pci_host();
1486 if (pci_host) {
8dcf525a
MT
1487 bus = PCI_HOST_BRIDGE(pci_host)->bus;
1488 }
72c194f7 1489
99fd437d 1490 if (bus) {
62b52c26 1491 Aml *scope = aml_scope("PCI0");
99fd437d 1492 /* Scan all PCI buses. Generate tables to support hotplug. */
62b52c26 1493 build_append_pci_bus_devices(scope, bus, pm->pcihp_bridge_en);
72d97b3a
IM
1494
1495 if (misc->tpm_version != TPM_VERSION_UNSPEC) {
1496 dev = aml_device("ISA.TPM");
1497 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C31")));
1498 aml_append(dev, aml_name_decl("_STA", aml_int(0xF)));
1499 crs = aml_resource_template();
1500 aml_append(crs, aml_memory32_fixed(TPM_TIS_ADDR_BASE,
1501 TPM_TIS_ADDR_SIZE, AML_READ_WRITE));
1502 aml_append(crs, aml_irq_no_flags(TPM_TIS_IRQ));
1503 aml_append(dev, aml_name_decl("_CRS", crs));
1504 aml_append(scope, dev);
1505 }
1506
62b52c26 1507 aml_append(sb_scope, scope);
72c194f7 1508 }
72c194f7 1509 }
011bb749 1510 aml_append(ssdt, sb_scope);
72c194f7
MT
1511 }
1512
011bb749
IM
1513 /* copy AML table into ACPI tables blob and patch header there */
1514 g_array_append_vals(table_data, ssdt->buf->data, ssdt->buf->len);
72c194f7 1515 build_header(linker, table_data,
011bb749 1516 (void *)(table_data->data + table_data->len - ssdt->buf->len),
8870ca0e 1517 "SSDT", ssdt->buf->len, 1, NULL);
011bb749 1518 free_aml_allocator();
72c194f7
MT
1519}
1520
1521static void
1522build_hpet(GArray *table_data, GArray *linker)
1523{
1524 Acpi20Hpet *hpet;
1525
1526 hpet = acpi_data_push(table_data, sizeof(*hpet));
1527 /* Note timer_block_id value must be kept in sync with value advertised by
1528 * emulated hpet
1529 */
1530 hpet->timer_block_id = cpu_to_le32(0x8086a201);
1531 hpet->addr.address = cpu_to_le64(HPET_BASE);
1532 build_header(linker, table_data,
8870ca0e 1533 (void *)hpet, "HPET", sizeof(*hpet), 1, NULL);
72c194f7
MT
1534}
1535
711b20b4 1536static void
42a5b308 1537build_tpm_tcpa(GArray *table_data, GArray *linker, GArray *tcpalog)
711b20b4
SB
1538{
1539 Acpi20Tcpa *tcpa = acpi_data_push(table_data, sizeof *tcpa);
42a5b308 1540 uint64_t log_area_start_address = acpi_data_len(tcpalog);
711b20b4
SB
1541
1542 tcpa->platform_class = cpu_to_le16(TPM_TCPA_ACPI_CLASS_CLIENT);
1543 tcpa->log_area_minimum_length = cpu_to_le32(TPM_LOG_AREA_MINIMUM_SIZE);
1544 tcpa->log_area_start_address = cpu_to_le64(log_area_start_address);
1545
42a5b308
SB
1546 bios_linker_loader_alloc(linker, ACPI_BUILD_TPMLOG_FILE, 1,
1547 false /* high memory */);
1548
711b20b4
SB
1549 /* log area start address to be filled by Guest linker */
1550 bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
42a5b308 1551 ACPI_BUILD_TPMLOG_FILE,
711b20b4
SB
1552 table_data, &tcpa->log_area_start_address,
1553 sizeof(tcpa->log_area_start_address));
1554
1555 build_header(linker, table_data,
8870ca0e 1556 (void *)tcpa, "TCPA", sizeof(*tcpa), 2, NULL);
711b20b4 1557
42a5b308 1558 acpi_data_push(tcpalog, TPM_LOG_AREA_MINIMUM_SIZE);
711b20b4
SB
1559}
1560
5cb18b3d
SB
1561static void
1562build_tpm2(GArray *table_data, GArray *linker)
1563{
1564 Acpi20TPM2 *tpm2_ptr;
5cb18b3d
SB
1565
1566 tpm2_ptr = acpi_data_push(table_data, sizeof *tpm2_ptr);
1567
1568 tpm2_ptr->platform_class = cpu_to_le16(TPM2_ACPI_CLASS_CLIENT);
1569 tpm2_ptr->control_area_address = cpu_to_le64(0);
1570 tpm2_ptr->start_method = cpu_to_le32(TPM2_START_METHOD_MMIO);
1571
1572 build_header(linker, table_data,
8870ca0e 1573 (void *)tpm2_ptr, "TPM2", sizeof(*tpm2_ptr), 4, NULL);
5cb18b3d
SB
1574}
1575
04ed3ea8
IM
1576typedef enum {
1577 MEM_AFFINITY_NOFLAGS = 0,
1578 MEM_AFFINITY_ENABLED = (1 << 0),
1579 MEM_AFFINITY_HOTPLUGGABLE = (1 << 1),
1580 MEM_AFFINITY_NON_VOLATILE = (1 << 2),
1581} MemoryAffinityFlags;
1582
72c194f7 1583static void
04ed3ea8
IM
1584acpi_build_srat_memory(AcpiSratMemoryAffinity *numamem, uint64_t base,
1585 uint64_t len, int node, MemoryAffinityFlags flags)
72c194f7
MT
1586{
1587 numamem->type = ACPI_SRAT_MEMORY;
1588 numamem->length = sizeof(*numamem);
1589 memset(numamem->proximity, 0, 4);
1590 numamem->proximity[0] = node;
04ed3ea8 1591 numamem->flags = cpu_to_le32(flags);
72c194f7
MT
1592 numamem->base_addr = cpu_to_le64(base);
1593 numamem->range_length = cpu_to_le64(len);
1594}
1595
1596static void
dd0247e0 1597build_srat(GArray *table_data, GArray *linker, PcGuestInfo *guest_info)
72c194f7
MT
1598{
1599 AcpiSystemResourceAffinityTable *srat;
1600 AcpiSratProcessorAffinity *core;
1601 AcpiSratMemoryAffinity *numamem;
1602
1603 int i;
1604 uint64_t curnode;
1605 int srat_start, numa_start, slots;
1606 uint64_t mem_len, mem_base, next_base;
cec65193
IM
1607 PCMachineState *pcms = PC_MACHINE(qdev_get_machine());
1608 ram_addr_t hotplugabble_address_space_size =
1609 object_property_get_int(OBJECT(pcms), PC_MACHINE_MEMHP_REGION_SIZE,
1610 NULL);
72c194f7
MT
1611
1612 srat_start = table_data->len;
1613
1614 srat = acpi_data_push(table_data, sizeof *srat);
1615 srat->reserved1 = cpu_to_le32(1);
1616 core = (void *)(srat + 1);
1617
1618 for (i = 0; i < guest_info->apic_id_limit; ++i) {
1619 core = acpi_data_push(table_data, sizeof *core);
1620 core->type = ACPI_SRAT_PROCESSOR;
1621 core->length = sizeof(*core);
1622 core->local_apic_id = i;
1623 curnode = guest_info->node_cpu[i];
1624 core->proximity_lo = curnode;
1625 memset(core->proximity_hi, 0, 3);
1626 core->local_sapic_eid = 0;
dd0247e0 1627 core->flags = cpu_to_le32(1);
72c194f7
MT
1628 }
1629
1630
1631 /* the memory map is a bit tricky, it contains at least one hole
1632 * from 640k-1M and possibly another one from 3.5G-4G.
1633 */
1634 next_base = 0;
1635 numa_start = table_data->len;
1636
1637 numamem = acpi_data_push(table_data, sizeof *numamem);
04ed3ea8 1638 acpi_build_srat_memory(numamem, 0, 640*1024, 0, MEM_AFFINITY_ENABLED);
72c194f7
MT
1639 next_base = 1024 * 1024;
1640 for (i = 1; i < guest_info->numa_nodes + 1; ++i) {
1641 mem_base = next_base;
1642 mem_len = guest_info->node_mem[i - 1];
1643 if (i == 1) {
1644 mem_len -= 1024 * 1024;
1645 }
1646 next_base = mem_base + mem_len;
1647
1648 /* Cut out the ACPI_PCI hole */
4c8a949b
EH
1649 if (mem_base <= guest_info->ram_size_below_4g &&
1650 next_base > guest_info->ram_size_below_4g) {
1651 mem_len -= next_base - guest_info->ram_size_below_4g;
72c194f7
MT
1652 if (mem_len > 0) {
1653 numamem = acpi_data_push(table_data, sizeof *numamem);
04ed3ea8
IM
1654 acpi_build_srat_memory(numamem, mem_base, mem_len, i - 1,
1655 MEM_AFFINITY_ENABLED);
72c194f7
MT
1656 }
1657 mem_base = 1ULL << 32;
4c8a949b
EH
1658 mem_len = next_base - guest_info->ram_size_below_4g;
1659 next_base += (1ULL << 32) - guest_info->ram_size_below_4g;
72c194f7
MT
1660 }
1661 numamem = acpi_data_push(table_data, sizeof *numamem);
04ed3ea8
IM
1662 acpi_build_srat_memory(numamem, mem_base, mem_len, i - 1,
1663 MEM_AFFINITY_ENABLED);
72c194f7
MT
1664 }
1665 slots = (table_data->len - numa_start) / sizeof *numamem;
1666 for (; slots < guest_info->numa_nodes + 2; slots++) {
1667 numamem = acpi_data_push(table_data, sizeof *numamem);
04ed3ea8 1668 acpi_build_srat_memory(numamem, 0, 0, 0, MEM_AFFINITY_NOFLAGS);
72c194f7
MT
1669 }
1670
cec65193
IM
1671 /*
1672 * Entry is required for Windows to enable memory hotplug in OS.
1673 * Memory devices may override proximity set by this entry,
1674 * providing _PXM method if necessary.
1675 */
1676 if (hotplugabble_address_space_size) {
1677 numamem = acpi_data_push(table_data, sizeof *numamem);
a7d69ff1 1678 acpi_build_srat_memory(numamem, pcms->hotplug_memory.base,
cec65193
IM
1679 hotplugabble_address_space_size, 0,
1680 MEM_AFFINITY_HOTPLUGGABLE |
1681 MEM_AFFINITY_ENABLED);
1682 }
1683
72c194f7
MT
1684 build_header(linker, table_data,
1685 (void *)(table_data->data + srat_start),
821e3227 1686 "SRAT",
8870ca0e 1687 table_data->len - srat_start, 1, NULL);
72c194f7
MT
1688}
1689
1690static void
1691build_mcfg_q35(GArray *table_data, GArray *linker, AcpiMcfgInfo *info)
1692{
1693 AcpiTableMcfg *mcfg;
821e3227 1694 const char *sig;
72c194f7
MT
1695 int len = sizeof(*mcfg) + 1 * sizeof(mcfg->allocation[0]);
1696
1697 mcfg = acpi_data_push(table_data, len);
1698 mcfg->allocation[0].address = cpu_to_le64(info->mcfg_base);
1699 /* Only a single allocation so no need to play with segments */
1700 mcfg->allocation[0].pci_segment = cpu_to_le16(0);
1701 mcfg->allocation[0].start_bus_number = 0;
1702 mcfg->allocation[0].end_bus_number = PCIE_MMCFG_BUS(info->mcfg_size - 1);
1703
1704 /* MCFG is used for ECAM which can be enabled or disabled by guest.
1705 * To avoid table size changes (which create migration issues),
1706 * always create the table even if there are no allocations,
1707 * but set the signature to a reserved value in this case.
1708 * ACPI spec requires OSPMs to ignore such tables.
1709 */
1710 if (info->mcfg_base == PCIE_BASE_ADDR_UNMAPPED) {
821e3227
MT
1711 /* Reserved signature: ignored by OSPM */
1712 sig = "QEMU";
72c194f7 1713 } else {
821e3227 1714 sig = "MCFG";
72c194f7 1715 }
8870ca0e 1716 build_header(linker, table_data, (void *)mcfg, sig, len, 1, NULL);
72c194f7
MT
1717}
1718
d4eb9119
LT
1719static void
1720build_dmar_q35(GArray *table_data, GArray *linker)
1721{
1722 int dmar_start = table_data->len;
1723
1724 AcpiTableDmar *dmar;
1725 AcpiDmarHardwareUnit *drhd;
1726
1727 dmar = acpi_data_push(table_data, sizeof(*dmar));
1728 dmar->host_address_width = VTD_HOST_ADDRESS_WIDTH - 1;
1729 dmar->flags = 0; /* No intr_remap for now */
1730
1731 /* DMAR Remapping Hardware Unit Definition structure */
1732 drhd = acpi_data_push(table_data, sizeof(*drhd));
1733 drhd->type = cpu_to_le16(ACPI_DMAR_TYPE_HARDWARE_UNIT);
1734 drhd->length = cpu_to_le16(sizeof(*drhd)); /* No device scope now */
1735 drhd->flags = ACPI_DMAR_INCLUDE_PCI_ALL;
1736 drhd->pci_segment = cpu_to_le16(0);
1737 drhd->address = cpu_to_le64(Q35_HOST_BRIDGE_IOMMU_ADDR);
1738
1739 build_header(linker, table_data, (void *)(table_data->data + dmar_start),
8870ca0e 1740 "DMAR", table_data->len - dmar_start, 1, NULL);
d4eb9119
LT
1741}
1742
72c194f7
MT
1743static void
1744build_dsdt(GArray *table_data, GArray *linker, AcpiMiscInfo *misc)
1745{
53db092a
MT
1746 AcpiTableHeader *dsdt;
1747
72c194f7 1748 assert(misc->dsdt_code && misc->dsdt_size);
53db092a 1749
72c194f7
MT
1750 dsdt = acpi_data_push(table_data, misc->dsdt_size);
1751 memcpy(dsdt, misc->dsdt_code, misc->dsdt_size);
53db092a
MT
1752
1753 memset(dsdt, 0, sizeof *dsdt);
821e3227 1754 build_header(linker, table_data, dsdt, "DSDT",
8870ca0e 1755 misc->dsdt_size, 1, NULL);
72c194f7
MT
1756}
1757
72c194f7
MT
1758static GArray *
1759build_rsdp(GArray *rsdp_table, GArray *linker, unsigned rsdt)
1760{
1761 AcpiRsdpDescriptor *rsdp = acpi_data_push(rsdp_table, sizeof *rsdp);
1762
d67aadcc 1763 bios_linker_loader_alloc(linker, ACPI_BUILD_RSDP_FILE, 16,
72c194f7
MT
1764 true /* fseg memory */);
1765
821e3227 1766 memcpy(&rsdp->signature, "RSD PTR ", 8);
72c194f7
MT
1767 memcpy(rsdp->oem_id, ACPI_BUILD_APPNAME6, 6);
1768 rsdp->rsdt_physical_address = cpu_to_le32(rsdt);
1769 /* Address to be filled by Guest linker */
1770 bios_linker_loader_add_pointer(linker, ACPI_BUILD_RSDP_FILE,
1771 ACPI_BUILD_TABLE_FILE,
1772 rsdp_table, &rsdp->rsdt_physical_address,
1773 sizeof rsdp->rsdt_physical_address);
1774 rsdp->checksum = 0;
1775 /* Checksum to be filled by Guest linker */
1776 bios_linker_loader_add_checksum(linker, ACPI_BUILD_RSDP_FILE,
1777 rsdp, rsdp, sizeof *rsdp, &rsdp->checksum);
1778
1779 return rsdp_table;
1780}
1781
72c194f7
MT
1782typedef
1783struct AcpiBuildState {
1784 /* Copy of table in RAM (for patching). */
339240b5 1785 MemoryRegion *table_mr;
72c194f7
MT
1786 /* Is table patched? */
1787 uint8_t patched;
1788 PcGuestInfo *guest_info;
d70414a5 1789 void *rsdp;
339240b5
PB
1790 MemoryRegion *rsdp_mr;
1791 MemoryRegion *linker_mr;
72c194f7
MT
1792} AcpiBuildState;
1793
1794static bool acpi_get_mcfg(AcpiMcfgInfo *mcfg)
1795{
1796 Object *pci_host;
1797 QObject *o;
72c194f7 1798
ca6c1855 1799 pci_host = acpi_get_i386_pci_host();
72c194f7
MT
1800 g_assert(pci_host);
1801
1802 o = object_property_get_qobject(pci_host, PCIE_HOST_MCFG_BASE, NULL);
1803 if (!o) {
1804 return false;
1805 }
1806 mcfg->mcfg_base = qint_get_int(qobject_to_qint(o));
097a97a6 1807 qobject_decref(o);
72c194f7
MT
1808
1809 o = object_property_get_qobject(pci_host, PCIE_HOST_MCFG_SIZE, NULL);
1810 assert(o);
1811 mcfg->mcfg_size = qint_get_int(qobject_to_qint(o));
097a97a6 1812 qobject_decref(o);
72c194f7
MT
1813 return true;
1814}
1815
d4eb9119
LT
1816static bool acpi_has_iommu(void)
1817{
1818 bool ambiguous;
1819 Object *intel_iommu;
1820
1821 intel_iommu = object_resolve_path_type("", TYPE_INTEL_IOMMU_DEVICE,
1822 &ambiguous);
1823 return intel_iommu && !ambiguous;
1824}
1825
87252e1b
XG
1826static bool acpi_has_nvdimm(void)
1827{
1828 PCMachineState *pcms = PC_MACHINE(qdev_get_machine());
1829
1830 return pcms->nvdimm;
1831}
1832
72c194f7
MT
1833static
1834void acpi_build(PcGuestInfo *guest_info, AcpiBuildTables *tables)
1835{
1836 GArray *table_offsets;
07fb6176 1837 unsigned facs, ssdt, dsdt, rsdt;
72c194f7
MT
1838 AcpiCpuInfo cpu;
1839 AcpiPmInfo pm;
1840 AcpiMiscInfo misc;
1841 AcpiMcfgInfo mcfg;
1842 PcPciInfo pci;
1843 uint8_t *u;
07fb6176 1844 size_t aml_len = 0;
7c2c1fa5 1845 GArray *tables_blob = tables->table_data;
72c194f7
MT
1846
1847 acpi_get_cpu_info(&cpu);
1848 acpi_get_pm_info(&pm);
1849 acpi_get_dsdt(&misc);
72c194f7
MT
1850 acpi_get_misc_info(&misc);
1851 acpi_get_pci_info(&pci);
1852
1853 table_offsets = g_array_new(false, true /* clear */,
1854 sizeof(uint32_t));
8b310fc4 1855 ACPI_BUILD_DPRINTF("init ACPI tables\n");
72c194f7
MT
1856
1857 bios_linker_loader_alloc(tables->linker, ACPI_BUILD_TABLE_FILE,
1858 64 /* Ensure FACS is aligned */,
1859 false /* high memory */);
1860
1861 /*
1862 * FACS is pointed to by FADT.
1863 * We place it first since it's the only table that has alignment
1864 * requirements.
1865 */
7c2c1fa5
IM
1866 facs = tables_blob->len;
1867 build_facs(tables_blob, tables->linker, guest_info);
72c194f7
MT
1868
1869 /* DSDT is pointed to by FADT */
7c2c1fa5
IM
1870 dsdt = tables_blob->len;
1871 build_dsdt(tables_blob, tables->linker, &misc);
72c194f7 1872
07fb6176
PB
1873 /* Count the size of the DSDT and SSDT, we will need it for legacy
1874 * sizing of ACPI tables.
1875 */
7c2c1fa5 1876 aml_len += tables_blob->len - dsdt;
07fb6176 1877
72c194f7 1878 /* ACPI tables pointed to by RSDT */
7c2c1fa5
IM
1879 acpi_add_table(table_offsets, tables_blob);
1880 build_fadt(tables_blob, tables->linker, &pm, facs, dsdt);
72c194f7 1881
7c2c1fa5
IM
1882 ssdt = tables_blob->len;
1883 acpi_add_table(table_offsets, tables_blob);
1884 build_ssdt(tables_blob, tables->linker, &cpu, &pm, &misc, &pci,
72c194f7 1885 guest_info);
7c2c1fa5 1886 aml_len += tables_blob->len - ssdt;
72c194f7 1887
7c2c1fa5
IM
1888 acpi_add_table(table_offsets, tables_blob);
1889 build_madt(tables_blob, tables->linker, &cpu, guest_info);
9ac1c4c0 1890
72c194f7 1891 if (misc.has_hpet) {
7c2c1fa5
IM
1892 acpi_add_table(table_offsets, tables_blob);
1893 build_hpet(tables_blob, tables->linker);
711b20b4 1894 }
5cb18b3d 1895 if (misc.tpm_version != TPM_VERSION_UNSPEC) {
7c2c1fa5
IM
1896 acpi_add_table(table_offsets, tables_blob);
1897 build_tpm_tcpa(tables_blob, tables->linker, tables->tcpalog);
711b20b4 1898
72d97b3a
IM
1899 if (misc.tpm_version == TPM_VERSION_2_0) {
1900 acpi_add_table(table_offsets, tables_blob);
5cb18b3d 1901 build_tpm2(tables_blob, tables->linker);
5cb18b3d 1902 }
72c194f7
MT
1903 }
1904 if (guest_info->numa_nodes) {
7c2c1fa5
IM
1905 acpi_add_table(table_offsets, tables_blob);
1906 build_srat(tables_blob, tables->linker, guest_info);
72c194f7
MT
1907 }
1908 if (acpi_get_mcfg(&mcfg)) {
7c2c1fa5
IM
1909 acpi_add_table(table_offsets, tables_blob);
1910 build_mcfg_q35(tables_blob, tables->linker, &mcfg);
72c194f7 1911 }
d4eb9119 1912 if (acpi_has_iommu()) {
7c2c1fa5
IM
1913 acpi_add_table(table_offsets, tables_blob);
1914 build_dmar_q35(tables_blob, tables->linker);
d4eb9119 1915 }
72c194f7 1916
87252e1b
XG
1917 if (acpi_has_nvdimm()) {
1918 nvdimm_build_acpi(table_offsets, tables_blob, tables->linker);
1919 }
1920
72c194f7
MT
1921 /* Add tables supplied by user (if any) */
1922 for (u = acpi_table_first(); u; u = acpi_table_next(u)) {
1923 unsigned len = acpi_table_len(u);
1924
7c2c1fa5
IM
1925 acpi_add_table(table_offsets, tables_blob);
1926 g_array_append_vals(tables_blob, u, len);
72c194f7
MT
1927 }
1928
1929 /* RSDT is pointed to by RSDP */
7c2c1fa5
IM
1930 rsdt = tables_blob->len;
1931 build_rsdt(tables_blob, tables->linker, table_offsets);
72c194f7
MT
1932
1933 /* RSDP is in FSEG memory, so allocate it separately */
1934 build_rsdp(tables->rsdp, tables->linker, rsdt);
1935
07fb6176 1936 /* We'll expose it all to Guest so we want to reduce
72c194f7 1937 * chance of size changes.
07fb6176
PB
1938 *
1939 * We used to align the tables to 4k, but of course this would
1940 * too simple to be enough. 4k turned out to be too small an
1941 * alignment very soon, and in fact it is almost impossible to
1942 * keep the table size stable for all (max_cpus, max_memory_slots)
1943 * combinations. So the table size is always 64k for pc-i440fx-2.1
1944 * and we give an error if the table grows beyond that limit.
1945 *
1946 * We still have the problem of migrating from "-M pc-i440fx-2.0". For
1947 * that, we exploit the fact that QEMU 2.1 generates _smaller_ tables
1948 * than 2.0 and we can always pad the smaller tables with zeros. We can
1949 * then use the exact size of the 2.0 tables.
1950 *
1951 * All this is for PIIX4, since QEMU 2.0 didn't support Q35 migration.
72c194f7 1952 */
07fb6176
PB
1953 if (guest_info->legacy_acpi_table_size) {
1954 /* Subtracting aml_len gives the size of fixed tables. Then add the
1955 * size of the PIIX4 DSDT/SSDT in QEMU 2.0.
1956 */
1957 int legacy_aml_len =
1958 guest_info->legacy_acpi_table_size +
1959 ACPI_BUILD_LEGACY_CPU_AML_SIZE * max_cpus;
1960 int legacy_table_size =
7c2c1fa5 1961 ROUND_UP(tables_blob->len - aml_len + legacy_aml_len,
07fb6176 1962 ACPI_BUILD_ALIGN_SIZE);
7c2c1fa5 1963 if (tables_blob->len > legacy_table_size) {
07fb6176 1964 /* Should happen only with PCI bridges and -M pc-i440fx-2.0. */
868270f2 1965 error_report("Warning: migration may not work.");
07fb6176 1966 }
7c2c1fa5 1967 g_array_set_size(tables_blob, legacy_table_size);
07fb6176 1968 } else {
868270f2 1969 /* Make sure we have a buffer in case we need to resize the tables. */
7c2c1fa5 1970 if (tables_blob->len > ACPI_BUILD_TABLE_SIZE / 2) {
18045fb9 1971 /* As of QEMU 2.1, this fires with 160 VCPUs and 255 memory slots. */
868270f2
MT
1972 error_report("Warning: ACPI tables are larger than 64k.");
1973 error_report("Warning: migration may not work.");
1974 error_report("Warning: please remove CPUs, NUMA nodes, "
1975 "memory slots or PCI bridges.");
18045fb9 1976 }
7c2c1fa5 1977 acpi_align_size(tables_blob, ACPI_BUILD_TABLE_SIZE);
07fb6176 1978 }
72c194f7 1979
07fb6176 1980 acpi_align_size(tables->linker, ACPI_BUILD_ALIGN_SIZE);
72c194f7
MT
1981
1982 /* Cleanup memory that's no longer used. */
1983 g_array_free(table_offsets, true);
1984}
1985
339240b5 1986static void acpi_ram_update(MemoryRegion *mr, GArray *data)
42d85900
MT
1987{
1988 uint32_t size = acpi_data_len(data);
1989
1990 /* Make sure RAM size is correct - in case it got changed e.g. by migration */
339240b5 1991 memory_region_ram_resize(mr, size, &error_abort);
42d85900 1992
339240b5
PB
1993 memcpy(memory_region_get_ram_ptr(mr), data->data, size);
1994 memory_region_set_dirty(mr, 0, size);
42d85900
MT
1995}
1996
3f8752b4 1997static void acpi_build_update(void *build_opaque)
72c194f7
MT
1998{
1999 AcpiBuildState *build_state = build_opaque;
2000 AcpiBuildTables tables;
2001
2002 /* No state to update or already patched? Nothing to do. */
2003 if (!build_state || build_state->patched) {
2004 return;
2005 }
2006 build_state->patched = 1;
2007
2008 acpi_build_tables_init(&tables);
2009
2010 acpi_build(build_state->guest_info, &tables);
2011
339240b5 2012 acpi_ram_update(build_state->table_mr, tables.table_data);
a1666142 2013
42d85900
MT
2014 if (build_state->rsdp) {
2015 memcpy(build_state->rsdp, tables.rsdp->data, acpi_data_len(tables.rsdp));
2016 } else {
339240b5 2017 acpi_ram_update(build_state->rsdp_mr, tables.rsdp);
42d85900 2018 }
ad5b88b1 2019
339240b5 2020 acpi_ram_update(build_state->linker_mr, tables.linker);
72c194f7
MT
2021 acpi_build_tables_cleanup(&tables, true);
2022}
2023
2024static void acpi_build_reset(void *build_opaque)
2025{
2026 AcpiBuildState *build_state = build_opaque;
2027 build_state->patched = 0;
2028}
2029
339240b5
PB
2030static MemoryRegion *acpi_add_rom_blob(AcpiBuildState *build_state,
2031 GArray *blob, const char *name,
2032 uint64_t max_size)
72c194f7 2033{
a1666142
MT
2034 return rom_add_blob(name, blob->data, acpi_data_len(blob), max_size, -1,
2035 name, acpi_build_update, build_state);
72c194f7
MT
2036}
2037
2038static const VMStateDescription vmstate_acpi_build = {
2039 .name = "acpi_build",
2040 .version_id = 1,
2041 .minimum_version_id = 1,
d49805ae 2042 .fields = (VMStateField[]) {
72c194f7
MT
2043 VMSTATE_UINT8(patched, AcpiBuildState),
2044 VMSTATE_END_OF_LIST()
2045 },
2046};
2047
2048void acpi_setup(PcGuestInfo *guest_info)
2049{
2050 AcpiBuildTables tables;
2051 AcpiBuildState *build_state;
2052
2053 if (!guest_info->fw_cfg) {
8b310fc4 2054 ACPI_BUILD_DPRINTF("No fw cfg. Bailing out.\n");
72c194f7
MT
2055 return;
2056 }
2057
2058 if (!guest_info->has_acpi_build) {
8b310fc4 2059 ACPI_BUILD_DPRINTF("ACPI build disabled. Bailing out.\n");
72c194f7
MT
2060 return;
2061 }
2062
81adc513 2063 if (!acpi_enabled) {
8b310fc4 2064 ACPI_BUILD_DPRINTF("ACPI disabled. Bailing out.\n");
81adc513
MT
2065 return;
2066 }
2067
72c194f7
MT
2068 build_state = g_malloc0(sizeof *build_state);
2069
2070 build_state->guest_info = guest_info;
2071
99fd437d
MT
2072 acpi_set_pci_info();
2073
72c194f7
MT
2074 acpi_build_tables_init(&tables);
2075 acpi_build(build_state->guest_info, &tables);
2076
2077 /* Now expose it all to Guest */
339240b5 2078 build_state->table_mr = acpi_add_rom_blob(build_state, tables.table_data,
a1666142
MT
2079 ACPI_BUILD_TABLE_FILE,
2080 ACPI_BUILD_TABLE_MAX_SIZE);
339240b5 2081 assert(build_state->table_mr != NULL);
72c194f7 2082
339240b5 2083 build_state->linker_mr =
6e00619b 2084 acpi_add_rom_blob(build_state, tables.linker, "etc/table-loader", 0);
72c194f7 2085
42a5b308
SB
2086 fw_cfg_add_file(guest_info->fw_cfg, ACPI_BUILD_TPMLOG_FILE,
2087 tables.tcpalog->data, acpi_data_len(tables.tcpalog));
2088
384fb32e 2089 if (!guest_info->rsdp_in_ram) {
358774d7
IM
2090 /*
2091 * Keep for compatibility with old machine types.
2092 * Though RSDP is small, its contents isn't immutable, so
afaa2e4b 2093 * we'll update it along with the rest of tables on guest access.
358774d7 2094 */
afaa2e4b
MT
2095 uint32_t rsdp_size = acpi_data_len(tables.rsdp);
2096
2097 build_state->rsdp = g_memdup(tables.rsdp->data, rsdp_size);
358774d7
IM
2098 fw_cfg_add_file_callback(guest_info->fw_cfg, ACPI_BUILD_RSDP_FILE,
2099 acpi_build_update, build_state,
afaa2e4b 2100 build_state->rsdp, rsdp_size);
339240b5 2101 build_state->rsdp_mr = NULL;
358774d7 2102 } else {
42d85900 2103 build_state->rsdp = NULL;
339240b5 2104 build_state->rsdp_mr = acpi_add_rom_blob(build_state, tables.rsdp,
42d85900 2105 ACPI_BUILD_RSDP_FILE, 0);
358774d7 2106 }
72c194f7
MT
2107
2108 qemu_register_reset(acpi_build_reset, build_state);
2109 acpi_build_reset(build_state);
2110 vmstate_register(NULL, 0, &vmstate_acpi_build, build_state);
2111
2112 /* Cleanup tables but don't free the memory: we track it
2113 * in build_state.
2114 */
2115 acpi_build_tables_cleanup(&tables, false);
2116}
This page took 0.450601 seconds and 4 git commands to generate.