blob: a2e9fdb8d8197624c5a36d8658ad68e021f34b9d [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
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +020030static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp);
31
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000032u8 acpi_checksum(u8 *table, u32 length)
Stefan Reinauer688b3852004-01-28 16:56:14 +000033{
Uwe Hermann622824c2010-11-19 15:14:42 +000034 u8 ret = 0;
Stefan Reinauer688b3852004-01-28 16:56:14 +000035 while (length--) {
36 ret += *table;
37 table++;
38 }
Stefan Reinauera7648c22004-01-29 17:31:34 +000039 return -ret;
Stefan Reinauer688b3852004-01-28 16:56:14 +000040}
41
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000042/**
Uwe Hermann622824c2010-11-19 15:14:42 +000043 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
44 * and checksum.
Stefan Reinauer06feb882004-02-03 16:11:35 +000045 */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000046void acpi_add_table(acpi_rsdp_t *rsdp, void *table)
Stefan Reinauer688b3852004-01-28 16:56:14 +000047{
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000048 int i, entries_num;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000049 acpi_rsdt_t *rsdt;
50 acpi_xsdt_t *xsdt = NULL;
51
Uwe Hermann622824c2010-11-19 15:14:42 +000052 /* The RSDT is mandatory... */
Stefan Reinauerdefee172015-06-18 01:11:19 -070053 rsdt = (acpi_rsdt_t *)(uintptr_t)rsdp->rsdt_address;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000054
Uwe Hermann622824c2010-11-19 15:14:42 +000055 /* ...while the XSDT is not. */
56 if (rsdp->xsdt_address)
Stefan Reinauerdefee172015-06-18 01:11:19 -070057 xsdt = (acpi_xsdt_t *)((uintptr_t)rsdp->xsdt_address);
Stefan Reinauer14e22772010-04-27 06:56:47 +000058
Uwe Hermann622824c2010-11-19 15:14:42 +000059 /* This should always be MAX_ACPI_TABLES. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000060 entries_num = ARRAY_SIZE(rsdt->entry);
Stefan Reinauer14e22772010-04-27 06:56:47 +000061
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000062 for (i = 0; i < entries_num; i++) {
Uwe Hermann622824c2010-11-19 15:14:42 +000063 if (rsdt->entry[i] == 0)
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000064 break;
Stefan Reinauer688b3852004-01-28 16:56:14 +000065 }
66
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000067 if (i >= entries_num) {
Uwe Hermann622824c2010-11-19 15:14:42 +000068 printk(BIOS_ERR, "ACPI: Error: Could not add ACPI table, "
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +030069 "too many tables.\n");
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000070 return;
71 }
72
Uwe Hermann622824c2010-11-19 15:14:42 +000073 /* Add table to the RSDT. */
Stefan Reinauerdefee172015-06-18 01:11:19 -070074 rsdt->entry[i] = (uintptr_t)table;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000075
Uwe Hermann622824c2010-11-19 15:14:42 +000076 /* Fix RSDT length or the kernel will assume invalid entries. */
77 rsdt->header.length = sizeof(acpi_header_t) + (sizeof(u32) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000078
Uwe Hermann622824c2010-11-19 15:14:42 +000079 /* Re-calculate checksum. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000080 rsdt->header.checksum = 0; /* Hope this won't get optimized away */
Uwe Hermann622824c2010-11-19 15:14:42 +000081 rsdt->header.checksum = acpi_checksum((u8 *)rsdt, rsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000082
Uwe Hermann622824c2010-11-19 15:14:42 +000083 /*
84 * And now the same thing for the XSDT. We use the same index as for
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000085 * now we want the XSDT and RSDT to always be in sync in coreboot.
86 */
87 if (xsdt) {
Uwe Hermann622824c2010-11-19 15:14:42 +000088 /* Add table to the XSDT. */
Stefan Reinauerdefee172015-06-18 01:11:19 -070089 xsdt->entry[i] = (u64)(uintptr_t)table;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000090
Uwe Hermann622824c2010-11-19 15:14:42 +000091 /* Fix XSDT length. */
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000092 xsdt->header.length = sizeof(acpi_header_t) +
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +030093 (sizeof(u64) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000094
Uwe Hermann622824c2010-11-19 15:14:42 +000095 /* Re-calculate checksum. */
96 xsdt->header.checksum = 0;
97 xsdt->header.checksum = acpi_checksum((u8 *)xsdt,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +030098 xsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000099 }
100
Uwe Hermann622824c2010-11-19 15:14:42 +0000101 printk(BIOS_DEBUG, "ACPI: added table %d/%d, length now %d\n",
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300102 i + 1, entries_num, rsdt->header.length);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000103}
104
Arthur Heymansadb80072023-06-28 09:56:00 +0200105static enum cb_err acpi_fill_header(acpi_header_t *header, const char name[4],
106 enum acpi_tables table, uint32_t size)
107{
108 if (!header)
109 return CB_ERR;
110
111 /* Fill out header fields. */
112 memcpy(header->signature, name, 4);
113 memcpy(header->oem_id, OEM_ID, 6);
114 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
115 memcpy(header->asl_compiler_id, ASLC, 4);
116
117 header->asl_compiler_revision = asl_revision;
118 header->revision = get_acpi_table_revision(table);
119 header->length = size;
120
121 return CB_SUCCESS;
122}
123
Arthur Heymans9362dd72023-06-08 19:56:51 +0200124static int acpi_create_mcfg_mmconfig(acpi_mcfg_mmconfig_t *mmconfig, u32 base,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300125 u16 seg_nr, u8 start, u8 end)
Stefan Reinauer26d431a2009-01-20 19:17:51 +0000126{
Vladimir Serbinenko60fccdc2014-10-05 11:05:16 +0200127 memset(mmconfig, 0, sizeof(*mmconfig));
Rudolf Mareke6409f22007-11-03 12:50:26 +0000128 mmconfig->base_address = base;
129 mmconfig->base_reserved = 0;
130 mmconfig->pci_segment_group_number = seg_nr;
131 mmconfig->start_bus_number = start;
132 mmconfig->end_bus_number = end;
Uwe Hermann622824c2010-11-19 15:14:42 +0000133
134 return sizeof(acpi_mcfg_mmconfig_t);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000135}
136
Arthur Heymans01af0f82023-06-27 11:58:04 +0200137static void acpi_create_madt(acpi_header_t *header, void *unused)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000138{
Arthur Heymans01af0f82023-06-27 11:58:04 +0200139 acpi_madt_t *madt = (acpi_madt_t *)header;
Uwe Hermann622824c2010-11-19 15:14:42 +0000140 unsigned long current = (unsigned long)madt + sizeof(acpi_madt_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000141
Arthur Heymansadb80072023-06-28 09:56:00 +0200142 if (acpi_fill_header(header, "APIC", MADT, sizeof(acpi_madt_t)) != CB_SUCCESS)
John Zhao2ba303e2019-05-28 16:48:14 -0700143 return;
144
Arthur Heymanscd46e5f2023-06-22 21:34:16 +0200145 current = acpi_arch_fill_madt(madt, current);
Kyösti Mälkki10bdee12023-04-11 01:00:17 +0300146
147 if (CONFIG(ACPI_CUSTOM_MADT))
Kyösti Mälkkia5fa5342022-11-18 13:23:52 +0200148 current = acpi_fill_madt(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000149
Arthur Heymans01af0f82023-06-27 11:58:04 +0200150 /* (Re)calculate length . */
Uwe Hermann622824c2010-11-19 15:14:42 +0000151 header->length = current - (unsigned long)madt;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000152}
153
Kyösti Mälkkib54388d2021-02-14 15:09:45 +0200154static unsigned long acpi_fill_mcfg(unsigned long current)
155{
156 current += acpi_create_mcfg_mmconfig((acpi_mcfg_mmconfig_t *)current,
Shelley Chen4e9bb332021-10-20 15:43:45 -0700157 CONFIG_ECAM_MMCONF_BASE_ADDRESS, 0, 0,
158 CONFIG_ECAM_MMCONF_BUS_NUMBER - 1);
Kyösti Mälkkib54388d2021-02-14 15:09:45 +0200159 return current;
160}
161
Uwe Hermann622824c2010-11-19 15:14:42 +0000162/* MCFG is defined in the PCI Firmware Specification 3.0. */
Arthur Heymans01af0f82023-06-27 11:58:04 +0200163static void acpi_create_mcfg(acpi_header_t *header, void *unused)
Rudolf Mareke6409f22007-11-03 12:50:26 +0000164{
Arthur Heymans01af0f82023-06-27 11:58:04 +0200165 acpi_mcfg_t *mcfg = (acpi_mcfg_t *)header;
Uwe Hermann622824c2010-11-19 15:14:42 +0000166 unsigned long current = (unsigned long)mcfg + sizeof(acpi_mcfg_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000167
Arthur Heymansadb80072023-06-28 09:56:00 +0200168
169 if (acpi_fill_header(header, "MCFG", MCFG, sizeof(acpi_mcfg_t)) != CB_SUCCESS)
John Zhao2ba303e2019-05-28 16:48:14 -0700170 return;
171
Shelley Chen4e9bb332021-10-20 15:43:45 -0700172 if (CONFIG(ECAM_MMCONF_SUPPORT))
Kyösti Mälkkib54388d2021-02-14 15:09:45 +0200173 current = acpi_fill_mcfg(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000174
Arthur Heymans01af0f82023-06-27 11:58:04 +0200175 /* (Re)calculate length */
Uwe Hermann622824c2010-11-19 15:14:42 +0000176 header->length = current - (unsigned long)mcfg;
Rudolf Mareke6409f22007-11-03 12:50:26 +0000177}
178
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200179static void *get_tcpa_log(u32 *size)
180{
181 const struct cbmem_entry *ce;
182 const u32 tcpa_default_log_len = 0x10000;
183 void *lasa;
Philipp Deppenwiese791ba972018-07-30 01:15:20 +0200184 ce = cbmem_entry_find(CBMEM_ID_TCPA_TCG_LOG);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200185 if (ce) {
186 lasa = cbmem_entry_start(ce);
187 *size = cbmem_entry_size(ce);
188 printk(BIOS_DEBUG, "TCPA log found at %p\n", lasa);
189 return lasa;
190 }
Philipp Deppenwiese791ba972018-07-30 01:15:20 +0200191 lasa = cbmem_add(CBMEM_ID_TCPA_TCG_LOG, tcpa_default_log_len);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200192 if (!lasa) {
193 printk(BIOS_ERR, "TCPA log creation failed\n");
194 return NULL;
195 }
196
197 printk(BIOS_DEBUG, "TCPA log created at %p\n", lasa);
Lee Leahy024b13d2017-03-16 13:41:11 -0700198 memset(lasa, 0, tcpa_default_log_len);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200199
200 *size = tcpa_default_log_len;
201 return lasa;
202}
203
Arthur Heymans01af0f82023-06-27 11:58:04 +0200204static void acpi_create_tcpa(acpi_header_t *header, void *unused)
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200205{
Arthur Heymans01af0f82023-06-27 11:58:04 +0200206 if (!CONFIG(TPM1))
207 return;
208
209 acpi_tcpa_t *tcpa = (acpi_tcpa_t *)header;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200210 u32 tcpa_log_len;
211 void *lasa;
212
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200213 lasa = get_tcpa_log(&tcpa_log_len);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700214 if (!lasa)
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200215 return;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200216
Arthur Heymansadb80072023-06-28 09:56:00 +0200217 if (acpi_fill_header(header, "TCPA", TCPA, sizeof(acpi_tcpa_t)) != CB_SUCCESS)
John Zhao2ba303e2019-05-28 16:48:14 -0700218 return;
219
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200220 tcpa->platform_class = 0;
221 tcpa->laml = tcpa_log_len;
Elyes Haouas3141fbad2022-11-18 15:22:32 +0100222 tcpa->lasa = (uintptr_t)lasa;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200223}
224
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100225static void *get_tpm2_log(u32 *size)
226{
227 const struct cbmem_entry *ce;
228 const u32 tpm2_default_log_len = 0x10000;
229 void *lasa;
230 ce = cbmem_entry_find(CBMEM_ID_TPM2_TCG_LOG);
231 if (ce) {
232 lasa = cbmem_entry_start(ce);
233 *size = cbmem_entry_size(ce);
234 printk(BIOS_DEBUG, "TPM2 log found at %p\n", lasa);
235 return lasa;
236 }
237 lasa = cbmem_add(CBMEM_ID_TPM2_TCG_LOG, tpm2_default_log_len);
238 if (!lasa) {
239 printk(BIOS_ERR, "TPM2 log creation failed\n");
240 return NULL;
241 }
242
243 printk(BIOS_DEBUG, "TPM2 log created at %p\n", lasa);
244 memset(lasa, 0, tpm2_default_log_len);
245
246 *size = tpm2_default_log_len;
247 return lasa;
248}
249
Arthur Heymans01af0f82023-06-27 11:58:04 +0200250static void acpi_create_tpm2(acpi_header_t *header, void *unused)
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200251{
Arthur Heymans01af0f82023-06-27 11:58:04 +0200252 if (!CONFIG(TPM2))
253 return;
254
255 acpi_tpm2_t *tpm2 = (acpi_tpm2_t *)header;
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100256 u32 tpm2_log_len;
257 void *lasa;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200258
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100259 /*
260 * Some payloads like SeaBIOS depend on log area to use TPM2.
261 * Get the memory size and address of TPM2 log area or initialize it.
262 */
263 lasa = get_tpm2_log(&tpm2_log_len);
264 if (!lasa)
265 tpm2_log_len = 0;
266
Arthur Heymansadb80072023-06-28 09:56:00 +0200267 if (acpi_fill_header(header, "TPM2", TPM2, sizeof(acpi_tpm2_t)) != CB_SUCCESS)
John Zhao2ba303e2019-05-28 16:48:14 -0700268 return;
269
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200270 /* Hard to detect for coreboot. Just set it to 0 */
271 tpm2->platform_class = 0;
Christian Walter26e0d4c2019-07-14 15:47:23 +0200272 if (CONFIG(CRB_TPM)) {
273 /* Must be set to 7 for CRB Support */
274 tpm2->control_area = CONFIG_CRB_TPM_BASE_ADDRESS + 0x40;
275 tpm2->start_method = 7;
276 } else {
277 /* Must be set to 0 for FIFO interface support */
278 tpm2->control_area = 0;
279 tpm2->start_method = 6;
280 }
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200281 memset(tpm2->msp, 0, sizeof(tpm2->msp));
282
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100283 /* Fill the log area size and start address fields. */
284 tpm2->laml = tpm2_log_len;
Elyes Haouas3141fbad2022-11-18 15:22:32 +0100285 tpm2->lasa = (uintptr_t)lasa;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200286}
287
Duncan Laurie37319032016-05-26 12:47:05 -0700288static void acpi_ssdt_write_cbtable(void)
289{
290 const struct cbmem_entry *cbtable;
291 uintptr_t base;
292 uint32_t size;
293
294 cbtable = cbmem_entry_find(CBMEM_ID_CBTABLE);
295 if (!cbtable)
296 return;
297 base = (uintptr_t)cbmem_entry_start(cbtable);
298 size = cbmem_entry_size(cbtable);
299
300 acpigen_write_device("CTBL");
301 acpigen_write_coreboot_hid(COREBOOT_ACPI_ID_CBTABLE);
302 acpigen_write_name_integer("_UID", 0);
David Wu5dff3962018-03-21 16:48:53 +0800303 acpigen_write_STA(ACPI_STATUS_DEVICE_HIDDEN_ON);
Duncan Laurie37319032016-05-26 12:47:05 -0700304 acpigen_write_name("_CRS");
305 acpigen_write_resourcetemplate_header();
306 acpigen_write_mem32fixed(0, base, size);
307 acpigen_write_resourcetemplate_footer();
308 acpigen_pop_len();
309}
310
Arthur Heymans01af0f82023-06-27 11:58:04 +0200311static void acpi_create_ssdt_generator(acpi_header_t *ssdt, void *unused)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000312{
Uwe Hermann622824c2010-11-19 15:14:42 +0000313 unsigned long current = (unsigned long)ssdt + sizeof(acpi_header_t);
314
Arthur Heymansadb80072023-06-28 09:56:00 +0200315 if (acpi_fill_header(ssdt, "SSDT", SSDT, sizeof(acpi_header_t)) != CB_SUCCESS)
316 return;
Rudolf Marek293b5f52009-02-01 18:35:15 +0000317
Elyes Haouas3141fbad2022-11-18 15:22:32 +0100318 acpigen_set_current((char *)current);
Duncan Laurie37319032016-05-26 12:47:05 -0700319
320 /* Write object to declare coreboot tables */
321 acpi_ssdt_write_cbtable();
322
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200323 {
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100324 struct device *dev;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200325 for (dev = all_devices; dev; dev = dev->next)
Karthikeyan Ramasubramaniand1c0f952020-11-02 16:26:52 -0700326 if (dev->enabled && dev->ops && dev->ops->acpi_fill_ssdt)
Nico Huber68680dd2020-03-31 17:34:52 +0200327 dev->ops->acpi_fill_ssdt(dev);
Elyes Haouas3141fbad2022-11-18 15:22:32 +0100328 current = (unsigned long)acpigen_get_current();
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200329 }
Rudolf Marek293b5f52009-02-01 18:35:15 +0000330
Uwe Hermann622824c2010-11-19 15:14:42 +0000331 /* (Re)calculate length and checksum. */
Rudolf Marek293b5f52009-02-01 18:35:15 +0000332 ssdt->length = current - (unsigned long)ssdt;
Rudolf Marek293b5f52009-02-01 18:35:15 +0000333}
334
Uwe Hermann622824c2010-11-19 15:14:42 +0000335int 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 +0300336 u32 flags)
Stefan Reinauerf622d592005-11-26 16:56:05 +0000337{
Uwe Hermann622824c2010-11-19 15:14:42 +0000338 mem->type = 1; /* Memory affinity structure */
339 mem->length = sizeof(acpi_srat_mem_t);
340 mem->base_address_low = (basek << 10);
341 mem->base_address_high = (basek >> (32 - 10));
342 mem->length_low = (sizek << 10);
343 mem->length_high = (sizek >> (32 - 10));
344 mem->proximity_domain = node;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000345 mem->flags = flags;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000346
Uwe Hermann622824c2010-11-19 15:14:42 +0000347 return mem->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000348}
349
Jonathan Zhang3164b642021-04-21 17:51:31 -0700350int acpi_create_srat_gia_pci(acpi_srat_gia_t *gia, u32 proximity_domain,
351 u16 seg, u8 bus, u8 dev, u8 func, u32 flags)
352{
353 gia->type = ACPI_SRAT_STRUCTURE_GIA;
354 gia->length = sizeof(acpi_srat_gia_t);
355 gia->proximity_domain = proximity_domain;
356 gia->dev_handle_type = ACPI_SRAT_GIA_DEV_HANDLE_PCI;
357 /* First two bytes has segment number */
358 memcpy(gia->dev_handle, &seg, 2);
359 gia->dev_handle[2] = bus; /* Byte 2 has bus number */
360 /* Byte 3 has bits 7:3 for dev, bits 2:0 for func */
361 gia->dev_handle[3] = PCI_SLOT(dev) | PCI_FUNC(func);
362 gia->flags = flags;
363
364 return gia->length;
365}
366
Uwe Hermann622824c2010-11-19 15:14:42 +0000367/* http://www.microsoft.com/whdc/system/sysinternals/sratdwn.mspx */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +0200368void acpi_create_srat(acpi_srat_t *srat,
369 unsigned long (*acpi_fill_srat)(unsigned long current))
Stefan Reinauerf622d592005-11-26 16:56:05 +0000370{
Uwe Hermann622824c2010-11-19 15:14:42 +0000371 acpi_header_t *header = &(srat->header);
372 unsigned long current = (unsigned long)srat + sizeof(acpi_srat_t);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000373
Uwe Hermann622824c2010-11-19 15:14:42 +0000374 memset((void *)srat, 0, sizeof(acpi_srat_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000375
Arthur Heymansadb80072023-06-28 09:56:00 +0200376 if (acpi_fill_header(header, "SRAT", SRAT, sizeof(acpi_srat_t)) != CB_SUCCESS)
John Zhao2ba303e2019-05-28 16:48:14 -0700377 return;
378
Uwe Hermann622824c2010-11-19 15:14:42 +0000379 srat->resv = 1; /* Spec: Reserved to 1 for backwards compatibility. */
Stefan Reinauerf622d592005-11-26 16:56:05 +0000380
Uwe Hermann622824c2010-11-19 15:14:42 +0000381 current = acpi_fill_srat(current);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000382
Uwe Hermann622824c2010-11-19 15:14:42 +0000383 /* (Re)calculate length and checksum. */
384 header->length = current - (unsigned long)srat;
385 header->checksum = acpi_checksum((void *)srat, header->length);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000386}
387
Jonathan Zhang3dcafa82022-05-11 13:11:20 -0700388int acpi_create_cedt_chbs(acpi_cedt_chbs_t *chbs, u32 uid, u32 cxl_ver, u64 base)
389{
390 memset((void *)chbs, 0, sizeof(acpi_cedt_chbs_t));
391
392 chbs->type = ACPI_CEDT_STRUCTURE_CHBS;
393 chbs->length = sizeof(acpi_cedt_chbs_t);
394 chbs->uid = uid;
395 chbs->cxl_ver = cxl_ver;
396 chbs->base = base;
397
398 /*
399 * CXL spec 2.0 section 9.14.1.2 "CXL CHBS"
400 * CXL 1.1 spec compliant host bridge: 8KB
401 * CXL 2.0 spec compliant host bridge: 64KB
402 */
403 if (cxl_ver == ACPI_CEDT_CHBS_CXL_VER_1_1)
404 chbs->len = 8 * KiB;
405 else if (cxl_ver == ACPI_CEDT_CHBS_CXL_VER_2_0)
406 chbs->len = 64 * KiB;
407 else
408 printk(BIOS_ERR, "ACPI(%s:%s): Incorrect CXL version:%d\n", __FILE__, __func__,
409 cxl_ver);
410
411 return chbs->length;
412}
413
414int acpi_create_cedt_cfmws(acpi_cedt_cfmws_t *cfmws, u64 base_hpa, u64 window_size, u8 eniw,
415 u32 hbig, u16 restriction, u16 qtg_id, const u32 *interleave_target)
416{
417 memset((void *)cfmws, 0, sizeof(acpi_cedt_cfmws_t));
418
419 cfmws->type = ACPI_CEDT_STRUCTURE_CFMWS;
420
421 u8 niw = 0;
422 if (eniw >= 8)
423 printk(BIOS_ERR, "ACPI(%s:%s): Incorrect eniw::%d\n", __FILE__, __func__, eniw);
424 else
425 /* NIW = 2 ** ENIW */
426 niw = 0x1 << eniw;
427 /* 36 + 4 * NIW */
428 cfmws->length = sizeof(acpi_cedt_cfmws_t) + 4 * niw;
429
430 cfmws->base_hpa = base_hpa;
431 cfmws->window_size = window_size;
432 cfmws->eniw = eniw;
433
434 // 0: Standard Modulo Arithmetic. Other values reserved.
435 cfmws->interleave_arithmetic = 0;
436
437 cfmws->hbig = hbig;
438 cfmws->restriction = restriction;
439 cfmws->qtg_id = qtg_id;
440 memcpy(&cfmws->interleave_target, interleave_target, 4 * niw);
441
442 return cfmws->length;
443}
444
445void acpi_create_cedt(acpi_cedt_t *cedt, unsigned long (*acpi_fill_cedt)(unsigned long current))
446{
447 acpi_header_t *header = &(cedt->header);
448 unsigned long current = (unsigned long)cedt + sizeof(acpi_cedt_t);
449
450 memset((void *)cedt, 0, sizeof(acpi_cedt_t));
451
Arthur Heymansadb80072023-06-28 09:56:00 +0200452 if (acpi_fill_header(header, "CEDT", CEDT, sizeof(acpi_cedt_t)) != CB_SUCCESS)
Jonathan Zhang3dcafa82022-05-11 13:11:20 -0700453 return;
454
Jonathan Zhang3dcafa82022-05-11 13:11:20 -0700455 current = acpi_fill_cedt(current);
456
457 /* (Re)calculate length and checksum. */
458 header->length = current - (unsigned long)cedt;
459 header->checksum = acpi_checksum((void *)cedt, header->length);
460}
461
Jonathan Zhang2a4e1f42021-04-01 11:43:37 -0700462int acpi_create_hmat_mpda(acpi_hmat_mpda_t *mpda, u32 initiator, u32 memory)
463{
464 memset((void *)mpda, 0, sizeof(acpi_hmat_mpda_t));
465
466 mpda->type = 0; /* Memory Proximity Domain Attributes structure */
467 mpda->length = sizeof(acpi_hmat_mpda_t);
468 /*
469 * Proximity Domain for Attached Initiator field is valid.
470 * Bit 1 and bit 2 are reserved since HMAT revision 2.
471 */
472 mpda->flags = (1 << 0);
473 mpda->proximity_domain_initiator = initiator;
474 mpda->proximity_domain_memory = memory;
475
476 return mpda->length;
477}
478
479void acpi_create_hmat(acpi_hmat_t *hmat,
480 unsigned long (*acpi_fill_hmat)(unsigned long current))
481{
482 acpi_header_t *header = &(hmat->header);
483 unsigned long current = (unsigned long)hmat + sizeof(acpi_hmat_t);
484
485 memset((void *)hmat, 0, sizeof(acpi_hmat_t));
486
Arthur Heymansadb80072023-06-28 09:56:00 +0200487 if (acpi_fill_header(header, "HMAT", HMAT, sizeof(acpi_hmat_t)) != CB_SUCCESS)
Jonathan Zhang2a4e1f42021-04-01 11:43:37 -0700488 return;
489
Jonathan Zhang2a4e1f42021-04-01 11:43:37 -0700490 current = acpi_fill_hmat(current);
491
492 /* (Re)calculate length and checksum. */
493 header->length = current - (unsigned long)hmat;
494 header->checksum = acpi_checksum((void *)hmat, header->length);
495}
496
Uwe Hermann622824c2010-11-19 15:14:42 +0000497/* http://h21007.www2.hp.com/portal/download/files/unprot/Itanium/slit.pdf */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +0200498void acpi_create_slit(acpi_slit_t *slit,
499 unsigned long (*acpi_fill_slit)(unsigned long current))
Yinghai Lud4b278c2006-10-04 20:46:15 +0000500{
Uwe Hermann622824c2010-11-19 15:14:42 +0000501 acpi_header_t *header = &(slit->header);
502 unsigned long current = (unsigned long)slit + sizeof(acpi_slit_t);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000503
Uwe Hermann622824c2010-11-19 15:14:42 +0000504 memset((void *)slit, 0, sizeof(acpi_slit_t));
Yinghai Lud4b278c2006-10-04 20:46:15 +0000505
Arthur Heymansadb80072023-06-28 09:56:00 +0200506 if (acpi_fill_header(header, "SLIT", SLIT, sizeof(acpi_slit_t)) != CB_SUCCESS)
John Zhao2ba303e2019-05-28 16:48:14 -0700507 return;
508
Uwe Hermann622824c2010-11-19 15:14:42 +0000509 current = acpi_fill_slit(current);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000510
Uwe Hermann622824c2010-11-19 15:14:42 +0000511 /* (Re)calculate length and checksum. */
512 header->length = current - (unsigned long)slit;
513 header->checksum = acpi_checksum((void *)slit, header->length);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000514}
515
Rocky Phaguraeff07132021-01-10 15:42:50 -0800516/*
517 * This method adds the ACPI error injection capability. It fills the default information.
518 * HW dependent code (caller) can modify the defaults upon return. If no changes are necessary
519 * and the defaults are acceptable then caller can simply add the table (acpi_add_table).
520 * INPUTS:
521 * einj - ptr to the starting location of EINJ table
522 * actions - number of actions to trigger an error (HW dependent)
523 * addr - address of trigger action table. This should be ACPI reserved memory and it will be
524 * shared between OS and FW.
525 */
526void acpi_create_einj(acpi_einj_t *einj, uintptr_t addr, u8 actions)
527{
528 int i;
529 acpi_header_t *header = &(einj->header);
530 acpi_injection_header_t *inj_header = &(einj->inj_header);
531 acpi_einj_smi_t *einj_smi = (acpi_einj_smi_t *)addr;
532 acpi_einj_trigger_table_t *tat;
533 if (!header)
534 return;
535
536 printk(BIOS_DEBUG, "%s einj_smi = %p\n", __func__, einj_smi);
537 memset(einj_smi, 0, sizeof(acpi_einj_smi_t));
Jonathan Zhangd5d9b282022-11-07 17:30:14 -0800538 tat = (acpi_einj_trigger_table_t *)((uint8_t *)einj_smi + sizeof(acpi_einj_smi_t));
Rocky Phaguraeff07132021-01-10 15:42:50 -0800539 tat->header_size = 16;
540 tat->revision = 0;
541 tat->table_size = sizeof(acpi_einj_trigger_table_t) +
542 sizeof(acpi_einj_action_table_t) * actions - 1;
543 tat->entry_count = actions;
544 printk(BIOS_DEBUG, "%s trigger_action_table = %p\n", __func__, tat);
545
546 for (i = 0; i < actions; i++) {
547 tat->trigger_action[i].action = TRIGGER_ERROR;
548 tat->trigger_action[i].instruction = NO_OP;
549 tat->trigger_action[i].flags = FLAG_IGNORE;
550 tat->trigger_action[i].reg.space_id = ACPI_ADDRESS_SPACE_MEMORY;
551 tat->trigger_action[i].reg.bit_width = 64;
552 tat->trigger_action[i].reg.bit_offset = 0;
553 tat->trigger_action[i].reg.access_size = ACPI_ACCESS_SIZE_QWORD_ACCESS;
554 tat->trigger_action[i].reg.addr = 0;
555 tat->trigger_action[i].value = 0;
556 tat->trigger_action[i].mask = 0xFFFFFFFF;
557 }
558
559 acpi_einj_action_table_t default_actions[ACTION_COUNT] = {
560 [0] = {
561 .action = BEGIN_INJECT_OP,
562 .instruction = WRITE_REGISTER_VALUE,
563 .flags = FLAG_PRESERVE,
564 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->op_state),
565 .value = 0,
566 .mask = 0xFFFFFFFF
567 },
568 [1] = {
569 .action = GET_TRIGGER_ACTION_TABLE,
570 .instruction = READ_REGISTER,
571 .flags = FLAG_IGNORE,
572 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->trigger_action_table),
573 .value = 0,
574 .mask = 0xFFFFFFFFFFFFFFFF
575 },
576 [2] = {
577 .action = SET_ERROR_TYPE,
578 .instruction = WRITE_REGISTER,
579 .flags = FLAG_PRESERVE,
580 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->err_inject[0]),
581 .value = 0,
582 .mask = 0xFFFFFFFF
583 },
584 [3] = {
585 .action = GET_ERROR_TYPE,
586 .instruction = READ_REGISTER,
587 .flags = FLAG_IGNORE,
588 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->err_inj_cap),
589 .value = 0,
590 .mask = 0xFFFFFFFF
591 },
592 [4] = {
593 .action = END_INJECT_OP,
594 .instruction = WRITE_REGISTER_VALUE,
595 .flags = FLAG_PRESERVE,
596 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->op_state),
597 .value = 0,
598 .mask = 0xFFFFFFFF
599
600 },
601 [5] = {
602 .action = EXECUTE_INJECT_OP,
603 .instruction = WRITE_REGISTER_VALUE,
604 .flags = FLAG_PRESERVE,
605 .reg = EINJ_REG_IO(),
606 .value = 0x9a,
607 .mask = 0xFFFF,
608 },
609 [6] = {
610 .action = CHECK_BUSY_STATUS,
611 .instruction = READ_REGISTER_VALUE,
612 .flags = FLAG_IGNORE,
613 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->op_status),
614 .value = 1,
615 .mask = 1,
616 },
617 [7] = {
618 .action = GET_CMD_STATUS,
619 .instruction = READ_REGISTER,
620 .flags = FLAG_PRESERVE,
621 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->cmd_sts),
622 .value = 0,
623 .mask = 0x1fe,
624 },
625 [8] = {
626 .action = SET_ERROR_TYPE_WITH_ADDRESS,
627 .instruction = WRITE_REGISTER,
628 .flags = FLAG_PRESERVE,
629 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->setaddrtable),
630 .value = 1,
631 .mask = 0xffffffff
632 }
633 };
634
635 einj_smi->err_inj_cap = ACPI_EINJ_DEFAULT_CAP;
Elyes Haouas3141fbad2022-11-18 15:22:32 +0100636 einj_smi->trigger_action_table = (u64)(uintptr_t)tat;
Rocky Phaguraeff07132021-01-10 15:42:50 -0800637
638 for (i = 0; i < ACTION_COUNT; i++)
639 printk(BIOS_DEBUG, "default_actions[%d].reg.addr is %llx\n", i,
640 default_actions[i].reg.addr);
641
Rocky Phagurab46a9e52021-05-21 13:34:03 -0700642 memset((void *)einj, 0, sizeof(*einj));
Rocky Phaguraeff07132021-01-10 15:42:50 -0800643
Arthur Heymansadb80072023-06-28 09:56:00 +0200644 if (acpi_fill_header(header, "EINJ", EINJ, sizeof(acpi_einj_t)) != CB_SUCCESS)
645 return;
Rocky Phaguraeff07132021-01-10 15:42:50 -0800646
Rocky Phaguraeff07132021-01-10 15:42:50 -0800647 inj_header->einj_header_size = sizeof(acpi_injection_header_t);
648 inj_header->entry_count = ACTION_COUNT;
649
650 printk(BIOS_DEBUG, "%s einj->action_table = %p\n",
651 __func__, einj->action_table);
652 memcpy((void *)einj->action_table, (void *)default_actions, sizeof(einj->action_table));
Rocky Phagurab46a9e52021-05-21 13:34:03 -0700653 header->checksum = acpi_checksum((void *)einj, sizeof(*einj));
Rocky Phaguraeff07132021-01-10 15:42:50 -0800654}
655
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700656void acpi_create_vfct(const struct device *device,
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530657 acpi_vfct_t *vfct,
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700658 unsigned long (*acpi_fill_vfct)(const struct device *device,
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530659 acpi_vfct_t *vfct_struct, unsigned long current))
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200660{
661 acpi_header_t *header = &(vfct->header);
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530662 unsigned long current = (unsigned long)vfct + sizeof(acpi_vfct_t);
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200663
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530664 memset((void *)vfct, 0, sizeof(acpi_vfct_t));
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200665
Arthur Heymansadb80072023-06-28 09:56:00 +0200666 if (acpi_fill_header(header, "VFCT", VFCT, sizeof(acpi_vfct_t)) != CB_SUCCESS)
John Zhao2ba303e2019-05-28 16:48:14 -0700667 return;
668
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200669 current = acpi_fill_vfct(device, vfct, current);
670
Kyösti Mälkkifb493792019-06-30 06:29:52 +0300671 /* If no BIOS image, return with header->length == 0. */
672 if (!vfct->VBIOSImageOffset)
673 return;
674
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200675 /* (Re)calculate length and checksum. */
676 header->length = current - (unsigned long)vfct;
677 header->checksum = acpi_checksum((void *)vfct, header->length);
678}
679
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700680void acpi_create_ipmi(const struct device *device,
Patrick Rudolph9d98e5a2019-06-14 19:00:04 +0200681 struct acpi_spmi *spmi,
682 const u16 ipmi_revision,
683 const acpi_addr_t *addr,
684 const enum acpi_ipmi_interface_type type,
685 const s8 gpe_interrupt,
686 const u32 apic_interrupt,
687 const u32 uid)
688{
689 acpi_header_t *header = &(spmi->header);
690 memset((void *)spmi, 0, sizeof(struct acpi_spmi));
691
Arthur Heymansadb80072023-06-28 09:56:00 +0200692 if (acpi_fill_header(header, "SPMI", SPMI, sizeof(struct acpi_spmi)) != CB_SUCCESS)
693 return;
Patrick Rudolph9d98e5a2019-06-14 19:00:04 +0200694
695 spmi->reserved = 1;
696
697 if (device->path.type == DEVICE_PATH_PCI) {
698 spmi->pci_device_flag = ACPI_IPMI_PCI_DEVICE_FLAG;
699 spmi->pci_bus = device->bus->secondary;
700 spmi->pci_device = device->path.pci.devfn >> 3;
701 spmi->pci_function = device->path.pci.devfn & 0x7;
702 } else if (type != IPMI_INTERFACE_SSIF) {
703 memcpy(spmi->uid, &uid, sizeof(spmi->uid));
704 }
705
706 spmi->base_address = *addr;
707 spmi->specification_revision = ipmi_revision;
708
709 spmi->interface_type = type;
710
711 if (gpe_interrupt >= 0 && gpe_interrupt < 32) {
712 spmi->gpe = gpe_interrupt;
713 spmi->interrupt_type |= ACPI_IPMI_INT_TYPE_SCI;
714 }
715 if (apic_interrupt > 0) {
716 spmi->global_system_interrupt = apic_interrupt;
717 spmi->interrupt_type |= ACPI_IPMI_INT_TYPE_APIC;
718 }
719
720 /* Calculate checksum. */
721 header->checksum = acpi_checksum((void *)spmi, header->length);
722}
723
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500724void acpi_create_ivrs(acpi_ivrs_t *ivrs,
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700725 unsigned long (*acpi_fill_ivrs)(acpi_ivrs_t *ivrs_struct,
726 unsigned long current))
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500727{
728 acpi_header_t *header = &(ivrs->header);
729 unsigned long current = (unsigned long)ivrs + sizeof(acpi_ivrs_t);
730
731 memset((void *)ivrs, 0, sizeof(acpi_ivrs_t));
732
Arthur Heymansadb80072023-06-28 09:56:00 +0200733 if (acpi_fill_header(header, "IVRS", IVRS, sizeof(acpi_ivrs_t)) != CB_SUCCESS)
John Zhao2ba303e2019-05-28 16:48:14 -0700734 return;
735
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500736 current = acpi_fill_ivrs(ivrs, current);
737
738 /* (Re)calculate length and checksum. */
739 header->length = current - (unsigned long)ivrs;
740 header->checksum = acpi_checksum((void *)ivrs, header->length);
741}
742
Jason Glenesk61624b22020-11-02 20:06:23 -0800743void acpi_create_crat(struct acpi_crat_header *crat,
744 unsigned long (*acpi_fill_crat)(struct acpi_crat_header *crat_struct,
745 unsigned long current))
746{
747 acpi_header_t *header = &(crat->header);
748 unsigned long current = (unsigned long)crat + sizeof(struct acpi_crat_header);
749
750 memset((void *)crat, 0, sizeof(struct acpi_crat_header));
751
Arthur Heymansadb80072023-06-28 09:56:00 +0200752 if (acpi_fill_header(header, "CRAT", CRAT, sizeof(struct acpi_crat_header)) != CB_SUCCESS)
Jason Glenesk61624b22020-11-02 20:06:23 -0800753 return;
754
Jason Glenesk61624b22020-11-02 20:06:23 -0800755 current = acpi_fill_crat(crat, current);
756
757 /* (Re)calculate length and checksum. */
758 header->length = current - (unsigned long)crat;
759 header->checksum = acpi_checksum((void *)crat, header->length);
760}
761
Arthur Heymans9368cf92023-04-25 16:07:49 +0200762static void acpi_create_dbg2(acpi_dbg2_header_t *dbg2,
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800763 int port_type, int port_subtype,
764 acpi_addr_t *address, uint32_t address_size,
765 const char *device_path)
766{
767 uintptr_t current;
768 acpi_dbg2_device_t *device;
769 uint32_t *dbg2_addr_size;
770 acpi_header_t *header;
771 size_t path_len;
772 const char *path;
773 char *namespace;
774
775 /* Fill out header fields. */
776 current = (uintptr_t)dbg2;
777 memset(dbg2, 0, sizeof(acpi_dbg2_header_t));
778 header = &(dbg2->header);
John Zhao2ba303e2019-05-28 16:48:14 -0700779
Arthur Heymansadb80072023-06-28 09:56:00 +0200780 if (acpi_fill_header(header, "DBG2", DBG2, sizeof(acpi_dbg2_header_t)) != CB_SUCCESS)
John Zhao2ba303e2019-05-28 16:48:14 -0700781 return;
782
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800783 /* One debug device defined */
784 dbg2->devices_offset = sizeof(acpi_dbg2_header_t);
785 dbg2->devices_count = 1;
786 current += sizeof(acpi_dbg2_header_t);
787
788 /* Device comes after the header */
789 device = (acpi_dbg2_device_t *)current;
790 memset(device, 0, sizeof(acpi_dbg2_device_t));
791 current += sizeof(acpi_dbg2_device_t);
792
793 device->revision = 0;
794 device->address_count = 1;
795 device->port_type = port_type;
796 device->port_subtype = port_subtype;
797
798 /* Base Address comes after device structure */
799 memcpy((void *)current, address, sizeof(acpi_addr_t));
800 device->base_address_offset = current - (uintptr_t)device;
801 current += sizeof(acpi_addr_t);
802
803 /* Address Size comes after address structure */
804 dbg2_addr_size = (uint32_t *)current;
805 device->address_size_offset = current - (uintptr_t)device;
806 *dbg2_addr_size = address_size;
807 current += sizeof(uint32_t);
808
809 /* Namespace string comes last, use '.' if not provided */
810 path = device_path ? : ".";
811 /* Namespace string length includes NULL terminator */
812 path_len = strlen(path) + 1;
813 namespace = (char *)current;
814 device->namespace_string_length = path_len;
815 device->namespace_string_offset = current - (uintptr_t)device;
816 strncpy(namespace, path, path_len);
817 current += path_len;
818
819 /* Update structure lengths and checksum */
820 device->length = current - (uintptr_t)device;
821 header->length = current - (uintptr_t)dbg2;
822 header->checksum = acpi_checksum((uint8_t *)dbg2, header->length);
823}
824
825unsigned long acpi_write_dbg2_pci_uart(acpi_rsdp_t *rsdp, unsigned long current,
Aamir Bohrae825d3f2019-07-30 10:11:41 +0530826 const struct device *dev, uint8_t access_size)
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800827{
828 acpi_dbg2_header_t *dbg2 = (acpi_dbg2_header_t *)current;
829 struct resource *res;
830 acpi_addr_t address;
831
832 if (!dev) {
Wim Vervoorn6cd52432020-02-03 14:41:46 +0100833 printk(BIOS_DEBUG, "%s: Device not found\n", __func__);
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800834 return current;
835 }
Mario Scheithauerf1eb0ea2017-11-21 13:52:53 +0100836 if (!dev->enabled) {
837 printk(BIOS_INFO, "%s: Device not enabled\n", __func__);
838 return current;
839 }
Angel Pons32d09be2021-11-03 13:27:45 +0100840 res = probe_resource(dev, PCI_BASE_ADDRESS_0);
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800841 if (!res) {
842 printk(BIOS_ERR, "%s: Unable to find resource for %s\n",
843 __func__, dev_path(dev));
844 return current;
845 }
846
847 memset(&address, 0, sizeof(address));
848 if (res->flags & IORESOURCE_IO)
849 address.space_id = ACPI_ADDRESS_SPACE_IO;
850 else if (res->flags & IORESOURCE_MEM)
851 address.space_id = ACPI_ADDRESS_SPACE_MEMORY;
852 else {
853 printk(BIOS_ERR, "%s: Unknown address space type\n", __func__);
854 return current;
855 }
856
857 address.addrl = (uint32_t)res->base;
858 address.addrh = (uint32_t)((res->base >> 32) & 0xffffffff);
859 address.access_size = access_size;
860
861 acpi_create_dbg2(dbg2,
862 ACPI_DBG2_PORT_SERIAL,
863 ACPI_DBG2_PORT_SERIAL_16550,
864 &address, res->size,
865 acpi_device_path(dev));
866
867 if (dbg2->header.length) {
868 current += dbg2->header.length;
869 current = acpi_align_current(current);
870 acpi_add_table(rsdp, dbg2);
871 }
872
873 return current;
874}
875
Arthur Heymans01af0f82023-06-27 11:58:04 +0200876static void acpi_create_facs(void *header)
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000877{
Arthur Heymans01af0f82023-06-27 11:58:04 +0200878 acpi_facs_t *facs = header;
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000879
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000880 memcpy(facs->signature, "FACS", 4);
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000881 facs->length = sizeof(acpi_facs_t);
882 facs->hardware_signature = 0;
883 facs->firmware_waking_vector = 0;
884 facs->global_lock = 0;
885 facs->flags = 0;
886 facs->x_firmware_waking_vector_l = 0;
887 facs->x_firmware_waking_vector_h = 0;
Marc Jones93a51762018-08-22 18:57:24 -0600888 facs->version = get_acpi_table_revision(FACS);
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000889}
Stefan Reinauer777224c2005-01-19 14:06:41 +0000890
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100891static void acpi_write_rsdt(acpi_rsdt_t *rsdt, char *oem_id, char *oem_table_id)
Stefan Reinauer14e22772010-04-27 06:56:47 +0000892{
Uwe Hermann622824c2010-11-19 15:14:42 +0000893 acpi_header_t *header = &(rsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000894
Arthur Heymansadb80072023-06-28 09:56:00 +0200895 if (acpi_fill_header(header, "RSDT", RSDT, sizeof(acpi_rsdt_t)) != CB_SUCCESS)
John Zhao2ba303e2019-05-28 16:48:14 -0700896 return;
897
Uwe Hermann622824c2010-11-19 15:14:42 +0000898 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauer688b3852004-01-28 16:56:14 +0000899
Uwe Hermann622824c2010-11-19 15:14:42 +0000900 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000901 header->checksum = acpi_checksum((void *)rsdt, sizeof(acpi_rsdt_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000902}
903
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100904static void acpi_write_xsdt(acpi_xsdt_t *xsdt, char *oem_id, char *oem_table_id)
Stefan Reinauer14e22772010-04-27 06:56:47 +0000905{
Uwe Hermann622824c2010-11-19 15:14:42 +0000906 acpi_header_t *header = &(xsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000907
Arthur Heymansadb80072023-06-28 09:56:00 +0200908 if (acpi_fill_header(header, "XSDT", XSDT, sizeof(acpi_xsdt_t)) != CB_SUCCESS)
John Zhao2ba303e2019-05-28 16:48:14 -0700909 return;
910
Uwe Hermann622824c2010-11-19 15:14:42 +0000911 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000912
Uwe Hermann622824c2010-11-19 15:14:42 +0000913 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000914 header->checksum = acpi_checksum((void *)xsdt, sizeof(acpi_xsdt_t));
915}
916
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100917static void acpi_write_rsdp(acpi_rsdp_t *rsdp, acpi_rsdt_t *rsdt,
918 acpi_xsdt_t *xsdt, char *oem_id)
Stefan Reinauer688b3852004-01-28 16:56:14 +0000919{
Stefan Reinauerd18faac2009-11-05 18:06:43 +0000920 memset(rsdp, 0, sizeof(acpi_rsdp_t));
Uwe Hermann622824c2010-11-19 15:14:42 +0000921
Stefan Reinauer688b3852004-01-28 16:56:14 +0000922 memcpy(rsdp->signature, RSDP_SIG, 8);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100923 memcpy(rsdp->oem_id, oem_id, 6);
Uwe Hermann622824c2010-11-19 15:14:42 +0000924
925 rsdp->length = sizeof(acpi_rsdp_t);
Stefan Reinauerdefee172015-06-18 01:11:19 -0700926 rsdp->rsdt_address = (uintptr_t)rsdt;
Uwe Hermann622824c2010-11-19 15:14:42 +0000927
928 /*
929 * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2.
930 *
931 * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
932 * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
933 * revision 0).
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000934 */
935 if (xsdt == NULL) {
Uwe Hermann622824c2010-11-19 15:14:42 +0000936 rsdp->revision = 0;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000937 } else {
Stefan Reinauerdefee172015-06-18 01:11:19 -0700938 rsdp->xsdt_address = (u64)(uintptr_t)xsdt;
Marc Jones93a51762018-08-22 18:57:24 -0600939 rsdp->revision = get_acpi_table_revision(RSDP);
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000940 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000941
942 /* Calculate checksums. */
943 rsdp->checksum = acpi_checksum((void *)rsdp, 20);
944 rsdp->ext_checksum = acpi_checksum((void *)rsdp, sizeof(acpi_rsdp_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000945}
946
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700947unsigned long acpi_create_hest_error_source(acpi_hest_t *hest,
948 acpi_hest_esd_t *esd, u16 type, void *data, u16 data_len)
zbaocaf494c82012-04-13 13:57:14 +0800949{
950 acpi_header_t *header = &(hest->header);
951 acpi_hest_hen_t *hen;
952 void *pos;
953 u16 len;
954
955 pos = esd;
956 memset(pos, 0, sizeof(acpi_hest_esd_t));
957 len = 0;
958 esd->type = type; /* MCE */
959 esd->source_id = hest->error_source_count;
960 esd->flags = 0; /* FIRMWARE_FIRST */
961 esd->enabled = 1;
962 esd->prealloc_erecords = 1;
963 esd->max_section_per_record = 0x1;
964
965 len += sizeof(acpi_hest_esd_t);
966 pos = esd + 1;
967
968 switch (type) {
969 case 0: /* MCE */
970 break;
971 case 1: /* CMC */
Elyes Haouas3141fbad2022-11-18 15:22:32 +0100972 hen = (acpi_hest_hen_t *)(pos);
zbaocaf494c82012-04-13 13:57:14 +0800973 memset(pos, 0, sizeof(acpi_hest_hen_t));
974 hen->type = 3; /* SCI? */
975 hen->length = sizeof(acpi_hest_hen_t);
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700976 hen->conf_we = 0; /* Configuration Write Enable. */
zbaocaf494c82012-04-13 13:57:14 +0800977 hen->poll_interval = 0;
978 hen->vector = 0;
979 hen->sw2poll_threshold_val = 0;
980 hen->sw2poll_threshold_win = 0;
981 hen->error_threshold_val = 0;
982 hen->error_threshold_win = 0;
983 len += sizeof(acpi_hest_hen_t);
984 pos = hen + 1;
985 break;
986 case 2: /* NMI */
987 case 6: /* AER Root Port */
988 case 7: /* AER Endpoint */
989 case 8: /* AER Bridge */
990 case 9: /* Generic Hardware Error Source. */
991 /* TODO: */
992 break;
993 default:
994 printk(BIOS_DEBUG, "Invalid type of Error Source.");
995 break;
996 }
Lee Leahy024b13d2017-03-16 13:41:11 -0700997 hest->error_source_count++;
zbaocaf494c82012-04-13 13:57:14 +0800998
999 memcpy(pos, data, data_len);
1000 len += data_len;
John Zhao2ba303e2019-05-28 16:48:14 -07001001 if (header)
1002 header->length += len;
zbaocaf494c82012-04-13 13:57:14 +08001003
1004 return len;
1005}
1006
1007/* ACPI 4.0 */
Vladimir Serbinenko807127f2014-11-09 13:36:18 +01001008void acpi_write_hest(acpi_hest_t *hest,
1009 unsigned long (*acpi_fill_hest)(acpi_hest_t *hest))
zbaocaf494c82012-04-13 13:57:14 +08001010{
1011 acpi_header_t *header = &(hest->header);
1012
1013 memset(hest, 0, sizeof(acpi_hest_t));
1014
Arthur Heymansadb80072023-06-28 09:56:00 +02001015 if (acpi_fill_header(header, "HEST", HEST, sizeof(acpi_hest_t)) != CB_SUCCESS)
John Zhao2ba303e2019-05-28 16:48:14 -07001016 return;
1017
zbaocaf494c82012-04-13 13:57:14 +08001018 acpi_fill_hest(hest);
1019
1020 /* Calculate checksums. */
1021 header->checksum = acpi_checksum((void *)hest, header->length);
1022}
1023
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001024/* ACPI 3.0b */
Arthur Heymans01af0f82023-06-27 11:58:04 +02001025static void acpi_create_bert(acpi_header_t *header, void *unused)
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001026{
Arthur Heymans01af0f82023-06-27 11:58:04 +02001027 if (!CONFIG(ACPI_BERT))
1028 return;
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001029
Arthur Heymans01af0f82023-06-27 11:58:04 +02001030 acpi_bert_t *bert = (acpi_bert_t *)header;
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001031
Arthur Heymans01af0f82023-06-27 11:58:04 +02001032 void *region;
1033 size_t size;
1034 if (acpi_soc_get_bert_region(&region, &size) != CB_SUCCESS)
1035 return;
1036
Arthur Heymansadb80072023-06-28 09:56:00 +02001037 if (acpi_fill_header(header, "BERT", BERT, sizeof(acpi_bert_t)) != CB_SUCCESS)
1038 return;
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001039
Arthur Heymans01af0f82023-06-27 11:58:04 +02001040 bert->error_region = (uintptr_t)region;
1041 bert->region_length = (size_t)size;
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001042}
1043
Angel Pons79572e42020-07-13 00:17:43 +02001044__weak void arch_fill_fadt(acpi_fadt_t *fadt) { }
Kyösti Mälkkif9aac922020-05-30 16:16:28 +03001045__weak void soc_fill_fadt(acpi_fadt_t *fadt) { }
Kyösti Mälkki02fd15d2020-06-02 03:34:43 +03001046__weak void mainboard_fill_fadt(acpi_fadt_t *fadt) { }
Kyösti Mälkkif9aac922020-05-30 16:16:28 +03001047
Arthur Heymans01af0f82023-06-27 11:58:04 +02001048static acpi_header_t *dsdt;
1049static void acpi_create_fadt(acpi_header_t *header, void *arg1)
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001050{
Arthur Heymans01af0f82023-06-27 11:58:04 +02001051 acpi_fadt_t *fadt = (acpi_fadt_t *)header;
1052 acpi_facs_t *facs = (acpi_facs_t *)(*(acpi_facs_t **)arg1);
John Zhao2ba303e2019-05-28 16:48:14 -07001053
Arthur Heymansadb80072023-06-28 09:56:00 +02001054 if (acpi_fill_header(header, "FACP", FADT, sizeof(acpi_fadt_t)) != CB_SUCCESS)
John Zhao2ba303e2019-05-28 16:48:14 -07001055 return;
1056
Elyes Haouas532e0432022-02-21 18:13:58 +01001057 fadt->FADT_MinorVersion = get_acpi_fadt_minor_version();
Arthur Heymans8473e8f2023-06-29 20:26:39 +02001058 if ((uintptr_t)facs <= UINT32_MAX)
1059 fadt->firmware_ctrl = (uintptr_t)facs;
1060 else
1061 fadt->x_firmware_ctl_h = (uint32_t)((uint64_t)(uintptr_t)facs >> 32);
1062 fadt->x_firmware_ctl_l = (uint32_t)(uintptr_t)facs;
Kyösti Mälkkid4acee82020-06-03 07:20:55 +03001063
Arthur Heymans8473e8f2023-06-29 20:26:39 +02001064 if ((uintptr_t)dsdt <= UINT32_MAX)
1065 fadt->dsdt = (uintptr_t)dsdt;
1066 else
1067 fadt->x_dsdt_h = (uint32_t)((uint64_t)(uintptr_t)dsdt >> 32);
1068 fadt->x_dsdt_l = (uint32_t)(uintptr_t)dsdt;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001069
Kyösti Mälkkid4acee82020-06-03 07:20:55 +03001070 /* should be 0 ACPI 3.0 */
1071 fadt->reserved = 0;
1072
Kyösti Mälkki67c48a32023-04-14 10:20:03 +03001073 /* P_LVLx latencies are not used as CPU _CST will override them. */
1074 fadt->p_lvl2_lat = ACPI_FADT_C2_NOT_SUPPORTED;
1075 fadt->p_lvl3_lat = ACPI_FADT_C3_NOT_SUPPORTED;
1076
Kyösti Mälkki9ff97972023-04-14 15:37:27 +03001077 /* Use CPU _PTC instead to provide P_CNT details. */
1078 fadt->duty_offset = 0;
1079 fadt->duty_width = 0;
1080
Kyösti Mälkkie0d38682020-06-07 12:01:58 +03001081 fadt->preferred_pm_profile = acpi_get_preferred_pm_profile();
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001082
Angel Pons79572e42020-07-13 00:17:43 +02001083 arch_fill_fadt(fadt);
1084
Vladimir Serbinenkoab83ef02014-10-25 15:18:25 +02001085 acpi_fill_fadt(fadt);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001086
Kyösti Mälkkif9aac922020-05-30 16:16:28 +03001087 soc_fill_fadt(fadt);
Kyösti Mälkki02fd15d2020-06-02 03:34:43 +03001088 mainboard_fill_fadt(fadt);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001089}
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001090
Arthur Heymans01af0f82023-06-27 11:58:04 +02001091static void acpi_create_lpit(acpi_header_t *header, void *unused)
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001092{
Arthur Heymans01af0f82023-06-27 11:58:04 +02001093 if (!CONFIG(ACPI_LPIT))
1094 return;
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001095
Arthur Heymans01af0f82023-06-27 11:58:04 +02001096 acpi_lpit_t *lpit = (acpi_lpit_t *)header;
1097 unsigned long current = (unsigned long)lpit + sizeof(acpi_lpit_t);
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001098
Arthur Heymansadb80072023-06-28 09:56:00 +02001099 if (acpi_fill_header(header, "LPIT", LPIT, sizeof(acpi_lpit_t)) != CB_SUCCESS)
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001100 return;
1101
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001102 current = acpi_fill_lpit(current);
1103
Arthur Heymans01af0f82023-06-27 11:58:04 +02001104 /* (Re)calculate length. */
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001105 header->length = current - (unsigned long)lpit;
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001106}
1107
Arthur Heymans2e3cb632023-06-30 15:01:08 +02001108static void acpi_create_gtdt(acpi_header_t *header, void *unused)
1109{
1110 if (!CONFIG(ACPI_GTDT))
1111 return;
1112
1113 acpi_gtdt_t *gtdt = (acpi_gtdt_t *)header;
1114 unsigned long current = (unsigned long)gtdt + sizeof(acpi_gtdt_t);
1115
1116 if (acpi_fill_header(header, "GTDT", GTDT, sizeof(acpi_gtdt_t)) != CB_SUCCESS)
1117 return;
1118
1119 /* Fill out header fields. */
1120 gtdt->platform_timer_offset = sizeof(acpi_gtdt_t);
1121
1122 acpi_soc_fill_gtdt(gtdt);
1123 current = acpi_soc_gtdt_add_timers(&gtdt->platform_timer_count, current);
1124
1125 /* (Re)calculate length. */
1126 header->length = current - (unsigned long)gtdt;
1127}
1128
1129unsigned long acpi_gtdt_add_timer_block(unsigned long current, const uint64_t address,
1130 struct acpi_gtdt_timer_entry *timers, size_t number)
1131{
1132 struct acpi_gtdt_timer_block *block = (struct acpi_gtdt_timer_block *)current;
1133 memset(block, 0, sizeof(struct acpi_gtdt_timer_block));
1134
1135 assert(number < 8 && number != 0);
1136 const size_t entries_size = number * sizeof(struct acpi_gtdt_timer_entry);
1137
1138 block->header.type = ACPI_GTDT_TYPE_TIMER_BLOCK;
1139 block->header.length = sizeof(struct acpi_gtdt_timer_block)
1140 + entries_size;
1141 block->block_address = address;
1142 block->timer_count = number;
1143 block->timer_offset = sizeof(struct acpi_gtdt_timer_block);
1144 current += sizeof(struct acpi_gtdt_timer_block);
1145 memcpy((void *)current, timers, entries_size);
1146 current += entries_size;
1147 return current;
1148}
1149
1150unsigned long acpi_gtdt_add_watchdog(unsigned long current, uint64_t refresh_frame,
1151 uint64_t control_frame, uint32_t gsiv, uint32_t flags)
1152{
1153 struct acpi_gtdt_watchdog *wd = (struct acpi_gtdt_watchdog *)current;
1154 memset(wd, 0, sizeof(struct acpi_gtdt_watchdog));
1155
1156 wd->header.type = ACPI_GTDT_TYPE_WATCHDOG;
1157 wd->header.length = sizeof(struct acpi_gtdt_watchdog);
1158 wd->refresh_frame_address = refresh_frame;
1159 wd->control_frame_address = control_frame;
1160 wd->timer_interrupt = gsiv;
1161 wd->timer_flags = flags;
1162
1163 return current + sizeof(struct acpi_gtdt_watchdog);
1164}
1165
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001166unsigned long acpi_create_lpi_desc_ncst(acpi_lpi_desc_ncst_t *lpi_desc, uint16_t uid)
1167{
1168 memset(lpi_desc, 0, sizeof(acpi_lpi_desc_ncst_t));
1169 lpi_desc->header.length = sizeof(acpi_lpi_desc_ncst_t);
1170 lpi_desc->header.type = ACPI_LPI_DESC_TYPE_NATIVE_CSTATE;
1171 lpi_desc->header.uid = uid;
1172
1173 return lpi_desc->header.length;
1174}
1175
Arthur Heymans90464072023-06-07 12:53:50 +02001176static uint8_t acpi_spcr_type(void)
1177{
1178 /* 16550-compatible with parameters defined in Generic Address Structure */
1179 if (CONFIG(DRIVERS_UART_8250IO) || CONFIG(DRIVERS_UART_8250MEM))
1180 return 0x12;
1181 if (CONFIG(DRIVERS_UART_PL011))
1182 return 0x3;
1183
1184 printk(BIOS_ERR, "%s: unknown serial type\n", __func__);
1185 return 0xff;
1186}
1187
Arthur Heymans01af0f82023-06-27 11:58:04 +02001188static void acpi_create_spcr(acpi_header_t *header, void *unused)
Arthur Heymans90464072023-06-07 12:53:50 +02001189{
Arthur Heymans01af0f82023-06-27 11:58:04 +02001190 acpi_spcr_t *spcr = (acpi_spcr_t *)header;
Arthur Heymans90464072023-06-07 12:53:50 +02001191 struct lb_serial serial;
1192
Arthur Heymans90464072023-06-07 12:53:50 +02001193 if (!CONFIG(CONSOLE_SERIAL))
1194 return;
1195
1196 if (fill_lb_serial(&serial) != CB_SUCCESS)
1197 return;
1198
Arthur Heymansadb80072023-06-28 09:56:00 +02001199 if (acpi_fill_header(header, "SPCR", SPCR, sizeof(acpi_spcr_t)) != CB_SUCCESS)
1200 return;
Arthur Heymans90464072023-06-07 12:53:50 +02001201
1202 spcr->interface_type = acpi_spcr_type();
1203 assert(serial.type == LB_SERIAL_TYPE_IO_MAPPED
1204 || serial.type == LB_SERIAL_TYPE_MEMORY_MAPPED);
1205 spcr->base_address.space_id = serial.type == LB_SERIAL_TYPE_IO_MAPPED ?
1206 ACPI_ADDRESS_SPACE_IO : ACPI_ADDRESS_SPACE_MEMORY;
1207 spcr->base_address.bit_width = serial.regwidth * 8;
1208 spcr->base_address.bit_offset = 0;
1209 switch (serial.regwidth) {
1210 case 1:
1211 spcr->base_address.access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS;
1212 break;
1213 case 2:
1214 spcr->base_address.access_size = ACPI_ACCESS_SIZE_WORD_ACCESS;
1215 break;
1216 case 4:
1217 spcr->base_address.access_size = ACPI_ACCESS_SIZE_DWORD_ACCESS;
1218 break;
1219 default:
1220 printk(BIOS_ERR, "%s, Invalid serial regwidth\n", __func__);
1221 }
1222
1223 spcr->base_address.addrl = serial.baseaddr;
1224 spcr->base_address.addrh = 0;
1225 spcr->interrupt_type = 0;
1226 spcr->irq = 0;
1227 spcr->configured_baudrate = 0; /* Have the OS use whatever is currently set */
1228 spcr->parity = 0;
1229 spcr->stop_bits = 1;
1230 spcr->flow_control = 0;
1231 spcr->terminal_type = 2; /* 2 = VT-UTF8 */
1232 spcr->language = 0;
1233 spcr->pci_did = 0xffff;
1234 spcr->pci_vid = 0xffff;
Nico Huberfeb27dc2023-06-28 20:09:30 +02001235
1236 header->checksum = acpi_checksum((void *)spcr, header->length);
Arthur Heymans90464072023-06-07 12:53:50 +02001237}
1238
Aaron Durbin64031672018-04-21 14:45:32 -06001239unsigned long __weak fw_cfg_acpi_tables(unsigned long start)
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001240{
1241 return 0;
1242}
1243
Raul E Rangel6b446b92021-11-19 11:38:35 -07001244void preload_acpi_dsdt(void)
1245{
1246 const char *file = CONFIG_CBFS_PREFIX "/dsdt.aml";
1247
1248 if (!CONFIG(CBFS_PRELOAD))
1249 return;
1250
1251 printk(BIOS_DEBUG, "Preloading %s\n", file);
1252 cbfs_preload(file);
1253}
1254
Arthur Heymans01af0f82023-06-27 11:58:04 +02001255static void acpi_create_dsdt(acpi_header_t *header, void *dsdt_file_arg)
1256{
1257 dsdt = header;
1258 acpi_header_t *dsdt_file = *(acpi_header_t **)dsdt_file_arg;
1259 unsigned long current = (unsigned long)header;
1260
1261 dsdt = (acpi_header_t *)current;
1262 memcpy(dsdt, dsdt_file, sizeof(acpi_header_t));
1263 if (dsdt->length >= sizeof(acpi_header_t)) {
1264 current += sizeof(acpi_header_t);
1265
1266 acpigen_set_current((char *)current);
1267
1268 if (CONFIG(ACPI_SOC_NVS))
1269 acpi_fill_gnvs();
1270 if (CONFIG(CHROMEOS_NVS))
1271 acpi_fill_cnvs();
1272
1273 for (const struct device *dev = all_devices; dev; dev = dev->next)
1274 if (dev->ops && dev->ops->acpi_inject_dsdt)
1275 dev->ops->acpi_inject_dsdt(dev);
1276 current = (unsigned long)acpigen_get_current();
1277 memcpy((char *)current,
1278 (char *)dsdt_file + sizeof(acpi_header_t),
1279 dsdt->length - sizeof(acpi_header_t));
1280 current += dsdt->length - sizeof(acpi_header_t);
1281
1282 /* (Re)calculate length. */
1283 dsdt->length = current - (unsigned long)dsdt;
1284 }
1285}
1286
1287static void acpi_create_slic(acpi_header_t *header, void *slic_file_arg)
1288{
1289 acpi_header_t *slic_file = *(acpi_header_t **)slic_file_arg;
1290 acpi_header_t *slic = header;
1291 if (slic_file)
1292 memcpy(slic, slic_file, slic_file->length);
1293}
1294
Arthur Heymans2e7e2d92022-03-03 22:28:27 +01001295static uintptr_t coreboot_rsdp;
1296
1297uintptr_t get_coreboot_rsdp(void)
1298{
1299 return coreboot_rsdp;
1300}
1301
Arthur Heymans0ad766c2023-06-07 10:45:59 +02001302static void acpixtract_compatible_hexdump(const void *memory, size_t length)
1303{
1304 size_t i, j;
1305 uint8_t *line;
1306 size_t num_bytes;
1307
1308 for (i = 0; i < length; i += 16) {
1309 num_bytes = MIN(length - i, 16);
1310 line = ((uint8_t *)memory) + i;
1311
1312 printk(BIOS_SPEW, " %04zX:", i);
1313 for (j = 0; j < num_bytes; j++)
1314 printk(BIOS_SPEW, " %02x", line[j]);
1315 for (; j < 16; j++)
1316 printk(BIOS_SPEW, " ");
1317 printk(BIOS_SPEW, " ");
1318 for (j = 0; j < num_bytes; j++)
1319 printk(BIOS_SPEW, "%c",
1320 isprint(line[j]) ? line[j] : '.');
1321 printk(BIOS_SPEW, "\n");
1322 }
1323}
1324
1325static void acpidump_print(void *table_ptr)
1326{
1327 const acpi_header_t *header = (acpi_header_t *)table_ptr;
1328 const size_t table_size = header->length;
1329 printk(BIOS_SPEW, "%.4s @ 0x0000000000000000\n", header->signature);
1330 acpixtract_compatible_hexdump(table_ptr, table_size);
1331 printk(BIOS_SPEW, "\n");
1332}
1333
Arthur Heymans7ebebf72023-06-17 14:08:46 +02001334unsigned long write_acpi_tables(const unsigned long start)
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001335{
1336 unsigned long current;
1337 acpi_rsdp_t *rsdp;
Arthur Heymans0ad766c2023-06-07 10:45:59 +02001338 acpi_rsdt_t *rsdt = NULL;
1339 acpi_xsdt_t *xsdt = NULL;
Arthur Heymans0ad766c2023-06-07 10:45:59 +02001340 acpi_facs_t *facs = NULL;
Arthur Heymans01af0f82023-06-27 11:58:04 +02001341 acpi_header_t *slic_file;
Arthur Heymans0ad766c2023-06-07 10:45:59 +02001342 acpi_header_t *ssdt = NULL;
Arthur Heymans01af0f82023-06-27 11:58:04 +02001343 acpi_header_t *dsdt_file;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +11001344 struct device *dev;
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001345 unsigned long fw;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001346 size_t slic_size, dsdt_size;
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001347 char oem_id[6], oem_table_id[8];
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001348
Arthur Heymans01af0f82023-06-27 11:58:04 +02001349 const struct acpi_table_generator {
1350 void (*create_table)(acpi_header_t *table, void *arg);
1351 void *args;
1352 size_t min_size;
1353 } tables[] = {
1354 { acpi_create_dsdt, &dsdt_file, sizeof(acpi_header_t) },
1355 { acpi_create_fadt, &facs, sizeof(acpi_fadt_t) },
1356 { acpi_create_slic, &slic_file, sizeof(acpi_header_t) },
1357 { acpi_create_ssdt_generator, NULL, sizeof(acpi_header_t) },
1358 { acpi_create_mcfg, NULL, sizeof(acpi_mcfg_t) },
1359 { acpi_create_tcpa, NULL, sizeof(acpi_tcpa_t) },
1360 { acpi_create_tpm2, NULL, sizeof(acpi_tpm2_t) },
1361 { acpi_create_lpit, NULL, sizeof(acpi_lpit_t) },
1362 { acpi_create_madt, NULL, sizeof(acpi_header_t) },
1363 { acpi_create_bert, NULL, sizeof(acpi_bert_t) },
1364 { acpi_create_spcr, NULL, sizeof(acpi_spcr_t) },
Arthur Heymans2e3cb632023-06-30 15:01:08 +02001365 { acpi_create_gtdt, NULL, sizeof(acpi_gtdt_t) },
Arthur Heymans01af0f82023-06-27 11:58:04 +02001366 };
1367
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001368 current = start;
1369
1370 /* Align ACPI tables to 16byte */
Aaron Durbin07a1b282015-12-10 17:07:38 -06001371 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001372
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001373 /* Special case for qemu */
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001374 fw = fw_cfg_acpi_tables(current);
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001375 if (fw) {
1376 rsdp = NULL;
1377 /* Find RSDP. */
1378 for (void *p = (void *)current; p < (void *)fw; p += 16) {
1379 if (valid_rsdp((acpi_rsdp_t *)p)) {
1380 rsdp = p;
Bin Mengf3027b82023-05-09 15:21:49 +08001381 coreboot_rsdp = (uintptr_t)rsdp;
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001382 break;
1383 }
1384 }
1385 if (!rsdp)
1386 return fw;
1387
1388 /* Add BOOT0000 for Linux google firmware driver */
1389 printk(BIOS_DEBUG, "ACPI: * SSDT\n");
1390 ssdt = (acpi_header_t *)fw;
1391 current = (unsigned long)ssdt + sizeof(acpi_header_t);
1392
1393 memset((void *)ssdt, 0, sizeof(acpi_header_t));
1394
1395 memcpy(&ssdt->signature, "SSDT", 4);
1396 ssdt->revision = get_acpi_table_revision(SSDT);
1397 memcpy(&ssdt->oem_id, OEM_ID, 6);
1398 memcpy(&ssdt->oem_table_id, oem_table_id, 8);
1399 ssdt->oem_revision = 42;
1400 memcpy(&ssdt->asl_compiler_id, ASLC, 4);
1401 ssdt->asl_compiler_revision = asl_revision;
1402 ssdt->length = sizeof(acpi_header_t);
1403
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001404 acpigen_set_current((char *)current);
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001405
1406 /* Write object to declare coreboot tables */
1407 acpi_ssdt_write_cbtable();
1408
1409 /* (Re)calculate length and checksum. */
1410 ssdt->length = current - (unsigned long)ssdt;
1411 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
1412
Arthur Heymans01af0f82023-06-27 11:58:04 +02001413 acpi_create_ssdt_generator(ssdt, NULL);
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001414
1415 acpi_add_table(rsdp, ssdt);
1416
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001417 return fw;
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001418 }
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001419
Julius Werner834b3ec2020-03-04 16:52:08 -08001420 dsdt_file = cbfs_map(CONFIG_CBFS_PREFIX "/dsdt.aml", &dsdt_size);
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001421 if (!dsdt_file) {
1422 printk(BIOS_ERR, "No DSDT file, skipping ACPI tables\n");
Arthur Heymans0600aa62023-06-17 14:13:54 +02001423 return start;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001424 }
1425
1426 if (dsdt_file->length > dsdt_size
Elyes HAOUAS7d87e762016-10-03 22:11:07 +02001427 || dsdt_file->length < sizeof(acpi_header_t)
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001428 || memcmp(dsdt_file->signature, "DSDT", 4) != 0) {
1429 printk(BIOS_ERR, "Invalid DSDT file, skipping ACPI tables\n");
Grzegorz Bernacki042ac352023-04-21 13:19:58 +00001430 cbfs_unmap(dsdt_file);
Arthur Heymans0600aa62023-06-17 14:13:54 +02001431 return start;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001432 }
1433
Julius Werner834b3ec2020-03-04 16:52:08 -08001434 slic_file = cbfs_map(CONFIG_CBFS_PREFIX "/slic", &slic_size);
Vladimir Serbinenko2cb29782015-05-31 11:32:09 +02001435 if (slic_file
1436 && (slic_file->length > slic_size
Elyes HAOUAS7d87e762016-10-03 22:11:07 +02001437 || slic_file->length < sizeof(acpi_header_t)
Benjamin Doron2b2dc0c2020-09-12 02:55:57 +00001438 || (memcmp(slic_file->signature, "SLIC", 4) != 0
1439 && memcmp(slic_file->signature, "MSDM", 4) != 0))) {
Grzegorz Bernacki042ac352023-04-21 13:19:58 +00001440 cbfs_unmap(slic_file);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001441 slic_file = 0;
1442 }
1443
1444 if (slic_file) {
1445 memcpy(oem_id, slic_file->oem_id, 6);
1446 memcpy(oem_table_id, slic_file->oem_table_id, 8);
1447 } else {
1448 memcpy(oem_id, OEM_ID, 6);
1449 memcpy(oem_table_id, ACPI_TABLE_CREATOR, 8);
1450 }
1451
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001452 printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx.\n", start);
1453
1454 /* We need at least an RSDP and an RSDT Table */
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001455 rsdp = (acpi_rsdp_t *)current;
Arthur Heymans2e7e2d92022-03-03 22:28:27 +01001456 coreboot_rsdp = (uintptr_t)rsdp;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001457 current += sizeof(acpi_rsdp_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001458 current = acpi_align_current(current);
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001459 rsdt = (acpi_rsdt_t *)current;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001460 current += sizeof(acpi_rsdt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001461 current = acpi_align_current(current);
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001462 xsdt = (acpi_xsdt_t *)current;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001463 current += sizeof(acpi_xsdt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001464 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001465
1466 /* clear all table memory */
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001467 memset((void *)start, 0, current - start);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001468
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001469 acpi_write_rsdp(rsdp, rsdt, xsdt, oem_id);
1470 acpi_write_rsdt(rsdt, oem_id, oem_table_id);
1471 acpi_write_xsdt(xsdt, oem_id, oem_table_id);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001472
Arthur Heymans6af72612023-06-29 20:25:32 +02001473 if (ENV_X86) {
1474 printk(BIOS_DEBUG, "ACPI: * FACS\n");
1475 current = ALIGN_UP(current, 64);
1476 facs = (acpi_facs_t *)current;
1477 current += sizeof(acpi_facs_t);
1478 current = acpi_align_current(current);
1479 acpi_create_facs(facs);
1480 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001481
Arthur Heymans01af0f82023-06-27 11:58:04 +02001482 for (size_t i = 0; i < ARRAY_SIZE(tables); i++) {
1483 acpi_header_t *header = (acpi_header_t *)current;
1484 memset(header, 0, tables[i].min_size);
1485 tables[i].create_table(header, tables[i].args);
1486 if (header->length < tables[i].min_size)
1487 continue;
1488 header->checksum = 0;
1489 header->checksum = acpi_checksum((void *)header, header->length);
1490 current += header->length;
Aaron Durbin07a1b282015-12-10 17:07:38 -06001491 current = acpi_align_current(current);
Kyösti Mälkki0bcdd402023-07-06 14:47:07 +03001492
1493 if (tables[i].create_table == acpi_create_dsdt)
1494 continue;
1495
Arthur Heymans01af0f82023-06-27 11:58:04 +02001496 printk(BIOS_DEBUG, "ACPI: * %.4s\n", header->signature);
1497 acpi_add_table(rsdp, header);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001498 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001499
Grzegorz Bernacki042ac352023-04-21 13:19:58 +00001500 /*
1501 * cbfs_unmap() uses mem_pool_free() which works correctly only
1502 * if freeing is done in reverse order than memory allocation.
1503 * This is why unmapping of dsdt_file must be done after
1504 * unmapping slic file.
1505 */
Arthur Heymans01af0f82023-06-27 11:58:04 +02001506 cbfs_unmap(slic_file);
Grzegorz Bernacki042ac352023-04-21 13:19:58 +00001507 cbfs_unmap(dsdt_file);
1508
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001509 printk(BIOS_DEBUG, "current = %lx\n", current);
1510
1511 for (dev = all_devices; dev; dev = dev->next) {
1512 if (dev->ops && dev->ops->write_acpi_tables) {
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001513 current = dev->ops->write_acpi_tables(dev, current,
1514 rsdp);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001515 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001516 }
1517 }
1518
1519 printk(BIOS_INFO, "ACPI: done.\n");
Arthur Heymans0ad766c2023-06-07 10:45:59 +02001520
1521 if (CONFIG(DEBUG_ACPICA_COMPATIBLE)) {
1522 printk(BIOS_DEBUG, "Printing ACPI tables in ACPICA compatible format\n");
Arthur Heymans1cdeaea2023-07-06 16:24:50 +02001523 if (facs)
1524 acpidump_print(facs);
1525 acpidump_print(dsdt);
Arthur Heymans3e523b42023-06-15 17:04:16 +02001526 for (size_t i = 0; xsdt->entry[i] != 0; i++) {
1527 acpidump_print((void *)(uintptr_t)xsdt->entry[i]);
Arthur Heymans0ad766c2023-06-07 10:45:59 +02001528 }
1529 printk(BIOS_DEBUG, "Done printing ACPI tables in ACPICA compatible format\n");
1530 }
1531
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001532 return current;
1533}
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001534
Rudolf Marek33cafe52009-04-13 18:07:02 +00001535static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp)
1536{
1537 if (strncmp((char *)rsdp, RSDP_SIG, sizeof(RSDP_SIG) - 1) != 0)
1538 return NULL;
1539
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001540 printk(BIOS_DEBUG, "Looking on %p for valid checksum\n", rsdp);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001541
1542 if (acpi_checksum((void *)rsdp, 20) != 0)
1543 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001544 printk(BIOS_DEBUG, "Checksum 1 passed\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001545
1546 if ((rsdp->revision > 1) && (acpi_checksum((void *)rsdp,
1547 rsdp->length) != 0))
1548 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001549 printk(BIOS_DEBUG, "Checksum 2 passed all OK\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001550
1551 return rsdp;
1552}
1553
Rudolf Marek33cafe52009-04-13 18:07:02 +00001554void *acpi_find_wakeup_vector(void)
1555{
1556 char *p, *end;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001557 acpi_rsdt_t *rsdt;
1558 acpi_facs_t *facs;
Martin Rothc5f49262012-12-14 19:17:55 -07001559 acpi_fadt_t *fadt = NULL;
Kyösti Mälkki072d436b2016-06-17 08:44:40 +03001560 acpi_rsdp_t *rsdp = NULL;
Uwe Hermann622824c2010-11-19 15:14:42 +00001561 void *wake_vec;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001562 int i;
1563
Kyösti Mälkkib8c7ea02020-11-17 16:41:38 +02001564 if (!acpi_is_wakeup_s3())
Rudolf Marek33cafe52009-04-13 18:07:02 +00001565 return NULL;
1566
Uwe Hermann622824c2010-11-19 15:14:42 +00001567 printk(BIOS_DEBUG, "Trying to find the wakeup vector...\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001568
Uwe Hermann622824c2010-11-19 15:14:42 +00001569 /* Find RSDP. */
1570 for (p = (char *)0xe0000; p < (char *)0xfffff; p += 16) {
Lee Leahy0b5678f2017-03-16 16:01:40 -07001571 rsdp = valid_rsdp((acpi_rsdp_t *)p);
1572 if (rsdp)
Rudolf Marek33cafe52009-04-13 18:07:02 +00001573 break;
1574 }
1575
Angel Pons98a68ad2018-11-04 12:25:25 +01001576 if (rsdp == NULL) {
1577 printk(BIOS_ALERT,
1578 "No RSDP found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001579 return NULL;
Angel Pons98a68ad2018-11-04 12:25:25 +01001580 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001581
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001582 printk(BIOS_DEBUG, "RSDP found at %p\n", rsdp);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001583 rsdt = (acpi_rsdt_t *)(uintptr_t)rsdp->rsdt_address;
Stefan Reinauer14e22772010-04-27 06:56:47 +00001584
Uwe Hermann622824c2010-11-19 15:14:42 +00001585 end = (char *)rsdt + rsdt->header.length;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001586 printk(BIOS_DEBUG, "RSDT found at %p ends at %p\n", rsdt, end);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001587
Uwe Hermann622824c2010-11-19 15:14:42 +00001588 for (i = 0; ((char *)&rsdt->entry[i]) < end; i++) {
Stefan Reinauer71a30182015-07-30 16:28:13 -07001589 fadt = (acpi_fadt_t *)(uintptr_t)rsdt->entry[i];
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001590 if (strncmp((char *)fadt, "FACP", 4) == 0)
Rudolf Marek33cafe52009-04-13 18:07:02 +00001591 break;
1592 fadt = NULL;
1593 }
1594
Angel Pons98a68ad2018-11-04 12:25:25 +01001595 if (fadt == NULL) {
1596 printk(BIOS_ALERT,
1597 "No FADT found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001598 return NULL;
Angel Pons98a68ad2018-11-04 12:25:25 +01001599 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001600
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001601 printk(BIOS_DEBUG, "FADT found at %p\n", fadt);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001602 facs = (acpi_facs_t *)(uintptr_t)fadt->firmware_ctrl;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001603
Stefan Reinauer7e9771c2009-04-22 08:17:38 +00001604 if (facs == NULL) {
Angel Pons98a68ad2018-11-04 12:25:25 +01001605 printk(BIOS_ALERT,
1606 "No FACS found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001607 return NULL;
Stefan Reinauer7e9771c2009-04-22 08:17:38 +00001608 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001609
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001610 printk(BIOS_DEBUG, "FACS found at %p\n", facs);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001611 wake_vec = (void *)(uintptr_t)facs->firmware_waking_vector;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001612 printk(BIOS_DEBUG, "OS waking vector is %p\n", wake_vec);
Uwe Hermann622824c2010-11-19 15:14:42 +00001613
Rudolf Marek33cafe52009-04-13 18:07:02 +00001614 return wake_vec;
1615}
1616
Aaron Durbin64031672018-04-21 14:45:32 -06001617__weak int acpi_get_gpe(int gpe)
Duncan Laurie1f6e6812016-09-19 12:04:19 -07001618{
1619 return -1; /* implemented by SOC */
1620}
Marc Jones93a51762018-08-22 18:57:24 -06001621
Elyes Haouas8b950f42022-02-16 12:08:16 +01001622u8 get_acpi_fadt_minor_version(void)
1623{
1624 return ACPI_FADT_MINOR_VERSION_0;
1625}
1626
Marc Jones93a51762018-08-22 18:57:24 -06001627int get_acpi_table_revision(enum acpi_tables table)
1628{
1629 switch (table) {
1630 case FADT:
Elyes Haouas8b950f42022-02-16 12:08:16 +01001631 return ACPI_FADT_REV_ACPI_6;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001632 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 +01001633 return 3;
Marc Jones93a51762018-08-22 18:57:24 -06001634 case MCFG:
1635 return 1;
1636 case TCPA:
1637 return 2;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001638 case TPM2:
1639 return 4;
Martin Roth0949e732021-10-01 14:28:22 -06001640 case SSDT: /* ACPI 3.0 up to 6.3: 2 */
Marc Jones93a51762018-08-22 18:57:24 -06001641 return 2;
Jonathan Zhanga3311b92022-12-01 17:13:41 -08001642 case SRAT: /* ACPI 2.0: 1, ACPI 3.0: 2, ACPI 4.0 up to 6.4: 3 */
1643 return 3;
Jonathan Zhang2a4e1f42021-04-01 11:43:37 -07001644 case HMAT: /* ACPI 6.4: 2 */
1645 return 2;
Marc Jones93a51762018-08-22 18:57:24 -06001646 case DMAR:
1647 return 1;
Martin Roth0949e732021-10-01 14:28:22 -06001648 case SLIT: /* ACPI 2.0 up to 6.3: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06001649 return 1;
Patrick Rudolph9d98e5a2019-06-14 19:00:04 +02001650 case SPMI: /* IMPI 2.0 */
1651 return 5;
Marc Jones93a51762018-08-22 18:57:24 -06001652 case HPET: /* Currently 1. Table added in ACPI 2.0. */
1653 return 1;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001654 case VFCT: /* ACPI 2.0/3.0/4.0: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06001655 return 1;
1656 case IVRS:
Jason Glenesk1916f892020-07-24 02:51:30 -07001657 return IVRS_FORMAT_MIXED;
Marc Jones93a51762018-08-22 18:57:24 -06001658 case DBG2:
1659 return 0;
Martin Roth0949e732021-10-01 14:28:22 -06001660 case FACS: /* ACPI 2.0/3.0: 1, ACPI 4.0 up to 6.3: 2 */
Marc Jones93a51762018-08-22 18:57:24 -06001661 return 1;
Martin Roth0949e732021-10-01 14:28:22 -06001662 case RSDT: /* ACPI 1.0 up to 6.3: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06001663 return 1;
Martin Roth0949e732021-10-01 14:28:22 -06001664 case XSDT: /* ACPI 2.0 up to 6.3: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06001665 return 1;
Martin Roth0949e732021-10-01 14:28:22 -06001666 case RSDP: /* ACPI 2.0 up to 6.3: 2 */
Marc Jones93a51762018-08-22 18:57:24 -06001667 return 2;
Rocky Phaguraeff07132021-01-10 15:42:50 -08001668 case EINJ:
1669 return 1;
Marc Jones93a51762018-08-22 18:57:24 -06001670 case HEST:
1671 return 1;
1672 case NHLT:
1673 return 5;
Marshall Dawson44705c62018-09-04 13:47:15 -06001674 case BERT:
1675 return 1;
Jonathan Zhanga3311b92022-12-01 17:13:41 -08001676 case CEDT: /* CXL 3.0 section 9.17.1 */
1677 return 1;
Jason Glenesk61624b22020-11-02 20:06:23 -08001678 case CRAT:
1679 return 1;
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001680 case LPIT: /* ACPI 5.1 up to 6.3: 0 */
1681 return 0;
Arthur Heymans90464072023-06-07 12:53:50 +02001682 case SPCR:
1683 return 4;
Arthur Heymans2e3cb632023-06-30 15:01:08 +02001684 case GTDT:
1685 return 3;
Marc Jones93a51762018-08-22 18:57:24 -06001686 default:
1687 return -1;
1688 }
1689 return -1;
1690}