blob: 9a031adff936b6c4bfc80be74138e6eddb2427e2 [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
414 dmar->host_address_width = 40 - 1; /* FIXME: == MTRR size? */
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
Patrick Georgic32a52c2015-06-22 21:10:34 +0200811#if CONFIG_COMPILE_IN_DSDT
812 extern char _binary_dsdt_aml_start;
813 extern char _binary_dsdt_aml_end;
814 dsdt_file = (acpi_header_t *)&_binary_dsdt_aml_start;
815 dsdt_size = (size_t)(&_binary_dsdt_aml_end - &_binary_dsdt_aml_start);
816#else
Vladimir Serbinenkoa4cf83d2015-06-02 21:40:29 +0200817 dsdt_file = cbfs_boot_map_with_leak(
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +0200818 CONFIG_CBFS_PREFIX "/dsdt.aml",
819 CBFS_TYPE_RAW, &dsdt_size);
Patrick Georgic32a52c2015-06-22 21:10:34 +0200820#endif
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +0200821 if (!dsdt_file) {
822 printk(BIOS_ERR, "No DSDT file, skipping ACPI tables\n");
823 return current;
824 }
825
826 if (dsdt_file->length > dsdt_size
827 || dsdt_file->length < sizeof (acpi_header_t)
828 || memcmp(dsdt_file->signature, "DSDT", 4) != 0) {
829 printk(BIOS_ERR, "Invalid DSDT file, skipping ACPI tables\n");
830 return current;
831 }
832
Aaron Durbin899d13d2015-05-15 23:39:23 -0500833 slic_file = cbfs_boot_map_with_leak(CONFIG_CBFS_PREFIX "/slic",
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100834 CBFS_TYPE_RAW, &slic_size);
Vladimir Serbinenko2cb29782015-05-31 11:32:09 +0200835 if (slic_file
836 && (slic_file->length > slic_size
837 || slic_file->length < sizeof (acpi_header_t)
838 || memcmp(slic_file->signature, "SLIC", 4) != 0)) {
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100839 slic_file = 0;
840 }
841
842 if (slic_file) {
843 memcpy(oem_id, slic_file->oem_id, 6);
844 memcpy(oem_table_id, slic_file->oem_table_id, 8);
845 } else {
846 memcpy(oem_id, OEM_ID, 6);
847 memcpy(oem_table_id, ACPI_TABLE_CREATOR, 8);
848 }
849
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200850 printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx.\n", start);
851
852 /* We need at least an RSDP and an RSDT Table */
853 rsdp = (acpi_rsdp_t *) current;
854 current += sizeof(acpi_rsdp_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -0600855 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200856 rsdt = (acpi_rsdt_t *) current;
857 current += sizeof(acpi_rsdt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -0600858 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200859 xsdt = (acpi_xsdt_t *) current;
860 current += sizeof(acpi_xsdt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -0600861 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200862
863 /* clear all table memory */
864 memset((void *) start, 0, current - start);
865
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100866 acpi_write_rsdp(rsdp, rsdt, xsdt, oem_id);
867 acpi_write_rsdt(rsdt, oem_id, oem_table_id);
868 acpi_write_xsdt(xsdt, oem_id, oem_table_id);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200869
870 printk(BIOS_DEBUG, "ACPI: * FACS\n");
Patrick Georgi133108a2015-08-08 23:20:58 +0200871 current = (ALIGN(current, 64));
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200872 facs = (acpi_facs_t *) current;
873 current += sizeof(acpi_facs_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -0600874 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200875 acpi_create_facs(facs);
876
877 printk(BIOS_DEBUG, "ACPI: * DSDT\n");
878 dsdt = (acpi_header_t *) current;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +0200879 memcpy(dsdt, dsdt_file, sizeof(acpi_header_t));
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +0200880 if (dsdt->length >= sizeof(acpi_header_t)) {
881 current += sizeof(acpi_header_t);
882
883 acpigen_set_current((char *) current);
884 for (dev = all_devices; dev; dev = dev->next)
885 if (dev->ops && dev->ops->acpi_inject_dsdt_generator) {
Alexander Couzensa90dad12015-04-12 21:49:46 +0200886 dev->ops->acpi_inject_dsdt_generator(dev);
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +0200887 }
888 current = (unsigned long) acpigen_get_current();
889 memcpy((char *)current,
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +0200890 (char *)dsdt_file + sizeof(acpi_header_t),
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +0200891 dsdt->length - sizeof(acpi_header_t));
892 current += dsdt->length - sizeof(acpi_header_t);
893
894 /* (Re)calculate length and checksum. */
895 dsdt->length = current - (unsigned long)dsdt;
896 dsdt->checksum = 0;
897 dsdt->checksum = acpi_checksum((void *)dsdt, dsdt->length);
898 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200899
Aaron Durbin07a1b282015-12-10 17:07:38 -0600900 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200901
902 printk(BIOS_DEBUG, "ACPI: * FADT\n");
903 fadt = (acpi_fadt_t *) current;
904 current += sizeof(acpi_fadt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -0600905 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200906
907 acpi_create_fadt(fadt, facs, dsdt);
908 acpi_add_table(rsdp, fadt);
909
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100910 if (slic_file) {
911 printk(BIOS_DEBUG, "ACPI: * SLIC\n");
912 slic = (acpi_header_t *)current;
913 memcpy(slic, slic_file, slic_file->length);
914 current += slic_file->length;
Aaron Durbin07a1b282015-12-10 17:07:38 -0600915 current = acpi_align_current(current);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100916 acpi_add_table(rsdp, slic);
917 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200918
919 printk(BIOS_DEBUG, "ACPI: * SSDT\n");
920 ssdt = (acpi_header_t *)current;
921 acpi_create_ssdt_generator(ssdt, ACPI_TABLE_CREATOR);
Vladimir Serbinenko9310df82014-10-10 20:40:41 +0200922 if (ssdt->length > sizeof(acpi_header_t)) {
923 current += ssdt->length;
924 acpi_add_table(rsdp, ssdt);
Aaron Durbin07a1b282015-12-10 17:07:38 -0600925 current = acpi_align_current(current);
Vladimir Serbinenko9310df82014-10-10 20:40:41 +0200926 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200927
928 printk(BIOS_DEBUG, "ACPI: * MCFG\n");
929 mcfg = (acpi_mcfg_t *) current;
930 acpi_create_mcfg(mcfg);
931 if (mcfg->header.length > sizeof(acpi_mcfg_t)) {
932 current += mcfg->header.length;
Aaron Durbin07a1b282015-12-10 17:07:38 -0600933 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200934 acpi_add_table(rsdp, mcfg);
935 }
936
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200937 printk(BIOS_DEBUG, "ACPI: * TCPA\n");
938 tcpa = (acpi_tcpa_t *) current;
939 acpi_create_tcpa(tcpa);
940 if (tcpa->header.length >= sizeof(acpi_tcpa_t)) {
941 current += tcpa->header.length;
Aaron Durbin07a1b282015-12-10 17:07:38 -0600942 current = acpi_align_current(current);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200943 acpi_add_table(rsdp, tcpa);
944 }
945
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200946 printk(BIOS_DEBUG, "ACPI: * MADT\n");
947
948 madt = (acpi_madt_t *) current;
949 acpi_create_madt(madt);
950 if (madt->header.length > sizeof(acpi_madt_t)) {
951 current+=madt->header.length;
952 acpi_add_table(rsdp,madt);
953 }
Aaron Durbin07a1b282015-12-10 17:07:38 -0600954 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200955
956 printk(BIOS_DEBUG, "current = %lx\n", current);
957
958 for (dev = all_devices; dev; dev = dev->next) {
959 if (dev->ops && dev->ops->write_acpi_tables) {
Alexander Couzens83fc32f2015-04-12 22:28:37 +0200960 current = dev->ops->write_acpi_tables(dev, current, rsdp);
Aaron Durbin07a1b282015-12-10 17:07:38 -0600961 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200962 }
963 }
964
965 printk(BIOS_INFO, "ACPI: done.\n");
966 return current;
967}
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200968
Patrick Georgie1667822012-05-05 15:29:32 +0200969#if CONFIG_HAVE_ACPI_RESUME
Kyösti Mälkki134f5042014-12-26 13:29:09 +0200970void __attribute__((weak)) mainboard_suspend_resume(void)
971{
972}
973
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500974void acpi_resume(void *wake_vec)
Stefan Reinauer7e9771c2009-04-22 08:17:38 +0000975{
Duncan Laurie11290c42012-10-03 19:07:05 -0700976#if CONFIG_HAVE_SMI_HANDLER
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500977 u32 *gnvs_address = cbmem_find(CBMEM_ID_ACPI_GNVS_PTR);
Duncan Laurie11290c42012-10-03 19:07:05 -0700978
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500979 /* Restore GNVS pointer in SMM if found */
980 if (gnvs_address && *gnvs_address) {
981 printk(BIOS_DEBUG, "Restore GNVS pointer to 0x%08x\n",
982 *gnvs_address);
983 smm_setup_structures((void *)*gnvs_address, NULL, NULL);
Stefan Reinauercb91e152012-04-03 23:28:22 +0200984 }
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500985#endif
986
987 /* Call mainboard resume handler first, if defined. */
Kyösti Mälkki134f5042014-12-26 13:29:09 +0200988 mainboard_suspend_resume();
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500989
990 post_code(POST_OS_RESUME);
991 acpi_jump_to_wakeup(wake_vec);
Stefan Reinauer7e9771c2009-04-22 08:17:38 +0000992}
993
Kyösti Mälkki7a846e72015-01-09 23:55:09 +0200994/* This is filled with acpi_is_wakeup() call early in ramstage. */
995int acpi_slp_type = -1;
Stefan Reinauer688b3852004-01-28 16:56:14 +0000996
Kyösti Mälkki9e94dbf2015-01-08 20:03:18 +0200997#if IS_ENABLED(CONFIG_EARLY_CBMEM_INIT)
998int acpi_get_sleep_type(void)
Kyösti Mälkki134f5042014-12-26 13:29:09 +0200999{
Kyösti Mälkki9e94dbf2015-01-08 20:03:18 +02001000 struct romstage_handoff *handoff;
1001
1002 handoff = cbmem_find(CBMEM_ID_ROMSTAGE_INFO);
1003
1004 if (handoff == NULL) {
1005 printk(BIOS_DEBUG, "Unknown boot method, assuming normal.\n");
1006 return 0;
1007 } else if (handoff->s3_resume) {
1008 printk(BIOS_DEBUG, "S3 Resume.\n");
1009 return 3;
1010 } else {
1011 printk(BIOS_DEBUG, "Normal boot.\n");
1012 return 0;
1013 }
Kyösti Mälkki134f5042014-12-26 13:29:09 +02001014}
Kyösti Mälkki9e94dbf2015-01-08 20:03:18 +02001015#endif
Kyösti Mälkki134f5042014-12-26 13:29:09 +02001016
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +03001017static void acpi_handoff_wakeup(void)
1018{
Kyösti Mälkki7a846e72015-01-09 23:55:09 +02001019 if (acpi_slp_type < 0)
1020 acpi_slp_type = acpi_get_sleep_type();
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +03001021}
1022
Kyösti Mälkkicb28f3f2014-01-03 15:15:22 +02001023int acpi_is_wakeup(void)
Rudolf Marek33cafe52009-04-13 18:07:02 +00001024{
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +03001025 acpi_handoff_wakeup();
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +01001026 /* Both resume from S2 and resume from S3 restart at CPU reset */
1027 return (acpi_slp_type == 3 || acpi_slp_type == 2);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001028}
1029
Kyösti Mälkki4d9b7722014-06-19 19:45:40 +03001030int acpi_is_wakeup_s3(void)
1031{
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +03001032 acpi_handoff_wakeup();
Kyösti Mälkki4d9b7722014-06-19 19:45:40 +03001033 return (acpi_slp_type == 3);
1034}
1035
zbao1897c2c2015-11-05 20:25:59 +08001036int acpi_is_wakeup_s4(void)
1037{
1038 acpi_handoff_wakeup();
1039 return (acpi_slp_type == 4);
1040}
1041
Kyösti Mälkkicb28f3f2014-01-03 15:15:22 +02001042void acpi_fail_wakeup(void)
1043{
1044 if (acpi_slp_type == 3 || acpi_slp_type == 2)
1045 acpi_slp_type = 0;
1046}
1047
Kyösti Mälkki7b14f082014-10-16 20:58:47 +03001048void acpi_prepare_resume_backup(void)
1049{
1050 if (!acpi_s3_resume_allowed())
1051 return;
1052
1053 /* Let's prepare the ACPI S3 Resume area now already, so we can rely on
1054 * it being there during reboot time. We don't need the pointer, nor
1055 * the result right now. If it fails, ACPI resume will be disabled.
1056 */
1057
1058 if (HIGH_MEMORY_SAVE)
1059 cbmem_add(CBMEM_ID_RESUME, HIGH_MEMORY_SAVE);
Kyösti Mälkki7b14f082014-10-16 20:58:47 +03001060}
1061
Rudolf Marek33cafe52009-04-13 18:07:02 +00001062static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp)
1063{
1064 if (strncmp((char *)rsdp, RSDP_SIG, sizeof(RSDP_SIG) - 1) != 0)
1065 return NULL;
1066
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001067 printk(BIOS_DEBUG, "Looking on %p for valid checksum\n", rsdp);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001068
1069 if (acpi_checksum((void *)rsdp, 20) != 0)
1070 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001071 printk(BIOS_DEBUG, "Checksum 1 passed\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001072
1073 if ((rsdp->revision > 1) && (acpi_checksum((void *)rsdp,
1074 rsdp->length) != 0))
1075 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001076 printk(BIOS_DEBUG, "Checksum 2 passed all OK\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001077
1078 return rsdp;
1079}
1080
1081static acpi_rsdp_t *rsdp;
1082
1083void *acpi_get_wakeup_rsdp(void)
1084{
1085 return rsdp;
1086}
1087
1088void *acpi_find_wakeup_vector(void)
1089{
1090 char *p, *end;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001091 acpi_rsdt_t *rsdt;
1092 acpi_facs_t *facs;
Martin Rothc5f49262012-12-14 19:17:55 -07001093 acpi_fadt_t *fadt = NULL;
Uwe Hermann622824c2010-11-19 15:14:42 +00001094 void *wake_vec;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001095 int i;
1096
1097 rsdp = NULL;
1098
1099 if (!acpi_is_wakeup())
1100 return NULL;
1101
Uwe Hermann622824c2010-11-19 15:14:42 +00001102 printk(BIOS_DEBUG, "Trying to find the wakeup vector...\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001103
Uwe Hermann622824c2010-11-19 15:14:42 +00001104 /* Find RSDP. */
1105 for (p = (char *)0xe0000; p < (char *)0xfffff; p += 16) {
1106 if ((rsdp = valid_rsdp((acpi_rsdp_t *)p)))
Rudolf Marek33cafe52009-04-13 18:07:02 +00001107 break;
1108 }
1109
1110 if (rsdp == NULL)
1111 return NULL;
1112
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001113 printk(BIOS_DEBUG, "RSDP found at %p\n", rsdp);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001114 rsdt = (acpi_rsdt_t *)(uintptr_t)rsdp->rsdt_address;
Stefan Reinauer14e22772010-04-27 06:56:47 +00001115
Uwe Hermann622824c2010-11-19 15:14:42 +00001116 end = (char *)rsdt + rsdt->header.length;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001117 printk(BIOS_DEBUG, "RSDT found at %p ends at %p\n", rsdt, end);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001118
Uwe Hermann622824c2010-11-19 15:14:42 +00001119 for (i = 0; ((char *)&rsdt->entry[i]) < end; i++) {
Stefan Reinauer71a30182015-07-30 16:28:13 -07001120 fadt = (acpi_fadt_t *)(uintptr_t)rsdt->entry[i];
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001121 if (strncmp((char *)fadt, "FACP", 4) == 0)
Rudolf Marek33cafe52009-04-13 18:07:02 +00001122 break;
1123 fadt = NULL;
1124 }
1125
1126 if (fadt == NULL)
1127 return NULL;
1128
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001129 printk(BIOS_DEBUG, "FADT found at %p\n", fadt);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001130 facs = (acpi_facs_t *)(uintptr_t)fadt->firmware_ctrl;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001131
Stefan Reinauer7e9771c2009-04-22 08:17:38 +00001132 if (facs == NULL) {
Uwe Hermann622824c2010-11-19 15:14:42 +00001133 printk(BIOS_DEBUG, "No FACS found, wake up from S3 not "
1134 "possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001135 return NULL;
Stefan Reinauer7e9771c2009-04-22 08:17:38 +00001136 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001137
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001138 printk(BIOS_DEBUG, "FACS found at %p\n", facs);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001139 wake_vec = (void *)(uintptr_t)facs->firmware_waking_vector;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001140 printk(BIOS_DEBUG, "OS waking vector is %p\n", wake_vec);
Uwe Hermann622824c2010-11-19 15:14:42 +00001141
Rudolf Marek33cafe52009-04-13 18:07:02 +00001142 return wake_vec;
1143}
1144
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +01001145#if CONFIG_SMP
Rudolf Marek33cafe52009-04-13 18:07:02 +00001146extern char *lowmem_backup;
1147extern char *lowmem_backup_ptr;
1148extern int lowmem_backup_size;
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +01001149#endif
Rudolf Marek33cafe52009-04-13 18:07:02 +00001150
Uwe Hermann622824c2010-11-19 15:14:42 +00001151#define WAKEUP_BASE 0x600
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001152
Stefan Reinauer71a30182015-07-30 16:28:13 -07001153void (*acpi_do_wakeup)(uintptr_t vector, u32 backup_source, u32 backup_target,
Stefan Reinauer399486e2012-12-06 13:54:29 -08001154 u32 backup_size) asmlinkage = (void *)WAKEUP_BASE;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001155
Aaron Durbina146d582013-02-08 16:56:51 -06001156extern unsigned char __wakeup;
1157extern unsigned int __wakeup_size;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001158
Rudolf Marek33cafe52009-04-13 18:07:02 +00001159void acpi_jump_to_wakeup(void *vector)
1160{
Stefan Reinauer71a30182015-07-30 16:28:13 -07001161 uintptr_t acpi_backup_memory = 0;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001162
Kyösti Mälkki2ca2afe2014-06-17 15:41:37 +03001163 if (HIGH_MEMORY_SAVE && acpi_s3_resume_allowed()) {
Stefan Reinauer71a30182015-07-30 16:28:13 -07001164 acpi_backup_memory = (uintptr_t)cbmem_find(CBMEM_ID_RESUME);
Kyösti Mälkki2ca2afe2014-06-17 15:41:37 +03001165
1166 if (!acpi_backup_memory) {
1167 printk(BIOS_WARNING, "ACPI: Backup memory missing. "
1168 "No S3 resume.\n");
1169 return;
1170 }
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001171 }
1172
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +01001173#if CONFIG_SMP
Martin Roth7b5f8ef2013-07-08 16:22:10 -06001174 // FIXME: This should go into the ACPI backup memory, too. No pork sausages.
Uwe Hermann622824c2010-11-19 15:14:42 +00001175 /*
1176 * Just restore the SMP trampoline and continue with wakeup on
1177 * assembly level.
1178 */
Rudolf Marek33cafe52009-04-13 18:07:02 +00001179 memcpy(lowmem_backup_ptr, lowmem_backup, lowmem_backup_size);
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +01001180#endif
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001181
Uwe Hermann622824c2010-11-19 15:14:42 +00001182 /* Copy wakeup trampoline in place. */
Aaron Durbina146d582013-02-08 16:56:51 -06001183 memcpy((void *)WAKEUP_BASE, &__wakeup, __wakeup_size);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001184
Duncan Lauriecde78012011-10-19 15:32:39 -07001185 timestamp_add_now(TS_ACPI_WAKE_JUMP);
Duncan Lauriecde78012011-10-19 15:32:39 -07001186
Stefan Reinauer71a30182015-07-30 16:28:13 -07001187 acpi_do_wakeup((uintptr_t)vector, acpi_backup_memory, CONFIG_RAMBASE,
Uwe Hermann622824c2010-11-19 15:14:42 +00001188 HIGH_MEMORY_SAVE);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001189}
1190#endif
Duncan Laurie11290c42012-10-03 19:07:05 -07001191
1192void acpi_save_gnvs(u32 gnvs_address)
1193{
Duncan Laurie9c07c8f2013-03-22 11:08:39 -07001194 u32 *gnvs = cbmem_add(CBMEM_ID_ACPI_GNVS_PTR, sizeof(*gnvs));
Duncan Laurie11290c42012-10-03 19:07:05 -07001195 if (gnvs)
1196 *gnvs = gnvs_address;
1197}