blob: 2f07741784121cc638e52f10d329cc35d3802c32 [file] [log] [blame]
Simon Glass91fe8b72020-04-08 16:57:38 -06001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Generic code used to generate ACPI tables
4 *
5 * Copyright 2019 Google LLC
6 */
7
8#include <common.h>
Simon Glassbfeb5d42020-04-08 16:57:39 -06009#include <dm.h>
10#include <cpu.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060011#include <log.h>
Simon Glass29b35112020-04-26 09:19:50 -060012#include <mapmem.h>
13#include <tables_csum.h>
Simon Glass93f7f822020-04-26 09:19:46 -060014#include <version.h>
Simon Glass86e17782020-04-26 09:19:47 -060015#include <acpi/acpi_table.h>
Simon Glass401d1c42020-10-30 21:38:53 -060016#include <asm/global_data.h>
Simon Glass86e17782020-04-26 09:19:47 -060017#include <dm/acpi.h>
Simon Glassbfeb5d42020-04-08 16:57:39 -060018
Simon Glassbfeb5d42020-04-08 16:57:39 -060019int acpi_create_dmar(struct acpi_dmar *dmar, enum dmar_flags flags)
20{
21 struct acpi_table_header *header = &dmar->header;
22 struct cpu_info info;
23 struct udevice *cpu;
24 int ret;
25
26 ret = uclass_first_device(UCLASS_CPU, &cpu);
27 if (ret)
28 return log_msg_ret("cpu", ret);
29 ret = cpu_get_info(cpu, &info);
30 if (ret)
31 return log_msg_ret("info", ret);
32 memset((void *)dmar, 0, sizeof(struct acpi_dmar));
33
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);
38
39 dmar->host_address_width = info.address_width - 1;
40 dmar->flags = flags;
41
42 return 0;
43}
Simon Glass91fe8b72020-04-08 16:57:38 -060044
45int acpi_get_table_revision(enum acpi_tables table)
46{
47 switch (table) {
48 case ACPITAB_FADT:
49 return ACPI_FADT_REV_ACPI_3_0;
50 case ACPITAB_MADT:
51 return ACPI_MADT_REV_ACPI_3_0;
52 case ACPITAB_MCFG:
53 return ACPI_MCFG_REV_ACPI_3_0;
54 case ACPITAB_TCPA:
55 /* This version and the rest are open-coded */
56 return 2;
57 case ACPITAB_TPM2:
58 return 4;
59 case ACPITAB_SSDT: /* ACPI 3.0 upto 6.3: 2 */
60 return 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 */
63 case ACPITAB_DMAR:
64 return 1;
65 case ACPITAB_SLIT: /* ACPI 2.0 upto 6.3: 1 */
66 return 1;
67 case ACPITAB_SPMI: /* IMPI 2.0 */
68 return 5;
69 case ACPITAB_HPET: /* Currently 1. Table added in ACPI 2.0 */
70 return 1;
71 case ACPITAB_VFCT: /* ACPI 2.0/3.0/4.0: 1 */
72 return 1;
73 case ACPITAB_IVRS:
74 return IVRS_FORMAT_FIXED;
75 case ACPITAB_DBG2:
76 return 0;
77 case ACPITAB_FACS: /* ACPI 2.0/3.0: 1, ACPI 4.0 to 6.3: 2 */
78 return 1;
79 case ACPITAB_RSDT: /* ACPI 1.0 upto 6.3: 1 */
80 return 1;
81 case ACPITAB_XSDT: /* ACPI 2.0 upto 6.3: 1 */
82 return 1;
83 case ACPITAB_RSDP: /* ACPI 2.0 upto 6.3: 2 */
84 return 2;
85 case ACPITAB_HEST:
86 return 1;
87 case ACPITAB_NHLT:
88 return 5;
89 case ACPITAB_BERT:
90 return 1;
91 case ACPITAB_SPCR:
92 return 2;
93 default:
94 return -EINVAL;
95 }
96}
Simon Glass93f7f822020-04-26 09:19:46 -060097
98void acpi_fill_header(struct acpi_table_header *header, char *signature)
99{
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);
105}
Simon Glass86e17782020-04-26 09:19:47 -0600106
107void acpi_align(struct acpi_ctx *ctx)
108{
109 ctx->current = (void *)ALIGN((ulong)ctx->current, 16);
110}
111
112void acpi_align64(struct acpi_ctx *ctx)
113{
114 ctx->current = (void *)ALIGN((ulong)ctx->current, 64);
115}
116
117void acpi_inc(struct acpi_ctx *ctx, uint amount)
118{
119 ctx->current += amount;
120}
121
122void acpi_inc_align(struct acpi_ctx *ctx, uint amount)
123{
124 ctx->current += amount;
125 acpi_align(ctx);
126}
Simon Glass29b35112020-04-26 09:19:50 -0600127
128/**
129 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
130 * and checksum.
131 */
132int acpi_add_table(struct acpi_ctx *ctx, void *table)
133{
134 int i, entries_num;
135 struct acpi_rsdt *rsdt;
136 struct acpi_xsdt *xsdt;
137
138 /* The RSDT is mandatory while the XSDT is not */
139 rsdt = ctx->rsdt;
140
141 /* This should always be MAX_ACPI_TABLES */
142 entries_num = ARRAY_SIZE(rsdt->entry);
143
144 for (i = 0; i < entries_num; i++) {
145 if (rsdt->entry[i] == 0)
146 break;
147 }
148
149 if (i >= entries_num) {
150 log_err("ACPI: Error: too many tables\n");
151 return -E2BIG;
152 }
153
154 /* Add table to the RSDT */
155 rsdt->entry[i] = map_to_sysmem(table);
156
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));
160
161 /* Re-calculate checksum */
162 rsdt->header.checksum = 0;
163 rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
164 rsdt->header.length);
165
166 /*
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
169 */
Simon Glassb38309b2020-04-26 09:19:52 -0600170 xsdt = ctx->xsdt;
Simon Glass29b35112020-04-26 09:19:50 -0600171
172 /* Add table to the XSDT */
173 xsdt->entry[i] = map_to_sysmem(table);
174
175 /* Fix XSDT length */
176 xsdt->header.length = sizeof(struct acpi_table_header) +
177 (sizeof(u64) * (i + 1));
178
179 /* Re-calculate checksum */
180 xsdt->header.checksum = 0;
181 xsdt->header.checksum = table_compute_checksum((u8 *)xsdt,
182 xsdt->header.length);
183
184 return 0;
185}
Simon Glass7e586f62020-04-26 09:19:51 -0600186
Simon Glassd2cb7a22020-11-04 09:57:25 -0700187void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt,
188 struct acpi_xsdt *xsdt)
Simon Glass7e586f62020-04-26 09:19:51 -0600189{
190 memset(rsdp, 0, sizeof(struct acpi_rsdp));
191
192 memcpy(rsdp->signature, RSDP_SIG, 8);
193 memcpy(rsdp->oem_id, OEM_ID, 6);
194
195 rsdp->length = sizeof(struct acpi_rsdp);
196 rsdp->rsdt_address = map_to_sysmem(rsdt);
197
198 rsdp->xsdt_address = map_to_sysmem(xsdt);
199 rsdp->revision = ACPI_RSDP_REV_ACPI_2_0;
200
201 /* Calculate checksums */
202 rsdp->checksum = table_compute_checksum(rsdp, 20);
203 rsdp->ext_checksum = table_compute_checksum(rsdp,
204 sizeof(struct acpi_rsdp));
205}
206
207static void acpi_write_rsdt(struct acpi_rsdt *rsdt)
208{
209 struct acpi_table_header *header = &rsdt->header;
210
211 /* Fill out header fields */
212 acpi_fill_header(header, "RSDT");
213 header->length = sizeof(struct acpi_rsdt);
214 header->revision = 1;
215
216 /* Entries are filled in later, we come with an empty set */
217
218 /* Fix checksum */
219 header->checksum = table_compute_checksum(rsdt,
220 sizeof(struct acpi_rsdt));
221}
222
223static void acpi_write_xsdt(struct acpi_xsdt *xsdt)
224{
225 struct acpi_table_header *header = &xsdt->header;
226
227 /* Fill out header fields */
228 acpi_fill_header(header, "XSDT");
229 header->length = sizeof(struct acpi_xsdt);
230 header->revision = 1;
231
232 /* Entries are filled in later, we come with an empty set */
233
234 /* Fix checksum */
235 header->checksum = table_compute_checksum(xsdt,
236 sizeof(struct acpi_xsdt));
237}
238
239void acpi_setup_base_tables(struct acpi_ctx *ctx, void *start)
240{
Simon Glass61cc9332020-07-07 13:11:42 -0600241 ctx->base = start;
Simon Glass7e586f62020-04-26 09:19:51 -0600242 ctx->current = start;
243
244 /* Align ACPI tables to 16 byte */
245 acpi_align(ctx);
Simon Glass0b885bc2020-04-26 09:19:53 -0600246 gd->arch.acpi_start = map_to_sysmem(ctx->current);
Simon Glass7e586f62020-04-26 09:19:51 -0600247
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));
Simon Glassb38309b2020-04-26 09:19:52 -0600253 ctx->xsdt = ctx->current;
Simon Glass7e586f62020-04-26 09:19:51 -0600254 acpi_inc_align(ctx, sizeof(struct acpi_xsdt));
255
256 /* clear all table memory */
257 memset((void *)start, '\0', ctx->current - start);
258
Simon Glassb38309b2020-04-26 09:19:52 -0600259 acpi_write_rsdp(ctx->rsdp, ctx->rsdt, ctx->xsdt);
Simon Glass7e586f62020-04-26 09:19:51 -0600260 acpi_write_rsdt(ctx->rsdt);
Simon Glassb38309b2020-04-26 09:19:52 -0600261 acpi_write_xsdt(ctx->xsdt);
Simon Glass7e586f62020-04-26 09:19:51 -0600262 /*
263 * Per ACPI spec, the FACS table address must be aligned to a 64 byte
264 * boundary (Windows checks this, but Linux does not).
265 */
266 acpi_align64(ctx);
267}
Simon Glassf37979e2020-09-22 12:45:10 -0600268
269void 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)
273{
274 uintptr_t current;
275 struct acpi_dbg2_device *device;
276 u32 *dbg2_addr_size;
277 struct acpi_table_header *header;
278 size_t path_len;
279 const char *path;
280 char *namespace;
281
282 /* Fill out header fields. */
283 current = (uintptr_t)dbg2;
284 memset(dbg2, '\0', sizeof(struct acpi_dbg2_header));
285 header = &dbg2->header;
286
287 header->revision = acpi_get_table_revision(ACPITAB_DBG2);
288 acpi_fill_header(header, "DBG2");
289 header->aslc_revision = ASL_REVISION;
290
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);
295
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);
300
301 device->revision = 0;
302 device->address_count = 1;
303 device->port_type = port_type;
304 device->port_subtype = port_subtype;
305
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);
310
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);
316
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);
325 current += path_len;
326
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);
331}