blob: 492d043bb895150e1f52c5b9ddd243f5b24f6319 [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
Kyösti Mälkki2e9f0d32023-04-07 23:05:46 +0300125static int 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
Kyösti Mälkki2e9f0d32023-04-07 23:05:46 +0300136static int acpi_create_madt_lx2apic(acpi_madt_lx2apic_t *lapic, u32 cpu, u32 apic)
Patrick Rudolph56a3ef22020-03-24 17:29:49 +0100137{
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
Kyösti Mälkki2e9f0d32023-04-07 23:05:46 +0300148unsigned long acpi_create_madt_one_lapic(unsigned long current, u32 index, u32 lapic_id)
149{
150 if (lapic_id <= ACPI_MADT_MAX_LAPIC_ID)
151 current += acpi_create_madt_lapic((acpi_madt_lapic_t *)current, index,
152 lapic_id);
153 else
154 current += acpi_create_madt_lx2apic((acpi_madt_lx2apic_t *)current, index,
155 lapic_id);
156
157 return current;
158}
159
Arthur Heymans85474292022-11-04 14:00:35 +0100160/* Increase if necessary. Currently all x86 CPUs only have 2 SMP threads */
161#define MAX_THREAD_ID 1
162/*
163 * From ACPI 6.4 spec:
164 * "The advent of multi-threaded processors yielded multiple logical processors
165 * executing on common processor hardware. ACPI defines logical processors in
166 * an identical manner as physical processors. To ensure that non
167 * multi-threading aware OSPM implementations realize optimal performance on
168 * platforms containing multi-threaded processors, two guidelines should be
169 * followed. The first is the same as above, that is, OSPM should initialize
170 * processors in the order that they appear in the MADT. The second is that
171 * platform firmware should list the first logical processor of each of the
172 * individual multi-threaded processors in the MADT before listing any of the
173 * second logical processors. This approach should be used for all successive
174 * logical processors."
175 */
Kyösti Mälkki899c7132021-06-16 11:06:26 +0300176static unsigned long acpi_create_madt_lapics(unsigned long current)
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000177{
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100178 struct device *cpu;
Arthur Heymans85474292022-11-04 14:00:35 +0100179 int index, apic_ids[CONFIG_MAX_CPUS] = {0}, num_cpus = 0, sort_start = 0;
180 for (unsigned int thread_id = 0; thread_id <= MAX_THREAD_ID; thread_id++) {
181 for (cpu = all_devices; cpu; cpu = cpu->next) {
182 if (!is_enabled_cpu(cpu))
183 continue;
184 if (num_cpus >= ARRAY_SIZE(apic_ids))
185 break;
186 if (cpu->path.apic.thread_id != thread_id)
187 continue;
188 apic_ids[num_cpus++] = cpu->path.apic.apic_id;
189 }
190 bubblesort(&apic_ids[sort_start], num_cpus - sort_start, NUM_ASCENDING);
191 sort_start = num_cpus;
Werner Zehdb561e62019-02-19 13:39:56 +0100192 }
Kyösti Mälkki2e9f0d32023-04-07 23:05:46 +0300193 for (index = 0; index < num_cpus; index++)
194 current = acpi_create_madt_one_lapic(current, index, apic_ids[index]);
Uwe Hermann622824c2010-11-19 15:14:42 +0000195
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000196 return current;
197}
198
Uwe Hermann622824c2010-11-19 15:14:42 +0000199int acpi_create_madt_ioapic(acpi_madt_ioapic_t *ioapic, u8 id, u32 addr,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300200 u32 gsi_base)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000201{
Himanshu Sahdev8c09b822019-10-21 18:50:50 +0530202 ioapic->type = IO_APIC; /* I/O APIC structure */
Uwe Hermann622824c2010-11-19 15:14:42 +0000203 ioapic->length = sizeof(acpi_madt_ioapic_t);
204 ioapic->reserved = 0x00;
205 ioapic->gsi_base = gsi_base;
206 ioapic->ioapic_id = id;
207 ioapic->ioapic_addr = addr;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000208
Uwe Hermann622824c2010-11-19 15:14:42 +0000209 return ioapic->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000210}
211
Kyösti Mälkkic7da0272021-06-08 11:37:08 +0300212#if ENV_X86
213/* For a system with multiple I/O APICs it's required that the one potentially
214 routing i8259 via ExtNMI delivery calls this first to get GSI #0. */
215int acpi_create_madt_ioapic_from_hw(acpi_madt_ioapic_t *ioapic, u32 addr)
216{
217 static u32 gsi_base;
218 u32 my_base;
219 u8 id = get_ioapic_id((void *)(uintptr_t)addr);
220 u8 count = ioapic_get_max_vectors((void *)(uintptr_t)addr);
221
222 my_base = gsi_base;
223 gsi_base += count;
224 return acpi_create_madt_ioapic(ioapic, id, addr, my_base);
225}
226#endif
227
Stefan Reinauer777224c2005-01-19 14:06:41 +0000228int acpi_create_madt_irqoverride(acpi_madt_irqoverride_t *irqoverride,
Stefan Reinauer06feb882004-02-03 16:11:35 +0000229 u8 bus, u8 source, u32 gsirq, u16 flags)
230{
Himanshu Sahdev8c09b822019-10-21 18:50:50 +0530231 irqoverride->type = IRQ_SOURCE_OVERRIDE; /* Interrupt source override */
Uwe Hermann622824c2010-11-19 15:14:42 +0000232 irqoverride->length = sizeof(acpi_madt_irqoverride_t);
233 irqoverride->bus = bus;
234 irqoverride->source = source;
235 irqoverride->gsirq = gsirq;
236 irqoverride->flags = flags;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000237
Uwe Hermann622824c2010-11-19 15:14:42 +0000238 return irqoverride->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000239}
240
Kyösti Mälkki9ac1fb72023-04-07 22:39:53 +0300241static int acpi_create_madt_lapic_nmi(acpi_madt_lapic_nmi_t *lapic_nmi, u8 cpu,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300242 u16 flags, u8 lint)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000243{
Himanshu Sahdev8c09b822019-10-21 18:50:50 +0530244 lapic_nmi->type = LOCAL_APIC_NMI; /* Local APIC NMI structure */
Uwe Hermann622824c2010-11-19 15:14:42 +0000245 lapic_nmi->length = sizeof(acpi_madt_lapic_nmi_t);
246 lapic_nmi->flags = flags;
247 lapic_nmi->processor_id = cpu;
248 lapic_nmi->lint = lint;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000249
Uwe Hermann622824c2010-11-19 15:14:42 +0000250 return lapic_nmi->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000251}
252
Kyösti Mälkki9ac1fb72023-04-07 22:39:53 +0300253static int acpi_create_madt_lx2apic_nmi(acpi_madt_lx2apic_nmi_t *lapic_nmi, u32 cpu,
Patrick Rudolph56a3ef22020-03-24 17:29:49 +0100254 u16 flags, u8 lint)
255{
256 lapic_nmi->type = LOCAL_X2APIC_NMI; /* Local APIC NMI structure */
257 lapic_nmi->length = sizeof(acpi_madt_lx2apic_nmi_t);
258 lapic_nmi->flags = flags;
259 lapic_nmi->processor_id = cpu;
260 lapic_nmi->lint = lint;
261 lapic_nmi->reserved[0] = 0;
262 lapic_nmi->reserved[1] = 0;
263 lapic_nmi->reserved[2] = 0;
264
265 return lapic_nmi->length;
266}
267
Kyösti Mälkki9ac1fb72023-04-07 22:39:53 +0300268unsigned long acpi_create_madt_lapic_nmis(unsigned long current)
Kyösti Mälkki66b5e1b2022-11-12 21:13:45 +0200269{
270 const u16 flags = MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH;
271
Kyösti Mälkki66b5e1b2022-11-12 21:13:45 +0200272 /* 1: LINT1 connect to NMI */
273 /* create all subtables for processors */
274 current += acpi_create_madt_lapic_nmi((acpi_madt_lapic_nmi_t *)current,
275 ACPI_MADT_LAPIC_NMI_ALL_PROCESSORS, flags, 1);
276
277 if (!CONFIG(XAPIC_ONLY))
278 current += acpi_create_madt_lx2apic_nmi((acpi_madt_lx2apic_nmi_t *)current,
279 ACPI_MADT_LX2APIC_NMI_ALL_PROCESSORS, flags, 1);
280
281 return current;
282}
283
Kyösti Mälkki9ac1fb72023-04-07 22:39:53 +0300284unsigned long acpi_create_madt_lapics_with_nmis(unsigned long current)
285{
286 current = acpi_create_madt_lapics(current);
287 current = acpi_create_madt_lapic_nmis(current);
288 return current;
289}
290
Stefan Reinauer777224c2005-01-19 14:06:41 +0000291void acpi_create_madt(acpi_madt_t *madt)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000292{
Uwe Hermann622824c2010-11-19 15:14:42 +0000293 acpi_header_t *header = &(madt->header);
294 unsigned long current = (unsigned long)madt + sizeof(acpi_madt_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000295
Stefan Reinauer06feb882004-02-03 16:11:35 +0000296 memset((void *)madt, 0, sizeof(acpi_madt_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000297
John Zhao2ba303e2019-05-28 16:48:14 -0700298 if (!header)
299 return;
300
Uwe Hermann622824c2010-11-19 15:14:42 +0000301 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000302 memcpy(header->signature, "APIC", 4);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000303 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000304 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000305 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000306
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100307 header->asl_compiler_revision = asl_revision;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000308 header->length = sizeof(acpi_madt_t);
Marc Jones93a51762018-08-22 18:57:24 -0600309 header->revision = get_acpi_table_revision(MADT);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000310
Furquan Shaikhb1859a62020-04-30 21:27:47 -0700311 madt->lapic_addr = cpu_get_lapic_addr();
Nico Huber9df72e02018-11-24 18:25:50 +0100312 if (CONFIG(ACPI_HAVE_PCAT_8259))
313 madt->flags |= 1;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000314
Kyösti Mälkki69a13962023-04-08 14:10:48 +0300315 if (CONFIG(ACPI_COMMON_MADT_LAPIC))
316 current = acpi_create_madt_lapics_with_nmis(current);
317
Kyösti Mälkkia5fa5342022-11-18 13:23:52 +0200318 if (!CONFIG(ACPI_NO_MADT))
319 current = acpi_fill_madt(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000320
Uwe Hermann622824c2010-11-19 15:14:42 +0000321 /* (Re)calculate length and checksum. */
322 header->length = current - (unsigned long)madt;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000323
Uwe Hermann622824c2010-11-19 15:14:42 +0000324 header->checksum = acpi_checksum((void *)madt, header->length);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000325}
326
Kyösti Mälkkib54388d2021-02-14 15:09:45 +0200327static unsigned long acpi_fill_mcfg(unsigned long current)
328{
329 current += acpi_create_mcfg_mmconfig((acpi_mcfg_mmconfig_t *)current,
Shelley Chen4e9bb332021-10-20 15:43:45 -0700330 CONFIG_ECAM_MMCONF_BASE_ADDRESS, 0, 0,
331 CONFIG_ECAM_MMCONF_BUS_NUMBER - 1);
Kyösti Mälkkib54388d2021-02-14 15:09:45 +0200332 return current;
333}
334
Uwe Hermann622824c2010-11-19 15:14:42 +0000335/* MCFG is defined in the PCI Firmware Specification 3.0. */
Rudolf Mareke6409f22007-11-03 12:50:26 +0000336void acpi_create_mcfg(acpi_mcfg_t *mcfg)
337{
Uwe Hermann622824c2010-11-19 15:14:42 +0000338 acpi_header_t *header = &(mcfg->header);
339 unsigned long current = (unsigned long)mcfg + sizeof(acpi_mcfg_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000340
Rudolf Mareke6409f22007-11-03 12:50:26 +0000341 memset((void *)mcfg, 0, sizeof(acpi_mcfg_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000342
John Zhao2ba303e2019-05-28 16:48:14 -0700343 if (!header)
344 return;
345
Uwe Hermann622824c2010-11-19 15:14:42 +0000346 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000347 memcpy(header->signature, "MCFG", 4);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000348 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000349 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000350 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000351
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100352 header->asl_compiler_revision = asl_revision;
Rudolf Mareke6409f22007-11-03 12:50:26 +0000353 header->length = sizeof(acpi_mcfg_t);
Marc Jones93a51762018-08-22 18:57:24 -0600354 header->revision = get_acpi_table_revision(MCFG);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000355
Shelley Chen4e9bb332021-10-20 15:43:45 -0700356 if (CONFIG(ECAM_MMCONF_SUPPORT))
Kyösti Mälkkib54388d2021-02-14 15:09:45 +0200357 current = acpi_fill_mcfg(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000358
Uwe Hermann622824c2010-11-19 15:14:42 +0000359 /* (Re)calculate length and checksum. */
360 header->length = current - (unsigned long)mcfg;
361 header->checksum = acpi_checksum((void *)mcfg, header->length);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000362}
363
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200364static void *get_tcpa_log(u32 *size)
365{
366 const struct cbmem_entry *ce;
367 const u32 tcpa_default_log_len = 0x10000;
368 void *lasa;
Philipp Deppenwiese791ba972018-07-30 01:15:20 +0200369 ce = cbmem_entry_find(CBMEM_ID_TCPA_TCG_LOG);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200370 if (ce) {
371 lasa = cbmem_entry_start(ce);
372 *size = cbmem_entry_size(ce);
373 printk(BIOS_DEBUG, "TCPA log found at %p\n", lasa);
374 return lasa;
375 }
Philipp Deppenwiese791ba972018-07-30 01:15:20 +0200376 lasa = cbmem_add(CBMEM_ID_TCPA_TCG_LOG, tcpa_default_log_len);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200377 if (!lasa) {
378 printk(BIOS_ERR, "TCPA log creation failed\n");
379 return NULL;
380 }
381
382 printk(BIOS_DEBUG, "TCPA log created at %p\n", lasa);
Lee Leahy024b13d2017-03-16 13:41:11 -0700383 memset(lasa, 0, tcpa_default_log_len);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200384
385 *size = tcpa_default_log_len;
386 return lasa;
387}
388
389static void acpi_create_tcpa(acpi_tcpa_t *tcpa)
390{
391 acpi_header_t *header = &(tcpa->header);
392 u32 tcpa_log_len;
393 void *lasa;
394
395 memset((void *)tcpa, 0, sizeof(acpi_tcpa_t));
396
397 lasa = get_tcpa_log(&tcpa_log_len);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700398 if (!lasa)
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200399 return;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200400
John Zhao2ba303e2019-05-28 16:48:14 -0700401 if (!header)
402 return;
403
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200404 /* Fill out header fields. */
405 memcpy(header->signature, "TCPA", 4);
406 memcpy(header->oem_id, OEM_ID, 6);
407 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
408 memcpy(header->asl_compiler_id, ASLC, 4);
409
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100410 header->asl_compiler_revision = asl_revision;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200411 header->length = sizeof(acpi_tcpa_t);
Marc Jones93a51762018-08-22 18:57:24 -0600412 header->revision = get_acpi_table_revision(TCPA);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200413
414 tcpa->platform_class = 0;
415 tcpa->laml = tcpa_log_len;
Elyes Haouas3141fbad2022-11-18 15:22:32 +0100416 tcpa->lasa = (uintptr_t)lasa;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200417
418 /* Calculate checksum. */
419 header->checksum = acpi_checksum((void *)tcpa, header->length);
420}
421
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100422static void *get_tpm2_log(u32 *size)
423{
424 const struct cbmem_entry *ce;
425 const u32 tpm2_default_log_len = 0x10000;
426 void *lasa;
427 ce = cbmem_entry_find(CBMEM_ID_TPM2_TCG_LOG);
428 if (ce) {
429 lasa = cbmem_entry_start(ce);
430 *size = cbmem_entry_size(ce);
431 printk(BIOS_DEBUG, "TPM2 log found at %p\n", lasa);
432 return lasa;
433 }
434 lasa = cbmem_add(CBMEM_ID_TPM2_TCG_LOG, tpm2_default_log_len);
435 if (!lasa) {
436 printk(BIOS_ERR, "TPM2 log creation failed\n");
437 return NULL;
438 }
439
440 printk(BIOS_DEBUG, "TPM2 log created at %p\n", lasa);
441 memset(lasa, 0, tpm2_default_log_len);
442
443 *size = tpm2_default_log_len;
444 return lasa;
445}
446
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200447static void acpi_create_tpm2(acpi_tpm2_t *tpm2)
448{
449 acpi_header_t *header = &(tpm2->header);
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100450 u32 tpm2_log_len;
451 void *lasa;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200452
453 memset((void *)tpm2, 0, sizeof(acpi_tpm2_t));
454
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100455 /*
456 * Some payloads like SeaBIOS depend on log area to use TPM2.
457 * Get the memory size and address of TPM2 log area or initialize it.
458 */
459 lasa = get_tpm2_log(&tpm2_log_len);
460 if (!lasa)
461 tpm2_log_len = 0;
462
John Zhao2ba303e2019-05-28 16:48:14 -0700463 if (!header)
464 return;
465
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200466 /* Fill out header fields. */
467 memcpy(header->signature, "TPM2", 4);
468 memcpy(header->oem_id, OEM_ID, 6);
469 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
470 memcpy(header->asl_compiler_id, ASLC, 4);
471
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100472 header->asl_compiler_revision = asl_revision;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200473 header->length = sizeof(acpi_tpm2_t);
474 header->revision = get_acpi_table_revision(TPM2);
475
476 /* Hard to detect for coreboot. Just set it to 0 */
477 tpm2->platform_class = 0;
Christian Walter26e0d4c2019-07-14 15:47:23 +0200478 if (CONFIG(CRB_TPM)) {
479 /* Must be set to 7 for CRB Support */
480 tpm2->control_area = CONFIG_CRB_TPM_BASE_ADDRESS + 0x40;
481 tpm2->start_method = 7;
482 } else {
483 /* Must be set to 0 for FIFO interface support */
484 tpm2->control_area = 0;
485 tpm2->start_method = 6;
486 }
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200487 memset(tpm2->msp, 0, sizeof(tpm2->msp));
488
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100489 /* Fill the log area size and start address fields. */
490 tpm2->laml = tpm2_log_len;
Elyes Haouas3141fbad2022-11-18 15:22:32 +0100491 tpm2->lasa = (uintptr_t)lasa;
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100492
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200493 /* Calculate checksum. */
494 header->checksum = acpi_checksum((void *)tpm2, header->length);
495}
496
Duncan Laurie37319032016-05-26 12:47:05 -0700497static void acpi_ssdt_write_cbtable(void)
498{
499 const struct cbmem_entry *cbtable;
500 uintptr_t base;
501 uint32_t size;
502
503 cbtable = cbmem_entry_find(CBMEM_ID_CBTABLE);
504 if (!cbtable)
505 return;
506 base = (uintptr_t)cbmem_entry_start(cbtable);
507 size = cbmem_entry_size(cbtable);
508
509 acpigen_write_device("CTBL");
510 acpigen_write_coreboot_hid(COREBOOT_ACPI_ID_CBTABLE);
511 acpigen_write_name_integer("_UID", 0);
David Wu5dff3962018-03-21 16:48:53 +0800512 acpigen_write_STA(ACPI_STATUS_DEVICE_HIDDEN_ON);
Duncan Laurie37319032016-05-26 12:47:05 -0700513 acpigen_write_name("_CRS");
514 acpigen_write_resourcetemplate_header();
515 acpigen_write_mem32fixed(0, base, size);
516 acpigen_write_resourcetemplate_footer();
517 acpigen_pop_len();
518}
519
Myles Watson3fe6b702009-10-09 20:13:43 +0000520void acpi_create_ssdt_generator(acpi_header_t *ssdt, const char *oem_table_id)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000521{
Uwe Hermann622824c2010-11-19 15:14:42 +0000522 unsigned long current = (unsigned long)ssdt + sizeof(acpi_header_t);
523
Rudolf Marek293b5f52009-02-01 18:35:15 +0000524 memset((void *)ssdt, 0, sizeof(acpi_header_t));
Uwe Hermann622824c2010-11-19 15:14:42 +0000525
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000526 memcpy(&ssdt->signature, "SSDT", 4);
Marc Jones93a51762018-08-22 18:57:24 -0600527 ssdt->revision = get_acpi_table_revision(SSDT);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000528 memcpy(&ssdt->oem_id, OEM_ID, 6);
529 memcpy(&ssdt->oem_table_id, oem_table_id, 8);
530 ssdt->oem_revision = 42;
Uwe Hermann622824c2010-11-19 15:14:42 +0000531 memcpy(&ssdt->asl_compiler_id, ASLC, 4);
Elyes HAOUAS26071aa2019-02-15 08:21:33 +0100532 ssdt->asl_compiler_revision = asl_revision;
Rudolf Marek293b5f52009-02-01 18:35:15 +0000533 ssdt->length = sizeof(acpi_header_t);
534
Elyes Haouas3141fbad2022-11-18 15:22:32 +0100535 acpigen_set_current((char *)current);
Duncan Laurie37319032016-05-26 12:47:05 -0700536
537 /* Write object to declare coreboot tables */
538 acpi_ssdt_write_cbtable();
539
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200540 {
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100541 struct device *dev;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200542 for (dev = all_devices; dev; dev = dev->next)
Karthikeyan Ramasubramaniand1c0f952020-11-02 16:26:52 -0700543 if (dev->enabled && dev->ops && dev->ops->acpi_fill_ssdt)
Nico Huber68680dd2020-03-31 17:34:52 +0200544 dev->ops->acpi_fill_ssdt(dev);
Elyes Haouas3141fbad2022-11-18 15:22:32 +0100545 current = (unsigned long)acpigen_get_current();
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200546 }
Rudolf Marek293b5f52009-02-01 18:35:15 +0000547
Uwe Hermann622824c2010-11-19 15:14:42 +0000548 /* (Re)calculate length and checksum. */
Rudolf Marek293b5f52009-02-01 18:35:15 +0000549 ssdt->length = current - (unsigned long)ssdt;
550 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
551}
552
Stefan Reinauerf622d592005-11-26 16:56:05 +0000553int acpi_create_srat_lapic(acpi_srat_lapic_t *lapic, u8 node, u8 apic)
554{
Myles Watson0b4c9f02009-03-10 18:06:47 +0000555 memset((void *)lapic, 0, sizeof(acpi_srat_lapic_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000556
Uwe Hermann622824c2010-11-19 15:14:42 +0000557 lapic->type = 0; /* Processor local APIC/SAPIC affinity structure */
558 lapic->length = sizeof(acpi_srat_lapic_t);
559 lapic->flags = (1 << 0); /* Enabled (the use of this structure). */
560 lapic->proximity_domain_7_0 = node;
561 /* TODO: proximity_domain_31_8, local SAPIC EID, clock domain. */
562 lapic->apic_id = apic;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000563
Uwe Hermann622824c2010-11-19 15:14:42 +0000564 return lapic->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000565}
566
Naresh Solanki76835cc2023-01-20 19:13:02 +0100567int acpi_create_srat_x2apic(acpi_srat_x2apic_t *x2apic, u32 node, u32 apic)
568{
569 memset((void *)x2apic, 0, sizeof(acpi_srat_x2apic_t));
570
571 x2apic->type = 2; /* Processor x2APIC structure */
572 x2apic->length = sizeof(acpi_srat_x2apic_t);
573 x2apic->flags = (1 << 0); /* Enabled (the use of this structure). */
574 x2apic->proximity_domain = node;
575 x2apic->x2apic_id = apic;
576
577 return x2apic->length;
578}
579
Uwe Hermann622824c2010-11-19 15:14:42 +0000580int 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 +0300581 u32 flags)
Stefan Reinauerf622d592005-11-26 16:56:05 +0000582{
Uwe Hermann622824c2010-11-19 15:14:42 +0000583 mem->type = 1; /* Memory affinity structure */
584 mem->length = sizeof(acpi_srat_mem_t);
585 mem->base_address_low = (basek << 10);
586 mem->base_address_high = (basek >> (32 - 10));
587 mem->length_low = (sizek << 10);
588 mem->length_high = (sizek >> (32 - 10));
589 mem->proximity_domain = node;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000590 mem->flags = flags;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000591
Uwe Hermann622824c2010-11-19 15:14:42 +0000592 return mem->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000593}
594
Jonathan Zhang3164b642021-04-21 17:51:31 -0700595int acpi_create_srat_gia_pci(acpi_srat_gia_t *gia, u32 proximity_domain,
596 u16 seg, u8 bus, u8 dev, u8 func, u32 flags)
597{
598 gia->type = ACPI_SRAT_STRUCTURE_GIA;
599 gia->length = sizeof(acpi_srat_gia_t);
600 gia->proximity_domain = proximity_domain;
601 gia->dev_handle_type = ACPI_SRAT_GIA_DEV_HANDLE_PCI;
602 /* First two bytes has segment number */
603 memcpy(gia->dev_handle, &seg, 2);
604 gia->dev_handle[2] = bus; /* Byte 2 has bus number */
605 /* Byte 3 has bits 7:3 for dev, bits 2:0 for func */
606 gia->dev_handle[3] = PCI_SLOT(dev) | PCI_FUNC(func);
607 gia->flags = flags;
608
609 return gia->length;
610}
611
Uwe Hermann622824c2010-11-19 15:14:42 +0000612/* http://www.microsoft.com/whdc/system/sysinternals/sratdwn.mspx */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +0200613void acpi_create_srat(acpi_srat_t *srat,
614 unsigned long (*acpi_fill_srat)(unsigned long current))
Stefan Reinauerf622d592005-11-26 16:56:05 +0000615{
Uwe Hermann622824c2010-11-19 15:14:42 +0000616 acpi_header_t *header = &(srat->header);
617 unsigned long current = (unsigned long)srat + sizeof(acpi_srat_t);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000618
Uwe Hermann622824c2010-11-19 15:14:42 +0000619 memset((void *)srat, 0, sizeof(acpi_srat_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000620
John Zhao2ba303e2019-05-28 16:48:14 -0700621 if (!header)
622 return;
623
Uwe Hermann622824c2010-11-19 15:14:42 +0000624 /* Fill out header fields. */
625 memcpy(header->signature, "SRAT", 4);
626 memcpy(header->oem_id, OEM_ID, 6);
627 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
628 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000629
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100630 header->asl_compiler_revision = asl_revision;
Uwe Hermann622824c2010-11-19 15:14:42 +0000631 header->length = sizeof(acpi_srat_t);
Marc Jones93a51762018-08-22 18:57:24 -0600632 header->revision = get_acpi_table_revision(SRAT);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000633
Uwe Hermann622824c2010-11-19 15:14:42 +0000634 srat->resv = 1; /* Spec: Reserved to 1 for backwards compatibility. */
Stefan Reinauerf622d592005-11-26 16:56:05 +0000635
Uwe Hermann622824c2010-11-19 15:14:42 +0000636 current = acpi_fill_srat(current);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000637
Uwe Hermann622824c2010-11-19 15:14:42 +0000638 /* (Re)calculate length and checksum. */
639 header->length = current - (unsigned long)srat;
640 header->checksum = acpi_checksum((void *)srat, header->length);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000641}
642
Jonathan Zhang3dcafa82022-05-11 13:11:20 -0700643int acpi_create_cedt_chbs(acpi_cedt_chbs_t *chbs, u32 uid, u32 cxl_ver, u64 base)
644{
645 memset((void *)chbs, 0, sizeof(acpi_cedt_chbs_t));
646
647 chbs->type = ACPI_CEDT_STRUCTURE_CHBS;
648 chbs->length = sizeof(acpi_cedt_chbs_t);
649 chbs->uid = uid;
650 chbs->cxl_ver = cxl_ver;
651 chbs->base = base;
652
653 /*
654 * CXL spec 2.0 section 9.14.1.2 "CXL CHBS"
655 * CXL 1.1 spec compliant host bridge: 8KB
656 * CXL 2.0 spec compliant host bridge: 64KB
657 */
658 if (cxl_ver == ACPI_CEDT_CHBS_CXL_VER_1_1)
659 chbs->len = 8 * KiB;
660 else if (cxl_ver == ACPI_CEDT_CHBS_CXL_VER_2_0)
661 chbs->len = 64 * KiB;
662 else
663 printk(BIOS_ERR, "ACPI(%s:%s): Incorrect CXL version:%d\n", __FILE__, __func__,
664 cxl_ver);
665
666 return chbs->length;
667}
668
669int acpi_create_cedt_cfmws(acpi_cedt_cfmws_t *cfmws, u64 base_hpa, u64 window_size, u8 eniw,
670 u32 hbig, u16 restriction, u16 qtg_id, const u32 *interleave_target)
671{
672 memset((void *)cfmws, 0, sizeof(acpi_cedt_cfmws_t));
673
674 cfmws->type = ACPI_CEDT_STRUCTURE_CFMWS;
675
676 u8 niw = 0;
677 if (eniw >= 8)
678 printk(BIOS_ERR, "ACPI(%s:%s): Incorrect eniw::%d\n", __FILE__, __func__, eniw);
679 else
680 /* NIW = 2 ** ENIW */
681 niw = 0x1 << eniw;
682 /* 36 + 4 * NIW */
683 cfmws->length = sizeof(acpi_cedt_cfmws_t) + 4 * niw;
684
685 cfmws->base_hpa = base_hpa;
686 cfmws->window_size = window_size;
687 cfmws->eniw = eniw;
688
689 // 0: Standard Modulo Arithmetic. Other values reserved.
690 cfmws->interleave_arithmetic = 0;
691
692 cfmws->hbig = hbig;
693 cfmws->restriction = restriction;
694 cfmws->qtg_id = qtg_id;
695 memcpy(&cfmws->interleave_target, interleave_target, 4 * niw);
696
697 return cfmws->length;
698}
699
700void acpi_create_cedt(acpi_cedt_t *cedt, unsigned long (*acpi_fill_cedt)(unsigned long current))
701{
702 acpi_header_t *header = &(cedt->header);
703 unsigned long current = (unsigned long)cedt + sizeof(acpi_cedt_t);
704
705 memset((void *)cedt, 0, sizeof(acpi_cedt_t));
706
707 if (!header)
708 return;
709
710 /* Fill out header fields. */
711 memcpy(header->signature, "CEDT", 4);
712 memcpy(header->oem_id, OEM_ID, 6);
713 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
714 memcpy(header->asl_compiler_id, ASLC, 4);
715
716 header->asl_compiler_revision = asl_revision;
717 header->length = sizeof(acpi_cedt_t);
718 header->revision = get_acpi_table_revision(CEDT);
719
720 current = acpi_fill_cedt(current);
721
722 /* (Re)calculate length and checksum. */
723 header->length = current - (unsigned long)cedt;
724 header->checksum = acpi_checksum((void *)cedt, header->length);
725}
726
Jonathan Zhang2a4e1f42021-04-01 11:43:37 -0700727int acpi_create_hmat_mpda(acpi_hmat_mpda_t *mpda, u32 initiator, u32 memory)
728{
729 memset((void *)mpda, 0, sizeof(acpi_hmat_mpda_t));
730
731 mpda->type = 0; /* Memory Proximity Domain Attributes structure */
732 mpda->length = sizeof(acpi_hmat_mpda_t);
733 /*
734 * Proximity Domain for Attached Initiator field is valid.
735 * Bit 1 and bit 2 are reserved since HMAT revision 2.
736 */
737 mpda->flags = (1 << 0);
738 mpda->proximity_domain_initiator = initiator;
739 mpda->proximity_domain_memory = memory;
740
741 return mpda->length;
742}
743
744void acpi_create_hmat(acpi_hmat_t *hmat,
745 unsigned long (*acpi_fill_hmat)(unsigned long current))
746{
747 acpi_header_t *header = &(hmat->header);
748 unsigned long current = (unsigned long)hmat + sizeof(acpi_hmat_t);
749
750 memset((void *)hmat, 0, sizeof(acpi_hmat_t));
751
752 if (!header)
753 return;
754
755 /* Fill out header fields. */
756 memcpy(header->signature, "HMAT", 4);
757 memcpy(header->oem_id, OEM_ID, 6);
758 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
759 memcpy(header->asl_compiler_id, ASLC, 4);
760
761 header->asl_compiler_revision = asl_revision;
762 header->length = sizeof(acpi_hmat_t);
763 header->revision = get_acpi_table_revision(HMAT);
764
765 current = acpi_fill_hmat(current);
766
767 /* (Re)calculate length and checksum. */
768 header->length = current - (unsigned long)hmat;
769 header->checksum = acpi_checksum((void *)hmat, header->length);
770}
771
Nico Hubere561f352015-10-26 11:51:25 +0100772void acpi_create_dmar(acpi_dmar_t *dmar, enum dmar_flags flags,
Lee Leahy024b13d2017-03-16 13:41:11 -0700773 unsigned long (*acpi_fill_dmar)(unsigned long))
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200774{
775 acpi_header_t *header = &(dmar->header);
776 unsigned long current = (unsigned long)dmar + sizeof(acpi_dmar_t);
777
778 memset((void *)dmar, 0, sizeof(acpi_dmar_t));
779
John Zhao2ba303e2019-05-28 16:48:14 -0700780 if (!header)
781 return;
782
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200783 /* Fill out header fields. */
784 memcpy(header->signature, "DMAR", 4);
785 memcpy(header->oem_id, OEM_ID, 6);
786 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
787 memcpy(header->asl_compiler_id, ASLC, 4);
788
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100789 header->asl_compiler_revision = asl_revision;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200790 header->length = sizeof(acpi_dmar_t);
Marc Jones93a51762018-08-22 18:57:24 -0600791 header->revision = get_acpi_table_revision(DMAR);
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200792
Jacob Laskaf3f654d2016-03-15 21:53:27 -0500793 dmar->host_address_width = cpu_phys_address_size() - 1;
Nico Hubere561f352015-10-26 11:51:25 +0100794 dmar->flags = flags;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200795
796 current = acpi_fill_dmar(current);
797
798 /* (Re)calculate length and checksum. */
799 header->length = current - (unsigned long)dmar;
800 header->checksum = acpi_checksum((void *)dmar, header->length);
801}
802
803unsigned long acpi_create_dmar_drhd(unsigned long current, u8 flags,
Matt DeVillier7866d492018-03-29 14:59:57 +0200804 u16 segment, u64 bar)
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200805{
806 dmar_entry_t *drhd = (dmar_entry_t *)current;
807 memset(drhd, 0, sizeof(*drhd));
808 drhd->type = DMAR_DRHD;
809 drhd->length = sizeof(*drhd); /* will be fixed up later */
810 drhd->flags = flags;
811 drhd->segment = segment;
812 drhd->bar = bar;
813
814 return drhd->length;
815}
816
Matt DeVillier7866d492018-03-29 14:59:57 +0200817unsigned long acpi_create_dmar_rmrr(unsigned long current, u16 segment,
818 u64 bar, u64 limit)
819{
820 dmar_rmrr_entry_t *rmrr = (dmar_rmrr_entry_t *)current;
821 memset(rmrr, 0, sizeof(*rmrr));
822 rmrr->type = DMAR_RMRR;
823 rmrr->length = sizeof(*rmrr); /* will be fixed up later */
824 rmrr->segment = segment;
825 rmrr->bar = bar;
826 rmrr->limit = limit;
827
828 return rmrr->length;
829}
830
Werner Zehd4d76952016-07-27 06:56:36 +0200831unsigned long acpi_create_dmar_atsr(unsigned long current, u8 flags,
832 u16 segment)
833{
834 dmar_atsr_entry_t *atsr = (dmar_atsr_entry_t *)current;
835 memset(atsr, 0, sizeof(*atsr));
836 atsr->type = DMAR_ATSR;
837 atsr->length = sizeof(*atsr); /* will be fixed up later */
838 atsr->flags = flags;
839 atsr->segment = segment;
840
841 return atsr->length;
842}
843
John Zhao76e70672019-04-04 11:03:28 -0700844unsigned long acpi_create_dmar_rhsa(unsigned long current, u64 base_addr,
845 u32 proximity_domain)
846{
847 dmar_rhsa_entry_t *rhsa = (dmar_rhsa_entry_t *)current;
848 memset(rhsa, 0, sizeof(*rhsa));
849 rhsa->type = DMAR_RHSA;
850 rhsa->length = sizeof(*rhsa);
851 rhsa->base_address = base_addr;
852 rhsa->proximity_domain = proximity_domain;
853
854 return rhsa->length;
855}
856
857unsigned long acpi_create_dmar_andd(unsigned long current, u8 device_number,
858 const char *device_name)
859{
860 dmar_andd_entry_t *andd = (dmar_andd_entry_t *)current;
861 int andd_len = sizeof(dmar_andd_entry_t) + strlen(device_name) + 1;
862 memset(andd, 0, andd_len);
863 andd->type = DMAR_ANDD;
864 andd->length = andd_len;
865 andd->device_number = device_number;
866 memcpy(&andd->device_name, device_name, strlen(device_name));
867
868 return andd->length;
869}
870
John Zhao091532d2021-04-17 16:03:21 -0700871unsigned long acpi_create_dmar_satc(unsigned long current, u8 flags, u16 segment)
John Zhao6edbb182021-03-24 11:55:09 -0700872{
873 dmar_satc_entry_t *satc = (dmar_satc_entry_t *)current;
John Zhao091532d2021-04-17 16:03:21 -0700874 int satc_len = sizeof(dmar_satc_entry_t);
John Zhao6edbb182021-03-24 11:55:09 -0700875 memset(satc, 0, satc_len);
876 satc->type = DMAR_SATC;
877 satc->length = satc_len;
878 satc->flags = flags;
879 satc->segment_number = segment;
John Zhao6edbb182021-03-24 11:55:09 -0700880
881 return satc->length;
882}
883
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200884void acpi_dmar_drhd_fixup(unsigned long base, unsigned long current)
885{
886 dmar_entry_t *drhd = (dmar_entry_t *)base;
887 drhd->length = current - base;
888}
889
Matt DeVillier7866d492018-03-29 14:59:57 +0200890void acpi_dmar_rmrr_fixup(unsigned long base, unsigned long current)
891{
892 dmar_rmrr_entry_t *rmrr = (dmar_rmrr_entry_t *)base;
893 rmrr->length = current - base;
894}
895
Werner Zehd4d76952016-07-27 06:56:36 +0200896void acpi_dmar_atsr_fixup(unsigned long base, unsigned long current)
897{
898 dmar_atsr_entry_t *atsr = (dmar_atsr_entry_t *)base;
899 atsr->length = current - base;
900}
901
John Zhao6edbb182021-03-24 11:55:09 -0700902void acpi_dmar_satc_fixup(unsigned long base, unsigned long current)
903{
904 dmar_satc_entry_t *satc = (dmar_satc_entry_t *)base;
905 satc->length = current - base;
906}
907
Matt DeVillier7866d492018-03-29 14:59:57 +0200908static unsigned long acpi_create_dmar_ds(unsigned long current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100909 enum dev_scope_type type, u8 enumeration_id, u8 bus, u8 dev, u8 fn)
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200910{
Nico Huber6c4751d2015-10-26 12:03:54 +0100911 /* we don't support longer paths yet */
912 const size_t dev_scope_length = sizeof(dev_scope_t) + 2;
913
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200914 dev_scope_t *ds = (dev_scope_t *)current;
Nico Huber6c4751d2015-10-26 12:03:54 +0100915 memset(ds, 0, dev_scope_length);
916 ds->type = type;
917 ds->length = dev_scope_length;
918 ds->enumeration = enumeration_id;
919 ds->start_bus = bus;
920 ds->path[0].dev = dev;
921 ds->path[0].fn = fn;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200922
923 return ds->length;
924}
925
Matt DeVillier7866d492018-03-29 14:59:57 +0200926unsigned long acpi_create_dmar_ds_pci_br(unsigned long current, u8 bus,
Werner Zeh21a5bff2016-07-27 07:07:20 +0200927 u8 dev, u8 fn)
928{
Matt DeVillier7866d492018-03-29 14:59:57 +0200929 return acpi_create_dmar_ds(current,
Werner Zeh21a5bff2016-07-27 07:07:20 +0200930 SCOPE_PCI_SUB, 0, bus, dev, fn);
931}
932
Matt DeVillier7866d492018-03-29 14:59:57 +0200933unsigned long acpi_create_dmar_ds_pci(unsigned long current, u8 bus,
Nico Huber6c4751d2015-10-26 12:03:54 +0100934 u8 dev, u8 fn)
935{
Matt DeVillier7866d492018-03-29 14:59:57 +0200936 return acpi_create_dmar_ds(current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100937 SCOPE_PCI_ENDPOINT, 0, bus, dev, fn);
938}
939
Matt DeVillier7866d492018-03-29 14:59:57 +0200940unsigned long acpi_create_dmar_ds_ioapic(unsigned long current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100941 u8 enumeration_id, u8 bus, u8 dev, u8 fn)
942{
Matt DeVillier7866d492018-03-29 14:59:57 +0200943 return acpi_create_dmar_ds(current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100944 SCOPE_IOAPIC, enumeration_id, bus, dev, fn);
945}
946
Arthur Heymansbc8f8592022-12-02 13:17:39 +0100947unsigned long acpi_create_dmar_ds_ioapic_from_hw(unsigned long current,
948 u32 addr, u8 bus, u8 dev, u8 fn)
949{
950 u8 enumeration_id = get_ioapic_id((void *)(uintptr_t)addr);
951 return acpi_create_dmar_ds(current,
952 SCOPE_IOAPIC, enumeration_id, bus, dev, fn);
953}
954
Matt DeVillier7866d492018-03-29 14:59:57 +0200955unsigned long acpi_create_dmar_ds_msi_hpet(unsigned long current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100956 u8 enumeration_id, u8 bus, u8 dev, u8 fn)
957{
Matt DeVillier7866d492018-03-29 14:59:57 +0200958 return acpi_create_dmar_ds(current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100959 SCOPE_MSI_HPET, enumeration_id, bus, dev, fn);
960}
961
Uwe Hermann622824c2010-11-19 15:14:42 +0000962/* http://h21007.www2.hp.com/portal/download/files/unprot/Itanium/slit.pdf */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +0200963void acpi_create_slit(acpi_slit_t *slit,
964 unsigned long (*acpi_fill_slit)(unsigned long current))
Yinghai Lud4b278c2006-10-04 20:46:15 +0000965{
Uwe Hermann622824c2010-11-19 15:14:42 +0000966 acpi_header_t *header = &(slit->header);
967 unsigned long current = (unsigned long)slit + sizeof(acpi_slit_t);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000968
Uwe Hermann622824c2010-11-19 15:14:42 +0000969 memset((void *)slit, 0, sizeof(acpi_slit_t));
Yinghai Lud4b278c2006-10-04 20:46:15 +0000970
John Zhao2ba303e2019-05-28 16:48:14 -0700971 if (!header)
972 return;
973
Uwe Hermann622824c2010-11-19 15:14:42 +0000974 /* Fill out header fields. */
975 memcpy(header->signature, "SLIT", 4);
976 memcpy(header->oem_id, OEM_ID, 6);
977 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
978 memcpy(header->asl_compiler_id, ASLC, 4);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000979
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100980 header->asl_compiler_revision = asl_revision;
Uwe Hermann622824c2010-11-19 15:14:42 +0000981 header->length = sizeof(acpi_slit_t);
Marc Jones93a51762018-08-22 18:57:24 -0600982 header->revision = get_acpi_table_revision(SLIT);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000983
Uwe Hermann622824c2010-11-19 15:14:42 +0000984 current = acpi_fill_slit(current);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000985
Uwe Hermann622824c2010-11-19 15:14:42 +0000986 /* (Re)calculate length and checksum. */
987 header->length = current - (unsigned long)slit;
988 header->checksum = acpi_checksum((void *)slit, header->length);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000989}
990
Uwe Hermann622824c2010-11-19 15:14:42 +0000991/* http://www.intel.com/hardwaredesign/hpetspec_1.pdf */
Stefan Reinauer777224c2005-01-19 14:06:41 +0000992void acpi_create_hpet(acpi_hpet_t *hpet)
Stefan Reinauer688b3852004-01-28 16:56:14 +0000993{
Uwe Hermann622824c2010-11-19 15:14:42 +0000994 acpi_header_t *header = &(hpet->header);
995 acpi_addr_t *addr = &(hpet->addr);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000996
Stefan Reinauera7648c22004-01-29 17:31:34 +0000997 memset((void *)hpet, 0, sizeof(acpi_hpet_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000998
John Zhao2ba303e2019-05-28 16:48:14 -0700999 if (!header)
1000 return;
1001
Uwe Hermann622824c2010-11-19 15:14:42 +00001002 /* Fill out header fields. */
Stefan Reinauer4704dc52009-07-22 01:11:37 +00001003 memcpy(header->signature, "HPET", 4);
Stefan Reinauer688b3852004-01-28 16:56:14 +00001004 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001005 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +00001006 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001007
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001008 header->asl_compiler_revision = asl_revision;
Stefan Reinauer688b3852004-01-28 16:56:14 +00001009 header->length = sizeof(acpi_hpet_t);
Marc Jones93a51762018-08-22 18:57:24 -06001010 header->revision = get_acpi_table_revision(HPET);
Stefan Reinauer688b3852004-01-28 16:56:14 +00001011
Uwe Hermann622824c2010-11-19 15:14:42 +00001012 /* Fill out HPET address. */
Elyes HAOUAS04071f42020-07-20 17:05:24 +02001013 addr->space_id = ACPI_ADDRESS_SPACE_MEMORY;
Uwe Hermann622824c2010-11-19 15:14:42 +00001014 addr->bit_width = 64;
1015 addr->bit_offset = 0;
Felix Held972d9f22022-02-23 16:32:20 +01001016 addr->addrl = HPET_BASE_ADDRESS & 0xffffffff;
1017 addr->addrh = ((unsigned long long)HPET_BASE_ADDRESS) >> 32;
Stefan Reinauer688b3852004-01-28 16:56:14 +00001018
Felix Held972d9f22022-02-23 16:32:20 +01001019 hpet->id = read32p(HPET_BASE_ADDRESS);
Uwe Hermann622824c2010-11-19 15:14:42 +00001020 hpet->number = 0;
Patrick Georgi9aeb6942012-10-05 21:54:38 +02001021 hpet->min_tick = CONFIG_HPET_MIN_TICKS;
Stefan Reinauer14e22772010-04-27 06:56:47 +00001022
Uwe Hermann622824c2010-11-19 15:14:42 +00001023 header->checksum = acpi_checksum((void *)hpet, sizeof(acpi_hpet_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +00001024}
Uwe Hermann622824c2010-11-19 15:14:42 +00001025
Rocky Phaguraeff07132021-01-10 15:42:50 -08001026/*
1027 * This method adds the ACPI error injection capability. It fills the default information.
1028 * HW dependent code (caller) can modify the defaults upon return. If no changes are necessary
1029 * and the defaults are acceptable then caller can simply add the table (acpi_add_table).
1030 * INPUTS:
1031 * einj - ptr to the starting location of EINJ table
1032 * actions - number of actions to trigger an error (HW dependent)
1033 * addr - address of trigger action table. This should be ACPI reserved memory and it will be
1034 * shared between OS and FW.
1035 */
1036void acpi_create_einj(acpi_einj_t *einj, uintptr_t addr, u8 actions)
1037{
1038 int i;
1039 acpi_header_t *header = &(einj->header);
1040 acpi_injection_header_t *inj_header = &(einj->inj_header);
1041 acpi_einj_smi_t *einj_smi = (acpi_einj_smi_t *)addr;
1042 acpi_einj_trigger_table_t *tat;
1043 if (!header)
1044 return;
1045
1046 printk(BIOS_DEBUG, "%s einj_smi = %p\n", __func__, einj_smi);
1047 memset(einj_smi, 0, sizeof(acpi_einj_smi_t));
Jonathan Zhangd5d9b282022-11-07 17:30:14 -08001048 tat = (acpi_einj_trigger_table_t *)((uint8_t *)einj_smi + sizeof(acpi_einj_smi_t));
Rocky Phaguraeff07132021-01-10 15:42:50 -08001049 tat->header_size = 16;
1050 tat->revision = 0;
1051 tat->table_size = sizeof(acpi_einj_trigger_table_t) +
1052 sizeof(acpi_einj_action_table_t) * actions - 1;
1053 tat->entry_count = actions;
1054 printk(BIOS_DEBUG, "%s trigger_action_table = %p\n", __func__, tat);
1055
1056 for (i = 0; i < actions; i++) {
1057 tat->trigger_action[i].action = TRIGGER_ERROR;
1058 tat->trigger_action[i].instruction = NO_OP;
1059 tat->trigger_action[i].flags = FLAG_IGNORE;
1060 tat->trigger_action[i].reg.space_id = ACPI_ADDRESS_SPACE_MEMORY;
1061 tat->trigger_action[i].reg.bit_width = 64;
1062 tat->trigger_action[i].reg.bit_offset = 0;
1063 tat->trigger_action[i].reg.access_size = ACPI_ACCESS_SIZE_QWORD_ACCESS;
1064 tat->trigger_action[i].reg.addr = 0;
1065 tat->trigger_action[i].value = 0;
1066 tat->trigger_action[i].mask = 0xFFFFFFFF;
1067 }
1068
1069 acpi_einj_action_table_t default_actions[ACTION_COUNT] = {
1070 [0] = {
1071 .action = BEGIN_INJECT_OP,
1072 .instruction = WRITE_REGISTER_VALUE,
1073 .flags = FLAG_PRESERVE,
1074 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->op_state),
1075 .value = 0,
1076 .mask = 0xFFFFFFFF
1077 },
1078 [1] = {
1079 .action = GET_TRIGGER_ACTION_TABLE,
1080 .instruction = READ_REGISTER,
1081 .flags = FLAG_IGNORE,
1082 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->trigger_action_table),
1083 .value = 0,
1084 .mask = 0xFFFFFFFFFFFFFFFF
1085 },
1086 [2] = {
1087 .action = SET_ERROR_TYPE,
1088 .instruction = WRITE_REGISTER,
1089 .flags = FLAG_PRESERVE,
1090 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->err_inject[0]),
1091 .value = 0,
1092 .mask = 0xFFFFFFFF
1093 },
1094 [3] = {
1095 .action = GET_ERROR_TYPE,
1096 .instruction = READ_REGISTER,
1097 .flags = FLAG_IGNORE,
1098 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->err_inj_cap),
1099 .value = 0,
1100 .mask = 0xFFFFFFFF
1101 },
1102 [4] = {
1103 .action = END_INJECT_OP,
1104 .instruction = WRITE_REGISTER_VALUE,
1105 .flags = FLAG_PRESERVE,
1106 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->op_state),
1107 .value = 0,
1108 .mask = 0xFFFFFFFF
1109
1110 },
1111 [5] = {
1112 .action = EXECUTE_INJECT_OP,
1113 .instruction = WRITE_REGISTER_VALUE,
1114 .flags = FLAG_PRESERVE,
1115 .reg = EINJ_REG_IO(),
1116 .value = 0x9a,
1117 .mask = 0xFFFF,
1118 },
1119 [6] = {
1120 .action = CHECK_BUSY_STATUS,
1121 .instruction = READ_REGISTER_VALUE,
1122 .flags = FLAG_IGNORE,
1123 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->op_status),
1124 .value = 1,
1125 .mask = 1,
1126 },
1127 [7] = {
1128 .action = GET_CMD_STATUS,
1129 .instruction = READ_REGISTER,
1130 .flags = FLAG_PRESERVE,
1131 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->cmd_sts),
1132 .value = 0,
1133 .mask = 0x1fe,
1134 },
1135 [8] = {
1136 .action = SET_ERROR_TYPE_WITH_ADDRESS,
1137 .instruction = WRITE_REGISTER,
1138 .flags = FLAG_PRESERVE,
1139 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->setaddrtable),
1140 .value = 1,
1141 .mask = 0xffffffff
1142 }
1143 };
1144
1145 einj_smi->err_inj_cap = ACPI_EINJ_DEFAULT_CAP;
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001146 einj_smi->trigger_action_table = (u64)(uintptr_t)tat;
Rocky Phaguraeff07132021-01-10 15:42:50 -08001147
1148 for (i = 0; i < ACTION_COUNT; i++)
1149 printk(BIOS_DEBUG, "default_actions[%d].reg.addr is %llx\n", i,
1150 default_actions[i].reg.addr);
1151
Rocky Phagurab46a9e52021-05-21 13:34:03 -07001152 memset((void *)einj, 0, sizeof(*einj));
Rocky Phaguraeff07132021-01-10 15:42:50 -08001153
1154 /* Fill out header fields. */
1155 memcpy(header->signature, "EINJ", 4);
1156 memcpy(header->oem_id, OEM_ID, 6);
1157 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1158 memcpy(header->asl_compiler_id, ASLC, 4);
1159
1160 header->asl_compiler_revision = asl_revision;
1161 header->length = sizeof(acpi_einj_t);
1162 header->revision = 1;
1163 inj_header->einj_header_size = sizeof(acpi_injection_header_t);
1164 inj_header->entry_count = ACTION_COUNT;
1165
1166 printk(BIOS_DEBUG, "%s einj->action_table = %p\n",
1167 __func__, einj->action_table);
1168 memcpy((void *)einj->action_table, (void *)default_actions, sizeof(einj->action_table));
Rocky Phagurab46a9e52021-05-21 13:34:03 -07001169 header->checksum = acpi_checksum((void *)einj, sizeof(*einj));
Rocky Phaguraeff07132021-01-10 15:42:50 -08001170}
1171
Furquan Shaikh0f007d82020-04-24 06:41:18 -07001172void acpi_create_vfct(const struct device *device,
Himanshu Sahdev7f1da072019-10-21 15:27:19 +05301173 acpi_vfct_t *vfct,
Furquan Shaikh0f007d82020-04-24 06:41:18 -07001174 unsigned long (*acpi_fill_vfct)(const struct device *device,
Himanshu Sahdev7f1da072019-10-21 15:27:19 +05301175 acpi_vfct_t *vfct_struct, unsigned long current))
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +02001176{
1177 acpi_header_t *header = &(vfct->header);
Himanshu Sahdev7f1da072019-10-21 15:27:19 +05301178 unsigned long current = (unsigned long)vfct + sizeof(acpi_vfct_t);
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +02001179
Himanshu Sahdev7f1da072019-10-21 15:27:19 +05301180 memset((void *)vfct, 0, sizeof(acpi_vfct_t));
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +02001181
John Zhao2ba303e2019-05-28 16:48:14 -07001182 if (!header)
1183 return;
1184
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +02001185 /* Fill out header fields. */
1186 memcpy(header->signature, "VFCT", 4);
1187 memcpy(header->oem_id, OEM_ID, 6);
1188 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1189 memcpy(header->asl_compiler_id, ASLC, 4);
1190
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001191 header->asl_compiler_revision = asl_revision;
Marc Jones93a51762018-08-22 18:57:24 -06001192 header->revision = get_acpi_table_revision(VFCT);
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +02001193
1194 current = acpi_fill_vfct(device, vfct, current);
1195
Kyösti Mälkkifb493792019-06-30 06:29:52 +03001196 /* If no BIOS image, return with header->length == 0. */
1197 if (!vfct->VBIOSImageOffset)
1198 return;
1199
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +02001200 /* (Re)calculate length and checksum. */
1201 header->length = current - (unsigned long)vfct;
1202 header->checksum = acpi_checksum((void *)vfct, header->length);
1203}
1204
Furquan Shaikh0f007d82020-04-24 06:41:18 -07001205void acpi_create_ipmi(const struct device *device,
Patrick Rudolph9d98e5a2019-06-14 19:00:04 +02001206 struct acpi_spmi *spmi,
1207 const u16 ipmi_revision,
1208 const acpi_addr_t *addr,
1209 const enum acpi_ipmi_interface_type type,
1210 const s8 gpe_interrupt,
1211 const u32 apic_interrupt,
1212 const u32 uid)
1213{
1214 acpi_header_t *header = &(spmi->header);
1215 memset((void *)spmi, 0, sizeof(struct acpi_spmi));
1216
1217 /* Fill out header fields. */
1218 memcpy(header->signature, "SPMI", 4);
1219 memcpy(header->oem_id, OEM_ID, 6);
1220 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1221 memcpy(header->asl_compiler_id, ASLC, 4);
1222
1223 header->asl_compiler_revision = asl_revision;
1224 header->length = sizeof(struct acpi_spmi);
1225 header->revision = get_acpi_table_revision(SPMI);
1226
1227 spmi->reserved = 1;
1228
1229 if (device->path.type == DEVICE_PATH_PCI) {
1230 spmi->pci_device_flag = ACPI_IPMI_PCI_DEVICE_FLAG;
1231 spmi->pci_bus = device->bus->secondary;
1232 spmi->pci_device = device->path.pci.devfn >> 3;
1233 spmi->pci_function = device->path.pci.devfn & 0x7;
1234 } else if (type != IPMI_INTERFACE_SSIF) {
1235 memcpy(spmi->uid, &uid, sizeof(spmi->uid));
1236 }
1237
1238 spmi->base_address = *addr;
1239 spmi->specification_revision = ipmi_revision;
1240
1241 spmi->interface_type = type;
1242
1243 if (gpe_interrupt >= 0 && gpe_interrupt < 32) {
1244 spmi->gpe = gpe_interrupt;
1245 spmi->interrupt_type |= ACPI_IPMI_INT_TYPE_SCI;
1246 }
1247 if (apic_interrupt > 0) {
1248 spmi->global_system_interrupt = apic_interrupt;
1249 spmi->interrupt_type |= ACPI_IPMI_INT_TYPE_APIC;
1250 }
1251
1252 /* Calculate checksum. */
1253 header->checksum = acpi_checksum((void *)spmi, header->length);
1254}
1255
Timothy Pearsonff8ccf02015-08-11 17:48:32 -05001256void acpi_create_ivrs(acpi_ivrs_t *ivrs,
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001257 unsigned long (*acpi_fill_ivrs)(acpi_ivrs_t *ivrs_struct,
1258 unsigned long current))
Timothy Pearsonff8ccf02015-08-11 17:48:32 -05001259{
1260 acpi_header_t *header = &(ivrs->header);
1261 unsigned long current = (unsigned long)ivrs + sizeof(acpi_ivrs_t);
1262
1263 memset((void *)ivrs, 0, sizeof(acpi_ivrs_t));
1264
John Zhao2ba303e2019-05-28 16:48:14 -07001265 if (!header)
1266 return;
1267
Timothy Pearsonff8ccf02015-08-11 17:48:32 -05001268 /* Fill out header fields. */
1269 memcpy(header->signature, "IVRS", 4);
1270 memcpy(header->oem_id, OEM_ID, 6);
1271 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1272 memcpy(header->asl_compiler_id, ASLC, 4);
1273
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001274 header->asl_compiler_revision = asl_revision;
Timothy Pearsonff8ccf02015-08-11 17:48:32 -05001275 header->length = sizeof(acpi_ivrs_t);
Marc Jones93a51762018-08-22 18:57:24 -06001276 header->revision = get_acpi_table_revision(IVRS);
Timothy Pearsonff8ccf02015-08-11 17:48:32 -05001277
1278 current = acpi_fill_ivrs(ivrs, current);
1279
1280 /* (Re)calculate length and checksum. */
1281 header->length = current - (unsigned long)ivrs;
1282 header->checksum = acpi_checksum((void *)ivrs, header->length);
1283}
1284
Jason Glenesk61624b22020-11-02 20:06:23 -08001285void acpi_create_crat(struct acpi_crat_header *crat,
1286 unsigned long (*acpi_fill_crat)(struct acpi_crat_header *crat_struct,
1287 unsigned long current))
1288{
1289 acpi_header_t *header = &(crat->header);
1290 unsigned long current = (unsigned long)crat + sizeof(struct acpi_crat_header);
1291
1292 memset((void *)crat, 0, sizeof(struct acpi_crat_header));
1293
1294 if (!header)
1295 return;
1296
1297 /* Fill out header fields. */
1298 memcpy(header->signature, "CRAT", 4);
1299 memcpy(header->oem_id, OEM_ID, 6);
1300 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1301 memcpy(header->asl_compiler_id, ASLC, 4);
1302
1303 header->asl_compiler_revision = asl_revision;
1304 header->length = sizeof(struct acpi_crat_header);
1305 header->revision = get_acpi_table_revision(CRAT);
1306
1307 current = acpi_fill_crat(crat, current);
1308
1309 /* (Re)calculate length and checksum. */
1310 header->length = current - (unsigned long)crat;
1311 header->checksum = acpi_checksum((void *)crat, header->length);
1312}
1313
Furquan Shaikh0f007d82020-04-24 06:41:18 -07001314unsigned long acpi_write_hpet(const struct device *device, unsigned long current,
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001315 acpi_rsdp_t *rsdp)
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001316{
1317 acpi_hpet_t *hpet;
1318
1319 /*
1320 * We explicitly add these tables later on:
1321 */
1322 printk(BIOS_DEBUG, "ACPI: * HPET\n");
1323
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001324 hpet = (acpi_hpet_t *)current;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001325 current += sizeof(acpi_hpet_t);
Felix Heldc4697122019-06-20 13:50:17 +02001326 current = ALIGN_UP(current, 16);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001327 acpi_create_hpet(hpet);
1328 acpi_add_table(rsdp, hpet);
1329
1330 return current;
1331}
1332
Duncan Lauriee4a36c72017-11-11 19:33:25 -08001333void acpi_create_dbg2(acpi_dbg2_header_t *dbg2,
1334 int port_type, int port_subtype,
1335 acpi_addr_t *address, uint32_t address_size,
1336 const char *device_path)
1337{
1338 uintptr_t current;
1339 acpi_dbg2_device_t *device;
1340 uint32_t *dbg2_addr_size;
1341 acpi_header_t *header;
1342 size_t path_len;
1343 const char *path;
1344 char *namespace;
1345
1346 /* Fill out header fields. */
1347 current = (uintptr_t)dbg2;
1348 memset(dbg2, 0, sizeof(acpi_dbg2_header_t));
1349 header = &(dbg2->header);
John Zhao2ba303e2019-05-28 16:48:14 -07001350
1351 if (!header)
1352 return;
1353
Marc Jones93a51762018-08-22 18:57:24 -06001354 header->revision = get_acpi_table_revision(DBG2);
Duncan Lauriee4a36c72017-11-11 19:33:25 -08001355 memcpy(header->signature, "DBG2", 4);
1356 memcpy(header->oem_id, OEM_ID, 6);
1357 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1358 memcpy(header->asl_compiler_id, ASLC, 4);
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001359 header->asl_compiler_revision = asl_revision;
Duncan Lauriee4a36c72017-11-11 19:33:25 -08001360
1361 /* One debug device defined */
1362 dbg2->devices_offset = sizeof(acpi_dbg2_header_t);
1363 dbg2->devices_count = 1;
1364 current += sizeof(acpi_dbg2_header_t);
1365
1366 /* Device comes after the header */
1367 device = (acpi_dbg2_device_t *)current;
1368 memset(device, 0, sizeof(acpi_dbg2_device_t));
1369 current += sizeof(acpi_dbg2_device_t);
1370
1371 device->revision = 0;
1372 device->address_count = 1;
1373 device->port_type = port_type;
1374 device->port_subtype = port_subtype;
1375
1376 /* Base Address comes after device structure */
1377 memcpy((void *)current, address, sizeof(acpi_addr_t));
1378 device->base_address_offset = current - (uintptr_t)device;
1379 current += sizeof(acpi_addr_t);
1380
1381 /* Address Size comes after address structure */
1382 dbg2_addr_size = (uint32_t *)current;
1383 device->address_size_offset = current - (uintptr_t)device;
1384 *dbg2_addr_size = address_size;
1385 current += sizeof(uint32_t);
1386
1387 /* Namespace string comes last, use '.' if not provided */
1388 path = device_path ? : ".";
1389 /* Namespace string length includes NULL terminator */
1390 path_len = strlen(path) + 1;
1391 namespace = (char *)current;
1392 device->namespace_string_length = path_len;
1393 device->namespace_string_offset = current - (uintptr_t)device;
1394 strncpy(namespace, path, path_len);
1395 current += path_len;
1396
1397 /* Update structure lengths and checksum */
1398 device->length = current - (uintptr_t)device;
1399 header->length = current - (uintptr_t)dbg2;
1400 header->checksum = acpi_checksum((uint8_t *)dbg2, header->length);
1401}
1402
1403unsigned long acpi_write_dbg2_pci_uart(acpi_rsdp_t *rsdp, unsigned long current,
Aamir Bohrae825d3f2019-07-30 10:11:41 +05301404 const struct device *dev, uint8_t access_size)
Duncan Lauriee4a36c72017-11-11 19:33:25 -08001405{
1406 acpi_dbg2_header_t *dbg2 = (acpi_dbg2_header_t *)current;
1407 struct resource *res;
1408 acpi_addr_t address;
1409
1410 if (!dev) {
Wim Vervoorn6cd52432020-02-03 14:41:46 +01001411 printk(BIOS_DEBUG, "%s: Device not found\n", __func__);
Duncan Lauriee4a36c72017-11-11 19:33:25 -08001412 return current;
1413 }
Mario Scheithauerf1eb0ea2017-11-21 13:52:53 +01001414 if (!dev->enabled) {
1415 printk(BIOS_INFO, "%s: Device not enabled\n", __func__);
1416 return current;
1417 }
Angel Pons32d09be2021-11-03 13:27:45 +01001418 res = probe_resource(dev, PCI_BASE_ADDRESS_0);
Duncan Lauriee4a36c72017-11-11 19:33:25 -08001419 if (!res) {
1420 printk(BIOS_ERR, "%s: Unable to find resource for %s\n",
1421 __func__, dev_path(dev));
1422 return current;
1423 }
1424
1425 memset(&address, 0, sizeof(address));
1426 if (res->flags & IORESOURCE_IO)
1427 address.space_id = ACPI_ADDRESS_SPACE_IO;
1428 else if (res->flags & IORESOURCE_MEM)
1429 address.space_id = ACPI_ADDRESS_SPACE_MEMORY;
1430 else {
1431 printk(BIOS_ERR, "%s: Unknown address space type\n", __func__);
1432 return current;
1433 }
1434
1435 address.addrl = (uint32_t)res->base;
1436 address.addrh = (uint32_t)((res->base >> 32) & 0xffffffff);
1437 address.access_size = access_size;
1438
1439 acpi_create_dbg2(dbg2,
1440 ACPI_DBG2_PORT_SERIAL,
1441 ACPI_DBG2_PORT_SERIAL_16550,
1442 &address, res->size,
1443 acpi_device_path(dev));
1444
1445 if (dbg2->header.length) {
1446 current += dbg2->header.length;
1447 current = acpi_align_current(current);
1448 acpi_add_table(rsdp, dbg2);
1449 }
1450
1451 return current;
1452}
1453
Stefan Reinauer777224c2005-01-19 14:06:41 +00001454void acpi_create_facs(acpi_facs_t *facs)
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +00001455{
Uwe Hermann622824c2010-11-19 15:14:42 +00001456 memset((void *)facs, 0, sizeof(acpi_facs_t));
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +00001457
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001458 memcpy(facs->signature, "FACS", 4);
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +00001459 facs->length = sizeof(acpi_facs_t);
1460 facs->hardware_signature = 0;
1461 facs->firmware_waking_vector = 0;
1462 facs->global_lock = 0;
1463 facs->flags = 0;
1464 facs->x_firmware_waking_vector_l = 0;
1465 facs->x_firmware_waking_vector_h = 0;
Marc Jones93a51762018-08-22 18:57:24 -06001466 facs->version = get_acpi_table_revision(FACS);
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +00001467}
Stefan Reinauer777224c2005-01-19 14:06:41 +00001468
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001469static void acpi_write_rsdt(acpi_rsdt_t *rsdt, char *oem_id, char *oem_table_id)
Stefan Reinauer14e22772010-04-27 06:56:47 +00001470{
Uwe Hermann622824c2010-11-19 15:14:42 +00001471 acpi_header_t *header = &(rsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001472
John Zhao2ba303e2019-05-28 16:48:14 -07001473 if (!header)
1474 return;
1475
Uwe Hermann622824c2010-11-19 15:14:42 +00001476 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001477 memcpy(header->signature, "RSDT", 4);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001478 memcpy(header->oem_id, oem_id, 6);
1479 memcpy(header->oem_table_id, oem_table_id, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +00001480 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001481
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001482 header->asl_compiler_revision = asl_revision;
Stefan Reinauer688b3852004-01-28 16:56:14 +00001483 header->length = sizeof(acpi_rsdt_t);
Marc Jones93a51762018-08-22 18:57:24 -06001484 header->revision = get_acpi_table_revision(RSDT);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001485
Uwe Hermann622824c2010-11-19 15:14:42 +00001486 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauer688b3852004-01-28 16:56:14 +00001487
Uwe Hermann622824c2010-11-19 15:14:42 +00001488 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001489 header->checksum = acpi_checksum((void *)rsdt, sizeof(acpi_rsdt_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +00001490}
1491
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001492static void acpi_write_xsdt(acpi_xsdt_t *xsdt, char *oem_id, char *oem_table_id)
Stefan Reinauer14e22772010-04-27 06:56:47 +00001493{
Uwe Hermann622824c2010-11-19 15:14:42 +00001494 acpi_header_t *header = &(xsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001495
John Zhao2ba303e2019-05-28 16:48:14 -07001496 if (!header)
1497 return;
1498
Uwe Hermann622824c2010-11-19 15:14:42 +00001499 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001500 memcpy(header->signature, "XSDT", 4);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001501 memcpy(header->oem_id, oem_id, 6);
1502 memcpy(header->oem_table_id, oem_table_id, 8);
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001503 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001504
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001505 header->asl_compiler_revision = asl_revision;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001506 header->length = sizeof(acpi_xsdt_t);
Marc Jones93a51762018-08-22 18:57:24 -06001507 header->revision = get_acpi_table_revision(XSDT);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001508
Uwe Hermann622824c2010-11-19 15:14:42 +00001509 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001510
Uwe Hermann622824c2010-11-19 15:14:42 +00001511 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001512 header->checksum = acpi_checksum((void *)xsdt, sizeof(acpi_xsdt_t));
1513}
1514
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001515static void acpi_write_rsdp(acpi_rsdp_t *rsdp, acpi_rsdt_t *rsdt,
1516 acpi_xsdt_t *xsdt, char *oem_id)
Stefan Reinauer688b3852004-01-28 16:56:14 +00001517{
Stefan Reinauerd18faac2009-11-05 18:06:43 +00001518 memset(rsdp, 0, sizeof(acpi_rsdp_t));
Uwe Hermann622824c2010-11-19 15:14:42 +00001519
Stefan Reinauer688b3852004-01-28 16:56:14 +00001520 memcpy(rsdp->signature, RSDP_SIG, 8);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001521 memcpy(rsdp->oem_id, oem_id, 6);
Uwe Hermann622824c2010-11-19 15:14:42 +00001522
1523 rsdp->length = sizeof(acpi_rsdp_t);
Stefan Reinauerdefee172015-06-18 01:11:19 -07001524 rsdp->rsdt_address = (uintptr_t)rsdt;
Uwe Hermann622824c2010-11-19 15:14:42 +00001525
1526 /*
1527 * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2.
1528 *
1529 * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
1530 * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
1531 * revision 0).
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001532 */
1533 if (xsdt == NULL) {
Uwe Hermann622824c2010-11-19 15:14:42 +00001534 rsdp->revision = 0;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001535 } else {
Stefan Reinauerdefee172015-06-18 01:11:19 -07001536 rsdp->xsdt_address = (u64)(uintptr_t)xsdt;
Marc Jones93a51762018-08-22 18:57:24 -06001537 rsdp->revision = get_acpi_table_revision(RSDP);
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001538 }
Uwe Hermann622824c2010-11-19 15:14:42 +00001539
1540 /* Calculate checksums. */
1541 rsdp->checksum = acpi_checksum((void *)rsdp, 20);
1542 rsdp->ext_checksum = acpi_checksum((void *)rsdp, sizeof(acpi_rsdp_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +00001543}
1544
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001545unsigned long acpi_create_hest_error_source(acpi_hest_t *hest,
1546 acpi_hest_esd_t *esd, u16 type, void *data, u16 data_len)
zbaocaf494c82012-04-13 13:57:14 +08001547{
1548 acpi_header_t *header = &(hest->header);
1549 acpi_hest_hen_t *hen;
1550 void *pos;
1551 u16 len;
1552
1553 pos = esd;
1554 memset(pos, 0, sizeof(acpi_hest_esd_t));
1555 len = 0;
1556 esd->type = type; /* MCE */
1557 esd->source_id = hest->error_source_count;
1558 esd->flags = 0; /* FIRMWARE_FIRST */
1559 esd->enabled = 1;
1560 esd->prealloc_erecords = 1;
1561 esd->max_section_per_record = 0x1;
1562
1563 len += sizeof(acpi_hest_esd_t);
1564 pos = esd + 1;
1565
1566 switch (type) {
1567 case 0: /* MCE */
1568 break;
1569 case 1: /* CMC */
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001570 hen = (acpi_hest_hen_t *)(pos);
zbaocaf494c82012-04-13 13:57:14 +08001571 memset(pos, 0, sizeof(acpi_hest_hen_t));
1572 hen->type = 3; /* SCI? */
1573 hen->length = sizeof(acpi_hest_hen_t);
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001574 hen->conf_we = 0; /* Configuration Write Enable. */
zbaocaf494c82012-04-13 13:57:14 +08001575 hen->poll_interval = 0;
1576 hen->vector = 0;
1577 hen->sw2poll_threshold_val = 0;
1578 hen->sw2poll_threshold_win = 0;
1579 hen->error_threshold_val = 0;
1580 hen->error_threshold_win = 0;
1581 len += sizeof(acpi_hest_hen_t);
1582 pos = hen + 1;
1583 break;
1584 case 2: /* NMI */
1585 case 6: /* AER Root Port */
1586 case 7: /* AER Endpoint */
1587 case 8: /* AER Bridge */
1588 case 9: /* Generic Hardware Error Source. */
1589 /* TODO: */
1590 break;
1591 default:
1592 printk(BIOS_DEBUG, "Invalid type of Error Source.");
1593 break;
1594 }
Lee Leahy024b13d2017-03-16 13:41:11 -07001595 hest->error_source_count++;
zbaocaf494c82012-04-13 13:57:14 +08001596
1597 memcpy(pos, data, data_len);
1598 len += data_len;
John Zhao2ba303e2019-05-28 16:48:14 -07001599 if (header)
1600 header->length += len;
zbaocaf494c82012-04-13 13:57:14 +08001601
1602 return len;
1603}
1604
1605/* ACPI 4.0 */
Vladimir Serbinenko807127f2014-11-09 13:36:18 +01001606void acpi_write_hest(acpi_hest_t *hest,
1607 unsigned long (*acpi_fill_hest)(acpi_hest_t *hest))
zbaocaf494c82012-04-13 13:57:14 +08001608{
1609 acpi_header_t *header = &(hest->header);
1610
1611 memset(hest, 0, sizeof(acpi_hest_t));
1612
John Zhao2ba303e2019-05-28 16:48:14 -07001613 if (!header)
1614 return;
1615
zbaocaf494c82012-04-13 13:57:14 +08001616 memcpy(header->signature, "HEST", 4);
1617 memcpy(header->oem_id, OEM_ID, 6);
1618 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1619 memcpy(header->asl_compiler_id, ASLC, 4);
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001620 header->asl_compiler_revision = asl_revision;
zbaocaf494c82012-04-13 13:57:14 +08001621 header->length += sizeof(acpi_hest_t);
Marc Jones93a51762018-08-22 18:57:24 -06001622 header->revision = get_acpi_table_revision(HEST);
zbaocaf494c82012-04-13 13:57:14 +08001623
1624 acpi_fill_hest(hest);
1625
1626 /* Calculate checksums. */
1627 header->checksum = acpi_checksum((void *)hest, header->length);
1628}
1629
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001630/* ACPI 3.0b */
1631void acpi_write_bert(acpi_bert_t *bert, uintptr_t region, size_t length)
1632{
1633 acpi_header_t *header = &(bert->header);
1634
1635 memset(bert, 0, sizeof(acpi_bert_t));
1636
John Zhao2ba303e2019-05-28 16:48:14 -07001637 if (!header)
1638 return;
1639
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001640 memcpy(header->signature, "BERT", 4);
1641 memcpy(header->oem_id, OEM_ID, 6);
1642 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1643 memcpy(header->asl_compiler_id, ASLC, 4);
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001644 header->asl_compiler_revision = asl_revision;
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001645 header->length += sizeof(acpi_bert_t);
1646 header->revision = get_acpi_table_revision(BERT);
1647
1648 bert->error_region = region;
1649 bert->region_length = length;
1650
1651 /* Calculate checksums. */
1652 header->checksum = acpi_checksum((void *)bert, header->length);
1653}
1654
Angel Pons79572e42020-07-13 00:17:43 +02001655__weak void arch_fill_fadt(acpi_fadt_t *fadt) { }
Kyösti Mälkkif9aac922020-05-30 16:16:28 +03001656__weak void soc_fill_fadt(acpi_fadt_t *fadt) { }
Kyösti Mälkki02fd15d2020-06-02 03:34:43 +03001657__weak void mainboard_fill_fadt(acpi_fadt_t *fadt) { }
Kyösti Mälkkif9aac922020-05-30 16:16:28 +03001658
Lee Leahy024b13d2017-03-16 13:41:11 -07001659void acpi_create_fadt(acpi_fadt_t *fadt, acpi_facs_t *facs, void *dsdt)
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001660{
1661 acpi_header_t *header = &(fadt->header);
1662
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001663 memset((void *)fadt, 0, sizeof(acpi_fadt_t));
John Zhao2ba303e2019-05-28 16:48:14 -07001664
1665 if (!header)
1666 return;
1667
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001668 memcpy(header->signature, "FACP", 4);
1669 header->length = sizeof(acpi_fadt_t);
Marc Jones93a51762018-08-22 18:57:24 -06001670 header->revision = get_acpi_table_revision(FADT);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001671 memcpy(header->oem_id, OEM_ID, 6);
1672 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1673 memcpy(header->asl_compiler_id, ASLC, 4);
Elyes HAOUAS26071aa2019-02-15 08:21:33 +01001674 header->asl_compiler_revision = asl_revision;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001675
Elyes Haouas532e0432022-02-21 18:13:58 +01001676 fadt->FADT_MinorVersion = get_acpi_fadt_minor_version();
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001677 fadt->firmware_ctrl = (unsigned long)facs;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001678 fadt->x_firmware_ctl_l = (unsigned long)facs;
1679 fadt->x_firmware_ctl_h = 0;
Kyösti Mälkkid4acee82020-06-03 07:20:55 +03001680
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001681 fadt->dsdt = (unsigned long)dsdt;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001682 fadt->x_dsdt_l = (unsigned long)dsdt;
1683 fadt->x_dsdt_h = 0;
1684
Kyösti Mälkkid4acee82020-06-03 07:20:55 +03001685 /* should be 0 ACPI 3.0 */
1686 fadt->reserved = 0;
1687
Kyösti Mälkkie0d38682020-06-07 12:01:58 +03001688 fadt->preferred_pm_profile = acpi_get_preferred_pm_profile();
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001689
Nico Huberd5811372021-07-12 18:11:58 +02001690 if (CONFIG(USE_PC_CMOS_ALTCENTURY))
1691 fadt->century = RTC_CLK_ALTCENTURY;
1692
Angel Pons79572e42020-07-13 00:17:43 +02001693 arch_fill_fadt(fadt);
1694
Vladimir Serbinenkoab83ef02014-10-25 15:18:25 +02001695 acpi_fill_fadt(fadt);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001696
Kyösti Mälkkif9aac922020-05-30 16:16:28 +03001697 soc_fill_fadt(fadt);
Kyösti Mälkki02fd15d2020-06-02 03:34:43 +03001698 mainboard_fill_fadt(fadt);
Kyösti Mälkkif9aac922020-05-30 16:16:28 +03001699
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001700 header->checksum =
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001701 acpi_checksum((void *)fadt, header->length);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001702}
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001703
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001704void acpi_create_lpit(acpi_lpit_t *lpit)
1705{
1706 acpi_header_t *header = &(lpit->header);
1707 unsigned long current = (unsigned long)lpit + sizeof(acpi_lpit_t);
1708
1709 memset((void *)lpit, 0, sizeof(acpi_lpit_t));
1710
1711 if (!header)
1712 return;
1713
1714 /* Fill out header fields. */
1715 memcpy(header->signature, "LPIT", 4);
1716 memcpy(header->oem_id, OEM_ID, 6);
1717 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1718 memcpy(header->asl_compiler_id, ASLC, 4);
1719
1720 header->asl_compiler_revision = asl_revision;
1721 header->revision = get_acpi_table_revision(LPIT);
1722 header->oem_revision = 42;
1723 header->length = sizeof(acpi_lpit_t);
1724
1725 current = acpi_fill_lpit(current);
1726
1727 /* (Re)calculate length and checksum. */
1728 header->length = current - (unsigned long)lpit;
1729 header->checksum = acpi_checksum((void *)lpit, header->length);
1730}
1731
1732unsigned long acpi_create_lpi_desc_ncst(acpi_lpi_desc_ncst_t *lpi_desc, uint16_t uid)
1733{
1734 memset(lpi_desc, 0, sizeof(acpi_lpi_desc_ncst_t));
1735 lpi_desc->header.length = sizeof(acpi_lpi_desc_ncst_t);
1736 lpi_desc->header.type = ACPI_LPI_DESC_TYPE_NATIVE_CSTATE;
1737 lpi_desc->header.uid = uid;
1738
1739 return lpi_desc->header.length;
1740}
1741
Aaron Durbin64031672018-04-21 14:45:32 -06001742unsigned long __weak fw_cfg_acpi_tables(unsigned long start)
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001743{
1744 return 0;
1745}
1746
Raul E Rangel6b446b92021-11-19 11:38:35 -07001747void preload_acpi_dsdt(void)
1748{
1749 const char *file = CONFIG_CBFS_PREFIX "/dsdt.aml";
1750
1751 if (!CONFIG(CBFS_PRELOAD))
1752 return;
1753
1754 printk(BIOS_DEBUG, "Preloading %s\n", file);
1755 cbfs_preload(file);
1756}
1757
Arthur Heymans2e7e2d92022-03-03 22:28:27 +01001758static uintptr_t coreboot_rsdp;
1759
1760uintptr_t get_coreboot_rsdp(void)
1761{
1762 return coreboot_rsdp;
1763}
1764
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001765unsigned long write_acpi_tables(unsigned long start)
1766{
1767 unsigned long current;
1768 acpi_rsdp_t *rsdp;
1769 acpi_rsdt_t *rsdt;
1770 acpi_xsdt_t *xsdt;
1771 acpi_fadt_t *fadt;
1772 acpi_facs_t *facs;
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001773 acpi_header_t *slic_file, *slic;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001774 acpi_header_t *ssdt;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001775 acpi_header_t *dsdt_file, *dsdt;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001776 acpi_mcfg_t *mcfg;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +02001777 acpi_tcpa_t *tcpa;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001778 acpi_tpm2_t *tpm2;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001779 acpi_madt_t *madt;
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001780 acpi_lpit_t *lpit;
Francois Toguo522e0db2021-01-21 09:55:19 -08001781 acpi_bert_t *bert;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +11001782 struct device *dev;
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001783 unsigned long fw;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001784 size_t slic_size, dsdt_size;
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001785 char oem_id[6], oem_table_id[8];
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001786
1787 current = start;
1788
1789 /* Align ACPI tables to 16byte */
Aaron Durbin07a1b282015-12-10 17:07:38 -06001790 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001791
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001792 /* Special case for qemu */
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001793 fw = fw_cfg_acpi_tables(current);
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001794 if (fw) {
1795 rsdp = NULL;
1796 /* Find RSDP. */
1797 for (void *p = (void *)current; p < (void *)fw; p += 16) {
1798 if (valid_rsdp((acpi_rsdp_t *)p)) {
1799 rsdp = p;
1800 break;
1801 }
1802 }
1803 if (!rsdp)
1804 return fw;
1805
1806 /* Add BOOT0000 for Linux google firmware driver */
1807 printk(BIOS_DEBUG, "ACPI: * SSDT\n");
1808 ssdt = (acpi_header_t *)fw;
1809 current = (unsigned long)ssdt + sizeof(acpi_header_t);
1810
1811 memset((void *)ssdt, 0, sizeof(acpi_header_t));
1812
1813 memcpy(&ssdt->signature, "SSDT", 4);
1814 ssdt->revision = get_acpi_table_revision(SSDT);
1815 memcpy(&ssdt->oem_id, OEM_ID, 6);
1816 memcpy(&ssdt->oem_table_id, oem_table_id, 8);
1817 ssdt->oem_revision = 42;
1818 memcpy(&ssdt->asl_compiler_id, ASLC, 4);
1819 ssdt->asl_compiler_revision = asl_revision;
1820 ssdt->length = sizeof(acpi_header_t);
1821
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001822 acpigen_set_current((char *)current);
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001823
1824 /* Write object to declare coreboot tables */
1825 acpi_ssdt_write_cbtable();
1826
1827 /* (Re)calculate length and checksum. */
1828 ssdt->length = current - (unsigned long)ssdt;
1829 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
1830
1831 acpi_create_ssdt_generator(ssdt, ACPI_TABLE_CREATOR);
1832
1833 acpi_add_table(rsdp, ssdt);
1834
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001835 return fw;
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001836 }
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001837
Julius Werner834b3ec2020-03-04 16:52:08 -08001838 dsdt_file = cbfs_map(CONFIG_CBFS_PREFIX "/dsdt.aml", &dsdt_size);
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001839 if (!dsdt_file) {
1840 printk(BIOS_ERR, "No DSDT file, skipping ACPI tables\n");
1841 return current;
1842 }
1843
1844 if (dsdt_file->length > dsdt_size
Elyes HAOUAS7d87e762016-10-03 22:11:07 +02001845 || dsdt_file->length < sizeof(acpi_header_t)
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001846 || memcmp(dsdt_file->signature, "DSDT", 4) != 0) {
1847 printk(BIOS_ERR, "Invalid DSDT file, skipping ACPI tables\n");
1848 return current;
1849 }
1850
Julius Werner834b3ec2020-03-04 16:52:08 -08001851 slic_file = cbfs_map(CONFIG_CBFS_PREFIX "/slic", &slic_size);
Vladimir Serbinenko2cb29782015-05-31 11:32:09 +02001852 if (slic_file
1853 && (slic_file->length > slic_size
Elyes HAOUAS7d87e762016-10-03 22:11:07 +02001854 || slic_file->length < sizeof(acpi_header_t)
Benjamin Doron2b2dc0c2020-09-12 02:55:57 +00001855 || (memcmp(slic_file->signature, "SLIC", 4) != 0
1856 && memcmp(slic_file->signature, "MSDM", 4) != 0))) {
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001857 slic_file = 0;
1858 }
1859
1860 if (slic_file) {
1861 memcpy(oem_id, slic_file->oem_id, 6);
1862 memcpy(oem_table_id, slic_file->oem_table_id, 8);
1863 } else {
1864 memcpy(oem_id, OEM_ID, 6);
1865 memcpy(oem_table_id, ACPI_TABLE_CREATOR, 8);
1866 }
1867
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001868 printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx.\n", start);
1869
1870 /* We need at least an RSDP and an RSDT Table */
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001871 rsdp = (acpi_rsdp_t *)current;
Arthur Heymans2e7e2d92022-03-03 22:28:27 +01001872 coreboot_rsdp = (uintptr_t)rsdp;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001873 current += sizeof(acpi_rsdp_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001874 current = acpi_align_current(current);
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001875 rsdt = (acpi_rsdt_t *)current;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001876 current += sizeof(acpi_rsdt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001877 current = acpi_align_current(current);
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001878 xsdt = (acpi_xsdt_t *)current;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001879 current += sizeof(acpi_xsdt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001880 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001881
1882 /* clear all table memory */
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001883 memset((void *)start, 0, current - start);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001884
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001885 acpi_write_rsdp(rsdp, rsdt, xsdt, oem_id);
1886 acpi_write_rsdt(rsdt, oem_id, oem_table_id);
1887 acpi_write_xsdt(xsdt, oem_id, oem_table_id);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001888
1889 printk(BIOS_DEBUG, "ACPI: * FACS\n");
Felix Heldc4697122019-06-20 13:50:17 +02001890 current = ALIGN_UP(current, 64);
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001891 facs = (acpi_facs_t *)current;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001892 current += sizeof(acpi_facs_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001893 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001894 acpi_create_facs(facs);
1895
1896 printk(BIOS_DEBUG, "ACPI: * DSDT\n");
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001897 dsdt = (acpi_header_t *)current;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001898 memcpy(dsdt, dsdt_file, sizeof(acpi_header_t));
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001899 if (dsdt->length >= sizeof(acpi_header_t)) {
1900 current += sizeof(acpi_header_t);
1901
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001902 acpigen_set_current((char *)current);
Kyösti Mälkki2ab4a962020-06-30 11:41:47 +03001903
Kyösti Mälkki37eb24b2021-01-11 18:40:37 +02001904 if (CONFIG(ACPI_SOC_NVS))
1905 acpi_fill_gnvs();
Kyösti Mälkki3dc17922021-03-16 19:01:48 +02001906 if (CONFIG(CHROMEOS_NVS))
1907 acpi_fill_cnvs();
Kyösti Mälkki2ab4a962020-06-30 11:41:47 +03001908
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001909 for (dev = all_devices; dev; dev = dev->next)
Nico Huber68680dd2020-03-31 17:34:52 +02001910 if (dev->ops && dev->ops->acpi_inject_dsdt)
1911 dev->ops->acpi_inject_dsdt(dev);
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001912 current = (unsigned long)acpigen_get_current();
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001913 memcpy((char *)current,
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001914 (char *)dsdt_file + sizeof(acpi_header_t),
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001915 dsdt->length - sizeof(acpi_header_t));
1916 current += dsdt->length - sizeof(acpi_header_t);
1917
1918 /* (Re)calculate length and checksum. */
1919 dsdt->length = current - (unsigned long)dsdt;
1920 dsdt->checksum = 0;
1921 dsdt->checksum = acpi_checksum((void *)dsdt, dsdt->length);
1922 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001923
Aaron Durbin07a1b282015-12-10 17:07:38 -06001924 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001925
1926 printk(BIOS_DEBUG, "ACPI: * FADT\n");
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001927 fadt = (acpi_fadt_t *)current;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001928 current += sizeof(acpi_fadt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001929 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001930
1931 acpi_create_fadt(fadt, facs, dsdt);
1932 acpi_add_table(rsdp, fadt);
1933
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001934 if (slic_file) {
1935 printk(BIOS_DEBUG, "ACPI: * SLIC\n");
1936 slic = (acpi_header_t *)current;
1937 memcpy(slic, slic_file, slic_file->length);
1938 current += slic_file->length;
Aaron Durbin07a1b282015-12-10 17:07:38 -06001939 current = acpi_align_current(current);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001940 acpi_add_table(rsdp, slic);
1941 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001942
1943 printk(BIOS_DEBUG, "ACPI: * SSDT\n");
1944 ssdt = (acpi_header_t *)current;
1945 acpi_create_ssdt_generator(ssdt, ACPI_TABLE_CREATOR);
Vladimir Serbinenko9310df82014-10-10 20:40:41 +02001946 if (ssdt->length > sizeof(acpi_header_t)) {
1947 current += ssdt->length;
1948 acpi_add_table(rsdp, ssdt);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001949 current = acpi_align_current(current);
Vladimir Serbinenko9310df82014-10-10 20:40:41 +02001950 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001951
1952 printk(BIOS_DEBUG, "ACPI: * MCFG\n");
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001953 mcfg = (acpi_mcfg_t *)current;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001954 acpi_create_mcfg(mcfg);
1955 if (mcfg->header.length > sizeof(acpi_mcfg_t)) {
1956 current += mcfg->header.length;
Aaron Durbin07a1b282015-12-10 17:07:38 -06001957 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001958 acpi_add_table(rsdp, mcfg);
1959 }
1960
Julius Wernercd49cce2019-03-05 16:53:33 -08001961 if (CONFIG(TPM1)) {
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001962 printk(BIOS_DEBUG, "ACPI: * TCPA\n");
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001963 tcpa = (acpi_tcpa_t *)current;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001964 acpi_create_tcpa(tcpa);
1965 if (tcpa->header.length >= sizeof(acpi_tcpa_t)) {
1966 current += tcpa->header.length;
1967 current = acpi_align_current(current);
1968 acpi_add_table(rsdp, tcpa);
1969 }
1970 }
1971
Julius Wernercd49cce2019-03-05 16:53:33 -08001972 if (CONFIG(TPM2)) {
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001973 printk(BIOS_DEBUG, "ACPI: * TPM2\n");
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001974 tpm2 = (acpi_tpm2_t *)current;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001975 acpi_create_tpm2(tpm2);
1976 if (tpm2->header.length >= sizeof(acpi_tpm2_t)) {
1977 current += tpm2->header.length;
1978 current = acpi_align_current(current);
1979 acpi_add_table(rsdp, tpm2);
1980 }
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +02001981 }
1982
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001983 if (CONFIG(ACPI_LPIT)) {
1984 printk(BIOS_DEBUG, "ACPI: * LPIT\n");
1985
1986 lpit = (acpi_lpit_t *)current;
1987 acpi_create_lpit(lpit);
1988 if (lpit->header.length >= sizeof(acpi_lpit_t)) {
1989 current += lpit->header.length;
1990 current = acpi_align_current(current);
1991 acpi_add_table(rsdp, lpit);
1992 }
1993 }
1994
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001995 printk(BIOS_DEBUG, "ACPI: * MADT\n");
1996
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001997 madt = (acpi_madt_t *)current;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001998 acpi_create_madt(madt);
1999 if (madt->header.length > sizeof(acpi_madt_t)) {
Lee Leahy024b13d2017-03-16 13:41:11 -07002000 current += madt->header.length;
2001 acpi_add_table(rsdp, madt);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02002002 }
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01002003
Aaron Durbin07a1b282015-12-10 17:07:38 -06002004 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02002005
Felix Heldfba479262021-05-28 16:11:43 +02002006 if (CONFIG(ACPI_BERT)) {
Francois Toguo522e0db2021-01-21 09:55:19 -08002007 void *region;
2008 size_t size;
Elyes Haouas3141fbad2022-11-18 15:22:32 +01002009 bert = (acpi_bert_t *)current;
Felix Heldfba479262021-05-28 16:11:43 +02002010 if (acpi_soc_get_bert_region(&region, &size) == CB_SUCCESS) {
2011 printk(BIOS_DEBUG, "ACPI: * BERT\n");
2012 acpi_write_bert(bert, (uintptr_t)region, size);
2013 if (bert->header.length >= sizeof(acpi_bert_t)) {
2014 current += bert->header.length;
2015 acpi_add_table(rsdp, bert);
2016 }
2017 current = acpi_align_current(current);
Francois Toguo522e0db2021-01-21 09:55:19 -08002018 }
Francois Toguo522e0db2021-01-21 09:55:19 -08002019 }
2020
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02002021 printk(BIOS_DEBUG, "current = %lx\n", current);
2022
2023 for (dev = all_devices; dev; dev = dev->next) {
2024 if (dev->ops && dev->ops->write_acpi_tables) {
Lee Leahy6f80ccc2017-03-16 15:18:22 -07002025 current = dev->ops->write_acpi_tables(dev, current,
2026 rsdp);
Aaron Durbin07a1b282015-12-10 17:07:38 -06002027 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02002028 }
2029 }
2030
2031 printk(BIOS_INFO, "ACPI: done.\n");
2032 return current;
2033}
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02002034
Rudolf Marek33cafe52009-04-13 18:07:02 +00002035static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp)
2036{
2037 if (strncmp((char *)rsdp, RSDP_SIG, sizeof(RSDP_SIG) - 1) != 0)
2038 return NULL;
2039
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00002040 printk(BIOS_DEBUG, "Looking on %p for valid checksum\n", rsdp);
Rudolf Marek33cafe52009-04-13 18:07:02 +00002041
2042 if (acpi_checksum((void *)rsdp, 20) != 0)
2043 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00002044 printk(BIOS_DEBUG, "Checksum 1 passed\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00002045
2046 if ((rsdp->revision > 1) && (acpi_checksum((void *)rsdp,
2047 rsdp->length) != 0))
2048 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00002049 printk(BIOS_DEBUG, "Checksum 2 passed all OK\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00002050
2051 return rsdp;
2052}
2053
Rudolf Marek33cafe52009-04-13 18:07:02 +00002054void *acpi_find_wakeup_vector(void)
2055{
2056 char *p, *end;
Rudolf Marek33cafe52009-04-13 18:07:02 +00002057 acpi_rsdt_t *rsdt;
2058 acpi_facs_t *facs;
Martin Rothc5f49262012-12-14 19:17:55 -07002059 acpi_fadt_t *fadt = NULL;
Kyösti Mälkki072d436b2016-06-17 08:44:40 +03002060 acpi_rsdp_t *rsdp = NULL;
Uwe Hermann622824c2010-11-19 15:14:42 +00002061 void *wake_vec;
Rudolf Marek33cafe52009-04-13 18:07:02 +00002062 int i;
2063
Kyösti Mälkkib8c7ea02020-11-17 16:41:38 +02002064 if (!acpi_is_wakeup_s3())
Rudolf Marek33cafe52009-04-13 18:07:02 +00002065 return NULL;
2066
Uwe Hermann622824c2010-11-19 15:14:42 +00002067 printk(BIOS_DEBUG, "Trying to find the wakeup vector...\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00002068
Uwe Hermann622824c2010-11-19 15:14:42 +00002069 /* Find RSDP. */
2070 for (p = (char *)0xe0000; p < (char *)0xfffff; p += 16) {
Lee Leahy0b5678f2017-03-16 16:01:40 -07002071 rsdp = valid_rsdp((acpi_rsdp_t *)p);
2072 if (rsdp)
Rudolf Marek33cafe52009-04-13 18:07:02 +00002073 break;
2074 }
2075
Angel Pons98a68ad2018-11-04 12:25:25 +01002076 if (rsdp == NULL) {
2077 printk(BIOS_ALERT,
2078 "No RSDP found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00002079 return NULL;
Angel Pons98a68ad2018-11-04 12:25:25 +01002080 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00002081
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00002082 printk(BIOS_DEBUG, "RSDP found at %p\n", rsdp);
Stefan Reinauer71a30182015-07-30 16:28:13 -07002083 rsdt = (acpi_rsdt_t *)(uintptr_t)rsdp->rsdt_address;
Stefan Reinauer14e22772010-04-27 06:56:47 +00002084
Uwe Hermann622824c2010-11-19 15:14:42 +00002085 end = (char *)rsdt + rsdt->header.length;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00002086 printk(BIOS_DEBUG, "RSDT found at %p ends at %p\n", rsdt, end);
Rudolf Marek33cafe52009-04-13 18:07:02 +00002087
Uwe Hermann622824c2010-11-19 15:14:42 +00002088 for (i = 0; ((char *)&rsdt->entry[i]) < end; i++) {
Stefan Reinauer71a30182015-07-30 16:28:13 -07002089 fadt = (acpi_fadt_t *)(uintptr_t)rsdt->entry[i];
Stefan Reinauercdfe3762009-07-21 22:15:43 +00002090 if (strncmp((char *)fadt, "FACP", 4) == 0)
Rudolf Marek33cafe52009-04-13 18:07:02 +00002091 break;
2092 fadt = NULL;
2093 }
2094
Angel Pons98a68ad2018-11-04 12:25:25 +01002095 if (fadt == NULL) {
2096 printk(BIOS_ALERT,
2097 "No FADT found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00002098 return NULL;
Angel Pons98a68ad2018-11-04 12:25:25 +01002099 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00002100
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00002101 printk(BIOS_DEBUG, "FADT found at %p\n", fadt);
Stefan Reinauer71a30182015-07-30 16:28:13 -07002102 facs = (acpi_facs_t *)(uintptr_t)fadt->firmware_ctrl;
Rudolf Marek33cafe52009-04-13 18:07:02 +00002103
Stefan Reinauer7e9771c2009-04-22 08:17:38 +00002104 if (facs == NULL) {
Angel Pons98a68ad2018-11-04 12:25:25 +01002105 printk(BIOS_ALERT,
2106 "No FACS found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00002107 return NULL;
Stefan Reinauer7e9771c2009-04-22 08:17:38 +00002108 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00002109
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00002110 printk(BIOS_DEBUG, "FACS found at %p\n", facs);
Stefan Reinauer71a30182015-07-30 16:28:13 -07002111 wake_vec = (void *)(uintptr_t)facs->firmware_waking_vector;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00002112 printk(BIOS_DEBUG, "OS waking vector is %p\n", wake_vec);
Uwe Hermann622824c2010-11-19 15:14:42 +00002113
Rudolf Marek33cafe52009-04-13 18:07:02 +00002114 return wake_vec;
2115}
2116
Aaron Durbin64031672018-04-21 14:45:32 -06002117__weak int acpi_get_gpe(int gpe)
Duncan Laurie1f6e6812016-09-19 12:04:19 -07002118{
2119 return -1; /* implemented by SOC */
2120}
Marc Jones93a51762018-08-22 18:57:24 -06002121
Elyes Haouas8b950f42022-02-16 12:08:16 +01002122u8 get_acpi_fadt_minor_version(void)
2123{
2124 return ACPI_FADT_MINOR_VERSION_0;
2125}
2126
Marc Jones93a51762018-08-22 18:57:24 -06002127int get_acpi_table_revision(enum acpi_tables table)
2128{
2129 switch (table) {
2130 case FADT:
Elyes Haouas8b950f42022-02-16 12:08:16 +01002131 return ACPI_FADT_REV_ACPI_6;
Elyes HAOUASf288b392018-11-11 15:22:30 +01002132 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 +01002133 return 3;
Marc Jones93a51762018-08-22 18:57:24 -06002134 case MCFG:
2135 return 1;
2136 case TCPA:
2137 return 2;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02002138 case TPM2:
2139 return 4;
Martin Roth0949e732021-10-01 14:28:22 -06002140 case SSDT: /* ACPI 3.0 up to 6.3: 2 */
Marc Jones93a51762018-08-22 18:57:24 -06002141 return 2;
Jonathan Zhanga3311b92022-12-01 17:13:41 -08002142 case SRAT: /* ACPI 2.0: 1, ACPI 3.0: 2, ACPI 4.0 up to 6.4: 3 */
2143 return 3;
Jonathan Zhang2a4e1f42021-04-01 11:43:37 -07002144 case HMAT: /* ACPI 6.4: 2 */
2145 return 2;
Marc Jones93a51762018-08-22 18:57:24 -06002146 case DMAR:
2147 return 1;
Martin Roth0949e732021-10-01 14:28:22 -06002148 case SLIT: /* ACPI 2.0 up to 6.3: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06002149 return 1;
Patrick Rudolph9d98e5a2019-06-14 19:00:04 +02002150 case SPMI: /* IMPI 2.0 */
2151 return 5;
Marc Jones93a51762018-08-22 18:57:24 -06002152 case HPET: /* Currently 1. Table added in ACPI 2.0. */
2153 return 1;
Elyes HAOUASf288b392018-11-11 15:22:30 +01002154 case VFCT: /* ACPI 2.0/3.0/4.0: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06002155 return 1;
2156 case IVRS:
Jason Glenesk1916f892020-07-24 02:51:30 -07002157 return IVRS_FORMAT_MIXED;
Marc Jones93a51762018-08-22 18:57:24 -06002158 case DBG2:
2159 return 0;
Martin Roth0949e732021-10-01 14:28:22 -06002160 case FACS: /* ACPI 2.0/3.0: 1, ACPI 4.0 up to 6.3: 2 */
Marc Jones93a51762018-08-22 18:57:24 -06002161 return 1;
Martin Roth0949e732021-10-01 14:28:22 -06002162 case RSDT: /* ACPI 1.0 up to 6.3: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06002163 return 1;
Martin Roth0949e732021-10-01 14:28:22 -06002164 case XSDT: /* ACPI 2.0 up to 6.3: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06002165 return 1;
Martin Roth0949e732021-10-01 14:28:22 -06002166 case RSDP: /* ACPI 2.0 up to 6.3: 2 */
Marc Jones93a51762018-08-22 18:57:24 -06002167 return 2;
Rocky Phaguraeff07132021-01-10 15:42:50 -08002168 case EINJ:
2169 return 1;
Marc Jones93a51762018-08-22 18:57:24 -06002170 case HEST:
2171 return 1;
2172 case NHLT:
2173 return 5;
Marshall Dawson44705c62018-09-04 13:47:15 -06002174 case BERT:
2175 return 1;
Jonathan Zhanga3311b92022-12-01 17:13:41 -08002176 case CEDT: /* CXL 3.0 section 9.17.1 */
2177 return 1;
Jason Glenesk61624b22020-11-02 20:06:23 -08002178 case CRAT:
2179 return 1;
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01002180 case LPIT: /* ACPI 5.1 up to 6.3: 0 */
2181 return 0;
Marc Jones93a51762018-08-22 18:57:24 -06002182 default:
2183 return -1;
2184 }
2185 return -1;
2186}