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>
16 #include <asm/global_data.h>
19 int acpi_create_dmar(struct acpi_dmar *dmar, enum dmar_flags flags)
21 struct acpi_table_header *header = &dmar->header;
26 ret = uclass_first_device(UCLASS_CPU, &cpu);
28 return log_msg_ret("cpu", ret);
29 ret = cpu_get_info(cpu, &info);
31 return log_msg_ret("info", ret);
32 memset((void *)dmar, 0, sizeof(struct acpi_dmar));
34 /* Fill out header fields. */
35 acpi_fill_header(&dmar->header, "DMAR");
36 header->length = sizeof(struct acpi_dmar);
37 header->revision = acpi_get_table_revision(ACPITAB_DMAR);
39 dmar->host_address_width = info.address_width - 1;
45 int acpi_get_table_revision(enum acpi_tables table)
49 return ACPI_FADT_REV_ACPI_3_0;
51 return ACPI_MADT_REV_ACPI_3_0;
53 return ACPI_MCFG_REV_ACPI_3_0;
55 /* This version and the rest are open-coded */
59 case ACPITAB_SSDT: /* ACPI 3.0 upto 6.3: 2 */
61 case ACPITAB_SRAT: /* ACPI 2.0: 1, ACPI 3.0: 2, ACPI 4.0 to 6.3: 3 */
62 return 1; /* TODO Should probably be upgraded to 2 */
65 case ACPITAB_SLIT: /* ACPI 2.0 upto 6.3: 1 */
67 case ACPITAB_SPMI: /* IMPI 2.0 */
69 case ACPITAB_HPET: /* Currently 1. Table added in ACPI 2.0 */
71 case ACPITAB_VFCT: /* ACPI 2.0/3.0/4.0: 1 */
74 return IVRS_FORMAT_FIXED;
77 case ACPITAB_FACS: /* ACPI 2.0/3.0: 1, ACPI 4.0 to 6.3: 2 */
79 case ACPITAB_RSDT: /* ACPI 1.0 upto 6.3: 1 */
81 case ACPITAB_XSDT: /* ACPI 2.0 upto 6.3: 1 */
83 case ACPITAB_RSDP: /* ACPI 2.0 upto 6.3: 2 */
98 void acpi_fill_header(struct acpi_table_header *header, char *signature)
100 memcpy(header->signature, signature, 4);
101 memcpy(header->oem_id, OEM_ID, 6);
102 memcpy(header->oem_table_id, OEM_TABLE_ID, 8);
103 header->oem_revision = U_BOOT_BUILD_DATE;
104 memcpy(header->aslc_id, ASLC_ID, 4);
107 void acpi_align(struct acpi_ctx *ctx)
109 ctx->current = (void *)ALIGN((ulong)ctx->current, 16);
112 void acpi_align64(struct acpi_ctx *ctx)
114 ctx->current = (void *)ALIGN((ulong)ctx->current, 64);
117 void acpi_inc(struct acpi_ctx *ctx, uint amount)
119 ctx->current += amount;
122 void acpi_inc_align(struct acpi_ctx *ctx, uint amount)
124 ctx->current += amount;
129 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
132 int acpi_add_table(struct acpi_ctx *ctx, void *table)
135 struct acpi_rsdt *rsdt;
136 struct acpi_xsdt *xsdt;
138 /* The RSDT is mandatory while the XSDT is not */
141 /* This should always be MAX_ACPI_TABLES */
142 entries_num = ARRAY_SIZE(rsdt->entry);
144 for (i = 0; i < entries_num; i++) {
145 if (rsdt->entry[i] == 0)
149 if (i >= entries_num) {
150 log_err("ACPI: Error: too many tables\n");
154 /* Add table to the RSDT */
155 rsdt->entry[i] = map_to_sysmem(table);
157 /* Fix RSDT length or the kernel will assume invalid entries */
158 rsdt->header.length = sizeof(struct acpi_table_header) +
159 (sizeof(u32) * (i + 1));
161 /* Re-calculate checksum */
162 rsdt->header.checksum = 0;
163 rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
164 rsdt->header.length);
167 * And now the same thing for the XSDT. We use the same index as for
168 * now we want the XSDT and RSDT to always be in sync in U-Boot
172 /* Add table to the XSDT */
173 xsdt->entry[i] = map_to_sysmem(table);
175 /* Fix XSDT length */
176 xsdt->header.length = sizeof(struct acpi_table_header) +
177 (sizeof(u64) * (i + 1));
179 /* Re-calculate checksum */
180 xsdt->header.checksum = 0;
181 xsdt->header.checksum = table_compute_checksum((u8 *)xsdt,
182 xsdt->header.length);
187 void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt,
188 struct acpi_xsdt *xsdt)
190 memset(rsdp, 0, sizeof(struct acpi_rsdp));
192 memcpy(rsdp->signature, RSDP_SIG, 8);
193 memcpy(rsdp->oem_id, OEM_ID, 6);
195 rsdp->length = sizeof(struct acpi_rsdp);
196 rsdp->rsdt_address = map_to_sysmem(rsdt);
198 rsdp->xsdt_address = map_to_sysmem(xsdt);
199 rsdp->revision = ACPI_RSDP_REV_ACPI_2_0;
201 /* Calculate checksums */
202 rsdp->checksum = table_compute_checksum(rsdp, 20);
203 rsdp->ext_checksum = table_compute_checksum(rsdp,
204 sizeof(struct acpi_rsdp));
207 static void acpi_write_rsdt(struct acpi_rsdt *rsdt)
209 struct acpi_table_header *header = &rsdt->header;
211 /* Fill out header fields */
212 acpi_fill_header(header, "RSDT");
213 header->length = sizeof(struct acpi_rsdt);
214 header->revision = 1;
216 /* Entries are filled in later, we come with an empty set */
219 header->checksum = table_compute_checksum(rsdt,
220 sizeof(struct acpi_rsdt));
223 static void acpi_write_xsdt(struct acpi_xsdt *xsdt)
225 struct acpi_table_header *header = &xsdt->header;
227 /* Fill out header fields */
228 acpi_fill_header(header, "XSDT");
229 header->length = sizeof(struct acpi_xsdt);
230 header->revision = 1;
232 /* Entries are filled in later, we come with an empty set */
235 header->checksum = table_compute_checksum(xsdt,
236 sizeof(struct acpi_xsdt));
239 void acpi_setup_base_tables(struct acpi_ctx *ctx, void *start)
242 ctx->current = start;
244 /* Align ACPI tables to 16 byte */
246 gd->arch.acpi_start = map_to_sysmem(ctx->current);
248 /* We need at least an RSDP and an RSDT Table */
249 ctx->rsdp = ctx->current;
250 acpi_inc_align(ctx, sizeof(struct acpi_rsdp));
251 ctx->rsdt = ctx->current;
252 acpi_inc_align(ctx, sizeof(struct acpi_rsdt));
253 ctx->xsdt = ctx->current;
254 acpi_inc_align(ctx, sizeof(struct acpi_xsdt));
256 /* clear all table memory */
257 memset((void *)start, '\0', ctx->current - start);
259 acpi_write_rsdp(ctx->rsdp, ctx->rsdt, ctx->xsdt);
260 acpi_write_rsdt(ctx->rsdt);
261 acpi_write_xsdt(ctx->xsdt);
263 * Per ACPI spec, the FACS table address must be aligned to a 64 byte
264 * boundary (Windows checks this, but Linux does not).
269 void acpi_create_dbg2(struct acpi_dbg2_header *dbg2,
270 int port_type, int port_subtype,
271 struct acpi_gen_regaddr *address, u32 address_size,
272 const char *device_path)
275 struct acpi_dbg2_device *device;
277 struct acpi_table_header *header;
282 /* Fill out header fields. */
283 current = (uintptr_t)dbg2;
284 memset(dbg2, '\0', sizeof(struct acpi_dbg2_header));
285 header = &dbg2->header;
287 header->revision = acpi_get_table_revision(ACPITAB_DBG2);
288 acpi_fill_header(header, "DBG2");
289 header->aslc_revision = ASL_REVISION;
291 /* One debug device defined */
292 dbg2->devices_offset = sizeof(struct acpi_dbg2_header);
293 dbg2->devices_count = 1;
294 current += sizeof(struct acpi_dbg2_header);
296 /* Device comes after the header */
297 device = (struct acpi_dbg2_device *)current;
298 memset(device, 0, sizeof(struct acpi_dbg2_device));
299 current += sizeof(struct acpi_dbg2_device);
301 device->revision = 0;
302 device->address_count = 1;
303 device->port_type = port_type;
304 device->port_subtype = port_subtype;
306 /* Base Address comes after device structure */
307 memcpy((void *)current, address, sizeof(struct acpi_gen_regaddr));
308 device->base_address_offset = current - (uintptr_t)device;
309 current += sizeof(struct acpi_gen_regaddr);
311 /* Address Size comes after address structure */
312 dbg2_addr_size = (uint32_t *)current;
313 device->address_size_offset = current - (uintptr_t)device;
314 *dbg2_addr_size = address_size;
315 current += sizeof(uint32_t);
317 /* Namespace string comes last, use '.' if not provided */
318 path = device_path ? : ".";
319 /* Namespace string length includes NULL terminator */
320 path_len = strlen(path) + 1;
321 namespace = (char *)current;
322 device->namespace_string_length = path_len;
323 device->namespace_string_offset = current - (uintptr_t)device;
324 strncpy(namespace, path, path_len);
327 /* Update structure lengths and checksum */
328 device->length = current - (uintptr_t)device;
329 header->length = current - (uintptr_t)dbg2;
330 header->checksum = table_compute_checksum(dbg2, header->length);