mb/google/brya/var/orisa: Update Type C DisplayPort HPD Configuration
[coreboot2.git] / src / acpi / acpi.c
blob06aa038caaca98f0123709759d7288bde13ae876
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3 * coreboot ACPI Table support
4 */
6 /*
7 * Each system port implementing ACPI has to provide two functions:
9 * write_acpi_tables()
10 * acpi_dump_apics()
12 * See Kontron 986LCD-M port for a good example of an ACPI implementation
13 * in coreboot.
16 #include <acpi/acpi.h>
17 #include <acpi/acpi_iort.h>
18 #include <acpi/acpi_ivrs.h>
19 #include <acpi/acpigen.h>
20 #include <cbfs.h>
21 #include <cbmem.h>
22 #include <commonlib/helpers.h>
23 #include <console/console.h>
24 #include <cpu/cpu.h>
25 #include <device/device.h>
26 #include <device/mmio.h>
27 #include <device/pci.h>
28 #include <drivers/crb/tpm.h>
29 #include <drivers/uart/pl011.h>
30 #include <security/tpm/tss.h>
31 #include <string.h>
32 #include <types.h>
33 #include <version.h>
35 static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp);
37 u8 acpi_checksum(u8 *table, u32 length)
39 u8 ret = 0;
40 while (length--) {
41 ret += *table;
42 table++;
44 return -ret;
47 /**
48 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
49 * and checksum.
51 void acpi_add_table(acpi_rsdp_t *rsdp, void *table)
53 int i, entries_num;
54 acpi_rsdt_t *rsdt;
55 acpi_xsdt_t *xsdt;
57 /* The 32bit RSDT may not be valid if tables live above 4GiB */
58 rsdt = (acpi_rsdt_t *)(uintptr_t)rsdp->rsdt_address;
59 xsdt = (acpi_xsdt_t *)(uintptr_t)rsdp->xsdt_address;
61 /* This should always be MAX_ACPI_TABLES. */
62 entries_num = ARRAY_SIZE(xsdt->entry);
64 for (i = 0; i < entries_num; i++) {
65 if (xsdt->entry[i] == 0)
66 break;
69 if (i >= entries_num) {
70 printk(BIOS_ERR, "ACPI: Error: Could not add ACPI table, "
71 "too many tables.\n");
72 return;
75 /* Add table to the XSDT. */
76 xsdt->entry[i] = (u64)(uintptr_t)table;
78 /* Fix XSDT length or the kernel will assume invalid entries. */
79 xsdt->header.length = sizeof(acpi_header_t) + (sizeof(u64) * (i + 1));
81 /* Re-calculate checksum. */
82 xsdt->header.checksum = 0; /* Hope this won't get optimized away */
83 xsdt->header.checksum = acpi_checksum((u8 *)xsdt, xsdt->header.length);
86 * And now the same thing for the RSDT. We use the same index as for
87 * now we want the XSDT and RSDT to always be in sync in coreboot.
89 if (rsdt && (uintptr_t)table < UINT32_MAX) {
90 /* Add table to the RSDT. */
91 rsdt->entry[i] = (u32)(uintptr_t)table;
93 /* Fix RSDT length. */
94 rsdt->header.length = sizeof(acpi_header_t) + (sizeof(u32) * (i + 1));
96 /* Re-calculate checksum. */
97 rsdt->header.checksum = 0;
98 rsdt->header.checksum = acpi_checksum((u8 *)rsdt, rsdt->header.length);
101 printk(BIOS_DEBUG, "ACPI: added table %d/%d, length now %d\n",
102 i + 1, entries_num, xsdt->header.length);
105 static enum cb_err acpi_fill_header(acpi_header_t *header, const char name[4],
106 enum acpi_tables table, uint32_t size)
108 if (!header)
109 return CB_ERR;
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);
117 header->asl_compiler_revision = asl_revision;
118 header->revision = get_acpi_table_revision(table);
119 header->length = size;
121 return CB_SUCCESS;
124 static int acpi_create_mcfg_mmconfig(acpi_mcfg_mmconfig_t *mmconfig, u64 base,
125 u16 seg_nr, u8 start, u8 end)
127 memset(mmconfig, 0, sizeof(*mmconfig));
128 mmconfig->base_address = base;
129 mmconfig->pci_segment_group_number = seg_nr;
130 mmconfig->start_bus_number = start;
131 mmconfig->end_bus_number = end;
133 return sizeof(acpi_mcfg_mmconfig_t);
136 static void acpi_create_madt(acpi_header_t *header, void *unused)
138 acpi_madt_t *madt = (acpi_madt_t *)header;
139 unsigned long current = (unsigned long)madt + sizeof(acpi_madt_t);
141 if (acpi_fill_header(header, "APIC", MADT, sizeof(acpi_madt_t)) != CB_SUCCESS)
142 return;
144 current = acpi_arch_fill_madt(madt, current);
146 if (CONFIG(ACPI_CUSTOM_MADT))
147 current = acpi_fill_madt(current);
149 /* (Re)calculate length . */
150 header->length = current - (unsigned long)madt;
153 static unsigned long acpi_fill_mcfg(unsigned long current)
155 for (int i = 0; i < PCI_SEGMENT_GROUP_COUNT; i++) {
156 current += acpi_create_mcfg_mmconfig((acpi_mcfg_mmconfig_t *)current,
157 CONFIG_ECAM_MMCONF_BASE_ADDRESS + i * PCI_PER_SEGMENT_GROUP_ECAM_SIZE,
160 PCI_BUSES_PER_SEGMENT_GROUP - 1);
163 return current;
166 /* MCFG is defined in the PCI Firmware Specification 3.0. */
167 static void acpi_create_mcfg(acpi_header_t *header, void *unused)
169 acpi_mcfg_t *mcfg = (acpi_mcfg_t *)header;
170 unsigned long current = (unsigned long)mcfg + sizeof(acpi_mcfg_t);
173 if (acpi_fill_header(header, "MCFG", MCFG, sizeof(acpi_mcfg_t)) != CB_SUCCESS)
174 return;
176 if (CONFIG(ECAM_MMCONF_SUPPORT))
177 current = acpi_fill_mcfg(current);
179 /* (Re)calculate length */
180 header->length = current - (unsigned long)mcfg;
183 static void *get_tcpa_log(u32 *size)
185 const struct cbmem_entry *ce;
186 const u32 tcpa_default_log_len = 0x10000;
187 void *lasa;
188 ce = cbmem_entry_find(CBMEM_ID_TCPA_TCG_LOG);
189 if (ce) {
190 lasa = cbmem_entry_start(ce);
191 *size = cbmem_entry_size(ce);
192 printk(BIOS_DEBUG, "TCPA log found at %p\n", lasa);
193 return lasa;
195 lasa = cbmem_add(CBMEM_ID_TCPA_TCG_LOG, tcpa_default_log_len);
196 if (!lasa) {
197 printk(BIOS_ERR, "TCPA log creation failed\n");
198 return NULL;
201 printk(BIOS_DEBUG, "TCPA log created at %p\n", lasa);
202 memset(lasa, 0, tcpa_default_log_len);
204 *size = tcpa_default_log_len;
205 return lasa;
208 static void acpi_create_tcpa(acpi_header_t *header, void *unused)
210 if (tlcl_get_family() != TPM_1)
211 return;
213 acpi_tcpa_t *tcpa = (acpi_tcpa_t *)header;
214 u32 tcpa_log_len;
215 void *lasa;
217 lasa = get_tcpa_log(&tcpa_log_len);
218 if (!lasa)
219 return;
221 if (acpi_fill_header(header, "TCPA", TCPA, sizeof(acpi_tcpa_t)) != CB_SUCCESS)
222 return;
224 tcpa->platform_class = 0;
225 tcpa->laml = tcpa_log_len;
226 tcpa->lasa = (uintptr_t)lasa;
229 static void *get_tpm2_log(u32 *size)
231 const struct cbmem_entry *ce;
232 const u32 tpm2_default_log_len = 0x10000;
233 void *lasa;
234 ce = cbmem_entry_find(CBMEM_ID_TPM2_TCG_LOG);
235 if (ce) {
236 lasa = cbmem_entry_start(ce);
237 *size = cbmem_entry_size(ce);
238 printk(BIOS_DEBUG, "TPM2 log found at %p\n", lasa);
239 return lasa;
241 lasa = cbmem_add(CBMEM_ID_TPM2_TCG_LOG, tpm2_default_log_len);
242 if (!lasa) {
243 printk(BIOS_ERR, "TPM2 log creation failed\n");
244 return NULL;
247 printk(BIOS_DEBUG, "TPM2 log created at %p\n", lasa);
248 memset(lasa, 0, tpm2_default_log_len);
250 *size = tpm2_default_log_len;
251 return lasa;
254 static void acpi_create_tpm2(acpi_header_t *header, void *unused)
256 if (tlcl_get_family() != TPM_2)
257 return;
259 acpi_tpm2_t *tpm2 = (acpi_tpm2_t *)header;
260 u32 tpm2_log_len;
261 void *lasa;
264 * Some payloads like SeaBIOS depend on log area to use TPM2.
265 * Get the memory size and address of TPM2 log area or initialize it.
267 lasa = get_tpm2_log(&tpm2_log_len);
268 if (!lasa)
269 tpm2_log_len = 0;
271 if (acpi_fill_header(header, "TPM2", TPM2, sizeof(acpi_tpm2_t)) != CB_SUCCESS)
272 return;
274 /* Hard to detect for coreboot. Just set it to 0 */
275 tpm2->platform_class = 0;
276 if (CONFIG(CRB_TPM) && crb_tpm_is_active()) {
277 /* Must be set to 7 for CRB Support */
278 tpm2->control_area = CONFIG_CRB_TPM_BASE_ADDRESS + 0x40;
279 tpm2->start_method = 7;
280 } else {
281 /* Must be set to 0 for FIFO interface support */
282 tpm2->control_area = 0;
283 tpm2->start_method = 6;
285 memset(tpm2->msp, 0, sizeof(tpm2->msp));
287 /* Fill the log area size and start address fields. */
288 tpm2->laml = tpm2_log_len;
289 tpm2->lasa = (uintptr_t)lasa;
292 static void acpi_ssdt_write_cbtable(void)
294 const struct cbmem_entry *cbtable;
295 uintptr_t base;
296 uint32_t size;
298 cbtable = cbmem_entry_find(CBMEM_ID_CBTABLE);
299 if (!cbtable)
300 return;
301 base = (uintptr_t)cbmem_entry_start(cbtable);
302 size = cbmem_entry_size(cbtable);
304 acpigen_write_device("CTBL");
305 acpigen_write_coreboot_hid(COREBOOT_ACPI_ID_CBTABLE);
306 acpigen_write_name_integer("_UID", 0);
307 acpigen_write_STA(ACPI_STATUS_DEVICE_ALL_ON);
308 acpigen_write_name("_CRS");
309 acpigen_write_resourcetemplate_header();
310 acpigen_resource_consumer_mmio(base, base + size - 1,
311 MEM_RSRC_FLAG_MEM_READ_ONLY
312 | MEM_RSRC_FLAG_MEM_ATTR_CACHE);
313 acpigen_write_resourcetemplate_footer();
314 acpigen_pop_len();
317 static void acpi_create_ssdt_generator(acpi_header_t *ssdt, void *unused)
319 unsigned long current = (unsigned long)ssdt + sizeof(acpi_header_t);
321 if (acpi_fill_header(ssdt, "SSDT", SSDT, sizeof(acpi_header_t)) != CB_SUCCESS)
322 return;
324 acpigen_set_current((char *)current);
326 /* Write object to declare coreboot tables */
327 acpi_ssdt_write_cbtable();
330 struct device *dev;
331 for (dev = all_devices; dev; dev = dev->next)
332 if (dev->enabled && dev->ops && dev->ops->acpi_fill_ssdt)
333 dev->ops->acpi_fill_ssdt(dev);
334 current = (unsigned long)acpigen_get_current();
337 /* (Re)calculate length and checksum. */
338 ssdt->length = current - (unsigned long)ssdt;
341 int acpi_create_srat_mem(acpi_srat_mem_t *mem, u8 node, u32 basek, u32 sizek,
342 u32 flags)
344 mem->type = 1; /* Memory affinity structure */
345 mem->length = sizeof(acpi_srat_mem_t);
346 mem->base_address_low = (basek << 10);
347 mem->base_address_high = (basek >> (32 - 10));
348 mem->length_low = (sizek << 10);
349 mem->length_high = (sizek >> (32 - 10));
350 mem->proximity_domain = node;
351 mem->flags = flags;
353 return mem->length;
356 int acpi_create_srat_gia_pci(acpi_srat_gia_t *gia, u32 proximity_domain,
357 struct device *dev, u32 flags)
359 /* Only handle PCI devices. */
360 if (dev->path.type != DEVICE_PATH_PCI)
361 return 0;
363 gia->type = ACPI_SRAT_STRUCTURE_GIA;
364 gia->length = sizeof(acpi_srat_gia_t);
365 gia->proximity_domain = proximity_domain;
366 gia->dev_handle_type = ACPI_SRAT_GIA_DEV_HANDLE_PCI;
367 /* First two bytes has segment number */
368 gia->dev_handle[0] = dev->upstream->segment_group;
369 gia->dev_handle[1] = 0;
370 gia->dev_handle[2] = dev->upstream->secondary; /* Byte 2 has bus number */
371 /* Byte 3 has bits 7:3 for dev, bits 2:0 for func */
372 gia->dev_handle[3] = dev->path.pci.devfn;
373 gia->flags = flags;
375 return gia->length;
378 /* http://www.microsoft.com/whdc/system/sysinternals/sratdwn.mspx */
379 void acpi_create_srat(acpi_srat_t *srat,
380 unsigned long (*acpi_fill_srat)(unsigned long current))
382 acpi_header_t *header = &(srat->header);
383 unsigned long current = (unsigned long)srat + sizeof(acpi_srat_t);
385 memset((void *)srat, 0, sizeof(acpi_srat_t));
387 if (acpi_fill_header(header, "SRAT", SRAT, sizeof(acpi_srat_t)) != CB_SUCCESS)
388 return;
390 srat->resv = 1; /* Spec: Reserved to 1 for backwards compatibility. */
392 current = acpi_fill_srat(current);
394 /* (Re)calculate length and checksum. */
395 header->length = current - (unsigned long)srat;
396 header->checksum = acpi_checksum((void *)srat, header->length);
399 int acpi_create_cedt_chbs(acpi_cedt_chbs_t *chbs, u32 uid, u32 cxl_ver, u64 base)
401 memset((void *)chbs, 0, sizeof(acpi_cedt_chbs_t));
403 chbs->type = ACPI_CEDT_STRUCTURE_CHBS;
404 chbs->length = sizeof(acpi_cedt_chbs_t);
405 chbs->uid = uid;
406 chbs->cxl_ver = cxl_ver;
407 chbs->base = base;
410 * CXL spec 2.0 section 9.14.1.2 "CXL CHBS"
411 * CXL 1.1 spec compliant host bridge: 8KB
412 * CXL 2.0 spec compliant host bridge: 64KB
414 if (cxl_ver == ACPI_CEDT_CHBS_CXL_VER_1_1)
415 chbs->len = 8 * KiB;
416 else if (cxl_ver == ACPI_CEDT_CHBS_CXL_VER_2_0)
417 chbs->len = 64 * KiB;
418 else
419 printk(BIOS_ERR, "ACPI(%s:%s): Incorrect CXL version:%d\n", __FILE__, __func__,
420 cxl_ver);
422 return chbs->length;
425 int acpi_create_cedt_cfmws(acpi_cedt_cfmws_t *cfmws, u64 base_hpa, u64 window_size, u8 eniw,
426 u32 hbig, u16 restriction, u16 qtg_id, const u32 *interleave_target)
428 memset((void *)cfmws, 0, sizeof(acpi_cedt_cfmws_t));
430 cfmws->type = ACPI_CEDT_STRUCTURE_CFMWS;
432 u8 niw = 0;
433 if (eniw >= 8)
434 printk(BIOS_ERR, "ACPI(%s:%s): Incorrect eniw::%d\n", __FILE__, __func__, eniw);
435 else
436 /* NIW = 2 ** ENIW */
437 niw = 0x1 << eniw;
438 /* 36 + 4 * NIW */
439 cfmws->length = sizeof(acpi_cedt_cfmws_t) + 4 * niw;
441 cfmws->base_hpa = base_hpa;
442 cfmws->window_size = window_size;
443 cfmws->eniw = eniw;
445 // 0: Standard Modulo Arithmetic. Other values reserved.
446 cfmws->interleave_arithmetic = 0;
448 cfmws->hbig = hbig;
449 cfmws->restriction = restriction;
450 cfmws->qtg_id = qtg_id;
451 memcpy(&cfmws->interleave_target, interleave_target, 4 * niw);
453 return cfmws->length;
456 void acpi_create_cedt(acpi_cedt_t *cedt, unsigned long (*acpi_fill_cedt)(unsigned long current))
458 acpi_header_t *header = &(cedt->header);
459 unsigned long current = (unsigned long)cedt + sizeof(acpi_cedt_t);
461 memset((void *)cedt, 0, sizeof(acpi_cedt_t));
463 if (acpi_fill_header(header, "CEDT", CEDT, sizeof(acpi_cedt_t)) != CB_SUCCESS)
464 return;
466 current = acpi_fill_cedt(current);
468 /* (Re)calculate length and checksum. */
469 header->length = current - (unsigned long)cedt;
470 header->checksum = acpi_checksum((void *)cedt, header->length);
473 int acpi_create_hmat_mpda(acpi_hmat_mpda_t *mpda, u32 initiator, u32 memory)
475 memset((void *)mpda, 0, sizeof(acpi_hmat_mpda_t));
477 mpda->type = 0; /* Memory Proximity Domain Attributes structure */
478 mpda->length = sizeof(acpi_hmat_mpda_t);
480 * Proximity Domain for Attached Initiator field is valid.
481 * Bit 1 and bit 2 are reserved since HMAT revision 2.
483 mpda->flags = (1 << 0);
484 mpda->proximity_domain_initiator = initiator;
485 mpda->proximity_domain_memory = memory;
487 return mpda->length;
490 void acpi_create_hmat(acpi_hmat_t *hmat,
491 unsigned long (*acpi_fill_hmat)(unsigned long current))
493 acpi_header_t *header = &(hmat->header);
494 unsigned long current = (unsigned long)hmat + sizeof(acpi_hmat_t);
496 memset((void *)hmat, 0, sizeof(acpi_hmat_t));
498 if (acpi_fill_header(header, "HMAT", HMAT, sizeof(acpi_hmat_t)) != CB_SUCCESS)
499 return;
501 current = acpi_fill_hmat(current);
503 /* (Re)calculate length and checksum. */
504 header->length = current - (unsigned long)hmat;
505 header->checksum = acpi_checksum((void *)hmat, header->length);
508 /* http://h21007.www2.hp.com/portal/download/files/unprot/Itanium/slit.pdf */
509 void acpi_create_slit(acpi_slit_t *slit,
510 unsigned long (*acpi_fill_slit)(unsigned long current))
512 acpi_header_t *header = &(slit->header);
513 unsigned long current = (unsigned long)slit + sizeof(acpi_slit_t);
515 memset((void *)slit, 0, sizeof(acpi_slit_t));
517 if (acpi_fill_header(header, "SLIT", SLIT, sizeof(acpi_slit_t)) != CB_SUCCESS)
518 return;
520 current = acpi_fill_slit(current);
522 /* (Re)calculate length and checksum. */
523 header->length = current - (unsigned long)slit;
524 header->checksum = acpi_checksum((void *)slit, header->length);
528 * This method adds the ACPI error injection capability. It fills the default information.
529 * HW dependent code (caller) can modify the defaults upon return. If no changes are necessary
530 * and the defaults are acceptable then caller can simply add the table (acpi_add_table).
531 * INPUTS:
532 * einj - ptr to the starting location of EINJ table
533 * actions - number of actions to trigger an error (HW dependent)
534 * addr - address of trigger action table. This should be ACPI reserved memory and it will be
535 * shared between OS and FW.
537 void acpi_create_einj(acpi_einj_t *einj, uintptr_t addr, u8 actions)
539 int i;
540 acpi_header_t *header = &(einj->header);
541 acpi_injection_header_t *inj_header = &(einj->inj_header);
542 acpi_einj_smi_t *einj_smi = (acpi_einj_smi_t *)addr;
543 acpi_einj_trigger_table_t *tat;
544 if (!header)
545 return;
547 printk(BIOS_DEBUG, "%s einj_smi = %p\n", __func__, einj_smi);
548 memset(einj_smi, 0, sizeof(acpi_einj_smi_t));
549 tat = (acpi_einj_trigger_table_t *)((uint8_t *)einj_smi + sizeof(acpi_einj_smi_t));
550 tat->header_size = 16;
551 tat->revision = 0;
552 tat->table_size = sizeof(acpi_einj_trigger_table_t) +
553 sizeof(acpi_einj_action_table_t) * actions - 1;
554 tat->entry_count = actions;
555 printk(BIOS_DEBUG, "%s trigger_action_table = %p\n", __func__, tat);
557 for (i = 0; i < actions; i++) {
558 tat->trigger_action[i].action = TRIGGER_ERROR;
559 tat->trigger_action[i].instruction = NO_OP;
560 tat->trigger_action[i].flags = FLAG_IGNORE;
561 tat->trigger_action[i].reg.space_id = ACPI_ADDRESS_SPACE_MEMORY;
562 tat->trigger_action[i].reg.bit_width = 64;
563 tat->trigger_action[i].reg.bit_offset = 0;
564 tat->trigger_action[i].reg.access_size = ACPI_ACCESS_SIZE_QWORD_ACCESS;
565 tat->trigger_action[i].reg.addr = 0;
566 tat->trigger_action[i].value = 0;
567 tat->trigger_action[i].mask = 0xFFFFFFFF;
570 acpi_einj_action_table_t default_actions[ACTION_COUNT] = {
571 [0] = {
572 .action = BEGIN_INJECT_OP,
573 .instruction = WRITE_REGISTER_VALUE,
574 .flags = FLAG_PRESERVE,
575 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->op_state),
576 .value = 0,
577 .mask = 0xFFFFFFFF
579 [1] = {
580 .action = GET_TRIGGER_ACTION_TABLE,
581 .instruction = READ_REGISTER,
582 .flags = FLAG_IGNORE,
583 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->trigger_action_table),
584 .value = 0,
585 .mask = 0xFFFFFFFFFFFFFFFF
587 [2] = {
588 .action = SET_ERROR_TYPE,
589 .instruction = WRITE_REGISTER,
590 .flags = FLAG_PRESERVE,
591 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->err_inject[0]),
592 .value = 0,
593 .mask = 0xFFFFFFFF
595 [3] = {
596 .action = GET_ERROR_TYPE,
597 .instruction = READ_REGISTER,
598 .flags = FLAG_IGNORE,
599 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->err_inj_cap),
600 .value = 0,
601 .mask = 0xFFFFFFFF
603 [4] = {
604 .action = END_INJECT_OP,
605 .instruction = WRITE_REGISTER_VALUE,
606 .flags = FLAG_PRESERVE,
607 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->op_state),
608 .value = 0,
609 .mask = 0xFFFFFFFF
612 [5] = {
613 .action = EXECUTE_INJECT_OP,
614 .instruction = WRITE_REGISTER_VALUE,
615 .flags = FLAG_PRESERVE,
616 .reg = EINJ_REG_IO(),
617 .value = 0x9a,
618 .mask = 0xFFFF,
620 [6] = {
621 .action = CHECK_BUSY_STATUS,
622 .instruction = READ_REGISTER_VALUE,
623 .flags = FLAG_IGNORE,
624 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->op_status),
625 .value = 1,
626 .mask = 1,
628 [7] = {
629 .action = GET_CMD_STATUS,
630 .instruction = READ_REGISTER,
631 .flags = FLAG_PRESERVE,
632 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->cmd_sts),
633 .value = 0,
634 .mask = 0x1fe,
636 [8] = {
637 .action = SET_ERROR_TYPE_WITH_ADDRESS,
638 .instruction = WRITE_REGISTER,
639 .flags = FLAG_PRESERVE,
640 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->setaddrtable),
641 .value = 1,
642 .mask = 0xffffffff
646 einj_smi->err_inj_cap = ACPI_EINJ_DEFAULT_CAP;
647 einj_smi->trigger_action_table = (u64)(uintptr_t)tat;
649 for (i = 0; i < ACTION_COUNT; i++)
650 printk(BIOS_DEBUG, "default_actions[%d].reg.addr is %llx\n", i,
651 default_actions[i].reg.addr);
653 memset((void *)einj, 0, sizeof(*einj));
655 if (acpi_fill_header(header, "EINJ", EINJ, sizeof(acpi_einj_t)) != CB_SUCCESS)
656 return;
658 inj_header->einj_header_size = sizeof(acpi_injection_header_t);
659 inj_header->entry_count = ACTION_COUNT;
661 printk(BIOS_DEBUG, "%s einj->action_table = %p\n",
662 __func__, einj->action_table);
663 memcpy((void *)einj->action_table, (void *)default_actions, sizeof(einj->action_table));
664 header->checksum = acpi_checksum((void *)einj, sizeof(*einj));
667 void acpi_create_vfct(const struct device *device,
668 acpi_vfct_t *vfct,
669 unsigned long (*acpi_fill_vfct)(const struct device *device,
670 acpi_vfct_t *vfct_struct, unsigned long current))
672 acpi_header_t *header = &(vfct->header);
673 unsigned long current = (unsigned long)vfct + sizeof(acpi_vfct_t);
675 memset((void *)vfct, 0, sizeof(acpi_vfct_t));
677 if (acpi_fill_header(header, "VFCT", VFCT, sizeof(acpi_vfct_t)) != CB_SUCCESS)
678 return;
680 current = acpi_fill_vfct(device, vfct, current);
682 /* If no BIOS image, return with header->length == 0. */
683 if (!vfct->VBIOSImageOffset)
684 return;
686 /* (Re)calculate length and checksum. */
687 header->length = current - (unsigned long)vfct;
688 header->checksum = acpi_checksum((void *)vfct, header->length);
691 void acpi_create_ipmi(const struct device *device,
692 struct acpi_spmi *spmi,
693 const u16 ipmi_revision,
694 const acpi_addr_t *addr,
695 const enum acpi_ipmi_interface_type type,
696 const s8 gpe_interrupt,
697 const u32 apic_interrupt,
698 const u32 uid)
700 acpi_header_t *header = &(spmi->header);
701 memset((void *)spmi, 0, sizeof(struct acpi_spmi));
703 if (acpi_fill_header(header, "SPMI", SPMI, sizeof(struct acpi_spmi)) != CB_SUCCESS)
704 return;
706 spmi->reserved = 1;
708 if (device->path.type == DEVICE_PATH_PCI) {
709 spmi->pci_device_flag = ACPI_IPMI_PCI_DEVICE_FLAG;
710 spmi->pci_segment_group = device->upstream->segment_group;
711 spmi->pci_bus = device->upstream->secondary;
712 spmi->pci_device = device->path.pci.devfn >> 3;
713 spmi->pci_function = device->path.pci.devfn & 0x7;
714 } else if (type != IPMI_INTERFACE_SSIF) {
715 memcpy(spmi->uid, &uid, sizeof(spmi->uid));
718 spmi->base_address = *addr;
719 spmi->specification_revision = ipmi_revision;
721 spmi->interface_type = type;
723 if (gpe_interrupt >= 0 && gpe_interrupt < 32) {
724 spmi->gpe = gpe_interrupt;
725 spmi->interrupt_type |= ACPI_IPMI_INT_TYPE_SCI;
727 if (apic_interrupt > 0) {
728 spmi->global_system_interrupt = apic_interrupt;
729 spmi->interrupt_type |= ACPI_IPMI_INT_TYPE_APIC;
732 /* Calculate checksum. */
733 header->checksum = acpi_checksum((void *)spmi, header->length);
736 void acpi_create_ivrs(acpi_ivrs_t *ivrs,
737 unsigned long (*acpi_fill_ivrs)(acpi_ivrs_t *ivrs_struct,
738 unsigned long current))
740 acpi_header_t *header = &(ivrs->header);
741 unsigned long current = (unsigned long)ivrs + sizeof(acpi_ivrs_t);
743 memset((void *)ivrs, 0, sizeof(acpi_ivrs_t));
745 if (acpi_fill_header(header, "IVRS", IVRS, sizeof(acpi_ivrs_t)) != CB_SUCCESS)
746 return;
748 current = acpi_fill_ivrs(ivrs, current);
750 /* (Re)calculate length and checksum. */
751 header->length = current - (unsigned long)ivrs;
752 header->checksum = acpi_checksum((void *)ivrs, header->length);
755 void acpi_create_crat(struct acpi_crat_header *crat,
756 unsigned long (*acpi_fill_crat)(struct acpi_crat_header *crat_struct,
757 unsigned long current))
759 acpi_header_t *header = &(crat->header);
760 unsigned long current = (unsigned long)crat + sizeof(struct acpi_crat_header);
762 memset((void *)crat, 0, sizeof(struct acpi_crat_header));
764 if (acpi_fill_header(header, "CRAT", CRAT, sizeof(struct acpi_crat_header)) != CB_SUCCESS)
765 return;
767 current = acpi_fill_crat(crat, current);
769 /* (Re)calculate length and checksum. */
770 header->length = current - (unsigned long)crat;
771 header->checksum = acpi_checksum((void *)crat, header->length);
774 static void acpi_create_dbg2(acpi_dbg2_header_t *dbg2,
775 int port_type, int port_subtype,
776 acpi_addr_t *address, uint32_t address_size,
777 const char *device_path)
779 uintptr_t current;
780 acpi_dbg2_device_t *device;
781 uint32_t *dbg2_addr_size;
782 acpi_header_t *header;
783 size_t path_len;
784 const char *path;
785 char *namespace;
787 /* Fill out header fields. */
788 current = (uintptr_t)dbg2;
789 memset(dbg2, 0, sizeof(acpi_dbg2_header_t));
790 header = &(dbg2->header);
792 if (acpi_fill_header(header, "DBG2", DBG2, sizeof(acpi_dbg2_header_t)) != CB_SUCCESS)
793 return;
795 /* One debug device defined */
796 dbg2->devices_offset = sizeof(acpi_dbg2_header_t);
797 dbg2->devices_count = 1;
798 current += sizeof(acpi_dbg2_header_t);
800 /* Device comes after the header */
801 device = (acpi_dbg2_device_t *)current;
802 memset(device, 0, sizeof(acpi_dbg2_device_t));
803 current += sizeof(acpi_dbg2_device_t);
805 device->revision = 0;
806 device->address_count = 1;
807 device->port_type = port_type;
808 device->port_subtype = port_subtype;
810 /* Base Address comes after device structure */
811 memcpy((void *)current, address, sizeof(acpi_addr_t));
812 device->base_address_offset = current - (uintptr_t)device;
813 current += sizeof(acpi_addr_t);
815 /* Address Size comes after address structure */
816 dbg2_addr_size = (uint32_t *)current;
817 device->address_size_offset = current - (uintptr_t)device;
818 *dbg2_addr_size = address_size;
819 current += sizeof(uint32_t);
821 /* Namespace string comes last, use '.' if not provided */
822 path = device_path ? : ".";
823 /* Namespace string length includes NULL terminator */
824 path_len = strlen(path) + 1;
825 namespace = (char *)current;
826 device->namespace_string_length = path_len;
827 device->namespace_string_offset = current - (uintptr_t)device;
828 strncpy(namespace, path, path_len);
829 current += path_len;
831 /* Update structure lengths and checksum */
832 device->length = current - (uintptr_t)device;
833 header->length = current - (uintptr_t)dbg2;
834 header->checksum = acpi_checksum((uint8_t *)dbg2, header->length);
837 static unsigned long acpi_write_dbg2_uart(acpi_rsdp_t *rsdp, unsigned long current,
838 int space_id, uint64_t base, uint32_t size,
839 int access_size, const char *name)
841 acpi_dbg2_header_t *dbg2 = (acpi_dbg2_header_t *)current;
842 acpi_addr_t address;
844 memset(&address, 0, sizeof(address));
846 address.space_id = space_id;
847 address.addrl = (uint32_t)base;
848 address.addrh = (uint32_t)((base >> 32) & 0xffffffff);
849 address.access_size = access_size;
851 int subtype;
852 /* 16550-compatible with parameters defined in Generic Address Structure */
853 if (CONFIG(DRIVERS_UART_8250IO) || CONFIG(DRIVERS_UART_8250MEM))
854 subtype = ACPI_DBG2_PORT_SERIAL_16550;
855 else if (CONFIG(DRIVERS_UART_PL011))
856 subtype = ACPI_DBG2_PORT_SERIAL_ARM_PL011;
857 else
858 return current;
860 acpi_create_dbg2(dbg2,
861 ACPI_DBG2_PORT_SERIAL,
862 subtype,
863 &address, size,
864 name);
866 if (dbg2->header.length) {
867 current += dbg2->header.length;
868 current = acpi_align_current(current);
869 acpi_add_table(rsdp, dbg2);
872 return current;
875 unsigned long acpi_write_dbg2_pci_uart(acpi_rsdp_t *rsdp, unsigned long current,
876 const struct device *dev, uint8_t access_size)
878 struct resource *res;
880 if (!dev) {
881 printk(BIOS_DEBUG, "%s: Device not found\n", __func__);
882 return current;
884 if (!dev->enabled) {
885 printk(BIOS_INFO, "%s: Device not enabled\n", __func__);
886 return current;
888 res = probe_resource(dev, PCI_BASE_ADDRESS_0);
889 if (!res) {
890 printk(BIOS_ERR, "%s: Unable to find resource for %s\n",
891 __func__, dev_path(dev));
892 return current;
895 int space_id;
896 if (res->flags & IORESOURCE_IO)
897 space_id = ACPI_ADDRESS_SPACE_IO;
898 else if (res->flags & IORESOURCE_MEM)
899 space_id = ACPI_ADDRESS_SPACE_MEMORY;
900 else {
901 printk(BIOS_ERR, "%s: Unknown address space type\n", __func__);
902 return current;
905 return acpi_write_dbg2_uart(rsdp, current, space_id, res->base, res->size, access_size, acpi_device_path(dev));
908 unsigned long acpi_pl011_write_dbg2_uart(acpi_rsdp_t *rsdp, unsigned long current,
909 uint64_t base, const char *name)
911 return acpi_write_dbg2_uart(rsdp, current, ACPI_ADDRESS_SPACE_MEMORY, base,
912 sizeof(struct pl011_uart), ACPI_ACCESS_SIZE_DWORD_ACCESS,
913 name);
916 unsigned long acpi_16550_mmio32_write_dbg2_uart(acpi_rsdp_t *rsdp, unsigned long current,
917 uint64_t base, const char *name)
919 return acpi_write_dbg2_uart(rsdp, current, ACPI_ADDRESS_SPACE_MEMORY, base,
920 0x100, ACPI_ACCESS_SIZE_DWORD_ACCESS,
921 name);
924 static void acpi_create_facs(void *header)
926 acpi_facs_t *facs = header;
928 memcpy(facs->signature, "FACS", 4);
929 facs->length = sizeof(acpi_facs_t);
930 facs->hardware_signature = 0;
931 facs->firmware_waking_vector = 0;
932 facs->global_lock = 0;
933 facs->flags = 0;
934 facs->x_firmware_waking_vector_l = 0;
935 facs->x_firmware_waking_vector_h = 0;
936 facs->version = get_acpi_table_revision(FACS);
939 static void acpi_write_rsdt(acpi_rsdt_t *rsdt, char *oem_id, char *oem_table_id)
941 acpi_header_t *header = &(rsdt->header);
943 if (acpi_fill_header(header, "RSDT", RSDT, sizeof(acpi_rsdt_t)) != CB_SUCCESS)
944 return;
946 /* Entries are filled in later, we come with an empty set. */
948 /* Fix checksum. */
949 header->checksum = acpi_checksum((void *)rsdt, sizeof(acpi_rsdt_t));
952 static void acpi_write_xsdt(acpi_xsdt_t *xsdt, char *oem_id, char *oem_table_id)
954 acpi_header_t *header = &(xsdt->header);
956 if (acpi_fill_header(header, "XSDT", XSDT, sizeof(acpi_xsdt_t)) != CB_SUCCESS)
957 return;
959 /* Entries are filled in later, we come with an empty set. */
961 /* Fix checksum. */
962 header->checksum = acpi_checksum((void *)xsdt, sizeof(acpi_xsdt_t));
965 static void acpi_write_rsdp(acpi_rsdp_t *rsdp, acpi_rsdt_t *rsdt,
966 acpi_xsdt_t *xsdt, char *oem_id)
968 memset(rsdp, 0, sizeof(acpi_rsdp_t));
970 memcpy(rsdp->signature, RSDP_SIG, 8);
971 memcpy(rsdp->oem_id, oem_id, 6);
973 rsdp->length = sizeof(acpi_rsdp_t);
974 rsdp->rsdt_address = (uintptr_t)rsdt;
977 * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2.
979 * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
980 * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
981 * revision 0).
983 if (xsdt == NULL) {
984 rsdp->revision = 0;
985 } else {
986 rsdp->xsdt_address = (u64)(uintptr_t)xsdt;
987 rsdp->revision = get_acpi_table_revision(RSDP);
990 /* Calculate checksums. */
991 rsdp->checksum = acpi_checksum((void *)rsdp, 20);
992 rsdp->ext_checksum = acpi_checksum((void *)rsdp, sizeof(acpi_rsdp_t));
995 unsigned long acpi_create_hest_error_source(acpi_hest_t *hest,
996 acpi_hest_esd_t *esd, u16 type, void *data, u16 data_len)
998 acpi_header_t *header = &(hest->header);
999 acpi_hest_hen_t *hen;
1000 void *pos;
1001 u16 len;
1003 pos = esd;
1004 memset(pos, 0, sizeof(acpi_hest_esd_t));
1005 len = 0;
1006 esd->type = type; /* MCE */
1007 esd->source_id = hest->error_source_count;
1008 esd->flags = 0; /* FIRMWARE_FIRST */
1009 esd->enabled = 1;
1010 esd->prealloc_erecords = 1;
1011 esd->max_section_per_record = 0x1;
1013 len += sizeof(acpi_hest_esd_t);
1014 pos = esd + 1;
1016 switch (type) {
1017 case 0: /* MCE */
1018 break;
1019 case 1: /* CMC */
1020 hen = (acpi_hest_hen_t *)(pos);
1021 memset(pos, 0, sizeof(acpi_hest_hen_t));
1022 hen->type = 3; /* SCI? */
1023 hen->length = sizeof(acpi_hest_hen_t);
1024 hen->conf_we = 0; /* Configuration Write Enable. */
1025 hen->poll_interval = 0;
1026 hen->vector = 0;
1027 hen->sw2poll_threshold_val = 0;
1028 hen->sw2poll_threshold_win = 0;
1029 hen->error_threshold_val = 0;
1030 hen->error_threshold_win = 0;
1031 len += sizeof(acpi_hest_hen_t);
1032 pos = hen + 1;
1033 break;
1034 case 2: /* NMI */
1035 case 6: /* AER Root Port */
1036 case 7: /* AER Endpoint */
1037 case 8: /* AER Bridge */
1038 case 9: /* Generic Hardware Error Source. */
1039 /* TODO: */
1040 break;
1041 default:
1042 printk(BIOS_DEBUG, "Invalid type of Error Source.");
1043 break;
1045 hest->error_source_count++;
1047 memcpy(pos, data, data_len);
1048 len += data_len;
1049 if (header)
1050 header->length += len;
1052 return len;
1055 /* ACPI 4.0 */
1056 void acpi_write_hest(acpi_hest_t *hest,
1057 unsigned long (*acpi_fill_hest)(acpi_hest_t *hest))
1059 acpi_header_t *header = &(hest->header);
1061 memset(hest, 0, sizeof(acpi_hest_t));
1063 if (acpi_fill_header(header, "HEST", HEST, sizeof(acpi_hest_t)) != CB_SUCCESS)
1064 return;
1066 acpi_fill_hest(hest);
1068 /* Calculate checksums. */
1069 header->checksum = acpi_checksum((void *)hest, header->length);
1072 /* ACPI 3.0b */
1073 static void acpi_create_bert(acpi_header_t *header, void *unused)
1075 if (!CONFIG(ACPI_BERT))
1076 return;
1078 acpi_bert_t *bert = (acpi_bert_t *)header;
1080 void *region;
1081 size_t size;
1082 if (acpi_soc_get_bert_region(&region, &size) != CB_SUCCESS)
1083 return;
1085 if (acpi_fill_header(header, "BERT", BERT, sizeof(acpi_bert_t)) != CB_SUCCESS)
1086 return;
1088 bert->error_region = (uintptr_t)region;
1089 bert->region_length = (size_t)size;
1092 __weak void arch_fill_fadt(acpi_fadt_t *fadt) { }
1093 __weak void soc_fill_fadt(acpi_fadt_t *fadt) { }
1094 __weak void mainboard_fill_fadt(acpi_fadt_t *fadt) { }
1096 static acpi_header_t *dsdt;
1097 static void acpi_create_fadt(acpi_header_t *header, void *arg1)
1099 acpi_fadt_t *fadt = (acpi_fadt_t *)header;
1100 acpi_facs_t *facs = (acpi_facs_t *)(*(acpi_facs_t **)arg1);
1102 if (acpi_fill_header(header, "FACP", FADT, sizeof(acpi_fadt_t)) != CB_SUCCESS)
1103 return;
1105 fadt->FADT_MinorVersion = get_acpi_fadt_minor_version();
1106 if ((uintptr_t)facs <= UINT32_MAX)
1107 fadt->firmware_ctrl = (uintptr_t)facs;
1108 else
1109 fadt->x_firmware_ctl_h = (uint32_t)((uint64_t)(uintptr_t)facs >> 32);
1110 fadt->x_firmware_ctl_l = (uint32_t)(uintptr_t)facs;
1112 if ((uintptr_t)dsdt <= UINT32_MAX)
1113 fadt->dsdt = (uintptr_t)dsdt;
1114 else
1115 fadt->x_dsdt_h = (uint32_t)((uint64_t)(uintptr_t)dsdt >> 32);
1116 fadt->x_dsdt_l = (uint32_t)(uintptr_t)dsdt;
1118 /* should be 0 ACPI 3.0 */
1119 fadt->reserved = 0;
1121 /* P_LVLx latencies are not used as CPU _CST will override them. */
1122 fadt->p_lvl2_lat = ACPI_FADT_C2_NOT_SUPPORTED;
1123 fadt->p_lvl3_lat = ACPI_FADT_C3_NOT_SUPPORTED;
1125 /* Use CPU _PTC instead to provide P_CNT details. */
1126 fadt->duty_offset = 0;
1127 fadt->duty_width = 0;
1129 fadt->preferred_pm_profile = acpi_get_preferred_pm_profile();
1131 arch_fill_fadt(fadt);
1133 acpi_fill_fadt(fadt);
1135 soc_fill_fadt(fadt);
1136 mainboard_fill_fadt(fadt);
1139 static void acpi_create_lpit(acpi_header_t *header, void *unused)
1141 if (!CONFIG(ACPI_LPIT))
1142 return;
1144 acpi_lpit_t *lpit = (acpi_lpit_t *)header;
1145 unsigned long current = (unsigned long)lpit + sizeof(acpi_lpit_t);
1147 if (acpi_fill_header(header, "LPIT", LPIT, sizeof(acpi_lpit_t)) != CB_SUCCESS)
1148 return;
1150 current = acpi_fill_lpit(current);
1152 /* (Re)calculate length. */
1153 header->length = current - (unsigned long)lpit;
1156 static void acpi_create_gtdt(acpi_header_t *header, void *unused)
1158 if (!CONFIG(ACPI_GTDT))
1159 return;
1161 acpi_gtdt_t *gtdt = (acpi_gtdt_t *)header;
1162 unsigned long current = (unsigned long)gtdt + sizeof(acpi_gtdt_t);
1164 if (acpi_fill_header(header, "GTDT", GTDT, sizeof(acpi_gtdt_t)) != CB_SUCCESS)
1165 return;
1167 /* Fill out header fields. */
1168 gtdt->platform_timer_offset = sizeof(acpi_gtdt_t);
1170 acpi_soc_fill_gtdt(gtdt);
1171 current = acpi_soc_gtdt_add_timers(&gtdt->platform_timer_count, current);
1173 /* (Re)calculate length. */
1174 header->length = current - (unsigned long)gtdt;
1177 unsigned long acpi_gtdt_add_timer_block(unsigned long current, const uint64_t address,
1178 struct acpi_gtdt_timer_entry *timers, size_t number)
1180 struct acpi_gtdt_timer_block *block = (struct acpi_gtdt_timer_block *)current;
1181 memset(block, 0, sizeof(struct acpi_gtdt_timer_block));
1183 assert(number < 8 && number != 0);
1184 const size_t entries_size = number * sizeof(struct acpi_gtdt_timer_entry);
1186 block->header.type = ACPI_GTDT_TYPE_TIMER_BLOCK;
1187 block->header.length = sizeof(struct acpi_gtdt_timer_block)
1188 + entries_size;
1189 block->block_address = address;
1190 block->timer_count = number;
1191 block->timer_offset = sizeof(struct acpi_gtdt_timer_block);
1192 current += sizeof(struct acpi_gtdt_timer_block);
1193 memcpy((void *)current, timers, entries_size);
1194 current += entries_size;
1195 return current;
1198 unsigned long acpi_gtdt_add_watchdog(unsigned long current, uint64_t refresh_frame,
1199 uint64_t control_frame, uint32_t gsiv, uint32_t flags)
1201 struct acpi_gtdt_watchdog *wd = (struct acpi_gtdt_watchdog *)current;
1202 memset(wd, 0, sizeof(struct acpi_gtdt_watchdog));
1204 wd->header.type = ACPI_GTDT_TYPE_WATCHDOG;
1205 wd->header.length = sizeof(struct acpi_gtdt_watchdog);
1206 wd->refresh_frame_address = refresh_frame;
1207 wd->control_frame_address = control_frame;
1208 wd->timer_interrupt = gsiv;
1209 wd->timer_flags = flags;
1211 return current + sizeof(struct acpi_gtdt_watchdog);
1214 static void acpi_create_iort(acpi_header_t *header, void *unused)
1216 if (!CONFIG(ACPI_IORT))
1217 return;
1219 acpi_iort_t *iort = (acpi_iort_t *)header;
1220 unsigned long current = (unsigned long)iort + sizeof(acpi_iort_t);
1222 if (acpi_fill_header(header, "IORT", IORT, sizeof(acpi_iort_t)) != CB_SUCCESS)
1223 return;
1225 iort->node_count = 0;
1226 iort->node_offset = current - (unsigned long)iort;
1228 current = acpi_soc_fill_iort(iort, current);
1230 /* (Re)calculate length */
1231 header->length = current - (unsigned long)iort;
1234 static void acpi_create_wdat(acpi_header_t *header, void *unused)
1236 if (!CONFIG(ACPI_WDAT_WDT))
1237 return;
1239 acpi_wdat_t *wdat = (acpi_wdat_t *)header;
1240 unsigned long current = (unsigned long)wdat + sizeof(acpi_wdat_t);
1242 memset((void *)wdat, 0, sizeof(acpi_wdat_t));
1244 if (acpi_fill_header(header, "WDAT", WDAT, sizeof(acpi_wdat_t)) != CB_SUCCESS)
1245 return;
1247 current = acpi_soc_fill_wdat(wdat, current);
1249 /* (Re)calculate length. */
1250 header->length = current - (unsigned long)wdat;
1253 unsigned long acpi_create_lpi_desc_ncst(acpi_lpi_desc_ncst_t *lpi_desc, uint16_t uid)
1255 memset(lpi_desc, 0, sizeof(acpi_lpi_desc_ncst_t));
1256 lpi_desc->header.length = sizeof(acpi_lpi_desc_ncst_t);
1257 lpi_desc->header.type = ACPI_LPI_DESC_TYPE_NATIVE_CSTATE;
1258 lpi_desc->header.uid = uid;
1260 return lpi_desc->header.length;
1263 static void acpi_create_pptt(acpi_header_t *header, void *unused)
1265 if (!CONFIG(ACPI_PPTT))
1266 return;
1268 if (acpi_fill_header(header, "PPTT", PPTT, sizeof(acpi_pptt_t)) != CB_SUCCESS)
1269 return;
1271 acpi_pptt_t *pptt = (acpi_pptt_t *)header;
1272 acpi_create_pptt_body(pptt);
1275 static uint8_t acpi_spcr_type(void)
1277 /* 16550-compatible with parameters defined in Generic Address Structure */
1278 if (CONFIG(DRIVERS_UART_8250IO) || CONFIG(DRIVERS_UART_8250MEM))
1279 return 0x12;
1280 if (CONFIG(DRIVERS_UART_PL011))
1281 return 0x3;
1283 printk(BIOS_ERR, "%s: unknown serial type\n", __func__);
1284 return 0xff;
1287 static void acpi_create_spcr(acpi_header_t *header, void *unused)
1289 acpi_spcr_t *spcr = (acpi_spcr_t *)header;
1290 struct lb_serial serial;
1292 if (!CONFIG(CONSOLE_SERIAL))
1293 return;
1295 if (fill_lb_serial(&serial) != CB_SUCCESS)
1296 return;
1298 if (acpi_fill_header(header, "SPCR", SPCR, sizeof(acpi_spcr_t)) != CB_SUCCESS)
1299 return;
1301 spcr->interface_type = acpi_spcr_type();
1302 assert(serial.type == LB_SERIAL_TYPE_IO_MAPPED
1303 || serial.type == LB_SERIAL_TYPE_MEMORY_MAPPED);
1304 spcr->base_address.space_id = serial.type == LB_SERIAL_TYPE_IO_MAPPED ?
1305 ACPI_ADDRESS_SPACE_IO : ACPI_ADDRESS_SPACE_MEMORY;
1306 spcr->base_address.bit_width = serial.regwidth * 8;
1307 spcr->base_address.bit_offset = 0;
1308 switch (serial.regwidth) {
1309 case 1:
1310 spcr->base_address.access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS;
1311 break;
1312 case 2:
1313 spcr->base_address.access_size = ACPI_ACCESS_SIZE_WORD_ACCESS;
1314 break;
1315 case 4:
1316 spcr->base_address.access_size = ACPI_ACCESS_SIZE_DWORD_ACCESS;
1317 break;
1318 default:
1319 printk(BIOS_ERR, "%s, Invalid serial regwidth\n", __func__);
1322 spcr->base_address.addrl = serial.baseaddr;
1323 spcr->base_address.addrh = 0;
1324 spcr->interrupt_type = 0;
1325 spcr->irq = 0;
1326 spcr->configured_baudrate = 0; /* Have the OS use whatever is currently set */
1327 spcr->parity = 0;
1328 spcr->stop_bits = 1;
1329 spcr->flow_control = 0;
1330 spcr->terminal_type = 2; /* 2 = VT-UTF8 */
1331 spcr->language = 0;
1332 spcr->pci_did = 0xffff;
1333 spcr->pci_vid = 0xffff;
1335 header->checksum = acpi_checksum((void *)spcr, header->length);
1338 unsigned long __weak fw_cfg_acpi_tables(unsigned long start)
1340 return 0;
1343 void preload_acpi_dsdt(void)
1345 const char *file = CONFIG_CBFS_PREFIX "/dsdt.aml";
1347 if (!CONFIG(CBFS_PRELOAD))
1348 return;
1350 printk(BIOS_DEBUG, "Preloading %s\n", file);
1351 cbfs_preload(file);
1354 static void acpi_create_dsdt(acpi_header_t *header, void *dsdt_file_arg)
1356 dsdt = header;
1357 acpi_header_t *dsdt_file = *(acpi_header_t **)dsdt_file_arg;
1358 unsigned long current = (unsigned long)header;
1360 dsdt = (acpi_header_t *)current;
1361 memcpy(dsdt, dsdt_file, sizeof(acpi_header_t));
1362 if (dsdt->length >= sizeof(acpi_header_t)) {
1363 current += sizeof(acpi_header_t);
1365 acpigen_set_current((char *)current);
1367 if (CONFIG(ACPI_SOC_NVS))
1368 acpi_fill_gnvs();
1369 if (CONFIG(CHROMEOS_NVS))
1370 acpi_fill_cnvs();
1372 current = (unsigned long)acpigen_get_current();
1373 memcpy((char *)current,
1374 (char *)dsdt_file + sizeof(acpi_header_t),
1375 dsdt->length - sizeof(acpi_header_t));
1376 current += dsdt->length - sizeof(acpi_header_t);
1378 /* (Re)calculate length. */
1379 dsdt->length = current - (unsigned long)dsdt;
1383 static void acpi_create_slic(acpi_header_t *header, void *slic_file_arg)
1385 acpi_header_t *slic_file = *(acpi_header_t **)slic_file_arg;
1386 acpi_header_t *slic = header;
1387 if (slic_file)
1388 memcpy(slic, slic_file, slic_file->length);
1391 static uintptr_t coreboot_rsdp;
1393 uintptr_t get_coreboot_rsdp(void)
1395 return coreboot_rsdp;
1398 static void acpixtract_compatible_hexdump(const void *memory, size_t length)
1400 size_t i, j;
1401 uint8_t *line;
1402 size_t num_bytes;
1404 for (i = 0; i < length; i += 16) {
1405 num_bytes = MIN(length - i, 16);
1406 line = ((uint8_t *)memory) + i;
1408 printk(BIOS_SPEW, " %04zX:", i);
1409 for (j = 0; j < num_bytes; j++)
1410 printk(BIOS_SPEW, " %02x", line[j]);
1411 for (; j < 16; j++)
1412 printk(BIOS_SPEW, " ");
1413 printk(BIOS_SPEW, " ");
1414 for (j = 0; j < num_bytes; j++)
1415 printk(BIOS_SPEW, "%c",
1416 isprint(line[j]) ? line[j] : '.');
1417 printk(BIOS_SPEW, "\n");
1421 static void acpidump_print(void *table_ptr)
1423 if (table_ptr == NULL)
1424 return;
1425 const acpi_header_t *header = (acpi_header_t *)table_ptr;
1426 const size_t table_size = header->length;
1427 printk(BIOS_SPEW, "%.4s @ 0x0000000000000000\n", header->signature);
1428 acpixtract_compatible_hexdump(table_ptr, table_size);
1429 printk(BIOS_SPEW, "\n");
1432 unsigned long write_acpi_tables(const unsigned long start)
1434 unsigned long current;
1435 acpi_rsdp_t *rsdp;
1436 acpi_rsdt_t *rsdt = NULL;
1437 acpi_xsdt_t *xsdt = NULL;
1438 acpi_facs_t *facs = NULL;
1439 acpi_header_t *slic_file;
1440 acpi_header_t *ssdt = NULL;
1441 acpi_header_t *dsdt_file;
1442 struct device *dev;
1443 unsigned long fw;
1444 size_t slic_size, dsdt_size;
1445 char oem_id[6], oem_table_id[8];
1447 const struct acpi_table_generator {
1448 void (*create_table)(acpi_header_t *table, void *arg);
1449 void *args;
1450 size_t min_size;
1451 } tables[] = {
1452 { acpi_create_dsdt, &dsdt_file, sizeof(acpi_header_t) },
1453 { acpi_create_fadt, &facs, sizeof(acpi_fadt_t) },
1454 { acpi_create_slic, &slic_file, sizeof(acpi_header_t) },
1455 { acpi_create_ssdt_generator, NULL, sizeof(acpi_header_t) },
1456 { acpi_create_mcfg, NULL, sizeof(acpi_mcfg_t) },
1457 { acpi_create_tcpa, NULL, sizeof(acpi_tcpa_t) },
1458 { acpi_create_tpm2, NULL, sizeof(acpi_tpm2_t) },
1459 { acpi_create_lpit, NULL, sizeof(acpi_lpit_t) },
1460 { acpi_create_madt, NULL, sizeof(acpi_header_t) },
1461 { acpi_create_bert, NULL, sizeof(acpi_bert_t) },
1462 { acpi_create_spcr, NULL, sizeof(acpi_spcr_t) },
1463 { acpi_create_gtdt, NULL, sizeof(acpi_gtdt_t) },
1464 { acpi_create_pptt, NULL, sizeof(acpi_pptt_t) },
1465 { acpi_create_iort, NULL, sizeof(acpi_iort_t) },
1466 { acpi_create_wdat, NULL, sizeof(acpi_wdat_t) },
1469 current = start;
1471 /* Align ACPI tables to 16byte */
1472 current = acpi_align_current(current);
1474 /* Special case for qemu */
1475 fw = fw_cfg_acpi_tables(current);
1476 if (fw) {
1477 rsdp = NULL;
1478 /* Find RSDP. */
1479 for (void *p = (void *)current; p < (void *)fw; p += 16) {
1480 if (valid_rsdp((acpi_rsdp_t *)p)) {
1481 rsdp = p;
1482 coreboot_rsdp = (uintptr_t)rsdp;
1483 break;
1486 if (!rsdp)
1487 return fw;
1489 current = fw;
1490 current = acpi_align_current(current);
1491 if (rsdp->xsdt_address == 0) {
1492 acpi_rsdt_t *existing_rsdt = (acpi_rsdt_t *)(uintptr_t)rsdp->rsdt_address;
1495 * Qemu only provides a smaller ACPI 1.0 RSDP, thus
1496 * allocate a bigger ACPI 2.0 RSDP structure.
1498 rsdp = (acpi_rsdp_t *)current;
1499 current += sizeof(acpi_rsdp_t);
1500 coreboot_rsdp = (uintptr_t)rsdp;
1502 xsdt = (acpi_xsdt_t *)current;
1503 current += sizeof(acpi_xsdt_t);
1504 current = acpi_align_current(current);
1507 * Qemu only creates an RSDT.
1508 * Add an XSDT based on the existing RSDT entries.
1510 acpi_write_rsdp(rsdp, existing_rsdt, xsdt, oem_id);
1511 acpi_write_xsdt(xsdt, oem_id, oem_table_id);
1513 * Copy existing entries to the new XSDT. This will override existing
1514 * RSDT entries with the same value.
1516 for (int i = 0; existing_rsdt->entry[i]; i++)
1517 acpi_add_table(rsdp, (void *)(uintptr_t)existing_rsdt->entry[i]);
1520 /* Add BOOT0000 for Linux google firmware driver */
1521 printk(BIOS_DEBUG, "ACPI: * SSDT\n");
1522 ssdt = (acpi_header_t *)current;
1523 current += sizeof(acpi_header_t);
1525 memset((void *)ssdt, 0, sizeof(acpi_header_t));
1527 memcpy(&ssdt->signature, "SSDT", 4);
1528 ssdt->revision = get_acpi_table_revision(SSDT);
1529 memcpy(&ssdt->oem_id, OEM_ID, 6);
1530 memcpy(&ssdt->oem_table_id, oem_table_id, 8);
1531 ssdt->oem_revision = 42;
1532 memcpy(&ssdt->asl_compiler_id, ASLC, 4);
1533 ssdt->asl_compiler_revision = asl_revision;
1534 ssdt->length = sizeof(acpi_header_t);
1536 acpigen_set_current((char *)current);
1538 /* Write object to declare coreboot tables */
1539 acpi_ssdt_write_cbtable();
1541 /* (Re)calculate length and checksum. */
1542 ssdt->length = current - (unsigned long)ssdt;
1543 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
1545 acpi_create_ssdt_generator(ssdt, NULL);
1547 acpi_add_table(rsdp, ssdt);
1549 return current;
1552 dsdt_file = cbfs_map(CONFIG_CBFS_PREFIX "/dsdt.aml", &dsdt_size);
1553 if (!dsdt_file) {
1554 printk(BIOS_ERR, "No DSDT file, skipping ACPI tables\n");
1555 return start;
1558 if (dsdt_file->length > dsdt_size
1559 || dsdt_file->length < sizeof(acpi_header_t)
1560 || memcmp(dsdt_file->signature, "DSDT", 4) != 0) {
1561 printk(BIOS_ERR, "Invalid DSDT file, skipping ACPI tables\n");
1562 cbfs_unmap(dsdt_file);
1563 return start;
1566 slic_file = cbfs_map(CONFIG_CBFS_PREFIX "/slic", &slic_size);
1567 if (slic_file
1568 && (slic_file->length > slic_size
1569 || slic_file->length < sizeof(acpi_header_t)
1570 || (memcmp(slic_file->signature, "SLIC", 4) != 0
1571 && memcmp(slic_file->signature, "MSDM", 4) != 0))) {
1572 cbfs_unmap(slic_file);
1573 slic_file = 0;
1576 if (slic_file) {
1577 memcpy(oem_id, slic_file->oem_id, 6);
1578 memcpy(oem_table_id, slic_file->oem_table_id, 8);
1579 } else {
1580 memcpy(oem_id, OEM_ID, 6);
1581 memcpy(oem_table_id, ACPI_TABLE_CREATOR, 8);
1584 printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx.\n", start);
1586 /* We need at least an RSDP, RSDT for ACPI 1.0 compat, otherwise XSDT */
1587 rsdp = (acpi_rsdp_t *)current;
1588 coreboot_rsdp = (uintptr_t)rsdp;
1589 current += sizeof(acpi_rsdp_t);
1590 current = acpi_align_current(current);
1591 if (current + sizeof(acpi_rsdt_t) - 1 <= UINT32_MAX) {
1592 rsdt = (acpi_rsdt_t *)current;
1593 current += sizeof(acpi_rsdt_t);
1594 current = acpi_align_current(current);
1595 } else {
1596 printk(BIOS_INFO, "Not adding RSDT because tables reside above 4G.");
1599 xsdt = (acpi_xsdt_t *)current;
1600 current += sizeof(acpi_xsdt_t);
1601 current = acpi_align_current(current);
1603 /* clear all table memory */
1604 memset((void *)start, 0, current - start);
1606 acpi_write_rsdp(rsdp, rsdt, xsdt, oem_id);
1607 acpi_write_rsdt(rsdt, oem_id, oem_table_id);
1608 acpi_write_xsdt(xsdt, oem_id, oem_table_id);
1610 if (ENV_X86) {
1611 printk(BIOS_DEBUG, "ACPI: * FACS\n");
1612 current = ALIGN_UP(current, 64);
1613 facs = (acpi_facs_t *)current;
1614 current += sizeof(acpi_facs_t);
1615 current = acpi_align_current(current);
1616 acpi_create_facs(facs);
1619 for (size_t i = 0; i < ARRAY_SIZE(tables); i++) {
1620 acpi_header_t *header = (acpi_header_t *)current;
1621 memset(header, 0, tables[i].min_size);
1622 tables[i].create_table(header, tables[i].args);
1623 if (header->length < tables[i].min_size)
1624 continue;
1625 header->checksum = 0;
1626 header->checksum = acpi_checksum((void *)header, header->length);
1627 current += header->length;
1628 current = acpi_align_current(current);
1630 if (tables[i].create_table == acpi_create_dsdt)
1631 continue;
1633 printk(BIOS_DEBUG, "ACPI: * %.4s\n", header->signature);
1634 acpi_add_table(rsdp, header);
1638 * cbfs_unmap() uses mem_pool_free() which works correctly only
1639 * if freeing is done in reverse order than memory allocation.
1640 * This is why unmapping of dsdt_file must be done after
1641 * unmapping slic file.
1643 cbfs_unmap(slic_file);
1644 cbfs_unmap(dsdt_file);
1646 printk(BIOS_DEBUG, "current = %lx\n", current);
1648 for (dev = all_devices; dev; dev = dev->next) {
1649 if (dev->ops && dev->ops->write_acpi_tables) {
1650 current = dev->ops->write_acpi_tables(dev, current,
1651 rsdp);
1652 current = acpi_align_current(current);
1656 printk(BIOS_INFO, "ACPI: done.\n");
1658 if (CONFIG(DEBUG_ACPICA_COMPATIBLE)) {
1659 printk(BIOS_DEBUG, "Printing ACPI tables in ACPICA compatible format\n");
1660 if (facs)
1661 acpidump_print(facs);
1662 acpidump_print(dsdt);
1663 for (size_t i = 0; xsdt->entry[i] != 0; i++) {
1664 acpidump_print((void *)(uintptr_t)xsdt->entry[i]);
1666 printk(BIOS_DEBUG, "Done printing ACPI tables in ACPICA compatible format\n");
1669 return current;
1672 static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp)
1674 if (strncmp((char *)rsdp, RSDP_SIG, sizeof(RSDP_SIG) - 1) != 0)
1675 return NULL;
1677 printk(BIOS_DEBUG, "Looking on %p for valid checksum\n", rsdp);
1679 if (acpi_checksum((void *)rsdp, 20) != 0)
1680 return NULL;
1681 printk(BIOS_DEBUG, "Checksum 1 passed\n");
1683 if ((rsdp->revision > 1) && (acpi_checksum((void *)rsdp,
1684 rsdp->length) != 0))
1685 return NULL;
1686 printk(BIOS_DEBUG, "Checksum 2 passed all OK\n");
1688 return rsdp;
1691 void *acpi_find_wakeup_vector(void)
1693 char *p, *end;
1694 acpi_xsdt_t *xsdt;
1695 acpi_facs_t *facs;
1696 acpi_fadt_t *fadt = NULL;
1697 acpi_rsdp_t *rsdp = NULL;
1698 void *wake_vec;
1699 int i;
1701 if (!acpi_is_wakeup_s3())
1702 return NULL;
1704 printk(BIOS_DEBUG, "Trying to find the wakeup vector...\n");
1706 /* Find RSDP. */
1707 for (p = (char *)0xe0000; p < (char *)0xfffff; p += 16) {
1708 rsdp = valid_rsdp((acpi_rsdp_t *)p);
1709 if (rsdp)
1710 break;
1713 if (rsdp == NULL) {
1714 printk(BIOS_ALERT,
1715 "No RSDP found, wake up from S3 not possible.\n");
1716 return NULL;
1719 printk(BIOS_DEBUG, "RSDP found at %p\n", rsdp);
1720 xsdt = (acpi_xsdt_t *)(uintptr_t)rsdp->xsdt_address;
1722 end = (char *)xsdt + xsdt->header.length;
1723 printk(BIOS_DEBUG, "XSDT found at %p ends at %p\n", xsdt, end);
1725 for (i = 0; ((char *)&xsdt->entry[i]) < end; i++) {
1726 fadt = (acpi_fadt_t *)(uintptr_t)xsdt->entry[i];
1727 if (strncmp((char *)fadt, "FACP", 4) == 0)
1728 break;
1729 fadt = NULL;
1732 if (fadt == NULL) {
1733 printk(BIOS_ALERT,
1734 "No FADT found, wake up from S3 not possible.\n");
1735 return NULL;
1738 printk(BIOS_DEBUG, "FADT found at %p\n", fadt);
1739 facs = (acpi_facs_t *)(uintptr_t)((uint64_t)fadt->x_firmware_ctl_l
1740 | (uint64_t)fadt->x_firmware_ctl_h << 32);
1742 if (facs == NULL) {
1743 printk(BIOS_ALERT,
1744 "No FACS found, wake up from S3 not possible.\n");
1745 return NULL;
1748 printk(BIOS_DEBUG, "FACS found at %p\n", facs);
1749 wake_vec = (void *)(uintptr_t)facs->firmware_waking_vector;
1750 printk(BIOS_DEBUG, "OS waking vector is %p\n", wake_vec);
1752 return wake_vec;
1755 __weak int acpi_get_gpe(int gpe)
1757 return -1; /* implemented by SOC */
1760 u8 get_acpi_fadt_minor_version(void)
1762 return ACPI_FADT_MINOR_VERSION_0;
1765 int get_acpi_table_revision(enum acpi_tables table)
1767 switch (table) {
1768 case FADT:
1769 return ACPI_FADT_REV_ACPI_6;
1770 case MADT: /* ACPI 3.0: 2, ACPI 4.0/5.0: 3, ACPI 6.2b/6.3: 5 */
1771 return 3;
1772 case MCFG:
1773 return 1;
1774 case TCPA:
1775 return 2;
1776 case TPM2:
1777 return 4;
1778 case SSDT: /* ACPI 3.0 up to 6.3: 2 */
1779 return 2;
1780 case SRAT: /* ACPI 2.0: 1, ACPI 3.0: 2, ACPI 4.0 up to 6.4: 3 */
1781 return 3;
1782 case HMAT: /* ACPI 6.4: 2 */
1783 return 2;
1784 case DMAR:
1785 return 1;
1786 case SLIT: /* ACPI 2.0 up to 6.3: 1 */
1787 return 1;
1788 case SPMI: /* IMPI 2.0 */
1789 return 5;
1790 case HPET: /* Currently 1. Table added in ACPI 2.0. */
1791 return 1;
1792 case VFCT: /* ACPI 2.0/3.0/4.0: 1 */
1793 return 1;
1794 case IVRS:
1795 return IVRS_FORMAT_MIXED;
1796 case DBG2:
1797 return 0;
1798 case FACS: /* ACPI 2.0/3.0: 1, ACPI 4.0 up to 6.3: 2 */
1799 return 1;
1800 case RSDT: /* ACPI 1.0 up to 6.3: 1 */
1801 return 1;
1802 case XSDT: /* ACPI 2.0 up to 6.3: 1 */
1803 return 1;
1804 case RSDP: /* ACPI 2.0 up to 6.3: 2 */
1805 return 2;
1806 case EINJ:
1807 return 1;
1808 case HEST:
1809 return 1;
1810 case NHLT:
1811 return 5;
1812 case BERT:
1813 return 1;
1814 case CEDT: /* CXL 3.0 section 9.17.1 */
1815 return 1;
1816 case CRAT:
1817 return 1;
1818 case LPIT: /* ACPI 5.1 up to 6.3: 0 */
1819 return 0;
1820 case SPCR:
1821 return 4;
1822 case GTDT:
1823 return 3;
1824 case PPTT: /* ACPI 6.4 */
1825 return 3;
1826 case IORT: /* IO Remapping Table E.e */
1827 return 6;
1828 case WDAT:
1829 return 1;
1830 default:
1831 return -1;
1833 return -1;