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#include <common.h>
  10#include <cpu.h>
  11#include <dm.h>
  12#include <dm/uclass-internal.h>
  13#include <version.h>
  14#include <asm/acpi/global_nvs.h>
  15#include <asm/acpi_table.h>
  16#include <asm/ioapic.h>
  17#include <asm/lapic.h>
  18#include <asm/mpspec.h>
  19#include <asm/tables.h>
  20#include <asm/arch/global_nvs.h>
  21
  22/*
  23 * IASL compiles the dsdt entries and writes the hex values
  24 * to a C array AmlCode[] (see dsdt.c).
  25 */
  26extern const unsigned char AmlCode[];
  27
  28/* ACPI RSDP address to be used in boot parameters */
  29static ulong acpi_rsdp_addr;
  30
  31static void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt,
  32                            struct acpi_xsdt *xsdt)
  33{
  34        memset(rsdp, 0, sizeof(struct acpi_rsdp));
  35
  36        memcpy(rsdp->signature, RSDP_SIG, 8);
  37        memcpy(rsdp->oem_id, OEM_ID, 6);
  38
  39        rsdp->length = sizeof(struct acpi_rsdp);
  40        rsdp->rsdt_address = (u32)rsdt;
  41
  42        /*
  43         * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2
  44         *
  45         * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
  46         * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
  47         * revision 0)
  48         */
  49        if (xsdt == NULL) {
  50                rsdp->revision = ACPI_RSDP_REV_ACPI_1_0;
  51        } else {
  52                rsdp->xsdt_address = (u64)(u32)xsdt;
  53                rsdp->revision = ACPI_RSDP_REV_ACPI_2_0;
  54        }
  55
  56        /* Calculate checksums */
  57        rsdp->checksum = table_compute_checksum((void *)rsdp, 20);
  58        rsdp->ext_checksum = table_compute_checksum((void *)rsdp,
  59                        sizeof(struct acpi_rsdp));
  60}
  61
  62void acpi_fill_header(struct acpi_table_header *header, char *signature)
  63{
  64        memcpy(header->signature, signature, 4);
  65        memcpy(header->oem_id, OEM_ID, 6);
  66        memcpy(header->oem_table_id, OEM_TABLE_ID, 8);
  67        header->oem_revision = U_BOOT_BUILD_DATE;
  68        memcpy(header->aslc_id, ASLC_ID, 4);
  69}
  70
  71static void acpi_write_rsdt(struct acpi_rsdt *rsdt)
  72{
  73        struct acpi_table_header *header = &(rsdt->header);
  74
  75        /* Fill out header fields */
  76        acpi_fill_header(header, "RSDT");
  77        header->length = sizeof(struct acpi_rsdt);
  78        header->revision = 1;
  79
  80        /* Entries are filled in later, we come with an empty set */
  81
  82        /* Fix checksum */
  83        header->checksum = table_compute_checksum((void *)rsdt,
  84                        sizeof(struct acpi_rsdt));
  85}
  86
  87static void acpi_write_xsdt(struct acpi_xsdt *xsdt)
  88{
  89        struct acpi_table_header *header = &(xsdt->header);
  90
  91        /* Fill out header fields */
  92        acpi_fill_header(header, "XSDT");
  93        header->length = sizeof(struct acpi_xsdt);
  94        header->revision = 1;
  95
  96        /* Entries are filled in later, we come with an empty set */
  97
  98        /* Fix checksum */
  99        header->checksum = table_compute_checksum((void *)xsdt,
 100                        sizeof(struct acpi_xsdt));
 101}
 102
 103/**
 104 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
 105 * and checksum.
 106 */
 107static void acpi_add_table(struct acpi_rsdp *rsdp, void *table)
 108{
 109        int i, entries_num;
 110        struct acpi_rsdt *rsdt;
 111        struct acpi_xsdt *xsdt = NULL;
 112
 113        /* The RSDT is mandatory while the XSDT is not */
 114        rsdt = (struct acpi_rsdt *)rsdp->rsdt_address;
 115
 116        if (rsdp->xsdt_address)
 117                xsdt = (struct acpi_xsdt *)((u32)rsdp->xsdt_address);
 118
 119        /* This should always be MAX_ACPI_TABLES */
 120        entries_num = ARRAY_SIZE(rsdt->entry);
 121
 122        for (i = 0; i < entries_num; i++) {
 123                if (rsdt->entry[i] == 0)
 124                        break;
 125        }
 126
 127        if (i >= entries_num) {
 128                debug("ACPI: Error: too many tables\n");
 129                return;
 130        }
 131
 132        /* Add table to the RSDT */
 133        rsdt->entry[i] = (u32)table;
 134
 135        /* Fix RSDT length or the kernel will assume invalid entries */
 136        rsdt->header.length = sizeof(struct acpi_table_header) +
 137                                (sizeof(u32) * (i + 1));
 138
 139        /* Re-calculate checksum */
 140        rsdt->header.checksum = 0;
 141        rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
 142                        rsdt->header.length);
 143
 144        /*
 145         * And now the same thing for the XSDT. We use the same index as for
 146         * now we want the XSDT and RSDT to always be in sync in U-Boot
 147         */
 148        if (xsdt) {
 149                /* Add table to the XSDT */
 150                xsdt->entry[i] = (u64)(u32)table;
 151
 152                /* Fix XSDT length */
 153                xsdt->header.length = sizeof(struct acpi_table_header) +
 154                        (sizeof(u64) * (i + 1));
 155
 156                /* Re-calculate checksum */
 157                xsdt->header.checksum = 0;
 158                xsdt->header.checksum = table_compute_checksum((u8 *)xsdt,
 159                                xsdt->header.length);
 160        }
 161}
 162
 163static void acpi_create_facs(struct acpi_facs *facs)
 164{
 165        memset((void *)facs, 0, sizeof(struct acpi_facs));
 166
 167        memcpy(facs->signature, "FACS", 4);
 168        facs->length = sizeof(struct acpi_facs);
 169        facs->hardware_signature = 0;
 170        facs->firmware_waking_vector = 0;
 171        facs->global_lock = 0;
 172        facs->flags = 0;
 173        facs->x_firmware_waking_vector_l = 0;
 174        facs->x_firmware_waking_vector_h = 0;
 175        facs->version = 1;
 176}
 177
 178static int acpi_create_madt_lapic(struct acpi_madt_lapic *lapic,
 179                                  u8 cpu, u8 apic)
 180{
 181        lapic->type = ACPI_APIC_LAPIC;
 182        lapic->length = sizeof(struct acpi_madt_lapic);
 183        lapic->flags = LOCAL_APIC_FLAG_ENABLED;
 184        lapic->processor_id = cpu;
 185        lapic->apic_id = apic;
 186
 187        return lapic->length;
 188}
 189
 190int acpi_create_madt_lapics(u32 current)
 191{
 192        struct udevice *dev;
 193        int total_length = 0;
 194
 195        for (uclass_find_first_device(UCLASS_CPU, &dev);
 196             dev;
 197             uclass_find_next_device(&dev)) {
 198                struct cpu_platdata *plat = dev_get_parent_platdata(dev);
 199                int length = acpi_create_madt_lapic(
 200                                (struct acpi_madt_lapic *)current,
 201                                plat->cpu_id, plat->cpu_id);
 202                current += length;
 203                total_length += length;
 204        }
 205
 206        return total_length;
 207}
 208
 209int acpi_create_madt_ioapic(struct acpi_madt_ioapic *ioapic, u8 id,
 210                            u32 addr, u32 gsi_base)
 211{
 212        ioapic->type = ACPI_APIC_IOAPIC;
 213        ioapic->length = sizeof(struct acpi_madt_ioapic);
 214        ioapic->reserved = 0x00;
 215        ioapic->gsi_base = gsi_base;
 216        ioapic->ioapic_id = id;
 217        ioapic->ioapic_addr = addr;
 218
 219        return ioapic->length;
 220}
 221
 222int acpi_create_madt_irqoverride(struct acpi_madt_irqoverride *irqoverride,
 223                                 u8 bus, u8 source, u32 gsirq, u16 flags)
 224{
 225        irqoverride->type = ACPI_APIC_IRQ_SRC_OVERRIDE;
 226        irqoverride->length = sizeof(struct acpi_madt_irqoverride);
 227        irqoverride->bus = bus;
 228        irqoverride->source = source;
 229        irqoverride->gsirq = gsirq;
 230        irqoverride->flags = flags;
 231
 232        return irqoverride->length;
 233}
 234
 235int acpi_create_madt_lapic_nmi(struct acpi_madt_lapic_nmi *lapic_nmi,
 236                               u8 cpu, u16 flags, u8 lint)
 237{
 238        lapic_nmi->type = ACPI_APIC_LAPIC_NMI;
 239        lapic_nmi->length = sizeof(struct acpi_madt_lapic_nmi);
 240        lapic_nmi->flags = flags;
 241        lapic_nmi->processor_id = cpu;
 242        lapic_nmi->lint = lint;
 243
 244        return lapic_nmi->length;
 245}
 246
 247static int acpi_create_madt_irq_overrides(u32 current)
 248{
 249        struct acpi_madt_irqoverride *irqovr;
 250        u16 sci_flags = MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_HIGH;
 251        int length = 0;
 252
 253        irqovr = (void *)current;
 254        length += acpi_create_madt_irqoverride(irqovr, 0, 0, 2, 0);
 255
 256        irqovr = (void *)(current + length);
 257        length += acpi_create_madt_irqoverride(irqovr, 0, 9, 9, sci_flags);
 258
 259        return length;
 260}
 261
 262__weak u32 acpi_fill_madt(u32 current)
 263{
 264        current += acpi_create_madt_lapics(current);
 265
 266        current += acpi_create_madt_ioapic((struct acpi_madt_ioapic *)current,
 267                        io_apic_read(IO_APIC_ID) >> 24, IO_APIC_ADDR, 0);
 268
 269        current += acpi_create_madt_irq_overrides(current);
 270
 271        return current;
 272}
 273
 274static void acpi_create_madt(struct acpi_madt *madt)
 275{
 276        struct acpi_table_header *header = &(madt->header);
 277        u32 current = (u32)madt + sizeof(struct acpi_madt);
 278
 279        memset((void *)madt, 0, sizeof(struct acpi_madt));
 280
 281        /* Fill out header fields */
 282        acpi_fill_header(header, "APIC");
 283        header->length = sizeof(struct acpi_madt);
 284        header->revision = 4;
 285
 286        madt->lapic_addr = LAPIC_DEFAULT_BASE;
 287        madt->flags = ACPI_MADT_PCAT_COMPAT;
 288
 289        current = acpi_fill_madt(current);
 290
 291        /* (Re)calculate length and checksum */
 292        header->length = current - (u32)madt;
 293
 294        header->checksum = table_compute_checksum((void *)madt, header->length);
 295}
 296
 297int acpi_create_mcfg_mmconfig(struct acpi_mcfg_mmconfig *mmconfig, u32 base,
 298                              u16 seg_nr, u8 start, u8 end)
 299{
 300        memset(mmconfig, 0, sizeof(*mmconfig));
 301        mmconfig->base_address_l = base;
 302        mmconfig->base_address_h = 0;
 303        mmconfig->pci_segment_group_number = seg_nr;
 304        mmconfig->start_bus_number = start;
 305        mmconfig->end_bus_number = end;
 306
 307        return sizeof(struct acpi_mcfg_mmconfig);
 308}
 309
 310__weak u32 acpi_fill_mcfg(u32 current)
 311{
 312        current += acpi_create_mcfg_mmconfig
 313                ((struct acpi_mcfg_mmconfig *)current,
 314                CONFIG_PCIE_ECAM_BASE, 0x0, 0x0, 255);
 315
 316        return current;
 317}
 318
 319/* MCFG is defined in the PCI Firmware Specification 3.0 */
 320static void acpi_create_mcfg(struct acpi_mcfg *mcfg)
 321{
 322        struct acpi_table_header *header = &(mcfg->header);
 323        u32 current = (u32)mcfg + sizeof(struct acpi_mcfg);
 324
 325        memset((void *)mcfg, 0, sizeof(struct acpi_mcfg));
 326
 327        /* Fill out header fields */
 328        acpi_fill_header(header, "MCFG");
 329        header->length = sizeof(struct acpi_mcfg);
 330        header->revision = 1;
 331
 332        current = acpi_fill_mcfg(current);
 333
 334        /* (Re)calculate length and checksum */
 335        header->length = current - (u32)mcfg;
 336        header->checksum = table_compute_checksum((void *)mcfg, header->length);
 337}
 338
 339/*
 340 * QEMU's version of write_acpi_tables is defined in drivers/misc/qfw.c
 341 */
 342ulong write_acpi_tables(ulong start)
 343{
 344        u32 current;
 345        struct acpi_rsdp *rsdp;
 346        struct acpi_rsdt *rsdt;
 347        struct acpi_xsdt *xsdt;
 348        struct acpi_facs *facs;
 349        struct acpi_table_header *dsdt;
 350        struct acpi_fadt *fadt;
 351        struct acpi_mcfg *mcfg;
 352        struct acpi_madt *madt;
 353        int i;
 354
 355        current = start;
 356
 357        /* Align ACPI tables to 16 byte */
 358        current = ALIGN(current, 16);
 359
 360        debug("ACPI: Writing ACPI tables at %lx\n", start);
 361
 362        /* We need at least an RSDP and an RSDT Table */
 363        rsdp = (struct acpi_rsdp *)current;
 364        current += sizeof(struct acpi_rsdp);
 365        current = ALIGN(current, 16);
 366        rsdt = (struct acpi_rsdt *)current;
 367        current += sizeof(struct acpi_rsdt);
 368        current = ALIGN(current, 16);
 369        xsdt = (struct acpi_xsdt *)current;
 370        current += sizeof(struct acpi_xsdt);
 371        /*
 372         * Per ACPI spec, the FACS table address must be aligned to a 64 byte
 373         * boundary (Windows checks this, but Linux does not).
 374         */
 375        current = ALIGN(current, 64);
 376
 377        /* clear all table memory */
 378        memset((void *)start, 0, current - start);
 379
 380        acpi_write_rsdp(rsdp, rsdt, xsdt);
 381        acpi_write_rsdt(rsdt);
 382        acpi_write_xsdt(xsdt);
 383
 384        debug("ACPI:    * FACS\n");
 385        facs = (struct acpi_facs *)current;
 386        current += sizeof(struct acpi_facs);
 387        current = ALIGN(current, 16);
 388
 389        acpi_create_facs(facs);
 390
 391        debug("ACPI:    * DSDT\n");
 392        dsdt = (struct acpi_table_header *)current;
 393        memcpy(dsdt, &AmlCode, sizeof(struct acpi_table_header));
 394        current += sizeof(struct acpi_table_header);
 395        memcpy((char *)current,
 396               (char *)&AmlCode + sizeof(struct acpi_table_header),
 397               dsdt->length - sizeof(struct acpi_table_header));
 398        current += dsdt->length - sizeof(struct acpi_table_header);
 399        current = ALIGN(current, 16);
 400
 401        /* Pack GNVS into the ACPI table area */
 402        for (i = 0; i < dsdt->length; i++) {
 403                u32 *gnvs = (u32 *)((u32)dsdt + i);
 404                if (*gnvs == ACPI_GNVS_ADDR) {
 405                        debug("Fix up global NVS in DSDT to 0x%08x\n", current);
 406                        *gnvs = current;
 407                        break;
 408                }
 409        }
 410
 411        /* Update DSDT checksum since we patched the GNVS address */
 412        dsdt->checksum = 0;
 413        dsdt->checksum = table_compute_checksum((void *)dsdt, dsdt->length);
 414
 415        /* Fill in platform-specific global NVS variables */
 416        acpi_create_gnvs((struct acpi_global_nvs *)current);
 417        current += sizeof(struct acpi_global_nvs);
 418        current = ALIGN(current, 16);
 419
 420        debug("ACPI:    * FADT\n");
 421        fadt = (struct acpi_fadt *)current;
 422        current += sizeof(struct acpi_fadt);
 423        current = ALIGN(current, 16);
 424        acpi_create_fadt(fadt, facs, dsdt);
 425        acpi_add_table(rsdp, fadt);
 426
 427        debug("ACPI:    * MADT\n");
 428        madt = (struct acpi_madt *)current;
 429        acpi_create_madt(madt);
 430        current += madt->header.length;
 431        acpi_add_table(rsdp, madt);
 432        current = ALIGN(current, 16);
 433
 434        debug("ACPI:    * MCFG\n");
 435        mcfg = (struct acpi_mcfg *)current;
 436        acpi_create_mcfg(mcfg);
 437        current += mcfg->header.length;
 438        acpi_add_table(rsdp, mcfg);
 439        current = ALIGN(current, 16);
 440
 441        debug("current = %x\n", current);
 442
 443        acpi_rsdp_addr = (unsigned long)rsdp;
 444        debug("ACPI: done\n");
 445
 446        return current;
 447}
 448
 449ulong acpi_get_rsdp_addr(void)
 450{
 451        return acpi_rsdp_addr;
 452}
 453