blob: 64af505941c6fd8d42fed57a76f5aadd874c3365 [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>
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000029
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +020030static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp);
31
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000032u8 acpi_checksum(u8 *table, u32 length)
Stefan Reinauer688b3852004-01-28 16:56:14 +000033{
Uwe Hermann622824c2010-11-19 15:14:42 +000034 u8 ret = 0;
Stefan Reinauer688b3852004-01-28 16:56:14 +000035 while (length--) {
36 ret += *table;
37 table++;
38 }
Stefan Reinauera7648c22004-01-29 17:31:34 +000039 return -ret;
Stefan Reinauer688b3852004-01-28 16:56:14 +000040}
41
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000042/**
Uwe Hermann622824c2010-11-19 15:14:42 +000043 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
44 * and checksum.
Stefan Reinauer06feb882004-02-03 16:11:35 +000045 */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000046void acpi_add_table(acpi_rsdp_t *rsdp, void *table)
Stefan Reinauer688b3852004-01-28 16:56:14 +000047{
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000048 int i, entries_num;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000049 acpi_rsdt_t *rsdt;
50 acpi_xsdt_t *xsdt = NULL;
51
Uwe Hermann622824c2010-11-19 15:14:42 +000052 /* The RSDT is mandatory... */
Stefan Reinauerdefee172015-06-18 01:11:19 -070053 rsdt = (acpi_rsdt_t *)(uintptr_t)rsdp->rsdt_address;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000054
Uwe Hermann622824c2010-11-19 15:14:42 +000055 /* ...while the XSDT is not. */
56 if (rsdp->xsdt_address)
Stefan Reinauerdefee172015-06-18 01:11:19 -070057 xsdt = (acpi_xsdt_t *)((uintptr_t)rsdp->xsdt_address);
Stefan Reinauer14e22772010-04-27 06:56:47 +000058
Uwe Hermann622824c2010-11-19 15:14:42 +000059 /* This should always be MAX_ACPI_TABLES. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000060 entries_num = ARRAY_SIZE(rsdt->entry);
Stefan Reinauer14e22772010-04-27 06:56:47 +000061
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000062 for (i = 0; i < entries_num; i++) {
Uwe Hermann622824c2010-11-19 15:14:42 +000063 if (rsdt->entry[i] == 0)
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000064 break;
Stefan Reinauer688b3852004-01-28 16:56:14 +000065 }
66
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000067 if (i >= entries_num) {
Uwe Hermann622824c2010-11-19 15:14:42 +000068 printk(BIOS_ERR, "ACPI: Error: Could not add ACPI table, "
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +030069 "too many tables.\n");
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000070 return;
71 }
72
Uwe Hermann622824c2010-11-19 15:14:42 +000073 /* Add table to the RSDT. */
Stefan Reinauerdefee172015-06-18 01:11:19 -070074 rsdt->entry[i] = (uintptr_t)table;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000075
Uwe Hermann622824c2010-11-19 15:14:42 +000076 /* Fix RSDT length or the kernel will assume invalid entries. */
77 rsdt->header.length = sizeof(acpi_header_t) + (sizeof(u32) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000078
Uwe Hermann622824c2010-11-19 15:14:42 +000079 /* Re-calculate checksum. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000080 rsdt->header.checksum = 0; /* Hope this won't get optimized away */
Uwe Hermann622824c2010-11-19 15:14:42 +000081 rsdt->header.checksum = acpi_checksum((u8 *)rsdt, rsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000082
Uwe Hermann622824c2010-11-19 15:14:42 +000083 /*
84 * And now the same thing for the XSDT. We use the same index as for
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000085 * now we want the XSDT and RSDT to always be in sync in coreboot.
86 */
87 if (xsdt) {
Uwe Hermann622824c2010-11-19 15:14:42 +000088 /* Add table to the XSDT. */
Stefan Reinauerdefee172015-06-18 01:11:19 -070089 xsdt->entry[i] = (u64)(uintptr_t)table;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000090
Uwe Hermann622824c2010-11-19 15:14:42 +000091 /* Fix XSDT length. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000092 xsdt->header.length = sizeof(acpi_header_t) +
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +030093 (sizeof(u64) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000094
Uwe Hermann622824c2010-11-19 15:14:42 +000095 /* Re-calculate checksum. */
96 xsdt->header.checksum = 0;
97 xsdt->header.checksum = acpi_checksum((u8 *)xsdt,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +030098 xsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000099 }
100
Uwe Hermann622824c2010-11-19 15:14:42 +0000101 printk(BIOS_DEBUG, "ACPI: added table %d/%d, length now %d\n",
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300102 i + 1, entries_num, rsdt->header.length);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000103}
104
Uwe Hermann622824c2010-11-19 15:14:42 +0000105int acpi_create_mcfg_mmconfig(acpi_mcfg_mmconfig_t *mmconfig, u32 base,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300106 u16 seg_nr, u8 start, u8 end)
Stefan Reinauer26d431a2009-01-20 19:17:51 +0000107{
Vladimir Serbinenko60fccdc2014-10-05 11:05:16 +0200108 memset(mmconfig, 0, sizeof(*mmconfig));
Rudolf Mareke6409f22007-11-03 12:50:26 +0000109 mmconfig->base_address = base;
110 mmconfig->base_reserved = 0;
111 mmconfig->pci_segment_group_number = seg_nr;
112 mmconfig->start_bus_number = start;
113 mmconfig->end_bus_number = end;
Uwe Hermann622824c2010-11-19 15:14:42 +0000114
115 return sizeof(acpi_mcfg_mmconfig_t);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000116}
117
Stefan Reinauer777224c2005-01-19 14:06:41 +0000118int acpi_create_madt_lapic(acpi_madt_lapic_t *lapic, u8 cpu, u8 apic)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000119{
Himanshu Sahdev8c09b822019-10-21 18:50:50 +0530120 lapic->type = LOCAL_APIC; /* Local APIC structure */
Uwe Hermann622824c2010-11-19 15:14:42 +0000121 lapic->length = sizeof(acpi_madt_lapic_t);
122 lapic->flags = (1 << 0); /* Processor/LAPIC enabled */
123 lapic->processor_id = cpu;
124 lapic->apic_id = apic;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000125
Uwe Hermann622824c2010-11-19 15:14:42 +0000126 return lapic->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000127}
128
Patrick Rudolph56a3ef22020-03-24 17:29:49 +0100129int acpi_create_madt_lx2apic(acpi_madt_lx2apic_t *lapic, u32 cpu, u32 apic)
130{
131 lapic->type = LOCAL_X2APIC; /* Local APIC structure */
132 lapic->reserved = 0;
133 lapic->length = sizeof(acpi_madt_lx2apic_t);
134 lapic->flags = (1 << 0); /* Processor/LAPIC enabled */
135 lapic->processor_id = cpu;
136 lapic->x2apic_id = apic;
137
138 return lapic->length;
139}
140
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000141unsigned long acpi_create_madt_lapics(unsigned long current)
142{
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100143 struct device *cpu;
Werner Zeh423adfb2019-03-06 09:31:02 +0100144 int index, apic_ids[CONFIG_MAX_CPUS] = {0}, num_cpus = 0;
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000145
Uwe Hermann622824c2010-11-19 15:14:42 +0000146 for (cpu = all_devices; cpu; cpu = cpu->next) {
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000147 if ((cpu->path.type != DEVICE_PATH_APIC) ||
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800148 (cpu->bus->dev->path.type != DEVICE_PATH_CPU_CLUSTER)) {
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000149 continue;
150 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000151 if (!cpu->enabled)
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000152 continue;
Werner Zehdb561e62019-02-19 13:39:56 +0100153 if (num_cpus >= ARRAY_SIZE(apic_ids))
154 break;
155 apic_ids[num_cpus++] = cpu->path.apic.apic_id;
156 }
Werner Zehfedb36e2019-03-12 07:07:50 +0100157 if (num_cpus > 1)
158 bubblesort(apic_ids, num_cpus, NUM_ASCENDING);
Werner Zehdb561e62019-02-19 13:39:56 +0100159 for (index = 0; index < num_cpus; index++) {
Patrick Rudolph56a3ef22020-03-24 17:29:49 +0100160 if (apic_ids[index] < 0xff)
161 current += acpi_create_madt_lapic((acpi_madt_lapic_t *)current,
162 index, apic_ids[index]);
163 else
164 current += acpi_create_madt_lx2apic((acpi_madt_lx2apic_t *)current,
165 index, apic_ids[index]);
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000166 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000167
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000168 return current;
169}
170
Uwe Hermann622824c2010-11-19 15:14:42 +0000171int acpi_create_madt_ioapic(acpi_madt_ioapic_t *ioapic, u8 id, u32 addr,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300172 u32 gsi_base)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000173{
Himanshu Sahdev8c09b822019-10-21 18:50:50 +0530174 ioapic->type = IO_APIC; /* I/O APIC structure */
Uwe Hermann622824c2010-11-19 15:14:42 +0000175 ioapic->length = sizeof(acpi_madt_ioapic_t);
176 ioapic->reserved = 0x00;
177 ioapic->gsi_base = gsi_base;
178 ioapic->ioapic_id = id;
179 ioapic->ioapic_addr = addr;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000180
Uwe Hermann622824c2010-11-19 15:14:42 +0000181 return ioapic->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000182}
183
Stefan Reinauer777224c2005-01-19 14:06:41 +0000184int acpi_create_madt_irqoverride(acpi_madt_irqoverride_t *irqoverride,
Stefan Reinauer06feb882004-02-03 16:11:35 +0000185 u8 bus, u8 source, u32 gsirq, u16 flags)
186{
Himanshu Sahdev8c09b822019-10-21 18:50:50 +0530187 irqoverride->type = IRQ_SOURCE_OVERRIDE; /* Interrupt source override */
Uwe Hermann622824c2010-11-19 15:14:42 +0000188 irqoverride->length = sizeof(acpi_madt_irqoverride_t);
189 irqoverride->bus = bus;
190 irqoverride->source = source;
191 irqoverride->gsirq = gsirq;
192 irqoverride->flags = flags;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000193
Uwe Hermann622824c2010-11-19 15:14:42 +0000194 return irqoverride->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000195}
196
Stefan Reinauer777224c2005-01-19 14:06:41 +0000197int acpi_create_madt_lapic_nmi(acpi_madt_lapic_nmi_t *lapic_nmi, u8 cpu,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300198 u16 flags, u8 lint)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000199{
Himanshu Sahdev8c09b822019-10-21 18:50:50 +0530200 lapic_nmi->type = LOCAL_APIC_NMI; /* Local APIC NMI structure */
Uwe Hermann622824c2010-11-19 15:14:42 +0000201 lapic_nmi->length = sizeof(acpi_madt_lapic_nmi_t);
202 lapic_nmi->flags = flags;
203 lapic_nmi->processor_id = cpu;
204 lapic_nmi->lint = lint;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000205
Uwe Hermann622824c2010-11-19 15:14:42 +0000206 return lapic_nmi->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000207}
208
Patrick Rudolph56a3ef22020-03-24 17:29:49 +0100209int acpi_create_madt_lx2apic_nmi(acpi_madt_lx2apic_nmi_t *lapic_nmi, u32 cpu,
210 u16 flags, u8 lint)
211{
212 lapic_nmi->type = LOCAL_X2APIC_NMI; /* Local APIC NMI structure */
213 lapic_nmi->length = sizeof(acpi_madt_lx2apic_nmi_t);
214 lapic_nmi->flags = flags;
215 lapic_nmi->processor_id = cpu;
216 lapic_nmi->lint = lint;
217 lapic_nmi->reserved[0] = 0;
218 lapic_nmi->reserved[1] = 0;
219 lapic_nmi->reserved[2] = 0;
220
221 return lapic_nmi->length;
222}
223
Furquan Shaikhb1859a62020-04-30 21:27:47 -0700224__weak uintptr_t cpu_get_lapic_addr(void)
225{
226 /*
227 * If an architecture does not support LAPIC, this weak implementation returns LAPIC
228 * addr as 0.
229 */
230 return 0;
231}
232
Stefan Reinauer777224c2005-01-19 14:06:41 +0000233void acpi_create_madt(acpi_madt_t *madt)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000234{
Uwe Hermann622824c2010-11-19 15:14:42 +0000235 acpi_header_t *header = &(madt->header);
236 unsigned long current = (unsigned long)madt + sizeof(acpi_madt_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000237
Stefan Reinauer06feb882004-02-03 16:11:35 +0000238 memset((void *)madt, 0, sizeof(acpi_madt_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000239
John Zhao2ba303e2019-05-28 16:48:14 -0700240 if (!header)
241 return;
242
Uwe Hermann622824c2010-11-19 15:14:42 +0000243 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000244 memcpy(header->signature, "APIC", 4);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000245 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000246 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000247 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000248
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100249 header->asl_compiler_revision = asl_revision;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000250 header->length = sizeof(acpi_madt_t);
Marc Jones93a51762018-08-22 18:57:24 -0600251 header->revision = get_acpi_table_revision(MADT);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000252
Furquan Shaikhb1859a62020-04-30 21:27:47 -0700253 madt->lapic_addr = cpu_get_lapic_addr();
Nico Huber9df72e02018-11-24 18:25:50 +0100254 if (CONFIG(ACPI_HAVE_PCAT_8259))
255 madt->flags |= 1;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000256
Stefan Reinauerf622d592005-11-26 16:56:05 +0000257 current = acpi_fill_madt(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000258
Uwe Hermann622824c2010-11-19 15:14:42 +0000259 /* (Re)calculate length and checksum. */
260 header->length = current - (unsigned long)madt;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000261
Uwe Hermann622824c2010-11-19 15:14:42 +0000262 header->checksum = acpi_checksum((void *)madt, header->length);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000263}
264
Uwe Hermann622824c2010-11-19 15:14:42 +0000265/* MCFG is defined in the PCI Firmware Specification 3.0. */
Rudolf Mareke6409f22007-11-03 12:50:26 +0000266void acpi_create_mcfg(acpi_mcfg_t *mcfg)
267{
Uwe Hermann622824c2010-11-19 15:14:42 +0000268 acpi_header_t *header = &(mcfg->header);
269 unsigned long current = (unsigned long)mcfg + sizeof(acpi_mcfg_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000270
Rudolf Mareke6409f22007-11-03 12:50:26 +0000271 memset((void *)mcfg, 0, sizeof(acpi_mcfg_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000272
John Zhao2ba303e2019-05-28 16:48:14 -0700273 if (!header)
274 return;
275
Uwe Hermann622824c2010-11-19 15:14:42 +0000276 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000277 memcpy(header->signature, "MCFG", 4);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000278 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000279 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000280 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000281
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100282 header->asl_compiler_revision = asl_revision;
Rudolf Mareke6409f22007-11-03 12:50:26 +0000283 header->length = sizeof(acpi_mcfg_t);
Marc Jones93a51762018-08-22 18:57:24 -0600284 header->revision = get_acpi_table_revision(MCFG);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000285
286 current = acpi_fill_mcfg(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000287
Uwe Hermann622824c2010-11-19 15:14:42 +0000288 /* (Re)calculate length and checksum. */
289 header->length = current - (unsigned long)mcfg;
290 header->checksum = acpi_checksum((void *)mcfg, header->length);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000291}
292
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200293static void *get_tcpa_log(u32 *size)
294{
295 const struct cbmem_entry *ce;
296 const u32 tcpa_default_log_len = 0x10000;
297 void *lasa;
Philipp Deppenwiese791ba972018-07-30 01:15:20 +0200298 ce = cbmem_entry_find(CBMEM_ID_TCPA_TCG_LOG);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200299 if (ce) {
300 lasa = cbmem_entry_start(ce);
301 *size = cbmem_entry_size(ce);
302 printk(BIOS_DEBUG, "TCPA log found at %p\n", lasa);
303 return lasa;
304 }
Philipp Deppenwiese791ba972018-07-30 01:15:20 +0200305 lasa = cbmem_add(CBMEM_ID_TCPA_TCG_LOG, tcpa_default_log_len);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200306 if (!lasa) {
307 printk(BIOS_ERR, "TCPA log creation failed\n");
308 return NULL;
309 }
310
311 printk(BIOS_DEBUG, "TCPA log created at %p\n", lasa);
Lee Leahy024b13d2017-03-16 13:41:11 -0700312 memset(lasa, 0, tcpa_default_log_len);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200313
314 *size = tcpa_default_log_len;
315 return lasa;
316}
317
318static void acpi_create_tcpa(acpi_tcpa_t *tcpa)
319{
320 acpi_header_t *header = &(tcpa->header);
321 u32 tcpa_log_len;
322 void *lasa;
323
324 memset((void *)tcpa, 0, sizeof(acpi_tcpa_t));
325
326 lasa = get_tcpa_log(&tcpa_log_len);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700327 if (!lasa)
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200328 return;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200329
John Zhao2ba303e2019-05-28 16:48:14 -0700330 if (!header)
331 return;
332
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200333 /* Fill out header fields. */
334 memcpy(header->signature, "TCPA", 4);
335 memcpy(header->oem_id, OEM_ID, 6);
336 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
337 memcpy(header->asl_compiler_id, ASLC, 4);
338
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100339 header->asl_compiler_revision = asl_revision;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200340 header->length = sizeof(acpi_tcpa_t);
Marc Jones93a51762018-08-22 18:57:24 -0600341 header->revision = get_acpi_table_revision(TCPA);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200342
343 tcpa->platform_class = 0;
344 tcpa->laml = tcpa_log_len;
Stefan Reinauerdefee172015-06-18 01:11:19 -0700345 tcpa->lasa = (uintptr_t) lasa;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200346
347 /* Calculate checksum. */
348 header->checksum = acpi_checksum((void *)tcpa, header->length);
349}
350
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100351static void *get_tpm2_log(u32 *size)
352{
353 const struct cbmem_entry *ce;
354 const u32 tpm2_default_log_len = 0x10000;
355 void *lasa;
356 ce = cbmem_entry_find(CBMEM_ID_TPM2_TCG_LOG);
357 if (ce) {
358 lasa = cbmem_entry_start(ce);
359 *size = cbmem_entry_size(ce);
360 printk(BIOS_DEBUG, "TPM2 log found at %p\n", lasa);
361 return lasa;
362 }
363 lasa = cbmem_add(CBMEM_ID_TPM2_TCG_LOG, tpm2_default_log_len);
364 if (!lasa) {
365 printk(BIOS_ERR, "TPM2 log creation failed\n");
366 return NULL;
367 }
368
369 printk(BIOS_DEBUG, "TPM2 log created at %p\n", lasa);
370 memset(lasa, 0, tpm2_default_log_len);
371
372 *size = tpm2_default_log_len;
373 return lasa;
374}
375
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200376static void acpi_create_tpm2(acpi_tpm2_t *tpm2)
377{
378 acpi_header_t *header = &(tpm2->header);
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100379 u32 tpm2_log_len;
380 void *lasa;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200381
382 memset((void *)tpm2, 0, sizeof(acpi_tpm2_t));
383
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100384 /*
385 * Some payloads like SeaBIOS depend on log area to use TPM2.
386 * Get the memory size and address of TPM2 log area or initialize it.
387 */
388 lasa = get_tpm2_log(&tpm2_log_len);
389 if (!lasa)
390 tpm2_log_len = 0;
391
John Zhao2ba303e2019-05-28 16:48:14 -0700392 if (!header)
393 return;
394
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200395 /* Fill out header fields. */
396 memcpy(header->signature, "TPM2", 4);
397 memcpy(header->oem_id, OEM_ID, 6);
398 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
399 memcpy(header->asl_compiler_id, ASLC, 4);
400
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100401 header->asl_compiler_revision = asl_revision;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200402 header->length = sizeof(acpi_tpm2_t);
403 header->revision = get_acpi_table_revision(TPM2);
404
405 /* Hard to detect for coreboot. Just set it to 0 */
406 tpm2->platform_class = 0;
Christian Walter26e0d4c2019-07-14 15:47:23 +0200407 if (CONFIG(CRB_TPM)) {
408 /* Must be set to 7 for CRB Support */
409 tpm2->control_area = CONFIG_CRB_TPM_BASE_ADDRESS + 0x40;
410 tpm2->start_method = 7;
411 } else {
412 /* Must be set to 0 for FIFO interface support */
413 tpm2->control_area = 0;
414 tpm2->start_method = 6;
415 }
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200416 memset(tpm2->msp, 0, sizeof(tpm2->msp));
417
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100418 /* Fill the log area size and start address fields. */
419 tpm2->laml = tpm2_log_len;
420 tpm2->lasa = (uintptr_t) lasa;
421
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200422 /* Calculate checksum. */
423 header->checksum = acpi_checksum((void *)tpm2, header->length);
424}
425
Duncan Laurie37319032016-05-26 12:47:05 -0700426static void acpi_ssdt_write_cbtable(void)
427{
428 const struct cbmem_entry *cbtable;
429 uintptr_t base;
430 uint32_t size;
431
432 cbtable = cbmem_entry_find(CBMEM_ID_CBTABLE);
433 if (!cbtable)
434 return;
435 base = (uintptr_t)cbmem_entry_start(cbtable);
436 size = cbmem_entry_size(cbtable);
437
438 acpigen_write_device("CTBL");
439 acpigen_write_coreboot_hid(COREBOOT_ACPI_ID_CBTABLE);
440 acpigen_write_name_integer("_UID", 0);
David Wu5dff3962018-03-21 16:48:53 +0800441 acpigen_write_STA(ACPI_STATUS_DEVICE_HIDDEN_ON);
Duncan Laurie37319032016-05-26 12:47:05 -0700442 acpigen_write_name("_CRS");
443 acpigen_write_resourcetemplate_header();
444 acpigen_write_mem32fixed(0, base, size);
445 acpigen_write_resourcetemplate_footer();
446 acpigen_pop_len();
447}
448
Myles Watson3fe6b702009-10-09 20:13:43 +0000449void acpi_create_ssdt_generator(acpi_header_t *ssdt, const char *oem_table_id)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000450{
Uwe Hermann622824c2010-11-19 15:14:42 +0000451 unsigned long current = (unsigned long)ssdt + sizeof(acpi_header_t);
452
Rudolf Marek293b5f52009-02-01 18:35:15 +0000453 memset((void *)ssdt, 0, sizeof(acpi_header_t));
Uwe Hermann622824c2010-11-19 15:14:42 +0000454
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000455 memcpy(&ssdt->signature, "SSDT", 4);
Marc Jones93a51762018-08-22 18:57:24 -0600456 ssdt->revision = get_acpi_table_revision(SSDT);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000457 memcpy(&ssdt->oem_id, OEM_ID, 6);
458 memcpy(&ssdt->oem_table_id, oem_table_id, 8);
459 ssdt->oem_revision = 42;
Uwe Hermann622824c2010-11-19 15:14:42 +0000460 memcpy(&ssdt->asl_compiler_id, ASLC, 4);
Elyes HAOUAS26071aa2019-02-15 08:21:33 +0100461 ssdt->asl_compiler_revision = asl_revision;
Rudolf Marek293b5f52009-02-01 18:35:15 +0000462 ssdt->length = sizeof(acpi_header_t);
463
Stefan Reinauer8dcd50b2009-03-06 17:24:29 +0000464 acpigen_set_current((char *) current);
Duncan Laurie37319032016-05-26 12:47:05 -0700465
466 /* Write object to declare coreboot tables */
467 acpi_ssdt_write_cbtable();
468
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200469 {
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100470 struct device *dev;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200471 for (dev = all_devices; dev; dev = dev->next)
Karthikeyan Ramasubramaniand1c0f952020-11-02 16:26:52 -0700472 if (dev->enabled && dev->ops && dev->ops->acpi_fill_ssdt)
Nico Huber68680dd2020-03-31 17:34:52 +0200473 dev->ops->acpi_fill_ssdt(dev);
Vladimir Serbinenko0a669912014-10-05 14:34:17 +0200474 current = (unsigned long) acpigen_get_current();
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200475 }
Rudolf Marek293b5f52009-02-01 18:35:15 +0000476
Uwe Hermann622824c2010-11-19 15:14:42 +0000477 /* (Re)calculate length and checksum. */
Rudolf Marek293b5f52009-02-01 18:35:15 +0000478 ssdt->length = current - (unsigned long)ssdt;
479 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
480}
481
Stefan Reinauerf622d592005-11-26 16:56:05 +0000482int acpi_create_srat_lapic(acpi_srat_lapic_t *lapic, u8 node, u8 apic)
483{
Myles Watson0b4c9f02009-03-10 18:06:47 +0000484 memset((void *)lapic, 0, sizeof(acpi_srat_lapic_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000485
Uwe Hermann622824c2010-11-19 15:14:42 +0000486 lapic->type = 0; /* Processor local APIC/SAPIC affinity structure */
487 lapic->length = sizeof(acpi_srat_lapic_t);
488 lapic->flags = (1 << 0); /* Enabled (the use of this structure). */
489 lapic->proximity_domain_7_0 = node;
490 /* TODO: proximity_domain_31_8, local SAPIC EID, clock domain. */
491 lapic->apic_id = apic;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000492
Uwe Hermann622824c2010-11-19 15:14:42 +0000493 return lapic->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000494}
495
Uwe Hermann622824c2010-11-19 15:14:42 +0000496int 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 +0300497 u32 flags)
Stefan Reinauerf622d592005-11-26 16:56:05 +0000498{
Uwe Hermann622824c2010-11-19 15:14:42 +0000499 mem->type = 1; /* Memory affinity structure */
500 mem->length = sizeof(acpi_srat_mem_t);
501 mem->base_address_low = (basek << 10);
502 mem->base_address_high = (basek >> (32 - 10));
503 mem->length_low = (sizek << 10);
504 mem->length_high = (sizek >> (32 - 10));
505 mem->proximity_domain = node;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000506 mem->flags = flags;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000507
Uwe Hermann622824c2010-11-19 15:14:42 +0000508 return mem->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000509}
510
Uwe Hermann622824c2010-11-19 15:14:42 +0000511/* http://www.microsoft.com/whdc/system/sysinternals/sratdwn.mspx */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +0200512void acpi_create_srat(acpi_srat_t *srat,
513 unsigned long (*acpi_fill_srat)(unsigned long current))
Stefan Reinauerf622d592005-11-26 16:56:05 +0000514{
Uwe Hermann622824c2010-11-19 15:14:42 +0000515 acpi_header_t *header = &(srat->header);
516 unsigned long current = (unsigned long)srat + sizeof(acpi_srat_t);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000517
Uwe Hermann622824c2010-11-19 15:14:42 +0000518 memset((void *)srat, 0, sizeof(acpi_srat_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000519
John Zhao2ba303e2019-05-28 16:48:14 -0700520 if (!header)
521 return;
522
Uwe Hermann622824c2010-11-19 15:14:42 +0000523 /* Fill out header fields. */
524 memcpy(header->signature, "SRAT", 4);
525 memcpy(header->oem_id, OEM_ID, 6);
526 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
527 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000528
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100529 header->asl_compiler_revision = asl_revision;
Uwe Hermann622824c2010-11-19 15:14:42 +0000530 header->length = sizeof(acpi_srat_t);
Marc Jones93a51762018-08-22 18:57:24 -0600531 header->revision = get_acpi_table_revision(SRAT);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000532
Uwe Hermann622824c2010-11-19 15:14:42 +0000533 srat->resv = 1; /* Spec: Reserved to 1 for backwards compatibility. */
Stefan Reinauerf622d592005-11-26 16:56:05 +0000534
Uwe Hermann622824c2010-11-19 15:14:42 +0000535 current = acpi_fill_srat(current);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000536
Uwe Hermann622824c2010-11-19 15:14:42 +0000537 /* (Re)calculate length and checksum. */
538 header->length = current - (unsigned long)srat;
539 header->checksum = acpi_checksum((void *)srat, header->length);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000540}
541
Nico Hubere561f352015-10-26 11:51:25 +0100542void acpi_create_dmar(acpi_dmar_t *dmar, enum dmar_flags flags,
Lee Leahy024b13d2017-03-16 13:41:11 -0700543 unsigned long (*acpi_fill_dmar)(unsigned long))
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200544{
545 acpi_header_t *header = &(dmar->header);
546 unsigned long current = (unsigned long)dmar + sizeof(acpi_dmar_t);
547
548 memset((void *)dmar, 0, sizeof(acpi_dmar_t));
549
John Zhao2ba303e2019-05-28 16:48:14 -0700550 if (!header)
551 return;
552
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200553 /* Fill out header fields. */
554 memcpy(header->signature, "DMAR", 4);
555 memcpy(header->oem_id, OEM_ID, 6);
556 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
557 memcpy(header->asl_compiler_id, ASLC, 4);
558
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100559 header->asl_compiler_revision = asl_revision;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200560 header->length = sizeof(acpi_dmar_t);
Marc Jones93a51762018-08-22 18:57:24 -0600561 header->revision = get_acpi_table_revision(DMAR);
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200562
Jacob Laskaf3f654d2016-03-15 21:53:27 -0500563 dmar->host_address_width = cpu_phys_address_size() - 1;
Nico Hubere561f352015-10-26 11:51:25 +0100564 dmar->flags = flags;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200565
566 current = acpi_fill_dmar(current);
567
568 /* (Re)calculate length and checksum. */
569 header->length = current - (unsigned long)dmar;
570 header->checksum = acpi_checksum((void *)dmar, header->length);
571}
572
573unsigned long acpi_create_dmar_drhd(unsigned long current, u8 flags,
Matt DeVillier7866d492018-03-29 14:59:57 +0200574 u16 segment, u64 bar)
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200575{
576 dmar_entry_t *drhd = (dmar_entry_t *)current;
577 memset(drhd, 0, sizeof(*drhd));
578 drhd->type = DMAR_DRHD;
579 drhd->length = sizeof(*drhd); /* will be fixed up later */
580 drhd->flags = flags;
581 drhd->segment = segment;
582 drhd->bar = bar;
583
584 return drhd->length;
585}
586
Matt DeVillier7866d492018-03-29 14:59:57 +0200587unsigned long acpi_create_dmar_rmrr(unsigned long current, u16 segment,
588 u64 bar, u64 limit)
589{
590 dmar_rmrr_entry_t *rmrr = (dmar_rmrr_entry_t *)current;
591 memset(rmrr, 0, sizeof(*rmrr));
592 rmrr->type = DMAR_RMRR;
593 rmrr->length = sizeof(*rmrr); /* will be fixed up later */
594 rmrr->segment = segment;
595 rmrr->bar = bar;
596 rmrr->limit = limit;
597
598 return rmrr->length;
599}
600
Werner Zehd4d76952016-07-27 06:56:36 +0200601unsigned long acpi_create_dmar_atsr(unsigned long current, u8 flags,
602 u16 segment)
603{
604 dmar_atsr_entry_t *atsr = (dmar_atsr_entry_t *)current;
605 memset(atsr, 0, sizeof(*atsr));
606 atsr->type = DMAR_ATSR;
607 atsr->length = sizeof(*atsr); /* will be fixed up later */
608 atsr->flags = flags;
609 atsr->segment = segment;
610
611 return atsr->length;
612}
613
John Zhao76e70672019-04-04 11:03:28 -0700614unsigned long acpi_create_dmar_rhsa(unsigned long current, u64 base_addr,
615 u32 proximity_domain)
616{
617 dmar_rhsa_entry_t *rhsa = (dmar_rhsa_entry_t *)current;
618 memset(rhsa, 0, sizeof(*rhsa));
619 rhsa->type = DMAR_RHSA;
620 rhsa->length = sizeof(*rhsa);
621 rhsa->base_address = base_addr;
622 rhsa->proximity_domain = proximity_domain;
623
624 return rhsa->length;
625}
626
627unsigned long acpi_create_dmar_andd(unsigned long current, u8 device_number,
628 const char *device_name)
629{
630 dmar_andd_entry_t *andd = (dmar_andd_entry_t *)current;
631 int andd_len = sizeof(dmar_andd_entry_t) + strlen(device_name) + 1;
632 memset(andd, 0, andd_len);
633 andd->type = DMAR_ANDD;
634 andd->length = andd_len;
635 andd->device_number = device_number;
636 memcpy(&andd->device_name, device_name, strlen(device_name));
637
638 return andd->length;
639}
640
John Zhao091532d2021-04-17 16:03:21 -0700641unsigned long acpi_create_dmar_satc(unsigned long current, u8 flags, u16 segment)
John Zhao6edbb182021-03-24 11:55:09 -0700642{
643 dmar_satc_entry_t *satc = (dmar_satc_entry_t *)current;
John Zhao091532d2021-04-17 16:03:21 -0700644 int satc_len = sizeof(dmar_satc_entry_t);
John Zhao6edbb182021-03-24 11:55:09 -0700645 memset(satc, 0, satc_len);
646 satc->type = DMAR_SATC;
647 satc->length = satc_len;
648 satc->flags = flags;
649 satc->segment_number = segment;
John Zhao6edbb182021-03-24 11:55:09 -0700650
651 return satc->length;
652}
653
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200654void acpi_dmar_drhd_fixup(unsigned long base, unsigned long current)
655{
656 dmar_entry_t *drhd = (dmar_entry_t *)base;
657 drhd->length = current - base;
658}
659
Matt DeVillier7866d492018-03-29 14:59:57 +0200660void acpi_dmar_rmrr_fixup(unsigned long base, unsigned long current)
661{
662 dmar_rmrr_entry_t *rmrr = (dmar_rmrr_entry_t *)base;
663 rmrr->length = current - base;
664}
665
Werner Zehd4d76952016-07-27 06:56:36 +0200666void acpi_dmar_atsr_fixup(unsigned long base, unsigned long current)
667{
668 dmar_atsr_entry_t *atsr = (dmar_atsr_entry_t *)base;
669 atsr->length = current - base;
670}
671
John Zhao6edbb182021-03-24 11:55:09 -0700672void acpi_dmar_satc_fixup(unsigned long base, unsigned long current)
673{
674 dmar_satc_entry_t *satc = (dmar_satc_entry_t *)base;
675 satc->length = current - base;
676}
677
Matt DeVillier7866d492018-03-29 14:59:57 +0200678static unsigned long acpi_create_dmar_ds(unsigned long current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100679 enum dev_scope_type type, u8 enumeration_id, u8 bus, u8 dev, u8 fn)
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200680{
Nico Huber6c4751d2015-10-26 12:03:54 +0100681 /* we don't support longer paths yet */
682 const size_t dev_scope_length = sizeof(dev_scope_t) + 2;
683
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200684 dev_scope_t *ds = (dev_scope_t *)current;
Nico Huber6c4751d2015-10-26 12:03:54 +0100685 memset(ds, 0, dev_scope_length);
686 ds->type = type;
687 ds->length = dev_scope_length;
688 ds->enumeration = enumeration_id;
689 ds->start_bus = bus;
690 ds->path[0].dev = dev;
691 ds->path[0].fn = fn;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200692
693 return ds->length;
694}
695
Matt DeVillier7866d492018-03-29 14:59:57 +0200696unsigned long acpi_create_dmar_ds_pci_br(unsigned long current, u8 bus,
Werner Zeh21a5bff2016-07-27 07:07:20 +0200697 u8 dev, u8 fn)
698{
Matt DeVillier7866d492018-03-29 14:59:57 +0200699 return acpi_create_dmar_ds(current,
Werner Zeh21a5bff2016-07-27 07:07:20 +0200700 SCOPE_PCI_SUB, 0, bus, dev, fn);
701}
702
Matt DeVillier7866d492018-03-29 14:59:57 +0200703unsigned long acpi_create_dmar_ds_pci(unsigned long current, u8 bus,
Nico Huber6c4751d2015-10-26 12:03:54 +0100704 u8 dev, u8 fn)
705{
Matt DeVillier7866d492018-03-29 14:59:57 +0200706 return acpi_create_dmar_ds(current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100707 SCOPE_PCI_ENDPOINT, 0, bus, dev, fn);
708}
709
Matt DeVillier7866d492018-03-29 14:59:57 +0200710unsigned long acpi_create_dmar_ds_ioapic(unsigned long current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100711 u8 enumeration_id, u8 bus, u8 dev, u8 fn)
712{
Matt DeVillier7866d492018-03-29 14:59:57 +0200713 return acpi_create_dmar_ds(current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100714 SCOPE_IOAPIC, enumeration_id, bus, dev, fn);
715}
716
Matt DeVillier7866d492018-03-29 14:59:57 +0200717unsigned long acpi_create_dmar_ds_msi_hpet(unsigned long current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100718 u8 enumeration_id, u8 bus, u8 dev, u8 fn)
719{
Matt DeVillier7866d492018-03-29 14:59:57 +0200720 return acpi_create_dmar_ds(current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100721 SCOPE_MSI_HPET, enumeration_id, bus, dev, fn);
722}
723
Uwe Hermann622824c2010-11-19 15:14:42 +0000724/* http://h21007.www2.hp.com/portal/download/files/unprot/Itanium/slit.pdf */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +0200725void acpi_create_slit(acpi_slit_t *slit,
726 unsigned long (*acpi_fill_slit)(unsigned long current))
Yinghai Lud4b278c2006-10-04 20:46:15 +0000727{
Uwe Hermann622824c2010-11-19 15:14:42 +0000728 acpi_header_t *header = &(slit->header);
729 unsigned long current = (unsigned long)slit + sizeof(acpi_slit_t);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000730
Uwe Hermann622824c2010-11-19 15:14:42 +0000731 memset((void *)slit, 0, sizeof(acpi_slit_t));
Yinghai Lud4b278c2006-10-04 20:46:15 +0000732
John Zhao2ba303e2019-05-28 16:48:14 -0700733 if (!header)
734 return;
735
Uwe Hermann622824c2010-11-19 15:14:42 +0000736 /* Fill out header fields. */
737 memcpy(header->signature, "SLIT", 4);
738 memcpy(header->oem_id, OEM_ID, 6);
739 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
740 memcpy(header->asl_compiler_id, ASLC, 4);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000741
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100742 header->asl_compiler_revision = asl_revision;
Uwe Hermann622824c2010-11-19 15:14:42 +0000743 header->length = sizeof(acpi_slit_t);
Marc Jones93a51762018-08-22 18:57:24 -0600744 header->revision = get_acpi_table_revision(SLIT);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000745
Uwe Hermann622824c2010-11-19 15:14:42 +0000746 current = acpi_fill_slit(current);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000747
Uwe Hermann622824c2010-11-19 15:14:42 +0000748 /* (Re)calculate length and checksum. */
749 header->length = current - (unsigned long)slit;
750 header->checksum = acpi_checksum((void *)slit, header->length);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000751}
752
Uwe Hermann622824c2010-11-19 15:14:42 +0000753/* http://www.intel.com/hardwaredesign/hpetspec_1.pdf */
Stefan Reinauer777224c2005-01-19 14:06:41 +0000754void acpi_create_hpet(acpi_hpet_t *hpet)
Stefan Reinauer688b3852004-01-28 16:56:14 +0000755{
Uwe Hermann622824c2010-11-19 15:14:42 +0000756 acpi_header_t *header = &(hpet->header);
757 acpi_addr_t *addr = &(hpet->addr);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000758
Stefan Reinauera7648c22004-01-29 17:31:34 +0000759 memset((void *)hpet, 0, sizeof(acpi_hpet_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000760
John Zhao2ba303e2019-05-28 16:48:14 -0700761 if (!header)
762 return;
763
Uwe Hermann622824c2010-11-19 15:14:42 +0000764 /* Fill out header fields. */
Stefan Reinauer4704dc52009-07-22 01:11:37 +0000765 memcpy(header->signature, "HPET", 4);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000766 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000767 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000768 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000769
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100770 header->asl_compiler_revision = asl_revision;
Stefan Reinauer688b3852004-01-28 16:56:14 +0000771 header->length = sizeof(acpi_hpet_t);
Marc Jones93a51762018-08-22 18:57:24 -0600772 header->revision = get_acpi_table_revision(HPET);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000773
Uwe Hermann622824c2010-11-19 15:14:42 +0000774 /* Fill out HPET address. */
Elyes HAOUAS04071f42020-07-20 17:05:24 +0200775 addr->space_id = ACPI_ADDRESS_SPACE_MEMORY;
Uwe Hermann622824c2010-11-19 15:14:42 +0000776 addr->bit_width = 64;
777 addr->bit_offset = 0;
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200778 addr->addrl = CONFIG_HPET_ADDRESS & 0xffffffff;
779 addr->addrh = ((unsigned long long)CONFIG_HPET_ADDRESS) >> 32;
Stefan Reinauer688b3852004-01-28 16:56:14 +0000780
Lee Leahy024b13d2017-03-16 13:41:11 -0700781 hpet->id = *(unsigned int *)CONFIG_HPET_ADDRESS;
Uwe Hermann622824c2010-11-19 15:14:42 +0000782 hpet->number = 0;
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200783 hpet->min_tick = CONFIG_HPET_MIN_TICKS;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000784
Uwe Hermann622824c2010-11-19 15:14:42 +0000785 header->checksum = acpi_checksum((void *)hpet, sizeof(acpi_hpet_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000786}
Uwe Hermann622824c2010-11-19 15:14:42 +0000787
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700788void acpi_create_vfct(const struct device *device,
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530789 acpi_vfct_t *vfct,
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700790 unsigned long (*acpi_fill_vfct)(const struct device *device,
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530791 acpi_vfct_t *vfct_struct, unsigned long current))
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200792{
793 acpi_header_t *header = &(vfct->header);
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530794 unsigned long current = (unsigned long)vfct + sizeof(acpi_vfct_t);
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200795
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530796 memset((void *)vfct, 0, sizeof(acpi_vfct_t));
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200797
John Zhao2ba303e2019-05-28 16:48:14 -0700798 if (!header)
799 return;
800
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200801 /* Fill out header fields. */
802 memcpy(header->signature, "VFCT", 4);
803 memcpy(header->oem_id, OEM_ID, 6);
804 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
805 memcpy(header->asl_compiler_id, ASLC, 4);
806
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100807 header->asl_compiler_revision = asl_revision;
Marc Jones93a51762018-08-22 18:57:24 -0600808 header->revision = get_acpi_table_revision(VFCT);
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200809
810 current = acpi_fill_vfct(device, vfct, current);
811
Kyösti Mälkkifb493792019-06-30 06:29:52 +0300812 /* If no BIOS image, return with header->length == 0. */
813 if (!vfct->VBIOSImageOffset)
814 return;
815
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200816 /* (Re)calculate length and checksum. */
817 header->length = current - (unsigned long)vfct;
818 header->checksum = acpi_checksum((void *)vfct, header->length);
819}
820
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700821void acpi_create_ipmi(const struct device *device,
Patrick Rudolph9d98e5a2019-06-14 19:00:04 +0200822 struct acpi_spmi *spmi,
823 const u16 ipmi_revision,
824 const acpi_addr_t *addr,
825 const enum acpi_ipmi_interface_type type,
826 const s8 gpe_interrupt,
827 const u32 apic_interrupt,
828 const u32 uid)
829{
830 acpi_header_t *header = &(spmi->header);
831 memset((void *)spmi, 0, sizeof(struct acpi_spmi));
832
833 /* Fill out header fields. */
834 memcpy(header->signature, "SPMI", 4);
835 memcpy(header->oem_id, OEM_ID, 6);
836 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
837 memcpy(header->asl_compiler_id, ASLC, 4);
838
839 header->asl_compiler_revision = asl_revision;
840 header->length = sizeof(struct acpi_spmi);
841 header->revision = get_acpi_table_revision(SPMI);
842
843 spmi->reserved = 1;
844
845 if (device->path.type == DEVICE_PATH_PCI) {
846 spmi->pci_device_flag = ACPI_IPMI_PCI_DEVICE_FLAG;
847 spmi->pci_bus = device->bus->secondary;
848 spmi->pci_device = device->path.pci.devfn >> 3;
849 spmi->pci_function = device->path.pci.devfn & 0x7;
850 } else if (type != IPMI_INTERFACE_SSIF) {
851 memcpy(spmi->uid, &uid, sizeof(spmi->uid));
852 }
853
854 spmi->base_address = *addr;
855 spmi->specification_revision = ipmi_revision;
856
857 spmi->interface_type = type;
858
859 if (gpe_interrupt >= 0 && gpe_interrupt < 32) {
860 spmi->gpe = gpe_interrupt;
861 spmi->interrupt_type |= ACPI_IPMI_INT_TYPE_SCI;
862 }
863 if (apic_interrupt > 0) {
864 spmi->global_system_interrupt = apic_interrupt;
865 spmi->interrupt_type |= ACPI_IPMI_INT_TYPE_APIC;
866 }
867
868 /* Calculate checksum. */
869 header->checksum = acpi_checksum((void *)spmi, header->length);
870}
871
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500872void acpi_create_ivrs(acpi_ivrs_t *ivrs,
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700873 unsigned long (*acpi_fill_ivrs)(acpi_ivrs_t *ivrs_struct,
874 unsigned long current))
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500875{
876 acpi_header_t *header = &(ivrs->header);
877 unsigned long current = (unsigned long)ivrs + sizeof(acpi_ivrs_t);
878
879 memset((void *)ivrs, 0, sizeof(acpi_ivrs_t));
880
John Zhao2ba303e2019-05-28 16:48:14 -0700881 if (!header)
882 return;
883
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500884 /* Fill out header fields. */
885 memcpy(header->signature, "IVRS", 4);
886 memcpy(header->oem_id, OEM_ID, 6);
887 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
888 memcpy(header->asl_compiler_id, ASLC, 4);
889
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100890 header->asl_compiler_revision = asl_revision;
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500891 header->length = sizeof(acpi_ivrs_t);
Marc Jones93a51762018-08-22 18:57:24 -0600892 header->revision = get_acpi_table_revision(IVRS);
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500893
894 current = acpi_fill_ivrs(ivrs, current);
895
896 /* (Re)calculate length and checksum. */
897 header->length = current - (unsigned long)ivrs;
898 header->checksum = acpi_checksum((void *)ivrs, header->length);
899}
900
Jason Glenesk61624b22020-11-02 20:06:23 -0800901void acpi_create_crat(struct acpi_crat_header *crat,
902 unsigned long (*acpi_fill_crat)(struct acpi_crat_header *crat_struct,
903 unsigned long current))
904{
905 acpi_header_t *header = &(crat->header);
906 unsigned long current = (unsigned long)crat + sizeof(struct acpi_crat_header);
907
908 memset((void *)crat, 0, sizeof(struct acpi_crat_header));
909
910 if (!header)
911 return;
912
913 /* Fill out header fields. */
914 memcpy(header->signature, "CRAT", 4);
915 memcpy(header->oem_id, OEM_ID, 6);
916 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
917 memcpy(header->asl_compiler_id, ASLC, 4);
918
919 header->asl_compiler_revision = asl_revision;
920 header->length = sizeof(struct acpi_crat_header);
921 header->revision = get_acpi_table_revision(CRAT);
922
923 current = acpi_fill_crat(crat, current);
924
925 /* (Re)calculate length and checksum. */
926 header->length = current - (unsigned long)crat;
927 header->checksum = acpi_checksum((void *)crat, header->length);
928}
929
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700930unsigned long acpi_write_hpet(const struct device *device, unsigned long current,
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700931 acpi_rsdp_t *rsdp)
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200932{
933 acpi_hpet_t *hpet;
934
935 /*
936 * We explicitly add these tables later on:
937 */
938 printk(BIOS_DEBUG, "ACPI: * HPET\n");
939
940 hpet = (acpi_hpet_t *) current;
941 current += sizeof(acpi_hpet_t);
Felix Heldc4697122019-06-20 13:50:17 +0200942 current = ALIGN_UP(current, 16);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200943 acpi_create_hpet(hpet);
944 acpi_add_table(rsdp, hpet);
945
946 return current;
947}
948
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800949void acpi_create_dbg2(acpi_dbg2_header_t *dbg2,
950 int port_type, int port_subtype,
951 acpi_addr_t *address, uint32_t address_size,
952 const char *device_path)
953{
954 uintptr_t current;
955 acpi_dbg2_device_t *device;
956 uint32_t *dbg2_addr_size;
957 acpi_header_t *header;
958 size_t path_len;
959 const char *path;
960 char *namespace;
961
962 /* Fill out header fields. */
963 current = (uintptr_t)dbg2;
964 memset(dbg2, 0, sizeof(acpi_dbg2_header_t));
965 header = &(dbg2->header);
John Zhao2ba303e2019-05-28 16:48:14 -0700966
967 if (!header)
968 return;
969
Marc Jones93a51762018-08-22 18:57:24 -0600970 header->revision = get_acpi_table_revision(DBG2);
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800971 memcpy(header->signature, "DBG2", 4);
972 memcpy(header->oem_id, OEM_ID, 6);
973 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
974 memcpy(header->asl_compiler_id, ASLC, 4);
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100975 header->asl_compiler_revision = asl_revision;
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800976
977 /* One debug device defined */
978 dbg2->devices_offset = sizeof(acpi_dbg2_header_t);
979 dbg2->devices_count = 1;
980 current += sizeof(acpi_dbg2_header_t);
981
982 /* Device comes after the header */
983 device = (acpi_dbg2_device_t *)current;
984 memset(device, 0, sizeof(acpi_dbg2_device_t));
985 current += sizeof(acpi_dbg2_device_t);
986
987 device->revision = 0;
988 device->address_count = 1;
989 device->port_type = port_type;
990 device->port_subtype = port_subtype;
991
992 /* Base Address comes after device structure */
993 memcpy((void *)current, address, sizeof(acpi_addr_t));
994 device->base_address_offset = current - (uintptr_t)device;
995 current += sizeof(acpi_addr_t);
996
997 /* Address Size comes after address structure */
998 dbg2_addr_size = (uint32_t *)current;
999 device->address_size_offset = current - (uintptr_t)device;
1000 *dbg2_addr_size = address_size;
1001 current += sizeof(uint32_t);
1002
1003 /* Namespace string comes last, use '.' if not provided */
1004 path = device_path ? : ".";
1005 /* Namespace string length includes NULL terminator */
1006 path_len = strlen(path) + 1;
1007 namespace = (char *)current;
1008 device->namespace_string_length = path_len;
1009 device->namespace_string_offset = current - (uintptr_t)device;
1010 strncpy(namespace, path, path_len);
1011 current += path_len;
1012
1013 /* Update structure lengths and checksum */
1014 device->length = current - (uintptr_t)device;
1015 header->length = current - (uintptr_t)dbg2;
1016 header->checksum = acpi_checksum((uint8_t *)dbg2, header->length);
1017}
1018
1019unsigned long acpi_write_dbg2_pci_uart(acpi_rsdp_t *rsdp, unsigned long current,
Aamir Bohrae825d3f2019-07-30 10:11:41 +05301020 const struct device *dev, uint8_t access_size)
Duncan Lauriee4a36c72017-11-11 19:33:25 -08001021{
1022 acpi_dbg2_header_t *dbg2 = (acpi_dbg2_header_t *)current;
1023 struct resource *res;
1024 acpi_addr_t address;
1025
1026 if (!dev) {
Wim Vervoorn6cd52432020-02-03 14:41:46 +01001027 printk(BIOS_DEBUG, "%s: Device not found\n", __func__);
Duncan Lauriee4a36c72017-11-11 19:33:25 -08001028 return current;
1029 }
Mario Scheithauerf1eb0ea2017-11-21 13:52:53 +01001030 if (!dev->enabled) {
1031 printk(BIOS_INFO, "%s: Device not enabled\n", __func__);
1032 return current;
1033 }
Duncan Lauriee4a36c72017-11-11 19:33:25 -08001034 res = find_resource(dev, PCI_BASE_ADDRESS_0);
1035 if (!res) {
1036 printk(BIOS_ERR, "%s: Unable to find resource for %s\n",
1037 __func__, dev_path(dev));
1038 return current;
1039 }
1040
1041 memset(&address, 0, sizeof(address));
1042 if (res->flags & IORESOURCE_IO)
1043 address.space_id = ACPI_ADDRESS_SPACE_IO;
1044 else if (res->flags & IORESOURCE_MEM)
1045 address.space_id = ACPI_ADDRESS_SPACE_MEMORY;
1046 else {
1047 printk(BIOS_ERR, "%s: Unknown address space type\n", __func__);
1048 return current;
1049 }
1050
1051 address.addrl = (uint32_t)res->base;
1052 address.addrh = (uint32_t)((res->base >> 32) & 0xffffffff);
1053 address.access_size = access_size;
1054
1055 acpi_create_dbg2(dbg2,
1056 ACPI_DBG2_PORT_SERIAL,
1057 ACPI_DBG2_PORT_SERIAL_16550,
1058 &address, res->size,
1059 acpi_device_path(dev));
1060
1061 if (dbg2->header.length) {
1062 current += dbg2->header.length;
1063 current = acpi_align_current(current);
1064 acpi_add_table(rsdp, dbg2);
1065 }
1066
1067 return current;
1068}
1069
Stefan Reinauer777224c2005-01-19 14:06:41 +00001070void acpi_create_facs(acpi_facs_t *facs)
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +00001071{
Uwe Hermann622824c2010-11-19 15:14:42 +00001072 memset((void *)facs, 0, sizeof(acpi_facs_t));
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +00001073
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001074 memcpy(facs->signature, "FACS", 4);
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +00001075 facs->length = sizeof(acpi_facs_t);
1076 facs->hardware_signature = 0;
1077 facs->firmware_waking_vector = 0;
1078 facs->global_lock = 0;
1079 facs->flags = 0;
1080 facs->x_firmware_waking_vector_l = 0;
1081 facs->x_firmware_waking_vector_h = 0;
Marc Jones93a51762018-08-22 18:57:24 -06001082 facs->version = get_acpi_table_revision(FACS);
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +00001083}
Stefan Reinauer777224c2005-01-19 14:06:41 +00001084
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001085static void acpi_write_rsdt(acpi_rsdt_t *rsdt, char *oem_id, char *oem_table_id)
Stefan Reinauer14e22772010-04-27 06:56:47 +00001086{
Uwe Hermann622824c2010-11-19 15:14:42 +00001087 acpi_header_t *header = &(rsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001088
John Zhao2ba303e2019-05-28 16:48:14 -07001089 if (!header)
1090 return;
1091
Uwe Hermann622824c2010-11-19 15:14:42 +00001092 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001093 memcpy(header->signature, "RSDT", 4);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001094 memcpy(header->oem_id, oem_id, 6);
1095 memcpy(header->oem_table_id, oem_table_id, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +00001096 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001097
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001098 header->asl_compiler_revision = asl_revision;
Stefan Reinauer688b3852004-01-28 16:56:14 +00001099 header->length = sizeof(acpi_rsdt_t);
Marc Jones93a51762018-08-22 18:57:24 -06001100 header->revision = get_acpi_table_revision(RSDT);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001101
Uwe Hermann622824c2010-11-19 15:14:42 +00001102 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauer688b3852004-01-28 16:56:14 +00001103
Uwe Hermann622824c2010-11-19 15:14:42 +00001104 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001105 header->checksum = acpi_checksum((void *)rsdt, sizeof(acpi_rsdt_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +00001106}
1107
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001108static void acpi_write_xsdt(acpi_xsdt_t *xsdt, char *oem_id, char *oem_table_id)
Stefan Reinauer14e22772010-04-27 06:56:47 +00001109{
Uwe Hermann622824c2010-11-19 15:14:42 +00001110 acpi_header_t *header = &(xsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001111
John Zhao2ba303e2019-05-28 16:48:14 -07001112 if (!header)
1113 return;
1114
Uwe Hermann622824c2010-11-19 15:14:42 +00001115 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001116 memcpy(header->signature, "XSDT", 4);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001117 memcpy(header->oem_id, oem_id, 6);
1118 memcpy(header->oem_table_id, oem_table_id, 8);
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001119 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001120
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001121 header->asl_compiler_revision = asl_revision;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001122 header->length = sizeof(acpi_xsdt_t);
Marc Jones93a51762018-08-22 18:57:24 -06001123 header->revision = get_acpi_table_revision(XSDT);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001124
Uwe Hermann622824c2010-11-19 15:14:42 +00001125 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001126
Uwe Hermann622824c2010-11-19 15:14:42 +00001127 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001128 header->checksum = acpi_checksum((void *)xsdt, sizeof(acpi_xsdt_t));
1129}
1130
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001131static void acpi_write_rsdp(acpi_rsdp_t *rsdp, acpi_rsdt_t *rsdt,
1132 acpi_xsdt_t *xsdt, char *oem_id)
Stefan Reinauer688b3852004-01-28 16:56:14 +00001133{
Stefan Reinauerd18faac2009-11-05 18:06:43 +00001134 memset(rsdp, 0, sizeof(acpi_rsdp_t));
Uwe Hermann622824c2010-11-19 15:14:42 +00001135
Stefan Reinauer688b3852004-01-28 16:56:14 +00001136 memcpy(rsdp->signature, RSDP_SIG, 8);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001137 memcpy(rsdp->oem_id, oem_id, 6);
Uwe Hermann622824c2010-11-19 15:14:42 +00001138
1139 rsdp->length = sizeof(acpi_rsdp_t);
Stefan Reinauerdefee172015-06-18 01:11:19 -07001140 rsdp->rsdt_address = (uintptr_t)rsdt;
Uwe Hermann622824c2010-11-19 15:14:42 +00001141
1142 /*
1143 * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2.
1144 *
1145 * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
1146 * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
1147 * revision 0).
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001148 */
1149 if (xsdt == NULL) {
Uwe Hermann622824c2010-11-19 15:14:42 +00001150 rsdp->revision = 0;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001151 } else {
Stefan Reinauerdefee172015-06-18 01:11:19 -07001152 rsdp->xsdt_address = (u64)(uintptr_t)xsdt;
Marc Jones93a51762018-08-22 18:57:24 -06001153 rsdp->revision = get_acpi_table_revision(RSDP);
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001154 }
Uwe Hermann622824c2010-11-19 15:14:42 +00001155
1156 /* Calculate checksums. */
1157 rsdp->checksum = acpi_checksum((void *)rsdp, 20);
1158 rsdp->ext_checksum = acpi_checksum((void *)rsdp, sizeof(acpi_rsdp_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +00001159}
1160
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001161unsigned long acpi_create_hest_error_source(acpi_hest_t *hest,
1162 acpi_hest_esd_t *esd, u16 type, void *data, u16 data_len)
zbaocaf494c82012-04-13 13:57:14 +08001163{
1164 acpi_header_t *header = &(hest->header);
1165 acpi_hest_hen_t *hen;
1166 void *pos;
1167 u16 len;
1168
1169 pos = esd;
1170 memset(pos, 0, sizeof(acpi_hest_esd_t));
1171 len = 0;
1172 esd->type = type; /* MCE */
1173 esd->source_id = hest->error_source_count;
1174 esd->flags = 0; /* FIRMWARE_FIRST */
1175 esd->enabled = 1;
1176 esd->prealloc_erecords = 1;
1177 esd->max_section_per_record = 0x1;
1178
1179 len += sizeof(acpi_hest_esd_t);
1180 pos = esd + 1;
1181
1182 switch (type) {
1183 case 0: /* MCE */
1184 break;
1185 case 1: /* CMC */
1186 hen = (acpi_hest_hen_t *) (pos);
1187 memset(pos, 0, sizeof(acpi_hest_hen_t));
1188 hen->type = 3; /* SCI? */
1189 hen->length = sizeof(acpi_hest_hen_t);
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001190 hen->conf_we = 0; /* Configuration Write Enable. */
zbaocaf494c82012-04-13 13:57:14 +08001191 hen->poll_interval = 0;
1192 hen->vector = 0;
1193 hen->sw2poll_threshold_val = 0;
1194 hen->sw2poll_threshold_win = 0;
1195 hen->error_threshold_val = 0;
1196 hen->error_threshold_win = 0;
1197 len += sizeof(acpi_hest_hen_t);
1198 pos = hen + 1;
1199 break;
1200 case 2: /* NMI */
1201 case 6: /* AER Root Port */
1202 case 7: /* AER Endpoint */
1203 case 8: /* AER Bridge */
1204 case 9: /* Generic Hardware Error Source. */
1205 /* TODO: */
1206 break;
1207 default:
1208 printk(BIOS_DEBUG, "Invalid type of Error Source.");
1209 break;
1210 }
Lee Leahy024b13d2017-03-16 13:41:11 -07001211 hest->error_source_count++;
zbaocaf494c82012-04-13 13:57:14 +08001212
1213 memcpy(pos, data, data_len);
1214 len += data_len;
John Zhao2ba303e2019-05-28 16:48:14 -07001215 if (header)
1216 header->length += len;
zbaocaf494c82012-04-13 13:57:14 +08001217
1218 return len;
1219}
1220
1221/* ACPI 4.0 */
Vladimir Serbinenko807127f2014-11-09 13:36:18 +01001222void acpi_write_hest(acpi_hest_t *hest,
1223 unsigned long (*acpi_fill_hest)(acpi_hest_t *hest))
zbaocaf494c82012-04-13 13:57:14 +08001224{
1225 acpi_header_t *header = &(hest->header);
1226
1227 memset(hest, 0, sizeof(acpi_hest_t));
1228
John Zhao2ba303e2019-05-28 16:48:14 -07001229 if (!header)
1230 return;
1231
zbaocaf494c82012-04-13 13:57:14 +08001232 memcpy(header->signature, "HEST", 4);
1233 memcpy(header->oem_id, OEM_ID, 6);
1234 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1235 memcpy(header->asl_compiler_id, ASLC, 4);
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001236 header->asl_compiler_revision = asl_revision;
zbaocaf494c82012-04-13 13:57:14 +08001237 header->length += sizeof(acpi_hest_t);
Marc Jones93a51762018-08-22 18:57:24 -06001238 header->revision = get_acpi_table_revision(HEST);
zbaocaf494c82012-04-13 13:57:14 +08001239
1240 acpi_fill_hest(hest);
1241
1242 /* Calculate checksums. */
1243 header->checksum = acpi_checksum((void *)hest, header->length);
1244}
1245
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001246/* ACPI 3.0b */
1247void acpi_write_bert(acpi_bert_t *bert, uintptr_t region, size_t length)
1248{
1249 acpi_header_t *header = &(bert->header);
1250
1251 memset(bert, 0, sizeof(acpi_bert_t));
1252
John Zhao2ba303e2019-05-28 16:48:14 -07001253 if (!header)
1254 return;
1255
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001256 memcpy(header->signature, "BERT", 4);
1257 memcpy(header->oem_id, OEM_ID, 6);
1258 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1259 memcpy(header->asl_compiler_id, ASLC, 4);
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001260 header->asl_compiler_revision = asl_revision;
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001261 header->length += sizeof(acpi_bert_t);
1262 header->revision = get_acpi_table_revision(BERT);
1263
1264 bert->error_region = region;
1265 bert->region_length = length;
1266
1267 /* Calculate checksums. */
1268 header->checksum = acpi_checksum((void *)bert, header->length);
1269}
1270
Angel Pons79572e42020-07-13 00:17:43 +02001271__weak void arch_fill_fadt(acpi_fadt_t *fadt) { }
Kyösti Mälkkif9aac922020-05-30 16:16:28 +03001272__weak void soc_fill_fadt(acpi_fadt_t *fadt) { }
Kyösti Mälkki02fd15d2020-06-02 03:34:43 +03001273__weak void mainboard_fill_fadt(acpi_fadt_t *fadt) { }
Kyösti Mälkkif9aac922020-05-30 16:16:28 +03001274
Lee Leahy024b13d2017-03-16 13:41:11 -07001275void acpi_create_fadt(acpi_fadt_t *fadt, acpi_facs_t *facs, void *dsdt)
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001276{
1277 acpi_header_t *header = &(fadt->header);
1278
1279 memset((void *) fadt, 0, sizeof(acpi_fadt_t));
John Zhao2ba303e2019-05-28 16:48:14 -07001280
1281 if (!header)
1282 return;
1283
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001284 memcpy(header->signature, "FACP", 4);
1285 header->length = sizeof(acpi_fadt_t);
Marc Jones93a51762018-08-22 18:57:24 -06001286 header->revision = get_acpi_table_revision(FADT);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001287 memcpy(header->oem_id, OEM_ID, 6);
1288 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1289 memcpy(header->asl_compiler_id, ASLC, 4);
Elyes HAOUAS26071aa2019-02-15 08:21:33 +01001290 header->asl_compiler_revision = asl_revision;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001291
1292 fadt->firmware_ctrl = (unsigned long) facs;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001293 fadt->x_firmware_ctl_l = (unsigned long)facs;
1294 fadt->x_firmware_ctl_h = 0;
Kyösti Mälkkid4acee82020-06-03 07:20:55 +03001295
1296 fadt->dsdt = (unsigned long) dsdt;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001297 fadt->x_dsdt_l = (unsigned long)dsdt;
1298 fadt->x_dsdt_h = 0;
1299
Kyösti Mälkkid4acee82020-06-03 07:20:55 +03001300 /* should be 0 ACPI 3.0 */
1301 fadt->reserved = 0;
1302
Kyösti Mälkkie0d38682020-06-07 12:01:58 +03001303 fadt->preferred_pm_profile = acpi_get_preferred_pm_profile();
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001304
Angel Pons79572e42020-07-13 00:17:43 +02001305 arch_fill_fadt(fadt);
1306
Vladimir Serbinenkoab83ef02014-10-25 15:18:25 +02001307 acpi_fill_fadt(fadt);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001308
Kyösti Mälkkif9aac922020-05-30 16:16:28 +03001309 soc_fill_fadt(fadt);
Kyösti Mälkki02fd15d2020-06-02 03:34:43 +03001310 mainboard_fill_fadt(fadt);
Kyösti Mälkkif9aac922020-05-30 16:16:28 +03001311
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001312 header->checksum =
1313 acpi_checksum((void *) fadt, header->length);
1314}
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001315
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001316void acpi_create_lpit(acpi_lpit_t *lpit)
1317{
1318 acpi_header_t *header = &(lpit->header);
1319 unsigned long current = (unsigned long)lpit + sizeof(acpi_lpit_t);
1320
1321 memset((void *)lpit, 0, sizeof(acpi_lpit_t));
1322
1323 if (!header)
1324 return;
1325
1326 /* Fill out header fields. */
1327 memcpy(header->signature, "LPIT", 4);
1328 memcpy(header->oem_id, OEM_ID, 6);
1329 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1330 memcpy(header->asl_compiler_id, ASLC, 4);
1331
1332 header->asl_compiler_revision = asl_revision;
1333 header->revision = get_acpi_table_revision(LPIT);
1334 header->oem_revision = 42;
1335 header->length = sizeof(acpi_lpit_t);
1336
1337 current = acpi_fill_lpit(current);
1338
1339 /* (Re)calculate length and checksum. */
1340 header->length = current - (unsigned long)lpit;
1341 header->checksum = acpi_checksum((void *)lpit, header->length);
1342}
1343
1344unsigned long acpi_create_lpi_desc_ncst(acpi_lpi_desc_ncst_t *lpi_desc, uint16_t uid)
1345{
1346 memset(lpi_desc, 0, sizeof(acpi_lpi_desc_ncst_t));
1347 lpi_desc->header.length = sizeof(acpi_lpi_desc_ncst_t);
1348 lpi_desc->header.type = ACPI_LPI_DESC_TYPE_NATIVE_CSTATE;
1349 lpi_desc->header.uid = uid;
1350
1351 return lpi_desc->header.length;
1352}
1353
Francois Toguo522e0db2021-01-21 09:55:19 -08001354/* BERT helpers */
1355bool __weak acpi_is_boot_error_src_present(void)
1356{
1357 return false;
1358}
1359
1360__weak void acpi_soc_fill_bert(acpi_bert_t *bert, void **region, size_t *length) {}
1361
Aaron Durbin64031672018-04-21 14:45:32 -06001362unsigned long __weak fw_cfg_acpi_tables(unsigned long start)
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001363{
1364 return 0;
1365}
1366
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001367unsigned long write_acpi_tables(unsigned long start)
1368{
1369 unsigned long current;
1370 acpi_rsdp_t *rsdp;
1371 acpi_rsdt_t *rsdt;
1372 acpi_xsdt_t *xsdt;
1373 acpi_fadt_t *fadt;
1374 acpi_facs_t *facs;
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001375 acpi_header_t *slic_file, *slic;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001376 acpi_header_t *ssdt;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001377 acpi_header_t *dsdt_file, *dsdt;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001378 acpi_mcfg_t *mcfg;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +02001379 acpi_tcpa_t *tcpa;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001380 acpi_tpm2_t *tpm2;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001381 acpi_madt_t *madt;
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001382 acpi_lpit_t *lpit;
Francois Toguo522e0db2021-01-21 09:55:19 -08001383 acpi_bert_t *bert;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +11001384 struct device *dev;
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001385 unsigned long fw;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001386 size_t slic_size, dsdt_size;
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001387 char oem_id[6], oem_table_id[8];
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001388
1389 current = start;
1390
1391 /* Align ACPI tables to 16byte */
Aaron Durbin07a1b282015-12-10 17:07:38 -06001392 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001393
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001394 /* Special case for qemu */
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001395 fw = fw_cfg_acpi_tables(current);
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001396 if (fw) {
1397 rsdp = NULL;
1398 /* Find RSDP. */
1399 for (void *p = (void *)current; p < (void *)fw; p += 16) {
1400 if (valid_rsdp((acpi_rsdp_t *)p)) {
1401 rsdp = p;
1402 break;
1403 }
1404 }
1405 if (!rsdp)
1406 return fw;
1407
1408 /* Add BOOT0000 for Linux google firmware driver */
1409 printk(BIOS_DEBUG, "ACPI: * SSDT\n");
1410 ssdt = (acpi_header_t *)fw;
1411 current = (unsigned long)ssdt + sizeof(acpi_header_t);
1412
1413 memset((void *)ssdt, 0, sizeof(acpi_header_t));
1414
1415 memcpy(&ssdt->signature, "SSDT", 4);
1416 ssdt->revision = get_acpi_table_revision(SSDT);
1417 memcpy(&ssdt->oem_id, OEM_ID, 6);
1418 memcpy(&ssdt->oem_table_id, oem_table_id, 8);
1419 ssdt->oem_revision = 42;
1420 memcpy(&ssdt->asl_compiler_id, ASLC, 4);
1421 ssdt->asl_compiler_revision = asl_revision;
1422 ssdt->length = sizeof(acpi_header_t);
1423
1424 acpigen_set_current((char *) current);
1425
1426 /* Write object to declare coreboot tables */
1427 acpi_ssdt_write_cbtable();
1428
1429 /* (Re)calculate length and checksum. */
1430 ssdt->length = current - (unsigned long)ssdt;
1431 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
1432
1433 acpi_create_ssdt_generator(ssdt, ACPI_TABLE_CREATOR);
1434
1435 acpi_add_table(rsdp, ssdt);
1436
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001437 return fw;
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001438 }
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001439
Julius Werner834b3ec2020-03-04 16:52:08 -08001440 dsdt_file = cbfs_map(CONFIG_CBFS_PREFIX "/dsdt.aml", &dsdt_size);
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001441 if (!dsdt_file) {
1442 printk(BIOS_ERR, "No DSDT file, skipping ACPI tables\n");
1443 return current;
1444 }
1445
1446 if (dsdt_file->length > dsdt_size
Elyes HAOUAS7d87e762016-10-03 22:11:07 +02001447 || dsdt_file->length < sizeof(acpi_header_t)
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001448 || memcmp(dsdt_file->signature, "DSDT", 4) != 0) {
1449 printk(BIOS_ERR, "Invalid DSDT file, skipping ACPI tables\n");
1450 return current;
1451 }
1452
Julius Werner834b3ec2020-03-04 16:52:08 -08001453 slic_file = cbfs_map(CONFIG_CBFS_PREFIX "/slic", &slic_size);
Vladimir Serbinenko2cb29782015-05-31 11:32:09 +02001454 if (slic_file
1455 && (slic_file->length > slic_size
Elyes HAOUAS7d87e762016-10-03 22:11:07 +02001456 || slic_file->length < sizeof(acpi_header_t)
Benjamin Doron2b2dc0c2020-09-12 02:55:57 +00001457 || (memcmp(slic_file->signature, "SLIC", 4) != 0
1458 && memcmp(slic_file->signature, "MSDM", 4) != 0))) {
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001459 slic_file = 0;
1460 }
1461
1462 if (slic_file) {
1463 memcpy(oem_id, slic_file->oem_id, 6);
1464 memcpy(oem_table_id, slic_file->oem_table_id, 8);
1465 } else {
1466 memcpy(oem_id, OEM_ID, 6);
1467 memcpy(oem_table_id, ACPI_TABLE_CREATOR, 8);
1468 }
1469
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001470 printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx.\n", start);
1471
1472 /* We need at least an RSDP and an RSDT Table */
1473 rsdp = (acpi_rsdp_t *) current;
1474 current += sizeof(acpi_rsdp_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001475 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001476 rsdt = (acpi_rsdt_t *) current;
1477 current += sizeof(acpi_rsdt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001478 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001479 xsdt = (acpi_xsdt_t *) current;
1480 current += sizeof(acpi_xsdt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001481 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001482
1483 /* clear all table memory */
1484 memset((void *) start, 0, current - start);
1485
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001486 acpi_write_rsdp(rsdp, rsdt, xsdt, oem_id);
1487 acpi_write_rsdt(rsdt, oem_id, oem_table_id);
1488 acpi_write_xsdt(xsdt, oem_id, oem_table_id);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001489
1490 printk(BIOS_DEBUG, "ACPI: * FACS\n");
Felix Heldc4697122019-06-20 13:50:17 +02001491 current = ALIGN_UP(current, 64);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001492 facs = (acpi_facs_t *) current;
1493 current += sizeof(acpi_facs_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001494 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001495 acpi_create_facs(facs);
1496
1497 printk(BIOS_DEBUG, "ACPI: * DSDT\n");
1498 dsdt = (acpi_header_t *) current;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001499 memcpy(dsdt, dsdt_file, sizeof(acpi_header_t));
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001500 if (dsdt->length >= sizeof(acpi_header_t)) {
1501 current += sizeof(acpi_header_t);
1502
1503 acpigen_set_current((char *) current);
Kyösti Mälkki2ab4a962020-06-30 11:41:47 +03001504
Kyösti Mälkki37eb24b2021-01-11 18:40:37 +02001505 if (CONFIG(ACPI_SOC_NVS))
1506 acpi_fill_gnvs();
Kyösti Mälkki2ab4a962020-06-30 11:41:47 +03001507
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001508 for (dev = all_devices; dev; dev = dev->next)
Nico Huber68680dd2020-03-31 17:34:52 +02001509 if (dev->ops && dev->ops->acpi_inject_dsdt)
1510 dev->ops->acpi_inject_dsdt(dev);
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001511 current = (unsigned long) acpigen_get_current();
1512 memcpy((char *)current,
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001513 (char *)dsdt_file + sizeof(acpi_header_t),
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001514 dsdt->length - sizeof(acpi_header_t));
1515 current += dsdt->length - sizeof(acpi_header_t);
1516
1517 /* (Re)calculate length and checksum. */
1518 dsdt->length = current - (unsigned long)dsdt;
1519 dsdt->checksum = 0;
1520 dsdt->checksum = acpi_checksum((void *)dsdt, dsdt->length);
1521 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001522
Aaron Durbin07a1b282015-12-10 17:07:38 -06001523 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001524
1525 printk(BIOS_DEBUG, "ACPI: * FADT\n");
1526 fadt = (acpi_fadt_t *) current;
1527 current += sizeof(acpi_fadt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001528 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001529
1530 acpi_create_fadt(fadt, facs, dsdt);
1531 acpi_add_table(rsdp, fadt);
1532
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001533 if (slic_file) {
1534 printk(BIOS_DEBUG, "ACPI: * SLIC\n");
1535 slic = (acpi_header_t *)current;
1536 memcpy(slic, slic_file, slic_file->length);
1537 current += slic_file->length;
Aaron Durbin07a1b282015-12-10 17:07:38 -06001538 current = acpi_align_current(current);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001539 acpi_add_table(rsdp, slic);
1540 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001541
1542 printk(BIOS_DEBUG, "ACPI: * SSDT\n");
1543 ssdt = (acpi_header_t *)current;
1544 acpi_create_ssdt_generator(ssdt, ACPI_TABLE_CREATOR);
Vladimir Serbinenko9310df82014-10-10 20:40:41 +02001545 if (ssdt->length > sizeof(acpi_header_t)) {
1546 current += ssdt->length;
1547 acpi_add_table(rsdp, ssdt);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001548 current = acpi_align_current(current);
Vladimir Serbinenko9310df82014-10-10 20:40:41 +02001549 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001550
1551 printk(BIOS_DEBUG, "ACPI: * MCFG\n");
1552 mcfg = (acpi_mcfg_t *) current;
1553 acpi_create_mcfg(mcfg);
1554 if (mcfg->header.length > sizeof(acpi_mcfg_t)) {
1555 current += mcfg->header.length;
Aaron Durbin07a1b282015-12-10 17:07:38 -06001556 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001557 acpi_add_table(rsdp, mcfg);
1558 }
1559
Julius Wernercd49cce2019-03-05 16:53:33 -08001560 if (CONFIG(TPM1)) {
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001561 printk(BIOS_DEBUG, "ACPI: * TCPA\n");
1562 tcpa = (acpi_tcpa_t *) current;
1563 acpi_create_tcpa(tcpa);
1564 if (tcpa->header.length >= sizeof(acpi_tcpa_t)) {
1565 current += tcpa->header.length;
1566 current = acpi_align_current(current);
1567 acpi_add_table(rsdp, tcpa);
1568 }
1569 }
1570
Julius Wernercd49cce2019-03-05 16:53:33 -08001571 if (CONFIG(TPM2)) {
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001572 printk(BIOS_DEBUG, "ACPI: * TPM2\n");
1573 tpm2 = (acpi_tpm2_t *) current;
1574 acpi_create_tpm2(tpm2);
1575 if (tpm2->header.length >= sizeof(acpi_tpm2_t)) {
1576 current += tpm2->header.length;
1577 current = acpi_align_current(current);
1578 acpi_add_table(rsdp, tpm2);
1579 }
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +02001580 }
1581
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001582 if (CONFIG(ACPI_LPIT)) {
1583 printk(BIOS_DEBUG, "ACPI: * LPIT\n");
1584
1585 lpit = (acpi_lpit_t *)current;
1586 acpi_create_lpit(lpit);
1587 if (lpit->header.length >= sizeof(acpi_lpit_t)) {
1588 current += lpit->header.length;
1589 current = acpi_align_current(current);
1590 acpi_add_table(rsdp, lpit);
1591 }
1592 }
1593
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001594 printk(BIOS_DEBUG, "ACPI: * MADT\n");
1595
1596 madt = (acpi_madt_t *) current;
1597 acpi_create_madt(madt);
1598 if (madt->header.length > sizeof(acpi_madt_t)) {
Lee Leahy024b13d2017-03-16 13:41:11 -07001599 current += madt->header.length;
1600 acpi_add_table(rsdp, madt);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001601 }
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001602
Aaron Durbin07a1b282015-12-10 17:07:38 -06001603 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001604
Francois Toguo522e0db2021-01-21 09:55:19 -08001605 if (acpi_is_boot_error_src_present()) {
1606 void *region;
1607 size_t size;
1608 printk(BIOS_DEBUG, "ACPI: * BERT\n");
1609 bert = (acpi_bert_t *) current;
1610 acpi_soc_fill_bert(bert, &region, &size);
1611 acpi_write_bert(bert, (uintptr_t)region, size);
1612 if (bert->header.length >= sizeof(acpi_bert_t)) {
1613 current += bert->header.length;
1614 acpi_add_table(rsdp, bert);
1615 }
1616 current = acpi_align_current(current);
1617 }
1618
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001619 printk(BIOS_DEBUG, "current = %lx\n", current);
1620
1621 for (dev = all_devices; dev; dev = dev->next) {
1622 if (dev->ops && dev->ops->write_acpi_tables) {
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001623 current = dev->ops->write_acpi_tables(dev, current,
1624 rsdp);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001625 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001626 }
1627 }
1628
1629 printk(BIOS_INFO, "ACPI: done.\n");
1630 return current;
1631}
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001632
Rudolf Marek33cafe52009-04-13 18:07:02 +00001633static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp)
1634{
1635 if (strncmp((char *)rsdp, RSDP_SIG, sizeof(RSDP_SIG) - 1) != 0)
1636 return NULL;
1637
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001638 printk(BIOS_DEBUG, "Looking on %p for valid checksum\n", rsdp);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001639
1640 if (acpi_checksum((void *)rsdp, 20) != 0)
1641 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001642 printk(BIOS_DEBUG, "Checksum 1 passed\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001643
1644 if ((rsdp->revision > 1) && (acpi_checksum((void *)rsdp,
1645 rsdp->length) != 0))
1646 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001647 printk(BIOS_DEBUG, "Checksum 2 passed all OK\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001648
1649 return rsdp;
1650}
1651
Rudolf Marek33cafe52009-04-13 18:07:02 +00001652void *acpi_find_wakeup_vector(void)
1653{
1654 char *p, *end;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001655 acpi_rsdt_t *rsdt;
1656 acpi_facs_t *facs;
Martin Rothc5f49262012-12-14 19:17:55 -07001657 acpi_fadt_t *fadt = NULL;
Kyösti Mälkki072d436b2016-06-17 08:44:40 +03001658 acpi_rsdp_t *rsdp = NULL;
Uwe Hermann622824c2010-11-19 15:14:42 +00001659 void *wake_vec;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001660 int i;
1661
Kyösti Mälkkib8c7ea02020-11-17 16:41:38 +02001662 if (!acpi_is_wakeup_s3())
Rudolf Marek33cafe52009-04-13 18:07:02 +00001663 return NULL;
1664
Uwe Hermann622824c2010-11-19 15:14:42 +00001665 printk(BIOS_DEBUG, "Trying to find the wakeup vector...\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001666
Uwe Hermann622824c2010-11-19 15:14:42 +00001667 /* Find RSDP. */
1668 for (p = (char *)0xe0000; p < (char *)0xfffff; p += 16) {
Lee Leahy0b5678f2017-03-16 16:01:40 -07001669 rsdp = valid_rsdp((acpi_rsdp_t *)p);
1670 if (rsdp)
Rudolf Marek33cafe52009-04-13 18:07:02 +00001671 break;
1672 }
1673
Angel Pons98a68ad2018-11-04 12:25:25 +01001674 if (rsdp == NULL) {
1675 printk(BIOS_ALERT,
1676 "No RSDP found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001677 return NULL;
Angel Pons98a68ad2018-11-04 12:25:25 +01001678 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001679
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001680 printk(BIOS_DEBUG, "RSDP found at %p\n", rsdp);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001681 rsdt = (acpi_rsdt_t *)(uintptr_t)rsdp->rsdt_address;
Stefan Reinauer14e22772010-04-27 06:56:47 +00001682
Uwe Hermann622824c2010-11-19 15:14:42 +00001683 end = (char *)rsdt + rsdt->header.length;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001684 printk(BIOS_DEBUG, "RSDT found at %p ends at %p\n", rsdt, end);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001685
Uwe Hermann622824c2010-11-19 15:14:42 +00001686 for (i = 0; ((char *)&rsdt->entry[i]) < end; i++) {
Stefan Reinauer71a30182015-07-30 16:28:13 -07001687 fadt = (acpi_fadt_t *)(uintptr_t)rsdt->entry[i];
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001688 if (strncmp((char *)fadt, "FACP", 4) == 0)
Rudolf Marek33cafe52009-04-13 18:07:02 +00001689 break;
1690 fadt = NULL;
1691 }
1692
Angel Pons98a68ad2018-11-04 12:25:25 +01001693 if (fadt == NULL) {
1694 printk(BIOS_ALERT,
1695 "No FADT found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001696 return NULL;
Angel Pons98a68ad2018-11-04 12:25:25 +01001697 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001698
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001699 printk(BIOS_DEBUG, "FADT found at %p\n", fadt);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001700 facs = (acpi_facs_t *)(uintptr_t)fadt->firmware_ctrl;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001701
Stefan Reinauer7e9771c2009-04-22 08:17:38 +00001702 if (facs == NULL) {
Angel Pons98a68ad2018-11-04 12:25:25 +01001703 printk(BIOS_ALERT,
1704 "No FACS found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001705 return NULL;
Stefan Reinauer7e9771c2009-04-22 08:17:38 +00001706 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001707
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001708 printk(BIOS_DEBUG, "FACS found at %p\n", facs);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001709 wake_vec = (void *)(uintptr_t)facs->firmware_waking_vector;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001710 printk(BIOS_DEBUG, "OS waking vector is %p\n", wake_vec);
Uwe Hermann622824c2010-11-19 15:14:42 +00001711
Rudolf Marek33cafe52009-04-13 18:07:02 +00001712 return wake_vec;
1713}
1714
Aaron Durbin64031672018-04-21 14:45:32 -06001715__weak int acpi_get_gpe(int gpe)
Duncan Laurie1f6e6812016-09-19 12:04:19 -07001716{
1717 return -1; /* implemented by SOC */
1718}
Marc Jones93a51762018-08-22 18:57:24 -06001719
1720int get_acpi_table_revision(enum acpi_tables table)
1721{
1722 switch (table) {
1723 case FADT:
Patrick Rudolphc02bda02020-02-28 10:19:41 +01001724 return ACPI_FADT_REV_ACPI_6_0;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001725 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 +01001726 return 3;
Marc Jones93a51762018-08-22 18:57:24 -06001727 case MCFG:
1728 return 1;
1729 case TCPA:
1730 return 2;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001731 case TPM2:
1732 return 4;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001733 case SSDT: /* ACPI 3.0 upto 6.3: 2 */
Marc Jones93a51762018-08-22 18:57:24 -06001734 return 2;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001735 case SRAT: /* ACPI 2.0: 1, ACPI 3.0: 2, ACPI 4.0 upto 6.3: 3 */
Marc Jones93a51762018-08-22 18:57:24 -06001736 return 1; /* TODO Should probably be upgraded to 2 */
1737 case DMAR:
1738 return 1;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001739 case SLIT: /* ACPI 2.0 upto 6.3: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06001740 return 1;
Patrick Rudolph9d98e5a2019-06-14 19:00:04 +02001741 case SPMI: /* IMPI 2.0 */
1742 return 5;
Marc Jones93a51762018-08-22 18:57:24 -06001743 case HPET: /* Currently 1. Table added in ACPI 2.0. */
1744 return 1;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001745 case VFCT: /* ACPI 2.0/3.0/4.0: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06001746 return 1;
1747 case IVRS:
Jason Glenesk1916f892020-07-24 02:51:30 -07001748 return IVRS_FORMAT_MIXED;
Marc Jones93a51762018-08-22 18:57:24 -06001749 case DBG2:
1750 return 0;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001751 case FACS: /* ACPI 2.0/3.0: 1, ACPI 4.0 upto 6.3: 2 */
Marc Jones93a51762018-08-22 18:57:24 -06001752 return 1;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001753 case RSDT: /* ACPI 1.0 upto 6.3: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06001754 return 1;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001755 case XSDT: /* ACPI 2.0 upto 6.3: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06001756 return 1;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001757 case RSDP: /* ACPI 2.0 upto 6.3: 2 */
Marc Jones93a51762018-08-22 18:57:24 -06001758 return 2;
1759 case HEST:
1760 return 1;
1761 case NHLT:
1762 return 5;
Marshall Dawson44705c62018-09-04 13:47:15 -06001763 case BERT:
1764 return 1;
Jason Glenesk61624b22020-11-02 20:06:23 -08001765 case CRAT:
1766 return 1;
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001767 case LPIT: /* ACPI 5.1 up to 6.3: 0 */
1768 return 0;
Marc Jones93a51762018-08-22 18:57:24 -06001769 default:
1770 return -1;
1771 }
1772 return -1;
1773}