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)
241 ctx->current = start;
243 /* Align ACPI tables to 16 byte */
245 gd->arch.acpi_start = map_to_sysmem(ctx->current);
247 /* We need at least an RSDP and an RSDT Table */
248 ctx->rsdp = ctx->current;
249 acpi_inc_align(ctx, sizeof(struct acpi_rsdp));
250 ctx->rsdt = ctx->current;
251 acpi_inc_align(ctx, sizeof(struct acpi_rsdt));
252 ctx->xsdt = ctx->current;
253 acpi_inc_align(ctx, sizeof(struct acpi_xsdt));
255 /* clear all table memory */
256 memset((void *)start, '\0', ctx->current - start);
258 acpi_write_rsdp(ctx->rsdp, ctx->rsdt, ctx->xsdt);
259 acpi_write_rsdt(ctx->rsdt);
260 acpi_write_xsdt(ctx->xsdt);
262 * Per ACPI spec, the FACS table address must be aligned to a 64 byte
263 * boundary (Windows checks this, but Linux does not).
268 void acpi_create_dbg2(struct acpi_dbg2_header *dbg2,
269 int port_type, int port_subtype,
270 struct acpi_gen_regaddr *address, u32 address_size,
271 const char *device_path)
274 struct acpi_dbg2_device *device;
276 struct acpi_table_header *header;
281 /* Fill out header fields. */
282 current = (uintptr_t)dbg2;
283 memset(dbg2, '\0', sizeof(struct acpi_dbg2_header));
284 header = &dbg2->header;
286 header->revision = acpi_get_table_revision(ACPITAB_DBG2);
287 acpi_fill_header(header, "DBG2");
288 header->aslc_revision = ASL_REVISION;
290 /* One debug device defined */
291 dbg2->devices_offset = sizeof(struct acpi_dbg2_header);
292 dbg2->devices_count = 1;
293 current += sizeof(struct acpi_dbg2_header);
295 /* Device comes after the header */
296 device = (struct acpi_dbg2_device *)current;
297 memset(device, 0, sizeof(struct acpi_dbg2_device));
298 current += sizeof(struct acpi_dbg2_device);
300 device->revision = 0;
301 device->address_count = 1;
302 device->port_type = port_type;
303 device->port_subtype = port_subtype;
305 /* Base Address comes after device structure */
306 memcpy((void *)current, address, sizeof(struct acpi_gen_regaddr));
307 device->base_address_offset = current - (uintptr_t)device;
308 current += sizeof(struct acpi_gen_regaddr);
310 /* Address Size comes after address structure */
311 dbg2_addr_size = (uint32_t *)current;
312 device->address_size_offset = current - (uintptr_t)device;
313 *dbg2_addr_size = address_size;
314 current += sizeof(uint32_t);
316 /* Namespace string comes last, use '.' if not provided */
317 path = device_path ? : ".";
318 /* Namespace string length includes NULL terminator */
319 path_len = strlen(path) + 1;
320 namespace = (char *)current;
321 device->namespace_string_length = path_len;
322 device->namespace_string_offset = current - (uintptr_t)device;
323 strncpy(namespace, path, path_len);
326 /* Update structure lengths and checksum */
327 device->length = current - (uintptr_t)device;
328 header->length = current - (uintptr_t)dbg2;
329 header->checksum = table_compute_checksum(dbg2, header->length);