blob: 3b33f1ba7f7b7e0b84cdb373a621da174f8cb45e [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 *
Stefan Reinauerf8ee1802008-01-18 15:08:58 +00004 * coreboot ACPI Table support
Stefan Reinauer688b3852004-01-28 16:56:14 +00005 * written by Stefan Reinauer <stepan@openbios.org>
Uwe Hermannc70e9fc2010-02-15 23:10:19 +00006 *
7 * Copyright (C) 2004 SUSE LINUX AG
8 * Copyright (C) 2005-2009 coresystems GmbH
Lee Leahy6f80ccc2017-03-16 15:18:22 -07009 * Copyright (C) 2015 Timothy Pearson <tpearson@raptorengineeringinc.com>,
10 * Raptor Engineering
Mario Scheithauerf1eb0ea2017-11-21 13:52:53 +010011 * Copyright (C) 2016-2017 Siemens AG
Stefan Reinauer777224c2005-01-19 14:06:41 +000012 *
Stefan Reinauer14e22772010-04-27 06:56:47 +000013 * ACPI FADT, FACS, and DSDT table support added by
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000014 * Nick Barker <nick.barker9@btinternet.com>, and those portions
Uwe Hermannc70e9fc2010-02-15 23:10:19 +000015 * Copyright (C) 2004 Nick Barker
Stefan Reinauerf622d592005-11-26 16:56:05 +000016 *
Uwe Hermannc70e9fc2010-02-15 23:10:19 +000017 * Copyright (C) 2005 ADVANCED MICRO DEVICES, INC. All Rights Reserved.
Stefan Reinauerd6edf7a2006-01-05 00:19:52 +000018 * 2005.9 yhlu add SRAT table generation
Martin Roth9df9e9392016-01-12 15:55:28 -070019 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; version 2 of the License.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
Stefan Reinauer777224c2005-01-19 14:06:41 +000028 */
29
Stefan Reinauer14e22772010-04-27 06:56:47 +000030/*
Stefan Reinauer777224c2005-01-19 14:06:41 +000031 * Each system port implementing ACPI has to provide two functions:
Stefan Reinauer14e22772010-04-27 06:56:47 +000032 *
Stefan Reinauer777224c2005-01-19 14:06:41 +000033 * write_acpi_tables()
34 * acpi_dump_apics()
Stefan Reinauer14e22772010-04-27 06:56:47 +000035 *
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000036 * See Kontron 986LCD-M port for a good example of an ACPI implementation
37 * in coreboot.
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000038 */
Stefan Reinauer688b3852004-01-28 16:56:14 +000039
40#include <console/console.h>
41#include <string.h>
42#include <arch/acpi.h>
Martin Rotha66df492016-09-06 10:22:34 -060043#include <arch/acpi_ivrs.h>
Rudolf Marek293b5f52009-02-01 18:35:15 +000044#include <arch/acpigen.h>
Stefan Reinauer777224c2005-01-19 14:06:41 +000045#include <device/pci.h>
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000046#include <cbmem.h>
Patrick Georgic8feedd2012-02-16 18:43:25 +010047#include <cpu/x86/lapic_def.h>
Duncan Laurie11290c42012-10-03 19:07:05 -070048#include <cpu/cpu.h>
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +010049#include <cbfs.h>
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000050
51u8 acpi_checksum(u8 *table, u32 length)
Stefan Reinauer688b3852004-01-28 16:56:14 +000052{
Uwe Hermann622824c2010-11-19 15:14:42 +000053 u8 ret = 0;
Stefan Reinauer688b3852004-01-28 16:56:14 +000054 while (length--) {
55 ret += *table;
56 table++;
57 }
Stefan Reinauera7648c22004-01-29 17:31:34 +000058 return -ret;
Stefan Reinauer688b3852004-01-28 16:56:14 +000059}
60
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000061/**
Uwe Hermann622824c2010-11-19 15:14:42 +000062 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
63 * and checksum.
Stefan Reinauer06feb882004-02-03 16:11:35 +000064 */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000065void acpi_add_table(acpi_rsdp_t *rsdp, void *table)
Stefan Reinauer688b3852004-01-28 16:56:14 +000066{
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000067 int i, entries_num;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000068 acpi_rsdt_t *rsdt;
69 acpi_xsdt_t *xsdt = NULL;
70
Uwe Hermann622824c2010-11-19 15:14:42 +000071 /* The RSDT is mandatory... */
Stefan Reinauerdefee172015-06-18 01:11:19 -070072 rsdt = (acpi_rsdt_t *)(uintptr_t)rsdp->rsdt_address;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000073
Uwe Hermann622824c2010-11-19 15:14:42 +000074 /* ...while the XSDT is not. */
75 if (rsdp->xsdt_address)
Stefan Reinauerdefee172015-06-18 01:11:19 -070076 xsdt = (acpi_xsdt_t *)((uintptr_t)rsdp->xsdt_address);
Stefan Reinauer14e22772010-04-27 06:56:47 +000077
Uwe Hermann622824c2010-11-19 15:14:42 +000078 /* This should always be MAX_ACPI_TABLES. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000079 entries_num = ARRAY_SIZE(rsdt->entry);
Stefan Reinauer14e22772010-04-27 06:56:47 +000080
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000081 for (i = 0; i < entries_num; i++) {
Uwe Hermann622824c2010-11-19 15:14:42 +000082 if (rsdt->entry[i] == 0)
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000083 break;
Stefan Reinauer688b3852004-01-28 16:56:14 +000084 }
85
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000086 if (i >= entries_num) {
Uwe Hermann622824c2010-11-19 15:14:42 +000087 printk(BIOS_ERR, "ACPI: Error: Could not add ACPI table, "
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +030088 "too many tables.\n");
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000089 return;
90 }
91
Uwe Hermann622824c2010-11-19 15:14:42 +000092 /* Add table to the RSDT. */
Stefan Reinauerdefee172015-06-18 01:11:19 -070093 rsdt->entry[i] = (uintptr_t)table;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000094
Uwe Hermann622824c2010-11-19 15:14:42 +000095 /* Fix RSDT length or the kernel will assume invalid entries. */
96 rsdt->header.length = sizeof(acpi_header_t) + (sizeof(u32) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000097
Uwe Hermann622824c2010-11-19 15:14:42 +000098 /* Re-calculate checksum. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000099 rsdt->header.checksum = 0; /* Hope this won't get optimized away */
Uwe Hermann622824c2010-11-19 15:14:42 +0000100 rsdt->header.checksum = acpi_checksum((u8 *)rsdt, rsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000101
Uwe Hermann622824c2010-11-19 15:14:42 +0000102 /*
103 * And now the same thing for the XSDT. We use the same index as for
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000104 * now we want the XSDT and RSDT to always be in sync in coreboot.
105 */
106 if (xsdt) {
Uwe Hermann622824c2010-11-19 15:14:42 +0000107 /* Add table to the XSDT. */
Stefan Reinauerdefee172015-06-18 01:11:19 -0700108 xsdt->entry[i] = (u64)(uintptr_t)table;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000109
Uwe Hermann622824c2010-11-19 15:14:42 +0000110 /* Fix XSDT length. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000111 xsdt->header.length = sizeof(acpi_header_t) +
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300112 (sizeof(u64) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000113
Uwe Hermann622824c2010-11-19 15:14:42 +0000114 /* Re-calculate checksum. */
115 xsdt->header.checksum = 0;
116 xsdt->header.checksum = acpi_checksum((u8 *)xsdt,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300117 xsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000118 }
119
Uwe Hermann622824c2010-11-19 15:14:42 +0000120 printk(BIOS_DEBUG, "ACPI: added table %d/%d, length now %d\n",
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300121 i + 1, entries_num, rsdt->header.length);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000122}
123
Uwe Hermann622824c2010-11-19 15:14:42 +0000124int acpi_create_mcfg_mmconfig(acpi_mcfg_mmconfig_t *mmconfig, u32 base,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300125 u16 seg_nr, u8 start, u8 end)
Stefan Reinauer26d431a2009-01-20 19:17:51 +0000126{
Vladimir Serbinenko60fccdc2014-10-05 11:05:16 +0200127 memset(mmconfig, 0, sizeof(*mmconfig));
Rudolf Mareke6409f22007-11-03 12:50:26 +0000128 mmconfig->base_address = base;
129 mmconfig->base_reserved = 0;
130 mmconfig->pci_segment_group_number = seg_nr;
131 mmconfig->start_bus_number = start;
132 mmconfig->end_bus_number = end;
Uwe Hermann622824c2010-11-19 15:14:42 +0000133
134 return sizeof(acpi_mcfg_mmconfig_t);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000135}
136
Stefan Reinauer777224c2005-01-19 14:06:41 +0000137int acpi_create_madt_lapic(acpi_madt_lapic_t *lapic, u8 cpu, u8 apic)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000138{
Uwe Hermann622824c2010-11-19 15:14:42 +0000139 lapic->type = 0; /* Local APIC structure */
140 lapic->length = sizeof(acpi_madt_lapic_t);
141 lapic->flags = (1 << 0); /* Processor/LAPIC enabled */
142 lapic->processor_id = cpu;
143 lapic->apic_id = apic;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000144
Uwe Hermann622824c2010-11-19 15:14:42 +0000145 return lapic->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000146}
147
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000148unsigned long acpi_create_madt_lapics(unsigned long current)
149{
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100150 struct device *cpu;
Duncan Laurie11290c42012-10-03 19:07:05 -0700151 int index = 0;
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000152
Uwe Hermann622824c2010-11-19 15:14:42 +0000153 for (cpu = all_devices; cpu; cpu = cpu->next) {
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000154 if ((cpu->path.type != DEVICE_PATH_APIC) ||
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800155 (cpu->bus->dev->path.type != DEVICE_PATH_CPU_CLUSTER)) {
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000156 continue;
157 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000158 if (!cpu->enabled)
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000159 continue;
Uwe Hermann622824c2010-11-19 15:14:42 +0000160 current += acpi_create_madt_lapic((acpi_madt_lapic_t *)current,
Duncan Laurie11290c42012-10-03 19:07:05 -0700161 index, cpu->path.apic.apic_id);
162 index++;
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000163 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000164
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000165 return current;
166}
167
Uwe Hermann622824c2010-11-19 15:14:42 +0000168int acpi_create_madt_ioapic(acpi_madt_ioapic_t *ioapic, u8 id, u32 addr,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300169 u32 gsi_base)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000170{
Uwe Hermann622824c2010-11-19 15:14:42 +0000171 ioapic->type = 1; /* I/O APIC structure */
172 ioapic->length = sizeof(acpi_madt_ioapic_t);
173 ioapic->reserved = 0x00;
174 ioapic->gsi_base = gsi_base;
175 ioapic->ioapic_id = id;
176 ioapic->ioapic_addr = addr;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000177
Uwe Hermann622824c2010-11-19 15:14:42 +0000178 return ioapic->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000179}
180
Stefan Reinauer777224c2005-01-19 14:06:41 +0000181int acpi_create_madt_irqoverride(acpi_madt_irqoverride_t *irqoverride,
Stefan Reinauer06feb882004-02-03 16:11:35 +0000182 u8 bus, u8 source, u32 gsirq, u16 flags)
183{
Uwe Hermann622824c2010-11-19 15:14:42 +0000184 irqoverride->type = 2; /* Interrupt source override */
185 irqoverride->length = sizeof(acpi_madt_irqoverride_t);
186 irqoverride->bus = bus;
187 irqoverride->source = source;
188 irqoverride->gsirq = gsirq;
189 irqoverride->flags = flags;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000190
Uwe Hermann622824c2010-11-19 15:14:42 +0000191 return irqoverride->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000192}
193
Stefan Reinauer777224c2005-01-19 14:06:41 +0000194int acpi_create_madt_lapic_nmi(acpi_madt_lapic_nmi_t *lapic_nmi, u8 cpu,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300195 u16 flags, u8 lint)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000196{
Uwe Hermann622824c2010-11-19 15:14:42 +0000197 lapic_nmi->type = 4; /* Local APIC NMI structure */
198 lapic_nmi->length = sizeof(acpi_madt_lapic_nmi_t);
199 lapic_nmi->flags = flags;
200 lapic_nmi->processor_id = cpu;
201 lapic_nmi->lint = lint;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000202
Uwe Hermann622824c2010-11-19 15:14:42 +0000203 return lapic_nmi->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000204}
205
Stefan Reinauer777224c2005-01-19 14:06:41 +0000206void acpi_create_madt(acpi_madt_t *madt)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000207{
Uwe Hermann622824c2010-11-19 15:14:42 +0000208 acpi_header_t *header = &(madt->header);
209 unsigned long current = (unsigned long)madt + sizeof(acpi_madt_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000210
Stefan Reinauer06feb882004-02-03 16:11:35 +0000211 memset((void *)madt, 0, sizeof(acpi_madt_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000212
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
Stefan Reinauer06feb882004-02-03 16:11:35 +0000219 header->length = sizeof(acpi_madt_t);
Marc Jones93a51762018-08-22 18:57:24 -0600220 header->revision = get_acpi_table_revision(MADT);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000221
Uwe Hermann622824c2010-11-19 15:14:42 +0000222 madt->lapic_addr = LOCAL_APIC_ADDR;
223 madt->flags = 0x1; /* PCAT_COMPAT */
Stefan Reinauer06feb882004-02-03 16:11:35 +0000224
Stefan Reinauerf622d592005-11-26 16:56:05 +0000225 current = acpi_fill_madt(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000226
Uwe Hermann622824c2010-11-19 15:14:42 +0000227 /* (Re)calculate length and checksum. */
228 header->length = current - (unsigned long)madt;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000229
Uwe Hermann622824c2010-11-19 15:14:42 +0000230 header->checksum = acpi_checksum((void *)madt, header->length);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000231}
232
Uwe Hermann622824c2010-11-19 15:14:42 +0000233/* MCFG is defined in the PCI Firmware Specification 3.0. */
Rudolf Mareke6409f22007-11-03 12:50:26 +0000234void acpi_create_mcfg(acpi_mcfg_t *mcfg)
235{
Uwe Hermann622824c2010-11-19 15:14:42 +0000236 acpi_header_t *header = &(mcfg->header);
237 unsigned long current = (unsigned long)mcfg + sizeof(acpi_mcfg_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000238
Rudolf Mareke6409f22007-11-03 12:50:26 +0000239 memset((void *)mcfg, 0, sizeof(acpi_mcfg_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000240
Uwe Hermann622824c2010-11-19 15:14:42 +0000241 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000242 memcpy(header->signature, "MCFG", 4);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000243 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000244 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000245 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000246
Rudolf Mareke6409f22007-11-03 12:50:26 +0000247 header->length = sizeof(acpi_mcfg_t);
Marc Jones93a51762018-08-22 18:57:24 -0600248 header->revision = get_acpi_table_revision(MCFG);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000249
250 current = acpi_fill_mcfg(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000251
Uwe Hermann622824c2010-11-19 15:14:42 +0000252 /* (Re)calculate length and checksum. */
253 header->length = current - (unsigned long)mcfg;
254 header->checksum = acpi_checksum((void *)mcfg, header->length);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000255}
256
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200257static void *get_tcpa_log(u32 *size)
258{
259 const struct cbmem_entry *ce;
260 const u32 tcpa_default_log_len = 0x10000;
261 void *lasa;
Philipp Deppenwiese791ba972018-07-30 01:15:20 +0200262 ce = cbmem_entry_find(CBMEM_ID_TCPA_TCG_LOG);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200263 if (ce) {
264 lasa = cbmem_entry_start(ce);
265 *size = cbmem_entry_size(ce);
266 printk(BIOS_DEBUG, "TCPA log found at %p\n", lasa);
267 return lasa;
268 }
Philipp Deppenwiese791ba972018-07-30 01:15:20 +0200269 lasa = cbmem_add(CBMEM_ID_TCPA_TCG_LOG, tcpa_default_log_len);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200270 if (!lasa) {
271 printk(BIOS_ERR, "TCPA log creation failed\n");
272 return NULL;
273 }
274
275 printk(BIOS_DEBUG, "TCPA log created at %p\n", lasa);
Lee Leahy024b13d2017-03-16 13:41:11 -0700276 memset(lasa, 0, tcpa_default_log_len);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200277
278 *size = tcpa_default_log_len;
279 return lasa;
280}
281
282static void acpi_create_tcpa(acpi_tcpa_t *tcpa)
283{
284 acpi_header_t *header = &(tcpa->header);
285 u32 tcpa_log_len;
286 void *lasa;
287
288 memset((void *)tcpa, 0, sizeof(acpi_tcpa_t));
289
290 lasa = get_tcpa_log(&tcpa_log_len);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700291 if (!lasa)
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200292 return;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200293
294 /* Fill out header fields. */
295 memcpy(header->signature, "TCPA", 4);
296 memcpy(header->oem_id, OEM_ID, 6);
297 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
298 memcpy(header->asl_compiler_id, ASLC, 4);
299
300 header->length = sizeof(acpi_tcpa_t);
Marc Jones93a51762018-08-22 18:57:24 -0600301 header->revision = get_acpi_table_revision(TCPA);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200302
303 tcpa->platform_class = 0;
304 tcpa->laml = tcpa_log_len;
Stefan Reinauerdefee172015-06-18 01:11:19 -0700305 tcpa->lasa = (uintptr_t) lasa;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200306
307 /* Calculate checksum. */
308 header->checksum = acpi_checksum((void *)tcpa, header->length);
309}
310
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100311static void *get_tpm2_log(u32 *size)
312{
313 const struct cbmem_entry *ce;
314 const u32 tpm2_default_log_len = 0x10000;
315 void *lasa;
316 ce = cbmem_entry_find(CBMEM_ID_TPM2_TCG_LOG);
317 if (ce) {
318 lasa = cbmem_entry_start(ce);
319 *size = cbmem_entry_size(ce);
320 printk(BIOS_DEBUG, "TPM2 log found at %p\n", lasa);
321 return lasa;
322 }
323 lasa = cbmem_add(CBMEM_ID_TPM2_TCG_LOG, tpm2_default_log_len);
324 if (!lasa) {
325 printk(BIOS_ERR, "TPM2 log creation failed\n");
326 return NULL;
327 }
328
329 printk(BIOS_DEBUG, "TPM2 log created at %p\n", lasa);
330 memset(lasa, 0, tpm2_default_log_len);
331
332 *size = tpm2_default_log_len;
333 return lasa;
334}
335
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200336static void acpi_create_tpm2(acpi_tpm2_t *tpm2)
337{
338 acpi_header_t *header = &(tpm2->header);
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100339 u32 tpm2_log_len;
340 void *lasa;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200341
342 memset((void *)tpm2, 0, sizeof(acpi_tpm2_t));
343
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100344 /*
345 * Some payloads like SeaBIOS depend on log area to use TPM2.
346 * Get the memory size and address of TPM2 log area or initialize it.
347 */
348 lasa = get_tpm2_log(&tpm2_log_len);
349 if (!lasa)
350 tpm2_log_len = 0;
351
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200352 /* Fill out header fields. */
353 memcpy(header->signature, "TPM2", 4);
354 memcpy(header->oem_id, OEM_ID, 6);
355 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
356 memcpy(header->asl_compiler_id, ASLC, 4);
357
358 header->length = sizeof(acpi_tpm2_t);
359 header->revision = get_acpi_table_revision(TPM2);
360
361 /* Hard to detect for coreboot. Just set it to 0 */
362 tpm2->platform_class = 0;
363 /* Must be set to 0 for TIS interface support */
364 tpm2->control_area = 0;
365 /* coreboot only supports the TIS interface driver. */
366 tpm2->start_method = 6;
367 memset(tpm2->msp, 0, sizeof(tpm2->msp));
368
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100369 /* Fill the log area size and start address fields. */
370 tpm2->laml = tpm2_log_len;
371 tpm2->lasa = (uintptr_t) lasa;
372
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200373 /* Calculate checksum. */
374 header->checksum = acpi_checksum((void *)tpm2, header->length);
375}
376
Duncan Laurie37319032016-05-26 12:47:05 -0700377static void acpi_ssdt_write_cbtable(void)
378{
379 const struct cbmem_entry *cbtable;
380 uintptr_t base;
381 uint32_t size;
382
383 cbtable = cbmem_entry_find(CBMEM_ID_CBTABLE);
384 if (!cbtable)
385 return;
386 base = (uintptr_t)cbmem_entry_start(cbtable);
387 size = cbmem_entry_size(cbtable);
388
389 acpigen_write_device("CTBL");
390 acpigen_write_coreboot_hid(COREBOOT_ACPI_ID_CBTABLE);
391 acpigen_write_name_integer("_UID", 0);
David Wu5dff3962018-03-21 16:48:53 +0800392 acpigen_write_STA(ACPI_STATUS_DEVICE_HIDDEN_ON);
Duncan Laurie37319032016-05-26 12:47:05 -0700393 acpigen_write_name("_CRS");
394 acpigen_write_resourcetemplate_header();
395 acpigen_write_mem32fixed(0, base, size);
396 acpigen_write_resourcetemplate_footer();
397 acpigen_pop_len();
398}
399
Myles Watson3fe6b702009-10-09 20:13:43 +0000400void acpi_create_ssdt_generator(acpi_header_t *ssdt, const char *oem_table_id)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000401{
Uwe Hermann622824c2010-11-19 15:14:42 +0000402 unsigned long current = (unsigned long)ssdt + sizeof(acpi_header_t);
403
Rudolf Marek293b5f52009-02-01 18:35:15 +0000404 memset((void *)ssdt, 0, sizeof(acpi_header_t));
Uwe Hermann622824c2010-11-19 15:14:42 +0000405
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000406 memcpy(&ssdt->signature, "SSDT", 4);
Marc Jones93a51762018-08-22 18:57:24 -0600407 ssdt->revision = get_acpi_table_revision(SSDT);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000408 memcpy(&ssdt->oem_id, OEM_ID, 6);
409 memcpy(&ssdt->oem_table_id, oem_table_id, 8);
410 ssdt->oem_revision = 42;
Uwe Hermann622824c2010-11-19 15:14:42 +0000411 memcpy(&ssdt->asl_compiler_id, ASLC, 4);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000412 ssdt->asl_compiler_revision = 42;
413 ssdt->length = sizeof(acpi_header_t);
414
Stefan Reinauer8dcd50b2009-03-06 17:24:29 +0000415 acpigen_set_current((char *) current);
Duncan Laurie37319032016-05-26 12:47:05 -0700416
417 /* Write object to declare coreboot tables */
418 acpi_ssdt_write_cbtable();
419
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200420 {
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100421 struct device *dev;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200422 for (dev = all_devices; dev; dev = dev->next)
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700423 if (dev->ops && dev->ops->acpi_fill_ssdt_generator)
Alexander Couzens5eea4582015-04-12 22:18:55 +0200424 dev->ops->acpi_fill_ssdt_generator(dev);
Vladimir Serbinenko0a669912014-10-05 14:34:17 +0200425 current = (unsigned long) acpigen_get_current();
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200426 }
Rudolf Marek293b5f52009-02-01 18:35:15 +0000427
Uwe Hermann622824c2010-11-19 15:14:42 +0000428 /* (Re)calculate length and checksum. */
Rudolf Marek293b5f52009-02-01 18:35:15 +0000429 ssdt->length = current - (unsigned long)ssdt;
430 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
431}
432
Stefan Reinauerf622d592005-11-26 16:56:05 +0000433int acpi_create_srat_lapic(acpi_srat_lapic_t *lapic, u8 node, u8 apic)
434{
Myles Watson0b4c9f02009-03-10 18:06:47 +0000435 memset((void *)lapic, 0, sizeof(acpi_srat_lapic_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000436
Uwe Hermann622824c2010-11-19 15:14:42 +0000437 lapic->type = 0; /* Processor local APIC/SAPIC affinity structure */
438 lapic->length = sizeof(acpi_srat_lapic_t);
439 lapic->flags = (1 << 0); /* Enabled (the use of this structure). */
440 lapic->proximity_domain_7_0 = node;
441 /* TODO: proximity_domain_31_8, local SAPIC EID, clock domain. */
442 lapic->apic_id = apic;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000443
Uwe Hermann622824c2010-11-19 15:14:42 +0000444 return lapic->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000445}
446
Uwe Hermann622824c2010-11-19 15:14:42 +0000447int 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 +0300448 u32 flags)
Stefan Reinauerf622d592005-11-26 16:56:05 +0000449{
Uwe Hermann622824c2010-11-19 15:14:42 +0000450 mem->type = 1; /* Memory affinity structure */
451 mem->length = sizeof(acpi_srat_mem_t);
452 mem->base_address_low = (basek << 10);
453 mem->base_address_high = (basek >> (32 - 10));
454 mem->length_low = (sizek << 10);
455 mem->length_high = (sizek >> (32 - 10));
456 mem->proximity_domain = node;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000457 mem->flags = flags;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000458
Uwe Hermann622824c2010-11-19 15:14:42 +0000459 return mem->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000460}
461
Uwe Hermann622824c2010-11-19 15:14:42 +0000462/* http://www.microsoft.com/whdc/system/sysinternals/sratdwn.mspx */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +0200463void acpi_create_srat(acpi_srat_t *srat,
464 unsigned long (*acpi_fill_srat)(unsigned long current))
Stefan Reinauerf622d592005-11-26 16:56:05 +0000465{
Uwe Hermann622824c2010-11-19 15:14:42 +0000466 acpi_header_t *header = &(srat->header);
467 unsigned long current = (unsigned long)srat + sizeof(acpi_srat_t);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000468
Uwe Hermann622824c2010-11-19 15:14:42 +0000469 memset((void *)srat, 0, sizeof(acpi_srat_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000470
Uwe Hermann622824c2010-11-19 15:14:42 +0000471 /* Fill out header fields. */
472 memcpy(header->signature, "SRAT", 4);
473 memcpy(header->oem_id, OEM_ID, 6);
474 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
475 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000476
Uwe Hermann622824c2010-11-19 15:14:42 +0000477 header->length = sizeof(acpi_srat_t);
Marc Jones93a51762018-08-22 18:57:24 -0600478 header->revision = get_acpi_table_revision(SRAT);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000479
Uwe Hermann622824c2010-11-19 15:14:42 +0000480 srat->resv = 1; /* Spec: Reserved to 1 for backwards compatibility. */
Stefan Reinauerf622d592005-11-26 16:56:05 +0000481
Uwe Hermann622824c2010-11-19 15:14:42 +0000482 current = acpi_fill_srat(current);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000483
Uwe Hermann622824c2010-11-19 15:14:42 +0000484 /* (Re)calculate length and checksum. */
485 header->length = current - (unsigned long)srat;
486 header->checksum = acpi_checksum((void *)srat, header->length);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000487}
488
Nico Hubere561f352015-10-26 11:51:25 +0100489void acpi_create_dmar(acpi_dmar_t *dmar, enum dmar_flags flags,
Lee Leahy024b13d2017-03-16 13:41:11 -0700490 unsigned long (*acpi_fill_dmar)(unsigned long))
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200491{
492 acpi_header_t *header = &(dmar->header);
493 unsigned long current = (unsigned long)dmar + sizeof(acpi_dmar_t);
494
495 memset((void *)dmar, 0, sizeof(acpi_dmar_t));
496
497 /* Fill out header fields. */
498 memcpy(header->signature, "DMAR", 4);
499 memcpy(header->oem_id, OEM_ID, 6);
500 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
501 memcpy(header->asl_compiler_id, ASLC, 4);
502
503 header->length = sizeof(acpi_dmar_t);
Marc Jones93a51762018-08-22 18:57:24 -0600504 header->revision = get_acpi_table_revision(DMAR);
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200505
Jacob Laskaf3f654d2016-03-15 21:53:27 -0500506 dmar->host_address_width = cpu_phys_address_size() - 1;
Nico Hubere561f352015-10-26 11:51:25 +0100507 dmar->flags = flags;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200508
509 current = acpi_fill_dmar(current);
510
511 /* (Re)calculate length and checksum. */
512 header->length = current - (unsigned long)dmar;
513 header->checksum = acpi_checksum((void *)dmar, header->length);
514}
515
516unsigned long acpi_create_dmar_drhd(unsigned long current, u8 flags,
Matt DeVillier7866d492018-03-29 14:59:57 +0200517 u16 segment, u64 bar)
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200518{
519 dmar_entry_t *drhd = (dmar_entry_t *)current;
520 memset(drhd, 0, sizeof(*drhd));
521 drhd->type = DMAR_DRHD;
522 drhd->length = sizeof(*drhd); /* will be fixed up later */
523 drhd->flags = flags;
524 drhd->segment = segment;
525 drhd->bar = bar;
526
527 return drhd->length;
528}
529
Matt DeVillier7866d492018-03-29 14:59:57 +0200530unsigned long acpi_create_dmar_rmrr(unsigned long current, u16 segment,
531 u64 bar, u64 limit)
532{
533 dmar_rmrr_entry_t *rmrr = (dmar_rmrr_entry_t *)current;
534 memset(rmrr, 0, sizeof(*rmrr));
535 rmrr->type = DMAR_RMRR;
536 rmrr->length = sizeof(*rmrr); /* will be fixed up later */
537 rmrr->segment = segment;
538 rmrr->bar = bar;
539 rmrr->limit = limit;
540
541 return rmrr->length;
542}
543
Werner Zehd4d76952016-07-27 06:56:36 +0200544unsigned long acpi_create_dmar_atsr(unsigned long current, u8 flags,
545 u16 segment)
546{
547 dmar_atsr_entry_t *atsr = (dmar_atsr_entry_t *)current;
548 memset(atsr, 0, sizeof(*atsr));
549 atsr->type = DMAR_ATSR;
550 atsr->length = sizeof(*atsr); /* will be fixed up later */
551 atsr->flags = flags;
552 atsr->segment = segment;
553
554 return atsr->length;
555}
556
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200557void acpi_dmar_drhd_fixup(unsigned long base, unsigned long current)
558{
559 dmar_entry_t *drhd = (dmar_entry_t *)base;
560 drhd->length = current - base;
561}
562
Matt DeVillier7866d492018-03-29 14:59:57 +0200563void acpi_dmar_rmrr_fixup(unsigned long base, unsigned long current)
564{
565 dmar_rmrr_entry_t *rmrr = (dmar_rmrr_entry_t *)base;
566 rmrr->length = current - base;
567}
568
Werner Zehd4d76952016-07-27 06:56:36 +0200569void acpi_dmar_atsr_fixup(unsigned long base, unsigned long current)
570{
571 dmar_atsr_entry_t *atsr = (dmar_atsr_entry_t *)base;
572 atsr->length = current - base;
573}
574
Matt DeVillier7866d492018-03-29 14:59:57 +0200575static unsigned long acpi_create_dmar_ds(unsigned long current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100576 enum dev_scope_type type, u8 enumeration_id, u8 bus, u8 dev, u8 fn)
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200577{
Nico Huber6c4751d2015-10-26 12:03:54 +0100578 /* we don't support longer paths yet */
579 const size_t dev_scope_length = sizeof(dev_scope_t) + 2;
580
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200581 dev_scope_t *ds = (dev_scope_t *)current;
Nico Huber6c4751d2015-10-26 12:03:54 +0100582 memset(ds, 0, dev_scope_length);
583 ds->type = type;
584 ds->length = dev_scope_length;
585 ds->enumeration = enumeration_id;
586 ds->start_bus = bus;
587 ds->path[0].dev = dev;
588 ds->path[0].fn = fn;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200589
590 return ds->length;
591}
592
Matt DeVillier7866d492018-03-29 14:59:57 +0200593unsigned long acpi_create_dmar_ds_pci_br(unsigned long current, u8 bus,
Werner Zeh21a5bff2016-07-27 07:07:20 +0200594 u8 dev, u8 fn)
595{
Matt DeVillier7866d492018-03-29 14:59:57 +0200596 return acpi_create_dmar_ds(current,
Werner Zeh21a5bff2016-07-27 07:07:20 +0200597 SCOPE_PCI_SUB, 0, bus, dev, fn);
598}
599
Matt DeVillier7866d492018-03-29 14:59:57 +0200600unsigned long acpi_create_dmar_ds_pci(unsigned long current, u8 bus,
Nico Huber6c4751d2015-10-26 12:03:54 +0100601 u8 dev, u8 fn)
602{
Matt DeVillier7866d492018-03-29 14:59:57 +0200603 return acpi_create_dmar_ds(current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100604 SCOPE_PCI_ENDPOINT, 0, bus, dev, fn);
605}
606
Matt DeVillier7866d492018-03-29 14:59:57 +0200607unsigned long acpi_create_dmar_ds_ioapic(unsigned long current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100608 u8 enumeration_id, u8 bus, u8 dev, u8 fn)
609{
Matt DeVillier7866d492018-03-29 14:59:57 +0200610 return acpi_create_dmar_ds(current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100611 SCOPE_IOAPIC, enumeration_id, bus, dev, fn);
612}
613
Matt DeVillier7866d492018-03-29 14:59:57 +0200614unsigned long acpi_create_dmar_ds_msi_hpet(unsigned long current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100615 u8 enumeration_id, u8 bus, u8 dev, u8 fn)
616{
Matt DeVillier7866d492018-03-29 14:59:57 +0200617 return acpi_create_dmar_ds(current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100618 SCOPE_MSI_HPET, enumeration_id, bus, dev, fn);
619}
620
Uwe Hermann622824c2010-11-19 15:14:42 +0000621/* http://h21007.www2.hp.com/portal/download/files/unprot/Itanium/slit.pdf */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +0200622void acpi_create_slit(acpi_slit_t *slit,
623 unsigned long (*acpi_fill_slit)(unsigned long current))
Yinghai Lud4b278c2006-10-04 20:46:15 +0000624{
Uwe Hermann622824c2010-11-19 15:14:42 +0000625 acpi_header_t *header = &(slit->header);
626 unsigned long current = (unsigned long)slit + sizeof(acpi_slit_t);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000627
Uwe Hermann622824c2010-11-19 15:14:42 +0000628 memset((void *)slit, 0, sizeof(acpi_slit_t));
Yinghai Lud4b278c2006-10-04 20:46:15 +0000629
Uwe Hermann622824c2010-11-19 15:14:42 +0000630 /* Fill out header fields. */
631 memcpy(header->signature, "SLIT", 4);
632 memcpy(header->oem_id, OEM_ID, 6);
633 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
634 memcpy(header->asl_compiler_id, ASLC, 4);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000635
Uwe Hermann622824c2010-11-19 15:14:42 +0000636 header->length = sizeof(acpi_slit_t);
Marc Jones93a51762018-08-22 18:57:24 -0600637 header->revision = get_acpi_table_revision(SLIT);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000638
Uwe Hermann622824c2010-11-19 15:14:42 +0000639 current = acpi_fill_slit(current);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000640
Uwe Hermann622824c2010-11-19 15:14:42 +0000641 /* (Re)calculate length and checksum. */
642 header->length = current - (unsigned long)slit;
643 header->checksum = acpi_checksum((void *)slit, header->length);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000644}
645
Uwe Hermann622824c2010-11-19 15:14:42 +0000646/* http://www.intel.com/hardwaredesign/hpetspec_1.pdf */
Stefan Reinauer777224c2005-01-19 14:06:41 +0000647void acpi_create_hpet(acpi_hpet_t *hpet)
Stefan Reinauer688b3852004-01-28 16:56:14 +0000648{
Uwe Hermann622824c2010-11-19 15:14:42 +0000649 acpi_header_t *header = &(hpet->header);
650 acpi_addr_t *addr = &(hpet->addr);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000651
Stefan Reinauera7648c22004-01-29 17:31:34 +0000652 memset((void *)hpet, 0, sizeof(acpi_hpet_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000653
Uwe Hermann622824c2010-11-19 15:14:42 +0000654 /* Fill out header fields. */
Stefan Reinauer4704dc52009-07-22 01:11:37 +0000655 memcpy(header->signature, "HPET", 4);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000656 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000657 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000658 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000659
Stefan Reinauer688b3852004-01-28 16:56:14 +0000660 header->length = sizeof(acpi_hpet_t);
Marc Jones93a51762018-08-22 18:57:24 -0600661 header->revision = get_acpi_table_revision(HPET);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000662
Uwe Hermann622824c2010-11-19 15:14:42 +0000663 /* Fill out HPET address. */
664 addr->space_id = 0; /* Memory */
665 addr->bit_width = 64;
666 addr->bit_offset = 0;
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200667 addr->addrl = CONFIG_HPET_ADDRESS & 0xffffffff;
668 addr->addrh = ((unsigned long long)CONFIG_HPET_ADDRESS) >> 32;
Stefan Reinauer688b3852004-01-28 16:56:14 +0000669
Lee Leahy024b13d2017-03-16 13:41:11 -0700670 hpet->id = *(unsigned int *)CONFIG_HPET_ADDRESS;
Uwe Hermann622824c2010-11-19 15:14:42 +0000671 hpet->number = 0;
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200672 hpet->min_tick = CONFIG_HPET_MIN_TICKS;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000673
Uwe Hermann622824c2010-11-19 15:14:42 +0000674 header->checksum = acpi_checksum((void *)hpet, sizeof(acpi_hpet_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000675}
Uwe Hermann622824c2010-11-19 15:14:42 +0000676
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200677void acpi_create_vfct(struct device *device,
678 struct acpi_vfct *vfct,
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700679 unsigned long (*acpi_fill_vfct)(struct device *device,
680 struct acpi_vfct *vfct_struct, unsigned long current))
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200681{
682 acpi_header_t *header = &(vfct->header);
683 unsigned long current = (unsigned long)vfct + sizeof(struct acpi_vfct);
684
685 memset((void *)vfct, 0, sizeof(struct acpi_vfct));
686
687 /* Fill out header fields. */
688 memcpy(header->signature, "VFCT", 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);
692
693 header->length = sizeof(struct acpi_vfct);
Marc Jones93a51762018-08-22 18:57:24 -0600694 header->revision = get_acpi_table_revision(VFCT);
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200695
696 current = acpi_fill_vfct(device, vfct, current);
697
698 /* (Re)calculate length and checksum. */
699 header->length = current - (unsigned long)vfct;
700 header->checksum = acpi_checksum((void *)vfct, header->length);
701}
702
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500703void acpi_create_ivrs(acpi_ivrs_t *ivrs,
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700704 unsigned long (*acpi_fill_ivrs)(acpi_ivrs_t *ivrs_struct,
705 unsigned long current))
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500706{
707 acpi_header_t *header = &(ivrs->header);
708 unsigned long current = (unsigned long)ivrs + sizeof(acpi_ivrs_t);
709
710 memset((void *)ivrs, 0, sizeof(acpi_ivrs_t));
711
712 /* Fill out header fields. */
713 memcpy(header->signature, "IVRS", 4);
714 memcpy(header->oem_id, OEM_ID, 6);
715 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
716 memcpy(header->asl_compiler_id, ASLC, 4);
717
718 header->length = sizeof(acpi_ivrs_t);
Marc Jones93a51762018-08-22 18:57:24 -0600719 header->revision = get_acpi_table_revision(IVRS);
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500720
721 current = acpi_fill_ivrs(ivrs, current);
722
723 /* (Re)calculate length and checksum. */
724 header->length = current - (unsigned long)ivrs;
725 header->checksum = acpi_checksum((void *)ivrs, header->length);
726}
727
Elyes HAOUAS9dd89cd2018-05-04 17:56:47 +0200728unsigned long acpi_write_hpet(struct device *device, unsigned long current,
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700729 acpi_rsdp_t *rsdp)
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200730{
731 acpi_hpet_t *hpet;
732
733 /*
734 * We explicitly add these tables later on:
735 */
736 printk(BIOS_DEBUG, "ACPI: * HPET\n");
737
738 hpet = (acpi_hpet_t *) current;
739 current += sizeof(acpi_hpet_t);
740 current = ALIGN(current, 16);
741 acpi_create_hpet(hpet);
742 acpi_add_table(rsdp, hpet);
743
744 return current;
745}
746
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800747void acpi_create_dbg2(acpi_dbg2_header_t *dbg2,
748 int port_type, int port_subtype,
749 acpi_addr_t *address, uint32_t address_size,
750 const char *device_path)
751{
752 uintptr_t current;
753 acpi_dbg2_device_t *device;
754 uint32_t *dbg2_addr_size;
755 acpi_header_t *header;
756 size_t path_len;
757 const char *path;
758 char *namespace;
759
760 /* Fill out header fields. */
761 current = (uintptr_t)dbg2;
762 memset(dbg2, 0, sizeof(acpi_dbg2_header_t));
763 header = &(dbg2->header);
Marc Jones93a51762018-08-22 18:57:24 -0600764 header->revision = get_acpi_table_revision(DBG2);
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800765 memcpy(header->signature, "DBG2", 4);
766 memcpy(header->oem_id, OEM_ID, 6);
767 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
768 memcpy(header->asl_compiler_id, ASLC, 4);
769
770 /* One debug device defined */
771 dbg2->devices_offset = sizeof(acpi_dbg2_header_t);
772 dbg2->devices_count = 1;
773 current += sizeof(acpi_dbg2_header_t);
774
775 /* Device comes after the header */
776 device = (acpi_dbg2_device_t *)current;
777 memset(device, 0, sizeof(acpi_dbg2_device_t));
778 current += sizeof(acpi_dbg2_device_t);
779
780 device->revision = 0;
781 device->address_count = 1;
782 device->port_type = port_type;
783 device->port_subtype = port_subtype;
784
785 /* Base Address comes after device structure */
786 memcpy((void *)current, address, sizeof(acpi_addr_t));
787 device->base_address_offset = current - (uintptr_t)device;
788 current += sizeof(acpi_addr_t);
789
790 /* Address Size comes after address structure */
791 dbg2_addr_size = (uint32_t *)current;
792 device->address_size_offset = current - (uintptr_t)device;
793 *dbg2_addr_size = address_size;
794 current += sizeof(uint32_t);
795
796 /* Namespace string comes last, use '.' if not provided */
797 path = device_path ? : ".";
798 /* Namespace string length includes NULL terminator */
799 path_len = strlen(path) + 1;
800 namespace = (char *)current;
801 device->namespace_string_length = path_len;
802 device->namespace_string_offset = current - (uintptr_t)device;
803 strncpy(namespace, path, path_len);
804 current += path_len;
805
806 /* Update structure lengths and checksum */
807 device->length = current - (uintptr_t)device;
808 header->length = current - (uintptr_t)dbg2;
809 header->checksum = acpi_checksum((uint8_t *)dbg2, header->length);
810}
811
812unsigned long acpi_write_dbg2_pci_uart(acpi_rsdp_t *rsdp, unsigned long current,
813 struct device *dev, uint8_t access_size)
814{
815 acpi_dbg2_header_t *dbg2 = (acpi_dbg2_header_t *)current;
816 struct resource *res;
817 acpi_addr_t address;
818
819 if (!dev) {
820 printk(BIOS_ERR, "%s: Device not found\n", __func__);
821 return current;
822 }
Mario Scheithauerf1eb0ea2017-11-21 13:52:53 +0100823 if (!dev->enabled) {
824 printk(BIOS_INFO, "%s: Device not enabled\n", __func__);
825 return current;
826 }
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800827 res = find_resource(dev, PCI_BASE_ADDRESS_0);
828 if (!res) {
829 printk(BIOS_ERR, "%s: Unable to find resource for %s\n",
830 __func__, dev_path(dev));
831 return current;
832 }
833
834 memset(&address, 0, sizeof(address));
835 if (res->flags & IORESOURCE_IO)
836 address.space_id = ACPI_ADDRESS_SPACE_IO;
837 else if (res->flags & IORESOURCE_MEM)
838 address.space_id = ACPI_ADDRESS_SPACE_MEMORY;
839 else {
840 printk(BIOS_ERR, "%s: Unknown address space type\n", __func__);
841 return current;
842 }
843
844 address.addrl = (uint32_t)res->base;
845 address.addrh = (uint32_t)((res->base >> 32) & 0xffffffff);
846 address.access_size = access_size;
847
848 acpi_create_dbg2(dbg2,
849 ACPI_DBG2_PORT_SERIAL,
850 ACPI_DBG2_PORT_SERIAL_16550,
851 &address, res->size,
852 acpi_device_path(dev));
853
854 if (dbg2->header.length) {
855 current += dbg2->header.length;
856 current = acpi_align_current(current);
857 acpi_add_table(rsdp, dbg2);
858 }
859
860 return current;
861}
862
Stefan Reinauer777224c2005-01-19 14:06:41 +0000863void acpi_create_facs(acpi_facs_t *facs)
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000864{
Uwe Hermann622824c2010-11-19 15:14:42 +0000865 memset((void *)facs, 0, sizeof(acpi_facs_t));
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000866
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000867 memcpy(facs->signature, "FACS", 4);
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000868 facs->length = sizeof(acpi_facs_t);
869 facs->hardware_signature = 0;
870 facs->firmware_waking_vector = 0;
871 facs->global_lock = 0;
872 facs->flags = 0;
873 facs->x_firmware_waking_vector_l = 0;
874 facs->x_firmware_waking_vector_h = 0;
Marc Jones93a51762018-08-22 18:57:24 -0600875 facs->version = get_acpi_table_revision(FACS);
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000876}
Stefan Reinauer777224c2005-01-19 14:06:41 +0000877
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100878static void acpi_write_rsdt(acpi_rsdt_t *rsdt, char *oem_id, char *oem_table_id)
Stefan Reinauer14e22772010-04-27 06:56:47 +0000879{
Uwe Hermann622824c2010-11-19 15:14:42 +0000880 acpi_header_t *header = &(rsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000881
Uwe Hermann622824c2010-11-19 15:14:42 +0000882 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000883 memcpy(header->signature, "RSDT", 4);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100884 memcpy(header->oem_id, oem_id, 6);
885 memcpy(header->oem_table_id, oem_table_id, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000886 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000887
Stefan Reinauer688b3852004-01-28 16:56:14 +0000888 header->length = sizeof(acpi_rsdt_t);
Marc Jones93a51762018-08-22 18:57:24 -0600889 header->revision = get_acpi_table_revision(RSDT);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000890
Uwe Hermann622824c2010-11-19 15:14:42 +0000891 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauer688b3852004-01-28 16:56:14 +0000892
Uwe Hermann622824c2010-11-19 15:14:42 +0000893 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000894 header->checksum = acpi_checksum((void *)rsdt, sizeof(acpi_rsdt_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000895}
896
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100897static void acpi_write_xsdt(acpi_xsdt_t *xsdt, char *oem_id, char *oem_table_id)
Stefan Reinauer14e22772010-04-27 06:56:47 +0000898{
Uwe Hermann622824c2010-11-19 15:14:42 +0000899 acpi_header_t *header = &(xsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000900
Uwe Hermann622824c2010-11-19 15:14:42 +0000901 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000902 memcpy(header->signature, "XSDT", 4);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100903 memcpy(header->oem_id, oem_id, 6);
904 memcpy(header->oem_table_id, oem_table_id, 8);
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000905 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000906
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000907 header->length = sizeof(acpi_xsdt_t);
Marc Jones93a51762018-08-22 18:57:24 -0600908 header->revision = get_acpi_table_revision(XSDT);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000909
Uwe Hermann622824c2010-11-19 15:14:42 +0000910 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000911
Uwe Hermann622824c2010-11-19 15:14:42 +0000912 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000913 header->checksum = acpi_checksum((void *)xsdt, sizeof(acpi_xsdt_t));
914}
915
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100916static void acpi_write_rsdp(acpi_rsdp_t *rsdp, acpi_rsdt_t *rsdt,
917 acpi_xsdt_t *xsdt, char *oem_id)
Stefan Reinauer688b3852004-01-28 16:56:14 +0000918{
Stefan Reinauerd18faac2009-11-05 18:06:43 +0000919 memset(rsdp, 0, sizeof(acpi_rsdp_t));
Uwe Hermann622824c2010-11-19 15:14:42 +0000920
Stefan Reinauer688b3852004-01-28 16:56:14 +0000921 memcpy(rsdp->signature, RSDP_SIG, 8);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100922 memcpy(rsdp->oem_id, oem_id, 6);
Uwe Hermann622824c2010-11-19 15:14:42 +0000923
924 rsdp->length = sizeof(acpi_rsdp_t);
Stefan Reinauerdefee172015-06-18 01:11:19 -0700925 rsdp->rsdt_address = (uintptr_t)rsdt;
Uwe Hermann622824c2010-11-19 15:14:42 +0000926
927 /*
928 * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2.
929 *
930 * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
931 * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
932 * revision 0).
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000933 */
934 if (xsdt == NULL) {
Uwe Hermann622824c2010-11-19 15:14:42 +0000935 rsdp->revision = 0;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000936 } else {
Stefan Reinauerdefee172015-06-18 01:11:19 -0700937 rsdp->xsdt_address = (u64)(uintptr_t)xsdt;
Marc Jones93a51762018-08-22 18:57:24 -0600938 rsdp->revision = get_acpi_table_revision(RSDP);
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000939 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000940
941 /* Calculate checksums. */
942 rsdp->checksum = acpi_checksum((void *)rsdp, 20);
943 rsdp->ext_checksum = acpi_checksum((void *)rsdp, sizeof(acpi_rsdp_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000944}
945
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700946unsigned long acpi_create_hest_error_source(acpi_hest_t *hest,
947 acpi_hest_esd_t *esd, u16 type, void *data, u16 data_len)
zbaocaf494c82012-04-13 13:57:14 +0800948{
949 acpi_header_t *header = &(hest->header);
950 acpi_hest_hen_t *hen;
951 void *pos;
952 u16 len;
953
954 pos = esd;
955 memset(pos, 0, sizeof(acpi_hest_esd_t));
956 len = 0;
957 esd->type = type; /* MCE */
958 esd->source_id = hest->error_source_count;
959 esd->flags = 0; /* FIRMWARE_FIRST */
960 esd->enabled = 1;
961 esd->prealloc_erecords = 1;
962 esd->max_section_per_record = 0x1;
963
964 len += sizeof(acpi_hest_esd_t);
965 pos = esd + 1;
966
967 switch (type) {
968 case 0: /* MCE */
969 break;
970 case 1: /* CMC */
971 hen = (acpi_hest_hen_t *) (pos);
972 memset(pos, 0, sizeof(acpi_hest_hen_t));
973 hen->type = 3; /* SCI? */
974 hen->length = sizeof(acpi_hest_hen_t);
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700975 hen->conf_we = 0; /* Configuration Write Enable. */
zbaocaf494c82012-04-13 13:57:14 +0800976 hen->poll_interval = 0;
977 hen->vector = 0;
978 hen->sw2poll_threshold_val = 0;
979 hen->sw2poll_threshold_win = 0;
980 hen->error_threshold_val = 0;
981 hen->error_threshold_win = 0;
982 len += sizeof(acpi_hest_hen_t);
983 pos = hen + 1;
984 break;
985 case 2: /* NMI */
986 case 6: /* AER Root Port */
987 case 7: /* AER Endpoint */
988 case 8: /* AER Bridge */
989 case 9: /* Generic Hardware Error Source. */
990 /* TODO: */
991 break;
992 default:
993 printk(BIOS_DEBUG, "Invalid type of Error Source.");
994 break;
995 }
Lee Leahy024b13d2017-03-16 13:41:11 -0700996 hest->error_source_count++;
zbaocaf494c82012-04-13 13:57:14 +0800997
998 memcpy(pos, data, data_len);
999 len += data_len;
1000 header->length += len;
1001
1002 return len;
1003}
1004
1005/* ACPI 4.0 */
Vladimir Serbinenko807127f2014-11-09 13:36:18 +01001006void acpi_write_hest(acpi_hest_t *hest,
1007 unsigned long (*acpi_fill_hest)(acpi_hest_t *hest))
zbaocaf494c82012-04-13 13:57:14 +08001008{
1009 acpi_header_t *header = &(hest->header);
1010
1011 memset(hest, 0, sizeof(acpi_hest_t));
1012
1013 memcpy(header->signature, "HEST", 4);
1014 memcpy(header->oem_id, OEM_ID, 6);
1015 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1016 memcpy(header->asl_compiler_id, ASLC, 4);
1017 header->length += sizeof(acpi_hest_t);
Marc Jones93a51762018-08-22 18:57:24 -06001018 header->revision = get_acpi_table_revision(HEST);
zbaocaf494c82012-04-13 13:57:14 +08001019
1020 acpi_fill_hest(hest);
1021
1022 /* Calculate checksums. */
1023 header->checksum = acpi_checksum((void *)hest, header->length);
1024}
1025
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001026/* ACPI 3.0b */
1027void acpi_write_bert(acpi_bert_t *bert, uintptr_t region, size_t length)
1028{
1029 acpi_header_t *header = &(bert->header);
1030
1031 memset(bert, 0, sizeof(acpi_bert_t));
1032
1033 memcpy(header->signature, "BERT", 4);
1034 memcpy(header->oem_id, OEM_ID, 6);
1035 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1036 memcpy(header->asl_compiler_id, ASLC, 4);
1037 header->length += sizeof(acpi_bert_t);
1038 header->revision = get_acpi_table_revision(BERT);
1039
1040 bert->error_region = region;
1041 bert->region_length = length;
1042
1043 /* Calculate checksums. */
1044 header->checksum = acpi_checksum((void *)bert, header->length);
1045}
1046
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001047#if IS_ENABLED(CONFIG_COMMON_FADT)
Lee Leahy024b13d2017-03-16 13:41:11 -07001048void acpi_create_fadt(acpi_fadt_t *fadt, acpi_facs_t *facs, void *dsdt)
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001049{
1050 acpi_header_t *header = &(fadt->header);
1051
1052 memset((void *) fadt, 0, sizeof(acpi_fadt_t));
1053 memcpy(header->signature, "FACP", 4);
1054 header->length = sizeof(acpi_fadt_t);
Marc Jones93a51762018-08-22 18:57:24 -06001055 header->revision = get_acpi_table_revision(FADT);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001056 memcpy(header->oem_id, OEM_ID, 6);
1057 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1058 memcpy(header->asl_compiler_id, ASLC, 4);
1059 header->asl_compiler_revision = 0;
1060
1061 fadt->firmware_ctrl = (unsigned long) facs;
1062 fadt->dsdt = (unsigned long) dsdt;
1063
1064 fadt->x_firmware_ctl_l = (unsigned long)facs;
1065 fadt->x_firmware_ctl_h = 0;
1066 fadt->x_dsdt_l = (unsigned long)dsdt;
1067 fadt->x_dsdt_h = 0;
1068
Lee Leahy9c7c6f72017-03-16 11:24:09 -07001069 if (IS_ENABLED(CONFIG_SYSTEM_TYPE_LAPTOP))
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001070 fadt->preferred_pm_profile = PM_MOBILE;
Lee Leahy9c7c6f72017-03-16 11:24:09 -07001071 else
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001072 fadt->preferred_pm_profile = PM_DESKTOP;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001073
Vladimir Serbinenkoab83ef02014-10-25 15:18:25 +02001074 acpi_fill_fadt(fadt);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001075
1076 header->checksum =
1077 acpi_checksum((void *) fadt, header->length);
1078}
1079#endif
1080
Aaron Durbin64031672018-04-21 14:45:32 -06001081unsigned long __weak fw_cfg_acpi_tables(unsigned long start)
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001082{
1083 return 0;
1084}
1085
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001086unsigned long write_acpi_tables(unsigned long start)
1087{
1088 unsigned long current;
1089 acpi_rsdp_t *rsdp;
1090 acpi_rsdt_t *rsdt;
1091 acpi_xsdt_t *xsdt;
1092 acpi_fadt_t *fadt;
1093 acpi_facs_t *facs;
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001094 acpi_header_t *slic_file, *slic;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001095 acpi_header_t *ssdt;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001096 acpi_header_t *dsdt_file, *dsdt;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001097 acpi_mcfg_t *mcfg;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +02001098 acpi_tcpa_t *tcpa;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001099 acpi_tpm2_t *tpm2;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001100 acpi_madt_t *madt;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +11001101 struct device *dev;
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001102 unsigned long fw;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001103 size_t slic_size, dsdt_size;
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001104 char oem_id[6], oem_table_id[8];
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001105
1106 current = start;
1107
1108 /* Align ACPI tables to 16byte */
Aaron Durbin07a1b282015-12-10 17:07:38 -06001109 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001110
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001111 fw = fw_cfg_acpi_tables(current);
1112 if (fw)
1113 return fw;
1114
Vladimir Serbinenkoa4cf83d2015-06-02 21:40:29 +02001115 dsdt_file = cbfs_boot_map_with_leak(
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001116 CONFIG_CBFS_PREFIX "/dsdt.aml",
1117 CBFS_TYPE_RAW, &dsdt_size);
1118 if (!dsdt_file) {
1119 printk(BIOS_ERR, "No DSDT file, skipping ACPI tables\n");
1120 return current;
1121 }
1122
1123 if (dsdt_file->length > dsdt_size
Elyes HAOUAS7d87e762016-10-03 22:11:07 +02001124 || dsdt_file->length < sizeof(acpi_header_t)
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001125 || memcmp(dsdt_file->signature, "DSDT", 4) != 0) {
1126 printk(BIOS_ERR, "Invalid DSDT file, skipping ACPI tables\n");
1127 return current;
1128 }
1129
Aaron Durbin899d13d2015-05-15 23:39:23 -05001130 slic_file = cbfs_boot_map_with_leak(CONFIG_CBFS_PREFIX "/slic",
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001131 CBFS_TYPE_RAW, &slic_size);
Vladimir Serbinenko2cb29782015-05-31 11:32:09 +02001132 if (slic_file
1133 && (slic_file->length > slic_size
Elyes HAOUAS7d87e762016-10-03 22:11:07 +02001134 || slic_file->length < sizeof(acpi_header_t)
Vladimir Serbinenko2cb29782015-05-31 11:32:09 +02001135 || memcmp(slic_file->signature, "SLIC", 4) != 0)) {
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001136 slic_file = 0;
1137 }
1138
1139 if (slic_file) {
1140 memcpy(oem_id, slic_file->oem_id, 6);
1141 memcpy(oem_table_id, slic_file->oem_table_id, 8);
1142 } else {
1143 memcpy(oem_id, OEM_ID, 6);
1144 memcpy(oem_table_id, ACPI_TABLE_CREATOR, 8);
1145 }
1146
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001147 printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx.\n", start);
1148
1149 /* We need at least an RSDP and an RSDT Table */
1150 rsdp = (acpi_rsdp_t *) current;
1151 current += sizeof(acpi_rsdp_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001152 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001153 rsdt = (acpi_rsdt_t *) current;
1154 current += sizeof(acpi_rsdt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001155 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001156 xsdt = (acpi_xsdt_t *) current;
1157 current += sizeof(acpi_xsdt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001158 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001159
1160 /* clear all table memory */
1161 memset((void *) start, 0, current - start);
1162
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001163 acpi_write_rsdp(rsdp, rsdt, xsdt, oem_id);
1164 acpi_write_rsdt(rsdt, oem_id, oem_table_id);
1165 acpi_write_xsdt(xsdt, oem_id, oem_table_id);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001166
1167 printk(BIOS_DEBUG, "ACPI: * FACS\n");
Patrick Georgi133108a2015-08-08 23:20:58 +02001168 current = (ALIGN(current, 64));
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001169 facs = (acpi_facs_t *) current;
1170 current += sizeof(acpi_facs_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001171 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001172 acpi_create_facs(facs);
1173
1174 printk(BIOS_DEBUG, "ACPI: * DSDT\n");
1175 dsdt = (acpi_header_t *) current;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001176 memcpy(dsdt, dsdt_file, sizeof(acpi_header_t));
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001177 if (dsdt->length >= sizeof(acpi_header_t)) {
1178 current += sizeof(acpi_header_t);
1179
1180 acpigen_set_current((char *) current);
1181 for (dev = all_devices; dev; dev = dev->next)
Lee Leahy9c7c6f72017-03-16 11:24:09 -07001182 if (dev->ops && dev->ops->acpi_inject_dsdt_generator)
Alexander Couzensa90dad12015-04-12 21:49:46 +02001183 dev->ops->acpi_inject_dsdt_generator(dev);
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001184 current = (unsigned long) acpigen_get_current();
1185 memcpy((char *)current,
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001186 (char *)dsdt_file + sizeof(acpi_header_t),
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001187 dsdt->length - sizeof(acpi_header_t));
1188 current += dsdt->length - sizeof(acpi_header_t);
1189
1190 /* (Re)calculate length and checksum. */
1191 dsdt->length = current - (unsigned long)dsdt;
1192 dsdt->checksum = 0;
1193 dsdt->checksum = acpi_checksum((void *)dsdt, dsdt->length);
1194 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001195
Aaron Durbin07a1b282015-12-10 17:07:38 -06001196 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001197
1198 printk(BIOS_DEBUG, "ACPI: * FADT\n");
1199 fadt = (acpi_fadt_t *) current;
1200 current += sizeof(acpi_fadt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001201 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001202
1203 acpi_create_fadt(fadt, facs, dsdt);
1204 acpi_add_table(rsdp, fadt);
1205
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001206 if (slic_file) {
1207 printk(BIOS_DEBUG, "ACPI: * SLIC\n");
1208 slic = (acpi_header_t *)current;
1209 memcpy(slic, slic_file, slic_file->length);
1210 current += slic_file->length;
Aaron Durbin07a1b282015-12-10 17:07:38 -06001211 current = acpi_align_current(current);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001212 acpi_add_table(rsdp, slic);
1213 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001214
1215 printk(BIOS_DEBUG, "ACPI: * SSDT\n");
1216 ssdt = (acpi_header_t *)current;
1217 acpi_create_ssdt_generator(ssdt, ACPI_TABLE_CREATOR);
Vladimir Serbinenko9310df82014-10-10 20:40:41 +02001218 if (ssdt->length > sizeof(acpi_header_t)) {
1219 current += ssdt->length;
1220 acpi_add_table(rsdp, ssdt);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001221 current = acpi_align_current(current);
Vladimir Serbinenko9310df82014-10-10 20:40:41 +02001222 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001223
1224 printk(BIOS_DEBUG, "ACPI: * MCFG\n");
1225 mcfg = (acpi_mcfg_t *) current;
1226 acpi_create_mcfg(mcfg);
1227 if (mcfg->header.length > sizeof(acpi_mcfg_t)) {
1228 current += mcfg->header.length;
Aaron Durbin07a1b282015-12-10 17:07:38 -06001229 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001230 acpi_add_table(rsdp, mcfg);
1231 }
1232
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001233 if (IS_ENABLED(CONFIG_TPM1)) {
1234 printk(BIOS_DEBUG, "ACPI: * TCPA\n");
1235 tcpa = (acpi_tcpa_t *) current;
1236 acpi_create_tcpa(tcpa);
1237 if (tcpa->header.length >= sizeof(acpi_tcpa_t)) {
1238 current += tcpa->header.length;
1239 current = acpi_align_current(current);
1240 acpi_add_table(rsdp, tcpa);
1241 }
1242 }
1243
1244 if (IS_ENABLED(CONFIG_TPM2)) {
1245 printk(BIOS_DEBUG, "ACPI: * TPM2\n");
1246 tpm2 = (acpi_tpm2_t *) current;
1247 acpi_create_tpm2(tpm2);
1248 if (tpm2->header.length >= sizeof(acpi_tpm2_t)) {
1249 current += tpm2->header.length;
1250 current = acpi_align_current(current);
1251 acpi_add_table(rsdp, tpm2);
1252 }
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +02001253 }
1254
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001255 printk(BIOS_DEBUG, "ACPI: * MADT\n");
1256
1257 madt = (acpi_madt_t *) current;
1258 acpi_create_madt(madt);
1259 if (madt->header.length > sizeof(acpi_madt_t)) {
Lee Leahy024b13d2017-03-16 13:41:11 -07001260 current += madt->header.length;
1261 acpi_add_table(rsdp, madt);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001262 }
Aaron Durbin07a1b282015-12-10 17:07:38 -06001263 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001264
1265 printk(BIOS_DEBUG, "current = %lx\n", current);
1266
1267 for (dev = all_devices; dev; dev = dev->next) {
1268 if (dev->ops && dev->ops->write_acpi_tables) {
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001269 current = dev->ops->write_acpi_tables(dev, current,
1270 rsdp);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001271 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001272 }
1273 }
1274
1275 printk(BIOS_INFO, "ACPI: done.\n");
1276 return current;
1277}
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001278
Rudolf Marek33cafe52009-04-13 18:07:02 +00001279static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp)
1280{
1281 if (strncmp((char *)rsdp, RSDP_SIG, sizeof(RSDP_SIG) - 1) != 0)
1282 return NULL;
1283
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001284 printk(BIOS_DEBUG, "Looking on %p for valid checksum\n", rsdp);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001285
1286 if (acpi_checksum((void *)rsdp, 20) != 0)
1287 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001288 printk(BIOS_DEBUG, "Checksum 1 passed\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001289
1290 if ((rsdp->revision > 1) && (acpi_checksum((void *)rsdp,
1291 rsdp->length) != 0))
1292 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001293 printk(BIOS_DEBUG, "Checksum 2 passed all OK\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001294
1295 return rsdp;
1296}
1297
Rudolf Marek33cafe52009-04-13 18:07:02 +00001298void *acpi_find_wakeup_vector(void)
1299{
1300 char *p, *end;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001301 acpi_rsdt_t *rsdt;
1302 acpi_facs_t *facs;
Martin Rothc5f49262012-12-14 19:17:55 -07001303 acpi_fadt_t *fadt = NULL;
Kyösti Mälkki072d436b2016-06-17 08:44:40 +03001304 acpi_rsdp_t *rsdp = NULL;
Uwe Hermann622824c2010-11-19 15:14:42 +00001305 void *wake_vec;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001306 int i;
1307
Rudolf Marek33cafe52009-04-13 18:07:02 +00001308 if (!acpi_is_wakeup())
1309 return NULL;
1310
Uwe Hermann622824c2010-11-19 15:14:42 +00001311 printk(BIOS_DEBUG, "Trying to find the wakeup vector...\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001312
Uwe Hermann622824c2010-11-19 15:14:42 +00001313 /* Find RSDP. */
1314 for (p = (char *)0xe0000; p < (char *)0xfffff; p += 16) {
Lee Leahy0b5678f2017-03-16 16:01:40 -07001315 rsdp = valid_rsdp((acpi_rsdp_t *)p);
1316 if (rsdp)
Rudolf Marek33cafe52009-04-13 18:07:02 +00001317 break;
1318 }
1319
Angel Pons98a68ad2018-11-04 12:25:25 +01001320 if (rsdp == NULL) {
1321 printk(BIOS_ALERT,
1322 "No RSDP found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001323 return NULL;
Angel Pons98a68ad2018-11-04 12:25:25 +01001324 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001325
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001326 printk(BIOS_DEBUG, "RSDP found at %p\n", rsdp);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001327 rsdt = (acpi_rsdt_t *)(uintptr_t)rsdp->rsdt_address;
Stefan Reinauer14e22772010-04-27 06:56:47 +00001328
Uwe Hermann622824c2010-11-19 15:14:42 +00001329 end = (char *)rsdt + rsdt->header.length;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001330 printk(BIOS_DEBUG, "RSDT found at %p ends at %p\n", rsdt, end);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001331
Uwe Hermann622824c2010-11-19 15:14:42 +00001332 for (i = 0; ((char *)&rsdt->entry[i]) < end; i++) {
Stefan Reinauer71a30182015-07-30 16:28:13 -07001333 fadt = (acpi_fadt_t *)(uintptr_t)rsdt->entry[i];
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001334 if (strncmp((char *)fadt, "FACP", 4) == 0)
Rudolf Marek33cafe52009-04-13 18:07:02 +00001335 break;
1336 fadt = NULL;
1337 }
1338
Angel Pons98a68ad2018-11-04 12:25:25 +01001339 if (fadt == NULL) {
1340 printk(BIOS_ALERT,
1341 "No FADT found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001342 return NULL;
Angel Pons98a68ad2018-11-04 12:25:25 +01001343 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001344
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001345 printk(BIOS_DEBUG, "FADT found at %p\n", fadt);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001346 facs = (acpi_facs_t *)(uintptr_t)fadt->firmware_ctrl;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001347
Stefan Reinauer7e9771c2009-04-22 08:17:38 +00001348 if (facs == NULL) {
Angel Pons98a68ad2018-11-04 12:25:25 +01001349 printk(BIOS_ALERT,
1350 "No FACS found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001351 return NULL;
Stefan Reinauer7e9771c2009-04-22 08:17:38 +00001352 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001353
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001354 printk(BIOS_DEBUG, "FACS found at %p\n", facs);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001355 wake_vec = (void *)(uintptr_t)facs->firmware_waking_vector;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001356 printk(BIOS_DEBUG, "OS waking vector is %p\n", wake_vec);
Uwe Hermann622824c2010-11-19 15:14:42 +00001357
Rudolf Marek33cafe52009-04-13 18:07:02 +00001358 return wake_vec;
1359}
1360
Aaron Durbin64031672018-04-21 14:45:32 -06001361__weak int acpi_get_gpe(int gpe)
Duncan Laurie1f6e6812016-09-19 12:04:19 -07001362{
1363 return -1; /* implemented by SOC */
1364}
Marc Jones93a51762018-08-22 18:57:24 -06001365
1366int get_acpi_table_revision(enum acpi_tables table)
1367{
1368 switch (table) {
1369 case FADT:
1370 return ACPI_FADT_REV_ACPI_3_0;
1371 case MADT: /* ACPI 1.0/2.0: 1, ACPI 3.0: 2, ACPI 4.0: 3 */
Marc Jones4ddd4762018-08-21 10:50:57 -06001372 return 2;
Marc Jones93a51762018-08-22 18:57:24 -06001373 case MCFG:
1374 return 1;
1375 case TCPA:
1376 return 2;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001377 case TPM2:
1378 return 4;
Marc Jones93a51762018-08-22 18:57:24 -06001379 case SSDT: /* ACPI 1.0/2.0: ?, ACPI 3.0/4.0: 2 */
1380 return 2;
1381 case SRAT: /* ACPI 1.0: N/A, 2.0: 1, 3.0: 2, 4.0: 3 */
1382 return 1; /* TODO Should probably be upgraded to 2 */
1383 case DMAR:
1384 return 1;
1385 case SLIT: /* ACPI 1.0: N/A, ACPI 2.0/3.0/4.0: 1 */
1386 return 1;
1387 case HPET: /* Currently 1. Table added in ACPI 2.0. */
1388 return 1;
1389 case VFCT: /* ACPI 1.0: N/A, ACPI 2.0/3.0/4.0: 1 */
1390 return 1;
1391 case IVRS:
1392 return IVRS_FORMAT_FIXED;
1393 case DBG2:
1394 return 0;
1395 case FACS: /* ACPI 1.0: 0, ACPI 2.0/3.0: 1, ACPI 4.0: 2 */
1396 return 1;
1397 case RSDT: /* ACPI 1.0/2.0/3.0/4.0: 1 */
1398 return 1;
1399 case XSDT: /* ACPI 1.0: N/A, 2.0/3.0/4.0: 1 */
1400 return 1;
1401 case RSDP: /* ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2. */
1402 return 2;
1403 case HEST:
1404 return 1;
1405 case NHLT:
1406 return 5;
Marshall Dawson44705c62018-09-04 13:47:15 -06001407 case BERT:
1408 return 1;
Marc Jones93a51762018-08-22 18:57:24 -06001409 default:
1410 return -1;
1411 }
1412 return -1;
1413}