blob: ffb4678e510b38e9249db3209bf0dd8666155613 [file] [log] [blame]
Saket Sinha867bcb62015-08-22 12:20:55 +05301/*
2 * Based on acpi.c from coreboot
3 *
4 * Copyright (C) 2015, Saket Sinha <saket.sinha89@gmail.com>
Bin Mengab5efd52016-05-07 07:46:25 -07005 * Copyright (C) 2016, Bin Meng <bmeng.cn@gmail.com>
Saket Sinha867bcb62015-08-22 12:20:55 +05306 *
7 * SPDX-License-Identifier: GPL-2.0+
8 */
9
10#include <common.h>
11#include <cpu.h>
12#include <dm.h>
13#include <dm/uclass-internal.h>
Saket Sinha867bcb62015-08-22 12:20:55 +053014#include <asm/acpi_table.h>
Bin Meng6aef68d2016-05-11 07:45:03 -070015#include <asm/io.h>
Saket Sinha867bcb62015-08-22 12:20:55 +053016#include <asm/lapic.h>
17#include <asm/tables.h>
Saket Sinha867bcb62015-08-22 12:20:55 +053018
19/*
Bin Mengdfbb18b2016-05-07 07:46:24 -070020 * IASL compiles the dsdt entries and writes the hex values
21 * to a C array AmlCode[] (see dsdt.c).
Saket Sinha867bcb62015-08-22 12:20:55 +053022 */
23extern const unsigned char AmlCode[];
24
Bin Mengab5efd52016-05-07 07:46:25 -070025static void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt,
26 struct acpi_xsdt *xsdt)
Saket Sinha867bcb62015-08-22 12:20:55 +053027{
Bin Mengab5efd52016-05-07 07:46:25 -070028 memset(rsdp, 0, sizeof(struct acpi_rsdp));
Saket Sinha867bcb62015-08-22 12:20:55 +053029
Bin Mengab5efd52016-05-07 07:46:25 -070030 memcpy(rsdp->signature, RSDP_SIG, 8);
31 memcpy(rsdp->oem_id, OEM_ID, 6);
Saket Sinha867bcb62015-08-22 12:20:55 +053032
Bin Mengab5efd52016-05-07 07:46:25 -070033 rsdp->length = sizeof(struct acpi_rsdp);
34 rsdp->rsdt_address = (u32)rsdt;
Saket Sinha867bcb62015-08-22 12:20:55 +053035
36 /*
Bin Mengab5efd52016-05-07 07:46:25 -070037 * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2
38 *
39 * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
40 * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
41 * revision 0)
Saket Sinha867bcb62015-08-22 12:20:55 +053042 */
Bin Mengab5efd52016-05-07 07:46:25 -070043 if (xsdt == NULL) {
44 rsdp->revision = ACPI_RSDP_REV_ACPI_1_0;
45 } else {
46 rsdp->xsdt_address = (u64)(u32)xsdt;
47 rsdp->revision = ACPI_RSDP_REV_ACPI_2_0;
Saket Sinha867bcb62015-08-22 12:20:55 +053048 }
Saket Sinha867bcb62015-08-22 12:20:55 +053049
Bin Mengab5efd52016-05-07 07:46:25 -070050 /* Calculate checksums */
51 rsdp->checksum = table_compute_checksum((void *)rsdp, 20);
52 rsdp->ext_checksum = table_compute_checksum((void *)rsdp,
53 sizeof(struct acpi_rsdp));
Saket Sinha867bcb62015-08-22 12:20:55 +053054}
55
Bin Mengdfbb18b2016-05-07 07:46:24 -070056void acpi_fill_header(struct acpi_table_header *header, char *signature)
Saket Sinha867bcb62015-08-22 12:20:55 +053057{
Bin Mengdfbb18b2016-05-07 07:46:24 -070058 memcpy(header->signature, signature, 4);
Saket Sinha867bcb62015-08-22 12:20:55 +053059 memcpy(header->oem_id, OEM_ID, 6);
Bin Meng8a8c0352016-05-07 07:46:21 -070060 memcpy(header->oem_table_id, OEM_TABLE_ID, 8);
61 memcpy(header->aslc_id, ASLC_ID, 4);
Saket Sinha867bcb62015-08-22 12:20:55 +053062}
63
Saket Sinha867bcb62015-08-22 12:20:55 +053064static void acpi_write_rsdt(struct acpi_rsdt *rsdt)
65{
Bin Meng8a8c0352016-05-07 07:46:21 -070066 struct acpi_table_header *header = &(rsdt->header);
Saket Sinha867bcb62015-08-22 12:20:55 +053067
68 /* Fill out header fields */
Bin Mengdfbb18b2016-05-07 07:46:24 -070069 acpi_fill_header(header, "RSDT");
Saket Sinha867bcb62015-08-22 12:20:55 +053070 header->length = sizeof(struct acpi_rsdt);
Bin Meng7e6343e2016-05-07 07:46:28 -070071 header->revision = 1;
Saket Sinha867bcb62015-08-22 12:20:55 +053072
73 /* Entries are filled in later, we come with an empty set */
74
75 /* Fix checksum */
76 header->checksum = table_compute_checksum((void *)rsdt,
77 sizeof(struct acpi_rsdt));
78}
79
80static void acpi_write_xsdt(struct acpi_xsdt *xsdt)
81{
Bin Meng8a8c0352016-05-07 07:46:21 -070082 struct acpi_table_header *header = &(xsdt->header);
Saket Sinha867bcb62015-08-22 12:20:55 +053083
84 /* Fill out header fields */
Bin Mengdfbb18b2016-05-07 07:46:24 -070085 acpi_fill_header(header, "XSDT");
Saket Sinha867bcb62015-08-22 12:20:55 +053086 header->length = sizeof(struct acpi_xsdt);
Bin Meng7e6343e2016-05-07 07:46:28 -070087 header->revision = 1;
Saket Sinha867bcb62015-08-22 12:20:55 +053088
89 /* Entries are filled in later, we come with an empty set */
90
91 /* Fix checksum */
92 header->checksum = table_compute_checksum((void *)xsdt,
93 sizeof(struct acpi_xsdt));
94}
95
Bin Mengab5efd52016-05-07 07:46:25 -070096/**
97 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
98 * and checksum.
99 */
100static void acpi_add_table(struct acpi_rsdp *rsdp, void *table)
Saket Sinha867bcb62015-08-22 12:20:55 +0530101{
Bin Mengab5efd52016-05-07 07:46:25 -0700102 int i, entries_num;
103 struct acpi_rsdt *rsdt;
104 struct acpi_xsdt *xsdt = NULL;
Saket Sinha867bcb62015-08-22 12:20:55 +0530105
Bin Mengab5efd52016-05-07 07:46:25 -0700106 /* The RSDT is mandatory while the XSDT is not */
107 rsdt = (struct acpi_rsdt *)rsdp->rsdt_address;
Saket Sinha867bcb62015-08-22 12:20:55 +0530108
Bin Mengab5efd52016-05-07 07:46:25 -0700109 if (rsdp->xsdt_address)
110 xsdt = (struct acpi_xsdt *)((u32)rsdp->xsdt_address);
Saket Sinha867bcb62015-08-22 12:20:55 +0530111
Bin Mengab5efd52016-05-07 07:46:25 -0700112 /* This should always be MAX_ACPI_TABLES */
113 entries_num = ARRAY_SIZE(rsdt->entry);
114
115 for (i = 0; i < entries_num; i++) {
116 if (rsdt->entry[i] == 0)
117 break;
Saket Sinha867bcb62015-08-22 12:20:55 +0530118 }
119
Bin Mengab5efd52016-05-07 07:46:25 -0700120 if (i >= entries_num) {
121 debug("ACPI: Error: too many tables\n");
122 return;
123 }
124
125 /* Add table to the RSDT */
126 rsdt->entry[i] = (u32)table;
127
128 /* Fix RSDT length or the kernel will assume invalid entries */
129 rsdt->header.length = sizeof(struct acpi_table_header) +
130 (sizeof(u32) * (i + 1));
131
132 /* Re-calculate checksum */
133 rsdt->header.checksum = 0;
134 rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
135 rsdt->header.length);
136
137 /*
138 * And now the same thing for the XSDT. We use the same index as for
139 * now we want the XSDT and RSDT to always be in sync in U-Boot
140 */
141 if (xsdt) {
142 /* Add table to the XSDT */
143 xsdt->entry[i] = (u64)(u32)table;
144
145 /* Fix XSDT length */
146 xsdt->header.length = sizeof(struct acpi_table_header) +
147 (sizeof(u64) * (i + 1));
148
149 /* Re-calculate checksum */
150 xsdt->header.checksum = 0;
151 xsdt->header.checksum = table_compute_checksum((u8 *)xsdt,
152 xsdt->header.length);
153 }
154}
155
156static void acpi_create_facs(struct acpi_facs *facs)
157{
158 memset((void *)facs, 0, sizeof(struct acpi_facs));
159
160 memcpy(facs->signature, "FACS", 4);
161 facs->length = sizeof(struct acpi_facs);
162 facs->hardware_signature = 0;
163 facs->firmware_waking_vector = 0;
164 facs->global_lock = 0;
165 facs->flags = 0;
166 facs->x_firmware_waking_vector_l = 0;
167 facs->x_firmware_waking_vector_h = 0;
168 facs->version = 1;
169}
170
171static int acpi_create_madt_lapic(struct acpi_madt_lapic *lapic,
172 u8 cpu, u8 apic)
173{
174 lapic->type = ACPI_APIC_LAPIC;
175 lapic->length = sizeof(struct acpi_madt_lapic);
176 lapic->flags = LOCAL_APIC_FLAG_ENABLED;
177 lapic->processor_id = cpu;
178 lapic->apic_id = apic;
179
180 return lapic->length;
181}
182
Bin Mengfc4f5cc2016-05-07 07:46:30 -0700183int acpi_create_madt_lapics(u32 current)
Bin Mengab5efd52016-05-07 07:46:25 -0700184{
185 struct udevice *dev;
Bin Mengfc4f5cc2016-05-07 07:46:30 -0700186 int length = 0;
Bin Mengab5efd52016-05-07 07:46:25 -0700187
188 for (uclass_find_first_device(UCLASS_CPU, &dev);
189 dev;
190 uclass_find_next_device(&dev)) {
191 struct cpu_platdata *plat = dev_get_parent_platdata(dev);
192
Bin Mengfc4f5cc2016-05-07 07:46:30 -0700193 length += acpi_create_madt_lapic(
Bin Mengab5efd52016-05-07 07:46:25 -0700194 (struct acpi_madt_lapic *)current,
195 plat->cpu_id, plat->cpu_id);
Bin Mengfc4f5cc2016-05-07 07:46:30 -0700196 current += length;
Bin Mengab5efd52016-05-07 07:46:25 -0700197 }
198
Bin Mengfc4f5cc2016-05-07 07:46:30 -0700199 return length;
Bin Mengab5efd52016-05-07 07:46:25 -0700200}
201
202int acpi_create_madt_ioapic(struct acpi_madt_ioapic *ioapic, u8 id,
203 u32 addr, u32 gsi_base)
204{
205 ioapic->type = ACPI_APIC_IOAPIC;
206 ioapic->length = sizeof(struct acpi_madt_ioapic);
207 ioapic->reserved = 0x00;
208 ioapic->gsi_base = gsi_base;
209 ioapic->ioapic_id = id;
210 ioapic->ioapic_addr = addr;
211
212 return ioapic->length;
213}
214
215int acpi_create_madt_irqoverride(struct acpi_madt_irqoverride *irqoverride,
216 u8 bus, u8 source, u32 gsirq, u16 flags)
217{
218 irqoverride->type = ACPI_APIC_IRQ_SRC_OVERRIDE;
219 irqoverride->length = sizeof(struct acpi_madt_irqoverride);
220 irqoverride->bus = bus;
221 irqoverride->source = source;
222 irqoverride->gsirq = gsirq;
223 irqoverride->flags = flags;
224
225 return irqoverride->length;
226}
227
228int acpi_create_madt_lapic_nmi(struct acpi_madt_lapic_nmi *lapic_nmi,
229 u8 cpu, u16 flags, u8 lint)
230{
231 lapic_nmi->type = ACPI_APIC_LAPIC_NMI;
232 lapic_nmi->length = sizeof(struct acpi_madt_lapic_nmi);
233 lapic_nmi->flags = flags;
234 lapic_nmi->processor_id = cpu;
235 lapic_nmi->lint = lint;
236
237 return lapic_nmi->length;
238}
239
240static void acpi_create_madt(struct acpi_madt *madt)
241{
242 struct acpi_table_header *header = &(madt->header);
Bin Meng7e79a6b2016-05-07 07:46:26 -0700243 u32 current = (u32)madt + sizeof(struct acpi_madt);
Bin Mengab5efd52016-05-07 07:46:25 -0700244
245 memset((void *)madt, 0, sizeof(struct acpi_madt));
246
247 /* Fill out header fields */
248 acpi_fill_header(header, "APIC");
249 header->length = sizeof(struct acpi_madt);
Bin Meng7e6343e2016-05-07 07:46:28 -0700250 header->revision = 4;
Bin Mengab5efd52016-05-07 07:46:25 -0700251
252 madt->lapic_addr = LAPIC_DEFAULT_BASE;
253 madt->flags = ACPI_MADT_PCAT_COMPAT;
254
255 current = acpi_fill_madt(current);
256
257 /* (Re)calculate length and checksum */
Bin Meng7e79a6b2016-05-07 07:46:26 -0700258 header->length = current - (u32)madt;
Bin Mengab5efd52016-05-07 07:46:25 -0700259
260 header->checksum = table_compute_checksum((void *)madt, header->length);
261}
262
263static int acpi_create_mcfg_mmconfig(struct acpi_mcfg_mmconfig *mmconfig,
264 u32 base, u16 seg_nr, u8 start, u8 end)
265{
266 memset(mmconfig, 0, sizeof(*mmconfig));
267 mmconfig->base_address_l = base;
268 mmconfig->base_address_h = 0;
269 mmconfig->pci_segment_group_number = seg_nr;
270 mmconfig->start_bus_number = start;
271 mmconfig->end_bus_number = end;
272
273 return sizeof(struct acpi_mcfg_mmconfig);
274}
275
Bin Meng7e79a6b2016-05-07 07:46:26 -0700276static u32 acpi_fill_mcfg(u32 current)
Bin Mengab5efd52016-05-07 07:46:25 -0700277{
278 current += acpi_create_mcfg_mmconfig
279 ((struct acpi_mcfg_mmconfig *)current,
280 CONFIG_PCIE_ECAM_BASE, 0x0, 0x0, 255);
281
282 return current;
283}
284
285/* MCFG is defined in the PCI Firmware Specification 3.0 */
286static void acpi_create_mcfg(struct acpi_mcfg *mcfg)
287{
288 struct acpi_table_header *header = &(mcfg->header);
Bin Meng7e79a6b2016-05-07 07:46:26 -0700289 u32 current = (u32)mcfg + sizeof(struct acpi_mcfg);
Bin Mengab5efd52016-05-07 07:46:25 -0700290
291 memset((void *)mcfg, 0, sizeof(struct acpi_mcfg));
292
293 /* Fill out header fields */
294 acpi_fill_header(header, "MCFG");
295 header->length = sizeof(struct acpi_mcfg);
Bin Meng7e6343e2016-05-07 07:46:28 -0700296 header->revision = 1;
Bin Mengab5efd52016-05-07 07:46:25 -0700297
298 current = acpi_fill_mcfg(current);
299
300 /* (Re)calculate length and checksum */
Bin Meng7e79a6b2016-05-07 07:46:26 -0700301 header->length = current - (u32)mcfg;
Bin Mengab5efd52016-05-07 07:46:25 -0700302 header->checksum = table_compute_checksum((void *)mcfg, header->length);
Saket Sinha867bcb62015-08-22 12:20:55 +0530303}
304
Bin Meng6aef68d2016-05-11 07:45:03 -0700305static void enter_acpi_mode(int pm1_cnt)
306{
307 /*
308 * PM1_CNT register bit0 selects the power management event to be
309 * either an SCI or SMI interrupt. When this bit is set, then power
310 * management events will generate an SCI interrupt. When this bit
311 * is reset power management events will generate an SMI interrupt.
312 *
313 * Per ACPI spec, it is the responsibility of the hardware to set
314 * or reset this bit. OSPM always preserves this bit position.
315 *
316 * U-Boot does not support SMI. And we don't have plan to support
317 * anything running in SMM within U-Boot. To create a legacy-free
318 * system, and expose ourselves to OSPM as working under ACPI mode
319 * already, turn this bit on.
320 */
321 outw(PM1_CNT_SCI_EN, pm1_cnt);
322}
323
Miao Yanfa287b12016-01-20 01:57:06 -0800324/*
325 * QEMU's version of write_acpi_tables is defined in
Tom Rinidd6f3ab2016-05-06 10:40:22 -0400326 * arch/x86/cpu/qemu/acpi_table.c
Miao Yanfa287b12016-01-20 01:57:06 -0800327 */
Bin Meng358bb3f2016-02-27 22:58:00 -0800328u32 write_acpi_tables(u32 start)
Saket Sinha867bcb62015-08-22 12:20:55 +0530329{
Bin Meng358bb3f2016-02-27 22:58:00 -0800330 u32 current;
Saket Sinha867bcb62015-08-22 12:20:55 +0530331 struct acpi_rsdp *rsdp;
332 struct acpi_rsdt *rsdt;
333 struct acpi_xsdt *xsdt;
334 struct acpi_facs *facs;
Bin Meng8a8c0352016-05-07 07:46:21 -0700335 struct acpi_table_header *dsdt;
Saket Sinha867bcb62015-08-22 12:20:55 +0530336 struct acpi_fadt *fadt;
337 struct acpi_mcfg *mcfg;
338 struct acpi_madt *madt;
Saket Sinha867bcb62015-08-22 12:20:55 +0530339
340 current = start;
341
Bin Mengab5efd52016-05-07 07:46:25 -0700342 /* Align ACPI tables to 16 byte */
Saket Sinha867bcb62015-08-22 12:20:55 +0530343 current = ALIGN(current, 16);
344
Bin Mengdca4d1a2016-05-07 07:46:12 -0700345 debug("ACPI: Writing ACPI tables at %x\n", start);
Saket Sinha867bcb62015-08-22 12:20:55 +0530346
347 /* We need at least an RSDP and an RSDT Table */
348 rsdp = (struct acpi_rsdp *)current;
349 current += sizeof(struct acpi_rsdp);
350 current = ALIGN(current, 16);
351 rsdt = (struct acpi_rsdt *)current;
352 current += sizeof(struct acpi_rsdt);
353 current = ALIGN(current, 16);
354 xsdt = (struct acpi_xsdt *)current;
355 current += sizeof(struct acpi_xsdt);
Bin Meng25e133e2016-05-07 07:46:27 -0700356 /*
357 * Per ACPI spec, the FACS table address must be aligned to a 64 byte
358 * boundary (Windows checks this, but Linux does not).
359 */
360 current = ALIGN(current, 64);
Saket Sinha867bcb62015-08-22 12:20:55 +0530361
362 /* clear all table memory */
363 memset((void *)start, 0, current - start);
364
365 acpi_write_rsdp(rsdp, rsdt, xsdt);
366 acpi_write_rsdt(rsdt);
367 acpi_write_xsdt(xsdt);
368
369 debug("ACPI: * FACS\n");
370 facs = (struct acpi_facs *)current;
371 current += sizeof(struct acpi_facs);
372 current = ALIGN(current, 16);
373
374 acpi_create_facs(facs);
375
376 debug("ACPI: * DSDT\n");
Bin Meng8a8c0352016-05-07 07:46:21 -0700377 dsdt = (struct acpi_table_header *)current;
378 memcpy(dsdt, &AmlCode, sizeof(struct acpi_table_header));
Bin Meng10fcabe2016-05-11 07:45:05 -0700379 current += sizeof(struct acpi_table_header);
380 memcpy((char *)current,
381 (char *)&AmlCode + sizeof(struct acpi_table_header),
382 dsdt->length - sizeof(struct acpi_table_header));
383 current += dsdt->length - sizeof(struct acpi_table_header);
Saket Sinha867bcb62015-08-22 12:20:55 +0530384 current = ALIGN(current, 16);
385
386 debug("ACPI: * FADT\n");
387 fadt = (struct acpi_fadt *)current;
388 current += sizeof(struct acpi_fadt);
389 current = ALIGN(current, 16);
390 acpi_create_fadt(fadt, facs, dsdt);
391 acpi_add_table(rsdp, fadt);
392
Saket Sinha867bcb62015-08-22 12:20:55 +0530393 debug("ACPI: * MADT\n");
394 madt = (struct acpi_madt *)current;
395 acpi_create_madt(madt);
Bin Meng10fcabe2016-05-11 07:45:05 -0700396 current += madt->header.length;
397 acpi_add_table(rsdp, madt);
Saket Sinha867bcb62015-08-22 12:20:55 +0530398 current = ALIGN(current, 16);
399
Bin Mengab5efd52016-05-07 07:46:25 -0700400 debug("ACPI: * MCFG\n");
401 mcfg = (struct acpi_mcfg *)current;
402 acpi_create_mcfg(mcfg);
Bin Meng10fcabe2016-05-11 07:45:05 -0700403 current += mcfg->header.length;
404 acpi_add_table(rsdp, mcfg);
405 current = ALIGN(current, 16);
Bin Mengab5efd52016-05-07 07:46:25 -0700406
Bin Mengdca4d1a2016-05-07 07:46:12 -0700407 debug("current = %x\n", current);
Saket Sinha867bcb62015-08-22 12:20:55 +0530408
Bin Mengdca4d1a2016-05-07 07:46:12 -0700409 debug("ACPI: done\n");
Saket Sinha867bcb62015-08-22 12:20:55 +0530410
Bin Meng6aef68d2016-05-11 07:45:03 -0700411 /*
412 * Other than waiting for OSPM to request us to switch to ACPI mode,
413 * do it by ourselves, since SMI will not be triggered.
414 */
415 enter_acpi_mode(fadt->pm1a_cnt_blk);
416
Saket Sinha867bcb62015-08-22 12:20:55 +0530417 return current;
418}