blob: 6dab3733cc729382d35a8ba6e3d021eb9a034538 [file] [log] [blame]
Stefan Reinauer688b3852004-01-28 16:56:14 +00001/*
Uwe Hermannc70e9fc2010-02-15 23:10:19 +00002 * This file is part of the coreboot project.
3 *
Martin Roth9df9e9392016-01-12 15:55:28 -07004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
Martin Roth20bbd812019-08-30 21:09:37 -060012 *
13 * coreboot ACPI Table support
Stefan Reinauer777224c2005-01-19 14:06:41 +000014 */
15
Stefan Reinauer14e22772010-04-27 06:56:47 +000016/*
Stefan Reinauer777224c2005-01-19 14:06:41 +000017 * Each system port implementing ACPI has to provide two functions:
Stefan Reinauer14e22772010-04-27 06:56:47 +000018 *
Stefan Reinauer777224c2005-01-19 14:06:41 +000019 * write_acpi_tables()
20 * acpi_dump_apics()
Stefan Reinauer14e22772010-04-27 06:56:47 +000021 *
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000022 * See Kontron 986LCD-M port for a good example of an ACPI implementation
23 * in coreboot.
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000024 */
Stefan Reinauer688b3852004-01-28 16:56:14 +000025
26#include <console/console.h>
27#include <string.h>
28#include <arch/acpi.h>
Martin Rotha66df492016-09-06 10:22:34 -060029#include <arch/acpi_ivrs.h>
Rudolf Marek293b5f52009-02-01 18:35:15 +000030#include <arch/acpigen.h>
Stefan Reinauer777224c2005-01-19 14:06:41 +000031#include <device/pci.h>
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000032#include <cbmem.h>
Elyes HAOUASe2d152c2019-06-21 07:06:50 +020033#include <commonlib/helpers.h>
Patrick Georgic8feedd2012-02-16 18:43:25 +010034#include <cpu/x86/lapic_def.h>
Duncan Laurie11290c42012-10-03 19:07:05 -070035#include <cpu/cpu.h>
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +010036#include <cbfs.h>
Elyes HAOUAS26071aa2019-02-15 08:21:33 +010037#include <version.h>
Werner Zehdb561e62019-02-19 13:39:56 +010038#include <commonlib/sort.h>
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000039
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +020040static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp);
41
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000042u8 acpi_checksum(u8 *table, u32 length)
Stefan Reinauer688b3852004-01-28 16:56:14 +000043{
Uwe Hermann622824c2010-11-19 15:14:42 +000044 u8 ret = 0;
Stefan Reinauer688b3852004-01-28 16:56:14 +000045 while (length--) {
46 ret += *table;
47 table++;
48 }
Stefan Reinauera7648c22004-01-29 17:31:34 +000049 return -ret;
Stefan Reinauer688b3852004-01-28 16:56:14 +000050}
51
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000052/**
Uwe Hermann622824c2010-11-19 15:14:42 +000053 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
54 * and checksum.
Stefan Reinauer06feb882004-02-03 16:11:35 +000055 */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000056void acpi_add_table(acpi_rsdp_t *rsdp, void *table)
Stefan Reinauer688b3852004-01-28 16:56:14 +000057{
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000058 int i, entries_num;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000059 acpi_rsdt_t *rsdt;
60 acpi_xsdt_t *xsdt = NULL;
61
Uwe Hermann622824c2010-11-19 15:14:42 +000062 /* The RSDT is mandatory... */
Stefan Reinauerdefee172015-06-18 01:11:19 -070063 rsdt = (acpi_rsdt_t *)(uintptr_t)rsdp->rsdt_address;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000064
Uwe Hermann622824c2010-11-19 15:14:42 +000065 /* ...while the XSDT is not. */
66 if (rsdp->xsdt_address)
Stefan Reinauerdefee172015-06-18 01:11:19 -070067 xsdt = (acpi_xsdt_t *)((uintptr_t)rsdp->xsdt_address);
Stefan Reinauer14e22772010-04-27 06:56:47 +000068
Uwe Hermann622824c2010-11-19 15:14:42 +000069 /* This should always be MAX_ACPI_TABLES. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000070 entries_num = ARRAY_SIZE(rsdt->entry);
Stefan Reinauer14e22772010-04-27 06:56:47 +000071
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000072 for (i = 0; i < entries_num; i++) {
Uwe Hermann622824c2010-11-19 15:14:42 +000073 if (rsdt->entry[i] == 0)
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000074 break;
Stefan Reinauer688b3852004-01-28 16:56:14 +000075 }
76
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000077 if (i >= entries_num) {
Uwe Hermann622824c2010-11-19 15:14:42 +000078 printk(BIOS_ERR, "ACPI: Error: Could not add ACPI table, "
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +030079 "too many tables.\n");
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000080 return;
81 }
82
Uwe Hermann622824c2010-11-19 15:14:42 +000083 /* Add table to the RSDT. */
Stefan Reinauerdefee172015-06-18 01:11:19 -070084 rsdt->entry[i] = (uintptr_t)table;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000085
Uwe Hermann622824c2010-11-19 15:14:42 +000086 /* Fix RSDT length or the kernel will assume invalid entries. */
87 rsdt->header.length = sizeof(acpi_header_t) + (sizeof(u32) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000088
Uwe Hermann622824c2010-11-19 15:14:42 +000089 /* Re-calculate checksum. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000090 rsdt->header.checksum = 0; /* Hope this won't get optimized away */
Uwe Hermann622824c2010-11-19 15:14:42 +000091 rsdt->header.checksum = acpi_checksum((u8 *)rsdt, rsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000092
Uwe Hermann622824c2010-11-19 15:14:42 +000093 /*
94 * And now the same thing for the XSDT. We use the same index as for
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000095 * now we want the XSDT and RSDT to always be in sync in coreboot.
96 */
97 if (xsdt) {
Uwe Hermann622824c2010-11-19 15:14:42 +000098 /* Add table to the XSDT. */
Stefan Reinauerdefee172015-06-18 01:11:19 -070099 xsdt->entry[i] = (u64)(uintptr_t)table;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000100
Uwe Hermann622824c2010-11-19 15:14:42 +0000101 /* Fix XSDT length. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000102 xsdt->header.length = sizeof(acpi_header_t) +
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300103 (sizeof(u64) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000104
Uwe Hermann622824c2010-11-19 15:14:42 +0000105 /* Re-calculate checksum. */
106 xsdt->header.checksum = 0;
107 xsdt->header.checksum = acpi_checksum((u8 *)xsdt,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300108 xsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000109 }
110
Uwe Hermann622824c2010-11-19 15:14:42 +0000111 printk(BIOS_DEBUG, "ACPI: added table %d/%d, length now %d\n",
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300112 i + 1, entries_num, rsdt->header.length);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000113}
114
Uwe Hermann622824c2010-11-19 15:14:42 +0000115int acpi_create_mcfg_mmconfig(acpi_mcfg_mmconfig_t *mmconfig, u32 base,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300116 u16 seg_nr, u8 start, u8 end)
Stefan Reinauer26d431a2009-01-20 19:17:51 +0000117{
Vladimir Serbinenko60fccdc2014-10-05 11:05:16 +0200118 memset(mmconfig, 0, sizeof(*mmconfig));
Rudolf Mareke6409f22007-11-03 12:50:26 +0000119 mmconfig->base_address = base;
120 mmconfig->base_reserved = 0;
121 mmconfig->pci_segment_group_number = seg_nr;
122 mmconfig->start_bus_number = start;
123 mmconfig->end_bus_number = end;
Uwe Hermann622824c2010-11-19 15:14:42 +0000124
125 return sizeof(acpi_mcfg_mmconfig_t);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000126}
127
Stefan Reinauer777224c2005-01-19 14:06:41 +0000128int acpi_create_madt_lapic(acpi_madt_lapic_t *lapic, u8 cpu, u8 apic)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000129{
Himanshu Sahdev8c09b822019-10-21 18:50:50 +0530130 lapic->type = LOCAL_APIC; /* Local APIC structure */
Uwe Hermann622824c2010-11-19 15:14:42 +0000131 lapic->length = sizeof(acpi_madt_lapic_t);
132 lapic->flags = (1 << 0); /* Processor/LAPIC enabled */
133 lapic->processor_id = cpu;
134 lapic->apic_id = apic;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000135
Uwe Hermann622824c2010-11-19 15:14:42 +0000136 return lapic->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000137}
138
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000139unsigned long acpi_create_madt_lapics(unsigned long current)
140{
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100141 struct device *cpu;
Werner Zeh423adfb2019-03-06 09:31:02 +0100142 int index, apic_ids[CONFIG_MAX_CPUS] = {0}, num_cpus = 0;
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000143
Uwe Hermann622824c2010-11-19 15:14:42 +0000144 for (cpu = all_devices; cpu; cpu = cpu->next) {
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000145 if ((cpu->path.type != DEVICE_PATH_APIC) ||
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800146 (cpu->bus->dev->path.type != DEVICE_PATH_CPU_CLUSTER)) {
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000147 continue;
148 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000149 if (!cpu->enabled)
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000150 continue;
Werner Zehdb561e62019-02-19 13:39:56 +0100151 if (num_cpus >= ARRAY_SIZE(apic_ids))
152 break;
153 apic_ids[num_cpus++] = cpu->path.apic.apic_id;
154 }
Werner Zehfedb36e2019-03-12 07:07:50 +0100155 if (num_cpus > 1)
156 bubblesort(apic_ids, num_cpus, NUM_ASCENDING);
Werner Zehdb561e62019-02-19 13:39:56 +0100157 for (index = 0; index < num_cpus; index++) {
Uwe Hermann622824c2010-11-19 15:14:42 +0000158 current += acpi_create_madt_lapic((acpi_madt_lapic_t *)current,
Werner Zehdb561e62019-02-19 13:39:56 +0100159 index, apic_ids[index]);
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000160 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000161
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000162 return current;
163}
164
Uwe Hermann622824c2010-11-19 15:14:42 +0000165int acpi_create_madt_ioapic(acpi_madt_ioapic_t *ioapic, u8 id, u32 addr,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300166 u32 gsi_base)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000167{
Himanshu Sahdev8c09b822019-10-21 18:50:50 +0530168 ioapic->type = IO_APIC; /* I/O APIC structure */
Uwe Hermann622824c2010-11-19 15:14:42 +0000169 ioapic->length = sizeof(acpi_madt_ioapic_t);
170 ioapic->reserved = 0x00;
171 ioapic->gsi_base = gsi_base;
172 ioapic->ioapic_id = id;
173 ioapic->ioapic_addr = addr;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000174
Uwe Hermann622824c2010-11-19 15:14:42 +0000175 return ioapic->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000176}
177
Stefan Reinauer777224c2005-01-19 14:06:41 +0000178int acpi_create_madt_irqoverride(acpi_madt_irqoverride_t *irqoverride,
Stefan Reinauer06feb882004-02-03 16:11:35 +0000179 u8 bus, u8 source, u32 gsirq, u16 flags)
180{
Himanshu Sahdev8c09b822019-10-21 18:50:50 +0530181 irqoverride->type = IRQ_SOURCE_OVERRIDE; /* Interrupt source override */
Uwe Hermann622824c2010-11-19 15:14:42 +0000182 irqoverride->length = sizeof(acpi_madt_irqoverride_t);
183 irqoverride->bus = bus;
184 irqoverride->source = source;
185 irqoverride->gsirq = gsirq;
186 irqoverride->flags = flags;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000187
Uwe Hermann622824c2010-11-19 15:14:42 +0000188 return irqoverride->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000189}
190
Stefan Reinauer777224c2005-01-19 14:06:41 +0000191int acpi_create_madt_lapic_nmi(acpi_madt_lapic_nmi_t *lapic_nmi, u8 cpu,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300192 u16 flags, u8 lint)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000193{
Himanshu Sahdev8c09b822019-10-21 18:50:50 +0530194 lapic_nmi->type = LOCAL_APIC_NMI; /* Local APIC NMI structure */
Uwe Hermann622824c2010-11-19 15:14:42 +0000195 lapic_nmi->length = sizeof(acpi_madt_lapic_nmi_t);
196 lapic_nmi->flags = flags;
197 lapic_nmi->processor_id = cpu;
198 lapic_nmi->lint = lint;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000199
Uwe Hermann622824c2010-11-19 15:14:42 +0000200 return lapic_nmi->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000201}
202
Stefan Reinauer777224c2005-01-19 14:06:41 +0000203void acpi_create_madt(acpi_madt_t *madt)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000204{
Uwe Hermann622824c2010-11-19 15:14:42 +0000205 acpi_header_t *header = &(madt->header);
206 unsigned long current = (unsigned long)madt + sizeof(acpi_madt_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000207
Stefan Reinauer06feb882004-02-03 16:11:35 +0000208 memset((void *)madt, 0, sizeof(acpi_madt_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000209
John Zhao2ba303e2019-05-28 16:48:14 -0700210 if (!header)
211 return;
212
Uwe Hermann622824c2010-11-19 15:14:42 +0000213 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000214 memcpy(header->signature, "APIC", 4);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000215 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000216 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000217 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000218
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100219 header->asl_compiler_revision = asl_revision;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000220 header->length = sizeof(acpi_madt_t);
Marc Jones93a51762018-08-22 18:57:24 -0600221 header->revision = get_acpi_table_revision(MADT);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000222
Uwe Hermann622824c2010-11-19 15:14:42 +0000223 madt->lapic_addr = LOCAL_APIC_ADDR;
Nico Huber9df72e02018-11-24 18:25:50 +0100224 if (CONFIG(ACPI_HAVE_PCAT_8259))
225 madt->flags |= 1;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000226
Stefan Reinauerf622d592005-11-26 16:56:05 +0000227 current = acpi_fill_madt(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000228
Uwe Hermann622824c2010-11-19 15:14:42 +0000229 /* (Re)calculate length and checksum. */
230 header->length = current - (unsigned long)madt;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000231
Uwe Hermann622824c2010-11-19 15:14:42 +0000232 header->checksum = acpi_checksum((void *)madt, header->length);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000233}
234
Uwe Hermann622824c2010-11-19 15:14:42 +0000235/* MCFG is defined in the PCI Firmware Specification 3.0. */
Rudolf Mareke6409f22007-11-03 12:50:26 +0000236void acpi_create_mcfg(acpi_mcfg_t *mcfg)
237{
Uwe Hermann622824c2010-11-19 15:14:42 +0000238 acpi_header_t *header = &(mcfg->header);
239 unsigned long current = (unsigned long)mcfg + sizeof(acpi_mcfg_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000240
Rudolf Mareke6409f22007-11-03 12:50:26 +0000241 memset((void *)mcfg, 0, sizeof(acpi_mcfg_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000242
John Zhao2ba303e2019-05-28 16:48:14 -0700243 if (!header)
244 return;
245
Uwe Hermann622824c2010-11-19 15:14:42 +0000246 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000247 memcpy(header->signature, "MCFG", 4);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000248 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000249 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000250 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000251
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100252 header->asl_compiler_revision = asl_revision;
Rudolf Mareke6409f22007-11-03 12:50:26 +0000253 header->length = sizeof(acpi_mcfg_t);
Marc Jones93a51762018-08-22 18:57:24 -0600254 header->revision = get_acpi_table_revision(MCFG);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000255
256 current = acpi_fill_mcfg(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)mcfg;
260 header->checksum = acpi_checksum((void *)mcfg, header->length);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000261}
262
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200263static void *get_tcpa_log(u32 *size)
264{
265 const struct cbmem_entry *ce;
266 const u32 tcpa_default_log_len = 0x10000;
267 void *lasa;
Philipp Deppenwiese791ba972018-07-30 01:15:20 +0200268 ce = cbmem_entry_find(CBMEM_ID_TCPA_TCG_LOG);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200269 if (ce) {
270 lasa = cbmem_entry_start(ce);
271 *size = cbmem_entry_size(ce);
272 printk(BIOS_DEBUG, "TCPA log found at %p\n", lasa);
273 return lasa;
274 }
Philipp Deppenwiese791ba972018-07-30 01:15:20 +0200275 lasa = cbmem_add(CBMEM_ID_TCPA_TCG_LOG, tcpa_default_log_len);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200276 if (!lasa) {
277 printk(BIOS_ERR, "TCPA log creation failed\n");
278 return NULL;
279 }
280
281 printk(BIOS_DEBUG, "TCPA log created at %p\n", lasa);
Lee Leahy024b13d2017-03-16 13:41:11 -0700282 memset(lasa, 0, tcpa_default_log_len);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200283
284 *size = tcpa_default_log_len;
285 return lasa;
286}
287
288static void acpi_create_tcpa(acpi_tcpa_t *tcpa)
289{
290 acpi_header_t *header = &(tcpa->header);
291 u32 tcpa_log_len;
292 void *lasa;
293
294 memset((void *)tcpa, 0, sizeof(acpi_tcpa_t));
295
296 lasa = get_tcpa_log(&tcpa_log_len);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700297 if (!lasa)
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200298 return;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200299
John Zhao2ba303e2019-05-28 16:48:14 -0700300 if (!header)
301 return;
302
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200303 /* Fill out header fields. */
304 memcpy(header->signature, "TCPA", 4);
305 memcpy(header->oem_id, OEM_ID, 6);
306 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
307 memcpy(header->asl_compiler_id, ASLC, 4);
308
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100309 header->asl_compiler_revision = asl_revision;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200310 header->length = sizeof(acpi_tcpa_t);
Marc Jones93a51762018-08-22 18:57:24 -0600311 header->revision = get_acpi_table_revision(TCPA);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200312
313 tcpa->platform_class = 0;
314 tcpa->laml = tcpa_log_len;
Stefan Reinauerdefee172015-06-18 01:11:19 -0700315 tcpa->lasa = (uintptr_t) lasa;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200316
317 /* Calculate checksum. */
318 header->checksum = acpi_checksum((void *)tcpa, header->length);
319}
320
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100321static void *get_tpm2_log(u32 *size)
322{
323 const struct cbmem_entry *ce;
324 const u32 tpm2_default_log_len = 0x10000;
325 void *lasa;
326 ce = cbmem_entry_find(CBMEM_ID_TPM2_TCG_LOG);
327 if (ce) {
328 lasa = cbmem_entry_start(ce);
329 *size = cbmem_entry_size(ce);
330 printk(BIOS_DEBUG, "TPM2 log found at %p\n", lasa);
331 return lasa;
332 }
333 lasa = cbmem_add(CBMEM_ID_TPM2_TCG_LOG, tpm2_default_log_len);
334 if (!lasa) {
335 printk(BIOS_ERR, "TPM2 log creation failed\n");
336 return NULL;
337 }
338
339 printk(BIOS_DEBUG, "TPM2 log created at %p\n", lasa);
340 memset(lasa, 0, tpm2_default_log_len);
341
342 *size = tpm2_default_log_len;
343 return lasa;
344}
345
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200346static void acpi_create_tpm2(acpi_tpm2_t *tpm2)
347{
348 acpi_header_t *header = &(tpm2->header);
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100349 u32 tpm2_log_len;
350 void *lasa;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200351
352 memset((void *)tpm2, 0, sizeof(acpi_tpm2_t));
353
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100354 /*
355 * Some payloads like SeaBIOS depend on log area to use TPM2.
356 * Get the memory size and address of TPM2 log area or initialize it.
357 */
358 lasa = get_tpm2_log(&tpm2_log_len);
359 if (!lasa)
360 tpm2_log_len = 0;
361
John Zhao2ba303e2019-05-28 16:48:14 -0700362 if (!header)
363 return;
364
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200365 /* Fill out header fields. */
366 memcpy(header->signature, "TPM2", 4);
367 memcpy(header->oem_id, OEM_ID, 6);
368 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
369 memcpy(header->asl_compiler_id, ASLC, 4);
370
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100371 header->asl_compiler_revision = asl_revision;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200372 header->length = sizeof(acpi_tpm2_t);
373 header->revision = get_acpi_table_revision(TPM2);
374
375 /* Hard to detect for coreboot. Just set it to 0 */
376 tpm2->platform_class = 0;
Christian Walter26e0d4c2019-07-14 15:47:23 +0200377 if (CONFIG(CRB_TPM)) {
378 /* Must be set to 7 for CRB Support */
379 tpm2->control_area = CONFIG_CRB_TPM_BASE_ADDRESS + 0x40;
380 tpm2->start_method = 7;
381 } else {
382 /* Must be set to 0 for FIFO interface support */
383 tpm2->control_area = 0;
384 tpm2->start_method = 6;
385 }
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200386 memset(tpm2->msp, 0, sizeof(tpm2->msp));
387
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100388 /* Fill the log area size and start address fields. */
389 tpm2->laml = tpm2_log_len;
390 tpm2->lasa = (uintptr_t) lasa;
391
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200392 /* Calculate checksum. */
393 header->checksum = acpi_checksum((void *)tpm2, header->length);
394}
395
Duncan Laurie37319032016-05-26 12:47:05 -0700396static void acpi_ssdt_write_cbtable(void)
397{
398 const struct cbmem_entry *cbtable;
399 uintptr_t base;
400 uint32_t size;
401
402 cbtable = cbmem_entry_find(CBMEM_ID_CBTABLE);
403 if (!cbtable)
404 return;
405 base = (uintptr_t)cbmem_entry_start(cbtable);
406 size = cbmem_entry_size(cbtable);
407
408 acpigen_write_device("CTBL");
409 acpigen_write_coreboot_hid(COREBOOT_ACPI_ID_CBTABLE);
410 acpigen_write_name_integer("_UID", 0);
David Wu5dff3962018-03-21 16:48:53 +0800411 acpigen_write_STA(ACPI_STATUS_DEVICE_HIDDEN_ON);
Duncan Laurie37319032016-05-26 12:47:05 -0700412 acpigen_write_name("_CRS");
413 acpigen_write_resourcetemplate_header();
414 acpigen_write_mem32fixed(0, base, size);
415 acpigen_write_resourcetemplate_footer();
416 acpigen_pop_len();
417}
418
Myles Watson3fe6b702009-10-09 20:13:43 +0000419void acpi_create_ssdt_generator(acpi_header_t *ssdt, const char *oem_table_id)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000420{
Uwe Hermann622824c2010-11-19 15:14:42 +0000421 unsigned long current = (unsigned long)ssdt + sizeof(acpi_header_t);
422
Rudolf Marek293b5f52009-02-01 18:35:15 +0000423 memset((void *)ssdt, 0, sizeof(acpi_header_t));
Uwe Hermann622824c2010-11-19 15:14:42 +0000424
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000425 memcpy(&ssdt->signature, "SSDT", 4);
Marc Jones93a51762018-08-22 18:57:24 -0600426 ssdt->revision = get_acpi_table_revision(SSDT);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000427 memcpy(&ssdt->oem_id, OEM_ID, 6);
428 memcpy(&ssdt->oem_table_id, oem_table_id, 8);
429 ssdt->oem_revision = 42;
Uwe Hermann622824c2010-11-19 15:14:42 +0000430 memcpy(&ssdt->asl_compiler_id, ASLC, 4);
Elyes HAOUAS26071aa2019-02-15 08:21:33 +0100431 ssdt->asl_compiler_revision = asl_revision;
Rudolf Marek293b5f52009-02-01 18:35:15 +0000432 ssdt->length = sizeof(acpi_header_t);
433
Stefan Reinauer8dcd50b2009-03-06 17:24:29 +0000434 acpigen_set_current((char *) current);
Duncan Laurie37319032016-05-26 12:47:05 -0700435
436 /* Write object to declare coreboot tables */
437 acpi_ssdt_write_cbtable();
438
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200439 {
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100440 struct device *dev;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200441 for (dev = all_devices; dev; dev = dev->next)
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700442 if (dev->ops && dev->ops->acpi_fill_ssdt_generator)
Alexander Couzens5eea4582015-04-12 22:18:55 +0200443 dev->ops->acpi_fill_ssdt_generator(dev);
Vladimir Serbinenko0a669912014-10-05 14:34:17 +0200444 current = (unsigned long) acpigen_get_current();
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200445 }
Rudolf Marek293b5f52009-02-01 18:35:15 +0000446
Uwe Hermann622824c2010-11-19 15:14:42 +0000447 /* (Re)calculate length and checksum. */
Rudolf Marek293b5f52009-02-01 18:35:15 +0000448 ssdt->length = current - (unsigned long)ssdt;
449 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
450}
451
Stefan Reinauerf622d592005-11-26 16:56:05 +0000452int acpi_create_srat_lapic(acpi_srat_lapic_t *lapic, u8 node, u8 apic)
453{
Myles Watson0b4c9f02009-03-10 18:06:47 +0000454 memset((void *)lapic, 0, sizeof(acpi_srat_lapic_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000455
Uwe Hermann622824c2010-11-19 15:14:42 +0000456 lapic->type = 0; /* Processor local APIC/SAPIC affinity structure */
457 lapic->length = sizeof(acpi_srat_lapic_t);
458 lapic->flags = (1 << 0); /* Enabled (the use of this structure). */
459 lapic->proximity_domain_7_0 = node;
460 /* TODO: proximity_domain_31_8, local SAPIC EID, clock domain. */
461 lapic->apic_id = apic;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000462
Uwe Hermann622824c2010-11-19 15:14:42 +0000463 return lapic->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000464}
465
Uwe Hermann622824c2010-11-19 15:14:42 +0000466int 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 +0300467 u32 flags)
Stefan Reinauerf622d592005-11-26 16:56:05 +0000468{
Uwe Hermann622824c2010-11-19 15:14:42 +0000469 mem->type = 1; /* Memory affinity structure */
470 mem->length = sizeof(acpi_srat_mem_t);
471 mem->base_address_low = (basek << 10);
472 mem->base_address_high = (basek >> (32 - 10));
473 mem->length_low = (sizek << 10);
474 mem->length_high = (sizek >> (32 - 10));
475 mem->proximity_domain = node;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000476 mem->flags = flags;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000477
Uwe Hermann622824c2010-11-19 15:14:42 +0000478 return mem->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000479}
480
Uwe Hermann622824c2010-11-19 15:14:42 +0000481/* http://www.microsoft.com/whdc/system/sysinternals/sratdwn.mspx */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +0200482void acpi_create_srat(acpi_srat_t *srat,
483 unsigned long (*acpi_fill_srat)(unsigned long current))
Stefan Reinauerf622d592005-11-26 16:56:05 +0000484{
Uwe Hermann622824c2010-11-19 15:14:42 +0000485 acpi_header_t *header = &(srat->header);
486 unsigned long current = (unsigned long)srat + sizeof(acpi_srat_t);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000487
Uwe Hermann622824c2010-11-19 15:14:42 +0000488 memset((void *)srat, 0, sizeof(acpi_srat_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000489
John Zhao2ba303e2019-05-28 16:48:14 -0700490 if (!header)
491 return;
492
Uwe Hermann622824c2010-11-19 15:14:42 +0000493 /* Fill out header fields. */
494 memcpy(header->signature, "SRAT", 4);
495 memcpy(header->oem_id, OEM_ID, 6);
496 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
497 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000498
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100499 header->asl_compiler_revision = asl_revision;
Uwe Hermann622824c2010-11-19 15:14:42 +0000500 header->length = sizeof(acpi_srat_t);
Marc Jones93a51762018-08-22 18:57:24 -0600501 header->revision = get_acpi_table_revision(SRAT);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000502
Uwe Hermann622824c2010-11-19 15:14:42 +0000503 srat->resv = 1; /* Spec: Reserved to 1 for backwards compatibility. */
Stefan Reinauerf622d592005-11-26 16:56:05 +0000504
Uwe Hermann622824c2010-11-19 15:14:42 +0000505 current = acpi_fill_srat(current);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000506
Uwe Hermann622824c2010-11-19 15:14:42 +0000507 /* (Re)calculate length and checksum. */
508 header->length = current - (unsigned long)srat;
509 header->checksum = acpi_checksum((void *)srat, header->length);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000510}
511
Nico Hubere561f352015-10-26 11:51:25 +0100512void acpi_create_dmar(acpi_dmar_t *dmar, enum dmar_flags flags,
Lee Leahy024b13d2017-03-16 13:41:11 -0700513 unsigned long (*acpi_fill_dmar)(unsigned long))
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200514{
515 acpi_header_t *header = &(dmar->header);
516 unsigned long current = (unsigned long)dmar + sizeof(acpi_dmar_t);
517
518 memset((void *)dmar, 0, sizeof(acpi_dmar_t));
519
John Zhao2ba303e2019-05-28 16:48:14 -0700520 if (!header)
521 return;
522
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200523 /* Fill out header fields. */
524 memcpy(header->signature, "DMAR", 4);
525 memcpy(header->oem_id, OEM_ID, 6);
526 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
527 memcpy(header->asl_compiler_id, ASLC, 4);
528
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100529 header->asl_compiler_revision = asl_revision;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200530 header->length = sizeof(acpi_dmar_t);
Marc Jones93a51762018-08-22 18:57:24 -0600531 header->revision = get_acpi_table_revision(DMAR);
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200532
Jacob Laskaf3f654d2016-03-15 21:53:27 -0500533 dmar->host_address_width = cpu_phys_address_size() - 1;
Nico Hubere561f352015-10-26 11:51:25 +0100534 dmar->flags = flags;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200535
536 current = acpi_fill_dmar(current);
537
538 /* (Re)calculate length and checksum. */
539 header->length = current - (unsigned long)dmar;
540 header->checksum = acpi_checksum((void *)dmar, header->length);
541}
542
543unsigned long acpi_create_dmar_drhd(unsigned long current, u8 flags,
Matt DeVillier7866d492018-03-29 14:59:57 +0200544 u16 segment, u64 bar)
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200545{
546 dmar_entry_t *drhd = (dmar_entry_t *)current;
547 memset(drhd, 0, sizeof(*drhd));
548 drhd->type = DMAR_DRHD;
549 drhd->length = sizeof(*drhd); /* will be fixed up later */
550 drhd->flags = flags;
551 drhd->segment = segment;
552 drhd->bar = bar;
553
554 return drhd->length;
555}
556
Matt DeVillier7866d492018-03-29 14:59:57 +0200557unsigned long acpi_create_dmar_rmrr(unsigned long current, u16 segment,
558 u64 bar, u64 limit)
559{
560 dmar_rmrr_entry_t *rmrr = (dmar_rmrr_entry_t *)current;
561 memset(rmrr, 0, sizeof(*rmrr));
562 rmrr->type = DMAR_RMRR;
563 rmrr->length = sizeof(*rmrr); /* will be fixed up later */
564 rmrr->segment = segment;
565 rmrr->bar = bar;
566 rmrr->limit = limit;
567
568 return rmrr->length;
569}
570
Werner Zehd4d76952016-07-27 06:56:36 +0200571unsigned long acpi_create_dmar_atsr(unsigned long current, u8 flags,
572 u16 segment)
573{
574 dmar_atsr_entry_t *atsr = (dmar_atsr_entry_t *)current;
575 memset(atsr, 0, sizeof(*atsr));
576 atsr->type = DMAR_ATSR;
577 atsr->length = sizeof(*atsr); /* will be fixed up later */
578 atsr->flags = flags;
579 atsr->segment = segment;
580
581 return atsr->length;
582}
583
John Zhao76e70672019-04-04 11:03:28 -0700584unsigned long acpi_create_dmar_rhsa(unsigned long current, u64 base_addr,
585 u32 proximity_domain)
586{
587 dmar_rhsa_entry_t *rhsa = (dmar_rhsa_entry_t *)current;
588 memset(rhsa, 0, sizeof(*rhsa));
589 rhsa->type = DMAR_RHSA;
590 rhsa->length = sizeof(*rhsa);
591 rhsa->base_address = base_addr;
592 rhsa->proximity_domain = proximity_domain;
593
594 return rhsa->length;
595}
596
597unsigned long acpi_create_dmar_andd(unsigned long current, u8 device_number,
598 const char *device_name)
599{
600 dmar_andd_entry_t *andd = (dmar_andd_entry_t *)current;
601 int andd_len = sizeof(dmar_andd_entry_t) + strlen(device_name) + 1;
602 memset(andd, 0, andd_len);
603 andd->type = DMAR_ANDD;
604 andd->length = andd_len;
605 andd->device_number = device_number;
606 memcpy(&andd->device_name, device_name, strlen(device_name));
607
608 return andd->length;
609}
610
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200611void acpi_dmar_drhd_fixup(unsigned long base, unsigned long current)
612{
613 dmar_entry_t *drhd = (dmar_entry_t *)base;
614 drhd->length = current - base;
615}
616
Matt DeVillier7866d492018-03-29 14:59:57 +0200617void acpi_dmar_rmrr_fixup(unsigned long base, unsigned long current)
618{
619 dmar_rmrr_entry_t *rmrr = (dmar_rmrr_entry_t *)base;
620 rmrr->length = current - base;
621}
622
Werner Zehd4d76952016-07-27 06:56:36 +0200623void acpi_dmar_atsr_fixup(unsigned long base, unsigned long current)
624{
625 dmar_atsr_entry_t *atsr = (dmar_atsr_entry_t *)base;
626 atsr->length = current - base;
627}
628
Matt DeVillier7866d492018-03-29 14:59:57 +0200629static unsigned long acpi_create_dmar_ds(unsigned long current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100630 enum dev_scope_type type, u8 enumeration_id, u8 bus, u8 dev, u8 fn)
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200631{
Nico Huber6c4751d2015-10-26 12:03:54 +0100632 /* we don't support longer paths yet */
633 const size_t dev_scope_length = sizeof(dev_scope_t) + 2;
634
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200635 dev_scope_t *ds = (dev_scope_t *)current;
Nico Huber6c4751d2015-10-26 12:03:54 +0100636 memset(ds, 0, dev_scope_length);
637 ds->type = type;
638 ds->length = dev_scope_length;
639 ds->enumeration = enumeration_id;
640 ds->start_bus = bus;
641 ds->path[0].dev = dev;
642 ds->path[0].fn = fn;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200643
644 return ds->length;
645}
646
Matt DeVillier7866d492018-03-29 14:59:57 +0200647unsigned long acpi_create_dmar_ds_pci_br(unsigned long current, u8 bus,
Werner Zeh21a5bff2016-07-27 07:07:20 +0200648 u8 dev, u8 fn)
649{
Matt DeVillier7866d492018-03-29 14:59:57 +0200650 return acpi_create_dmar_ds(current,
Werner Zeh21a5bff2016-07-27 07:07:20 +0200651 SCOPE_PCI_SUB, 0, bus, dev, fn);
652}
653
Matt DeVillier7866d492018-03-29 14:59:57 +0200654unsigned long acpi_create_dmar_ds_pci(unsigned long current, u8 bus,
Nico Huber6c4751d2015-10-26 12:03:54 +0100655 u8 dev, u8 fn)
656{
Matt DeVillier7866d492018-03-29 14:59:57 +0200657 return acpi_create_dmar_ds(current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100658 SCOPE_PCI_ENDPOINT, 0, bus, dev, fn);
659}
660
Matt DeVillier7866d492018-03-29 14:59:57 +0200661unsigned long acpi_create_dmar_ds_ioapic(unsigned long current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100662 u8 enumeration_id, u8 bus, u8 dev, u8 fn)
663{
Matt DeVillier7866d492018-03-29 14:59:57 +0200664 return acpi_create_dmar_ds(current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100665 SCOPE_IOAPIC, enumeration_id, bus, dev, fn);
666}
667
Matt DeVillier7866d492018-03-29 14:59:57 +0200668unsigned long acpi_create_dmar_ds_msi_hpet(unsigned long current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100669 u8 enumeration_id, u8 bus, u8 dev, u8 fn)
670{
Matt DeVillier7866d492018-03-29 14:59:57 +0200671 return acpi_create_dmar_ds(current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100672 SCOPE_MSI_HPET, enumeration_id, bus, dev, fn);
673}
674
Uwe Hermann622824c2010-11-19 15:14:42 +0000675/* http://h21007.www2.hp.com/portal/download/files/unprot/Itanium/slit.pdf */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +0200676void acpi_create_slit(acpi_slit_t *slit,
677 unsigned long (*acpi_fill_slit)(unsigned long current))
Yinghai Lud4b278c2006-10-04 20:46:15 +0000678{
Uwe Hermann622824c2010-11-19 15:14:42 +0000679 acpi_header_t *header = &(slit->header);
680 unsigned long current = (unsigned long)slit + sizeof(acpi_slit_t);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000681
Uwe Hermann622824c2010-11-19 15:14:42 +0000682 memset((void *)slit, 0, sizeof(acpi_slit_t));
Yinghai Lud4b278c2006-10-04 20:46:15 +0000683
John Zhao2ba303e2019-05-28 16:48:14 -0700684 if (!header)
685 return;
686
Uwe Hermann622824c2010-11-19 15:14:42 +0000687 /* Fill out header fields. */
688 memcpy(header->signature, "SLIT", 4);
689 memcpy(header->oem_id, OEM_ID, 6);
690 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
691 memcpy(header->asl_compiler_id, ASLC, 4);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000692
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100693 header->asl_compiler_revision = asl_revision;
Uwe Hermann622824c2010-11-19 15:14:42 +0000694 header->length = sizeof(acpi_slit_t);
Marc Jones93a51762018-08-22 18:57:24 -0600695 header->revision = get_acpi_table_revision(SLIT);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000696
Uwe Hermann622824c2010-11-19 15:14:42 +0000697 current = acpi_fill_slit(current);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000698
Uwe Hermann622824c2010-11-19 15:14:42 +0000699 /* (Re)calculate length and checksum. */
700 header->length = current - (unsigned long)slit;
701 header->checksum = acpi_checksum((void *)slit, header->length);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000702}
703
Uwe Hermann622824c2010-11-19 15:14:42 +0000704/* http://www.intel.com/hardwaredesign/hpetspec_1.pdf */
Stefan Reinauer777224c2005-01-19 14:06:41 +0000705void acpi_create_hpet(acpi_hpet_t *hpet)
Stefan Reinauer688b3852004-01-28 16:56:14 +0000706{
Uwe Hermann622824c2010-11-19 15:14:42 +0000707 acpi_header_t *header = &(hpet->header);
708 acpi_addr_t *addr = &(hpet->addr);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000709
Stefan Reinauera7648c22004-01-29 17:31:34 +0000710 memset((void *)hpet, 0, sizeof(acpi_hpet_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000711
John Zhao2ba303e2019-05-28 16:48:14 -0700712 if (!header)
713 return;
714
Uwe Hermann622824c2010-11-19 15:14:42 +0000715 /* Fill out header fields. */
Stefan Reinauer4704dc52009-07-22 01:11:37 +0000716 memcpy(header->signature, "HPET", 4);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000717 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000718 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000719 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000720
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100721 header->asl_compiler_revision = asl_revision;
Stefan Reinauer688b3852004-01-28 16:56:14 +0000722 header->length = sizeof(acpi_hpet_t);
Marc Jones93a51762018-08-22 18:57:24 -0600723 header->revision = get_acpi_table_revision(HPET);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000724
Uwe Hermann622824c2010-11-19 15:14:42 +0000725 /* Fill out HPET address. */
726 addr->space_id = 0; /* Memory */
727 addr->bit_width = 64;
728 addr->bit_offset = 0;
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200729 addr->addrl = CONFIG_HPET_ADDRESS & 0xffffffff;
730 addr->addrh = ((unsigned long long)CONFIG_HPET_ADDRESS) >> 32;
Stefan Reinauer688b3852004-01-28 16:56:14 +0000731
Lee Leahy024b13d2017-03-16 13:41:11 -0700732 hpet->id = *(unsigned int *)CONFIG_HPET_ADDRESS;
Uwe Hermann622824c2010-11-19 15:14:42 +0000733 hpet->number = 0;
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200734 hpet->min_tick = CONFIG_HPET_MIN_TICKS;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000735
Uwe Hermann622824c2010-11-19 15:14:42 +0000736 header->checksum = acpi_checksum((void *)hpet, sizeof(acpi_hpet_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000737}
Uwe Hermann622824c2010-11-19 15:14:42 +0000738
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200739void acpi_create_vfct(struct device *device,
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530740 acpi_vfct_t *vfct,
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700741 unsigned long (*acpi_fill_vfct)(struct device *device,
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530742 acpi_vfct_t *vfct_struct, unsigned long current))
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200743{
744 acpi_header_t *header = &(vfct->header);
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530745 unsigned long current = (unsigned long)vfct + sizeof(acpi_vfct_t);
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200746
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530747 memset((void *)vfct, 0, sizeof(acpi_vfct_t));
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200748
John Zhao2ba303e2019-05-28 16:48:14 -0700749 if (!header)
750 return;
751
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200752 /* Fill out header fields. */
753 memcpy(header->signature, "VFCT", 4);
754 memcpy(header->oem_id, OEM_ID, 6);
755 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
756 memcpy(header->asl_compiler_id, ASLC, 4);
757
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100758 header->asl_compiler_revision = asl_revision;
Marc Jones93a51762018-08-22 18:57:24 -0600759 header->revision = get_acpi_table_revision(VFCT);
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200760
761 current = acpi_fill_vfct(device, vfct, current);
762
Kyösti Mälkkifb493792019-06-30 06:29:52 +0300763 /* If no BIOS image, return with header->length == 0. */
764 if (!vfct->VBIOSImageOffset)
765 return;
766
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200767 /* (Re)calculate length and checksum. */
768 header->length = current - (unsigned long)vfct;
769 header->checksum = acpi_checksum((void *)vfct, header->length);
770}
771
Patrick Rudolph9d98e5a2019-06-14 19:00:04 +0200772void acpi_create_ipmi(struct device *device,
773 struct acpi_spmi *spmi,
774 const u16 ipmi_revision,
775 const acpi_addr_t *addr,
776 const enum acpi_ipmi_interface_type type,
777 const s8 gpe_interrupt,
778 const u32 apic_interrupt,
779 const u32 uid)
780{
781 acpi_header_t *header = &(spmi->header);
782 memset((void *)spmi, 0, sizeof(struct acpi_spmi));
783
784 /* Fill out header fields. */
785 memcpy(header->signature, "SPMI", 4);
786 memcpy(header->oem_id, OEM_ID, 6);
787 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
788 memcpy(header->asl_compiler_id, ASLC, 4);
789
790 header->asl_compiler_revision = asl_revision;
791 header->length = sizeof(struct acpi_spmi);
792 header->revision = get_acpi_table_revision(SPMI);
793
794 spmi->reserved = 1;
795
796 if (device->path.type == DEVICE_PATH_PCI) {
797 spmi->pci_device_flag = ACPI_IPMI_PCI_DEVICE_FLAG;
798 spmi->pci_bus = device->bus->secondary;
799 spmi->pci_device = device->path.pci.devfn >> 3;
800 spmi->pci_function = device->path.pci.devfn & 0x7;
801 } else if (type != IPMI_INTERFACE_SSIF) {
802 memcpy(spmi->uid, &uid, sizeof(spmi->uid));
803 }
804
805 spmi->base_address = *addr;
806 spmi->specification_revision = ipmi_revision;
807
808 spmi->interface_type = type;
809
810 if (gpe_interrupt >= 0 && gpe_interrupt < 32) {
811 spmi->gpe = gpe_interrupt;
812 spmi->interrupt_type |= ACPI_IPMI_INT_TYPE_SCI;
813 }
814 if (apic_interrupt > 0) {
815 spmi->global_system_interrupt = apic_interrupt;
816 spmi->interrupt_type |= ACPI_IPMI_INT_TYPE_APIC;
817 }
818
819 /* Calculate checksum. */
820 header->checksum = acpi_checksum((void *)spmi, header->length);
821}
822
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500823void acpi_create_ivrs(acpi_ivrs_t *ivrs,
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700824 unsigned long (*acpi_fill_ivrs)(acpi_ivrs_t *ivrs_struct,
825 unsigned long current))
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500826{
827 acpi_header_t *header = &(ivrs->header);
828 unsigned long current = (unsigned long)ivrs + sizeof(acpi_ivrs_t);
829
830 memset((void *)ivrs, 0, sizeof(acpi_ivrs_t));
831
John Zhao2ba303e2019-05-28 16:48:14 -0700832 if (!header)
833 return;
834
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500835 /* Fill out header fields. */
836 memcpy(header->signature, "IVRS", 4);
837 memcpy(header->oem_id, OEM_ID, 6);
838 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
839 memcpy(header->asl_compiler_id, ASLC, 4);
840
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100841 header->asl_compiler_revision = asl_revision;
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500842 header->length = sizeof(acpi_ivrs_t);
Marc Jones93a51762018-08-22 18:57:24 -0600843 header->revision = get_acpi_table_revision(IVRS);
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500844
845 current = acpi_fill_ivrs(ivrs, current);
846
847 /* (Re)calculate length and checksum. */
848 header->length = current - (unsigned long)ivrs;
849 header->checksum = acpi_checksum((void *)ivrs, header->length);
850}
851
Elyes HAOUAS9dd89cd2018-05-04 17:56:47 +0200852unsigned long acpi_write_hpet(struct device *device, unsigned long current,
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700853 acpi_rsdp_t *rsdp)
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200854{
855 acpi_hpet_t *hpet;
856
857 /*
858 * We explicitly add these tables later on:
859 */
860 printk(BIOS_DEBUG, "ACPI: * HPET\n");
861
862 hpet = (acpi_hpet_t *) current;
863 current += sizeof(acpi_hpet_t);
Felix Heldc4697122019-06-20 13:50:17 +0200864 current = ALIGN_UP(current, 16);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200865 acpi_create_hpet(hpet);
866 acpi_add_table(rsdp, hpet);
867
868 return current;
869}
870
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800871void acpi_create_dbg2(acpi_dbg2_header_t *dbg2,
872 int port_type, int port_subtype,
873 acpi_addr_t *address, uint32_t address_size,
874 const char *device_path)
875{
876 uintptr_t current;
877 acpi_dbg2_device_t *device;
878 uint32_t *dbg2_addr_size;
879 acpi_header_t *header;
880 size_t path_len;
881 const char *path;
882 char *namespace;
883
884 /* Fill out header fields. */
885 current = (uintptr_t)dbg2;
886 memset(dbg2, 0, sizeof(acpi_dbg2_header_t));
887 header = &(dbg2->header);
John Zhao2ba303e2019-05-28 16:48:14 -0700888
889 if (!header)
890 return;
891
Marc Jones93a51762018-08-22 18:57:24 -0600892 header->revision = get_acpi_table_revision(DBG2);
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800893 memcpy(header->signature, "DBG2", 4);
894 memcpy(header->oem_id, OEM_ID, 6);
895 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
896 memcpy(header->asl_compiler_id, ASLC, 4);
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100897 header->asl_compiler_revision = asl_revision;
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800898
899 /* One debug device defined */
900 dbg2->devices_offset = sizeof(acpi_dbg2_header_t);
901 dbg2->devices_count = 1;
902 current += sizeof(acpi_dbg2_header_t);
903
904 /* Device comes after the header */
905 device = (acpi_dbg2_device_t *)current;
906 memset(device, 0, sizeof(acpi_dbg2_device_t));
907 current += sizeof(acpi_dbg2_device_t);
908
909 device->revision = 0;
910 device->address_count = 1;
911 device->port_type = port_type;
912 device->port_subtype = port_subtype;
913
914 /* Base Address comes after device structure */
915 memcpy((void *)current, address, sizeof(acpi_addr_t));
916 device->base_address_offset = current - (uintptr_t)device;
917 current += sizeof(acpi_addr_t);
918
919 /* Address Size comes after address structure */
920 dbg2_addr_size = (uint32_t *)current;
921 device->address_size_offset = current - (uintptr_t)device;
922 *dbg2_addr_size = address_size;
923 current += sizeof(uint32_t);
924
925 /* Namespace string comes last, use '.' if not provided */
926 path = device_path ? : ".";
927 /* Namespace string length includes NULL terminator */
928 path_len = strlen(path) + 1;
929 namespace = (char *)current;
930 device->namespace_string_length = path_len;
931 device->namespace_string_offset = current - (uintptr_t)device;
932 strncpy(namespace, path, path_len);
933 current += path_len;
934
935 /* Update structure lengths and checksum */
936 device->length = current - (uintptr_t)device;
937 header->length = current - (uintptr_t)dbg2;
938 header->checksum = acpi_checksum((uint8_t *)dbg2, header->length);
939}
940
941unsigned long acpi_write_dbg2_pci_uart(acpi_rsdp_t *rsdp, unsigned long current,
Aamir Bohrae825d3f2019-07-30 10:11:41 +0530942 const struct device *dev, uint8_t access_size)
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800943{
944 acpi_dbg2_header_t *dbg2 = (acpi_dbg2_header_t *)current;
945 struct resource *res;
946 acpi_addr_t address;
947
948 if (!dev) {
Wim Vervoorn6cd52432020-02-03 14:41:46 +0100949 printk(BIOS_DEBUG, "%s: Device not found\n", __func__);
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800950 return current;
951 }
Mario Scheithauerf1eb0ea2017-11-21 13:52:53 +0100952 if (!dev->enabled) {
953 printk(BIOS_INFO, "%s: Device not enabled\n", __func__);
954 return current;
955 }
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800956 res = find_resource(dev, PCI_BASE_ADDRESS_0);
957 if (!res) {
958 printk(BIOS_ERR, "%s: Unable to find resource for %s\n",
959 __func__, dev_path(dev));
960 return current;
961 }
962
963 memset(&address, 0, sizeof(address));
964 if (res->flags & IORESOURCE_IO)
965 address.space_id = ACPI_ADDRESS_SPACE_IO;
966 else if (res->flags & IORESOURCE_MEM)
967 address.space_id = ACPI_ADDRESS_SPACE_MEMORY;
968 else {
969 printk(BIOS_ERR, "%s: Unknown address space type\n", __func__);
970 return current;
971 }
972
973 address.addrl = (uint32_t)res->base;
974 address.addrh = (uint32_t)((res->base >> 32) & 0xffffffff);
975 address.access_size = access_size;
976
977 acpi_create_dbg2(dbg2,
978 ACPI_DBG2_PORT_SERIAL,
979 ACPI_DBG2_PORT_SERIAL_16550,
980 &address, res->size,
981 acpi_device_path(dev));
982
983 if (dbg2->header.length) {
984 current += dbg2->header.length;
985 current = acpi_align_current(current);
986 acpi_add_table(rsdp, dbg2);
987 }
988
989 return current;
990}
991
Stefan Reinauer777224c2005-01-19 14:06:41 +0000992void acpi_create_facs(acpi_facs_t *facs)
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000993{
Uwe Hermann622824c2010-11-19 15:14:42 +0000994 memset((void *)facs, 0, sizeof(acpi_facs_t));
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000995
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000996 memcpy(facs->signature, "FACS", 4);
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000997 facs->length = sizeof(acpi_facs_t);
998 facs->hardware_signature = 0;
999 facs->firmware_waking_vector = 0;
1000 facs->global_lock = 0;
1001 facs->flags = 0;
1002 facs->x_firmware_waking_vector_l = 0;
1003 facs->x_firmware_waking_vector_h = 0;
Marc Jones93a51762018-08-22 18:57:24 -06001004 facs->version = get_acpi_table_revision(FACS);
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +00001005}
Stefan Reinauer777224c2005-01-19 14:06:41 +00001006
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001007static void acpi_write_rsdt(acpi_rsdt_t *rsdt, char *oem_id, char *oem_table_id)
Stefan Reinauer14e22772010-04-27 06:56:47 +00001008{
Uwe Hermann622824c2010-11-19 15:14:42 +00001009 acpi_header_t *header = &(rsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001010
John Zhao2ba303e2019-05-28 16:48:14 -07001011 if (!header)
1012 return;
1013
Uwe Hermann622824c2010-11-19 15:14:42 +00001014 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001015 memcpy(header->signature, "RSDT", 4);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001016 memcpy(header->oem_id, oem_id, 6);
1017 memcpy(header->oem_table_id, oem_table_id, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +00001018 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001019
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001020 header->asl_compiler_revision = asl_revision;
Stefan Reinauer688b3852004-01-28 16:56:14 +00001021 header->length = sizeof(acpi_rsdt_t);
Marc Jones93a51762018-08-22 18:57:24 -06001022 header->revision = get_acpi_table_revision(RSDT);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001023
Uwe Hermann622824c2010-11-19 15:14:42 +00001024 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauer688b3852004-01-28 16:56:14 +00001025
Uwe Hermann622824c2010-11-19 15:14:42 +00001026 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001027 header->checksum = acpi_checksum((void *)rsdt, sizeof(acpi_rsdt_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +00001028}
1029
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001030static void acpi_write_xsdt(acpi_xsdt_t *xsdt, char *oem_id, char *oem_table_id)
Stefan Reinauer14e22772010-04-27 06:56:47 +00001031{
Uwe Hermann622824c2010-11-19 15:14:42 +00001032 acpi_header_t *header = &(xsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001033
John Zhao2ba303e2019-05-28 16:48:14 -07001034 if (!header)
1035 return;
1036
Uwe Hermann622824c2010-11-19 15:14:42 +00001037 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001038 memcpy(header->signature, "XSDT", 4);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001039 memcpy(header->oem_id, oem_id, 6);
1040 memcpy(header->oem_table_id, oem_table_id, 8);
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001041 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001042
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001043 header->asl_compiler_revision = asl_revision;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001044 header->length = sizeof(acpi_xsdt_t);
Marc Jones93a51762018-08-22 18:57:24 -06001045 header->revision = get_acpi_table_revision(XSDT);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001046
Uwe Hermann622824c2010-11-19 15:14:42 +00001047 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001048
Uwe Hermann622824c2010-11-19 15:14:42 +00001049 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001050 header->checksum = acpi_checksum((void *)xsdt, sizeof(acpi_xsdt_t));
1051}
1052
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001053static void acpi_write_rsdp(acpi_rsdp_t *rsdp, acpi_rsdt_t *rsdt,
1054 acpi_xsdt_t *xsdt, char *oem_id)
Stefan Reinauer688b3852004-01-28 16:56:14 +00001055{
Stefan Reinauerd18faac2009-11-05 18:06:43 +00001056 memset(rsdp, 0, sizeof(acpi_rsdp_t));
Uwe Hermann622824c2010-11-19 15:14:42 +00001057
Stefan Reinauer688b3852004-01-28 16:56:14 +00001058 memcpy(rsdp->signature, RSDP_SIG, 8);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001059 memcpy(rsdp->oem_id, oem_id, 6);
Uwe Hermann622824c2010-11-19 15:14:42 +00001060
1061 rsdp->length = sizeof(acpi_rsdp_t);
Stefan Reinauerdefee172015-06-18 01:11:19 -07001062 rsdp->rsdt_address = (uintptr_t)rsdt;
Uwe Hermann622824c2010-11-19 15:14:42 +00001063
1064 /*
1065 * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2.
1066 *
1067 * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
1068 * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
1069 * revision 0).
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001070 */
1071 if (xsdt == NULL) {
Uwe Hermann622824c2010-11-19 15:14:42 +00001072 rsdp->revision = 0;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001073 } else {
Stefan Reinauerdefee172015-06-18 01:11:19 -07001074 rsdp->xsdt_address = (u64)(uintptr_t)xsdt;
Marc Jones93a51762018-08-22 18:57:24 -06001075 rsdp->revision = get_acpi_table_revision(RSDP);
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001076 }
Uwe Hermann622824c2010-11-19 15:14:42 +00001077
1078 /* Calculate checksums. */
1079 rsdp->checksum = acpi_checksum((void *)rsdp, 20);
1080 rsdp->ext_checksum = acpi_checksum((void *)rsdp, sizeof(acpi_rsdp_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +00001081}
1082
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001083unsigned long acpi_create_hest_error_source(acpi_hest_t *hest,
1084 acpi_hest_esd_t *esd, u16 type, void *data, u16 data_len)
zbaocaf494c82012-04-13 13:57:14 +08001085{
1086 acpi_header_t *header = &(hest->header);
1087 acpi_hest_hen_t *hen;
1088 void *pos;
1089 u16 len;
1090
1091 pos = esd;
1092 memset(pos, 0, sizeof(acpi_hest_esd_t));
1093 len = 0;
1094 esd->type = type; /* MCE */
1095 esd->source_id = hest->error_source_count;
1096 esd->flags = 0; /* FIRMWARE_FIRST */
1097 esd->enabled = 1;
1098 esd->prealloc_erecords = 1;
1099 esd->max_section_per_record = 0x1;
1100
1101 len += sizeof(acpi_hest_esd_t);
1102 pos = esd + 1;
1103
1104 switch (type) {
1105 case 0: /* MCE */
1106 break;
1107 case 1: /* CMC */
1108 hen = (acpi_hest_hen_t *) (pos);
1109 memset(pos, 0, sizeof(acpi_hest_hen_t));
1110 hen->type = 3; /* SCI? */
1111 hen->length = sizeof(acpi_hest_hen_t);
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001112 hen->conf_we = 0; /* Configuration Write Enable. */
zbaocaf494c82012-04-13 13:57:14 +08001113 hen->poll_interval = 0;
1114 hen->vector = 0;
1115 hen->sw2poll_threshold_val = 0;
1116 hen->sw2poll_threshold_win = 0;
1117 hen->error_threshold_val = 0;
1118 hen->error_threshold_win = 0;
1119 len += sizeof(acpi_hest_hen_t);
1120 pos = hen + 1;
1121 break;
1122 case 2: /* NMI */
1123 case 6: /* AER Root Port */
1124 case 7: /* AER Endpoint */
1125 case 8: /* AER Bridge */
1126 case 9: /* Generic Hardware Error Source. */
1127 /* TODO: */
1128 break;
1129 default:
1130 printk(BIOS_DEBUG, "Invalid type of Error Source.");
1131 break;
1132 }
Lee Leahy024b13d2017-03-16 13:41:11 -07001133 hest->error_source_count++;
zbaocaf494c82012-04-13 13:57:14 +08001134
1135 memcpy(pos, data, data_len);
1136 len += data_len;
John Zhao2ba303e2019-05-28 16:48:14 -07001137 if (header)
1138 header->length += len;
zbaocaf494c82012-04-13 13:57:14 +08001139
1140 return len;
1141}
1142
1143/* ACPI 4.0 */
Vladimir Serbinenko807127f2014-11-09 13:36:18 +01001144void acpi_write_hest(acpi_hest_t *hest,
1145 unsigned long (*acpi_fill_hest)(acpi_hest_t *hest))
zbaocaf494c82012-04-13 13:57:14 +08001146{
1147 acpi_header_t *header = &(hest->header);
1148
1149 memset(hest, 0, sizeof(acpi_hest_t));
1150
John Zhao2ba303e2019-05-28 16:48:14 -07001151 if (!header)
1152 return;
1153
zbaocaf494c82012-04-13 13:57:14 +08001154 memcpy(header->signature, "HEST", 4);
1155 memcpy(header->oem_id, OEM_ID, 6);
1156 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1157 memcpy(header->asl_compiler_id, ASLC, 4);
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001158 header->asl_compiler_revision = asl_revision;
zbaocaf494c82012-04-13 13:57:14 +08001159 header->length += sizeof(acpi_hest_t);
Marc Jones93a51762018-08-22 18:57:24 -06001160 header->revision = get_acpi_table_revision(HEST);
zbaocaf494c82012-04-13 13:57:14 +08001161
1162 acpi_fill_hest(hest);
1163
1164 /* Calculate checksums. */
1165 header->checksum = acpi_checksum((void *)hest, header->length);
1166}
1167
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001168/* ACPI 3.0b */
1169void acpi_write_bert(acpi_bert_t *bert, uintptr_t region, size_t length)
1170{
1171 acpi_header_t *header = &(bert->header);
1172
1173 memset(bert, 0, sizeof(acpi_bert_t));
1174
John Zhao2ba303e2019-05-28 16:48:14 -07001175 if (!header)
1176 return;
1177
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001178 memcpy(header->signature, "BERT", 4);
1179 memcpy(header->oem_id, OEM_ID, 6);
1180 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1181 memcpy(header->asl_compiler_id, ASLC, 4);
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001182 header->asl_compiler_revision = asl_revision;
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001183 header->length += sizeof(acpi_bert_t);
1184 header->revision = get_acpi_table_revision(BERT);
1185
1186 bert->error_region = region;
1187 bert->region_length = length;
1188
1189 /* Calculate checksums. */
1190 header->checksum = acpi_checksum((void *)bert, header->length);
1191}
1192
Julius Wernercd49cce2019-03-05 16:53:33 -08001193#if CONFIG(COMMON_FADT)
Lee Leahy024b13d2017-03-16 13:41:11 -07001194void acpi_create_fadt(acpi_fadt_t *fadt, acpi_facs_t *facs, void *dsdt)
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001195{
1196 acpi_header_t *header = &(fadt->header);
1197
1198 memset((void *) fadt, 0, sizeof(acpi_fadt_t));
John Zhao2ba303e2019-05-28 16:48:14 -07001199
1200 if (!header)
1201 return;
1202
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001203 memcpy(header->signature, "FACP", 4);
1204 header->length = sizeof(acpi_fadt_t);
Marc Jones93a51762018-08-22 18:57:24 -06001205 header->revision = get_acpi_table_revision(FADT);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001206 memcpy(header->oem_id, OEM_ID, 6);
1207 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1208 memcpy(header->asl_compiler_id, ASLC, 4);
Elyes HAOUAS26071aa2019-02-15 08:21:33 +01001209 header->asl_compiler_revision = asl_revision;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001210
1211 fadt->firmware_ctrl = (unsigned long) facs;
1212 fadt->dsdt = (unsigned long) dsdt;
1213
1214 fadt->x_firmware_ctl_l = (unsigned long)facs;
1215 fadt->x_firmware_ctl_h = 0;
1216 fadt->x_dsdt_l = (unsigned long)dsdt;
1217 fadt->x_dsdt_h = 0;
1218
Julius Wernercd49cce2019-03-05 16:53:33 -08001219 if (CONFIG(SYSTEM_TYPE_CONVERTIBLE) ||
1220 CONFIG(SYSTEM_TYPE_LAPTOP))
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001221 fadt->preferred_pm_profile = PM_MOBILE;
Julius Wernercd49cce2019-03-05 16:53:33 -08001222 else if (CONFIG(SYSTEM_TYPE_DETACHABLE) ||
1223 CONFIG(SYSTEM_TYPE_TABLET))
Duncan Lauriefa9c6f12019-02-07 11:30:06 -08001224 fadt->preferred_pm_profile = PM_TABLET;
Lee Leahy9c7c6f72017-03-16 11:24:09 -07001225 else
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001226 fadt->preferred_pm_profile = PM_DESKTOP;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001227
Vladimir Serbinenkoab83ef02014-10-25 15:18:25 +02001228 acpi_fill_fadt(fadt);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001229
1230 header->checksum =
1231 acpi_checksum((void *) fadt, header->length);
1232}
1233#endif
1234
Aaron Durbin64031672018-04-21 14:45:32 -06001235unsigned long __weak fw_cfg_acpi_tables(unsigned long start)
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001236{
1237 return 0;
1238}
1239
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001240unsigned long write_acpi_tables(unsigned long start)
1241{
1242 unsigned long current;
1243 acpi_rsdp_t *rsdp;
1244 acpi_rsdt_t *rsdt;
1245 acpi_xsdt_t *xsdt;
1246 acpi_fadt_t *fadt;
1247 acpi_facs_t *facs;
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001248 acpi_header_t *slic_file, *slic;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001249 acpi_header_t *ssdt;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001250 acpi_header_t *dsdt_file, *dsdt;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001251 acpi_mcfg_t *mcfg;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +02001252 acpi_tcpa_t *tcpa;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001253 acpi_tpm2_t *tpm2;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001254 acpi_madt_t *madt;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +11001255 struct device *dev;
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001256 unsigned long fw;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001257 size_t slic_size, dsdt_size;
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001258 char oem_id[6], oem_table_id[8];
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001259
1260 current = start;
1261
1262 /* Align ACPI tables to 16byte */
Aaron Durbin07a1b282015-12-10 17:07:38 -06001263 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001264
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001265 /* Special case for qemu */
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001266 fw = fw_cfg_acpi_tables(current);
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001267 if (fw) {
1268 rsdp = NULL;
1269 /* Find RSDP. */
1270 for (void *p = (void *)current; p < (void *)fw; p += 16) {
1271 if (valid_rsdp((acpi_rsdp_t *)p)) {
1272 rsdp = p;
1273 break;
1274 }
1275 }
1276 if (!rsdp)
1277 return fw;
1278
1279 /* Add BOOT0000 for Linux google firmware driver */
1280 printk(BIOS_DEBUG, "ACPI: * SSDT\n");
1281 ssdt = (acpi_header_t *)fw;
1282 current = (unsigned long)ssdt + sizeof(acpi_header_t);
1283
1284 memset((void *)ssdt, 0, sizeof(acpi_header_t));
1285
1286 memcpy(&ssdt->signature, "SSDT", 4);
1287 ssdt->revision = get_acpi_table_revision(SSDT);
1288 memcpy(&ssdt->oem_id, OEM_ID, 6);
1289 memcpy(&ssdt->oem_table_id, oem_table_id, 8);
1290 ssdt->oem_revision = 42;
1291 memcpy(&ssdt->asl_compiler_id, ASLC, 4);
1292 ssdt->asl_compiler_revision = asl_revision;
1293 ssdt->length = sizeof(acpi_header_t);
1294
1295 acpigen_set_current((char *) current);
1296
1297 /* Write object to declare coreboot tables */
1298 acpi_ssdt_write_cbtable();
1299
1300 /* (Re)calculate length and checksum. */
1301 ssdt->length = current - (unsigned long)ssdt;
1302 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
1303
1304 acpi_create_ssdt_generator(ssdt, ACPI_TABLE_CREATOR);
1305
1306 acpi_add_table(rsdp, ssdt);
1307
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001308 return fw;
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001309 }
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001310
Vladimir Serbinenkoa4cf83d2015-06-02 21:40:29 +02001311 dsdt_file = cbfs_boot_map_with_leak(
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001312 CONFIG_CBFS_PREFIX "/dsdt.aml",
1313 CBFS_TYPE_RAW, &dsdt_size);
1314 if (!dsdt_file) {
1315 printk(BIOS_ERR, "No DSDT file, skipping ACPI tables\n");
1316 return current;
1317 }
1318
1319 if (dsdt_file->length > dsdt_size
Elyes HAOUAS7d87e762016-10-03 22:11:07 +02001320 || dsdt_file->length < sizeof(acpi_header_t)
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001321 || memcmp(dsdt_file->signature, "DSDT", 4) != 0) {
1322 printk(BIOS_ERR, "Invalid DSDT file, skipping ACPI tables\n");
1323 return current;
1324 }
1325
Aaron Durbin899d13d2015-05-15 23:39:23 -05001326 slic_file = cbfs_boot_map_with_leak(CONFIG_CBFS_PREFIX "/slic",
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001327 CBFS_TYPE_RAW, &slic_size);
Vladimir Serbinenko2cb29782015-05-31 11:32:09 +02001328 if (slic_file
1329 && (slic_file->length > slic_size
Elyes HAOUAS7d87e762016-10-03 22:11:07 +02001330 || slic_file->length < sizeof(acpi_header_t)
Vladimir Serbinenko2cb29782015-05-31 11:32:09 +02001331 || memcmp(slic_file->signature, "SLIC", 4) != 0)) {
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001332 slic_file = 0;
1333 }
1334
1335 if (slic_file) {
1336 memcpy(oem_id, slic_file->oem_id, 6);
1337 memcpy(oem_table_id, slic_file->oem_table_id, 8);
1338 } else {
1339 memcpy(oem_id, OEM_ID, 6);
1340 memcpy(oem_table_id, ACPI_TABLE_CREATOR, 8);
1341 }
1342
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001343 printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx.\n", start);
1344
1345 /* We need at least an RSDP and an RSDT Table */
1346 rsdp = (acpi_rsdp_t *) current;
1347 current += sizeof(acpi_rsdp_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001348 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001349 rsdt = (acpi_rsdt_t *) current;
1350 current += sizeof(acpi_rsdt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001351 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001352 xsdt = (acpi_xsdt_t *) current;
1353 current += sizeof(acpi_xsdt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001354 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001355
1356 /* clear all table memory */
1357 memset((void *) start, 0, current - start);
1358
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001359 acpi_write_rsdp(rsdp, rsdt, xsdt, oem_id);
1360 acpi_write_rsdt(rsdt, oem_id, oem_table_id);
1361 acpi_write_xsdt(xsdt, oem_id, oem_table_id);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001362
1363 printk(BIOS_DEBUG, "ACPI: * FACS\n");
Felix Heldc4697122019-06-20 13:50:17 +02001364 current = ALIGN_UP(current, 64);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001365 facs = (acpi_facs_t *) current;
1366 current += sizeof(acpi_facs_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001367 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001368 acpi_create_facs(facs);
1369
1370 printk(BIOS_DEBUG, "ACPI: * DSDT\n");
1371 dsdt = (acpi_header_t *) current;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001372 memcpy(dsdt, dsdt_file, sizeof(acpi_header_t));
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001373 if (dsdt->length >= sizeof(acpi_header_t)) {
1374 current += sizeof(acpi_header_t);
1375
1376 acpigen_set_current((char *) current);
1377 for (dev = all_devices; dev; dev = dev->next)
Lee Leahy9c7c6f72017-03-16 11:24:09 -07001378 if (dev->ops && dev->ops->acpi_inject_dsdt_generator)
Alexander Couzensa90dad12015-04-12 21:49:46 +02001379 dev->ops->acpi_inject_dsdt_generator(dev);
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001380 current = (unsigned long) acpigen_get_current();
1381 memcpy((char *)current,
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001382 (char *)dsdt_file + sizeof(acpi_header_t),
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001383 dsdt->length - sizeof(acpi_header_t));
1384 current += dsdt->length - sizeof(acpi_header_t);
1385
1386 /* (Re)calculate length and checksum. */
1387 dsdt->length = current - (unsigned long)dsdt;
1388 dsdt->checksum = 0;
1389 dsdt->checksum = acpi_checksum((void *)dsdt, dsdt->length);
1390 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001391
Aaron Durbin07a1b282015-12-10 17:07:38 -06001392 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001393
1394 printk(BIOS_DEBUG, "ACPI: * FADT\n");
1395 fadt = (acpi_fadt_t *) current;
1396 current += sizeof(acpi_fadt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001397 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001398
1399 acpi_create_fadt(fadt, facs, dsdt);
1400 acpi_add_table(rsdp, fadt);
1401
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001402 if (slic_file) {
1403 printk(BIOS_DEBUG, "ACPI: * SLIC\n");
1404 slic = (acpi_header_t *)current;
1405 memcpy(slic, slic_file, slic_file->length);
1406 current += slic_file->length;
Aaron Durbin07a1b282015-12-10 17:07:38 -06001407 current = acpi_align_current(current);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001408 acpi_add_table(rsdp, slic);
1409 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001410
1411 printk(BIOS_DEBUG, "ACPI: * SSDT\n");
1412 ssdt = (acpi_header_t *)current;
1413 acpi_create_ssdt_generator(ssdt, ACPI_TABLE_CREATOR);
Vladimir Serbinenko9310df82014-10-10 20:40:41 +02001414 if (ssdt->length > sizeof(acpi_header_t)) {
1415 current += ssdt->length;
1416 acpi_add_table(rsdp, ssdt);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001417 current = acpi_align_current(current);
Vladimir Serbinenko9310df82014-10-10 20:40:41 +02001418 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001419
1420 printk(BIOS_DEBUG, "ACPI: * MCFG\n");
1421 mcfg = (acpi_mcfg_t *) current;
1422 acpi_create_mcfg(mcfg);
1423 if (mcfg->header.length > sizeof(acpi_mcfg_t)) {
1424 current += mcfg->header.length;
Aaron Durbin07a1b282015-12-10 17:07:38 -06001425 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001426 acpi_add_table(rsdp, mcfg);
1427 }
1428
Julius Wernercd49cce2019-03-05 16:53:33 -08001429 if (CONFIG(TPM1)) {
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001430 printk(BIOS_DEBUG, "ACPI: * TCPA\n");
1431 tcpa = (acpi_tcpa_t *) current;
1432 acpi_create_tcpa(tcpa);
1433 if (tcpa->header.length >= sizeof(acpi_tcpa_t)) {
1434 current += tcpa->header.length;
1435 current = acpi_align_current(current);
1436 acpi_add_table(rsdp, tcpa);
1437 }
1438 }
1439
Julius Wernercd49cce2019-03-05 16:53:33 -08001440 if (CONFIG(TPM2)) {
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001441 printk(BIOS_DEBUG, "ACPI: * TPM2\n");
1442 tpm2 = (acpi_tpm2_t *) current;
1443 acpi_create_tpm2(tpm2);
1444 if (tpm2->header.length >= sizeof(acpi_tpm2_t)) {
1445 current += tpm2->header.length;
1446 current = acpi_align_current(current);
1447 acpi_add_table(rsdp, tpm2);
1448 }
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +02001449 }
1450
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001451 printk(BIOS_DEBUG, "ACPI: * MADT\n");
1452
1453 madt = (acpi_madt_t *) current;
1454 acpi_create_madt(madt);
1455 if (madt->header.length > sizeof(acpi_madt_t)) {
Lee Leahy024b13d2017-03-16 13:41:11 -07001456 current += madt->header.length;
1457 acpi_add_table(rsdp, madt);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001458 }
Aaron Durbin07a1b282015-12-10 17:07:38 -06001459 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001460
1461 printk(BIOS_DEBUG, "current = %lx\n", current);
1462
1463 for (dev = all_devices; dev; dev = dev->next) {
1464 if (dev->ops && dev->ops->write_acpi_tables) {
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001465 current = dev->ops->write_acpi_tables(dev, current,
1466 rsdp);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001467 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001468 }
1469 }
1470
1471 printk(BIOS_INFO, "ACPI: done.\n");
1472 return current;
1473}
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001474
Rudolf Marek33cafe52009-04-13 18:07:02 +00001475static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp)
1476{
1477 if (strncmp((char *)rsdp, RSDP_SIG, sizeof(RSDP_SIG) - 1) != 0)
1478 return NULL;
1479
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001480 printk(BIOS_DEBUG, "Looking on %p for valid checksum\n", rsdp);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001481
1482 if (acpi_checksum((void *)rsdp, 20) != 0)
1483 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001484 printk(BIOS_DEBUG, "Checksum 1 passed\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001485
1486 if ((rsdp->revision > 1) && (acpi_checksum((void *)rsdp,
1487 rsdp->length) != 0))
1488 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001489 printk(BIOS_DEBUG, "Checksum 2 passed all OK\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001490
1491 return rsdp;
1492}
1493
Rudolf Marek33cafe52009-04-13 18:07:02 +00001494void *acpi_find_wakeup_vector(void)
1495{
1496 char *p, *end;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001497 acpi_rsdt_t *rsdt;
1498 acpi_facs_t *facs;
Martin Rothc5f49262012-12-14 19:17:55 -07001499 acpi_fadt_t *fadt = NULL;
Kyösti Mälkki072d436b2016-06-17 08:44:40 +03001500 acpi_rsdp_t *rsdp = NULL;
Uwe Hermann622824c2010-11-19 15:14:42 +00001501 void *wake_vec;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001502 int i;
1503
Rudolf Marek33cafe52009-04-13 18:07:02 +00001504 if (!acpi_is_wakeup())
1505 return NULL;
1506
Uwe Hermann622824c2010-11-19 15:14:42 +00001507 printk(BIOS_DEBUG, "Trying to find the wakeup vector...\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001508
Uwe Hermann622824c2010-11-19 15:14:42 +00001509 /* Find RSDP. */
1510 for (p = (char *)0xe0000; p < (char *)0xfffff; p += 16) {
Lee Leahy0b5678f2017-03-16 16:01:40 -07001511 rsdp = valid_rsdp((acpi_rsdp_t *)p);
1512 if (rsdp)
Rudolf Marek33cafe52009-04-13 18:07:02 +00001513 break;
1514 }
1515
Angel Pons98a68ad2018-11-04 12:25:25 +01001516 if (rsdp == NULL) {
1517 printk(BIOS_ALERT,
1518 "No RSDP found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001519 return NULL;
Angel Pons98a68ad2018-11-04 12:25:25 +01001520 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001521
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001522 printk(BIOS_DEBUG, "RSDP found at %p\n", rsdp);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001523 rsdt = (acpi_rsdt_t *)(uintptr_t)rsdp->rsdt_address;
Stefan Reinauer14e22772010-04-27 06:56:47 +00001524
Uwe Hermann622824c2010-11-19 15:14:42 +00001525 end = (char *)rsdt + rsdt->header.length;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001526 printk(BIOS_DEBUG, "RSDT found at %p ends at %p\n", rsdt, end);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001527
Uwe Hermann622824c2010-11-19 15:14:42 +00001528 for (i = 0; ((char *)&rsdt->entry[i]) < end; i++) {
Stefan Reinauer71a30182015-07-30 16:28:13 -07001529 fadt = (acpi_fadt_t *)(uintptr_t)rsdt->entry[i];
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001530 if (strncmp((char *)fadt, "FACP", 4) == 0)
Rudolf Marek33cafe52009-04-13 18:07:02 +00001531 break;
1532 fadt = NULL;
1533 }
1534
Angel Pons98a68ad2018-11-04 12:25:25 +01001535 if (fadt == NULL) {
1536 printk(BIOS_ALERT,
1537 "No FADT found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001538 return NULL;
Angel Pons98a68ad2018-11-04 12:25:25 +01001539 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001540
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001541 printk(BIOS_DEBUG, "FADT found at %p\n", fadt);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001542 facs = (acpi_facs_t *)(uintptr_t)fadt->firmware_ctrl;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001543
Stefan Reinauer7e9771c2009-04-22 08:17:38 +00001544 if (facs == NULL) {
Angel Pons98a68ad2018-11-04 12:25:25 +01001545 printk(BIOS_ALERT,
1546 "No FACS found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001547 return NULL;
Stefan Reinauer7e9771c2009-04-22 08:17:38 +00001548 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001549
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001550 printk(BIOS_DEBUG, "FACS found at %p\n", facs);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001551 wake_vec = (void *)(uintptr_t)facs->firmware_waking_vector;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001552 printk(BIOS_DEBUG, "OS waking vector is %p\n", wake_vec);
Uwe Hermann622824c2010-11-19 15:14:42 +00001553
Rudolf Marek33cafe52009-04-13 18:07:02 +00001554 return wake_vec;
1555}
1556
Aaron Durbin64031672018-04-21 14:45:32 -06001557__weak int acpi_get_gpe(int gpe)
Duncan Laurie1f6e6812016-09-19 12:04:19 -07001558{
1559 return -1; /* implemented by SOC */
1560}
Marc Jones93a51762018-08-22 18:57:24 -06001561
1562int get_acpi_table_revision(enum acpi_tables table)
1563{
1564 switch (table) {
1565 case FADT:
1566 return ACPI_FADT_REV_ACPI_3_0;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001567 case MADT: /* ACPI 3.0: 2, ACPI 4.0/5.0: 3, ACPI 6.2b/6.3: 5 */
Marc Jones4ddd4762018-08-21 10:50:57 -06001568 return 2;
Marc Jones93a51762018-08-22 18:57:24 -06001569 case MCFG:
1570 return 1;
1571 case TCPA:
1572 return 2;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001573 case TPM2:
1574 return 4;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001575 case SSDT: /* ACPI 3.0 upto 6.3: 2 */
Marc Jones93a51762018-08-22 18:57:24 -06001576 return 2;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001577 case SRAT: /* ACPI 2.0: 1, ACPI 3.0: 2, ACPI 4.0 upto 6.3: 3 */
Marc Jones93a51762018-08-22 18:57:24 -06001578 return 1; /* TODO Should probably be upgraded to 2 */
1579 case DMAR:
1580 return 1;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001581 case SLIT: /* ACPI 2.0 upto 6.3: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06001582 return 1;
Patrick Rudolph9d98e5a2019-06-14 19:00:04 +02001583 case SPMI: /* IMPI 2.0 */
1584 return 5;
Marc Jones93a51762018-08-22 18:57:24 -06001585 case HPET: /* Currently 1. Table added in ACPI 2.0. */
1586 return 1;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001587 case VFCT: /* ACPI 2.0/3.0/4.0: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06001588 return 1;
1589 case IVRS:
1590 return IVRS_FORMAT_FIXED;
1591 case DBG2:
1592 return 0;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001593 case FACS: /* ACPI 2.0/3.0: 1, ACPI 4.0 upto 6.3: 2 */
Marc Jones93a51762018-08-22 18:57:24 -06001594 return 1;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001595 case RSDT: /* ACPI 1.0 upto 6.3: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06001596 return 1;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001597 case XSDT: /* ACPI 2.0 upto 6.3: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06001598 return 1;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001599 case RSDP: /* ACPI 2.0 upto 6.3: 2 */
Marc Jones93a51762018-08-22 18:57:24 -06001600 return 2;
1601 case HEST:
1602 return 1;
1603 case NHLT:
1604 return 5;
Marshall Dawson44705c62018-09-04 13:47:15 -06001605 case BERT:
1606 return 1;
Marc Jones93a51762018-08-22 18:57:24 -06001607 default:
1608 return -1;
1609 }
1610 return -1;
1611}