2 * Based on acpi.c from coreboot
4 * Copyright (C) 2015, Saket Sinha <saket.sinha89@gmail.com>
5 * Copyright (C) 2016, Bin Meng <bmeng.cn@gmail.com>
7 * SPDX-License-Identifier: GPL-2.0+
13 #include <dm/uclass-internal.h>
15 #include <asm/acpi/global_nvs.h>
16 #include <asm/acpi_table.h>
18 #include <asm/ioapic.h>
19 #include <asm/lapic.h>
20 #include <asm/mpspec.h>
21 #include <asm/tables.h>
22 #include <asm/arch/global_nvs.h>
23 #include "acpi_table.h"
26 * IASL compiles the dsdt entries and writes the hex values
27 * to a C array AmlCode[] (see dsdt.c).
29 extern const unsigned char AmlCode[];
31 /* ACPI RSDP address to be used in boot parameters */
32 unsigned long acpi_rsdp_addr;
34 static void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt,
35 struct acpi_xsdt *xsdt)
37 memset(rsdp, 0, sizeof(struct acpi_rsdp));
39 memcpy(rsdp->signature, RSDP_SIG, 8);
40 memcpy(rsdp->oem_id, OEM_ID, 6);
42 rsdp->length = sizeof(struct acpi_rsdp);
43 rsdp->rsdt_address = (u32)rsdt;
46 * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2
48 * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
49 * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
53 rsdp->revision = ACPI_RSDP_REV_ACPI_1_0;
55 rsdp->xsdt_address = (u64)(u32)xsdt;
56 rsdp->revision = ACPI_RSDP_REV_ACPI_2_0;
59 /* Calculate checksums */
60 rsdp->checksum = table_compute_checksum((void *)rsdp, 20);
61 rsdp->ext_checksum = table_compute_checksum((void *)rsdp,
62 sizeof(struct acpi_rsdp));
65 void acpi_fill_header(struct acpi_table_header *header, char *signature)
67 memcpy(header->signature, signature, 4);
68 memcpy(header->oem_id, OEM_ID, 6);
69 memcpy(header->oem_table_id, OEM_TABLE_ID, 8);
70 header->oem_revision = U_BOOT_BUILD_DATE;
71 memcpy(header->aslc_id, ASLC_ID, 4);
74 static void acpi_write_rsdt(struct acpi_rsdt *rsdt)
76 struct acpi_table_header *header = &(rsdt->header);
78 /* Fill out header fields */
79 acpi_fill_header(header, "RSDT");
80 header->length = sizeof(struct acpi_rsdt);
83 /* Entries are filled in later, we come with an empty set */
86 header->checksum = table_compute_checksum((void *)rsdt,
87 sizeof(struct acpi_rsdt));
90 static void acpi_write_xsdt(struct acpi_xsdt *xsdt)
92 struct acpi_table_header *header = &(xsdt->header);
94 /* Fill out header fields */
95 acpi_fill_header(header, "XSDT");
96 header->length = sizeof(struct acpi_xsdt);
99 /* Entries are filled in later, we come with an empty set */
102 header->checksum = table_compute_checksum((void *)xsdt,
103 sizeof(struct acpi_xsdt));
107 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
110 static void acpi_add_table(struct acpi_rsdp *rsdp, void *table)
113 struct acpi_rsdt *rsdt;
114 struct acpi_xsdt *xsdt = NULL;
116 /* The RSDT is mandatory while the XSDT is not */
117 rsdt = (struct acpi_rsdt *)rsdp->rsdt_address;
119 if (rsdp->xsdt_address)
120 xsdt = (struct acpi_xsdt *)((u32)rsdp->xsdt_address);
122 /* This should always be MAX_ACPI_TABLES */
123 entries_num = ARRAY_SIZE(rsdt->entry);
125 for (i = 0; i < entries_num; i++) {
126 if (rsdt->entry[i] == 0)
130 if (i >= entries_num) {
131 debug("ACPI: Error: too many tables\n");
135 /* Add table to the RSDT */
136 rsdt->entry[i] = (u32)table;
138 /* Fix RSDT length or the kernel will assume invalid entries */
139 rsdt->header.length = sizeof(struct acpi_table_header) +
140 (sizeof(u32) * (i + 1));
142 /* Re-calculate checksum */
143 rsdt->header.checksum = 0;
144 rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
145 rsdt->header.length);
148 * And now the same thing for the XSDT. We use the same index as for
149 * now we want the XSDT and RSDT to always be in sync in U-Boot
152 /* Add table to the XSDT */
153 xsdt->entry[i] = (u64)(u32)table;
155 /* Fix XSDT length */
156 xsdt->header.length = sizeof(struct acpi_table_header) +
157 (sizeof(u64) * (i + 1));
159 /* Re-calculate checksum */
160 xsdt->header.checksum = 0;
161 xsdt->header.checksum = table_compute_checksum((u8 *)xsdt,
162 xsdt->header.length);
166 static void acpi_create_facs(struct acpi_facs *facs)
168 memset((void *)facs, 0, sizeof(struct acpi_facs));
170 memcpy(facs->signature, "FACS", 4);
171 facs->length = sizeof(struct acpi_facs);
172 facs->hardware_signature = 0;
173 facs->firmware_waking_vector = 0;
174 facs->global_lock = 0;
176 facs->x_firmware_waking_vector_l = 0;
177 facs->x_firmware_waking_vector_h = 0;
181 static int acpi_create_madt_lapic(struct acpi_madt_lapic *lapic,
184 lapic->type = ACPI_APIC_LAPIC;
185 lapic->length = sizeof(struct acpi_madt_lapic);
186 lapic->flags = LOCAL_APIC_FLAG_ENABLED;
187 lapic->processor_id = cpu;
188 lapic->apic_id = apic;
190 return lapic->length;
193 int acpi_create_madt_lapics(u32 current)
196 int total_length = 0;
198 for (uclass_find_first_device(UCLASS_CPU, &dev);
200 uclass_find_next_device(&dev)) {
201 struct cpu_platdata *plat = dev_get_parent_platdata(dev);
202 int length = acpi_create_madt_lapic(
203 (struct acpi_madt_lapic *)current,
204 plat->cpu_id, plat->cpu_id);
206 total_length += length;
212 int acpi_create_madt_ioapic(struct acpi_madt_ioapic *ioapic, u8 id,
213 u32 addr, u32 gsi_base)
215 ioapic->type = ACPI_APIC_IOAPIC;
216 ioapic->length = sizeof(struct acpi_madt_ioapic);
217 ioapic->reserved = 0x00;
218 ioapic->gsi_base = gsi_base;
219 ioapic->ioapic_id = id;
220 ioapic->ioapic_addr = addr;
222 return ioapic->length;
225 int acpi_create_madt_irqoverride(struct acpi_madt_irqoverride *irqoverride,
226 u8 bus, u8 source, u32 gsirq, u16 flags)
228 irqoverride->type = ACPI_APIC_IRQ_SRC_OVERRIDE;
229 irqoverride->length = sizeof(struct acpi_madt_irqoverride);
230 irqoverride->bus = bus;
231 irqoverride->source = source;
232 irqoverride->gsirq = gsirq;
233 irqoverride->flags = flags;
235 return irqoverride->length;
238 int acpi_create_madt_lapic_nmi(struct acpi_madt_lapic_nmi *lapic_nmi,
239 u8 cpu, u16 flags, u8 lint)
241 lapic_nmi->type = ACPI_APIC_LAPIC_NMI;
242 lapic_nmi->length = sizeof(struct acpi_madt_lapic_nmi);
243 lapic_nmi->flags = flags;
244 lapic_nmi->processor_id = cpu;
245 lapic_nmi->lint = lint;
247 return lapic_nmi->length;
250 static int acpi_create_madt_irq_overrides(u32 current)
252 struct acpi_madt_irqoverride *irqovr;
253 u16 sci_flags = MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_HIGH;
256 irqovr = (void *)current;
257 length += acpi_create_madt_irqoverride(irqovr, 0, 0, 2, 0);
259 irqovr = (void *)(current + length);
260 length += acpi_create_madt_irqoverride(irqovr, 0, 9, 9, sci_flags);
265 __weak u32 acpi_fill_madt(u32 current)
267 current += acpi_create_madt_lapics(current);
269 current += acpi_create_madt_ioapic((struct acpi_madt_ioapic *)current,
270 io_apic_read(IO_APIC_ID) >> 24, IO_APIC_ADDR, 0);
272 current += acpi_create_madt_irq_overrides(current);
277 static void acpi_create_madt(struct acpi_madt *madt)
279 struct acpi_table_header *header = &(madt->header);
280 u32 current = (u32)madt + sizeof(struct acpi_madt);
282 memset((void *)madt, 0, sizeof(struct acpi_madt));
284 /* Fill out header fields */
285 acpi_fill_header(header, "APIC");
286 header->length = sizeof(struct acpi_madt);
287 header->revision = 4;
289 madt->lapic_addr = LAPIC_DEFAULT_BASE;
290 madt->flags = ACPI_MADT_PCAT_COMPAT;
292 current = acpi_fill_madt(current);
294 /* (Re)calculate length and checksum */
295 header->length = current - (u32)madt;
297 header->checksum = table_compute_checksum((void *)madt, header->length);
300 int acpi_create_mcfg_mmconfig(struct acpi_mcfg_mmconfig *mmconfig, u32 base,
301 u16 seg_nr, u8 start, u8 end)
303 memset(mmconfig, 0, sizeof(*mmconfig));
304 mmconfig->base_address_l = base;
305 mmconfig->base_address_h = 0;
306 mmconfig->pci_segment_group_number = seg_nr;
307 mmconfig->start_bus_number = start;
308 mmconfig->end_bus_number = end;
310 return sizeof(struct acpi_mcfg_mmconfig);
313 __weak u32 acpi_fill_mcfg(u32 current)
315 current += acpi_create_mcfg_mmconfig
316 ((struct acpi_mcfg_mmconfig *)current,
317 CONFIG_PCIE_ECAM_BASE, 0x0, 0x0, 255);
322 /* MCFG is defined in the PCI Firmware Specification 3.0 */
323 static void acpi_create_mcfg(struct acpi_mcfg *mcfg)
325 struct acpi_table_header *header = &(mcfg->header);
326 u32 current = (u32)mcfg + sizeof(struct acpi_mcfg);
328 memset((void *)mcfg, 0, sizeof(struct acpi_mcfg));
330 /* Fill out header fields */
331 acpi_fill_header(header, "MCFG");
332 header->length = sizeof(struct acpi_mcfg);
333 header->revision = 1;
335 current = acpi_fill_mcfg(current);
337 /* (Re)calculate length and checksum */
338 header->length = current - (u32)mcfg;
339 header->checksum = table_compute_checksum((void *)mcfg, header->length);
342 void enter_acpi_mode(int pm1_cnt)
344 u16 val = inw(pm1_cnt);
347 * PM1_CNT register bit0 selects the power management event to be
348 * either an SCI or SMI interrupt. When this bit is set, then power
349 * management events will generate an SCI interrupt. When this bit
350 * is reset power management events will generate an SMI interrupt.
352 * Per ACPI spec, it is the responsibility of the hardware to set
353 * or reset this bit. OSPM always preserves this bit position.
355 * U-Boot does not support SMI. And we don't have plan to support
356 * anything running in SMM within U-Boot. To create a legacy-free
357 * system, and expose ourselves to OSPM as working under ACPI mode
358 * already, turn this bit on.
360 outw(val | PM1_CNT_SCI_EN, pm1_cnt);
364 * QEMU's version of write_acpi_tables is defined in drivers/misc/qfw.c
366 ulong write_acpi_tables(ulong start)
369 struct acpi_rsdp *rsdp;
370 struct acpi_rsdt *rsdt;
371 struct acpi_xsdt *xsdt;
372 struct acpi_facs *facs;
373 struct acpi_table_header *dsdt;
374 struct acpi_fadt *fadt;
375 struct acpi_mcfg *mcfg;
376 struct acpi_madt *madt;
381 /* Align ACPI tables to 16 byte */
382 current = ALIGN(current, 16);
384 debug("ACPI: Writing ACPI tables at %lx\n", start);
386 /* We need at least an RSDP and an RSDT Table */
387 rsdp = (struct acpi_rsdp *)current;
388 current += sizeof(struct acpi_rsdp);
389 current = ALIGN(current, 16);
390 rsdt = (struct acpi_rsdt *)current;
391 current += sizeof(struct acpi_rsdt);
392 current = ALIGN(current, 16);
393 xsdt = (struct acpi_xsdt *)current;
394 current += sizeof(struct acpi_xsdt);
396 * Per ACPI spec, the FACS table address must be aligned to a 64 byte
397 * boundary (Windows checks this, but Linux does not).
399 current = ALIGN(current, 64);
401 /* clear all table memory */
402 memset((void *)start, 0, current - start);
404 acpi_write_rsdp(rsdp, rsdt, xsdt);
405 acpi_write_rsdt(rsdt);
406 acpi_write_xsdt(xsdt);
408 debug("ACPI: * FACS\n");
409 facs = (struct acpi_facs *)current;
410 current += sizeof(struct acpi_facs);
411 current = ALIGN(current, 16);
413 acpi_create_facs(facs);
415 debug("ACPI: * DSDT\n");
416 dsdt = (struct acpi_table_header *)current;
417 memcpy(dsdt, &AmlCode, sizeof(struct acpi_table_header));
418 current += sizeof(struct acpi_table_header);
419 memcpy((char *)current,
420 (char *)&AmlCode + sizeof(struct acpi_table_header),
421 dsdt->length - sizeof(struct acpi_table_header));
422 current += dsdt->length - sizeof(struct acpi_table_header);
423 current = ALIGN(current, 16);
425 /* Pack GNVS into the ACPI table area */
426 for (i = 0; i < dsdt->length; i++) {
427 u32 *gnvs = (u32 *)((u32)dsdt + i);
428 if (*gnvs == ACPI_GNVS_ADDR) {
429 debug("Fix up global NVS in DSDT to 0x%08x\n", current);
435 /* Update DSDT checksum since we patched the GNVS address */
437 dsdt->checksum = table_compute_checksum((void *)dsdt, dsdt->length);
439 /* Fill in platform-specific global NVS variables */
440 acpi_create_gnvs((struct acpi_global_nvs *)current);
441 current += sizeof(struct acpi_global_nvs);
442 current = ALIGN(current, 16);
444 debug("ACPI: * FADT\n");
445 fadt = (struct acpi_fadt *)current;
446 current += sizeof(struct acpi_fadt);
447 current = ALIGN(current, 16);
448 acpi_create_fadt(fadt, facs, dsdt);
449 acpi_add_table(rsdp, fadt);
451 debug("ACPI: * MADT\n");
452 madt = (struct acpi_madt *)current;
453 acpi_create_madt(madt);
454 current += madt->header.length;
455 acpi_add_table(rsdp, madt);
456 current = ALIGN(current, 16);
458 debug("ACPI: * MCFG\n");
459 mcfg = (struct acpi_mcfg *)current;
460 acpi_create_mcfg(mcfg);
461 current += mcfg->header.length;
462 acpi_add_table(rsdp, mcfg);
463 current = ALIGN(current, 16);
465 debug("current = %x\n", current);
467 acpi_rsdp_addr = (unsigned long)rsdp;
468 debug("ACPI: done\n");
470 /* Don't touch ACPI hardware on HW reduced platforms */
471 if (fadt->flags & ACPI_FADT_HW_REDUCED_ACPI)
475 * Other than waiting for OSPM to request us to switch to ACPI mode,
476 * do it by ourselves, since SMI will not be triggered.
478 enter_acpi_mode(fadt->pm1a_cnt_blk);
483 static struct acpi_rsdp *acpi_valid_rsdp(struct acpi_rsdp *rsdp)
485 if (strncmp((char *)rsdp, RSDP_SIG, sizeof(RSDP_SIG) - 1) != 0)
488 debug("Looking on %p for valid checksum\n", rsdp);
490 if (table_compute_checksum((void *)rsdp, 20) != 0)
492 debug("acpi rsdp checksum 1 passed\n");
494 if ((rsdp->revision > 1) &&
495 (table_compute_checksum((void *)rsdp, rsdp->length) != 0))
497 debug("acpi rsdp checksum 2 passed\n");
502 struct acpi_fadt *acpi_find_fadt(void)
505 struct acpi_rsdp *rsdp = NULL;
506 struct acpi_rsdt *rsdt;
507 struct acpi_fadt *fadt = NULL;
511 for (p = (char *)ROM_TABLE_ADDR; p < (char *)ROM_TABLE_END; p += 16) {
512 rsdp = acpi_valid_rsdp((struct acpi_rsdp *)p);
520 debug("RSDP found at %p\n", rsdp);
521 rsdt = (struct acpi_rsdt *)rsdp->rsdt_address;
523 end = (char *)rsdt + rsdt->header.length;
524 debug("RSDT found at %p ends at %p\n", rsdt, end);
526 for (i = 0; ((char *)&rsdt->entry[i]) < end; i++) {
527 fadt = (struct acpi_fadt *)rsdt->entry[i];
528 if (strncmp((char *)fadt, "FACP", 4) == 0)
536 debug("FADT found at %p\n", fadt);
540 void *acpi_find_wakeup_vector(struct acpi_fadt *fadt)
542 struct acpi_facs *facs;
545 debug("Trying to find the wakeup vector...\n");
547 facs = (struct acpi_facs *)fadt->firmware_ctrl;
550 debug("No FACS found, wake up from S3 not possible.\n");
554 debug("FACS found at %p\n", facs);
555 wake_vec = (void *)facs->firmware_waking_vector;
556 debug("OS waking vector is %p\n", wake_vec);