blob: 2e5b1a6c0cde1a433d43e30a17e7a7a2a6cb5b2d [file] [log] [blame]
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001/* 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"
Paolo Bonzini07fb6172014-07-28 17:34:15 +020028#include "qemu/osdep.h"
Paolo Bonzini07fb6172014-07-28 17:34:15 +020029#include "qemu/error-report.h"
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +030030#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"
Shannon Zhao395e5fb2015-04-03 18:03:33 +080035#include "hw/acpi/acpi-defs.h"
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +030036#include "hw/acpi/acpi.h"
37#include "hw/nvram/fw_cfg.h"
Michael S. Tsirkin0058ae12015-01-19 23:58:55 +020038#include "hw/acpi/bios-linker-loader.h"
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +030039#include "hw/loader.h"
Gabriel L. Somlo15bce1b2013-12-22 10:34:56 -050040#include "hw/isa/isa.h"
Igor Mammedovbef34922014-06-02 15:25:26 +020041#include "hw/acpi/memory_hotplug.h"
Xiao Guangrong87252e12015-12-02 15:20:58 +080042#include "hw/mem/nvdimm.h"
Stefan Berger711b20b2014-08-11 16:33:36 -040043#include "sysemu/tpm.h"
44#include "hw/acpi/tpm.h"
Stefan Berger5cb18b32015-05-26 16:51:07 -040045#include "sysemu/tpm_backend.h"
Laszlo Ersekf070efa2015-12-10 18:25:34 +010046#include "hw/timer/mc146818rtc_regs.h"
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +030047
48/* Supported chipsets: */
49#include "hw/acpi/piix4.h"
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +030050#include "hw/acpi/pcihp.h"
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +030051#include "hw/i386/ich9.h"
52#include "hw/pci/pci_bus.h"
53#include "hw/pci-host/q35.h"
Le Tand4eb9112014-08-16 13:55:39 +080054#include "hw/i386/intel_iommu.h"
Igor Mammedova57d7082015-12-28 18:02:29 +010055#include "hw/timer/hpet.h"
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +030056
57#include "hw/i386/q35-acpi-dsdt.hex"
58#include "hw/i386/acpi-dsdt.hex"
59
Igor Mammedov19934e02015-01-30 13:29:36 +000060#include "hw/acpi/aml-build.h"
61
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +030062#include "qapi/qmp/qint.h"
63#include "qom/qom-qobject.h"
64
Paolo Bonzini07fb6172014-07-28 17:34:15 +020065/* 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
Michael S. Tsirkin868270f2014-07-28 23:07:11 +020073#define ACPI_BUILD_TABLE_SIZE 0x20000
Paolo Bonzini18045fb2014-07-28 17:34:16 +020074
Gonglei8b310fc2014-11-13 10:59:37 +080075/* #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
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +030083typedef struct AcpiCpuInfo {
Eduardo Habkost798325e2014-03-14 16:33:53 -030084 DECLARE_BITMAP(found_cpus, ACPI_CPU_HOTPLUG_ID_LIMIT);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +030085} 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;
Igor Mammedov133a2da2014-07-28 17:34:18 +020095 bool pcihp_bridge_en;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +030096 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;
Igor Mammedovddf1ec22015-02-18 19:14:44 +0000103 uint16_t cpu_hp_io_base;
104 uint16_t cpu_hp_io_len;
Igor Mammedov2c6b94d2015-02-18 19:14:47 +0000105 uint16_t mem_hp_io_base;
106 uint16_t mem_hp_io_len;
Igor Mammedov500b11e2015-02-18 19:14:50 +0000107 uint16_t pcihp_io_base;
108 uint16_t pcihp_io_len;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300109} AcpiPmInfo;
110
111typedef struct AcpiMiscInfo {
112 bool has_hpet;
Stefan Berger5cb18b32015-05-26 16:51:07 -0400113 TPMVersion tpm_version;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300114 const unsigned char *dsdt_code;
115 unsigned dsdt_size;
116 uint16_t pvpanic_port;
Igor Mammedov8ac6f7a2015-02-20 18:22:12 +0000117 uint16_t applesmc_io_base;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300118} AcpiMiscInfo;
119
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300120typedef struct AcpiBuildPciBusHotplugState {
121 GArray *device_table;
122 GArray *notify_table;
123 struct AcpiBuildPciBusHotplugState *parent;
Igor Mammedov133a2da2014-07-28 17:34:18 +0200124 bool pcihp_bridge_en;
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300125} AcpiBuildPciBusHotplugState;
126
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300127static 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);
Eduardo Habkost798325e2014-03-14 16:33:53 -0300151 assert(apic_id < ACPI_CPU_HOTPLUG_ID_LIMIT);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300152
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
Daniel P. Berrange94aaca62015-07-31 11:14:35 +0100175 pm->cpu_hp_io_base = 0;
Igor Mammedov500b11e2015-02-18 19:14:50 +0000176 pm->pcihp_io_base = 0;
177 pm->pcihp_io_len = 0;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300178 if (piix) {
179 obj = piix;
Igor Mammedovddf1ec22015-02-18 19:14:44 +0000180 pm->cpu_hp_io_base = PIIX4_CPU_HOTPLUG_IO_BASE;
Igor Mammedov500b11e2015-02-18 19:14:50 +0000181 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);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300185 }
186 if (lpc) {
187 obj = lpc;
Igor Mammedovddf1ec22015-02-18 19:14:44 +0000188 pm->cpu_hp_io_base = ICH9_CPU_HOTPLUG_IO_BASE;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300189 }
190 assert(obj);
191
Igor Mammedovddf1ec22015-02-18 19:14:44 +0000192 pm->cpu_hp_io_len = ACPI_GPE_PROC_LEN;
Igor Mammedov2c6b94d2015-02-18 19:14:47 +0000193 pm->mem_hp_io_base = ACPI_MEMORY_HOTPLUG_BASE;
194 pm->mem_hp_io_len = ACPI_MEMORY_HOTPLUG_IO_LEN;
195
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300196 /* 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 }
Kirill Batuzov097a97a2014-04-24 18:15:57 +0400203 qobject_decref(o);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300204 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 }
Kirill Batuzov097a97a2014-04-24 18:15:57 +0400210 qobject_decref(o);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300211 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 }
Kirill Batuzov097a97a2014-04-24 18:15:57 +0400217 qobject_decref(o);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300218
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);
Igor Mammedov133a2da2014-07-28 17:34:18 +0200234 pm->pcihp_bridge_en =
235 object_property_get_bool(obj, "acpi-pci-hotplug-with-bridge-support",
236 NULL);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300237}
238
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300239static void acpi_get_misc_info(AcpiMiscInfo *info)
240{
241 info->has_hpet = hpet_find();
Stefan Berger5cb18b32015-05-26 16:51:07 -0400242 info->tpm_version = tpm_get_version();
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300243 info->pvpanic_port = pvpanic_port();
Igor Mammedov8ac6f7a2015-02-20 18:22:12 +0000244 info->applesmc_io_base = applesmc_port();
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300245}
246
Marcel Apfelbaumca6c1852015-06-02 14:22:59 +0300247/*
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
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300267static void acpi_get_pci_info(PcPciInfo *info)
268{
269 Object *pci_host;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300270
Marcel Apfelbaumca6c1852015-06-02 14:22:59 +0300271
272 pci_host = acpi_get_i386_pci_host();
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300273 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
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300289#define ACPI_PORT_SMI_CMD 0x00b2 /* TODO: this is APM_CNT_IOPORT */
290
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300291static 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 */
Michael S. Tsirkin134d42d2013-11-26 00:00:39 +0200296 g_array_set_size(blob, ROUND_UP(acpi_data_len(blob), align));
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300297}
298
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300299/* FACS */
300static void
301build_facs(GArray *table_data, GArray *linker, PcGuestInfo *guest_info)
302{
303 AcpiFacsDescriptorRev1 *facs = acpi_data_push(table_data, sizeof *facs);
Michael S. Tsirkin821e3222014-03-18 15:49:41 +0200304 memcpy(&facs->signature, "FACS", 4);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300305 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);
zhanghailiang07b81ed2014-08-29 11:52:51 +0800334 /* 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 }
Laszlo Ersekf070efa2015-12-10 18:25:34 +0100340 fadt->century = RTC_CENTURY;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300341}
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,
Xiao Guangrong8870ca02015-12-02 15:20:57 +0800368 (void *)fadt, "FACP", sizeof(*fadt), 1, NULL);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300369}
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,
Michael S. Tsirkin821e3222014-03-18 15:49:41 +0200437 (void *)(table_data->data + madt_start), "APIC",
Xiao Guangrong8870ca02015-12-02 15:20:57 +0800438 table_data->len - madt_start, 1, NULL);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300439}
440
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300441/* 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;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300448
Igor Mammedov39b888b2014-09-26 09:28:17 +0000449 if (qbus_is_hotpluggable(BUS(bus))) {
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300450 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);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300455 }
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300456
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
Igor Mammedov62b52c22015-02-20 18:22:18 +0000471static void build_append_pcihp_notify_entry(Aml *method, int slot)
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300472{
Igor Mammedov62b52c22015-02-20 18:22:18 +0000473 Aml *if_ctx;
474 int32_t devfn = PCI_DEVFN(slot, 0);
Igor Mammedovb23046a2015-02-20 18:22:16 +0000475
Igor Mammedov55304272015-12-10 00:41:17 +0100476 if_ctx = aml_if(aml_and(aml_arg(0), aml_int(0x1U << slot), NULL));
Igor Mammedov62b52c22015-02-20 18:22:18 +0000477 aml_append(if_ctx, aml_notify(aml_name("S%.02X", devfn), aml_arg(1)));
478 aml_append(method, if_ctx);
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300479}
480
Igor Mammedov62b52c22015-02-20 18:22:18 +0000481static void build_append_pci_bus_devices(Aml *parent_scope, PCIBus *bus,
Igor Mammedovb23046a2015-02-20 18:22:16 +0000482 bool pcihp_bridge_en)
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300483{
Igor Mammedov62b52c22015-02-20 18:22:18 +0000484 Aml *dev, *notify_method, *method;
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300485 QObject *bsel;
Igor Mammedovb23046a2015-02-20 18:22:16 +0000486 PCIBus *sec;
487 int i;
Igor Mammedov133a2da2014-07-28 17:34:18 +0200488
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300489 bsel = object_property_get_qobject(OBJECT(bus), ACPI_PCIHP_PROP_BSEL, NULL);
490 if (bsel) {
Igor Mammedov62b52c22015-02-20 18:22:18 +0000491 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)));
Xiao Guangrong4dbfc882015-12-17 13:37:13 +0000494 notify_method = aml_method("DVNT", 2, AML_NOTSERIALIZED);
Michael S. Tsirkin8dcf5252014-02-04 17:43:47 +0200495 }
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300496
Michael S. Tsirkin8dcf5252014-02-04 17:43:47 +0200497 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);
Igor Mammedovb23046a2015-02-20 18:22:16 +0000502 bool hotplug_enabled_dev;
Michael S. Tsirkin093a35e2014-07-28 22:56:45 +0200503 bool bridge_in_acpi;
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300504
Michael S. Tsirkin8dcf5252014-02-04 17:43:47 +0200505 if (!pdev) {
Igor Mammedovb23046a2015-02-20 18:22:16 +0000506 if (bsel) { /* add hotplug slots for non present devices */
Igor Mammedov62b52c22015-02-20 18:22:18 +0000507 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)));
Xiao Guangrong4dbfc882015-12-17 13:37:13 +0000510 method = aml_method("_EJ0", 1, AML_NOTSERIALIZED);
Igor Mammedov62b52c22015-02-20 18:22:18 +0000511 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);
Igor Mammedovb23046a2015-02-20 18:22:16 +0000516
Igor Mammedov62b52c22015-02-20 18:22:18 +0000517 build_append_pcihp_notify_entry(notify_method, slot);
Igor Mammedovb23046a2015-02-20 18:22:16 +0000518 }
Michael S. Tsirkin8dcf5252014-02-04 17:43:47 +0200519 continue;
520 }
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300521
Michael S. Tsirkin8dcf5252014-02-04 17:43:47 +0200522 pc = PCI_DEVICE_GET_CLASS(pdev);
523 dc = DEVICE_GET_CLASS(pdev);
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300524
Michael S. Tsirkin093a35e2014-07-28 22:56:45 +0200525 /* 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.
Michael S. Tsirkina20275f2015-01-28 18:30:38 +0200528 * Hotplugged bridges *are* hot-pluggable.
Michael S. Tsirkin093a35e2014-07-28 22:56:45 +0200529 */
Igor Mammedovb23046a2015-02-20 18:22:16 +0000530 bridge_in_acpi = pc->is_bridge && pcihp_bridge_en &&
531 !DEVICE(pdev)->hotplugged;
Michael S. Tsirkin093a35e2014-07-28 22:56:45 +0200532
Igor Mammedovb23046a2015-02-20 18:22:16 +0000533 hotplug_enabled_dev = bsel && dc->hotpluggable && !bridge_in_acpi;
534
535 if (pc->class_id == PCI_CLASS_BRIDGE_ISA) {
536 continue;
Michael S. Tsirkin8dcf5252014-02-04 17:43:47 +0200537 }
538
Igor Mammedov62b52c22015-02-20 18:22:18 +0000539 /* 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
Michael S. Tsirkin8dcf5252014-02-04 17:43:47 +0200543 if (pc->class_id == PCI_CLASS_DISPLAY_VGA) {
Igor Mammedov62b52c22015-02-20 18:22:18 +0000544 /* add VGA specific AML methods */
545 int s3d;
546
Michael S. Tsirkin8dcf5252014-02-04 17:43:47 +0200547 if (object_dynamic_cast(OBJECT(pdev), "qxl-vga")) {
Igor Mammedov62b52c22015-02-20 18:22:18 +0000548 s3d = 3;
Igor Mammedovb23046a2015-02-20 18:22:16 +0000549 } else {
Igor Mammedov62b52c22015-02-20 18:22:18 +0000550 s3d = 0;
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300551 }
Igor Mammedov62b52c22015-02-20 18:22:18 +0000552
Xiao Guangrong4dbfc882015-12-17 13:37:13 +0000553 method = aml_method("_S1D", 0, AML_NOTSERIALIZED);
Igor Mammedov62b52c22015-02-20 18:22:18 +0000554 aml_append(method, aml_return(aml_int(0)));
555 aml_append(dev, method);
556
Xiao Guangrong4dbfc882015-12-17 13:37:13 +0000557 method = aml_method("_S2D", 0, AML_NOTSERIALIZED);
Igor Mammedov62b52c22015-02-20 18:22:18 +0000558 aml_append(method, aml_return(aml_int(0)));
559 aml_append(dev, method);
560
Xiao Guangrong4dbfc882015-12-17 13:37:13 +0000561 method = aml_method("_S3D", 0, AML_NOTSERIALIZED);
Igor Mammedov62b52c22015-02-20 18:22:18 +0000562 aml_append(method, aml_return(aml_int(s3d)));
563 aml_append(dev, method);
Igor Mammedovb23046a2015-02-20 18:22:16 +0000564 } else if (hotplug_enabled_dev) {
Igor Mammedov62b52c22015-02-20 18:22:18 +0000565 /* add _SUN/_EJ0 to make slot hotpluggable */
566 aml_append(dev, aml_name_decl("_SUN", aml_int(slot)));
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300567
Xiao Guangrong4dbfc882015-12-17 13:37:13 +0000568 method = aml_method("_EJ0", 1, AML_NOTSERIALIZED);
Igor Mammedov62b52c22015-02-20 18:22:18 +0000569 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 }
Igor Mammedovb23046a2015-02-20 18:22:16 +0000577 } else if (bridge_in_acpi) {
Igor Mammedov62b52c22015-02-20 18:22:18 +0000578 /*
579 * device is coldplugged bridge,
580 * add child device descriptions into its scope
581 */
Igor Mammedovb23046a2015-02-20 18:22:16 +0000582 PCIBus *sec_bus = pci_bridge_get_sec_bus(PCI_BRIDGE(pdev));
Igor Mammedovb23046a2015-02-20 18:22:16 +0000583
Igor Mammedov62b52c22015-02-20 18:22:18 +0000584 build_append_pci_bus_devices(dev, sec_bus, pcihp_bridge_en);
Michael S. Tsirkin8dcf5252014-02-04 17:43:47 +0200585 }
Igor Mammedov62b52c22015-02-20 18:22:18 +0000586 /* slot descriptor has been composed, add it into parent context */
587 aml_append(parent_scope, dev);
Michael S. Tsirkin8dcf5252014-02-04 17:43:47 +0200588 }
589
590 if (bsel) {
Igor Mammedov62b52c22015-02-20 18:22:18 +0000591 aml_append(parent_scope, notify_method);
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300592 }
593
594 /* Append PCNT method to notify about events on local and child buses.
595 * Add unconditionally for root since DSDT expects it.
596 */
Xiao Guangrong4dbfc882015-12-17 13:37:13 +0000597 method = aml_method("PCNT", 0, AML_NOTSERIALIZED);
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300598
Igor Mammedovb23046a2015-02-20 18:22:16 +0000599 /* If bus supports hotplug select it and notify about local events */
600 if (bsel) {
Igor Mammedov62b52c22015-02-20 18:22:18 +0000601 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 );
Igor Mammedovb23046a2015-02-20 18:22:16 +0000609 }
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300610
Igor Mammedovb23046a2015-02-20 18:22:16 +0000611 /* Notify about child bus events in any case */
612 if (pcihp_bridge_en) {
613 QLIST_FOREACH(sec, &bus->child, sibling) {
Igor Mammedov62b52c22015-02-20 18:22:18 +0000614 int32_t devfn = sec->parent_dev->devfn;
615
616 aml_append(method, aml_name("^S%.02X.PCNT", devfn));
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +0300617 }
618 }
Igor Mammedov62b52c22015-02-20 18:22:18 +0000619 aml_append(parent_scope, method);
Shannon Zhaod370dfa2015-05-26 09:46:07 +0800620 qobject_decref(bsel);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +0300621}
622
Marcel Apfelbaum0d8935e2015-06-02 14:23:02 +0300623/*
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
Xiao Guangrong4dbfc882015-12-17 13:37:13 +0000658 method = aml_method("_PRT", 0, AML_NOTSERIALIZED);
Marcel Apfelbaum0d8935e2015-06-02 14:23:02 +0300659 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,
Igor Mammedovc3606392015-12-10 00:41:06 +0100673 aml_store(aml_shiftright(pin, aml_int(2), NULL), slot));
Marcel Apfelbaum0d8935e2015-06-02 14:23:02 +0300674 /* lnk_idx = (slot + pin) & 3 */
675 aml_append(while_ctx,
Igor Mammedov55304272015-12-10 00:41:17 +0100676 aml_store(aml_and(aml_add(pin, slot, NULL), aml_int(3), NULL),
677 lnk_idx));
Marcel Apfelbaum0d8935e2015-06-02 14:23:02 +0300678
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,
Igor Mammedovca3df952015-12-10 00:41:16 +0100687 aml_store(aml_or(aml_shiftleft(slot, aml_int(16)), aml_int(0xFFFF),
688 NULL),
Marcel Apfelbaum0d8935e2015-06-02 14:23:02 +0300689 aml_index(route, aml_int(0))));
690 /* route[1] = pin & 3 */
691 aml_append(while_ctx,
Igor Mammedov55304272015-12-10 00:41:17 +0100692 aml_store(aml_and(pin, aml_int(3), NULL),
693 aml_index(route, aml_int(1))));
Marcel Apfelbaum0d8935e2015-06-02 14:23:02 +0300694 /* 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
Marcel Apfelbauma43c6e22015-06-02 14:23:03 +0300706typedef 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
Marcel Apfelbaumdcdca292015-06-02 14:23:04 +0300728static 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
Marcel Apfelbaumd7fd0e62015-11-26 18:00:26 +0200772/*
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
Marcel Apfelbauma43c6e22015-06-02 14:23:03 +0300812static Aml *build_crs(PCIHostState *host,
813 GPtrArray *io_ranges, GPtrArray *mem_ranges)
814{
815 Aml *crs = aml_resource_template();
Marcel Apfelbaumd7fd0e62015-11-26 18:00:26 +0200816 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;
Marcel Apfelbauma43c6e22015-06-02 14:23:03 +0300819 uint8_t max_bus = pci_bus_num(host->bus);
820 uint8_t type;
821 int devfn;
Marcel Apfelbaumd7fd0e62015-11-26 18:00:26 +0200822 int i;
Marcel Apfelbauma43c6e22015-06-02 14:23:03 +0300823
824 for (devfn = 0; devfn < ARRAY_SIZE(host->bus->devices); devfn++) {
Marcel Apfelbauma43c6e22015-06-02 14:23:03 +0300825 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
Marcel Apfelbaum0f6dd8e2015-06-02 14:23:11 +0300838 /*
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
Marcel Apfelbauma43c6e22015-06-02 14:23:03 +0300846 if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
Marcel Apfelbaumd7fd0e62015-11-26 18:00:26 +0200847 crs_range_insert(host_io_ranges, range_base, range_limit);
Marcel Apfelbauma43c6e22015-06-02 14:23:03 +0300848 } else { /* "memory" */
Marcel Apfelbaumd7fd0e62015-11-26 18:00:26 +0200849 crs_range_insert(host_mem_ranges, range_base, range_limit);
Marcel Apfelbauma43c6e22015-06-02 14:23:03 +0300850 }
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);
Marcel Apfelbaum0f6dd8e2015-06-02 14:23:11 +0300862
863 /*
864 * Work-around for old bioses
865 * that do not support multiple root buses
866 */
Laszlo Ersek4ebc7362015-06-11 02:37:59 +0200867 if (range_base && range_base <= range_limit) {
Marcel Apfelbaumd7fd0e62015-11-26 18:00:26 +0200868 crs_range_insert(host_io_ranges, range_base, range_limit);
Marcel Apfelbaum0f6dd8e2015-06-02 14:23:11 +0300869 }
Marcel Apfelbauma43c6e22015-06-02 14:23:03 +0300870
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);
Marcel Apfelbaum0f6dd8e2015-06-02 14:23:11 +0300875
876 /*
877 * Work-around for old bioses
878 * that do not support multiple root buses
879 */
Laszlo Ersek4ebc7362015-06-11 02:37:59 +0200880 if (range_base && range_base <= range_limit) {
Marcel Apfelbaumd7fd0e62015-11-26 18:00:26 +0200881 crs_range_insert(host_mem_ranges, range_base, range_limit);
Laszlo Ersek4ebc7362015-06-11 02:37:59 +0200882 }
Marcel Apfelbauma43c6e22015-06-02 14:23:03 +0300883
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);
Marcel Apfelbaum0f6dd8e2015-06-02 14:23:11 +0300888
889 /*
890 * Work-around for old bioses
891 * that do not support multiple root buses
892 */
Laszlo Ersek4ebc7362015-06-11 02:37:59 +0200893 if (range_base && range_base <= range_limit) {
Marcel Apfelbaumd7fd0e62015-11-26 18:00:26 +0200894 crs_range_insert(host_mem_ranges, range_base, range_limit);
Marcel Apfelbaum0f6dd8e2015-06-02 14:23:11 +0300895 }
Marcel Apfelbauma43c6e22015-06-02 14:23:03 +0300896 }
897 }
898
Marcel Apfelbaumd7fd0e62015-11-26 18:00:26 +0200899 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
Marcel Apfelbauma43c6e22015-06-02 14:23:03 +0300924 aml_append(crs,
Marcel Apfelbaumdcdca292015-06-02 14:23:04 +0300925 aml_word_bus_number(AML_MIN_FIXED, AML_MAX_FIXED, AML_POS_DECODE,
Marcel Apfelbauma43c6e22015-06-02 14:23:03 +0300926 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
Igor Mammedov5ca5efa2015-12-28 18:02:28 +0100935static 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
Igor Mammedovf177d402015-12-28 18:02:19 +01001027static 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);
Igor Mammedovf84548d2015-12-28 18:02:21 +01001040 scope = aml_scope("\\_SB.PCI0." MEMORY_HOTPLUG_DEVICE);
Igor Mammedovf177d402015-12-28 18:02:19 +01001041 aml_append(scope,
Igor Mammedovf84548d2015-12-28 18:02:21 +01001042 aml_name_decl(MEMORY_SLOTS_NUMBER, aml_int(nr_mem))
Igor Mammedovf177d402015-12-28 18:02:19 +01001043 );
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(
Igor Mammedovf84548d2015-12-28 18:02:21 +01001052 MEMORY_HOTPLUG_IO_REGION, AML_SYSTEM_IO,
Igor Mammedovf177d402015-12-28 18:02:19 +01001053 io_base, io_len)
1054 );
1055
Igor Mammedovf84548d2015-12-28 18:02:21 +01001056 field = aml_field(MEMORY_HOTPLUG_IO_REGION, AML_DWORD_ACC,
Igor Mammedovf177d402015-12-28 18:02:19 +01001057 AML_NOLOCK, AML_PRESERVE);
1058 aml_append(field, /* read only */
Igor Mammedovf84548d2015-12-28 18:02:21 +01001059 aml_named_field(MEMORY_SLOT_ADDR_LOW, 32));
Igor Mammedovf177d402015-12-28 18:02:19 +01001060 aml_append(field, /* read only */
Igor Mammedovf84548d2015-12-28 18:02:21 +01001061 aml_named_field(MEMORY_SLOT_ADDR_HIGH, 32));
Igor Mammedovf177d402015-12-28 18:02:19 +01001062 aml_append(field, /* read only */
Igor Mammedovf84548d2015-12-28 18:02:21 +01001063 aml_named_field(MEMORY_SLOT_SIZE_LOW, 32));
Igor Mammedovf177d402015-12-28 18:02:19 +01001064 aml_append(field, /* read only */
Igor Mammedovf84548d2015-12-28 18:02:21 +01001065 aml_named_field(MEMORY_SLOT_SIZE_HIGH, 32));
Igor Mammedovf177d402015-12-28 18:02:19 +01001066 aml_append(field, /* read only */
Igor Mammedovf84548d2015-12-28 18:02:21 +01001067 aml_named_field(MEMORY_SLOT_PROXIMITY, 32));
Igor Mammedovf177d402015-12-28 18:02:19 +01001068 aml_append(scope, field);
1069
Igor Mammedovf84548d2015-12-28 18:02:21 +01001070 field = aml_field(MEMORY_HOTPLUG_IO_REGION, AML_BYTE_ACC,
Igor Mammedovf177d402015-12-28 18:02:19 +01001071 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 */
Igor Mammedovf84548d2015-12-28 18:02:21 +01001074 aml_named_field(MEMORY_SLOT_ENABLED, 1));
Igor Mammedovf177d402015-12-28 18:02:19 +01001075 aml_append(field,
1076 /*(read) 1 if has a insert event. (write) 1 to clear event */
Igor Mammedovf84548d2015-12-28 18:02:21 +01001077 aml_named_field(MEMORY_SLOT_INSERT_EVENT, 1));
Igor Mammedovf177d402015-12-28 18:02:19 +01001078 aml_append(field,
1079 /* (read) 1 if has a remove event. (write) 1 to clear event */
Igor Mammedovf84548d2015-12-28 18:02:21 +01001080 aml_named_field(MEMORY_SLOT_REMOVE_EVENT, 1));
Igor Mammedovf177d402015-12-28 18:02:19 +01001081 aml_append(field,
1082 /* initiates device eject, write only */
Igor Mammedovf84548d2015-12-28 18:02:21 +01001083 aml_named_field(MEMORY_SLOT_EJECT, 1));
Igor Mammedovf177d402015-12-28 18:02:19 +01001084 aml_append(scope, field);
1085
Igor Mammedovf84548d2015-12-28 18:02:21 +01001086 field = aml_field(MEMORY_HOTPLUG_IO_REGION, AML_DWORD_ACC,
Igor Mammedovf177d402015-12-28 18:02:19 +01001087 AML_NOLOCK, AML_PRESERVE);
1088 aml_append(field, /* DIMM selector, write only */
Igor Mammedovf84548d2015-12-28 18:02:21 +01001089 aml_named_field(MEMORY_SLOT_SLECTOR, 32));
Igor Mammedovf177d402015-12-28 18:02:19 +01001090 aml_append(field, /* _OST event code, write only */
Igor Mammedovf84548d2015-12-28 18:02:21 +01001091 aml_named_field(MEMORY_SLOT_OST_EVENT, 32));
Igor Mammedovf177d402015-12-28 18:02:19 +01001092 aml_append(field, /* _OST status code, write only */
Igor Mammedovf84548d2015-12-28 18:02:21 +01001093 aml_named_field(MEMORY_SLOT_OST_STATUS, 32));
Igor Mammedovf177d402015-12-28 18:02:19 +01001094 aml_append(scope, field);
1095 aml_append(sb_scope, scope);
1096
1097 for (i = 0; i < nr_mem; i++) {
Igor Mammedovf84548d2015-12-28 18:02:21 +01001098 #define BASEPATH "\\_SB.PCI0." MEMORY_HOTPLUG_DEVICE "."
Igor Mammedovf177d402015-12-28 18:02:19 +01001099 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);
Igor Mammedovf84548d2015-12-28 18:02:21 +01001106 s = BASEPATH MEMORY_SLOT_CRS_METHOD;
Igor Mammedovf177d402015-12-28 18:02:19 +01001107 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);
Igor Mammedovf84548d2015-12-28 18:02:21 +01001111 s = BASEPATH MEMORY_SLOT_STATUS_METHOD;
Igor Mammedovf177d402015-12-28 18:02:19 +01001112 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);
Igor Mammedovf84548d2015-12-28 18:02:21 +01001116 s = BASEPATH MEMORY_SLOT_PROXIMITY_METHOD;
Igor Mammedovf177d402015-12-28 18:02:19 +01001117 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);
Igor Mammedovf84548d2015-12-28 18:02:21 +01001121 s = BASEPATH MEMORY_SLOT_OST_METHOD;
1122
Igor Mammedovf177d402015-12-28 18:02:19 +01001123 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);
Igor Mammedovf84548d2015-12-28 18:02:21 +01001129 s = BASEPATH MEMORY_SLOT_EJECT_METHOD;
Igor Mammedovf177d402015-12-28 18:02:19 +01001130 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 */
Igor Mammedovf84548d2015-12-28 18:02:21 +01001140 method = aml_method(MEMORY_SLOT_NOTIFY_METHOD, 2, AML_NOTSERIALIZED);
Igor Mammedovf177d402015-12-28 18:02:19 +01001141 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
Igor Mammedova57d7082015-12-28 18:02:29 +01001151static 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
Igor Mammedov95ed7e92015-12-28 18:02:34 +01001202static Aml *build_fdc_device_aml(void)
1203{
1204 Aml *dev;
1205 Aml *crs;
1206 Aml *method;
1207 Aml *if_ctx;
1208 Aml *else_ctx;
1209 Aml *zero = aml_int(0);
1210 Aml *is_present = aml_local(0);
1211
1212 dev = aml_device("FDC0");
1213 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0700")));
1214
1215 method = aml_method("_STA", 0, AML_NOTSERIALIZED);
1216 aml_append(method, aml_store(aml_name("FDEN"), is_present));
1217 if_ctx = aml_if(aml_equal(is_present, zero));
1218 {
1219 aml_append(if_ctx, aml_return(aml_int(0x00)));
1220 }
1221 aml_append(method, if_ctx);
1222 else_ctx = aml_else();
1223 {
1224 aml_append(else_ctx, aml_return(aml_int(0x0f)));
1225 }
1226 aml_append(method, else_ctx);
1227 aml_append(dev, method);
1228
1229 crs = aml_resource_template();
1230 aml_append(crs, aml_io(AML_DECODE16, 0x03F2, 0x03F2, 0x00, 0x04));
1231 aml_append(crs, aml_io(AML_DECODE16, 0x03F7, 0x03F7, 0x00, 0x01));
1232 aml_append(crs, aml_irq_no_flags(6));
1233 aml_append(crs,
1234 aml_dma(AML_COMPATIBILITY, AML_NOTBUSMASTER, AML_TRANSFER8, 2));
1235 aml_append(dev, aml_name_decl("_CRS", crs));
1236
1237 return dev;
1238}
1239
Igor Mammedovee135842015-12-28 18:02:31 +01001240static Aml *build_rtc_device_aml(void)
1241{
1242 Aml *dev;
1243 Aml *crs;
1244
1245 dev = aml_device("RTC");
1246 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0B00")));
1247 crs = aml_resource_template();
1248 aml_append(crs, aml_io(AML_DECODE16, 0x0070, 0x0070, 0x10, 0x02));
1249 aml_append(crs, aml_irq_no_flags(8));
1250 aml_append(crs, aml_io(AML_DECODE16, 0x0072, 0x0072, 0x02, 0x06));
Igor Mammedov95ed7e92015-12-28 18:02:34 +01001251 aml_append(dev, aml_name_decl("_CRS", crs));
Igor Mammedovf58190e2015-12-28 18:02:32 +01001252
1253 return dev;
1254}
1255
1256static Aml *build_kbd_device_aml(void)
1257{
1258 Aml *dev;
1259 Aml *crs;
1260 Aml *method;
1261
1262 dev = aml_device("KBD");
1263 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0303")));
1264
1265 method = aml_method("_STA", 0, AML_NOTSERIALIZED);
1266 aml_append(method, aml_return(aml_int(0x0f)));
1267 aml_append(dev, method);
1268
1269 crs = aml_resource_template();
1270 aml_append(crs, aml_io(AML_DECODE16, 0x0060, 0x0060, 0x01, 0x01));
1271 aml_append(crs, aml_io(AML_DECODE16, 0x0064, 0x0064, 0x01, 0x01));
1272 aml_append(crs, aml_irq_no_flags(1));
Igor Mammedovee135842015-12-28 18:02:31 +01001273 aml_append(dev, aml_name_decl("_CRS", crs));
1274
1275 return dev;
1276}
1277
Igor Mammedovc355cb22015-12-28 18:02:33 +01001278static Aml *build_mouse_device_aml(void)
1279{
1280 Aml *dev;
1281 Aml *crs;
1282 Aml *method;
1283
1284 dev = aml_device("MOU");
1285 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0F13")));
1286
1287 method = aml_method("_STA", 0, AML_NOTSERIALIZED);
1288 aml_append(method, aml_return(aml_int(0x0f)));
1289 aml_append(dev, method);
1290
1291 crs = aml_resource_template();
1292 aml_append(crs, aml_irq_no_flags(12));
1293 aml_append(dev, aml_name_decl("_CRS", crs));
1294
1295 return dev;
1296}
1297
Igor Mammedov8b1da5f2015-12-28 18:02:35 +01001298static Aml *build_lpt_device_aml(void)
1299{
1300 Aml *dev;
1301 Aml *crs;
1302 Aml *method;
1303 Aml *if_ctx;
1304 Aml *else_ctx;
1305 Aml *zero = aml_int(0);
1306 Aml *is_present = aml_local(0);
1307
1308 dev = aml_device("LPT");
1309 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0400")));
1310
1311 method = aml_method("_STA", 0, AML_NOTSERIALIZED);
1312 aml_append(method, aml_store(aml_name("LPEN"), is_present));
1313 if_ctx = aml_if(aml_equal(is_present, zero));
1314 {
1315 aml_append(if_ctx, aml_return(aml_int(0x00)));
1316 }
1317 aml_append(method, if_ctx);
1318 else_ctx = aml_else();
1319 {
1320 aml_append(else_ctx, aml_return(aml_int(0x0f)));
1321 }
1322 aml_append(method, else_ctx);
1323 aml_append(dev, method);
1324
1325 crs = aml_resource_template();
1326 aml_append(crs, aml_io(AML_DECODE16, 0x0378, 0x0378, 0x08, 0x08));
1327 aml_append(crs, aml_irq_no_flags(7));
1328 aml_append(dev, aml_name_decl("_CRS", crs));
1329
1330 return dev;
1331}
1332
Igor Mammedovee135842015-12-28 18:02:31 +01001333static void build_isa_devices_aml(Aml *table)
1334{
1335 Aml *scope = aml_scope("_SB.PCI0.ISA");
1336
1337 aml_append(scope, build_rtc_device_aml());
Igor Mammedovf58190e2015-12-28 18:02:32 +01001338 aml_append(scope, build_kbd_device_aml());
Igor Mammedovc355cb22015-12-28 18:02:33 +01001339 aml_append(scope, build_mouse_device_aml());
Igor Mammedov95ed7e92015-12-28 18:02:34 +01001340 aml_append(scope, build_fdc_device_aml());
Igor Mammedov8b1da5f2015-12-28 18:02:35 +01001341 aml_append(scope, build_lpt_device_aml());
Igor Mammedovee135842015-12-28 18:02:31 +01001342
1343 aml_append(table, scope);
1344}
1345
Igor Mammedov3892a2b2015-12-28 18:02:30 +01001346static void build_dbg_aml(Aml *table)
1347{
1348 Aml *field;
1349 Aml *method;
1350 Aml *while_ctx;
1351 Aml *scope = aml_scope("\\");
1352 Aml *buf = aml_local(0);
1353 Aml *len = aml_local(1);
1354 Aml *idx = aml_local(2);
1355
1356 aml_append(scope,
1357 aml_operation_region("DBG", AML_SYSTEM_IO, 0x0402, 0x01));
1358 field = aml_field("DBG", AML_BYTE_ACC, AML_NOLOCK, AML_PRESERVE);
1359 aml_append(field, aml_named_field("DBGB", 8));
1360 aml_append(scope, field);
1361
1362 method = aml_method("DBUG", 1, AML_NOTSERIALIZED);
1363
1364 aml_append(method, aml_to_hexstring(aml_arg(0), buf));
1365 aml_append(method, aml_to_buffer(buf, buf));
1366 aml_append(method, aml_subtract(aml_sizeof(buf), aml_int(1), len));
1367 aml_append(method, aml_store(aml_int(0), idx));
1368
1369 while_ctx = aml_while(aml_lless(idx, len));
1370 aml_append(while_ctx,
1371 aml_store(aml_derefof(aml_index(buf, idx)), aml_name("DBGB")));
1372 aml_append(while_ctx, aml_increment(idx));
1373 aml_append(method, while_ctx);
1374
1375 aml_append(method, aml_store(aml_int(0x0A), aml_name("DBGB")));
1376 aml_append(scope, method);
1377
1378 aml_append(table, scope);
1379}
1380
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001381static void
1382build_ssdt(GArray *table_data, GArray *linker,
1383 AcpiCpuInfo *cpu, AcpiPmInfo *pm, AcpiMiscInfo *misc,
1384 PcPciInfo *pci, PcGuestInfo *guest_info)
1385{
Igor Mammedovbef34922014-06-02 15:25:26 +02001386 MachineState *machine = MACHINE(qdev_get_machine());
1387 uint32_t nr_mem = machine->ram_slots;
Igor Mammedov5ca5efa2015-12-28 18:02:28 +01001388 Aml *ssdt, *sb_scope, *scope, *pkg, *dev, *method, *crs, *field;
Marcel Apfelbauma4894202015-06-02 14:23:01 +03001389 PCIBus *bus = NULL;
Marcel Apfelbauma43c6e22015-06-02 14:23:03 +03001390 GPtrArray *io_ranges = g_ptr_array_new_with_free_func(crs_range_free);
1391 GPtrArray *mem_ranges = g_ptr_array_new_with_free_func(crs_range_free);
Marcel Apfelbaumdcdca292015-06-02 14:23:04 +03001392 CrsRangeEntry *entry;
1393 int root_bus_limit = 0xFF;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001394 int i;
1395
Igor Mammedov011bb742015-02-18 19:14:16 +00001396 ssdt = init_aml_allocator();
Laszlo Ersek2fd71f12014-03-17 17:05:17 +01001397
Igor Mammedov4ec8d2b2015-02-20 18:22:09 +00001398 /* Reserve space for header */
1399 acpi_data_push(ssdt->buf, sizeof(AcpiTableHeader));
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001400
Igor Mammedov3892a2b2015-12-28 18:02:30 +01001401 build_dbg_aml(ssdt);
Igor Mammedova57d7082015-12-28 18:02:29 +01001402 build_hpet_aml(ssdt);
Igor Mammedovee135842015-12-28 18:02:31 +01001403 build_isa_devices_aml(ssdt);
Igor Mammedovfbd7a6b2015-12-28 18:02:23 +01001404 build_cpu_hotplug_aml(ssdt);
Igor Mammedov30bd0cf2015-12-28 18:02:09 +01001405 build_memory_hotplug_aml(ssdt, nr_mem, pm->mem_hp_io_base,
1406 pm->mem_hp_io_len);
1407
Igor Mammedov7f4495e2015-12-28 18:02:20 +01001408 scope = aml_scope("\\_GPE");
Igor Mammedov6b306082015-12-28 18:02:27 +01001409 {
1410 method = aml_method("_E02", 0, AML_NOTSERIALIZED);
1411 aml_append(method, aml_call0("\\_SB." CPU_SCAN_METHOD));
1412 aml_append(scope, method);
1413
1414 method = aml_method("_E03", 0, AML_NOTSERIALIZED);
1415 aml_append(method, aml_call0(MEMORY_HOTPLUG_HANDLER_PATH));
1416 aml_append(scope, method);
1417 }
Igor Mammedov7f4495e2015-12-28 18:02:20 +01001418 aml_append(ssdt, scope);
1419
Marcel Apfelbaum81ed6482015-11-26 18:00:28 +02001420 bus = PC_MACHINE(machine)->bus;
Marcel Apfelbauma4894202015-06-02 14:23:01 +03001421 if (bus) {
1422 QLIST_FOREACH(bus, &bus->child, sibling) {
1423 uint8_t bus_num = pci_bus_num(bus);
Marcel Apfelbaum0e79e512015-06-02 14:23:10 +03001424 uint8_t numa_node = pci_bus_numa_node(bus);
Marcel Apfelbauma4894202015-06-02 14:23:01 +03001425
1426 /* look only for expander root buses */
1427 if (!pci_bus_is_root(bus)) {
1428 continue;
1429 }
1430
Marcel Apfelbaumdcdca292015-06-02 14:23:04 +03001431 if (bus_num < root_bus_limit) {
1432 root_bus_limit = bus_num - 1;
1433 }
1434
Marcel Apfelbauma4894202015-06-02 14:23:01 +03001435 scope = aml_scope("\\_SB");
1436 dev = aml_device("PC%.02X", bus_num);
Laszlo Ersekc96d9282015-06-11 02:37:58 +02001437 aml_append(dev, aml_name_decl("_UID", aml_int(bus_num)));
1438 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A03")));
Marcel Apfelbauma4894202015-06-02 14:23:01 +03001439 aml_append(dev, aml_name_decl("_BBN", aml_int(bus_num)));
Marcel Apfelbaum0e79e512015-06-02 14:23:10 +03001440
1441 if (numa_node != NUMA_NODE_UNASSIGNED) {
1442 aml_append(dev, aml_name_decl("_PXM", aml_int(numa_node)));
1443 }
1444
Marcel Apfelbaum0d8935e2015-06-02 14:23:02 +03001445 aml_append(dev, build_prt());
Marcel Apfelbauma43c6e22015-06-02 14:23:03 +03001446 crs = build_crs(PCI_HOST_BRIDGE(BUS(bus)->parent),
1447 io_ranges, mem_ranges);
1448 aml_append(dev, aml_name_decl("_CRS", crs));
Marcel Apfelbauma4894202015-06-02 14:23:01 +03001449 aml_append(scope, dev);
1450 aml_append(ssdt, scope);
1451 }
1452 }
1453
Igor Mammedov500b11e2015-02-18 19:14:50 +00001454 scope = aml_scope("\\_SB.PCI0");
Igor Mammedov60efd422015-02-20 18:22:05 +00001455 /* build PCI0._CRS */
1456 crs = aml_resource_template();
1457 aml_append(crs,
Shannon Zhaoff80dc72015-05-29 11:28:54 +01001458 aml_word_bus_number(AML_MIN_FIXED, AML_MAX_FIXED, AML_POS_DECODE,
Marcel Apfelbaumdcdca292015-06-02 14:23:04 +03001459 0x0000, 0x0, root_bus_limit,
1460 0x0000, root_bus_limit + 1));
Shannon Zhaoff80dc72015-05-29 11:28:54 +01001461 aml_append(crs, aml_io(AML_DECODE16, 0x0CF8, 0x0CF8, 0x01, 0x08));
Igor Mammedov60efd422015-02-20 18:22:05 +00001462
1463 aml_append(crs,
Shannon Zhaoff80dc72015-05-29 11:28:54 +01001464 aml_word_io(AML_MIN_FIXED, AML_MAX_FIXED,
1465 AML_POS_DECODE, AML_ENTIRE_RANGE,
Igor Mammedov60efd422015-02-20 18:22:05 +00001466 0x0000, 0x0000, 0x0CF7, 0x0000, 0x0CF8));
Marcel Apfelbaumdcdca292015-06-02 14:23:04 +03001467
1468 crs_replace_with_free_ranges(io_ranges, 0x0D00, 0xFFFF);
1469 for (i = 0; i < io_ranges->len; i++) {
1470 entry = g_ptr_array_index(io_ranges, i);
1471 aml_append(crs,
1472 aml_word_io(AML_MIN_FIXED, AML_MAX_FIXED,
1473 AML_POS_DECODE, AML_ENTIRE_RANGE,
1474 0x0000, entry->base, entry->limit,
1475 0x0000, entry->limit - entry->base + 1));
1476 }
1477
Igor Mammedov60efd422015-02-20 18:22:05 +00001478 aml_append(crs,
Shannon Zhaoff80dc72015-05-29 11:28:54 +01001479 aml_dword_memory(AML_POS_DECODE, AML_MIN_FIXED, AML_MAX_FIXED,
1480 AML_CACHEABLE, AML_READ_WRITE,
Igor Mammedov60efd422015-02-20 18:22:05 +00001481 0, 0x000A0000, 0x000BFFFF, 0, 0x00020000));
Marcel Apfelbaumdcdca292015-06-02 14:23:04 +03001482
1483 crs_replace_with_free_ranges(mem_ranges, pci->w32.begin, pci->w32.end - 1);
1484 for (i = 0; i < mem_ranges->len; i++) {
1485 entry = g_ptr_array_index(mem_ranges, i);
1486 aml_append(crs,
1487 aml_dword_memory(AML_POS_DECODE, AML_MIN_FIXED, AML_MAX_FIXED,
1488 AML_NON_CACHEABLE, AML_READ_WRITE,
1489 0, entry->base, entry->limit,
1490 0, entry->limit - entry->base + 1));
1491 }
1492
Igor Mammedov60efd422015-02-20 18:22:05 +00001493 if (pci->w64.begin) {
1494 aml_append(crs,
Shannon Zhaoff80dc72015-05-29 11:28:54 +01001495 aml_qword_memory(AML_POS_DECODE, AML_MIN_FIXED, AML_MAX_FIXED,
1496 AML_CACHEABLE, AML_READ_WRITE,
Igor Mammedov60efd422015-02-20 18:22:05 +00001497 0, pci->w64.begin, pci->w64.end - 1, 0,
1498 pci->w64.end - pci->w64.begin));
1499 }
1500 aml_append(scope, aml_name_decl("_CRS", crs));
1501
Igor Mammedovd31c9092015-02-20 18:22:08 +00001502 /* reserve GPE0 block resources */
1503 dev = aml_device("GPE0");
1504 aml_append(dev, aml_name_decl("_HID", aml_string("PNP0A06")));
1505 aml_append(dev, aml_name_decl("_UID", aml_string("GPE0 resources")));
1506 /* device present, functioning, decoding, not shown in UI */
1507 aml_append(dev, aml_name_decl("_STA", aml_int(0xB)));
1508 crs = aml_resource_template();
1509 aml_append(crs,
Shannon Zhaoff80dc72015-05-29 11:28:54 +01001510 aml_io(AML_DECODE16, pm->gpe0_blk, pm->gpe0_blk, 1, pm->gpe0_blk_len)
Igor Mammedovd31c9092015-02-20 18:22:08 +00001511 );
1512 aml_append(dev, aml_name_decl("_CRS", crs));
1513 aml_append(scope, dev);
1514
Marcel Apfelbaumdcdca292015-06-02 14:23:04 +03001515 g_ptr_array_free(io_ranges, true);
1516 g_ptr_array_free(mem_ranges, true);
1517
Igor Mammedov500b11e2015-02-18 19:14:50 +00001518 /* reserve PCIHP resources */
1519 if (pm->pcihp_io_len) {
1520 dev = aml_device("PHPR");
1521 aml_append(dev, aml_name_decl("_HID", aml_string("PNP0A06")));
1522 aml_append(dev,
1523 aml_name_decl("_UID", aml_string("PCI Hotplug resources")));
1524 /* device present, functioning, decoding, not shown in UI */
1525 aml_append(dev, aml_name_decl("_STA", aml_int(0xB)));
1526 crs = aml_resource_template();
1527 aml_append(crs,
Shannon Zhaoff80dc72015-05-29 11:28:54 +01001528 aml_io(AML_DECODE16, pm->pcihp_io_base, pm->pcihp_io_base, 1,
Igor Mammedov500b11e2015-02-18 19:14:50 +00001529 pm->pcihp_io_len)
1530 );
1531 aml_append(dev, aml_name_decl("_CRS", crs));
1532 aml_append(scope, dev);
1533 }
1534 aml_append(ssdt, scope);
1535
Igor Mammedovebc30282015-02-18 19:14:29 +00001536 /* create S3_ / S4_ / S5_ packages if necessary */
1537 scope = aml_scope("\\");
1538 if (!pm->s3_disabled) {
1539 pkg = aml_package(4);
1540 aml_append(pkg, aml_int(1)); /* PM1a_CNT.SLP_TYP */
1541 aml_append(pkg, aml_int(1)); /* PM1b_CNT.SLP_TYP, FIXME: not impl. */
1542 aml_append(pkg, aml_int(0)); /* reserved */
1543 aml_append(pkg, aml_int(0)); /* reserved */
1544 aml_append(scope, aml_name_decl("_S3", pkg));
1545 }
1546
1547 if (!pm->s4_disabled) {
1548 pkg = aml_package(4);
1549 aml_append(pkg, aml_int(pm->s4_val)); /* PM1a_CNT.SLP_TYP */
1550 /* PM1b_CNT.SLP_TYP, FIXME: not impl. */
1551 aml_append(pkg, aml_int(pm->s4_val));
1552 aml_append(pkg, aml_int(0)); /* reserved */
1553 aml_append(pkg, aml_int(0)); /* reserved */
1554 aml_append(scope, aml_name_decl("_S4", pkg));
1555 }
1556
1557 pkg = aml_package(4);
1558 aml_append(pkg, aml_int(0)); /* PM1a_CNT.SLP_TYP */
1559 aml_append(pkg, aml_int(0)); /* PM1b_CNT.SLP_TYP not impl. */
1560 aml_append(pkg, aml_int(0)); /* reserved */
1561 aml_append(pkg, aml_int(0)); /* reserved */
1562 aml_append(scope, aml_name_decl("_S5", pkg));
1563 aml_append(ssdt, scope);
1564
Igor Mammedov8ac6f7a2015-02-20 18:22:12 +00001565 if (misc->applesmc_io_base) {
1566 scope = aml_scope("\\_SB.PCI0.ISA");
1567 dev = aml_device("SMC");
1568
1569 aml_append(dev, aml_name_decl("_HID", aml_eisaid("APP0001")));
1570 /* device present, functioning, decoding, not shown in UI */
1571 aml_append(dev, aml_name_decl("_STA", aml_int(0xB)));
1572
1573 crs = aml_resource_template();
1574 aml_append(crs,
Shannon Zhaoff80dc72015-05-29 11:28:54 +01001575 aml_io(AML_DECODE16, misc->applesmc_io_base, misc->applesmc_io_base,
Igor Mammedov8ac6f7a2015-02-20 18:22:12 +00001576 0x01, APPLESMC_MAX_DATA_LENGTH)
1577 );
1578 aml_append(crs, aml_irq_no_flags(6));
1579 aml_append(dev, aml_name_decl("_CRS", crs));
1580
1581 aml_append(scope, dev);
1582 aml_append(ssdt, scope);
1583 }
1584
Igor Mammedovcd61cb22015-02-18 19:14:38 +00001585 if (misc->pvpanic_port) {
1586 scope = aml_scope("\\_SB.PCI0.ISA");
1587
Radim Krčmář23323332015-05-29 21:57:32 +02001588 dev = aml_device("PEVT");
Igor Mammedove65bef62015-03-30 14:18:27 +02001589 aml_append(dev, aml_name_decl("_HID", aml_string("QEMU0001")));
Igor Mammedovcd61cb22015-02-18 19:14:38 +00001590
1591 crs = aml_resource_template();
1592 aml_append(crs,
Shannon Zhaoff80dc72015-05-29 11:28:54 +01001593 aml_io(AML_DECODE16, misc->pvpanic_port, misc->pvpanic_port, 1, 1)
Igor Mammedovcd61cb22015-02-18 19:14:38 +00001594 );
1595 aml_append(dev, aml_name_decl("_CRS", crs));
1596
Shannon Zhaoff80dc72015-05-29 11:28:54 +01001597 aml_append(dev, aml_operation_region("PEOR", AML_SYSTEM_IO,
Igor Mammedovcd61cb22015-02-18 19:14:38 +00001598 misc->pvpanic_port, 1));
Igor Mammedov36de8842015-12-10 00:41:12 +01001599 field = aml_field("PEOR", AML_BYTE_ACC, AML_NOLOCK, AML_PRESERVE);
Igor Mammedovcd61cb22015-02-18 19:14:38 +00001600 aml_append(field, aml_named_field("PEPT", 8));
1601 aml_append(dev, field);
1602
Gal Hammer8ef3ea22015-07-26 11:00:51 +03001603 /* device present, functioning, decoding, shown in UI */
1604 aml_append(dev, aml_name_decl("_STA", aml_int(0xF)));
Radim Krčmář23323332015-05-29 21:57:32 +02001605
Xiao Guangrong4dbfc882015-12-17 13:37:13 +00001606 method = aml_method("RDPT", 0, AML_NOTSERIALIZED);
Igor Mammedovcd61cb22015-02-18 19:14:38 +00001607 aml_append(method, aml_store(aml_name("PEPT"), aml_local(0)));
1608 aml_append(method, aml_return(aml_local(0)));
1609 aml_append(dev, method);
1610
Xiao Guangrong4dbfc882015-12-17 13:37:13 +00001611 method = aml_method("WRPT", 1, AML_NOTSERIALIZED);
Igor Mammedovcd61cb22015-02-18 19:14:38 +00001612 aml_append(method, aml_store(aml_arg(0), aml_name("PEPT")));
1613 aml_append(dev, method);
1614
1615 aml_append(scope, dev);
1616 aml_append(ssdt, scope);
1617 }
1618
Gal Hammer7824df32015-04-21 11:26:12 +03001619 sb_scope = aml_scope("\\_SB");
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001620 {
Igor Mammedov5ca5efa2015-12-28 18:02:28 +01001621 build_processor_devices(sb_scope, guest_info->apic_id_limit, cpu, pm);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001622
Igor Mammedovf177d402015-12-28 18:02:19 +01001623 build_memory_devices(sb_scope, nr_mem, pm->mem_hp_io_base,
1624 pm->mem_hp_io_len);
Igor Mammedov8698c0c2015-02-18 19:14:46 +00001625
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001626 {
Michael S. Tsirkin8dcf5252014-02-04 17:43:47 +02001627 Object *pci_host;
1628 PCIBus *bus = NULL;
Michael S. Tsirkin8dcf5252014-02-04 17:43:47 +02001629
Marcel Apfelbaumca6c1852015-06-02 14:22:59 +03001630 pci_host = acpi_get_i386_pci_host();
1631 if (pci_host) {
Michael S. Tsirkin8dcf5252014-02-04 17:43:47 +02001632 bus = PCI_HOST_BRIDGE(pci_host)->bus;
1633 }
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001634
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +03001635 if (bus) {
Igor Mammedov62b52c22015-02-20 18:22:18 +00001636 Aml *scope = aml_scope("PCI0");
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +03001637 /* Scan all PCI buses. Generate tables to support hotplug. */
Igor Mammedov62b52c22015-02-20 18:22:18 +00001638 build_append_pci_bus_devices(scope, bus, pm->pcihp_bridge_en);
Igor Mammedov72d97b32015-06-09 05:31:53 +02001639
1640 if (misc->tpm_version != TPM_VERSION_UNSPEC) {
1641 dev = aml_device("ISA.TPM");
1642 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C31")));
1643 aml_append(dev, aml_name_decl("_STA", aml_int(0xF)));
1644 crs = aml_resource_template();
1645 aml_append(crs, aml_memory32_fixed(TPM_TIS_ADDR_BASE,
1646 TPM_TIS_ADDR_SIZE, AML_READ_WRITE));
1647 aml_append(crs, aml_irq_no_flags(TPM_TIS_IRQ));
1648 aml_append(dev, aml_name_decl("_CRS", crs));
1649 aml_append(scope, dev);
1650 }
1651
Igor Mammedov62b52c22015-02-20 18:22:18 +00001652 aml_append(sb_scope, scope);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001653 }
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001654 }
Igor Mammedov011bb742015-02-18 19:14:16 +00001655 aml_append(ssdt, sb_scope);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001656 }
1657
Igor Mammedov011bb742015-02-18 19:14:16 +00001658 /* copy AML table into ACPI tables blob and patch header there */
1659 g_array_append_vals(table_data, ssdt->buf->data, ssdt->buf->len);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001660 build_header(linker, table_data,
Igor Mammedov011bb742015-02-18 19:14:16 +00001661 (void *)(table_data->data + table_data->len - ssdt->buf->len),
Xiao Guangrong8870ca02015-12-02 15:20:57 +08001662 "SSDT", ssdt->buf->len, 1, NULL);
Igor Mammedov011bb742015-02-18 19:14:16 +00001663 free_aml_allocator();
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001664}
1665
1666static void
1667build_hpet(GArray *table_data, GArray *linker)
1668{
1669 Acpi20Hpet *hpet;
1670
1671 hpet = acpi_data_push(table_data, sizeof(*hpet));
1672 /* Note timer_block_id value must be kept in sync with value advertised by
1673 * emulated hpet
1674 */
1675 hpet->timer_block_id = cpu_to_le32(0x8086a201);
1676 hpet->addr.address = cpu_to_le64(HPET_BASE);
1677 build_header(linker, table_data,
Xiao Guangrong8870ca02015-12-02 15:20:57 +08001678 (void *)hpet, "HPET", sizeof(*hpet), 1, NULL);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001679}
1680
Stefan Berger711b20b2014-08-11 16:33:36 -04001681static void
Stefan Berger42a5b302014-10-24 13:21:04 -04001682build_tpm_tcpa(GArray *table_data, GArray *linker, GArray *tcpalog)
Stefan Berger711b20b2014-08-11 16:33:36 -04001683{
1684 Acpi20Tcpa *tcpa = acpi_data_push(table_data, sizeof *tcpa);
Stefan Berger42a5b302014-10-24 13:21:04 -04001685 uint64_t log_area_start_address = acpi_data_len(tcpalog);
Stefan Berger711b20b2014-08-11 16:33:36 -04001686
1687 tcpa->platform_class = cpu_to_le16(TPM_TCPA_ACPI_CLASS_CLIENT);
1688 tcpa->log_area_minimum_length = cpu_to_le32(TPM_LOG_AREA_MINIMUM_SIZE);
1689 tcpa->log_area_start_address = cpu_to_le64(log_area_start_address);
1690
Stefan Berger42a5b302014-10-24 13:21:04 -04001691 bios_linker_loader_alloc(linker, ACPI_BUILD_TPMLOG_FILE, 1,
1692 false /* high memory */);
1693
Stefan Berger711b20b2014-08-11 16:33:36 -04001694 /* log area start address to be filled by Guest linker */
1695 bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
Stefan Berger42a5b302014-10-24 13:21:04 -04001696 ACPI_BUILD_TPMLOG_FILE,
Stefan Berger711b20b2014-08-11 16:33:36 -04001697 table_data, &tcpa->log_area_start_address,
1698 sizeof(tcpa->log_area_start_address));
1699
1700 build_header(linker, table_data,
Xiao Guangrong8870ca02015-12-02 15:20:57 +08001701 (void *)tcpa, "TCPA", sizeof(*tcpa), 2, NULL);
Stefan Berger711b20b2014-08-11 16:33:36 -04001702
Stefan Berger42a5b302014-10-24 13:21:04 -04001703 acpi_data_push(tcpalog, TPM_LOG_AREA_MINIMUM_SIZE);
Stefan Berger711b20b2014-08-11 16:33:36 -04001704}
1705
1706static void
Stefan Berger5cb18b32015-05-26 16:51:07 -04001707build_tpm2(GArray *table_data, GArray *linker)
1708{
1709 Acpi20TPM2 *tpm2_ptr;
Stefan Berger5cb18b32015-05-26 16:51:07 -04001710
1711 tpm2_ptr = acpi_data_push(table_data, sizeof *tpm2_ptr);
1712
1713 tpm2_ptr->platform_class = cpu_to_le16(TPM2_ACPI_CLASS_CLIENT);
1714 tpm2_ptr->control_area_address = cpu_to_le64(0);
1715 tpm2_ptr->start_method = cpu_to_le32(TPM2_START_METHOD_MMIO);
1716
1717 build_header(linker, table_data,
Xiao Guangrong8870ca02015-12-02 15:20:57 +08001718 (void *)tpm2_ptr, "TPM2", sizeof(*tpm2_ptr), 4, NULL);
Stefan Berger5cb18b32015-05-26 16:51:07 -04001719}
1720
Igor Mammedov04ed3ea2014-06-02 15:24:58 +02001721typedef enum {
1722 MEM_AFFINITY_NOFLAGS = 0,
1723 MEM_AFFINITY_ENABLED = (1 << 0),
1724 MEM_AFFINITY_HOTPLUGGABLE = (1 << 1),
1725 MEM_AFFINITY_NON_VOLATILE = (1 << 2),
1726} MemoryAffinityFlags;
1727
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001728static void
Igor Mammedov04ed3ea2014-06-02 15:24:58 +02001729acpi_build_srat_memory(AcpiSratMemoryAffinity *numamem, uint64_t base,
1730 uint64_t len, int node, MemoryAffinityFlags flags)
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001731{
1732 numamem->type = ACPI_SRAT_MEMORY;
1733 numamem->length = sizeof(*numamem);
1734 memset(numamem->proximity, 0, 4);
1735 numamem->proximity[0] = node;
Igor Mammedov04ed3ea2014-06-02 15:24:58 +02001736 numamem->flags = cpu_to_le32(flags);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001737 numamem->base_addr = cpu_to_le64(base);
1738 numamem->range_length = cpu_to_le64(len);
1739}
1740
1741static void
Igor Mammedovdd0247e2014-11-10 16:20:50 +00001742build_srat(GArray *table_data, GArray *linker, PcGuestInfo *guest_info)
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001743{
1744 AcpiSystemResourceAffinityTable *srat;
1745 AcpiSratProcessorAffinity *core;
1746 AcpiSratMemoryAffinity *numamem;
1747
1748 int i;
1749 uint64_t curnode;
1750 int srat_start, numa_start, slots;
1751 uint64_t mem_len, mem_base, next_base;
Igor Mammedovcec65192014-06-02 15:25:28 +02001752 PCMachineState *pcms = PC_MACHINE(qdev_get_machine());
1753 ram_addr_t hotplugabble_address_space_size =
1754 object_property_get_int(OBJECT(pcms), PC_MACHINE_MEMHP_REGION_SIZE,
1755 NULL);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001756
1757 srat_start = table_data->len;
1758
1759 srat = acpi_data_push(table_data, sizeof *srat);
1760 srat->reserved1 = cpu_to_le32(1);
1761 core = (void *)(srat + 1);
1762
1763 for (i = 0; i < guest_info->apic_id_limit; ++i) {
1764 core = acpi_data_push(table_data, sizeof *core);
1765 core->type = ACPI_SRAT_PROCESSOR;
1766 core->length = sizeof(*core);
1767 core->local_apic_id = i;
1768 curnode = guest_info->node_cpu[i];
1769 core->proximity_lo = curnode;
1770 memset(core->proximity_hi, 0, 3);
1771 core->local_sapic_eid = 0;
Igor Mammedovdd0247e2014-11-10 16:20:50 +00001772 core->flags = cpu_to_le32(1);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001773 }
1774
1775
1776 /* the memory map is a bit tricky, it contains at least one hole
1777 * from 640k-1M and possibly another one from 3.5G-4G.
1778 */
1779 next_base = 0;
1780 numa_start = table_data->len;
1781
1782 numamem = acpi_data_push(table_data, sizeof *numamem);
Igor Mammedov04ed3ea2014-06-02 15:24:58 +02001783 acpi_build_srat_memory(numamem, 0, 640*1024, 0, MEM_AFFINITY_ENABLED);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001784 next_base = 1024 * 1024;
1785 for (i = 1; i < guest_info->numa_nodes + 1; ++i) {
1786 mem_base = next_base;
1787 mem_len = guest_info->node_mem[i - 1];
1788 if (i == 1) {
1789 mem_len -= 1024 * 1024;
1790 }
1791 next_base = mem_base + mem_len;
1792
1793 /* Cut out the ACPI_PCI hole */
Eduardo Habkost4c8a9492014-01-09 17:12:43 -02001794 if (mem_base <= guest_info->ram_size_below_4g &&
1795 next_base > guest_info->ram_size_below_4g) {
1796 mem_len -= next_base - guest_info->ram_size_below_4g;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001797 if (mem_len > 0) {
1798 numamem = acpi_data_push(table_data, sizeof *numamem);
Igor Mammedov04ed3ea2014-06-02 15:24:58 +02001799 acpi_build_srat_memory(numamem, mem_base, mem_len, i - 1,
1800 MEM_AFFINITY_ENABLED);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001801 }
1802 mem_base = 1ULL << 32;
Eduardo Habkost4c8a9492014-01-09 17:12:43 -02001803 mem_len = next_base - guest_info->ram_size_below_4g;
1804 next_base += (1ULL << 32) - guest_info->ram_size_below_4g;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001805 }
1806 numamem = acpi_data_push(table_data, sizeof *numamem);
Igor Mammedov04ed3ea2014-06-02 15:24:58 +02001807 acpi_build_srat_memory(numamem, mem_base, mem_len, i - 1,
1808 MEM_AFFINITY_ENABLED);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001809 }
1810 slots = (table_data->len - numa_start) / sizeof *numamem;
1811 for (; slots < guest_info->numa_nodes + 2; slots++) {
1812 numamem = acpi_data_push(table_data, sizeof *numamem);
Igor Mammedov04ed3ea2014-06-02 15:24:58 +02001813 acpi_build_srat_memory(numamem, 0, 0, 0, MEM_AFFINITY_NOFLAGS);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001814 }
1815
Igor Mammedovcec65192014-06-02 15:25:28 +02001816 /*
1817 * Entry is required for Windows to enable memory hotplug in OS.
1818 * Memory devices may override proximity set by this entry,
1819 * providing _PXM method if necessary.
1820 */
1821 if (hotplugabble_address_space_size) {
1822 numamem = acpi_data_push(table_data, sizeof *numamem);
Bharata B Raoa7d69ff2015-06-29 13:50:22 +05301823 acpi_build_srat_memory(numamem, pcms->hotplug_memory.base,
Igor Mammedovcec65192014-06-02 15:25:28 +02001824 hotplugabble_address_space_size, 0,
1825 MEM_AFFINITY_HOTPLUGGABLE |
1826 MEM_AFFINITY_ENABLED);
1827 }
1828
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001829 build_header(linker, table_data,
1830 (void *)(table_data->data + srat_start),
Michael S. Tsirkin821e3222014-03-18 15:49:41 +02001831 "SRAT",
Xiao Guangrong8870ca02015-12-02 15:20:57 +08001832 table_data->len - srat_start, 1, NULL);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001833}
1834
1835static void
1836build_mcfg_q35(GArray *table_data, GArray *linker, AcpiMcfgInfo *info)
1837{
1838 AcpiTableMcfg *mcfg;
Michael S. Tsirkin821e3222014-03-18 15:49:41 +02001839 const char *sig;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001840 int len = sizeof(*mcfg) + 1 * sizeof(mcfg->allocation[0]);
1841
1842 mcfg = acpi_data_push(table_data, len);
1843 mcfg->allocation[0].address = cpu_to_le64(info->mcfg_base);
1844 /* Only a single allocation so no need to play with segments */
1845 mcfg->allocation[0].pci_segment = cpu_to_le16(0);
1846 mcfg->allocation[0].start_bus_number = 0;
1847 mcfg->allocation[0].end_bus_number = PCIE_MMCFG_BUS(info->mcfg_size - 1);
1848
1849 /* MCFG is used for ECAM which can be enabled or disabled by guest.
1850 * To avoid table size changes (which create migration issues),
1851 * always create the table even if there are no allocations,
1852 * but set the signature to a reserved value in this case.
1853 * ACPI spec requires OSPMs to ignore such tables.
1854 */
1855 if (info->mcfg_base == PCIE_BASE_ADDR_UNMAPPED) {
Michael S. Tsirkin821e3222014-03-18 15:49:41 +02001856 /* Reserved signature: ignored by OSPM */
1857 sig = "QEMU";
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001858 } else {
Michael S. Tsirkin821e3222014-03-18 15:49:41 +02001859 sig = "MCFG";
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001860 }
Xiao Guangrong8870ca02015-12-02 15:20:57 +08001861 build_header(linker, table_data, (void *)mcfg, sig, len, 1, NULL);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001862}
1863
1864static void
Le Tand4eb9112014-08-16 13:55:39 +08001865build_dmar_q35(GArray *table_data, GArray *linker)
1866{
1867 int dmar_start = table_data->len;
1868
1869 AcpiTableDmar *dmar;
1870 AcpiDmarHardwareUnit *drhd;
1871
1872 dmar = acpi_data_push(table_data, sizeof(*dmar));
1873 dmar->host_address_width = VTD_HOST_ADDRESS_WIDTH - 1;
1874 dmar->flags = 0; /* No intr_remap for now */
1875
1876 /* DMAR Remapping Hardware Unit Definition structure */
1877 drhd = acpi_data_push(table_data, sizeof(*drhd));
1878 drhd->type = cpu_to_le16(ACPI_DMAR_TYPE_HARDWARE_UNIT);
1879 drhd->length = cpu_to_le16(sizeof(*drhd)); /* No device scope now */
1880 drhd->flags = ACPI_DMAR_INCLUDE_PCI_ALL;
1881 drhd->pci_segment = cpu_to_le16(0);
1882 drhd->address = cpu_to_le64(Q35_HOST_BRIDGE_IOMMU_ADDR);
1883
1884 build_header(linker, table_data, (void *)(table_data->data + dmar_start),
Xiao Guangrong8870ca02015-12-02 15:20:57 +08001885 "DMAR", table_data->len - dmar_start, 1, NULL);
Le Tand4eb9112014-08-16 13:55:39 +08001886}
1887
1888static void
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001889build_dsdt(GArray *table_data, GArray *linker, AcpiMiscInfo *misc)
1890{
Michael S. Tsirkin53db0922013-11-14 13:51:25 +02001891 AcpiTableHeader *dsdt;
1892
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001893 assert(misc->dsdt_code && misc->dsdt_size);
Michael S. Tsirkin53db0922013-11-14 13:51:25 +02001894
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001895 dsdt = acpi_data_push(table_data, misc->dsdt_size);
1896 memcpy(dsdt, misc->dsdt_code, misc->dsdt_size);
Michael S. Tsirkin53db0922013-11-14 13:51:25 +02001897
1898 memset(dsdt, 0, sizeof *dsdt);
Michael S. Tsirkin821e3222014-03-18 15:49:41 +02001899 build_header(linker, table_data, dsdt, "DSDT",
Xiao Guangrong8870ca02015-12-02 15:20:57 +08001900 misc->dsdt_size, 1, NULL);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001901}
1902
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001903static GArray *
1904build_rsdp(GArray *rsdp_table, GArray *linker, unsigned rsdt)
1905{
1906 AcpiRsdpDescriptor *rsdp = acpi_data_push(rsdp_table, sizeof *rsdp);
1907
Michael S. Tsirkind67aadc2014-08-04 16:56:57 +02001908 bios_linker_loader_alloc(linker, ACPI_BUILD_RSDP_FILE, 16,
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001909 true /* fseg memory */);
1910
Michael S. Tsirkin821e3222014-03-18 15:49:41 +02001911 memcpy(&rsdp->signature, "RSD PTR ", 8);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001912 memcpy(rsdp->oem_id, ACPI_BUILD_APPNAME6, 6);
1913 rsdp->rsdt_physical_address = cpu_to_le32(rsdt);
1914 /* Address to be filled by Guest linker */
1915 bios_linker_loader_add_pointer(linker, ACPI_BUILD_RSDP_FILE,
1916 ACPI_BUILD_TABLE_FILE,
1917 rsdp_table, &rsdp->rsdt_physical_address,
1918 sizeof rsdp->rsdt_physical_address);
1919 rsdp->checksum = 0;
1920 /* Checksum to be filled by Guest linker */
1921 bios_linker_loader_add_checksum(linker, ACPI_BUILD_RSDP_FILE,
1922 rsdp, rsdp, sizeof *rsdp, &rsdp->checksum);
1923
1924 return rsdp_table;
1925}
1926
1927typedef
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001928struct AcpiBuildState {
1929 /* Copy of table in RAM (for patching). */
Paolo Bonzini339240b2015-03-23 10:24:16 +01001930 MemoryRegion *table_mr;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001931 /* Is table patched? */
1932 uint8_t patched;
1933 PcGuestInfo *guest_info;
Michael S. Tsirkind70414a2015-02-09 13:59:53 +00001934 void *rsdp;
Paolo Bonzini339240b2015-03-23 10:24:16 +01001935 MemoryRegion *rsdp_mr;
1936 MemoryRegion *linker_mr;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001937} AcpiBuildState;
1938
1939static bool acpi_get_mcfg(AcpiMcfgInfo *mcfg)
1940{
1941 Object *pci_host;
1942 QObject *o;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001943
Marcel Apfelbaumca6c1852015-06-02 14:22:59 +03001944 pci_host = acpi_get_i386_pci_host();
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001945 g_assert(pci_host);
1946
1947 o = object_property_get_qobject(pci_host, PCIE_HOST_MCFG_BASE, NULL);
1948 if (!o) {
1949 return false;
1950 }
1951 mcfg->mcfg_base = qint_get_int(qobject_to_qint(o));
Kirill Batuzov097a97a2014-04-24 18:15:57 +04001952 qobject_decref(o);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001953
1954 o = object_property_get_qobject(pci_host, PCIE_HOST_MCFG_SIZE, NULL);
1955 assert(o);
1956 mcfg->mcfg_size = qint_get_int(qobject_to_qint(o));
Kirill Batuzov097a97a2014-04-24 18:15:57 +04001957 qobject_decref(o);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001958 return true;
1959}
1960
Le Tand4eb9112014-08-16 13:55:39 +08001961static bool acpi_has_iommu(void)
1962{
1963 bool ambiguous;
1964 Object *intel_iommu;
1965
1966 intel_iommu = object_resolve_path_type("", TYPE_INTEL_IOMMU_DEVICE,
1967 &ambiguous);
1968 return intel_iommu && !ambiguous;
1969}
1970
Xiao Guangrong87252e12015-12-02 15:20:58 +08001971static bool acpi_has_nvdimm(void)
1972{
1973 PCMachineState *pcms = PC_MACHINE(qdev_get_machine());
1974
1975 return pcms->nvdimm;
1976}
1977
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001978static
1979void acpi_build(PcGuestInfo *guest_info, AcpiBuildTables *tables)
1980{
1981 GArray *table_offsets;
Paolo Bonzini07fb6172014-07-28 17:34:15 +02001982 unsigned facs, ssdt, dsdt, rsdt;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001983 AcpiCpuInfo cpu;
1984 AcpiPmInfo pm;
1985 AcpiMiscInfo misc;
1986 AcpiMcfgInfo mcfg;
1987 PcPciInfo pci;
1988 uint8_t *u;
Paolo Bonzini07fb6172014-07-28 17:34:15 +02001989 size_t aml_len = 0;
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00001990 GArray *tables_blob = tables->table_data;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001991
1992 acpi_get_cpu_info(&cpu);
1993 acpi_get_pm_info(&pm);
1994 acpi_get_dsdt(&misc);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03001995 acpi_get_misc_info(&misc);
1996 acpi_get_pci_info(&pci);
1997
1998 table_offsets = g_array_new(false, true /* clear */,
1999 sizeof(uint32_t));
Gonglei8b310fc2014-11-13 10:59:37 +08002000 ACPI_BUILD_DPRINTF("init ACPI tables\n");
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002001
2002 bios_linker_loader_alloc(tables->linker, ACPI_BUILD_TABLE_FILE,
2003 64 /* Ensure FACS is aligned */,
2004 false /* high memory */);
2005
2006 /*
2007 * FACS is pointed to by FADT.
2008 * We place it first since it's the only table that has alignment
2009 * requirements.
2010 */
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002011 facs = tables_blob->len;
2012 build_facs(tables_blob, tables->linker, guest_info);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002013
2014 /* DSDT is pointed to by FADT */
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002015 dsdt = tables_blob->len;
2016 build_dsdt(tables_blob, tables->linker, &misc);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002017
Paolo Bonzini07fb6172014-07-28 17:34:15 +02002018 /* Count the size of the DSDT and SSDT, we will need it for legacy
2019 * sizing of ACPI tables.
2020 */
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002021 aml_len += tables_blob->len - dsdt;
Paolo Bonzini07fb6172014-07-28 17:34:15 +02002022
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002023 /* ACPI tables pointed to by RSDT */
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002024 acpi_add_table(table_offsets, tables_blob);
2025 build_fadt(tables_blob, tables->linker, &pm, facs, dsdt);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002026
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002027 ssdt = tables_blob->len;
2028 acpi_add_table(table_offsets, tables_blob);
2029 build_ssdt(tables_blob, tables->linker, &cpu, &pm, &misc, &pci,
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002030 guest_info);
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002031 aml_len += tables_blob->len - ssdt;
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002032
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002033 acpi_add_table(table_offsets, tables_blob);
2034 build_madt(tables_blob, tables->linker, &cpu, guest_info);
Michael S. Tsirkin9ac1c4c2014-04-28 08:15:32 +03002035
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002036 if (misc.has_hpet) {
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002037 acpi_add_table(table_offsets, tables_blob);
2038 build_hpet(tables_blob, tables->linker);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002039 }
Stefan Berger5cb18b32015-05-26 16:51:07 -04002040 if (misc.tpm_version != TPM_VERSION_UNSPEC) {
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002041 acpi_add_table(table_offsets, tables_blob);
2042 build_tpm_tcpa(tables_blob, tables->linker, tables->tcpalog);
Stefan Berger711b20b2014-08-11 16:33:36 -04002043
Igor Mammedov72d97b32015-06-09 05:31:53 +02002044 if (misc.tpm_version == TPM_VERSION_2_0) {
2045 acpi_add_table(table_offsets, tables_blob);
Stefan Berger5cb18b32015-05-26 16:51:07 -04002046 build_tpm2(tables_blob, tables->linker);
Stefan Berger5cb18b32015-05-26 16:51:07 -04002047 }
Stefan Berger711b20b2014-08-11 16:33:36 -04002048 }
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002049 if (guest_info->numa_nodes) {
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002050 acpi_add_table(table_offsets, tables_blob);
2051 build_srat(tables_blob, tables->linker, guest_info);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002052 }
2053 if (acpi_get_mcfg(&mcfg)) {
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002054 acpi_add_table(table_offsets, tables_blob);
2055 build_mcfg_q35(tables_blob, tables->linker, &mcfg);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002056 }
Le Tand4eb9112014-08-16 13:55:39 +08002057 if (acpi_has_iommu()) {
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002058 acpi_add_table(table_offsets, tables_blob);
2059 build_dmar_q35(tables_blob, tables->linker);
Le Tand4eb9112014-08-16 13:55:39 +08002060 }
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002061
Xiao Guangrong87252e12015-12-02 15:20:58 +08002062 if (acpi_has_nvdimm()) {
2063 nvdimm_build_acpi(table_offsets, tables_blob, tables->linker);
2064 }
2065
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002066 /* Add tables supplied by user (if any) */
2067 for (u = acpi_table_first(); u; u = acpi_table_next(u)) {
2068 unsigned len = acpi_table_len(u);
2069
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002070 acpi_add_table(table_offsets, tables_blob);
2071 g_array_append_vals(tables_blob, u, len);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002072 }
2073
2074 /* RSDT is pointed to by RSDP */
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002075 rsdt = tables_blob->len;
2076 build_rsdt(tables_blob, tables->linker, table_offsets);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002077
2078 /* RSDP is in FSEG memory, so allocate it separately */
2079 build_rsdp(tables->rsdp, tables->linker, rsdt);
2080
Paolo Bonzini07fb6172014-07-28 17:34:15 +02002081 /* We'll expose it all to Guest so we want to reduce
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002082 * chance of size changes.
Paolo Bonzini07fb6172014-07-28 17:34:15 +02002083 *
2084 * We used to align the tables to 4k, but of course this would
2085 * too simple to be enough. 4k turned out to be too small an
2086 * alignment very soon, and in fact it is almost impossible to
2087 * keep the table size stable for all (max_cpus, max_memory_slots)
2088 * combinations. So the table size is always 64k for pc-i440fx-2.1
2089 * and we give an error if the table grows beyond that limit.
2090 *
2091 * We still have the problem of migrating from "-M pc-i440fx-2.0". For
2092 * that, we exploit the fact that QEMU 2.1 generates _smaller_ tables
2093 * than 2.0 and we can always pad the smaller tables with zeros. We can
2094 * then use the exact size of the 2.0 tables.
2095 *
2096 * All this is for PIIX4, since QEMU 2.0 didn't support Q35 migration.
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002097 */
Paolo Bonzini07fb6172014-07-28 17:34:15 +02002098 if (guest_info->legacy_acpi_table_size) {
2099 /* Subtracting aml_len gives the size of fixed tables. Then add the
2100 * size of the PIIX4 DSDT/SSDT in QEMU 2.0.
2101 */
2102 int legacy_aml_len =
2103 guest_info->legacy_acpi_table_size +
2104 ACPI_BUILD_LEGACY_CPU_AML_SIZE * max_cpus;
2105 int legacy_table_size =
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002106 ROUND_UP(tables_blob->len - aml_len + legacy_aml_len,
Paolo Bonzini07fb6172014-07-28 17:34:15 +02002107 ACPI_BUILD_ALIGN_SIZE);
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002108 if (tables_blob->len > legacy_table_size) {
Paolo Bonzini07fb6172014-07-28 17:34:15 +02002109 /* Should happen only with PCI bridges and -M pc-i440fx-2.0. */
Michael S. Tsirkin868270f2014-07-28 23:07:11 +02002110 error_report("Warning: migration may not work.");
Paolo Bonzini07fb6172014-07-28 17:34:15 +02002111 }
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002112 g_array_set_size(tables_blob, legacy_table_size);
Paolo Bonzini07fb6172014-07-28 17:34:15 +02002113 } else {
Michael S. Tsirkin868270f2014-07-28 23:07:11 +02002114 /* Make sure we have a buffer in case we need to resize the tables. */
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002115 if (tables_blob->len > ACPI_BUILD_TABLE_SIZE / 2) {
Paolo Bonzini18045fb2014-07-28 17:34:16 +02002116 /* As of QEMU 2.1, this fires with 160 VCPUs and 255 memory slots. */
Michael S. Tsirkin868270f2014-07-28 23:07:11 +02002117 error_report("Warning: ACPI tables are larger than 64k.");
2118 error_report("Warning: migration may not work.");
2119 error_report("Warning: please remove CPUs, NUMA nodes, "
2120 "memory slots or PCI bridges.");
Paolo Bonzini18045fb2014-07-28 17:34:16 +02002121 }
Igor Mammedov7c2c1fa2015-02-09 10:53:24 +00002122 acpi_align_size(tables_blob, ACPI_BUILD_TABLE_SIZE);
Paolo Bonzini07fb6172014-07-28 17:34:15 +02002123 }
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002124
Paolo Bonzini07fb6172014-07-28 17:34:15 +02002125 acpi_align_size(tables->linker, ACPI_BUILD_ALIGN_SIZE);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002126
2127 /* Cleanup memory that's no longer used. */
2128 g_array_free(table_offsets, true);
2129}
2130
Paolo Bonzini339240b2015-03-23 10:24:16 +01002131static void acpi_ram_update(MemoryRegion *mr, GArray *data)
Michael S. Tsirkin42d85902015-02-15 17:12:11 +01002132{
2133 uint32_t size = acpi_data_len(data);
2134
2135 /* Make sure RAM size is correct - in case it got changed e.g. by migration */
Paolo Bonzini339240b2015-03-23 10:24:16 +01002136 memory_region_ram_resize(mr, size, &error_abort);
Michael S. Tsirkin42d85902015-02-15 17:12:11 +01002137
Paolo Bonzini339240b2015-03-23 10:24:16 +01002138 memcpy(memory_region_get_ram_ptr(mr), data->data, size);
2139 memory_region_set_dirty(mr, 0, size);
Michael S. Tsirkin42d85902015-02-15 17:12:11 +01002140}
2141
Gabriel L. Somlo3f8752b2015-11-05 09:32:49 -05002142static void acpi_build_update(void *build_opaque)
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002143{
2144 AcpiBuildState *build_state = build_opaque;
2145 AcpiBuildTables tables;
2146
2147 /* No state to update or already patched? Nothing to do. */
2148 if (!build_state || build_state->patched) {
2149 return;
2150 }
2151 build_state->patched = 1;
2152
2153 acpi_build_tables_init(&tables);
2154
2155 acpi_build(build_state->guest_info, &tables);
2156
Paolo Bonzini339240b2015-03-23 10:24:16 +01002157 acpi_ram_update(build_state->table_mr, tables.table_data);
Michael S. Tsirkina1666142014-11-17 07:51:50 +02002158
Michael S. Tsirkin42d85902015-02-15 17:12:11 +01002159 if (build_state->rsdp) {
2160 memcpy(build_state->rsdp, tables.rsdp->data, acpi_data_len(tables.rsdp));
2161 } else {
Paolo Bonzini339240b2015-03-23 10:24:16 +01002162 acpi_ram_update(build_state->rsdp_mr, tables.rsdp);
Michael S. Tsirkin42d85902015-02-15 17:12:11 +01002163 }
Michael S. Tsirkina1666142014-11-17 07:51:50 +02002164
Paolo Bonzini339240b2015-03-23 10:24:16 +01002165 acpi_ram_update(build_state->linker_mr, tables.linker);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002166 acpi_build_tables_cleanup(&tables, true);
2167}
2168
2169static void acpi_build_reset(void *build_opaque)
2170{
2171 AcpiBuildState *build_state = build_opaque;
2172 build_state->patched = 0;
2173}
2174
Paolo Bonzini339240b2015-03-23 10:24:16 +01002175static MemoryRegion *acpi_add_rom_blob(AcpiBuildState *build_state,
2176 GArray *blob, const char *name,
2177 uint64_t max_size)
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002178{
Michael S. Tsirkina1666142014-11-17 07:51:50 +02002179 return rom_add_blob(name, blob->data, acpi_data_len(blob), max_size, -1,
2180 name, acpi_build_update, build_state);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002181}
2182
2183static const VMStateDescription vmstate_acpi_build = {
2184 .name = "acpi_build",
2185 .version_id = 1,
2186 .minimum_version_id = 1,
Juan Quintelad49805a2014-04-16 15:32:32 +02002187 .fields = (VMStateField[]) {
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002188 VMSTATE_UINT8(patched, AcpiBuildState),
2189 VMSTATE_END_OF_LIST()
2190 },
2191};
2192
2193void acpi_setup(PcGuestInfo *guest_info)
2194{
2195 AcpiBuildTables tables;
2196 AcpiBuildState *build_state;
2197
2198 if (!guest_info->fw_cfg) {
Gonglei8b310fc2014-11-13 10:59:37 +08002199 ACPI_BUILD_DPRINTF("No fw cfg. Bailing out.\n");
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002200 return;
2201 }
2202
2203 if (!guest_info->has_acpi_build) {
Gonglei8b310fc2014-11-13 10:59:37 +08002204 ACPI_BUILD_DPRINTF("ACPI build disabled. Bailing out.\n");
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002205 return;
2206 }
2207
Michael S. Tsirkin81adc512013-11-07 14:12:05 +02002208 if (!acpi_enabled) {
Gonglei8b310fc2014-11-13 10:59:37 +08002209 ACPI_BUILD_DPRINTF("ACPI disabled. Bailing out.\n");
Michael S. Tsirkin81adc512013-11-07 14:12:05 +02002210 return;
2211 }
2212
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002213 build_state = g_malloc0(sizeof *build_state);
2214
2215 build_state->guest_info = guest_info;
2216
Michael S. Tsirkin99fd4372013-10-14 18:01:29 +03002217 acpi_set_pci_info();
2218
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002219 acpi_build_tables_init(&tables);
2220 acpi_build(build_state->guest_info, &tables);
2221
2222 /* Now expose it all to Guest */
Paolo Bonzini339240b2015-03-23 10:24:16 +01002223 build_state->table_mr = acpi_add_rom_blob(build_state, tables.table_data,
Michael S. Tsirkina1666142014-11-17 07:51:50 +02002224 ACPI_BUILD_TABLE_FILE,
2225 ACPI_BUILD_TABLE_MAX_SIZE);
Paolo Bonzini339240b2015-03-23 10:24:16 +01002226 assert(build_state->table_mr != NULL);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002227
Paolo Bonzini339240b2015-03-23 10:24:16 +01002228 build_state->linker_mr =
Igor Mammedov6e006192015-02-09 13:59:54 +00002229 acpi_add_rom_blob(build_state, tables.linker, "etc/table-loader", 0);
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002230
Stefan Berger42a5b302014-10-24 13:21:04 -04002231 fw_cfg_add_file(guest_info->fw_cfg, ACPI_BUILD_TPMLOG_FILE,
2232 tables.tcpalog->data, acpi_data_len(tables.tcpalog));
2233
Michael S. Tsirkin384fb322015-02-17 10:04:40 +01002234 if (!guest_info->rsdp_in_ram) {
Igor Mammedov358774d2015-02-09 13:59:55 +00002235 /*
2236 * Keep for compatibility with old machine types.
2237 * Though RSDP is small, its contents isn't immutable, so
Michael S. Tsirkinafaa2e42015-02-17 10:40:30 +01002238 * we'll update it along with the rest of tables on guest access.
Igor Mammedov358774d2015-02-09 13:59:55 +00002239 */
Michael S. Tsirkinafaa2e42015-02-17 10:40:30 +01002240 uint32_t rsdp_size = acpi_data_len(tables.rsdp);
2241
2242 build_state->rsdp = g_memdup(tables.rsdp->data, rsdp_size);
Igor Mammedov358774d2015-02-09 13:59:55 +00002243 fw_cfg_add_file_callback(guest_info->fw_cfg, ACPI_BUILD_RSDP_FILE,
2244 acpi_build_update, build_state,
Michael S. Tsirkinafaa2e42015-02-17 10:40:30 +01002245 build_state->rsdp, rsdp_size);
Paolo Bonzini339240b2015-03-23 10:24:16 +01002246 build_state->rsdp_mr = NULL;
Igor Mammedov358774d2015-02-09 13:59:55 +00002247 } else {
Michael S. Tsirkin42d85902015-02-15 17:12:11 +01002248 build_state->rsdp = NULL;
Paolo Bonzini339240b2015-03-23 10:24:16 +01002249 build_state->rsdp_mr = acpi_add_rom_blob(build_state, tables.rsdp,
Michael S. Tsirkin42d85902015-02-15 17:12:11 +01002250 ACPI_BUILD_RSDP_FILE, 0);
Igor Mammedov358774d2015-02-09 13:59:55 +00002251 }
Michael S. Tsirkin72c194f2013-07-24 18:56:14 +03002252
2253 qemu_register_reset(acpi_build_reset, build_state);
2254 acpi_build_reset(build_state);
2255 vmstate_register(NULL, 0, &vmstate_acpi_build, build_state);
2256
2257 /* Cleanup tables but don't free the memory: we track it
2258 * in build_state.
2259 */
2260 acpi_build_tables_cleanup(&tables, false);
2261}