blob: e211558f62f18d81980ef0a535760df8b31f7dba [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
Furquan Shaikh76cedd22020-05-02 10:24:23 -070016#include <acpi/acpi.h>
17#include <acpi/acpi_ivrs.h>
18#include <acpi/acpigen.h>
Felix Held972d9f22022-02-23 16:32:20 +010019#include <arch/hpet.h>
Jianjun Wang8565b94a2022-03-02 10:20:26 +080020#include <cbfs.h>
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000021#include <cbmem.h>
Elyes HAOUASe2d152c2019-06-21 07:06:50 +020022#include <commonlib/helpers.h>
Jianjun Wang8565b94a2022-03-02 10:20:26 +080023#include <commonlib/sort.h>
24#include <console/console.h>
Duncan Laurie11290c42012-10-03 19:07:05 -070025#include <cpu/cpu.h>
Jianjun Wang8565b94a2022-03-02 10:20:26 +080026#include <device/mmio.h>
27#include <device/pci.h>
28#include <pc80/mc146818rtc.h>
29#include <string.h>
Elyes HAOUAScdd2f632021-02-11 19:37:11 +010030#include <types.h>
Elyes HAOUAS26071aa2019-02-15 08:21:33 +010031#include <version.h>
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000032
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +020033static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp);
34
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000035u8 acpi_checksum(u8 *table, u32 length)
Stefan Reinauer688b3852004-01-28 16:56:14 +000036{
Uwe Hermann622824c2010-11-19 15:14:42 +000037 u8 ret = 0;
Stefan Reinauer688b3852004-01-28 16:56:14 +000038 while (length--) {
39 ret += *table;
40 table++;
41 }
Stefan Reinauera7648c22004-01-29 17:31:34 +000042 return -ret;
Stefan Reinauer688b3852004-01-28 16:56:14 +000043}
44
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000045/**
Uwe Hermann622824c2010-11-19 15:14:42 +000046 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
47 * and checksum.
Stefan Reinauer06feb882004-02-03 16:11:35 +000048 */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000049void acpi_add_table(acpi_rsdp_t *rsdp, void *table)
Stefan Reinauer688b3852004-01-28 16:56:14 +000050{
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000051 int i, entries_num;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000052 acpi_rsdt_t *rsdt;
53 acpi_xsdt_t *xsdt = NULL;
54
Uwe Hermann622824c2010-11-19 15:14:42 +000055 /* The RSDT is mandatory... */
Stefan Reinauerdefee172015-06-18 01:11:19 -070056 rsdt = (acpi_rsdt_t *)(uintptr_t)rsdp->rsdt_address;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000057
Uwe Hermann622824c2010-11-19 15:14:42 +000058 /* ...while the XSDT is not. */
59 if (rsdp->xsdt_address)
Stefan Reinauerdefee172015-06-18 01:11:19 -070060 xsdt = (acpi_xsdt_t *)((uintptr_t)rsdp->xsdt_address);
Stefan Reinauer14e22772010-04-27 06:56:47 +000061
Uwe Hermann622824c2010-11-19 15:14:42 +000062 /* This should always be MAX_ACPI_TABLES. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000063 entries_num = ARRAY_SIZE(rsdt->entry);
Stefan Reinauer14e22772010-04-27 06:56:47 +000064
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000065 for (i = 0; i < entries_num; i++) {
Uwe Hermann622824c2010-11-19 15:14:42 +000066 if (rsdt->entry[i] == 0)
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000067 break;
Stefan Reinauer688b3852004-01-28 16:56:14 +000068 }
69
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000070 if (i >= entries_num) {
Uwe Hermann622824c2010-11-19 15:14:42 +000071 printk(BIOS_ERR, "ACPI: Error: Could not add ACPI table, "
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +030072 "too many tables.\n");
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000073 return;
74 }
75
Uwe Hermann622824c2010-11-19 15:14:42 +000076 /* Add table to the RSDT. */
Stefan Reinauerdefee172015-06-18 01:11:19 -070077 rsdt->entry[i] = (uintptr_t)table;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000078
Uwe Hermann622824c2010-11-19 15:14:42 +000079 /* Fix RSDT length or the kernel will assume invalid entries. */
80 rsdt->header.length = sizeof(acpi_header_t) + (sizeof(u32) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000081
Uwe Hermann622824c2010-11-19 15:14:42 +000082 /* Re-calculate checksum. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000083 rsdt->header.checksum = 0; /* Hope this won't get optimized away */
Uwe Hermann622824c2010-11-19 15:14:42 +000084 rsdt->header.checksum = acpi_checksum((u8 *)rsdt, rsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000085
Uwe Hermann622824c2010-11-19 15:14:42 +000086 /*
87 * And now the same thing for the XSDT. We use the same index as for
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000088 * now we want the XSDT and RSDT to always be in sync in coreboot.
89 */
90 if (xsdt) {
Uwe Hermann622824c2010-11-19 15:14:42 +000091 /* Add table to the XSDT. */
Stefan Reinauerdefee172015-06-18 01:11:19 -070092 xsdt->entry[i] = (u64)(uintptr_t)table;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000093
Uwe Hermann622824c2010-11-19 15:14:42 +000094 /* Fix XSDT length. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000095 xsdt->header.length = sizeof(acpi_header_t) +
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +030096 (sizeof(u64) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000097
Uwe Hermann622824c2010-11-19 15:14:42 +000098 /* Re-calculate checksum. */
99 xsdt->header.checksum = 0;
100 xsdt->header.checksum = acpi_checksum((u8 *)xsdt,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300101 xsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000102 }
103
Uwe Hermann622824c2010-11-19 15:14:42 +0000104 printk(BIOS_DEBUG, "ACPI: added table %d/%d, length now %d\n",
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300105 i + 1, entries_num, rsdt->header.length);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000106}
107
Uwe Hermann622824c2010-11-19 15:14:42 +0000108int acpi_create_mcfg_mmconfig(acpi_mcfg_mmconfig_t *mmconfig, u32 base,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300109 u16 seg_nr, u8 start, u8 end)
Stefan Reinauer26d431a2009-01-20 19:17:51 +0000110{
Vladimir Serbinenko60fccdc2014-10-05 11:05:16 +0200111 memset(mmconfig, 0, sizeof(*mmconfig));
Rudolf Mareke6409f22007-11-03 12:50:26 +0000112 mmconfig->base_address = base;
113 mmconfig->base_reserved = 0;
114 mmconfig->pci_segment_group_number = seg_nr;
115 mmconfig->start_bus_number = start;
116 mmconfig->end_bus_number = end;
Uwe Hermann622824c2010-11-19 15:14:42 +0000117
118 return sizeof(acpi_mcfg_mmconfig_t);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000119}
120
Stefan Reinauer777224c2005-01-19 14:06:41 +0000121int acpi_create_madt_lapic(acpi_madt_lapic_t *lapic, u8 cpu, u8 apic)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000122{
Himanshu Sahdev8c09b822019-10-21 18:50:50 +0530123 lapic->type = LOCAL_APIC; /* Local APIC structure */
Uwe Hermann622824c2010-11-19 15:14:42 +0000124 lapic->length = sizeof(acpi_madt_lapic_t);
125 lapic->flags = (1 << 0); /* Processor/LAPIC enabled */
126 lapic->processor_id = cpu;
127 lapic->apic_id = apic;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000128
Uwe Hermann622824c2010-11-19 15:14:42 +0000129 return lapic->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000130}
131
Patrick Rudolph56a3ef22020-03-24 17:29:49 +0100132int acpi_create_madt_lx2apic(acpi_madt_lx2apic_t *lapic, u32 cpu, u32 apic)
133{
134 lapic->type = LOCAL_X2APIC; /* Local APIC structure */
135 lapic->reserved = 0;
136 lapic->length = sizeof(acpi_madt_lx2apic_t);
137 lapic->flags = (1 << 0); /* Processor/LAPIC enabled */
138 lapic->processor_id = cpu;
139 lapic->x2apic_id = apic;
140
141 return lapic->length;
142}
143
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000144unsigned long acpi_create_madt_lapics(unsigned long current)
145{
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100146 struct device *cpu;
Werner Zeh423adfb2019-03-06 09:31:02 +0100147 int index, apic_ids[CONFIG_MAX_CPUS] = {0}, num_cpus = 0;
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000148
Uwe Hermann622824c2010-11-19 15:14:42 +0000149 for (cpu = all_devices; cpu; cpu = cpu->next) {
Fabio Aiuto45aae7f2022-09-23 16:51:34 +0200150 if (!is_enabled_cpu(cpu))
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000151 continue;
Werner Zehdb561e62019-02-19 13:39:56 +0100152 if (num_cpus >= ARRAY_SIZE(apic_ids))
153 break;
154 apic_ids[num_cpus++] = cpu->path.apic.apic_id;
155 }
Werner Zehfedb36e2019-03-12 07:07:50 +0100156 if (num_cpus > 1)
157 bubblesort(apic_ids, num_cpus, NUM_ASCENDING);
Werner Zehdb561e62019-02-19 13:39:56 +0100158 for (index = 0; index < num_cpus; index++) {
Patrick Rudolph56a3ef22020-03-24 17:29:49 +0100159 if (apic_ids[index] < 0xff)
160 current += acpi_create_madt_lapic((acpi_madt_lapic_t *)current,
161 index, apic_ids[index]);
162 else
163 current += acpi_create_madt_lx2apic((acpi_madt_lx2apic_t *)current,
164 index, apic_ids[index]);
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000165 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000166
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000167 return current;
168}
169
Uwe Hermann622824c2010-11-19 15:14:42 +0000170int acpi_create_madt_ioapic(acpi_madt_ioapic_t *ioapic, u8 id, u32 addr,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300171 u32 gsi_base)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000172{
Himanshu Sahdev8c09b822019-10-21 18:50:50 +0530173 ioapic->type = IO_APIC; /* I/O APIC structure */
Uwe Hermann622824c2010-11-19 15:14:42 +0000174 ioapic->length = sizeof(acpi_madt_ioapic_t);
175 ioapic->reserved = 0x00;
176 ioapic->gsi_base = gsi_base;
177 ioapic->ioapic_id = id;
178 ioapic->ioapic_addr = addr;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000179
Uwe Hermann622824c2010-11-19 15:14:42 +0000180 return ioapic->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000181}
182
Stefan Reinauer777224c2005-01-19 14:06:41 +0000183int acpi_create_madt_irqoverride(acpi_madt_irqoverride_t *irqoverride,
Stefan Reinauer06feb882004-02-03 16:11:35 +0000184 u8 bus, u8 source, u32 gsirq, u16 flags)
185{
Himanshu Sahdev8c09b822019-10-21 18:50:50 +0530186 irqoverride->type = IRQ_SOURCE_OVERRIDE; /* Interrupt source override */
Uwe Hermann622824c2010-11-19 15:14:42 +0000187 irqoverride->length = sizeof(acpi_madt_irqoverride_t);
188 irqoverride->bus = bus;
189 irqoverride->source = source;
190 irqoverride->gsirq = gsirq;
191 irqoverride->flags = flags;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000192
Uwe Hermann622824c2010-11-19 15:14:42 +0000193 return irqoverride->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000194}
195
Stefan Reinauer777224c2005-01-19 14:06:41 +0000196int acpi_create_madt_lapic_nmi(acpi_madt_lapic_nmi_t *lapic_nmi, u8 cpu,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300197 u16 flags, u8 lint)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000198{
Himanshu Sahdev8c09b822019-10-21 18:50:50 +0530199 lapic_nmi->type = LOCAL_APIC_NMI; /* Local APIC NMI structure */
Uwe Hermann622824c2010-11-19 15:14:42 +0000200 lapic_nmi->length = sizeof(acpi_madt_lapic_nmi_t);
201 lapic_nmi->flags = flags;
202 lapic_nmi->processor_id = cpu;
203 lapic_nmi->lint = lint;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000204
Uwe Hermann622824c2010-11-19 15:14:42 +0000205 return lapic_nmi->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000206}
207
Patrick Rudolph56a3ef22020-03-24 17:29:49 +0100208int acpi_create_madt_lx2apic_nmi(acpi_madt_lx2apic_nmi_t *lapic_nmi, u32 cpu,
209 u16 flags, u8 lint)
210{
211 lapic_nmi->type = LOCAL_X2APIC_NMI; /* Local APIC NMI structure */
212 lapic_nmi->length = sizeof(acpi_madt_lx2apic_nmi_t);
213 lapic_nmi->flags = flags;
214 lapic_nmi->processor_id = cpu;
215 lapic_nmi->lint = lint;
216 lapic_nmi->reserved[0] = 0;
217 lapic_nmi->reserved[1] = 0;
218 lapic_nmi->reserved[2] = 0;
219
220 return lapic_nmi->length;
221}
222
Stefan Reinauer777224c2005-01-19 14:06:41 +0000223void acpi_create_madt(acpi_madt_t *madt)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000224{
Uwe Hermann622824c2010-11-19 15:14:42 +0000225 acpi_header_t *header = &(madt->header);
226 unsigned long current = (unsigned long)madt + sizeof(acpi_madt_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000227
Stefan Reinauer06feb882004-02-03 16:11:35 +0000228 memset((void *)madt, 0, sizeof(acpi_madt_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000229
John Zhao2ba303e2019-05-28 16:48:14 -0700230 if (!header)
231 return;
232
Uwe Hermann622824c2010-11-19 15:14:42 +0000233 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000234 memcpy(header->signature, "APIC", 4);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000235 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000236 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000237 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000238
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100239 header->asl_compiler_revision = asl_revision;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000240 header->length = sizeof(acpi_madt_t);
Marc Jones93a51762018-08-22 18:57:24 -0600241 header->revision = get_acpi_table_revision(MADT);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000242
Furquan Shaikhb1859a62020-04-30 21:27:47 -0700243 madt->lapic_addr = cpu_get_lapic_addr();
Nico Huber9df72e02018-11-24 18:25:50 +0100244 if (CONFIG(ACPI_HAVE_PCAT_8259))
245 madt->flags |= 1;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000246
Stefan Reinauerf622d592005-11-26 16:56:05 +0000247 current = acpi_fill_madt(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000248
Uwe Hermann622824c2010-11-19 15:14:42 +0000249 /* (Re)calculate length and checksum. */
250 header->length = current - (unsigned long)madt;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000251
Uwe Hermann622824c2010-11-19 15:14:42 +0000252 header->checksum = acpi_checksum((void *)madt, header->length);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000253}
254
Kyösti Mälkkib54388d2021-02-14 15:09:45 +0200255static unsigned long acpi_fill_mcfg(unsigned long current)
256{
257 current += acpi_create_mcfg_mmconfig((acpi_mcfg_mmconfig_t *)current,
Shelley Chen4e9bb332021-10-20 15:43:45 -0700258 CONFIG_ECAM_MMCONF_BASE_ADDRESS, 0, 0,
259 CONFIG_ECAM_MMCONF_BUS_NUMBER - 1);
Kyösti Mälkkib54388d2021-02-14 15:09:45 +0200260 return current;
261}
262
Uwe Hermann622824c2010-11-19 15:14:42 +0000263/* MCFG is defined in the PCI Firmware Specification 3.0. */
Rudolf Mareke6409f22007-11-03 12:50:26 +0000264void acpi_create_mcfg(acpi_mcfg_t *mcfg)
265{
Uwe Hermann622824c2010-11-19 15:14:42 +0000266 acpi_header_t *header = &(mcfg->header);
267 unsigned long current = (unsigned long)mcfg + sizeof(acpi_mcfg_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000268
Rudolf Mareke6409f22007-11-03 12:50:26 +0000269 memset((void *)mcfg, 0, sizeof(acpi_mcfg_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000270
John Zhao2ba303e2019-05-28 16:48:14 -0700271 if (!header)
272 return;
273
Uwe Hermann622824c2010-11-19 15:14:42 +0000274 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000275 memcpy(header->signature, "MCFG", 4);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000276 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000277 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000278 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000279
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100280 header->asl_compiler_revision = asl_revision;
Rudolf Mareke6409f22007-11-03 12:50:26 +0000281 header->length = sizeof(acpi_mcfg_t);
Marc Jones93a51762018-08-22 18:57:24 -0600282 header->revision = get_acpi_table_revision(MCFG);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000283
Shelley Chen4e9bb332021-10-20 15:43:45 -0700284 if (CONFIG(ECAM_MMCONF_SUPPORT))
Kyösti Mälkkib54388d2021-02-14 15:09:45 +0200285 current = acpi_fill_mcfg(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000286
Uwe Hermann622824c2010-11-19 15:14:42 +0000287 /* (Re)calculate length and checksum. */
288 header->length = current - (unsigned long)mcfg;
289 header->checksum = acpi_checksum((void *)mcfg, header->length);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000290}
291
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200292static void *get_tcpa_log(u32 *size)
293{
294 const struct cbmem_entry *ce;
295 const u32 tcpa_default_log_len = 0x10000;
296 void *lasa;
Philipp Deppenwiese791ba972018-07-30 01:15:20 +0200297 ce = cbmem_entry_find(CBMEM_ID_TCPA_TCG_LOG);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200298 if (ce) {
299 lasa = cbmem_entry_start(ce);
300 *size = cbmem_entry_size(ce);
301 printk(BIOS_DEBUG, "TCPA log found at %p\n", lasa);
302 return lasa;
303 }
Philipp Deppenwiese791ba972018-07-30 01:15:20 +0200304 lasa = cbmem_add(CBMEM_ID_TCPA_TCG_LOG, tcpa_default_log_len);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200305 if (!lasa) {
306 printk(BIOS_ERR, "TCPA log creation failed\n");
307 return NULL;
308 }
309
310 printk(BIOS_DEBUG, "TCPA log created at %p\n", lasa);
Lee Leahy024b13d2017-03-16 13:41:11 -0700311 memset(lasa, 0, tcpa_default_log_len);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200312
313 *size = tcpa_default_log_len;
314 return lasa;
315}
316
317static void acpi_create_tcpa(acpi_tcpa_t *tcpa)
318{
319 acpi_header_t *header = &(tcpa->header);
320 u32 tcpa_log_len;
321 void *lasa;
322
323 memset((void *)tcpa, 0, sizeof(acpi_tcpa_t));
324
325 lasa = get_tcpa_log(&tcpa_log_len);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700326 if (!lasa)
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200327 return;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200328
John Zhao2ba303e2019-05-28 16:48:14 -0700329 if (!header)
330 return;
331
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200332 /* Fill out header fields. */
333 memcpy(header->signature, "TCPA", 4);
334 memcpy(header->oem_id, OEM_ID, 6);
335 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
336 memcpy(header->asl_compiler_id, ASLC, 4);
337
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100338 header->asl_compiler_revision = asl_revision;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200339 header->length = sizeof(acpi_tcpa_t);
Marc Jones93a51762018-08-22 18:57:24 -0600340 header->revision = get_acpi_table_revision(TCPA);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200341
342 tcpa->platform_class = 0;
343 tcpa->laml = tcpa_log_len;
Stefan Reinauerdefee172015-06-18 01:11:19 -0700344 tcpa->lasa = (uintptr_t) lasa;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200345
346 /* Calculate checksum. */
347 header->checksum = acpi_checksum((void *)tcpa, header->length);
348}
349
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100350static void *get_tpm2_log(u32 *size)
351{
352 const struct cbmem_entry *ce;
353 const u32 tpm2_default_log_len = 0x10000;
354 void *lasa;
355 ce = cbmem_entry_find(CBMEM_ID_TPM2_TCG_LOG);
356 if (ce) {
357 lasa = cbmem_entry_start(ce);
358 *size = cbmem_entry_size(ce);
359 printk(BIOS_DEBUG, "TPM2 log found at %p\n", lasa);
360 return lasa;
361 }
362 lasa = cbmem_add(CBMEM_ID_TPM2_TCG_LOG, tpm2_default_log_len);
363 if (!lasa) {
364 printk(BIOS_ERR, "TPM2 log creation failed\n");
365 return NULL;
366 }
367
368 printk(BIOS_DEBUG, "TPM2 log created at %p\n", lasa);
369 memset(lasa, 0, tpm2_default_log_len);
370
371 *size = tpm2_default_log_len;
372 return lasa;
373}
374
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200375static void acpi_create_tpm2(acpi_tpm2_t *tpm2)
376{
377 acpi_header_t *header = &(tpm2->header);
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100378 u32 tpm2_log_len;
379 void *lasa;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200380
381 memset((void *)tpm2, 0, sizeof(acpi_tpm2_t));
382
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100383 /*
384 * Some payloads like SeaBIOS depend on log area to use TPM2.
385 * Get the memory size and address of TPM2 log area or initialize it.
386 */
387 lasa = get_tpm2_log(&tpm2_log_len);
388 if (!lasa)
389 tpm2_log_len = 0;
390
John Zhao2ba303e2019-05-28 16:48:14 -0700391 if (!header)
392 return;
393
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200394 /* Fill out header fields. */
395 memcpy(header->signature, "TPM2", 4);
396 memcpy(header->oem_id, OEM_ID, 6);
397 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
398 memcpy(header->asl_compiler_id, ASLC, 4);
399
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100400 header->asl_compiler_revision = asl_revision;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200401 header->length = sizeof(acpi_tpm2_t);
402 header->revision = get_acpi_table_revision(TPM2);
403
404 /* Hard to detect for coreboot. Just set it to 0 */
405 tpm2->platform_class = 0;
Christian Walter26e0d4c2019-07-14 15:47:23 +0200406 if (CONFIG(CRB_TPM)) {
407 /* Must be set to 7 for CRB Support */
408 tpm2->control_area = CONFIG_CRB_TPM_BASE_ADDRESS + 0x40;
409 tpm2->start_method = 7;
410 } else {
411 /* Must be set to 0 for FIFO interface support */
412 tpm2->control_area = 0;
413 tpm2->start_method = 6;
414 }
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200415 memset(tpm2->msp, 0, sizeof(tpm2->msp));
416
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100417 /* Fill the log area size and start address fields. */
418 tpm2->laml = tpm2_log_len;
419 tpm2->lasa = (uintptr_t) lasa;
420
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200421 /* Calculate checksum. */
422 header->checksum = acpi_checksum((void *)tpm2, header->length);
423}
424
Duncan Laurie37319032016-05-26 12:47:05 -0700425static void acpi_ssdt_write_cbtable(void)
426{
427 const struct cbmem_entry *cbtable;
428 uintptr_t base;
429 uint32_t size;
430
431 cbtable = cbmem_entry_find(CBMEM_ID_CBTABLE);
432 if (!cbtable)
433 return;
434 base = (uintptr_t)cbmem_entry_start(cbtable);
435 size = cbmem_entry_size(cbtable);
436
437 acpigen_write_device("CTBL");
438 acpigen_write_coreboot_hid(COREBOOT_ACPI_ID_CBTABLE);
439 acpigen_write_name_integer("_UID", 0);
David Wu5dff3962018-03-21 16:48:53 +0800440 acpigen_write_STA(ACPI_STATUS_DEVICE_HIDDEN_ON);
Duncan Laurie37319032016-05-26 12:47:05 -0700441 acpigen_write_name("_CRS");
442 acpigen_write_resourcetemplate_header();
443 acpigen_write_mem32fixed(0, base, size);
444 acpigen_write_resourcetemplate_footer();
445 acpigen_pop_len();
446}
447
Myles Watson3fe6b702009-10-09 20:13:43 +0000448void acpi_create_ssdt_generator(acpi_header_t *ssdt, const char *oem_table_id)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000449{
Uwe Hermann622824c2010-11-19 15:14:42 +0000450 unsigned long current = (unsigned long)ssdt + sizeof(acpi_header_t);
451
Rudolf Marek293b5f52009-02-01 18:35:15 +0000452 memset((void *)ssdt, 0, sizeof(acpi_header_t));
Uwe Hermann622824c2010-11-19 15:14:42 +0000453
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000454 memcpy(&ssdt->signature, "SSDT", 4);
Marc Jones93a51762018-08-22 18:57:24 -0600455 ssdt->revision = get_acpi_table_revision(SSDT);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000456 memcpy(&ssdt->oem_id, OEM_ID, 6);
457 memcpy(&ssdt->oem_table_id, oem_table_id, 8);
458 ssdt->oem_revision = 42;
Uwe Hermann622824c2010-11-19 15:14:42 +0000459 memcpy(&ssdt->asl_compiler_id, ASLC, 4);
Elyes HAOUAS26071aa2019-02-15 08:21:33 +0100460 ssdt->asl_compiler_revision = asl_revision;
Rudolf Marek293b5f52009-02-01 18:35:15 +0000461 ssdt->length = sizeof(acpi_header_t);
462
Stefan Reinauer8dcd50b2009-03-06 17:24:29 +0000463 acpigen_set_current((char *) current);
Duncan Laurie37319032016-05-26 12:47:05 -0700464
465 /* Write object to declare coreboot tables */
466 acpi_ssdt_write_cbtable();
467
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200468 {
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100469 struct device *dev;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200470 for (dev = all_devices; dev; dev = dev->next)
Karthikeyan Ramasubramaniand1c0f952020-11-02 16:26:52 -0700471 if (dev->enabled && dev->ops && dev->ops->acpi_fill_ssdt)
Nico Huber68680dd2020-03-31 17:34:52 +0200472 dev->ops->acpi_fill_ssdt(dev);
Vladimir Serbinenko0a669912014-10-05 14:34:17 +0200473 current = (unsigned long) acpigen_get_current();
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200474 }
Rudolf Marek293b5f52009-02-01 18:35:15 +0000475
Uwe Hermann622824c2010-11-19 15:14:42 +0000476 /* (Re)calculate length and checksum. */
Rudolf Marek293b5f52009-02-01 18:35:15 +0000477 ssdt->length = current - (unsigned long)ssdt;
478 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
479}
480
Stefan Reinauerf622d592005-11-26 16:56:05 +0000481int acpi_create_srat_lapic(acpi_srat_lapic_t *lapic, u8 node, u8 apic)
482{
Myles Watson0b4c9f02009-03-10 18:06:47 +0000483 memset((void *)lapic, 0, sizeof(acpi_srat_lapic_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000484
Uwe Hermann622824c2010-11-19 15:14:42 +0000485 lapic->type = 0; /* Processor local APIC/SAPIC affinity structure */
486 lapic->length = sizeof(acpi_srat_lapic_t);
487 lapic->flags = (1 << 0); /* Enabled (the use of this structure). */
488 lapic->proximity_domain_7_0 = node;
489 /* TODO: proximity_domain_31_8, local SAPIC EID, clock domain. */
490 lapic->apic_id = apic;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000491
Uwe Hermann622824c2010-11-19 15:14:42 +0000492 return lapic->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000493}
494
Uwe Hermann622824c2010-11-19 15:14:42 +0000495int 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 +0300496 u32 flags)
Stefan Reinauerf622d592005-11-26 16:56:05 +0000497{
Uwe Hermann622824c2010-11-19 15:14:42 +0000498 mem->type = 1; /* Memory affinity structure */
499 mem->length = sizeof(acpi_srat_mem_t);
500 mem->base_address_low = (basek << 10);
501 mem->base_address_high = (basek >> (32 - 10));
502 mem->length_low = (sizek << 10);
503 mem->length_high = (sizek >> (32 - 10));
504 mem->proximity_domain = node;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000505 mem->flags = flags;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000506
Uwe Hermann622824c2010-11-19 15:14:42 +0000507 return mem->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000508}
509
Jonathan Zhang3164b642021-04-21 17:51:31 -0700510int acpi_create_srat_gia_pci(acpi_srat_gia_t *gia, u32 proximity_domain,
511 u16 seg, u8 bus, u8 dev, u8 func, u32 flags)
512{
513 gia->type = ACPI_SRAT_STRUCTURE_GIA;
514 gia->length = sizeof(acpi_srat_gia_t);
515 gia->proximity_domain = proximity_domain;
516 gia->dev_handle_type = ACPI_SRAT_GIA_DEV_HANDLE_PCI;
517 /* First two bytes has segment number */
518 memcpy(gia->dev_handle, &seg, 2);
519 gia->dev_handle[2] = bus; /* Byte 2 has bus number */
520 /* Byte 3 has bits 7:3 for dev, bits 2:0 for func */
521 gia->dev_handle[3] = PCI_SLOT(dev) | PCI_FUNC(func);
522 gia->flags = flags;
523
524 return gia->length;
525}
526
Uwe Hermann622824c2010-11-19 15:14:42 +0000527/* http://www.microsoft.com/whdc/system/sysinternals/sratdwn.mspx */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +0200528void acpi_create_srat(acpi_srat_t *srat,
529 unsigned long (*acpi_fill_srat)(unsigned long current))
Stefan Reinauerf622d592005-11-26 16:56:05 +0000530{
Uwe Hermann622824c2010-11-19 15:14:42 +0000531 acpi_header_t *header = &(srat->header);
532 unsigned long current = (unsigned long)srat + sizeof(acpi_srat_t);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000533
Uwe Hermann622824c2010-11-19 15:14:42 +0000534 memset((void *)srat, 0, sizeof(acpi_srat_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000535
John Zhao2ba303e2019-05-28 16:48:14 -0700536 if (!header)
537 return;
538
Uwe Hermann622824c2010-11-19 15:14:42 +0000539 /* Fill out header fields. */
540 memcpy(header->signature, "SRAT", 4);
541 memcpy(header->oem_id, OEM_ID, 6);
542 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
543 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000544
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100545 header->asl_compiler_revision = asl_revision;
Uwe Hermann622824c2010-11-19 15:14:42 +0000546 header->length = sizeof(acpi_srat_t);
Marc Jones93a51762018-08-22 18:57:24 -0600547 header->revision = get_acpi_table_revision(SRAT);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000548
Uwe Hermann622824c2010-11-19 15:14:42 +0000549 srat->resv = 1; /* Spec: Reserved to 1 for backwards compatibility. */
Stefan Reinauerf622d592005-11-26 16:56:05 +0000550
Uwe Hermann622824c2010-11-19 15:14:42 +0000551 current = acpi_fill_srat(current);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000552
Uwe Hermann622824c2010-11-19 15:14:42 +0000553 /* (Re)calculate length and checksum. */
554 header->length = current - (unsigned long)srat;
555 header->checksum = acpi_checksum((void *)srat, header->length);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000556}
557
Jonathan Zhang2a4e1f42021-04-01 11:43:37 -0700558int acpi_create_hmat_mpda(acpi_hmat_mpda_t *mpda, u32 initiator, u32 memory)
559{
560 memset((void *)mpda, 0, sizeof(acpi_hmat_mpda_t));
561
562 mpda->type = 0; /* Memory Proximity Domain Attributes structure */
563 mpda->length = sizeof(acpi_hmat_mpda_t);
564 /*
565 * Proximity Domain for Attached Initiator field is valid.
566 * Bit 1 and bit 2 are reserved since HMAT revision 2.
567 */
568 mpda->flags = (1 << 0);
569 mpda->proximity_domain_initiator = initiator;
570 mpda->proximity_domain_memory = memory;
571
572 return mpda->length;
573}
574
575void acpi_create_hmat(acpi_hmat_t *hmat,
576 unsigned long (*acpi_fill_hmat)(unsigned long current))
577{
578 acpi_header_t *header = &(hmat->header);
579 unsigned long current = (unsigned long)hmat + sizeof(acpi_hmat_t);
580
581 memset((void *)hmat, 0, sizeof(acpi_hmat_t));
582
583 if (!header)
584 return;
585
586 /* Fill out header fields. */
587 memcpy(header->signature, "HMAT", 4);
588 memcpy(header->oem_id, OEM_ID, 6);
589 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
590 memcpy(header->asl_compiler_id, ASLC, 4);
591
592 header->asl_compiler_revision = asl_revision;
593 header->length = sizeof(acpi_hmat_t);
594 header->revision = get_acpi_table_revision(HMAT);
595
596 current = acpi_fill_hmat(current);
597
598 /* (Re)calculate length and checksum. */
599 header->length = current - (unsigned long)hmat;
600 header->checksum = acpi_checksum((void *)hmat, header->length);
601}
602
Nico Hubere561f352015-10-26 11:51:25 +0100603void acpi_create_dmar(acpi_dmar_t *dmar, enum dmar_flags flags,
Lee Leahy024b13d2017-03-16 13:41:11 -0700604 unsigned long (*acpi_fill_dmar)(unsigned long))
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200605{
606 acpi_header_t *header = &(dmar->header);
607 unsigned long current = (unsigned long)dmar + sizeof(acpi_dmar_t);
608
609 memset((void *)dmar, 0, sizeof(acpi_dmar_t));
610
John Zhao2ba303e2019-05-28 16:48:14 -0700611 if (!header)
612 return;
613
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200614 /* Fill out header fields. */
615 memcpy(header->signature, "DMAR", 4);
616 memcpy(header->oem_id, OEM_ID, 6);
617 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
618 memcpy(header->asl_compiler_id, ASLC, 4);
619
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100620 header->asl_compiler_revision = asl_revision;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200621 header->length = sizeof(acpi_dmar_t);
Marc Jones93a51762018-08-22 18:57:24 -0600622 header->revision = get_acpi_table_revision(DMAR);
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200623
Jacob Laskaf3f654d2016-03-15 21:53:27 -0500624 dmar->host_address_width = cpu_phys_address_size() - 1;
Nico Hubere561f352015-10-26 11:51:25 +0100625 dmar->flags = flags;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200626
627 current = acpi_fill_dmar(current);
628
629 /* (Re)calculate length and checksum. */
630 header->length = current - (unsigned long)dmar;
631 header->checksum = acpi_checksum((void *)dmar, header->length);
632}
633
634unsigned long acpi_create_dmar_drhd(unsigned long current, u8 flags,
Matt DeVillier7866d492018-03-29 14:59:57 +0200635 u16 segment, u64 bar)
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200636{
637 dmar_entry_t *drhd = (dmar_entry_t *)current;
638 memset(drhd, 0, sizeof(*drhd));
639 drhd->type = DMAR_DRHD;
640 drhd->length = sizeof(*drhd); /* will be fixed up later */
641 drhd->flags = flags;
642 drhd->segment = segment;
643 drhd->bar = bar;
644
645 return drhd->length;
646}
647
Matt DeVillier7866d492018-03-29 14:59:57 +0200648unsigned long acpi_create_dmar_rmrr(unsigned long current, u16 segment,
649 u64 bar, u64 limit)
650{
651 dmar_rmrr_entry_t *rmrr = (dmar_rmrr_entry_t *)current;
652 memset(rmrr, 0, sizeof(*rmrr));
653 rmrr->type = DMAR_RMRR;
654 rmrr->length = sizeof(*rmrr); /* will be fixed up later */
655 rmrr->segment = segment;
656 rmrr->bar = bar;
657 rmrr->limit = limit;
658
659 return rmrr->length;
660}
661
Werner Zehd4d76952016-07-27 06:56:36 +0200662unsigned long acpi_create_dmar_atsr(unsigned long current, u8 flags,
663 u16 segment)
664{
665 dmar_atsr_entry_t *atsr = (dmar_atsr_entry_t *)current;
666 memset(atsr, 0, sizeof(*atsr));
667 atsr->type = DMAR_ATSR;
668 atsr->length = sizeof(*atsr); /* will be fixed up later */
669 atsr->flags = flags;
670 atsr->segment = segment;
671
672 return atsr->length;
673}
674
John Zhao76e70672019-04-04 11:03:28 -0700675unsigned long acpi_create_dmar_rhsa(unsigned long current, u64 base_addr,
676 u32 proximity_domain)
677{
678 dmar_rhsa_entry_t *rhsa = (dmar_rhsa_entry_t *)current;
679 memset(rhsa, 0, sizeof(*rhsa));
680 rhsa->type = DMAR_RHSA;
681 rhsa->length = sizeof(*rhsa);
682 rhsa->base_address = base_addr;
683 rhsa->proximity_domain = proximity_domain;
684
685 return rhsa->length;
686}
687
688unsigned long acpi_create_dmar_andd(unsigned long current, u8 device_number,
689 const char *device_name)
690{
691 dmar_andd_entry_t *andd = (dmar_andd_entry_t *)current;
692 int andd_len = sizeof(dmar_andd_entry_t) + strlen(device_name) + 1;
693 memset(andd, 0, andd_len);
694 andd->type = DMAR_ANDD;
695 andd->length = andd_len;
696 andd->device_number = device_number;
697 memcpy(&andd->device_name, device_name, strlen(device_name));
698
699 return andd->length;
700}
701
John Zhao091532d2021-04-17 16:03:21 -0700702unsigned long acpi_create_dmar_satc(unsigned long current, u8 flags, u16 segment)
John Zhao6edbb182021-03-24 11:55:09 -0700703{
704 dmar_satc_entry_t *satc = (dmar_satc_entry_t *)current;
John Zhao091532d2021-04-17 16:03:21 -0700705 int satc_len = sizeof(dmar_satc_entry_t);
John Zhao6edbb182021-03-24 11:55:09 -0700706 memset(satc, 0, satc_len);
707 satc->type = DMAR_SATC;
708 satc->length = satc_len;
709 satc->flags = flags;
710 satc->segment_number = segment;
John Zhao6edbb182021-03-24 11:55:09 -0700711
712 return satc->length;
713}
714
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200715void acpi_dmar_drhd_fixup(unsigned long base, unsigned long current)
716{
717 dmar_entry_t *drhd = (dmar_entry_t *)base;
718 drhd->length = current - base;
719}
720
Matt DeVillier7866d492018-03-29 14:59:57 +0200721void acpi_dmar_rmrr_fixup(unsigned long base, unsigned long current)
722{
723 dmar_rmrr_entry_t *rmrr = (dmar_rmrr_entry_t *)base;
724 rmrr->length = current - base;
725}
726
Werner Zehd4d76952016-07-27 06:56:36 +0200727void acpi_dmar_atsr_fixup(unsigned long base, unsigned long current)
728{
729 dmar_atsr_entry_t *atsr = (dmar_atsr_entry_t *)base;
730 atsr->length = current - base;
731}
732
John Zhao6edbb182021-03-24 11:55:09 -0700733void acpi_dmar_satc_fixup(unsigned long base, unsigned long current)
734{
735 dmar_satc_entry_t *satc = (dmar_satc_entry_t *)base;
736 satc->length = current - base;
737}
738
Matt DeVillier7866d492018-03-29 14:59:57 +0200739static unsigned long acpi_create_dmar_ds(unsigned long current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100740 enum dev_scope_type type, u8 enumeration_id, u8 bus, u8 dev, u8 fn)
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200741{
Nico Huber6c4751d2015-10-26 12:03:54 +0100742 /* we don't support longer paths yet */
743 const size_t dev_scope_length = sizeof(dev_scope_t) + 2;
744
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200745 dev_scope_t *ds = (dev_scope_t *)current;
Nico Huber6c4751d2015-10-26 12:03:54 +0100746 memset(ds, 0, dev_scope_length);
747 ds->type = type;
748 ds->length = dev_scope_length;
749 ds->enumeration = enumeration_id;
750 ds->start_bus = bus;
751 ds->path[0].dev = dev;
752 ds->path[0].fn = fn;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200753
754 return ds->length;
755}
756
Matt DeVillier7866d492018-03-29 14:59:57 +0200757unsigned long acpi_create_dmar_ds_pci_br(unsigned long current, u8 bus,
Werner Zeh21a5bff2016-07-27 07:07:20 +0200758 u8 dev, u8 fn)
759{
Matt DeVillier7866d492018-03-29 14:59:57 +0200760 return acpi_create_dmar_ds(current,
Werner Zeh21a5bff2016-07-27 07:07:20 +0200761 SCOPE_PCI_SUB, 0, bus, dev, fn);
762}
763
Matt DeVillier7866d492018-03-29 14:59:57 +0200764unsigned long acpi_create_dmar_ds_pci(unsigned long current, u8 bus,
Nico Huber6c4751d2015-10-26 12:03:54 +0100765 u8 dev, u8 fn)
766{
Matt DeVillier7866d492018-03-29 14:59:57 +0200767 return acpi_create_dmar_ds(current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100768 SCOPE_PCI_ENDPOINT, 0, bus, dev, fn);
769}
770
Matt DeVillier7866d492018-03-29 14:59:57 +0200771unsigned long acpi_create_dmar_ds_ioapic(unsigned long current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100772 u8 enumeration_id, u8 bus, u8 dev, u8 fn)
773{
Matt DeVillier7866d492018-03-29 14:59:57 +0200774 return acpi_create_dmar_ds(current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100775 SCOPE_IOAPIC, enumeration_id, bus, dev, fn);
776}
777
Matt DeVillier7866d492018-03-29 14:59:57 +0200778unsigned long acpi_create_dmar_ds_msi_hpet(unsigned long current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100779 u8 enumeration_id, u8 bus, u8 dev, u8 fn)
780{
Matt DeVillier7866d492018-03-29 14:59:57 +0200781 return acpi_create_dmar_ds(current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100782 SCOPE_MSI_HPET, enumeration_id, bus, dev, fn);
783}
784
Uwe Hermann622824c2010-11-19 15:14:42 +0000785/* http://h21007.www2.hp.com/portal/download/files/unprot/Itanium/slit.pdf */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +0200786void acpi_create_slit(acpi_slit_t *slit,
787 unsigned long (*acpi_fill_slit)(unsigned long current))
Yinghai Lud4b278c2006-10-04 20:46:15 +0000788{
Uwe Hermann622824c2010-11-19 15:14:42 +0000789 acpi_header_t *header = &(slit->header);
790 unsigned long current = (unsigned long)slit + sizeof(acpi_slit_t);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000791
Uwe Hermann622824c2010-11-19 15:14:42 +0000792 memset((void *)slit, 0, sizeof(acpi_slit_t));
Yinghai Lud4b278c2006-10-04 20:46:15 +0000793
John Zhao2ba303e2019-05-28 16:48:14 -0700794 if (!header)
795 return;
796
Uwe Hermann622824c2010-11-19 15:14:42 +0000797 /* Fill out header fields. */
798 memcpy(header->signature, "SLIT", 4);
799 memcpy(header->oem_id, OEM_ID, 6);
800 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
801 memcpy(header->asl_compiler_id, ASLC, 4);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000802
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100803 header->asl_compiler_revision = asl_revision;
Uwe Hermann622824c2010-11-19 15:14:42 +0000804 header->length = sizeof(acpi_slit_t);
Marc Jones93a51762018-08-22 18:57:24 -0600805 header->revision = get_acpi_table_revision(SLIT);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000806
Uwe Hermann622824c2010-11-19 15:14:42 +0000807 current = acpi_fill_slit(current);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000808
Uwe Hermann622824c2010-11-19 15:14:42 +0000809 /* (Re)calculate length and checksum. */
810 header->length = current - (unsigned long)slit;
811 header->checksum = acpi_checksum((void *)slit, header->length);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000812}
813
Uwe Hermann622824c2010-11-19 15:14:42 +0000814/* http://www.intel.com/hardwaredesign/hpetspec_1.pdf */
Stefan Reinauer777224c2005-01-19 14:06:41 +0000815void acpi_create_hpet(acpi_hpet_t *hpet)
Stefan Reinauer688b3852004-01-28 16:56:14 +0000816{
Uwe Hermann622824c2010-11-19 15:14:42 +0000817 acpi_header_t *header = &(hpet->header);
818 acpi_addr_t *addr = &(hpet->addr);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000819
Stefan Reinauera7648c22004-01-29 17:31:34 +0000820 memset((void *)hpet, 0, sizeof(acpi_hpet_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000821
John Zhao2ba303e2019-05-28 16:48:14 -0700822 if (!header)
823 return;
824
Uwe Hermann622824c2010-11-19 15:14:42 +0000825 /* Fill out header fields. */
Stefan Reinauer4704dc52009-07-22 01:11:37 +0000826 memcpy(header->signature, "HPET", 4);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000827 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000828 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000829 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000830
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100831 header->asl_compiler_revision = asl_revision;
Stefan Reinauer688b3852004-01-28 16:56:14 +0000832 header->length = sizeof(acpi_hpet_t);
Marc Jones93a51762018-08-22 18:57:24 -0600833 header->revision = get_acpi_table_revision(HPET);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000834
Uwe Hermann622824c2010-11-19 15:14:42 +0000835 /* Fill out HPET address. */
Elyes HAOUAS04071f42020-07-20 17:05:24 +0200836 addr->space_id = ACPI_ADDRESS_SPACE_MEMORY;
Uwe Hermann622824c2010-11-19 15:14:42 +0000837 addr->bit_width = 64;
838 addr->bit_offset = 0;
Felix Held972d9f22022-02-23 16:32:20 +0100839 addr->addrl = HPET_BASE_ADDRESS & 0xffffffff;
840 addr->addrh = ((unsigned long long)HPET_BASE_ADDRESS) >> 32;
Stefan Reinauer688b3852004-01-28 16:56:14 +0000841
Felix Held972d9f22022-02-23 16:32:20 +0100842 hpet->id = read32p(HPET_BASE_ADDRESS);
Uwe Hermann622824c2010-11-19 15:14:42 +0000843 hpet->number = 0;
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200844 hpet->min_tick = CONFIG_HPET_MIN_TICKS;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000845
Uwe Hermann622824c2010-11-19 15:14:42 +0000846 header->checksum = acpi_checksum((void *)hpet, sizeof(acpi_hpet_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000847}
Uwe Hermann622824c2010-11-19 15:14:42 +0000848
Rocky Phaguraeff07132021-01-10 15:42:50 -0800849/*
850 * This method adds the ACPI error injection capability. It fills the default information.
851 * HW dependent code (caller) can modify the defaults upon return. If no changes are necessary
852 * and the defaults are acceptable then caller can simply add the table (acpi_add_table).
853 * INPUTS:
854 * einj - ptr to the starting location of EINJ table
855 * actions - number of actions to trigger an error (HW dependent)
856 * addr - address of trigger action table. This should be ACPI reserved memory and it will be
857 * shared between OS and FW.
858 */
859void acpi_create_einj(acpi_einj_t *einj, uintptr_t addr, u8 actions)
860{
861 int i;
862 acpi_header_t *header = &(einj->header);
863 acpi_injection_header_t *inj_header = &(einj->inj_header);
864 acpi_einj_smi_t *einj_smi = (acpi_einj_smi_t *)addr;
865 acpi_einj_trigger_table_t *tat;
866 if (!header)
867 return;
868
869 printk(BIOS_DEBUG, "%s einj_smi = %p\n", __func__, einj_smi);
870 memset(einj_smi, 0, sizeof(acpi_einj_smi_t));
871 tat = (acpi_einj_trigger_table_t *)(einj_smi + sizeof(acpi_einj_smi_t));
872 tat->header_size = 16;
873 tat->revision = 0;
874 tat->table_size = sizeof(acpi_einj_trigger_table_t) +
875 sizeof(acpi_einj_action_table_t) * actions - 1;
876 tat->entry_count = actions;
877 printk(BIOS_DEBUG, "%s trigger_action_table = %p\n", __func__, tat);
878
879 for (i = 0; i < actions; i++) {
880 tat->trigger_action[i].action = TRIGGER_ERROR;
881 tat->trigger_action[i].instruction = NO_OP;
882 tat->trigger_action[i].flags = FLAG_IGNORE;
883 tat->trigger_action[i].reg.space_id = ACPI_ADDRESS_SPACE_MEMORY;
884 tat->trigger_action[i].reg.bit_width = 64;
885 tat->trigger_action[i].reg.bit_offset = 0;
886 tat->trigger_action[i].reg.access_size = ACPI_ACCESS_SIZE_QWORD_ACCESS;
887 tat->trigger_action[i].reg.addr = 0;
888 tat->trigger_action[i].value = 0;
889 tat->trigger_action[i].mask = 0xFFFFFFFF;
890 }
891
892 acpi_einj_action_table_t default_actions[ACTION_COUNT] = {
893 [0] = {
894 .action = BEGIN_INJECT_OP,
895 .instruction = WRITE_REGISTER_VALUE,
896 .flags = FLAG_PRESERVE,
897 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->op_state),
898 .value = 0,
899 .mask = 0xFFFFFFFF
900 },
901 [1] = {
902 .action = GET_TRIGGER_ACTION_TABLE,
903 .instruction = READ_REGISTER,
904 .flags = FLAG_IGNORE,
905 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->trigger_action_table),
906 .value = 0,
907 .mask = 0xFFFFFFFFFFFFFFFF
908 },
909 [2] = {
910 .action = SET_ERROR_TYPE,
911 .instruction = WRITE_REGISTER,
912 .flags = FLAG_PRESERVE,
913 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->err_inject[0]),
914 .value = 0,
915 .mask = 0xFFFFFFFF
916 },
917 [3] = {
918 .action = GET_ERROR_TYPE,
919 .instruction = READ_REGISTER,
920 .flags = FLAG_IGNORE,
921 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->err_inj_cap),
922 .value = 0,
923 .mask = 0xFFFFFFFF
924 },
925 [4] = {
926 .action = END_INJECT_OP,
927 .instruction = WRITE_REGISTER_VALUE,
928 .flags = FLAG_PRESERVE,
929 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->op_state),
930 .value = 0,
931 .mask = 0xFFFFFFFF
932
933 },
934 [5] = {
935 .action = EXECUTE_INJECT_OP,
936 .instruction = WRITE_REGISTER_VALUE,
937 .flags = FLAG_PRESERVE,
938 .reg = EINJ_REG_IO(),
939 .value = 0x9a,
940 .mask = 0xFFFF,
941 },
942 [6] = {
943 .action = CHECK_BUSY_STATUS,
944 .instruction = READ_REGISTER_VALUE,
945 .flags = FLAG_IGNORE,
946 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->op_status),
947 .value = 1,
948 .mask = 1,
949 },
950 [7] = {
951 .action = GET_CMD_STATUS,
952 .instruction = READ_REGISTER,
953 .flags = FLAG_PRESERVE,
954 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->cmd_sts),
955 .value = 0,
956 .mask = 0x1fe,
957 },
958 [8] = {
959 .action = SET_ERROR_TYPE_WITH_ADDRESS,
960 .instruction = WRITE_REGISTER,
961 .flags = FLAG_PRESERVE,
962 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->setaddrtable),
963 .value = 1,
964 .mask = 0xffffffff
965 }
966 };
967
968 einj_smi->err_inj_cap = ACPI_EINJ_DEFAULT_CAP;
969 einj_smi->trigger_action_table = (u64) (uintptr_t)tat;
970
971 for (i = 0; i < ACTION_COUNT; i++)
972 printk(BIOS_DEBUG, "default_actions[%d].reg.addr is %llx\n", i,
973 default_actions[i].reg.addr);
974
Rocky Phagurab46a9e52021-05-21 13:34:03 -0700975 memset((void *)einj, 0, sizeof(*einj));
Rocky Phaguraeff07132021-01-10 15:42:50 -0800976
977 /* Fill out header fields. */
978 memcpy(header->signature, "EINJ", 4);
979 memcpy(header->oem_id, OEM_ID, 6);
980 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
981 memcpy(header->asl_compiler_id, ASLC, 4);
982
983 header->asl_compiler_revision = asl_revision;
984 header->length = sizeof(acpi_einj_t);
985 header->revision = 1;
986 inj_header->einj_header_size = sizeof(acpi_injection_header_t);
987 inj_header->entry_count = ACTION_COUNT;
988
989 printk(BIOS_DEBUG, "%s einj->action_table = %p\n",
990 __func__, einj->action_table);
991 memcpy((void *)einj->action_table, (void *)default_actions, sizeof(einj->action_table));
Rocky Phagurab46a9e52021-05-21 13:34:03 -0700992 header->checksum = acpi_checksum((void *)einj, sizeof(*einj));
Rocky Phaguraeff07132021-01-10 15:42:50 -0800993}
994
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700995void acpi_create_vfct(const struct device *device,
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530996 acpi_vfct_t *vfct,
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700997 unsigned long (*acpi_fill_vfct)(const struct device *device,
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530998 acpi_vfct_t *vfct_struct, unsigned long current))
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200999{
1000 acpi_header_t *header = &(vfct->header);
Himanshu Sahdev7f1da072019-10-21 15:27:19 +05301001 unsigned long current = (unsigned long)vfct + sizeof(acpi_vfct_t);
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +02001002
Himanshu Sahdev7f1da072019-10-21 15:27:19 +05301003 memset((void *)vfct, 0, sizeof(acpi_vfct_t));
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +02001004
John Zhao2ba303e2019-05-28 16:48:14 -07001005 if (!header)
1006 return;
1007
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +02001008 /* Fill out header fields. */
1009 memcpy(header->signature, "VFCT", 4);
1010 memcpy(header->oem_id, OEM_ID, 6);
1011 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1012 memcpy(header->asl_compiler_id, ASLC, 4);
1013
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001014 header->asl_compiler_revision = asl_revision;
Marc Jones93a51762018-08-22 18:57:24 -06001015 header->revision = get_acpi_table_revision(VFCT);
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +02001016
1017 current = acpi_fill_vfct(device, vfct, current);
1018
Kyösti Mälkkifb493792019-06-30 06:29:52 +03001019 /* If no BIOS image, return with header->length == 0. */
1020 if (!vfct->VBIOSImageOffset)
1021 return;
1022
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +02001023 /* (Re)calculate length and checksum. */
1024 header->length = current - (unsigned long)vfct;
1025 header->checksum = acpi_checksum((void *)vfct, header->length);
1026}
1027
Furquan Shaikh0f007d82020-04-24 06:41:18 -07001028void acpi_create_ipmi(const struct device *device,
Patrick Rudolph9d98e5a2019-06-14 19:00:04 +02001029 struct acpi_spmi *spmi,
1030 const u16 ipmi_revision,
1031 const acpi_addr_t *addr,
1032 const enum acpi_ipmi_interface_type type,
1033 const s8 gpe_interrupt,
1034 const u32 apic_interrupt,
1035 const u32 uid)
1036{
1037 acpi_header_t *header = &(spmi->header);
1038 memset((void *)spmi, 0, sizeof(struct acpi_spmi));
1039
1040 /* Fill out header fields. */
1041 memcpy(header->signature, "SPMI", 4);
1042 memcpy(header->oem_id, OEM_ID, 6);
1043 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1044 memcpy(header->asl_compiler_id, ASLC, 4);
1045
1046 header->asl_compiler_revision = asl_revision;
1047 header->length = sizeof(struct acpi_spmi);
1048 header->revision = get_acpi_table_revision(SPMI);
1049
1050 spmi->reserved = 1;
1051
1052 if (device->path.type == DEVICE_PATH_PCI) {
1053 spmi->pci_device_flag = ACPI_IPMI_PCI_DEVICE_FLAG;
1054 spmi->pci_bus = device->bus->secondary;
1055 spmi->pci_device = device->path.pci.devfn >> 3;
1056 spmi->pci_function = device->path.pci.devfn & 0x7;
1057 } else if (type != IPMI_INTERFACE_SSIF) {
1058 memcpy(spmi->uid, &uid, sizeof(spmi->uid));
1059 }
1060
1061 spmi->base_address = *addr;
1062 spmi->specification_revision = ipmi_revision;
1063
1064 spmi->interface_type = type;
1065
1066 if (gpe_interrupt >= 0 && gpe_interrupt < 32) {
1067 spmi->gpe = gpe_interrupt;
1068 spmi->interrupt_type |= ACPI_IPMI_INT_TYPE_SCI;
1069 }
1070 if (apic_interrupt > 0) {
1071 spmi->global_system_interrupt = apic_interrupt;
1072 spmi->interrupt_type |= ACPI_IPMI_INT_TYPE_APIC;
1073 }
1074
1075 /* Calculate checksum. */
1076 header->checksum = acpi_checksum((void *)spmi, header->length);
1077}
1078
Timothy Pearsonff8ccf02015-08-11 17:48:32 -05001079void acpi_create_ivrs(acpi_ivrs_t *ivrs,
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001080 unsigned long (*acpi_fill_ivrs)(acpi_ivrs_t *ivrs_struct,
1081 unsigned long current))
Timothy Pearsonff8ccf02015-08-11 17:48:32 -05001082{
1083 acpi_header_t *header = &(ivrs->header);
1084 unsigned long current = (unsigned long)ivrs + sizeof(acpi_ivrs_t);
1085
1086 memset((void *)ivrs, 0, sizeof(acpi_ivrs_t));
1087
John Zhao2ba303e2019-05-28 16:48:14 -07001088 if (!header)
1089 return;
1090
Timothy Pearsonff8ccf02015-08-11 17:48:32 -05001091 /* Fill out header fields. */
1092 memcpy(header->signature, "IVRS", 4);
1093 memcpy(header->oem_id, OEM_ID, 6);
1094 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1095 memcpy(header->asl_compiler_id, ASLC, 4);
1096
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001097 header->asl_compiler_revision = asl_revision;
Timothy Pearsonff8ccf02015-08-11 17:48:32 -05001098 header->length = sizeof(acpi_ivrs_t);
Marc Jones93a51762018-08-22 18:57:24 -06001099 header->revision = get_acpi_table_revision(IVRS);
Timothy Pearsonff8ccf02015-08-11 17:48:32 -05001100
1101 current = acpi_fill_ivrs(ivrs, current);
1102
1103 /* (Re)calculate length and checksum. */
1104 header->length = current - (unsigned long)ivrs;
1105 header->checksum = acpi_checksum((void *)ivrs, header->length);
1106}
1107
Jason Glenesk61624b22020-11-02 20:06:23 -08001108void acpi_create_crat(struct acpi_crat_header *crat,
1109 unsigned long (*acpi_fill_crat)(struct acpi_crat_header *crat_struct,
1110 unsigned long current))
1111{
1112 acpi_header_t *header = &(crat->header);
1113 unsigned long current = (unsigned long)crat + sizeof(struct acpi_crat_header);
1114
1115 memset((void *)crat, 0, sizeof(struct acpi_crat_header));
1116
1117 if (!header)
1118 return;
1119
1120 /* Fill out header fields. */
1121 memcpy(header->signature, "CRAT", 4);
1122 memcpy(header->oem_id, OEM_ID, 6);
1123 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1124 memcpy(header->asl_compiler_id, ASLC, 4);
1125
1126 header->asl_compiler_revision = asl_revision;
1127 header->length = sizeof(struct acpi_crat_header);
1128 header->revision = get_acpi_table_revision(CRAT);
1129
1130 current = acpi_fill_crat(crat, current);
1131
1132 /* (Re)calculate length and checksum. */
1133 header->length = current - (unsigned long)crat;
1134 header->checksum = acpi_checksum((void *)crat, header->length);
1135}
1136
Furquan Shaikh0f007d82020-04-24 06:41:18 -07001137unsigned long acpi_write_hpet(const struct device *device, unsigned long current,
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001138 acpi_rsdp_t *rsdp)
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001139{
1140 acpi_hpet_t *hpet;
1141
1142 /*
1143 * We explicitly add these tables later on:
1144 */
1145 printk(BIOS_DEBUG, "ACPI: * HPET\n");
1146
1147 hpet = (acpi_hpet_t *) current;
1148 current += sizeof(acpi_hpet_t);
Felix Heldc4697122019-06-20 13:50:17 +02001149 current = ALIGN_UP(current, 16);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001150 acpi_create_hpet(hpet);
1151 acpi_add_table(rsdp, hpet);
1152
1153 return current;
1154}
1155
Duncan Lauriee4a36c72017-11-11 19:33:25 -08001156void acpi_create_dbg2(acpi_dbg2_header_t *dbg2,
1157 int port_type, int port_subtype,
1158 acpi_addr_t *address, uint32_t address_size,
1159 const char *device_path)
1160{
1161 uintptr_t current;
1162 acpi_dbg2_device_t *device;
1163 uint32_t *dbg2_addr_size;
1164 acpi_header_t *header;
1165 size_t path_len;
1166 const char *path;
1167 char *namespace;
1168
1169 /* Fill out header fields. */
1170 current = (uintptr_t)dbg2;
1171 memset(dbg2, 0, sizeof(acpi_dbg2_header_t));
1172 header = &(dbg2->header);
John Zhao2ba303e2019-05-28 16:48:14 -07001173
1174 if (!header)
1175 return;
1176
Marc Jones93a51762018-08-22 18:57:24 -06001177 header->revision = get_acpi_table_revision(DBG2);
Duncan Lauriee4a36c72017-11-11 19:33:25 -08001178 memcpy(header->signature, "DBG2", 4);
1179 memcpy(header->oem_id, OEM_ID, 6);
1180 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1181 memcpy(header->asl_compiler_id, ASLC, 4);
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001182 header->asl_compiler_revision = asl_revision;
Duncan Lauriee4a36c72017-11-11 19:33:25 -08001183
1184 /* One debug device defined */
1185 dbg2->devices_offset = sizeof(acpi_dbg2_header_t);
1186 dbg2->devices_count = 1;
1187 current += sizeof(acpi_dbg2_header_t);
1188
1189 /* Device comes after the header */
1190 device = (acpi_dbg2_device_t *)current;
1191 memset(device, 0, sizeof(acpi_dbg2_device_t));
1192 current += sizeof(acpi_dbg2_device_t);
1193
1194 device->revision = 0;
1195 device->address_count = 1;
1196 device->port_type = port_type;
1197 device->port_subtype = port_subtype;
1198
1199 /* Base Address comes after device structure */
1200 memcpy((void *)current, address, sizeof(acpi_addr_t));
1201 device->base_address_offset = current - (uintptr_t)device;
1202 current += sizeof(acpi_addr_t);
1203
1204 /* Address Size comes after address structure */
1205 dbg2_addr_size = (uint32_t *)current;
1206 device->address_size_offset = current - (uintptr_t)device;
1207 *dbg2_addr_size = address_size;
1208 current += sizeof(uint32_t);
1209
1210 /* Namespace string comes last, use '.' if not provided */
1211 path = device_path ? : ".";
1212 /* Namespace string length includes NULL terminator */
1213 path_len = strlen(path) + 1;
1214 namespace = (char *)current;
1215 device->namespace_string_length = path_len;
1216 device->namespace_string_offset = current - (uintptr_t)device;
1217 strncpy(namespace, path, path_len);
1218 current += path_len;
1219
1220 /* Update structure lengths and checksum */
1221 device->length = current - (uintptr_t)device;
1222 header->length = current - (uintptr_t)dbg2;
1223 header->checksum = acpi_checksum((uint8_t *)dbg2, header->length);
1224}
1225
1226unsigned long acpi_write_dbg2_pci_uart(acpi_rsdp_t *rsdp, unsigned long current,
Aamir Bohrae825d3f2019-07-30 10:11:41 +05301227 const struct device *dev, uint8_t access_size)
Duncan Lauriee4a36c72017-11-11 19:33:25 -08001228{
1229 acpi_dbg2_header_t *dbg2 = (acpi_dbg2_header_t *)current;
1230 struct resource *res;
1231 acpi_addr_t address;
1232
1233 if (!dev) {
Wim Vervoorn6cd52432020-02-03 14:41:46 +01001234 printk(BIOS_DEBUG, "%s: Device not found\n", __func__);
Duncan Lauriee4a36c72017-11-11 19:33:25 -08001235 return current;
1236 }
Mario Scheithauerf1eb0ea2017-11-21 13:52:53 +01001237 if (!dev->enabled) {
1238 printk(BIOS_INFO, "%s: Device not enabled\n", __func__);
1239 return current;
1240 }
Angel Pons32d09be2021-11-03 13:27:45 +01001241 res = probe_resource(dev, PCI_BASE_ADDRESS_0);
Duncan Lauriee4a36c72017-11-11 19:33:25 -08001242 if (!res) {
1243 printk(BIOS_ERR, "%s: Unable to find resource for %s\n",
1244 __func__, dev_path(dev));
1245 return current;
1246 }
1247
1248 memset(&address, 0, sizeof(address));
1249 if (res->flags & IORESOURCE_IO)
1250 address.space_id = ACPI_ADDRESS_SPACE_IO;
1251 else if (res->flags & IORESOURCE_MEM)
1252 address.space_id = ACPI_ADDRESS_SPACE_MEMORY;
1253 else {
1254 printk(BIOS_ERR, "%s: Unknown address space type\n", __func__);
1255 return current;
1256 }
1257
1258 address.addrl = (uint32_t)res->base;
1259 address.addrh = (uint32_t)((res->base >> 32) & 0xffffffff);
1260 address.access_size = access_size;
1261
1262 acpi_create_dbg2(dbg2,
1263 ACPI_DBG2_PORT_SERIAL,
1264 ACPI_DBG2_PORT_SERIAL_16550,
1265 &address, res->size,
1266 acpi_device_path(dev));
1267
1268 if (dbg2->header.length) {
1269 current += dbg2->header.length;
1270 current = acpi_align_current(current);
1271 acpi_add_table(rsdp, dbg2);
1272 }
1273
1274 return current;
1275}
1276
Stefan Reinauer777224c2005-01-19 14:06:41 +00001277void acpi_create_facs(acpi_facs_t *facs)
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +00001278{
Uwe Hermann622824c2010-11-19 15:14:42 +00001279 memset((void *)facs, 0, sizeof(acpi_facs_t));
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +00001280
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001281 memcpy(facs->signature, "FACS", 4);
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +00001282 facs->length = sizeof(acpi_facs_t);
1283 facs->hardware_signature = 0;
1284 facs->firmware_waking_vector = 0;
1285 facs->global_lock = 0;
1286 facs->flags = 0;
1287 facs->x_firmware_waking_vector_l = 0;
1288 facs->x_firmware_waking_vector_h = 0;
Marc Jones93a51762018-08-22 18:57:24 -06001289 facs->version = get_acpi_table_revision(FACS);
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +00001290}
Stefan Reinauer777224c2005-01-19 14:06:41 +00001291
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001292static void acpi_write_rsdt(acpi_rsdt_t *rsdt, char *oem_id, char *oem_table_id)
Stefan Reinauer14e22772010-04-27 06:56:47 +00001293{
Uwe Hermann622824c2010-11-19 15:14:42 +00001294 acpi_header_t *header = &(rsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001295
John Zhao2ba303e2019-05-28 16:48:14 -07001296 if (!header)
1297 return;
1298
Uwe Hermann622824c2010-11-19 15:14:42 +00001299 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001300 memcpy(header->signature, "RSDT", 4);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001301 memcpy(header->oem_id, oem_id, 6);
1302 memcpy(header->oem_table_id, oem_table_id, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +00001303 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001304
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001305 header->asl_compiler_revision = asl_revision;
Stefan Reinauer688b3852004-01-28 16:56:14 +00001306 header->length = sizeof(acpi_rsdt_t);
Marc Jones93a51762018-08-22 18:57:24 -06001307 header->revision = get_acpi_table_revision(RSDT);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001308
Uwe Hermann622824c2010-11-19 15:14:42 +00001309 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauer688b3852004-01-28 16:56:14 +00001310
Uwe Hermann622824c2010-11-19 15:14:42 +00001311 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001312 header->checksum = acpi_checksum((void *)rsdt, sizeof(acpi_rsdt_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +00001313}
1314
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001315static void acpi_write_xsdt(acpi_xsdt_t *xsdt, char *oem_id, char *oem_table_id)
Stefan Reinauer14e22772010-04-27 06:56:47 +00001316{
Uwe Hermann622824c2010-11-19 15:14:42 +00001317 acpi_header_t *header = &(xsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001318
John Zhao2ba303e2019-05-28 16:48:14 -07001319 if (!header)
1320 return;
1321
Uwe Hermann622824c2010-11-19 15:14:42 +00001322 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001323 memcpy(header->signature, "XSDT", 4);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001324 memcpy(header->oem_id, oem_id, 6);
1325 memcpy(header->oem_table_id, oem_table_id, 8);
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001326 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001327
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001328 header->asl_compiler_revision = asl_revision;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001329 header->length = sizeof(acpi_xsdt_t);
Marc Jones93a51762018-08-22 18:57:24 -06001330 header->revision = get_acpi_table_revision(XSDT);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001331
Uwe Hermann622824c2010-11-19 15:14:42 +00001332 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001333
Uwe Hermann622824c2010-11-19 15:14:42 +00001334 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001335 header->checksum = acpi_checksum((void *)xsdt, sizeof(acpi_xsdt_t));
1336}
1337
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001338static void acpi_write_rsdp(acpi_rsdp_t *rsdp, acpi_rsdt_t *rsdt,
1339 acpi_xsdt_t *xsdt, char *oem_id)
Stefan Reinauer688b3852004-01-28 16:56:14 +00001340{
Stefan Reinauerd18faac2009-11-05 18:06:43 +00001341 memset(rsdp, 0, sizeof(acpi_rsdp_t));
Uwe Hermann622824c2010-11-19 15:14:42 +00001342
Stefan Reinauer688b3852004-01-28 16:56:14 +00001343 memcpy(rsdp->signature, RSDP_SIG, 8);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001344 memcpy(rsdp->oem_id, oem_id, 6);
Uwe Hermann622824c2010-11-19 15:14:42 +00001345
1346 rsdp->length = sizeof(acpi_rsdp_t);
Stefan Reinauerdefee172015-06-18 01:11:19 -07001347 rsdp->rsdt_address = (uintptr_t)rsdt;
Uwe Hermann622824c2010-11-19 15:14:42 +00001348
1349 /*
1350 * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2.
1351 *
1352 * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
1353 * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
1354 * revision 0).
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001355 */
1356 if (xsdt == NULL) {
Uwe Hermann622824c2010-11-19 15:14:42 +00001357 rsdp->revision = 0;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001358 } else {
Stefan Reinauerdefee172015-06-18 01:11:19 -07001359 rsdp->xsdt_address = (u64)(uintptr_t)xsdt;
Marc Jones93a51762018-08-22 18:57:24 -06001360 rsdp->revision = get_acpi_table_revision(RSDP);
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001361 }
Uwe Hermann622824c2010-11-19 15:14:42 +00001362
1363 /* Calculate checksums. */
1364 rsdp->checksum = acpi_checksum((void *)rsdp, 20);
1365 rsdp->ext_checksum = acpi_checksum((void *)rsdp, sizeof(acpi_rsdp_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +00001366}
1367
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001368unsigned long acpi_create_hest_error_source(acpi_hest_t *hest,
1369 acpi_hest_esd_t *esd, u16 type, void *data, u16 data_len)
zbaocaf494c82012-04-13 13:57:14 +08001370{
1371 acpi_header_t *header = &(hest->header);
1372 acpi_hest_hen_t *hen;
1373 void *pos;
1374 u16 len;
1375
1376 pos = esd;
1377 memset(pos, 0, sizeof(acpi_hest_esd_t));
1378 len = 0;
1379 esd->type = type; /* MCE */
1380 esd->source_id = hest->error_source_count;
1381 esd->flags = 0; /* FIRMWARE_FIRST */
1382 esd->enabled = 1;
1383 esd->prealloc_erecords = 1;
1384 esd->max_section_per_record = 0x1;
1385
1386 len += sizeof(acpi_hest_esd_t);
1387 pos = esd + 1;
1388
1389 switch (type) {
1390 case 0: /* MCE */
1391 break;
1392 case 1: /* CMC */
1393 hen = (acpi_hest_hen_t *) (pos);
1394 memset(pos, 0, sizeof(acpi_hest_hen_t));
1395 hen->type = 3; /* SCI? */
1396 hen->length = sizeof(acpi_hest_hen_t);
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001397 hen->conf_we = 0; /* Configuration Write Enable. */
zbaocaf494c82012-04-13 13:57:14 +08001398 hen->poll_interval = 0;
1399 hen->vector = 0;
1400 hen->sw2poll_threshold_val = 0;
1401 hen->sw2poll_threshold_win = 0;
1402 hen->error_threshold_val = 0;
1403 hen->error_threshold_win = 0;
1404 len += sizeof(acpi_hest_hen_t);
1405 pos = hen + 1;
1406 break;
1407 case 2: /* NMI */
1408 case 6: /* AER Root Port */
1409 case 7: /* AER Endpoint */
1410 case 8: /* AER Bridge */
1411 case 9: /* Generic Hardware Error Source. */
1412 /* TODO: */
1413 break;
1414 default:
1415 printk(BIOS_DEBUG, "Invalid type of Error Source.");
1416 break;
1417 }
Lee Leahy024b13d2017-03-16 13:41:11 -07001418 hest->error_source_count++;
zbaocaf494c82012-04-13 13:57:14 +08001419
1420 memcpy(pos, data, data_len);
1421 len += data_len;
John Zhao2ba303e2019-05-28 16:48:14 -07001422 if (header)
1423 header->length += len;
zbaocaf494c82012-04-13 13:57:14 +08001424
1425 return len;
1426}
1427
1428/* ACPI 4.0 */
Vladimir Serbinenko807127f2014-11-09 13:36:18 +01001429void acpi_write_hest(acpi_hest_t *hest,
1430 unsigned long (*acpi_fill_hest)(acpi_hest_t *hest))
zbaocaf494c82012-04-13 13:57:14 +08001431{
1432 acpi_header_t *header = &(hest->header);
1433
1434 memset(hest, 0, sizeof(acpi_hest_t));
1435
John Zhao2ba303e2019-05-28 16:48:14 -07001436 if (!header)
1437 return;
1438
zbaocaf494c82012-04-13 13:57:14 +08001439 memcpy(header->signature, "HEST", 4);
1440 memcpy(header->oem_id, OEM_ID, 6);
1441 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1442 memcpy(header->asl_compiler_id, ASLC, 4);
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001443 header->asl_compiler_revision = asl_revision;
zbaocaf494c82012-04-13 13:57:14 +08001444 header->length += sizeof(acpi_hest_t);
Marc Jones93a51762018-08-22 18:57:24 -06001445 header->revision = get_acpi_table_revision(HEST);
zbaocaf494c82012-04-13 13:57:14 +08001446
1447 acpi_fill_hest(hest);
1448
1449 /* Calculate checksums. */
1450 header->checksum = acpi_checksum((void *)hest, header->length);
1451}
1452
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001453/* ACPI 3.0b */
1454void acpi_write_bert(acpi_bert_t *bert, uintptr_t region, size_t length)
1455{
1456 acpi_header_t *header = &(bert->header);
1457
1458 memset(bert, 0, sizeof(acpi_bert_t));
1459
John Zhao2ba303e2019-05-28 16:48:14 -07001460 if (!header)
1461 return;
1462
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001463 memcpy(header->signature, "BERT", 4);
1464 memcpy(header->oem_id, OEM_ID, 6);
1465 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1466 memcpy(header->asl_compiler_id, ASLC, 4);
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001467 header->asl_compiler_revision = asl_revision;
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001468 header->length += sizeof(acpi_bert_t);
1469 header->revision = get_acpi_table_revision(BERT);
1470
1471 bert->error_region = region;
1472 bert->region_length = length;
1473
1474 /* Calculate checksums. */
1475 header->checksum = acpi_checksum((void *)bert, header->length);
1476}
1477
Angel Pons79572e42020-07-13 00:17:43 +02001478__weak void arch_fill_fadt(acpi_fadt_t *fadt) { }
Kyösti Mälkkif9aac922020-05-30 16:16:28 +03001479__weak void soc_fill_fadt(acpi_fadt_t *fadt) { }
Kyösti Mälkki02fd15d2020-06-02 03:34:43 +03001480__weak void mainboard_fill_fadt(acpi_fadt_t *fadt) { }
Kyösti Mälkkif9aac922020-05-30 16:16:28 +03001481
Lee Leahy024b13d2017-03-16 13:41:11 -07001482void acpi_create_fadt(acpi_fadt_t *fadt, acpi_facs_t *facs, void *dsdt)
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001483{
1484 acpi_header_t *header = &(fadt->header);
1485
1486 memset((void *) fadt, 0, sizeof(acpi_fadt_t));
John Zhao2ba303e2019-05-28 16:48:14 -07001487
1488 if (!header)
1489 return;
1490
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001491 memcpy(header->signature, "FACP", 4);
1492 header->length = sizeof(acpi_fadt_t);
Marc Jones93a51762018-08-22 18:57:24 -06001493 header->revision = get_acpi_table_revision(FADT);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001494 memcpy(header->oem_id, OEM_ID, 6);
1495 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1496 memcpy(header->asl_compiler_id, ASLC, 4);
Elyes HAOUAS26071aa2019-02-15 08:21:33 +01001497 header->asl_compiler_revision = asl_revision;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001498
Elyes Haouas532e0432022-02-21 18:13:58 +01001499 fadt->FADT_MinorVersion = get_acpi_fadt_minor_version();
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001500 fadt->firmware_ctrl = (unsigned long) facs;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001501 fadt->x_firmware_ctl_l = (unsigned long)facs;
1502 fadt->x_firmware_ctl_h = 0;
Kyösti Mälkkid4acee82020-06-03 07:20:55 +03001503
1504 fadt->dsdt = (unsigned long) dsdt;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001505 fadt->x_dsdt_l = (unsigned long)dsdt;
1506 fadt->x_dsdt_h = 0;
1507
Kyösti Mälkkid4acee82020-06-03 07:20:55 +03001508 /* should be 0 ACPI 3.0 */
1509 fadt->reserved = 0;
1510
Kyösti Mälkkie0d38682020-06-07 12:01:58 +03001511 fadt->preferred_pm_profile = acpi_get_preferred_pm_profile();
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001512
Nico Huberd5811372021-07-12 18:11:58 +02001513 if (CONFIG(USE_PC_CMOS_ALTCENTURY))
1514 fadt->century = RTC_CLK_ALTCENTURY;
1515
Angel Pons79572e42020-07-13 00:17:43 +02001516 arch_fill_fadt(fadt);
1517
Vladimir Serbinenkoab83ef02014-10-25 15:18:25 +02001518 acpi_fill_fadt(fadt);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001519
Kyösti Mälkkif9aac922020-05-30 16:16:28 +03001520 soc_fill_fadt(fadt);
Kyösti Mälkki02fd15d2020-06-02 03:34:43 +03001521 mainboard_fill_fadt(fadt);
Kyösti Mälkkif9aac922020-05-30 16:16:28 +03001522
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001523 header->checksum =
1524 acpi_checksum((void *) fadt, header->length);
1525}
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001526
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001527void acpi_create_lpit(acpi_lpit_t *lpit)
1528{
1529 acpi_header_t *header = &(lpit->header);
1530 unsigned long current = (unsigned long)lpit + sizeof(acpi_lpit_t);
1531
1532 memset((void *)lpit, 0, sizeof(acpi_lpit_t));
1533
1534 if (!header)
1535 return;
1536
1537 /* Fill out header fields. */
1538 memcpy(header->signature, "LPIT", 4);
1539 memcpy(header->oem_id, OEM_ID, 6);
1540 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1541 memcpy(header->asl_compiler_id, ASLC, 4);
1542
1543 header->asl_compiler_revision = asl_revision;
1544 header->revision = get_acpi_table_revision(LPIT);
1545 header->oem_revision = 42;
1546 header->length = sizeof(acpi_lpit_t);
1547
1548 current = acpi_fill_lpit(current);
1549
1550 /* (Re)calculate length and checksum. */
1551 header->length = current - (unsigned long)lpit;
1552 header->checksum = acpi_checksum((void *)lpit, header->length);
1553}
1554
1555unsigned long acpi_create_lpi_desc_ncst(acpi_lpi_desc_ncst_t *lpi_desc, uint16_t uid)
1556{
1557 memset(lpi_desc, 0, sizeof(acpi_lpi_desc_ncst_t));
1558 lpi_desc->header.length = sizeof(acpi_lpi_desc_ncst_t);
1559 lpi_desc->header.type = ACPI_LPI_DESC_TYPE_NATIVE_CSTATE;
1560 lpi_desc->header.uid = uid;
1561
1562 return lpi_desc->header.length;
1563}
1564
Aaron Durbin64031672018-04-21 14:45:32 -06001565unsigned long __weak fw_cfg_acpi_tables(unsigned long start)
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001566{
1567 return 0;
1568}
1569
Raul E Rangel6b446b92021-11-19 11:38:35 -07001570void preload_acpi_dsdt(void)
1571{
1572 const char *file = CONFIG_CBFS_PREFIX "/dsdt.aml";
1573
1574 if (!CONFIG(CBFS_PRELOAD))
1575 return;
1576
1577 printk(BIOS_DEBUG, "Preloading %s\n", file);
1578 cbfs_preload(file);
1579}
1580
Arthur Heymans2e7e2d92022-03-03 22:28:27 +01001581static uintptr_t coreboot_rsdp;
1582
1583uintptr_t get_coreboot_rsdp(void)
1584{
1585 return coreboot_rsdp;
1586}
1587
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001588unsigned long write_acpi_tables(unsigned long start)
1589{
1590 unsigned long current;
1591 acpi_rsdp_t *rsdp;
1592 acpi_rsdt_t *rsdt;
1593 acpi_xsdt_t *xsdt;
1594 acpi_fadt_t *fadt;
1595 acpi_facs_t *facs;
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001596 acpi_header_t *slic_file, *slic;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001597 acpi_header_t *ssdt;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001598 acpi_header_t *dsdt_file, *dsdt;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001599 acpi_mcfg_t *mcfg;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +02001600 acpi_tcpa_t *tcpa;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001601 acpi_tpm2_t *tpm2;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001602 acpi_madt_t *madt;
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001603 acpi_lpit_t *lpit;
Francois Toguo522e0db2021-01-21 09:55:19 -08001604 acpi_bert_t *bert;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +11001605 struct device *dev;
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001606 unsigned long fw;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001607 size_t slic_size, dsdt_size;
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001608 char oem_id[6], oem_table_id[8];
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001609
1610 current = start;
1611
1612 /* Align ACPI tables to 16byte */
Aaron Durbin07a1b282015-12-10 17:07:38 -06001613 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001614
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001615 /* Special case for qemu */
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001616 fw = fw_cfg_acpi_tables(current);
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001617 if (fw) {
1618 rsdp = NULL;
1619 /* Find RSDP. */
1620 for (void *p = (void *)current; p < (void *)fw; p += 16) {
1621 if (valid_rsdp((acpi_rsdp_t *)p)) {
1622 rsdp = p;
1623 break;
1624 }
1625 }
1626 if (!rsdp)
1627 return fw;
1628
1629 /* Add BOOT0000 for Linux google firmware driver */
1630 printk(BIOS_DEBUG, "ACPI: * SSDT\n");
1631 ssdt = (acpi_header_t *)fw;
1632 current = (unsigned long)ssdt + sizeof(acpi_header_t);
1633
1634 memset((void *)ssdt, 0, sizeof(acpi_header_t));
1635
1636 memcpy(&ssdt->signature, "SSDT", 4);
1637 ssdt->revision = get_acpi_table_revision(SSDT);
1638 memcpy(&ssdt->oem_id, OEM_ID, 6);
1639 memcpy(&ssdt->oem_table_id, oem_table_id, 8);
1640 ssdt->oem_revision = 42;
1641 memcpy(&ssdt->asl_compiler_id, ASLC, 4);
1642 ssdt->asl_compiler_revision = asl_revision;
1643 ssdt->length = sizeof(acpi_header_t);
1644
1645 acpigen_set_current((char *) current);
1646
1647 /* Write object to declare coreboot tables */
1648 acpi_ssdt_write_cbtable();
1649
1650 /* (Re)calculate length and checksum. */
1651 ssdt->length = current - (unsigned long)ssdt;
1652 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
1653
1654 acpi_create_ssdt_generator(ssdt, ACPI_TABLE_CREATOR);
1655
1656 acpi_add_table(rsdp, ssdt);
1657
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001658 return fw;
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001659 }
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001660
Julius Werner834b3ec2020-03-04 16:52:08 -08001661 dsdt_file = cbfs_map(CONFIG_CBFS_PREFIX "/dsdt.aml", &dsdt_size);
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001662 if (!dsdt_file) {
1663 printk(BIOS_ERR, "No DSDT file, skipping ACPI tables\n");
1664 return current;
1665 }
1666
1667 if (dsdt_file->length > dsdt_size
Elyes HAOUAS7d87e762016-10-03 22:11:07 +02001668 || dsdt_file->length < sizeof(acpi_header_t)
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001669 || memcmp(dsdt_file->signature, "DSDT", 4) != 0) {
1670 printk(BIOS_ERR, "Invalid DSDT file, skipping ACPI tables\n");
1671 return current;
1672 }
1673
Julius Werner834b3ec2020-03-04 16:52:08 -08001674 slic_file = cbfs_map(CONFIG_CBFS_PREFIX "/slic", &slic_size);
Vladimir Serbinenko2cb29782015-05-31 11:32:09 +02001675 if (slic_file
1676 && (slic_file->length > slic_size
Elyes HAOUAS7d87e762016-10-03 22:11:07 +02001677 || slic_file->length < sizeof(acpi_header_t)
Benjamin Doron2b2dc0c2020-09-12 02:55:57 +00001678 || (memcmp(slic_file->signature, "SLIC", 4) != 0
1679 && memcmp(slic_file->signature, "MSDM", 4) != 0))) {
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001680 slic_file = 0;
1681 }
1682
1683 if (slic_file) {
1684 memcpy(oem_id, slic_file->oem_id, 6);
1685 memcpy(oem_table_id, slic_file->oem_table_id, 8);
1686 } else {
1687 memcpy(oem_id, OEM_ID, 6);
1688 memcpy(oem_table_id, ACPI_TABLE_CREATOR, 8);
1689 }
1690
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001691 printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx.\n", start);
1692
1693 /* We need at least an RSDP and an RSDT Table */
1694 rsdp = (acpi_rsdp_t *) current;
Arthur Heymans2e7e2d92022-03-03 22:28:27 +01001695 coreboot_rsdp = (uintptr_t)rsdp;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001696 current += sizeof(acpi_rsdp_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001697 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001698 rsdt = (acpi_rsdt_t *) current;
1699 current += sizeof(acpi_rsdt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001700 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001701 xsdt = (acpi_xsdt_t *) current;
1702 current += sizeof(acpi_xsdt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001703 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001704
1705 /* clear all table memory */
1706 memset((void *) start, 0, current - start);
1707
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001708 acpi_write_rsdp(rsdp, rsdt, xsdt, oem_id);
1709 acpi_write_rsdt(rsdt, oem_id, oem_table_id);
1710 acpi_write_xsdt(xsdt, oem_id, oem_table_id);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001711
1712 printk(BIOS_DEBUG, "ACPI: * FACS\n");
Felix Heldc4697122019-06-20 13:50:17 +02001713 current = ALIGN_UP(current, 64);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001714 facs = (acpi_facs_t *) current;
1715 current += sizeof(acpi_facs_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001716 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001717 acpi_create_facs(facs);
1718
1719 printk(BIOS_DEBUG, "ACPI: * DSDT\n");
1720 dsdt = (acpi_header_t *) current;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001721 memcpy(dsdt, dsdt_file, sizeof(acpi_header_t));
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001722 if (dsdt->length >= sizeof(acpi_header_t)) {
1723 current += sizeof(acpi_header_t);
1724
1725 acpigen_set_current((char *) current);
Kyösti Mälkki2ab4a962020-06-30 11:41:47 +03001726
Kyösti Mälkki37eb24b2021-01-11 18:40:37 +02001727 if (CONFIG(ACPI_SOC_NVS))
1728 acpi_fill_gnvs();
Kyösti Mälkki3dc17922021-03-16 19:01:48 +02001729 if (CONFIG(CHROMEOS_NVS))
1730 acpi_fill_cnvs();
Kyösti Mälkki2ab4a962020-06-30 11:41:47 +03001731
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001732 for (dev = all_devices; dev; dev = dev->next)
Nico Huber68680dd2020-03-31 17:34:52 +02001733 if (dev->ops && dev->ops->acpi_inject_dsdt)
1734 dev->ops->acpi_inject_dsdt(dev);
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001735 current = (unsigned long) acpigen_get_current();
1736 memcpy((char *)current,
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001737 (char *)dsdt_file + sizeof(acpi_header_t),
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001738 dsdt->length - sizeof(acpi_header_t));
1739 current += dsdt->length - sizeof(acpi_header_t);
1740
1741 /* (Re)calculate length and checksum. */
1742 dsdt->length = current - (unsigned long)dsdt;
1743 dsdt->checksum = 0;
1744 dsdt->checksum = acpi_checksum((void *)dsdt, dsdt->length);
1745 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001746
Aaron Durbin07a1b282015-12-10 17:07:38 -06001747 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001748
1749 printk(BIOS_DEBUG, "ACPI: * FADT\n");
1750 fadt = (acpi_fadt_t *) current;
1751 current += sizeof(acpi_fadt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001752 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001753
1754 acpi_create_fadt(fadt, facs, dsdt);
1755 acpi_add_table(rsdp, fadt);
1756
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001757 if (slic_file) {
1758 printk(BIOS_DEBUG, "ACPI: * SLIC\n");
1759 slic = (acpi_header_t *)current;
1760 memcpy(slic, slic_file, slic_file->length);
1761 current += slic_file->length;
Aaron Durbin07a1b282015-12-10 17:07:38 -06001762 current = acpi_align_current(current);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001763 acpi_add_table(rsdp, slic);
1764 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001765
1766 printk(BIOS_DEBUG, "ACPI: * SSDT\n");
1767 ssdt = (acpi_header_t *)current;
1768 acpi_create_ssdt_generator(ssdt, ACPI_TABLE_CREATOR);
Vladimir Serbinenko9310df82014-10-10 20:40:41 +02001769 if (ssdt->length > sizeof(acpi_header_t)) {
1770 current += ssdt->length;
1771 acpi_add_table(rsdp, ssdt);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001772 current = acpi_align_current(current);
Vladimir Serbinenko9310df82014-10-10 20:40:41 +02001773 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001774
1775 printk(BIOS_DEBUG, "ACPI: * MCFG\n");
1776 mcfg = (acpi_mcfg_t *) current;
1777 acpi_create_mcfg(mcfg);
1778 if (mcfg->header.length > sizeof(acpi_mcfg_t)) {
1779 current += mcfg->header.length;
Aaron Durbin07a1b282015-12-10 17:07:38 -06001780 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001781 acpi_add_table(rsdp, mcfg);
1782 }
1783
Julius Wernercd49cce2019-03-05 16:53:33 -08001784 if (CONFIG(TPM1)) {
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001785 printk(BIOS_DEBUG, "ACPI: * TCPA\n");
1786 tcpa = (acpi_tcpa_t *) current;
1787 acpi_create_tcpa(tcpa);
1788 if (tcpa->header.length >= sizeof(acpi_tcpa_t)) {
1789 current += tcpa->header.length;
1790 current = acpi_align_current(current);
1791 acpi_add_table(rsdp, tcpa);
1792 }
1793 }
1794
Julius Wernercd49cce2019-03-05 16:53:33 -08001795 if (CONFIG(TPM2)) {
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001796 printk(BIOS_DEBUG, "ACPI: * TPM2\n");
1797 tpm2 = (acpi_tpm2_t *) current;
1798 acpi_create_tpm2(tpm2);
1799 if (tpm2->header.length >= sizeof(acpi_tpm2_t)) {
1800 current += tpm2->header.length;
1801 current = acpi_align_current(current);
1802 acpi_add_table(rsdp, tpm2);
1803 }
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +02001804 }
1805
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001806 if (CONFIG(ACPI_LPIT)) {
1807 printk(BIOS_DEBUG, "ACPI: * LPIT\n");
1808
1809 lpit = (acpi_lpit_t *)current;
1810 acpi_create_lpit(lpit);
1811 if (lpit->header.length >= sizeof(acpi_lpit_t)) {
1812 current += lpit->header.length;
1813 current = acpi_align_current(current);
1814 acpi_add_table(rsdp, lpit);
1815 }
1816 }
1817
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001818 printk(BIOS_DEBUG, "ACPI: * MADT\n");
1819
1820 madt = (acpi_madt_t *) current;
1821 acpi_create_madt(madt);
1822 if (madt->header.length > sizeof(acpi_madt_t)) {
Lee Leahy024b13d2017-03-16 13:41:11 -07001823 current += madt->header.length;
1824 acpi_add_table(rsdp, madt);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001825 }
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001826
Aaron Durbin07a1b282015-12-10 17:07:38 -06001827 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001828
Felix Heldfba479262021-05-28 16:11:43 +02001829 if (CONFIG(ACPI_BERT)) {
Francois Toguo522e0db2021-01-21 09:55:19 -08001830 void *region;
1831 size_t size;
Francois Toguo522e0db2021-01-21 09:55:19 -08001832 bert = (acpi_bert_t *) current;
Felix Heldfba479262021-05-28 16:11:43 +02001833 if (acpi_soc_get_bert_region(&region, &size) == CB_SUCCESS) {
1834 printk(BIOS_DEBUG, "ACPI: * BERT\n");
1835 acpi_write_bert(bert, (uintptr_t)region, size);
1836 if (bert->header.length >= sizeof(acpi_bert_t)) {
1837 current += bert->header.length;
1838 acpi_add_table(rsdp, bert);
1839 }
1840 current = acpi_align_current(current);
Francois Toguo522e0db2021-01-21 09:55:19 -08001841 }
Francois Toguo522e0db2021-01-21 09:55:19 -08001842 }
1843
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001844 printk(BIOS_DEBUG, "current = %lx\n", current);
1845
1846 for (dev = all_devices; dev; dev = dev->next) {
1847 if (dev->ops && dev->ops->write_acpi_tables) {
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001848 current = dev->ops->write_acpi_tables(dev, current,
1849 rsdp);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001850 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001851 }
1852 }
1853
1854 printk(BIOS_INFO, "ACPI: done.\n");
1855 return current;
1856}
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001857
Rudolf Marek33cafe52009-04-13 18:07:02 +00001858static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp)
1859{
1860 if (strncmp((char *)rsdp, RSDP_SIG, sizeof(RSDP_SIG) - 1) != 0)
1861 return NULL;
1862
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001863 printk(BIOS_DEBUG, "Looking on %p for valid checksum\n", rsdp);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001864
1865 if (acpi_checksum((void *)rsdp, 20) != 0)
1866 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001867 printk(BIOS_DEBUG, "Checksum 1 passed\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001868
1869 if ((rsdp->revision > 1) && (acpi_checksum((void *)rsdp,
1870 rsdp->length) != 0))
1871 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001872 printk(BIOS_DEBUG, "Checksum 2 passed all OK\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001873
1874 return rsdp;
1875}
1876
Rudolf Marek33cafe52009-04-13 18:07:02 +00001877void *acpi_find_wakeup_vector(void)
1878{
1879 char *p, *end;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001880 acpi_rsdt_t *rsdt;
1881 acpi_facs_t *facs;
Martin Rothc5f49262012-12-14 19:17:55 -07001882 acpi_fadt_t *fadt = NULL;
Kyösti Mälkki072d436b2016-06-17 08:44:40 +03001883 acpi_rsdp_t *rsdp = NULL;
Uwe Hermann622824c2010-11-19 15:14:42 +00001884 void *wake_vec;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001885 int i;
1886
Kyösti Mälkkib8c7ea02020-11-17 16:41:38 +02001887 if (!acpi_is_wakeup_s3())
Rudolf Marek33cafe52009-04-13 18:07:02 +00001888 return NULL;
1889
Uwe Hermann622824c2010-11-19 15:14:42 +00001890 printk(BIOS_DEBUG, "Trying to find the wakeup vector...\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001891
Uwe Hermann622824c2010-11-19 15:14:42 +00001892 /* Find RSDP. */
1893 for (p = (char *)0xe0000; p < (char *)0xfffff; p += 16) {
Lee Leahy0b5678f2017-03-16 16:01:40 -07001894 rsdp = valid_rsdp((acpi_rsdp_t *)p);
1895 if (rsdp)
Rudolf Marek33cafe52009-04-13 18:07:02 +00001896 break;
1897 }
1898
Angel Pons98a68ad2018-11-04 12:25:25 +01001899 if (rsdp == NULL) {
1900 printk(BIOS_ALERT,
1901 "No RSDP found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001902 return NULL;
Angel Pons98a68ad2018-11-04 12:25:25 +01001903 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001904
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001905 printk(BIOS_DEBUG, "RSDP found at %p\n", rsdp);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001906 rsdt = (acpi_rsdt_t *)(uintptr_t)rsdp->rsdt_address;
Stefan Reinauer14e22772010-04-27 06:56:47 +00001907
Uwe Hermann622824c2010-11-19 15:14:42 +00001908 end = (char *)rsdt + rsdt->header.length;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001909 printk(BIOS_DEBUG, "RSDT found at %p ends at %p\n", rsdt, end);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001910
Uwe Hermann622824c2010-11-19 15:14:42 +00001911 for (i = 0; ((char *)&rsdt->entry[i]) < end; i++) {
Stefan Reinauer71a30182015-07-30 16:28:13 -07001912 fadt = (acpi_fadt_t *)(uintptr_t)rsdt->entry[i];
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001913 if (strncmp((char *)fadt, "FACP", 4) == 0)
Rudolf Marek33cafe52009-04-13 18:07:02 +00001914 break;
1915 fadt = NULL;
1916 }
1917
Angel Pons98a68ad2018-11-04 12:25:25 +01001918 if (fadt == NULL) {
1919 printk(BIOS_ALERT,
1920 "No FADT found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001921 return NULL;
Angel Pons98a68ad2018-11-04 12:25:25 +01001922 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001923
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001924 printk(BIOS_DEBUG, "FADT found at %p\n", fadt);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001925 facs = (acpi_facs_t *)(uintptr_t)fadt->firmware_ctrl;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001926
Stefan Reinauer7e9771c2009-04-22 08:17:38 +00001927 if (facs == NULL) {
Angel Pons98a68ad2018-11-04 12:25:25 +01001928 printk(BIOS_ALERT,
1929 "No FACS found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001930 return NULL;
Stefan Reinauer7e9771c2009-04-22 08:17:38 +00001931 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001932
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001933 printk(BIOS_DEBUG, "FACS found at %p\n", facs);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001934 wake_vec = (void *)(uintptr_t)facs->firmware_waking_vector;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001935 printk(BIOS_DEBUG, "OS waking vector is %p\n", wake_vec);
Uwe Hermann622824c2010-11-19 15:14:42 +00001936
Rudolf Marek33cafe52009-04-13 18:07:02 +00001937 return wake_vec;
1938}
1939
Aaron Durbin64031672018-04-21 14:45:32 -06001940__weak int acpi_get_gpe(int gpe)
Duncan Laurie1f6e6812016-09-19 12:04:19 -07001941{
1942 return -1; /* implemented by SOC */
1943}
Marc Jones93a51762018-08-22 18:57:24 -06001944
Elyes Haouas8b950f42022-02-16 12:08:16 +01001945u8 get_acpi_fadt_minor_version(void)
1946{
1947 return ACPI_FADT_MINOR_VERSION_0;
1948}
1949
Marc Jones93a51762018-08-22 18:57:24 -06001950int get_acpi_table_revision(enum acpi_tables table)
1951{
1952 switch (table) {
1953 case FADT:
Elyes Haouas8b950f42022-02-16 12:08:16 +01001954 return ACPI_FADT_REV_ACPI_6;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001955 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 +01001956 return 3;
Marc Jones93a51762018-08-22 18:57:24 -06001957 case MCFG:
1958 return 1;
1959 case TCPA:
1960 return 2;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001961 case TPM2:
1962 return 4;
Martin Roth0949e732021-10-01 14:28:22 -06001963 case SSDT: /* ACPI 3.0 up to 6.3: 2 */
Marc Jones93a51762018-08-22 18:57:24 -06001964 return 2;
Martin Roth0949e732021-10-01 14:28:22 -06001965 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 -06001966 return 1; /* TODO Should probably be upgraded to 2 */
Jonathan Zhang2a4e1f42021-04-01 11:43:37 -07001967 case HMAT: /* ACPI 6.4: 2 */
1968 return 2;
Marc Jones93a51762018-08-22 18:57:24 -06001969 case DMAR:
1970 return 1;
Martin Roth0949e732021-10-01 14:28:22 -06001971 case SLIT: /* ACPI 2.0 up to 6.3: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06001972 return 1;
Patrick Rudolph9d98e5a2019-06-14 19:00:04 +02001973 case SPMI: /* IMPI 2.0 */
1974 return 5;
Marc Jones93a51762018-08-22 18:57:24 -06001975 case HPET: /* Currently 1. Table added in ACPI 2.0. */
1976 return 1;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001977 case VFCT: /* ACPI 2.0/3.0/4.0: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06001978 return 1;
1979 case IVRS:
Jason Glenesk1916f892020-07-24 02:51:30 -07001980 return IVRS_FORMAT_MIXED;
Marc Jones93a51762018-08-22 18:57:24 -06001981 case DBG2:
1982 return 0;
Martin Roth0949e732021-10-01 14:28:22 -06001983 case FACS: /* ACPI 2.0/3.0: 1, ACPI 4.0 up to 6.3: 2 */
Marc Jones93a51762018-08-22 18:57:24 -06001984 return 1;
Martin Roth0949e732021-10-01 14:28:22 -06001985 case RSDT: /* ACPI 1.0 up to 6.3: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06001986 return 1;
Martin Roth0949e732021-10-01 14:28:22 -06001987 case XSDT: /* ACPI 2.0 up to 6.3: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06001988 return 1;
Martin Roth0949e732021-10-01 14:28:22 -06001989 case RSDP: /* ACPI 2.0 up to 6.3: 2 */
Marc Jones93a51762018-08-22 18:57:24 -06001990 return 2;
Rocky Phaguraeff07132021-01-10 15:42:50 -08001991 case EINJ:
1992 return 1;
Marc Jones93a51762018-08-22 18:57:24 -06001993 case HEST:
1994 return 1;
1995 case NHLT:
1996 return 5;
Marshall Dawson44705c62018-09-04 13:47:15 -06001997 case BERT:
1998 return 1;
Jason Glenesk61624b22020-11-02 20:06:23 -08001999 case CRAT:
2000 return 1;
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01002001 case LPIT: /* ACPI 5.1 up to 6.3: 0 */
2002 return 0;
Marc Jones93a51762018-08-22 18:57:24 -06002003 default:
2004 return -1;
2005 }
2006 return -1;
2007}