blob: a8d4b470001d16f3811a42d001184118104087b0 [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 Glass90b01272023-04-29 19:21:46 -060014#include <version_string.h>
Simon Glass0e113842020-04-26 09:19:47 -060015#include <acpi/acpi_table.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060016#include <asm/global_data.h>
Simon Glass0e113842020-04-26 09:19:47 -060017#include <dm/acpi.h>
Simon Glasse9629892020-04-08 16:57:39 -060018
Pali Rohárf0de20e2021-07-10 13:10:01 +020019/*
20 * OEM_REVISION is 32-bit unsigned number. It should be increased only when
21 * changing software version. Therefore it should not depend on build time.
22 * U-Boot calculates it from U-Boot version and represent it in hexadecimal
23 * notation. As U-Boot version is in form year.month set low 8 bits to 0x01
24 * to have valid date. So for U-Boot version 2021.04 OEM_REVISION is set to
25 * value 0x20210401.
26 */
Simon Glass90b01272023-04-29 19:21:46 -060027#define OEM_REVISION ((((version_num / 1000) % 10) << 28) | \
28 (((version_num / 100) % 10) << 24) | \
29 (((version_num / 10) % 10) << 20) | \
30 ((version_num % 10) << 16) | \
31 (((version_num_patch / 10) % 10) << 12) | \
32 ((version_num_patch % 10) << 8) | \
Pali Rohárf0de20e2021-07-10 13:10:01 +020033 0x01)
34
Simon Glasse9629892020-04-08 16:57:39 -060035int acpi_create_dmar(struct acpi_dmar *dmar, enum dmar_flags flags)
36{
37 struct acpi_table_header *header = &dmar->header;
38 struct cpu_info info;
39 struct udevice *cpu;
40 int ret;
41
Michal Suchanekac12a2f2022-10-12 21:57:59 +020042 ret = uclass_first_device_err(UCLASS_CPU, &cpu);
Simon Glasse9629892020-04-08 16:57:39 -060043 if (ret)
44 return log_msg_ret("cpu", ret);
45 ret = cpu_get_info(cpu, &info);
46 if (ret)
47 return log_msg_ret("info", ret);
48 memset((void *)dmar, 0, sizeof(struct acpi_dmar));
49
50 /* Fill out header fields. */
51 acpi_fill_header(&dmar->header, "DMAR");
52 header->length = sizeof(struct acpi_dmar);
53 header->revision = acpi_get_table_revision(ACPITAB_DMAR);
54
55 dmar->host_address_width = info.address_width - 1;
56 dmar->flags = flags;
57
58 return 0;
59}
Simon Glassb2672ea2020-04-08 16:57:38 -060060
61int acpi_get_table_revision(enum acpi_tables table)
62{
63 switch (table) {
64 case ACPITAB_FADT:
65 return ACPI_FADT_REV_ACPI_3_0;
66 case ACPITAB_MADT:
67 return ACPI_MADT_REV_ACPI_3_0;
68 case ACPITAB_MCFG:
69 return ACPI_MCFG_REV_ACPI_3_0;
70 case ACPITAB_TCPA:
71 /* This version and the rest are open-coded */
72 return 2;
73 case ACPITAB_TPM2:
74 return 4;
75 case ACPITAB_SSDT: /* ACPI 3.0 upto 6.3: 2 */
76 return 2;
77 case ACPITAB_SRAT: /* ACPI 2.0: 1, ACPI 3.0: 2, ACPI 4.0 to 6.3: 3 */
78 return 1; /* TODO Should probably be upgraded to 2 */
79 case ACPITAB_DMAR:
80 return 1;
81 case ACPITAB_SLIT: /* ACPI 2.0 upto 6.3: 1 */
82 return 1;
83 case ACPITAB_SPMI: /* IMPI 2.0 */
84 return 5;
85 case ACPITAB_HPET: /* Currently 1. Table added in ACPI 2.0 */
86 return 1;
87 case ACPITAB_VFCT: /* ACPI 2.0/3.0/4.0: 1 */
88 return 1;
89 case ACPITAB_IVRS:
90 return IVRS_FORMAT_FIXED;
91 case ACPITAB_DBG2:
92 return 0;
93 case ACPITAB_FACS: /* ACPI 2.0/3.0: 1, ACPI 4.0 to 6.3: 2 */
94 return 1;
95 case ACPITAB_RSDT: /* ACPI 1.0 upto 6.3: 1 */
96 return 1;
97 case ACPITAB_XSDT: /* ACPI 2.0 upto 6.3: 1 */
98 return 1;
99 case ACPITAB_RSDP: /* ACPI 2.0 upto 6.3: 2 */
100 return 2;
101 case ACPITAB_HEST:
102 return 1;
103 case ACPITAB_NHLT:
104 return 5;
105 case ACPITAB_BERT:
106 return 1;
107 case ACPITAB_SPCR:
108 return 2;
109 default:
110 return -EINVAL;
111 }
112}
Simon Glass17968c32020-04-26 09:19:46 -0600113
114void acpi_fill_header(struct acpi_table_header *header, char *signature)
115{
116 memcpy(header->signature, signature, 4);
117 memcpy(header->oem_id, OEM_ID, 6);
118 memcpy(header->oem_table_id, OEM_TABLE_ID, 8);
Pali Rohárf0de20e2021-07-10 13:10:01 +0200119 header->oem_revision = OEM_REVISION;
Simon Glass17968c32020-04-26 09:19:46 -0600120 memcpy(header->aslc_id, ASLC_ID, 4);
121}
Simon Glass0e113842020-04-26 09:19:47 -0600122
123void acpi_align(struct acpi_ctx *ctx)
124{
125 ctx->current = (void *)ALIGN((ulong)ctx->current, 16);
126}
127
128void acpi_align64(struct acpi_ctx *ctx)
129{
130 ctx->current = (void *)ALIGN((ulong)ctx->current, 64);
131}
132
133void acpi_inc(struct acpi_ctx *ctx, uint amount)
134{
135 ctx->current += amount;
136}
137
138void acpi_inc_align(struct acpi_ctx *ctx, uint amount)
139{
140 ctx->current += amount;
141 acpi_align(ctx);
142}
Simon Glass575a5472020-04-26 09:19:50 -0600143
144/**
145 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
146 * and checksum.
147 */
148int acpi_add_table(struct acpi_ctx *ctx, void *table)
149{
150 int i, entries_num;
151 struct acpi_rsdt *rsdt;
152 struct acpi_xsdt *xsdt;
153
154 /* The RSDT is mandatory while the XSDT is not */
155 rsdt = ctx->rsdt;
156
157 /* This should always be MAX_ACPI_TABLES */
158 entries_num = ARRAY_SIZE(rsdt->entry);
159
160 for (i = 0; i < entries_num; i++) {
161 if (rsdt->entry[i] == 0)
162 break;
163 }
164
165 if (i >= entries_num) {
166 log_err("ACPI: Error: too many tables\n");
167 return -E2BIG;
168 }
169
170 /* Add table to the RSDT */
171 rsdt->entry[i] = map_to_sysmem(table);
172
173 /* Fix RSDT length or the kernel will assume invalid entries */
174 rsdt->header.length = sizeof(struct acpi_table_header) +
175 (sizeof(u32) * (i + 1));
176
177 /* Re-calculate checksum */
178 rsdt->header.checksum = 0;
179 rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
180 rsdt->header.length);
181
182 /*
183 * And now the same thing for the XSDT. We use the same index as for
184 * now we want the XSDT and RSDT to always be in sync in U-Boot
185 */
Simon Glassabeaca82020-04-26 09:19:52 -0600186 xsdt = ctx->xsdt;
Simon Glass575a5472020-04-26 09:19:50 -0600187
188 /* Add table to the XSDT */
189 xsdt->entry[i] = map_to_sysmem(table);
190
191 /* Fix XSDT length */
192 xsdt->header.length = sizeof(struct acpi_table_header) +
193 (sizeof(u64) * (i + 1));
194
195 /* Re-calculate checksum */
196 xsdt->header.checksum = 0;
197 xsdt->header.checksum = table_compute_checksum((u8 *)xsdt,
198 xsdt->header.length);
199
200 return 0;
201}
Simon Glass9c442a62020-04-26 09:19:51 -0600202
Simon Glass95971892020-09-22 12:45:10 -0600203void acpi_create_dbg2(struct acpi_dbg2_header *dbg2,
204 int port_type, int port_subtype,
205 struct acpi_gen_regaddr *address, u32 address_size,
206 const char *device_path)
207{
208 uintptr_t current;
209 struct acpi_dbg2_device *device;
210 u32 *dbg2_addr_size;
211 struct acpi_table_header *header;
212 size_t path_len;
213 const char *path;
214 char *namespace;
215
216 /* Fill out header fields. */
217 current = (uintptr_t)dbg2;
218 memset(dbg2, '\0', sizeof(struct acpi_dbg2_header));
219 header = &dbg2->header;
220
221 header->revision = acpi_get_table_revision(ACPITAB_DBG2);
222 acpi_fill_header(header, "DBG2");
223 header->aslc_revision = ASL_REVISION;
224
225 /* One debug device defined */
226 dbg2->devices_offset = sizeof(struct acpi_dbg2_header);
227 dbg2->devices_count = 1;
228 current += sizeof(struct acpi_dbg2_header);
229
230 /* Device comes after the header */
231 device = (struct acpi_dbg2_device *)current;
232 memset(device, 0, sizeof(struct acpi_dbg2_device));
233 current += sizeof(struct acpi_dbg2_device);
234
235 device->revision = 0;
236 device->address_count = 1;
237 device->port_type = port_type;
238 device->port_subtype = port_subtype;
239
240 /* Base Address comes after device structure */
241 memcpy((void *)current, address, sizeof(struct acpi_gen_regaddr));
242 device->base_address_offset = current - (uintptr_t)device;
243 current += sizeof(struct acpi_gen_regaddr);
244
245 /* Address Size comes after address structure */
246 dbg2_addr_size = (uint32_t *)current;
247 device->address_size_offset = current - (uintptr_t)device;
248 *dbg2_addr_size = address_size;
249 current += sizeof(uint32_t);
250
251 /* Namespace string comes last, use '.' if not provided */
252 path = device_path ? : ".";
253 /* Namespace string length includes NULL terminator */
254 path_len = strlen(path) + 1;
255 namespace = (char *)current;
256 device->namespace_string_length = path_len;
257 device->namespace_string_offset = current - (uintptr_t)device;
258 strncpy(namespace, path, path_len);
259 current += path_len;
260
261 /* Update structure lengths and checksum */
262 device->length = current - (uintptr_t)device;
263 header->length = current - (uintptr_t)dbg2;
264 header->checksum = table_compute_checksum(dbg2, header->length);
265}