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>
15 #include <asm/acpi/global_nvs.h>
16 #include <asm/acpi_table.h>
17 #include <asm/ioapic.h>
18 #include <asm/lapic.h>
19 #include <asm/mpspec.h>
20 #include <asm/tables.h>
21 #include <asm/arch/global_nvs.h>
24 * IASL compiles the dsdt entries and writes the hex values
25 * to a C array AmlCode[] (see dsdt.c).
27 extern const unsigned char AmlCode[];
29 /* ACPI RSDP address to be used in boot parameters */
30 static ulong acpi_rsdp_addr;
32 static void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt,
33 struct acpi_xsdt *xsdt)
35 memset(rsdp, 0, sizeof(struct acpi_rsdp));
37 memcpy(rsdp->signature, RSDP_SIG, 8);
38 memcpy(rsdp->oem_id, OEM_ID, 6);
40 rsdp->length = sizeof(struct acpi_rsdp);
41 rsdp->rsdt_address = (u32)rsdt;
44 * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2
46 * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
47 * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
51 rsdp->revision = ACPI_RSDP_REV_ACPI_1_0;
53 rsdp->xsdt_address = (u64)(u32)xsdt;
54 rsdp->revision = ACPI_RSDP_REV_ACPI_2_0;
57 /* Calculate checksums */
58 rsdp->checksum = table_compute_checksum((void *)rsdp, 20);
59 rsdp->ext_checksum = table_compute_checksum((void *)rsdp,
60 sizeof(struct acpi_rsdp));
63 void acpi_fill_header(struct acpi_table_header *header, char *signature)
65 memcpy(header->signature, signature, 4);
66 memcpy(header->oem_id, OEM_ID, 6);
67 memcpy(header->oem_table_id, OEM_TABLE_ID, 8);
68 header->oem_revision = U_BOOT_BUILD_DATE;
69 memcpy(header->aslc_id, ASLC_ID, 4);
72 static void acpi_write_rsdt(struct acpi_rsdt *rsdt)
74 struct acpi_table_header *header = &(rsdt->header);
76 /* Fill out header fields */
77 acpi_fill_header(header, "RSDT");
78 header->length = sizeof(struct acpi_rsdt);
81 /* Entries are filled in later, we come with an empty set */
84 header->checksum = table_compute_checksum((void *)rsdt,
85 sizeof(struct acpi_rsdt));
88 static void acpi_write_xsdt(struct acpi_xsdt *xsdt)
90 struct acpi_table_header *header = &(xsdt->header);
92 /* Fill out header fields */
93 acpi_fill_header(header, "XSDT");
94 header->length = sizeof(struct acpi_xsdt);
97 /* Entries are filled in later, we come with an empty set */
100 header->checksum = table_compute_checksum((void *)xsdt,
101 sizeof(struct acpi_xsdt));
105 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
108 static void acpi_add_table(struct acpi_rsdp *rsdp, void *table)
111 struct acpi_rsdt *rsdt;
112 struct acpi_xsdt *xsdt = NULL;
114 /* The RSDT is mandatory while the XSDT is not */
115 rsdt = (struct acpi_rsdt *)rsdp->rsdt_address;
117 if (rsdp->xsdt_address)
118 xsdt = (struct acpi_xsdt *)((u32)rsdp->xsdt_address);
120 /* This should always be MAX_ACPI_TABLES */
121 entries_num = ARRAY_SIZE(rsdt->entry);
123 for (i = 0; i < entries_num; i++) {
124 if (rsdt->entry[i] == 0)
128 if (i >= entries_num) {
129 debug("ACPI: Error: too many tables\n");
133 /* Add table to the RSDT */
134 rsdt->entry[i] = (u32)table;
136 /* Fix RSDT length or the kernel will assume invalid entries */
137 rsdt->header.length = sizeof(struct acpi_table_header) +
138 (sizeof(u32) * (i + 1));
140 /* Re-calculate checksum */
141 rsdt->header.checksum = 0;
142 rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
143 rsdt->header.length);
146 * And now the same thing for the XSDT. We use the same index as for
147 * now we want the XSDT and RSDT to always be in sync in U-Boot
150 /* Add table to the XSDT */
151 xsdt->entry[i] = (u64)(u32)table;
153 /* Fix XSDT length */
154 xsdt->header.length = sizeof(struct acpi_table_header) +
155 (sizeof(u64) * (i + 1));
157 /* Re-calculate checksum */
158 xsdt->header.checksum = 0;
159 xsdt->header.checksum = table_compute_checksum((u8 *)xsdt,
160 xsdt->header.length);
164 static void acpi_create_facs(struct acpi_facs *facs)
166 memset((void *)facs, 0, sizeof(struct acpi_facs));
168 memcpy(facs->signature, "FACS", 4);
169 facs->length = sizeof(struct acpi_facs);
170 facs->hardware_signature = 0;
171 facs->firmware_waking_vector = 0;
172 facs->global_lock = 0;
174 facs->x_firmware_waking_vector_l = 0;
175 facs->x_firmware_waking_vector_h = 0;
179 static int acpi_create_madt_lapic(struct acpi_madt_lapic *lapic,
182 lapic->type = ACPI_APIC_LAPIC;
183 lapic->length = sizeof(struct acpi_madt_lapic);
184 lapic->flags = LOCAL_APIC_FLAG_ENABLED;
185 lapic->processor_id = cpu;
186 lapic->apic_id = apic;
188 return lapic->length;
191 int acpi_create_madt_lapics(u32 current)
194 int total_length = 0;
196 for (uclass_find_first_device(UCLASS_CPU, &dev);
198 uclass_find_next_device(&dev)) {
199 struct cpu_platdata *plat = dev_get_parent_platdata(dev);
200 int length = acpi_create_madt_lapic(
201 (struct acpi_madt_lapic *)current,
202 plat->cpu_id, plat->cpu_id);
204 total_length += length;
210 int acpi_create_madt_ioapic(struct acpi_madt_ioapic *ioapic, u8 id,
211 u32 addr, u32 gsi_base)
213 ioapic->type = ACPI_APIC_IOAPIC;
214 ioapic->length = sizeof(struct acpi_madt_ioapic);
215 ioapic->reserved = 0x00;
216 ioapic->gsi_base = gsi_base;
217 ioapic->ioapic_id = id;
218 ioapic->ioapic_addr = addr;
220 return ioapic->length;
223 int acpi_create_madt_irqoverride(struct acpi_madt_irqoverride *irqoverride,
224 u8 bus, u8 source, u32 gsirq, u16 flags)
226 irqoverride->type = ACPI_APIC_IRQ_SRC_OVERRIDE;
227 irqoverride->length = sizeof(struct acpi_madt_irqoverride);
228 irqoverride->bus = bus;
229 irqoverride->source = source;
230 irqoverride->gsirq = gsirq;
231 irqoverride->flags = flags;
233 return irqoverride->length;
236 int acpi_create_madt_lapic_nmi(struct acpi_madt_lapic_nmi *lapic_nmi,
237 u8 cpu, u16 flags, u8 lint)
239 lapic_nmi->type = ACPI_APIC_LAPIC_NMI;
240 lapic_nmi->length = sizeof(struct acpi_madt_lapic_nmi);
241 lapic_nmi->flags = flags;
242 lapic_nmi->processor_id = cpu;
243 lapic_nmi->lint = lint;
245 return lapic_nmi->length;
248 static int acpi_create_madt_irq_overrides(u32 current)
250 struct acpi_madt_irqoverride *irqovr;
251 u16 sci_flags = MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_HIGH;
254 irqovr = (void *)current;
255 length += acpi_create_madt_irqoverride(irqovr, 0, 0, 2, 0);
257 irqovr = (void *)(current + length);
258 length += acpi_create_madt_irqoverride(irqovr, 0, 9, 9, sci_flags);
263 __weak u32 acpi_fill_madt(u32 current)
265 current += acpi_create_madt_lapics(current);
267 current += acpi_create_madt_ioapic((struct acpi_madt_ioapic *)current,
268 io_apic_read(IO_APIC_ID) >> 24, IO_APIC_ADDR, 0);
270 current += acpi_create_madt_irq_overrides(current);
275 static void acpi_create_madt(struct acpi_madt *madt)
277 struct acpi_table_header *header = &(madt->header);
278 u32 current = (u32)madt + sizeof(struct acpi_madt);
280 memset((void *)madt, 0, sizeof(struct acpi_madt));
282 /* Fill out header fields */
283 acpi_fill_header(header, "APIC");
284 header->length = sizeof(struct acpi_madt);
285 header->revision = 4;
287 madt->lapic_addr = LAPIC_DEFAULT_BASE;
288 madt->flags = ACPI_MADT_PCAT_COMPAT;
290 current = acpi_fill_madt(current);
292 /* (Re)calculate length and checksum */
293 header->length = current - (u32)madt;
295 header->checksum = table_compute_checksum((void *)madt, header->length);
298 int acpi_create_mcfg_mmconfig(struct acpi_mcfg_mmconfig *mmconfig, u32 base,
299 u16 seg_nr, u8 start, u8 end)
301 memset(mmconfig, 0, sizeof(*mmconfig));
302 mmconfig->base_address_l = base;
303 mmconfig->base_address_h = 0;
304 mmconfig->pci_segment_group_number = seg_nr;
305 mmconfig->start_bus_number = start;
306 mmconfig->end_bus_number = end;
308 return sizeof(struct acpi_mcfg_mmconfig);
311 __weak u32 acpi_fill_mcfg(u32 current)
313 current += acpi_create_mcfg_mmconfig
314 ((struct acpi_mcfg_mmconfig *)current,
315 CONFIG_PCIE_ECAM_BASE, 0x0, 0x0, 255);
320 /* MCFG is defined in the PCI Firmware Specification 3.0 */
321 static void acpi_create_mcfg(struct acpi_mcfg *mcfg)
323 struct acpi_table_header *header = &(mcfg->header);
324 u32 current = (u32)mcfg + sizeof(struct acpi_mcfg);
326 memset((void *)mcfg, 0, sizeof(struct acpi_mcfg));
328 /* Fill out header fields */
329 acpi_fill_header(header, "MCFG");
330 header->length = sizeof(struct acpi_mcfg);
331 header->revision = 1;
333 current = acpi_fill_mcfg(current);
335 /* (Re)calculate length and checksum */
336 header->length = current - (u32)mcfg;
337 header->checksum = table_compute_checksum((void *)mcfg, header->length);
340 static void acpi_create_spcr(struct acpi_spcr *spcr)
342 struct acpi_table_header *header = &(spcr->header);
343 struct serial_device_info serial_info = {0};
344 ulong serial_address, serial_offset;
352 /* Fill out header fields */
353 acpi_fill_header(header, "SPCR");
354 header->length = sizeof(struct acpi_spcr);
355 header->revision = 2;
357 /* Read the device once, here. It is reused below */
358 dev = gd->cur_serial_dev;
360 ret = serial_getinfo(dev, &serial_info);
362 serial_info.type = SERIAL_CHIP_UNKNOWN;
364 /* Encode chip type */
365 switch (serial_info.type) {
366 case SERIAL_CHIP_16550_COMPATIBLE:
367 spcr->interface_type = ACPI_DBG2_16550_COMPATIBLE;
369 case SERIAL_CHIP_UNKNOWN:
371 spcr->interface_type = ACPI_DBG2_UNKNOWN;
375 /* Encode address space */
376 switch (serial_info.addr_space) {
377 case SERIAL_ADDRESS_SPACE_MEMORY:
378 space_id = ACPI_ADDRESS_SPACE_MEMORY;
380 case SERIAL_ADDRESS_SPACE_IO:
382 space_id = ACPI_ADDRESS_SPACE_IO;
386 serial_width = serial_info.reg_width * 8;
387 serial_offset = serial_info.reg_offset << serial_info.reg_shift;
388 serial_address = serial_info.addr + serial_offset;
390 /* Encode register access size */
391 switch (serial_info.reg_shift) {
393 access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS;
396 access_size = ACPI_ACCESS_SIZE_WORD_ACCESS;
399 access_size = ACPI_ACCESS_SIZE_DWORD_ACCESS;
402 access_size = ACPI_ACCESS_SIZE_QWORD_ACCESS;
405 access_size = ACPI_ACCESS_SIZE_UNDEFINED;
409 debug("UART type %u @ %lx\n", spcr->interface_type, serial_address);
412 spcr->serial_port.space_id = space_id;
413 spcr->serial_port.bit_width = serial_width;
414 spcr->serial_port.bit_offset = 0;
415 spcr->serial_port.access_size = access_size;
416 spcr->serial_port.addrl = lower_32_bits(serial_address);
417 spcr->serial_port.addrh = upper_32_bits(serial_address);
419 /* Encode baud rate */
420 switch (serial_info.baudrate) {
438 serial_config = SERIAL_DEFAULT_CONFIG;
440 ret = serial_getconfig(dev, &serial_config);
442 spcr->parity = SERIAL_GET_PARITY(serial_config);
443 spcr->stop_bits = SERIAL_GET_STOP(serial_config);
445 /* No PCI devices for now */
446 spcr->pci_device_id = 0xffff;
447 spcr->pci_vendor_id = 0xffff;
450 header->checksum = table_compute_checksum((void *)spcr, header->length);
454 * QEMU's version of write_acpi_tables is defined in drivers/misc/qfw.c
456 ulong write_acpi_tables(ulong start)
459 struct acpi_rsdp *rsdp;
460 struct acpi_rsdt *rsdt;
461 struct acpi_xsdt *xsdt;
462 struct acpi_facs *facs;
463 struct acpi_table_header *dsdt;
464 struct acpi_fadt *fadt;
465 struct acpi_mcfg *mcfg;
466 struct acpi_madt *madt;
467 struct acpi_spcr *spcr;
472 /* Align ACPI tables to 16 byte */
473 current = ALIGN(current, 16);
475 debug("ACPI: Writing ACPI tables at %lx\n", start);
477 /* We need at least an RSDP and an RSDT Table */
478 rsdp = (struct acpi_rsdp *)current;
479 current += sizeof(struct acpi_rsdp);
480 current = ALIGN(current, 16);
481 rsdt = (struct acpi_rsdt *)current;
482 current += sizeof(struct acpi_rsdt);
483 current = ALIGN(current, 16);
484 xsdt = (struct acpi_xsdt *)current;
485 current += sizeof(struct acpi_xsdt);
487 * Per ACPI spec, the FACS table address must be aligned to a 64 byte
488 * boundary (Windows checks this, but Linux does not).
490 current = ALIGN(current, 64);
492 /* clear all table memory */
493 memset((void *)start, 0, current - start);
495 acpi_write_rsdp(rsdp, rsdt, xsdt);
496 acpi_write_rsdt(rsdt);
497 acpi_write_xsdt(xsdt);
499 debug("ACPI: * FACS\n");
500 facs = (struct acpi_facs *)current;
501 current += sizeof(struct acpi_facs);
502 current = ALIGN(current, 16);
504 acpi_create_facs(facs);
506 debug("ACPI: * DSDT\n");
507 dsdt = (struct acpi_table_header *)current;
508 memcpy(dsdt, &AmlCode, sizeof(struct acpi_table_header));
509 current += sizeof(struct acpi_table_header);
510 memcpy((char *)current,
511 (char *)&AmlCode + sizeof(struct acpi_table_header),
512 dsdt->length - sizeof(struct acpi_table_header));
513 current += dsdt->length - sizeof(struct acpi_table_header);
514 current = ALIGN(current, 16);
516 /* Pack GNVS into the ACPI table area */
517 for (i = 0; i < dsdt->length; i++) {
518 u32 *gnvs = (u32 *)((u32)dsdt + i);
519 if (*gnvs == ACPI_GNVS_ADDR) {
520 debug("Fix up global NVS in DSDT to 0x%08x\n", current);
526 /* Update DSDT checksum since we patched the GNVS address */
528 dsdt->checksum = table_compute_checksum((void *)dsdt, dsdt->length);
530 /* Fill in platform-specific global NVS variables */
531 acpi_create_gnvs((struct acpi_global_nvs *)current);
532 current += sizeof(struct acpi_global_nvs);
533 current = ALIGN(current, 16);
535 debug("ACPI: * FADT\n");
536 fadt = (struct acpi_fadt *)current;
537 current += sizeof(struct acpi_fadt);
538 current = ALIGN(current, 16);
539 acpi_create_fadt(fadt, facs, dsdt);
540 acpi_add_table(rsdp, fadt);
542 debug("ACPI: * MADT\n");
543 madt = (struct acpi_madt *)current;
544 acpi_create_madt(madt);
545 current += madt->header.length;
546 acpi_add_table(rsdp, madt);
547 current = ALIGN(current, 16);
549 debug("ACPI: * MCFG\n");
550 mcfg = (struct acpi_mcfg *)current;
551 acpi_create_mcfg(mcfg);
552 current += mcfg->header.length;
553 acpi_add_table(rsdp, mcfg);
554 current = ALIGN(current, 16);
556 debug("ACPI: * SPCR\n");
557 spcr = (struct acpi_spcr *)current;
558 acpi_create_spcr(spcr);
559 current += spcr->header.length;
560 acpi_add_table(rsdp, spcr);
561 current = ALIGN(current, 16);
563 debug("current = %x\n", current);
565 acpi_rsdp_addr = (unsigned long)rsdp;
566 debug("ACPI: done\n");
571 ulong acpi_get_rsdp_addr(void)
573 return acpi_rsdp_addr;