soc/intel/alderlake: Add ADL-P 4+4 with 28W TDP
[coreboot.git] / src / acpi / acpi.c
blob2706bf7f15209508c7981fbff2ad8b1708d63d53
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_ivrs.h>
18 #include <acpi/acpigen.h>
19 #include <arch/hpet.h>
20 #include <cbfs.h>
21 #include <cbmem.h>
22 #include <commonlib/helpers.h>
23 #include <commonlib/sort.h>
24 #include <console/console.h>
25 #include <cpu/cpu.h>
26 #include <device/mmio.h>
27 #include <device/pci.h>
28 #include <pc80/mc146818rtc.h>
29 #include <string.h>
30 #include <types.h>
31 #include <version.h>
33 #if ENV_X86
34 #include <arch/ioapic.h>
35 #endif
37 static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp);
39 u8 acpi_checksum(u8 *table, u32 length)
41 u8 ret = 0;
42 while (length--) {
43 ret += *table;
44 table++;
46 return -ret;
49 /**
50 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
51 * and checksum.
53 void acpi_add_table(acpi_rsdp_t *rsdp, void *table)
55 int i, entries_num;
56 acpi_rsdt_t *rsdt;
57 acpi_xsdt_t *xsdt = NULL;
59 /* The RSDT is mandatory... */
60 rsdt = (acpi_rsdt_t *)(uintptr_t)rsdp->rsdt_address;
62 /* ...while the XSDT is not. */
63 if (rsdp->xsdt_address)
64 xsdt = (acpi_xsdt_t *)((uintptr_t)rsdp->xsdt_address);
66 /* This should always be MAX_ACPI_TABLES. */
67 entries_num = ARRAY_SIZE(rsdt->entry);
69 for (i = 0; i < entries_num; i++) {
70 if (rsdt->entry[i] == 0)
71 break;
74 if (i >= entries_num) {
75 printk(BIOS_ERR, "ACPI: Error: Could not add ACPI table, "
76 "too many tables.\n");
77 return;
80 /* Add table to the RSDT. */
81 rsdt->entry[i] = (uintptr_t)table;
83 /* Fix RSDT length or the kernel will assume invalid entries. */
84 rsdt->header.length = sizeof(acpi_header_t) + (sizeof(u32) * (i + 1));
86 /* Re-calculate checksum. */
87 rsdt->header.checksum = 0; /* Hope this won't get optimized away */
88 rsdt->header.checksum = acpi_checksum((u8 *)rsdt, rsdt->header.length);
91 * And now the same thing for the XSDT. We use the same index as for
92 * now we want the XSDT and RSDT to always be in sync in coreboot.
94 if (xsdt) {
95 /* Add table to the XSDT. */
96 xsdt->entry[i] = (u64)(uintptr_t)table;
98 /* Fix XSDT length. */
99 xsdt->header.length = sizeof(acpi_header_t) +
100 (sizeof(u64) * (i + 1));
102 /* Re-calculate checksum. */
103 xsdt->header.checksum = 0;
104 xsdt->header.checksum = acpi_checksum((u8 *)xsdt,
105 xsdt->header.length);
108 printk(BIOS_DEBUG, "ACPI: added table %d/%d, length now %d\n",
109 i + 1, entries_num, rsdt->header.length);
112 int acpi_create_mcfg_mmconfig(acpi_mcfg_mmconfig_t *mmconfig, u32 base,
113 u16 seg_nr, u8 start, u8 end)
115 memset(mmconfig, 0, sizeof(*mmconfig));
116 mmconfig->base_address = base;
117 mmconfig->base_reserved = 0;
118 mmconfig->pci_segment_group_number = seg_nr;
119 mmconfig->start_bus_number = start;
120 mmconfig->end_bus_number = end;
122 return sizeof(acpi_mcfg_mmconfig_t);
125 int acpi_create_madt_lapic(acpi_madt_lapic_t *lapic, u8 cpu, u8 apic)
127 lapic->type = LOCAL_APIC; /* Local APIC structure */
128 lapic->length = sizeof(acpi_madt_lapic_t);
129 lapic->flags = (1 << 0); /* Processor/LAPIC enabled */
130 lapic->processor_id = cpu;
131 lapic->apic_id = apic;
133 return lapic->length;
136 int acpi_create_madt_lx2apic(acpi_madt_lx2apic_t *lapic, u32 cpu, u32 apic)
138 lapic->type = LOCAL_X2APIC; /* Local APIC structure */
139 lapic->reserved = 0;
140 lapic->length = sizeof(acpi_madt_lx2apic_t);
141 lapic->flags = (1 << 0); /* Processor/LAPIC enabled */
142 lapic->processor_id = cpu;
143 lapic->x2apic_id = apic;
145 return lapic->length;
148 unsigned long acpi_create_madt_lapics(unsigned long current)
150 struct device *cpu;
151 int index, apic_ids[CONFIG_MAX_CPUS] = {0}, num_cpus = 0;
153 for (cpu = all_devices; cpu; cpu = cpu->next) {
154 if (!is_enabled_cpu(cpu))
155 continue;
156 if (num_cpus >= ARRAY_SIZE(apic_ids))
157 break;
158 apic_ids[num_cpus++] = cpu->path.apic.apic_id;
160 if (num_cpus > 1)
161 bubblesort(apic_ids, num_cpus, NUM_ASCENDING);
162 for (index = 0; index < num_cpus; index++) {
163 if (apic_ids[index] < 0xff)
164 current += acpi_create_madt_lapic((acpi_madt_lapic_t *)current,
165 index, apic_ids[index]);
166 else
167 current += acpi_create_madt_lx2apic((acpi_madt_lx2apic_t *)current,
168 index, apic_ids[index]);
171 return current;
174 int acpi_create_madt_ioapic(acpi_madt_ioapic_t *ioapic, u8 id, u32 addr,
175 u32 gsi_base)
177 ioapic->type = IO_APIC; /* I/O APIC structure */
178 ioapic->length = sizeof(acpi_madt_ioapic_t);
179 ioapic->reserved = 0x00;
180 ioapic->gsi_base = gsi_base;
181 ioapic->ioapic_id = id;
182 ioapic->ioapic_addr = addr;
184 return ioapic->length;
187 #if ENV_X86
188 /* For a system with multiple I/O APICs it's required that the one potentially
189 routing i8259 via ExtNMI delivery calls this first to get GSI #0. */
190 int acpi_create_madt_ioapic_from_hw(acpi_madt_ioapic_t *ioapic, u32 addr)
192 static u32 gsi_base;
193 u32 my_base;
194 u8 id = get_ioapic_id((void *)(uintptr_t)addr);
195 u8 count = ioapic_get_max_vectors((void *)(uintptr_t)addr);
197 my_base = gsi_base;
198 gsi_base += count;
199 return acpi_create_madt_ioapic(ioapic, id, addr, my_base);
201 #endif
203 int acpi_create_madt_irqoverride(acpi_madt_irqoverride_t *irqoverride,
204 u8 bus, u8 source, u32 gsirq, u16 flags)
206 irqoverride->type = IRQ_SOURCE_OVERRIDE; /* Interrupt source override */
207 irqoverride->length = sizeof(acpi_madt_irqoverride_t);
208 irqoverride->bus = bus;
209 irqoverride->source = source;
210 irqoverride->gsirq = gsirq;
211 irqoverride->flags = flags;
213 return irqoverride->length;
216 int acpi_create_madt_lapic_nmi(acpi_madt_lapic_nmi_t *lapic_nmi, u8 cpu,
217 u16 flags, u8 lint)
219 lapic_nmi->type = LOCAL_APIC_NMI; /* Local APIC NMI structure */
220 lapic_nmi->length = sizeof(acpi_madt_lapic_nmi_t);
221 lapic_nmi->flags = flags;
222 lapic_nmi->processor_id = cpu;
223 lapic_nmi->lint = lint;
225 return lapic_nmi->length;
228 int acpi_create_madt_lx2apic_nmi(acpi_madt_lx2apic_nmi_t *lapic_nmi, u32 cpu,
229 u16 flags, u8 lint)
231 lapic_nmi->type = LOCAL_X2APIC_NMI; /* Local APIC NMI structure */
232 lapic_nmi->length = sizeof(acpi_madt_lx2apic_nmi_t);
233 lapic_nmi->flags = flags;
234 lapic_nmi->processor_id = cpu;
235 lapic_nmi->lint = lint;
236 lapic_nmi->reserved[0] = 0;
237 lapic_nmi->reserved[1] = 0;
238 lapic_nmi->reserved[2] = 0;
240 return lapic_nmi->length;
243 unsigned long acpi_create_madt_lapics_with_nmis(unsigned long current)
245 const u16 flags = MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH;
247 current = acpi_create_madt_lapics(current);
249 /* 1: LINT1 connect to NMI */
250 /* create all subtables for processors */
251 current += acpi_create_madt_lapic_nmi((acpi_madt_lapic_nmi_t *)current,
252 ACPI_MADT_LAPIC_NMI_ALL_PROCESSORS, flags, 1);
254 if (!CONFIG(XAPIC_ONLY))
255 current += acpi_create_madt_lx2apic_nmi((acpi_madt_lx2apic_nmi_t *)current,
256 ACPI_MADT_LX2APIC_NMI_ALL_PROCESSORS, flags, 1);
258 return current;
261 void acpi_create_madt(acpi_madt_t *madt)
263 acpi_header_t *header = &(madt->header);
264 unsigned long current = (unsigned long)madt + sizeof(acpi_madt_t);
266 memset((void *)madt, 0, sizeof(acpi_madt_t));
268 if (!header)
269 return;
271 /* Fill out header fields. */
272 memcpy(header->signature, "APIC", 4);
273 memcpy(header->oem_id, OEM_ID, 6);
274 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
275 memcpy(header->asl_compiler_id, ASLC, 4);
277 header->asl_compiler_revision = asl_revision;
278 header->length = sizeof(acpi_madt_t);
279 header->revision = get_acpi_table_revision(MADT);
281 madt->lapic_addr = cpu_get_lapic_addr();
282 if (CONFIG(ACPI_HAVE_PCAT_8259))
283 madt->flags |= 1;
285 if (!CONFIG(ACPI_NO_MADT))
286 current = acpi_fill_madt(current);
288 /* (Re)calculate length and checksum. */
289 header->length = current - (unsigned long)madt;
291 header->checksum = acpi_checksum((void *)madt, header->length);
294 static unsigned long acpi_fill_mcfg(unsigned long current)
296 current += acpi_create_mcfg_mmconfig((acpi_mcfg_mmconfig_t *)current,
297 CONFIG_ECAM_MMCONF_BASE_ADDRESS, 0, 0,
298 CONFIG_ECAM_MMCONF_BUS_NUMBER - 1);
299 return current;
302 /* MCFG is defined in the PCI Firmware Specification 3.0. */
303 void acpi_create_mcfg(acpi_mcfg_t *mcfg)
305 acpi_header_t *header = &(mcfg->header);
306 unsigned long current = (unsigned long)mcfg + sizeof(acpi_mcfg_t);
308 memset((void *)mcfg, 0, sizeof(acpi_mcfg_t));
310 if (!header)
311 return;
313 /* Fill out header fields. */
314 memcpy(header->signature, "MCFG", 4);
315 memcpy(header->oem_id, OEM_ID, 6);
316 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
317 memcpy(header->asl_compiler_id, ASLC, 4);
319 header->asl_compiler_revision = asl_revision;
320 header->length = sizeof(acpi_mcfg_t);
321 header->revision = get_acpi_table_revision(MCFG);
323 if (CONFIG(ECAM_MMCONF_SUPPORT))
324 current = acpi_fill_mcfg(current);
326 /* (Re)calculate length and checksum. */
327 header->length = current - (unsigned long)mcfg;
328 header->checksum = acpi_checksum((void *)mcfg, header->length);
331 static void *get_tcpa_log(u32 *size)
333 const struct cbmem_entry *ce;
334 const u32 tcpa_default_log_len = 0x10000;
335 void *lasa;
336 ce = cbmem_entry_find(CBMEM_ID_TCPA_TCG_LOG);
337 if (ce) {
338 lasa = cbmem_entry_start(ce);
339 *size = cbmem_entry_size(ce);
340 printk(BIOS_DEBUG, "TCPA log found at %p\n", lasa);
341 return lasa;
343 lasa = cbmem_add(CBMEM_ID_TCPA_TCG_LOG, tcpa_default_log_len);
344 if (!lasa) {
345 printk(BIOS_ERR, "TCPA log creation failed\n");
346 return NULL;
349 printk(BIOS_DEBUG, "TCPA log created at %p\n", lasa);
350 memset(lasa, 0, tcpa_default_log_len);
352 *size = tcpa_default_log_len;
353 return lasa;
356 static void acpi_create_tcpa(acpi_tcpa_t *tcpa)
358 acpi_header_t *header = &(tcpa->header);
359 u32 tcpa_log_len;
360 void *lasa;
362 memset((void *)tcpa, 0, sizeof(acpi_tcpa_t));
364 lasa = get_tcpa_log(&tcpa_log_len);
365 if (!lasa)
366 return;
368 if (!header)
369 return;
371 /* Fill out header fields. */
372 memcpy(header->signature, "TCPA", 4);
373 memcpy(header->oem_id, OEM_ID, 6);
374 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
375 memcpy(header->asl_compiler_id, ASLC, 4);
377 header->asl_compiler_revision = asl_revision;
378 header->length = sizeof(acpi_tcpa_t);
379 header->revision = get_acpi_table_revision(TCPA);
381 tcpa->platform_class = 0;
382 tcpa->laml = tcpa_log_len;
383 tcpa->lasa = (uintptr_t)lasa;
385 /* Calculate checksum. */
386 header->checksum = acpi_checksum((void *)tcpa, header->length);
389 static void *get_tpm2_log(u32 *size)
391 const struct cbmem_entry *ce;
392 const u32 tpm2_default_log_len = 0x10000;
393 void *lasa;
394 ce = cbmem_entry_find(CBMEM_ID_TPM2_TCG_LOG);
395 if (ce) {
396 lasa = cbmem_entry_start(ce);
397 *size = cbmem_entry_size(ce);
398 printk(BIOS_DEBUG, "TPM2 log found at %p\n", lasa);
399 return lasa;
401 lasa = cbmem_add(CBMEM_ID_TPM2_TCG_LOG, tpm2_default_log_len);
402 if (!lasa) {
403 printk(BIOS_ERR, "TPM2 log creation failed\n");
404 return NULL;
407 printk(BIOS_DEBUG, "TPM2 log created at %p\n", lasa);
408 memset(lasa, 0, tpm2_default_log_len);
410 *size = tpm2_default_log_len;
411 return lasa;
414 static void acpi_create_tpm2(acpi_tpm2_t *tpm2)
416 acpi_header_t *header = &(tpm2->header);
417 u32 tpm2_log_len;
418 void *lasa;
420 memset((void *)tpm2, 0, sizeof(acpi_tpm2_t));
423 * Some payloads like SeaBIOS depend on log area to use TPM2.
424 * Get the memory size and address of TPM2 log area or initialize it.
426 lasa = get_tpm2_log(&tpm2_log_len);
427 if (!lasa)
428 tpm2_log_len = 0;
430 if (!header)
431 return;
433 /* Fill out header fields. */
434 memcpy(header->signature, "TPM2", 4);
435 memcpy(header->oem_id, OEM_ID, 6);
436 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
437 memcpy(header->asl_compiler_id, ASLC, 4);
439 header->asl_compiler_revision = asl_revision;
440 header->length = sizeof(acpi_tpm2_t);
441 header->revision = get_acpi_table_revision(TPM2);
443 /* Hard to detect for coreboot. Just set it to 0 */
444 tpm2->platform_class = 0;
445 if (CONFIG(CRB_TPM)) {
446 /* Must be set to 7 for CRB Support */
447 tpm2->control_area = CONFIG_CRB_TPM_BASE_ADDRESS + 0x40;
448 tpm2->start_method = 7;
449 } else {
450 /* Must be set to 0 for FIFO interface support */
451 tpm2->control_area = 0;
452 tpm2->start_method = 6;
454 memset(tpm2->msp, 0, sizeof(tpm2->msp));
456 /* Fill the log area size and start address fields. */
457 tpm2->laml = tpm2_log_len;
458 tpm2->lasa = (uintptr_t)lasa;
460 /* Calculate checksum. */
461 header->checksum = acpi_checksum((void *)tpm2, header->length);
464 static void acpi_ssdt_write_cbtable(void)
466 const struct cbmem_entry *cbtable;
467 uintptr_t base;
468 uint32_t size;
470 cbtable = cbmem_entry_find(CBMEM_ID_CBTABLE);
471 if (!cbtable)
472 return;
473 base = (uintptr_t)cbmem_entry_start(cbtable);
474 size = cbmem_entry_size(cbtable);
476 acpigen_write_device("CTBL");
477 acpigen_write_coreboot_hid(COREBOOT_ACPI_ID_CBTABLE);
478 acpigen_write_name_integer("_UID", 0);
479 acpigen_write_STA(ACPI_STATUS_DEVICE_HIDDEN_ON);
480 acpigen_write_name("_CRS");
481 acpigen_write_resourcetemplate_header();
482 acpigen_write_mem32fixed(0, base, size);
483 acpigen_write_resourcetemplate_footer();
484 acpigen_pop_len();
487 void acpi_create_ssdt_generator(acpi_header_t *ssdt, const char *oem_table_id)
489 unsigned long current = (unsigned long)ssdt + sizeof(acpi_header_t);
491 memset((void *)ssdt, 0, sizeof(acpi_header_t));
493 memcpy(&ssdt->signature, "SSDT", 4);
494 ssdt->revision = get_acpi_table_revision(SSDT);
495 memcpy(&ssdt->oem_id, OEM_ID, 6);
496 memcpy(&ssdt->oem_table_id, oem_table_id, 8);
497 ssdt->oem_revision = 42;
498 memcpy(&ssdt->asl_compiler_id, ASLC, 4);
499 ssdt->asl_compiler_revision = asl_revision;
500 ssdt->length = sizeof(acpi_header_t);
502 acpigen_set_current((char *)current);
504 /* Write object to declare coreboot tables */
505 acpi_ssdt_write_cbtable();
508 struct device *dev;
509 for (dev = all_devices; dev; dev = dev->next)
510 if (dev->enabled && dev->ops && dev->ops->acpi_fill_ssdt)
511 dev->ops->acpi_fill_ssdt(dev);
512 current = (unsigned long)acpigen_get_current();
515 /* (Re)calculate length and checksum. */
516 ssdt->length = current - (unsigned long)ssdt;
517 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
520 int acpi_create_srat_lapic(acpi_srat_lapic_t *lapic, u8 node, u8 apic)
522 memset((void *)lapic, 0, sizeof(acpi_srat_lapic_t));
524 lapic->type = 0; /* Processor local APIC/SAPIC affinity structure */
525 lapic->length = sizeof(acpi_srat_lapic_t);
526 lapic->flags = (1 << 0); /* Enabled (the use of this structure). */
527 lapic->proximity_domain_7_0 = node;
528 /* TODO: proximity_domain_31_8, local SAPIC EID, clock domain. */
529 lapic->apic_id = apic;
531 return lapic->length;
534 int acpi_create_srat_x2apic(acpi_srat_x2apic_t *x2apic, u32 node, u32 apic)
536 memset((void *)x2apic, 0, sizeof(acpi_srat_x2apic_t));
538 x2apic->type = 2; /* Processor x2APIC structure */
539 x2apic->length = sizeof(acpi_srat_x2apic_t);
540 x2apic->flags = (1 << 0); /* Enabled (the use of this structure). */
541 x2apic->proximity_domain = node;
542 x2apic->x2apic_id = apic;
544 return x2apic->length;
547 int acpi_create_srat_mem(acpi_srat_mem_t *mem, u8 node, u32 basek, u32 sizek,
548 u32 flags)
550 mem->type = 1; /* Memory affinity structure */
551 mem->length = sizeof(acpi_srat_mem_t);
552 mem->base_address_low = (basek << 10);
553 mem->base_address_high = (basek >> (32 - 10));
554 mem->length_low = (sizek << 10);
555 mem->length_high = (sizek >> (32 - 10));
556 mem->proximity_domain = node;
557 mem->flags = flags;
559 return mem->length;
562 int acpi_create_srat_gia_pci(acpi_srat_gia_t *gia, u32 proximity_domain,
563 u16 seg, u8 bus, u8 dev, u8 func, u32 flags)
565 gia->type = ACPI_SRAT_STRUCTURE_GIA;
566 gia->length = sizeof(acpi_srat_gia_t);
567 gia->proximity_domain = proximity_domain;
568 gia->dev_handle_type = ACPI_SRAT_GIA_DEV_HANDLE_PCI;
569 /* First two bytes has segment number */
570 memcpy(gia->dev_handle, &seg, 2);
571 gia->dev_handle[2] = bus; /* Byte 2 has bus number */
572 /* Byte 3 has bits 7:3 for dev, bits 2:0 for func */
573 gia->dev_handle[3] = PCI_SLOT(dev) | PCI_FUNC(func);
574 gia->flags = flags;
576 return gia->length;
579 /* http://www.microsoft.com/whdc/system/sysinternals/sratdwn.mspx */
580 void acpi_create_srat(acpi_srat_t *srat,
581 unsigned long (*acpi_fill_srat)(unsigned long current))
583 acpi_header_t *header = &(srat->header);
584 unsigned long current = (unsigned long)srat + sizeof(acpi_srat_t);
586 memset((void *)srat, 0, sizeof(acpi_srat_t));
588 if (!header)
589 return;
591 /* Fill out header fields. */
592 memcpy(header->signature, "SRAT", 4);
593 memcpy(header->oem_id, OEM_ID, 6);
594 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
595 memcpy(header->asl_compiler_id, ASLC, 4);
597 header->asl_compiler_revision = asl_revision;
598 header->length = sizeof(acpi_srat_t);
599 header->revision = get_acpi_table_revision(SRAT);
601 srat->resv = 1; /* Spec: Reserved to 1 for backwards compatibility. */
603 current = acpi_fill_srat(current);
605 /* (Re)calculate length and checksum. */
606 header->length = current - (unsigned long)srat;
607 header->checksum = acpi_checksum((void *)srat, header->length);
610 int acpi_create_cedt_chbs(acpi_cedt_chbs_t *chbs, u32 uid, u32 cxl_ver, u64 base)
612 memset((void *)chbs, 0, sizeof(acpi_cedt_chbs_t));
614 chbs->type = ACPI_CEDT_STRUCTURE_CHBS;
615 chbs->length = sizeof(acpi_cedt_chbs_t);
616 chbs->uid = uid;
617 chbs->cxl_ver = cxl_ver;
618 chbs->base = base;
621 * CXL spec 2.0 section 9.14.1.2 "CXL CHBS"
622 * CXL 1.1 spec compliant host bridge: 8KB
623 * CXL 2.0 spec compliant host bridge: 64KB
625 if (cxl_ver == ACPI_CEDT_CHBS_CXL_VER_1_1)
626 chbs->len = 8 * KiB;
627 else if (cxl_ver == ACPI_CEDT_CHBS_CXL_VER_2_0)
628 chbs->len = 64 * KiB;
629 else
630 printk(BIOS_ERR, "ACPI(%s:%s): Incorrect CXL version:%d\n", __FILE__, __func__,
631 cxl_ver);
633 return chbs->length;
636 int acpi_create_cedt_cfmws(acpi_cedt_cfmws_t *cfmws, u64 base_hpa, u64 window_size, u8 eniw,
637 u32 hbig, u16 restriction, u16 qtg_id, const u32 *interleave_target)
639 memset((void *)cfmws, 0, sizeof(acpi_cedt_cfmws_t));
641 cfmws->type = ACPI_CEDT_STRUCTURE_CFMWS;
643 u8 niw = 0;
644 if (eniw >= 8)
645 printk(BIOS_ERR, "ACPI(%s:%s): Incorrect eniw::%d\n", __FILE__, __func__, eniw);
646 else
647 /* NIW = 2 ** ENIW */
648 niw = 0x1 << eniw;
649 /* 36 + 4 * NIW */
650 cfmws->length = sizeof(acpi_cedt_cfmws_t) + 4 * niw;
652 cfmws->base_hpa = base_hpa;
653 cfmws->window_size = window_size;
654 cfmws->eniw = eniw;
656 // 0: Standard Modulo Arithmetic. Other values reserved.
657 cfmws->interleave_arithmetic = 0;
659 cfmws->hbig = hbig;
660 cfmws->restriction = restriction;
661 cfmws->qtg_id = qtg_id;
662 memcpy(&cfmws->interleave_target, interleave_target, 4 * niw);
664 return cfmws->length;
667 void acpi_create_cedt(acpi_cedt_t *cedt, unsigned long (*acpi_fill_cedt)(unsigned long current))
669 acpi_header_t *header = &(cedt->header);
670 unsigned long current = (unsigned long)cedt + sizeof(acpi_cedt_t);
672 memset((void *)cedt, 0, sizeof(acpi_cedt_t));
674 if (!header)
675 return;
677 /* Fill out header fields. */
678 memcpy(header->signature, "CEDT", 4);
679 memcpy(header->oem_id, OEM_ID, 6);
680 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
681 memcpy(header->asl_compiler_id, ASLC, 4);
683 header->asl_compiler_revision = asl_revision;
684 header->length = sizeof(acpi_cedt_t);
685 header->revision = get_acpi_table_revision(CEDT);
687 current = acpi_fill_cedt(current);
689 /* (Re)calculate length and checksum. */
690 header->length = current - (unsigned long)cedt;
691 header->checksum = acpi_checksum((void *)cedt, header->length);
694 int acpi_create_hmat_mpda(acpi_hmat_mpda_t *mpda, u32 initiator, u32 memory)
696 memset((void *)mpda, 0, sizeof(acpi_hmat_mpda_t));
698 mpda->type = 0; /* Memory Proximity Domain Attributes structure */
699 mpda->length = sizeof(acpi_hmat_mpda_t);
701 * Proximity Domain for Attached Initiator field is valid.
702 * Bit 1 and bit 2 are reserved since HMAT revision 2.
704 mpda->flags = (1 << 0);
705 mpda->proximity_domain_initiator = initiator;
706 mpda->proximity_domain_memory = memory;
708 return mpda->length;
711 void acpi_create_hmat(acpi_hmat_t *hmat,
712 unsigned long (*acpi_fill_hmat)(unsigned long current))
714 acpi_header_t *header = &(hmat->header);
715 unsigned long current = (unsigned long)hmat + sizeof(acpi_hmat_t);
717 memset((void *)hmat, 0, sizeof(acpi_hmat_t));
719 if (!header)
720 return;
722 /* Fill out header fields. */
723 memcpy(header->signature, "HMAT", 4);
724 memcpy(header->oem_id, OEM_ID, 6);
725 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
726 memcpy(header->asl_compiler_id, ASLC, 4);
728 header->asl_compiler_revision = asl_revision;
729 header->length = sizeof(acpi_hmat_t);
730 header->revision = get_acpi_table_revision(HMAT);
732 current = acpi_fill_hmat(current);
734 /* (Re)calculate length and checksum. */
735 header->length = current - (unsigned long)hmat;
736 header->checksum = acpi_checksum((void *)hmat, header->length);
739 void acpi_create_dmar(acpi_dmar_t *dmar, enum dmar_flags flags,
740 unsigned long (*acpi_fill_dmar)(unsigned long))
742 acpi_header_t *header = &(dmar->header);
743 unsigned long current = (unsigned long)dmar + sizeof(acpi_dmar_t);
745 memset((void *)dmar, 0, sizeof(acpi_dmar_t));
747 if (!header)
748 return;
750 /* Fill out header fields. */
751 memcpy(header->signature, "DMAR", 4);
752 memcpy(header->oem_id, OEM_ID, 6);
753 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
754 memcpy(header->asl_compiler_id, ASLC, 4);
756 header->asl_compiler_revision = asl_revision;
757 header->length = sizeof(acpi_dmar_t);
758 header->revision = get_acpi_table_revision(DMAR);
760 dmar->host_address_width = cpu_phys_address_size() - 1;
761 dmar->flags = flags;
763 current = acpi_fill_dmar(current);
765 /* (Re)calculate length and checksum. */
766 header->length = current - (unsigned long)dmar;
767 header->checksum = acpi_checksum((void *)dmar, header->length);
770 unsigned long acpi_create_dmar_drhd(unsigned long current, u8 flags,
771 u16 segment, u64 bar)
773 dmar_entry_t *drhd = (dmar_entry_t *)current;
774 memset(drhd, 0, sizeof(*drhd));
775 drhd->type = DMAR_DRHD;
776 drhd->length = sizeof(*drhd); /* will be fixed up later */
777 drhd->flags = flags;
778 drhd->segment = segment;
779 drhd->bar = bar;
781 return drhd->length;
784 unsigned long acpi_create_dmar_rmrr(unsigned long current, u16 segment,
785 u64 bar, u64 limit)
787 dmar_rmrr_entry_t *rmrr = (dmar_rmrr_entry_t *)current;
788 memset(rmrr, 0, sizeof(*rmrr));
789 rmrr->type = DMAR_RMRR;
790 rmrr->length = sizeof(*rmrr); /* will be fixed up later */
791 rmrr->segment = segment;
792 rmrr->bar = bar;
793 rmrr->limit = limit;
795 return rmrr->length;
798 unsigned long acpi_create_dmar_atsr(unsigned long current, u8 flags,
799 u16 segment)
801 dmar_atsr_entry_t *atsr = (dmar_atsr_entry_t *)current;
802 memset(atsr, 0, sizeof(*atsr));
803 atsr->type = DMAR_ATSR;
804 atsr->length = sizeof(*atsr); /* will be fixed up later */
805 atsr->flags = flags;
806 atsr->segment = segment;
808 return atsr->length;
811 unsigned long acpi_create_dmar_rhsa(unsigned long current, u64 base_addr,
812 u32 proximity_domain)
814 dmar_rhsa_entry_t *rhsa = (dmar_rhsa_entry_t *)current;
815 memset(rhsa, 0, sizeof(*rhsa));
816 rhsa->type = DMAR_RHSA;
817 rhsa->length = sizeof(*rhsa);
818 rhsa->base_address = base_addr;
819 rhsa->proximity_domain = proximity_domain;
821 return rhsa->length;
824 unsigned long acpi_create_dmar_andd(unsigned long current, u8 device_number,
825 const char *device_name)
827 dmar_andd_entry_t *andd = (dmar_andd_entry_t *)current;
828 int andd_len = sizeof(dmar_andd_entry_t) + strlen(device_name) + 1;
829 memset(andd, 0, andd_len);
830 andd->type = DMAR_ANDD;
831 andd->length = andd_len;
832 andd->device_number = device_number;
833 memcpy(&andd->device_name, device_name, strlen(device_name));
835 return andd->length;
838 unsigned long acpi_create_dmar_satc(unsigned long current, u8 flags, u16 segment)
840 dmar_satc_entry_t *satc = (dmar_satc_entry_t *)current;
841 int satc_len = sizeof(dmar_satc_entry_t);
842 memset(satc, 0, satc_len);
843 satc->type = DMAR_SATC;
844 satc->length = satc_len;
845 satc->flags = flags;
846 satc->segment_number = segment;
848 return satc->length;
851 void acpi_dmar_drhd_fixup(unsigned long base, unsigned long current)
853 dmar_entry_t *drhd = (dmar_entry_t *)base;
854 drhd->length = current - base;
857 void acpi_dmar_rmrr_fixup(unsigned long base, unsigned long current)
859 dmar_rmrr_entry_t *rmrr = (dmar_rmrr_entry_t *)base;
860 rmrr->length = current - base;
863 void acpi_dmar_atsr_fixup(unsigned long base, unsigned long current)
865 dmar_atsr_entry_t *atsr = (dmar_atsr_entry_t *)base;
866 atsr->length = current - base;
869 void acpi_dmar_satc_fixup(unsigned long base, unsigned long current)
871 dmar_satc_entry_t *satc = (dmar_satc_entry_t *)base;
872 satc->length = current - base;
875 static unsigned long acpi_create_dmar_ds(unsigned long current,
876 enum dev_scope_type type, u8 enumeration_id, u8 bus, u8 dev, u8 fn)
878 /* we don't support longer paths yet */
879 const size_t dev_scope_length = sizeof(dev_scope_t) + 2;
881 dev_scope_t *ds = (dev_scope_t *)current;
882 memset(ds, 0, dev_scope_length);
883 ds->type = type;
884 ds->length = dev_scope_length;
885 ds->enumeration = enumeration_id;
886 ds->start_bus = bus;
887 ds->path[0].dev = dev;
888 ds->path[0].fn = fn;
890 return ds->length;
893 unsigned long acpi_create_dmar_ds_pci_br(unsigned long current, u8 bus,
894 u8 dev, u8 fn)
896 return acpi_create_dmar_ds(current,
897 SCOPE_PCI_SUB, 0, bus, dev, fn);
900 unsigned long acpi_create_dmar_ds_pci(unsigned long current, u8 bus,
901 u8 dev, u8 fn)
903 return acpi_create_dmar_ds(current,
904 SCOPE_PCI_ENDPOINT, 0, bus, dev, fn);
907 unsigned long acpi_create_dmar_ds_ioapic(unsigned long current,
908 u8 enumeration_id, u8 bus, u8 dev, u8 fn)
910 return acpi_create_dmar_ds(current,
911 SCOPE_IOAPIC, enumeration_id, bus, dev, fn);
914 unsigned long acpi_create_dmar_ds_ioapic_from_hw(unsigned long current,
915 u32 addr, u8 bus, u8 dev, u8 fn)
917 u8 enumeration_id = get_ioapic_id((void *)(uintptr_t)addr);
918 return acpi_create_dmar_ds(current,
919 SCOPE_IOAPIC, enumeration_id, bus, dev, fn);
922 unsigned long acpi_create_dmar_ds_msi_hpet(unsigned long current,
923 u8 enumeration_id, u8 bus, u8 dev, u8 fn)
925 return acpi_create_dmar_ds(current,
926 SCOPE_MSI_HPET, enumeration_id, bus, dev, fn);
929 /* http://h21007.www2.hp.com/portal/download/files/unprot/Itanium/slit.pdf */
930 void acpi_create_slit(acpi_slit_t *slit,
931 unsigned long (*acpi_fill_slit)(unsigned long current))
933 acpi_header_t *header = &(slit->header);
934 unsigned long current = (unsigned long)slit + sizeof(acpi_slit_t);
936 memset((void *)slit, 0, sizeof(acpi_slit_t));
938 if (!header)
939 return;
941 /* Fill out header fields. */
942 memcpy(header->signature, "SLIT", 4);
943 memcpy(header->oem_id, OEM_ID, 6);
944 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
945 memcpy(header->asl_compiler_id, ASLC, 4);
947 header->asl_compiler_revision = asl_revision;
948 header->length = sizeof(acpi_slit_t);
949 header->revision = get_acpi_table_revision(SLIT);
951 current = acpi_fill_slit(current);
953 /* (Re)calculate length and checksum. */
954 header->length = current - (unsigned long)slit;
955 header->checksum = acpi_checksum((void *)slit, header->length);
958 /* http://www.intel.com/hardwaredesign/hpetspec_1.pdf */
959 void acpi_create_hpet(acpi_hpet_t *hpet)
961 acpi_header_t *header = &(hpet->header);
962 acpi_addr_t *addr = &(hpet->addr);
964 memset((void *)hpet, 0, sizeof(acpi_hpet_t));
966 if (!header)
967 return;
969 /* Fill out header fields. */
970 memcpy(header->signature, "HPET", 4);
971 memcpy(header->oem_id, OEM_ID, 6);
972 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
973 memcpy(header->asl_compiler_id, ASLC, 4);
975 header->asl_compiler_revision = asl_revision;
976 header->length = sizeof(acpi_hpet_t);
977 header->revision = get_acpi_table_revision(HPET);
979 /* Fill out HPET address. */
980 addr->space_id = ACPI_ADDRESS_SPACE_MEMORY;
981 addr->bit_width = 64;
982 addr->bit_offset = 0;
983 addr->addrl = HPET_BASE_ADDRESS & 0xffffffff;
984 addr->addrh = ((unsigned long long)HPET_BASE_ADDRESS) >> 32;
986 hpet->id = read32p(HPET_BASE_ADDRESS);
987 hpet->number = 0;
988 hpet->min_tick = CONFIG_HPET_MIN_TICKS;
990 header->checksum = acpi_checksum((void *)hpet, sizeof(acpi_hpet_t));
994 * This method adds the ACPI error injection capability. It fills the default information.
995 * HW dependent code (caller) can modify the defaults upon return. If no changes are necessary
996 * and the defaults are acceptable then caller can simply add the table (acpi_add_table).
997 * INPUTS:
998 * einj - ptr to the starting location of EINJ table
999 * actions - number of actions to trigger an error (HW dependent)
1000 * addr - address of trigger action table. This should be ACPI reserved memory and it will be
1001 * shared between OS and FW.
1003 void acpi_create_einj(acpi_einj_t *einj, uintptr_t addr, u8 actions)
1005 int i;
1006 acpi_header_t *header = &(einj->header);
1007 acpi_injection_header_t *inj_header = &(einj->inj_header);
1008 acpi_einj_smi_t *einj_smi = (acpi_einj_smi_t *)addr;
1009 acpi_einj_trigger_table_t *tat;
1010 if (!header)
1011 return;
1013 printk(BIOS_DEBUG, "%s einj_smi = %p\n", __func__, einj_smi);
1014 memset(einj_smi, 0, sizeof(acpi_einj_smi_t));
1015 tat = (acpi_einj_trigger_table_t *)((uint8_t *)einj_smi + sizeof(acpi_einj_smi_t));
1016 tat->header_size = 16;
1017 tat->revision = 0;
1018 tat->table_size = sizeof(acpi_einj_trigger_table_t) +
1019 sizeof(acpi_einj_action_table_t) * actions - 1;
1020 tat->entry_count = actions;
1021 printk(BIOS_DEBUG, "%s trigger_action_table = %p\n", __func__, tat);
1023 for (i = 0; i < actions; i++) {
1024 tat->trigger_action[i].action = TRIGGER_ERROR;
1025 tat->trigger_action[i].instruction = NO_OP;
1026 tat->trigger_action[i].flags = FLAG_IGNORE;
1027 tat->trigger_action[i].reg.space_id = ACPI_ADDRESS_SPACE_MEMORY;
1028 tat->trigger_action[i].reg.bit_width = 64;
1029 tat->trigger_action[i].reg.bit_offset = 0;
1030 tat->trigger_action[i].reg.access_size = ACPI_ACCESS_SIZE_QWORD_ACCESS;
1031 tat->trigger_action[i].reg.addr = 0;
1032 tat->trigger_action[i].value = 0;
1033 tat->trigger_action[i].mask = 0xFFFFFFFF;
1036 acpi_einj_action_table_t default_actions[ACTION_COUNT] = {
1037 [0] = {
1038 .action = BEGIN_INJECT_OP,
1039 .instruction = WRITE_REGISTER_VALUE,
1040 .flags = FLAG_PRESERVE,
1041 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->op_state),
1042 .value = 0,
1043 .mask = 0xFFFFFFFF
1045 [1] = {
1046 .action = GET_TRIGGER_ACTION_TABLE,
1047 .instruction = READ_REGISTER,
1048 .flags = FLAG_IGNORE,
1049 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->trigger_action_table),
1050 .value = 0,
1051 .mask = 0xFFFFFFFFFFFFFFFF
1053 [2] = {
1054 .action = SET_ERROR_TYPE,
1055 .instruction = WRITE_REGISTER,
1056 .flags = FLAG_PRESERVE,
1057 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->err_inject[0]),
1058 .value = 0,
1059 .mask = 0xFFFFFFFF
1061 [3] = {
1062 .action = GET_ERROR_TYPE,
1063 .instruction = READ_REGISTER,
1064 .flags = FLAG_IGNORE,
1065 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->err_inj_cap),
1066 .value = 0,
1067 .mask = 0xFFFFFFFF
1069 [4] = {
1070 .action = END_INJECT_OP,
1071 .instruction = WRITE_REGISTER_VALUE,
1072 .flags = FLAG_PRESERVE,
1073 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->op_state),
1074 .value = 0,
1075 .mask = 0xFFFFFFFF
1078 [5] = {
1079 .action = EXECUTE_INJECT_OP,
1080 .instruction = WRITE_REGISTER_VALUE,
1081 .flags = FLAG_PRESERVE,
1082 .reg = EINJ_REG_IO(),
1083 .value = 0x9a,
1084 .mask = 0xFFFF,
1086 [6] = {
1087 .action = CHECK_BUSY_STATUS,
1088 .instruction = READ_REGISTER_VALUE,
1089 .flags = FLAG_IGNORE,
1090 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->op_status),
1091 .value = 1,
1092 .mask = 1,
1094 [7] = {
1095 .action = GET_CMD_STATUS,
1096 .instruction = READ_REGISTER,
1097 .flags = FLAG_PRESERVE,
1098 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->cmd_sts),
1099 .value = 0,
1100 .mask = 0x1fe,
1102 [8] = {
1103 .action = SET_ERROR_TYPE_WITH_ADDRESS,
1104 .instruction = WRITE_REGISTER,
1105 .flags = FLAG_PRESERVE,
1106 .reg = EINJ_REG_MEMORY((u64)(uintptr_t)&einj_smi->setaddrtable),
1107 .value = 1,
1108 .mask = 0xffffffff
1112 einj_smi->err_inj_cap = ACPI_EINJ_DEFAULT_CAP;
1113 einj_smi->trigger_action_table = (u64)(uintptr_t)tat;
1115 for (i = 0; i < ACTION_COUNT; i++)
1116 printk(BIOS_DEBUG, "default_actions[%d].reg.addr is %llx\n", i,
1117 default_actions[i].reg.addr);
1119 memset((void *)einj, 0, sizeof(*einj));
1121 /* Fill out header fields. */
1122 memcpy(header->signature, "EINJ", 4);
1123 memcpy(header->oem_id, OEM_ID, 6);
1124 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1125 memcpy(header->asl_compiler_id, ASLC, 4);
1127 header->asl_compiler_revision = asl_revision;
1128 header->length = sizeof(acpi_einj_t);
1129 header->revision = 1;
1130 inj_header->einj_header_size = sizeof(acpi_injection_header_t);
1131 inj_header->entry_count = ACTION_COUNT;
1133 printk(BIOS_DEBUG, "%s einj->action_table = %p\n",
1134 __func__, einj->action_table);
1135 memcpy((void *)einj->action_table, (void *)default_actions, sizeof(einj->action_table));
1136 header->checksum = acpi_checksum((void *)einj, sizeof(*einj));
1139 void acpi_create_vfct(const struct device *device,
1140 acpi_vfct_t *vfct,
1141 unsigned long (*acpi_fill_vfct)(const struct device *device,
1142 acpi_vfct_t *vfct_struct, unsigned long current))
1144 acpi_header_t *header = &(vfct->header);
1145 unsigned long current = (unsigned long)vfct + sizeof(acpi_vfct_t);
1147 memset((void *)vfct, 0, sizeof(acpi_vfct_t));
1149 if (!header)
1150 return;
1152 /* Fill out header fields. */
1153 memcpy(header->signature, "VFCT", 4);
1154 memcpy(header->oem_id, OEM_ID, 6);
1155 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1156 memcpy(header->asl_compiler_id, ASLC, 4);
1158 header->asl_compiler_revision = asl_revision;
1159 header->revision = get_acpi_table_revision(VFCT);
1161 current = acpi_fill_vfct(device, vfct, current);
1163 /* If no BIOS image, return with header->length == 0. */
1164 if (!vfct->VBIOSImageOffset)
1165 return;
1167 /* (Re)calculate length and checksum. */
1168 header->length = current - (unsigned long)vfct;
1169 header->checksum = acpi_checksum((void *)vfct, header->length);
1172 void acpi_create_ipmi(const struct device *device,
1173 struct acpi_spmi *spmi,
1174 const u16 ipmi_revision,
1175 const acpi_addr_t *addr,
1176 const enum acpi_ipmi_interface_type type,
1177 const s8 gpe_interrupt,
1178 const u32 apic_interrupt,
1179 const u32 uid)
1181 acpi_header_t *header = &(spmi->header);
1182 memset((void *)spmi, 0, sizeof(struct acpi_spmi));
1184 /* Fill out header fields. */
1185 memcpy(header->signature, "SPMI", 4);
1186 memcpy(header->oem_id, OEM_ID, 6);
1187 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1188 memcpy(header->asl_compiler_id, ASLC, 4);
1190 header->asl_compiler_revision = asl_revision;
1191 header->length = sizeof(struct acpi_spmi);
1192 header->revision = get_acpi_table_revision(SPMI);
1194 spmi->reserved = 1;
1196 if (device->path.type == DEVICE_PATH_PCI) {
1197 spmi->pci_device_flag = ACPI_IPMI_PCI_DEVICE_FLAG;
1198 spmi->pci_bus = device->bus->secondary;
1199 spmi->pci_device = device->path.pci.devfn >> 3;
1200 spmi->pci_function = device->path.pci.devfn & 0x7;
1201 } else if (type != IPMI_INTERFACE_SSIF) {
1202 memcpy(spmi->uid, &uid, sizeof(spmi->uid));
1205 spmi->base_address = *addr;
1206 spmi->specification_revision = ipmi_revision;
1208 spmi->interface_type = type;
1210 if (gpe_interrupt >= 0 && gpe_interrupt < 32) {
1211 spmi->gpe = gpe_interrupt;
1212 spmi->interrupt_type |= ACPI_IPMI_INT_TYPE_SCI;
1214 if (apic_interrupt > 0) {
1215 spmi->global_system_interrupt = apic_interrupt;
1216 spmi->interrupt_type |= ACPI_IPMI_INT_TYPE_APIC;
1219 /* Calculate checksum. */
1220 header->checksum = acpi_checksum((void *)spmi, header->length);
1223 void acpi_create_ivrs(acpi_ivrs_t *ivrs,
1224 unsigned long (*acpi_fill_ivrs)(acpi_ivrs_t *ivrs_struct,
1225 unsigned long current))
1227 acpi_header_t *header = &(ivrs->header);
1228 unsigned long current = (unsigned long)ivrs + sizeof(acpi_ivrs_t);
1230 memset((void *)ivrs, 0, sizeof(acpi_ivrs_t));
1232 if (!header)
1233 return;
1235 /* Fill out header fields. */
1236 memcpy(header->signature, "IVRS", 4);
1237 memcpy(header->oem_id, OEM_ID, 6);
1238 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1239 memcpy(header->asl_compiler_id, ASLC, 4);
1241 header->asl_compiler_revision = asl_revision;
1242 header->length = sizeof(acpi_ivrs_t);
1243 header->revision = get_acpi_table_revision(IVRS);
1245 current = acpi_fill_ivrs(ivrs, current);
1247 /* (Re)calculate length and checksum. */
1248 header->length = current - (unsigned long)ivrs;
1249 header->checksum = acpi_checksum((void *)ivrs, header->length);
1252 void acpi_create_crat(struct acpi_crat_header *crat,
1253 unsigned long (*acpi_fill_crat)(struct acpi_crat_header *crat_struct,
1254 unsigned long current))
1256 acpi_header_t *header = &(crat->header);
1257 unsigned long current = (unsigned long)crat + sizeof(struct acpi_crat_header);
1259 memset((void *)crat, 0, sizeof(struct acpi_crat_header));
1261 if (!header)
1262 return;
1264 /* Fill out header fields. */
1265 memcpy(header->signature, "CRAT", 4);
1266 memcpy(header->oem_id, OEM_ID, 6);
1267 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1268 memcpy(header->asl_compiler_id, ASLC, 4);
1270 header->asl_compiler_revision = asl_revision;
1271 header->length = sizeof(struct acpi_crat_header);
1272 header->revision = get_acpi_table_revision(CRAT);
1274 current = acpi_fill_crat(crat, current);
1276 /* (Re)calculate length and checksum. */
1277 header->length = current - (unsigned long)crat;
1278 header->checksum = acpi_checksum((void *)crat, header->length);
1281 unsigned long acpi_write_hpet(const struct device *device, unsigned long current,
1282 acpi_rsdp_t *rsdp)
1284 acpi_hpet_t *hpet;
1287 * We explicitly add these tables later on:
1289 printk(BIOS_DEBUG, "ACPI: * HPET\n");
1291 hpet = (acpi_hpet_t *)current;
1292 current += sizeof(acpi_hpet_t);
1293 current = ALIGN_UP(current, 16);
1294 acpi_create_hpet(hpet);
1295 acpi_add_table(rsdp, hpet);
1297 return current;
1300 void acpi_create_dbg2(acpi_dbg2_header_t *dbg2,
1301 int port_type, int port_subtype,
1302 acpi_addr_t *address, uint32_t address_size,
1303 const char *device_path)
1305 uintptr_t current;
1306 acpi_dbg2_device_t *device;
1307 uint32_t *dbg2_addr_size;
1308 acpi_header_t *header;
1309 size_t path_len;
1310 const char *path;
1311 char *namespace;
1313 /* Fill out header fields. */
1314 current = (uintptr_t)dbg2;
1315 memset(dbg2, 0, sizeof(acpi_dbg2_header_t));
1316 header = &(dbg2->header);
1318 if (!header)
1319 return;
1321 header->revision = get_acpi_table_revision(DBG2);
1322 memcpy(header->signature, "DBG2", 4);
1323 memcpy(header->oem_id, OEM_ID, 6);
1324 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1325 memcpy(header->asl_compiler_id, ASLC, 4);
1326 header->asl_compiler_revision = asl_revision;
1328 /* One debug device defined */
1329 dbg2->devices_offset = sizeof(acpi_dbg2_header_t);
1330 dbg2->devices_count = 1;
1331 current += sizeof(acpi_dbg2_header_t);
1333 /* Device comes after the header */
1334 device = (acpi_dbg2_device_t *)current;
1335 memset(device, 0, sizeof(acpi_dbg2_device_t));
1336 current += sizeof(acpi_dbg2_device_t);
1338 device->revision = 0;
1339 device->address_count = 1;
1340 device->port_type = port_type;
1341 device->port_subtype = port_subtype;
1343 /* Base Address comes after device structure */
1344 memcpy((void *)current, address, sizeof(acpi_addr_t));
1345 device->base_address_offset = current - (uintptr_t)device;
1346 current += sizeof(acpi_addr_t);
1348 /* Address Size comes after address structure */
1349 dbg2_addr_size = (uint32_t *)current;
1350 device->address_size_offset = current - (uintptr_t)device;
1351 *dbg2_addr_size = address_size;
1352 current += sizeof(uint32_t);
1354 /* Namespace string comes last, use '.' if not provided */
1355 path = device_path ? : ".";
1356 /* Namespace string length includes NULL terminator */
1357 path_len = strlen(path) + 1;
1358 namespace = (char *)current;
1359 device->namespace_string_length = path_len;
1360 device->namespace_string_offset = current - (uintptr_t)device;
1361 strncpy(namespace, path, path_len);
1362 current += path_len;
1364 /* Update structure lengths and checksum */
1365 device->length = current - (uintptr_t)device;
1366 header->length = current - (uintptr_t)dbg2;
1367 header->checksum = acpi_checksum((uint8_t *)dbg2, header->length);
1370 unsigned long acpi_write_dbg2_pci_uart(acpi_rsdp_t *rsdp, unsigned long current,
1371 const struct device *dev, uint8_t access_size)
1373 acpi_dbg2_header_t *dbg2 = (acpi_dbg2_header_t *)current;
1374 struct resource *res;
1375 acpi_addr_t address;
1377 if (!dev) {
1378 printk(BIOS_DEBUG, "%s: Device not found\n", __func__);
1379 return current;
1381 if (!dev->enabled) {
1382 printk(BIOS_INFO, "%s: Device not enabled\n", __func__);
1383 return current;
1385 res = probe_resource(dev, PCI_BASE_ADDRESS_0);
1386 if (!res) {
1387 printk(BIOS_ERR, "%s: Unable to find resource for %s\n",
1388 __func__, dev_path(dev));
1389 return current;
1392 memset(&address, 0, sizeof(address));
1393 if (res->flags & IORESOURCE_IO)
1394 address.space_id = ACPI_ADDRESS_SPACE_IO;
1395 else if (res->flags & IORESOURCE_MEM)
1396 address.space_id = ACPI_ADDRESS_SPACE_MEMORY;
1397 else {
1398 printk(BIOS_ERR, "%s: Unknown address space type\n", __func__);
1399 return current;
1402 address.addrl = (uint32_t)res->base;
1403 address.addrh = (uint32_t)((res->base >> 32) & 0xffffffff);
1404 address.access_size = access_size;
1406 acpi_create_dbg2(dbg2,
1407 ACPI_DBG2_PORT_SERIAL,
1408 ACPI_DBG2_PORT_SERIAL_16550,
1409 &address, res->size,
1410 acpi_device_path(dev));
1412 if (dbg2->header.length) {
1413 current += dbg2->header.length;
1414 current = acpi_align_current(current);
1415 acpi_add_table(rsdp, dbg2);
1418 return current;
1421 void acpi_create_facs(acpi_facs_t *facs)
1423 memset((void *)facs, 0, sizeof(acpi_facs_t));
1425 memcpy(facs->signature, "FACS", 4);
1426 facs->length = sizeof(acpi_facs_t);
1427 facs->hardware_signature = 0;
1428 facs->firmware_waking_vector = 0;
1429 facs->global_lock = 0;
1430 facs->flags = 0;
1431 facs->x_firmware_waking_vector_l = 0;
1432 facs->x_firmware_waking_vector_h = 0;
1433 facs->version = get_acpi_table_revision(FACS);
1436 static void acpi_write_rsdt(acpi_rsdt_t *rsdt, char *oem_id, char *oem_table_id)
1438 acpi_header_t *header = &(rsdt->header);
1440 if (!header)
1441 return;
1443 /* Fill out header fields. */
1444 memcpy(header->signature, "RSDT", 4);
1445 memcpy(header->oem_id, oem_id, 6);
1446 memcpy(header->oem_table_id, oem_table_id, 8);
1447 memcpy(header->asl_compiler_id, ASLC, 4);
1449 header->asl_compiler_revision = asl_revision;
1450 header->length = sizeof(acpi_rsdt_t);
1451 header->revision = get_acpi_table_revision(RSDT);
1453 /* Entries are filled in later, we come with an empty set. */
1455 /* Fix checksum. */
1456 header->checksum = acpi_checksum((void *)rsdt, sizeof(acpi_rsdt_t));
1459 static void acpi_write_xsdt(acpi_xsdt_t *xsdt, char *oem_id, char *oem_table_id)
1461 acpi_header_t *header = &(xsdt->header);
1463 if (!header)
1464 return;
1466 /* Fill out header fields. */
1467 memcpy(header->signature, "XSDT", 4);
1468 memcpy(header->oem_id, oem_id, 6);
1469 memcpy(header->oem_table_id, oem_table_id, 8);
1470 memcpy(header->asl_compiler_id, ASLC, 4);
1472 header->asl_compiler_revision = asl_revision;
1473 header->length = sizeof(acpi_xsdt_t);
1474 header->revision = get_acpi_table_revision(XSDT);
1476 /* Entries are filled in later, we come with an empty set. */
1478 /* Fix checksum. */
1479 header->checksum = acpi_checksum((void *)xsdt, sizeof(acpi_xsdt_t));
1482 static void acpi_write_rsdp(acpi_rsdp_t *rsdp, acpi_rsdt_t *rsdt,
1483 acpi_xsdt_t *xsdt, char *oem_id)
1485 memset(rsdp, 0, sizeof(acpi_rsdp_t));
1487 memcpy(rsdp->signature, RSDP_SIG, 8);
1488 memcpy(rsdp->oem_id, oem_id, 6);
1490 rsdp->length = sizeof(acpi_rsdp_t);
1491 rsdp->rsdt_address = (uintptr_t)rsdt;
1494 * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2.
1496 * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
1497 * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
1498 * revision 0).
1500 if (xsdt == NULL) {
1501 rsdp->revision = 0;
1502 } else {
1503 rsdp->xsdt_address = (u64)(uintptr_t)xsdt;
1504 rsdp->revision = get_acpi_table_revision(RSDP);
1507 /* Calculate checksums. */
1508 rsdp->checksum = acpi_checksum((void *)rsdp, 20);
1509 rsdp->ext_checksum = acpi_checksum((void *)rsdp, sizeof(acpi_rsdp_t));
1512 unsigned long acpi_create_hest_error_source(acpi_hest_t *hest,
1513 acpi_hest_esd_t *esd, u16 type, void *data, u16 data_len)
1515 acpi_header_t *header = &(hest->header);
1516 acpi_hest_hen_t *hen;
1517 void *pos;
1518 u16 len;
1520 pos = esd;
1521 memset(pos, 0, sizeof(acpi_hest_esd_t));
1522 len = 0;
1523 esd->type = type; /* MCE */
1524 esd->source_id = hest->error_source_count;
1525 esd->flags = 0; /* FIRMWARE_FIRST */
1526 esd->enabled = 1;
1527 esd->prealloc_erecords = 1;
1528 esd->max_section_per_record = 0x1;
1530 len += sizeof(acpi_hest_esd_t);
1531 pos = esd + 1;
1533 switch (type) {
1534 case 0: /* MCE */
1535 break;
1536 case 1: /* CMC */
1537 hen = (acpi_hest_hen_t *)(pos);
1538 memset(pos, 0, sizeof(acpi_hest_hen_t));
1539 hen->type = 3; /* SCI? */
1540 hen->length = sizeof(acpi_hest_hen_t);
1541 hen->conf_we = 0; /* Configuration Write Enable. */
1542 hen->poll_interval = 0;
1543 hen->vector = 0;
1544 hen->sw2poll_threshold_val = 0;
1545 hen->sw2poll_threshold_win = 0;
1546 hen->error_threshold_val = 0;
1547 hen->error_threshold_win = 0;
1548 len += sizeof(acpi_hest_hen_t);
1549 pos = hen + 1;
1550 break;
1551 case 2: /* NMI */
1552 case 6: /* AER Root Port */
1553 case 7: /* AER Endpoint */
1554 case 8: /* AER Bridge */
1555 case 9: /* Generic Hardware Error Source. */
1556 /* TODO: */
1557 break;
1558 default:
1559 printk(BIOS_DEBUG, "Invalid type of Error Source.");
1560 break;
1562 hest->error_source_count++;
1564 memcpy(pos, data, data_len);
1565 len += data_len;
1566 if (header)
1567 header->length += len;
1569 return len;
1572 /* ACPI 4.0 */
1573 void acpi_write_hest(acpi_hest_t *hest,
1574 unsigned long (*acpi_fill_hest)(acpi_hest_t *hest))
1576 acpi_header_t *header = &(hest->header);
1578 memset(hest, 0, sizeof(acpi_hest_t));
1580 if (!header)
1581 return;
1583 memcpy(header->signature, "HEST", 4);
1584 memcpy(header->oem_id, OEM_ID, 6);
1585 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1586 memcpy(header->asl_compiler_id, ASLC, 4);
1587 header->asl_compiler_revision = asl_revision;
1588 header->length += sizeof(acpi_hest_t);
1589 header->revision = get_acpi_table_revision(HEST);
1591 acpi_fill_hest(hest);
1593 /* Calculate checksums. */
1594 header->checksum = acpi_checksum((void *)hest, header->length);
1597 /* ACPI 3.0b */
1598 void acpi_write_bert(acpi_bert_t *bert, uintptr_t region, size_t length)
1600 acpi_header_t *header = &(bert->header);
1602 memset(bert, 0, sizeof(acpi_bert_t));
1604 if (!header)
1605 return;
1607 memcpy(header->signature, "BERT", 4);
1608 memcpy(header->oem_id, OEM_ID, 6);
1609 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1610 memcpy(header->asl_compiler_id, ASLC, 4);
1611 header->asl_compiler_revision = asl_revision;
1612 header->length += sizeof(acpi_bert_t);
1613 header->revision = get_acpi_table_revision(BERT);
1615 bert->error_region = region;
1616 bert->region_length = length;
1618 /* Calculate checksums. */
1619 header->checksum = acpi_checksum((void *)bert, header->length);
1622 __weak void arch_fill_fadt(acpi_fadt_t *fadt) { }
1623 __weak void soc_fill_fadt(acpi_fadt_t *fadt) { }
1624 __weak void mainboard_fill_fadt(acpi_fadt_t *fadt) { }
1626 void acpi_create_fadt(acpi_fadt_t *fadt, acpi_facs_t *facs, void *dsdt)
1628 acpi_header_t *header = &(fadt->header);
1630 memset((void *)fadt, 0, sizeof(acpi_fadt_t));
1632 if (!header)
1633 return;
1635 memcpy(header->signature, "FACP", 4);
1636 header->length = sizeof(acpi_fadt_t);
1637 header->revision = get_acpi_table_revision(FADT);
1638 memcpy(header->oem_id, OEM_ID, 6);
1639 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1640 memcpy(header->asl_compiler_id, ASLC, 4);
1641 header->asl_compiler_revision = asl_revision;
1643 fadt->FADT_MinorVersion = get_acpi_fadt_minor_version();
1644 fadt->firmware_ctrl = (unsigned long)facs;
1645 fadt->x_firmware_ctl_l = (unsigned long)facs;
1646 fadt->x_firmware_ctl_h = 0;
1648 fadt->dsdt = (unsigned long)dsdt;
1649 fadt->x_dsdt_l = (unsigned long)dsdt;
1650 fadt->x_dsdt_h = 0;
1652 /* should be 0 ACPI 3.0 */
1653 fadt->reserved = 0;
1655 fadt->preferred_pm_profile = acpi_get_preferred_pm_profile();
1657 if (CONFIG(USE_PC_CMOS_ALTCENTURY))
1658 fadt->century = RTC_CLK_ALTCENTURY;
1660 arch_fill_fadt(fadt);
1662 acpi_fill_fadt(fadt);
1664 soc_fill_fadt(fadt);
1665 mainboard_fill_fadt(fadt);
1667 header->checksum =
1668 acpi_checksum((void *)fadt, header->length);
1671 void acpi_create_lpit(acpi_lpit_t *lpit)
1673 acpi_header_t *header = &(lpit->header);
1674 unsigned long current = (unsigned long)lpit + sizeof(acpi_lpit_t);
1676 memset((void *)lpit, 0, sizeof(acpi_lpit_t));
1678 if (!header)
1679 return;
1681 /* Fill out header fields. */
1682 memcpy(header->signature, "LPIT", 4);
1683 memcpy(header->oem_id, OEM_ID, 6);
1684 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
1685 memcpy(header->asl_compiler_id, ASLC, 4);
1687 header->asl_compiler_revision = asl_revision;
1688 header->revision = get_acpi_table_revision(LPIT);
1689 header->oem_revision = 42;
1690 header->length = sizeof(acpi_lpit_t);
1692 current = acpi_fill_lpit(current);
1694 /* (Re)calculate length and checksum. */
1695 header->length = current - (unsigned long)lpit;
1696 header->checksum = acpi_checksum((void *)lpit, header->length);
1699 unsigned long acpi_create_lpi_desc_ncst(acpi_lpi_desc_ncst_t *lpi_desc, uint16_t uid)
1701 memset(lpi_desc, 0, sizeof(acpi_lpi_desc_ncst_t));
1702 lpi_desc->header.length = sizeof(acpi_lpi_desc_ncst_t);
1703 lpi_desc->header.type = ACPI_LPI_DESC_TYPE_NATIVE_CSTATE;
1704 lpi_desc->header.uid = uid;
1706 return lpi_desc->header.length;
1709 unsigned long __weak fw_cfg_acpi_tables(unsigned long start)
1711 return 0;
1714 void preload_acpi_dsdt(void)
1716 const char *file = CONFIG_CBFS_PREFIX "/dsdt.aml";
1718 if (!CONFIG(CBFS_PRELOAD))
1719 return;
1721 printk(BIOS_DEBUG, "Preloading %s\n", file);
1722 cbfs_preload(file);
1725 static uintptr_t coreboot_rsdp;
1727 uintptr_t get_coreboot_rsdp(void)
1729 return coreboot_rsdp;
1732 unsigned long write_acpi_tables(unsigned long start)
1734 unsigned long current;
1735 acpi_rsdp_t *rsdp;
1736 acpi_rsdt_t *rsdt;
1737 acpi_xsdt_t *xsdt;
1738 acpi_fadt_t *fadt;
1739 acpi_facs_t *facs;
1740 acpi_header_t *slic_file, *slic;
1741 acpi_header_t *ssdt;
1742 acpi_header_t *dsdt_file, *dsdt;
1743 acpi_mcfg_t *mcfg;
1744 acpi_tcpa_t *tcpa;
1745 acpi_tpm2_t *tpm2;
1746 acpi_madt_t *madt;
1747 acpi_lpit_t *lpit;
1748 acpi_bert_t *bert;
1749 struct device *dev;
1750 unsigned long fw;
1751 size_t slic_size, dsdt_size;
1752 char oem_id[6], oem_table_id[8];
1754 current = start;
1756 /* Align ACPI tables to 16byte */
1757 current = acpi_align_current(current);
1759 /* Special case for qemu */
1760 fw = fw_cfg_acpi_tables(current);
1761 if (fw) {
1762 rsdp = NULL;
1763 /* Find RSDP. */
1764 for (void *p = (void *)current; p < (void *)fw; p += 16) {
1765 if (valid_rsdp((acpi_rsdp_t *)p)) {
1766 rsdp = p;
1767 break;
1770 if (!rsdp)
1771 return fw;
1773 /* Add BOOT0000 for Linux google firmware driver */
1774 printk(BIOS_DEBUG, "ACPI: * SSDT\n");
1775 ssdt = (acpi_header_t *)fw;
1776 current = (unsigned long)ssdt + sizeof(acpi_header_t);
1778 memset((void *)ssdt, 0, sizeof(acpi_header_t));
1780 memcpy(&ssdt->signature, "SSDT", 4);
1781 ssdt->revision = get_acpi_table_revision(SSDT);
1782 memcpy(&ssdt->oem_id, OEM_ID, 6);
1783 memcpy(&ssdt->oem_table_id, oem_table_id, 8);
1784 ssdt->oem_revision = 42;
1785 memcpy(&ssdt->asl_compiler_id, ASLC, 4);
1786 ssdt->asl_compiler_revision = asl_revision;
1787 ssdt->length = sizeof(acpi_header_t);
1789 acpigen_set_current((char *)current);
1791 /* Write object to declare coreboot tables */
1792 acpi_ssdt_write_cbtable();
1794 /* (Re)calculate length and checksum. */
1795 ssdt->length = current - (unsigned long)ssdt;
1796 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
1798 acpi_create_ssdt_generator(ssdt, ACPI_TABLE_CREATOR);
1800 acpi_add_table(rsdp, ssdt);
1802 return fw;
1805 dsdt_file = cbfs_map(CONFIG_CBFS_PREFIX "/dsdt.aml", &dsdt_size);
1806 if (!dsdt_file) {
1807 printk(BIOS_ERR, "No DSDT file, skipping ACPI tables\n");
1808 return current;
1811 if (dsdt_file->length > dsdt_size
1812 || dsdt_file->length < sizeof(acpi_header_t)
1813 || memcmp(dsdt_file->signature, "DSDT", 4) != 0) {
1814 printk(BIOS_ERR, "Invalid DSDT file, skipping ACPI tables\n");
1815 return current;
1818 slic_file = cbfs_map(CONFIG_CBFS_PREFIX "/slic", &slic_size);
1819 if (slic_file
1820 && (slic_file->length > slic_size
1821 || slic_file->length < sizeof(acpi_header_t)
1822 || (memcmp(slic_file->signature, "SLIC", 4) != 0
1823 && memcmp(slic_file->signature, "MSDM", 4) != 0))) {
1824 slic_file = 0;
1827 if (slic_file) {
1828 memcpy(oem_id, slic_file->oem_id, 6);
1829 memcpy(oem_table_id, slic_file->oem_table_id, 8);
1830 } else {
1831 memcpy(oem_id, OEM_ID, 6);
1832 memcpy(oem_table_id, ACPI_TABLE_CREATOR, 8);
1835 printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx.\n", start);
1837 /* We need at least an RSDP and an RSDT Table */
1838 rsdp = (acpi_rsdp_t *)current;
1839 coreboot_rsdp = (uintptr_t)rsdp;
1840 current += sizeof(acpi_rsdp_t);
1841 current = acpi_align_current(current);
1842 rsdt = (acpi_rsdt_t *)current;
1843 current += sizeof(acpi_rsdt_t);
1844 current = acpi_align_current(current);
1845 xsdt = (acpi_xsdt_t *)current;
1846 current += sizeof(acpi_xsdt_t);
1847 current = acpi_align_current(current);
1849 /* clear all table memory */
1850 memset((void *)start, 0, current - start);
1852 acpi_write_rsdp(rsdp, rsdt, xsdt, oem_id);
1853 acpi_write_rsdt(rsdt, oem_id, oem_table_id);
1854 acpi_write_xsdt(xsdt, oem_id, oem_table_id);
1856 printk(BIOS_DEBUG, "ACPI: * FACS\n");
1857 current = ALIGN_UP(current, 64);
1858 facs = (acpi_facs_t *)current;
1859 current += sizeof(acpi_facs_t);
1860 current = acpi_align_current(current);
1861 acpi_create_facs(facs);
1863 printk(BIOS_DEBUG, "ACPI: * DSDT\n");
1864 dsdt = (acpi_header_t *)current;
1865 memcpy(dsdt, dsdt_file, sizeof(acpi_header_t));
1866 if (dsdt->length >= sizeof(acpi_header_t)) {
1867 current += sizeof(acpi_header_t);
1869 acpigen_set_current((char *)current);
1871 if (CONFIG(ACPI_SOC_NVS))
1872 acpi_fill_gnvs();
1873 if (CONFIG(CHROMEOS_NVS))
1874 acpi_fill_cnvs();
1876 for (dev = all_devices; dev; dev = dev->next)
1877 if (dev->ops && dev->ops->acpi_inject_dsdt)
1878 dev->ops->acpi_inject_dsdt(dev);
1879 current = (unsigned long)acpigen_get_current();
1880 memcpy((char *)current,
1881 (char *)dsdt_file + sizeof(acpi_header_t),
1882 dsdt->length - sizeof(acpi_header_t));
1883 current += dsdt->length - sizeof(acpi_header_t);
1885 /* (Re)calculate length and checksum. */
1886 dsdt->length = current - (unsigned long)dsdt;
1887 dsdt->checksum = 0;
1888 dsdt->checksum = acpi_checksum((void *)dsdt, dsdt->length);
1891 current = acpi_align_current(current);
1893 printk(BIOS_DEBUG, "ACPI: * FADT\n");
1894 fadt = (acpi_fadt_t *)current;
1895 current += sizeof(acpi_fadt_t);
1896 current = acpi_align_current(current);
1898 acpi_create_fadt(fadt, facs, dsdt);
1899 acpi_add_table(rsdp, fadt);
1901 if (slic_file) {
1902 printk(BIOS_DEBUG, "ACPI: * SLIC\n");
1903 slic = (acpi_header_t *)current;
1904 memcpy(slic, slic_file, slic_file->length);
1905 current += slic_file->length;
1906 current = acpi_align_current(current);
1907 acpi_add_table(rsdp, slic);
1910 printk(BIOS_DEBUG, "ACPI: * SSDT\n");
1911 ssdt = (acpi_header_t *)current;
1912 acpi_create_ssdt_generator(ssdt, ACPI_TABLE_CREATOR);
1913 if (ssdt->length > sizeof(acpi_header_t)) {
1914 current += ssdt->length;
1915 acpi_add_table(rsdp, ssdt);
1916 current = acpi_align_current(current);
1919 printk(BIOS_DEBUG, "ACPI: * MCFG\n");
1920 mcfg = (acpi_mcfg_t *)current;
1921 acpi_create_mcfg(mcfg);
1922 if (mcfg->header.length > sizeof(acpi_mcfg_t)) {
1923 current += mcfg->header.length;
1924 current = acpi_align_current(current);
1925 acpi_add_table(rsdp, mcfg);
1928 if (CONFIG(TPM1)) {
1929 printk(BIOS_DEBUG, "ACPI: * TCPA\n");
1930 tcpa = (acpi_tcpa_t *)current;
1931 acpi_create_tcpa(tcpa);
1932 if (tcpa->header.length >= sizeof(acpi_tcpa_t)) {
1933 current += tcpa->header.length;
1934 current = acpi_align_current(current);
1935 acpi_add_table(rsdp, tcpa);
1939 if (CONFIG(TPM2)) {
1940 printk(BIOS_DEBUG, "ACPI: * TPM2\n");
1941 tpm2 = (acpi_tpm2_t *)current;
1942 acpi_create_tpm2(tpm2);
1943 if (tpm2->header.length >= sizeof(acpi_tpm2_t)) {
1944 current += tpm2->header.length;
1945 current = acpi_align_current(current);
1946 acpi_add_table(rsdp, tpm2);
1950 if (CONFIG(ACPI_LPIT)) {
1951 printk(BIOS_DEBUG, "ACPI: * LPIT\n");
1953 lpit = (acpi_lpit_t *)current;
1954 acpi_create_lpit(lpit);
1955 if (lpit->header.length >= sizeof(acpi_lpit_t)) {
1956 current += lpit->header.length;
1957 current = acpi_align_current(current);
1958 acpi_add_table(rsdp, lpit);
1962 printk(BIOS_DEBUG, "ACPI: * MADT\n");
1964 madt = (acpi_madt_t *)current;
1965 acpi_create_madt(madt);
1966 if (madt->header.length > sizeof(acpi_madt_t)) {
1967 current += madt->header.length;
1968 acpi_add_table(rsdp, madt);
1971 current = acpi_align_current(current);
1973 if (CONFIG(ACPI_BERT)) {
1974 void *region;
1975 size_t size;
1976 bert = (acpi_bert_t *)current;
1977 if (acpi_soc_get_bert_region(&region, &size) == CB_SUCCESS) {
1978 printk(BIOS_DEBUG, "ACPI: * BERT\n");
1979 acpi_write_bert(bert, (uintptr_t)region, size);
1980 if (bert->header.length >= sizeof(acpi_bert_t)) {
1981 current += bert->header.length;
1982 acpi_add_table(rsdp, bert);
1984 current = acpi_align_current(current);
1988 printk(BIOS_DEBUG, "current = %lx\n", current);
1990 for (dev = all_devices; dev; dev = dev->next) {
1991 if (dev->ops && dev->ops->write_acpi_tables) {
1992 current = dev->ops->write_acpi_tables(dev, current,
1993 rsdp);
1994 current = acpi_align_current(current);
1998 printk(BIOS_INFO, "ACPI: done.\n");
1999 return current;
2002 static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp)
2004 if (strncmp((char *)rsdp, RSDP_SIG, sizeof(RSDP_SIG) - 1) != 0)
2005 return NULL;
2007 printk(BIOS_DEBUG, "Looking on %p for valid checksum\n", rsdp);
2009 if (acpi_checksum((void *)rsdp, 20) != 0)
2010 return NULL;
2011 printk(BIOS_DEBUG, "Checksum 1 passed\n");
2013 if ((rsdp->revision > 1) && (acpi_checksum((void *)rsdp,
2014 rsdp->length) != 0))
2015 return NULL;
2016 printk(BIOS_DEBUG, "Checksum 2 passed all OK\n");
2018 return rsdp;
2021 void *acpi_find_wakeup_vector(void)
2023 char *p, *end;
2024 acpi_rsdt_t *rsdt;
2025 acpi_facs_t *facs;
2026 acpi_fadt_t *fadt = NULL;
2027 acpi_rsdp_t *rsdp = NULL;
2028 void *wake_vec;
2029 int i;
2031 if (!acpi_is_wakeup_s3())
2032 return NULL;
2034 printk(BIOS_DEBUG, "Trying to find the wakeup vector...\n");
2036 /* Find RSDP. */
2037 for (p = (char *)0xe0000; p < (char *)0xfffff; p += 16) {
2038 rsdp = valid_rsdp((acpi_rsdp_t *)p);
2039 if (rsdp)
2040 break;
2043 if (rsdp == NULL) {
2044 printk(BIOS_ALERT,
2045 "No RSDP found, wake up from S3 not possible.\n");
2046 return NULL;
2049 printk(BIOS_DEBUG, "RSDP found at %p\n", rsdp);
2050 rsdt = (acpi_rsdt_t *)(uintptr_t)rsdp->rsdt_address;
2052 end = (char *)rsdt + rsdt->header.length;
2053 printk(BIOS_DEBUG, "RSDT found at %p ends at %p\n", rsdt, end);
2055 for (i = 0; ((char *)&rsdt->entry[i]) < end; i++) {
2056 fadt = (acpi_fadt_t *)(uintptr_t)rsdt->entry[i];
2057 if (strncmp((char *)fadt, "FACP", 4) == 0)
2058 break;
2059 fadt = NULL;
2062 if (fadt == NULL) {
2063 printk(BIOS_ALERT,
2064 "No FADT found, wake up from S3 not possible.\n");
2065 return NULL;
2068 printk(BIOS_DEBUG, "FADT found at %p\n", fadt);
2069 facs = (acpi_facs_t *)(uintptr_t)fadt->firmware_ctrl;
2071 if (facs == NULL) {
2072 printk(BIOS_ALERT,
2073 "No FACS found, wake up from S3 not possible.\n");
2074 return NULL;
2077 printk(BIOS_DEBUG, "FACS found at %p\n", facs);
2078 wake_vec = (void *)(uintptr_t)facs->firmware_waking_vector;
2079 printk(BIOS_DEBUG, "OS waking vector is %p\n", wake_vec);
2081 return wake_vec;
2084 __weak int acpi_get_gpe(int gpe)
2086 return -1; /* implemented by SOC */
2089 u8 get_acpi_fadt_minor_version(void)
2091 return ACPI_FADT_MINOR_VERSION_0;
2094 int get_acpi_table_revision(enum acpi_tables table)
2096 switch (table) {
2097 case FADT:
2098 return ACPI_FADT_REV_ACPI_6;
2099 case MADT: /* ACPI 3.0: 2, ACPI 4.0/5.0: 3, ACPI 6.2b/6.3: 5 */
2100 return 3;
2101 case MCFG:
2102 return 1;
2103 case TCPA:
2104 return 2;
2105 case TPM2:
2106 return 4;
2107 case SSDT: /* ACPI 3.0 up to 6.3: 2 */
2108 return 2;
2109 case SRAT: /* ACPI 2.0: 1, ACPI 3.0: 2, ACPI 4.0 up to 6.4: 3 */
2110 return 3;
2111 case HMAT: /* ACPI 6.4: 2 */
2112 return 2;
2113 case DMAR:
2114 return 1;
2115 case SLIT: /* ACPI 2.0 up to 6.3: 1 */
2116 return 1;
2117 case SPMI: /* IMPI 2.0 */
2118 return 5;
2119 case HPET: /* Currently 1. Table added in ACPI 2.0. */
2120 return 1;
2121 case VFCT: /* ACPI 2.0/3.0/4.0: 1 */
2122 return 1;
2123 case IVRS:
2124 return IVRS_FORMAT_MIXED;
2125 case DBG2:
2126 return 0;
2127 case FACS: /* ACPI 2.0/3.0: 1, ACPI 4.0 up to 6.3: 2 */
2128 return 1;
2129 case RSDT: /* ACPI 1.0 up to 6.3: 1 */
2130 return 1;
2131 case XSDT: /* ACPI 2.0 up to 6.3: 1 */
2132 return 1;
2133 case RSDP: /* ACPI 2.0 up to 6.3: 2 */
2134 return 2;
2135 case EINJ:
2136 return 1;
2137 case HEST:
2138 return 1;
2139 case NHLT:
2140 return 5;
2141 case BERT:
2142 return 1;
2143 case CEDT: /* CXL 3.0 section 9.17.1 */
2144 return 1;
2145 case CRAT:
2146 return 1;
2147 case LPIT: /* ACPI 5.1 up to 6.3: 0 */
2148 return 0;
2149 default:
2150 return -1;
2152 return -1;