blob: 398dc26e6e37d9259eda8015af50c4be6bc3ac1b [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
Stefan Reinauer777224c2005-01-19 14:06:41 +000017 */
18
Stefan Reinauer14e22772010-04-27 06:56:47 +000019/*
Stefan Reinauer777224c2005-01-19 14:06:41 +000020 * Each system port implementing ACPI has to provide two functions:
Stefan Reinauer14e22772010-04-27 06:56:47 +000021 *
Stefan Reinauer777224c2005-01-19 14:06:41 +000022 * write_acpi_tables()
23 * acpi_dump_apics()
Stefan Reinauer14e22772010-04-27 06:56:47 +000024 *
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000025 * See Kontron 986LCD-M port for a good example of an ACPI implementation
26 * in coreboot.
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000027 */
Stefan Reinauer688b3852004-01-28 16:56:14 +000028
29#include <console/console.h>
30#include <string.h>
31#include <arch/acpi.h>
Rudolf Marek293b5f52009-02-01 18:35:15 +000032#include <arch/acpigen.h>
Stefan Reinauer777224c2005-01-19 14:06:41 +000033#include <device/pci.h>
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000034#include <cbmem.h>
Patrick Georgic8feedd2012-02-16 18:43:25 +010035#include <cpu/x86/lapic_def.h>
Duncan Laurie11290c42012-10-03 19:07:05 -070036#include <cpu/cpu.h>
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +010037#include <cbfs.h>
Duncan Lauriecde78012011-10-19 15:32:39 -070038#include <timestamp.h>
Kyösti Mälkki9e94dbf2015-01-08 20:03:18 +020039#include <romstage_handoff.h>
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000040
41u8 acpi_checksum(u8 *table, u32 length)
Stefan Reinauer688b3852004-01-28 16:56:14 +000042{
Uwe Hermann622824c2010-11-19 15:14:42 +000043 u8 ret = 0;
Stefan Reinauer688b3852004-01-28 16:56:14 +000044 while (length--) {
45 ret += *table;
46 table++;
47 }
Stefan Reinauera7648c22004-01-29 17:31:34 +000048 return -ret;
Stefan Reinauer688b3852004-01-28 16:56:14 +000049}
50
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000051/**
Uwe Hermann622824c2010-11-19 15:14:42 +000052 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
53 * and checksum.
Stefan Reinauer06feb882004-02-03 16:11:35 +000054 */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000055void acpi_add_table(acpi_rsdp_t *rsdp, void *table)
Stefan Reinauer688b3852004-01-28 16:56:14 +000056{
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000057 int i, entries_num;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000058 acpi_rsdt_t *rsdt;
59 acpi_xsdt_t *xsdt = NULL;
60
Uwe Hermann622824c2010-11-19 15:14:42 +000061 /* The RSDT is mandatory... */
Stefan Reinauerdefee172015-06-18 01:11:19 -070062 rsdt = (acpi_rsdt_t *)(uintptr_t)rsdp->rsdt_address;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000063
Uwe Hermann622824c2010-11-19 15:14:42 +000064 /* ...while the XSDT is not. */
65 if (rsdp->xsdt_address)
Stefan Reinauerdefee172015-06-18 01:11:19 -070066 xsdt = (acpi_xsdt_t *)((uintptr_t)rsdp->xsdt_address);
Stefan Reinauer14e22772010-04-27 06:56:47 +000067
Uwe Hermann622824c2010-11-19 15:14:42 +000068 /* This should always be MAX_ACPI_TABLES. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000069 entries_num = ARRAY_SIZE(rsdt->entry);
Stefan Reinauer14e22772010-04-27 06:56:47 +000070
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000071 for (i = 0; i < entries_num; i++) {
Uwe Hermann622824c2010-11-19 15:14:42 +000072 if (rsdt->entry[i] == 0)
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000073 break;
Stefan Reinauer688b3852004-01-28 16:56:14 +000074 }
75
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000076 if (i >= entries_num) {
Uwe Hermann622824c2010-11-19 15:14:42 +000077 printk(BIOS_ERR, "ACPI: Error: Could not add ACPI table, "
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +030078 "too many tables.\n");
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000079 return;
80 }
81
Uwe Hermann622824c2010-11-19 15:14:42 +000082 /* Add table to the RSDT. */
Stefan Reinauerdefee172015-06-18 01:11:19 -070083 rsdt->entry[i] = (uintptr_t)table;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000084
Uwe Hermann622824c2010-11-19 15:14:42 +000085 /* Fix RSDT length or the kernel will assume invalid entries. */
86 rsdt->header.length = sizeof(acpi_header_t) + (sizeof(u32) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000087
Uwe Hermann622824c2010-11-19 15:14:42 +000088 /* Re-calculate checksum. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000089 rsdt->header.checksum = 0; /* Hope this won't get optimized away */
Uwe Hermann622824c2010-11-19 15:14:42 +000090 rsdt->header.checksum = acpi_checksum((u8 *)rsdt, rsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000091
Uwe Hermann622824c2010-11-19 15:14:42 +000092 /*
93 * And now the same thing for the XSDT. We use the same index as for
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000094 * now we want the XSDT and RSDT to always be in sync in coreboot.
95 */
96 if (xsdt) {
Uwe Hermann622824c2010-11-19 15:14:42 +000097 /* Add table to the XSDT. */
Stefan Reinauerdefee172015-06-18 01:11:19 -070098 xsdt->entry[i] = (u64)(uintptr_t)table;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000099
Uwe Hermann622824c2010-11-19 15:14:42 +0000100 /* Fix XSDT length. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000101 xsdt->header.length = sizeof(acpi_header_t) +
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300102 (sizeof(u64) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000103
Uwe Hermann622824c2010-11-19 15:14:42 +0000104 /* Re-calculate checksum. */
105 xsdt->header.checksum = 0;
106 xsdt->header.checksum = acpi_checksum((u8 *)xsdt,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300107 xsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000108 }
109
Uwe Hermann622824c2010-11-19 15:14:42 +0000110 printk(BIOS_DEBUG, "ACPI: added table %d/%d, length now %d\n",
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300111 i + 1, entries_num, rsdt->header.length);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000112}
113
Uwe Hermann622824c2010-11-19 15:14:42 +0000114int acpi_create_mcfg_mmconfig(acpi_mcfg_mmconfig_t *mmconfig, u32 base,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300115 u16 seg_nr, u8 start, u8 end)
Stefan Reinauer26d431a2009-01-20 19:17:51 +0000116{
Vladimir Serbinenko60fccdc2014-10-05 11:05:16 +0200117 memset(mmconfig, 0, sizeof(*mmconfig));
Rudolf Mareke6409f22007-11-03 12:50:26 +0000118 mmconfig->base_address = base;
119 mmconfig->base_reserved = 0;
120 mmconfig->pci_segment_group_number = seg_nr;
121 mmconfig->start_bus_number = start;
122 mmconfig->end_bus_number = end;
Uwe Hermann622824c2010-11-19 15:14:42 +0000123
124 return sizeof(acpi_mcfg_mmconfig_t);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000125}
126
Stefan Reinauer777224c2005-01-19 14:06:41 +0000127int acpi_create_madt_lapic(acpi_madt_lapic_t *lapic, u8 cpu, u8 apic)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000128{
Uwe Hermann622824c2010-11-19 15:14:42 +0000129 lapic->type = 0; /* Local APIC structure */
130 lapic->length = sizeof(acpi_madt_lapic_t);
131 lapic->flags = (1 << 0); /* Processor/LAPIC enabled */
132 lapic->processor_id = cpu;
133 lapic->apic_id = apic;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000134
Uwe Hermann622824c2010-11-19 15:14:42 +0000135 return lapic->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000136}
137
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000138unsigned long acpi_create_madt_lapics(unsigned long current)
139{
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100140 struct device *cpu;
Duncan Laurie11290c42012-10-03 19:07:05 -0700141 int index = 0;
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000142
Uwe Hermann622824c2010-11-19 15:14:42 +0000143 for (cpu = all_devices; cpu; cpu = cpu->next) {
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000144 if ((cpu->path.type != DEVICE_PATH_APIC) ||
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800145 (cpu->bus->dev->path.type != DEVICE_PATH_CPU_CLUSTER)) {
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000146 continue;
147 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000148 if (!cpu->enabled)
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000149 continue;
Uwe Hermann622824c2010-11-19 15:14:42 +0000150 current += acpi_create_madt_lapic((acpi_madt_lapic_t *)current,
Duncan Laurie11290c42012-10-03 19:07:05 -0700151 index, cpu->path.apic.apic_id);
152 index++;
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000153 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000154
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000155 return current;
156}
157
Uwe Hermann622824c2010-11-19 15:14:42 +0000158int acpi_create_madt_ioapic(acpi_madt_ioapic_t *ioapic, u8 id, u32 addr,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300159 u32 gsi_base)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000160{
Uwe Hermann622824c2010-11-19 15:14:42 +0000161 ioapic->type = 1; /* I/O APIC structure */
162 ioapic->length = sizeof(acpi_madt_ioapic_t);
163 ioapic->reserved = 0x00;
164 ioapic->gsi_base = gsi_base;
165 ioapic->ioapic_id = id;
166 ioapic->ioapic_addr = addr;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000167
Uwe Hermann622824c2010-11-19 15:14:42 +0000168 return ioapic->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000169}
170
Stefan Reinauer777224c2005-01-19 14:06:41 +0000171int acpi_create_madt_irqoverride(acpi_madt_irqoverride_t *irqoverride,
Stefan Reinauer06feb882004-02-03 16:11:35 +0000172 u8 bus, u8 source, u32 gsirq, u16 flags)
173{
Uwe Hermann622824c2010-11-19 15:14:42 +0000174 irqoverride->type = 2; /* Interrupt source override */
175 irqoverride->length = sizeof(acpi_madt_irqoverride_t);
176 irqoverride->bus = bus;
177 irqoverride->source = source;
178 irqoverride->gsirq = gsirq;
179 irqoverride->flags = flags;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000180
Uwe Hermann622824c2010-11-19 15:14:42 +0000181 return irqoverride->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000182}
183
Stefan Reinauer777224c2005-01-19 14:06:41 +0000184int acpi_create_madt_lapic_nmi(acpi_madt_lapic_nmi_t *lapic_nmi, u8 cpu,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300185 u16 flags, u8 lint)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000186{
Uwe Hermann622824c2010-11-19 15:14:42 +0000187 lapic_nmi->type = 4; /* Local APIC NMI structure */
188 lapic_nmi->length = sizeof(acpi_madt_lapic_nmi_t);
189 lapic_nmi->flags = flags;
190 lapic_nmi->processor_id = cpu;
191 lapic_nmi->lint = lint;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000192
Uwe Hermann622824c2010-11-19 15:14:42 +0000193 return lapic_nmi->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000194}
195
Stefan Reinauer777224c2005-01-19 14:06:41 +0000196void acpi_create_madt(acpi_madt_t *madt)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000197{
Uwe Hermann622824c2010-11-19 15:14:42 +0000198 acpi_header_t *header = &(madt->header);
199 unsigned long current = (unsigned long)madt + sizeof(acpi_madt_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000200
Stefan Reinauer06feb882004-02-03 16:11:35 +0000201 memset((void *)madt, 0, sizeof(acpi_madt_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000202
Uwe Hermann622824c2010-11-19 15:14:42 +0000203 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000204 memcpy(header->signature, "APIC", 4);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000205 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000206 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000207 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000208
Stefan Reinauer06feb882004-02-03 16:11:35 +0000209 header->length = sizeof(acpi_madt_t);
Uwe Hermann622824c2010-11-19 15:14:42 +0000210 header->revision = 1; /* ACPI 1.0/2.0: 1, ACPI 3.0: 2, ACPI 4.0: 3 */
Stefan Reinauer06feb882004-02-03 16:11:35 +0000211
Uwe Hermann622824c2010-11-19 15:14:42 +0000212 madt->lapic_addr = LOCAL_APIC_ADDR;
213 madt->flags = 0x1; /* PCAT_COMPAT */
Stefan Reinauer06feb882004-02-03 16:11:35 +0000214
Stefan Reinauerf622d592005-11-26 16:56:05 +0000215 current = acpi_fill_madt(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000216
Uwe Hermann622824c2010-11-19 15:14:42 +0000217 /* (Re)calculate length and checksum. */
218 header->length = current - (unsigned long)madt;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000219
Uwe Hermann622824c2010-11-19 15:14:42 +0000220 header->checksum = acpi_checksum((void *)madt, header->length);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000221}
222
Uwe Hermann622824c2010-11-19 15:14:42 +0000223/* MCFG is defined in the PCI Firmware Specification 3.0. */
Rudolf Mareke6409f22007-11-03 12:50:26 +0000224void acpi_create_mcfg(acpi_mcfg_t *mcfg)
225{
Uwe Hermann622824c2010-11-19 15:14:42 +0000226 acpi_header_t *header = &(mcfg->header);
227 unsigned long current = (unsigned long)mcfg + sizeof(acpi_mcfg_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000228
Rudolf Mareke6409f22007-11-03 12:50:26 +0000229 memset((void *)mcfg, 0, sizeof(acpi_mcfg_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000230
Uwe Hermann622824c2010-11-19 15:14:42 +0000231 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000232 memcpy(header->signature, "MCFG", 4);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000233 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000234 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000235 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000236
Rudolf Mareke6409f22007-11-03 12:50:26 +0000237 header->length = sizeof(acpi_mcfg_t);
238 header->revision = 1;
239
240 current = acpi_fill_mcfg(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000241
Uwe Hermann622824c2010-11-19 15:14:42 +0000242 /* (Re)calculate length and checksum. */
243 header->length = current - (unsigned long)mcfg;
244 header->checksum = acpi_checksum((void *)mcfg, header->length);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000245}
246
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200247static void *get_tcpa_log(u32 *size)
248{
249 const struct cbmem_entry *ce;
250 const u32 tcpa_default_log_len = 0x10000;
251 void *lasa;
252 ce = cbmem_entry_find(CBMEM_ID_TCPA_LOG);
253 if (ce) {
254 lasa = cbmem_entry_start(ce);
255 *size = cbmem_entry_size(ce);
256 printk(BIOS_DEBUG, "TCPA log found at %p\n", lasa);
257 return lasa;
258 }
259 lasa = cbmem_add(CBMEM_ID_TCPA_LOG, tcpa_default_log_len);
260 if (!lasa) {
261 printk(BIOS_ERR, "TCPA log creation failed\n");
262 return NULL;
263 }
264
265 printk(BIOS_DEBUG, "TCPA log created at %p\n", lasa);
266 memset (lasa, 0, tcpa_default_log_len);
267
268 *size = tcpa_default_log_len;
269 return lasa;
270}
271
272static void acpi_create_tcpa(acpi_tcpa_t *tcpa)
273{
274 acpi_header_t *header = &(tcpa->header);
275 u32 tcpa_log_len;
276 void *lasa;
277
278 memset((void *)tcpa, 0, sizeof(acpi_tcpa_t));
279
280 lasa = get_tcpa_log(&tcpa_log_len);
281 if (!lasa) {
282 return;
283 }
284
285 /* Fill out header fields. */
286 memcpy(header->signature, "TCPA", 4);
287 memcpy(header->oem_id, OEM_ID, 6);
288 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
289 memcpy(header->asl_compiler_id, ASLC, 4);
290
291 header->length = sizeof(acpi_tcpa_t);
292 header->revision = 2;
293
294 tcpa->platform_class = 0;
295 tcpa->laml = tcpa_log_len;
Stefan Reinauerdefee172015-06-18 01:11:19 -0700296 tcpa->lasa = (uintptr_t) lasa;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200297
298 /* Calculate checksum. */
299 header->checksum = acpi_checksum((void *)tcpa, header->length);
300}
301
Myles Watson3fe6b702009-10-09 20:13:43 +0000302void acpi_create_ssdt_generator(acpi_header_t *ssdt, const char *oem_table_id)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000303{
Uwe Hermann622824c2010-11-19 15:14:42 +0000304 unsigned long current = (unsigned long)ssdt + sizeof(acpi_header_t);
305
Rudolf Marek293b5f52009-02-01 18:35:15 +0000306 memset((void *)ssdt, 0, sizeof(acpi_header_t));
Uwe Hermann622824c2010-11-19 15:14:42 +0000307
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000308 memcpy(&ssdt->signature, "SSDT", 4);
Uwe Hermann622824c2010-11-19 15:14:42 +0000309 ssdt->revision = 2; /* ACPI 1.0/2.0: ?, ACPI 3.0/4.0: 2 */
Rudolf Marek293b5f52009-02-01 18:35:15 +0000310 memcpy(&ssdt->oem_id, OEM_ID, 6);
311 memcpy(&ssdt->oem_table_id, oem_table_id, 8);
312 ssdt->oem_revision = 42;
Uwe Hermann622824c2010-11-19 15:14:42 +0000313 memcpy(&ssdt->asl_compiler_id, ASLC, 4);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000314 ssdt->asl_compiler_revision = 42;
315 ssdt->length = sizeof(acpi_header_t);
316
Stefan Reinauer8dcd50b2009-03-06 17:24:29 +0000317 acpigen_set_current((char *) current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200318 {
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100319 struct device *dev;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200320 for (dev = all_devices; dev; dev = dev->next)
321 if (dev->ops && dev->ops->acpi_fill_ssdt_generator) {
Alexander Couzens5eea4582015-04-12 22:18:55 +0200322 dev->ops->acpi_fill_ssdt_generator(dev);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200323 }
Vladimir Serbinenko0a669912014-10-05 14:34:17 +0200324 current = (unsigned long) acpigen_get_current();
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200325 }
Rudolf Marek293b5f52009-02-01 18:35:15 +0000326
Uwe Hermann622824c2010-11-19 15:14:42 +0000327 /* (Re)calculate length and checksum. */
Rudolf Marek293b5f52009-02-01 18:35:15 +0000328 ssdt->length = current - (unsigned long)ssdt;
329 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
330}
331
Stefan Reinauerf622d592005-11-26 16:56:05 +0000332int acpi_create_srat_lapic(acpi_srat_lapic_t *lapic, u8 node, u8 apic)
333{
Myles Watson0b4c9f02009-03-10 18:06:47 +0000334 memset((void *)lapic, 0, sizeof(acpi_srat_lapic_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000335
Uwe Hermann622824c2010-11-19 15:14:42 +0000336 lapic->type = 0; /* Processor local APIC/SAPIC affinity structure */
337 lapic->length = sizeof(acpi_srat_lapic_t);
338 lapic->flags = (1 << 0); /* Enabled (the use of this structure). */
339 lapic->proximity_domain_7_0 = node;
340 /* TODO: proximity_domain_31_8, local SAPIC EID, clock domain. */
341 lapic->apic_id = apic;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000342
Uwe Hermann622824c2010-11-19 15:14:42 +0000343 return lapic->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000344}
345
Uwe Hermann622824c2010-11-19 15:14:42 +0000346int 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 +0300347 u32 flags)
Stefan Reinauerf622d592005-11-26 16:56:05 +0000348{
Uwe Hermann622824c2010-11-19 15:14:42 +0000349 mem->type = 1; /* Memory affinity structure */
350 mem->length = sizeof(acpi_srat_mem_t);
351 mem->base_address_low = (basek << 10);
352 mem->base_address_high = (basek >> (32 - 10));
353 mem->length_low = (sizek << 10);
354 mem->length_high = (sizek >> (32 - 10));
355 mem->proximity_domain = node;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000356 mem->flags = flags;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000357
Uwe Hermann622824c2010-11-19 15:14:42 +0000358 return mem->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000359}
360
Uwe Hermann622824c2010-11-19 15:14:42 +0000361/* http://www.microsoft.com/whdc/system/sysinternals/sratdwn.mspx */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +0200362void acpi_create_srat(acpi_srat_t *srat,
363 unsigned long (*acpi_fill_srat)(unsigned long current))
Stefan Reinauerf622d592005-11-26 16:56:05 +0000364{
Uwe Hermann622824c2010-11-19 15:14:42 +0000365 acpi_header_t *header = &(srat->header);
366 unsigned long current = (unsigned long)srat + sizeof(acpi_srat_t);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000367
Uwe Hermann622824c2010-11-19 15:14:42 +0000368 memset((void *)srat, 0, sizeof(acpi_srat_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000369
Uwe Hermann622824c2010-11-19 15:14:42 +0000370 /* Fill out header fields. */
371 memcpy(header->signature, "SRAT", 4);
372 memcpy(header->oem_id, OEM_ID, 6);
373 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
374 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000375
Uwe Hermann622824c2010-11-19 15:14:42 +0000376 header->length = sizeof(acpi_srat_t);
377 header->revision = 1; /* ACPI 1.0: N/A, 2.0: 1, 3.0: 2, 4.0: 3 */
Stefan Reinauerf622d592005-11-26 16:56:05 +0000378
Uwe Hermann622824c2010-11-19 15:14:42 +0000379 srat->resv = 1; /* Spec: Reserved to 1 for backwards compatibility. */
Stefan Reinauerf622d592005-11-26 16:56:05 +0000380
Uwe Hermann622824c2010-11-19 15:14:42 +0000381 current = acpi_fill_srat(current);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000382
Uwe Hermann622824c2010-11-19 15:14:42 +0000383 /* (Re)calculate length and checksum. */
384 header->length = current - (unsigned long)srat;
385 header->checksum = acpi_checksum((void *)srat, header->length);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000386}
387
Nico Hubere561f352015-10-26 11:51:25 +0100388void acpi_create_dmar(acpi_dmar_t *dmar, enum dmar_flags flags,
Vladimir Serbinenko8d70e942014-11-09 13:22:27 +0100389 unsigned long (*acpi_fill_dmar) (unsigned long))
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200390{
391 acpi_header_t *header = &(dmar->header);
392 unsigned long current = (unsigned long)dmar + sizeof(acpi_dmar_t);
393
394 memset((void *)dmar, 0, sizeof(acpi_dmar_t));
395
396 /* Fill out header fields. */
397 memcpy(header->signature, "DMAR", 4);
398 memcpy(header->oem_id, OEM_ID, 6);
399 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
400 memcpy(header->asl_compiler_id, ASLC, 4);
401
402 header->length = sizeof(acpi_dmar_t);
403 header->revision = 1;
404
405 dmar->host_address_width = 40 - 1; /* FIXME: == MTRR size? */
Nico Hubere561f352015-10-26 11:51:25 +0100406 dmar->flags = flags;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200407
408 current = acpi_fill_dmar(current);
409
410 /* (Re)calculate length and checksum. */
411 header->length = current - (unsigned long)dmar;
412 header->checksum = acpi_checksum((void *)dmar, header->length);
413}
414
415unsigned long acpi_create_dmar_drhd(unsigned long current, u8 flags,
416 u16 segment, u32 bar)
417{
418 dmar_entry_t *drhd = (dmar_entry_t *)current;
419 memset(drhd, 0, sizeof(*drhd));
420 drhd->type = DMAR_DRHD;
421 drhd->length = sizeof(*drhd); /* will be fixed up later */
422 drhd->flags = flags;
423 drhd->segment = segment;
424 drhd->bar = bar;
425
426 return drhd->length;
427}
428
429void acpi_dmar_drhd_fixup(unsigned long base, unsigned long current)
430{
431 dmar_entry_t *drhd = (dmar_entry_t *)base;
432 drhd->length = current - base;
433}
434
Nico Huber6c4751d2015-10-26 12:03:54 +0100435static unsigned long acpi_create_dmar_drhd_ds(unsigned long current,
436 enum dev_scope_type type, u8 enumeration_id, u8 bus, u8 dev, u8 fn)
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200437{
Nico Huber6c4751d2015-10-26 12:03:54 +0100438 /* we don't support longer paths yet */
439 const size_t dev_scope_length = sizeof(dev_scope_t) + 2;
440
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200441 dev_scope_t *ds = (dev_scope_t *)current;
Nico Huber6c4751d2015-10-26 12:03:54 +0100442 memset(ds, 0, dev_scope_length);
443 ds->type = type;
444 ds->length = dev_scope_length;
445 ds->enumeration = enumeration_id;
446 ds->start_bus = bus;
447 ds->path[0].dev = dev;
448 ds->path[0].fn = fn;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200449
450 return ds->length;
451}
452
Nico Huber6c4751d2015-10-26 12:03:54 +0100453unsigned long acpi_create_dmar_drhd_ds_pci(unsigned long current, u8 bus,
454 u8 dev, u8 fn)
455{
456 return acpi_create_dmar_drhd_ds(current,
457 SCOPE_PCI_ENDPOINT, 0, bus, dev, fn);
458}
459
460unsigned long acpi_create_dmar_drhd_ds_ioapic(unsigned long current,
461 u8 enumeration_id, u8 bus, u8 dev, u8 fn)
462{
463 return acpi_create_dmar_drhd_ds(current,
464 SCOPE_IOAPIC, enumeration_id, bus, dev, fn);
465}
466
467unsigned long acpi_create_dmar_drhd_ds_msi_hpet(unsigned long current,
468 u8 enumeration_id, u8 bus, u8 dev, u8 fn)
469{
470 return acpi_create_dmar_drhd_ds(current,
471 SCOPE_MSI_HPET, enumeration_id, bus, dev, fn);
472}
473
Uwe Hermann622824c2010-11-19 15:14:42 +0000474/* http://h21007.www2.hp.com/portal/download/files/unprot/Itanium/slit.pdf */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +0200475void acpi_create_slit(acpi_slit_t *slit,
476 unsigned long (*acpi_fill_slit)(unsigned long current))
Yinghai Lud4b278c2006-10-04 20:46:15 +0000477{
Uwe Hermann622824c2010-11-19 15:14:42 +0000478 acpi_header_t *header = &(slit->header);
479 unsigned long current = (unsigned long)slit + sizeof(acpi_slit_t);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000480
Uwe Hermann622824c2010-11-19 15:14:42 +0000481 memset((void *)slit, 0, sizeof(acpi_slit_t));
Yinghai Lud4b278c2006-10-04 20:46:15 +0000482
Uwe Hermann622824c2010-11-19 15:14:42 +0000483 /* Fill out header fields. */
484 memcpy(header->signature, "SLIT", 4);
485 memcpy(header->oem_id, OEM_ID, 6);
486 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
487 memcpy(header->asl_compiler_id, ASLC, 4);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000488
Uwe Hermann622824c2010-11-19 15:14:42 +0000489 header->length = sizeof(acpi_slit_t);
490 header->revision = 1; /* ACPI 1.0: N/A, ACPI 2.0/3.0/4.0: 1 */
Yinghai Lud4b278c2006-10-04 20:46:15 +0000491
Uwe Hermann622824c2010-11-19 15:14:42 +0000492 current = acpi_fill_slit(current);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000493
Uwe Hermann622824c2010-11-19 15:14:42 +0000494 /* (Re)calculate length and checksum. */
495 header->length = current - (unsigned long)slit;
496 header->checksum = acpi_checksum((void *)slit, header->length);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000497}
498
Uwe Hermann622824c2010-11-19 15:14:42 +0000499/* http://www.intel.com/hardwaredesign/hpetspec_1.pdf */
Stefan Reinauer777224c2005-01-19 14:06:41 +0000500void acpi_create_hpet(acpi_hpet_t *hpet)
Stefan Reinauer688b3852004-01-28 16:56:14 +0000501{
Uwe Hermann622824c2010-11-19 15:14:42 +0000502 acpi_header_t *header = &(hpet->header);
503 acpi_addr_t *addr = &(hpet->addr);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000504
Stefan Reinauera7648c22004-01-29 17:31:34 +0000505 memset((void *)hpet, 0, sizeof(acpi_hpet_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000506
Uwe Hermann622824c2010-11-19 15:14:42 +0000507 /* Fill out header fields. */
Stefan Reinauer4704dc52009-07-22 01:11:37 +0000508 memcpy(header->signature, "HPET", 4);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000509 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000510 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000511 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000512
Stefan Reinauer688b3852004-01-28 16:56:14 +0000513 header->length = sizeof(acpi_hpet_t);
Uwe Hermann622824c2010-11-19 15:14:42 +0000514 header->revision = 1; /* Currently 1. Table added in ACPI 2.0. */
Stefan Reinauer688b3852004-01-28 16:56:14 +0000515
Uwe Hermann622824c2010-11-19 15:14:42 +0000516 /* Fill out HPET address. */
517 addr->space_id = 0; /* Memory */
518 addr->bit_width = 64;
519 addr->bit_offset = 0;
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200520 addr->addrl = CONFIG_HPET_ADDRESS & 0xffffffff;
521 addr->addrh = ((unsigned long long)CONFIG_HPET_ADDRESS) >> 32;
Stefan Reinauer688b3852004-01-28 16:56:14 +0000522
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200523 hpet->id = *(unsigned int*)CONFIG_HPET_ADDRESS;
Uwe Hermann622824c2010-11-19 15:14:42 +0000524 hpet->number = 0;
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200525 hpet->min_tick = CONFIG_HPET_MIN_TICKS;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000526
Uwe Hermann622824c2010-11-19 15:14:42 +0000527 header->checksum = acpi_checksum((void *)hpet, sizeof(acpi_hpet_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000528}
Uwe Hermann622824c2010-11-19 15:14:42 +0000529
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500530void acpi_create_ivrs(acpi_ivrs_t *ivrs,
531 unsigned long (*acpi_fill_ivrs)(acpi_ivrs_t* ivrs_struct, unsigned long current))
532{
533 acpi_header_t *header = &(ivrs->header);
534 unsigned long current = (unsigned long)ivrs + sizeof(acpi_ivrs_t);
535
536 memset((void *)ivrs, 0, sizeof(acpi_ivrs_t));
537
538 /* Fill out header fields. */
539 memcpy(header->signature, "IVRS", 4);
540 memcpy(header->oem_id, OEM_ID, 6);
541 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
542 memcpy(header->asl_compiler_id, ASLC, 4);
543
544 header->length = sizeof(acpi_ivrs_t);
545 header->revision = 1; /* ACPI 1.0: N/A, ACPI 2.0/3.0/4.0: 1 */
546
547 current = acpi_fill_ivrs(ivrs, current);
548
549 /* (Re)calculate length and checksum. */
550 header->length = current - (unsigned long)ivrs;
551 header->checksum = acpi_checksum((void *)ivrs, header->length);
552}
553
Alexander Couzens83fc32f2015-04-12 22:28:37 +0200554unsigned long acpi_write_hpet(device_t device, unsigned long current, acpi_rsdp_t *rsdp)
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200555{
556 acpi_hpet_t *hpet;
557
558 /*
559 * We explicitly add these tables later on:
560 */
561 printk(BIOS_DEBUG, "ACPI: * HPET\n");
562
563 hpet = (acpi_hpet_t *) current;
564 current += sizeof(acpi_hpet_t);
565 current = ALIGN(current, 16);
566 acpi_create_hpet(hpet);
567 acpi_add_table(rsdp, hpet);
568
569 return current;
570}
571
Stefan Reinauer777224c2005-01-19 14:06:41 +0000572void acpi_create_facs(acpi_facs_t *facs)
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000573{
Uwe Hermann622824c2010-11-19 15:14:42 +0000574 memset((void *)facs, 0, sizeof(acpi_facs_t));
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000575
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000576 memcpy(facs->signature, "FACS", 4);
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000577 facs->length = sizeof(acpi_facs_t);
578 facs->hardware_signature = 0;
579 facs->firmware_waking_vector = 0;
580 facs->global_lock = 0;
581 facs->flags = 0;
582 facs->x_firmware_waking_vector_l = 0;
583 facs->x_firmware_waking_vector_h = 0;
Uwe Hermann622824c2010-11-19 15:14:42 +0000584 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 +0000585}
Stefan Reinauer777224c2005-01-19 14:06:41 +0000586
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100587static void acpi_write_rsdt(acpi_rsdt_t *rsdt, char *oem_id, char *oem_table_id)
Stefan Reinauer14e22772010-04-27 06:56:47 +0000588{
Uwe Hermann622824c2010-11-19 15:14:42 +0000589 acpi_header_t *header = &(rsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000590
Uwe Hermann622824c2010-11-19 15:14:42 +0000591 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000592 memcpy(header->signature, "RSDT", 4);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100593 memcpy(header->oem_id, oem_id, 6);
594 memcpy(header->oem_table_id, oem_table_id, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000595 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000596
Stefan Reinauer688b3852004-01-28 16:56:14 +0000597 header->length = sizeof(acpi_rsdt_t);
Uwe Hermann622824c2010-11-19 15:14:42 +0000598 header->revision = 1; /* ACPI 1.0/2.0/3.0/4.0: 1 */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000599
Uwe Hermann622824c2010-11-19 15:14:42 +0000600 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauer688b3852004-01-28 16:56:14 +0000601
Uwe Hermann622824c2010-11-19 15:14:42 +0000602 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000603 header->checksum = acpi_checksum((void *)rsdt, sizeof(acpi_rsdt_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000604}
605
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100606static void acpi_write_xsdt(acpi_xsdt_t *xsdt, char *oem_id, char *oem_table_id)
Stefan Reinauer14e22772010-04-27 06:56:47 +0000607{
Uwe Hermann622824c2010-11-19 15:14:42 +0000608 acpi_header_t *header = &(xsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000609
Uwe Hermann622824c2010-11-19 15:14:42 +0000610 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000611 memcpy(header->signature, "XSDT", 4);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100612 memcpy(header->oem_id, oem_id, 6);
613 memcpy(header->oem_table_id, oem_table_id, 8);
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000614 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000615
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000616 header->length = sizeof(acpi_xsdt_t);
Uwe Hermann622824c2010-11-19 15:14:42 +0000617 header->revision = 1; /* ACPI 1.0: N/A, 2.0/3.0/4.0: 1 */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000618
Uwe Hermann622824c2010-11-19 15:14:42 +0000619 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000620
Uwe Hermann622824c2010-11-19 15:14:42 +0000621 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000622 header->checksum = acpi_checksum((void *)xsdt, sizeof(acpi_xsdt_t));
623}
624
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100625static void acpi_write_rsdp(acpi_rsdp_t *rsdp, acpi_rsdt_t *rsdt,
626 acpi_xsdt_t *xsdt, char *oem_id)
Stefan Reinauer688b3852004-01-28 16:56:14 +0000627{
Stefan Reinauerd18faac2009-11-05 18:06:43 +0000628 memset(rsdp, 0, sizeof(acpi_rsdp_t));
Uwe Hermann622824c2010-11-19 15:14:42 +0000629
Stefan Reinauer688b3852004-01-28 16:56:14 +0000630 memcpy(rsdp->signature, RSDP_SIG, 8);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100631 memcpy(rsdp->oem_id, oem_id, 6);
Uwe Hermann622824c2010-11-19 15:14:42 +0000632
633 rsdp->length = sizeof(acpi_rsdp_t);
Stefan Reinauerdefee172015-06-18 01:11:19 -0700634 rsdp->rsdt_address = (uintptr_t)rsdt;
Uwe Hermann622824c2010-11-19 15:14:42 +0000635
636 /*
637 * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2.
638 *
639 * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
640 * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
641 * revision 0).
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000642 */
643 if (xsdt == NULL) {
Uwe Hermann622824c2010-11-19 15:14:42 +0000644 rsdp->revision = 0;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000645 } else {
Stefan Reinauerdefee172015-06-18 01:11:19 -0700646 rsdp->xsdt_address = (u64)(uintptr_t)xsdt;
Uwe Hermann622824c2010-11-19 15:14:42 +0000647 rsdp->revision = 2;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000648 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000649
650 /* Calculate checksums. */
651 rsdp->checksum = acpi_checksum((void *)rsdp, 20);
652 rsdp->ext_checksum = acpi_checksum((void *)rsdp, sizeof(acpi_rsdp_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000653}
654
zbaocaf494c82012-04-13 13:57:14 +0800655unsigned long acpi_create_hest_error_source(acpi_hest_t *hest, acpi_hest_esd_t *esd, u16 type, void *data, u16 data_len)
656{
657 acpi_header_t *header = &(hest->header);
658 acpi_hest_hen_t *hen;
659 void *pos;
660 u16 len;
661
662 pos = esd;
663 memset(pos, 0, sizeof(acpi_hest_esd_t));
664 len = 0;
665 esd->type = type; /* MCE */
666 esd->source_id = hest->error_source_count;
667 esd->flags = 0; /* FIRMWARE_FIRST */
668 esd->enabled = 1;
669 esd->prealloc_erecords = 1;
670 esd->max_section_per_record = 0x1;
671
672 len += sizeof(acpi_hest_esd_t);
673 pos = esd + 1;
674
675 switch (type) {
676 case 0: /* MCE */
677 break;
678 case 1: /* CMC */
679 hen = (acpi_hest_hen_t *) (pos);
680 memset(pos, 0, sizeof(acpi_hest_hen_t));
681 hen->type = 3; /* SCI? */
682 hen->length = sizeof(acpi_hest_hen_t);
683 hen->conf_we = 0; /* Configuration Write Enable. */
684 hen->poll_interval = 0;
685 hen->vector = 0;
686 hen->sw2poll_threshold_val = 0;
687 hen->sw2poll_threshold_win = 0;
688 hen->error_threshold_val = 0;
689 hen->error_threshold_win = 0;
690 len += sizeof(acpi_hest_hen_t);
691 pos = hen + 1;
692 break;
693 case 2: /* NMI */
694 case 6: /* AER Root Port */
695 case 7: /* AER Endpoint */
696 case 8: /* AER Bridge */
697 case 9: /* Generic Hardware Error Source. */
698 /* TODO: */
699 break;
700 default:
701 printk(BIOS_DEBUG, "Invalid type of Error Source.");
702 break;
703 }
704 hest->error_source_count ++;
705
706 memcpy(pos, data, data_len);
707 len += data_len;
708 header->length += len;
709
710 return len;
711}
712
713/* ACPI 4.0 */
Vladimir Serbinenko807127f2014-11-09 13:36:18 +0100714void acpi_write_hest(acpi_hest_t *hest,
715 unsigned long (*acpi_fill_hest)(acpi_hest_t *hest))
zbaocaf494c82012-04-13 13:57:14 +0800716{
717 acpi_header_t *header = &(hest->header);
718
719 memset(hest, 0, sizeof(acpi_hest_t));
720
721 memcpy(header->signature, "HEST", 4);
722 memcpy(header->oem_id, OEM_ID, 6);
723 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
724 memcpy(header->asl_compiler_id, ASLC, 4);
725 header->length += sizeof(acpi_hest_t);
726 header->revision = 1;
727
728 acpi_fill_hest(hest);
729
730 /* Calculate checksums. */
731 header->checksum = acpi_checksum((void *)hest, header->length);
732}
733
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +0200734#if IS_ENABLED(CONFIG_COMMON_FADT)
735void acpi_create_fadt(acpi_fadt_t *fadt,acpi_facs_t *facs, void *dsdt)
736{
737 acpi_header_t *header = &(fadt->header);
738
739 memset((void *) fadt, 0, sizeof(acpi_fadt_t));
740 memcpy(header->signature, "FACP", 4);
741 header->length = sizeof(acpi_fadt_t);
Vladimir Serbinenkoab83ef02014-10-25 15:18:25 +0200742 header->revision = 4;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +0200743 memcpy(header->oem_id, OEM_ID, 6);
744 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
745 memcpy(header->asl_compiler_id, ASLC, 4);
746 header->asl_compiler_revision = 0;
747
748 fadt->firmware_ctrl = (unsigned long) facs;
749 fadt->dsdt = (unsigned long) dsdt;
750
751 fadt->x_firmware_ctl_l = (unsigned long)facs;
752 fadt->x_firmware_ctl_h = 0;
753 fadt->x_dsdt_l = (unsigned long)dsdt;
754 fadt->x_dsdt_h = 0;
755
756 if(IS_ENABLED(CONFIG_SYSTEM_TYPE_LAPTOP)) {
757 fadt->preferred_pm_profile = PM_MOBILE;
758 } else {
759 fadt->preferred_pm_profile = PM_DESKTOP;
760 }
761
Vladimir Serbinenkoab83ef02014-10-25 15:18:25 +0200762 acpi_fill_fadt(fadt);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +0200763
764 header->checksum =
765 acpi_checksum((void *) fadt, header->length);
766}
767#endif
768
Vladimir Serbinenko41877d82014-09-01 22:18:01 +0200769unsigned long __attribute__ ((weak)) fw_cfg_acpi_tables(unsigned long start)
770{
771 return 0;
772}
773
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200774unsigned long write_acpi_tables(unsigned long start)
775{
776 unsigned long current;
777 acpi_rsdp_t *rsdp;
778 acpi_rsdt_t *rsdt;
779 acpi_xsdt_t *xsdt;
780 acpi_fadt_t *fadt;
781 acpi_facs_t *facs;
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100782 acpi_header_t *slic_file, *slic;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200783 acpi_header_t *ssdt;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +0200784 acpi_header_t *dsdt_file, *dsdt;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200785 acpi_mcfg_t *mcfg;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200786 acpi_tcpa_t *tcpa;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200787 acpi_madt_t *madt;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100788 struct device *dev;
Vladimir Serbinenko41877d82014-09-01 22:18:01 +0200789 unsigned long fw;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +0200790 size_t slic_size, dsdt_size;
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100791 char oem_id[6], oem_table_id[8];
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200792
793 current = start;
794
795 /* Align ACPI tables to 16byte */
Aaron Durbin07a1b282015-12-10 17:07:38 -0600796 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200797
Vladimir Serbinenko41877d82014-09-01 22:18:01 +0200798 fw = fw_cfg_acpi_tables(current);
799 if (fw)
800 return fw;
801
Patrick Georgic32a52c2015-06-22 21:10:34 +0200802#if CONFIG_COMPILE_IN_DSDT
803 extern char _binary_dsdt_aml_start;
804 extern char _binary_dsdt_aml_end;
805 dsdt_file = (acpi_header_t *)&_binary_dsdt_aml_start;
806 dsdt_size = (size_t)(&_binary_dsdt_aml_end - &_binary_dsdt_aml_start);
807#else
Vladimir Serbinenkoa4cf83d2015-06-02 21:40:29 +0200808 dsdt_file = cbfs_boot_map_with_leak(
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +0200809 CONFIG_CBFS_PREFIX "/dsdt.aml",
810 CBFS_TYPE_RAW, &dsdt_size);
Patrick Georgic32a52c2015-06-22 21:10:34 +0200811#endif
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +0200812 if (!dsdt_file) {
813 printk(BIOS_ERR, "No DSDT file, skipping ACPI tables\n");
814 return current;
815 }
816
817 if (dsdt_file->length > dsdt_size
818 || dsdt_file->length < sizeof (acpi_header_t)
819 || memcmp(dsdt_file->signature, "DSDT", 4) != 0) {
820 printk(BIOS_ERR, "Invalid DSDT file, skipping ACPI tables\n");
821 return current;
822 }
823
Aaron Durbin899d13d2015-05-15 23:39:23 -0500824 slic_file = cbfs_boot_map_with_leak(CONFIG_CBFS_PREFIX "/slic",
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100825 CBFS_TYPE_RAW, &slic_size);
Vladimir Serbinenko2cb29782015-05-31 11:32:09 +0200826 if (slic_file
827 && (slic_file->length > slic_size
828 || slic_file->length < sizeof (acpi_header_t)
829 || memcmp(slic_file->signature, "SLIC", 4) != 0)) {
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100830 slic_file = 0;
831 }
832
833 if (slic_file) {
834 memcpy(oem_id, slic_file->oem_id, 6);
835 memcpy(oem_table_id, slic_file->oem_table_id, 8);
836 } else {
837 memcpy(oem_id, OEM_ID, 6);
838 memcpy(oem_table_id, ACPI_TABLE_CREATOR, 8);
839 }
840
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200841 printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx.\n", start);
842
843 /* We need at least an RSDP and an RSDT Table */
844 rsdp = (acpi_rsdp_t *) current;
845 current += sizeof(acpi_rsdp_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -0600846 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200847 rsdt = (acpi_rsdt_t *) current;
848 current += sizeof(acpi_rsdt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -0600849 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200850 xsdt = (acpi_xsdt_t *) current;
851 current += sizeof(acpi_xsdt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -0600852 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200853
854 /* clear all table memory */
855 memset((void *) start, 0, current - start);
856
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100857 acpi_write_rsdp(rsdp, rsdt, xsdt, oem_id);
858 acpi_write_rsdt(rsdt, oem_id, oem_table_id);
859 acpi_write_xsdt(xsdt, oem_id, oem_table_id);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200860
861 printk(BIOS_DEBUG, "ACPI: * FACS\n");
Patrick Georgi133108a2015-08-08 23:20:58 +0200862 current = (ALIGN(current, 64));
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200863 facs = (acpi_facs_t *) current;
864 current += sizeof(acpi_facs_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -0600865 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200866 acpi_create_facs(facs);
867
868 printk(BIOS_DEBUG, "ACPI: * DSDT\n");
869 dsdt = (acpi_header_t *) current;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +0200870 memcpy(dsdt, dsdt_file, sizeof(acpi_header_t));
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +0200871 if (dsdt->length >= sizeof(acpi_header_t)) {
872 current += sizeof(acpi_header_t);
873
874 acpigen_set_current((char *) current);
875 for (dev = all_devices; dev; dev = dev->next)
876 if (dev->ops && dev->ops->acpi_inject_dsdt_generator) {
Alexander Couzensa90dad12015-04-12 21:49:46 +0200877 dev->ops->acpi_inject_dsdt_generator(dev);
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +0200878 }
879 current = (unsigned long) acpigen_get_current();
880 memcpy((char *)current,
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +0200881 (char *)dsdt_file + sizeof(acpi_header_t),
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +0200882 dsdt->length - sizeof(acpi_header_t));
883 current += dsdt->length - sizeof(acpi_header_t);
884
885 /* (Re)calculate length and checksum. */
886 dsdt->length = current - (unsigned long)dsdt;
887 dsdt->checksum = 0;
888 dsdt->checksum = acpi_checksum((void *)dsdt, dsdt->length);
889 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200890
Aaron Durbin07a1b282015-12-10 17:07:38 -0600891 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200892
893 printk(BIOS_DEBUG, "ACPI: * FADT\n");
894 fadt = (acpi_fadt_t *) current;
895 current += sizeof(acpi_fadt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -0600896 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200897
898 acpi_create_fadt(fadt, facs, dsdt);
899 acpi_add_table(rsdp, fadt);
900
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100901 if (slic_file) {
902 printk(BIOS_DEBUG, "ACPI: * SLIC\n");
903 slic = (acpi_header_t *)current;
904 memcpy(slic, slic_file, slic_file->length);
905 current += slic_file->length;
Aaron Durbin07a1b282015-12-10 17:07:38 -0600906 current = acpi_align_current(current);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100907 acpi_add_table(rsdp, slic);
908 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200909
910 printk(BIOS_DEBUG, "ACPI: * SSDT\n");
911 ssdt = (acpi_header_t *)current;
912 acpi_create_ssdt_generator(ssdt, ACPI_TABLE_CREATOR);
Vladimir Serbinenko9310df82014-10-10 20:40:41 +0200913 if (ssdt->length > sizeof(acpi_header_t)) {
914 current += ssdt->length;
915 acpi_add_table(rsdp, ssdt);
Aaron Durbin07a1b282015-12-10 17:07:38 -0600916 current = acpi_align_current(current);
Vladimir Serbinenko9310df82014-10-10 20:40:41 +0200917 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200918
919 printk(BIOS_DEBUG, "ACPI: * MCFG\n");
920 mcfg = (acpi_mcfg_t *) current;
921 acpi_create_mcfg(mcfg);
922 if (mcfg->header.length > sizeof(acpi_mcfg_t)) {
923 current += mcfg->header.length;
Aaron Durbin07a1b282015-12-10 17:07:38 -0600924 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200925 acpi_add_table(rsdp, mcfg);
926 }
927
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200928 printk(BIOS_DEBUG, "ACPI: * TCPA\n");
929 tcpa = (acpi_tcpa_t *) current;
930 acpi_create_tcpa(tcpa);
931 if (tcpa->header.length >= sizeof(acpi_tcpa_t)) {
932 current += tcpa->header.length;
Aaron Durbin07a1b282015-12-10 17:07:38 -0600933 current = acpi_align_current(current);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200934 acpi_add_table(rsdp, tcpa);
935 }
936
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200937 printk(BIOS_DEBUG, "ACPI: * MADT\n");
938
939 madt = (acpi_madt_t *) current;
940 acpi_create_madt(madt);
941 if (madt->header.length > sizeof(acpi_madt_t)) {
942 current+=madt->header.length;
943 acpi_add_table(rsdp,madt);
944 }
Aaron Durbin07a1b282015-12-10 17:07:38 -0600945 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200946
947 printk(BIOS_DEBUG, "current = %lx\n", current);
948
949 for (dev = all_devices; dev; dev = dev->next) {
950 if (dev->ops && dev->ops->write_acpi_tables) {
Alexander Couzens83fc32f2015-04-12 22:28:37 +0200951 current = dev->ops->write_acpi_tables(dev, current, rsdp);
Aaron Durbin07a1b282015-12-10 17:07:38 -0600952 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200953 }
954 }
955
956 printk(BIOS_INFO, "ACPI: done.\n");
957 return current;
958}
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200959
Patrick Georgie1667822012-05-05 15:29:32 +0200960#if CONFIG_HAVE_ACPI_RESUME
Kyösti Mälkki134f5042014-12-26 13:29:09 +0200961void __attribute__((weak)) mainboard_suspend_resume(void)
962{
963}
964
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500965void acpi_resume(void *wake_vec)
Stefan Reinauer7e9771c2009-04-22 08:17:38 +0000966{
Duncan Laurie11290c42012-10-03 19:07:05 -0700967#if CONFIG_HAVE_SMI_HANDLER
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500968 u32 *gnvs_address = cbmem_find(CBMEM_ID_ACPI_GNVS_PTR);
Duncan Laurie11290c42012-10-03 19:07:05 -0700969
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500970 /* Restore GNVS pointer in SMM if found */
971 if (gnvs_address && *gnvs_address) {
972 printk(BIOS_DEBUG, "Restore GNVS pointer to 0x%08x\n",
973 *gnvs_address);
974 smm_setup_structures((void *)*gnvs_address, NULL, NULL);
Stefan Reinauercb91e152012-04-03 23:28:22 +0200975 }
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500976#endif
977
978 /* Call mainboard resume handler first, if defined. */
Kyösti Mälkki134f5042014-12-26 13:29:09 +0200979 mainboard_suspend_resume();
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500980
981 post_code(POST_OS_RESUME);
982 acpi_jump_to_wakeup(wake_vec);
Stefan Reinauer7e9771c2009-04-22 08:17:38 +0000983}
984
Kyösti Mälkki7a846e72015-01-09 23:55:09 +0200985/* This is filled with acpi_is_wakeup() call early in ramstage. */
986int acpi_slp_type = -1;
Stefan Reinauer688b3852004-01-28 16:56:14 +0000987
Kyösti Mälkki9e94dbf2015-01-08 20:03:18 +0200988#if IS_ENABLED(CONFIG_EARLY_CBMEM_INIT)
989int acpi_get_sleep_type(void)
Kyösti Mälkki134f5042014-12-26 13:29:09 +0200990{
Kyösti Mälkki9e94dbf2015-01-08 20:03:18 +0200991 struct romstage_handoff *handoff;
992
993 handoff = cbmem_find(CBMEM_ID_ROMSTAGE_INFO);
994
995 if (handoff == NULL) {
996 printk(BIOS_DEBUG, "Unknown boot method, assuming normal.\n");
997 return 0;
998 } else if (handoff->s3_resume) {
999 printk(BIOS_DEBUG, "S3 Resume.\n");
1000 return 3;
1001 } else {
1002 printk(BIOS_DEBUG, "Normal boot.\n");
1003 return 0;
1004 }
Kyösti Mälkki134f5042014-12-26 13:29:09 +02001005}
Kyösti Mälkki9e94dbf2015-01-08 20:03:18 +02001006#endif
Kyösti Mälkki134f5042014-12-26 13:29:09 +02001007
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +03001008static void acpi_handoff_wakeup(void)
1009{
Kyösti Mälkki7a846e72015-01-09 23:55:09 +02001010 if (acpi_slp_type < 0)
1011 acpi_slp_type = acpi_get_sleep_type();
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +03001012}
1013
Kyösti Mälkkicb28f3f2014-01-03 15:15:22 +02001014int acpi_is_wakeup(void)
Rudolf Marek33cafe52009-04-13 18:07:02 +00001015{
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +03001016 acpi_handoff_wakeup();
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +01001017 /* Both resume from S2 and resume from S3 restart at CPU reset */
1018 return (acpi_slp_type == 3 || acpi_slp_type == 2);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001019}
1020
Kyösti Mälkki4d9b7722014-06-19 19:45:40 +03001021int acpi_is_wakeup_s3(void)
1022{
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +03001023 acpi_handoff_wakeup();
Kyösti Mälkki4d9b7722014-06-19 19:45:40 +03001024 return (acpi_slp_type == 3);
1025}
1026
zbao1897c2c2015-11-05 20:25:59 +08001027int acpi_is_wakeup_s4(void)
1028{
1029 acpi_handoff_wakeup();
1030 return (acpi_slp_type == 4);
1031}
1032
Kyösti Mälkkicb28f3f2014-01-03 15:15:22 +02001033void acpi_fail_wakeup(void)
1034{
1035 if (acpi_slp_type == 3 || acpi_slp_type == 2)
1036 acpi_slp_type = 0;
1037}
1038
Kyösti Mälkki7b14f082014-10-16 20:58:47 +03001039void acpi_prepare_resume_backup(void)
1040{
1041 if (!acpi_s3_resume_allowed())
1042 return;
1043
1044 /* Let's prepare the ACPI S3 Resume area now already, so we can rely on
1045 * it being there during reboot time. We don't need the pointer, nor
1046 * the result right now. If it fails, ACPI resume will be disabled.
1047 */
1048
1049 if (HIGH_MEMORY_SAVE)
1050 cbmem_add(CBMEM_ID_RESUME, HIGH_MEMORY_SAVE);
Kyösti Mälkki7b14f082014-10-16 20:58:47 +03001051}
1052
Rudolf Marek33cafe52009-04-13 18:07:02 +00001053static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp)
1054{
1055 if (strncmp((char *)rsdp, RSDP_SIG, sizeof(RSDP_SIG) - 1) != 0)
1056 return NULL;
1057
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001058 printk(BIOS_DEBUG, "Looking on %p for valid checksum\n", rsdp);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001059
1060 if (acpi_checksum((void *)rsdp, 20) != 0)
1061 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001062 printk(BIOS_DEBUG, "Checksum 1 passed\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001063
1064 if ((rsdp->revision > 1) && (acpi_checksum((void *)rsdp,
1065 rsdp->length) != 0))
1066 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001067 printk(BIOS_DEBUG, "Checksum 2 passed all OK\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001068
1069 return rsdp;
1070}
1071
1072static acpi_rsdp_t *rsdp;
1073
1074void *acpi_get_wakeup_rsdp(void)
1075{
1076 return rsdp;
1077}
1078
1079void *acpi_find_wakeup_vector(void)
1080{
1081 char *p, *end;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001082 acpi_rsdt_t *rsdt;
1083 acpi_facs_t *facs;
Martin Rothc5f49262012-12-14 19:17:55 -07001084 acpi_fadt_t *fadt = NULL;
Uwe Hermann622824c2010-11-19 15:14:42 +00001085 void *wake_vec;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001086 int i;
1087
1088 rsdp = NULL;
1089
1090 if (!acpi_is_wakeup())
1091 return NULL;
1092
Uwe Hermann622824c2010-11-19 15:14:42 +00001093 printk(BIOS_DEBUG, "Trying to find the wakeup vector...\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001094
Uwe Hermann622824c2010-11-19 15:14:42 +00001095 /* Find RSDP. */
1096 for (p = (char *)0xe0000; p < (char *)0xfffff; p += 16) {
1097 if ((rsdp = valid_rsdp((acpi_rsdp_t *)p)))
Rudolf Marek33cafe52009-04-13 18:07:02 +00001098 break;
1099 }
1100
1101 if (rsdp == NULL)
1102 return NULL;
1103
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001104 printk(BIOS_DEBUG, "RSDP found at %p\n", rsdp);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001105 rsdt = (acpi_rsdt_t *)(uintptr_t)rsdp->rsdt_address;
Stefan Reinauer14e22772010-04-27 06:56:47 +00001106
Uwe Hermann622824c2010-11-19 15:14:42 +00001107 end = (char *)rsdt + rsdt->header.length;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001108 printk(BIOS_DEBUG, "RSDT found at %p ends at %p\n", rsdt, end);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001109
Uwe Hermann622824c2010-11-19 15:14:42 +00001110 for (i = 0; ((char *)&rsdt->entry[i]) < end; i++) {
Stefan Reinauer71a30182015-07-30 16:28:13 -07001111 fadt = (acpi_fadt_t *)(uintptr_t)rsdt->entry[i];
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001112 if (strncmp((char *)fadt, "FACP", 4) == 0)
Rudolf Marek33cafe52009-04-13 18:07:02 +00001113 break;
1114 fadt = NULL;
1115 }
1116
1117 if (fadt == NULL)
1118 return NULL;
1119
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001120 printk(BIOS_DEBUG, "FADT found at %p\n", fadt);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001121 facs = (acpi_facs_t *)(uintptr_t)fadt->firmware_ctrl;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001122
Stefan Reinauer7e9771c2009-04-22 08:17:38 +00001123 if (facs == NULL) {
Uwe Hermann622824c2010-11-19 15:14:42 +00001124 printk(BIOS_DEBUG, "No FACS found, wake up from S3 not "
1125 "possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001126 return NULL;
Stefan Reinauer7e9771c2009-04-22 08:17:38 +00001127 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001128
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001129 printk(BIOS_DEBUG, "FACS found at %p\n", facs);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001130 wake_vec = (void *)(uintptr_t)facs->firmware_waking_vector;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001131 printk(BIOS_DEBUG, "OS waking vector is %p\n", wake_vec);
Uwe Hermann622824c2010-11-19 15:14:42 +00001132
Rudolf Marek33cafe52009-04-13 18:07:02 +00001133 return wake_vec;
1134}
1135
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +01001136#if CONFIG_SMP
Rudolf Marek33cafe52009-04-13 18:07:02 +00001137extern char *lowmem_backup;
1138extern char *lowmem_backup_ptr;
1139extern int lowmem_backup_size;
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +01001140#endif
Rudolf Marek33cafe52009-04-13 18:07:02 +00001141
Uwe Hermann622824c2010-11-19 15:14:42 +00001142#define WAKEUP_BASE 0x600
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001143
Stefan Reinauer71a30182015-07-30 16:28:13 -07001144void (*acpi_do_wakeup)(uintptr_t vector, u32 backup_source, u32 backup_target,
Stefan Reinauer399486e2012-12-06 13:54:29 -08001145 u32 backup_size) asmlinkage = (void *)WAKEUP_BASE;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001146
Aaron Durbina146d582013-02-08 16:56:51 -06001147extern unsigned char __wakeup;
1148extern unsigned int __wakeup_size;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001149
Rudolf Marek33cafe52009-04-13 18:07:02 +00001150void acpi_jump_to_wakeup(void *vector)
1151{
Stefan Reinauer71a30182015-07-30 16:28:13 -07001152 uintptr_t acpi_backup_memory = 0;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001153
Kyösti Mälkki2ca2afe2014-06-17 15:41:37 +03001154 if (HIGH_MEMORY_SAVE && acpi_s3_resume_allowed()) {
Stefan Reinauer71a30182015-07-30 16:28:13 -07001155 acpi_backup_memory = (uintptr_t)cbmem_find(CBMEM_ID_RESUME);
Kyösti Mälkki2ca2afe2014-06-17 15:41:37 +03001156
1157 if (!acpi_backup_memory) {
1158 printk(BIOS_WARNING, "ACPI: Backup memory missing. "
1159 "No S3 resume.\n");
1160 return;
1161 }
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001162 }
1163
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +01001164#if CONFIG_SMP
Martin Roth7b5f8ef2013-07-08 16:22:10 -06001165 // FIXME: This should go into the ACPI backup memory, too. No pork sausages.
Uwe Hermann622824c2010-11-19 15:14:42 +00001166 /*
1167 * Just restore the SMP trampoline and continue with wakeup on
1168 * assembly level.
1169 */
Rudolf Marek33cafe52009-04-13 18:07:02 +00001170 memcpy(lowmem_backup_ptr, lowmem_backup, lowmem_backup_size);
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +01001171#endif
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001172
Uwe Hermann622824c2010-11-19 15:14:42 +00001173 /* Copy wakeup trampoline in place. */
Aaron Durbina146d582013-02-08 16:56:51 -06001174 memcpy((void *)WAKEUP_BASE, &__wakeup, __wakeup_size);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001175
Duncan Lauriecde78012011-10-19 15:32:39 -07001176 timestamp_add_now(TS_ACPI_WAKE_JUMP);
Duncan Lauriecde78012011-10-19 15:32:39 -07001177
Stefan Reinauer71a30182015-07-30 16:28:13 -07001178 acpi_do_wakeup((uintptr_t)vector, acpi_backup_memory, CONFIG_RAMBASE,
Uwe Hermann622824c2010-11-19 15:14:42 +00001179 HIGH_MEMORY_SAVE);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001180}
1181#endif
Duncan Laurie11290c42012-10-03 19:07:05 -07001182
1183void acpi_save_gnvs(u32 gnvs_address)
1184{
Duncan Laurie9c07c8f2013-03-22 11:08:39 -07001185 u32 *gnvs = cbmem_add(CBMEM_ID_ACPI_GNVS_PTR, sizeof(*gnvs));
Duncan Laurie11290c42012-10-03 19:07:05 -07001186 if (gnvs)
1187 *gnvs = gnvs_address;
1188}