1 /* Support for generating ACPI tables and passing them to Guests 2 * 3 * Copyright (C) 2008-2010 Kevin O'Connor <kevin@koconnor.net> 4 * Copyright (C) 2006 Fabrice Bellard 5 * Copyright (C) 2013 Red Hat Inc 6 * 7 * Author: Michael S. Tsirkin <mst@redhat.com> 8 * 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License as published by 11 * the Free Software Foundation; either version 2 of the License, or 12 * (at your option) any later version. 13 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 19 * You should have received a copy of the GNU General Public License along 20 * with this program; if not, see <http://www.gnu.org/licenses/>. 21 */ 22 23 #include "qemu/osdep.h" 24 #include "qapi/error.h" 25 #include "qapi/qmp/qnum.h" 26 #include "acpi-build.h" 27 #include "acpi-common.h" 28 #include "qemu/bitmap.h" 29 #include "qemu/error-report.h" 30 #include "hw/pci/pci_bridge.h" 31 #include "hw/cxl/cxl.h" 32 #include "hw/core/cpu.h" 33 #include "target/i386/cpu.h" 34 #include "hw/timer/hpet.h" 35 #include "hw/acpi/acpi-defs.h" 36 #include "hw/acpi/acpi.h" 37 #include "hw/acpi/cpu.h" 38 #include "hw/nvram/fw_cfg.h" 39 #include "hw/acpi/bios-linker-loader.h" 40 #include "hw/acpi/acpi_aml_interface.h" 41 #include "hw/input/i8042.h" 42 #include "hw/acpi/memory_hotplug.h" 43 #include "sysemu/tpm.h" 44 #include "hw/acpi/tpm.h" 45 #include "hw/acpi/vmgenid.h" 46 #include "hw/acpi/erst.h" 47 #include "hw/acpi/piix4.h" 48 #include "sysemu/tpm_backend.h" 49 #include "hw/rtc/mc146818rtc_regs.h" 50 #include "migration/vmstate.h" 51 #include "hw/mem/memory-device.h" 52 #include "hw/mem/nvdimm.h" 53 #include "sysemu/numa.h" 54 #include "sysemu/reset.h" 55 #include "hw/hyperv/vmbus-bridge.h" 56 57 /* Supported chipsets: */ 58 #include "hw/southbridge/ich9.h" 59 #include "hw/southbridge/piix.h" 60 #include "hw/acpi/pcihp.h" 61 #include "hw/i386/fw_cfg.h" 62 #include "hw/i386/pc.h" 63 #include "hw/pci/pci_bus.h" 64 #include "hw/pci-host/i440fx.h" 65 #include "hw/pci-host/q35.h" 66 #include "hw/i386/x86-iommu.h" 67 68 #include "hw/acpi/aml-build.h" 69 #include "hw/acpi/utils.h" 70 #include "hw/acpi/pci.h" 71 #include "hw/acpi/cxl.h" 72 73 #include "qom/qom-qobject.h" 74 #include "hw/i386/amd_iommu.h" 75 #include "hw/i386/intel_iommu.h" 76 #include "hw/virtio/virtio-iommu.h" 77 78 #include "hw/acpi/hmat.h" 79 #include "hw/acpi/viot.h" 80 81 #include CONFIG_DEVICES 82 83 /* These are used to size the ACPI tables for -M pc-i440fx-1.7 and 84 * -M pc-i440fx-2.0. Even if the actual amount of AML generated grows 85 * a little bit, there should be plenty of free space since the DSDT 86 * shrunk by ~1.5k between QEMU 2.0 and QEMU 2.1. 87 */ 88 #define ACPI_BUILD_LEGACY_CPU_AML_SIZE 97 89 #define ACPI_BUILD_ALIGN_SIZE 0x1000 90 91 #define ACPI_BUILD_TABLE_SIZE 0x20000 92 93 /* #define DEBUG_ACPI_BUILD */ 94 #ifdef DEBUG_ACPI_BUILD 95 #define ACPI_BUILD_DPRINTF(fmt, ...) \ 96 do {printf("ACPI_BUILD: " fmt, ## __VA_ARGS__); } while (0) 97 #else 98 #define ACPI_BUILD_DPRINTF(fmt, ...) 99 #endif 100 101 typedef struct AcpiPmInfo { 102 bool s3_disabled; 103 bool s4_disabled; 104 bool pcihp_bridge_en; 105 bool smi_on_cpuhp; 106 bool smi_on_cpu_unplug; 107 bool pcihp_root_en; 108 uint8_t s4_val; 109 AcpiFadtData fadt; 110 uint16_t cpu_hp_io_base; 111 uint16_t pcihp_io_base; 112 uint16_t pcihp_io_len; 113 } AcpiPmInfo; 114 115 typedef struct AcpiMiscInfo { 116 bool has_hpet; 117 #ifdef CONFIG_TPM 118 TPMVersion tpm_version; 119 #endif 120 } AcpiMiscInfo; 121 122 typedef struct FwCfgTPMConfig { 123 uint32_t tpmppi_address; 124 uint8_t tpm_version; 125 uint8_t tpmppi_version; 126 } QEMU_PACKED FwCfgTPMConfig; 127 128 static bool acpi_get_mcfg(AcpiMcfgInfo *mcfg); 129 130 const struct AcpiGenericAddress x86_nvdimm_acpi_dsmio = { 131 .space_id = AML_AS_SYSTEM_IO, 132 .address = NVDIMM_ACPI_IO_BASE, 133 .bit_width = NVDIMM_ACPI_IO_LEN << 3 134 }; 135 136 static void init_common_fadt_data(MachineState *ms, Object *o, 137 AcpiFadtData *data) 138 { 139 X86MachineState *x86ms = X86_MACHINE(ms); 140 /* 141 * "ICH9-LPC" or "PIIX4_PM" has "smm-compat" property to keep the old 142 * behavior for compatibility irrelevant to smm_enabled, which doesn't 143 * comforms to ACPI spec. 144 */ 145 bool smm_enabled = object_property_get_bool(o, "smm-compat", NULL) ? 146 true : x86_machine_is_smm_enabled(x86ms); 147 uint32_t io = object_property_get_uint(o, ACPI_PM_PROP_PM_IO_BASE, NULL); 148 AmlAddressSpace as = AML_AS_SYSTEM_IO; 149 AcpiFadtData fadt = { 150 .rev = 3, 151 .flags = 152 (1 << ACPI_FADT_F_WBINVD) | 153 (1 << ACPI_FADT_F_PROC_C1) | 154 (1 << ACPI_FADT_F_SLP_BUTTON) | 155 (1 << ACPI_FADT_F_RTC_S4) | 156 (1 << ACPI_FADT_F_USE_PLATFORM_CLOCK) | 157 /* APIC destination mode ("Flat Logical") has an upper limit of 8 158 * CPUs for more than 8 CPUs, "Clustered Logical" mode has to be 159 * used 160 */ 161 ((ms->smp.max_cpus > 8) ? 162 (1 << ACPI_FADT_F_FORCE_APIC_CLUSTER_MODEL) : 0), 163 .int_model = 1 /* Multiple APIC */, 164 .rtc_century = RTC_CENTURY, 165 .plvl2_lat = 0xfff /* C2 state not supported */, 166 .plvl3_lat = 0xfff /* C3 state not supported */, 167 .smi_cmd = smm_enabled ? ACPI_PORT_SMI_CMD : 0, 168 .sci_int = object_property_get_uint(o, ACPI_PM_PROP_SCI_INT, NULL), 169 .acpi_enable_cmd = 170 smm_enabled ? 171 object_property_get_uint(o, ACPI_PM_PROP_ACPI_ENABLE_CMD, NULL) : 172 0, 173 .acpi_disable_cmd = 174 smm_enabled ? 175 object_property_get_uint(o, ACPI_PM_PROP_ACPI_DISABLE_CMD, NULL) : 176 0, 177 .pm1a_evt = { .space_id = as, .bit_width = 4 * 8, .address = io }, 178 .pm1a_cnt = { .space_id = as, .bit_width = 2 * 8, 179 .address = io + 0x04 }, 180 .pm_tmr = { .space_id = as, .bit_width = 4 * 8, .address = io + 0x08 }, 181 .gpe0_blk = { .space_id = as, .bit_width = 182 object_property_get_uint(o, ACPI_PM_PROP_GPE0_BLK_LEN, NULL) * 8, 183 .address = object_property_get_uint(o, ACPI_PM_PROP_GPE0_BLK, NULL) 184 }, 185 }; 186 187 /* 188 * ACPI v2, Table 5-10 - Fixed ACPI Description Table Boot Architecture 189 * Flags, bit offset 1 - 8042. 190 */ 191 fadt.iapc_boot_arch = iapc_boot_arch_8042(); 192 193 *data = fadt; 194 } 195 196 static Object *object_resolve_type_unambiguous(const char *typename) 197 { 198 bool ambig; 199 Object *o = object_resolve_path_type("", typename, &ambig); 200 201 if (ambig || !o) { 202 return NULL; 203 } 204 return o; 205 } 206 207 static void acpi_get_pm_info(MachineState *machine, AcpiPmInfo *pm) 208 { 209 Object *piix = object_resolve_type_unambiguous(TYPE_PIIX4_PM); 210 Object *lpc = object_resolve_type_unambiguous(TYPE_ICH9_LPC_DEVICE); 211 Object *obj = piix ? piix : lpc; 212 QObject *o; 213 pm->cpu_hp_io_base = 0; 214 pm->pcihp_io_base = 0; 215 pm->pcihp_io_len = 0; 216 pm->smi_on_cpuhp = false; 217 pm->smi_on_cpu_unplug = false; 218 219 assert(obj); 220 init_common_fadt_data(machine, obj, &pm->fadt); 221 if (piix) { 222 /* w2k requires FADT(rev1) or it won't boot, keep PC compatible */ 223 pm->fadt.rev = 1; 224 pm->cpu_hp_io_base = PIIX4_CPU_HOTPLUG_IO_BASE; 225 } 226 if (lpc) { 227 uint64_t smi_features = object_property_get_uint(lpc, 228 ICH9_LPC_SMI_NEGOTIATED_FEAT_PROP, NULL); 229 struct AcpiGenericAddress r = { .space_id = AML_AS_SYSTEM_IO, 230 .bit_width = 8, .address = ICH9_RST_CNT_IOPORT }; 231 pm->fadt.reset_reg = r; 232 pm->fadt.reset_val = 0xf; 233 pm->fadt.flags |= 1 << ACPI_FADT_F_RESET_REG_SUP; 234 pm->cpu_hp_io_base = ICH9_CPU_HOTPLUG_IO_BASE; 235 pm->smi_on_cpuhp = 236 !!(smi_features & BIT_ULL(ICH9_LPC_SMI_F_CPU_HOTPLUG_BIT)); 237 pm->smi_on_cpu_unplug = 238 !!(smi_features & BIT_ULL(ICH9_LPC_SMI_F_CPU_HOT_UNPLUG_BIT)); 239 } 240 pm->pcihp_io_base = 241 object_property_get_uint(obj, ACPI_PCIHP_IO_BASE_PROP, NULL); 242 pm->pcihp_io_len = 243 object_property_get_uint(obj, ACPI_PCIHP_IO_LEN_PROP, NULL); 244 245 /* The above need not be conditional on machine type because the reset port 246 * happens to be the same on PIIX (pc) and ICH9 (q35). */ 247 QEMU_BUILD_BUG_ON(ICH9_RST_CNT_IOPORT != PIIX_RCR_IOPORT); 248 249 /* Fill in optional s3/s4 related properties */ 250 o = object_property_get_qobject(obj, ACPI_PM_PROP_S3_DISABLED, NULL); 251 if (o) { 252 pm->s3_disabled = qnum_get_uint(qobject_to(QNum, o)); 253 } else { 254 pm->s3_disabled = false; 255 } 256 qobject_unref(o); 257 o = object_property_get_qobject(obj, ACPI_PM_PROP_S4_DISABLED, NULL); 258 if (o) { 259 pm->s4_disabled = qnum_get_uint(qobject_to(QNum, o)); 260 } else { 261 pm->s4_disabled = false; 262 } 263 qobject_unref(o); 264 o = object_property_get_qobject(obj, ACPI_PM_PROP_S4_VAL, NULL); 265 if (o) { 266 pm->s4_val = qnum_get_uint(qobject_to(QNum, o)); 267 } else { 268 pm->s4_val = false; 269 } 270 qobject_unref(o); 271 272 pm->pcihp_bridge_en = 273 object_property_get_bool(obj, ACPI_PM_PROP_ACPI_PCIHP_BRIDGE, 274 NULL); 275 pm->pcihp_root_en = 276 object_property_get_bool(obj, ACPI_PM_PROP_ACPI_PCI_ROOTHP, 277 NULL); 278 } 279 280 static void acpi_get_misc_info(AcpiMiscInfo *info) 281 { 282 info->has_hpet = hpet_find(); 283 #ifdef CONFIG_TPM 284 info->tpm_version = tpm_get_version(tpm_find()); 285 #endif 286 } 287 288 /* 289 * Because of the PXB hosts we cannot simply query TYPE_PCI_HOST_BRIDGE. 290 * On i386 arch we only have two pci hosts, so we can look only for them. 291 */ 292 Object *acpi_get_i386_pci_host(void) 293 { 294 PCIHostState *host; 295 296 host = PCI_HOST_BRIDGE(object_resolve_path("/machine/i440fx", NULL)); 297 if (!host) { 298 host = PCI_HOST_BRIDGE(object_resolve_path("/machine/q35", NULL)); 299 } 300 301 return OBJECT(host); 302 } 303 304 static void acpi_get_pci_holes(Range *hole, Range *hole64) 305 { 306 Object *pci_host; 307 308 pci_host = acpi_get_i386_pci_host(); 309 310 if (!pci_host) { 311 return; 312 } 313 314 range_set_bounds1(hole, 315 object_property_get_uint(pci_host, 316 PCI_HOST_PROP_PCI_HOLE_START, 317 NULL), 318 object_property_get_uint(pci_host, 319 PCI_HOST_PROP_PCI_HOLE_END, 320 NULL)); 321 range_set_bounds1(hole64, 322 object_property_get_uint(pci_host, 323 PCI_HOST_PROP_PCI_HOLE64_START, 324 NULL), 325 object_property_get_uint(pci_host, 326 PCI_HOST_PROP_PCI_HOLE64_END, 327 NULL)); 328 } 329 330 static void acpi_align_size(GArray *blob, unsigned align) 331 { 332 /* Align size to multiple of given size. This reduces the chance 333 * we need to change size in the future (breaking cross version migration). 334 */ 335 g_array_set_size(blob, ROUND_UP(acpi_data_len(blob), align)); 336 } 337 338 /* 339 * ACPI spec 1.0b, 340 * 5.2.6 Firmware ACPI Control Structure 341 */ 342 static void 343 build_facs(GArray *table_data) 344 { 345 const char *sig = "FACS"; 346 const uint8_t reserved[40] = {}; 347 348 g_array_append_vals(table_data, sig, 4); /* Signature */ 349 build_append_int_noprefix(table_data, 64, 4); /* Length */ 350 build_append_int_noprefix(table_data, 0, 4); /* Hardware Signature */ 351 build_append_int_noprefix(table_data, 0, 4); /* Firmware Waking Vector */ 352 build_append_int_noprefix(table_data, 0, 4); /* Global Lock */ 353 build_append_int_noprefix(table_data, 0, 4); /* Flags */ 354 g_array_append_vals(table_data, reserved, 40); /* Reserved */ 355 } 356 357 Aml *aml_pci_device_dsm(void) 358 { 359 Aml *method; 360 361 method = aml_method("_DSM", 4, AML_SERIALIZED); 362 { 363 Aml *params = aml_local(0); 364 Aml *pkg = aml_package(2); 365 aml_append(pkg, aml_name("BSEL")); 366 aml_append(pkg, aml_name("ASUN")); 367 aml_append(method, aml_store(pkg, params)); 368 aml_append(method, 369 aml_return(aml_call5("PDSM", aml_arg(0), aml_arg(1), 370 aml_arg(2), aml_arg(3), params)) 371 ); 372 } 373 return method; 374 } 375 376 static void build_append_pci_dsm_func0_common(Aml *ctx, Aml *retvar) 377 { 378 Aml *UUID, *ifctx1; 379 uint8_t byte_list[1] = { 0 }; /* nothing supported yet */ 380 381 aml_append(ctx, aml_store(aml_buffer(1, byte_list), retvar)); 382 /* 383 * PCI Firmware Specification 3.1 384 * 4.6. _DSM Definitions for PCI 385 */ 386 UUID = aml_touuid("E5C937D0-3553-4D7A-9117-EA4D19C3434D"); 387 ifctx1 = aml_if(aml_lnot(aml_equal(aml_arg(0), UUID))); 388 { 389 /* call is for unsupported UUID, bail out */ 390 aml_append(ifctx1, aml_return(retvar)); 391 } 392 aml_append(ctx, ifctx1); 393 394 ifctx1 = aml_if(aml_lless(aml_arg(1), aml_int(2))); 395 { 396 /* call is for unsupported REV, bail out */ 397 aml_append(ifctx1, aml_return(retvar)); 398 } 399 aml_append(ctx, ifctx1); 400 } 401 402 static Aml *aml_pci_edsm(void) 403 { 404 Aml *method, *ifctx; 405 Aml *zero = aml_int(0); 406 Aml *func = aml_arg(2); 407 Aml *ret = aml_local(0); 408 Aml *aidx = aml_local(1); 409 Aml *params = aml_arg(4); 410 411 method = aml_method("EDSM", 5, AML_SERIALIZED); 412 413 /* get supported functions */ 414 ifctx = aml_if(aml_equal(func, zero)); 415 { 416 /* 1: have supported functions */ 417 /* 7: support for function 7 */ 418 const uint8_t caps = 1 | BIT(7); 419 build_append_pci_dsm_func0_common(ifctx, ret); 420 aml_append(ifctx, aml_store(aml_int(caps), aml_index(ret, zero))); 421 aml_append(ifctx, aml_return(ret)); 422 } 423 aml_append(method, ifctx); 424 425 /* handle specific functions requests */ 426 /* 427 * PCI Firmware Specification 3.1 428 * 4.6.7. _DSM for Naming a PCI or PCI Express Device Under 429 * Operating Systems 430 */ 431 ifctx = aml_if(aml_equal(func, aml_int(7))); 432 { 433 Aml *pkg = aml_package(2); 434 aml_append(pkg, zero); 435 /* optional, if not impl. should return null string */ 436 aml_append(pkg, aml_string("%s", "")); 437 aml_append(ifctx, aml_store(pkg, ret)); 438 439 /* 440 * IASL is fine when initializing Package with computational data, 441 * however it makes guest unhappy /it fails to process such AML/. 442 * So use runtime assignment to set acpi-index after initializer 443 * to make OSPM happy. 444 */ 445 aml_append(ifctx, 446 aml_store(aml_derefof(aml_index(params, aml_int(0))), aidx)); 447 aml_append(ifctx, aml_store(aidx, aml_index(ret, zero))); 448 aml_append(ifctx, aml_return(ret)); 449 } 450 aml_append(method, ifctx); 451 452 return method; 453 } 454 455 static void build_append_pcihp_notify_entry(Aml *method, int slot) 456 { 457 Aml *if_ctx; 458 int32_t devfn = PCI_DEVFN(slot, 0); 459 460 if_ctx = aml_if(aml_and(aml_arg(0), aml_int(0x1U << slot), NULL)); 461 aml_append(if_ctx, aml_notify(aml_name("S%.02X", devfn), aml_arg(1))); 462 aml_append(method, if_ctx); 463 } 464 465 static bool is_devfn_ignored_generic(const int devfn, const PCIBus *bus) 466 { 467 const PCIDevice *pdev = bus->devices[devfn]; 468 469 if (PCI_FUNC(devfn)) { 470 if (IS_PCI_BRIDGE(pdev)) { 471 /* 472 * Ignore only hotplugged PCI bridges on !0 functions, but 473 * allow describing cold plugged bridges on all functions 474 */ 475 if (DEVICE(pdev)->hotplugged) { 476 return true; 477 } 478 } else if (!get_dev_aml_func(DEVICE(pdev))) { 479 /* 480 * Ignore all other devices on !0 functions unless they 481 * have AML description (i.e have get_dev_aml_func() != 0) 482 */ 483 return true; 484 } 485 } 486 return false; 487 } 488 489 static bool is_devfn_ignored_hotplug(const int devfn, const PCIBus *bus) 490 { 491 PCIDevice *pdev = bus->devices[devfn]; 492 if (pdev) { 493 return is_devfn_ignored_generic(devfn, bus) || 494 !DEVICE_GET_CLASS(pdev)->hotpluggable || 495 /* Cold plugged bridges aren't themselves hot-pluggable */ 496 (IS_PCI_BRIDGE(pdev) && !DEVICE(pdev)->hotplugged); 497 } else { /* non populated slots */ 498 /* 499 * hotplug is supported only for non-multifunction device 500 * so generate device description only for function 0 501 */ 502 if (PCI_FUNC(devfn) || 503 (pci_bus_is_express(bus) && PCI_SLOT(devfn) > 0)) { 504 return true; 505 } 506 } 507 return false; 508 } 509 510 static void build_append_pcihp_slots(Aml *parent_scope, PCIBus *bus, 511 QObject *bsel) 512 { 513 int devfn; 514 Aml *dev, *notify_method = NULL, *method; 515 uint64_t bsel_val = qnum_get_uint(qobject_to(QNum, bsel)); 516 517 aml_append(parent_scope, aml_name_decl("BSEL", aml_int(bsel_val))); 518 notify_method = aml_method("DVNT", 2, AML_NOTSERIALIZED); 519 520 for (devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) { 521 int slot = PCI_SLOT(devfn); 522 int adr = slot << 16 | PCI_FUNC(devfn); 523 524 if (is_devfn_ignored_hotplug(devfn, bus)) { 525 continue; 526 } 527 528 if (bus->devices[devfn]) { 529 dev = aml_scope("S%.02X", devfn); 530 } else { 531 dev = aml_device("S%.02X", devfn); 532 aml_append(dev, aml_name_decl("_ADR", aml_int(adr))); 533 } 534 535 /* 536 * Can't declare _SUN here for every device as it changes 'slot' 537 * enumeration order in linux kernel, so use another variable for it 538 */ 539 aml_append(dev, aml_name_decl("ASUN", aml_int(slot))); 540 aml_append(dev, aml_pci_device_dsm()); 541 542 aml_append(dev, aml_name_decl("_SUN", aml_int(slot))); 543 /* add _EJ0 to make slot hotpluggable */ 544 method = aml_method("_EJ0", 1, AML_NOTSERIALIZED); 545 aml_append(method, 546 aml_call2("PCEJ", aml_name("BSEL"), aml_name("_SUN")) 547 ); 548 aml_append(dev, method); 549 550 build_append_pcihp_notify_entry(notify_method, slot); 551 552 /* device descriptor has been composed, add it into parent context */ 553 aml_append(parent_scope, dev); 554 } 555 aml_append(parent_scope, notify_method); 556 } 557 558 void build_append_pci_bus_devices(Aml *parent_scope, PCIBus *bus) 559 { 560 QObject *bsel; 561 int devfn; 562 Aml *dev; 563 564 bsel = object_property_get_qobject(OBJECT(bus), ACPI_PCIHP_PROP_BSEL, NULL); 565 566 for (devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) { 567 /* ACPI spec: 1.0b: Table 6-2 _ADR Object Bus Types, PCI type */ 568 int adr = PCI_SLOT(devfn) << 16 | PCI_FUNC(devfn); 569 PCIDevice *pdev = bus->devices[devfn]; 570 571 if (!pdev || is_devfn_ignored_generic(devfn, bus)) { 572 continue; 573 } 574 575 /* start to compose PCI device descriptor */ 576 dev = aml_device("S%.02X", devfn); 577 aml_append(dev, aml_name_decl("_ADR", aml_int(adr))); 578 579 call_dev_aml_func(DEVICE(bus->devices[devfn]), dev); 580 581 /* device descriptor has been composed, add it into parent context */ 582 aml_append(parent_scope, dev); 583 } 584 585 if (bsel) { 586 build_append_pcihp_slots(parent_scope, bus, bsel); 587 } 588 589 qobject_unref(bsel); 590 } 591 592 static bool build_append_notfication_callback(Aml *parent_scope, 593 const PCIBus *bus) 594 { 595 Aml *method; 596 PCIBus *sec; 597 QObject *bsel; 598 int nr_notifiers = 0; 599 GQueue *pcnt_bus_list = g_queue_new(); 600 601 QLIST_FOREACH(sec, &bus->child, sibling) { 602 Aml *br_scope = aml_scope("S%.02X", sec->parent_dev->devfn); 603 if (pci_bus_is_root(sec)) { 604 continue; 605 } 606 nr_notifiers = nr_notifiers + 607 build_append_notfication_callback(br_scope, sec); 608 /* 609 * add new child scope to parent 610 * and keep track of bus that have PCNT, 611 * bus list is used later to call children PCNTs from this level PCNT 612 */ 613 if (nr_notifiers) { 614 g_queue_push_tail(pcnt_bus_list, sec); 615 aml_append(parent_scope, br_scope); 616 } 617 } 618 619 /* 620 * Append PCNT method to notify about events on local and child buses. 621 * ps: hostbridge might not have hotplug (bsel) enabled but might have 622 * child bridges that do have bsel. 623 */ 624 method = aml_method("PCNT", 0, AML_NOTSERIALIZED); 625 626 /* If bus supports hotplug select it and notify about local events */ 627 bsel = object_property_get_qobject(OBJECT(bus), ACPI_PCIHP_PROP_BSEL, NULL); 628 if (bsel) { 629 uint64_t bsel_val = qnum_get_uint(qobject_to(QNum, bsel)); 630 631 aml_append(method, aml_store(aml_int(bsel_val), aml_name("BNUM"))); 632 aml_append(method, aml_call2("DVNT", aml_name("PCIU"), 633 aml_int(1))); /* Device Check */ 634 aml_append(method, aml_call2("DVNT", aml_name("PCID"), 635 aml_int(3))); /* Eject Request */ 636 nr_notifiers++; 637 } 638 639 /* Notify about child bus events in any case */ 640 while ((sec = g_queue_pop_head(pcnt_bus_list))) { 641 aml_append(method, aml_name("^S%.02X.PCNT", sec->parent_dev->devfn)); 642 } 643 644 aml_append(parent_scope, method); 645 qobject_unref(bsel); 646 g_queue_free(pcnt_bus_list); 647 return !!nr_notifiers; 648 } 649 650 static Aml *aml_pci_pdsm(void) 651 { 652 Aml *method, *ifctx, *ifctx1; 653 Aml *ret = aml_local(0); 654 Aml *caps = aml_local(1); 655 Aml *acpi_index = aml_local(2); 656 Aml *zero = aml_int(0); 657 Aml *one = aml_int(1); 658 Aml *func = aml_arg(2); 659 Aml *params = aml_arg(4); 660 Aml *bnum = aml_derefof(aml_index(params, aml_int(0))); 661 Aml *sunum = aml_derefof(aml_index(params, aml_int(1))); 662 663 method = aml_method("PDSM", 5, AML_SERIALIZED); 664 665 /* get supported functions */ 666 ifctx = aml_if(aml_equal(func, zero)); 667 { 668 build_append_pci_dsm_func0_common(ifctx, ret); 669 670 aml_append(ifctx, aml_store(zero, caps)); 671 aml_append(ifctx, 672 aml_store(aml_call2("AIDX", bnum, sunum), acpi_index)); 673 /* 674 * advertise function 7 if device has acpi-index 675 * acpi_index values: 676 * 0: not present (default value) 677 * FFFFFFFF: not supported (old QEMU without PIDX reg) 678 * other: device's acpi-index 679 */ 680 ifctx1 = aml_if(aml_lnot( 681 aml_or(aml_equal(acpi_index, zero), 682 aml_equal(acpi_index, aml_int(0xFFFFFFFF)), NULL) 683 )); 684 { 685 /* have supported functions */ 686 aml_append(ifctx1, aml_or(caps, one, caps)); 687 /* support for function 7 */ 688 aml_append(ifctx1, 689 aml_or(caps, aml_shiftleft(one, aml_int(7)), caps)); 690 } 691 aml_append(ifctx, ifctx1); 692 693 aml_append(ifctx, aml_store(caps, aml_index(ret, zero))); 694 aml_append(ifctx, aml_return(ret)); 695 } 696 aml_append(method, ifctx); 697 698 /* handle specific functions requests */ 699 /* 700 * PCI Firmware Specification 3.1 701 * 4.6.7. _DSM for Naming a PCI or PCI Express Device Under 702 * Operating Systems 703 */ 704 ifctx = aml_if(aml_equal(func, aml_int(7))); 705 { 706 Aml *pkg = aml_package(2); 707 708 aml_append(pkg, zero); 709 /* 710 * optional, if not impl. should return null string 711 */ 712 aml_append(pkg, aml_string("%s", "")); 713 aml_append(ifctx, aml_store(pkg, ret)); 714 715 aml_append(ifctx, aml_store(aml_call2("AIDX", bnum, sunum), acpi_index)); 716 /* 717 * update acpi-index to actual value 718 */ 719 aml_append(ifctx, aml_store(acpi_index, aml_index(ret, zero))); 720 aml_append(ifctx, aml_return(ret)); 721 } 722 723 aml_append(method, ifctx); 724 return method; 725 } 726 727 /** 728 * build_prt_entry: 729 * @link_name: link name for PCI route entry 730 * 731 * build AML package containing a PCI route entry for @link_name 732 */ 733 static Aml *build_prt_entry(const char *link_name) 734 { 735 Aml *a_zero = aml_int(0); 736 Aml *pkg = aml_package(4); 737 aml_append(pkg, a_zero); 738 aml_append(pkg, a_zero); 739 aml_append(pkg, aml_name("%s", link_name)); 740 aml_append(pkg, a_zero); 741 return pkg; 742 } 743 744 /* 745 * initialize_route - Initialize the interrupt routing rule 746 * through a specific LINK: 747 * if (lnk_idx == idx) 748 * route using link 'link_name' 749 */ 750 static Aml *initialize_route(Aml *route, const char *link_name, 751 Aml *lnk_idx, int idx) 752 { 753 Aml *if_ctx = aml_if(aml_equal(lnk_idx, aml_int(idx))); 754 Aml *pkg = build_prt_entry(link_name); 755 756 aml_append(if_ctx, aml_store(pkg, route)); 757 758 return if_ctx; 759 } 760 761 /* 762 * build_prt - Define interrupt rounting rules 763 * 764 * Returns an array of 128 routes, one for each device, 765 * based on device location. 766 * The main goal is to equaly distribute the interrupts 767 * over the 4 existing ACPI links (works only for i440fx). 768 * The hash function is (slot + pin) & 3 -> "LNK[D|A|B|C]". 769 * 770 */ 771 static Aml *build_prt(bool is_pci0_prt) 772 { 773 Aml *method, *while_ctx, *pin, *res; 774 775 method = aml_method("_PRT", 0, AML_NOTSERIALIZED); 776 res = aml_local(0); 777 pin = aml_local(1); 778 aml_append(method, aml_store(aml_package(128), res)); 779 aml_append(method, aml_store(aml_int(0), pin)); 780 781 /* while (pin < 128) */ 782 while_ctx = aml_while(aml_lless(pin, aml_int(128))); 783 { 784 Aml *slot = aml_local(2); 785 Aml *lnk_idx = aml_local(3); 786 Aml *route = aml_local(4); 787 788 /* slot = pin >> 2 */ 789 aml_append(while_ctx, 790 aml_store(aml_shiftright(pin, aml_int(2), NULL), slot)); 791 /* lnk_idx = (slot + pin) & 3 */ 792 aml_append(while_ctx, 793 aml_store(aml_and(aml_add(pin, slot, NULL), aml_int(3), NULL), 794 lnk_idx)); 795 796 /* route[2] = "LNK[D|A|B|C]", selection based on pin % 3 */ 797 aml_append(while_ctx, initialize_route(route, "LNKD", lnk_idx, 0)); 798 if (is_pci0_prt) { 799 Aml *if_device_1, *if_pin_4, *else_pin_4; 800 801 /* device 1 is the power-management device, needs SCI */ 802 if_device_1 = aml_if(aml_equal(lnk_idx, aml_int(1))); 803 { 804 if_pin_4 = aml_if(aml_equal(pin, aml_int(4))); 805 { 806 aml_append(if_pin_4, 807 aml_store(build_prt_entry("LNKS"), route)); 808 } 809 aml_append(if_device_1, if_pin_4); 810 else_pin_4 = aml_else(); 811 { 812 aml_append(else_pin_4, 813 aml_store(build_prt_entry("LNKA"), route)); 814 } 815 aml_append(if_device_1, else_pin_4); 816 } 817 aml_append(while_ctx, if_device_1); 818 } else { 819 aml_append(while_ctx, initialize_route(route, "LNKA", lnk_idx, 1)); 820 } 821 aml_append(while_ctx, initialize_route(route, "LNKB", lnk_idx, 2)); 822 aml_append(while_ctx, initialize_route(route, "LNKC", lnk_idx, 3)); 823 824 /* route[0] = 0x[slot]FFFF */ 825 aml_append(while_ctx, 826 aml_store(aml_or(aml_shiftleft(slot, aml_int(16)), aml_int(0xFFFF), 827 NULL), 828 aml_index(route, aml_int(0)))); 829 /* route[1] = pin & 3 */ 830 aml_append(while_ctx, 831 aml_store(aml_and(pin, aml_int(3), NULL), 832 aml_index(route, aml_int(1)))); 833 /* res[pin] = route */ 834 aml_append(while_ctx, aml_store(route, aml_index(res, pin))); 835 /* pin++ */ 836 aml_append(while_ctx, aml_increment(pin)); 837 } 838 aml_append(method, while_ctx); 839 /* return res*/ 840 aml_append(method, aml_return(res)); 841 842 return method; 843 } 844 845 static void build_hpet_aml(Aml *table) 846 { 847 Aml *crs; 848 Aml *field; 849 Aml *method; 850 Aml *if_ctx; 851 Aml *scope = aml_scope("_SB"); 852 Aml *dev = aml_device("HPET"); 853 Aml *zero = aml_int(0); 854 Aml *id = aml_local(0); 855 Aml *period = aml_local(1); 856 857 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0103"))); 858 aml_append(dev, aml_name_decl("_UID", zero)); 859 860 aml_append(dev, 861 aml_operation_region("HPTM", AML_SYSTEM_MEMORY, aml_int(HPET_BASE), 862 HPET_LEN)); 863 field = aml_field("HPTM", AML_DWORD_ACC, AML_LOCK, AML_PRESERVE); 864 aml_append(field, aml_named_field("VEND", 32)); 865 aml_append(field, aml_named_field("PRD", 32)); 866 aml_append(dev, field); 867 868 method = aml_method("_STA", 0, AML_NOTSERIALIZED); 869 aml_append(method, aml_store(aml_name("VEND"), id)); 870 aml_append(method, aml_store(aml_name("PRD"), period)); 871 aml_append(method, aml_shiftright(id, aml_int(16), id)); 872 if_ctx = aml_if(aml_lor(aml_equal(id, zero), 873 aml_equal(id, aml_int(0xffff)))); 874 { 875 aml_append(if_ctx, aml_return(zero)); 876 } 877 aml_append(method, if_ctx); 878 879 if_ctx = aml_if(aml_lor(aml_equal(period, zero), 880 aml_lgreater(period, aml_int(100000000)))); 881 { 882 aml_append(if_ctx, aml_return(zero)); 883 } 884 aml_append(method, if_ctx); 885 886 aml_append(method, aml_return(aml_int(0x0F))); 887 aml_append(dev, method); 888 889 crs = aml_resource_template(); 890 aml_append(crs, aml_memory32_fixed(HPET_BASE, HPET_LEN, AML_READ_ONLY)); 891 aml_append(dev, aml_name_decl("_CRS", crs)); 892 893 aml_append(scope, dev); 894 aml_append(table, scope); 895 } 896 897 static Aml *build_vmbus_device_aml(VMBusBridge *vmbus_bridge) 898 { 899 Aml *dev; 900 Aml *method; 901 Aml *crs; 902 903 dev = aml_device("VMBS"); 904 aml_append(dev, aml_name_decl("STA", aml_int(0xF))); 905 aml_append(dev, aml_name_decl("_HID", aml_string("VMBus"))); 906 aml_append(dev, aml_name_decl("_UID", aml_int(0x0))); 907 aml_append(dev, aml_name_decl("_DDN", aml_string("VMBUS"))); 908 909 method = aml_method("_DIS", 0, AML_NOTSERIALIZED); 910 aml_append(method, aml_store(aml_and(aml_name("STA"), aml_int(0xD), NULL), 911 aml_name("STA"))); 912 aml_append(dev, method); 913 914 method = aml_method("_PS0", 0, AML_NOTSERIALIZED); 915 aml_append(method, aml_store(aml_or(aml_name("STA"), aml_int(0xF), NULL), 916 aml_name("STA"))); 917 aml_append(dev, method); 918 919 method = aml_method("_STA", 0, AML_NOTSERIALIZED); 920 aml_append(method, aml_return(aml_name("STA"))); 921 aml_append(dev, method); 922 923 aml_append(dev, aml_name_decl("_PS3", aml_int(0x0))); 924 925 crs = aml_resource_template(); 926 aml_append(crs, aml_irq_no_flags(vmbus_bridge->irq)); 927 aml_append(dev, aml_name_decl("_CRS", crs)); 928 929 return dev; 930 } 931 932 static void build_dbg_aml(Aml *table) 933 { 934 Aml *field; 935 Aml *method; 936 Aml *while_ctx; 937 Aml *scope = aml_scope("\\"); 938 Aml *buf = aml_local(0); 939 Aml *len = aml_local(1); 940 Aml *idx = aml_local(2); 941 942 aml_append(scope, 943 aml_operation_region("DBG", AML_SYSTEM_IO, aml_int(0x0402), 0x01)); 944 field = aml_field("DBG", AML_BYTE_ACC, AML_NOLOCK, AML_PRESERVE); 945 aml_append(field, aml_named_field("DBGB", 8)); 946 aml_append(scope, field); 947 948 method = aml_method("DBUG", 1, AML_NOTSERIALIZED); 949 950 aml_append(method, aml_to_hexstring(aml_arg(0), buf)); 951 aml_append(method, aml_to_buffer(buf, buf)); 952 aml_append(method, aml_subtract(aml_sizeof(buf), aml_int(1), len)); 953 aml_append(method, aml_store(aml_int(0), idx)); 954 955 while_ctx = aml_while(aml_lless(idx, len)); 956 aml_append(while_ctx, 957 aml_store(aml_derefof(aml_index(buf, idx)), aml_name("DBGB"))); 958 aml_append(while_ctx, aml_increment(idx)); 959 aml_append(method, while_ctx); 960 961 aml_append(method, aml_store(aml_int(0x0A), aml_name("DBGB"))); 962 aml_append(scope, method); 963 964 aml_append(table, scope); 965 } 966 967 static Aml *build_link_dev(const char *name, uint8_t uid, Aml *reg) 968 { 969 Aml *dev; 970 Aml *crs; 971 Aml *method; 972 uint32_t irqs[] = {5, 10, 11}; 973 974 dev = aml_device("%s", name); 975 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C0F"))); 976 aml_append(dev, aml_name_decl("_UID", aml_int(uid))); 977 978 crs = aml_resource_template(); 979 aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL, AML_ACTIVE_HIGH, 980 AML_SHARED, irqs, ARRAY_SIZE(irqs))); 981 aml_append(dev, aml_name_decl("_PRS", crs)); 982 983 method = aml_method("_STA", 0, AML_NOTSERIALIZED); 984 aml_append(method, aml_return(aml_call1("IQST", reg))); 985 aml_append(dev, method); 986 987 method = aml_method("_DIS", 0, AML_NOTSERIALIZED); 988 aml_append(method, aml_or(reg, aml_int(0x80), reg)); 989 aml_append(dev, method); 990 991 method = aml_method("_CRS", 0, AML_NOTSERIALIZED); 992 aml_append(method, aml_return(aml_call1("IQCR", reg))); 993 aml_append(dev, method); 994 995 method = aml_method("_SRS", 1, AML_NOTSERIALIZED); 996 aml_append(method, aml_create_dword_field(aml_arg(0), aml_int(5), "PRRI")); 997 aml_append(method, aml_store(aml_name("PRRI"), reg)); 998 aml_append(dev, method); 999 1000 return dev; 1001 } 1002 1003 static Aml *build_gsi_link_dev(const char *name, uint8_t uid, uint8_t gsi) 1004 { 1005 Aml *dev; 1006 Aml *crs; 1007 Aml *method; 1008 uint32_t irqs; 1009 1010 dev = aml_device("%s", name); 1011 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C0F"))); 1012 aml_append(dev, aml_name_decl("_UID", aml_int(uid))); 1013 1014 crs = aml_resource_template(); 1015 irqs = gsi; 1016 aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL, AML_ACTIVE_HIGH, 1017 AML_SHARED, &irqs, 1)); 1018 aml_append(dev, aml_name_decl("_PRS", crs)); 1019 1020 aml_append(dev, aml_name_decl("_CRS", crs)); 1021 1022 /* 1023 * _DIS can be no-op because the interrupt cannot be disabled. 1024 */ 1025 method = aml_method("_DIS", 0, AML_NOTSERIALIZED); 1026 aml_append(dev, method); 1027 1028 method = aml_method("_SRS", 1, AML_NOTSERIALIZED); 1029 aml_append(dev, method); 1030 1031 return dev; 1032 } 1033 1034 /* _CRS method - get current settings */ 1035 static Aml *build_iqcr_method(bool is_piix4) 1036 { 1037 Aml *if_ctx; 1038 uint32_t irqs; 1039 Aml *method = aml_method("IQCR", 1, AML_SERIALIZED); 1040 Aml *crs = aml_resource_template(); 1041 1042 irqs = 0; 1043 aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL, 1044 AML_ACTIVE_HIGH, AML_SHARED, &irqs, 1)); 1045 aml_append(method, aml_name_decl("PRR0", crs)); 1046 1047 aml_append(method, 1048 aml_create_dword_field(aml_name("PRR0"), aml_int(5), "PRRI")); 1049 1050 if (is_piix4) { 1051 if_ctx = aml_if(aml_lless(aml_arg(0), aml_int(0x80))); 1052 aml_append(if_ctx, aml_store(aml_arg(0), aml_name("PRRI"))); 1053 aml_append(method, if_ctx); 1054 } else { 1055 aml_append(method, 1056 aml_store(aml_and(aml_arg(0), aml_int(0xF), NULL), 1057 aml_name("PRRI"))); 1058 } 1059 1060 aml_append(method, aml_return(aml_name("PRR0"))); 1061 return method; 1062 } 1063 1064 /* _STA method - get status */ 1065 static Aml *build_irq_status_method(void) 1066 { 1067 Aml *if_ctx; 1068 Aml *method = aml_method("IQST", 1, AML_NOTSERIALIZED); 1069 1070 if_ctx = aml_if(aml_and(aml_int(0x80), aml_arg(0), NULL)); 1071 aml_append(if_ctx, aml_return(aml_int(0x09))); 1072 aml_append(method, if_ctx); 1073 aml_append(method, aml_return(aml_int(0x0B))); 1074 return method; 1075 } 1076 1077 static void build_piix4_pci0_int(Aml *table) 1078 { 1079 Aml *dev; 1080 Aml *crs; 1081 Aml *method; 1082 uint32_t irqs; 1083 Aml *sb_scope = aml_scope("_SB"); 1084 Aml *pci0_scope = aml_scope("PCI0"); 1085 1086 aml_append(pci0_scope, build_prt(true)); 1087 aml_append(sb_scope, pci0_scope); 1088 1089 aml_append(sb_scope, build_irq_status_method()); 1090 aml_append(sb_scope, build_iqcr_method(true)); 1091 1092 aml_append(sb_scope, build_link_dev("LNKA", 0, aml_name("PRQ0"))); 1093 aml_append(sb_scope, build_link_dev("LNKB", 1, aml_name("PRQ1"))); 1094 aml_append(sb_scope, build_link_dev("LNKC", 2, aml_name("PRQ2"))); 1095 aml_append(sb_scope, build_link_dev("LNKD", 3, aml_name("PRQ3"))); 1096 1097 dev = aml_device("LNKS"); 1098 { 1099 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C0F"))); 1100 aml_append(dev, aml_name_decl("_UID", aml_int(4))); 1101 1102 crs = aml_resource_template(); 1103 irqs = 9; 1104 aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL, 1105 AML_ACTIVE_HIGH, AML_SHARED, 1106 &irqs, 1)); 1107 aml_append(dev, aml_name_decl("_PRS", crs)); 1108 1109 /* The SCI cannot be disabled and is always attached to GSI 9, 1110 * so these are no-ops. We only need this link to override the 1111 * polarity to active high and match the content of the MADT. 1112 */ 1113 method = aml_method("_STA", 0, AML_NOTSERIALIZED); 1114 aml_append(method, aml_return(aml_int(0x0b))); 1115 aml_append(dev, method); 1116 1117 method = aml_method("_DIS", 0, AML_NOTSERIALIZED); 1118 aml_append(dev, method); 1119 1120 method = aml_method("_CRS", 0, AML_NOTSERIALIZED); 1121 aml_append(method, aml_return(aml_name("_PRS"))); 1122 aml_append(dev, method); 1123 1124 method = aml_method("_SRS", 1, AML_NOTSERIALIZED); 1125 aml_append(dev, method); 1126 } 1127 aml_append(sb_scope, dev); 1128 1129 aml_append(table, sb_scope); 1130 } 1131 1132 static void append_q35_prt_entry(Aml *ctx, uint32_t nr, const char *name) 1133 { 1134 int i; 1135 int head; 1136 Aml *pkg; 1137 char base = name[3] < 'E' ? 'A' : 'E'; 1138 char *s = g_strdup(name); 1139 Aml *a_nr = aml_int((nr << 16) | 0xffff); 1140 1141 assert(strlen(s) == 4); 1142 1143 head = name[3] - base; 1144 for (i = 0; i < 4; i++) { 1145 if (head + i > 3) { 1146 head = i * -1; 1147 } 1148 s[3] = base + head + i; 1149 pkg = aml_package(4); 1150 aml_append(pkg, a_nr); 1151 aml_append(pkg, aml_int(i)); 1152 aml_append(pkg, aml_name("%s", s)); 1153 aml_append(pkg, aml_int(0)); 1154 aml_append(ctx, pkg); 1155 } 1156 g_free(s); 1157 } 1158 1159 static Aml *build_q35_routing_table(const char *str) 1160 { 1161 int i; 1162 Aml *pkg; 1163 char *name = g_strdup_printf("%s ", str); 1164 1165 pkg = aml_package(128); 1166 for (i = 0; i < 0x18; i++) { 1167 name[3] = 'E' + (i & 0x3); 1168 append_q35_prt_entry(pkg, i, name); 1169 } 1170 1171 name[3] = 'E'; 1172 append_q35_prt_entry(pkg, 0x18, name); 1173 1174 /* INTA -> PIRQA for slot 25 - 31, see the default value of D<N>IR */ 1175 for (i = 0x0019; i < 0x1e; i++) { 1176 name[3] = 'A'; 1177 append_q35_prt_entry(pkg, i, name); 1178 } 1179 1180 /* PCIe->PCI bridge. use PIRQ[E-H] */ 1181 name[3] = 'E'; 1182 append_q35_prt_entry(pkg, 0x1e, name); 1183 name[3] = 'A'; 1184 append_q35_prt_entry(pkg, 0x1f, name); 1185 1186 g_free(name); 1187 return pkg; 1188 } 1189 1190 static void build_q35_pci0_int(Aml *table) 1191 { 1192 Aml *method; 1193 Aml *sb_scope = aml_scope("_SB"); 1194 Aml *pci0_scope = aml_scope("PCI0"); 1195 1196 /* Zero => PIC mode, One => APIC Mode */ 1197 aml_append(table, aml_name_decl("PICF", aml_int(0))); 1198 method = aml_method("_PIC", 1, AML_NOTSERIALIZED); 1199 { 1200 aml_append(method, aml_store(aml_arg(0), aml_name("PICF"))); 1201 } 1202 aml_append(table, method); 1203 1204 aml_append(pci0_scope, 1205 aml_name_decl("PRTP", build_q35_routing_table("LNK"))); 1206 aml_append(pci0_scope, 1207 aml_name_decl("PRTA", build_q35_routing_table("GSI"))); 1208 1209 method = aml_method("_PRT", 0, AML_NOTSERIALIZED); 1210 { 1211 Aml *if_ctx; 1212 Aml *else_ctx; 1213 1214 /* PCI IRQ routing table, example from ACPI 2.0a specification, 1215 section 6.2.8.1 */ 1216 /* Note: we provide the same info as the PCI routing 1217 table of the Bochs BIOS */ 1218 if_ctx = aml_if(aml_equal(aml_name("PICF"), aml_int(0))); 1219 aml_append(if_ctx, aml_return(aml_name("PRTP"))); 1220 aml_append(method, if_ctx); 1221 else_ctx = aml_else(); 1222 aml_append(else_ctx, aml_return(aml_name("PRTA"))); 1223 aml_append(method, else_ctx); 1224 } 1225 aml_append(pci0_scope, method); 1226 aml_append(sb_scope, pci0_scope); 1227 1228 aml_append(sb_scope, build_irq_status_method()); 1229 aml_append(sb_scope, build_iqcr_method(false)); 1230 1231 aml_append(sb_scope, build_link_dev("LNKA", 0, aml_name("PRQA"))); 1232 aml_append(sb_scope, build_link_dev("LNKB", 1, aml_name("PRQB"))); 1233 aml_append(sb_scope, build_link_dev("LNKC", 2, aml_name("PRQC"))); 1234 aml_append(sb_scope, build_link_dev("LNKD", 3, aml_name("PRQD"))); 1235 aml_append(sb_scope, build_link_dev("LNKE", 4, aml_name("PRQE"))); 1236 aml_append(sb_scope, build_link_dev("LNKF", 5, aml_name("PRQF"))); 1237 aml_append(sb_scope, build_link_dev("LNKG", 6, aml_name("PRQG"))); 1238 aml_append(sb_scope, build_link_dev("LNKH", 7, aml_name("PRQH"))); 1239 1240 aml_append(sb_scope, build_gsi_link_dev("GSIA", 0x10, 0x10)); 1241 aml_append(sb_scope, build_gsi_link_dev("GSIB", 0x11, 0x11)); 1242 aml_append(sb_scope, build_gsi_link_dev("GSIC", 0x12, 0x12)); 1243 aml_append(sb_scope, build_gsi_link_dev("GSID", 0x13, 0x13)); 1244 aml_append(sb_scope, build_gsi_link_dev("GSIE", 0x14, 0x14)); 1245 aml_append(sb_scope, build_gsi_link_dev("GSIF", 0x15, 0x15)); 1246 aml_append(sb_scope, build_gsi_link_dev("GSIG", 0x16, 0x16)); 1247 aml_append(sb_scope, build_gsi_link_dev("GSIH", 0x17, 0x17)); 1248 1249 aml_append(table, sb_scope); 1250 } 1251 1252 static Aml *build_q35_dram_controller(const AcpiMcfgInfo *mcfg) 1253 { 1254 Aml *dev; 1255 Aml *resource_template; 1256 1257 /* DRAM controller */ 1258 dev = aml_device("DRAC"); 1259 aml_append(dev, aml_name_decl("_HID", aml_string("PNP0C01"))); 1260 1261 resource_template = aml_resource_template(); 1262 if (mcfg->base + mcfg->size - 1 >= (1ULL << 32)) { 1263 aml_append(resource_template, 1264 aml_qword_memory(AML_POS_DECODE, 1265 AML_MIN_FIXED, 1266 AML_MAX_FIXED, 1267 AML_NON_CACHEABLE, 1268 AML_READ_WRITE, 1269 0x0000000000000000, 1270 mcfg->base, 1271 mcfg->base + mcfg->size - 1, 1272 0x0000000000000000, 1273 mcfg->size)); 1274 } else { 1275 aml_append(resource_template, 1276 aml_dword_memory(AML_POS_DECODE, 1277 AML_MIN_FIXED, 1278 AML_MAX_FIXED, 1279 AML_NON_CACHEABLE, 1280 AML_READ_WRITE, 1281 0x0000000000000000, 1282 mcfg->base, 1283 mcfg->base + mcfg->size - 1, 1284 0x0000000000000000, 1285 mcfg->size)); 1286 } 1287 aml_append(dev, aml_name_decl("_CRS", resource_template)); 1288 1289 return dev; 1290 } 1291 1292 static void build_x86_acpi_pci_hotplug(Aml *table, uint64_t pcihp_addr) 1293 { 1294 Aml *scope; 1295 Aml *field; 1296 Aml *method; 1297 1298 scope = aml_scope("_SB.PCI0"); 1299 1300 aml_append(scope, 1301 aml_operation_region("PCST", AML_SYSTEM_IO, aml_int(pcihp_addr), 0x08)); 1302 field = aml_field("PCST", AML_DWORD_ACC, AML_NOLOCK, AML_WRITE_AS_ZEROS); 1303 aml_append(field, aml_named_field("PCIU", 32)); 1304 aml_append(field, aml_named_field("PCID", 32)); 1305 aml_append(scope, field); 1306 1307 aml_append(scope, 1308 aml_operation_region("SEJ", AML_SYSTEM_IO, 1309 aml_int(pcihp_addr + ACPI_PCIHP_SEJ_BASE), 0x04)); 1310 field = aml_field("SEJ", AML_DWORD_ACC, AML_NOLOCK, AML_WRITE_AS_ZEROS); 1311 aml_append(field, aml_named_field("B0EJ", 32)); 1312 aml_append(scope, field); 1313 1314 aml_append(scope, 1315 aml_operation_region("BNMR", AML_SYSTEM_IO, 1316 aml_int(pcihp_addr + ACPI_PCIHP_BNMR_BASE), 0x08)); 1317 field = aml_field("BNMR", AML_DWORD_ACC, AML_NOLOCK, AML_WRITE_AS_ZEROS); 1318 aml_append(field, aml_named_field("BNUM", 32)); 1319 aml_append(field, aml_named_field("PIDX", 32)); 1320 aml_append(scope, field); 1321 1322 aml_append(scope, aml_mutex("BLCK", 0)); 1323 1324 method = aml_method("PCEJ", 2, AML_NOTSERIALIZED); 1325 aml_append(method, aml_acquire(aml_name("BLCK"), 0xFFFF)); 1326 aml_append(method, aml_store(aml_arg(0), aml_name("BNUM"))); 1327 aml_append(method, 1328 aml_store(aml_shiftleft(aml_int(1), aml_arg(1)), aml_name("B0EJ"))); 1329 aml_append(method, aml_release(aml_name("BLCK"))); 1330 aml_append(method, aml_return(aml_int(0))); 1331 aml_append(scope, method); 1332 1333 method = aml_method("AIDX", 2, AML_NOTSERIALIZED); 1334 aml_append(method, aml_acquire(aml_name("BLCK"), 0xFFFF)); 1335 aml_append(method, aml_store(aml_arg(0), aml_name("BNUM"))); 1336 aml_append(method, 1337 aml_store(aml_shiftleft(aml_int(1), aml_arg(1)), aml_name("PIDX"))); 1338 aml_append(method, aml_store(aml_name("PIDX"), aml_local(0))); 1339 aml_append(method, aml_release(aml_name("BLCK"))); 1340 aml_append(method, aml_return(aml_local(0))); 1341 aml_append(scope, method); 1342 1343 aml_append(scope, aml_pci_pdsm()); 1344 1345 aml_append(table, scope); 1346 } 1347 1348 static Aml *build_q35_osc_method(bool enable_native_pcie_hotplug) 1349 { 1350 Aml *if_ctx; 1351 Aml *if_ctx2; 1352 Aml *else_ctx; 1353 Aml *method; 1354 Aml *a_cwd1 = aml_name("CDW1"); 1355 Aml *a_ctrl = aml_local(0); 1356 1357 method = aml_method("_OSC", 4, AML_NOTSERIALIZED); 1358 aml_append(method, aml_create_dword_field(aml_arg(3), aml_int(0), "CDW1")); 1359 1360 if_ctx = aml_if(aml_equal( 1361 aml_arg(0), aml_touuid("33DB4D5B-1FF7-401C-9657-7441C03DD766"))); 1362 aml_append(if_ctx, aml_create_dword_field(aml_arg(3), aml_int(4), "CDW2")); 1363 aml_append(if_ctx, aml_create_dword_field(aml_arg(3), aml_int(8), "CDW3")); 1364 1365 aml_append(if_ctx, aml_store(aml_name("CDW3"), a_ctrl)); 1366 1367 /* 1368 * Always allow native PME, AER (no dependencies) 1369 * Allow SHPC (PCI bridges can have SHPC controller) 1370 * Disable PCIe Native Hot-plug if ACPI PCI Hot-plug is enabled. 1371 */ 1372 aml_append(if_ctx, aml_and(a_ctrl, 1373 aml_int(0x1E | (enable_native_pcie_hotplug ? 0x1 : 0x0)), a_ctrl)); 1374 1375 if_ctx2 = aml_if(aml_lnot(aml_equal(aml_arg(1), aml_int(1)))); 1376 /* Unknown revision */ 1377 aml_append(if_ctx2, aml_or(a_cwd1, aml_int(0x08), a_cwd1)); 1378 aml_append(if_ctx, if_ctx2); 1379 1380 if_ctx2 = aml_if(aml_lnot(aml_equal(aml_name("CDW3"), a_ctrl))); 1381 /* Capabilities bits were masked */ 1382 aml_append(if_ctx2, aml_or(a_cwd1, aml_int(0x10), a_cwd1)); 1383 aml_append(if_ctx, if_ctx2); 1384 1385 /* Update DWORD3 in the buffer */ 1386 aml_append(if_ctx, aml_store(a_ctrl, aml_name("CDW3"))); 1387 aml_append(method, if_ctx); 1388 1389 else_ctx = aml_else(); 1390 /* Unrecognized UUID */ 1391 aml_append(else_ctx, aml_or(a_cwd1, aml_int(4), a_cwd1)); 1392 aml_append(method, else_ctx); 1393 1394 aml_append(method, aml_return(aml_arg(3))); 1395 return method; 1396 } 1397 1398 static void build_acpi0017(Aml *table) 1399 { 1400 Aml *dev, *scope, *method; 1401 1402 scope = aml_scope("_SB"); 1403 dev = aml_device("CXLM"); 1404 aml_append(dev, aml_name_decl("_HID", aml_string("ACPI0017"))); 1405 1406 method = aml_method("_STA", 0, AML_NOTSERIALIZED); 1407 aml_append(method, aml_return(aml_int(0x01))); 1408 aml_append(dev, method); 1409 1410 aml_append(scope, dev); 1411 aml_append(table, scope); 1412 } 1413 1414 static void 1415 build_dsdt(GArray *table_data, BIOSLinker *linker, 1416 AcpiPmInfo *pm, AcpiMiscInfo *misc, 1417 Range *pci_hole, Range *pci_hole64, MachineState *machine) 1418 { 1419 Object *i440fx = object_resolve_type_unambiguous(TYPE_I440FX_PCI_HOST_BRIDGE); 1420 Object *q35 = object_resolve_type_unambiguous(TYPE_Q35_HOST_DEVICE); 1421 CrsRangeEntry *entry; 1422 Aml *dsdt, *sb_scope, *scope, *dev, *method, *field, *pkg, *crs; 1423 CrsRangeSet crs_range_set; 1424 PCMachineState *pcms = PC_MACHINE(machine); 1425 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(machine); 1426 X86MachineState *x86ms = X86_MACHINE(machine); 1427 AcpiMcfgInfo mcfg; 1428 bool mcfg_valid = !!acpi_get_mcfg(&mcfg); 1429 uint32_t nr_mem = machine->ram_slots; 1430 int root_bus_limit = 0xFF; 1431 PCIBus *bus = NULL; 1432 #ifdef CONFIG_TPM 1433 TPMIf *tpm = tpm_find(); 1434 #endif 1435 bool cxl_present = false; 1436 int i; 1437 VMBusBridge *vmbus_bridge = vmbus_bridge_find(); 1438 AcpiTable table = { .sig = "DSDT", .rev = 1, .oem_id = x86ms->oem_id, 1439 .oem_table_id = x86ms->oem_table_id }; 1440 1441 assert(!!i440fx != !!q35); 1442 1443 acpi_table_begin(&table, table_data); 1444 dsdt = init_aml_allocator(); 1445 1446 build_dbg_aml(dsdt); 1447 if (i440fx) { 1448 sb_scope = aml_scope("_SB"); 1449 dev = aml_device("PCI0"); 1450 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A03"))); 1451 aml_append(dev, aml_name_decl("_ADR", aml_int(0))); 1452 aml_append(dev, aml_name_decl("_UID", aml_int(pcmc->pci_root_uid))); 1453 aml_append(dev, aml_pci_edsm()); 1454 aml_append(sb_scope, dev); 1455 aml_append(dsdt, sb_scope); 1456 1457 if (pm->pcihp_bridge_en || pm->pcihp_root_en) { 1458 build_x86_acpi_pci_hotplug(dsdt, pm->pcihp_io_base); 1459 } 1460 build_piix4_pci0_int(dsdt); 1461 } else if (q35) { 1462 sb_scope = aml_scope("_SB"); 1463 dev = aml_device("PCI0"); 1464 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A08"))); 1465 aml_append(dev, aml_name_decl("_CID", aml_eisaid("PNP0A03"))); 1466 aml_append(dev, aml_name_decl("_ADR", aml_int(0))); 1467 aml_append(dev, aml_name_decl("_UID", aml_int(pcmc->pci_root_uid))); 1468 aml_append(dev, build_q35_osc_method(!pm->pcihp_bridge_en)); 1469 aml_append(dev, aml_pci_edsm()); 1470 aml_append(sb_scope, dev); 1471 if (mcfg_valid) { 1472 aml_append(sb_scope, build_q35_dram_controller(&mcfg)); 1473 } 1474 1475 if (pm->smi_on_cpuhp) { 1476 /* reserve SMI block resources, IO ports 0xB2, 0xB3 */ 1477 dev = aml_device("PCI0.SMI0"); 1478 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A06"))); 1479 aml_append(dev, aml_name_decl("_UID", aml_string("SMI resources"))); 1480 crs = aml_resource_template(); 1481 aml_append(crs, 1482 aml_io( 1483 AML_DECODE16, 1484 ACPI_PORT_SMI_CMD, 1485 ACPI_PORT_SMI_CMD, 1486 1, 1487 2) 1488 ); 1489 aml_append(dev, aml_name_decl("_CRS", crs)); 1490 aml_append(dev, aml_operation_region("SMIR", AML_SYSTEM_IO, 1491 aml_int(ACPI_PORT_SMI_CMD), 2)); 1492 field = aml_field("SMIR", AML_BYTE_ACC, AML_NOLOCK, 1493 AML_WRITE_AS_ZEROS); 1494 aml_append(field, aml_named_field("SMIC", 8)); 1495 aml_append(field, aml_reserved_field(8)); 1496 aml_append(dev, field); 1497 aml_append(sb_scope, dev); 1498 } 1499 1500 aml_append(dsdt, sb_scope); 1501 1502 if (pm->pcihp_bridge_en) { 1503 build_x86_acpi_pci_hotplug(dsdt, pm->pcihp_io_base); 1504 } 1505 build_q35_pci0_int(dsdt); 1506 } 1507 1508 if (misc->has_hpet) { 1509 build_hpet_aml(dsdt); 1510 } 1511 1512 if (vmbus_bridge) { 1513 sb_scope = aml_scope("_SB"); 1514 aml_append(sb_scope, build_vmbus_device_aml(vmbus_bridge)); 1515 aml_append(dsdt, sb_scope); 1516 } 1517 1518 scope = aml_scope("_GPE"); 1519 { 1520 aml_append(scope, aml_name_decl("_HID", aml_string("ACPI0006"))); 1521 if (machine->nvdimms_state->is_enabled) { 1522 method = aml_method("_E04", 0, AML_NOTSERIALIZED); 1523 aml_append(method, aml_notify(aml_name("\\_SB.NVDR"), 1524 aml_int(0x80))); 1525 aml_append(scope, method); 1526 } 1527 } 1528 aml_append(dsdt, scope); 1529 1530 if (pcmc->legacy_cpu_hotplug) { 1531 build_legacy_cpu_hotplug_aml(dsdt, machine, pm->cpu_hp_io_base); 1532 } else { 1533 CPUHotplugFeatures opts = { 1534 .acpi_1_compatible = true, .has_legacy_cphp = true, 1535 .smi_path = pm->smi_on_cpuhp ? "\\_SB.PCI0.SMI0.SMIC" : NULL, 1536 .fw_unplugs_cpu = pm->smi_on_cpu_unplug, 1537 }; 1538 build_cpus_aml(dsdt, machine, opts, pm->cpu_hp_io_base, 1539 "\\_SB.PCI0", "\\_GPE._E02"); 1540 } 1541 1542 if (pcms->memhp_io_base && nr_mem) { 1543 build_memory_hotplug_aml(dsdt, nr_mem, "\\_SB.PCI0", 1544 "\\_GPE._E03", AML_SYSTEM_IO, 1545 pcms->memhp_io_base); 1546 } 1547 1548 crs_range_set_init(&crs_range_set); 1549 bus = PC_MACHINE(machine)->bus; 1550 if (bus) { 1551 QLIST_FOREACH(bus, &bus->child, sibling) { 1552 uint8_t bus_num = pci_bus_num(bus); 1553 uint8_t numa_node = pci_bus_numa_node(bus); 1554 1555 /* look only for expander root buses */ 1556 if (!pci_bus_is_root(bus)) { 1557 continue; 1558 } 1559 1560 if (bus_num < root_bus_limit) { 1561 root_bus_limit = bus_num - 1; 1562 } 1563 1564 scope = aml_scope("\\_SB"); 1565 1566 if (pci_bus_is_cxl(bus)) { 1567 dev = aml_device("CL%.02X", bus_num); 1568 } else { 1569 dev = aml_device("PC%.02X", bus_num); 1570 } 1571 aml_append(dev, aml_name_decl("_UID", aml_int(bus_num))); 1572 aml_append(dev, aml_name_decl("_BBN", aml_int(bus_num))); 1573 if (pci_bus_is_cxl(bus)) { 1574 struct Aml *pkg = aml_package(2); 1575 1576 aml_append(dev, aml_name_decl("_HID", aml_string("ACPI0016"))); 1577 aml_append(pkg, aml_eisaid("PNP0A08")); 1578 aml_append(pkg, aml_eisaid("PNP0A03")); 1579 aml_append(dev, aml_name_decl("_CID", pkg)); 1580 aml_append(dev, aml_name_decl("_ADR", aml_int(0))); 1581 build_cxl_osc_method(dev); 1582 } else if (pci_bus_is_express(bus)) { 1583 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A08"))); 1584 aml_append(dev, aml_name_decl("_CID", aml_eisaid("PNP0A03"))); 1585 1586 /* Expander bridges do not have ACPI PCI Hot-plug enabled */ 1587 aml_append(dev, build_q35_osc_method(true)); 1588 } else { 1589 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A03"))); 1590 } 1591 1592 if (numa_node != NUMA_NODE_UNASSIGNED) { 1593 aml_append(dev, aml_name_decl("_PXM", aml_int(numa_node))); 1594 } 1595 1596 aml_append(dev, build_prt(false)); 1597 crs = build_crs(PCI_HOST_BRIDGE(BUS(bus)->parent), &crs_range_set, 1598 0, 0, 0, 0); 1599 aml_append(dev, aml_name_decl("_CRS", crs)); 1600 aml_append(scope, dev); 1601 aml_append(dsdt, scope); 1602 1603 /* Handle the ranges for the PXB expanders */ 1604 if (pci_bus_is_cxl(bus)) { 1605 MemoryRegion *mr = &pcms->cxl_devices_state.host_mr; 1606 uint64_t base = mr->addr; 1607 1608 cxl_present = true; 1609 crs_range_insert(crs_range_set.mem_ranges, base, 1610 base + memory_region_size(mr) - 1); 1611 } 1612 } 1613 } 1614 1615 if (cxl_present) { 1616 build_acpi0017(dsdt); 1617 } 1618 1619 /* 1620 * At this point crs_range_set has all the ranges used by pci 1621 * busses *other* than PCI0. These ranges will be excluded from 1622 * the PCI0._CRS. Add mmconfig to the set so it will be excluded 1623 * too. 1624 */ 1625 if (mcfg_valid) { 1626 crs_range_insert(crs_range_set.mem_ranges, 1627 mcfg.base, mcfg.base + mcfg.size - 1); 1628 } 1629 1630 scope = aml_scope("\\_SB.PCI0"); 1631 /* build PCI0._CRS */ 1632 crs = aml_resource_template(); 1633 aml_append(crs, 1634 aml_word_bus_number(AML_MIN_FIXED, AML_MAX_FIXED, AML_POS_DECODE, 1635 0x0000, 0x0, root_bus_limit, 1636 0x0000, root_bus_limit + 1)); 1637 aml_append(crs, aml_io(AML_DECODE16, 0x0CF8, 0x0CF8, 0x01, 0x08)); 1638 1639 aml_append(crs, 1640 aml_word_io(AML_MIN_FIXED, AML_MAX_FIXED, 1641 AML_POS_DECODE, AML_ENTIRE_RANGE, 1642 0x0000, 0x0000, 0x0CF7, 0x0000, 0x0CF8)); 1643 1644 crs_replace_with_free_ranges(crs_range_set.io_ranges, 0x0D00, 0xFFFF); 1645 for (i = 0; i < crs_range_set.io_ranges->len; i++) { 1646 entry = g_ptr_array_index(crs_range_set.io_ranges, i); 1647 aml_append(crs, 1648 aml_word_io(AML_MIN_FIXED, AML_MAX_FIXED, 1649 AML_POS_DECODE, AML_ENTIRE_RANGE, 1650 0x0000, entry->base, entry->limit, 1651 0x0000, entry->limit - entry->base + 1)); 1652 } 1653 1654 aml_append(crs, 1655 aml_dword_memory(AML_POS_DECODE, AML_MIN_FIXED, AML_MAX_FIXED, 1656 AML_CACHEABLE, AML_READ_WRITE, 1657 0, 0x000A0000, 0x000BFFFF, 0, 0x00020000)); 1658 1659 crs_replace_with_free_ranges(crs_range_set.mem_ranges, 1660 range_lob(pci_hole), 1661 range_upb(pci_hole)); 1662 for (i = 0; i < crs_range_set.mem_ranges->len; i++) { 1663 entry = g_ptr_array_index(crs_range_set.mem_ranges, i); 1664 aml_append(crs, 1665 aml_dword_memory(AML_POS_DECODE, AML_MIN_FIXED, AML_MAX_FIXED, 1666 AML_NON_CACHEABLE, AML_READ_WRITE, 1667 0, entry->base, entry->limit, 1668 0, entry->limit - entry->base + 1)); 1669 } 1670 1671 if (!range_is_empty(pci_hole64)) { 1672 crs_replace_with_free_ranges(crs_range_set.mem_64bit_ranges, 1673 range_lob(pci_hole64), 1674 range_upb(pci_hole64)); 1675 for (i = 0; i < crs_range_set.mem_64bit_ranges->len; i++) { 1676 entry = g_ptr_array_index(crs_range_set.mem_64bit_ranges, i); 1677 aml_append(crs, 1678 aml_qword_memory(AML_POS_DECODE, AML_MIN_FIXED, 1679 AML_MAX_FIXED, 1680 AML_CACHEABLE, AML_READ_WRITE, 1681 0, entry->base, entry->limit, 1682 0, entry->limit - entry->base + 1)); 1683 } 1684 } 1685 1686 #ifdef CONFIG_TPM 1687 if (TPM_IS_TIS_ISA(tpm_find())) { 1688 aml_append(crs, aml_memory32_fixed(TPM_TIS_ADDR_BASE, 1689 TPM_TIS_ADDR_SIZE, AML_READ_WRITE)); 1690 } 1691 #endif 1692 aml_append(scope, aml_name_decl("_CRS", crs)); 1693 1694 /* reserve GPE0 block resources */ 1695 dev = aml_device("GPE0"); 1696 aml_append(dev, aml_name_decl("_HID", aml_string("PNP0A06"))); 1697 aml_append(dev, aml_name_decl("_UID", aml_string("GPE0 resources"))); 1698 /* device present, functioning, decoding, not shown in UI */ 1699 aml_append(dev, aml_name_decl("_STA", aml_int(0xB))); 1700 crs = aml_resource_template(); 1701 aml_append(crs, 1702 aml_io( 1703 AML_DECODE16, 1704 pm->fadt.gpe0_blk.address, 1705 pm->fadt.gpe0_blk.address, 1706 1, 1707 pm->fadt.gpe0_blk.bit_width / 8) 1708 ); 1709 aml_append(dev, aml_name_decl("_CRS", crs)); 1710 aml_append(scope, dev); 1711 1712 crs_range_set_free(&crs_range_set); 1713 1714 /* reserve PCIHP resources */ 1715 if (pm->pcihp_io_len && (pm->pcihp_bridge_en || pm->pcihp_root_en)) { 1716 dev = aml_device("PHPR"); 1717 aml_append(dev, aml_name_decl("_HID", aml_string("PNP0A06"))); 1718 aml_append(dev, 1719 aml_name_decl("_UID", aml_string("PCI Hotplug resources"))); 1720 /* device present, functioning, decoding, not shown in UI */ 1721 aml_append(dev, aml_name_decl("_STA", aml_int(0xB))); 1722 crs = aml_resource_template(); 1723 aml_append(crs, 1724 aml_io(AML_DECODE16, pm->pcihp_io_base, pm->pcihp_io_base, 1, 1725 pm->pcihp_io_len) 1726 ); 1727 aml_append(dev, aml_name_decl("_CRS", crs)); 1728 aml_append(scope, dev); 1729 } 1730 aml_append(dsdt, scope); 1731 1732 /* create S3_ / S4_ / S5_ packages if necessary */ 1733 scope = aml_scope("\\"); 1734 if (!pm->s3_disabled) { 1735 pkg = aml_package(4); 1736 aml_append(pkg, aml_int(1)); /* PM1a_CNT.SLP_TYP */ 1737 aml_append(pkg, aml_int(1)); /* PM1b_CNT.SLP_TYP, FIXME: not impl. */ 1738 aml_append(pkg, aml_int(0)); /* reserved */ 1739 aml_append(pkg, aml_int(0)); /* reserved */ 1740 aml_append(scope, aml_name_decl("_S3", pkg)); 1741 } 1742 1743 if (!pm->s4_disabled) { 1744 pkg = aml_package(4); 1745 aml_append(pkg, aml_int(pm->s4_val)); /* PM1a_CNT.SLP_TYP */ 1746 /* PM1b_CNT.SLP_TYP, FIXME: not impl. */ 1747 aml_append(pkg, aml_int(pm->s4_val)); 1748 aml_append(pkg, aml_int(0)); /* reserved */ 1749 aml_append(pkg, aml_int(0)); /* reserved */ 1750 aml_append(scope, aml_name_decl("_S4", pkg)); 1751 } 1752 1753 pkg = aml_package(4); 1754 aml_append(pkg, aml_int(0)); /* PM1a_CNT.SLP_TYP */ 1755 aml_append(pkg, aml_int(0)); /* PM1b_CNT.SLP_TYP not impl. */ 1756 aml_append(pkg, aml_int(0)); /* reserved */ 1757 aml_append(pkg, aml_int(0)); /* reserved */ 1758 aml_append(scope, aml_name_decl("_S5", pkg)); 1759 aml_append(dsdt, scope); 1760 1761 /* create fw_cfg node, unconditionally */ 1762 { 1763 scope = aml_scope("\\_SB.PCI0"); 1764 fw_cfg_add_acpi_dsdt(scope, x86ms->fw_cfg); 1765 aml_append(dsdt, scope); 1766 } 1767 1768 sb_scope = aml_scope("\\_SB"); 1769 { 1770 Object *pci_host = acpi_get_i386_pci_host(); 1771 1772 if (pci_host) { 1773 PCIBus *bus = PCI_HOST_BRIDGE(pci_host)->bus; 1774 Aml *scope = aml_scope("PCI0"); 1775 /* Scan all PCI buses. Generate tables to support hotplug. */ 1776 build_append_pci_bus_devices(scope, bus); 1777 aml_append(sb_scope, scope); 1778 } 1779 } 1780 1781 #ifdef CONFIG_TPM 1782 if (TPM_IS_CRB(tpm)) { 1783 dev = aml_device("TPM"); 1784 aml_append(dev, aml_name_decl("_HID", aml_string("MSFT0101"))); 1785 aml_append(dev, aml_name_decl("_STR", 1786 aml_string("TPM 2.0 Device"))); 1787 crs = aml_resource_template(); 1788 aml_append(crs, aml_memory32_fixed(TPM_CRB_ADDR_BASE, 1789 TPM_CRB_ADDR_SIZE, AML_READ_WRITE)); 1790 aml_append(dev, aml_name_decl("_CRS", crs)); 1791 1792 aml_append(dev, aml_name_decl("_STA", aml_int(0xf))); 1793 aml_append(dev, aml_name_decl("_UID", aml_int(1))); 1794 1795 tpm_build_ppi_acpi(tpm, dev); 1796 1797 aml_append(sb_scope, dev); 1798 } 1799 #endif 1800 1801 if (pcms->sgx_epc.size != 0) { 1802 uint64_t epc_base = pcms->sgx_epc.base; 1803 uint64_t epc_size = pcms->sgx_epc.size; 1804 1805 dev = aml_device("EPC"); 1806 aml_append(dev, aml_name_decl("_HID", aml_eisaid("INT0E0C"))); 1807 aml_append(dev, aml_name_decl("_STR", 1808 aml_unicode("Enclave Page Cache 1.0"))); 1809 crs = aml_resource_template(); 1810 aml_append(crs, 1811 aml_qword_memory(AML_POS_DECODE, AML_MIN_FIXED, 1812 AML_MAX_FIXED, AML_NON_CACHEABLE, 1813 AML_READ_WRITE, 0, epc_base, 1814 epc_base + epc_size - 1, 0, epc_size)); 1815 aml_append(dev, aml_name_decl("_CRS", crs)); 1816 1817 method = aml_method("_STA", 0, AML_NOTSERIALIZED); 1818 aml_append(method, aml_return(aml_int(0x0f))); 1819 aml_append(dev, method); 1820 1821 aml_append(sb_scope, dev); 1822 } 1823 aml_append(dsdt, sb_scope); 1824 1825 if (pm->pcihp_bridge_en || pm->pcihp_root_en) { 1826 bool has_pcnt; 1827 1828 Object *pci_host = acpi_get_i386_pci_host(); 1829 PCIBus *bus = PCI_HOST_BRIDGE(pci_host)->bus; 1830 1831 scope = aml_scope("\\_SB.PCI0"); 1832 has_pcnt = build_append_notfication_callback(scope, bus); 1833 if (has_pcnt) { 1834 aml_append(dsdt, scope); 1835 } 1836 1837 scope = aml_scope("_GPE"); 1838 { 1839 method = aml_method("_E01", 0, AML_NOTSERIALIZED); 1840 if (has_pcnt) { 1841 aml_append(method, 1842 aml_acquire(aml_name("\\_SB.PCI0.BLCK"), 0xFFFF)); 1843 aml_append(method, aml_call0("\\_SB.PCI0.PCNT")); 1844 aml_append(method, aml_release(aml_name("\\_SB.PCI0.BLCK"))); 1845 } 1846 aml_append(scope, method); 1847 } 1848 aml_append(dsdt, scope); 1849 } 1850 1851 /* copy AML table into ACPI tables blob and patch header there */ 1852 g_array_append_vals(table_data, dsdt->buf->data, dsdt->buf->len); 1853 acpi_table_end(linker, &table); 1854 free_aml_allocator(); 1855 } 1856 1857 /* 1858 * IA-PC HPET (High Precision Event Timers) Specification (Revision: 1.0a) 1859 * 3.2.4The ACPI 2.0 HPET Description Table (HPET) 1860 */ 1861 static void 1862 build_hpet(GArray *table_data, BIOSLinker *linker, const char *oem_id, 1863 const char *oem_table_id) 1864 { 1865 AcpiTable table = { .sig = "HPET", .rev = 1, 1866 .oem_id = oem_id, .oem_table_id = oem_table_id }; 1867 1868 acpi_table_begin(&table, table_data); 1869 /* Note timer_block_id value must be kept in sync with value advertised by 1870 * emulated hpet 1871 */ 1872 /* Event Timer Block ID */ 1873 build_append_int_noprefix(table_data, 0x8086a201, 4); 1874 /* BASE_ADDRESS */ 1875 build_append_gas(table_data, AML_AS_SYSTEM_MEMORY, 0, 0, 0, HPET_BASE); 1876 /* HPET Number */ 1877 build_append_int_noprefix(table_data, 0, 1); 1878 /* Main Counter Minimum Clock_tick in Periodic Mode */ 1879 build_append_int_noprefix(table_data, 0, 2); 1880 /* Page Protection And OEM Attribute */ 1881 build_append_int_noprefix(table_data, 0, 1); 1882 acpi_table_end(linker, &table); 1883 } 1884 1885 #ifdef CONFIG_TPM 1886 /* 1887 * TCPA Description Table 1888 * 1889 * Following Level 00, Rev 00.37 of specs: 1890 * http://www.trustedcomputinggroup.org/resources/tcg_acpi_specification 1891 * 7.1.2 ACPI Table Layout 1892 */ 1893 static void 1894 build_tpm_tcpa(GArray *table_data, BIOSLinker *linker, GArray *tcpalog, 1895 const char *oem_id, const char *oem_table_id) 1896 { 1897 unsigned log_addr_offset; 1898 AcpiTable table = { .sig = "TCPA", .rev = 2, 1899 .oem_id = oem_id, .oem_table_id = oem_table_id }; 1900 1901 acpi_table_begin(&table, table_data); 1902 /* Platform Class */ 1903 build_append_int_noprefix(table_data, TPM_TCPA_ACPI_CLASS_CLIENT, 2); 1904 /* Log Area Minimum Length (LAML) */ 1905 build_append_int_noprefix(table_data, TPM_LOG_AREA_MINIMUM_SIZE, 4); 1906 /* Log Area Start Address (LASA) */ 1907 log_addr_offset = table_data->len; 1908 build_append_int_noprefix(table_data, 0, 8); 1909 1910 /* allocate/reserve space for TPM log area */ 1911 acpi_data_push(tcpalog, TPM_LOG_AREA_MINIMUM_SIZE); 1912 bios_linker_loader_alloc(linker, ACPI_BUILD_TPMLOG_FILE, tcpalog, 1, 1913 false /* high memory */); 1914 /* log area start address to be filled by Guest linker */ 1915 bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE, 1916 log_addr_offset, 8, ACPI_BUILD_TPMLOG_FILE, 0); 1917 1918 acpi_table_end(linker, &table); 1919 } 1920 #endif 1921 1922 #define HOLE_640K_START (640 * KiB) 1923 #define HOLE_640K_END (1 * MiB) 1924 1925 /* 1926 * ACPI spec, Revision 3.0 1927 * 5.2.15 System Resource Affinity Table (SRAT) 1928 */ 1929 static void 1930 build_srat(GArray *table_data, BIOSLinker *linker, MachineState *machine) 1931 { 1932 int i; 1933 int numa_mem_start, slots; 1934 uint64_t mem_len, mem_base, next_base; 1935 MachineClass *mc = MACHINE_GET_CLASS(machine); 1936 X86MachineState *x86ms = X86_MACHINE(machine); 1937 const CPUArchIdList *apic_ids = mc->possible_cpu_arch_ids(machine); 1938 PCMachineState *pcms = PC_MACHINE(machine); 1939 int nb_numa_nodes = machine->numa_state->num_nodes; 1940 NodeInfo *numa_info = machine->numa_state->nodes; 1941 ram_addr_t hotpluggable_address_space_size = 1942 object_property_get_int(OBJECT(pcms), PC_MACHINE_DEVMEM_REGION_SIZE, 1943 NULL); 1944 AcpiTable table = { .sig = "SRAT", .rev = 1, .oem_id = x86ms->oem_id, 1945 .oem_table_id = x86ms->oem_table_id }; 1946 1947 acpi_table_begin(&table, table_data); 1948 build_append_int_noprefix(table_data, 1, 4); /* Reserved */ 1949 build_append_int_noprefix(table_data, 0, 8); /* Reserved */ 1950 1951 for (i = 0; i < apic_ids->len; i++) { 1952 int node_id = apic_ids->cpus[i].props.node_id; 1953 uint32_t apic_id = apic_ids->cpus[i].arch_id; 1954 1955 if (apic_id < 255) { 1956 /* 5.2.15.1 Processor Local APIC/SAPIC Affinity Structure */ 1957 build_append_int_noprefix(table_data, 0, 1); /* Type */ 1958 build_append_int_noprefix(table_data, 16, 1); /* Length */ 1959 /* Proximity Domain [7:0] */ 1960 build_append_int_noprefix(table_data, node_id, 1); 1961 build_append_int_noprefix(table_data, apic_id, 1); /* APIC ID */ 1962 /* Flags, Table 5-36 */ 1963 build_append_int_noprefix(table_data, 1, 4); 1964 build_append_int_noprefix(table_data, 0, 1); /* Local SAPIC EID */ 1965 /* Proximity Domain [31:8] */ 1966 build_append_int_noprefix(table_data, 0, 3); 1967 build_append_int_noprefix(table_data, 0, 4); /* Reserved */ 1968 } else { 1969 /* 1970 * ACPI spec, Revision 4.0 1971 * 5.2.16.3 Processor Local x2APIC Affinity Structure 1972 */ 1973 build_append_int_noprefix(table_data, 2, 1); /* Type */ 1974 build_append_int_noprefix(table_data, 24, 1); /* Length */ 1975 build_append_int_noprefix(table_data, 0, 2); /* Reserved */ 1976 /* Proximity Domain */ 1977 build_append_int_noprefix(table_data, node_id, 4); 1978 build_append_int_noprefix(table_data, apic_id, 4); /* X2APIC ID */ 1979 /* Flags, Table 5-39 */ 1980 build_append_int_noprefix(table_data, 1 /* Enabled */, 4); 1981 build_append_int_noprefix(table_data, 0, 4); /* Clock Domain */ 1982 build_append_int_noprefix(table_data, 0, 4); /* Reserved */ 1983 } 1984 } 1985 1986 /* the memory map is a bit tricky, it contains at least one hole 1987 * from 640k-1M and possibly another one from 3.5G-4G. 1988 */ 1989 next_base = 0; 1990 numa_mem_start = table_data->len; 1991 1992 for (i = 1; i < nb_numa_nodes + 1; ++i) { 1993 mem_base = next_base; 1994 mem_len = numa_info[i - 1].node_mem; 1995 next_base = mem_base + mem_len; 1996 1997 /* Cut out the 640K hole */ 1998 if (mem_base <= HOLE_640K_START && 1999 next_base > HOLE_640K_START) { 2000 mem_len -= next_base - HOLE_640K_START; 2001 if (mem_len > 0) { 2002 build_srat_memory(table_data, mem_base, mem_len, i - 1, 2003 MEM_AFFINITY_ENABLED); 2004 } 2005 2006 /* Check for the rare case: 640K < RAM < 1M */ 2007 if (next_base <= HOLE_640K_END) { 2008 next_base = HOLE_640K_END; 2009 continue; 2010 } 2011 mem_base = HOLE_640K_END; 2012 mem_len = next_base - HOLE_640K_END; 2013 } 2014 2015 /* Cut out the ACPI_PCI hole */ 2016 if (mem_base <= x86ms->below_4g_mem_size && 2017 next_base > x86ms->below_4g_mem_size) { 2018 mem_len -= next_base - x86ms->below_4g_mem_size; 2019 if (mem_len > 0) { 2020 build_srat_memory(table_data, mem_base, mem_len, i - 1, 2021 MEM_AFFINITY_ENABLED); 2022 } 2023 mem_base = x86ms->above_4g_mem_start; 2024 mem_len = next_base - x86ms->below_4g_mem_size; 2025 next_base = mem_base + mem_len; 2026 } 2027 2028 if (mem_len > 0) { 2029 build_srat_memory(table_data, mem_base, mem_len, i - 1, 2030 MEM_AFFINITY_ENABLED); 2031 } 2032 } 2033 2034 if (machine->nvdimms_state->is_enabled) { 2035 nvdimm_build_srat(table_data); 2036 } 2037 2038 sgx_epc_build_srat(table_data); 2039 2040 /* 2041 * TODO: this part is not in ACPI spec and current linux kernel boots fine 2042 * without these entries. But I recall there were issues the last time I 2043 * tried to remove it with some ancient guest OS, however I can't remember 2044 * what that was so keep this around for now 2045 */ 2046 slots = (table_data->len - numa_mem_start) / 40 /* mem affinity len */; 2047 for (; slots < nb_numa_nodes + 2; slots++) { 2048 build_srat_memory(table_data, 0, 0, 0, MEM_AFFINITY_NOFLAGS); 2049 } 2050 2051 /* 2052 * Entry is required for Windows to enable memory hotplug in OS 2053 * and for Linux to enable SWIOTLB when booted with less than 2054 * 4G of RAM. Windows works better if the entry sets proximity 2055 * to the highest NUMA node in the machine. 2056 * Memory devices may override proximity set by this entry, 2057 * providing _PXM method if necessary. 2058 */ 2059 if (hotpluggable_address_space_size) { 2060 build_srat_memory(table_data, machine->device_memory->base, 2061 hotpluggable_address_space_size, nb_numa_nodes - 1, 2062 MEM_AFFINITY_HOTPLUGGABLE | MEM_AFFINITY_ENABLED); 2063 } 2064 2065 acpi_table_end(linker, &table); 2066 } 2067 2068 /* 2069 * Insert DMAR scope for PCI bridges and endpoint devcie 2070 */ 2071 static void 2072 insert_scope(PCIBus *bus, PCIDevice *dev, void *opaque) 2073 { 2074 const size_t device_scope_size = 6 /* device scope structure */ + 2075 2 /* 1 path entry */; 2076 GArray *scope_blob = opaque; 2077 2078 if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_BRIDGE)) { 2079 /* Dmar Scope Type: 0x02 for PCI Bridge */ 2080 build_append_int_noprefix(scope_blob, 0x02, 1); 2081 } else { 2082 /* Dmar Scope Type: 0x01 for PCI Endpoint Device */ 2083 build_append_int_noprefix(scope_blob, 0x01, 1); 2084 } 2085 2086 /* length */ 2087 build_append_int_noprefix(scope_blob, device_scope_size, 1); 2088 /* reserved */ 2089 build_append_int_noprefix(scope_blob, 0, 2); 2090 /* enumeration_id */ 2091 build_append_int_noprefix(scope_blob, 0, 1); 2092 /* bus */ 2093 build_append_int_noprefix(scope_blob, pci_bus_num(bus), 1); 2094 /* device */ 2095 build_append_int_noprefix(scope_blob, PCI_SLOT(dev->devfn), 1); 2096 /* function */ 2097 build_append_int_noprefix(scope_blob, PCI_FUNC(dev->devfn), 1); 2098 } 2099 2100 /* For a given PCI host bridge, walk and insert DMAR scope */ 2101 static int 2102 dmar_host_bridges(Object *obj, void *opaque) 2103 { 2104 GArray *scope_blob = opaque; 2105 2106 if (object_dynamic_cast(obj, TYPE_PCI_HOST_BRIDGE)) { 2107 PCIBus *bus = PCI_HOST_BRIDGE(obj)->bus; 2108 2109 if (bus && !pci_bus_bypass_iommu(bus)) { 2110 pci_for_each_device_under_bus(bus, insert_scope, scope_blob); 2111 } 2112 } 2113 2114 return 0; 2115 } 2116 2117 /* 2118 * Intel ® Virtualization Technology for Directed I/O 2119 * Architecture Specification. Revision 3.3 2120 * 8.1 DMA Remapping Reporting Structure 2121 */ 2122 static void 2123 build_dmar_q35(GArray *table_data, BIOSLinker *linker, const char *oem_id, 2124 const char *oem_table_id) 2125 { 2126 uint8_t dmar_flags = 0; 2127 uint8_t rsvd10[10] = {}; 2128 /* Root complex IOAPIC uses one path only */ 2129 const size_t ioapic_scope_size = 6 /* device scope structure */ + 2130 2 /* 1 path entry */; 2131 X86IOMMUState *iommu = x86_iommu_get_default(); 2132 IntelIOMMUState *intel_iommu = INTEL_IOMMU_DEVICE(iommu); 2133 GArray *scope_blob = g_array_new(false, true, 1); 2134 2135 AcpiTable table = { .sig = "DMAR", .rev = 1, .oem_id = oem_id, 2136 .oem_table_id = oem_table_id }; 2137 2138 /* 2139 * A PCI bus walk, for each PCI host bridge. 2140 * Insert scope for each PCI bridge and endpoint device which 2141 * is attached to a bus with iommu enabled. 2142 */ 2143 object_child_foreach_recursive(object_get_root(), 2144 dmar_host_bridges, scope_blob); 2145 2146 assert(iommu); 2147 if (x86_iommu_ir_supported(iommu)) { 2148 dmar_flags |= 0x1; /* Flags: 0x1: INT_REMAP */ 2149 } 2150 2151 acpi_table_begin(&table, table_data); 2152 /* Host Address Width */ 2153 build_append_int_noprefix(table_data, intel_iommu->aw_bits - 1, 1); 2154 build_append_int_noprefix(table_data, dmar_flags, 1); /* Flags */ 2155 g_array_append_vals(table_data, rsvd10, sizeof(rsvd10)); /* Reserved */ 2156 2157 /* 8.3 DMAR Remapping Hardware Unit Definition structure */ 2158 build_append_int_noprefix(table_data, 0, 2); /* Type */ 2159 /* Length */ 2160 build_append_int_noprefix(table_data, 2161 16 + ioapic_scope_size + scope_blob->len, 2); 2162 /* Flags */ 2163 build_append_int_noprefix(table_data, 0 /* Don't include all pci device */ , 2164 1); 2165 build_append_int_noprefix(table_data, 0 , 1); /* Reserved */ 2166 build_append_int_noprefix(table_data, 0 , 2); /* Segment Number */ 2167 /* Register Base Address */ 2168 build_append_int_noprefix(table_data, Q35_HOST_BRIDGE_IOMMU_ADDR , 8); 2169 2170 /* Scope definition for the root-complex IOAPIC. See VT-d spec 2171 * 8.3.1 (version Oct. 2014 or later). */ 2172 build_append_int_noprefix(table_data, 0x03 /* IOAPIC */, 1); /* Type */ 2173 build_append_int_noprefix(table_data, ioapic_scope_size, 1); /* Length */ 2174 build_append_int_noprefix(table_data, 0, 2); /* Reserved */ 2175 /* Enumeration ID */ 2176 build_append_int_noprefix(table_data, ACPI_BUILD_IOAPIC_ID, 1); 2177 /* Start Bus Number */ 2178 build_append_int_noprefix(table_data, Q35_PSEUDO_BUS_PLATFORM, 1); 2179 /* Path, {Device, Function} pair */ 2180 build_append_int_noprefix(table_data, PCI_SLOT(Q35_PSEUDO_DEVFN_IOAPIC), 1); 2181 build_append_int_noprefix(table_data, PCI_FUNC(Q35_PSEUDO_DEVFN_IOAPIC), 1); 2182 2183 /* Add scope found above */ 2184 g_array_append_vals(table_data, scope_blob->data, scope_blob->len); 2185 g_array_free(scope_blob, true); 2186 2187 if (iommu->dt_supported) { 2188 /* 8.5 Root Port ATS Capability Reporting Structure */ 2189 build_append_int_noprefix(table_data, 2, 2); /* Type */ 2190 build_append_int_noprefix(table_data, 8, 2); /* Length */ 2191 build_append_int_noprefix(table_data, 1 /* ALL_PORTS */, 1); /* Flags */ 2192 build_append_int_noprefix(table_data, 0, 1); /* Reserved */ 2193 build_append_int_noprefix(table_data, 0, 2); /* Segment Number */ 2194 } 2195 2196 acpi_table_end(linker, &table); 2197 } 2198 2199 /* 2200 * Windows ACPI Emulated Devices Table 2201 * (Version 1.0 - April 6, 2009) 2202 * Spec: http://download.microsoft.com/download/7/E/7/7E7662CF-CBEA-470B-A97E-CE7CE0D98DC2/WAET.docx 2203 * 2204 * Helpful to speedup Windows guests and ignored by others. 2205 */ 2206 static void 2207 build_waet(GArray *table_data, BIOSLinker *linker, const char *oem_id, 2208 const char *oem_table_id) 2209 { 2210 AcpiTable table = { .sig = "WAET", .rev = 1, .oem_id = oem_id, 2211 .oem_table_id = oem_table_id }; 2212 2213 acpi_table_begin(&table, table_data); 2214 /* 2215 * Set "ACPI PM timer good" flag. 2216 * 2217 * Tells Windows guests that our ACPI PM timer is reliable in the 2218 * sense that guest can read it only once to obtain a reliable value. 2219 * Which avoids costly VMExits caused by guest re-reading it unnecessarily. 2220 */ 2221 build_append_int_noprefix(table_data, 1 << 1 /* ACPI PM timer good */, 4); 2222 acpi_table_end(linker, &table); 2223 } 2224 2225 /* 2226 * IVRS table as specified in AMD IOMMU Specification v2.62, Section 5.2 2227 * accessible here http://support.amd.com/TechDocs/48882_IOMMU.pdf 2228 */ 2229 #define IOAPIC_SB_DEVID (uint64_t)PCI_BUILD_BDF(0, PCI_DEVFN(0x14, 0)) 2230 2231 /* 2232 * Insert IVHD entry for device and recurse, insert alias, or insert range as 2233 * necessary for the PCI topology. 2234 */ 2235 static void 2236 insert_ivhd(PCIBus *bus, PCIDevice *dev, void *opaque) 2237 { 2238 GArray *table_data = opaque; 2239 uint32_t entry; 2240 2241 /* "Select" IVHD entry, type 0x2 */ 2242 entry = PCI_BUILD_BDF(pci_bus_num(bus), dev->devfn) << 8 | 0x2; 2243 build_append_int_noprefix(table_data, entry, 4); 2244 2245 if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_BRIDGE)) { 2246 PCIBus *sec_bus = pci_bridge_get_sec_bus(PCI_BRIDGE(dev)); 2247 uint8_t sec = pci_bus_num(sec_bus); 2248 uint8_t sub = dev->config[PCI_SUBORDINATE_BUS]; 2249 2250 if (pci_bus_is_express(sec_bus)) { 2251 /* 2252 * Walk the bus if there are subordinates, otherwise use a range 2253 * to cover an entire leaf bus. We could potentially also use a 2254 * range for traversed buses, but we'd need to take care not to 2255 * create both Select and Range entries covering the same device. 2256 * This is easier and potentially more compact. 2257 * 2258 * An example bare metal system seems to use Select entries for 2259 * root ports without a slot (ie. built-ins) and Range entries 2260 * when there is a slot. The same system also only hard-codes 2261 * the alias range for an onboard PCIe-to-PCI bridge, apparently 2262 * making no effort to support nested bridges. We attempt to 2263 * be more thorough here. 2264 */ 2265 if (sec == sub) { /* leaf bus */ 2266 /* "Start of Range" IVHD entry, type 0x3 */ 2267 entry = PCI_BUILD_BDF(sec, PCI_DEVFN(0, 0)) << 8 | 0x3; 2268 build_append_int_noprefix(table_data, entry, 4); 2269 /* "End of Range" IVHD entry, type 0x4 */ 2270 entry = PCI_BUILD_BDF(sub, PCI_DEVFN(31, 7)) << 8 | 0x4; 2271 build_append_int_noprefix(table_data, entry, 4); 2272 } else { 2273 pci_for_each_device(sec_bus, sec, insert_ivhd, table_data); 2274 } 2275 } else { 2276 /* 2277 * If the secondary bus is conventional, then we need to create an 2278 * Alias range for everything downstream. The range covers the 2279 * first devfn on the secondary bus to the last devfn on the 2280 * subordinate bus. The alias target depends on legacy versus 2281 * express bridges, just as in pci_device_iommu_address_space(). 2282 * DeviceIDa vs DeviceIDb as per the AMD IOMMU spec. 2283 */ 2284 uint16_t dev_id_a, dev_id_b; 2285 2286 dev_id_a = PCI_BUILD_BDF(sec, PCI_DEVFN(0, 0)); 2287 2288 if (pci_is_express(dev) && 2289 pcie_cap_get_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE) { 2290 dev_id_b = dev_id_a; 2291 } else { 2292 dev_id_b = PCI_BUILD_BDF(pci_bus_num(bus), dev->devfn); 2293 } 2294 2295 /* "Alias Start of Range" IVHD entry, type 0x43, 8 bytes */ 2296 build_append_int_noprefix(table_data, dev_id_a << 8 | 0x43, 4); 2297 build_append_int_noprefix(table_data, dev_id_b << 8 | 0x0, 4); 2298 2299 /* "End of Range" IVHD entry, type 0x4 */ 2300 entry = PCI_BUILD_BDF(sub, PCI_DEVFN(31, 7)) << 8 | 0x4; 2301 build_append_int_noprefix(table_data, entry, 4); 2302 } 2303 } 2304 } 2305 2306 /* For all PCI host bridges, walk and insert IVHD entries */ 2307 static int 2308 ivrs_host_bridges(Object *obj, void *opaque) 2309 { 2310 GArray *ivhd_blob = opaque; 2311 2312 if (object_dynamic_cast(obj, TYPE_PCI_HOST_BRIDGE)) { 2313 PCIBus *bus = PCI_HOST_BRIDGE(obj)->bus; 2314 2315 if (bus && !pci_bus_bypass_iommu(bus)) { 2316 pci_for_each_device_under_bus(bus, insert_ivhd, ivhd_blob); 2317 } 2318 } 2319 2320 return 0; 2321 } 2322 2323 static void 2324 build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id, 2325 const char *oem_table_id) 2326 { 2327 int ivhd_table_len = 24; 2328 AMDVIState *s = AMD_IOMMU_DEVICE(x86_iommu_get_default()); 2329 GArray *ivhd_blob = g_array_new(false, true, 1); 2330 AcpiTable table = { .sig = "IVRS", .rev = 1, .oem_id = oem_id, 2331 .oem_table_id = oem_table_id }; 2332 2333 acpi_table_begin(&table, table_data); 2334 /* IVinfo - IO virtualization information common to all 2335 * IOMMU units in a system 2336 */ 2337 build_append_int_noprefix(table_data, 40UL << 8/* PASize */, 4); 2338 /* reserved */ 2339 build_append_int_noprefix(table_data, 0, 8); 2340 2341 /* IVHD definition - type 10h */ 2342 build_append_int_noprefix(table_data, 0x10, 1); 2343 /* virtualization flags */ 2344 build_append_int_noprefix(table_data, 2345 (1UL << 0) | /* HtTunEn */ 2346 (1UL << 4) | /* iotblSup */ 2347 (1UL << 6) | /* PrefSup */ 2348 (1UL << 7), /* PPRSup */ 2349 1); 2350 2351 /* 2352 * A PCI bus walk, for each PCI host bridge, is necessary to create a 2353 * complete set of IVHD entries. Do this into a separate blob so that we 2354 * can calculate the total IVRS table length here and then append the new 2355 * blob further below. Fall back to an entry covering all devices, which 2356 * is sufficient when no aliases are present. 2357 */ 2358 object_child_foreach_recursive(object_get_root(), 2359 ivrs_host_bridges, ivhd_blob); 2360 2361 if (!ivhd_blob->len) { 2362 /* 2363 * Type 1 device entry reporting all devices 2364 * These are 4-byte device entries currently reporting the range of 2365 * Refer to Spec - Table 95:IVHD Device Entry Type Codes(4-byte) 2366 */ 2367 build_append_int_noprefix(ivhd_blob, 0x0000001, 4); 2368 } 2369 2370 ivhd_table_len += ivhd_blob->len; 2371 2372 /* 2373 * When interrupt remapping is supported, we add a special IVHD device 2374 * for type IO-APIC. 2375 */ 2376 if (x86_iommu_ir_supported(x86_iommu_get_default())) { 2377 ivhd_table_len += 8; 2378 } 2379 2380 /* IVHD length */ 2381 build_append_int_noprefix(table_data, ivhd_table_len, 2); 2382 /* DeviceID */ 2383 build_append_int_noprefix(table_data, s->devid, 2); 2384 /* Capability offset */ 2385 build_append_int_noprefix(table_data, s->capab_offset, 2); 2386 /* IOMMU base address */ 2387 build_append_int_noprefix(table_data, s->mmio.addr, 8); 2388 /* PCI Segment Group */ 2389 build_append_int_noprefix(table_data, 0, 2); 2390 /* IOMMU info */ 2391 build_append_int_noprefix(table_data, 0, 2); 2392 /* IOMMU Feature Reporting */ 2393 build_append_int_noprefix(table_data, 2394 (48UL << 30) | /* HATS */ 2395 (48UL << 28) | /* GATS */ 2396 (1UL << 2) | /* GTSup */ 2397 (1UL << 6), /* GASup */ 2398 4); 2399 2400 /* IVHD entries as found above */ 2401 g_array_append_vals(table_data, ivhd_blob->data, ivhd_blob->len); 2402 g_array_free(ivhd_blob, TRUE); 2403 2404 /* 2405 * Add a special IVHD device type. 2406 * Refer to spec - Table 95: IVHD device entry type codes 2407 * 2408 * Linux IOMMU driver checks for the special IVHD device (type IO-APIC). 2409 * See Linux kernel commit 'c2ff5cf5294bcbd7fa50f7d860e90a66db7e5059' 2410 */ 2411 if (x86_iommu_ir_supported(x86_iommu_get_default())) { 2412 build_append_int_noprefix(table_data, 2413 (0x1ull << 56) | /* type IOAPIC */ 2414 (IOAPIC_SB_DEVID << 40) | /* IOAPIC devid */ 2415 0x48, /* special device */ 2416 8); 2417 } 2418 acpi_table_end(linker, &table); 2419 } 2420 2421 typedef 2422 struct AcpiBuildState { 2423 /* Copy of table in RAM (for patching). */ 2424 MemoryRegion *table_mr; 2425 /* Is table patched? */ 2426 uint8_t patched; 2427 void *rsdp; 2428 MemoryRegion *rsdp_mr; 2429 MemoryRegion *linker_mr; 2430 } AcpiBuildState; 2431 2432 static bool acpi_get_mcfg(AcpiMcfgInfo *mcfg) 2433 { 2434 Object *pci_host; 2435 QObject *o; 2436 2437 pci_host = acpi_get_i386_pci_host(); 2438 if (!pci_host) { 2439 return false; 2440 } 2441 2442 o = object_property_get_qobject(pci_host, PCIE_HOST_MCFG_BASE, NULL); 2443 if (!o) { 2444 return false; 2445 } 2446 mcfg->base = qnum_get_uint(qobject_to(QNum, o)); 2447 qobject_unref(o); 2448 if (mcfg->base == PCIE_BASE_ADDR_UNMAPPED) { 2449 return false; 2450 } 2451 2452 o = object_property_get_qobject(pci_host, PCIE_HOST_MCFG_SIZE, NULL); 2453 assert(o); 2454 mcfg->size = qnum_get_uint(qobject_to(QNum, o)); 2455 qobject_unref(o); 2456 return true; 2457 } 2458 2459 static 2460 void acpi_build(AcpiBuildTables *tables, MachineState *machine) 2461 { 2462 PCMachineState *pcms = PC_MACHINE(machine); 2463 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms); 2464 X86MachineState *x86ms = X86_MACHINE(machine); 2465 DeviceState *iommu = pcms->iommu; 2466 GArray *table_offsets; 2467 unsigned facs, dsdt, rsdt, fadt; 2468 AcpiPmInfo pm; 2469 AcpiMiscInfo misc; 2470 AcpiMcfgInfo mcfg; 2471 Range pci_hole = {}, pci_hole64 = {}; 2472 uint8_t *u; 2473 size_t aml_len = 0; 2474 GArray *tables_blob = tables->table_data; 2475 AcpiSlicOem slic_oem = { .id = NULL, .table_id = NULL }; 2476 Object *vmgenid_dev; 2477 char *oem_id; 2478 char *oem_table_id; 2479 2480 acpi_get_pm_info(machine, &pm); 2481 acpi_get_misc_info(&misc); 2482 acpi_get_pci_holes(&pci_hole, &pci_hole64); 2483 acpi_get_slic_oem(&slic_oem); 2484 2485 if (slic_oem.id) { 2486 oem_id = slic_oem.id; 2487 } else { 2488 oem_id = x86ms->oem_id; 2489 } 2490 2491 if (slic_oem.table_id) { 2492 oem_table_id = slic_oem.table_id; 2493 } else { 2494 oem_table_id = x86ms->oem_table_id; 2495 } 2496 2497 table_offsets = g_array_new(false, true /* clear */, 2498 sizeof(uint32_t)); 2499 ACPI_BUILD_DPRINTF("init ACPI tables\n"); 2500 2501 bios_linker_loader_alloc(tables->linker, 2502 ACPI_BUILD_TABLE_FILE, tables_blob, 2503 64 /* Ensure FACS is aligned */, 2504 false /* high memory */); 2505 2506 /* 2507 * FACS is pointed to by FADT. 2508 * We place it first since it's the only table that has alignment 2509 * requirements. 2510 */ 2511 facs = tables_blob->len; 2512 build_facs(tables_blob); 2513 2514 /* DSDT is pointed to by FADT */ 2515 dsdt = tables_blob->len; 2516 build_dsdt(tables_blob, tables->linker, &pm, &misc, 2517 &pci_hole, &pci_hole64, machine); 2518 2519 /* Count the size of the DSDT and SSDT, we will need it for legacy 2520 * sizing of ACPI tables. 2521 */ 2522 aml_len += tables_blob->len - dsdt; 2523 2524 /* ACPI tables pointed to by RSDT */ 2525 fadt = tables_blob->len; 2526 acpi_add_table(table_offsets, tables_blob); 2527 pm.fadt.facs_tbl_offset = &facs; 2528 pm.fadt.dsdt_tbl_offset = &dsdt; 2529 pm.fadt.xdsdt_tbl_offset = &dsdt; 2530 build_fadt(tables_blob, tables->linker, &pm.fadt, oem_id, oem_table_id); 2531 aml_len += tables_blob->len - fadt; 2532 2533 acpi_add_table(table_offsets, tables_blob); 2534 acpi_build_madt(tables_blob, tables->linker, x86ms, 2535 ACPI_DEVICE_IF(x86ms->acpi_dev), x86ms->oem_id, 2536 x86ms->oem_table_id); 2537 2538 #ifdef CONFIG_ACPI_ERST 2539 { 2540 Object *erst_dev; 2541 erst_dev = find_erst_dev(); 2542 if (erst_dev) { 2543 acpi_add_table(table_offsets, tables_blob); 2544 build_erst(tables_blob, tables->linker, erst_dev, 2545 x86ms->oem_id, x86ms->oem_table_id); 2546 } 2547 } 2548 #endif 2549 2550 vmgenid_dev = find_vmgenid_dev(); 2551 if (vmgenid_dev) { 2552 acpi_add_table(table_offsets, tables_blob); 2553 vmgenid_build_acpi(VMGENID(vmgenid_dev), tables_blob, 2554 tables->vmgenid, tables->linker, x86ms->oem_id); 2555 } 2556 2557 if (misc.has_hpet) { 2558 acpi_add_table(table_offsets, tables_blob); 2559 build_hpet(tables_blob, tables->linker, x86ms->oem_id, 2560 x86ms->oem_table_id); 2561 } 2562 #ifdef CONFIG_TPM 2563 if (misc.tpm_version != TPM_VERSION_UNSPEC) { 2564 if (misc.tpm_version == TPM_VERSION_1_2) { 2565 acpi_add_table(table_offsets, tables_blob); 2566 build_tpm_tcpa(tables_blob, tables->linker, tables->tcpalog, 2567 x86ms->oem_id, x86ms->oem_table_id); 2568 } else { /* TPM_VERSION_2_0 */ 2569 acpi_add_table(table_offsets, tables_blob); 2570 build_tpm2(tables_blob, tables->linker, tables->tcpalog, 2571 x86ms->oem_id, x86ms->oem_table_id); 2572 } 2573 } 2574 #endif 2575 if (machine->numa_state->num_nodes) { 2576 acpi_add_table(table_offsets, tables_blob); 2577 build_srat(tables_blob, tables->linker, machine); 2578 if (machine->numa_state->have_numa_distance) { 2579 acpi_add_table(table_offsets, tables_blob); 2580 build_slit(tables_blob, tables->linker, machine, x86ms->oem_id, 2581 x86ms->oem_table_id); 2582 } 2583 if (machine->numa_state->hmat_enabled) { 2584 acpi_add_table(table_offsets, tables_blob); 2585 build_hmat(tables_blob, tables->linker, machine->numa_state, 2586 x86ms->oem_id, x86ms->oem_table_id); 2587 } 2588 } 2589 if (acpi_get_mcfg(&mcfg)) { 2590 acpi_add_table(table_offsets, tables_blob); 2591 build_mcfg(tables_blob, tables->linker, &mcfg, x86ms->oem_id, 2592 x86ms->oem_table_id); 2593 } 2594 if (object_dynamic_cast(OBJECT(iommu), TYPE_AMD_IOMMU_DEVICE)) { 2595 acpi_add_table(table_offsets, tables_blob); 2596 build_amd_iommu(tables_blob, tables->linker, x86ms->oem_id, 2597 x86ms->oem_table_id); 2598 } else if (object_dynamic_cast(OBJECT(iommu), TYPE_INTEL_IOMMU_DEVICE)) { 2599 acpi_add_table(table_offsets, tables_blob); 2600 build_dmar_q35(tables_blob, tables->linker, x86ms->oem_id, 2601 x86ms->oem_table_id); 2602 } else if (object_dynamic_cast(OBJECT(iommu), TYPE_VIRTIO_IOMMU_PCI)) { 2603 PCIDevice *pdev = PCI_DEVICE(iommu); 2604 2605 acpi_add_table(table_offsets, tables_blob); 2606 build_viot(machine, tables_blob, tables->linker, pci_get_bdf(pdev), 2607 x86ms->oem_id, x86ms->oem_table_id); 2608 } 2609 if (machine->nvdimms_state->is_enabled) { 2610 nvdimm_build_acpi(table_offsets, tables_blob, tables->linker, 2611 machine->nvdimms_state, machine->ram_slots, 2612 x86ms->oem_id, x86ms->oem_table_id); 2613 } 2614 if (pcms->cxl_devices_state.is_enabled) { 2615 cxl_build_cedt(table_offsets, tables_blob, tables->linker, 2616 x86ms->oem_id, x86ms->oem_table_id, &pcms->cxl_devices_state); 2617 } 2618 2619 acpi_add_table(table_offsets, tables_blob); 2620 build_waet(tables_blob, tables->linker, x86ms->oem_id, x86ms->oem_table_id); 2621 2622 /* Add tables supplied by user (if any) */ 2623 for (u = acpi_table_first(); u; u = acpi_table_next(u)) { 2624 unsigned len = acpi_table_len(u); 2625 2626 acpi_add_table(table_offsets, tables_blob); 2627 g_array_append_vals(tables_blob, u, len); 2628 } 2629 2630 /* RSDT is pointed to by RSDP */ 2631 rsdt = tables_blob->len; 2632 build_rsdt(tables_blob, tables->linker, table_offsets, 2633 oem_id, oem_table_id); 2634 2635 /* RSDP is in FSEG memory, so allocate it separately */ 2636 { 2637 AcpiRsdpData rsdp_data = { 2638 .revision = 0, 2639 .oem_id = x86ms->oem_id, 2640 .xsdt_tbl_offset = NULL, 2641 .rsdt_tbl_offset = &rsdt, 2642 }; 2643 build_rsdp(tables->rsdp, tables->linker, &rsdp_data); 2644 if (!pcmc->rsdp_in_ram) { 2645 /* We used to allocate some extra space for RSDP revision 2 but 2646 * only used the RSDP revision 0 space. The extra bytes were 2647 * zeroed out and not used. 2648 * Here we continue wasting those extra 16 bytes to make sure we 2649 * don't break migration for machine types 2.2 and older due to 2650 * RSDP blob size mismatch. 2651 */ 2652 build_append_int_noprefix(tables->rsdp, 0, 16); 2653 } 2654 } 2655 2656 /* We'll expose it all to Guest so we want to reduce 2657 * chance of size changes. 2658 * 2659 * We used to align the tables to 4k, but of course this would 2660 * too simple to be enough. 4k turned out to be too small an 2661 * alignment very soon, and in fact it is almost impossible to 2662 * keep the table size stable for all (max_cpus, max_memory_slots) 2663 * combinations. So the table size is always 64k for pc-i440fx-2.1 2664 * and we give an error if the table grows beyond that limit. 2665 * 2666 * We still have the problem of migrating from "-M pc-i440fx-2.0". For 2667 * that, we exploit the fact that QEMU 2.1 generates _smaller_ tables 2668 * than 2.0 and we can always pad the smaller tables with zeros. We can 2669 * then use the exact size of the 2.0 tables. 2670 * 2671 * All this is for PIIX4, since QEMU 2.0 didn't support Q35 migration. 2672 */ 2673 if (pcmc->legacy_acpi_table_size) { 2674 /* Subtracting aml_len gives the size of fixed tables. Then add the 2675 * size of the PIIX4 DSDT/SSDT in QEMU 2.0. 2676 */ 2677 int legacy_aml_len = 2678 pcmc->legacy_acpi_table_size + 2679 ACPI_BUILD_LEGACY_CPU_AML_SIZE * x86ms->apic_id_limit; 2680 int legacy_table_size = 2681 ROUND_UP(tables_blob->len - aml_len + legacy_aml_len, 2682 ACPI_BUILD_ALIGN_SIZE); 2683 if (tables_blob->len > legacy_table_size) { 2684 /* Should happen only with PCI bridges and -M pc-i440fx-2.0. */ 2685 warn_report("ACPI table size %u exceeds %d bytes," 2686 " migration may not work", 2687 tables_blob->len, legacy_table_size); 2688 error_printf("Try removing CPUs, NUMA nodes, memory slots" 2689 " or PCI bridges."); 2690 } 2691 g_array_set_size(tables_blob, legacy_table_size); 2692 } else { 2693 /* Make sure we have a buffer in case we need to resize the tables. */ 2694 if (tables_blob->len > ACPI_BUILD_TABLE_SIZE / 2) { 2695 /* As of QEMU 2.1, this fires with 160 VCPUs and 255 memory slots. */ 2696 warn_report("ACPI table size %u exceeds %d bytes," 2697 " migration may not work", 2698 tables_blob->len, ACPI_BUILD_TABLE_SIZE / 2); 2699 error_printf("Try removing CPUs, NUMA nodes, memory slots" 2700 " or PCI bridges."); 2701 } 2702 acpi_align_size(tables_blob, ACPI_BUILD_TABLE_SIZE); 2703 } 2704 2705 acpi_align_size(tables->linker->cmd_blob, ACPI_BUILD_ALIGN_SIZE); 2706 2707 /* Cleanup memory that's no longer used. */ 2708 g_array_free(table_offsets, true); 2709 g_free(slic_oem.id); 2710 g_free(slic_oem.table_id); 2711 } 2712 2713 static void acpi_ram_update(MemoryRegion *mr, GArray *data) 2714 { 2715 uint32_t size = acpi_data_len(data); 2716 2717 /* Make sure RAM size is correct - in case it got changed e.g. by migration */ 2718 memory_region_ram_resize(mr, size, &error_abort); 2719 2720 memcpy(memory_region_get_ram_ptr(mr), data->data, size); 2721 memory_region_set_dirty(mr, 0, size); 2722 } 2723 2724 static void acpi_build_update(void *build_opaque) 2725 { 2726 AcpiBuildState *build_state = build_opaque; 2727 AcpiBuildTables tables; 2728 2729 /* No state to update or already patched? Nothing to do. */ 2730 if (!build_state || build_state->patched) { 2731 return; 2732 } 2733 build_state->patched = 1; 2734 2735 acpi_build_tables_init(&tables); 2736 2737 acpi_build(&tables, MACHINE(qdev_get_machine())); 2738 2739 acpi_ram_update(build_state->table_mr, tables.table_data); 2740 2741 if (build_state->rsdp) { 2742 memcpy(build_state->rsdp, tables.rsdp->data, acpi_data_len(tables.rsdp)); 2743 } else { 2744 acpi_ram_update(build_state->rsdp_mr, tables.rsdp); 2745 } 2746 2747 acpi_ram_update(build_state->linker_mr, tables.linker->cmd_blob); 2748 acpi_build_tables_cleanup(&tables, true); 2749 } 2750 2751 static void acpi_build_reset(void *build_opaque) 2752 { 2753 AcpiBuildState *build_state = build_opaque; 2754 build_state->patched = 0; 2755 } 2756 2757 static const VMStateDescription vmstate_acpi_build = { 2758 .name = "acpi_build", 2759 .version_id = 1, 2760 .minimum_version_id = 1, 2761 .fields = (VMStateField[]) { 2762 VMSTATE_UINT8(patched, AcpiBuildState), 2763 VMSTATE_END_OF_LIST() 2764 }, 2765 }; 2766 2767 void acpi_setup(void) 2768 { 2769 PCMachineState *pcms = PC_MACHINE(qdev_get_machine()); 2770 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms); 2771 X86MachineState *x86ms = X86_MACHINE(pcms); 2772 AcpiBuildTables tables; 2773 AcpiBuildState *build_state; 2774 Object *vmgenid_dev; 2775 #ifdef CONFIG_TPM 2776 TPMIf *tpm; 2777 static FwCfgTPMConfig tpm_config; 2778 #endif 2779 2780 if (!x86ms->fw_cfg) { 2781 ACPI_BUILD_DPRINTF("No fw cfg. Bailing out.\n"); 2782 return; 2783 } 2784 2785 if (!pcms->acpi_build_enabled) { 2786 ACPI_BUILD_DPRINTF("ACPI build disabled. Bailing out.\n"); 2787 return; 2788 } 2789 2790 if (!x86_machine_is_acpi_enabled(X86_MACHINE(pcms))) { 2791 ACPI_BUILD_DPRINTF("ACPI disabled. Bailing out.\n"); 2792 return; 2793 } 2794 2795 build_state = g_malloc0(sizeof *build_state); 2796 2797 acpi_build_tables_init(&tables); 2798 acpi_build(&tables, MACHINE(pcms)); 2799 2800 /* Now expose it all to Guest */ 2801 build_state->table_mr = acpi_add_rom_blob(acpi_build_update, 2802 build_state, tables.table_data, 2803 ACPI_BUILD_TABLE_FILE); 2804 assert(build_state->table_mr != NULL); 2805 2806 build_state->linker_mr = 2807 acpi_add_rom_blob(acpi_build_update, build_state, 2808 tables.linker->cmd_blob, ACPI_BUILD_LOADER_FILE); 2809 2810 #ifdef CONFIG_TPM 2811 fw_cfg_add_file(x86ms->fw_cfg, ACPI_BUILD_TPMLOG_FILE, 2812 tables.tcpalog->data, acpi_data_len(tables.tcpalog)); 2813 2814 tpm = tpm_find(); 2815 if (tpm && object_property_get_bool(OBJECT(tpm), "ppi", &error_abort)) { 2816 tpm_config = (FwCfgTPMConfig) { 2817 .tpmppi_address = cpu_to_le32(TPM_PPI_ADDR_BASE), 2818 .tpm_version = tpm_get_version(tpm), 2819 .tpmppi_version = TPM_PPI_VERSION_1_30 2820 }; 2821 fw_cfg_add_file(x86ms->fw_cfg, "etc/tpm/config", 2822 &tpm_config, sizeof tpm_config); 2823 } 2824 #endif 2825 2826 vmgenid_dev = find_vmgenid_dev(); 2827 if (vmgenid_dev) { 2828 vmgenid_add_fw_cfg(VMGENID(vmgenid_dev), x86ms->fw_cfg, 2829 tables.vmgenid); 2830 } 2831 2832 if (!pcmc->rsdp_in_ram) { 2833 /* 2834 * Keep for compatibility with old machine types. 2835 * Though RSDP is small, its contents isn't immutable, so 2836 * we'll update it along with the rest of tables on guest access. 2837 */ 2838 uint32_t rsdp_size = acpi_data_len(tables.rsdp); 2839 2840 build_state->rsdp = g_memdup(tables.rsdp->data, rsdp_size); 2841 fw_cfg_add_file_callback(x86ms->fw_cfg, ACPI_BUILD_RSDP_FILE, 2842 acpi_build_update, NULL, build_state, 2843 build_state->rsdp, rsdp_size, true); 2844 build_state->rsdp_mr = NULL; 2845 } else { 2846 build_state->rsdp = NULL; 2847 build_state->rsdp_mr = acpi_add_rom_blob(acpi_build_update, 2848 build_state, tables.rsdp, 2849 ACPI_BUILD_RSDP_FILE); 2850 } 2851 2852 qemu_register_reset(acpi_build_reset, build_state); 2853 acpi_build_reset(build_state); 2854 vmstate_register(NULL, 0, &vmstate_acpi_build, build_state); 2855 2856 /* Cleanup tables but don't free the memory: we track it 2857 * in build_state. 2858 */ 2859 acpi_build_tables_cleanup(&tables, false); 2860 } 2861