1 // SPDX-License-Identifier: GPL-2.0+
3 * Generic code used to generate ACPI tables
5 * Copyright 2019 Google LLC
12 #include <tables_csum.h>
14 #include <acpi/acpi_table.h>
17 int acpi_create_dmar(struct acpi_dmar *dmar, enum dmar_flags flags)
19 struct acpi_table_header *header = &dmar->header;
24 ret = uclass_first_device(UCLASS_CPU, &cpu);
26 return log_msg_ret("cpu", ret);
27 ret = cpu_get_info(cpu, &info);
29 return log_msg_ret("info", ret);
30 memset((void *)dmar, 0, sizeof(struct acpi_dmar));
32 /* Fill out header fields. */
33 acpi_fill_header(&dmar->header, "DMAR");
34 header->length = sizeof(struct acpi_dmar);
35 header->revision = acpi_get_table_revision(ACPITAB_DMAR);
37 dmar->host_address_width = info.address_width - 1;
43 int acpi_get_table_revision(enum acpi_tables table)
47 return ACPI_FADT_REV_ACPI_3_0;
49 return ACPI_MADT_REV_ACPI_3_0;
51 return ACPI_MCFG_REV_ACPI_3_0;
53 /* This version and the rest are open-coded */
57 case ACPITAB_SSDT: /* ACPI 3.0 upto 6.3: 2 */
59 case ACPITAB_SRAT: /* ACPI 2.0: 1, ACPI 3.0: 2, ACPI 4.0 to 6.3: 3 */
60 return 1; /* TODO Should probably be upgraded to 2 */
63 case ACPITAB_SLIT: /* ACPI 2.0 upto 6.3: 1 */
65 case ACPITAB_SPMI: /* IMPI 2.0 */
67 case ACPITAB_HPET: /* Currently 1. Table added in ACPI 2.0 */
69 case ACPITAB_VFCT: /* ACPI 2.0/3.0/4.0: 1 */
72 return IVRS_FORMAT_FIXED;
75 case ACPITAB_FACS: /* ACPI 2.0/3.0: 1, ACPI 4.0 to 6.3: 2 */
77 case ACPITAB_RSDT: /* ACPI 1.0 upto 6.3: 1 */
79 case ACPITAB_XSDT: /* ACPI 2.0 upto 6.3: 1 */
81 case ACPITAB_RSDP: /* ACPI 2.0 upto 6.3: 2 */
96 void acpi_fill_header(struct acpi_table_header *header, char *signature)
98 memcpy(header->signature, signature, 4);
99 memcpy(header->oem_id, OEM_ID, 6);
100 memcpy(header->oem_table_id, OEM_TABLE_ID, 8);
101 header->oem_revision = U_BOOT_BUILD_DATE;
102 memcpy(header->aslc_id, ASLC_ID, 4);
105 void acpi_align(struct acpi_ctx *ctx)
107 ctx->current = (void *)ALIGN((ulong)ctx->current, 16);
110 void acpi_align64(struct acpi_ctx *ctx)
112 ctx->current = (void *)ALIGN((ulong)ctx->current, 64);
115 void acpi_inc(struct acpi_ctx *ctx, uint amount)
117 ctx->current += amount;
120 void acpi_inc_align(struct acpi_ctx *ctx, uint amount)
122 ctx->current += amount;
127 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
130 int acpi_add_table(struct acpi_ctx *ctx, void *table)
133 struct acpi_rsdt *rsdt;
134 struct acpi_xsdt *xsdt;
136 /* The RSDT is mandatory while the XSDT is not */
139 /* This should always be MAX_ACPI_TABLES */
140 entries_num = ARRAY_SIZE(rsdt->entry);
142 for (i = 0; i < entries_num; i++) {
143 if (rsdt->entry[i] == 0)
147 if (i >= entries_num) {
148 log_err("ACPI: Error: too many tables\n");
152 /* Add table to the RSDT */
153 rsdt->entry[i] = map_to_sysmem(table);
155 /* Fix RSDT length or the kernel will assume invalid entries */
156 rsdt->header.length = sizeof(struct acpi_table_header) +
157 (sizeof(u32) * (i + 1));
159 /* Re-calculate checksum */
160 rsdt->header.checksum = 0;
161 rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
162 rsdt->header.length);
165 * And now the same thing for the XSDT. We use the same index as for
166 * now we want the XSDT and RSDT to always be in sync in U-Boot
168 xsdt = map_sysmem(ctx->rsdp->xsdt_address, sizeof(*xsdt));
170 /* Add table to the XSDT */
171 xsdt->entry[i] = map_to_sysmem(table);
173 /* Fix XSDT length */
174 xsdt->header.length = sizeof(struct acpi_table_header) +
175 (sizeof(u64) * (i + 1));
177 /* Re-calculate checksum */
178 xsdt->header.checksum = 0;
179 xsdt->header.checksum = table_compute_checksum((u8 *)xsdt,
180 xsdt->header.length);
185 static void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt,
186 struct acpi_xsdt *xsdt)
188 memset(rsdp, 0, sizeof(struct acpi_rsdp));
190 memcpy(rsdp->signature, RSDP_SIG, 8);
191 memcpy(rsdp->oem_id, OEM_ID, 6);
193 rsdp->length = sizeof(struct acpi_rsdp);
194 rsdp->rsdt_address = map_to_sysmem(rsdt);
196 rsdp->xsdt_address = map_to_sysmem(xsdt);
197 rsdp->revision = ACPI_RSDP_REV_ACPI_2_0;
199 /* Calculate checksums */
200 rsdp->checksum = table_compute_checksum(rsdp, 20);
201 rsdp->ext_checksum = table_compute_checksum(rsdp,
202 sizeof(struct acpi_rsdp));
205 static void acpi_write_rsdt(struct acpi_rsdt *rsdt)
207 struct acpi_table_header *header = &rsdt->header;
209 /* Fill out header fields */
210 acpi_fill_header(header, "RSDT");
211 header->length = sizeof(struct acpi_rsdt);
212 header->revision = 1;
214 /* Entries are filled in later, we come with an empty set */
217 header->checksum = table_compute_checksum(rsdt,
218 sizeof(struct acpi_rsdt));
221 static void acpi_write_xsdt(struct acpi_xsdt *xsdt)
223 struct acpi_table_header *header = &xsdt->header;
225 /* Fill out header fields */
226 acpi_fill_header(header, "XSDT");
227 header->length = sizeof(struct acpi_xsdt);
228 header->revision = 1;
230 /* Entries are filled in later, we come with an empty set */
233 header->checksum = table_compute_checksum(xsdt,
234 sizeof(struct acpi_xsdt));
237 void acpi_setup_base_tables(struct acpi_ctx *ctx, void *start)
239 struct acpi_xsdt *xsdt;
241 ctx->current = start;
243 /* Align ACPI tables to 16 byte */
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));
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, xsdt);
258 acpi_write_rsdt(ctx->rsdt);
259 acpi_write_xsdt(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).