blob: 65be82eed0b01a8e42f3c59d935ba47f89d20e0f [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>
Jianjun Wang8565b94a2022-03-02 10:20:26 +080019#include <cbfs.h>
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000020#include <cbmem.h>
Elyes HAOUASe2d152c2019-06-21 07:06:50 +020021#include <commonlib/helpers.h>
Jianjun Wang8565b94a2022-03-02 10:20:26 +080022#include <console/console.h>
Duncan Laurie11290c42012-10-03 19:07:05 -070023#include <cpu/cpu.h>
Jianjun Wang8565b94a2022-03-02 10:20:26 +080024#include <device/mmio.h>
25#include <device/pci.h>
Jianjun Wang8565b94a2022-03-02 10:20:26 +080026#include <string.h>
Elyes HAOUAScdd2f632021-02-11 19:37:11 +010027#include <types.h>
Elyes HAOUAS26071aa2019-02-15 08:21:33 +010028#include <version.h>
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000029
Kyösti Mälkkic7da0272021-06-08 11:37:08 +030030#if ENV_X86
31#include <arch/ioapic.h>
Arthur Heymans92a3b672023-06-22 21:30:58 +020032#include <arch/smp/mpspec.h>
Kyösti Mälkkic7da0272021-06-08 11:37:08 +030033#endif
34
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +020035static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp);
36
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000037u8 acpi_checksum(u8 *table, u32 length)
Stefan Reinauer688b3852004-01-28 16:56:14 +000038{
Uwe Hermann622824c2010-11-19 15:14:42 +000039 u8 ret = 0;
Stefan Reinauer688b3852004-01-28 16:56:14 +000040 while (length--) {
41 ret += *table;
42 table++;
43 }
Stefan Reinauera7648c22004-01-29 17:31:34 +000044 return -ret;
Stefan Reinauer688b3852004-01-28 16:56:14 +000045}
46
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000047/**
Uwe Hermann622824c2010-11-19 15:14:42 +000048 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
49 * and checksum.
Stefan Reinauer06feb882004-02-03 16:11:35 +000050 */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000051void acpi_add_table(acpi_rsdp_t *rsdp, void *table)
Stefan Reinauer688b3852004-01-28 16:56:14 +000052{
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000053 int i, entries_num;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000054 acpi_rsdt_t *rsdt;
55 acpi_xsdt_t *xsdt = NULL;
56
Uwe Hermann622824c2010-11-19 15:14:42 +000057 /* The RSDT is mandatory... */
Stefan Reinauerdefee172015-06-18 01:11:19 -070058 rsdt = (acpi_rsdt_t *)(uintptr_t)rsdp->rsdt_address;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000059
Uwe Hermann622824c2010-11-19 15:14:42 +000060 /* ...while the XSDT is not. */
61 if (rsdp->xsdt_address)
Stefan Reinauerdefee172015-06-18 01:11:19 -070062 xsdt = (acpi_xsdt_t *)((uintptr_t)rsdp->xsdt_address);
Stefan Reinauer14e22772010-04-27 06:56:47 +000063
Uwe Hermann622824c2010-11-19 15:14:42 +000064 /* This should always be MAX_ACPI_TABLES. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000065 entries_num = ARRAY_SIZE(rsdt->entry);
Stefan Reinauer14e22772010-04-27 06:56:47 +000066
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000067 for (i = 0; i < entries_num; i++) {
Uwe Hermann622824c2010-11-19 15:14:42 +000068 if (rsdt->entry[i] == 0)
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000069 break;
Stefan Reinauer688b3852004-01-28 16:56:14 +000070 }
71
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000072 if (i >= entries_num) {
Uwe Hermann622824c2010-11-19 15:14:42 +000073 printk(BIOS_ERR, "ACPI: Error: Could not add ACPI table, "
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +030074 "too many tables.\n");
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000075 return;
76 }
77
Uwe Hermann622824c2010-11-19 15:14:42 +000078 /* Add table to the RSDT. */
Stefan Reinauerdefee172015-06-18 01:11:19 -070079 rsdt->entry[i] = (uintptr_t)table;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000080
Uwe Hermann622824c2010-11-19 15:14:42 +000081 /* Fix RSDT length or the kernel will assume invalid entries. */
82 rsdt->header.length = sizeof(acpi_header_t) + (sizeof(u32) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000083
Uwe Hermann622824c2010-11-19 15:14:42 +000084 /* Re-calculate checksum. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000085 rsdt->header.checksum = 0; /* Hope this won't get optimized away */
Uwe Hermann622824c2010-11-19 15:14:42 +000086 rsdt->header.checksum = acpi_checksum((u8 *)rsdt, rsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000087
Uwe Hermann622824c2010-11-19 15:14:42 +000088 /*
89 * And now the same thing for the XSDT. We use the same index as for
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000090 * now we want the XSDT and RSDT to always be in sync in coreboot.
91 */
92 if (xsdt) {
Uwe Hermann622824c2010-11-19 15:14:42 +000093 /* Add table to the XSDT. */
Stefan Reinauerdefee172015-06-18 01:11:19 -070094 xsdt->entry[i] = (u64)(uintptr_t)table;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000095
Uwe Hermann622824c2010-11-19 15:14:42 +000096 /* Fix XSDT length. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000097 xsdt->header.length = sizeof(acpi_header_t) +
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +030098 (sizeof(u64) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000099
Uwe Hermann622824c2010-11-19 15:14:42 +0000100 /* Re-calculate checksum. */
101 xsdt->header.checksum = 0;
102 xsdt->header.checksum = acpi_checksum((u8 *)xsdt,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300103 xsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +0000104 }
105
Uwe Hermann622824c2010-11-19 15:14:42 +0000106 printk(BIOS_DEBUG, "ACPI: added table %d/%d, length now %d\n",
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300107 i + 1, entries_num, rsdt->header.length);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000108}
109
Arthur Heymansadb80072023-06-28 09:56:00 +0200110static enum cb_err acpi_fill_header(acpi_header_t *header, const char name[4],
111 enum acpi_tables table, uint32_t size)
112{
113 if (!header)
114 return CB_ERR;
115
116 /* Fill out header fields. */
117 memcpy(header->signature, name, 4);
118 memcpy(header->oem_id, OEM_ID, 6);
119 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
120 memcpy(header->asl_compiler_id, ASLC, 4);
121
122 header->asl_compiler_revision = asl_revision;
123 header->revision = get_acpi_table_revision(table);
124 header->length = size;
125
126 return CB_SUCCESS;
127}
128
Arthur Heymans9362dd72023-06-08 19:56:51 +0200129static int acpi_create_mcfg_mmconfig(acpi_mcfg_mmconfig_t *mmconfig, u32 base,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300130 u16 seg_nr, u8 start, u8 end)
Stefan Reinauer26d431a2009-01-20 19:17:51 +0000131{
Vladimir Serbinenko60fccdc2014-10-05 11:05:16 +0200132 memset(mmconfig, 0, sizeof(*mmconfig));
Rudolf Mareke6409f22007-11-03 12:50:26 +0000133 mmconfig->base_address = base;
134 mmconfig->base_reserved = 0;
135 mmconfig->pci_segment_group_number = seg_nr;
136 mmconfig->start_bus_number = start;
137 mmconfig->end_bus_number = end;
Uwe Hermann622824c2010-11-19 15:14:42 +0000138
139 return sizeof(acpi_mcfg_mmconfig_t);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000140}
141
Kyösti Mälkkie742b682023-04-10 17:03:32 +0300142static u16 acpi_sci_int(void)
Kyösti Mälkkiae1b2d42023-04-10 16:45:39 +0300143{
144#if ENV_X86
145 u8 gsi, irq, flags;
146
147 ioapic_get_sci_pin(&gsi, &irq, &flags);
148
149 /* ACPI Release 6.5, 5.2.9 and 5.2.15.5. */
150 if (!CONFIG(ACPI_HAVE_PCAT_8259))
151 return gsi;
152
153 assert(irq < 16);
154 return irq;
155#else
156 return 0;
157#endif
158}
159
Arthur Heymans01af0f82023-06-27 11:58:04 +0200160static void acpi_create_madt(acpi_header_t *header, void *unused)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000161{
Arthur Heymans01af0f82023-06-27 11:58:04 +0200162 acpi_madt_t *madt = (acpi_madt_t *)header;
Uwe Hermann622824c2010-11-19 15:14:42 +0000163 unsigned long current = (unsigned long)madt + sizeof(acpi_madt_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000164
Arthur Heymansadb80072023-06-28 09:56:00 +0200165 if (acpi_fill_header(header, "APIC", MADT, sizeof(acpi_madt_t)) != CB_SUCCESS)
John Zhao2ba303e2019-05-28 16:48:14 -0700166 return;
167
Furquan Shaikhb1859a62020-04-30 21:27:47 -0700168 madt->lapic_addr = cpu_get_lapic_addr();
Nico Huber9df72e02018-11-24 18:25:50 +0100169 if (CONFIG(ACPI_HAVE_PCAT_8259))
170 madt->flags |= 1;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000171
Kyösti Mälkki69a13962023-04-08 14:10:48 +0300172 if (CONFIG(ACPI_COMMON_MADT_LAPIC))
173 current = acpi_create_madt_lapics_with_nmis(current);
174
Kyösti Mälkki10bdee12023-04-11 01:00:17 +0300175 if (CONFIG(ACPI_COMMON_MADT_IOAPIC))
176 current = acpi_create_madt_ioapic_gsi0_default(current);
177
178 if (CONFIG(ACPI_CUSTOM_MADT))
Kyösti Mälkkia5fa5342022-11-18 13:23:52 +0200179 current = acpi_fill_madt(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000180
Arthur Heymans01af0f82023-06-27 11:58:04 +0200181 /* (Re)calculate length . */
Uwe Hermann622824c2010-11-19 15:14:42 +0000182 header->length = current - (unsigned long)madt;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000183}
184
Kyösti Mälkkib54388d2021-02-14 15:09:45 +0200185static unsigned long acpi_fill_mcfg(unsigned long current)
186{
187 current += acpi_create_mcfg_mmconfig((acpi_mcfg_mmconfig_t *)current,
Shelley Chen4e9bb332021-10-20 15:43:45 -0700188 CONFIG_ECAM_MMCONF_BASE_ADDRESS, 0, 0,
189 CONFIG_ECAM_MMCONF_BUS_NUMBER - 1);
Kyösti Mälkkib54388d2021-02-14 15:09:45 +0200190 return current;
191}
192
Uwe Hermann622824c2010-11-19 15:14:42 +0000193/* MCFG is defined in the PCI Firmware Specification 3.0. */
Arthur Heymans01af0f82023-06-27 11:58:04 +0200194static void acpi_create_mcfg(acpi_header_t *header, void *unused)
Rudolf Mareke6409f22007-11-03 12:50:26 +0000195{
Arthur Heymans01af0f82023-06-27 11:58:04 +0200196 acpi_mcfg_t *mcfg = (acpi_mcfg_t *)header;
Uwe Hermann622824c2010-11-19 15:14:42 +0000197 unsigned long current = (unsigned long)mcfg + sizeof(acpi_mcfg_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000198
Arthur Heymansadb80072023-06-28 09:56:00 +0200199
200 if (acpi_fill_header(header, "MCFG", MCFG, sizeof(acpi_mcfg_t)) != CB_SUCCESS)
John Zhao2ba303e2019-05-28 16:48:14 -0700201 return;
202
Shelley Chen4e9bb332021-10-20 15:43:45 -0700203 if (CONFIG(ECAM_MMCONF_SUPPORT))
Kyösti Mälkkib54388d2021-02-14 15:09:45 +0200204 current = acpi_fill_mcfg(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000205
Arthur Heymans01af0f82023-06-27 11:58:04 +0200206 /* (Re)calculate length */
Uwe Hermann622824c2010-11-19 15:14:42 +0000207 header->length = current - (unsigned long)mcfg;
Rudolf Mareke6409f22007-11-03 12:50:26 +0000208}
209
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200210static void *get_tcpa_log(u32 *size)
211{
212 const struct cbmem_entry *ce;
213 const u32 tcpa_default_log_len = 0x10000;
214 void *lasa;
Philipp Deppenwiese791ba972018-07-30 01:15:20 +0200215 ce = cbmem_entry_find(CBMEM_ID_TCPA_TCG_LOG);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200216 if (ce) {
217 lasa = cbmem_entry_start(ce);
218 *size = cbmem_entry_size(ce);
219 printk(BIOS_DEBUG, "TCPA log found at %p\n", lasa);
220 return lasa;
221 }
Philipp Deppenwiese791ba972018-07-30 01:15:20 +0200222 lasa = cbmem_add(CBMEM_ID_TCPA_TCG_LOG, tcpa_default_log_len);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200223 if (!lasa) {
224 printk(BIOS_ERR, "TCPA log creation failed\n");
225 return NULL;
226 }
227
228 printk(BIOS_DEBUG, "TCPA log created at %p\n", lasa);
Lee Leahy024b13d2017-03-16 13:41:11 -0700229 memset(lasa, 0, tcpa_default_log_len);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200230
231 *size = tcpa_default_log_len;
232 return lasa;
233}
234
Arthur Heymans01af0f82023-06-27 11:58:04 +0200235static void acpi_create_tcpa(acpi_header_t *header, void *unused)
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200236{
Arthur Heymans01af0f82023-06-27 11:58:04 +0200237 if (!CONFIG(TPM1))
238 return;
239
240 acpi_tcpa_t *tcpa = (acpi_tcpa_t *)header;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200241 u32 tcpa_log_len;
242 void *lasa;
243
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200244 lasa = get_tcpa_log(&tcpa_log_len);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700245 if (!lasa)
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200246 return;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200247
Arthur Heymansadb80072023-06-28 09:56:00 +0200248 if (acpi_fill_header(header, "TCPA", TCPA, sizeof(acpi_tcpa_t)) != CB_SUCCESS)
John Zhao2ba303e2019-05-28 16:48:14 -0700249 return;
250
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200251 tcpa->platform_class = 0;
252 tcpa->laml = tcpa_log_len;
Elyes Haouas3141fbad2022-11-18 15:22:32 +0100253 tcpa->lasa = (uintptr_t)lasa;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200254}
255
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100256static void *get_tpm2_log(u32 *size)
257{
258 const struct cbmem_entry *ce;
259 const u32 tpm2_default_log_len = 0x10000;
260 void *lasa;
261 ce = cbmem_entry_find(CBMEM_ID_TPM2_TCG_LOG);
262 if (ce) {
263 lasa = cbmem_entry_start(ce);
264 *size = cbmem_entry_size(ce);
265 printk(BIOS_DEBUG, "TPM2 log found at %p\n", lasa);
266 return lasa;
267 }
268 lasa = cbmem_add(CBMEM_ID_TPM2_TCG_LOG, tpm2_default_log_len);
269 if (!lasa) {
270 printk(BIOS_ERR, "TPM2 log creation failed\n");
271 return NULL;
272 }
273
274 printk(BIOS_DEBUG, "TPM2 log created at %p\n", lasa);
275 memset(lasa, 0, tpm2_default_log_len);
276
277 *size = tpm2_default_log_len;
278 return lasa;
279}
280
Arthur Heymans01af0f82023-06-27 11:58:04 +0200281static void acpi_create_tpm2(acpi_header_t *header, void *unused)
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200282{
Arthur Heymans01af0f82023-06-27 11:58:04 +0200283 if (!CONFIG(TPM2))
284 return;
285
286 acpi_tpm2_t *tpm2 = (acpi_tpm2_t *)header;
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100287 u32 tpm2_log_len;
288 void *lasa;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200289
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100290 /*
291 * Some payloads like SeaBIOS depend on log area to use TPM2.
292 * Get the memory size and address of TPM2 log area or initialize it.
293 */
294 lasa = get_tpm2_log(&tpm2_log_len);
295 if (!lasa)
296 tpm2_log_len = 0;
297
Arthur Heymansadb80072023-06-28 09:56:00 +0200298 if (acpi_fill_header(header, "TPM2", TPM2, sizeof(acpi_tpm2_t)) != CB_SUCCESS)
John Zhao2ba303e2019-05-28 16:48:14 -0700299 return;
300
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200301 /* Hard to detect for coreboot. Just set it to 0 */
302 tpm2->platform_class = 0;
Christian Walter26e0d4c2019-07-14 15:47:23 +0200303 if (CONFIG(CRB_TPM)) {
304 /* Must be set to 7 for CRB Support */
305 tpm2->control_area = CONFIG_CRB_TPM_BASE_ADDRESS + 0x40;
306 tpm2->start_method = 7;
307 } else {
308 /* Must be set to 0 for FIFO interface support */
309 tpm2->control_area = 0;
310 tpm2->start_method = 6;
311 }
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200312 memset(tpm2->msp, 0, sizeof(tpm2->msp));
313
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100314 /* Fill the log area size and start address fields. */
315 tpm2->laml = tpm2_log_len;
Elyes Haouas3141fbad2022-11-18 15:22:32 +0100316 tpm2->lasa = (uintptr_t)lasa;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200317}
318
Duncan Laurie37319032016-05-26 12:47:05 -0700319static void acpi_ssdt_write_cbtable(void)
320{
321 const struct cbmem_entry *cbtable;
322 uintptr_t base;
323 uint32_t size;
324
325 cbtable = cbmem_entry_find(CBMEM_ID_CBTABLE);
326 if (!cbtable)
327 return;
328 base = (uintptr_t)cbmem_entry_start(cbtable);
329 size = cbmem_entry_size(cbtable);
330
331 acpigen_write_device("CTBL");
332 acpigen_write_coreboot_hid(COREBOOT_ACPI_ID_CBTABLE);
333 acpigen_write_name_integer("_UID", 0);
David Wu5dff3962018-03-21 16:48:53 +0800334 acpigen_write_STA(ACPI_STATUS_DEVICE_HIDDEN_ON);
Duncan Laurie37319032016-05-26 12:47:05 -0700335 acpigen_write_name("_CRS");
336 acpigen_write_resourcetemplate_header();
337 acpigen_write_mem32fixed(0, base, size);
338 acpigen_write_resourcetemplate_footer();
339 acpigen_pop_len();
340}
341
Arthur Heymans01af0f82023-06-27 11:58:04 +0200342static void acpi_create_ssdt_generator(acpi_header_t *ssdt, void *unused)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000343{
Uwe Hermann622824c2010-11-19 15:14:42 +0000344 unsigned long current = (unsigned long)ssdt + sizeof(acpi_header_t);
345
Arthur Heymansadb80072023-06-28 09:56:00 +0200346 if (acpi_fill_header(ssdt, "SSDT", SSDT, sizeof(acpi_header_t)) != CB_SUCCESS)
347 return;
Rudolf Marek293b5f52009-02-01 18:35:15 +0000348
Elyes Haouas3141fbad2022-11-18 15:22:32 +0100349 acpigen_set_current((char *)current);
Duncan Laurie37319032016-05-26 12:47:05 -0700350
351 /* Write object to declare coreboot tables */
352 acpi_ssdt_write_cbtable();
353
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200354 {
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100355 struct device *dev;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200356 for (dev = all_devices; dev; dev = dev->next)
Karthikeyan Ramasubramaniand1c0f952020-11-02 16:26:52 -0700357 if (dev->enabled && dev->ops && dev->ops->acpi_fill_ssdt)
Nico Huber68680dd2020-03-31 17:34:52 +0200358 dev->ops->acpi_fill_ssdt(dev);
Elyes Haouas3141fbad2022-11-18 15:22:32 +0100359 current = (unsigned long)acpigen_get_current();
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200360 }
Rudolf Marek293b5f52009-02-01 18:35:15 +0000361
Uwe Hermann622824c2010-11-19 15:14:42 +0000362 /* (Re)calculate length and checksum. */
Rudolf Marek293b5f52009-02-01 18:35:15 +0000363 ssdt->length = current - (unsigned long)ssdt;
Rudolf Marek293b5f52009-02-01 18:35:15 +0000364}
365
Uwe Hermann622824c2010-11-19 15:14:42 +0000366int 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 +0300367 u32 flags)
Stefan Reinauerf622d592005-11-26 16:56:05 +0000368{
Uwe Hermann622824c2010-11-19 15:14:42 +0000369 mem->type = 1; /* Memory affinity structure */
370 mem->length = sizeof(acpi_srat_mem_t);
371 mem->base_address_low = (basek << 10);
372 mem->base_address_high = (basek >> (32 - 10));
373 mem->length_low = (sizek << 10);
374 mem->length_high = (sizek >> (32 - 10));
375 mem->proximity_domain = node;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000376 mem->flags = flags;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000377
Uwe Hermann622824c2010-11-19 15:14:42 +0000378 return mem->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000379}
380
Jonathan Zhang3164b642021-04-21 17:51:31 -0700381int acpi_create_srat_gia_pci(acpi_srat_gia_t *gia, u32 proximity_domain,
382 u16 seg, u8 bus, u8 dev, u8 func, u32 flags)
383{
384 gia->type = ACPI_SRAT_STRUCTURE_GIA;
385 gia->length = sizeof(acpi_srat_gia_t);
386 gia->proximity_domain = proximity_domain;
387 gia->dev_handle_type = ACPI_SRAT_GIA_DEV_HANDLE_PCI;
388 /* First two bytes has segment number */
389 memcpy(gia->dev_handle, &seg, 2);
390 gia->dev_handle[2] = bus; /* Byte 2 has bus number */
391 /* Byte 3 has bits 7:3 for dev, bits 2:0 for func */
392 gia->dev_handle[3] = PCI_SLOT(dev) | PCI_FUNC(func);
393 gia->flags = flags;
394
395 return gia->length;
396}
397
Uwe Hermann622824c2010-11-19 15:14:42 +0000398/* http://www.microsoft.com/whdc/system/sysinternals/sratdwn.mspx */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +0200399void acpi_create_srat(acpi_srat_t *srat,
400 unsigned long (*acpi_fill_srat)(unsigned long current))
Stefan Reinauerf622d592005-11-26 16:56:05 +0000401{
Uwe Hermann622824c2010-11-19 15:14:42 +0000402 acpi_header_t *header = &(srat->header);
403 unsigned long current = (unsigned long)srat + sizeof(acpi_srat_t);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000404
Uwe Hermann622824c2010-11-19 15:14:42 +0000405 memset((void *)srat, 0, sizeof(acpi_srat_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000406
Arthur Heymansadb80072023-06-28 09:56:00 +0200407 if (acpi_fill_header(header, "SRAT", SRAT, sizeof(acpi_srat_t)) != CB_SUCCESS)
John Zhao2ba303e2019-05-28 16:48:14 -0700408 return;
409
Uwe Hermann622824c2010-11-19 15:14:42 +0000410 srat->resv = 1; /* Spec: Reserved to 1 for backwards compatibility. */
Stefan Reinauerf622d592005-11-26 16:56:05 +0000411
Uwe Hermann622824c2010-11-19 15:14:42 +0000412 current = acpi_fill_srat(current);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000413
Uwe Hermann622824c2010-11-19 15:14:42 +0000414 /* (Re)calculate length and checksum. */
415 header->length = current - (unsigned long)srat;
416 header->checksum = acpi_checksum((void *)srat, header->length);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000417}
418
Jonathan Zhang3dcafa82022-05-11 13:11:20 -0700419int acpi_create_cedt_chbs(acpi_cedt_chbs_t *chbs, u32 uid, u32 cxl_ver, u64 base)
420{
421 memset((void *)chbs, 0, sizeof(acpi_cedt_chbs_t));
422
423 chbs->type = ACPI_CEDT_STRUCTURE_CHBS;
424 chbs->length = sizeof(acpi_cedt_chbs_t);
425 chbs->uid = uid;
426 chbs->cxl_ver = cxl_ver;
427 chbs->base = base;
428
429 /*
430 * CXL spec 2.0 section 9.14.1.2 "CXL CHBS"
431 * CXL 1.1 spec compliant host bridge: 8KB
432 * CXL 2.0 spec compliant host bridge: 64KB
433 */
434 if (cxl_ver == ACPI_CEDT_CHBS_CXL_VER_1_1)
435 chbs->len = 8 * KiB;
436 else if (cxl_ver == ACPI_CEDT_CHBS_CXL_VER_2_0)
437 chbs->len = 64 * KiB;
438 else
439 printk(BIOS_ERR, "ACPI(%s:%s): Incorrect CXL version:%d\n", __FILE__, __func__,
440 cxl_ver);
441
442 return chbs->length;
443}
444
445int acpi_create_cedt_cfmws(acpi_cedt_cfmws_t *cfmws, u64 base_hpa, u64 window_size, u8 eniw,
446 u32 hbig, u16 restriction, u16 qtg_id, const u32 *interleave_target)
447{
448 memset((void *)cfmws, 0, sizeof(acpi_cedt_cfmws_t));
449
450 cfmws->type = ACPI_CEDT_STRUCTURE_CFMWS;
451
452 u8 niw = 0;
453 if (eniw >= 8)
454 printk(BIOS_ERR, "ACPI(%s:%s): Incorrect eniw::%d\n", __FILE__, __func__, eniw);
455 else
456 /* NIW = 2 ** ENIW */
457 niw = 0x1 << eniw;
458 /* 36 + 4 * NIW */
459 cfmws->length = sizeof(acpi_cedt_cfmws_t) + 4 * niw;
460
461 cfmws->base_hpa = base_hpa;
462 cfmws->window_size = window_size;
463 cfmws->eniw = eniw;
464
465 // 0: Standard Modulo Arithmetic. Other values reserved.
466 cfmws->interleave_arithmetic = 0;
467
468 cfmws->hbig = hbig;
469 cfmws->restriction = restriction;
470 cfmws->qtg_id = qtg_id;
471 memcpy(&cfmws->interleave_target, interleave_target, 4 * niw);
472
473 return cfmws->length;
474}
475
476void acpi_create_cedt(acpi_cedt_t *cedt, unsigned long (*acpi_fill_cedt)(unsigned long current))
477{
478 acpi_header_t *header = &(cedt->header);
479 unsigned long current = (unsigned long)cedt + sizeof(acpi_cedt_t);
480
481 memset((void *)cedt, 0, sizeof(acpi_cedt_t));
482
Arthur Heymansadb80072023-06-28 09:56:00 +0200483 if (acpi_fill_header(header, "CEDT", CEDT, sizeof(acpi_cedt_t)) != CB_SUCCESS)
Jonathan Zhang3dcafa82022-05-11 13:11:20 -0700484 return;
485
Jonathan Zhang3dcafa82022-05-11 13:11:20 -0700486 current = acpi_fill_cedt(current);
487
488 /* (Re)calculate length and checksum. */
489 header->length = current - (unsigned long)cedt;
490 header->checksum = acpi_checksum((void *)cedt, header->length);
491}
492
Jonathan Zhang2a4e1f42021-04-01 11:43:37 -0700493int acpi_create_hmat_mpda(acpi_hmat_mpda_t *mpda, u32 initiator, u32 memory)
494{
495 memset((void *)mpda, 0, sizeof(acpi_hmat_mpda_t));
496
497 mpda->type = 0; /* Memory Proximity Domain Attributes structure */
498 mpda->length = sizeof(acpi_hmat_mpda_t);
499 /*
500 * Proximity Domain for Attached Initiator field is valid.
501 * Bit 1 and bit 2 are reserved since HMAT revision 2.
502 */
503 mpda->flags = (1 << 0);
504 mpda->proximity_domain_initiator = initiator;
505 mpda->proximity_domain_memory = memory;
506
507 return mpda->length;
508}
509
510void acpi_create_hmat(acpi_hmat_t *hmat,
511 unsigned long (*acpi_fill_hmat)(unsigned long current))
512{
513 acpi_header_t *header = &(hmat->header);
514 unsigned long current = (unsigned long)hmat + sizeof(acpi_hmat_t);
515
516 memset((void *)hmat, 0, sizeof(acpi_hmat_t));
517
Arthur Heymansadb80072023-06-28 09:56:00 +0200518 if (acpi_fill_header(header, "HMAT", HMAT, sizeof(acpi_hmat_t)) != CB_SUCCESS)
Jonathan Zhang2a4e1f42021-04-01 11:43:37 -0700519 return;
520
Jonathan Zhang2a4e1f42021-04-01 11:43:37 -0700521 current = acpi_fill_hmat(current);
522
523 /* (Re)calculate length and checksum. */
524 header->length = current - (unsigned long)hmat;
525 header->checksum = acpi_checksum((void *)hmat, header->length);
526}
527
Uwe Hermann622824c2010-11-19 15:14:42 +0000528/* http://h21007.www2.hp.com/portal/download/files/unprot/Itanium/slit.pdf */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +0200529void acpi_create_slit(acpi_slit_t *slit,
530 unsigned long (*acpi_fill_slit)(unsigned long current))
Yinghai Lud4b278c2006-10-04 20:46:15 +0000531{
Uwe Hermann622824c2010-11-19 15:14:42 +0000532 acpi_header_t *header = &(slit->header);
533 unsigned long current = (unsigned long)slit + sizeof(acpi_slit_t);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000534
Uwe Hermann622824c2010-11-19 15:14:42 +0000535 memset((void *)slit, 0, sizeof(acpi_slit_t));
Yinghai Lud4b278c2006-10-04 20:46:15 +0000536
Arthur Heymansadb80072023-06-28 09:56:00 +0200537 if (acpi_fill_header(header, "SLIT", SLIT, sizeof(acpi_slit_t)) != CB_SUCCESS)
John Zhao2ba303e2019-05-28 16:48:14 -0700538 return;
539
Uwe Hermann622824c2010-11-19 15:14:42 +0000540 current = acpi_fill_slit(current);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000541
Uwe Hermann622824c2010-11-19 15:14:42 +0000542 /* (Re)calculate length and checksum. */
543 header->length = current - (unsigned long)slit;
544 header->checksum = acpi_checksum((void *)slit, header->length);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000545}
546
Rocky Phaguraeff07132021-01-10 15:42:50 -0800547/*
548 * This method adds the ACPI error injection capability. It fills the default information.
549 * HW dependent code (caller) can modify the defaults upon return. If no changes are necessary
550 * and the defaults are acceptable then caller can simply add the table (acpi_add_table).
551 * INPUTS:
552 * einj - ptr to the starting location of EINJ table
553 * actions - number of actions to trigger an error (HW dependent)
554 * addr - address of trigger action table. This should be ACPI reserved memory and it will be
555 * shared between OS and FW.
556 */
557void acpi_create_einj(acpi_einj_t *einj, uintptr_t addr, u8 actions)
558{
559 int i;
560 acpi_header_t *header = &(einj->header);
561 acpi_injection_header_t *inj_header = &(einj->inj_header);
562 acpi_einj_smi_t *einj_smi = (acpi_einj_smi_t *)addr;
563 acpi_einj_trigger_table_t *tat;
564 if (!header)
565 return;
566
567 printk(BIOS_DEBUG, "%s einj_smi = %p\n", __func__, einj_smi);
568 memset(einj_smi, 0, sizeof(acpi_einj_smi_t));
Jonathan Zhangd5d9b282022-11-07 17:30:14 -0800569 tat = (acpi_einj_trigger_table_t *)((uint8_t *)einj_smi + sizeof(acpi_einj_smi_t));
Rocky Phaguraeff07132021-01-10 15:42:50 -0800570 tat->header_size = 16;
571 tat->revision = 0;
572 tat->table_size = sizeof(acpi_einj_trigger_table_t) +
573 sizeof(acpi_einj_action_table_t) * actions - 1;
574 tat->entry_count = actions;
575 printk(BIOS_DEBUG, "%s trigger_action_table = %p\n", __func__, tat);
576
577 for (i = 0; i < actions; i++) {
578 tat->trigger_action[i].action = TRIGGER_ERROR;
579 tat->trigger_action[i].instruction = NO_OP;
580 tat->trigger_action[i].flags = FLAG_IGNORE;
581 tat->trigger_action[i].reg.space_id = ACPI_ADDRESS_SPACE_MEMORY;
582 tat->trigger_action[i].reg.bit_width = 64;
583 tat->trigger_action[i].reg.bit_offset = 0;
584 tat->trigger_action[i].reg.access_size = ACPI_ACCESS_SIZE_QWORD_ACCESS;
585 tat->trigger_action[i].reg.addr = 0;
586 tat->trigger_action[i].value = 0;
587 tat->trigger_action[i].mask = 0xFFFFFFFF;
588 }
589
590 acpi_einj_action_table_t default_actions[ACTION_COUNT] = {
591 [0] = {
592 .action = BEGIN_INJECT_OP,
593 .instruction = WRITE_REGISTER_VALUE,
594 .flags = FLAG_PRESERVE,
595 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->op_state),
596 .value = 0,
597 .mask = 0xFFFFFFFF
598 },
599 [1] = {
600 .action = GET_TRIGGER_ACTION_TABLE,
601 .instruction = READ_REGISTER,
602 .flags = FLAG_IGNORE,
603 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->trigger_action_table),
604 .value = 0,
605 .mask = 0xFFFFFFFFFFFFFFFF
606 },
607 [2] = {
608 .action = SET_ERROR_TYPE,
609 .instruction = WRITE_REGISTER,
610 .flags = FLAG_PRESERVE,
611 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->err_inject[0]),
612 .value = 0,
613 .mask = 0xFFFFFFFF
614 },
615 [3] = {
616 .action = GET_ERROR_TYPE,
617 .instruction = READ_REGISTER,
618 .flags = FLAG_IGNORE,
619 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->err_inj_cap),
620 .value = 0,
621 .mask = 0xFFFFFFFF
622 },
623 [4] = {
624 .action = END_INJECT_OP,
625 .instruction = WRITE_REGISTER_VALUE,
626 .flags = FLAG_PRESERVE,
627 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->op_state),
628 .value = 0,
629 .mask = 0xFFFFFFFF
630
631 },
632 [5] = {
633 .action = EXECUTE_INJECT_OP,
634 .instruction = WRITE_REGISTER_VALUE,
635 .flags = FLAG_PRESERVE,
636 .reg = EINJ_REG_IO(),
637 .value = 0x9a,
638 .mask = 0xFFFF,
639 },
640 [6] = {
641 .action = CHECK_BUSY_STATUS,
642 .instruction = READ_REGISTER_VALUE,
643 .flags = FLAG_IGNORE,
644 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->op_status),
645 .value = 1,
646 .mask = 1,
647 },
648 [7] = {
649 .action = GET_CMD_STATUS,
650 .instruction = READ_REGISTER,
651 .flags = FLAG_PRESERVE,
652 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->cmd_sts),
653 .value = 0,
654 .mask = 0x1fe,
655 },
656 [8] = {
657 .action = SET_ERROR_TYPE_WITH_ADDRESS,
658 .instruction = WRITE_REGISTER,
659 .flags = FLAG_PRESERVE,
660 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->setaddrtable),
661 .value = 1,
662 .mask = 0xffffffff
663 }
664 };
665
666 einj_smi->err_inj_cap = ACPI_EINJ_DEFAULT_CAP;
Elyes Haouas3141fbad2022-11-18 15:22:32 +0100667 einj_smi->trigger_action_table = (u64)(uintptr_t)tat;
Rocky Phaguraeff07132021-01-10 15:42:50 -0800668
669 for (i = 0; i < ACTION_COUNT; i++)
670 printk(BIOS_DEBUG, "default_actions[%d].reg.addr is %llx\n", i,
671 default_actions[i].reg.addr);
672
Rocky Phagurab46a9e52021-05-21 13:34:03 -0700673 memset((void *)einj, 0, sizeof(*einj));
Rocky Phaguraeff07132021-01-10 15:42:50 -0800674
Arthur Heymansadb80072023-06-28 09:56:00 +0200675 if (acpi_fill_header(header, "EINJ", EINJ, sizeof(acpi_einj_t)) != CB_SUCCESS)
676 return;
Rocky Phaguraeff07132021-01-10 15:42:50 -0800677
Rocky Phaguraeff07132021-01-10 15:42:50 -0800678 inj_header->einj_header_size = sizeof(acpi_injection_header_t);
679 inj_header->entry_count = ACTION_COUNT;
680
681 printk(BIOS_DEBUG, "%s einj->action_table = %p\n",
682 __func__, einj->action_table);
683 memcpy((void *)einj->action_table, (void *)default_actions, sizeof(einj->action_table));
Rocky Phagurab46a9e52021-05-21 13:34:03 -0700684 header->checksum = acpi_checksum((void *)einj, sizeof(*einj));
Rocky Phaguraeff07132021-01-10 15:42:50 -0800685}
686
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700687void acpi_create_vfct(const struct device *device,
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530688 acpi_vfct_t *vfct,
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700689 unsigned long (*acpi_fill_vfct)(const struct device *device,
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530690 acpi_vfct_t *vfct_struct, unsigned long current))
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200691{
692 acpi_header_t *header = &(vfct->header);
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530693 unsigned long current = (unsigned long)vfct + sizeof(acpi_vfct_t);
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200694
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530695 memset((void *)vfct, 0, sizeof(acpi_vfct_t));
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200696
Arthur Heymansadb80072023-06-28 09:56:00 +0200697 if (acpi_fill_header(header, "VFCT", VFCT, sizeof(acpi_vfct_t)) != CB_SUCCESS)
John Zhao2ba303e2019-05-28 16:48:14 -0700698 return;
699
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200700 current = acpi_fill_vfct(device, vfct, current);
701
Kyösti Mälkkifb493792019-06-30 06:29:52 +0300702 /* If no BIOS image, return with header->length == 0. */
703 if (!vfct->VBIOSImageOffset)
704 return;
705
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200706 /* (Re)calculate length and checksum. */
707 header->length = current - (unsigned long)vfct;
708 header->checksum = acpi_checksum((void *)vfct, header->length);
709}
710
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700711void acpi_create_ipmi(const struct device *device,
Patrick Rudolph9d98e5a2019-06-14 19:00:04 +0200712 struct acpi_spmi *spmi,
713 const u16 ipmi_revision,
714 const acpi_addr_t *addr,
715 const enum acpi_ipmi_interface_type type,
716 const s8 gpe_interrupt,
717 const u32 apic_interrupt,
718 const u32 uid)
719{
720 acpi_header_t *header = &(spmi->header);
721 memset((void *)spmi, 0, sizeof(struct acpi_spmi));
722
Arthur Heymansadb80072023-06-28 09:56:00 +0200723 if (acpi_fill_header(header, "SPMI", SPMI, sizeof(struct acpi_spmi)) != CB_SUCCESS)
724 return;
Patrick Rudolph9d98e5a2019-06-14 19:00:04 +0200725
726 spmi->reserved = 1;
727
728 if (device->path.type == DEVICE_PATH_PCI) {
729 spmi->pci_device_flag = ACPI_IPMI_PCI_DEVICE_FLAG;
730 spmi->pci_bus = device->bus->secondary;
731 spmi->pci_device = device->path.pci.devfn >> 3;
732 spmi->pci_function = device->path.pci.devfn & 0x7;
733 } else if (type != IPMI_INTERFACE_SSIF) {
734 memcpy(spmi->uid, &uid, sizeof(spmi->uid));
735 }
736
737 spmi->base_address = *addr;
738 spmi->specification_revision = ipmi_revision;
739
740 spmi->interface_type = type;
741
742 if (gpe_interrupt >= 0 && gpe_interrupt < 32) {
743 spmi->gpe = gpe_interrupt;
744 spmi->interrupt_type |= ACPI_IPMI_INT_TYPE_SCI;
745 }
746 if (apic_interrupt > 0) {
747 spmi->global_system_interrupt = apic_interrupt;
748 spmi->interrupt_type |= ACPI_IPMI_INT_TYPE_APIC;
749 }
750
751 /* Calculate checksum. */
752 header->checksum = acpi_checksum((void *)spmi, header->length);
753}
754
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500755void acpi_create_ivrs(acpi_ivrs_t *ivrs,
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700756 unsigned long (*acpi_fill_ivrs)(acpi_ivrs_t *ivrs_struct,
757 unsigned long current))
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500758{
759 acpi_header_t *header = &(ivrs->header);
760 unsigned long current = (unsigned long)ivrs + sizeof(acpi_ivrs_t);
761
762 memset((void *)ivrs, 0, sizeof(acpi_ivrs_t));
763
Arthur Heymansadb80072023-06-28 09:56:00 +0200764 if (acpi_fill_header(header, "IVRS", IVRS, sizeof(acpi_ivrs_t)) != CB_SUCCESS)
John Zhao2ba303e2019-05-28 16:48:14 -0700765 return;
766
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500767 current = acpi_fill_ivrs(ivrs, current);
768
769 /* (Re)calculate length and checksum. */
770 header->length = current - (unsigned long)ivrs;
771 header->checksum = acpi_checksum((void *)ivrs, header->length);
772}
773
Jason Glenesk61624b22020-11-02 20:06:23 -0800774void acpi_create_crat(struct acpi_crat_header *crat,
775 unsigned long (*acpi_fill_crat)(struct acpi_crat_header *crat_struct,
776 unsigned long current))
777{
778 acpi_header_t *header = &(crat->header);
779 unsigned long current = (unsigned long)crat + sizeof(struct acpi_crat_header);
780
781 memset((void *)crat, 0, sizeof(struct acpi_crat_header));
782
Arthur Heymansadb80072023-06-28 09:56:00 +0200783 if (acpi_fill_header(header, "CRAT", CRAT, sizeof(struct acpi_crat_header)) != CB_SUCCESS)
Jason Glenesk61624b22020-11-02 20:06:23 -0800784 return;
785
Jason Glenesk61624b22020-11-02 20:06:23 -0800786 current = acpi_fill_crat(crat, current);
787
788 /* (Re)calculate length and checksum. */
789 header->length = current - (unsigned long)crat;
790 header->checksum = acpi_checksum((void *)crat, header->length);
791}
792
Arthur Heymans9368cf92023-04-25 16:07:49 +0200793static void acpi_create_dbg2(acpi_dbg2_header_t *dbg2,
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800794 int port_type, int port_subtype,
795 acpi_addr_t *address, uint32_t address_size,
796 const char *device_path)
797{
798 uintptr_t current;
799 acpi_dbg2_device_t *device;
800 uint32_t *dbg2_addr_size;
801 acpi_header_t *header;
802 size_t path_len;
803 const char *path;
804 char *namespace;
805
806 /* Fill out header fields. */
807 current = (uintptr_t)dbg2;
808 memset(dbg2, 0, sizeof(acpi_dbg2_header_t));
809 header = &(dbg2->header);
John Zhao2ba303e2019-05-28 16:48:14 -0700810
Arthur Heymansadb80072023-06-28 09:56:00 +0200811 if (acpi_fill_header(header, "DBG2", DBG2, sizeof(acpi_dbg2_header_t)) != CB_SUCCESS)
John Zhao2ba303e2019-05-28 16:48:14 -0700812 return;
813
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800814 /* One debug device defined */
815 dbg2->devices_offset = sizeof(acpi_dbg2_header_t);
816 dbg2->devices_count = 1;
817 current += sizeof(acpi_dbg2_header_t);
818
819 /* Device comes after the header */
820 device = (acpi_dbg2_device_t *)current;
821 memset(device, 0, sizeof(acpi_dbg2_device_t));
822 current += sizeof(acpi_dbg2_device_t);
823
824 device->revision = 0;
825 device->address_count = 1;
826 device->port_type = port_type;
827 device->port_subtype = port_subtype;
828
829 /* Base Address comes after device structure */
830 memcpy((void *)current, address, sizeof(acpi_addr_t));
831 device->base_address_offset = current - (uintptr_t)device;
832 current += sizeof(acpi_addr_t);
833
834 /* Address Size comes after address structure */
835 dbg2_addr_size = (uint32_t *)current;
836 device->address_size_offset = current - (uintptr_t)device;
837 *dbg2_addr_size = address_size;
838 current += sizeof(uint32_t);
839
840 /* Namespace string comes last, use '.' if not provided */
841 path = device_path ? : ".";
842 /* Namespace string length includes NULL terminator */
843 path_len = strlen(path) + 1;
844 namespace = (char *)current;
845 device->namespace_string_length = path_len;
846 device->namespace_string_offset = current - (uintptr_t)device;
847 strncpy(namespace, path, path_len);
848 current += path_len;
849
850 /* Update structure lengths and checksum */
851 device->length = current - (uintptr_t)device;
852 header->length = current - (uintptr_t)dbg2;
853 header->checksum = acpi_checksum((uint8_t *)dbg2, header->length);
854}
855
856unsigned long acpi_write_dbg2_pci_uart(acpi_rsdp_t *rsdp, unsigned long current,
Aamir Bohrae825d3f2019-07-30 10:11:41 +0530857 const struct device *dev, uint8_t access_size)
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800858{
859 acpi_dbg2_header_t *dbg2 = (acpi_dbg2_header_t *)current;
860 struct resource *res;
861 acpi_addr_t address;
862
863 if (!dev) {
Wim Vervoorn6cd52432020-02-03 14:41:46 +0100864 printk(BIOS_DEBUG, "%s: Device not found\n", __func__);
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800865 return current;
866 }
Mario Scheithauerf1eb0ea2017-11-21 13:52:53 +0100867 if (!dev->enabled) {
868 printk(BIOS_INFO, "%s: Device not enabled\n", __func__);
869 return current;
870 }
Angel Pons32d09be2021-11-03 13:27:45 +0100871 res = probe_resource(dev, PCI_BASE_ADDRESS_0);
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800872 if (!res) {
873 printk(BIOS_ERR, "%s: Unable to find resource for %s\n",
874 __func__, dev_path(dev));
875 return current;
876 }
877
878 memset(&address, 0, sizeof(address));
879 if (res->flags & IORESOURCE_IO)
880 address.space_id = ACPI_ADDRESS_SPACE_IO;
881 else if (res->flags & IORESOURCE_MEM)
882 address.space_id = ACPI_ADDRESS_SPACE_MEMORY;
883 else {
884 printk(BIOS_ERR, "%s: Unknown address space type\n", __func__);
885 return current;
886 }
887
888 address.addrl = (uint32_t)res->base;
889 address.addrh = (uint32_t)((res->base >> 32) & 0xffffffff);
890 address.access_size = access_size;
891
892 acpi_create_dbg2(dbg2,
893 ACPI_DBG2_PORT_SERIAL,
894 ACPI_DBG2_PORT_SERIAL_16550,
895 &address, res->size,
896 acpi_device_path(dev));
897
898 if (dbg2->header.length) {
899 current += dbg2->header.length;
900 current = acpi_align_current(current);
901 acpi_add_table(rsdp, dbg2);
902 }
903
904 return current;
905}
906
Arthur Heymans01af0f82023-06-27 11:58:04 +0200907static void acpi_create_facs(void *header)
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000908{
Arthur Heymans01af0f82023-06-27 11:58:04 +0200909 acpi_facs_t *facs = header;
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000910
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000911 memcpy(facs->signature, "FACS", 4);
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000912 facs->length = sizeof(acpi_facs_t);
913 facs->hardware_signature = 0;
914 facs->firmware_waking_vector = 0;
915 facs->global_lock = 0;
916 facs->flags = 0;
917 facs->x_firmware_waking_vector_l = 0;
918 facs->x_firmware_waking_vector_h = 0;
Marc Jones93a51762018-08-22 18:57:24 -0600919 facs->version = get_acpi_table_revision(FACS);
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000920}
Stefan Reinauer777224c2005-01-19 14:06:41 +0000921
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100922static void acpi_write_rsdt(acpi_rsdt_t *rsdt, char *oem_id, char *oem_table_id)
Stefan Reinauer14e22772010-04-27 06:56:47 +0000923{
Uwe Hermann622824c2010-11-19 15:14:42 +0000924 acpi_header_t *header = &(rsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000925
Arthur Heymansadb80072023-06-28 09:56:00 +0200926 if (acpi_fill_header(header, "RSDT", RSDT, sizeof(acpi_rsdt_t)) != CB_SUCCESS)
John Zhao2ba303e2019-05-28 16:48:14 -0700927 return;
928
Uwe Hermann622824c2010-11-19 15:14:42 +0000929 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauer688b3852004-01-28 16:56:14 +0000930
Uwe Hermann622824c2010-11-19 15:14:42 +0000931 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000932 header->checksum = acpi_checksum((void *)rsdt, sizeof(acpi_rsdt_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000933}
934
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100935static void acpi_write_xsdt(acpi_xsdt_t *xsdt, char *oem_id, char *oem_table_id)
Stefan Reinauer14e22772010-04-27 06:56:47 +0000936{
Uwe Hermann622824c2010-11-19 15:14:42 +0000937 acpi_header_t *header = &(xsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000938
Arthur Heymansadb80072023-06-28 09:56:00 +0200939 if (acpi_fill_header(header, "XSDT", XSDT, sizeof(acpi_xsdt_t)) != CB_SUCCESS)
John Zhao2ba303e2019-05-28 16:48:14 -0700940 return;
941
Uwe Hermann622824c2010-11-19 15:14:42 +0000942 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000943
Uwe Hermann622824c2010-11-19 15:14:42 +0000944 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000945 header->checksum = acpi_checksum((void *)xsdt, sizeof(acpi_xsdt_t));
946}
947
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100948static void acpi_write_rsdp(acpi_rsdp_t *rsdp, acpi_rsdt_t *rsdt,
949 acpi_xsdt_t *xsdt, char *oem_id)
Stefan Reinauer688b3852004-01-28 16:56:14 +0000950{
Stefan Reinauerd18faac2009-11-05 18:06:43 +0000951 memset(rsdp, 0, sizeof(acpi_rsdp_t));
Uwe Hermann622824c2010-11-19 15:14:42 +0000952
Stefan Reinauer688b3852004-01-28 16:56:14 +0000953 memcpy(rsdp->signature, RSDP_SIG, 8);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100954 memcpy(rsdp->oem_id, oem_id, 6);
Uwe Hermann622824c2010-11-19 15:14:42 +0000955
956 rsdp->length = sizeof(acpi_rsdp_t);
Stefan Reinauerdefee172015-06-18 01:11:19 -0700957 rsdp->rsdt_address = (uintptr_t)rsdt;
Uwe Hermann622824c2010-11-19 15:14:42 +0000958
959 /*
960 * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2.
961 *
962 * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
963 * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
964 * revision 0).
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000965 */
966 if (xsdt == NULL) {
Uwe Hermann622824c2010-11-19 15:14:42 +0000967 rsdp->revision = 0;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000968 } else {
Stefan Reinauerdefee172015-06-18 01:11:19 -0700969 rsdp->xsdt_address = (u64)(uintptr_t)xsdt;
Marc Jones93a51762018-08-22 18:57:24 -0600970 rsdp->revision = get_acpi_table_revision(RSDP);
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000971 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000972
973 /* Calculate checksums. */
974 rsdp->checksum = acpi_checksum((void *)rsdp, 20);
975 rsdp->ext_checksum = acpi_checksum((void *)rsdp, sizeof(acpi_rsdp_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000976}
977
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700978unsigned long acpi_create_hest_error_source(acpi_hest_t *hest,
979 acpi_hest_esd_t *esd, u16 type, void *data, u16 data_len)
zbaocaf494c82012-04-13 13:57:14 +0800980{
981 acpi_header_t *header = &(hest->header);
982 acpi_hest_hen_t *hen;
983 void *pos;
984 u16 len;
985
986 pos = esd;
987 memset(pos, 0, sizeof(acpi_hest_esd_t));
988 len = 0;
989 esd->type = type; /* MCE */
990 esd->source_id = hest->error_source_count;
991 esd->flags = 0; /* FIRMWARE_FIRST */
992 esd->enabled = 1;
993 esd->prealloc_erecords = 1;
994 esd->max_section_per_record = 0x1;
995
996 len += sizeof(acpi_hest_esd_t);
997 pos = esd + 1;
998
999 switch (type) {
1000 case 0: /* MCE */
1001 break;
1002 case 1: /* CMC */
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001003 hen = (acpi_hest_hen_t *)(pos);
zbaocaf494c82012-04-13 13:57:14 +08001004 memset(pos, 0, sizeof(acpi_hest_hen_t));
1005 hen->type = 3; /* SCI? */
1006 hen->length = sizeof(acpi_hest_hen_t);
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001007 hen->conf_we = 0; /* Configuration Write Enable. */
zbaocaf494c82012-04-13 13:57:14 +08001008 hen->poll_interval = 0;
1009 hen->vector = 0;
1010 hen->sw2poll_threshold_val = 0;
1011 hen->sw2poll_threshold_win = 0;
1012 hen->error_threshold_val = 0;
1013 hen->error_threshold_win = 0;
1014 len += sizeof(acpi_hest_hen_t);
1015 pos = hen + 1;
1016 break;
1017 case 2: /* NMI */
1018 case 6: /* AER Root Port */
1019 case 7: /* AER Endpoint */
1020 case 8: /* AER Bridge */
1021 case 9: /* Generic Hardware Error Source. */
1022 /* TODO: */
1023 break;
1024 default:
1025 printk(BIOS_DEBUG, "Invalid type of Error Source.");
1026 break;
1027 }
Lee Leahy024b13d2017-03-16 13:41:11 -07001028 hest->error_source_count++;
zbaocaf494c82012-04-13 13:57:14 +08001029
1030 memcpy(pos, data, data_len);
1031 len += data_len;
John Zhao2ba303e2019-05-28 16:48:14 -07001032 if (header)
1033 header->length += len;
zbaocaf494c82012-04-13 13:57:14 +08001034
1035 return len;
1036}
1037
1038/* ACPI 4.0 */
Vladimir Serbinenko807127f2014-11-09 13:36:18 +01001039void acpi_write_hest(acpi_hest_t *hest,
1040 unsigned long (*acpi_fill_hest)(acpi_hest_t *hest))
zbaocaf494c82012-04-13 13:57:14 +08001041{
1042 acpi_header_t *header = &(hest->header);
1043
1044 memset(hest, 0, sizeof(acpi_hest_t));
1045
Arthur Heymansadb80072023-06-28 09:56:00 +02001046 if (acpi_fill_header(header, "HEST", HEST, sizeof(acpi_hest_t)) != CB_SUCCESS)
John Zhao2ba303e2019-05-28 16:48:14 -07001047 return;
1048
zbaocaf494c82012-04-13 13:57:14 +08001049 acpi_fill_hest(hest);
1050
1051 /* Calculate checksums. */
1052 header->checksum = acpi_checksum((void *)hest, header->length);
1053}
1054
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001055/* ACPI 3.0b */
Arthur Heymans01af0f82023-06-27 11:58:04 +02001056static void acpi_create_bert(acpi_header_t *header, void *unused)
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001057{
Arthur Heymans01af0f82023-06-27 11:58:04 +02001058 if (!CONFIG(ACPI_BERT))
1059 return;
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001060
Arthur Heymans01af0f82023-06-27 11:58:04 +02001061 acpi_bert_t *bert = (acpi_bert_t *)header;
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001062
Arthur Heymans01af0f82023-06-27 11:58:04 +02001063 void *region;
1064 size_t size;
1065 if (acpi_soc_get_bert_region(&region, &size) != CB_SUCCESS)
1066 return;
1067
Arthur Heymansadb80072023-06-28 09:56:00 +02001068 if (acpi_fill_header(header, "BERT", BERT, sizeof(acpi_bert_t)) != CB_SUCCESS)
1069 return;
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001070
Arthur Heymans01af0f82023-06-27 11:58:04 +02001071 bert->error_region = (uintptr_t)region;
1072 bert->region_length = (size_t)size;
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001073}
1074
Angel Pons79572e42020-07-13 00:17:43 +02001075__weak void arch_fill_fadt(acpi_fadt_t *fadt) { }
Kyösti Mälkkif9aac922020-05-30 16:16:28 +03001076__weak void soc_fill_fadt(acpi_fadt_t *fadt) { }
Kyösti Mälkki02fd15d2020-06-02 03:34:43 +03001077__weak void mainboard_fill_fadt(acpi_fadt_t *fadt) { }
Kyösti Mälkkif9aac922020-05-30 16:16:28 +03001078
Arthur Heymans01af0f82023-06-27 11:58:04 +02001079static acpi_header_t *dsdt;
1080static void acpi_create_fadt(acpi_header_t *header, void *arg1)
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001081{
Arthur Heymans01af0f82023-06-27 11:58:04 +02001082 acpi_fadt_t *fadt = (acpi_fadt_t *)header;
1083 acpi_facs_t *facs = (acpi_facs_t *)(*(acpi_facs_t **)arg1);
John Zhao2ba303e2019-05-28 16:48:14 -07001084
Arthur Heymansadb80072023-06-28 09:56:00 +02001085 if (acpi_fill_header(header, "FACP", FADT, sizeof(acpi_fadt_t)) != CB_SUCCESS)
John Zhao2ba303e2019-05-28 16:48:14 -07001086 return;
1087
Elyes Haouas532e0432022-02-21 18:13:58 +01001088 fadt->FADT_MinorVersion = get_acpi_fadt_minor_version();
Arthur Heymans8473e8f2023-06-29 20:26:39 +02001089 if ((uintptr_t)facs <= UINT32_MAX)
1090 fadt->firmware_ctrl = (uintptr_t)facs;
1091 else
1092 fadt->x_firmware_ctl_h = (uint32_t)((uint64_t)(uintptr_t)facs >> 32);
1093 fadt->x_firmware_ctl_l = (uint32_t)(uintptr_t)facs;
Kyösti Mälkkid4acee82020-06-03 07:20:55 +03001094
Arthur Heymans8473e8f2023-06-29 20:26:39 +02001095 if ((uintptr_t)dsdt <= UINT32_MAX)
1096 fadt->dsdt = (uintptr_t)dsdt;
1097 else
1098 fadt->x_dsdt_h = (uint32_t)((uint64_t)(uintptr_t)dsdt >> 32);
1099 fadt->x_dsdt_l = (uint32_t)(uintptr_t)dsdt;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001100
Kyösti Mälkkid4acee82020-06-03 07:20:55 +03001101 /* should be 0 ACPI 3.0 */
1102 fadt->reserved = 0;
1103
Kyösti Mälkki67c48a32023-04-14 10:20:03 +03001104 /* P_LVLx latencies are not used as CPU _CST will override them. */
1105 fadt->p_lvl2_lat = ACPI_FADT_C2_NOT_SUPPORTED;
1106 fadt->p_lvl3_lat = ACPI_FADT_C3_NOT_SUPPORTED;
1107
Kyösti Mälkki9ff97972023-04-14 15:37:27 +03001108 /* Use CPU _PTC instead to provide P_CNT details. */
1109 fadt->duty_offset = 0;
1110 fadt->duty_width = 0;
1111
Kyösti Mälkkie0d38682020-06-07 12:01:58 +03001112 fadt->preferred_pm_profile = acpi_get_preferred_pm_profile();
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001113
Kyösti Mälkkie742b682023-04-10 17:03:32 +03001114 fadt->sci_int = acpi_sci_int();
1115
Angel Pons79572e42020-07-13 00:17:43 +02001116 arch_fill_fadt(fadt);
1117
Vladimir Serbinenkoab83ef02014-10-25 15:18:25 +02001118 acpi_fill_fadt(fadt);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001119
Kyösti Mälkkif9aac922020-05-30 16:16:28 +03001120 soc_fill_fadt(fadt);
Kyösti Mälkki02fd15d2020-06-02 03:34:43 +03001121 mainboard_fill_fadt(fadt);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001122}
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001123
Arthur Heymans01af0f82023-06-27 11:58:04 +02001124static void acpi_create_lpit(acpi_header_t *header, void *unused)
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001125{
Arthur Heymans01af0f82023-06-27 11:58:04 +02001126 if (!CONFIG(ACPI_LPIT))
1127 return;
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001128
Arthur Heymans01af0f82023-06-27 11:58:04 +02001129 acpi_lpit_t *lpit = (acpi_lpit_t *)header;
1130 unsigned long current = (unsigned long)lpit + sizeof(acpi_lpit_t);
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001131
Arthur Heymansadb80072023-06-28 09:56:00 +02001132 if (acpi_fill_header(header, "LPIT", LPIT, sizeof(acpi_lpit_t)) != CB_SUCCESS)
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001133 return;
1134
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001135 current = acpi_fill_lpit(current);
1136
Arthur Heymans01af0f82023-06-27 11:58:04 +02001137 /* (Re)calculate length. */
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001138 header->length = current - (unsigned long)lpit;
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001139}
1140
Arthur Heymans2e3cb632023-06-30 15:01:08 +02001141static void acpi_create_gtdt(acpi_header_t *header, void *unused)
1142{
1143 if (!CONFIG(ACPI_GTDT))
1144 return;
1145
1146 acpi_gtdt_t *gtdt = (acpi_gtdt_t *)header;
1147 unsigned long current = (unsigned long)gtdt + sizeof(acpi_gtdt_t);
1148
1149 if (acpi_fill_header(header, "GTDT", GTDT, sizeof(acpi_gtdt_t)) != CB_SUCCESS)
1150 return;
1151
1152 /* Fill out header fields. */
1153 gtdt->platform_timer_offset = sizeof(acpi_gtdt_t);
1154
1155 acpi_soc_fill_gtdt(gtdt);
1156 current = acpi_soc_gtdt_add_timers(&gtdt->platform_timer_count, current);
1157
1158 /* (Re)calculate length. */
1159 header->length = current - (unsigned long)gtdt;
1160}
1161
1162unsigned long acpi_gtdt_add_timer_block(unsigned long current, const uint64_t address,
1163 struct acpi_gtdt_timer_entry *timers, size_t number)
1164{
1165 struct acpi_gtdt_timer_block *block = (struct acpi_gtdt_timer_block *)current;
1166 memset(block, 0, sizeof(struct acpi_gtdt_timer_block));
1167
1168 assert(number < 8 && number != 0);
1169 const size_t entries_size = number * sizeof(struct acpi_gtdt_timer_entry);
1170
1171 block->header.type = ACPI_GTDT_TYPE_TIMER_BLOCK;
1172 block->header.length = sizeof(struct acpi_gtdt_timer_block)
1173 + entries_size;
1174 block->block_address = address;
1175 block->timer_count = number;
1176 block->timer_offset = sizeof(struct acpi_gtdt_timer_block);
1177 current += sizeof(struct acpi_gtdt_timer_block);
1178 memcpy((void *)current, timers, entries_size);
1179 current += entries_size;
1180 return current;
1181}
1182
1183unsigned long acpi_gtdt_add_watchdog(unsigned long current, uint64_t refresh_frame,
1184 uint64_t control_frame, uint32_t gsiv, uint32_t flags)
1185{
1186 struct acpi_gtdt_watchdog *wd = (struct acpi_gtdt_watchdog *)current;
1187 memset(wd, 0, sizeof(struct acpi_gtdt_watchdog));
1188
1189 wd->header.type = ACPI_GTDT_TYPE_WATCHDOG;
1190 wd->header.length = sizeof(struct acpi_gtdt_watchdog);
1191 wd->refresh_frame_address = refresh_frame;
1192 wd->control_frame_address = control_frame;
1193 wd->timer_interrupt = gsiv;
1194 wd->timer_flags = flags;
1195
1196 return current + sizeof(struct acpi_gtdt_watchdog);
1197}
1198
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001199unsigned long acpi_create_lpi_desc_ncst(acpi_lpi_desc_ncst_t *lpi_desc, uint16_t uid)
1200{
1201 memset(lpi_desc, 0, sizeof(acpi_lpi_desc_ncst_t));
1202 lpi_desc->header.length = sizeof(acpi_lpi_desc_ncst_t);
1203 lpi_desc->header.type = ACPI_LPI_DESC_TYPE_NATIVE_CSTATE;
1204 lpi_desc->header.uid = uid;
1205
1206 return lpi_desc->header.length;
1207}
1208
Arthur Heymans90464072023-06-07 12:53:50 +02001209static uint8_t acpi_spcr_type(void)
1210{
1211 /* 16550-compatible with parameters defined in Generic Address Structure */
1212 if (CONFIG(DRIVERS_UART_8250IO) || CONFIG(DRIVERS_UART_8250MEM))
1213 return 0x12;
1214 if (CONFIG(DRIVERS_UART_PL011))
1215 return 0x3;
1216
1217 printk(BIOS_ERR, "%s: unknown serial type\n", __func__);
1218 return 0xff;
1219}
1220
Arthur Heymans01af0f82023-06-27 11:58:04 +02001221static void acpi_create_spcr(acpi_header_t *header, void *unused)
Arthur Heymans90464072023-06-07 12:53:50 +02001222{
Arthur Heymans01af0f82023-06-27 11:58:04 +02001223 acpi_spcr_t *spcr = (acpi_spcr_t *)header;
Arthur Heymans90464072023-06-07 12:53:50 +02001224 struct lb_serial serial;
1225
Arthur Heymans90464072023-06-07 12:53:50 +02001226 if (!CONFIG(CONSOLE_SERIAL))
1227 return;
1228
1229 if (fill_lb_serial(&serial) != CB_SUCCESS)
1230 return;
1231
Arthur Heymansadb80072023-06-28 09:56:00 +02001232 if (acpi_fill_header(header, "SPCR", SPCR, sizeof(acpi_spcr_t)) != CB_SUCCESS)
1233 return;
Arthur Heymans90464072023-06-07 12:53:50 +02001234
1235 spcr->interface_type = acpi_spcr_type();
1236 assert(serial.type == LB_SERIAL_TYPE_IO_MAPPED
1237 || serial.type == LB_SERIAL_TYPE_MEMORY_MAPPED);
1238 spcr->base_address.space_id = serial.type == LB_SERIAL_TYPE_IO_MAPPED ?
1239 ACPI_ADDRESS_SPACE_IO : ACPI_ADDRESS_SPACE_MEMORY;
1240 spcr->base_address.bit_width = serial.regwidth * 8;
1241 spcr->base_address.bit_offset = 0;
1242 switch (serial.regwidth) {
1243 case 1:
1244 spcr->base_address.access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS;
1245 break;
1246 case 2:
1247 spcr->base_address.access_size = ACPI_ACCESS_SIZE_WORD_ACCESS;
1248 break;
1249 case 4:
1250 spcr->base_address.access_size = ACPI_ACCESS_SIZE_DWORD_ACCESS;
1251 break;
1252 default:
1253 printk(BIOS_ERR, "%s, Invalid serial regwidth\n", __func__);
1254 }
1255
1256 spcr->base_address.addrl = serial.baseaddr;
1257 spcr->base_address.addrh = 0;
1258 spcr->interrupt_type = 0;
1259 spcr->irq = 0;
1260 spcr->configured_baudrate = 0; /* Have the OS use whatever is currently set */
1261 spcr->parity = 0;
1262 spcr->stop_bits = 1;
1263 spcr->flow_control = 0;
1264 spcr->terminal_type = 2; /* 2 = VT-UTF8 */
1265 spcr->language = 0;
1266 spcr->pci_did = 0xffff;
1267 spcr->pci_vid = 0xffff;
Nico Huberfeb27dc2023-06-28 20:09:30 +02001268
1269 header->checksum = acpi_checksum((void *)spcr, header->length);
Arthur Heymans90464072023-06-07 12:53:50 +02001270}
1271
Aaron Durbin64031672018-04-21 14:45:32 -06001272unsigned long __weak fw_cfg_acpi_tables(unsigned long start)
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001273{
1274 return 0;
1275}
1276
Raul E Rangel6b446b92021-11-19 11:38:35 -07001277void preload_acpi_dsdt(void)
1278{
1279 const char *file = CONFIG_CBFS_PREFIX "/dsdt.aml";
1280
1281 if (!CONFIG(CBFS_PRELOAD))
1282 return;
1283
1284 printk(BIOS_DEBUG, "Preloading %s\n", file);
1285 cbfs_preload(file);
1286}
1287
Arthur Heymans01af0f82023-06-27 11:58:04 +02001288static void acpi_create_dsdt(acpi_header_t *header, void *dsdt_file_arg)
1289{
1290 dsdt = header;
1291 acpi_header_t *dsdt_file = *(acpi_header_t **)dsdt_file_arg;
1292 unsigned long current = (unsigned long)header;
1293
1294 dsdt = (acpi_header_t *)current;
1295 memcpy(dsdt, dsdt_file, sizeof(acpi_header_t));
1296 if (dsdt->length >= sizeof(acpi_header_t)) {
1297 current += sizeof(acpi_header_t);
1298
1299 acpigen_set_current((char *)current);
1300
1301 if (CONFIG(ACPI_SOC_NVS))
1302 acpi_fill_gnvs();
1303 if (CONFIG(CHROMEOS_NVS))
1304 acpi_fill_cnvs();
1305
1306 for (const struct device *dev = all_devices; dev; dev = dev->next)
1307 if (dev->ops && dev->ops->acpi_inject_dsdt)
1308 dev->ops->acpi_inject_dsdt(dev);
1309 current = (unsigned long)acpigen_get_current();
1310 memcpy((char *)current,
1311 (char *)dsdt_file + sizeof(acpi_header_t),
1312 dsdt->length - sizeof(acpi_header_t));
1313 current += dsdt->length - sizeof(acpi_header_t);
1314
1315 /* (Re)calculate length. */
1316 dsdt->length = current - (unsigned long)dsdt;
1317 }
1318}
1319
1320static void acpi_create_slic(acpi_header_t *header, void *slic_file_arg)
1321{
1322 acpi_header_t *slic_file = *(acpi_header_t **)slic_file_arg;
1323 acpi_header_t *slic = header;
1324 if (slic_file)
1325 memcpy(slic, slic_file, slic_file->length);
1326}
1327
Arthur Heymans2e7e2d92022-03-03 22:28:27 +01001328static uintptr_t coreboot_rsdp;
1329
1330uintptr_t get_coreboot_rsdp(void)
1331{
1332 return coreboot_rsdp;
1333}
1334
Arthur Heymans0ad766c2023-06-07 10:45:59 +02001335static void acpixtract_compatible_hexdump(const void *memory, size_t length)
1336{
1337 size_t i, j;
1338 uint8_t *line;
1339 size_t num_bytes;
1340
1341 for (i = 0; i < length; i += 16) {
1342 num_bytes = MIN(length - i, 16);
1343 line = ((uint8_t *)memory) + i;
1344
1345 printk(BIOS_SPEW, " %04zX:", i);
1346 for (j = 0; j < num_bytes; j++)
1347 printk(BIOS_SPEW, " %02x", line[j]);
1348 for (; j < 16; j++)
1349 printk(BIOS_SPEW, " ");
1350 printk(BIOS_SPEW, " ");
1351 for (j = 0; j < num_bytes; j++)
1352 printk(BIOS_SPEW, "%c",
1353 isprint(line[j]) ? line[j] : '.');
1354 printk(BIOS_SPEW, "\n");
1355 }
1356}
1357
1358static void acpidump_print(void *table_ptr)
1359{
1360 const acpi_header_t *header = (acpi_header_t *)table_ptr;
1361 const size_t table_size = header->length;
1362 printk(BIOS_SPEW, "%.4s @ 0x0000000000000000\n", header->signature);
1363 acpixtract_compatible_hexdump(table_ptr, table_size);
1364 printk(BIOS_SPEW, "\n");
1365}
1366
Arthur Heymans7ebebf72023-06-17 14:08:46 +02001367unsigned long write_acpi_tables(const unsigned long start)
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001368{
1369 unsigned long current;
1370 acpi_rsdp_t *rsdp;
Arthur Heymans0ad766c2023-06-07 10:45:59 +02001371 acpi_rsdt_t *rsdt = NULL;
1372 acpi_xsdt_t *xsdt = NULL;
Arthur Heymans0ad766c2023-06-07 10:45:59 +02001373 acpi_facs_t *facs = NULL;
Arthur Heymans01af0f82023-06-27 11:58:04 +02001374 acpi_header_t *slic_file;
Arthur Heymans0ad766c2023-06-07 10:45:59 +02001375 acpi_header_t *ssdt = NULL;
Arthur Heymans01af0f82023-06-27 11:58:04 +02001376 acpi_header_t *dsdt_file;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +11001377 struct device *dev;
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001378 unsigned long fw;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001379 size_t slic_size, dsdt_size;
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001380 char oem_id[6], oem_table_id[8];
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001381
Arthur Heymans01af0f82023-06-27 11:58:04 +02001382 const struct acpi_table_generator {
1383 void (*create_table)(acpi_header_t *table, void *arg);
1384 void *args;
1385 size_t min_size;
1386 } tables[] = {
1387 { acpi_create_dsdt, &dsdt_file, sizeof(acpi_header_t) },
1388 { acpi_create_fadt, &facs, sizeof(acpi_fadt_t) },
1389 { acpi_create_slic, &slic_file, sizeof(acpi_header_t) },
1390 { acpi_create_ssdt_generator, NULL, sizeof(acpi_header_t) },
1391 { acpi_create_mcfg, NULL, sizeof(acpi_mcfg_t) },
1392 { acpi_create_tcpa, NULL, sizeof(acpi_tcpa_t) },
1393 { acpi_create_tpm2, NULL, sizeof(acpi_tpm2_t) },
1394 { acpi_create_lpit, NULL, sizeof(acpi_lpit_t) },
1395 { acpi_create_madt, NULL, sizeof(acpi_header_t) },
1396 { acpi_create_bert, NULL, sizeof(acpi_bert_t) },
1397 { acpi_create_spcr, NULL, sizeof(acpi_spcr_t) },
Arthur Heymans2e3cb632023-06-30 15:01:08 +02001398 { acpi_create_gtdt, NULL, sizeof(acpi_gtdt_t) },
Arthur Heymans01af0f82023-06-27 11:58:04 +02001399 };
1400
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001401 current = start;
1402
1403 /* Align ACPI tables to 16byte */
Aaron Durbin07a1b282015-12-10 17:07:38 -06001404 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001405
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001406 /* Special case for qemu */
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001407 fw = fw_cfg_acpi_tables(current);
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001408 if (fw) {
1409 rsdp = NULL;
1410 /* Find RSDP. */
1411 for (void *p = (void *)current; p < (void *)fw; p += 16) {
1412 if (valid_rsdp((acpi_rsdp_t *)p)) {
1413 rsdp = p;
Bin Mengf3027b82023-05-09 15:21:49 +08001414 coreboot_rsdp = (uintptr_t)rsdp;
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001415 break;
1416 }
1417 }
1418 if (!rsdp)
1419 return fw;
1420
1421 /* Add BOOT0000 for Linux google firmware driver */
1422 printk(BIOS_DEBUG, "ACPI: * SSDT\n");
1423 ssdt = (acpi_header_t *)fw;
1424 current = (unsigned long)ssdt + sizeof(acpi_header_t);
1425
1426 memset((void *)ssdt, 0, sizeof(acpi_header_t));
1427
1428 memcpy(&ssdt->signature, "SSDT", 4);
1429 ssdt->revision = get_acpi_table_revision(SSDT);
1430 memcpy(&ssdt->oem_id, OEM_ID, 6);
1431 memcpy(&ssdt->oem_table_id, oem_table_id, 8);
1432 ssdt->oem_revision = 42;
1433 memcpy(&ssdt->asl_compiler_id, ASLC, 4);
1434 ssdt->asl_compiler_revision = asl_revision;
1435 ssdt->length = sizeof(acpi_header_t);
1436
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001437 acpigen_set_current((char *)current);
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001438
1439 /* Write object to declare coreboot tables */
1440 acpi_ssdt_write_cbtable();
1441
1442 /* (Re)calculate length and checksum. */
1443 ssdt->length = current - (unsigned long)ssdt;
1444 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
1445
Arthur Heymans01af0f82023-06-27 11:58:04 +02001446 acpi_create_ssdt_generator(ssdt, NULL);
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001447
1448 acpi_add_table(rsdp, ssdt);
1449
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001450 return fw;
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001451 }
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001452
Julius Werner834b3ec2020-03-04 16:52:08 -08001453 dsdt_file = cbfs_map(CONFIG_CBFS_PREFIX "/dsdt.aml", &dsdt_size);
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001454 if (!dsdt_file) {
1455 printk(BIOS_ERR, "No DSDT file, skipping ACPI tables\n");
Arthur Heymans0600aa62023-06-17 14:13:54 +02001456 return start;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001457 }
1458
1459 if (dsdt_file->length > dsdt_size
Elyes HAOUAS7d87e762016-10-03 22:11:07 +02001460 || dsdt_file->length < sizeof(acpi_header_t)
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001461 || memcmp(dsdt_file->signature, "DSDT", 4) != 0) {
1462 printk(BIOS_ERR, "Invalid DSDT file, skipping ACPI tables\n");
Grzegorz Bernacki042ac352023-04-21 13:19:58 +00001463 cbfs_unmap(dsdt_file);
Arthur Heymans0600aa62023-06-17 14:13:54 +02001464 return start;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001465 }
1466
Julius Werner834b3ec2020-03-04 16:52:08 -08001467 slic_file = cbfs_map(CONFIG_CBFS_PREFIX "/slic", &slic_size);
Vladimir Serbinenko2cb29782015-05-31 11:32:09 +02001468 if (slic_file
1469 && (slic_file->length > slic_size
Elyes HAOUAS7d87e762016-10-03 22:11:07 +02001470 || slic_file->length < sizeof(acpi_header_t)
Benjamin Doron2b2dc0c2020-09-12 02:55:57 +00001471 || (memcmp(slic_file->signature, "SLIC", 4) != 0
1472 && memcmp(slic_file->signature, "MSDM", 4) != 0))) {
Grzegorz Bernacki042ac352023-04-21 13:19:58 +00001473 cbfs_unmap(slic_file);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001474 slic_file = 0;
1475 }
1476
1477 if (slic_file) {
1478 memcpy(oem_id, slic_file->oem_id, 6);
1479 memcpy(oem_table_id, slic_file->oem_table_id, 8);
1480 } else {
1481 memcpy(oem_id, OEM_ID, 6);
1482 memcpy(oem_table_id, ACPI_TABLE_CREATOR, 8);
1483 }
1484
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001485 printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx.\n", start);
1486
1487 /* We need at least an RSDP and an RSDT Table */
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001488 rsdp = (acpi_rsdp_t *)current;
Arthur Heymans2e7e2d92022-03-03 22:28:27 +01001489 coreboot_rsdp = (uintptr_t)rsdp;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001490 current += sizeof(acpi_rsdp_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001491 current = acpi_align_current(current);
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001492 rsdt = (acpi_rsdt_t *)current;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001493 current += sizeof(acpi_rsdt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001494 current = acpi_align_current(current);
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001495 xsdt = (acpi_xsdt_t *)current;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001496 current += sizeof(acpi_xsdt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001497 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001498
1499 /* clear all table memory */
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001500 memset((void *)start, 0, current - start);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001501
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001502 acpi_write_rsdp(rsdp, rsdt, xsdt, oem_id);
1503 acpi_write_rsdt(rsdt, oem_id, oem_table_id);
1504 acpi_write_xsdt(xsdt, oem_id, oem_table_id);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001505
Arthur Heymans6af72612023-06-29 20:25:32 +02001506 if (ENV_X86) {
1507 printk(BIOS_DEBUG, "ACPI: * FACS\n");
1508 current = ALIGN_UP(current, 64);
1509 facs = (acpi_facs_t *)current;
1510 current += sizeof(acpi_facs_t);
1511 current = acpi_align_current(current);
1512 acpi_create_facs(facs);
1513 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001514
Arthur Heymans01af0f82023-06-27 11:58:04 +02001515 for (size_t i = 0; i < ARRAY_SIZE(tables); i++) {
1516 acpi_header_t *header = (acpi_header_t *)current;
1517 memset(header, 0, tables[i].min_size);
1518 tables[i].create_table(header, tables[i].args);
1519 if (header->length < tables[i].min_size)
1520 continue;
1521 header->checksum = 0;
1522 header->checksum = acpi_checksum((void *)header, header->length);
1523 current += header->length;
Aaron Durbin07a1b282015-12-10 17:07:38 -06001524 current = acpi_align_current(current);
Kyösti Mälkki0bcdd402023-07-06 14:47:07 +03001525
1526 if (tables[i].create_table == acpi_create_dsdt)
1527 continue;
1528
Arthur Heymans01af0f82023-06-27 11:58:04 +02001529 printk(BIOS_DEBUG, "ACPI: * %.4s\n", header->signature);
1530 acpi_add_table(rsdp, header);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001531 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001532
Grzegorz Bernacki042ac352023-04-21 13:19:58 +00001533 /*
1534 * cbfs_unmap() uses mem_pool_free() which works correctly only
1535 * if freeing is done in reverse order than memory allocation.
1536 * This is why unmapping of dsdt_file must be done after
1537 * unmapping slic file.
1538 */
Arthur Heymans01af0f82023-06-27 11:58:04 +02001539 cbfs_unmap(slic_file);
Grzegorz Bernacki042ac352023-04-21 13:19:58 +00001540 cbfs_unmap(dsdt_file);
1541
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001542 printk(BIOS_DEBUG, "current = %lx\n", current);
1543
1544 for (dev = all_devices; dev; dev = dev->next) {
1545 if (dev->ops && dev->ops->write_acpi_tables) {
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001546 current = dev->ops->write_acpi_tables(dev, current,
1547 rsdp);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001548 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001549 }
1550 }
1551
1552 printk(BIOS_INFO, "ACPI: done.\n");
Arthur Heymans0ad766c2023-06-07 10:45:59 +02001553
1554 if (CONFIG(DEBUG_ACPICA_COMPATIBLE)) {
1555 printk(BIOS_DEBUG, "Printing ACPI tables in ACPICA compatible format\n");
Arthur Heymans1cdeaea2023-07-06 16:24:50 +02001556 if (facs)
1557 acpidump_print(facs);
1558 acpidump_print(dsdt);
Arthur Heymans3e523b42023-06-15 17:04:16 +02001559 for (size_t i = 0; xsdt->entry[i] != 0; i++) {
1560 acpidump_print((void *)(uintptr_t)xsdt->entry[i]);
Arthur Heymans0ad766c2023-06-07 10:45:59 +02001561 }
1562 printk(BIOS_DEBUG, "Done printing ACPI tables in ACPICA compatible format\n");
1563 }
1564
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001565 return current;
1566}
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001567
Rudolf Marek33cafe52009-04-13 18:07:02 +00001568static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp)
1569{
1570 if (strncmp((char *)rsdp, RSDP_SIG, sizeof(RSDP_SIG) - 1) != 0)
1571 return NULL;
1572
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001573 printk(BIOS_DEBUG, "Looking on %p for valid checksum\n", rsdp);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001574
1575 if (acpi_checksum((void *)rsdp, 20) != 0)
1576 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001577 printk(BIOS_DEBUG, "Checksum 1 passed\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001578
1579 if ((rsdp->revision > 1) && (acpi_checksum((void *)rsdp,
1580 rsdp->length) != 0))
1581 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001582 printk(BIOS_DEBUG, "Checksum 2 passed all OK\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001583
1584 return rsdp;
1585}
1586
Rudolf Marek33cafe52009-04-13 18:07:02 +00001587void *acpi_find_wakeup_vector(void)
1588{
1589 char *p, *end;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001590 acpi_rsdt_t *rsdt;
1591 acpi_facs_t *facs;
Martin Rothc5f49262012-12-14 19:17:55 -07001592 acpi_fadt_t *fadt = NULL;
Kyösti Mälkki072d436b2016-06-17 08:44:40 +03001593 acpi_rsdp_t *rsdp = NULL;
Uwe Hermann622824c2010-11-19 15:14:42 +00001594 void *wake_vec;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001595 int i;
1596
Kyösti Mälkkib8c7ea02020-11-17 16:41:38 +02001597 if (!acpi_is_wakeup_s3())
Rudolf Marek33cafe52009-04-13 18:07:02 +00001598 return NULL;
1599
Uwe Hermann622824c2010-11-19 15:14:42 +00001600 printk(BIOS_DEBUG, "Trying to find the wakeup vector...\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001601
Uwe Hermann622824c2010-11-19 15:14:42 +00001602 /* Find RSDP. */
1603 for (p = (char *)0xe0000; p < (char *)0xfffff; p += 16) {
Lee Leahy0b5678f2017-03-16 16:01:40 -07001604 rsdp = valid_rsdp((acpi_rsdp_t *)p);
1605 if (rsdp)
Rudolf Marek33cafe52009-04-13 18:07:02 +00001606 break;
1607 }
1608
Angel Pons98a68ad2018-11-04 12:25:25 +01001609 if (rsdp == NULL) {
1610 printk(BIOS_ALERT,
1611 "No RSDP found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001612 return NULL;
Angel Pons98a68ad2018-11-04 12:25:25 +01001613 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001614
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001615 printk(BIOS_DEBUG, "RSDP found at %p\n", rsdp);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001616 rsdt = (acpi_rsdt_t *)(uintptr_t)rsdp->rsdt_address;
Stefan Reinauer14e22772010-04-27 06:56:47 +00001617
Uwe Hermann622824c2010-11-19 15:14:42 +00001618 end = (char *)rsdt + rsdt->header.length;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001619 printk(BIOS_DEBUG, "RSDT found at %p ends at %p\n", rsdt, end);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001620
Uwe Hermann622824c2010-11-19 15:14:42 +00001621 for (i = 0; ((char *)&rsdt->entry[i]) < end; i++) {
Stefan Reinauer71a30182015-07-30 16:28:13 -07001622 fadt = (acpi_fadt_t *)(uintptr_t)rsdt->entry[i];
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001623 if (strncmp((char *)fadt, "FACP", 4) == 0)
Rudolf Marek33cafe52009-04-13 18:07:02 +00001624 break;
1625 fadt = NULL;
1626 }
1627
Angel Pons98a68ad2018-11-04 12:25:25 +01001628 if (fadt == NULL) {
1629 printk(BIOS_ALERT,
1630 "No FADT found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001631 return NULL;
Angel Pons98a68ad2018-11-04 12:25:25 +01001632 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001633
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001634 printk(BIOS_DEBUG, "FADT found at %p\n", fadt);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001635 facs = (acpi_facs_t *)(uintptr_t)fadt->firmware_ctrl;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001636
Stefan Reinauer7e9771c2009-04-22 08:17:38 +00001637 if (facs == NULL) {
Angel Pons98a68ad2018-11-04 12:25:25 +01001638 printk(BIOS_ALERT,
1639 "No FACS found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001640 return NULL;
Stefan Reinauer7e9771c2009-04-22 08:17:38 +00001641 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001642
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001643 printk(BIOS_DEBUG, "FACS found at %p\n", facs);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001644 wake_vec = (void *)(uintptr_t)facs->firmware_waking_vector;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001645 printk(BIOS_DEBUG, "OS waking vector is %p\n", wake_vec);
Uwe Hermann622824c2010-11-19 15:14:42 +00001646
Rudolf Marek33cafe52009-04-13 18:07:02 +00001647 return wake_vec;
1648}
1649
Aaron Durbin64031672018-04-21 14:45:32 -06001650__weak int acpi_get_gpe(int gpe)
Duncan Laurie1f6e6812016-09-19 12:04:19 -07001651{
1652 return -1; /* implemented by SOC */
1653}
Marc Jones93a51762018-08-22 18:57:24 -06001654
Elyes Haouas8b950f42022-02-16 12:08:16 +01001655u8 get_acpi_fadt_minor_version(void)
1656{
1657 return ACPI_FADT_MINOR_VERSION_0;
1658}
1659
Marc Jones93a51762018-08-22 18:57:24 -06001660int get_acpi_table_revision(enum acpi_tables table)
1661{
1662 switch (table) {
1663 case FADT:
Elyes Haouas8b950f42022-02-16 12:08:16 +01001664 return ACPI_FADT_REV_ACPI_6;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001665 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 +01001666 return 3;
Marc Jones93a51762018-08-22 18:57:24 -06001667 case MCFG:
1668 return 1;
1669 case TCPA:
1670 return 2;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001671 case TPM2:
1672 return 4;
Martin Roth0949e732021-10-01 14:28:22 -06001673 case SSDT: /* ACPI 3.0 up to 6.3: 2 */
Marc Jones93a51762018-08-22 18:57:24 -06001674 return 2;
Jonathan Zhanga3311b92022-12-01 17:13:41 -08001675 case SRAT: /* ACPI 2.0: 1, ACPI 3.0: 2, ACPI 4.0 up to 6.4: 3 */
1676 return 3;
Jonathan Zhang2a4e1f42021-04-01 11:43:37 -07001677 case HMAT: /* ACPI 6.4: 2 */
1678 return 2;
Marc Jones93a51762018-08-22 18:57:24 -06001679 case DMAR:
1680 return 1;
Martin Roth0949e732021-10-01 14:28:22 -06001681 case SLIT: /* ACPI 2.0 up to 6.3: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06001682 return 1;
Patrick Rudolph9d98e5a2019-06-14 19:00:04 +02001683 case SPMI: /* IMPI 2.0 */
1684 return 5;
Marc Jones93a51762018-08-22 18:57:24 -06001685 case HPET: /* Currently 1. Table added in ACPI 2.0. */
1686 return 1;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001687 case VFCT: /* ACPI 2.0/3.0/4.0: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06001688 return 1;
1689 case IVRS:
Jason Glenesk1916f892020-07-24 02:51:30 -07001690 return IVRS_FORMAT_MIXED;
Marc Jones93a51762018-08-22 18:57:24 -06001691 case DBG2:
1692 return 0;
Martin Roth0949e732021-10-01 14:28:22 -06001693 case FACS: /* ACPI 2.0/3.0: 1, ACPI 4.0 up to 6.3: 2 */
Marc Jones93a51762018-08-22 18:57:24 -06001694 return 1;
Martin Roth0949e732021-10-01 14:28:22 -06001695 case RSDT: /* ACPI 1.0 up to 6.3: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06001696 return 1;
Martin Roth0949e732021-10-01 14:28:22 -06001697 case XSDT: /* ACPI 2.0 up to 6.3: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06001698 return 1;
Martin Roth0949e732021-10-01 14:28:22 -06001699 case RSDP: /* ACPI 2.0 up to 6.3: 2 */
Marc Jones93a51762018-08-22 18:57:24 -06001700 return 2;
Rocky Phaguraeff07132021-01-10 15:42:50 -08001701 case EINJ:
1702 return 1;
Marc Jones93a51762018-08-22 18:57:24 -06001703 case HEST:
1704 return 1;
1705 case NHLT:
1706 return 5;
Marshall Dawson44705c62018-09-04 13:47:15 -06001707 case BERT:
1708 return 1;
Jonathan Zhanga3311b92022-12-01 17:13:41 -08001709 case CEDT: /* CXL 3.0 section 9.17.1 */
1710 return 1;
Jason Glenesk61624b22020-11-02 20:06:23 -08001711 case CRAT:
1712 return 1;
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001713 case LPIT: /* ACPI 5.1 up to 6.3: 0 */
1714 return 0;
Arthur Heymans90464072023-06-07 12:53:50 +02001715 case SPCR:
1716 return 4;
Arthur Heymans2e3cb632023-06-30 15:01:08 +02001717 case GTDT:
1718 return 3;
Marc Jones93a51762018-08-22 18:57:24 -06001719 default:
1720 return -1;
1721 }
1722 return -1;
1723}