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 | |
| 8 | #include <common.h> |
Simon Glass | e962989 | 2020-04-08 16:57:39 -0600 | [diff] [blame] | 9 | #include <dm.h> |
| 10 | #include <cpu.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Simon Glass | 575a547 | 2020-04-26 09:19:50 -0600 | [diff] [blame] | 12 | #include <mapmem.h> |
| 13 | #include <tables_csum.h> |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 14 | #include <version.h> |
Simon Glass | 0e11384 | 2020-04-26 09:19:47 -0600 | [diff] [blame] | 15 | #include <acpi/acpi_table.h> |
| 16 | #include <dm/acpi.h> |
Simon Glass | e962989 | 2020-04-08 16:57:39 -0600 | [diff] [blame] | 17 | |
Simon Glass | e962989 | 2020-04-08 16:57:39 -0600 | [diff] [blame] | 18 | int acpi_create_dmar(struct acpi_dmar *dmar, enum dmar_flags flags) |
| 19 | { |
| 20 | struct acpi_table_header *header = &dmar->header; |
| 21 | struct cpu_info info; |
| 22 | struct udevice *cpu; |
| 23 | int ret; |
| 24 | |
| 25 | ret = uclass_first_device(UCLASS_CPU, &cpu); |
| 26 | if (ret) |
| 27 | return log_msg_ret("cpu", ret); |
| 28 | ret = cpu_get_info(cpu, &info); |
| 29 | if (ret) |
| 30 | return log_msg_ret("info", ret); |
| 31 | memset((void *)dmar, 0, sizeof(struct acpi_dmar)); |
| 32 | |
| 33 | /* Fill out header fields. */ |
| 34 | acpi_fill_header(&dmar->header, "DMAR"); |
| 35 | header->length = sizeof(struct acpi_dmar); |
| 36 | header->revision = acpi_get_table_revision(ACPITAB_DMAR); |
| 37 | |
| 38 | dmar->host_address_width = info.address_width - 1; |
| 39 | dmar->flags = flags; |
| 40 | |
| 41 | return 0; |
| 42 | } |
Simon Glass | b2672ea | 2020-04-08 16:57:38 -0600 | [diff] [blame] | 43 | |
| 44 | int acpi_get_table_revision(enum acpi_tables table) |
| 45 | { |
| 46 | switch (table) { |
| 47 | case ACPITAB_FADT: |
| 48 | return ACPI_FADT_REV_ACPI_3_0; |
| 49 | case ACPITAB_MADT: |
| 50 | return ACPI_MADT_REV_ACPI_3_0; |
| 51 | case ACPITAB_MCFG: |
| 52 | return ACPI_MCFG_REV_ACPI_3_0; |
| 53 | case ACPITAB_TCPA: |
| 54 | /* This version and the rest are open-coded */ |
| 55 | return 2; |
| 56 | case ACPITAB_TPM2: |
| 57 | return 4; |
| 58 | case ACPITAB_SSDT: /* ACPI 3.0 upto 6.3: 2 */ |
| 59 | return 2; |
| 60 | case ACPITAB_SRAT: /* ACPI 2.0: 1, ACPI 3.0: 2, ACPI 4.0 to 6.3: 3 */ |
| 61 | return 1; /* TODO Should probably be upgraded to 2 */ |
| 62 | case ACPITAB_DMAR: |
| 63 | return 1; |
| 64 | case ACPITAB_SLIT: /* ACPI 2.0 upto 6.3: 1 */ |
| 65 | return 1; |
| 66 | case ACPITAB_SPMI: /* IMPI 2.0 */ |
| 67 | return 5; |
| 68 | case ACPITAB_HPET: /* Currently 1. Table added in ACPI 2.0 */ |
| 69 | return 1; |
| 70 | case ACPITAB_VFCT: /* ACPI 2.0/3.0/4.0: 1 */ |
| 71 | return 1; |
| 72 | case ACPITAB_IVRS: |
| 73 | return IVRS_FORMAT_FIXED; |
| 74 | case ACPITAB_DBG2: |
| 75 | return 0; |
| 76 | case ACPITAB_FACS: /* ACPI 2.0/3.0: 1, ACPI 4.0 to 6.3: 2 */ |
| 77 | return 1; |
| 78 | case ACPITAB_RSDT: /* ACPI 1.0 upto 6.3: 1 */ |
| 79 | return 1; |
| 80 | case ACPITAB_XSDT: /* ACPI 2.0 upto 6.3: 1 */ |
| 81 | return 1; |
| 82 | case ACPITAB_RSDP: /* ACPI 2.0 upto 6.3: 2 */ |
| 83 | return 2; |
| 84 | case ACPITAB_HEST: |
| 85 | return 1; |
| 86 | case ACPITAB_NHLT: |
| 87 | return 5; |
| 88 | case ACPITAB_BERT: |
| 89 | return 1; |
| 90 | case ACPITAB_SPCR: |
| 91 | return 2; |
| 92 | default: |
| 93 | return -EINVAL; |
| 94 | } |
| 95 | } |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 96 | |
| 97 | void acpi_fill_header(struct acpi_table_header *header, char *signature) |
| 98 | { |
| 99 | memcpy(header->signature, signature, 4); |
| 100 | memcpy(header->oem_id, OEM_ID, 6); |
| 101 | memcpy(header->oem_table_id, OEM_TABLE_ID, 8); |
| 102 | header->oem_revision = U_BOOT_BUILD_DATE; |
| 103 | memcpy(header->aslc_id, ASLC_ID, 4); |
| 104 | } |
Simon Glass | 0e11384 | 2020-04-26 09:19:47 -0600 | [diff] [blame] | 105 | |
| 106 | void acpi_align(struct acpi_ctx *ctx) |
| 107 | { |
| 108 | ctx->current = (void *)ALIGN((ulong)ctx->current, 16); |
| 109 | } |
| 110 | |
| 111 | void acpi_align64(struct acpi_ctx *ctx) |
| 112 | { |
| 113 | ctx->current = (void *)ALIGN((ulong)ctx->current, 64); |
| 114 | } |
| 115 | |
| 116 | void acpi_inc(struct acpi_ctx *ctx, uint amount) |
| 117 | { |
| 118 | ctx->current += amount; |
| 119 | } |
| 120 | |
| 121 | void acpi_inc_align(struct acpi_ctx *ctx, uint amount) |
| 122 | { |
| 123 | ctx->current += amount; |
| 124 | acpi_align(ctx); |
| 125 | } |
Simon Glass | 575a547 | 2020-04-26 09:19:50 -0600 | [diff] [blame] | 126 | |
| 127 | /** |
| 128 | * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length |
| 129 | * and checksum. |
| 130 | */ |
| 131 | int acpi_add_table(struct acpi_ctx *ctx, void *table) |
| 132 | { |
| 133 | int i, entries_num; |
| 134 | struct acpi_rsdt *rsdt; |
| 135 | struct acpi_xsdt *xsdt; |
| 136 | |
| 137 | /* The RSDT is mandatory while the XSDT is not */ |
| 138 | rsdt = ctx->rsdt; |
| 139 | |
| 140 | /* This should always be MAX_ACPI_TABLES */ |
| 141 | entries_num = ARRAY_SIZE(rsdt->entry); |
| 142 | |
| 143 | for (i = 0; i < entries_num; i++) { |
| 144 | if (rsdt->entry[i] == 0) |
| 145 | break; |
| 146 | } |
| 147 | |
| 148 | if (i >= entries_num) { |
| 149 | log_err("ACPI: Error: too many tables\n"); |
| 150 | return -E2BIG; |
| 151 | } |
| 152 | |
| 153 | /* Add table to the RSDT */ |
| 154 | rsdt->entry[i] = map_to_sysmem(table); |
| 155 | |
| 156 | /* Fix RSDT length or the kernel will assume invalid entries */ |
| 157 | rsdt->header.length = sizeof(struct acpi_table_header) + |
| 158 | (sizeof(u32) * (i + 1)); |
| 159 | |
| 160 | /* Re-calculate checksum */ |
| 161 | rsdt->header.checksum = 0; |
| 162 | rsdt->header.checksum = table_compute_checksum((u8 *)rsdt, |
| 163 | rsdt->header.length); |
| 164 | |
| 165 | /* |
| 166 | * And now the same thing for the XSDT. We use the same index as for |
| 167 | * now we want the XSDT and RSDT to always be in sync in U-Boot |
| 168 | */ |
Simon Glass | abeaca8 | 2020-04-26 09:19:52 -0600 | [diff] [blame] | 169 | xsdt = ctx->xsdt; |
Simon Glass | 575a547 | 2020-04-26 09:19:50 -0600 | [diff] [blame] | 170 | |
| 171 | /* Add table to the XSDT */ |
| 172 | xsdt->entry[i] = map_to_sysmem(table); |
| 173 | |
| 174 | /* Fix XSDT length */ |
| 175 | xsdt->header.length = sizeof(struct acpi_table_header) + |
| 176 | (sizeof(u64) * (i + 1)); |
| 177 | |
| 178 | /* Re-calculate checksum */ |
| 179 | xsdt->header.checksum = 0; |
| 180 | xsdt->header.checksum = table_compute_checksum((u8 *)xsdt, |
| 181 | xsdt->header.length); |
| 182 | |
| 183 | return 0; |
| 184 | } |
Simon Glass | 9c442a6 | 2020-04-26 09:19:51 -0600 | [diff] [blame] | 185 | |
| 186 | static void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt, |
| 187 | struct acpi_xsdt *xsdt) |
| 188 | { |
| 189 | memset(rsdp, 0, sizeof(struct acpi_rsdp)); |
| 190 | |
| 191 | memcpy(rsdp->signature, RSDP_SIG, 8); |
| 192 | memcpy(rsdp->oem_id, OEM_ID, 6); |
| 193 | |
| 194 | rsdp->length = sizeof(struct acpi_rsdp); |
| 195 | rsdp->rsdt_address = map_to_sysmem(rsdt); |
| 196 | |
| 197 | rsdp->xsdt_address = map_to_sysmem(xsdt); |
| 198 | rsdp->revision = ACPI_RSDP_REV_ACPI_2_0; |
| 199 | |
| 200 | /* Calculate checksums */ |
| 201 | rsdp->checksum = table_compute_checksum(rsdp, 20); |
| 202 | rsdp->ext_checksum = table_compute_checksum(rsdp, |
| 203 | sizeof(struct acpi_rsdp)); |
| 204 | } |
| 205 | |
| 206 | static void acpi_write_rsdt(struct acpi_rsdt *rsdt) |
| 207 | { |
| 208 | struct acpi_table_header *header = &rsdt->header; |
| 209 | |
| 210 | /* Fill out header fields */ |
| 211 | acpi_fill_header(header, "RSDT"); |
| 212 | header->length = sizeof(struct acpi_rsdt); |
| 213 | header->revision = 1; |
| 214 | |
| 215 | /* Entries are filled in later, we come with an empty set */ |
| 216 | |
| 217 | /* Fix checksum */ |
| 218 | header->checksum = table_compute_checksum(rsdt, |
| 219 | sizeof(struct acpi_rsdt)); |
| 220 | } |
| 221 | |
| 222 | static void acpi_write_xsdt(struct acpi_xsdt *xsdt) |
| 223 | { |
| 224 | struct acpi_table_header *header = &xsdt->header; |
| 225 | |
| 226 | /* Fill out header fields */ |
| 227 | acpi_fill_header(header, "XSDT"); |
| 228 | header->length = sizeof(struct acpi_xsdt); |
| 229 | header->revision = 1; |
| 230 | |
| 231 | /* Entries are filled in later, we come with an empty set */ |
| 232 | |
| 233 | /* Fix checksum */ |
| 234 | header->checksum = table_compute_checksum(xsdt, |
| 235 | sizeof(struct acpi_xsdt)); |
| 236 | } |
| 237 | |
| 238 | void acpi_setup_base_tables(struct acpi_ctx *ctx, void *start) |
| 239 | { |
Simon Glass | 9c442a6 | 2020-04-26 09:19:51 -0600 | [diff] [blame] | 240 | ctx->current = start; |
| 241 | |
| 242 | /* Align ACPI tables to 16 byte */ |
| 243 | acpi_align(ctx); |
Simon Glass | faf08c7 | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 244 | gd->arch.acpi_start = map_to_sysmem(ctx->current); |
Simon Glass | 9c442a6 | 2020-04-26 09:19:51 -0600 | [diff] [blame] | 245 | |
| 246 | /* We need at least an RSDP and an RSDT Table */ |
| 247 | ctx->rsdp = ctx->current; |
| 248 | acpi_inc_align(ctx, sizeof(struct acpi_rsdp)); |
| 249 | ctx->rsdt = ctx->current; |
| 250 | acpi_inc_align(ctx, sizeof(struct acpi_rsdt)); |
Simon Glass | abeaca8 | 2020-04-26 09:19:52 -0600 | [diff] [blame] | 251 | ctx->xsdt = ctx->current; |
Simon Glass | 9c442a6 | 2020-04-26 09:19:51 -0600 | [diff] [blame] | 252 | acpi_inc_align(ctx, sizeof(struct acpi_xsdt)); |
| 253 | |
| 254 | /* clear all table memory */ |
| 255 | memset((void *)start, '\0', ctx->current - start); |
| 256 | |
Simon Glass | abeaca8 | 2020-04-26 09:19:52 -0600 | [diff] [blame] | 257 | acpi_write_rsdp(ctx->rsdp, ctx->rsdt, ctx->xsdt); |
Simon Glass | 9c442a6 | 2020-04-26 09:19:51 -0600 | [diff] [blame] | 258 | acpi_write_rsdt(ctx->rsdt); |
Simon Glass | abeaca8 | 2020-04-26 09:19:52 -0600 | [diff] [blame] | 259 | acpi_write_xsdt(ctx->xsdt); |
Simon Glass | 9c442a6 | 2020-04-26 09:19:51 -0600 | [diff] [blame] | 260 | /* |
| 261 | * Per ACPI spec, the FACS table address must be aligned to a 64 byte |
| 262 | * boundary (Windows checks this, but Linux does not). |
| 263 | */ |
| 264 | acpi_align64(ctx); |
| 265 | } |