blob: 7ea4b2e87eea0b0c7fd84f7d9fe2ecde5b886517 [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>
Pali Rohár6e1f0852021-08-02 15:18:38 +020014#include <timestamp.h>
Simon Glass17968c32020-04-26 09:19:46 -060015#include <version.h>
Simon Glass0e113842020-04-26 09:19:47 -060016#include <acpi/acpi_table.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060017#include <asm/global_data.h>
Simon Glass0e113842020-04-26 09:19:47 -060018#include <dm/acpi.h>
Simon Glasse9629892020-04-08 16:57:39 -060019
Simon Glasse9629892020-04-08 16:57:39 -060020int acpi_create_dmar(struct acpi_dmar *dmar, enum dmar_flags flags)
21{
22 struct acpi_table_header *header = &dmar->header;
23 struct cpu_info info;
24 struct udevice *cpu;
25 int ret;
26
27 ret = uclass_first_device(UCLASS_CPU, &cpu);
28 if (ret)
29 return log_msg_ret("cpu", ret);
30 ret = cpu_get_info(cpu, &info);
31 if (ret)
32 return log_msg_ret("info", ret);
33 memset((void *)dmar, 0, sizeof(struct acpi_dmar));
34
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);
39
40 dmar->host_address_width = info.address_width - 1;
41 dmar->flags = flags;
42
43 return 0;
44}
Simon Glassb2672ea2020-04-08 16:57:38 -060045
46int acpi_get_table_revision(enum acpi_tables table)
47{
48 switch (table) {
49 case ACPITAB_FADT:
50 return ACPI_FADT_REV_ACPI_3_0;
51 case ACPITAB_MADT:
52 return ACPI_MADT_REV_ACPI_3_0;
53 case ACPITAB_MCFG:
54 return ACPI_MCFG_REV_ACPI_3_0;
55 case ACPITAB_TCPA:
56 /* This version and the rest are open-coded */
57 return 2;
58 case ACPITAB_TPM2:
59 return 4;
60 case ACPITAB_SSDT: /* ACPI 3.0 upto 6.3: 2 */
61 return 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 */
64 case ACPITAB_DMAR:
65 return 1;
66 case ACPITAB_SLIT: /* ACPI 2.0 upto 6.3: 1 */
67 return 1;
68 case ACPITAB_SPMI: /* IMPI 2.0 */
69 return 5;
70 case ACPITAB_HPET: /* Currently 1. Table added in ACPI 2.0 */
71 return 1;
72 case ACPITAB_VFCT: /* ACPI 2.0/3.0/4.0: 1 */
73 return 1;
74 case ACPITAB_IVRS:
75 return IVRS_FORMAT_FIXED;
76 case ACPITAB_DBG2:
77 return 0;
78 case ACPITAB_FACS: /* ACPI 2.0/3.0: 1, ACPI 4.0 to 6.3: 2 */
79 return 1;
80 case ACPITAB_RSDT: /* ACPI 1.0 upto 6.3: 1 */
81 return 1;
82 case ACPITAB_XSDT: /* ACPI 2.0 upto 6.3: 1 */
83 return 1;
84 case ACPITAB_RSDP: /* ACPI 2.0 upto 6.3: 2 */
85 return 2;
86 case ACPITAB_HEST:
87 return 1;
88 case ACPITAB_NHLT:
89 return 5;
90 case ACPITAB_BERT:
91 return 1;
92 case ACPITAB_SPCR:
93 return 2;
94 default:
95 return -EINVAL;
96 }
97}
Simon Glass17968c32020-04-26 09:19:46 -060098
99void acpi_fill_header(struct acpi_table_header *header, char *signature)
100{
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);
106}
Simon Glass0e113842020-04-26 09:19:47 -0600107
108void acpi_align(struct acpi_ctx *ctx)
109{
110 ctx->current = (void *)ALIGN((ulong)ctx->current, 16);
111}
112
113void acpi_align64(struct acpi_ctx *ctx)
114{
115 ctx->current = (void *)ALIGN((ulong)ctx->current, 64);
116}
117
118void acpi_inc(struct acpi_ctx *ctx, uint amount)
119{
120 ctx->current += amount;
121}
122
123void acpi_inc_align(struct acpi_ctx *ctx, uint amount)
124{
125 ctx->current += amount;
126 acpi_align(ctx);
127}
Simon Glass575a5472020-04-26 09:19:50 -0600128
129/**
130 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
131 * and checksum.
132 */
133int acpi_add_table(struct acpi_ctx *ctx, void *table)
134{
135 int i, entries_num;
136 struct acpi_rsdt *rsdt;
137 struct acpi_xsdt *xsdt;
138
139 /* The RSDT is mandatory while the XSDT is not */
140 rsdt = ctx->rsdt;
141
142 /* This should always be MAX_ACPI_TABLES */
143 entries_num = ARRAY_SIZE(rsdt->entry);
144
145 for (i = 0; i < entries_num; i++) {
146 if (rsdt->entry[i] == 0)
147 break;
148 }
149
150 if (i >= entries_num) {
151 log_err("ACPI: Error: too many tables\n");
152 return -E2BIG;
153 }
154
155 /* Add table to the RSDT */
156 rsdt->entry[i] = map_to_sysmem(table);
157
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));
161
162 /* Re-calculate checksum */
163 rsdt->header.checksum = 0;
164 rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
165 rsdt->header.length);
166
167 /*
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
170 */
Simon Glassabeaca82020-04-26 09:19:52 -0600171 xsdt = ctx->xsdt;
Simon Glass575a5472020-04-26 09:19:50 -0600172
173 /* Add table to the XSDT */
174 xsdt->entry[i] = map_to_sysmem(table);
175
176 /* Fix XSDT length */
177 xsdt->header.length = sizeof(struct acpi_table_header) +
178 (sizeof(u64) * (i + 1));
179
180 /* Re-calculate checksum */
181 xsdt->header.checksum = 0;
182 xsdt->header.checksum = table_compute_checksum((u8 *)xsdt,
183 xsdt->header.length);
184
185 return 0;
186}
Simon Glass9c442a62020-04-26 09:19:51 -0600187
Simon Glass5d093f32020-11-04 09:57:25 -0700188void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt,
189 struct acpi_xsdt *xsdt)
Simon Glass9c442a62020-04-26 09:19:51 -0600190{
191 memset(rsdp, 0, sizeof(struct acpi_rsdp));
192
193 memcpy(rsdp->signature, RSDP_SIG, 8);
194 memcpy(rsdp->oem_id, OEM_ID, 6);
195
196 rsdp->length = sizeof(struct acpi_rsdp);
197 rsdp->rsdt_address = map_to_sysmem(rsdt);
198
199 rsdp->xsdt_address = map_to_sysmem(xsdt);
200 rsdp->revision = ACPI_RSDP_REV_ACPI_2_0;
201
202 /* Calculate checksums */
203 rsdp->checksum = table_compute_checksum(rsdp, 20);
204 rsdp->ext_checksum = table_compute_checksum(rsdp,
205 sizeof(struct acpi_rsdp));
206}
207
208static void acpi_write_rsdt(struct acpi_rsdt *rsdt)
209{
210 struct acpi_table_header *header = &rsdt->header;
211
212 /* Fill out header fields */
213 acpi_fill_header(header, "RSDT");
214 header->length = sizeof(struct acpi_rsdt);
215 header->revision = 1;
216
217 /* Entries are filled in later, we come with an empty set */
218
219 /* Fix checksum */
220 header->checksum = table_compute_checksum(rsdt,
221 sizeof(struct acpi_rsdt));
222}
223
224static void acpi_write_xsdt(struct acpi_xsdt *xsdt)
225{
226 struct acpi_table_header *header = &xsdt->header;
227
228 /* Fill out header fields */
229 acpi_fill_header(header, "XSDT");
230 header->length = sizeof(struct acpi_xsdt);
231 header->revision = 1;
232
233 /* Entries are filled in later, we come with an empty set */
234
235 /* Fix checksum */
236 header->checksum = table_compute_checksum(xsdt,
237 sizeof(struct acpi_xsdt));
238}
239
240void acpi_setup_base_tables(struct acpi_ctx *ctx, void *start)
241{
Simon Glass98528d42020-07-07 13:11:42 -0600242 ctx->base = start;
Simon Glass9c442a62020-04-26 09:19:51 -0600243 ctx->current = start;
244
245 /* Align ACPI tables to 16 byte */
246 acpi_align(ctx);
Simon Glassfaf08c72020-04-26 09:19:53 -0600247 gd->arch.acpi_start = map_to_sysmem(ctx->current);
Simon Glass9c442a62020-04-26 09:19:51 -0600248
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));
Simon Glassabeaca82020-04-26 09:19:52 -0600254 ctx->xsdt = ctx->current;
Simon Glass9c442a62020-04-26 09:19:51 -0600255 acpi_inc_align(ctx, sizeof(struct acpi_xsdt));
256
257 /* clear all table memory */
258 memset((void *)start, '\0', ctx->current - start);
259
Simon Glassabeaca82020-04-26 09:19:52 -0600260 acpi_write_rsdp(ctx->rsdp, ctx->rsdt, ctx->xsdt);
Simon Glass9c442a62020-04-26 09:19:51 -0600261 acpi_write_rsdt(ctx->rsdt);
Simon Glassabeaca82020-04-26 09:19:52 -0600262 acpi_write_xsdt(ctx->xsdt);
Simon Glass9c442a62020-04-26 09:19:51 -0600263 /*
264 * Per ACPI spec, the FACS table address must be aligned to a 64 byte
265 * boundary (Windows checks this, but Linux does not).
266 */
267 acpi_align64(ctx);
268}
Simon Glass95971892020-09-22 12:45:10 -0600269
270void 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)
274{
275 uintptr_t current;
276 struct acpi_dbg2_device *device;
277 u32 *dbg2_addr_size;
278 struct acpi_table_header *header;
279 size_t path_len;
280 const char *path;
281 char *namespace;
282
283 /* Fill out header fields. */
284 current = (uintptr_t)dbg2;
285 memset(dbg2, '\0', sizeof(struct acpi_dbg2_header));
286 header = &dbg2->header;
287
288 header->revision = acpi_get_table_revision(ACPITAB_DBG2);
289 acpi_fill_header(header, "DBG2");
290 header->aslc_revision = ASL_REVISION;
291
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);
296
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);
301
302 device->revision = 0;
303 device->address_count = 1;
304 device->port_type = port_type;
305 device->port_subtype = port_subtype;
306
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);
311
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);
317
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);
326 current += path_len;
327
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);
332}