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>
16 #include <acpi/acpi_table.h>
17 #include <asm/acpi/global_nvs.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>
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 static ulong 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 static void acpi_write_rsdt(struct acpi_rsdt *rsdt)
67 struct acpi_table_header *header = &(rsdt->header);
69 /* Fill out header fields */
70 acpi_fill_header(header, "RSDT");
71 header->length = sizeof(struct acpi_rsdt);
74 /* Entries are filled in later, we come with an empty set */
77 header->checksum = table_compute_checksum((void *)rsdt,
78 sizeof(struct acpi_rsdt));
81 static void acpi_write_xsdt(struct acpi_xsdt *xsdt)
83 struct acpi_table_header *header = &(xsdt->header);
85 /* Fill out header fields */
86 acpi_fill_header(header, "XSDT");
87 header->length = sizeof(struct acpi_xsdt);
90 /* Entries are filled in later, we come with an empty set */
93 header->checksum = table_compute_checksum((void *)xsdt,
94 sizeof(struct acpi_xsdt));
98 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
101 static void acpi_add_table(struct acpi_rsdp *rsdp, void *table)
104 struct acpi_rsdt *rsdt;
105 struct acpi_xsdt *xsdt;
107 /* The RSDT is mandatory while the XSDT is not */
108 rsdt = (struct acpi_rsdt *)rsdp->rsdt_address;
110 /* This should always be MAX_ACPI_TABLES */
111 entries_num = ARRAY_SIZE(rsdt->entry);
113 for (i = 0; i < entries_num; i++) {
114 if (rsdt->entry[i] == 0)
118 if (i >= entries_num) {
119 debug("ACPI: Error: too many tables\n");
123 /* Add table to the RSDT */
124 rsdt->entry[i] = (u32)table;
126 /* Fix RSDT length or the kernel will assume invalid entries */
127 rsdt->header.length = sizeof(struct acpi_table_header) +
128 sizeof(u32) * (i + 1);
130 /* Re-calculate checksum */
131 rsdt->header.checksum = 0;
132 rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
133 rsdt->header.length);
135 /* The RSDT is mandatory while the XSDT is not */
136 if (!rsdp->xsdt_address)
140 * And now the same thing for the XSDT. We use the same index as for
141 * now we want the XSDT and RSDT to always be in sync in U-Boot
143 xsdt = (struct acpi_xsdt *)((u32)rsdp->xsdt_address);
145 /* Add table to the XSDT */
146 xsdt->entry[i] = (u64)(u32)table;
148 /* Fix XSDT length */
149 xsdt->header.length = sizeof(struct acpi_table_header) +
150 sizeof(u64) * (i + 1);
152 /* Re-calculate checksum */
153 xsdt->header.checksum = 0;
154 xsdt->header.checksum = table_compute_checksum((u8 *)xsdt,
155 xsdt->header.length);
158 static void acpi_create_facs(struct acpi_facs *facs)
160 memset((void *)facs, 0, sizeof(struct acpi_facs));
162 memcpy(facs->signature, "FACS", 4);
163 facs->length = sizeof(struct acpi_facs);
164 facs->hardware_signature = 0;
165 facs->firmware_waking_vector = 0;
166 facs->global_lock = 0;
168 facs->x_firmware_waking_vector_l = 0;
169 facs->x_firmware_waking_vector_h = 0;
173 static int acpi_create_madt_lapic(struct acpi_madt_lapic *lapic,
176 lapic->type = ACPI_APIC_LAPIC;
177 lapic->length = sizeof(struct acpi_madt_lapic);
178 lapic->flags = LOCAL_APIC_FLAG_ENABLED;
179 lapic->processor_id = cpu;
180 lapic->apic_id = apic;
182 return lapic->length;
185 int acpi_create_madt_lapics(u32 current)
188 int total_length = 0;
190 for (uclass_find_first_device(UCLASS_CPU, &dev);
192 uclass_find_next_device(&dev)) {
193 struct cpu_platdata *plat = dev_get_parent_platdata(dev);
194 int length = acpi_create_madt_lapic(
195 (struct acpi_madt_lapic *)current,
196 plat->cpu_id, plat->cpu_id);
198 total_length += length;
204 int acpi_create_madt_ioapic(struct acpi_madt_ioapic *ioapic, u8 id,
205 u32 addr, u32 gsi_base)
207 ioapic->type = ACPI_APIC_IOAPIC;
208 ioapic->length = sizeof(struct acpi_madt_ioapic);
209 ioapic->reserved = 0x00;
210 ioapic->gsi_base = gsi_base;
211 ioapic->ioapic_id = id;
212 ioapic->ioapic_addr = addr;
214 return ioapic->length;
217 int acpi_create_madt_irqoverride(struct acpi_madt_irqoverride *irqoverride,
218 u8 bus, u8 source, u32 gsirq, u16 flags)
220 irqoverride->type = ACPI_APIC_IRQ_SRC_OVERRIDE;
221 irqoverride->length = sizeof(struct acpi_madt_irqoverride);
222 irqoverride->bus = bus;
223 irqoverride->source = source;
224 irqoverride->gsirq = gsirq;
225 irqoverride->flags = flags;
227 return irqoverride->length;
230 int acpi_create_madt_lapic_nmi(struct acpi_madt_lapic_nmi *lapic_nmi,
231 u8 cpu, u16 flags, u8 lint)
233 lapic_nmi->type = ACPI_APIC_LAPIC_NMI;
234 lapic_nmi->length = sizeof(struct acpi_madt_lapic_nmi);
235 lapic_nmi->flags = flags;
236 lapic_nmi->processor_id = cpu;
237 lapic_nmi->lint = lint;
239 return lapic_nmi->length;
242 static int acpi_create_madt_irq_overrides(u32 current)
244 struct acpi_madt_irqoverride *irqovr;
245 u16 sci_flags = MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_HIGH;
248 irqovr = (void *)current;
249 length += acpi_create_madt_irqoverride(irqovr, 0, 0, 2, 0);
251 irqovr = (void *)(current + length);
252 length += acpi_create_madt_irqoverride(irqovr, 0, 9, 9, sci_flags);
257 __weak u32 acpi_fill_madt(u32 current)
259 current += acpi_create_madt_lapics(current);
261 current += acpi_create_madt_ioapic((struct acpi_madt_ioapic *)current,
262 io_apic_read(IO_APIC_ID) >> 24, IO_APIC_ADDR, 0);
264 current += acpi_create_madt_irq_overrides(current);
269 static void acpi_create_madt(struct acpi_madt *madt)
271 struct acpi_table_header *header = &(madt->header);
272 u32 current = (u32)madt + sizeof(struct acpi_madt);
274 memset((void *)madt, 0, sizeof(struct acpi_madt));
276 /* Fill out header fields */
277 acpi_fill_header(header, "APIC");
278 header->length = sizeof(struct acpi_madt);
279 header->revision = 4;
281 madt->lapic_addr = LAPIC_DEFAULT_BASE;
282 madt->flags = ACPI_MADT_PCAT_COMPAT;
284 current = acpi_fill_madt(current);
286 /* (Re)calculate length and checksum */
287 header->length = current - (u32)madt;
289 header->checksum = table_compute_checksum((void *)madt, header->length);
292 int acpi_create_mcfg_mmconfig(struct acpi_mcfg_mmconfig *mmconfig, u32 base,
293 u16 seg_nr, u8 start, u8 end)
295 memset(mmconfig, 0, sizeof(*mmconfig));
296 mmconfig->base_address_l = base;
297 mmconfig->base_address_h = 0;
298 mmconfig->pci_segment_group_number = seg_nr;
299 mmconfig->start_bus_number = start;
300 mmconfig->end_bus_number = end;
302 return sizeof(struct acpi_mcfg_mmconfig);
305 __weak u32 acpi_fill_mcfg(u32 current)
307 current += acpi_create_mcfg_mmconfig
308 ((struct acpi_mcfg_mmconfig *)current,
309 CONFIG_PCIE_ECAM_BASE, 0x0, 0x0, 255);
314 /* MCFG is defined in the PCI Firmware Specification 3.0 */
315 static void acpi_create_mcfg(struct acpi_mcfg *mcfg)
317 struct acpi_table_header *header = &(mcfg->header);
318 u32 current = (u32)mcfg + sizeof(struct acpi_mcfg);
320 memset((void *)mcfg, 0, sizeof(struct acpi_mcfg));
322 /* Fill out header fields */
323 acpi_fill_header(header, "MCFG");
324 header->length = sizeof(struct acpi_mcfg);
325 header->revision = 1;
327 current = acpi_fill_mcfg(current);
329 /* (Re)calculate length and checksum */
330 header->length = current - (u32)mcfg;
331 header->checksum = table_compute_checksum((void *)mcfg, header->length);
334 __weak u32 acpi_fill_csrt(u32 current)
339 static void acpi_create_csrt(struct acpi_csrt *csrt)
341 struct acpi_table_header *header = &(csrt->header);
342 u32 current = (u32)csrt + sizeof(struct acpi_csrt);
344 memset((void *)csrt, 0, sizeof(struct acpi_csrt));
346 /* Fill out header fields */
347 acpi_fill_header(header, "CSRT");
348 header->length = sizeof(struct acpi_csrt);
349 header->revision = 0;
351 current = acpi_fill_csrt(current);
353 /* (Re)calculate length and checksum */
354 header->length = current - (u32)csrt;
355 header->checksum = table_compute_checksum((void *)csrt, header->length);
358 static void acpi_create_spcr(struct acpi_spcr *spcr)
360 struct acpi_table_header *header = &(spcr->header);
361 struct serial_device_info serial_info = {0};
362 ulong serial_address, serial_offset;
370 /* Fill out header fields */
371 acpi_fill_header(header, "SPCR");
372 header->length = sizeof(struct acpi_spcr);
373 header->revision = 2;
375 /* Read the device once, here. It is reused below */
376 dev = gd->cur_serial_dev;
378 ret = serial_getinfo(dev, &serial_info);
380 serial_info.type = SERIAL_CHIP_UNKNOWN;
382 /* Encode chip type */
383 switch (serial_info.type) {
384 case SERIAL_CHIP_16550_COMPATIBLE:
385 spcr->interface_type = ACPI_DBG2_16550_COMPATIBLE;
387 case SERIAL_CHIP_UNKNOWN:
389 spcr->interface_type = ACPI_DBG2_UNKNOWN;
393 /* Encode address space */
394 switch (serial_info.addr_space) {
395 case SERIAL_ADDRESS_SPACE_MEMORY:
396 space_id = ACPI_ADDRESS_SPACE_MEMORY;
398 case SERIAL_ADDRESS_SPACE_IO:
400 space_id = ACPI_ADDRESS_SPACE_IO;
404 serial_width = serial_info.reg_width * 8;
405 serial_offset = serial_info.reg_offset << serial_info.reg_shift;
406 serial_address = serial_info.addr + serial_offset;
408 /* Encode register access size */
409 switch (serial_info.reg_shift) {
411 access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS;
414 access_size = ACPI_ACCESS_SIZE_WORD_ACCESS;
417 access_size = ACPI_ACCESS_SIZE_DWORD_ACCESS;
420 access_size = ACPI_ACCESS_SIZE_QWORD_ACCESS;
423 access_size = ACPI_ACCESS_SIZE_UNDEFINED;
427 debug("UART type %u @ %lx\n", spcr->interface_type, serial_address);
430 spcr->serial_port.space_id = space_id;
431 spcr->serial_port.bit_width = serial_width;
432 spcr->serial_port.bit_offset = 0;
433 spcr->serial_port.access_size = access_size;
434 spcr->serial_port.addrl = lower_32_bits(serial_address);
435 spcr->serial_port.addrh = upper_32_bits(serial_address);
437 /* Encode baud rate */
438 switch (serial_info.baudrate) {
456 serial_config = SERIAL_DEFAULT_CONFIG;
458 ret = serial_getconfig(dev, &serial_config);
460 spcr->parity = SERIAL_GET_PARITY(serial_config);
461 spcr->stop_bits = SERIAL_GET_STOP(serial_config);
463 /* No PCI devices for now */
464 spcr->pci_device_id = 0xffff;
465 spcr->pci_vendor_id = 0xffff;
468 * SPCR has no clue if the UART base clock speed is different
469 * to the default one. However, the SPCR 1.04 defines baud rate
470 * 0 as a preconfigured state of UART and OS is supposed not
471 * to touch the configuration of the serial device.
473 if (serial_info.clock != SERIAL_DEFAULT_CLOCK)
477 header->checksum = table_compute_checksum((void *)spcr, header->length);
481 * QEMU's version of write_acpi_tables is defined in drivers/misc/qfw.c
483 ulong write_acpi_tables(ulong start_addr)
485 struct acpi_ctx sctx, *ctx = &sctx;
486 struct acpi_rsdp *rsdp;
487 struct acpi_rsdt *rsdt;
488 struct acpi_xsdt *xsdt;
489 struct acpi_facs *facs;
490 struct acpi_table_header *dsdt;
491 struct acpi_fadt *fadt;
492 struct acpi_mcfg *mcfg;
493 struct acpi_madt *madt;
494 struct acpi_csrt *csrt;
495 struct acpi_spcr *spcr;
500 start = map_sysmem(start_addr, 0);
501 ctx->current = start;
503 /* Align ACPI tables to 16 byte */
506 debug("ACPI: Writing ACPI tables at %lx\n", start_addr);
508 /* We need at least an RSDP and an RSDT Table */
510 acpi_inc_align(ctx, sizeof(struct acpi_rsdp));
512 acpi_inc_align(ctx, sizeof(struct acpi_rsdt));
514 acpi_inc_align(ctx, sizeof(struct acpi_xsdt));
516 * Per ACPI spec, the FACS table address must be aligned to a 64 byte
517 * boundary (Windows checks this, but Linux does not).
521 /* clear all table memory */
522 memset((void *)start, 0, ctx->current - start);
524 acpi_write_rsdp(rsdp, rsdt, xsdt);
525 acpi_write_rsdt(rsdt);
526 acpi_write_xsdt(xsdt);
528 debug("ACPI: * FACS\n");
530 acpi_inc_align(ctx, sizeof(struct acpi_facs));
532 acpi_create_facs(facs);
534 debug("ACPI: * DSDT\n");
536 memcpy(dsdt, &AmlCode, sizeof(struct acpi_table_header));
537 acpi_inc(ctx, sizeof(struct acpi_table_header));
539 (char *)&AmlCode + sizeof(struct acpi_table_header),
540 dsdt->length - sizeof(struct acpi_table_header));
541 acpi_inc_align(ctx, dsdt->length - sizeof(struct acpi_table_header));
543 /* Pack GNVS into the ACPI table area */
544 for (i = 0; i < dsdt->length; i++) {
545 u32 *gnvs = (u32 *)((u32)dsdt + i);
546 if (*gnvs == ACPI_GNVS_ADDR) {
547 ulong addr = (ulong)map_to_sysmem(ctx->current);
549 debug("Fix up global NVS in DSDT to %#08lx\n", addr);
555 /* Update DSDT checksum since we patched the GNVS address */
557 dsdt->checksum = table_compute_checksum((void *)dsdt, dsdt->length);
559 /* Fill in platform-specific global NVS variables */
560 acpi_create_gnvs(ctx->current);
561 acpi_inc_align(ctx, sizeof(struct acpi_global_nvs));
563 debug("ACPI: * FADT\n");
565 acpi_inc_align(ctx, sizeof(struct acpi_fadt));
566 acpi_create_fadt(fadt, facs, dsdt);
567 acpi_add_table(rsdp, fadt);
569 debug("ACPI: * MADT\n");
571 acpi_create_madt(madt);
572 acpi_inc_align(ctx, madt->header.length);
573 acpi_add_table(rsdp, madt);
575 debug("ACPI: * MCFG\n");
577 acpi_create_mcfg(mcfg);
578 acpi_inc_align(ctx, mcfg->header.length);
579 acpi_add_table(rsdp, mcfg);
581 debug("ACPI: * CSRT\n");
583 acpi_create_csrt(csrt);
584 acpi_inc_align(ctx, csrt->header.length);
585 acpi_add_table(rsdp, csrt);
587 debug("ACPI: * SPCR\n");
589 acpi_create_spcr(spcr);
590 acpi_inc_align(ctx, spcr->header.length);
591 acpi_add_table(rsdp, spcr);
593 acpi_write_dev_tables(ctx);
595 addr = map_to_sysmem(ctx->current);
596 debug("current = %lx\n", addr);
598 acpi_rsdp_addr = (unsigned long)rsdp;
599 debug("ACPI: done\n");
604 ulong acpi_get_rsdp_addr(void)
606 return acpi_rsdp_addr;