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