blob: f21e5094615c23c8fbe2a19b91b12a0b1d4660b8 [file] [log] [blame]
Simon Glass7ae69762023-05-04 16:54:58 -06001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Utility functions for ACPI
4 *
5 * Copyright 2023 Google LLC
6 */
7
8#include <common.h>
9#include <mapmem.h>
10#include <acpi/acpi_table.h>
11#include <asm/global_data.h>
12
13DECLARE_GLOBAL_DATA_PTR;
14
15struct acpi_table_header *acpi_find_table(const char *sig)
16{
17 struct acpi_rsdp *rsdp;
18 struct acpi_rsdt *rsdt;
Heinrich Schuchardt23c14e12023-11-18 22:57:26 +010019 struct acpi_xsdt *xsdt;
Simon Glass7ae69762023-05-04 16:54:58 -060020 int len, i, count;
21
22 rsdp = map_sysmem(gd_acpi_start(), 0);
23 if (!rsdp)
24 return NULL;
Heinrich Schuchardt23c14e12023-11-18 22:57:26 +010025 if (rsdp->xsdt_address) {
26 xsdt = map_sysmem(rsdp->xsdt_address, 0);
27 len = xsdt->header.length - sizeof(xsdt->header);
28 count = len / sizeof(u64);
29 } else {
30 if (!rsdp->rsdt_address)
31 return NULL;
32 rsdt = map_sysmem(rsdp->rsdt_address, 0);
33 len = rsdt->header.length - sizeof(rsdt->header);
34 count = len / sizeof(u32);
35 }
Simon Glass7ae69762023-05-04 16:54:58 -060036 for (i = 0; i < count; i++) {
37 struct acpi_table_header *hdr;
38
Heinrich Schuchardt23c14e12023-11-18 22:57:26 +010039 if (rsdp->xsdt_address)
40 hdr = map_sysmem(xsdt->entry[i], 0);
41 else
42 hdr = map_sysmem(rsdt->entry[i], 0);
Simon Glass7ae69762023-05-04 16:54:58 -060043 if (!memcmp(hdr->signature, sig, ACPI_NAME_LEN))
44 return hdr;
45 if (!memcmp(hdr->signature, "FACP", ACPI_NAME_LEN)) {
46 struct acpi_fadt *fadt = (struct acpi_fadt *)hdr;
47
48 if (!memcmp(sig, "DSDT", ACPI_NAME_LEN) && fadt->dsdt)
49 return map_sysmem(fadt->dsdt, 0);
50 if (!memcmp(sig, "FACS", ACPI_NAME_LEN) &&
51 fadt->firmware_ctrl)
52 return map_sysmem(fadt->firmware_ctrl, 0);
53 }
54 }
55
56 return NULL;
57}