blob: e06c3ad71ba6783584aa2bfbe27c6d181d0e7e97 [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
Werner Zehdb561e62019-02-19 13:39:56 +010011 * Copyright (C) 2016-2019 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>
Elyes HAOUAS26071aa2019-02-15 08:21:33 +010050#include <version.h>
Werner Zehdb561e62019-02-19 13:39:56 +010051#include <commonlib/sort.h>
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000052
53u8 acpi_checksum(u8 *table, u32 length)
Stefan Reinauer688b3852004-01-28 16:56:14 +000054{
Uwe Hermann622824c2010-11-19 15:14:42 +000055 u8 ret = 0;
Stefan Reinauer688b3852004-01-28 16:56:14 +000056 while (length--) {
57 ret += *table;
58 table++;
59 }
Stefan Reinauera7648c22004-01-29 17:31:34 +000060 return -ret;
Stefan Reinauer688b3852004-01-28 16:56:14 +000061}
62
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000063/**
Uwe Hermann622824c2010-11-19 15:14:42 +000064 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
65 * and checksum.
Stefan Reinauer06feb882004-02-03 16:11:35 +000066 */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000067void acpi_add_table(acpi_rsdp_t *rsdp, void *table)
Stefan Reinauer688b3852004-01-28 16:56:14 +000068{
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000069 int i, entries_num;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000070 acpi_rsdt_t *rsdt;
71 acpi_xsdt_t *xsdt = NULL;
72
Uwe Hermann622824c2010-11-19 15:14:42 +000073 /* The RSDT is mandatory... */
Stefan Reinauerdefee172015-06-18 01:11:19 -070074 rsdt = (acpi_rsdt_t *)(uintptr_t)rsdp->rsdt_address;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000075
Uwe Hermann622824c2010-11-19 15:14:42 +000076 /* ...while the XSDT is not. */
77 if (rsdp->xsdt_address)
Stefan Reinauerdefee172015-06-18 01:11:19 -070078 xsdt = (acpi_xsdt_t *)((uintptr_t)rsdp->xsdt_address);
Stefan Reinauer14e22772010-04-27 06:56:47 +000079
Uwe Hermann622824c2010-11-19 15:14:42 +000080 /* This should always be MAX_ACPI_TABLES. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000081 entries_num = ARRAY_SIZE(rsdt->entry);
Stefan Reinauer14e22772010-04-27 06:56:47 +000082
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000083 for (i = 0; i < entries_num; i++) {
Uwe Hermann622824c2010-11-19 15:14:42 +000084 if (rsdt->entry[i] == 0)
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000085 break;
Stefan Reinauer688b3852004-01-28 16:56:14 +000086 }
87
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000088 if (i >= entries_num) {
Uwe Hermann622824c2010-11-19 15:14:42 +000089 printk(BIOS_ERR, "ACPI: Error: Could not add ACPI table, "
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +030090 "too many tables.\n");
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000091 return;
92 }
93
Uwe Hermann622824c2010-11-19 15:14:42 +000094 /* Add table to the RSDT. */
Stefan Reinauerdefee172015-06-18 01:11:19 -070095 rsdt->entry[i] = (uintptr_t)table;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000096
Uwe Hermann622824c2010-11-19 15:14:42 +000097 /* Fix RSDT length or the kernel will assume invalid entries. */
98 rsdt->header.length = sizeof(acpi_header_t) + (sizeof(u32) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000099
Uwe Hermann622824c2010-11-19 15:14:42 +0000100 /* Re-calculate checksum. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000101 rsdt->header.checksum = 0; /* Hope this won't get optimized away */
Uwe Hermann622824c2010-11-19 15:14:42 +0000102 rsdt->header.checksum = acpi_checksum((u8 *)rsdt, rsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000103
Uwe Hermann622824c2010-11-19 15:14:42 +0000104 /*
105 * And now the same thing for the XSDT. We use the same index as for
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000106 * now we want the XSDT and RSDT to always be in sync in coreboot.
107 */
108 if (xsdt) {
Uwe Hermann622824c2010-11-19 15:14:42 +0000109 /* Add table to the XSDT. */
Stefan Reinauerdefee172015-06-18 01:11:19 -0700110 xsdt->entry[i] = (u64)(uintptr_t)table;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000111
Uwe Hermann622824c2010-11-19 15:14:42 +0000112 /* Fix XSDT length. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000113 xsdt->header.length = sizeof(acpi_header_t) +
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300114 (sizeof(u64) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000115
Uwe Hermann622824c2010-11-19 15:14:42 +0000116 /* Re-calculate checksum. */
117 xsdt->header.checksum = 0;
118 xsdt->header.checksum = acpi_checksum((u8 *)xsdt,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300119 xsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000120 }
121
Uwe Hermann622824c2010-11-19 15:14:42 +0000122 printk(BIOS_DEBUG, "ACPI: added table %d/%d, length now %d\n",
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300123 i + 1, entries_num, rsdt->header.length);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000124}
125
Uwe Hermann622824c2010-11-19 15:14:42 +0000126int acpi_create_mcfg_mmconfig(acpi_mcfg_mmconfig_t *mmconfig, u32 base,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300127 u16 seg_nr, u8 start, u8 end)
Stefan Reinauer26d431a2009-01-20 19:17:51 +0000128{
Vladimir Serbinenko60fccdc2014-10-05 11:05:16 +0200129 memset(mmconfig, 0, sizeof(*mmconfig));
Rudolf Mareke6409f22007-11-03 12:50:26 +0000130 mmconfig->base_address = base;
131 mmconfig->base_reserved = 0;
132 mmconfig->pci_segment_group_number = seg_nr;
133 mmconfig->start_bus_number = start;
134 mmconfig->end_bus_number = end;
Uwe Hermann622824c2010-11-19 15:14:42 +0000135
136 return sizeof(acpi_mcfg_mmconfig_t);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000137}
138
Stefan Reinauer777224c2005-01-19 14:06:41 +0000139int acpi_create_madt_lapic(acpi_madt_lapic_t *lapic, u8 cpu, u8 apic)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000140{
Uwe Hermann622824c2010-11-19 15:14:42 +0000141 lapic->type = 0; /* Local APIC structure */
142 lapic->length = sizeof(acpi_madt_lapic_t);
143 lapic->flags = (1 << 0); /* Processor/LAPIC enabled */
144 lapic->processor_id = cpu;
145 lapic->apic_id = apic;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000146
Uwe Hermann622824c2010-11-19 15:14:42 +0000147 return lapic->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000148}
149
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000150unsigned long acpi_create_madt_lapics(unsigned long current)
151{
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100152 struct device *cpu;
Werner Zehdb561e62019-02-19 13:39:56 +0100153 int index, apic_ids[CONFIG_MAX_CPUS], num_cpus = 0;
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000154
Uwe Hermann622824c2010-11-19 15:14:42 +0000155 for (cpu = all_devices; cpu; cpu = cpu->next) {
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000156 if ((cpu->path.type != DEVICE_PATH_APIC) ||
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800157 (cpu->bus->dev->path.type != DEVICE_PATH_CPU_CLUSTER)) {
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000158 continue;
159 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000160 if (!cpu->enabled)
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000161 continue;
Werner Zehdb561e62019-02-19 13:39:56 +0100162 if (num_cpus >= ARRAY_SIZE(apic_ids))
163 break;
164 apic_ids[num_cpus++] = cpu->path.apic.apic_id;
165 }
166 bubblesort(apic_ids, num_cpus, NUM_ASCENDING);
167 for (index = 0; index < num_cpus; index++) {
Uwe Hermann622824c2010-11-19 15:14:42 +0000168 current += acpi_create_madt_lapic((acpi_madt_lapic_t *)current,
Werner Zehdb561e62019-02-19 13:39:56 +0100169 index, apic_ids[index]);
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000170 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000171
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000172 return current;
173}
174
Uwe Hermann622824c2010-11-19 15:14:42 +0000175int acpi_create_madt_ioapic(acpi_madt_ioapic_t *ioapic, u8 id, u32 addr,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300176 u32 gsi_base)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000177{
Uwe Hermann622824c2010-11-19 15:14:42 +0000178 ioapic->type = 1; /* I/O APIC structure */
179 ioapic->length = sizeof(acpi_madt_ioapic_t);
180 ioapic->reserved = 0x00;
181 ioapic->gsi_base = gsi_base;
182 ioapic->ioapic_id = id;
183 ioapic->ioapic_addr = addr;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000184
Uwe Hermann622824c2010-11-19 15:14:42 +0000185 return ioapic->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000186}
187
Stefan Reinauer777224c2005-01-19 14:06:41 +0000188int acpi_create_madt_irqoverride(acpi_madt_irqoverride_t *irqoverride,
Stefan Reinauer06feb882004-02-03 16:11:35 +0000189 u8 bus, u8 source, u32 gsirq, u16 flags)
190{
Uwe Hermann622824c2010-11-19 15:14:42 +0000191 irqoverride->type = 2; /* Interrupt source override */
192 irqoverride->length = sizeof(acpi_madt_irqoverride_t);
193 irqoverride->bus = bus;
194 irqoverride->source = source;
195 irqoverride->gsirq = gsirq;
196 irqoverride->flags = flags;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000197
Uwe Hermann622824c2010-11-19 15:14:42 +0000198 return irqoverride->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000199}
200
Stefan Reinauer777224c2005-01-19 14:06:41 +0000201int acpi_create_madt_lapic_nmi(acpi_madt_lapic_nmi_t *lapic_nmi, u8 cpu,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300202 u16 flags, u8 lint)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000203{
Uwe Hermann622824c2010-11-19 15:14:42 +0000204 lapic_nmi->type = 4; /* Local APIC NMI structure */
205 lapic_nmi->length = sizeof(acpi_madt_lapic_nmi_t);
206 lapic_nmi->flags = flags;
207 lapic_nmi->processor_id = cpu;
208 lapic_nmi->lint = lint;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000209
Uwe Hermann622824c2010-11-19 15:14:42 +0000210 return lapic_nmi->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000211}
212
Stefan Reinauer777224c2005-01-19 14:06:41 +0000213void acpi_create_madt(acpi_madt_t *madt)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000214{
Uwe Hermann622824c2010-11-19 15:14:42 +0000215 acpi_header_t *header = &(madt->header);
216 unsigned long current = (unsigned long)madt + sizeof(acpi_madt_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000217
Stefan Reinauer06feb882004-02-03 16:11:35 +0000218 memset((void *)madt, 0, sizeof(acpi_madt_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000219
Uwe Hermann622824c2010-11-19 15:14:42 +0000220 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000221 memcpy(header->signature, "APIC", 4);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000222 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000223 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000224 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000225
Stefan Reinauer06feb882004-02-03 16:11:35 +0000226 header->length = sizeof(acpi_madt_t);
Marc Jones93a51762018-08-22 18:57:24 -0600227 header->revision = get_acpi_table_revision(MADT);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000228
Uwe Hermann622824c2010-11-19 15:14:42 +0000229 madt->lapic_addr = LOCAL_APIC_ADDR;
230 madt->flags = 0x1; /* PCAT_COMPAT */
Stefan Reinauer06feb882004-02-03 16:11:35 +0000231
Stefan Reinauerf622d592005-11-26 16:56:05 +0000232 current = acpi_fill_madt(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000233
Uwe Hermann622824c2010-11-19 15:14:42 +0000234 /* (Re)calculate length and checksum. */
235 header->length = current - (unsigned long)madt;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000236
Uwe Hermann622824c2010-11-19 15:14:42 +0000237 header->checksum = acpi_checksum((void *)madt, header->length);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000238}
239
Uwe Hermann622824c2010-11-19 15:14:42 +0000240/* MCFG is defined in the PCI Firmware Specification 3.0. */
Rudolf Mareke6409f22007-11-03 12:50:26 +0000241void acpi_create_mcfg(acpi_mcfg_t *mcfg)
242{
Uwe Hermann622824c2010-11-19 15:14:42 +0000243 acpi_header_t *header = &(mcfg->header);
244 unsigned long current = (unsigned long)mcfg + sizeof(acpi_mcfg_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000245
Rudolf Mareke6409f22007-11-03 12:50:26 +0000246 memset((void *)mcfg, 0, sizeof(acpi_mcfg_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000247
Uwe Hermann622824c2010-11-19 15:14:42 +0000248 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000249 memcpy(header->signature, "MCFG", 4);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000250 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000251 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000252 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000253
Rudolf Mareke6409f22007-11-03 12:50:26 +0000254 header->length = sizeof(acpi_mcfg_t);
Marc Jones93a51762018-08-22 18:57:24 -0600255 header->revision = get_acpi_table_revision(MCFG);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000256
257 current = acpi_fill_mcfg(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000258
Uwe Hermann622824c2010-11-19 15:14:42 +0000259 /* (Re)calculate length and checksum. */
260 header->length = current - (unsigned long)mcfg;
261 header->checksum = acpi_checksum((void *)mcfg, header->length);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000262}
263
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200264static void *get_tcpa_log(u32 *size)
265{
266 const struct cbmem_entry *ce;
267 const u32 tcpa_default_log_len = 0x10000;
268 void *lasa;
Philipp Deppenwiese791ba972018-07-30 01:15:20 +0200269 ce = cbmem_entry_find(CBMEM_ID_TCPA_TCG_LOG);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200270 if (ce) {
271 lasa = cbmem_entry_start(ce);
272 *size = cbmem_entry_size(ce);
273 printk(BIOS_DEBUG, "TCPA log found at %p\n", lasa);
274 return lasa;
275 }
Philipp Deppenwiese791ba972018-07-30 01:15:20 +0200276 lasa = cbmem_add(CBMEM_ID_TCPA_TCG_LOG, tcpa_default_log_len);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200277 if (!lasa) {
278 printk(BIOS_ERR, "TCPA log creation failed\n");
279 return NULL;
280 }
281
282 printk(BIOS_DEBUG, "TCPA log created at %p\n", lasa);
Lee Leahy024b13d2017-03-16 13:41:11 -0700283 memset(lasa, 0, tcpa_default_log_len);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200284
285 *size = tcpa_default_log_len;
286 return lasa;
287}
288
289static void acpi_create_tcpa(acpi_tcpa_t *tcpa)
290{
291 acpi_header_t *header = &(tcpa->header);
292 u32 tcpa_log_len;
293 void *lasa;
294
295 memset((void *)tcpa, 0, sizeof(acpi_tcpa_t));
296
297 lasa = get_tcpa_log(&tcpa_log_len);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700298 if (!lasa)
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200299 return;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200300
301 /* Fill out header fields. */
302 memcpy(header->signature, "TCPA", 4);
303 memcpy(header->oem_id, OEM_ID, 6);
304 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
305 memcpy(header->asl_compiler_id, ASLC, 4);
306
307 header->length = sizeof(acpi_tcpa_t);
Marc Jones93a51762018-08-22 18:57:24 -0600308 header->revision = get_acpi_table_revision(TCPA);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200309
310 tcpa->platform_class = 0;
311 tcpa->laml = tcpa_log_len;
Stefan Reinauerdefee172015-06-18 01:11:19 -0700312 tcpa->lasa = (uintptr_t) lasa;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200313
314 /* Calculate checksum. */
315 header->checksum = acpi_checksum((void *)tcpa, header->length);
316}
317
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100318static void *get_tpm2_log(u32 *size)
319{
320 const struct cbmem_entry *ce;
321 const u32 tpm2_default_log_len = 0x10000;
322 void *lasa;
323 ce = cbmem_entry_find(CBMEM_ID_TPM2_TCG_LOG);
324 if (ce) {
325 lasa = cbmem_entry_start(ce);
326 *size = cbmem_entry_size(ce);
327 printk(BIOS_DEBUG, "TPM2 log found at %p\n", lasa);
328 return lasa;
329 }
330 lasa = cbmem_add(CBMEM_ID_TPM2_TCG_LOG, tpm2_default_log_len);
331 if (!lasa) {
332 printk(BIOS_ERR, "TPM2 log creation failed\n");
333 return NULL;
334 }
335
336 printk(BIOS_DEBUG, "TPM2 log created at %p\n", lasa);
337 memset(lasa, 0, tpm2_default_log_len);
338
339 *size = tpm2_default_log_len;
340 return lasa;
341}
342
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200343static void acpi_create_tpm2(acpi_tpm2_t *tpm2)
344{
345 acpi_header_t *header = &(tpm2->header);
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100346 u32 tpm2_log_len;
347 void *lasa;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200348
349 memset((void *)tpm2, 0, sizeof(acpi_tpm2_t));
350
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100351 /*
352 * Some payloads like SeaBIOS depend on log area to use TPM2.
353 * Get the memory size and address of TPM2 log area or initialize it.
354 */
355 lasa = get_tpm2_log(&tpm2_log_len);
356 if (!lasa)
357 tpm2_log_len = 0;
358
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200359 /* Fill out header fields. */
360 memcpy(header->signature, "TPM2", 4);
361 memcpy(header->oem_id, OEM_ID, 6);
362 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
363 memcpy(header->asl_compiler_id, ASLC, 4);
364
365 header->length = sizeof(acpi_tpm2_t);
366 header->revision = get_acpi_table_revision(TPM2);
367
368 /* Hard to detect for coreboot. Just set it to 0 */
369 tpm2->platform_class = 0;
370 /* Must be set to 0 for TIS interface support */
371 tpm2->control_area = 0;
372 /* coreboot only supports the TIS interface driver. */
373 tpm2->start_method = 6;
374 memset(tpm2->msp, 0, sizeof(tpm2->msp));
375
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100376 /* Fill the log area size and start address fields. */
377 tpm2->laml = tpm2_log_len;
378 tpm2->lasa = (uintptr_t) lasa;
379
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200380 /* Calculate checksum. */
381 header->checksum = acpi_checksum((void *)tpm2, header->length);
382}
383
Duncan Laurie37319032016-05-26 12:47:05 -0700384static void acpi_ssdt_write_cbtable(void)
385{
386 const struct cbmem_entry *cbtable;
387 uintptr_t base;
388 uint32_t size;
389
390 cbtable = cbmem_entry_find(CBMEM_ID_CBTABLE);
391 if (!cbtable)
392 return;
393 base = (uintptr_t)cbmem_entry_start(cbtable);
394 size = cbmem_entry_size(cbtable);
395
396 acpigen_write_device("CTBL");
397 acpigen_write_coreboot_hid(COREBOOT_ACPI_ID_CBTABLE);
398 acpigen_write_name_integer("_UID", 0);
David Wu5dff3962018-03-21 16:48:53 +0800399 acpigen_write_STA(ACPI_STATUS_DEVICE_HIDDEN_ON);
Duncan Laurie37319032016-05-26 12:47:05 -0700400 acpigen_write_name("_CRS");
401 acpigen_write_resourcetemplate_header();
402 acpigen_write_mem32fixed(0, base, size);
403 acpigen_write_resourcetemplate_footer();
404 acpigen_pop_len();
405}
406
Myles Watson3fe6b702009-10-09 20:13:43 +0000407void acpi_create_ssdt_generator(acpi_header_t *ssdt, const char *oem_table_id)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000408{
Uwe Hermann622824c2010-11-19 15:14:42 +0000409 unsigned long current = (unsigned long)ssdt + sizeof(acpi_header_t);
410
Rudolf Marek293b5f52009-02-01 18:35:15 +0000411 memset((void *)ssdt, 0, sizeof(acpi_header_t));
Uwe Hermann622824c2010-11-19 15:14:42 +0000412
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000413 memcpy(&ssdt->signature, "SSDT", 4);
Marc Jones93a51762018-08-22 18:57:24 -0600414 ssdt->revision = get_acpi_table_revision(SSDT);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000415 memcpy(&ssdt->oem_id, OEM_ID, 6);
416 memcpy(&ssdt->oem_table_id, oem_table_id, 8);
417 ssdt->oem_revision = 42;
Uwe Hermann622824c2010-11-19 15:14:42 +0000418 memcpy(&ssdt->asl_compiler_id, ASLC, 4);
Elyes HAOUAS26071aa2019-02-15 08:21:33 +0100419 ssdt->asl_compiler_revision = asl_revision;
Rudolf Marek293b5f52009-02-01 18:35:15 +0000420 ssdt->length = sizeof(acpi_header_t);
421
Stefan Reinauer8dcd50b2009-03-06 17:24:29 +0000422 acpigen_set_current((char *) current);
Duncan Laurie37319032016-05-26 12:47:05 -0700423
424 /* Write object to declare coreboot tables */
425 acpi_ssdt_write_cbtable();
426
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200427 {
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100428 struct device *dev;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200429 for (dev = all_devices; dev; dev = dev->next)
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700430 if (dev->ops && dev->ops->acpi_fill_ssdt_generator)
Alexander Couzens5eea4582015-04-12 22:18:55 +0200431 dev->ops->acpi_fill_ssdt_generator(dev);
Vladimir Serbinenko0a669912014-10-05 14:34:17 +0200432 current = (unsigned long) acpigen_get_current();
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200433 }
Rudolf Marek293b5f52009-02-01 18:35:15 +0000434
Uwe Hermann622824c2010-11-19 15:14:42 +0000435 /* (Re)calculate length and checksum. */
Rudolf Marek293b5f52009-02-01 18:35:15 +0000436 ssdt->length = current - (unsigned long)ssdt;
437 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
438}
439
Stefan Reinauerf622d592005-11-26 16:56:05 +0000440int acpi_create_srat_lapic(acpi_srat_lapic_t *lapic, u8 node, u8 apic)
441{
Myles Watson0b4c9f02009-03-10 18:06:47 +0000442 memset((void *)lapic, 0, sizeof(acpi_srat_lapic_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000443
Uwe Hermann622824c2010-11-19 15:14:42 +0000444 lapic->type = 0; /* Processor local APIC/SAPIC affinity structure */
445 lapic->length = sizeof(acpi_srat_lapic_t);
446 lapic->flags = (1 << 0); /* Enabled (the use of this structure). */
447 lapic->proximity_domain_7_0 = node;
448 /* TODO: proximity_domain_31_8, local SAPIC EID, clock domain. */
449 lapic->apic_id = apic;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000450
Uwe Hermann622824c2010-11-19 15:14:42 +0000451 return lapic->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000452}
453
Uwe Hermann622824c2010-11-19 15:14:42 +0000454int 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 +0300455 u32 flags)
Stefan Reinauerf622d592005-11-26 16:56:05 +0000456{
Uwe Hermann622824c2010-11-19 15:14:42 +0000457 mem->type = 1; /* Memory affinity structure */
458 mem->length = sizeof(acpi_srat_mem_t);
459 mem->base_address_low = (basek << 10);
460 mem->base_address_high = (basek >> (32 - 10));
461 mem->length_low = (sizek << 10);
462 mem->length_high = (sizek >> (32 - 10));
463 mem->proximity_domain = node;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000464 mem->flags = flags;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000465
Uwe Hermann622824c2010-11-19 15:14:42 +0000466 return mem->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000467}
468
Uwe Hermann622824c2010-11-19 15:14:42 +0000469/* http://www.microsoft.com/whdc/system/sysinternals/sratdwn.mspx */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +0200470void acpi_create_srat(acpi_srat_t *srat,
471 unsigned long (*acpi_fill_srat)(unsigned long current))
Stefan Reinauerf622d592005-11-26 16:56:05 +0000472{
Uwe Hermann622824c2010-11-19 15:14:42 +0000473 acpi_header_t *header = &(srat->header);
474 unsigned long current = (unsigned long)srat + sizeof(acpi_srat_t);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000475
Uwe Hermann622824c2010-11-19 15:14:42 +0000476 memset((void *)srat, 0, sizeof(acpi_srat_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000477
Uwe Hermann622824c2010-11-19 15:14:42 +0000478 /* Fill out header fields. */
479 memcpy(header->signature, "SRAT", 4);
480 memcpy(header->oem_id, OEM_ID, 6);
481 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
482 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000483
Uwe Hermann622824c2010-11-19 15:14:42 +0000484 header->length = sizeof(acpi_srat_t);
Marc Jones93a51762018-08-22 18:57:24 -0600485 header->revision = get_acpi_table_revision(SRAT);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000486
Uwe Hermann622824c2010-11-19 15:14:42 +0000487 srat->resv = 1; /* Spec: Reserved to 1 for backwards compatibility. */
Stefan Reinauerf622d592005-11-26 16:56:05 +0000488
Uwe Hermann622824c2010-11-19 15:14:42 +0000489 current = acpi_fill_srat(current);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000490
Uwe Hermann622824c2010-11-19 15:14:42 +0000491 /* (Re)calculate length and checksum. */
492 header->length = current - (unsigned long)srat;
493 header->checksum = acpi_checksum((void *)srat, header->length);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000494}
495
Nico Hubere561f352015-10-26 11:51:25 +0100496void acpi_create_dmar(acpi_dmar_t *dmar, enum dmar_flags flags,
Lee Leahy024b13d2017-03-16 13:41:11 -0700497 unsigned long (*acpi_fill_dmar)(unsigned long))
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200498{
499 acpi_header_t *header = &(dmar->header);
500 unsigned long current = (unsigned long)dmar + sizeof(acpi_dmar_t);
501
502 memset((void *)dmar, 0, sizeof(acpi_dmar_t));
503
504 /* Fill out header fields. */
505 memcpy(header->signature, "DMAR", 4);
506 memcpy(header->oem_id, OEM_ID, 6);
507 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
508 memcpy(header->asl_compiler_id, ASLC, 4);
509
510 header->length = sizeof(acpi_dmar_t);
Marc Jones93a51762018-08-22 18:57:24 -0600511 header->revision = get_acpi_table_revision(DMAR);
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200512
Jacob Laskaf3f654d2016-03-15 21:53:27 -0500513 dmar->host_address_width = cpu_phys_address_size() - 1;
Nico Hubere561f352015-10-26 11:51:25 +0100514 dmar->flags = flags;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200515
516 current = acpi_fill_dmar(current);
517
518 /* (Re)calculate length and checksum. */
519 header->length = current - (unsigned long)dmar;
520 header->checksum = acpi_checksum((void *)dmar, header->length);
521}
522
523unsigned long acpi_create_dmar_drhd(unsigned long current, u8 flags,
Matt DeVillier7866d492018-03-29 14:59:57 +0200524 u16 segment, u64 bar)
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200525{
526 dmar_entry_t *drhd = (dmar_entry_t *)current;
527 memset(drhd, 0, sizeof(*drhd));
528 drhd->type = DMAR_DRHD;
529 drhd->length = sizeof(*drhd); /* will be fixed up later */
530 drhd->flags = flags;
531 drhd->segment = segment;
532 drhd->bar = bar;
533
534 return drhd->length;
535}
536
Matt DeVillier7866d492018-03-29 14:59:57 +0200537unsigned long acpi_create_dmar_rmrr(unsigned long current, u16 segment,
538 u64 bar, u64 limit)
539{
540 dmar_rmrr_entry_t *rmrr = (dmar_rmrr_entry_t *)current;
541 memset(rmrr, 0, sizeof(*rmrr));
542 rmrr->type = DMAR_RMRR;
543 rmrr->length = sizeof(*rmrr); /* will be fixed up later */
544 rmrr->segment = segment;
545 rmrr->bar = bar;
546 rmrr->limit = limit;
547
548 return rmrr->length;
549}
550
Werner Zehd4d76952016-07-27 06:56:36 +0200551unsigned long acpi_create_dmar_atsr(unsigned long current, u8 flags,
552 u16 segment)
553{
554 dmar_atsr_entry_t *atsr = (dmar_atsr_entry_t *)current;
555 memset(atsr, 0, sizeof(*atsr));
556 atsr->type = DMAR_ATSR;
557 atsr->length = sizeof(*atsr); /* will be fixed up later */
558 atsr->flags = flags;
559 atsr->segment = segment;
560
561 return atsr->length;
562}
563
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200564void acpi_dmar_drhd_fixup(unsigned long base, unsigned long current)
565{
566 dmar_entry_t *drhd = (dmar_entry_t *)base;
567 drhd->length = current - base;
568}
569
Matt DeVillier7866d492018-03-29 14:59:57 +0200570void acpi_dmar_rmrr_fixup(unsigned long base, unsigned long current)
571{
572 dmar_rmrr_entry_t *rmrr = (dmar_rmrr_entry_t *)base;
573 rmrr->length = current - base;
574}
575
Werner Zehd4d76952016-07-27 06:56:36 +0200576void acpi_dmar_atsr_fixup(unsigned long base, unsigned long current)
577{
578 dmar_atsr_entry_t *atsr = (dmar_atsr_entry_t *)base;
579 atsr->length = current - base;
580}
581
Matt DeVillier7866d492018-03-29 14:59:57 +0200582static unsigned long acpi_create_dmar_ds(unsigned long current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100583 enum dev_scope_type type, u8 enumeration_id, u8 bus, u8 dev, u8 fn)
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200584{
Nico Huber6c4751d2015-10-26 12:03:54 +0100585 /* we don't support longer paths yet */
586 const size_t dev_scope_length = sizeof(dev_scope_t) + 2;
587
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200588 dev_scope_t *ds = (dev_scope_t *)current;
Nico Huber6c4751d2015-10-26 12:03:54 +0100589 memset(ds, 0, dev_scope_length);
590 ds->type = type;
591 ds->length = dev_scope_length;
592 ds->enumeration = enumeration_id;
593 ds->start_bus = bus;
594 ds->path[0].dev = dev;
595 ds->path[0].fn = fn;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200596
597 return ds->length;
598}
599
Matt DeVillier7866d492018-03-29 14:59:57 +0200600unsigned long acpi_create_dmar_ds_pci_br(unsigned long current, u8 bus,
Werner Zeh21a5bff2016-07-27 07:07:20 +0200601 u8 dev, u8 fn)
602{
Matt DeVillier7866d492018-03-29 14:59:57 +0200603 return acpi_create_dmar_ds(current,
Werner Zeh21a5bff2016-07-27 07:07:20 +0200604 SCOPE_PCI_SUB, 0, bus, dev, fn);
605}
606
Matt DeVillier7866d492018-03-29 14:59:57 +0200607unsigned long acpi_create_dmar_ds_pci(unsigned long current, u8 bus,
Nico Huber6c4751d2015-10-26 12:03:54 +0100608 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_PCI_ENDPOINT, 0, bus, dev, fn);
612}
613
Matt DeVillier7866d492018-03-29 14:59:57 +0200614unsigned long acpi_create_dmar_ds_ioapic(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_IOAPIC, enumeration_id, bus, dev, fn);
619}
620
Matt DeVillier7866d492018-03-29 14:59:57 +0200621unsigned long acpi_create_dmar_ds_msi_hpet(unsigned long current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100622 u8 enumeration_id, u8 bus, u8 dev, u8 fn)
623{
Matt DeVillier7866d492018-03-29 14:59:57 +0200624 return acpi_create_dmar_ds(current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100625 SCOPE_MSI_HPET, enumeration_id, bus, dev, fn);
626}
627
Uwe Hermann622824c2010-11-19 15:14:42 +0000628/* http://h21007.www2.hp.com/portal/download/files/unprot/Itanium/slit.pdf */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +0200629void acpi_create_slit(acpi_slit_t *slit,
630 unsigned long (*acpi_fill_slit)(unsigned long current))
Yinghai Lud4b278c2006-10-04 20:46:15 +0000631{
Uwe Hermann622824c2010-11-19 15:14:42 +0000632 acpi_header_t *header = &(slit->header);
633 unsigned long current = (unsigned long)slit + sizeof(acpi_slit_t);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000634
Uwe Hermann622824c2010-11-19 15:14:42 +0000635 memset((void *)slit, 0, sizeof(acpi_slit_t));
Yinghai Lud4b278c2006-10-04 20:46:15 +0000636
Uwe Hermann622824c2010-11-19 15:14:42 +0000637 /* Fill out header fields. */
638 memcpy(header->signature, "SLIT", 4);
639 memcpy(header->oem_id, OEM_ID, 6);
640 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
641 memcpy(header->asl_compiler_id, ASLC, 4);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000642
Uwe Hermann622824c2010-11-19 15:14:42 +0000643 header->length = sizeof(acpi_slit_t);
Marc Jones93a51762018-08-22 18:57:24 -0600644 header->revision = get_acpi_table_revision(SLIT);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000645
Uwe Hermann622824c2010-11-19 15:14:42 +0000646 current = acpi_fill_slit(current);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000647
Uwe Hermann622824c2010-11-19 15:14:42 +0000648 /* (Re)calculate length and checksum. */
649 header->length = current - (unsigned long)slit;
650 header->checksum = acpi_checksum((void *)slit, header->length);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000651}
652
Uwe Hermann622824c2010-11-19 15:14:42 +0000653/* http://www.intel.com/hardwaredesign/hpetspec_1.pdf */
Stefan Reinauer777224c2005-01-19 14:06:41 +0000654void acpi_create_hpet(acpi_hpet_t *hpet)
Stefan Reinauer688b3852004-01-28 16:56:14 +0000655{
Uwe Hermann622824c2010-11-19 15:14:42 +0000656 acpi_header_t *header = &(hpet->header);
657 acpi_addr_t *addr = &(hpet->addr);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000658
Stefan Reinauera7648c22004-01-29 17:31:34 +0000659 memset((void *)hpet, 0, sizeof(acpi_hpet_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000660
Uwe Hermann622824c2010-11-19 15:14:42 +0000661 /* Fill out header fields. */
Stefan Reinauer4704dc52009-07-22 01:11:37 +0000662 memcpy(header->signature, "HPET", 4);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000663 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000664 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000665 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000666
Stefan Reinauer688b3852004-01-28 16:56:14 +0000667 header->length = sizeof(acpi_hpet_t);
Marc Jones93a51762018-08-22 18:57:24 -0600668 header->revision = get_acpi_table_revision(HPET);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000669
Uwe Hermann622824c2010-11-19 15:14:42 +0000670 /* Fill out HPET address. */
671 addr->space_id = 0; /* Memory */
672 addr->bit_width = 64;
673 addr->bit_offset = 0;
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200674 addr->addrl = CONFIG_HPET_ADDRESS & 0xffffffff;
675 addr->addrh = ((unsigned long long)CONFIG_HPET_ADDRESS) >> 32;
Stefan Reinauer688b3852004-01-28 16:56:14 +0000676
Lee Leahy024b13d2017-03-16 13:41:11 -0700677 hpet->id = *(unsigned int *)CONFIG_HPET_ADDRESS;
Uwe Hermann622824c2010-11-19 15:14:42 +0000678 hpet->number = 0;
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200679 hpet->min_tick = CONFIG_HPET_MIN_TICKS;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000680
Uwe Hermann622824c2010-11-19 15:14:42 +0000681 header->checksum = acpi_checksum((void *)hpet, sizeof(acpi_hpet_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000682}
Uwe Hermann622824c2010-11-19 15:14:42 +0000683
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200684void acpi_create_vfct(struct device *device,
685 struct acpi_vfct *vfct,
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700686 unsigned long (*acpi_fill_vfct)(struct device *device,
687 struct acpi_vfct *vfct_struct, unsigned long current))
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200688{
689 acpi_header_t *header = &(vfct->header);
690 unsigned long current = (unsigned long)vfct + sizeof(struct acpi_vfct);
691
692 memset((void *)vfct, 0, sizeof(struct acpi_vfct));
693
694 /* Fill out header fields. */
695 memcpy(header->signature, "VFCT", 4);
696 memcpy(header->oem_id, OEM_ID, 6);
697 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
698 memcpy(header->asl_compiler_id, ASLC, 4);
699
700 header->length = sizeof(struct acpi_vfct);
Marc Jones93a51762018-08-22 18:57:24 -0600701 header->revision = get_acpi_table_revision(VFCT);
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200702
703 current = acpi_fill_vfct(device, vfct, current);
704
705 /* (Re)calculate length and checksum. */
706 header->length = current - (unsigned long)vfct;
707 header->checksum = acpi_checksum((void *)vfct, header->length);
708}
709
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500710void acpi_create_ivrs(acpi_ivrs_t *ivrs,
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700711 unsigned long (*acpi_fill_ivrs)(acpi_ivrs_t *ivrs_struct,
712 unsigned long current))
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500713{
714 acpi_header_t *header = &(ivrs->header);
715 unsigned long current = (unsigned long)ivrs + sizeof(acpi_ivrs_t);
716
717 memset((void *)ivrs, 0, sizeof(acpi_ivrs_t));
718
719 /* Fill out header fields. */
720 memcpy(header->signature, "IVRS", 4);
721 memcpy(header->oem_id, OEM_ID, 6);
722 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
723 memcpy(header->asl_compiler_id, ASLC, 4);
724
725 header->length = sizeof(acpi_ivrs_t);
Marc Jones93a51762018-08-22 18:57:24 -0600726 header->revision = get_acpi_table_revision(IVRS);
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500727
728 current = acpi_fill_ivrs(ivrs, current);
729
730 /* (Re)calculate length and checksum. */
731 header->length = current - (unsigned long)ivrs;
732 header->checksum = acpi_checksum((void *)ivrs, header->length);
733}
734
Elyes HAOUAS9dd89cd2018-05-04 17:56:47 +0200735unsigned long acpi_write_hpet(struct device *device, unsigned long current,
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700736 acpi_rsdp_t *rsdp)
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200737{
738 acpi_hpet_t *hpet;
739
740 /*
741 * We explicitly add these tables later on:
742 */
743 printk(BIOS_DEBUG, "ACPI: * HPET\n");
744
745 hpet = (acpi_hpet_t *) current;
746 current += sizeof(acpi_hpet_t);
747 current = ALIGN(current, 16);
748 acpi_create_hpet(hpet);
749 acpi_add_table(rsdp, hpet);
750
751 return current;
752}
753
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800754void acpi_create_dbg2(acpi_dbg2_header_t *dbg2,
755 int port_type, int port_subtype,
756 acpi_addr_t *address, uint32_t address_size,
757 const char *device_path)
758{
759 uintptr_t current;
760 acpi_dbg2_device_t *device;
761 uint32_t *dbg2_addr_size;
762 acpi_header_t *header;
763 size_t path_len;
764 const char *path;
765 char *namespace;
766
767 /* Fill out header fields. */
768 current = (uintptr_t)dbg2;
769 memset(dbg2, 0, sizeof(acpi_dbg2_header_t));
770 header = &(dbg2->header);
Marc Jones93a51762018-08-22 18:57:24 -0600771 header->revision = get_acpi_table_revision(DBG2);
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800772 memcpy(header->signature, "DBG2", 4);
773 memcpy(header->oem_id, OEM_ID, 6);
774 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
775 memcpy(header->asl_compiler_id, ASLC, 4);
776
777 /* One debug device defined */
778 dbg2->devices_offset = sizeof(acpi_dbg2_header_t);
779 dbg2->devices_count = 1;
780 current += sizeof(acpi_dbg2_header_t);
781
782 /* Device comes after the header */
783 device = (acpi_dbg2_device_t *)current;
784 memset(device, 0, sizeof(acpi_dbg2_device_t));
785 current += sizeof(acpi_dbg2_device_t);
786
787 device->revision = 0;
788 device->address_count = 1;
789 device->port_type = port_type;
790 device->port_subtype = port_subtype;
791
792 /* Base Address comes after device structure */
793 memcpy((void *)current, address, sizeof(acpi_addr_t));
794 device->base_address_offset = current - (uintptr_t)device;
795 current += sizeof(acpi_addr_t);
796
797 /* Address Size comes after address structure */
798 dbg2_addr_size = (uint32_t *)current;
799 device->address_size_offset = current - (uintptr_t)device;
800 *dbg2_addr_size = address_size;
801 current += sizeof(uint32_t);
802
803 /* Namespace string comes last, use '.' if not provided */
804 path = device_path ? : ".";
805 /* Namespace string length includes NULL terminator */
806 path_len = strlen(path) + 1;
807 namespace = (char *)current;
808 device->namespace_string_length = path_len;
809 device->namespace_string_offset = current - (uintptr_t)device;
810 strncpy(namespace, path, path_len);
811 current += path_len;
812
813 /* Update structure lengths and checksum */
814 device->length = current - (uintptr_t)device;
815 header->length = current - (uintptr_t)dbg2;
816 header->checksum = acpi_checksum((uint8_t *)dbg2, header->length);
817}
818
819unsigned long acpi_write_dbg2_pci_uart(acpi_rsdp_t *rsdp, unsigned long current,
820 struct device *dev, uint8_t access_size)
821{
822 acpi_dbg2_header_t *dbg2 = (acpi_dbg2_header_t *)current;
823 struct resource *res;
824 acpi_addr_t address;
825
826 if (!dev) {
827 printk(BIOS_ERR, "%s: Device not found\n", __func__);
828 return current;
829 }
Mario Scheithauerf1eb0ea2017-11-21 13:52:53 +0100830 if (!dev->enabled) {
831 printk(BIOS_INFO, "%s: Device not enabled\n", __func__);
832 return current;
833 }
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800834 res = find_resource(dev, PCI_BASE_ADDRESS_0);
835 if (!res) {
836 printk(BIOS_ERR, "%s: Unable to find resource for %s\n",
837 __func__, dev_path(dev));
838 return current;
839 }
840
841 memset(&address, 0, sizeof(address));
842 if (res->flags & IORESOURCE_IO)
843 address.space_id = ACPI_ADDRESS_SPACE_IO;
844 else if (res->flags & IORESOURCE_MEM)
845 address.space_id = ACPI_ADDRESS_SPACE_MEMORY;
846 else {
847 printk(BIOS_ERR, "%s: Unknown address space type\n", __func__);
848 return current;
849 }
850
851 address.addrl = (uint32_t)res->base;
852 address.addrh = (uint32_t)((res->base >> 32) & 0xffffffff);
853 address.access_size = access_size;
854
855 acpi_create_dbg2(dbg2,
856 ACPI_DBG2_PORT_SERIAL,
857 ACPI_DBG2_PORT_SERIAL_16550,
858 &address, res->size,
859 acpi_device_path(dev));
860
861 if (dbg2->header.length) {
862 current += dbg2->header.length;
863 current = acpi_align_current(current);
864 acpi_add_table(rsdp, dbg2);
865 }
866
867 return current;
868}
869
Stefan Reinauer777224c2005-01-19 14:06:41 +0000870void acpi_create_facs(acpi_facs_t *facs)
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000871{
Uwe Hermann622824c2010-11-19 15:14:42 +0000872 memset((void *)facs, 0, sizeof(acpi_facs_t));
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000873
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000874 memcpy(facs->signature, "FACS", 4);
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000875 facs->length = sizeof(acpi_facs_t);
876 facs->hardware_signature = 0;
877 facs->firmware_waking_vector = 0;
878 facs->global_lock = 0;
879 facs->flags = 0;
880 facs->x_firmware_waking_vector_l = 0;
881 facs->x_firmware_waking_vector_h = 0;
Marc Jones93a51762018-08-22 18:57:24 -0600882 facs->version = get_acpi_table_revision(FACS);
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000883}
Stefan Reinauer777224c2005-01-19 14:06:41 +0000884
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100885static void acpi_write_rsdt(acpi_rsdt_t *rsdt, char *oem_id, char *oem_table_id)
Stefan Reinauer14e22772010-04-27 06:56:47 +0000886{
Uwe Hermann622824c2010-11-19 15:14:42 +0000887 acpi_header_t *header = &(rsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000888
Uwe Hermann622824c2010-11-19 15:14:42 +0000889 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000890 memcpy(header->signature, "RSDT", 4);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100891 memcpy(header->oem_id, oem_id, 6);
892 memcpy(header->oem_table_id, oem_table_id, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000893 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000894
Stefan Reinauer688b3852004-01-28 16:56:14 +0000895 header->length = sizeof(acpi_rsdt_t);
Marc Jones93a51762018-08-22 18:57:24 -0600896 header->revision = get_acpi_table_revision(RSDT);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000897
Uwe Hermann622824c2010-11-19 15:14:42 +0000898 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauer688b3852004-01-28 16:56:14 +0000899
Uwe Hermann622824c2010-11-19 15:14:42 +0000900 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000901 header->checksum = acpi_checksum((void *)rsdt, sizeof(acpi_rsdt_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000902}
903
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100904static void acpi_write_xsdt(acpi_xsdt_t *xsdt, char *oem_id, char *oem_table_id)
Stefan Reinauer14e22772010-04-27 06:56:47 +0000905{
Uwe Hermann622824c2010-11-19 15:14:42 +0000906 acpi_header_t *header = &(xsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000907
Uwe Hermann622824c2010-11-19 15:14:42 +0000908 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000909 memcpy(header->signature, "XSDT", 4);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100910 memcpy(header->oem_id, oem_id, 6);
911 memcpy(header->oem_table_id, oem_table_id, 8);
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000912 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000913
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000914 header->length = sizeof(acpi_xsdt_t);
Marc Jones93a51762018-08-22 18:57:24 -0600915 header->revision = get_acpi_table_revision(XSDT);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000916
Uwe Hermann622824c2010-11-19 15:14:42 +0000917 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000918
Uwe Hermann622824c2010-11-19 15:14:42 +0000919 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000920 header->checksum = acpi_checksum((void *)xsdt, sizeof(acpi_xsdt_t));
921}
922
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100923static void acpi_write_rsdp(acpi_rsdp_t *rsdp, acpi_rsdt_t *rsdt,
924 acpi_xsdt_t *xsdt, char *oem_id)
Stefan Reinauer688b3852004-01-28 16:56:14 +0000925{
Stefan Reinauerd18faac2009-11-05 18:06:43 +0000926 memset(rsdp, 0, sizeof(acpi_rsdp_t));
Uwe Hermann622824c2010-11-19 15:14:42 +0000927
Stefan Reinauer688b3852004-01-28 16:56:14 +0000928 memcpy(rsdp->signature, RSDP_SIG, 8);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100929 memcpy(rsdp->oem_id, oem_id, 6);
Uwe Hermann622824c2010-11-19 15:14:42 +0000930
931 rsdp->length = sizeof(acpi_rsdp_t);
Stefan Reinauerdefee172015-06-18 01:11:19 -0700932 rsdp->rsdt_address = (uintptr_t)rsdt;
Uwe Hermann622824c2010-11-19 15:14:42 +0000933
934 /*
935 * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2.
936 *
937 * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
938 * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
939 * revision 0).
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000940 */
941 if (xsdt == NULL) {
Uwe Hermann622824c2010-11-19 15:14:42 +0000942 rsdp->revision = 0;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000943 } else {
Stefan Reinauerdefee172015-06-18 01:11:19 -0700944 rsdp->xsdt_address = (u64)(uintptr_t)xsdt;
Marc Jones93a51762018-08-22 18:57:24 -0600945 rsdp->revision = get_acpi_table_revision(RSDP);
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000946 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000947
948 /* Calculate checksums. */
949 rsdp->checksum = acpi_checksum((void *)rsdp, 20);
950 rsdp->ext_checksum = acpi_checksum((void *)rsdp, sizeof(acpi_rsdp_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000951}
952
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700953unsigned long acpi_create_hest_error_source(acpi_hest_t *hest,
954 acpi_hest_esd_t *esd, u16 type, void *data, u16 data_len)
zbaocaf494c82012-04-13 13:57:14 +0800955{
956 acpi_header_t *header = &(hest->header);
957 acpi_hest_hen_t *hen;
958 void *pos;
959 u16 len;
960
961 pos = esd;
962 memset(pos, 0, sizeof(acpi_hest_esd_t));
963 len = 0;
964 esd->type = type; /* MCE */
965 esd->source_id = hest->error_source_count;
966 esd->flags = 0; /* FIRMWARE_FIRST */
967 esd->enabled = 1;
968 esd->prealloc_erecords = 1;
969 esd->max_section_per_record = 0x1;
970
971 len += sizeof(acpi_hest_esd_t);
972 pos = esd + 1;
973
974 switch (type) {
975 case 0: /* MCE */
976 break;
977 case 1: /* CMC */
978 hen = (acpi_hest_hen_t *) (pos);
979 memset(pos, 0, sizeof(acpi_hest_hen_t));
980 hen->type = 3; /* SCI? */
981 hen->length = sizeof(acpi_hest_hen_t);
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700982 hen->conf_we = 0; /* Configuration Write Enable. */
zbaocaf494c82012-04-13 13:57:14 +0800983 hen->poll_interval = 0;
984 hen->vector = 0;
985 hen->sw2poll_threshold_val = 0;
986 hen->sw2poll_threshold_win = 0;
987 hen->error_threshold_val = 0;
988 hen->error_threshold_win = 0;
989 len += sizeof(acpi_hest_hen_t);
990 pos = hen + 1;
991 break;
992 case 2: /* NMI */
993 case 6: /* AER Root Port */
994 case 7: /* AER Endpoint */
995 case 8: /* AER Bridge */
996 case 9: /* Generic Hardware Error Source. */
997 /* TODO: */
998 break;
999 default:
1000 printk(BIOS_DEBUG, "Invalid type of Error Source.");
1001 break;
1002 }
Lee Leahy024b13d2017-03-16 13:41:11 -07001003 hest->error_source_count++;
zbaocaf494c82012-04-13 13:57:14 +08001004
1005 memcpy(pos, data, data_len);
1006 len += data_len;
1007 header->length += len;
1008
1009 return len;
1010}
1011
1012/* ACPI 4.0 */
Vladimir Serbinenko807127f2014-11-09 13:36:18 +01001013void acpi_write_hest(acpi_hest_t *hest,
1014 unsigned long (*acpi_fill_hest)(acpi_hest_t *hest))
zbaocaf494c82012-04-13 13:57:14 +08001015{
1016 acpi_header_t *header = &(hest->header);
1017
1018 memset(hest, 0, sizeof(acpi_hest_t));
1019
1020 memcpy(header->signature, "HEST", 4);
1021 memcpy(header->oem_id, OEM_ID, 6);
1022 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1023 memcpy(header->asl_compiler_id, ASLC, 4);
1024 header->length += sizeof(acpi_hest_t);
Marc Jones93a51762018-08-22 18:57:24 -06001025 header->revision = get_acpi_table_revision(HEST);
zbaocaf494c82012-04-13 13:57:14 +08001026
1027 acpi_fill_hest(hest);
1028
1029 /* Calculate checksums. */
1030 header->checksum = acpi_checksum((void *)hest, header->length);
1031}
1032
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001033/* ACPI 3.0b */
1034void acpi_write_bert(acpi_bert_t *bert, uintptr_t region, size_t length)
1035{
1036 acpi_header_t *header = &(bert->header);
1037
1038 memset(bert, 0, sizeof(acpi_bert_t));
1039
1040 memcpy(header->signature, "BERT", 4);
1041 memcpy(header->oem_id, OEM_ID, 6);
1042 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1043 memcpy(header->asl_compiler_id, ASLC, 4);
1044 header->length += sizeof(acpi_bert_t);
1045 header->revision = get_acpi_table_revision(BERT);
1046
1047 bert->error_region = region;
1048 bert->region_length = length;
1049
1050 /* Calculate checksums. */
1051 header->checksum = acpi_checksum((void *)bert, header->length);
1052}
1053
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001054#if IS_ENABLED(CONFIG_COMMON_FADT)
Lee Leahy024b13d2017-03-16 13:41:11 -07001055void acpi_create_fadt(acpi_fadt_t *fadt, acpi_facs_t *facs, void *dsdt)
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001056{
1057 acpi_header_t *header = &(fadt->header);
1058
1059 memset((void *) fadt, 0, sizeof(acpi_fadt_t));
1060 memcpy(header->signature, "FACP", 4);
1061 header->length = sizeof(acpi_fadt_t);
Marc Jones93a51762018-08-22 18:57:24 -06001062 header->revision = get_acpi_table_revision(FADT);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001063 memcpy(header->oem_id, OEM_ID, 6);
1064 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1065 memcpy(header->asl_compiler_id, ASLC, 4);
Elyes HAOUAS26071aa2019-02-15 08:21:33 +01001066 header->asl_compiler_revision = asl_revision;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001067
1068 fadt->firmware_ctrl = (unsigned long) facs;
1069 fadt->dsdt = (unsigned long) dsdt;
1070
1071 fadt->x_firmware_ctl_l = (unsigned long)facs;
1072 fadt->x_firmware_ctl_h = 0;
1073 fadt->x_dsdt_l = (unsigned long)dsdt;
1074 fadt->x_dsdt_h = 0;
1075
Duncan Laurie8312df42019-02-01 11:33:57 -08001076 if (IS_ENABLED(CONFIG_SYSTEM_TYPE_CONVERTIBLE) ||
Duncan Lauriefa9c6f12019-02-07 11:30:06 -08001077 IS_ENABLED(CONFIG_SYSTEM_TYPE_LAPTOP))
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001078 fadt->preferred_pm_profile = PM_MOBILE;
Duncan Lauriefa9c6f12019-02-07 11:30:06 -08001079 else if (IS_ENABLED(CONFIG_SYSTEM_TYPE_DETACHABLE) ||
1080 IS_ENABLED(CONFIG_SYSTEM_TYPE_TABLET))
1081 fadt->preferred_pm_profile = PM_TABLET;
Lee Leahy9c7c6f72017-03-16 11:24:09 -07001082 else
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001083 fadt->preferred_pm_profile = PM_DESKTOP;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001084
Vladimir Serbinenkoab83ef02014-10-25 15:18:25 +02001085 acpi_fill_fadt(fadt);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001086
1087 header->checksum =
1088 acpi_checksum((void *) fadt, header->length);
1089}
1090#endif
1091
Aaron Durbin64031672018-04-21 14:45:32 -06001092unsigned long __weak fw_cfg_acpi_tables(unsigned long start)
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001093{
1094 return 0;
1095}
1096
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001097unsigned long write_acpi_tables(unsigned long start)
1098{
1099 unsigned long current;
1100 acpi_rsdp_t *rsdp;
1101 acpi_rsdt_t *rsdt;
1102 acpi_xsdt_t *xsdt;
1103 acpi_fadt_t *fadt;
1104 acpi_facs_t *facs;
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001105 acpi_header_t *slic_file, *slic;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001106 acpi_header_t *ssdt;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001107 acpi_header_t *dsdt_file, *dsdt;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001108 acpi_mcfg_t *mcfg;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +02001109 acpi_tcpa_t *tcpa;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001110 acpi_tpm2_t *tpm2;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001111 acpi_madt_t *madt;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +11001112 struct device *dev;
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001113 unsigned long fw;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001114 size_t slic_size, dsdt_size;
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001115 char oem_id[6], oem_table_id[8];
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001116
1117 current = start;
1118
1119 /* Align ACPI tables to 16byte */
Aaron Durbin07a1b282015-12-10 17:07:38 -06001120 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001121
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001122 fw = fw_cfg_acpi_tables(current);
1123 if (fw)
1124 return fw;
1125
Vladimir Serbinenkoa4cf83d2015-06-02 21:40:29 +02001126 dsdt_file = cbfs_boot_map_with_leak(
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001127 CONFIG_CBFS_PREFIX "/dsdt.aml",
1128 CBFS_TYPE_RAW, &dsdt_size);
1129 if (!dsdt_file) {
1130 printk(BIOS_ERR, "No DSDT file, skipping ACPI tables\n");
1131 return current;
1132 }
1133
1134 if (dsdt_file->length > dsdt_size
Elyes HAOUAS7d87e762016-10-03 22:11:07 +02001135 || dsdt_file->length < sizeof(acpi_header_t)
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001136 || memcmp(dsdt_file->signature, "DSDT", 4) != 0) {
1137 printk(BIOS_ERR, "Invalid DSDT file, skipping ACPI tables\n");
1138 return current;
1139 }
1140
Aaron Durbin899d13d2015-05-15 23:39:23 -05001141 slic_file = cbfs_boot_map_with_leak(CONFIG_CBFS_PREFIX "/slic",
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001142 CBFS_TYPE_RAW, &slic_size);
Vladimir Serbinenko2cb29782015-05-31 11:32:09 +02001143 if (slic_file
1144 && (slic_file->length > slic_size
Elyes HAOUAS7d87e762016-10-03 22:11:07 +02001145 || slic_file->length < sizeof(acpi_header_t)
Vladimir Serbinenko2cb29782015-05-31 11:32:09 +02001146 || memcmp(slic_file->signature, "SLIC", 4) != 0)) {
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001147 slic_file = 0;
1148 }
1149
1150 if (slic_file) {
1151 memcpy(oem_id, slic_file->oem_id, 6);
1152 memcpy(oem_table_id, slic_file->oem_table_id, 8);
1153 } else {
1154 memcpy(oem_id, OEM_ID, 6);
1155 memcpy(oem_table_id, ACPI_TABLE_CREATOR, 8);
1156 }
1157
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001158 printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx.\n", start);
1159
1160 /* We need at least an RSDP and an RSDT Table */
1161 rsdp = (acpi_rsdp_t *) current;
1162 current += sizeof(acpi_rsdp_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001163 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001164 rsdt = (acpi_rsdt_t *) current;
1165 current += sizeof(acpi_rsdt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001166 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001167 xsdt = (acpi_xsdt_t *) current;
1168 current += sizeof(acpi_xsdt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001169 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001170
1171 /* clear all table memory */
1172 memset((void *) start, 0, current - start);
1173
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001174 acpi_write_rsdp(rsdp, rsdt, xsdt, oem_id);
1175 acpi_write_rsdt(rsdt, oem_id, oem_table_id);
1176 acpi_write_xsdt(xsdt, oem_id, oem_table_id);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001177
1178 printk(BIOS_DEBUG, "ACPI: * FACS\n");
Patrick Georgi133108a2015-08-08 23:20:58 +02001179 current = (ALIGN(current, 64));
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001180 facs = (acpi_facs_t *) current;
1181 current += sizeof(acpi_facs_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001182 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001183 acpi_create_facs(facs);
1184
1185 printk(BIOS_DEBUG, "ACPI: * DSDT\n");
1186 dsdt = (acpi_header_t *) current;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001187 memcpy(dsdt, dsdt_file, sizeof(acpi_header_t));
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001188 if (dsdt->length >= sizeof(acpi_header_t)) {
1189 current += sizeof(acpi_header_t);
1190
1191 acpigen_set_current((char *) current);
1192 for (dev = all_devices; dev; dev = dev->next)
Lee Leahy9c7c6f72017-03-16 11:24:09 -07001193 if (dev->ops && dev->ops->acpi_inject_dsdt_generator)
Alexander Couzensa90dad12015-04-12 21:49:46 +02001194 dev->ops->acpi_inject_dsdt_generator(dev);
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001195 current = (unsigned long) acpigen_get_current();
1196 memcpy((char *)current,
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001197 (char *)dsdt_file + sizeof(acpi_header_t),
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001198 dsdt->length - sizeof(acpi_header_t));
1199 current += dsdt->length - sizeof(acpi_header_t);
1200
1201 /* (Re)calculate length and checksum. */
1202 dsdt->length = current - (unsigned long)dsdt;
1203 dsdt->checksum = 0;
1204 dsdt->checksum = acpi_checksum((void *)dsdt, dsdt->length);
1205 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001206
Aaron Durbin07a1b282015-12-10 17:07:38 -06001207 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001208
1209 printk(BIOS_DEBUG, "ACPI: * FADT\n");
1210 fadt = (acpi_fadt_t *) current;
1211 current += sizeof(acpi_fadt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001212 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001213
1214 acpi_create_fadt(fadt, facs, dsdt);
1215 acpi_add_table(rsdp, fadt);
1216
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001217 if (slic_file) {
1218 printk(BIOS_DEBUG, "ACPI: * SLIC\n");
1219 slic = (acpi_header_t *)current;
1220 memcpy(slic, slic_file, slic_file->length);
1221 current += slic_file->length;
Aaron Durbin07a1b282015-12-10 17:07:38 -06001222 current = acpi_align_current(current);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001223 acpi_add_table(rsdp, slic);
1224 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001225
1226 printk(BIOS_DEBUG, "ACPI: * SSDT\n");
1227 ssdt = (acpi_header_t *)current;
1228 acpi_create_ssdt_generator(ssdt, ACPI_TABLE_CREATOR);
Vladimir Serbinenko9310df82014-10-10 20:40:41 +02001229 if (ssdt->length > sizeof(acpi_header_t)) {
1230 current += ssdt->length;
1231 acpi_add_table(rsdp, ssdt);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001232 current = acpi_align_current(current);
Vladimir Serbinenko9310df82014-10-10 20:40:41 +02001233 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001234
1235 printk(BIOS_DEBUG, "ACPI: * MCFG\n");
1236 mcfg = (acpi_mcfg_t *) current;
1237 acpi_create_mcfg(mcfg);
1238 if (mcfg->header.length > sizeof(acpi_mcfg_t)) {
1239 current += mcfg->header.length;
Aaron Durbin07a1b282015-12-10 17:07:38 -06001240 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001241 acpi_add_table(rsdp, mcfg);
1242 }
1243
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001244 if (IS_ENABLED(CONFIG_TPM1)) {
1245 printk(BIOS_DEBUG, "ACPI: * TCPA\n");
1246 tcpa = (acpi_tcpa_t *) current;
1247 acpi_create_tcpa(tcpa);
1248 if (tcpa->header.length >= sizeof(acpi_tcpa_t)) {
1249 current += tcpa->header.length;
1250 current = acpi_align_current(current);
1251 acpi_add_table(rsdp, tcpa);
1252 }
1253 }
1254
1255 if (IS_ENABLED(CONFIG_TPM2)) {
1256 printk(BIOS_DEBUG, "ACPI: * TPM2\n");
1257 tpm2 = (acpi_tpm2_t *) current;
1258 acpi_create_tpm2(tpm2);
1259 if (tpm2->header.length >= sizeof(acpi_tpm2_t)) {
1260 current += tpm2->header.length;
1261 current = acpi_align_current(current);
1262 acpi_add_table(rsdp, tpm2);
1263 }
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +02001264 }
1265
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001266 printk(BIOS_DEBUG, "ACPI: * MADT\n");
1267
1268 madt = (acpi_madt_t *) current;
1269 acpi_create_madt(madt);
1270 if (madt->header.length > sizeof(acpi_madt_t)) {
Lee Leahy024b13d2017-03-16 13:41:11 -07001271 current += madt->header.length;
1272 acpi_add_table(rsdp, madt);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001273 }
Aaron Durbin07a1b282015-12-10 17:07:38 -06001274 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001275
1276 printk(BIOS_DEBUG, "current = %lx\n", current);
1277
1278 for (dev = all_devices; dev; dev = dev->next) {
1279 if (dev->ops && dev->ops->write_acpi_tables) {
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001280 current = dev->ops->write_acpi_tables(dev, current,
1281 rsdp);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001282 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001283 }
1284 }
1285
1286 printk(BIOS_INFO, "ACPI: done.\n");
1287 return current;
1288}
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001289
Rudolf Marek33cafe52009-04-13 18:07:02 +00001290static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp)
1291{
1292 if (strncmp((char *)rsdp, RSDP_SIG, sizeof(RSDP_SIG) - 1) != 0)
1293 return NULL;
1294
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001295 printk(BIOS_DEBUG, "Looking on %p for valid checksum\n", rsdp);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001296
1297 if (acpi_checksum((void *)rsdp, 20) != 0)
1298 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001299 printk(BIOS_DEBUG, "Checksum 1 passed\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001300
1301 if ((rsdp->revision > 1) && (acpi_checksum((void *)rsdp,
1302 rsdp->length) != 0))
1303 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001304 printk(BIOS_DEBUG, "Checksum 2 passed all OK\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001305
1306 return rsdp;
1307}
1308
Rudolf Marek33cafe52009-04-13 18:07:02 +00001309void *acpi_find_wakeup_vector(void)
1310{
1311 char *p, *end;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001312 acpi_rsdt_t *rsdt;
1313 acpi_facs_t *facs;
Martin Rothc5f49262012-12-14 19:17:55 -07001314 acpi_fadt_t *fadt = NULL;
Kyösti Mälkki072d436b2016-06-17 08:44:40 +03001315 acpi_rsdp_t *rsdp = NULL;
Uwe Hermann622824c2010-11-19 15:14:42 +00001316 void *wake_vec;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001317 int i;
1318
Rudolf Marek33cafe52009-04-13 18:07:02 +00001319 if (!acpi_is_wakeup())
1320 return NULL;
1321
Uwe Hermann622824c2010-11-19 15:14:42 +00001322 printk(BIOS_DEBUG, "Trying to find the wakeup vector...\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001323
Uwe Hermann622824c2010-11-19 15:14:42 +00001324 /* Find RSDP. */
1325 for (p = (char *)0xe0000; p < (char *)0xfffff; p += 16) {
Lee Leahy0b5678f2017-03-16 16:01:40 -07001326 rsdp = valid_rsdp((acpi_rsdp_t *)p);
1327 if (rsdp)
Rudolf Marek33cafe52009-04-13 18:07:02 +00001328 break;
1329 }
1330
Angel Pons98a68ad2018-11-04 12:25:25 +01001331 if (rsdp == NULL) {
1332 printk(BIOS_ALERT,
1333 "No RSDP found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001334 return NULL;
Angel Pons98a68ad2018-11-04 12:25:25 +01001335 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001336
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001337 printk(BIOS_DEBUG, "RSDP found at %p\n", rsdp);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001338 rsdt = (acpi_rsdt_t *)(uintptr_t)rsdp->rsdt_address;
Stefan Reinauer14e22772010-04-27 06:56:47 +00001339
Uwe Hermann622824c2010-11-19 15:14:42 +00001340 end = (char *)rsdt + rsdt->header.length;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001341 printk(BIOS_DEBUG, "RSDT found at %p ends at %p\n", rsdt, end);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001342
Uwe Hermann622824c2010-11-19 15:14:42 +00001343 for (i = 0; ((char *)&rsdt->entry[i]) < end; i++) {
Stefan Reinauer71a30182015-07-30 16:28:13 -07001344 fadt = (acpi_fadt_t *)(uintptr_t)rsdt->entry[i];
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001345 if (strncmp((char *)fadt, "FACP", 4) == 0)
Rudolf Marek33cafe52009-04-13 18:07:02 +00001346 break;
1347 fadt = NULL;
1348 }
1349
Angel Pons98a68ad2018-11-04 12:25:25 +01001350 if (fadt == NULL) {
1351 printk(BIOS_ALERT,
1352 "No FADT found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001353 return NULL;
Angel Pons98a68ad2018-11-04 12:25:25 +01001354 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001355
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001356 printk(BIOS_DEBUG, "FADT found at %p\n", fadt);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001357 facs = (acpi_facs_t *)(uintptr_t)fadt->firmware_ctrl;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001358
Stefan Reinauer7e9771c2009-04-22 08:17:38 +00001359 if (facs == NULL) {
Angel Pons98a68ad2018-11-04 12:25:25 +01001360 printk(BIOS_ALERT,
1361 "No FACS found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001362 return NULL;
Stefan Reinauer7e9771c2009-04-22 08:17:38 +00001363 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001364
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001365 printk(BIOS_DEBUG, "FACS found at %p\n", facs);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001366 wake_vec = (void *)(uintptr_t)facs->firmware_waking_vector;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001367 printk(BIOS_DEBUG, "OS waking vector is %p\n", wake_vec);
Uwe Hermann622824c2010-11-19 15:14:42 +00001368
Rudolf Marek33cafe52009-04-13 18:07:02 +00001369 return wake_vec;
1370}
1371
Aaron Durbin64031672018-04-21 14:45:32 -06001372__weak int acpi_get_gpe(int gpe)
Duncan Laurie1f6e6812016-09-19 12:04:19 -07001373{
1374 return -1; /* implemented by SOC */
1375}
Marc Jones93a51762018-08-22 18:57:24 -06001376
1377int get_acpi_table_revision(enum acpi_tables table)
1378{
1379 switch (table) {
1380 case FADT:
1381 return ACPI_FADT_REV_ACPI_3_0;
1382 case MADT: /* ACPI 1.0/2.0: 1, ACPI 3.0: 2, ACPI 4.0: 3 */
Marc Jones4ddd4762018-08-21 10:50:57 -06001383 return 2;
Marc Jones93a51762018-08-22 18:57:24 -06001384 case MCFG:
1385 return 1;
1386 case TCPA:
1387 return 2;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001388 case TPM2:
1389 return 4;
Marc Jones93a51762018-08-22 18:57:24 -06001390 case SSDT: /* ACPI 1.0/2.0: ?, ACPI 3.0/4.0: 2 */
1391 return 2;
1392 case SRAT: /* ACPI 1.0: N/A, 2.0: 1, 3.0: 2, 4.0: 3 */
1393 return 1; /* TODO Should probably be upgraded to 2 */
1394 case DMAR:
1395 return 1;
1396 case SLIT: /* ACPI 1.0: N/A, ACPI 2.0/3.0/4.0: 1 */
1397 return 1;
1398 case HPET: /* Currently 1. Table added in ACPI 2.0. */
1399 return 1;
1400 case VFCT: /* ACPI 1.0: N/A, ACPI 2.0/3.0/4.0: 1 */
1401 return 1;
1402 case IVRS:
1403 return IVRS_FORMAT_FIXED;
1404 case DBG2:
1405 return 0;
1406 case FACS: /* ACPI 1.0: 0, ACPI 2.0/3.0: 1, ACPI 4.0: 2 */
1407 return 1;
1408 case RSDT: /* ACPI 1.0/2.0/3.0/4.0: 1 */
1409 return 1;
1410 case XSDT: /* ACPI 1.0: N/A, 2.0/3.0/4.0: 1 */
1411 return 1;
1412 case RSDP: /* ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2. */
1413 return 2;
1414 case HEST:
1415 return 1;
1416 case NHLT:
1417 return 5;
Marshall Dawson44705c62018-09-04 13:47:15 -06001418 case BERT:
1419 return 1;
Marc Jones93a51762018-08-22 18:57:24 -06001420 default:
1421 return -1;
1422 }
1423 return -1;
1424}