blob: c543f1e3c290f9dfa9db1434dce1ebea6f314ece [file] [log] [blame]
Simon Glassfaf08c72020-04-26 09:19:53 -06001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2019 Google LLC
4 * Written by Simon Glass <sjg@chromium.org>
5 */
6#include <common.h>
7#include <command.h>
8#include <mapmem.h>
9#include <acpi/acpi_table.h>
10#include <asm/acpi_table.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060011#include <asm/global_data.h>
Simon Glassfaf08c72020-04-26 09:19:53 -060012#include <dm/acpi.h>
13
14DECLARE_GLOBAL_DATA_PTR;
15
16/**
17 * dump_hdr() - Dump an ACPI header
18 *
19 * If the header is for FACS then it shows the revision information as well
20 *
21 * @hdr: ACPI header to dump
22 */
23static void dump_hdr(struct acpi_table_header *hdr)
24{
25 bool has_hdr = memcmp(hdr->signature, "FACS", ACPI_NAME_LEN);
26
Simon Glasseb025f32021-12-01 09:03:06 -070027 printf("%.*s %08lx %5x", ACPI_NAME_LEN, hdr->signature,
Simon Glassfaf08c72020-04-26 09:19:53 -060028 (ulong)map_to_sysmem(hdr), hdr->length);
29 if (has_hdr) {
Simon Glasseb025f32021-12-01 09:03:06 -070030 printf(" v%02d %.6s %.8s %x %.4s %x\n", hdr->revision,
Simon Glassfaf08c72020-04-26 09:19:53 -060031 hdr->oem_id, hdr->oem_table_id, hdr->oem_revision,
32 hdr->aslc_id, hdr->aslc_revision);
33 } else {
34 printf("\n");
35 }
36}
37
38/**
39 * find_table() - Look up an ACPI table
40 *
41 * @sig: Signature of table (4 characters, upper case)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010042 * Return: pointer to table header, or NULL if not found
Simon Glassfaf08c72020-04-26 09:19:53 -060043 */
44struct acpi_table_header *find_table(const char *sig)
45{
46 struct acpi_rsdp *rsdp;
47 struct acpi_rsdt *rsdt;
48 int len, i, count;
49
Simon Glassb33f2f92021-12-01 09:02:37 -070050 rsdp = map_sysmem(gd_acpi_start(), 0);
Simon Glassfaf08c72020-04-26 09:19:53 -060051 if (!rsdp)
52 return NULL;
53 rsdt = map_sysmem(rsdp->rsdt_address, 0);
54 len = rsdt->header.length - sizeof(rsdt->header);
55 count = len / sizeof(u32);
56 for (i = 0; i < count; i++) {
57 struct acpi_table_header *hdr;
58
59 hdr = map_sysmem(rsdt->entry[i], 0);
60 if (!memcmp(hdr->signature, sig, ACPI_NAME_LEN))
61 return hdr;
62 if (!memcmp(hdr->signature, "FACP", ACPI_NAME_LEN)) {
63 struct acpi_fadt *fadt = (struct acpi_fadt *)hdr;
64
65 if (!memcmp(sig, "DSDT", ACPI_NAME_LEN) && fadt->dsdt)
66 return map_sysmem(fadt->dsdt, 0);
67 if (!memcmp(sig, "FACS", ACPI_NAME_LEN) &&
68 fadt->firmware_ctrl)
69 return map_sysmem(fadt->firmware_ctrl, 0);
70 }
71 }
72
73 return NULL;
74}
75
76static int dump_table_name(const char *sig)
77{
78 struct acpi_table_header *hdr;
79
80 hdr = find_table(sig);
81 if (!hdr)
82 return -ENOENT;
83 printf("%.*s @ %08lx\n", ACPI_NAME_LEN, hdr->signature,
84 (ulong)map_to_sysmem(hdr));
85 print_buffer(0, hdr, 1, hdr->length, 0);
86
87 return 0;
88}
89
90static void list_fadt(struct acpi_fadt *fadt)
91{
92 if (fadt->dsdt)
93 dump_hdr(map_sysmem(fadt->dsdt, 0));
94 if (fadt->firmware_ctrl)
95 dump_hdr(map_sysmem(fadt->firmware_ctrl, 0));
96}
97
98static int list_rsdt(struct acpi_rsdt *rsdt, struct acpi_xsdt *xsdt)
99{
100 int len, i, count;
101
102 dump_hdr(&rsdt->header);
103 if (xsdt)
104 dump_hdr(&xsdt->header);
105 len = rsdt->header.length - sizeof(rsdt->header);
106 count = len / sizeof(u32);
107 for (i = 0; i < count; i++) {
108 struct acpi_table_header *hdr;
109
110 if (!rsdt->entry[i])
111 break;
112 hdr = map_sysmem(rsdt->entry[i], 0);
113 dump_hdr(hdr);
114 if (!memcmp(hdr->signature, "FACP", ACPI_NAME_LEN))
115 list_fadt((struct acpi_fadt *)hdr);
116 if (xsdt) {
117 if (xsdt->entry[i] != rsdt->entry[i]) {
118 printf(" (xsdt mismatch %llx)\n",
119 xsdt->entry[i]);
120 }
121 }
122 }
123
124 return 0;
125}
126
127static int list_rsdp(struct acpi_rsdp *rsdp)
128{
129 struct acpi_rsdt *rsdt;
130 struct acpi_xsdt *xsdt;
131
Simon Glasseb025f32021-12-01 09:03:06 -0700132 printf("RSDP %08lx %5x v%02d %.6s\n", (ulong)map_to_sysmem(rsdp),
Simon Glassfaf08c72020-04-26 09:19:53 -0600133 rsdp->length, rsdp->revision, rsdp->oem_id);
134 rsdt = map_sysmem(rsdp->rsdt_address, 0);
135 xsdt = map_sysmem(rsdp->xsdt_address, 0);
136 list_rsdt(rsdt, xsdt);
137
138 return 0;
139}
140
Simon Glassed38aef2020-05-10 11:40:03 -0600141static int do_acpi_list(struct cmd_tbl *cmdtp, int flag, int argc,
Simon Glassfaf08c72020-04-26 09:19:53 -0600142 char *const argv[])
143{
144 struct acpi_rsdp *rsdp;
145
Simon Glassb33f2f92021-12-01 09:02:37 -0700146 rsdp = map_sysmem(gd_acpi_start(), 0);
Simon Glassfaf08c72020-04-26 09:19:53 -0600147 if (!rsdp) {
148 printf("No ACPI tables present\n");
149 return 0;
150 }
Simon Glasseb025f32021-12-01 09:03:06 -0700151 printf("Name Base Size Detail\n");
152 printf("---- -------- ----- ------\n");
Simon Glassfaf08c72020-04-26 09:19:53 -0600153 list_rsdp(rsdp);
154
155 return 0;
156}
157
Simon Glass64bbc0d2020-07-07 13:12:12 -0600158static int do_acpi_items(struct cmd_tbl *cmdtp, int flag, int argc,
159 char *const argv[])
160{
161 bool dump_contents;
162
163 dump_contents = argc >= 2 && !strcmp("-d", argv[1]);
164 acpi_dump_items(dump_contents ? ACPI_DUMP_CONTENTS : ACPI_DUMP_LIST);
165
166 return 0;
167}
168
Simon Glassed38aef2020-05-10 11:40:03 -0600169static int do_acpi_dump(struct cmd_tbl *cmdtp, int flag, int argc,
Simon Glassfaf08c72020-04-26 09:19:53 -0600170 char *const argv[])
171{
172 const char *name;
173 char sig[ACPI_NAME_LEN];
174 int ret;
175
Simon Glassfaf08c72020-04-26 09:19:53 -0600176 name = argv[1];
177 if (strlen(name) != ACPI_NAME_LEN) {
178 printf("Table name '%s' must be four characters\n", name);
179 return CMD_RET_FAILURE;
180 }
181 str_to_upper(name, sig, -1);
182 ret = dump_table_name(sig);
183 if (ret) {
184 printf("Table '%.*s' not found\n", ACPI_NAME_LEN, sig);
185 return CMD_RET_FAILURE;
186 }
187
188 return 0;
189}
190
Simon Glassac8d9d52021-03-15 18:00:25 +1300191#ifdef CONFIG_SYS_LONGHELP
Simon Glassfaf08c72020-04-26 09:19:53 -0600192static char acpi_help_text[] =
193 "list - list ACPI tables\n"
Simon Glass64bbc0d2020-07-07 13:12:12 -0600194 "acpi items [-d] - List/dump each piece of ACPI data from devices\n"
Simon Glassfaf08c72020-04-26 09:19:53 -0600195 "acpi dump <name> - Dump ACPI table";
Simon Glassac8d9d52021-03-15 18:00:25 +1300196#endif
Simon Glassfaf08c72020-04-26 09:19:53 -0600197
198U_BOOT_CMD_WITH_SUBCMDS(acpi, "ACPI tables", acpi_help_text,
199 U_BOOT_SUBCMD_MKENT(list, 1, 1, do_acpi_list),
Simon Glass64bbc0d2020-07-07 13:12:12 -0600200 U_BOOT_SUBCMD_MKENT(items, 2, 1, do_acpi_items),
Simon Glassfaf08c72020-04-26 09:19:53 -0600201 U_BOOT_SUBCMD_MKENT(dump, 2, 1, do_acpi_dump));