blob: 7c4321b5ae3acde54709d42b20d916c8c8ac395a [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Saket Sinha331141a2015-08-22 12:20:55 +05302/*
3 * Based on acpi.c from coreboot
4 *
5 * Copyright (C) 2015, Saket Sinha <saket.sinha89@gmail.com>
Bin Meng44256b02016-05-07 07:46:25 -07006 * Copyright (C) 2016, Bin Meng <bmeng.cn@gmail.com>
Saket Sinha331141a2015-08-22 12:20:55 +05307 */
8
9#include <common.h>
10#include <cpu.h>
11#include <dm.h>
12#include <dm/uclass-internal.h>
Andy Shevchenko87e95372017-07-21 22:32:02 +030013#include <version.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>
Andy Shevchenko13a5d872017-07-21 22:32:04 +030017#include <asm/ioapic.h>
Saket Sinha331141a2015-08-22 12:20:55 +053018#include <asm/lapic.h>
Andy Shevchenko13a5d872017-07-21 22:32:04 +030019#include <asm/mpspec.h>
Saket Sinha331141a2015-08-22 12:20:55 +053020#include <asm/tables.h>
Bin Mengd9050c62016-06-17 02:13:16 -070021#include <asm/arch/global_nvs.h>
Saket Sinha331141a2015-08-22 12:20:55 +053022
23/*
Bin Mengb063d5f2016-05-07 07:46:24 -070024 * IASL compiles the dsdt entries and writes the hex values
25 * to a C array AmlCode[] (see dsdt.c).
Saket Sinha331141a2015-08-22 12:20:55 +053026 */
27extern const unsigned char AmlCode[];
28
Andy Shevchenko66d3e632018-01-10 19:40:15 +020029/* ACPI RSDP address to be used in boot parameters */
Bin Menge1029252018-01-30 05:01:16 -080030static ulong acpi_rsdp_addr;
Andy Shevchenko66d3e632018-01-10 19:40:15 +020031
Bin Meng44256b02016-05-07 07:46:25 -070032static void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt,
33 struct acpi_xsdt *xsdt)
34{
35 memset(rsdp, 0, sizeof(struct acpi_rsdp));
36
37 memcpy(rsdp->signature, RSDP_SIG, 8);
38 memcpy(rsdp->oem_id, OEM_ID, 6);
39
40 rsdp->length = sizeof(struct acpi_rsdp);
41 rsdp->rsdt_address = (u32)rsdt;
42
43 /*
44 * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2
45 *
46 * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
47 * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
48 * revision 0)
49 */
50 if (xsdt == NULL) {
51 rsdp->revision = ACPI_RSDP_REV_ACPI_1_0;
52 } else {
53 rsdp->xsdt_address = (u64)(u32)xsdt;
54 rsdp->revision = ACPI_RSDP_REV_ACPI_2_0;
55 }
56
57 /* Calculate checksums */
58 rsdp->checksum = table_compute_checksum((void *)rsdp, 20);
59 rsdp->ext_checksum = table_compute_checksum((void *)rsdp,
60 sizeof(struct acpi_rsdp));
61}
62
63void acpi_fill_header(struct acpi_table_header *header, char *signature)
64{
65 memcpy(header->signature, signature, 4);
66 memcpy(header->oem_id, OEM_ID, 6);
67 memcpy(header->oem_table_id, OEM_TABLE_ID, 8);
Andy Shevchenko87e95372017-07-21 22:32:02 +030068 header->oem_revision = U_BOOT_BUILD_DATE;
Bin Meng44256b02016-05-07 07:46:25 -070069 memcpy(header->aslc_id, ASLC_ID, 4);
70}
71
72static void acpi_write_rsdt(struct acpi_rsdt *rsdt)
73{
74 struct acpi_table_header *header = &(rsdt->header);
75
76 /* Fill out header fields */
77 acpi_fill_header(header, "RSDT");
78 header->length = sizeof(struct acpi_rsdt);
Bin Mengf662fe42016-05-07 07:46:28 -070079 header->revision = 1;
Bin Meng44256b02016-05-07 07:46:25 -070080
81 /* Entries are filled in later, we come with an empty set */
82
83 /* Fix checksum */
84 header->checksum = table_compute_checksum((void *)rsdt,
85 sizeof(struct acpi_rsdt));
86}
87
88static void acpi_write_xsdt(struct acpi_xsdt *xsdt)
89{
90 struct acpi_table_header *header = &(xsdt->header);
91
92 /* Fill out header fields */
93 acpi_fill_header(header, "XSDT");
94 header->length = sizeof(struct acpi_xsdt);
Bin Mengf662fe42016-05-07 07:46:28 -070095 header->revision = 1;
Bin Meng44256b02016-05-07 07:46:25 -070096
97 /* Entries are filled in later, we come with an empty set */
98
99 /* Fix checksum */
100 header->checksum = table_compute_checksum((void *)xsdt,
101 sizeof(struct acpi_xsdt));
102}
103
Saket Sinha331141a2015-08-22 12:20:55 +0530104/**
Bin Meng44256b02016-05-07 07:46:25 -0700105 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
106 * and checksum.
107 */
Saket Sinha331141a2015-08-22 12:20:55 +0530108static void acpi_add_table(struct acpi_rsdp *rsdp, void *table)
109{
110 int i, entries_num;
111 struct acpi_rsdt *rsdt;
112 struct acpi_xsdt *xsdt = NULL;
113
114 /* The RSDT is mandatory while the XSDT is not */
115 rsdt = (struct acpi_rsdt *)rsdp->rsdt_address;
116
117 if (rsdp->xsdt_address)
118 xsdt = (struct acpi_xsdt *)((u32)rsdp->xsdt_address);
119
120 /* This should always be MAX_ACPI_TABLES */
121 entries_num = ARRAY_SIZE(rsdt->entry);
122
123 for (i = 0; i < entries_num; i++) {
124 if (rsdt->entry[i] == 0)
125 break;
126 }
127
128 if (i >= entries_num) {
Bin Mengd2d22182016-05-07 07:46:12 -0700129 debug("ACPI: Error: too many tables\n");
Saket Sinha331141a2015-08-22 12:20:55 +0530130 return;
131 }
132
133 /* Add table to the RSDT */
134 rsdt->entry[i] = (u32)table;
135
136 /* Fix RSDT length or the kernel will assume invalid entries */
Bin Meng6a421582016-05-07 07:46:21 -0700137 rsdt->header.length = sizeof(struct acpi_table_header) +
Bin Meng44256b02016-05-07 07:46:25 -0700138 (sizeof(u32) * (i + 1));
Saket Sinha331141a2015-08-22 12:20:55 +0530139
140 /* Re-calculate checksum */
141 rsdt->header.checksum = 0;
142 rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
Bin Meng44256b02016-05-07 07:46:25 -0700143 rsdt->header.length);
Saket Sinha331141a2015-08-22 12:20:55 +0530144
145 /*
146 * And now the same thing for the XSDT. We use the same index as for
147 * now we want the XSDT and RSDT to always be in sync in U-Boot
148 */
149 if (xsdt) {
150 /* Add table to the XSDT */
151 xsdt->entry[i] = (u64)(u32)table;
152
153 /* Fix XSDT length */
Bin Meng6a421582016-05-07 07:46:21 -0700154 xsdt->header.length = sizeof(struct acpi_table_header) +
Bin Meng44256b02016-05-07 07:46:25 -0700155 (sizeof(u64) * (i + 1));
Saket Sinha331141a2015-08-22 12:20:55 +0530156
157 /* Re-calculate checksum */
158 xsdt->header.checksum = 0;
159 xsdt->header.checksum = table_compute_checksum((u8 *)xsdt,
160 xsdt->header.length);
161 }
162}
163
Bin Meng44256b02016-05-07 07:46:25 -0700164static void acpi_create_facs(struct acpi_facs *facs)
165{
166 memset((void *)facs, 0, sizeof(struct acpi_facs));
167
168 memcpy(facs->signature, "FACS", 4);
169 facs->length = sizeof(struct acpi_facs);
170 facs->hardware_signature = 0;
171 facs->firmware_waking_vector = 0;
172 facs->global_lock = 0;
173 facs->flags = 0;
174 facs->x_firmware_waking_vector_l = 0;
175 facs->x_firmware_waking_vector_h = 0;
176 facs->version = 1;
177}
178
Saket Sinha331141a2015-08-22 12:20:55 +0530179static int acpi_create_madt_lapic(struct acpi_madt_lapic *lapic,
Bin Meng44256b02016-05-07 07:46:25 -0700180 u8 cpu, u8 apic)
Saket Sinha331141a2015-08-22 12:20:55 +0530181{
Bin Meng44256b02016-05-07 07:46:25 -0700182 lapic->type = ACPI_APIC_LAPIC;
Saket Sinha331141a2015-08-22 12:20:55 +0530183 lapic->length = sizeof(struct acpi_madt_lapic);
Bin Meng44256b02016-05-07 07:46:25 -0700184 lapic->flags = LOCAL_APIC_FLAG_ENABLED;
Saket Sinha331141a2015-08-22 12:20:55 +0530185 lapic->processor_id = cpu;
186 lapic->apic_id = apic;
187
188 return lapic->length;
189}
190
Bin Meng3c5234e2016-05-07 07:46:30 -0700191int acpi_create_madt_lapics(u32 current)
Saket Sinha331141a2015-08-22 12:20:55 +0530192{
193 struct udevice *dev;
George McCollister5a49f872016-06-07 13:40:18 -0500194 int total_length = 0;
Saket Sinha331141a2015-08-22 12:20:55 +0530195
196 for (uclass_find_first_device(UCLASS_CPU, &dev);
197 dev;
198 uclass_find_next_device(&dev)) {
199 struct cpu_platdata *plat = dev_get_parent_platdata(dev);
George McCollister5a49f872016-06-07 13:40:18 -0500200 int length = acpi_create_madt_lapic(
201 (struct acpi_madt_lapic *)current,
202 plat->cpu_id, plat->cpu_id);
Bin Meng3c5234e2016-05-07 07:46:30 -0700203 current += length;
George McCollister5a49f872016-06-07 13:40:18 -0500204 total_length += length;
Bin Meng44256b02016-05-07 07:46:25 -0700205 }
206
George McCollister5a49f872016-06-07 13:40:18 -0500207 return total_length;
Saket Sinha331141a2015-08-22 12:20:55 +0530208}
209
Bin Meng44256b02016-05-07 07:46:25 -0700210int acpi_create_madt_ioapic(struct acpi_madt_ioapic *ioapic, u8 id,
211 u32 addr, u32 gsi_base)
Saket Sinha331141a2015-08-22 12:20:55 +0530212{
Bin Meng6a421582016-05-07 07:46:21 -0700213 ioapic->type = ACPI_APIC_IOAPIC;
Saket Sinha331141a2015-08-22 12:20:55 +0530214 ioapic->length = sizeof(struct acpi_madt_ioapic);
215 ioapic->reserved = 0x00;
216 ioapic->gsi_base = gsi_base;
217 ioapic->ioapic_id = id;
218 ioapic->ioapic_addr = addr;
219
220 return ioapic->length;
221}
222
223int acpi_create_madt_irqoverride(struct acpi_madt_irqoverride *irqoverride,
Bin Meng44256b02016-05-07 07:46:25 -0700224 u8 bus, u8 source, u32 gsirq, u16 flags)
Saket Sinha331141a2015-08-22 12:20:55 +0530225{
Bin Meng6a421582016-05-07 07:46:21 -0700226 irqoverride->type = ACPI_APIC_IRQ_SRC_OVERRIDE;
Saket Sinha331141a2015-08-22 12:20:55 +0530227 irqoverride->length = sizeof(struct acpi_madt_irqoverride);
228 irqoverride->bus = bus;
229 irqoverride->source = source;
230 irqoverride->gsirq = gsirq;
231 irqoverride->flags = flags;
232
233 return irqoverride->length;
234}
235
236int acpi_create_madt_lapic_nmi(struct acpi_madt_lapic_nmi *lapic_nmi,
Bin Meng44256b02016-05-07 07:46:25 -0700237 u8 cpu, u16 flags, u8 lint)
Saket Sinha331141a2015-08-22 12:20:55 +0530238{
Bin Meng6a421582016-05-07 07:46:21 -0700239 lapic_nmi->type = ACPI_APIC_LAPIC_NMI;
Saket Sinha331141a2015-08-22 12:20:55 +0530240 lapic_nmi->length = sizeof(struct acpi_madt_lapic_nmi);
241 lapic_nmi->flags = flags;
242 lapic_nmi->processor_id = cpu;
243 lapic_nmi->lint = lint;
244
245 return lapic_nmi->length;
246}
247
Andy Shevchenko13a5d872017-07-21 22:32:04 +0300248static int acpi_create_madt_irq_overrides(u32 current)
249{
250 struct acpi_madt_irqoverride *irqovr;
251 u16 sci_flags = MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_HIGH;
252 int length = 0;
253
254 irqovr = (void *)current;
255 length += acpi_create_madt_irqoverride(irqovr, 0, 0, 2, 0);
256
257 irqovr = (void *)(current + length);
258 length += acpi_create_madt_irqoverride(irqovr, 0, 9, 9, sci_flags);
259
260 return length;
261}
262
263__weak u32 acpi_fill_madt(u32 current)
264{
265 current += acpi_create_madt_lapics(current);
266
267 current += acpi_create_madt_ioapic((struct acpi_madt_ioapic *)current,
268 io_apic_read(IO_APIC_ID) >> 24, IO_APIC_ADDR, 0);
269
270 current += acpi_create_madt_irq_overrides(current);
271
272 return current;
273}
274
Saket Sinha331141a2015-08-22 12:20:55 +0530275static void acpi_create_madt(struct acpi_madt *madt)
276{
Bin Meng6a421582016-05-07 07:46:21 -0700277 struct acpi_table_header *header = &(madt->header);
Bin Menga1ec7db2016-05-07 07:46:26 -0700278 u32 current = (u32)madt + sizeof(struct acpi_madt);
Saket Sinha331141a2015-08-22 12:20:55 +0530279
280 memset((void *)madt, 0, sizeof(struct acpi_madt));
281
282 /* Fill out header fields */
Bin Mengb063d5f2016-05-07 07:46:24 -0700283 acpi_fill_header(header, "APIC");
Saket Sinha331141a2015-08-22 12:20:55 +0530284 header->length = sizeof(struct acpi_madt);
Bin Mengf662fe42016-05-07 07:46:28 -0700285 header->revision = 4;
Saket Sinha331141a2015-08-22 12:20:55 +0530286
287 madt->lapic_addr = LAPIC_DEFAULT_BASE;
Bin Meng6a421582016-05-07 07:46:21 -0700288 madt->flags = ACPI_MADT_PCAT_COMPAT;
Saket Sinha331141a2015-08-22 12:20:55 +0530289
290 current = acpi_fill_madt(current);
291
292 /* (Re)calculate length and checksum */
Bin Menga1ec7db2016-05-07 07:46:26 -0700293 header->length = current - (u32)madt;
Saket Sinha331141a2015-08-22 12:20:55 +0530294
295 header->checksum = table_compute_checksum((void *)madt, header->length);
296}
297
Andy Shevchenkoc1ae9802017-07-21 22:32:05 +0300298int acpi_create_mcfg_mmconfig(struct acpi_mcfg_mmconfig *mmconfig, u32 base,
299 u16 seg_nr, u8 start, u8 end)
Saket Sinha331141a2015-08-22 12:20:55 +0530300{
301 memset(mmconfig, 0, sizeof(*mmconfig));
Bin Meng6a421582016-05-07 07:46:21 -0700302 mmconfig->base_address_l = base;
303 mmconfig->base_address_h = 0;
Saket Sinha331141a2015-08-22 12:20:55 +0530304 mmconfig->pci_segment_group_number = seg_nr;
305 mmconfig->start_bus_number = start;
306 mmconfig->end_bus_number = end;
307
308 return sizeof(struct acpi_mcfg_mmconfig);
309}
310
Andy Shevchenkoc1ae9802017-07-21 22:32:05 +0300311__weak u32 acpi_fill_mcfg(u32 current)
Saket Sinha331141a2015-08-22 12:20:55 +0530312{
313 current += acpi_create_mcfg_mmconfig
314 ((struct acpi_mcfg_mmconfig *)current,
Bin Meng44256b02016-05-07 07:46:25 -0700315 CONFIG_PCIE_ECAM_BASE, 0x0, 0x0, 255);
Saket Sinha331141a2015-08-22 12:20:55 +0530316
317 return current;
318}
319
320/* MCFG is defined in the PCI Firmware Specification 3.0 */
321static void acpi_create_mcfg(struct acpi_mcfg *mcfg)
322{
Bin Meng6a421582016-05-07 07:46:21 -0700323 struct acpi_table_header *header = &(mcfg->header);
Bin Menga1ec7db2016-05-07 07:46:26 -0700324 u32 current = (u32)mcfg + sizeof(struct acpi_mcfg);
Saket Sinha331141a2015-08-22 12:20:55 +0530325
326 memset((void *)mcfg, 0, sizeof(struct acpi_mcfg));
327
328 /* Fill out header fields */
Bin Mengb063d5f2016-05-07 07:46:24 -0700329 acpi_fill_header(header, "MCFG");
Saket Sinha331141a2015-08-22 12:20:55 +0530330 header->length = sizeof(struct acpi_mcfg);
Bin Mengf662fe42016-05-07 07:46:28 -0700331 header->revision = 1;
Saket Sinha331141a2015-08-22 12:20:55 +0530332
333 current = acpi_fill_mcfg(current);
334
335 /* (Re)calculate length and checksum */
Bin Menga1ec7db2016-05-07 07:46:26 -0700336 header->length = current - (u32)mcfg;
Saket Sinha331141a2015-08-22 12:20:55 +0530337 header->checksum = table_compute_checksum((void *)mcfg, header->length);
338}
339
Bin Mengdaa5b832017-04-21 07:24:43 -0700340void enter_acpi_mode(int pm1_cnt)
Bin Menge259eb22016-05-11 07:45:03 -0700341{
Bin Meng313ddd62017-04-21 07:24:42 -0700342 u16 val = inw(pm1_cnt);
343
Bin Menge259eb22016-05-11 07:45:03 -0700344 /*
345 * PM1_CNT register bit0 selects the power management event to be
346 * either an SCI or SMI interrupt. When this bit is set, then power
347 * management events will generate an SCI interrupt. When this bit
348 * is reset power management events will generate an SMI interrupt.
349 *
350 * Per ACPI spec, it is the responsibility of the hardware to set
351 * or reset this bit. OSPM always preserves this bit position.
352 *
353 * U-Boot does not support SMI. And we don't have plan to support
354 * anything running in SMM within U-Boot. To create a legacy-free
355 * system, and expose ourselves to OSPM as working under ACPI mode
356 * already, turn this bit on.
357 */
Bin Meng313ddd62017-04-21 07:24:42 -0700358 outw(val | PM1_CNT_SCI_EN, pm1_cnt);
Bin Menge259eb22016-05-11 07:45:03 -0700359}
360
Miao Yan3b68c522016-01-20 01:57:06 -0800361/*
Andy Shevchenko4b05bac2018-01-10 19:33:10 +0200362 * QEMU's version of write_acpi_tables is defined in drivers/misc/qfw.c
Miao Yan3b68c522016-01-20 01:57:06 -0800363 */
Simon Glassca37a392017-01-16 07:03:35 -0700364ulong write_acpi_tables(ulong start)
Saket Sinha331141a2015-08-22 12:20:55 +0530365{
Bin Meng5c549102016-02-27 22:58:00 -0800366 u32 current;
Saket Sinha331141a2015-08-22 12:20:55 +0530367 struct acpi_rsdp *rsdp;
368 struct acpi_rsdt *rsdt;
369 struct acpi_xsdt *xsdt;
370 struct acpi_facs *facs;
Bin Meng6a421582016-05-07 07:46:21 -0700371 struct acpi_table_header *dsdt;
Saket Sinha331141a2015-08-22 12:20:55 +0530372 struct acpi_fadt *fadt;
373 struct acpi_mcfg *mcfg;
374 struct acpi_madt *madt;
Bin Mengd9050c62016-06-17 02:13:16 -0700375 int i;
Saket Sinha331141a2015-08-22 12:20:55 +0530376
377 current = start;
378
Bin Meng44256b02016-05-07 07:46:25 -0700379 /* Align ACPI tables to 16 byte */
Saket Sinha331141a2015-08-22 12:20:55 +0530380 current = ALIGN(current, 16);
381
Simon Glassca37a392017-01-16 07:03:35 -0700382 debug("ACPI: Writing ACPI tables at %lx\n", start);
Saket Sinha331141a2015-08-22 12:20:55 +0530383
384 /* We need at least an RSDP and an RSDT Table */
385 rsdp = (struct acpi_rsdp *)current;
386 current += sizeof(struct acpi_rsdp);
387 current = ALIGN(current, 16);
388 rsdt = (struct acpi_rsdt *)current;
389 current += sizeof(struct acpi_rsdt);
390 current = ALIGN(current, 16);
391 xsdt = (struct acpi_xsdt *)current;
392 current += sizeof(struct acpi_xsdt);
Bin Meng3d938d12016-05-07 07:46:27 -0700393 /*
394 * Per ACPI spec, the FACS table address must be aligned to a 64 byte
395 * boundary (Windows checks this, but Linux does not).
396 */
397 current = ALIGN(current, 64);
Saket Sinha331141a2015-08-22 12:20:55 +0530398
399 /* clear all table memory */
400 memset((void *)start, 0, current - start);
401
402 acpi_write_rsdp(rsdp, rsdt, xsdt);
403 acpi_write_rsdt(rsdt);
404 acpi_write_xsdt(xsdt);
405
406 debug("ACPI: * FACS\n");
407 facs = (struct acpi_facs *)current;
408 current += sizeof(struct acpi_facs);
409 current = ALIGN(current, 16);
410
411 acpi_create_facs(facs);
412
413 debug("ACPI: * DSDT\n");
Bin Meng6a421582016-05-07 07:46:21 -0700414 dsdt = (struct acpi_table_header *)current;
415 memcpy(dsdt, &AmlCode, sizeof(struct acpi_table_header));
Bin Mengd90434b2016-05-11 07:45:05 -0700416 current += sizeof(struct acpi_table_header);
417 memcpy((char *)current,
418 (char *)&AmlCode + sizeof(struct acpi_table_header),
419 dsdt->length - sizeof(struct acpi_table_header));
420 current += dsdt->length - sizeof(struct acpi_table_header);
Saket Sinha331141a2015-08-22 12:20:55 +0530421 current = ALIGN(current, 16);
422
Bin Mengd9050c62016-06-17 02:13:16 -0700423 /* Pack GNVS into the ACPI table area */
424 for (i = 0; i < dsdt->length; i++) {
425 u32 *gnvs = (u32 *)((u32)dsdt + i);
426 if (*gnvs == ACPI_GNVS_ADDR) {
427 debug("Fix up global NVS in DSDT to 0x%08x\n", current);
428 *gnvs = current;
429 break;
430 }
431 }
432
433 /* Update DSDT checksum since we patched the GNVS address */
434 dsdt->checksum = 0;
435 dsdt->checksum = table_compute_checksum((void *)dsdt, dsdt->length);
436
437 /* Fill in platform-specific global NVS variables */
438 acpi_create_gnvs((struct acpi_global_nvs *)current);
439 current += sizeof(struct acpi_global_nvs);
440 current = ALIGN(current, 16);
441
Saket Sinha331141a2015-08-22 12:20:55 +0530442 debug("ACPI: * FADT\n");
443 fadt = (struct acpi_fadt *)current;
444 current += sizeof(struct acpi_fadt);
445 current = ALIGN(current, 16);
446 acpi_create_fadt(fadt, facs, dsdt);
447 acpi_add_table(rsdp, fadt);
448
Saket Sinha331141a2015-08-22 12:20:55 +0530449 debug("ACPI: * MADT\n");
450 madt = (struct acpi_madt *)current;
451 acpi_create_madt(madt);
Bin Mengd90434b2016-05-11 07:45:05 -0700452 current += madt->header.length;
453 acpi_add_table(rsdp, madt);
Saket Sinha331141a2015-08-22 12:20:55 +0530454 current = ALIGN(current, 16);
455
Bin Meng44256b02016-05-07 07:46:25 -0700456 debug("ACPI: * MCFG\n");
457 mcfg = (struct acpi_mcfg *)current;
458 acpi_create_mcfg(mcfg);
Bin Mengd90434b2016-05-11 07:45:05 -0700459 current += mcfg->header.length;
460 acpi_add_table(rsdp, mcfg);
461 current = ALIGN(current, 16);
Bin Meng44256b02016-05-07 07:46:25 -0700462
Bin Mengd2d22182016-05-07 07:46:12 -0700463 debug("current = %x\n", current);
Saket Sinha331141a2015-08-22 12:20:55 +0530464
Andy Shevchenko66d3e632018-01-10 19:40:15 +0200465 acpi_rsdp_addr = (unsigned long)rsdp;
Bin Mengd2d22182016-05-07 07:46:12 -0700466 debug("ACPI: done\n");
Saket Sinha331141a2015-08-22 12:20:55 +0530467
Andy Shevchenko683b6962017-07-21 22:32:06 +0300468 /* Don't touch ACPI hardware on HW reduced platforms */
469 if (fadt->flags & ACPI_FADT_HW_REDUCED_ACPI)
470 return current;
471
Bin Menge259eb22016-05-11 07:45:03 -0700472 /*
473 * Other than waiting for OSPM to request us to switch to ACPI mode,
474 * do it by ourselves, since SMI will not be triggered.
475 */
476 enter_acpi_mode(fadt->pm1a_cnt_blk);
477
Saket Sinha331141a2015-08-22 12:20:55 +0530478 return current;
479}
Bin Meng34bc74a2017-04-21 07:24:36 -0700480
Bin Menge1029252018-01-30 05:01:16 -0800481ulong acpi_get_rsdp_addr(void)
482{
483 return acpi_rsdp_addr;
484}
485
Bin Meng34bc74a2017-04-21 07:24:36 -0700486static struct acpi_rsdp *acpi_valid_rsdp(struct acpi_rsdp *rsdp)
487{
488 if (strncmp((char *)rsdp, RSDP_SIG, sizeof(RSDP_SIG) - 1) != 0)
489 return NULL;
490
491 debug("Looking on %p for valid checksum\n", rsdp);
492
493 if (table_compute_checksum((void *)rsdp, 20) != 0)
494 return NULL;
495 debug("acpi rsdp checksum 1 passed\n");
496
497 if ((rsdp->revision > 1) &&
498 (table_compute_checksum((void *)rsdp, rsdp->length) != 0))
499 return NULL;
500 debug("acpi rsdp checksum 2 passed\n");
501
502 return rsdp;
503}
504
Bin Meng280aebe2017-04-21 07:24:44 -0700505struct acpi_fadt *acpi_find_fadt(void)
Bin Meng34bc74a2017-04-21 07:24:36 -0700506{
507 char *p, *end;
508 struct acpi_rsdp *rsdp = NULL;
509 struct acpi_rsdt *rsdt;
510 struct acpi_fadt *fadt = NULL;
Bin Meng34bc74a2017-04-21 07:24:36 -0700511 int i;
512
Bin Meng34bc74a2017-04-21 07:24:36 -0700513 /* Find RSDP */
514 for (p = (char *)ROM_TABLE_ADDR; p < (char *)ROM_TABLE_END; p += 16) {
515 rsdp = acpi_valid_rsdp((struct acpi_rsdp *)p);
516 if (rsdp)
517 break;
518 }
519
520 if (rsdp == NULL)
521 return NULL;
522
523 debug("RSDP found at %p\n", rsdp);
524 rsdt = (struct acpi_rsdt *)rsdp->rsdt_address;
525
526 end = (char *)rsdt + rsdt->header.length;
527 debug("RSDT found at %p ends at %p\n", rsdt, end);
528
529 for (i = 0; ((char *)&rsdt->entry[i]) < end; i++) {
530 fadt = (struct acpi_fadt *)rsdt->entry[i];
531 if (strncmp((char *)fadt, "FACP", 4) == 0)
532 break;
533 fadt = NULL;
534 }
535
536 if (fadt == NULL)
537 return NULL;
538
539 debug("FADT found at %p\n", fadt);
Bin Meng280aebe2017-04-21 07:24:44 -0700540 return fadt;
541}
542
543void *acpi_find_wakeup_vector(struct acpi_fadt *fadt)
544{
545 struct acpi_facs *facs;
546 void *wake_vec;
547
548 debug("Trying to find the wakeup vector...\n");
549
Bin Meng34bc74a2017-04-21 07:24:36 -0700550 facs = (struct acpi_facs *)fadt->firmware_ctrl;
551
552 if (facs == NULL) {
553 debug("No FACS found, wake up from S3 not possible.\n");
554 return NULL;
555 }
556
557 debug("FACS found at %p\n", facs);
558 wake_vec = (void *)facs->firmware_waking_vector;
559 debug("OS waking vector is %p\n", wake_vec);
560
561 return wake_vec;
562}