blob: b2e0df3687793eb72e47a45ef3ef71ab9aa8783f [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
Kyösti Mälkki9e94dbf2015-01-08 20:03:18 +020039#include <romstage_handoff.h>
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000040
Patrick Georgi9aeb6942012-10-05 21:54:38 +020041/* FIXME: Kconfig doesn't support overridable defaults :-( */
42#ifndef CONFIG_HPET_MIN_TICKS
43#define CONFIG_HPET_MIN_TICKS 0x1000
44#endif
45
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000046u8 acpi_checksum(u8 *table, u32 length)
Stefan Reinauer688b3852004-01-28 16:56:14 +000047{
Uwe Hermann622824c2010-11-19 15:14:42 +000048 u8 ret = 0;
Stefan Reinauer688b3852004-01-28 16:56:14 +000049 while (length--) {
50 ret += *table;
51 table++;
52 }
Stefan Reinauera7648c22004-01-29 17:31:34 +000053 return -ret;
Stefan Reinauer688b3852004-01-28 16:56:14 +000054}
55
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000056/**
Uwe Hermann622824c2010-11-19 15:14:42 +000057 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
58 * and checksum.
Stefan Reinauer06feb882004-02-03 16:11:35 +000059 */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000060void acpi_add_table(acpi_rsdp_t *rsdp, void *table)
Stefan Reinauer688b3852004-01-28 16:56:14 +000061{
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000062 int i, entries_num;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000063 acpi_rsdt_t *rsdt;
64 acpi_xsdt_t *xsdt = NULL;
65
Uwe Hermann622824c2010-11-19 15:14:42 +000066 /* The RSDT is mandatory... */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000067 rsdt = (acpi_rsdt_t *)rsdp->rsdt_address;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000068
Uwe Hermann622824c2010-11-19 15:14:42 +000069 /* ...while the XSDT is not. */
70 if (rsdp->xsdt_address)
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000071 xsdt = (acpi_xsdt_t *)((u32)rsdp->xsdt_address);
Stefan Reinauer14e22772010-04-27 06:56:47 +000072
Uwe Hermann622824c2010-11-19 15:14:42 +000073 /* This should always be MAX_ACPI_TABLES. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000074 entries_num = ARRAY_SIZE(rsdt->entry);
Stefan Reinauer14e22772010-04-27 06:56:47 +000075
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000076 for (i = 0; i < entries_num; i++) {
Uwe Hermann622824c2010-11-19 15:14:42 +000077 if (rsdt->entry[i] == 0)
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000078 break;
Stefan Reinauer688b3852004-01-28 16:56:14 +000079 }
80
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000081 if (i >= entries_num) {
Uwe Hermann622824c2010-11-19 15:14:42 +000082 printk(BIOS_ERR, "ACPI: Error: Could not add ACPI table, "
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +030083 "too many tables.\n");
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000084 return;
85 }
86
Uwe Hermann622824c2010-11-19 15:14:42 +000087 /* Add table to the RSDT. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000088 rsdt->entry[i] = (u32)table;
89
Uwe Hermann622824c2010-11-19 15:14:42 +000090 /* Fix RSDT length or the kernel will assume invalid entries. */
91 rsdt->header.length = sizeof(acpi_header_t) + (sizeof(u32) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000092
Uwe Hermann622824c2010-11-19 15:14:42 +000093 /* Re-calculate checksum. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000094 rsdt->header.checksum = 0; /* Hope this won't get optimized away */
Uwe Hermann622824c2010-11-19 15:14:42 +000095 rsdt->header.checksum = acpi_checksum((u8 *)rsdt, rsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000096
Uwe Hermann622824c2010-11-19 15:14:42 +000097 /*
98 * And now the same thing for the XSDT. We use the same index as for
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000099 * now we want the XSDT and RSDT to always be in sync in coreboot.
100 */
101 if (xsdt) {
Uwe Hermann622824c2010-11-19 15:14:42 +0000102 /* Add table to the XSDT. */
103 xsdt->entry[i] = (u64)(u32)table;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000104
Uwe Hermann622824c2010-11-19 15:14:42 +0000105 /* Fix XSDT length. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000106 xsdt->header.length = sizeof(acpi_header_t) +
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300107 (sizeof(u64) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000108
Uwe Hermann622824c2010-11-19 15:14:42 +0000109 /* Re-calculate checksum. */
110 xsdt->header.checksum = 0;
111 xsdt->header.checksum = acpi_checksum((u8 *)xsdt,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300112 xsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000113 }
114
Uwe Hermann622824c2010-11-19 15:14:42 +0000115 printk(BIOS_DEBUG, "ACPI: added table %d/%d, length now %d\n",
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300116 i + 1, entries_num, rsdt->header.length);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000117}
118
Uwe Hermann622824c2010-11-19 15:14:42 +0000119int acpi_create_mcfg_mmconfig(acpi_mcfg_mmconfig_t *mmconfig, u32 base,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300120 u16 seg_nr, u8 start, u8 end)
Stefan Reinauer26d431a2009-01-20 19:17:51 +0000121{
Vladimir Serbinenko60fccdc2014-10-05 11:05:16 +0200122 memset(mmconfig, 0, sizeof(*mmconfig));
Rudolf Mareke6409f22007-11-03 12:50:26 +0000123 mmconfig->base_address = base;
124 mmconfig->base_reserved = 0;
125 mmconfig->pci_segment_group_number = seg_nr;
126 mmconfig->start_bus_number = start;
127 mmconfig->end_bus_number = end;
Uwe Hermann622824c2010-11-19 15:14:42 +0000128
129 return sizeof(acpi_mcfg_mmconfig_t);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000130}
131
Stefan Reinauer777224c2005-01-19 14:06:41 +0000132int acpi_create_madt_lapic(acpi_madt_lapic_t *lapic, u8 cpu, u8 apic)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000133{
Uwe Hermann622824c2010-11-19 15:14:42 +0000134 lapic->type = 0; /* Local APIC structure */
135 lapic->length = sizeof(acpi_madt_lapic_t);
136 lapic->flags = (1 << 0); /* Processor/LAPIC enabled */
137 lapic->processor_id = cpu;
138 lapic->apic_id = apic;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000139
Uwe Hermann622824c2010-11-19 15:14:42 +0000140 return lapic->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000141}
142
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000143unsigned long acpi_create_madt_lapics(unsigned long current)
144{
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100145 struct device *cpu;
Duncan Laurie11290c42012-10-03 19:07:05 -0700146 int index = 0;
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000147
Uwe Hermann622824c2010-11-19 15:14:42 +0000148 for (cpu = all_devices; cpu; cpu = cpu->next) {
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000149 if ((cpu->path.type != DEVICE_PATH_APIC) ||
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800150 (cpu->bus->dev->path.type != DEVICE_PATH_CPU_CLUSTER)) {
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000151 continue;
152 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000153 if (!cpu->enabled)
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000154 continue;
Uwe Hermann622824c2010-11-19 15:14:42 +0000155 current += acpi_create_madt_lapic((acpi_madt_lapic_t *)current,
Duncan Laurie11290c42012-10-03 19:07:05 -0700156 index, cpu->path.apic.apic_id);
157 index++;
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000158 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000159
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000160 return current;
161}
162
Uwe Hermann622824c2010-11-19 15:14:42 +0000163int acpi_create_madt_ioapic(acpi_madt_ioapic_t *ioapic, u8 id, u32 addr,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300164 u32 gsi_base)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000165{
Uwe Hermann622824c2010-11-19 15:14:42 +0000166 ioapic->type = 1; /* I/O APIC structure */
167 ioapic->length = sizeof(acpi_madt_ioapic_t);
168 ioapic->reserved = 0x00;
169 ioapic->gsi_base = gsi_base;
170 ioapic->ioapic_id = id;
171 ioapic->ioapic_addr = addr;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000172
Uwe Hermann622824c2010-11-19 15:14:42 +0000173 return ioapic->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000174}
175
Stefan Reinauer777224c2005-01-19 14:06:41 +0000176int acpi_create_madt_irqoverride(acpi_madt_irqoverride_t *irqoverride,
Stefan Reinauer06feb882004-02-03 16:11:35 +0000177 u8 bus, u8 source, u32 gsirq, u16 flags)
178{
Uwe Hermann622824c2010-11-19 15:14:42 +0000179 irqoverride->type = 2; /* Interrupt source override */
180 irqoverride->length = sizeof(acpi_madt_irqoverride_t);
181 irqoverride->bus = bus;
182 irqoverride->source = source;
183 irqoverride->gsirq = gsirq;
184 irqoverride->flags = flags;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000185
Uwe Hermann622824c2010-11-19 15:14:42 +0000186 return irqoverride->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000187}
188
Stefan Reinauer777224c2005-01-19 14:06:41 +0000189int acpi_create_madt_lapic_nmi(acpi_madt_lapic_nmi_t *lapic_nmi, u8 cpu,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300190 u16 flags, u8 lint)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000191{
Uwe Hermann622824c2010-11-19 15:14:42 +0000192 lapic_nmi->type = 4; /* Local APIC NMI structure */
193 lapic_nmi->length = sizeof(acpi_madt_lapic_nmi_t);
194 lapic_nmi->flags = flags;
195 lapic_nmi->processor_id = cpu;
196 lapic_nmi->lint = lint;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000197
Uwe Hermann622824c2010-11-19 15:14:42 +0000198 return lapic_nmi->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000199}
200
Stefan Reinauer777224c2005-01-19 14:06:41 +0000201void acpi_create_madt(acpi_madt_t *madt)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000202{
Uwe Hermann622824c2010-11-19 15:14:42 +0000203 acpi_header_t *header = &(madt->header);
204 unsigned long current = (unsigned long)madt + sizeof(acpi_madt_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000205
Stefan Reinauer06feb882004-02-03 16:11:35 +0000206 memset((void *)madt, 0, sizeof(acpi_madt_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000207
Uwe Hermann622824c2010-11-19 15:14:42 +0000208 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000209 memcpy(header->signature, "APIC", 4);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000210 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000211 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000212 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000213
Stefan Reinauer06feb882004-02-03 16:11:35 +0000214 header->length = sizeof(acpi_madt_t);
Uwe Hermann622824c2010-11-19 15:14:42 +0000215 header->revision = 1; /* ACPI 1.0/2.0: 1, ACPI 3.0: 2, ACPI 4.0: 3 */
Stefan Reinauer06feb882004-02-03 16:11:35 +0000216
Uwe Hermann622824c2010-11-19 15:14:42 +0000217 madt->lapic_addr = LOCAL_APIC_ADDR;
218 madt->flags = 0x1; /* PCAT_COMPAT */
Stefan Reinauer06feb882004-02-03 16:11:35 +0000219
Stefan Reinauerf622d592005-11-26 16:56:05 +0000220 current = acpi_fill_madt(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000221
Uwe Hermann622824c2010-11-19 15:14:42 +0000222 /* (Re)calculate length and checksum. */
223 header->length = current - (unsigned long)madt;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000224
Uwe Hermann622824c2010-11-19 15:14:42 +0000225 header->checksum = acpi_checksum((void *)madt, header->length);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000226}
227
Uwe Hermann622824c2010-11-19 15:14:42 +0000228/* MCFG is defined in the PCI Firmware Specification 3.0. */
Rudolf Mareke6409f22007-11-03 12:50:26 +0000229void acpi_create_mcfg(acpi_mcfg_t *mcfg)
230{
Uwe Hermann622824c2010-11-19 15:14:42 +0000231 acpi_header_t *header = &(mcfg->header);
232 unsigned long current = (unsigned long)mcfg + sizeof(acpi_mcfg_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000233
Rudolf Mareke6409f22007-11-03 12:50:26 +0000234 memset((void *)mcfg, 0, sizeof(acpi_mcfg_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000235
Uwe Hermann622824c2010-11-19 15:14:42 +0000236 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000237 memcpy(header->signature, "MCFG", 4);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000238 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000239 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000240 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000241
Rudolf Mareke6409f22007-11-03 12:50:26 +0000242 header->length = sizeof(acpi_mcfg_t);
243 header->revision = 1;
244
245 current = acpi_fill_mcfg(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000246
Uwe Hermann622824c2010-11-19 15:14:42 +0000247 /* (Re)calculate length and checksum. */
248 header->length = current - (unsigned long)mcfg;
249 header->checksum = acpi_checksum((void *)mcfg, header->length);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000250}
251
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200252#if !IS_ENABLED(CONFIG_PER_DEVICE_ACPI_TABLES)
Uwe Hermann622824c2010-11-19 15:14:42 +0000253/*
Martin Roth7b5f8ef2013-07-08 16:22:10 -0600254 * This can be overridden by platform ACPI setup code, if it calls
Uwe Hermann622824c2010-11-19 15:14:42 +0000255 * acpi_create_ssdt_generator().
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000256 */
Uwe Hermann622824c2010-11-19 15:14:42 +0000257unsigned long __attribute__((weak)) acpi_fill_ssdt_generator(
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200258 unsigned long current, const char *oem_table_id)
Uwe Hermann622824c2010-11-19 15:14:42 +0000259{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000260 return current;
261}
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200262#endif
Rudolf Marek293b5f52009-02-01 18:35:15 +0000263
Myles Watson3fe6b702009-10-09 20:13:43 +0000264void acpi_create_ssdt_generator(acpi_header_t *ssdt, const char *oem_table_id)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000265{
Uwe Hermann622824c2010-11-19 15:14:42 +0000266 unsigned long current = (unsigned long)ssdt + sizeof(acpi_header_t);
267
Rudolf Marek293b5f52009-02-01 18:35:15 +0000268 memset((void *)ssdt, 0, sizeof(acpi_header_t));
Uwe Hermann622824c2010-11-19 15:14:42 +0000269
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000270 memcpy(&ssdt->signature, "SSDT", 4);
Uwe Hermann622824c2010-11-19 15:14:42 +0000271 ssdt->revision = 2; /* ACPI 1.0/2.0: ?, ACPI 3.0/4.0: 2 */
Rudolf Marek293b5f52009-02-01 18:35:15 +0000272 memcpy(&ssdt->oem_id, OEM_ID, 6);
273 memcpy(&ssdt->oem_table_id, oem_table_id, 8);
274 ssdt->oem_revision = 42;
Uwe Hermann622824c2010-11-19 15:14:42 +0000275 memcpy(&ssdt->asl_compiler_id, ASLC, 4);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000276 ssdt->asl_compiler_revision = 42;
277 ssdt->length = sizeof(acpi_header_t);
278
Stefan Reinauer8dcd50b2009-03-06 17:24:29 +0000279 acpigen_set_current((char *) current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200280 {
281#if IS_ENABLED(CONFIG_PER_DEVICE_ACPI_TABLES)
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100282 struct device *dev;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200283 for (dev = all_devices; dev; dev = dev->next)
284 if (dev->ops && dev->ops->acpi_fill_ssdt_generator) {
Vladimir Serbinenko0a669912014-10-05 14:34:17 +0200285 dev->ops->acpi_fill_ssdt_generator();
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200286 }
Vladimir Serbinenko0a669912014-10-05 14:34:17 +0200287 current = (unsigned long) acpigen_get_current();
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200288#else
289 current = acpi_fill_ssdt_generator(current, oem_table_id);
290#endif
291 }
Rudolf Marek293b5f52009-02-01 18:35:15 +0000292
Uwe Hermann622824c2010-11-19 15:14:42 +0000293 /* (Re)calculate length and checksum. */
Rudolf Marek293b5f52009-02-01 18:35:15 +0000294 ssdt->length = current - (unsigned long)ssdt;
295 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
296}
297
Stefan Reinauerf622d592005-11-26 16:56:05 +0000298int acpi_create_srat_lapic(acpi_srat_lapic_t *lapic, u8 node, u8 apic)
299{
Myles Watson0b4c9f02009-03-10 18:06:47 +0000300 memset((void *)lapic, 0, sizeof(acpi_srat_lapic_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000301
Uwe Hermann622824c2010-11-19 15:14:42 +0000302 lapic->type = 0; /* Processor local APIC/SAPIC affinity structure */
303 lapic->length = sizeof(acpi_srat_lapic_t);
304 lapic->flags = (1 << 0); /* Enabled (the use of this structure). */
305 lapic->proximity_domain_7_0 = node;
306 /* TODO: proximity_domain_31_8, local SAPIC EID, clock domain. */
307 lapic->apic_id = apic;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000308
Uwe Hermann622824c2010-11-19 15:14:42 +0000309 return lapic->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000310}
311
Uwe Hermann622824c2010-11-19 15:14:42 +0000312int 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 +0300313 u32 flags)
Stefan Reinauerf622d592005-11-26 16:56:05 +0000314{
Uwe Hermann622824c2010-11-19 15:14:42 +0000315 mem->type = 1; /* Memory affinity structure */
316 mem->length = sizeof(acpi_srat_mem_t);
317 mem->base_address_low = (basek << 10);
318 mem->base_address_high = (basek >> (32 - 10));
319 mem->length_low = (sizek << 10);
320 mem->length_high = (sizek >> (32 - 10));
321 mem->proximity_domain = node;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000322 mem->flags = flags;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000323
Uwe Hermann622824c2010-11-19 15:14:42 +0000324 return mem->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000325}
326
Uwe Hermann622824c2010-11-19 15:14:42 +0000327/* http://www.microsoft.com/whdc/system/sysinternals/sratdwn.mspx */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +0200328void acpi_create_srat(acpi_srat_t *srat,
329 unsigned long (*acpi_fill_srat)(unsigned long current))
Stefan Reinauerf622d592005-11-26 16:56:05 +0000330{
Uwe Hermann622824c2010-11-19 15:14:42 +0000331 acpi_header_t *header = &(srat->header);
332 unsigned long current = (unsigned long)srat + sizeof(acpi_srat_t);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000333
Uwe Hermann622824c2010-11-19 15:14:42 +0000334 memset((void *)srat, 0, sizeof(acpi_srat_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000335
Uwe Hermann622824c2010-11-19 15:14:42 +0000336 /* Fill out header fields. */
337 memcpy(header->signature, "SRAT", 4);
338 memcpy(header->oem_id, OEM_ID, 6);
339 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
340 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000341
Uwe Hermann622824c2010-11-19 15:14:42 +0000342 header->length = sizeof(acpi_srat_t);
343 header->revision = 1; /* ACPI 1.0: N/A, 2.0: 1, 3.0: 2, 4.0: 3 */
Stefan Reinauerf622d592005-11-26 16:56:05 +0000344
Uwe Hermann622824c2010-11-19 15:14:42 +0000345 srat->resv = 1; /* Spec: Reserved to 1 for backwards compatibility. */
Stefan Reinauerf622d592005-11-26 16:56:05 +0000346
Uwe Hermann622824c2010-11-19 15:14:42 +0000347 current = acpi_fill_srat(current);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000348
Uwe Hermann622824c2010-11-19 15:14:42 +0000349 /* (Re)calculate length and checksum. */
350 header->length = current - (unsigned long)srat;
351 header->checksum = acpi_checksum((void *)srat, header->length);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000352}
353
Vladimir Serbinenko8d70e942014-11-09 13:22:27 +0100354void acpi_create_dmar(acpi_dmar_t *dmar,
355 unsigned long (*acpi_fill_dmar) (unsigned long))
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200356{
357 acpi_header_t *header = &(dmar->header);
358 unsigned long current = (unsigned long)dmar + sizeof(acpi_dmar_t);
359
360 memset((void *)dmar, 0, sizeof(acpi_dmar_t));
361
362 /* Fill out header fields. */
363 memcpy(header->signature, "DMAR", 4);
364 memcpy(header->oem_id, OEM_ID, 6);
365 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
366 memcpy(header->asl_compiler_id, ASLC, 4);
367
368 header->length = sizeof(acpi_dmar_t);
369 header->revision = 1;
370
371 dmar->host_address_width = 40 - 1; /* FIXME: == MTRR size? */
372 dmar->flags = 0;
373
374 current = acpi_fill_dmar(current);
375
376 /* (Re)calculate length and checksum. */
377 header->length = current - (unsigned long)dmar;
378 header->checksum = acpi_checksum((void *)dmar, header->length);
379}
380
381unsigned long acpi_create_dmar_drhd(unsigned long current, u8 flags,
382 u16 segment, u32 bar)
383{
384 dmar_entry_t *drhd = (dmar_entry_t *)current;
385 memset(drhd, 0, sizeof(*drhd));
386 drhd->type = DMAR_DRHD;
387 drhd->length = sizeof(*drhd); /* will be fixed up later */
388 drhd->flags = flags;
389 drhd->segment = segment;
390 drhd->bar = bar;
391
392 return drhd->length;
393}
394
395void acpi_dmar_drhd_fixup(unsigned long base, unsigned long current)
396{
397 dmar_entry_t *drhd = (dmar_entry_t *)base;
398 drhd->length = current - base;
399}
400
401unsigned long acpi_create_dmar_drhd_ds_pci(unsigned long current, u8 segment,
402 u8 dev, u8 fn)
403{
404 dev_scope_t *ds = (dev_scope_t *)current;
405 memset(ds, 0, sizeof(*ds));
406 ds->type = SCOPE_PCI_ENDPOINT;
407 ds->length = sizeof(*ds) + 2; /* we don't support longer paths yet */
408 ds->start_bus = segment;
409 ds->path[0].dev = dev;
410 ds->path[0].fn = fn;
411
412 return ds->length;
413}
414
Uwe Hermann622824c2010-11-19 15:14:42 +0000415/* http://h21007.www2.hp.com/portal/download/files/unprot/Itanium/slit.pdf */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +0200416void acpi_create_slit(acpi_slit_t *slit,
417 unsigned long (*acpi_fill_slit)(unsigned long current))
Yinghai Lud4b278c2006-10-04 20:46:15 +0000418{
Uwe Hermann622824c2010-11-19 15:14:42 +0000419 acpi_header_t *header = &(slit->header);
420 unsigned long current = (unsigned long)slit + sizeof(acpi_slit_t);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000421
Uwe Hermann622824c2010-11-19 15:14:42 +0000422 memset((void *)slit, 0, sizeof(acpi_slit_t));
Yinghai Lud4b278c2006-10-04 20:46:15 +0000423
Uwe Hermann622824c2010-11-19 15:14:42 +0000424 /* Fill out header fields. */
425 memcpy(header->signature, "SLIT", 4);
426 memcpy(header->oem_id, OEM_ID, 6);
427 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
428 memcpy(header->asl_compiler_id, ASLC, 4);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000429
Uwe Hermann622824c2010-11-19 15:14:42 +0000430 header->length = sizeof(acpi_slit_t);
431 header->revision = 1; /* ACPI 1.0: N/A, ACPI 2.0/3.0/4.0: 1 */
Yinghai Lud4b278c2006-10-04 20:46:15 +0000432
Uwe Hermann622824c2010-11-19 15:14:42 +0000433 current = acpi_fill_slit(current);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000434
Uwe Hermann622824c2010-11-19 15:14:42 +0000435 /* (Re)calculate length and checksum. */
436 header->length = current - (unsigned long)slit;
437 header->checksum = acpi_checksum((void *)slit, header->length);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000438}
439
Uwe Hermann622824c2010-11-19 15:14:42 +0000440/* http://www.intel.com/hardwaredesign/hpetspec_1.pdf */
Stefan Reinauer777224c2005-01-19 14:06:41 +0000441void acpi_create_hpet(acpi_hpet_t *hpet)
Stefan Reinauer688b3852004-01-28 16:56:14 +0000442{
Uwe Hermann622824c2010-11-19 15:14:42 +0000443 acpi_header_t *header = &(hpet->header);
444 acpi_addr_t *addr = &(hpet->addr);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000445
Stefan Reinauera7648c22004-01-29 17:31:34 +0000446 memset((void *)hpet, 0, sizeof(acpi_hpet_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000447
Uwe Hermann622824c2010-11-19 15:14:42 +0000448 /* Fill out header fields. */
Stefan Reinauer4704dc52009-07-22 01:11:37 +0000449 memcpy(header->signature, "HPET", 4);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000450 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000451 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000452 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000453
Stefan Reinauer688b3852004-01-28 16:56:14 +0000454 header->length = sizeof(acpi_hpet_t);
Uwe Hermann622824c2010-11-19 15:14:42 +0000455 header->revision = 1; /* Currently 1. Table added in ACPI 2.0. */
Stefan Reinauer688b3852004-01-28 16:56:14 +0000456
Uwe Hermann622824c2010-11-19 15:14:42 +0000457 /* Fill out HPET address. */
458 addr->space_id = 0; /* Memory */
459 addr->bit_width = 64;
460 addr->bit_offset = 0;
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200461 addr->addrl = CONFIG_HPET_ADDRESS & 0xffffffff;
462 addr->addrh = ((unsigned long long)CONFIG_HPET_ADDRESS) >> 32;
Stefan Reinauer688b3852004-01-28 16:56:14 +0000463
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200464 hpet->id = *(unsigned int*)CONFIG_HPET_ADDRESS;
Uwe Hermann622824c2010-11-19 15:14:42 +0000465 hpet->number = 0;
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200466 hpet->min_tick = CONFIG_HPET_MIN_TICKS;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000467
Uwe Hermann622824c2010-11-19 15:14:42 +0000468 header->checksum = acpi_checksum((void *)hpet, sizeof(acpi_hpet_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000469}
Uwe Hermann622824c2010-11-19 15:14:42 +0000470
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200471unsigned long acpi_write_hpet(unsigned long current, acpi_rsdp_t *rsdp)
472{
473 acpi_hpet_t *hpet;
474
475 /*
476 * We explicitly add these tables later on:
477 */
478 printk(BIOS_DEBUG, "ACPI: * HPET\n");
479
480 hpet = (acpi_hpet_t *) current;
481 current += sizeof(acpi_hpet_t);
482 current = ALIGN(current, 16);
483 acpi_create_hpet(hpet);
484 acpi_add_table(rsdp, hpet);
485
486 return current;
487}
488
Stefan Reinauer777224c2005-01-19 14:06:41 +0000489void acpi_create_facs(acpi_facs_t *facs)
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000490{
Uwe Hermann622824c2010-11-19 15:14:42 +0000491 memset((void *)facs, 0, sizeof(acpi_facs_t));
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000492
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000493 memcpy(facs->signature, "FACS", 4);
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000494 facs->length = sizeof(acpi_facs_t);
495 facs->hardware_signature = 0;
496 facs->firmware_waking_vector = 0;
497 facs->global_lock = 0;
498 facs->flags = 0;
499 facs->x_firmware_waking_vector_l = 0;
500 facs->x_firmware_waking_vector_h = 0;
Uwe Hermann622824c2010-11-19 15:14:42 +0000501 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 +0000502}
Stefan Reinauer777224c2005-01-19 14:06:41 +0000503
504void acpi_write_rsdt(acpi_rsdt_t *rsdt)
Stefan Reinauer14e22772010-04-27 06:56:47 +0000505{
Uwe Hermann622824c2010-11-19 15:14:42 +0000506 acpi_header_t *header = &(rsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000507
Uwe Hermann622824c2010-11-19 15:14:42 +0000508 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000509 memcpy(header->signature, "RSDT", 4);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000510 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000511 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000512 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000513
Stefan Reinauer688b3852004-01-28 16:56:14 +0000514 header->length = sizeof(acpi_rsdt_t);
Uwe Hermann622824c2010-11-19 15:14:42 +0000515 header->revision = 1; /* ACPI 1.0/2.0/3.0/4.0: 1 */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000516
Uwe Hermann622824c2010-11-19 15:14:42 +0000517 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauer688b3852004-01-28 16:56:14 +0000518
Uwe Hermann622824c2010-11-19 15:14:42 +0000519 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000520 header->checksum = acpi_checksum((void *)rsdt, sizeof(acpi_rsdt_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000521}
522
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000523void acpi_write_xsdt(acpi_xsdt_t *xsdt)
Stefan Reinauer14e22772010-04-27 06:56:47 +0000524{
Uwe Hermann622824c2010-11-19 15:14:42 +0000525 acpi_header_t *header = &(xsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000526
Uwe Hermann622824c2010-11-19 15:14:42 +0000527 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000528 memcpy(header->signature, "XSDT", 4);
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000529 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000530 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000531 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000532
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000533 header->length = sizeof(acpi_xsdt_t);
Uwe Hermann622824c2010-11-19 15:14:42 +0000534 header->revision = 1; /* ACPI 1.0: N/A, 2.0/3.0/4.0: 1 */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000535
Uwe Hermann622824c2010-11-19 15:14:42 +0000536 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000537
Uwe Hermann622824c2010-11-19 15:14:42 +0000538 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000539 header->checksum = acpi_checksum((void *)xsdt, sizeof(acpi_xsdt_t));
540}
541
542void acpi_write_rsdp(acpi_rsdp_t *rsdp, acpi_rsdt_t *rsdt, acpi_xsdt_t *xsdt)
Stefan Reinauer688b3852004-01-28 16:56:14 +0000543{
Stefan Reinauerd18faac2009-11-05 18:06:43 +0000544 memset(rsdp, 0, sizeof(acpi_rsdp_t));
Uwe Hermann622824c2010-11-19 15:14:42 +0000545
Stefan Reinauer688b3852004-01-28 16:56:14 +0000546 memcpy(rsdp->signature, RSDP_SIG, 8);
547 memcpy(rsdp->oem_id, OEM_ID, 6);
Uwe Hermann622824c2010-11-19 15:14:42 +0000548
549 rsdp->length = sizeof(acpi_rsdp_t);
550 rsdp->rsdt_address = (u32)rsdt;
551
552 /*
553 * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2.
554 *
555 * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
556 * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
557 * revision 0).
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000558 */
559 if (xsdt == NULL) {
Uwe Hermann622824c2010-11-19 15:14:42 +0000560 rsdp->revision = 0;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000561 } else {
Uwe Hermann622824c2010-11-19 15:14:42 +0000562 rsdp->xsdt_address = (u64)(u32)xsdt;
563 rsdp->revision = 2;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000564 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000565
566 /* Calculate checksums. */
567 rsdp->checksum = acpi_checksum((void *)rsdp, 20);
568 rsdp->ext_checksum = acpi_checksum((void *)rsdp, sizeof(acpi_rsdp_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000569}
570
zbaocaf494c82012-04-13 13:57:14 +0800571unsigned long __attribute__((weak)) acpi_fill_hest(acpi_hest_t *hest)
572{
573 return (unsigned long)hest;
574}
575
576unsigned long acpi_create_hest_error_source(acpi_hest_t *hest, acpi_hest_esd_t *esd, u16 type, void *data, u16 data_len)
577{
578 acpi_header_t *header = &(hest->header);
579 acpi_hest_hen_t *hen;
580 void *pos;
581 u16 len;
582
583 pos = esd;
584 memset(pos, 0, sizeof(acpi_hest_esd_t));
585 len = 0;
586 esd->type = type; /* MCE */
587 esd->source_id = hest->error_source_count;
588 esd->flags = 0; /* FIRMWARE_FIRST */
589 esd->enabled = 1;
590 esd->prealloc_erecords = 1;
591 esd->max_section_per_record = 0x1;
592
593 len += sizeof(acpi_hest_esd_t);
594 pos = esd + 1;
595
596 switch (type) {
597 case 0: /* MCE */
598 break;
599 case 1: /* CMC */
600 hen = (acpi_hest_hen_t *) (pos);
601 memset(pos, 0, sizeof(acpi_hest_hen_t));
602 hen->type = 3; /* SCI? */
603 hen->length = sizeof(acpi_hest_hen_t);
604 hen->conf_we = 0; /* Configuration Write Enable. */
605 hen->poll_interval = 0;
606 hen->vector = 0;
607 hen->sw2poll_threshold_val = 0;
608 hen->sw2poll_threshold_win = 0;
609 hen->error_threshold_val = 0;
610 hen->error_threshold_win = 0;
611 len += sizeof(acpi_hest_hen_t);
612 pos = hen + 1;
613 break;
614 case 2: /* NMI */
615 case 6: /* AER Root Port */
616 case 7: /* AER Endpoint */
617 case 8: /* AER Bridge */
618 case 9: /* Generic Hardware Error Source. */
619 /* TODO: */
620 break;
621 default:
622 printk(BIOS_DEBUG, "Invalid type of Error Source.");
623 break;
624 }
625 hest->error_source_count ++;
626
627 memcpy(pos, data, data_len);
628 len += data_len;
629 header->length += len;
630
631 return len;
632}
633
634/* ACPI 4.0 */
635void acpi_write_hest(acpi_hest_t *hest)
636{
637 acpi_header_t *header = &(hest->header);
638
639 memset(hest, 0, sizeof(acpi_hest_t));
640
641 memcpy(header->signature, "HEST", 4);
642 memcpy(header->oem_id, OEM_ID, 6);
643 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
644 memcpy(header->asl_compiler_id, ASLC, 4);
645 header->length += sizeof(acpi_hest_t);
646 header->revision = 1;
647
648 acpi_fill_hest(hest);
649
650 /* Calculate checksums. */
651 header->checksum = acpi_checksum((void *)hest, header->length);
652}
653
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +0200654#if IS_ENABLED(CONFIG_COMMON_FADT)
655void acpi_create_fadt(acpi_fadt_t *fadt,acpi_facs_t *facs, void *dsdt)
656{
657 acpi_header_t *header = &(fadt->header);
658
659 memset((void *) fadt, 0, sizeof(acpi_fadt_t));
660 memcpy(header->signature, "FACP", 4);
661 header->length = sizeof(acpi_fadt_t);
Vladimir Serbinenkoab83ef02014-10-25 15:18:25 +0200662 header->revision = 4;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +0200663 memcpy(header->oem_id, OEM_ID, 6);
664 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
665 memcpy(header->asl_compiler_id, ASLC, 4);
666 header->asl_compiler_revision = 0;
667
668 fadt->firmware_ctrl = (unsigned long) facs;
669 fadt->dsdt = (unsigned long) dsdt;
670
671 fadt->x_firmware_ctl_l = (unsigned long)facs;
672 fadt->x_firmware_ctl_h = 0;
673 fadt->x_dsdt_l = (unsigned long)dsdt;
674 fadt->x_dsdt_h = 0;
675
676 if(IS_ENABLED(CONFIG_SYSTEM_TYPE_LAPTOP)) {
677 fadt->preferred_pm_profile = PM_MOBILE;
678 } else {
679 fadt->preferred_pm_profile = PM_DESKTOP;
680 }
681
Vladimir Serbinenkoab83ef02014-10-25 15:18:25 +0200682 acpi_fill_fadt(fadt);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +0200683
684 header->checksum =
685 acpi_checksum((void *) fadt, header->length);
686}
687#endif
688
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200689#if IS_ENABLED(CONFIG_PER_DEVICE_ACPI_TABLES)
690
691extern const unsigned char AmlCode[];
692
Vladimir Serbinenko41877d82014-09-01 22:18:01 +0200693unsigned long __attribute__ ((weak)) fw_cfg_acpi_tables(unsigned long start)
694{
695 return 0;
696}
697
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200698#define ALIGN_CURRENT current = (ALIGN(current, 16))
699unsigned long write_acpi_tables(unsigned long start)
700{
701 unsigned long current;
702 acpi_rsdp_t *rsdp;
703 acpi_rsdt_t *rsdt;
704 acpi_xsdt_t *xsdt;
705 acpi_fadt_t *fadt;
706 acpi_facs_t *facs;
707#if CONFIG_HAVE_ACPI_SLIC
708 acpi_header_t *slic;
709#endif
710 acpi_header_t *ssdt;
711 acpi_header_t *dsdt;
712 acpi_mcfg_t *mcfg;
713 acpi_madt_t *madt;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100714 struct device *dev;
Vladimir Serbinenko41877d82014-09-01 22:18:01 +0200715 unsigned long fw;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200716
717 current = start;
718
719 /* Align ACPI tables to 16byte */
720 ALIGN_CURRENT;
721
Vladimir Serbinenko41877d82014-09-01 22:18:01 +0200722 fw = fw_cfg_acpi_tables(current);
723 if (fw)
724 return fw;
725
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200726 printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx.\n", start);
727
728 /* We need at least an RSDP and an RSDT Table */
729 rsdp = (acpi_rsdp_t *) current;
730 current += sizeof(acpi_rsdp_t);
731 ALIGN_CURRENT;
732 rsdt = (acpi_rsdt_t *) current;
733 current += sizeof(acpi_rsdt_t);
734 ALIGN_CURRENT;
735 xsdt = (acpi_xsdt_t *) current;
736 current += sizeof(acpi_xsdt_t);
737 ALIGN_CURRENT;
738
739 /* clear all table memory */
740 memset((void *) start, 0, current - start);
741
742 acpi_write_rsdp(rsdp, rsdt, xsdt);
743 acpi_write_rsdt(rsdt);
744 acpi_write_xsdt(xsdt);
745
746 printk(BIOS_DEBUG, "ACPI: * FACS\n");
747 facs = (acpi_facs_t *) current;
748 current += sizeof(acpi_facs_t);
749 ALIGN_CURRENT;
750 acpi_create_facs(facs);
751
752 printk(BIOS_DEBUG, "ACPI: * DSDT\n");
753 dsdt = (acpi_header_t *) current;
754 memcpy(dsdt, &AmlCode, sizeof(acpi_header_t));
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +0200755 if (dsdt->length >= sizeof(acpi_header_t)) {
756 current += sizeof(acpi_header_t);
757
758 acpigen_set_current((char *) current);
759 for (dev = all_devices; dev; dev = dev->next)
760 if (dev->ops && dev->ops->acpi_inject_dsdt_generator) {
761 dev->ops->acpi_inject_dsdt_generator();
762 }
763 current = (unsigned long) acpigen_get_current();
764 memcpy((char *)current,
765 (char *)&AmlCode + sizeof(acpi_header_t),
766 dsdt->length - sizeof(acpi_header_t));
767 current += dsdt->length - sizeof(acpi_header_t);
768
769 /* (Re)calculate length and checksum. */
770 dsdt->length = current - (unsigned long)dsdt;
771 dsdt->checksum = 0;
772 dsdt->checksum = acpi_checksum((void *)dsdt, dsdt->length);
773 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200774
775 ALIGN_CURRENT;
776
777 printk(BIOS_DEBUG, "ACPI: * FADT\n");
778 fadt = (acpi_fadt_t *) current;
779 current += sizeof(acpi_fadt_t);
780 ALIGN_CURRENT;
781
782 acpi_create_fadt(fadt, facs, dsdt);
783 acpi_add_table(rsdp, fadt);
784
785#if CONFIG_HAVE_ACPI_SLIC
786 printk(BIOS_DEBUG, "ACPI: * SLIC\n");
787 slic = (acpi_header_t *)current;
788 current += acpi_create_slic(current);
789 ALIGN_CURRENT;
790 acpi_add_table(rsdp, slic);
791#endif
792
793 printk(BIOS_DEBUG, "ACPI: * SSDT\n");
794 ssdt = (acpi_header_t *)current;
795 acpi_create_ssdt_generator(ssdt, ACPI_TABLE_CREATOR);
Vladimir Serbinenko9310df82014-10-10 20:40:41 +0200796 if (ssdt->length > sizeof(acpi_header_t)) {
797 current += ssdt->length;
798 acpi_add_table(rsdp, ssdt);
799 ALIGN_CURRENT;
800 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200801
802 printk(BIOS_DEBUG, "ACPI: * MCFG\n");
803 mcfg = (acpi_mcfg_t *) current;
804 acpi_create_mcfg(mcfg);
805 if (mcfg->header.length > sizeof(acpi_mcfg_t)) {
806 current += mcfg->header.length;
807 ALIGN_CURRENT;
808 acpi_add_table(rsdp, mcfg);
809 }
810
811 printk(BIOS_DEBUG, "ACPI: * MADT\n");
812
813 madt = (acpi_madt_t *) current;
814 acpi_create_madt(madt);
815 if (madt->header.length > sizeof(acpi_madt_t)) {
816 current+=madt->header.length;
817 acpi_add_table(rsdp,madt);
818 }
819 ALIGN_CURRENT;
820
821 printk(BIOS_DEBUG, "current = %lx\n", current);
822
823 for (dev = all_devices; dev; dev = dev->next) {
824 if (dev->ops && dev->ops->write_acpi_tables) {
825 current = dev->ops->write_acpi_tables(current, rsdp);
826 ALIGN_CURRENT;
827 }
828 }
829
830 printk(BIOS_INFO, "ACPI: done.\n");
831 return current;
832}
833#endif
834
Patrick Georgie1667822012-05-05 15:29:32 +0200835#if CONFIG_HAVE_ACPI_RESUME
Kyösti Mälkki134f5042014-12-26 13:29:09 +0200836void __attribute__((weak)) mainboard_suspend_resume(void)
837{
838}
839
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500840void acpi_resume(void *wake_vec)
Stefan Reinauer7e9771c2009-04-22 08:17:38 +0000841{
Duncan Laurie11290c42012-10-03 19:07:05 -0700842#if CONFIG_HAVE_SMI_HANDLER
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500843 u32 *gnvs_address = cbmem_find(CBMEM_ID_ACPI_GNVS_PTR);
Duncan Laurie11290c42012-10-03 19:07:05 -0700844
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500845 /* Restore GNVS pointer in SMM if found */
846 if (gnvs_address && *gnvs_address) {
847 printk(BIOS_DEBUG, "Restore GNVS pointer to 0x%08x\n",
848 *gnvs_address);
849 smm_setup_structures((void *)*gnvs_address, NULL, NULL);
Stefan Reinauercb91e152012-04-03 23:28:22 +0200850 }
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500851#endif
852
853 /* Call mainboard resume handler first, if defined. */
Kyösti Mälkki134f5042014-12-26 13:29:09 +0200854 mainboard_suspend_resume();
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500855
856 post_code(POST_OS_RESUME);
857 acpi_jump_to_wakeup(wake_vec);
Stefan Reinauer7e9771c2009-04-22 08:17:38 +0000858}
859
Kyösti Mälkki7a846e72015-01-09 23:55:09 +0200860/* This is filled with acpi_is_wakeup() call early in ramstage. */
861int acpi_slp_type = -1;
Stefan Reinauer688b3852004-01-28 16:56:14 +0000862
Kyösti Mälkki9e94dbf2015-01-08 20:03:18 +0200863#if IS_ENABLED(CONFIG_EARLY_CBMEM_INIT)
864int acpi_get_sleep_type(void)
Kyösti Mälkki134f5042014-12-26 13:29:09 +0200865{
Kyösti Mälkki9e94dbf2015-01-08 20:03:18 +0200866 struct romstage_handoff *handoff;
867
868 handoff = cbmem_find(CBMEM_ID_ROMSTAGE_INFO);
869
870 if (handoff == NULL) {
871 printk(BIOS_DEBUG, "Unknown boot method, assuming normal.\n");
872 return 0;
873 } else if (handoff->s3_resume) {
874 printk(BIOS_DEBUG, "S3 Resume.\n");
875 return 3;
876 } else {
877 printk(BIOS_DEBUG, "Normal boot.\n");
878 return 0;
879 }
Kyösti Mälkki134f5042014-12-26 13:29:09 +0200880}
Kyösti Mälkki9e94dbf2015-01-08 20:03:18 +0200881#endif
Kyösti Mälkki134f5042014-12-26 13:29:09 +0200882
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +0300883static void acpi_handoff_wakeup(void)
884{
Kyösti Mälkki7a846e72015-01-09 23:55:09 +0200885 if (acpi_slp_type < 0)
886 acpi_slp_type = acpi_get_sleep_type();
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +0300887}
888
Kyösti Mälkkicb28f3f2014-01-03 15:15:22 +0200889int acpi_is_wakeup(void)
Rudolf Marek33cafe52009-04-13 18:07:02 +0000890{
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +0300891 acpi_handoff_wakeup();
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +0100892 /* Both resume from S2 and resume from S3 restart at CPU reset */
893 return (acpi_slp_type == 3 || acpi_slp_type == 2);
Rudolf Marek33cafe52009-04-13 18:07:02 +0000894}
895
Kyösti Mälkki4d9b7722014-06-19 19:45:40 +0300896int acpi_is_wakeup_s3(void)
897{
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +0300898 acpi_handoff_wakeup();
Kyösti Mälkki4d9b7722014-06-19 19:45:40 +0300899 return (acpi_slp_type == 3);
900}
901
Kyösti Mälkkicb28f3f2014-01-03 15:15:22 +0200902void acpi_fail_wakeup(void)
903{
904 if (acpi_slp_type == 3 || acpi_slp_type == 2)
905 acpi_slp_type = 0;
906}
907
Kyösti Mälkki7b14f082014-10-16 20:58:47 +0300908void acpi_prepare_resume_backup(void)
909{
910 if (!acpi_s3_resume_allowed())
911 return;
912
913 /* Let's prepare the ACPI S3 Resume area now already, so we can rely on
914 * it being there during reboot time. We don't need the pointer, nor
915 * the result right now. If it fails, ACPI resume will be disabled.
916 */
917
918 if (HIGH_MEMORY_SAVE)
919 cbmem_add(CBMEM_ID_RESUME, HIGH_MEMORY_SAVE);
920
921 if (HIGH_MEMORY_SCRATCH)
922 cbmem_add(CBMEM_ID_RESUME_SCRATCH, HIGH_MEMORY_SCRATCH);
923}
924
Rudolf Marek33cafe52009-04-13 18:07:02 +0000925static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp)
926{
927 if (strncmp((char *)rsdp, RSDP_SIG, sizeof(RSDP_SIG) - 1) != 0)
928 return NULL;
929
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000930 printk(BIOS_DEBUG, "Looking on %p for valid checksum\n", rsdp);
Rudolf Marek33cafe52009-04-13 18:07:02 +0000931
932 if (acpi_checksum((void *)rsdp, 20) != 0)
933 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000934 printk(BIOS_DEBUG, "Checksum 1 passed\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +0000935
936 if ((rsdp->revision > 1) && (acpi_checksum((void *)rsdp,
937 rsdp->length) != 0))
938 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000939 printk(BIOS_DEBUG, "Checksum 2 passed all OK\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +0000940
941 return rsdp;
942}
943
944static acpi_rsdp_t *rsdp;
945
946void *acpi_get_wakeup_rsdp(void)
947{
948 return rsdp;
949}
950
951void *acpi_find_wakeup_vector(void)
952{
953 char *p, *end;
Rudolf Marek33cafe52009-04-13 18:07:02 +0000954 acpi_rsdt_t *rsdt;
955 acpi_facs_t *facs;
Martin Rothc5f49262012-12-14 19:17:55 -0700956 acpi_fadt_t *fadt = NULL;
Uwe Hermann622824c2010-11-19 15:14:42 +0000957 void *wake_vec;
Rudolf Marek33cafe52009-04-13 18:07:02 +0000958 int i;
959
960 rsdp = NULL;
961
962 if (!acpi_is_wakeup())
963 return NULL;
964
Uwe Hermann622824c2010-11-19 15:14:42 +0000965 printk(BIOS_DEBUG, "Trying to find the wakeup vector...\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +0000966
Uwe Hermann622824c2010-11-19 15:14:42 +0000967 /* Find RSDP. */
968 for (p = (char *)0xe0000; p < (char *)0xfffff; p += 16) {
969 if ((rsdp = valid_rsdp((acpi_rsdp_t *)p)))
Rudolf Marek33cafe52009-04-13 18:07:02 +0000970 break;
971 }
972
973 if (rsdp == NULL)
974 return NULL;
975
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000976 printk(BIOS_DEBUG, "RSDP found at %p\n", rsdp);
Rudolf Marek33cafe52009-04-13 18:07:02 +0000977 rsdt = (acpi_rsdt_t *) rsdp->rsdt_address;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000978
Uwe Hermann622824c2010-11-19 15:14:42 +0000979 end = (char *)rsdt + rsdt->header.length;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000980 printk(BIOS_DEBUG, "RSDT found at %p ends at %p\n", rsdt, end);
Rudolf Marek33cafe52009-04-13 18:07:02 +0000981
Uwe Hermann622824c2010-11-19 15:14:42 +0000982 for (i = 0; ((char *)&rsdt->entry[i]) < end; i++) {
983 fadt = (acpi_fadt_t *)rsdt->entry[i];
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000984 if (strncmp((char *)fadt, "FACP", 4) == 0)
Rudolf Marek33cafe52009-04-13 18:07:02 +0000985 break;
986 fadt = NULL;
987 }
988
989 if (fadt == NULL)
990 return NULL;
991
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000992 printk(BIOS_DEBUG, "FADT found at %p\n", fadt);
Stefan Reinauer69390db2009-05-26 12:33:52 +0000993 facs = (acpi_facs_t *)fadt->firmware_ctrl;
Rudolf Marek33cafe52009-04-13 18:07:02 +0000994
Stefan Reinauer7e9771c2009-04-22 08:17:38 +0000995 if (facs == NULL) {
Uwe Hermann622824c2010-11-19 15:14:42 +0000996 printk(BIOS_DEBUG, "No FACS found, wake up from S3 not "
997 "possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +0000998 return NULL;
Stefan Reinauer7e9771c2009-04-22 08:17:38 +0000999 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001000
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001001 printk(BIOS_DEBUG, "FACS found at %p\n", facs);
Uwe Hermann622824c2010-11-19 15:14:42 +00001002 wake_vec = (void *)facs->firmware_waking_vector;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001003 printk(BIOS_DEBUG, "OS waking vector is %p\n", wake_vec);
Uwe Hermann622824c2010-11-19 15:14:42 +00001004
Rudolf Marek33cafe52009-04-13 18:07:02 +00001005 return wake_vec;
1006}
1007
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +01001008#if CONFIG_SMP
Rudolf Marek33cafe52009-04-13 18:07:02 +00001009extern char *lowmem_backup;
1010extern char *lowmem_backup_ptr;
1011extern int lowmem_backup_size;
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +01001012#endif
Rudolf Marek33cafe52009-04-13 18:07:02 +00001013
Uwe Hermann622824c2010-11-19 15:14:42 +00001014#define WAKEUP_BASE 0x600
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001015
Uwe Hermann622824c2010-11-19 15:14:42 +00001016void (*acpi_do_wakeup)(u32 vector, u32 backup_source, u32 backup_target,
Stefan Reinauer399486e2012-12-06 13:54:29 -08001017 u32 backup_size) asmlinkage = (void *)WAKEUP_BASE;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001018
Aaron Durbina146d582013-02-08 16:56:51 -06001019extern unsigned char __wakeup;
1020extern unsigned int __wakeup_size;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001021
Rudolf Marek33cafe52009-04-13 18:07:02 +00001022void acpi_jump_to_wakeup(void *vector)
1023{
Aaron Durbin8e4a3552013-02-08 17:28:04 -06001024 u32 acpi_backup_memory = 0;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001025
Kyösti Mälkki2ca2afe2014-06-17 15:41:37 +03001026 if (HIGH_MEMORY_SAVE && acpi_s3_resume_allowed()) {
1027 acpi_backup_memory = (u32)cbmem_find(CBMEM_ID_RESUME);
1028
1029 if (!acpi_backup_memory) {
1030 printk(BIOS_WARNING, "ACPI: Backup memory missing. "
1031 "No S3 resume.\n");
1032 return;
1033 }
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001034 }
1035
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +01001036#if CONFIG_SMP
Martin Roth7b5f8ef2013-07-08 16:22:10 -06001037 // FIXME: This should go into the ACPI backup memory, too. No pork sausages.
Uwe Hermann622824c2010-11-19 15:14:42 +00001038 /*
1039 * Just restore the SMP trampoline and continue with wakeup on
1040 * assembly level.
1041 */
Rudolf Marek33cafe52009-04-13 18:07:02 +00001042 memcpy(lowmem_backup_ptr, lowmem_backup, lowmem_backup_size);
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +01001043#endif
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001044
Uwe Hermann622824c2010-11-19 15:14:42 +00001045 /* Copy wakeup trampoline in place. */
Aaron Durbina146d582013-02-08 16:56:51 -06001046 memcpy((void *)WAKEUP_BASE, &__wakeup, __wakeup_size);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001047
Duncan Lauriecde78012011-10-19 15:32:39 -07001048#if CONFIG_COLLECT_TIMESTAMPS
1049 timestamp_add_now(TS_ACPI_WAKE_JUMP);
1050#endif
1051
Uwe Hermann622824c2010-11-19 15:14:42 +00001052 acpi_do_wakeup((u32)vector, acpi_backup_memory, CONFIG_RAMBASE,
1053 HIGH_MEMORY_SAVE);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001054}
1055#endif
Duncan Laurie11290c42012-10-03 19:07:05 -07001056
1057void acpi_save_gnvs(u32 gnvs_address)
1058{
Duncan Laurie9c07c8f2013-03-22 11:08:39 -07001059 u32 *gnvs = cbmem_add(CBMEM_ID_ACPI_GNVS_PTR, sizeof(*gnvs));
Duncan Laurie11290c42012-10-03 19:07:05 -07001060 if (gnvs)
1061 *gnvs = gnvs_address;
1062}