Simon Glass | 0b885bc | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 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> |
Simon Glass | 4e4bf94 | 2022-07-31 12:28:48 -0600 | [diff] [blame] | 8 | #include <display_options.h> |
Simon Glass | 0b885bc | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 9 | #include <mapmem.h> |
| 10 | #include <acpi/acpi_table.h> |
| 11 | #include <asm/acpi_table.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 12 | #include <asm/global_data.h> |
Simon Glass | 0b885bc | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 13 | #include <dm/acpi.h> |
| 14 | |
| 15 | DECLARE_GLOBAL_DATA_PTR; |
| 16 | |
| 17 | /** |
| 18 | * dump_hdr() - Dump an ACPI header |
| 19 | * |
| 20 | * If the header is for FACS then it shows the revision information as well |
| 21 | * |
| 22 | * @hdr: ACPI header to dump |
| 23 | */ |
| 24 | static void dump_hdr(struct acpi_table_header *hdr) |
| 25 | { |
| 26 | bool has_hdr = memcmp(hdr->signature, "FACS", ACPI_NAME_LEN); |
| 27 | |
Simon Glass | 06f6f3d | 2021-12-01 09:03:06 -0700 | [diff] [blame] | 28 | printf("%.*s %08lx %5x", ACPI_NAME_LEN, hdr->signature, |
Simon Glass | 0b885bc | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 29 | (ulong)map_to_sysmem(hdr), hdr->length); |
| 30 | if (has_hdr) { |
Simon Glass | 06f6f3d | 2021-12-01 09:03:06 -0700 | [diff] [blame] | 31 | printf(" v%02d %.6s %.8s %x %.4s %x\n", hdr->revision, |
Simon Glass | 0b885bc | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 32 | hdr->oem_id, hdr->oem_table_id, hdr->oem_revision, |
| 33 | hdr->aslc_id, hdr->aslc_revision); |
| 34 | } else { |
| 35 | printf("\n"); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * find_table() - Look up an ACPI table |
| 41 | * |
| 42 | * @sig: Signature of table (4 characters, upper case) |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 43 | * Return: pointer to table header, or NULL if not found |
Simon Glass | 0b885bc | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 44 | */ |
| 45 | struct acpi_table_header *find_table(const char *sig) |
| 46 | { |
| 47 | struct acpi_rsdp *rsdp; |
| 48 | struct acpi_rsdt *rsdt; |
| 49 | int len, i, count; |
| 50 | |
Simon Glass | 233f0e3 | 2021-12-01 09:02:37 -0700 | [diff] [blame] | 51 | rsdp = map_sysmem(gd_acpi_start(), 0); |
Simon Glass | 0b885bc | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 52 | if (!rsdp) |
| 53 | return NULL; |
| 54 | rsdt = map_sysmem(rsdp->rsdt_address, 0); |
| 55 | len = rsdt->header.length - sizeof(rsdt->header); |
| 56 | count = len / sizeof(u32); |
| 57 | for (i = 0; i < count; i++) { |
| 58 | struct acpi_table_header *hdr; |
| 59 | |
| 60 | hdr = map_sysmem(rsdt->entry[i], 0); |
| 61 | if (!memcmp(hdr->signature, sig, ACPI_NAME_LEN)) |
| 62 | return hdr; |
| 63 | if (!memcmp(hdr->signature, "FACP", ACPI_NAME_LEN)) { |
| 64 | struct acpi_fadt *fadt = (struct acpi_fadt *)hdr; |
| 65 | |
| 66 | if (!memcmp(sig, "DSDT", ACPI_NAME_LEN) && fadt->dsdt) |
| 67 | return map_sysmem(fadt->dsdt, 0); |
| 68 | if (!memcmp(sig, "FACS", ACPI_NAME_LEN) && |
| 69 | fadt->firmware_ctrl) |
| 70 | return map_sysmem(fadt->firmware_ctrl, 0); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return NULL; |
| 75 | } |
| 76 | |
| 77 | static int dump_table_name(const char *sig) |
| 78 | { |
| 79 | struct acpi_table_header *hdr; |
| 80 | |
| 81 | hdr = find_table(sig); |
| 82 | if (!hdr) |
| 83 | return -ENOENT; |
| 84 | printf("%.*s @ %08lx\n", ACPI_NAME_LEN, hdr->signature, |
| 85 | (ulong)map_to_sysmem(hdr)); |
| 86 | print_buffer(0, hdr, 1, hdr->length, 0); |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | static void list_fadt(struct acpi_fadt *fadt) |
| 92 | { |
| 93 | if (fadt->dsdt) |
| 94 | dump_hdr(map_sysmem(fadt->dsdt, 0)); |
| 95 | if (fadt->firmware_ctrl) |
| 96 | dump_hdr(map_sysmem(fadt->firmware_ctrl, 0)); |
| 97 | } |
| 98 | |
| 99 | static int list_rsdt(struct acpi_rsdt *rsdt, struct acpi_xsdt *xsdt) |
| 100 | { |
| 101 | int len, i, count; |
| 102 | |
| 103 | dump_hdr(&rsdt->header); |
| 104 | if (xsdt) |
| 105 | dump_hdr(&xsdt->header); |
| 106 | len = rsdt->header.length - sizeof(rsdt->header); |
| 107 | count = len / sizeof(u32); |
| 108 | for (i = 0; i < count; i++) { |
| 109 | struct acpi_table_header *hdr; |
| 110 | |
| 111 | if (!rsdt->entry[i]) |
| 112 | break; |
| 113 | hdr = map_sysmem(rsdt->entry[i], 0); |
| 114 | dump_hdr(hdr); |
| 115 | if (!memcmp(hdr->signature, "FACP", ACPI_NAME_LEN)) |
| 116 | list_fadt((struct acpi_fadt *)hdr); |
| 117 | if (xsdt) { |
| 118 | if (xsdt->entry[i] != rsdt->entry[i]) { |
| 119 | printf(" (xsdt mismatch %llx)\n", |
| 120 | xsdt->entry[i]); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | static int list_rsdp(struct acpi_rsdp *rsdp) |
| 129 | { |
| 130 | struct acpi_rsdt *rsdt; |
| 131 | struct acpi_xsdt *xsdt; |
| 132 | |
Simon Glass | 06f6f3d | 2021-12-01 09:03:06 -0700 | [diff] [blame] | 133 | printf("RSDP %08lx %5x v%02d %.6s\n", (ulong)map_to_sysmem(rsdp), |
Simon Glass | 0b885bc | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 134 | rsdp->length, rsdp->revision, rsdp->oem_id); |
| 135 | rsdt = map_sysmem(rsdp->rsdt_address, 0); |
| 136 | xsdt = map_sysmem(rsdp->xsdt_address, 0); |
| 137 | list_rsdt(rsdt, xsdt); |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 142 | static int do_acpi_list(struct cmd_tbl *cmdtp, int flag, int argc, |
Simon Glass | 0b885bc | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 143 | char *const argv[]) |
| 144 | { |
| 145 | struct acpi_rsdp *rsdp; |
| 146 | |
Simon Glass | 233f0e3 | 2021-12-01 09:02:37 -0700 | [diff] [blame] | 147 | rsdp = map_sysmem(gd_acpi_start(), 0); |
Simon Glass | 0b885bc | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 148 | if (!rsdp) { |
| 149 | printf("No ACPI tables present\n"); |
| 150 | return 0; |
| 151 | } |
Simon Glass | 06f6f3d | 2021-12-01 09:03:06 -0700 | [diff] [blame] | 152 | printf("Name Base Size Detail\n"); |
| 153 | printf("---- -------- ----- ------\n"); |
Simon Glass | 0b885bc | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 154 | list_rsdp(rsdp); |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
Simon Glass | a4f8208 | 2020-07-07 13:12:12 -0600 | [diff] [blame] | 159 | static int do_acpi_items(struct cmd_tbl *cmdtp, int flag, int argc, |
| 160 | char *const argv[]) |
| 161 | { |
| 162 | bool dump_contents; |
| 163 | |
| 164 | dump_contents = argc >= 2 && !strcmp("-d", argv[1]); |
Simon Glass | 0992a90 | 2023-05-04 16:54:57 -0600 | [diff] [blame^] | 165 | if (!IS_ENABLED(CONFIG_ACPIGEN)) { |
| 166 | printf("Not supported (enable ACPIGEN)\n"); |
| 167 | return CMD_RET_FAILURE; |
| 168 | } |
Simon Glass | a4f8208 | 2020-07-07 13:12:12 -0600 | [diff] [blame] | 169 | acpi_dump_items(dump_contents ? ACPI_DUMP_CONTENTS : ACPI_DUMP_LIST); |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 174 | static int do_acpi_dump(struct cmd_tbl *cmdtp, int flag, int argc, |
Simon Glass | 0b885bc | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 175 | char *const argv[]) |
| 176 | { |
| 177 | const char *name; |
| 178 | char sig[ACPI_NAME_LEN]; |
| 179 | int ret; |
| 180 | |
Simon Glass | 0b885bc | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 181 | name = argv[1]; |
| 182 | if (strlen(name) != ACPI_NAME_LEN) { |
| 183 | printf("Table name '%s' must be four characters\n", name); |
| 184 | return CMD_RET_FAILURE; |
| 185 | } |
Andrew Scull | 3849ca7 | 2022-04-03 10:39:09 +0000 | [diff] [blame] | 186 | str_to_upper(name, sig, ACPI_NAME_LEN); |
Simon Glass | 0b885bc | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 187 | ret = dump_table_name(sig); |
| 188 | if (ret) { |
| 189 | printf("Table '%.*s' not found\n", ACPI_NAME_LEN, sig); |
| 190 | return CMD_RET_FAILURE; |
| 191 | } |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
Simon Glass | 907bcd3 | 2021-03-15 18:00:25 +1300 | [diff] [blame] | 196 | #ifdef CONFIG_SYS_LONGHELP |
Simon Glass | 0b885bc | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 197 | static char acpi_help_text[] = |
| 198 | "list - list ACPI tables\n" |
Simon Glass | a4f8208 | 2020-07-07 13:12:12 -0600 | [diff] [blame] | 199 | "acpi items [-d] - List/dump each piece of ACPI data from devices\n" |
Simon Glass | 0b885bc | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 200 | "acpi dump <name> - Dump ACPI table"; |
Simon Glass | 907bcd3 | 2021-03-15 18:00:25 +1300 | [diff] [blame] | 201 | #endif |
Simon Glass | 0b885bc | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 202 | |
| 203 | U_BOOT_CMD_WITH_SUBCMDS(acpi, "ACPI tables", acpi_help_text, |
| 204 | U_BOOT_SUBCMD_MKENT(list, 1, 1, do_acpi_list), |
Simon Glass | a4f8208 | 2020-07-07 13:12:12 -0600 | [diff] [blame] | 205 | U_BOOT_SUBCMD_MKENT(items, 2, 1, do_acpi_items), |
Simon Glass | 0b885bc | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 206 | U_BOOT_SUBCMD_MKENT(dump, 2, 1, do_acpi_dump)); |