uboot/arch/x86/lib/acpi_table.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * Based on acpi.c from coreboot
   4 *
   5 * Copyright (C) 2015, Saket Sinha <saket.sinha89@gmail.com>
   6 * Copyright (C) 2016, Bin Meng <bmeng.cn@gmail.com>
   7 */
   8
   9#define LOG_CATEGORY LOGC_ACPI
  10
  11#include <common.h>
  12#include <bloblist.h>
  13#include <cpu.h>
  14#include <dm.h>
  15#include <log.h>
  16#include <dm/uclass-internal.h>
  17#include <mapmem.h>
  18#include <serial.h>
  19#include <version.h>
  20#include <acpi/acpigen.h>
  21#include <acpi/acpi_device.h>
  22#include <acpi/acpi_table.h>
  23#include <asm/acpi/global_nvs.h>
  24#include <asm/ioapic.h>
  25#include <asm/lapic.h>
  26#include <asm/mpspec.h>
  27#include <asm/tables.h>
  28#include <asm/arch/global_nvs.h>
  29#include <dm/acpi.h>
  30#include <linux/err.h>
  31
  32/*
  33 * IASL compiles the dsdt entries and writes the hex values
  34 * to a C array AmlCode[] (see dsdt.c).
  35 */
  36extern const unsigned char AmlCode[];
  37
  38/* ACPI RSDP address to be used in boot parameters */
  39static ulong acpi_rsdp_addr;
  40
  41static void acpi_create_facs(struct acpi_facs *facs)
  42{
  43        memset((void *)facs, 0, sizeof(struct acpi_facs));
  44
  45        memcpy(facs->signature, "FACS", 4);
  46        facs->length = sizeof(struct acpi_facs);
  47        facs->hardware_signature = 0;
  48        facs->firmware_waking_vector = 0;
  49        facs->global_lock = 0;
  50        facs->flags = 0;
  51        facs->x_firmware_waking_vector_l = 0;
  52        facs->x_firmware_waking_vector_h = 0;
  53        facs->version = 1;
  54}
  55
  56static int acpi_create_madt_lapic(struct acpi_madt_lapic *lapic,
  57                                  u8 cpu, u8 apic)
  58{
  59        lapic->type = ACPI_APIC_LAPIC;
  60        lapic->length = sizeof(struct acpi_madt_lapic);
  61        lapic->flags = LOCAL_APIC_FLAG_ENABLED;
  62        lapic->processor_id = cpu;
  63        lapic->apic_id = apic;
  64
  65        return lapic->length;
  66}
  67
  68int acpi_create_madt_lapics(u32 current)
  69{
  70        struct udevice *dev;
  71        int total_length = 0;
  72        int cpu_num = 0;
  73
  74        for (uclass_find_first_device(UCLASS_CPU, &dev);
  75             dev;
  76             uclass_find_next_device(&dev)) {
  77                struct cpu_platdata *plat = dev_get_parent_platdata(dev);
  78                int length;
  79
  80                length = acpi_create_madt_lapic(
  81                        (struct acpi_madt_lapic *)current, cpu_num++,
  82                        plat->cpu_id);
  83                current += length;
  84                total_length += length;
  85        }
  86
  87        return total_length;
  88}
  89
  90int acpi_create_madt_ioapic(struct acpi_madt_ioapic *ioapic, u8 id,
  91                            u32 addr, u32 gsi_base)
  92{
  93        ioapic->type = ACPI_APIC_IOAPIC;
  94        ioapic->length = sizeof(struct acpi_madt_ioapic);
  95        ioapic->reserved = 0x00;
  96        ioapic->gsi_base = gsi_base;
  97        ioapic->ioapic_id = id;
  98        ioapic->ioapic_addr = addr;
  99
 100        return ioapic->length;
 101}
 102
 103int acpi_create_madt_irqoverride(struct acpi_madt_irqoverride *irqoverride,
 104                                 u8 bus, u8 source, u32 gsirq, u16 flags)
 105{
 106        irqoverride->type = ACPI_APIC_IRQ_SRC_OVERRIDE;
 107        irqoverride->length = sizeof(struct acpi_madt_irqoverride);
 108        irqoverride->bus = bus;
 109        irqoverride->source = source;
 110        irqoverride->gsirq = gsirq;
 111        irqoverride->flags = flags;
 112
 113        return irqoverride->length;
 114}
 115
 116int acpi_create_madt_lapic_nmi(struct acpi_madt_lapic_nmi *lapic_nmi,
 117                               u8 cpu, u16 flags, u8 lint)
 118{
 119        lapic_nmi->type = ACPI_APIC_LAPIC_NMI;
 120        lapic_nmi->length = sizeof(struct acpi_madt_lapic_nmi);
 121        lapic_nmi->flags = flags;
 122        lapic_nmi->processor_id = cpu;
 123        lapic_nmi->lint = lint;
 124
 125        return lapic_nmi->length;
 126}
 127
 128static int acpi_create_madt_irq_overrides(u32 current)
 129{
 130        struct acpi_madt_irqoverride *irqovr;
 131        u16 sci_flags = MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_HIGH;
 132        int length = 0;
 133
 134        irqovr = (void *)current;
 135        length += acpi_create_madt_irqoverride(irqovr, 0, 0, 2, 0);
 136
 137        irqovr = (void *)(current + length);
 138        length += acpi_create_madt_irqoverride(irqovr, 0, 9, 9, sci_flags);
 139
 140        return length;
 141}
 142
 143__weak u32 acpi_fill_madt(u32 current)
 144{
 145        current += acpi_create_madt_lapics(current);
 146
 147        current += acpi_create_madt_ioapic((struct acpi_madt_ioapic *)current,
 148                        io_apic_read(IO_APIC_ID) >> 24, IO_APIC_ADDR, 0);
 149
 150        current += acpi_create_madt_irq_overrides(current);
 151
 152        return current;
 153}
 154
 155static void acpi_create_madt(struct acpi_madt *madt)
 156{
 157        struct acpi_table_header *header = &(madt->header);
 158        u32 current = (u32)madt + sizeof(struct acpi_madt);
 159
 160        memset((void *)madt, 0, sizeof(struct acpi_madt));
 161
 162        /* Fill out header fields */
 163        acpi_fill_header(header, "APIC");
 164        header->length = sizeof(struct acpi_madt);
 165        header->revision = ACPI_MADT_REV_ACPI_3_0;
 166
 167        madt->lapic_addr = LAPIC_DEFAULT_BASE;
 168        madt->flags = ACPI_MADT_PCAT_COMPAT;
 169
 170        current = acpi_fill_madt(current);
 171
 172        /* (Re)calculate length and checksum */
 173        header->length = current - (u32)madt;
 174
 175        header->checksum = table_compute_checksum((void *)madt, header->length);
 176}
 177
 178int acpi_create_mcfg_mmconfig(struct acpi_mcfg_mmconfig *mmconfig, u32 base,
 179                              u16 seg_nr, u8 start, u8 end)
 180{
 181        memset(mmconfig, 0, sizeof(*mmconfig));
 182        mmconfig->base_address_l = base;
 183        mmconfig->base_address_h = 0;
 184        mmconfig->pci_segment_group_number = seg_nr;
 185        mmconfig->start_bus_number = start;
 186        mmconfig->end_bus_number = end;
 187
 188        return sizeof(struct acpi_mcfg_mmconfig);
 189}
 190
 191__weak u32 acpi_fill_mcfg(u32 current)
 192{
 193        current += acpi_create_mcfg_mmconfig
 194                ((struct acpi_mcfg_mmconfig *)current,
 195                CONFIG_PCIE_ECAM_BASE, 0x0, 0x0, 255);
 196
 197        return current;
 198}
 199
 200/* MCFG is defined in the PCI Firmware Specification 3.0 */
 201static void acpi_create_mcfg(struct acpi_mcfg *mcfg)
 202{
 203        struct acpi_table_header *header = &(mcfg->header);
 204        u32 current = (u32)mcfg + sizeof(struct acpi_mcfg);
 205
 206        memset((void *)mcfg, 0, sizeof(struct acpi_mcfg));
 207
 208        /* Fill out header fields */
 209        acpi_fill_header(header, "MCFG");
 210        header->length = sizeof(struct acpi_mcfg);
 211        header->revision = 1;
 212
 213        current = acpi_fill_mcfg(current);
 214
 215        /* (Re)calculate length and checksum */
 216        header->length = current - (u32)mcfg;
 217        header->checksum = table_compute_checksum((void *)mcfg, header->length);
 218}
 219
 220/**
 221 * acpi_create_tcpa() - Create a TCPA table
 222 *
 223 * @tcpa: Pointer to place to put table
 224 *
 225 * Trusted Computing Platform Alliance Capabilities Table
 226 * TCPA PC Specific Implementation SpecificationTCPA is defined in the PCI
 227 * Firmware Specification 3.0
 228 */
 229static int acpi_create_tcpa(struct acpi_tcpa *tcpa)
 230{
 231        struct acpi_table_header *header = &tcpa->header;
 232        u32 current = (u32)tcpa + sizeof(struct acpi_tcpa);
 233        int size = 0x10000;     /* Use this as the default size */
 234        void *log;
 235        int ret;
 236
 237        if (!CONFIG_IS_ENABLED(BLOBLIST))
 238                return -ENXIO;
 239        memset(tcpa, '\0', sizeof(struct acpi_tcpa));
 240
 241        /* Fill out header fields */
 242        acpi_fill_header(header, "TCPA");
 243        header->length = sizeof(struct acpi_tcpa);
 244        header->revision = 1;
 245
 246        ret = bloblist_ensure_size_ret(BLOBLISTT_TCPA_LOG, &size, &log);
 247        if (ret)
 248                return log_msg_ret("blob", ret);
 249
 250        tcpa->platform_class = 0;
 251        tcpa->laml = size;
 252        tcpa->lasa = (ulong)log;
 253
 254        /* (Re)calculate length and checksum */
 255        header->length = current - (u32)tcpa;
 256        header->checksum = table_compute_checksum((void *)tcpa, header->length);
 257
 258        return 0;
 259}
 260
 261static int get_tpm2_log(void **ptrp, int *sizep)
 262{
 263        const int tpm2_default_log_len = 0x10000;
 264        int size;
 265        int ret;
 266
 267        *sizep = 0;
 268        size = tpm2_default_log_len;
 269        ret = bloblist_ensure_size_ret(BLOBLISTT_TPM2_TCG_LOG, &size, ptrp);
 270        if (ret)
 271                return log_msg_ret("blob", ret);
 272        *sizep = size;
 273
 274        return 0;
 275}
 276
 277static int acpi_create_tpm2(struct acpi_tpm2 *tpm2)
 278{
 279        struct acpi_table_header *header = &tpm2->header;
 280        int tpm2_log_len;
 281        void *lasa;
 282        int ret;
 283
 284        memset((void *)tpm2, 0, sizeof(struct acpi_tpm2));
 285
 286        /*
 287         * Some payloads like SeaBIOS depend on log area to use TPM2.
 288         * Get the memory size and address of TPM2 log area or initialize it.
 289         */
 290        ret = get_tpm2_log(&lasa, &tpm2_log_len);
 291        if (ret)
 292                return ret;
 293
 294        /* Fill out header fields. */
 295        acpi_fill_header(header, "TPM2");
 296        memcpy(header->aslc_id, ASLC_ID, 4);
 297
 298        header->length = sizeof(struct acpi_tpm2);
 299        header->revision = acpi_get_table_revision(ACPITAB_TPM2);
 300
 301        /* Hard to detect for coreboot. Just set it to 0 */
 302        tpm2->platform_class = 0;
 303
 304        /* Must be set to 0 for FIFO-interface support */
 305        tpm2->control_area = 0;
 306        tpm2->start_method = 6;
 307        memset(tpm2->msp, 0, sizeof(tpm2->msp));
 308
 309        /* Fill the log area size and start address fields. */
 310        tpm2->laml = tpm2_log_len;
 311        tpm2->lasa = (uintptr_t)lasa;
 312
 313        /* Calculate checksum. */
 314        header->checksum = table_compute_checksum((void *)tpm2, header->length);
 315
 316        return 0;
 317}
 318
 319__weak u32 acpi_fill_csrt(u32 current)
 320{
 321        return 0;
 322}
 323
 324static int acpi_create_csrt(struct acpi_csrt *csrt)
 325{
 326        struct acpi_table_header *header = &(csrt->header);
 327        u32 current = (u32)csrt + sizeof(struct acpi_csrt);
 328        uint ptr;
 329
 330        memset((void *)csrt, 0, sizeof(struct acpi_csrt));
 331
 332        /* Fill out header fields */
 333        acpi_fill_header(header, "CSRT");
 334        header->length = sizeof(struct acpi_csrt);
 335        header->revision = 0;
 336
 337        ptr = acpi_fill_csrt(current);
 338        if (!ptr)
 339                return -ENOENT;
 340        current = ptr;
 341
 342        /* (Re)calculate length and checksum */
 343        header->length = current - (u32)csrt;
 344        header->checksum = table_compute_checksum((void *)csrt, header->length);
 345
 346        return 0;
 347}
 348
 349static void acpi_create_spcr(struct acpi_spcr *spcr)
 350{
 351        struct acpi_table_header *header = &(spcr->header);
 352        struct serial_device_info serial_info = {0};
 353        ulong serial_address, serial_offset;
 354        struct udevice *dev;
 355        uint serial_config;
 356        uint serial_width;
 357        int access_size;
 358        int space_id;
 359        int ret = -ENODEV;
 360
 361        memset((void *)spcr, 0, sizeof(struct acpi_spcr));
 362
 363        /* Fill out header fields */
 364        acpi_fill_header(header, "SPCR");
 365        header->length = sizeof(struct acpi_spcr);
 366        header->revision = 2;
 367
 368        /* Read the device once, here. It is reused below */
 369        dev = gd->cur_serial_dev;
 370        if (dev)
 371                ret = serial_getinfo(dev, &serial_info);
 372        if (ret)
 373                serial_info.type = SERIAL_CHIP_UNKNOWN;
 374
 375        /* Encode chip type */
 376        switch (serial_info.type) {
 377        case SERIAL_CHIP_16550_COMPATIBLE:
 378                spcr->interface_type = ACPI_DBG2_16550_COMPATIBLE;
 379                break;
 380        case SERIAL_CHIP_UNKNOWN:
 381        default:
 382                spcr->interface_type = ACPI_DBG2_UNKNOWN;
 383                break;
 384        }
 385
 386        /* Encode address space */
 387        switch (serial_info.addr_space) {
 388        case SERIAL_ADDRESS_SPACE_MEMORY:
 389                space_id = ACPI_ADDRESS_SPACE_MEMORY;
 390                break;
 391        case SERIAL_ADDRESS_SPACE_IO:
 392        default:
 393                space_id = ACPI_ADDRESS_SPACE_IO;
 394                break;
 395        }
 396
 397        serial_width = serial_info.reg_width * 8;
 398        serial_offset = serial_info.reg_offset << serial_info.reg_shift;
 399        serial_address = serial_info.addr + serial_offset;
 400
 401        /* Encode register access size */
 402        switch (serial_info.reg_shift) {
 403        case 0:
 404                access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS;
 405                break;
 406        case 1:
 407                access_size = ACPI_ACCESS_SIZE_WORD_ACCESS;
 408                break;
 409        case 2:
 410                access_size = ACPI_ACCESS_SIZE_DWORD_ACCESS;
 411                break;
 412        case 3:
 413                access_size = ACPI_ACCESS_SIZE_QWORD_ACCESS;
 414                break;
 415        default:
 416                access_size = ACPI_ACCESS_SIZE_UNDEFINED;
 417                break;
 418        }
 419
 420        debug("UART type %u @ %lx\n", spcr->interface_type, serial_address);
 421
 422        /* Fill GAS */
 423        spcr->serial_port.space_id = space_id;
 424        spcr->serial_port.bit_width = serial_width;
 425        spcr->serial_port.bit_offset = 0;
 426        spcr->serial_port.access_size = access_size;
 427        spcr->serial_port.addrl = lower_32_bits(serial_address);
 428        spcr->serial_port.addrh = upper_32_bits(serial_address);
 429
 430        /* Encode baud rate */
 431        switch (serial_info.baudrate) {
 432        case 9600:
 433                spcr->baud_rate = 3;
 434                break;
 435        case 19200:
 436                spcr->baud_rate = 4;
 437                break;
 438        case 57600:
 439                spcr->baud_rate = 6;
 440                break;
 441        case 115200:
 442                spcr->baud_rate = 7;
 443                break;
 444        default:
 445                spcr->baud_rate = 0;
 446                break;
 447        }
 448
 449        serial_config = SERIAL_DEFAULT_CONFIG;
 450        if (dev)
 451                ret = serial_getconfig(dev, &serial_config);
 452
 453        spcr->parity = SERIAL_GET_PARITY(serial_config);
 454        spcr->stop_bits = SERIAL_GET_STOP(serial_config);
 455
 456        /* No PCI devices for now */
 457        spcr->pci_device_id = 0xffff;
 458        spcr->pci_vendor_id = 0xffff;
 459
 460        /*
 461         * SPCR has no clue if the UART base clock speed is different
 462         * to the default one. However, the SPCR 1.04 defines baud rate
 463         * 0 as a preconfigured state of UART and OS is supposed not
 464         * to touch the configuration of the serial device.
 465         */
 466        if (serial_info.clock != SERIAL_DEFAULT_CLOCK)
 467                spcr->baud_rate = 0;
 468
 469        /* Fix checksum */
 470        header->checksum = table_compute_checksum((void *)spcr, header->length);
 471}
 472
 473static int acpi_create_ssdt(struct acpi_ctx *ctx,
 474                            struct acpi_table_header *ssdt,
 475                            const char *oem_table_id)
 476{
 477        memset((void *)ssdt, '\0', sizeof(struct acpi_table_header));
 478
 479        acpi_fill_header(ssdt, "SSDT");
 480        ssdt->revision = acpi_get_table_revision(ACPITAB_SSDT);
 481        ssdt->aslc_revision = 1;
 482        ssdt->length = sizeof(struct acpi_table_header);
 483
 484        acpi_inc(ctx, sizeof(struct acpi_table_header));
 485
 486        acpi_fill_ssdt(ctx);
 487
 488        /* (Re)calculate length and checksum */
 489        ssdt->length = ctx->current - (void *)ssdt;
 490        ssdt->checksum = table_compute_checksum((void *)ssdt, ssdt->length);
 491        log_debug("SSDT at %p, length %x\n", ssdt, ssdt->length);
 492
 493        /* Drop the table if it is empty */
 494        if (ssdt->length == sizeof(struct acpi_table_header)) {
 495                ctx->current = ssdt;
 496                return -ENOENT;
 497        }
 498        acpi_align(ctx);
 499
 500        return 0;
 501}
 502
 503/*
 504 * QEMU's version of write_acpi_tables is defined in drivers/misc/qfw.c
 505 */
 506ulong write_acpi_tables(ulong start_addr)
 507{
 508        const int thl = sizeof(struct acpi_table_header);
 509        struct acpi_ctx *ctx;
 510        struct acpi_facs *facs;
 511        struct acpi_table_header *dsdt;
 512        struct acpi_fadt *fadt;
 513        struct acpi_table_header *ssdt;
 514        struct acpi_mcfg *mcfg;
 515        struct acpi_tcpa *tcpa;
 516        struct acpi_madt *madt;
 517        struct acpi_csrt *csrt;
 518        struct acpi_spcr *spcr;
 519        void *start;
 520        int aml_len;
 521        ulong addr;
 522        int ret;
 523        int i;
 524
 525        ctx = calloc(1, sizeof(*ctx));
 526        if (!ctx)
 527                return log_msg_ret("mem", -ENOMEM);
 528        gd->acpi_ctx = ctx;
 529
 530        start = map_sysmem(start_addr, 0);
 531
 532        debug("ACPI: Writing ACPI tables at %lx\n", start_addr);
 533
 534        acpi_reset_items();
 535        acpi_setup_base_tables(ctx, start);
 536
 537        debug("ACPI:    * FACS\n");
 538        facs = ctx->current;
 539        acpi_inc_align(ctx, sizeof(struct acpi_facs));
 540
 541        acpi_create_facs(facs);
 542
 543        debug("ACPI:    * DSDT\n");
 544        dsdt = ctx->current;
 545
 546        /* Put the table header first */
 547        memcpy(dsdt, &AmlCode, thl);
 548        acpi_inc(ctx, thl);
 549        log_debug("DSDT starts at %p, hdr ends at %p\n", dsdt, ctx->current);
 550
 551        /* If the table is not empty, allow devices to inject things */
 552        aml_len = dsdt->length - thl;
 553        if (aml_len) {
 554                void *base = ctx->current;
 555
 556                acpi_inject_dsdt(ctx);
 557                log_debug("Added %x bytes from inject_dsdt, now at %p\n",
 558                          ctx->current - base, ctx->current);
 559                log_debug("Copy AML code size %x to %p\n", aml_len,
 560                          ctx->current);
 561                memcpy(ctx->current, AmlCode + thl, aml_len);
 562                acpi_inc(ctx, aml_len);
 563        }
 564
 565        dsdt->length = ctx->current - (void *)dsdt;
 566        acpi_align(ctx);
 567        log_debug("Updated DSDT length to %x, total %x\n", dsdt->length,
 568                  ctx->current - (void *)dsdt);
 569
 570        if (!IS_ENABLED(CONFIG_ACPI_GNVS_EXTERNAL)) {
 571                /* Pack GNVS into the ACPI table area */
 572                for (i = 0; i < dsdt->length; i++) {
 573                        u32 *gnvs = (u32 *)((u32)dsdt + i);
 574
 575                        if (*gnvs == ACPI_GNVS_ADDR) {
 576                                *gnvs = map_to_sysmem(ctx->current);
 577                                debug("Fix up global NVS in DSDT to %#08x\n",
 578                                      *gnvs);
 579                                break;
 580                        }
 581                }
 582
 583                /*
 584                 * Fill in platform-specific global NVS variables. If this fails
 585                 * we cannot return the error but this should only happen while
 586                 * debugging.
 587                 */
 588                addr = acpi_create_gnvs(ctx->current);
 589                if (IS_ERR_VALUE(addr))
 590                        printf("Error: Gailed to create GNVS\n");
 591                acpi_inc_align(ctx, sizeof(struct acpi_global_nvs));
 592        }
 593
 594        /*
 595         * Recalculate the length and update the DSDT checksum since we patched
 596         * the GNVS address. Set the checksum to zero since it is part of the
 597         * region being checksummed.
 598         */
 599        dsdt->checksum = 0;
 600        dsdt->checksum = table_compute_checksum((void *)dsdt, dsdt->length);
 601
 602        /*
 603         * Fill in platform-specific global NVS variables. If this fails we
 604         * cannot return the error but this should only happen while debugging.
 605         */
 606        addr = acpi_create_gnvs(ctx->current);
 607        if (IS_ERR_VALUE(addr))
 608                printf("Error: Failed to create GNVS\n");
 609
 610        acpi_inc_align(ctx, sizeof(struct acpi_global_nvs));
 611
 612        debug("ACPI:    * FADT\n");
 613        fadt = ctx->current;
 614        acpi_inc_align(ctx, sizeof(struct acpi_fadt));
 615        acpi_create_fadt(fadt, facs, dsdt);
 616        acpi_add_table(ctx, fadt);
 617
 618        debug("ACPI:     * SSDT\n");
 619        ssdt = (struct acpi_table_header *)ctx->current;
 620        if (!acpi_create_ssdt(ctx, ssdt, OEM_TABLE_ID))
 621                acpi_add_table(ctx, ssdt);
 622
 623        debug("ACPI:    * MCFG\n");
 624        mcfg = ctx->current;
 625        acpi_create_mcfg(mcfg);
 626        acpi_inc_align(ctx, mcfg->header.length);
 627        acpi_add_table(ctx, mcfg);
 628
 629        if (IS_ENABLED(CONFIG_TPM_V2)) {
 630                struct acpi_tpm2 *tpm2;
 631
 632                debug("ACPI:    * TPM2\n");
 633                tpm2 = (struct acpi_tpm2 *)ctx->current;
 634                ret = acpi_create_tpm2(tpm2);
 635                if (!ret) {
 636                        acpi_inc_align(ctx, tpm2->header.length);
 637                        acpi_add_table(ctx, tpm2);
 638                } else {
 639                        log_warning("TPM2 table creation failed\n");
 640                }
 641        }
 642
 643        debug("ACPI:    * MADT\n");
 644        madt = ctx->current;
 645        acpi_create_madt(madt);
 646        acpi_inc_align(ctx, madt->header.length);
 647        acpi_add_table(ctx, madt);
 648
 649        if (IS_ENABLED(CONFIG_TPM_V1)) {
 650                debug("ACPI:    * TCPA\n");
 651                tcpa = (struct acpi_tcpa *)ctx->current;
 652                ret = acpi_create_tcpa(tcpa);
 653                if (ret) {
 654                        log_warning("Failed to create TCPA table (err=%d)\n",
 655                                    ret);
 656                } else {
 657                        acpi_inc_align(ctx, tcpa->header.length);
 658                        acpi_add_table(ctx, tcpa);
 659                }
 660        }
 661
 662        debug("ACPI:    * CSRT\n");
 663        csrt = ctx->current;
 664        if (!acpi_create_csrt(csrt)) {
 665                acpi_inc_align(ctx, csrt->header.length);
 666                acpi_add_table(ctx, csrt);
 667        }
 668
 669        debug("ACPI:    * SPCR\n");
 670        spcr = ctx->current;
 671        acpi_create_spcr(spcr);
 672        acpi_inc_align(ctx, spcr->header.length);
 673        acpi_add_table(ctx, spcr);
 674
 675        acpi_write_dev_tables(ctx);
 676
 677        addr = map_to_sysmem(ctx->current);
 678        debug("current = %lx\n", addr);
 679
 680        acpi_rsdp_addr = (unsigned long)ctx->rsdp;
 681        debug("ACPI: done\n");
 682
 683        return addr;
 684}
 685
 686ulong acpi_get_rsdp_addr(void)
 687{
 688        return acpi_rsdp_addr;
 689}
 690
 691/**
 692 * acpi_write_hpet() - Write out a HPET table
 693 *
 694 * Write out the table for High-Precision Event Timers
 695 *
 696 * @hpet: Place to put HPET table
 697 */
 698static int acpi_create_hpet(struct acpi_hpet *hpet)
 699{
 700        struct acpi_table_header *header = &hpet->header;
 701        struct acpi_gen_regaddr *addr = &hpet->addr;
 702
 703        /*
 704         * See IA-PC HPET (High Precision Event Timers) Specification v1.0a
 705         * https://www.intel.com/content/dam/www/public/us/en/documents/technical-specifications/software-developers-hpet-spec-1-0a.pdf
 706         */
 707        memset((void *)hpet, '\0', sizeof(struct acpi_hpet));
 708
 709        /* Fill out header fields. */
 710        acpi_fill_header(header, "HPET");
 711
 712        header->aslc_revision = ASL_REVISION;
 713        header->length = sizeof(struct acpi_hpet);
 714        header->revision = acpi_get_table_revision(ACPITAB_HPET);
 715
 716        /* Fill out HPET address */
 717        addr->space_id = 0;  /* Memory */
 718        addr->bit_width = 64;
 719        addr->bit_offset = 0;
 720        addr->addrl = CONFIG_HPET_ADDRESS & 0xffffffff;
 721        addr->addrh = ((unsigned long long)CONFIG_HPET_ADDRESS) >> 32;
 722
 723        hpet->id = *(u32 *)CONFIG_HPET_ADDRESS;
 724        hpet->number = 0;
 725        hpet->min_tick = 0; /* HPET_MIN_TICKS */
 726
 727        header->checksum = table_compute_checksum(hpet,
 728                                                  sizeof(struct acpi_hpet));
 729
 730        return 0;
 731}
 732
 733int acpi_write_hpet(struct acpi_ctx *ctx)
 734{
 735        struct acpi_hpet *hpet;
 736        int ret;
 737
 738        log_debug("ACPI:    * HPET\n");
 739
 740        hpet = ctx->current;
 741        acpi_inc_align(ctx, sizeof(struct acpi_hpet));
 742        acpi_create_hpet(hpet);
 743        ret = acpi_add_table(ctx, hpet);
 744        if (ret)
 745                return log_msg_ret("add", ret);
 746
 747        return 0;
 748}
 749
 750int acpi_write_dbg2_pci_uart(struct acpi_ctx *ctx, struct udevice *dev,
 751                             uint access_size)
 752{
 753        struct acpi_dbg2_header *dbg2 = ctx->current;
 754        char path[ACPI_PATH_MAX];
 755        struct acpi_gen_regaddr address;
 756        phys_addr_t addr;
 757        int ret;
 758
 759        if (!device_active(dev)) {
 760                log_info("Device not enabled\n");
 761                return -EACCES;
 762        }
 763        /*
 764         * PCI devices don't remember their resource allocation information in
 765         * U-Boot at present. We assume that MMIO is used for the UART and that
 766         * the address space is 32 bytes: ns16550 uses 8 registers of up to
 767         * 32-bits each. This is only for debugging so it is not a big deal.
 768         */
 769        addr = dm_pci_read_bar32(dev, 0);
 770        log_debug("UART addr %lx\n", (ulong)addr);
 771
 772        memset(&address, '\0', sizeof(address));
 773        address.space_id = ACPI_ADDRESS_SPACE_MEMORY;
 774        address.addrl = (uint32_t)addr;
 775        address.addrh = (uint32_t)((addr >> 32) & 0xffffffff);
 776        address.access_size = access_size;
 777
 778        ret = acpi_device_path(dev, path, sizeof(path));
 779        if (ret)
 780                return log_msg_ret("path", ret);
 781        acpi_create_dbg2(dbg2, ACPI_DBG2_SERIAL_PORT,
 782                         ACPI_DBG2_16550_COMPATIBLE, &address, 0x1000, path);
 783
 784        acpi_inc_align(ctx, dbg2->header.length);
 785        acpi_add_table(ctx, dbg2);
 786
 787        return 0;
 788}
 789
 790void acpi_fadt_common(struct acpi_fadt *fadt, struct acpi_facs *facs,
 791                      void *dsdt)
 792{
 793        struct acpi_table_header *header = &fadt->header;
 794
 795        memset((void *)fadt, '\0', sizeof(struct acpi_fadt));
 796
 797        acpi_fill_header(header, "FACP");
 798        header->length = sizeof(struct acpi_fadt);
 799        header->revision = 4;
 800        memcpy(header->oem_id, OEM_ID, 6);
 801        memcpy(header->oem_table_id, OEM_TABLE_ID, 8);
 802        memcpy(header->aslc_id, ASLC_ID, 4);
 803        header->aslc_revision = 1;
 804
 805        fadt->firmware_ctrl = (unsigned long)facs;
 806        fadt->dsdt = (unsigned long)dsdt;
 807
 808        fadt->x_firmware_ctl_l = (unsigned long)facs;
 809        fadt->x_firmware_ctl_h = 0;
 810        fadt->x_dsdt_l = (unsigned long)dsdt;
 811        fadt->x_dsdt_h = 0;
 812
 813        fadt->preferred_pm_profile = ACPI_PM_MOBILE;
 814
 815        /* Use ACPI 3.0 revision */
 816        fadt->header.revision = 4;
 817}
 818
 819void acpi_create_dmar_drhd(struct acpi_ctx *ctx, uint flags, uint segment,
 820                           u64 bar)
 821{
 822        struct dmar_entry *drhd = ctx->current;
 823
 824        memset(drhd, '\0', sizeof(*drhd));
 825        drhd->type = DMAR_DRHD;
 826        drhd->length = sizeof(*drhd); /* will be fixed up later */
 827        drhd->flags = flags;
 828        drhd->segment = segment;
 829        drhd->bar = bar;
 830        acpi_inc(ctx, drhd->length);
 831}
 832
 833void acpi_create_dmar_rmrr(struct acpi_ctx *ctx, uint segment, u64 bar,
 834                           u64 limit)
 835{
 836        struct dmar_rmrr_entry *rmrr = ctx->current;
 837
 838        memset(rmrr, '\0', sizeof(*rmrr));
 839        rmrr->type = DMAR_RMRR;
 840        rmrr->length = sizeof(*rmrr); /* will be fixed up later */
 841        rmrr->segment = segment;
 842        rmrr->bar = bar;
 843        rmrr->limit = limit;
 844        acpi_inc(ctx, rmrr->length);
 845}
 846
 847void acpi_dmar_drhd_fixup(struct acpi_ctx *ctx, void *base)
 848{
 849        struct dmar_entry *drhd = base;
 850
 851        drhd->length = ctx->current - base;
 852}
 853
 854void acpi_dmar_rmrr_fixup(struct acpi_ctx *ctx, void *base)
 855{
 856        struct dmar_rmrr_entry *rmrr = base;
 857
 858        rmrr->length = ctx->current - base;
 859}
 860
 861static int acpi_create_dmar_ds(struct acpi_ctx *ctx, enum dev_scope_type type,
 862                               uint enumeration_id, pci_dev_t bdf)
 863{
 864        /* we don't support longer paths yet */
 865        const size_t dev_scope_length = sizeof(struct dev_scope) + 2;
 866        struct dev_scope *ds = ctx->current;
 867
 868        memset(ds, '\0', dev_scope_length);
 869        ds->type = type;
 870        ds->length = dev_scope_length;
 871        ds->enumeration = enumeration_id;
 872        ds->start_bus = PCI_BUS(bdf);
 873        ds->path[0].dev = PCI_DEV(bdf);
 874        ds->path[0].fn = PCI_FUNC(bdf);
 875
 876        return ds->length;
 877}
 878
 879int acpi_create_dmar_ds_pci_br(struct acpi_ctx *ctx, pci_dev_t bdf)
 880{
 881        return acpi_create_dmar_ds(ctx, SCOPE_PCI_SUB, 0, bdf);
 882}
 883
 884int acpi_create_dmar_ds_pci(struct acpi_ctx *ctx, pci_dev_t bdf)
 885{
 886        return acpi_create_dmar_ds(ctx, SCOPE_PCI_ENDPOINT, 0, bdf);
 887}
 888
 889int acpi_create_dmar_ds_ioapic(struct acpi_ctx *ctx, uint enumeration_id,
 890                               pci_dev_t bdf)
 891{
 892        return acpi_create_dmar_ds(ctx, SCOPE_IOAPIC, enumeration_id, bdf);
 893}
 894
 895int acpi_create_dmar_ds_msi_hpet(struct acpi_ctx *ctx, uint enumeration_id,
 896                                 pci_dev_t bdf)
 897{
 898        return acpi_create_dmar_ds(ctx, SCOPE_MSI_HPET, enumeration_id, bdf);
 899}
 900