2 * Based on acpi.c from coreboot
4 * Copyright (C) 2015, Saket Sinha <saket.sinha89@gmail.com>
5 * Copyright (C) 2016, Bin Meng <bmeng.cn@gmail.com>
7 * SPDX-License-Identifier: GPL-2.0+
13 #include <dm/uclass-internal.h>
14 #include <asm/acpi_table.h>
15 #include <asm/lapic.h>
16 #include <asm/tables.h>
19 * IASL compiles the dsdt entries and writes the hex values
20 * to a C array AmlCode[] (see dsdt.c).
22 extern const unsigned char AmlCode[];
24 static void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt,
25 struct acpi_xsdt *xsdt)
27 memset(rsdp, 0, sizeof(struct acpi_rsdp));
29 memcpy(rsdp->signature, RSDP_SIG, 8);
30 memcpy(rsdp->oem_id, OEM_ID, 6);
32 rsdp->length = sizeof(struct acpi_rsdp);
33 rsdp->rsdt_address = (u32)rsdt;
36 * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2
38 * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
39 * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
43 rsdp->revision = ACPI_RSDP_REV_ACPI_1_0;
45 rsdp->xsdt_address = (u64)(u32)xsdt;
46 rsdp->revision = ACPI_RSDP_REV_ACPI_2_0;
49 /* Calculate checksums */
50 rsdp->checksum = table_compute_checksum((void *)rsdp, 20);
51 rsdp->ext_checksum = table_compute_checksum((void *)rsdp,
52 sizeof(struct acpi_rsdp));
55 void acpi_fill_header(struct acpi_table_header *header, char *signature)
57 memcpy(header->signature, signature, 4);
58 memcpy(header->oem_id, OEM_ID, 6);
59 memcpy(header->oem_table_id, OEM_TABLE_ID, 8);
60 memcpy(header->aslc_id, ASLC_ID, 4);
63 static void acpi_write_rsdt(struct acpi_rsdt *rsdt)
65 struct acpi_table_header *header = &(rsdt->header);
67 /* Fill out header fields */
68 acpi_fill_header(header, "RSDT");
69 header->length = sizeof(struct acpi_rsdt);
72 /* Entries are filled in later, we come with an empty set */
75 header->checksum = table_compute_checksum((void *)rsdt,
76 sizeof(struct acpi_rsdt));
79 static void acpi_write_xsdt(struct acpi_xsdt *xsdt)
81 struct acpi_table_header *header = &(xsdt->header);
83 /* Fill out header fields */
84 acpi_fill_header(header, "XSDT");
85 header->length = sizeof(struct acpi_xsdt);
88 /* Entries are filled in later, we come with an empty set */
91 header->checksum = table_compute_checksum((void *)xsdt,
92 sizeof(struct acpi_xsdt));
96 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
99 static void acpi_add_table(struct acpi_rsdp *rsdp, void *table)
102 struct acpi_rsdt *rsdt;
103 struct acpi_xsdt *xsdt = NULL;
105 /* The RSDT is mandatory while the XSDT is not */
106 rsdt = (struct acpi_rsdt *)rsdp->rsdt_address;
108 if (rsdp->xsdt_address)
109 xsdt = (struct acpi_xsdt *)((u32)rsdp->xsdt_address);
111 /* This should always be MAX_ACPI_TABLES */
112 entries_num = ARRAY_SIZE(rsdt->entry);
114 for (i = 0; i < entries_num; i++) {
115 if (rsdt->entry[i] == 0)
119 if (i >= entries_num) {
120 debug("ACPI: Error: too many tables\n");
124 /* Add table to the RSDT */
125 rsdt->entry[i] = (u32)table;
127 /* Fix RSDT length or the kernel will assume invalid entries */
128 rsdt->header.length = sizeof(struct acpi_table_header) +
129 (sizeof(u32) * (i + 1));
131 /* Re-calculate checksum */
132 rsdt->header.checksum = 0;
133 rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
134 rsdt->header.length);
137 * And now the same thing for the XSDT. We use the same index as for
138 * now we want the XSDT and RSDT to always be in sync in U-Boot
141 /* Add table to the XSDT */
142 xsdt->entry[i] = (u64)(u32)table;
144 /* Fix XSDT length */
145 xsdt->header.length = sizeof(struct acpi_table_header) +
146 (sizeof(u64) * (i + 1));
148 /* Re-calculate checksum */
149 xsdt->header.checksum = 0;
150 xsdt->header.checksum = table_compute_checksum((u8 *)xsdt,
151 xsdt->header.length);
155 static void acpi_create_facs(struct acpi_facs *facs)
157 memset((void *)facs, 0, sizeof(struct acpi_facs));
159 memcpy(facs->signature, "FACS", 4);
160 facs->length = sizeof(struct acpi_facs);
161 facs->hardware_signature = 0;
162 facs->firmware_waking_vector = 0;
163 facs->global_lock = 0;
165 facs->x_firmware_waking_vector_l = 0;
166 facs->x_firmware_waking_vector_h = 0;
170 static int acpi_create_madt_lapic(struct acpi_madt_lapic *lapic,
173 lapic->type = ACPI_APIC_LAPIC;
174 lapic->length = sizeof(struct acpi_madt_lapic);
175 lapic->flags = LOCAL_APIC_FLAG_ENABLED;
176 lapic->processor_id = cpu;
177 lapic->apic_id = apic;
179 return lapic->length;
182 u32 acpi_create_madt_lapics(u32 current)
186 for (uclass_find_first_device(UCLASS_CPU, &dev);
188 uclass_find_next_device(&dev)) {
189 struct cpu_platdata *plat = dev_get_parent_platdata(dev);
191 current += acpi_create_madt_lapic(
192 (struct acpi_madt_lapic *)current,
193 plat->cpu_id, plat->cpu_id);
199 int acpi_create_madt_ioapic(struct acpi_madt_ioapic *ioapic, u8 id,
200 u32 addr, u32 gsi_base)
202 ioapic->type = ACPI_APIC_IOAPIC;
203 ioapic->length = sizeof(struct acpi_madt_ioapic);
204 ioapic->reserved = 0x00;
205 ioapic->gsi_base = gsi_base;
206 ioapic->ioapic_id = id;
207 ioapic->ioapic_addr = addr;
209 return ioapic->length;
212 int acpi_create_madt_irqoverride(struct acpi_madt_irqoverride *irqoverride,
213 u8 bus, u8 source, u32 gsirq, u16 flags)
215 irqoverride->type = ACPI_APIC_IRQ_SRC_OVERRIDE;
216 irqoverride->length = sizeof(struct acpi_madt_irqoverride);
217 irqoverride->bus = bus;
218 irqoverride->source = source;
219 irqoverride->gsirq = gsirq;
220 irqoverride->flags = flags;
222 return irqoverride->length;
225 int acpi_create_madt_lapic_nmi(struct acpi_madt_lapic_nmi *lapic_nmi,
226 u8 cpu, u16 flags, u8 lint)
228 lapic_nmi->type = ACPI_APIC_LAPIC_NMI;
229 lapic_nmi->length = sizeof(struct acpi_madt_lapic_nmi);
230 lapic_nmi->flags = flags;
231 lapic_nmi->processor_id = cpu;
232 lapic_nmi->lint = lint;
234 return lapic_nmi->length;
237 static void acpi_create_madt(struct acpi_madt *madt)
239 struct acpi_table_header *header = &(madt->header);
240 u32 current = (u32)madt + sizeof(struct acpi_madt);
242 memset((void *)madt, 0, sizeof(struct acpi_madt));
244 /* Fill out header fields */
245 acpi_fill_header(header, "APIC");
246 header->length = sizeof(struct acpi_madt);
247 header->revision = 4;
249 madt->lapic_addr = LAPIC_DEFAULT_BASE;
250 madt->flags = ACPI_MADT_PCAT_COMPAT;
252 current = acpi_fill_madt(current);
254 /* (Re)calculate length and checksum */
255 header->length = current - (u32)madt;
257 header->checksum = table_compute_checksum((void *)madt, header->length);
260 static int acpi_create_mcfg_mmconfig(struct acpi_mcfg_mmconfig *mmconfig,
261 u32 base, u16 seg_nr, u8 start, u8 end)
263 memset(mmconfig, 0, sizeof(*mmconfig));
264 mmconfig->base_address_l = base;
265 mmconfig->base_address_h = 0;
266 mmconfig->pci_segment_group_number = seg_nr;
267 mmconfig->start_bus_number = start;
268 mmconfig->end_bus_number = end;
270 return sizeof(struct acpi_mcfg_mmconfig);
273 static u32 acpi_fill_mcfg(u32 current)
275 current += acpi_create_mcfg_mmconfig
276 ((struct acpi_mcfg_mmconfig *)current,
277 CONFIG_PCIE_ECAM_BASE, 0x0, 0x0, 255);
282 /* MCFG is defined in the PCI Firmware Specification 3.0 */
283 static void acpi_create_mcfg(struct acpi_mcfg *mcfg)
285 struct acpi_table_header *header = &(mcfg->header);
286 u32 current = (u32)mcfg + sizeof(struct acpi_mcfg);
288 memset((void *)mcfg, 0, sizeof(struct acpi_mcfg));
290 /* Fill out header fields */
291 acpi_fill_header(header, "MCFG");
292 header->length = sizeof(struct acpi_mcfg);
293 header->revision = 1;
295 current = acpi_fill_mcfg(current);
297 /* (Re)calculate length and checksum */
298 header->length = current - (u32)mcfg;
299 header->checksum = table_compute_checksum((void *)mcfg, header->length);
303 * QEMU's version of write_acpi_tables is defined in
304 * arch/x86/cpu/qemu/fw_cfg.c
306 u32 write_acpi_tables(u32 start)
309 struct acpi_rsdp *rsdp;
310 struct acpi_rsdt *rsdt;
311 struct acpi_xsdt *xsdt;
312 struct acpi_facs *facs;
313 struct acpi_table_header *dsdt;
314 struct acpi_fadt *fadt;
315 struct acpi_mcfg *mcfg;
316 struct acpi_madt *madt;
320 /* Align ACPI tables to 16 byte */
321 current = ALIGN(current, 16);
323 debug("ACPI: Writing ACPI tables at %x\n", start);
325 /* We need at least an RSDP and an RSDT Table */
326 rsdp = (struct acpi_rsdp *)current;
327 current += sizeof(struct acpi_rsdp);
328 current = ALIGN(current, 16);
329 rsdt = (struct acpi_rsdt *)current;
330 current += sizeof(struct acpi_rsdt);
331 current = ALIGN(current, 16);
332 xsdt = (struct acpi_xsdt *)current;
333 current += sizeof(struct acpi_xsdt);
335 * Per ACPI spec, the FACS table address must be aligned to a 64 byte
336 * boundary (Windows checks this, but Linux does not).
338 current = ALIGN(current, 64);
340 /* clear all table memory */
341 memset((void *)start, 0, current - start);
343 acpi_write_rsdp(rsdp, rsdt, xsdt);
344 acpi_write_rsdt(rsdt);
345 acpi_write_xsdt(xsdt);
347 debug("ACPI: * FACS\n");
348 facs = (struct acpi_facs *)current;
349 current += sizeof(struct acpi_facs);
350 current = ALIGN(current, 16);
352 acpi_create_facs(facs);
354 debug("ACPI: * DSDT\n");
355 dsdt = (struct acpi_table_header *)current;
356 memcpy(dsdt, &AmlCode, sizeof(struct acpi_table_header));
357 if (dsdt->length >= sizeof(struct acpi_table_header)) {
358 current += sizeof(struct acpi_table_header);
359 memcpy((char *)current,
360 (char *)&AmlCode + sizeof(struct acpi_table_header),
361 dsdt->length - sizeof(struct acpi_table_header));
362 current += dsdt->length - sizeof(struct acpi_table_header);
364 /* (Re)calculate length and checksum */
365 dsdt->length = current - (u32)dsdt;
367 dsdt->checksum = table_compute_checksum((void *)dsdt,
370 current = ALIGN(current, 16);
372 debug("ACPI: * FADT\n");
373 fadt = (struct acpi_fadt *)current;
374 current += sizeof(struct acpi_fadt);
375 current = ALIGN(current, 16);
376 acpi_create_fadt(fadt, facs, dsdt);
377 acpi_add_table(rsdp, fadt);
379 debug("ACPI: * MADT\n");
380 madt = (struct acpi_madt *)current;
381 acpi_create_madt(madt);
382 if (madt->header.length > sizeof(struct acpi_madt)) {
383 current += madt->header.length;
384 acpi_add_table(rsdp, madt);
386 current = ALIGN(current, 16);
388 debug("ACPI: * MCFG\n");
389 mcfg = (struct acpi_mcfg *)current;
390 acpi_create_mcfg(mcfg);
391 if (mcfg->header.length > sizeof(struct acpi_mcfg)) {
392 current += mcfg->header.length;
393 current = ALIGN(current, 16);
394 acpi_add_table(rsdp, mcfg);
397 debug("current = %x\n", current);
399 debug("ACPI: done\n");