blob: 2dc7b998e22454ed12ab7ed16d95dd12d8abed23 [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;
Patrick Rudolph7f91bca2024-10-23 15:19:53 +0200110 case ACPITAB_PPTT: /* ACPI 6.2: 1 */
111 return 1;
112 case ACPITAB_GTDT: /* ACPI 6.2: 2, ACPI 6.3: 3 */
113 return 2;
Simon Glass91fe8b72020-04-08 16:57:38 -0600114 default:
115 return -EINVAL;
116 }
117}
Simon Glass93f7f822020-04-26 09:19:46 -0600118
119void acpi_fill_header(struct acpi_table_header *header, char *signature)
120{
121 memcpy(header->signature, signature, 4);
122 memcpy(header->oem_id, OEM_ID, 6);
123 memcpy(header->oem_table_id, OEM_TABLE_ID, 8);
Pali Rohára3423b32021-07-10 13:10:01 +0200124 header->oem_revision = OEM_REVISION;
Heinrich Schuchardt4735d032024-01-21 12:52:48 +0100125 memcpy(header->creator_id, ASLC_ID, 4);
Heinrich Schuchardt07a6c692024-04-18 05:11:13 +0200126 header->creator_revision = ASL_REVISION;
Simon Glass93f7f822020-04-26 09:19:46 -0600127}
Simon Glass86e17782020-04-26 09:19:47 -0600128
129void acpi_align(struct acpi_ctx *ctx)
130{
131 ctx->current = (void *)ALIGN((ulong)ctx->current, 16);
132}
133
134void acpi_align64(struct acpi_ctx *ctx)
135{
136 ctx->current = (void *)ALIGN((ulong)ctx->current, 64);
137}
138
139void acpi_inc(struct acpi_ctx *ctx, uint amount)
140{
141 ctx->current += amount;
142}
143
144void acpi_inc_align(struct acpi_ctx *ctx, uint amount)
145{
146 ctx->current += amount;
147 acpi_align(ctx);
148}
Simon Glass29b35112020-04-26 09:19:50 -0600149
150/**
151 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
152 * and checksum.
153 */
154int acpi_add_table(struct acpi_ctx *ctx, void *table)
155{
156 int i, entries_num;
157 struct acpi_rsdt *rsdt;
158 struct acpi_xsdt *xsdt;
159
160 /* The RSDT is mandatory while the XSDT is not */
161 rsdt = ctx->rsdt;
162
163 /* This should always be MAX_ACPI_TABLES */
164 entries_num = ARRAY_SIZE(rsdt->entry);
165
166 for (i = 0; i < entries_num; i++) {
167 if (rsdt->entry[i] == 0)
168 break;
169 }
170
171 if (i >= entries_num) {
172 log_err("ACPI: Error: too many tables\n");
173 return -E2BIG;
174 }
175
176 /* Add table to the RSDT */
Simon Glassa8efebe2023-12-31 08:25:54 -0700177 rsdt->entry[i] = nomap_to_sysmem(table);
Simon Glass29b35112020-04-26 09:19:50 -0600178
179 /* Fix RSDT length or the kernel will assume invalid entries */
180 rsdt->header.length = sizeof(struct acpi_table_header) +
181 (sizeof(u32) * (i + 1));
182
183 /* Re-calculate checksum */
184 rsdt->header.checksum = 0;
185 rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
186 rsdt->header.length);
187
188 /*
189 * And now the same thing for the XSDT. We use the same index as for
190 * now we want the XSDT and RSDT to always be in sync in U-Boot
191 */
Simon Glassb38309b2020-04-26 09:19:52 -0600192 xsdt = ctx->xsdt;
Simon Glass29b35112020-04-26 09:19:50 -0600193
194 /* Add table to the XSDT */
Simon Glassa8efebe2023-12-31 08:25:54 -0700195 xsdt->entry[i] = nomap_to_sysmem(table);
Simon Glass29b35112020-04-26 09:19:50 -0600196
197 /* Fix XSDT length */
198 xsdt->header.length = sizeof(struct acpi_table_header) +
199 (sizeof(u64) * (i + 1));
200
201 /* Re-calculate checksum */
202 xsdt->header.checksum = 0;
203 xsdt->header.checksum = table_compute_checksum((u8 *)xsdt,
204 xsdt->header.length);
205
206 return 0;
207}
Simon Glass7e586f62020-04-26 09:19:51 -0600208
Maximilian Brunef5f79622024-10-23 15:19:45 +0200209int acpi_write_fadt(struct acpi_ctx *ctx, const struct acpi_writer *entry)
210{
211 struct acpi_table_header *header;
212 struct acpi_fadt *fadt;
213
214 fadt = ctx->current;
215 header = &fadt->header;
216
217 memset((void *)fadt, '\0', sizeof(struct acpi_fadt));
218
219 acpi_fill_header(header, "FACP");
220 header->length = sizeof(struct acpi_fadt);
221 header->revision = acpi_get_table_revision(ACPITAB_FADT);
222 memcpy(header->oem_id, OEM_ID, 6);
223 memcpy(header->oem_table_id, OEM_TABLE_ID, 8);
224 memcpy(header->creator_id, ASLC_ID, 4);
225 header->creator_revision = 1;
Patrick Rudolph4b882f62024-10-23 15:19:52 +0200226 fadt->minor_revision = 2;
Maximilian Brunef5f79622024-10-23 15:19:45 +0200227
228 fadt->x_firmware_ctrl = map_to_sysmem(ctx->facs);
229 fadt->x_dsdt = map_to_sysmem(ctx->dsdt);
230
231 if (fadt->x_firmware_ctrl < 0x100000000ULL)
232 fadt->firmware_ctrl = fadt->x_firmware_ctrl;
233
234 if (fadt->x_dsdt < 0x100000000ULL)
235 fadt->dsdt = fadt->x_dsdt;
236
237 fadt->preferred_pm_profile = ACPI_PM_UNSPECIFIED;
238
239 acpi_fill_fadt(fadt);
240
241 header->checksum = table_compute_checksum(fadt, header->length);
242
243 return acpi_add_fadt(ctx, fadt);
244}
245
246ACPI_WRITER(5fadt, "FADT", acpi_write_fadt, 0);
247
Patrick Rudolph4a3fc0f2024-10-23 15:19:46 +0200248int acpi_write_madt(struct acpi_ctx *ctx, const struct acpi_writer *entry)
249{
250 struct acpi_table_header *header;
251 struct acpi_madt *madt;
252 void *current;
253
254 madt = ctx->current;
255
256 memset(madt, '\0', sizeof(struct acpi_madt));
257 header = &madt->header;
258
259 /* Fill out header fields */
260 acpi_fill_header(header, "APIC");
261 header->length = sizeof(struct acpi_madt);
Patrick Rudolph4b882f62024-10-23 15:19:52 +0200262 header->revision = acpi_get_table_revision(ACPITAB_MADT);
Patrick Rudolph4a3fc0f2024-10-23 15:19:46 +0200263
264 acpi_inc(ctx, sizeof(struct acpi_madt));
Patrick Rudolph763bad32024-10-23 15:19:51 +0200265 /* TODO: Get rid of acpi_fill_madt and use driver model */
Patrick Rudolph4a3fc0f2024-10-23 15:19:46 +0200266 current = acpi_fill_madt(madt, ctx);
267
268 /* (Re)calculate length and checksum */
269 header->length = (uintptr_t)current - (uintptr_t)madt;
Patrick Rudolph4a3fc0f2024-10-23 15:19:46 +0200270 header->checksum = table_compute_checksum((void *)madt, header->length);
271 acpi_add_table(ctx, madt);
272 ctx->current = (void *)madt + madt->header.length;
273
274 return 0;
275}
276
277ACPI_WRITER(5madt, "MADT", acpi_write_madt, 0);
278
Simon Glassf37979e2020-09-22 12:45:10 -0600279void acpi_create_dbg2(struct acpi_dbg2_header *dbg2,
280 int port_type, int port_subtype,
281 struct acpi_gen_regaddr *address, u32 address_size,
282 const char *device_path)
283{
284 uintptr_t current;
285 struct acpi_dbg2_device *device;
286 u32 *dbg2_addr_size;
287 struct acpi_table_header *header;
288 size_t path_len;
289 const char *path;
290 char *namespace;
291
292 /* Fill out header fields. */
293 current = (uintptr_t)dbg2;
294 memset(dbg2, '\0', sizeof(struct acpi_dbg2_header));
295 header = &dbg2->header;
296
297 header->revision = acpi_get_table_revision(ACPITAB_DBG2);
298 acpi_fill_header(header, "DBG2");
Simon Glassf37979e2020-09-22 12:45:10 -0600299
300 /* One debug device defined */
301 dbg2->devices_offset = sizeof(struct acpi_dbg2_header);
302 dbg2->devices_count = 1;
303 current += sizeof(struct acpi_dbg2_header);
304
305 /* Device comes after the header */
306 device = (struct acpi_dbg2_device *)current;
307 memset(device, 0, sizeof(struct acpi_dbg2_device));
308 current += sizeof(struct acpi_dbg2_device);
309
310 device->revision = 0;
311 device->address_count = 1;
312 device->port_type = port_type;
313 device->port_subtype = port_subtype;
314
315 /* Base Address comes after device structure */
316 memcpy((void *)current, address, sizeof(struct acpi_gen_regaddr));
317 device->base_address_offset = current - (uintptr_t)device;
318 current += sizeof(struct acpi_gen_regaddr);
319
320 /* Address Size comes after address structure */
321 dbg2_addr_size = (uint32_t *)current;
322 device->address_size_offset = current - (uintptr_t)device;
323 *dbg2_addr_size = address_size;
324 current += sizeof(uint32_t);
325
326 /* Namespace string comes last, use '.' if not provided */
327 path = device_path ? : ".";
328 /* Namespace string length includes NULL terminator */
329 path_len = strlen(path) + 1;
330 namespace = (char *)current;
331 device->namespace_string_length = path_len;
332 device->namespace_string_offset = current - (uintptr_t)device;
333 strncpy(namespace, path, path_len);
334 current += path_len;
335
336 /* Update structure lengths and checksum */
337 device->length = current - (uintptr_t)device;
338 header->length = current - (uintptr_t)dbg2;
339 header->checksum = table_compute_checksum(dbg2, header->length);
340}
Maximilian Brune1c03efc2024-10-23 15:19:44 +0200341
342int acpi_write_dbg2_pci_uart(struct acpi_ctx *ctx, struct udevice *dev,
343 uint access_size)
344{
345 struct acpi_dbg2_header *dbg2 = ctx->current;
346 char path[ACPI_PATH_MAX];
347 struct acpi_gen_regaddr address;
348 u64 addr;
349 int ret;
350
351 if (!device_active(dev)) {
352 log_info("Device not enabled\n");
353 return -EACCES;
354 }
355 /*
356 * PCI devices don't remember their resource allocation information in
357 * U-Boot at present. We assume that MMIO is used for the UART and that
358 * the address space is 32 bytes: ns16550 uses 8 registers of up to
359 * 32-bits each. This is only for debugging so it is not a big deal.
360 */
361 addr = dm_pci_read_bar32(dev, 0);
362 log_debug("UART addr %lx\n", (ulong)addr);
363
364 ret = acpi_device_path(dev, path, sizeof(path));
365 if (ret)
366 return log_msg_ret("path", ret);
367
368 memset(&address, '\0', sizeof(address));
369 address.space_id = ACPI_ADDRESS_SPACE_MEMORY;
370 address.addrl = (uint32_t)addr;
371 address.addrh = (uint32_t)((addr >> 32) & 0xffffffff);
372 address.access_size = access_size;
373
374 ret = acpi_device_path(dev, path, sizeof(path));
375 if (ret)
376 return log_msg_ret("path", ret);
377 acpi_create_dbg2(dbg2, ACPI_DBG2_SERIAL_PORT,
378 ACPI_DBG2_16550_COMPATIBLE, &address, 0x1000, path);
379
380 acpi_inc_align(ctx, dbg2->header.length);
381 acpi_add_table(ctx, dbg2);
382
383 return 0;
384}
385
386static int acpi_write_spcr(struct acpi_ctx *ctx, const struct acpi_writer *entry)
387{
388 struct serial_device_info serial_info = {0};
389 ulong serial_address, serial_offset;
390 struct acpi_table_header *header;
391 struct acpi_spcr *spcr;
392 struct udevice *dev;
393 uint serial_config;
394 uint serial_width;
395 int access_size;
396 int space_id;
397 int ret = -ENODEV;
398
399 spcr = ctx->current;
400 header = &spcr->header;
401
402 memset(spcr, '\0', sizeof(struct acpi_spcr));
403
404 /* Fill out header fields */
405 acpi_fill_header(header, "SPCR");
406 header->length = sizeof(struct acpi_spcr);
407 header->revision = 2;
408
409 /* Read the device once, here. It is reused below */
410 dev = gd->cur_serial_dev;
411 if (dev)
412 ret = serial_getinfo(dev, &serial_info);
413 if (ret)
414 serial_info.type = SERIAL_CHIP_UNKNOWN;
415
416 /* Encode chip type */
417 switch (serial_info.type) {
418 case SERIAL_CHIP_16550_COMPATIBLE:
419 spcr->interface_type = ACPI_DBG2_16550_COMPATIBLE;
420 break;
421 case SERIAL_CHIP_PL01X:
422 spcr->interface_type = ACPI_DBG2_ARM_PL011;
423 break;
424 case SERIAL_CHIP_UNKNOWN:
425 default:
426 spcr->interface_type = ACPI_DBG2_UNKNOWN;
427 break;
428 }
429
430 /* Encode address space */
431 switch (serial_info.addr_space) {
432 case SERIAL_ADDRESS_SPACE_MEMORY:
433 space_id = ACPI_ADDRESS_SPACE_MEMORY;
434 break;
435 case SERIAL_ADDRESS_SPACE_IO:
436 default:
437 space_id = ACPI_ADDRESS_SPACE_IO;
438 break;
439 }
440
441 serial_width = serial_info.reg_width * 8;
442 serial_offset = serial_info.reg_offset << serial_info.reg_shift;
443 serial_address = serial_info.addr + serial_offset;
444
445 /* Encode register access size */
446 switch (serial_info.reg_shift) {
447 case 0:
448 access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS;
449 break;
450 case 1:
451 access_size = ACPI_ACCESS_SIZE_WORD_ACCESS;
452 break;
453 case 2:
454 access_size = ACPI_ACCESS_SIZE_DWORD_ACCESS;
455 break;
456 case 3:
457 access_size = ACPI_ACCESS_SIZE_QWORD_ACCESS;
458 break;
459 default:
460 access_size = ACPI_ACCESS_SIZE_UNDEFINED;
461 break;
462 }
463
464 debug("UART type %u @ %lx\n", spcr->interface_type, serial_address);
465
466 /* Fill GAS */
467 spcr->serial_port.space_id = space_id;
468 spcr->serial_port.bit_width = serial_width;
469 spcr->serial_port.bit_offset = 0;
470 spcr->serial_port.access_size = access_size;
471 spcr->serial_port.addrl = lower_32_bits(serial_address);
472 spcr->serial_port.addrh = upper_32_bits(serial_address);
473
474 /* Encode baud rate */
475 switch (serial_info.baudrate) {
476 case 9600:
477 spcr->baud_rate = 3;
478 break;
479 case 19200:
480 spcr->baud_rate = 4;
481 break;
482 case 57600:
483 spcr->baud_rate = 6;
484 break;
485 case 115200:
486 spcr->baud_rate = 7;
487 break;
488 default:
489 spcr->baud_rate = 0;
490 break;
491 }
492
493 serial_config = SERIAL_DEFAULT_CONFIG;
494 if (dev)
495 ret = serial_getconfig(dev, &serial_config);
496
497 spcr->parity = SERIAL_GET_PARITY(serial_config);
498 spcr->stop_bits = SERIAL_GET_STOP(serial_config);
499
500 /* No PCI devices for now */
501 spcr->pci_device_id = 0xffff;
502 spcr->pci_vendor_id = 0xffff;
503
504 /*
505 * SPCR has no clue if the UART base clock speed is different
506 * to the default one. However, the SPCR 1.04 defines baud rate
507 * 0 as a preconfigured state of UART and OS is supposed not
508 * to touch the configuration of the serial device.
509 */
510 if (serial_info.clock != SERIAL_DEFAULT_CLOCK)
511 spcr->baud_rate = 0;
512
513 /* Fix checksum */
514 header->checksum = table_compute_checksum((void *)spcr, header->length);
515
516 acpi_add_table(ctx, spcr);
517 acpi_inc(ctx, spcr->header.length);
518
519 return 0;
520}
521
522ACPI_WRITER(5spcr, "SPCR", acpi_write_spcr, 0);