blob: bc2a0ccf13df312ff7b9fd32223f5d96ac9997d6 [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>
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +010036#include <cbfs.h>
Duncan Lauriecde78012011-10-19 15:32:39 -070037#if CONFIG_COLLECT_TIMESTAMPS
38#include <timestamp.h>
39#endif
Kyösti Mälkki9e94dbf2015-01-08 20:03:18 +020040#include <romstage_handoff.h>
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000041
Patrick Georgi9aeb6942012-10-05 21:54:38 +020042/* FIXME: Kconfig doesn't support overridable defaults :-( */
43#ifndef CONFIG_HPET_MIN_TICKS
44#define CONFIG_HPET_MIN_TICKS 0x1000
45#endif
46
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000047u8 acpi_checksum(u8 *table, u32 length)
Stefan Reinauer688b3852004-01-28 16:56:14 +000048{
Uwe Hermann622824c2010-11-19 15:14:42 +000049 u8 ret = 0;
Stefan Reinauer688b3852004-01-28 16:56:14 +000050 while (length--) {
51 ret += *table;
52 table++;
53 }
Stefan Reinauera7648c22004-01-29 17:31:34 +000054 return -ret;
Stefan Reinauer688b3852004-01-28 16:56:14 +000055}
56
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000057/**
Uwe Hermann622824c2010-11-19 15:14:42 +000058 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
59 * and checksum.
Stefan Reinauer06feb882004-02-03 16:11:35 +000060 */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000061void acpi_add_table(acpi_rsdp_t *rsdp, void *table)
Stefan Reinauer688b3852004-01-28 16:56:14 +000062{
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000063 int i, entries_num;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000064 acpi_rsdt_t *rsdt;
65 acpi_xsdt_t *xsdt = NULL;
66
Uwe Hermann622824c2010-11-19 15:14:42 +000067 /* The RSDT is mandatory... */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000068 rsdt = (acpi_rsdt_t *)rsdp->rsdt_address;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000069
Uwe Hermann622824c2010-11-19 15:14:42 +000070 /* ...while the XSDT is not. */
71 if (rsdp->xsdt_address)
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000072 xsdt = (acpi_xsdt_t *)((u32)rsdp->xsdt_address);
Stefan Reinauer14e22772010-04-27 06:56:47 +000073
Uwe Hermann622824c2010-11-19 15:14:42 +000074 /* This should always be MAX_ACPI_TABLES. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000075 entries_num = ARRAY_SIZE(rsdt->entry);
Stefan Reinauer14e22772010-04-27 06:56:47 +000076
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000077 for (i = 0; i < entries_num; i++) {
Uwe Hermann622824c2010-11-19 15:14:42 +000078 if (rsdt->entry[i] == 0)
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000079 break;
Stefan Reinauer688b3852004-01-28 16:56:14 +000080 }
81
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000082 if (i >= entries_num) {
Uwe Hermann622824c2010-11-19 15:14:42 +000083 printk(BIOS_ERR, "ACPI: Error: Could not add ACPI table, "
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +030084 "too many tables.\n");
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000085 return;
86 }
87
Uwe Hermann622824c2010-11-19 15:14:42 +000088 /* Add table to the RSDT. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000089 rsdt->entry[i] = (u32)table;
90
Uwe Hermann622824c2010-11-19 15:14:42 +000091 /* Fix RSDT length or the kernel will assume invalid entries. */
92 rsdt->header.length = sizeof(acpi_header_t) + (sizeof(u32) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000093
Uwe Hermann622824c2010-11-19 15:14:42 +000094 /* Re-calculate checksum. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000095 rsdt->header.checksum = 0; /* Hope this won't get optimized away */
Uwe Hermann622824c2010-11-19 15:14:42 +000096 rsdt->header.checksum = acpi_checksum((u8 *)rsdt, rsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000097
Uwe Hermann622824c2010-11-19 15:14:42 +000098 /*
99 * And now the same thing for the XSDT. We use the same index as for
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000100 * now we want the XSDT and RSDT to always be in sync in coreboot.
101 */
102 if (xsdt) {
Uwe Hermann622824c2010-11-19 15:14:42 +0000103 /* Add table to the XSDT. */
104 xsdt->entry[i] = (u64)(u32)table;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000105
Uwe Hermann622824c2010-11-19 15:14:42 +0000106 /* Fix XSDT length. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000107 xsdt->header.length = sizeof(acpi_header_t) +
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300108 (sizeof(u64) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000109
Uwe Hermann622824c2010-11-19 15:14:42 +0000110 /* Re-calculate checksum. */
111 xsdt->header.checksum = 0;
112 xsdt->header.checksum = acpi_checksum((u8 *)xsdt,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300113 xsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000114 }
115
Uwe Hermann622824c2010-11-19 15:14:42 +0000116 printk(BIOS_DEBUG, "ACPI: added table %d/%d, length now %d\n",
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300117 i + 1, entries_num, rsdt->header.length);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000118}
119
Uwe Hermann622824c2010-11-19 15:14:42 +0000120int acpi_create_mcfg_mmconfig(acpi_mcfg_mmconfig_t *mmconfig, u32 base,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300121 u16 seg_nr, u8 start, u8 end)
Stefan Reinauer26d431a2009-01-20 19:17:51 +0000122{
Vladimir Serbinenko60fccdc2014-10-05 11:05:16 +0200123 memset(mmconfig, 0, sizeof(*mmconfig));
Rudolf Mareke6409f22007-11-03 12:50:26 +0000124 mmconfig->base_address = base;
125 mmconfig->base_reserved = 0;
126 mmconfig->pci_segment_group_number = seg_nr;
127 mmconfig->start_bus_number = start;
128 mmconfig->end_bus_number = end;
Uwe Hermann622824c2010-11-19 15:14:42 +0000129
130 return sizeof(acpi_mcfg_mmconfig_t);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000131}
132
Stefan Reinauer777224c2005-01-19 14:06:41 +0000133int acpi_create_madt_lapic(acpi_madt_lapic_t *lapic, u8 cpu, u8 apic)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000134{
Uwe Hermann622824c2010-11-19 15:14:42 +0000135 lapic->type = 0; /* Local APIC structure */
136 lapic->length = sizeof(acpi_madt_lapic_t);
137 lapic->flags = (1 << 0); /* Processor/LAPIC enabled */
138 lapic->processor_id = cpu;
139 lapic->apic_id = apic;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000140
Uwe Hermann622824c2010-11-19 15:14:42 +0000141 return lapic->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000142}
143
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000144unsigned long acpi_create_madt_lapics(unsigned long current)
145{
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100146 struct device *cpu;
Duncan Laurie11290c42012-10-03 19:07:05 -0700147 int index = 0;
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000148
Uwe Hermann622824c2010-11-19 15:14:42 +0000149 for (cpu = all_devices; cpu; cpu = cpu->next) {
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000150 if ((cpu->path.type != DEVICE_PATH_APIC) ||
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800151 (cpu->bus->dev->path.type != DEVICE_PATH_CPU_CLUSTER)) {
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000152 continue;
153 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000154 if (!cpu->enabled)
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000155 continue;
Uwe Hermann622824c2010-11-19 15:14:42 +0000156 current += acpi_create_madt_lapic((acpi_madt_lapic_t *)current,
Duncan Laurie11290c42012-10-03 19:07:05 -0700157 index, cpu->path.apic.apic_id);
158 index++;
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000159 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000160
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000161 return current;
162}
163
Uwe Hermann622824c2010-11-19 15:14:42 +0000164int acpi_create_madt_ioapic(acpi_madt_ioapic_t *ioapic, u8 id, u32 addr,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300165 u32 gsi_base)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000166{
Uwe Hermann622824c2010-11-19 15:14:42 +0000167 ioapic->type = 1; /* I/O APIC structure */
168 ioapic->length = sizeof(acpi_madt_ioapic_t);
169 ioapic->reserved = 0x00;
170 ioapic->gsi_base = gsi_base;
171 ioapic->ioapic_id = id;
172 ioapic->ioapic_addr = addr;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000173
Uwe Hermann622824c2010-11-19 15:14:42 +0000174 return ioapic->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000175}
176
Stefan Reinauer777224c2005-01-19 14:06:41 +0000177int acpi_create_madt_irqoverride(acpi_madt_irqoverride_t *irqoverride,
Stefan Reinauer06feb882004-02-03 16:11:35 +0000178 u8 bus, u8 source, u32 gsirq, u16 flags)
179{
Uwe Hermann622824c2010-11-19 15:14:42 +0000180 irqoverride->type = 2; /* Interrupt source override */
181 irqoverride->length = sizeof(acpi_madt_irqoverride_t);
182 irqoverride->bus = bus;
183 irqoverride->source = source;
184 irqoverride->gsirq = gsirq;
185 irqoverride->flags = flags;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000186
Uwe Hermann622824c2010-11-19 15:14:42 +0000187 return irqoverride->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000188}
189
Stefan Reinauer777224c2005-01-19 14:06:41 +0000190int acpi_create_madt_lapic_nmi(acpi_madt_lapic_nmi_t *lapic_nmi, u8 cpu,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300191 u16 flags, u8 lint)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000192{
Uwe Hermann622824c2010-11-19 15:14:42 +0000193 lapic_nmi->type = 4; /* Local APIC NMI structure */
194 lapic_nmi->length = sizeof(acpi_madt_lapic_nmi_t);
195 lapic_nmi->flags = flags;
196 lapic_nmi->processor_id = cpu;
197 lapic_nmi->lint = lint;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000198
Uwe Hermann622824c2010-11-19 15:14:42 +0000199 return lapic_nmi->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000200}
201
Stefan Reinauer777224c2005-01-19 14:06:41 +0000202void acpi_create_madt(acpi_madt_t *madt)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000203{
Uwe Hermann622824c2010-11-19 15:14:42 +0000204 acpi_header_t *header = &(madt->header);
205 unsigned long current = (unsigned long)madt + sizeof(acpi_madt_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000206
Stefan Reinauer06feb882004-02-03 16:11:35 +0000207 memset((void *)madt, 0, sizeof(acpi_madt_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000208
Uwe Hermann622824c2010-11-19 15:14:42 +0000209 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000210 memcpy(header->signature, "APIC", 4);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000211 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000212 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000213 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000214
Stefan Reinauer06feb882004-02-03 16:11:35 +0000215 header->length = sizeof(acpi_madt_t);
Uwe Hermann622824c2010-11-19 15:14:42 +0000216 header->revision = 1; /* ACPI 1.0/2.0: 1, ACPI 3.0: 2, ACPI 4.0: 3 */
Stefan Reinauer06feb882004-02-03 16:11:35 +0000217
Uwe Hermann622824c2010-11-19 15:14:42 +0000218 madt->lapic_addr = LOCAL_APIC_ADDR;
219 madt->flags = 0x1; /* PCAT_COMPAT */
Stefan Reinauer06feb882004-02-03 16:11:35 +0000220
Stefan Reinauerf622d592005-11-26 16:56:05 +0000221 current = acpi_fill_madt(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000222
Uwe Hermann622824c2010-11-19 15:14:42 +0000223 /* (Re)calculate length and checksum. */
224 header->length = current - (unsigned long)madt;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000225
Uwe Hermann622824c2010-11-19 15:14:42 +0000226 header->checksum = acpi_checksum((void *)madt, header->length);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000227}
228
Uwe Hermann622824c2010-11-19 15:14:42 +0000229/* MCFG is defined in the PCI Firmware Specification 3.0. */
Rudolf Mareke6409f22007-11-03 12:50:26 +0000230void acpi_create_mcfg(acpi_mcfg_t *mcfg)
231{
Uwe Hermann622824c2010-11-19 15:14:42 +0000232 acpi_header_t *header = &(mcfg->header);
233 unsigned long current = (unsigned long)mcfg + sizeof(acpi_mcfg_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000234
Rudolf Mareke6409f22007-11-03 12:50:26 +0000235 memset((void *)mcfg, 0, sizeof(acpi_mcfg_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000236
Uwe Hermann622824c2010-11-19 15:14:42 +0000237 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000238 memcpy(header->signature, "MCFG", 4);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000239 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000240 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000241 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000242
Rudolf Mareke6409f22007-11-03 12:50:26 +0000243 header->length = sizeof(acpi_mcfg_t);
244 header->revision = 1;
245
246 current = acpi_fill_mcfg(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000247
Uwe Hermann622824c2010-11-19 15:14:42 +0000248 /* (Re)calculate length and checksum. */
249 header->length = current - (unsigned long)mcfg;
250 header->checksum = acpi_checksum((void *)mcfg, header->length);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000251}
252
Myles Watson3fe6b702009-10-09 20:13:43 +0000253void acpi_create_ssdt_generator(acpi_header_t *ssdt, const char *oem_table_id)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000254{
Uwe Hermann622824c2010-11-19 15:14:42 +0000255 unsigned long current = (unsigned long)ssdt + sizeof(acpi_header_t);
256
Rudolf Marek293b5f52009-02-01 18:35:15 +0000257 memset((void *)ssdt, 0, sizeof(acpi_header_t));
Uwe Hermann622824c2010-11-19 15:14:42 +0000258
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000259 memcpy(&ssdt->signature, "SSDT", 4);
Uwe Hermann622824c2010-11-19 15:14:42 +0000260 ssdt->revision = 2; /* ACPI 1.0/2.0: ?, ACPI 3.0/4.0: 2 */
Rudolf Marek293b5f52009-02-01 18:35:15 +0000261 memcpy(&ssdt->oem_id, OEM_ID, 6);
262 memcpy(&ssdt->oem_table_id, oem_table_id, 8);
263 ssdt->oem_revision = 42;
Uwe Hermann622824c2010-11-19 15:14:42 +0000264 memcpy(&ssdt->asl_compiler_id, ASLC, 4);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000265 ssdt->asl_compiler_revision = 42;
266 ssdt->length = sizeof(acpi_header_t);
267
Stefan Reinauer8dcd50b2009-03-06 17:24:29 +0000268 acpigen_set_current((char *) current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200269 {
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100270 struct device *dev;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200271 for (dev = all_devices; dev; dev = dev->next)
272 if (dev->ops && dev->ops->acpi_fill_ssdt_generator) {
Vladimir Serbinenko0a669912014-10-05 14:34:17 +0200273 dev->ops->acpi_fill_ssdt_generator();
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200274 }
Vladimir Serbinenko0a669912014-10-05 14:34:17 +0200275 current = (unsigned long) acpigen_get_current();
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200276 }
Rudolf Marek293b5f52009-02-01 18:35:15 +0000277
Uwe Hermann622824c2010-11-19 15:14:42 +0000278 /* (Re)calculate length and checksum. */
Rudolf Marek293b5f52009-02-01 18:35:15 +0000279 ssdt->length = current - (unsigned long)ssdt;
280 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
281}
282
Stefan Reinauerf622d592005-11-26 16:56:05 +0000283int acpi_create_srat_lapic(acpi_srat_lapic_t *lapic, u8 node, u8 apic)
284{
Myles Watson0b4c9f02009-03-10 18:06:47 +0000285 memset((void *)lapic, 0, sizeof(acpi_srat_lapic_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000286
Uwe Hermann622824c2010-11-19 15:14:42 +0000287 lapic->type = 0; /* Processor local APIC/SAPIC affinity structure */
288 lapic->length = sizeof(acpi_srat_lapic_t);
289 lapic->flags = (1 << 0); /* Enabled (the use of this structure). */
290 lapic->proximity_domain_7_0 = node;
291 /* TODO: proximity_domain_31_8, local SAPIC EID, clock domain. */
292 lapic->apic_id = apic;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000293
Uwe Hermann622824c2010-11-19 15:14:42 +0000294 return lapic->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000295}
296
Uwe Hermann622824c2010-11-19 15:14:42 +0000297int 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 +0300298 u32 flags)
Stefan Reinauerf622d592005-11-26 16:56:05 +0000299{
Uwe Hermann622824c2010-11-19 15:14:42 +0000300 mem->type = 1; /* Memory affinity structure */
301 mem->length = sizeof(acpi_srat_mem_t);
302 mem->base_address_low = (basek << 10);
303 mem->base_address_high = (basek >> (32 - 10));
304 mem->length_low = (sizek << 10);
305 mem->length_high = (sizek >> (32 - 10));
306 mem->proximity_domain = node;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000307 mem->flags = flags;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000308
Uwe Hermann622824c2010-11-19 15:14:42 +0000309 return mem->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000310}
311
Uwe Hermann622824c2010-11-19 15:14:42 +0000312/* http://www.microsoft.com/whdc/system/sysinternals/sratdwn.mspx */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +0200313void acpi_create_srat(acpi_srat_t *srat,
314 unsigned long (*acpi_fill_srat)(unsigned long current))
Stefan Reinauerf622d592005-11-26 16:56:05 +0000315{
Uwe Hermann622824c2010-11-19 15:14:42 +0000316 acpi_header_t *header = &(srat->header);
317 unsigned long current = (unsigned long)srat + sizeof(acpi_srat_t);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000318
Uwe Hermann622824c2010-11-19 15:14:42 +0000319 memset((void *)srat, 0, sizeof(acpi_srat_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000320
Uwe Hermann622824c2010-11-19 15:14:42 +0000321 /* Fill out header fields. */
322 memcpy(header->signature, "SRAT", 4);
323 memcpy(header->oem_id, OEM_ID, 6);
324 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
325 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000326
Uwe Hermann622824c2010-11-19 15:14:42 +0000327 header->length = sizeof(acpi_srat_t);
328 header->revision = 1; /* ACPI 1.0: N/A, 2.0: 1, 3.0: 2, 4.0: 3 */
Stefan Reinauerf622d592005-11-26 16:56:05 +0000329
Uwe Hermann622824c2010-11-19 15:14:42 +0000330 srat->resv = 1; /* Spec: Reserved to 1 for backwards compatibility. */
Stefan Reinauerf622d592005-11-26 16:56:05 +0000331
Uwe Hermann622824c2010-11-19 15:14:42 +0000332 current = acpi_fill_srat(current);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000333
Uwe Hermann622824c2010-11-19 15:14:42 +0000334 /* (Re)calculate length and checksum. */
335 header->length = current - (unsigned long)srat;
336 header->checksum = acpi_checksum((void *)srat, header->length);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000337}
338
Vladimir Serbinenko8d70e942014-11-09 13:22:27 +0100339void acpi_create_dmar(acpi_dmar_t *dmar,
340 unsigned long (*acpi_fill_dmar) (unsigned long))
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200341{
342 acpi_header_t *header = &(dmar->header);
343 unsigned long current = (unsigned long)dmar + sizeof(acpi_dmar_t);
344
345 memset((void *)dmar, 0, sizeof(acpi_dmar_t));
346
347 /* Fill out header fields. */
348 memcpy(header->signature, "DMAR", 4);
349 memcpy(header->oem_id, OEM_ID, 6);
350 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
351 memcpy(header->asl_compiler_id, ASLC, 4);
352
353 header->length = sizeof(acpi_dmar_t);
354 header->revision = 1;
355
356 dmar->host_address_width = 40 - 1; /* FIXME: == MTRR size? */
357 dmar->flags = 0;
358
359 current = acpi_fill_dmar(current);
360
361 /* (Re)calculate length and checksum. */
362 header->length = current - (unsigned long)dmar;
363 header->checksum = acpi_checksum((void *)dmar, header->length);
364}
365
366unsigned long acpi_create_dmar_drhd(unsigned long current, u8 flags,
367 u16 segment, u32 bar)
368{
369 dmar_entry_t *drhd = (dmar_entry_t *)current;
370 memset(drhd, 0, sizeof(*drhd));
371 drhd->type = DMAR_DRHD;
372 drhd->length = sizeof(*drhd); /* will be fixed up later */
373 drhd->flags = flags;
374 drhd->segment = segment;
375 drhd->bar = bar;
376
377 return drhd->length;
378}
379
380void acpi_dmar_drhd_fixup(unsigned long base, unsigned long current)
381{
382 dmar_entry_t *drhd = (dmar_entry_t *)base;
383 drhd->length = current - base;
384}
385
386unsigned long acpi_create_dmar_drhd_ds_pci(unsigned long current, u8 segment,
387 u8 dev, u8 fn)
388{
389 dev_scope_t *ds = (dev_scope_t *)current;
390 memset(ds, 0, sizeof(*ds));
391 ds->type = SCOPE_PCI_ENDPOINT;
392 ds->length = sizeof(*ds) + 2; /* we don't support longer paths yet */
393 ds->start_bus = segment;
394 ds->path[0].dev = dev;
395 ds->path[0].fn = fn;
396
397 return ds->length;
398}
399
Uwe Hermann622824c2010-11-19 15:14:42 +0000400/* http://h21007.www2.hp.com/portal/download/files/unprot/Itanium/slit.pdf */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +0200401void acpi_create_slit(acpi_slit_t *slit,
402 unsigned long (*acpi_fill_slit)(unsigned long current))
Yinghai Lud4b278c2006-10-04 20:46:15 +0000403{
Uwe Hermann622824c2010-11-19 15:14:42 +0000404 acpi_header_t *header = &(slit->header);
405 unsigned long current = (unsigned long)slit + sizeof(acpi_slit_t);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000406
Uwe Hermann622824c2010-11-19 15:14:42 +0000407 memset((void *)slit, 0, sizeof(acpi_slit_t));
Yinghai Lud4b278c2006-10-04 20:46:15 +0000408
Uwe Hermann622824c2010-11-19 15:14:42 +0000409 /* Fill out header fields. */
410 memcpy(header->signature, "SLIT", 4);
411 memcpy(header->oem_id, OEM_ID, 6);
412 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
413 memcpy(header->asl_compiler_id, ASLC, 4);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000414
Uwe Hermann622824c2010-11-19 15:14:42 +0000415 header->length = sizeof(acpi_slit_t);
416 header->revision = 1; /* ACPI 1.0: N/A, ACPI 2.0/3.0/4.0: 1 */
Yinghai Lud4b278c2006-10-04 20:46:15 +0000417
Uwe Hermann622824c2010-11-19 15:14:42 +0000418 current = acpi_fill_slit(current);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000419
Uwe Hermann622824c2010-11-19 15:14:42 +0000420 /* (Re)calculate length and checksum. */
421 header->length = current - (unsigned long)slit;
422 header->checksum = acpi_checksum((void *)slit, header->length);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000423}
424
Uwe Hermann622824c2010-11-19 15:14:42 +0000425/* http://www.intel.com/hardwaredesign/hpetspec_1.pdf */
Stefan Reinauer777224c2005-01-19 14:06:41 +0000426void acpi_create_hpet(acpi_hpet_t *hpet)
Stefan Reinauer688b3852004-01-28 16:56:14 +0000427{
Uwe Hermann622824c2010-11-19 15:14:42 +0000428 acpi_header_t *header = &(hpet->header);
429 acpi_addr_t *addr = &(hpet->addr);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000430
Stefan Reinauera7648c22004-01-29 17:31:34 +0000431 memset((void *)hpet, 0, sizeof(acpi_hpet_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000432
Uwe Hermann622824c2010-11-19 15:14:42 +0000433 /* Fill out header fields. */
Stefan Reinauer4704dc52009-07-22 01:11:37 +0000434 memcpy(header->signature, "HPET", 4);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000435 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000436 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000437 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000438
Stefan Reinauer688b3852004-01-28 16:56:14 +0000439 header->length = sizeof(acpi_hpet_t);
Uwe Hermann622824c2010-11-19 15:14:42 +0000440 header->revision = 1; /* Currently 1. Table added in ACPI 2.0. */
Stefan Reinauer688b3852004-01-28 16:56:14 +0000441
Uwe Hermann622824c2010-11-19 15:14:42 +0000442 /* Fill out HPET address. */
443 addr->space_id = 0; /* Memory */
444 addr->bit_width = 64;
445 addr->bit_offset = 0;
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200446 addr->addrl = CONFIG_HPET_ADDRESS & 0xffffffff;
447 addr->addrh = ((unsigned long long)CONFIG_HPET_ADDRESS) >> 32;
Stefan Reinauer688b3852004-01-28 16:56:14 +0000448
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200449 hpet->id = *(unsigned int*)CONFIG_HPET_ADDRESS;
Uwe Hermann622824c2010-11-19 15:14:42 +0000450 hpet->number = 0;
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200451 hpet->min_tick = CONFIG_HPET_MIN_TICKS;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000452
Uwe Hermann622824c2010-11-19 15:14:42 +0000453 header->checksum = acpi_checksum((void *)hpet, sizeof(acpi_hpet_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000454}
Uwe Hermann622824c2010-11-19 15:14:42 +0000455
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200456unsigned long acpi_write_hpet(unsigned long current, acpi_rsdp_t *rsdp)
457{
458 acpi_hpet_t *hpet;
459
460 /*
461 * We explicitly add these tables later on:
462 */
463 printk(BIOS_DEBUG, "ACPI: * HPET\n");
464
465 hpet = (acpi_hpet_t *) current;
466 current += sizeof(acpi_hpet_t);
467 current = ALIGN(current, 16);
468 acpi_create_hpet(hpet);
469 acpi_add_table(rsdp, hpet);
470
471 return current;
472}
473
Stefan Reinauer777224c2005-01-19 14:06:41 +0000474void acpi_create_facs(acpi_facs_t *facs)
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000475{
Uwe Hermann622824c2010-11-19 15:14:42 +0000476 memset((void *)facs, 0, sizeof(acpi_facs_t));
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000477
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000478 memcpy(facs->signature, "FACS", 4);
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000479 facs->length = sizeof(acpi_facs_t);
480 facs->hardware_signature = 0;
481 facs->firmware_waking_vector = 0;
482 facs->global_lock = 0;
483 facs->flags = 0;
484 facs->x_firmware_waking_vector_l = 0;
485 facs->x_firmware_waking_vector_h = 0;
Uwe Hermann622824c2010-11-19 15:14:42 +0000486 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 +0000487}
Stefan Reinauer777224c2005-01-19 14:06:41 +0000488
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100489static void acpi_write_rsdt(acpi_rsdt_t *rsdt, char *oem_id, char *oem_table_id)
Stefan Reinauer14e22772010-04-27 06:56:47 +0000490{
Uwe Hermann622824c2010-11-19 15:14:42 +0000491 acpi_header_t *header = &(rsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000492
Uwe Hermann622824c2010-11-19 15:14:42 +0000493 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000494 memcpy(header->signature, "RSDT", 4);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100495 memcpy(header->oem_id, oem_id, 6);
496 memcpy(header->oem_table_id, oem_table_id, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000497 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000498
Stefan Reinauer688b3852004-01-28 16:56:14 +0000499 header->length = sizeof(acpi_rsdt_t);
Uwe Hermann622824c2010-11-19 15:14:42 +0000500 header->revision = 1; /* ACPI 1.0/2.0/3.0/4.0: 1 */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000501
Uwe Hermann622824c2010-11-19 15:14:42 +0000502 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauer688b3852004-01-28 16:56:14 +0000503
Uwe Hermann622824c2010-11-19 15:14:42 +0000504 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000505 header->checksum = acpi_checksum((void *)rsdt, sizeof(acpi_rsdt_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000506}
507
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100508static void acpi_write_xsdt(acpi_xsdt_t *xsdt, char *oem_id, char *oem_table_id)
Stefan Reinauer14e22772010-04-27 06:56:47 +0000509{
Uwe Hermann622824c2010-11-19 15:14:42 +0000510 acpi_header_t *header = &(xsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000511
Uwe Hermann622824c2010-11-19 15:14:42 +0000512 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000513 memcpy(header->signature, "XSDT", 4);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100514 memcpy(header->oem_id, oem_id, 6);
515 memcpy(header->oem_table_id, oem_table_id, 8);
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000516 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000517
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000518 header->length = sizeof(acpi_xsdt_t);
Uwe Hermann622824c2010-11-19 15:14:42 +0000519 header->revision = 1; /* ACPI 1.0: N/A, 2.0/3.0/4.0: 1 */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000520
Uwe Hermann622824c2010-11-19 15:14:42 +0000521 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000522
Uwe Hermann622824c2010-11-19 15:14:42 +0000523 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000524 header->checksum = acpi_checksum((void *)xsdt, sizeof(acpi_xsdt_t));
525}
526
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100527static void acpi_write_rsdp(acpi_rsdp_t *rsdp, acpi_rsdt_t *rsdt,
528 acpi_xsdt_t *xsdt, char *oem_id)
Stefan Reinauer688b3852004-01-28 16:56:14 +0000529{
Stefan Reinauerd18faac2009-11-05 18:06:43 +0000530 memset(rsdp, 0, sizeof(acpi_rsdp_t));
Uwe Hermann622824c2010-11-19 15:14:42 +0000531
Stefan Reinauer688b3852004-01-28 16:56:14 +0000532 memcpy(rsdp->signature, RSDP_SIG, 8);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100533 memcpy(rsdp->oem_id, oem_id, 6);
Uwe Hermann622824c2010-11-19 15:14:42 +0000534
535 rsdp->length = sizeof(acpi_rsdp_t);
536 rsdp->rsdt_address = (u32)rsdt;
537
538 /*
539 * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2.
540 *
541 * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
542 * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
543 * revision 0).
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000544 */
545 if (xsdt == NULL) {
Uwe Hermann622824c2010-11-19 15:14:42 +0000546 rsdp->revision = 0;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000547 } else {
Uwe Hermann622824c2010-11-19 15:14:42 +0000548 rsdp->xsdt_address = (u64)(u32)xsdt;
549 rsdp->revision = 2;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000550 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000551
552 /* Calculate checksums. */
553 rsdp->checksum = acpi_checksum((void *)rsdp, 20);
554 rsdp->ext_checksum = acpi_checksum((void *)rsdp, sizeof(acpi_rsdp_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000555}
556
zbaocaf494c82012-04-13 13:57:14 +0800557unsigned long __attribute__((weak)) acpi_fill_hest(acpi_hest_t *hest)
558{
559 return (unsigned long)hest;
560}
561
562unsigned long acpi_create_hest_error_source(acpi_hest_t *hest, acpi_hest_esd_t *esd, u16 type, void *data, u16 data_len)
563{
564 acpi_header_t *header = &(hest->header);
565 acpi_hest_hen_t *hen;
566 void *pos;
567 u16 len;
568
569 pos = esd;
570 memset(pos, 0, sizeof(acpi_hest_esd_t));
571 len = 0;
572 esd->type = type; /* MCE */
573 esd->source_id = hest->error_source_count;
574 esd->flags = 0; /* FIRMWARE_FIRST */
575 esd->enabled = 1;
576 esd->prealloc_erecords = 1;
577 esd->max_section_per_record = 0x1;
578
579 len += sizeof(acpi_hest_esd_t);
580 pos = esd + 1;
581
582 switch (type) {
583 case 0: /* MCE */
584 break;
585 case 1: /* CMC */
586 hen = (acpi_hest_hen_t *) (pos);
587 memset(pos, 0, sizeof(acpi_hest_hen_t));
588 hen->type = 3; /* SCI? */
589 hen->length = sizeof(acpi_hest_hen_t);
590 hen->conf_we = 0; /* Configuration Write Enable. */
591 hen->poll_interval = 0;
592 hen->vector = 0;
593 hen->sw2poll_threshold_val = 0;
594 hen->sw2poll_threshold_win = 0;
595 hen->error_threshold_val = 0;
596 hen->error_threshold_win = 0;
597 len += sizeof(acpi_hest_hen_t);
598 pos = hen + 1;
599 break;
600 case 2: /* NMI */
601 case 6: /* AER Root Port */
602 case 7: /* AER Endpoint */
603 case 8: /* AER Bridge */
604 case 9: /* Generic Hardware Error Source. */
605 /* TODO: */
606 break;
607 default:
608 printk(BIOS_DEBUG, "Invalid type of Error Source.");
609 break;
610 }
611 hest->error_source_count ++;
612
613 memcpy(pos, data, data_len);
614 len += data_len;
615 header->length += len;
616
617 return len;
618}
619
620/* ACPI 4.0 */
621void acpi_write_hest(acpi_hest_t *hest)
622{
623 acpi_header_t *header = &(hest->header);
624
625 memset(hest, 0, sizeof(acpi_hest_t));
626
627 memcpy(header->signature, "HEST", 4);
628 memcpy(header->oem_id, OEM_ID, 6);
629 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
630 memcpy(header->asl_compiler_id, ASLC, 4);
631 header->length += sizeof(acpi_hest_t);
632 header->revision = 1;
633
634 acpi_fill_hest(hest);
635
636 /* Calculate checksums. */
637 header->checksum = acpi_checksum((void *)hest, header->length);
638}
639
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +0200640#if IS_ENABLED(CONFIG_COMMON_FADT)
641void acpi_create_fadt(acpi_fadt_t *fadt,acpi_facs_t *facs, void *dsdt)
642{
643 acpi_header_t *header = &(fadt->header);
644
645 memset((void *) fadt, 0, sizeof(acpi_fadt_t));
646 memcpy(header->signature, "FACP", 4);
647 header->length = sizeof(acpi_fadt_t);
Vladimir Serbinenkoab83ef02014-10-25 15:18:25 +0200648 header->revision = 4;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +0200649 memcpy(header->oem_id, OEM_ID, 6);
650 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
651 memcpy(header->asl_compiler_id, ASLC, 4);
652 header->asl_compiler_revision = 0;
653
654 fadt->firmware_ctrl = (unsigned long) facs;
655 fadt->dsdt = (unsigned long) dsdt;
656
657 fadt->x_firmware_ctl_l = (unsigned long)facs;
658 fadt->x_firmware_ctl_h = 0;
659 fadt->x_dsdt_l = (unsigned long)dsdt;
660 fadt->x_dsdt_h = 0;
661
662 if(IS_ENABLED(CONFIG_SYSTEM_TYPE_LAPTOP)) {
663 fadt->preferred_pm_profile = PM_MOBILE;
664 } else {
665 fadt->preferred_pm_profile = PM_DESKTOP;
666 }
667
Vladimir Serbinenkoab83ef02014-10-25 15:18:25 +0200668 acpi_fill_fadt(fadt);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +0200669
670 header->checksum =
671 acpi_checksum((void *) fadt, header->length);
672}
673#endif
674
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200675extern const unsigned char AmlCode[];
676
Vladimir Serbinenko41877d82014-09-01 22:18:01 +0200677unsigned long __attribute__ ((weak)) fw_cfg_acpi_tables(unsigned long start)
678{
679 return 0;
680}
681
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200682#define ALIGN_CURRENT current = (ALIGN(current, 16))
683unsigned long write_acpi_tables(unsigned long start)
684{
685 unsigned long current;
686 acpi_rsdp_t *rsdp;
687 acpi_rsdt_t *rsdt;
688 acpi_xsdt_t *xsdt;
689 acpi_fadt_t *fadt;
690 acpi_facs_t *facs;
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100691 acpi_header_t *slic_file, *slic;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200692 acpi_header_t *ssdt;
693 acpi_header_t *dsdt;
694 acpi_mcfg_t *mcfg;
695 acpi_madt_t *madt;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100696 struct device *dev;
Vladimir Serbinenko41877d82014-09-01 22:18:01 +0200697 unsigned long fw;
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100698 size_t slic_size;
699 char oem_id[6], oem_table_id[8];
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200700
701 current = start;
702
703 /* Align ACPI tables to 16byte */
704 ALIGN_CURRENT;
705
Vladimir Serbinenko41877d82014-09-01 22:18:01 +0200706 fw = fw_cfg_acpi_tables(current);
707 if (fw)
708 return fw;
709
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100710 slic_file = cbfs_get_file_content(CBFS_DEFAULT_MEDIA,
711 CONFIG_CBFS_PREFIX "/slic",
712 CBFS_TYPE_RAW, &slic_size);
713 if (slic_file && (slic_file->length > slic_size
714 || slic_file->length < sizeof (acpi_header_t))) {
715 slic_file = 0;
716 }
717
718 if (slic_file) {
719 memcpy(oem_id, slic_file->oem_id, 6);
720 memcpy(oem_table_id, slic_file->oem_table_id, 8);
721 } else {
722 memcpy(oem_id, OEM_ID, 6);
723 memcpy(oem_table_id, ACPI_TABLE_CREATOR, 8);
724 }
725
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200726 printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx.\n", start);
727
728 /* We need at least an RSDP and an RSDT Table */
729 rsdp = (acpi_rsdp_t *) current;
730 current += sizeof(acpi_rsdp_t);
731 ALIGN_CURRENT;
732 rsdt = (acpi_rsdt_t *) current;
733 current += sizeof(acpi_rsdt_t);
734 ALIGN_CURRENT;
735 xsdt = (acpi_xsdt_t *) current;
736 current += sizeof(acpi_xsdt_t);
737 ALIGN_CURRENT;
738
739 /* clear all table memory */
740 memset((void *) start, 0, current - start);
741
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100742 acpi_write_rsdp(rsdp, rsdt, xsdt, oem_id);
743 acpi_write_rsdt(rsdt, oem_id, oem_table_id);
744 acpi_write_xsdt(xsdt, oem_id, oem_table_id);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200745
746 printk(BIOS_DEBUG, "ACPI: * FACS\n");
747 facs = (acpi_facs_t *) current;
748 current += sizeof(acpi_facs_t);
749 ALIGN_CURRENT;
750 acpi_create_facs(facs);
751
752 printk(BIOS_DEBUG, "ACPI: * DSDT\n");
753 dsdt = (acpi_header_t *) current;
754 memcpy(dsdt, &AmlCode, sizeof(acpi_header_t));
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +0200755 if (dsdt->length >= sizeof(acpi_header_t)) {
756 current += sizeof(acpi_header_t);
757
758 acpigen_set_current((char *) current);
759 for (dev = all_devices; dev; dev = dev->next)
760 if (dev->ops && dev->ops->acpi_inject_dsdt_generator) {
761 dev->ops->acpi_inject_dsdt_generator();
762 }
763 current = (unsigned long) acpigen_get_current();
764 memcpy((char *)current,
765 (char *)&AmlCode + sizeof(acpi_header_t),
766 dsdt->length - sizeof(acpi_header_t));
767 current += dsdt->length - sizeof(acpi_header_t);
768
769 /* (Re)calculate length and checksum. */
770 dsdt->length = current - (unsigned long)dsdt;
771 dsdt->checksum = 0;
772 dsdt->checksum = acpi_checksum((void *)dsdt, dsdt->length);
773 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200774
775 ALIGN_CURRENT;
776
777 printk(BIOS_DEBUG, "ACPI: * FADT\n");
778 fadt = (acpi_fadt_t *) current;
779 current += sizeof(acpi_fadt_t);
780 ALIGN_CURRENT;
781
782 acpi_create_fadt(fadt, facs, dsdt);
783 acpi_add_table(rsdp, fadt);
784
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100785 if (slic_file) {
786 printk(BIOS_DEBUG, "ACPI: * SLIC\n");
787 slic = (acpi_header_t *)current;
788 memcpy(slic, slic_file, slic_file->length);
789 current += slic_file->length;
790 ALIGN_CURRENT;
791 acpi_add_table(rsdp, slic);
792 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200793
794 printk(BIOS_DEBUG, "ACPI: * SSDT\n");
795 ssdt = (acpi_header_t *)current;
796 acpi_create_ssdt_generator(ssdt, ACPI_TABLE_CREATOR);
Vladimir Serbinenko9310df82014-10-10 20:40:41 +0200797 if (ssdt->length > sizeof(acpi_header_t)) {
798 current += ssdt->length;
799 acpi_add_table(rsdp, ssdt);
800 ALIGN_CURRENT;
801 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200802
803 printk(BIOS_DEBUG, "ACPI: * MCFG\n");
804 mcfg = (acpi_mcfg_t *) current;
805 acpi_create_mcfg(mcfg);
806 if (mcfg->header.length > sizeof(acpi_mcfg_t)) {
807 current += mcfg->header.length;
808 ALIGN_CURRENT;
809 acpi_add_table(rsdp, mcfg);
810 }
811
812 printk(BIOS_DEBUG, "ACPI: * MADT\n");
813
814 madt = (acpi_madt_t *) current;
815 acpi_create_madt(madt);
816 if (madt->header.length > sizeof(acpi_madt_t)) {
817 current+=madt->header.length;
818 acpi_add_table(rsdp,madt);
819 }
820 ALIGN_CURRENT;
821
822 printk(BIOS_DEBUG, "current = %lx\n", current);
823
824 for (dev = all_devices; dev; dev = dev->next) {
825 if (dev->ops && dev->ops->write_acpi_tables) {
826 current = dev->ops->write_acpi_tables(current, rsdp);
827 ALIGN_CURRENT;
828 }
829 }
830
831 printk(BIOS_INFO, "ACPI: done.\n");
832 return current;
833}
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200834
Patrick Georgie1667822012-05-05 15:29:32 +0200835#if CONFIG_HAVE_ACPI_RESUME
Kyösti Mälkki134f5042014-12-26 13:29:09 +0200836void __attribute__((weak)) mainboard_suspend_resume(void)
837{
838}
839
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500840void acpi_resume(void *wake_vec)
Stefan Reinauer7e9771c2009-04-22 08:17:38 +0000841{
Duncan Laurie11290c42012-10-03 19:07:05 -0700842#if CONFIG_HAVE_SMI_HANDLER
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500843 u32 *gnvs_address = cbmem_find(CBMEM_ID_ACPI_GNVS_PTR);
Duncan Laurie11290c42012-10-03 19:07:05 -0700844
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500845 /* Restore GNVS pointer in SMM if found */
846 if (gnvs_address && *gnvs_address) {
847 printk(BIOS_DEBUG, "Restore GNVS pointer to 0x%08x\n",
848 *gnvs_address);
849 smm_setup_structures((void *)*gnvs_address, NULL, NULL);
Stefan Reinauercb91e152012-04-03 23:28:22 +0200850 }
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500851#endif
852
853 /* Call mainboard resume handler first, if defined. */
Kyösti Mälkki134f5042014-12-26 13:29:09 +0200854 mainboard_suspend_resume();
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500855
856 post_code(POST_OS_RESUME);
857 acpi_jump_to_wakeup(wake_vec);
Stefan Reinauer7e9771c2009-04-22 08:17:38 +0000858}
859
Kyösti Mälkki7a846e72015-01-09 23:55:09 +0200860/* This is filled with acpi_is_wakeup() call early in ramstage. */
861int acpi_slp_type = -1;
Stefan Reinauer688b3852004-01-28 16:56:14 +0000862
Kyösti Mälkki9e94dbf2015-01-08 20:03:18 +0200863#if IS_ENABLED(CONFIG_EARLY_CBMEM_INIT)
864int acpi_get_sleep_type(void)
Kyösti Mälkki134f5042014-12-26 13:29:09 +0200865{
Kyösti Mälkki9e94dbf2015-01-08 20:03:18 +0200866 struct romstage_handoff *handoff;
867
868 handoff = cbmem_find(CBMEM_ID_ROMSTAGE_INFO);
869
870 if (handoff == NULL) {
871 printk(BIOS_DEBUG, "Unknown boot method, assuming normal.\n");
872 return 0;
873 } else if (handoff->s3_resume) {
874 printk(BIOS_DEBUG, "S3 Resume.\n");
875 return 3;
876 } else {
877 printk(BIOS_DEBUG, "Normal boot.\n");
878 return 0;
879 }
Kyösti Mälkki134f5042014-12-26 13:29:09 +0200880}
Kyösti Mälkki9e94dbf2015-01-08 20:03:18 +0200881#endif
Kyösti Mälkki134f5042014-12-26 13:29:09 +0200882
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +0300883static void acpi_handoff_wakeup(void)
884{
Kyösti Mälkki7a846e72015-01-09 23:55:09 +0200885 if (acpi_slp_type < 0)
886 acpi_slp_type = acpi_get_sleep_type();
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +0300887}
888
Kyösti Mälkkicb28f3f2014-01-03 15:15:22 +0200889int acpi_is_wakeup(void)
Rudolf Marek33cafe52009-04-13 18:07:02 +0000890{
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +0300891 acpi_handoff_wakeup();
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +0100892 /* Both resume from S2 and resume from S3 restart at CPU reset */
893 return (acpi_slp_type == 3 || acpi_slp_type == 2);
Rudolf Marek33cafe52009-04-13 18:07:02 +0000894}
895
Kyösti Mälkki4d9b7722014-06-19 19:45:40 +0300896int acpi_is_wakeup_s3(void)
897{
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +0300898 acpi_handoff_wakeup();
Kyösti Mälkki4d9b7722014-06-19 19:45:40 +0300899 return (acpi_slp_type == 3);
900}
901
Kyösti Mälkkicb28f3f2014-01-03 15:15:22 +0200902void acpi_fail_wakeup(void)
903{
904 if (acpi_slp_type == 3 || acpi_slp_type == 2)
905 acpi_slp_type = 0;
906}
907
Kyösti Mälkki7b14f082014-10-16 20:58:47 +0300908void acpi_prepare_resume_backup(void)
909{
910 if (!acpi_s3_resume_allowed())
911 return;
912
913 /* Let's prepare the ACPI S3 Resume area now already, so we can rely on
914 * it being there during reboot time. We don't need the pointer, nor
915 * the result right now. If it fails, ACPI resume will be disabled.
916 */
917
918 if (HIGH_MEMORY_SAVE)
919 cbmem_add(CBMEM_ID_RESUME, HIGH_MEMORY_SAVE);
920
921 if (HIGH_MEMORY_SCRATCH)
922 cbmem_add(CBMEM_ID_RESUME_SCRATCH, HIGH_MEMORY_SCRATCH);
923}
924
Rudolf Marek33cafe52009-04-13 18:07:02 +0000925static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp)
926{
927 if (strncmp((char *)rsdp, RSDP_SIG, sizeof(RSDP_SIG) - 1) != 0)
928 return NULL;
929
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000930 printk(BIOS_DEBUG, "Looking on %p for valid checksum\n", rsdp);
Rudolf Marek33cafe52009-04-13 18:07:02 +0000931
932 if (acpi_checksum((void *)rsdp, 20) != 0)
933 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000934 printk(BIOS_DEBUG, "Checksum 1 passed\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +0000935
936 if ((rsdp->revision > 1) && (acpi_checksum((void *)rsdp,
937 rsdp->length) != 0))
938 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000939 printk(BIOS_DEBUG, "Checksum 2 passed all OK\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +0000940
941 return rsdp;
942}
943
944static acpi_rsdp_t *rsdp;
945
946void *acpi_get_wakeup_rsdp(void)
947{
948 return rsdp;
949}
950
951void *acpi_find_wakeup_vector(void)
952{
953 char *p, *end;
Rudolf Marek33cafe52009-04-13 18:07:02 +0000954 acpi_rsdt_t *rsdt;
955 acpi_facs_t *facs;
Martin Rothc5f49262012-12-14 19:17:55 -0700956 acpi_fadt_t *fadt = NULL;
Uwe Hermann622824c2010-11-19 15:14:42 +0000957 void *wake_vec;
Rudolf Marek33cafe52009-04-13 18:07:02 +0000958 int i;
959
960 rsdp = NULL;
961
962 if (!acpi_is_wakeup())
963 return NULL;
964
Uwe Hermann622824c2010-11-19 15:14:42 +0000965 printk(BIOS_DEBUG, "Trying to find the wakeup vector...\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +0000966
Uwe Hermann622824c2010-11-19 15:14:42 +0000967 /* Find RSDP. */
968 for (p = (char *)0xe0000; p < (char *)0xfffff; p += 16) {
969 if ((rsdp = valid_rsdp((acpi_rsdp_t *)p)))
Rudolf Marek33cafe52009-04-13 18:07:02 +0000970 break;
971 }
972
973 if (rsdp == NULL)
974 return NULL;
975
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000976 printk(BIOS_DEBUG, "RSDP found at %p\n", rsdp);
Rudolf Marek33cafe52009-04-13 18:07:02 +0000977 rsdt = (acpi_rsdt_t *) rsdp->rsdt_address;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000978
Uwe Hermann622824c2010-11-19 15:14:42 +0000979 end = (char *)rsdt + rsdt->header.length;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000980 printk(BIOS_DEBUG, "RSDT found at %p ends at %p\n", rsdt, end);
Rudolf Marek33cafe52009-04-13 18:07:02 +0000981
Uwe Hermann622824c2010-11-19 15:14:42 +0000982 for (i = 0; ((char *)&rsdt->entry[i]) < end; i++) {
983 fadt = (acpi_fadt_t *)rsdt->entry[i];
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000984 if (strncmp((char *)fadt, "FACP", 4) == 0)
Rudolf Marek33cafe52009-04-13 18:07:02 +0000985 break;
986 fadt = NULL;
987 }
988
989 if (fadt == NULL)
990 return NULL;
991
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000992 printk(BIOS_DEBUG, "FADT found at %p\n", fadt);
Stefan Reinauer69390db2009-05-26 12:33:52 +0000993 facs = (acpi_facs_t *)fadt->firmware_ctrl;
Rudolf Marek33cafe52009-04-13 18:07:02 +0000994
Stefan Reinauer7e9771c2009-04-22 08:17:38 +0000995 if (facs == NULL) {
Uwe Hermann622824c2010-11-19 15:14:42 +0000996 printk(BIOS_DEBUG, "No FACS found, wake up from S3 not "
997 "possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +0000998 return NULL;
Stefan Reinauer7e9771c2009-04-22 08:17:38 +0000999 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001000
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001001 printk(BIOS_DEBUG, "FACS found at %p\n", facs);
Uwe Hermann622824c2010-11-19 15:14:42 +00001002 wake_vec = (void *)facs->firmware_waking_vector;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001003 printk(BIOS_DEBUG, "OS waking vector is %p\n", wake_vec);
Uwe Hermann622824c2010-11-19 15:14:42 +00001004
Rudolf Marek33cafe52009-04-13 18:07:02 +00001005 return wake_vec;
1006}
1007
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +01001008#if CONFIG_SMP
Rudolf Marek33cafe52009-04-13 18:07:02 +00001009extern char *lowmem_backup;
1010extern char *lowmem_backup_ptr;
1011extern int lowmem_backup_size;
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +01001012#endif
Rudolf Marek33cafe52009-04-13 18:07:02 +00001013
Uwe Hermann622824c2010-11-19 15:14:42 +00001014#define WAKEUP_BASE 0x600
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001015
Uwe Hermann622824c2010-11-19 15:14:42 +00001016void (*acpi_do_wakeup)(u32 vector, u32 backup_source, u32 backup_target,
Stefan Reinauer399486e2012-12-06 13:54:29 -08001017 u32 backup_size) asmlinkage = (void *)WAKEUP_BASE;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001018
Aaron Durbina146d582013-02-08 16:56:51 -06001019extern unsigned char __wakeup;
1020extern unsigned int __wakeup_size;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001021
Rudolf Marek33cafe52009-04-13 18:07:02 +00001022void acpi_jump_to_wakeup(void *vector)
1023{
Aaron Durbin8e4a3552013-02-08 17:28:04 -06001024 u32 acpi_backup_memory = 0;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001025
Kyösti Mälkki2ca2afe2014-06-17 15:41:37 +03001026 if (HIGH_MEMORY_SAVE && acpi_s3_resume_allowed()) {
1027 acpi_backup_memory = (u32)cbmem_find(CBMEM_ID_RESUME);
1028
1029 if (!acpi_backup_memory) {
1030 printk(BIOS_WARNING, "ACPI: Backup memory missing. "
1031 "No S3 resume.\n");
1032 return;
1033 }
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001034 }
1035
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +01001036#if CONFIG_SMP
Martin Roth7b5f8ef2013-07-08 16:22:10 -06001037 // FIXME: This should go into the ACPI backup memory, too. No pork sausages.
Uwe Hermann622824c2010-11-19 15:14:42 +00001038 /*
1039 * Just restore the SMP trampoline and continue with wakeup on
1040 * assembly level.
1041 */
Rudolf Marek33cafe52009-04-13 18:07:02 +00001042 memcpy(lowmem_backup_ptr, lowmem_backup, lowmem_backup_size);
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +01001043#endif
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001044
Uwe Hermann622824c2010-11-19 15:14:42 +00001045 /* Copy wakeup trampoline in place. */
Aaron Durbina146d582013-02-08 16:56:51 -06001046 memcpy((void *)WAKEUP_BASE, &__wakeup, __wakeup_size);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001047
Duncan Lauriecde78012011-10-19 15:32:39 -07001048#if CONFIG_COLLECT_TIMESTAMPS
1049 timestamp_add_now(TS_ACPI_WAKE_JUMP);
1050#endif
1051
Uwe Hermann622824c2010-11-19 15:14:42 +00001052 acpi_do_wakeup((u32)vector, acpi_backup_memory, CONFIG_RAMBASE,
1053 HIGH_MEMORY_SAVE);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001054}
1055#endif
Duncan Laurie11290c42012-10-03 19:07:05 -07001056
1057void acpi_save_gnvs(u32 gnvs_address)
1058{
Duncan Laurie9c07c8f2013-03-22 11:08:39 -07001059 u32 *gnvs = cbmem_add(CBMEM_ID_ACPI_GNVS_PTR, sizeof(*gnvs));
Duncan Laurie11290c42012-10-03 19:07:05 -07001060 if (gnvs)
1061 *gnvs = gnvs_address;
1062}