blob: c357e2bd230bc5effd9834acc6d18fac22c0e90f [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>
Kyösti Mälkkiae1b2d42023-04-10 16:45:39 +030020#include <arch/smp/mpspec.h>
Jianjun Wang8565b94a2022-03-02 10:20:26 +080021#include <cbfs.h>
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000022#include <cbmem.h>
Elyes HAOUASe2d152c2019-06-21 07:06:50 +020023#include <commonlib/helpers.h>
Jianjun Wang8565b94a2022-03-02 10:20:26 +080024#include <commonlib/sort.h>
25#include <console/console.h>
Duncan Laurie11290c42012-10-03 19:07:05 -070026#include <cpu/cpu.h>
Jianjun Wang8565b94a2022-03-02 10:20:26 +080027#include <device/mmio.h>
28#include <device/pci.h>
29#include <pc80/mc146818rtc.h>
30#include <string.h>
Elyes HAOUAScdd2f632021-02-11 19:37:11 +010031#include <types.h>
Elyes HAOUAS26071aa2019-02-15 08:21:33 +010032#include <version.h>
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000033
Kyösti Mälkkic7da0272021-06-08 11:37:08 +030034#if ENV_X86
35#include <arch/ioapic.h>
36#endif
37
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +020038static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp);
39
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000040u8 acpi_checksum(u8 *table, u32 length)
Stefan Reinauer688b3852004-01-28 16:56:14 +000041{
Uwe Hermann622824c2010-11-19 15:14:42 +000042 u8 ret = 0;
Stefan Reinauer688b3852004-01-28 16:56:14 +000043 while (length--) {
44 ret += *table;
45 table++;
46 }
Stefan Reinauera7648c22004-01-29 17:31:34 +000047 return -ret;
Stefan Reinauer688b3852004-01-28 16:56:14 +000048}
49
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000050/**
Uwe Hermann622824c2010-11-19 15:14:42 +000051 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
52 * and checksum.
Stefan Reinauer06feb882004-02-03 16:11:35 +000053 */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000054void acpi_add_table(acpi_rsdp_t *rsdp, void *table)
Stefan Reinauer688b3852004-01-28 16:56:14 +000055{
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000056 int i, entries_num;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000057 acpi_rsdt_t *rsdt;
58 acpi_xsdt_t *xsdt = NULL;
59
Uwe Hermann622824c2010-11-19 15:14:42 +000060 /* The RSDT is mandatory... */
Stefan Reinauerdefee172015-06-18 01:11:19 -070061 rsdt = (acpi_rsdt_t *)(uintptr_t)rsdp->rsdt_address;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000062
Uwe Hermann622824c2010-11-19 15:14:42 +000063 /* ...while the XSDT is not. */
64 if (rsdp->xsdt_address)
Stefan Reinauerdefee172015-06-18 01:11:19 -070065 xsdt = (acpi_xsdt_t *)((uintptr_t)rsdp->xsdt_address);
Stefan Reinauer14e22772010-04-27 06:56:47 +000066
Uwe Hermann622824c2010-11-19 15:14:42 +000067 /* This should always be MAX_ACPI_TABLES. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000068 entries_num = ARRAY_SIZE(rsdt->entry);
Stefan Reinauer14e22772010-04-27 06:56:47 +000069
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000070 for (i = 0; i < entries_num; i++) {
Uwe Hermann622824c2010-11-19 15:14:42 +000071 if (rsdt->entry[i] == 0)
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000072 break;
Stefan Reinauer688b3852004-01-28 16:56:14 +000073 }
74
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000075 if (i >= entries_num) {
Uwe Hermann622824c2010-11-19 15:14:42 +000076 printk(BIOS_ERR, "ACPI: Error: Could not add ACPI table, "
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +030077 "too many tables.\n");
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000078 return;
79 }
80
Uwe Hermann622824c2010-11-19 15:14:42 +000081 /* Add table to the RSDT. */
Stefan Reinauerdefee172015-06-18 01:11:19 -070082 rsdt->entry[i] = (uintptr_t)table;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000083
Uwe Hermann622824c2010-11-19 15:14:42 +000084 /* Fix RSDT length or the kernel will assume invalid entries. */
85 rsdt->header.length = sizeof(acpi_header_t) + (sizeof(u32) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000086
Uwe Hermann622824c2010-11-19 15:14:42 +000087 /* Re-calculate checksum. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000088 rsdt->header.checksum = 0; /* Hope this won't get optimized away */
Uwe Hermann622824c2010-11-19 15:14:42 +000089 rsdt->header.checksum = acpi_checksum((u8 *)rsdt, rsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000090
Uwe Hermann622824c2010-11-19 15:14:42 +000091 /*
92 * And now the same thing for the XSDT. We use the same index as for
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000093 * now we want the XSDT and RSDT to always be in sync in coreboot.
94 */
95 if (xsdt) {
Uwe Hermann622824c2010-11-19 15:14:42 +000096 /* Add table to the XSDT. */
Stefan Reinauerdefee172015-06-18 01:11:19 -070097 xsdt->entry[i] = (u64)(uintptr_t)table;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000098
Uwe Hermann622824c2010-11-19 15:14:42 +000099 /* Fix XSDT length. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000100 xsdt->header.length = sizeof(acpi_header_t) +
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300101 (sizeof(u64) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000102
Uwe Hermann622824c2010-11-19 15:14:42 +0000103 /* Re-calculate checksum. */
104 xsdt->header.checksum = 0;
105 xsdt->header.checksum = acpi_checksum((u8 *)xsdt,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300106 xsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000107 }
108
Uwe Hermann622824c2010-11-19 15:14:42 +0000109 printk(BIOS_DEBUG, "ACPI: added table %d/%d, length now %d\n",
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300110 i + 1, entries_num, rsdt->header.length);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000111}
112
Uwe Hermann622824c2010-11-19 15:14:42 +0000113int acpi_create_mcfg_mmconfig(acpi_mcfg_mmconfig_t *mmconfig, u32 base,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300114 u16 seg_nr, u8 start, u8 end)
Stefan Reinauer26d431a2009-01-20 19:17:51 +0000115{
Vladimir Serbinenko60fccdc2014-10-05 11:05:16 +0200116 memset(mmconfig, 0, sizeof(*mmconfig));
Rudolf Mareke6409f22007-11-03 12:50:26 +0000117 mmconfig->base_address = base;
118 mmconfig->base_reserved = 0;
119 mmconfig->pci_segment_group_number = seg_nr;
120 mmconfig->start_bus_number = start;
121 mmconfig->end_bus_number = end;
Uwe Hermann622824c2010-11-19 15:14:42 +0000122
123 return sizeof(acpi_mcfg_mmconfig_t);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000124}
125
Kyösti Mälkki2e9f0d32023-04-07 23:05:46 +0300126static int acpi_create_madt_lapic(acpi_madt_lapic_t *lapic, u8 cpu, u8 apic)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000127{
Himanshu Sahdev8c09b822019-10-21 18:50:50 +0530128 lapic->type = LOCAL_APIC; /* Local APIC structure */
Uwe Hermann622824c2010-11-19 15:14:42 +0000129 lapic->length = sizeof(acpi_madt_lapic_t);
130 lapic->flags = (1 << 0); /* Processor/LAPIC enabled */
131 lapic->processor_id = cpu;
132 lapic->apic_id = apic;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000133
Uwe Hermann622824c2010-11-19 15:14:42 +0000134 return lapic->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000135}
136
Kyösti Mälkki2e9f0d32023-04-07 23:05:46 +0300137static int acpi_create_madt_lx2apic(acpi_madt_lx2apic_t *lapic, u32 cpu, u32 apic)
Patrick Rudolph56a3ef22020-03-24 17:29:49 +0100138{
139 lapic->type = LOCAL_X2APIC; /* Local APIC structure */
140 lapic->reserved = 0;
141 lapic->length = sizeof(acpi_madt_lx2apic_t);
142 lapic->flags = (1 << 0); /* Processor/LAPIC enabled */
143 lapic->processor_id = cpu;
144 lapic->x2apic_id = apic;
145
146 return lapic->length;
147}
148
Kyösti Mälkki2e9f0d32023-04-07 23:05:46 +0300149unsigned long acpi_create_madt_one_lapic(unsigned long current, u32 index, u32 lapic_id)
150{
151 if (lapic_id <= ACPI_MADT_MAX_LAPIC_ID)
152 current += acpi_create_madt_lapic((acpi_madt_lapic_t *)current, index,
153 lapic_id);
154 else
155 current += acpi_create_madt_lx2apic((acpi_madt_lx2apic_t *)current, index,
156 lapic_id);
157
158 return current;
159}
160
Arthur Heymans85474292022-11-04 14:00:35 +0100161/* Increase if necessary. Currently all x86 CPUs only have 2 SMP threads */
162#define MAX_THREAD_ID 1
163/*
164 * From ACPI 6.4 spec:
165 * "The advent of multi-threaded processors yielded multiple logical processors
166 * executing on common processor hardware. ACPI defines logical processors in
167 * an identical manner as physical processors. To ensure that non
168 * multi-threading aware OSPM implementations realize optimal performance on
169 * platforms containing multi-threaded processors, two guidelines should be
170 * followed. The first is the same as above, that is, OSPM should initialize
171 * processors in the order that they appear in the MADT. The second is that
172 * platform firmware should list the first logical processor of each of the
173 * individual multi-threaded processors in the MADT before listing any of the
174 * second logical processors. This approach should be used for all successive
175 * logical processors."
176 */
Kyösti Mälkki899c7132021-06-16 11:06:26 +0300177static unsigned long acpi_create_madt_lapics(unsigned long current)
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000178{
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100179 struct device *cpu;
Arthur Heymans85474292022-11-04 14:00:35 +0100180 int index, apic_ids[CONFIG_MAX_CPUS] = {0}, num_cpus = 0, sort_start = 0;
181 for (unsigned int thread_id = 0; thread_id <= MAX_THREAD_ID; thread_id++) {
182 for (cpu = all_devices; cpu; cpu = cpu->next) {
183 if (!is_enabled_cpu(cpu))
184 continue;
185 if (num_cpus >= ARRAY_SIZE(apic_ids))
186 break;
187 if (cpu->path.apic.thread_id != thread_id)
188 continue;
189 apic_ids[num_cpus++] = cpu->path.apic.apic_id;
190 }
191 bubblesort(&apic_ids[sort_start], num_cpus - sort_start, NUM_ASCENDING);
192 sort_start = num_cpus;
Werner Zehdb561e62019-02-19 13:39:56 +0100193 }
Kyösti Mälkki2e9f0d32023-04-07 23:05:46 +0300194 for (index = 0; index < num_cpus; index++)
195 current = acpi_create_madt_one_lapic(current, index, apic_ids[index]);
Uwe Hermann622824c2010-11-19 15:14:42 +0000196
Stefan Reinauerda65fbf2009-04-22 08:18:37 +0000197 return current;
198}
199
Uwe Hermann622824c2010-11-19 15:14:42 +0000200int acpi_create_madt_ioapic(acpi_madt_ioapic_t *ioapic, u8 id, u32 addr,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300201 u32 gsi_base)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000202{
Himanshu Sahdev8c09b822019-10-21 18:50:50 +0530203 ioapic->type = IO_APIC; /* I/O APIC structure */
Uwe Hermann622824c2010-11-19 15:14:42 +0000204 ioapic->length = sizeof(acpi_madt_ioapic_t);
205 ioapic->reserved = 0x00;
206 ioapic->gsi_base = gsi_base;
207 ioapic->ioapic_id = id;
208 ioapic->ioapic_addr = addr;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000209
Uwe Hermann622824c2010-11-19 15:14:42 +0000210 return ioapic->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000211}
212
Kyösti Mälkkic7da0272021-06-08 11:37:08 +0300213#if ENV_X86
214/* For a system with multiple I/O APICs it's required that the one potentially
215 routing i8259 via ExtNMI delivery calls this first to get GSI #0. */
216int acpi_create_madt_ioapic_from_hw(acpi_madt_ioapic_t *ioapic, u32 addr)
217{
218 static u32 gsi_base;
219 u32 my_base;
220 u8 id = get_ioapic_id((void *)(uintptr_t)addr);
221 u8 count = ioapic_get_max_vectors((void *)(uintptr_t)addr);
222
223 my_base = gsi_base;
224 gsi_base += count;
225 return acpi_create_madt_ioapic(ioapic, id, addr, my_base);
226}
227#endif
228
Kyösti Mälkkie742b682023-04-10 17:03:32 +0300229static u16 acpi_sci_int(void)
Kyösti Mälkkiae1b2d42023-04-10 16:45:39 +0300230{
231#if ENV_X86
232 u8 gsi, irq, flags;
233
234 ioapic_get_sci_pin(&gsi, &irq, &flags);
235
236 /* ACPI Release 6.5, 5.2.9 and 5.2.15.5. */
237 if (!CONFIG(ACPI_HAVE_PCAT_8259))
238 return gsi;
239
240 assert(irq < 16);
241 return irq;
242#else
243 return 0;
244#endif
245}
246
Stefan Reinauer777224c2005-01-19 14:06:41 +0000247int acpi_create_madt_irqoverride(acpi_madt_irqoverride_t *irqoverride,
Stefan Reinauer06feb882004-02-03 16:11:35 +0000248 u8 bus, u8 source, u32 gsirq, u16 flags)
249{
Himanshu Sahdev8c09b822019-10-21 18:50:50 +0530250 irqoverride->type = IRQ_SOURCE_OVERRIDE; /* Interrupt source override */
Uwe Hermann622824c2010-11-19 15:14:42 +0000251 irqoverride->length = sizeof(acpi_madt_irqoverride_t);
252 irqoverride->bus = bus;
253 irqoverride->source = source;
254 irqoverride->gsirq = gsirq;
255 irqoverride->flags = flags;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000256
Uwe Hermann622824c2010-11-19 15:14:42 +0000257 return irqoverride->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000258}
259
Kyösti Mälkkiae1b2d42023-04-10 16:45:39 +0300260int acpi_create_madt_sci_override(acpi_madt_irqoverride_t *irqoverride)
261{
262 u8 gsi, irq, flags;
263
264 ioapic_get_sci_pin(&gsi, &irq, &flags);
265
266 if (!CONFIG(ACPI_HAVE_PCAT_8259))
267 irq = gsi;
268
269 irqoverride->type = IRQ_SOURCE_OVERRIDE; /* Interrupt source override */
270 irqoverride->length = sizeof(acpi_madt_irqoverride_t);
271 irqoverride->bus = MP_BUS_ISA;
272 irqoverride->source = irq;
273 irqoverride->gsirq = gsi;
274 irqoverride->flags = flags;
275
276 return irqoverride->length;
277}
278
Kyösti Mälkki9ac1fb72023-04-07 22:39:53 +0300279static 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 +0300280 u16 flags, u8 lint)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000281{
Himanshu Sahdev8c09b822019-10-21 18:50:50 +0530282 lapic_nmi->type = LOCAL_APIC_NMI; /* Local APIC NMI structure */
Uwe Hermann622824c2010-11-19 15:14:42 +0000283 lapic_nmi->length = sizeof(acpi_madt_lapic_nmi_t);
284 lapic_nmi->flags = flags;
285 lapic_nmi->processor_id = cpu;
286 lapic_nmi->lint = lint;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000287
Uwe Hermann622824c2010-11-19 15:14:42 +0000288 return lapic_nmi->length;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000289}
290
Kyösti Mälkki9ac1fb72023-04-07 22:39:53 +0300291static int acpi_create_madt_lx2apic_nmi(acpi_madt_lx2apic_nmi_t *lapic_nmi, u32 cpu,
Patrick Rudolph56a3ef22020-03-24 17:29:49 +0100292 u16 flags, u8 lint)
293{
294 lapic_nmi->type = LOCAL_X2APIC_NMI; /* Local APIC NMI structure */
295 lapic_nmi->length = sizeof(acpi_madt_lx2apic_nmi_t);
296 lapic_nmi->flags = flags;
297 lapic_nmi->processor_id = cpu;
298 lapic_nmi->lint = lint;
299 lapic_nmi->reserved[0] = 0;
300 lapic_nmi->reserved[1] = 0;
301 lapic_nmi->reserved[2] = 0;
302
303 return lapic_nmi->length;
304}
305
Kyösti Mälkki9ac1fb72023-04-07 22:39:53 +0300306unsigned long acpi_create_madt_lapic_nmis(unsigned long current)
Kyösti Mälkki66b5e1b2022-11-12 21:13:45 +0200307{
308 const u16 flags = MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH;
309
Kyösti Mälkki66b5e1b2022-11-12 21:13:45 +0200310 /* 1: LINT1 connect to NMI */
311 /* create all subtables for processors */
312 current += acpi_create_madt_lapic_nmi((acpi_madt_lapic_nmi_t *)current,
313 ACPI_MADT_LAPIC_NMI_ALL_PROCESSORS, flags, 1);
314
315 if (!CONFIG(XAPIC_ONLY))
316 current += acpi_create_madt_lx2apic_nmi((acpi_madt_lx2apic_nmi_t *)current,
317 ACPI_MADT_LX2APIC_NMI_ALL_PROCESSORS, flags, 1);
318
319 return current;
320}
321
Kyösti Mälkki9ac1fb72023-04-07 22:39:53 +0300322unsigned long acpi_create_madt_lapics_with_nmis(unsigned long current)
323{
324 current = acpi_create_madt_lapics(current);
325 current = acpi_create_madt_lapic_nmis(current);
326 return current;
327}
328
Arthur Heymans9368cf92023-04-25 16:07:49 +0200329static void acpi_create_madt(acpi_madt_t *madt)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000330{
Uwe Hermann622824c2010-11-19 15:14:42 +0000331 acpi_header_t *header = &(madt->header);
332 unsigned long current = (unsigned long)madt + sizeof(acpi_madt_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000333
Stefan Reinauer06feb882004-02-03 16:11:35 +0000334 memset((void *)madt, 0, sizeof(acpi_madt_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000335
John Zhao2ba303e2019-05-28 16:48:14 -0700336 if (!header)
337 return;
338
Uwe Hermann622824c2010-11-19 15:14:42 +0000339 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000340 memcpy(header->signature, "APIC", 4);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000341 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000342 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000343 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000344
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100345 header->asl_compiler_revision = asl_revision;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000346 header->length = sizeof(acpi_madt_t);
Marc Jones93a51762018-08-22 18:57:24 -0600347 header->revision = get_acpi_table_revision(MADT);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000348
Furquan Shaikhb1859a62020-04-30 21:27:47 -0700349 madt->lapic_addr = cpu_get_lapic_addr();
Nico Huber9df72e02018-11-24 18:25:50 +0100350 if (CONFIG(ACPI_HAVE_PCAT_8259))
351 madt->flags |= 1;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000352
Kyösti Mälkki69a13962023-04-08 14:10:48 +0300353 if (CONFIG(ACPI_COMMON_MADT_LAPIC))
354 current = acpi_create_madt_lapics_with_nmis(current);
355
Kyösti Mälkkia5fa5342022-11-18 13:23:52 +0200356 if (!CONFIG(ACPI_NO_MADT))
357 current = acpi_fill_madt(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)madt;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000361
Uwe Hermann622824c2010-11-19 15:14:42 +0000362 header->checksum = acpi_checksum((void *)madt, header->length);
Stefan Reinauer06feb882004-02-03 16:11:35 +0000363}
364
Kyösti Mälkkib54388d2021-02-14 15:09:45 +0200365static unsigned long acpi_fill_mcfg(unsigned long current)
366{
367 current += acpi_create_mcfg_mmconfig((acpi_mcfg_mmconfig_t *)current,
Shelley Chen4e9bb332021-10-20 15:43:45 -0700368 CONFIG_ECAM_MMCONF_BASE_ADDRESS, 0, 0,
369 CONFIG_ECAM_MMCONF_BUS_NUMBER - 1);
Kyösti Mälkkib54388d2021-02-14 15:09:45 +0200370 return current;
371}
372
Uwe Hermann622824c2010-11-19 15:14:42 +0000373/* MCFG is defined in the PCI Firmware Specification 3.0. */
Arthur Heymans9368cf92023-04-25 16:07:49 +0200374static void acpi_create_mcfg(acpi_mcfg_t *mcfg)
Rudolf Mareke6409f22007-11-03 12:50:26 +0000375{
Uwe Hermann622824c2010-11-19 15:14:42 +0000376 acpi_header_t *header = &(mcfg->header);
377 unsigned long current = (unsigned long)mcfg + sizeof(acpi_mcfg_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000378
Rudolf Mareke6409f22007-11-03 12:50:26 +0000379 memset((void *)mcfg, 0, sizeof(acpi_mcfg_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000380
John Zhao2ba303e2019-05-28 16:48:14 -0700381 if (!header)
382 return;
383
Uwe Hermann622824c2010-11-19 15:14:42 +0000384 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000385 memcpy(header->signature, "MCFG", 4);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000386 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000387 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000388 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000389
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100390 header->asl_compiler_revision = asl_revision;
Rudolf Mareke6409f22007-11-03 12:50:26 +0000391 header->length = sizeof(acpi_mcfg_t);
Marc Jones93a51762018-08-22 18:57:24 -0600392 header->revision = get_acpi_table_revision(MCFG);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000393
Shelley Chen4e9bb332021-10-20 15:43:45 -0700394 if (CONFIG(ECAM_MMCONF_SUPPORT))
Kyösti Mälkkib54388d2021-02-14 15:09:45 +0200395 current = acpi_fill_mcfg(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000396
Uwe Hermann622824c2010-11-19 15:14:42 +0000397 /* (Re)calculate length and checksum. */
398 header->length = current - (unsigned long)mcfg;
399 header->checksum = acpi_checksum((void *)mcfg, header->length);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000400}
401
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200402static void *get_tcpa_log(u32 *size)
403{
404 const struct cbmem_entry *ce;
405 const u32 tcpa_default_log_len = 0x10000;
406 void *lasa;
Philipp Deppenwiese791ba972018-07-30 01:15:20 +0200407 ce = cbmem_entry_find(CBMEM_ID_TCPA_TCG_LOG);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200408 if (ce) {
409 lasa = cbmem_entry_start(ce);
410 *size = cbmem_entry_size(ce);
411 printk(BIOS_DEBUG, "TCPA log found at %p\n", lasa);
412 return lasa;
413 }
Philipp Deppenwiese791ba972018-07-30 01:15:20 +0200414 lasa = cbmem_add(CBMEM_ID_TCPA_TCG_LOG, tcpa_default_log_len);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200415 if (!lasa) {
416 printk(BIOS_ERR, "TCPA log creation failed\n");
417 return NULL;
418 }
419
420 printk(BIOS_DEBUG, "TCPA log created at %p\n", lasa);
Lee Leahy024b13d2017-03-16 13:41:11 -0700421 memset(lasa, 0, tcpa_default_log_len);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200422
423 *size = tcpa_default_log_len;
424 return lasa;
425}
426
427static void acpi_create_tcpa(acpi_tcpa_t *tcpa)
428{
429 acpi_header_t *header = &(tcpa->header);
430 u32 tcpa_log_len;
431 void *lasa;
432
433 memset((void *)tcpa, 0, sizeof(acpi_tcpa_t));
434
435 lasa = get_tcpa_log(&tcpa_log_len);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700436 if (!lasa)
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200437 return;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200438
John Zhao2ba303e2019-05-28 16:48:14 -0700439 if (!header)
440 return;
441
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200442 /* Fill out header fields. */
443 memcpy(header->signature, "TCPA", 4);
444 memcpy(header->oem_id, OEM_ID, 6);
445 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
446 memcpy(header->asl_compiler_id, ASLC, 4);
447
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100448 header->asl_compiler_revision = asl_revision;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200449 header->length = sizeof(acpi_tcpa_t);
Marc Jones93a51762018-08-22 18:57:24 -0600450 header->revision = get_acpi_table_revision(TCPA);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200451
452 tcpa->platform_class = 0;
453 tcpa->laml = tcpa_log_len;
Elyes Haouas3141fbad2022-11-18 15:22:32 +0100454 tcpa->lasa = (uintptr_t)lasa;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200455
456 /* Calculate checksum. */
457 header->checksum = acpi_checksum((void *)tcpa, header->length);
458}
459
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100460static void *get_tpm2_log(u32 *size)
461{
462 const struct cbmem_entry *ce;
463 const u32 tpm2_default_log_len = 0x10000;
464 void *lasa;
465 ce = cbmem_entry_find(CBMEM_ID_TPM2_TCG_LOG);
466 if (ce) {
467 lasa = cbmem_entry_start(ce);
468 *size = cbmem_entry_size(ce);
469 printk(BIOS_DEBUG, "TPM2 log found at %p\n", lasa);
470 return lasa;
471 }
472 lasa = cbmem_add(CBMEM_ID_TPM2_TCG_LOG, tpm2_default_log_len);
473 if (!lasa) {
474 printk(BIOS_ERR, "TPM2 log creation failed\n");
475 return NULL;
476 }
477
478 printk(BIOS_DEBUG, "TPM2 log created at %p\n", lasa);
479 memset(lasa, 0, tpm2_default_log_len);
480
481 *size = tpm2_default_log_len;
482 return lasa;
483}
484
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200485static void acpi_create_tpm2(acpi_tpm2_t *tpm2)
486{
487 acpi_header_t *header = &(tpm2->header);
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100488 u32 tpm2_log_len;
489 void *lasa;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200490
491 memset((void *)tpm2, 0, sizeof(acpi_tpm2_t));
492
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100493 /*
494 * Some payloads like SeaBIOS depend on log area to use TPM2.
495 * Get the memory size and address of TPM2 log area or initialize it.
496 */
497 lasa = get_tpm2_log(&tpm2_log_len);
498 if (!lasa)
499 tpm2_log_len = 0;
500
John Zhao2ba303e2019-05-28 16:48:14 -0700501 if (!header)
502 return;
503
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200504 /* Fill out header fields. */
505 memcpy(header->signature, "TPM2", 4);
506 memcpy(header->oem_id, OEM_ID, 6);
507 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
508 memcpy(header->asl_compiler_id, ASLC, 4);
509
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100510 header->asl_compiler_revision = asl_revision;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200511 header->length = sizeof(acpi_tpm2_t);
512 header->revision = get_acpi_table_revision(TPM2);
513
514 /* Hard to detect for coreboot. Just set it to 0 */
515 tpm2->platform_class = 0;
Christian Walter26e0d4c2019-07-14 15:47:23 +0200516 if (CONFIG(CRB_TPM)) {
517 /* Must be set to 7 for CRB Support */
518 tpm2->control_area = CONFIG_CRB_TPM_BASE_ADDRESS + 0x40;
519 tpm2->start_method = 7;
520 } else {
521 /* Must be set to 0 for FIFO interface support */
522 tpm2->control_area = 0;
523 tpm2->start_method = 6;
524 }
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200525 memset(tpm2->msp, 0, sizeof(tpm2->msp));
526
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100527 /* Fill the log area size and start address fields. */
528 tpm2->laml = tpm2_log_len;
Elyes Haouas3141fbad2022-11-18 15:22:32 +0100529 tpm2->lasa = (uintptr_t)lasa;
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100530
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200531 /* Calculate checksum. */
532 header->checksum = acpi_checksum((void *)tpm2, header->length);
533}
534
Duncan Laurie37319032016-05-26 12:47:05 -0700535static void acpi_ssdt_write_cbtable(void)
536{
537 const struct cbmem_entry *cbtable;
538 uintptr_t base;
539 uint32_t size;
540
541 cbtable = cbmem_entry_find(CBMEM_ID_CBTABLE);
542 if (!cbtable)
543 return;
544 base = (uintptr_t)cbmem_entry_start(cbtable);
545 size = cbmem_entry_size(cbtable);
546
547 acpigen_write_device("CTBL");
548 acpigen_write_coreboot_hid(COREBOOT_ACPI_ID_CBTABLE);
549 acpigen_write_name_integer("_UID", 0);
David Wu5dff3962018-03-21 16:48:53 +0800550 acpigen_write_STA(ACPI_STATUS_DEVICE_HIDDEN_ON);
Duncan Laurie37319032016-05-26 12:47:05 -0700551 acpigen_write_name("_CRS");
552 acpigen_write_resourcetemplate_header();
553 acpigen_write_mem32fixed(0, base, size);
554 acpigen_write_resourcetemplate_footer();
555 acpigen_pop_len();
556}
557
Arthur Heymans9368cf92023-04-25 16:07:49 +0200558static void acpi_create_ssdt_generator(acpi_header_t *ssdt, const char *oem_table_id)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000559{
Uwe Hermann622824c2010-11-19 15:14:42 +0000560 unsigned long current = (unsigned long)ssdt + sizeof(acpi_header_t);
561
Rudolf Marek293b5f52009-02-01 18:35:15 +0000562 memset((void *)ssdt, 0, sizeof(acpi_header_t));
Uwe Hermann622824c2010-11-19 15:14:42 +0000563
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000564 memcpy(&ssdt->signature, "SSDT", 4);
Marc Jones93a51762018-08-22 18:57:24 -0600565 ssdt->revision = get_acpi_table_revision(SSDT);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000566 memcpy(&ssdt->oem_id, OEM_ID, 6);
567 memcpy(&ssdt->oem_table_id, oem_table_id, 8);
568 ssdt->oem_revision = 42;
Uwe Hermann622824c2010-11-19 15:14:42 +0000569 memcpy(&ssdt->asl_compiler_id, ASLC, 4);
Elyes HAOUAS26071aa2019-02-15 08:21:33 +0100570 ssdt->asl_compiler_revision = asl_revision;
Rudolf Marek293b5f52009-02-01 18:35:15 +0000571 ssdt->length = sizeof(acpi_header_t);
572
Elyes Haouas3141fbad2022-11-18 15:22:32 +0100573 acpigen_set_current((char *)current);
Duncan Laurie37319032016-05-26 12:47:05 -0700574
575 /* Write object to declare coreboot tables */
576 acpi_ssdt_write_cbtable();
577
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200578 {
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100579 struct device *dev;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200580 for (dev = all_devices; dev; dev = dev->next)
Karthikeyan Ramasubramaniand1c0f952020-11-02 16:26:52 -0700581 if (dev->enabled && dev->ops && dev->ops->acpi_fill_ssdt)
Nico Huber68680dd2020-03-31 17:34:52 +0200582 dev->ops->acpi_fill_ssdt(dev);
Elyes Haouas3141fbad2022-11-18 15:22:32 +0100583 current = (unsigned long)acpigen_get_current();
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200584 }
Rudolf Marek293b5f52009-02-01 18:35:15 +0000585
Uwe Hermann622824c2010-11-19 15:14:42 +0000586 /* (Re)calculate length and checksum. */
Rudolf Marek293b5f52009-02-01 18:35:15 +0000587 ssdt->length = current - (unsigned long)ssdt;
588 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
589}
590
Stefan Reinauerf622d592005-11-26 16:56:05 +0000591int acpi_create_srat_lapic(acpi_srat_lapic_t *lapic, u8 node, u8 apic)
592{
Myles Watson0b4c9f02009-03-10 18:06:47 +0000593 memset((void *)lapic, 0, sizeof(acpi_srat_lapic_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000594
Uwe Hermann622824c2010-11-19 15:14:42 +0000595 lapic->type = 0; /* Processor local APIC/SAPIC affinity structure */
596 lapic->length = sizeof(acpi_srat_lapic_t);
597 lapic->flags = (1 << 0); /* Enabled (the use of this structure). */
598 lapic->proximity_domain_7_0 = node;
599 /* TODO: proximity_domain_31_8, local SAPIC EID, clock domain. */
600 lapic->apic_id = apic;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000601
Uwe Hermann622824c2010-11-19 15:14:42 +0000602 return lapic->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000603}
604
Naresh Solanki76835cc2023-01-20 19:13:02 +0100605int acpi_create_srat_x2apic(acpi_srat_x2apic_t *x2apic, u32 node, u32 apic)
606{
607 memset((void *)x2apic, 0, sizeof(acpi_srat_x2apic_t));
608
609 x2apic->type = 2; /* Processor x2APIC structure */
610 x2apic->length = sizeof(acpi_srat_x2apic_t);
611 x2apic->flags = (1 << 0); /* Enabled (the use of this structure). */
612 x2apic->proximity_domain = node;
613 x2apic->x2apic_id = apic;
614
615 return x2apic->length;
616}
617
Uwe Hermann622824c2010-11-19 15:14:42 +0000618int 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 +0300619 u32 flags)
Stefan Reinauerf622d592005-11-26 16:56:05 +0000620{
Uwe Hermann622824c2010-11-19 15:14:42 +0000621 mem->type = 1; /* Memory affinity structure */
622 mem->length = sizeof(acpi_srat_mem_t);
623 mem->base_address_low = (basek << 10);
624 mem->base_address_high = (basek >> (32 - 10));
625 mem->length_low = (sizek << 10);
626 mem->length_high = (sizek >> (32 - 10));
627 mem->proximity_domain = node;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000628 mem->flags = flags;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000629
Uwe Hermann622824c2010-11-19 15:14:42 +0000630 return mem->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000631}
632
Jonathan Zhang3164b642021-04-21 17:51:31 -0700633int acpi_create_srat_gia_pci(acpi_srat_gia_t *gia, u32 proximity_domain,
634 u16 seg, u8 bus, u8 dev, u8 func, u32 flags)
635{
636 gia->type = ACPI_SRAT_STRUCTURE_GIA;
637 gia->length = sizeof(acpi_srat_gia_t);
638 gia->proximity_domain = proximity_domain;
639 gia->dev_handle_type = ACPI_SRAT_GIA_DEV_HANDLE_PCI;
640 /* First two bytes has segment number */
641 memcpy(gia->dev_handle, &seg, 2);
642 gia->dev_handle[2] = bus; /* Byte 2 has bus number */
643 /* Byte 3 has bits 7:3 for dev, bits 2:0 for func */
644 gia->dev_handle[3] = PCI_SLOT(dev) | PCI_FUNC(func);
645 gia->flags = flags;
646
647 return gia->length;
648}
649
Uwe Hermann622824c2010-11-19 15:14:42 +0000650/* http://www.microsoft.com/whdc/system/sysinternals/sratdwn.mspx */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +0200651void acpi_create_srat(acpi_srat_t *srat,
652 unsigned long (*acpi_fill_srat)(unsigned long current))
Stefan Reinauerf622d592005-11-26 16:56:05 +0000653{
Uwe Hermann622824c2010-11-19 15:14:42 +0000654 acpi_header_t *header = &(srat->header);
655 unsigned long current = (unsigned long)srat + sizeof(acpi_srat_t);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000656
Uwe Hermann622824c2010-11-19 15:14:42 +0000657 memset((void *)srat, 0, sizeof(acpi_srat_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000658
John Zhao2ba303e2019-05-28 16:48:14 -0700659 if (!header)
660 return;
661
Uwe Hermann622824c2010-11-19 15:14:42 +0000662 /* Fill out header fields. */
663 memcpy(header->signature, "SRAT", 4);
664 memcpy(header->oem_id, OEM_ID, 6);
665 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
666 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000667
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100668 header->asl_compiler_revision = asl_revision;
Uwe Hermann622824c2010-11-19 15:14:42 +0000669 header->length = sizeof(acpi_srat_t);
Marc Jones93a51762018-08-22 18:57:24 -0600670 header->revision = get_acpi_table_revision(SRAT);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000671
Uwe Hermann622824c2010-11-19 15:14:42 +0000672 srat->resv = 1; /* Spec: Reserved to 1 for backwards compatibility. */
Stefan Reinauerf622d592005-11-26 16:56:05 +0000673
Uwe Hermann622824c2010-11-19 15:14:42 +0000674 current = acpi_fill_srat(current);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000675
Uwe Hermann622824c2010-11-19 15:14:42 +0000676 /* (Re)calculate length and checksum. */
677 header->length = current - (unsigned long)srat;
678 header->checksum = acpi_checksum((void *)srat, header->length);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000679}
680
Jonathan Zhang3dcafa82022-05-11 13:11:20 -0700681int acpi_create_cedt_chbs(acpi_cedt_chbs_t *chbs, u32 uid, u32 cxl_ver, u64 base)
682{
683 memset((void *)chbs, 0, sizeof(acpi_cedt_chbs_t));
684
685 chbs->type = ACPI_CEDT_STRUCTURE_CHBS;
686 chbs->length = sizeof(acpi_cedt_chbs_t);
687 chbs->uid = uid;
688 chbs->cxl_ver = cxl_ver;
689 chbs->base = base;
690
691 /*
692 * CXL spec 2.0 section 9.14.1.2 "CXL CHBS"
693 * CXL 1.1 spec compliant host bridge: 8KB
694 * CXL 2.0 spec compliant host bridge: 64KB
695 */
696 if (cxl_ver == ACPI_CEDT_CHBS_CXL_VER_1_1)
697 chbs->len = 8 * KiB;
698 else if (cxl_ver == ACPI_CEDT_CHBS_CXL_VER_2_0)
699 chbs->len = 64 * KiB;
700 else
701 printk(BIOS_ERR, "ACPI(%s:%s): Incorrect CXL version:%d\n", __FILE__, __func__,
702 cxl_ver);
703
704 return chbs->length;
705}
706
707int acpi_create_cedt_cfmws(acpi_cedt_cfmws_t *cfmws, u64 base_hpa, u64 window_size, u8 eniw,
708 u32 hbig, u16 restriction, u16 qtg_id, const u32 *interleave_target)
709{
710 memset((void *)cfmws, 0, sizeof(acpi_cedt_cfmws_t));
711
712 cfmws->type = ACPI_CEDT_STRUCTURE_CFMWS;
713
714 u8 niw = 0;
715 if (eniw >= 8)
716 printk(BIOS_ERR, "ACPI(%s:%s): Incorrect eniw::%d\n", __FILE__, __func__, eniw);
717 else
718 /* NIW = 2 ** ENIW */
719 niw = 0x1 << eniw;
720 /* 36 + 4 * NIW */
721 cfmws->length = sizeof(acpi_cedt_cfmws_t) + 4 * niw;
722
723 cfmws->base_hpa = base_hpa;
724 cfmws->window_size = window_size;
725 cfmws->eniw = eniw;
726
727 // 0: Standard Modulo Arithmetic. Other values reserved.
728 cfmws->interleave_arithmetic = 0;
729
730 cfmws->hbig = hbig;
731 cfmws->restriction = restriction;
732 cfmws->qtg_id = qtg_id;
733 memcpy(&cfmws->interleave_target, interleave_target, 4 * niw);
734
735 return cfmws->length;
736}
737
738void acpi_create_cedt(acpi_cedt_t *cedt, unsigned long (*acpi_fill_cedt)(unsigned long current))
739{
740 acpi_header_t *header = &(cedt->header);
741 unsigned long current = (unsigned long)cedt + sizeof(acpi_cedt_t);
742
743 memset((void *)cedt, 0, sizeof(acpi_cedt_t));
744
745 if (!header)
746 return;
747
748 /* Fill out header fields. */
749 memcpy(header->signature, "CEDT", 4);
750 memcpy(header->oem_id, OEM_ID, 6);
751 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
752 memcpy(header->asl_compiler_id, ASLC, 4);
753
754 header->asl_compiler_revision = asl_revision;
755 header->length = sizeof(acpi_cedt_t);
756 header->revision = get_acpi_table_revision(CEDT);
757
758 current = acpi_fill_cedt(current);
759
760 /* (Re)calculate length and checksum. */
761 header->length = current - (unsigned long)cedt;
762 header->checksum = acpi_checksum((void *)cedt, header->length);
763}
764
Jonathan Zhang2a4e1f42021-04-01 11:43:37 -0700765int acpi_create_hmat_mpda(acpi_hmat_mpda_t *mpda, u32 initiator, u32 memory)
766{
767 memset((void *)mpda, 0, sizeof(acpi_hmat_mpda_t));
768
769 mpda->type = 0; /* Memory Proximity Domain Attributes structure */
770 mpda->length = sizeof(acpi_hmat_mpda_t);
771 /*
772 * Proximity Domain for Attached Initiator field is valid.
773 * Bit 1 and bit 2 are reserved since HMAT revision 2.
774 */
775 mpda->flags = (1 << 0);
776 mpda->proximity_domain_initiator = initiator;
777 mpda->proximity_domain_memory = memory;
778
779 return mpda->length;
780}
781
782void acpi_create_hmat(acpi_hmat_t *hmat,
783 unsigned long (*acpi_fill_hmat)(unsigned long current))
784{
785 acpi_header_t *header = &(hmat->header);
786 unsigned long current = (unsigned long)hmat + sizeof(acpi_hmat_t);
787
788 memset((void *)hmat, 0, sizeof(acpi_hmat_t));
789
790 if (!header)
791 return;
792
793 /* Fill out header fields. */
794 memcpy(header->signature, "HMAT", 4);
795 memcpy(header->oem_id, OEM_ID, 6);
796 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
797 memcpy(header->asl_compiler_id, ASLC, 4);
798
799 header->asl_compiler_revision = asl_revision;
800 header->length = sizeof(acpi_hmat_t);
801 header->revision = get_acpi_table_revision(HMAT);
802
803 current = acpi_fill_hmat(current);
804
805 /* (Re)calculate length and checksum. */
806 header->length = current - (unsigned long)hmat;
807 header->checksum = acpi_checksum((void *)hmat, header->length);
808}
809
Nico Hubere561f352015-10-26 11:51:25 +0100810void acpi_create_dmar(acpi_dmar_t *dmar, enum dmar_flags flags,
Lee Leahy024b13d2017-03-16 13:41:11 -0700811 unsigned long (*acpi_fill_dmar)(unsigned long))
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200812{
813 acpi_header_t *header = &(dmar->header);
814 unsigned long current = (unsigned long)dmar + sizeof(acpi_dmar_t);
815
816 memset((void *)dmar, 0, sizeof(acpi_dmar_t));
817
John Zhao2ba303e2019-05-28 16:48:14 -0700818 if (!header)
819 return;
820
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200821 /* Fill out header fields. */
822 memcpy(header->signature, "DMAR", 4);
823 memcpy(header->oem_id, OEM_ID, 6);
824 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
825 memcpy(header->asl_compiler_id, ASLC, 4);
826
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +0100827 header->asl_compiler_revision = asl_revision;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200828 header->length = sizeof(acpi_dmar_t);
Marc Jones93a51762018-08-22 18:57:24 -0600829 header->revision = get_acpi_table_revision(DMAR);
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200830
Jacob Laskaf3f654d2016-03-15 21:53:27 -0500831 dmar->host_address_width = cpu_phys_address_size() - 1;
Nico Hubere561f352015-10-26 11:51:25 +0100832 dmar->flags = flags;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200833
834 current = acpi_fill_dmar(current);
835
836 /* (Re)calculate length and checksum. */
837 header->length = current - (unsigned long)dmar;
838 header->checksum = acpi_checksum((void *)dmar, header->length);
839}
840
841unsigned long acpi_create_dmar_drhd(unsigned long current, u8 flags,
Matt DeVillier7866d492018-03-29 14:59:57 +0200842 u16 segment, u64 bar)
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200843{
844 dmar_entry_t *drhd = (dmar_entry_t *)current;
845 memset(drhd, 0, sizeof(*drhd));
846 drhd->type = DMAR_DRHD;
847 drhd->length = sizeof(*drhd); /* will be fixed up later */
848 drhd->flags = flags;
849 drhd->segment = segment;
850 drhd->bar = bar;
851
852 return drhd->length;
853}
854
Matt DeVillier7866d492018-03-29 14:59:57 +0200855unsigned long acpi_create_dmar_rmrr(unsigned long current, u16 segment,
856 u64 bar, u64 limit)
857{
858 dmar_rmrr_entry_t *rmrr = (dmar_rmrr_entry_t *)current;
859 memset(rmrr, 0, sizeof(*rmrr));
860 rmrr->type = DMAR_RMRR;
861 rmrr->length = sizeof(*rmrr); /* will be fixed up later */
862 rmrr->segment = segment;
863 rmrr->bar = bar;
864 rmrr->limit = limit;
865
866 return rmrr->length;
867}
868
Werner Zehd4d76952016-07-27 06:56:36 +0200869unsigned long acpi_create_dmar_atsr(unsigned long current, u8 flags,
870 u16 segment)
871{
872 dmar_atsr_entry_t *atsr = (dmar_atsr_entry_t *)current;
873 memset(atsr, 0, sizeof(*atsr));
874 atsr->type = DMAR_ATSR;
875 atsr->length = sizeof(*atsr); /* will be fixed up later */
876 atsr->flags = flags;
877 atsr->segment = segment;
878
879 return atsr->length;
880}
881
John Zhao76e70672019-04-04 11:03:28 -0700882unsigned long acpi_create_dmar_rhsa(unsigned long current, u64 base_addr,
883 u32 proximity_domain)
884{
885 dmar_rhsa_entry_t *rhsa = (dmar_rhsa_entry_t *)current;
886 memset(rhsa, 0, sizeof(*rhsa));
887 rhsa->type = DMAR_RHSA;
888 rhsa->length = sizeof(*rhsa);
889 rhsa->base_address = base_addr;
890 rhsa->proximity_domain = proximity_domain;
891
892 return rhsa->length;
893}
894
895unsigned long acpi_create_dmar_andd(unsigned long current, u8 device_number,
896 const char *device_name)
897{
898 dmar_andd_entry_t *andd = (dmar_andd_entry_t *)current;
899 int andd_len = sizeof(dmar_andd_entry_t) + strlen(device_name) + 1;
900 memset(andd, 0, andd_len);
901 andd->type = DMAR_ANDD;
902 andd->length = andd_len;
903 andd->device_number = device_number;
904 memcpy(&andd->device_name, device_name, strlen(device_name));
905
906 return andd->length;
907}
908
John Zhao091532d2021-04-17 16:03:21 -0700909unsigned long acpi_create_dmar_satc(unsigned long current, u8 flags, u16 segment)
John Zhao6edbb182021-03-24 11:55:09 -0700910{
911 dmar_satc_entry_t *satc = (dmar_satc_entry_t *)current;
John Zhao091532d2021-04-17 16:03:21 -0700912 int satc_len = sizeof(dmar_satc_entry_t);
John Zhao6edbb182021-03-24 11:55:09 -0700913 memset(satc, 0, satc_len);
914 satc->type = DMAR_SATC;
915 satc->length = satc_len;
916 satc->flags = flags;
917 satc->segment_number = segment;
John Zhao6edbb182021-03-24 11:55:09 -0700918
919 return satc->length;
920}
921
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200922void acpi_dmar_drhd_fixup(unsigned long base, unsigned long current)
923{
924 dmar_entry_t *drhd = (dmar_entry_t *)base;
925 drhd->length = current - base;
926}
927
Matt DeVillier7866d492018-03-29 14:59:57 +0200928void acpi_dmar_rmrr_fixup(unsigned long base, unsigned long current)
929{
930 dmar_rmrr_entry_t *rmrr = (dmar_rmrr_entry_t *)base;
931 rmrr->length = current - base;
932}
933
Werner Zehd4d76952016-07-27 06:56:36 +0200934void acpi_dmar_atsr_fixup(unsigned long base, unsigned long current)
935{
936 dmar_atsr_entry_t *atsr = (dmar_atsr_entry_t *)base;
937 atsr->length = current - base;
938}
939
John Zhao6edbb182021-03-24 11:55:09 -0700940void acpi_dmar_satc_fixup(unsigned long base, unsigned long current)
941{
942 dmar_satc_entry_t *satc = (dmar_satc_entry_t *)base;
943 satc->length = current - base;
944}
945
Matt DeVillier7866d492018-03-29 14:59:57 +0200946static unsigned long acpi_create_dmar_ds(unsigned long current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100947 enum dev_scope_type type, u8 enumeration_id, u8 bus, u8 dev, u8 fn)
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200948{
Nico Huber6c4751d2015-10-26 12:03:54 +0100949 /* we don't support longer paths yet */
950 const size_t dev_scope_length = sizeof(dev_scope_t) + 2;
951
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200952 dev_scope_t *ds = (dev_scope_t *)current;
Nico Huber6c4751d2015-10-26 12:03:54 +0100953 memset(ds, 0, dev_scope_length);
954 ds->type = type;
955 ds->length = dev_scope_length;
956 ds->enumeration = enumeration_id;
957 ds->start_bus = bus;
958 ds->path[0].dev = dev;
959 ds->path[0].fn = fn;
Patrick Georgi3b590ff2012-09-11 15:21:01 +0200960
961 return ds->length;
962}
963
Matt DeVillier7866d492018-03-29 14:59:57 +0200964unsigned long acpi_create_dmar_ds_pci_br(unsigned long current, u8 bus,
Werner Zeh21a5bff2016-07-27 07:07:20 +0200965 u8 dev, u8 fn)
966{
Matt DeVillier7866d492018-03-29 14:59:57 +0200967 return acpi_create_dmar_ds(current,
Werner Zeh21a5bff2016-07-27 07:07:20 +0200968 SCOPE_PCI_SUB, 0, bus, dev, fn);
969}
970
Matt DeVillier7866d492018-03-29 14:59:57 +0200971unsigned long acpi_create_dmar_ds_pci(unsigned long current, u8 bus,
Nico Huber6c4751d2015-10-26 12:03:54 +0100972 u8 dev, u8 fn)
973{
Matt DeVillier7866d492018-03-29 14:59:57 +0200974 return acpi_create_dmar_ds(current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100975 SCOPE_PCI_ENDPOINT, 0, bus, dev, fn);
976}
977
Matt DeVillier7866d492018-03-29 14:59:57 +0200978unsigned long acpi_create_dmar_ds_ioapic(unsigned long current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100979 u8 enumeration_id, u8 bus, u8 dev, u8 fn)
980{
Matt DeVillier7866d492018-03-29 14:59:57 +0200981 return acpi_create_dmar_ds(current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100982 SCOPE_IOAPIC, enumeration_id, bus, dev, fn);
983}
984
Arthur Heymansbc8f8592022-12-02 13:17:39 +0100985unsigned long acpi_create_dmar_ds_ioapic_from_hw(unsigned long current,
986 u32 addr, u8 bus, u8 dev, u8 fn)
987{
988 u8 enumeration_id = get_ioapic_id((void *)(uintptr_t)addr);
989 return acpi_create_dmar_ds(current,
990 SCOPE_IOAPIC, enumeration_id, bus, dev, fn);
991}
992
Matt DeVillier7866d492018-03-29 14:59:57 +0200993unsigned long acpi_create_dmar_ds_msi_hpet(unsigned long current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100994 u8 enumeration_id, u8 bus, u8 dev, u8 fn)
995{
Matt DeVillier7866d492018-03-29 14:59:57 +0200996 return acpi_create_dmar_ds(current,
Nico Huber6c4751d2015-10-26 12:03:54 +0100997 SCOPE_MSI_HPET, enumeration_id, bus, dev, fn);
998}
999
Uwe Hermann622824c2010-11-19 15:14:42 +00001000/* http://h21007.www2.hp.com/portal/download/files/unprot/Itanium/slit.pdf */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +02001001void acpi_create_slit(acpi_slit_t *slit,
1002 unsigned long (*acpi_fill_slit)(unsigned long current))
Yinghai Lud4b278c2006-10-04 20:46:15 +00001003{
Uwe Hermann622824c2010-11-19 15:14:42 +00001004 acpi_header_t *header = &(slit->header);
1005 unsigned long current = (unsigned long)slit + sizeof(acpi_slit_t);
Yinghai Lud4b278c2006-10-04 20:46:15 +00001006
Uwe Hermann622824c2010-11-19 15:14:42 +00001007 memset((void *)slit, 0, sizeof(acpi_slit_t));
Yinghai Lud4b278c2006-10-04 20:46:15 +00001008
John Zhao2ba303e2019-05-28 16:48:14 -07001009 if (!header)
1010 return;
1011
Uwe Hermann622824c2010-11-19 15:14:42 +00001012 /* Fill out header fields. */
1013 memcpy(header->signature, "SLIT", 4);
1014 memcpy(header->oem_id, OEM_ID, 6);
1015 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1016 memcpy(header->asl_compiler_id, ASLC, 4);
Yinghai Lud4b278c2006-10-04 20:46:15 +00001017
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001018 header->asl_compiler_revision = asl_revision;
Uwe Hermann622824c2010-11-19 15:14:42 +00001019 header->length = sizeof(acpi_slit_t);
Marc Jones93a51762018-08-22 18:57:24 -06001020 header->revision = get_acpi_table_revision(SLIT);
Yinghai Lud4b278c2006-10-04 20:46:15 +00001021
Uwe Hermann622824c2010-11-19 15:14:42 +00001022 current = acpi_fill_slit(current);
Yinghai Lud4b278c2006-10-04 20:46:15 +00001023
Uwe Hermann622824c2010-11-19 15:14:42 +00001024 /* (Re)calculate length and checksum. */
1025 header->length = current - (unsigned long)slit;
1026 header->checksum = acpi_checksum((void *)slit, header->length);
Yinghai Lud4b278c2006-10-04 20:46:15 +00001027}
1028
Uwe Hermann622824c2010-11-19 15:14:42 +00001029/* http://www.intel.com/hardwaredesign/hpetspec_1.pdf */
Arthur Heymans9368cf92023-04-25 16:07:49 +02001030static void acpi_create_hpet(acpi_hpet_t *hpet)
Stefan Reinauer688b3852004-01-28 16:56:14 +00001031{
Uwe Hermann622824c2010-11-19 15:14:42 +00001032 acpi_header_t *header = &(hpet->header);
1033 acpi_addr_t *addr = &(hpet->addr);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001034
Stefan Reinauera7648c22004-01-29 17:31:34 +00001035 memset((void *)hpet, 0, sizeof(acpi_hpet_t));
Stefan Reinauer14e22772010-04-27 06:56:47 +00001036
John Zhao2ba303e2019-05-28 16:48:14 -07001037 if (!header)
1038 return;
1039
Uwe Hermann622824c2010-11-19 15:14:42 +00001040 /* Fill out header fields. */
Stefan Reinauer4704dc52009-07-22 01:11:37 +00001041 memcpy(header->signature, "HPET", 4);
Stefan Reinauer688b3852004-01-28 16:56:14 +00001042 memcpy(header->oem_id, OEM_ID, 6);
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001043 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +00001044 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001045
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001046 header->asl_compiler_revision = asl_revision;
Stefan Reinauer688b3852004-01-28 16:56:14 +00001047 header->length = sizeof(acpi_hpet_t);
Marc Jones93a51762018-08-22 18:57:24 -06001048 header->revision = get_acpi_table_revision(HPET);
Stefan Reinauer688b3852004-01-28 16:56:14 +00001049
Uwe Hermann622824c2010-11-19 15:14:42 +00001050 /* Fill out HPET address. */
Elyes HAOUAS04071f42020-07-20 17:05:24 +02001051 addr->space_id = ACPI_ADDRESS_SPACE_MEMORY;
Uwe Hermann622824c2010-11-19 15:14:42 +00001052 addr->bit_width = 64;
1053 addr->bit_offset = 0;
Felix Held972d9f22022-02-23 16:32:20 +01001054 addr->addrl = HPET_BASE_ADDRESS & 0xffffffff;
1055 addr->addrh = ((unsigned long long)HPET_BASE_ADDRESS) >> 32;
Stefan Reinauer688b3852004-01-28 16:56:14 +00001056
Felix Held972d9f22022-02-23 16:32:20 +01001057 hpet->id = read32p(HPET_BASE_ADDRESS);
Uwe Hermann622824c2010-11-19 15:14:42 +00001058 hpet->number = 0;
Patrick Georgi9aeb6942012-10-05 21:54:38 +02001059 hpet->min_tick = CONFIG_HPET_MIN_TICKS;
Stefan Reinauer14e22772010-04-27 06:56:47 +00001060
Uwe Hermann622824c2010-11-19 15:14:42 +00001061 header->checksum = acpi_checksum((void *)hpet, sizeof(acpi_hpet_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +00001062}
Uwe Hermann622824c2010-11-19 15:14:42 +00001063
Rocky Phaguraeff07132021-01-10 15:42:50 -08001064/*
1065 * This method adds the ACPI error injection capability. It fills the default information.
1066 * HW dependent code (caller) can modify the defaults upon return. If no changes are necessary
1067 * and the defaults are acceptable then caller can simply add the table (acpi_add_table).
1068 * INPUTS:
1069 * einj - ptr to the starting location of EINJ table
1070 * actions - number of actions to trigger an error (HW dependent)
1071 * addr - address of trigger action table. This should be ACPI reserved memory and it will be
1072 * shared between OS and FW.
1073 */
1074void acpi_create_einj(acpi_einj_t *einj, uintptr_t addr, u8 actions)
1075{
1076 int i;
1077 acpi_header_t *header = &(einj->header);
1078 acpi_injection_header_t *inj_header = &(einj->inj_header);
1079 acpi_einj_smi_t *einj_smi = (acpi_einj_smi_t *)addr;
1080 acpi_einj_trigger_table_t *tat;
1081 if (!header)
1082 return;
1083
1084 printk(BIOS_DEBUG, "%s einj_smi = %p\n", __func__, einj_smi);
1085 memset(einj_smi, 0, sizeof(acpi_einj_smi_t));
Jonathan Zhangd5d9b282022-11-07 17:30:14 -08001086 tat = (acpi_einj_trigger_table_t *)((uint8_t *)einj_smi + sizeof(acpi_einj_smi_t));
Rocky Phaguraeff07132021-01-10 15:42:50 -08001087 tat->header_size = 16;
1088 tat->revision = 0;
1089 tat->table_size = sizeof(acpi_einj_trigger_table_t) +
1090 sizeof(acpi_einj_action_table_t) * actions - 1;
1091 tat->entry_count = actions;
1092 printk(BIOS_DEBUG, "%s trigger_action_table = %p\n", __func__, tat);
1093
1094 for (i = 0; i < actions; i++) {
1095 tat->trigger_action[i].action = TRIGGER_ERROR;
1096 tat->trigger_action[i].instruction = NO_OP;
1097 tat->trigger_action[i].flags = FLAG_IGNORE;
1098 tat->trigger_action[i].reg.space_id = ACPI_ADDRESS_SPACE_MEMORY;
1099 tat->trigger_action[i].reg.bit_width = 64;
1100 tat->trigger_action[i].reg.bit_offset = 0;
1101 tat->trigger_action[i].reg.access_size = ACPI_ACCESS_SIZE_QWORD_ACCESS;
1102 tat->trigger_action[i].reg.addr = 0;
1103 tat->trigger_action[i].value = 0;
1104 tat->trigger_action[i].mask = 0xFFFFFFFF;
1105 }
1106
1107 acpi_einj_action_table_t default_actions[ACTION_COUNT] = {
1108 [0] = {
1109 .action = BEGIN_INJECT_OP,
1110 .instruction = WRITE_REGISTER_VALUE,
1111 .flags = FLAG_PRESERVE,
1112 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->op_state),
1113 .value = 0,
1114 .mask = 0xFFFFFFFF
1115 },
1116 [1] = {
1117 .action = GET_TRIGGER_ACTION_TABLE,
1118 .instruction = READ_REGISTER,
1119 .flags = FLAG_IGNORE,
1120 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->trigger_action_table),
1121 .value = 0,
1122 .mask = 0xFFFFFFFFFFFFFFFF
1123 },
1124 [2] = {
1125 .action = SET_ERROR_TYPE,
1126 .instruction = WRITE_REGISTER,
1127 .flags = FLAG_PRESERVE,
1128 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->err_inject[0]),
1129 .value = 0,
1130 .mask = 0xFFFFFFFF
1131 },
1132 [3] = {
1133 .action = GET_ERROR_TYPE,
1134 .instruction = READ_REGISTER,
1135 .flags = FLAG_IGNORE,
1136 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->err_inj_cap),
1137 .value = 0,
1138 .mask = 0xFFFFFFFF
1139 },
1140 [4] = {
1141 .action = END_INJECT_OP,
1142 .instruction = WRITE_REGISTER_VALUE,
1143 .flags = FLAG_PRESERVE,
1144 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->op_state),
1145 .value = 0,
1146 .mask = 0xFFFFFFFF
1147
1148 },
1149 [5] = {
1150 .action = EXECUTE_INJECT_OP,
1151 .instruction = WRITE_REGISTER_VALUE,
1152 .flags = FLAG_PRESERVE,
1153 .reg = EINJ_REG_IO(),
1154 .value = 0x9a,
1155 .mask = 0xFFFF,
1156 },
1157 [6] = {
1158 .action = CHECK_BUSY_STATUS,
1159 .instruction = READ_REGISTER_VALUE,
1160 .flags = FLAG_IGNORE,
1161 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->op_status),
1162 .value = 1,
1163 .mask = 1,
1164 },
1165 [7] = {
1166 .action = GET_CMD_STATUS,
1167 .instruction = READ_REGISTER,
1168 .flags = FLAG_PRESERVE,
1169 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->cmd_sts),
1170 .value = 0,
1171 .mask = 0x1fe,
1172 },
1173 [8] = {
1174 .action = SET_ERROR_TYPE_WITH_ADDRESS,
1175 .instruction = WRITE_REGISTER,
1176 .flags = FLAG_PRESERVE,
1177 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->setaddrtable),
1178 .value = 1,
1179 .mask = 0xffffffff
1180 }
1181 };
1182
1183 einj_smi->err_inj_cap = ACPI_EINJ_DEFAULT_CAP;
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001184 einj_smi->trigger_action_table = (u64)(uintptr_t)tat;
Rocky Phaguraeff07132021-01-10 15:42:50 -08001185
1186 for (i = 0; i < ACTION_COUNT; i++)
1187 printk(BIOS_DEBUG, "default_actions[%d].reg.addr is %llx\n", i,
1188 default_actions[i].reg.addr);
1189
Rocky Phagurab46a9e52021-05-21 13:34:03 -07001190 memset((void *)einj, 0, sizeof(*einj));
Rocky Phaguraeff07132021-01-10 15:42:50 -08001191
1192 /* Fill out header fields. */
1193 memcpy(header->signature, "EINJ", 4);
1194 memcpy(header->oem_id, OEM_ID, 6);
1195 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1196 memcpy(header->asl_compiler_id, ASLC, 4);
1197
1198 header->asl_compiler_revision = asl_revision;
1199 header->length = sizeof(acpi_einj_t);
1200 header->revision = 1;
1201 inj_header->einj_header_size = sizeof(acpi_injection_header_t);
1202 inj_header->entry_count = ACTION_COUNT;
1203
1204 printk(BIOS_DEBUG, "%s einj->action_table = %p\n",
1205 __func__, einj->action_table);
1206 memcpy((void *)einj->action_table, (void *)default_actions, sizeof(einj->action_table));
Rocky Phagurab46a9e52021-05-21 13:34:03 -07001207 header->checksum = acpi_checksum((void *)einj, sizeof(*einj));
Rocky Phaguraeff07132021-01-10 15:42:50 -08001208}
1209
Furquan Shaikh0f007d82020-04-24 06:41:18 -07001210void acpi_create_vfct(const struct device *device,
Himanshu Sahdev7f1da072019-10-21 15:27:19 +05301211 acpi_vfct_t *vfct,
Furquan Shaikh0f007d82020-04-24 06:41:18 -07001212 unsigned long (*acpi_fill_vfct)(const struct device *device,
Himanshu Sahdev7f1da072019-10-21 15:27:19 +05301213 acpi_vfct_t *vfct_struct, unsigned long current))
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +02001214{
1215 acpi_header_t *header = &(vfct->header);
Himanshu Sahdev7f1da072019-10-21 15:27:19 +05301216 unsigned long current = (unsigned long)vfct + sizeof(acpi_vfct_t);
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +02001217
Himanshu Sahdev7f1da072019-10-21 15:27:19 +05301218 memset((void *)vfct, 0, sizeof(acpi_vfct_t));
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +02001219
John Zhao2ba303e2019-05-28 16:48:14 -07001220 if (!header)
1221 return;
1222
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +02001223 /* Fill out header fields. */
1224 memcpy(header->signature, "VFCT", 4);
1225 memcpy(header->oem_id, OEM_ID, 6);
1226 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1227 memcpy(header->asl_compiler_id, ASLC, 4);
1228
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001229 header->asl_compiler_revision = asl_revision;
Marc Jones93a51762018-08-22 18:57:24 -06001230 header->revision = get_acpi_table_revision(VFCT);
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +02001231
1232 current = acpi_fill_vfct(device, vfct, current);
1233
Kyösti Mälkkifb493792019-06-30 06:29:52 +03001234 /* If no BIOS image, return with header->length == 0. */
1235 if (!vfct->VBIOSImageOffset)
1236 return;
1237
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +02001238 /* (Re)calculate length and checksum. */
1239 header->length = current - (unsigned long)vfct;
1240 header->checksum = acpi_checksum((void *)vfct, header->length);
1241}
1242
Furquan Shaikh0f007d82020-04-24 06:41:18 -07001243void acpi_create_ipmi(const struct device *device,
Patrick Rudolph9d98e5a2019-06-14 19:00:04 +02001244 struct acpi_spmi *spmi,
1245 const u16 ipmi_revision,
1246 const acpi_addr_t *addr,
1247 const enum acpi_ipmi_interface_type type,
1248 const s8 gpe_interrupt,
1249 const u32 apic_interrupt,
1250 const u32 uid)
1251{
1252 acpi_header_t *header = &(spmi->header);
1253 memset((void *)spmi, 0, sizeof(struct acpi_spmi));
1254
1255 /* Fill out header fields. */
1256 memcpy(header->signature, "SPMI", 4);
1257 memcpy(header->oem_id, OEM_ID, 6);
1258 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1259 memcpy(header->asl_compiler_id, ASLC, 4);
1260
1261 header->asl_compiler_revision = asl_revision;
1262 header->length = sizeof(struct acpi_spmi);
1263 header->revision = get_acpi_table_revision(SPMI);
1264
1265 spmi->reserved = 1;
1266
1267 if (device->path.type == DEVICE_PATH_PCI) {
1268 spmi->pci_device_flag = ACPI_IPMI_PCI_DEVICE_FLAG;
1269 spmi->pci_bus = device->bus->secondary;
1270 spmi->pci_device = device->path.pci.devfn >> 3;
1271 spmi->pci_function = device->path.pci.devfn & 0x7;
1272 } else if (type != IPMI_INTERFACE_SSIF) {
1273 memcpy(spmi->uid, &uid, sizeof(spmi->uid));
1274 }
1275
1276 spmi->base_address = *addr;
1277 spmi->specification_revision = ipmi_revision;
1278
1279 spmi->interface_type = type;
1280
1281 if (gpe_interrupt >= 0 && gpe_interrupt < 32) {
1282 spmi->gpe = gpe_interrupt;
1283 spmi->interrupt_type |= ACPI_IPMI_INT_TYPE_SCI;
1284 }
1285 if (apic_interrupt > 0) {
1286 spmi->global_system_interrupt = apic_interrupt;
1287 spmi->interrupt_type |= ACPI_IPMI_INT_TYPE_APIC;
1288 }
1289
1290 /* Calculate checksum. */
1291 header->checksum = acpi_checksum((void *)spmi, header->length);
1292}
1293
Timothy Pearsonff8ccf02015-08-11 17:48:32 -05001294void acpi_create_ivrs(acpi_ivrs_t *ivrs,
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001295 unsigned long (*acpi_fill_ivrs)(acpi_ivrs_t *ivrs_struct,
1296 unsigned long current))
Timothy Pearsonff8ccf02015-08-11 17:48:32 -05001297{
1298 acpi_header_t *header = &(ivrs->header);
1299 unsigned long current = (unsigned long)ivrs + sizeof(acpi_ivrs_t);
1300
1301 memset((void *)ivrs, 0, sizeof(acpi_ivrs_t));
1302
John Zhao2ba303e2019-05-28 16:48:14 -07001303 if (!header)
1304 return;
1305
Timothy Pearsonff8ccf02015-08-11 17:48:32 -05001306 /* Fill out header fields. */
1307 memcpy(header->signature, "IVRS", 4);
1308 memcpy(header->oem_id, OEM_ID, 6);
1309 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1310 memcpy(header->asl_compiler_id, ASLC, 4);
1311
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001312 header->asl_compiler_revision = asl_revision;
Timothy Pearsonff8ccf02015-08-11 17:48:32 -05001313 header->length = sizeof(acpi_ivrs_t);
Marc Jones93a51762018-08-22 18:57:24 -06001314 header->revision = get_acpi_table_revision(IVRS);
Timothy Pearsonff8ccf02015-08-11 17:48:32 -05001315
1316 current = acpi_fill_ivrs(ivrs, current);
1317
1318 /* (Re)calculate length and checksum. */
1319 header->length = current - (unsigned long)ivrs;
1320 header->checksum = acpi_checksum((void *)ivrs, header->length);
1321}
1322
Jason Glenesk61624b22020-11-02 20:06:23 -08001323void acpi_create_crat(struct acpi_crat_header *crat,
1324 unsigned long (*acpi_fill_crat)(struct acpi_crat_header *crat_struct,
1325 unsigned long current))
1326{
1327 acpi_header_t *header = &(crat->header);
1328 unsigned long current = (unsigned long)crat + sizeof(struct acpi_crat_header);
1329
1330 memset((void *)crat, 0, sizeof(struct acpi_crat_header));
1331
1332 if (!header)
1333 return;
1334
1335 /* Fill out header fields. */
1336 memcpy(header->signature, "CRAT", 4);
1337 memcpy(header->oem_id, OEM_ID, 6);
1338 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1339 memcpy(header->asl_compiler_id, ASLC, 4);
1340
1341 header->asl_compiler_revision = asl_revision;
1342 header->length = sizeof(struct acpi_crat_header);
1343 header->revision = get_acpi_table_revision(CRAT);
1344
1345 current = acpi_fill_crat(crat, current);
1346
1347 /* (Re)calculate length and checksum. */
1348 header->length = current - (unsigned long)crat;
1349 header->checksum = acpi_checksum((void *)crat, header->length);
1350}
1351
Furquan Shaikh0f007d82020-04-24 06:41:18 -07001352unsigned long acpi_write_hpet(const struct device *device, unsigned long current,
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001353 acpi_rsdp_t *rsdp)
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001354{
1355 acpi_hpet_t *hpet;
1356
1357 /*
1358 * We explicitly add these tables later on:
1359 */
1360 printk(BIOS_DEBUG, "ACPI: * HPET\n");
1361
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001362 hpet = (acpi_hpet_t *)current;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001363 current += sizeof(acpi_hpet_t);
Felix Heldc4697122019-06-20 13:50:17 +02001364 current = ALIGN_UP(current, 16);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001365 acpi_create_hpet(hpet);
1366 acpi_add_table(rsdp, hpet);
1367
1368 return current;
1369}
1370
Arthur Heymans9368cf92023-04-25 16:07:49 +02001371static void acpi_create_dbg2(acpi_dbg2_header_t *dbg2,
Duncan Lauriee4a36c72017-11-11 19:33:25 -08001372 int port_type, int port_subtype,
1373 acpi_addr_t *address, uint32_t address_size,
1374 const char *device_path)
1375{
1376 uintptr_t current;
1377 acpi_dbg2_device_t *device;
1378 uint32_t *dbg2_addr_size;
1379 acpi_header_t *header;
1380 size_t path_len;
1381 const char *path;
1382 char *namespace;
1383
1384 /* Fill out header fields. */
1385 current = (uintptr_t)dbg2;
1386 memset(dbg2, 0, sizeof(acpi_dbg2_header_t));
1387 header = &(dbg2->header);
John Zhao2ba303e2019-05-28 16:48:14 -07001388
1389 if (!header)
1390 return;
1391
Marc Jones93a51762018-08-22 18:57:24 -06001392 header->revision = get_acpi_table_revision(DBG2);
Duncan Lauriee4a36c72017-11-11 19:33:25 -08001393 memcpy(header->signature, "DBG2", 4);
1394 memcpy(header->oem_id, OEM_ID, 6);
1395 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1396 memcpy(header->asl_compiler_id, ASLC, 4);
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001397 header->asl_compiler_revision = asl_revision;
Duncan Lauriee4a36c72017-11-11 19:33:25 -08001398
1399 /* One debug device defined */
1400 dbg2->devices_offset = sizeof(acpi_dbg2_header_t);
1401 dbg2->devices_count = 1;
1402 current += sizeof(acpi_dbg2_header_t);
1403
1404 /* Device comes after the header */
1405 device = (acpi_dbg2_device_t *)current;
1406 memset(device, 0, sizeof(acpi_dbg2_device_t));
1407 current += sizeof(acpi_dbg2_device_t);
1408
1409 device->revision = 0;
1410 device->address_count = 1;
1411 device->port_type = port_type;
1412 device->port_subtype = port_subtype;
1413
1414 /* Base Address comes after device structure */
1415 memcpy((void *)current, address, sizeof(acpi_addr_t));
1416 device->base_address_offset = current - (uintptr_t)device;
1417 current += sizeof(acpi_addr_t);
1418
1419 /* Address Size comes after address structure */
1420 dbg2_addr_size = (uint32_t *)current;
1421 device->address_size_offset = current - (uintptr_t)device;
1422 *dbg2_addr_size = address_size;
1423 current += sizeof(uint32_t);
1424
1425 /* Namespace string comes last, use '.' if not provided */
1426 path = device_path ? : ".";
1427 /* Namespace string length includes NULL terminator */
1428 path_len = strlen(path) + 1;
1429 namespace = (char *)current;
1430 device->namespace_string_length = path_len;
1431 device->namespace_string_offset = current - (uintptr_t)device;
1432 strncpy(namespace, path, path_len);
1433 current += path_len;
1434
1435 /* Update structure lengths and checksum */
1436 device->length = current - (uintptr_t)device;
1437 header->length = current - (uintptr_t)dbg2;
1438 header->checksum = acpi_checksum((uint8_t *)dbg2, header->length);
1439}
1440
1441unsigned long acpi_write_dbg2_pci_uart(acpi_rsdp_t *rsdp, unsigned long current,
Aamir Bohrae825d3f2019-07-30 10:11:41 +05301442 const struct device *dev, uint8_t access_size)
Duncan Lauriee4a36c72017-11-11 19:33:25 -08001443{
1444 acpi_dbg2_header_t *dbg2 = (acpi_dbg2_header_t *)current;
1445 struct resource *res;
1446 acpi_addr_t address;
1447
1448 if (!dev) {
Wim Vervoorn6cd52432020-02-03 14:41:46 +01001449 printk(BIOS_DEBUG, "%s: Device not found\n", __func__);
Duncan Lauriee4a36c72017-11-11 19:33:25 -08001450 return current;
1451 }
Mario Scheithauerf1eb0ea2017-11-21 13:52:53 +01001452 if (!dev->enabled) {
1453 printk(BIOS_INFO, "%s: Device not enabled\n", __func__);
1454 return current;
1455 }
Angel Pons32d09be2021-11-03 13:27:45 +01001456 res = probe_resource(dev, PCI_BASE_ADDRESS_0);
Duncan Lauriee4a36c72017-11-11 19:33:25 -08001457 if (!res) {
1458 printk(BIOS_ERR, "%s: Unable to find resource for %s\n",
1459 __func__, dev_path(dev));
1460 return current;
1461 }
1462
1463 memset(&address, 0, sizeof(address));
1464 if (res->flags & IORESOURCE_IO)
1465 address.space_id = ACPI_ADDRESS_SPACE_IO;
1466 else if (res->flags & IORESOURCE_MEM)
1467 address.space_id = ACPI_ADDRESS_SPACE_MEMORY;
1468 else {
1469 printk(BIOS_ERR, "%s: Unknown address space type\n", __func__);
1470 return current;
1471 }
1472
1473 address.addrl = (uint32_t)res->base;
1474 address.addrh = (uint32_t)((res->base >> 32) & 0xffffffff);
1475 address.access_size = access_size;
1476
1477 acpi_create_dbg2(dbg2,
1478 ACPI_DBG2_PORT_SERIAL,
1479 ACPI_DBG2_PORT_SERIAL_16550,
1480 &address, res->size,
1481 acpi_device_path(dev));
1482
1483 if (dbg2->header.length) {
1484 current += dbg2->header.length;
1485 current = acpi_align_current(current);
1486 acpi_add_table(rsdp, dbg2);
1487 }
1488
1489 return current;
1490}
1491
Arthur Heymans9368cf92023-04-25 16:07:49 +02001492static void acpi_create_facs(acpi_facs_t *facs)
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +00001493{
Uwe Hermann622824c2010-11-19 15:14:42 +00001494 memset((void *)facs, 0, sizeof(acpi_facs_t));
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +00001495
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001496 memcpy(facs->signature, "FACS", 4);
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +00001497 facs->length = sizeof(acpi_facs_t);
1498 facs->hardware_signature = 0;
1499 facs->firmware_waking_vector = 0;
1500 facs->global_lock = 0;
1501 facs->flags = 0;
1502 facs->x_firmware_waking_vector_l = 0;
1503 facs->x_firmware_waking_vector_h = 0;
Marc Jones93a51762018-08-22 18:57:24 -06001504 facs->version = get_acpi_table_revision(FACS);
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +00001505}
Stefan Reinauer777224c2005-01-19 14:06:41 +00001506
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001507static void acpi_write_rsdt(acpi_rsdt_t *rsdt, char *oem_id, char *oem_table_id)
Stefan Reinauer14e22772010-04-27 06:56:47 +00001508{
Uwe Hermann622824c2010-11-19 15:14:42 +00001509 acpi_header_t *header = &(rsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001510
John Zhao2ba303e2019-05-28 16:48:14 -07001511 if (!header)
1512 return;
1513
Uwe Hermann622824c2010-11-19 15:14:42 +00001514 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001515 memcpy(header->signature, "RSDT", 4);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001516 memcpy(header->oem_id, oem_id, 6);
1517 memcpy(header->oem_table_id, oem_table_id, 8);
Stefan Reinauer688b3852004-01-28 16:56:14 +00001518 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001519
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001520 header->asl_compiler_revision = asl_revision;
Stefan Reinauer688b3852004-01-28 16:56:14 +00001521 header->length = sizeof(acpi_rsdt_t);
Marc Jones93a51762018-08-22 18:57:24 -06001522 header->revision = get_acpi_table_revision(RSDT);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001523
Uwe Hermann622824c2010-11-19 15:14:42 +00001524 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauer688b3852004-01-28 16:56:14 +00001525
Uwe Hermann622824c2010-11-19 15:14:42 +00001526 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001527 header->checksum = acpi_checksum((void *)rsdt, sizeof(acpi_rsdt_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +00001528}
1529
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001530static void acpi_write_xsdt(acpi_xsdt_t *xsdt, char *oem_id, char *oem_table_id)
Stefan Reinauer14e22772010-04-27 06:56:47 +00001531{
Uwe Hermann622824c2010-11-19 15:14:42 +00001532 acpi_header_t *header = &(xsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001533
John Zhao2ba303e2019-05-28 16:48:14 -07001534 if (!header)
1535 return;
1536
Uwe Hermann622824c2010-11-19 15:14:42 +00001537 /* Fill out header fields. */
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001538 memcpy(header->signature, "XSDT", 4);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001539 memcpy(header->oem_id, oem_id, 6);
1540 memcpy(header->oem_table_id, oem_table_id, 8);
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001541 memcpy(header->asl_compiler_id, ASLC, 4);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001542
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001543 header->asl_compiler_revision = asl_revision;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001544 header->length = sizeof(acpi_xsdt_t);
Marc Jones93a51762018-08-22 18:57:24 -06001545 header->revision = get_acpi_table_revision(XSDT);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001546
Uwe Hermann622824c2010-11-19 15:14:42 +00001547 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001548
Uwe Hermann622824c2010-11-19 15:14:42 +00001549 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001550 header->checksum = acpi_checksum((void *)xsdt, sizeof(acpi_xsdt_t));
1551}
1552
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001553static void acpi_write_rsdp(acpi_rsdp_t *rsdp, acpi_rsdt_t *rsdt,
1554 acpi_xsdt_t *xsdt, char *oem_id)
Stefan Reinauer688b3852004-01-28 16:56:14 +00001555{
Stefan Reinauerd18faac2009-11-05 18:06:43 +00001556 memset(rsdp, 0, sizeof(acpi_rsdp_t));
Uwe Hermann622824c2010-11-19 15:14:42 +00001557
Stefan Reinauer688b3852004-01-28 16:56:14 +00001558 memcpy(rsdp->signature, RSDP_SIG, 8);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001559 memcpy(rsdp->oem_id, oem_id, 6);
Uwe Hermann622824c2010-11-19 15:14:42 +00001560
1561 rsdp->length = sizeof(acpi_rsdp_t);
Stefan Reinauerdefee172015-06-18 01:11:19 -07001562 rsdp->rsdt_address = (uintptr_t)rsdt;
Uwe Hermann622824c2010-11-19 15:14:42 +00001563
1564 /*
1565 * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2.
1566 *
1567 * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
1568 * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
1569 * revision 0).
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001570 */
1571 if (xsdt == NULL) {
Uwe Hermann622824c2010-11-19 15:14:42 +00001572 rsdp->revision = 0;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001573 } else {
Stefan Reinauerdefee172015-06-18 01:11:19 -07001574 rsdp->xsdt_address = (u64)(uintptr_t)xsdt;
Marc Jones93a51762018-08-22 18:57:24 -06001575 rsdp->revision = get_acpi_table_revision(RSDP);
Stefan Reinauerb657a3c2009-07-21 21:38:33 +00001576 }
Uwe Hermann622824c2010-11-19 15:14:42 +00001577
1578 /* Calculate checksums. */
1579 rsdp->checksum = acpi_checksum((void *)rsdp, 20);
1580 rsdp->ext_checksum = acpi_checksum((void *)rsdp, sizeof(acpi_rsdp_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +00001581}
1582
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001583unsigned long acpi_create_hest_error_source(acpi_hest_t *hest,
1584 acpi_hest_esd_t *esd, u16 type, void *data, u16 data_len)
zbaocaf494c82012-04-13 13:57:14 +08001585{
1586 acpi_header_t *header = &(hest->header);
1587 acpi_hest_hen_t *hen;
1588 void *pos;
1589 u16 len;
1590
1591 pos = esd;
1592 memset(pos, 0, sizeof(acpi_hest_esd_t));
1593 len = 0;
1594 esd->type = type; /* MCE */
1595 esd->source_id = hest->error_source_count;
1596 esd->flags = 0; /* FIRMWARE_FIRST */
1597 esd->enabled = 1;
1598 esd->prealloc_erecords = 1;
1599 esd->max_section_per_record = 0x1;
1600
1601 len += sizeof(acpi_hest_esd_t);
1602 pos = esd + 1;
1603
1604 switch (type) {
1605 case 0: /* MCE */
1606 break;
1607 case 1: /* CMC */
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001608 hen = (acpi_hest_hen_t *)(pos);
zbaocaf494c82012-04-13 13:57:14 +08001609 memset(pos, 0, sizeof(acpi_hest_hen_t));
1610 hen->type = 3; /* SCI? */
1611 hen->length = sizeof(acpi_hest_hen_t);
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001612 hen->conf_we = 0; /* Configuration Write Enable. */
zbaocaf494c82012-04-13 13:57:14 +08001613 hen->poll_interval = 0;
1614 hen->vector = 0;
1615 hen->sw2poll_threshold_val = 0;
1616 hen->sw2poll_threshold_win = 0;
1617 hen->error_threshold_val = 0;
1618 hen->error_threshold_win = 0;
1619 len += sizeof(acpi_hest_hen_t);
1620 pos = hen + 1;
1621 break;
1622 case 2: /* NMI */
1623 case 6: /* AER Root Port */
1624 case 7: /* AER Endpoint */
1625 case 8: /* AER Bridge */
1626 case 9: /* Generic Hardware Error Source. */
1627 /* TODO: */
1628 break;
1629 default:
1630 printk(BIOS_DEBUG, "Invalid type of Error Source.");
1631 break;
1632 }
Lee Leahy024b13d2017-03-16 13:41:11 -07001633 hest->error_source_count++;
zbaocaf494c82012-04-13 13:57:14 +08001634
1635 memcpy(pos, data, data_len);
1636 len += data_len;
John Zhao2ba303e2019-05-28 16:48:14 -07001637 if (header)
1638 header->length += len;
zbaocaf494c82012-04-13 13:57:14 +08001639
1640 return len;
1641}
1642
1643/* ACPI 4.0 */
Vladimir Serbinenko807127f2014-11-09 13:36:18 +01001644void acpi_write_hest(acpi_hest_t *hest,
1645 unsigned long (*acpi_fill_hest)(acpi_hest_t *hest))
zbaocaf494c82012-04-13 13:57:14 +08001646{
1647 acpi_header_t *header = &(hest->header);
1648
1649 memset(hest, 0, sizeof(acpi_hest_t));
1650
John Zhao2ba303e2019-05-28 16:48:14 -07001651 if (!header)
1652 return;
1653
zbaocaf494c82012-04-13 13:57:14 +08001654 memcpy(header->signature, "HEST", 4);
1655 memcpy(header->oem_id, OEM_ID, 6);
1656 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1657 memcpy(header->asl_compiler_id, ASLC, 4);
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001658 header->asl_compiler_revision = asl_revision;
zbaocaf494c82012-04-13 13:57:14 +08001659 header->length += sizeof(acpi_hest_t);
Marc Jones93a51762018-08-22 18:57:24 -06001660 header->revision = get_acpi_table_revision(HEST);
zbaocaf494c82012-04-13 13:57:14 +08001661
1662 acpi_fill_hest(hest);
1663
1664 /* Calculate checksums. */
1665 header->checksum = acpi_checksum((void *)hest, header->length);
1666}
1667
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001668/* ACPI 3.0b */
Arthur Heymans9368cf92023-04-25 16:07:49 +02001669static void acpi_write_bert(acpi_bert_t *bert, uintptr_t region, size_t length)
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001670{
1671 acpi_header_t *header = &(bert->header);
1672
1673 memset(bert, 0, sizeof(acpi_bert_t));
1674
John Zhao2ba303e2019-05-28 16:48:14 -07001675 if (!header)
1676 return;
1677
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001678 memcpy(header->signature, "BERT", 4);
1679 memcpy(header->oem_id, OEM_ID, 6);
1680 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1681 memcpy(header->asl_compiler_id, ASLC, 4);
Elyes HAOUASf8f2e8c2019-02-25 15:42:07 +01001682 header->asl_compiler_revision = asl_revision;
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001683 header->length += sizeof(acpi_bert_t);
1684 header->revision = get_acpi_table_revision(BERT);
1685
1686 bert->error_region = region;
1687 bert->region_length = length;
1688
1689 /* Calculate checksums. */
1690 header->checksum = acpi_checksum((void *)bert, header->length);
1691}
1692
Angel Pons79572e42020-07-13 00:17:43 +02001693__weak void arch_fill_fadt(acpi_fadt_t *fadt) { }
Kyösti Mälkkif9aac922020-05-30 16:16:28 +03001694__weak void soc_fill_fadt(acpi_fadt_t *fadt) { }
Kyösti Mälkki02fd15d2020-06-02 03:34:43 +03001695__weak void mainboard_fill_fadt(acpi_fadt_t *fadt) { }
Kyösti Mälkkif9aac922020-05-30 16:16:28 +03001696
Arthur Heymans9368cf92023-04-25 16:07:49 +02001697static void acpi_create_fadt(acpi_fadt_t *fadt, acpi_facs_t *facs, void *dsdt)
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001698{
1699 acpi_header_t *header = &(fadt->header);
1700
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001701 memset((void *)fadt, 0, sizeof(acpi_fadt_t));
John Zhao2ba303e2019-05-28 16:48:14 -07001702
1703 if (!header)
1704 return;
1705
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001706 memcpy(header->signature, "FACP", 4);
1707 header->length = sizeof(acpi_fadt_t);
Marc Jones93a51762018-08-22 18:57:24 -06001708 header->revision = get_acpi_table_revision(FADT);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001709 memcpy(header->oem_id, OEM_ID, 6);
1710 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1711 memcpy(header->asl_compiler_id, ASLC, 4);
Elyes HAOUAS26071aa2019-02-15 08:21:33 +01001712 header->asl_compiler_revision = asl_revision;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001713
Elyes Haouas532e0432022-02-21 18:13:58 +01001714 fadt->FADT_MinorVersion = get_acpi_fadt_minor_version();
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001715 fadt->firmware_ctrl = (unsigned long)facs;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001716 fadt->x_firmware_ctl_l = (unsigned long)facs;
1717 fadt->x_firmware_ctl_h = 0;
Kyösti Mälkkid4acee82020-06-03 07:20:55 +03001718
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001719 fadt->dsdt = (unsigned long)dsdt;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001720 fadt->x_dsdt_l = (unsigned long)dsdt;
1721 fadt->x_dsdt_h = 0;
1722
Kyösti Mälkkid4acee82020-06-03 07:20:55 +03001723 /* should be 0 ACPI 3.0 */
1724 fadt->reserved = 0;
1725
Kyösti Mälkki67c48a32023-04-14 10:20:03 +03001726 /* P_LVLx latencies are not used as CPU _CST will override them. */
1727 fadt->p_lvl2_lat = ACPI_FADT_C2_NOT_SUPPORTED;
1728 fadt->p_lvl3_lat = ACPI_FADT_C3_NOT_SUPPORTED;
1729
Kyösti Mälkki9ff97972023-04-14 15:37:27 +03001730 /* Use CPU _PTC instead to provide P_CNT details. */
1731 fadt->duty_offset = 0;
1732 fadt->duty_width = 0;
1733
Kyösti Mälkkie0d38682020-06-07 12:01:58 +03001734 fadt->preferred_pm_profile = acpi_get_preferred_pm_profile();
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001735
Nico Huberd5811372021-07-12 18:11:58 +02001736 if (CONFIG(USE_PC_CMOS_ALTCENTURY))
1737 fadt->century = RTC_CLK_ALTCENTURY;
1738
Kyösti Mälkkie742b682023-04-10 17:03:32 +03001739 fadt->sci_int = acpi_sci_int();
1740
Angel Pons79572e42020-07-13 00:17:43 +02001741 arch_fill_fadt(fadt);
1742
Vladimir Serbinenkoab83ef02014-10-25 15:18:25 +02001743 acpi_fill_fadt(fadt);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001744
Kyösti Mälkkif9aac922020-05-30 16:16:28 +03001745 soc_fill_fadt(fadt);
Kyösti Mälkki02fd15d2020-06-02 03:34:43 +03001746 mainboard_fill_fadt(fadt);
Kyösti Mälkkif9aac922020-05-30 16:16:28 +03001747
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001748 header->checksum =
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001749 acpi_checksum((void *)fadt, header->length);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001750}
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001751
Arthur Heymans9368cf92023-04-25 16:07:49 +02001752static void acpi_create_lpit(acpi_lpit_t *lpit)
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001753{
1754 acpi_header_t *header = &(lpit->header);
1755 unsigned long current = (unsigned long)lpit + sizeof(acpi_lpit_t);
1756
1757 memset((void *)lpit, 0, sizeof(acpi_lpit_t));
1758
1759 if (!header)
1760 return;
1761
1762 /* Fill out header fields. */
1763 memcpy(header->signature, "LPIT", 4);
1764 memcpy(header->oem_id, OEM_ID, 6);
1765 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1766 memcpy(header->asl_compiler_id, ASLC, 4);
1767
1768 header->asl_compiler_revision = asl_revision;
1769 header->revision = get_acpi_table_revision(LPIT);
1770 header->oem_revision = 42;
1771 header->length = sizeof(acpi_lpit_t);
1772
1773 current = acpi_fill_lpit(current);
1774
1775 /* (Re)calculate length and checksum. */
1776 header->length = current - (unsigned long)lpit;
1777 header->checksum = acpi_checksum((void *)lpit, header->length);
1778}
1779
1780unsigned long acpi_create_lpi_desc_ncst(acpi_lpi_desc_ncst_t *lpi_desc, uint16_t uid)
1781{
1782 memset(lpi_desc, 0, sizeof(acpi_lpi_desc_ncst_t));
1783 lpi_desc->header.length = sizeof(acpi_lpi_desc_ncst_t);
1784 lpi_desc->header.type = ACPI_LPI_DESC_TYPE_NATIVE_CSTATE;
1785 lpi_desc->header.uid = uid;
1786
1787 return lpi_desc->header.length;
1788}
1789
Aaron Durbin64031672018-04-21 14:45:32 -06001790unsigned long __weak fw_cfg_acpi_tables(unsigned long start)
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001791{
1792 return 0;
1793}
1794
Raul E Rangel6b446b92021-11-19 11:38:35 -07001795void preload_acpi_dsdt(void)
1796{
1797 const char *file = CONFIG_CBFS_PREFIX "/dsdt.aml";
1798
1799 if (!CONFIG(CBFS_PRELOAD))
1800 return;
1801
1802 printk(BIOS_DEBUG, "Preloading %s\n", file);
1803 cbfs_preload(file);
1804}
1805
Arthur Heymans2e7e2d92022-03-03 22:28:27 +01001806static uintptr_t coreboot_rsdp;
1807
1808uintptr_t get_coreboot_rsdp(void)
1809{
1810 return coreboot_rsdp;
1811}
1812
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001813unsigned long write_acpi_tables(unsigned long start)
1814{
1815 unsigned long current;
1816 acpi_rsdp_t *rsdp;
1817 acpi_rsdt_t *rsdt;
1818 acpi_xsdt_t *xsdt;
1819 acpi_fadt_t *fadt;
1820 acpi_facs_t *facs;
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001821 acpi_header_t *slic_file, *slic;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001822 acpi_header_t *ssdt;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001823 acpi_header_t *dsdt_file, *dsdt;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001824 acpi_mcfg_t *mcfg;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +02001825 acpi_tcpa_t *tcpa;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001826 acpi_tpm2_t *tpm2;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001827 acpi_madt_t *madt;
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001828 acpi_lpit_t *lpit;
Francois Toguo522e0db2021-01-21 09:55:19 -08001829 acpi_bert_t *bert;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +11001830 struct device *dev;
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001831 unsigned long fw;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001832 size_t slic_size, dsdt_size;
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001833 char oem_id[6], oem_table_id[8];
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001834
1835 current = start;
1836
1837 /* Align ACPI tables to 16byte */
Aaron Durbin07a1b282015-12-10 17:07:38 -06001838 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001839
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001840 /* Special case for qemu */
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001841 fw = fw_cfg_acpi_tables(current);
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001842 if (fw) {
1843 rsdp = NULL;
1844 /* Find RSDP. */
1845 for (void *p = (void *)current; p < (void *)fw; p += 16) {
1846 if (valid_rsdp((acpi_rsdp_t *)p)) {
1847 rsdp = p;
1848 break;
1849 }
1850 }
1851 if (!rsdp)
1852 return fw;
1853
1854 /* Add BOOT0000 for Linux google firmware driver */
1855 printk(BIOS_DEBUG, "ACPI: * SSDT\n");
1856 ssdt = (acpi_header_t *)fw;
1857 current = (unsigned long)ssdt + sizeof(acpi_header_t);
1858
1859 memset((void *)ssdt, 0, sizeof(acpi_header_t));
1860
1861 memcpy(&ssdt->signature, "SSDT", 4);
1862 ssdt->revision = get_acpi_table_revision(SSDT);
1863 memcpy(&ssdt->oem_id, OEM_ID, 6);
1864 memcpy(&ssdt->oem_table_id, oem_table_id, 8);
1865 ssdt->oem_revision = 42;
1866 memcpy(&ssdt->asl_compiler_id, ASLC, 4);
1867 ssdt->asl_compiler_revision = asl_revision;
1868 ssdt->length = sizeof(acpi_header_t);
1869
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001870 acpigen_set_current((char *)current);
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001871
1872 /* Write object to declare coreboot tables */
1873 acpi_ssdt_write_cbtable();
1874
1875 /* (Re)calculate length and checksum. */
1876 ssdt->length = current - (unsigned long)ssdt;
1877 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
1878
1879 acpi_create_ssdt_generator(ssdt, ACPI_TABLE_CREATOR);
1880
1881 acpi_add_table(rsdp, ssdt);
1882
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001883 return fw;
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001884 }
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001885
Julius Werner834b3ec2020-03-04 16:52:08 -08001886 dsdt_file = cbfs_map(CONFIG_CBFS_PREFIX "/dsdt.aml", &dsdt_size);
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001887 if (!dsdt_file) {
1888 printk(BIOS_ERR, "No DSDT file, skipping ACPI tables\n");
1889 return current;
1890 }
1891
1892 if (dsdt_file->length > dsdt_size
Elyes HAOUAS7d87e762016-10-03 22:11:07 +02001893 || dsdt_file->length < sizeof(acpi_header_t)
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001894 || memcmp(dsdt_file->signature, "DSDT", 4) != 0) {
1895 printk(BIOS_ERR, "Invalid DSDT file, skipping ACPI tables\n");
1896 return current;
1897 }
1898
Julius Werner834b3ec2020-03-04 16:52:08 -08001899 slic_file = cbfs_map(CONFIG_CBFS_PREFIX "/slic", &slic_size);
Vladimir Serbinenko2cb29782015-05-31 11:32:09 +02001900 if (slic_file
1901 && (slic_file->length > slic_size
Elyes HAOUAS7d87e762016-10-03 22:11:07 +02001902 || slic_file->length < sizeof(acpi_header_t)
Benjamin Doron2b2dc0c2020-09-12 02:55:57 +00001903 || (memcmp(slic_file->signature, "SLIC", 4) != 0
1904 && memcmp(slic_file->signature, "MSDM", 4) != 0))) {
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001905 slic_file = 0;
1906 }
1907
1908 if (slic_file) {
1909 memcpy(oem_id, slic_file->oem_id, 6);
1910 memcpy(oem_table_id, slic_file->oem_table_id, 8);
1911 } else {
1912 memcpy(oem_id, OEM_ID, 6);
1913 memcpy(oem_table_id, ACPI_TABLE_CREATOR, 8);
1914 }
1915
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001916 printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx.\n", start);
1917
1918 /* We need at least an RSDP and an RSDT Table */
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001919 rsdp = (acpi_rsdp_t *)current;
Arthur Heymans2e7e2d92022-03-03 22:28:27 +01001920 coreboot_rsdp = (uintptr_t)rsdp;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001921 current += sizeof(acpi_rsdp_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001922 current = acpi_align_current(current);
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001923 rsdt = (acpi_rsdt_t *)current;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001924 current += sizeof(acpi_rsdt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001925 current = acpi_align_current(current);
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001926 xsdt = (acpi_xsdt_t *)current;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001927 current += sizeof(acpi_xsdt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001928 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001929
1930 /* clear all table memory */
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001931 memset((void *)start, 0, current - start);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001932
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001933 acpi_write_rsdp(rsdp, rsdt, xsdt, oem_id);
1934 acpi_write_rsdt(rsdt, oem_id, oem_table_id);
1935 acpi_write_xsdt(xsdt, oem_id, oem_table_id);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001936
1937 printk(BIOS_DEBUG, "ACPI: * FACS\n");
Felix Heldc4697122019-06-20 13:50:17 +02001938 current = ALIGN_UP(current, 64);
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001939 facs = (acpi_facs_t *)current;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001940 current += sizeof(acpi_facs_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001941 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001942 acpi_create_facs(facs);
1943
1944 printk(BIOS_DEBUG, "ACPI: * DSDT\n");
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001945 dsdt = (acpi_header_t *)current;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001946 memcpy(dsdt, dsdt_file, sizeof(acpi_header_t));
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001947 if (dsdt->length >= sizeof(acpi_header_t)) {
1948 current += sizeof(acpi_header_t);
1949
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001950 acpigen_set_current((char *)current);
Kyösti Mälkki2ab4a962020-06-30 11:41:47 +03001951
Kyösti Mälkki37eb24b2021-01-11 18:40:37 +02001952 if (CONFIG(ACPI_SOC_NVS))
1953 acpi_fill_gnvs();
Kyösti Mälkki3dc17922021-03-16 19:01:48 +02001954 if (CONFIG(CHROMEOS_NVS))
1955 acpi_fill_cnvs();
Kyösti Mälkki2ab4a962020-06-30 11:41:47 +03001956
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001957 for (dev = all_devices; dev; dev = dev->next)
Nico Huber68680dd2020-03-31 17:34:52 +02001958 if (dev->ops && dev->ops->acpi_inject_dsdt)
1959 dev->ops->acpi_inject_dsdt(dev);
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001960 current = (unsigned long)acpigen_get_current();
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001961 memcpy((char *)current,
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001962 (char *)dsdt_file + sizeof(acpi_header_t),
Vladimir Serbinenko334fd8e2014-10-05 11:10:35 +02001963 dsdt->length - sizeof(acpi_header_t));
1964 current += dsdt->length - sizeof(acpi_header_t);
1965
1966 /* (Re)calculate length and checksum. */
1967 dsdt->length = current - (unsigned long)dsdt;
1968 dsdt->checksum = 0;
1969 dsdt->checksum = acpi_checksum((void *)dsdt, dsdt->length);
1970 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001971
Aaron Durbin07a1b282015-12-10 17:07:38 -06001972 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001973
1974 printk(BIOS_DEBUG, "ACPI: * FADT\n");
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001975 fadt = (acpi_fadt_t *)current;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001976 current += sizeof(acpi_fadt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001977 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001978
1979 acpi_create_fadt(fadt, facs, dsdt);
1980 acpi_add_table(rsdp, fadt);
1981
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001982 if (slic_file) {
1983 printk(BIOS_DEBUG, "ACPI: * SLIC\n");
1984 slic = (acpi_header_t *)current;
1985 memcpy(slic, slic_file, slic_file->length);
1986 current += slic_file->length;
Aaron Durbin07a1b282015-12-10 17:07:38 -06001987 current = acpi_align_current(current);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001988 acpi_add_table(rsdp, slic);
1989 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001990
1991 printk(BIOS_DEBUG, "ACPI: * SSDT\n");
1992 ssdt = (acpi_header_t *)current;
1993 acpi_create_ssdt_generator(ssdt, ACPI_TABLE_CREATOR);
Vladimir Serbinenko9310df82014-10-10 20:40:41 +02001994 if (ssdt->length > sizeof(acpi_header_t)) {
1995 current += ssdt->length;
1996 acpi_add_table(rsdp, ssdt);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001997 current = acpi_align_current(current);
Vladimir Serbinenko9310df82014-10-10 20:40:41 +02001998 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001999
2000 printk(BIOS_DEBUG, "ACPI: * MCFG\n");
Elyes Haouas3141fbad2022-11-18 15:22:32 +01002001 mcfg = (acpi_mcfg_t *)current;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02002002 acpi_create_mcfg(mcfg);
2003 if (mcfg->header.length > sizeof(acpi_mcfg_t)) {
2004 current += mcfg->header.length;
Aaron Durbin07a1b282015-12-10 17:07:38 -06002005 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02002006 acpi_add_table(rsdp, mcfg);
2007 }
2008
Julius Wernercd49cce2019-03-05 16:53:33 -08002009 if (CONFIG(TPM1)) {
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02002010 printk(BIOS_DEBUG, "ACPI: * TCPA\n");
Elyes Haouas3141fbad2022-11-18 15:22:32 +01002011 tcpa = (acpi_tcpa_t *)current;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02002012 acpi_create_tcpa(tcpa);
2013 if (tcpa->header.length >= sizeof(acpi_tcpa_t)) {
2014 current += tcpa->header.length;
2015 current = acpi_align_current(current);
2016 acpi_add_table(rsdp, tcpa);
2017 }
2018 }
2019
Julius Wernercd49cce2019-03-05 16:53:33 -08002020 if (CONFIG(TPM2)) {
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02002021 printk(BIOS_DEBUG, "ACPI: * TPM2\n");
Elyes Haouas3141fbad2022-11-18 15:22:32 +01002022 tpm2 = (acpi_tpm2_t *)current;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02002023 acpi_create_tpm2(tpm2);
2024 if (tpm2->header.length >= sizeof(acpi_tpm2_t)) {
2025 current += tpm2->header.length;
2026 current = acpi_align_current(current);
2027 acpi_add_table(rsdp, tpm2);
2028 }
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +02002029 }
2030
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01002031 if (CONFIG(ACPI_LPIT)) {
2032 printk(BIOS_DEBUG, "ACPI: * LPIT\n");
2033
2034 lpit = (acpi_lpit_t *)current;
2035 acpi_create_lpit(lpit);
2036 if (lpit->header.length >= sizeof(acpi_lpit_t)) {
2037 current += lpit->header.length;
2038 current = acpi_align_current(current);
2039 acpi_add_table(rsdp, lpit);
2040 }
2041 }
2042
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02002043 printk(BIOS_DEBUG, "ACPI: * MADT\n");
2044
Elyes Haouas3141fbad2022-11-18 15:22:32 +01002045 madt = (acpi_madt_t *)current;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02002046 acpi_create_madt(madt);
2047 if (madt->header.length > sizeof(acpi_madt_t)) {
Lee Leahy024b13d2017-03-16 13:41:11 -07002048 current += madt->header.length;
2049 acpi_add_table(rsdp, madt);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02002050 }
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01002051
Aaron Durbin07a1b282015-12-10 17:07:38 -06002052 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02002053
Felix Heldfba479262021-05-28 16:11:43 +02002054 if (CONFIG(ACPI_BERT)) {
Francois Toguo522e0db2021-01-21 09:55:19 -08002055 void *region;
2056 size_t size;
Elyes Haouas3141fbad2022-11-18 15:22:32 +01002057 bert = (acpi_bert_t *)current;
Felix Heldfba479262021-05-28 16:11:43 +02002058 if (acpi_soc_get_bert_region(&region, &size) == CB_SUCCESS) {
2059 printk(BIOS_DEBUG, "ACPI: * BERT\n");
2060 acpi_write_bert(bert, (uintptr_t)region, size);
2061 if (bert->header.length >= sizeof(acpi_bert_t)) {
2062 current += bert->header.length;
2063 acpi_add_table(rsdp, bert);
2064 }
2065 current = acpi_align_current(current);
Francois Toguo522e0db2021-01-21 09:55:19 -08002066 }
Francois Toguo522e0db2021-01-21 09:55:19 -08002067 }
2068
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02002069 printk(BIOS_DEBUG, "current = %lx\n", current);
2070
2071 for (dev = all_devices; dev; dev = dev->next) {
2072 if (dev->ops && dev->ops->write_acpi_tables) {
Lee Leahy6f80ccc2017-03-16 15:18:22 -07002073 current = dev->ops->write_acpi_tables(dev, current,
2074 rsdp);
Aaron Durbin07a1b282015-12-10 17:07:38 -06002075 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02002076 }
2077 }
2078
2079 printk(BIOS_INFO, "ACPI: done.\n");
2080 return current;
2081}
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02002082
Rudolf Marek33cafe52009-04-13 18:07:02 +00002083static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp)
2084{
2085 if (strncmp((char *)rsdp, RSDP_SIG, sizeof(RSDP_SIG) - 1) != 0)
2086 return NULL;
2087
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00002088 printk(BIOS_DEBUG, "Looking on %p for valid checksum\n", rsdp);
Rudolf Marek33cafe52009-04-13 18:07:02 +00002089
2090 if (acpi_checksum((void *)rsdp, 20) != 0)
2091 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00002092 printk(BIOS_DEBUG, "Checksum 1 passed\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00002093
2094 if ((rsdp->revision > 1) && (acpi_checksum((void *)rsdp,
2095 rsdp->length) != 0))
2096 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00002097 printk(BIOS_DEBUG, "Checksum 2 passed all OK\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00002098
2099 return rsdp;
2100}
2101
Rudolf Marek33cafe52009-04-13 18:07:02 +00002102void *acpi_find_wakeup_vector(void)
2103{
2104 char *p, *end;
Rudolf Marek33cafe52009-04-13 18:07:02 +00002105 acpi_rsdt_t *rsdt;
2106 acpi_facs_t *facs;
Martin Rothc5f49262012-12-14 19:17:55 -07002107 acpi_fadt_t *fadt = NULL;
Kyösti Mälkki072d436b2016-06-17 08:44:40 +03002108 acpi_rsdp_t *rsdp = NULL;
Uwe Hermann622824c2010-11-19 15:14:42 +00002109 void *wake_vec;
Rudolf Marek33cafe52009-04-13 18:07:02 +00002110 int i;
2111
Kyösti Mälkkib8c7ea02020-11-17 16:41:38 +02002112 if (!acpi_is_wakeup_s3())
Rudolf Marek33cafe52009-04-13 18:07:02 +00002113 return NULL;
2114
Uwe Hermann622824c2010-11-19 15:14:42 +00002115 printk(BIOS_DEBUG, "Trying to find the wakeup vector...\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00002116
Uwe Hermann622824c2010-11-19 15:14:42 +00002117 /* Find RSDP. */
2118 for (p = (char *)0xe0000; p < (char *)0xfffff; p += 16) {
Lee Leahy0b5678f2017-03-16 16:01:40 -07002119 rsdp = valid_rsdp((acpi_rsdp_t *)p);
2120 if (rsdp)
Rudolf Marek33cafe52009-04-13 18:07:02 +00002121 break;
2122 }
2123
Angel Pons98a68ad2018-11-04 12:25:25 +01002124 if (rsdp == NULL) {
2125 printk(BIOS_ALERT,
2126 "No RSDP found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00002127 return NULL;
Angel Pons98a68ad2018-11-04 12:25:25 +01002128 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00002129
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00002130 printk(BIOS_DEBUG, "RSDP found at %p\n", rsdp);
Stefan Reinauer71a30182015-07-30 16:28:13 -07002131 rsdt = (acpi_rsdt_t *)(uintptr_t)rsdp->rsdt_address;
Stefan Reinauer14e22772010-04-27 06:56:47 +00002132
Uwe Hermann622824c2010-11-19 15:14:42 +00002133 end = (char *)rsdt + rsdt->header.length;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00002134 printk(BIOS_DEBUG, "RSDT found at %p ends at %p\n", rsdt, end);
Rudolf Marek33cafe52009-04-13 18:07:02 +00002135
Uwe Hermann622824c2010-11-19 15:14:42 +00002136 for (i = 0; ((char *)&rsdt->entry[i]) < end; i++) {
Stefan Reinauer71a30182015-07-30 16:28:13 -07002137 fadt = (acpi_fadt_t *)(uintptr_t)rsdt->entry[i];
Stefan Reinauercdfe3762009-07-21 22:15:43 +00002138 if (strncmp((char *)fadt, "FACP", 4) == 0)
Rudolf Marek33cafe52009-04-13 18:07:02 +00002139 break;
2140 fadt = NULL;
2141 }
2142
Angel Pons98a68ad2018-11-04 12:25:25 +01002143 if (fadt == NULL) {
2144 printk(BIOS_ALERT,
2145 "No FADT found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00002146 return NULL;
Angel Pons98a68ad2018-11-04 12:25:25 +01002147 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00002148
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00002149 printk(BIOS_DEBUG, "FADT found at %p\n", fadt);
Stefan Reinauer71a30182015-07-30 16:28:13 -07002150 facs = (acpi_facs_t *)(uintptr_t)fadt->firmware_ctrl;
Rudolf Marek33cafe52009-04-13 18:07:02 +00002151
Stefan Reinauer7e9771c2009-04-22 08:17:38 +00002152 if (facs == NULL) {
Angel Pons98a68ad2018-11-04 12:25:25 +01002153 printk(BIOS_ALERT,
2154 "No FACS found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00002155 return NULL;
Stefan Reinauer7e9771c2009-04-22 08:17:38 +00002156 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00002157
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00002158 printk(BIOS_DEBUG, "FACS found at %p\n", facs);
Stefan Reinauer71a30182015-07-30 16:28:13 -07002159 wake_vec = (void *)(uintptr_t)facs->firmware_waking_vector;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00002160 printk(BIOS_DEBUG, "OS waking vector is %p\n", wake_vec);
Uwe Hermann622824c2010-11-19 15:14:42 +00002161
Rudolf Marek33cafe52009-04-13 18:07:02 +00002162 return wake_vec;
2163}
2164
Aaron Durbin64031672018-04-21 14:45:32 -06002165__weak int acpi_get_gpe(int gpe)
Duncan Laurie1f6e6812016-09-19 12:04:19 -07002166{
2167 return -1; /* implemented by SOC */
2168}
Marc Jones93a51762018-08-22 18:57:24 -06002169
Elyes Haouas8b950f42022-02-16 12:08:16 +01002170u8 get_acpi_fadt_minor_version(void)
2171{
2172 return ACPI_FADT_MINOR_VERSION_0;
2173}
2174
Marc Jones93a51762018-08-22 18:57:24 -06002175int get_acpi_table_revision(enum acpi_tables table)
2176{
2177 switch (table) {
2178 case FADT:
Elyes Haouas8b950f42022-02-16 12:08:16 +01002179 return ACPI_FADT_REV_ACPI_6;
Elyes HAOUASf288b392018-11-11 15:22:30 +01002180 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 +01002181 return 3;
Marc Jones93a51762018-08-22 18:57:24 -06002182 case MCFG:
2183 return 1;
2184 case TCPA:
2185 return 2;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02002186 case TPM2:
2187 return 4;
Martin Roth0949e732021-10-01 14:28:22 -06002188 case SSDT: /* ACPI 3.0 up to 6.3: 2 */
Marc Jones93a51762018-08-22 18:57:24 -06002189 return 2;
Jonathan Zhanga3311b92022-12-01 17:13:41 -08002190 case SRAT: /* ACPI 2.0: 1, ACPI 3.0: 2, ACPI 4.0 up to 6.4: 3 */
2191 return 3;
Jonathan Zhang2a4e1f42021-04-01 11:43:37 -07002192 case HMAT: /* ACPI 6.4: 2 */
2193 return 2;
Marc Jones93a51762018-08-22 18:57:24 -06002194 case DMAR:
2195 return 1;
Martin Roth0949e732021-10-01 14:28:22 -06002196 case SLIT: /* ACPI 2.0 up to 6.3: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06002197 return 1;
Patrick Rudolph9d98e5a2019-06-14 19:00:04 +02002198 case SPMI: /* IMPI 2.0 */
2199 return 5;
Marc Jones93a51762018-08-22 18:57:24 -06002200 case HPET: /* Currently 1. Table added in ACPI 2.0. */
2201 return 1;
Elyes HAOUASf288b392018-11-11 15:22:30 +01002202 case VFCT: /* ACPI 2.0/3.0/4.0: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06002203 return 1;
2204 case IVRS:
Jason Glenesk1916f892020-07-24 02:51:30 -07002205 return IVRS_FORMAT_MIXED;
Marc Jones93a51762018-08-22 18:57:24 -06002206 case DBG2:
2207 return 0;
Martin Roth0949e732021-10-01 14:28:22 -06002208 case FACS: /* ACPI 2.0/3.0: 1, ACPI 4.0 up to 6.3: 2 */
Marc Jones93a51762018-08-22 18:57:24 -06002209 return 1;
Martin Roth0949e732021-10-01 14:28:22 -06002210 case RSDT: /* ACPI 1.0 up to 6.3: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06002211 return 1;
Martin Roth0949e732021-10-01 14:28:22 -06002212 case XSDT: /* ACPI 2.0 up to 6.3: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06002213 return 1;
Martin Roth0949e732021-10-01 14:28:22 -06002214 case RSDP: /* ACPI 2.0 up to 6.3: 2 */
Marc Jones93a51762018-08-22 18:57:24 -06002215 return 2;
Rocky Phaguraeff07132021-01-10 15:42:50 -08002216 case EINJ:
2217 return 1;
Marc Jones93a51762018-08-22 18:57:24 -06002218 case HEST:
2219 return 1;
2220 case NHLT:
2221 return 5;
Marshall Dawson44705c62018-09-04 13:47:15 -06002222 case BERT:
2223 return 1;
Jonathan Zhanga3311b92022-12-01 17:13:41 -08002224 case CEDT: /* CXL 3.0 section 9.17.1 */
2225 return 1;
Jason Glenesk61624b22020-11-02 20:06:23 -08002226 case CRAT:
2227 return 1;
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01002228 case LPIT: /* ACPI 5.1 up to 6.3: 0 */
2229 return 0;
Marc Jones93a51762018-08-22 18:57:24 -06002230 default:
2231 return -1;
2232 }
2233 return -1;
2234}