blob: b9fbc8162e6b7a76d74e0936726170a413efe05c [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
Stefan Reinauer777224c2005-01-19 14:06:41 +00009 *
Stefan Reinauer14e22772010-04-27 06:56:47 +000010 * ACPI FADT, FACS, and DSDT table support added by
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000011 * Nick Barker <nick.barker9@btinternet.com>, and those portions
Uwe Hermannc70e9fc2010-02-15 23:10:19 +000012 * Copyright (C) 2004 Nick Barker
Stefan Reinauerf622d592005-11-26 16:56:05 +000013 *
Uwe Hermannc70e9fc2010-02-15 23:10:19 +000014 * Copyright (C) 2005 ADVANCED MICRO DEVICES, INC. All Rights Reserved.
Stefan Reinauerd6edf7a2006-01-05 00:19:52 +000015 * 2005.9 yhlu add SRAT table generation
Stefan Reinauer777224c2005-01-19 14:06:41 +000016 */
17
Stefan Reinauer14e22772010-04-27 06:56:47 +000018/*
Stefan Reinauer777224c2005-01-19 14:06:41 +000019 * Each system port implementing ACPI has to provide two functions:
Stefan Reinauer14e22772010-04-27 06:56:47 +000020 *
Stefan Reinauer777224c2005-01-19 14:06:41 +000021 * write_acpi_tables()
22 * acpi_dump_apics()
Stefan Reinauer14e22772010-04-27 06:56:47 +000023 *
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000024 * See Kontron 986LCD-M port for a good example of an ACPI implementation
25 * in coreboot.
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000026 */
Stefan Reinauer688b3852004-01-28 16:56:14 +000027
28#include <console/console.h>
29#include <string.h>
30#include <arch/acpi.h>
Rudolf Marek293b5f52009-02-01 18:35:15 +000031#include <arch/acpigen.h>
Stefan Reinauer777224c2005-01-19 14:06:41 +000032#include <device/pci.h>
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000033#include <cbmem.h>
Patrick Georgic8feedd2012-02-16 18:43:25 +010034#include <cpu/x86/lapic_def.h>
Duncan Laurie11290c42012-10-03 19:07:05 -070035#include <cpu/cpu.h>
Duncan Lauriecde78012011-10-19 15:32:39 -070036#if CONFIG_COLLECT_TIMESTAMPS
37#include <timestamp.h>
38#endif
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000039
Patrick Georgi9aeb6942012-10-05 21:54:38 +020040/* FIXME: Kconfig doesn't support overridable defaults :-( */
41#ifndef CONFIG_HPET_MIN_TICKS
42#define CONFIG_HPET_MIN_TICKS 0x1000
43#endif
44
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000045u8 acpi_checksum(u8 *table, u32 length)
Stefan Reinauer688b3852004-01-28 16:56:14 +000046{
Uwe Hermann622824c2010-11-19 15:14:42 +000047 u8 ret = 0;
Stefan Reinauer688b3852004-01-28 16:56:14 +000048 while (length--) {
49 ret += *table;
50 table++;
51 }
Stefan Reinauera7648c22004-01-29 17:31:34 +000052 return -ret;
Stefan Reinauer688b3852004-01-28 16:56:14 +000053}
54
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000055/**
Uwe Hermann622824c2010-11-19 15:14:42 +000056 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
57 * and checksum.
Stefan Reinauer06feb882004-02-03 16:11:35 +000058 */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000059void acpi_add_table(acpi_rsdp_t *rsdp, void *table)
Stefan Reinauer688b3852004-01-28 16:56:14 +000060{
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000061 int i, entries_num;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000062 acpi_rsdt_t *rsdt;
63 acpi_xsdt_t *xsdt = NULL;
64
Uwe Hermann622824c2010-11-19 15:14:42 +000065 /* The RSDT is mandatory... */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000066 rsdt = (acpi_rsdt_t *)rsdp->rsdt_address;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000067
Uwe Hermann622824c2010-11-19 15:14:42 +000068 /* ...while the XSDT is not. */
69 if (rsdp->xsdt_address)
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000070 xsdt = (acpi_xsdt_t *)((u32)rsdp->xsdt_address);
Stefan Reinauer14e22772010-04-27 06:56:47 +000071
Uwe Hermann622824c2010-11-19 15:14:42 +000072 /* This should always be MAX_ACPI_TABLES. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000073 entries_num = ARRAY_SIZE(rsdt->entry);
Stefan Reinauer14e22772010-04-27 06:56:47 +000074
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000075 for (i = 0; i < entries_num; i++) {
Uwe Hermann622824c2010-11-19 15:14:42 +000076 if (rsdt->entry[i] == 0)
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000077 break;
Stefan Reinauer688b3852004-01-28 16:56:14 +000078 }
79
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000080 if (i >= entries_num) {
Uwe Hermann622824c2010-11-19 15:14:42 +000081 printk(BIOS_ERR, "ACPI: Error: Could not add ACPI table, "
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +030082 "too many tables.\n");
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000083 return;
84 }
85
Uwe Hermann622824c2010-11-19 15:14:42 +000086 /* Add table to the RSDT. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000087 rsdt->entry[i] = (u32)table;
88
Uwe Hermann622824c2010-11-19 15:14:42 +000089 /* Fix RSDT length or the kernel will assume invalid entries. */
90 rsdt->header.length = sizeof(acpi_header_t) + (sizeof(u32) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000091
Uwe Hermann622824c2010-11-19 15:14:42 +000092 /* Re-calculate checksum. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000093 rsdt->header.checksum = 0; /* Hope this won't get optimized away */
Uwe Hermann622824c2010-11-19 15:14:42 +000094 rsdt->header.checksum = acpi_checksum((u8 *)rsdt, rsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000095
Uwe Hermann622824c2010-11-19 15:14:42 +000096 /*
97 * And now the same thing for the XSDT. We use the same index as for
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000098 * now we want the XSDT and RSDT to always be in sync in coreboot.
99 */
100 if (xsdt) {
Uwe Hermann622824c2010-11-19 15:14:42 +0000101 /* Add table to the XSDT. */
102 xsdt->entry[i] = (u64)(u32)table;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000103
Uwe Hermann622824c2010-11-19 15:14:42 +0000104 /* Fix XSDT length. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000105 xsdt->header.length = sizeof(acpi_header_t) +
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300106 (sizeof(u64) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000107
Uwe Hermann622824c2010-11-19 15:14:42 +0000108 /* Re-calculate checksum. */
109 xsdt->header.checksum = 0;
110 xsdt->header.checksum = acpi_checksum((u8 *)xsdt,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300111 xsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000112 }
113
Uwe Hermann622824c2010-11-19 15:14:42 +0000114 printk(BIOS_DEBUG, "ACPI: added table %d/%d, length now %d\n",
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300115 i + 1, entries_num, rsdt->header.length);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000116}
117
Uwe Hermann622824c2010-11-19 15:14:42 +0000118int acpi_create_mcfg_mmconfig(acpi_mcfg_mmconfig_t *mmconfig, u32 base,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300119 u16 seg_nr, u8 start, u8 end)
Stefan Reinauer26d431a2009-01-20 19:17:51 +0000120{
Vladimir Serbinenko60fccdc2014-10-05 11:05:16 +0200121 memset(mmconfig, 0, sizeof(*mmconfig));
Rudolf Mareke6409f22007-11-03 12:50:26 +0000122 mmconfig->base_address = base;
123 mmconfig->base_reserved = 0;
124 mmconfig->pci_segment_group_number = seg_nr;
125 mmconfig->start_bus_number = start;
126 mmconfig->end_bus_number = end;
Uwe Hermann622824c2010-11-19 15:14:42 +0000127
128 return sizeof(acpi_mcfg_mmconfig_t);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000129}
130
Stefan Reinauer777224c2005-01-19 14:06:41 +0000131int acpi_create_madt_lapic(acpi_madt_lapic_t *lapic, u8 cpu, u8 apic)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000132{
Uwe Hermann622824c2010-11-19 15:14:42 +0000133 lapic->type = 0; /* Local APIC structure */
134 lapic->length = sizeof(acpi_madt_lapic_t);
135 lapic->flags = (1 << 0); /* Processor/LAPIC enabled */
136 lapic->processor_id = cpu;
137 lapic->apic_id = apic;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000138
Uwe Hermann622824c2010-11-19 15:14:42 +0000139 return lapic->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000140}
141
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000142unsigned long acpi_create_madt_lapics(unsigned long current)
143{
144 device_t cpu;
Duncan Laurie11290c42012-10-03 19:07:05 -0700145 int index = 0;
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000146
Uwe Hermann622824c2010-11-19 15:14:42 +0000147 for (cpu = all_devices; cpu; cpu = cpu->next) {
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000148 if ((cpu->path.type != DEVICE_PATH_APIC) ||
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800149 (cpu->bus->dev->path.type != DEVICE_PATH_CPU_CLUSTER)) {
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000150 continue;
151 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000152 if (!cpu->enabled)
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000153 continue;
Uwe Hermann622824c2010-11-19 15:14:42 +0000154 current += acpi_create_madt_lapic((acpi_madt_lapic_t *)current,
Duncan Laurie11290c42012-10-03 19:07:05 -0700155 index, cpu->path.apic.apic_id);
156 index++;
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000157 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000158
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000159 return current;
160}
161
Uwe Hermann622824c2010-11-19 15:14:42 +0000162int acpi_create_madt_ioapic(acpi_madt_ioapic_t *ioapic, u8 id, u32 addr,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300163 u32 gsi_base)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000164{
Uwe Hermann622824c2010-11-19 15:14:42 +0000165 ioapic->type = 1; /* I/O APIC structure */
166 ioapic->length = sizeof(acpi_madt_ioapic_t);
167 ioapic->reserved = 0x00;
168 ioapic->gsi_base = gsi_base;
169 ioapic->ioapic_id = id;
170 ioapic->ioapic_addr = addr;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000171
Uwe Hermann622824c2010-11-19 15:14:42 +0000172 return ioapic->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000173}
174
Stefan Reinauer777224c2005-01-19 14:06:41 +0000175int acpi_create_madt_irqoverride(acpi_madt_irqoverride_t *irqoverride,
Stefan Reinauer06feb882004-02-03 16:11:35 +0000176 u8 bus, u8 source, u32 gsirq, u16 flags)
177{
Uwe Hermann622824c2010-11-19 15:14:42 +0000178 irqoverride->type = 2; /* Interrupt source override */
179 irqoverride->length = sizeof(acpi_madt_irqoverride_t);
180 irqoverride->bus = bus;
181 irqoverride->source = source;
182 irqoverride->gsirq = gsirq;
183 irqoverride->flags = flags;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000184
Uwe Hermann622824c2010-11-19 15:14:42 +0000185 return irqoverride->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000186}
187
Stefan Reinauer777224c2005-01-19 14:06:41 +0000188int acpi_create_madt_lapic_nmi(acpi_madt_lapic_nmi_t *lapic_nmi, u8 cpu,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300189 u16 flags, u8 lint)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000190{
Uwe Hermann622824c2010-11-19 15:14:42 +0000191 lapic_nmi->type = 4; /* Local APIC NMI structure */
192 lapic_nmi->length = sizeof(acpi_madt_lapic_nmi_t);
193 lapic_nmi->flags = flags;
194 lapic_nmi->processor_id = cpu;
195 lapic_nmi->lint = lint;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000196
Uwe Hermann622824c2010-11-19 15:14:42 +0000197 return lapic_nmi->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000198}
199
Stefan Reinauer777224c2005-01-19 14:06:41 +0000200void acpi_create_madt(acpi_madt_t *madt)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000201{
Uwe Hermann622824c2010-11-19 15:14:42 +0000202 acpi_header_t *header = &(madt->header);
203 unsigned long current = (unsigned long)madt + sizeof(acpi_madt_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000204
Stefan Reinauer06feb882004-02-03 16:11:35 +0000205 memset((void *)madt, 0, sizeof(acpi_madt_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000206
Uwe Hermann622824c2010-11-19 15:14:42 +0000207 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000208 memcpy(header->signature, "APIC", 4);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000209 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000210 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000211 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000212
Stefan Reinauer06feb882004-02-03 16:11:35 +0000213 header->length = sizeof(acpi_madt_t);
Uwe Hermann622824c2010-11-19 15:14:42 +0000214 header->revision = 1; /* ACPI 1.0/2.0: 1, ACPI 3.0: 2, ACPI 4.0: 3 */
Stefan Reinauer06feb882004-02-03 16:11:35 +0000215
Uwe Hermann622824c2010-11-19 15:14:42 +0000216 madt->lapic_addr = LOCAL_APIC_ADDR;
217 madt->flags = 0x1; /* PCAT_COMPAT */
Stefan Reinauer06feb882004-02-03 16:11:35 +0000218
Stefan Reinauerf622d592005-11-26 16:56:05 +0000219 current = acpi_fill_madt(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000220
Uwe Hermann622824c2010-11-19 15:14:42 +0000221 /* (Re)calculate length and checksum. */
222 header->length = current - (unsigned long)madt;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000223
Uwe Hermann622824c2010-11-19 15:14:42 +0000224 header->checksum = acpi_checksum((void *)madt, header->length);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000225}
226
Uwe Hermann622824c2010-11-19 15:14:42 +0000227/* MCFG is defined in the PCI Firmware Specification 3.0. */
Rudolf Mareke6409f22007-11-03 12:50:26 +0000228void acpi_create_mcfg(acpi_mcfg_t *mcfg)
229{
Uwe Hermann622824c2010-11-19 15:14:42 +0000230 acpi_header_t *header = &(mcfg->header);
231 unsigned long current = (unsigned long)mcfg + sizeof(acpi_mcfg_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000232
Rudolf Mareke6409f22007-11-03 12:50:26 +0000233 memset((void *)mcfg, 0, sizeof(acpi_mcfg_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000234
Uwe Hermann622824c2010-11-19 15:14:42 +0000235 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000236 memcpy(header->signature, "MCFG", 4);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000237 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000238 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000239 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000240
Rudolf Mareke6409f22007-11-03 12:50:26 +0000241 header->length = sizeof(acpi_mcfg_t);
242 header->revision = 1;
243
244 current = acpi_fill_mcfg(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000245
Uwe Hermann622824c2010-11-19 15:14:42 +0000246 /* (Re)calculate length and checksum. */
247 header->length = current - (unsigned long)mcfg;
248 header->checksum = acpi_checksum((void *)mcfg, header->length);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000249}
250
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200251#if !IS_ENABLED(CONFIG_PER_DEVICE_ACPI_TABLES)
Uwe Hermann622824c2010-11-19 15:14:42 +0000252/*
Martin Roth7b5f8ef2013-07-08 16:22:10 -0600253 * This can be overridden by platform ACPI setup code, if it calls
Uwe Hermann622824c2010-11-19 15:14:42 +0000254 * acpi_create_ssdt_generator().
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000255 */
Uwe Hermann622824c2010-11-19 15:14:42 +0000256unsigned long __attribute__((weak)) acpi_fill_ssdt_generator(
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200257 unsigned long current, const char *oem_table_id)
Uwe Hermann622824c2010-11-19 15:14:42 +0000258{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000259 return current;
260}
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200261#endif
Rudolf Marek293b5f52009-02-01 18:35:15 +0000262
Myles Watson3fe6b702009-10-09 20:13:43 +0000263void acpi_create_ssdt_generator(acpi_header_t *ssdt, const char *oem_table_id)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000264{
Uwe Hermann622824c2010-11-19 15:14:42 +0000265 unsigned long current = (unsigned long)ssdt + sizeof(acpi_header_t);
266
Rudolf Marek293b5f52009-02-01 18:35:15 +0000267 memset((void *)ssdt, 0, sizeof(acpi_header_t));
Uwe Hermann622824c2010-11-19 15:14:42 +0000268
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000269 memcpy(&ssdt->signature, "SSDT", 4);
Uwe Hermann622824c2010-11-19 15:14:42 +0000270 ssdt->revision = 2; /* ACPI 1.0/2.0: ?, ACPI 3.0/4.0: 2 */
Rudolf Marek293b5f52009-02-01 18:35:15 +0000271 memcpy(&ssdt->oem_id, OEM_ID, 6);
272 memcpy(&ssdt->oem_table_id, oem_table_id, 8);
273 ssdt->oem_revision = 42;
Uwe Hermann622824c2010-11-19 15:14:42 +0000274 memcpy(&ssdt->asl_compiler_id, ASLC, 4);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000275 ssdt->asl_compiler_revision = 42;
276 ssdt->length = sizeof(acpi_header_t);
277
Stefan Reinauer8dcd50b2009-03-06 17:24:29 +0000278 acpigen_set_current((char *) current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200279 {
280#if IS_ENABLED(CONFIG_PER_DEVICE_ACPI_TABLES)
281 device_t dev;
282 for (dev = all_devices; dev; dev = dev->next)
283 if (dev->ops && dev->ops->acpi_fill_ssdt_generator) {
Vladimir Serbinenko0a669912014-10-05 14:34:17 +0200284 dev->ops->acpi_fill_ssdt_generator();
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200285 }
Vladimir Serbinenko0a669912014-10-05 14:34:17 +0200286 current = (unsigned long) acpigen_get_current();
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200287#else
288 current = acpi_fill_ssdt_generator(current, oem_table_id);
289#endif
290 }
Rudolf Marek293b5f52009-02-01 18:35:15 +0000291
Uwe Hermann622824c2010-11-19 15:14:42 +0000292 /* (Re)calculate length and checksum. */
Rudolf Marek293b5f52009-02-01 18:35:15 +0000293 ssdt->length = current - (unsigned long)ssdt;
294 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
295}
296
Stefan Reinauerf622d592005-11-26 16:56:05 +0000297int acpi_create_srat_lapic(acpi_srat_lapic_t *lapic, u8 node, u8 apic)
298{
Myles Watson0b4c9f02009-03-10 18:06:47 +0000299 memset((void *)lapic, 0, sizeof(acpi_srat_lapic_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000300
Uwe Hermann622824c2010-11-19 15:14:42 +0000301 lapic->type = 0; /* Processor local APIC/SAPIC affinity structure */
302 lapic->length = sizeof(acpi_srat_lapic_t);
303 lapic->flags = (1 << 0); /* Enabled (the use of this structure). */
304 lapic->proximity_domain_7_0 = node;
305 /* TODO: proximity_domain_31_8, local SAPIC EID, clock domain. */
306 lapic->apic_id = apic;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000307
Uwe Hermann622824c2010-11-19 15:14:42 +0000308 return lapic->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000309}
310
Uwe Hermann622824c2010-11-19 15:14:42 +0000311int 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 +0300312 u32 flags)
Stefan Reinauerf622d592005-11-26 16:56:05 +0000313{
Uwe Hermann622824c2010-11-19 15:14:42 +0000314 mem->type = 1; /* Memory affinity structure */
315 mem->length = sizeof(acpi_srat_mem_t);
316 mem->base_address_low = (basek << 10);
317 mem->base_address_high = (basek >> (32 - 10));
318 mem->length_low = (sizek << 10);
319 mem->length_high = (sizek >> (32 - 10));
320 mem->proximity_domain = node;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000321 mem->flags = flags;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000322
Uwe Hermann622824c2010-11-19 15:14:42 +0000323 return mem->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000324}
325
Uwe Hermann622824c2010-11-19 15:14:42 +0000326/* http://www.microsoft.com/whdc/system/sysinternals/sratdwn.mspx */
Stefan Reinauerf622d592005-11-26 16:56:05 +0000327void acpi_create_srat(acpi_srat_t *srat)
328{
Uwe Hermann622824c2010-11-19 15:14:42 +0000329 acpi_header_t *header = &(srat->header);
330 unsigned long current = (unsigned long)srat + sizeof(acpi_srat_t);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000331
Uwe Hermann622824c2010-11-19 15:14:42 +0000332 memset((void *)srat, 0, sizeof(acpi_srat_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000333
Uwe Hermann622824c2010-11-19 15:14:42 +0000334 /* Fill out header fields. */
335 memcpy(header->signature, "SRAT", 4);
336 memcpy(header->oem_id, OEM_ID, 6);
337 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
338 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000339
Uwe Hermann622824c2010-11-19 15:14:42 +0000340 header->length = sizeof(acpi_srat_t);
341 header->revision = 1; /* ACPI 1.0: N/A, 2.0: 1, 3.0: 2, 4.0: 3 */
Stefan Reinauerf622d592005-11-26 16:56:05 +0000342
Uwe Hermann622824c2010-11-19 15:14:42 +0000343 srat->resv = 1; /* Spec: Reserved to 1 for backwards compatibility. */
Stefan Reinauerf622d592005-11-26 16:56:05 +0000344
Uwe Hermann622824c2010-11-19 15:14:42 +0000345 current = acpi_fill_srat(current);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000346
Uwe Hermann622824c2010-11-19 15:14:42 +0000347 /* (Re)calculate length and checksum. */
348 header->length = current - (unsigned long)srat;
349 header->checksum = acpi_checksum((void *)srat, header->length);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000350}
351
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200352unsigned long __attribute__((weak)) acpi_fill_dmar(unsigned long current)
353{
354 return current;
355}
356
357void acpi_create_dmar(acpi_dmar_t *dmar)
358{
359 acpi_header_t *header = &(dmar->header);
360 unsigned long current = (unsigned long)dmar + sizeof(acpi_dmar_t);
361
362 memset((void *)dmar, 0, sizeof(acpi_dmar_t));
363
364 /* Fill out header fields. */
365 memcpy(header->signature, "DMAR", 4);
366 memcpy(header->oem_id, OEM_ID, 6);
367 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
368 memcpy(header->asl_compiler_id, ASLC, 4);
369
370 header->length = sizeof(acpi_dmar_t);
371 header->revision = 1;
372
373 dmar->host_address_width = 40 - 1; /* FIXME: == MTRR size? */
374 dmar->flags = 0;
375
376 current = acpi_fill_dmar(current);
377
378 /* (Re)calculate length and checksum. */
379 header->length = current - (unsigned long)dmar;
380 header->checksum = acpi_checksum((void *)dmar, header->length);
381}
382
383unsigned long acpi_create_dmar_drhd(unsigned long current, u8 flags,
384 u16 segment, u32 bar)
385{
386 dmar_entry_t *drhd = (dmar_entry_t *)current;
387 memset(drhd, 0, sizeof(*drhd));
388 drhd->type = DMAR_DRHD;
389 drhd->length = sizeof(*drhd); /* will be fixed up later */
390 drhd->flags = flags;
391 drhd->segment = segment;
392 drhd->bar = bar;
393
394 return drhd->length;
395}
396
397void acpi_dmar_drhd_fixup(unsigned long base, unsigned long current)
398{
399 dmar_entry_t *drhd = (dmar_entry_t *)base;
400 drhd->length = current - base;
401}
402
403unsigned long acpi_create_dmar_drhd_ds_pci(unsigned long current, u8 segment,
404 u8 dev, u8 fn)
405{
406 dev_scope_t *ds = (dev_scope_t *)current;
407 memset(ds, 0, sizeof(*ds));
408 ds->type = SCOPE_PCI_ENDPOINT;
409 ds->length = sizeof(*ds) + 2; /* we don't support longer paths yet */
410 ds->start_bus = segment;
411 ds->path[0].dev = dev;
412 ds->path[0].fn = fn;
413
414 return ds->length;
415}
416
Uwe Hermann622824c2010-11-19 15:14:42 +0000417/* http://h21007.www2.hp.com/portal/download/files/unprot/Itanium/slit.pdf */
Yinghai Lud4b278c2006-10-04 20:46:15 +0000418void acpi_create_slit(acpi_slit_t *slit)
419{
Uwe Hermann622824c2010-11-19 15:14:42 +0000420 acpi_header_t *header = &(slit->header);
421 unsigned long current = (unsigned long)slit + sizeof(acpi_slit_t);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000422
Uwe Hermann622824c2010-11-19 15:14:42 +0000423 memset((void *)slit, 0, sizeof(acpi_slit_t));
Yinghai Lud4b278c2006-10-04 20:46:15 +0000424
Uwe Hermann622824c2010-11-19 15:14:42 +0000425 /* Fill out header fields. */
426 memcpy(header->signature, "SLIT", 4);
427 memcpy(header->oem_id, OEM_ID, 6);
428 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
429 memcpy(header->asl_compiler_id, ASLC, 4);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000430
Uwe Hermann622824c2010-11-19 15:14:42 +0000431 header->length = sizeof(acpi_slit_t);
432 header->revision = 1; /* ACPI 1.0: N/A, ACPI 2.0/3.0/4.0: 1 */
Yinghai Lud4b278c2006-10-04 20:46:15 +0000433
Uwe Hermann622824c2010-11-19 15:14:42 +0000434 current = acpi_fill_slit(current);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000435
Uwe Hermann622824c2010-11-19 15:14:42 +0000436 /* (Re)calculate length and checksum. */
437 header->length = current - (unsigned long)slit;
438 header->checksum = acpi_checksum((void *)slit, header->length);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000439}
440
Uwe Hermann622824c2010-11-19 15:14:42 +0000441/* http://www.intel.com/hardwaredesign/hpetspec_1.pdf */
Stefan Reinauer777224c2005-01-19 14:06:41 +0000442void acpi_create_hpet(acpi_hpet_t *hpet)
Stefan Reinauer688b3852004-01-28 16:56:14 +0000443{
Uwe Hermann622824c2010-11-19 15:14:42 +0000444 acpi_header_t *header = &(hpet->header);
445 acpi_addr_t *addr = &(hpet->addr);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000446
Stefan Reinauera7648c22004-01-29 17:31:34 +0000447 memset((void *)hpet, 0, sizeof(acpi_hpet_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000448
Uwe Hermann622824c2010-11-19 15:14:42 +0000449 /* Fill out header fields. */
Stefan Reinauer4704dc52009-07-22 01:11:37 +0000450 memcpy(header->signature, "HPET", 4);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000451 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000452 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000453 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000454
Stefan Reinauer688b3852004-01-28 16:56:14 +0000455 header->length = sizeof(acpi_hpet_t);
Uwe Hermann622824c2010-11-19 15:14:42 +0000456 header->revision = 1; /* Currently 1. Table added in ACPI 2.0. */
Stefan Reinauer688b3852004-01-28 16:56:14 +0000457
Uwe Hermann622824c2010-11-19 15:14:42 +0000458 /* Fill out HPET address. */
459 addr->space_id = 0; /* Memory */
460 addr->bit_width = 64;
461 addr->bit_offset = 0;
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200462 addr->addrl = CONFIG_HPET_ADDRESS & 0xffffffff;
463 addr->addrh = ((unsigned long long)CONFIG_HPET_ADDRESS) >> 32;
Stefan Reinauer688b3852004-01-28 16:56:14 +0000464
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200465 hpet->id = *(unsigned int*)CONFIG_HPET_ADDRESS;
Uwe Hermann622824c2010-11-19 15:14:42 +0000466 hpet->number = 0;
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200467 hpet->min_tick = CONFIG_HPET_MIN_TICKS;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000468
Uwe Hermann622824c2010-11-19 15:14:42 +0000469 header->checksum = acpi_checksum((void *)hpet, sizeof(acpi_hpet_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000470}
Uwe Hermann622824c2010-11-19 15:14:42 +0000471
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200472unsigned long acpi_write_hpet(unsigned long current, acpi_rsdp_t *rsdp)
473{
474 acpi_hpet_t *hpet;
475
476 /*
477 * We explicitly add these tables later on:
478 */
479 printk(BIOS_DEBUG, "ACPI: * HPET\n");
480
481 hpet = (acpi_hpet_t *) current;
482 current += sizeof(acpi_hpet_t);
483 current = ALIGN(current, 16);
484 acpi_create_hpet(hpet);
485 acpi_add_table(rsdp, hpet);
486
487 return current;
488}
489
Stefan Reinauer777224c2005-01-19 14:06:41 +0000490void acpi_create_facs(acpi_facs_t *facs)
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000491{
Uwe Hermann622824c2010-11-19 15:14:42 +0000492 memset((void *)facs, 0, sizeof(acpi_facs_t));
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000493
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000494 memcpy(facs->signature, "FACS", 4);
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000495 facs->length = sizeof(acpi_facs_t);
496 facs->hardware_signature = 0;
497 facs->firmware_waking_vector = 0;
498 facs->global_lock = 0;
499 facs->flags = 0;
500 facs->x_firmware_waking_vector_l = 0;
501 facs->x_firmware_waking_vector_h = 0;
Uwe Hermann622824c2010-11-19 15:14:42 +0000502 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 +0000503}
Stefan Reinauer777224c2005-01-19 14:06:41 +0000504
505void acpi_write_rsdt(acpi_rsdt_t *rsdt)
Stefan Reinauer14e22772010-04-27 06:56:47 +0000506{
Uwe Hermann622824c2010-11-19 15:14:42 +0000507 acpi_header_t *header = &(rsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000508
Uwe Hermann622824c2010-11-19 15:14:42 +0000509 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000510 memcpy(header->signature, "RSDT", 4);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000511 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000512 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000513 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000514
Stefan Reinauer688b3852004-01-28 16:56:14 +0000515 header->length = sizeof(acpi_rsdt_t);
Uwe Hermann622824c2010-11-19 15:14:42 +0000516 header->revision = 1; /* ACPI 1.0/2.0/3.0/4.0: 1 */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000517
Uwe Hermann622824c2010-11-19 15:14:42 +0000518 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauer688b3852004-01-28 16:56:14 +0000519
Uwe Hermann622824c2010-11-19 15:14:42 +0000520 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000521 header->checksum = acpi_checksum((void *)rsdt, sizeof(acpi_rsdt_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000522}
523
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000524void acpi_write_xsdt(acpi_xsdt_t *xsdt)
Stefan Reinauer14e22772010-04-27 06:56:47 +0000525{
Uwe Hermann622824c2010-11-19 15:14:42 +0000526 acpi_header_t *header = &(xsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000527
Uwe Hermann622824c2010-11-19 15:14:42 +0000528 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000529 memcpy(header->signature, "XSDT", 4);
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000530 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000531 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000532 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000533
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000534 header->length = sizeof(acpi_xsdt_t);
Uwe Hermann622824c2010-11-19 15:14:42 +0000535 header->revision = 1; /* ACPI 1.0: N/A, 2.0/3.0/4.0: 1 */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000536
Uwe Hermann622824c2010-11-19 15:14:42 +0000537 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000538
Uwe Hermann622824c2010-11-19 15:14:42 +0000539 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000540 header->checksum = acpi_checksum((void *)xsdt, sizeof(acpi_xsdt_t));
541}
542
543void acpi_write_rsdp(acpi_rsdp_t *rsdp, acpi_rsdt_t *rsdt, acpi_xsdt_t *xsdt)
Stefan Reinauer688b3852004-01-28 16:56:14 +0000544{
Stefan Reinauerd18faac2009-11-05 18:06:43 +0000545 memset(rsdp, 0, sizeof(acpi_rsdp_t));
Uwe Hermann622824c2010-11-19 15:14:42 +0000546
Stefan Reinauer688b3852004-01-28 16:56:14 +0000547 memcpy(rsdp->signature, RSDP_SIG, 8);
548 memcpy(rsdp->oem_id, OEM_ID, 6);
Uwe Hermann622824c2010-11-19 15:14:42 +0000549
550 rsdp->length = sizeof(acpi_rsdp_t);
551 rsdp->rsdt_address = (u32)rsdt;
552
553 /*
554 * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2.
555 *
556 * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
557 * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
558 * revision 0).
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000559 */
560 if (xsdt == NULL) {
Uwe Hermann622824c2010-11-19 15:14:42 +0000561 rsdp->revision = 0;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000562 } else {
Uwe Hermann622824c2010-11-19 15:14:42 +0000563 rsdp->xsdt_address = (u64)(u32)xsdt;
564 rsdp->revision = 2;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000565 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000566
567 /* Calculate checksums. */
568 rsdp->checksum = acpi_checksum((void *)rsdp, 20);
569 rsdp->ext_checksum = acpi_checksum((void *)rsdp, sizeof(acpi_rsdp_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000570}
571
zbaocaf494c82012-04-13 13:57:14 +0800572unsigned long __attribute__((weak)) acpi_fill_hest(acpi_hest_t *hest)
573{
574 return (unsigned long)hest;
575}
576
577unsigned long acpi_create_hest_error_source(acpi_hest_t *hest, acpi_hest_esd_t *esd, u16 type, void *data, u16 data_len)
578{
579 acpi_header_t *header = &(hest->header);
580 acpi_hest_hen_t *hen;
581 void *pos;
582 u16 len;
583
584 pos = esd;
585 memset(pos, 0, sizeof(acpi_hest_esd_t));
586 len = 0;
587 esd->type = type; /* MCE */
588 esd->source_id = hest->error_source_count;
589 esd->flags = 0; /* FIRMWARE_FIRST */
590 esd->enabled = 1;
591 esd->prealloc_erecords = 1;
592 esd->max_section_per_record = 0x1;
593
594 len += sizeof(acpi_hest_esd_t);
595 pos = esd + 1;
596
597 switch (type) {
598 case 0: /* MCE */
599 break;
600 case 1: /* CMC */
601 hen = (acpi_hest_hen_t *) (pos);
602 memset(pos, 0, sizeof(acpi_hest_hen_t));
603 hen->type = 3; /* SCI? */
604 hen->length = sizeof(acpi_hest_hen_t);
605 hen->conf_we = 0; /* Configuration Write Enable. */
606 hen->poll_interval = 0;
607 hen->vector = 0;
608 hen->sw2poll_threshold_val = 0;
609 hen->sw2poll_threshold_win = 0;
610 hen->error_threshold_val = 0;
611 hen->error_threshold_win = 0;
612 len += sizeof(acpi_hest_hen_t);
613 pos = hen + 1;
614 break;
615 case 2: /* NMI */
616 case 6: /* AER Root Port */
617 case 7: /* AER Endpoint */
618 case 8: /* AER Bridge */
619 case 9: /* Generic Hardware Error Source. */
620 /* TODO: */
621 break;
622 default:
623 printk(BIOS_DEBUG, "Invalid type of Error Source.");
624 break;
625 }
626 hest->error_source_count ++;
627
628 memcpy(pos, data, data_len);
629 len += data_len;
630 header->length += len;
631
632 return len;
633}
634
635/* ACPI 4.0 */
636void acpi_write_hest(acpi_hest_t *hest)
637{
638 acpi_header_t *header = &(hest->header);
639
640 memset(hest, 0, sizeof(acpi_hest_t));
641
642 memcpy(header->signature, "HEST", 4);
643 memcpy(header->oem_id, OEM_ID, 6);
644 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
645 memcpy(header->asl_compiler_id, ASLC, 4);
646 header->length += sizeof(acpi_hest_t);
647 header->revision = 1;
648
649 acpi_fill_hest(hest);
650
651 /* Calculate checksums. */
652 header->checksum = acpi_checksum((void *)hest, header->length);
653}
654
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200655#if IS_ENABLED(CONFIG_PER_DEVICE_ACPI_TABLES)
656
657extern const unsigned char AmlCode[];
658
659#define ALIGN_CURRENT current = (ALIGN(current, 16))
660unsigned long write_acpi_tables(unsigned long start)
661{
662 unsigned long current;
663 acpi_rsdp_t *rsdp;
664 acpi_rsdt_t *rsdt;
665 acpi_xsdt_t *xsdt;
666 acpi_fadt_t *fadt;
667 acpi_facs_t *facs;
668#if CONFIG_HAVE_ACPI_SLIC
669 acpi_header_t *slic;
670#endif
671 acpi_header_t *ssdt;
672 acpi_header_t *dsdt;
673 acpi_mcfg_t *mcfg;
674 acpi_madt_t *madt;
675 device_t dev;
676
677 current = start;
678
679 /* Align ACPI tables to 16byte */
680 ALIGN_CURRENT;
681
682 printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx.\n", start);
683
684 /* We need at least an RSDP and an RSDT Table */
685 rsdp = (acpi_rsdp_t *) current;
686 current += sizeof(acpi_rsdp_t);
687 ALIGN_CURRENT;
688 rsdt = (acpi_rsdt_t *) current;
689 current += sizeof(acpi_rsdt_t);
690 ALIGN_CURRENT;
691 xsdt = (acpi_xsdt_t *) current;
692 current += sizeof(acpi_xsdt_t);
693 ALIGN_CURRENT;
694
695 /* clear all table memory */
696 memset((void *) start, 0, current - start);
697
698 acpi_write_rsdp(rsdp, rsdt, xsdt);
699 acpi_write_rsdt(rsdt);
700 acpi_write_xsdt(xsdt);
701
702 printk(BIOS_DEBUG, "ACPI: * FACS\n");
703 facs = (acpi_facs_t *) current;
704 current += sizeof(acpi_facs_t);
705 ALIGN_CURRENT;
706 acpi_create_facs(facs);
707
708 printk(BIOS_DEBUG, "ACPI: * DSDT\n");
709 dsdt = (acpi_header_t *) current;
710 memcpy(dsdt, &AmlCode, sizeof(acpi_header_t));
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +0200711 if (dsdt->length >= sizeof(acpi_header_t)) {
712 current += sizeof(acpi_header_t);
713
714 acpigen_set_current((char *) current);
715 for (dev = all_devices; dev; dev = dev->next)
716 if (dev->ops && dev->ops->acpi_inject_dsdt_generator) {
717 dev->ops->acpi_inject_dsdt_generator();
718 }
719 current = (unsigned long) acpigen_get_current();
720 memcpy((char *)current,
721 (char *)&AmlCode + sizeof(acpi_header_t),
722 dsdt->length - sizeof(acpi_header_t));
723 current += dsdt->length - sizeof(acpi_header_t);
724
725 /* (Re)calculate length and checksum. */
726 dsdt->length = current - (unsigned long)dsdt;
727 dsdt->checksum = 0;
728 dsdt->checksum = acpi_checksum((void *)dsdt, dsdt->length);
729 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200730
731 ALIGN_CURRENT;
732
733 printk(BIOS_DEBUG, "ACPI: * FADT\n");
734 fadt = (acpi_fadt_t *) current;
735 current += sizeof(acpi_fadt_t);
736 ALIGN_CURRENT;
737
738 acpi_create_fadt(fadt, facs, dsdt);
739 acpi_add_table(rsdp, fadt);
740
741#if CONFIG_HAVE_ACPI_SLIC
742 printk(BIOS_DEBUG, "ACPI: * SLIC\n");
743 slic = (acpi_header_t *)current;
744 current += acpi_create_slic(current);
745 ALIGN_CURRENT;
746 acpi_add_table(rsdp, slic);
747#endif
748
749 printk(BIOS_DEBUG, "ACPI: * SSDT\n");
750 ssdt = (acpi_header_t *)current;
751 acpi_create_ssdt_generator(ssdt, ACPI_TABLE_CREATOR);
Vladimir Serbinenko9310df82014-10-10 20:40:41 +0200752 if (ssdt->length > sizeof(acpi_header_t)) {
753 current += ssdt->length;
754 acpi_add_table(rsdp, ssdt);
755 ALIGN_CURRENT;
756 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200757
758 printk(BIOS_DEBUG, "ACPI: * MCFG\n");
759 mcfg = (acpi_mcfg_t *) current;
760 acpi_create_mcfg(mcfg);
761 if (mcfg->header.length > sizeof(acpi_mcfg_t)) {
762 current += mcfg->header.length;
763 ALIGN_CURRENT;
764 acpi_add_table(rsdp, mcfg);
765 }
766
767 printk(BIOS_DEBUG, "ACPI: * MADT\n");
768
769 madt = (acpi_madt_t *) current;
770 acpi_create_madt(madt);
771 if (madt->header.length > sizeof(acpi_madt_t)) {
772 current+=madt->header.length;
773 acpi_add_table(rsdp,madt);
774 }
775 ALIGN_CURRENT;
776
777 printk(BIOS_DEBUG, "current = %lx\n", current);
778
779 for (dev = all_devices; dev; dev = dev->next) {
780 if (dev->ops && dev->ops->write_acpi_tables) {
781 current = dev->ops->write_acpi_tables(current, rsdp);
782 ALIGN_CURRENT;
783 }
784 }
785
786 printk(BIOS_INFO, "ACPI: done.\n");
787 return current;
788}
789#endif
790
Patrick Georgie1667822012-05-05 15:29:32 +0200791#if CONFIG_HAVE_ACPI_RESUME
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500792void acpi_resume(void *wake_vec)
Stefan Reinauer7e9771c2009-04-22 08:17:38 +0000793{
Duncan Laurie11290c42012-10-03 19:07:05 -0700794#if CONFIG_HAVE_SMI_HANDLER
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500795 u32 *gnvs_address = cbmem_find(CBMEM_ID_ACPI_GNVS_PTR);
Duncan Laurie11290c42012-10-03 19:07:05 -0700796
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500797 /* Restore GNVS pointer in SMM if found */
798 if (gnvs_address && *gnvs_address) {
799 printk(BIOS_DEBUG, "Restore GNVS pointer to 0x%08x\n",
800 *gnvs_address);
801 smm_setup_structures((void *)*gnvs_address, NULL, NULL);
Stefan Reinauercb91e152012-04-03 23:28:22 +0200802 }
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500803#endif
804
805 /* Call mainboard resume handler first, if defined. */
806 if (mainboard_suspend_resume)
807 mainboard_suspend_resume();
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500808
809 post_code(POST_OS_RESUME);
810 acpi_jump_to_wakeup(wake_vec);
Stefan Reinauer7e9771c2009-04-22 08:17:38 +0000811}
812
Uwe Hermann622824c2010-11-19 15:14:42 +0000813/* This is to be filled by SB code - startup value what was found. */
Stefan Reinauer7e9771c2009-04-22 08:17:38 +0000814u8 acpi_slp_type = 0;
Stefan Reinauer688b3852004-01-28 16:56:14 +0000815
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +0300816static void acpi_handoff_wakeup(void)
817{
818 static int once = 0;
819 if (once)
820 return;
821 if (acpi_get_sleep_type)
822 acpi_slp_type = acpi_get_sleep_type();
823 once = 1;
824}
825
Kyösti Mälkkicb28f3f2014-01-03 15:15:22 +0200826int acpi_is_wakeup(void)
Rudolf Marek33cafe52009-04-13 18:07:02 +0000827{
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +0300828 acpi_handoff_wakeup();
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +0100829 /* Both resume from S2 and resume from S3 restart at CPU reset */
830 return (acpi_slp_type == 3 || acpi_slp_type == 2);
Rudolf Marek33cafe52009-04-13 18:07:02 +0000831}
832
Kyösti Mälkki4d9b7722014-06-19 19:45:40 +0300833int acpi_is_wakeup_s3(void)
834{
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +0300835 acpi_handoff_wakeup();
Kyösti Mälkki4d9b7722014-06-19 19:45:40 +0300836 return (acpi_slp_type == 3);
837}
838
Kyösti Mälkkicb28f3f2014-01-03 15:15:22 +0200839void acpi_fail_wakeup(void)
840{
841 if (acpi_slp_type == 3 || acpi_slp_type == 2)
842 acpi_slp_type = 0;
843}
844
Rudolf Marek33cafe52009-04-13 18:07:02 +0000845static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp)
846{
847 if (strncmp((char *)rsdp, RSDP_SIG, sizeof(RSDP_SIG) - 1) != 0)
848 return NULL;
849
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000850 printk(BIOS_DEBUG, "Looking on %p for valid checksum\n", rsdp);
Rudolf Marek33cafe52009-04-13 18:07:02 +0000851
852 if (acpi_checksum((void *)rsdp, 20) != 0)
853 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000854 printk(BIOS_DEBUG, "Checksum 1 passed\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +0000855
856 if ((rsdp->revision > 1) && (acpi_checksum((void *)rsdp,
857 rsdp->length) != 0))
858 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000859 printk(BIOS_DEBUG, "Checksum 2 passed all OK\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +0000860
861 return rsdp;
862}
863
864static acpi_rsdp_t *rsdp;
865
866void *acpi_get_wakeup_rsdp(void)
867{
868 return rsdp;
869}
870
871void *acpi_find_wakeup_vector(void)
872{
873 char *p, *end;
Rudolf Marek33cafe52009-04-13 18:07:02 +0000874 acpi_rsdt_t *rsdt;
875 acpi_facs_t *facs;
Martin Rothc5f49262012-12-14 19:17:55 -0700876 acpi_fadt_t *fadt = NULL;
Uwe Hermann622824c2010-11-19 15:14:42 +0000877 void *wake_vec;
Rudolf Marek33cafe52009-04-13 18:07:02 +0000878 int i;
879
880 rsdp = NULL;
881
882 if (!acpi_is_wakeup())
883 return NULL;
884
Uwe Hermann622824c2010-11-19 15:14:42 +0000885 printk(BIOS_DEBUG, "Trying to find the wakeup vector...\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +0000886
Uwe Hermann622824c2010-11-19 15:14:42 +0000887 /* Find RSDP. */
888 for (p = (char *)0xe0000; p < (char *)0xfffff; p += 16) {
889 if ((rsdp = valid_rsdp((acpi_rsdp_t *)p)))
Rudolf Marek33cafe52009-04-13 18:07:02 +0000890 break;
891 }
892
893 if (rsdp == NULL)
894 return NULL;
895
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000896 printk(BIOS_DEBUG, "RSDP found at %p\n", rsdp);
Rudolf Marek33cafe52009-04-13 18:07:02 +0000897 rsdt = (acpi_rsdt_t *) rsdp->rsdt_address;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000898
Uwe Hermann622824c2010-11-19 15:14:42 +0000899 end = (char *)rsdt + rsdt->header.length;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000900 printk(BIOS_DEBUG, "RSDT found at %p ends at %p\n", rsdt, end);
Rudolf Marek33cafe52009-04-13 18:07:02 +0000901
Uwe Hermann622824c2010-11-19 15:14:42 +0000902 for (i = 0; ((char *)&rsdt->entry[i]) < end; i++) {
903 fadt = (acpi_fadt_t *)rsdt->entry[i];
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000904 if (strncmp((char *)fadt, "FACP", 4) == 0)
Rudolf Marek33cafe52009-04-13 18:07:02 +0000905 break;
906 fadt = NULL;
907 }
908
909 if (fadt == NULL)
910 return NULL;
911
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000912 printk(BIOS_DEBUG, "FADT found at %p\n", fadt);
Stefan Reinauer69390db2009-05-26 12:33:52 +0000913 facs = (acpi_facs_t *)fadt->firmware_ctrl;
Rudolf Marek33cafe52009-04-13 18:07:02 +0000914
Stefan Reinauer7e9771c2009-04-22 08:17:38 +0000915 if (facs == NULL) {
Uwe Hermann622824c2010-11-19 15:14:42 +0000916 printk(BIOS_DEBUG, "No FACS found, wake up from S3 not "
917 "possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +0000918 return NULL;
Stefan Reinauer7e9771c2009-04-22 08:17:38 +0000919 }
Rudolf Marek33cafe52009-04-13 18:07:02 +0000920
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000921 printk(BIOS_DEBUG, "FACS found at %p\n", facs);
Uwe Hermann622824c2010-11-19 15:14:42 +0000922 wake_vec = (void *)facs->firmware_waking_vector;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000923 printk(BIOS_DEBUG, "OS waking vector is %p\n", wake_vec);
Uwe Hermann622824c2010-11-19 15:14:42 +0000924
Rudolf Marek33cafe52009-04-13 18:07:02 +0000925 return wake_vec;
926}
927
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +0100928#if CONFIG_SMP
Rudolf Marek33cafe52009-04-13 18:07:02 +0000929extern char *lowmem_backup;
930extern char *lowmem_backup_ptr;
931extern int lowmem_backup_size;
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +0100932#endif
Rudolf Marek33cafe52009-04-13 18:07:02 +0000933
Uwe Hermann622824c2010-11-19 15:14:42 +0000934#define WAKEUP_BASE 0x600
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000935
Uwe Hermann622824c2010-11-19 15:14:42 +0000936void (*acpi_do_wakeup)(u32 vector, u32 backup_source, u32 backup_target,
Stefan Reinauer399486e2012-12-06 13:54:29 -0800937 u32 backup_size) asmlinkage = (void *)WAKEUP_BASE;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000938
Aaron Durbina146d582013-02-08 16:56:51 -0600939extern unsigned char __wakeup;
940extern unsigned int __wakeup_size;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000941
Rudolf Marek33cafe52009-04-13 18:07:02 +0000942void acpi_jump_to_wakeup(void *vector)
943{
Aaron Durbin8e4a3552013-02-08 17:28:04 -0600944 u32 acpi_backup_memory = 0;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000945
Kyösti Mälkki2ca2afe2014-06-17 15:41:37 +0300946 if (HIGH_MEMORY_SAVE && acpi_s3_resume_allowed()) {
947 acpi_backup_memory = (u32)cbmem_find(CBMEM_ID_RESUME);
948
949 if (!acpi_backup_memory) {
950 printk(BIOS_WARNING, "ACPI: Backup memory missing. "
951 "No S3 resume.\n");
952 return;
953 }
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000954 }
955
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +0100956#if CONFIG_SMP
Martin Roth7b5f8ef2013-07-08 16:22:10 -0600957 // FIXME: This should go into the ACPI backup memory, too. No pork sausages.
Uwe Hermann622824c2010-11-19 15:14:42 +0000958 /*
959 * Just restore the SMP trampoline and continue with wakeup on
960 * assembly level.
961 */
Rudolf Marek33cafe52009-04-13 18:07:02 +0000962 memcpy(lowmem_backup_ptr, lowmem_backup, lowmem_backup_size);
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +0100963#endif
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000964
Uwe Hermann622824c2010-11-19 15:14:42 +0000965 /* Copy wakeup trampoline in place. */
Aaron Durbina146d582013-02-08 16:56:51 -0600966 memcpy((void *)WAKEUP_BASE, &__wakeup, __wakeup_size);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000967
Duncan Lauriecde78012011-10-19 15:32:39 -0700968#if CONFIG_COLLECT_TIMESTAMPS
969 timestamp_add_now(TS_ACPI_WAKE_JUMP);
970#endif
971
Uwe Hermann622824c2010-11-19 15:14:42 +0000972 acpi_do_wakeup((u32)vector, acpi_backup_memory, CONFIG_RAMBASE,
973 HIGH_MEMORY_SAVE);
Rudolf Marek33cafe52009-04-13 18:07:02 +0000974}
975#endif
Duncan Laurie11290c42012-10-03 19:07:05 -0700976
977void acpi_save_gnvs(u32 gnvs_address)
978{
Duncan Laurie9c07c8f2013-03-22 11:08:39 -0700979 u32 *gnvs = cbmem_add(CBMEM_ID_ACPI_GNVS_PTR, sizeof(*gnvs));
Duncan Laurie11290c42012-10-03 19:07:05 -0700980 if (gnvs)
981 *gnvs = gnvs_address;
982}