blob: 959cac9e2e3b2030bb1d46368ebf73fb702ff3e0 [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:
Patrick Rudolph4b882f62024-10-23 15:19:52 +020066 return ACPI_FADT_REV_ACPI_6_0;
Simon Glass91fe8b72020-04-08 16:57:38 -060067 case ACPITAB_MADT:
Patrick Rudolph4b882f62024-10-23 15:19:52 +020068 return ACPI_MADT_REV_ACPI_6_2;
Simon Glass91fe8b72020-04-08 16:57:38 -060069 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
Maximilian Brunef5f79622024-10-23 15:19:45 +0200205int acpi_write_fadt(struct acpi_ctx *ctx, const struct acpi_writer *entry)
206{
207 struct acpi_table_header *header;
208 struct acpi_fadt *fadt;
209
210 fadt = ctx->current;
211 header = &fadt->header;
212
213 memset((void *)fadt, '\0', sizeof(struct acpi_fadt));
214
215 acpi_fill_header(header, "FACP");
216 header->length = sizeof(struct acpi_fadt);
217 header->revision = acpi_get_table_revision(ACPITAB_FADT);
218 memcpy(header->oem_id, OEM_ID, 6);
219 memcpy(header->oem_table_id, OEM_TABLE_ID, 8);
220 memcpy(header->creator_id, ASLC_ID, 4);
221 header->creator_revision = 1;
Patrick Rudolph4b882f62024-10-23 15:19:52 +0200222 fadt->minor_revision = 2;
Maximilian Brunef5f79622024-10-23 15:19:45 +0200223
224 fadt->x_firmware_ctrl = map_to_sysmem(ctx->facs);
225 fadt->x_dsdt = map_to_sysmem(ctx->dsdt);
226
227 if (fadt->x_firmware_ctrl < 0x100000000ULL)
228 fadt->firmware_ctrl = fadt->x_firmware_ctrl;
229
230 if (fadt->x_dsdt < 0x100000000ULL)
231 fadt->dsdt = fadt->x_dsdt;
232
233 fadt->preferred_pm_profile = ACPI_PM_UNSPECIFIED;
234
235 acpi_fill_fadt(fadt);
236
237 header->checksum = table_compute_checksum(fadt, header->length);
238
239 return acpi_add_fadt(ctx, fadt);
240}
241
242ACPI_WRITER(5fadt, "FADT", acpi_write_fadt, 0);
243
Patrick Rudolph4a3fc0f2024-10-23 15:19:46 +0200244int acpi_write_madt(struct acpi_ctx *ctx, const struct acpi_writer *entry)
245{
246 struct acpi_table_header *header;
247 struct acpi_madt *madt;
248 void *current;
249
250 madt = ctx->current;
251
252 memset(madt, '\0', sizeof(struct acpi_madt));
253 header = &madt->header;
254
255 /* Fill out header fields */
256 acpi_fill_header(header, "APIC");
257 header->length = sizeof(struct acpi_madt);
Patrick Rudolph4b882f62024-10-23 15:19:52 +0200258 header->revision = acpi_get_table_revision(ACPITAB_MADT);
Patrick Rudolph4a3fc0f2024-10-23 15:19:46 +0200259
260 acpi_inc(ctx, sizeof(struct acpi_madt));
Patrick Rudolph763bad32024-10-23 15:19:51 +0200261 /* TODO: Get rid of acpi_fill_madt and use driver model */
Patrick Rudolph4a3fc0f2024-10-23 15:19:46 +0200262 current = acpi_fill_madt(madt, ctx);
263
264 /* (Re)calculate length and checksum */
265 header->length = (uintptr_t)current - (uintptr_t)madt;
Patrick Rudolph4a3fc0f2024-10-23 15:19:46 +0200266 header->checksum = table_compute_checksum((void *)madt, header->length);
267 acpi_add_table(ctx, madt);
268 ctx->current = (void *)madt + madt->header.length;
269
270 return 0;
271}
272
273ACPI_WRITER(5madt, "MADT", acpi_write_madt, 0);
274
Simon Glassf37979e2020-09-22 12:45:10 -0600275void acpi_create_dbg2(struct acpi_dbg2_header *dbg2,
276 int port_type, int port_subtype,
277 struct acpi_gen_regaddr *address, u32 address_size,
278 const char *device_path)
279{
280 uintptr_t current;
281 struct acpi_dbg2_device *device;
282 u32 *dbg2_addr_size;
283 struct acpi_table_header *header;
284 size_t path_len;
285 const char *path;
286 char *namespace;
287
288 /* Fill out header fields. */
289 current = (uintptr_t)dbg2;
290 memset(dbg2, '\0', sizeof(struct acpi_dbg2_header));
291 header = &dbg2->header;
292
293 header->revision = acpi_get_table_revision(ACPITAB_DBG2);
294 acpi_fill_header(header, "DBG2");
Simon Glassf37979e2020-09-22 12:45:10 -0600295
296 /* One debug device defined */
297 dbg2->devices_offset = sizeof(struct acpi_dbg2_header);
298 dbg2->devices_count = 1;
299 current += sizeof(struct acpi_dbg2_header);
300
301 /* Device comes after the header */
302 device = (struct acpi_dbg2_device *)current;
303 memset(device, 0, sizeof(struct acpi_dbg2_device));
304 current += sizeof(struct acpi_dbg2_device);
305
306 device->revision = 0;
307 device->address_count = 1;
308 device->port_type = port_type;
309 device->port_subtype = port_subtype;
310
311 /* Base Address comes after device structure */
312 memcpy((void *)current, address, sizeof(struct acpi_gen_regaddr));
313 device->base_address_offset = current - (uintptr_t)device;
314 current += sizeof(struct acpi_gen_regaddr);
315
316 /* Address Size comes after address structure */
317 dbg2_addr_size = (uint32_t *)current;
318 device->address_size_offset = current - (uintptr_t)device;
319 *dbg2_addr_size = address_size;
320 current += sizeof(uint32_t);
321
322 /* Namespace string comes last, use '.' if not provided */
323 path = device_path ? : ".";
324 /* Namespace string length includes NULL terminator */
325 path_len = strlen(path) + 1;
326 namespace = (char *)current;
327 device->namespace_string_length = path_len;
328 device->namespace_string_offset = current - (uintptr_t)device;
329 strncpy(namespace, path, path_len);
330 current += path_len;
331
332 /* Update structure lengths and checksum */
333 device->length = current - (uintptr_t)device;
334 header->length = current - (uintptr_t)dbg2;
335 header->checksum = table_compute_checksum(dbg2, header->length);
336}
Maximilian Brune1c03efc2024-10-23 15:19:44 +0200337
338int acpi_write_dbg2_pci_uart(struct acpi_ctx *ctx, struct udevice *dev,
339 uint access_size)
340{
341 struct acpi_dbg2_header *dbg2 = ctx->current;
342 char path[ACPI_PATH_MAX];
343 struct acpi_gen_regaddr address;
344 u64 addr;
345 int ret;
346
347 if (!device_active(dev)) {
348 log_info("Device not enabled\n");
349 return -EACCES;
350 }
351 /*
352 * PCI devices don't remember their resource allocation information in
353 * U-Boot at present. We assume that MMIO is used for the UART and that
354 * the address space is 32 bytes: ns16550 uses 8 registers of up to
355 * 32-bits each. This is only for debugging so it is not a big deal.
356 */
357 addr = dm_pci_read_bar32(dev, 0);
358 log_debug("UART addr %lx\n", (ulong)addr);
359
360 ret = acpi_device_path(dev, path, sizeof(path));
361 if (ret)
362 return log_msg_ret("path", ret);
363
364 memset(&address, '\0', sizeof(address));
365 address.space_id = ACPI_ADDRESS_SPACE_MEMORY;
366 address.addrl = (uint32_t)addr;
367 address.addrh = (uint32_t)((addr >> 32) & 0xffffffff);
368 address.access_size = access_size;
369
370 ret = acpi_device_path(dev, path, sizeof(path));
371 if (ret)
372 return log_msg_ret("path", ret);
373 acpi_create_dbg2(dbg2, ACPI_DBG2_SERIAL_PORT,
374 ACPI_DBG2_16550_COMPATIBLE, &address, 0x1000, path);
375
376 acpi_inc_align(ctx, dbg2->header.length);
377 acpi_add_table(ctx, dbg2);
378
379 return 0;
380}
381
382static int acpi_write_spcr(struct acpi_ctx *ctx, const struct acpi_writer *entry)
383{
384 struct serial_device_info serial_info = {0};
385 ulong serial_address, serial_offset;
386 struct acpi_table_header *header;
387 struct acpi_spcr *spcr;
388 struct udevice *dev;
389 uint serial_config;
390 uint serial_width;
391 int access_size;
392 int space_id;
393 int ret = -ENODEV;
394
395 spcr = ctx->current;
396 header = &spcr->header;
397
398 memset(spcr, '\0', sizeof(struct acpi_spcr));
399
400 /* Fill out header fields */
401 acpi_fill_header(header, "SPCR");
402 header->length = sizeof(struct acpi_spcr);
403 header->revision = 2;
404
405 /* Read the device once, here. It is reused below */
406 dev = gd->cur_serial_dev;
407 if (dev)
408 ret = serial_getinfo(dev, &serial_info);
409 if (ret)
410 serial_info.type = SERIAL_CHIP_UNKNOWN;
411
412 /* Encode chip type */
413 switch (serial_info.type) {
414 case SERIAL_CHIP_16550_COMPATIBLE:
415 spcr->interface_type = ACPI_DBG2_16550_COMPATIBLE;
416 break;
417 case SERIAL_CHIP_PL01X:
418 spcr->interface_type = ACPI_DBG2_ARM_PL011;
419 break;
420 case SERIAL_CHIP_UNKNOWN:
421 default:
422 spcr->interface_type = ACPI_DBG2_UNKNOWN;
423 break;
424 }
425
426 /* Encode address space */
427 switch (serial_info.addr_space) {
428 case SERIAL_ADDRESS_SPACE_MEMORY:
429 space_id = ACPI_ADDRESS_SPACE_MEMORY;
430 break;
431 case SERIAL_ADDRESS_SPACE_IO:
432 default:
433 space_id = ACPI_ADDRESS_SPACE_IO;
434 break;
435 }
436
437 serial_width = serial_info.reg_width * 8;
438 serial_offset = serial_info.reg_offset << serial_info.reg_shift;
439 serial_address = serial_info.addr + serial_offset;
440
441 /* Encode register access size */
442 switch (serial_info.reg_shift) {
443 case 0:
444 access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS;
445 break;
446 case 1:
447 access_size = ACPI_ACCESS_SIZE_WORD_ACCESS;
448 break;
449 case 2:
450 access_size = ACPI_ACCESS_SIZE_DWORD_ACCESS;
451 break;
452 case 3:
453 access_size = ACPI_ACCESS_SIZE_QWORD_ACCESS;
454 break;
455 default:
456 access_size = ACPI_ACCESS_SIZE_UNDEFINED;
457 break;
458 }
459
460 debug("UART type %u @ %lx\n", spcr->interface_type, serial_address);
461
462 /* Fill GAS */
463 spcr->serial_port.space_id = space_id;
464 spcr->serial_port.bit_width = serial_width;
465 spcr->serial_port.bit_offset = 0;
466 spcr->serial_port.access_size = access_size;
467 spcr->serial_port.addrl = lower_32_bits(serial_address);
468 spcr->serial_port.addrh = upper_32_bits(serial_address);
469
470 /* Encode baud rate */
471 switch (serial_info.baudrate) {
472 case 9600:
473 spcr->baud_rate = 3;
474 break;
475 case 19200:
476 spcr->baud_rate = 4;
477 break;
478 case 57600:
479 spcr->baud_rate = 6;
480 break;
481 case 115200:
482 spcr->baud_rate = 7;
483 break;
484 default:
485 spcr->baud_rate = 0;
486 break;
487 }
488
489 serial_config = SERIAL_DEFAULT_CONFIG;
490 if (dev)
491 ret = serial_getconfig(dev, &serial_config);
492
493 spcr->parity = SERIAL_GET_PARITY(serial_config);
494 spcr->stop_bits = SERIAL_GET_STOP(serial_config);
495
496 /* No PCI devices for now */
497 spcr->pci_device_id = 0xffff;
498 spcr->pci_vendor_id = 0xffff;
499
500 /*
501 * SPCR has no clue if the UART base clock speed is different
502 * to the default one. However, the SPCR 1.04 defines baud rate
503 * 0 as a preconfigured state of UART and OS is supposed not
504 * to touch the configuration of the serial device.
505 */
506 if (serial_info.clock != SERIAL_DEFAULT_CLOCK)
507 spcr->baud_rate = 0;
508
509 /* Fix checksum */
510 header->checksum = table_compute_checksum((void *)spcr, header->length);
511
512 acpi_add_table(ctx, spcr);
513 acpi_inc(ctx, spcr->header.length);
514
515 return 0;
516}
517
518ACPI_WRITER(5spcr, "SPCR", acpi_write_spcr, 0);