blob: 98c8ecdc131609575570252f0010cc7950b163e1 [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>
Naresh Solanki6920c6f2023-09-13 12:01:58 +020017#include <acpi/acpi_iort.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -070018#include <acpi/acpi_ivrs.h>
19#include <acpi/acpigen.h>
Jianjun Wang8565b94a2022-03-02 10:20:26 +080020#include <cbfs.h>
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000021#include <cbmem.h>
Elyes HAOUASe2d152c2019-06-21 07:06:50 +020022#include <commonlib/helpers.h>
Jianjun Wang8565b94a2022-03-02 10:20:26 +080023#include <console/console.h>
Duncan Laurie11290c42012-10-03 19:07:05 -070024#include <cpu/cpu.h>
Felix Held4a9ed702023-12-05 22:09:14 +010025#include <device/device.h>
Jianjun Wang8565b94a2022-03-02 10:20:26 +080026#include <device/mmio.h>
27#include <device/pci.h>
Arthur Heymans736d4d22023-06-30 15:37:38 +020028#include <drivers/uart/pl011.h>
Jianjun Wang8565b94a2022-03-02 10:20:26 +080029#include <string.h>
Elyes HAOUAScdd2f632021-02-11 19:37:11 +010030#include <types.h>
Elyes HAOUAS26071aa2019-02-15 08:21:33 +010031#include <version.h>
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000032
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +020033static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp);
34
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +000035u8 acpi_checksum(u8 *table, u32 length)
Stefan Reinauer688b3852004-01-28 16:56:14 +000036{
Uwe Hermann622824c2010-11-19 15:14:42 +000037 u8 ret = 0;
Stefan Reinauer688b3852004-01-28 16:56:14 +000038 while (length--) {
39 ret += *table;
40 table++;
41 }
Stefan Reinauera7648c22004-01-29 17:31:34 +000042 return -ret;
Stefan Reinauer688b3852004-01-28 16:56:14 +000043}
44
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000045/**
Uwe Hermann622824c2010-11-19 15:14:42 +000046 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
47 * and checksum.
Stefan Reinauer06feb882004-02-03 16:11:35 +000048 */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000049void acpi_add_table(acpi_rsdp_t *rsdp, void *table)
Stefan Reinauer688b3852004-01-28 16:56:14 +000050{
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000051 int i, entries_num;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000052 acpi_rsdt_t *rsdt;
Arthur Heymansc2830c92023-08-22 12:50:43 +020053 acpi_xsdt_t *xsdt;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +000054
Arthur Heymansd8f2dce2023-06-22 13:42:36 +020055 /* The 32bit RSDT may not be valid if tables live above 4GiB */
Stefan Reinauerdefee172015-06-18 01:11:19 -070056 rsdt = (acpi_rsdt_t *)(uintptr_t)rsdp->rsdt_address;
Arthur Heymansd8f2dce2023-06-22 13:42:36 +020057 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. */
Arthur Heymansd8f2dce2023-06-22 13:42:36 +020060 entries_num = ARRAY_SIZE(xsdt->entry);
Stefan Reinauer14e22772010-04-27 06:56:47 +000061
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000062 for (i = 0; i < entries_num; i++) {
Arthur Heymansd8f2dce2023-06-22 13:42:36 +020063 if (xsdt->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
Arthur Heymansd8f2dce2023-06-22 13:42:36 +020073 /* Add table to the XSDT. */
74 xsdt->entry[i] = (u64)(uintptr_t)table;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000075
Arthur Heymansd8f2dce2023-06-22 13:42:36 +020076 /* Fix XSDT length or the kernel will assume invalid entries. */
77 xsdt->header.length = sizeof(acpi_header_t) + (sizeof(u64) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000078
Uwe Hermann622824c2010-11-19 15:14:42 +000079 /* Re-calculate checksum. */
Arthur Heymansd8f2dce2023-06-22 13:42:36 +020080 xsdt->header.checksum = 0; /* Hope this won't get optimized away */
81 xsdt->header.checksum = acpi_checksum((u8 *)xsdt, xsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000082
Uwe Hermann622824c2010-11-19 15:14:42 +000083 /*
Arthur Heymansd8f2dce2023-06-22 13:42:36 +020084 * And now the same thing for the RSDT. 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 */
Arthur Heymansd8f2dce2023-06-22 13:42:36 +020087 if (rsdt && (uintptr_t)table < UINT32_MAX) {
88 /* Add table to the RSDT. */
89 rsdt->entry[i] = (u32)(uintptr_t)table;
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000090
Arthur Heymansd8f2dce2023-06-22 13:42:36 +020091 /* Fix RSDT length. */
92 rsdt->header.length = sizeof(acpi_header_t) + (sizeof(u32) * (i + 1));
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000093
Uwe Hermann622824c2010-11-19 15:14:42 +000094 /* Re-calculate checksum. */
Arthur Heymansd8f2dce2023-06-22 13:42:36 +020095 rsdt->header.checksum = 0;
96 rsdt->header.checksum = acpi_checksum((u8 *)rsdt, rsdt->header.length);
Stefan Reinauerc0ac7e92009-11-10 22:17:15 +000097 }
98
Uwe Hermann622824c2010-11-19 15:14:42 +000099 printk(BIOS_DEBUG, "ACPI: added table %d/%d, length now %d\n",
Arthur Heymansd8f2dce2023-06-22 13:42:36 +0200100 i + 1, entries_num, xsdt->header.length);
Stefan Reinauer688b3852004-01-28 16:56:14 +0000101}
102
Arthur Heymansadb80072023-06-28 09:56:00 +0200103static enum cb_err acpi_fill_header(acpi_header_t *header, const char name[4],
104 enum acpi_tables table, uint32_t size)
105{
106 if (!header)
107 return CB_ERR;
108
109 /* Fill out header fields. */
110 memcpy(header->signature, name, 4);
111 memcpy(header->oem_id, OEM_ID, 6);
112 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
113 memcpy(header->asl_compiler_id, ASLC, 4);
114
115 header->asl_compiler_revision = asl_revision;
116 header->revision = get_acpi_table_revision(table);
117 header->length = size;
118
119 return CB_SUCCESS;
120}
121
Naresh Solanki4d0b1842023-08-25 12:58:11 +0200122static int acpi_create_mcfg_mmconfig(acpi_mcfg_mmconfig_t *mmconfig, u64 base,
Cristian Măgherușan-Stanciuba482812011-07-02 00:57:07 +0300123 u16 seg_nr, u8 start, u8 end)
Stefan Reinauer26d431a2009-01-20 19:17:51 +0000124{
Vladimir Serbinenko60fccdc2014-10-05 11:05:16 +0200125 memset(mmconfig, 0, sizeof(*mmconfig));
Rudolf Mareke6409f22007-11-03 12:50:26 +0000126 mmconfig->base_address = base;
Rudolf Mareke6409f22007-11-03 12:50:26 +0000127 mmconfig->pci_segment_group_number = seg_nr;
128 mmconfig->start_bus_number = start;
129 mmconfig->end_bus_number = end;
Uwe Hermann622824c2010-11-19 15:14:42 +0000130
131 return sizeof(acpi_mcfg_mmconfig_t);
Rudolf Mareke6409f22007-11-03 12:50:26 +0000132}
133
Arthur Heymans01af0f82023-06-27 11:58:04 +0200134static void acpi_create_madt(acpi_header_t *header, void *unused)
Stefan Reinauer06feb882004-02-03 16:11:35 +0000135{
Arthur Heymans01af0f82023-06-27 11:58:04 +0200136 acpi_madt_t *madt = (acpi_madt_t *)header;
Uwe Hermann622824c2010-11-19 15:14:42 +0000137 unsigned long current = (unsigned long)madt + sizeof(acpi_madt_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000138
Arthur Heymansadb80072023-06-28 09:56:00 +0200139 if (acpi_fill_header(header, "APIC", MADT, sizeof(acpi_madt_t)) != CB_SUCCESS)
John Zhao2ba303e2019-05-28 16:48:14 -0700140 return;
141
Arthur Heymanscd46e5f2023-06-22 21:34:16 +0200142 current = acpi_arch_fill_madt(madt, current);
Kyösti Mälkki10bdee12023-04-11 01:00:17 +0300143
144 if (CONFIG(ACPI_CUSTOM_MADT))
Kyösti Mälkkia5fa5342022-11-18 13:23:52 +0200145 current = acpi_fill_madt(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000146
Arthur Heymans01af0f82023-06-27 11:58:04 +0200147 /* (Re)calculate length . */
Uwe Hermann622824c2010-11-19 15:14:42 +0000148 header->length = current - (unsigned long)madt;
Stefan Reinauer06feb882004-02-03 16:11:35 +0000149}
150
Kyösti Mälkkib54388d2021-02-14 15:09:45 +0200151static unsigned long acpi_fill_mcfg(unsigned long current)
152{
Felix Held3b5b66d2024-01-11 22:26:18 +0100153 for (int i = 0; i < PCI_SEGMENT_GROUP_COUNT; i++) {
154 current += acpi_create_mcfg_mmconfig((acpi_mcfg_mmconfig_t *)current,
155 CONFIG_ECAM_MMCONF_BASE_ADDRESS + i * PCI_PER_SEGMENT_GROUP_ECAM_SIZE,
156 i,
157 0,
158 PCI_BUSES_PER_SEGMENT_GROUP - 1);
159 }
160
Kyösti Mälkkib54388d2021-02-14 15:09:45 +0200161 return current;
162}
163
Uwe Hermann622824c2010-11-19 15:14:42 +0000164/* MCFG is defined in the PCI Firmware Specification 3.0. */
Arthur Heymans01af0f82023-06-27 11:58:04 +0200165static void acpi_create_mcfg(acpi_header_t *header, void *unused)
Rudolf Mareke6409f22007-11-03 12:50:26 +0000166{
Arthur Heymans01af0f82023-06-27 11:58:04 +0200167 acpi_mcfg_t *mcfg = (acpi_mcfg_t *)header;
Uwe Hermann622824c2010-11-19 15:14:42 +0000168 unsigned long current = (unsigned long)mcfg + sizeof(acpi_mcfg_t);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000169
Arthur Heymansadb80072023-06-28 09:56:00 +0200170
171 if (acpi_fill_header(header, "MCFG", MCFG, sizeof(acpi_mcfg_t)) != CB_SUCCESS)
John Zhao2ba303e2019-05-28 16:48:14 -0700172 return;
173
Shelley Chen4e9bb332021-10-20 15:43:45 -0700174 if (CONFIG(ECAM_MMCONF_SUPPORT))
Kyösti Mälkkib54388d2021-02-14 15:09:45 +0200175 current = acpi_fill_mcfg(current);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000176
Arthur Heymans01af0f82023-06-27 11:58:04 +0200177 /* (Re)calculate length */
Uwe Hermann622824c2010-11-19 15:14:42 +0000178 header->length = current - (unsigned long)mcfg;
Rudolf Mareke6409f22007-11-03 12:50:26 +0000179}
180
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200181static void *get_tcpa_log(u32 *size)
182{
183 const struct cbmem_entry *ce;
184 const u32 tcpa_default_log_len = 0x10000;
185 void *lasa;
Philipp Deppenwiese791ba972018-07-30 01:15:20 +0200186 ce = cbmem_entry_find(CBMEM_ID_TCPA_TCG_LOG);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200187 if (ce) {
188 lasa = cbmem_entry_start(ce);
189 *size = cbmem_entry_size(ce);
190 printk(BIOS_DEBUG, "TCPA log found at %p\n", lasa);
191 return lasa;
192 }
Philipp Deppenwiese791ba972018-07-30 01:15:20 +0200193 lasa = cbmem_add(CBMEM_ID_TCPA_TCG_LOG, tcpa_default_log_len);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200194 if (!lasa) {
195 printk(BIOS_ERR, "TCPA log creation failed\n");
196 return NULL;
197 }
198
199 printk(BIOS_DEBUG, "TCPA log created at %p\n", lasa);
Lee Leahy024b13d2017-03-16 13:41:11 -0700200 memset(lasa, 0, tcpa_default_log_len);
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200201
202 *size = tcpa_default_log_len;
203 return lasa;
204}
205
Arthur Heymans01af0f82023-06-27 11:58:04 +0200206static void acpi_create_tcpa(acpi_header_t *header, void *unused)
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200207{
Arthur Heymans01af0f82023-06-27 11:58:04 +0200208 if (!CONFIG(TPM1))
209 return;
210
211 acpi_tcpa_t *tcpa = (acpi_tcpa_t *)header;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200212 u32 tcpa_log_len;
213 void *lasa;
214
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200215 lasa = get_tcpa_log(&tcpa_log_len);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700216 if (!lasa)
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200217 return;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200218
Arthur Heymansadb80072023-06-28 09:56:00 +0200219 if (acpi_fill_header(header, "TCPA", TCPA, sizeof(acpi_tcpa_t)) != CB_SUCCESS)
John Zhao2ba303e2019-05-28 16:48:14 -0700220 return;
221
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200222 tcpa->platform_class = 0;
223 tcpa->laml = tcpa_log_len;
Elyes Haouas3141fbad2022-11-18 15:22:32 +0100224 tcpa->lasa = (uintptr_t)lasa;
Vladimir Serbinenkof44ac132015-05-20 19:52:01 +0200225}
226
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100227static void *get_tpm2_log(u32 *size)
228{
229 const struct cbmem_entry *ce;
230 const u32 tpm2_default_log_len = 0x10000;
231 void *lasa;
232 ce = cbmem_entry_find(CBMEM_ID_TPM2_TCG_LOG);
233 if (ce) {
234 lasa = cbmem_entry_start(ce);
235 *size = cbmem_entry_size(ce);
236 printk(BIOS_DEBUG, "TPM2 log found at %p\n", lasa);
237 return lasa;
238 }
239 lasa = cbmem_add(CBMEM_ID_TPM2_TCG_LOG, tpm2_default_log_len);
240 if (!lasa) {
241 printk(BIOS_ERR, "TPM2 log creation failed\n");
242 return NULL;
243 }
244
245 printk(BIOS_DEBUG, "TPM2 log created at %p\n", lasa);
246 memset(lasa, 0, tpm2_default_log_len);
247
248 *size = tpm2_default_log_len;
249 return lasa;
250}
251
Arthur Heymans01af0f82023-06-27 11:58:04 +0200252static void acpi_create_tpm2(acpi_header_t *header, void *unused)
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200253{
Arthur Heymans01af0f82023-06-27 11:58:04 +0200254 if (!CONFIG(TPM2))
255 return;
256
257 acpi_tpm2_t *tpm2 = (acpi_tpm2_t *)header;
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100258 u32 tpm2_log_len;
259 void *lasa;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200260
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100261 /*
262 * Some payloads like SeaBIOS depend on log area to use TPM2.
263 * Get the memory size and address of TPM2 log area or initialize it.
264 */
265 lasa = get_tpm2_log(&tpm2_log_len);
266 if (!lasa)
267 tpm2_log_len = 0;
268
Arthur Heymansadb80072023-06-28 09:56:00 +0200269 if (acpi_fill_header(header, "TPM2", TPM2, sizeof(acpi_tpm2_t)) != CB_SUCCESS)
John Zhao2ba303e2019-05-28 16:48:14 -0700270 return;
271
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200272 /* Hard to detect for coreboot. Just set it to 0 */
273 tpm2->platform_class = 0;
Christian Walter26e0d4c2019-07-14 15:47:23 +0200274 if (CONFIG(CRB_TPM)) {
275 /* Must be set to 7 for CRB Support */
276 tpm2->control_area = CONFIG_CRB_TPM_BASE_ADDRESS + 0x40;
277 tpm2->start_method = 7;
278 } else {
279 /* Must be set to 0 for FIFO interface support */
280 tpm2->control_area = 0;
281 tpm2->start_method = 6;
282 }
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200283 memset(tpm2->msp, 0, sizeof(tpm2->msp));
284
Michał Żygowski6e8692e2018-11-22 16:57:50 +0100285 /* Fill the log area size and start address fields. */
286 tpm2->laml = tpm2_log_len;
Elyes Haouas3141fbad2022-11-18 15:22:32 +0100287 tpm2->lasa = (uintptr_t)lasa;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +0200288}
289
Duncan Laurie37319032016-05-26 12:47:05 -0700290static void acpi_ssdt_write_cbtable(void)
291{
292 const struct cbmem_entry *cbtable;
293 uintptr_t base;
294 uint32_t size;
295
296 cbtable = cbmem_entry_find(CBMEM_ID_CBTABLE);
297 if (!cbtable)
298 return;
299 base = (uintptr_t)cbmem_entry_start(cbtable);
300 size = cbmem_entry_size(cbtable);
301
302 acpigen_write_device("CTBL");
303 acpigen_write_coreboot_hid(COREBOOT_ACPI_ID_CBTABLE);
304 acpigen_write_name_integer("_UID", 0);
Matt DeVillierce10b6f2022-10-15 11:52:54 -0500305 acpigen_write_STA(ACPI_STATUS_DEVICE_ALL_ON);
Duncan Laurie37319032016-05-26 12:47:05 -0700306 acpigen_write_name("_CRS");
307 acpigen_write_resourcetemplate_header();
Arthur Heymans87837df2023-06-29 21:09:52 +0200308 acpigen_resource_consumer_mmio(base, base + size - 1,
309 MEM_RSRC_FLAG_MEM_READ_ONLY
310 | MEM_RSRC_FLAG_MEM_ATTR_CACHE);
Duncan Laurie37319032016-05-26 12:47:05 -0700311 acpigen_write_resourcetemplate_footer();
312 acpigen_pop_len();
313}
314
Arthur Heymans01af0f82023-06-27 11:58:04 +0200315static void acpi_create_ssdt_generator(acpi_header_t *ssdt, void *unused)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000316{
Uwe Hermann622824c2010-11-19 15:14:42 +0000317 unsigned long current = (unsigned long)ssdt + sizeof(acpi_header_t);
318
Arthur Heymansadb80072023-06-28 09:56:00 +0200319 if (acpi_fill_header(ssdt, "SSDT", SSDT, sizeof(acpi_header_t)) != CB_SUCCESS)
320 return;
Rudolf Marek293b5f52009-02-01 18:35:15 +0000321
Elyes Haouas3141fbad2022-11-18 15:22:32 +0100322 acpigen_set_current((char *)current);
Duncan Laurie37319032016-05-26 12:47:05 -0700323
324 /* Write object to declare coreboot tables */
325 acpi_ssdt_write_cbtable();
326
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200327 {
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100328 struct device *dev;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200329 for (dev = all_devices; dev; dev = dev->next)
Karthikeyan Ramasubramaniand1c0f952020-11-02 16:26:52 -0700330 if (dev->enabled && dev->ops && dev->ops->acpi_fill_ssdt)
Nico Huber68680dd2020-03-31 17:34:52 +0200331 dev->ops->acpi_fill_ssdt(dev);
Elyes Haouas3141fbad2022-11-18 15:22:32 +0100332 current = (unsigned long)acpigen_get_current();
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +0200333 }
Rudolf Marek293b5f52009-02-01 18:35:15 +0000334
Uwe Hermann622824c2010-11-19 15:14:42 +0000335 /* (Re)calculate length and checksum. */
Rudolf Marek293b5f52009-02-01 18:35:15 +0000336 ssdt->length = current - (unsigned long)ssdt;
Rudolf Marek293b5f52009-02-01 18:35:15 +0000337}
338
Uwe Hermann622824c2010-11-19 15:14:42 +0000339int 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 +0300340 u32 flags)
Stefan Reinauerf622d592005-11-26 16:56:05 +0000341{
Uwe Hermann622824c2010-11-19 15:14:42 +0000342 mem->type = 1; /* Memory affinity structure */
343 mem->length = sizeof(acpi_srat_mem_t);
344 mem->base_address_low = (basek << 10);
345 mem->base_address_high = (basek >> (32 - 10));
346 mem->length_low = (sizek << 10);
347 mem->length_high = (sizek >> (32 - 10));
348 mem->proximity_domain = node;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000349 mem->flags = flags;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000350
Uwe Hermann622824c2010-11-19 15:14:42 +0000351 return mem->length;
Stefan Reinauerf622d592005-11-26 16:56:05 +0000352}
353
Jonathan Zhang3164b642021-04-21 17:51:31 -0700354int acpi_create_srat_gia_pci(acpi_srat_gia_t *gia, u32 proximity_domain,
Patrick Rudolph344ebf12024-01-31 11:38:38 +0100355 struct device *dev, u32 flags)
Jonathan Zhang3164b642021-04-21 17:51:31 -0700356{
Patrick Rudolph344ebf12024-01-31 11:38:38 +0100357 /* Only handle PCI devices. */
358 if (dev->path.type != DEVICE_PATH_PCI)
359 return 0;
360
Jonathan Zhang3164b642021-04-21 17:51:31 -0700361 gia->type = ACPI_SRAT_STRUCTURE_GIA;
362 gia->length = sizeof(acpi_srat_gia_t);
363 gia->proximity_domain = proximity_domain;
364 gia->dev_handle_type = ACPI_SRAT_GIA_DEV_HANDLE_PCI;
365 /* First two bytes has segment number */
Patrick Rudolph344ebf12024-01-31 11:38:38 +0100366 gia->dev_handle[0] = dev->upstream->segment_group;
367 gia->dev_handle[1] = 0;
368 gia->dev_handle[2] = dev->upstream->secondary; /* Byte 2 has bus number */
Jonathan Zhang3164b642021-04-21 17:51:31 -0700369 /* Byte 3 has bits 7:3 for dev, bits 2:0 for func */
Patrick Rudolph344ebf12024-01-31 11:38:38 +0100370 gia->dev_handle[3] = dev->path.pci.devfn;
Jonathan Zhang3164b642021-04-21 17:51:31 -0700371 gia->flags = flags;
372
373 return gia->length;
374}
375
Uwe Hermann622824c2010-11-19 15:14:42 +0000376/* http://www.microsoft.com/whdc/system/sysinternals/sratdwn.mspx */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +0200377void acpi_create_srat(acpi_srat_t *srat,
378 unsigned long (*acpi_fill_srat)(unsigned long current))
Stefan Reinauerf622d592005-11-26 16:56:05 +0000379{
Uwe Hermann622824c2010-11-19 15:14:42 +0000380 acpi_header_t *header = &(srat->header);
381 unsigned long current = (unsigned long)srat + sizeof(acpi_srat_t);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000382
Uwe Hermann622824c2010-11-19 15:14:42 +0000383 memset((void *)srat, 0, sizeof(acpi_srat_t));
Stefan Reinauerf622d592005-11-26 16:56:05 +0000384
Arthur Heymansadb80072023-06-28 09:56:00 +0200385 if (acpi_fill_header(header, "SRAT", SRAT, sizeof(acpi_srat_t)) != CB_SUCCESS)
John Zhao2ba303e2019-05-28 16:48:14 -0700386 return;
387
Uwe Hermann622824c2010-11-19 15:14:42 +0000388 srat->resv = 1; /* Spec: Reserved to 1 for backwards compatibility. */
Stefan Reinauerf622d592005-11-26 16:56:05 +0000389
Uwe Hermann622824c2010-11-19 15:14:42 +0000390 current = acpi_fill_srat(current);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000391
Uwe Hermann622824c2010-11-19 15:14:42 +0000392 /* (Re)calculate length and checksum. */
393 header->length = current - (unsigned long)srat;
394 header->checksum = acpi_checksum((void *)srat, header->length);
Stefan Reinauerf622d592005-11-26 16:56:05 +0000395}
396
Jonathan Zhang3dcafa82022-05-11 13:11:20 -0700397int acpi_create_cedt_chbs(acpi_cedt_chbs_t *chbs, u32 uid, u32 cxl_ver, u64 base)
398{
399 memset((void *)chbs, 0, sizeof(acpi_cedt_chbs_t));
400
401 chbs->type = ACPI_CEDT_STRUCTURE_CHBS;
402 chbs->length = sizeof(acpi_cedt_chbs_t);
403 chbs->uid = uid;
404 chbs->cxl_ver = cxl_ver;
405 chbs->base = base;
406
407 /*
408 * CXL spec 2.0 section 9.14.1.2 "CXL CHBS"
409 * CXL 1.1 spec compliant host bridge: 8KB
410 * CXL 2.0 spec compliant host bridge: 64KB
411 */
412 if (cxl_ver == ACPI_CEDT_CHBS_CXL_VER_1_1)
413 chbs->len = 8 * KiB;
414 else if (cxl_ver == ACPI_CEDT_CHBS_CXL_VER_2_0)
415 chbs->len = 64 * KiB;
416 else
417 printk(BIOS_ERR, "ACPI(%s:%s): Incorrect CXL version:%d\n", __FILE__, __func__,
418 cxl_ver);
419
420 return chbs->length;
421}
422
423int acpi_create_cedt_cfmws(acpi_cedt_cfmws_t *cfmws, u64 base_hpa, u64 window_size, u8 eniw,
424 u32 hbig, u16 restriction, u16 qtg_id, const u32 *interleave_target)
425{
426 memset((void *)cfmws, 0, sizeof(acpi_cedt_cfmws_t));
427
428 cfmws->type = ACPI_CEDT_STRUCTURE_CFMWS;
429
430 u8 niw = 0;
431 if (eniw >= 8)
432 printk(BIOS_ERR, "ACPI(%s:%s): Incorrect eniw::%d\n", __FILE__, __func__, eniw);
433 else
434 /* NIW = 2 ** ENIW */
435 niw = 0x1 << eniw;
436 /* 36 + 4 * NIW */
437 cfmws->length = sizeof(acpi_cedt_cfmws_t) + 4 * niw;
438
439 cfmws->base_hpa = base_hpa;
440 cfmws->window_size = window_size;
441 cfmws->eniw = eniw;
442
443 // 0: Standard Modulo Arithmetic. Other values reserved.
444 cfmws->interleave_arithmetic = 0;
445
446 cfmws->hbig = hbig;
447 cfmws->restriction = restriction;
448 cfmws->qtg_id = qtg_id;
449 memcpy(&cfmws->interleave_target, interleave_target, 4 * niw);
450
451 return cfmws->length;
452}
453
454void acpi_create_cedt(acpi_cedt_t *cedt, unsigned long (*acpi_fill_cedt)(unsigned long current))
455{
456 acpi_header_t *header = &(cedt->header);
457 unsigned long current = (unsigned long)cedt + sizeof(acpi_cedt_t);
458
459 memset((void *)cedt, 0, sizeof(acpi_cedt_t));
460
Arthur Heymansadb80072023-06-28 09:56:00 +0200461 if (acpi_fill_header(header, "CEDT", CEDT, sizeof(acpi_cedt_t)) != CB_SUCCESS)
Jonathan Zhang3dcafa82022-05-11 13:11:20 -0700462 return;
463
Jonathan Zhang3dcafa82022-05-11 13:11:20 -0700464 current = acpi_fill_cedt(current);
465
466 /* (Re)calculate length and checksum. */
467 header->length = current - (unsigned long)cedt;
468 header->checksum = acpi_checksum((void *)cedt, header->length);
469}
470
Jonathan Zhang2a4e1f42021-04-01 11:43:37 -0700471int acpi_create_hmat_mpda(acpi_hmat_mpda_t *mpda, u32 initiator, u32 memory)
472{
473 memset((void *)mpda, 0, sizeof(acpi_hmat_mpda_t));
474
475 mpda->type = 0; /* Memory Proximity Domain Attributes structure */
476 mpda->length = sizeof(acpi_hmat_mpda_t);
477 /*
478 * Proximity Domain for Attached Initiator field is valid.
479 * Bit 1 and bit 2 are reserved since HMAT revision 2.
480 */
481 mpda->flags = (1 << 0);
482 mpda->proximity_domain_initiator = initiator;
483 mpda->proximity_domain_memory = memory;
484
485 return mpda->length;
486}
487
488void acpi_create_hmat(acpi_hmat_t *hmat,
489 unsigned long (*acpi_fill_hmat)(unsigned long current))
490{
491 acpi_header_t *header = &(hmat->header);
492 unsigned long current = (unsigned long)hmat + sizeof(acpi_hmat_t);
493
494 memset((void *)hmat, 0, sizeof(acpi_hmat_t));
495
Arthur Heymansadb80072023-06-28 09:56:00 +0200496 if (acpi_fill_header(header, "HMAT", HMAT, sizeof(acpi_hmat_t)) != CB_SUCCESS)
Jonathan Zhang2a4e1f42021-04-01 11:43:37 -0700497 return;
498
Jonathan Zhang2a4e1f42021-04-01 11:43:37 -0700499 current = acpi_fill_hmat(current);
500
501 /* (Re)calculate length and checksum. */
502 header->length = current - (unsigned long)hmat;
503 header->checksum = acpi_checksum((void *)hmat, header->length);
504}
505
Uwe Hermann622824c2010-11-19 15:14:42 +0000506/* http://h21007.www2.hp.com/portal/download/files/unprot/Itanium/slit.pdf */
Vladimir Serbinenko5e597572014-10-11 23:45:40 +0200507void acpi_create_slit(acpi_slit_t *slit,
508 unsigned long (*acpi_fill_slit)(unsigned long current))
Yinghai Lud4b278c2006-10-04 20:46:15 +0000509{
Uwe Hermann622824c2010-11-19 15:14:42 +0000510 acpi_header_t *header = &(slit->header);
511 unsigned long current = (unsigned long)slit + sizeof(acpi_slit_t);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000512
Uwe Hermann622824c2010-11-19 15:14:42 +0000513 memset((void *)slit, 0, sizeof(acpi_slit_t));
Yinghai Lud4b278c2006-10-04 20:46:15 +0000514
Arthur Heymansadb80072023-06-28 09:56:00 +0200515 if (acpi_fill_header(header, "SLIT", SLIT, sizeof(acpi_slit_t)) != CB_SUCCESS)
John Zhao2ba303e2019-05-28 16:48:14 -0700516 return;
517
Uwe Hermann622824c2010-11-19 15:14:42 +0000518 current = acpi_fill_slit(current);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000519
Uwe Hermann622824c2010-11-19 15:14:42 +0000520 /* (Re)calculate length and checksum. */
521 header->length = current - (unsigned long)slit;
522 header->checksum = acpi_checksum((void *)slit, header->length);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000523}
524
Rocky Phaguraeff07132021-01-10 15:42:50 -0800525/*
526 * This method adds the ACPI error injection capability. It fills the default information.
527 * HW dependent code (caller) can modify the defaults upon return. If no changes are necessary
528 * and the defaults are acceptable then caller can simply add the table (acpi_add_table).
529 * INPUTS:
530 * einj - ptr to the starting location of EINJ table
531 * actions - number of actions to trigger an error (HW dependent)
532 * addr - address of trigger action table. This should be ACPI reserved memory and it will be
533 * shared between OS and FW.
534 */
535void acpi_create_einj(acpi_einj_t *einj, uintptr_t addr, u8 actions)
536{
537 int i;
538 acpi_header_t *header = &(einj->header);
539 acpi_injection_header_t *inj_header = &(einj->inj_header);
540 acpi_einj_smi_t *einj_smi = (acpi_einj_smi_t *)addr;
541 acpi_einj_trigger_table_t *tat;
542 if (!header)
543 return;
544
545 printk(BIOS_DEBUG, "%s einj_smi = %p\n", __func__, einj_smi);
546 memset(einj_smi, 0, sizeof(acpi_einj_smi_t));
Jonathan Zhangd5d9b282022-11-07 17:30:14 -0800547 tat = (acpi_einj_trigger_table_t *)((uint8_t *)einj_smi + sizeof(acpi_einj_smi_t));
Rocky Phaguraeff07132021-01-10 15:42:50 -0800548 tat->header_size = 16;
549 tat->revision = 0;
550 tat->table_size = sizeof(acpi_einj_trigger_table_t) +
551 sizeof(acpi_einj_action_table_t) * actions - 1;
552 tat->entry_count = actions;
553 printk(BIOS_DEBUG, "%s trigger_action_table = %p\n", __func__, tat);
554
555 for (i = 0; i < actions; i++) {
556 tat->trigger_action[i].action = TRIGGER_ERROR;
557 tat->trigger_action[i].instruction = NO_OP;
558 tat->trigger_action[i].flags = FLAG_IGNORE;
559 tat->trigger_action[i].reg.space_id = ACPI_ADDRESS_SPACE_MEMORY;
560 tat->trigger_action[i].reg.bit_width = 64;
561 tat->trigger_action[i].reg.bit_offset = 0;
562 tat->trigger_action[i].reg.access_size = ACPI_ACCESS_SIZE_QWORD_ACCESS;
563 tat->trigger_action[i].reg.addr = 0;
564 tat->trigger_action[i].value = 0;
565 tat->trigger_action[i].mask = 0xFFFFFFFF;
566 }
567
568 acpi_einj_action_table_t default_actions[ACTION_COUNT] = {
569 [0] = {
570 .action = BEGIN_INJECT_OP,
571 .instruction = WRITE_REGISTER_VALUE,
572 .flags = FLAG_PRESERVE,
573 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->op_state),
574 .value = 0,
575 .mask = 0xFFFFFFFF
576 },
577 [1] = {
578 .action = GET_TRIGGER_ACTION_TABLE,
579 .instruction = READ_REGISTER,
580 .flags = FLAG_IGNORE,
581 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->trigger_action_table),
582 .value = 0,
583 .mask = 0xFFFFFFFFFFFFFFFF
584 },
585 [2] = {
586 .action = SET_ERROR_TYPE,
587 .instruction = WRITE_REGISTER,
588 .flags = FLAG_PRESERVE,
589 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->err_inject[0]),
590 .value = 0,
591 .mask = 0xFFFFFFFF
592 },
593 [3] = {
594 .action = GET_ERROR_TYPE,
595 .instruction = READ_REGISTER,
596 .flags = FLAG_IGNORE,
597 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->err_inj_cap),
598 .value = 0,
599 .mask = 0xFFFFFFFF
600 },
601 [4] = {
602 .action = END_INJECT_OP,
603 .instruction = WRITE_REGISTER_VALUE,
604 .flags = FLAG_PRESERVE,
605 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->op_state),
606 .value = 0,
607 .mask = 0xFFFFFFFF
608
609 },
610 [5] = {
611 .action = EXECUTE_INJECT_OP,
612 .instruction = WRITE_REGISTER_VALUE,
613 .flags = FLAG_PRESERVE,
614 .reg = EINJ_REG_IO(),
615 .value = 0x9a,
616 .mask = 0xFFFF,
617 },
618 [6] = {
619 .action = CHECK_BUSY_STATUS,
620 .instruction = READ_REGISTER_VALUE,
621 .flags = FLAG_IGNORE,
622 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->op_status),
623 .value = 1,
624 .mask = 1,
625 },
626 [7] = {
627 .action = GET_CMD_STATUS,
628 .instruction = READ_REGISTER,
629 .flags = FLAG_PRESERVE,
630 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->cmd_sts),
631 .value = 0,
632 .mask = 0x1fe,
633 },
634 [8] = {
635 .action = SET_ERROR_TYPE_WITH_ADDRESS,
636 .instruction = WRITE_REGISTER,
637 .flags = FLAG_PRESERVE,
638 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->setaddrtable),
639 .value = 1,
640 .mask = 0xffffffff
641 }
642 };
643
644 einj_smi->err_inj_cap = ACPI_EINJ_DEFAULT_CAP;
Elyes Haouas3141fbad2022-11-18 15:22:32 +0100645 einj_smi->trigger_action_table = (u64)(uintptr_t)tat;
Rocky Phaguraeff07132021-01-10 15:42:50 -0800646
647 for (i = 0; i < ACTION_COUNT; i++)
648 printk(BIOS_DEBUG, "default_actions[%d].reg.addr is %llx\n", i,
649 default_actions[i].reg.addr);
650
Rocky Phagurab46a9e52021-05-21 13:34:03 -0700651 memset((void *)einj, 0, sizeof(*einj));
Rocky Phaguraeff07132021-01-10 15:42:50 -0800652
Arthur Heymansadb80072023-06-28 09:56:00 +0200653 if (acpi_fill_header(header, "EINJ", EINJ, sizeof(acpi_einj_t)) != CB_SUCCESS)
654 return;
Rocky Phaguraeff07132021-01-10 15:42:50 -0800655
Rocky Phaguraeff07132021-01-10 15:42:50 -0800656 inj_header->einj_header_size = sizeof(acpi_injection_header_t);
657 inj_header->entry_count = ACTION_COUNT;
658
659 printk(BIOS_DEBUG, "%s einj->action_table = %p\n",
660 __func__, einj->action_table);
661 memcpy((void *)einj->action_table, (void *)default_actions, sizeof(einj->action_table));
Rocky Phagurab46a9e52021-05-21 13:34:03 -0700662 header->checksum = acpi_checksum((void *)einj, sizeof(*einj));
Rocky Phaguraeff07132021-01-10 15:42:50 -0800663}
664
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700665void acpi_create_vfct(const struct device *device,
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530666 acpi_vfct_t *vfct,
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700667 unsigned long (*acpi_fill_vfct)(const struct device *device,
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530668 acpi_vfct_t *vfct_struct, unsigned long current))
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200669{
670 acpi_header_t *header = &(vfct->header);
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530671 unsigned long current = (unsigned long)vfct + sizeof(acpi_vfct_t);
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200672
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530673 memset((void *)vfct, 0, sizeof(acpi_vfct_t));
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200674
Arthur Heymansadb80072023-06-28 09:56:00 +0200675 if (acpi_fill_header(header, "VFCT", VFCT, sizeof(acpi_vfct_t)) != CB_SUCCESS)
John Zhao2ba303e2019-05-28 16:48:14 -0700676 return;
677
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200678 current = acpi_fill_vfct(device, vfct, current);
679
Kyösti Mälkkifb493792019-06-30 06:29:52 +0300680 /* If no BIOS image, return with header->length == 0. */
681 if (!vfct->VBIOSImageOffset)
682 return;
683
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200684 /* (Re)calculate length and checksum. */
685 header->length = current - (unsigned long)vfct;
686 header->checksum = acpi_checksum((void *)vfct, header->length);
687}
688
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700689void acpi_create_ipmi(const struct device *device,
Patrick Rudolph9d98e5a2019-06-14 19:00:04 +0200690 struct acpi_spmi *spmi,
691 const u16 ipmi_revision,
692 const acpi_addr_t *addr,
693 const enum acpi_ipmi_interface_type type,
694 const s8 gpe_interrupt,
695 const u32 apic_interrupt,
696 const u32 uid)
697{
698 acpi_header_t *header = &(spmi->header);
699 memset((void *)spmi, 0, sizeof(struct acpi_spmi));
700
Arthur Heymansadb80072023-06-28 09:56:00 +0200701 if (acpi_fill_header(header, "SPMI", SPMI, sizeof(struct acpi_spmi)) != CB_SUCCESS)
702 return;
Patrick Rudolph9d98e5a2019-06-14 19:00:04 +0200703
704 spmi->reserved = 1;
705
706 if (device->path.type == DEVICE_PATH_PCI) {
707 spmi->pci_device_flag = ACPI_IPMI_PCI_DEVICE_FLAG;
Arthur Heymans7fcd4d52023-08-24 15:12:19 +0200708 spmi->pci_segment_group = device->upstream->segment_group;
709 spmi->pci_bus = device->upstream->secondary;
Patrick Rudolph9d98e5a2019-06-14 19:00:04 +0200710 spmi->pci_device = device->path.pci.devfn >> 3;
711 spmi->pci_function = device->path.pci.devfn & 0x7;
712 } else if (type != IPMI_INTERFACE_SSIF) {
713 memcpy(spmi->uid, &uid, sizeof(spmi->uid));
714 }
715
716 spmi->base_address = *addr;
717 spmi->specification_revision = ipmi_revision;
718
719 spmi->interface_type = type;
720
721 if (gpe_interrupt >= 0 && gpe_interrupt < 32) {
722 spmi->gpe = gpe_interrupt;
723 spmi->interrupt_type |= ACPI_IPMI_INT_TYPE_SCI;
724 }
725 if (apic_interrupt > 0) {
726 spmi->global_system_interrupt = apic_interrupt;
727 spmi->interrupt_type |= ACPI_IPMI_INT_TYPE_APIC;
728 }
729
730 /* Calculate checksum. */
731 header->checksum = acpi_checksum((void *)spmi, header->length);
732}
733
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500734void acpi_create_ivrs(acpi_ivrs_t *ivrs,
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700735 unsigned long (*acpi_fill_ivrs)(acpi_ivrs_t *ivrs_struct,
736 unsigned long current))
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500737{
738 acpi_header_t *header = &(ivrs->header);
739 unsigned long current = (unsigned long)ivrs + sizeof(acpi_ivrs_t);
740
741 memset((void *)ivrs, 0, sizeof(acpi_ivrs_t));
742
Arthur Heymansadb80072023-06-28 09:56:00 +0200743 if (acpi_fill_header(header, "IVRS", IVRS, sizeof(acpi_ivrs_t)) != CB_SUCCESS)
John Zhao2ba303e2019-05-28 16:48:14 -0700744 return;
745
Timothy Pearsonff8ccf02015-08-11 17:48:32 -0500746 current = acpi_fill_ivrs(ivrs, current);
747
748 /* (Re)calculate length and checksum. */
749 header->length = current - (unsigned long)ivrs;
750 header->checksum = acpi_checksum((void *)ivrs, header->length);
751}
752
Jason Glenesk61624b22020-11-02 20:06:23 -0800753void acpi_create_crat(struct acpi_crat_header *crat,
754 unsigned long (*acpi_fill_crat)(struct acpi_crat_header *crat_struct,
755 unsigned long current))
756{
757 acpi_header_t *header = &(crat->header);
758 unsigned long current = (unsigned long)crat + sizeof(struct acpi_crat_header);
759
760 memset((void *)crat, 0, sizeof(struct acpi_crat_header));
761
Arthur Heymansadb80072023-06-28 09:56:00 +0200762 if (acpi_fill_header(header, "CRAT", CRAT, sizeof(struct acpi_crat_header)) != CB_SUCCESS)
Jason Glenesk61624b22020-11-02 20:06:23 -0800763 return;
764
Jason Glenesk61624b22020-11-02 20:06:23 -0800765 current = acpi_fill_crat(crat, current);
766
767 /* (Re)calculate length and checksum. */
768 header->length = current - (unsigned long)crat;
769 header->checksum = acpi_checksum((void *)crat, header->length);
770}
771
Arthur Heymans9368cf92023-04-25 16:07:49 +0200772static void acpi_create_dbg2(acpi_dbg2_header_t *dbg2,
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800773 int port_type, int port_subtype,
774 acpi_addr_t *address, uint32_t address_size,
775 const char *device_path)
776{
777 uintptr_t current;
778 acpi_dbg2_device_t *device;
779 uint32_t *dbg2_addr_size;
780 acpi_header_t *header;
781 size_t path_len;
782 const char *path;
783 char *namespace;
784
785 /* Fill out header fields. */
786 current = (uintptr_t)dbg2;
787 memset(dbg2, 0, sizeof(acpi_dbg2_header_t));
788 header = &(dbg2->header);
John Zhao2ba303e2019-05-28 16:48:14 -0700789
Arthur Heymansadb80072023-06-28 09:56:00 +0200790 if (acpi_fill_header(header, "DBG2", DBG2, sizeof(acpi_dbg2_header_t)) != CB_SUCCESS)
John Zhao2ba303e2019-05-28 16:48:14 -0700791 return;
792
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800793 /* One debug device defined */
794 dbg2->devices_offset = sizeof(acpi_dbg2_header_t);
795 dbg2->devices_count = 1;
796 current += sizeof(acpi_dbg2_header_t);
797
798 /* Device comes after the header */
799 device = (acpi_dbg2_device_t *)current;
800 memset(device, 0, sizeof(acpi_dbg2_device_t));
801 current += sizeof(acpi_dbg2_device_t);
802
803 device->revision = 0;
804 device->address_count = 1;
805 device->port_type = port_type;
806 device->port_subtype = port_subtype;
807
808 /* Base Address comes after device structure */
809 memcpy((void *)current, address, sizeof(acpi_addr_t));
810 device->base_address_offset = current - (uintptr_t)device;
811 current += sizeof(acpi_addr_t);
812
813 /* Address Size comes after address structure */
814 dbg2_addr_size = (uint32_t *)current;
815 device->address_size_offset = current - (uintptr_t)device;
816 *dbg2_addr_size = address_size;
817 current += sizeof(uint32_t);
818
819 /* Namespace string comes last, use '.' if not provided */
820 path = device_path ? : ".";
821 /* Namespace string length includes NULL terminator */
822 path_len = strlen(path) + 1;
823 namespace = (char *)current;
824 device->namespace_string_length = path_len;
825 device->namespace_string_offset = current - (uintptr_t)device;
826 strncpy(namespace, path, path_len);
827 current += path_len;
828
829 /* Update structure lengths and checksum */
830 device->length = current - (uintptr_t)device;
831 header->length = current - (uintptr_t)dbg2;
832 header->checksum = acpi_checksum((uint8_t *)dbg2, header->length);
833}
834
Arthur Heymans736d4d22023-06-30 15:37:38 +0200835static unsigned long acpi_write_dbg2_uart(acpi_rsdp_t *rsdp, unsigned long current,
836 int space_id, uint64_t base, uint32_t size,
837 int access_size, const char *name)
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800838{
839 acpi_dbg2_header_t *dbg2 = (acpi_dbg2_header_t *)current;
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800840 acpi_addr_t address;
841
Arthur Heymans736d4d22023-06-30 15:37:38 +0200842 memset(&address, 0, sizeof(address));
843
844 address.space_id = space_id;
845 address.addrl = (uint32_t)base;
846 address.addrh = (uint32_t)((base >> 32) & 0xffffffff);
847 address.access_size = access_size;
848
849 int subtype;
850 /* 16550-compatible with parameters defined in Generic Address Structure */
851 if (CONFIG(DRIVERS_UART_8250IO) || CONFIG(DRIVERS_UART_8250MEM))
852 subtype = ACPI_DBG2_PORT_SERIAL_16550;
853 else if (CONFIG(DRIVERS_UART_PL011))
854 subtype = ACPI_DBG2_PORT_SERIAL_ARM_PL011;
855 else
856 return current;
857
858 acpi_create_dbg2(dbg2,
859 ACPI_DBG2_PORT_SERIAL,
860 subtype,
861 &address, size,
862 name);
863
864 if (dbg2->header.length) {
865 current += dbg2->header.length;
866 current = acpi_align_current(current);
867 acpi_add_table(rsdp, dbg2);
868 }
869
870 return current;
871}
872
873unsigned long acpi_write_dbg2_pci_uart(acpi_rsdp_t *rsdp, unsigned long current,
874 const struct device *dev, uint8_t access_size)
875{
876 struct resource *res;
877
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800878 if (!dev) {
Wim Vervoorn6cd52432020-02-03 14:41:46 +0100879 printk(BIOS_DEBUG, "%s: Device not found\n", __func__);
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800880 return current;
881 }
Mario Scheithauerf1eb0ea2017-11-21 13:52:53 +0100882 if (!dev->enabled) {
883 printk(BIOS_INFO, "%s: Device not enabled\n", __func__);
884 return current;
885 }
Angel Pons32d09be2021-11-03 13:27:45 +0100886 res = probe_resource(dev, PCI_BASE_ADDRESS_0);
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800887 if (!res) {
888 printk(BIOS_ERR, "%s: Unable to find resource for %s\n",
889 __func__, dev_path(dev));
890 return current;
891 }
892
Arthur Heymans736d4d22023-06-30 15:37:38 +0200893 int space_id;
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800894 if (res->flags & IORESOURCE_IO)
Arthur Heymans736d4d22023-06-30 15:37:38 +0200895 space_id = ACPI_ADDRESS_SPACE_IO;
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800896 else if (res->flags & IORESOURCE_MEM)
Arthur Heymans736d4d22023-06-30 15:37:38 +0200897 space_id = ACPI_ADDRESS_SPACE_MEMORY;
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800898 else {
899 printk(BIOS_ERR, "%s: Unknown address space type\n", __func__);
900 return current;
901 }
902
Arthur Heymans736d4d22023-06-30 15:37:38 +0200903 return acpi_write_dbg2_uart(rsdp, current, space_id, res->base, res->size, access_size, acpi_device_path(dev));
904}
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800905
Arthur Heymans736d4d22023-06-30 15:37:38 +0200906unsigned long acpi_pl011_write_dbg2_uart(acpi_rsdp_t *rsdp, unsigned long current,
907 uint64_t base, const char *name)
908{
909 return acpi_write_dbg2_uart(rsdp, current, ACPI_ADDRESS_SPACE_MEMORY, base,
910 sizeof(struct pl011_uart), ACPI_ACCESS_SIZE_DWORD_ACCESS,
911 name);
Duncan Lauriee4a36c72017-11-11 19:33:25 -0800912}
913
Zheng Bao3ea3fbe2023-11-20 14:17:25 +0800914unsigned long acpi_16550_mmio32_write_dbg2_uart(acpi_rsdp_t *rsdp, unsigned long current,
915 uint64_t base, const char *name)
916{
917 return acpi_write_dbg2_uart(rsdp, current, ACPI_ADDRESS_SPACE_MEMORY, base,
918 0x100, ACPI_ACCESS_SIZE_DWORD_ACCESS,
919 name);
920}
921
Arthur Heymans01af0f82023-06-27 11:58:04 +0200922static void acpi_create_facs(void *header)
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000923{
Arthur Heymans01af0f82023-06-27 11:58:04 +0200924 acpi_facs_t *facs = header;
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000925
Stefan Reinauercdfe3762009-07-21 22:15:43 +0000926 memcpy(facs->signature, "FACS", 4);
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000927 facs->length = sizeof(acpi_facs_t);
928 facs->hardware_signature = 0;
929 facs->firmware_waking_vector = 0;
930 facs->global_lock = 0;
931 facs->flags = 0;
932 facs->x_firmware_waking_vector_l = 0;
933 facs->x_firmware_waking_vector_h = 0;
Marc Jones93a51762018-08-22 18:57:24 -0600934 facs->version = get_acpi_table_revision(FACS);
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000935}
Stefan Reinauer777224c2005-01-19 14:06:41 +0000936
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100937static void acpi_write_rsdt(acpi_rsdt_t *rsdt, char *oem_id, char *oem_table_id)
Stefan Reinauer14e22772010-04-27 06:56:47 +0000938{
Uwe Hermann622824c2010-11-19 15:14:42 +0000939 acpi_header_t *header = &(rsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000940
Arthur Heymansadb80072023-06-28 09:56:00 +0200941 if (acpi_fill_header(header, "RSDT", RSDT, sizeof(acpi_rsdt_t)) != CB_SUCCESS)
John Zhao2ba303e2019-05-28 16:48:14 -0700942 return;
943
Uwe Hermann622824c2010-11-19 15:14:42 +0000944 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauer688b3852004-01-28 16:56:14 +0000945
Uwe Hermann622824c2010-11-19 15:14:42 +0000946 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000947 header->checksum = acpi_checksum((void *)rsdt, sizeof(acpi_rsdt_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000948}
949
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100950static void acpi_write_xsdt(acpi_xsdt_t *xsdt, char *oem_id, char *oem_table_id)
Stefan Reinauer14e22772010-04-27 06:56:47 +0000951{
Uwe Hermann622824c2010-11-19 15:14:42 +0000952 acpi_header_t *header = &(xsdt->header);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000953
Arthur Heymansadb80072023-06-28 09:56:00 +0200954 if (acpi_fill_header(header, "XSDT", XSDT, sizeof(acpi_xsdt_t)) != CB_SUCCESS)
John Zhao2ba303e2019-05-28 16:48:14 -0700955 return;
956
Uwe Hermann622824c2010-11-19 15:14:42 +0000957 /* Entries are filled in later, we come with an empty set. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000958
Uwe Hermann622824c2010-11-19 15:14:42 +0000959 /* Fix checksum. */
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000960 header->checksum = acpi_checksum((void *)xsdt, sizeof(acpi_xsdt_t));
961}
962
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100963static void acpi_write_rsdp(acpi_rsdp_t *rsdp, acpi_rsdt_t *rsdt,
964 acpi_xsdt_t *xsdt, char *oem_id)
Stefan Reinauer688b3852004-01-28 16:56:14 +0000965{
Stefan Reinauerd18faac2009-11-05 18:06:43 +0000966 memset(rsdp, 0, sizeof(acpi_rsdp_t));
Uwe Hermann622824c2010-11-19 15:14:42 +0000967
Stefan Reinauer688b3852004-01-28 16:56:14 +0000968 memcpy(rsdp->signature, RSDP_SIG, 8);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100969 memcpy(rsdp->oem_id, oem_id, 6);
Uwe Hermann622824c2010-11-19 15:14:42 +0000970
971 rsdp->length = sizeof(acpi_rsdp_t);
Stefan Reinauerdefee172015-06-18 01:11:19 -0700972 rsdp->rsdt_address = (uintptr_t)rsdt;
Uwe Hermann622824c2010-11-19 15:14:42 +0000973
974 /*
975 * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2.
976 *
977 * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
978 * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
979 * revision 0).
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000980 */
981 if (xsdt == NULL) {
Uwe Hermann622824c2010-11-19 15:14:42 +0000982 rsdp->revision = 0;
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000983 } else {
Stefan Reinauerdefee172015-06-18 01:11:19 -0700984 rsdp->xsdt_address = (u64)(uintptr_t)xsdt;
Marc Jones93a51762018-08-22 18:57:24 -0600985 rsdp->revision = get_acpi_table_revision(RSDP);
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000986 }
Uwe Hermann622824c2010-11-19 15:14:42 +0000987
988 /* Calculate checksums. */
989 rsdp->checksum = acpi_checksum((void *)rsdp, 20);
990 rsdp->ext_checksum = acpi_checksum((void *)rsdp, sizeof(acpi_rsdp_t));
Stefan Reinauer688b3852004-01-28 16:56:14 +0000991}
992
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700993unsigned long acpi_create_hest_error_source(acpi_hest_t *hest,
994 acpi_hest_esd_t *esd, u16 type, void *data, u16 data_len)
zbaocaf494c82012-04-13 13:57:14 +0800995{
996 acpi_header_t *header = &(hest->header);
997 acpi_hest_hen_t *hen;
998 void *pos;
999 u16 len;
1000
1001 pos = esd;
1002 memset(pos, 0, sizeof(acpi_hest_esd_t));
1003 len = 0;
1004 esd->type = type; /* MCE */
1005 esd->source_id = hest->error_source_count;
1006 esd->flags = 0; /* FIRMWARE_FIRST */
1007 esd->enabled = 1;
1008 esd->prealloc_erecords = 1;
1009 esd->max_section_per_record = 0x1;
1010
1011 len += sizeof(acpi_hest_esd_t);
1012 pos = esd + 1;
1013
1014 switch (type) {
1015 case 0: /* MCE */
1016 break;
1017 case 1: /* CMC */
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001018 hen = (acpi_hest_hen_t *)(pos);
zbaocaf494c82012-04-13 13:57:14 +08001019 memset(pos, 0, sizeof(acpi_hest_hen_t));
1020 hen->type = 3; /* SCI? */
1021 hen->length = sizeof(acpi_hest_hen_t);
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001022 hen->conf_we = 0; /* Configuration Write Enable. */
zbaocaf494c82012-04-13 13:57:14 +08001023 hen->poll_interval = 0;
1024 hen->vector = 0;
1025 hen->sw2poll_threshold_val = 0;
1026 hen->sw2poll_threshold_win = 0;
1027 hen->error_threshold_val = 0;
1028 hen->error_threshold_win = 0;
1029 len += sizeof(acpi_hest_hen_t);
1030 pos = hen + 1;
1031 break;
1032 case 2: /* NMI */
1033 case 6: /* AER Root Port */
1034 case 7: /* AER Endpoint */
1035 case 8: /* AER Bridge */
1036 case 9: /* Generic Hardware Error Source. */
1037 /* TODO: */
1038 break;
1039 default:
1040 printk(BIOS_DEBUG, "Invalid type of Error Source.");
1041 break;
1042 }
Lee Leahy024b13d2017-03-16 13:41:11 -07001043 hest->error_source_count++;
zbaocaf494c82012-04-13 13:57:14 +08001044
1045 memcpy(pos, data, data_len);
1046 len += data_len;
John Zhao2ba303e2019-05-28 16:48:14 -07001047 if (header)
1048 header->length += len;
zbaocaf494c82012-04-13 13:57:14 +08001049
1050 return len;
1051}
1052
1053/* ACPI 4.0 */
Vladimir Serbinenko807127f2014-11-09 13:36:18 +01001054void acpi_write_hest(acpi_hest_t *hest,
1055 unsigned long (*acpi_fill_hest)(acpi_hest_t *hest))
zbaocaf494c82012-04-13 13:57:14 +08001056{
1057 acpi_header_t *header = &(hest->header);
1058
1059 memset(hest, 0, sizeof(acpi_hest_t));
1060
Arthur Heymansadb80072023-06-28 09:56:00 +02001061 if (acpi_fill_header(header, "HEST", HEST, sizeof(acpi_hest_t)) != CB_SUCCESS)
John Zhao2ba303e2019-05-28 16:48:14 -07001062 return;
1063
zbaocaf494c82012-04-13 13:57:14 +08001064 acpi_fill_hest(hest);
1065
1066 /* Calculate checksums. */
1067 header->checksum = acpi_checksum((void *)hest, header->length);
1068}
1069
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001070/* ACPI 3.0b */
Arthur Heymans01af0f82023-06-27 11:58:04 +02001071static void acpi_create_bert(acpi_header_t *header, void *unused)
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001072{
Arthur Heymans01af0f82023-06-27 11:58:04 +02001073 if (!CONFIG(ACPI_BERT))
1074 return;
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001075
Arthur Heymans01af0f82023-06-27 11:58:04 +02001076 acpi_bert_t *bert = (acpi_bert_t *)header;
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001077
Arthur Heymans01af0f82023-06-27 11:58:04 +02001078 void *region;
1079 size_t size;
1080 if (acpi_soc_get_bert_region(&region, &size) != CB_SUCCESS)
1081 return;
1082
Arthur Heymansadb80072023-06-28 09:56:00 +02001083 if (acpi_fill_header(header, "BERT", BERT, sizeof(acpi_bert_t)) != CB_SUCCESS)
1084 return;
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001085
Arthur Heymans01af0f82023-06-27 11:58:04 +02001086 bert->error_region = (uintptr_t)region;
1087 bert->region_length = (size_t)size;
Marshall Dawson1d8d3692018-09-04 13:45:26 -06001088}
1089
Angel Pons79572e42020-07-13 00:17:43 +02001090__weak void arch_fill_fadt(acpi_fadt_t *fadt) { }
Kyösti Mälkkif9aac922020-05-30 16:16:28 +03001091__weak void soc_fill_fadt(acpi_fadt_t *fadt) { }
Kyösti Mälkki02fd15d2020-06-02 03:34:43 +03001092__weak void mainboard_fill_fadt(acpi_fadt_t *fadt) { }
Kyösti Mälkkif9aac922020-05-30 16:16:28 +03001093
Arthur Heymans01af0f82023-06-27 11:58:04 +02001094static acpi_header_t *dsdt;
1095static void acpi_create_fadt(acpi_header_t *header, void *arg1)
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001096{
Arthur Heymans01af0f82023-06-27 11:58:04 +02001097 acpi_fadt_t *fadt = (acpi_fadt_t *)header;
1098 acpi_facs_t *facs = (acpi_facs_t *)(*(acpi_facs_t **)arg1);
John Zhao2ba303e2019-05-28 16:48:14 -07001099
Arthur Heymansadb80072023-06-28 09:56:00 +02001100 if (acpi_fill_header(header, "FACP", FADT, sizeof(acpi_fadt_t)) != CB_SUCCESS)
John Zhao2ba303e2019-05-28 16:48:14 -07001101 return;
1102
Elyes Haouas532e0432022-02-21 18:13:58 +01001103 fadt->FADT_MinorVersion = get_acpi_fadt_minor_version();
Arthur Heymans8473e8f2023-06-29 20:26:39 +02001104 if ((uintptr_t)facs <= UINT32_MAX)
1105 fadt->firmware_ctrl = (uintptr_t)facs;
1106 else
1107 fadt->x_firmware_ctl_h = (uint32_t)((uint64_t)(uintptr_t)facs >> 32);
1108 fadt->x_firmware_ctl_l = (uint32_t)(uintptr_t)facs;
Kyösti Mälkkid4acee82020-06-03 07:20:55 +03001109
Arthur Heymans8473e8f2023-06-29 20:26:39 +02001110 if ((uintptr_t)dsdt <= UINT32_MAX)
1111 fadt->dsdt = (uintptr_t)dsdt;
1112 else
1113 fadt->x_dsdt_h = (uint32_t)((uint64_t)(uintptr_t)dsdt >> 32);
1114 fadt->x_dsdt_l = (uint32_t)(uintptr_t)dsdt;
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001115
Kyösti Mälkkid4acee82020-06-03 07:20:55 +03001116 /* should be 0 ACPI 3.0 */
1117 fadt->reserved = 0;
1118
Kyösti Mälkki67c48a32023-04-14 10:20:03 +03001119 /* P_LVLx latencies are not used as CPU _CST will override them. */
1120 fadt->p_lvl2_lat = ACPI_FADT_C2_NOT_SUPPORTED;
1121 fadt->p_lvl3_lat = ACPI_FADT_C3_NOT_SUPPORTED;
1122
Kyösti Mälkki9ff97972023-04-14 15:37:27 +03001123 /* Use CPU _PTC instead to provide P_CNT details. */
1124 fadt->duty_offset = 0;
1125 fadt->duty_width = 0;
1126
Kyösti Mälkkie0d38682020-06-07 12:01:58 +03001127 fadt->preferred_pm_profile = acpi_get_preferred_pm_profile();
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001128
Angel Pons79572e42020-07-13 00:17:43 +02001129 arch_fill_fadt(fadt);
1130
Vladimir Serbinenkoab83ef02014-10-25 15:18:25 +02001131 acpi_fill_fadt(fadt);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001132
Kyösti Mälkkif9aac922020-05-30 16:16:28 +03001133 soc_fill_fadt(fadt);
Kyösti Mälkki02fd15d2020-06-02 03:34:43 +03001134 mainboard_fill_fadt(fadt);
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001135}
Vladimir Serbinenkoc21e0732014-10-16 12:48:19 +02001136
Arthur Heymans01af0f82023-06-27 11:58:04 +02001137static void acpi_create_lpit(acpi_header_t *header, void *unused)
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001138{
Arthur Heymans01af0f82023-06-27 11:58:04 +02001139 if (!CONFIG(ACPI_LPIT))
1140 return;
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001141
Arthur Heymans01af0f82023-06-27 11:58:04 +02001142 acpi_lpit_t *lpit = (acpi_lpit_t *)header;
1143 unsigned long current = (unsigned long)lpit + sizeof(acpi_lpit_t);
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001144
Arthur Heymansadb80072023-06-28 09:56:00 +02001145 if (acpi_fill_header(header, "LPIT", LPIT, sizeof(acpi_lpit_t)) != CB_SUCCESS)
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001146 return;
1147
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001148 current = acpi_fill_lpit(current);
1149
Arthur Heymans01af0f82023-06-27 11:58:04 +02001150 /* (Re)calculate length. */
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001151 header->length = current - (unsigned long)lpit;
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001152}
1153
Arthur Heymans2e3cb632023-06-30 15:01:08 +02001154static void acpi_create_gtdt(acpi_header_t *header, void *unused)
1155{
1156 if (!CONFIG(ACPI_GTDT))
1157 return;
1158
1159 acpi_gtdt_t *gtdt = (acpi_gtdt_t *)header;
1160 unsigned long current = (unsigned long)gtdt + sizeof(acpi_gtdt_t);
1161
1162 if (acpi_fill_header(header, "GTDT", GTDT, sizeof(acpi_gtdt_t)) != CB_SUCCESS)
1163 return;
1164
1165 /* Fill out header fields. */
1166 gtdt->platform_timer_offset = sizeof(acpi_gtdt_t);
1167
1168 acpi_soc_fill_gtdt(gtdt);
1169 current = acpi_soc_gtdt_add_timers(&gtdt->platform_timer_count, current);
1170
1171 /* (Re)calculate length. */
1172 header->length = current - (unsigned long)gtdt;
1173}
1174
1175unsigned long acpi_gtdt_add_timer_block(unsigned long current, const uint64_t address,
1176 struct acpi_gtdt_timer_entry *timers, size_t number)
1177{
1178 struct acpi_gtdt_timer_block *block = (struct acpi_gtdt_timer_block *)current;
1179 memset(block, 0, sizeof(struct acpi_gtdt_timer_block));
1180
1181 assert(number < 8 && number != 0);
1182 const size_t entries_size = number * sizeof(struct acpi_gtdt_timer_entry);
1183
1184 block->header.type = ACPI_GTDT_TYPE_TIMER_BLOCK;
1185 block->header.length = sizeof(struct acpi_gtdt_timer_block)
1186 + entries_size;
1187 block->block_address = address;
1188 block->timer_count = number;
1189 block->timer_offset = sizeof(struct acpi_gtdt_timer_block);
1190 current += sizeof(struct acpi_gtdt_timer_block);
1191 memcpy((void *)current, timers, entries_size);
1192 current += entries_size;
1193 return current;
1194}
1195
1196unsigned long acpi_gtdt_add_watchdog(unsigned long current, uint64_t refresh_frame,
1197 uint64_t control_frame, uint32_t gsiv, uint32_t flags)
1198{
1199 struct acpi_gtdt_watchdog *wd = (struct acpi_gtdt_watchdog *)current;
1200 memset(wd, 0, sizeof(struct acpi_gtdt_watchdog));
1201
1202 wd->header.type = ACPI_GTDT_TYPE_WATCHDOG;
1203 wd->header.length = sizeof(struct acpi_gtdt_watchdog);
1204 wd->refresh_frame_address = refresh_frame;
1205 wd->control_frame_address = control_frame;
1206 wd->timer_interrupt = gsiv;
1207 wd->timer_flags = flags;
1208
1209 return current + sizeof(struct acpi_gtdt_watchdog);
1210}
1211
Naresh Solanki6920c6f2023-09-13 12:01:58 +02001212static void acpi_create_iort(acpi_header_t *header, void *unused)
1213{
1214 if (!CONFIG(ACPI_IORT))
1215 return;
1216
1217 acpi_iort_t *iort = (acpi_iort_t *)header;
1218 unsigned long current = (unsigned long)iort + sizeof(acpi_iort_t);
1219
1220 if (acpi_fill_header(header, "IORT", IORT, sizeof(acpi_iort_t)) != CB_SUCCESS)
1221 return;
1222
1223 iort->node_count = 0;
1224 iort->node_offset = current - (unsigned long)iort;
1225
1226 current = acpi_soc_fill_iort(iort, current);
1227
1228 /* (Re)calculate length */
1229 header->length = current - (unsigned long)iort;
1230}
1231
Marek Maslanka017003c2023-12-07 13:21:35 +00001232static void acpi_create_wdat(acpi_header_t *header, void *unused)
1233{
1234 if (!CONFIG(ACPI_WDAT_WDT))
1235 return;
1236
1237 acpi_wdat_t *wdat = (acpi_wdat_t *)header;
1238 unsigned long current = (unsigned long)wdat + sizeof(acpi_wdat_t);
1239
1240 memset((void *)wdat, 0, sizeof(acpi_wdat_t));
1241
1242 if (acpi_fill_header(header, "WDAT", WDAT, sizeof(acpi_wdat_t)) != CB_SUCCESS)
1243 return;
1244
1245 current = acpi_soc_fill_wdat(wdat, current);
1246
1247 /* (Re)calculate length. */
1248 header->length = current - (unsigned long)wdat;
1249}
1250
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001251unsigned long acpi_create_lpi_desc_ncst(acpi_lpi_desc_ncst_t *lpi_desc, uint16_t uid)
1252{
1253 memset(lpi_desc, 0, sizeof(acpi_lpi_desc_ncst_t));
1254 lpi_desc->header.length = sizeof(acpi_lpi_desc_ncst_t);
1255 lpi_desc->header.type = ACPI_LPI_DESC_TYPE_NATIVE_CSTATE;
1256 lpi_desc->header.uid = uid;
1257
1258 return lpi_desc->header.length;
1259}
1260
David Milosevicd9822742023-09-22 14:34:28 +02001261static void acpi_create_pptt(acpi_header_t *header, void *unused)
1262{
1263 if (!CONFIG(ACPI_PPTT))
1264 return;
1265
1266 if (acpi_fill_header(header, "PPTT", PPTT, sizeof(acpi_pptt_t)) != CB_SUCCESS)
1267 return;
1268
1269 acpi_pptt_t *pptt = (acpi_pptt_t *)header;
1270 acpi_create_pptt_body(pptt);
1271}
1272
Arthur Heymans90464072023-06-07 12:53:50 +02001273static uint8_t acpi_spcr_type(void)
1274{
1275 /* 16550-compatible with parameters defined in Generic Address Structure */
1276 if (CONFIG(DRIVERS_UART_8250IO) || CONFIG(DRIVERS_UART_8250MEM))
1277 return 0x12;
1278 if (CONFIG(DRIVERS_UART_PL011))
1279 return 0x3;
1280
1281 printk(BIOS_ERR, "%s: unknown serial type\n", __func__);
1282 return 0xff;
1283}
1284
Arthur Heymans01af0f82023-06-27 11:58:04 +02001285static void acpi_create_spcr(acpi_header_t *header, void *unused)
Arthur Heymans90464072023-06-07 12:53:50 +02001286{
Arthur Heymans01af0f82023-06-27 11:58:04 +02001287 acpi_spcr_t *spcr = (acpi_spcr_t *)header;
Arthur Heymans90464072023-06-07 12:53:50 +02001288 struct lb_serial serial;
1289
Arthur Heymans90464072023-06-07 12:53:50 +02001290 if (!CONFIG(CONSOLE_SERIAL))
1291 return;
1292
1293 if (fill_lb_serial(&serial) != CB_SUCCESS)
1294 return;
1295
Arthur Heymansadb80072023-06-28 09:56:00 +02001296 if (acpi_fill_header(header, "SPCR", SPCR, sizeof(acpi_spcr_t)) != CB_SUCCESS)
1297 return;
Arthur Heymans90464072023-06-07 12:53:50 +02001298
1299 spcr->interface_type = acpi_spcr_type();
1300 assert(serial.type == LB_SERIAL_TYPE_IO_MAPPED
1301 || serial.type == LB_SERIAL_TYPE_MEMORY_MAPPED);
1302 spcr->base_address.space_id = serial.type == LB_SERIAL_TYPE_IO_MAPPED ?
1303 ACPI_ADDRESS_SPACE_IO : ACPI_ADDRESS_SPACE_MEMORY;
1304 spcr->base_address.bit_width = serial.regwidth * 8;
1305 spcr->base_address.bit_offset = 0;
1306 switch (serial.regwidth) {
1307 case 1:
1308 spcr->base_address.access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS;
1309 break;
1310 case 2:
1311 spcr->base_address.access_size = ACPI_ACCESS_SIZE_WORD_ACCESS;
1312 break;
1313 case 4:
1314 spcr->base_address.access_size = ACPI_ACCESS_SIZE_DWORD_ACCESS;
1315 break;
1316 default:
1317 printk(BIOS_ERR, "%s, Invalid serial regwidth\n", __func__);
1318 }
1319
1320 spcr->base_address.addrl = serial.baseaddr;
1321 spcr->base_address.addrh = 0;
1322 spcr->interrupt_type = 0;
1323 spcr->irq = 0;
1324 spcr->configured_baudrate = 0; /* Have the OS use whatever is currently set */
1325 spcr->parity = 0;
1326 spcr->stop_bits = 1;
1327 spcr->flow_control = 0;
1328 spcr->terminal_type = 2; /* 2 = VT-UTF8 */
1329 spcr->language = 0;
1330 spcr->pci_did = 0xffff;
1331 spcr->pci_vid = 0xffff;
Nico Huberfeb27dc2023-06-28 20:09:30 +02001332
1333 header->checksum = acpi_checksum((void *)spcr, header->length);
Arthur Heymans90464072023-06-07 12:53:50 +02001334}
1335
Aaron Durbin64031672018-04-21 14:45:32 -06001336unsigned long __weak fw_cfg_acpi_tables(unsigned long start)
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001337{
1338 return 0;
1339}
1340
Raul E Rangel6b446b92021-11-19 11:38:35 -07001341void preload_acpi_dsdt(void)
1342{
1343 const char *file = CONFIG_CBFS_PREFIX "/dsdt.aml";
1344
1345 if (!CONFIG(CBFS_PRELOAD))
1346 return;
1347
1348 printk(BIOS_DEBUG, "Preloading %s\n", file);
1349 cbfs_preload(file);
1350}
1351
Arthur Heymans01af0f82023-06-27 11:58:04 +02001352static void acpi_create_dsdt(acpi_header_t *header, void *dsdt_file_arg)
1353{
1354 dsdt = header;
1355 acpi_header_t *dsdt_file = *(acpi_header_t **)dsdt_file_arg;
1356 unsigned long current = (unsigned long)header;
1357
1358 dsdt = (acpi_header_t *)current;
1359 memcpy(dsdt, dsdt_file, sizeof(acpi_header_t));
1360 if (dsdt->length >= sizeof(acpi_header_t)) {
1361 current += sizeof(acpi_header_t);
1362
1363 acpigen_set_current((char *)current);
1364
1365 if (CONFIG(ACPI_SOC_NVS))
1366 acpi_fill_gnvs();
1367 if (CONFIG(CHROMEOS_NVS))
1368 acpi_fill_cnvs();
1369
Arthur Heymans01af0f82023-06-27 11:58:04 +02001370 current = (unsigned long)acpigen_get_current();
1371 memcpy((char *)current,
1372 (char *)dsdt_file + sizeof(acpi_header_t),
1373 dsdt->length - sizeof(acpi_header_t));
1374 current += dsdt->length - sizeof(acpi_header_t);
1375
1376 /* (Re)calculate length. */
1377 dsdt->length = current - (unsigned long)dsdt;
1378 }
1379}
1380
1381static void acpi_create_slic(acpi_header_t *header, void *slic_file_arg)
1382{
1383 acpi_header_t *slic_file = *(acpi_header_t **)slic_file_arg;
1384 acpi_header_t *slic = header;
1385 if (slic_file)
1386 memcpy(slic, slic_file, slic_file->length);
1387}
1388
Arthur Heymans2e7e2d92022-03-03 22:28:27 +01001389static uintptr_t coreboot_rsdp;
1390
1391uintptr_t get_coreboot_rsdp(void)
1392{
1393 return coreboot_rsdp;
1394}
1395
Arthur Heymans0ad766c2023-06-07 10:45:59 +02001396static void acpixtract_compatible_hexdump(const void *memory, size_t length)
1397{
1398 size_t i, j;
1399 uint8_t *line;
1400 size_t num_bytes;
1401
1402 for (i = 0; i < length; i += 16) {
1403 num_bytes = MIN(length - i, 16);
1404 line = ((uint8_t *)memory) + i;
1405
1406 printk(BIOS_SPEW, " %04zX:", i);
1407 for (j = 0; j < num_bytes; j++)
1408 printk(BIOS_SPEW, " %02x", line[j]);
1409 for (; j < 16; j++)
1410 printk(BIOS_SPEW, " ");
1411 printk(BIOS_SPEW, " ");
1412 for (j = 0; j < num_bytes; j++)
1413 printk(BIOS_SPEW, "%c",
1414 isprint(line[j]) ? line[j] : '.');
1415 printk(BIOS_SPEW, "\n");
1416 }
1417}
1418
1419static void acpidump_print(void *table_ptr)
1420{
Felix Held67b3c8f2023-08-03 01:08:14 +02001421 if (table_ptr == NULL)
1422 return;
Arthur Heymans0ad766c2023-06-07 10:45:59 +02001423 const acpi_header_t *header = (acpi_header_t *)table_ptr;
1424 const size_t table_size = header->length;
1425 printk(BIOS_SPEW, "%.4s @ 0x0000000000000000\n", header->signature);
1426 acpixtract_compatible_hexdump(table_ptr, table_size);
1427 printk(BIOS_SPEW, "\n");
1428}
1429
Arthur Heymans7ebebf72023-06-17 14:08:46 +02001430unsigned long write_acpi_tables(const unsigned long start)
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001431{
1432 unsigned long current;
1433 acpi_rsdp_t *rsdp;
Arthur Heymans0ad766c2023-06-07 10:45:59 +02001434 acpi_rsdt_t *rsdt = NULL;
1435 acpi_xsdt_t *xsdt = NULL;
Arthur Heymans0ad766c2023-06-07 10:45:59 +02001436 acpi_facs_t *facs = NULL;
Arthur Heymans01af0f82023-06-27 11:58:04 +02001437 acpi_header_t *slic_file;
Arthur Heymans0ad766c2023-06-07 10:45:59 +02001438 acpi_header_t *ssdt = NULL;
Arthur Heymans01af0f82023-06-27 11:58:04 +02001439 acpi_header_t *dsdt_file;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +11001440 struct device *dev;
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001441 unsigned long fw;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001442 size_t slic_size, dsdt_size;
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001443 char oem_id[6], oem_table_id[8];
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001444
Arthur Heymans01af0f82023-06-27 11:58:04 +02001445 const struct acpi_table_generator {
1446 void (*create_table)(acpi_header_t *table, void *arg);
1447 void *args;
1448 size_t min_size;
1449 } tables[] = {
1450 { acpi_create_dsdt, &dsdt_file, sizeof(acpi_header_t) },
1451 { acpi_create_fadt, &facs, sizeof(acpi_fadt_t) },
1452 { acpi_create_slic, &slic_file, sizeof(acpi_header_t) },
1453 { acpi_create_ssdt_generator, NULL, sizeof(acpi_header_t) },
1454 { acpi_create_mcfg, NULL, sizeof(acpi_mcfg_t) },
1455 { acpi_create_tcpa, NULL, sizeof(acpi_tcpa_t) },
1456 { acpi_create_tpm2, NULL, sizeof(acpi_tpm2_t) },
1457 { acpi_create_lpit, NULL, sizeof(acpi_lpit_t) },
1458 { acpi_create_madt, NULL, sizeof(acpi_header_t) },
1459 { acpi_create_bert, NULL, sizeof(acpi_bert_t) },
1460 { acpi_create_spcr, NULL, sizeof(acpi_spcr_t) },
Arthur Heymans2e3cb632023-06-30 15:01:08 +02001461 { acpi_create_gtdt, NULL, sizeof(acpi_gtdt_t) },
David Milosevicd9822742023-09-22 14:34:28 +02001462 { acpi_create_pptt, NULL, sizeof(acpi_pptt_t) },
Naresh Solanki6920c6f2023-09-13 12:01:58 +02001463 { acpi_create_iort, NULL, sizeof(acpi_iort_t) },
Marek Maslanka017003c2023-12-07 13:21:35 +00001464 { acpi_create_wdat, NULL, sizeof(acpi_wdat_t) },
Arthur Heymans01af0f82023-06-27 11:58:04 +02001465 };
1466
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001467 current = start;
1468
1469 /* Align ACPI tables to 16byte */
Aaron Durbin07a1b282015-12-10 17:07:38 -06001470 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001471
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001472 /* Special case for qemu */
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001473 fw = fw_cfg_acpi_tables(current);
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001474 if (fw) {
1475 rsdp = NULL;
1476 /* Find RSDP. */
1477 for (void *p = (void *)current; p < (void *)fw; p += 16) {
1478 if (valid_rsdp((acpi_rsdp_t *)p)) {
1479 rsdp = p;
Bin Mengf3027b82023-05-09 15:21:49 +08001480 coreboot_rsdp = (uintptr_t)rsdp;
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001481 break;
1482 }
1483 }
1484 if (!rsdp)
1485 return fw;
1486
Arthur Heymansc2830c92023-08-22 12:50:43 +02001487 current = fw;
1488 current = acpi_align_current(current);
1489 if (rsdp->xsdt_address == 0) {
1490 xsdt = (acpi_xsdt_t *)current;
1491 current += sizeof(acpi_xsdt_t);
1492 current = acpi_align_current(current);
1493
1494 /*
1495 * Qemu only creates an RSDT.
1496 * Add an XSDT based on the existing RSDT entries.
1497 */
1498 acpi_rsdt_t *existing_rsdt = (acpi_rsdt_t *)(uintptr_t)rsdp->rsdt_address;
1499 acpi_write_rsdp(rsdp, existing_rsdt, xsdt, oem_id);
1500 acpi_write_xsdt(xsdt, oem_id, oem_table_id);
1501 /*
1502 * Copy existing entries to the new XSDT. This will override existing
1503 * RSDT entries with the same value.
1504 */
1505 for (int i = 0; existing_rsdt->entry[i]; i++)
1506 acpi_add_table(rsdp, (void *)(uintptr_t)existing_rsdt->entry[i]);
1507
1508 }
1509
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001510 /* Add BOOT0000 for Linux google firmware driver */
1511 printk(BIOS_DEBUG, "ACPI: * SSDT\n");
Arthur Heymansc2830c92023-08-22 12:50:43 +02001512 ssdt = (acpi_header_t *)current;
1513 current += sizeof(acpi_header_t);
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001514
1515 memset((void *)ssdt, 0, sizeof(acpi_header_t));
1516
1517 memcpy(&ssdt->signature, "SSDT", 4);
1518 ssdt->revision = get_acpi_table_revision(SSDT);
1519 memcpy(&ssdt->oem_id, OEM_ID, 6);
1520 memcpy(&ssdt->oem_table_id, oem_table_id, 8);
1521 ssdt->oem_revision = 42;
1522 memcpy(&ssdt->asl_compiler_id, ASLC, 4);
1523 ssdt->asl_compiler_revision = asl_revision;
1524 ssdt->length = sizeof(acpi_header_t);
1525
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001526 acpigen_set_current((char *)current);
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001527
1528 /* Write object to declare coreboot tables */
1529 acpi_ssdt_write_cbtable();
1530
1531 /* (Re)calculate length and checksum. */
1532 ssdt->length = current - (unsigned long)ssdt;
1533 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
1534
Arthur Heymans01af0f82023-06-27 11:58:04 +02001535 acpi_create_ssdt_generator(ssdt, NULL);
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001536
1537 acpi_add_table(rsdp, ssdt);
1538
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001539 return fw;
Patrick Rudolphcfe08ff2019-09-10 15:40:47 +02001540 }
Vladimir Serbinenko41877d82014-09-01 22:18:01 +02001541
Julius Werner834b3ec2020-03-04 16:52:08 -08001542 dsdt_file = cbfs_map(CONFIG_CBFS_PREFIX "/dsdt.aml", &dsdt_size);
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001543 if (!dsdt_file) {
1544 printk(BIOS_ERR, "No DSDT file, skipping ACPI tables\n");
Arthur Heymans0600aa62023-06-17 14:13:54 +02001545 return start;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001546 }
1547
1548 if (dsdt_file->length > dsdt_size
Elyes HAOUAS7d87e762016-10-03 22:11:07 +02001549 || dsdt_file->length < sizeof(acpi_header_t)
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001550 || memcmp(dsdt_file->signature, "DSDT", 4) != 0) {
1551 printk(BIOS_ERR, "Invalid DSDT file, skipping ACPI tables\n");
Grzegorz Bernacki042ac352023-04-21 13:19:58 +00001552 cbfs_unmap(dsdt_file);
Arthur Heymans0600aa62023-06-17 14:13:54 +02001553 return start;
Vladimir Serbinenko36f8d272015-05-31 12:31:59 +02001554 }
1555
Julius Werner834b3ec2020-03-04 16:52:08 -08001556 slic_file = cbfs_map(CONFIG_CBFS_PREFIX "/slic", &slic_size);
Vladimir Serbinenko2cb29782015-05-31 11:32:09 +02001557 if (slic_file
1558 && (slic_file->length > slic_size
Elyes HAOUAS7d87e762016-10-03 22:11:07 +02001559 || slic_file->length < sizeof(acpi_header_t)
Benjamin Doron2b2dc0c2020-09-12 02:55:57 +00001560 || (memcmp(slic_file->signature, "SLIC", 4) != 0
1561 && memcmp(slic_file->signature, "MSDM", 4) != 0))) {
Grzegorz Bernacki042ac352023-04-21 13:19:58 +00001562 cbfs_unmap(slic_file);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001563 slic_file = 0;
1564 }
1565
1566 if (slic_file) {
1567 memcpy(oem_id, slic_file->oem_id, 6);
1568 memcpy(oem_table_id, slic_file->oem_table_id, 8);
1569 } else {
1570 memcpy(oem_id, OEM_ID, 6);
1571 memcpy(oem_table_id, ACPI_TABLE_CREATOR, 8);
1572 }
1573
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001574 printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx.\n", start);
1575
Arthur Heymansd8f2dce2023-06-22 13:42:36 +02001576 /* We need at least an RSDP, RSDT for ACPI 1.0 compat, otherwise XSDT */
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001577 rsdp = (acpi_rsdp_t *)current;
Arthur Heymans2e7e2d92022-03-03 22:28:27 +01001578 coreboot_rsdp = (uintptr_t)rsdp;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001579 current += sizeof(acpi_rsdp_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001580 current = acpi_align_current(current);
Arthur Heymansd8f2dce2023-06-22 13:42:36 +02001581 if (current + sizeof(acpi_rsdt_t) - 1 <= UINT32_MAX) {
1582 rsdt = (acpi_rsdt_t *)current;
1583 current += sizeof(acpi_rsdt_t);
1584 current = acpi_align_current(current);
1585 } else {
1586 printk(BIOS_INFO, "Not adding RSDT because tables reside above 4G.");
1587 }
1588
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001589 xsdt = (acpi_xsdt_t *)current;
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001590 current += sizeof(acpi_xsdt_t);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001591 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001592
1593 /* clear all table memory */
Elyes Haouas3141fbad2022-11-18 15:22:32 +01001594 memset((void *)start, 0, current - start);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001595
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001596 acpi_write_rsdp(rsdp, rsdt, xsdt, oem_id);
1597 acpi_write_rsdt(rsdt, oem_id, oem_table_id);
1598 acpi_write_xsdt(xsdt, oem_id, oem_table_id);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001599
Arthur Heymans6af72612023-06-29 20:25:32 +02001600 if (ENV_X86) {
1601 printk(BIOS_DEBUG, "ACPI: * FACS\n");
1602 current = ALIGN_UP(current, 64);
1603 facs = (acpi_facs_t *)current;
1604 current += sizeof(acpi_facs_t);
1605 current = acpi_align_current(current);
1606 acpi_create_facs(facs);
1607 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001608
Arthur Heymans01af0f82023-06-27 11:58:04 +02001609 for (size_t i = 0; i < ARRAY_SIZE(tables); i++) {
1610 acpi_header_t *header = (acpi_header_t *)current;
1611 memset(header, 0, tables[i].min_size);
1612 tables[i].create_table(header, tables[i].args);
1613 if (header->length < tables[i].min_size)
1614 continue;
1615 header->checksum = 0;
1616 header->checksum = acpi_checksum((void *)header, header->length);
1617 current += header->length;
Aaron Durbin07a1b282015-12-10 17:07:38 -06001618 current = acpi_align_current(current);
Kyösti Mälkki0bcdd402023-07-06 14:47:07 +03001619
1620 if (tables[i].create_table == acpi_create_dsdt)
1621 continue;
1622
Arthur Heymans01af0f82023-06-27 11:58:04 +02001623 printk(BIOS_DEBUG, "ACPI: * %.4s\n", header->signature);
1624 acpi_add_table(rsdp, header);
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +01001625 }
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001626
Grzegorz Bernacki042ac352023-04-21 13:19:58 +00001627 /*
1628 * cbfs_unmap() uses mem_pool_free() which works correctly only
1629 * if freeing is done in reverse order than memory allocation.
1630 * This is why unmapping of dsdt_file must be done after
1631 * unmapping slic file.
1632 */
Arthur Heymans01af0f82023-06-27 11:58:04 +02001633 cbfs_unmap(slic_file);
Grzegorz Bernacki042ac352023-04-21 13:19:58 +00001634 cbfs_unmap(dsdt_file);
1635
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001636 printk(BIOS_DEBUG, "current = %lx\n", current);
1637
1638 for (dev = all_devices; dev; dev = dev->next) {
1639 if (dev->ops && dev->ops->write_acpi_tables) {
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001640 current = dev->ops->write_acpi_tables(dev, current,
1641 rsdp);
Aaron Durbin07a1b282015-12-10 17:07:38 -06001642 current = acpi_align_current(current);
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001643 }
1644 }
1645
1646 printk(BIOS_INFO, "ACPI: done.\n");
Arthur Heymans0ad766c2023-06-07 10:45:59 +02001647
1648 if (CONFIG(DEBUG_ACPICA_COMPATIBLE)) {
1649 printk(BIOS_DEBUG, "Printing ACPI tables in ACPICA compatible format\n");
Arthur Heymans1cdeaea2023-07-06 16:24:50 +02001650 if (facs)
1651 acpidump_print(facs);
1652 acpidump_print(dsdt);
Arthur Heymans3e523b42023-06-15 17:04:16 +02001653 for (size_t i = 0; xsdt->entry[i] != 0; i++) {
1654 acpidump_print((void *)(uintptr_t)xsdt->entry[i]);
Arthur Heymans0ad766c2023-06-07 10:45:59 +02001655 }
1656 printk(BIOS_DEBUG, "Done printing ACPI tables in ACPICA compatible format\n");
1657 }
1658
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001659 return current;
1660}
Vladimir Serbinenko2d7bd8a2014-08-30 19:28:05 +02001661
Rudolf Marek33cafe52009-04-13 18:07:02 +00001662static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp)
1663{
1664 if (strncmp((char *)rsdp, RSDP_SIG, sizeof(RSDP_SIG) - 1) != 0)
1665 return NULL;
1666
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001667 printk(BIOS_DEBUG, "Looking on %p for valid checksum\n", rsdp);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001668
1669 if (acpi_checksum((void *)rsdp, 20) != 0)
1670 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001671 printk(BIOS_DEBUG, "Checksum 1 passed\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001672
1673 if ((rsdp->revision > 1) && (acpi_checksum((void *)rsdp,
1674 rsdp->length) != 0))
1675 return NULL;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001676 printk(BIOS_DEBUG, "Checksum 2 passed all OK\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001677
1678 return rsdp;
1679}
1680
Rudolf Marek33cafe52009-04-13 18:07:02 +00001681void *acpi_find_wakeup_vector(void)
1682{
1683 char *p, *end;
Arthur Heymansd8f2dce2023-06-22 13:42:36 +02001684 acpi_xsdt_t *xsdt;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001685 acpi_facs_t *facs;
Martin Rothc5f49262012-12-14 19:17:55 -07001686 acpi_fadt_t *fadt = NULL;
Kyösti Mälkki072d436b2016-06-17 08:44:40 +03001687 acpi_rsdp_t *rsdp = NULL;
Uwe Hermann622824c2010-11-19 15:14:42 +00001688 void *wake_vec;
Rudolf Marek33cafe52009-04-13 18:07:02 +00001689 int i;
1690
Kyösti Mälkkib8c7ea02020-11-17 16:41:38 +02001691 if (!acpi_is_wakeup_s3())
Rudolf Marek33cafe52009-04-13 18:07:02 +00001692 return NULL;
1693
Uwe Hermann622824c2010-11-19 15:14:42 +00001694 printk(BIOS_DEBUG, "Trying to find the wakeup vector...\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001695
Uwe Hermann622824c2010-11-19 15:14:42 +00001696 /* Find RSDP. */
1697 for (p = (char *)0xe0000; p < (char *)0xfffff; p += 16) {
Lee Leahy0b5678f2017-03-16 16:01:40 -07001698 rsdp = valid_rsdp((acpi_rsdp_t *)p);
1699 if (rsdp)
Rudolf Marek33cafe52009-04-13 18:07:02 +00001700 break;
1701 }
1702
Angel Pons98a68ad2018-11-04 12:25:25 +01001703 if (rsdp == NULL) {
1704 printk(BIOS_ALERT,
1705 "No RSDP found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001706 return NULL;
Angel Pons98a68ad2018-11-04 12:25:25 +01001707 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001708
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001709 printk(BIOS_DEBUG, "RSDP found at %p\n", rsdp);
Arthur Heymansd8f2dce2023-06-22 13:42:36 +02001710 xsdt = (acpi_xsdt_t *)(uintptr_t)rsdp->xsdt_address;
Stefan Reinauer14e22772010-04-27 06:56:47 +00001711
Arthur Heymansd8f2dce2023-06-22 13:42:36 +02001712 end = (char *)xsdt + xsdt->header.length;
1713 printk(BIOS_DEBUG, "XSDT found at %p ends at %p\n", xsdt, end);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001714
Arthur Heymansd8f2dce2023-06-22 13:42:36 +02001715 for (i = 0; ((char *)&xsdt->entry[i]) < end; i++) {
1716 fadt = (acpi_fadt_t *)(uintptr_t)xsdt->entry[i];
Stefan Reinauercdfe3762009-07-21 22:15:43 +00001717 if (strncmp((char *)fadt, "FACP", 4) == 0)
Rudolf Marek33cafe52009-04-13 18:07:02 +00001718 break;
1719 fadt = NULL;
1720 }
1721
Angel Pons98a68ad2018-11-04 12:25:25 +01001722 if (fadt == NULL) {
1723 printk(BIOS_ALERT,
1724 "No FADT found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001725 return NULL;
Angel Pons98a68ad2018-11-04 12:25:25 +01001726 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001727
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001728 printk(BIOS_DEBUG, "FADT found at %p\n", fadt);
Arthur Heymansa07b09a2023-07-05 12:53:10 +02001729 facs = (acpi_facs_t *)(uintptr_t)((uint64_t)fadt->x_firmware_ctl_l
1730 | (uint64_t)fadt->x_firmware_ctl_h << 32);
Rudolf Marek33cafe52009-04-13 18:07:02 +00001731
Stefan Reinauer7e9771c2009-04-22 08:17:38 +00001732 if (facs == NULL) {
Angel Pons98a68ad2018-11-04 12:25:25 +01001733 printk(BIOS_ALERT,
1734 "No FACS found, wake up from S3 not possible.\n");
Rudolf Marek33cafe52009-04-13 18:07:02 +00001735 return NULL;
Stefan Reinauer7e9771c2009-04-22 08:17:38 +00001736 }
Rudolf Marek33cafe52009-04-13 18:07:02 +00001737
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001738 printk(BIOS_DEBUG, "FACS found at %p\n", facs);
Stefan Reinauer71a30182015-07-30 16:28:13 -07001739 wake_vec = (void *)(uintptr_t)facs->firmware_waking_vector;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001740 printk(BIOS_DEBUG, "OS waking vector is %p\n", wake_vec);
Uwe Hermann622824c2010-11-19 15:14:42 +00001741
Rudolf Marek33cafe52009-04-13 18:07:02 +00001742 return wake_vec;
1743}
1744
Aaron Durbin64031672018-04-21 14:45:32 -06001745__weak int acpi_get_gpe(int gpe)
Duncan Laurie1f6e6812016-09-19 12:04:19 -07001746{
1747 return -1; /* implemented by SOC */
1748}
Marc Jones93a51762018-08-22 18:57:24 -06001749
Elyes Haouas8b950f42022-02-16 12:08:16 +01001750u8 get_acpi_fadt_minor_version(void)
1751{
1752 return ACPI_FADT_MINOR_VERSION_0;
1753}
1754
Marc Jones93a51762018-08-22 18:57:24 -06001755int get_acpi_table_revision(enum acpi_tables table)
1756{
1757 switch (table) {
1758 case FADT:
Elyes Haouas8b950f42022-02-16 12:08:16 +01001759 return ACPI_FADT_REV_ACPI_6;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001760 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 +01001761 return 3;
Marc Jones93a51762018-08-22 18:57:24 -06001762 case MCFG:
1763 return 1;
1764 case TCPA:
1765 return 2;
Philipp Deppenwiese296164e02018-10-18 15:39:34 +02001766 case TPM2:
1767 return 4;
Martin Roth0949e732021-10-01 14:28:22 -06001768 case SSDT: /* ACPI 3.0 up to 6.3: 2 */
Marc Jones93a51762018-08-22 18:57:24 -06001769 return 2;
Jonathan Zhanga3311b92022-12-01 17:13:41 -08001770 case SRAT: /* ACPI 2.0: 1, ACPI 3.0: 2, ACPI 4.0 up to 6.4: 3 */
1771 return 3;
Jonathan Zhang2a4e1f42021-04-01 11:43:37 -07001772 case HMAT: /* ACPI 6.4: 2 */
1773 return 2;
Marc Jones93a51762018-08-22 18:57:24 -06001774 case DMAR:
1775 return 1;
Martin Roth0949e732021-10-01 14:28:22 -06001776 case SLIT: /* ACPI 2.0 up to 6.3: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06001777 return 1;
Patrick Rudolph9d98e5a2019-06-14 19:00:04 +02001778 case SPMI: /* IMPI 2.0 */
1779 return 5;
Marc Jones93a51762018-08-22 18:57:24 -06001780 case HPET: /* Currently 1. Table added in ACPI 2.0. */
1781 return 1;
Elyes HAOUASf288b392018-11-11 15:22:30 +01001782 case VFCT: /* ACPI 2.0/3.0/4.0: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06001783 return 1;
1784 case IVRS:
Jason Glenesk1916f892020-07-24 02:51:30 -07001785 return IVRS_FORMAT_MIXED;
Marc Jones93a51762018-08-22 18:57:24 -06001786 case DBG2:
1787 return 0;
Martin Roth0949e732021-10-01 14:28:22 -06001788 case FACS: /* ACPI 2.0/3.0: 1, ACPI 4.0 up to 6.3: 2 */
Marc Jones93a51762018-08-22 18:57:24 -06001789 return 1;
Martin Roth0949e732021-10-01 14:28:22 -06001790 case RSDT: /* ACPI 1.0 up to 6.3: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06001791 return 1;
Martin Roth0949e732021-10-01 14:28:22 -06001792 case XSDT: /* ACPI 2.0 up to 6.3: 1 */
Marc Jones93a51762018-08-22 18:57:24 -06001793 return 1;
Martin Roth0949e732021-10-01 14:28:22 -06001794 case RSDP: /* ACPI 2.0 up to 6.3: 2 */
Marc Jones93a51762018-08-22 18:57:24 -06001795 return 2;
Rocky Phaguraeff07132021-01-10 15:42:50 -08001796 case EINJ:
1797 return 1;
Marc Jones93a51762018-08-22 18:57:24 -06001798 case HEST:
1799 return 1;
1800 case NHLT:
1801 return 5;
Marshall Dawson44705c62018-09-04 13:47:15 -06001802 case BERT:
1803 return 1;
Jonathan Zhanga3311b92022-12-01 17:13:41 -08001804 case CEDT: /* CXL 3.0 section 9.17.1 */
1805 return 1;
Jason Glenesk61624b22020-11-02 20:06:23 -08001806 case CRAT:
1807 return 1;
Michael Niewöhnerf0a44ae2021-01-01 21:04:09 +01001808 case LPIT: /* ACPI 5.1 up to 6.3: 0 */
1809 return 0;
Arthur Heymans90464072023-06-07 12:53:50 +02001810 case SPCR:
1811 return 4;
Arthur Heymans2e3cb632023-06-30 15:01:08 +02001812 case GTDT:
1813 return 3;
David Milosevicd9822742023-09-22 14:34:28 +02001814 case PPTT: /* ACPI 6.4 */
1815 return 3;
Naresh Solanki6920c6f2023-09-13 12:01:58 +02001816 case IORT: /* IO Remapping Table E.e */
1817 return 6;
Marek Maslanka017003c2023-12-07 13:21:35 +00001818 case WDAT:
1819 return 1;
Marc Jones93a51762018-08-22 18:57:24 -06001820 default:
1821 return -1;
1822 }
1823 return -1;
1824}