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