1 // SPDX-License-Identifier: GPL-2.0+
3 * Based on acpi.c from coreboot
5 * Copyright (C) 2015, Saket Sinha <saket.sinha89@gmail.com>
6 * Copyright (C) 2016, Bin Meng <bmeng.cn@gmail.com>
12 #include <dm/uclass-internal.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>
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 /* ACPI RSDP address to be used in boot parameters */
29 static ulong acpi_rsdp_addr;
31 static void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt,
32 struct acpi_xsdt *xsdt)
34 memset(rsdp, 0, sizeof(struct acpi_rsdp));
36 memcpy(rsdp->signature, RSDP_SIG, 8);
37 memcpy(rsdp->oem_id, OEM_ID, 6);
39 rsdp->length = sizeof(struct acpi_rsdp);
40 rsdp->rsdt_address = (u32)rsdt;
43 * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2
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
50 rsdp->revision = ACPI_RSDP_REV_ACPI_1_0;
52 rsdp->xsdt_address = (u64)(u32)xsdt;
53 rsdp->revision = ACPI_RSDP_REV_ACPI_2_0;
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));
62 void acpi_fill_header(struct acpi_table_header *header, char *signature)
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);
71 static void acpi_write_rsdt(struct acpi_rsdt *rsdt)
73 struct acpi_table_header *header = &(rsdt->header);
75 /* Fill out header fields */
76 acpi_fill_header(header, "RSDT");
77 header->length = sizeof(struct acpi_rsdt);
80 /* Entries are filled in later, we come with an empty set */
83 header->checksum = table_compute_checksum((void *)rsdt,
84 sizeof(struct acpi_rsdt));
87 static void acpi_write_xsdt(struct acpi_xsdt *xsdt)
89 struct acpi_table_header *header = &(xsdt->header);
91 /* Fill out header fields */
92 acpi_fill_header(header, "XSDT");
93 header->length = sizeof(struct acpi_xsdt);
96 /* Entries are filled in later, we come with an empty set */
99 header->checksum = table_compute_checksum((void *)xsdt,
100 sizeof(struct acpi_xsdt));
104 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
107 static void acpi_add_table(struct acpi_rsdp *rsdp, void *table)
110 struct acpi_rsdt *rsdt;
111 struct acpi_xsdt *xsdt = NULL;
113 /* The RSDT is mandatory while the XSDT is not */
114 rsdt = (struct acpi_rsdt *)rsdp->rsdt_address;
116 if (rsdp->xsdt_address)
117 xsdt = (struct acpi_xsdt *)((u32)rsdp->xsdt_address);
119 /* This should always be MAX_ACPI_TABLES */
120 entries_num = ARRAY_SIZE(rsdt->entry);
122 for (i = 0; i < entries_num; i++) {
123 if (rsdt->entry[i] == 0)
127 if (i >= entries_num) {
128 debug("ACPI: Error: too many tables\n");
132 /* Add table to the RSDT */
133 rsdt->entry[i] = (u32)table;
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));
139 /* Re-calculate checksum */
140 rsdt->header.checksum = 0;
141 rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
142 rsdt->header.length);
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
149 /* Add table to the XSDT */
150 xsdt->entry[i] = (u64)(u32)table;
152 /* Fix XSDT length */
153 xsdt->header.length = sizeof(struct acpi_table_header) +
154 (sizeof(u64) * (i + 1));
156 /* Re-calculate checksum */
157 xsdt->header.checksum = 0;
158 xsdt->header.checksum = table_compute_checksum((u8 *)xsdt,
159 xsdt->header.length);
163 static void acpi_create_facs(struct acpi_facs *facs)
165 memset((void *)facs, 0, sizeof(struct acpi_facs));
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;
173 facs->x_firmware_waking_vector_l = 0;
174 facs->x_firmware_waking_vector_h = 0;
178 static int acpi_create_madt_lapic(struct acpi_madt_lapic *lapic,
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;
187 return lapic->length;
190 int acpi_create_madt_lapics(u32 current)
193 int total_length = 0;
195 for (uclass_find_first_device(UCLASS_CPU, &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);
203 total_length += length;
209 int acpi_create_madt_ioapic(struct acpi_madt_ioapic *ioapic, u8 id,
210 u32 addr, u32 gsi_base)
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;
219 return ioapic->length;
222 int acpi_create_madt_irqoverride(struct acpi_madt_irqoverride *irqoverride,
223 u8 bus, u8 source, u32 gsirq, u16 flags)
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;
232 return irqoverride->length;
235 int acpi_create_madt_lapic_nmi(struct acpi_madt_lapic_nmi *lapic_nmi,
236 u8 cpu, u16 flags, u8 lint)
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;
244 return lapic_nmi->length;
247 static int acpi_create_madt_irq_overrides(u32 current)
249 struct acpi_madt_irqoverride *irqovr;
250 u16 sci_flags = MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_HIGH;
253 irqovr = (void *)current;
254 length += acpi_create_madt_irqoverride(irqovr, 0, 0, 2, 0);
256 irqovr = (void *)(current + length);
257 length += acpi_create_madt_irqoverride(irqovr, 0, 9, 9, sci_flags);
262 __weak u32 acpi_fill_madt(u32 current)
264 current += acpi_create_madt_lapics(current);
266 current += acpi_create_madt_ioapic((struct acpi_madt_ioapic *)current,
267 io_apic_read(IO_APIC_ID) >> 24, IO_APIC_ADDR, 0);
269 current += acpi_create_madt_irq_overrides(current);
274 static void acpi_create_madt(struct acpi_madt *madt)
276 struct acpi_table_header *header = &(madt->header);
277 u32 current = (u32)madt + sizeof(struct acpi_madt);
279 memset((void *)madt, 0, sizeof(struct acpi_madt));
281 /* Fill out header fields */
282 acpi_fill_header(header, "APIC");
283 header->length = sizeof(struct acpi_madt);
284 header->revision = 4;
286 madt->lapic_addr = LAPIC_DEFAULT_BASE;
287 madt->flags = ACPI_MADT_PCAT_COMPAT;
289 current = acpi_fill_madt(current);
291 /* (Re)calculate length and checksum */
292 header->length = current - (u32)madt;
294 header->checksum = table_compute_checksum((void *)madt, header->length);
297 int acpi_create_mcfg_mmconfig(struct acpi_mcfg_mmconfig *mmconfig, u32 base,
298 u16 seg_nr, u8 start, u8 end)
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;
307 return sizeof(struct acpi_mcfg_mmconfig);
310 __weak u32 acpi_fill_mcfg(u32 current)
312 current += acpi_create_mcfg_mmconfig
313 ((struct acpi_mcfg_mmconfig *)current,
314 CONFIG_PCIE_ECAM_BASE, 0x0, 0x0, 255);
319 /* MCFG is defined in the PCI Firmware Specification 3.0 */
320 static void acpi_create_mcfg(struct acpi_mcfg *mcfg)
322 struct acpi_table_header *header = &(mcfg->header);
323 u32 current = (u32)mcfg + sizeof(struct acpi_mcfg);
325 memset((void *)mcfg, 0, sizeof(struct acpi_mcfg));
327 /* Fill out header fields */
328 acpi_fill_header(header, "MCFG");
329 header->length = sizeof(struct acpi_mcfg);
330 header->revision = 1;
332 current = acpi_fill_mcfg(current);
334 /* (Re)calculate length and checksum */
335 header->length = current - (u32)mcfg;
336 header->checksum = table_compute_checksum((void *)mcfg, header->length);
340 * QEMU's version of write_acpi_tables is defined in drivers/misc/qfw.c
342 ulong write_acpi_tables(ulong start)
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;
357 /* Align ACPI tables to 16 byte */
358 current = ALIGN(current, 16);
360 debug("ACPI: Writing ACPI tables at %lx\n", start);
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);
372 * Per ACPI spec, the FACS table address must be aligned to a 64 byte
373 * boundary (Windows checks this, but Linux does not).
375 current = ALIGN(current, 64);
377 /* clear all table memory */
378 memset((void *)start, 0, current - start);
380 acpi_write_rsdp(rsdp, rsdt, xsdt);
381 acpi_write_rsdt(rsdt);
382 acpi_write_xsdt(xsdt);
384 debug("ACPI: * FACS\n");
385 facs = (struct acpi_facs *)current;
386 current += sizeof(struct acpi_facs);
387 current = ALIGN(current, 16);
389 acpi_create_facs(facs);
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);
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);
411 /* Update DSDT checksum since we patched the GNVS address */
413 dsdt->checksum = table_compute_checksum((void *)dsdt, dsdt->length);
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);
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);
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);
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);
441 debug("current = %x\n", current);
443 acpi_rsdp_addr = (unsigned long)rsdp;
444 debug("ACPI: done\n");
449 ulong acpi_get_rsdp_addr(void)
451 return acpi_rsdp_addr;