blob: 7001e8ba348932541ecd0ca1f647f7dda6f62759 [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>
Bin Mengd9050c62016-06-17 02:13:16 -070014#include <asm/acpi/global_nvs.h>
Saket Sinha331141a2015-08-22 12:20:55 +053015#include <asm/acpi_table.h>
Bin Menge259eb22016-05-11 07:45:03 -070016#include <asm/io.h>
Saket Sinha331141a2015-08-22 12:20:55 +053017#include <asm/lapic.h>
18#include <asm/tables.h>
Bin Mengd9050c62016-06-17 02:13:16 -070019#include <asm/arch/global_nvs.h>
Saket Sinha331141a2015-08-22 12:20:55 +053020
21/*
Bin Mengb063d5f2016-05-07 07:46:24 -070022 * IASL compiles the dsdt entries and writes the hex values
23 * to a C array AmlCode[] (see dsdt.c).
Saket Sinha331141a2015-08-22 12:20:55 +053024 */
25extern const unsigned char AmlCode[];
26
Bin Meng44256b02016-05-07 07:46:25 -070027static void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt,
28 struct acpi_xsdt *xsdt)
29{
30 memset(rsdp, 0, sizeof(struct acpi_rsdp));
31
32 memcpy(rsdp->signature, RSDP_SIG, 8);
33 memcpy(rsdp->oem_id, OEM_ID, 6);
34
35 rsdp->length = sizeof(struct acpi_rsdp);
36 rsdp->rsdt_address = (u32)rsdt;
37
38 /*
39 * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2
40 *
41 * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
42 * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
43 * revision 0)
44 */
45 if (xsdt == NULL) {
46 rsdp->revision = ACPI_RSDP_REV_ACPI_1_0;
47 } else {
48 rsdp->xsdt_address = (u64)(u32)xsdt;
49 rsdp->revision = ACPI_RSDP_REV_ACPI_2_0;
50 }
51
52 /* Calculate checksums */
53 rsdp->checksum = table_compute_checksum((void *)rsdp, 20);
54 rsdp->ext_checksum = table_compute_checksum((void *)rsdp,
55 sizeof(struct acpi_rsdp));
56}
57
58void acpi_fill_header(struct acpi_table_header *header, char *signature)
59{
60 memcpy(header->signature, signature, 4);
61 memcpy(header->oem_id, OEM_ID, 6);
62 memcpy(header->oem_table_id, OEM_TABLE_ID, 8);
63 memcpy(header->aslc_id, ASLC_ID, 4);
64}
65
66static void acpi_write_rsdt(struct acpi_rsdt *rsdt)
67{
68 struct acpi_table_header *header = &(rsdt->header);
69
70 /* Fill out header fields */
71 acpi_fill_header(header, "RSDT");
72 header->length = sizeof(struct acpi_rsdt);
Bin Mengf662fe42016-05-07 07:46:28 -070073 header->revision = 1;
Bin Meng44256b02016-05-07 07:46:25 -070074
75 /* Entries are filled in later, we come with an empty set */
76
77 /* Fix checksum */
78 header->checksum = table_compute_checksum((void *)rsdt,
79 sizeof(struct acpi_rsdt));
80}
81
82static void acpi_write_xsdt(struct acpi_xsdt *xsdt)
83{
84 struct acpi_table_header *header = &(xsdt->header);
85
86 /* Fill out header fields */
87 acpi_fill_header(header, "XSDT");
88 header->length = sizeof(struct acpi_xsdt);
Bin Mengf662fe42016-05-07 07:46:28 -070089 header->revision = 1;
Bin Meng44256b02016-05-07 07:46:25 -070090
91 /* Entries are filled in later, we come with an empty set */
92
93 /* Fix checksum */
94 header->checksum = table_compute_checksum((void *)xsdt,
95 sizeof(struct acpi_xsdt));
96}
97
Saket Sinha331141a2015-08-22 12:20:55 +053098/**
Bin Meng44256b02016-05-07 07:46:25 -070099 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
100 * and checksum.
101 */
Saket Sinha331141a2015-08-22 12:20:55 +0530102static void acpi_add_table(struct acpi_rsdp *rsdp, void *table)
103{
104 int i, entries_num;
105 struct acpi_rsdt *rsdt;
106 struct acpi_xsdt *xsdt = NULL;
107
108 /* The RSDT is mandatory while the XSDT is not */
109 rsdt = (struct acpi_rsdt *)rsdp->rsdt_address;
110
111 if (rsdp->xsdt_address)
112 xsdt = (struct acpi_xsdt *)((u32)rsdp->xsdt_address);
113
114 /* This should always be MAX_ACPI_TABLES */
115 entries_num = ARRAY_SIZE(rsdt->entry);
116
117 for (i = 0; i < entries_num; i++) {
118 if (rsdt->entry[i] == 0)
119 break;
120 }
121
122 if (i >= entries_num) {
Bin Mengd2d22182016-05-07 07:46:12 -0700123 debug("ACPI: Error: too many tables\n");
Saket Sinha331141a2015-08-22 12:20:55 +0530124 return;
125 }
126
127 /* Add table to the RSDT */
128 rsdt->entry[i] = (u32)table;
129
130 /* Fix RSDT length or the kernel will assume invalid entries */
Bin Meng6a421582016-05-07 07:46:21 -0700131 rsdt->header.length = sizeof(struct acpi_table_header) +
Bin Meng44256b02016-05-07 07:46:25 -0700132 (sizeof(u32) * (i + 1));
Saket Sinha331141a2015-08-22 12:20:55 +0530133
134 /* Re-calculate checksum */
135 rsdt->header.checksum = 0;
136 rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
Bin Meng44256b02016-05-07 07:46:25 -0700137 rsdt->header.length);
Saket Sinha331141a2015-08-22 12:20:55 +0530138
139 /*
140 * And now the same thing for the XSDT. We use the same index as for
141 * now we want the XSDT and RSDT to always be in sync in U-Boot
142 */
143 if (xsdt) {
144 /* Add table to the XSDT */
145 xsdt->entry[i] = (u64)(u32)table;
146
147 /* Fix XSDT length */
Bin Meng6a421582016-05-07 07:46:21 -0700148 xsdt->header.length = sizeof(struct acpi_table_header) +
Bin Meng44256b02016-05-07 07:46:25 -0700149 (sizeof(u64) * (i + 1));
Saket Sinha331141a2015-08-22 12:20:55 +0530150
151 /* Re-calculate checksum */
152 xsdt->header.checksum = 0;
153 xsdt->header.checksum = table_compute_checksum((u8 *)xsdt,
154 xsdt->header.length);
155 }
156}
157
Bin Meng44256b02016-05-07 07:46:25 -0700158static void acpi_create_facs(struct acpi_facs *facs)
159{
160 memset((void *)facs, 0, sizeof(struct acpi_facs));
161
162 memcpy(facs->signature, "FACS", 4);
163 facs->length = sizeof(struct acpi_facs);
164 facs->hardware_signature = 0;
165 facs->firmware_waking_vector = 0;
166 facs->global_lock = 0;
167 facs->flags = 0;
168 facs->x_firmware_waking_vector_l = 0;
169 facs->x_firmware_waking_vector_h = 0;
170 facs->version = 1;
171}
172
Saket Sinha331141a2015-08-22 12:20:55 +0530173static int acpi_create_madt_lapic(struct acpi_madt_lapic *lapic,
Bin Meng44256b02016-05-07 07:46:25 -0700174 u8 cpu, u8 apic)
Saket Sinha331141a2015-08-22 12:20:55 +0530175{
Bin Meng44256b02016-05-07 07:46:25 -0700176 lapic->type = ACPI_APIC_LAPIC;
Saket Sinha331141a2015-08-22 12:20:55 +0530177 lapic->length = sizeof(struct acpi_madt_lapic);
Bin Meng44256b02016-05-07 07:46:25 -0700178 lapic->flags = LOCAL_APIC_FLAG_ENABLED;
Saket Sinha331141a2015-08-22 12:20:55 +0530179 lapic->processor_id = cpu;
180 lapic->apic_id = apic;
181
182 return lapic->length;
183}
184
Bin Meng3c5234e2016-05-07 07:46:30 -0700185int acpi_create_madt_lapics(u32 current)
Saket Sinha331141a2015-08-22 12:20:55 +0530186{
187 struct udevice *dev;
George McCollister5a49f872016-06-07 13:40:18 -0500188 int total_length = 0;
Saket Sinha331141a2015-08-22 12:20:55 +0530189
190 for (uclass_find_first_device(UCLASS_CPU, &dev);
191 dev;
192 uclass_find_next_device(&dev)) {
193 struct cpu_platdata *plat = dev_get_parent_platdata(dev);
George McCollister5a49f872016-06-07 13:40:18 -0500194 int length = acpi_create_madt_lapic(
195 (struct acpi_madt_lapic *)current,
196 plat->cpu_id, plat->cpu_id);
Bin Meng3c5234e2016-05-07 07:46:30 -0700197 current += length;
George McCollister5a49f872016-06-07 13:40:18 -0500198 total_length += length;
Bin Meng44256b02016-05-07 07:46:25 -0700199 }
200
George McCollister5a49f872016-06-07 13:40:18 -0500201 return total_length;
Saket Sinha331141a2015-08-22 12:20:55 +0530202}
203
Bin Meng44256b02016-05-07 07:46:25 -0700204int acpi_create_madt_ioapic(struct acpi_madt_ioapic *ioapic, u8 id,
205 u32 addr, u32 gsi_base)
Saket Sinha331141a2015-08-22 12:20:55 +0530206{
Bin Meng6a421582016-05-07 07:46:21 -0700207 ioapic->type = ACPI_APIC_IOAPIC;
Saket Sinha331141a2015-08-22 12:20:55 +0530208 ioapic->length = sizeof(struct acpi_madt_ioapic);
209 ioapic->reserved = 0x00;
210 ioapic->gsi_base = gsi_base;
211 ioapic->ioapic_id = id;
212 ioapic->ioapic_addr = addr;
213
214 return ioapic->length;
215}
216
217int acpi_create_madt_irqoverride(struct acpi_madt_irqoverride *irqoverride,
Bin Meng44256b02016-05-07 07:46:25 -0700218 u8 bus, u8 source, u32 gsirq, u16 flags)
Saket Sinha331141a2015-08-22 12:20:55 +0530219{
Bin Meng6a421582016-05-07 07:46:21 -0700220 irqoverride->type = ACPI_APIC_IRQ_SRC_OVERRIDE;
Saket Sinha331141a2015-08-22 12:20:55 +0530221 irqoverride->length = sizeof(struct acpi_madt_irqoverride);
222 irqoverride->bus = bus;
223 irqoverride->source = source;
224 irqoverride->gsirq = gsirq;
225 irqoverride->flags = flags;
226
227 return irqoverride->length;
228}
229
230int acpi_create_madt_lapic_nmi(struct acpi_madt_lapic_nmi *lapic_nmi,
Bin Meng44256b02016-05-07 07:46:25 -0700231 u8 cpu, u16 flags, u8 lint)
Saket Sinha331141a2015-08-22 12:20:55 +0530232{
Bin Meng6a421582016-05-07 07:46:21 -0700233 lapic_nmi->type = ACPI_APIC_LAPIC_NMI;
Saket Sinha331141a2015-08-22 12:20:55 +0530234 lapic_nmi->length = sizeof(struct acpi_madt_lapic_nmi);
235 lapic_nmi->flags = flags;
236 lapic_nmi->processor_id = cpu;
237 lapic_nmi->lint = lint;
238
239 return lapic_nmi->length;
240}
241
Saket Sinha331141a2015-08-22 12:20:55 +0530242static void acpi_create_madt(struct acpi_madt *madt)
243{
Bin Meng6a421582016-05-07 07:46:21 -0700244 struct acpi_table_header *header = &(madt->header);
Bin Menga1ec7db2016-05-07 07:46:26 -0700245 u32 current = (u32)madt + sizeof(struct acpi_madt);
Saket Sinha331141a2015-08-22 12:20:55 +0530246
247 memset((void *)madt, 0, sizeof(struct acpi_madt));
248
249 /* Fill out header fields */
Bin Mengb063d5f2016-05-07 07:46:24 -0700250 acpi_fill_header(header, "APIC");
Saket Sinha331141a2015-08-22 12:20:55 +0530251 header->length = sizeof(struct acpi_madt);
Bin Mengf662fe42016-05-07 07:46:28 -0700252 header->revision = 4;
Saket Sinha331141a2015-08-22 12:20:55 +0530253
254 madt->lapic_addr = LAPIC_DEFAULT_BASE;
Bin Meng6a421582016-05-07 07:46:21 -0700255 madt->flags = ACPI_MADT_PCAT_COMPAT;
Saket Sinha331141a2015-08-22 12:20:55 +0530256
257 current = acpi_fill_madt(current);
258
259 /* (Re)calculate length and checksum */
Bin Menga1ec7db2016-05-07 07:46:26 -0700260 header->length = current - (u32)madt;
Saket Sinha331141a2015-08-22 12:20:55 +0530261
262 header->checksum = table_compute_checksum((void *)madt, header->length);
263}
264
265static int acpi_create_mcfg_mmconfig(struct acpi_mcfg_mmconfig *mmconfig,
Bin Meng44256b02016-05-07 07:46:25 -0700266 u32 base, u16 seg_nr, u8 start, u8 end)
Saket Sinha331141a2015-08-22 12:20:55 +0530267{
268 memset(mmconfig, 0, sizeof(*mmconfig));
Bin Meng6a421582016-05-07 07:46:21 -0700269 mmconfig->base_address_l = base;
270 mmconfig->base_address_h = 0;
Saket Sinha331141a2015-08-22 12:20:55 +0530271 mmconfig->pci_segment_group_number = seg_nr;
272 mmconfig->start_bus_number = start;
273 mmconfig->end_bus_number = end;
274
275 return sizeof(struct acpi_mcfg_mmconfig);
276}
277
Bin Menga1ec7db2016-05-07 07:46:26 -0700278static u32 acpi_fill_mcfg(u32 current)
Saket Sinha331141a2015-08-22 12:20:55 +0530279{
280 current += acpi_create_mcfg_mmconfig
281 ((struct acpi_mcfg_mmconfig *)current,
Bin Meng44256b02016-05-07 07:46:25 -0700282 CONFIG_PCIE_ECAM_BASE, 0x0, 0x0, 255);
Saket Sinha331141a2015-08-22 12:20:55 +0530283
284 return current;
285}
286
287/* MCFG is defined in the PCI Firmware Specification 3.0 */
288static void acpi_create_mcfg(struct acpi_mcfg *mcfg)
289{
Bin Meng6a421582016-05-07 07:46:21 -0700290 struct acpi_table_header *header = &(mcfg->header);
Bin Menga1ec7db2016-05-07 07:46:26 -0700291 u32 current = (u32)mcfg + sizeof(struct acpi_mcfg);
Saket Sinha331141a2015-08-22 12:20:55 +0530292
293 memset((void *)mcfg, 0, sizeof(struct acpi_mcfg));
294
295 /* Fill out header fields */
Bin Mengb063d5f2016-05-07 07:46:24 -0700296 acpi_fill_header(header, "MCFG");
Saket Sinha331141a2015-08-22 12:20:55 +0530297 header->length = sizeof(struct acpi_mcfg);
Bin Mengf662fe42016-05-07 07:46:28 -0700298 header->revision = 1;
Saket Sinha331141a2015-08-22 12:20:55 +0530299
300 current = acpi_fill_mcfg(current);
301
302 /* (Re)calculate length and checksum */
Bin Menga1ec7db2016-05-07 07:46:26 -0700303 header->length = current - (u32)mcfg;
Saket Sinha331141a2015-08-22 12:20:55 +0530304 header->checksum = table_compute_checksum((void *)mcfg, header->length);
305}
306
Bin Menge259eb22016-05-11 07:45:03 -0700307static void enter_acpi_mode(int pm1_cnt)
308{
309 /*
310 * PM1_CNT register bit0 selects the power management event to be
311 * either an SCI or SMI interrupt. When this bit is set, then power
312 * management events will generate an SCI interrupt. When this bit
313 * is reset power management events will generate an SMI interrupt.
314 *
315 * Per ACPI spec, it is the responsibility of the hardware to set
316 * or reset this bit. OSPM always preserves this bit position.
317 *
318 * U-Boot does not support SMI. And we don't have plan to support
319 * anything running in SMM within U-Boot. To create a legacy-free
320 * system, and expose ourselves to OSPM as working under ACPI mode
321 * already, turn this bit on.
322 */
323 outw(PM1_CNT_SCI_EN, pm1_cnt);
324}
325
Miao Yan3b68c522016-01-20 01:57:06 -0800326/*
327 * QEMU's version of write_acpi_tables is defined in
Tom Rinibcb3c8d2016-05-06 10:40:22 -0400328 * arch/x86/cpu/qemu/acpi_table.c
Miao Yan3b68c522016-01-20 01:57:06 -0800329 */
Bin Meng5c549102016-02-27 22:58:00 -0800330u32 write_acpi_tables(u32 start)
Saket Sinha331141a2015-08-22 12:20:55 +0530331{
Bin Meng5c549102016-02-27 22:58:00 -0800332 u32 current;
Saket Sinha331141a2015-08-22 12:20:55 +0530333 struct acpi_rsdp *rsdp;
334 struct acpi_rsdt *rsdt;
335 struct acpi_xsdt *xsdt;
336 struct acpi_facs *facs;
Bin Meng6a421582016-05-07 07:46:21 -0700337 struct acpi_table_header *dsdt;
Saket Sinha331141a2015-08-22 12:20:55 +0530338 struct acpi_fadt *fadt;
339 struct acpi_mcfg *mcfg;
340 struct acpi_madt *madt;
Bin Mengd9050c62016-06-17 02:13:16 -0700341 int i;
Saket Sinha331141a2015-08-22 12:20:55 +0530342
343 current = start;
344
Bin Meng44256b02016-05-07 07:46:25 -0700345 /* Align ACPI tables to 16 byte */
Saket Sinha331141a2015-08-22 12:20:55 +0530346 current = ALIGN(current, 16);
347
Bin Mengd2d22182016-05-07 07:46:12 -0700348 debug("ACPI: Writing ACPI tables at %x\n", start);
Saket Sinha331141a2015-08-22 12:20:55 +0530349
350 /* We need at least an RSDP and an RSDT Table */
351 rsdp = (struct acpi_rsdp *)current;
352 current += sizeof(struct acpi_rsdp);
353 current = ALIGN(current, 16);
354 rsdt = (struct acpi_rsdt *)current;
355 current += sizeof(struct acpi_rsdt);
356 current = ALIGN(current, 16);
357 xsdt = (struct acpi_xsdt *)current;
358 current += sizeof(struct acpi_xsdt);
Bin Meng3d938d12016-05-07 07:46:27 -0700359 /*
360 * Per ACPI spec, the FACS table address must be aligned to a 64 byte
361 * boundary (Windows checks this, but Linux does not).
362 */
363 current = ALIGN(current, 64);
Saket Sinha331141a2015-08-22 12:20:55 +0530364
365 /* clear all table memory */
366 memset((void *)start, 0, current - start);
367
368 acpi_write_rsdp(rsdp, rsdt, xsdt);
369 acpi_write_rsdt(rsdt);
370 acpi_write_xsdt(xsdt);
371
372 debug("ACPI: * FACS\n");
373 facs = (struct acpi_facs *)current;
374 current += sizeof(struct acpi_facs);
375 current = ALIGN(current, 16);
376
377 acpi_create_facs(facs);
378
379 debug("ACPI: * DSDT\n");
Bin Meng6a421582016-05-07 07:46:21 -0700380 dsdt = (struct acpi_table_header *)current;
381 memcpy(dsdt, &AmlCode, sizeof(struct acpi_table_header));
Bin Mengd90434b2016-05-11 07:45:05 -0700382 current += sizeof(struct acpi_table_header);
383 memcpy((char *)current,
384 (char *)&AmlCode + sizeof(struct acpi_table_header),
385 dsdt->length - sizeof(struct acpi_table_header));
386 current += dsdt->length - sizeof(struct acpi_table_header);
Saket Sinha331141a2015-08-22 12:20:55 +0530387 current = ALIGN(current, 16);
388
Bin Mengd9050c62016-06-17 02:13:16 -0700389 /* Pack GNVS into the ACPI table area */
390 for (i = 0; i < dsdt->length; i++) {
391 u32 *gnvs = (u32 *)((u32)dsdt + i);
392 if (*gnvs == ACPI_GNVS_ADDR) {
393 debug("Fix up global NVS in DSDT to 0x%08x\n", current);
394 *gnvs = current;
395 break;
396 }
397 }
398
399 /* Update DSDT checksum since we patched the GNVS address */
400 dsdt->checksum = 0;
401 dsdt->checksum = table_compute_checksum((void *)dsdt, dsdt->length);
402
403 /* Fill in platform-specific global NVS variables */
404 acpi_create_gnvs((struct acpi_global_nvs *)current);
405 current += sizeof(struct acpi_global_nvs);
406 current = ALIGN(current, 16);
407
Saket Sinha331141a2015-08-22 12:20:55 +0530408 debug("ACPI: * FADT\n");
409 fadt = (struct acpi_fadt *)current;
410 current += sizeof(struct acpi_fadt);
411 current = ALIGN(current, 16);
412 acpi_create_fadt(fadt, facs, dsdt);
413 acpi_add_table(rsdp, fadt);
414
Saket Sinha331141a2015-08-22 12:20:55 +0530415 debug("ACPI: * MADT\n");
416 madt = (struct acpi_madt *)current;
417 acpi_create_madt(madt);
Bin Mengd90434b2016-05-11 07:45:05 -0700418 current += madt->header.length;
419 acpi_add_table(rsdp, madt);
Saket Sinha331141a2015-08-22 12:20:55 +0530420 current = ALIGN(current, 16);
421
Bin Meng44256b02016-05-07 07:46:25 -0700422 debug("ACPI: * MCFG\n");
423 mcfg = (struct acpi_mcfg *)current;
424 acpi_create_mcfg(mcfg);
Bin Mengd90434b2016-05-11 07:45:05 -0700425 current += mcfg->header.length;
426 acpi_add_table(rsdp, mcfg);
427 current = ALIGN(current, 16);
Bin Meng44256b02016-05-07 07:46:25 -0700428
Bin Mengd2d22182016-05-07 07:46:12 -0700429 debug("current = %x\n", current);
Saket Sinha331141a2015-08-22 12:20:55 +0530430
Bin Mengd2d22182016-05-07 07:46:12 -0700431 debug("ACPI: done\n");
Saket Sinha331141a2015-08-22 12:20:55 +0530432
Bin Menge259eb22016-05-11 07:45:03 -0700433 /*
434 * Other than waiting for OSPM to request us to switch to ACPI mode,
435 * do it by ourselves, since SMI will not be triggered.
436 */
437 enter_acpi_mode(fadt->pm1a_cnt_blk);
438
Saket Sinha331141a2015-08-22 12:20:55 +0530439 return current;
440}