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