blob: fb902dbfa2f419f33606e181b744183e7eb0d866 [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 */
Stefan Reinauerf622d592005-11-26 16:56:05 +0000328void acpi_create_srat(acpi_srat_t *srat)
329{
Uwe Hermann622824c2010-11-19 15:14:42 +0000330 acpi_header_t *header = &(srat->header);
331 unsigned long current = (unsigned long)srat + sizeof(acpi_srat_t);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000332
Uwe Hermann622824c2010-11-19 15:14:42 +0000333 memset((void *)srat, 0, sizeof(acpi_srat_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000334
Uwe Hermann622824c2010-11-19 15:14:42 +0000335 /* Fill out header fields. */
336 memcpy(header->signature, "SRAT", 4);
337 memcpy(header->oem_id, OEM_ID, 6);
338 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
339 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000340
Uwe Hermann622824c2010-11-19 15:14:42 +0000341 header->length = sizeof(acpi_srat_t);
342 header->revision = 1; /* ACPI 1.0: N/A, 2.0: 1, 3.0: 2, 4.0: 3 */
Stefan Reinauerf622d592005-11-26 16:56:05 +0000343
Uwe Hermann622824c2010-11-19 15:14:42 +0000344 srat->resv = 1; /* Spec: Reserved to 1 for backwards compatibility. */
Stefan Reinauerf622d592005-11-26 16:56:05 +0000345
Uwe Hermann622824c2010-11-19 15:14:42 +0000346 current = acpi_fill_srat(current);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000347
Uwe Hermann622824c2010-11-19 15:14:42 +0000348 /* (Re)calculate length and checksum. */
349 header->length = current - (unsigned long)srat;
350 header->checksum = acpi_checksum((void *)srat, header->length);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000351}
352
Vladimir Serbinenko8d70e942014-11-09 13:22:27 +0100353void acpi_create_dmar(acpi_dmar_t *dmar,
354 unsigned long (*acpi_fill_dmar) (unsigned long))
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200355{
356 acpi_header_t *header = &(dmar->header);
357 unsigned long current = (unsigned long)dmar + sizeof(acpi_dmar_t);
358
359 memset((void *)dmar, 0, sizeof(acpi_dmar_t));
360
361 /* Fill out header fields. */
362 memcpy(header->signature, "DMAR", 4);
363 memcpy(header->oem_id, OEM_ID, 6);
364 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
365 memcpy(header->asl_compiler_id, ASLC, 4);
366
367 header->length = sizeof(acpi_dmar_t);
368 header->revision = 1;
369
370 dmar->host_address_width = 40 - 1; /* FIXME: == MTRR size? */
371 dmar->flags = 0;
372
373 current = acpi_fill_dmar(current);
374
375 /* (Re)calculate length and checksum. */
376 header->length = current - (unsigned long)dmar;
377 header->checksum = acpi_checksum((void *)dmar, header->length);
378}
379
380unsigned long acpi_create_dmar_drhd(unsigned long current, u8 flags,
381 u16 segment, u32 bar)
382{
383 dmar_entry_t *drhd = (dmar_entry_t *)current;
384 memset(drhd, 0, sizeof(*drhd));
385 drhd->type = DMAR_DRHD;
386 drhd->length = sizeof(*drhd); /* will be fixed up later */
387 drhd->flags = flags;
388 drhd->segment = segment;
389 drhd->bar = bar;
390
391 return drhd->length;
392}
393
394void acpi_dmar_drhd_fixup(unsigned long base, unsigned long current)
395{
396 dmar_entry_t *drhd = (dmar_entry_t *)base;
397 drhd->length = current - base;
398}
399
400unsigned long acpi_create_dmar_drhd_ds_pci(unsigned long current, u8 segment,
401 u8 dev, u8 fn)
402{
403 dev_scope_t *ds = (dev_scope_t *)current;
404 memset(ds, 0, sizeof(*ds));
405 ds->type = SCOPE_PCI_ENDPOINT;
406 ds->length = sizeof(*ds) + 2; /* we don't support longer paths yet */
407 ds->start_bus = segment;
408 ds->path[0].dev = dev;
409 ds->path[0].fn = fn;
410
411 return ds->length;
412}
413
Uwe Hermann622824c2010-11-19 15:14:42 +0000414/* http://h21007.www2.hp.com/portal/download/files/unprot/Itanium/slit.pdf */
Yinghai Lud4b278c2006-10-04 20:46:15 +0000415void acpi_create_slit(acpi_slit_t *slit)
416{
Uwe Hermann622824c2010-11-19 15:14:42 +0000417 acpi_header_t *header = &(slit->header);
418 unsigned long current = (unsigned long)slit + sizeof(acpi_slit_t);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000419
Uwe Hermann622824c2010-11-19 15:14:42 +0000420 memset((void *)slit, 0, sizeof(acpi_slit_t));
Yinghai Lud4b278c2006-10-04 20:46:15 +0000421
Uwe Hermann622824c2010-11-19 15:14:42 +0000422 /* Fill out header fields. */
423 memcpy(header->signature, "SLIT", 4);
424 memcpy(header->oem_id, OEM_ID, 6);
425 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
426 memcpy(header->asl_compiler_id, ASLC, 4);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000427
Uwe Hermann622824c2010-11-19 15:14:42 +0000428 header->length = sizeof(acpi_slit_t);
429 header->revision = 1; /* ACPI 1.0: N/A, ACPI 2.0/3.0/4.0: 1 */
Yinghai Lud4b278c2006-10-04 20:46:15 +0000430
Uwe Hermann622824c2010-11-19 15:14:42 +0000431 current = acpi_fill_slit(current);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000432
Uwe Hermann622824c2010-11-19 15:14:42 +0000433 /* (Re)calculate length and checksum. */
434 header->length = current - (unsigned long)slit;
435 header->checksum = acpi_checksum((void *)slit, header->length);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000436}
437
Uwe Hermann622824c2010-11-19 15:14:42 +0000438/* http://www.intel.com/hardwaredesign/hpetspec_1.pdf */
Stefan Reinauer777224c2005-01-19 14:06:41 +0000439void acpi_create_hpet(acpi_hpet_t *hpet)
Stefan Reinauer688b3852004-01-28 16:56:14 +0000440{
Uwe Hermann622824c2010-11-19 15:14:42 +0000441 acpi_header_t *header = &(hpet->header);
442 acpi_addr_t *addr = &(hpet->addr);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000443
Stefan Reinauera7648c22004-01-29 17:31:34 +0000444 memset((void *)hpet, 0, sizeof(acpi_hpet_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000445
Uwe Hermann622824c2010-11-19 15:14:42 +0000446 /* Fill out header fields. */
Stefan Reinauer4704dc52009-07-22 01:11:37 +0000447 memcpy(header->signature, "HPET", 4);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000448 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000449 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000450 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000451
Stefan Reinauer688b3852004-01-28 16:56:14 +0000452 header->length = sizeof(acpi_hpet_t);
Uwe Hermann622824c2010-11-19 15:14:42 +0000453 header->revision = 1; /* Currently 1. Table added in ACPI 2.0. */
Stefan Reinauer688b3852004-01-28 16:56:14 +0000454
Uwe Hermann622824c2010-11-19 15:14:42 +0000455 /* Fill out HPET address. */
456 addr->space_id = 0; /* Memory */
457 addr->bit_width = 64;
458 addr->bit_offset = 0;
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200459 addr->addrl = CONFIG_HPET_ADDRESS & 0xffffffff;
460 addr->addrh = ((unsigned long long)CONFIG_HPET_ADDRESS) >> 32;
Stefan Reinauer688b3852004-01-28 16:56:14 +0000461
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200462 hpet->id = *(unsigned int*)CONFIG_HPET_ADDRESS;
Uwe Hermann622824c2010-11-19 15:14:42 +0000463 hpet->number = 0;
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200464 hpet->min_tick = CONFIG_HPET_MIN_TICKS;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000465
Uwe Hermann622824c2010-11-19 15:14:42 +0000466 header->checksum = acpi_checksum((void *)hpet, sizeof(acpi_hpet_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000467}
Uwe Hermann622824c2010-11-19 15:14:42 +0000468
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200469unsigned long acpi_write_hpet(unsigned long current, acpi_rsdp_t *rsdp)
470{
471 acpi_hpet_t *hpet;
472
473 /*
474 * We explicitly add these tables later on:
475 */
476 printk(BIOS_DEBUG, "ACPI: * HPET\n");
477
478 hpet = (acpi_hpet_t *) current;
479 current += sizeof(acpi_hpet_t);
480 current = ALIGN(current, 16);
481 acpi_create_hpet(hpet);
482 acpi_add_table(rsdp, hpet);
483
484 return current;
485}
486
Stefan Reinauer777224c2005-01-19 14:06:41 +0000487void acpi_create_facs(acpi_facs_t *facs)
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000488{
Uwe Hermann622824c2010-11-19 15:14:42 +0000489 memset((void *)facs, 0, sizeof(acpi_facs_t));
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000490
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000491 memcpy(facs->signature, "FACS", 4);
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000492 facs->length = sizeof(acpi_facs_t);
493 facs->hardware_signature = 0;
494 facs->firmware_waking_vector = 0;
495 facs->global_lock = 0;
496 facs->flags = 0;
497 facs->x_firmware_waking_vector_l = 0;
498 facs->x_firmware_waking_vector_h = 0;
Uwe Hermann622824c2010-11-19 15:14:42 +0000499 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 +0000500}
Stefan Reinauer777224c2005-01-19 14:06:41 +0000501
502void acpi_write_rsdt(acpi_rsdt_t *rsdt)
Stefan Reinauer14e22772010-04-27 06:56:47 +0000503{
Uwe Hermann622824c2010-11-19 15:14:42 +0000504 acpi_header_t *header = &(rsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000505
Uwe Hermann622824c2010-11-19 15:14:42 +0000506 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000507 memcpy(header->signature, "RSDT", 4);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000508 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000509 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000510 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000511
Stefan Reinauer688b3852004-01-28 16:56:14 +0000512 header->length = sizeof(acpi_rsdt_t);
Uwe Hermann622824c2010-11-19 15:14:42 +0000513 header->revision = 1; /* ACPI 1.0/2.0/3.0/4.0: 1 */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000514
Uwe Hermann622824c2010-11-19 15:14:42 +0000515 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauer688b3852004-01-28 16:56:14 +0000516
Uwe Hermann622824c2010-11-19 15:14:42 +0000517 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000518 header->checksum = acpi_checksum((void *)rsdt, sizeof(acpi_rsdt_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000519}
520
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000521void acpi_write_xsdt(acpi_xsdt_t *xsdt)
Stefan Reinauer14e22772010-04-27 06:56:47 +0000522{
Uwe Hermann622824c2010-11-19 15:14:42 +0000523 acpi_header_t *header = &(xsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000524
Uwe Hermann622824c2010-11-19 15:14:42 +0000525 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000526 memcpy(header->signature, "XSDT", 4);
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000527 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000528 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000529 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000530
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000531 header->length = sizeof(acpi_xsdt_t);
Uwe Hermann622824c2010-11-19 15:14:42 +0000532 header->revision = 1; /* ACPI 1.0: N/A, 2.0/3.0/4.0: 1 */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000533
Uwe Hermann622824c2010-11-19 15:14:42 +0000534 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000535
Uwe Hermann622824c2010-11-19 15:14:42 +0000536 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000537 header->checksum = acpi_checksum((void *)xsdt, sizeof(acpi_xsdt_t));
538}
539
540void acpi_write_rsdp(acpi_rsdp_t *rsdp, acpi_rsdt_t *rsdt, acpi_xsdt_t *xsdt)
Stefan Reinauer688b3852004-01-28 16:56:14 +0000541{
Stefan Reinauerd18faac2009-11-05 18:06:43 +0000542 memset(rsdp, 0, sizeof(acpi_rsdp_t));
Uwe Hermann622824c2010-11-19 15:14:42 +0000543
Stefan Reinauer688b3852004-01-28 16:56:14 +0000544 memcpy(rsdp->signature, RSDP_SIG, 8);
545 memcpy(rsdp->oem_id, OEM_ID, 6);
Uwe Hermann622824c2010-11-19 15:14:42 +0000546
547 rsdp->length = sizeof(acpi_rsdp_t);
548 rsdp->rsdt_address = (u32)rsdt;
549
550 /*
551 * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2.
552 *
553 * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
554 * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
555 * revision 0).
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000556 */
557 if (xsdt == NULL) {
Uwe Hermann622824c2010-11-19 15:14:42 +0000558 rsdp->revision = 0;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000559 } else {
Uwe Hermann622824c2010-11-19 15:14:42 +0000560 rsdp->xsdt_address = (u64)(u32)xsdt;
561 rsdp->revision = 2;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000562 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000563
564 /* Calculate checksums. */
565 rsdp->checksum = acpi_checksum((void *)rsdp, 20);
566 rsdp->ext_checksum = acpi_checksum((void *)rsdp, sizeof(acpi_rsdp_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000567}
568
zbaocaf494c82012-04-13 13:57:14 +0800569unsigned long __attribute__((weak)) acpi_fill_hest(acpi_hest_t *hest)
570{
571 return (unsigned long)hest;
572}
573
574unsigned long acpi_create_hest_error_source(acpi_hest_t *hest, acpi_hest_esd_t *esd, u16 type, void *data, u16 data_len)
575{
576 acpi_header_t *header = &(hest->header);
577 acpi_hest_hen_t *hen;
578 void *pos;
579 u16 len;
580
581 pos = esd;
582 memset(pos, 0, sizeof(acpi_hest_esd_t));
583 len = 0;
584 esd->type = type; /* MCE */
585 esd->source_id = hest->error_source_count;
586 esd->flags = 0; /* FIRMWARE_FIRST */
587 esd->enabled = 1;
588 esd->prealloc_erecords = 1;
589 esd->max_section_per_record = 0x1;
590
591 len += sizeof(acpi_hest_esd_t);
592 pos = esd + 1;
593
594 switch (type) {
595 case 0: /* MCE */
596 break;
597 case 1: /* CMC */
598 hen = (acpi_hest_hen_t *) (pos);
599 memset(pos, 0, sizeof(acpi_hest_hen_t));
600 hen->type = 3; /* SCI? */
601 hen->length = sizeof(acpi_hest_hen_t);
602 hen->conf_we = 0; /* Configuration Write Enable. */
603 hen->poll_interval = 0;
604 hen->vector = 0;
605 hen->sw2poll_threshold_val = 0;
606 hen->sw2poll_threshold_win = 0;
607 hen->error_threshold_val = 0;
608 hen->error_threshold_win = 0;
609 len += sizeof(acpi_hest_hen_t);
610 pos = hen + 1;
611 break;
612 case 2: /* NMI */
613 case 6: /* AER Root Port */
614 case 7: /* AER Endpoint */
615 case 8: /* AER Bridge */
616 case 9: /* Generic Hardware Error Source. */
617 /* TODO: */
618 break;
619 default:
620 printk(BIOS_DEBUG, "Invalid type of Error Source.");
621 break;
622 }
623 hest->error_source_count ++;
624
625 memcpy(pos, data, data_len);
626 len += data_len;
627 header->length += len;
628
629 return len;
630}
631
632/* ACPI 4.0 */
633void acpi_write_hest(acpi_hest_t *hest)
634{
635 acpi_header_t *header = &(hest->header);
636
637 memset(hest, 0, sizeof(acpi_hest_t));
638
639 memcpy(header->signature, "HEST", 4);
640 memcpy(header->oem_id, OEM_ID, 6);
641 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
642 memcpy(header->asl_compiler_id, ASLC, 4);
643 header->length += sizeof(acpi_hest_t);
644 header->revision = 1;
645
646 acpi_fill_hest(hest);
647
648 /* Calculate checksums. */
649 header->checksum = acpi_checksum((void *)hest, header->length);
650}
651
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +0200652#if IS_ENABLED(CONFIG_COMMON_FADT)
653void acpi_create_fadt(acpi_fadt_t *fadt,acpi_facs_t *facs, void *dsdt)
654{
655 acpi_header_t *header = &(fadt->header);
656
657 memset((void *) fadt, 0, sizeof(acpi_fadt_t));
658 memcpy(header->signature, "FACP", 4);
659 header->length = sizeof(acpi_fadt_t);
Vladimir Serbinenkoab83ef02014-10-25 15:18:25 +0200660 header->revision = 4;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +0200661 memcpy(header->oem_id, OEM_ID, 6);
662 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
663 memcpy(header->asl_compiler_id, ASLC, 4);
664 header->asl_compiler_revision = 0;
665
666 fadt->firmware_ctrl = (unsigned long) facs;
667 fadt->dsdt = (unsigned long) dsdt;
668
669 fadt->x_firmware_ctl_l = (unsigned long)facs;
670 fadt->x_firmware_ctl_h = 0;
671 fadt->x_dsdt_l = (unsigned long)dsdt;
672 fadt->x_dsdt_h = 0;
673
674 if(IS_ENABLED(CONFIG_SYSTEM_TYPE_LAPTOP)) {
675 fadt->preferred_pm_profile = PM_MOBILE;
676 } else {
677 fadt->preferred_pm_profile = PM_DESKTOP;
678 }
679
Vladimir Serbinenkoab83ef02014-10-25 15:18:25 +0200680 acpi_fill_fadt(fadt);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +0200681
682 header->checksum =
683 acpi_checksum((void *) fadt, header->length);
684}
685#endif
686
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200687#if IS_ENABLED(CONFIG_PER_DEVICE_ACPI_TABLES)
688
689extern const unsigned char AmlCode[];
690
Vladimir Serbinenko41877d82014-09-01 22:18:01 +0200691unsigned long __attribute__ ((weak)) fw_cfg_acpi_tables(unsigned long start)
692{
693 return 0;
694}
695
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200696#define ALIGN_CURRENT current = (ALIGN(current, 16))
697unsigned long write_acpi_tables(unsigned long start)
698{
699 unsigned long current;
700 acpi_rsdp_t *rsdp;
701 acpi_rsdt_t *rsdt;
702 acpi_xsdt_t *xsdt;
703 acpi_fadt_t *fadt;
704 acpi_facs_t *facs;
705#if CONFIG_HAVE_ACPI_SLIC
706 acpi_header_t *slic;
707#endif
708 acpi_header_t *ssdt;
709 acpi_header_t *dsdt;
710 acpi_mcfg_t *mcfg;
711 acpi_madt_t *madt;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100712 struct device *dev;
Vladimir Serbinenko41877d82014-09-01 22:18:01 +0200713 unsigned long fw;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200714
715 current = start;
716
717 /* Align ACPI tables to 16byte */
718 ALIGN_CURRENT;
719
Vladimir Serbinenko41877d82014-09-01 22:18:01 +0200720 fw = fw_cfg_acpi_tables(current);
721 if (fw)
722 return fw;
723
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200724 printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx.\n", start);
725
726 /* We need at least an RSDP and an RSDT Table */
727 rsdp = (acpi_rsdp_t *) current;
728 current += sizeof(acpi_rsdp_t);
729 ALIGN_CURRENT;
730 rsdt = (acpi_rsdt_t *) current;
731 current += sizeof(acpi_rsdt_t);
732 ALIGN_CURRENT;
733 xsdt = (acpi_xsdt_t *) current;
734 current += sizeof(acpi_xsdt_t);
735 ALIGN_CURRENT;
736
737 /* clear all table memory */
738 memset((void *) start, 0, current - start);
739
740 acpi_write_rsdp(rsdp, rsdt, xsdt);
741 acpi_write_rsdt(rsdt);
742 acpi_write_xsdt(xsdt);
743
744 printk(BIOS_DEBUG, "ACPI: * FACS\n");
745 facs = (acpi_facs_t *) current;
746 current += sizeof(acpi_facs_t);
747 ALIGN_CURRENT;
748 acpi_create_facs(facs);
749
750 printk(BIOS_DEBUG, "ACPI: * DSDT\n");
751 dsdt = (acpi_header_t *) current;
752 memcpy(dsdt, &AmlCode, sizeof(acpi_header_t));
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +0200753 if (dsdt->length >= sizeof(acpi_header_t)) {
754 current += sizeof(acpi_header_t);
755
756 acpigen_set_current((char *) current);
757 for (dev = all_devices; dev; dev = dev->next)
758 if (dev->ops && dev->ops->acpi_inject_dsdt_generator) {
759 dev->ops->acpi_inject_dsdt_generator();
760 }
761 current = (unsigned long) acpigen_get_current();
762 memcpy((char *)current,
763 (char *)&AmlCode + sizeof(acpi_header_t),
764 dsdt->length - sizeof(acpi_header_t));
765 current += dsdt->length - sizeof(acpi_header_t);
766
767 /* (Re)calculate length and checksum. */
768 dsdt->length = current - (unsigned long)dsdt;
769 dsdt->checksum = 0;
770 dsdt->checksum = acpi_checksum((void *)dsdt, dsdt->length);
771 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200772
773 ALIGN_CURRENT;
774
775 printk(BIOS_DEBUG, "ACPI: * FADT\n");
776 fadt = (acpi_fadt_t *) current;
777 current += sizeof(acpi_fadt_t);
778 ALIGN_CURRENT;
779
780 acpi_create_fadt(fadt, facs, dsdt);
781 acpi_add_table(rsdp, fadt);
782
783#if CONFIG_HAVE_ACPI_SLIC
784 printk(BIOS_DEBUG, "ACPI: * SLIC\n");
785 slic = (acpi_header_t *)current;
786 current += acpi_create_slic(current);
787 ALIGN_CURRENT;
788 acpi_add_table(rsdp, slic);
789#endif
790
791 printk(BIOS_DEBUG, "ACPI: * SSDT\n");
792 ssdt = (acpi_header_t *)current;
793 acpi_create_ssdt_generator(ssdt, ACPI_TABLE_CREATOR);
Vladimir Serbinenko9310df82014-10-10 20:40:41 +0200794 if (ssdt->length > sizeof(acpi_header_t)) {
795 current += ssdt->length;
796 acpi_add_table(rsdp, ssdt);
797 ALIGN_CURRENT;
798 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200799
800 printk(BIOS_DEBUG, "ACPI: * MCFG\n");
801 mcfg = (acpi_mcfg_t *) current;
802 acpi_create_mcfg(mcfg);
803 if (mcfg->header.length > sizeof(acpi_mcfg_t)) {
804 current += mcfg->header.length;
805 ALIGN_CURRENT;
806 acpi_add_table(rsdp, mcfg);
807 }
808
809 printk(BIOS_DEBUG, "ACPI: * MADT\n");
810
811 madt = (acpi_madt_t *) current;
812 acpi_create_madt(madt);
813 if (madt->header.length > sizeof(acpi_madt_t)) {
814 current+=madt->header.length;
815 acpi_add_table(rsdp,madt);
816 }
817 ALIGN_CURRENT;
818
819 printk(BIOS_DEBUG, "current = %lx\n", current);
820
821 for (dev = all_devices; dev; dev = dev->next) {
822 if (dev->ops && dev->ops->write_acpi_tables) {
823 current = dev->ops->write_acpi_tables(current, rsdp);
824 ALIGN_CURRENT;
825 }
826 }
827
828 printk(BIOS_INFO, "ACPI: done.\n");
829 return current;
830}
831#endif
832
Patrick Georgie1667822012-05-05 15:29:32 +0200833#if CONFIG_HAVE_ACPI_RESUME
Kyösti Mälkki134f5042014-12-26 13:29:09 +0200834void __attribute__((weak)) mainboard_suspend_resume(void)
835{
836}
837
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500838void acpi_resume(void *wake_vec)
Stefan Reinauer7e9771c2009-04-22 08:17:38 +0000839{
Duncan Laurie11290c42012-10-03 19:07:05 -0700840#if CONFIG_HAVE_SMI_HANDLER
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500841 u32 *gnvs_address = cbmem_find(CBMEM_ID_ACPI_GNVS_PTR);
Duncan Laurie11290c42012-10-03 19:07:05 -0700842
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500843 /* Restore GNVS pointer in SMM if found */
844 if (gnvs_address && *gnvs_address) {
845 printk(BIOS_DEBUG, "Restore GNVS pointer to 0x%08x\n",
846 *gnvs_address);
847 smm_setup_structures((void *)*gnvs_address, NULL, NULL);
Stefan Reinauercb91e152012-04-03 23:28:22 +0200848 }
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500849#endif
850
851 /* Call mainboard resume handler first, if defined. */
Kyösti Mälkki134f5042014-12-26 13:29:09 +0200852 mainboard_suspend_resume();
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500853
854 post_code(POST_OS_RESUME);
855 acpi_jump_to_wakeup(wake_vec);
Stefan Reinauer7e9771c2009-04-22 08:17:38 +0000856}
857
Kyösti Mälkki7a846e72015-01-09 23:55:09 +0200858/* This is filled with acpi_is_wakeup() call early in ramstage. */
859int acpi_slp_type = -1;
Stefan Reinauer688b3852004-01-28 16:56:14 +0000860
Kyösti Mälkki9e94dbf2015-01-08 20:03:18 +0200861#if IS_ENABLED(CONFIG_EARLY_CBMEM_INIT)
862int acpi_get_sleep_type(void)
Kyösti Mälkki134f5042014-12-26 13:29:09 +0200863{
Kyösti Mälkki9e94dbf2015-01-08 20:03:18 +0200864 struct romstage_handoff *handoff;
865
866 handoff = cbmem_find(CBMEM_ID_ROMSTAGE_INFO);
867
868 if (handoff == NULL) {
869 printk(BIOS_DEBUG, "Unknown boot method, assuming normal.\n");
870 return 0;
871 } else if (handoff->s3_resume) {
872 printk(BIOS_DEBUG, "S3 Resume.\n");
873 return 3;
874 } else {
875 printk(BIOS_DEBUG, "Normal boot.\n");
876 return 0;
877 }
Kyösti Mälkki134f5042014-12-26 13:29:09 +0200878}
Kyösti Mälkki9e94dbf2015-01-08 20:03:18 +0200879#endif
Kyösti Mälkki134f5042014-12-26 13:29:09 +0200880
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +0300881static void acpi_handoff_wakeup(void)
882{
Kyösti Mälkki7a846e72015-01-09 23:55:09 +0200883 if (acpi_slp_type < 0)
884 acpi_slp_type = acpi_get_sleep_type();
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +0300885}
886
Kyösti Mälkkicb28f3f2014-01-03 15:15:22 +0200887int acpi_is_wakeup(void)
Rudolf Marek33cafe52009-04-13 18:07:02 +0000888{
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +0300889 acpi_handoff_wakeup();
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +0100890 /* Both resume from S2 and resume from S3 restart at CPU reset */
891 return (acpi_slp_type == 3 || acpi_slp_type == 2);
Rudolf Marek33cafe52009-04-13 18:07:02 +0000892}
893
Kyösti Mälkki4d9b7722014-06-19 19:45:40 +0300894int acpi_is_wakeup_s3(void)
895{
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +0300896 acpi_handoff_wakeup();
Kyösti Mälkki4d9b7722014-06-19 19:45:40 +0300897 return (acpi_slp_type == 3);
898}
899
Kyösti Mälkkicb28f3f2014-01-03 15:15:22 +0200900void acpi_fail_wakeup(void)
901{
902 if (acpi_slp_type == 3 || acpi_slp_type == 2)
903 acpi_slp_type = 0;
904}
905
Kyösti Mälkki7b14f082014-10-16 20:58:47 +0300906void acpi_prepare_resume_backup(void)
907{
908 if (!acpi_s3_resume_allowed())
909 return;
910
911 /* Let's prepare the ACPI S3 Resume area now already, so we can rely on
912 * it being there during reboot time. We don't need the pointer, nor
913 * the result right now. If it fails, ACPI resume will be disabled.
914 */
915
916 if (HIGH_MEMORY_SAVE)
917 cbmem_add(CBMEM_ID_RESUME, HIGH_MEMORY_SAVE);
918
919 if (HIGH_MEMORY_SCRATCH)
920 cbmem_add(CBMEM_ID_RESUME_SCRATCH, HIGH_MEMORY_SCRATCH);
921}
922
Rudolf Marek33cafe52009-04-13 18:07:02 +0000923static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp)
924{
925 if (strncmp((char *)rsdp, RSDP_SIG, sizeof(RSDP_SIG) - 1) != 0)
926 return NULL;
927
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000928 printk(BIOS_DEBUG, "Looking on %p for valid checksum\n", rsdp);
Rudolf Marek33cafe52009-04-13 18:07:02 +0000929
930 if (acpi_checksum((void *)rsdp, 20) != 0)
931 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000932 printk(BIOS_DEBUG, "Checksum 1 passed\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +0000933
934 if ((rsdp->revision > 1) && (acpi_checksum((void *)rsdp,
935 rsdp->length) != 0))
936 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000937 printk(BIOS_DEBUG, "Checksum 2 passed all OK\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +0000938
939 return rsdp;
940}
941
942static acpi_rsdp_t *rsdp;
943
944void *acpi_get_wakeup_rsdp(void)
945{
946 return rsdp;
947}
948
949void *acpi_find_wakeup_vector(void)
950{
951 char *p, *end;
Rudolf Marek33cafe52009-04-13 18:07:02 +0000952 acpi_rsdt_t *rsdt;
953 acpi_facs_t *facs;
Martin Rothc5f49262012-12-14 19:17:55 -0700954 acpi_fadt_t *fadt = NULL;
Uwe Hermann622824c2010-11-19 15:14:42 +0000955 void *wake_vec;
Rudolf Marek33cafe52009-04-13 18:07:02 +0000956 int i;
957
958 rsdp = NULL;
959
960 if (!acpi_is_wakeup())
961 return NULL;
962
Uwe Hermann622824c2010-11-19 15:14:42 +0000963 printk(BIOS_DEBUG, "Trying to find the wakeup vector...\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +0000964
Uwe Hermann622824c2010-11-19 15:14:42 +0000965 /* Find RSDP. */
966 for (p = (char *)0xe0000; p < (char *)0xfffff; p += 16) {
967 if ((rsdp = valid_rsdp((acpi_rsdp_t *)p)))
Rudolf Marek33cafe52009-04-13 18:07:02 +0000968 break;
969 }
970
971 if (rsdp == NULL)
972 return NULL;
973
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000974 printk(BIOS_DEBUG, "RSDP found at %p\n", rsdp);
Rudolf Marek33cafe52009-04-13 18:07:02 +0000975 rsdt = (acpi_rsdt_t *) rsdp->rsdt_address;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000976
Uwe Hermann622824c2010-11-19 15:14:42 +0000977 end = (char *)rsdt + rsdt->header.length;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000978 printk(BIOS_DEBUG, "RSDT found at %p ends at %p\n", rsdt, end);
Rudolf Marek33cafe52009-04-13 18:07:02 +0000979
Uwe Hermann622824c2010-11-19 15:14:42 +0000980 for (i = 0; ((char *)&rsdt->entry[i]) < end; i++) {
981 fadt = (acpi_fadt_t *)rsdt->entry[i];
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000982 if (strncmp((char *)fadt, "FACP", 4) == 0)
Rudolf Marek33cafe52009-04-13 18:07:02 +0000983 break;
984 fadt = NULL;
985 }
986
987 if (fadt == NULL)
988 return NULL;
989
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000990 printk(BIOS_DEBUG, "FADT found at %p\n", fadt);
Stefan Reinauer69390db2009-05-26 12:33:52 +0000991 facs = (acpi_facs_t *)fadt->firmware_ctrl;
Rudolf Marek33cafe52009-04-13 18:07:02 +0000992
Stefan Reinauer7e9771c2009-04-22 08:17:38 +0000993 if (facs == NULL) {
Uwe Hermann622824c2010-11-19 15:14:42 +0000994 printk(BIOS_DEBUG, "No FACS found, wake up from S3 not "
995 "possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +0000996 return NULL;
Stefan Reinauer7e9771c2009-04-22 08:17:38 +0000997 }
Rudolf Marek33cafe52009-04-13 18:07:02 +0000998
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000999 printk(BIOS_DEBUG, "FACS found at %p\n", facs);
Uwe Hermann622824c2010-11-19 15:14:42 +00001000 wake_vec = (void *)facs->firmware_waking_vector;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001001 printk(BIOS_DEBUG, "OS waking vector is %p\n", wake_vec);
Uwe Hermann622824c2010-11-19 15:14:42 +00001002
Rudolf Marek33cafe52009-04-13 18:07:02 +00001003 return wake_vec;
1004}
1005
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +01001006#if CONFIG_SMP
Rudolf Marek33cafe52009-04-13 18:07:02 +00001007extern char *lowmem_backup;
1008extern char *lowmem_backup_ptr;
1009extern int lowmem_backup_size;
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +01001010#endif
Rudolf Marek33cafe52009-04-13 18:07:02 +00001011
Uwe Hermann622824c2010-11-19 15:14:42 +00001012#define WAKEUP_BASE 0x600
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001013
Uwe Hermann622824c2010-11-19 15:14:42 +00001014void (*acpi_do_wakeup)(u32 vector, u32 backup_source, u32 backup_target,
Stefan Reinauer399486e2012-12-06 13:54:29 -08001015 u32 backup_size) asmlinkage = (void *)WAKEUP_BASE;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001016
Aaron Durbina146d582013-02-08 16:56:51 -06001017extern unsigned char __wakeup;
1018extern unsigned int __wakeup_size;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001019
Rudolf Marek33cafe52009-04-13 18:07:02 +00001020void acpi_jump_to_wakeup(void *vector)
1021{
Aaron Durbin8e4a3552013-02-08 17:28:04 -06001022 u32 acpi_backup_memory = 0;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001023
Kyösti Mälkki2ca2afe2014-06-17 15:41:37 +03001024 if (HIGH_MEMORY_SAVE && acpi_s3_resume_allowed()) {
1025 acpi_backup_memory = (u32)cbmem_find(CBMEM_ID_RESUME);
1026
1027 if (!acpi_backup_memory) {
1028 printk(BIOS_WARNING, "ACPI: Backup memory missing. "
1029 "No S3 resume.\n");
1030 return;
1031 }
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001032 }
1033
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +01001034#if CONFIG_SMP
Martin Roth7b5f8ef2013-07-08 16:22:10 -06001035 // FIXME: This should go into the ACPI backup memory, too. No pork sausages.
Uwe Hermann622824c2010-11-19 15:14:42 +00001036 /*
1037 * Just restore the SMP trampoline and continue with wakeup on
1038 * assembly level.
1039 */
Rudolf Marek33cafe52009-04-13 18:07:02 +00001040 memcpy(lowmem_backup_ptr, lowmem_backup, lowmem_backup_size);
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +01001041#endif
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001042
Uwe Hermann622824c2010-11-19 15:14:42 +00001043 /* Copy wakeup trampoline in place. */
Aaron Durbina146d582013-02-08 16:56:51 -06001044 memcpy((void *)WAKEUP_BASE, &__wakeup, __wakeup_size);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001045
Duncan Lauriecde78012011-10-19 15:32:39 -07001046#if CONFIG_COLLECT_TIMESTAMPS
1047 timestamp_add_now(TS_ACPI_WAKE_JUMP);
1048#endif
1049
Uwe Hermann622824c2010-11-19 15:14:42 +00001050 acpi_do_wakeup((u32)vector, acpi_backup_memory, CONFIG_RAMBASE,
1051 HIGH_MEMORY_SAVE);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001052}
1053#endif
Duncan Laurie11290c42012-10-03 19:07:05 -07001054
1055void acpi_save_gnvs(u32 gnvs_address)
1056{
Duncan Laurie9c07c8f2013-03-22 11:08:39 -07001057 u32 *gnvs = cbmem_add(CBMEM_ID_ACPI_GNVS_PTR, sizeof(*gnvs));
Duncan Laurie11290c42012-10-03 19:07:05 -07001058 if (gnvs)
1059 *gnvs = gnvs_address;
1060}