blob: 6f64d108f9e8133bbf5a9225c849a3a7ad67a4f3 [file] [log] [blame]
Patrick Georgi11f00792020-03-04 15:10:45 +01001/* SPDX-License-Identifier: GPL-2.0-only */
Stefan Reinauer688b3852004-01-28 16:56:14 +00002/*
Martin Roth20bbd812019-08-30 21:09:37 -06003 * coreboot ACPI Table support
Stefan Reinauer777224c2005-01-19 14:06:41 +00004 */
5
Stefan Reinauer14e22772010-04-27 06:56:47 +00006/*
Stefan Reinauer777224c2005-01-19 14:06:41 +00007 * Each system port implementing ACPI has to provide two functions:
Stefan Reinauer14e22772010-04-27 06:56:47 +00008 *
Stefan Reinauer777224c2005-01-19 14:06:41 +00009 * write_acpi_tables()
10 * acpi_dump_apics()
Stefan Reinauer14e22772010-04-27 06:56:47 +000011 *
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000012 * See Kontron 986LCD-M port for a good example of an ACPI implementation
13 * in coreboot.
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000014 */
Stefan Reinauer688b3852004-01-28 16:56:14 +000015
16#include <console/console.h>
17#include <string.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -070018#include <acpi/acpi.h>
19#include <acpi/acpi_ivrs.h>
20#include <acpi/acpigen.h>
Stefan Reinauer777224c2005-01-19 14:06:41 +000021#include <device/pci.h>
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000022#include <cbmem.h>
Elyes HAOUASe2d152c2019-06-21 07:06:50 +020023#include <commonlib/helpers.h>
Duncan Laurie11290c42012-10-03 19:07:05 -070024#include <cpu/cpu.h>
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +010025#include <cbfs.h>
Elyes HAOUAS26071aa2019-02-15 08:21:33 +010026#include <version.h>
Werner Zehdb561e62019-02-19 13:39:56 +010027#include <commonlib/sort.h>
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000028
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +020029static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp);
30
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000031u8 acpi_checksum(u8 *table, u32 length)
Stefan Reinauer688b3852004-01-28 16:56:14 +000032{
Uwe Hermann622824c2010-11-19 15:14:42 +000033 u8 ret = 0;
Stefan Reinauer688b3852004-01-28 16:56:14 +000034 while (length--) {
35 ret += *table;
36 table++;
37 }
Stefan Reinauera7648c22004-01-29 17:31:34 +000038 return -ret;
Stefan Reinauer688b3852004-01-28 16:56:14 +000039}
40
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000041/**
Uwe Hermann622824c2010-11-19 15:14:42 +000042 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
43 * and checksum.
Stefan Reinauer06feb882004-02-03 16:11:35 +000044 */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000045void acpi_add_table(acpi_rsdp_t *rsdp, void *table)
Stefan Reinauer688b3852004-01-28 16:56:14 +000046{
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000047 int i, entries_num;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000048 acpi_rsdt_t *rsdt;
49 acpi_xsdt_t *xsdt = NULL;
50
Uwe Hermann622824c2010-11-19 15:14:42 +000051 /* The RSDT is mandatory... */
Stefan Reinauerdefee172015-06-18 01:11:19 -070052 rsdt = (acpi_rsdt_t *)(uintptr_t)rsdp->rsdt_address;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000053
Uwe Hermann622824c2010-11-19 15:14:42 +000054 /* ...while the XSDT is not. */
55 if (rsdp->xsdt_address)
Stefan Reinauerdefee172015-06-18 01:11:19 -070056 xsdt = (acpi_xsdt_t *)((uintptr_t)rsdp->xsdt_address);
Stefan Reinauer14e22772010-04-27 06:56:47 +000057
Uwe Hermann622824c2010-11-19 15:14:42 +000058 /* This should always be MAX_ACPI_TABLES. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000059 entries_num = ARRAY_SIZE(rsdt->entry);
Stefan Reinauer14e22772010-04-27 06:56:47 +000060
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000061 for (i = 0; i < entries_num; i++) {
Uwe Hermann622824c2010-11-19 15:14:42 +000062 if (rsdt->entry[i] == 0)
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000063 break;
Stefan Reinauer688b3852004-01-28 16:56:14 +000064 }
65
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000066 if (i >= entries_num) {
Uwe Hermann622824c2010-11-19 15:14:42 +000067 printk(BIOS_ERR, "ACPI: Error: Could not add ACPI table, "
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +030068 "too many tables.\n");
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000069 return;
70 }
71
Uwe Hermann622824c2010-11-19 15:14:42 +000072 /* Add table to the RSDT. */
Stefan Reinauerdefee172015-06-18 01:11:19 -070073 rsdt->entry[i] = (uintptr_t)table;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000074
Uwe Hermann622824c2010-11-19 15:14:42 +000075 /* Fix RSDT length or the kernel will assume invalid entries. */
76 rsdt->header.length = sizeof(acpi_header_t) + (sizeof(u32) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000077
Uwe Hermann622824c2010-11-19 15:14:42 +000078 /* Re-calculate checksum. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000079 rsdt->header.checksum = 0; /* Hope this won't get optimized away */
Uwe Hermann622824c2010-11-19 15:14:42 +000080 rsdt->header.checksum = acpi_checksum((u8 *)rsdt, rsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000081
Uwe Hermann622824c2010-11-19 15:14:42 +000082 /*
83 * And now the same thing for the XSDT. We use the same index as for
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000084 * now we want the XSDT and RSDT to always be in sync in coreboot.
85 */
86 if (xsdt) {
Uwe Hermann622824c2010-11-19 15:14:42 +000087 /* Add table to the XSDT. */
Stefan Reinauerdefee172015-06-18 01:11:19 -070088 xsdt->entry[i] = (u64)(uintptr_t)table;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000089
Uwe Hermann622824c2010-11-19 15:14:42 +000090 /* Fix XSDT length. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000091 xsdt->header.length = sizeof(acpi_header_t) +
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +030092 (sizeof(u64) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000093
Uwe Hermann622824c2010-11-19 15:14:42 +000094 /* Re-calculate checksum. */
95 xsdt->header.checksum = 0;
96 xsdt->header.checksum = acpi_checksum((u8 *)xsdt,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +030097 xsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000098 }
99
Uwe Hermann622824c2010-11-19 15:14:42 +0000100 printk(BIOS_DEBUG, "ACPI: added table %d/%d, length now %d\n",
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300101 i + 1, entries_num, rsdt->header.length);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000102}
103
Uwe Hermann622824c2010-11-19 15:14:42 +0000104int acpi_create_mcfg_mmconfig(acpi_mcfg_mmconfig_t *mmconfig, u32 base,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300105 u16 seg_nr, u8 start, u8 end)
Stefan Reinauer26d431a2009-01-20 19:17:51 +0000106{
Vladimir Serbinenko60fccdc2014-10-05 11:05:16 +0200107 memset(mmconfig, 0, sizeof(*mmconfig));
Rudolf Mareke6409f22007-11-03 12:50:26 +0000108 mmconfig->base_address = base;
109 mmconfig->base_reserved = 0;
110 mmconfig->pci_segment_group_number = seg_nr;
111 mmconfig->start_bus_number = start;
112 mmconfig->end_bus_number = end;
Uwe Hermann622824c2010-11-19 15:14:42 +0000113
114 return sizeof(acpi_mcfg_mmconfig_t);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000115}
116
Stefan Reinauer777224c2005-01-19 14:06:41 +0000117int acpi_create_madt_lapic(acpi_madt_lapic_t *lapic, u8 cpu, u8 apic)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000118{
Himanshu Sahdev8c09b822019-10-21 18:50:50 +0530119 lapic->type = LOCAL_APIC; /* Local APIC structure */
Uwe Hermann622824c2010-11-19 15:14:42 +0000120 lapic->length = sizeof(acpi_madt_lapic_t);
121 lapic->flags = (1 << 0); /* Processor/LAPIC enabled */
122 lapic->processor_id = cpu;
123 lapic->apic_id = apic;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000124
Uwe Hermann622824c2010-11-19 15:14:42 +0000125 return lapic->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000126}
127
Patrick Rudolph56a3ef22020-03-24 17:29:49 +0100128int acpi_create_madt_lx2apic(acpi_madt_lx2apic_t *lapic, u32 cpu, u32 apic)
129{
130 lapic->type = LOCAL_X2APIC; /* Local APIC structure */
131 lapic->reserved = 0;
132 lapic->length = sizeof(acpi_madt_lx2apic_t);
133 lapic->flags = (1 << 0); /* Processor/LAPIC enabled */
134 lapic->processor_id = cpu;
135 lapic->x2apic_id = apic;
136
137 return lapic->length;
138}
139
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000140unsigned long acpi_create_madt_lapics(unsigned long current)
141{
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100142 struct device *cpu;
Werner Zeh423adfb2019-03-06 09:31:02 +0100143 int index, apic_ids[CONFIG_MAX_CPUS] = {0}, num_cpus = 0;
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000144
Uwe Hermann622824c2010-11-19 15:14:42 +0000145 for (cpu = all_devices; cpu; cpu = cpu->next) {
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000146 if ((cpu->path.type != DEVICE_PATH_APIC) ||
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800147 (cpu->bus->dev->path.type != DEVICE_PATH_CPU_CLUSTER)) {
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000148 continue;
149 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000150 if (!cpu->enabled)
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000151 continue;
Werner Zehdb561e62019-02-19 13:39:56 +0100152 if (num_cpus >= ARRAY_SIZE(apic_ids))
153 break;
154 apic_ids[num_cpus++] = cpu->path.apic.apic_id;
155 }
Werner Zehfedb36e2019-03-12 07:07:50 +0100156 if (num_cpus > 1)
157 bubblesort(apic_ids, num_cpus, NUM_ASCENDING);
Werner Zehdb561e62019-02-19 13:39:56 +0100158 for (index = 0; index < num_cpus; index++) {
Patrick Rudolph56a3ef22020-03-24 17:29:49 +0100159 if (apic_ids[index] < 0xff)
160 current += acpi_create_madt_lapic((acpi_madt_lapic_t *)current,
161 index, apic_ids[index]);
162 else
163 current += acpi_create_madt_lx2apic((acpi_madt_lx2apic_t *)current,
164 index, apic_ids[index]);
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000165 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000166
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000167 return current;
168}
169
Uwe Hermann622824c2010-11-19 15:14:42 +0000170int acpi_create_madt_ioapic(acpi_madt_ioapic_t *ioapic, u8 id, u32 addr,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300171 u32 gsi_base)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000172{
Himanshu Sahdev8c09b822019-10-21 18:50:50 +0530173 ioapic->type = IO_APIC; /* I/O APIC structure */
Uwe Hermann622824c2010-11-19 15:14:42 +0000174 ioapic->length = sizeof(acpi_madt_ioapic_t);
175 ioapic->reserved = 0x00;
176 ioapic->gsi_base = gsi_base;
177 ioapic->ioapic_id = id;
178 ioapic->ioapic_addr = addr;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000179
Uwe Hermann622824c2010-11-19 15:14:42 +0000180 return ioapic->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000181}
182
Stefan Reinauer777224c2005-01-19 14:06:41 +0000183int acpi_create_madt_irqoverride(acpi_madt_irqoverride_t *irqoverride,
Stefan Reinauer06feb882004-02-03 16:11:35 +0000184 u8 bus, u8 source, u32 gsirq, u16 flags)
185{
Himanshu Sahdev8c09b822019-10-21 18:50:50 +0530186 irqoverride->type = IRQ_SOURCE_OVERRIDE; /* Interrupt source override */
Uwe Hermann622824c2010-11-19 15:14:42 +0000187 irqoverride->length = sizeof(acpi_madt_irqoverride_t);
188 irqoverride->bus = bus;
189 irqoverride->source = source;
190 irqoverride->gsirq = gsirq;
191 irqoverride->flags = flags;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000192
Uwe Hermann622824c2010-11-19 15:14:42 +0000193 return irqoverride->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000194}
195
Stefan Reinauer777224c2005-01-19 14:06:41 +0000196int acpi_create_madt_lapic_nmi(acpi_madt_lapic_nmi_t *lapic_nmi, u8 cpu,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300197 u16 flags, u8 lint)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000198{
Himanshu Sahdev8c09b822019-10-21 18:50:50 +0530199 lapic_nmi->type = LOCAL_APIC_NMI; /* Local APIC NMI structure */
Uwe Hermann622824c2010-11-19 15:14:42 +0000200 lapic_nmi->length = sizeof(acpi_madt_lapic_nmi_t);
201 lapic_nmi->flags = flags;
202 lapic_nmi->processor_id = cpu;
203 lapic_nmi->lint = lint;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000204
Uwe Hermann622824c2010-11-19 15:14:42 +0000205 return lapic_nmi->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000206}
207
Patrick Rudolph56a3ef22020-03-24 17:29:49 +0100208int acpi_create_madt_lx2apic_nmi(acpi_madt_lx2apic_nmi_t *lapic_nmi, u32 cpu,
209 u16 flags, u8 lint)
210{
211 lapic_nmi->type = LOCAL_X2APIC_NMI; /* Local APIC NMI structure */
212 lapic_nmi->length = sizeof(acpi_madt_lx2apic_nmi_t);
213 lapic_nmi->flags = flags;
214 lapic_nmi->processor_id = cpu;
215 lapic_nmi->lint = lint;
216 lapic_nmi->reserved[0] = 0;
217 lapic_nmi->reserved[1] = 0;
218 lapic_nmi->reserved[2] = 0;
219
220 return lapic_nmi->length;
221}
222
Furquan Shaikhb1859a62020-04-30 21:27:47 -0700223__weak uintptr_t cpu_get_lapic_addr(void)
224{
225 /*
226 * If an architecture does not support LAPIC, this weak implementation returns LAPIC
227 * addr as 0.
228 */
229 return 0;
230}
231
Stefan Reinauer777224c2005-01-19 14:06:41 +0000232void acpi_create_madt(acpi_madt_t *madt)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000233{
Uwe Hermann622824c2010-11-19 15:14:42 +0000234 acpi_header_t *header = &(madt->header);
235 unsigned long current = (unsigned long)madt + sizeof(acpi_madt_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000236
Stefan Reinauer06feb882004-02-03 16:11:35 +0000237 memset((void *)madt, 0, sizeof(acpi_madt_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000238
John Zhao2ba303e2019-05-28 16:48:14 -0700239 if (!header)
240 return;
241
Uwe Hermann622824c2010-11-19 15:14:42 +0000242 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000243 memcpy(header->signature, "APIC", 4);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000244 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000245 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000246 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000247
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100248 header->asl_compiler_revision = asl_revision;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000249 header->length = sizeof(acpi_madt_t);
Marc Jones93a51762018-08-22 18:57:24 -0600250 header->revision = get_acpi_table_revision(MADT);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000251
Furquan Shaikhb1859a62020-04-30 21:27:47 -0700252 madt->lapic_addr = cpu_get_lapic_addr();
Nico Huber9df72e02018-11-24 18:25:50 +0100253 if (CONFIG(ACPI_HAVE_PCAT_8259))
254 madt->flags |= 1;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000255
Stefan Reinauerf622d592005-11-26 16:56:05 +0000256 current = acpi_fill_madt(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000257
Uwe Hermann622824c2010-11-19 15:14:42 +0000258 /* (Re)calculate length and checksum. */
259 header->length = current - (unsigned long)madt;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000260
Uwe Hermann622824c2010-11-19 15:14:42 +0000261 header->checksum = acpi_checksum((void *)madt, header->length);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000262}
263
Uwe Hermann622824c2010-11-19 15:14:42 +0000264/* MCFG is defined in the PCI Firmware Specification 3.0. */
Rudolf Mareke6409f22007-11-03 12:50:26 +0000265void acpi_create_mcfg(acpi_mcfg_t *mcfg)
266{
Uwe Hermann622824c2010-11-19 15:14:42 +0000267 acpi_header_t *header = &(mcfg->header);
268 unsigned long current = (unsigned long)mcfg + sizeof(acpi_mcfg_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000269
Rudolf Mareke6409f22007-11-03 12:50:26 +0000270 memset((void *)mcfg, 0, sizeof(acpi_mcfg_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000271
John Zhao2ba303e2019-05-28 16:48:14 -0700272 if (!header)
273 return;
274
Uwe Hermann622824c2010-11-19 15:14:42 +0000275 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000276 memcpy(header->signature, "MCFG", 4);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000277 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000278 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000279 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000280
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100281 header->asl_compiler_revision = asl_revision;
Rudolf Mareke6409f22007-11-03 12:50:26 +0000282 header->length = sizeof(acpi_mcfg_t);
Marc Jones93a51762018-08-22 18:57:24 -0600283 header->revision = get_acpi_table_revision(MCFG);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000284
285 current = acpi_fill_mcfg(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000286
Uwe Hermann622824c2010-11-19 15:14:42 +0000287 /* (Re)calculate length and checksum. */
288 header->length = current - (unsigned long)mcfg;
289 header->checksum = acpi_checksum((void *)mcfg, header->length);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000290}
291
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200292static void *get_tcpa_log(u32 *size)
293{
294 const struct cbmem_entry *ce;
295 const u32 tcpa_default_log_len = 0x10000;
296 void *lasa;
Philipp Deppenwiese791ba972018-07-30 01:15:20 +0200297 ce = cbmem_entry_find(CBMEM_ID_TCPA_TCG_LOG);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200298 if (ce) {
299 lasa = cbmem_entry_start(ce);
300 *size = cbmem_entry_size(ce);
301 printk(BIOS_DEBUG, "TCPA log found at %p\n", lasa);
302 return lasa;
303 }
Philipp Deppenwiese791ba972018-07-30 01:15:20 +0200304 lasa = cbmem_add(CBMEM_ID_TCPA_TCG_LOG, tcpa_default_log_len);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200305 if (!lasa) {
306 printk(BIOS_ERR, "TCPA log creation failed\n");
307 return NULL;
308 }
309
310 printk(BIOS_DEBUG, "TCPA log created at %p\n", lasa);
Lee Leahy024b13d2017-03-16 13:41:11 -0700311 memset(lasa, 0, tcpa_default_log_len);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200312
313 *size = tcpa_default_log_len;
314 return lasa;
315}
316
317static void acpi_create_tcpa(acpi_tcpa_t *tcpa)
318{
319 acpi_header_t *header = &(tcpa->header);
320 u32 tcpa_log_len;
321 void *lasa;
322
323 memset((void *)tcpa, 0, sizeof(acpi_tcpa_t));
324
325 lasa = get_tcpa_log(&tcpa_log_len);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700326 if (!lasa)
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200327 return;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200328
John Zhao2ba303e2019-05-28 16:48:14 -0700329 if (!header)
330 return;
331
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200332 /* Fill out header fields. */
333 memcpy(header->signature, "TCPA", 4);
334 memcpy(header->oem_id, OEM_ID, 6);
335 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
336 memcpy(header->asl_compiler_id, ASLC, 4);
337
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100338 header->asl_compiler_revision = asl_revision;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200339 header->length = sizeof(acpi_tcpa_t);
Marc Jones93a51762018-08-22 18:57:24 -0600340 header->revision = get_acpi_table_revision(TCPA);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200341
342 tcpa->platform_class = 0;
343 tcpa->laml = tcpa_log_len;
Stefan Reinauerdefee172015-06-18 01:11:19 -0700344 tcpa->lasa = (uintptr_t) lasa;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200345
346 /* Calculate checksum. */
347 header->checksum = acpi_checksum((void *)tcpa, header->length);
348}
349
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100350static void *get_tpm2_log(u32 *size)
351{
352 const struct cbmem_entry *ce;
353 const u32 tpm2_default_log_len = 0x10000;
354 void *lasa;
355 ce = cbmem_entry_find(CBMEM_ID_TPM2_TCG_LOG);
356 if (ce) {
357 lasa = cbmem_entry_start(ce);
358 *size = cbmem_entry_size(ce);
359 printk(BIOS_DEBUG, "TPM2 log found at %p\n", lasa);
360 return lasa;
361 }
362 lasa = cbmem_add(CBMEM_ID_TPM2_TCG_LOG, tpm2_default_log_len);
363 if (!lasa) {
364 printk(BIOS_ERR, "TPM2 log creation failed\n");
365 return NULL;
366 }
367
368 printk(BIOS_DEBUG, "TPM2 log created at %p\n", lasa);
369 memset(lasa, 0, tpm2_default_log_len);
370
371 *size = tpm2_default_log_len;
372 return lasa;
373}
374
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200375static void acpi_create_tpm2(acpi_tpm2_t *tpm2)
376{
377 acpi_header_t *header = &(tpm2->header);
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100378 u32 tpm2_log_len;
379 void *lasa;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200380
381 memset((void *)tpm2, 0, sizeof(acpi_tpm2_t));
382
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100383 /*
384 * Some payloads like SeaBIOS depend on log area to use TPM2.
385 * Get the memory size and address of TPM2 log area or initialize it.
386 */
387 lasa = get_tpm2_log(&tpm2_log_len);
388 if (!lasa)
389 tpm2_log_len = 0;
390
John Zhao2ba303e2019-05-28 16:48:14 -0700391 if (!header)
392 return;
393
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200394 /* Fill out header fields. */
395 memcpy(header->signature, "TPM2", 4);
396 memcpy(header->oem_id, OEM_ID, 6);
397 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
398 memcpy(header->asl_compiler_id, ASLC, 4);
399
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100400 header->asl_compiler_revision = asl_revision;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200401 header->length = sizeof(acpi_tpm2_t);
402 header->revision = get_acpi_table_revision(TPM2);
403
404 /* Hard to detect for coreboot. Just set it to 0 */
405 tpm2->platform_class = 0;
Christian Walter26e0d4c2019-07-14 15:47:23 +0200406 if (CONFIG(CRB_TPM)) {
407 /* Must be set to 7 for CRB Support */
408 tpm2->control_area = CONFIG_CRB_TPM_BASE_ADDRESS + 0x40;
409 tpm2->start_method = 7;
410 } else {
411 /* Must be set to 0 for FIFO interface support */
412 tpm2->control_area = 0;
413 tpm2->start_method = 6;
414 }
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200415 memset(tpm2->msp, 0, sizeof(tpm2->msp));
416
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100417 /* Fill the log area size and start address fields. */
418 tpm2->laml = tpm2_log_len;
419 tpm2->lasa = (uintptr_t) lasa;
420
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200421 /* Calculate checksum. */
422 header->checksum = acpi_checksum((void *)tpm2, header->length);
423}
424
Duncan Laurie37319032016-05-26 12:47:05 -0700425static void acpi_ssdt_write_cbtable(void)
426{
427 const struct cbmem_entry *cbtable;
428 uintptr_t base;
429 uint32_t size;
430
431 cbtable = cbmem_entry_find(CBMEM_ID_CBTABLE);
432 if (!cbtable)
433 return;
434 base = (uintptr_t)cbmem_entry_start(cbtable);
435 size = cbmem_entry_size(cbtable);
436
437 acpigen_write_device("CTBL");
438 acpigen_write_coreboot_hid(COREBOOT_ACPI_ID_CBTABLE);
439 acpigen_write_name_integer("_UID", 0);
David Wu5dff3962018-03-21 16:48:53 +0800440 acpigen_write_STA(ACPI_STATUS_DEVICE_HIDDEN_ON);
Duncan Laurie37319032016-05-26 12:47:05 -0700441 acpigen_write_name("_CRS");
442 acpigen_write_resourcetemplate_header();
443 acpigen_write_mem32fixed(0, base, size);
444 acpigen_write_resourcetemplate_footer();
445 acpigen_pop_len();
446}
447
Myles Watson3fe6b702009-10-09 20:13:43 +0000448void acpi_create_ssdt_generator(acpi_header_t *ssdt, const char *oem_table_id)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000449{
Uwe Hermann622824c2010-11-19 15:14:42 +0000450 unsigned long current = (unsigned long)ssdt + sizeof(acpi_header_t);
451
Rudolf Marek293b5f52009-02-01 18:35:15 +0000452 memset((void *)ssdt, 0, sizeof(acpi_header_t));
Uwe Hermann622824c2010-11-19 15:14:42 +0000453
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000454 memcpy(&ssdt->signature, "SSDT", 4);
Marc Jones93a51762018-08-22 18:57:24 -0600455 ssdt->revision = get_acpi_table_revision(SSDT);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000456 memcpy(&ssdt->oem_id, OEM_ID, 6);
457 memcpy(&ssdt->oem_table_id, oem_table_id, 8);
458 ssdt->oem_revision = 42;
Uwe Hermann622824c2010-11-19 15:14:42 +0000459 memcpy(&ssdt->asl_compiler_id, ASLC, 4);
Elyes HAOUAS26071aa2019-02-15 08:21:33 +0100460 ssdt->asl_compiler_revision = asl_revision;
Rudolf Marek293b5f52009-02-01 18:35:15 +0000461 ssdt->length = sizeof(acpi_header_t);
462
Stefan Reinauer8dcd50b2009-03-06 17:24:29 +0000463 acpigen_set_current((char *) current);
Duncan Laurie37319032016-05-26 12:47:05 -0700464
465 /* Write object to declare coreboot tables */
466 acpi_ssdt_write_cbtable();
467
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200468 {
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100469 struct device *dev;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200470 for (dev = all_devices; dev; dev = dev->next)
Karthikeyan Ramasubramaniand1c0f952020-11-02 16:26:52 -0700471 if (dev->enabled && dev->ops && dev->ops->acpi_fill_ssdt)
Nico Huber68680dd2020-03-31 17:34:52 +0200472 dev->ops->acpi_fill_ssdt(dev);
Vladimir Serbinenko0a669912014-10-05 14:34:17 +0200473 current = (unsigned long) acpigen_get_current();
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200474 }
Rudolf Marek293b5f52009-02-01 18:35:15 +0000475
Uwe Hermann622824c2010-11-19 15:14:42 +0000476 /* (Re)calculate length and checksum. */
Rudolf Marek293b5f52009-02-01 18:35:15 +0000477 ssdt->length = current - (unsigned long)ssdt;
478 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
479}
480
Stefan Reinauerf622d592005-11-26 16:56:05 +0000481int acpi_create_srat_lapic(acpi_srat_lapic_t *lapic, u8 node, u8 apic)
482{
Myles Watson0b4c9f02009-03-10 18:06:47 +0000483 memset((void *)lapic, 0, sizeof(acpi_srat_lapic_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000484
Uwe Hermann622824c2010-11-19 15:14:42 +0000485 lapic->type = 0; /* Processor local APIC/SAPIC affinity structure */
486 lapic->length = sizeof(acpi_srat_lapic_t);
487 lapic->flags = (1 << 0); /* Enabled (the use of this structure). */
488 lapic->proximity_domain_7_0 = node;
489 /* TODO: proximity_domain_31_8, local SAPIC EID, clock domain. */
490 lapic->apic_id = apic;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000491
Uwe Hermann622824c2010-11-19 15:14:42 +0000492 return lapic->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000493}
494
Uwe Hermann622824c2010-11-19 15:14:42 +0000495int 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 +0300496 u32 flags)
Stefan Reinauerf622d592005-11-26 16:56:05 +0000497{
Uwe Hermann622824c2010-11-19 15:14:42 +0000498 mem->type = 1; /* Memory affinity structure */
499 mem->length = sizeof(acpi_srat_mem_t);
500 mem->base_address_low = (basek << 10);
501 mem->base_address_high = (basek >> (32 - 10));
502 mem->length_low = (sizek << 10);
503 mem->length_high = (sizek >> (32 - 10));
504 mem->proximity_domain = node;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000505 mem->flags = flags;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000506
Uwe Hermann622824c2010-11-19 15:14:42 +0000507 return mem->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000508}
509
Uwe Hermann622824c2010-11-19 15:14:42 +0000510/* http://www.microsoft.com/whdc/system/sysinternals/sratdwn.mspx */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +0200511void acpi_create_srat(acpi_srat_t *srat,
512 unsigned long (*acpi_fill_srat)(unsigned long current))
Stefan Reinauerf622d592005-11-26 16:56:05 +0000513{
Uwe Hermann622824c2010-11-19 15:14:42 +0000514 acpi_header_t *header = &(srat->header);
515 unsigned long current = (unsigned long)srat + sizeof(acpi_srat_t);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000516
Uwe Hermann622824c2010-11-19 15:14:42 +0000517 memset((void *)srat, 0, sizeof(acpi_srat_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000518
John Zhao2ba303e2019-05-28 16:48:14 -0700519 if (!header)
520 return;
521
Uwe Hermann622824c2010-11-19 15:14:42 +0000522 /* Fill out header fields. */
523 memcpy(header->signature, "SRAT", 4);
524 memcpy(header->oem_id, OEM_ID, 6);
525 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
526 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000527
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100528 header->asl_compiler_revision = asl_revision;
Uwe Hermann622824c2010-11-19 15:14:42 +0000529 header->length = sizeof(acpi_srat_t);
Marc Jones93a51762018-08-22 18:57:24 -0600530 header->revision = get_acpi_table_revision(SRAT);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000531
Uwe Hermann622824c2010-11-19 15:14:42 +0000532 srat->resv = 1; /* Spec: Reserved to 1 for backwards compatibility. */
Stefan Reinauerf622d592005-11-26 16:56:05 +0000533
Uwe Hermann622824c2010-11-19 15:14:42 +0000534 current = acpi_fill_srat(current);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000535
Uwe Hermann622824c2010-11-19 15:14:42 +0000536 /* (Re)calculate length and checksum. */
537 header->length = current - (unsigned long)srat;
538 header->checksum = acpi_checksum((void *)srat, header->length);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000539}
540
Nico Hubere561f352015-10-26 11:51:25 +0100541void acpi_create_dmar(acpi_dmar_t *dmar, enum dmar_flags flags,
Lee Leahy024b13d2017-03-16 13:41:11 -0700542 unsigned long (*acpi_fill_dmar)(unsigned long))
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200543{
544 acpi_header_t *header = &(dmar->header);
545 unsigned long current = (unsigned long)dmar + sizeof(acpi_dmar_t);
546
547 memset((void *)dmar, 0, sizeof(acpi_dmar_t));
548
John Zhao2ba303e2019-05-28 16:48:14 -0700549 if (!header)
550 return;
551
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200552 /* Fill out header fields. */
553 memcpy(header->signature, "DMAR", 4);
554 memcpy(header->oem_id, OEM_ID, 6);
555 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
556 memcpy(header->asl_compiler_id, ASLC, 4);
557
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100558 header->asl_compiler_revision = asl_revision;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200559 header->length = sizeof(acpi_dmar_t);
Marc Jones93a51762018-08-22 18:57:24 -0600560 header->revision = get_acpi_table_revision(DMAR);
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200561
Jacob Laskaf3f654d2016-03-15 21:53:27 -0500562 dmar->host_address_width = cpu_phys_address_size() - 1;
Nico Hubere561f352015-10-26 11:51:25 +0100563 dmar->flags = flags;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200564
565 current = acpi_fill_dmar(current);
566
567 /* (Re)calculate length and checksum. */
568 header->length = current - (unsigned long)dmar;
569 header->checksum = acpi_checksum((void *)dmar, header->length);
570}
571
572unsigned long acpi_create_dmar_drhd(unsigned long current, u8 flags,
Matt DeVillier7866d492018-03-29 14:59:57 +0200573 u16 segment, u64 bar)
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200574{
575 dmar_entry_t *drhd = (dmar_entry_t *)current;
576 memset(drhd, 0, sizeof(*drhd));
577 drhd->type = DMAR_DRHD;
578 drhd->length = sizeof(*drhd); /* will be fixed up later */
579 drhd->flags = flags;
580 drhd->segment = segment;
581 drhd->bar = bar;
582
583 return drhd->length;
584}
585
Matt DeVillier7866d492018-03-29 14:59:57 +0200586unsigned long acpi_create_dmar_rmrr(unsigned long current, u16 segment,
587 u64 bar, u64 limit)
588{
589 dmar_rmrr_entry_t *rmrr = (dmar_rmrr_entry_t *)current;
590 memset(rmrr, 0, sizeof(*rmrr));
591 rmrr->type = DMAR_RMRR;
592 rmrr->length = sizeof(*rmrr); /* will be fixed up later */
593 rmrr->segment = segment;
594 rmrr->bar = bar;
595 rmrr->limit = limit;
596
597 return rmrr->length;
598}
599
Werner Zehd4d76952016-07-27 06:56:36 +0200600unsigned long acpi_create_dmar_atsr(unsigned long current, u8 flags,
601 u16 segment)
602{
603 dmar_atsr_entry_t *atsr = (dmar_atsr_entry_t *)current;
604 memset(atsr, 0, sizeof(*atsr));
605 atsr->type = DMAR_ATSR;
606 atsr->length = sizeof(*atsr); /* will be fixed up later */
607 atsr->flags = flags;
608 atsr->segment = segment;
609
610 return atsr->length;
611}
612
John Zhao76e70672019-04-04 11:03:28 -0700613unsigned long acpi_create_dmar_rhsa(unsigned long current, u64 base_addr,
614 u32 proximity_domain)
615{
616 dmar_rhsa_entry_t *rhsa = (dmar_rhsa_entry_t *)current;
617 memset(rhsa, 0, sizeof(*rhsa));
618 rhsa->type = DMAR_RHSA;
619 rhsa->length = sizeof(*rhsa);
620 rhsa->base_address = base_addr;
621 rhsa->proximity_domain = proximity_domain;
622
623 return rhsa->length;
624}
625
626unsigned long acpi_create_dmar_andd(unsigned long current, u8 device_number,
627 const char *device_name)
628{
629 dmar_andd_entry_t *andd = (dmar_andd_entry_t *)current;
630 int andd_len = sizeof(dmar_andd_entry_t) + strlen(device_name) + 1;
631 memset(andd, 0, andd_len);
632 andd->type = DMAR_ANDD;
633 andd->length = andd_len;
634 andd->device_number = device_number;
635 memcpy(&andd->device_name, device_name, strlen(device_name));
636
637 return andd->length;
638}
639
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200640void acpi_dmar_drhd_fixup(unsigned long base, unsigned long current)
641{
642 dmar_entry_t *drhd = (dmar_entry_t *)base;
643 drhd->length = current - base;
644}
645
Matt DeVillier7866d492018-03-29 14:59:57 +0200646void acpi_dmar_rmrr_fixup(unsigned long base, unsigned long current)
647{
648 dmar_rmrr_entry_t *rmrr = (dmar_rmrr_entry_t *)base;
649 rmrr->length = current - base;
650}
651
Werner Zehd4d76952016-07-27 06:56:36 +0200652void acpi_dmar_atsr_fixup(unsigned long base, unsigned long current)
653{
654 dmar_atsr_entry_t *atsr = (dmar_atsr_entry_t *)base;
655 atsr->length = current - base;
656}
657
Matt DeVillier7866d492018-03-29 14:59:57 +0200658static unsigned long acpi_create_dmar_ds(unsigned long current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100659 enum dev_scope_type type, u8 enumeration_id, u8 bus, u8 dev, u8 fn)
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200660{
Nico Huber6c4751d2015-10-26 12:03:54 +0100661 /* we don't support longer paths yet */
662 const size_t dev_scope_length = sizeof(dev_scope_t) + 2;
663
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200664 dev_scope_t *ds = (dev_scope_t *)current;
Nico Huber6c4751d2015-10-26 12:03:54 +0100665 memset(ds, 0, dev_scope_length);
666 ds->type = type;
667 ds->length = dev_scope_length;
668 ds->enumeration = enumeration_id;
669 ds->start_bus = bus;
670 ds->path[0].dev = dev;
671 ds->path[0].fn = fn;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200672
673 return ds->length;
674}
675
Matt DeVillier7866d492018-03-29 14:59:57 +0200676unsigned long acpi_create_dmar_ds_pci_br(unsigned long current, u8 bus,
Werner Zeh21a5bff2016-07-27 07:07:20 +0200677 u8 dev, u8 fn)
678{
Matt DeVillier7866d492018-03-29 14:59:57 +0200679 return acpi_create_dmar_ds(current,
Werner Zeh21a5bff2016-07-27 07:07:20 +0200680 SCOPE_PCI_SUB, 0, bus, dev, fn);
681}
682
Matt DeVillier7866d492018-03-29 14:59:57 +0200683unsigned long acpi_create_dmar_ds_pci(unsigned long current, u8 bus,
Nico Huber6c4751d2015-10-26 12:03:54 +0100684 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_PCI_ENDPOINT, 0, bus, dev, fn);
688}
689
Matt DeVillier7866d492018-03-29 14:59:57 +0200690unsigned long acpi_create_dmar_ds_ioapic(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_IOAPIC, enumeration_id, bus, dev, fn);
695}
696
Matt DeVillier7866d492018-03-29 14:59:57 +0200697unsigned long acpi_create_dmar_ds_msi_hpet(unsigned long current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100698 u8 enumeration_id, u8 bus, u8 dev, u8 fn)
699{
Matt DeVillier7866d492018-03-29 14:59:57 +0200700 return acpi_create_dmar_ds(current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100701 SCOPE_MSI_HPET, enumeration_id, bus, dev, fn);
702}
703
Uwe Hermann622824c2010-11-19 15:14:42 +0000704/* http://h21007.www2.hp.com/portal/download/files/unprot/Itanium/slit.pdf */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +0200705void acpi_create_slit(acpi_slit_t *slit,
706 unsigned long (*acpi_fill_slit)(unsigned long current))
Yinghai Lud4b278c2006-10-04 20:46:15 +0000707{
Uwe Hermann622824c2010-11-19 15:14:42 +0000708 acpi_header_t *header = &(slit->header);
709 unsigned long current = (unsigned long)slit + sizeof(acpi_slit_t);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000710
Uwe Hermann622824c2010-11-19 15:14:42 +0000711 memset((void *)slit, 0, sizeof(acpi_slit_t));
Yinghai Lud4b278c2006-10-04 20:46:15 +0000712
John Zhao2ba303e2019-05-28 16:48:14 -0700713 if (!header)
714 return;
715
Uwe Hermann622824c2010-11-19 15:14:42 +0000716 /* Fill out header fields. */
717 memcpy(header->signature, "SLIT", 4);
718 memcpy(header->oem_id, OEM_ID, 6);
719 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
720 memcpy(header->asl_compiler_id, ASLC, 4);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000721
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100722 header->asl_compiler_revision = asl_revision;
Uwe Hermann622824c2010-11-19 15:14:42 +0000723 header->length = sizeof(acpi_slit_t);
Marc Jones93a51762018-08-22 18:57:24 -0600724 header->revision = get_acpi_table_revision(SLIT);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000725
Uwe Hermann622824c2010-11-19 15:14:42 +0000726 current = acpi_fill_slit(current);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000727
Uwe Hermann622824c2010-11-19 15:14:42 +0000728 /* (Re)calculate length and checksum. */
729 header->length = current - (unsigned long)slit;
730 header->checksum = acpi_checksum((void *)slit, header->length);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000731}
732
Uwe Hermann622824c2010-11-19 15:14:42 +0000733/* http://www.intel.com/hardwaredesign/hpetspec_1.pdf */
Stefan Reinauer777224c2005-01-19 14:06:41 +0000734void acpi_create_hpet(acpi_hpet_t *hpet)
Stefan Reinauer688b3852004-01-28 16:56:14 +0000735{
Uwe Hermann622824c2010-11-19 15:14:42 +0000736 acpi_header_t *header = &(hpet->header);
737 acpi_addr_t *addr = &(hpet->addr);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000738
Stefan Reinauera7648c22004-01-29 17:31:34 +0000739 memset((void *)hpet, 0, sizeof(acpi_hpet_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000740
John Zhao2ba303e2019-05-28 16:48:14 -0700741 if (!header)
742 return;
743
Uwe Hermann622824c2010-11-19 15:14:42 +0000744 /* Fill out header fields. */
Stefan Reinauer4704dc52009-07-22 01:11:37 +0000745 memcpy(header->signature, "HPET", 4);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000746 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000747 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000748 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000749
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100750 header->asl_compiler_revision = asl_revision;
Stefan Reinauer688b3852004-01-28 16:56:14 +0000751 header->length = sizeof(acpi_hpet_t);
Marc Jones93a51762018-08-22 18:57:24 -0600752 header->revision = get_acpi_table_revision(HPET);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000753
Uwe Hermann622824c2010-11-19 15:14:42 +0000754 /* Fill out HPET address. */
Elyes HAOUAS04071f42020-07-20 17:05:24 +0200755 addr->space_id = ACPI_ADDRESS_SPACE_MEMORY;
Uwe Hermann622824c2010-11-19 15:14:42 +0000756 addr->bit_width = 64;
757 addr->bit_offset = 0;
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200758 addr->addrl = CONFIG_HPET_ADDRESS & 0xffffffff;
759 addr->addrh = ((unsigned long long)CONFIG_HPET_ADDRESS) >> 32;
Stefan Reinauer688b3852004-01-28 16:56:14 +0000760
Lee Leahy024b13d2017-03-16 13:41:11 -0700761 hpet->id = *(unsigned int *)CONFIG_HPET_ADDRESS;
Uwe Hermann622824c2010-11-19 15:14:42 +0000762 hpet->number = 0;
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200763 hpet->min_tick = CONFIG_HPET_MIN_TICKS;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000764
Uwe Hermann622824c2010-11-19 15:14:42 +0000765 header->checksum = acpi_checksum((void *)hpet, sizeof(acpi_hpet_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000766}
Uwe Hermann622824c2010-11-19 15:14:42 +0000767
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700768void acpi_create_vfct(const struct device *device,
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530769 acpi_vfct_t *vfct,
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700770 unsigned long (*acpi_fill_vfct)(const struct device *device,
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530771 acpi_vfct_t *vfct_struct, unsigned long current))
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200772{
773 acpi_header_t *header = &(vfct->header);
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530774 unsigned long current = (unsigned long)vfct + sizeof(acpi_vfct_t);
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200775
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530776 memset((void *)vfct, 0, sizeof(acpi_vfct_t));
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200777
John Zhao2ba303e2019-05-28 16:48:14 -0700778 if (!header)
779 return;
780
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200781 /* Fill out header fields. */
782 memcpy(header->signature, "VFCT", 4);
783 memcpy(header->oem_id, OEM_ID, 6);
784 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
785 memcpy(header->asl_compiler_id, ASLC, 4);
786
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100787 header->asl_compiler_revision = asl_revision;
Marc Jones93a51762018-08-22 18:57:24 -0600788 header->revision = get_acpi_table_revision(VFCT);
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200789
790 current = acpi_fill_vfct(device, vfct, current);
791
Kyösti Mälkkifb493792019-06-30 06:29:52 +0300792 /* If no BIOS image, return with header->length == 0. */
793 if (!vfct->VBIOSImageOffset)
794 return;
795
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200796 /* (Re)calculate length and checksum. */
797 header->length = current - (unsigned long)vfct;
798 header->checksum = acpi_checksum((void *)vfct, header->length);
799}
800
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700801void acpi_create_ipmi(const struct device *device,
Patrick Rudolph9d98e5a2019-06-14 19:00:04 +0200802 struct acpi_spmi *spmi,
803 const u16 ipmi_revision,
804 const acpi_addr_t *addr,
805 const enum acpi_ipmi_interface_type type,
806 const s8 gpe_interrupt,
807 const u32 apic_interrupt,
808 const u32 uid)
809{
810 acpi_header_t *header = &(spmi->header);
811 memset((void *)spmi, 0, sizeof(struct acpi_spmi));
812
813 /* Fill out header fields. */
814 memcpy(header->signature, "SPMI", 4);
815 memcpy(header->oem_id, OEM_ID, 6);
816 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
817 memcpy(header->asl_compiler_id, ASLC, 4);
818
819 header->asl_compiler_revision = asl_revision;
820 header->length = sizeof(struct acpi_spmi);
821 header->revision = get_acpi_table_revision(SPMI);
822
823 spmi->reserved = 1;
824
825 if (device->path.type == DEVICE_PATH_PCI) {
826 spmi->pci_device_flag = ACPI_IPMI_PCI_DEVICE_FLAG;
827 spmi->pci_bus = device->bus->secondary;
828 spmi->pci_device = device->path.pci.devfn >> 3;
829 spmi->pci_function = device->path.pci.devfn & 0x7;
830 } else if (type != IPMI_INTERFACE_SSIF) {
831 memcpy(spmi->uid, &uid, sizeof(spmi->uid));
832 }
833
834 spmi->base_address = *addr;
835 spmi->specification_revision = ipmi_revision;
836
837 spmi->interface_type = type;
838
839 if (gpe_interrupt >= 0 && gpe_interrupt < 32) {
840 spmi->gpe = gpe_interrupt;
841 spmi->interrupt_type |= ACPI_IPMI_INT_TYPE_SCI;
842 }
843 if (apic_interrupt > 0) {
844 spmi->global_system_interrupt = apic_interrupt;
845 spmi->interrupt_type |= ACPI_IPMI_INT_TYPE_APIC;
846 }
847
848 /* Calculate checksum. */
849 header->checksum = acpi_checksum((void *)spmi, header->length);
850}
851
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500852void acpi_create_ivrs(acpi_ivrs_t *ivrs,
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700853 unsigned long (*acpi_fill_ivrs)(acpi_ivrs_t *ivrs_struct,
854 unsigned long current))
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500855{
856 acpi_header_t *header = &(ivrs->header);
857 unsigned long current = (unsigned long)ivrs + sizeof(acpi_ivrs_t);
858
859 memset((void *)ivrs, 0, sizeof(acpi_ivrs_t));
860
John Zhao2ba303e2019-05-28 16:48:14 -0700861 if (!header)
862 return;
863
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500864 /* Fill out header fields. */
865 memcpy(header->signature, "IVRS", 4);
866 memcpy(header->oem_id, OEM_ID, 6);
867 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
868 memcpy(header->asl_compiler_id, ASLC, 4);
869
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100870 header->asl_compiler_revision = asl_revision;
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500871 header->length = sizeof(acpi_ivrs_t);
Marc Jones93a51762018-08-22 18:57:24 -0600872 header->revision = get_acpi_table_revision(IVRS);
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500873
874 current = acpi_fill_ivrs(ivrs, current);
875
876 /* (Re)calculate length and checksum. */
877 header->length = current - (unsigned long)ivrs;
878 header->checksum = acpi_checksum((void *)ivrs, header->length);
879}
880
Jason Glenesk61624b22020-11-02 20:06:23 -0800881void acpi_create_crat(struct acpi_crat_header *crat,
882 unsigned long (*acpi_fill_crat)(struct acpi_crat_header *crat_struct,
883 unsigned long current))
884{
885 acpi_header_t *header = &(crat->header);
886 unsigned long current = (unsigned long)crat + sizeof(struct acpi_crat_header);
887
888 memset((void *)crat, 0, sizeof(struct acpi_crat_header));
889
890 if (!header)
891 return;
892
893 /* Fill out header fields. */
894 memcpy(header->signature, "CRAT", 4);
895 memcpy(header->oem_id, OEM_ID, 6);
896 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
897 memcpy(header->asl_compiler_id, ASLC, 4);
898
899 header->asl_compiler_revision = asl_revision;
900 header->length = sizeof(struct acpi_crat_header);
901 header->revision = get_acpi_table_revision(CRAT);
902
903 current = acpi_fill_crat(crat, current);
904
905 /* (Re)calculate length and checksum. */
906 header->length = current - (unsigned long)crat;
907 header->checksum = acpi_checksum((void *)crat, header->length);
908}
909
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700910unsigned long acpi_write_hpet(const struct device *device, unsigned long current,
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700911 acpi_rsdp_t *rsdp)
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200912{
913 acpi_hpet_t *hpet;
914
915 /*
916 * We explicitly add these tables later on:
917 */
918 printk(BIOS_DEBUG, "ACPI: * HPET\n");
919
920 hpet = (acpi_hpet_t *) current;
921 current += sizeof(acpi_hpet_t);
Felix Heldc4697122019-06-20 13:50:17 +0200922 current = ALIGN_UP(current, 16);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200923 acpi_create_hpet(hpet);
924 acpi_add_table(rsdp, hpet);
925
926 return current;
927}
928
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800929void acpi_create_dbg2(acpi_dbg2_header_t *dbg2,
930 int port_type, int port_subtype,
931 acpi_addr_t *address, uint32_t address_size,
932 const char *device_path)
933{
934 uintptr_t current;
935 acpi_dbg2_device_t *device;
936 uint32_t *dbg2_addr_size;
937 acpi_header_t *header;
938 size_t path_len;
939 const char *path;
940 char *namespace;
941
942 /* Fill out header fields. */
943 current = (uintptr_t)dbg2;
944 memset(dbg2, 0, sizeof(acpi_dbg2_header_t));
945 header = &(dbg2->header);
John Zhao2ba303e2019-05-28 16:48:14 -0700946
947 if (!header)
948 return;
949
Marc Jones93a51762018-08-22 18:57:24 -0600950 header->revision = get_acpi_table_revision(DBG2);
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800951 memcpy(header->signature, "DBG2", 4);
952 memcpy(header->oem_id, OEM_ID, 6);
953 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
954 memcpy(header->asl_compiler_id, ASLC, 4);
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100955 header->asl_compiler_revision = asl_revision;
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800956
957 /* One debug device defined */
958 dbg2->devices_offset = sizeof(acpi_dbg2_header_t);
959 dbg2->devices_count = 1;
960 current += sizeof(acpi_dbg2_header_t);
961
962 /* Device comes after the header */
963 device = (acpi_dbg2_device_t *)current;
964 memset(device, 0, sizeof(acpi_dbg2_device_t));
965 current += sizeof(acpi_dbg2_device_t);
966
967 device->revision = 0;
968 device->address_count = 1;
969 device->port_type = port_type;
970 device->port_subtype = port_subtype;
971
972 /* Base Address comes after device structure */
973 memcpy((void *)current, address, sizeof(acpi_addr_t));
974 device->base_address_offset = current - (uintptr_t)device;
975 current += sizeof(acpi_addr_t);
976
977 /* Address Size comes after address structure */
978 dbg2_addr_size = (uint32_t *)current;
979 device->address_size_offset = current - (uintptr_t)device;
980 *dbg2_addr_size = address_size;
981 current += sizeof(uint32_t);
982
983 /* Namespace string comes last, use '.' if not provided */
984 path = device_path ? : ".";
985 /* Namespace string length includes NULL terminator */
986 path_len = strlen(path) + 1;
987 namespace = (char *)current;
988 device->namespace_string_length = path_len;
989 device->namespace_string_offset = current - (uintptr_t)device;
990 strncpy(namespace, path, path_len);
991 current += path_len;
992
993 /* Update structure lengths and checksum */
994 device->length = current - (uintptr_t)device;
995 header->length = current - (uintptr_t)dbg2;
996 header->checksum = acpi_checksum((uint8_t *)dbg2, header->length);
997}
998
999unsigned long acpi_write_dbg2_pci_uart(acpi_rsdp_t *rsdp, unsigned long current,
Aamir Bohrae825d3f2019-07-30 10:11:41 +05301000 const struct device *dev, uint8_t access_size)
Duncan Lauriee4a36c72017-11-11 19:33:25 -08001001{
1002 acpi_dbg2_header_t *dbg2 = (acpi_dbg2_header_t *)current;
1003 struct resource *res;
1004 acpi_addr_t address;
1005
1006 if (!dev) {
Wim Vervoorn6cd52432020-02-03 14:41:46 +01001007 printk(BIOS_DEBUG, "%s: Device not found\n", __func__);
Duncan Lauriee4a36c72017-11-11 19:33:25 -08001008 return current;
1009 }
Mario Scheithauerf1eb0ea2017-11-21 13:52:53 +01001010 if (!dev->enabled) {
1011 printk(BIOS_INFO, "%s: Device not enabled\n", __func__);
1012 return current;
1013 }
Duncan Lauriee4a36c72017-11-11 19:33:25 -08001014 res = find_resource(dev, PCI_BASE_ADDRESS_0);
1015 if (!res) {
1016 printk(BIOS_ERR, "%s: Unable to find resource for %s\n",
1017 __func__, dev_path(dev));
1018 return current;
1019 }
1020
1021 memset(&address, 0, sizeof(address));
1022 if (res->flags & IORESOURCE_IO)
1023 address.space_id = ACPI_ADDRESS_SPACE_IO;
1024 else if (res->flags & IORESOURCE_MEM)
1025 address.space_id = ACPI_ADDRESS_SPACE_MEMORY;
1026 else {
1027 printk(BIOS_ERR, "%s: Unknown address space type\n", __func__);
1028 return current;
1029 }
1030
1031 address.addrl = (uint32_t)res->base;
1032 address.addrh = (uint32_t)((res->base >> 32) & 0xffffffff);
1033 address.access_size = access_size;
1034
1035 acpi_create_dbg2(dbg2,
1036 ACPI_DBG2_PORT_SERIAL,
1037 ACPI_DBG2_PORT_SERIAL_16550,
1038 &address, res->size,
1039 acpi_device_path(dev));
1040
1041 if (dbg2->header.length) {
1042 current += dbg2->header.length;
1043 current = acpi_align_current(current);
1044 acpi_add_table(rsdp, dbg2);
1045 }
1046
1047 return current;
1048}
1049
Stefan Reinauer777224c2005-01-19 14:06:41 +00001050void acpi_create_facs(acpi_facs_t *facs)
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +00001051{
Uwe Hermann622824c2010-11-19 15:14:42 +00001052 memset((void *)facs, 0, sizeof(acpi_facs_t));
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +00001053
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001054 memcpy(facs->signature, "FACS", 4);
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +00001055 facs->length = sizeof(acpi_facs_t);
1056 facs->hardware_signature = 0;
1057 facs->firmware_waking_vector = 0;
1058 facs->global_lock = 0;
1059 facs->flags = 0;
1060 facs->x_firmware_waking_vector_l = 0;
1061 facs->x_firmware_waking_vector_h = 0;
Marc Jones93a51762018-08-22 18:57:24 -06001062 facs->version = get_acpi_table_revision(FACS);
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +00001063}
Stefan Reinauer777224c2005-01-19 14:06:41 +00001064
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001065static void acpi_write_rsdt(acpi_rsdt_t *rsdt, char *oem_id, char *oem_table_id)
Stefan Reinauer14e22772010-04-27 06:56:47 +00001066{
Uwe Hermann622824c2010-11-19 15:14:42 +00001067 acpi_header_t *header = &(rsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001068
John Zhao2ba303e2019-05-28 16:48:14 -07001069 if (!header)
1070 return;
1071
Uwe Hermann622824c2010-11-19 15:14:42 +00001072 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001073 memcpy(header->signature, "RSDT", 4);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001074 memcpy(header->oem_id, oem_id, 6);
1075 memcpy(header->oem_table_id, oem_table_id, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +00001076 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001077
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001078 header->asl_compiler_revision = asl_revision;
Stefan Reinauer688b3852004-01-28 16:56:14 +00001079 header->length = sizeof(acpi_rsdt_t);
Marc Jones93a51762018-08-22 18:57:24 -06001080 header->revision = get_acpi_table_revision(RSDT);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001081
Uwe Hermann622824c2010-11-19 15:14:42 +00001082 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauer688b3852004-01-28 16:56:14 +00001083
Uwe Hermann622824c2010-11-19 15:14:42 +00001084 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001085 header->checksum = acpi_checksum((void *)rsdt, sizeof(acpi_rsdt_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +00001086}
1087
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001088static void acpi_write_xsdt(acpi_xsdt_t *xsdt, char *oem_id, char *oem_table_id)
Stefan Reinauer14e22772010-04-27 06:56:47 +00001089{
Uwe Hermann622824c2010-11-19 15:14:42 +00001090 acpi_header_t *header = &(xsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001091
John Zhao2ba303e2019-05-28 16:48:14 -07001092 if (!header)
1093 return;
1094
Uwe Hermann622824c2010-11-19 15:14:42 +00001095 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001096 memcpy(header->signature, "XSDT", 4);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001097 memcpy(header->oem_id, oem_id, 6);
1098 memcpy(header->oem_table_id, oem_table_id, 8);
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001099 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001100
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001101 header->asl_compiler_revision = asl_revision;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001102 header->length = sizeof(acpi_xsdt_t);
Marc Jones93a51762018-08-22 18:57:24 -06001103 header->revision = get_acpi_table_revision(XSDT);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001104
Uwe Hermann622824c2010-11-19 15:14:42 +00001105 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001106
Uwe Hermann622824c2010-11-19 15:14:42 +00001107 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001108 header->checksum = acpi_checksum((void *)xsdt, sizeof(acpi_xsdt_t));
1109}
1110
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001111static void acpi_write_rsdp(acpi_rsdp_t *rsdp, acpi_rsdt_t *rsdt,
1112 acpi_xsdt_t *xsdt, char *oem_id)
Stefan Reinauer688b3852004-01-28 16:56:14 +00001113{
Stefan Reinauerd18faac2009-11-05 18:06:43 +00001114 memset(rsdp, 0, sizeof(acpi_rsdp_t));
Uwe Hermann622824c2010-11-19 15:14:42 +00001115
Stefan Reinauer688b3852004-01-28 16:56:14 +00001116 memcpy(rsdp->signature, RSDP_SIG, 8);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001117 memcpy(rsdp->oem_id, oem_id, 6);
Uwe Hermann622824c2010-11-19 15:14:42 +00001118
1119 rsdp->length = sizeof(acpi_rsdp_t);
Stefan Reinauerdefee172015-06-18 01:11:19 -07001120 rsdp->rsdt_address = (uintptr_t)rsdt;
Uwe Hermann622824c2010-11-19 15:14:42 +00001121
1122 /*
1123 * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2.
1124 *
1125 * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
1126 * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
1127 * revision 0).
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001128 */
1129 if (xsdt == NULL) {
Uwe Hermann622824c2010-11-19 15:14:42 +00001130 rsdp->revision = 0;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001131 } else {
Stefan Reinauerdefee172015-06-18 01:11:19 -07001132 rsdp->xsdt_address = (u64)(uintptr_t)xsdt;
Marc Jones93a51762018-08-22 18:57:24 -06001133 rsdp->revision = get_acpi_table_revision(RSDP);
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001134 }
Uwe Hermann622824c2010-11-19 15:14:42 +00001135
1136 /* Calculate checksums. */
1137 rsdp->checksum = acpi_checksum((void *)rsdp, 20);
1138 rsdp->ext_checksum = acpi_checksum((void *)rsdp, sizeof(acpi_rsdp_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +00001139}
1140
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001141unsigned long acpi_create_hest_error_source(acpi_hest_t *hest,
1142 acpi_hest_esd_t *esd, u16 type, void *data, u16 data_len)
zbaocaf494c82012-04-13 13:57:14 +08001143{
1144 acpi_header_t *header = &(hest->header);
1145 acpi_hest_hen_t *hen;
1146 void *pos;
1147 u16 len;
1148
1149 pos = esd;
1150 memset(pos, 0, sizeof(acpi_hest_esd_t));
1151 len = 0;
1152 esd->type = type; /* MCE */
1153 esd->source_id = hest->error_source_count;
1154 esd->flags = 0; /* FIRMWARE_FIRST */
1155 esd->enabled = 1;
1156 esd->prealloc_erecords = 1;
1157 esd->max_section_per_record = 0x1;
1158
1159 len += sizeof(acpi_hest_esd_t);
1160 pos = esd + 1;
1161
1162 switch (type) {
1163 case 0: /* MCE */
1164 break;
1165 case 1: /* CMC */
1166 hen = (acpi_hest_hen_t *) (pos);
1167 memset(pos, 0, sizeof(acpi_hest_hen_t));
1168 hen->type = 3; /* SCI? */
1169 hen->length = sizeof(acpi_hest_hen_t);
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001170 hen->conf_we = 0; /* Configuration Write Enable. */
zbaocaf494c82012-04-13 13:57:14 +08001171 hen->poll_interval = 0;
1172 hen->vector = 0;
1173 hen->sw2poll_threshold_val = 0;
1174 hen->sw2poll_threshold_win = 0;
1175 hen->error_threshold_val = 0;
1176 hen->error_threshold_win = 0;
1177 len += sizeof(acpi_hest_hen_t);
1178 pos = hen + 1;
1179 break;
1180 case 2: /* NMI */
1181 case 6: /* AER Root Port */
1182 case 7: /* AER Endpoint */
1183 case 8: /* AER Bridge */
1184 case 9: /* Generic Hardware Error Source. */
1185 /* TODO: */
1186 break;
1187 default:
1188 printk(BIOS_DEBUG, "Invalid type of Error Source.");
1189 break;
1190 }
Lee Leahy024b13d2017-03-16 13:41:11 -07001191 hest->error_source_count++;
zbaocaf494c82012-04-13 13:57:14 +08001192
1193 memcpy(pos, data, data_len);
1194 len += data_len;
John Zhao2ba303e2019-05-28 16:48:14 -07001195 if (header)
1196 header->length += len;
zbaocaf494c82012-04-13 13:57:14 +08001197
1198 return len;
1199}
1200
1201/* ACPI 4.0 */
Vladimir Serbinenko807127f2014-11-09 13:36:18 +01001202void acpi_write_hest(acpi_hest_t *hest,
1203 unsigned long (*acpi_fill_hest)(acpi_hest_t *hest))
zbaocaf494c82012-04-13 13:57:14 +08001204{
1205 acpi_header_t *header = &(hest->header);
1206
1207 memset(hest, 0, sizeof(acpi_hest_t));
1208
John Zhao2ba303e2019-05-28 16:48:14 -07001209 if (!header)
1210 return;
1211
zbaocaf494c82012-04-13 13:57:14 +08001212 memcpy(header->signature, "HEST", 4);
1213 memcpy(header->oem_id, OEM_ID, 6);
1214 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1215 memcpy(header->asl_compiler_id, ASLC, 4);
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001216 header->asl_compiler_revision = asl_revision;
zbaocaf494c82012-04-13 13:57:14 +08001217 header->length += sizeof(acpi_hest_t);
Marc Jones93a51762018-08-22 18:57:24 -06001218 header->revision = get_acpi_table_revision(HEST);
zbaocaf494c82012-04-13 13:57:14 +08001219
1220 acpi_fill_hest(hest);
1221
1222 /* Calculate checksums. */
1223 header->checksum = acpi_checksum((void *)hest, header->length);
1224}
1225
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001226/* ACPI 3.0b */
1227void acpi_write_bert(acpi_bert_t *bert, uintptr_t region, size_t length)
1228{
1229 acpi_header_t *header = &(bert->header);
1230
1231 memset(bert, 0, sizeof(acpi_bert_t));
1232
John Zhao2ba303e2019-05-28 16:48:14 -07001233 if (!header)
1234 return;
1235
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001236 memcpy(header->signature, "BERT", 4);
1237 memcpy(header->oem_id, OEM_ID, 6);
1238 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1239 memcpy(header->asl_compiler_id, ASLC, 4);
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001240 header->asl_compiler_revision = asl_revision;
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001241 header->length += sizeof(acpi_bert_t);
1242 header->revision = get_acpi_table_revision(BERT);
1243
1244 bert->error_region = region;
1245 bert->region_length = length;
1246
1247 /* Calculate checksums. */
1248 header->checksum = acpi_checksum((void *)bert, header->length);
1249}
1250
Angel Pons79572e42020-07-13 00:17:43 +02001251__weak void arch_fill_fadt(acpi_fadt_t *fadt) { }
Kyösti Mälkkif9aac922020-05-30 16:16:28 +03001252__weak void soc_fill_fadt(acpi_fadt_t *fadt) { }
Kyösti Mälkki02fd15d2020-06-02 03:34:43 +03001253__weak void mainboard_fill_fadt(acpi_fadt_t *fadt) { }
Kyösti Mälkkif9aac922020-05-30 16:16:28 +03001254
Lee Leahy024b13d2017-03-16 13:41:11 -07001255void acpi_create_fadt(acpi_fadt_t *fadt, acpi_facs_t *facs, void *dsdt)
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001256{
1257 acpi_header_t *header = &(fadt->header);
1258
1259 memset((void *) fadt, 0, sizeof(acpi_fadt_t));
John Zhao2ba303e2019-05-28 16:48:14 -07001260
1261 if (!header)
1262 return;
1263
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001264 memcpy(header->signature, "FACP", 4);
1265 header->length = sizeof(acpi_fadt_t);
Marc Jones93a51762018-08-22 18:57:24 -06001266 header->revision = get_acpi_table_revision(FADT);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001267 memcpy(header->oem_id, OEM_ID, 6);
1268 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1269 memcpy(header->asl_compiler_id, ASLC, 4);
Elyes HAOUAS26071aa2019-02-15 08:21:33 +01001270 header->asl_compiler_revision = asl_revision;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001271
1272 fadt->firmware_ctrl = (unsigned long) facs;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001273 fadt->x_firmware_ctl_l = (unsigned long)facs;
1274 fadt->x_firmware_ctl_h = 0;
Kyösti Mälkkid4acee82020-06-03 07:20:55 +03001275
1276 fadt->dsdt = (unsigned long) dsdt;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001277 fadt->x_dsdt_l = (unsigned long)dsdt;
1278 fadt->x_dsdt_h = 0;
1279
Kyösti Mälkkid4acee82020-06-03 07:20:55 +03001280 /* should be 0 ACPI 3.0 */
1281 fadt->reserved = 0;
1282
Kyösti Mälkkie0d38682020-06-07 12:01:58 +03001283 fadt->preferred_pm_profile = acpi_get_preferred_pm_profile();
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001284
Angel Pons79572e42020-07-13 00:17:43 +02001285 arch_fill_fadt(fadt);
1286
Vladimir Serbinenkoab83ef02014-10-25 15:18:25 +02001287 acpi_fill_fadt(fadt);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001288
Kyösti Mälkkif9aac922020-05-30 16:16:28 +03001289 soc_fill_fadt(fadt);
Kyösti Mälkki02fd15d2020-06-02 03:34:43 +03001290 mainboard_fill_fadt(fadt);
Kyösti Mälkkif9aac922020-05-30 16:16:28 +03001291
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001292 header->checksum =
1293 acpi_checksum((void *) fadt, header->length);
1294}
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001295
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001296void acpi_create_lpit(acpi_lpit_t *lpit)
1297{
1298 acpi_header_t *header = &(lpit->header);
1299 unsigned long current = (unsigned long)lpit + sizeof(acpi_lpit_t);
1300
1301 memset((void *)lpit, 0, sizeof(acpi_lpit_t));
1302
1303 if (!header)
1304 return;
1305
1306 /* Fill out header fields. */
1307 memcpy(header->signature, "LPIT", 4);
1308 memcpy(header->oem_id, OEM_ID, 6);
1309 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1310 memcpy(header->asl_compiler_id, ASLC, 4);
1311
1312 header->asl_compiler_revision = asl_revision;
1313 header->revision = get_acpi_table_revision(LPIT);
1314 header->oem_revision = 42;
1315 header->length = sizeof(acpi_lpit_t);
1316
1317 current = acpi_fill_lpit(current);
1318
1319 /* (Re)calculate length and checksum. */
1320 header->length = current - (unsigned long)lpit;
1321 header->checksum = acpi_checksum((void *)lpit, header->length);
1322}
1323
1324unsigned long acpi_create_lpi_desc_ncst(acpi_lpi_desc_ncst_t *lpi_desc, uint16_t uid)
1325{
1326 memset(lpi_desc, 0, sizeof(acpi_lpi_desc_ncst_t));
1327 lpi_desc->header.length = sizeof(acpi_lpi_desc_ncst_t);
1328 lpi_desc->header.type = ACPI_LPI_DESC_TYPE_NATIVE_CSTATE;
1329 lpi_desc->header.uid = uid;
1330
1331 return lpi_desc->header.length;
1332}
1333
Aaron Durbin64031672018-04-21 14:45:32 -06001334unsigned long __weak fw_cfg_acpi_tables(unsigned long start)
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001335{
1336 return 0;
1337}
1338
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001339unsigned long write_acpi_tables(unsigned long start)
1340{
1341 unsigned long current;
1342 acpi_rsdp_t *rsdp;
1343 acpi_rsdt_t *rsdt;
1344 acpi_xsdt_t *xsdt;
1345 acpi_fadt_t *fadt;
1346 acpi_facs_t *facs;
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001347 acpi_header_t *slic_file, *slic;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001348 acpi_header_t *ssdt;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001349 acpi_header_t *dsdt_file, *dsdt;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001350 acpi_mcfg_t *mcfg;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +02001351 acpi_tcpa_t *tcpa;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001352 acpi_tpm2_t *tpm2;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001353 acpi_madt_t *madt;
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001354 acpi_lpit_t *lpit;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +11001355 struct device *dev;
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001356 unsigned long fw;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001357 size_t slic_size, dsdt_size;
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001358 char oem_id[6], oem_table_id[8];
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001359
1360 current = start;
1361
1362 /* Align ACPI tables to 16byte */
Aaron Durbin07a1b282015-12-10 17:07:38 -06001363 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001364
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001365 /* Special case for qemu */
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001366 fw = fw_cfg_acpi_tables(current);
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001367 if (fw) {
1368 rsdp = NULL;
1369 /* Find RSDP. */
1370 for (void *p = (void *)current; p < (void *)fw; p += 16) {
1371 if (valid_rsdp((acpi_rsdp_t *)p)) {
1372 rsdp = p;
1373 break;
1374 }
1375 }
1376 if (!rsdp)
1377 return fw;
1378
1379 /* Add BOOT0000 for Linux google firmware driver */
1380 printk(BIOS_DEBUG, "ACPI: * SSDT\n");
1381 ssdt = (acpi_header_t *)fw;
1382 current = (unsigned long)ssdt + sizeof(acpi_header_t);
1383
1384 memset((void *)ssdt, 0, sizeof(acpi_header_t));
1385
1386 memcpy(&ssdt->signature, "SSDT", 4);
1387 ssdt->revision = get_acpi_table_revision(SSDT);
1388 memcpy(&ssdt->oem_id, OEM_ID, 6);
1389 memcpy(&ssdt->oem_table_id, oem_table_id, 8);
1390 ssdt->oem_revision = 42;
1391 memcpy(&ssdt->asl_compiler_id, ASLC, 4);
1392 ssdt->asl_compiler_revision = asl_revision;
1393 ssdt->length = sizeof(acpi_header_t);
1394
1395 acpigen_set_current((char *) current);
1396
1397 /* Write object to declare coreboot tables */
1398 acpi_ssdt_write_cbtable();
1399
1400 /* (Re)calculate length and checksum. */
1401 ssdt->length = current - (unsigned long)ssdt;
1402 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
1403
1404 acpi_create_ssdt_generator(ssdt, ACPI_TABLE_CREATOR);
1405
1406 acpi_add_table(rsdp, ssdt);
1407
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001408 return fw;
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001409 }
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001410
Julius Werner834b3ec2020-03-04 16:52:08 -08001411 dsdt_file = cbfs_map(CONFIG_CBFS_PREFIX "/dsdt.aml", &dsdt_size);
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001412 if (!dsdt_file) {
1413 printk(BIOS_ERR, "No DSDT file, skipping ACPI tables\n");
1414 return current;
1415 }
1416
1417 if (dsdt_file->length > dsdt_size
Elyes HAOUAS7d87e762016-10-03 22:11:07 +02001418 || dsdt_file->length < sizeof(acpi_header_t)
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001419 || memcmp(dsdt_file->signature, "DSDT", 4) != 0) {
1420 printk(BIOS_ERR, "Invalid DSDT file, skipping ACPI tables\n");
1421 return current;
1422 }
1423
Julius Werner834b3ec2020-03-04 16:52:08 -08001424 slic_file = cbfs_map(CONFIG_CBFS_PREFIX "/slic", &slic_size);
Vladimir Serbinenko2cb29782015-05-31 11:32:09 +02001425 if (slic_file
1426 && (slic_file->length > slic_size
Elyes HAOUAS7d87e762016-10-03 22:11:07 +02001427 || slic_file->length < sizeof(acpi_header_t)
Benjamin Doron2b2dc0c2020-09-12 02:55:57 +00001428 || (memcmp(slic_file->signature, "SLIC", 4) != 0
1429 && memcmp(slic_file->signature, "MSDM", 4) != 0))) {
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001430 slic_file = 0;
1431 }
1432
1433 if (slic_file) {
1434 memcpy(oem_id, slic_file->oem_id, 6);
1435 memcpy(oem_table_id, slic_file->oem_table_id, 8);
1436 } else {
1437 memcpy(oem_id, OEM_ID, 6);
1438 memcpy(oem_table_id, ACPI_TABLE_CREATOR, 8);
1439 }
1440
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001441 printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx.\n", start);
1442
1443 /* We need at least an RSDP and an RSDT Table */
1444 rsdp = (acpi_rsdp_t *) current;
1445 current += sizeof(acpi_rsdp_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001446 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001447 rsdt = (acpi_rsdt_t *) current;
1448 current += sizeof(acpi_rsdt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001449 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001450 xsdt = (acpi_xsdt_t *) current;
1451 current += sizeof(acpi_xsdt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001452 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001453
1454 /* clear all table memory */
1455 memset((void *) start, 0, current - start);
1456
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001457 acpi_write_rsdp(rsdp, rsdt, xsdt, oem_id);
1458 acpi_write_rsdt(rsdt, oem_id, oem_table_id);
1459 acpi_write_xsdt(xsdt, oem_id, oem_table_id);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001460
1461 printk(BIOS_DEBUG, "ACPI: * FACS\n");
Felix Heldc4697122019-06-20 13:50:17 +02001462 current = ALIGN_UP(current, 64);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001463 facs = (acpi_facs_t *) current;
1464 current += sizeof(acpi_facs_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001465 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001466 acpi_create_facs(facs);
1467
1468 printk(BIOS_DEBUG, "ACPI: * DSDT\n");
1469 dsdt = (acpi_header_t *) current;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001470 memcpy(dsdt, dsdt_file, sizeof(acpi_header_t));
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001471 if (dsdt->length >= sizeof(acpi_header_t)) {
1472 current += sizeof(acpi_header_t);
1473
1474 acpigen_set_current((char *) current);
Kyösti Mälkki2ab4a962020-06-30 11:41:47 +03001475
1476 acpi_fill_gnvs();
1477
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001478 for (dev = all_devices; dev; dev = dev->next)
Nico Huber68680dd2020-03-31 17:34:52 +02001479 if (dev->ops && dev->ops->acpi_inject_dsdt)
1480 dev->ops->acpi_inject_dsdt(dev);
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001481 current = (unsigned long) acpigen_get_current();
1482 memcpy((char *)current,
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001483 (char *)dsdt_file + sizeof(acpi_header_t),
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001484 dsdt->length - sizeof(acpi_header_t));
1485 current += dsdt->length - sizeof(acpi_header_t);
1486
1487 /* (Re)calculate length and checksum. */
1488 dsdt->length = current - (unsigned long)dsdt;
1489 dsdt->checksum = 0;
1490 dsdt->checksum = acpi_checksum((void *)dsdt, dsdt->length);
1491 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001492
Aaron Durbin07a1b282015-12-10 17:07:38 -06001493 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001494
1495 printk(BIOS_DEBUG, "ACPI: * FADT\n");
1496 fadt = (acpi_fadt_t *) current;
1497 current += sizeof(acpi_fadt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001498 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001499
1500 acpi_create_fadt(fadt, facs, dsdt);
1501 acpi_add_table(rsdp, fadt);
1502
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001503 if (slic_file) {
1504 printk(BIOS_DEBUG, "ACPI: * SLIC\n");
1505 slic = (acpi_header_t *)current;
1506 memcpy(slic, slic_file, slic_file->length);
1507 current += slic_file->length;
Aaron Durbin07a1b282015-12-10 17:07:38 -06001508 current = acpi_align_current(current);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001509 acpi_add_table(rsdp, slic);
1510 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001511
1512 printk(BIOS_DEBUG, "ACPI: * SSDT\n");
1513 ssdt = (acpi_header_t *)current;
1514 acpi_create_ssdt_generator(ssdt, ACPI_TABLE_CREATOR);
Vladimir Serbinenko9310df82014-10-10 20:40:41 +02001515 if (ssdt->length > sizeof(acpi_header_t)) {
1516 current += ssdt->length;
1517 acpi_add_table(rsdp, ssdt);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001518 current = acpi_align_current(current);
Vladimir Serbinenko9310df82014-10-10 20:40:41 +02001519 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001520
1521 printk(BIOS_DEBUG, "ACPI: * MCFG\n");
1522 mcfg = (acpi_mcfg_t *) current;
1523 acpi_create_mcfg(mcfg);
1524 if (mcfg->header.length > sizeof(acpi_mcfg_t)) {
1525 current += mcfg->header.length;
Aaron Durbin07a1b282015-12-10 17:07:38 -06001526 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001527 acpi_add_table(rsdp, mcfg);
1528 }
1529
Julius Wernercd49cce2019-03-05 16:53:33 -08001530 if (CONFIG(TPM1)) {
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001531 printk(BIOS_DEBUG, "ACPI: * TCPA\n");
1532 tcpa = (acpi_tcpa_t *) current;
1533 acpi_create_tcpa(tcpa);
1534 if (tcpa->header.length >= sizeof(acpi_tcpa_t)) {
1535 current += tcpa->header.length;
1536 current = acpi_align_current(current);
1537 acpi_add_table(rsdp, tcpa);
1538 }
1539 }
1540
Julius Wernercd49cce2019-03-05 16:53:33 -08001541 if (CONFIG(TPM2)) {
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001542 printk(BIOS_DEBUG, "ACPI: * TPM2\n");
1543 tpm2 = (acpi_tpm2_t *) current;
1544 acpi_create_tpm2(tpm2);
1545 if (tpm2->header.length >= sizeof(acpi_tpm2_t)) {
1546 current += tpm2->header.length;
1547 current = acpi_align_current(current);
1548 acpi_add_table(rsdp, tpm2);
1549 }
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +02001550 }
1551
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001552 if (CONFIG(ACPI_LPIT)) {
1553 printk(BIOS_DEBUG, "ACPI: * LPIT\n");
1554
1555 lpit = (acpi_lpit_t *)current;
1556 acpi_create_lpit(lpit);
1557 if (lpit->header.length >= sizeof(acpi_lpit_t)) {
1558 current += lpit->header.length;
1559 current = acpi_align_current(current);
1560 acpi_add_table(rsdp, lpit);
1561 }
1562 }
1563
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001564 printk(BIOS_DEBUG, "ACPI: * MADT\n");
1565
1566 madt = (acpi_madt_t *) current;
1567 acpi_create_madt(madt);
1568 if (madt->header.length > sizeof(acpi_madt_t)) {
Lee Leahy024b13d2017-03-16 13:41:11 -07001569 current += madt->header.length;
1570 acpi_add_table(rsdp, madt);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001571 }
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001572
Aaron Durbin07a1b282015-12-10 17:07:38 -06001573 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001574
1575 printk(BIOS_DEBUG, "current = %lx\n", current);
1576
1577 for (dev = all_devices; dev; dev = dev->next) {
1578 if (dev->ops && dev->ops->write_acpi_tables) {
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001579 current = dev->ops->write_acpi_tables(dev, current,
1580 rsdp);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001581 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001582 }
1583 }
1584
1585 printk(BIOS_INFO, "ACPI: done.\n");
1586 return current;
1587}
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001588
Rudolf Marek33cafe52009-04-13 18:07:02 +00001589static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp)
1590{
1591 if (strncmp((char *)rsdp, RSDP_SIG, sizeof(RSDP_SIG) - 1) != 0)
1592 return NULL;
1593
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001594 printk(BIOS_DEBUG, "Looking on %p for valid checksum\n", rsdp);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001595
1596 if (acpi_checksum((void *)rsdp, 20) != 0)
1597 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001598 printk(BIOS_DEBUG, "Checksum 1 passed\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001599
1600 if ((rsdp->revision > 1) && (acpi_checksum((void *)rsdp,
1601 rsdp->length) != 0))
1602 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001603 printk(BIOS_DEBUG, "Checksum 2 passed all OK\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001604
1605 return rsdp;
1606}
1607
Rudolf Marek33cafe52009-04-13 18:07:02 +00001608void *acpi_find_wakeup_vector(void)
1609{
1610 char *p, *end;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001611 acpi_rsdt_t *rsdt;
1612 acpi_facs_t *facs;
Martin Rothc5f49262012-12-14 19:17:55 -07001613 acpi_fadt_t *fadt = NULL;
Kyösti Mälkki072d436b2016-06-17 08:44:40 +03001614 acpi_rsdp_t *rsdp = NULL;
Uwe Hermann622824c2010-11-19 15:14:42 +00001615 void *wake_vec;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001616 int i;
1617
Kyösti Mälkkib8c7ea02020-11-17 16:41:38 +02001618 if (!acpi_is_wakeup_s3())
Rudolf Marek33cafe52009-04-13 18:07:02 +00001619 return NULL;
1620
Uwe Hermann622824c2010-11-19 15:14:42 +00001621 printk(BIOS_DEBUG, "Trying to find the wakeup vector...\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001622
Uwe Hermann622824c2010-11-19 15:14:42 +00001623 /* Find RSDP. */
1624 for (p = (char *)0xe0000; p < (char *)0xfffff; p += 16) {
Lee Leahy0b5678f2017-03-16 16:01:40 -07001625 rsdp = valid_rsdp((acpi_rsdp_t *)p);
1626 if (rsdp)
Rudolf Marek33cafe52009-04-13 18:07:02 +00001627 break;
1628 }
1629
Angel Pons98a68ad2018-11-04 12:25:25 +01001630 if (rsdp == NULL) {
1631 printk(BIOS_ALERT,
1632 "No RSDP found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001633 return NULL;
Angel Pons98a68ad2018-11-04 12:25:25 +01001634 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001635
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001636 printk(BIOS_DEBUG, "RSDP found at %p\n", rsdp);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001637 rsdt = (acpi_rsdt_t *)(uintptr_t)rsdp->rsdt_address;
Stefan Reinauer14e22772010-04-27 06:56:47 +00001638
Uwe Hermann622824c2010-11-19 15:14:42 +00001639 end = (char *)rsdt + rsdt->header.length;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001640 printk(BIOS_DEBUG, "RSDT found at %p ends at %p\n", rsdt, end);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001641
Uwe Hermann622824c2010-11-19 15:14:42 +00001642 for (i = 0; ((char *)&rsdt->entry[i]) < end; i++) {
Stefan Reinauer71a30182015-07-30 16:28:13 -07001643 fadt = (acpi_fadt_t *)(uintptr_t)rsdt->entry[i];
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001644 if (strncmp((char *)fadt, "FACP", 4) == 0)
Rudolf Marek33cafe52009-04-13 18:07:02 +00001645 break;
1646 fadt = NULL;
1647 }
1648
Angel Pons98a68ad2018-11-04 12:25:25 +01001649 if (fadt == NULL) {
1650 printk(BIOS_ALERT,
1651 "No FADT found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001652 return NULL;
Angel Pons98a68ad2018-11-04 12:25:25 +01001653 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001654
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001655 printk(BIOS_DEBUG, "FADT found at %p\n", fadt);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001656 facs = (acpi_facs_t *)(uintptr_t)fadt->firmware_ctrl;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001657
Stefan Reinauer7e9771c2009-04-22 08:17:38 +00001658 if (facs == NULL) {
Angel Pons98a68ad2018-11-04 12:25:25 +01001659 printk(BIOS_ALERT,
1660 "No FACS found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001661 return NULL;
Stefan Reinauer7e9771c2009-04-22 08:17:38 +00001662 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001663
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001664 printk(BIOS_DEBUG, "FACS found at %p\n", facs);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001665 wake_vec = (void *)(uintptr_t)facs->firmware_waking_vector;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001666 printk(BIOS_DEBUG, "OS waking vector is %p\n", wake_vec);
Uwe Hermann622824c2010-11-19 15:14:42 +00001667
Rudolf Marek33cafe52009-04-13 18:07:02 +00001668 return wake_vec;
1669}
1670
Aaron Durbin64031672018-04-21 14:45:32 -06001671__weak int acpi_get_gpe(int gpe)
Duncan Laurie1f6e6812016-09-19 12:04:19 -07001672{
1673 return -1; /* implemented by SOC */
1674}
Marc Jones93a51762018-08-22 18:57:24 -06001675
1676int get_acpi_table_revision(enum acpi_tables table)
1677{
1678 switch (table) {
1679 case FADT:
Patrick Rudolphc02bda02020-02-28 10:19:41 +01001680 return ACPI_FADT_REV_ACPI_6_0;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001681 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 +01001682 return 3;
Marc Jones93a51762018-08-22 18:57:24 -06001683 case MCFG:
1684 return 1;
1685 case TCPA:
1686 return 2;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001687 case TPM2:
1688 return 4;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001689 case SSDT: /* ACPI 3.0 upto 6.3: 2 */
Marc Jones93a51762018-08-22 18:57:24 -06001690 return 2;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001691 case SRAT: /* ACPI 2.0: 1, ACPI 3.0: 2, ACPI 4.0 upto 6.3: 3 */
Marc Jones93a51762018-08-22 18:57:24 -06001692 return 1; /* TODO Should probably be upgraded to 2 */
1693 case DMAR:
1694 return 1;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001695 case SLIT: /* ACPI 2.0 upto 6.3: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06001696 return 1;
Patrick Rudolph9d98e5a2019-06-14 19:00:04 +02001697 case SPMI: /* IMPI 2.0 */
1698 return 5;
Marc Jones93a51762018-08-22 18:57:24 -06001699 case HPET: /* Currently 1. Table added in ACPI 2.0. */
1700 return 1;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001701 case VFCT: /* ACPI 2.0/3.0/4.0: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06001702 return 1;
1703 case IVRS:
Jason Glenesk1916f892020-07-24 02:51:30 -07001704 return IVRS_FORMAT_MIXED;
Marc Jones93a51762018-08-22 18:57:24 -06001705 case DBG2:
1706 return 0;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001707 case FACS: /* ACPI 2.0/3.0: 1, ACPI 4.0 upto 6.3: 2 */
Marc Jones93a51762018-08-22 18:57:24 -06001708 return 1;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001709 case RSDT: /* ACPI 1.0 upto 6.3: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06001710 return 1;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001711 case XSDT: /* ACPI 2.0 upto 6.3: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06001712 return 1;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001713 case RSDP: /* ACPI 2.0 upto 6.3: 2 */
Marc Jones93a51762018-08-22 18:57:24 -06001714 return 2;
1715 case HEST:
1716 return 1;
1717 case NHLT:
1718 return 5;
Marshall Dawson44705c62018-09-04 13:47:15 -06001719 case BERT:
1720 return 1;
Jason Glenesk61624b22020-11-02 20:06:23 -08001721 case CRAT:
1722 return 1;
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001723 case LPIT: /* ACPI 5.1 up to 6.3: 0 */
1724 return 0;
Marc Jones93a51762018-08-22 18:57:24 -06001725 default:
1726 return -1;
1727 }
1728 return -1;
1729}