blob: 366129734f9bfd8152c153a61368155ea86a91b9 [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
Timothy Pearsonff8ccf02015-08-11 17:48:32 -05009 * Copyright (C) 2015 Timothy Pearson <tpearson@raptorengineeringinc.com>, Raptor Engineering
Stefan Reinauer777224c2005-01-19 14:06:41 +000010 *
Stefan Reinauer14e22772010-04-27 06:56:47 +000011 * ACPI FADT, FACS, and DSDT table support added by
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000012 * Nick Barker <nick.barker9@btinternet.com>, and those portions
Uwe Hermannc70e9fc2010-02-15 23:10:19 +000013 * Copyright (C) 2004 Nick Barker
Stefan Reinauerf622d592005-11-26 16:56:05 +000014 *
Uwe Hermannc70e9fc2010-02-15 23:10:19 +000015 * Copyright (C) 2005 ADVANCED MICRO DEVICES, INC. All Rights Reserved.
Stefan Reinauerd6edf7a2006-01-05 00:19:52 +000016 * 2005.9 yhlu add SRAT table generation
Martin Roth9df9e9392016-01-12 15:55:28 -070017 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; version 2 of the License.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
Stefan Reinauer777224c2005-01-19 14:06:41 +000026 */
27
Stefan Reinauer14e22772010-04-27 06:56:47 +000028/*
Stefan Reinauer777224c2005-01-19 14:06:41 +000029 * Each system port implementing ACPI has to provide two functions:
Stefan Reinauer14e22772010-04-27 06:56:47 +000030 *
Stefan Reinauer777224c2005-01-19 14:06:41 +000031 * write_acpi_tables()
32 * acpi_dump_apics()
Stefan Reinauer14e22772010-04-27 06:56:47 +000033 *
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000034 * See Kontron 986LCD-M port for a good example of an ACPI implementation
35 * in coreboot.
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000036 */
Stefan Reinauer688b3852004-01-28 16:56:14 +000037
38#include <console/console.h>
39#include <string.h>
40#include <arch/acpi.h>
Rudolf Marek293b5f52009-02-01 18:35:15 +000041#include <arch/acpigen.h>
Stefan Reinauer777224c2005-01-19 14:06:41 +000042#include <device/pci.h>
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000043#include <cbmem.h>
Patrick Georgic8feedd2012-02-16 18:43:25 +010044#include <cpu/x86/lapic_def.h>
Duncan Laurie11290c42012-10-03 19:07:05 -070045#include <cpu/cpu.h>
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +010046#include <cbfs.h>
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000047
48u8 acpi_checksum(u8 *table, u32 length)
Stefan Reinauer688b3852004-01-28 16:56:14 +000049{
Uwe Hermann622824c2010-11-19 15:14:42 +000050 u8 ret = 0;
Stefan Reinauer688b3852004-01-28 16:56:14 +000051 while (length--) {
52 ret += *table;
53 table++;
54 }
Stefan Reinauera7648c22004-01-29 17:31:34 +000055 return -ret;
Stefan Reinauer688b3852004-01-28 16:56:14 +000056}
57
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000058/**
Uwe Hermann622824c2010-11-19 15:14:42 +000059 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
60 * and checksum.
Stefan Reinauer06feb882004-02-03 16:11:35 +000061 */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000062void acpi_add_table(acpi_rsdp_t *rsdp, void *table)
Stefan Reinauer688b3852004-01-28 16:56:14 +000063{
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000064 int i, entries_num;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000065 acpi_rsdt_t *rsdt;
66 acpi_xsdt_t *xsdt = NULL;
67
Uwe Hermann622824c2010-11-19 15:14:42 +000068 /* The RSDT is mandatory... */
Stefan Reinauerdefee172015-06-18 01:11:19 -070069 rsdt = (acpi_rsdt_t *)(uintptr_t)rsdp->rsdt_address;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000070
Uwe Hermann622824c2010-11-19 15:14:42 +000071 /* ...while the XSDT is not. */
72 if (rsdp->xsdt_address)
Stefan Reinauerdefee172015-06-18 01:11:19 -070073 xsdt = (acpi_xsdt_t *)((uintptr_t)rsdp->xsdt_address);
Stefan Reinauer14e22772010-04-27 06:56:47 +000074
Uwe Hermann622824c2010-11-19 15:14:42 +000075 /* This should always be MAX_ACPI_TABLES. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000076 entries_num = ARRAY_SIZE(rsdt->entry);
Stefan Reinauer14e22772010-04-27 06:56:47 +000077
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000078 for (i = 0; i < entries_num; i++) {
Uwe Hermann622824c2010-11-19 15:14:42 +000079 if (rsdt->entry[i] == 0)
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000080 break;
Stefan Reinauer688b3852004-01-28 16:56:14 +000081 }
82
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000083 if (i >= entries_num) {
Uwe Hermann622824c2010-11-19 15:14:42 +000084 printk(BIOS_ERR, "ACPI: Error: Could not add ACPI table, "
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +030085 "too many tables.\n");
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000086 return;
87 }
88
Uwe Hermann622824c2010-11-19 15:14:42 +000089 /* Add table to the RSDT. */
Stefan Reinauerdefee172015-06-18 01:11:19 -070090 rsdt->entry[i] = (uintptr_t)table;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000091
Uwe Hermann622824c2010-11-19 15:14:42 +000092 /* Fix RSDT length or the kernel will assume invalid entries. */
93 rsdt->header.length = sizeof(acpi_header_t) + (sizeof(u32) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000094
Uwe Hermann622824c2010-11-19 15:14:42 +000095 /* Re-calculate checksum. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000096 rsdt->header.checksum = 0; /* Hope this won't get optimized away */
Uwe Hermann622824c2010-11-19 15:14:42 +000097 rsdt->header.checksum = acpi_checksum((u8 *)rsdt, rsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000098
Uwe Hermann622824c2010-11-19 15:14:42 +000099 /*
100 * And now the same thing for the XSDT. We use the same index as for
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000101 * now we want the XSDT and RSDT to always be in sync in coreboot.
102 */
103 if (xsdt) {
Uwe Hermann622824c2010-11-19 15:14:42 +0000104 /* Add table to the XSDT. */
Stefan Reinauerdefee172015-06-18 01:11:19 -0700105 xsdt->entry[i] = (u64)(uintptr_t)table;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000106
Uwe Hermann622824c2010-11-19 15:14:42 +0000107 /* Fix XSDT length. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000108 xsdt->header.length = sizeof(acpi_header_t) +
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300109 (sizeof(u64) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000110
Uwe Hermann622824c2010-11-19 15:14:42 +0000111 /* Re-calculate checksum. */
112 xsdt->header.checksum = 0;
113 xsdt->header.checksum = acpi_checksum((u8 *)xsdt,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300114 xsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000115 }
116
Uwe Hermann622824c2010-11-19 15:14:42 +0000117 printk(BIOS_DEBUG, "ACPI: added table %d/%d, length now %d\n",
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300118 i + 1, entries_num, rsdt->header.length);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000119}
120
Uwe Hermann622824c2010-11-19 15:14:42 +0000121int acpi_create_mcfg_mmconfig(acpi_mcfg_mmconfig_t *mmconfig, u32 base,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300122 u16 seg_nr, u8 start, u8 end)
Stefan Reinauer26d431a2009-01-20 19:17:51 +0000123{
Vladimir Serbinenko60fccdc2014-10-05 11:05:16 +0200124 memset(mmconfig, 0, sizeof(*mmconfig));
Rudolf Mareke6409f22007-11-03 12:50:26 +0000125 mmconfig->base_address = base;
126 mmconfig->base_reserved = 0;
127 mmconfig->pci_segment_group_number = seg_nr;
128 mmconfig->start_bus_number = start;
129 mmconfig->end_bus_number = end;
Uwe Hermann622824c2010-11-19 15:14:42 +0000130
131 return sizeof(acpi_mcfg_mmconfig_t);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000132}
133
Stefan Reinauer777224c2005-01-19 14:06:41 +0000134int acpi_create_madt_lapic(acpi_madt_lapic_t *lapic, u8 cpu, u8 apic)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000135{
Uwe Hermann622824c2010-11-19 15:14:42 +0000136 lapic->type = 0; /* Local APIC structure */
137 lapic->length = sizeof(acpi_madt_lapic_t);
138 lapic->flags = (1 << 0); /* Processor/LAPIC enabled */
139 lapic->processor_id = cpu;
140 lapic->apic_id = apic;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000141
Uwe Hermann622824c2010-11-19 15:14:42 +0000142 return lapic->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000143}
144
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000145unsigned long acpi_create_madt_lapics(unsigned long current)
146{
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100147 struct device *cpu;
Duncan Laurie11290c42012-10-03 19:07:05 -0700148 int index = 0;
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000149
Uwe Hermann622824c2010-11-19 15:14:42 +0000150 for (cpu = all_devices; cpu; cpu = cpu->next) {
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000151 if ((cpu->path.type != DEVICE_PATH_APIC) ||
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800152 (cpu->bus->dev->path.type != DEVICE_PATH_CPU_CLUSTER)) {
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000153 continue;
154 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000155 if (!cpu->enabled)
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000156 continue;
Uwe Hermann622824c2010-11-19 15:14:42 +0000157 current += acpi_create_madt_lapic((acpi_madt_lapic_t *)current,
Duncan Laurie11290c42012-10-03 19:07:05 -0700158 index, cpu->path.apic.apic_id);
159 index++;
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000160 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000161
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000162 return current;
163}
164
Uwe Hermann622824c2010-11-19 15:14:42 +0000165int acpi_create_madt_ioapic(acpi_madt_ioapic_t *ioapic, u8 id, u32 addr,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300166 u32 gsi_base)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000167{
Uwe Hermann622824c2010-11-19 15:14:42 +0000168 ioapic->type = 1; /* I/O APIC structure */
169 ioapic->length = sizeof(acpi_madt_ioapic_t);
170 ioapic->reserved = 0x00;
171 ioapic->gsi_base = gsi_base;
172 ioapic->ioapic_id = id;
173 ioapic->ioapic_addr = addr;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000174
Uwe Hermann622824c2010-11-19 15:14:42 +0000175 return ioapic->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000176}
177
Stefan Reinauer777224c2005-01-19 14:06:41 +0000178int acpi_create_madt_irqoverride(acpi_madt_irqoverride_t *irqoverride,
Stefan Reinauer06feb882004-02-03 16:11:35 +0000179 u8 bus, u8 source, u32 gsirq, u16 flags)
180{
Uwe Hermann622824c2010-11-19 15:14:42 +0000181 irqoverride->type = 2; /* Interrupt source override */
182 irqoverride->length = sizeof(acpi_madt_irqoverride_t);
183 irqoverride->bus = bus;
184 irqoverride->source = source;
185 irqoverride->gsirq = gsirq;
186 irqoverride->flags = flags;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000187
Uwe Hermann622824c2010-11-19 15:14:42 +0000188 return irqoverride->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000189}
190
Stefan Reinauer777224c2005-01-19 14:06:41 +0000191int acpi_create_madt_lapic_nmi(acpi_madt_lapic_nmi_t *lapic_nmi, u8 cpu,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300192 u16 flags, u8 lint)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000193{
Uwe Hermann622824c2010-11-19 15:14:42 +0000194 lapic_nmi->type = 4; /* Local APIC NMI structure */
195 lapic_nmi->length = sizeof(acpi_madt_lapic_nmi_t);
196 lapic_nmi->flags = flags;
197 lapic_nmi->processor_id = cpu;
198 lapic_nmi->lint = lint;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000199
Uwe Hermann622824c2010-11-19 15:14:42 +0000200 return lapic_nmi->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000201}
202
Stefan Reinauer777224c2005-01-19 14:06:41 +0000203void acpi_create_madt(acpi_madt_t *madt)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000204{
Uwe Hermann622824c2010-11-19 15:14:42 +0000205 acpi_header_t *header = &(madt->header);
206 unsigned long current = (unsigned long)madt + sizeof(acpi_madt_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000207
Stefan Reinauer06feb882004-02-03 16:11:35 +0000208 memset((void *)madt, 0, sizeof(acpi_madt_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000209
Uwe Hermann622824c2010-11-19 15:14:42 +0000210 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000211 memcpy(header->signature, "APIC", 4);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000212 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000213 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000214 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000215
Stefan Reinauer06feb882004-02-03 16:11:35 +0000216 header->length = sizeof(acpi_madt_t);
Uwe Hermann622824c2010-11-19 15:14:42 +0000217 header->revision = 1; /* ACPI 1.0/2.0: 1, ACPI 3.0: 2, ACPI 4.0: 3 */
Stefan Reinauer06feb882004-02-03 16:11:35 +0000218
Uwe Hermann622824c2010-11-19 15:14:42 +0000219 madt->lapic_addr = LOCAL_APIC_ADDR;
220 madt->flags = 0x1; /* PCAT_COMPAT */
Stefan Reinauer06feb882004-02-03 16:11:35 +0000221
Stefan Reinauerf622d592005-11-26 16:56:05 +0000222 current = acpi_fill_madt(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000223
Uwe Hermann622824c2010-11-19 15:14:42 +0000224 /* (Re)calculate length and checksum. */
225 header->length = current - (unsigned long)madt;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000226
Uwe Hermann622824c2010-11-19 15:14:42 +0000227 header->checksum = acpi_checksum((void *)madt, header->length);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000228}
229
Uwe Hermann622824c2010-11-19 15:14:42 +0000230/* MCFG is defined in the PCI Firmware Specification 3.0. */
Rudolf Mareke6409f22007-11-03 12:50:26 +0000231void acpi_create_mcfg(acpi_mcfg_t *mcfg)
232{
Uwe Hermann622824c2010-11-19 15:14:42 +0000233 acpi_header_t *header = &(mcfg->header);
234 unsigned long current = (unsigned long)mcfg + sizeof(acpi_mcfg_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000235
Rudolf Mareke6409f22007-11-03 12:50:26 +0000236 memset((void *)mcfg, 0, sizeof(acpi_mcfg_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000237
Uwe Hermann622824c2010-11-19 15:14:42 +0000238 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000239 memcpy(header->signature, "MCFG", 4);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000240 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000241 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000242 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000243
Rudolf Mareke6409f22007-11-03 12:50:26 +0000244 header->length = sizeof(acpi_mcfg_t);
245 header->revision = 1;
246
247 current = acpi_fill_mcfg(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000248
Uwe Hermann622824c2010-11-19 15:14:42 +0000249 /* (Re)calculate length and checksum. */
250 header->length = current - (unsigned long)mcfg;
251 header->checksum = acpi_checksum((void *)mcfg, header->length);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000252}
253
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200254static void *get_tcpa_log(u32 *size)
255{
256 const struct cbmem_entry *ce;
257 const u32 tcpa_default_log_len = 0x10000;
258 void *lasa;
259 ce = cbmem_entry_find(CBMEM_ID_TCPA_LOG);
260 if (ce) {
261 lasa = cbmem_entry_start(ce);
262 *size = cbmem_entry_size(ce);
263 printk(BIOS_DEBUG, "TCPA log found at %p\n", lasa);
264 return lasa;
265 }
266 lasa = cbmem_add(CBMEM_ID_TCPA_LOG, tcpa_default_log_len);
267 if (!lasa) {
268 printk(BIOS_ERR, "TCPA log creation failed\n");
269 return NULL;
270 }
271
272 printk(BIOS_DEBUG, "TCPA log created at %p\n", lasa);
273 memset (lasa, 0, tcpa_default_log_len);
274
275 *size = tcpa_default_log_len;
276 return lasa;
277}
278
279static void acpi_create_tcpa(acpi_tcpa_t *tcpa)
280{
281 acpi_header_t *header = &(tcpa->header);
282 u32 tcpa_log_len;
283 void *lasa;
284
285 memset((void *)tcpa, 0, sizeof(acpi_tcpa_t));
286
287 lasa = get_tcpa_log(&tcpa_log_len);
288 if (!lasa) {
289 return;
290 }
291
292 /* Fill out header fields. */
293 memcpy(header->signature, "TCPA", 4);
294 memcpy(header->oem_id, OEM_ID, 6);
295 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
296 memcpy(header->asl_compiler_id, ASLC, 4);
297
298 header->length = sizeof(acpi_tcpa_t);
299 header->revision = 2;
300
301 tcpa->platform_class = 0;
302 tcpa->laml = tcpa_log_len;
Stefan Reinauerdefee172015-06-18 01:11:19 -0700303 tcpa->lasa = (uintptr_t) lasa;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200304
305 /* Calculate checksum. */
306 header->checksum = acpi_checksum((void *)tcpa, header->length);
307}
308
Myles Watson3fe6b702009-10-09 20:13:43 +0000309void acpi_create_ssdt_generator(acpi_header_t *ssdt, const char *oem_table_id)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000310{
Uwe Hermann622824c2010-11-19 15:14:42 +0000311 unsigned long current = (unsigned long)ssdt + sizeof(acpi_header_t);
312
Rudolf Marek293b5f52009-02-01 18:35:15 +0000313 memset((void *)ssdt, 0, sizeof(acpi_header_t));
Uwe Hermann622824c2010-11-19 15:14:42 +0000314
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000315 memcpy(&ssdt->signature, "SSDT", 4);
Uwe Hermann622824c2010-11-19 15:14:42 +0000316 ssdt->revision = 2; /* ACPI 1.0/2.0: ?, ACPI 3.0/4.0: 2 */
Rudolf Marek293b5f52009-02-01 18:35:15 +0000317 memcpy(&ssdt->oem_id, OEM_ID, 6);
318 memcpy(&ssdt->oem_table_id, oem_table_id, 8);
319 ssdt->oem_revision = 42;
Uwe Hermann622824c2010-11-19 15:14:42 +0000320 memcpy(&ssdt->asl_compiler_id, ASLC, 4);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000321 ssdt->asl_compiler_revision = 42;
322 ssdt->length = sizeof(acpi_header_t);
323
Stefan Reinauer8dcd50b2009-03-06 17:24:29 +0000324 acpigen_set_current((char *) current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200325 {
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100326 struct device *dev;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200327 for (dev = all_devices; dev; dev = dev->next)
328 if (dev->ops && dev->ops->acpi_fill_ssdt_generator) {
Alexander Couzens5eea4582015-04-12 22:18:55 +0200329 dev->ops->acpi_fill_ssdt_generator(dev);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200330 }
Vladimir Serbinenko0a669912014-10-05 14:34:17 +0200331 current = (unsigned long) acpigen_get_current();
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200332 }
Rudolf Marek293b5f52009-02-01 18:35:15 +0000333
Uwe Hermann622824c2010-11-19 15:14:42 +0000334 /* (Re)calculate length and checksum. */
Rudolf Marek293b5f52009-02-01 18:35:15 +0000335 ssdt->length = current - (unsigned long)ssdt;
336 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
337}
338
Stefan Reinauerf622d592005-11-26 16:56:05 +0000339int acpi_create_srat_lapic(acpi_srat_lapic_t *lapic, u8 node, u8 apic)
340{
Myles Watson0b4c9f02009-03-10 18:06:47 +0000341 memset((void *)lapic, 0, sizeof(acpi_srat_lapic_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000342
Uwe Hermann622824c2010-11-19 15:14:42 +0000343 lapic->type = 0; /* Processor local APIC/SAPIC affinity structure */
344 lapic->length = sizeof(acpi_srat_lapic_t);
345 lapic->flags = (1 << 0); /* Enabled (the use of this structure). */
346 lapic->proximity_domain_7_0 = node;
347 /* TODO: proximity_domain_31_8, local SAPIC EID, clock domain. */
348 lapic->apic_id = apic;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000349
Uwe Hermann622824c2010-11-19 15:14:42 +0000350 return lapic->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000351}
352
Uwe Hermann622824c2010-11-19 15:14:42 +0000353int 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 +0300354 u32 flags)
Stefan Reinauerf622d592005-11-26 16:56:05 +0000355{
Uwe Hermann622824c2010-11-19 15:14:42 +0000356 mem->type = 1; /* Memory affinity structure */
357 mem->length = sizeof(acpi_srat_mem_t);
358 mem->base_address_low = (basek << 10);
359 mem->base_address_high = (basek >> (32 - 10));
360 mem->length_low = (sizek << 10);
361 mem->length_high = (sizek >> (32 - 10));
362 mem->proximity_domain = node;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000363 mem->flags = flags;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000364
Uwe Hermann622824c2010-11-19 15:14:42 +0000365 return mem->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000366}
367
Uwe Hermann622824c2010-11-19 15:14:42 +0000368/* http://www.microsoft.com/whdc/system/sysinternals/sratdwn.mspx */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +0200369void acpi_create_srat(acpi_srat_t *srat,
370 unsigned long (*acpi_fill_srat)(unsigned long current))
Stefan Reinauerf622d592005-11-26 16:56:05 +0000371{
Uwe Hermann622824c2010-11-19 15:14:42 +0000372 acpi_header_t *header = &(srat->header);
373 unsigned long current = (unsigned long)srat + sizeof(acpi_srat_t);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000374
Uwe Hermann622824c2010-11-19 15:14:42 +0000375 memset((void *)srat, 0, sizeof(acpi_srat_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000376
Uwe Hermann622824c2010-11-19 15:14:42 +0000377 /* Fill out header fields. */
378 memcpy(header->signature, "SRAT", 4);
379 memcpy(header->oem_id, OEM_ID, 6);
380 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
381 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000382
Uwe Hermann622824c2010-11-19 15:14:42 +0000383 header->length = sizeof(acpi_srat_t);
384 header->revision = 1; /* ACPI 1.0: N/A, 2.0: 1, 3.0: 2, 4.0: 3 */
Stefan Reinauerf622d592005-11-26 16:56:05 +0000385
Uwe Hermann622824c2010-11-19 15:14:42 +0000386 srat->resv = 1; /* Spec: Reserved to 1 for backwards compatibility. */
Stefan Reinauerf622d592005-11-26 16:56:05 +0000387
Uwe Hermann622824c2010-11-19 15:14:42 +0000388 current = acpi_fill_srat(current);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000389
Uwe Hermann622824c2010-11-19 15:14:42 +0000390 /* (Re)calculate length and checksum. */
391 header->length = current - (unsigned long)srat;
392 header->checksum = acpi_checksum((void *)srat, header->length);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000393}
394
Nico Hubere561f352015-10-26 11:51:25 +0100395void acpi_create_dmar(acpi_dmar_t *dmar, enum dmar_flags flags,
Vladimir Serbinenko8d70e942014-11-09 13:22:27 +0100396 unsigned long (*acpi_fill_dmar) (unsigned long))
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200397{
398 acpi_header_t *header = &(dmar->header);
399 unsigned long current = (unsigned long)dmar + sizeof(acpi_dmar_t);
400
401 memset((void *)dmar, 0, sizeof(acpi_dmar_t));
402
403 /* Fill out header fields. */
404 memcpy(header->signature, "DMAR", 4);
405 memcpy(header->oem_id, OEM_ID, 6);
406 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
407 memcpy(header->asl_compiler_id, ASLC, 4);
408
409 header->length = sizeof(acpi_dmar_t);
410 header->revision = 1;
411
Jacob Laskaf3f654d2016-03-15 21:53:27 -0500412 dmar->host_address_width = cpu_phys_address_size() - 1;
Nico Hubere561f352015-10-26 11:51:25 +0100413 dmar->flags = flags;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200414
415 current = acpi_fill_dmar(current);
416
417 /* (Re)calculate length and checksum. */
418 header->length = current - (unsigned long)dmar;
419 header->checksum = acpi_checksum((void *)dmar, header->length);
420}
421
422unsigned long acpi_create_dmar_drhd(unsigned long current, u8 flags,
423 u16 segment, u32 bar)
424{
425 dmar_entry_t *drhd = (dmar_entry_t *)current;
426 memset(drhd, 0, sizeof(*drhd));
427 drhd->type = DMAR_DRHD;
428 drhd->length = sizeof(*drhd); /* will be fixed up later */
429 drhd->flags = flags;
430 drhd->segment = segment;
431 drhd->bar = bar;
432
433 return drhd->length;
434}
435
436void acpi_dmar_drhd_fixup(unsigned long base, unsigned long current)
437{
438 dmar_entry_t *drhd = (dmar_entry_t *)base;
439 drhd->length = current - base;
440}
441
Nico Huber6c4751d2015-10-26 12:03:54 +0100442static unsigned long acpi_create_dmar_drhd_ds(unsigned long current,
443 enum dev_scope_type type, u8 enumeration_id, u8 bus, u8 dev, u8 fn)
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200444{
Nico Huber6c4751d2015-10-26 12:03:54 +0100445 /* we don't support longer paths yet */
446 const size_t dev_scope_length = sizeof(dev_scope_t) + 2;
447
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200448 dev_scope_t *ds = (dev_scope_t *)current;
Nico Huber6c4751d2015-10-26 12:03:54 +0100449 memset(ds, 0, dev_scope_length);
450 ds->type = type;
451 ds->length = dev_scope_length;
452 ds->enumeration = enumeration_id;
453 ds->start_bus = bus;
454 ds->path[0].dev = dev;
455 ds->path[0].fn = fn;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200456
457 return ds->length;
458}
459
Nico Huber6c4751d2015-10-26 12:03:54 +0100460unsigned long acpi_create_dmar_drhd_ds_pci(unsigned long current, u8 bus,
461 u8 dev, u8 fn)
462{
463 return acpi_create_dmar_drhd_ds(current,
464 SCOPE_PCI_ENDPOINT, 0, bus, dev, fn);
465}
466
467unsigned long acpi_create_dmar_drhd_ds_ioapic(unsigned long current,
468 u8 enumeration_id, u8 bus, u8 dev, u8 fn)
469{
470 return acpi_create_dmar_drhd_ds(current,
471 SCOPE_IOAPIC, enumeration_id, bus, dev, fn);
472}
473
474unsigned long acpi_create_dmar_drhd_ds_msi_hpet(unsigned long current,
475 u8 enumeration_id, u8 bus, u8 dev, u8 fn)
476{
477 return acpi_create_dmar_drhd_ds(current,
478 SCOPE_MSI_HPET, enumeration_id, bus, dev, fn);
479}
480
Uwe Hermann622824c2010-11-19 15:14:42 +0000481/* http://h21007.www2.hp.com/portal/download/files/unprot/Itanium/slit.pdf */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +0200482void acpi_create_slit(acpi_slit_t *slit,
483 unsigned long (*acpi_fill_slit)(unsigned long current))
Yinghai Lud4b278c2006-10-04 20:46:15 +0000484{
Uwe Hermann622824c2010-11-19 15:14:42 +0000485 acpi_header_t *header = &(slit->header);
486 unsigned long current = (unsigned long)slit + sizeof(acpi_slit_t);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000487
Uwe Hermann622824c2010-11-19 15:14:42 +0000488 memset((void *)slit, 0, sizeof(acpi_slit_t));
Yinghai Lud4b278c2006-10-04 20:46:15 +0000489
Uwe Hermann622824c2010-11-19 15:14:42 +0000490 /* Fill out header fields. */
491 memcpy(header->signature, "SLIT", 4);
492 memcpy(header->oem_id, OEM_ID, 6);
493 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
494 memcpy(header->asl_compiler_id, ASLC, 4);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000495
Uwe Hermann622824c2010-11-19 15:14:42 +0000496 header->length = sizeof(acpi_slit_t);
497 header->revision = 1; /* ACPI 1.0: N/A, ACPI 2.0/3.0/4.0: 1 */
Yinghai Lud4b278c2006-10-04 20:46:15 +0000498
Uwe Hermann622824c2010-11-19 15:14:42 +0000499 current = acpi_fill_slit(current);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000500
Uwe Hermann622824c2010-11-19 15:14:42 +0000501 /* (Re)calculate length and checksum. */
502 header->length = current - (unsigned long)slit;
503 header->checksum = acpi_checksum((void *)slit, header->length);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000504}
505
Uwe Hermann622824c2010-11-19 15:14:42 +0000506/* http://www.intel.com/hardwaredesign/hpetspec_1.pdf */
Stefan Reinauer777224c2005-01-19 14:06:41 +0000507void acpi_create_hpet(acpi_hpet_t *hpet)
Stefan Reinauer688b3852004-01-28 16:56:14 +0000508{
Uwe Hermann622824c2010-11-19 15:14:42 +0000509 acpi_header_t *header = &(hpet->header);
510 acpi_addr_t *addr = &(hpet->addr);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000511
Stefan Reinauera7648c22004-01-29 17:31:34 +0000512 memset((void *)hpet, 0, sizeof(acpi_hpet_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000513
Uwe Hermann622824c2010-11-19 15:14:42 +0000514 /* Fill out header fields. */
Stefan Reinauer4704dc52009-07-22 01:11:37 +0000515 memcpy(header->signature, "HPET", 4);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000516 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000517 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000518 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000519
Stefan Reinauer688b3852004-01-28 16:56:14 +0000520 header->length = sizeof(acpi_hpet_t);
Uwe Hermann622824c2010-11-19 15:14:42 +0000521 header->revision = 1; /* Currently 1. Table added in ACPI 2.0. */
Stefan Reinauer688b3852004-01-28 16:56:14 +0000522
Uwe Hermann622824c2010-11-19 15:14:42 +0000523 /* Fill out HPET address. */
524 addr->space_id = 0; /* Memory */
525 addr->bit_width = 64;
526 addr->bit_offset = 0;
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200527 addr->addrl = CONFIG_HPET_ADDRESS & 0xffffffff;
528 addr->addrh = ((unsigned long long)CONFIG_HPET_ADDRESS) >> 32;
Stefan Reinauer688b3852004-01-28 16:56:14 +0000529
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200530 hpet->id = *(unsigned int*)CONFIG_HPET_ADDRESS;
Uwe Hermann622824c2010-11-19 15:14:42 +0000531 hpet->number = 0;
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200532 hpet->min_tick = CONFIG_HPET_MIN_TICKS;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000533
Uwe Hermann622824c2010-11-19 15:14:42 +0000534 header->checksum = acpi_checksum((void *)hpet, sizeof(acpi_hpet_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000535}
Uwe Hermann622824c2010-11-19 15:14:42 +0000536
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500537void acpi_create_ivrs(acpi_ivrs_t *ivrs,
538 unsigned long (*acpi_fill_ivrs)(acpi_ivrs_t* ivrs_struct, unsigned long current))
539{
540 acpi_header_t *header = &(ivrs->header);
541 unsigned long current = (unsigned long)ivrs + sizeof(acpi_ivrs_t);
542
543 memset((void *)ivrs, 0, sizeof(acpi_ivrs_t));
544
545 /* Fill out header fields. */
546 memcpy(header->signature, "IVRS", 4);
547 memcpy(header->oem_id, OEM_ID, 6);
548 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
549 memcpy(header->asl_compiler_id, ASLC, 4);
550
551 header->length = sizeof(acpi_ivrs_t);
552 header->revision = 1; /* ACPI 1.0: N/A, ACPI 2.0/3.0/4.0: 1 */
553
554 current = acpi_fill_ivrs(ivrs, current);
555
556 /* (Re)calculate length and checksum. */
557 header->length = current - (unsigned long)ivrs;
558 header->checksum = acpi_checksum((void *)ivrs, header->length);
559}
560
Alexander Couzens83fc32f2015-04-12 22:28:37 +0200561unsigned long acpi_write_hpet(device_t device, unsigned long current, acpi_rsdp_t *rsdp)
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200562{
563 acpi_hpet_t *hpet;
564
565 /*
566 * We explicitly add these tables later on:
567 */
568 printk(BIOS_DEBUG, "ACPI: * HPET\n");
569
570 hpet = (acpi_hpet_t *) current;
571 current += sizeof(acpi_hpet_t);
572 current = ALIGN(current, 16);
573 acpi_create_hpet(hpet);
574 acpi_add_table(rsdp, hpet);
575
576 return current;
577}
578
Stefan Reinauer777224c2005-01-19 14:06:41 +0000579void acpi_create_facs(acpi_facs_t *facs)
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000580{
Uwe Hermann622824c2010-11-19 15:14:42 +0000581 memset((void *)facs, 0, sizeof(acpi_facs_t));
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000582
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000583 memcpy(facs->signature, "FACS", 4);
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000584 facs->length = sizeof(acpi_facs_t);
585 facs->hardware_signature = 0;
586 facs->firmware_waking_vector = 0;
587 facs->global_lock = 0;
588 facs->flags = 0;
589 facs->x_firmware_waking_vector_l = 0;
590 facs->x_firmware_waking_vector_h = 0;
Uwe Hermann622824c2010-11-19 15:14:42 +0000591 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 +0000592}
Stefan Reinauer777224c2005-01-19 14:06:41 +0000593
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100594static void acpi_write_rsdt(acpi_rsdt_t *rsdt, char *oem_id, char *oem_table_id)
Stefan Reinauer14e22772010-04-27 06:56:47 +0000595{
Uwe Hermann622824c2010-11-19 15:14:42 +0000596 acpi_header_t *header = &(rsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000597
Uwe Hermann622824c2010-11-19 15:14:42 +0000598 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000599 memcpy(header->signature, "RSDT", 4);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100600 memcpy(header->oem_id, oem_id, 6);
601 memcpy(header->oem_table_id, oem_table_id, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000602 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000603
Stefan Reinauer688b3852004-01-28 16:56:14 +0000604 header->length = sizeof(acpi_rsdt_t);
Uwe Hermann622824c2010-11-19 15:14:42 +0000605 header->revision = 1; /* ACPI 1.0/2.0/3.0/4.0: 1 */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000606
Uwe Hermann622824c2010-11-19 15:14:42 +0000607 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauer688b3852004-01-28 16:56:14 +0000608
Uwe Hermann622824c2010-11-19 15:14:42 +0000609 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000610 header->checksum = acpi_checksum((void *)rsdt, sizeof(acpi_rsdt_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000611}
612
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100613static void acpi_write_xsdt(acpi_xsdt_t *xsdt, char *oem_id, char *oem_table_id)
Stefan Reinauer14e22772010-04-27 06:56:47 +0000614{
Uwe Hermann622824c2010-11-19 15:14:42 +0000615 acpi_header_t *header = &(xsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000616
Uwe Hermann622824c2010-11-19 15:14:42 +0000617 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000618 memcpy(header->signature, "XSDT", 4);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100619 memcpy(header->oem_id, oem_id, 6);
620 memcpy(header->oem_table_id, oem_table_id, 8);
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000621 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000622
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000623 header->length = sizeof(acpi_xsdt_t);
Uwe Hermann622824c2010-11-19 15:14:42 +0000624 header->revision = 1; /* ACPI 1.0: N/A, 2.0/3.0/4.0: 1 */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000625
Uwe Hermann622824c2010-11-19 15:14:42 +0000626 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000627
Uwe Hermann622824c2010-11-19 15:14:42 +0000628 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000629 header->checksum = acpi_checksum((void *)xsdt, sizeof(acpi_xsdt_t));
630}
631
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100632static void acpi_write_rsdp(acpi_rsdp_t *rsdp, acpi_rsdt_t *rsdt,
633 acpi_xsdt_t *xsdt, char *oem_id)
Stefan Reinauer688b3852004-01-28 16:56:14 +0000634{
Stefan Reinauerd18faac2009-11-05 18:06:43 +0000635 memset(rsdp, 0, sizeof(acpi_rsdp_t));
Uwe Hermann622824c2010-11-19 15:14:42 +0000636
Stefan Reinauer688b3852004-01-28 16:56:14 +0000637 memcpy(rsdp->signature, RSDP_SIG, 8);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100638 memcpy(rsdp->oem_id, oem_id, 6);
Uwe Hermann622824c2010-11-19 15:14:42 +0000639
640 rsdp->length = sizeof(acpi_rsdp_t);
Stefan Reinauerdefee172015-06-18 01:11:19 -0700641 rsdp->rsdt_address = (uintptr_t)rsdt;
Uwe Hermann622824c2010-11-19 15:14:42 +0000642
643 /*
644 * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2.
645 *
646 * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
647 * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
648 * revision 0).
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000649 */
650 if (xsdt == NULL) {
Uwe Hermann622824c2010-11-19 15:14:42 +0000651 rsdp->revision = 0;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000652 } else {
Stefan Reinauerdefee172015-06-18 01:11:19 -0700653 rsdp->xsdt_address = (u64)(uintptr_t)xsdt;
Uwe Hermann622824c2010-11-19 15:14:42 +0000654 rsdp->revision = 2;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000655 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000656
657 /* Calculate checksums. */
658 rsdp->checksum = acpi_checksum((void *)rsdp, 20);
659 rsdp->ext_checksum = acpi_checksum((void *)rsdp, sizeof(acpi_rsdp_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000660}
661
zbaocaf494c82012-04-13 13:57:14 +0800662unsigned long acpi_create_hest_error_source(acpi_hest_t *hest, acpi_hest_esd_t *esd, u16 type, void *data, u16 data_len)
663{
664 acpi_header_t *header = &(hest->header);
665 acpi_hest_hen_t *hen;
666 void *pos;
667 u16 len;
668
669 pos = esd;
670 memset(pos, 0, sizeof(acpi_hest_esd_t));
671 len = 0;
672 esd->type = type; /* MCE */
673 esd->source_id = hest->error_source_count;
674 esd->flags = 0; /* FIRMWARE_FIRST */
675 esd->enabled = 1;
676 esd->prealloc_erecords = 1;
677 esd->max_section_per_record = 0x1;
678
679 len += sizeof(acpi_hest_esd_t);
680 pos = esd + 1;
681
682 switch (type) {
683 case 0: /* MCE */
684 break;
685 case 1: /* CMC */
686 hen = (acpi_hest_hen_t *) (pos);
687 memset(pos, 0, sizeof(acpi_hest_hen_t));
688 hen->type = 3; /* SCI? */
689 hen->length = sizeof(acpi_hest_hen_t);
690 hen->conf_we = 0; /* Configuration Write Enable. */
691 hen->poll_interval = 0;
692 hen->vector = 0;
693 hen->sw2poll_threshold_val = 0;
694 hen->sw2poll_threshold_win = 0;
695 hen->error_threshold_val = 0;
696 hen->error_threshold_win = 0;
697 len += sizeof(acpi_hest_hen_t);
698 pos = hen + 1;
699 break;
700 case 2: /* NMI */
701 case 6: /* AER Root Port */
702 case 7: /* AER Endpoint */
703 case 8: /* AER Bridge */
704 case 9: /* Generic Hardware Error Source. */
705 /* TODO: */
706 break;
707 default:
708 printk(BIOS_DEBUG, "Invalid type of Error Source.");
709 break;
710 }
711 hest->error_source_count ++;
712
713 memcpy(pos, data, data_len);
714 len += data_len;
715 header->length += len;
716
717 return len;
718}
719
720/* ACPI 4.0 */
Vladimir Serbinenko807127f2014-11-09 13:36:18 +0100721void acpi_write_hest(acpi_hest_t *hest,
722 unsigned long (*acpi_fill_hest)(acpi_hest_t *hest))
zbaocaf494c82012-04-13 13:57:14 +0800723{
724 acpi_header_t *header = &(hest->header);
725
726 memset(hest, 0, sizeof(acpi_hest_t));
727
728 memcpy(header->signature, "HEST", 4);
729 memcpy(header->oem_id, OEM_ID, 6);
730 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
731 memcpy(header->asl_compiler_id, ASLC, 4);
732 header->length += sizeof(acpi_hest_t);
733 header->revision = 1;
734
735 acpi_fill_hest(hest);
736
737 /* Calculate checksums. */
738 header->checksum = acpi_checksum((void *)hest, header->length);
739}
740
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +0200741#if IS_ENABLED(CONFIG_COMMON_FADT)
742void acpi_create_fadt(acpi_fadt_t *fadt,acpi_facs_t *facs, void *dsdt)
743{
744 acpi_header_t *header = &(fadt->header);
745
746 memset((void *) fadt, 0, sizeof(acpi_fadt_t));
747 memcpy(header->signature, "FACP", 4);
748 header->length = sizeof(acpi_fadt_t);
Vladimir Serbinenkoab83ef02014-10-25 15:18:25 +0200749 header->revision = 4;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +0200750 memcpy(header->oem_id, OEM_ID, 6);
751 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
752 memcpy(header->asl_compiler_id, ASLC, 4);
753 header->asl_compiler_revision = 0;
754
755 fadt->firmware_ctrl = (unsigned long) facs;
756 fadt->dsdt = (unsigned long) dsdt;
757
758 fadt->x_firmware_ctl_l = (unsigned long)facs;
759 fadt->x_firmware_ctl_h = 0;
760 fadt->x_dsdt_l = (unsigned long)dsdt;
761 fadt->x_dsdt_h = 0;
762
763 if(IS_ENABLED(CONFIG_SYSTEM_TYPE_LAPTOP)) {
764 fadt->preferred_pm_profile = PM_MOBILE;
765 } else {
766 fadt->preferred_pm_profile = PM_DESKTOP;
767 }
768
Vladimir Serbinenkoab83ef02014-10-25 15:18:25 +0200769 acpi_fill_fadt(fadt);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +0200770
771 header->checksum =
772 acpi_checksum((void *) fadt, header->length);
773}
774#endif
775
Vladimir Serbinenko41877d82014-09-01 22:18:01 +0200776unsigned long __attribute__ ((weak)) fw_cfg_acpi_tables(unsigned long start)
777{
778 return 0;
779}
780
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200781unsigned long write_acpi_tables(unsigned long start)
782{
783 unsigned long current;
784 acpi_rsdp_t *rsdp;
785 acpi_rsdt_t *rsdt;
786 acpi_xsdt_t *xsdt;
787 acpi_fadt_t *fadt;
788 acpi_facs_t *facs;
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100789 acpi_header_t *slic_file, *slic;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200790 acpi_header_t *ssdt;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +0200791 acpi_header_t *dsdt_file, *dsdt;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200792 acpi_mcfg_t *mcfg;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200793 acpi_tcpa_t *tcpa;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200794 acpi_madt_t *madt;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100795 struct device *dev;
Vladimir Serbinenko41877d82014-09-01 22:18:01 +0200796 unsigned long fw;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +0200797 size_t slic_size, dsdt_size;
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100798 char oem_id[6], oem_table_id[8];
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200799
800 current = start;
801
802 /* Align ACPI tables to 16byte */
Aaron Durbin07a1b282015-12-10 17:07:38 -0600803 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200804
Vladimir Serbinenko41877d82014-09-01 22:18:01 +0200805 fw = fw_cfg_acpi_tables(current);
806 if (fw)
807 return fw;
808
Vladimir Serbinenkoa4cf83d2015-06-02 21:40:29 +0200809 dsdt_file = cbfs_boot_map_with_leak(
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +0200810 CONFIG_CBFS_PREFIX "/dsdt.aml",
811 CBFS_TYPE_RAW, &dsdt_size);
812 if (!dsdt_file) {
813 printk(BIOS_ERR, "No DSDT file, skipping ACPI tables\n");
814 return current;
815 }
816
817 if (dsdt_file->length > dsdt_size
818 || dsdt_file->length < sizeof (acpi_header_t)
819 || memcmp(dsdt_file->signature, "DSDT", 4) != 0) {
820 printk(BIOS_ERR, "Invalid DSDT file, skipping ACPI tables\n");
821 return current;
822 }
823
Aaron Durbin899d13d2015-05-15 23:39:23 -0500824 slic_file = cbfs_boot_map_with_leak(CONFIG_CBFS_PREFIX "/slic",
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100825 CBFS_TYPE_RAW, &slic_size);
Vladimir Serbinenko2cb29782015-05-31 11:32:09 +0200826 if (slic_file
827 && (slic_file->length > slic_size
828 || slic_file->length < sizeof (acpi_header_t)
829 || memcmp(slic_file->signature, "SLIC", 4) != 0)) {
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100830 slic_file = 0;
831 }
832
833 if (slic_file) {
834 memcpy(oem_id, slic_file->oem_id, 6);
835 memcpy(oem_table_id, slic_file->oem_table_id, 8);
836 } else {
837 memcpy(oem_id, OEM_ID, 6);
838 memcpy(oem_table_id, ACPI_TABLE_CREATOR, 8);
839 }
840
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200841 printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx.\n", start);
842
843 /* We need at least an RSDP and an RSDT Table */
844 rsdp = (acpi_rsdp_t *) current;
845 current += sizeof(acpi_rsdp_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -0600846 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200847 rsdt = (acpi_rsdt_t *) current;
848 current += sizeof(acpi_rsdt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -0600849 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200850 xsdt = (acpi_xsdt_t *) current;
851 current += sizeof(acpi_xsdt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -0600852 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200853
854 /* clear all table memory */
855 memset((void *) start, 0, current - start);
856
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100857 acpi_write_rsdp(rsdp, rsdt, xsdt, oem_id);
858 acpi_write_rsdt(rsdt, oem_id, oem_table_id);
859 acpi_write_xsdt(xsdt, oem_id, oem_table_id);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200860
861 printk(BIOS_DEBUG, "ACPI: * FACS\n");
Patrick Georgi133108a2015-08-08 23:20:58 +0200862 current = (ALIGN(current, 64));
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200863 facs = (acpi_facs_t *) current;
864 current += sizeof(acpi_facs_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -0600865 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200866 acpi_create_facs(facs);
867
868 printk(BIOS_DEBUG, "ACPI: * DSDT\n");
869 dsdt = (acpi_header_t *) current;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +0200870 memcpy(dsdt, dsdt_file, sizeof(acpi_header_t));
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +0200871 if (dsdt->length >= sizeof(acpi_header_t)) {
872 current += sizeof(acpi_header_t);
873
874 acpigen_set_current((char *) current);
875 for (dev = all_devices; dev; dev = dev->next)
876 if (dev->ops && dev->ops->acpi_inject_dsdt_generator) {
Alexander Couzensa90dad12015-04-12 21:49:46 +0200877 dev->ops->acpi_inject_dsdt_generator(dev);
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +0200878 }
879 current = (unsigned long) acpigen_get_current();
880 memcpy((char *)current,
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +0200881 (char *)dsdt_file + sizeof(acpi_header_t),
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +0200882 dsdt->length - sizeof(acpi_header_t));
883 current += dsdt->length - sizeof(acpi_header_t);
884
885 /* (Re)calculate length and checksum. */
886 dsdt->length = current - (unsigned long)dsdt;
887 dsdt->checksum = 0;
888 dsdt->checksum = acpi_checksum((void *)dsdt, dsdt->length);
889 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200890
Aaron Durbin07a1b282015-12-10 17:07:38 -0600891 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200892
893 printk(BIOS_DEBUG, "ACPI: * FADT\n");
894 fadt = (acpi_fadt_t *) current;
895 current += sizeof(acpi_fadt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -0600896 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200897
898 acpi_create_fadt(fadt, facs, dsdt);
899 acpi_add_table(rsdp, fadt);
900
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100901 if (slic_file) {
902 printk(BIOS_DEBUG, "ACPI: * SLIC\n");
903 slic = (acpi_header_t *)current;
904 memcpy(slic, slic_file, slic_file->length);
905 current += slic_file->length;
Aaron Durbin07a1b282015-12-10 17:07:38 -0600906 current = acpi_align_current(current);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100907 acpi_add_table(rsdp, slic);
908 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200909
910 printk(BIOS_DEBUG, "ACPI: * SSDT\n");
911 ssdt = (acpi_header_t *)current;
912 acpi_create_ssdt_generator(ssdt, ACPI_TABLE_CREATOR);
Vladimir Serbinenko9310df82014-10-10 20:40:41 +0200913 if (ssdt->length > sizeof(acpi_header_t)) {
914 current += ssdt->length;
915 acpi_add_table(rsdp, ssdt);
Aaron Durbin07a1b282015-12-10 17:07:38 -0600916 current = acpi_align_current(current);
Vladimir Serbinenko9310df82014-10-10 20:40:41 +0200917 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200918
919 printk(BIOS_DEBUG, "ACPI: * MCFG\n");
920 mcfg = (acpi_mcfg_t *) current;
921 acpi_create_mcfg(mcfg);
922 if (mcfg->header.length > sizeof(acpi_mcfg_t)) {
923 current += mcfg->header.length;
Aaron Durbin07a1b282015-12-10 17:07:38 -0600924 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200925 acpi_add_table(rsdp, mcfg);
926 }
927
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200928 printk(BIOS_DEBUG, "ACPI: * TCPA\n");
929 tcpa = (acpi_tcpa_t *) current;
930 acpi_create_tcpa(tcpa);
931 if (tcpa->header.length >= sizeof(acpi_tcpa_t)) {
932 current += tcpa->header.length;
Aaron Durbin07a1b282015-12-10 17:07:38 -0600933 current = acpi_align_current(current);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200934 acpi_add_table(rsdp, tcpa);
935 }
936
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200937 printk(BIOS_DEBUG, "ACPI: * MADT\n");
938
939 madt = (acpi_madt_t *) current;
940 acpi_create_madt(madt);
941 if (madt->header.length > sizeof(acpi_madt_t)) {
942 current+=madt->header.length;
943 acpi_add_table(rsdp,madt);
944 }
Aaron Durbin07a1b282015-12-10 17:07:38 -0600945 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200946
947 printk(BIOS_DEBUG, "current = %lx\n", current);
948
949 for (dev = all_devices; dev; dev = dev->next) {
950 if (dev->ops && dev->ops->write_acpi_tables) {
Alexander Couzens83fc32f2015-04-12 22:28:37 +0200951 current = dev->ops->write_acpi_tables(dev, current, rsdp);
Aaron Durbin07a1b282015-12-10 17:07:38 -0600952 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200953 }
954 }
955
956 printk(BIOS_INFO, "ACPI: done.\n");
957 return current;
958}
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200959
Rudolf Marek33cafe52009-04-13 18:07:02 +0000960static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp)
961{
962 if (strncmp((char *)rsdp, RSDP_SIG, sizeof(RSDP_SIG) - 1) != 0)
963 return NULL;
964
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000965 printk(BIOS_DEBUG, "Looking on %p for valid checksum\n", rsdp);
Rudolf Marek33cafe52009-04-13 18:07:02 +0000966
967 if (acpi_checksum((void *)rsdp, 20) != 0)
968 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000969 printk(BIOS_DEBUG, "Checksum 1 passed\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +0000970
971 if ((rsdp->revision > 1) && (acpi_checksum((void *)rsdp,
972 rsdp->length) != 0))
973 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000974 printk(BIOS_DEBUG, "Checksum 2 passed all OK\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +0000975
976 return rsdp;
977}
978
Rudolf Marek33cafe52009-04-13 18:07:02 +0000979void *acpi_find_wakeup_vector(void)
980{
981 char *p, *end;
Rudolf Marek33cafe52009-04-13 18:07:02 +0000982 acpi_rsdt_t *rsdt;
983 acpi_facs_t *facs;
Martin Rothc5f49262012-12-14 19:17:55 -0700984 acpi_fadt_t *fadt = NULL;
Kyösti Mälkki072d436b2016-06-17 08:44:40 +0300985 acpi_rsdp_t *rsdp = NULL;
Uwe Hermann622824c2010-11-19 15:14:42 +0000986 void *wake_vec;
Rudolf Marek33cafe52009-04-13 18:07:02 +0000987 int i;
988
Rudolf Marek33cafe52009-04-13 18:07:02 +0000989 if (!acpi_is_wakeup())
990 return NULL;
991
Uwe Hermann622824c2010-11-19 15:14:42 +0000992 printk(BIOS_DEBUG, "Trying to find the wakeup vector...\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +0000993
Uwe Hermann622824c2010-11-19 15:14:42 +0000994 /* Find RSDP. */
995 for (p = (char *)0xe0000; p < (char *)0xfffff; p += 16) {
996 if ((rsdp = valid_rsdp((acpi_rsdp_t *)p)))
Rudolf Marek33cafe52009-04-13 18:07:02 +0000997 break;
998 }
999
1000 if (rsdp == NULL)
1001 return NULL;
1002
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001003 printk(BIOS_DEBUG, "RSDP found at %p\n", rsdp);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001004 rsdt = (acpi_rsdt_t *)(uintptr_t)rsdp->rsdt_address;
Stefan Reinauer14e22772010-04-27 06:56:47 +00001005
Uwe Hermann622824c2010-11-19 15:14:42 +00001006 end = (char *)rsdt + rsdt->header.length;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001007 printk(BIOS_DEBUG, "RSDT found at %p ends at %p\n", rsdt, end);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001008
Uwe Hermann622824c2010-11-19 15:14:42 +00001009 for (i = 0; ((char *)&rsdt->entry[i]) < end; i++) {
Stefan Reinauer71a30182015-07-30 16:28:13 -07001010 fadt = (acpi_fadt_t *)(uintptr_t)rsdt->entry[i];
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001011 if (strncmp((char *)fadt, "FACP", 4) == 0)
Rudolf Marek33cafe52009-04-13 18:07:02 +00001012 break;
1013 fadt = NULL;
1014 }
1015
1016 if (fadt == NULL)
1017 return NULL;
1018
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001019 printk(BIOS_DEBUG, "FADT found at %p\n", fadt);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001020 facs = (acpi_facs_t *)(uintptr_t)fadt->firmware_ctrl;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001021
Stefan Reinauer7e9771c2009-04-22 08:17:38 +00001022 if (facs == NULL) {
Uwe Hermann622824c2010-11-19 15:14:42 +00001023 printk(BIOS_DEBUG, "No FACS found, wake up from S3 not "
1024 "possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001025 return NULL;
Stefan Reinauer7e9771c2009-04-22 08:17:38 +00001026 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001027
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001028 printk(BIOS_DEBUG, "FACS found at %p\n", facs);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001029 wake_vec = (void *)(uintptr_t)facs->firmware_waking_vector;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001030 printk(BIOS_DEBUG, "OS waking vector is %p\n", wake_vec);
Uwe Hermann622824c2010-11-19 15:14:42 +00001031
Rudolf Marek33cafe52009-04-13 18:07:02 +00001032 return wake_vec;
1033}
1034
Duncan Laurie11290c42012-10-03 19:07:05 -07001035void acpi_save_gnvs(u32 gnvs_address)
1036{
Duncan Laurie9c07c8f2013-03-22 11:08:39 -07001037 u32 *gnvs = cbmem_add(CBMEM_ID_ACPI_GNVS_PTR, sizeof(*gnvs));
Duncan Laurie11290c42012-10-03 19:07:05 -07001038 if (gnvs)
1039 *gnvs = gnvs_address;
1040}