blob: 5c15a5e6afc1e28b309f902587392aff708f0d6c [file] [log] [blame]
Patrick Georgi11f00792020-03-04 15:10:45 +01001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Stefan Reinauer688b3852004-01-28 16:56:14 +00003/*
Martin Roth20bbd812019-08-30 21:09:37 -06004 * coreboot ACPI Table support
Stefan Reinauer777224c2005-01-19 14:06:41 +00005 */
6
Stefan Reinauer14e22772010-04-27 06:56:47 +00007/*
Stefan Reinauer777224c2005-01-19 14:06:41 +00008 * Each system port implementing ACPI has to provide two functions:
Stefan Reinauer14e22772010-04-27 06:56:47 +00009 *
Stefan Reinauer777224c2005-01-19 14:06:41 +000010 * write_acpi_tables()
11 * acpi_dump_apics()
Stefan Reinauer14e22772010-04-27 06:56:47 +000012 *
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000013 * See Kontron 986LCD-M port for a good example of an ACPI implementation
14 * in coreboot.
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000015 */
Stefan Reinauer688b3852004-01-28 16:56:14 +000016
17#include <console/console.h>
18#include <string.h>
19#include <arch/acpi.h>
Martin Rotha66df492016-09-06 10:22:34 -060020#include <arch/acpi_ivrs.h>
Rudolf Marek293b5f52009-02-01 18:35:15 +000021#include <arch/acpigen.h>
Stefan Reinauer777224c2005-01-19 14:06:41 +000022#include <device/pci.h>
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000023#include <cbmem.h>
Elyes HAOUASe2d152c2019-06-21 07:06:50 +020024#include <commonlib/helpers.h>
Patrick Georgic8feedd2012-02-16 18:43:25 +010025#include <cpu/x86/lapic_def.h>
Duncan Laurie11290c42012-10-03 19:07:05 -070026#include <cpu/cpu.h>
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +010027#include <cbfs.h>
Elyes HAOUAS26071aa2019-02-15 08:21:33 +010028#include <version.h>
Werner Zehdb561e62019-02-19 13:39:56 +010029#include <commonlib/sort.h>
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000030
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +020031static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp);
32
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000033u8 acpi_checksum(u8 *table, u32 length)
Stefan Reinauer688b3852004-01-28 16:56:14 +000034{
Uwe Hermann622824c2010-11-19 15:14:42 +000035 u8 ret = 0;
Stefan Reinauer688b3852004-01-28 16:56:14 +000036 while (length--) {
37 ret += *table;
38 table++;
39 }
Stefan Reinauera7648c22004-01-29 17:31:34 +000040 return -ret;
Stefan Reinauer688b3852004-01-28 16:56:14 +000041}
42
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000043/**
Uwe Hermann622824c2010-11-19 15:14:42 +000044 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
45 * and checksum.
Stefan Reinauer06feb882004-02-03 16:11:35 +000046 */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000047void acpi_add_table(acpi_rsdp_t *rsdp, void *table)
Stefan Reinauer688b3852004-01-28 16:56:14 +000048{
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000049 int i, entries_num;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000050 acpi_rsdt_t *rsdt;
51 acpi_xsdt_t *xsdt = NULL;
52
Uwe Hermann622824c2010-11-19 15:14:42 +000053 /* The RSDT is mandatory... */
Stefan Reinauerdefee172015-06-18 01:11:19 -070054 rsdt = (acpi_rsdt_t *)(uintptr_t)rsdp->rsdt_address;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000055
Uwe Hermann622824c2010-11-19 15:14:42 +000056 /* ...while the XSDT is not. */
57 if (rsdp->xsdt_address)
Stefan Reinauerdefee172015-06-18 01:11:19 -070058 xsdt = (acpi_xsdt_t *)((uintptr_t)rsdp->xsdt_address);
Stefan Reinauer14e22772010-04-27 06:56:47 +000059
Uwe Hermann622824c2010-11-19 15:14:42 +000060 /* This should always be MAX_ACPI_TABLES. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000061 entries_num = ARRAY_SIZE(rsdt->entry);
Stefan Reinauer14e22772010-04-27 06:56:47 +000062
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000063 for (i = 0; i < entries_num; i++) {
Uwe Hermann622824c2010-11-19 15:14:42 +000064 if (rsdt->entry[i] == 0)
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000065 break;
Stefan Reinauer688b3852004-01-28 16:56:14 +000066 }
67
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000068 if (i >= entries_num) {
Uwe Hermann622824c2010-11-19 15:14:42 +000069 printk(BIOS_ERR, "ACPI: Error: Could not add ACPI table, "
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +030070 "too many tables.\n");
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000071 return;
72 }
73
Uwe Hermann622824c2010-11-19 15:14:42 +000074 /* Add table to the RSDT. */
Stefan Reinauerdefee172015-06-18 01:11:19 -070075 rsdt->entry[i] = (uintptr_t)table;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000076
Uwe Hermann622824c2010-11-19 15:14:42 +000077 /* Fix RSDT length or the kernel will assume invalid entries. */
78 rsdt->header.length = sizeof(acpi_header_t) + (sizeof(u32) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000079
Uwe Hermann622824c2010-11-19 15:14:42 +000080 /* Re-calculate checksum. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000081 rsdt->header.checksum = 0; /* Hope this won't get optimized away */
Uwe Hermann622824c2010-11-19 15:14:42 +000082 rsdt->header.checksum = acpi_checksum((u8 *)rsdt, rsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000083
Uwe Hermann622824c2010-11-19 15:14:42 +000084 /*
85 * And now the same thing for the XSDT. We use the same index as for
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000086 * now we want the XSDT and RSDT to always be in sync in coreboot.
87 */
88 if (xsdt) {
Uwe Hermann622824c2010-11-19 15:14:42 +000089 /* Add table to the XSDT. */
Stefan Reinauerdefee172015-06-18 01:11:19 -070090 xsdt->entry[i] = (u64)(uintptr_t)table;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000091
Uwe Hermann622824c2010-11-19 15:14:42 +000092 /* Fix XSDT length. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000093 xsdt->header.length = sizeof(acpi_header_t) +
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +030094 (sizeof(u64) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000095
Uwe Hermann622824c2010-11-19 15:14:42 +000096 /* Re-calculate checksum. */
97 xsdt->header.checksum = 0;
98 xsdt->header.checksum = acpi_checksum((u8 *)xsdt,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +030099 xsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000100 }
101
Uwe Hermann622824c2010-11-19 15:14:42 +0000102 printk(BIOS_DEBUG, "ACPI: added table %d/%d, length now %d\n",
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300103 i + 1, entries_num, rsdt->header.length);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000104}
105
Uwe Hermann622824c2010-11-19 15:14:42 +0000106int acpi_create_mcfg_mmconfig(acpi_mcfg_mmconfig_t *mmconfig, u32 base,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300107 u16 seg_nr, u8 start, u8 end)
Stefan Reinauer26d431a2009-01-20 19:17:51 +0000108{
Vladimir Serbinenko60fccdc2014-10-05 11:05:16 +0200109 memset(mmconfig, 0, sizeof(*mmconfig));
Rudolf Mareke6409f22007-11-03 12:50:26 +0000110 mmconfig->base_address = base;
111 mmconfig->base_reserved = 0;
112 mmconfig->pci_segment_group_number = seg_nr;
113 mmconfig->start_bus_number = start;
114 mmconfig->end_bus_number = end;
Uwe Hermann622824c2010-11-19 15:14:42 +0000115
116 return sizeof(acpi_mcfg_mmconfig_t);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000117}
118
Stefan Reinauer777224c2005-01-19 14:06:41 +0000119int acpi_create_madt_lapic(acpi_madt_lapic_t *lapic, u8 cpu, u8 apic)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000120{
Himanshu Sahdev8c09b822019-10-21 18:50:50 +0530121 lapic->type = LOCAL_APIC; /* Local APIC structure */
Uwe Hermann622824c2010-11-19 15:14:42 +0000122 lapic->length = sizeof(acpi_madt_lapic_t);
123 lapic->flags = (1 << 0); /* Processor/LAPIC enabled */
124 lapic->processor_id = cpu;
125 lapic->apic_id = apic;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000126
Uwe Hermann622824c2010-11-19 15:14:42 +0000127 return lapic->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000128}
129
Patrick Rudolph56a3ef22020-03-24 17:29:49 +0100130int acpi_create_madt_lx2apic(acpi_madt_lx2apic_t *lapic, u32 cpu, u32 apic)
131{
132 lapic->type = LOCAL_X2APIC; /* Local APIC structure */
133 lapic->reserved = 0;
134 lapic->length = sizeof(acpi_madt_lx2apic_t);
135 lapic->flags = (1 << 0); /* Processor/LAPIC enabled */
136 lapic->processor_id = cpu;
137 lapic->x2apic_id = apic;
138
139 return lapic->length;
140}
141
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000142unsigned long acpi_create_madt_lapics(unsigned long current)
143{
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100144 struct device *cpu;
Werner Zeh423adfb2019-03-06 09:31:02 +0100145 int index, apic_ids[CONFIG_MAX_CPUS] = {0}, num_cpus = 0;
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000146
Uwe Hermann622824c2010-11-19 15:14:42 +0000147 for (cpu = all_devices; cpu; cpu = cpu->next) {
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000148 if ((cpu->path.type != DEVICE_PATH_APIC) ||
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800149 (cpu->bus->dev->path.type != DEVICE_PATH_CPU_CLUSTER)) {
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000150 continue;
151 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000152 if (!cpu->enabled)
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000153 continue;
Werner Zehdb561e62019-02-19 13:39:56 +0100154 if (num_cpus >= ARRAY_SIZE(apic_ids))
155 break;
156 apic_ids[num_cpus++] = cpu->path.apic.apic_id;
157 }
Werner Zehfedb36e2019-03-12 07:07:50 +0100158 if (num_cpus > 1)
159 bubblesort(apic_ids, num_cpus, NUM_ASCENDING);
Werner Zehdb561e62019-02-19 13:39:56 +0100160 for (index = 0; index < num_cpus; index++) {
Patrick Rudolph56a3ef22020-03-24 17:29:49 +0100161 if (apic_ids[index] < 0xff)
162 current += acpi_create_madt_lapic((acpi_madt_lapic_t *)current,
163 index, apic_ids[index]);
164 else
165 current += acpi_create_madt_lx2apic((acpi_madt_lx2apic_t *)current,
166 index, apic_ids[index]);
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000167 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000168
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000169 return current;
170}
171
Uwe Hermann622824c2010-11-19 15:14:42 +0000172int acpi_create_madt_ioapic(acpi_madt_ioapic_t *ioapic, u8 id, u32 addr,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300173 u32 gsi_base)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000174{
Himanshu Sahdev8c09b822019-10-21 18:50:50 +0530175 ioapic->type = IO_APIC; /* I/O APIC structure */
Uwe Hermann622824c2010-11-19 15:14:42 +0000176 ioapic->length = sizeof(acpi_madt_ioapic_t);
177 ioapic->reserved = 0x00;
178 ioapic->gsi_base = gsi_base;
179 ioapic->ioapic_id = id;
180 ioapic->ioapic_addr = addr;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000181
Uwe Hermann622824c2010-11-19 15:14:42 +0000182 return ioapic->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000183}
184
Stefan Reinauer777224c2005-01-19 14:06:41 +0000185int acpi_create_madt_irqoverride(acpi_madt_irqoverride_t *irqoverride,
Stefan Reinauer06feb882004-02-03 16:11:35 +0000186 u8 bus, u8 source, u32 gsirq, u16 flags)
187{
Himanshu Sahdev8c09b822019-10-21 18:50:50 +0530188 irqoverride->type = IRQ_SOURCE_OVERRIDE; /* Interrupt source override */
Uwe Hermann622824c2010-11-19 15:14:42 +0000189 irqoverride->length = sizeof(acpi_madt_irqoverride_t);
190 irqoverride->bus = bus;
191 irqoverride->source = source;
192 irqoverride->gsirq = gsirq;
193 irqoverride->flags = flags;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000194
Uwe Hermann622824c2010-11-19 15:14:42 +0000195 return irqoverride->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000196}
197
Stefan Reinauer777224c2005-01-19 14:06:41 +0000198int acpi_create_madt_lapic_nmi(acpi_madt_lapic_nmi_t *lapic_nmi, u8 cpu,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300199 u16 flags, u8 lint)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000200{
Himanshu Sahdev8c09b822019-10-21 18:50:50 +0530201 lapic_nmi->type = LOCAL_APIC_NMI; /* Local APIC NMI structure */
Uwe Hermann622824c2010-11-19 15:14:42 +0000202 lapic_nmi->length = sizeof(acpi_madt_lapic_nmi_t);
203 lapic_nmi->flags = flags;
204 lapic_nmi->processor_id = cpu;
205 lapic_nmi->lint = lint;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000206
Uwe Hermann622824c2010-11-19 15:14:42 +0000207 return lapic_nmi->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000208}
209
Patrick Rudolph56a3ef22020-03-24 17:29:49 +0100210int acpi_create_madt_lx2apic_nmi(acpi_madt_lx2apic_nmi_t *lapic_nmi, u32 cpu,
211 u16 flags, u8 lint)
212{
213 lapic_nmi->type = LOCAL_X2APIC_NMI; /* Local APIC NMI structure */
214 lapic_nmi->length = sizeof(acpi_madt_lx2apic_nmi_t);
215 lapic_nmi->flags = flags;
216 lapic_nmi->processor_id = cpu;
217 lapic_nmi->lint = lint;
218 lapic_nmi->reserved[0] = 0;
219 lapic_nmi->reserved[1] = 0;
220 lapic_nmi->reserved[2] = 0;
221
222 return lapic_nmi->length;
223}
224
Stefan Reinauer777224c2005-01-19 14:06:41 +0000225void acpi_create_madt(acpi_madt_t *madt)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000226{
Uwe Hermann622824c2010-11-19 15:14:42 +0000227 acpi_header_t *header = &(madt->header);
228 unsigned long current = (unsigned long)madt + sizeof(acpi_madt_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000229
Stefan Reinauer06feb882004-02-03 16:11:35 +0000230 memset((void *)madt, 0, sizeof(acpi_madt_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000231
John Zhao2ba303e2019-05-28 16:48:14 -0700232 if (!header)
233 return;
234
Uwe Hermann622824c2010-11-19 15:14:42 +0000235 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000236 memcpy(header->signature, "APIC", 4);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000237 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000238 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000239 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000240
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100241 header->asl_compiler_revision = asl_revision;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000242 header->length = sizeof(acpi_madt_t);
Marc Jones93a51762018-08-22 18:57:24 -0600243 header->revision = get_acpi_table_revision(MADT);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000244
Uwe Hermann622824c2010-11-19 15:14:42 +0000245 madt->lapic_addr = LOCAL_APIC_ADDR;
Nico Huber9df72e02018-11-24 18:25:50 +0100246 if (CONFIG(ACPI_HAVE_PCAT_8259))
247 madt->flags |= 1;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000248
Stefan Reinauerf622d592005-11-26 16:56:05 +0000249 current = acpi_fill_madt(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000250
Uwe Hermann622824c2010-11-19 15:14:42 +0000251 /* (Re)calculate length and checksum. */
252 header->length = current - (unsigned long)madt;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000253
Uwe Hermann622824c2010-11-19 15:14:42 +0000254 header->checksum = acpi_checksum((void *)madt, header->length);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000255}
256
Uwe Hermann622824c2010-11-19 15:14:42 +0000257/* MCFG is defined in the PCI Firmware Specification 3.0. */
Rudolf Mareke6409f22007-11-03 12:50:26 +0000258void acpi_create_mcfg(acpi_mcfg_t *mcfg)
259{
Uwe Hermann622824c2010-11-19 15:14:42 +0000260 acpi_header_t *header = &(mcfg->header);
261 unsigned long current = (unsigned long)mcfg + sizeof(acpi_mcfg_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000262
Rudolf Mareke6409f22007-11-03 12:50:26 +0000263 memset((void *)mcfg, 0, sizeof(acpi_mcfg_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000264
John Zhao2ba303e2019-05-28 16:48:14 -0700265 if (!header)
266 return;
267
Uwe Hermann622824c2010-11-19 15:14:42 +0000268 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000269 memcpy(header->signature, "MCFG", 4);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000270 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000271 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000272 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000273
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100274 header->asl_compiler_revision = asl_revision;
Rudolf Mareke6409f22007-11-03 12:50:26 +0000275 header->length = sizeof(acpi_mcfg_t);
Marc Jones93a51762018-08-22 18:57:24 -0600276 header->revision = get_acpi_table_revision(MCFG);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000277
278 current = acpi_fill_mcfg(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000279
Uwe Hermann622824c2010-11-19 15:14:42 +0000280 /* (Re)calculate length and checksum. */
281 header->length = current - (unsigned long)mcfg;
282 header->checksum = acpi_checksum((void *)mcfg, header->length);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000283}
284
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200285static void *get_tcpa_log(u32 *size)
286{
287 const struct cbmem_entry *ce;
288 const u32 tcpa_default_log_len = 0x10000;
289 void *lasa;
Philipp Deppenwiese791ba972018-07-30 01:15:20 +0200290 ce = cbmem_entry_find(CBMEM_ID_TCPA_TCG_LOG);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200291 if (ce) {
292 lasa = cbmem_entry_start(ce);
293 *size = cbmem_entry_size(ce);
294 printk(BIOS_DEBUG, "TCPA log found at %p\n", lasa);
295 return lasa;
296 }
Philipp Deppenwiese791ba972018-07-30 01:15:20 +0200297 lasa = cbmem_add(CBMEM_ID_TCPA_TCG_LOG, tcpa_default_log_len);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200298 if (!lasa) {
299 printk(BIOS_ERR, "TCPA log creation failed\n");
300 return NULL;
301 }
302
303 printk(BIOS_DEBUG, "TCPA log created at %p\n", lasa);
Lee Leahy024b13d2017-03-16 13:41:11 -0700304 memset(lasa, 0, tcpa_default_log_len);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200305
306 *size = tcpa_default_log_len;
307 return lasa;
308}
309
310static void acpi_create_tcpa(acpi_tcpa_t *tcpa)
311{
312 acpi_header_t *header = &(tcpa->header);
313 u32 tcpa_log_len;
314 void *lasa;
315
316 memset((void *)tcpa, 0, sizeof(acpi_tcpa_t));
317
318 lasa = get_tcpa_log(&tcpa_log_len);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700319 if (!lasa)
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200320 return;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200321
John Zhao2ba303e2019-05-28 16:48:14 -0700322 if (!header)
323 return;
324
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200325 /* Fill out header fields. */
326 memcpy(header->signature, "TCPA", 4);
327 memcpy(header->oem_id, OEM_ID, 6);
328 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
329 memcpy(header->asl_compiler_id, ASLC, 4);
330
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100331 header->asl_compiler_revision = asl_revision;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200332 header->length = sizeof(acpi_tcpa_t);
Marc Jones93a51762018-08-22 18:57:24 -0600333 header->revision = get_acpi_table_revision(TCPA);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200334
335 tcpa->platform_class = 0;
336 tcpa->laml = tcpa_log_len;
Stefan Reinauerdefee172015-06-18 01:11:19 -0700337 tcpa->lasa = (uintptr_t) lasa;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200338
339 /* Calculate checksum. */
340 header->checksum = acpi_checksum((void *)tcpa, header->length);
341}
342
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100343static void *get_tpm2_log(u32 *size)
344{
345 const struct cbmem_entry *ce;
346 const u32 tpm2_default_log_len = 0x10000;
347 void *lasa;
348 ce = cbmem_entry_find(CBMEM_ID_TPM2_TCG_LOG);
349 if (ce) {
350 lasa = cbmem_entry_start(ce);
351 *size = cbmem_entry_size(ce);
352 printk(BIOS_DEBUG, "TPM2 log found at %p\n", lasa);
353 return lasa;
354 }
355 lasa = cbmem_add(CBMEM_ID_TPM2_TCG_LOG, tpm2_default_log_len);
356 if (!lasa) {
357 printk(BIOS_ERR, "TPM2 log creation failed\n");
358 return NULL;
359 }
360
361 printk(BIOS_DEBUG, "TPM2 log created at %p\n", lasa);
362 memset(lasa, 0, tpm2_default_log_len);
363
364 *size = tpm2_default_log_len;
365 return lasa;
366}
367
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200368static void acpi_create_tpm2(acpi_tpm2_t *tpm2)
369{
370 acpi_header_t *header = &(tpm2->header);
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100371 u32 tpm2_log_len;
372 void *lasa;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200373
374 memset((void *)tpm2, 0, sizeof(acpi_tpm2_t));
375
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100376 /*
377 * Some payloads like SeaBIOS depend on log area to use TPM2.
378 * Get the memory size and address of TPM2 log area or initialize it.
379 */
380 lasa = get_tpm2_log(&tpm2_log_len);
381 if (!lasa)
382 tpm2_log_len = 0;
383
John Zhao2ba303e2019-05-28 16:48:14 -0700384 if (!header)
385 return;
386
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200387 /* Fill out header fields. */
388 memcpy(header->signature, "TPM2", 4);
389 memcpy(header->oem_id, OEM_ID, 6);
390 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
391 memcpy(header->asl_compiler_id, ASLC, 4);
392
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100393 header->asl_compiler_revision = asl_revision;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200394 header->length = sizeof(acpi_tpm2_t);
395 header->revision = get_acpi_table_revision(TPM2);
396
397 /* Hard to detect for coreboot. Just set it to 0 */
398 tpm2->platform_class = 0;
Christian Walter26e0d4c2019-07-14 15:47:23 +0200399 if (CONFIG(CRB_TPM)) {
400 /* Must be set to 7 for CRB Support */
401 tpm2->control_area = CONFIG_CRB_TPM_BASE_ADDRESS + 0x40;
402 tpm2->start_method = 7;
403 } else {
404 /* Must be set to 0 for FIFO interface support */
405 tpm2->control_area = 0;
406 tpm2->start_method = 6;
407 }
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200408 memset(tpm2->msp, 0, sizeof(tpm2->msp));
409
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100410 /* Fill the log area size and start address fields. */
411 tpm2->laml = tpm2_log_len;
412 tpm2->lasa = (uintptr_t) lasa;
413
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200414 /* Calculate checksum. */
415 header->checksum = acpi_checksum((void *)tpm2, header->length);
416}
417
Duncan Laurie37319032016-05-26 12:47:05 -0700418static void acpi_ssdt_write_cbtable(void)
419{
420 const struct cbmem_entry *cbtable;
421 uintptr_t base;
422 uint32_t size;
423
424 cbtable = cbmem_entry_find(CBMEM_ID_CBTABLE);
425 if (!cbtable)
426 return;
427 base = (uintptr_t)cbmem_entry_start(cbtable);
428 size = cbmem_entry_size(cbtable);
429
430 acpigen_write_device("CTBL");
431 acpigen_write_coreboot_hid(COREBOOT_ACPI_ID_CBTABLE);
432 acpigen_write_name_integer("_UID", 0);
David Wu5dff3962018-03-21 16:48:53 +0800433 acpigen_write_STA(ACPI_STATUS_DEVICE_HIDDEN_ON);
Duncan Laurie37319032016-05-26 12:47:05 -0700434 acpigen_write_name("_CRS");
435 acpigen_write_resourcetemplate_header();
436 acpigen_write_mem32fixed(0, base, size);
437 acpigen_write_resourcetemplate_footer();
438 acpigen_pop_len();
439}
440
Myles Watson3fe6b702009-10-09 20:13:43 +0000441void acpi_create_ssdt_generator(acpi_header_t *ssdt, const char *oem_table_id)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000442{
Uwe Hermann622824c2010-11-19 15:14:42 +0000443 unsigned long current = (unsigned long)ssdt + sizeof(acpi_header_t);
444
Rudolf Marek293b5f52009-02-01 18:35:15 +0000445 memset((void *)ssdt, 0, sizeof(acpi_header_t));
Uwe Hermann622824c2010-11-19 15:14:42 +0000446
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000447 memcpy(&ssdt->signature, "SSDT", 4);
Marc Jones93a51762018-08-22 18:57:24 -0600448 ssdt->revision = get_acpi_table_revision(SSDT);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000449 memcpy(&ssdt->oem_id, OEM_ID, 6);
450 memcpy(&ssdt->oem_table_id, oem_table_id, 8);
451 ssdt->oem_revision = 42;
Uwe Hermann622824c2010-11-19 15:14:42 +0000452 memcpy(&ssdt->asl_compiler_id, ASLC, 4);
Elyes HAOUAS26071aa2019-02-15 08:21:33 +0100453 ssdt->asl_compiler_revision = asl_revision;
Rudolf Marek293b5f52009-02-01 18:35:15 +0000454 ssdt->length = sizeof(acpi_header_t);
455
Stefan Reinauer8dcd50b2009-03-06 17:24:29 +0000456 acpigen_set_current((char *) current);
Duncan Laurie37319032016-05-26 12:47:05 -0700457
458 /* Write object to declare coreboot tables */
459 acpi_ssdt_write_cbtable();
460
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200461 {
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100462 struct device *dev;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200463 for (dev = all_devices; dev; dev = dev->next)
Nico Huber68680dd2020-03-31 17:34:52 +0200464 if (dev->ops && dev->ops->acpi_fill_ssdt)
465 dev->ops->acpi_fill_ssdt(dev);
Vladimir Serbinenko0a669912014-10-05 14:34:17 +0200466 current = (unsigned long) acpigen_get_current();
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200467 }
Rudolf Marek293b5f52009-02-01 18:35:15 +0000468
Uwe Hermann622824c2010-11-19 15:14:42 +0000469 /* (Re)calculate length and checksum. */
Rudolf Marek293b5f52009-02-01 18:35:15 +0000470 ssdt->length = current - (unsigned long)ssdt;
471 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
472}
473
Stefan Reinauerf622d592005-11-26 16:56:05 +0000474int acpi_create_srat_lapic(acpi_srat_lapic_t *lapic, u8 node, u8 apic)
475{
Myles Watson0b4c9f02009-03-10 18:06:47 +0000476 memset((void *)lapic, 0, sizeof(acpi_srat_lapic_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000477
Uwe Hermann622824c2010-11-19 15:14:42 +0000478 lapic->type = 0; /* Processor local APIC/SAPIC affinity structure */
479 lapic->length = sizeof(acpi_srat_lapic_t);
480 lapic->flags = (1 << 0); /* Enabled (the use of this structure). */
481 lapic->proximity_domain_7_0 = node;
482 /* TODO: proximity_domain_31_8, local SAPIC EID, clock domain. */
483 lapic->apic_id = apic;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000484
Uwe Hermann622824c2010-11-19 15:14:42 +0000485 return lapic->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000486}
487
Uwe Hermann622824c2010-11-19 15:14:42 +0000488int acpi_create_srat_mem(acpi_srat_mem_t *mem, u8 node, u32 basek, u32 sizek,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300489 u32 flags)
Stefan Reinauerf622d592005-11-26 16:56:05 +0000490{
Uwe Hermann622824c2010-11-19 15:14:42 +0000491 mem->type = 1; /* Memory affinity structure */
492 mem->length = sizeof(acpi_srat_mem_t);
493 mem->base_address_low = (basek << 10);
494 mem->base_address_high = (basek >> (32 - 10));
495 mem->length_low = (sizek << 10);
496 mem->length_high = (sizek >> (32 - 10));
497 mem->proximity_domain = node;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000498 mem->flags = flags;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000499
Uwe Hermann622824c2010-11-19 15:14:42 +0000500 return mem->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000501}
502
Uwe Hermann622824c2010-11-19 15:14:42 +0000503/* http://www.microsoft.com/whdc/system/sysinternals/sratdwn.mspx */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +0200504void acpi_create_srat(acpi_srat_t *srat,
505 unsigned long (*acpi_fill_srat)(unsigned long current))
Stefan Reinauerf622d592005-11-26 16:56:05 +0000506{
Uwe Hermann622824c2010-11-19 15:14:42 +0000507 acpi_header_t *header = &(srat->header);
508 unsigned long current = (unsigned long)srat + sizeof(acpi_srat_t);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000509
Uwe Hermann622824c2010-11-19 15:14:42 +0000510 memset((void *)srat, 0, sizeof(acpi_srat_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000511
John Zhao2ba303e2019-05-28 16:48:14 -0700512 if (!header)
513 return;
514
Uwe Hermann622824c2010-11-19 15:14:42 +0000515 /* Fill out header fields. */
516 memcpy(header->signature, "SRAT", 4);
517 memcpy(header->oem_id, OEM_ID, 6);
518 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
519 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000520
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100521 header->asl_compiler_revision = asl_revision;
Uwe Hermann622824c2010-11-19 15:14:42 +0000522 header->length = sizeof(acpi_srat_t);
Marc Jones93a51762018-08-22 18:57:24 -0600523 header->revision = get_acpi_table_revision(SRAT);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000524
Uwe Hermann622824c2010-11-19 15:14:42 +0000525 srat->resv = 1; /* Spec: Reserved to 1 for backwards compatibility. */
Stefan Reinauerf622d592005-11-26 16:56:05 +0000526
Uwe Hermann622824c2010-11-19 15:14:42 +0000527 current = acpi_fill_srat(current);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000528
Uwe Hermann622824c2010-11-19 15:14:42 +0000529 /* (Re)calculate length and checksum. */
530 header->length = current - (unsigned long)srat;
531 header->checksum = acpi_checksum((void *)srat, header->length);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000532}
533
Nico Hubere561f352015-10-26 11:51:25 +0100534void acpi_create_dmar(acpi_dmar_t *dmar, enum dmar_flags flags,
Lee Leahy024b13d2017-03-16 13:41:11 -0700535 unsigned long (*acpi_fill_dmar)(unsigned long))
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200536{
537 acpi_header_t *header = &(dmar->header);
538 unsigned long current = (unsigned long)dmar + sizeof(acpi_dmar_t);
539
540 memset((void *)dmar, 0, sizeof(acpi_dmar_t));
541
John Zhao2ba303e2019-05-28 16:48:14 -0700542 if (!header)
543 return;
544
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200545 /* Fill out header fields. */
546 memcpy(header->signature, "DMAR", 4);
547 memcpy(header->oem_id, OEM_ID, 6);
548 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
549 memcpy(header->asl_compiler_id, ASLC, 4);
550
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100551 header->asl_compiler_revision = asl_revision;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200552 header->length = sizeof(acpi_dmar_t);
Marc Jones93a51762018-08-22 18:57:24 -0600553 header->revision = get_acpi_table_revision(DMAR);
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200554
Jacob Laskaf3f654d2016-03-15 21:53:27 -0500555 dmar->host_address_width = cpu_phys_address_size() - 1;
Nico Hubere561f352015-10-26 11:51:25 +0100556 dmar->flags = flags;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200557
558 current = acpi_fill_dmar(current);
559
560 /* (Re)calculate length and checksum. */
561 header->length = current - (unsigned long)dmar;
562 header->checksum = acpi_checksum((void *)dmar, header->length);
563}
564
565unsigned long acpi_create_dmar_drhd(unsigned long current, u8 flags,
Matt DeVillier7866d492018-03-29 14:59:57 +0200566 u16 segment, u64 bar)
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200567{
568 dmar_entry_t *drhd = (dmar_entry_t *)current;
569 memset(drhd, 0, sizeof(*drhd));
570 drhd->type = DMAR_DRHD;
571 drhd->length = sizeof(*drhd); /* will be fixed up later */
572 drhd->flags = flags;
573 drhd->segment = segment;
574 drhd->bar = bar;
575
576 return drhd->length;
577}
578
Matt DeVillier7866d492018-03-29 14:59:57 +0200579unsigned long acpi_create_dmar_rmrr(unsigned long current, u16 segment,
580 u64 bar, u64 limit)
581{
582 dmar_rmrr_entry_t *rmrr = (dmar_rmrr_entry_t *)current;
583 memset(rmrr, 0, sizeof(*rmrr));
584 rmrr->type = DMAR_RMRR;
585 rmrr->length = sizeof(*rmrr); /* will be fixed up later */
586 rmrr->segment = segment;
587 rmrr->bar = bar;
588 rmrr->limit = limit;
589
590 return rmrr->length;
591}
592
Werner Zehd4d76952016-07-27 06:56:36 +0200593unsigned long acpi_create_dmar_atsr(unsigned long current, u8 flags,
594 u16 segment)
595{
596 dmar_atsr_entry_t *atsr = (dmar_atsr_entry_t *)current;
597 memset(atsr, 0, sizeof(*atsr));
598 atsr->type = DMAR_ATSR;
599 atsr->length = sizeof(*atsr); /* will be fixed up later */
600 atsr->flags = flags;
601 atsr->segment = segment;
602
603 return atsr->length;
604}
605
John Zhao76e70672019-04-04 11:03:28 -0700606unsigned long acpi_create_dmar_rhsa(unsigned long current, u64 base_addr,
607 u32 proximity_domain)
608{
609 dmar_rhsa_entry_t *rhsa = (dmar_rhsa_entry_t *)current;
610 memset(rhsa, 0, sizeof(*rhsa));
611 rhsa->type = DMAR_RHSA;
612 rhsa->length = sizeof(*rhsa);
613 rhsa->base_address = base_addr;
614 rhsa->proximity_domain = proximity_domain;
615
616 return rhsa->length;
617}
618
619unsigned long acpi_create_dmar_andd(unsigned long current, u8 device_number,
620 const char *device_name)
621{
622 dmar_andd_entry_t *andd = (dmar_andd_entry_t *)current;
623 int andd_len = sizeof(dmar_andd_entry_t) + strlen(device_name) + 1;
624 memset(andd, 0, andd_len);
625 andd->type = DMAR_ANDD;
626 andd->length = andd_len;
627 andd->device_number = device_number;
628 memcpy(&andd->device_name, device_name, strlen(device_name));
629
630 return andd->length;
631}
632
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200633void acpi_dmar_drhd_fixup(unsigned long base, unsigned long current)
634{
635 dmar_entry_t *drhd = (dmar_entry_t *)base;
636 drhd->length = current - base;
637}
638
Matt DeVillier7866d492018-03-29 14:59:57 +0200639void acpi_dmar_rmrr_fixup(unsigned long base, unsigned long current)
640{
641 dmar_rmrr_entry_t *rmrr = (dmar_rmrr_entry_t *)base;
642 rmrr->length = current - base;
643}
644
Werner Zehd4d76952016-07-27 06:56:36 +0200645void acpi_dmar_atsr_fixup(unsigned long base, unsigned long current)
646{
647 dmar_atsr_entry_t *atsr = (dmar_atsr_entry_t *)base;
648 atsr->length = current - base;
649}
650
Matt DeVillier7866d492018-03-29 14:59:57 +0200651static unsigned long acpi_create_dmar_ds(unsigned long current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100652 enum dev_scope_type type, u8 enumeration_id, u8 bus, u8 dev, u8 fn)
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200653{
Nico Huber6c4751d2015-10-26 12:03:54 +0100654 /* we don't support longer paths yet */
655 const size_t dev_scope_length = sizeof(dev_scope_t) + 2;
656
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200657 dev_scope_t *ds = (dev_scope_t *)current;
Nico Huber6c4751d2015-10-26 12:03:54 +0100658 memset(ds, 0, dev_scope_length);
659 ds->type = type;
660 ds->length = dev_scope_length;
661 ds->enumeration = enumeration_id;
662 ds->start_bus = bus;
663 ds->path[0].dev = dev;
664 ds->path[0].fn = fn;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200665
666 return ds->length;
667}
668
Matt DeVillier7866d492018-03-29 14:59:57 +0200669unsigned long acpi_create_dmar_ds_pci_br(unsigned long current, u8 bus,
Werner Zeh21a5bff2016-07-27 07:07:20 +0200670 u8 dev, u8 fn)
671{
Matt DeVillier7866d492018-03-29 14:59:57 +0200672 return acpi_create_dmar_ds(current,
Werner Zeh21a5bff2016-07-27 07:07:20 +0200673 SCOPE_PCI_SUB, 0, bus, dev, fn);
674}
675
Matt DeVillier7866d492018-03-29 14:59:57 +0200676unsigned long acpi_create_dmar_ds_pci(unsigned long current, u8 bus,
Nico Huber6c4751d2015-10-26 12:03:54 +0100677 u8 dev, u8 fn)
678{
Matt DeVillier7866d492018-03-29 14:59:57 +0200679 return acpi_create_dmar_ds(current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100680 SCOPE_PCI_ENDPOINT, 0, bus, dev, fn);
681}
682
Matt DeVillier7866d492018-03-29 14:59:57 +0200683unsigned long acpi_create_dmar_ds_ioapic(unsigned long current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100684 u8 enumeration_id, u8 bus, u8 dev, u8 fn)
685{
Matt DeVillier7866d492018-03-29 14:59:57 +0200686 return acpi_create_dmar_ds(current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100687 SCOPE_IOAPIC, enumeration_id, bus, dev, fn);
688}
689
Matt DeVillier7866d492018-03-29 14:59:57 +0200690unsigned long acpi_create_dmar_ds_msi_hpet(unsigned long current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100691 u8 enumeration_id, u8 bus, u8 dev, u8 fn)
692{
Matt DeVillier7866d492018-03-29 14:59:57 +0200693 return acpi_create_dmar_ds(current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100694 SCOPE_MSI_HPET, enumeration_id, bus, dev, fn);
695}
696
Uwe Hermann622824c2010-11-19 15:14:42 +0000697/* http://h21007.www2.hp.com/portal/download/files/unprot/Itanium/slit.pdf */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +0200698void acpi_create_slit(acpi_slit_t *slit,
699 unsigned long (*acpi_fill_slit)(unsigned long current))
Yinghai Lud4b278c2006-10-04 20:46:15 +0000700{
Uwe Hermann622824c2010-11-19 15:14:42 +0000701 acpi_header_t *header = &(slit->header);
702 unsigned long current = (unsigned long)slit + sizeof(acpi_slit_t);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000703
Uwe Hermann622824c2010-11-19 15:14:42 +0000704 memset((void *)slit, 0, sizeof(acpi_slit_t));
Yinghai Lud4b278c2006-10-04 20:46:15 +0000705
John Zhao2ba303e2019-05-28 16:48:14 -0700706 if (!header)
707 return;
708
Uwe Hermann622824c2010-11-19 15:14:42 +0000709 /* Fill out header fields. */
710 memcpy(header->signature, "SLIT", 4);
711 memcpy(header->oem_id, OEM_ID, 6);
712 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
713 memcpy(header->asl_compiler_id, ASLC, 4);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000714
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100715 header->asl_compiler_revision = asl_revision;
Uwe Hermann622824c2010-11-19 15:14:42 +0000716 header->length = sizeof(acpi_slit_t);
Marc Jones93a51762018-08-22 18:57:24 -0600717 header->revision = get_acpi_table_revision(SLIT);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000718
Uwe Hermann622824c2010-11-19 15:14:42 +0000719 current = acpi_fill_slit(current);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000720
Uwe Hermann622824c2010-11-19 15:14:42 +0000721 /* (Re)calculate length and checksum. */
722 header->length = current - (unsigned long)slit;
723 header->checksum = acpi_checksum((void *)slit, header->length);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000724}
725
Uwe Hermann622824c2010-11-19 15:14:42 +0000726/* http://www.intel.com/hardwaredesign/hpetspec_1.pdf */
Stefan Reinauer777224c2005-01-19 14:06:41 +0000727void acpi_create_hpet(acpi_hpet_t *hpet)
Stefan Reinauer688b3852004-01-28 16:56:14 +0000728{
Uwe Hermann622824c2010-11-19 15:14:42 +0000729 acpi_header_t *header = &(hpet->header);
730 acpi_addr_t *addr = &(hpet->addr);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000731
Stefan Reinauera7648c22004-01-29 17:31:34 +0000732 memset((void *)hpet, 0, sizeof(acpi_hpet_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000733
John Zhao2ba303e2019-05-28 16:48:14 -0700734 if (!header)
735 return;
736
Uwe Hermann622824c2010-11-19 15:14:42 +0000737 /* Fill out header fields. */
Stefan Reinauer4704dc52009-07-22 01:11:37 +0000738 memcpy(header->signature, "HPET", 4);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000739 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000740 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000741 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000742
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100743 header->asl_compiler_revision = asl_revision;
Stefan Reinauer688b3852004-01-28 16:56:14 +0000744 header->length = sizeof(acpi_hpet_t);
Marc Jones93a51762018-08-22 18:57:24 -0600745 header->revision = get_acpi_table_revision(HPET);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000746
Uwe Hermann622824c2010-11-19 15:14:42 +0000747 /* Fill out HPET address. */
748 addr->space_id = 0; /* Memory */
749 addr->bit_width = 64;
750 addr->bit_offset = 0;
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200751 addr->addrl = CONFIG_HPET_ADDRESS & 0xffffffff;
752 addr->addrh = ((unsigned long long)CONFIG_HPET_ADDRESS) >> 32;
Stefan Reinauer688b3852004-01-28 16:56:14 +0000753
Lee Leahy024b13d2017-03-16 13:41:11 -0700754 hpet->id = *(unsigned int *)CONFIG_HPET_ADDRESS;
Uwe Hermann622824c2010-11-19 15:14:42 +0000755 hpet->number = 0;
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200756 hpet->min_tick = CONFIG_HPET_MIN_TICKS;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000757
Uwe Hermann622824c2010-11-19 15:14:42 +0000758 header->checksum = acpi_checksum((void *)hpet, sizeof(acpi_hpet_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000759}
Uwe Hermann622824c2010-11-19 15:14:42 +0000760
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700761void acpi_create_vfct(const struct device *device,
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530762 acpi_vfct_t *vfct,
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700763 unsigned long (*acpi_fill_vfct)(const struct device *device,
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530764 acpi_vfct_t *vfct_struct, unsigned long current))
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200765{
766 acpi_header_t *header = &(vfct->header);
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530767 unsigned long current = (unsigned long)vfct + sizeof(acpi_vfct_t);
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200768
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530769 memset((void *)vfct, 0, sizeof(acpi_vfct_t));
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200770
John Zhao2ba303e2019-05-28 16:48:14 -0700771 if (!header)
772 return;
773
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200774 /* Fill out header fields. */
775 memcpy(header->signature, "VFCT", 4);
776 memcpy(header->oem_id, OEM_ID, 6);
777 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
778 memcpy(header->asl_compiler_id, ASLC, 4);
779
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100780 header->asl_compiler_revision = asl_revision;
Marc Jones93a51762018-08-22 18:57:24 -0600781 header->revision = get_acpi_table_revision(VFCT);
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200782
783 current = acpi_fill_vfct(device, vfct, current);
784
Kyösti Mälkkifb493792019-06-30 06:29:52 +0300785 /* If no BIOS image, return with header->length == 0. */
786 if (!vfct->VBIOSImageOffset)
787 return;
788
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200789 /* (Re)calculate length and checksum. */
790 header->length = current - (unsigned long)vfct;
791 header->checksum = acpi_checksum((void *)vfct, header->length);
792}
793
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700794void acpi_create_ipmi(const struct device *device,
Patrick Rudolph9d98e5a2019-06-14 19:00:04 +0200795 struct acpi_spmi *spmi,
796 const u16 ipmi_revision,
797 const acpi_addr_t *addr,
798 const enum acpi_ipmi_interface_type type,
799 const s8 gpe_interrupt,
800 const u32 apic_interrupt,
801 const u32 uid)
802{
803 acpi_header_t *header = &(spmi->header);
804 memset((void *)spmi, 0, sizeof(struct acpi_spmi));
805
806 /* Fill out header fields. */
807 memcpy(header->signature, "SPMI", 4);
808 memcpy(header->oem_id, OEM_ID, 6);
809 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
810 memcpy(header->asl_compiler_id, ASLC, 4);
811
812 header->asl_compiler_revision = asl_revision;
813 header->length = sizeof(struct acpi_spmi);
814 header->revision = get_acpi_table_revision(SPMI);
815
816 spmi->reserved = 1;
817
818 if (device->path.type == DEVICE_PATH_PCI) {
819 spmi->pci_device_flag = ACPI_IPMI_PCI_DEVICE_FLAG;
820 spmi->pci_bus = device->bus->secondary;
821 spmi->pci_device = device->path.pci.devfn >> 3;
822 spmi->pci_function = device->path.pci.devfn & 0x7;
823 } else if (type != IPMI_INTERFACE_SSIF) {
824 memcpy(spmi->uid, &uid, sizeof(spmi->uid));
825 }
826
827 spmi->base_address = *addr;
828 spmi->specification_revision = ipmi_revision;
829
830 spmi->interface_type = type;
831
832 if (gpe_interrupt >= 0 && gpe_interrupt < 32) {
833 spmi->gpe = gpe_interrupt;
834 spmi->interrupt_type |= ACPI_IPMI_INT_TYPE_SCI;
835 }
836 if (apic_interrupt > 0) {
837 spmi->global_system_interrupt = apic_interrupt;
838 spmi->interrupt_type |= ACPI_IPMI_INT_TYPE_APIC;
839 }
840
841 /* Calculate checksum. */
842 header->checksum = acpi_checksum((void *)spmi, header->length);
843}
844
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500845void acpi_create_ivrs(acpi_ivrs_t *ivrs,
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700846 unsigned long (*acpi_fill_ivrs)(acpi_ivrs_t *ivrs_struct,
847 unsigned long current))
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500848{
849 acpi_header_t *header = &(ivrs->header);
850 unsigned long current = (unsigned long)ivrs + sizeof(acpi_ivrs_t);
851
852 memset((void *)ivrs, 0, sizeof(acpi_ivrs_t));
853
John Zhao2ba303e2019-05-28 16:48:14 -0700854 if (!header)
855 return;
856
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500857 /* Fill out header fields. */
858 memcpy(header->signature, "IVRS", 4);
859 memcpy(header->oem_id, OEM_ID, 6);
860 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
861 memcpy(header->asl_compiler_id, ASLC, 4);
862
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100863 header->asl_compiler_revision = asl_revision;
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500864 header->length = sizeof(acpi_ivrs_t);
Marc Jones93a51762018-08-22 18:57:24 -0600865 header->revision = get_acpi_table_revision(IVRS);
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500866
867 current = acpi_fill_ivrs(ivrs, current);
868
869 /* (Re)calculate length and checksum. */
870 header->length = current - (unsigned long)ivrs;
871 header->checksum = acpi_checksum((void *)ivrs, header->length);
872}
873
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700874unsigned long acpi_write_hpet(const struct device *device, unsigned long current,
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700875 acpi_rsdp_t *rsdp)
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200876{
877 acpi_hpet_t *hpet;
878
879 /*
880 * We explicitly add these tables later on:
881 */
882 printk(BIOS_DEBUG, "ACPI: * HPET\n");
883
884 hpet = (acpi_hpet_t *) current;
885 current += sizeof(acpi_hpet_t);
Felix Heldc4697122019-06-20 13:50:17 +0200886 current = ALIGN_UP(current, 16);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200887 acpi_create_hpet(hpet);
888 acpi_add_table(rsdp, hpet);
889
890 return current;
891}
892
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800893void acpi_create_dbg2(acpi_dbg2_header_t *dbg2,
894 int port_type, int port_subtype,
895 acpi_addr_t *address, uint32_t address_size,
896 const char *device_path)
897{
898 uintptr_t current;
899 acpi_dbg2_device_t *device;
900 uint32_t *dbg2_addr_size;
901 acpi_header_t *header;
902 size_t path_len;
903 const char *path;
904 char *namespace;
905
906 /* Fill out header fields. */
907 current = (uintptr_t)dbg2;
908 memset(dbg2, 0, sizeof(acpi_dbg2_header_t));
909 header = &(dbg2->header);
John Zhao2ba303e2019-05-28 16:48:14 -0700910
911 if (!header)
912 return;
913
Marc Jones93a51762018-08-22 18:57:24 -0600914 header->revision = get_acpi_table_revision(DBG2);
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800915 memcpy(header->signature, "DBG2", 4);
916 memcpy(header->oem_id, OEM_ID, 6);
917 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
918 memcpy(header->asl_compiler_id, ASLC, 4);
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100919 header->asl_compiler_revision = asl_revision;
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800920
921 /* One debug device defined */
922 dbg2->devices_offset = sizeof(acpi_dbg2_header_t);
923 dbg2->devices_count = 1;
924 current += sizeof(acpi_dbg2_header_t);
925
926 /* Device comes after the header */
927 device = (acpi_dbg2_device_t *)current;
928 memset(device, 0, sizeof(acpi_dbg2_device_t));
929 current += sizeof(acpi_dbg2_device_t);
930
931 device->revision = 0;
932 device->address_count = 1;
933 device->port_type = port_type;
934 device->port_subtype = port_subtype;
935
936 /* Base Address comes after device structure */
937 memcpy((void *)current, address, sizeof(acpi_addr_t));
938 device->base_address_offset = current - (uintptr_t)device;
939 current += sizeof(acpi_addr_t);
940
941 /* Address Size comes after address structure */
942 dbg2_addr_size = (uint32_t *)current;
943 device->address_size_offset = current - (uintptr_t)device;
944 *dbg2_addr_size = address_size;
945 current += sizeof(uint32_t);
946
947 /* Namespace string comes last, use '.' if not provided */
948 path = device_path ? : ".";
949 /* Namespace string length includes NULL terminator */
950 path_len = strlen(path) + 1;
951 namespace = (char *)current;
952 device->namespace_string_length = path_len;
953 device->namespace_string_offset = current - (uintptr_t)device;
954 strncpy(namespace, path, path_len);
955 current += path_len;
956
957 /* Update structure lengths and checksum */
958 device->length = current - (uintptr_t)device;
959 header->length = current - (uintptr_t)dbg2;
960 header->checksum = acpi_checksum((uint8_t *)dbg2, header->length);
961}
962
963unsigned long acpi_write_dbg2_pci_uart(acpi_rsdp_t *rsdp, unsigned long current,
Aamir Bohrae825d3f2019-07-30 10:11:41 +0530964 const struct device *dev, uint8_t access_size)
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800965{
966 acpi_dbg2_header_t *dbg2 = (acpi_dbg2_header_t *)current;
967 struct resource *res;
968 acpi_addr_t address;
969
970 if (!dev) {
Wim Vervoorn6cd52432020-02-03 14:41:46 +0100971 printk(BIOS_DEBUG, "%s: Device not found\n", __func__);
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800972 return current;
973 }
Mario Scheithauerf1eb0ea2017-11-21 13:52:53 +0100974 if (!dev->enabled) {
975 printk(BIOS_INFO, "%s: Device not enabled\n", __func__);
976 return current;
977 }
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800978 res = find_resource(dev, PCI_BASE_ADDRESS_0);
979 if (!res) {
980 printk(BIOS_ERR, "%s: Unable to find resource for %s\n",
981 __func__, dev_path(dev));
982 return current;
983 }
984
985 memset(&address, 0, sizeof(address));
986 if (res->flags & IORESOURCE_IO)
987 address.space_id = ACPI_ADDRESS_SPACE_IO;
988 else if (res->flags & IORESOURCE_MEM)
989 address.space_id = ACPI_ADDRESS_SPACE_MEMORY;
990 else {
991 printk(BIOS_ERR, "%s: Unknown address space type\n", __func__);
992 return current;
993 }
994
995 address.addrl = (uint32_t)res->base;
996 address.addrh = (uint32_t)((res->base >> 32) & 0xffffffff);
997 address.access_size = access_size;
998
999 acpi_create_dbg2(dbg2,
1000 ACPI_DBG2_PORT_SERIAL,
1001 ACPI_DBG2_PORT_SERIAL_16550,
1002 &address, res->size,
1003 acpi_device_path(dev));
1004
1005 if (dbg2->header.length) {
1006 current += dbg2->header.length;
1007 current = acpi_align_current(current);
1008 acpi_add_table(rsdp, dbg2);
1009 }
1010
1011 return current;
1012}
1013
Stefan Reinauer777224c2005-01-19 14:06:41 +00001014void acpi_create_facs(acpi_facs_t *facs)
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +00001015{
Uwe Hermann622824c2010-11-19 15:14:42 +00001016 memset((void *)facs, 0, sizeof(acpi_facs_t));
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +00001017
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001018 memcpy(facs->signature, "FACS", 4);
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +00001019 facs->length = sizeof(acpi_facs_t);
1020 facs->hardware_signature = 0;
1021 facs->firmware_waking_vector = 0;
1022 facs->global_lock = 0;
1023 facs->flags = 0;
1024 facs->x_firmware_waking_vector_l = 0;
1025 facs->x_firmware_waking_vector_h = 0;
Marc Jones93a51762018-08-22 18:57:24 -06001026 facs->version = get_acpi_table_revision(FACS);
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +00001027}
Stefan Reinauer777224c2005-01-19 14:06:41 +00001028
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001029static void acpi_write_rsdt(acpi_rsdt_t *rsdt, char *oem_id, char *oem_table_id)
Stefan Reinauer14e22772010-04-27 06:56:47 +00001030{
Uwe Hermann622824c2010-11-19 15:14:42 +00001031 acpi_header_t *header = &(rsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001032
John Zhao2ba303e2019-05-28 16:48:14 -07001033 if (!header)
1034 return;
1035
Uwe Hermann622824c2010-11-19 15:14:42 +00001036 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001037 memcpy(header->signature, "RSDT", 4);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001038 memcpy(header->oem_id, oem_id, 6);
1039 memcpy(header->oem_table_id, oem_table_id, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +00001040 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001041
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001042 header->asl_compiler_revision = asl_revision;
Stefan Reinauer688b3852004-01-28 16:56:14 +00001043 header->length = sizeof(acpi_rsdt_t);
Marc Jones93a51762018-08-22 18:57:24 -06001044 header->revision = get_acpi_table_revision(RSDT);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001045
Uwe Hermann622824c2010-11-19 15:14:42 +00001046 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauer688b3852004-01-28 16:56:14 +00001047
Uwe Hermann622824c2010-11-19 15:14:42 +00001048 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001049 header->checksum = acpi_checksum((void *)rsdt, sizeof(acpi_rsdt_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +00001050}
1051
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001052static void acpi_write_xsdt(acpi_xsdt_t *xsdt, char *oem_id, char *oem_table_id)
Stefan Reinauer14e22772010-04-27 06:56:47 +00001053{
Uwe Hermann622824c2010-11-19 15:14:42 +00001054 acpi_header_t *header = &(xsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001055
John Zhao2ba303e2019-05-28 16:48:14 -07001056 if (!header)
1057 return;
1058
Uwe Hermann622824c2010-11-19 15:14:42 +00001059 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001060 memcpy(header->signature, "XSDT", 4);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001061 memcpy(header->oem_id, oem_id, 6);
1062 memcpy(header->oem_table_id, oem_table_id, 8);
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001063 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001064
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001065 header->asl_compiler_revision = asl_revision;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001066 header->length = sizeof(acpi_xsdt_t);
Marc Jones93a51762018-08-22 18:57:24 -06001067 header->revision = get_acpi_table_revision(XSDT);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001068
Uwe Hermann622824c2010-11-19 15:14:42 +00001069 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001070
Uwe Hermann622824c2010-11-19 15:14:42 +00001071 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001072 header->checksum = acpi_checksum((void *)xsdt, sizeof(acpi_xsdt_t));
1073}
1074
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001075static void acpi_write_rsdp(acpi_rsdp_t *rsdp, acpi_rsdt_t *rsdt,
1076 acpi_xsdt_t *xsdt, char *oem_id)
Stefan Reinauer688b3852004-01-28 16:56:14 +00001077{
Stefan Reinauerd18faac2009-11-05 18:06:43 +00001078 memset(rsdp, 0, sizeof(acpi_rsdp_t));
Uwe Hermann622824c2010-11-19 15:14:42 +00001079
Stefan Reinauer688b3852004-01-28 16:56:14 +00001080 memcpy(rsdp->signature, RSDP_SIG, 8);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001081 memcpy(rsdp->oem_id, oem_id, 6);
Uwe Hermann622824c2010-11-19 15:14:42 +00001082
1083 rsdp->length = sizeof(acpi_rsdp_t);
Stefan Reinauerdefee172015-06-18 01:11:19 -07001084 rsdp->rsdt_address = (uintptr_t)rsdt;
Uwe Hermann622824c2010-11-19 15:14:42 +00001085
1086 /*
1087 * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2.
1088 *
1089 * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
1090 * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
1091 * revision 0).
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001092 */
1093 if (xsdt == NULL) {
Uwe Hermann622824c2010-11-19 15:14:42 +00001094 rsdp->revision = 0;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001095 } else {
Stefan Reinauerdefee172015-06-18 01:11:19 -07001096 rsdp->xsdt_address = (u64)(uintptr_t)xsdt;
Marc Jones93a51762018-08-22 18:57:24 -06001097 rsdp->revision = get_acpi_table_revision(RSDP);
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001098 }
Uwe Hermann622824c2010-11-19 15:14:42 +00001099
1100 /* Calculate checksums. */
1101 rsdp->checksum = acpi_checksum((void *)rsdp, 20);
1102 rsdp->ext_checksum = acpi_checksum((void *)rsdp, sizeof(acpi_rsdp_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +00001103}
1104
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001105unsigned long acpi_create_hest_error_source(acpi_hest_t *hest,
1106 acpi_hest_esd_t *esd, u16 type, void *data, u16 data_len)
zbaocaf494c82012-04-13 13:57:14 +08001107{
1108 acpi_header_t *header = &(hest->header);
1109 acpi_hest_hen_t *hen;
1110 void *pos;
1111 u16 len;
1112
1113 pos = esd;
1114 memset(pos, 0, sizeof(acpi_hest_esd_t));
1115 len = 0;
1116 esd->type = type; /* MCE */
1117 esd->source_id = hest->error_source_count;
1118 esd->flags = 0; /* FIRMWARE_FIRST */
1119 esd->enabled = 1;
1120 esd->prealloc_erecords = 1;
1121 esd->max_section_per_record = 0x1;
1122
1123 len += sizeof(acpi_hest_esd_t);
1124 pos = esd + 1;
1125
1126 switch (type) {
1127 case 0: /* MCE */
1128 break;
1129 case 1: /* CMC */
1130 hen = (acpi_hest_hen_t *) (pos);
1131 memset(pos, 0, sizeof(acpi_hest_hen_t));
1132 hen->type = 3; /* SCI? */
1133 hen->length = sizeof(acpi_hest_hen_t);
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001134 hen->conf_we = 0; /* Configuration Write Enable. */
zbaocaf494c82012-04-13 13:57:14 +08001135 hen->poll_interval = 0;
1136 hen->vector = 0;
1137 hen->sw2poll_threshold_val = 0;
1138 hen->sw2poll_threshold_win = 0;
1139 hen->error_threshold_val = 0;
1140 hen->error_threshold_win = 0;
1141 len += sizeof(acpi_hest_hen_t);
1142 pos = hen + 1;
1143 break;
1144 case 2: /* NMI */
1145 case 6: /* AER Root Port */
1146 case 7: /* AER Endpoint */
1147 case 8: /* AER Bridge */
1148 case 9: /* Generic Hardware Error Source. */
1149 /* TODO: */
1150 break;
1151 default:
1152 printk(BIOS_DEBUG, "Invalid type of Error Source.");
1153 break;
1154 }
Lee Leahy024b13d2017-03-16 13:41:11 -07001155 hest->error_source_count++;
zbaocaf494c82012-04-13 13:57:14 +08001156
1157 memcpy(pos, data, data_len);
1158 len += data_len;
John Zhao2ba303e2019-05-28 16:48:14 -07001159 if (header)
1160 header->length += len;
zbaocaf494c82012-04-13 13:57:14 +08001161
1162 return len;
1163}
1164
1165/* ACPI 4.0 */
Vladimir Serbinenko807127f2014-11-09 13:36:18 +01001166void acpi_write_hest(acpi_hest_t *hest,
1167 unsigned long (*acpi_fill_hest)(acpi_hest_t *hest))
zbaocaf494c82012-04-13 13:57:14 +08001168{
1169 acpi_header_t *header = &(hest->header);
1170
1171 memset(hest, 0, sizeof(acpi_hest_t));
1172
John Zhao2ba303e2019-05-28 16:48:14 -07001173 if (!header)
1174 return;
1175
zbaocaf494c82012-04-13 13:57:14 +08001176 memcpy(header->signature, "HEST", 4);
1177 memcpy(header->oem_id, OEM_ID, 6);
1178 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1179 memcpy(header->asl_compiler_id, ASLC, 4);
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001180 header->asl_compiler_revision = asl_revision;
zbaocaf494c82012-04-13 13:57:14 +08001181 header->length += sizeof(acpi_hest_t);
Marc Jones93a51762018-08-22 18:57:24 -06001182 header->revision = get_acpi_table_revision(HEST);
zbaocaf494c82012-04-13 13:57:14 +08001183
1184 acpi_fill_hest(hest);
1185
1186 /* Calculate checksums. */
1187 header->checksum = acpi_checksum((void *)hest, header->length);
1188}
1189
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001190/* ACPI 3.0b */
1191void acpi_write_bert(acpi_bert_t *bert, uintptr_t region, size_t length)
1192{
1193 acpi_header_t *header = &(bert->header);
1194
1195 memset(bert, 0, sizeof(acpi_bert_t));
1196
John Zhao2ba303e2019-05-28 16:48:14 -07001197 if (!header)
1198 return;
1199
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001200 memcpy(header->signature, "BERT", 4);
1201 memcpy(header->oem_id, OEM_ID, 6);
1202 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1203 memcpy(header->asl_compiler_id, ASLC, 4);
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001204 header->asl_compiler_revision = asl_revision;
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001205 header->length += sizeof(acpi_bert_t);
1206 header->revision = get_acpi_table_revision(BERT);
1207
1208 bert->error_region = region;
1209 bert->region_length = length;
1210
1211 /* Calculate checksums. */
1212 header->checksum = acpi_checksum((void *)bert, header->length);
1213}
1214
Julius Wernercd49cce2019-03-05 16:53:33 -08001215#if CONFIG(COMMON_FADT)
Lee Leahy024b13d2017-03-16 13:41:11 -07001216void acpi_create_fadt(acpi_fadt_t *fadt, acpi_facs_t *facs, void *dsdt)
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001217{
1218 acpi_header_t *header = &(fadt->header);
1219
1220 memset((void *) fadt, 0, sizeof(acpi_fadt_t));
John Zhao2ba303e2019-05-28 16:48:14 -07001221
1222 if (!header)
1223 return;
1224
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001225 memcpy(header->signature, "FACP", 4);
1226 header->length = sizeof(acpi_fadt_t);
Marc Jones93a51762018-08-22 18:57:24 -06001227 header->revision = get_acpi_table_revision(FADT);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001228 memcpy(header->oem_id, OEM_ID, 6);
1229 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1230 memcpy(header->asl_compiler_id, ASLC, 4);
Elyes HAOUAS26071aa2019-02-15 08:21:33 +01001231 header->asl_compiler_revision = asl_revision;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001232
1233 fadt->firmware_ctrl = (unsigned long) facs;
1234 fadt->dsdt = (unsigned long) dsdt;
1235
1236 fadt->x_firmware_ctl_l = (unsigned long)facs;
1237 fadt->x_firmware_ctl_h = 0;
1238 fadt->x_dsdt_l = (unsigned long)dsdt;
1239 fadt->x_dsdt_h = 0;
1240
Julius Wernercd49cce2019-03-05 16:53:33 -08001241 if (CONFIG(SYSTEM_TYPE_CONVERTIBLE) ||
1242 CONFIG(SYSTEM_TYPE_LAPTOP))
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001243 fadt->preferred_pm_profile = PM_MOBILE;
Julius Wernercd49cce2019-03-05 16:53:33 -08001244 else if (CONFIG(SYSTEM_TYPE_DETACHABLE) ||
1245 CONFIG(SYSTEM_TYPE_TABLET))
Duncan Lauriefa9c6f12019-02-07 11:30:06 -08001246 fadt->preferred_pm_profile = PM_TABLET;
Lee Leahy9c7c6f72017-03-16 11:24:09 -07001247 else
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001248 fadt->preferred_pm_profile = PM_DESKTOP;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001249
Vladimir Serbinenkoab83ef02014-10-25 15:18:25 +02001250 acpi_fill_fadt(fadt);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001251
1252 header->checksum =
1253 acpi_checksum((void *) fadt, header->length);
1254}
1255#endif
1256
Aaron Durbin64031672018-04-21 14:45:32 -06001257unsigned long __weak fw_cfg_acpi_tables(unsigned long start)
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001258{
1259 return 0;
1260}
1261
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001262unsigned long write_acpi_tables(unsigned long start)
1263{
1264 unsigned long current;
1265 acpi_rsdp_t *rsdp;
1266 acpi_rsdt_t *rsdt;
1267 acpi_xsdt_t *xsdt;
1268 acpi_fadt_t *fadt;
1269 acpi_facs_t *facs;
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001270 acpi_header_t *slic_file, *slic;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001271 acpi_header_t *ssdt;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001272 acpi_header_t *dsdt_file, *dsdt;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001273 acpi_mcfg_t *mcfg;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +02001274 acpi_tcpa_t *tcpa;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001275 acpi_tpm2_t *tpm2;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001276 acpi_madt_t *madt;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +11001277 struct device *dev;
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001278 unsigned long fw;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001279 size_t slic_size, dsdt_size;
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001280 char oem_id[6], oem_table_id[8];
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001281
1282 current = start;
1283
1284 /* Align ACPI tables to 16byte */
Aaron Durbin07a1b282015-12-10 17:07:38 -06001285 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001286
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001287 /* Special case for qemu */
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001288 fw = fw_cfg_acpi_tables(current);
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001289 if (fw) {
1290 rsdp = NULL;
1291 /* Find RSDP. */
1292 for (void *p = (void *)current; p < (void *)fw; p += 16) {
1293 if (valid_rsdp((acpi_rsdp_t *)p)) {
1294 rsdp = p;
1295 break;
1296 }
1297 }
1298 if (!rsdp)
1299 return fw;
1300
1301 /* Add BOOT0000 for Linux google firmware driver */
1302 printk(BIOS_DEBUG, "ACPI: * SSDT\n");
1303 ssdt = (acpi_header_t *)fw;
1304 current = (unsigned long)ssdt + sizeof(acpi_header_t);
1305
1306 memset((void *)ssdt, 0, sizeof(acpi_header_t));
1307
1308 memcpy(&ssdt->signature, "SSDT", 4);
1309 ssdt->revision = get_acpi_table_revision(SSDT);
1310 memcpy(&ssdt->oem_id, OEM_ID, 6);
1311 memcpy(&ssdt->oem_table_id, oem_table_id, 8);
1312 ssdt->oem_revision = 42;
1313 memcpy(&ssdt->asl_compiler_id, ASLC, 4);
1314 ssdt->asl_compiler_revision = asl_revision;
1315 ssdt->length = sizeof(acpi_header_t);
1316
1317 acpigen_set_current((char *) current);
1318
1319 /* Write object to declare coreboot tables */
1320 acpi_ssdt_write_cbtable();
1321
1322 /* (Re)calculate length and checksum. */
1323 ssdt->length = current - (unsigned long)ssdt;
1324 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
1325
1326 acpi_create_ssdt_generator(ssdt, ACPI_TABLE_CREATOR);
1327
1328 acpi_add_table(rsdp, ssdt);
1329
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001330 return fw;
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001331 }
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001332
Vladimir Serbinenkoa4cf83d2015-06-02 21:40:29 +02001333 dsdt_file = cbfs_boot_map_with_leak(
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001334 CONFIG_CBFS_PREFIX "/dsdt.aml",
1335 CBFS_TYPE_RAW, &dsdt_size);
1336 if (!dsdt_file) {
1337 printk(BIOS_ERR, "No DSDT file, skipping ACPI tables\n");
1338 return current;
1339 }
1340
1341 if (dsdt_file->length > dsdt_size
Elyes HAOUAS7d87e762016-10-03 22:11:07 +02001342 || dsdt_file->length < sizeof(acpi_header_t)
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001343 || memcmp(dsdt_file->signature, "DSDT", 4) != 0) {
1344 printk(BIOS_ERR, "Invalid DSDT file, skipping ACPI tables\n");
1345 return current;
1346 }
1347
Aaron Durbin899d13d2015-05-15 23:39:23 -05001348 slic_file = cbfs_boot_map_with_leak(CONFIG_CBFS_PREFIX "/slic",
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001349 CBFS_TYPE_RAW, &slic_size);
Vladimir Serbinenko2cb29782015-05-31 11:32:09 +02001350 if (slic_file
1351 && (slic_file->length > slic_size
Elyes HAOUAS7d87e762016-10-03 22:11:07 +02001352 || slic_file->length < sizeof(acpi_header_t)
Vladimir Serbinenko2cb29782015-05-31 11:32:09 +02001353 || memcmp(slic_file->signature, "SLIC", 4) != 0)) {
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001354 slic_file = 0;
1355 }
1356
1357 if (slic_file) {
1358 memcpy(oem_id, slic_file->oem_id, 6);
1359 memcpy(oem_table_id, slic_file->oem_table_id, 8);
1360 } else {
1361 memcpy(oem_id, OEM_ID, 6);
1362 memcpy(oem_table_id, ACPI_TABLE_CREATOR, 8);
1363 }
1364
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001365 printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx.\n", start);
1366
1367 /* We need at least an RSDP and an RSDT Table */
1368 rsdp = (acpi_rsdp_t *) current;
1369 current += sizeof(acpi_rsdp_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001370 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001371 rsdt = (acpi_rsdt_t *) current;
1372 current += sizeof(acpi_rsdt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001373 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001374 xsdt = (acpi_xsdt_t *) current;
1375 current += sizeof(acpi_xsdt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001376 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001377
1378 /* clear all table memory */
1379 memset((void *) start, 0, current - start);
1380
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001381 acpi_write_rsdp(rsdp, rsdt, xsdt, oem_id);
1382 acpi_write_rsdt(rsdt, oem_id, oem_table_id);
1383 acpi_write_xsdt(xsdt, oem_id, oem_table_id);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001384
1385 printk(BIOS_DEBUG, "ACPI: * FACS\n");
Felix Heldc4697122019-06-20 13:50:17 +02001386 current = ALIGN_UP(current, 64);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001387 facs = (acpi_facs_t *) current;
1388 current += sizeof(acpi_facs_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001389 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001390 acpi_create_facs(facs);
1391
1392 printk(BIOS_DEBUG, "ACPI: * DSDT\n");
1393 dsdt = (acpi_header_t *) current;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001394 memcpy(dsdt, dsdt_file, sizeof(acpi_header_t));
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001395 if (dsdt->length >= sizeof(acpi_header_t)) {
1396 current += sizeof(acpi_header_t);
1397
1398 acpigen_set_current((char *) current);
1399 for (dev = all_devices; dev; dev = dev->next)
Nico Huber68680dd2020-03-31 17:34:52 +02001400 if (dev->ops && dev->ops->acpi_inject_dsdt)
1401 dev->ops->acpi_inject_dsdt(dev);
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001402 current = (unsigned long) acpigen_get_current();
1403 memcpy((char *)current,
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001404 (char *)dsdt_file + sizeof(acpi_header_t),
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001405 dsdt->length - sizeof(acpi_header_t));
1406 current += dsdt->length - sizeof(acpi_header_t);
1407
1408 /* (Re)calculate length and checksum. */
1409 dsdt->length = current - (unsigned long)dsdt;
1410 dsdt->checksum = 0;
1411 dsdt->checksum = acpi_checksum((void *)dsdt, dsdt->length);
1412 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001413
Aaron Durbin07a1b282015-12-10 17:07:38 -06001414 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001415
1416 printk(BIOS_DEBUG, "ACPI: * FADT\n");
1417 fadt = (acpi_fadt_t *) current;
1418 current += sizeof(acpi_fadt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001419 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001420
1421 acpi_create_fadt(fadt, facs, dsdt);
1422 acpi_add_table(rsdp, fadt);
1423
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001424 if (slic_file) {
1425 printk(BIOS_DEBUG, "ACPI: * SLIC\n");
1426 slic = (acpi_header_t *)current;
1427 memcpy(slic, slic_file, slic_file->length);
1428 current += slic_file->length;
Aaron Durbin07a1b282015-12-10 17:07:38 -06001429 current = acpi_align_current(current);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001430 acpi_add_table(rsdp, slic);
1431 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001432
1433 printk(BIOS_DEBUG, "ACPI: * SSDT\n");
1434 ssdt = (acpi_header_t *)current;
1435 acpi_create_ssdt_generator(ssdt, ACPI_TABLE_CREATOR);
Vladimir Serbinenko9310df82014-10-10 20:40:41 +02001436 if (ssdt->length > sizeof(acpi_header_t)) {
1437 current += ssdt->length;
1438 acpi_add_table(rsdp, ssdt);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001439 current = acpi_align_current(current);
Vladimir Serbinenko9310df82014-10-10 20:40:41 +02001440 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001441
1442 printk(BIOS_DEBUG, "ACPI: * MCFG\n");
1443 mcfg = (acpi_mcfg_t *) current;
1444 acpi_create_mcfg(mcfg);
1445 if (mcfg->header.length > sizeof(acpi_mcfg_t)) {
1446 current += mcfg->header.length;
Aaron Durbin07a1b282015-12-10 17:07:38 -06001447 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001448 acpi_add_table(rsdp, mcfg);
1449 }
1450
Julius Wernercd49cce2019-03-05 16:53:33 -08001451 if (CONFIG(TPM1)) {
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001452 printk(BIOS_DEBUG, "ACPI: * TCPA\n");
1453 tcpa = (acpi_tcpa_t *) current;
1454 acpi_create_tcpa(tcpa);
1455 if (tcpa->header.length >= sizeof(acpi_tcpa_t)) {
1456 current += tcpa->header.length;
1457 current = acpi_align_current(current);
1458 acpi_add_table(rsdp, tcpa);
1459 }
1460 }
1461
Julius Wernercd49cce2019-03-05 16:53:33 -08001462 if (CONFIG(TPM2)) {
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001463 printk(BIOS_DEBUG, "ACPI: * TPM2\n");
1464 tpm2 = (acpi_tpm2_t *) current;
1465 acpi_create_tpm2(tpm2);
1466 if (tpm2->header.length >= sizeof(acpi_tpm2_t)) {
1467 current += tpm2->header.length;
1468 current = acpi_align_current(current);
1469 acpi_add_table(rsdp, tpm2);
1470 }
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +02001471 }
1472
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001473 printk(BIOS_DEBUG, "ACPI: * MADT\n");
1474
1475 madt = (acpi_madt_t *) current;
1476 acpi_create_madt(madt);
1477 if (madt->header.length > sizeof(acpi_madt_t)) {
Lee Leahy024b13d2017-03-16 13:41:11 -07001478 current += madt->header.length;
1479 acpi_add_table(rsdp, madt);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001480 }
Aaron Durbin07a1b282015-12-10 17:07:38 -06001481 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001482
1483 printk(BIOS_DEBUG, "current = %lx\n", current);
1484
1485 for (dev = all_devices; dev; dev = dev->next) {
1486 if (dev->ops && dev->ops->write_acpi_tables) {
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001487 current = dev->ops->write_acpi_tables(dev, current,
1488 rsdp);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001489 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001490 }
1491 }
1492
1493 printk(BIOS_INFO, "ACPI: done.\n");
1494 return current;
1495}
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001496
Rudolf Marek33cafe52009-04-13 18:07:02 +00001497static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp)
1498{
1499 if (strncmp((char *)rsdp, RSDP_SIG, sizeof(RSDP_SIG) - 1) != 0)
1500 return NULL;
1501
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001502 printk(BIOS_DEBUG, "Looking on %p for valid checksum\n", rsdp);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001503
1504 if (acpi_checksum((void *)rsdp, 20) != 0)
1505 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001506 printk(BIOS_DEBUG, "Checksum 1 passed\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001507
1508 if ((rsdp->revision > 1) && (acpi_checksum((void *)rsdp,
1509 rsdp->length) != 0))
1510 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001511 printk(BIOS_DEBUG, "Checksum 2 passed all OK\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001512
1513 return rsdp;
1514}
1515
Rudolf Marek33cafe52009-04-13 18:07:02 +00001516void *acpi_find_wakeup_vector(void)
1517{
1518 char *p, *end;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001519 acpi_rsdt_t *rsdt;
1520 acpi_facs_t *facs;
Martin Rothc5f49262012-12-14 19:17:55 -07001521 acpi_fadt_t *fadt = NULL;
Kyösti Mälkki072d436b2016-06-17 08:44:40 +03001522 acpi_rsdp_t *rsdp = NULL;
Uwe Hermann622824c2010-11-19 15:14:42 +00001523 void *wake_vec;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001524 int i;
1525
Rudolf Marek33cafe52009-04-13 18:07:02 +00001526 if (!acpi_is_wakeup())
1527 return NULL;
1528
Uwe Hermann622824c2010-11-19 15:14:42 +00001529 printk(BIOS_DEBUG, "Trying to find the wakeup vector...\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001530
Uwe Hermann622824c2010-11-19 15:14:42 +00001531 /* Find RSDP. */
1532 for (p = (char *)0xe0000; p < (char *)0xfffff; p += 16) {
Lee Leahy0b5678f2017-03-16 16:01:40 -07001533 rsdp = valid_rsdp((acpi_rsdp_t *)p);
1534 if (rsdp)
Rudolf Marek33cafe52009-04-13 18:07:02 +00001535 break;
1536 }
1537
Angel Pons98a68ad2018-11-04 12:25:25 +01001538 if (rsdp == NULL) {
1539 printk(BIOS_ALERT,
1540 "No RSDP found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001541 return NULL;
Angel Pons98a68ad2018-11-04 12:25:25 +01001542 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001543
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001544 printk(BIOS_DEBUG, "RSDP found at %p\n", rsdp);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001545 rsdt = (acpi_rsdt_t *)(uintptr_t)rsdp->rsdt_address;
Stefan Reinauer14e22772010-04-27 06:56:47 +00001546
Uwe Hermann622824c2010-11-19 15:14:42 +00001547 end = (char *)rsdt + rsdt->header.length;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001548 printk(BIOS_DEBUG, "RSDT found at %p ends at %p\n", rsdt, end);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001549
Uwe Hermann622824c2010-11-19 15:14:42 +00001550 for (i = 0; ((char *)&rsdt->entry[i]) < end; i++) {
Stefan Reinauer71a30182015-07-30 16:28:13 -07001551 fadt = (acpi_fadt_t *)(uintptr_t)rsdt->entry[i];
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001552 if (strncmp((char *)fadt, "FACP", 4) == 0)
Rudolf Marek33cafe52009-04-13 18:07:02 +00001553 break;
1554 fadt = NULL;
1555 }
1556
Angel Pons98a68ad2018-11-04 12:25:25 +01001557 if (fadt == NULL) {
1558 printk(BIOS_ALERT,
1559 "No FADT found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001560 return NULL;
Angel Pons98a68ad2018-11-04 12:25:25 +01001561 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001562
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001563 printk(BIOS_DEBUG, "FADT found at %p\n", fadt);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001564 facs = (acpi_facs_t *)(uintptr_t)fadt->firmware_ctrl;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001565
Stefan Reinauer7e9771c2009-04-22 08:17:38 +00001566 if (facs == NULL) {
Angel Pons98a68ad2018-11-04 12:25:25 +01001567 printk(BIOS_ALERT,
1568 "No FACS found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001569 return NULL;
Stefan Reinauer7e9771c2009-04-22 08:17:38 +00001570 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001571
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001572 printk(BIOS_DEBUG, "FACS found at %p\n", facs);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001573 wake_vec = (void *)(uintptr_t)facs->firmware_waking_vector;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001574 printk(BIOS_DEBUG, "OS waking vector is %p\n", wake_vec);
Uwe Hermann622824c2010-11-19 15:14:42 +00001575
Rudolf Marek33cafe52009-04-13 18:07:02 +00001576 return wake_vec;
1577}
1578
Aaron Durbin64031672018-04-21 14:45:32 -06001579__weak int acpi_get_gpe(int gpe)
Duncan Laurie1f6e6812016-09-19 12:04:19 -07001580{
1581 return -1; /* implemented by SOC */
1582}
Marc Jones93a51762018-08-22 18:57:24 -06001583
1584int get_acpi_table_revision(enum acpi_tables table)
1585{
1586 switch (table) {
1587 case FADT:
Patrick Rudolphc02bda02020-02-28 10:19:41 +01001588 return ACPI_FADT_REV_ACPI_6_0;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001589 case MADT: /* ACPI 3.0: 2, ACPI 4.0/5.0: 3, ACPI 6.2b/6.3: 5 */
Patrick Rudolph56a3ef22020-03-24 17:29:49 +01001590 return 3;
Marc Jones93a51762018-08-22 18:57:24 -06001591 case MCFG:
1592 return 1;
1593 case TCPA:
1594 return 2;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001595 case TPM2:
1596 return 4;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001597 case SSDT: /* ACPI 3.0 upto 6.3: 2 */
Marc Jones93a51762018-08-22 18:57:24 -06001598 return 2;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001599 case SRAT: /* ACPI 2.0: 1, ACPI 3.0: 2, ACPI 4.0 upto 6.3: 3 */
Marc Jones93a51762018-08-22 18:57:24 -06001600 return 1; /* TODO Should probably be upgraded to 2 */
1601 case DMAR:
1602 return 1;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001603 case SLIT: /* ACPI 2.0 upto 6.3: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06001604 return 1;
Patrick Rudolph9d98e5a2019-06-14 19:00:04 +02001605 case SPMI: /* IMPI 2.0 */
1606 return 5;
Marc Jones93a51762018-08-22 18:57:24 -06001607 case HPET: /* Currently 1. Table added in ACPI 2.0. */
1608 return 1;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001609 case VFCT: /* ACPI 2.0/3.0/4.0: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06001610 return 1;
1611 case IVRS:
1612 return IVRS_FORMAT_FIXED;
1613 case DBG2:
1614 return 0;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001615 case FACS: /* ACPI 2.0/3.0: 1, ACPI 4.0 upto 6.3: 2 */
Marc Jones93a51762018-08-22 18:57:24 -06001616 return 1;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001617 case RSDT: /* ACPI 1.0 upto 6.3: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06001618 return 1;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001619 case XSDT: /* ACPI 2.0 upto 6.3: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06001620 return 1;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001621 case RSDP: /* ACPI 2.0 upto 6.3: 2 */
Marc Jones93a51762018-08-22 18:57:24 -06001622 return 2;
1623 case HEST:
1624 return 1;
1625 case NHLT:
1626 return 5;
Marshall Dawson44705c62018-09-04 13:47:15 -06001627 case BERT:
1628 return 1;
Marc Jones93a51762018-08-22 18:57:24 -06001629 default:
1630 return -1;
1631 }
1632 return -1;
1633}