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>
13 #include <asm/acpi_table.h>
14 #include <asm/lapic.h>
15 #include <asm/tables.h>
18 * IASL compiles the dsdt entries and writes the hex values
19 * to a C array AmlCode[] (see dsdt.c).
21 extern const unsigned char AmlCode[];
24 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
27 static void acpi_add_table(struct acpi_rsdp *rsdp, void *table)
30 struct acpi_rsdt *rsdt;
31 struct acpi_xsdt *xsdt = NULL;
33 /* The RSDT is mandatory while the XSDT is not */
34 rsdt = (struct acpi_rsdt *)rsdp->rsdt_address;
36 if (rsdp->xsdt_address)
37 xsdt = (struct acpi_xsdt *)((u32)rsdp->xsdt_address);
39 /* This should always be MAX_ACPI_TABLES */
40 entries_num = ARRAY_SIZE(rsdt->entry);
42 for (i = 0; i < entries_num; i++) {
43 if (rsdt->entry[i] == 0)
47 if (i >= entries_num) {
48 debug("ACPI: Error: too many tables\n");
52 /* Add table to the RSDT */
53 rsdt->entry[i] = (u32)table;
55 /* Fix RSDT length or the kernel will assume invalid entries */
56 rsdt->header.length = sizeof(struct acpi_table_header) +
57 (sizeof(u32) * (i + 1));
59 /* Re-calculate checksum */
60 rsdt->header.checksum = 0;
61 rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
65 * And now the same thing for the XSDT. We use the same index as for
66 * now we want the XSDT and RSDT to always be in sync in U-Boot
69 /* Add table to the XSDT */
70 xsdt->entry[i] = (u64)(u32)table;
73 xsdt->header.length = sizeof(struct acpi_table_header) +
74 (sizeof(u64) * (i + 1));
76 /* Re-calculate checksum */
77 xsdt->header.checksum = 0;
78 xsdt->header.checksum = table_compute_checksum((u8 *)xsdt,
83 static int acpi_create_madt_lapic(struct acpi_madt_lapic *lapic,
86 lapic->type = ACPI_APIC_LAPIC; /* Local APIC structure */
87 lapic->length = sizeof(struct acpi_madt_lapic);
88 lapic->flags = LOCAL_APIC_FLAG_ENABLED; /* Processor/LAPIC enabled */
89 lapic->processor_id = cpu;
90 lapic->apic_id = apic;
95 unsigned long acpi_create_madt_lapics(unsigned long current)
99 for (uclass_find_first_device(UCLASS_CPU, &dev);
101 uclass_find_next_device(&dev)) {
102 struct cpu_platdata *plat = dev_get_parent_platdata(dev);
104 current += acpi_create_madt_lapic(
105 (struct acpi_madt_lapic *)current,
106 plat->cpu_id, plat->cpu_id);
111 int acpi_create_madt_ioapic(struct acpi_madt_ioapic *ioapic, u8 id, u32 addr,
114 ioapic->type = ACPI_APIC_IOAPIC;
115 ioapic->length = sizeof(struct acpi_madt_ioapic);
116 ioapic->reserved = 0x00;
117 ioapic->gsi_base = gsi_base;
118 ioapic->ioapic_id = id;
119 ioapic->ioapic_addr = addr;
121 return ioapic->length;
124 int acpi_create_madt_irqoverride(struct acpi_madt_irqoverride *irqoverride,
125 u8 bus, u8 source, u32 gsirq, u16 flags)
127 irqoverride->type = ACPI_APIC_IRQ_SRC_OVERRIDE;
128 irqoverride->length = sizeof(struct acpi_madt_irqoverride);
129 irqoverride->bus = bus;
130 irqoverride->source = source;
131 irqoverride->gsirq = gsirq;
132 irqoverride->flags = flags;
134 return irqoverride->length;
137 int acpi_create_madt_lapic_nmi(struct acpi_madt_lapic_nmi *lapic_nmi,
138 u8 cpu, u16 flags, u8 lint)
140 lapic_nmi->type = ACPI_APIC_LAPIC_NMI;
141 lapic_nmi->length = sizeof(struct acpi_madt_lapic_nmi);
142 lapic_nmi->flags = flags;
143 lapic_nmi->processor_id = cpu;
144 lapic_nmi->lint = lint;
146 return lapic_nmi->length;
149 void acpi_fill_header(struct acpi_table_header *header, char *signature)
151 memcpy(header->signature, signature, 4);
152 memcpy(header->oem_id, OEM_ID, 6);
153 memcpy(header->oem_table_id, OEM_TABLE_ID, 8);
154 memcpy(header->aslc_id, ASLC_ID, 4);
157 static void acpi_create_madt(struct acpi_madt *madt)
159 struct acpi_table_header *header = &(madt->header);
160 unsigned long current = (unsigned long)madt + sizeof(struct acpi_madt);
162 memset((void *)madt, 0, sizeof(struct acpi_madt));
164 /* Fill out header fields */
165 acpi_fill_header(header, "APIC");
166 header->length = sizeof(struct acpi_madt);
168 /* ACPI 1.0/2.0: 1, ACPI 3.0: 2, ACPI 4.0: 3 */
169 header->revision = ACPI_REV_ACPI_2_0;
171 madt->lapic_addr = LAPIC_DEFAULT_BASE;
172 madt->flags = ACPI_MADT_PCAT_COMPAT;
174 current = acpi_fill_madt(current);
176 /* (Re)calculate length and checksum */
177 header->length = current - (unsigned long)madt;
179 header->checksum = table_compute_checksum((void *)madt, header->length);
182 static int acpi_create_mcfg_mmconfig(struct acpi_mcfg_mmconfig *mmconfig,
183 u32 base, u16 seg_nr, u8 start, u8 end)
185 memset(mmconfig, 0, sizeof(*mmconfig));
186 mmconfig->base_address_l = base;
187 mmconfig->base_address_h = 0;
188 mmconfig->pci_segment_group_number = seg_nr;
189 mmconfig->start_bus_number = start;
190 mmconfig->end_bus_number = end;
192 return sizeof(struct acpi_mcfg_mmconfig);
195 static unsigned long acpi_fill_mcfg(unsigned long current)
197 current += acpi_create_mcfg_mmconfig
198 ((struct acpi_mcfg_mmconfig *)current,
199 CONFIG_PCIE_ECAM_BASE, 0x0, 0x0, 255);
204 /* MCFG is defined in the PCI Firmware Specification 3.0 */
205 static void acpi_create_mcfg(struct acpi_mcfg *mcfg)
207 struct acpi_table_header *header = &(mcfg->header);
208 unsigned long current = (unsigned long)mcfg + sizeof(struct acpi_mcfg);
210 memset((void *)mcfg, 0, sizeof(struct acpi_mcfg));
212 /* Fill out header fields */
213 acpi_fill_header(header, "MCFG");
214 header->length = sizeof(struct acpi_mcfg);
216 /* ACPI 1.0/2.0: 1, ACPI 3.0: 2, ACPI 4.0: 3 */
217 header->revision = ACPI_REV_ACPI_2_0;
219 current = acpi_fill_mcfg(current);
221 /* (Re)calculate length and checksum */
222 header->length = current - (unsigned long)mcfg;
223 header->checksum = table_compute_checksum((void *)mcfg, header->length);
226 static void acpi_create_facs(struct acpi_facs *facs)
228 memset((void *)facs, 0, sizeof(struct acpi_facs));
230 memcpy(facs->signature, "FACS", 4);
231 facs->length = sizeof(struct acpi_facs);
232 facs->hardware_signature = 0;
233 facs->firmware_waking_vector = 0;
234 facs->global_lock = 0;
236 facs->x_firmware_waking_vector_l = 0;
237 facs->x_firmware_waking_vector_h = 0;
238 facs->version = 1; /* ACPI 1.0: 0, ACPI 2.0/3.0: 1, ACPI 4.0: 2 */
241 static void acpi_write_rsdt(struct acpi_rsdt *rsdt)
243 struct acpi_table_header *header = &(rsdt->header);
245 /* Fill out header fields */
246 acpi_fill_header(header, "RSDT");
247 header->length = sizeof(struct acpi_rsdt);
249 /* ACPI 1.0/2.0: 1, ACPI 3.0: 2, ACPI 4.0: 3 */
250 header->revision = ACPI_REV_ACPI_2_0;
252 /* Entries are filled in later, we come with an empty set */
255 header->checksum = table_compute_checksum((void *)rsdt,
256 sizeof(struct acpi_rsdt));
259 static void acpi_write_xsdt(struct acpi_xsdt *xsdt)
261 struct acpi_table_header *header = &(xsdt->header);
263 /* Fill out header fields */
264 acpi_fill_header(header, "XSDT");
265 header->length = sizeof(struct acpi_xsdt);
267 /* ACPI 1.0/2.0: 1, ACPI 3.0: 2, ACPI 4.0: 3 */
268 header->revision = ACPI_REV_ACPI_2_0;
270 /* Entries are filled in later, we come with an empty set */
273 header->checksum = table_compute_checksum((void *)xsdt,
274 sizeof(struct acpi_xsdt));
277 static void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt,
278 struct acpi_xsdt *xsdt)
280 memset(rsdp, 0, sizeof(struct acpi_rsdp));
282 memcpy(rsdp->signature, RSDP_SIG, 8);
283 memcpy(rsdp->oem_id, OEM_ID, 6);
285 rsdp->length = sizeof(struct acpi_rsdp);
286 rsdp->rsdt_address = (u32)rsdt;
289 * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2
291 * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
292 * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
296 rsdp->revision = ACPI_RSDP_REV_ACPI_1_0;
298 rsdp->xsdt_address = (u64)(u32)xsdt;
299 rsdp->revision = ACPI_RSDP_REV_ACPI_2_0;
302 /* Calculate checksums */
303 rsdp->checksum = table_compute_checksum((void *)rsdp, 20);
304 rsdp->ext_checksum = table_compute_checksum((void *)rsdp,
305 sizeof(struct acpi_rsdp));
309 * QEMU's version of write_acpi_tables is defined in
310 * arch/x86/cpu/qemu/fw_cfg.c
312 u32 write_acpi_tables(u32 start)
315 struct acpi_rsdp *rsdp;
316 struct acpi_rsdt *rsdt;
317 struct acpi_xsdt *xsdt;
318 struct acpi_facs *facs;
319 struct acpi_table_header *dsdt;
320 struct acpi_fadt *fadt;
321 struct acpi_mcfg *mcfg;
322 struct acpi_madt *madt;
326 /* Align ACPI tables to 16byte */
327 current = ALIGN(current, 16);
329 debug("ACPI: Writing ACPI tables at %x\n", start);
331 /* We need at least an RSDP and an RSDT Table */
332 rsdp = (struct acpi_rsdp *)current;
333 current += sizeof(struct acpi_rsdp);
334 current = ALIGN(current, 16);
335 rsdt = (struct acpi_rsdt *)current;
336 current += sizeof(struct acpi_rsdt);
337 current = ALIGN(current, 16);
338 xsdt = (struct acpi_xsdt *)current;
339 current += sizeof(struct acpi_xsdt);
340 current = ALIGN(current, 16);
342 /* clear all table memory */
343 memset((void *)start, 0, current - start);
345 acpi_write_rsdp(rsdp, rsdt, xsdt);
346 acpi_write_rsdt(rsdt);
347 acpi_write_xsdt(xsdt);
349 debug("ACPI: * FACS\n");
350 facs = (struct acpi_facs *)current;
351 current += sizeof(struct acpi_facs);
352 current = ALIGN(current, 16);
354 acpi_create_facs(facs);
356 debug("ACPI: * DSDT\n");
357 dsdt = (struct acpi_table_header *)current;
358 memcpy(dsdt, &AmlCode, sizeof(struct acpi_table_header));
359 if (dsdt->length >= sizeof(struct acpi_table_header)) {
360 current += sizeof(struct acpi_table_header);
361 memcpy((char *)current,
362 (char *)&AmlCode + sizeof(struct acpi_table_header),
363 dsdt->length - sizeof(struct acpi_table_header));
364 current += dsdt->length - sizeof(struct acpi_table_header);
366 /* (Re)calculate length and checksum */
367 dsdt->length = current - (unsigned long)dsdt;
369 dsdt->checksum = table_compute_checksum((void *)dsdt,
372 current = ALIGN(current, 16);
374 debug("ACPI: * FADT\n");
375 fadt = (struct acpi_fadt *)current;
376 current += sizeof(struct acpi_fadt);
377 current = ALIGN(current, 16);
378 acpi_create_fadt(fadt, facs, dsdt);
379 acpi_add_table(rsdp, fadt);
381 debug("ACPI: * MCFG\n");
382 mcfg = (struct acpi_mcfg *)current;
383 acpi_create_mcfg(mcfg);
384 if (mcfg->header.length > sizeof(struct acpi_mcfg)) {
385 current += mcfg->header.length;
386 current = ALIGN(current, 16);
387 acpi_add_table(rsdp, mcfg);
390 debug("ACPI: * MADT\n");
391 madt = (struct acpi_madt *)current;
392 acpi_create_madt(madt);
393 if (madt->header.length > sizeof(struct acpi_madt)) {
394 current += madt->header.length;
395 acpi_add_table(rsdp, madt);
397 current = ALIGN(current, 16);
399 debug("current = %x\n", current);
401 debug("ACPI: done\n");