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>
14 #include <timestamp.h>
16 #include <acpi/acpi_table.h>
17 #include <asm/global_data.h>
20 int acpi_create_dmar(struct acpi_dmar *dmar, enum dmar_flags flags)
22 struct acpi_table_header *header = &dmar->header;
27 ret = uclass_first_device(UCLASS_CPU, &cpu);
29 return log_msg_ret("cpu", ret);
30 ret = cpu_get_info(cpu, &info);
32 return log_msg_ret("info", ret);
33 memset((void *)dmar, 0, sizeof(struct acpi_dmar));
35 /* Fill out header fields. */
36 acpi_fill_header(&dmar->header, "DMAR");
37 header->length = sizeof(struct acpi_dmar);
38 header->revision = acpi_get_table_revision(ACPITAB_DMAR);
40 dmar->host_address_width = info.address_width - 1;
46 int acpi_get_table_revision(enum acpi_tables table)
50 return ACPI_FADT_REV_ACPI_3_0;
52 return ACPI_MADT_REV_ACPI_3_0;
54 return ACPI_MCFG_REV_ACPI_3_0;
56 /* This version and the rest are open-coded */
60 case ACPITAB_SSDT: /* ACPI 3.0 upto 6.3: 2 */
62 case ACPITAB_SRAT: /* ACPI 2.0: 1, ACPI 3.0: 2, ACPI 4.0 to 6.3: 3 */
63 return 1; /* TODO Should probably be upgraded to 2 */
66 case ACPITAB_SLIT: /* ACPI 2.0 upto 6.3: 1 */
68 case ACPITAB_SPMI: /* IMPI 2.0 */
70 case ACPITAB_HPET: /* Currently 1. Table added in ACPI 2.0 */
72 case ACPITAB_VFCT: /* ACPI 2.0/3.0/4.0: 1 */
75 return IVRS_FORMAT_FIXED;
78 case ACPITAB_FACS: /* ACPI 2.0/3.0: 1, ACPI 4.0 to 6.3: 2 */
80 case ACPITAB_RSDT: /* ACPI 1.0 upto 6.3: 1 */
82 case ACPITAB_XSDT: /* ACPI 2.0 upto 6.3: 1 */
84 case ACPITAB_RSDP: /* ACPI 2.0 upto 6.3: 2 */
99 void acpi_fill_header(struct acpi_table_header *header, char *signature)
101 memcpy(header->signature, signature, 4);
102 memcpy(header->oem_id, OEM_ID, 6);
103 memcpy(header->oem_table_id, OEM_TABLE_ID, 8);
104 header->oem_revision = U_BOOT_BUILD_DATE;
105 memcpy(header->aslc_id, ASLC_ID, 4);
108 void acpi_align(struct acpi_ctx *ctx)
110 ctx->current = (void *)ALIGN((ulong)ctx->current, 16);
113 void acpi_align64(struct acpi_ctx *ctx)
115 ctx->current = (void *)ALIGN((ulong)ctx->current, 64);
118 void acpi_inc(struct acpi_ctx *ctx, uint amount)
120 ctx->current += amount;
123 void acpi_inc_align(struct acpi_ctx *ctx, uint amount)
125 ctx->current += amount;
130 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
133 int acpi_add_table(struct acpi_ctx *ctx, void *table)
136 struct acpi_rsdt *rsdt;
137 struct acpi_xsdt *xsdt;
139 /* The RSDT is mandatory while the XSDT is not */
142 /* This should always be MAX_ACPI_TABLES */
143 entries_num = ARRAY_SIZE(rsdt->entry);
145 for (i = 0; i < entries_num; i++) {
146 if (rsdt->entry[i] == 0)
150 if (i >= entries_num) {
151 log_err("ACPI: Error: too many tables\n");
155 /* Add table to the RSDT */
156 rsdt->entry[i] = map_to_sysmem(table);
158 /* Fix RSDT length or the kernel will assume invalid entries */
159 rsdt->header.length = sizeof(struct acpi_table_header) +
160 (sizeof(u32) * (i + 1));
162 /* Re-calculate checksum */
163 rsdt->header.checksum = 0;
164 rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
165 rsdt->header.length);
168 * And now the same thing for the XSDT. We use the same index as for
169 * now we want the XSDT and RSDT to always be in sync in U-Boot
173 /* Add table to the XSDT */
174 xsdt->entry[i] = map_to_sysmem(table);
176 /* Fix XSDT length */
177 xsdt->header.length = sizeof(struct acpi_table_header) +
178 (sizeof(u64) * (i + 1));
180 /* Re-calculate checksum */
181 xsdt->header.checksum = 0;
182 xsdt->header.checksum = table_compute_checksum((u8 *)xsdt,
183 xsdt->header.length);
188 void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt,
189 struct acpi_xsdt *xsdt)
191 memset(rsdp, 0, sizeof(struct acpi_rsdp));
193 memcpy(rsdp->signature, RSDP_SIG, 8);
194 memcpy(rsdp->oem_id, OEM_ID, 6);
196 rsdp->length = sizeof(struct acpi_rsdp);
197 rsdp->rsdt_address = map_to_sysmem(rsdt);
199 rsdp->xsdt_address = map_to_sysmem(xsdt);
200 rsdp->revision = ACPI_RSDP_REV_ACPI_2_0;
202 /* Calculate checksums */
203 rsdp->checksum = table_compute_checksum(rsdp, 20);
204 rsdp->ext_checksum = table_compute_checksum(rsdp,
205 sizeof(struct acpi_rsdp));
208 static void acpi_write_rsdt(struct acpi_rsdt *rsdt)
210 struct acpi_table_header *header = &rsdt->header;
212 /* Fill out header fields */
213 acpi_fill_header(header, "RSDT");
214 header->length = sizeof(struct acpi_rsdt);
215 header->revision = 1;
217 /* Entries are filled in later, we come with an empty set */
220 header->checksum = table_compute_checksum(rsdt,
221 sizeof(struct acpi_rsdt));
224 static void acpi_write_xsdt(struct acpi_xsdt *xsdt)
226 struct acpi_table_header *header = &xsdt->header;
228 /* Fill out header fields */
229 acpi_fill_header(header, "XSDT");
230 header->length = sizeof(struct acpi_xsdt);
231 header->revision = 1;
233 /* Entries are filled in later, we come with an empty set */
236 header->checksum = table_compute_checksum(xsdt,
237 sizeof(struct acpi_xsdt));
240 void acpi_setup_base_tables(struct acpi_ctx *ctx, void *start)
243 ctx->current = start;
245 /* Align ACPI tables to 16 byte */
247 gd->arch.acpi_start = map_to_sysmem(ctx->current);
249 /* We need at least an RSDP and an RSDT Table */
250 ctx->rsdp = ctx->current;
251 acpi_inc_align(ctx, sizeof(struct acpi_rsdp));
252 ctx->rsdt = ctx->current;
253 acpi_inc_align(ctx, sizeof(struct acpi_rsdt));
254 ctx->xsdt = ctx->current;
255 acpi_inc_align(ctx, sizeof(struct acpi_xsdt));
257 /* clear all table memory */
258 memset((void *)start, '\0', ctx->current - start);
260 acpi_write_rsdp(ctx->rsdp, ctx->rsdt, ctx->xsdt);
261 acpi_write_rsdt(ctx->rsdt);
262 acpi_write_xsdt(ctx->xsdt);
264 * Per ACPI spec, the FACS table address must be aligned to a 64 byte
265 * boundary (Windows checks this, but Linux does not).
270 void acpi_create_dbg2(struct acpi_dbg2_header *dbg2,
271 int port_type, int port_subtype,
272 struct acpi_gen_regaddr *address, u32 address_size,
273 const char *device_path)
276 struct acpi_dbg2_device *device;
278 struct acpi_table_header *header;
283 /* Fill out header fields. */
284 current = (uintptr_t)dbg2;
285 memset(dbg2, '\0', sizeof(struct acpi_dbg2_header));
286 header = &dbg2->header;
288 header->revision = acpi_get_table_revision(ACPITAB_DBG2);
289 acpi_fill_header(header, "DBG2");
290 header->aslc_revision = ASL_REVISION;
292 /* One debug device defined */
293 dbg2->devices_offset = sizeof(struct acpi_dbg2_header);
294 dbg2->devices_count = 1;
295 current += sizeof(struct acpi_dbg2_header);
297 /* Device comes after the header */
298 device = (struct acpi_dbg2_device *)current;
299 memset(device, 0, sizeof(struct acpi_dbg2_device));
300 current += sizeof(struct acpi_dbg2_device);
302 device->revision = 0;
303 device->address_count = 1;
304 device->port_type = port_type;
305 device->port_subtype = port_subtype;
307 /* Base Address comes after device structure */
308 memcpy((void *)current, address, sizeof(struct acpi_gen_regaddr));
309 device->base_address_offset = current - (uintptr_t)device;
310 current += sizeof(struct acpi_gen_regaddr);
312 /* Address Size comes after address structure */
313 dbg2_addr_size = (uint32_t *)current;
314 device->address_size_offset = current - (uintptr_t)device;
315 *dbg2_addr_size = address_size;
316 current += sizeof(uint32_t);
318 /* Namespace string comes last, use '.' if not provided */
319 path = device_path ? : ".";
320 /* Namespace string length includes NULL terminator */
321 path_len = strlen(path) + 1;
322 namespace = (char *)current;
323 device->namespace_string_length = path_len;
324 device->namespace_string_offset = current - (uintptr_t)device;
325 strncpy(namespace, path, path_len);
328 /* Update structure lengths and checksum */
329 device->length = current - (uintptr_t)device;
330 header->length = current - (uintptr_t)dbg2;
331 header->checksum = table_compute_checksum(dbg2, header->length);