blob: 8d7579d41cbcaf7657d87272918f16b26471cc56 [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>
Aaron Durbin64031672018-04-21 14:45:32 -060047#include <compiler.h>
Patrick Georgic8feedd2012-02-16 18:43:25 +010048#include <cpu/x86/lapic_def.h>
Duncan Laurie11290c42012-10-03 19:07:05 -070049#include <cpu/cpu.h>
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +010050#include <cbfs.h>
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000051
52u8 acpi_checksum(u8 *table, u32 length)
Stefan Reinauer688b3852004-01-28 16:56:14 +000053{
Uwe Hermann622824c2010-11-19 15:14:42 +000054 u8 ret = 0;
Stefan Reinauer688b3852004-01-28 16:56:14 +000055 while (length--) {
56 ret += *table;
57 table++;
58 }
Stefan Reinauera7648c22004-01-29 17:31:34 +000059 return -ret;
Stefan Reinauer688b3852004-01-28 16:56:14 +000060}
61
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000062/**
Uwe Hermann622824c2010-11-19 15:14:42 +000063 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
64 * and checksum.
Stefan Reinauer06feb882004-02-03 16:11:35 +000065 */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000066void acpi_add_table(acpi_rsdp_t *rsdp, void *table)
Stefan Reinauer688b3852004-01-28 16:56:14 +000067{
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000068 int i, entries_num;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000069 acpi_rsdt_t *rsdt;
70 acpi_xsdt_t *xsdt = NULL;
71
Uwe Hermann622824c2010-11-19 15:14:42 +000072 /* The RSDT is mandatory... */
Stefan Reinauerdefee172015-06-18 01:11:19 -070073 rsdt = (acpi_rsdt_t *)(uintptr_t)rsdp->rsdt_address;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000074
Uwe Hermann622824c2010-11-19 15:14:42 +000075 /* ...while the XSDT is not. */
76 if (rsdp->xsdt_address)
Stefan Reinauerdefee172015-06-18 01:11:19 -070077 xsdt = (acpi_xsdt_t *)((uintptr_t)rsdp->xsdt_address);
Stefan Reinauer14e22772010-04-27 06:56:47 +000078
Uwe Hermann622824c2010-11-19 15:14:42 +000079 /* This should always be MAX_ACPI_TABLES. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000080 entries_num = ARRAY_SIZE(rsdt->entry);
Stefan Reinauer14e22772010-04-27 06:56:47 +000081
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000082 for (i = 0; i < entries_num; i++) {
Uwe Hermann622824c2010-11-19 15:14:42 +000083 if (rsdt->entry[i] == 0)
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000084 break;
Stefan Reinauer688b3852004-01-28 16:56:14 +000085 }
86
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000087 if (i >= entries_num) {
Uwe Hermann622824c2010-11-19 15:14:42 +000088 printk(BIOS_ERR, "ACPI: Error: Could not add ACPI table, "
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +030089 "too many tables.\n");
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000090 return;
91 }
92
Uwe Hermann622824c2010-11-19 15:14:42 +000093 /* Add table to the RSDT. */
Stefan Reinauerdefee172015-06-18 01:11:19 -070094 rsdt->entry[i] = (uintptr_t)table;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000095
Uwe Hermann622824c2010-11-19 15:14:42 +000096 /* Fix RSDT length or the kernel will assume invalid entries. */
97 rsdt->header.length = sizeof(acpi_header_t) + (sizeof(u32) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000098
Uwe Hermann622824c2010-11-19 15:14:42 +000099 /* Re-calculate checksum. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000100 rsdt->header.checksum = 0; /* Hope this won't get optimized away */
Uwe Hermann622824c2010-11-19 15:14:42 +0000101 rsdt->header.checksum = acpi_checksum((u8 *)rsdt, rsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000102
Uwe Hermann622824c2010-11-19 15:14:42 +0000103 /*
104 * And now the same thing for the XSDT. We use the same index as for
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000105 * now we want the XSDT and RSDT to always be in sync in coreboot.
106 */
107 if (xsdt) {
Uwe Hermann622824c2010-11-19 15:14:42 +0000108 /* Add table to the XSDT. */
Stefan Reinauerdefee172015-06-18 01:11:19 -0700109 xsdt->entry[i] = (u64)(uintptr_t)table;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000110
Uwe Hermann622824c2010-11-19 15:14:42 +0000111 /* Fix XSDT length. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000112 xsdt->header.length = sizeof(acpi_header_t) +
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300113 (sizeof(u64) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000114
Uwe Hermann622824c2010-11-19 15:14:42 +0000115 /* Re-calculate checksum. */
116 xsdt->header.checksum = 0;
117 xsdt->header.checksum = acpi_checksum((u8 *)xsdt,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300118 xsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000119 }
120
Uwe Hermann622824c2010-11-19 15:14:42 +0000121 printk(BIOS_DEBUG, "ACPI: added table %d/%d, length now %d\n",
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300122 i + 1, entries_num, rsdt->header.length);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000123}
124
Uwe Hermann622824c2010-11-19 15:14:42 +0000125int acpi_create_mcfg_mmconfig(acpi_mcfg_mmconfig_t *mmconfig, u32 base,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300126 u16 seg_nr, u8 start, u8 end)
Stefan Reinauer26d431a2009-01-20 19:17:51 +0000127{
Vladimir Serbinenko60fccdc2014-10-05 11:05:16 +0200128 memset(mmconfig, 0, sizeof(*mmconfig));
Rudolf Mareke6409f22007-11-03 12:50:26 +0000129 mmconfig->base_address = base;
130 mmconfig->base_reserved = 0;
131 mmconfig->pci_segment_group_number = seg_nr;
132 mmconfig->start_bus_number = start;
133 mmconfig->end_bus_number = end;
Uwe Hermann622824c2010-11-19 15:14:42 +0000134
135 return sizeof(acpi_mcfg_mmconfig_t);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000136}
137
Stefan Reinauer777224c2005-01-19 14:06:41 +0000138int acpi_create_madt_lapic(acpi_madt_lapic_t *lapic, u8 cpu, u8 apic)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000139{
Uwe Hermann622824c2010-11-19 15:14:42 +0000140 lapic->type = 0; /* Local APIC structure */
141 lapic->length = sizeof(acpi_madt_lapic_t);
142 lapic->flags = (1 << 0); /* Processor/LAPIC enabled */
143 lapic->processor_id = cpu;
144 lapic->apic_id = apic;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000145
Uwe Hermann622824c2010-11-19 15:14:42 +0000146 return lapic->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000147}
148
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000149unsigned long acpi_create_madt_lapics(unsigned long current)
150{
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100151 struct device *cpu;
Duncan Laurie11290c42012-10-03 19:07:05 -0700152 int index = 0;
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000153
Uwe Hermann622824c2010-11-19 15:14:42 +0000154 for (cpu = all_devices; cpu; cpu = cpu->next) {
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000155 if ((cpu->path.type != DEVICE_PATH_APIC) ||
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800156 (cpu->bus->dev->path.type != DEVICE_PATH_CPU_CLUSTER)) {
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000157 continue;
158 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000159 if (!cpu->enabled)
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000160 continue;
Uwe Hermann622824c2010-11-19 15:14:42 +0000161 current += acpi_create_madt_lapic((acpi_madt_lapic_t *)current,
Duncan Laurie11290c42012-10-03 19:07:05 -0700162 index, cpu->path.apic.apic_id);
163 index++;
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000164 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000165
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000166 return current;
167}
168
Uwe Hermann622824c2010-11-19 15:14:42 +0000169int acpi_create_madt_ioapic(acpi_madt_ioapic_t *ioapic, u8 id, u32 addr,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300170 u32 gsi_base)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000171{
Uwe Hermann622824c2010-11-19 15:14:42 +0000172 ioapic->type = 1; /* I/O APIC structure */
173 ioapic->length = sizeof(acpi_madt_ioapic_t);
174 ioapic->reserved = 0x00;
175 ioapic->gsi_base = gsi_base;
176 ioapic->ioapic_id = id;
177 ioapic->ioapic_addr = addr;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000178
Uwe Hermann622824c2010-11-19 15:14:42 +0000179 return ioapic->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000180}
181
Stefan Reinauer777224c2005-01-19 14:06:41 +0000182int acpi_create_madt_irqoverride(acpi_madt_irqoverride_t *irqoverride,
Stefan Reinauer06feb882004-02-03 16:11:35 +0000183 u8 bus, u8 source, u32 gsirq, u16 flags)
184{
Uwe Hermann622824c2010-11-19 15:14:42 +0000185 irqoverride->type = 2; /* Interrupt source override */
186 irqoverride->length = sizeof(acpi_madt_irqoverride_t);
187 irqoverride->bus = bus;
188 irqoverride->source = source;
189 irqoverride->gsirq = gsirq;
190 irqoverride->flags = flags;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000191
Uwe Hermann622824c2010-11-19 15:14:42 +0000192 return irqoverride->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000193}
194
Stefan Reinauer777224c2005-01-19 14:06:41 +0000195int acpi_create_madt_lapic_nmi(acpi_madt_lapic_nmi_t *lapic_nmi, u8 cpu,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300196 u16 flags, u8 lint)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000197{
Uwe Hermann622824c2010-11-19 15:14:42 +0000198 lapic_nmi->type = 4; /* Local APIC NMI structure */
199 lapic_nmi->length = sizeof(acpi_madt_lapic_nmi_t);
200 lapic_nmi->flags = flags;
201 lapic_nmi->processor_id = cpu;
202 lapic_nmi->lint = lint;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000203
Uwe Hermann622824c2010-11-19 15:14:42 +0000204 return lapic_nmi->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000205}
206
Stefan Reinauer777224c2005-01-19 14:06:41 +0000207void acpi_create_madt(acpi_madt_t *madt)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000208{
Uwe Hermann622824c2010-11-19 15:14:42 +0000209 acpi_header_t *header = &(madt->header);
210 unsigned long current = (unsigned long)madt + sizeof(acpi_madt_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000211
Stefan Reinauer06feb882004-02-03 16:11:35 +0000212 memset((void *)madt, 0, sizeof(acpi_madt_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000213
Uwe Hermann622824c2010-11-19 15:14:42 +0000214 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000215 memcpy(header->signature, "APIC", 4);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000216 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000217 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000218 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000219
Stefan Reinauer06feb882004-02-03 16:11:35 +0000220 header->length = sizeof(acpi_madt_t);
Uwe Hermann622824c2010-11-19 15:14:42 +0000221 header->revision = 1; /* ACPI 1.0/2.0: 1, ACPI 3.0: 2, ACPI 4.0: 3 */
Stefan Reinauer06feb882004-02-03 16:11:35 +0000222
Uwe Hermann622824c2010-11-19 15:14:42 +0000223 madt->lapic_addr = LOCAL_APIC_ADDR;
224 madt->flags = 0x1; /* PCAT_COMPAT */
Stefan Reinauer06feb882004-02-03 16:11:35 +0000225
Stefan Reinauerf622d592005-11-26 16:56:05 +0000226 current = acpi_fill_madt(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000227
Uwe Hermann622824c2010-11-19 15:14:42 +0000228 /* (Re)calculate length and checksum. */
229 header->length = current - (unsigned long)madt;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000230
Uwe Hermann622824c2010-11-19 15:14:42 +0000231 header->checksum = acpi_checksum((void *)madt, header->length);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000232}
233
Uwe Hermann622824c2010-11-19 15:14:42 +0000234/* MCFG is defined in the PCI Firmware Specification 3.0. */
Rudolf Mareke6409f22007-11-03 12:50:26 +0000235void acpi_create_mcfg(acpi_mcfg_t *mcfg)
236{
Uwe Hermann622824c2010-11-19 15:14:42 +0000237 acpi_header_t *header = &(mcfg->header);
238 unsigned long current = (unsigned long)mcfg + sizeof(acpi_mcfg_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000239
Rudolf Mareke6409f22007-11-03 12:50:26 +0000240 memset((void *)mcfg, 0, sizeof(acpi_mcfg_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000241
Uwe Hermann622824c2010-11-19 15:14:42 +0000242 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000243 memcpy(header->signature, "MCFG", 4);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000244 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000245 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000246 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000247
Rudolf Mareke6409f22007-11-03 12:50:26 +0000248 header->length = sizeof(acpi_mcfg_t);
249 header->revision = 1;
250
251 current = acpi_fill_mcfg(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000252
Uwe Hermann622824c2010-11-19 15:14:42 +0000253 /* (Re)calculate length and checksum. */
254 header->length = current - (unsigned long)mcfg;
255 header->checksum = acpi_checksum((void *)mcfg, header->length);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000256}
257
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200258static void *get_tcpa_log(u32 *size)
259{
260 const struct cbmem_entry *ce;
261 const u32 tcpa_default_log_len = 0x10000;
262 void *lasa;
Philipp Deppenwiese791ba972018-07-30 01:15:20 +0200263 ce = cbmem_entry_find(CBMEM_ID_TCPA_TCG_LOG);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200264 if (ce) {
265 lasa = cbmem_entry_start(ce);
266 *size = cbmem_entry_size(ce);
267 printk(BIOS_DEBUG, "TCPA log found at %p\n", lasa);
268 return lasa;
269 }
Philipp Deppenwiese791ba972018-07-30 01:15:20 +0200270 lasa = cbmem_add(CBMEM_ID_TCPA_TCG_LOG, tcpa_default_log_len);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200271 if (!lasa) {
272 printk(BIOS_ERR, "TCPA log creation failed\n");
273 return NULL;
274 }
275
276 printk(BIOS_DEBUG, "TCPA log created at %p\n", lasa);
Lee Leahy024b13d2017-03-16 13:41:11 -0700277 memset(lasa, 0, tcpa_default_log_len);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200278
279 *size = tcpa_default_log_len;
280 return lasa;
281}
282
283static void acpi_create_tcpa(acpi_tcpa_t *tcpa)
284{
285 acpi_header_t *header = &(tcpa->header);
286 u32 tcpa_log_len;
287 void *lasa;
288
289 memset((void *)tcpa, 0, sizeof(acpi_tcpa_t));
290
291 lasa = get_tcpa_log(&tcpa_log_len);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700292 if (!lasa)
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200293 return;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200294
295 /* Fill out header fields. */
296 memcpy(header->signature, "TCPA", 4);
297 memcpy(header->oem_id, OEM_ID, 6);
298 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
299 memcpy(header->asl_compiler_id, ASLC, 4);
300
301 header->length = sizeof(acpi_tcpa_t);
302 header->revision = 2;
303
304 tcpa->platform_class = 0;
305 tcpa->laml = tcpa_log_len;
Stefan Reinauerdefee172015-06-18 01:11:19 -0700306 tcpa->lasa = (uintptr_t) lasa;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200307
308 /* Calculate checksum. */
309 header->checksum = acpi_checksum((void *)tcpa, header->length);
310}
311
Duncan Laurie37319032016-05-26 12:47:05 -0700312static void acpi_ssdt_write_cbtable(void)
313{
314 const struct cbmem_entry *cbtable;
315 uintptr_t base;
316 uint32_t size;
317
318 cbtable = cbmem_entry_find(CBMEM_ID_CBTABLE);
319 if (!cbtable)
320 return;
321 base = (uintptr_t)cbmem_entry_start(cbtable);
322 size = cbmem_entry_size(cbtable);
323
324 acpigen_write_device("CTBL");
325 acpigen_write_coreboot_hid(COREBOOT_ACPI_ID_CBTABLE);
326 acpigen_write_name_integer("_UID", 0);
327 acpigen_write_STA(ACPI_STATUS_DEVICE_ALL_ON);
328 acpigen_write_name("_CRS");
329 acpigen_write_resourcetemplate_header();
330 acpigen_write_mem32fixed(0, base, size);
331 acpigen_write_resourcetemplate_footer();
332 acpigen_pop_len();
333}
334
Myles Watson3fe6b702009-10-09 20:13:43 +0000335void acpi_create_ssdt_generator(acpi_header_t *ssdt, const char *oem_table_id)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000336{
Uwe Hermann622824c2010-11-19 15:14:42 +0000337 unsigned long current = (unsigned long)ssdt + sizeof(acpi_header_t);
338
Rudolf Marek293b5f52009-02-01 18:35:15 +0000339 memset((void *)ssdt, 0, sizeof(acpi_header_t));
Uwe Hermann622824c2010-11-19 15:14:42 +0000340
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000341 memcpy(&ssdt->signature, "SSDT", 4);
Uwe Hermann622824c2010-11-19 15:14:42 +0000342 ssdt->revision = 2; /* ACPI 1.0/2.0: ?, ACPI 3.0/4.0: 2 */
Rudolf Marek293b5f52009-02-01 18:35:15 +0000343 memcpy(&ssdt->oem_id, OEM_ID, 6);
344 memcpy(&ssdt->oem_table_id, oem_table_id, 8);
345 ssdt->oem_revision = 42;
Uwe Hermann622824c2010-11-19 15:14:42 +0000346 memcpy(&ssdt->asl_compiler_id, ASLC, 4);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000347 ssdt->asl_compiler_revision = 42;
348 ssdt->length = sizeof(acpi_header_t);
349
Stefan Reinauer8dcd50b2009-03-06 17:24:29 +0000350 acpigen_set_current((char *) current);
Duncan Laurie37319032016-05-26 12:47:05 -0700351
352 /* Write object to declare coreboot tables */
353 acpi_ssdt_write_cbtable();
354
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200355 {
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100356 struct device *dev;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200357 for (dev = all_devices; dev; dev = dev->next)
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700358 if (dev->ops && dev->ops->acpi_fill_ssdt_generator)
Alexander Couzens5eea4582015-04-12 22:18:55 +0200359 dev->ops->acpi_fill_ssdt_generator(dev);
Vladimir Serbinenko0a669912014-10-05 14:34:17 +0200360 current = (unsigned long) acpigen_get_current();
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200361 }
Rudolf Marek293b5f52009-02-01 18:35:15 +0000362
Uwe Hermann622824c2010-11-19 15:14:42 +0000363 /* (Re)calculate length and checksum. */
Rudolf Marek293b5f52009-02-01 18:35:15 +0000364 ssdt->length = current - (unsigned long)ssdt;
365 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
366}
367
Stefan Reinauerf622d592005-11-26 16:56:05 +0000368int acpi_create_srat_lapic(acpi_srat_lapic_t *lapic, u8 node, u8 apic)
369{
Myles Watson0b4c9f02009-03-10 18:06:47 +0000370 memset((void *)lapic, 0, sizeof(acpi_srat_lapic_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000371
Uwe Hermann622824c2010-11-19 15:14:42 +0000372 lapic->type = 0; /* Processor local APIC/SAPIC affinity structure */
373 lapic->length = sizeof(acpi_srat_lapic_t);
374 lapic->flags = (1 << 0); /* Enabled (the use of this structure). */
375 lapic->proximity_domain_7_0 = node;
376 /* TODO: proximity_domain_31_8, local SAPIC EID, clock domain. */
377 lapic->apic_id = apic;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000378
Uwe Hermann622824c2010-11-19 15:14:42 +0000379 return lapic->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000380}
381
Uwe Hermann622824c2010-11-19 15:14:42 +0000382int 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 +0300383 u32 flags)
Stefan Reinauerf622d592005-11-26 16:56:05 +0000384{
Uwe Hermann622824c2010-11-19 15:14:42 +0000385 mem->type = 1; /* Memory affinity structure */
386 mem->length = sizeof(acpi_srat_mem_t);
387 mem->base_address_low = (basek << 10);
388 mem->base_address_high = (basek >> (32 - 10));
389 mem->length_low = (sizek << 10);
390 mem->length_high = (sizek >> (32 - 10));
391 mem->proximity_domain = node;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000392 mem->flags = flags;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000393
Uwe Hermann622824c2010-11-19 15:14:42 +0000394 return mem->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000395}
396
Uwe Hermann622824c2010-11-19 15:14:42 +0000397/* http://www.microsoft.com/whdc/system/sysinternals/sratdwn.mspx */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +0200398void acpi_create_srat(acpi_srat_t *srat,
399 unsigned long (*acpi_fill_srat)(unsigned long current))
Stefan Reinauerf622d592005-11-26 16:56:05 +0000400{
Uwe Hermann622824c2010-11-19 15:14:42 +0000401 acpi_header_t *header = &(srat->header);
402 unsigned long current = (unsigned long)srat + sizeof(acpi_srat_t);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000403
Uwe Hermann622824c2010-11-19 15:14:42 +0000404 memset((void *)srat, 0, sizeof(acpi_srat_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000405
Uwe Hermann622824c2010-11-19 15:14:42 +0000406 /* Fill out header fields. */
407 memcpy(header->signature, "SRAT", 4);
408 memcpy(header->oem_id, OEM_ID, 6);
409 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
410 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000411
Uwe Hermann622824c2010-11-19 15:14:42 +0000412 header->length = sizeof(acpi_srat_t);
413 header->revision = 1; /* ACPI 1.0: N/A, 2.0: 1, 3.0: 2, 4.0: 3 */
Stefan Reinauerf622d592005-11-26 16:56:05 +0000414
Uwe Hermann622824c2010-11-19 15:14:42 +0000415 srat->resv = 1; /* Spec: Reserved to 1 for backwards compatibility. */
Stefan Reinauerf622d592005-11-26 16:56:05 +0000416
Uwe Hermann622824c2010-11-19 15:14:42 +0000417 current = acpi_fill_srat(current);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000418
Uwe Hermann622824c2010-11-19 15:14:42 +0000419 /* (Re)calculate length and checksum. */
420 header->length = current - (unsigned long)srat;
421 header->checksum = acpi_checksum((void *)srat, header->length);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000422}
423
Nico Hubere561f352015-10-26 11:51:25 +0100424void acpi_create_dmar(acpi_dmar_t *dmar, enum dmar_flags flags,
Lee Leahy024b13d2017-03-16 13:41:11 -0700425 unsigned long (*acpi_fill_dmar)(unsigned long))
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200426{
427 acpi_header_t *header = &(dmar->header);
428 unsigned long current = (unsigned long)dmar + sizeof(acpi_dmar_t);
429
430 memset((void *)dmar, 0, sizeof(acpi_dmar_t));
431
432 /* Fill out header fields. */
433 memcpy(header->signature, "DMAR", 4);
434 memcpy(header->oem_id, OEM_ID, 6);
435 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
436 memcpy(header->asl_compiler_id, ASLC, 4);
437
438 header->length = sizeof(acpi_dmar_t);
439 header->revision = 1;
440
Jacob Laskaf3f654d2016-03-15 21:53:27 -0500441 dmar->host_address_width = cpu_phys_address_size() - 1;
Nico Hubere561f352015-10-26 11:51:25 +0100442 dmar->flags = flags;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200443
444 current = acpi_fill_dmar(current);
445
446 /* (Re)calculate length and checksum. */
447 header->length = current - (unsigned long)dmar;
448 header->checksum = acpi_checksum((void *)dmar, header->length);
449}
450
451unsigned long acpi_create_dmar_drhd(unsigned long current, u8 flags,
Matt DeVillier7866d492018-03-29 14:59:57 +0200452 u16 segment, u64 bar)
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200453{
454 dmar_entry_t *drhd = (dmar_entry_t *)current;
455 memset(drhd, 0, sizeof(*drhd));
456 drhd->type = DMAR_DRHD;
457 drhd->length = sizeof(*drhd); /* will be fixed up later */
458 drhd->flags = flags;
459 drhd->segment = segment;
460 drhd->bar = bar;
461
462 return drhd->length;
463}
464
Matt DeVillier7866d492018-03-29 14:59:57 +0200465unsigned long acpi_create_dmar_rmrr(unsigned long current, u16 segment,
466 u64 bar, u64 limit)
467{
468 dmar_rmrr_entry_t *rmrr = (dmar_rmrr_entry_t *)current;
469 memset(rmrr, 0, sizeof(*rmrr));
470 rmrr->type = DMAR_RMRR;
471 rmrr->length = sizeof(*rmrr); /* will be fixed up later */
472 rmrr->segment = segment;
473 rmrr->bar = bar;
474 rmrr->limit = limit;
475
476 return rmrr->length;
477}
478
Werner Zehd4d76952016-07-27 06:56:36 +0200479unsigned long acpi_create_dmar_atsr(unsigned long current, u8 flags,
480 u16 segment)
481{
482 dmar_atsr_entry_t *atsr = (dmar_atsr_entry_t *)current;
483 memset(atsr, 0, sizeof(*atsr));
484 atsr->type = DMAR_ATSR;
485 atsr->length = sizeof(*atsr); /* will be fixed up later */
486 atsr->flags = flags;
487 atsr->segment = segment;
488
489 return atsr->length;
490}
491
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200492void acpi_dmar_drhd_fixup(unsigned long base, unsigned long current)
493{
494 dmar_entry_t *drhd = (dmar_entry_t *)base;
495 drhd->length = current - base;
496}
497
Matt DeVillier7866d492018-03-29 14:59:57 +0200498void acpi_dmar_rmrr_fixup(unsigned long base, unsigned long current)
499{
500 dmar_rmrr_entry_t *rmrr = (dmar_rmrr_entry_t *)base;
501 rmrr->length = current - base;
502}
503
Werner Zehd4d76952016-07-27 06:56:36 +0200504void acpi_dmar_atsr_fixup(unsigned long base, unsigned long current)
505{
506 dmar_atsr_entry_t *atsr = (dmar_atsr_entry_t *)base;
507 atsr->length = current - base;
508}
509
Matt DeVillier7866d492018-03-29 14:59:57 +0200510static unsigned long acpi_create_dmar_ds(unsigned long current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100511 enum dev_scope_type type, u8 enumeration_id, u8 bus, u8 dev, u8 fn)
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200512{
Nico Huber6c4751d2015-10-26 12:03:54 +0100513 /* we don't support longer paths yet */
514 const size_t dev_scope_length = sizeof(dev_scope_t) + 2;
515
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200516 dev_scope_t *ds = (dev_scope_t *)current;
Nico Huber6c4751d2015-10-26 12:03:54 +0100517 memset(ds, 0, dev_scope_length);
518 ds->type = type;
519 ds->length = dev_scope_length;
520 ds->enumeration = enumeration_id;
521 ds->start_bus = bus;
522 ds->path[0].dev = dev;
523 ds->path[0].fn = fn;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200524
525 return ds->length;
526}
527
Matt DeVillier7866d492018-03-29 14:59:57 +0200528unsigned long acpi_create_dmar_ds_pci_br(unsigned long current, u8 bus,
Werner Zeh21a5bff2016-07-27 07:07:20 +0200529 u8 dev, u8 fn)
530{
Matt DeVillier7866d492018-03-29 14:59:57 +0200531 return acpi_create_dmar_ds(current,
Werner Zeh21a5bff2016-07-27 07:07:20 +0200532 SCOPE_PCI_SUB, 0, bus, dev, fn);
533}
534
Matt DeVillier7866d492018-03-29 14:59:57 +0200535unsigned long acpi_create_dmar_ds_pci(unsigned long current, u8 bus,
Nico Huber6c4751d2015-10-26 12:03:54 +0100536 u8 dev, u8 fn)
537{
Matt DeVillier7866d492018-03-29 14:59:57 +0200538 return acpi_create_dmar_ds(current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100539 SCOPE_PCI_ENDPOINT, 0, bus, dev, fn);
540}
541
Matt DeVillier7866d492018-03-29 14:59:57 +0200542unsigned long acpi_create_dmar_ds_ioapic(unsigned long current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100543 u8 enumeration_id, u8 bus, u8 dev, u8 fn)
544{
Matt DeVillier7866d492018-03-29 14:59:57 +0200545 return acpi_create_dmar_ds(current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100546 SCOPE_IOAPIC, enumeration_id, bus, dev, fn);
547}
548
Matt DeVillier7866d492018-03-29 14:59:57 +0200549unsigned long acpi_create_dmar_ds_msi_hpet(unsigned long current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100550 u8 enumeration_id, u8 bus, u8 dev, u8 fn)
551{
Matt DeVillier7866d492018-03-29 14:59:57 +0200552 return acpi_create_dmar_ds(current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100553 SCOPE_MSI_HPET, enumeration_id, bus, dev, fn);
554}
555
Uwe Hermann622824c2010-11-19 15:14:42 +0000556/* http://h21007.www2.hp.com/portal/download/files/unprot/Itanium/slit.pdf */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +0200557void acpi_create_slit(acpi_slit_t *slit,
558 unsigned long (*acpi_fill_slit)(unsigned long current))
Yinghai Lud4b278c2006-10-04 20:46:15 +0000559{
Uwe Hermann622824c2010-11-19 15:14:42 +0000560 acpi_header_t *header = &(slit->header);
561 unsigned long current = (unsigned long)slit + sizeof(acpi_slit_t);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000562
Uwe Hermann622824c2010-11-19 15:14:42 +0000563 memset((void *)slit, 0, sizeof(acpi_slit_t));
Yinghai Lud4b278c2006-10-04 20:46:15 +0000564
Uwe Hermann622824c2010-11-19 15:14:42 +0000565 /* Fill out header fields. */
566 memcpy(header->signature, "SLIT", 4);
567 memcpy(header->oem_id, OEM_ID, 6);
568 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
569 memcpy(header->asl_compiler_id, ASLC, 4);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000570
Uwe Hermann622824c2010-11-19 15:14:42 +0000571 header->length = sizeof(acpi_slit_t);
572 header->revision = 1; /* ACPI 1.0: N/A, ACPI 2.0/3.0/4.0: 1 */
Yinghai Lud4b278c2006-10-04 20:46:15 +0000573
Uwe Hermann622824c2010-11-19 15:14:42 +0000574 current = acpi_fill_slit(current);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000575
Uwe Hermann622824c2010-11-19 15:14:42 +0000576 /* (Re)calculate length and checksum. */
577 header->length = current - (unsigned long)slit;
578 header->checksum = acpi_checksum((void *)slit, header->length);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000579}
580
Uwe Hermann622824c2010-11-19 15:14:42 +0000581/* http://www.intel.com/hardwaredesign/hpetspec_1.pdf */
Stefan Reinauer777224c2005-01-19 14:06:41 +0000582void acpi_create_hpet(acpi_hpet_t *hpet)
Stefan Reinauer688b3852004-01-28 16:56:14 +0000583{
Uwe Hermann622824c2010-11-19 15:14:42 +0000584 acpi_header_t *header = &(hpet->header);
585 acpi_addr_t *addr = &(hpet->addr);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000586
Stefan Reinauera7648c22004-01-29 17:31:34 +0000587 memset((void *)hpet, 0, sizeof(acpi_hpet_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000588
Uwe Hermann622824c2010-11-19 15:14:42 +0000589 /* Fill out header fields. */
Stefan Reinauer4704dc52009-07-22 01:11:37 +0000590 memcpy(header->signature, "HPET", 4);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000591 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000592 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000593 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000594
Stefan Reinauer688b3852004-01-28 16:56:14 +0000595 header->length = sizeof(acpi_hpet_t);
Uwe Hermann622824c2010-11-19 15:14:42 +0000596 header->revision = 1; /* Currently 1. Table added in ACPI 2.0. */
Stefan Reinauer688b3852004-01-28 16:56:14 +0000597
Uwe Hermann622824c2010-11-19 15:14:42 +0000598 /* Fill out HPET address. */
599 addr->space_id = 0; /* Memory */
600 addr->bit_width = 64;
601 addr->bit_offset = 0;
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200602 addr->addrl = CONFIG_HPET_ADDRESS & 0xffffffff;
603 addr->addrh = ((unsigned long long)CONFIG_HPET_ADDRESS) >> 32;
Stefan Reinauer688b3852004-01-28 16:56:14 +0000604
Lee Leahy024b13d2017-03-16 13:41:11 -0700605 hpet->id = *(unsigned int *)CONFIG_HPET_ADDRESS;
Uwe Hermann622824c2010-11-19 15:14:42 +0000606 hpet->number = 0;
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200607 hpet->min_tick = CONFIG_HPET_MIN_TICKS;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000608
Uwe Hermann622824c2010-11-19 15:14:42 +0000609 header->checksum = acpi_checksum((void *)hpet, sizeof(acpi_hpet_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000610}
Uwe Hermann622824c2010-11-19 15:14:42 +0000611
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200612void acpi_create_vfct(struct device *device,
613 struct acpi_vfct *vfct,
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700614 unsigned long (*acpi_fill_vfct)(struct device *device,
615 struct acpi_vfct *vfct_struct, unsigned long current))
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200616{
617 acpi_header_t *header = &(vfct->header);
618 unsigned long current = (unsigned long)vfct + sizeof(struct acpi_vfct);
619
620 memset((void *)vfct, 0, sizeof(struct acpi_vfct));
621
622 /* Fill out header fields. */
623 memcpy(header->signature, "VFCT", 4);
624 memcpy(header->oem_id, OEM_ID, 6);
625 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
626 memcpy(header->asl_compiler_id, ASLC, 4);
627
628 header->length = sizeof(struct acpi_vfct);
629 header->revision = 1; /* ACPI 1.0: N/A, ACPI 2.0/3.0/4.0: 1 */
630
631 current = acpi_fill_vfct(device, vfct, current);
632
633 /* (Re)calculate length and checksum. */
634 header->length = current - (unsigned long)vfct;
635 header->checksum = acpi_checksum((void *)vfct, header->length);
636}
637
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500638void acpi_create_ivrs(acpi_ivrs_t *ivrs,
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700639 unsigned long (*acpi_fill_ivrs)(acpi_ivrs_t *ivrs_struct,
640 unsigned long current))
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500641{
642 acpi_header_t *header = &(ivrs->header);
643 unsigned long current = (unsigned long)ivrs + sizeof(acpi_ivrs_t);
644
645 memset((void *)ivrs, 0, sizeof(acpi_ivrs_t));
646
647 /* Fill out header fields. */
648 memcpy(header->signature, "IVRS", 4);
649 memcpy(header->oem_id, OEM_ID, 6);
650 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
651 memcpy(header->asl_compiler_id, ASLC, 4);
652
653 header->length = sizeof(acpi_ivrs_t);
Martin Rotha66df492016-09-06 10:22:34 -0600654 header->revision = IVRS_FORMAT_FIXED;
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500655
656 current = acpi_fill_ivrs(ivrs, current);
657
658 /* (Re)calculate length and checksum. */
659 header->length = current - (unsigned long)ivrs;
660 header->checksum = acpi_checksum((void *)ivrs, header->length);
661}
662
Elyes HAOUAS9dd89cd2018-05-04 17:56:47 +0200663unsigned long acpi_write_hpet(struct device *device, unsigned long current,
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700664 acpi_rsdp_t *rsdp)
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200665{
666 acpi_hpet_t *hpet;
667
668 /*
669 * We explicitly add these tables later on:
670 */
671 printk(BIOS_DEBUG, "ACPI: * HPET\n");
672
673 hpet = (acpi_hpet_t *) current;
674 current += sizeof(acpi_hpet_t);
675 current = ALIGN(current, 16);
676 acpi_create_hpet(hpet);
677 acpi_add_table(rsdp, hpet);
678
679 return current;
680}
681
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800682void acpi_create_dbg2(acpi_dbg2_header_t *dbg2,
683 int port_type, int port_subtype,
684 acpi_addr_t *address, uint32_t address_size,
685 const char *device_path)
686{
687 uintptr_t current;
688 acpi_dbg2_device_t *device;
689 uint32_t *dbg2_addr_size;
690 acpi_header_t *header;
691 size_t path_len;
692 const char *path;
693 char *namespace;
694
695 /* Fill out header fields. */
696 current = (uintptr_t)dbg2;
697 memset(dbg2, 0, sizeof(acpi_dbg2_header_t));
698 header = &(dbg2->header);
699 header->revision = 0;
700 memcpy(header->signature, "DBG2", 4);
701 memcpy(header->oem_id, OEM_ID, 6);
702 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
703 memcpy(header->asl_compiler_id, ASLC, 4);
704
705 /* One debug device defined */
706 dbg2->devices_offset = sizeof(acpi_dbg2_header_t);
707 dbg2->devices_count = 1;
708 current += sizeof(acpi_dbg2_header_t);
709
710 /* Device comes after the header */
711 device = (acpi_dbg2_device_t *)current;
712 memset(device, 0, sizeof(acpi_dbg2_device_t));
713 current += sizeof(acpi_dbg2_device_t);
714
715 device->revision = 0;
716 device->address_count = 1;
717 device->port_type = port_type;
718 device->port_subtype = port_subtype;
719
720 /* Base Address comes after device structure */
721 memcpy((void *)current, address, sizeof(acpi_addr_t));
722 device->base_address_offset = current - (uintptr_t)device;
723 current += sizeof(acpi_addr_t);
724
725 /* Address Size comes after address structure */
726 dbg2_addr_size = (uint32_t *)current;
727 device->address_size_offset = current - (uintptr_t)device;
728 *dbg2_addr_size = address_size;
729 current += sizeof(uint32_t);
730
731 /* Namespace string comes last, use '.' if not provided */
732 path = device_path ? : ".";
733 /* Namespace string length includes NULL terminator */
734 path_len = strlen(path) + 1;
735 namespace = (char *)current;
736 device->namespace_string_length = path_len;
737 device->namespace_string_offset = current - (uintptr_t)device;
738 strncpy(namespace, path, path_len);
739 current += path_len;
740
741 /* Update structure lengths and checksum */
742 device->length = current - (uintptr_t)device;
743 header->length = current - (uintptr_t)dbg2;
744 header->checksum = acpi_checksum((uint8_t *)dbg2, header->length);
745}
746
747unsigned long acpi_write_dbg2_pci_uart(acpi_rsdp_t *rsdp, unsigned long current,
748 struct device *dev, uint8_t access_size)
749{
750 acpi_dbg2_header_t *dbg2 = (acpi_dbg2_header_t *)current;
751 struct resource *res;
752 acpi_addr_t address;
753
754 if (!dev) {
755 printk(BIOS_ERR, "%s: Device not found\n", __func__);
756 return current;
757 }
Mario Scheithauerf1eb0ea2017-11-21 13:52:53 +0100758 if (!dev->enabled) {
759 printk(BIOS_INFO, "%s: Device not enabled\n", __func__);
760 return current;
761 }
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800762 res = find_resource(dev, PCI_BASE_ADDRESS_0);
763 if (!res) {
764 printk(BIOS_ERR, "%s: Unable to find resource for %s\n",
765 __func__, dev_path(dev));
766 return current;
767 }
768
769 memset(&address, 0, sizeof(address));
770 if (res->flags & IORESOURCE_IO)
771 address.space_id = ACPI_ADDRESS_SPACE_IO;
772 else if (res->flags & IORESOURCE_MEM)
773 address.space_id = ACPI_ADDRESS_SPACE_MEMORY;
774 else {
775 printk(BIOS_ERR, "%s: Unknown address space type\n", __func__);
776 return current;
777 }
778
779 address.addrl = (uint32_t)res->base;
780 address.addrh = (uint32_t)((res->base >> 32) & 0xffffffff);
781 address.access_size = access_size;
782
783 acpi_create_dbg2(dbg2,
784 ACPI_DBG2_PORT_SERIAL,
785 ACPI_DBG2_PORT_SERIAL_16550,
786 &address, res->size,
787 acpi_device_path(dev));
788
789 if (dbg2->header.length) {
790 current += dbg2->header.length;
791 current = acpi_align_current(current);
792 acpi_add_table(rsdp, dbg2);
793 }
794
795 return current;
796}
797
Stefan Reinauer777224c2005-01-19 14:06:41 +0000798void acpi_create_facs(acpi_facs_t *facs)
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000799{
Uwe Hermann622824c2010-11-19 15:14:42 +0000800 memset((void *)facs, 0, sizeof(acpi_facs_t));
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000801
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000802 memcpy(facs->signature, "FACS", 4);
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000803 facs->length = sizeof(acpi_facs_t);
804 facs->hardware_signature = 0;
805 facs->firmware_waking_vector = 0;
806 facs->global_lock = 0;
807 facs->flags = 0;
808 facs->x_firmware_waking_vector_l = 0;
809 facs->x_firmware_waking_vector_h = 0;
Uwe Hermann622824c2010-11-19 15:14:42 +0000810 facs->version = 1; /* ACPI 1.0: 0, ACPI 2.0/3.0: 1, ACPI 4.0: 2 */
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000811}
Stefan Reinauer777224c2005-01-19 14:06:41 +0000812
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100813static void acpi_write_rsdt(acpi_rsdt_t *rsdt, char *oem_id, char *oem_table_id)
Stefan Reinauer14e22772010-04-27 06:56:47 +0000814{
Uwe Hermann622824c2010-11-19 15:14:42 +0000815 acpi_header_t *header = &(rsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000816
Uwe Hermann622824c2010-11-19 15:14:42 +0000817 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000818 memcpy(header->signature, "RSDT", 4);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100819 memcpy(header->oem_id, oem_id, 6);
820 memcpy(header->oem_table_id, oem_table_id, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000821 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000822
Stefan Reinauer688b3852004-01-28 16:56:14 +0000823 header->length = sizeof(acpi_rsdt_t);
Uwe Hermann622824c2010-11-19 15:14:42 +0000824 header->revision = 1; /* ACPI 1.0/2.0/3.0/4.0: 1 */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000825
Uwe Hermann622824c2010-11-19 15:14:42 +0000826 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauer688b3852004-01-28 16:56:14 +0000827
Uwe Hermann622824c2010-11-19 15:14:42 +0000828 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000829 header->checksum = acpi_checksum((void *)rsdt, sizeof(acpi_rsdt_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000830}
831
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100832static void acpi_write_xsdt(acpi_xsdt_t *xsdt, char *oem_id, char *oem_table_id)
Stefan Reinauer14e22772010-04-27 06:56:47 +0000833{
Uwe Hermann622824c2010-11-19 15:14:42 +0000834 acpi_header_t *header = &(xsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000835
Uwe Hermann622824c2010-11-19 15:14:42 +0000836 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000837 memcpy(header->signature, "XSDT", 4);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100838 memcpy(header->oem_id, oem_id, 6);
839 memcpy(header->oem_table_id, oem_table_id, 8);
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000840 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000841
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000842 header->length = sizeof(acpi_xsdt_t);
Uwe Hermann622824c2010-11-19 15:14:42 +0000843 header->revision = 1; /* ACPI 1.0: N/A, 2.0/3.0/4.0: 1 */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000844
Uwe Hermann622824c2010-11-19 15:14:42 +0000845 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000846
Uwe Hermann622824c2010-11-19 15:14:42 +0000847 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000848 header->checksum = acpi_checksum((void *)xsdt, sizeof(acpi_xsdt_t));
849}
850
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100851static void acpi_write_rsdp(acpi_rsdp_t *rsdp, acpi_rsdt_t *rsdt,
852 acpi_xsdt_t *xsdt, char *oem_id)
Stefan Reinauer688b3852004-01-28 16:56:14 +0000853{
Stefan Reinauerd18faac2009-11-05 18:06:43 +0000854 memset(rsdp, 0, sizeof(acpi_rsdp_t));
Uwe Hermann622824c2010-11-19 15:14:42 +0000855
Stefan Reinauer688b3852004-01-28 16:56:14 +0000856 memcpy(rsdp->signature, RSDP_SIG, 8);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100857 memcpy(rsdp->oem_id, oem_id, 6);
Uwe Hermann622824c2010-11-19 15:14:42 +0000858
859 rsdp->length = sizeof(acpi_rsdp_t);
Stefan Reinauerdefee172015-06-18 01:11:19 -0700860 rsdp->rsdt_address = (uintptr_t)rsdt;
Uwe Hermann622824c2010-11-19 15:14:42 +0000861
862 /*
863 * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2.
864 *
865 * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
866 * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
867 * revision 0).
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000868 */
869 if (xsdt == NULL) {
Uwe Hermann622824c2010-11-19 15:14:42 +0000870 rsdp->revision = 0;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000871 } else {
Stefan Reinauerdefee172015-06-18 01:11:19 -0700872 rsdp->xsdt_address = (u64)(uintptr_t)xsdt;
Uwe Hermann622824c2010-11-19 15:14:42 +0000873 rsdp->revision = 2;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000874 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000875
876 /* Calculate checksums. */
877 rsdp->checksum = acpi_checksum((void *)rsdp, 20);
878 rsdp->ext_checksum = acpi_checksum((void *)rsdp, sizeof(acpi_rsdp_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000879}
880
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700881unsigned long acpi_create_hest_error_source(acpi_hest_t *hest,
882 acpi_hest_esd_t *esd, u16 type, void *data, u16 data_len)
zbaocaf494c82012-04-13 13:57:14 +0800883{
884 acpi_header_t *header = &(hest->header);
885 acpi_hest_hen_t *hen;
886 void *pos;
887 u16 len;
888
889 pos = esd;
890 memset(pos, 0, sizeof(acpi_hest_esd_t));
891 len = 0;
892 esd->type = type; /* MCE */
893 esd->source_id = hest->error_source_count;
894 esd->flags = 0; /* FIRMWARE_FIRST */
895 esd->enabled = 1;
896 esd->prealloc_erecords = 1;
897 esd->max_section_per_record = 0x1;
898
899 len += sizeof(acpi_hest_esd_t);
900 pos = esd + 1;
901
902 switch (type) {
903 case 0: /* MCE */
904 break;
905 case 1: /* CMC */
906 hen = (acpi_hest_hen_t *) (pos);
907 memset(pos, 0, sizeof(acpi_hest_hen_t));
908 hen->type = 3; /* SCI? */
909 hen->length = sizeof(acpi_hest_hen_t);
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700910 hen->conf_we = 0; /* Configuration Write Enable. */
zbaocaf494c82012-04-13 13:57:14 +0800911 hen->poll_interval = 0;
912 hen->vector = 0;
913 hen->sw2poll_threshold_val = 0;
914 hen->sw2poll_threshold_win = 0;
915 hen->error_threshold_val = 0;
916 hen->error_threshold_win = 0;
917 len += sizeof(acpi_hest_hen_t);
918 pos = hen + 1;
919 break;
920 case 2: /* NMI */
921 case 6: /* AER Root Port */
922 case 7: /* AER Endpoint */
923 case 8: /* AER Bridge */
924 case 9: /* Generic Hardware Error Source. */
925 /* TODO: */
926 break;
927 default:
928 printk(BIOS_DEBUG, "Invalid type of Error Source.");
929 break;
930 }
Lee Leahy024b13d2017-03-16 13:41:11 -0700931 hest->error_source_count++;
zbaocaf494c82012-04-13 13:57:14 +0800932
933 memcpy(pos, data, data_len);
934 len += data_len;
935 header->length += len;
936
937 return len;
938}
939
940/* ACPI 4.0 */
Vladimir Serbinenko807127f2014-11-09 13:36:18 +0100941void acpi_write_hest(acpi_hest_t *hest,
942 unsigned long (*acpi_fill_hest)(acpi_hest_t *hest))
zbaocaf494c82012-04-13 13:57:14 +0800943{
944 acpi_header_t *header = &(hest->header);
945
946 memset(hest, 0, sizeof(acpi_hest_t));
947
948 memcpy(header->signature, "HEST", 4);
949 memcpy(header->oem_id, OEM_ID, 6);
950 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
951 memcpy(header->asl_compiler_id, ASLC, 4);
952 header->length += sizeof(acpi_hest_t);
953 header->revision = 1;
954
955 acpi_fill_hest(hest);
956
957 /* Calculate checksums. */
958 header->checksum = acpi_checksum((void *)hest, header->length);
959}
960
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +0200961#if IS_ENABLED(CONFIG_COMMON_FADT)
Lee Leahy024b13d2017-03-16 13:41:11 -0700962void acpi_create_fadt(acpi_fadt_t *fadt, acpi_facs_t *facs, void *dsdt)
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +0200963{
964 acpi_header_t *header = &(fadt->header);
965
966 memset((void *) fadt, 0, sizeof(acpi_fadt_t));
967 memcpy(header->signature, "FACP", 4);
968 header->length = sizeof(acpi_fadt_t);
Vladimir Serbinenkoab83ef02014-10-25 15:18:25 +0200969 header->revision = 4;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +0200970 memcpy(header->oem_id, OEM_ID, 6);
971 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
972 memcpy(header->asl_compiler_id, ASLC, 4);
973 header->asl_compiler_revision = 0;
974
975 fadt->firmware_ctrl = (unsigned long) facs;
976 fadt->dsdt = (unsigned long) dsdt;
977
978 fadt->x_firmware_ctl_l = (unsigned long)facs;
979 fadt->x_firmware_ctl_h = 0;
980 fadt->x_dsdt_l = (unsigned long)dsdt;
981 fadt->x_dsdt_h = 0;
982
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700983 if (IS_ENABLED(CONFIG_SYSTEM_TYPE_LAPTOP))
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +0200984 fadt->preferred_pm_profile = PM_MOBILE;
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700985 else
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +0200986 fadt->preferred_pm_profile = PM_DESKTOP;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +0200987
Vladimir Serbinenkoab83ef02014-10-25 15:18:25 +0200988 acpi_fill_fadt(fadt);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +0200989
990 header->checksum =
991 acpi_checksum((void *) fadt, header->length);
992}
993#endif
994
Aaron Durbin64031672018-04-21 14:45:32 -0600995unsigned long __weak fw_cfg_acpi_tables(unsigned long start)
Vladimir Serbinenko41877d82014-09-01 22:18:01 +0200996{
997 return 0;
998}
999
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001000unsigned long write_acpi_tables(unsigned long start)
1001{
1002 unsigned long current;
1003 acpi_rsdp_t *rsdp;
1004 acpi_rsdt_t *rsdt;
1005 acpi_xsdt_t *xsdt;
1006 acpi_fadt_t *fadt;
1007 acpi_facs_t *facs;
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001008 acpi_header_t *slic_file, *slic;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001009 acpi_header_t *ssdt;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001010 acpi_header_t *dsdt_file, *dsdt;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001011 acpi_mcfg_t *mcfg;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +02001012 acpi_tcpa_t *tcpa;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001013 acpi_madt_t *madt;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +11001014 struct device *dev;
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001015 unsigned long fw;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001016 size_t slic_size, dsdt_size;
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001017 char oem_id[6], oem_table_id[8];
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001018
1019 current = start;
1020
1021 /* Align ACPI tables to 16byte */
Aaron Durbin07a1b282015-12-10 17:07:38 -06001022 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001023
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001024 fw = fw_cfg_acpi_tables(current);
1025 if (fw)
1026 return fw;
1027
Vladimir Serbinenkoa4cf83d2015-06-02 21:40:29 +02001028 dsdt_file = cbfs_boot_map_with_leak(
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001029 CONFIG_CBFS_PREFIX "/dsdt.aml",
1030 CBFS_TYPE_RAW, &dsdt_size);
1031 if (!dsdt_file) {
1032 printk(BIOS_ERR, "No DSDT file, skipping ACPI tables\n");
1033 return current;
1034 }
1035
1036 if (dsdt_file->length > dsdt_size
Elyes HAOUAS7d87e762016-10-03 22:11:07 +02001037 || dsdt_file->length < sizeof(acpi_header_t)
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001038 || memcmp(dsdt_file->signature, "DSDT", 4) != 0) {
1039 printk(BIOS_ERR, "Invalid DSDT file, skipping ACPI tables\n");
1040 return current;
1041 }
1042
Aaron Durbin899d13d2015-05-15 23:39:23 -05001043 slic_file = cbfs_boot_map_with_leak(CONFIG_CBFS_PREFIX "/slic",
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001044 CBFS_TYPE_RAW, &slic_size);
Vladimir Serbinenko2cb29782015-05-31 11:32:09 +02001045 if (slic_file
1046 && (slic_file->length > slic_size
Elyes HAOUAS7d87e762016-10-03 22:11:07 +02001047 || slic_file->length < sizeof(acpi_header_t)
Vladimir Serbinenko2cb29782015-05-31 11:32:09 +02001048 || memcmp(slic_file->signature, "SLIC", 4) != 0)) {
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001049 slic_file = 0;
1050 }
1051
1052 if (slic_file) {
1053 memcpy(oem_id, slic_file->oem_id, 6);
1054 memcpy(oem_table_id, slic_file->oem_table_id, 8);
1055 } else {
1056 memcpy(oem_id, OEM_ID, 6);
1057 memcpy(oem_table_id, ACPI_TABLE_CREATOR, 8);
1058 }
1059
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001060 printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx.\n", start);
1061
1062 /* We need at least an RSDP and an RSDT Table */
1063 rsdp = (acpi_rsdp_t *) current;
1064 current += sizeof(acpi_rsdp_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001065 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001066 rsdt = (acpi_rsdt_t *) current;
1067 current += sizeof(acpi_rsdt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001068 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001069 xsdt = (acpi_xsdt_t *) current;
1070 current += sizeof(acpi_xsdt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001071 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001072
1073 /* clear all table memory */
1074 memset((void *) start, 0, current - start);
1075
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001076 acpi_write_rsdp(rsdp, rsdt, xsdt, oem_id);
1077 acpi_write_rsdt(rsdt, oem_id, oem_table_id);
1078 acpi_write_xsdt(xsdt, oem_id, oem_table_id);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001079
1080 printk(BIOS_DEBUG, "ACPI: * FACS\n");
Patrick Georgi133108a2015-08-08 23:20:58 +02001081 current = (ALIGN(current, 64));
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001082 facs = (acpi_facs_t *) current;
1083 current += sizeof(acpi_facs_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001084 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001085 acpi_create_facs(facs);
1086
1087 printk(BIOS_DEBUG, "ACPI: * DSDT\n");
1088 dsdt = (acpi_header_t *) current;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001089 memcpy(dsdt, dsdt_file, sizeof(acpi_header_t));
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001090 if (dsdt->length >= sizeof(acpi_header_t)) {
1091 current += sizeof(acpi_header_t);
1092
1093 acpigen_set_current((char *) current);
1094 for (dev = all_devices; dev; dev = dev->next)
Lee Leahy9c7c6f72017-03-16 11:24:09 -07001095 if (dev->ops && dev->ops->acpi_inject_dsdt_generator)
Alexander Couzensa90dad12015-04-12 21:49:46 +02001096 dev->ops->acpi_inject_dsdt_generator(dev);
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001097 current = (unsigned long) acpigen_get_current();
1098 memcpy((char *)current,
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001099 (char *)dsdt_file + sizeof(acpi_header_t),
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001100 dsdt->length - sizeof(acpi_header_t));
1101 current += dsdt->length - sizeof(acpi_header_t);
1102
1103 /* (Re)calculate length and checksum. */
1104 dsdt->length = current - (unsigned long)dsdt;
1105 dsdt->checksum = 0;
1106 dsdt->checksum = acpi_checksum((void *)dsdt, dsdt->length);
1107 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001108
Aaron Durbin07a1b282015-12-10 17:07:38 -06001109 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001110
1111 printk(BIOS_DEBUG, "ACPI: * FADT\n");
1112 fadt = (acpi_fadt_t *) current;
1113 current += sizeof(acpi_fadt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001114 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001115
1116 acpi_create_fadt(fadt, facs, dsdt);
1117 acpi_add_table(rsdp, fadt);
1118
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001119 if (slic_file) {
1120 printk(BIOS_DEBUG, "ACPI: * SLIC\n");
1121 slic = (acpi_header_t *)current;
1122 memcpy(slic, slic_file, slic_file->length);
1123 current += slic_file->length;
Aaron Durbin07a1b282015-12-10 17:07:38 -06001124 current = acpi_align_current(current);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001125 acpi_add_table(rsdp, slic);
1126 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001127
1128 printk(BIOS_DEBUG, "ACPI: * SSDT\n");
1129 ssdt = (acpi_header_t *)current;
1130 acpi_create_ssdt_generator(ssdt, ACPI_TABLE_CREATOR);
Vladimir Serbinenko9310df82014-10-10 20:40:41 +02001131 if (ssdt->length > sizeof(acpi_header_t)) {
1132 current += ssdt->length;
1133 acpi_add_table(rsdp, ssdt);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001134 current = acpi_align_current(current);
Vladimir Serbinenko9310df82014-10-10 20:40:41 +02001135 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001136
1137 printk(BIOS_DEBUG, "ACPI: * MCFG\n");
1138 mcfg = (acpi_mcfg_t *) current;
1139 acpi_create_mcfg(mcfg);
1140 if (mcfg->header.length > sizeof(acpi_mcfg_t)) {
1141 current += mcfg->header.length;
Aaron Durbin07a1b282015-12-10 17:07:38 -06001142 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001143 acpi_add_table(rsdp, mcfg);
1144 }
1145
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +02001146 printk(BIOS_DEBUG, "ACPI: * TCPA\n");
1147 tcpa = (acpi_tcpa_t *) current;
1148 acpi_create_tcpa(tcpa);
1149 if (tcpa->header.length >= sizeof(acpi_tcpa_t)) {
1150 current += tcpa->header.length;
Aaron Durbin07a1b282015-12-10 17:07:38 -06001151 current = acpi_align_current(current);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +02001152 acpi_add_table(rsdp, tcpa);
1153 }
1154
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001155 printk(BIOS_DEBUG, "ACPI: * MADT\n");
1156
1157 madt = (acpi_madt_t *) current;
1158 acpi_create_madt(madt);
1159 if (madt->header.length > sizeof(acpi_madt_t)) {
Lee Leahy024b13d2017-03-16 13:41:11 -07001160 current += madt->header.length;
1161 acpi_add_table(rsdp, madt);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001162 }
Aaron Durbin07a1b282015-12-10 17:07:38 -06001163 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001164
1165 printk(BIOS_DEBUG, "current = %lx\n", current);
1166
1167 for (dev = all_devices; dev; dev = dev->next) {
1168 if (dev->ops && dev->ops->write_acpi_tables) {
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001169 current = dev->ops->write_acpi_tables(dev, current,
1170 rsdp);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001171 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001172 }
1173 }
1174
1175 printk(BIOS_INFO, "ACPI: done.\n");
1176 return current;
1177}
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001178
Rudolf Marek33cafe52009-04-13 18:07:02 +00001179static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp)
1180{
1181 if (strncmp((char *)rsdp, RSDP_SIG, sizeof(RSDP_SIG) - 1) != 0)
1182 return NULL;
1183
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001184 printk(BIOS_DEBUG, "Looking on %p for valid checksum\n", rsdp);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001185
1186 if (acpi_checksum((void *)rsdp, 20) != 0)
1187 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001188 printk(BIOS_DEBUG, "Checksum 1 passed\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001189
1190 if ((rsdp->revision > 1) && (acpi_checksum((void *)rsdp,
1191 rsdp->length) != 0))
1192 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001193 printk(BIOS_DEBUG, "Checksum 2 passed all OK\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001194
1195 return rsdp;
1196}
1197
Rudolf Marek33cafe52009-04-13 18:07:02 +00001198void *acpi_find_wakeup_vector(void)
1199{
1200 char *p, *end;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001201 acpi_rsdt_t *rsdt;
1202 acpi_facs_t *facs;
Martin Rothc5f49262012-12-14 19:17:55 -07001203 acpi_fadt_t *fadt = NULL;
Kyösti Mälkki072d436b2016-06-17 08:44:40 +03001204 acpi_rsdp_t *rsdp = NULL;
Uwe Hermann622824c2010-11-19 15:14:42 +00001205 void *wake_vec;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001206 int i;
1207
Rudolf Marek33cafe52009-04-13 18:07:02 +00001208 if (!acpi_is_wakeup())
1209 return NULL;
1210
Uwe Hermann622824c2010-11-19 15:14:42 +00001211 printk(BIOS_DEBUG, "Trying to find the wakeup vector...\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001212
Uwe Hermann622824c2010-11-19 15:14:42 +00001213 /* Find RSDP. */
1214 for (p = (char *)0xe0000; p < (char *)0xfffff; p += 16) {
Lee Leahy0b5678f2017-03-16 16:01:40 -07001215 rsdp = valid_rsdp((acpi_rsdp_t *)p);
1216 if (rsdp)
Rudolf Marek33cafe52009-04-13 18:07:02 +00001217 break;
1218 }
1219
1220 if (rsdp == NULL)
1221 return NULL;
1222
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001223 printk(BIOS_DEBUG, "RSDP found at %p\n", rsdp);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001224 rsdt = (acpi_rsdt_t *)(uintptr_t)rsdp->rsdt_address;
Stefan Reinauer14e22772010-04-27 06:56:47 +00001225
Uwe Hermann622824c2010-11-19 15:14:42 +00001226 end = (char *)rsdt + rsdt->header.length;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001227 printk(BIOS_DEBUG, "RSDT found at %p ends at %p\n", rsdt, end);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001228
Uwe Hermann622824c2010-11-19 15:14:42 +00001229 for (i = 0; ((char *)&rsdt->entry[i]) < end; i++) {
Stefan Reinauer71a30182015-07-30 16:28:13 -07001230 fadt = (acpi_fadt_t *)(uintptr_t)rsdt->entry[i];
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001231 if (strncmp((char *)fadt, "FACP", 4) == 0)
Rudolf Marek33cafe52009-04-13 18:07:02 +00001232 break;
1233 fadt = NULL;
1234 }
1235
1236 if (fadt == NULL)
1237 return NULL;
1238
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001239 printk(BIOS_DEBUG, "FADT found at %p\n", fadt);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001240 facs = (acpi_facs_t *)(uintptr_t)fadt->firmware_ctrl;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001241
Stefan Reinauer7e9771c2009-04-22 08:17:38 +00001242 if (facs == NULL) {
Uwe Hermann622824c2010-11-19 15:14:42 +00001243 printk(BIOS_DEBUG, "No FACS found, wake up from S3 not "
1244 "possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001245 return NULL;
Stefan Reinauer7e9771c2009-04-22 08:17:38 +00001246 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001247
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001248 printk(BIOS_DEBUG, "FACS found at %p\n", facs);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001249 wake_vec = (void *)(uintptr_t)facs->firmware_waking_vector;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001250 printk(BIOS_DEBUG, "OS waking vector is %p\n", wake_vec);
Uwe Hermann622824c2010-11-19 15:14:42 +00001251
Rudolf Marek33cafe52009-04-13 18:07:02 +00001252 return wake_vec;
1253}
1254
Duncan Laurie11290c42012-10-03 19:07:05 -07001255void acpi_save_gnvs(u32 gnvs_address)
1256{
Duncan Laurie9c07c8f2013-03-22 11:08:39 -07001257 u32 *gnvs = cbmem_add(CBMEM_ID_ACPI_GNVS_PTR, sizeof(*gnvs));
Duncan Laurie11290c42012-10-03 19:07:05 -07001258 if (gnvs)
1259 *gnvs = gnvs_address;
1260}
Duncan Laurie1f6e6812016-09-19 12:04:19 -07001261
Aaron Durbin64031672018-04-21 14:45:32 -06001262__weak int acpi_get_gpe(int gpe)
Duncan Laurie1f6e6812016-09-19 12:04:19 -07001263{
1264 return -1; /* implemented by SOC */
1265}