1 // SPDX-License-Identifier: GPL-2.0+
3 * Generic code used to generate ACPI tables
5 * Copyright 2019 Google LLC
13 #include <tables_csum.h>
15 #include <acpi/acpi_table.h>
18 int acpi_create_dmar(struct acpi_dmar *dmar, enum dmar_flags flags)
20 struct acpi_table_header *header = &dmar->header;
25 ret = uclass_first_device(UCLASS_CPU, &cpu);
27 return log_msg_ret("cpu", ret);
28 ret = cpu_get_info(cpu, &info);
30 return log_msg_ret("info", ret);
31 memset((void *)dmar, 0, sizeof(struct acpi_dmar));
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);
38 dmar->host_address_width = info.address_width - 1;
44 int acpi_get_table_revision(enum acpi_tables table)
48 return ACPI_FADT_REV_ACPI_3_0;
50 return ACPI_MADT_REV_ACPI_3_0;
52 return ACPI_MCFG_REV_ACPI_3_0;
54 /* This version and the rest are open-coded */
58 case ACPITAB_SSDT: /* ACPI 3.0 upto 6.3: 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 */
64 case ACPITAB_SLIT: /* ACPI 2.0 upto 6.3: 1 */
66 case ACPITAB_SPMI: /* IMPI 2.0 */
68 case ACPITAB_HPET: /* Currently 1. Table added in ACPI 2.0 */
70 case ACPITAB_VFCT: /* ACPI 2.0/3.0/4.0: 1 */
73 return IVRS_FORMAT_FIXED;
76 case ACPITAB_FACS: /* ACPI 2.0/3.0: 1, ACPI 4.0 to 6.3: 2 */
78 case ACPITAB_RSDT: /* ACPI 1.0 upto 6.3: 1 */
80 case ACPITAB_XSDT: /* ACPI 2.0 upto 6.3: 1 */
82 case ACPITAB_RSDP: /* ACPI 2.0 upto 6.3: 2 */
97 void acpi_fill_header(struct acpi_table_header *header, char *signature)
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);
106 void acpi_align(struct acpi_ctx *ctx)
108 ctx->current = (void *)ALIGN((ulong)ctx->current, 16);
111 void acpi_align64(struct acpi_ctx *ctx)
113 ctx->current = (void *)ALIGN((ulong)ctx->current, 64);
116 void acpi_inc(struct acpi_ctx *ctx, uint amount)
118 ctx->current += amount;
121 void acpi_inc_align(struct acpi_ctx *ctx, uint amount)
123 ctx->current += amount;
128 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
131 int acpi_add_table(struct acpi_ctx *ctx, void *table)
134 struct acpi_rsdt *rsdt;
135 struct acpi_xsdt *xsdt;
137 /* The RSDT is mandatory while the XSDT is not */
140 /* This should always be MAX_ACPI_TABLES */
141 entries_num = ARRAY_SIZE(rsdt->entry);
143 for (i = 0; i < entries_num; i++) {
144 if (rsdt->entry[i] == 0)
148 if (i >= entries_num) {
149 log_err("ACPI: Error: too many tables\n");
153 /* Add table to the RSDT */
154 rsdt->entry[i] = map_to_sysmem(table);
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));
160 /* Re-calculate checksum */
161 rsdt->header.checksum = 0;
162 rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
163 rsdt->header.length);
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
171 /* Add table to the XSDT */
172 xsdt->entry[i] = map_to_sysmem(table);
174 /* Fix XSDT length */
175 xsdt->header.length = sizeof(struct acpi_table_header) +
176 (sizeof(u64) * (i + 1));
178 /* Re-calculate checksum */
179 xsdt->header.checksum = 0;
180 xsdt->header.checksum = table_compute_checksum((u8 *)xsdt,
181 xsdt->header.length);
186 static void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt,
187 struct acpi_xsdt *xsdt)
189 memset(rsdp, 0, sizeof(struct acpi_rsdp));
191 memcpy(rsdp->signature, RSDP_SIG, 8);
192 memcpy(rsdp->oem_id, OEM_ID, 6);
194 rsdp->length = sizeof(struct acpi_rsdp);
195 rsdp->rsdt_address = map_to_sysmem(rsdt);
197 rsdp->xsdt_address = map_to_sysmem(xsdt);
198 rsdp->revision = ACPI_RSDP_REV_ACPI_2_0;
200 /* Calculate checksums */
201 rsdp->checksum = table_compute_checksum(rsdp, 20);
202 rsdp->ext_checksum = table_compute_checksum(rsdp,
203 sizeof(struct acpi_rsdp));
206 static void acpi_write_rsdt(struct acpi_rsdt *rsdt)
208 struct acpi_table_header *header = &rsdt->header;
210 /* Fill out header fields */
211 acpi_fill_header(header, "RSDT");
212 header->length = sizeof(struct acpi_rsdt);
213 header->revision = 1;
215 /* Entries are filled in later, we come with an empty set */
218 header->checksum = table_compute_checksum(rsdt,
219 sizeof(struct acpi_rsdt));
222 static void acpi_write_xsdt(struct acpi_xsdt *xsdt)
224 struct acpi_table_header *header = &xsdt->header;
226 /* Fill out header fields */
227 acpi_fill_header(header, "XSDT");
228 header->length = sizeof(struct acpi_xsdt);
229 header->revision = 1;
231 /* Entries are filled in later, we come with an empty set */
234 header->checksum = table_compute_checksum(xsdt,
235 sizeof(struct acpi_xsdt));
238 void acpi_setup_base_tables(struct acpi_ctx *ctx, void *start)
240 ctx->current = start;
242 /* Align ACPI tables to 16 byte */
244 gd->arch.acpi_start = map_to_sysmem(ctx->current);
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));
251 ctx->xsdt = ctx->current;
252 acpi_inc_align(ctx, sizeof(struct acpi_xsdt));
254 /* clear all table memory */
255 memset((void *)start, '\0', ctx->current - start);
257 acpi_write_rsdp(ctx->rsdp, ctx->rsdt, ctx->xsdt);
258 acpi_write_rsdt(ctx->rsdt);
259 acpi_write_xsdt(ctx->xsdt);
261 * Per ACPI spec, the FACS table address must be aligned to a 64 byte
262 * boundary (Windows checks this, but Linux does not).