blob: c9ddcca8cb558396ca26c4ff0eb965df8e64fb6a [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
Simon Glassbfeb5d42020-04-08 16:57:39 -06008#include <dm.h>
9#include <cpu.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Simon Glass29b35112020-04-26 09:19:50 -060011#include <mapmem.h>
12#include <tables_csum.h>
Maximilian Brune1c03efc2024-10-23 15:19:44 +020013#include <serial.h>
Simon Glass1e4d9652023-04-29 19:21:46 -060014#include <version_string.h>
Simon Glass86e17782020-04-26 09:19:47 -060015#include <acpi/acpi_table.h>
Maximilian Brune1c03efc2024-10-23 15:19:44 +020016#include <acpi/acpi_device.h>
Simon Glass401d1c42020-10-30 21:38:53 -060017#include <asm/global_data.h>
Simon Glass86e17782020-04-26 09:19:47 -060018#include <dm/acpi.h>
Simon Glassbfeb5d42020-04-08 16:57:39 -060019
Pali Rohára3423b32021-07-10 13:10:01 +020020/*
21 * OEM_REVISION is 32-bit unsigned number. It should be increased only when
22 * changing software version. Therefore it should not depend on build time.
23 * U-Boot calculates it from U-Boot version and represent it in hexadecimal
24 * notation. As U-Boot version is in form year.month set low 8 bits to 0x01
25 * to have valid date. So for U-Boot version 2021.04 OEM_REVISION is set to
26 * value 0x20210401.
27 */
Simon Glass1e4d9652023-04-29 19:21:46 -060028#define OEM_REVISION ((((version_num / 1000) % 10) << 28) | \
29 (((version_num / 100) % 10) << 24) | \
30 (((version_num / 10) % 10) << 20) | \
31 ((version_num % 10) << 16) | \
32 (((version_num_patch / 10) % 10) << 12) | \
33 ((version_num_patch % 10) << 8) | \
Pali Rohára3423b32021-07-10 13:10:01 +020034 0x01)
35
Simon Glassbfeb5d42020-04-08 16:57:39 -060036int acpi_create_dmar(struct acpi_dmar *dmar, enum dmar_flags flags)
37{
38 struct acpi_table_header *header = &dmar->header;
39 struct cpu_info info;
40 struct udevice *cpu;
41 int ret;
42
Michal Suchanekc726fc02022-10-12 21:57:59 +020043 ret = uclass_first_device_err(UCLASS_CPU, &cpu);
Simon Glassbfeb5d42020-04-08 16:57:39 -060044 if (ret)
45 return log_msg_ret("cpu", ret);
46 ret = cpu_get_info(cpu, &info);
47 if (ret)
48 return log_msg_ret("info", ret);
49 memset((void *)dmar, 0, sizeof(struct acpi_dmar));
50
51 /* Fill out header fields. */
52 acpi_fill_header(&dmar->header, "DMAR");
53 header->length = sizeof(struct acpi_dmar);
54 header->revision = acpi_get_table_revision(ACPITAB_DMAR);
55
56 dmar->host_address_width = info.address_width - 1;
57 dmar->flags = flags;
58
59 return 0;
60}
Simon Glass91fe8b72020-04-08 16:57:38 -060061
62int acpi_get_table_revision(enum acpi_tables table)
63{
64 switch (table) {
65 case ACPITAB_FADT:
66 return ACPI_FADT_REV_ACPI_3_0;
67 case ACPITAB_MADT:
68 return ACPI_MADT_REV_ACPI_3_0;
69 case ACPITAB_MCFG:
70 return ACPI_MCFG_REV_ACPI_3_0;
71 case ACPITAB_TCPA:
72 /* This version and the rest are open-coded */
73 return 2;
74 case ACPITAB_TPM2:
75 return 4;
76 case ACPITAB_SSDT: /* ACPI 3.0 upto 6.3: 2 */
77 return 2;
78 case ACPITAB_SRAT: /* ACPI 2.0: 1, ACPI 3.0: 2, ACPI 4.0 to 6.3: 3 */
79 return 1; /* TODO Should probably be upgraded to 2 */
80 case ACPITAB_DMAR:
81 return 1;
82 case ACPITAB_SLIT: /* ACPI 2.0 upto 6.3: 1 */
83 return 1;
84 case ACPITAB_SPMI: /* IMPI 2.0 */
85 return 5;
86 case ACPITAB_HPET: /* Currently 1. Table added in ACPI 2.0 */
87 return 1;
88 case ACPITAB_VFCT: /* ACPI 2.0/3.0/4.0: 1 */
89 return 1;
90 case ACPITAB_IVRS:
91 return IVRS_FORMAT_FIXED;
92 case ACPITAB_DBG2:
93 return 0;
94 case ACPITAB_FACS: /* ACPI 2.0/3.0: 1, ACPI 4.0 to 6.3: 2 */
95 return 1;
96 case ACPITAB_RSDT: /* ACPI 1.0 upto 6.3: 1 */
97 return 1;
98 case ACPITAB_XSDT: /* ACPI 2.0 upto 6.3: 1 */
99 return 1;
100 case ACPITAB_RSDP: /* ACPI 2.0 upto 6.3: 2 */
101 return 2;
102 case ACPITAB_HEST:
103 return 1;
104 case ACPITAB_NHLT:
105 return 5;
106 case ACPITAB_BERT:
107 return 1;
108 case ACPITAB_SPCR:
109 return 2;
110 default:
111 return -EINVAL;
112 }
113}
Simon Glass93f7f822020-04-26 09:19:46 -0600114
115void acpi_fill_header(struct acpi_table_header *header, char *signature)
116{
117 memcpy(header->signature, signature, 4);
118 memcpy(header->oem_id, OEM_ID, 6);
119 memcpy(header->oem_table_id, OEM_TABLE_ID, 8);
Pali Rohára3423b32021-07-10 13:10:01 +0200120 header->oem_revision = OEM_REVISION;
Heinrich Schuchardt4735d032024-01-21 12:52:48 +0100121 memcpy(header->creator_id, ASLC_ID, 4);
Heinrich Schuchardt07a6c692024-04-18 05:11:13 +0200122 header->creator_revision = ASL_REVISION;
Simon Glass93f7f822020-04-26 09:19:46 -0600123}
Simon Glass86e17782020-04-26 09:19:47 -0600124
125void acpi_align(struct acpi_ctx *ctx)
126{
127 ctx->current = (void *)ALIGN((ulong)ctx->current, 16);
128}
129
130void acpi_align64(struct acpi_ctx *ctx)
131{
132 ctx->current = (void *)ALIGN((ulong)ctx->current, 64);
133}
134
135void acpi_inc(struct acpi_ctx *ctx, uint amount)
136{
137 ctx->current += amount;
138}
139
140void acpi_inc_align(struct acpi_ctx *ctx, uint amount)
141{
142 ctx->current += amount;
143 acpi_align(ctx);
144}
Simon Glass29b35112020-04-26 09:19:50 -0600145
146/**
147 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
148 * and checksum.
149 */
150int acpi_add_table(struct acpi_ctx *ctx, void *table)
151{
152 int i, entries_num;
153 struct acpi_rsdt *rsdt;
154 struct acpi_xsdt *xsdt;
155
156 /* The RSDT is mandatory while the XSDT is not */
157 rsdt = ctx->rsdt;
158
159 /* This should always be MAX_ACPI_TABLES */
160 entries_num = ARRAY_SIZE(rsdt->entry);
161
162 for (i = 0; i < entries_num; i++) {
163 if (rsdt->entry[i] == 0)
164 break;
165 }
166
167 if (i >= entries_num) {
168 log_err("ACPI: Error: too many tables\n");
169 return -E2BIG;
170 }
171
172 /* Add table to the RSDT */
Simon Glassa8efebe2023-12-31 08:25:54 -0700173 rsdt->entry[i] = nomap_to_sysmem(table);
Simon Glass29b35112020-04-26 09:19:50 -0600174
175 /* Fix RSDT length or the kernel will assume invalid entries */
176 rsdt->header.length = sizeof(struct acpi_table_header) +
177 (sizeof(u32) * (i + 1));
178
179 /* Re-calculate checksum */
180 rsdt->header.checksum = 0;
181 rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
182 rsdt->header.length);
183
184 /*
185 * And now the same thing for the XSDT. We use the same index as for
186 * now we want the XSDT and RSDT to always be in sync in U-Boot
187 */
Simon Glassb38309b2020-04-26 09:19:52 -0600188 xsdt = ctx->xsdt;
Simon Glass29b35112020-04-26 09:19:50 -0600189
190 /* Add table to the XSDT */
Simon Glassa8efebe2023-12-31 08:25:54 -0700191 xsdt->entry[i] = nomap_to_sysmem(table);
Simon Glass29b35112020-04-26 09:19:50 -0600192
193 /* Fix XSDT length */
194 xsdt->header.length = sizeof(struct acpi_table_header) +
195 (sizeof(u64) * (i + 1));
196
197 /* Re-calculate checksum */
198 xsdt->header.checksum = 0;
199 xsdt->header.checksum = table_compute_checksum((u8 *)xsdt,
200 xsdt->header.length);
201
202 return 0;
203}
Simon Glass7e586f62020-04-26 09:19:51 -0600204
Simon Glassf37979e2020-09-22 12:45:10 -0600205void acpi_create_dbg2(struct acpi_dbg2_header *dbg2,
206 int port_type, int port_subtype,
207 struct acpi_gen_regaddr *address, u32 address_size,
208 const char *device_path)
209{
210 uintptr_t current;
211 struct acpi_dbg2_device *device;
212 u32 *dbg2_addr_size;
213 struct acpi_table_header *header;
214 size_t path_len;
215 const char *path;
216 char *namespace;
217
218 /* Fill out header fields. */
219 current = (uintptr_t)dbg2;
220 memset(dbg2, '\0', sizeof(struct acpi_dbg2_header));
221 header = &dbg2->header;
222
223 header->revision = acpi_get_table_revision(ACPITAB_DBG2);
224 acpi_fill_header(header, "DBG2");
Simon Glassf37979e2020-09-22 12:45:10 -0600225
226 /* One debug device defined */
227 dbg2->devices_offset = sizeof(struct acpi_dbg2_header);
228 dbg2->devices_count = 1;
229 current += sizeof(struct acpi_dbg2_header);
230
231 /* Device comes after the header */
232 device = (struct acpi_dbg2_device *)current;
233 memset(device, 0, sizeof(struct acpi_dbg2_device));
234 current += sizeof(struct acpi_dbg2_device);
235
236 device->revision = 0;
237 device->address_count = 1;
238 device->port_type = port_type;
239 device->port_subtype = port_subtype;
240
241 /* Base Address comes after device structure */
242 memcpy((void *)current, address, sizeof(struct acpi_gen_regaddr));
243 device->base_address_offset = current - (uintptr_t)device;
244 current += sizeof(struct acpi_gen_regaddr);
245
246 /* Address Size comes after address structure */
247 dbg2_addr_size = (uint32_t *)current;
248 device->address_size_offset = current - (uintptr_t)device;
249 *dbg2_addr_size = address_size;
250 current += sizeof(uint32_t);
251
252 /* Namespace string comes last, use '.' if not provided */
253 path = device_path ? : ".";
254 /* Namespace string length includes NULL terminator */
255 path_len = strlen(path) + 1;
256 namespace = (char *)current;
257 device->namespace_string_length = path_len;
258 device->namespace_string_offset = current - (uintptr_t)device;
259 strncpy(namespace, path, path_len);
260 current += path_len;
261
262 /* Update structure lengths and checksum */
263 device->length = current - (uintptr_t)device;
264 header->length = current - (uintptr_t)dbg2;
265 header->checksum = table_compute_checksum(dbg2, header->length);
266}
Maximilian Brune1c03efc2024-10-23 15:19:44 +0200267
268int acpi_write_dbg2_pci_uart(struct acpi_ctx *ctx, struct udevice *dev,
269 uint access_size)
270{
271 struct acpi_dbg2_header *dbg2 = ctx->current;
272 char path[ACPI_PATH_MAX];
273 struct acpi_gen_regaddr address;
274 u64 addr;
275 int ret;
276
277 if (!device_active(dev)) {
278 log_info("Device not enabled\n");
279 return -EACCES;
280 }
281 /*
282 * PCI devices don't remember their resource allocation information in
283 * U-Boot at present. We assume that MMIO is used for the UART and that
284 * the address space is 32 bytes: ns16550 uses 8 registers of up to
285 * 32-bits each. This is only for debugging so it is not a big deal.
286 */
287 addr = dm_pci_read_bar32(dev, 0);
288 log_debug("UART addr %lx\n", (ulong)addr);
289
290 ret = acpi_device_path(dev, path, sizeof(path));
291 if (ret)
292 return log_msg_ret("path", ret);
293
294 memset(&address, '\0', sizeof(address));
295 address.space_id = ACPI_ADDRESS_SPACE_MEMORY;
296 address.addrl = (uint32_t)addr;
297 address.addrh = (uint32_t)((addr >> 32) & 0xffffffff);
298 address.access_size = access_size;
299
300 ret = acpi_device_path(dev, path, sizeof(path));
301 if (ret)
302 return log_msg_ret("path", ret);
303 acpi_create_dbg2(dbg2, ACPI_DBG2_SERIAL_PORT,
304 ACPI_DBG2_16550_COMPATIBLE, &address, 0x1000, path);
305
306 acpi_inc_align(ctx, dbg2->header.length);
307 acpi_add_table(ctx, dbg2);
308
309 return 0;
310}
311
312static int acpi_write_spcr(struct acpi_ctx *ctx, const struct acpi_writer *entry)
313{
314 struct serial_device_info serial_info = {0};
315 ulong serial_address, serial_offset;
316 struct acpi_table_header *header;
317 struct acpi_spcr *spcr;
318 struct udevice *dev;
319 uint serial_config;
320 uint serial_width;
321 int access_size;
322 int space_id;
323 int ret = -ENODEV;
324
325 spcr = ctx->current;
326 header = &spcr->header;
327
328 memset(spcr, '\0', sizeof(struct acpi_spcr));
329
330 /* Fill out header fields */
331 acpi_fill_header(header, "SPCR");
332 header->length = sizeof(struct acpi_spcr);
333 header->revision = 2;
334
335 /* Read the device once, here. It is reused below */
336 dev = gd->cur_serial_dev;
337 if (dev)
338 ret = serial_getinfo(dev, &serial_info);
339 if (ret)
340 serial_info.type = SERIAL_CHIP_UNKNOWN;
341
342 /* Encode chip type */
343 switch (serial_info.type) {
344 case SERIAL_CHIP_16550_COMPATIBLE:
345 spcr->interface_type = ACPI_DBG2_16550_COMPATIBLE;
346 break;
347 case SERIAL_CHIP_PL01X:
348 spcr->interface_type = ACPI_DBG2_ARM_PL011;
349 break;
350 case SERIAL_CHIP_UNKNOWN:
351 default:
352 spcr->interface_type = ACPI_DBG2_UNKNOWN;
353 break;
354 }
355
356 /* Encode address space */
357 switch (serial_info.addr_space) {
358 case SERIAL_ADDRESS_SPACE_MEMORY:
359 space_id = ACPI_ADDRESS_SPACE_MEMORY;
360 break;
361 case SERIAL_ADDRESS_SPACE_IO:
362 default:
363 space_id = ACPI_ADDRESS_SPACE_IO;
364 break;
365 }
366
367 serial_width = serial_info.reg_width * 8;
368 serial_offset = serial_info.reg_offset << serial_info.reg_shift;
369 serial_address = serial_info.addr + serial_offset;
370
371 /* Encode register access size */
372 switch (serial_info.reg_shift) {
373 case 0:
374 access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS;
375 break;
376 case 1:
377 access_size = ACPI_ACCESS_SIZE_WORD_ACCESS;
378 break;
379 case 2:
380 access_size = ACPI_ACCESS_SIZE_DWORD_ACCESS;
381 break;
382 case 3:
383 access_size = ACPI_ACCESS_SIZE_QWORD_ACCESS;
384 break;
385 default:
386 access_size = ACPI_ACCESS_SIZE_UNDEFINED;
387 break;
388 }
389
390 debug("UART type %u @ %lx\n", spcr->interface_type, serial_address);
391
392 /* Fill GAS */
393 spcr->serial_port.space_id = space_id;
394 spcr->serial_port.bit_width = serial_width;
395 spcr->serial_port.bit_offset = 0;
396 spcr->serial_port.access_size = access_size;
397 spcr->serial_port.addrl = lower_32_bits(serial_address);
398 spcr->serial_port.addrh = upper_32_bits(serial_address);
399
400 /* Encode baud rate */
401 switch (serial_info.baudrate) {
402 case 9600:
403 spcr->baud_rate = 3;
404 break;
405 case 19200:
406 spcr->baud_rate = 4;
407 break;
408 case 57600:
409 spcr->baud_rate = 6;
410 break;
411 case 115200:
412 spcr->baud_rate = 7;
413 break;
414 default:
415 spcr->baud_rate = 0;
416 break;
417 }
418
419 serial_config = SERIAL_DEFAULT_CONFIG;
420 if (dev)
421 ret = serial_getconfig(dev, &serial_config);
422
423 spcr->parity = SERIAL_GET_PARITY(serial_config);
424 spcr->stop_bits = SERIAL_GET_STOP(serial_config);
425
426 /* No PCI devices for now */
427 spcr->pci_device_id = 0xffff;
428 spcr->pci_vendor_id = 0xffff;
429
430 /*
431 * SPCR has no clue if the UART base clock speed is different
432 * to the default one. However, the SPCR 1.04 defines baud rate
433 * 0 as a preconfigured state of UART and OS is supposed not
434 * to touch the configuration of the serial device.
435 */
436 if (serial_info.clock != SERIAL_DEFAULT_CLOCK)
437 spcr->baud_rate = 0;
438
439 /* Fix checksum */
440 header->checksum = table_compute_checksum((void *)spcr, header->length);
441
442 acpi_add_table(ctx, spcr);
443 acpi_inc(ctx, spcr->header.length);
444
445 return 0;
446}
447
448ACPI_WRITER(5spcr, "SPCR", acpi_write_spcr, 0);