blob: 5abc17849b98b93a3151f6073f17d21937e1abff [file] [log] [blame]
Stefan Reinauer688b3852004-01-28 16:56:14 +00001/*
Uwe Hermannc70e9fc2010-02-15 23:10:19 +00002 * This file is part of the coreboot project.
3 *
Stefan Reinauerf8ee1802008-01-18 15:08:58 +00004 * coreboot ACPI Table support
Stefan Reinauer688b3852004-01-28 16:56:14 +00005 * written by Stefan Reinauer <stepan@openbios.org>
Uwe Hermannc70e9fc2010-02-15 23:10:19 +00006 *
7 * Copyright (C) 2004 SUSE LINUX AG
8 * Copyright (C) 2005-2009 coresystems GmbH
Stefan Reinauer777224c2005-01-19 14:06:41 +00009 *
Stefan Reinauer14e22772010-04-27 06:56:47 +000010 * ACPI FADT, FACS, and DSDT table support added by
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000011 * Nick Barker <nick.barker9@btinternet.com>, and those portions
Uwe Hermannc70e9fc2010-02-15 23:10:19 +000012 * Copyright (C) 2004 Nick Barker
Stefan Reinauerf622d592005-11-26 16:56:05 +000013 *
Uwe Hermannc70e9fc2010-02-15 23:10:19 +000014 * Copyright (C) 2005 ADVANCED MICRO DEVICES, INC. All Rights Reserved.
Stefan Reinauerd6edf7a2006-01-05 00:19:52 +000015 * 2005.9 yhlu add SRAT table generation
Stefan Reinauer777224c2005-01-19 14:06:41 +000016 */
17
Stefan Reinauer14e22772010-04-27 06:56:47 +000018/*
Stefan Reinauer777224c2005-01-19 14:06:41 +000019 * Each system port implementing ACPI has to provide two functions:
Stefan Reinauer14e22772010-04-27 06:56:47 +000020 *
Stefan Reinauer777224c2005-01-19 14:06:41 +000021 * write_acpi_tables()
22 * acpi_dump_apics()
Stefan Reinauer14e22772010-04-27 06:56:47 +000023 *
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000024 * See Kontron 986LCD-M port for a good example of an ACPI implementation
25 * in coreboot.
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000026 */
Stefan Reinauer688b3852004-01-28 16:56:14 +000027
28#include <console/console.h>
29#include <string.h>
30#include <arch/acpi.h>
Rudolf Marek293b5f52009-02-01 18:35:15 +000031#include <arch/acpigen.h>
Stefan Reinauer777224c2005-01-19 14:06:41 +000032#include <device/pci.h>
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000033#include <cbmem.h>
Patrick Georgic8feedd2012-02-16 18:43:25 +010034#include <cpu/x86/lapic_def.h>
Duncan Laurie11290c42012-10-03 19:07:05 -070035#include <cpu/cpu.h>
Duncan Lauriecde78012011-10-19 15:32:39 -070036#if CONFIG_COLLECT_TIMESTAMPS
37#include <timestamp.h>
38#endif
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000039
Patrick Georgi9aeb6942012-10-05 21:54:38 +020040/* FIXME: Kconfig doesn't support overridable defaults :-( */
41#ifndef CONFIG_HPET_MIN_TICKS
42#define CONFIG_HPET_MIN_TICKS 0x1000
43#endif
44
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000045u8 acpi_checksum(u8 *table, u32 length)
Stefan Reinauer688b3852004-01-28 16:56:14 +000046{
Uwe Hermann622824c2010-11-19 15:14:42 +000047 u8 ret = 0;
Stefan Reinauer688b3852004-01-28 16:56:14 +000048 while (length--) {
49 ret += *table;
50 table++;
51 }
Stefan Reinauera7648c22004-01-29 17:31:34 +000052 return -ret;
Stefan Reinauer688b3852004-01-28 16:56:14 +000053}
54
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000055/**
Uwe Hermann622824c2010-11-19 15:14:42 +000056 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
57 * and checksum.
Stefan Reinauer06feb882004-02-03 16:11:35 +000058 */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000059void acpi_add_table(acpi_rsdp_t *rsdp, void *table)
Stefan Reinauer688b3852004-01-28 16:56:14 +000060{
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000061 int i, entries_num;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000062 acpi_rsdt_t *rsdt;
63 acpi_xsdt_t *xsdt = NULL;
64
Uwe Hermann622824c2010-11-19 15:14:42 +000065 /* The RSDT is mandatory... */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000066 rsdt = (acpi_rsdt_t *)rsdp->rsdt_address;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000067
Uwe Hermann622824c2010-11-19 15:14:42 +000068 /* ...while the XSDT is not. */
69 if (rsdp->xsdt_address)
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000070 xsdt = (acpi_xsdt_t *)((u32)rsdp->xsdt_address);
Stefan Reinauer14e22772010-04-27 06:56:47 +000071
Uwe Hermann622824c2010-11-19 15:14:42 +000072 /* This should always be MAX_ACPI_TABLES. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000073 entries_num = ARRAY_SIZE(rsdt->entry);
Stefan Reinauer14e22772010-04-27 06:56:47 +000074
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000075 for (i = 0; i < entries_num; i++) {
Uwe Hermann622824c2010-11-19 15:14:42 +000076 if (rsdt->entry[i] == 0)
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000077 break;
Stefan Reinauer688b3852004-01-28 16:56:14 +000078 }
79
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000080 if (i >= entries_num) {
Uwe Hermann622824c2010-11-19 15:14:42 +000081 printk(BIOS_ERR, "ACPI: Error: Could not add ACPI table, "
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +030082 "too many tables.\n");
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000083 return;
84 }
85
Uwe Hermann622824c2010-11-19 15:14:42 +000086 /* Add table to the RSDT. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000087 rsdt->entry[i] = (u32)table;
88
Uwe Hermann622824c2010-11-19 15:14:42 +000089 /* Fix RSDT length or the kernel will assume invalid entries. */
90 rsdt->header.length = sizeof(acpi_header_t) + (sizeof(u32) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000091
Uwe Hermann622824c2010-11-19 15:14:42 +000092 /* Re-calculate checksum. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000093 rsdt->header.checksum = 0; /* Hope this won't get optimized away */
Uwe Hermann622824c2010-11-19 15:14:42 +000094 rsdt->header.checksum = acpi_checksum((u8 *)rsdt, rsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000095
Uwe Hermann622824c2010-11-19 15:14:42 +000096 /*
97 * And now the same thing for the XSDT. We use the same index as for
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000098 * now we want the XSDT and RSDT to always be in sync in coreboot.
99 */
100 if (xsdt) {
Uwe Hermann622824c2010-11-19 15:14:42 +0000101 /* Add table to the XSDT. */
102 xsdt->entry[i] = (u64)(u32)table;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000103
Uwe Hermann622824c2010-11-19 15:14:42 +0000104 /* Fix XSDT length. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000105 xsdt->header.length = sizeof(acpi_header_t) +
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300106 (sizeof(u64) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000107
Uwe Hermann622824c2010-11-19 15:14:42 +0000108 /* Re-calculate checksum. */
109 xsdt->header.checksum = 0;
110 xsdt->header.checksum = acpi_checksum((u8 *)xsdt,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300111 xsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000112 }
113
Uwe Hermann622824c2010-11-19 15:14:42 +0000114 printk(BIOS_DEBUG, "ACPI: added table %d/%d, length now %d\n",
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300115 i + 1, entries_num, rsdt->header.length);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000116}
117
Uwe Hermann622824c2010-11-19 15:14:42 +0000118int acpi_create_mcfg_mmconfig(acpi_mcfg_mmconfig_t *mmconfig, u32 base,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300119 u16 seg_nr, u8 start, u8 end)
Stefan Reinauer26d431a2009-01-20 19:17:51 +0000120{
Vladimir Serbinenko60fccdc2014-10-05 11:05:16 +0200121 memset(mmconfig, 0, sizeof(*mmconfig));
Rudolf Mareke6409f22007-11-03 12:50:26 +0000122 mmconfig->base_address = base;
123 mmconfig->base_reserved = 0;
124 mmconfig->pci_segment_group_number = seg_nr;
125 mmconfig->start_bus_number = start;
126 mmconfig->end_bus_number = end;
Uwe Hermann622824c2010-11-19 15:14:42 +0000127
128 return sizeof(acpi_mcfg_mmconfig_t);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000129}
130
Stefan Reinauer777224c2005-01-19 14:06:41 +0000131int acpi_create_madt_lapic(acpi_madt_lapic_t *lapic, u8 cpu, u8 apic)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000132{
Uwe Hermann622824c2010-11-19 15:14:42 +0000133 lapic->type = 0; /* Local APIC structure */
134 lapic->length = sizeof(acpi_madt_lapic_t);
135 lapic->flags = (1 << 0); /* Processor/LAPIC enabled */
136 lapic->processor_id = cpu;
137 lapic->apic_id = apic;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000138
Uwe Hermann622824c2010-11-19 15:14:42 +0000139 return lapic->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000140}
141
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000142unsigned long acpi_create_madt_lapics(unsigned long current)
143{
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100144 struct device *cpu;
Duncan Laurie11290c42012-10-03 19:07:05 -0700145 int index = 0;
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000146
Uwe Hermann622824c2010-11-19 15:14:42 +0000147 for (cpu = all_devices; cpu; cpu = cpu->next) {
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000148 if ((cpu->path.type != DEVICE_PATH_APIC) ||
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800149 (cpu->bus->dev->path.type != DEVICE_PATH_CPU_CLUSTER)) {
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000150 continue;
151 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000152 if (!cpu->enabled)
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000153 continue;
Uwe Hermann622824c2010-11-19 15:14:42 +0000154 current += acpi_create_madt_lapic((acpi_madt_lapic_t *)current,
Duncan Laurie11290c42012-10-03 19:07:05 -0700155 index, cpu->path.apic.apic_id);
156 index++;
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000157 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000158
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000159 return current;
160}
161
Uwe Hermann622824c2010-11-19 15:14:42 +0000162int acpi_create_madt_ioapic(acpi_madt_ioapic_t *ioapic, u8 id, u32 addr,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300163 u32 gsi_base)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000164{
Uwe Hermann622824c2010-11-19 15:14:42 +0000165 ioapic->type = 1; /* I/O APIC structure */
166 ioapic->length = sizeof(acpi_madt_ioapic_t);
167 ioapic->reserved = 0x00;
168 ioapic->gsi_base = gsi_base;
169 ioapic->ioapic_id = id;
170 ioapic->ioapic_addr = addr;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000171
Uwe Hermann622824c2010-11-19 15:14:42 +0000172 return ioapic->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000173}
174
Stefan Reinauer777224c2005-01-19 14:06:41 +0000175int acpi_create_madt_irqoverride(acpi_madt_irqoverride_t *irqoverride,
Stefan Reinauer06feb882004-02-03 16:11:35 +0000176 u8 bus, u8 source, u32 gsirq, u16 flags)
177{
Uwe Hermann622824c2010-11-19 15:14:42 +0000178 irqoverride->type = 2; /* Interrupt source override */
179 irqoverride->length = sizeof(acpi_madt_irqoverride_t);
180 irqoverride->bus = bus;
181 irqoverride->source = source;
182 irqoverride->gsirq = gsirq;
183 irqoverride->flags = flags;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000184
Uwe Hermann622824c2010-11-19 15:14:42 +0000185 return irqoverride->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000186}
187
Stefan Reinauer777224c2005-01-19 14:06:41 +0000188int acpi_create_madt_lapic_nmi(acpi_madt_lapic_nmi_t *lapic_nmi, u8 cpu,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300189 u16 flags, u8 lint)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000190{
Uwe Hermann622824c2010-11-19 15:14:42 +0000191 lapic_nmi->type = 4; /* Local APIC NMI structure */
192 lapic_nmi->length = sizeof(acpi_madt_lapic_nmi_t);
193 lapic_nmi->flags = flags;
194 lapic_nmi->processor_id = cpu;
195 lapic_nmi->lint = lint;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000196
Uwe Hermann622824c2010-11-19 15:14:42 +0000197 return lapic_nmi->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000198}
199
Stefan Reinauer777224c2005-01-19 14:06:41 +0000200void acpi_create_madt(acpi_madt_t *madt)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000201{
Uwe Hermann622824c2010-11-19 15:14:42 +0000202 acpi_header_t *header = &(madt->header);
203 unsigned long current = (unsigned long)madt + sizeof(acpi_madt_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000204
Stefan Reinauer06feb882004-02-03 16:11:35 +0000205 memset((void *)madt, 0, sizeof(acpi_madt_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000206
Uwe Hermann622824c2010-11-19 15:14:42 +0000207 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000208 memcpy(header->signature, "APIC", 4);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000209 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000210 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000211 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000212
Stefan Reinauer06feb882004-02-03 16:11:35 +0000213 header->length = sizeof(acpi_madt_t);
Uwe Hermann622824c2010-11-19 15:14:42 +0000214 header->revision = 1; /* ACPI 1.0/2.0: 1, ACPI 3.0: 2, ACPI 4.0: 3 */
Stefan Reinauer06feb882004-02-03 16:11:35 +0000215
Uwe Hermann622824c2010-11-19 15:14:42 +0000216 madt->lapic_addr = LOCAL_APIC_ADDR;
217 madt->flags = 0x1; /* PCAT_COMPAT */
Stefan Reinauer06feb882004-02-03 16:11:35 +0000218
Stefan Reinauerf622d592005-11-26 16:56:05 +0000219 current = acpi_fill_madt(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000220
Uwe Hermann622824c2010-11-19 15:14:42 +0000221 /* (Re)calculate length and checksum. */
222 header->length = current - (unsigned long)madt;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000223
Uwe Hermann622824c2010-11-19 15:14:42 +0000224 header->checksum = acpi_checksum((void *)madt, header->length);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000225}
226
Uwe Hermann622824c2010-11-19 15:14:42 +0000227/* MCFG is defined in the PCI Firmware Specification 3.0. */
Rudolf Mareke6409f22007-11-03 12:50:26 +0000228void acpi_create_mcfg(acpi_mcfg_t *mcfg)
229{
Uwe Hermann622824c2010-11-19 15:14:42 +0000230 acpi_header_t *header = &(mcfg->header);
231 unsigned long current = (unsigned long)mcfg + sizeof(acpi_mcfg_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000232
Rudolf Mareke6409f22007-11-03 12:50:26 +0000233 memset((void *)mcfg, 0, sizeof(acpi_mcfg_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000234
Uwe Hermann622824c2010-11-19 15:14:42 +0000235 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000236 memcpy(header->signature, "MCFG", 4);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000237 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000238 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000239 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000240
Rudolf Mareke6409f22007-11-03 12:50:26 +0000241 header->length = sizeof(acpi_mcfg_t);
242 header->revision = 1;
243
244 current = acpi_fill_mcfg(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000245
Uwe Hermann622824c2010-11-19 15:14:42 +0000246 /* (Re)calculate length and checksum. */
247 header->length = current - (unsigned long)mcfg;
248 header->checksum = acpi_checksum((void *)mcfg, header->length);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000249}
250
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200251#if !IS_ENABLED(CONFIG_PER_DEVICE_ACPI_TABLES)
Uwe Hermann622824c2010-11-19 15:14:42 +0000252/*
Martin Roth7b5f8ef2013-07-08 16:22:10 -0600253 * This can be overridden by platform ACPI setup code, if it calls
Uwe Hermann622824c2010-11-19 15:14:42 +0000254 * acpi_create_ssdt_generator().
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000255 */
Uwe Hermann622824c2010-11-19 15:14:42 +0000256unsigned long __attribute__((weak)) acpi_fill_ssdt_generator(
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200257 unsigned long current, const char *oem_table_id)
Uwe Hermann622824c2010-11-19 15:14:42 +0000258{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000259 return current;
260}
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200261#endif
Rudolf Marek293b5f52009-02-01 18:35:15 +0000262
Myles Watson3fe6b702009-10-09 20:13:43 +0000263void acpi_create_ssdt_generator(acpi_header_t *ssdt, const char *oem_table_id)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000264{
Uwe Hermann622824c2010-11-19 15:14:42 +0000265 unsigned long current = (unsigned long)ssdt + sizeof(acpi_header_t);
266
Rudolf Marek293b5f52009-02-01 18:35:15 +0000267 memset((void *)ssdt, 0, sizeof(acpi_header_t));
Uwe Hermann622824c2010-11-19 15:14:42 +0000268
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000269 memcpy(&ssdt->signature, "SSDT", 4);
Uwe Hermann622824c2010-11-19 15:14:42 +0000270 ssdt->revision = 2; /* ACPI 1.0/2.0: ?, ACPI 3.0/4.0: 2 */
Rudolf Marek293b5f52009-02-01 18:35:15 +0000271 memcpy(&ssdt->oem_id, OEM_ID, 6);
272 memcpy(&ssdt->oem_table_id, oem_table_id, 8);
273 ssdt->oem_revision = 42;
Uwe Hermann622824c2010-11-19 15:14:42 +0000274 memcpy(&ssdt->asl_compiler_id, ASLC, 4);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000275 ssdt->asl_compiler_revision = 42;
276 ssdt->length = sizeof(acpi_header_t);
277
Stefan Reinauer8dcd50b2009-03-06 17:24:29 +0000278 acpigen_set_current((char *) current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200279 {
280#if IS_ENABLED(CONFIG_PER_DEVICE_ACPI_TABLES)
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100281 struct device *dev;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200282 for (dev = all_devices; dev; dev = dev->next)
283 if (dev->ops && dev->ops->acpi_fill_ssdt_generator) {
Vladimir Serbinenko0a669912014-10-05 14:34:17 +0200284 dev->ops->acpi_fill_ssdt_generator();
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200285 }
Vladimir Serbinenko0a669912014-10-05 14:34:17 +0200286 current = (unsigned long) acpigen_get_current();
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200287#else
288 current = acpi_fill_ssdt_generator(current, oem_table_id);
289#endif
290 }
Rudolf Marek293b5f52009-02-01 18:35:15 +0000291
Uwe Hermann622824c2010-11-19 15:14:42 +0000292 /* (Re)calculate length and checksum. */
Rudolf Marek293b5f52009-02-01 18:35:15 +0000293 ssdt->length = current - (unsigned long)ssdt;
294 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
295}
296
Stefan Reinauerf622d592005-11-26 16:56:05 +0000297int acpi_create_srat_lapic(acpi_srat_lapic_t *lapic, u8 node, u8 apic)
298{
Myles Watson0b4c9f02009-03-10 18:06:47 +0000299 memset((void *)lapic, 0, sizeof(acpi_srat_lapic_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000300
Uwe Hermann622824c2010-11-19 15:14:42 +0000301 lapic->type = 0; /* Processor local APIC/SAPIC affinity structure */
302 lapic->length = sizeof(acpi_srat_lapic_t);
303 lapic->flags = (1 << 0); /* Enabled (the use of this structure). */
304 lapic->proximity_domain_7_0 = node;
305 /* TODO: proximity_domain_31_8, local SAPIC EID, clock domain. */
306 lapic->apic_id = apic;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000307
Uwe Hermann622824c2010-11-19 15:14:42 +0000308 return lapic->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000309}
310
Uwe Hermann622824c2010-11-19 15:14:42 +0000311int acpi_create_srat_mem(acpi_srat_mem_t *mem, u8 node, u32 basek, u32 sizek,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300312 u32 flags)
Stefan Reinauerf622d592005-11-26 16:56:05 +0000313{
Uwe Hermann622824c2010-11-19 15:14:42 +0000314 mem->type = 1; /* Memory affinity structure */
315 mem->length = sizeof(acpi_srat_mem_t);
316 mem->base_address_low = (basek << 10);
317 mem->base_address_high = (basek >> (32 - 10));
318 mem->length_low = (sizek << 10);
319 mem->length_high = (sizek >> (32 - 10));
320 mem->proximity_domain = node;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000321 mem->flags = flags;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000322
Uwe Hermann622824c2010-11-19 15:14:42 +0000323 return mem->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000324}
325
Uwe Hermann622824c2010-11-19 15:14:42 +0000326/* http://www.microsoft.com/whdc/system/sysinternals/sratdwn.mspx */
Stefan Reinauerf622d592005-11-26 16:56:05 +0000327void acpi_create_srat(acpi_srat_t *srat)
328{
Uwe Hermann622824c2010-11-19 15:14:42 +0000329 acpi_header_t *header = &(srat->header);
330 unsigned long current = (unsigned long)srat + sizeof(acpi_srat_t);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000331
Uwe Hermann622824c2010-11-19 15:14:42 +0000332 memset((void *)srat, 0, sizeof(acpi_srat_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000333
Uwe Hermann622824c2010-11-19 15:14:42 +0000334 /* Fill out header fields. */
335 memcpy(header->signature, "SRAT", 4);
336 memcpy(header->oem_id, OEM_ID, 6);
337 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
338 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000339
Uwe Hermann622824c2010-11-19 15:14:42 +0000340 header->length = sizeof(acpi_srat_t);
341 header->revision = 1; /* ACPI 1.0: N/A, 2.0: 1, 3.0: 2, 4.0: 3 */
Stefan Reinauerf622d592005-11-26 16:56:05 +0000342
Uwe Hermann622824c2010-11-19 15:14:42 +0000343 srat->resv = 1; /* Spec: Reserved to 1 for backwards compatibility. */
Stefan Reinauerf622d592005-11-26 16:56:05 +0000344
Uwe Hermann622824c2010-11-19 15:14:42 +0000345 current = acpi_fill_srat(current);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000346
Uwe Hermann622824c2010-11-19 15:14:42 +0000347 /* (Re)calculate length and checksum. */
348 header->length = current - (unsigned long)srat;
349 header->checksum = acpi_checksum((void *)srat, header->length);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000350}
351
Vladimir Serbinenko8d70e942014-11-09 13:22:27 +0100352void acpi_create_dmar(acpi_dmar_t *dmar,
353 unsigned long (*acpi_fill_dmar) (unsigned long))
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200354{
355 acpi_header_t *header = &(dmar->header);
356 unsigned long current = (unsigned long)dmar + sizeof(acpi_dmar_t);
357
358 memset((void *)dmar, 0, sizeof(acpi_dmar_t));
359
360 /* Fill out header fields. */
361 memcpy(header->signature, "DMAR", 4);
362 memcpy(header->oem_id, OEM_ID, 6);
363 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
364 memcpy(header->asl_compiler_id, ASLC, 4);
365
366 header->length = sizeof(acpi_dmar_t);
367 header->revision = 1;
368
369 dmar->host_address_width = 40 - 1; /* FIXME: == MTRR size? */
370 dmar->flags = 0;
371
372 current = acpi_fill_dmar(current);
373
374 /* (Re)calculate length and checksum. */
375 header->length = current - (unsigned long)dmar;
376 header->checksum = acpi_checksum((void *)dmar, header->length);
377}
378
379unsigned long acpi_create_dmar_drhd(unsigned long current, u8 flags,
380 u16 segment, u32 bar)
381{
382 dmar_entry_t *drhd = (dmar_entry_t *)current;
383 memset(drhd, 0, sizeof(*drhd));
384 drhd->type = DMAR_DRHD;
385 drhd->length = sizeof(*drhd); /* will be fixed up later */
386 drhd->flags = flags;
387 drhd->segment = segment;
388 drhd->bar = bar;
389
390 return drhd->length;
391}
392
393void acpi_dmar_drhd_fixup(unsigned long base, unsigned long current)
394{
395 dmar_entry_t *drhd = (dmar_entry_t *)base;
396 drhd->length = current - base;
397}
398
399unsigned long acpi_create_dmar_drhd_ds_pci(unsigned long current, u8 segment,
400 u8 dev, u8 fn)
401{
402 dev_scope_t *ds = (dev_scope_t *)current;
403 memset(ds, 0, sizeof(*ds));
404 ds->type = SCOPE_PCI_ENDPOINT;
405 ds->length = sizeof(*ds) + 2; /* we don't support longer paths yet */
406 ds->start_bus = segment;
407 ds->path[0].dev = dev;
408 ds->path[0].fn = fn;
409
410 return ds->length;
411}
412
Uwe Hermann622824c2010-11-19 15:14:42 +0000413/* http://h21007.www2.hp.com/portal/download/files/unprot/Itanium/slit.pdf */
Yinghai Lud4b278c2006-10-04 20:46:15 +0000414void acpi_create_slit(acpi_slit_t *slit)
415{
Uwe Hermann622824c2010-11-19 15:14:42 +0000416 acpi_header_t *header = &(slit->header);
417 unsigned long current = (unsigned long)slit + sizeof(acpi_slit_t);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000418
Uwe Hermann622824c2010-11-19 15:14:42 +0000419 memset((void *)slit, 0, sizeof(acpi_slit_t));
Yinghai Lud4b278c2006-10-04 20:46:15 +0000420
Uwe Hermann622824c2010-11-19 15:14:42 +0000421 /* Fill out header fields. */
422 memcpy(header->signature, "SLIT", 4);
423 memcpy(header->oem_id, OEM_ID, 6);
424 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
425 memcpy(header->asl_compiler_id, ASLC, 4);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000426
Uwe Hermann622824c2010-11-19 15:14:42 +0000427 header->length = sizeof(acpi_slit_t);
428 header->revision = 1; /* ACPI 1.0: N/A, ACPI 2.0/3.0/4.0: 1 */
Yinghai Lud4b278c2006-10-04 20:46:15 +0000429
Uwe Hermann622824c2010-11-19 15:14:42 +0000430 current = acpi_fill_slit(current);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000431
Uwe Hermann622824c2010-11-19 15:14:42 +0000432 /* (Re)calculate length and checksum. */
433 header->length = current - (unsigned long)slit;
434 header->checksum = acpi_checksum((void *)slit, header->length);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000435}
436
Uwe Hermann622824c2010-11-19 15:14:42 +0000437/* http://www.intel.com/hardwaredesign/hpetspec_1.pdf */
Stefan Reinauer777224c2005-01-19 14:06:41 +0000438void acpi_create_hpet(acpi_hpet_t *hpet)
Stefan Reinauer688b3852004-01-28 16:56:14 +0000439{
Uwe Hermann622824c2010-11-19 15:14:42 +0000440 acpi_header_t *header = &(hpet->header);
441 acpi_addr_t *addr = &(hpet->addr);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000442
Stefan Reinauera7648c22004-01-29 17:31:34 +0000443 memset((void *)hpet, 0, sizeof(acpi_hpet_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000444
Uwe Hermann622824c2010-11-19 15:14:42 +0000445 /* Fill out header fields. */
Stefan Reinauer4704dc52009-07-22 01:11:37 +0000446 memcpy(header->signature, "HPET", 4);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000447 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000448 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000449 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000450
Stefan Reinauer688b3852004-01-28 16:56:14 +0000451 header->length = sizeof(acpi_hpet_t);
Uwe Hermann622824c2010-11-19 15:14:42 +0000452 header->revision = 1; /* Currently 1. Table added in ACPI 2.0. */
Stefan Reinauer688b3852004-01-28 16:56:14 +0000453
Uwe Hermann622824c2010-11-19 15:14:42 +0000454 /* Fill out HPET address. */
455 addr->space_id = 0; /* Memory */
456 addr->bit_width = 64;
457 addr->bit_offset = 0;
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200458 addr->addrl = CONFIG_HPET_ADDRESS & 0xffffffff;
459 addr->addrh = ((unsigned long long)CONFIG_HPET_ADDRESS) >> 32;
Stefan Reinauer688b3852004-01-28 16:56:14 +0000460
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200461 hpet->id = *(unsigned int*)CONFIG_HPET_ADDRESS;
Uwe Hermann622824c2010-11-19 15:14:42 +0000462 hpet->number = 0;
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200463 hpet->min_tick = CONFIG_HPET_MIN_TICKS;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000464
Uwe Hermann622824c2010-11-19 15:14:42 +0000465 header->checksum = acpi_checksum((void *)hpet, sizeof(acpi_hpet_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000466}
Uwe Hermann622824c2010-11-19 15:14:42 +0000467
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200468unsigned long acpi_write_hpet(unsigned long current, acpi_rsdp_t *rsdp)
469{
470 acpi_hpet_t *hpet;
471
472 /*
473 * We explicitly add these tables later on:
474 */
475 printk(BIOS_DEBUG, "ACPI: * HPET\n");
476
477 hpet = (acpi_hpet_t *) current;
478 current += sizeof(acpi_hpet_t);
479 current = ALIGN(current, 16);
480 acpi_create_hpet(hpet);
481 acpi_add_table(rsdp, hpet);
482
483 return current;
484}
485
Stefan Reinauer777224c2005-01-19 14:06:41 +0000486void acpi_create_facs(acpi_facs_t *facs)
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000487{
Uwe Hermann622824c2010-11-19 15:14:42 +0000488 memset((void *)facs, 0, sizeof(acpi_facs_t));
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000489
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000490 memcpy(facs->signature, "FACS", 4);
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000491 facs->length = sizeof(acpi_facs_t);
492 facs->hardware_signature = 0;
493 facs->firmware_waking_vector = 0;
494 facs->global_lock = 0;
495 facs->flags = 0;
496 facs->x_firmware_waking_vector_l = 0;
497 facs->x_firmware_waking_vector_h = 0;
Uwe Hermann622824c2010-11-19 15:14:42 +0000498 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 +0000499}
Stefan Reinauer777224c2005-01-19 14:06:41 +0000500
501void acpi_write_rsdt(acpi_rsdt_t *rsdt)
Stefan Reinauer14e22772010-04-27 06:56:47 +0000502{
Uwe Hermann622824c2010-11-19 15:14:42 +0000503 acpi_header_t *header = &(rsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000504
Uwe Hermann622824c2010-11-19 15:14:42 +0000505 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000506 memcpy(header->signature, "RSDT", 4);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000507 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000508 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000509 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000510
Stefan Reinauer688b3852004-01-28 16:56:14 +0000511 header->length = sizeof(acpi_rsdt_t);
Uwe Hermann622824c2010-11-19 15:14:42 +0000512 header->revision = 1; /* ACPI 1.0/2.0/3.0/4.0: 1 */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000513
Uwe Hermann622824c2010-11-19 15:14:42 +0000514 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauer688b3852004-01-28 16:56:14 +0000515
Uwe Hermann622824c2010-11-19 15:14:42 +0000516 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000517 header->checksum = acpi_checksum((void *)rsdt, sizeof(acpi_rsdt_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000518}
519
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000520void acpi_write_xsdt(acpi_xsdt_t *xsdt)
Stefan Reinauer14e22772010-04-27 06:56:47 +0000521{
Uwe Hermann622824c2010-11-19 15:14:42 +0000522 acpi_header_t *header = &(xsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000523
Uwe Hermann622824c2010-11-19 15:14:42 +0000524 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000525 memcpy(header->signature, "XSDT", 4);
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000526 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000527 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000528 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000529
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000530 header->length = sizeof(acpi_xsdt_t);
Uwe Hermann622824c2010-11-19 15:14:42 +0000531 header->revision = 1; /* ACPI 1.0: N/A, 2.0/3.0/4.0: 1 */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000532
Uwe Hermann622824c2010-11-19 15:14:42 +0000533 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000534
Uwe Hermann622824c2010-11-19 15:14:42 +0000535 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000536 header->checksum = acpi_checksum((void *)xsdt, sizeof(acpi_xsdt_t));
537}
538
539void acpi_write_rsdp(acpi_rsdp_t *rsdp, acpi_rsdt_t *rsdt, acpi_xsdt_t *xsdt)
Stefan Reinauer688b3852004-01-28 16:56:14 +0000540{
Stefan Reinauerd18faac2009-11-05 18:06:43 +0000541 memset(rsdp, 0, sizeof(acpi_rsdp_t));
Uwe Hermann622824c2010-11-19 15:14:42 +0000542
Stefan Reinauer688b3852004-01-28 16:56:14 +0000543 memcpy(rsdp->signature, RSDP_SIG, 8);
544 memcpy(rsdp->oem_id, OEM_ID, 6);
Uwe Hermann622824c2010-11-19 15:14:42 +0000545
546 rsdp->length = sizeof(acpi_rsdp_t);
547 rsdp->rsdt_address = (u32)rsdt;
548
549 /*
550 * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2.
551 *
552 * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
553 * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
554 * revision 0).
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000555 */
556 if (xsdt == NULL) {
Uwe Hermann622824c2010-11-19 15:14:42 +0000557 rsdp->revision = 0;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000558 } else {
Uwe Hermann622824c2010-11-19 15:14:42 +0000559 rsdp->xsdt_address = (u64)(u32)xsdt;
560 rsdp->revision = 2;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000561 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000562
563 /* Calculate checksums. */
564 rsdp->checksum = acpi_checksum((void *)rsdp, 20);
565 rsdp->ext_checksum = acpi_checksum((void *)rsdp, sizeof(acpi_rsdp_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000566}
567
zbaocaf494c82012-04-13 13:57:14 +0800568unsigned long __attribute__((weak)) acpi_fill_hest(acpi_hest_t *hest)
569{
570 return (unsigned long)hest;
571}
572
573unsigned long acpi_create_hest_error_source(acpi_hest_t *hest, acpi_hest_esd_t *esd, u16 type, void *data, u16 data_len)
574{
575 acpi_header_t *header = &(hest->header);
576 acpi_hest_hen_t *hen;
577 void *pos;
578 u16 len;
579
580 pos = esd;
581 memset(pos, 0, sizeof(acpi_hest_esd_t));
582 len = 0;
583 esd->type = type; /* MCE */
584 esd->source_id = hest->error_source_count;
585 esd->flags = 0; /* FIRMWARE_FIRST */
586 esd->enabled = 1;
587 esd->prealloc_erecords = 1;
588 esd->max_section_per_record = 0x1;
589
590 len += sizeof(acpi_hest_esd_t);
591 pos = esd + 1;
592
593 switch (type) {
594 case 0: /* MCE */
595 break;
596 case 1: /* CMC */
597 hen = (acpi_hest_hen_t *) (pos);
598 memset(pos, 0, sizeof(acpi_hest_hen_t));
599 hen->type = 3; /* SCI? */
600 hen->length = sizeof(acpi_hest_hen_t);
601 hen->conf_we = 0; /* Configuration Write Enable. */
602 hen->poll_interval = 0;
603 hen->vector = 0;
604 hen->sw2poll_threshold_val = 0;
605 hen->sw2poll_threshold_win = 0;
606 hen->error_threshold_val = 0;
607 hen->error_threshold_win = 0;
608 len += sizeof(acpi_hest_hen_t);
609 pos = hen + 1;
610 break;
611 case 2: /* NMI */
612 case 6: /* AER Root Port */
613 case 7: /* AER Endpoint */
614 case 8: /* AER Bridge */
615 case 9: /* Generic Hardware Error Source. */
616 /* TODO: */
617 break;
618 default:
619 printk(BIOS_DEBUG, "Invalid type of Error Source.");
620 break;
621 }
622 hest->error_source_count ++;
623
624 memcpy(pos, data, data_len);
625 len += data_len;
626 header->length += len;
627
628 return len;
629}
630
631/* ACPI 4.0 */
632void acpi_write_hest(acpi_hest_t *hest)
633{
634 acpi_header_t *header = &(hest->header);
635
636 memset(hest, 0, sizeof(acpi_hest_t));
637
638 memcpy(header->signature, "HEST", 4);
639 memcpy(header->oem_id, OEM_ID, 6);
640 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
641 memcpy(header->asl_compiler_id, ASLC, 4);
642 header->length += sizeof(acpi_hest_t);
643 header->revision = 1;
644
645 acpi_fill_hest(hest);
646
647 /* Calculate checksums. */
648 header->checksum = acpi_checksum((void *)hest, header->length);
649}
650
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +0200651#if IS_ENABLED(CONFIG_COMMON_FADT)
652void acpi_create_fadt(acpi_fadt_t *fadt,acpi_facs_t *facs, void *dsdt)
653{
654 acpi_header_t *header = &(fadt->header);
655
656 memset((void *) fadt, 0, sizeof(acpi_fadt_t));
657 memcpy(header->signature, "FACP", 4);
658 header->length = sizeof(acpi_fadt_t);
Vladimir Serbinenkoab83ef02014-10-25 15:18:25 +0200659 header->revision = 4;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +0200660 memcpy(header->oem_id, OEM_ID, 6);
661 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
662 memcpy(header->asl_compiler_id, ASLC, 4);
663 header->asl_compiler_revision = 0;
664
665 fadt->firmware_ctrl = (unsigned long) facs;
666 fadt->dsdt = (unsigned long) dsdt;
667
668 fadt->x_firmware_ctl_l = (unsigned long)facs;
669 fadt->x_firmware_ctl_h = 0;
670 fadt->x_dsdt_l = (unsigned long)dsdt;
671 fadt->x_dsdt_h = 0;
672
673 if(IS_ENABLED(CONFIG_SYSTEM_TYPE_LAPTOP)) {
674 fadt->preferred_pm_profile = PM_MOBILE;
675 } else {
676 fadt->preferred_pm_profile = PM_DESKTOP;
677 }
678
Vladimir Serbinenkoab83ef02014-10-25 15:18:25 +0200679 acpi_fill_fadt(fadt);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +0200680
681 header->checksum =
682 acpi_checksum((void *) fadt, header->length);
683}
684#endif
685
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200686#if IS_ENABLED(CONFIG_PER_DEVICE_ACPI_TABLES)
687
688extern const unsigned char AmlCode[];
689
Vladimir Serbinenko41877d82014-09-01 22:18:01 +0200690unsigned long __attribute__ ((weak)) fw_cfg_acpi_tables(unsigned long start)
691{
692 return 0;
693}
694
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200695#define ALIGN_CURRENT current = (ALIGN(current, 16))
696unsigned long write_acpi_tables(unsigned long start)
697{
698 unsigned long current;
699 acpi_rsdp_t *rsdp;
700 acpi_rsdt_t *rsdt;
701 acpi_xsdt_t *xsdt;
702 acpi_fadt_t *fadt;
703 acpi_facs_t *facs;
704#if CONFIG_HAVE_ACPI_SLIC
705 acpi_header_t *slic;
706#endif
707 acpi_header_t *ssdt;
708 acpi_header_t *dsdt;
709 acpi_mcfg_t *mcfg;
710 acpi_madt_t *madt;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100711 struct device *dev;
Vladimir Serbinenko41877d82014-09-01 22:18:01 +0200712 unsigned long fw;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200713
714 current = start;
715
716 /* Align ACPI tables to 16byte */
717 ALIGN_CURRENT;
718
Vladimir Serbinenko41877d82014-09-01 22:18:01 +0200719 fw = fw_cfg_acpi_tables(current);
720 if (fw)
721 return fw;
722
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200723 printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx.\n", start);
724
725 /* We need at least an RSDP and an RSDT Table */
726 rsdp = (acpi_rsdp_t *) current;
727 current += sizeof(acpi_rsdp_t);
728 ALIGN_CURRENT;
729 rsdt = (acpi_rsdt_t *) current;
730 current += sizeof(acpi_rsdt_t);
731 ALIGN_CURRENT;
732 xsdt = (acpi_xsdt_t *) current;
733 current += sizeof(acpi_xsdt_t);
734 ALIGN_CURRENT;
735
736 /* clear all table memory */
737 memset((void *) start, 0, current - start);
738
739 acpi_write_rsdp(rsdp, rsdt, xsdt);
740 acpi_write_rsdt(rsdt);
741 acpi_write_xsdt(xsdt);
742
743 printk(BIOS_DEBUG, "ACPI: * FACS\n");
744 facs = (acpi_facs_t *) current;
745 current += sizeof(acpi_facs_t);
746 ALIGN_CURRENT;
747 acpi_create_facs(facs);
748
749 printk(BIOS_DEBUG, "ACPI: * DSDT\n");
750 dsdt = (acpi_header_t *) current;
751 memcpy(dsdt, &AmlCode, sizeof(acpi_header_t));
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +0200752 if (dsdt->length >= sizeof(acpi_header_t)) {
753 current += sizeof(acpi_header_t);
754
755 acpigen_set_current((char *) current);
756 for (dev = all_devices; dev; dev = dev->next)
757 if (dev->ops && dev->ops->acpi_inject_dsdt_generator) {
758 dev->ops->acpi_inject_dsdt_generator();
759 }
760 current = (unsigned long) acpigen_get_current();
761 memcpy((char *)current,
762 (char *)&AmlCode + sizeof(acpi_header_t),
763 dsdt->length - sizeof(acpi_header_t));
764 current += dsdt->length - sizeof(acpi_header_t);
765
766 /* (Re)calculate length and checksum. */
767 dsdt->length = current - (unsigned long)dsdt;
768 dsdt->checksum = 0;
769 dsdt->checksum = acpi_checksum((void *)dsdt, dsdt->length);
770 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200771
772 ALIGN_CURRENT;
773
774 printk(BIOS_DEBUG, "ACPI: * FADT\n");
775 fadt = (acpi_fadt_t *) current;
776 current += sizeof(acpi_fadt_t);
777 ALIGN_CURRENT;
778
779 acpi_create_fadt(fadt, facs, dsdt);
780 acpi_add_table(rsdp, fadt);
781
782#if CONFIG_HAVE_ACPI_SLIC
783 printk(BIOS_DEBUG, "ACPI: * SLIC\n");
784 slic = (acpi_header_t *)current;
785 current += acpi_create_slic(current);
786 ALIGN_CURRENT;
787 acpi_add_table(rsdp, slic);
788#endif
789
790 printk(BIOS_DEBUG, "ACPI: * SSDT\n");
791 ssdt = (acpi_header_t *)current;
792 acpi_create_ssdt_generator(ssdt, ACPI_TABLE_CREATOR);
Vladimir Serbinenko9310df82014-10-10 20:40:41 +0200793 if (ssdt->length > sizeof(acpi_header_t)) {
794 current += ssdt->length;
795 acpi_add_table(rsdp, ssdt);
796 ALIGN_CURRENT;
797 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200798
799 printk(BIOS_DEBUG, "ACPI: * MCFG\n");
800 mcfg = (acpi_mcfg_t *) current;
801 acpi_create_mcfg(mcfg);
802 if (mcfg->header.length > sizeof(acpi_mcfg_t)) {
803 current += mcfg->header.length;
804 ALIGN_CURRENT;
805 acpi_add_table(rsdp, mcfg);
806 }
807
808 printk(BIOS_DEBUG, "ACPI: * MADT\n");
809
810 madt = (acpi_madt_t *) current;
811 acpi_create_madt(madt);
812 if (madt->header.length > sizeof(acpi_madt_t)) {
813 current+=madt->header.length;
814 acpi_add_table(rsdp,madt);
815 }
816 ALIGN_CURRENT;
817
818 printk(BIOS_DEBUG, "current = %lx\n", current);
819
820 for (dev = all_devices; dev; dev = dev->next) {
821 if (dev->ops && dev->ops->write_acpi_tables) {
822 current = dev->ops->write_acpi_tables(current, rsdp);
823 ALIGN_CURRENT;
824 }
825 }
826
827 printk(BIOS_INFO, "ACPI: done.\n");
828 return current;
829}
830#endif
831
Patrick Georgie1667822012-05-05 15:29:32 +0200832#if CONFIG_HAVE_ACPI_RESUME
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500833void acpi_resume(void *wake_vec)
Stefan Reinauer7e9771c2009-04-22 08:17:38 +0000834{
Duncan Laurie11290c42012-10-03 19:07:05 -0700835#if CONFIG_HAVE_SMI_HANDLER
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500836 u32 *gnvs_address = cbmem_find(CBMEM_ID_ACPI_GNVS_PTR);
Duncan Laurie11290c42012-10-03 19:07:05 -0700837
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500838 /* Restore GNVS pointer in SMM if found */
839 if (gnvs_address && *gnvs_address) {
840 printk(BIOS_DEBUG, "Restore GNVS pointer to 0x%08x\n",
841 *gnvs_address);
842 smm_setup_structures((void *)*gnvs_address, NULL, NULL);
Stefan Reinauercb91e152012-04-03 23:28:22 +0200843 }
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500844#endif
845
846 /* Call mainboard resume handler first, if defined. */
847 if (mainboard_suspend_resume)
848 mainboard_suspend_resume();
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500849
850 post_code(POST_OS_RESUME);
851 acpi_jump_to_wakeup(wake_vec);
Stefan Reinauer7e9771c2009-04-22 08:17:38 +0000852}
853
Uwe Hermann622824c2010-11-19 15:14:42 +0000854/* This is to be filled by SB code - startup value what was found. */
Stefan Reinauer7e9771c2009-04-22 08:17:38 +0000855u8 acpi_slp_type = 0;
Stefan Reinauer688b3852004-01-28 16:56:14 +0000856
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +0300857static void acpi_handoff_wakeup(void)
858{
859 static int once = 0;
860 if (once)
861 return;
862 if (acpi_get_sleep_type)
863 acpi_slp_type = acpi_get_sleep_type();
864 once = 1;
865}
866
Kyösti Mälkkicb28f3f2014-01-03 15:15:22 +0200867int acpi_is_wakeup(void)
Rudolf Marek33cafe52009-04-13 18:07:02 +0000868{
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +0300869 acpi_handoff_wakeup();
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +0100870 /* Both resume from S2 and resume from S3 restart at CPU reset */
871 return (acpi_slp_type == 3 || acpi_slp_type == 2);
Rudolf Marek33cafe52009-04-13 18:07:02 +0000872}
873
Kyösti Mälkki4d9b7722014-06-19 19:45:40 +0300874int acpi_is_wakeup_s3(void)
875{
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +0300876 acpi_handoff_wakeup();
Kyösti Mälkki4d9b7722014-06-19 19:45:40 +0300877 return (acpi_slp_type == 3);
878}
879
Kyösti Mälkkicb28f3f2014-01-03 15:15:22 +0200880void acpi_fail_wakeup(void)
881{
882 if (acpi_slp_type == 3 || acpi_slp_type == 2)
883 acpi_slp_type = 0;
884}
885
Kyösti Mälkki7b14f082014-10-16 20:58:47 +0300886void acpi_prepare_resume_backup(void)
887{
888 if (!acpi_s3_resume_allowed())
889 return;
890
891 /* Let's prepare the ACPI S3 Resume area now already, so we can rely on
892 * it being there during reboot time. We don't need the pointer, nor
893 * the result right now. If it fails, ACPI resume will be disabled.
894 */
895
896 if (HIGH_MEMORY_SAVE)
897 cbmem_add(CBMEM_ID_RESUME, HIGH_MEMORY_SAVE);
898
899 if (HIGH_MEMORY_SCRATCH)
900 cbmem_add(CBMEM_ID_RESUME_SCRATCH, HIGH_MEMORY_SCRATCH);
901}
902
Rudolf Marek33cafe52009-04-13 18:07:02 +0000903static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp)
904{
905 if (strncmp((char *)rsdp, RSDP_SIG, sizeof(RSDP_SIG) - 1) != 0)
906 return NULL;
907
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000908 printk(BIOS_DEBUG, "Looking on %p for valid checksum\n", rsdp);
Rudolf Marek33cafe52009-04-13 18:07:02 +0000909
910 if (acpi_checksum((void *)rsdp, 20) != 0)
911 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000912 printk(BIOS_DEBUG, "Checksum 1 passed\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +0000913
914 if ((rsdp->revision > 1) && (acpi_checksum((void *)rsdp,
915 rsdp->length) != 0))
916 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000917 printk(BIOS_DEBUG, "Checksum 2 passed all OK\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +0000918
919 return rsdp;
920}
921
922static acpi_rsdp_t *rsdp;
923
924void *acpi_get_wakeup_rsdp(void)
925{
926 return rsdp;
927}
928
929void *acpi_find_wakeup_vector(void)
930{
931 char *p, *end;
Rudolf Marek33cafe52009-04-13 18:07:02 +0000932 acpi_rsdt_t *rsdt;
933 acpi_facs_t *facs;
Martin Rothc5f49262012-12-14 19:17:55 -0700934 acpi_fadt_t *fadt = NULL;
Uwe Hermann622824c2010-11-19 15:14:42 +0000935 void *wake_vec;
Rudolf Marek33cafe52009-04-13 18:07:02 +0000936 int i;
937
938 rsdp = NULL;
939
940 if (!acpi_is_wakeup())
941 return NULL;
942
Uwe Hermann622824c2010-11-19 15:14:42 +0000943 printk(BIOS_DEBUG, "Trying to find the wakeup vector...\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +0000944
Uwe Hermann622824c2010-11-19 15:14:42 +0000945 /* Find RSDP. */
946 for (p = (char *)0xe0000; p < (char *)0xfffff; p += 16) {
947 if ((rsdp = valid_rsdp((acpi_rsdp_t *)p)))
Rudolf Marek33cafe52009-04-13 18:07:02 +0000948 break;
949 }
950
951 if (rsdp == NULL)
952 return NULL;
953
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000954 printk(BIOS_DEBUG, "RSDP found at %p\n", rsdp);
Rudolf Marek33cafe52009-04-13 18:07:02 +0000955 rsdt = (acpi_rsdt_t *) rsdp->rsdt_address;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000956
Uwe Hermann622824c2010-11-19 15:14:42 +0000957 end = (char *)rsdt + rsdt->header.length;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000958 printk(BIOS_DEBUG, "RSDT found at %p ends at %p\n", rsdt, end);
Rudolf Marek33cafe52009-04-13 18:07:02 +0000959
Uwe Hermann622824c2010-11-19 15:14:42 +0000960 for (i = 0; ((char *)&rsdt->entry[i]) < end; i++) {
961 fadt = (acpi_fadt_t *)rsdt->entry[i];
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000962 if (strncmp((char *)fadt, "FACP", 4) == 0)
Rudolf Marek33cafe52009-04-13 18:07:02 +0000963 break;
964 fadt = NULL;
965 }
966
967 if (fadt == NULL)
968 return NULL;
969
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000970 printk(BIOS_DEBUG, "FADT found at %p\n", fadt);
Stefan Reinauer69390db2009-05-26 12:33:52 +0000971 facs = (acpi_facs_t *)fadt->firmware_ctrl;
Rudolf Marek33cafe52009-04-13 18:07:02 +0000972
Stefan Reinauer7e9771c2009-04-22 08:17:38 +0000973 if (facs == NULL) {
Uwe Hermann622824c2010-11-19 15:14:42 +0000974 printk(BIOS_DEBUG, "No FACS found, wake up from S3 not "
975 "possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +0000976 return NULL;
Stefan Reinauer7e9771c2009-04-22 08:17:38 +0000977 }
Rudolf Marek33cafe52009-04-13 18:07:02 +0000978
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000979 printk(BIOS_DEBUG, "FACS found at %p\n", facs);
Uwe Hermann622824c2010-11-19 15:14:42 +0000980 wake_vec = (void *)facs->firmware_waking_vector;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000981 printk(BIOS_DEBUG, "OS waking vector is %p\n", wake_vec);
Uwe Hermann622824c2010-11-19 15:14:42 +0000982
Rudolf Marek33cafe52009-04-13 18:07:02 +0000983 return wake_vec;
984}
985
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +0100986#if CONFIG_SMP
Rudolf Marek33cafe52009-04-13 18:07:02 +0000987extern char *lowmem_backup;
988extern char *lowmem_backup_ptr;
989extern int lowmem_backup_size;
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +0100990#endif
Rudolf Marek33cafe52009-04-13 18:07:02 +0000991
Uwe Hermann622824c2010-11-19 15:14:42 +0000992#define WAKEUP_BASE 0x600
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000993
Uwe Hermann622824c2010-11-19 15:14:42 +0000994void (*acpi_do_wakeup)(u32 vector, u32 backup_source, u32 backup_target,
Stefan Reinauer399486e2012-12-06 13:54:29 -0800995 u32 backup_size) asmlinkage = (void *)WAKEUP_BASE;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000996
Aaron Durbina146d582013-02-08 16:56:51 -0600997extern unsigned char __wakeup;
998extern unsigned int __wakeup_size;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000999
Rudolf Marek33cafe52009-04-13 18:07:02 +00001000void acpi_jump_to_wakeup(void *vector)
1001{
Aaron Durbin8e4a3552013-02-08 17:28:04 -06001002 u32 acpi_backup_memory = 0;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001003
Kyösti Mälkki2ca2afe2014-06-17 15:41:37 +03001004 if (HIGH_MEMORY_SAVE && acpi_s3_resume_allowed()) {
1005 acpi_backup_memory = (u32)cbmem_find(CBMEM_ID_RESUME);
1006
1007 if (!acpi_backup_memory) {
1008 printk(BIOS_WARNING, "ACPI: Backup memory missing. "
1009 "No S3 resume.\n");
1010 return;
1011 }
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001012 }
1013
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +01001014#if CONFIG_SMP
Martin Roth7b5f8ef2013-07-08 16:22:10 -06001015 // FIXME: This should go into the ACPI backup memory, too. No pork sausages.
Uwe Hermann622824c2010-11-19 15:14:42 +00001016 /*
1017 * Just restore the SMP trampoline and continue with wakeup on
1018 * assembly level.
1019 */
Rudolf Marek33cafe52009-04-13 18:07:02 +00001020 memcpy(lowmem_backup_ptr, lowmem_backup, lowmem_backup_size);
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +01001021#endif
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001022
Uwe Hermann622824c2010-11-19 15:14:42 +00001023 /* Copy wakeup trampoline in place. */
Aaron Durbina146d582013-02-08 16:56:51 -06001024 memcpy((void *)WAKEUP_BASE, &__wakeup, __wakeup_size);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001025
Duncan Lauriecde78012011-10-19 15:32:39 -07001026#if CONFIG_COLLECT_TIMESTAMPS
1027 timestamp_add_now(TS_ACPI_WAKE_JUMP);
1028#endif
1029
Uwe Hermann622824c2010-11-19 15:14:42 +00001030 acpi_do_wakeup((u32)vector, acpi_backup_memory, CONFIG_RAMBASE,
1031 HIGH_MEMORY_SAVE);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001032}
1033#endif
Duncan Laurie11290c42012-10-03 19:07:05 -07001034
1035void acpi_save_gnvs(u32 gnvs_address)
1036{
Duncan Laurie9c07c8f2013-03-22 11:08:39 -07001037 u32 *gnvs = cbmem_add(CBMEM_ID_ACPI_GNVS_PTR, sizeof(*gnvs));
Duncan Laurie11290c42012-10-03 19:07:05 -07001038 if (gnvs)
1039 *gnvs = gnvs_address;
1040}