2 * Based on acpi.c from coreboot
4 * Copyright (C) 2015, Saket Sinha <saket.sinha89@gmail.com>
6 * SPDX-License-Identifier: GPL-2.0+
12 #include <dm/uclass-internal.h>
14 #include <asm/acpi_table.h>
16 #include <asm/ioapic.h>
17 #include <asm/lapic.h>
18 #include <asm/tables.h>
22 * IASL compiles the dsdt entries and
23 * writes the hex values to AmlCode array.
24 * CamelCase cannot be handled here.
26 extern const unsigned char AmlCode[];
29 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
32 static void acpi_add_table(struct acpi_rsdp *rsdp, void *table)
35 struct acpi_rsdt *rsdt;
36 struct acpi_xsdt *xsdt = NULL;
38 /* The RSDT is mandatory while the XSDT is not */
39 rsdt = (struct acpi_rsdt *)rsdp->rsdt_address;
41 if (rsdp->xsdt_address)
42 xsdt = (struct acpi_xsdt *)((u32)rsdp->xsdt_address);
44 /* This should always be MAX_ACPI_TABLES */
45 entries_num = ARRAY_SIZE(rsdt->entry);
47 for (i = 0; i < entries_num; i++) {
48 if (rsdt->entry[i] == 0)
52 if (i >= entries_num) {
53 debug("ACPI: Error: too many tables.\n");
57 /* Add table to the RSDT */
58 rsdt->entry[i] = (u32)table;
60 /* Fix RSDT length or the kernel will assume invalid entries */
61 rsdt->header.length = sizeof(acpi_header_t) + (sizeof(u32) * (i + 1));
63 /* Re-calculate checksum */
64 rsdt->header.checksum = 0;
65 rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
69 * And now the same thing for the XSDT. We use the same index as for
70 * now we want the XSDT and RSDT to always be in sync in U-Boot
73 /* Add table to the XSDT */
74 xsdt->entry[i] = (u64)(u32)table;
77 xsdt->header.length = sizeof(acpi_header_t) +
78 (sizeof(u64) * (i + 1));
80 /* Re-calculate checksum */
81 xsdt->header.checksum = 0;
82 xsdt->header.checksum = table_compute_checksum((u8 *)xsdt,
87 static int acpi_create_madt_lapic(struct acpi_madt_lapic *lapic,
90 lapic->type = LOCALAPIC; /* Local APIC structure */
91 lapic->length = sizeof(struct acpi_madt_lapic);
92 lapic->flags = LOCAL_APIC_FLAG_ENABLED; /* Processor/LAPIC enabled */
93 lapic->processor_id = cpu;
94 lapic->apic_id = apic;
99 unsigned long acpi_create_madt_lapics(unsigned long current)
103 for (uclass_find_first_device(UCLASS_CPU, &dev);
105 uclass_find_next_device(&dev)) {
106 struct cpu_platdata *plat = dev_get_parent_platdata(dev);
108 current += acpi_create_madt_lapic(
109 (struct acpi_madt_lapic *)current,
110 plat->cpu_id, plat->cpu_id);
115 int acpi_create_madt_ioapic(struct acpi_madt_ioapic *ioapic, u8 id, u32 addr,
118 ioapic->type = IOAPIC;
119 ioapic->length = sizeof(struct acpi_madt_ioapic);
120 ioapic->reserved = 0x00;
121 ioapic->gsi_base = gsi_base;
122 ioapic->ioapic_id = id;
123 ioapic->ioapic_addr = addr;
125 return ioapic->length;
128 int acpi_create_madt_irqoverride(struct acpi_madt_irqoverride *irqoverride,
129 u8 bus, u8 source, u32 gsirq, u16 flags)
131 irqoverride->type = IRQSOURCEOVERRIDE;
132 irqoverride->length = sizeof(struct acpi_madt_irqoverride);
133 irqoverride->bus = bus;
134 irqoverride->source = source;
135 irqoverride->gsirq = gsirq;
136 irqoverride->flags = flags;
138 return irqoverride->length;
141 int acpi_create_madt_lapic_nmi(struct acpi_madt_lapic_nmi *lapic_nmi,
142 u8 cpu, u16 flags, u8 lint)
144 lapic_nmi->type = LOCALNMITYPE;
145 lapic_nmi->length = sizeof(struct acpi_madt_lapic_nmi);
146 lapic_nmi->flags = flags;
147 lapic_nmi->processor_id = cpu;
148 lapic_nmi->lint = lint;
150 return lapic_nmi->length;
153 static void fill_header(acpi_header_t *header, char *signature, int length)
155 memcpy(header->signature, signature, length);
156 memcpy(header->oem_id, OEM_ID, 6);
157 memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
158 memcpy(header->asl_compiler_id, ASLC, 4);
161 static void acpi_create_madt(struct acpi_madt *madt)
163 acpi_header_t *header = &(madt->header);
164 unsigned long current = (unsigned long)madt + sizeof(struct acpi_madt);
166 memset((void *)madt, 0, sizeof(struct acpi_madt));
168 /* Fill out header fields */
169 fill_header(header, "APIC", 4);
170 header->length = sizeof(struct acpi_madt);
172 /* ACPI 1.0/2.0: 1, ACPI 3.0: 2, ACPI 4.0: 3 */
173 header->revision = ACPI_REV_ACPI_2_0;
175 madt->lapic_addr = LAPIC_DEFAULT_BASE;
176 madt->flags = PCAT_COMPAT;
178 current = acpi_fill_madt(current);
180 /* (Re)calculate length and checksum */
181 header->length = current - (unsigned long)madt;
183 header->checksum = table_compute_checksum((void *)madt, header->length);
186 static int acpi_create_mcfg_mmconfig(struct acpi_mcfg_mmconfig *mmconfig,
187 u32 base, u16 seg_nr, u8 start, u8 end)
189 memset(mmconfig, 0, sizeof(*mmconfig));
190 mmconfig->base_address = base;
191 mmconfig->base_reserved = 0;
192 mmconfig->pci_segment_group_number = seg_nr;
193 mmconfig->start_bus_number = start;
194 mmconfig->end_bus_number = end;
196 return sizeof(struct acpi_mcfg_mmconfig);
199 static unsigned long acpi_fill_mcfg(unsigned long current)
201 current += acpi_create_mcfg_mmconfig
202 ((struct acpi_mcfg_mmconfig *)current,
203 CONFIG_PCIE_ECAM_BASE, 0x0, 0x0, 255);
208 /* MCFG is defined in the PCI Firmware Specification 3.0 */
209 static void acpi_create_mcfg(struct acpi_mcfg *mcfg)
211 acpi_header_t *header = &(mcfg->header);
212 unsigned long current = (unsigned long)mcfg + sizeof(struct acpi_mcfg);
214 memset((void *)mcfg, 0, sizeof(struct acpi_mcfg));
216 /* Fill out header fields */
217 fill_header(header, "MCFG", 4);
218 header->length = sizeof(struct acpi_mcfg);
220 /* ACPI 1.0/2.0: 1, ACPI 3.0: 2, ACPI 4.0: 3 */
221 header->revision = ACPI_REV_ACPI_2_0;
223 current = acpi_fill_mcfg(current);
225 /* (Re)calculate length and checksum */
226 header->length = current - (unsigned long)mcfg;
227 header->checksum = table_compute_checksum((void *)mcfg, header->length);
230 static void acpi_create_facs(struct acpi_facs *facs)
232 memset((void *)facs, 0, sizeof(struct acpi_facs));
234 memcpy(facs->signature, "FACS", 4);
235 facs->length = sizeof(struct acpi_facs);
236 facs->hardware_signature = 0;
237 facs->firmware_waking_vector = 0;
238 facs->global_lock = 0;
240 facs->x_firmware_waking_vector_l = 0;
241 facs->x_firmware_waking_vector_h = 0;
242 facs->version = 1; /* ACPI 1.0: 0, ACPI 2.0/3.0: 1, ACPI 4.0: 2 */
245 static void acpi_write_rsdt(struct acpi_rsdt *rsdt)
247 acpi_header_t *header = &(rsdt->header);
249 /* Fill out header fields */
250 fill_header(header, "RSDT", 4);
251 header->length = sizeof(struct acpi_rsdt);
253 /* ACPI 1.0/2.0: 1, ACPI 3.0: 2, ACPI 4.0: 3 */
254 header->revision = ACPI_REV_ACPI_2_0;
256 /* Entries are filled in later, we come with an empty set */
259 header->checksum = table_compute_checksum((void *)rsdt,
260 sizeof(struct acpi_rsdt));
263 static void acpi_write_xsdt(struct acpi_xsdt *xsdt)
265 acpi_header_t *header = &(xsdt->header);
267 /* Fill out header fields */
268 fill_header(header, "XSDT", 4);
269 header->length = sizeof(struct acpi_xsdt);
271 /* ACPI 1.0/2.0: 1, ACPI 3.0: 2, ACPI 4.0: 3 */
272 header->revision = ACPI_REV_ACPI_2_0;
274 /* Entries are filled in later, we come with an empty set */
277 header->checksum = table_compute_checksum((void *)xsdt,
278 sizeof(struct acpi_xsdt));
281 static void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt,
282 struct acpi_xsdt *xsdt)
284 memset(rsdp, 0, sizeof(struct acpi_rsdp));
286 memcpy(rsdp->signature, RSDP_SIG, 8);
287 memcpy(rsdp->oem_id, OEM_ID, 6);
289 rsdp->length = sizeof(struct acpi_rsdp);
290 rsdp->rsdt_address = (u32)rsdt;
293 * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2
295 * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
296 * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
300 rsdp->revision = ACPI_RSDP_REV_ACPI_1_0;
302 rsdp->xsdt_address = (u64)(u32)xsdt;
303 rsdp->revision = ACPI_RSDP_REV_ACPI_2_0;
306 /* Calculate checksums */
307 rsdp->checksum = table_compute_checksum((void *)rsdp, 20);
308 rsdp->ext_checksum = table_compute_checksum((void *)rsdp,
309 sizeof(struct acpi_rsdp));
312 static void acpi_create_ssdt_generator(acpi_header_t *ssdt,
313 const char *oem_table_id)
315 unsigned long current = (unsigned long)ssdt + sizeof(acpi_header_t);
317 memset((void *)ssdt, 0, sizeof(acpi_header_t));
319 memcpy(&ssdt->signature, "SSDT", 4);
320 /* Access size in ACPI 2.0c/3.0/4.0/5.0 */
321 ssdt->revision = ACPI_REV_ACPI_3_0;
322 memcpy(&ssdt->oem_id, OEM_ID, 6);
323 memcpy(&ssdt->oem_table_id, oem_table_id, 8);
324 ssdt->oem_revision = OEM_REVISION;
325 memcpy(&ssdt->asl_compiler_id, ASLC, 4);
326 ssdt->asl_compiler_revision = ASL_COMPILER_REVISION;
327 ssdt->length = sizeof(acpi_header_t);
329 /* (Re)calculate length and checksum */
330 ssdt->length = current - (unsigned long)ssdt;
331 ssdt->checksum = table_compute_checksum((void *)ssdt, ssdt->length);
335 * QEMU's version of write_acpi_tables is defined in
336 * arch/x86/cpu/qemu/fw_cfg.c
338 u32 write_acpi_tables(u32 start)
341 struct acpi_rsdp *rsdp;
342 struct acpi_rsdt *rsdt;
343 struct acpi_xsdt *xsdt;
344 struct acpi_facs *facs;
346 struct acpi_fadt *fadt;
347 struct acpi_mcfg *mcfg;
348 struct acpi_madt *madt;
353 /* Align ACPI tables to 16byte */
354 current = ALIGN(current, 16);
356 debug("ACPI: Writing ACPI tables at %lx.\n", start);
358 /* We need at least an RSDP and an RSDT Table */
359 rsdp = (struct acpi_rsdp *)current;
360 current += sizeof(struct acpi_rsdp);
361 current = ALIGN(current, 16);
362 rsdt = (struct acpi_rsdt *)current;
363 current += sizeof(struct acpi_rsdt);
364 current = ALIGN(current, 16);
365 xsdt = (struct acpi_xsdt *)current;
366 current += sizeof(struct acpi_xsdt);
367 current = ALIGN(current, 16);
369 /* clear all table memory */
370 memset((void *)start, 0, current - start);
372 acpi_write_rsdp(rsdp, rsdt, xsdt);
373 acpi_write_rsdt(rsdt);
374 acpi_write_xsdt(xsdt);
376 debug("ACPI: * FACS\n");
377 facs = (struct acpi_facs *)current;
378 current += sizeof(struct acpi_facs);
379 current = ALIGN(current, 16);
381 acpi_create_facs(facs);
383 debug("ACPI: * DSDT\n");
384 dsdt = (acpi_header_t *)current;
385 memcpy(dsdt, &AmlCode, sizeof(acpi_header_t));
386 if (dsdt->length >= sizeof(acpi_header_t)) {
387 current += sizeof(acpi_header_t);
388 memcpy((char *)current,
389 (char *)&AmlCode + sizeof(acpi_header_t),
390 dsdt->length - sizeof(acpi_header_t));
391 current += dsdt->length - sizeof(acpi_header_t);
393 /* (Re)calculate length and checksum */
394 dsdt->length = current - (unsigned long)dsdt;
396 dsdt->checksum = table_compute_checksum((void *)dsdt,
399 current = ALIGN(current, 16);
401 debug("ACPI: * FADT\n");
402 fadt = (struct acpi_fadt *)current;
403 current += sizeof(struct acpi_fadt);
404 current = ALIGN(current, 16);
405 acpi_create_fadt(fadt, facs, dsdt);
406 acpi_add_table(rsdp, fadt);
408 debug("ACPI: * MCFG\n");
409 mcfg = (struct acpi_mcfg *)current;
410 acpi_create_mcfg(mcfg);
411 if (mcfg->header.length > sizeof(struct acpi_mcfg)) {
412 current += mcfg->header.length;
413 current = ALIGN(current, 16);
414 acpi_add_table(rsdp, mcfg);
417 debug("ACPI: * MADT\n");
418 madt = (struct acpi_madt *)current;
419 acpi_create_madt(madt);
420 if (madt->header.length > sizeof(struct acpi_madt)) {
421 current += madt->header.length;
422 acpi_add_table(rsdp, madt);
424 current = ALIGN(current, 16);
426 debug("ACPI: * SSDT\n");
427 ssdt = (acpi_header_t *)current;
428 acpi_create_ssdt_generator(ssdt, ACPI_TABLE_CREATOR);
429 if (ssdt->length > sizeof(acpi_header_t)) {
430 current += ssdt->length;
431 acpi_add_table(rsdp, ssdt);
432 current = ALIGN(current, 16);
435 debug("current = %lx\n", current);
437 debug("ACPI: done.\n");