blob: 60f8c21335a2c8c8a21a5071f5fe169e6c10a828 [file] [log] [blame]
Patrick Georgi11f00792020-03-04 15:10:45 +01001/* SPDX-License-Identifier: GPL-2.0-only */
Stefan Reinauer688b3852004-01-28 16:56:14 +00002/*
Martin Roth20bbd812019-08-30 21:09:37 -06003 * coreboot ACPI Table support
Stefan Reinauer777224c2005-01-19 14:06:41 +00004 */
5
Stefan Reinauer14e22772010-04-27 06:56:47 +00006/*
Stefan Reinauer777224c2005-01-19 14:06:41 +00007 * Each system port implementing ACPI has to provide two functions:
Stefan Reinauer14e22772010-04-27 06:56:47 +00008 *
Stefan Reinauer777224c2005-01-19 14:06:41 +00009 * write_acpi_tables()
10 * acpi_dump_apics()
Stefan Reinauer14e22772010-04-27 06:56:47 +000011 *
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000012 * See Kontron 986LCD-M port for a good example of an ACPI implementation
13 * in coreboot.
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000014 */
Stefan Reinauer688b3852004-01-28 16:56:14 +000015
16#include <console/console.h>
17#include <string.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -070018#include <acpi/acpi.h>
19#include <acpi/acpi_ivrs.h>
20#include <acpi/acpigen.h>
Stefan Reinauer777224c2005-01-19 14:06:41 +000021#include <device/pci.h>
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000022#include <cbmem.h>
Elyes HAOUASe2d152c2019-06-21 07:06:50 +020023#include <commonlib/helpers.h>
Duncan Laurie11290c42012-10-03 19:07:05 -070024#include <cpu/cpu.h>
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +010025#include <cbfs.h>
Elyes HAOUAScdd2f632021-02-11 19:37:11 +010026#include <types.h>
Elyes HAOUAS26071aa2019-02-15 08:21:33 +010027#include <version.h>
Werner Zehdb561e62019-02-19 13:39:56 +010028#include <commonlib/sort.h>
Nico Huberd5811372021-07-12 18:11:58 +020029#include <pc80/mc146818rtc.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
Patrick Rudolph56a3ef22020-03-24 17:29:49 +0100130int acpi_create_madt_lx2apic(acpi_madt_lx2apic_t *lapic, u32 cpu, u32 apic)
131{
132 lapic->type = LOCAL_X2APIC; /* Local APIC structure */
133 lapic->reserved = 0;
134 lapic->length = sizeof(acpi_madt_lx2apic_t);
135 lapic->flags = (1 << 0); /* Processor/LAPIC enabled */
136 lapic->processor_id = cpu;
137 lapic->x2apic_id = apic;
138
139 return lapic->length;
140}
141
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000142unsigned long acpi_create_madt_lapics(unsigned long current)
143{
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100144 struct device *cpu;
Werner Zeh423adfb2019-03-06 09:31:02 +0100145 int index, apic_ids[CONFIG_MAX_CPUS] = {0}, num_cpus = 0;
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000146
Uwe Hermann622824c2010-11-19 15:14:42 +0000147 for (cpu = all_devices; cpu; cpu = cpu->next) {
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000148 if ((cpu->path.type != DEVICE_PATH_APIC) ||
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800149 (cpu->bus->dev->path.type != DEVICE_PATH_CPU_CLUSTER)) {
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000150 continue;
151 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000152 if (!cpu->enabled)
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000153 continue;
Werner Zehdb561e62019-02-19 13:39:56 +0100154 if (num_cpus >= ARRAY_SIZE(apic_ids))
155 break;
156 apic_ids[num_cpus++] = cpu->path.apic.apic_id;
157 }
Werner Zehfedb36e2019-03-12 07:07:50 +0100158 if (num_cpus > 1)
159 bubblesort(apic_ids, num_cpus, NUM_ASCENDING);
Werner Zehdb561e62019-02-19 13:39:56 +0100160 for (index = 0; index < num_cpus; index++) {
Patrick Rudolph56a3ef22020-03-24 17:29:49 +0100161 if (apic_ids[index] < 0xff)
162 current += acpi_create_madt_lapic((acpi_madt_lapic_t *)current,
163 index, apic_ids[index]);
164 else
165 current += acpi_create_madt_lx2apic((acpi_madt_lx2apic_t *)current,
166 index, apic_ids[index]);
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000167 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000168
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000169 return current;
170}
171
Uwe Hermann622824c2010-11-19 15:14:42 +0000172int acpi_create_madt_ioapic(acpi_madt_ioapic_t *ioapic, u8 id, u32 addr,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300173 u32 gsi_base)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000174{
Himanshu Sahdev8c09b822019-10-21 18:50:50 +0530175 ioapic->type = IO_APIC; /* I/O APIC structure */
Uwe Hermann622824c2010-11-19 15:14:42 +0000176 ioapic->length = sizeof(acpi_madt_ioapic_t);
177 ioapic->reserved = 0x00;
178 ioapic->gsi_base = gsi_base;
179 ioapic->ioapic_id = id;
180 ioapic->ioapic_addr = addr;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000181
Uwe Hermann622824c2010-11-19 15:14:42 +0000182 return ioapic->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000183}
184
Stefan Reinauer777224c2005-01-19 14:06:41 +0000185int acpi_create_madt_irqoverride(acpi_madt_irqoverride_t *irqoverride,
Stefan Reinauer06feb882004-02-03 16:11:35 +0000186 u8 bus, u8 source, u32 gsirq, u16 flags)
187{
Himanshu Sahdev8c09b822019-10-21 18:50:50 +0530188 irqoverride->type = IRQ_SOURCE_OVERRIDE; /* Interrupt source override */
Uwe Hermann622824c2010-11-19 15:14:42 +0000189 irqoverride->length = sizeof(acpi_madt_irqoverride_t);
190 irqoverride->bus = bus;
191 irqoverride->source = source;
192 irqoverride->gsirq = gsirq;
193 irqoverride->flags = flags;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000194
Uwe Hermann622824c2010-11-19 15:14:42 +0000195 return irqoverride->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000196}
197
Stefan Reinauer777224c2005-01-19 14:06:41 +0000198int acpi_create_madt_lapic_nmi(acpi_madt_lapic_nmi_t *lapic_nmi, u8 cpu,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300199 u16 flags, u8 lint)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000200{
Himanshu Sahdev8c09b822019-10-21 18:50:50 +0530201 lapic_nmi->type = LOCAL_APIC_NMI; /* Local APIC NMI structure */
Uwe Hermann622824c2010-11-19 15:14:42 +0000202 lapic_nmi->length = sizeof(acpi_madt_lapic_nmi_t);
203 lapic_nmi->flags = flags;
204 lapic_nmi->processor_id = cpu;
205 lapic_nmi->lint = lint;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000206
Uwe Hermann622824c2010-11-19 15:14:42 +0000207 return lapic_nmi->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000208}
209
Patrick Rudolph56a3ef22020-03-24 17:29:49 +0100210int acpi_create_madt_lx2apic_nmi(acpi_madt_lx2apic_nmi_t *lapic_nmi, u32 cpu,
211 u16 flags, u8 lint)
212{
213 lapic_nmi->type = LOCAL_X2APIC_NMI; /* Local APIC NMI structure */
214 lapic_nmi->length = sizeof(acpi_madt_lx2apic_nmi_t);
215 lapic_nmi->flags = flags;
216 lapic_nmi->processor_id = cpu;
217 lapic_nmi->lint = lint;
218 lapic_nmi->reserved[0] = 0;
219 lapic_nmi->reserved[1] = 0;
220 lapic_nmi->reserved[2] = 0;
221
222 return lapic_nmi->length;
223}
224
Furquan Shaikhb1859a62020-04-30 21:27:47 -0700225__weak uintptr_t cpu_get_lapic_addr(void)
226{
227 /*
228 * If an architecture does not support LAPIC, this weak implementation returns LAPIC
229 * addr as 0.
230 */
231 return 0;
232}
233
Stefan Reinauer777224c2005-01-19 14:06:41 +0000234void acpi_create_madt(acpi_madt_t *madt)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000235{
Uwe Hermann622824c2010-11-19 15:14:42 +0000236 acpi_header_t *header = &(madt->header);
237 unsigned long current = (unsigned long)madt + sizeof(acpi_madt_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000238
Stefan Reinauer06feb882004-02-03 16:11:35 +0000239 memset((void *)madt, 0, sizeof(acpi_madt_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000240
John Zhao2ba303e2019-05-28 16:48:14 -0700241 if (!header)
242 return;
243
Uwe Hermann622824c2010-11-19 15:14:42 +0000244 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000245 memcpy(header->signature, "APIC", 4);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000246 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000247 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000248 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000249
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100250 header->asl_compiler_revision = asl_revision;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000251 header->length = sizeof(acpi_madt_t);
Marc Jones93a51762018-08-22 18:57:24 -0600252 header->revision = get_acpi_table_revision(MADT);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000253
Furquan Shaikhb1859a62020-04-30 21:27:47 -0700254 madt->lapic_addr = cpu_get_lapic_addr();
Nico Huber9df72e02018-11-24 18:25:50 +0100255 if (CONFIG(ACPI_HAVE_PCAT_8259))
256 madt->flags |= 1;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000257
Stefan Reinauerf622d592005-11-26 16:56:05 +0000258 current = acpi_fill_madt(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000259
Uwe Hermann622824c2010-11-19 15:14:42 +0000260 /* (Re)calculate length and checksum. */
261 header->length = current - (unsigned long)madt;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000262
Uwe Hermann622824c2010-11-19 15:14:42 +0000263 header->checksum = acpi_checksum((void *)madt, header->length);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000264}
265
Kyösti Mälkkib54388d2021-02-14 15:09:45 +0200266static unsigned long acpi_fill_mcfg(unsigned long current)
267{
268 current += acpi_create_mcfg_mmconfig((acpi_mcfg_mmconfig_t *)current,
269 CONFIG_MMCONF_BASE_ADDRESS, 0, 0, CONFIG_MMCONF_BUS_NUMBER - 1);
270 return current;
271}
272
Uwe Hermann622824c2010-11-19 15:14:42 +0000273/* MCFG is defined in the PCI Firmware Specification 3.0. */
Rudolf Mareke6409f22007-11-03 12:50:26 +0000274void acpi_create_mcfg(acpi_mcfg_t *mcfg)
275{
Uwe Hermann622824c2010-11-19 15:14:42 +0000276 acpi_header_t *header = &(mcfg->header);
277 unsigned long current = (unsigned long)mcfg + sizeof(acpi_mcfg_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000278
Rudolf Mareke6409f22007-11-03 12:50:26 +0000279 memset((void *)mcfg, 0, sizeof(acpi_mcfg_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000280
John Zhao2ba303e2019-05-28 16:48:14 -0700281 if (!header)
282 return;
283
Uwe Hermann622824c2010-11-19 15:14:42 +0000284 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000285 memcpy(header->signature, "MCFG", 4);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000286 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000287 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000288 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000289
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100290 header->asl_compiler_revision = asl_revision;
Rudolf Mareke6409f22007-11-03 12:50:26 +0000291 header->length = sizeof(acpi_mcfg_t);
Marc Jones93a51762018-08-22 18:57:24 -0600292 header->revision = get_acpi_table_revision(MCFG);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000293
Kyösti Mälkkib54388d2021-02-14 15:09:45 +0200294 if (CONFIG(MMCONF_SUPPORT))
295 current = acpi_fill_mcfg(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000296
Uwe Hermann622824c2010-11-19 15:14:42 +0000297 /* (Re)calculate length and checksum. */
298 header->length = current - (unsigned long)mcfg;
299 header->checksum = acpi_checksum((void *)mcfg, header->length);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000300}
301
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200302static void *get_tcpa_log(u32 *size)
303{
304 const struct cbmem_entry *ce;
305 const u32 tcpa_default_log_len = 0x10000;
306 void *lasa;
Philipp Deppenwiese791ba972018-07-30 01:15:20 +0200307 ce = cbmem_entry_find(CBMEM_ID_TCPA_TCG_LOG);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200308 if (ce) {
309 lasa = cbmem_entry_start(ce);
310 *size = cbmem_entry_size(ce);
311 printk(BIOS_DEBUG, "TCPA log found at %p\n", lasa);
312 return lasa;
313 }
Philipp Deppenwiese791ba972018-07-30 01:15:20 +0200314 lasa = cbmem_add(CBMEM_ID_TCPA_TCG_LOG, tcpa_default_log_len);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200315 if (!lasa) {
316 printk(BIOS_ERR, "TCPA log creation failed\n");
317 return NULL;
318 }
319
320 printk(BIOS_DEBUG, "TCPA log created at %p\n", lasa);
Lee Leahy024b13d2017-03-16 13:41:11 -0700321 memset(lasa, 0, tcpa_default_log_len);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200322
323 *size = tcpa_default_log_len;
324 return lasa;
325}
326
327static void acpi_create_tcpa(acpi_tcpa_t *tcpa)
328{
329 acpi_header_t *header = &(tcpa->header);
330 u32 tcpa_log_len;
331 void *lasa;
332
333 memset((void *)tcpa, 0, sizeof(acpi_tcpa_t));
334
335 lasa = get_tcpa_log(&tcpa_log_len);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700336 if (!lasa)
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200337 return;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200338
John Zhao2ba303e2019-05-28 16:48:14 -0700339 if (!header)
340 return;
341
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200342 /* Fill out header fields. */
343 memcpy(header->signature, "TCPA", 4);
344 memcpy(header->oem_id, OEM_ID, 6);
345 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
346 memcpy(header->asl_compiler_id, ASLC, 4);
347
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100348 header->asl_compiler_revision = asl_revision;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200349 header->length = sizeof(acpi_tcpa_t);
Marc Jones93a51762018-08-22 18:57:24 -0600350 header->revision = get_acpi_table_revision(TCPA);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200351
352 tcpa->platform_class = 0;
353 tcpa->laml = tcpa_log_len;
Stefan Reinauerdefee172015-06-18 01:11:19 -0700354 tcpa->lasa = (uintptr_t) lasa;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200355
356 /* Calculate checksum. */
357 header->checksum = acpi_checksum((void *)tcpa, header->length);
358}
359
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100360static void *get_tpm2_log(u32 *size)
361{
362 const struct cbmem_entry *ce;
363 const u32 tpm2_default_log_len = 0x10000;
364 void *lasa;
365 ce = cbmem_entry_find(CBMEM_ID_TPM2_TCG_LOG);
366 if (ce) {
367 lasa = cbmem_entry_start(ce);
368 *size = cbmem_entry_size(ce);
369 printk(BIOS_DEBUG, "TPM2 log found at %p\n", lasa);
370 return lasa;
371 }
372 lasa = cbmem_add(CBMEM_ID_TPM2_TCG_LOG, tpm2_default_log_len);
373 if (!lasa) {
374 printk(BIOS_ERR, "TPM2 log creation failed\n");
375 return NULL;
376 }
377
378 printk(BIOS_DEBUG, "TPM2 log created at %p\n", lasa);
379 memset(lasa, 0, tpm2_default_log_len);
380
381 *size = tpm2_default_log_len;
382 return lasa;
383}
384
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200385static void acpi_create_tpm2(acpi_tpm2_t *tpm2)
386{
387 acpi_header_t *header = &(tpm2->header);
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100388 u32 tpm2_log_len;
389 void *lasa;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200390
391 memset((void *)tpm2, 0, sizeof(acpi_tpm2_t));
392
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100393 /*
394 * Some payloads like SeaBIOS depend on log area to use TPM2.
395 * Get the memory size and address of TPM2 log area or initialize it.
396 */
397 lasa = get_tpm2_log(&tpm2_log_len);
398 if (!lasa)
399 tpm2_log_len = 0;
400
John Zhao2ba303e2019-05-28 16:48:14 -0700401 if (!header)
402 return;
403
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200404 /* Fill out header fields. */
405 memcpy(header->signature, "TPM2", 4);
406 memcpy(header->oem_id, OEM_ID, 6);
407 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
408 memcpy(header->asl_compiler_id, ASLC, 4);
409
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100410 header->asl_compiler_revision = asl_revision;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200411 header->length = sizeof(acpi_tpm2_t);
412 header->revision = get_acpi_table_revision(TPM2);
413
414 /* Hard to detect for coreboot. Just set it to 0 */
415 tpm2->platform_class = 0;
Christian Walter26e0d4c2019-07-14 15:47:23 +0200416 if (CONFIG(CRB_TPM)) {
417 /* Must be set to 7 for CRB Support */
418 tpm2->control_area = CONFIG_CRB_TPM_BASE_ADDRESS + 0x40;
419 tpm2->start_method = 7;
420 } else {
421 /* Must be set to 0 for FIFO interface support */
422 tpm2->control_area = 0;
423 tpm2->start_method = 6;
424 }
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200425 memset(tpm2->msp, 0, sizeof(tpm2->msp));
426
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100427 /* Fill the log area size and start address fields. */
428 tpm2->laml = tpm2_log_len;
429 tpm2->lasa = (uintptr_t) lasa;
430
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200431 /* Calculate checksum. */
432 header->checksum = acpi_checksum((void *)tpm2, header->length);
433}
434
Duncan Laurie37319032016-05-26 12:47:05 -0700435static void acpi_ssdt_write_cbtable(void)
436{
437 const struct cbmem_entry *cbtable;
438 uintptr_t base;
439 uint32_t size;
440
441 cbtable = cbmem_entry_find(CBMEM_ID_CBTABLE);
442 if (!cbtable)
443 return;
444 base = (uintptr_t)cbmem_entry_start(cbtable);
445 size = cbmem_entry_size(cbtable);
446
447 acpigen_write_device("CTBL");
448 acpigen_write_coreboot_hid(COREBOOT_ACPI_ID_CBTABLE);
449 acpigen_write_name_integer("_UID", 0);
David Wu5dff3962018-03-21 16:48:53 +0800450 acpigen_write_STA(ACPI_STATUS_DEVICE_HIDDEN_ON);
Duncan Laurie37319032016-05-26 12:47:05 -0700451 acpigen_write_name("_CRS");
452 acpigen_write_resourcetemplate_header();
453 acpigen_write_mem32fixed(0, base, size);
454 acpigen_write_resourcetemplate_footer();
455 acpigen_pop_len();
456}
457
Myles Watson3fe6b702009-10-09 20:13:43 +0000458void acpi_create_ssdt_generator(acpi_header_t *ssdt, const char *oem_table_id)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000459{
Uwe Hermann622824c2010-11-19 15:14:42 +0000460 unsigned long current = (unsigned long)ssdt + sizeof(acpi_header_t);
461
Rudolf Marek293b5f52009-02-01 18:35:15 +0000462 memset((void *)ssdt, 0, sizeof(acpi_header_t));
Uwe Hermann622824c2010-11-19 15:14:42 +0000463
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000464 memcpy(&ssdt->signature, "SSDT", 4);
Marc Jones93a51762018-08-22 18:57:24 -0600465 ssdt->revision = get_acpi_table_revision(SSDT);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000466 memcpy(&ssdt->oem_id, OEM_ID, 6);
467 memcpy(&ssdt->oem_table_id, oem_table_id, 8);
468 ssdt->oem_revision = 42;
Uwe Hermann622824c2010-11-19 15:14:42 +0000469 memcpy(&ssdt->asl_compiler_id, ASLC, 4);
Elyes HAOUAS26071aa2019-02-15 08:21:33 +0100470 ssdt->asl_compiler_revision = asl_revision;
Rudolf Marek293b5f52009-02-01 18:35:15 +0000471 ssdt->length = sizeof(acpi_header_t);
472
Stefan Reinauer8dcd50b2009-03-06 17:24:29 +0000473 acpigen_set_current((char *) current);
Duncan Laurie37319032016-05-26 12:47:05 -0700474
475 /* Write object to declare coreboot tables */
476 acpi_ssdt_write_cbtable();
477
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200478 {
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100479 struct device *dev;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200480 for (dev = all_devices; dev; dev = dev->next)
Karthikeyan Ramasubramaniand1c0f952020-11-02 16:26:52 -0700481 if (dev->enabled && dev->ops && dev->ops->acpi_fill_ssdt)
Nico Huber68680dd2020-03-31 17:34:52 +0200482 dev->ops->acpi_fill_ssdt(dev);
Vladimir Serbinenko0a669912014-10-05 14:34:17 +0200483 current = (unsigned long) acpigen_get_current();
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200484 }
Rudolf Marek293b5f52009-02-01 18:35:15 +0000485
Uwe Hermann622824c2010-11-19 15:14:42 +0000486 /* (Re)calculate length and checksum. */
Rudolf Marek293b5f52009-02-01 18:35:15 +0000487 ssdt->length = current - (unsigned long)ssdt;
488 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
489}
490
Stefan Reinauerf622d592005-11-26 16:56:05 +0000491int acpi_create_srat_lapic(acpi_srat_lapic_t *lapic, u8 node, u8 apic)
492{
Myles Watson0b4c9f02009-03-10 18:06:47 +0000493 memset((void *)lapic, 0, sizeof(acpi_srat_lapic_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000494
Uwe Hermann622824c2010-11-19 15:14:42 +0000495 lapic->type = 0; /* Processor local APIC/SAPIC affinity structure */
496 lapic->length = sizeof(acpi_srat_lapic_t);
497 lapic->flags = (1 << 0); /* Enabled (the use of this structure). */
498 lapic->proximity_domain_7_0 = node;
499 /* TODO: proximity_domain_31_8, local SAPIC EID, clock domain. */
500 lapic->apic_id = apic;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000501
Uwe Hermann622824c2010-11-19 15:14:42 +0000502 return lapic->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000503}
504
Uwe Hermann622824c2010-11-19 15:14:42 +0000505int 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 +0300506 u32 flags)
Stefan Reinauerf622d592005-11-26 16:56:05 +0000507{
Uwe Hermann622824c2010-11-19 15:14:42 +0000508 mem->type = 1; /* Memory affinity structure */
509 mem->length = sizeof(acpi_srat_mem_t);
510 mem->base_address_low = (basek << 10);
511 mem->base_address_high = (basek >> (32 - 10));
512 mem->length_low = (sizek << 10);
513 mem->length_high = (sizek >> (32 - 10));
514 mem->proximity_domain = node;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000515 mem->flags = flags;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000516
Uwe Hermann622824c2010-11-19 15:14:42 +0000517 return mem->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000518}
519
Jonathan Zhang3164b642021-04-21 17:51:31 -0700520int acpi_create_srat_gia_pci(acpi_srat_gia_t *gia, u32 proximity_domain,
521 u16 seg, u8 bus, u8 dev, u8 func, u32 flags)
522{
523 gia->type = ACPI_SRAT_STRUCTURE_GIA;
524 gia->length = sizeof(acpi_srat_gia_t);
525 gia->proximity_domain = proximity_domain;
526 gia->dev_handle_type = ACPI_SRAT_GIA_DEV_HANDLE_PCI;
527 /* First two bytes has segment number */
528 memcpy(gia->dev_handle, &seg, 2);
529 gia->dev_handle[2] = bus; /* Byte 2 has bus number */
530 /* Byte 3 has bits 7:3 for dev, bits 2:0 for func */
531 gia->dev_handle[3] = PCI_SLOT(dev) | PCI_FUNC(func);
532 gia->flags = flags;
533
534 return gia->length;
535}
536
Uwe Hermann622824c2010-11-19 15:14:42 +0000537/* http://www.microsoft.com/whdc/system/sysinternals/sratdwn.mspx */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +0200538void acpi_create_srat(acpi_srat_t *srat,
539 unsigned long (*acpi_fill_srat)(unsigned long current))
Stefan Reinauerf622d592005-11-26 16:56:05 +0000540{
Uwe Hermann622824c2010-11-19 15:14:42 +0000541 acpi_header_t *header = &(srat->header);
542 unsigned long current = (unsigned long)srat + sizeof(acpi_srat_t);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000543
Uwe Hermann622824c2010-11-19 15:14:42 +0000544 memset((void *)srat, 0, sizeof(acpi_srat_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000545
John Zhao2ba303e2019-05-28 16:48:14 -0700546 if (!header)
547 return;
548
Uwe Hermann622824c2010-11-19 15:14:42 +0000549 /* Fill out header fields. */
550 memcpy(header->signature, "SRAT", 4);
551 memcpy(header->oem_id, OEM_ID, 6);
552 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
553 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000554
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100555 header->asl_compiler_revision = asl_revision;
Uwe Hermann622824c2010-11-19 15:14:42 +0000556 header->length = sizeof(acpi_srat_t);
Marc Jones93a51762018-08-22 18:57:24 -0600557 header->revision = get_acpi_table_revision(SRAT);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000558
Uwe Hermann622824c2010-11-19 15:14:42 +0000559 srat->resv = 1; /* Spec: Reserved to 1 for backwards compatibility. */
Stefan Reinauerf622d592005-11-26 16:56:05 +0000560
Uwe Hermann622824c2010-11-19 15:14:42 +0000561 current = acpi_fill_srat(current);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000562
Uwe Hermann622824c2010-11-19 15:14:42 +0000563 /* (Re)calculate length and checksum. */
564 header->length = current - (unsigned long)srat;
565 header->checksum = acpi_checksum((void *)srat, header->length);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000566}
567
Jonathan Zhang2a4e1f42021-04-01 11:43:37 -0700568int acpi_create_hmat_mpda(acpi_hmat_mpda_t *mpda, u32 initiator, u32 memory)
569{
570 memset((void *)mpda, 0, sizeof(acpi_hmat_mpda_t));
571
572 mpda->type = 0; /* Memory Proximity Domain Attributes structure */
573 mpda->length = sizeof(acpi_hmat_mpda_t);
574 /*
575 * Proximity Domain for Attached Initiator field is valid.
576 * Bit 1 and bit 2 are reserved since HMAT revision 2.
577 */
578 mpda->flags = (1 << 0);
579 mpda->proximity_domain_initiator = initiator;
580 mpda->proximity_domain_memory = memory;
581
582 return mpda->length;
583}
584
585void acpi_create_hmat(acpi_hmat_t *hmat,
586 unsigned long (*acpi_fill_hmat)(unsigned long current))
587{
588 acpi_header_t *header = &(hmat->header);
589 unsigned long current = (unsigned long)hmat + sizeof(acpi_hmat_t);
590
591 memset((void *)hmat, 0, sizeof(acpi_hmat_t));
592
593 if (!header)
594 return;
595
596 /* Fill out header fields. */
597 memcpy(header->signature, "HMAT", 4);
598 memcpy(header->oem_id, OEM_ID, 6);
599 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
600 memcpy(header->asl_compiler_id, ASLC, 4);
601
602 header->asl_compiler_revision = asl_revision;
603 header->length = sizeof(acpi_hmat_t);
604 header->revision = get_acpi_table_revision(HMAT);
605
606 current = acpi_fill_hmat(current);
607
608 /* (Re)calculate length and checksum. */
609 header->length = current - (unsigned long)hmat;
610 header->checksum = acpi_checksum((void *)hmat, header->length);
611}
612
Nico Hubere561f352015-10-26 11:51:25 +0100613void acpi_create_dmar(acpi_dmar_t *dmar, enum dmar_flags flags,
Lee Leahy024b13d2017-03-16 13:41:11 -0700614 unsigned long (*acpi_fill_dmar)(unsigned long))
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200615{
616 acpi_header_t *header = &(dmar->header);
617 unsigned long current = (unsigned long)dmar + sizeof(acpi_dmar_t);
618
619 memset((void *)dmar, 0, sizeof(acpi_dmar_t));
620
John Zhao2ba303e2019-05-28 16:48:14 -0700621 if (!header)
622 return;
623
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200624 /* Fill out header fields. */
625 memcpy(header->signature, "DMAR", 4);
626 memcpy(header->oem_id, OEM_ID, 6);
627 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
628 memcpy(header->asl_compiler_id, ASLC, 4);
629
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100630 header->asl_compiler_revision = asl_revision;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200631 header->length = sizeof(acpi_dmar_t);
Marc Jones93a51762018-08-22 18:57:24 -0600632 header->revision = get_acpi_table_revision(DMAR);
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200633
Jacob Laskaf3f654d2016-03-15 21:53:27 -0500634 dmar->host_address_width = cpu_phys_address_size() - 1;
Nico Hubere561f352015-10-26 11:51:25 +0100635 dmar->flags = flags;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200636
637 current = acpi_fill_dmar(current);
638
639 /* (Re)calculate length and checksum. */
640 header->length = current - (unsigned long)dmar;
641 header->checksum = acpi_checksum((void *)dmar, header->length);
642}
643
644unsigned long acpi_create_dmar_drhd(unsigned long current, u8 flags,
Matt DeVillier7866d492018-03-29 14:59:57 +0200645 u16 segment, u64 bar)
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200646{
647 dmar_entry_t *drhd = (dmar_entry_t *)current;
648 memset(drhd, 0, sizeof(*drhd));
649 drhd->type = DMAR_DRHD;
650 drhd->length = sizeof(*drhd); /* will be fixed up later */
651 drhd->flags = flags;
652 drhd->segment = segment;
653 drhd->bar = bar;
654
655 return drhd->length;
656}
657
Matt DeVillier7866d492018-03-29 14:59:57 +0200658unsigned long acpi_create_dmar_rmrr(unsigned long current, u16 segment,
659 u64 bar, u64 limit)
660{
661 dmar_rmrr_entry_t *rmrr = (dmar_rmrr_entry_t *)current;
662 memset(rmrr, 0, sizeof(*rmrr));
663 rmrr->type = DMAR_RMRR;
664 rmrr->length = sizeof(*rmrr); /* will be fixed up later */
665 rmrr->segment = segment;
666 rmrr->bar = bar;
667 rmrr->limit = limit;
668
669 return rmrr->length;
670}
671
Werner Zehd4d76952016-07-27 06:56:36 +0200672unsigned long acpi_create_dmar_atsr(unsigned long current, u8 flags,
673 u16 segment)
674{
675 dmar_atsr_entry_t *atsr = (dmar_atsr_entry_t *)current;
676 memset(atsr, 0, sizeof(*atsr));
677 atsr->type = DMAR_ATSR;
678 atsr->length = sizeof(*atsr); /* will be fixed up later */
679 atsr->flags = flags;
680 atsr->segment = segment;
681
682 return atsr->length;
683}
684
John Zhao76e70672019-04-04 11:03:28 -0700685unsigned long acpi_create_dmar_rhsa(unsigned long current, u64 base_addr,
686 u32 proximity_domain)
687{
688 dmar_rhsa_entry_t *rhsa = (dmar_rhsa_entry_t *)current;
689 memset(rhsa, 0, sizeof(*rhsa));
690 rhsa->type = DMAR_RHSA;
691 rhsa->length = sizeof(*rhsa);
692 rhsa->base_address = base_addr;
693 rhsa->proximity_domain = proximity_domain;
694
695 return rhsa->length;
696}
697
698unsigned long acpi_create_dmar_andd(unsigned long current, u8 device_number,
699 const char *device_name)
700{
701 dmar_andd_entry_t *andd = (dmar_andd_entry_t *)current;
702 int andd_len = sizeof(dmar_andd_entry_t) + strlen(device_name) + 1;
703 memset(andd, 0, andd_len);
704 andd->type = DMAR_ANDD;
705 andd->length = andd_len;
706 andd->device_number = device_number;
707 memcpy(&andd->device_name, device_name, strlen(device_name));
708
709 return andd->length;
710}
711
John Zhao091532d2021-04-17 16:03:21 -0700712unsigned long acpi_create_dmar_satc(unsigned long current, u8 flags, u16 segment)
John Zhao6edbb182021-03-24 11:55:09 -0700713{
714 dmar_satc_entry_t *satc = (dmar_satc_entry_t *)current;
John Zhao091532d2021-04-17 16:03:21 -0700715 int satc_len = sizeof(dmar_satc_entry_t);
John Zhao6edbb182021-03-24 11:55:09 -0700716 memset(satc, 0, satc_len);
717 satc->type = DMAR_SATC;
718 satc->length = satc_len;
719 satc->flags = flags;
720 satc->segment_number = segment;
John Zhao6edbb182021-03-24 11:55:09 -0700721
722 return satc->length;
723}
724
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200725void acpi_dmar_drhd_fixup(unsigned long base, unsigned long current)
726{
727 dmar_entry_t *drhd = (dmar_entry_t *)base;
728 drhd->length = current - base;
729}
730
Matt DeVillier7866d492018-03-29 14:59:57 +0200731void acpi_dmar_rmrr_fixup(unsigned long base, unsigned long current)
732{
733 dmar_rmrr_entry_t *rmrr = (dmar_rmrr_entry_t *)base;
734 rmrr->length = current - base;
735}
736
Werner Zehd4d76952016-07-27 06:56:36 +0200737void acpi_dmar_atsr_fixup(unsigned long base, unsigned long current)
738{
739 dmar_atsr_entry_t *atsr = (dmar_atsr_entry_t *)base;
740 atsr->length = current - base;
741}
742
John Zhao6edbb182021-03-24 11:55:09 -0700743void acpi_dmar_satc_fixup(unsigned long base, unsigned long current)
744{
745 dmar_satc_entry_t *satc = (dmar_satc_entry_t *)base;
746 satc->length = current - base;
747}
748
Matt DeVillier7866d492018-03-29 14:59:57 +0200749static unsigned long acpi_create_dmar_ds(unsigned long current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100750 enum dev_scope_type type, u8 enumeration_id, u8 bus, u8 dev, u8 fn)
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200751{
Nico Huber6c4751d2015-10-26 12:03:54 +0100752 /* we don't support longer paths yet */
753 const size_t dev_scope_length = sizeof(dev_scope_t) + 2;
754
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200755 dev_scope_t *ds = (dev_scope_t *)current;
Nico Huber6c4751d2015-10-26 12:03:54 +0100756 memset(ds, 0, dev_scope_length);
757 ds->type = type;
758 ds->length = dev_scope_length;
759 ds->enumeration = enumeration_id;
760 ds->start_bus = bus;
761 ds->path[0].dev = dev;
762 ds->path[0].fn = fn;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200763
764 return ds->length;
765}
766
Matt DeVillier7866d492018-03-29 14:59:57 +0200767unsigned long acpi_create_dmar_ds_pci_br(unsigned long current, u8 bus,
Werner Zeh21a5bff2016-07-27 07:07:20 +0200768 u8 dev, u8 fn)
769{
Matt DeVillier7866d492018-03-29 14:59:57 +0200770 return acpi_create_dmar_ds(current,
Werner Zeh21a5bff2016-07-27 07:07:20 +0200771 SCOPE_PCI_SUB, 0, bus, dev, fn);
772}
773
Matt DeVillier7866d492018-03-29 14:59:57 +0200774unsigned long acpi_create_dmar_ds_pci(unsigned long current, u8 bus,
Nico Huber6c4751d2015-10-26 12:03:54 +0100775 u8 dev, u8 fn)
776{
Matt DeVillier7866d492018-03-29 14:59:57 +0200777 return acpi_create_dmar_ds(current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100778 SCOPE_PCI_ENDPOINT, 0, bus, dev, fn);
779}
780
Matt DeVillier7866d492018-03-29 14:59:57 +0200781unsigned long acpi_create_dmar_ds_ioapic(unsigned long current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100782 u8 enumeration_id, u8 bus, u8 dev, u8 fn)
783{
Matt DeVillier7866d492018-03-29 14:59:57 +0200784 return acpi_create_dmar_ds(current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100785 SCOPE_IOAPIC, enumeration_id, bus, dev, fn);
786}
787
Matt DeVillier7866d492018-03-29 14:59:57 +0200788unsigned long acpi_create_dmar_ds_msi_hpet(unsigned long current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100789 u8 enumeration_id, u8 bus, u8 dev, u8 fn)
790{
Matt DeVillier7866d492018-03-29 14:59:57 +0200791 return acpi_create_dmar_ds(current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100792 SCOPE_MSI_HPET, enumeration_id, bus, dev, fn);
793}
794
Uwe Hermann622824c2010-11-19 15:14:42 +0000795/* http://h21007.www2.hp.com/portal/download/files/unprot/Itanium/slit.pdf */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +0200796void acpi_create_slit(acpi_slit_t *slit,
797 unsigned long (*acpi_fill_slit)(unsigned long current))
Yinghai Lud4b278c2006-10-04 20:46:15 +0000798{
Uwe Hermann622824c2010-11-19 15:14:42 +0000799 acpi_header_t *header = &(slit->header);
800 unsigned long current = (unsigned long)slit + sizeof(acpi_slit_t);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000801
Uwe Hermann622824c2010-11-19 15:14:42 +0000802 memset((void *)slit, 0, sizeof(acpi_slit_t));
Yinghai Lud4b278c2006-10-04 20:46:15 +0000803
John Zhao2ba303e2019-05-28 16:48:14 -0700804 if (!header)
805 return;
806
Uwe Hermann622824c2010-11-19 15:14:42 +0000807 /* Fill out header fields. */
808 memcpy(header->signature, "SLIT", 4);
809 memcpy(header->oem_id, OEM_ID, 6);
810 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
811 memcpy(header->asl_compiler_id, ASLC, 4);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000812
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100813 header->asl_compiler_revision = asl_revision;
Uwe Hermann622824c2010-11-19 15:14:42 +0000814 header->length = sizeof(acpi_slit_t);
Marc Jones93a51762018-08-22 18:57:24 -0600815 header->revision = get_acpi_table_revision(SLIT);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000816
Uwe Hermann622824c2010-11-19 15:14:42 +0000817 current = acpi_fill_slit(current);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000818
Uwe Hermann622824c2010-11-19 15:14:42 +0000819 /* (Re)calculate length and checksum. */
820 header->length = current - (unsigned long)slit;
821 header->checksum = acpi_checksum((void *)slit, header->length);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000822}
823
Uwe Hermann622824c2010-11-19 15:14:42 +0000824/* http://www.intel.com/hardwaredesign/hpetspec_1.pdf */
Stefan Reinauer777224c2005-01-19 14:06:41 +0000825void acpi_create_hpet(acpi_hpet_t *hpet)
Stefan Reinauer688b3852004-01-28 16:56:14 +0000826{
Uwe Hermann622824c2010-11-19 15:14:42 +0000827 acpi_header_t *header = &(hpet->header);
828 acpi_addr_t *addr = &(hpet->addr);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000829
Stefan Reinauera7648c22004-01-29 17:31:34 +0000830 memset((void *)hpet, 0, sizeof(acpi_hpet_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000831
John Zhao2ba303e2019-05-28 16:48:14 -0700832 if (!header)
833 return;
834
Uwe Hermann622824c2010-11-19 15:14:42 +0000835 /* Fill out header fields. */
Stefan Reinauer4704dc52009-07-22 01:11:37 +0000836 memcpy(header->signature, "HPET", 4);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000837 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000838 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000839 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000840
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100841 header->asl_compiler_revision = asl_revision;
Stefan Reinauer688b3852004-01-28 16:56:14 +0000842 header->length = sizeof(acpi_hpet_t);
Marc Jones93a51762018-08-22 18:57:24 -0600843 header->revision = get_acpi_table_revision(HPET);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000844
Uwe Hermann622824c2010-11-19 15:14:42 +0000845 /* Fill out HPET address. */
Elyes HAOUAS04071f42020-07-20 17:05:24 +0200846 addr->space_id = ACPI_ADDRESS_SPACE_MEMORY;
Uwe Hermann622824c2010-11-19 15:14:42 +0000847 addr->bit_width = 64;
848 addr->bit_offset = 0;
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200849 addr->addrl = CONFIG_HPET_ADDRESS & 0xffffffff;
850 addr->addrh = ((unsigned long long)CONFIG_HPET_ADDRESS) >> 32;
Stefan Reinauer688b3852004-01-28 16:56:14 +0000851
Lee Leahy024b13d2017-03-16 13:41:11 -0700852 hpet->id = *(unsigned int *)CONFIG_HPET_ADDRESS;
Uwe Hermann622824c2010-11-19 15:14:42 +0000853 hpet->number = 0;
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200854 hpet->min_tick = CONFIG_HPET_MIN_TICKS;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000855
Uwe Hermann622824c2010-11-19 15:14:42 +0000856 header->checksum = acpi_checksum((void *)hpet, sizeof(acpi_hpet_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000857}
Uwe Hermann622824c2010-11-19 15:14:42 +0000858
Rocky Phaguraeff07132021-01-10 15:42:50 -0800859/*
860 * This method adds the ACPI error injection capability. It fills the default information.
861 * HW dependent code (caller) can modify the defaults upon return. If no changes are necessary
862 * and the defaults are acceptable then caller can simply add the table (acpi_add_table).
863 * INPUTS:
864 * einj - ptr to the starting location of EINJ table
865 * actions - number of actions to trigger an error (HW dependent)
866 * addr - address of trigger action table. This should be ACPI reserved memory and it will be
867 * shared between OS and FW.
868 */
869void acpi_create_einj(acpi_einj_t *einj, uintptr_t addr, u8 actions)
870{
871 int i;
872 acpi_header_t *header = &(einj->header);
873 acpi_injection_header_t *inj_header = &(einj->inj_header);
874 acpi_einj_smi_t *einj_smi = (acpi_einj_smi_t *)addr;
875 acpi_einj_trigger_table_t *tat;
876 if (!header)
877 return;
878
879 printk(BIOS_DEBUG, "%s einj_smi = %p\n", __func__, einj_smi);
880 memset(einj_smi, 0, sizeof(acpi_einj_smi_t));
881 tat = (acpi_einj_trigger_table_t *)(einj_smi + sizeof(acpi_einj_smi_t));
882 tat->header_size = 16;
883 tat->revision = 0;
884 tat->table_size = sizeof(acpi_einj_trigger_table_t) +
885 sizeof(acpi_einj_action_table_t) * actions - 1;
886 tat->entry_count = actions;
887 printk(BIOS_DEBUG, "%s trigger_action_table = %p\n", __func__, tat);
888
889 for (i = 0; i < actions; i++) {
890 tat->trigger_action[i].action = TRIGGER_ERROR;
891 tat->trigger_action[i].instruction = NO_OP;
892 tat->trigger_action[i].flags = FLAG_IGNORE;
893 tat->trigger_action[i].reg.space_id = ACPI_ADDRESS_SPACE_MEMORY;
894 tat->trigger_action[i].reg.bit_width = 64;
895 tat->trigger_action[i].reg.bit_offset = 0;
896 tat->trigger_action[i].reg.access_size = ACPI_ACCESS_SIZE_QWORD_ACCESS;
897 tat->trigger_action[i].reg.addr = 0;
898 tat->trigger_action[i].value = 0;
899 tat->trigger_action[i].mask = 0xFFFFFFFF;
900 }
901
902 acpi_einj_action_table_t default_actions[ACTION_COUNT] = {
903 [0] = {
904 .action = BEGIN_INJECT_OP,
905 .instruction = WRITE_REGISTER_VALUE,
906 .flags = FLAG_PRESERVE,
907 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->op_state),
908 .value = 0,
909 .mask = 0xFFFFFFFF
910 },
911 [1] = {
912 .action = GET_TRIGGER_ACTION_TABLE,
913 .instruction = READ_REGISTER,
914 .flags = FLAG_IGNORE,
915 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->trigger_action_table),
916 .value = 0,
917 .mask = 0xFFFFFFFFFFFFFFFF
918 },
919 [2] = {
920 .action = SET_ERROR_TYPE,
921 .instruction = WRITE_REGISTER,
922 .flags = FLAG_PRESERVE,
923 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->err_inject[0]),
924 .value = 0,
925 .mask = 0xFFFFFFFF
926 },
927 [3] = {
928 .action = GET_ERROR_TYPE,
929 .instruction = READ_REGISTER,
930 .flags = FLAG_IGNORE,
931 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->err_inj_cap),
932 .value = 0,
933 .mask = 0xFFFFFFFF
934 },
935 [4] = {
936 .action = END_INJECT_OP,
937 .instruction = WRITE_REGISTER_VALUE,
938 .flags = FLAG_PRESERVE,
939 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->op_state),
940 .value = 0,
941 .mask = 0xFFFFFFFF
942
943 },
944 [5] = {
945 .action = EXECUTE_INJECT_OP,
946 .instruction = WRITE_REGISTER_VALUE,
947 .flags = FLAG_PRESERVE,
948 .reg = EINJ_REG_IO(),
949 .value = 0x9a,
950 .mask = 0xFFFF,
951 },
952 [6] = {
953 .action = CHECK_BUSY_STATUS,
954 .instruction = READ_REGISTER_VALUE,
955 .flags = FLAG_IGNORE,
956 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->op_status),
957 .value = 1,
958 .mask = 1,
959 },
960 [7] = {
961 .action = GET_CMD_STATUS,
962 .instruction = READ_REGISTER,
963 .flags = FLAG_PRESERVE,
964 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->cmd_sts),
965 .value = 0,
966 .mask = 0x1fe,
967 },
968 [8] = {
969 .action = SET_ERROR_TYPE_WITH_ADDRESS,
970 .instruction = WRITE_REGISTER,
971 .flags = FLAG_PRESERVE,
972 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->setaddrtable),
973 .value = 1,
974 .mask = 0xffffffff
975 }
976 };
977
978 einj_smi->err_inj_cap = ACPI_EINJ_DEFAULT_CAP;
979 einj_smi->trigger_action_table = (u64) (uintptr_t)tat;
980
981 for (i = 0; i < ACTION_COUNT; i++)
982 printk(BIOS_DEBUG, "default_actions[%d].reg.addr is %llx\n", i,
983 default_actions[i].reg.addr);
984
Rocky Phagurab46a9e52021-05-21 13:34:03 -0700985 memset((void *)einj, 0, sizeof(*einj));
Rocky Phaguraeff07132021-01-10 15:42:50 -0800986
987 /* Fill out header fields. */
988 memcpy(header->signature, "EINJ", 4);
989 memcpy(header->oem_id, OEM_ID, 6);
990 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
991 memcpy(header->asl_compiler_id, ASLC, 4);
992
993 header->asl_compiler_revision = asl_revision;
994 header->length = sizeof(acpi_einj_t);
995 header->revision = 1;
996 inj_header->einj_header_size = sizeof(acpi_injection_header_t);
997 inj_header->entry_count = ACTION_COUNT;
998
999 printk(BIOS_DEBUG, "%s einj->action_table = %p\n",
1000 __func__, einj->action_table);
1001 memcpy((void *)einj->action_table, (void *)default_actions, sizeof(einj->action_table));
Rocky Phagurab46a9e52021-05-21 13:34:03 -07001002 header->checksum = acpi_checksum((void *)einj, sizeof(*einj));
Rocky Phaguraeff07132021-01-10 15:42:50 -08001003}
1004
Furquan Shaikh0f007d82020-04-24 06:41:18 -07001005void acpi_create_vfct(const struct device *device,
Himanshu Sahdev7f1da072019-10-21 15:27:19 +05301006 acpi_vfct_t *vfct,
Furquan Shaikh0f007d82020-04-24 06:41:18 -07001007 unsigned long (*acpi_fill_vfct)(const struct device *device,
Himanshu Sahdev7f1da072019-10-21 15:27:19 +05301008 acpi_vfct_t *vfct_struct, unsigned long current))
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +02001009{
1010 acpi_header_t *header = &(vfct->header);
Himanshu Sahdev7f1da072019-10-21 15:27:19 +05301011 unsigned long current = (unsigned long)vfct + sizeof(acpi_vfct_t);
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +02001012
Himanshu Sahdev7f1da072019-10-21 15:27:19 +05301013 memset((void *)vfct, 0, sizeof(acpi_vfct_t));
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +02001014
John Zhao2ba303e2019-05-28 16:48:14 -07001015 if (!header)
1016 return;
1017
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +02001018 /* Fill out header fields. */
1019 memcpy(header->signature, "VFCT", 4);
1020 memcpy(header->oem_id, OEM_ID, 6);
1021 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1022 memcpy(header->asl_compiler_id, ASLC, 4);
1023
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001024 header->asl_compiler_revision = asl_revision;
Marc Jones93a51762018-08-22 18:57:24 -06001025 header->revision = get_acpi_table_revision(VFCT);
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +02001026
1027 current = acpi_fill_vfct(device, vfct, current);
1028
Kyösti Mälkkifb493792019-06-30 06:29:52 +03001029 /* If no BIOS image, return with header->length == 0. */
1030 if (!vfct->VBIOSImageOffset)
1031 return;
1032
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +02001033 /* (Re)calculate length and checksum. */
1034 header->length = current - (unsigned long)vfct;
1035 header->checksum = acpi_checksum((void *)vfct, header->length);
1036}
1037
Furquan Shaikh0f007d82020-04-24 06:41:18 -07001038void acpi_create_ipmi(const struct device *device,
Patrick Rudolph9d98e5a2019-06-14 19:00:04 +02001039 struct acpi_spmi *spmi,
1040 const u16 ipmi_revision,
1041 const acpi_addr_t *addr,
1042 const enum acpi_ipmi_interface_type type,
1043 const s8 gpe_interrupt,
1044 const u32 apic_interrupt,
1045 const u32 uid)
1046{
1047 acpi_header_t *header = &(spmi->header);
1048 memset((void *)spmi, 0, sizeof(struct acpi_spmi));
1049
1050 /* Fill out header fields. */
1051 memcpy(header->signature, "SPMI", 4);
1052 memcpy(header->oem_id, OEM_ID, 6);
1053 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1054 memcpy(header->asl_compiler_id, ASLC, 4);
1055
1056 header->asl_compiler_revision = asl_revision;
1057 header->length = sizeof(struct acpi_spmi);
1058 header->revision = get_acpi_table_revision(SPMI);
1059
1060 spmi->reserved = 1;
1061
1062 if (device->path.type == DEVICE_PATH_PCI) {
1063 spmi->pci_device_flag = ACPI_IPMI_PCI_DEVICE_FLAG;
1064 spmi->pci_bus = device->bus->secondary;
1065 spmi->pci_device = device->path.pci.devfn >> 3;
1066 spmi->pci_function = device->path.pci.devfn & 0x7;
1067 } else if (type != IPMI_INTERFACE_SSIF) {
1068 memcpy(spmi->uid, &uid, sizeof(spmi->uid));
1069 }
1070
1071 spmi->base_address = *addr;
1072 spmi->specification_revision = ipmi_revision;
1073
1074 spmi->interface_type = type;
1075
1076 if (gpe_interrupt >= 0 && gpe_interrupt < 32) {
1077 spmi->gpe = gpe_interrupt;
1078 spmi->interrupt_type |= ACPI_IPMI_INT_TYPE_SCI;
1079 }
1080 if (apic_interrupt > 0) {
1081 spmi->global_system_interrupt = apic_interrupt;
1082 spmi->interrupt_type |= ACPI_IPMI_INT_TYPE_APIC;
1083 }
1084
1085 /* Calculate checksum. */
1086 header->checksum = acpi_checksum((void *)spmi, header->length);
1087}
1088
Timothy Pearsonff8ccf02015-08-11 17:48:32 -05001089void acpi_create_ivrs(acpi_ivrs_t *ivrs,
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001090 unsigned long (*acpi_fill_ivrs)(acpi_ivrs_t *ivrs_struct,
1091 unsigned long current))
Timothy Pearsonff8ccf02015-08-11 17:48:32 -05001092{
1093 acpi_header_t *header = &(ivrs->header);
1094 unsigned long current = (unsigned long)ivrs + sizeof(acpi_ivrs_t);
1095
1096 memset((void *)ivrs, 0, sizeof(acpi_ivrs_t));
1097
John Zhao2ba303e2019-05-28 16:48:14 -07001098 if (!header)
1099 return;
1100
Timothy Pearsonff8ccf02015-08-11 17:48:32 -05001101 /* Fill out header fields. */
1102 memcpy(header->signature, "IVRS", 4);
1103 memcpy(header->oem_id, OEM_ID, 6);
1104 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1105 memcpy(header->asl_compiler_id, ASLC, 4);
1106
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001107 header->asl_compiler_revision = asl_revision;
Timothy Pearsonff8ccf02015-08-11 17:48:32 -05001108 header->length = sizeof(acpi_ivrs_t);
Marc Jones93a51762018-08-22 18:57:24 -06001109 header->revision = get_acpi_table_revision(IVRS);
Timothy Pearsonff8ccf02015-08-11 17:48:32 -05001110
1111 current = acpi_fill_ivrs(ivrs, current);
1112
1113 /* (Re)calculate length and checksum. */
1114 header->length = current - (unsigned long)ivrs;
1115 header->checksum = acpi_checksum((void *)ivrs, header->length);
1116}
1117
Jason Glenesk61624b22020-11-02 20:06:23 -08001118void acpi_create_crat(struct acpi_crat_header *crat,
1119 unsigned long (*acpi_fill_crat)(struct acpi_crat_header *crat_struct,
1120 unsigned long current))
1121{
1122 acpi_header_t *header = &(crat->header);
1123 unsigned long current = (unsigned long)crat + sizeof(struct acpi_crat_header);
1124
1125 memset((void *)crat, 0, sizeof(struct acpi_crat_header));
1126
1127 if (!header)
1128 return;
1129
1130 /* Fill out header fields. */
1131 memcpy(header->signature, "CRAT", 4);
1132 memcpy(header->oem_id, OEM_ID, 6);
1133 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1134 memcpy(header->asl_compiler_id, ASLC, 4);
1135
1136 header->asl_compiler_revision = asl_revision;
1137 header->length = sizeof(struct acpi_crat_header);
1138 header->revision = get_acpi_table_revision(CRAT);
1139
1140 current = acpi_fill_crat(crat, current);
1141
1142 /* (Re)calculate length and checksum. */
1143 header->length = current - (unsigned long)crat;
1144 header->checksum = acpi_checksum((void *)crat, header->length);
1145}
1146
Furquan Shaikh0f007d82020-04-24 06:41:18 -07001147unsigned long acpi_write_hpet(const struct device *device, unsigned long current,
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001148 acpi_rsdp_t *rsdp)
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001149{
1150 acpi_hpet_t *hpet;
1151
1152 /*
1153 * We explicitly add these tables later on:
1154 */
1155 printk(BIOS_DEBUG, "ACPI: * HPET\n");
1156
1157 hpet = (acpi_hpet_t *) current;
1158 current += sizeof(acpi_hpet_t);
Felix Heldc4697122019-06-20 13:50:17 +02001159 current = ALIGN_UP(current, 16);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001160 acpi_create_hpet(hpet);
1161 acpi_add_table(rsdp, hpet);
1162
1163 return current;
1164}
1165
Duncan Lauriee4a36c72017-11-11 19:33:25 -08001166void acpi_create_dbg2(acpi_dbg2_header_t *dbg2,
1167 int port_type, int port_subtype,
1168 acpi_addr_t *address, uint32_t address_size,
1169 const char *device_path)
1170{
1171 uintptr_t current;
1172 acpi_dbg2_device_t *device;
1173 uint32_t *dbg2_addr_size;
1174 acpi_header_t *header;
1175 size_t path_len;
1176 const char *path;
1177 char *namespace;
1178
1179 /* Fill out header fields. */
1180 current = (uintptr_t)dbg2;
1181 memset(dbg2, 0, sizeof(acpi_dbg2_header_t));
1182 header = &(dbg2->header);
John Zhao2ba303e2019-05-28 16:48:14 -07001183
1184 if (!header)
1185 return;
1186
Marc Jones93a51762018-08-22 18:57:24 -06001187 header->revision = get_acpi_table_revision(DBG2);
Duncan Lauriee4a36c72017-11-11 19:33:25 -08001188 memcpy(header->signature, "DBG2", 4);
1189 memcpy(header->oem_id, OEM_ID, 6);
1190 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1191 memcpy(header->asl_compiler_id, ASLC, 4);
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001192 header->asl_compiler_revision = asl_revision;
Duncan Lauriee4a36c72017-11-11 19:33:25 -08001193
1194 /* One debug device defined */
1195 dbg2->devices_offset = sizeof(acpi_dbg2_header_t);
1196 dbg2->devices_count = 1;
1197 current += sizeof(acpi_dbg2_header_t);
1198
1199 /* Device comes after the header */
1200 device = (acpi_dbg2_device_t *)current;
1201 memset(device, 0, sizeof(acpi_dbg2_device_t));
1202 current += sizeof(acpi_dbg2_device_t);
1203
1204 device->revision = 0;
1205 device->address_count = 1;
1206 device->port_type = port_type;
1207 device->port_subtype = port_subtype;
1208
1209 /* Base Address comes after device structure */
1210 memcpy((void *)current, address, sizeof(acpi_addr_t));
1211 device->base_address_offset = current - (uintptr_t)device;
1212 current += sizeof(acpi_addr_t);
1213
1214 /* Address Size comes after address structure */
1215 dbg2_addr_size = (uint32_t *)current;
1216 device->address_size_offset = current - (uintptr_t)device;
1217 *dbg2_addr_size = address_size;
1218 current += sizeof(uint32_t);
1219
1220 /* Namespace string comes last, use '.' if not provided */
1221 path = device_path ? : ".";
1222 /* Namespace string length includes NULL terminator */
1223 path_len = strlen(path) + 1;
1224 namespace = (char *)current;
1225 device->namespace_string_length = path_len;
1226 device->namespace_string_offset = current - (uintptr_t)device;
1227 strncpy(namespace, path, path_len);
1228 current += path_len;
1229
1230 /* Update structure lengths and checksum */
1231 device->length = current - (uintptr_t)device;
1232 header->length = current - (uintptr_t)dbg2;
1233 header->checksum = acpi_checksum((uint8_t *)dbg2, header->length);
1234}
1235
1236unsigned long acpi_write_dbg2_pci_uart(acpi_rsdp_t *rsdp, unsigned long current,
Aamir Bohrae825d3f2019-07-30 10:11:41 +05301237 const struct device *dev, uint8_t access_size)
Duncan Lauriee4a36c72017-11-11 19:33:25 -08001238{
1239 acpi_dbg2_header_t *dbg2 = (acpi_dbg2_header_t *)current;
1240 struct resource *res;
1241 acpi_addr_t address;
1242
1243 if (!dev) {
Wim Vervoorn6cd52432020-02-03 14:41:46 +01001244 printk(BIOS_DEBUG, "%s: Device not found\n", __func__);
Duncan Lauriee4a36c72017-11-11 19:33:25 -08001245 return current;
1246 }
Mario Scheithauerf1eb0ea2017-11-21 13:52:53 +01001247 if (!dev->enabled) {
1248 printk(BIOS_INFO, "%s: Device not enabled\n", __func__);
1249 return current;
1250 }
Angel Pons32d09be2021-11-03 13:27:45 +01001251 res = probe_resource(dev, PCI_BASE_ADDRESS_0);
Duncan Lauriee4a36c72017-11-11 19:33:25 -08001252 if (!res) {
1253 printk(BIOS_ERR, "%s: Unable to find resource for %s\n",
1254 __func__, dev_path(dev));
1255 return current;
1256 }
1257
1258 memset(&address, 0, sizeof(address));
1259 if (res->flags & IORESOURCE_IO)
1260 address.space_id = ACPI_ADDRESS_SPACE_IO;
1261 else if (res->flags & IORESOURCE_MEM)
1262 address.space_id = ACPI_ADDRESS_SPACE_MEMORY;
1263 else {
1264 printk(BIOS_ERR, "%s: Unknown address space type\n", __func__);
1265 return current;
1266 }
1267
1268 address.addrl = (uint32_t)res->base;
1269 address.addrh = (uint32_t)((res->base >> 32) & 0xffffffff);
1270 address.access_size = access_size;
1271
1272 acpi_create_dbg2(dbg2,
1273 ACPI_DBG2_PORT_SERIAL,
1274 ACPI_DBG2_PORT_SERIAL_16550,
1275 &address, res->size,
1276 acpi_device_path(dev));
1277
1278 if (dbg2->header.length) {
1279 current += dbg2->header.length;
1280 current = acpi_align_current(current);
1281 acpi_add_table(rsdp, dbg2);
1282 }
1283
1284 return current;
1285}
1286
Stefan Reinauer777224c2005-01-19 14:06:41 +00001287void acpi_create_facs(acpi_facs_t *facs)
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +00001288{
Uwe Hermann622824c2010-11-19 15:14:42 +00001289 memset((void *)facs, 0, sizeof(acpi_facs_t));
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +00001290
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001291 memcpy(facs->signature, "FACS", 4);
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +00001292 facs->length = sizeof(acpi_facs_t);
1293 facs->hardware_signature = 0;
1294 facs->firmware_waking_vector = 0;
1295 facs->global_lock = 0;
1296 facs->flags = 0;
1297 facs->x_firmware_waking_vector_l = 0;
1298 facs->x_firmware_waking_vector_h = 0;
Marc Jones93a51762018-08-22 18:57:24 -06001299 facs->version = get_acpi_table_revision(FACS);
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +00001300}
Stefan Reinauer777224c2005-01-19 14:06:41 +00001301
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001302static void acpi_write_rsdt(acpi_rsdt_t *rsdt, char *oem_id, char *oem_table_id)
Stefan Reinauer14e22772010-04-27 06:56:47 +00001303{
Uwe Hermann622824c2010-11-19 15:14:42 +00001304 acpi_header_t *header = &(rsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001305
John Zhao2ba303e2019-05-28 16:48:14 -07001306 if (!header)
1307 return;
1308
Uwe Hermann622824c2010-11-19 15:14:42 +00001309 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001310 memcpy(header->signature, "RSDT", 4);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001311 memcpy(header->oem_id, oem_id, 6);
1312 memcpy(header->oem_table_id, oem_table_id, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +00001313 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001314
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001315 header->asl_compiler_revision = asl_revision;
Stefan Reinauer688b3852004-01-28 16:56:14 +00001316 header->length = sizeof(acpi_rsdt_t);
Marc Jones93a51762018-08-22 18:57:24 -06001317 header->revision = get_acpi_table_revision(RSDT);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001318
Uwe Hermann622824c2010-11-19 15:14:42 +00001319 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauer688b3852004-01-28 16:56:14 +00001320
Uwe Hermann622824c2010-11-19 15:14:42 +00001321 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001322 header->checksum = acpi_checksum((void *)rsdt, sizeof(acpi_rsdt_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +00001323}
1324
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001325static void acpi_write_xsdt(acpi_xsdt_t *xsdt, char *oem_id, char *oem_table_id)
Stefan Reinauer14e22772010-04-27 06:56:47 +00001326{
Uwe Hermann622824c2010-11-19 15:14:42 +00001327 acpi_header_t *header = &(xsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001328
John Zhao2ba303e2019-05-28 16:48:14 -07001329 if (!header)
1330 return;
1331
Uwe Hermann622824c2010-11-19 15:14:42 +00001332 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001333 memcpy(header->signature, "XSDT", 4);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001334 memcpy(header->oem_id, oem_id, 6);
1335 memcpy(header->oem_table_id, oem_table_id, 8);
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001336 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001337
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001338 header->asl_compiler_revision = asl_revision;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001339 header->length = sizeof(acpi_xsdt_t);
Marc Jones93a51762018-08-22 18:57:24 -06001340 header->revision = get_acpi_table_revision(XSDT);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001341
Uwe Hermann622824c2010-11-19 15:14:42 +00001342 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001343
Uwe Hermann622824c2010-11-19 15:14:42 +00001344 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001345 header->checksum = acpi_checksum((void *)xsdt, sizeof(acpi_xsdt_t));
1346}
1347
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001348static void acpi_write_rsdp(acpi_rsdp_t *rsdp, acpi_rsdt_t *rsdt,
1349 acpi_xsdt_t *xsdt, char *oem_id)
Stefan Reinauer688b3852004-01-28 16:56:14 +00001350{
Stefan Reinauerd18faac2009-11-05 18:06:43 +00001351 memset(rsdp, 0, sizeof(acpi_rsdp_t));
Uwe Hermann622824c2010-11-19 15:14:42 +00001352
Stefan Reinauer688b3852004-01-28 16:56:14 +00001353 memcpy(rsdp->signature, RSDP_SIG, 8);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001354 memcpy(rsdp->oem_id, oem_id, 6);
Uwe Hermann622824c2010-11-19 15:14:42 +00001355
1356 rsdp->length = sizeof(acpi_rsdp_t);
Stefan Reinauerdefee172015-06-18 01:11:19 -07001357 rsdp->rsdt_address = (uintptr_t)rsdt;
Uwe Hermann622824c2010-11-19 15:14:42 +00001358
1359 /*
1360 * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2.
1361 *
1362 * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
1363 * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
1364 * revision 0).
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001365 */
1366 if (xsdt == NULL) {
Uwe Hermann622824c2010-11-19 15:14:42 +00001367 rsdp->revision = 0;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001368 } else {
Stefan Reinauerdefee172015-06-18 01:11:19 -07001369 rsdp->xsdt_address = (u64)(uintptr_t)xsdt;
Marc Jones93a51762018-08-22 18:57:24 -06001370 rsdp->revision = get_acpi_table_revision(RSDP);
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001371 }
Uwe Hermann622824c2010-11-19 15:14:42 +00001372
1373 /* Calculate checksums. */
1374 rsdp->checksum = acpi_checksum((void *)rsdp, 20);
1375 rsdp->ext_checksum = acpi_checksum((void *)rsdp, sizeof(acpi_rsdp_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +00001376}
1377
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001378unsigned long acpi_create_hest_error_source(acpi_hest_t *hest,
1379 acpi_hest_esd_t *esd, u16 type, void *data, u16 data_len)
zbaocaf494c82012-04-13 13:57:14 +08001380{
1381 acpi_header_t *header = &(hest->header);
1382 acpi_hest_hen_t *hen;
1383 void *pos;
1384 u16 len;
1385
1386 pos = esd;
1387 memset(pos, 0, sizeof(acpi_hest_esd_t));
1388 len = 0;
1389 esd->type = type; /* MCE */
1390 esd->source_id = hest->error_source_count;
1391 esd->flags = 0; /* FIRMWARE_FIRST */
1392 esd->enabled = 1;
1393 esd->prealloc_erecords = 1;
1394 esd->max_section_per_record = 0x1;
1395
1396 len += sizeof(acpi_hest_esd_t);
1397 pos = esd + 1;
1398
1399 switch (type) {
1400 case 0: /* MCE */
1401 break;
1402 case 1: /* CMC */
1403 hen = (acpi_hest_hen_t *) (pos);
1404 memset(pos, 0, sizeof(acpi_hest_hen_t));
1405 hen->type = 3; /* SCI? */
1406 hen->length = sizeof(acpi_hest_hen_t);
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001407 hen->conf_we = 0; /* Configuration Write Enable. */
zbaocaf494c82012-04-13 13:57:14 +08001408 hen->poll_interval = 0;
1409 hen->vector = 0;
1410 hen->sw2poll_threshold_val = 0;
1411 hen->sw2poll_threshold_win = 0;
1412 hen->error_threshold_val = 0;
1413 hen->error_threshold_win = 0;
1414 len += sizeof(acpi_hest_hen_t);
1415 pos = hen + 1;
1416 break;
1417 case 2: /* NMI */
1418 case 6: /* AER Root Port */
1419 case 7: /* AER Endpoint */
1420 case 8: /* AER Bridge */
1421 case 9: /* Generic Hardware Error Source. */
1422 /* TODO: */
1423 break;
1424 default:
1425 printk(BIOS_DEBUG, "Invalid type of Error Source.");
1426 break;
1427 }
Lee Leahy024b13d2017-03-16 13:41:11 -07001428 hest->error_source_count++;
zbaocaf494c82012-04-13 13:57:14 +08001429
1430 memcpy(pos, data, data_len);
1431 len += data_len;
John Zhao2ba303e2019-05-28 16:48:14 -07001432 if (header)
1433 header->length += len;
zbaocaf494c82012-04-13 13:57:14 +08001434
1435 return len;
1436}
1437
1438/* ACPI 4.0 */
Vladimir Serbinenko807127f2014-11-09 13:36:18 +01001439void acpi_write_hest(acpi_hest_t *hest,
1440 unsigned long (*acpi_fill_hest)(acpi_hest_t *hest))
zbaocaf494c82012-04-13 13:57:14 +08001441{
1442 acpi_header_t *header = &(hest->header);
1443
1444 memset(hest, 0, sizeof(acpi_hest_t));
1445
John Zhao2ba303e2019-05-28 16:48:14 -07001446 if (!header)
1447 return;
1448
zbaocaf494c82012-04-13 13:57:14 +08001449 memcpy(header->signature, "HEST", 4);
1450 memcpy(header->oem_id, OEM_ID, 6);
1451 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1452 memcpy(header->asl_compiler_id, ASLC, 4);
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001453 header->asl_compiler_revision = asl_revision;
zbaocaf494c82012-04-13 13:57:14 +08001454 header->length += sizeof(acpi_hest_t);
Marc Jones93a51762018-08-22 18:57:24 -06001455 header->revision = get_acpi_table_revision(HEST);
zbaocaf494c82012-04-13 13:57:14 +08001456
1457 acpi_fill_hest(hest);
1458
1459 /* Calculate checksums. */
1460 header->checksum = acpi_checksum((void *)hest, header->length);
1461}
1462
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001463/* ACPI 3.0b */
1464void acpi_write_bert(acpi_bert_t *bert, uintptr_t region, size_t length)
1465{
1466 acpi_header_t *header = &(bert->header);
1467
1468 memset(bert, 0, sizeof(acpi_bert_t));
1469
John Zhao2ba303e2019-05-28 16:48:14 -07001470 if (!header)
1471 return;
1472
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001473 memcpy(header->signature, "BERT", 4);
1474 memcpy(header->oem_id, OEM_ID, 6);
1475 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1476 memcpy(header->asl_compiler_id, ASLC, 4);
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001477 header->asl_compiler_revision = asl_revision;
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001478 header->length += sizeof(acpi_bert_t);
1479 header->revision = get_acpi_table_revision(BERT);
1480
1481 bert->error_region = region;
1482 bert->region_length = length;
1483
1484 /* Calculate checksums. */
1485 header->checksum = acpi_checksum((void *)bert, header->length);
1486}
1487
Angel Pons79572e42020-07-13 00:17:43 +02001488__weak void arch_fill_fadt(acpi_fadt_t *fadt) { }
Kyösti Mälkkif9aac922020-05-30 16:16:28 +03001489__weak void soc_fill_fadt(acpi_fadt_t *fadt) { }
Kyösti Mälkki02fd15d2020-06-02 03:34:43 +03001490__weak void mainboard_fill_fadt(acpi_fadt_t *fadt) { }
Kyösti Mälkkif9aac922020-05-30 16:16:28 +03001491
Lee Leahy024b13d2017-03-16 13:41:11 -07001492void acpi_create_fadt(acpi_fadt_t *fadt, acpi_facs_t *facs, void *dsdt)
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001493{
1494 acpi_header_t *header = &(fadt->header);
1495
1496 memset((void *) fadt, 0, sizeof(acpi_fadt_t));
John Zhao2ba303e2019-05-28 16:48:14 -07001497
1498 if (!header)
1499 return;
1500
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001501 memcpy(header->signature, "FACP", 4);
1502 header->length = sizeof(acpi_fadt_t);
Marc Jones93a51762018-08-22 18:57:24 -06001503 header->revision = get_acpi_table_revision(FADT);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001504 memcpy(header->oem_id, OEM_ID, 6);
1505 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1506 memcpy(header->asl_compiler_id, ASLC, 4);
Elyes HAOUAS26071aa2019-02-15 08:21:33 +01001507 header->asl_compiler_revision = asl_revision;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001508
1509 fadt->firmware_ctrl = (unsigned long) facs;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001510 fadt->x_firmware_ctl_l = (unsigned long)facs;
1511 fadt->x_firmware_ctl_h = 0;
Kyösti Mälkkid4acee82020-06-03 07:20:55 +03001512
1513 fadt->dsdt = (unsigned long) dsdt;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001514 fadt->x_dsdt_l = (unsigned long)dsdt;
1515 fadt->x_dsdt_h = 0;
1516
Kyösti Mälkkid4acee82020-06-03 07:20:55 +03001517 /* should be 0 ACPI 3.0 */
1518 fadt->reserved = 0;
1519
Kyösti Mälkkie0d38682020-06-07 12:01:58 +03001520 fadt->preferred_pm_profile = acpi_get_preferred_pm_profile();
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001521
Nico Huberd5811372021-07-12 18:11:58 +02001522 if (CONFIG(USE_PC_CMOS_ALTCENTURY))
1523 fadt->century = RTC_CLK_ALTCENTURY;
1524
Angel Pons79572e42020-07-13 00:17:43 +02001525 arch_fill_fadt(fadt);
1526
Vladimir Serbinenkoab83ef02014-10-25 15:18:25 +02001527 acpi_fill_fadt(fadt);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001528
Kyösti Mälkkif9aac922020-05-30 16:16:28 +03001529 soc_fill_fadt(fadt);
Kyösti Mälkki02fd15d2020-06-02 03:34:43 +03001530 mainboard_fill_fadt(fadt);
Kyösti Mälkkif9aac922020-05-30 16:16:28 +03001531
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001532 header->checksum =
1533 acpi_checksum((void *) fadt, header->length);
1534}
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001535
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001536void acpi_create_lpit(acpi_lpit_t *lpit)
1537{
1538 acpi_header_t *header = &(lpit->header);
1539 unsigned long current = (unsigned long)lpit + sizeof(acpi_lpit_t);
1540
1541 memset((void *)lpit, 0, sizeof(acpi_lpit_t));
1542
1543 if (!header)
1544 return;
1545
1546 /* Fill out header fields. */
1547 memcpy(header->signature, "LPIT", 4);
1548 memcpy(header->oem_id, OEM_ID, 6);
1549 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1550 memcpy(header->asl_compiler_id, ASLC, 4);
1551
1552 header->asl_compiler_revision = asl_revision;
1553 header->revision = get_acpi_table_revision(LPIT);
1554 header->oem_revision = 42;
1555 header->length = sizeof(acpi_lpit_t);
1556
1557 current = acpi_fill_lpit(current);
1558
1559 /* (Re)calculate length and checksum. */
1560 header->length = current - (unsigned long)lpit;
1561 header->checksum = acpi_checksum((void *)lpit, header->length);
1562}
1563
1564unsigned long acpi_create_lpi_desc_ncst(acpi_lpi_desc_ncst_t *lpi_desc, uint16_t uid)
1565{
1566 memset(lpi_desc, 0, sizeof(acpi_lpi_desc_ncst_t));
1567 lpi_desc->header.length = sizeof(acpi_lpi_desc_ncst_t);
1568 lpi_desc->header.type = ACPI_LPI_DESC_TYPE_NATIVE_CSTATE;
1569 lpi_desc->header.uid = uid;
1570
1571 return lpi_desc->header.length;
1572}
1573
Aaron Durbin64031672018-04-21 14:45:32 -06001574unsigned long __weak fw_cfg_acpi_tables(unsigned long start)
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001575{
1576 return 0;
1577}
1578
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001579unsigned long write_acpi_tables(unsigned long start)
1580{
1581 unsigned long current;
1582 acpi_rsdp_t *rsdp;
1583 acpi_rsdt_t *rsdt;
1584 acpi_xsdt_t *xsdt;
1585 acpi_fadt_t *fadt;
1586 acpi_facs_t *facs;
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001587 acpi_header_t *slic_file, *slic;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001588 acpi_header_t *ssdt;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001589 acpi_header_t *dsdt_file, *dsdt;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001590 acpi_mcfg_t *mcfg;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +02001591 acpi_tcpa_t *tcpa;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001592 acpi_tpm2_t *tpm2;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001593 acpi_madt_t *madt;
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001594 acpi_lpit_t *lpit;
Francois Toguo522e0db2021-01-21 09:55:19 -08001595 acpi_bert_t *bert;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +11001596 struct device *dev;
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001597 unsigned long fw;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001598 size_t slic_size, dsdt_size;
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001599 char oem_id[6], oem_table_id[8];
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001600
1601 current = start;
1602
1603 /* Align ACPI tables to 16byte */
Aaron Durbin07a1b282015-12-10 17:07:38 -06001604 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001605
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001606 /* Special case for qemu */
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001607 fw = fw_cfg_acpi_tables(current);
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001608 if (fw) {
1609 rsdp = NULL;
1610 /* Find RSDP. */
1611 for (void *p = (void *)current; p < (void *)fw; p += 16) {
1612 if (valid_rsdp((acpi_rsdp_t *)p)) {
1613 rsdp = p;
1614 break;
1615 }
1616 }
1617 if (!rsdp)
1618 return fw;
1619
1620 /* Add BOOT0000 for Linux google firmware driver */
1621 printk(BIOS_DEBUG, "ACPI: * SSDT\n");
1622 ssdt = (acpi_header_t *)fw;
1623 current = (unsigned long)ssdt + sizeof(acpi_header_t);
1624
1625 memset((void *)ssdt, 0, sizeof(acpi_header_t));
1626
1627 memcpy(&ssdt->signature, "SSDT", 4);
1628 ssdt->revision = get_acpi_table_revision(SSDT);
1629 memcpy(&ssdt->oem_id, OEM_ID, 6);
1630 memcpy(&ssdt->oem_table_id, oem_table_id, 8);
1631 ssdt->oem_revision = 42;
1632 memcpy(&ssdt->asl_compiler_id, ASLC, 4);
1633 ssdt->asl_compiler_revision = asl_revision;
1634 ssdt->length = sizeof(acpi_header_t);
1635
1636 acpigen_set_current((char *) current);
1637
1638 /* Write object to declare coreboot tables */
1639 acpi_ssdt_write_cbtable();
1640
1641 /* (Re)calculate length and checksum. */
1642 ssdt->length = current - (unsigned long)ssdt;
1643 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
1644
1645 acpi_create_ssdt_generator(ssdt, ACPI_TABLE_CREATOR);
1646
1647 acpi_add_table(rsdp, ssdt);
1648
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001649 return fw;
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001650 }
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001651
Julius Werner834b3ec2020-03-04 16:52:08 -08001652 dsdt_file = cbfs_map(CONFIG_CBFS_PREFIX "/dsdt.aml", &dsdt_size);
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001653 if (!dsdt_file) {
1654 printk(BIOS_ERR, "No DSDT file, skipping ACPI tables\n");
1655 return current;
1656 }
1657
1658 if (dsdt_file->length > dsdt_size
Elyes HAOUAS7d87e762016-10-03 22:11:07 +02001659 || dsdt_file->length < sizeof(acpi_header_t)
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001660 || memcmp(dsdt_file->signature, "DSDT", 4) != 0) {
1661 printk(BIOS_ERR, "Invalid DSDT file, skipping ACPI tables\n");
1662 return current;
1663 }
1664
Julius Werner834b3ec2020-03-04 16:52:08 -08001665 slic_file = cbfs_map(CONFIG_CBFS_PREFIX "/slic", &slic_size);
Vladimir Serbinenko2cb29782015-05-31 11:32:09 +02001666 if (slic_file
1667 && (slic_file->length > slic_size
Elyes HAOUAS7d87e762016-10-03 22:11:07 +02001668 || slic_file->length < sizeof(acpi_header_t)
Benjamin Doron2b2dc0c2020-09-12 02:55:57 +00001669 || (memcmp(slic_file->signature, "SLIC", 4) != 0
1670 && memcmp(slic_file->signature, "MSDM", 4) != 0))) {
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001671 slic_file = 0;
1672 }
1673
1674 if (slic_file) {
1675 memcpy(oem_id, slic_file->oem_id, 6);
1676 memcpy(oem_table_id, slic_file->oem_table_id, 8);
1677 } else {
1678 memcpy(oem_id, OEM_ID, 6);
1679 memcpy(oem_table_id, ACPI_TABLE_CREATOR, 8);
1680 }
1681
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001682 printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx.\n", start);
1683
1684 /* We need at least an RSDP and an RSDT Table */
1685 rsdp = (acpi_rsdp_t *) current;
1686 current += sizeof(acpi_rsdp_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001687 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001688 rsdt = (acpi_rsdt_t *) current;
1689 current += sizeof(acpi_rsdt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001690 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001691 xsdt = (acpi_xsdt_t *) current;
1692 current += sizeof(acpi_xsdt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001693 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001694
1695 /* clear all table memory */
1696 memset((void *) start, 0, current - start);
1697
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001698 acpi_write_rsdp(rsdp, rsdt, xsdt, oem_id);
1699 acpi_write_rsdt(rsdt, oem_id, oem_table_id);
1700 acpi_write_xsdt(xsdt, oem_id, oem_table_id);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001701
1702 printk(BIOS_DEBUG, "ACPI: * FACS\n");
Felix Heldc4697122019-06-20 13:50:17 +02001703 current = ALIGN_UP(current, 64);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001704 facs = (acpi_facs_t *) current;
1705 current += sizeof(acpi_facs_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001706 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001707 acpi_create_facs(facs);
1708
1709 printk(BIOS_DEBUG, "ACPI: * DSDT\n");
1710 dsdt = (acpi_header_t *) current;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001711 memcpy(dsdt, dsdt_file, sizeof(acpi_header_t));
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001712 if (dsdt->length >= sizeof(acpi_header_t)) {
1713 current += sizeof(acpi_header_t);
1714
1715 acpigen_set_current((char *) current);
Kyösti Mälkki2ab4a962020-06-30 11:41:47 +03001716
Kyösti Mälkki37eb24b2021-01-11 18:40:37 +02001717 if (CONFIG(ACPI_SOC_NVS))
1718 acpi_fill_gnvs();
Kyösti Mälkki3dc17922021-03-16 19:01:48 +02001719 if (CONFIG(CHROMEOS_NVS))
1720 acpi_fill_cnvs();
Kyösti Mälkki2ab4a962020-06-30 11:41:47 +03001721
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001722 for (dev = all_devices; dev; dev = dev->next)
Nico Huber68680dd2020-03-31 17:34:52 +02001723 if (dev->ops && dev->ops->acpi_inject_dsdt)
1724 dev->ops->acpi_inject_dsdt(dev);
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001725 current = (unsigned long) acpigen_get_current();
1726 memcpy((char *)current,
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001727 (char *)dsdt_file + sizeof(acpi_header_t),
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001728 dsdt->length - sizeof(acpi_header_t));
1729 current += dsdt->length - sizeof(acpi_header_t);
1730
1731 /* (Re)calculate length and checksum. */
1732 dsdt->length = current - (unsigned long)dsdt;
1733 dsdt->checksum = 0;
1734 dsdt->checksum = acpi_checksum((void *)dsdt, dsdt->length);
1735 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001736
Aaron Durbin07a1b282015-12-10 17:07:38 -06001737 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001738
1739 printk(BIOS_DEBUG, "ACPI: * FADT\n");
1740 fadt = (acpi_fadt_t *) current;
1741 current += sizeof(acpi_fadt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001742 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001743
1744 acpi_create_fadt(fadt, facs, dsdt);
1745 acpi_add_table(rsdp, fadt);
1746
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001747 if (slic_file) {
1748 printk(BIOS_DEBUG, "ACPI: * SLIC\n");
1749 slic = (acpi_header_t *)current;
1750 memcpy(slic, slic_file, slic_file->length);
1751 current += slic_file->length;
Aaron Durbin07a1b282015-12-10 17:07:38 -06001752 current = acpi_align_current(current);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001753 acpi_add_table(rsdp, slic);
1754 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001755
1756 printk(BIOS_DEBUG, "ACPI: * SSDT\n");
1757 ssdt = (acpi_header_t *)current;
1758 acpi_create_ssdt_generator(ssdt, ACPI_TABLE_CREATOR);
Vladimir Serbinenko9310df82014-10-10 20:40:41 +02001759 if (ssdt->length > sizeof(acpi_header_t)) {
1760 current += ssdt->length;
1761 acpi_add_table(rsdp, ssdt);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001762 current = acpi_align_current(current);
Vladimir Serbinenko9310df82014-10-10 20:40:41 +02001763 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001764
1765 printk(BIOS_DEBUG, "ACPI: * MCFG\n");
1766 mcfg = (acpi_mcfg_t *) current;
1767 acpi_create_mcfg(mcfg);
1768 if (mcfg->header.length > sizeof(acpi_mcfg_t)) {
1769 current += mcfg->header.length;
Aaron Durbin07a1b282015-12-10 17:07:38 -06001770 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001771 acpi_add_table(rsdp, mcfg);
1772 }
1773
Julius Wernercd49cce2019-03-05 16:53:33 -08001774 if (CONFIG(TPM1)) {
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001775 printk(BIOS_DEBUG, "ACPI: * TCPA\n");
1776 tcpa = (acpi_tcpa_t *) current;
1777 acpi_create_tcpa(tcpa);
1778 if (tcpa->header.length >= sizeof(acpi_tcpa_t)) {
1779 current += tcpa->header.length;
1780 current = acpi_align_current(current);
1781 acpi_add_table(rsdp, tcpa);
1782 }
1783 }
1784
Julius Wernercd49cce2019-03-05 16:53:33 -08001785 if (CONFIG(TPM2)) {
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001786 printk(BIOS_DEBUG, "ACPI: * TPM2\n");
1787 tpm2 = (acpi_tpm2_t *) current;
1788 acpi_create_tpm2(tpm2);
1789 if (tpm2->header.length >= sizeof(acpi_tpm2_t)) {
1790 current += tpm2->header.length;
1791 current = acpi_align_current(current);
1792 acpi_add_table(rsdp, tpm2);
1793 }
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +02001794 }
1795
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001796 if (CONFIG(ACPI_LPIT)) {
1797 printk(BIOS_DEBUG, "ACPI: * LPIT\n");
1798
1799 lpit = (acpi_lpit_t *)current;
1800 acpi_create_lpit(lpit);
1801 if (lpit->header.length >= sizeof(acpi_lpit_t)) {
1802 current += lpit->header.length;
1803 current = acpi_align_current(current);
1804 acpi_add_table(rsdp, lpit);
1805 }
1806 }
1807
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001808 printk(BIOS_DEBUG, "ACPI: * MADT\n");
1809
1810 madt = (acpi_madt_t *) current;
1811 acpi_create_madt(madt);
1812 if (madt->header.length > sizeof(acpi_madt_t)) {
Lee Leahy024b13d2017-03-16 13:41:11 -07001813 current += madt->header.length;
1814 acpi_add_table(rsdp, madt);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001815 }
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001816
Aaron Durbin07a1b282015-12-10 17:07:38 -06001817 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001818
Felix Heldfba479262021-05-28 16:11:43 +02001819 if (CONFIG(ACPI_BERT)) {
Francois Toguo522e0db2021-01-21 09:55:19 -08001820 void *region;
1821 size_t size;
Francois Toguo522e0db2021-01-21 09:55:19 -08001822 bert = (acpi_bert_t *) current;
Felix Heldfba479262021-05-28 16:11:43 +02001823 if (acpi_soc_get_bert_region(&region, &size) == CB_SUCCESS) {
1824 printk(BIOS_DEBUG, "ACPI: * BERT\n");
1825 acpi_write_bert(bert, (uintptr_t)region, size);
1826 if (bert->header.length >= sizeof(acpi_bert_t)) {
1827 current += bert->header.length;
1828 acpi_add_table(rsdp, bert);
1829 }
1830 current = acpi_align_current(current);
Francois Toguo522e0db2021-01-21 09:55:19 -08001831 }
Francois Toguo522e0db2021-01-21 09:55:19 -08001832 }
1833
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001834 printk(BIOS_DEBUG, "current = %lx\n", current);
1835
1836 for (dev = all_devices; dev; dev = dev->next) {
1837 if (dev->ops && dev->ops->write_acpi_tables) {
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001838 current = dev->ops->write_acpi_tables(dev, current,
1839 rsdp);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001840 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001841 }
1842 }
1843
1844 printk(BIOS_INFO, "ACPI: done.\n");
1845 return current;
1846}
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001847
Rudolf Marek33cafe52009-04-13 18:07:02 +00001848static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp)
1849{
1850 if (strncmp((char *)rsdp, RSDP_SIG, sizeof(RSDP_SIG) - 1) != 0)
1851 return NULL;
1852
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001853 printk(BIOS_DEBUG, "Looking on %p for valid checksum\n", rsdp);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001854
1855 if (acpi_checksum((void *)rsdp, 20) != 0)
1856 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001857 printk(BIOS_DEBUG, "Checksum 1 passed\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001858
1859 if ((rsdp->revision > 1) && (acpi_checksum((void *)rsdp,
1860 rsdp->length) != 0))
1861 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001862 printk(BIOS_DEBUG, "Checksum 2 passed all OK\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001863
1864 return rsdp;
1865}
1866
Rudolf Marek33cafe52009-04-13 18:07:02 +00001867void *acpi_find_wakeup_vector(void)
1868{
1869 char *p, *end;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001870 acpi_rsdt_t *rsdt;
1871 acpi_facs_t *facs;
Martin Rothc5f49262012-12-14 19:17:55 -07001872 acpi_fadt_t *fadt = NULL;
Kyösti Mälkki072d436b2016-06-17 08:44:40 +03001873 acpi_rsdp_t *rsdp = NULL;
Uwe Hermann622824c2010-11-19 15:14:42 +00001874 void *wake_vec;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001875 int i;
1876
Kyösti Mälkkib8c7ea02020-11-17 16:41:38 +02001877 if (!acpi_is_wakeup_s3())
Rudolf Marek33cafe52009-04-13 18:07:02 +00001878 return NULL;
1879
Uwe Hermann622824c2010-11-19 15:14:42 +00001880 printk(BIOS_DEBUG, "Trying to find the wakeup vector...\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001881
Uwe Hermann622824c2010-11-19 15:14:42 +00001882 /* Find RSDP. */
1883 for (p = (char *)0xe0000; p < (char *)0xfffff; p += 16) {
Lee Leahy0b5678f2017-03-16 16:01:40 -07001884 rsdp = valid_rsdp((acpi_rsdp_t *)p);
1885 if (rsdp)
Rudolf Marek33cafe52009-04-13 18:07:02 +00001886 break;
1887 }
1888
Angel Pons98a68ad2018-11-04 12:25:25 +01001889 if (rsdp == NULL) {
1890 printk(BIOS_ALERT,
1891 "No RSDP found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001892 return NULL;
Angel Pons98a68ad2018-11-04 12:25:25 +01001893 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001894
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001895 printk(BIOS_DEBUG, "RSDP found at %p\n", rsdp);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001896 rsdt = (acpi_rsdt_t *)(uintptr_t)rsdp->rsdt_address;
Stefan Reinauer14e22772010-04-27 06:56:47 +00001897
Uwe Hermann622824c2010-11-19 15:14:42 +00001898 end = (char *)rsdt + rsdt->header.length;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001899 printk(BIOS_DEBUG, "RSDT found at %p ends at %p\n", rsdt, end);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001900
Uwe Hermann622824c2010-11-19 15:14:42 +00001901 for (i = 0; ((char *)&rsdt->entry[i]) < end; i++) {
Stefan Reinauer71a30182015-07-30 16:28:13 -07001902 fadt = (acpi_fadt_t *)(uintptr_t)rsdt->entry[i];
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001903 if (strncmp((char *)fadt, "FACP", 4) == 0)
Rudolf Marek33cafe52009-04-13 18:07:02 +00001904 break;
1905 fadt = NULL;
1906 }
1907
Angel Pons98a68ad2018-11-04 12:25:25 +01001908 if (fadt == NULL) {
1909 printk(BIOS_ALERT,
1910 "No FADT found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001911 return NULL;
Angel Pons98a68ad2018-11-04 12:25:25 +01001912 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001913
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001914 printk(BIOS_DEBUG, "FADT found at %p\n", fadt);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001915 facs = (acpi_facs_t *)(uintptr_t)fadt->firmware_ctrl;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001916
Stefan Reinauer7e9771c2009-04-22 08:17:38 +00001917 if (facs == NULL) {
Angel Pons98a68ad2018-11-04 12:25:25 +01001918 printk(BIOS_ALERT,
1919 "No FACS found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001920 return NULL;
Stefan Reinauer7e9771c2009-04-22 08:17:38 +00001921 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001922
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001923 printk(BIOS_DEBUG, "FACS found at %p\n", facs);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001924 wake_vec = (void *)(uintptr_t)facs->firmware_waking_vector;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001925 printk(BIOS_DEBUG, "OS waking vector is %p\n", wake_vec);
Uwe Hermann622824c2010-11-19 15:14:42 +00001926
Rudolf Marek33cafe52009-04-13 18:07:02 +00001927 return wake_vec;
1928}
1929
Aaron Durbin64031672018-04-21 14:45:32 -06001930__weak int acpi_get_gpe(int gpe)
Duncan Laurie1f6e6812016-09-19 12:04:19 -07001931{
1932 return -1; /* implemented by SOC */
1933}
Marc Jones93a51762018-08-22 18:57:24 -06001934
1935int get_acpi_table_revision(enum acpi_tables table)
1936{
1937 switch (table) {
1938 case FADT:
Patrick Rudolphc02bda02020-02-28 10:19:41 +01001939 return ACPI_FADT_REV_ACPI_6_0;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001940 case MADT: /* ACPI 3.0: 2, ACPI 4.0/5.0: 3, ACPI 6.2b/6.3: 5 */
Patrick Rudolph56a3ef22020-03-24 17:29:49 +01001941 return 3;
Marc Jones93a51762018-08-22 18:57:24 -06001942 case MCFG:
1943 return 1;
1944 case TCPA:
1945 return 2;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001946 case TPM2:
1947 return 4;
Martin Roth0949e732021-10-01 14:28:22 -06001948 case SSDT: /* ACPI 3.0 up to 6.3: 2 */
Marc Jones93a51762018-08-22 18:57:24 -06001949 return 2;
Martin Roth0949e732021-10-01 14:28:22 -06001950 case SRAT: /* ACPI 2.0: 1, ACPI 3.0: 2, ACPI 4.0 up to 6.3: 3 */
Marc Jones93a51762018-08-22 18:57:24 -06001951 return 1; /* TODO Should probably be upgraded to 2 */
Jonathan Zhang2a4e1f42021-04-01 11:43:37 -07001952 case HMAT: /* ACPI 6.4: 2 */
1953 return 2;
Marc Jones93a51762018-08-22 18:57:24 -06001954 case DMAR:
1955 return 1;
Martin Roth0949e732021-10-01 14:28:22 -06001956 case SLIT: /* ACPI 2.0 up to 6.3: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06001957 return 1;
Patrick Rudolph9d98e5a2019-06-14 19:00:04 +02001958 case SPMI: /* IMPI 2.0 */
1959 return 5;
Marc Jones93a51762018-08-22 18:57:24 -06001960 case HPET: /* Currently 1. Table added in ACPI 2.0. */
1961 return 1;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001962 case VFCT: /* ACPI 2.0/3.0/4.0: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06001963 return 1;
1964 case IVRS:
Jason Glenesk1916f892020-07-24 02:51:30 -07001965 return IVRS_FORMAT_MIXED;
Marc Jones93a51762018-08-22 18:57:24 -06001966 case DBG2:
1967 return 0;
Martin Roth0949e732021-10-01 14:28:22 -06001968 case FACS: /* ACPI 2.0/3.0: 1, ACPI 4.0 up to 6.3: 2 */
Marc Jones93a51762018-08-22 18:57:24 -06001969 return 1;
Martin Roth0949e732021-10-01 14:28:22 -06001970 case RSDT: /* ACPI 1.0 up to 6.3: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06001971 return 1;
Martin Roth0949e732021-10-01 14:28:22 -06001972 case XSDT: /* ACPI 2.0 up to 6.3: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06001973 return 1;
Martin Roth0949e732021-10-01 14:28:22 -06001974 case RSDP: /* ACPI 2.0 up to 6.3: 2 */
Marc Jones93a51762018-08-22 18:57:24 -06001975 return 2;
Rocky Phaguraeff07132021-01-10 15:42:50 -08001976 case EINJ:
1977 return 1;
Marc Jones93a51762018-08-22 18:57:24 -06001978 case HEST:
1979 return 1;
1980 case NHLT:
1981 return 5;
Marshall Dawson44705c62018-09-04 13:47:15 -06001982 case BERT:
1983 return 1;
Jason Glenesk61624b22020-11-02 20:06:23 -08001984 case CRAT:
1985 return 1;
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001986 case LPIT: /* ACPI 5.1 up to 6.3: 0 */
1987 return 0;
Marc Jones93a51762018-08-22 18:57:24 -06001988 default:
1989 return -1;
1990 }
1991 return -1;
1992}