blob: 2706bf7f15209508c7981fbff2ad8b1708d63d53 [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
Kyösti Mälkkic7da0272021-06-08 11:37:08 +030033#if ENV_X86
34#include <arch/ioapic.h>
35#endif
36
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +020037static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp);
38
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000039u8 acpi_checksum(u8 *table, u32 length)
Stefan Reinauer688b3852004-01-28 16:56:14 +000040{
Uwe Hermann622824c2010-11-19 15:14:42 +000041 u8 ret = 0;
Stefan Reinauer688b3852004-01-28 16:56:14 +000042 while (length--) {
43 ret += *table;
44 table++;
45 }
Stefan Reinauera7648c22004-01-29 17:31:34 +000046 return -ret;
Stefan Reinauer688b3852004-01-28 16:56:14 +000047}
48
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000049/**
Uwe Hermann622824c2010-11-19 15:14:42 +000050 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
51 * and checksum.
Stefan Reinauer06feb882004-02-03 16:11:35 +000052 */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000053void acpi_add_table(acpi_rsdp_t *rsdp, void *table)
Stefan Reinauer688b3852004-01-28 16:56:14 +000054{
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000055 int i, entries_num;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000056 acpi_rsdt_t *rsdt;
57 acpi_xsdt_t *xsdt = NULL;
58
Uwe Hermann622824c2010-11-19 15:14:42 +000059 /* The RSDT is mandatory... */
Stefan Reinauerdefee172015-06-18 01:11:19 -070060 rsdt = (acpi_rsdt_t *)(uintptr_t)rsdp->rsdt_address;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000061
Uwe Hermann622824c2010-11-19 15:14:42 +000062 /* ...while the XSDT is not. */
63 if (rsdp->xsdt_address)
Stefan Reinauerdefee172015-06-18 01:11:19 -070064 xsdt = (acpi_xsdt_t *)((uintptr_t)rsdp->xsdt_address);
Stefan Reinauer14e22772010-04-27 06:56:47 +000065
Uwe Hermann622824c2010-11-19 15:14:42 +000066 /* This should always be MAX_ACPI_TABLES. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000067 entries_num = ARRAY_SIZE(rsdt->entry);
Stefan Reinauer14e22772010-04-27 06:56:47 +000068
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000069 for (i = 0; i < entries_num; i++) {
Uwe Hermann622824c2010-11-19 15:14:42 +000070 if (rsdt->entry[i] == 0)
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000071 break;
Stefan Reinauer688b3852004-01-28 16:56:14 +000072 }
73
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000074 if (i >= entries_num) {
Uwe Hermann622824c2010-11-19 15:14:42 +000075 printk(BIOS_ERR, "ACPI: Error: Could not add ACPI table, "
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +030076 "too many tables.\n");
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000077 return;
78 }
79
Uwe Hermann622824c2010-11-19 15:14:42 +000080 /* Add table to the RSDT. */
Stefan Reinauerdefee172015-06-18 01:11:19 -070081 rsdt->entry[i] = (uintptr_t)table;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000082
Uwe Hermann622824c2010-11-19 15:14:42 +000083 /* Fix RSDT length or the kernel will assume invalid entries. */
84 rsdt->header.length = sizeof(acpi_header_t) + (sizeof(u32) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000085
Uwe Hermann622824c2010-11-19 15:14:42 +000086 /* Re-calculate checksum. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000087 rsdt->header.checksum = 0; /* Hope this won't get optimized away */
Uwe Hermann622824c2010-11-19 15:14:42 +000088 rsdt->header.checksum = acpi_checksum((u8 *)rsdt, rsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000089
Uwe Hermann622824c2010-11-19 15:14:42 +000090 /*
91 * And now the same thing for the XSDT. We use the same index as for
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000092 * now we want the XSDT and RSDT to always be in sync in coreboot.
93 */
94 if (xsdt) {
Uwe Hermann622824c2010-11-19 15:14:42 +000095 /* Add table to the XSDT. */
Stefan Reinauerdefee172015-06-18 01:11:19 -070096 xsdt->entry[i] = (u64)(uintptr_t)table;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000097
Uwe Hermann622824c2010-11-19 15:14:42 +000098 /* Fix XSDT length. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000099 xsdt->header.length = sizeof(acpi_header_t) +
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300100 (sizeof(u64) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000101
Uwe Hermann622824c2010-11-19 15:14:42 +0000102 /* Re-calculate checksum. */
103 xsdt->header.checksum = 0;
104 xsdt->header.checksum = acpi_checksum((u8 *)xsdt,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300105 xsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000106 }
107
Uwe Hermann622824c2010-11-19 15:14:42 +0000108 printk(BIOS_DEBUG, "ACPI: added table %d/%d, length now %d\n",
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300109 i + 1, entries_num, rsdt->header.length);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000110}
111
Uwe Hermann622824c2010-11-19 15:14:42 +0000112int acpi_create_mcfg_mmconfig(acpi_mcfg_mmconfig_t *mmconfig, u32 base,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300113 u16 seg_nr, u8 start, u8 end)
Stefan Reinauer26d431a2009-01-20 19:17:51 +0000114{
Vladimir Serbinenko60fccdc2014-10-05 11:05:16 +0200115 memset(mmconfig, 0, sizeof(*mmconfig));
Rudolf Mareke6409f22007-11-03 12:50:26 +0000116 mmconfig->base_address = base;
117 mmconfig->base_reserved = 0;
118 mmconfig->pci_segment_group_number = seg_nr;
119 mmconfig->start_bus_number = start;
120 mmconfig->end_bus_number = end;
Uwe Hermann622824c2010-11-19 15:14:42 +0000121
122 return sizeof(acpi_mcfg_mmconfig_t);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000123}
124
Stefan Reinauer777224c2005-01-19 14:06:41 +0000125int acpi_create_madt_lapic(acpi_madt_lapic_t *lapic, u8 cpu, u8 apic)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000126{
Himanshu Sahdev8c09b822019-10-21 18:50:50 +0530127 lapic->type = LOCAL_APIC; /* Local APIC structure */
Uwe Hermann622824c2010-11-19 15:14:42 +0000128 lapic->length = sizeof(acpi_madt_lapic_t);
129 lapic->flags = (1 << 0); /* Processor/LAPIC enabled */
130 lapic->processor_id = cpu;
131 lapic->apic_id = apic;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000132
Uwe Hermann622824c2010-11-19 15:14:42 +0000133 return lapic->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000134}
135
Patrick Rudolph56a3ef22020-03-24 17:29:49 +0100136int acpi_create_madt_lx2apic(acpi_madt_lx2apic_t *lapic, u32 cpu, u32 apic)
137{
138 lapic->type = LOCAL_X2APIC; /* Local APIC structure */
139 lapic->reserved = 0;
140 lapic->length = sizeof(acpi_madt_lx2apic_t);
141 lapic->flags = (1 << 0); /* Processor/LAPIC enabled */
142 lapic->processor_id = cpu;
143 lapic->x2apic_id = apic;
144
145 return lapic->length;
146}
147
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000148unsigned long acpi_create_madt_lapics(unsigned long current)
149{
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100150 struct device *cpu;
Werner Zeh423adfb2019-03-06 09:31:02 +0100151 int index, apic_ids[CONFIG_MAX_CPUS] = {0}, num_cpus = 0;
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000152
Uwe Hermann622824c2010-11-19 15:14:42 +0000153 for (cpu = all_devices; cpu; cpu = cpu->next) {
Fabio Aiuto45aae7f2022-09-23 16:51:34 +0200154 if (!is_enabled_cpu(cpu))
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000155 continue;
Werner Zehdb561e62019-02-19 13:39:56 +0100156 if (num_cpus >= ARRAY_SIZE(apic_ids))
157 break;
158 apic_ids[num_cpus++] = cpu->path.apic.apic_id;
159 }
Werner Zehfedb36e2019-03-12 07:07:50 +0100160 if (num_cpus > 1)
161 bubblesort(apic_ids, num_cpus, NUM_ASCENDING);
Werner Zehdb561e62019-02-19 13:39:56 +0100162 for (index = 0; index < num_cpus; index++) {
Patrick Rudolph56a3ef22020-03-24 17:29:49 +0100163 if (apic_ids[index] < 0xff)
164 current += acpi_create_madt_lapic((acpi_madt_lapic_t *)current,
165 index, apic_ids[index]);
166 else
167 current += acpi_create_madt_lx2apic((acpi_madt_lx2apic_t *)current,
168 index, apic_ids[index]);
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000169 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000170
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000171 return current;
172}
173
Uwe Hermann622824c2010-11-19 15:14:42 +0000174int acpi_create_madt_ioapic(acpi_madt_ioapic_t *ioapic, u8 id, u32 addr,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300175 u32 gsi_base)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000176{
Himanshu Sahdev8c09b822019-10-21 18:50:50 +0530177 ioapic->type = IO_APIC; /* I/O APIC structure */
Uwe Hermann622824c2010-11-19 15:14:42 +0000178 ioapic->length = sizeof(acpi_madt_ioapic_t);
179 ioapic->reserved = 0x00;
180 ioapic->gsi_base = gsi_base;
181 ioapic->ioapic_id = id;
182 ioapic->ioapic_addr = addr;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000183
Uwe Hermann622824c2010-11-19 15:14:42 +0000184 return ioapic->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000185}
186
Kyösti Mälkkic7da0272021-06-08 11:37:08 +0300187#if ENV_X86
188/* For a system with multiple I/O APICs it's required that the one potentially
189 routing i8259 via ExtNMI delivery calls this first to get GSI #0. */
190int acpi_create_madt_ioapic_from_hw(acpi_madt_ioapic_t *ioapic, u32 addr)
191{
192 static u32 gsi_base;
193 u32 my_base;
194 u8 id = get_ioapic_id((void *)(uintptr_t)addr);
195 u8 count = ioapic_get_max_vectors((void *)(uintptr_t)addr);
196
197 my_base = gsi_base;
198 gsi_base += count;
199 return acpi_create_madt_ioapic(ioapic, id, addr, my_base);
200}
201#endif
202
Stefan Reinauer777224c2005-01-19 14:06:41 +0000203int acpi_create_madt_irqoverride(acpi_madt_irqoverride_t *irqoverride,
Stefan Reinauer06feb882004-02-03 16:11:35 +0000204 u8 bus, u8 source, u32 gsirq, u16 flags)
205{
Himanshu Sahdev8c09b822019-10-21 18:50:50 +0530206 irqoverride->type = IRQ_SOURCE_OVERRIDE; /* Interrupt source override */
Uwe Hermann622824c2010-11-19 15:14:42 +0000207 irqoverride->length = sizeof(acpi_madt_irqoverride_t);
208 irqoverride->bus = bus;
209 irqoverride->source = source;
210 irqoverride->gsirq = gsirq;
211 irqoverride->flags = flags;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000212
Uwe Hermann622824c2010-11-19 15:14:42 +0000213 return irqoverride->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000214}
215
Stefan Reinauer777224c2005-01-19 14:06:41 +0000216int acpi_create_madt_lapic_nmi(acpi_madt_lapic_nmi_t *lapic_nmi, u8 cpu,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300217 u16 flags, u8 lint)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000218{
Himanshu Sahdev8c09b822019-10-21 18:50:50 +0530219 lapic_nmi->type = LOCAL_APIC_NMI; /* Local APIC NMI structure */
Uwe Hermann622824c2010-11-19 15:14:42 +0000220 lapic_nmi->length = sizeof(acpi_madt_lapic_nmi_t);
221 lapic_nmi->flags = flags;
222 lapic_nmi->processor_id = cpu;
223 lapic_nmi->lint = lint;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000224
Uwe Hermann622824c2010-11-19 15:14:42 +0000225 return lapic_nmi->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000226}
227
Patrick Rudolph56a3ef22020-03-24 17:29:49 +0100228int acpi_create_madt_lx2apic_nmi(acpi_madt_lx2apic_nmi_t *lapic_nmi, u32 cpu,
229 u16 flags, u8 lint)
230{
231 lapic_nmi->type = LOCAL_X2APIC_NMI; /* Local APIC NMI structure */
232 lapic_nmi->length = sizeof(acpi_madt_lx2apic_nmi_t);
233 lapic_nmi->flags = flags;
234 lapic_nmi->processor_id = cpu;
235 lapic_nmi->lint = lint;
236 lapic_nmi->reserved[0] = 0;
237 lapic_nmi->reserved[1] = 0;
238 lapic_nmi->reserved[2] = 0;
239
240 return lapic_nmi->length;
241}
242
Kyösti Mälkki66b5e1b2022-11-12 21:13:45 +0200243unsigned long acpi_create_madt_lapics_with_nmis(unsigned long current)
244{
245 const u16 flags = MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH;
246
247 current = acpi_create_madt_lapics(current);
248
249 /* 1: LINT1 connect to NMI */
250 /* create all subtables for processors */
251 current += acpi_create_madt_lapic_nmi((acpi_madt_lapic_nmi_t *)current,
252 ACPI_MADT_LAPIC_NMI_ALL_PROCESSORS, flags, 1);
253
254 if (!CONFIG(XAPIC_ONLY))
255 current += acpi_create_madt_lx2apic_nmi((acpi_madt_lx2apic_nmi_t *)current,
256 ACPI_MADT_LX2APIC_NMI_ALL_PROCESSORS, flags, 1);
257
258 return current;
259}
260
Stefan Reinauer777224c2005-01-19 14:06:41 +0000261void acpi_create_madt(acpi_madt_t *madt)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000262{
Uwe Hermann622824c2010-11-19 15:14:42 +0000263 acpi_header_t *header = &(madt->header);
264 unsigned long current = (unsigned long)madt + sizeof(acpi_madt_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000265
Stefan Reinauer06feb882004-02-03 16:11:35 +0000266 memset((void *)madt, 0, sizeof(acpi_madt_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000267
John Zhao2ba303e2019-05-28 16:48:14 -0700268 if (!header)
269 return;
270
Uwe Hermann622824c2010-11-19 15:14:42 +0000271 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000272 memcpy(header->signature, "APIC", 4);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000273 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000274 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000275 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000276
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100277 header->asl_compiler_revision = asl_revision;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000278 header->length = sizeof(acpi_madt_t);
Marc Jones93a51762018-08-22 18:57:24 -0600279 header->revision = get_acpi_table_revision(MADT);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000280
Furquan Shaikhb1859a62020-04-30 21:27:47 -0700281 madt->lapic_addr = cpu_get_lapic_addr();
Nico Huber9df72e02018-11-24 18:25:50 +0100282 if (CONFIG(ACPI_HAVE_PCAT_8259))
283 madt->flags |= 1;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000284
Kyösti Mälkkia5fa5342022-11-18 13:23:52 +0200285 if (!CONFIG(ACPI_NO_MADT))
286 current = acpi_fill_madt(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000287
Uwe Hermann622824c2010-11-19 15:14:42 +0000288 /* (Re)calculate length and checksum. */
289 header->length = current - (unsigned long)madt;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000290
Uwe Hermann622824c2010-11-19 15:14:42 +0000291 header->checksum = acpi_checksum((void *)madt, header->length);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000292}
293
Kyösti Mälkkib54388d2021-02-14 15:09:45 +0200294static unsigned long acpi_fill_mcfg(unsigned long current)
295{
296 current += acpi_create_mcfg_mmconfig((acpi_mcfg_mmconfig_t *)current,
Shelley Chen4e9bb332021-10-20 15:43:45 -0700297 CONFIG_ECAM_MMCONF_BASE_ADDRESS, 0, 0,
298 CONFIG_ECAM_MMCONF_BUS_NUMBER - 1);
Kyösti Mälkkib54388d2021-02-14 15:09:45 +0200299 return current;
300}
301
Uwe Hermann622824c2010-11-19 15:14:42 +0000302/* MCFG is defined in the PCI Firmware Specification 3.0. */
Rudolf Mareke6409f22007-11-03 12:50:26 +0000303void acpi_create_mcfg(acpi_mcfg_t *mcfg)
304{
Uwe Hermann622824c2010-11-19 15:14:42 +0000305 acpi_header_t *header = &(mcfg->header);
306 unsigned long current = (unsigned long)mcfg + sizeof(acpi_mcfg_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000307
Rudolf Mareke6409f22007-11-03 12:50:26 +0000308 memset((void *)mcfg, 0, sizeof(acpi_mcfg_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000309
John Zhao2ba303e2019-05-28 16:48:14 -0700310 if (!header)
311 return;
312
Uwe Hermann622824c2010-11-19 15:14:42 +0000313 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000314 memcpy(header->signature, "MCFG", 4);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000315 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000316 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000317 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000318
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100319 header->asl_compiler_revision = asl_revision;
Rudolf Mareke6409f22007-11-03 12:50:26 +0000320 header->length = sizeof(acpi_mcfg_t);
Marc Jones93a51762018-08-22 18:57:24 -0600321 header->revision = get_acpi_table_revision(MCFG);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000322
Shelley Chen4e9bb332021-10-20 15:43:45 -0700323 if (CONFIG(ECAM_MMCONF_SUPPORT))
Kyösti Mälkkib54388d2021-02-14 15:09:45 +0200324 current = acpi_fill_mcfg(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000325
Uwe Hermann622824c2010-11-19 15:14:42 +0000326 /* (Re)calculate length and checksum. */
327 header->length = current - (unsigned long)mcfg;
328 header->checksum = acpi_checksum((void *)mcfg, header->length);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000329}
330
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200331static void *get_tcpa_log(u32 *size)
332{
333 const struct cbmem_entry *ce;
334 const u32 tcpa_default_log_len = 0x10000;
335 void *lasa;
Philipp Deppenwiese791ba972018-07-30 01:15:20 +0200336 ce = cbmem_entry_find(CBMEM_ID_TCPA_TCG_LOG);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200337 if (ce) {
338 lasa = cbmem_entry_start(ce);
339 *size = cbmem_entry_size(ce);
340 printk(BIOS_DEBUG, "TCPA log found at %p\n", lasa);
341 return lasa;
342 }
Philipp Deppenwiese791ba972018-07-30 01:15:20 +0200343 lasa = cbmem_add(CBMEM_ID_TCPA_TCG_LOG, tcpa_default_log_len);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200344 if (!lasa) {
345 printk(BIOS_ERR, "TCPA log creation failed\n");
346 return NULL;
347 }
348
349 printk(BIOS_DEBUG, "TCPA log created at %p\n", lasa);
Lee Leahy024b13d2017-03-16 13:41:11 -0700350 memset(lasa, 0, tcpa_default_log_len);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200351
352 *size = tcpa_default_log_len;
353 return lasa;
354}
355
356static void acpi_create_tcpa(acpi_tcpa_t *tcpa)
357{
358 acpi_header_t *header = &(tcpa->header);
359 u32 tcpa_log_len;
360 void *lasa;
361
362 memset((void *)tcpa, 0, sizeof(acpi_tcpa_t));
363
364 lasa = get_tcpa_log(&tcpa_log_len);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700365 if (!lasa)
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200366 return;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200367
John Zhao2ba303e2019-05-28 16:48:14 -0700368 if (!header)
369 return;
370
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200371 /* Fill out header fields. */
372 memcpy(header->signature, "TCPA", 4);
373 memcpy(header->oem_id, OEM_ID, 6);
374 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
375 memcpy(header->asl_compiler_id, ASLC, 4);
376
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100377 header->asl_compiler_revision = asl_revision;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200378 header->length = sizeof(acpi_tcpa_t);
Marc Jones93a51762018-08-22 18:57:24 -0600379 header->revision = get_acpi_table_revision(TCPA);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200380
381 tcpa->platform_class = 0;
382 tcpa->laml = tcpa_log_len;
Elyes Haouas3141fbad2022-11-18 15:22:32 +0100383 tcpa->lasa = (uintptr_t)lasa;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200384
385 /* Calculate checksum. */
386 header->checksum = acpi_checksum((void *)tcpa, header->length);
387}
388
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100389static void *get_tpm2_log(u32 *size)
390{
391 const struct cbmem_entry *ce;
392 const u32 tpm2_default_log_len = 0x10000;
393 void *lasa;
394 ce = cbmem_entry_find(CBMEM_ID_TPM2_TCG_LOG);
395 if (ce) {
396 lasa = cbmem_entry_start(ce);
397 *size = cbmem_entry_size(ce);
398 printk(BIOS_DEBUG, "TPM2 log found at %p\n", lasa);
399 return lasa;
400 }
401 lasa = cbmem_add(CBMEM_ID_TPM2_TCG_LOG, tpm2_default_log_len);
402 if (!lasa) {
403 printk(BIOS_ERR, "TPM2 log creation failed\n");
404 return NULL;
405 }
406
407 printk(BIOS_DEBUG, "TPM2 log created at %p\n", lasa);
408 memset(lasa, 0, tpm2_default_log_len);
409
410 *size = tpm2_default_log_len;
411 return lasa;
412}
413
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200414static void acpi_create_tpm2(acpi_tpm2_t *tpm2)
415{
416 acpi_header_t *header = &(tpm2->header);
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100417 u32 tpm2_log_len;
418 void *lasa;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200419
420 memset((void *)tpm2, 0, sizeof(acpi_tpm2_t));
421
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100422 /*
423 * Some payloads like SeaBIOS depend on log area to use TPM2.
424 * Get the memory size and address of TPM2 log area or initialize it.
425 */
426 lasa = get_tpm2_log(&tpm2_log_len);
427 if (!lasa)
428 tpm2_log_len = 0;
429
John Zhao2ba303e2019-05-28 16:48:14 -0700430 if (!header)
431 return;
432
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200433 /* Fill out header fields. */
434 memcpy(header->signature, "TPM2", 4);
435 memcpy(header->oem_id, OEM_ID, 6);
436 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
437 memcpy(header->asl_compiler_id, ASLC, 4);
438
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100439 header->asl_compiler_revision = asl_revision;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200440 header->length = sizeof(acpi_tpm2_t);
441 header->revision = get_acpi_table_revision(TPM2);
442
443 /* Hard to detect for coreboot. Just set it to 0 */
444 tpm2->platform_class = 0;
Christian Walter26e0d4c2019-07-14 15:47:23 +0200445 if (CONFIG(CRB_TPM)) {
446 /* Must be set to 7 for CRB Support */
447 tpm2->control_area = CONFIG_CRB_TPM_BASE_ADDRESS + 0x40;
448 tpm2->start_method = 7;
449 } else {
450 /* Must be set to 0 for FIFO interface support */
451 tpm2->control_area = 0;
452 tpm2->start_method = 6;
453 }
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200454 memset(tpm2->msp, 0, sizeof(tpm2->msp));
455
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100456 /* Fill the log area size and start address fields. */
457 tpm2->laml = tpm2_log_len;
Elyes Haouas3141fbad2022-11-18 15:22:32 +0100458 tpm2->lasa = (uintptr_t)lasa;
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100459
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200460 /* Calculate checksum. */
461 header->checksum = acpi_checksum((void *)tpm2, header->length);
462}
463
Duncan Laurie37319032016-05-26 12:47:05 -0700464static void acpi_ssdt_write_cbtable(void)
465{
466 const struct cbmem_entry *cbtable;
467 uintptr_t base;
468 uint32_t size;
469
470 cbtable = cbmem_entry_find(CBMEM_ID_CBTABLE);
471 if (!cbtable)
472 return;
473 base = (uintptr_t)cbmem_entry_start(cbtable);
474 size = cbmem_entry_size(cbtable);
475
476 acpigen_write_device("CTBL");
477 acpigen_write_coreboot_hid(COREBOOT_ACPI_ID_CBTABLE);
478 acpigen_write_name_integer("_UID", 0);
David Wu5dff3962018-03-21 16:48:53 +0800479 acpigen_write_STA(ACPI_STATUS_DEVICE_HIDDEN_ON);
Duncan Laurie37319032016-05-26 12:47:05 -0700480 acpigen_write_name("_CRS");
481 acpigen_write_resourcetemplate_header();
482 acpigen_write_mem32fixed(0, base, size);
483 acpigen_write_resourcetemplate_footer();
484 acpigen_pop_len();
485}
486
Myles Watson3fe6b702009-10-09 20:13:43 +0000487void acpi_create_ssdt_generator(acpi_header_t *ssdt, const char *oem_table_id)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000488{
Uwe Hermann622824c2010-11-19 15:14:42 +0000489 unsigned long current = (unsigned long)ssdt + sizeof(acpi_header_t);
490
Rudolf Marek293b5f52009-02-01 18:35:15 +0000491 memset((void *)ssdt, 0, sizeof(acpi_header_t));
Uwe Hermann622824c2010-11-19 15:14:42 +0000492
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000493 memcpy(&ssdt->signature, "SSDT", 4);
Marc Jones93a51762018-08-22 18:57:24 -0600494 ssdt->revision = get_acpi_table_revision(SSDT);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000495 memcpy(&ssdt->oem_id, OEM_ID, 6);
496 memcpy(&ssdt->oem_table_id, oem_table_id, 8);
497 ssdt->oem_revision = 42;
Uwe Hermann622824c2010-11-19 15:14:42 +0000498 memcpy(&ssdt->asl_compiler_id, ASLC, 4);
Elyes HAOUAS26071aa2019-02-15 08:21:33 +0100499 ssdt->asl_compiler_revision = asl_revision;
Rudolf Marek293b5f52009-02-01 18:35:15 +0000500 ssdt->length = sizeof(acpi_header_t);
501
Elyes Haouas3141fbad2022-11-18 15:22:32 +0100502 acpigen_set_current((char *)current);
Duncan Laurie37319032016-05-26 12:47:05 -0700503
504 /* Write object to declare coreboot tables */
505 acpi_ssdt_write_cbtable();
506
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200507 {
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100508 struct device *dev;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200509 for (dev = all_devices; dev; dev = dev->next)
Karthikeyan Ramasubramaniand1c0f952020-11-02 16:26:52 -0700510 if (dev->enabled && dev->ops && dev->ops->acpi_fill_ssdt)
Nico Huber68680dd2020-03-31 17:34:52 +0200511 dev->ops->acpi_fill_ssdt(dev);
Elyes Haouas3141fbad2022-11-18 15:22:32 +0100512 current = (unsigned long)acpigen_get_current();
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200513 }
Rudolf Marek293b5f52009-02-01 18:35:15 +0000514
Uwe Hermann622824c2010-11-19 15:14:42 +0000515 /* (Re)calculate length and checksum. */
Rudolf Marek293b5f52009-02-01 18:35:15 +0000516 ssdt->length = current - (unsigned long)ssdt;
517 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
518}
519
Stefan Reinauerf622d592005-11-26 16:56:05 +0000520int acpi_create_srat_lapic(acpi_srat_lapic_t *lapic, u8 node, u8 apic)
521{
Myles Watson0b4c9f02009-03-10 18:06:47 +0000522 memset((void *)lapic, 0, sizeof(acpi_srat_lapic_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000523
Uwe Hermann622824c2010-11-19 15:14:42 +0000524 lapic->type = 0; /* Processor local APIC/SAPIC affinity structure */
525 lapic->length = sizeof(acpi_srat_lapic_t);
526 lapic->flags = (1 << 0); /* Enabled (the use of this structure). */
527 lapic->proximity_domain_7_0 = node;
528 /* TODO: proximity_domain_31_8, local SAPIC EID, clock domain. */
529 lapic->apic_id = apic;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000530
Uwe Hermann622824c2010-11-19 15:14:42 +0000531 return lapic->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000532}
533
Naresh Solanki76835cc2023-01-20 19:13:02 +0100534int acpi_create_srat_x2apic(acpi_srat_x2apic_t *x2apic, u32 node, u32 apic)
535{
536 memset((void *)x2apic, 0, sizeof(acpi_srat_x2apic_t));
537
538 x2apic->type = 2; /* Processor x2APIC structure */
539 x2apic->length = sizeof(acpi_srat_x2apic_t);
540 x2apic->flags = (1 << 0); /* Enabled (the use of this structure). */
541 x2apic->proximity_domain = node;
542 x2apic->x2apic_id = apic;
543
544 return x2apic->length;
545}
546
Uwe Hermann622824c2010-11-19 15:14:42 +0000547int 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 +0300548 u32 flags)
Stefan Reinauerf622d592005-11-26 16:56:05 +0000549{
Uwe Hermann622824c2010-11-19 15:14:42 +0000550 mem->type = 1; /* Memory affinity structure */
551 mem->length = sizeof(acpi_srat_mem_t);
552 mem->base_address_low = (basek << 10);
553 mem->base_address_high = (basek >> (32 - 10));
554 mem->length_low = (sizek << 10);
555 mem->length_high = (sizek >> (32 - 10));
556 mem->proximity_domain = node;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000557 mem->flags = flags;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000558
Uwe Hermann622824c2010-11-19 15:14:42 +0000559 return mem->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000560}
561
Jonathan Zhang3164b642021-04-21 17:51:31 -0700562int acpi_create_srat_gia_pci(acpi_srat_gia_t *gia, u32 proximity_domain,
563 u16 seg, u8 bus, u8 dev, u8 func, u32 flags)
564{
565 gia->type = ACPI_SRAT_STRUCTURE_GIA;
566 gia->length = sizeof(acpi_srat_gia_t);
567 gia->proximity_domain = proximity_domain;
568 gia->dev_handle_type = ACPI_SRAT_GIA_DEV_HANDLE_PCI;
569 /* First two bytes has segment number */
570 memcpy(gia->dev_handle, &seg, 2);
571 gia->dev_handle[2] = bus; /* Byte 2 has bus number */
572 /* Byte 3 has bits 7:3 for dev, bits 2:0 for func */
573 gia->dev_handle[3] = PCI_SLOT(dev) | PCI_FUNC(func);
574 gia->flags = flags;
575
576 return gia->length;
577}
578
Uwe Hermann622824c2010-11-19 15:14:42 +0000579/* http://www.microsoft.com/whdc/system/sysinternals/sratdwn.mspx */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +0200580void acpi_create_srat(acpi_srat_t *srat,
581 unsigned long (*acpi_fill_srat)(unsigned long current))
Stefan Reinauerf622d592005-11-26 16:56:05 +0000582{
Uwe Hermann622824c2010-11-19 15:14:42 +0000583 acpi_header_t *header = &(srat->header);
584 unsigned long current = (unsigned long)srat + sizeof(acpi_srat_t);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000585
Uwe Hermann622824c2010-11-19 15:14:42 +0000586 memset((void *)srat, 0, sizeof(acpi_srat_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000587
John Zhao2ba303e2019-05-28 16:48:14 -0700588 if (!header)
589 return;
590
Uwe Hermann622824c2010-11-19 15:14:42 +0000591 /* Fill out header fields. */
592 memcpy(header->signature, "SRAT", 4);
593 memcpy(header->oem_id, OEM_ID, 6);
594 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
595 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000596
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100597 header->asl_compiler_revision = asl_revision;
Uwe Hermann622824c2010-11-19 15:14:42 +0000598 header->length = sizeof(acpi_srat_t);
Marc Jones93a51762018-08-22 18:57:24 -0600599 header->revision = get_acpi_table_revision(SRAT);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000600
Uwe Hermann622824c2010-11-19 15:14:42 +0000601 srat->resv = 1; /* Spec: Reserved to 1 for backwards compatibility. */
Stefan Reinauerf622d592005-11-26 16:56:05 +0000602
Uwe Hermann622824c2010-11-19 15:14:42 +0000603 current = acpi_fill_srat(current);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000604
Uwe Hermann622824c2010-11-19 15:14:42 +0000605 /* (Re)calculate length and checksum. */
606 header->length = current - (unsigned long)srat;
607 header->checksum = acpi_checksum((void *)srat, header->length);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000608}
609
Jonathan Zhang3dcafa82022-05-11 13:11:20 -0700610int acpi_create_cedt_chbs(acpi_cedt_chbs_t *chbs, u32 uid, u32 cxl_ver, u64 base)
611{
612 memset((void *)chbs, 0, sizeof(acpi_cedt_chbs_t));
613
614 chbs->type = ACPI_CEDT_STRUCTURE_CHBS;
615 chbs->length = sizeof(acpi_cedt_chbs_t);
616 chbs->uid = uid;
617 chbs->cxl_ver = cxl_ver;
618 chbs->base = base;
619
620 /*
621 * CXL spec 2.0 section 9.14.1.2 "CXL CHBS"
622 * CXL 1.1 spec compliant host bridge: 8KB
623 * CXL 2.0 spec compliant host bridge: 64KB
624 */
625 if (cxl_ver == ACPI_CEDT_CHBS_CXL_VER_1_1)
626 chbs->len = 8 * KiB;
627 else if (cxl_ver == ACPI_CEDT_CHBS_CXL_VER_2_0)
628 chbs->len = 64 * KiB;
629 else
630 printk(BIOS_ERR, "ACPI(%s:%s): Incorrect CXL version:%d\n", __FILE__, __func__,
631 cxl_ver);
632
633 return chbs->length;
634}
635
636int acpi_create_cedt_cfmws(acpi_cedt_cfmws_t *cfmws, u64 base_hpa, u64 window_size, u8 eniw,
637 u32 hbig, u16 restriction, u16 qtg_id, const u32 *interleave_target)
638{
639 memset((void *)cfmws, 0, sizeof(acpi_cedt_cfmws_t));
640
641 cfmws->type = ACPI_CEDT_STRUCTURE_CFMWS;
642
643 u8 niw = 0;
644 if (eniw >= 8)
645 printk(BIOS_ERR, "ACPI(%s:%s): Incorrect eniw::%d\n", __FILE__, __func__, eniw);
646 else
647 /* NIW = 2 ** ENIW */
648 niw = 0x1 << eniw;
649 /* 36 + 4 * NIW */
650 cfmws->length = sizeof(acpi_cedt_cfmws_t) + 4 * niw;
651
652 cfmws->base_hpa = base_hpa;
653 cfmws->window_size = window_size;
654 cfmws->eniw = eniw;
655
656 // 0: Standard Modulo Arithmetic. Other values reserved.
657 cfmws->interleave_arithmetic = 0;
658
659 cfmws->hbig = hbig;
660 cfmws->restriction = restriction;
661 cfmws->qtg_id = qtg_id;
662 memcpy(&cfmws->interleave_target, interleave_target, 4 * niw);
663
664 return cfmws->length;
665}
666
667void acpi_create_cedt(acpi_cedt_t *cedt, unsigned long (*acpi_fill_cedt)(unsigned long current))
668{
669 acpi_header_t *header = &(cedt->header);
670 unsigned long current = (unsigned long)cedt + sizeof(acpi_cedt_t);
671
672 memset((void *)cedt, 0, sizeof(acpi_cedt_t));
673
674 if (!header)
675 return;
676
677 /* Fill out header fields. */
678 memcpy(header->signature, "CEDT", 4);
679 memcpy(header->oem_id, OEM_ID, 6);
680 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
681 memcpy(header->asl_compiler_id, ASLC, 4);
682
683 header->asl_compiler_revision = asl_revision;
684 header->length = sizeof(acpi_cedt_t);
685 header->revision = get_acpi_table_revision(CEDT);
686
687 current = acpi_fill_cedt(current);
688
689 /* (Re)calculate length and checksum. */
690 header->length = current - (unsigned long)cedt;
691 header->checksum = acpi_checksum((void *)cedt, header->length);
692}
693
Jonathan Zhang2a4e1f42021-04-01 11:43:37 -0700694int acpi_create_hmat_mpda(acpi_hmat_mpda_t *mpda, u32 initiator, u32 memory)
695{
696 memset((void *)mpda, 0, sizeof(acpi_hmat_mpda_t));
697
698 mpda->type = 0; /* Memory Proximity Domain Attributes structure */
699 mpda->length = sizeof(acpi_hmat_mpda_t);
700 /*
701 * Proximity Domain for Attached Initiator field is valid.
702 * Bit 1 and bit 2 are reserved since HMAT revision 2.
703 */
704 mpda->flags = (1 << 0);
705 mpda->proximity_domain_initiator = initiator;
706 mpda->proximity_domain_memory = memory;
707
708 return mpda->length;
709}
710
711void acpi_create_hmat(acpi_hmat_t *hmat,
712 unsigned long (*acpi_fill_hmat)(unsigned long current))
713{
714 acpi_header_t *header = &(hmat->header);
715 unsigned long current = (unsigned long)hmat + sizeof(acpi_hmat_t);
716
717 memset((void *)hmat, 0, sizeof(acpi_hmat_t));
718
719 if (!header)
720 return;
721
722 /* Fill out header fields. */
723 memcpy(header->signature, "HMAT", 4);
724 memcpy(header->oem_id, OEM_ID, 6);
725 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
726 memcpy(header->asl_compiler_id, ASLC, 4);
727
728 header->asl_compiler_revision = asl_revision;
729 header->length = sizeof(acpi_hmat_t);
730 header->revision = get_acpi_table_revision(HMAT);
731
732 current = acpi_fill_hmat(current);
733
734 /* (Re)calculate length and checksum. */
735 header->length = current - (unsigned long)hmat;
736 header->checksum = acpi_checksum((void *)hmat, header->length);
737}
738
Nico Hubere561f352015-10-26 11:51:25 +0100739void acpi_create_dmar(acpi_dmar_t *dmar, enum dmar_flags flags,
Lee Leahy024b13d2017-03-16 13:41:11 -0700740 unsigned long (*acpi_fill_dmar)(unsigned long))
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200741{
742 acpi_header_t *header = &(dmar->header);
743 unsigned long current = (unsigned long)dmar + sizeof(acpi_dmar_t);
744
745 memset((void *)dmar, 0, sizeof(acpi_dmar_t));
746
John Zhao2ba303e2019-05-28 16:48:14 -0700747 if (!header)
748 return;
749
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200750 /* Fill out header fields. */
751 memcpy(header->signature, "DMAR", 4);
752 memcpy(header->oem_id, OEM_ID, 6);
753 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
754 memcpy(header->asl_compiler_id, ASLC, 4);
755
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100756 header->asl_compiler_revision = asl_revision;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200757 header->length = sizeof(acpi_dmar_t);
Marc Jones93a51762018-08-22 18:57:24 -0600758 header->revision = get_acpi_table_revision(DMAR);
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200759
Jacob Laskaf3f654d2016-03-15 21:53:27 -0500760 dmar->host_address_width = cpu_phys_address_size() - 1;
Nico Hubere561f352015-10-26 11:51:25 +0100761 dmar->flags = flags;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200762
763 current = acpi_fill_dmar(current);
764
765 /* (Re)calculate length and checksum. */
766 header->length = current - (unsigned long)dmar;
767 header->checksum = acpi_checksum((void *)dmar, header->length);
768}
769
770unsigned long acpi_create_dmar_drhd(unsigned long current, u8 flags,
Matt DeVillier7866d492018-03-29 14:59:57 +0200771 u16 segment, u64 bar)
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200772{
773 dmar_entry_t *drhd = (dmar_entry_t *)current;
774 memset(drhd, 0, sizeof(*drhd));
775 drhd->type = DMAR_DRHD;
776 drhd->length = sizeof(*drhd); /* will be fixed up later */
777 drhd->flags = flags;
778 drhd->segment = segment;
779 drhd->bar = bar;
780
781 return drhd->length;
782}
783
Matt DeVillier7866d492018-03-29 14:59:57 +0200784unsigned long acpi_create_dmar_rmrr(unsigned long current, u16 segment,
785 u64 bar, u64 limit)
786{
787 dmar_rmrr_entry_t *rmrr = (dmar_rmrr_entry_t *)current;
788 memset(rmrr, 0, sizeof(*rmrr));
789 rmrr->type = DMAR_RMRR;
790 rmrr->length = sizeof(*rmrr); /* will be fixed up later */
791 rmrr->segment = segment;
792 rmrr->bar = bar;
793 rmrr->limit = limit;
794
795 return rmrr->length;
796}
797
Werner Zehd4d76952016-07-27 06:56:36 +0200798unsigned long acpi_create_dmar_atsr(unsigned long current, u8 flags,
799 u16 segment)
800{
801 dmar_atsr_entry_t *atsr = (dmar_atsr_entry_t *)current;
802 memset(atsr, 0, sizeof(*atsr));
803 atsr->type = DMAR_ATSR;
804 atsr->length = sizeof(*atsr); /* will be fixed up later */
805 atsr->flags = flags;
806 atsr->segment = segment;
807
808 return atsr->length;
809}
810
John Zhao76e70672019-04-04 11:03:28 -0700811unsigned long acpi_create_dmar_rhsa(unsigned long current, u64 base_addr,
812 u32 proximity_domain)
813{
814 dmar_rhsa_entry_t *rhsa = (dmar_rhsa_entry_t *)current;
815 memset(rhsa, 0, sizeof(*rhsa));
816 rhsa->type = DMAR_RHSA;
817 rhsa->length = sizeof(*rhsa);
818 rhsa->base_address = base_addr;
819 rhsa->proximity_domain = proximity_domain;
820
821 return rhsa->length;
822}
823
824unsigned long acpi_create_dmar_andd(unsigned long current, u8 device_number,
825 const char *device_name)
826{
827 dmar_andd_entry_t *andd = (dmar_andd_entry_t *)current;
828 int andd_len = sizeof(dmar_andd_entry_t) + strlen(device_name) + 1;
829 memset(andd, 0, andd_len);
830 andd->type = DMAR_ANDD;
831 andd->length = andd_len;
832 andd->device_number = device_number;
833 memcpy(&andd->device_name, device_name, strlen(device_name));
834
835 return andd->length;
836}
837
John Zhao091532d2021-04-17 16:03:21 -0700838unsigned long acpi_create_dmar_satc(unsigned long current, u8 flags, u16 segment)
John Zhao6edbb182021-03-24 11:55:09 -0700839{
840 dmar_satc_entry_t *satc = (dmar_satc_entry_t *)current;
John Zhao091532d2021-04-17 16:03:21 -0700841 int satc_len = sizeof(dmar_satc_entry_t);
John Zhao6edbb182021-03-24 11:55:09 -0700842 memset(satc, 0, satc_len);
843 satc->type = DMAR_SATC;
844 satc->length = satc_len;
845 satc->flags = flags;
846 satc->segment_number = segment;
John Zhao6edbb182021-03-24 11:55:09 -0700847
848 return satc->length;
849}
850
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200851void acpi_dmar_drhd_fixup(unsigned long base, unsigned long current)
852{
853 dmar_entry_t *drhd = (dmar_entry_t *)base;
854 drhd->length = current - base;
855}
856
Matt DeVillier7866d492018-03-29 14:59:57 +0200857void acpi_dmar_rmrr_fixup(unsigned long base, unsigned long current)
858{
859 dmar_rmrr_entry_t *rmrr = (dmar_rmrr_entry_t *)base;
860 rmrr->length = current - base;
861}
862
Werner Zehd4d76952016-07-27 06:56:36 +0200863void acpi_dmar_atsr_fixup(unsigned long base, unsigned long current)
864{
865 dmar_atsr_entry_t *atsr = (dmar_atsr_entry_t *)base;
866 atsr->length = current - base;
867}
868
John Zhao6edbb182021-03-24 11:55:09 -0700869void acpi_dmar_satc_fixup(unsigned long base, unsigned long current)
870{
871 dmar_satc_entry_t *satc = (dmar_satc_entry_t *)base;
872 satc->length = current - base;
873}
874
Matt DeVillier7866d492018-03-29 14:59:57 +0200875static unsigned long acpi_create_dmar_ds(unsigned long current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100876 enum dev_scope_type type, u8 enumeration_id, u8 bus, u8 dev, u8 fn)
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200877{
Nico Huber6c4751d2015-10-26 12:03:54 +0100878 /* we don't support longer paths yet */
879 const size_t dev_scope_length = sizeof(dev_scope_t) + 2;
880
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200881 dev_scope_t *ds = (dev_scope_t *)current;
Nico Huber6c4751d2015-10-26 12:03:54 +0100882 memset(ds, 0, dev_scope_length);
883 ds->type = type;
884 ds->length = dev_scope_length;
885 ds->enumeration = enumeration_id;
886 ds->start_bus = bus;
887 ds->path[0].dev = dev;
888 ds->path[0].fn = fn;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200889
890 return ds->length;
891}
892
Matt DeVillier7866d492018-03-29 14:59:57 +0200893unsigned long acpi_create_dmar_ds_pci_br(unsigned long current, u8 bus,
Werner Zeh21a5bff2016-07-27 07:07:20 +0200894 u8 dev, u8 fn)
895{
Matt DeVillier7866d492018-03-29 14:59:57 +0200896 return acpi_create_dmar_ds(current,
Werner Zeh21a5bff2016-07-27 07:07:20 +0200897 SCOPE_PCI_SUB, 0, bus, dev, fn);
898}
899
Matt DeVillier7866d492018-03-29 14:59:57 +0200900unsigned long acpi_create_dmar_ds_pci(unsigned long current, u8 bus,
Nico Huber6c4751d2015-10-26 12:03:54 +0100901 u8 dev, u8 fn)
902{
Matt DeVillier7866d492018-03-29 14:59:57 +0200903 return acpi_create_dmar_ds(current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100904 SCOPE_PCI_ENDPOINT, 0, bus, dev, fn);
905}
906
Matt DeVillier7866d492018-03-29 14:59:57 +0200907unsigned long acpi_create_dmar_ds_ioapic(unsigned long current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100908 u8 enumeration_id, u8 bus, u8 dev, u8 fn)
909{
Matt DeVillier7866d492018-03-29 14:59:57 +0200910 return acpi_create_dmar_ds(current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100911 SCOPE_IOAPIC, enumeration_id, bus, dev, fn);
912}
913
Arthur Heymansbc8f8592022-12-02 13:17:39 +0100914unsigned long acpi_create_dmar_ds_ioapic_from_hw(unsigned long current,
915 u32 addr, u8 bus, u8 dev, u8 fn)
916{
917 u8 enumeration_id = get_ioapic_id((void *)(uintptr_t)addr);
918 return acpi_create_dmar_ds(current,
919 SCOPE_IOAPIC, enumeration_id, bus, dev, fn);
920}
921
Matt DeVillier7866d492018-03-29 14:59:57 +0200922unsigned long acpi_create_dmar_ds_msi_hpet(unsigned long current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100923 u8 enumeration_id, u8 bus, u8 dev, u8 fn)
924{
Matt DeVillier7866d492018-03-29 14:59:57 +0200925 return acpi_create_dmar_ds(current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100926 SCOPE_MSI_HPET, enumeration_id, bus, dev, fn);
927}
928
Uwe Hermann622824c2010-11-19 15:14:42 +0000929/* http://h21007.www2.hp.com/portal/download/files/unprot/Itanium/slit.pdf */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +0200930void acpi_create_slit(acpi_slit_t *slit,
931 unsigned long (*acpi_fill_slit)(unsigned long current))
Yinghai Lud4b278c2006-10-04 20:46:15 +0000932{
Uwe Hermann622824c2010-11-19 15:14:42 +0000933 acpi_header_t *header = &(slit->header);
934 unsigned long current = (unsigned long)slit + sizeof(acpi_slit_t);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000935
Uwe Hermann622824c2010-11-19 15:14:42 +0000936 memset((void *)slit, 0, sizeof(acpi_slit_t));
Yinghai Lud4b278c2006-10-04 20:46:15 +0000937
John Zhao2ba303e2019-05-28 16:48:14 -0700938 if (!header)
939 return;
940
Uwe Hermann622824c2010-11-19 15:14:42 +0000941 /* Fill out header fields. */
942 memcpy(header->signature, "SLIT", 4);
943 memcpy(header->oem_id, OEM_ID, 6);
944 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
945 memcpy(header->asl_compiler_id, ASLC, 4);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000946
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100947 header->asl_compiler_revision = asl_revision;
Uwe Hermann622824c2010-11-19 15:14:42 +0000948 header->length = sizeof(acpi_slit_t);
Marc Jones93a51762018-08-22 18:57:24 -0600949 header->revision = get_acpi_table_revision(SLIT);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000950
Uwe Hermann622824c2010-11-19 15:14:42 +0000951 current = acpi_fill_slit(current);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000952
Uwe Hermann622824c2010-11-19 15:14:42 +0000953 /* (Re)calculate length and checksum. */
954 header->length = current - (unsigned long)slit;
955 header->checksum = acpi_checksum((void *)slit, header->length);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000956}
957
Uwe Hermann622824c2010-11-19 15:14:42 +0000958/* http://www.intel.com/hardwaredesign/hpetspec_1.pdf */
Stefan Reinauer777224c2005-01-19 14:06:41 +0000959void acpi_create_hpet(acpi_hpet_t *hpet)
Stefan Reinauer688b3852004-01-28 16:56:14 +0000960{
Uwe Hermann622824c2010-11-19 15:14:42 +0000961 acpi_header_t *header = &(hpet->header);
962 acpi_addr_t *addr = &(hpet->addr);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000963
Stefan Reinauera7648c22004-01-29 17:31:34 +0000964 memset((void *)hpet, 0, sizeof(acpi_hpet_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000965
John Zhao2ba303e2019-05-28 16:48:14 -0700966 if (!header)
967 return;
968
Uwe Hermann622824c2010-11-19 15:14:42 +0000969 /* Fill out header fields. */
Stefan Reinauer4704dc52009-07-22 01:11:37 +0000970 memcpy(header->signature, "HPET", 4);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000971 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000972 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000973 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000974
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100975 header->asl_compiler_revision = asl_revision;
Stefan Reinauer688b3852004-01-28 16:56:14 +0000976 header->length = sizeof(acpi_hpet_t);
Marc Jones93a51762018-08-22 18:57:24 -0600977 header->revision = get_acpi_table_revision(HPET);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000978
Uwe Hermann622824c2010-11-19 15:14:42 +0000979 /* Fill out HPET address. */
Elyes HAOUAS04071f42020-07-20 17:05:24 +0200980 addr->space_id = ACPI_ADDRESS_SPACE_MEMORY;
Uwe Hermann622824c2010-11-19 15:14:42 +0000981 addr->bit_width = 64;
982 addr->bit_offset = 0;
Felix Held972d9f22022-02-23 16:32:20 +0100983 addr->addrl = HPET_BASE_ADDRESS & 0xffffffff;
984 addr->addrh = ((unsigned long long)HPET_BASE_ADDRESS) >> 32;
Stefan Reinauer688b3852004-01-28 16:56:14 +0000985
Felix Held972d9f22022-02-23 16:32:20 +0100986 hpet->id = read32p(HPET_BASE_ADDRESS);
Uwe Hermann622824c2010-11-19 15:14:42 +0000987 hpet->number = 0;
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200988 hpet->min_tick = CONFIG_HPET_MIN_TICKS;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000989
Uwe Hermann622824c2010-11-19 15:14:42 +0000990 header->checksum = acpi_checksum((void *)hpet, sizeof(acpi_hpet_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000991}
Uwe Hermann622824c2010-11-19 15:14:42 +0000992
Rocky Phaguraeff07132021-01-10 15:42:50 -0800993/*
994 * This method adds the ACPI error injection capability. It fills the default information.
995 * HW dependent code (caller) can modify the defaults upon return. If no changes are necessary
996 * and the defaults are acceptable then caller can simply add the table (acpi_add_table).
997 * INPUTS:
998 * einj - ptr to the starting location of EINJ table
999 * actions - number of actions to trigger an error (HW dependent)
1000 * addr - address of trigger action table. This should be ACPI reserved memory and it will be
1001 * shared between OS and FW.
1002 */
1003void acpi_create_einj(acpi_einj_t *einj, uintptr_t addr, u8 actions)
1004{
1005 int i;
1006 acpi_header_t *header = &(einj->header);
1007 acpi_injection_header_t *inj_header = &(einj->inj_header);
1008 acpi_einj_smi_t *einj_smi = (acpi_einj_smi_t *)addr;
1009 acpi_einj_trigger_table_t *tat;
1010 if (!header)
1011 return;
1012
1013 printk(BIOS_DEBUG, "%s einj_smi = %p\n", __func__, einj_smi);
1014 memset(einj_smi, 0, sizeof(acpi_einj_smi_t));
Jonathan Zhangd5d9b282022-11-07 17:30:14 -08001015 tat = (acpi_einj_trigger_table_t *)((uint8_t *)einj_smi + sizeof(acpi_einj_smi_t));
Rocky Phaguraeff07132021-01-10 15:42:50 -08001016 tat->header_size = 16;
1017 tat->revision = 0;
1018 tat->table_size = sizeof(acpi_einj_trigger_table_t) +
1019 sizeof(acpi_einj_action_table_t) * actions - 1;
1020 tat->entry_count = actions;
1021 printk(BIOS_DEBUG, "%s trigger_action_table = %p\n", __func__, tat);
1022
1023 for (i = 0; i < actions; i++) {
1024 tat->trigger_action[i].action = TRIGGER_ERROR;
1025 tat->trigger_action[i].instruction = NO_OP;
1026 tat->trigger_action[i].flags = FLAG_IGNORE;
1027 tat->trigger_action[i].reg.space_id = ACPI_ADDRESS_SPACE_MEMORY;
1028 tat->trigger_action[i].reg.bit_width = 64;
1029 tat->trigger_action[i].reg.bit_offset = 0;
1030 tat->trigger_action[i].reg.access_size = ACPI_ACCESS_SIZE_QWORD_ACCESS;
1031 tat->trigger_action[i].reg.addr = 0;
1032 tat->trigger_action[i].value = 0;
1033 tat->trigger_action[i].mask = 0xFFFFFFFF;
1034 }
1035
1036 acpi_einj_action_table_t default_actions[ACTION_COUNT] = {
1037 [0] = {
1038 .action = BEGIN_INJECT_OP,
1039 .instruction = WRITE_REGISTER_VALUE,
1040 .flags = FLAG_PRESERVE,
1041 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->op_state),
1042 .value = 0,
1043 .mask = 0xFFFFFFFF
1044 },
1045 [1] = {
1046 .action = GET_TRIGGER_ACTION_TABLE,
1047 .instruction = READ_REGISTER,
1048 .flags = FLAG_IGNORE,
1049 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->trigger_action_table),
1050 .value = 0,
1051 .mask = 0xFFFFFFFFFFFFFFFF
1052 },
1053 [2] = {
1054 .action = SET_ERROR_TYPE,
1055 .instruction = WRITE_REGISTER,
1056 .flags = FLAG_PRESERVE,
1057 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->err_inject[0]),
1058 .value = 0,
1059 .mask = 0xFFFFFFFF
1060 },
1061 [3] = {
1062 .action = GET_ERROR_TYPE,
1063 .instruction = READ_REGISTER,
1064 .flags = FLAG_IGNORE,
1065 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->err_inj_cap),
1066 .value = 0,
1067 .mask = 0xFFFFFFFF
1068 },
1069 [4] = {
1070 .action = END_INJECT_OP,
1071 .instruction = WRITE_REGISTER_VALUE,
1072 .flags = FLAG_PRESERVE,
1073 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->op_state),
1074 .value = 0,
1075 .mask = 0xFFFFFFFF
1076
1077 },
1078 [5] = {
1079 .action = EXECUTE_INJECT_OP,
1080 .instruction = WRITE_REGISTER_VALUE,
1081 .flags = FLAG_PRESERVE,
1082 .reg = EINJ_REG_IO(),
1083 .value = 0x9a,
1084 .mask = 0xFFFF,
1085 },
1086 [6] = {
1087 .action = CHECK_BUSY_STATUS,
1088 .instruction = READ_REGISTER_VALUE,
1089 .flags = FLAG_IGNORE,
1090 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->op_status),
1091 .value = 1,
1092 .mask = 1,
1093 },
1094 [7] = {
1095 .action = GET_CMD_STATUS,
1096 .instruction = READ_REGISTER,
1097 .flags = FLAG_PRESERVE,
1098 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->cmd_sts),
1099 .value = 0,
1100 .mask = 0x1fe,
1101 },
1102 [8] = {
1103 .action = SET_ERROR_TYPE_WITH_ADDRESS,
1104 .instruction = WRITE_REGISTER,
1105 .flags = FLAG_PRESERVE,
1106 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->setaddrtable),
1107 .value = 1,
1108 .mask = 0xffffffff
1109 }
1110 };
1111
1112 einj_smi->err_inj_cap = ACPI_EINJ_DEFAULT_CAP;
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001113 einj_smi->trigger_action_table = (u64)(uintptr_t)tat;
Rocky Phaguraeff07132021-01-10 15:42:50 -08001114
1115 for (i = 0; i < ACTION_COUNT; i++)
1116 printk(BIOS_DEBUG, "default_actions[%d].reg.addr is %llx\n", i,
1117 default_actions[i].reg.addr);
1118
Rocky Phagurab46a9e52021-05-21 13:34:03 -07001119 memset((void *)einj, 0, sizeof(*einj));
Rocky Phaguraeff07132021-01-10 15:42:50 -08001120
1121 /* Fill out header fields. */
1122 memcpy(header->signature, "EINJ", 4);
1123 memcpy(header->oem_id, OEM_ID, 6);
1124 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1125 memcpy(header->asl_compiler_id, ASLC, 4);
1126
1127 header->asl_compiler_revision = asl_revision;
1128 header->length = sizeof(acpi_einj_t);
1129 header->revision = 1;
1130 inj_header->einj_header_size = sizeof(acpi_injection_header_t);
1131 inj_header->entry_count = ACTION_COUNT;
1132
1133 printk(BIOS_DEBUG, "%s einj->action_table = %p\n",
1134 __func__, einj->action_table);
1135 memcpy((void *)einj->action_table, (void *)default_actions, sizeof(einj->action_table));
Rocky Phagurab46a9e52021-05-21 13:34:03 -07001136 header->checksum = acpi_checksum((void *)einj, sizeof(*einj));
Rocky Phaguraeff07132021-01-10 15:42:50 -08001137}
1138
Furquan Shaikh0f007d82020-04-24 06:41:18 -07001139void acpi_create_vfct(const struct device *device,
Himanshu Sahdev7f1da072019-10-21 15:27:19 +05301140 acpi_vfct_t *vfct,
Furquan Shaikh0f007d82020-04-24 06:41:18 -07001141 unsigned long (*acpi_fill_vfct)(const struct device *device,
Himanshu Sahdev7f1da072019-10-21 15:27:19 +05301142 acpi_vfct_t *vfct_struct, unsigned long current))
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +02001143{
1144 acpi_header_t *header = &(vfct->header);
Himanshu Sahdev7f1da072019-10-21 15:27:19 +05301145 unsigned long current = (unsigned long)vfct + sizeof(acpi_vfct_t);
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +02001146
Himanshu Sahdev7f1da072019-10-21 15:27:19 +05301147 memset((void *)vfct, 0, sizeof(acpi_vfct_t));
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +02001148
John Zhao2ba303e2019-05-28 16:48:14 -07001149 if (!header)
1150 return;
1151
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +02001152 /* Fill out header fields. */
1153 memcpy(header->signature, "VFCT", 4);
1154 memcpy(header->oem_id, OEM_ID, 6);
1155 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1156 memcpy(header->asl_compiler_id, ASLC, 4);
1157
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001158 header->asl_compiler_revision = asl_revision;
Marc Jones93a51762018-08-22 18:57:24 -06001159 header->revision = get_acpi_table_revision(VFCT);
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +02001160
1161 current = acpi_fill_vfct(device, vfct, current);
1162
Kyösti Mälkkifb493792019-06-30 06:29:52 +03001163 /* If no BIOS image, return with header->length == 0. */
1164 if (!vfct->VBIOSImageOffset)
1165 return;
1166
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +02001167 /* (Re)calculate length and checksum. */
1168 header->length = current - (unsigned long)vfct;
1169 header->checksum = acpi_checksum((void *)vfct, header->length);
1170}
1171
Furquan Shaikh0f007d82020-04-24 06:41:18 -07001172void acpi_create_ipmi(const struct device *device,
Patrick Rudolph9d98e5a2019-06-14 19:00:04 +02001173 struct acpi_spmi *spmi,
1174 const u16 ipmi_revision,
1175 const acpi_addr_t *addr,
1176 const enum acpi_ipmi_interface_type type,
1177 const s8 gpe_interrupt,
1178 const u32 apic_interrupt,
1179 const u32 uid)
1180{
1181 acpi_header_t *header = &(spmi->header);
1182 memset((void *)spmi, 0, sizeof(struct acpi_spmi));
1183
1184 /* Fill out header fields. */
1185 memcpy(header->signature, "SPMI", 4);
1186 memcpy(header->oem_id, OEM_ID, 6);
1187 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1188 memcpy(header->asl_compiler_id, ASLC, 4);
1189
1190 header->asl_compiler_revision = asl_revision;
1191 header->length = sizeof(struct acpi_spmi);
1192 header->revision = get_acpi_table_revision(SPMI);
1193
1194 spmi->reserved = 1;
1195
1196 if (device->path.type == DEVICE_PATH_PCI) {
1197 spmi->pci_device_flag = ACPI_IPMI_PCI_DEVICE_FLAG;
1198 spmi->pci_bus = device->bus->secondary;
1199 spmi->pci_device = device->path.pci.devfn >> 3;
1200 spmi->pci_function = device->path.pci.devfn & 0x7;
1201 } else if (type != IPMI_INTERFACE_SSIF) {
1202 memcpy(spmi->uid, &uid, sizeof(spmi->uid));
1203 }
1204
1205 spmi->base_address = *addr;
1206 spmi->specification_revision = ipmi_revision;
1207
1208 spmi->interface_type = type;
1209
1210 if (gpe_interrupt >= 0 && gpe_interrupt < 32) {
1211 spmi->gpe = gpe_interrupt;
1212 spmi->interrupt_type |= ACPI_IPMI_INT_TYPE_SCI;
1213 }
1214 if (apic_interrupt > 0) {
1215 spmi->global_system_interrupt = apic_interrupt;
1216 spmi->interrupt_type |= ACPI_IPMI_INT_TYPE_APIC;
1217 }
1218
1219 /* Calculate checksum. */
1220 header->checksum = acpi_checksum((void *)spmi, header->length);
1221}
1222
Timothy Pearsonff8ccf02015-08-11 17:48:32 -05001223void acpi_create_ivrs(acpi_ivrs_t *ivrs,
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001224 unsigned long (*acpi_fill_ivrs)(acpi_ivrs_t *ivrs_struct,
1225 unsigned long current))
Timothy Pearsonff8ccf02015-08-11 17:48:32 -05001226{
1227 acpi_header_t *header = &(ivrs->header);
1228 unsigned long current = (unsigned long)ivrs + sizeof(acpi_ivrs_t);
1229
1230 memset((void *)ivrs, 0, sizeof(acpi_ivrs_t));
1231
John Zhao2ba303e2019-05-28 16:48:14 -07001232 if (!header)
1233 return;
1234
Timothy Pearsonff8ccf02015-08-11 17:48:32 -05001235 /* Fill out header fields. */
1236 memcpy(header->signature, "IVRS", 4);
1237 memcpy(header->oem_id, OEM_ID, 6);
1238 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1239 memcpy(header->asl_compiler_id, ASLC, 4);
1240
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001241 header->asl_compiler_revision = asl_revision;
Timothy Pearsonff8ccf02015-08-11 17:48:32 -05001242 header->length = sizeof(acpi_ivrs_t);
Marc Jones93a51762018-08-22 18:57:24 -06001243 header->revision = get_acpi_table_revision(IVRS);
Timothy Pearsonff8ccf02015-08-11 17:48:32 -05001244
1245 current = acpi_fill_ivrs(ivrs, current);
1246
1247 /* (Re)calculate length and checksum. */
1248 header->length = current - (unsigned long)ivrs;
1249 header->checksum = acpi_checksum((void *)ivrs, header->length);
1250}
1251
Jason Glenesk61624b22020-11-02 20:06:23 -08001252void acpi_create_crat(struct acpi_crat_header *crat,
1253 unsigned long (*acpi_fill_crat)(struct acpi_crat_header *crat_struct,
1254 unsigned long current))
1255{
1256 acpi_header_t *header = &(crat->header);
1257 unsigned long current = (unsigned long)crat + sizeof(struct acpi_crat_header);
1258
1259 memset((void *)crat, 0, sizeof(struct acpi_crat_header));
1260
1261 if (!header)
1262 return;
1263
1264 /* Fill out header fields. */
1265 memcpy(header->signature, "CRAT", 4);
1266 memcpy(header->oem_id, OEM_ID, 6);
1267 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1268 memcpy(header->asl_compiler_id, ASLC, 4);
1269
1270 header->asl_compiler_revision = asl_revision;
1271 header->length = sizeof(struct acpi_crat_header);
1272 header->revision = get_acpi_table_revision(CRAT);
1273
1274 current = acpi_fill_crat(crat, current);
1275
1276 /* (Re)calculate length and checksum. */
1277 header->length = current - (unsigned long)crat;
1278 header->checksum = acpi_checksum((void *)crat, header->length);
1279}
1280
Furquan Shaikh0f007d82020-04-24 06:41:18 -07001281unsigned long acpi_write_hpet(const struct device *device, unsigned long current,
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001282 acpi_rsdp_t *rsdp)
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001283{
1284 acpi_hpet_t *hpet;
1285
1286 /*
1287 * We explicitly add these tables later on:
1288 */
1289 printk(BIOS_DEBUG, "ACPI: * HPET\n");
1290
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001291 hpet = (acpi_hpet_t *)current;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001292 current += sizeof(acpi_hpet_t);
Felix Heldc4697122019-06-20 13:50:17 +02001293 current = ALIGN_UP(current, 16);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001294 acpi_create_hpet(hpet);
1295 acpi_add_table(rsdp, hpet);
1296
1297 return current;
1298}
1299
Duncan Lauriee4a36c72017-11-11 19:33:25 -08001300void acpi_create_dbg2(acpi_dbg2_header_t *dbg2,
1301 int port_type, int port_subtype,
1302 acpi_addr_t *address, uint32_t address_size,
1303 const char *device_path)
1304{
1305 uintptr_t current;
1306 acpi_dbg2_device_t *device;
1307 uint32_t *dbg2_addr_size;
1308 acpi_header_t *header;
1309 size_t path_len;
1310 const char *path;
1311 char *namespace;
1312
1313 /* Fill out header fields. */
1314 current = (uintptr_t)dbg2;
1315 memset(dbg2, 0, sizeof(acpi_dbg2_header_t));
1316 header = &(dbg2->header);
John Zhao2ba303e2019-05-28 16:48:14 -07001317
1318 if (!header)
1319 return;
1320
Marc Jones93a51762018-08-22 18:57:24 -06001321 header->revision = get_acpi_table_revision(DBG2);
Duncan Lauriee4a36c72017-11-11 19:33:25 -08001322 memcpy(header->signature, "DBG2", 4);
1323 memcpy(header->oem_id, OEM_ID, 6);
1324 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1325 memcpy(header->asl_compiler_id, ASLC, 4);
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001326 header->asl_compiler_revision = asl_revision;
Duncan Lauriee4a36c72017-11-11 19:33:25 -08001327
1328 /* One debug device defined */
1329 dbg2->devices_offset = sizeof(acpi_dbg2_header_t);
1330 dbg2->devices_count = 1;
1331 current += sizeof(acpi_dbg2_header_t);
1332
1333 /* Device comes after the header */
1334 device = (acpi_dbg2_device_t *)current;
1335 memset(device, 0, sizeof(acpi_dbg2_device_t));
1336 current += sizeof(acpi_dbg2_device_t);
1337
1338 device->revision = 0;
1339 device->address_count = 1;
1340 device->port_type = port_type;
1341 device->port_subtype = port_subtype;
1342
1343 /* Base Address comes after device structure */
1344 memcpy((void *)current, address, sizeof(acpi_addr_t));
1345 device->base_address_offset = current - (uintptr_t)device;
1346 current += sizeof(acpi_addr_t);
1347
1348 /* Address Size comes after address structure */
1349 dbg2_addr_size = (uint32_t *)current;
1350 device->address_size_offset = current - (uintptr_t)device;
1351 *dbg2_addr_size = address_size;
1352 current += sizeof(uint32_t);
1353
1354 /* Namespace string comes last, use '.' if not provided */
1355 path = device_path ? : ".";
1356 /* Namespace string length includes NULL terminator */
1357 path_len = strlen(path) + 1;
1358 namespace = (char *)current;
1359 device->namespace_string_length = path_len;
1360 device->namespace_string_offset = current - (uintptr_t)device;
1361 strncpy(namespace, path, path_len);
1362 current += path_len;
1363
1364 /* Update structure lengths and checksum */
1365 device->length = current - (uintptr_t)device;
1366 header->length = current - (uintptr_t)dbg2;
1367 header->checksum = acpi_checksum((uint8_t *)dbg2, header->length);
1368}
1369
1370unsigned long acpi_write_dbg2_pci_uart(acpi_rsdp_t *rsdp, unsigned long current,
Aamir Bohrae825d3f2019-07-30 10:11:41 +05301371 const struct device *dev, uint8_t access_size)
Duncan Lauriee4a36c72017-11-11 19:33:25 -08001372{
1373 acpi_dbg2_header_t *dbg2 = (acpi_dbg2_header_t *)current;
1374 struct resource *res;
1375 acpi_addr_t address;
1376
1377 if (!dev) {
Wim Vervoorn6cd52432020-02-03 14:41:46 +01001378 printk(BIOS_DEBUG, "%s: Device not found\n", __func__);
Duncan Lauriee4a36c72017-11-11 19:33:25 -08001379 return current;
1380 }
Mario Scheithauerf1eb0ea2017-11-21 13:52:53 +01001381 if (!dev->enabled) {
1382 printk(BIOS_INFO, "%s: Device not enabled\n", __func__);
1383 return current;
1384 }
Angel Pons32d09be2021-11-03 13:27:45 +01001385 res = probe_resource(dev, PCI_BASE_ADDRESS_0);
Duncan Lauriee4a36c72017-11-11 19:33:25 -08001386 if (!res) {
1387 printk(BIOS_ERR, "%s: Unable to find resource for %s\n",
1388 __func__, dev_path(dev));
1389 return current;
1390 }
1391
1392 memset(&address, 0, sizeof(address));
1393 if (res->flags & IORESOURCE_IO)
1394 address.space_id = ACPI_ADDRESS_SPACE_IO;
1395 else if (res->flags & IORESOURCE_MEM)
1396 address.space_id = ACPI_ADDRESS_SPACE_MEMORY;
1397 else {
1398 printk(BIOS_ERR, "%s: Unknown address space type\n", __func__);
1399 return current;
1400 }
1401
1402 address.addrl = (uint32_t)res->base;
1403 address.addrh = (uint32_t)((res->base >> 32) & 0xffffffff);
1404 address.access_size = access_size;
1405
1406 acpi_create_dbg2(dbg2,
1407 ACPI_DBG2_PORT_SERIAL,
1408 ACPI_DBG2_PORT_SERIAL_16550,
1409 &address, res->size,
1410 acpi_device_path(dev));
1411
1412 if (dbg2->header.length) {
1413 current += dbg2->header.length;
1414 current = acpi_align_current(current);
1415 acpi_add_table(rsdp, dbg2);
1416 }
1417
1418 return current;
1419}
1420
Stefan Reinauer777224c2005-01-19 14:06:41 +00001421void acpi_create_facs(acpi_facs_t *facs)
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +00001422{
Uwe Hermann622824c2010-11-19 15:14:42 +00001423 memset((void *)facs, 0, sizeof(acpi_facs_t));
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +00001424
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001425 memcpy(facs->signature, "FACS", 4);
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +00001426 facs->length = sizeof(acpi_facs_t);
1427 facs->hardware_signature = 0;
1428 facs->firmware_waking_vector = 0;
1429 facs->global_lock = 0;
1430 facs->flags = 0;
1431 facs->x_firmware_waking_vector_l = 0;
1432 facs->x_firmware_waking_vector_h = 0;
Marc Jones93a51762018-08-22 18:57:24 -06001433 facs->version = get_acpi_table_revision(FACS);
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +00001434}
Stefan Reinauer777224c2005-01-19 14:06:41 +00001435
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001436static void acpi_write_rsdt(acpi_rsdt_t *rsdt, char *oem_id, char *oem_table_id)
Stefan Reinauer14e22772010-04-27 06:56:47 +00001437{
Uwe Hermann622824c2010-11-19 15:14:42 +00001438 acpi_header_t *header = &(rsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001439
John Zhao2ba303e2019-05-28 16:48:14 -07001440 if (!header)
1441 return;
1442
Uwe Hermann622824c2010-11-19 15:14:42 +00001443 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001444 memcpy(header->signature, "RSDT", 4);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001445 memcpy(header->oem_id, oem_id, 6);
1446 memcpy(header->oem_table_id, oem_table_id, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +00001447 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001448
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001449 header->asl_compiler_revision = asl_revision;
Stefan Reinauer688b3852004-01-28 16:56:14 +00001450 header->length = sizeof(acpi_rsdt_t);
Marc Jones93a51762018-08-22 18:57:24 -06001451 header->revision = get_acpi_table_revision(RSDT);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001452
Uwe Hermann622824c2010-11-19 15:14:42 +00001453 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauer688b3852004-01-28 16:56:14 +00001454
Uwe Hermann622824c2010-11-19 15:14:42 +00001455 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001456 header->checksum = acpi_checksum((void *)rsdt, sizeof(acpi_rsdt_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +00001457}
1458
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001459static void acpi_write_xsdt(acpi_xsdt_t *xsdt, char *oem_id, char *oem_table_id)
Stefan Reinauer14e22772010-04-27 06:56:47 +00001460{
Uwe Hermann622824c2010-11-19 15:14:42 +00001461 acpi_header_t *header = &(xsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001462
John Zhao2ba303e2019-05-28 16:48:14 -07001463 if (!header)
1464 return;
1465
Uwe Hermann622824c2010-11-19 15:14:42 +00001466 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001467 memcpy(header->signature, "XSDT", 4);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001468 memcpy(header->oem_id, oem_id, 6);
1469 memcpy(header->oem_table_id, oem_table_id, 8);
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001470 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001471
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001472 header->asl_compiler_revision = asl_revision;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001473 header->length = sizeof(acpi_xsdt_t);
Marc Jones93a51762018-08-22 18:57:24 -06001474 header->revision = get_acpi_table_revision(XSDT);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001475
Uwe Hermann622824c2010-11-19 15:14:42 +00001476 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001477
Uwe Hermann622824c2010-11-19 15:14:42 +00001478 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001479 header->checksum = acpi_checksum((void *)xsdt, sizeof(acpi_xsdt_t));
1480}
1481
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001482static void acpi_write_rsdp(acpi_rsdp_t *rsdp, acpi_rsdt_t *rsdt,
1483 acpi_xsdt_t *xsdt, char *oem_id)
Stefan Reinauer688b3852004-01-28 16:56:14 +00001484{
Stefan Reinauerd18faac2009-11-05 18:06:43 +00001485 memset(rsdp, 0, sizeof(acpi_rsdp_t));
Uwe Hermann622824c2010-11-19 15:14:42 +00001486
Stefan Reinauer688b3852004-01-28 16:56:14 +00001487 memcpy(rsdp->signature, RSDP_SIG, 8);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001488 memcpy(rsdp->oem_id, oem_id, 6);
Uwe Hermann622824c2010-11-19 15:14:42 +00001489
1490 rsdp->length = sizeof(acpi_rsdp_t);
Stefan Reinauerdefee172015-06-18 01:11:19 -07001491 rsdp->rsdt_address = (uintptr_t)rsdt;
Uwe Hermann622824c2010-11-19 15:14:42 +00001492
1493 /*
1494 * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2.
1495 *
1496 * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
1497 * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
1498 * revision 0).
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001499 */
1500 if (xsdt == NULL) {
Uwe Hermann622824c2010-11-19 15:14:42 +00001501 rsdp->revision = 0;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001502 } else {
Stefan Reinauerdefee172015-06-18 01:11:19 -07001503 rsdp->xsdt_address = (u64)(uintptr_t)xsdt;
Marc Jones93a51762018-08-22 18:57:24 -06001504 rsdp->revision = get_acpi_table_revision(RSDP);
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001505 }
Uwe Hermann622824c2010-11-19 15:14:42 +00001506
1507 /* Calculate checksums. */
1508 rsdp->checksum = acpi_checksum((void *)rsdp, 20);
1509 rsdp->ext_checksum = acpi_checksum((void *)rsdp, sizeof(acpi_rsdp_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +00001510}
1511
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001512unsigned long acpi_create_hest_error_source(acpi_hest_t *hest,
1513 acpi_hest_esd_t *esd, u16 type, void *data, u16 data_len)
zbaocaf494c82012-04-13 13:57:14 +08001514{
1515 acpi_header_t *header = &(hest->header);
1516 acpi_hest_hen_t *hen;
1517 void *pos;
1518 u16 len;
1519
1520 pos = esd;
1521 memset(pos, 0, sizeof(acpi_hest_esd_t));
1522 len = 0;
1523 esd->type = type; /* MCE */
1524 esd->source_id = hest->error_source_count;
1525 esd->flags = 0; /* FIRMWARE_FIRST */
1526 esd->enabled = 1;
1527 esd->prealloc_erecords = 1;
1528 esd->max_section_per_record = 0x1;
1529
1530 len += sizeof(acpi_hest_esd_t);
1531 pos = esd + 1;
1532
1533 switch (type) {
1534 case 0: /* MCE */
1535 break;
1536 case 1: /* CMC */
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001537 hen = (acpi_hest_hen_t *)(pos);
zbaocaf494c82012-04-13 13:57:14 +08001538 memset(pos, 0, sizeof(acpi_hest_hen_t));
1539 hen->type = 3; /* SCI? */
1540 hen->length = sizeof(acpi_hest_hen_t);
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001541 hen->conf_we = 0; /* Configuration Write Enable. */
zbaocaf494c82012-04-13 13:57:14 +08001542 hen->poll_interval = 0;
1543 hen->vector = 0;
1544 hen->sw2poll_threshold_val = 0;
1545 hen->sw2poll_threshold_win = 0;
1546 hen->error_threshold_val = 0;
1547 hen->error_threshold_win = 0;
1548 len += sizeof(acpi_hest_hen_t);
1549 pos = hen + 1;
1550 break;
1551 case 2: /* NMI */
1552 case 6: /* AER Root Port */
1553 case 7: /* AER Endpoint */
1554 case 8: /* AER Bridge */
1555 case 9: /* Generic Hardware Error Source. */
1556 /* TODO: */
1557 break;
1558 default:
1559 printk(BIOS_DEBUG, "Invalid type of Error Source.");
1560 break;
1561 }
Lee Leahy024b13d2017-03-16 13:41:11 -07001562 hest->error_source_count++;
zbaocaf494c82012-04-13 13:57:14 +08001563
1564 memcpy(pos, data, data_len);
1565 len += data_len;
John Zhao2ba303e2019-05-28 16:48:14 -07001566 if (header)
1567 header->length += len;
zbaocaf494c82012-04-13 13:57:14 +08001568
1569 return len;
1570}
1571
1572/* ACPI 4.0 */
Vladimir Serbinenko807127f2014-11-09 13:36:18 +01001573void acpi_write_hest(acpi_hest_t *hest,
1574 unsigned long (*acpi_fill_hest)(acpi_hest_t *hest))
zbaocaf494c82012-04-13 13:57:14 +08001575{
1576 acpi_header_t *header = &(hest->header);
1577
1578 memset(hest, 0, sizeof(acpi_hest_t));
1579
John Zhao2ba303e2019-05-28 16:48:14 -07001580 if (!header)
1581 return;
1582
zbaocaf494c82012-04-13 13:57:14 +08001583 memcpy(header->signature, "HEST", 4);
1584 memcpy(header->oem_id, OEM_ID, 6);
1585 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1586 memcpy(header->asl_compiler_id, ASLC, 4);
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001587 header->asl_compiler_revision = asl_revision;
zbaocaf494c82012-04-13 13:57:14 +08001588 header->length += sizeof(acpi_hest_t);
Marc Jones93a51762018-08-22 18:57:24 -06001589 header->revision = get_acpi_table_revision(HEST);
zbaocaf494c82012-04-13 13:57:14 +08001590
1591 acpi_fill_hest(hest);
1592
1593 /* Calculate checksums. */
1594 header->checksum = acpi_checksum((void *)hest, header->length);
1595}
1596
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001597/* ACPI 3.0b */
1598void acpi_write_bert(acpi_bert_t *bert, uintptr_t region, size_t length)
1599{
1600 acpi_header_t *header = &(bert->header);
1601
1602 memset(bert, 0, sizeof(acpi_bert_t));
1603
John Zhao2ba303e2019-05-28 16:48:14 -07001604 if (!header)
1605 return;
1606
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001607 memcpy(header->signature, "BERT", 4);
1608 memcpy(header->oem_id, OEM_ID, 6);
1609 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1610 memcpy(header->asl_compiler_id, ASLC, 4);
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001611 header->asl_compiler_revision = asl_revision;
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001612 header->length += sizeof(acpi_bert_t);
1613 header->revision = get_acpi_table_revision(BERT);
1614
1615 bert->error_region = region;
1616 bert->region_length = length;
1617
1618 /* Calculate checksums. */
1619 header->checksum = acpi_checksum((void *)bert, header->length);
1620}
1621
Angel Pons79572e42020-07-13 00:17:43 +02001622__weak void arch_fill_fadt(acpi_fadt_t *fadt) { }
Kyösti Mälkkif9aac922020-05-30 16:16:28 +03001623__weak void soc_fill_fadt(acpi_fadt_t *fadt) { }
Kyösti Mälkki02fd15d2020-06-02 03:34:43 +03001624__weak void mainboard_fill_fadt(acpi_fadt_t *fadt) { }
Kyösti Mälkkif9aac922020-05-30 16:16:28 +03001625
Lee Leahy024b13d2017-03-16 13:41:11 -07001626void acpi_create_fadt(acpi_fadt_t *fadt, acpi_facs_t *facs, void *dsdt)
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001627{
1628 acpi_header_t *header = &(fadt->header);
1629
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001630 memset((void *)fadt, 0, sizeof(acpi_fadt_t));
John Zhao2ba303e2019-05-28 16:48:14 -07001631
1632 if (!header)
1633 return;
1634
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001635 memcpy(header->signature, "FACP", 4);
1636 header->length = sizeof(acpi_fadt_t);
Marc Jones93a51762018-08-22 18:57:24 -06001637 header->revision = get_acpi_table_revision(FADT);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001638 memcpy(header->oem_id, OEM_ID, 6);
1639 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1640 memcpy(header->asl_compiler_id, ASLC, 4);
Elyes HAOUAS26071aa2019-02-15 08:21:33 +01001641 header->asl_compiler_revision = asl_revision;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001642
Elyes Haouas532e0432022-02-21 18:13:58 +01001643 fadt->FADT_MinorVersion = get_acpi_fadt_minor_version();
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001644 fadt->firmware_ctrl = (unsigned long)facs;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001645 fadt->x_firmware_ctl_l = (unsigned long)facs;
1646 fadt->x_firmware_ctl_h = 0;
Kyösti Mälkkid4acee82020-06-03 07:20:55 +03001647
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001648 fadt->dsdt = (unsigned long)dsdt;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001649 fadt->x_dsdt_l = (unsigned long)dsdt;
1650 fadt->x_dsdt_h = 0;
1651
Kyösti Mälkkid4acee82020-06-03 07:20:55 +03001652 /* should be 0 ACPI 3.0 */
1653 fadt->reserved = 0;
1654
Kyösti Mälkkie0d38682020-06-07 12:01:58 +03001655 fadt->preferred_pm_profile = acpi_get_preferred_pm_profile();
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001656
Nico Huberd5811372021-07-12 18:11:58 +02001657 if (CONFIG(USE_PC_CMOS_ALTCENTURY))
1658 fadt->century = RTC_CLK_ALTCENTURY;
1659
Angel Pons79572e42020-07-13 00:17:43 +02001660 arch_fill_fadt(fadt);
1661
Vladimir Serbinenkoab83ef02014-10-25 15:18:25 +02001662 acpi_fill_fadt(fadt);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001663
Kyösti Mälkkif9aac922020-05-30 16:16:28 +03001664 soc_fill_fadt(fadt);
Kyösti Mälkki02fd15d2020-06-02 03:34:43 +03001665 mainboard_fill_fadt(fadt);
Kyösti Mälkkif9aac922020-05-30 16:16:28 +03001666
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001667 header->checksum =
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001668 acpi_checksum((void *)fadt, header->length);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001669}
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001670
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001671void acpi_create_lpit(acpi_lpit_t *lpit)
1672{
1673 acpi_header_t *header = &(lpit->header);
1674 unsigned long current = (unsigned long)lpit + sizeof(acpi_lpit_t);
1675
1676 memset((void *)lpit, 0, sizeof(acpi_lpit_t));
1677
1678 if (!header)
1679 return;
1680
1681 /* Fill out header fields. */
1682 memcpy(header->signature, "LPIT", 4);
1683 memcpy(header->oem_id, OEM_ID, 6);
1684 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1685 memcpy(header->asl_compiler_id, ASLC, 4);
1686
1687 header->asl_compiler_revision = asl_revision;
1688 header->revision = get_acpi_table_revision(LPIT);
1689 header->oem_revision = 42;
1690 header->length = sizeof(acpi_lpit_t);
1691
1692 current = acpi_fill_lpit(current);
1693
1694 /* (Re)calculate length and checksum. */
1695 header->length = current - (unsigned long)lpit;
1696 header->checksum = acpi_checksum((void *)lpit, header->length);
1697}
1698
1699unsigned long acpi_create_lpi_desc_ncst(acpi_lpi_desc_ncst_t *lpi_desc, uint16_t uid)
1700{
1701 memset(lpi_desc, 0, sizeof(acpi_lpi_desc_ncst_t));
1702 lpi_desc->header.length = sizeof(acpi_lpi_desc_ncst_t);
1703 lpi_desc->header.type = ACPI_LPI_DESC_TYPE_NATIVE_CSTATE;
1704 lpi_desc->header.uid = uid;
1705
1706 return lpi_desc->header.length;
1707}
1708
Aaron Durbin64031672018-04-21 14:45:32 -06001709unsigned long __weak fw_cfg_acpi_tables(unsigned long start)
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001710{
1711 return 0;
1712}
1713
Raul E Rangel6b446b92021-11-19 11:38:35 -07001714void preload_acpi_dsdt(void)
1715{
1716 const char *file = CONFIG_CBFS_PREFIX "/dsdt.aml";
1717
1718 if (!CONFIG(CBFS_PRELOAD))
1719 return;
1720
1721 printk(BIOS_DEBUG, "Preloading %s\n", file);
1722 cbfs_preload(file);
1723}
1724
Arthur Heymans2e7e2d92022-03-03 22:28:27 +01001725static uintptr_t coreboot_rsdp;
1726
1727uintptr_t get_coreboot_rsdp(void)
1728{
1729 return coreboot_rsdp;
1730}
1731
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001732unsigned long write_acpi_tables(unsigned long start)
1733{
1734 unsigned long current;
1735 acpi_rsdp_t *rsdp;
1736 acpi_rsdt_t *rsdt;
1737 acpi_xsdt_t *xsdt;
1738 acpi_fadt_t *fadt;
1739 acpi_facs_t *facs;
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001740 acpi_header_t *slic_file, *slic;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001741 acpi_header_t *ssdt;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001742 acpi_header_t *dsdt_file, *dsdt;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001743 acpi_mcfg_t *mcfg;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +02001744 acpi_tcpa_t *tcpa;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001745 acpi_tpm2_t *tpm2;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001746 acpi_madt_t *madt;
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001747 acpi_lpit_t *lpit;
Francois Toguo522e0db2021-01-21 09:55:19 -08001748 acpi_bert_t *bert;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +11001749 struct device *dev;
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001750 unsigned long fw;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001751 size_t slic_size, dsdt_size;
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001752 char oem_id[6], oem_table_id[8];
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001753
1754 current = start;
1755
1756 /* Align ACPI tables to 16byte */
Aaron Durbin07a1b282015-12-10 17:07:38 -06001757 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001758
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001759 /* Special case for qemu */
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001760 fw = fw_cfg_acpi_tables(current);
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001761 if (fw) {
1762 rsdp = NULL;
1763 /* Find RSDP. */
1764 for (void *p = (void *)current; p < (void *)fw; p += 16) {
1765 if (valid_rsdp((acpi_rsdp_t *)p)) {
1766 rsdp = p;
1767 break;
1768 }
1769 }
1770 if (!rsdp)
1771 return fw;
1772
1773 /* Add BOOT0000 for Linux google firmware driver */
1774 printk(BIOS_DEBUG, "ACPI: * SSDT\n");
1775 ssdt = (acpi_header_t *)fw;
1776 current = (unsigned long)ssdt + sizeof(acpi_header_t);
1777
1778 memset((void *)ssdt, 0, sizeof(acpi_header_t));
1779
1780 memcpy(&ssdt->signature, "SSDT", 4);
1781 ssdt->revision = get_acpi_table_revision(SSDT);
1782 memcpy(&ssdt->oem_id, OEM_ID, 6);
1783 memcpy(&ssdt->oem_table_id, oem_table_id, 8);
1784 ssdt->oem_revision = 42;
1785 memcpy(&ssdt->asl_compiler_id, ASLC, 4);
1786 ssdt->asl_compiler_revision = asl_revision;
1787 ssdt->length = sizeof(acpi_header_t);
1788
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001789 acpigen_set_current((char *)current);
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001790
1791 /* Write object to declare coreboot tables */
1792 acpi_ssdt_write_cbtable();
1793
1794 /* (Re)calculate length and checksum. */
1795 ssdt->length = current - (unsigned long)ssdt;
1796 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
1797
1798 acpi_create_ssdt_generator(ssdt, ACPI_TABLE_CREATOR);
1799
1800 acpi_add_table(rsdp, ssdt);
1801
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001802 return fw;
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001803 }
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001804
Julius Werner834b3ec2020-03-04 16:52:08 -08001805 dsdt_file = cbfs_map(CONFIG_CBFS_PREFIX "/dsdt.aml", &dsdt_size);
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001806 if (!dsdt_file) {
1807 printk(BIOS_ERR, "No DSDT file, skipping ACPI tables\n");
1808 return current;
1809 }
1810
1811 if (dsdt_file->length > dsdt_size
Elyes HAOUAS7d87e762016-10-03 22:11:07 +02001812 || dsdt_file->length < sizeof(acpi_header_t)
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001813 || memcmp(dsdt_file->signature, "DSDT", 4) != 0) {
1814 printk(BIOS_ERR, "Invalid DSDT file, skipping ACPI tables\n");
1815 return current;
1816 }
1817
Julius Werner834b3ec2020-03-04 16:52:08 -08001818 slic_file = cbfs_map(CONFIG_CBFS_PREFIX "/slic", &slic_size);
Vladimir Serbinenko2cb29782015-05-31 11:32:09 +02001819 if (slic_file
1820 && (slic_file->length > slic_size
Elyes HAOUAS7d87e762016-10-03 22:11:07 +02001821 || slic_file->length < sizeof(acpi_header_t)
Benjamin Doron2b2dc0c2020-09-12 02:55:57 +00001822 || (memcmp(slic_file->signature, "SLIC", 4) != 0
1823 && memcmp(slic_file->signature, "MSDM", 4) != 0))) {
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001824 slic_file = 0;
1825 }
1826
1827 if (slic_file) {
1828 memcpy(oem_id, slic_file->oem_id, 6);
1829 memcpy(oem_table_id, slic_file->oem_table_id, 8);
1830 } else {
1831 memcpy(oem_id, OEM_ID, 6);
1832 memcpy(oem_table_id, ACPI_TABLE_CREATOR, 8);
1833 }
1834
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001835 printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx.\n", start);
1836
1837 /* We need at least an RSDP and an RSDT Table */
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001838 rsdp = (acpi_rsdp_t *)current;
Arthur Heymans2e7e2d92022-03-03 22:28:27 +01001839 coreboot_rsdp = (uintptr_t)rsdp;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001840 current += sizeof(acpi_rsdp_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001841 current = acpi_align_current(current);
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001842 rsdt = (acpi_rsdt_t *)current;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001843 current += sizeof(acpi_rsdt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001844 current = acpi_align_current(current);
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001845 xsdt = (acpi_xsdt_t *)current;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001846 current += sizeof(acpi_xsdt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001847 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001848
1849 /* clear all table memory */
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001850 memset((void *)start, 0, current - start);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001851
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001852 acpi_write_rsdp(rsdp, rsdt, xsdt, oem_id);
1853 acpi_write_rsdt(rsdt, oem_id, oem_table_id);
1854 acpi_write_xsdt(xsdt, oem_id, oem_table_id);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001855
1856 printk(BIOS_DEBUG, "ACPI: * FACS\n");
Felix Heldc4697122019-06-20 13:50:17 +02001857 current = ALIGN_UP(current, 64);
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001858 facs = (acpi_facs_t *)current;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001859 current += sizeof(acpi_facs_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001860 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001861 acpi_create_facs(facs);
1862
1863 printk(BIOS_DEBUG, "ACPI: * DSDT\n");
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001864 dsdt = (acpi_header_t *)current;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001865 memcpy(dsdt, dsdt_file, sizeof(acpi_header_t));
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001866 if (dsdt->length >= sizeof(acpi_header_t)) {
1867 current += sizeof(acpi_header_t);
1868
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001869 acpigen_set_current((char *)current);
Kyösti Mälkki2ab4a962020-06-30 11:41:47 +03001870
Kyösti Mälkki37eb24b2021-01-11 18:40:37 +02001871 if (CONFIG(ACPI_SOC_NVS))
1872 acpi_fill_gnvs();
Kyösti Mälkki3dc17922021-03-16 19:01:48 +02001873 if (CONFIG(CHROMEOS_NVS))
1874 acpi_fill_cnvs();
Kyösti Mälkki2ab4a962020-06-30 11:41:47 +03001875
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001876 for (dev = all_devices; dev; dev = dev->next)
Nico Huber68680dd2020-03-31 17:34:52 +02001877 if (dev->ops && dev->ops->acpi_inject_dsdt)
1878 dev->ops->acpi_inject_dsdt(dev);
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001879 current = (unsigned long)acpigen_get_current();
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001880 memcpy((char *)current,
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001881 (char *)dsdt_file + sizeof(acpi_header_t),
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001882 dsdt->length - sizeof(acpi_header_t));
1883 current += dsdt->length - sizeof(acpi_header_t);
1884
1885 /* (Re)calculate length and checksum. */
1886 dsdt->length = current - (unsigned long)dsdt;
1887 dsdt->checksum = 0;
1888 dsdt->checksum = acpi_checksum((void *)dsdt, dsdt->length);
1889 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001890
Aaron Durbin07a1b282015-12-10 17:07:38 -06001891 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001892
1893 printk(BIOS_DEBUG, "ACPI: * FADT\n");
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001894 fadt = (acpi_fadt_t *)current;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001895 current += sizeof(acpi_fadt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001896 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001897
1898 acpi_create_fadt(fadt, facs, dsdt);
1899 acpi_add_table(rsdp, fadt);
1900
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001901 if (slic_file) {
1902 printk(BIOS_DEBUG, "ACPI: * SLIC\n");
1903 slic = (acpi_header_t *)current;
1904 memcpy(slic, slic_file, slic_file->length);
1905 current += slic_file->length;
Aaron Durbin07a1b282015-12-10 17:07:38 -06001906 current = acpi_align_current(current);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001907 acpi_add_table(rsdp, slic);
1908 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001909
1910 printk(BIOS_DEBUG, "ACPI: * SSDT\n");
1911 ssdt = (acpi_header_t *)current;
1912 acpi_create_ssdt_generator(ssdt, ACPI_TABLE_CREATOR);
Vladimir Serbinenko9310df82014-10-10 20:40:41 +02001913 if (ssdt->length > sizeof(acpi_header_t)) {
1914 current += ssdt->length;
1915 acpi_add_table(rsdp, ssdt);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001916 current = acpi_align_current(current);
Vladimir Serbinenko9310df82014-10-10 20:40:41 +02001917 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001918
1919 printk(BIOS_DEBUG, "ACPI: * MCFG\n");
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001920 mcfg = (acpi_mcfg_t *)current;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001921 acpi_create_mcfg(mcfg);
1922 if (mcfg->header.length > sizeof(acpi_mcfg_t)) {
1923 current += mcfg->header.length;
Aaron Durbin07a1b282015-12-10 17:07:38 -06001924 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001925 acpi_add_table(rsdp, mcfg);
1926 }
1927
Julius Wernercd49cce2019-03-05 16:53:33 -08001928 if (CONFIG(TPM1)) {
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001929 printk(BIOS_DEBUG, "ACPI: * TCPA\n");
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001930 tcpa = (acpi_tcpa_t *)current;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001931 acpi_create_tcpa(tcpa);
1932 if (tcpa->header.length >= sizeof(acpi_tcpa_t)) {
1933 current += tcpa->header.length;
1934 current = acpi_align_current(current);
1935 acpi_add_table(rsdp, tcpa);
1936 }
1937 }
1938
Julius Wernercd49cce2019-03-05 16:53:33 -08001939 if (CONFIG(TPM2)) {
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001940 printk(BIOS_DEBUG, "ACPI: * TPM2\n");
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001941 tpm2 = (acpi_tpm2_t *)current;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001942 acpi_create_tpm2(tpm2);
1943 if (tpm2->header.length >= sizeof(acpi_tpm2_t)) {
1944 current += tpm2->header.length;
1945 current = acpi_align_current(current);
1946 acpi_add_table(rsdp, tpm2);
1947 }
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +02001948 }
1949
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001950 if (CONFIG(ACPI_LPIT)) {
1951 printk(BIOS_DEBUG, "ACPI: * LPIT\n");
1952
1953 lpit = (acpi_lpit_t *)current;
1954 acpi_create_lpit(lpit);
1955 if (lpit->header.length >= sizeof(acpi_lpit_t)) {
1956 current += lpit->header.length;
1957 current = acpi_align_current(current);
1958 acpi_add_table(rsdp, lpit);
1959 }
1960 }
1961
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001962 printk(BIOS_DEBUG, "ACPI: * MADT\n");
1963
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001964 madt = (acpi_madt_t *)current;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001965 acpi_create_madt(madt);
1966 if (madt->header.length > sizeof(acpi_madt_t)) {
Lee Leahy024b13d2017-03-16 13:41:11 -07001967 current += madt->header.length;
1968 acpi_add_table(rsdp, madt);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001969 }
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001970
Aaron Durbin07a1b282015-12-10 17:07:38 -06001971 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001972
Felix Heldfba479262021-05-28 16:11:43 +02001973 if (CONFIG(ACPI_BERT)) {
Francois Toguo522e0db2021-01-21 09:55:19 -08001974 void *region;
1975 size_t size;
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001976 bert = (acpi_bert_t *)current;
Felix Heldfba479262021-05-28 16:11:43 +02001977 if (acpi_soc_get_bert_region(&region, &size) == CB_SUCCESS) {
1978 printk(BIOS_DEBUG, "ACPI: * BERT\n");
1979 acpi_write_bert(bert, (uintptr_t)region, size);
1980 if (bert->header.length >= sizeof(acpi_bert_t)) {
1981 current += bert->header.length;
1982 acpi_add_table(rsdp, bert);
1983 }
1984 current = acpi_align_current(current);
Francois Toguo522e0db2021-01-21 09:55:19 -08001985 }
Francois Toguo522e0db2021-01-21 09:55:19 -08001986 }
1987
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001988 printk(BIOS_DEBUG, "current = %lx\n", current);
1989
1990 for (dev = all_devices; dev; dev = dev->next) {
1991 if (dev->ops && dev->ops->write_acpi_tables) {
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001992 current = dev->ops->write_acpi_tables(dev, current,
1993 rsdp);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001994 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001995 }
1996 }
1997
1998 printk(BIOS_INFO, "ACPI: done.\n");
1999 return current;
2000}
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02002001
Rudolf Marek33cafe52009-04-13 18:07:02 +00002002static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp)
2003{
2004 if (strncmp((char *)rsdp, RSDP_SIG, sizeof(RSDP_SIG) - 1) != 0)
2005 return NULL;
2006
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00002007 printk(BIOS_DEBUG, "Looking on %p for valid checksum\n", rsdp);
Rudolf Marek33cafe52009-04-13 18:07:02 +00002008
2009 if (acpi_checksum((void *)rsdp, 20) != 0)
2010 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00002011 printk(BIOS_DEBUG, "Checksum 1 passed\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00002012
2013 if ((rsdp->revision > 1) && (acpi_checksum((void *)rsdp,
2014 rsdp->length) != 0))
2015 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00002016 printk(BIOS_DEBUG, "Checksum 2 passed all OK\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00002017
2018 return rsdp;
2019}
2020
Rudolf Marek33cafe52009-04-13 18:07:02 +00002021void *acpi_find_wakeup_vector(void)
2022{
2023 char *p, *end;
Rudolf Marek33cafe52009-04-13 18:07:02 +00002024 acpi_rsdt_t *rsdt;
2025 acpi_facs_t *facs;
Martin Rothc5f49262012-12-14 19:17:55 -07002026 acpi_fadt_t *fadt = NULL;
Kyösti Mälkki072d436b2016-06-17 08:44:40 +03002027 acpi_rsdp_t *rsdp = NULL;
Uwe Hermann622824c2010-11-19 15:14:42 +00002028 void *wake_vec;
Rudolf Marek33cafe52009-04-13 18:07:02 +00002029 int i;
2030
Kyösti Mälkkib8c7ea02020-11-17 16:41:38 +02002031 if (!acpi_is_wakeup_s3())
Rudolf Marek33cafe52009-04-13 18:07:02 +00002032 return NULL;
2033
Uwe Hermann622824c2010-11-19 15:14:42 +00002034 printk(BIOS_DEBUG, "Trying to find the wakeup vector...\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00002035
Uwe Hermann622824c2010-11-19 15:14:42 +00002036 /* Find RSDP. */
2037 for (p = (char *)0xe0000; p < (char *)0xfffff; p += 16) {
Lee Leahy0b5678f2017-03-16 16:01:40 -07002038 rsdp = valid_rsdp((acpi_rsdp_t *)p);
2039 if (rsdp)
Rudolf Marek33cafe52009-04-13 18:07:02 +00002040 break;
2041 }
2042
Angel Pons98a68ad2018-11-04 12:25:25 +01002043 if (rsdp == NULL) {
2044 printk(BIOS_ALERT,
2045 "No RSDP found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00002046 return NULL;
Angel Pons98a68ad2018-11-04 12:25:25 +01002047 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00002048
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00002049 printk(BIOS_DEBUG, "RSDP found at %p\n", rsdp);
Stefan Reinauer71a30182015-07-30 16:28:13 -07002050 rsdt = (acpi_rsdt_t *)(uintptr_t)rsdp->rsdt_address;
Stefan Reinauer14e22772010-04-27 06:56:47 +00002051
Uwe Hermann622824c2010-11-19 15:14:42 +00002052 end = (char *)rsdt + rsdt->header.length;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00002053 printk(BIOS_DEBUG, "RSDT found at %p ends at %p\n", rsdt, end);
Rudolf Marek33cafe52009-04-13 18:07:02 +00002054
Uwe Hermann622824c2010-11-19 15:14:42 +00002055 for (i = 0; ((char *)&rsdt->entry[i]) < end; i++) {
Stefan Reinauer71a30182015-07-30 16:28:13 -07002056 fadt = (acpi_fadt_t *)(uintptr_t)rsdt->entry[i];
Stefan Reinauercdfe3762009-07-21 22:15:43 +00002057 if (strncmp((char *)fadt, "FACP", 4) == 0)
Rudolf Marek33cafe52009-04-13 18:07:02 +00002058 break;
2059 fadt = NULL;
2060 }
2061
Angel Pons98a68ad2018-11-04 12:25:25 +01002062 if (fadt == NULL) {
2063 printk(BIOS_ALERT,
2064 "No FADT found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00002065 return NULL;
Angel Pons98a68ad2018-11-04 12:25:25 +01002066 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00002067
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00002068 printk(BIOS_DEBUG, "FADT found at %p\n", fadt);
Stefan Reinauer71a30182015-07-30 16:28:13 -07002069 facs = (acpi_facs_t *)(uintptr_t)fadt->firmware_ctrl;
Rudolf Marek33cafe52009-04-13 18:07:02 +00002070
Stefan Reinauer7e9771c2009-04-22 08:17:38 +00002071 if (facs == NULL) {
Angel Pons98a68ad2018-11-04 12:25:25 +01002072 printk(BIOS_ALERT,
2073 "No FACS found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00002074 return NULL;
Stefan Reinauer7e9771c2009-04-22 08:17:38 +00002075 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00002076
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00002077 printk(BIOS_DEBUG, "FACS found at %p\n", facs);
Stefan Reinauer71a30182015-07-30 16:28:13 -07002078 wake_vec = (void *)(uintptr_t)facs->firmware_waking_vector;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00002079 printk(BIOS_DEBUG, "OS waking vector is %p\n", wake_vec);
Uwe Hermann622824c2010-11-19 15:14:42 +00002080
Rudolf Marek33cafe52009-04-13 18:07:02 +00002081 return wake_vec;
2082}
2083
Aaron Durbin64031672018-04-21 14:45:32 -06002084__weak int acpi_get_gpe(int gpe)
Duncan Laurie1f6e6812016-09-19 12:04:19 -07002085{
2086 return -1; /* implemented by SOC */
2087}
Marc Jones93a51762018-08-22 18:57:24 -06002088
Elyes Haouas8b950f42022-02-16 12:08:16 +01002089u8 get_acpi_fadt_minor_version(void)
2090{
2091 return ACPI_FADT_MINOR_VERSION_0;
2092}
2093
Marc Jones93a51762018-08-22 18:57:24 -06002094int get_acpi_table_revision(enum acpi_tables table)
2095{
2096 switch (table) {
2097 case FADT:
Elyes Haouas8b950f42022-02-16 12:08:16 +01002098 return ACPI_FADT_REV_ACPI_6;
Elyes HAOUASf288b392018-11-11 15:22:30 +01002099 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 +01002100 return 3;
Marc Jones93a51762018-08-22 18:57:24 -06002101 case MCFG:
2102 return 1;
2103 case TCPA:
2104 return 2;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02002105 case TPM2:
2106 return 4;
Martin Roth0949e732021-10-01 14:28:22 -06002107 case SSDT: /* ACPI 3.0 up to 6.3: 2 */
Marc Jones93a51762018-08-22 18:57:24 -06002108 return 2;
Jonathan Zhanga3311b92022-12-01 17:13:41 -08002109 case SRAT: /* ACPI 2.0: 1, ACPI 3.0: 2, ACPI 4.0 up to 6.4: 3 */
2110 return 3;
Jonathan Zhang2a4e1f42021-04-01 11:43:37 -07002111 case HMAT: /* ACPI 6.4: 2 */
2112 return 2;
Marc Jones93a51762018-08-22 18:57:24 -06002113 case DMAR:
2114 return 1;
Martin Roth0949e732021-10-01 14:28:22 -06002115 case SLIT: /* ACPI 2.0 up to 6.3: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06002116 return 1;
Patrick Rudolph9d98e5a2019-06-14 19:00:04 +02002117 case SPMI: /* IMPI 2.0 */
2118 return 5;
Marc Jones93a51762018-08-22 18:57:24 -06002119 case HPET: /* Currently 1. Table added in ACPI 2.0. */
2120 return 1;
Elyes HAOUASf288b392018-11-11 15:22:30 +01002121 case VFCT: /* ACPI 2.0/3.0/4.0: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06002122 return 1;
2123 case IVRS:
Jason Glenesk1916f892020-07-24 02:51:30 -07002124 return IVRS_FORMAT_MIXED;
Marc Jones93a51762018-08-22 18:57:24 -06002125 case DBG2:
2126 return 0;
Martin Roth0949e732021-10-01 14:28:22 -06002127 case FACS: /* ACPI 2.0/3.0: 1, ACPI 4.0 up to 6.3: 2 */
Marc Jones93a51762018-08-22 18:57:24 -06002128 return 1;
Martin Roth0949e732021-10-01 14:28:22 -06002129 case RSDT: /* ACPI 1.0 up to 6.3: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06002130 return 1;
Martin Roth0949e732021-10-01 14:28:22 -06002131 case XSDT: /* ACPI 2.0 up to 6.3: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06002132 return 1;
Martin Roth0949e732021-10-01 14:28:22 -06002133 case RSDP: /* ACPI 2.0 up to 6.3: 2 */
Marc Jones93a51762018-08-22 18:57:24 -06002134 return 2;
Rocky Phaguraeff07132021-01-10 15:42:50 -08002135 case EINJ:
2136 return 1;
Marc Jones93a51762018-08-22 18:57:24 -06002137 case HEST:
2138 return 1;
2139 case NHLT:
2140 return 5;
Marshall Dawson44705c62018-09-04 13:47:15 -06002141 case BERT:
2142 return 1;
Jonathan Zhanga3311b92022-12-01 17:13:41 -08002143 case CEDT: /* CXL 3.0 section 9.17.1 */
2144 return 1;
Jason Glenesk61624b22020-11-02 20:06:23 -08002145 case CRAT:
2146 return 1;
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01002147 case LPIT: /* ACPI 5.1 up to 6.3: 0 */
2148 return 0;
Marc Jones93a51762018-08-22 18:57:24 -06002149 default:
2150 return -1;
2151 }
2152 return -1;
2153}