blob: 431776666e2b0b66b51df267dbdf829fc2086835 [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>
16#include <dm/acpi.h>
Simon Glassbfeb5d42020-04-08 16:57:39 -060017
Simon Glassbfeb5d42020-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 Glass91fe8b72020-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 Glass93f7f822020-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 Glass86e17782020-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 Glass29b35112020-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 Glassb38309b2020-04-26 09:19:52 -0600169 xsdt = ctx->xsdt;
Simon Glass29b35112020-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 Glass7e586f62020-04-26 09:19:51 -0600185
186static void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt,
187 struct acpi_xsdt *xsdt)
188{
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 Glass7e586f62020-04-26 09:19:51 -0600240 ctx->current = start;
241
242 /* Align ACPI tables to 16 byte */
243 acpi_align(ctx);
Simon Glass0b885bc2020-04-26 09:19:53 -0600244 gd->arch.acpi_start = map_to_sysmem(ctx->current);
Simon Glass7e586f62020-04-26 09:19:51 -0600245
246 /* We need at least an RSDP and an RSDT Table */
247 ctx->rsdp = ctx->current;
248 acpi_inc_align(ctx, sizeof(struct acpi_rsdp));
249 ctx->rsdt = ctx->current;
250 acpi_inc_align(ctx, sizeof(struct acpi_rsdt));
Simon Glassb38309b2020-04-26 09:19:52 -0600251 ctx->xsdt = ctx->current;
Simon Glass7e586f62020-04-26 09:19:51 -0600252 acpi_inc_align(ctx, sizeof(struct acpi_xsdt));
253
254 /* clear all table memory */
255 memset((void *)start, '\0', ctx->current - start);
256
Simon Glassb38309b2020-04-26 09:19:52 -0600257 acpi_write_rsdp(ctx->rsdp, ctx->rsdt, ctx->xsdt);
Simon Glass7e586f62020-04-26 09:19:51 -0600258 acpi_write_rsdt(ctx->rsdt);
Simon Glassb38309b2020-04-26 09:19:52 -0600259 acpi_write_xsdt(ctx->xsdt);
Simon Glass7e586f62020-04-26 09:19:51 -0600260 /*
261 * Per ACPI spec, the FACS table address must be aligned to a 64 byte
262 * boundary (Windows checks this, but Linux does not).
263 */
264 acpi_align64(ctx);
265}