rockchip: Remove ARCH= references from documentation
[oweals/u-boot.git] / cmd / acpi.c
1 // 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>
11 #include <dm/acpi.h>
12
13 DECLARE_GLOBAL_DATA_PTR;
14
15 /**
16  * dump_hdr() - Dump an ACPI header
17  *
18  * If the header is for FACS then it shows the revision information as well
19  *
20  * @hdr: ACPI header to dump
21  */
22 static void dump_hdr(struct acpi_table_header *hdr)
23 {
24         bool has_hdr = memcmp(hdr->signature, "FACS", ACPI_NAME_LEN);
25
26         printf("%.*s %08lx %06x", ACPI_NAME_LEN, hdr->signature,
27                (ulong)map_to_sysmem(hdr), hdr->length);
28         if (has_hdr) {
29                 printf(" (v%02d %.6s %.8s %u %.4s %d)\n", hdr->revision,
30                        hdr->oem_id, hdr->oem_table_id, hdr->oem_revision,
31                        hdr->aslc_id, hdr->aslc_revision);
32         } else {
33                 printf("\n");
34         }
35 }
36
37 /**
38  * find_table() - Look up an ACPI table
39  *
40  * @sig: Signature of table (4 characters, upper case)
41  * @return pointer to table header, or NULL if not found
42  */
43 struct acpi_table_header *find_table(const char *sig)
44 {
45         struct acpi_rsdp *rsdp;
46         struct acpi_rsdt *rsdt;
47         int len, i, count;
48
49         rsdp = map_sysmem(gd->arch.acpi_start, 0);
50         if (!rsdp)
51                 return NULL;
52         rsdt = map_sysmem(rsdp->rsdt_address, 0);
53         len = rsdt->header.length - sizeof(rsdt->header);
54         count = len / sizeof(u32);
55         for (i = 0; i < count; i++) {
56                 struct acpi_table_header *hdr;
57
58                 hdr = map_sysmem(rsdt->entry[i], 0);
59                 if (!memcmp(hdr->signature, sig, ACPI_NAME_LEN))
60                         return hdr;
61                 if (!memcmp(hdr->signature, "FACP", ACPI_NAME_LEN)) {
62                         struct acpi_fadt *fadt = (struct acpi_fadt *)hdr;
63
64                         if (!memcmp(sig, "DSDT", ACPI_NAME_LEN) && fadt->dsdt)
65                                 return map_sysmem(fadt->dsdt, 0);
66                         if (!memcmp(sig, "FACS", ACPI_NAME_LEN) &&
67                             fadt->firmware_ctrl)
68                                 return map_sysmem(fadt->firmware_ctrl, 0);
69                 }
70         }
71
72         return NULL;
73 }
74
75 static int dump_table_name(const char *sig)
76 {
77         struct acpi_table_header *hdr;
78
79         hdr = find_table(sig);
80         if (!hdr)
81                 return -ENOENT;
82         printf("%.*s @ %08lx\n", ACPI_NAME_LEN, hdr->signature,
83                (ulong)map_to_sysmem(hdr));
84         print_buffer(0, hdr, 1, hdr->length, 0);
85
86         return 0;
87 }
88
89 static void list_fadt(struct acpi_fadt *fadt)
90 {
91         if (fadt->dsdt)
92                 dump_hdr(map_sysmem(fadt->dsdt, 0));
93         if (fadt->firmware_ctrl)
94                 dump_hdr(map_sysmem(fadt->firmware_ctrl, 0));
95 }
96
97 static int list_rsdt(struct acpi_rsdt *rsdt, struct acpi_xsdt *xsdt)
98 {
99         int len, i, count;
100
101         dump_hdr(&rsdt->header);
102         if (xsdt)
103                 dump_hdr(&xsdt->header);
104         len = rsdt->header.length - sizeof(rsdt->header);
105         count = len / sizeof(u32);
106         for (i = 0; i < count; i++) {
107                 struct acpi_table_header *hdr;
108
109                 if (!rsdt->entry[i])
110                         break;
111                 hdr = map_sysmem(rsdt->entry[i], 0);
112                 dump_hdr(hdr);
113                 if (!memcmp(hdr->signature, "FACP", ACPI_NAME_LEN))
114                         list_fadt((struct acpi_fadt *)hdr);
115                 if (xsdt) {
116                         if (xsdt->entry[i] != rsdt->entry[i]) {
117                                 printf("   (xsdt mismatch %llx)\n",
118                                        xsdt->entry[i]);
119                         }
120                 }
121         }
122
123         return 0;
124 }
125
126 static int list_rsdp(struct acpi_rsdp *rsdp)
127 {
128         struct acpi_rsdt *rsdt;
129         struct acpi_xsdt *xsdt;
130
131         printf("RSDP %08lx %06x (v%02d %.6s)\n", (ulong)map_to_sysmem(rsdp),
132                rsdp->length, rsdp->revision, rsdp->oem_id);
133         rsdt = map_sysmem(rsdp->rsdt_address, 0);
134         xsdt = map_sysmem(rsdp->xsdt_address, 0);
135         list_rsdt(rsdt, xsdt);
136
137         return 0;
138 }
139
140 static int do_acpi_list(struct cmd_tbl *cmdtp, int flag, int argc,
141                         char *const argv[])
142 {
143         struct acpi_rsdp *rsdp;
144
145         rsdp = map_sysmem(gd->arch.acpi_start, 0);
146         if (!rsdp) {
147                 printf("No ACPI tables present\n");
148                 return 0;
149         }
150         printf("ACPI tables start at %lx\n", gd->arch.acpi_start);
151         list_rsdp(rsdp);
152
153         return 0;
154 }
155
156 static int do_acpi_dump(struct cmd_tbl *cmdtp, int flag, int argc,
157                         char *const argv[])
158 {
159         const char *name;
160         char sig[ACPI_NAME_LEN];
161         int ret;
162
163         if (argc < 2)
164                 return CMD_RET_USAGE;
165         name = argv[1];
166         if (strlen(name) != ACPI_NAME_LEN) {
167                 printf("Table name '%s' must be four characters\n", name);
168                 return CMD_RET_FAILURE;
169         }
170         str_to_upper(name, sig, -1);
171         ret = dump_table_name(sig);
172         if (ret) {
173                 printf("Table '%.*s' not found\n", ACPI_NAME_LEN, sig);
174                 return CMD_RET_FAILURE;
175         }
176
177         return 0;
178 }
179
180 static char acpi_help_text[] =
181         "list - list ACPI tables\n"
182         "acpi dump <name> - Dump ACPI table";
183
184 U_BOOT_CMD_WITH_SUBCMDS(acpi, "ACPI tables", acpi_help_text,
185         U_BOOT_SUBCMD_MKENT(list, 1, 1, do_acpi_list),
186         U_BOOT_SUBCMD_MKENT(dump, 2, 1, do_acpi_dump));