blob: ffb4678e510b38e9249db3209bf0dd8666155613 [file] [log] [blame]
Saket Sinha331141a2015-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 Meng44256b02016-05-07 07:46:25 -07005 * Copyright (C) 2016, Bin Meng <bmeng.cn@gmail.com>
Saket Sinha331141a2015-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 Sinha331141a2015-08-22 12:20:55 +053014#include <asm/acpi_table.h>
Bin Menge259eb22016-05-11 07:45:03 -070015#include <asm/io.h>
Saket Sinha331141a2015-08-22 12:20:55 +053016#include <asm/lapic.h>
17#include <asm/tables.h>
Saket Sinha331141a2015-08-22 12:20:55 +053018
19/*
Bin Mengb063d5f2016-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 Sinha331141a2015-08-22 12:20:55 +053022 */
23extern const unsigned char AmlCode[];
24
Bin Meng44256b02016-05-07 07:46:25 -070025static void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt,
26 struct acpi_xsdt *xsdt)
27{
28 memset(rsdp, 0, sizeof(struct acpi_rsdp));
29
30 memcpy(rsdp->signature, RSDP_SIG, 8);
31 memcpy(rsdp->oem_id, OEM_ID, 6);
32
33 rsdp->length = sizeof(struct acpi_rsdp);
34 rsdp->rsdt_address = (u32)rsdt;
35
36 /*
37 * 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)
42 */
43 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;
48 }
49
50 /* 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));
54}
55
56void acpi_fill_header(struct acpi_table_header *header, char *signature)
57{
58 memcpy(header->signature, signature, 4);
59 memcpy(header->oem_id, OEM_ID, 6);
60 memcpy(header->oem_table_id, OEM_TABLE_ID, 8);
61 memcpy(header->aslc_id, ASLC_ID, 4);
62}
63
64static void acpi_write_rsdt(struct acpi_rsdt *rsdt)
65{
66 struct acpi_table_header *header = &(rsdt->header);
67
68 /* Fill out header fields */
69 acpi_fill_header(header, "RSDT");
70 header->length = sizeof(struct acpi_rsdt);
Bin Mengf662fe42016-05-07 07:46:28 -070071 header->revision = 1;
Bin Meng44256b02016-05-07 07:46:25 -070072
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{
82 struct acpi_table_header *header = &(xsdt->header);
83
84 /* Fill out header fields */
85 acpi_fill_header(header, "XSDT");
86 header->length = sizeof(struct acpi_xsdt);
Bin Mengf662fe42016-05-07 07:46:28 -070087 header->revision = 1;
Bin Meng44256b02016-05-07 07:46:25 -070088
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
Saket Sinha331141a2015-08-22 12:20:55 +053096/**
Bin Meng44256b02016-05-07 07:46:25 -070097 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
98 * and checksum.
99 */
Saket Sinha331141a2015-08-22 12:20:55 +0530100static void acpi_add_table(struct acpi_rsdp *rsdp, void *table)
101{
102 int i, entries_num;
103 struct acpi_rsdt *rsdt;
104 struct acpi_xsdt *xsdt = NULL;
105
106 /* The RSDT is mandatory while the XSDT is not */
107 rsdt = (struct acpi_rsdt *)rsdp->rsdt_address;
108
109 if (rsdp->xsdt_address)
110 xsdt = (struct acpi_xsdt *)((u32)rsdp->xsdt_address);
111
112 /* 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;
118 }
119
120 if (i >= entries_num) {
Bin Mengd2d22182016-05-07 07:46:12 -0700121 debug("ACPI: Error: too many tables\n");
Saket Sinha331141a2015-08-22 12:20:55 +0530122 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 */
Bin Meng6a421582016-05-07 07:46:21 -0700129 rsdt->header.length = sizeof(struct acpi_table_header) +
Bin Meng44256b02016-05-07 07:46:25 -0700130 (sizeof(u32) * (i + 1));
Saket Sinha331141a2015-08-22 12:20:55 +0530131
132 /* Re-calculate checksum */
133 rsdt->header.checksum = 0;
134 rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
Bin Meng44256b02016-05-07 07:46:25 -0700135 rsdt->header.length);
Saket Sinha331141a2015-08-22 12:20:55 +0530136
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 */
Bin Meng6a421582016-05-07 07:46:21 -0700146 xsdt->header.length = sizeof(struct acpi_table_header) +
Bin Meng44256b02016-05-07 07:46:25 -0700147 (sizeof(u64) * (i + 1));
Saket Sinha331141a2015-08-22 12:20:55 +0530148
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
Bin Meng44256b02016-05-07 07:46:25 -0700156static 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
Saket Sinha331141a2015-08-22 12:20:55 +0530171static int acpi_create_madt_lapic(struct acpi_madt_lapic *lapic,
Bin Meng44256b02016-05-07 07:46:25 -0700172 u8 cpu, u8 apic)
Saket Sinha331141a2015-08-22 12:20:55 +0530173{
Bin Meng44256b02016-05-07 07:46:25 -0700174 lapic->type = ACPI_APIC_LAPIC;
Saket Sinha331141a2015-08-22 12:20:55 +0530175 lapic->length = sizeof(struct acpi_madt_lapic);
Bin Meng44256b02016-05-07 07:46:25 -0700176 lapic->flags = LOCAL_APIC_FLAG_ENABLED;
Saket Sinha331141a2015-08-22 12:20:55 +0530177 lapic->processor_id = cpu;
178 lapic->apic_id = apic;
179
180 return lapic->length;
181}
182
Bin Meng3c5234e2016-05-07 07:46:30 -0700183int acpi_create_madt_lapics(u32 current)
Saket Sinha331141a2015-08-22 12:20:55 +0530184{
185 struct udevice *dev;
Bin Meng3c5234e2016-05-07 07:46:30 -0700186 int length = 0;
Saket Sinha331141a2015-08-22 12:20:55 +0530187
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 Meng3c5234e2016-05-07 07:46:30 -0700193 length += acpi_create_madt_lapic(
Saket Sinha331141a2015-08-22 12:20:55 +0530194 (struct acpi_madt_lapic *)current,
195 plat->cpu_id, plat->cpu_id);
Bin Meng3c5234e2016-05-07 07:46:30 -0700196 current += length;
Bin Meng44256b02016-05-07 07:46:25 -0700197 }
198
Bin Meng3c5234e2016-05-07 07:46:30 -0700199 return length;
Saket Sinha331141a2015-08-22 12:20:55 +0530200}
201
Bin Meng44256b02016-05-07 07:46:25 -0700202int acpi_create_madt_ioapic(struct acpi_madt_ioapic *ioapic, u8 id,
203 u32 addr, u32 gsi_base)
Saket Sinha331141a2015-08-22 12:20:55 +0530204{
Bin Meng6a421582016-05-07 07:46:21 -0700205 ioapic->type = ACPI_APIC_IOAPIC;
Saket Sinha331141a2015-08-22 12:20:55 +0530206 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,
Bin Meng44256b02016-05-07 07:46:25 -0700216 u8 bus, u8 source, u32 gsirq, u16 flags)
Saket Sinha331141a2015-08-22 12:20:55 +0530217{
Bin Meng6a421582016-05-07 07:46:21 -0700218 irqoverride->type = ACPI_APIC_IRQ_SRC_OVERRIDE;
Saket Sinha331141a2015-08-22 12:20:55 +0530219 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,
Bin Meng44256b02016-05-07 07:46:25 -0700229 u8 cpu, u16 flags, u8 lint)
Saket Sinha331141a2015-08-22 12:20:55 +0530230{
Bin Meng6a421582016-05-07 07:46:21 -0700231 lapic_nmi->type = ACPI_APIC_LAPIC_NMI;
Saket Sinha331141a2015-08-22 12:20:55 +0530232 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
Saket Sinha331141a2015-08-22 12:20:55 +0530240static void acpi_create_madt(struct acpi_madt *madt)
241{
Bin Meng6a421582016-05-07 07:46:21 -0700242 struct acpi_table_header *header = &(madt->header);
Bin Menga1ec7db2016-05-07 07:46:26 -0700243 u32 current = (u32)madt + sizeof(struct acpi_madt);
Saket Sinha331141a2015-08-22 12:20:55 +0530244
245 memset((void *)madt, 0, sizeof(struct acpi_madt));
246
247 /* Fill out header fields */
Bin Mengb063d5f2016-05-07 07:46:24 -0700248 acpi_fill_header(header, "APIC");
Saket Sinha331141a2015-08-22 12:20:55 +0530249 header->length = sizeof(struct acpi_madt);
Bin Mengf662fe42016-05-07 07:46:28 -0700250 header->revision = 4;
Saket Sinha331141a2015-08-22 12:20:55 +0530251
252 madt->lapic_addr = LAPIC_DEFAULT_BASE;
Bin Meng6a421582016-05-07 07:46:21 -0700253 madt->flags = ACPI_MADT_PCAT_COMPAT;
Saket Sinha331141a2015-08-22 12:20:55 +0530254
255 current = acpi_fill_madt(current);
256
257 /* (Re)calculate length and checksum */
Bin Menga1ec7db2016-05-07 07:46:26 -0700258 header->length = current - (u32)madt;
Saket Sinha331141a2015-08-22 12:20:55 +0530259
260 header->checksum = table_compute_checksum((void *)madt, header->length);
261}
262
263static int acpi_create_mcfg_mmconfig(struct acpi_mcfg_mmconfig *mmconfig,
Bin Meng44256b02016-05-07 07:46:25 -0700264 u32 base, u16 seg_nr, u8 start, u8 end)
Saket Sinha331141a2015-08-22 12:20:55 +0530265{
266 memset(mmconfig, 0, sizeof(*mmconfig));
Bin Meng6a421582016-05-07 07:46:21 -0700267 mmconfig->base_address_l = base;
268 mmconfig->base_address_h = 0;
Saket Sinha331141a2015-08-22 12:20:55 +0530269 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 Menga1ec7db2016-05-07 07:46:26 -0700276static u32 acpi_fill_mcfg(u32 current)
Saket Sinha331141a2015-08-22 12:20:55 +0530277{
278 current += acpi_create_mcfg_mmconfig
279 ((struct acpi_mcfg_mmconfig *)current,
Bin Meng44256b02016-05-07 07:46:25 -0700280 CONFIG_PCIE_ECAM_BASE, 0x0, 0x0, 255);
Saket Sinha331141a2015-08-22 12:20:55 +0530281
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{
Bin Meng6a421582016-05-07 07:46:21 -0700288 struct acpi_table_header *header = &(mcfg->header);
Bin Menga1ec7db2016-05-07 07:46:26 -0700289 u32 current = (u32)mcfg + sizeof(struct acpi_mcfg);
Saket Sinha331141a2015-08-22 12:20:55 +0530290
291 memset((void *)mcfg, 0, sizeof(struct acpi_mcfg));
292
293 /* Fill out header fields */
Bin Mengb063d5f2016-05-07 07:46:24 -0700294 acpi_fill_header(header, "MCFG");
Saket Sinha331141a2015-08-22 12:20:55 +0530295 header->length = sizeof(struct acpi_mcfg);
Bin Mengf662fe42016-05-07 07:46:28 -0700296 header->revision = 1;
Saket Sinha331141a2015-08-22 12:20:55 +0530297
298 current = acpi_fill_mcfg(current);
299
300 /* (Re)calculate length and checksum */
Bin Menga1ec7db2016-05-07 07:46:26 -0700301 header->length = current - (u32)mcfg;
Saket Sinha331141a2015-08-22 12:20:55 +0530302 header->checksum = table_compute_checksum((void *)mcfg, header->length);
303}
304
Bin Menge259eb22016-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 Yan3b68c522016-01-20 01:57:06 -0800324/*
325 * QEMU's version of write_acpi_tables is defined in
Tom Rinibcb3c8d2016-05-06 10:40:22 -0400326 * arch/x86/cpu/qemu/acpi_table.c
Miao Yan3b68c522016-01-20 01:57:06 -0800327 */
Bin Meng5c549102016-02-27 22:58:00 -0800328u32 write_acpi_tables(u32 start)
Saket Sinha331141a2015-08-22 12:20:55 +0530329{
Bin Meng5c549102016-02-27 22:58:00 -0800330 u32 current;
Saket Sinha331141a2015-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 Meng6a421582016-05-07 07:46:21 -0700335 struct acpi_table_header *dsdt;
Saket Sinha331141a2015-08-22 12:20:55 +0530336 struct acpi_fadt *fadt;
337 struct acpi_mcfg *mcfg;
338 struct acpi_madt *madt;
Saket Sinha331141a2015-08-22 12:20:55 +0530339
340 current = start;
341
Bin Meng44256b02016-05-07 07:46:25 -0700342 /* Align ACPI tables to 16 byte */
Saket Sinha331141a2015-08-22 12:20:55 +0530343 current = ALIGN(current, 16);
344
Bin Mengd2d22182016-05-07 07:46:12 -0700345 debug("ACPI: Writing ACPI tables at %x\n", start);
Saket Sinha331141a2015-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 Meng3d938d12016-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 Sinha331141a2015-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 Meng6a421582016-05-07 07:46:21 -0700377 dsdt = (struct acpi_table_header *)current;
378 memcpy(dsdt, &AmlCode, sizeof(struct acpi_table_header));
Bin Mengd90434b2016-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 Sinha331141a2015-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 Sinha331141a2015-08-22 12:20:55 +0530393 debug("ACPI: * MADT\n");
394 madt = (struct acpi_madt *)current;
395 acpi_create_madt(madt);
Bin Mengd90434b2016-05-11 07:45:05 -0700396 current += madt->header.length;
397 acpi_add_table(rsdp, madt);
Saket Sinha331141a2015-08-22 12:20:55 +0530398 current = ALIGN(current, 16);
399
Bin Meng44256b02016-05-07 07:46:25 -0700400 debug("ACPI: * MCFG\n");
401 mcfg = (struct acpi_mcfg *)current;
402 acpi_create_mcfg(mcfg);
Bin Mengd90434b2016-05-11 07:45:05 -0700403 current += mcfg->header.length;
404 acpi_add_table(rsdp, mcfg);
405 current = ALIGN(current, 16);
Bin Meng44256b02016-05-07 07:46:25 -0700406
Bin Mengd2d22182016-05-07 07:46:12 -0700407 debug("current = %x\n", current);
Saket Sinha331141a2015-08-22 12:20:55 +0530408
Bin Mengd2d22182016-05-07 07:46:12 -0700409 debug("ACPI: done\n");
Saket Sinha331141a2015-08-22 12:20:55 +0530410
Bin Menge259eb22016-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 Sinha331141a2015-08-22 12:20:55 +0530417 return current;
418}