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>
14 #include <asm/acpi_table.h>
16 #include <asm/lapic.h>
17 #include <asm/tables.h>
20 * IASL compiles the dsdt entries and writes the hex values
21 * to a C array AmlCode[] (see dsdt.c).
23 extern const unsigned char AmlCode[];
25 static void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt,
26 struct acpi_xsdt *xsdt)
28 memset(rsdp, 0, sizeof(struct acpi_rsdp));
30 memcpy(rsdp->signature, RSDP_SIG, 8);
31 memcpy(rsdp->oem_id, OEM_ID, 6);
33 rsdp->length = sizeof(struct acpi_rsdp);
34 rsdp->rsdt_address = (u32)rsdt;
37 * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2
39 * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
40 * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
44 rsdp->revision = ACPI_RSDP_REV_ACPI_1_0;
46 rsdp->xsdt_address = (u64)(u32)xsdt;
47 rsdp->revision = ACPI_RSDP_REV_ACPI_2_0;
50 /* Calculate checksums */
51 rsdp->checksum = table_compute_checksum((void *)rsdp, 20);
52 rsdp->ext_checksum = table_compute_checksum((void *)rsdp,
53 sizeof(struct acpi_rsdp));
56 void acpi_fill_header(struct acpi_table_header *header, char *signature)
58 memcpy(header->signature, signature, 4);
59 memcpy(header->oem_id, OEM_ID, 6);
60 memcpy(header->oem_table_id, OEM_TABLE_ID, 8);
61 memcpy(header->aslc_id, ASLC_ID, 4);
64 static void acpi_write_rsdt(struct acpi_rsdt *rsdt)
66 struct acpi_table_header *header = &(rsdt->header);
68 /* Fill out header fields */
69 acpi_fill_header(header, "RSDT");
70 header->length = sizeof(struct acpi_rsdt);
73 /* Entries are filled in later, we come with an empty set */
76 header->checksum = table_compute_checksum((void *)rsdt,
77 sizeof(struct acpi_rsdt));
80 static void acpi_write_xsdt(struct acpi_xsdt *xsdt)
82 struct acpi_table_header *header = &(xsdt->header);
84 /* Fill out header fields */
85 acpi_fill_header(header, "XSDT");
86 header->length = sizeof(struct acpi_xsdt);
89 /* Entries are filled in later, we come with an empty set */
92 header->checksum = table_compute_checksum((void *)xsdt,
93 sizeof(struct acpi_xsdt));
97 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
100 static void acpi_add_table(struct acpi_rsdp *rsdp, void *table)
103 struct acpi_rsdt *rsdt;
104 struct acpi_xsdt *xsdt = NULL;
106 /* The RSDT is mandatory while the XSDT is not */
107 rsdt = (struct acpi_rsdt *)rsdp->rsdt_address;
109 if (rsdp->xsdt_address)
110 xsdt = (struct acpi_xsdt *)((u32)rsdp->xsdt_address);
112 /* This should always be MAX_ACPI_TABLES */
113 entries_num = ARRAY_SIZE(rsdt->entry);
115 for (i = 0; i < entries_num; i++) {
116 if (rsdt->entry[i] == 0)
120 if (i >= entries_num) {
121 debug("ACPI: Error: too many tables\n");
125 /* Add table to the RSDT */
126 rsdt->entry[i] = (u32)table;
128 /* Fix RSDT length or the kernel will assume invalid entries */
129 rsdt->header.length = sizeof(struct acpi_table_header) +
130 (sizeof(u32) * (i + 1));
132 /* Re-calculate checksum */
133 rsdt->header.checksum = 0;
134 rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
135 rsdt->header.length);
138 * And now the same thing for the XSDT. We use the same index as for
139 * now we want the XSDT and RSDT to always be in sync in U-Boot
142 /* Add table to the XSDT */
143 xsdt->entry[i] = (u64)(u32)table;
145 /* Fix XSDT length */
146 xsdt->header.length = sizeof(struct acpi_table_header) +
147 (sizeof(u64) * (i + 1));
149 /* Re-calculate checksum */
150 xsdt->header.checksum = 0;
151 xsdt->header.checksum = table_compute_checksum((u8 *)xsdt,
152 xsdt->header.length);
156 static void acpi_create_facs(struct acpi_facs *facs)
158 memset((void *)facs, 0, sizeof(struct acpi_facs));
160 memcpy(facs->signature, "FACS", 4);
161 facs->length = sizeof(struct acpi_facs);
162 facs->hardware_signature = 0;
163 facs->firmware_waking_vector = 0;
164 facs->global_lock = 0;
166 facs->x_firmware_waking_vector_l = 0;
167 facs->x_firmware_waking_vector_h = 0;
171 static int acpi_create_madt_lapic(struct acpi_madt_lapic *lapic,
174 lapic->type = ACPI_APIC_LAPIC;
175 lapic->length = sizeof(struct acpi_madt_lapic);
176 lapic->flags = LOCAL_APIC_FLAG_ENABLED;
177 lapic->processor_id = cpu;
178 lapic->apic_id = apic;
180 return lapic->length;
183 int acpi_create_madt_lapics(u32 current)
188 for (uclass_find_first_device(UCLASS_CPU, &dev);
190 uclass_find_next_device(&dev)) {
191 struct cpu_platdata *plat = dev_get_parent_platdata(dev);
193 length += acpi_create_madt_lapic(
194 (struct acpi_madt_lapic *)current,
195 plat->cpu_id, plat->cpu_id);
202 int acpi_create_madt_ioapic(struct acpi_madt_ioapic *ioapic, u8 id,
203 u32 addr, u32 gsi_base)
205 ioapic->type = ACPI_APIC_IOAPIC;
206 ioapic->length = sizeof(struct acpi_madt_ioapic);
207 ioapic->reserved = 0x00;
208 ioapic->gsi_base = gsi_base;
209 ioapic->ioapic_id = id;
210 ioapic->ioapic_addr = addr;
212 return ioapic->length;
215 int acpi_create_madt_irqoverride(struct acpi_madt_irqoverride *irqoverride,
216 u8 bus, u8 source, u32 gsirq, u16 flags)
218 irqoverride->type = ACPI_APIC_IRQ_SRC_OVERRIDE;
219 irqoverride->length = sizeof(struct acpi_madt_irqoverride);
220 irqoverride->bus = bus;
221 irqoverride->source = source;
222 irqoverride->gsirq = gsirq;
223 irqoverride->flags = flags;
225 return irqoverride->length;
228 int acpi_create_madt_lapic_nmi(struct acpi_madt_lapic_nmi *lapic_nmi,
229 u8 cpu, u16 flags, u8 lint)
231 lapic_nmi->type = ACPI_APIC_LAPIC_NMI;
232 lapic_nmi->length = sizeof(struct acpi_madt_lapic_nmi);
233 lapic_nmi->flags = flags;
234 lapic_nmi->processor_id = cpu;
235 lapic_nmi->lint = lint;
237 return lapic_nmi->length;
240 static void acpi_create_madt(struct acpi_madt *madt)
242 struct acpi_table_header *header = &(madt->header);
243 u32 current = (u32)madt + sizeof(struct acpi_madt);
245 memset((void *)madt, 0, sizeof(struct acpi_madt));
247 /* Fill out header fields */
248 acpi_fill_header(header, "APIC");
249 header->length = sizeof(struct acpi_madt);
250 header->revision = 4;
252 madt->lapic_addr = LAPIC_DEFAULT_BASE;
253 madt->flags = ACPI_MADT_PCAT_COMPAT;
255 current = acpi_fill_madt(current);
257 /* (Re)calculate length and checksum */
258 header->length = current - (u32)madt;
260 header->checksum = table_compute_checksum((void *)madt, header->length);
263 static int acpi_create_mcfg_mmconfig(struct acpi_mcfg_mmconfig *mmconfig,
264 u32 base, u16 seg_nr, u8 start, u8 end)
266 memset(mmconfig, 0, sizeof(*mmconfig));
267 mmconfig->base_address_l = base;
268 mmconfig->base_address_h = 0;
269 mmconfig->pci_segment_group_number = seg_nr;
270 mmconfig->start_bus_number = start;
271 mmconfig->end_bus_number = end;
273 return sizeof(struct acpi_mcfg_mmconfig);
276 static u32 acpi_fill_mcfg(u32 current)
278 current += acpi_create_mcfg_mmconfig
279 ((struct acpi_mcfg_mmconfig *)current,
280 CONFIG_PCIE_ECAM_BASE, 0x0, 0x0, 255);
285 /* MCFG is defined in the PCI Firmware Specification 3.0 */
286 static void acpi_create_mcfg(struct acpi_mcfg *mcfg)
288 struct acpi_table_header *header = &(mcfg->header);
289 u32 current = (u32)mcfg + sizeof(struct acpi_mcfg);
291 memset((void *)mcfg, 0, sizeof(struct acpi_mcfg));
293 /* Fill out header fields */
294 acpi_fill_header(header, "MCFG");
295 header->length = sizeof(struct acpi_mcfg);
296 header->revision = 1;
298 current = acpi_fill_mcfg(current);
300 /* (Re)calculate length and checksum */
301 header->length = current - (u32)mcfg;
302 header->checksum = table_compute_checksum((void *)mcfg, header->length);
305 static void enter_acpi_mode(int pm1_cnt)
308 * PM1_CNT register bit0 selects the power management event to be
309 * either an SCI or SMI interrupt. When this bit is set, then power
310 * management events will generate an SCI interrupt. When this bit
311 * is reset power management events will generate an SMI interrupt.
313 * Per ACPI spec, it is the responsibility of the hardware to set
314 * or reset this bit. OSPM always preserves this bit position.
316 * U-Boot does not support SMI. And we don't have plan to support
317 * anything running in SMM within U-Boot. To create a legacy-free
318 * system, and expose ourselves to OSPM as working under ACPI mode
319 * already, turn this bit on.
321 outw(PM1_CNT_SCI_EN, pm1_cnt);
325 * QEMU's version of write_acpi_tables is defined in
326 * arch/x86/cpu/qemu/acpi_table.c
328 u32 write_acpi_tables(u32 start)
331 struct acpi_rsdp *rsdp;
332 struct acpi_rsdt *rsdt;
333 struct acpi_xsdt *xsdt;
334 struct acpi_facs *facs;
335 struct acpi_table_header *dsdt;
336 struct acpi_fadt *fadt;
337 struct acpi_mcfg *mcfg;
338 struct acpi_madt *madt;
342 /* Align ACPI tables to 16 byte */
343 current = ALIGN(current, 16);
345 debug("ACPI: Writing ACPI tables at %x\n", start);
347 /* We need at least an RSDP and an RSDT Table */
348 rsdp = (struct acpi_rsdp *)current;
349 current += sizeof(struct acpi_rsdp);
350 current = ALIGN(current, 16);
351 rsdt = (struct acpi_rsdt *)current;
352 current += sizeof(struct acpi_rsdt);
353 current = ALIGN(current, 16);
354 xsdt = (struct acpi_xsdt *)current;
355 current += sizeof(struct acpi_xsdt);
357 * Per ACPI spec, the FACS table address must be aligned to a 64 byte
358 * boundary (Windows checks this, but Linux does not).
360 current = ALIGN(current, 64);
362 /* clear all table memory */
363 memset((void *)start, 0, current - start);
365 acpi_write_rsdp(rsdp, rsdt, xsdt);
366 acpi_write_rsdt(rsdt);
367 acpi_write_xsdt(xsdt);
369 debug("ACPI: * FACS\n");
370 facs = (struct acpi_facs *)current;
371 current += sizeof(struct acpi_facs);
372 current = ALIGN(current, 16);
374 acpi_create_facs(facs);
376 debug("ACPI: * DSDT\n");
377 dsdt = (struct acpi_table_header *)current;
378 memcpy(dsdt, &AmlCode, sizeof(struct acpi_table_header));
379 if (dsdt->length >= sizeof(struct acpi_table_header)) {
380 current += sizeof(struct acpi_table_header);
381 memcpy((char *)current,
382 (char *)&AmlCode + sizeof(struct acpi_table_header),
383 dsdt->length - sizeof(struct acpi_table_header));
384 current += dsdt->length - sizeof(struct acpi_table_header);
386 current = ALIGN(current, 16);
388 debug("ACPI: * FADT\n");
389 fadt = (struct acpi_fadt *)current;
390 current += sizeof(struct acpi_fadt);
391 current = ALIGN(current, 16);
392 acpi_create_fadt(fadt, facs, dsdt);
393 acpi_add_table(rsdp, fadt);
395 debug("ACPI: * MADT\n");
396 madt = (struct acpi_madt *)current;
397 acpi_create_madt(madt);
398 if (madt->header.length > sizeof(struct acpi_madt)) {
399 current += madt->header.length;
400 acpi_add_table(rsdp, madt);
402 current = ALIGN(current, 16);
404 debug("ACPI: * MCFG\n");
405 mcfg = (struct acpi_mcfg *)current;
406 acpi_create_mcfg(mcfg);
407 if (mcfg->header.length > sizeof(struct acpi_mcfg)) {
408 current += mcfg->header.length;
409 current = ALIGN(current, 16);
410 acpi_add_table(rsdp, mcfg);
413 debug("current = %x\n", current);
415 debug("ACPI: done\n");
418 * Other than waiting for OSPM to request us to switch to ACPI mode,
419 * do it by ourselves, since SMI will not be triggered.
421 enter_acpi_mode(fadt->pm1a_cnt_blk);