blob: a5c5c494737c6a4a8440ad1cde6d7b84f8d74578 [file] [log] [blame]
Patrick Georgi11f00792020-03-04 15:10:45 +01001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Stefan Reinauer688b3852004-01-28 16:56:14 +00003/*
Martin Roth20bbd812019-08-30 21:09:37 -06004 * coreboot ACPI Table support
Stefan Reinauer777224c2005-01-19 14:06:41 +00005 */
6
Stefan Reinauer14e22772010-04-27 06:56:47 +00007/*
Stefan Reinauer777224c2005-01-19 14:06:41 +00008 * Each system port implementing ACPI has to provide two functions:
Stefan Reinauer14e22772010-04-27 06:56:47 +00009 *
Stefan Reinauer777224c2005-01-19 14:06:41 +000010 * write_acpi_tables()
11 * acpi_dump_apics()
Stefan Reinauer14e22772010-04-27 06:56:47 +000012 *
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000013 * See Kontron 986LCD-M port for a good example of an ACPI implementation
14 * in coreboot.
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000015 */
Stefan Reinauer688b3852004-01-28 16:56:14 +000016
17#include <console/console.h>
18#include <string.h>
19#include <arch/acpi.h>
Martin Rotha66df492016-09-06 10:22:34 -060020#include <arch/acpi_ivrs.h>
Rudolf Marek293b5f52009-02-01 18:35:15 +000021#include <arch/acpigen.h>
Stefan Reinauer777224c2005-01-19 14:06:41 +000022#include <device/pci.h>
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000023#include <cbmem.h>
Elyes HAOUASe2d152c2019-06-21 07:06:50 +020024#include <commonlib/helpers.h>
Patrick Georgic8feedd2012-02-16 18:43:25 +010025#include <cpu/x86/lapic_def.h>
Duncan Laurie11290c42012-10-03 19:07:05 -070026#include <cpu/cpu.h>
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +010027#include <cbfs.h>
Elyes HAOUAS26071aa2019-02-15 08:21:33 +010028#include <version.h>
Werner Zehdb561e62019-02-19 13:39:56 +010029#include <commonlib/sort.h>
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000030
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +020031static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp);
32
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000033u8 acpi_checksum(u8 *table, u32 length)
Stefan Reinauer688b3852004-01-28 16:56:14 +000034{
Uwe Hermann622824c2010-11-19 15:14:42 +000035 u8 ret = 0;
Stefan Reinauer688b3852004-01-28 16:56:14 +000036 while (length--) {
37 ret += *table;
38 table++;
39 }
Stefan Reinauera7648c22004-01-29 17:31:34 +000040 return -ret;
Stefan Reinauer688b3852004-01-28 16:56:14 +000041}
42
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000043/**
Uwe Hermann622824c2010-11-19 15:14:42 +000044 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
45 * and checksum.
Stefan Reinauer06feb882004-02-03 16:11:35 +000046 */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000047void acpi_add_table(acpi_rsdp_t *rsdp, void *table)
Stefan Reinauer688b3852004-01-28 16:56:14 +000048{
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000049 int i, entries_num;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000050 acpi_rsdt_t *rsdt;
51 acpi_xsdt_t *xsdt = NULL;
52
Uwe Hermann622824c2010-11-19 15:14:42 +000053 /* The RSDT is mandatory... */
Stefan Reinauerdefee172015-06-18 01:11:19 -070054 rsdt = (acpi_rsdt_t *)(uintptr_t)rsdp->rsdt_address;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000055
Uwe Hermann622824c2010-11-19 15:14:42 +000056 /* ...while the XSDT is not. */
57 if (rsdp->xsdt_address)
Stefan Reinauerdefee172015-06-18 01:11:19 -070058 xsdt = (acpi_xsdt_t *)((uintptr_t)rsdp->xsdt_address);
Stefan Reinauer14e22772010-04-27 06:56:47 +000059
Uwe Hermann622824c2010-11-19 15:14:42 +000060 /* This should always be MAX_ACPI_TABLES. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000061 entries_num = ARRAY_SIZE(rsdt->entry);
Stefan Reinauer14e22772010-04-27 06:56:47 +000062
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000063 for (i = 0; i < entries_num; i++) {
Uwe Hermann622824c2010-11-19 15:14:42 +000064 if (rsdt->entry[i] == 0)
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000065 break;
Stefan Reinauer688b3852004-01-28 16:56:14 +000066 }
67
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000068 if (i >= entries_num) {
Uwe Hermann622824c2010-11-19 15:14:42 +000069 printk(BIOS_ERR, "ACPI: Error: Could not add ACPI table, "
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +030070 "too many tables.\n");
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000071 return;
72 }
73
Uwe Hermann622824c2010-11-19 15:14:42 +000074 /* Add table to the RSDT. */
Stefan Reinauerdefee172015-06-18 01:11:19 -070075 rsdt->entry[i] = (uintptr_t)table;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000076
Uwe Hermann622824c2010-11-19 15:14:42 +000077 /* Fix RSDT length or the kernel will assume invalid entries. */
78 rsdt->header.length = sizeof(acpi_header_t) + (sizeof(u32) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000079
Uwe Hermann622824c2010-11-19 15:14:42 +000080 /* Re-calculate checksum. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000081 rsdt->header.checksum = 0; /* Hope this won't get optimized away */
Uwe Hermann622824c2010-11-19 15:14:42 +000082 rsdt->header.checksum = acpi_checksum((u8 *)rsdt, rsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000083
Uwe Hermann622824c2010-11-19 15:14:42 +000084 /*
85 * And now the same thing for the XSDT. We use the same index as for
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000086 * now we want the XSDT and RSDT to always be in sync in coreboot.
87 */
88 if (xsdt) {
Uwe Hermann622824c2010-11-19 15:14:42 +000089 /* Add table to the XSDT. */
Stefan Reinauerdefee172015-06-18 01:11:19 -070090 xsdt->entry[i] = (u64)(uintptr_t)table;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000091
Uwe Hermann622824c2010-11-19 15:14:42 +000092 /* Fix XSDT length. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000093 xsdt->header.length = sizeof(acpi_header_t) +
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +030094 (sizeof(u64) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000095
Uwe Hermann622824c2010-11-19 15:14:42 +000096 /* Re-calculate checksum. */
97 xsdt->header.checksum = 0;
98 xsdt->header.checksum = acpi_checksum((u8 *)xsdt,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +030099 xsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000100 }
101
Uwe Hermann622824c2010-11-19 15:14:42 +0000102 printk(BIOS_DEBUG, "ACPI: added table %d/%d, length now %d\n",
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300103 i + 1, entries_num, rsdt->header.length);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000104}
105
Uwe Hermann622824c2010-11-19 15:14:42 +0000106int acpi_create_mcfg_mmconfig(acpi_mcfg_mmconfig_t *mmconfig, u32 base,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300107 u16 seg_nr, u8 start, u8 end)
Stefan Reinauer26d431a2009-01-20 19:17:51 +0000108{
Vladimir Serbinenko60fccdc2014-10-05 11:05:16 +0200109 memset(mmconfig, 0, sizeof(*mmconfig));
Rudolf Mareke6409f22007-11-03 12:50:26 +0000110 mmconfig->base_address = base;
111 mmconfig->base_reserved = 0;
112 mmconfig->pci_segment_group_number = seg_nr;
113 mmconfig->start_bus_number = start;
114 mmconfig->end_bus_number = end;
Uwe Hermann622824c2010-11-19 15:14:42 +0000115
116 return sizeof(acpi_mcfg_mmconfig_t);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000117}
118
Stefan Reinauer777224c2005-01-19 14:06:41 +0000119int acpi_create_madt_lapic(acpi_madt_lapic_t *lapic, u8 cpu, u8 apic)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000120{
Himanshu Sahdev8c09b822019-10-21 18:50:50 +0530121 lapic->type = LOCAL_APIC; /* Local APIC structure */
Uwe Hermann622824c2010-11-19 15:14:42 +0000122 lapic->length = sizeof(acpi_madt_lapic_t);
123 lapic->flags = (1 << 0); /* Processor/LAPIC enabled */
124 lapic->processor_id = cpu;
125 lapic->apic_id = apic;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000126
Uwe Hermann622824c2010-11-19 15:14:42 +0000127 return lapic->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000128}
129
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000130unsigned long acpi_create_madt_lapics(unsigned long current)
131{
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100132 struct device *cpu;
Werner Zeh423adfb2019-03-06 09:31:02 +0100133 int index, apic_ids[CONFIG_MAX_CPUS] = {0}, num_cpus = 0;
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000134
Uwe Hermann622824c2010-11-19 15:14:42 +0000135 for (cpu = all_devices; cpu; cpu = cpu->next) {
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000136 if ((cpu->path.type != DEVICE_PATH_APIC) ||
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800137 (cpu->bus->dev->path.type != DEVICE_PATH_CPU_CLUSTER)) {
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000138 continue;
139 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000140 if (!cpu->enabled)
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000141 continue;
Werner Zehdb561e62019-02-19 13:39:56 +0100142 if (num_cpus >= ARRAY_SIZE(apic_ids))
143 break;
144 apic_ids[num_cpus++] = cpu->path.apic.apic_id;
145 }
Werner Zehfedb36e2019-03-12 07:07:50 +0100146 if (num_cpus > 1)
147 bubblesort(apic_ids, num_cpus, NUM_ASCENDING);
Werner Zehdb561e62019-02-19 13:39:56 +0100148 for (index = 0; index < num_cpus; index++) {
Uwe Hermann622824c2010-11-19 15:14:42 +0000149 current += acpi_create_madt_lapic((acpi_madt_lapic_t *)current,
Werner Zehdb561e62019-02-19 13:39:56 +0100150 index, apic_ids[index]);
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000151 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000152
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000153 return current;
154}
155
Uwe Hermann622824c2010-11-19 15:14:42 +0000156int acpi_create_madt_ioapic(acpi_madt_ioapic_t *ioapic, u8 id, u32 addr,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300157 u32 gsi_base)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000158{
Himanshu Sahdev8c09b822019-10-21 18:50:50 +0530159 ioapic->type = IO_APIC; /* I/O APIC structure */
Uwe Hermann622824c2010-11-19 15:14:42 +0000160 ioapic->length = sizeof(acpi_madt_ioapic_t);
161 ioapic->reserved = 0x00;
162 ioapic->gsi_base = gsi_base;
163 ioapic->ioapic_id = id;
164 ioapic->ioapic_addr = addr;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000165
Uwe Hermann622824c2010-11-19 15:14:42 +0000166 return ioapic->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000167}
168
Stefan Reinauer777224c2005-01-19 14:06:41 +0000169int acpi_create_madt_irqoverride(acpi_madt_irqoverride_t *irqoverride,
Stefan Reinauer06feb882004-02-03 16:11:35 +0000170 u8 bus, u8 source, u32 gsirq, u16 flags)
171{
Himanshu Sahdev8c09b822019-10-21 18:50:50 +0530172 irqoverride->type = IRQ_SOURCE_OVERRIDE; /* Interrupt source override */
Uwe Hermann622824c2010-11-19 15:14:42 +0000173 irqoverride->length = sizeof(acpi_madt_irqoverride_t);
174 irqoverride->bus = bus;
175 irqoverride->source = source;
176 irqoverride->gsirq = gsirq;
177 irqoverride->flags = flags;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000178
Uwe Hermann622824c2010-11-19 15:14:42 +0000179 return irqoverride->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000180}
181
Stefan Reinauer777224c2005-01-19 14:06:41 +0000182int acpi_create_madt_lapic_nmi(acpi_madt_lapic_nmi_t *lapic_nmi, u8 cpu,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300183 u16 flags, u8 lint)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000184{
Himanshu Sahdev8c09b822019-10-21 18:50:50 +0530185 lapic_nmi->type = LOCAL_APIC_NMI; /* Local APIC NMI structure */
Uwe Hermann622824c2010-11-19 15:14:42 +0000186 lapic_nmi->length = sizeof(acpi_madt_lapic_nmi_t);
187 lapic_nmi->flags = flags;
188 lapic_nmi->processor_id = cpu;
189 lapic_nmi->lint = lint;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000190
Uwe Hermann622824c2010-11-19 15:14:42 +0000191 return lapic_nmi->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000192}
193
Stefan Reinauer777224c2005-01-19 14:06:41 +0000194void acpi_create_madt(acpi_madt_t *madt)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000195{
Uwe Hermann622824c2010-11-19 15:14:42 +0000196 acpi_header_t *header = &(madt->header);
197 unsigned long current = (unsigned long)madt + sizeof(acpi_madt_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000198
Stefan Reinauer06feb882004-02-03 16:11:35 +0000199 memset((void *)madt, 0, sizeof(acpi_madt_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000200
John Zhao2ba303e2019-05-28 16:48:14 -0700201 if (!header)
202 return;
203
Uwe Hermann622824c2010-11-19 15:14:42 +0000204 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000205 memcpy(header->signature, "APIC", 4);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000206 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000207 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000208 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000209
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100210 header->asl_compiler_revision = asl_revision;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000211 header->length = sizeof(acpi_madt_t);
Marc Jones93a51762018-08-22 18:57:24 -0600212 header->revision = get_acpi_table_revision(MADT);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000213
Uwe Hermann622824c2010-11-19 15:14:42 +0000214 madt->lapic_addr = LOCAL_APIC_ADDR;
Nico Huber9df72e02018-11-24 18:25:50 +0100215 if (CONFIG(ACPI_HAVE_PCAT_8259))
216 madt->flags |= 1;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000217
Stefan Reinauerf622d592005-11-26 16:56:05 +0000218 current = acpi_fill_madt(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000219
Uwe Hermann622824c2010-11-19 15:14:42 +0000220 /* (Re)calculate length and checksum. */
221 header->length = current - (unsigned long)madt;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000222
Uwe Hermann622824c2010-11-19 15:14:42 +0000223 header->checksum = acpi_checksum((void *)madt, header->length);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000224}
225
Uwe Hermann622824c2010-11-19 15:14:42 +0000226/* MCFG is defined in the PCI Firmware Specification 3.0. */
Rudolf Mareke6409f22007-11-03 12:50:26 +0000227void acpi_create_mcfg(acpi_mcfg_t *mcfg)
228{
Uwe Hermann622824c2010-11-19 15:14:42 +0000229 acpi_header_t *header = &(mcfg->header);
230 unsigned long current = (unsigned long)mcfg + sizeof(acpi_mcfg_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000231
Rudolf Mareke6409f22007-11-03 12:50:26 +0000232 memset((void *)mcfg, 0, sizeof(acpi_mcfg_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000233
John Zhao2ba303e2019-05-28 16:48:14 -0700234 if (!header)
235 return;
236
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
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100243 header->asl_compiler_revision = asl_revision;
Rudolf Mareke6409f22007-11-03 12:50:26 +0000244 header->length = sizeof(acpi_mcfg_t);
Marc Jones93a51762018-08-22 18:57:24 -0600245 header->revision = get_acpi_table_revision(MCFG);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000246
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;
Philipp Deppenwiese791ba972018-07-30 01:15:20 +0200259 ce = cbmem_entry_find(CBMEM_ID_TCPA_TCG_LOG);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200260 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 }
Philipp Deppenwiese791ba972018-07-30 01:15:20 +0200266 lasa = cbmem_add(CBMEM_ID_TCPA_TCG_LOG, tcpa_default_log_len);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200267 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);
Lee Leahy024b13d2017-03-16 13:41:11 -0700273 memset(lasa, 0, tcpa_default_log_len);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200274
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);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700288 if (!lasa)
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200289 return;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200290
John Zhao2ba303e2019-05-28 16:48:14 -0700291 if (!header)
292 return;
293
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200294 /* Fill out header fields. */
295 memcpy(header->signature, "TCPA", 4);
296 memcpy(header->oem_id, OEM_ID, 6);
297 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
298 memcpy(header->asl_compiler_id, ASLC, 4);
299
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100300 header->asl_compiler_revision = asl_revision;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200301 header->length = sizeof(acpi_tcpa_t);
Marc Jones93a51762018-08-22 18:57:24 -0600302 header->revision = get_acpi_table_revision(TCPA);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200303
304 tcpa->platform_class = 0;
305 tcpa->laml = tcpa_log_len;
Stefan Reinauerdefee172015-06-18 01:11:19 -0700306 tcpa->lasa = (uintptr_t) lasa;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200307
308 /* Calculate checksum. */
309 header->checksum = acpi_checksum((void *)tcpa, header->length);
310}
311
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100312static void *get_tpm2_log(u32 *size)
313{
314 const struct cbmem_entry *ce;
315 const u32 tpm2_default_log_len = 0x10000;
316 void *lasa;
317 ce = cbmem_entry_find(CBMEM_ID_TPM2_TCG_LOG);
318 if (ce) {
319 lasa = cbmem_entry_start(ce);
320 *size = cbmem_entry_size(ce);
321 printk(BIOS_DEBUG, "TPM2 log found at %p\n", lasa);
322 return lasa;
323 }
324 lasa = cbmem_add(CBMEM_ID_TPM2_TCG_LOG, tpm2_default_log_len);
325 if (!lasa) {
326 printk(BIOS_ERR, "TPM2 log creation failed\n");
327 return NULL;
328 }
329
330 printk(BIOS_DEBUG, "TPM2 log created at %p\n", lasa);
331 memset(lasa, 0, tpm2_default_log_len);
332
333 *size = tpm2_default_log_len;
334 return lasa;
335}
336
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200337static void acpi_create_tpm2(acpi_tpm2_t *tpm2)
338{
339 acpi_header_t *header = &(tpm2->header);
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100340 u32 tpm2_log_len;
341 void *lasa;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200342
343 memset((void *)tpm2, 0, sizeof(acpi_tpm2_t));
344
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100345 /*
346 * Some payloads like SeaBIOS depend on log area to use TPM2.
347 * Get the memory size and address of TPM2 log area or initialize it.
348 */
349 lasa = get_tpm2_log(&tpm2_log_len);
350 if (!lasa)
351 tpm2_log_len = 0;
352
John Zhao2ba303e2019-05-28 16:48:14 -0700353 if (!header)
354 return;
355
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200356 /* Fill out header fields. */
357 memcpy(header->signature, "TPM2", 4);
358 memcpy(header->oem_id, OEM_ID, 6);
359 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
360 memcpy(header->asl_compiler_id, ASLC, 4);
361
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100362 header->asl_compiler_revision = asl_revision;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200363 header->length = sizeof(acpi_tpm2_t);
364 header->revision = get_acpi_table_revision(TPM2);
365
366 /* Hard to detect for coreboot. Just set it to 0 */
367 tpm2->platform_class = 0;
Christian Walter26e0d4c2019-07-14 15:47:23 +0200368 if (CONFIG(CRB_TPM)) {
369 /* Must be set to 7 for CRB Support */
370 tpm2->control_area = CONFIG_CRB_TPM_BASE_ADDRESS + 0x40;
371 tpm2->start_method = 7;
372 } else {
373 /* Must be set to 0 for FIFO interface support */
374 tpm2->control_area = 0;
375 tpm2->start_method = 6;
376 }
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200377 memset(tpm2->msp, 0, sizeof(tpm2->msp));
378
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100379 /* Fill the log area size and start address fields. */
380 tpm2->laml = tpm2_log_len;
381 tpm2->lasa = (uintptr_t) lasa;
382
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200383 /* Calculate checksum. */
384 header->checksum = acpi_checksum((void *)tpm2, header->length);
385}
386
Duncan Laurie37319032016-05-26 12:47:05 -0700387static void acpi_ssdt_write_cbtable(void)
388{
389 const struct cbmem_entry *cbtable;
390 uintptr_t base;
391 uint32_t size;
392
393 cbtable = cbmem_entry_find(CBMEM_ID_CBTABLE);
394 if (!cbtable)
395 return;
396 base = (uintptr_t)cbmem_entry_start(cbtable);
397 size = cbmem_entry_size(cbtable);
398
399 acpigen_write_device("CTBL");
400 acpigen_write_coreboot_hid(COREBOOT_ACPI_ID_CBTABLE);
401 acpigen_write_name_integer("_UID", 0);
David Wu5dff3962018-03-21 16:48:53 +0800402 acpigen_write_STA(ACPI_STATUS_DEVICE_HIDDEN_ON);
Duncan Laurie37319032016-05-26 12:47:05 -0700403 acpigen_write_name("_CRS");
404 acpigen_write_resourcetemplate_header();
405 acpigen_write_mem32fixed(0, base, size);
406 acpigen_write_resourcetemplate_footer();
407 acpigen_pop_len();
408}
409
Myles Watson3fe6b702009-10-09 20:13:43 +0000410void acpi_create_ssdt_generator(acpi_header_t *ssdt, const char *oem_table_id)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000411{
Uwe Hermann622824c2010-11-19 15:14:42 +0000412 unsigned long current = (unsigned long)ssdt + sizeof(acpi_header_t);
413
Rudolf Marek293b5f52009-02-01 18:35:15 +0000414 memset((void *)ssdt, 0, sizeof(acpi_header_t));
Uwe Hermann622824c2010-11-19 15:14:42 +0000415
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000416 memcpy(&ssdt->signature, "SSDT", 4);
Marc Jones93a51762018-08-22 18:57:24 -0600417 ssdt->revision = get_acpi_table_revision(SSDT);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000418 memcpy(&ssdt->oem_id, OEM_ID, 6);
419 memcpy(&ssdt->oem_table_id, oem_table_id, 8);
420 ssdt->oem_revision = 42;
Uwe Hermann622824c2010-11-19 15:14:42 +0000421 memcpy(&ssdt->asl_compiler_id, ASLC, 4);
Elyes HAOUAS26071aa2019-02-15 08:21:33 +0100422 ssdt->asl_compiler_revision = asl_revision;
Rudolf Marek293b5f52009-02-01 18:35:15 +0000423 ssdt->length = sizeof(acpi_header_t);
424
Stefan Reinauer8dcd50b2009-03-06 17:24:29 +0000425 acpigen_set_current((char *) current);
Duncan Laurie37319032016-05-26 12:47:05 -0700426
427 /* Write object to declare coreboot tables */
428 acpi_ssdt_write_cbtable();
429
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200430 {
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100431 struct device *dev;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200432 for (dev = all_devices; dev; dev = dev->next)
Nico Huber68680dd2020-03-31 17:34:52 +0200433 if (dev->ops && dev->ops->acpi_fill_ssdt)
434 dev->ops->acpi_fill_ssdt(dev);
Vladimir Serbinenko0a669912014-10-05 14:34:17 +0200435 current = (unsigned long) acpigen_get_current();
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200436 }
Rudolf Marek293b5f52009-02-01 18:35:15 +0000437
Uwe Hermann622824c2010-11-19 15:14:42 +0000438 /* (Re)calculate length and checksum. */
Rudolf Marek293b5f52009-02-01 18:35:15 +0000439 ssdt->length = current - (unsigned long)ssdt;
440 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
441}
442
Stefan Reinauerf622d592005-11-26 16:56:05 +0000443int acpi_create_srat_lapic(acpi_srat_lapic_t *lapic, u8 node, u8 apic)
444{
Myles Watson0b4c9f02009-03-10 18:06:47 +0000445 memset((void *)lapic, 0, sizeof(acpi_srat_lapic_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000446
Uwe Hermann622824c2010-11-19 15:14:42 +0000447 lapic->type = 0; /* Processor local APIC/SAPIC affinity structure */
448 lapic->length = sizeof(acpi_srat_lapic_t);
449 lapic->flags = (1 << 0); /* Enabled (the use of this structure). */
450 lapic->proximity_domain_7_0 = node;
451 /* TODO: proximity_domain_31_8, local SAPIC EID, clock domain. */
452 lapic->apic_id = apic;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000453
Uwe Hermann622824c2010-11-19 15:14:42 +0000454 return lapic->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000455}
456
Uwe Hermann622824c2010-11-19 15:14:42 +0000457int 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 +0300458 u32 flags)
Stefan Reinauerf622d592005-11-26 16:56:05 +0000459{
Uwe Hermann622824c2010-11-19 15:14:42 +0000460 mem->type = 1; /* Memory affinity structure */
461 mem->length = sizeof(acpi_srat_mem_t);
462 mem->base_address_low = (basek << 10);
463 mem->base_address_high = (basek >> (32 - 10));
464 mem->length_low = (sizek << 10);
465 mem->length_high = (sizek >> (32 - 10));
466 mem->proximity_domain = node;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000467 mem->flags = flags;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000468
Uwe Hermann622824c2010-11-19 15:14:42 +0000469 return mem->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000470}
471
Uwe Hermann622824c2010-11-19 15:14:42 +0000472/* http://www.microsoft.com/whdc/system/sysinternals/sratdwn.mspx */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +0200473void acpi_create_srat(acpi_srat_t *srat,
474 unsigned long (*acpi_fill_srat)(unsigned long current))
Stefan Reinauerf622d592005-11-26 16:56:05 +0000475{
Uwe Hermann622824c2010-11-19 15:14:42 +0000476 acpi_header_t *header = &(srat->header);
477 unsigned long current = (unsigned long)srat + sizeof(acpi_srat_t);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000478
Uwe Hermann622824c2010-11-19 15:14:42 +0000479 memset((void *)srat, 0, sizeof(acpi_srat_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000480
John Zhao2ba303e2019-05-28 16:48:14 -0700481 if (!header)
482 return;
483
Uwe Hermann622824c2010-11-19 15:14:42 +0000484 /* Fill out header fields. */
485 memcpy(header->signature, "SRAT", 4);
486 memcpy(header->oem_id, OEM_ID, 6);
487 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
488 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000489
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100490 header->asl_compiler_revision = asl_revision;
Uwe Hermann622824c2010-11-19 15:14:42 +0000491 header->length = sizeof(acpi_srat_t);
Marc Jones93a51762018-08-22 18:57:24 -0600492 header->revision = get_acpi_table_revision(SRAT);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000493
Uwe Hermann622824c2010-11-19 15:14:42 +0000494 srat->resv = 1; /* Spec: Reserved to 1 for backwards compatibility. */
Stefan Reinauerf622d592005-11-26 16:56:05 +0000495
Uwe Hermann622824c2010-11-19 15:14:42 +0000496 current = acpi_fill_srat(current);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000497
Uwe Hermann622824c2010-11-19 15:14:42 +0000498 /* (Re)calculate length and checksum. */
499 header->length = current - (unsigned long)srat;
500 header->checksum = acpi_checksum((void *)srat, header->length);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000501}
502
Nico Hubere561f352015-10-26 11:51:25 +0100503void acpi_create_dmar(acpi_dmar_t *dmar, enum dmar_flags flags,
Lee Leahy024b13d2017-03-16 13:41:11 -0700504 unsigned long (*acpi_fill_dmar)(unsigned long))
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200505{
506 acpi_header_t *header = &(dmar->header);
507 unsigned long current = (unsigned long)dmar + sizeof(acpi_dmar_t);
508
509 memset((void *)dmar, 0, sizeof(acpi_dmar_t));
510
John Zhao2ba303e2019-05-28 16:48:14 -0700511 if (!header)
512 return;
513
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200514 /* Fill out header fields. */
515 memcpy(header->signature, "DMAR", 4);
516 memcpy(header->oem_id, OEM_ID, 6);
517 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
518 memcpy(header->asl_compiler_id, ASLC, 4);
519
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100520 header->asl_compiler_revision = asl_revision;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200521 header->length = sizeof(acpi_dmar_t);
Marc Jones93a51762018-08-22 18:57:24 -0600522 header->revision = get_acpi_table_revision(DMAR);
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200523
Jacob Laskaf3f654d2016-03-15 21:53:27 -0500524 dmar->host_address_width = cpu_phys_address_size() - 1;
Nico Hubere561f352015-10-26 11:51:25 +0100525 dmar->flags = flags;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200526
527 current = acpi_fill_dmar(current);
528
529 /* (Re)calculate length and checksum. */
530 header->length = current - (unsigned long)dmar;
531 header->checksum = acpi_checksum((void *)dmar, header->length);
532}
533
534unsigned long acpi_create_dmar_drhd(unsigned long current, u8 flags,
Matt DeVillier7866d492018-03-29 14:59:57 +0200535 u16 segment, u64 bar)
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200536{
537 dmar_entry_t *drhd = (dmar_entry_t *)current;
538 memset(drhd, 0, sizeof(*drhd));
539 drhd->type = DMAR_DRHD;
540 drhd->length = sizeof(*drhd); /* will be fixed up later */
541 drhd->flags = flags;
542 drhd->segment = segment;
543 drhd->bar = bar;
544
545 return drhd->length;
546}
547
Matt DeVillier7866d492018-03-29 14:59:57 +0200548unsigned long acpi_create_dmar_rmrr(unsigned long current, u16 segment,
549 u64 bar, u64 limit)
550{
551 dmar_rmrr_entry_t *rmrr = (dmar_rmrr_entry_t *)current;
552 memset(rmrr, 0, sizeof(*rmrr));
553 rmrr->type = DMAR_RMRR;
554 rmrr->length = sizeof(*rmrr); /* will be fixed up later */
555 rmrr->segment = segment;
556 rmrr->bar = bar;
557 rmrr->limit = limit;
558
559 return rmrr->length;
560}
561
Werner Zehd4d76952016-07-27 06:56:36 +0200562unsigned long acpi_create_dmar_atsr(unsigned long current, u8 flags,
563 u16 segment)
564{
565 dmar_atsr_entry_t *atsr = (dmar_atsr_entry_t *)current;
566 memset(atsr, 0, sizeof(*atsr));
567 atsr->type = DMAR_ATSR;
568 atsr->length = sizeof(*atsr); /* will be fixed up later */
569 atsr->flags = flags;
570 atsr->segment = segment;
571
572 return atsr->length;
573}
574
John Zhao76e70672019-04-04 11:03:28 -0700575unsigned long acpi_create_dmar_rhsa(unsigned long current, u64 base_addr,
576 u32 proximity_domain)
577{
578 dmar_rhsa_entry_t *rhsa = (dmar_rhsa_entry_t *)current;
579 memset(rhsa, 0, sizeof(*rhsa));
580 rhsa->type = DMAR_RHSA;
581 rhsa->length = sizeof(*rhsa);
582 rhsa->base_address = base_addr;
583 rhsa->proximity_domain = proximity_domain;
584
585 return rhsa->length;
586}
587
588unsigned long acpi_create_dmar_andd(unsigned long current, u8 device_number,
589 const char *device_name)
590{
591 dmar_andd_entry_t *andd = (dmar_andd_entry_t *)current;
592 int andd_len = sizeof(dmar_andd_entry_t) + strlen(device_name) + 1;
593 memset(andd, 0, andd_len);
594 andd->type = DMAR_ANDD;
595 andd->length = andd_len;
596 andd->device_number = device_number;
597 memcpy(&andd->device_name, device_name, strlen(device_name));
598
599 return andd->length;
600}
601
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200602void acpi_dmar_drhd_fixup(unsigned long base, unsigned long current)
603{
604 dmar_entry_t *drhd = (dmar_entry_t *)base;
605 drhd->length = current - base;
606}
607
Matt DeVillier7866d492018-03-29 14:59:57 +0200608void acpi_dmar_rmrr_fixup(unsigned long base, unsigned long current)
609{
610 dmar_rmrr_entry_t *rmrr = (dmar_rmrr_entry_t *)base;
611 rmrr->length = current - base;
612}
613
Werner Zehd4d76952016-07-27 06:56:36 +0200614void acpi_dmar_atsr_fixup(unsigned long base, unsigned long current)
615{
616 dmar_atsr_entry_t *atsr = (dmar_atsr_entry_t *)base;
617 atsr->length = current - base;
618}
619
Matt DeVillier7866d492018-03-29 14:59:57 +0200620static unsigned long acpi_create_dmar_ds(unsigned long current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100621 enum dev_scope_type type, u8 enumeration_id, u8 bus, u8 dev, u8 fn)
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200622{
Nico Huber6c4751d2015-10-26 12:03:54 +0100623 /* we don't support longer paths yet */
624 const size_t dev_scope_length = sizeof(dev_scope_t) + 2;
625
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200626 dev_scope_t *ds = (dev_scope_t *)current;
Nico Huber6c4751d2015-10-26 12:03:54 +0100627 memset(ds, 0, dev_scope_length);
628 ds->type = type;
629 ds->length = dev_scope_length;
630 ds->enumeration = enumeration_id;
631 ds->start_bus = bus;
632 ds->path[0].dev = dev;
633 ds->path[0].fn = fn;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200634
635 return ds->length;
636}
637
Matt DeVillier7866d492018-03-29 14:59:57 +0200638unsigned long acpi_create_dmar_ds_pci_br(unsigned long current, u8 bus,
Werner Zeh21a5bff2016-07-27 07:07:20 +0200639 u8 dev, u8 fn)
640{
Matt DeVillier7866d492018-03-29 14:59:57 +0200641 return acpi_create_dmar_ds(current,
Werner Zeh21a5bff2016-07-27 07:07:20 +0200642 SCOPE_PCI_SUB, 0, bus, dev, fn);
643}
644
Matt DeVillier7866d492018-03-29 14:59:57 +0200645unsigned long acpi_create_dmar_ds_pci(unsigned long current, u8 bus,
Nico Huber6c4751d2015-10-26 12:03:54 +0100646 u8 dev, u8 fn)
647{
Matt DeVillier7866d492018-03-29 14:59:57 +0200648 return acpi_create_dmar_ds(current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100649 SCOPE_PCI_ENDPOINT, 0, bus, dev, fn);
650}
651
Matt DeVillier7866d492018-03-29 14:59:57 +0200652unsigned long acpi_create_dmar_ds_ioapic(unsigned long current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100653 u8 enumeration_id, u8 bus, u8 dev, u8 fn)
654{
Matt DeVillier7866d492018-03-29 14:59:57 +0200655 return acpi_create_dmar_ds(current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100656 SCOPE_IOAPIC, enumeration_id, bus, dev, fn);
657}
658
Matt DeVillier7866d492018-03-29 14:59:57 +0200659unsigned long acpi_create_dmar_ds_msi_hpet(unsigned long current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100660 u8 enumeration_id, u8 bus, u8 dev, u8 fn)
661{
Matt DeVillier7866d492018-03-29 14:59:57 +0200662 return acpi_create_dmar_ds(current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100663 SCOPE_MSI_HPET, enumeration_id, bus, dev, fn);
664}
665
Uwe Hermann622824c2010-11-19 15:14:42 +0000666/* http://h21007.www2.hp.com/portal/download/files/unprot/Itanium/slit.pdf */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +0200667void acpi_create_slit(acpi_slit_t *slit,
668 unsigned long (*acpi_fill_slit)(unsigned long current))
Yinghai Lud4b278c2006-10-04 20:46:15 +0000669{
Uwe Hermann622824c2010-11-19 15:14:42 +0000670 acpi_header_t *header = &(slit->header);
671 unsigned long current = (unsigned long)slit + sizeof(acpi_slit_t);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000672
Uwe Hermann622824c2010-11-19 15:14:42 +0000673 memset((void *)slit, 0, sizeof(acpi_slit_t));
Yinghai Lud4b278c2006-10-04 20:46:15 +0000674
John Zhao2ba303e2019-05-28 16:48:14 -0700675 if (!header)
676 return;
677
Uwe Hermann622824c2010-11-19 15:14:42 +0000678 /* Fill out header fields. */
679 memcpy(header->signature, "SLIT", 4);
680 memcpy(header->oem_id, OEM_ID, 6);
681 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
682 memcpy(header->asl_compiler_id, ASLC, 4);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000683
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100684 header->asl_compiler_revision = asl_revision;
Uwe Hermann622824c2010-11-19 15:14:42 +0000685 header->length = sizeof(acpi_slit_t);
Marc Jones93a51762018-08-22 18:57:24 -0600686 header->revision = get_acpi_table_revision(SLIT);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000687
Uwe Hermann622824c2010-11-19 15:14:42 +0000688 current = acpi_fill_slit(current);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000689
Uwe Hermann622824c2010-11-19 15:14:42 +0000690 /* (Re)calculate length and checksum. */
691 header->length = current - (unsigned long)slit;
692 header->checksum = acpi_checksum((void *)slit, header->length);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000693}
694
Uwe Hermann622824c2010-11-19 15:14:42 +0000695/* http://www.intel.com/hardwaredesign/hpetspec_1.pdf */
Stefan Reinauer777224c2005-01-19 14:06:41 +0000696void acpi_create_hpet(acpi_hpet_t *hpet)
Stefan Reinauer688b3852004-01-28 16:56:14 +0000697{
Uwe Hermann622824c2010-11-19 15:14:42 +0000698 acpi_header_t *header = &(hpet->header);
699 acpi_addr_t *addr = &(hpet->addr);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000700
Stefan Reinauera7648c22004-01-29 17:31:34 +0000701 memset((void *)hpet, 0, sizeof(acpi_hpet_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000702
John Zhao2ba303e2019-05-28 16:48:14 -0700703 if (!header)
704 return;
705
Uwe Hermann622824c2010-11-19 15:14:42 +0000706 /* Fill out header fields. */
Stefan Reinauer4704dc52009-07-22 01:11:37 +0000707 memcpy(header->signature, "HPET", 4);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000708 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000709 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000710 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000711
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100712 header->asl_compiler_revision = asl_revision;
Stefan Reinauer688b3852004-01-28 16:56:14 +0000713 header->length = sizeof(acpi_hpet_t);
Marc Jones93a51762018-08-22 18:57:24 -0600714 header->revision = get_acpi_table_revision(HPET);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000715
Uwe Hermann622824c2010-11-19 15:14:42 +0000716 /* Fill out HPET address. */
717 addr->space_id = 0; /* Memory */
718 addr->bit_width = 64;
719 addr->bit_offset = 0;
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200720 addr->addrl = CONFIG_HPET_ADDRESS & 0xffffffff;
721 addr->addrh = ((unsigned long long)CONFIG_HPET_ADDRESS) >> 32;
Stefan Reinauer688b3852004-01-28 16:56:14 +0000722
Lee Leahy024b13d2017-03-16 13:41:11 -0700723 hpet->id = *(unsigned int *)CONFIG_HPET_ADDRESS;
Uwe Hermann622824c2010-11-19 15:14:42 +0000724 hpet->number = 0;
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200725 hpet->min_tick = CONFIG_HPET_MIN_TICKS;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000726
Uwe Hermann622824c2010-11-19 15:14:42 +0000727 header->checksum = acpi_checksum((void *)hpet, sizeof(acpi_hpet_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000728}
Uwe Hermann622824c2010-11-19 15:14:42 +0000729
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200730void acpi_create_vfct(struct device *device,
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530731 acpi_vfct_t *vfct,
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700732 unsigned long (*acpi_fill_vfct)(struct device *device,
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530733 acpi_vfct_t *vfct_struct, unsigned long current))
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200734{
735 acpi_header_t *header = &(vfct->header);
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530736 unsigned long current = (unsigned long)vfct + sizeof(acpi_vfct_t);
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200737
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530738 memset((void *)vfct, 0, sizeof(acpi_vfct_t));
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200739
John Zhao2ba303e2019-05-28 16:48:14 -0700740 if (!header)
741 return;
742
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200743 /* Fill out header fields. */
744 memcpy(header->signature, "VFCT", 4);
745 memcpy(header->oem_id, OEM_ID, 6);
746 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
747 memcpy(header->asl_compiler_id, ASLC, 4);
748
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100749 header->asl_compiler_revision = asl_revision;
Marc Jones93a51762018-08-22 18:57:24 -0600750 header->revision = get_acpi_table_revision(VFCT);
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200751
752 current = acpi_fill_vfct(device, vfct, current);
753
Kyösti Mälkkifb493792019-06-30 06:29:52 +0300754 /* If no BIOS image, return with header->length == 0. */
755 if (!vfct->VBIOSImageOffset)
756 return;
757
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200758 /* (Re)calculate length and checksum. */
759 header->length = current - (unsigned long)vfct;
760 header->checksum = acpi_checksum((void *)vfct, header->length);
761}
762
Patrick Rudolph9d98e5a2019-06-14 19:00:04 +0200763void acpi_create_ipmi(struct device *device,
764 struct acpi_spmi *spmi,
765 const u16 ipmi_revision,
766 const acpi_addr_t *addr,
767 const enum acpi_ipmi_interface_type type,
768 const s8 gpe_interrupt,
769 const u32 apic_interrupt,
770 const u32 uid)
771{
772 acpi_header_t *header = &(spmi->header);
773 memset((void *)spmi, 0, sizeof(struct acpi_spmi));
774
775 /* Fill out header fields. */
776 memcpy(header->signature, "SPMI", 4);
777 memcpy(header->oem_id, OEM_ID, 6);
778 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
779 memcpy(header->asl_compiler_id, ASLC, 4);
780
781 header->asl_compiler_revision = asl_revision;
782 header->length = sizeof(struct acpi_spmi);
783 header->revision = get_acpi_table_revision(SPMI);
784
785 spmi->reserved = 1;
786
787 if (device->path.type == DEVICE_PATH_PCI) {
788 spmi->pci_device_flag = ACPI_IPMI_PCI_DEVICE_FLAG;
789 spmi->pci_bus = device->bus->secondary;
790 spmi->pci_device = device->path.pci.devfn >> 3;
791 spmi->pci_function = device->path.pci.devfn & 0x7;
792 } else if (type != IPMI_INTERFACE_SSIF) {
793 memcpy(spmi->uid, &uid, sizeof(spmi->uid));
794 }
795
796 spmi->base_address = *addr;
797 spmi->specification_revision = ipmi_revision;
798
799 spmi->interface_type = type;
800
801 if (gpe_interrupt >= 0 && gpe_interrupt < 32) {
802 spmi->gpe = gpe_interrupt;
803 spmi->interrupt_type |= ACPI_IPMI_INT_TYPE_SCI;
804 }
805 if (apic_interrupt > 0) {
806 spmi->global_system_interrupt = apic_interrupt;
807 spmi->interrupt_type |= ACPI_IPMI_INT_TYPE_APIC;
808 }
809
810 /* Calculate checksum. */
811 header->checksum = acpi_checksum((void *)spmi, header->length);
812}
813
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500814void acpi_create_ivrs(acpi_ivrs_t *ivrs,
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700815 unsigned long (*acpi_fill_ivrs)(acpi_ivrs_t *ivrs_struct,
816 unsigned long current))
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500817{
818 acpi_header_t *header = &(ivrs->header);
819 unsigned long current = (unsigned long)ivrs + sizeof(acpi_ivrs_t);
820
821 memset((void *)ivrs, 0, sizeof(acpi_ivrs_t));
822
John Zhao2ba303e2019-05-28 16:48:14 -0700823 if (!header)
824 return;
825
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500826 /* Fill out header fields. */
827 memcpy(header->signature, "IVRS", 4);
828 memcpy(header->oem_id, OEM_ID, 6);
829 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
830 memcpy(header->asl_compiler_id, ASLC, 4);
831
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100832 header->asl_compiler_revision = asl_revision;
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500833 header->length = sizeof(acpi_ivrs_t);
Marc Jones93a51762018-08-22 18:57:24 -0600834 header->revision = get_acpi_table_revision(IVRS);
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500835
836 current = acpi_fill_ivrs(ivrs, current);
837
838 /* (Re)calculate length and checksum. */
839 header->length = current - (unsigned long)ivrs;
840 header->checksum = acpi_checksum((void *)ivrs, header->length);
841}
842
Elyes HAOUAS9dd89cd2018-05-04 17:56:47 +0200843unsigned long acpi_write_hpet(struct device *device, unsigned long current,
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700844 acpi_rsdp_t *rsdp)
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200845{
846 acpi_hpet_t *hpet;
847
848 /*
849 * We explicitly add these tables later on:
850 */
851 printk(BIOS_DEBUG, "ACPI: * HPET\n");
852
853 hpet = (acpi_hpet_t *) current;
854 current += sizeof(acpi_hpet_t);
Felix Heldc4697122019-06-20 13:50:17 +0200855 current = ALIGN_UP(current, 16);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200856 acpi_create_hpet(hpet);
857 acpi_add_table(rsdp, hpet);
858
859 return current;
860}
861
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800862void acpi_create_dbg2(acpi_dbg2_header_t *dbg2,
863 int port_type, int port_subtype,
864 acpi_addr_t *address, uint32_t address_size,
865 const char *device_path)
866{
867 uintptr_t current;
868 acpi_dbg2_device_t *device;
869 uint32_t *dbg2_addr_size;
870 acpi_header_t *header;
871 size_t path_len;
872 const char *path;
873 char *namespace;
874
875 /* Fill out header fields. */
876 current = (uintptr_t)dbg2;
877 memset(dbg2, 0, sizeof(acpi_dbg2_header_t));
878 header = &(dbg2->header);
John Zhao2ba303e2019-05-28 16:48:14 -0700879
880 if (!header)
881 return;
882
Marc Jones93a51762018-08-22 18:57:24 -0600883 header->revision = get_acpi_table_revision(DBG2);
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800884 memcpy(header->signature, "DBG2", 4);
885 memcpy(header->oem_id, OEM_ID, 6);
886 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
887 memcpy(header->asl_compiler_id, ASLC, 4);
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100888 header->asl_compiler_revision = asl_revision;
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800889
890 /* One debug device defined */
891 dbg2->devices_offset = sizeof(acpi_dbg2_header_t);
892 dbg2->devices_count = 1;
893 current += sizeof(acpi_dbg2_header_t);
894
895 /* Device comes after the header */
896 device = (acpi_dbg2_device_t *)current;
897 memset(device, 0, sizeof(acpi_dbg2_device_t));
898 current += sizeof(acpi_dbg2_device_t);
899
900 device->revision = 0;
901 device->address_count = 1;
902 device->port_type = port_type;
903 device->port_subtype = port_subtype;
904
905 /* Base Address comes after device structure */
906 memcpy((void *)current, address, sizeof(acpi_addr_t));
907 device->base_address_offset = current - (uintptr_t)device;
908 current += sizeof(acpi_addr_t);
909
910 /* Address Size comes after address structure */
911 dbg2_addr_size = (uint32_t *)current;
912 device->address_size_offset = current - (uintptr_t)device;
913 *dbg2_addr_size = address_size;
914 current += sizeof(uint32_t);
915
916 /* Namespace string comes last, use '.' if not provided */
917 path = device_path ? : ".";
918 /* Namespace string length includes NULL terminator */
919 path_len = strlen(path) + 1;
920 namespace = (char *)current;
921 device->namespace_string_length = path_len;
922 device->namespace_string_offset = current - (uintptr_t)device;
923 strncpy(namespace, path, path_len);
924 current += path_len;
925
926 /* Update structure lengths and checksum */
927 device->length = current - (uintptr_t)device;
928 header->length = current - (uintptr_t)dbg2;
929 header->checksum = acpi_checksum((uint8_t *)dbg2, header->length);
930}
931
932unsigned long acpi_write_dbg2_pci_uart(acpi_rsdp_t *rsdp, unsigned long current,
Aamir Bohrae825d3f2019-07-30 10:11:41 +0530933 const struct device *dev, uint8_t access_size)
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800934{
935 acpi_dbg2_header_t *dbg2 = (acpi_dbg2_header_t *)current;
936 struct resource *res;
937 acpi_addr_t address;
938
939 if (!dev) {
Wim Vervoorn6cd52432020-02-03 14:41:46 +0100940 printk(BIOS_DEBUG, "%s: Device not found\n", __func__);
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800941 return current;
942 }
Mario Scheithauerf1eb0ea2017-11-21 13:52:53 +0100943 if (!dev->enabled) {
944 printk(BIOS_INFO, "%s: Device not enabled\n", __func__);
945 return current;
946 }
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800947 res = find_resource(dev, PCI_BASE_ADDRESS_0);
948 if (!res) {
949 printk(BIOS_ERR, "%s: Unable to find resource for %s\n",
950 __func__, dev_path(dev));
951 return current;
952 }
953
954 memset(&address, 0, sizeof(address));
955 if (res->flags & IORESOURCE_IO)
956 address.space_id = ACPI_ADDRESS_SPACE_IO;
957 else if (res->flags & IORESOURCE_MEM)
958 address.space_id = ACPI_ADDRESS_SPACE_MEMORY;
959 else {
960 printk(BIOS_ERR, "%s: Unknown address space type\n", __func__);
961 return current;
962 }
963
964 address.addrl = (uint32_t)res->base;
965 address.addrh = (uint32_t)((res->base >> 32) & 0xffffffff);
966 address.access_size = access_size;
967
968 acpi_create_dbg2(dbg2,
969 ACPI_DBG2_PORT_SERIAL,
970 ACPI_DBG2_PORT_SERIAL_16550,
971 &address, res->size,
972 acpi_device_path(dev));
973
974 if (dbg2->header.length) {
975 current += dbg2->header.length;
976 current = acpi_align_current(current);
977 acpi_add_table(rsdp, dbg2);
978 }
979
980 return current;
981}
982
Stefan Reinauer777224c2005-01-19 14:06:41 +0000983void acpi_create_facs(acpi_facs_t *facs)
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000984{
Uwe Hermann622824c2010-11-19 15:14:42 +0000985 memset((void *)facs, 0, sizeof(acpi_facs_t));
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000986
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000987 memcpy(facs->signature, "FACS", 4);
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000988 facs->length = sizeof(acpi_facs_t);
989 facs->hardware_signature = 0;
990 facs->firmware_waking_vector = 0;
991 facs->global_lock = 0;
992 facs->flags = 0;
993 facs->x_firmware_waking_vector_l = 0;
994 facs->x_firmware_waking_vector_h = 0;
Marc Jones93a51762018-08-22 18:57:24 -0600995 facs->version = get_acpi_table_revision(FACS);
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000996}
Stefan Reinauer777224c2005-01-19 14:06:41 +0000997
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100998static void acpi_write_rsdt(acpi_rsdt_t *rsdt, char *oem_id, char *oem_table_id)
Stefan Reinauer14e22772010-04-27 06:56:47 +0000999{
Uwe Hermann622824c2010-11-19 15:14:42 +00001000 acpi_header_t *header = &(rsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001001
John Zhao2ba303e2019-05-28 16:48:14 -07001002 if (!header)
1003 return;
1004
Uwe Hermann622824c2010-11-19 15:14:42 +00001005 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001006 memcpy(header->signature, "RSDT", 4);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001007 memcpy(header->oem_id, oem_id, 6);
1008 memcpy(header->oem_table_id, oem_table_id, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +00001009 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001010
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001011 header->asl_compiler_revision = asl_revision;
Stefan Reinauer688b3852004-01-28 16:56:14 +00001012 header->length = sizeof(acpi_rsdt_t);
Marc Jones93a51762018-08-22 18:57:24 -06001013 header->revision = get_acpi_table_revision(RSDT);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001014
Uwe Hermann622824c2010-11-19 15:14:42 +00001015 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauer688b3852004-01-28 16:56:14 +00001016
Uwe Hermann622824c2010-11-19 15:14:42 +00001017 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001018 header->checksum = acpi_checksum((void *)rsdt, sizeof(acpi_rsdt_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +00001019}
1020
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001021static void acpi_write_xsdt(acpi_xsdt_t *xsdt, char *oem_id, char *oem_table_id)
Stefan Reinauer14e22772010-04-27 06:56:47 +00001022{
Uwe Hermann622824c2010-11-19 15:14:42 +00001023 acpi_header_t *header = &(xsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001024
John Zhao2ba303e2019-05-28 16:48:14 -07001025 if (!header)
1026 return;
1027
Uwe Hermann622824c2010-11-19 15:14:42 +00001028 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001029 memcpy(header->signature, "XSDT", 4);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001030 memcpy(header->oem_id, oem_id, 6);
1031 memcpy(header->oem_table_id, oem_table_id, 8);
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001032 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001033
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001034 header->asl_compiler_revision = asl_revision;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001035 header->length = sizeof(acpi_xsdt_t);
Marc Jones93a51762018-08-22 18:57:24 -06001036 header->revision = get_acpi_table_revision(XSDT);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001037
Uwe Hermann622824c2010-11-19 15:14:42 +00001038 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001039
Uwe Hermann622824c2010-11-19 15:14:42 +00001040 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001041 header->checksum = acpi_checksum((void *)xsdt, sizeof(acpi_xsdt_t));
1042}
1043
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001044static void acpi_write_rsdp(acpi_rsdp_t *rsdp, acpi_rsdt_t *rsdt,
1045 acpi_xsdt_t *xsdt, char *oem_id)
Stefan Reinauer688b3852004-01-28 16:56:14 +00001046{
Stefan Reinauerd18faac2009-11-05 18:06:43 +00001047 memset(rsdp, 0, sizeof(acpi_rsdp_t));
Uwe Hermann622824c2010-11-19 15:14:42 +00001048
Stefan Reinauer688b3852004-01-28 16:56:14 +00001049 memcpy(rsdp->signature, RSDP_SIG, 8);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001050 memcpy(rsdp->oem_id, oem_id, 6);
Uwe Hermann622824c2010-11-19 15:14:42 +00001051
1052 rsdp->length = sizeof(acpi_rsdp_t);
Stefan Reinauerdefee172015-06-18 01:11:19 -07001053 rsdp->rsdt_address = (uintptr_t)rsdt;
Uwe Hermann622824c2010-11-19 15:14:42 +00001054
1055 /*
1056 * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2.
1057 *
1058 * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
1059 * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
1060 * revision 0).
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001061 */
1062 if (xsdt == NULL) {
Uwe Hermann622824c2010-11-19 15:14:42 +00001063 rsdp->revision = 0;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001064 } else {
Stefan Reinauerdefee172015-06-18 01:11:19 -07001065 rsdp->xsdt_address = (u64)(uintptr_t)xsdt;
Marc Jones93a51762018-08-22 18:57:24 -06001066 rsdp->revision = get_acpi_table_revision(RSDP);
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001067 }
Uwe Hermann622824c2010-11-19 15:14:42 +00001068
1069 /* Calculate checksums. */
1070 rsdp->checksum = acpi_checksum((void *)rsdp, 20);
1071 rsdp->ext_checksum = acpi_checksum((void *)rsdp, sizeof(acpi_rsdp_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +00001072}
1073
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001074unsigned long acpi_create_hest_error_source(acpi_hest_t *hest,
1075 acpi_hest_esd_t *esd, u16 type, void *data, u16 data_len)
zbaocaf494c82012-04-13 13:57:14 +08001076{
1077 acpi_header_t *header = &(hest->header);
1078 acpi_hest_hen_t *hen;
1079 void *pos;
1080 u16 len;
1081
1082 pos = esd;
1083 memset(pos, 0, sizeof(acpi_hest_esd_t));
1084 len = 0;
1085 esd->type = type; /* MCE */
1086 esd->source_id = hest->error_source_count;
1087 esd->flags = 0; /* FIRMWARE_FIRST */
1088 esd->enabled = 1;
1089 esd->prealloc_erecords = 1;
1090 esd->max_section_per_record = 0x1;
1091
1092 len += sizeof(acpi_hest_esd_t);
1093 pos = esd + 1;
1094
1095 switch (type) {
1096 case 0: /* MCE */
1097 break;
1098 case 1: /* CMC */
1099 hen = (acpi_hest_hen_t *) (pos);
1100 memset(pos, 0, sizeof(acpi_hest_hen_t));
1101 hen->type = 3; /* SCI? */
1102 hen->length = sizeof(acpi_hest_hen_t);
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001103 hen->conf_we = 0; /* Configuration Write Enable. */
zbaocaf494c82012-04-13 13:57:14 +08001104 hen->poll_interval = 0;
1105 hen->vector = 0;
1106 hen->sw2poll_threshold_val = 0;
1107 hen->sw2poll_threshold_win = 0;
1108 hen->error_threshold_val = 0;
1109 hen->error_threshold_win = 0;
1110 len += sizeof(acpi_hest_hen_t);
1111 pos = hen + 1;
1112 break;
1113 case 2: /* NMI */
1114 case 6: /* AER Root Port */
1115 case 7: /* AER Endpoint */
1116 case 8: /* AER Bridge */
1117 case 9: /* Generic Hardware Error Source. */
1118 /* TODO: */
1119 break;
1120 default:
1121 printk(BIOS_DEBUG, "Invalid type of Error Source.");
1122 break;
1123 }
Lee Leahy024b13d2017-03-16 13:41:11 -07001124 hest->error_source_count++;
zbaocaf494c82012-04-13 13:57:14 +08001125
1126 memcpy(pos, data, data_len);
1127 len += data_len;
John Zhao2ba303e2019-05-28 16:48:14 -07001128 if (header)
1129 header->length += len;
zbaocaf494c82012-04-13 13:57:14 +08001130
1131 return len;
1132}
1133
1134/* ACPI 4.0 */
Vladimir Serbinenko807127f2014-11-09 13:36:18 +01001135void acpi_write_hest(acpi_hest_t *hest,
1136 unsigned long (*acpi_fill_hest)(acpi_hest_t *hest))
zbaocaf494c82012-04-13 13:57:14 +08001137{
1138 acpi_header_t *header = &(hest->header);
1139
1140 memset(hest, 0, sizeof(acpi_hest_t));
1141
John Zhao2ba303e2019-05-28 16:48:14 -07001142 if (!header)
1143 return;
1144
zbaocaf494c82012-04-13 13:57:14 +08001145 memcpy(header->signature, "HEST", 4);
1146 memcpy(header->oem_id, OEM_ID, 6);
1147 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1148 memcpy(header->asl_compiler_id, ASLC, 4);
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001149 header->asl_compiler_revision = asl_revision;
zbaocaf494c82012-04-13 13:57:14 +08001150 header->length += sizeof(acpi_hest_t);
Marc Jones93a51762018-08-22 18:57:24 -06001151 header->revision = get_acpi_table_revision(HEST);
zbaocaf494c82012-04-13 13:57:14 +08001152
1153 acpi_fill_hest(hest);
1154
1155 /* Calculate checksums. */
1156 header->checksum = acpi_checksum((void *)hest, header->length);
1157}
1158
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001159/* ACPI 3.0b */
1160void acpi_write_bert(acpi_bert_t *bert, uintptr_t region, size_t length)
1161{
1162 acpi_header_t *header = &(bert->header);
1163
1164 memset(bert, 0, sizeof(acpi_bert_t));
1165
John Zhao2ba303e2019-05-28 16:48:14 -07001166 if (!header)
1167 return;
1168
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001169 memcpy(header->signature, "BERT", 4);
1170 memcpy(header->oem_id, OEM_ID, 6);
1171 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1172 memcpy(header->asl_compiler_id, ASLC, 4);
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001173 header->asl_compiler_revision = asl_revision;
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001174 header->length += sizeof(acpi_bert_t);
1175 header->revision = get_acpi_table_revision(BERT);
1176
1177 bert->error_region = region;
1178 bert->region_length = length;
1179
1180 /* Calculate checksums. */
1181 header->checksum = acpi_checksum((void *)bert, header->length);
1182}
1183
Julius Wernercd49cce2019-03-05 16:53:33 -08001184#if CONFIG(COMMON_FADT)
Lee Leahy024b13d2017-03-16 13:41:11 -07001185void acpi_create_fadt(acpi_fadt_t *fadt, acpi_facs_t *facs, void *dsdt)
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001186{
1187 acpi_header_t *header = &(fadt->header);
1188
1189 memset((void *) fadt, 0, sizeof(acpi_fadt_t));
John Zhao2ba303e2019-05-28 16:48:14 -07001190
1191 if (!header)
1192 return;
1193
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001194 memcpy(header->signature, "FACP", 4);
1195 header->length = sizeof(acpi_fadt_t);
Marc Jones93a51762018-08-22 18:57:24 -06001196 header->revision = get_acpi_table_revision(FADT);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001197 memcpy(header->oem_id, OEM_ID, 6);
1198 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1199 memcpy(header->asl_compiler_id, ASLC, 4);
Elyes HAOUAS26071aa2019-02-15 08:21:33 +01001200 header->asl_compiler_revision = asl_revision;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001201
1202 fadt->firmware_ctrl = (unsigned long) facs;
1203 fadt->dsdt = (unsigned long) dsdt;
1204
1205 fadt->x_firmware_ctl_l = (unsigned long)facs;
1206 fadt->x_firmware_ctl_h = 0;
1207 fadt->x_dsdt_l = (unsigned long)dsdt;
1208 fadt->x_dsdt_h = 0;
1209
Julius Wernercd49cce2019-03-05 16:53:33 -08001210 if (CONFIG(SYSTEM_TYPE_CONVERTIBLE) ||
1211 CONFIG(SYSTEM_TYPE_LAPTOP))
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001212 fadt->preferred_pm_profile = PM_MOBILE;
Julius Wernercd49cce2019-03-05 16:53:33 -08001213 else if (CONFIG(SYSTEM_TYPE_DETACHABLE) ||
1214 CONFIG(SYSTEM_TYPE_TABLET))
Duncan Lauriefa9c6f12019-02-07 11:30:06 -08001215 fadt->preferred_pm_profile = PM_TABLET;
Lee Leahy9c7c6f72017-03-16 11:24:09 -07001216 else
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001217 fadt->preferred_pm_profile = PM_DESKTOP;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001218
Vladimir Serbinenkoab83ef02014-10-25 15:18:25 +02001219 acpi_fill_fadt(fadt);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001220
1221 header->checksum =
1222 acpi_checksum((void *) fadt, header->length);
1223}
1224#endif
1225
Aaron Durbin64031672018-04-21 14:45:32 -06001226unsigned long __weak fw_cfg_acpi_tables(unsigned long start)
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001227{
1228 return 0;
1229}
1230
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001231unsigned long write_acpi_tables(unsigned long start)
1232{
1233 unsigned long current;
1234 acpi_rsdp_t *rsdp;
1235 acpi_rsdt_t *rsdt;
1236 acpi_xsdt_t *xsdt;
1237 acpi_fadt_t *fadt;
1238 acpi_facs_t *facs;
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001239 acpi_header_t *slic_file, *slic;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001240 acpi_header_t *ssdt;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001241 acpi_header_t *dsdt_file, *dsdt;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001242 acpi_mcfg_t *mcfg;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +02001243 acpi_tcpa_t *tcpa;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001244 acpi_tpm2_t *tpm2;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001245 acpi_madt_t *madt;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +11001246 struct device *dev;
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001247 unsigned long fw;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001248 size_t slic_size, dsdt_size;
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001249 char oem_id[6], oem_table_id[8];
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001250
1251 current = start;
1252
1253 /* Align ACPI tables to 16byte */
Aaron Durbin07a1b282015-12-10 17:07:38 -06001254 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001255
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001256 /* Special case for qemu */
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001257 fw = fw_cfg_acpi_tables(current);
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001258 if (fw) {
1259 rsdp = NULL;
1260 /* Find RSDP. */
1261 for (void *p = (void *)current; p < (void *)fw; p += 16) {
1262 if (valid_rsdp((acpi_rsdp_t *)p)) {
1263 rsdp = p;
1264 break;
1265 }
1266 }
1267 if (!rsdp)
1268 return fw;
1269
1270 /* Add BOOT0000 for Linux google firmware driver */
1271 printk(BIOS_DEBUG, "ACPI: * SSDT\n");
1272 ssdt = (acpi_header_t *)fw;
1273 current = (unsigned long)ssdt + sizeof(acpi_header_t);
1274
1275 memset((void *)ssdt, 0, sizeof(acpi_header_t));
1276
1277 memcpy(&ssdt->signature, "SSDT", 4);
1278 ssdt->revision = get_acpi_table_revision(SSDT);
1279 memcpy(&ssdt->oem_id, OEM_ID, 6);
1280 memcpy(&ssdt->oem_table_id, oem_table_id, 8);
1281 ssdt->oem_revision = 42;
1282 memcpy(&ssdt->asl_compiler_id, ASLC, 4);
1283 ssdt->asl_compiler_revision = asl_revision;
1284 ssdt->length = sizeof(acpi_header_t);
1285
1286 acpigen_set_current((char *) current);
1287
1288 /* Write object to declare coreboot tables */
1289 acpi_ssdt_write_cbtable();
1290
1291 /* (Re)calculate length and checksum. */
1292 ssdt->length = current - (unsigned long)ssdt;
1293 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
1294
1295 acpi_create_ssdt_generator(ssdt, ACPI_TABLE_CREATOR);
1296
1297 acpi_add_table(rsdp, ssdt);
1298
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001299 return fw;
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001300 }
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001301
Vladimir Serbinenkoa4cf83d2015-06-02 21:40:29 +02001302 dsdt_file = cbfs_boot_map_with_leak(
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001303 CONFIG_CBFS_PREFIX "/dsdt.aml",
1304 CBFS_TYPE_RAW, &dsdt_size);
1305 if (!dsdt_file) {
1306 printk(BIOS_ERR, "No DSDT file, skipping ACPI tables\n");
1307 return current;
1308 }
1309
1310 if (dsdt_file->length > dsdt_size
Elyes HAOUAS7d87e762016-10-03 22:11:07 +02001311 || dsdt_file->length < sizeof(acpi_header_t)
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001312 || memcmp(dsdt_file->signature, "DSDT", 4) != 0) {
1313 printk(BIOS_ERR, "Invalid DSDT file, skipping ACPI tables\n");
1314 return current;
1315 }
1316
Aaron Durbin899d13d2015-05-15 23:39:23 -05001317 slic_file = cbfs_boot_map_with_leak(CONFIG_CBFS_PREFIX "/slic",
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001318 CBFS_TYPE_RAW, &slic_size);
Vladimir Serbinenko2cb29782015-05-31 11:32:09 +02001319 if (slic_file
1320 && (slic_file->length > slic_size
Elyes HAOUAS7d87e762016-10-03 22:11:07 +02001321 || slic_file->length < sizeof(acpi_header_t)
Vladimir Serbinenko2cb29782015-05-31 11:32:09 +02001322 || memcmp(slic_file->signature, "SLIC", 4) != 0)) {
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001323 slic_file = 0;
1324 }
1325
1326 if (slic_file) {
1327 memcpy(oem_id, slic_file->oem_id, 6);
1328 memcpy(oem_table_id, slic_file->oem_table_id, 8);
1329 } else {
1330 memcpy(oem_id, OEM_ID, 6);
1331 memcpy(oem_table_id, ACPI_TABLE_CREATOR, 8);
1332 }
1333
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001334 printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx.\n", start);
1335
1336 /* We need at least an RSDP and an RSDT Table */
1337 rsdp = (acpi_rsdp_t *) current;
1338 current += sizeof(acpi_rsdp_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001339 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001340 rsdt = (acpi_rsdt_t *) current;
1341 current += sizeof(acpi_rsdt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001342 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001343 xsdt = (acpi_xsdt_t *) current;
1344 current += sizeof(acpi_xsdt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001345 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001346
1347 /* clear all table memory */
1348 memset((void *) start, 0, current - start);
1349
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001350 acpi_write_rsdp(rsdp, rsdt, xsdt, oem_id);
1351 acpi_write_rsdt(rsdt, oem_id, oem_table_id);
1352 acpi_write_xsdt(xsdt, oem_id, oem_table_id);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001353
1354 printk(BIOS_DEBUG, "ACPI: * FACS\n");
Felix Heldc4697122019-06-20 13:50:17 +02001355 current = ALIGN_UP(current, 64);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001356 facs = (acpi_facs_t *) current;
1357 current += sizeof(acpi_facs_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001358 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001359 acpi_create_facs(facs);
1360
1361 printk(BIOS_DEBUG, "ACPI: * DSDT\n");
1362 dsdt = (acpi_header_t *) current;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001363 memcpy(dsdt, dsdt_file, sizeof(acpi_header_t));
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001364 if (dsdt->length >= sizeof(acpi_header_t)) {
1365 current += sizeof(acpi_header_t);
1366
1367 acpigen_set_current((char *) current);
1368 for (dev = all_devices; dev; dev = dev->next)
Nico Huber68680dd2020-03-31 17:34:52 +02001369 if (dev->ops && dev->ops->acpi_inject_dsdt)
1370 dev->ops->acpi_inject_dsdt(dev);
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001371 current = (unsigned long) acpigen_get_current();
1372 memcpy((char *)current,
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001373 (char *)dsdt_file + sizeof(acpi_header_t),
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001374 dsdt->length - sizeof(acpi_header_t));
1375 current += dsdt->length - sizeof(acpi_header_t);
1376
1377 /* (Re)calculate length and checksum. */
1378 dsdt->length = current - (unsigned long)dsdt;
1379 dsdt->checksum = 0;
1380 dsdt->checksum = acpi_checksum((void *)dsdt, dsdt->length);
1381 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001382
Aaron Durbin07a1b282015-12-10 17:07:38 -06001383 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001384
1385 printk(BIOS_DEBUG, "ACPI: * FADT\n");
1386 fadt = (acpi_fadt_t *) current;
1387 current += sizeof(acpi_fadt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001388 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001389
1390 acpi_create_fadt(fadt, facs, dsdt);
1391 acpi_add_table(rsdp, fadt);
1392
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001393 if (slic_file) {
1394 printk(BIOS_DEBUG, "ACPI: * SLIC\n");
1395 slic = (acpi_header_t *)current;
1396 memcpy(slic, slic_file, slic_file->length);
1397 current += slic_file->length;
Aaron Durbin07a1b282015-12-10 17:07:38 -06001398 current = acpi_align_current(current);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001399 acpi_add_table(rsdp, slic);
1400 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001401
1402 printk(BIOS_DEBUG, "ACPI: * SSDT\n");
1403 ssdt = (acpi_header_t *)current;
1404 acpi_create_ssdt_generator(ssdt, ACPI_TABLE_CREATOR);
Vladimir Serbinenko9310df82014-10-10 20:40:41 +02001405 if (ssdt->length > sizeof(acpi_header_t)) {
1406 current += ssdt->length;
1407 acpi_add_table(rsdp, ssdt);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001408 current = acpi_align_current(current);
Vladimir Serbinenko9310df82014-10-10 20:40:41 +02001409 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001410
1411 printk(BIOS_DEBUG, "ACPI: * MCFG\n");
1412 mcfg = (acpi_mcfg_t *) current;
1413 acpi_create_mcfg(mcfg);
1414 if (mcfg->header.length > sizeof(acpi_mcfg_t)) {
1415 current += mcfg->header.length;
Aaron Durbin07a1b282015-12-10 17:07:38 -06001416 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001417 acpi_add_table(rsdp, mcfg);
1418 }
1419
Julius Wernercd49cce2019-03-05 16:53:33 -08001420 if (CONFIG(TPM1)) {
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001421 printk(BIOS_DEBUG, "ACPI: * TCPA\n");
1422 tcpa = (acpi_tcpa_t *) current;
1423 acpi_create_tcpa(tcpa);
1424 if (tcpa->header.length >= sizeof(acpi_tcpa_t)) {
1425 current += tcpa->header.length;
1426 current = acpi_align_current(current);
1427 acpi_add_table(rsdp, tcpa);
1428 }
1429 }
1430
Julius Wernercd49cce2019-03-05 16:53:33 -08001431 if (CONFIG(TPM2)) {
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001432 printk(BIOS_DEBUG, "ACPI: * TPM2\n");
1433 tpm2 = (acpi_tpm2_t *) current;
1434 acpi_create_tpm2(tpm2);
1435 if (tpm2->header.length >= sizeof(acpi_tpm2_t)) {
1436 current += tpm2->header.length;
1437 current = acpi_align_current(current);
1438 acpi_add_table(rsdp, tpm2);
1439 }
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +02001440 }
1441
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001442 printk(BIOS_DEBUG, "ACPI: * MADT\n");
1443
1444 madt = (acpi_madt_t *) current;
1445 acpi_create_madt(madt);
1446 if (madt->header.length > sizeof(acpi_madt_t)) {
Lee Leahy024b13d2017-03-16 13:41:11 -07001447 current += madt->header.length;
1448 acpi_add_table(rsdp, madt);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001449 }
Aaron Durbin07a1b282015-12-10 17:07:38 -06001450 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001451
1452 printk(BIOS_DEBUG, "current = %lx\n", current);
1453
1454 for (dev = all_devices; dev; dev = dev->next) {
1455 if (dev->ops && dev->ops->write_acpi_tables) {
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001456 current = dev->ops->write_acpi_tables(dev, current,
1457 rsdp);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001458 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001459 }
1460 }
1461
1462 printk(BIOS_INFO, "ACPI: done.\n");
1463 return current;
1464}
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001465
Rudolf Marek33cafe52009-04-13 18:07:02 +00001466static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp)
1467{
1468 if (strncmp((char *)rsdp, RSDP_SIG, sizeof(RSDP_SIG) - 1) != 0)
1469 return NULL;
1470
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001471 printk(BIOS_DEBUG, "Looking on %p for valid checksum\n", rsdp);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001472
1473 if (acpi_checksum((void *)rsdp, 20) != 0)
1474 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001475 printk(BIOS_DEBUG, "Checksum 1 passed\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001476
1477 if ((rsdp->revision > 1) && (acpi_checksum((void *)rsdp,
1478 rsdp->length) != 0))
1479 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001480 printk(BIOS_DEBUG, "Checksum 2 passed all OK\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001481
1482 return rsdp;
1483}
1484
Rudolf Marek33cafe52009-04-13 18:07:02 +00001485void *acpi_find_wakeup_vector(void)
1486{
1487 char *p, *end;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001488 acpi_rsdt_t *rsdt;
1489 acpi_facs_t *facs;
Martin Rothc5f49262012-12-14 19:17:55 -07001490 acpi_fadt_t *fadt = NULL;
Kyösti Mälkki072d436b2016-06-17 08:44:40 +03001491 acpi_rsdp_t *rsdp = NULL;
Uwe Hermann622824c2010-11-19 15:14:42 +00001492 void *wake_vec;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001493 int i;
1494
Rudolf Marek33cafe52009-04-13 18:07:02 +00001495 if (!acpi_is_wakeup())
1496 return NULL;
1497
Uwe Hermann622824c2010-11-19 15:14:42 +00001498 printk(BIOS_DEBUG, "Trying to find the wakeup vector...\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001499
Uwe Hermann622824c2010-11-19 15:14:42 +00001500 /* Find RSDP. */
1501 for (p = (char *)0xe0000; p < (char *)0xfffff; p += 16) {
Lee Leahy0b5678f2017-03-16 16:01:40 -07001502 rsdp = valid_rsdp((acpi_rsdp_t *)p);
1503 if (rsdp)
Rudolf Marek33cafe52009-04-13 18:07:02 +00001504 break;
1505 }
1506
Angel Pons98a68ad2018-11-04 12:25:25 +01001507 if (rsdp == NULL) {
1508 printk(BIOS_ALERT,
1509 "No RSDP found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001510 return NULL;
Angel Pons98a68ad2018-11-04 12:25:25 +01001511 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001512
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001513 printk(BIOS_DEBUG, "RSDP found at %p\n", rsdp);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001514 rsdt = (acpi_rsdt_t *)(uintptr_t)rsdp->rsdt_address;
Stefan Reinauer14e22772010-04-27 06:56:47 +00001515
Uwe Hermann622824c2010-11-19 15:14:42 +00001516 end = (char *)rsdt + rsdt->header.length;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001517 printk(BIOS_DEBUG, "RSDT found at %p ends at %p\n", rsdt, end);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001518
Uwe Hermann622824c2010-11-19 15:14:42 +00001519 for (i = 0; ((char *)&rsdt->entry[i]) < end; i++) {
Stefan Reinauer71a30182015-07-30 16:28:13 -07001520 fadt = (acpi_fadt_t *)(uintptr_t)rsdt->entry[i];
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001521 if (strncmp((char *)fadt, "FACP", 4) == 0)
Rudolf Marek33cafe52009-04-13 18:07:02 +00001522 break;
1523 fadt = NULL;
1524 }
1525
Angel Pons98a68ad2018-11-04 12:25:25 +01001526 if (fadt == NULL) {
1527 printk(BIOS_ALERT,
1528 "No FADT found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001529 return NULL;
Angel Pons98a68ad2018-11-04 12:25:25 +01001530 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001531
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001532 printk(BIOS_DEBUG, "FADT found at %p\n", fadt);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001533 facs = (acpi_facs_t *)(uintptr_t)fadt->firmware_ctrl;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001534
Stefan Reinauer7e9771c2009-04-22 08:17:38 +00001535 if (facs == NULL) {
Angel Pons98a68ad2018-11-04 12:25:25 +01001536 printk(BIOS_ALERT,
1537 "No FACS found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001538 return NULL;
Stefan Reinauer7e9771c2009-04-22 08:17:38 +00001539 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001540
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001541 printk(BIOS_DEBUG, "FACS found at %p\n", facs);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001542 wake_vec = (void *)(uintptr_t)facs->firmware_waking_vector;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001543 printk(BIOS_DEBUG, "OS waking vector is %p\n", wake_vec);
Uwe Hermann622824c2010-11-19 15:14:42 +00001544
Rudolf Marek33cafe52009-04-13 18:07:02 +00001545 return wake_vec;
1546}
1547
Aaron Durbin64031672018-04-21 14:45:32 -06001548__weak int acpi_get_gpe(int gpe)
Duncan Laurie1f6e6812016-09-19 12:04:19 -07001549{
1550 return -1; /* implemented by SOC */
1551}
Marc Jones93a51762018-08-22 18:57:24 -06001552
1553int get_acpi_table_revision(enum acpi_tables table)
1554{
1555 switch (table) {
1556 case FADT:
Patrick Rudolphc02bda02020-02-28 10:19:41 +01001557 return ACPI_FADT_REV_ACPI_6_0;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001558 case MADT: /* ACPI 3.0: 2, ACPI 4.0/5.0: 3, ACPI 6.2b/6.3: 5 */
Marc Jones4ddd4762018-08-21 10:50:57 -06001559 return 2;
Marc Jones93a51762018-08-22 18:57:24 -06001560 case MCFG:
1561 return 1;
1562 case TCPA:
1563 return 2;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001564 case TPM2:
1565 return 4;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001566 case SSDT: /* ACPI 3.0 upto 6.3: 2 */
Marc Jones93a51762018-08-22 18:57:24 -06001567 return 2;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001568 case SRAT: /* ACPI 2.0: 1, ACPI 3.0: 2, ACPI 4.0 upto 6.3: 3 */
Marc Jones93a51762018-08-22 18:57:24 -06001569 return 1; /* TODO Should probably be upgraded to 2 */
1570 case DMAR:
1571 return 1;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001572 case SLIT: /* ACPI 2.0 upto 6.3: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06001573 return 1;
Patrick Rudolph9d98e5a2019-06-14 19:00:04 +02001574 case SPMI: /* IMPI 2.0 */
1575 return 5;
Marc Jones93a51762018-08-22 18:57:24 -06001576 case HPET: /* Currently 1. Table added in ACPI 2.0. */
1577 return 1;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001578 case VFCT: /* ACPI 2.0/3.0/4.0: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06001579 return 1;
1580 case IVRS:
1581 return IVRS_FORMAT_FIXED;
1582 case DBG2:
1583 return 0;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001584 case FACS: /* ACPI 2.0/3.0: 1, ACPI 4.0 upto 6.3: 2 */
Marc Jones93a51762018-08-22 18:57:24 -06001585 return 1;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001586 case RSDT: /* ACPI 1.0 upto 6.3: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06001587 return 1;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001588 case XSDT: /* ACPI 2.0 upto 6.3: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06001589 return 1;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001590 case RSDP: /* ACPI 2.0 upto 6.3: 2 */
Marc Jones93a51762018-08-22 18:57:24 -06001591 return 2;
1592 case HEST:
1593 return 1;
1594 case NHLT:
1595 return 5;
Marshall Dawson44705c62018-09-04 13:47:15 -06001596 case BERT:
1597 return 1;
Marc Jones93a51762018-08-22 18:57:24 -06001598 default:
1599 return -1;
1600 }
1601 return -1;
1602}