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/lapic.h>
19 #include <asm/tables.h>
20 #include <asm/arch/global_nvs.h>
23 * IASL compiles the dsdt entries and writes the hex values
24 * to a C array AmlCode[] (see dsdt.c).
26 extern const unsigned char AmlCode[];
28 static void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt,
29 struct acpi_xsdt *xsdt)
31 memset(rsdp, 0, sizeof(struct acpi_rsdp));
33 memcpy(rsdp->signature, RSDP_SIG, 8);
34 memcpy(rsdp->oem_id, OEM_ID, 6);
36 rsdp->length = sizeof(struct acpi_rsdp);
37 rsdp->rsdt_address = (u32)rsdt;
40 * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2
42 * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
43 * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
47 rsdp->revision = ACPI_RSDP_REV_ACPI_1_0;
49 rsdp->xsdt_address = (u64)(u32)xsdt;
50 rsdp->revision = ACPI_RSDP_REV_ACPI_2_0;
53 /* Calculate checksums */
54 rsdp->checksum = table_compute_checksum((void *)rsdp, 20);
55 rsdp->ext_checksum = table_compute_checksum((void *)rsdp,
56 sizeof(struct acpi_rsdp));
59 void acpi_fill_header(struct acpi_table_header *header, char *signature)
61 memcpy(header->signature, signature, 4);
62 memcpy(header->oem_id, OEM_ID, 6);
63 memcpy(header->oem_table_id, OEM_TABLE_ID, 8);
64 header->oem_revision = U_BOOT_BUILD_DATE;
65 memcpy(header->aslc_id, ASLC_ID, 4);
68 static void acpi_write_rsdt(struct acpi_rsdt *rsdt)
70 struct acpi_table_header *header = &(rsdt->header);
72 /* Fill out header fields */
73 acpi_fill_header(header, "RSDT");
74 header->length = sizeof(struct acpi_rsdt);
77 /* Entries are filled in later, we come with an empty set */
80 header->checksum = table_compute_checksum((void *)rsdt,
81 sizeof(struct acpi_rsdt));
84 static void acpi_write_xsdt(struct acpi_xsdt *xsdt)
86 struct acpi_table_header *header = &(xsdt->header);
88 /* Fill out header fields */
89 acpi_fill_header(header, "XSDT");
90 header->length = sizeof(struct acpi_xsdt);
93 /* Entries are filled in later, we come with an empty set */
96 header->checksum = table_compute_checksum((void *)xsdt,
97 sizeof(struct acpi_xsdt));
101 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
104 static void acpi_add_table(struct acpi_rsdp *rsdp, void *table)
107 struct acpi_rsdt *rsdt;
108 struct acpi_xsdt *xsdt = NULL;
110 /* The RSDT is mandatory while the XSDT is not */
111 rsdt = (struct acpi_rsdt *)rsdp->rsdt_address;
113 if (rsdp->xsdt_address)
114 xsdt = (struct acpi_xsdt *)((u32)rsdp->xsdt_address);
116 /* This should always be MAX_ACPI_TABLES */
117 entries_num = ARRAY_SIZE(rsdt->entry);
119 for (i = 0; i < entries_num; i++) {
120 if (rsdt->entry[i] == 0)
124 if (i >= entries_num) {
125 debug("ACPI: Error: too many tables\n");
129 /* Add table to the RSDT */
130 rsdt->entry[i] = (u32)table;
132 /* Fix RSDT length or the kernel will assume invalid entries */
133 rsdt->header.length = sizeof(struct acpi_table_header) +
134 (sizeof(u32) * (i + 1));
136 /* Re-calculate checksum */
137 rsdt->header.checksum = 0;
138 rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
139 rsdt->header.length);
142 * And now the same thing for the XSDT. We use the same index as for
143 * now we want the XSDT and RSDT to always be in sync in U-Boot
146 /* Add table to the XSDT */
147 xsdt->entry[i] = (u64)(u32)table;
149 /* Fix XSDT length */
150 xsdt->header.length = sizeof(struct acpi_table_header) +
151 (sizeof(u64) * (i + 1));
153 /* Re-calculate checksum */
154 xsdt->header.checksum = 0;
155 xsdt->header.checksum = table_compute_checksum((u8 *)xsdt,
156 xsdt->header.length);
160 static void acpi_create_facs(struct acpi_facs *facs)
162 memset((void *)facs, 0, sizeof(struct acpi_facs));
164 memcpy(facs->signature, "FACS", 4);
165 facs->length = sizeof(struct acpi_facs);
166 facs->hardware_signature = 0;
167 facs->firmware_waking_vector = 0;
168 facs->global_lock = 0;
170 facs->x_firmware_waking_vector_l = 0;
171 facs->x_firmware_waking_vector_h = 0;
175 static int acpi_create_madt_lapic(struct acpi_madt_lapic *lapic,
178 lapic->type = ACPI_APIC_LAPIC;
179 lapic->length = sizeof(struct acpi_madt_lapic);
180 lapic->flags = LOCAL_APIC_FLAG_ENABLED;
181 lapic->processor_id = cpu;
182 lapic->apic_id = apic;
184 return lapic->length;
187 int acpi_create_madt_lapics(u32 current)
190 int total_length = 0;
192 for (uclass_find_first_device(UCLASS_CPU, &dev);
194 uclass_find_next_device(&dev)) {
195 struct cpu_platdata *plat = dev_get_parent_platdata(dev);
196 int length = acpi_create_madt_lapic(
197 (struct acpi_madt_lapic *)current,
198 plat->cpu_id, plat->cpu_id);
200 total_length += length;
206 int acpi_create_madt_ioapic(struct acpi_madt_ioapic *ioapic, u8 id,
207 u32 addr, u32 gsi_base)
209 ioapic->type = ACPI_APIC_IOAPIC;
210 ioapic->length = sizeof(struct acpi_madt_ioapic);
211 ioapic->reserved = 0x00;
212 ioapic->gsi_base = gsi_base;
213 ioapic->ioapic_id = id;
214 ioapic->ioapic_addr = addr;
216 return ioapic->length;
219 int acpi_create_madt_irqoverride(struct acpi_madt_irqoverride *irqoverride,
220 u8 bus, u8 source, u32 gsirq, u16 flags)
222 irqoverride->type = ACPI_APIC_IRQ_SRC_OVERRIDE;
223 irqoverride->length = sizeof(struct acpi_madt_irqoverride);
224 irqoverride->bus = bus;
225 irqoverride->source = source;
226 irqoverride->gsirq = gsirq;
227 irqoverride->flags = flags;
229 return irqoverride->length;
232 int acpi_create_madt_lapic_nmi(struct acpi_madt_lapic_nmi *lapic_nmi,
233 u8 cpu, u16 flags, u8 lint)
235 lapic_nmi->type = ACPI_APIC_LAPIC_NMI;
236 lapic_nmi->length = sizeof(struct acpi_madt_lapic_nmi);
237 lapic_nmi->flags = flags;
238 lapic_nmi->processor_id = cpu;
239 lapic_nmi->lint = lint;
241 return lapic_nmi->length;
244 static void acpi_create_madt(struct acpi_madt *madt)
246 struct acpi_table_header *header = &(madt->header);
247 u32 current = (u32)madt + sizeof(struct acpi_madt);
249 memset((void *)madt, 0, sizeof(struct acpi_madt));
251 /* Fill out header fields */
252 acpi_fill_header(header, "APIC");
253 header->length = sizeof(struct acpi_madt);
254 header->revision = 4;
256 madt->lapic_addr = LAPIC_DEFAULT_BASE;
257 madt->flags = ACPI_MADT_PCAT_COMPAT;
259 current = acpi_fill_madt(current);
261 /* (Re)calculate length and checksum */
262 header->length = current - (u32)madt;
264 header->checksum = table_compute_checksum((void *)madt, header->length);
267 static int acpi_create_mcfg_mmconfig(struct acpi_mcfg_mmconfig *mmconfig,
268 u32 base, u16 seg_nr, u8 start, u8 end)
270 memset(mmconfig, 0, sizeof(*mmconfig));
271 mmconfig->base_address_l = base;
272 mmconfig->base_address_h = 0;
273 mmconfig->pci_segment_group_number = seg_nr;
274 mmconfig->start_bus_number = start;
275 mmconfig->end_bus_number = end;
277 return sizeof(struct acpi_mcfg_mmconfig);
280 static u32 acpi_fill_mcfg(u32 current)
282 current += acpi_create_mcfg_mmconfig
283 ((struct acpi_mcfg_mmconfig *)current,
284 CONFIG_PCIE_ECAM_BASE, 0x0, 0x0, 255);
289 /* MCFG is defined in the PCI Firmware Specification 3.0 */
290 static void acpi_create_mcfg(struct acpi_mcfg *mcfg)
292 struct acpi_table_header *header = &(mcfg->header);
293 u32 current = (u32)mcfg + sizeof(struct acpi_mcfg);
295 memset((void *)mcfg, 0, sizeof(struct acpi_mcfg));
297 /* Fill out header fields */
298 acpi_fill_header(header, "MCFG");
299 header->length = sizeof(struct acpi_mcfg);
300 header->revision = 1;
302 current = acpi_fill_mcfg(current);
304 /* (Re)calculate length and checksum */
305 header->length = current - (u32)mcfg;
306 header->checksum = table_compute_checksum((void *)mcfg, header->length);
309 void enter_acpi_mode(int pm1_cnt)
311 u16 val = inw(pm1_cnt);
314 * PM1_CNT register bit0 selects the power management event to be
315 * either an SCI or SMI interrupt. When this bit is set, then power
316 * management events will generate an SCI interrupt. When this bit
317 * is reset power management events will generate an SMI interrupt.
319 * Per ACPI spec, it is the responsibility of the hardware to set
320 * or reset this bit. OSPM always preserves this bit position.
322 * U-Boot does not support SMI. And we don't have plan to support
323 * anything running in SMM within U-Boot. To create a legacy-free
324 * system, and expose ourselves to OSPM as working under ACPI mode
325 * already, turn this bit on.
327 outw(val | PM1_CNT_SCI_EN, pm1_cnt);
331 * QEMU's version of write_acpi_tables is defined in
332 * arch/x86/cpu/qemu/acpi_table.c
334 ulong write_acpi_tables(ulong start)
337 struct acpi_rsdp *rsdp;
338 struct acpi_rsdt *rsdt;
339 struct acpi_xsdt *xsdt;
340 struct acpi_facs *facs;
341 struct acpi_table_header *dsdt;
342 struct acpi_fadt *fadt;
343 struct acpi_mcfg *mcfg;
344 struct acpi_madt *madt;
349 /* Align ACPI tables to 16 byte */
350 current = ALIGN(current, 16);
352 debug("ACPI: Writing ACPI tables at %lx\n", start);
354 /* We need at least an RSDP and an RSDT Table */
355 rsdp = (struct acpi_rsdp *)current;
356 current += sizeof(struct acpi_rsdp);
357 current = ALIGN(current, 16);
358 rsdt = (struct acpi_rsdt *)current;
359 current += sizeof(struct acpi_rsdt);
360 current = ALIGN(current, 16);
361 xsdt = (struct acpi_xsdt *)current;
362 current += sizeof(struct acpi_xsdt);
364 * Per ACPI spec, the FACS table address must be aligned to a 64 byte
365 * boundary (Windows checks this, but Linux does not).
367 current = ALIGN(current, 64);
369 /* clear all table memory */
370 memset((void *)start, 0, current - start);
372 acpi_write_rsdp(rsdp, rsdt, xsdt);
373 acpi_write_rsdt(rsdt);
374 acpi_write_xsdt(xsdt);
376 debug("ACPI: * FACS\n");
377 facs = (struct acpi_facs *)current;
378 current += sizeof(struct acpi_facs);
379 current = ALIGN(current, 16);
381 acpi_create_facs(facs);
383 debug("ACPI: * DSDT\n");
384 dsdt = (struct acpi_table_header *)current;
385 memcpy(dsdt, &AmlCode, sizeof(struct acpi_table_header));
386 current += sizeof(struct acpi_table_header);
387 memcpy((char *)current,
388 (char *)&AmlCode + sizeof(struct acpi_table_header),
389 dsdt->length - sizeof(struct acpi_table_header));
390 current += dsdt->length - sizeof(struct acpi_table_header);
391 current = ALIGN(current, 16);
393 /* Pack GNVS into the ACPI table area */
394 for (i = 0; i < dsdt->length; i++) {
395 u32 *gnvs = (u32 *)((u32)dsdt + i);
396 if (*gnvs == ACPI_GNVS_ADDR) {
397 debug("Fix up global NVS in DSDT to 0x%08x\n", current);
403 /* Update DSDT checksum since we patched the GNVS address */
405 dsdt->checksum = table_compute_checksum((void *)dsdt, dsdt->length);
407 /* Fill in platform-specific global NVS variables */
408 acpi_create_gnvs((struct acpi_global_nvs *)current);
409 current += sizeof(struct acpi_global_nvs);
410 current = ALIGN(current, 16);
412 debug("ACPI: * FADT\n");
413 fadt = (struct acpi_fadt *)current;
414 current += sizeof(struct acpi_fadt);
415 current = ALIGN(current, 16);
416 acpi_create_fadt(fadt, facs, dsdt);
417 acpi_add_table(rsdp, fadt);
419 debug("ACPI: * MADT\n");
420 madt = (struct acpi_madt *)current;
421 acpi_create_madt(madt);
422 current += madt->header.length;
423 acpi_add_table(rsdp, madt);
424 current = ALIGN(current, 16);
426 debug("ACPI: * MCFG\n");
427 mcfg = (struct acpi_mcfg *)current;
428 acpi_create_mcfg(mcfg);
429 current += mcfg->header.length;
430 acpi_add_table(rsdp, mcfg);
431 current = ALIGN(current, 16);
433 debug("current = %x\n", current);
435 debug("ACPI: done\n");
438 * Other than waiting for OSPM to request us to switch to ACPI mode,
439 * do it by ourselves, since SMI will not be triggered.
441 enter_acpi_mode(fadt->pm1a_cnt_blk);
446 static struct acpi_rsdp *acpi_valid_rsdp(struct acpi_rsdp *rsdp)
448 if (strncmp((char *)rsdp, RSDP_SIG, sizeof(RSDP_SIG) - 1) != 0)
451 debug("Looking on %p for valid checksum\n", rsdp);
453 if (table_compute_checksum((void *)rsdp, 20) != 0)
455 debug("acpi rsdp checksum 1 passed\n");
457 if ((rsdp->revision > 1) &&
458 (table_compute_checksum((void *)rsdp, rsdp->length) != 0))
460 debug("acpi rsdp checksum 2 passed\n");
465 struct acpi_fadt *acpi_find_fadt(void)
468 struct acpi_rsdp *rsdp = NULL;
469 struct acpi_rsdt *rsdt;
470 struct acpi_fadt *fadt = NULL;
474 for (p = (char *)ROM_TABLE_ADDR; p < (char *)ROM_TABLE_END; p += 16) {
475 rsdp = acpi_valid_rsdp((struct acpi_rsdp *)p);
483 debug("RSDP found at %p\n", rsdp);
484 rsdt = (struct acpi_rsdt *)rsdp->rsdt_address;
486 end = (char *)rsdt + rsdt->header.length;
487 debug("RSDT found at %p ends at %p\n", rsdt, end);
489 for (i = 0; ((char *)&rsdt->entry[i]) < end; i++) {
490 fadt = (struct acpi_fadt *)rsdt->entry[i];
491 if (strncmp((char *)fadt, "FACP", 4) == 0)
499 debug("FADT found at %p\n", fadt);
503 void *acpi_find_wakeup_vector(struct acpi_fadt *fadt)
505 struct acpi_facs *facs;
508 debug("Trying to find the wakeup vector...\n");
510 facs = (struct acpi_facs *)fadt->firmware_ctrl;
513 debug("No FACS found, wake up from S3 not possible.\n");
517 debug("FACS found at %p\n", facs);
518 wake_vec = (void *)facs->firmware_waking_vector;
519 debug("OS waking vector is %p\n", wake_vec);