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>
9 #define LOG_CATEGORY LOGC_ACPI
16 #include <dm/uclass-internal.h>
19 #include <acpi/acpigen.h>
20 #include <acpi/acpi_device.h>
21 #include <acpi/acpi_table.h>
22 #include <asm/acpi/global_nvs.h>
23 #include <asm/ioapic.h>
24 #include <asm/global_data.h>
25 #include <asm/lapic.h>
26 #include <asm/mpspec.h>
27 #include <asm/tables.h>
28 #include <asm/arch/global_nvs.h>
30 #include <linux/err.h>
32 /* ACPI RSDP address to be used in boot parameters */
33 static ulong acpi_rsdp_addr;
35 static int acpi_create_madt_lapic(struct acpi_madt_lapic *lapic,
38 lapic->type = ACPI_APIC_LAPIC;
39 lapic->length = sizeof(struct acpi_madt_lapic);
40 lapic->flags = LOCAL_APIC_FLAG_ENABLED;
41 lapic->processor_id = cpu;
42 lapic->apic_id = apic;
47 int acpi_create_madt_lapics(u32 current)
53 for (uclass_find_first_device(UCLASS_CPU, &dev);
55 uclass_find_next_device(&dev)) {
56 struct cpu_plat *plat = dev_get_parent_plat(dev);
59 length = acpi_create_madt_lapic(
60 (struct acpi_madt_lapic *)current, cpu_num++,
63 total_length += length;
69 int acpi_create_madt_ioapic(struct acpi_madt_ioapic *ioapic, u8 id,
70 u32 addr, u32 gsi_base)
72 ioapic->type = ACPI_APIC_IOAPIC;
73 ioapic->length = sizeof(struct acpi_madt_ioapic);
74 ioapic->reserved = 0x00;
75 ioapic->gsi_base = gsi_base;
76 ioapic->ioapic_id = id;
77 ioapic->ioapic_addr = addr;
79 return ioapic->length;
82 int acpi_create_madt_irqoverride(struct acpi_madt_irqoverride *irqoverride,
83 u8 bus, u8 source, u32 gsirq, u16 flags)
85 irqoverride->type = ACPI_APIC_IRQ_SRC_OVERRIDE;
86 irqoverride->length = sizeof(struct acpi_madt_irqoverride);
87 irqoverride->bus = bus;
88 irqoverride->source = source;
89 irqoverride->gsirq = gsirq;
90 irqoverride->flags = flags;
92 return irqoverride->length;
95 int acpi_create_madt_lapic_nmi(struct acpi_madt_lapic_nmi *lapic_nmi,
96 u8 cpu, u16 flags, u8 lint)
98 lapic_nmi->type = ACPI_APIC_LAPIC_NMI;
99 lapic_nmi->length = sizeof(struct acpi_madt_lapic_nmi);
100 lapic_nmi->flags = flags;
101 lapic_nmi->processor_id = cpu;
102 lapic_nmi->lint = lint;
104 return lapic_nmi->length;
107 static int acpi_create_madt_irq_overrides(u32 current)
109 struct acpi_madt_irqoverride *irqovr;
110 u16 sci_flags = MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_HIGH;
113 irqovr = (void *)current;
114 length += acpi_create_madt_irqoverride(irqovr, 0, 0, 2, 0);
116 irqovr = (void *)(current + length);
117 length += acpi_create_madt_irqoverride(irqovr, 0, 9, 9, sci_flags);
122 __weak u32 acpi_fill_madt(u32 current)
124 current += acpi_create_madt_lapics(current);
126 current += acpi_create_madt_ioapic((struct acpi_madt_ioapic *)current,
127 io_apic_read(IO_APIC_ID) >> 24, IO_APIC_ADDR, 0);
129 current += acpi_create_madt_irq_overrides(current);
134 int acpi_write_madt(struct acpi_ctx *ctx, const struct acpi_writer *entry)
136 struct acpi_table_header *header;
137 struct acpi_madt *madt;
142 memset(madt, '\0', sizeof(struct acpi_madt));
143 header = &madt->header;
145 /* Fill out header fields */
146 acpi_fill_header(header, "APIC");
147 header->length = sizeof(struct acpi_madt);
148 header->revision = ACPI_MADT_REV_ACPI_3_0;
150 madt->lapic_addr = LAPIC_DEFAULT_BASE;
151 madt->flags = ACPI_MADT_PCAT_COMPAT;
153 current = (u32)madt + sizeof(struct acpi_madt);
154 current = acpi_fill_madt(current);
156 /* (Re)calculate length and checksum */
157 header->length = current - (u32)madt;
159 header->checksum = table_compute_checksum((void *)madt, header->length);
160 acpi_add_table(ctx, madt);
161 acpi_inc(ctx, madt->header.length);
165 ACPI_WRITER(5x86, NULL, acpi_write_madt, 0);
167 int acpi_create_mcfg_mmconfig(struct acpi_mcfg_mmconfig *mmconfig, u32 base,
168 u16 seg_nr, u8 start, u8 end)
170 memset(mmconfig, 0, sizeof(*mmconfig));
171 mmconfig->base_address_l = base;
172 mmconfig->base_address_h = 0;
173 mmconfig->pci_segment_group_number = seg_nr;
174 mmconfig->start_bus_number = start;
175 mmconfig->end_bus_number = end;
177 return sizeof(struct acpi_mcfg_mmconfig);
180 __weak u32 acpi_fill_mcfg(u32 current)
182 current += acpi_create_mcfg_mmconfig
183 ((struct acpi_mcfg_mmconfig *)current,
184 CONFIG_PCIE_ECAM_BASE, 0x0, 0x0, 255);
190 * acpi_create_tcpa() - Create a TCPA table
192 * @tcpa: Pointer to place to put table
194 * Trusted Computing Platform Alliance Capabilities Table
195 * TCPA PC Specific Implementation SpecificationTCPA is defined in the PCI
196 * Firmware Specification 3.0
198 static int acpi_create_tcpa(struct acpi_tcpa *tcpa)
200 struct acpi_table_header *header = &tcpa->header;
201 u32 current = (u32)tcpa + sizeof(struct acpi_tcpa);
202 int size = 0x10000; /* Use this as the default size */
206 if (!CONFIG_IS_ENABLED(BLOBLIST))
208 memset(tcpa, '\0', sizeof(struct acpi_tcpa));
210 /* Fill out header fields */
211 acpi_fill_header(header, "TCPA");
212 header->length = sizeof(struct acpi_tcpa);
213 header->revision = 1;
215 ret = bloblist_ensure_size_ret(BLOBLISTT_TCPA_LOG, &size, &log);
217 return log_msg_ret("blob", ret);
219 tcpa->platform_class = 0;
221 tcpa->lasa = (ulong)log;
223 /* (Re)calculate length and checksum */
224 header->length = current - (u32)tcpa;
225 header->checksum = table_compute_checksum((void *)tcpa, header->length);
230 static int get_tpm2_log(void **ptrp, int *sizep)
232 const int tpm2_default_log_len = 0x10000;
237 size = tpm2_default_log_len;
238 ret = bloblist_ensure_size_ret(BLOBLISTT_TPM2_TCG_LOG, &size, ptrp);
240 return log_msg_ret("blob", ret);
246 static int acpi_write_tpm2(struct acpi_ctx *ctx,
247 const struct acpi_writer *entry)
249 struct acpi_table_header *header;
250 struct acpi_tpm2 *tpm2;
255 if (!IS_ENABLED(CONFIG_TPM_V2))
256 return log_msg_ret("none", -ENOENT);
259 header = &tpm2->header;
260 memset(tpm2, '\0', sizeof(struct acpi_tpm2));
263 * Some payloads like SeaBIOS depend on log area to use TPM2.
264 * Get the memory size and address of TPM2 log area or initialize it.
266 ret = get_tpm2_log(&lasa, &tpm2_log_len);
268 return log_msg_ret("log", ret);
270 /* Fill out header fields. */
271 acpi_fill_header(header, "TPM2");
272 memcpy(header->aslc_id, ASLC_ID, 4);
274 header->length = sizeof(struct acpi_tpm2);
275 header->revision = acpi_get_table_revision(ACPITAB_TPM2);
277 /* Hard to detect for U-Boot. Just set it to 0 */
278 tpm2->platform_class = 0;
280 /* Must be set to 0 for FIFO-interface support */
281 tpm2->control_area = 0;
282 tpm2->start_method = 6;
283 memset(tpm2->msp, 0, sizeof(tpm2->msp));
285 /* Fill the log area size and start address fields. */
286 tpm2->laml = tpm2_log_len;
287 tpm2->lasa = map_to_sysmem(lasa);
289 /* Calculate checksum. */
290 header->checksum = table_compute_checksum(tpm2, header->length);
292 acpi_inc(ctx, tpm2->header.length);
293 acpi_add_table(ctx, tpm2);
297 ACPI_WRITER(5tpm2, "TPM2", acpi_write_tpm2, 0);
299 __weak u32 acpi_fill_csrt(u32 current)
304 static int acpi_create_csrt(struct acpi_csrt *csrt)
306 struct acpi_table_header *header = &(csrt->header);
307 u32 current = (u32)csrt + sizeof(struct acpi_csrt);
310 memset((void *)csrt, 0, sizeof(struct acpi_csrt));
312 /* Fill out header fields */
313 acpi_fill_header(header, "CSRT");
314 header->length = sizeof(struct acpi_csrt);
315 header->revision = 0;
317 ptr = acpi_fill_csrt(current);
322 /* (Re)calculate length and checksum */
323 header->length = current - (u32)csrt;
324 header->checksum = table_compute_checksum((void *)csrt, header->length);
329 static void acpi_create_spcr(struct acpi_spcr *spcr)
331 struct acpi_table_header *header = &(spcr->header);
332 struct serial_device_info serial_info = {0};
333 ulong serial_address, serial_offset;
341 memset((void *)spcr, 0, sizeof(struct acpi_spcr));
343 /* Fill out header fields */
344 acpi_fill_header(header, "SPCR");
345 header->length = sizeof(struct acpi_spcr);
346 header->revision = 2;
348 /* Read the device once, here. It is reused below */
349 dev = gd->cur_serial_dev;
351 ret = serial_getinfo(dev, &serial_info);
353 serial_info.type = SERIAL_CHIP_UNKNOWN;
355 /* Encode chip type */
356 switch (serial_info.type) {
357 case SERIAL_CHIP_16550_COMPATIBLE:
358 spcr->interface_type = ACPI_DBG2_16550_COMPATIBLE;
360 case SERIAL_CHIP_UNKNOWN:
362 spcr->interface_type = ACPI_DBG2_UNKNOWN;
366 /* Encode address space */
367 switch (serial_info.addr_space) {
368 case SERIAL_ADDRESS_SPACE_MEMORY:
369 space_id = ACPI_ADDRESS_SPACE_MEMORY;
371 case SERIAL_ADDRESS_SPACE_IO:
373 space_id = ACPI_ADDRESS_SPACE_IO;
377 serial_width = serial_info.reg_width * 8;
378 serial_offset = serial_info.reg_offset << serial_info.reg_shift;
379 serial_address = serial_info.addr + serial_offset;
381 /* Encode register access size */
382 switch (serial_info.reg_shift) {
384 access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS;
387 access_size = ACPI_ACCESS_SIZE_WORD_ACCESS;
390 access_size = ACPI_ACCESS_SIZE_DWORD_ACCESS;
393 access_size = ACPI_ACCESS_SIZE_QWORD_ACCESS;
396 access_size = ACPI_ACCESS_SIZE_UNDEFINED;
400 debug("UART type %u @ %lx\n", spcr->interface_type, serial_address);
403 spcr->serial_port.space_id = space_id;
404 spcr->serial_port.bit_width = serial_width;
405 spcr->serial_port.bit_offset = 0;
406 spcr->serial_port.access_size = access_size;
407 spcr->serial_port.addrl = lower_32_bits(serial_address);
408 spcr->serial_port.addrh = upper_32_bits(serial_address);
410 /* Encode baud rate */
411 switch (serial_info.baudrate) {
429 serial_config = SERIAL_DEFAULT_CONFIG;
431 ret = serial_getconfig(dev, &serial_config);
433 spcr->parity = SERIAL_GET_PARITY(serial_config);
434 spcr->stop_bits = SERIAL_GET_STOP(serial_config);
436 /* No PCI devices for now */
437 spcr->pci_device_id = 0xffff;
438 spcr->pci_vendor_id = 0xffff;
441 * SPCR has no clue if the UART base clock speed is different
442 * to the default one. However, the SPCR 1.04 defines baud rate
443 * 0 as a preconfigured state of UART and OS is supposed not
444 * to touch the configuration of the serial device.
446 if (serial_info.clock != SERIAL_DEFAULT_CLOCK)
450 header->checksum = table_compute_checksum((void *)spcr, header->length);
453 int acpi_write_gnvs(struct acpi_ctx *ctx, const struct acpi_writer *entry)
457 if (!IS_ENABLED(CONFIG_ACPI_GNVS_EXTERNAL)) {
460 /* We need the DSDT to be done */
462 return log_msg_ret("dsdt", -EAGAIN);
464 /* Pack GNVS into the ACPI table area */
465 for (i = 0; i < ctx->dsdt->length; i++) {
466 u32 *gnvs = (u32 *)((u32)ctx->dsdt + i);
468 if (*gnvs == ACPI_GNVS_ADDR) {
469 *gnvs = map_to_sysmem(ctx->current);
470 log_debug("Fix up global NVS in DSDT to %#08x\n",
477 * Recalculate the length and update the DSDT checksum since we
478 * patched the GNVS address. Set the checksum to zero since it
479 * is part of the region being checksummed.
481 ctx->dsdt->checksum = 0;
482 ctx->dsdt->checksum = table_compute_checksum((void *)ctx->dsdt,
486 /* Fill in platform-specific global NVS variables */
487 addr = acpi_create_gnvs(ctx->current);
488 if (IS_ERR_VALUE(addr))
489 return log_msg_ret("gnvs", (int)addr);
491 acpi_inc_align(ctx, sizeof(struct acpi_global_nvs));
495 ACPI_WRITER(4gnvs, "GNVS", acpi_write_gnvs, 0);
497 /* MCFG is defined in the PCI Firmware Specification 3.0 */
498 int acpi_write_mcfg(struct acpi_ctx *ctx, const struct acpi_writer *entry)
500 struct acpi_table_header *header;
501 struct acpi_mcfg *mcfg;
505 header = &mcfg->header;
507 current = (u32)mcfg + sizeof(struct acpi_mcfg);
509 memset(mcfg, '\0', sizeof(struct acpi_mcfg));
511 /* Fill out header fields */
512 acpi_fill_header(header, "MCFG");
513 header->length = sizeof(struct acpi_mcfg);
514 header->revision = 1;
516 /* (Re)calculate length and checksum */
517 header->length = current - (u32)mcfg;
518 header->checksum = table_compute_checksum(mcfg, header->length);
520 acpi_inc(ctx, mcfg->header.length);
521 acpi_add_table(ctx, mcfg);
525 ACPI_WRITER(5mcfg, "MCFG", acpi_write_mcfg, 0);
528 * QEMU's version of write_acpi_tables is defined in drivers/misc/qfw.c
530 int write_acpi_tables_x86(struct acpi_ctx *ctx,
531 const struct acpi_writer *entry)
533 struct acpi_tcpa *tcpa;
534 struct acpi_csrt *csrt;
535 struct acpi_spcr *spcr;
538 if (IS_ENABLED(CONFIG_TPM_V1)) {
539 debug("ACPI: * TCPA\n");
540 tcpa = (struct acpi_tcpa *)ctx->current;
541 ret = acpi_create_tcpa(tcpa);
543 log_warning("Failed to create TCPA table (err=%d)\n",
546 acpi_inc_align(ctx, tcpa->header.length);
547 acpi_add_table(ctx, tcpa);
551 debug("ACPI: * CSRT\n");
553 if (!acpi_create_csrt(csrt)) {
554 acpi_inc_align(ctx, csrt->header.length);
555 acpi_add_table(ctx, csrt);
558 debug("ACPI: * SPCR\n");
560 acpi_create_spcr(spcr);
561 acpi_inc_align(ctx, spcr->header.length);
562 acpi_add_table(ctx, spcr);
564 acpi_write_dev_tables(ctx);
566 acpi_rsdp_addr = (unsigned long)ctx->rsdp;
567 debug("ACPI: done\n");
571 ACPI_WRITER(9x86, NULL, write_acpi_tables_x86, 0);
573 ulong acpi_get_rsdp_addr(void)
575 return acpi_rsdp_addr;
579 * acpi_write_hpet() - Write out a HPET table
581 * Write out the table for High-Precision Event Timers
583 * @hpet: Place to put HPET table
585 static int acpi_create_hpet(struct acpi_hpet *hpet)
587 struct acpi_table_header *header = &hpet->header;
588 struct acpi_gen_regaddr *addr = &hpet->addr;
591 * See IA-PC HPET (High Precision Event Timers) Specification v1.0a
592 * https://www.intel.com/content/dam/www/public/us/en/documents/technical-specifications/software-developers-hpet-spec-1-0a.pdf
594 memset((void *)hpet, '\0', sizeof(struct acpi_hpet));
596 /* Fill out header fields. */
597 acpi_fill_header(header, "HPET");
599 header->aslc_revision = ASL_REVISION;
600 header->length = sizeof(struct acpi_hpet);
601 header->revision = acpi_get_table_revision(ACPITAB_HPET);
603 /* Fill out HPET address */
604 addr->space_id = 0; /* Memory */
605 addr->bit_width = 64;
606 addr->bit_offset = 0;
607 addr->addrl = CONFIG_HPET_ADDRESS & 0xffffffff;
608 addr->addrh = ((unsigned long long)CONFIG_HPET_ADDRESS) >> 32;
610 hpet->id = *(u32 *)CONFIG_HPET_ADDRESS;
612 hpet->min_tick = 0; /* HPET_MIN_TICKS */
614 header->checksum = table_compute_checksum(hpet,
615 sizeof(struct acpi_hpet));
620 int acpi_write_hpet(struct acpi_ctx *ctx)
622 struct acpi_hpet *hpet;
625 log_debug("ACPI: * HPET\n");
628 acpi_inc_align(ctx, sizeof(struct acpi_hpet));
629 acpi_create_hpet(hpet);
630 ret = acpi_add_table(ctx, hpet);
632 return log_msg_ret("add", ret);
637 int acpi_write_dbg2_pci_uart(struct acpi_ctx *ctx, struct udevice *dev,
640 struct acpi_dbg2_header *dbg2 = ctx->current;
641 char path[ACPI_PATH_MAX];
642 struct acpi_gen_regaddr address;
646 if (!device_active(dev)) {
647 log_info("Device not enabled\n");
651 * PCI devices don't remember their resource allocation information in
652 * U-Boot at present. We assume that MMIO is used for the UART and that
653 * the address space is 32 bytes: ns16550 uses 8 registers of up to
654 * 32-bits each. This is only for debugging so it is not a big deal.
656 addr = dm_pci_read_bar32(dev, 0);
657 log_debug("UART addr %lx\n", (ulong)addr);
659 memset(&address, '\0', sizeof(address));
660 address.space_id = ACPI_ADDRESS_SPACE_MEMORY;
661 address.addrl = (uint32_t)addr;
662 address.addrh = (uint32_t)((addr >> 32) & 0xffffffff);
663 address.access_size = access_size;
665 ret = acpi_device_path(dev, path, sizeof(path));
667 return log_msg_ret("path", ret);
668 acpi_create_dbg2(dbg2, ACPI_DBG2_SERIAL_PORT,
669 ACPI_DBG2_16550_COMPATIBLE, &address, 0x1000, path);
671 acpi_inc_align(ctx, dbg2->header.length);
672 acpi_add_table(ctx, dbg2);
677 void acpi_fadt_common(struct acpi_fadt *fadt, struct acpi_facs *facs,
680 struct acpi_table_header *header = &fadt->header;
682 memset((void *)fadt, '\0', sizeof(struct acpi_fadt));
684 acpi_fill_header(header, "FACP");
685 header->length = sizeof(struct acpi_fadt);
686 header->revision = 4;
687 memcpy(header->oem_id, OEM_ID, 6);
688 memcpy(header->oem_table_id, OEM_TABLE_ID, 8);
689 memcpy(header->aslc_id, ASLC_ID, 4);
690 header->aslc_revision = 1;
692 fadt->firmware_ctrl = (unsigned long)facs;
693 fadt->dsdt = (unsigned long)dsdt;
695 fadt->x_firmware_ctl_l = (unsigned long)facs;
696 fadt->x_firmware_ctl_h = 0;
697 fadt->x_dsdt_l = (unsigned long)dsdt;
700 fadt->preferred_pm_profile = ACPI_PM_MOBILE;
702 /* Use ACPI 3.0 revision */
703 fadt->header.revision = 4;
706 void acpi_create_dmar_drhd(struct acpi_ctx *ctx, uint flags, uint segment,
709 struct dmar_entry *drhd = ctx->current;
711 memset(drhd, '\0', sizeof(*drhd));
712 drhd->type = DMAR_DRHD;
713 drhd->length = sizeof(*drhd); /* will be fixed up later */
715 drhd->segment = segment;
717 acpi_inc(ctx, drhd->length);
720 void acpi_create_dmar_rmrr(struct acpi_ctx *ctx, uint segment, u64 bar,
723 struct dmar_rmrr_entry *rmrr = ctx->current;
725 memset(rmrr, '\0', sizeof(*rmrr));
726 rmrr->type = DMAR_RMRR;
727 rmrr->length = sizeof(*rmrr); /* will be fixed up later */
728 rmrr->segment = segment;
731 acpi_inc(ctx, rmrr->length);
734 void acpi_dmar_drhd_fixup(struct acpi_ctx *ctx, void *base)
736 struct dmar_entry *drhd = base;
738 drhd->length = ctx->current - base;
741 void acpi_dmar_rmrr_fixup(struct acpi_ctx *ctx, void *base)
743 struct dmar_rmrr_entry *rmrr = base;
745 rmrr->length = ctx->current - base;
748 static int acpi_create_dmar_ds(struct acpi_ctx *ctx, enum dev_scope_type type,
749 uint enumeration_id, pci_dev_t bdf)
751 /* we don't support longer paths yet */
752 const size_t dev_scope_length = sizeof(struct dev_scope) + 2;
753 struct dev_scope *ds = ctx->current;
755 memset(ds, '\0', dev_scope_length);
757 ds->length = dev_scope_length;
758 ds->enumeration = enumeration_id;
759 ds->start_bus = PCI_BUS(bdf);
760 ds->path[0].dev = PCI_DEV(bdf);
761 ds->path[0].fn = PCI_FUNC(bdf);
766 int acpi_create_dmar_ds_pci_br(struct acpi_ctx *ctx, pci_dev_t bdf)
768 return acpi_create_dmar_ds(ctx, SCOPE_PCI_SUB, 0, bdf);
771 int acpi_create_dmar_ds_pci(struct acpi_ctx *ctx, pci_dev_t bdf)
773 return acpi_create_dmar_ds(ctx, SCOPE_PCI_ENDPOINT, 0, bdf);
776 int acpi_create_dmar_ds_ioapic(struct acpi_ctx *ctx, uint enumeration_id,
779 return acpi_create_dmar_ds(ctx, SCOPE_IOAPIC, enumeration_id, bdf);
782 int acpi_create_dmar_ds_msi_hpet(struct acpi_ctx *ctx, uint enumeration_id,
785 return acpi_create_dmar_ds(ctx, SCOPE_MSI_HPET, enumeration_id, bdf);