blob: 5640ad0654873670189ece72768279be1d34e668 [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
Timothy Pearsonff8ccf02015-08-11 17:48:32 -05009 * Copyright (C) 2015 Timothy Pearson <tpearson@raptorengineeringinc.com>, Raptor Engineering
Stefan Reinauer777224c2005-01-19 14:06:41 +000010 *
Stefan Reinauer14e22772010-04-27 06:56:47 +000011 * ACPI FADT, FACS, and DSDT table support added by
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000012 * Nick Barker <nick.barker9@btinternet.com>, and those portions
Uwe Hermannc70e9fc2010-02-15 23:10:19 +000013 * Copyright (C) 2004 Nick Barker
Stefan Reinauerf622d592005-11-26 16:56:05 +000014 *
Uwe Hermannc70e9fc2010-02-15 23:10:19 +000015 * Copyright (C) 2005 ADVANCED MICRO DEVICES, INC. All Rights Reserved.
Stefan Reinauerd6edf7a2006-01-05 00:19:52 +000016 * 2005.9 yhlu add SRAT table generation
Martin Roth9df9e9392016-01-12 15:55:28 -070017 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; version 2 of the License.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
Stefan Reinauer777224c2005-01-19 14:06:41 +000026 */
27
Stefan Reinauer14e22772010-04-27 06:56:47 +000028/*
Stefan Reinauer777224c2005-01-19 14:06:41 +000029 * Each system port implementing ACPI has to provide two functions:
Stefan Reinauer14e22772010-04-27 06:56:47 +000030 *
Stefan Reinauer777224c2005-01-19 14:06:41 +000031 * write_acpi_tables()
32 * acpi_dump_apics()
Stefan Reinauer14e22772010-04-27 06:56:47 +000033 *
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000034 * See Kontron 986LCD-M port for a good example of an ACPI implementation
35 * in coreboot.
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000036 */
Stefan Reinauer688b3852004-01-28 16:56:14 +000037
38#include <console/console.h>
39#include <string.h>
40#include <arch/acpi.h>
Rudolf Marek293b5f52009-02-01 18:35:15 +000041#include <arch/acpigen.h>
Stefan Reinauer777224c2005-01-19 14:06:41 +000042#include <device/pci.h>
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000043#include <cbmem.h>
Patrick Georgic8feedd2012-02-16 18:43:25 +010044#include <cpu/x86/lapic_def.h>
Duncan Laurie11290c42012-10-03 19:07:05 -070045#include <cpu/cpu.h>
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +010046#include <cbfs.h>
Duncan Lauriecde78012011-10-19 15:32:39 -070047#include <timestamp.h>
Kyösti Mälkki9e94dbf2015-01-08 20:03:18 +020048#include <romstage_handoff.h>
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000049
50u8 acpi_checksum(u8 *table, u32 length)
Stefan Reinauer688b3852004-01-28 16:56:14 +000051{
Uwe Hermann622824c2010-11-19 15:14:42 +000052 u8 ret = 0;
Stefan Reinauer688b3852004-01-28 16:56:14 +000053 while (length--) {
54 ret += *table;
55 table++;
56 }
Stefan Reinauera7648c22004-01-29 17:31:34 +000057 return -ret;
Stefan Reinauer688b3852004-01-28 16:56:14 +000058}
59
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000060/**
Uwe Hermann622824c2010-11-19 15:14:42 +000061 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
62 * and checksum.
Stefan Reinauer06feb882004-02-03 16:11:35 +000063 */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000064void acpi_add_table(acpi_rsdp_t *rsdp, void *table)
Stefan Reinauer688b3852004-01-28 16:56:14 +000065{
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000066 int i, entries_num;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000067 acpi_rsdt_t *rsdt;
68 acpi_xsdt_t *xsdt = NULL;
69
Uwe Hermann622824c2010-11-19 15:14:42 +000070 /* The RSDT is mandatory... */
Stefan Reinauerdefee172015-06-18 01:11:19 -070071 rsdt = (acpi_rsdt_t *)(uintptr_t)rsdp->rsdt_address;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000072
Uwe Hermann622824c2010-11-19 15:14:42 +000073 /* ...while the XSDT is not. */
74 if (rsdp->xsdt_address)
Stefan Reinauerdefee172015-06-18 01:11:19 -070075 xsdt = (acpi_xsdt_t *)((uintptr_t)rsdp->xsdt_address);
Stefan Reinauer14e22772010-04-27 06:56:47 +000076
Uwe Hermann622824c2010-11-19 15:14:42 +000077 /* This should always be MAX_ACPI_TABLES. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000078 entries_num = ARRAY_SIZE(rsdt->entry);
Stefan Reinauer14e22772010-04-27 06:56:47 +000079
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000080 for (i = 0; i < entries_num; i++) {
Uwe Hermann622824c2010-11-19 15:14:42 +000081 if (rsdt->entry[i] == 0)
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000082 break;
Stefan Reinauer688b3852004-01-28 16:56:14 +000083 }
84
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000085 if (i >= entries_num) {
Uwe Hermann622824c2010-11-19 15:14:42 +000086 printk(BIOS_ERR, "ACPI: Error: Could not add ACPI table, "
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +030087 "too many tables.\n");
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000088 return;
89 }
90
Uwe Hermann622824c2010-11-19 15:14:42 +000091 /* Add table to the RSDT. */
Stefan Reinauerdefee172015-06-18 01:11:19 -070092 rsdt->entry[i] = (uintptr_t)table;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000093
Uwe Hermann622824c2010-11-19 15:14:42 +000094 /* Fix RSDT length or the kernel will assume invalid entries. */
95 rsdt->header.length = sizeof(acpi_header_t) + (sizeof(u32) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000096
Uwe Hermann622824c2010-11-19 15:14:42 +000097 /* Re-calculate checksum. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000098 rsdt->header.checksum = 0; /* Hope this won't get optimized away */
Uwe Hermann622824c2010-11-19 15:14:42 +000099 rsdt->header.checksum = acpi_checksum((u8 *)rsdt, rsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000100
Uwe Hermann622824c2010-11-19 15:14:42 +0000101 /*
102 * And now the same thing for the XSDT. We use the same index as for
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000103 * now we want the XSDT and RSDT to always be in sync in coreboot.
104 */
105 if (xsdt) {
Uwe Hermann622824c2010-11-19 15:14:42 +0000106 /* Add table to the XSDT. */
Stefan Reinauerdefee172015-06-18 01:11:19 -0700107 xsdt->entry[i] = (u64)(uintptr_t)table;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000108
Uwe Hermann622824c2010-11-19 15:14:42 +0000109 /* Fix XSDT length. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000110 xsdt->header.length = sizeof(acpi_header_t) +
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300111 (sizeof(u64) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000112
Uwe Hermann622824c2010-11-19 15:14:42 +0000113 /* Re-calculate checksum. */
114 xsdt->header.checksum = 0;
115 xsdt->header.checksum = acpi_checksum((u8 *)xsdt,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300116 xsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000117 }
118
Uwe Hermann622824c2010-11-19 15:14:42 +0000119 printk(BIOS_DEBUG, "ACPI: added table %d/%d, length now %d\n",
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300120 i + 1, entries_num, rsdt->header.length);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000121}
122
Uwe Hermann622824c2010-11-19 15:14:42 +0000123int acpi_create_mcfg_mmconfig(acpi_mcfg_mmconfig_t *mmconfig, u32 base,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300124 u16 seg_nr, u8 start, u8 end)
Stefan Reinauer26d431a2009-01-20 19:17:51 +0000125{
Vladimir Serbinenko60fccdc2014-10-05 11:05:16 +0200126 memset(mmconfig, 0, sizeof(*mmconfig));
Rudolf Mareke6409f22007-11-03 12:50:26 +0000127 mmconfig->base_address = base;
128 mmconfig->base_reserved = 0;
129 mmconfig->pci_segment_group_number = seg_nr;
130 mmconfig->start_bus_number = start;
131 mmconfig->end_bus_number = end;
Uwe Hermann622824c2010-11-19 15:14:42 +0000132
133 return sizeof(acpi_mcfg_mmconfig_t);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000134}
135
Stefan Reinauer777224c2005-01-19 14:06:41 +0000136int acpi_create_madt_lapic(acpi_madt_lapic_t *lapic, u8 cpu, u8 apic)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000137{
Uwe Hermann622824c2010-11-19 15:14:42 +0000138 lapic->type = 0; /* Local APIC structure */
139 lapic->length = sizeof(acpi_madt_lapic_t);
140 lapic->flags = (1 << 0); /* Processor/LAPIC enabled */
141 lapic->processor_id = cpu;
142 lapic->apic_id = apic;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000143
Uwe Hermann622824c2010-11-19 15:14:42 +0000144 return lapic->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000145}
146
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000147unsigned long acpi_create_madt_lapics(unsigned long current)
148{
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100149 struct device *cpu;
Duncan Laurie11290c42012-10-03 19:07:05 -0700150 int index = 0;
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000151
Uwe Hermann622824c2010-11-19 15:14:42 +0000152 for (cpu = all_devices; cpu; cpu = cpu->next) {
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000153 if ((cpu->path.type != DEVICE_PATH_APIC) ||
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800154 (cpu->bus->dev->path.type != DEVICE_PATH_CPU_CLUSTER)) {
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000155 continue;
156 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000157 if (!cpu->enabled)
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000158 continue;
Uwe Hermann622824c2010-11-19 15:14:42 +0000159 current += acpi_create_madt_lapic((acpi_madt_lapic_t *)current,
Duncan Laurie11290c42012-10-03 19:07:05 -0700160 index, cpu->path.apic.apic_id);
161 index++;
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000162 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000163
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000164 return current;
165}
166
Uwe Hermann622824c2010-11-19 15:14:42 +0000167int acpi_create_madt_ioapic(acpi_madt_ioapic_t *ioapic, u8 id, u32 addr,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300168 u32 gsi_base)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000169{
Uwe Hermann622824c2010-11-19 15:14:42 +0000170 ioapic->type = 1; /* I/O APIC structure */
171 ioapic->length = sizeof(acpi_madt_ioapic_t);
172 ioapic->reserved = 0x00;
173 ioapic->gsi_base = gsi_base;
174 ioapic->ioapic_id = id;
175 ioapic->ioapic_addr = addr;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000176
Uwe Hermann622824c2010-11-19 15:14:42 +0000177 return ioapic->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000178}
179
Stefan Reinauer777224c2005-01-19 14:06:41 +0000180int acpi_create_madt_irqoverride(acpi_madt_irqoverride_t *irqoverride,
Stefan Reinauer06feb882004-02-03 16:11:35 +0000181 u8 bus, u8 source, u32 gsirq, u16 flags)
182{
Uwe Hermann622824c2010-11-19 15:14:42 +0000183 irqoverride->type = 2; /* Interrupt source override */
184 irqoverride->length = sizeof(acpi_madt_irqoverride_t);
185 irqoverride->bus = bus;
186 irqoverride->source = source;
187 irqoverride->gsirq = gsirq;
188 irqoverride->flags = flags;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000189
Uwe Hermann622824c2010-11-19 15:14:42 +0000190 return irqoverride->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000191}
192
Stefan Reinauer777224c2005-01-19 14:06:41 +0000193int acpi_create_madt_lapic_nmi(acpi_madt_lapic_nmi_t *lapic_nmi, u8 cpu,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300194 u16 flags, u8 lint)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000195{
Uwe Hermann622824c2010-11-19 15:14:42 +0000196 lapic_nmi->type = 4; /* Local APIC NMI structure */
197 lapic_nmi->length = sizeof(acpi_madt_lapic_nmi_t);
198 lapic_nmi->flags = flags;
199 lapic_nmi->processor_id = cpu;
200 lapic_nmi->lint = lint;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000201
Uwe Hermann622824c2010-11-19 15:14:42 +0000202 return lapic_nmi->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000203}
204
Stefan Reinauer777224c2005-01-19 14:06:41 +0000205void acpi_create_madt(acpi_madt_t *madt)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000206{
Uwe Hermann622824c2010-11-19 15:14:42 +0000207 acpi_header_t *header = &(madt->header);
208 unsigned long current = (unsigned long)madt + sizeof(acpi_madt_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000209
Stefan Reinauer06feb882004-02-03 16:11:35 +0000210 memset((void *)madt, 0, sizeof(acpi_madt_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000211
Uwe Hermann622824c2010-11-19 15:14:42 +0000212 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000213 memcpy(header->signature, "APIC", 4);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000214 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000215 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000216 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000217
Stefan Reinauer06feb882004-02-03 16:11:35 +0000218 header->length = sizeof(acpi_madt_t);
Uwe Hermann622824c2010-11-19 15:14:42 +0000219 header->revision = 1; /* ACPI 1.0/2.0: 1, ACPI 3.0: 2, ACPI 4.0: 3 */
Stefan Reinauer06feb882004-02-03 16:11:35 +0000220
Uwe Hermann622824c2010-11-19 15:14:42 +0000221 madt->lapic_addr = LOCAL_APIC_ADDR;
222 madt->flags = 0x1; /* PCAT_COMPAT */
Stefan Reinauer06feb882004-02-03 16:11:35 +0000223
Stefan Reinauerf622d592005-11-26 16:56:05 +0000224 current = acpi_fill_madt(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000225
Uwe Hermann622824c2010-11-19 15:14:42 +0000226 /* (Re)calculate length and checksum. */
227 header->length = current - (unsigned long)madt;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000228
Uwe Hermann622824c2010-11-19 15:14:42 +0000229 header->checksum = acpi_checksum((void *)madt, header->length);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000230}
231
Uwe Hermann622824c2010-11-19 15:14:42 +0000232/* MCFG is defined in the PCI Firmware Specification 3.0. */
Rudolf Mareke6409f22007-11-03 12:50:26 +0000233void acpi_create_mcfg(acpi_mcfg_t *mcfg)
234{
Uwe Hermann622824c2010-11-19 15:14:42 +0000235 acpi_header_t *header = &(mcfg->header);
236 unsigned long current = (unsigned long)mcfg + sizeof(acpi_mcfg_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000237
Rudolf Mareke6409f22007-11-03 12:50:26 +0000238 memset((void *)mcfg, 0, sizeof(acpi_mcfg_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000239
Uwe Hermann622824c2010-11-19 15:14:42 +0000240 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000241 memcpy(header->signature, "MCFG", 4);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000242 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000243 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000244 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000245
Rudolf Mareke6409f22007-11-03 12:50:26 +0000246 header->length = sizeof(acpi_mcfg_t);
247 header->revision = 1;
248
249 current = acpi_fill_mcfg(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000250
Uwe Hermann622824c2010-11-19 15:14:42 +0000251 /* (Re)calculate length and checksum. */
252 header->length = current - (unsigned long)mcfg;
253 header->checksum = acpi_checksum((void *)mcfg, header->length);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000254}
255
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200256static void *get_tcpa_log(u32 *size)
257{
258 const struct cbmem_entry *ce;
259 const u32 tcpa_default_log_len = 0x10000;
260 void *lasa;
261 ce = cbmem_entry_find(CBMEM_ID_TCPA_LOG);
262 if (ce) {
263 lasa = cbmem_entry_start(ce);
264 *size = cbmem_entry_size(ce);
265 printk(BIOS_DEBUG, "TCPA log found at %p\n", lasa);
266 return lasa;
267 }
268 lasa = cbmem_add(CBMEM_ID_TCPA_LOG, tcpa_default_log_len);
269 if (!lasa) {
270 printk(BIOS_ERR, "TCPA log creation failed\n");
271 return NULL;
272 }
273
274 printk(BIOS_DEBUG, "TCPA log created at %p\n", lasa);
275 memset (lasa, 0, tcpa_default_log_len);
276
277 *size = tcpa_default_log_len;
278 return lasa;
279}
280
281static void acpi_create_tcpa(acpi_tcpa_t *tcpa)
282{
283 acpi_header_t *header = &(tcpa->header);
284 u32 tcpa_log_len;
285 void *lasa;
286
287 memset((void *)tcpa, 0, sizeof(acpi_tcpa_t));
288
289 lasa = get_tcpa_log(&tcpa_log_len);
290 if (!lasa) {
291 return;
292 }
293
294 /* Fill out header fields. */
295 memcpy(header->signature, "TCPA", 4);
296 memcpy(header->oem_id, OEM_ID, 6);
297 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
298 memcpy(header->asl_compiler_id, ASLC, 4);
299
300 header->length = sizeof(acpi_tcpa_t);
301 header->revision = 2;
302
303 tcpa->platform_class = 0;
304 tcpa->laml = tcpa_log_len;
Stefan Reinauerdefee172015-06-18 01:11:19 -0700305 tcpa->lasa = (uintptr_t) lasa;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200306
307 /* Calculate checksum. */
308 header->checksum = acpi_checksum((void *)tcpa, header->length);
309}
310
Myles Watson3fe6b702009-10-09 20:13:43 +0000311void acpi_create_ssdt_generator(acpi_header_t *ssdt, const char *oem_table_id)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000312{
Uwe Hermann622824c2010-11-19 15:14:42 +0000313 unsigned long current = (unsigned long)ssdt + sizeof(acpi_header_t);
314
Rudolf Marek293b5f52009-02-01 18:35:15 +0000315 memset((void *)ssdt, 0, sizeof(acpi_header_t));
Uwe Hermann622824c2010-11-19 15:14:42 +0000316
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000317 memcpy(&ssdt->signature, "SSDT", 4);
Uwe Hermann622824c2010-11-19 15:14:42 +0000318 ssdt->revision = 2; /* ACPI 1.0/2.0: ?, ACPI 3.0/4.0: 2 */
Rudolf Marek293b5f52009-02-01 18:35:15 +0000319 memcpy(&ssdt->oem_id, OEM_ID, 6);
320 memcpy(&ssdt->oem_table_id, oem_table_id, 8);
321 ssdt->oem_revision = 42;
Uwe Hermann622824c2010-11-19 15:14:42 +0000322 memcpy(&ssdt->asl_compiler_id, ASLC, 4);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000323 ssdt->asl_compiler_revision = 42;
324 ssdt->length = sizeof(acpi_header_t);
325
Stefan Reinauer8dcd50b2009-03-06 17:24:29 +0000326 acpigen_set_current((char *) current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200327 {
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100328 struct device *dev;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200329 for (dev = all_devices; dev; dev = dev->next)
330 if (dev->ops && dev->ops->acpi_fill_ssdt_generator) {
Alexander Couzens5eea4582015-04-12 22:18:55 +0200331 dev->ops->acpi_fill_ssdt_generator(dev);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200332 }
Vladimir Serbinenko0a669912014-10-05 14:34:17 +0200333 current = (unsigned long) acpigen_get_current();
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200334 }
Rudolf Marek293b5f52009-02-01 18:35:15 +0000335
Uwe Hermann622824c2010-11-19 15:14:42 +0000336 /* (Re)calculate length and checksum. */
Rudolf Marek293b5f52009-02-01 18:35:15 +0000337 ssdt->length = current - (unsigned long)ssdt;
338 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
339}
340
Stefan Reinauerf622d592005-11-26 16:56:05 +0000341int acpi_create_srat_lapic(acpi_srat_lapic_t *lapic, u8 node, u8 apic)
342{
Myles Watson0b4c9f02009-03-10 18:06:47 +0000343 memset((void *)lapic, 0, sizeof(acpi_srat_lapic_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000344
Uwe Hermann622824c2010-11-19 15:14:42 +0000345 lapic->type = 0; /* Processor local APIC/SAPIC affinity structure */
346 lapic->length = sizeof(acpi_srat_lapic_t);
347 lapic->flags = (1 << 0); /* Enabled (the use of this structure). */
348 lapic->proximity_domain_7_0 = node;
349 /* TODO: proximity_domain_31_8, local SAPIC EID, clock domain. */
350 lapic->apic_id = apic;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000351
Uwe Hermann622824c2010-11-19 15:14:42 +0000352 return lapic->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000353}
354
Uwe Hermann622824c2010-11-19 15:14:42 +0000355int 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 +0300356 u32 flags)
Stefan Reinauerf622d592005-11-26 16:56:05 +0000357{
Uwe Hermann622824c2010-11-19 15:14:42 +0000358 mem->type = 1; /* Memory affinity structure */
359 mem->length = sizeof(acpi_srat_mem_t);
360 mem->base_address_low = (basek << 10);
361 mem->base_address_high = (basek >> (32 - 10));
362 mem->length_low = (sizek << 10);
363 mem->length_high = (sizek >> (32 - 10));
364 mem->proximity_domain = node;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000365 mem->flags = flags;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000366
Uwe Hermann622824c2010-11-19 15:14:42 +0000367 return mem->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000368}
369
Uwe Hermann622824c2010-11-19 15:14:42 +0000370/* http://www.microsoft.com/whdc/system/sysinternals/sratdwn.mspx */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +0200371void acpi_create_srat(acpi_srat_t *srat,
372 unsigned long (*acpi_fill_srat)(unsigned long current))
Stefan Reinauerf622d592005-11-26 16:56:05 +0000373{
Uwe Hermann622824c2010-11-19 15:14:42 +0000374 acpi_header_t *header = &(srat->header);
375 unsigned long current = (unsigned long)srat + sizeof(acpi_srat_t);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000376
Uwe Hermann622824c2010-11-19 15:14:42 +0000377 memset((void *)srat, 0, sizeof(acpi_srat_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000378
Uwe Hermann622824c2010-11-19 15:14:42 +0000379 /* Fill out header fields. */
380 memcpy(header->signature, "SRAT", 4);
381 memcpy(header->oem_id, OEM_ID, 6);
382 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
383 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000384
Uwe Hermann622824c2010-11-19 15:14:42 +0000385 header->length = sizeof(acpi_srat_t);
386 header->revision = 1; /* ACPI 1.0: N/A, 2.0: 1, 3.0: 2, 4.0: 3 */
Stefan Reinauerf622d592005-11-26 16:56:05 +0000387
Uwe Hermann622824c2010-11-19 15:14:42 +0000388 srat->resv = 1; /* Spec: Reserved to 1 for backwards compatibility. */
Stefan Reinauerf622d592005-11-26 16:56:05 +0000389
Uwe Hermann622824c2010-11-19 15:14:42 +0000390 current = acpi_fill_srat(current);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000391
Uwe Hermann622824c2010-11-19 15:14:42 +0000392 /* (Re)calculate length and checksum. */
393 header->length = current - (unsigned long)srat;
394 header->checksum = acpi_checksum((void *)srat, header->length);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000395}
396
Nico Hubere561f352015-10-26 11:51:25 +0100397void acpi_create_dmar(acpi_dmar_t *dmar, enum dmar_flags flags,
Vladimir Serbinenko8d70e942014-11-09 13:22:27 +0100398 unsigned long (*acpi_fill_dmar) (unsigned long))
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200399{
400 acpi_header_t *header = &(dmar->header);
401 unsigned long current = (unsigned long)dmar + sizeof(acpi_dmar_t);
402
403 memset((void *)dmar, 0, sizeof(acpi_dmar_t));
404
405 /* Fill out header fields. */
406 memcpy(header->signature, "DMAR", 4);
407 memcpy(header->oem_id, OEM_ID, 6);
408 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
409 memcpy(header->asl_compiler_id, ASLC, 4);
410
411 header->length = sizeof(acpi_dmar_t);
412 header->revision = 1;
413
Jacob Laskaf3f654d2016-03-15 21:53:27 -0500414 dmar->host_address_width = cpu_phys_address_size() - 1;
Nico Hubere561f352015-10-26 11:51:25 +0100415 dmar->flags = flags;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200416
417 current = acpi_fill_dmar(current);
418
419 /* (Re)calculate length and checksum. */
420 header->length = current - (unsigned long)dmar;
421 header->checksum = acpi_checksum((void *)dmar, header->length);
422}
423
424unsigned long acpi_create_dmar_drhd(unsigned long current, u8 flags,
425 u16 segment, u32 bar)
426{
427 dmar_entry_t *drhd = (dmar_entry_t *)current;
428 memset(drhd, 0, sizeof(*drhd));
429 drhd->type = DMAR_DRHD;
430 drhd->length = sizeof(*drhd); /* will be fixed up later */
431 drhd->flags = flags;
432 drhd->segment = segment;
433 drhd->bar = bar;
434
435 return drhd->length;
436}
437
438void acpi_dmar_drhd_fixup(unsigned long base, unsigned long current)
439{
440 dmar_entry_t *drhd = (dmar_entry_t *)base;
441 drhd->length = current - base;
442}
443
Nico Huber6c4751d2015-10-26 12:03:54 +0100444static unsigned long acpi_create_dmar_drhd_ds(unsigned long current,
445 enum dev_scope_type type, u8 enumeration_id, u8 bus, u8 dev, u8 fn)
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200446{
Nico Huber6c4751d2015-10-26 12:03:54 +0100447 /* we don't support longer paths yet */
448 const size_t dev_scope_length = sizeof(dev_scope_t) + 2;
449
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200450 dev_scope_t *ds = (dev_scope_t *)current;
Nico Huber6c4751d2015-10-26 12:03:54 +0100451 memset(ds, 0, dev_scope_length);
452 ds->type = type;
453 ds->length = dev_scope_length;
454 ds->enumeration = enumeration_id;
455 ds->start_bus = bus;
456 ds->path[0].dev = dev;
457 ds->path[0].fn = fn;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200458
459 return ds->length;
460}
461
Nico Huber6c4751d2015-10-26 12:03:54 +0100462unsigned long acpi_create_dmar_drhd_ds_pci(unsigned long current, u8 bus,
463 u8 dev, u8 fn)
464{
465 return acpi_create_dmar_drhd_ds(current,
466 SCOPE_PCI_ENDPOINT, 0, bus, dev, fn);
467}
468
469unsigned long acpi_create_dmar_drhd_ds_ioapic(unsigned long current,
470 u8 enumeration_id, u8 bus, u8 dev, u8 fn)
471{
472 return acpi_create_dmar_drhd_ds(current,
473 SCOPE_IOAPIC, enumeration_id, bus, dev, fn);
474}
475
476unsigned long acpi_create_dmar_drhd_ds_msi_hpet(unsigned long current,
477 u8 enumeration_id, u8 bus, u8 dev, u8 fn)
478{
479 return acpi_create_dmar_drhd_ds(current,
480 SCOPE_MSI_HPET, enumeration_id, bus, dev, fn);
481}
482
Uwe Hermann622824c2010-11-19 15:14:42 +0000483/* http://h21007.www2.hp.com/portal/download/files/unprot/Itanium/slit.pdf */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +0200484void acpi_create_slit(acpi_slit_t *slit,
485 unsigned long (*acpi_fill_slit)(unsigned long current))
Yinghai Lud4b278c2006-10-04 20:46:15 +0000486{
Uwe Hermann622824c2010-11-19 15:14:42 +0000487 acpi_header_t *header = &(slit->header);
488 unsigned long current = (unsigned long)slit + sizeof(acpi_slit_t);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000489
Uwe Hermann622824c2010-11-19 15:14:42 +0000490 memset((void *)slit, 0, sizeof(acpi_slit_t));
Yinghai Lud4b278c2006-10-04 20:46:15 +0000491
Uwe Hermann622824c2010-11-19 15:14:42 +0000492 /* Fill out header fields. */
493 memcpy(header->signature, "SLIT", 4);
494 memcpy(header->oem_id, OEM_ID, 6);
495 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
496 memcpy(header->asl_compiler_id, ASLC, 4);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000497
Uwe Hermann622824c2010-11-19 15:14:42 +0000498 header->length = sizeof(acpi_slit_t);
499 header->revision = 1; /* ACPI 1.0: N/A, ACPI 2.0/3.0/4.0: 1 */
Yinghai Lud4b278c2006-10-04 20:46:15 +0000500
Uwe Hermann622824c2010-11-19 15:14:42 +0000501 current = acpi_fill_slit(current);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000502
Uwe Hermann622824c2010-11-19 15:14:42 +0000503 /* (Re)calculate length and checksum. */
504 header->length = current - (unsigned long)slit;
505 header->checksum = acpi_checksum((void *)slit, header->length);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000506}
507
Uwe Hermann622824c2010-11-19 15:14:42 +0000508/* http://www.intel.com/hardwaredesign/hpetspec_1.pdf */
Stefan Reinauer777224c2005-01-19 14:06:41 +0000509void acpi_create_hpet(acpi_hpet_t *hpet)
Stefan Reinauer688b3852004-01-28 16:56:14 +0000510{
Uwe Hermann622824c2010-11-19 15:14:42 +0000511 acpi_header_t *header = &(hpet->header);
512 acpi_addr_t *addr = &(hpet->addr);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000513
Stefan Reinauera7648c22004-01-29 17:31:34 +0000514 memset((void *)hpet, 0, sizeof(acpi_hpet_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000515
Uwe Hermann622824c2010-11-19 15:14:42 +0000516 /* Fill out header fields. */
Stefan Reinauer4704dc52009-07-22 01:11:37 +0000517 memcpy(header->signature, "HPET", 4);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000518 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000519 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000520 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000521
Stefan Reinauer688b3852004-01-28 16:56:14 +0000522 header->length = sizeof(acpi_hpet_t);
Uwe Hermann622824c2010-11-19 15:14:42 +0000523 header->revision = 1; /* Currently 1. Table added in ACPI 2.0. */
Stefan Reinauer688b3852004-01-28 16:56:14 +0000524
Uwe Hermann622824c2010-11-19 15:14:42 +0000525 /* Fill out HPET address. */
526 addr->space_id = 0; /* Memory */
527 addr->bit_width = 64;
528 addr->bit_offset = 0;
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200529 addr->addrl = CONFIG_HPET_ADDRESS & 0xffffffff;
530 addr->addrh = ((unsigned long long)CONFIG_HPET_ADDRESS) >> 32;
Stefan Reinauer688b3852004-01-28 16:56:14 +0000531
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200532 hpet->id = *(unsigned int*)CONFIG_HPET_ADDRESS;
Uwe Hermann622824c2010-11-19 15:14:42 +0000533 hpet->number = 0;
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200534 hpet->min_tick = CONFIG_HPET_MIN_TICKS;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000535
Uwe Hermann622824c2010-11-19 15:14:42 +0000536 header->checksum = acpi_checksum((void *)hpet, sizeof(acpi_hpet_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000537}
Uwe Hermann622824c2010-11-19 15:14:42 +0000538
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500539void acpi_create_ivrs(acpi_ivrs_t *ivrs,
540 unsigned long (*acpi_fill_ivrs)(acpi_ivrs_t* ivrs_struct, unsigned long current))
541{
542 acpi_header_t *header = &(ivrs->header);
543 unsigned long current = (unsigned long)ivrs + sizeof(acpi_ivrs_t);
544
545 memset((void *)ivrs, 0, sizeof(acpi_ivrs_t));
546
547 /* Fill out header fields. */
548 memcpy(header->signature, "IVRS", 4);
549 memcpy(header->oem_id, OEM_ID, 6);
550 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
551 memcpy(header->asl_compiler_id, ASLC, 4);
552
553 header->length = sizeof(acpi_ivrs_t);
554 header->revision = 1; /* ACPI 1.0: N/A, ACPI 2.0/3.0/4.0: 1 */
555
556 current = acpi_fill_ivrs(ivrs, current);
557
558 /* (Re)calculate length and checksum. */
559 header->length = current - (unsigned long)ivrs;
560 header->checksum = acpi_checksum((void *)ivrs, header->length);
561}
562
Alexander Couzens83fc32f2015-04-12 22:28:37 +0200563unsigned long acpi_write_hpet(device_t device, unsigned long current, acpi_rsdp_t *rsdp)
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200564{
565 acpi_hpet_t *hpet;
566
567 /*
568 * We explicitly add these tables later on:
569 */
570 printk(BIOS_DEBUG, "ACPI: * HPET\n");
571
572 hpet = (acpi_hpet_t *) current;
573 current += sizeof(acpi_hpet_t);
574 current = ALIGN(current, 16);
575 acpi_create_hpet(hpet);
576 acpi_add_table(rsdp, hpet);
577
578 return current;
579}
580
Stefan Reinauer777224c2005-01-19 14:06:41 +0000581void acpi_create_facs(acpi_facs_t *facs)
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000582{
Uwe Hermann622824c2010-11-19 15:14:42 +0000583 memset((void *)facs, 0, sizeof(acpi_facs_t));
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000584
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000585 memcpy(facs->signature, "FACS", 4);
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000586 facs->length = sizeof(acpi_facs_t);
587 facs->hardware_signature = 0;
588 facs->firmware_waking_vector = 0;
589 facs->global_lock = 0;
590 facs->flags = 0;
591 facs->x_firmware_waking_vector_l = 0;
592 facs->x_firmware_waking_vector_h = 0;
Uwe Hermann622824c2010-11-19 15:14:42 +0000593 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 +0000594}
Stefan Reinauer777224c2005-01-19 14:06:41 +0000595
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100596static void acpi_write_rsdt(acpi_rsdt_t *rsdt, char *oem_id, char *oem_table_id)
Stefan Reinauer14e22772010-04-27 06:56:47 +0000597{
Uwe Hermann622824c2010-11-19 15:14:42 +0000598 acpi_header_t *header = &(rsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000599
Uwe Hermann622824c2010-11-19 15:14:42 +0000600 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000601 memcpy(header->signature, "RSDT", 4);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100602 memcpy(header->oem_id, oem_id, 6);
603 memcpy(header->oem_table_id, oem_table_id, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000604 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000605
Stefan Reinauer688b3852004-01-28 16:56:14 +0000606 header->length = sizeof(acpi_rsdt_t);
Uwe Hermann622824c2010-11-19 15:14:42 +0000607 header->revision = 1; /* ACPI 1.0/2.0/3.0/4.0: 1 */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000608
Uwe Hermann622824c2010-11-19 15:14:42 +0000609 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauer688b3852004-01-28 16:56:14 +0000610
Uwe Hermann622824c2010-11-19 15:14:42 +0000611 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000612 header->checksum = acpi_checksum((void *)rsdt, sizeof(acpi_rsdt_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000613}
614
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100615static void acpi_write_xsdt(acpi_xsdt_t *xsdt, char *oem_id, char *oem_table_id)
Stefan Reinauer14e22772010-04-27 06:56:47 +0000616{
Uwe Hermann622824c2010-11-19 15:14:42 +0000617 acpi_header_t *header = &(xsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000618
Uwe Hermann622824c2010-11-19 15:14:42 +0000619 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000620 memcpy(header->signature, "XSDT", 4);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100621 memcpy(header->oem_id, oem_id, 6);
622 memcpy(header->oem_table_id, oem_table_id, 8);
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000623 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000624
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000625 header->length = sizeof(acpi_xsdt_t);
Uwe Hermann622824c2010-11-19 15:14:42 +0000626 header->revision = 1; /* ACPI 1.0: N/A, 2.0/3.0/4.0: 1 */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000627
Uwe Hermann622824c2010-11-19 15:14:42 +0000628 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000629
Uwe Hermann622824c2010-11-19 15:14:42 +0000630 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000631 header->checksum = acpi_checksum((void *)xsdt, sizeof(acpi_xsdt_t));
632}
633
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100634static void acpi_write_rsdp(acpi_rsdp_t *rsdp, acpi_rsdt_t *rsdt,
635 acpi_xsdt_t *xsdt, char *oem_id)
Stefan Reinauer688b3852004-01-28 16:56:14 +0000636{
Stefan Reinauerd18faac2009-11-05 18:06:43 +0000637 memset(rsdp, 0, sizeof(acpi_rsdp_t));
Uwe Hermann622824c2010-11-19 15:14:42 +0000638
Stefan Reinauer688b3852004-01-28 16:56:14 +0000639 memcpy(rsdp->signature, RSDP_SIG, 8);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100640 memcpy(rsdp->oem_id, oem_id, 6);
Uwe Hermann622824c2010-11-19 15:14:42 +0000641
642 rsdp->length = sizeof(acpi_rsdp_t);
Stefan Reinauerdefee172015-06-18 01:11:19 -0700643 rsdp->rsdt_address = (uintptr_t)rsdt;
Uwe Hermann622824c2010-11-19 15:14:42 +0000644
645 /*
646 * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2.
647 *
648 * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
649 * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
650 * revision 0).
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000651 */
652 if (xsdt == NULL) {
Uwe Hermann622824c2010-11-19 15:14:42 +0000653 rsdp->revision = 0;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000654 } else {
Stefan Reinauerdefee172015-06-18 01:11:19 -0700655 rsdp->xsdt_address = (u64)(uintptr_t)xsdt;
Uwe Hermann622824c2010-11-19 15:14:42 +0000656 rsdp->revision = 2;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000657 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000658
659 /* Calculate checksums. */
660 rsdp->checksum = acpi_checksum((void *)rsdp, 20);
661 rsdp->ext_checksum = acpi_checksum((void *)rsdp, sizeof(acpi_rsdp_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000662}
663
zbaocaf494c82012-04-13 13:57:14 +0800664unsigned long acpi_create_hest_error_source(acpi_hest_t *hest, acpi_hest_esd_t *esd, u16 type, void *data, u16 data_len)
665{
666 acpi_header_t *header = &(hest->header);
667 acpi_hest_hen_t *hen;
668 void *pos;
669 u16 len;
670
671 pos = esd;
672 memset(pos, 0, sizeof(acpi_hest_esd_t));
673 len = 0;
674 esd->type = type; /* MCE */
675 esd->source_id = hest->error_source_count;
676 esd->flags = 0; /* FIRMWARE_FIRST */
677 esd->enabled = 1;
678 esd->prealloc_erecords = 1;
679 esd->max_section_per_record = 0x1;
680
681 len += sizeof(acpi_hest_esd_t);
682 pos = esd + 1;
683
684 switch (type) {
685 case 0: /* MCE */
686 break;
687 case 1: /* CMC */
688 hen = (acpi_hest_hen_t *) (pos);
689 memset(pos, 0, sizeof(acpi_hest_hen_t));
690 hen->type = 3; /* SCI? */
691 hen->length = sizeof(acpi_hest_hen_t);
692 hen->conf_we = 0; /* Configuration Write Enable. */
693 hen->poll_interval = 0;
694 hen->vector = 0;
695 hen->sw2poll_threshold_val = 0;
696 hen->sw2poll_threshold_win = 0;
697 hen->error_threshold_val = 0;
698 hen->error_threshold_win = 0;
699 len += sizeof(acpi_hest_hen_t);
700 pos = hen + 1;
701 break;
702 case 2: /* NMI */
703 case 6: /* AER Root Port */
704 case 7: /* AER Endpoint */
705 case 8: /* AER Bridge */
706 case 9: /* Generic Hardware Error Source. */
707 /* TODO: */
708 break;
709 default:
710 printk(BIOS_DEBUG, "Invalid type of Error Source.");
711 break;
712 }
713 hest->error_source_count ++;
714
715 memcpy(pos, data, data_len);
716 len += data_len;
717 header->length += len;
718
719 return len;
720}
721
722/* ACPI 4.0 */
Vladimir Serbinenko807127f2014-11-09 13:36:18 +0100723void acpi_write_hest(acpi_hest_t *hest,
724 unsigned long (*acpi_fill_hest)(acpi_hest_t *hest))
zbaocaf494c82012-04-13 13:57:14 +0800725{
726 acpi_header_t *header = &(hest->header);
727
728 memset(hest, 0, sizeof(acpi_hest_t));
729
730 memcpy(header->signature, "HEST", 4);
731 memcpy(header->oem_id, OEM_ID, 6);
732 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
733 memcpy(header->asl_compiler_id, ASLC, 4);
734 header->length += sizeof(acpi_hest_t);
735 header->revision = 1;
736
737 acpi_fill_hest(hest);
738
739 /* Calculate checksums. */
740 header->checksum = acpi_checksum((void *)hest, header->length);
741}
742
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +0200743#if IS_ENABLED(CONFIG_COMMON_FADT)
744void acpi_create_fadt(acpi_fadt_t *fadt,acpi_facs_t *facs, void *dsdt)
745{
746 acpi_header_t *header = &(fadt->header);
747
748 memset((void *) fadt, 0, sizeof(acpi_fadt_t));
749 memcpy(header->signature, "FACP", 4);
750 header->length = sizeof(acpi_fadt_t);
Vladimir Serbinenkoab83ef02014-10-25 15:18:25 +0200751 header->revision = 4;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +0200752 memcpy(header->oem_id, OEM_ID, 6);
753 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
754 memcpy(header->asl_compiler_id, ASLC, 4);
755 header->asl_compiler_revision = 0;
756
757 fadt->firmware_ctrl = (unsigned long) facs;
758 fadt->dsdt = (unsigned long) dsdt;
759
760 fadt->x_firmware_ctl_l = (unsigned long)facs;
761 fadt->x_firmware_ctl_h = 0;
762 fadt->x_dsdt_l = (unsigned long)dsdt;
763 fadt->x_dsdt_h = 0;
764
765 if(IS_ENABLED(CONFIG_SYSTEM_TYPE_LAPTOP)) {
766 fadt->preferred_pm_profile = PM_MOBILE;
767 } else {
768 fadt->preferred_pm_profile = PM_DESKTOP;
769 }
770
Vladimir Serbinenkoab83ef02014-10-25 15:18:25 +0200771 acpi_fill_fadt(fadt);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +0200772
773 header->checksum =
774 acpi_checksum((void *) fadt, header->length);
775}
776#endif
777
Vladimir Serbinenko41877d82014-09-01 22:18:01 +0200778unsigned long __attribute__ ((weak)) fw_cfg_acpi_tables(unsigned long start)
779{
780 return 0;
781}
782
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200783unsigned long write_acpi_tables(unsigned long start)
784{
785 unsigned long current;
786 acpi_rsdp_t *rsdp;
787 acpi_rsdt_t *rsdt;
788 acpi_xsdt_t *xsdt;
789 acpi_fadt_t *fadt;
790 acpi_facs_t *facs;
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100791 acpi_header_t *slic_file, *slic;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200792 acpi_header_t *ssdt;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +0200793 acpi_header_t *dsdt_file, *dsdt;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200794 acpi_mcfg_t *mcfg;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200795 acpi_tcpa_t *tcpa;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200796 acpi_madt_t *madt;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100797 struct device *dev;
Vladimir Serbinenko41877d82014-09-01 22:18:01 +0200798 unsigned long fw;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +0200799 size_t slic_size, dsdt_size;
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100800 char oem_id[6], oem_table_id[8];
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200801
802 current = start;
803
804 /* Align ACPI tables to 16byte */
Aaron Durbin07a1b282015-12-10 17:07:38 -0600805 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200806
Vladimir Serbinenko41877d82014-09-01 22:18:01 +0200807 fw = fw_cfg_acpi_tables(current);
808 if (fw)
809 return fw;
810
Vladimir Serbinenkoa4cf83d2015-06-02 21:40:29 +0200811 dsdt_file = cbfs_boot_map_with_leak(
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +0200812 CONFIG_CBFS_PREFIX "/dsdt.aml",
813 CBFS_TYPE_RAW, &dsdt_size);
814 if (!dsdt_file) {
815 printk(BIOS_ERR, "No DSDT file, skipping ACPI tables\n");
816 return current;
817 }
818
819 if (dsdt_file->length > dsdt_size
820 || dsdt_file->length < sizeof (acpi_header_t)
821 || memcmp(dsdt_file->signature, "DSDT", 4) != 0) {
822 printk(BIOS_ERR, "Invalid DSDT file, skipping ACPI tables\n");
823 return current;
824 }
825
Aaron Durbin899d13d2015-05-15 23:39:23 -0500826 slic_file = cbfs_boot_map_with_leak(CONFIG_CBFS_PREFIX "/slic",
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100827 CBFS_TYPE_RAW, &slic_size);
Vladimir Serbinenko2cb29782015-05-31 11:32:09 +0200828 if (slic_file
829 && (slic_file->length > slic_size
830 || slic_file->length < sizeof (acpi_header_t)
831 || memcmp(slic_file->signature, "SLIC", 4) != 0)) {
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100832 slic_file = 0;
833 }
834
835 if (slic_file) {
836 memcpy(oem_id, slic_file->oem_id, 6);
837 memcpy(oem_table_id, slic_file->oem_table_id, 8);
838 } else {
839 memcpy(oem_id, OEM_ID, 6);
840 memcpy(oem_table_id, ACPI_TABLE_CREATOR, 8);
841 }
842
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200843 printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx.\n", start);
844
845 /* We need at least an RSDP and an RSDT Table */
846 rsdp = (acpi_rsdp_t *) current;
847 current += sizeof(acpi_rsdp_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -0600848 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200849 rsdt = (acpi_rsdt_t *) current;
850 current += sizeof(acpi_rsdt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -0600851 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200852 xsdt = (acpi_xsdt_t *) current;
853 current += sizeof(acpi_xsdt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -0600854 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200855
856 /* clear all table memory */
857 memset((void *) start, 0, current - start);
858
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100859 acpi_write_rsdp(rsdp, rsdt, xsdt, oem_id);
860 acpi_write_rsdt(rsdt, oem_id, oem_table_id);
861 acpi_write_xsdt(xsdt, oem_id, oem_table_id);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200862
863 printk(BIOS_DEBUG, "ACPI: * FACS\n");
Patrick Georgi133108a2015-08-08 23:20:58 +0200864 current = (ALIGN(current, 64));
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200865 facs = (acpi_facs_t *) current;
866 current += sizeof(acpi_facs_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -0600867 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200868 acpi_create_facs(facs);
869
870 printk(BIOS_DEBUG, "ACPI: * DSDT\n");
871 dsdt = (acpi_header_t *) current;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +0200872 memcpy(dsdt, dsdt_file, sizeof(acpi_header_t));
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +0200873 if (dsdt->length >= sizeof(acpi_header_t)) {
874 current += sizeof(acpi_header_t);
875
876 acpigen_set_current((char *) current);
877 for (dev = all_devices; dev; dev = dev->next)
878 if (dev->ops && dev->ops->acpi_inject_dsdt_generator) {
Alexander Couzensa90dad12015-04-12 21:49:46 +0200879 dev->ops->acpi_inject_dsdt_generator(dev);
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +0200880 }
881 current = (unsigned long) acpigen_get_current();
882 memcpy((char *)current,
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +0200883 (char *)dsdt_file + sizeof(acpi_header_t),
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +0200884 dsdt->length - sizeof(acpi_header_t));
885 current += dsdt->length - sizeof(acpi_header_t);
886
887 /* (Re)calculate length and checksum. */
888 dsdt->length = current - (unsigned long)dsdt;
889 dsdt->checksum = 0;
890 dsdt->checksum = acpi_checksum((void *)dsdt, dsdt->length);
891 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200892
Aaron Durbin07a1b282015-12-10 17:07:38 -0600893 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200894
895 printk(BIOS_DEBUG, "ACPI: * FADT\n");
896 fadt = (acpi_fadt_t *) current;
897 current += sizeof(acpi_fadt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -0600898 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200899
900 acpi_create_fadt(fadt, facs, dsdt);
901 acpi_add_table(rsdp, fadt);
902
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100903 if (slic_file) {
904 printk(BIOS_DEBUG, "ACPI: * SLIC\n");
905 slic = (acpi_header_t *)current;
906 memcpy(slic, slic_file, slic_file->length);
907 current += slic_file->length;
Aaron Durbin07a1b282015-12-10 17:07:38 -0600908 current = acpi_align_current(current);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100909 acpi_add_table(rsdp, slic);
910 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200911
912 printk(BIOS_DEBUG, "ACPI: * SSDT\n");
913 ssdt = (acpi_header_t *)current;
914 acpi_create_ssdt_generator(ssdt, ACPI_TABLE_CREATOR);
Vladimir Serbinenko9310df82014-10-10 20:40:41 +0200915 if (ssdt->length > sizeof(acpi_header_t)) {
916 current += ssdt->length;
917 acpi_add_table(rsdp, ssdt);
Aaron Durbin07a1b282015-12-10 17:07:38 -0600918 current = acpi_align_current(current);
Vladimir Serbinenko9310df82014-10-10 20:40:41 +0200919 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200920
921 printk(BIOS_DEBUG, "ACPI: * MCFG\n");
922 mcfg = (acpi_mcfg_t *) current;
923 acpi_create_mcfg(mcfg);
924 if (mcfg->header.length > sizeof(acpi_mcfg_t)) {
925 current += mcfg->header.length;
Aaron Durbin07a1b282015-12-10 17:07:38 -0600926 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200927 acpi_add_table(rsdp, mcfg);
928 }
929
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200930 printk(BIOS_DEBUG, "ACPI: * TCPA\n");
931 tcpa = (acpi_tcpa_t *) current;
932 acpi_create_tcpa(tcpa);
933 if (tcpa->header.length >= sizeof(acpi_tcpa_t)) {
934 current += tcpa->header.length;
Aaron Durbin07a1b282015-12-10 17:07:38 -0600935 current = acpi_align_current(current);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200936 acpi_add_table(rsdp, tcpa);
937 }
938
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200939 printk(BIOS_DEBUG, "ACPI: * MADT\n");
940
941 madt = (acpi_madt_t *) current;
942 acpi_create_madt(madt);
943 if (madt->header.length > sizeof(acpi_madt_t)) {
944 current+=madt->header.length;
945 acpi_add_table(rsdp,madt);
946 }
Aaron Durbin07a1b282015-12-10 17:07:38 -0600947 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200948
949 printk(BIOS_DEBUG, "current = %lx\n", current);
950
951 for (dev = all_devices; dev; dev = dev->next) {
952 if (dev->ops && dev->ops->write_acpi_tables) {
Alexander Couzens83fc32f2015-04-12 22:28:37 +0200953 current = dev->ops->write_acpi_tables(dev, current, rsdp);
Aaron Durbin07a1b282015-12-10 17:07:38 -0600954 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200955 }
956 }
957
958 printk(BIOS_INFO, "ACPI: done.\n");
959 return current;
960}
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200961
Patrick Georgie1667822012-05-05 15:29:32 +0200962#if CONFIG_HAVE_ACPI_RESUME
Kyösti Mälkki134f5042014-12-26 13:29:09 +0200963void __attribute__((weak)) mainboard_suspend_resume(void)
964{
965}
966
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500967void acpi_resume(void *wake_vec)
Stefan Reinauer7e9771c2009-04-22 08:17:38 +0000968{
Duncan Laurie11290c42012-10-03 19:07:05 -0700969#if CONFIG_HAVE_SMI_HANDLER
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500970 u32 *gnvs_address = cbmem_find(CBMEM_ID_ACPI_GNVS_PTR);
Duncan Laurie11290c42012-10-03 19:07:05 -0700971
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500972 /* Restore GNVS pointer in SMM if found */
973 if (gnvs_address && *gnvs_address) {
974 printk(BIOS_DEBUG, "Restore GNVS pointer to 0x%08x\n",
975 *gnvs_address);
976 smm_setup_structures((void *)*gnvs_address, NULL, NULL);
Stefan Reinauercb91e152012-04-03 23:28:22 +0200977 }
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500978#endif
979
980 /* Call mainboard resume handler first, if defined. */
Kyösti Mälkki134f5042014-12-26 13:29:09 +0200981 mainboard_suspend_resume();
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500982
983 post_code(POST_OS_RESUME);
984 acpi_jump_to_wakeup(wake_vec);
Stefan Reinauer7e9771c2009-04-22 08:17:38 +0000985}
986
Kyösti Mälkki7a846e72015-01-09 23:55:09 +0200987/* This is filled with acpi_is_wakeup() call early in ramstage. */
988int acpi_slp_type = -1;
Stefan Reinauer688b3852004-01-28 16:56:14 +0000989
Kyösti Mälkki9e94dbf2015-01-08 20:03:18 +0200990#if IS_ENABLED(CONFIG_EARLY_CBMEM_INIT)
991int acpi_get_sleep_type(void)
Kyösti Mälkki134f5042014-12-26 13:29:09 +0200992{
Kyösti Mälkki9e94dbf2015-01-08 20:03:18 +0200993 struct romstage_handoff *handoff;
994
995 handoff = cbmem_find(CBMEM_ID_ROMSTAGE_INFO);
996
997 if (handoff == NULL) {
998 printk(BIOS_DEBUG, "Unknown boot method, assuming normal.\n");
999 return 0;
1000 } else if (handoff->s3_resume) {
1001 printk(BIOS_DEBUG, "S3 Resume.\n");
1002 return 3;
1003 } else {
1004 printk(BIOS_DEBUG, "Normal boot.\n");
1005 return 0;
1006 }
Kyösti Mälkki134f5042014-12-26 13:29:09 +02001007}
Kyösti Mälkki9e94dbf2015-01-08 20:03:18 +02001008#endif
Kyösti Mälkki134f5042014-12-26 13:29:09 +02001009
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +03001010static void acpi_handoff_wakeup(void)
1011{
Kyösti Mälkki7a846e72015-01-09 23:55:09 +02001012 if (acpi_slp_type < 0)
1013 acpi_slp_type = acpi_get_sleep_type();
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +03001014}
1015
Kyösti Mälkkicb28f3f2014-01-03 15:15:22 +02001016int acpi_is_wakeup(void)
Rudolf Marek33cafe52009-04-13 18:07:02 +00001017{
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +03001018 acpi_handoff_wakeup();
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +01001019 /* Both resume from S2 and resume from S3 restart at CPU reset */
1020 return (acpi_slp_type == 3 || acpi_slp_type == 2);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001021}
1022
Kyösti Mälkki4d9b7722014-06-19 19:45:40 +03001023int acpi_is_wakeup_s3(void)
1024{
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +03001025 acpi_handoff_wakeup();
Kyösti Mälkki4d9b7722014-06-19 19:45:40 +03001026 return (acpi_slp_type == 3);
1027}
1028
zbao1897c2c2015-11-05 20:25:59 +08001029int acpi_is_wakeup_s4(void)
1030{
1031 acpi_handoff_wakeup();
1032 return (acpi_slp_type == 4);
1033}
1034
Kyösti Mälkkicb28f3f2014-01-03 15:15:22 +02001035void acpi_fail_wakeup(void)
1036{
1037 if (acpi_slp_type == 3 || acpi_slp_type == 2)
1038 acpi_slp_type = 0;
1039}
1040
Kyösti Mälkki7b14f082014-10-16 20:58:47 +03001041void acpi_prepare_resume_backup(void)
1042{
1043 if (!acpi_s3_resume_allowed())
1044 return;
1045
1046 /* Let's prepare the ACPI S3 Resume area now already, so we can rely on
1047 * it being there during reboot time. We don't need the pointer, nor
1048 * the result right now. If it fails, ACPI resume will be disabled.
1049 */
1050
1051 if (HIGH_MEMORY_SAVE)
1052 cbmem_add(CBMEM_ID_RESUME, HIGH_MEMORY_SAVE);
Kyösti Mälkki7b14f082014-10-16 20:58:47 +03001053}
1054
Rudolf Marek33cafe52009-04-13 18:07:02 +00001055static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp)
1056{
1057 if (strncmp((char *)rsdp, RSDP_SIG, sizeof(RSDP_SIG) - 1) != 0)
1058 return NULL;
1059
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001060 printk(BIOS_DEBUG, "Looking on %p for valid checksum\n", rsdp);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001061
1062 if (acpi_checksum((void *)rsdp, 20) != 0)
1063 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001064 printk(BIOS_DEBUG, "Checksum 1 passed\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001065
1066 if ((rsdp->revision > 1) && (acpi_checksum((void *)rsdp,
1067 rsdp->length) != 0))
1068 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001069 printk(BIOS_DEBUG, "Checksum 2 passed all OK\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001070
1071 return rsdp;
1072}
1073
1074static acpi_rsdp_t *rsdp;
1075
1076void *acpi_get_wakeup_rsdp(void)
1077{
1078 return rsdp;
1079}
1080
1081void *acpi_find_wakeup_vector(void)
1082{
1083 char *p, *end;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001084 acpi_rsdt_t *rsdt;
1085 acpi_facs_t *facs;
Martin Rothc5f49262012-12-14 19:17:55 -07001086 acpi_fadt_t *fadt = NULL;
Uwe Hermann622824c2010-11-19 15:14:42 +00001087 void *wake_vec;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001088 int i;
1089
1090 rsdp = NULL;
1091
1092 if (!acpi_is_wakeup())
1093 return NULL;
1094
Uwe Hermann622824c2010-11-19 15:14:42 +00001095 printk(BIOS_DEBUG, "Trying to find the wakeup vector...\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001096
Uwe Hermann622824c2010-11-19 15:14:42 +00001097 /* Find RSDP. */
1098 for (p = (char *)0xe0000; p < (char *)0xfffff; p += 16) {
1099 if ((rsdp = valid_rsdp((acpi_rsdp_t *)p)))
Rudolf Marek33cafe52009-04-13 18:07:02 +00001100 break;
1101 }
1102
1103 if (rsdp == NULL)
1104 return NULL;
1105
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001106 printk(BIOS_DEBUG, "RSDP found at %p\n", rsdp);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001107 rsdt = (acpi_rsdt_t *)(uintptr_t)rsdp->rsdt_address;
Stefan Reinauer14e22772010-04-27 06:56:47 +00001108
Uwe Hermann622824c2010-11-19 15:14:42 +00001109 end = (char *)rsdt + rsdt->header.length;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001110 printk(BIOS_DEBUG, "RSDT found at %p ends at %p\n", rsdt, end);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001111
Uwe Hermann622824c2010-11-19 15:14:42 +00001112 for (i = 0; ((char *)&rsdt->entry[i]) < end; i++) {
Stefan Reinauer71a30182015-07-30 16:28:13 -07001113 fadt = (acpi_fadt_t *)(uintptr_t)rsdt->entry[i];
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001114 if (strncmp((char *)fadt, "FACP", 4) == 0)
Rudolf Marek33cafe52009-04-13 18:07:02 +00001115 break;
1116 fadt = NULL;
1117 }
1118
1119 if (fadt == NULL)
1120 return NULL;
1121
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001122 printk(BIOS_DEBUG, "FADT found at %p\n", fadt);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001123 facs = (acpi_facs_t *)(uintptr_t)fadt->firmware_ctrl;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001124
Stefan Reinauer7e9771c2009-04-22 08:17:38 +00001125 if (facs == NULL) {
Uwe Hermann622824c2010-11-19 15:14:42 +00001126 printk(BIOS_DEBUG, "No FACS found, wake up from S3 not "
1127 "possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001128 return NULL;
Stefan Reinauer7e9771c2009-04-22 08:17:38 +00001129 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001130
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001131 printk(BIOS_DEBUG, "FACS found at %p\n", facs);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001132 wake_vec = (void *)(uintptr_t)facs->firmware_waking_vector;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001133 printk(BIOS_DEBUG, "OS waking vector is %p\n", wake_vec);
Uwe Hermann622824c2010-11-19 15:14:42 +00001134
Rudolf Marek33cafe52009-04-13 18:07:02 +00001135 return wake_vec;
1136}
1137
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +01001138#if CONFIG_SMP
Rudolf Marek33cafe52009-04-13 18:07:02 +00001139extern char *lowmem_backup;
1140extern char *lowmem_backup_ptr;
1141extern int lowmem_backup_size;
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +01001142#endif
Rudolf Marek33cafe52009-04-13 18:07:02 +00001143
Uwe Hermann622824c2010-11-19 15:14:42 +00001144#define WAKEUP_BASE 0x600
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001145
Stefan Reinauer71a30182015-07-30 16:28:13 -07001146void (*acpi_do_wakeup)(uintptr_t vector, u32 backup_source, u32 backup_target,
Stefan Reinauer399486e2012-12-06 13:54:29 -08001147 u32 backup_size) asmlinkage = (void *)WAKEUP_BASE;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001148
Aaron Durbina146d582013-02-08 16:56:51 -06001149extern unsigned char __wakeup;
1150extern unsigned int __wakeup_size;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001151
Rudolf Marek33cafe52009-04-13 18:07:02 +00001152void acpi_jump_to_wakeup(void *vector)
1153{
Stefan Reinauer71a30182015-07-30 16:28:13 -07001154 uintptr_t acpi_backup_memory = 0;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001155
Kyösti Mälkki2ca2afe2014-06-17 15:41:37 +03001156 if (HIGH_MEMORY_SAVE && acpi_s3_resume_allowed()) {
Stefan Reinauer71a30182015-07-30 16:28:13 -07001157 acpi_backup_memory = (uintptr_t)cbmem_find(CBMEM_ID_RESUME);
Kyösti Mälkki2ca2afe2014-06-17 15:41:37 +03001158
1159 if (!acpi_backup_memory) {
1160 printk(BIOS_WARNING, "ACPI: Backup memory missing. "
1161 "No S3 resume.\n");
1162 return;
1163 }
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001164 }
1165
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +01001166#if CONFIG_SMP
Martin Roth7b5f8ef2013-07-08 16:22:10 -06001167 // FIXME: This should go into the ACPI backup memory, too. No pork sausages.
Uwe Hermann622824c2010-11-19 15:14:42 +00001168 /*
1169 * Just restore the SMP trampoline and continue with wakeup on
1170 * assembly level.
1171 */
Rudolf Marek33cafe52009-04-13 18:07:02 +00001172 memcpy(lowmem_backup_ptr, lowmem_backup, lowmem_backup_size);
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +01001173#endif
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001174
Uwe Hermann622824c2010-11-19 15:14:42 +00001175 /* Copy wakeup trampoline in place. */
Aaron Durbina146d582013-02-08 16:56:51 -06001176 memcpy((void *)WAKEUP_BASE, &__wakeup, __wakeup_size);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001177
Duncan Lauriecde78012011-10-19 15:32:39 -07001178 timestamp_add_now(TS_ACPI_WAKE_JUMP);
Duncan Lauriecde78012011-10-19 15:32:39 -07001179
Stefan Reinauer71a30182015-07-30 16:28:13 -07001180 acpi_do_wakeup((uintptr_t)vector, acpi_backup_memory, CONFIG_RAMBASE,
Uwe Hermann622824c2010-11-19 15:14:42 +00001181 HIGH_MEMORY_SAVE);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001182}
1183#endif
Duncan Laurie11290c42012-10-03 19:07:05 -07001184
1185void acpi_save_gnvs(u32 gnvs_address)
1186{
Duncan Laurie9c07c8f2013-03-22 11:08:39 -07001187 u32 *gnvs = cbmem_add(CBMEM_ID_ACPI_GNVS_PTR, sizeof(*gnvs));
Duncan Laurie11290c42012-10-03 19:07:05 -07001188 if (gnvs)
1189 *gnvs = gnvs_address;
1190}