blob: c6f4d8941c911c1b61811891e4e3dad96839a9c1 [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>
Andy Shevchenko87e95372017-07-21 22:32:02 +030014#include <version.h>
Bin Mengd9050c62016-06-17 02:13:16 -070015#include <asm/acpi/global_nvs.h>
Saket Sinha331141a2015-08-22 12:20:55 +053016#include <asm/acpi_table.h>
Bin Menge259eb22016-05-11 07:45:03 -070017#include <asm/io.h>
Saket Sinha331141a2015-08-22 12:20:55 +053018#include <asm/lapic.h>
19#include <asm/tables.h>
Bin Mengd9050c62016-06-17 02:13:16 -070020#include <asm/arch/global_nvs.h>
Saket Sinha331141a2015-08-22 12:20:55 +053021
22/*
Bin Mengb063d5f2016-05-07 07:46:24 -070023 * IASL compiles the dsdt entries and writes the hex values
24 * to a C array AmlCode[] (see dsdt.c).
Saket Sinha331141a2015-08-22 12:20:55 +053025 */
26extern const unsigned char AmlCode[];
27
Bin Meng44256b02016-05-07 07:46:25 -070028static void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt,
29 struct acpi_xsdt *xsdt)
30{
31 memset(rsdp, 0, sizeof(struct acpi_rsdp));
32
33 memcpy(rsdp->signature, RSDP_SIG, 8);
34 memcpy(rsdp->oem_id, OEM_ID, 6);
35
36 rsdp->length = sizeof(struct acpi_rsdp);
37 rsdp->rsdt_address = (u32)rsdt;
38
39 /*
40 * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2
41 *
42 * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
43 * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
44 * revision 0)
45 */
46 if (xsdt == NULL) {
47 rsdp->revision = ACPI_RSDP_REV_ACPI_1_0;
48 } else {
49 rsdp->xsdt_address = (u64)(u32)xsdt;
50 rsdp->revision = ACPI_RSDP_REV_ACPI_2_0;
51 }
52
53 /* Calculate checksums */
54 rsdp->checksum = table_compute_checksum((void *)rsdp, 20);
55 rsdp->ext_checksum = table_compute_checksum((void *)rsdp,
56 sizeof(struct acpi_rsdp));
57}
58
59void acpi_fill_header(struct acpi_table_header *header, char *signature)
60{
61 memcpy(header->signature, signature, 4);
62 memcpy(header->oem_id, OEM_ID, 6);
63 memcpy(header->oem_table_id, OEM_TABLE_ID, 8);
Andy Shevchenko87e95372017-07-21 22:32:02 +030064 header->oem_revision = U_BOOT_BUILD_DATE;
Bin Meng44256b02016-05-07 07:46:25 -070065 memcpy(header->aslc_id, ASLC_ID, 4);
66}
67
68static void acpi_write_rsdt(struct acpi_rsdt *rsdt)
69{
70 struct acpi_table_header *header = &(rsdt->header);
71
72 /* Fill out header fields */
73 acpi_fill_header(header, "RSDT");
74 header->length = sizeof(struct acpi_rsdt);
Bin Mengf662fe42016-05-07 07:46:28 -070075 header->revision = 1;
Bin Meng44256b02016-05-07 07:46:25 -070076
77 /* Entries are filled in later, we come with an empty set */
78
79 /* Fix checksum */
80 header->checksum = table_compute_checksum((void *)rsdt,
81 sizeof(struct acpi_rsdt));
82}
83
84static void acpi_write_xsdt(struct acpi_xsdt *xsdt)
85{
86 struct acpi_table_header *header = &(xsdt->header);
87
88 /* Fill out header fields */
89 acpi_fill_header(header, "XSDT");
90 header->length = sizeof(struct acpi_xsdt);
Bin Mengf662fe42016-05-07 07:46:28 -070091 header->revision = 1;
Bin Meng44256b02016-05-07 07:46:25 -070092
93 /* Entries are filled in later, we come with an empty set */
94
95 /* Fix checksum */
96 header->checksum = table_compute_checksum((void *)xsdt,
97 sizeof(struct acpi_xsdt));
98}
99
Saket Sinha331141a2015-08-22 12:20:55 +0530100/**
Bin Meng44256b02016-05-07 07:46:25 -0700101 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
102 * and checksum.
103 */
Saket Sinha331141a2015-08-22 12:20:55 +0530104static void acpi_add_table(struct acpi_rsdp *rsdp, void *table)
105{
106 int i, entries_num;
107 struct acpi_rsdt *rsdt;
108 struct acpi_xsdt *xsdt = NULL;
109
110 /* The RSDT is mandatory while the XSDT is not */
111 rsdt = (struct acpi_rsdt *)rsdp->rsdt_address;
112
113 if (rsdp->xsdt_address)
114 xsdt = (struct acpi_xsdt *)((u32)rsdp->xsdt_address);
115
116 /* This should always be MAX_ACPI_TABLES */
117 entries_num = ARRAY_SIZE(rsdt->entry);
118
119 for (i = 0; i < entries_num; i++) {
120 if (rsdt->entry[i] == 0)
121 break;
122 }
123
124 if (i >= entries_num) {
Bin Mengd2d22182016-05-07 07:46:12 -0700125 debug("ACPI: Error: too many tables\n");
Saket Sinha331141a2015-08-22 12:20:55 +0530126 return;
127 }
128
129 /* Add table to the RSDT */
130 rsdt->entry[i] = (u32)table;
131
132 /* Fix RSDT length or the kernel will assume invalid entries */
Bin Meng6a421582016-05-07 07:46:21 -0700133 rsdt->header.length = sizeof(struct acpi_table_header) +
Bin Meng44256b02016-05-07 07:46:25 -0700134 (sizeof(u32) * (i + 1));
Saket Sinha331141a2015-08-22 12:20:55 +0530135
136 /* Re-calculate checksum */
137 rsdt->header.checksum = 0;
138 rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
Bin Meng44256b02016-05-07 07:46:25 -0700139 rsdt->header.length);
Saket Sinha331141a2015-08-22 12:20:55 +0530140
141 /*
142 * And now the same thing for the XSDT. We use the same index as for
143 * now we want the XSDT and RSDT to always be in sync in U-Boot
144 */
145 if (xsdt) {
146 /* Add table to the XSDT */
147 xsdt->entry[i] = (u64)(u32)table;
148
149 /* Fix XSDT length */
Bin Meng6a421582016-05-07 07:46:21 -0700150 xsdt->header.length = sizeof(struct acpi_table_header) +
Bin Meng44256b02016-05-07 07:46:25 -0700151 (sizeof(u64) * (i + 1));
Saket Sinha331141a2015-08-22 12:20:55 +0530152
153 /* Re-calculate checksum */
154 xsdt->header.checksum = 0;
155 xsdt->header.checksum = table_compute_checksum((u8 *)xsdt,
156 xsdt->header.length);
157 }
158}
159
Bin Meng44256b02016-05-07 07:46:25 -0700160static void acpi_create_facs(struct acpi_facs *facs)
161{
162 memset((void *)facs, 0, sizeof(struct acpi_facs));
163
164 memcpy(facs->signature, "FACS", 4);
165 facs->length = sizeof(struct acpi_facs);
166 facs->hardware_signature = 0;
167 facs->firmware_waking_vector = 0;
168 facs->global_lock = 0;
169 facs->flags = 0;
170 facs->x_firmware_waking_vector_l = 0;
171 facs->x_firmware_waking_vector_h = 0;
172 facs->version = 1;
173}
174
Saket Sinha331141a2015-08-22 12:20:55 +0530175static int acpi_create_madt_lapic(struct acpi_madt_lapic *lapic,
Bin Meng44256b02016-05-07 07:46:25 -0700176 u8 cpu, u8 apic)
Saket Sinha331141a2015-08-22 12:20:55 +0530177{
Bin Meng44256b02016-05-07 07:46:25 -0700178 lapic->type = ACPI_APIC_LAPIC;
Saket Sinha331141a2015-08-22 12:20:55 +0530179 lapic->length = sizeof(struct acpi_madt_lapic);
Bin Meng44256b02016-05-07 07:46:25 -0700180 lapic->flags = LOCAL_APIC_FLAG_ENABLED;
Saket Sinha331141a2015-08-22 12:20:55 +0530181 lapic->processor_id = cpu;
182 lapic->apic_id = apic;
183
184 return lapic->length;
185}
186
Bin Meng3c5234e2016-05-07 07:46:30 -0700187int acpi_create_madt_lapics(u32 current)
Saket Sinha331141a2015-08-22 12:20:55 +0530188{
189 struct udevice *dev;
George McCollister5a49f872016-06-07 13:40:18 -0500190 int total_length = 0;
Saket Sinha331141a2015-08-22 12:20:55 +0530191
192 for (uclass_find_first_device(UCLASS_CPU, &dev);
193 dev;
194 uclass_find_next_device(&dev)) {
195 struct cpu_platdata *plat = dev_get_parent_platdata(dev);
George McCollister5a49f872016-06-07 13:40:18 -0500196 int length = acpi_create_madt_lapic(
197 (struct acpi_madt_lapic *)current,
198 plat->cpu_id, plat->cpu_id);
Bin Meng3c5234e2016-05-07 07:46:30 -0700199 current += length;
George McCollister5a49f872016-06-07 13:40:18 -0500200 total_length += length;
Bin Meng44256b02016-05-07 07:46:25 -0700201 }
202
George McCollister5a49f872016-06-07 13:40:18 -0500203 return total_length;
Saket Sinha331141a2015-08-22 12:20:55 +0530204}
205
Bin Meng44256b02016-05-07 07:46:25 -0700206int acpi_create_madt_ioapic(struct acpi_madt_ioapic *ioapic, u8 id,
207 u32 addr, u32 gsi_base)
Saket Sinha331141a2015-08-22 12:20:55 +0530208{
Bin Meng6a421582016-05-07 07:46:21 -0700209 ioapic->type = ACPI_APIC_IOAPIC;
Saket Sinha331141a2015-08-22 12:20:55 +0530210 ioapic->length = sizeof(struct acpi_madt_ioapic);
211 ioapic->reserved = 0x00;
212 ioapic->gsi_base = gsi_base;
213 ioapic->ioapic_id = id;
214 ioapic->ioapic_addr = addr;
215
216 return ioapic->length;
217}
218
219int acpi_create_madt_irqoverride(struct acpi_madt_irqoverride *irqoverride,
Bin Meng44256b02016-05-07 07:46:25 -0700220 u8 bus, u8 source, u32 gsirq, u16 flags)
Saket Sinha331141a2015-08-22 12:20:55 +0530221{
Bin Meng6a421582016-05-07 07:46:21 -0700222 irqoverride->type = ACPI_APIC_IRQ_SRC_OVERRIDE;
Saket Sinha331141a2015-08-22 12:20:55 +0530223 irqoverride->length = sizeof(struct acpi_madt_irqoverride);
224 irqoverride->bus = bus;
225 irqoverride->source = source;
226 irqoverride->gsirq = gsirq;
227 irqoverride->flags = flags;
228
229 return irqoverride->length;
230}
231
232int acpi_create_madt_lapic_nmi(struct acpi_madt_lapic_nmi *lapic_nmi,
Bin Meng44256b02016-05-07 07:46:25 -0700233 u8 cpu, u16 flags, u8 lint)
Saket Sinha331141a2015-08-22 12:20:55 +0530234{
Bin Meng6a421582016-05-07 07:46:21 -0700235 lapic_nmi->type = ACPI_APIC_LAPIC_NMI;
Saket Sinha331141a2015-08-22 12:20:55 +0530236 lapic_nmi->length = sizeof(struct acpi_madt_lapic_nmi);
237 lapic_nmi->flags = flags;
238 lapic_nmi->processor_id = cpu;
239 lapic_nmi->lint = lint;
240
241 return lapic_nmi->length;
242}
243
Saket Sinha331141a2015-08-22 12:20:55 +0530244static void acpi_create_madt(struct acpi_madt *madt)
245{
Bin Meng6a421582016-05-07 07:46:21 -0700246 struct acpi_table_header *header = &(madt->header);
Bin Menga1ec7db2016-05-07 07:46:26 -0700247 u32 current = (u32)madt + sizeof(struct acpi_madt);
Saket Sinha331141a2015-08-22 12:20:55 +0530248
249 memset((void *)madt, 0, sizeof(struct acpi_madt));
250
251 /* Fill out header fields */
Bin Mengb063d5f2016-05-07 07:46:24 -0700252 acpi_fill_header(header, "APIC");
Saket Sinha331141a2015-08-22 12:20:55 +0530253 header->length = sizeof(struct acpi_madt);
Bin Mengf662fe42016-05-07 07:46:28 -0700254 header->revision = 4;
Saket Sinha331141a2015-08-22 12:20:55 +0530255
256 madt->lapic_addr = LAPIC_DEFAULT_BASE;
Bin Meng6a421582016-05-07 07:46:21 -0700257 madt->flags = ACPI_MADT_PCAT_COMPAT;
Saket Sinha331141a2015-08-22 12:20:55 +0530258
259 current = acpi_fill_madt(current);
260
261 /* (Re)calculate length and checksum */
Bin Menga1ec7db2016-05-07 07:46:26 -0700262 header->length = current - (u32)madt;
Saket Sinha331141a2015-08-22 12:20:55 +0530263
264 header->checksum = table_compute_checksum((void *)madt, header->length);
265}
266
267static int acpi_create_mcfg_mmconfig(struct acpi_mcfg_mmconfig *mmconfig,
Bin Meng44256b02016-05-07 07:46:25 -0700268 u32 base, u16 seg_nr, u8 start, u8 end)
Saket Sinha331141a2015-08-22 12:20:55 +0530269{
270 memset(mmconfig, 0, sizeof(*mmconfig));
Bin Meng6a421582016-05-07 07:46:21 -0700271 mmconfig->base_address_l = base;
272 mmconfig->base_address_h = 0;
Saket Sinha331141a2015-08-22 12:20:55 +0530273 mmconfig->pci_segment_group_number = seg_nr;
274 mmconfig->start_bus_number = start;
275 mmconfig->end_bus_number = end;
276
277 return sizeof(struct acpi_mcfg_mmconfig);
278}
279
Bin Menga1ec7db2016-05-07 07:46:26 -0700280static u32 acpi_fill_mcfg(u32 current)
Saket Sinha331141a2015-08-22 12:20:55 +0530281{
282 current += acpi_create_mcfg_mmconfig
283 ((struct acpi_mcfg_mmconfig *)current,
Bin Meng44256b02016-05-07 07:46:25 -0700284 CONFIG_PCIE_ECAM_BASE, 0x0, 0x0, 255);
Saket Sinha331141a2015-08-22 12:20:55 +0530285
286 return current;
287}
288
289/* MCFG is defined in the PCI Firmware Specification 3.0 */
290static void acpi_create_mcfg(struct acpi_mcfg *mcfg)
291{
Bin Meng6a421582016-05-07 07:46:21 -0700292 struct acpi_table_header *header = &(mcfg->header);
Bin Menga1ec7db2016-05-07 07:46:26 -0700293 u32 current = (u32)mcfg + sizeof(struct acpi_mcfg);
Saket Sinha331141a2015-08-22 12:20:55 +0530294
295 memset((void *)mcfg, 0, sizeof(struct acpi_mcfg));
296
297 /* Fill out header fields */
Bin Mengb063d5f2016-05-07 07:46:24 -0700298 acpi_fill_header(header, "MCFG");
Saket Sinha331141a2015-08-22 12:20:55 +0530299 header->length = sizeof(struct acpi_mcfg);
Bin Mengf662fe42016-05-07 07:46:28 -0700300 header->revision = 1;
Saket Sinha331141a2015-08-22 12:20:55 +0530301
302 current = acpi_fill_mcfg(current);
303
304 /* (Re)calculate length and checksum */
Bin Menga1ec7db2016-05-07 07:46:26 -0700305 header->length = current - (u32)mcfg;
Saket Sinha331141a2015-08-22 12:20:55 +0530306 header->checksum = table_compute_checksum((void *)mcfg, header->length);
307}
308
Bin Mengdaa5b832017-04-21 07:24:43 -0700309void enter_acpi_mode(int pm1_cnt)
Bin Menge259eb22016-05-11 07:45:03 -0700310{
Bin Meng313ddd62017-04-21 07:24:42 -0700311 u16 val = inw(pm1_cnt);
312
Bin Menge259eb22016-05-11 07:45:03 -0700313 /*
314 * PM1_CNT register bit0 selects the power management event to be
315 * either an SCI or SMI interrupt. When this bit is set, then power
316 * management events will generate an SCI interrupt. When this bit
317 * is reset power management events will generate an SMI interrupt.
318 *
319 * Per ACPI spec, it is the responsibility of the hardware to set
320 * or reset this bit. OSPM always preserves this bit position.
321 *
322 * U-Boot does not support SMI. And we don't have plan to support
323 * anything running in SMM within U-Boot. To create a legacy-free
324 * system, and expose ourselves to OSPM as working under ACPI mode
325 * already, turn this bit on.
326 */
Bin Meng313ddd62017-04-21 07:24:42 -0700327 outw(val | PM1_CNT_SCI_EN, pm1_cnt);
Bin Menge259eb22016-05-11 07:45:03 -0700328}
329
Miao Yan3b68c522016-01-20 01:57:06 -0800330/*
331 * QEMU's version of write_acpi_tables is defined in
Tom Rinibcb3c8d2016-05-06 10:40:22 -0400332 * arch/x86/cpu/qemu/acpi_table.c
Miao Yan3b68c522016-01-20 01:57:06 -0800333 */
Simon Glassca37a392017-01-16 07:03:35 -0700334ulong write_acpi_tables(ulong start)
Saket Sinha331141a2015-08-22 12:20:55 +0530335{
Bin Meng5c549102016-02-27 22:58:00 -0800336 u32 current;
Saket Sinha331141a2015-08-22 12:20:55 +0530337 struct acpi_rsdp *rsdp;
338 struct acpi_rsdt *rsdt;
339 struct acpi_xsdt *xsdt;
340 struct acpi_facs *facs;
Bin Meng6a421582016-05-07 07:46:21 -0700341 struct acpi_table_header *dsdt;
Saket Sinha331141a2015-08-22 12:20:55 +0530342 struct acpi_fadt *fadt;
343 struct acpi_mcfg *mcfg;
344 struct acpi_madt *madt;
Bin Mengd9050c62016-06-17 02:13:16 -0700345 int i;
Saket Sinha331141a2015-08-22 12:20:55 +0530346
347 current = start;
348
Bin Meng44256b02016-05-07 07:46:25 -0700349 /* Align ACPI tables to 16 byte */
Saket Sinha331141a2015-08-22 12:20:55 +0530350 current = ALIGN(current, 16);
351
Simon Glassca37a392017-01-16 07:03:35 -0700352 debug("ACPI: Writing ACPI tables at %lx\n", start);
Saket Sinha331141a2015-08-22 12:20:55 +0530353
354 /* We need at least an RSDP and an RSDT Table */
355 rsdp = (struct acpi_rsdp *)current;
356 current += sizeof(struct acpi_rsdp);
357 current = ALIGN(current, 16);
358 rsdt = (struct acpi_rsdt *)current;
359 current += sizeof(struct acpi_rsdt);
360 current = ALIGN(current, 16);
361 xsdt = (struct acpi_xsdt *)current;
362 current += sizeof(struct acpi_xsdt);
Bin Meng3d938d12016-05-07 07:46:27 -0700363 /*
364 * Per ACPI spec, the FACS table address must be aligned to a 64 byte
365 * boundary (Windows checks this, but Linux does not).
366 */
367 current = ALIGN(current, 64);
Saket Sinha331141a2015-08-22 12:20:55 +0530368
369 /* clear all table memory */
370 memset((void *)start, 0, current - start);
371
372 acpi_write_rsdp(rsdp, rsdt, xsdt);
373 acpi_write_rsdt(rsdt);
374 acpi_write_xsdt(xsdt);
375
376 debug("ACPI: * FACS\n");
377 facs = (struct acpi_facs *)current;
378 current += sizeof(struct acpi_facs);
379 current = ALIGN(current, 16);
380
381 acpi_create_facs(facs);
382
383 debug("ACPI: * DSDT\n");
Bin Meng6a421582016-05-07 07:46:21 -0700384 dsdt = (struct acpi_table_header *)current;
385 memcpy(dsdt, &AmlCode, sizeof(struct acpi_table_header));
Bin Mengd90434b2016-05-11 07:45:05 -0700386 current += sizeof(struct acpi_table_header);
387 memcpy((char *)current,
388 (char *)&AmlCode + sizeof(struct acpi_table_header),
389 dsdt->length - sizeof(struct acpi_table_header));
390 current += dsdt->length - sizeof(struct acpi_table_header);
Saket Sinha331141a2015-08-22 12:20:55 +0530391 current = ALIGN(current, 16);
392
Bin Mengd9050c62016-06-17 02:13:16 -0700393 /* Pack GNVS into the ACPI table area */
394 for (i = 0; i < dsdt->length; i++) {
395 u32 *gnvs = (u32 *)((u32)dsdt + i);
396 if (*gnvs == ACPI_GNVS_ADDR) {
397 debug("Fix up global NVS in DSDT to 0x%08x\n", current);
398 *gnvs = current;
399 break;
400 }
401 }
402
403 /* Update DSDT checksum since we patched the GNVS address */
404 dsdt->checksum = 0;
405 dsdt->checksum = table_compute_checksum((void *)dsdt, dsdt->length);
406
407 /* Fill in platform-specific global NVS variables */
408 acpi_create_gnvs((struct acpi_global_nvs *)current);
409 current += sizeof(struct acpi_global_nvs);
410 current = ALIGN(current, 16);
411
Saket Sinha331141a2015-08-22 12:20:55 +0530412 debug("ACPI: * FADT\n");
413 fadt = (struct acpi_fadt *)current;
414 current += sizeof(struct acpi_fadt);
415 current = ALIGN(current, 16);
416 acpi_create_fadt(fadt, facs, dsdt);
417 acpi_add_table(rsdp, fadt);
418
Saket Sinha331141a2015-08-22 12:20:55 +0530419 debug("ACPI: * MADT\n");
420 madt = (struct acpi_madt *)current;
421 acpi_create_madt(madt);
Bin Mengd90434b2016-05-11 07:45:05 -0700422 current += madt->header.length;
423 acpi_add_table(rsdp, madt);
Saket Sinha331141a2015-08-22 12:20:55 +0530424 current = ALIGN(current, 16);
425
Bin Meng44256b02016-05-07 07:46:25 -0700426 debug("ACPI: * MCFG\n");
427 mcfg = (struct acpi_mcfg *)current;
428 acpi_create_mcfg(mcfg);
Bin Mengd90434b2016-05-11 07:45:05 -0700429 current += mcfg->header.length;
430 acpi_add_table(rsdp, mcfg);
431 current = ALIGN(current, 16);
Bin Meng44256b02016-05-07 07:46:25 -0700432
Bin Mengd2d22182016-05-07 07:46:12 -0700433 debug("current = %x\n", current);
Saket Sinha331141a2015-08-22 12:20:55 +0530434
Bin Mengd2d22182016-05-07 07:46:12 -0700435 debug("ACPI: done\n");
Saket Sinha331141a2015-08-22 12:20:55 +0530436
Bin Menge259eb22016-05-11 07:45:03 -0700437 /*
438 * Other than waiting for OSPM to request us to switch to ACPI mode,
439 * do it by ourselves, since SMI will not be triggered.
440 */
441 enter_acpi_mode(fadt->pm1a_cnt_blk);
442
Saket Sinha331141a2015-08-22 12:20:55 +0530443 return current;
444}
Bin Meng34bc74a2017-04-21 07:24:36 -0700445
446static struct acpi_rsdp *acpi_valid_rsdp(struct acpi_rsdp *rsdp)
447{
448 if (strncmp((char *)rsdp, RSDP_SIG, sizeof(RSDP_SIG) - 1) != 0)
449 return NULL;
450
451 debug("Looking on %p for valid checksum\n", rsdp);
452
453 if (table_compute_checksum((void *)rsdp, 20) != 0)
454 return NULL;
455 debug("acpi rsdp checksum 1 passed\n");
456
457 if ((rsdp->revision > 1) &&
458 (table_compute_checksum((void *)rsdp, rsdp->length) != 0))
459 return NULL;
460 debug("acpi rsdp checksum 2 passed\n");
461
462 return rsdp;
463}
464
Bin Meng280aebe2017-04-21 07:24:44 -0700465struct acpi_fadt *acpi_find_fadt(void)
Bin Meng34bc74a2017-04-21 07:24:36 -0700466{
467 char *p, *end;
468 struct acpi_rsdp *rsdp = NULL;
469 struct acpi_rsdt *rsdt;
470 struct acpi_fadt *fadt = NULL;
Bin Meng34bc74a2017-04-21 07:24:36 -0700471 int i;
472
Bin Meng34bc74a2017-04-21 07:24:36 -0700473 /* Find RSDP */
474 for (p = (char *)ROM_TABLE_ADDR; p < (char *)ROM_TABLE_END; p += 16) {
475 rsdp = acpi_valid_rsdp((struct acpi_rsdp *)p);
476 if (rsdp)
477 break;
478 }
479
480 if (rsdp == NULL)
481 return NULL;
482
483 debug("RSDP found at %p\n", rsdp);
484 rsdt = (struct acpi_rsdt *)rsdp->rsdt_address;
485
486 end = (char *)rsdt + rsdt->header.length;
487 debug("RSDT found at %p ends at %p\n", rsdt, end);
488
489 for (i = 0; ((char *)&rsdt->entry[i]) < end; i++) {
490 fadt = (struct acpi_fadt *)rsdt->entry[i];
491 if (strncmp((char *)fadt, "FACP", 4) == 0)
492 break;
493 fadt = NULL;
494 }
495
496 if (fadt == NULL)
497 return NULL;
498
499 debug("FADT found at %p\n", fadt);
Bin Meng280aebe2017-04-21 07:24:44 -0700500 return fadt;
501}
502
503void *acpi_find_wakeup_vector(struct acpi_fadt *fadt)
504{
505 struct acpi_facs *facs;
506 void *wake_vec;
507
508 debug("Trying to find the wakeup vector...\n");
509
Bin Meng34bc74a2017-04-21 07:24:36 -0700510 facs = (struct acpi_facs *)fadt->firmware_ctrl;
511
512 if (facs == NULL) {
513 debug("No FACS found, wake up from S3 not possible.\n");
514 return NULL;
515 }
516
517 debug("FACS found at %p\n", facs);
518 wake_vec = (void *)facs->firmware_waking_vector;
519 debug("OS waking vector is %p\n", wake_vec);
520
521 return wake_vec;
522}