blob: bb994e2a9d8f2fb1054cd978d12d0ee09e065a0f [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 acpi_create_hest_error_source(acpi_hest_t *hest, acpi_hest_esd_t *esd, u16 type, void *data, u16 data_len)
558{
559 acpi_header_t *header = &(hest->header);
560 acpi_hest_hen_t *hen;
561 void *pos;
562 u16 len;
563
564 pos = esd;
565 memset(pos, 0, sizeof(acpi_hest_esd_t));
566 len = 0;
567 esd->type = type; /* MCE */
568 esd->source_id = hest->error_source_count;
569 esd->flags = 0; /* FIRMWARE_FIRST */
570 esd->enabled = 1;
571 esd->prealloc_erecords = 1;
572 esd->max_section_per_record = 0x1;
573
574 len += sizeof(acpi_hest_esd_t);
575 pos = esd + 1;
576
577 switch (type) {
578 case 0: /* MCE */
579 break;
580 case 1: /* CMC */
581 hen = (acpi_hest_hen_t *) (pos);
582 memset(pos, 0, sizeof(acpi_hest_hen_t));
583 hen->type = 3; /* SCI? */
584 hen->length = sizeof(acpi_hest_hen_t);
585 hen->conf_we = 0; /* Configuration Write Enable. */
586 hen->poll_interval = 0;
587 hen->vector = 0;
588 hen->sw2poll_threshold_val = 0;
589 hen->sw2poll_threshold_win = 0;
590 hen->error_threshold_val = 0;
591 hen->error_threshold_win = 0;
592 len += sizeof(acpi_hest_hen_t);
593 pos = hen + 1;
594 break;
595 case 2: /* NMI */
596 case 6: /* AER Root Port */
597 case 7: /* AER Endpoint */
598 case 8: /* AER Bridge */
599 case 9: /* Generic Hardware Error Source. */
600 /* TODO: */
601 break;
602 default:
603 printk(BIOS_DEBUG, "Invalid type of Error Source.");
604 break;
605 }
606 hest->error_source_count ++;
607
608 memcpy(pos, data, data_len);
609 len += data_len;
610 header->length += len;
611
612 return len;
613}
614
615/* ACPI 4.0 */
Vladimir Serbinenko807127f2014-11-09 13:36:18 +0100616void acpi_write_hest(acpi_hest_t *hest,
617 unsigned long (*acpi_fill_hest)(acpi_hest_t *hest))
zbaocaf494c82012-04-13 13:57:14 +0800618{
619 acpi_header_t *header = &(hest->header);
620
621 memset(hest, 0, sizeof(acpi_hest_t));
622
623 memcpy(header->signature, "HEST", 4);
624 memcpy(header->oem_id, OEM_ID, 6);
625 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
626 memcpy(header->asl_compiler_id, ASLC, 4);
627 header->length += sizeof(acpi_hest_t);
628 header->revision = 1;
629
630 acpi_fill_hest(hest);
631
632 /* Calculate checksums. */
633 header->checksum = acpi_checksum((void *)hest, header->length);
634}
635
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +0200636#if IS_ENABLED(CONFIG_COMMON_FADT)
637void acpi_create_fadt(acpi_fadt_t *fadt,acpi_facs_t *facs, void *dsdt)
638{
639 acpi_header_t *header = &(fadt->header);
640
641 memset((void *) fadt, 0, sizeof(acpi_fadt_t));
642 memcpy(header->signature, "FACP", 4);
643 header->length = sizeof(acpi_fadt_t);
Vladimir Serbinenkoab83ef02014-10-25 15:18:25 +0200644 header->revision = 4;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +0200645 memcpy(header->oem_id, OEM_ID, 6);
646 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
647 memcpy(header->asl_compiler_id, ASLC, 4);
648 header->asl_compiler_revision = 0;
649
650 fadt->firmware_ctrl = (unsigned long) facs;
651 fadt->dsdt = (unsigned long) dsdt;
652
653 fadt->x_firmware_ctl_l = (unsigned long)facs;
654 fadt->x_firmware_ctl_h = 0;
655 fadt->x_dsdt_l = (unsigned long)dsdt;
656 fadt->x_dsdt_h = 0;
657
658 if(IS_ENABLED(CONFIG_SYSTEM_TYPE_LAPTOP)) {
659 fadt->preferred_pm_profile = PM_MOBILE;
660 } else {
661 fadt->preferred_pm_profile = PM_DESKTOP;
662 }
663
Vladimir Serbinenkoab83ef02014-10-25 15:18:25 +0200664 acpi_fill_fadt(fadt);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +0200665
666 header->checksum =
667 acpi_checksum((void *) fadt, header->length);
668}
669#endif
670
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200671extern const unsigned char AmlCode[];
672
Vladimir Serbinenko41877d82014-09-01 22:18:01 +0200673unsigned long __attribute__ ((weak)) fw_cfg_acpi_tables(unsigned long start)
674{
675 return 0;
676}
677
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200678#define ALIGN_CURRENT current = (ALIGN(current, 16))
679unsigned long write_acpi_tables(unsigned long start)
680{
681 unsigned long current;
682 acpi_rsdp_t *rsdp;
683 acpi_rsdt_t *rsdt;
684 acpi_xsdt_t *xsdt;
685 acpi_fadt_t *fadt;
686 acpi_facs_t *facs;
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100687 acpi_header_t *slic_file, *slic;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200688 acpi_header_t *ssdt;
689 acpi_header_t *dsdt;
690 acpi_mcfg_t *mcfg;
691 acpi_madt_t *madt;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100692 struct device *dev;
Vladimir Serbinenko41877d82014-09-01 22:18:01 +0200693 unsigned long fw;
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100694 size_t slic_size;
695 char oem_id[6], oem_table_id[8];
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200696
697 current = start;
698
699 /* Align ACPI tables to 16byte */
700 ALIGN_CURRENT;
701
Vladimir Serbinenko41877d82014-09-01 22:18:01 +0200702 fw = fw_cfg_acpi_tables(current);
703 if (fw)
704 return fw;
705
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100706 slic_file = cbfs_get_file_content(CBFS_DEFAULT_MEDIA,
707 CONFIG_CBFS_PREFIX "/slic",
708 CBFS_TYPE_RAW, &slic_size);
709 if (slic_file && (slic_file->length > slic_size
710 || slic_file->length < sizeof (acpi_header_t))) {
711 slic_file = 0;
712 }
713
714 if (slic_file) {
715 memcpy(oem_id, slic_file->oem_id, 6);
716 memcpy(oem_table_id, slic_file->oem_table_id, 8);
717 } else {
718 memcpy(oem_id, OEM_ID, 6);
719 memcpy(oem_table_id, ACPI_TABLE_CREATOR, 8);
720 }
721
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200722 printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx.\n", start);
723
724 /* We need at least an RSDP and an RSDT Table */
725 rsdp = (acpi_rsdp_t *) current;
726 current += sizeof(acpi_rsdp_t);
727 ALIGN_CURRENT;
728 rsdt = (acpi_rsdt_t *) current;
729 current += sizeof(acpi_rsdt_t);
730 ALIGN_CURRENT;
731 xsdt = (acpi_xsdt_t *) current;
732 current += sizeof(acpi_xsdt_t);
733 ALIGN_CURRENT;
734
735 /* clear all table memory */
736 memset((void *) start, 0, current - start);
737
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100738 acpi_write_rsdp(rsdp, rsdt, xsdt, oem_id);
739 acpi_write_rsdt(rsdt, oem_id, oem_table_id);
740 acpi_write_xsdt(xsdt, oem_id, oem_table_id);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200741
742 printk(BIOS_DEBUG, "ACPI: * FACS\n");
743 facs = (acpi_facs_t *) current;
744 current += sizeof(acpi_facs_t);
745 ALIGN_CURRENT;
746 acpi_create_facs(facs);
747
748 printk(BIOS_DEBUG, "ACPI: * DSDT\n");
749 dsdt = (acpi_header_t *) current;
750 memcpy(dsdt, &AmlCode, sizeof(acpi_header_t));
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +0200751 if (dsdt->length >= sizeof(acpi_header_t)) {
752 current += sizeof(acpi_header_t);
753
754 acpigen_set_current((char *) current);
755 for (dev = all_devices; dev; dev = dev->next)
756 if (dev->ops && dev->ops->acpi_inject_dsdt_generator) {
757 dev->ops->acpi_inject_dsdt_generator();
758 }
759 current = (unsigned long) acpigen_get_current();
760 memcpy((char *)current,
761 (char *)&AmlCode + sizeof(acpi_header_t),
762 dsdt->length - sizeof(acpi_header_t));
763 current += dsdt->length - sizeof(acpi_header_t);
764
765 /* (Re)calculate length and checksum. */
766 dsdt->length = current - (unsigned long)dsdt;
767 dsdt->checksum = 0;
768 dsdt->checksum = acpi_checksum((void *)dsdt, dsdt->length);
769 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200770
771 ALIGN_CURRENT;
772
773 printk(BIOS_DEBUG, "ACPI: * FADT\n");
774 fadt = (acpi_fadt_t *) current;
775 current += sizeof(acpi_fadt_t);
776 ALIGN_CURRENT;
777
778 acpi_create_fadt(fadt, facs, dsdt);
779 acpi_add_table(rsdp, fadt);
780
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100781 if (slic_file) {
782 printk(BIOS_DEBUG, "ACPI: * SLIC\n");
783 slic = (acpi_header_t *)current;
784 memcpy(slic, slic_file, slic_file->length);
785 current += slic_file->length;
786 ALIGN_CURRENT;
787 acpi_add_table(rsdp, slic);
788 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200789
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}
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200830
Patrick Georgie1667822012-05-05 15:29:32 +0200831#if CONFIG_HAVE_ACPI_RESUME
Kyösti Mälkki134f5042014-12-26 13:29:09 +0200832void __attribute__((weak)) mainboard_suspend_resume(void)
833{
834}
835
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500836void acpi_resume(void *wake_vec)
Stefan Reinauer7e9771c2009-04-22 08:17:38 +0000837{
Duncan Laurie11290c42012-10-03 19:07:05 -0700838#if CONFIG_HAVE_SMI_HANDLER
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500839 u32 *gnvs_address = cbmem_find(CBMEM_ID_ACPI_GNVS_PTR);
Duncan Laurie11290c42012-10-03 19:07:05 -0700840
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500841 /* Restore GNVS pointer in SMM if found */
842 if (gnvs_address && *gnvs_address) {
843 printk(BIOS_DEBUG, "Restore GNVS pointer to 0x%08x\n",
844 *gnvs_address);
845 smm_setup_structures((void *)*gnvs_address, NULL, NULL);
Stefan Reinauercb91e152012-04-03 23:28:22 +0200846 }
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500847#endif
848
849 /* Call mainboard resume handler first, if defined. */
Kyösti Mälkki134f5042014-12-26 13:29:09 +0200850 mainboard_suspend_resume();
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500851
852 post_code(POST_OS_RESUME);
853 acpi_jump_to_wakeup(wake_vec);
Stefan Reinauer7e9771c2009-04-22 08:17:38 +0000854}
855
Kyösti Mälkki7a846e72015-01-09 23:55:09 +0200856/* This is filled with acpi_is_wakeup() call early in ramstage. */
857int acpi_slp_type = -1;
Stefan Reinauer688b3852004-01-28 16:56:14 +0000858
Kyösti Mälkki9e94dbf2015-01-08 20:03:18 +0200859#if IS_ENABLED(CONFIG_EARLY_CBMEM_INIT)
860int acpi_get_sleep_type(void)
Kyösti Mälkki134f5042014-12-26 13:29:09 +0200861{
Kyösti Mälkki9e94dbf2015-01-08 20:03:18 +0200862 struct romstage_handoff *handoff;
863
864 handoff = cbmem_find(CBMEM_ID_ROMSTAGE_INFO);
865
866 if (handoff == NULL) {
867 printk(BIOS_DEBUG, "Unknown boot method, assuming normal.\n");
868 return 0;
869 } else if (handoff->s3_resume) {
870 printk(BIOS_DEBUG, "S3 Resume.\n");
871 return 3;
872 } else {
873 printk(BIOS_DEBUG, "Normal boot.\n");
874 return 0;
875 }
Kyösti Mälkki134f5042014-12-26 13:29:09 +0200876}
Kyösti Mälkki9e94dbf2015-01-08 20:03:18 +0200877#endif
Kyösti Mälkki134f5042014-12-26 13:29:09 +0200878
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +0300879static void acpi_handoff_wakeup(void)
880{
Kyösti Mälkki7a846e72015-01-09 23:55:09 +0200881 if (acpi_slp_type < 0)
882 acpi_slp_type = acpi_get_sleep_type();
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +0300883}
884
Kyösti Mälkkicb28f3f2014-01-03 15:15:22 +0200885int acpi_is_wakeup(void)
Rudolf Marek33cafe52009-04-13 18:07:02 +0000886{
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +0300887 acpi_handoff_wakeup();
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +0100888 /* Both resume from S2 and resume from S3 restart at CPU reset */
889 return (acpi_slp_type == 3 || acpi_slp_type == 2);
Rudolf Marek33cafe52009-04-13 18:07:02 +0000890}
891
Kyösti Mälkki4d9b7722014-06-19 19:45:40 +0300892int acpi_is_wakeup_s3(void)
893{
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +0300894 acpi_handoff_wakeup();
Kyösti Mälkki4d9b7722014-06-19 19:45:40 +0300895 return (acpi_slp_type == 3);
896}
897
Kyösti Mälkkicb28f3f2014-01-03 15:15:22 +0200898void acpi_fail_wakeup(void)
899{
900 if (acpi_slp_type == 3 || acpi_slp_type == 2)
901 acpi_slp_type = 0;
902}
903
Kyösti Mälkki7b14f082014-10-16 20:58:47 +0300904void acpi_prepare_resume_backup(void)
905{
906 if (!acpi_s3_resume_allowed())
907 return;
908
909 /* Let's prepare the ACPI S3 Resume area now already, so we can rely on
910 * it being there during reboot time. We don't need the pointer, nor
911 * the result right now. If it fails, ACPI resume will be disabled.
912 */
913
914 if (HIGH_MEMORY_SAVE)
915 cbmem_add(CBMEM_ID_RESUME, HIGH_MEMORY_SAVE);
916
917 if (HIGH_MEMORY_SCRATCH)
918 cbmem_add(CBMEM_ID_RESUME_SCRATCH, HIGH_MEMORY_SCRATCH);
919}
920
Rudolf Marek33cafe52009-04-13 18:07:02 +0000921static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp)
922{
923 if (strncmp((char *)rsdp, RSDP_SIG, sizeof(RSDP_SIG) - 1) != 0)
924 return NULL;
925
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000926 printk(BIOS_DEBUG, "Looking on %p for valid checksum\n", rsdp);
Rudolf Marek33cafe52009-04-13 18:07:02 +0000927
928 if (acpi_checksum((void *)rsdp, 20) != 0)
929 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000930 printk(BIOS_DEBUG, "Checksum 1 passed\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +0000931
932 if ((rsdp->revision > 1) && (acpi_checksum((void *)rsdp,
933 rsdp->length) != 0))
934 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000935 printk(BIOS_DEBUG, "Checksum 2 passed all OK\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +0000936
937 return rsdp;
938}
939
940static acpi_rsdp_t *rsdp;
941
942void *acpi_get_wakeup_rsdp(void)
943{
944 return rsdp;
945}
946
947void *acpi_find_wakeup_vector(void)
948{
949 char *p, *end;
Rudolf Marek33cafe52009-04-13 18:07:02 +0000950 acpi_rsdt_t *rsdt;
951 acpi_facs_t *facs;
Martin Rothc5f49262012-12-14 19:17:55 -0700952 acpi_fadt_t *fadt = NULL;
Uwe Hermann622824c2010-11-19 15:14:42 +0000953 void *wake_vec;
Rudolf Marek33cafe52009-04-13 18:07:02 +0000954 int i;
955
956 rsdp = NULL;
957
958 if (!acpi_is_wakeup())
959 return NULL;
960
Uwe Hermann622824c2010-11-19 15:14:42 +0000961 printk(BIOS_DEBUG, "Trying to find the wakeup vector...\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +0000962
Uwe Hermann622824c2010-11-19 15:14:42 +0000963 /* Find RSDP. */
964 for (p = (char *)0xe0000; p < (char *)0xfffff; p += 16) {
965 if ((rsdp = valid_rsdp((acpi_rsdp_t *)p)))
Rudolf Marek33cafe52009-04-13 18:07:02 +0000966 break;
967 }
968
969 if (rsdp == NULL)
970 return NULL;
971
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000972 printk(BIOS_DEBUG, "RSDP found at %p\n", rsdp);
Rudolf Marek33cafe52009-04-13 18:07:02 +0000973 rsdt = (acpi_rsdt_t *) rsdp->rsdt_address;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000974
Uwe Hermann622824c2010-11-19 15:14:42 +0000975 end = (char *)rsdt + rsdt->header.length;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000976 printk(BIOS_DEBUG, "RSDT found at %p ends at %p\n", rsdt, end);
Rudolf Marek33cafe52009-04-13 18:07:02 +0000977
Uwe Hermann622824c2010-11-19 15:14:42 +0000978 for (i = 0; ((char *)&rsdt->entry[i]) < end; i++) {
979 fadt = (acpi_fadt_t *)rsdt->entry[i];
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000980 if (strncmp((char *)fadt, "FACP", 4) == 0)
Rudolf Marek33cafe52009-04-13 18:07:02 +0000981 break;
982 fadt = NULL;
983 }
984
985 if (fadt == NULL)
986 return NULL;
987
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000988 printk(BIOS_DEBUG, "FADT found at %p\n", fadt);
Stefan Reinauer69390db2009-05-26 12:33:52 +0000989 facs = (acpi_facs_t *)fadt->firmware_ctrl;
Rudolf Marek33cafe52009-04-13 18:07:02 +0000990
Stefan Reinauer7e9771c2009-04-22 08:17:38 +0000991 if (facs == NULL) {
Uwe Hermann622824c2010-11-19 15:14:42 +0000992 printk(BIOS_DEBUG, "No FACS found, wake up from S3 not "
993 "possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +0000994 return NULL;
Stefan Reinauer7e9771c2009-04-22 08:17:38 +0000995 }
Rudolf Marek33cafe52009-04-13 18:07:02 +0000996
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000997 printk(BIOS_DEBUG, "FACS found at %p\n", facs);
Uwe Hermann622824c2010-11-19 15:14:42 +0000998 wake_vec = (void *)facs->firmware_waking_vector;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000999 printk(BIOS_DEBUG, "OS waking vector is %p\n", wake_vec);
Uwe Hermann622824c2010-11-19 15:14:42 +00001000
Rudolf Marek33cafe52009-04-13 18:07:02 +00001001 return wake_vec;
1002}
1003
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +01001004#if CONFIG_SMP
Rudolf Marek33cafe52009-04-13 18:07:02 +00001005extern char *lowmem_backup;
1006extern char *lowmem_backup_ptr;
1007extern int lowmem_backup_size;
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +01001008#endif
Rudolf Marek33cafe52009-04-13 18:07:02 +00001009
Uwe Hermann622824c2010-11-19 15:14:42 +00001010#define WAKEUP_BASE 0x600
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001011
Uwe Hermann622824c2010-11-19 15:14:42 +00001012void (*acpi_do_wakeup)(u32 vector, u32 backup_source, u32 backup_target,
Stefan Reinauer399486e2012-12-06 13:54:29 -08001013 u32 backup_size) asmlinkage = (void *)WAKEUP_BASE;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001014
Aaron Durbina146d582013-02-08 16:56:51 -06001015extern unsigned char __wakeup;
1016extern unsigned int __wakeup_size;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001017
Rudolf Marek33cafe52009-04-13 18:07:02 +00001018void acpi_jump_to_wakeup(void *vector)
1019{
Aaron Durbin8e4a3552013-02-08 17:28:04 -06001020 u32 acpi_backup_memory = 0;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001021
Kyösti Mälkki2ca2afe2014-06-17 15:41:37 +03001022 if (HIGH_MEMORY_SAVE && acpi_s3_resume_allowed()) {
1023 acpi_backup_memory = (u32)cbmem_find(CBMEM_ID_RESUME);
1024
1025 if (!acpi_backup_memory) {
1026 printk(BIOS_WARNING, "ACPI: Backup memory missing. "
1027 "No S3 resume.\n");
1028 return;
1029 }
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001030 }
1031
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +01001032#if CONFIG_SMP
Martin Roth7b5f8ef2013-07-08 16:22:10 -06001033 // FIXME: This should go into the ACPI backup memory, too. No pork sausages.
Uwe Hermann622824c2010-11-19 15:14:42 +00001034 /*
1035 * Just restore the SMP trampoline and continue with wakeup on
1036 * assembly level.
1037 */
Rudolf Marek33cafe52009-04-13 18:07:02 +00001038 memcpy(lowmem_backup_ptr, lowmem_backup, lowmem_backup_size);
Tobias Diedrich4e22a3b2010-12-13 22:39:46 +01001039#endif
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001040
Uwe Hermann622824c2010-11-19 15:14:42 +00001041 /* Copy wakeup trampoline in place. */
Aaron Durbina146d582013-02-08 16:56:51 -06001042 memcpy((void *)WAKEUP_BASE, &__wakeup, __wakeup_size);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +00001043
Duncan Lauriecde78012011-10-19 15:32:39 -07001044#if CONFIG_COLLECT_TIMESTAMPS
1045 timestamp_add_now(TS_ACPI_WAKE_JUMP);
1046#endif
1047
Uwe Hermann622824c2010-11-19 15:14:42 +00001048 acpi_do_wakeup((u32)vector, acpi_backup_memory, CONFIG_RAMBASE,
1049 HIGH_MEMORY_SAVE);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001050}
1051#endif
Duncan Laurie11290c42012-10-03 19:07:05 -07001052
1053void acpi_save_gnvs(u32 gnvs_address)
1054{
Duncan Laurie9c07c8f2013-03-22 11:08:39 -07001055 u32 *gnvs = cbmem_add(CBMEM_ID_ACPI_GNVS_PTR, sizeof(*gnvs));
Duncan Laurie11290c42012-10-03 19:07:05 -07001056 if (gnvs)
1057 *gnvs = gnvs_address;
1058}