Simon Glass | b2672ea | 2020-04-08 16:57:38 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Generic code used to generate ACPI tables |
| 4 | * |
| 5 | * Copyright 2019 Google LLC |
| 6 | */ |
| 7 | |
Simon Glass | e962989 | 2020-04-08 16:57:39 -0600 | [diff] [blame] | 8 | #include <dm.h> |
| 9 | #include <cpu.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Simon Glass | 575a547 | 2020-04-26 09:19:50 -0600 | [diff] [blame] | 11 | #include <mapmem.h> |
| 12 | #include <tables_csum.h> |
Simon Glass | 90b0127 | 2023-04-29 19:21:46 -0600 | [diff] [blame] | 13 | #include <version_string.h> |
Simon Glass | 0e11384 | 2020-04-26 09:19:47 -0600 | [diff] [blame] | 14 | #include <acpi/acpi_table.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 15 | #include <asm/global_data.h> |
Simon Glass | 0e11384 | 2020-04-26 09:19:47 -0600 | [diff] [blame] | 16 | #include <dm/acpi.h> |
Simon Glass | e962989 | 2020-04-08 16:57:39 -0600 | [diff] [blame] | 17 | |
Pali Rohár | f0de20e | 2021-07-10 13:10:01 +0200 | [diff] [blame] | 18 | /* |
| 19 | * OEM_REVISION is 32-bit unsigned number. It should be increased only when |
| 20 | * changing software version. Therefore it should not depend on build time. |
| 21 | * U-Boot calculates it from U-Boot version and represent it in hexadecimal |
| 22 | * notation. As U-Boot version is in form year.month set low 8 bits to 0x01 |
| 23 | * to have valid date. So for U-Boot version 2021.04 OEM_REVISION is set to |
| 24 | * value 0x20210401. |
| 25 | */ |
Simon Glass | 90b0127 | 2023-04-29 19:21:46 -0600 | [diff] [blame] | 26 | #define OEM_REVISION ((((version_num / 1000) % 10) << 28) | \ |
| 27 | (((version_num / 100) % 10) << 24) | \ |
| 28 | (((version_num / 10) % 10) << 20) | \ |
| 29 | ((version_num % 10) << 16) | \ |
| 30 | (((version_num_patch / 10) % 10) << 12) | \ |
| 31 | ((version_num_patch % 10) << 8) | \ |
Pali Rohár | f0de20e | 2021-07-10 13:10:01 +0200 | [diff] [blame] | 32 | 0x01) |
| 33 | |
Simon Glass | e962989 | 2020-04-08 16:57:39 -0600 | [diff] [blame] | 34 | int acpi_create_dmar(struct acpi_dmar *dmar, enum dmar_flags flags) |
| 35 | { |
| 36 | struct acpi_table_header *header = &dmar->header; |
| 37 | struct cpu_info info; |
| 38 | struct udevice *cpu; |
| 39 | int ret; |
| 40 | |
Michal Suchanek | ac12a2f | 2022-10-12 21:57:59 +0200 | [diff] [blame] | 41 | ret = uclass_first_device_err(UCLASS_CPU, &cpu); |
Simon Glass | e962989 | 2020-04-08 16:57:39 -0600 | [diff] [blame] | 42 | if (ret) |
| 43 | return log_msg_ret("cpu", ret); |
| 44 | ret = cpu_get_info(cpu, &info); |
| 45 | if (ret) |
| 46 | return log_msg_ret("info", ret); |
| 47 | memset((void *)dmar, 0, sizeof(struct acpi_dmar)); |
| 48 | |
| 49 | /* Fill out header fields. */ |
| 50 | acpi_fill_header(&dmar->header, "DMAR"); |
| 51 | header->length = sizeof(struct acpi_dmar); |
| 52 | header->revision = acpi_get_table_revision(ACPITAB_DMAR); |
| 53 | |
| 54 | dmar->host_address_width = info.address_width - 1; |
| 55 | dmar->flags = flags; |
| 56 | |
| 57 | return 0; |
| 58 | } |
Simon Glass | b2672ea | 2020-04-08 16:57:38 -0600 | [diff] [blame] | 59 | |
| 60 | int acpi_get_table_revision(enum acpi_tables table) |
| 61 | { |
| 62 | switch (table) { |
| 63 | case ACPITAB_FADT: |
| 64 | return ACPI_FADT_REV_ACPI_3_0; |
| 65 | case ACPITAB_MADT: |
| 66 | return ACPI_MADT_REV_ACPI_3_0; |
| 67 | case ACPITAB_MCFG: |
| 68 | return ACPI_MCFG_REV_ACPI_3_0; |
| 69 | case ACPITAB_TCPA: |
| 70 | /* This version and the rest are open-coded */ |
| 71 | return 2; |
| 72 | case ACPITAB_TPM2: |
| 73 | return 4; |
| 74 | case ACPITAB_SSDT: /* ACPI 3.0 upto 6.3: 2 */ |
| 75 | return 2; |
| 76 | case ACPITAB_SRAT: /* ACPI 2.0: 1, ACPI 3.0: 2, ACPI 4.0 to 6.3: 3 */ |
| 77 | return 1; /* TODO Should probably be upgraded to 2 */ |
| 78 | case ACPITAB_DMAR: |
| 79 | return 1; |
| 80 | case ACPITAB_SLIT: /* ACPI 2.0 upto 6.3: 1 */ |
| 81 | return 1; |
| 82 | case ACPITAB_SPMI: /* IMPI 2.0 */ |
| 83 | return 5; |
| 84 | case ACPITAB_HPET: /* Currently 1. Table added in ACPI 2.0 */ |
| 85 | return 1; |
| 86 | case ACPITAB_VFCT: /* ACPI 2.0/3.0/4.0: 1 */ |
| 87 | return 1; |
| 88 | case ACPITAB_IVRS: |
| 89 | return IVRS_FORMAT_FIXED; |
| 90 | case ACPITAB_DBG2: |
| 91 | return 0; |
| 92 | case ACPITAB_FACS: /* ACPI 2.0/3.0: 1, ACPI 4.0 to 6.3: 2 */ |
| 93 | return 1; |
| 94 | case ACPITAB_RSDT: /* ACPI 1.0 upto 6.3: 1 */ |
| 95 | return 1; |
| 96 | case ACPITAB_XSDT: /* ACPI 2.0 upto 6.3: 1 */ |
| 97 | return 1; |
| 98 | case ACPITAB_RSDP: /* ACPI 2.0 upto 6.3: 2 */ |
| 99 | return 2; |
| 100 | case ACPITAB_HEST: |
| 101 | return 1; |
| 102 | case ACPITAB_NHLT: |
| 103 | return 5; |
| 104 | case ACPITAB_BERT: |
| 105 | return 1; |
| 106 | case ACPITAB_SPCR: |
| 107 | return 2; |
| 108 | default: |
| 109 | return -EINVAL; |
| 110 | } |
| 111 | } |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 112 | |
| 113 | void acpi_fill_header(struct acpi_table_header *header, char *signature) |
| 114 | { |
| 115 | memcpy(header->signature, signature, 4); |
| 116 | memcpy(header->oem_id, OEM_ID, 6); |
| 117 | memcpy(header->oem_table_id, OEM_TABLE_ID, 8); |
Pali Rohár | f0de20e | 2021-07-10 13:10:01 +0200 | [diff] [blame] | 118 | header->oem_revision = OEM_REVISION; |
Heinrich Schuchardt | 10de8a8 | 2024-01-21 12:52:48 +0100 | [diff] [blame] | 119 | memcpy(header->creator_id, ASLC_ID, 4); |
Heinrich Schuchardt | 9838d34 | 2024-04-18 05:11:13 +0200 | [diff] [blame] | 120 | header->creator_revision = ASL_REVISION; |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 121 | } |
Simon Glass | 0e11384 | 2020-04-26 09:19:47 -0600 | [diff] [blame] | 122 | |
| 123 | void acpi_align(struct acpi_ctx *ctx) |
| 124 | { |
| 125 | ctx->current = (void *)ALIGN((ulong)ctx->current, 16); |
| 126 | } |
| 127 | |
| 128 | void acpi_align64(struct acpi_ctx *ctx) |
| 129 | { |
| 130 | ctx->current = (void *)ALIGN((ulong)ctx->current, 64); |
| 131 | } |
| 132 | |
| 133 | void acpi_inc(struct acpi_ctx *ctx, uint amount) |
| 134 | { |
| 135 | ctx->current += amount; |
| 136 | } |
| 137 | |
| 138 | void acpi_inc_align(struct acpi_ctx *ctx, uint amount) |
| 139 | { |
| 140 | ctx->current += amount; |
| 141 | acpi_align(ctx); |
| 142 | } |
Simon Glass | 575a547 | 2020-04-26 09:19:50 -0600 | [diff] [blame] | 143 | |
| 144 | /** |
| 145 | * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length |
| 146 | * and checksum. |
| 147 | */ |
| 148 | int acpi_add_table(struct acpi_ctx *ctx, void *table) |
| 149 | { |
| 150 | int i, entries_num; |
| 151 | struct acpi_rsdt *rsdt; |
| 152 | struct acpi_xsdt *xsdt; |
| 153 | |
| 154 | /* The RSDT is mandatory while the XSDT is not */ |
| 155 | rsdt = ctx->rsdt; |
| 156 | |
| 157 | /* This should always be MAX_ACPI_TABLES */ |
| 158 | entries_num = ARRAY_SIZE(rsdt->entry); |
| 159 | |
| 160 | for (i = 0; i < entries_num; i++) { |
| 161 | if (rsdt->entry[i] == 0) |
| 162 | break; |
| 163 | } |
| 164 | |
| 165 | if (i >= entries_num) { |
| 166 | log_err("ACPI: Error: too many tables\n"); |
| 167 | return -E2BIG; |
| 168 | } |
| 169 | |
| 170 | /* Add table to the RSDT */ |
Simon Glass | 919f835 | 2023-12-31 08:25:54 -0700 | [diff] [blame] | 171 | rsdt->entry[i] = nomap_to_sysmem(table); |
Simon Glass | 575a547 | 2020-04-26 09:19:50 -0600 | [diff] [blame] | 172 | |
| 173 | /* Fix RSDT length or the kernel will assume invalid entries */ |
| 174 | rsdt->header.length = sizeof(struct acpi_table_header) + |
| 175 | (sizeof(u32) * (i + 1)); |
| 176 | |
| 177 | /* Re-calculate checksum */ |
| 178 | rsdt->header.checksum = 0; |
| 179 | rsdt->header.checksum = table_compute_checksum((u8 *)rsdt, |
| 180 | rsdt->header.length); |
| 181 | |
| 182 | /* |
| 183 | * And now the same thing for the XSDT. We use the same index as for |
| 184 | * now we want the XSDT and RSDT to always be in sync in U-Boot |
| 185 | */ |
Simon Glass | abeaca8 | 2020-04-26 09:19:52 -0600 | [diff] [blame] | 186 | xsdt = ctx->xsdt; |
Simon Glass | 575a547 | 2020-04-26 09:19:50 -0600 | [diff] [blame] | 187 | |
| 188 | /* Add table to the XSDT */ |
Simon Glass | 919f835 | 2023-12-31 08:25:54 -0700 | [diff] [blame] | 189 | xsdt->entry[i] = nomap_to_sysmem(table); |
Simon Glass | 575a547 | 2020-04-26 09:19:50 -0600 | [diff] [blame] | 190 | |
| 191 | /* Fix XSDT length */ |
| 192 | xsdt->header.length = sizeof(struct acpi_table_header) + |
| 193 | (sizeof(u64) * (i + 1)); |
| 194 | |
| 195 | /* Re-calculate checksum */ |
| 196 | xsdt->header.checksum = 0; |
| 197 | xsdt->header.checksum = table_compute_checksum((u8 *)xsdt, |
| 198 | xsdt->header.length); |
| 199 | |
| 200 | return 0; |
| 201 | } |
Simon Glass | 9c442a6 | 2020-04-26 09:19:51 -0600 | [diff] [blame] | 202 | |
Simon Glass | 9597189 | 2020-09-22 12:45:10 -0600 | [diff] [blame] | 203 | void acpi_create_dbg2(struct acpi_dbg2_header *dbg2, |
| 204 | int port_type, int port_subtype, |
| 205 | struct acpi_gen_regaddr *address, u32 address_size, |
| 206 | const char *device_path) |
| 207 | { |
| 208 | uintptr_t current; |
| 209 | struct acpi_dbg2_device *device; |
| 210 | u32 *dbg2_addr_size; |
| 211 | struct acpi_table_header *header; |
| 212 | size_t path_len; |
| 213 | const char *path; |
| 214 | char *namespace; |
| 215 | |
| 216 | /* Fill out header fields. */ |
| 217 | current = (uintptr_t)dbg2; |
| 218 | memset(dbg2, '\0', sizeof(struct acpi_dbg2_header)); |
| 219 | header = &dbg2->header; |
| 220 | |
| 221 | header->revision = acpi_get_table_revision(ACPITAB_DBG2); |
| 222 | acpi_fill_header(header, "DBG2"); |
Simon Glass | 9597189 | 2020-09-22 12:45:10 -0600 | [diff] [blame] | 223 | |
| 224 | /* One debug device defined */ |
| 225 | dbg2->devices_offset = sizeof(struct acpi_dbg2_header); |
| 226 | dbg2->devices_count = 1; |
| 227 | current += sizeof(struct acpi_dbg2_header); |
| 228 | |
| 229 | /* Device comes after the header */ |
| 230 | device = (struct acpi_dbg2_device *)current; |
| 231 | memset(device, 0, sizeof(struct acpi_dbg2_device)); |
| 232 | current += sizeof(struct acpi_dbg2_device); |
| 233 | |
| 234 | device->revision = 0; |
| 235 | device->address_count = 1; |
| 236 | device->port_type = port_type; |
| 237 | device->port_subtype = port_subtype; |
| 238 | |
| 239 | /* Base Address comes after device structure */ |
| 240 | memcpy((void *)current, address, sizeof(struct acpi_gen_regaddr)); |
| 241 | device->base_address_offset = current - (uintptr_t)device; |
| 242 | current += sizeof(struct acpi_gen_regaddr); |
| 243 | |
| 244 | /* Address Size comes after address structure */ |
| 245 | dbg2_addr_size = (uint32_t *)current; |
| 246 | device->address_size_offset = current - (uintptr_t)device; |
| 247 | *dbg2_addr_size = address_size; |
| 248 | current += sizeof(uint32_t); |
| 249 | |
| 250 | /* Namespace string comes last, use '.' if not provided */ |
| 251 | path = device_path ? : "."; |
| 252 | /* Namespace string length includes NULL terminator */ |
| 253 | path_len = strlen(path) + 1; |
| 254 | namespace = (char *)current; |
| 255 | device->namespace_string_length = path_len; |
| 256 | device->namespace_string_offset = current - (uintptr_t)device; |
| 257 | strncpy(namespace, path, path_len); |
| 258 | current += path_len; |
| 259 | |
| 260 | /* Update structure lengths and checksum */ |
| 261 | device->length = current - (uintptr_t)device; |
| 262 | header->length = current - (uintptr_t)dbg2; |
| 263 | header->checksum = table_compute_checksum(dbg2, header->length); |
| 264 | } |