blob: 43b7118213338037191cefa6391fd86d7c332608 [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
Patrick Rudolph158efd62024-10-23 15:19:57 +02008#include <bloblist.h>
Simon Glasse9629892020-04-08 16:57:39 -06009#include <cpu.h>
Patrick Rudolph158efd62024-10-23 15:19:57 +020010#include <dm.h>
11#include <efi_api.h>
12#include <efi_loader.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glass575a5472020-04-26 09:19:50 -060014#include <mapmem.h>
15#include <tables_csum.h>
Maximilian Bruned7fa54b2024-10-23 15:19:44 +020016#include <serial.h>
Simon Glass90b01272023-04-29 19:21:46 -060017#include <version_string.h>
Simon Glass0e113842020-04-26 09:19:47 -060018#include <acpi/acpi_table.h>
Maximilian Bruned7fa54b2024-10-23 15:19:44 +020019#include <acpi/acpi_device.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060020#include <asm/global_data.h>
Simon Glass0e113842020-04-26 09:19:47 -060021#include <dm/acpi.h>
Patrick Rudolph158efd62024-10-23 15:19:57 +020022#include <linux/sizes.h>
23#include <linux/log2.h>
24
25enum {
26 TABLE_SIZE = SZ_64K,
27};
28
29DECLARE_GLOBAL_DATA_PTR;
Simon Glasse9629892020-04-08 16:57:39 -060030
Pali Rohárf0de20e2021-07-10 13:10:01 +020031/*
32 * OEM_REVISION is 32-bit unsigned number. It should be increased only when
33 * changing software version. Therefore it should not depend on build time.
34 * U-Boot calculates it from U-Boot version and represent it in hexadecimal
35 * notation. As U-Boot version is in form year.month set low 8 bits to 0x01
36 * to have valid date. So for U-Boot version 2021.04 OEM_REVISION is set to
37 * value 0x20210401.
38 */
Simon Glass90b01272023-04-29 19:21:46 -060039#define OEM_REVISION ((((version_num / 1000) % 10) << 28) | \
40 (((version_num / 100) % 10) << 24) | \
41 (((version_num / 10) % 10) << 20) | \
42 ((version_num % 10) << 16) | \
43 (((version_num_patch / 10) % 10) << 12) | \
44 ((version_num_patch % 10) << 8) | \
Pali Rohárf0de20e2021-07-10 13:10:01 +020045 0x01)
46
Simon Glasse9629892020-04-08 16:57:39 -060047int acpi_create_dmar(struct acpi_dmar *dmar, enum dmar_flags flags)
48{
49 struct acpi_table_header *header = &dmar->header;
50 struct cpu_info info;
51 struct udevice *cpu;
52 int ret;
53
Michal Suchanekac12a2f2022-10-12 21:57:59 +020054 ret = uclass_first_device_err(UCLASS_CPU, &cpu);
Simon Glasse9629892020-04-08 16:57:39 -060055 if (ret)
56 return log_msg_ret("cpu", ret);
57 ret = cpu_get_info(cpu, &info);
58 if (ret)
59 return log_msg_ret("info", ret);
60 memset((void *)dmar, 0, sizeof(struct acpi_dmar));
61
62 /* Fill out header fields. */
63 acpi_fill_header(&dmar->header, "DMAR");
64 header->length = sizeof(struct acpi_dmar);
65 header->revision = acpi_get_table_revision(ACPITAB_DMAR);
66
67 dmar->host_address_width = info.address_width - 1;
68 dmar->flags = flags;
Simon Glass7b9e8f42025-03-15 14:26:03 +000069 header->checksum = table_compute_checksum(dmar, header->length);
Simon Glasse9629892020-04-08 16:57:39 -060070
71 return 0;
72}
Simon Glassb2672ea2020-04-08 16:57:38 -060073
74int acpi_get_table_revision(enum acpi_tables table)
75{
76 switch (table) {
77 case ACPITAB_FADT:
Patrick Rudolphf317fce2024-10-23 15:19:52 +020078 return ACPI_FADT_REV_ACPI_6_0;
Simon Glassb2672ea2020-04-08 16:57:38 -060079 case ACPITAB_MADT:
Patrick Rudolphf317fce2024-10-23 15:19:52 +020080 return ACPI_MADT_REV_ACPI_6_2;
Simon Glassb2672ea2020-04-08 16:57:38 -060081 case ACPITAB_MCFG:
82 return ACPI_MCFG_REV_ACPI_3_0;
83 case ACPITAB_TCPA:
84 /* This version and the rest are open-coded */
85 return 2;
86 case ACPITAB_TPM2:
87 return 4;
88 case ACPITAB_SSDT: /* ACPI 3.0 upto 6.3: 2 */
89 return 2;
90 case ACPITAB_SRAT: /* ACPI 2.0: 1, ACPI 3.0: 2, ACPI 4.0 to 6.3: 3 */
91 return 1; /* TODO Should probably be upgraded to 2 */
92 case ACPITAB_DMAR:
93 return 1;
94 case ACPITAB_SLIT: /* ACPI 2.0 upto 6.3: 1 */
95 return 1;
96 case ACPITAB_SPMI: /* IMPI 2.0 */
97 return 5;
98 case ACPITAB_HPET: /* Currently 1. Table added in ACPI 2.0 */
99 return 1;
100 case ACPITAB_VFCT: /* ACPI 2.0/3.0/4.0: 1 */
101 return 1;
102 case ACPITAB_IVRS:
103 return IVRS_FORMAT_FIXED;
104 case ACPITAB_DBG2:
105 return 0;
106 case ACPITAB_FACS: /* ACPI 2.0/3.0: 1, ACPI 4.0 to 6.3: 2 */
107 return 1;
108 case ACPITAB_RSDT: /* ACPI 1.0 upto 6.3: 1 */
109 return 1;
110 case ACPITAB_XSDT: /* ACPI 2.0 upto 6.3: 1 */
111 return 1;
112 case ACPITAB_RSDP: /* ACPI 2.0 upto 6.3: 2 */
113 return 2;
114 case ACPITAB_HEST:
115 return 1;
116 case ACPITAB_NHLT:
117 return 5;
118 case ACPITAB_BERT:
119 return 1;
120 case ACPITAB_SPCR:
121 return 2;
Patrick Rudolph4d3cf1a2024-10-23 15:19:53 +0200122 case ACPITAB_PPTT: /* ACPI 6.2: 1 */
123 return 1;
124 case ACPITAB_GTDT: /* ACPI 6.2: 2, ACPI 6.3: 3 */
125 return 2;
Simon Glassb2672ea2020-04-08 16:57:38 -0600126 default:
127 return -EINVAL;
128 }
129}
Simon Glass17968c32020-04-26 09:19:46 -0600130
131void acpi_fill_header(struct acpi_table_header *header, char *signature)
132{
133 memcpy(header->signature, signature, 4);
134 memcpy(header->oem_id, OEM_ID, 6);
135 memcpy(header->oem_table_id, OEM_TABLE_ID, 8);
Pali Rohárf0de20e2021-07-10 13:10:01 +0200136 header->oem_revision = OEM_REVISION;
Heinrich Schuchardt10de8a82024-01-21 12:52:48 +0100137 memcpy(header->creator_id, ASLC_ID, 4);
Heinrich Schuchardt9838d342024-04-18 05:11:13 +0200138 header->creator_revision = ASL_REVISION;
Simon Glass17968c32020-04-26 09:19:46 -0600139}
Simon Glass0e113842020-04-26 09:19:47 -0600140
141void acpi_align(struct acpi_ctx *ctx)
142{
143 ctx->current = (void *)ALIGN((ulong)ctx->current, 16);
144}
145
146void acpi_align64(struct acpi_ctx *ctx)
147{
148 ctx->current = (void *)ALIGN((ulong)ctx->current, 64);
149}
150
151void acpi_inc(struct acpi_ctx *ctx, uint amount)
152{
153 ctx->current += amount;
154}
155
156void acpi_inc_align(struct acpi_ctx *ctx, uint amount)
157{
158 ctx->current += amount;
159 acpi_align(ctx);
160}
Simon Glass575a5472020-04-26 09:19:50 -0600161
162/**
163 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
164 * and checksum.
165 */
166int acpi_add_table(struct acpi_ctx *ctx, void *table)
167{
168 int i, entries_num;
169 struct acpi_rsdt *rsdt;
170 struct acpi_xsdt *xsdt;
171
Patrick Rudolphd69eb292024-10-23 15:19:56 +0200172 /* On legacy x86 platforms the RSDT is mandatory while the XSDT is not.
173 * On other platforms there might be no memory below 4GiB, thus RSDT is NULL.
174 */
175 if (ctx->rsdt) {
176 rsdt = ctx->rsdt;
Simon Glass575a5472020-04-26 09:19:50 -0600177
Patrick Rudolphd69eb292024-10-23 15:19:56 +0200178 /* This should always be MAX_ACPI_TABLES */
179 entries_num = ARRAY_SIZE(rsdt->entry);
Simon Glass575a5472020-04-26 09:19:50 -0600180
Patrick Rudolphd69eb292024-10-23 15:19:56 +0200181 for (i = 0; i < entries_num; i++) {
182 if (rsdt->entry[i] == 0)
183 break;
184 }
Simon Glass575a5472020-04-26 09:19:50 -0600185
Patrick Rudolphd69eb292024-10-23 15:19:56 +0200186 if (i >= entries_num) {
187 log_err("ACPI: Error: too many tables\n");
188 return -E2BIG;
189 }
190
191 /* Add table to the RSDT */
192 rsdt->entry[i] = nomap_to_sysmem(table);
193
194 /* Fix RSDT length or the kernel will assume invalid entries */
195 rsdt->header.length = sizeof(struct acpi_table_header) +
196 (sizeof(u32) * (i + 1));
197
198 /* Re-calculate checksum */
Heinrich Schuchardt7b332b52025-03-22 00:21:17 +0100199 acpi_update_checksum(&rsdt->header);
Simon Glass575a5472020-04-26 09:19:50 -0600200 }
201
Patrick Rudolphd69eb292024-10-23 15:19:56 +0200202 if (ctx->xsdt) {
203 /*
204 * And now the same thing for the XSDT. We use the same index as for
205 * now we want the XSDT and RSDT to always be in sync in U-Boot
206 */
207 xsdt = ctx->xsdt;
Simon Glass575a5472020-04-26 09:19:50 -0600208
Patrick Rudolphd69eb292024-10-23 15:19:56 +0200209 /* This should always be MAX_ACPI_TABLES */
210 entries_num = ARRAY_SIZE(xsdt->entry);
Simon Glass575a5472020-04-26 09:19:50 -0600211
Patrick Rudolphd69eb292024-10-23 15:19:56 +0200212 for (i = 0; i < entries_num; i++) {
213 if (xsdt->entry[i] == 0)
214 break;
215 }
Simon Glass575a5472020-04-26 09:19:50 -0600216
Patrick Rudolphd69eb292024-10-23 15:19:56 +0200217 if (i >= entries_num) {
218 log_err("ACPI: Error: too many tables\n");
219 return -E2BIG;
220 }
Simon Glass575a5472020-04-26 09:19:50 -0600221
Patrick Rudolphd69eb292024-10-23 15:19:56 +0200222 /* Add table to the XSDT */
223 xsdt->entry[i] = nomap_to_sysmem(table);
Simon Glass575a5472020-04-26 09:19:50 -0600224
Patrick Rudolphd69eb292024-10-23 15:19:56 +0200225 /* Fix XSDT length */
226 xsdt->header.length = sizeof(struct acpi_table_header) +
227 (sizeof(u64) * (i + 1));
Simon Glass575a5472020-04-26 09:19:50 -0600228
Patrick Rudolphd69eb292024-10-23 15:19:56 +0200229 /* Re-calculate checksum */
Heinrich Schuchardt7b332b52025-03-22 00:21:17 +0100230 acpi_update_checksum(&xsdt->header);
Patrick Rudolphd69eb292024-10-23 15:19:56 +0200231 }
Simon Glass575a5472020-04-26 09:19:50 -0600232
233 return 0;
234}
Simon Glass9c442a62020-04-26 09:19:51 -0600235
Maximilian Brune8dc45122024-10-23 15:19:45 +0200236int acpi_write_fadt(struct acpi_ctx *ctx, const struct acpi_writer *entry)
237{
238 struct acpi_table_header *header;
239 struct acpi_fadt *fadt;
240
241 fadt = ctx->current;
242 header = &fadt->header;
243
244 memset((void *)fadt, '\0', sizeof(struct acpi_fadt));
245
246 acpi_fill_header(header, "FACP");
247 header->length = sizeof(struct acpi_fadt);
248 header->revision = acpi_get_table_revision(ACPITAB_FADT);
249 memcpy(header->oem_id, OEM_ID, 6);
250 memcpy(header->oem_table_id, OEM_TABLE_ID, 8);
251 memcpy(header->creator_id, ASLC_ID, 4);
252 header->creator_revision = 1;
Patrick Rudolphf317fce2024-10-23 15:19:52 +0200253 fadt->minor_revision = 2;
Maximilian Brune8dc45122024-10-23 15:19:45 +0200254
Simon Glass4cd81bc2025-03-15 14:26:02 +0000255 fadt->x_firmware_ctrl = nomap_to_sysmem(ctx->facs);
256 fadt->x_dsdt = nomap_to_sysmem(ctx->dsdt);
Maximilian Brune8dc45122024-10-23 15:19:45 +0200257
258 if (fadt->x_firmware_ctrl < 0x100000000ULL)
259 fadt->firmware_ctrl = fadt->x_firmware_ctrl;
260
261 if (fadt->x_dsdt < 0x100000000ULL)
262 fadt->dsdt = fadt->x_dsdt;
263
264 fadt->preferred_pm_profile = ACPI_PM_UNSPECIFIED;
265
266 acpi_fill_fadt(fadt);
267
Heinrich Schuchardt7b332b52025-03-22 00:21:17 +0100268 acpi_update_checksum(header);
Maximilian Brune8dc45122024-10-23 15:19:45 +0200269
270 return acpi_add_fadt(ctx, fadt);
271}
272
Heinrich Schuchardt08839712024-12-20 01:37:59 +0100273#ifndef CONFIG_QFW_ACPI
Maximilian Brune8dc45122024-10-23 15:19:45 +0200274ACPI_WRITER(5fadt, "FADT", acpi_write_fadt, 0);
Heinrich Schuchardt08839712024-12-20 01:37:59 +0100275#endif
Maximilian Brune8dc45122024-10-23 15:19:45 +0200276
Patrick Rudolph97b4c8a2024-10-23 15:19:46 +0200277int acpi_write_madt(struct acpi_ctx *ctx, const struct acpi_writer *entry)
278{
279 struct acpi_table_header *header;
280 struct acpi_madt *madt;
281 void *current;
282
283 madt = ctx->current;
284
285 memset(madt, '\0', sizeof(struct acpi_madt));
286 header = &madt->header;
287
288 /* Fill out header fields */
289 acpi_fill_header(header, "APIC");
290 header->length = sizeof(struct acpi_madt);
Patrick Rudolphf317fce2024-10-23 15:19:52 +0200291 header->revision = acpi_get_table_revision(ACPITAB_MADT);
Patrick Rudolph97b4c8a2024-10-23 15:19:46 +0200292
293 acpi_inc(ctx, sizeof(struct acpi_madt));
Patrick Rudolphbff7c572024-10-23 15:19:51 +0200294 /* TODO: Get rid of acpi_fill_madt and use driver model */
Patrick Rudolph97b4c8a2024-10-23 15:19:46 +0200295 current = acpi_fill_madt(madt, ctx);
296
297 /* (Re)calculate length and checksum */
298 header->length = (uintptr_t)current - (uintptr_t)madt;
Patrick Rudolph2f6f8d92024-10-23 15:20:13 +0200299
300 if (IS_ENABLED(CONFIG_ACPI_PARKING_PROTOCOL))
301 acpi_write_park(madt);
302
Heinrich Schuchardt7b332b52025-03-22 00:21:17 +0100303 acpi_update_checksum(header);
Patrick Rudolph97b4c8a2024-10-23 15:19:46 +0200304 acpi_add_table(ctx, madt);
305 ctx->current = (void *)madt + madt->header.length;
306
307 return 0;
308}
309
Heinrich Schuchardt08839712024-12-20 01:37:59 +0100310#ifndef CONFIG_QFW_ACPI
Patrick Rudolph97b4c8a2024-10-23 15:19:46 +0200311ACPI_WRITER(5madt, "MADT", acpi_write_madt, 0);
Heinrich Schuchardt08839712024-12-20 01:37:59 +0100312#endif
Patrick Rudolph97b4c8a2024-10-23 15:19:46 +0200313
Simon Glass95971892020-09-22 12:45:10 -0600314void acpi_create_dbg2(struct acpi_dbg2_header *dbg2,
315 int port_type, int port_subtype,
316 struct acpi_gen_regaddr *address, u32 address_size,
317 const char *device_path)
318{
319 uintptr_t current;
320 struct acpi_dbg2_device *device;
321 u32 *dbg2_addr_size;
322 struct acpi_table_header *header;
323 size_t path_len;
324 const char *path;
325 char *namespace;
326
327 /* Fill out header fields. */
328 current = (uintptr_t)dbg2;
329 memset(dbg2, '\0', sizeof(struct acpi_dbg2_header));
330 header = &dbg2->header;
331
332 header->revision = acpi_get_table_revision(ACPITAB_DBG2);
333 acpi_fill_header(header, "DBG2");
Simon Glass95971892020-09-22 12:45:10 -0600334
335 /* One debug device defined */
336 dbg2->devices_offset = sizeof(struct acpi_dbg2_header);
337 dbg2->devices_count = 1;
338 current += sizeof(struct acpi_dbg2_header);
339
340 /* Device comes after the header */
341 device = (struct acpi_dbg2_device *)current;
342 memset(device, 0, sizeof(struct acpi_dbg2_device));
343 current += sizeof(struct acpi_dbg2_device);
344
345 device->revision = 0;
346 device->address_count = 1;
347 device->port_type = port_type;
348 device->port_subtype = port_subtype;
349
350 /* Base Address comes after device structure */
351 memcpy((void *)current, address, sizeof(struct acpi_gen_regaddr));
352 device->base_address_offset = current - (uintptr_t)device;
353 current += sizeof(struct acpi_gen_regaddr);
354
355 /* Address Size comes after address structure */
356 dbg2_addr_size = (uint32_t *)current;
357 device->address_size_offset = current - (uintptr_t)device;
358 *dbg2_addr_size = address_size;
359 current += sizeof(uint32_t);
360
361 /* Namespace string comes last, use '.' if not provided */
362 path = device_path ? : ".";
363 /* Namespace string length includes NULL terminator */
364 path_len = strlen(path) + 1;
365 namespace = (char *)current;
366 device->namespace_string_length = path_len;
367 device->namespace_string_offset = current - (uintptr_t)device;
368 strncpy(namespace, path, path_len);
369 current += path_len;
370
371 /* Update structure lengths and checksum */
372 device->length = current - (uintptr_t)device;
373 header->length = current - (uintptr_t)dbg2;
Heinrich Schuchardt7b332b52025-03-22 00:21:17 +0100374 acpi_update_checksum(header);
Simon Glass95971892020-09-22 12:45:10 -0600375}
Maximilian Bruned7fa54b2024-10-23 15:19:44 +0200376
377int acpi_write_dbg2_pci_uart(struct acpi_ctx *ctx, struct udevice *dev,
378 uint access_size)
379{
380 struct acpi_dbg2_header *dbg2 = ctx->current;
381 char path[ACPI_PATH_MAX];
382 struct acpi_gen_regaddr address;
383 u64 addr;
384 int ret;
385
386 if (!device_active(dev)) {
387 log_info("Device not enabled\n");
388 return -EACCES;
389 }
390 /*
391 * PCI devices don't remember their resource allocation information in
392 * U-Boot at present. We assume that MMIO is used for the UART and that
393 * the address space is 32 bytes: ns16550 uses 8 registers of up to
394 * 32-bits each. This is only for debugging so it is not a big deal.
395 */
396 addr = dm_pci_read_bar32(dev, 0);
397 log_debug("UART addr %lx\n", (ulong)addr);
398
399 ret = acpi_device_path(dev, path, sizeof(path));
400 if (ret)
401 return log_msg_ret("path", ret);
402
403 memset(&address, '\0', sizeof(address));
404 address.space_id = ACPI_ADDRESS_SPACE_MEMORY;
405 address.addrl = (uint32_t)addr;
406 address.addrh = (uint32_t)((addr >> 32) & 0xffffffff);
407 address.access_size = access_size;
408
409 ret = acpi_device_path(dev, path, sizeof(path));
410 if (ret)
411 return log_msg_ret("path", ret);
412 acpi_create_dbg2(dbg2, ACPI_DBG2_SERIAL_PORT,
413 ACPI_DBG2_16550_COMPATIBLE, &address, 0x1000, path);
414
415 acpi_inc_align(ctx, dbg2->header.length);
416 acpi_add_table(ctx, dbg2);
417
418 return 0;
419}
420
421static int acpi_write_spcr(struct acpi_ctx *ctx, const struct acpi_writer *entry)
422{
423 struct serial_device_info serial_info = {0};
Patrick Rudolph1bf4cb22024-10-30 14:11:46 +0100424 u64 serial_address, serial_offset;
Maximilian Bruned7fa54b2024-10-23 15:19:44 +0200425 struct acpi_table_header *header;
426 struct acpi_spcr *spcr;
427 struct udevice *dev;
428 uint serial_config;
429 uint serial_width;
430 int access_size;
431 int space_id;
432 int ret = -ENODEV;
433
434 spcr = ctx->current;
435 header = &spcr->header;
436
437 memset(spcr, '\0', sizeof(struct acpi_spcr));
438
439 /* Fill out header fields */
440 acpi_fill_header(header, "SPCR");
441 header->length = sizeof(struct acpi_spcr);
442 header->revision = 2;
443
444 /* Read the device once, here. It is reused below */
445 dev = gd->cur_serial_dev;
446 if (dev)
447 ret = serial_getinfo(dev, &serial_info);
448 if (ret)
449 serial_info.type = SERIAL_CHIP_UNKNOWN;
450
451 /* Encode chip type */
452 switch (serial_info.type) {
453 case SERIAL_CHIP_16550_COMPATIBLE:
454 spcr->interface_type = ACPI_DBG2_16550_COMPATIBLE;
455 break;
456 case SERIAL_CHIP_PL01X:
457 spcr->interface_type = ACPI_DBG2_ARM_PL011;
458 break;
459 case SERIAL_CHIP_UNKNOWN:
460 default:
461 spcr->interface_type = ACPI_DBG2_UNKNOWN;
462 break;
463 }
464
465 /* Encode address space */
466 switch (serial_info.addr_space) {
467 case SERIAL_ADDRESS_SPACE_MEMORY:
468 space_id = ACPI_ADDRESS_SPACE_MEMORY;
469 break;
470 case SERIAL_ADDRESS_SPACE_IO:
471 default:
472 space_id = ACPI_ADDRESS_SPACE_IO;
473 break;
474 }
475
476 serial_width = serial_info.reg_width * 8;
Patrick Rudolph1bf4cb22024-10-30 14:11:46 +0100477 serial_offset = ((u64)serial_info.reg_offset) << serial_info.reg_shift;
Maximilian Bruned7fa54b2024-10-23 15:19:44 +0200478 serial_address = serial_info.addr + serial_offset;
479
480 /* Encode register access size */
481 switch (serial_info.reg_shift) {
482 case 0:
483 access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS;
484 break;
485 case 1:
486 access_size = ACPI_ACCESS_SIZE_WORD_ACCESS;
487 break;
488 case 2:
489 access_size = ACPI_ACCESS_SIZE_DWORD_ACCESS;
490 break;
491 case 3:
492 access_size = ACPI_ACCESS_SIZE_QWORD_ACCESS;
493 break;
494 default:
495 access_size = ACPI_ACCESS_SIZE_UNDEFINED;
496 break;
497 }
498
Patrick Rudolph1bf4cb22024-10-30 14:11:46 +0100499 debug("UART type %u @ %llx\n", spcr->interface_type, serial_address);
Maximilian Bruned7fa54b2024-10-23 15:19:44 +0200500
501 /* Fill GAS */
502 spcr->serial_port.space_id = space_id;
503 spcr->serial_port.bit_width = serial_width;
504 spcr->serial_port.bit_offset = 0;
505 spcr->serial_port.access_size = access_size;
506 spcr->serial_port.addrl = lower_32_bits(serial_address);
507 spcr->serial_port.addrh = upper_32_bits(serial_address);
508
509 /* Encode baud rate */
510 switch (serial_info.baudrate) {
511 case 9600:
512 spcr->baud_rate = 3;
513 break;
514 case 19200:
515 spcr->baud_rate = 4;
516 break;
517 case 57600:
518 spcr->baud_rate = 6;
519 break;
520 case 115200:
521 spcr->baud_rate = 7;
522 break;
523 default:
524 spcr->baud_rate = 0;
525 break;
526 }
527
528 serial_config = SERIAL_DEFAULT_CONFIG;
529 if (dev)
530 ret = serial_getconfig(dev, &serial_config);
531
532 spcr->parity = SERIAL_GET_PARITY(serial_config);
533 spcr->stop_bits = SERIAL_GET_STOP(serial_config);
534
535 /* No PCI devices for now */
536 spcr->pci_device_id = 0xffff;
537 spcr->pci_vendor_id = 0xffff;
538
539 /*
540 * SPCR has no clue if the UART base clock speed is different
541 * to the default one. However, the SPCR 1.04 defines baud rate
542 * 0 as a preconfigured state of UART and OS is supposed not
543 * to touch the configuration of the serial device.
544 */
545 if (serial_info.clock != SERIAL_DEFAULT_CLOCK)
546 spcr->baud_rate = 0;
547
548 /* Fix checksum */
Heinrich Schuchardt7b332b52025-03-22 00:21:17 +0100549 acpi_update_checksum(header);
Maximilian Bruned7fa54b2024-10-23 15:19:44 +0200550
551 acpi_add_table(ctx, spcr);
552 acpi_inc(ctx, spcr->header.length);
553
554 return 0;
555}
556
557ACPI_WRITER(5spcr, "SPCR", acpi_write_spcr, 0);
Patrick Rudolph1669ce72024-10-23 15:19:54 +0200558
559__weak int acpi_fill_iort(struct acpi_ctx *ctx)
560{
561 return 0;
562}
563
564int acpi_iort_add_its_group(struct acpi_ctx *ctx,
565 const u32 its_count,
566 const u32 *identifiers)
567{
568 struct acpi_iort_node *node;
569 struct acpi_iort_its_group *group;
570 int offset;
571
572 offset = ctx->current - ctx->tab_start;
573
574 node = ctx->current;
575 memset(node, '\0', sizeof(struct acpi_iort_node));
576
577 node->type = ACPI_IORT_NODE_ITS_GROUP;
578 node->revision = 1;
579
580 node->length = sizeof(struct acpi_iort_node);
581 node->length += sizeof(struct acpi_iort_its_group);
582 node->length += sizeof(u32) * its_count;
583
584 group = (struct acpi_iort_its_group *)node->node_data;
585 group->its_count = its_count;
586 memcpy(&group->identifiers, identifiers, sizeof(u32) * its_count);
587
588 ctx->current += node->length;
589
590 return offset;
591}
592
593int acpi_iort_add_named_component(struct acpi_ctx *ctx,
594 const u32 node_flags,
595 const u64 memory_properties,
596 const u8 memory_address_limit,
597 const char *device_name)
598{
599 struct acpi_iort_node *node;
600 struct acpi_iort_named_component *comp;
601 int offset;
602
603 offset = ctx->current - ctx->tab_start;
604
605 node = ctx->current;
606 memset(node, '\0', sizeof(struct acpi_iort_node));
607
608 node->type = ACPI_IORT_NODE_NAMED_COMPONENT;
609 node->revision = 4;
610 node->length = sizeof(struct acpi_iort_node);
611 node->length += sizeof(struct acpi_iort_named_component);
612 node->length += strlen(device_name) + 1;
613
614 comp = (struct acpi_iort_named_component *)node->node_data;
Patrick Rudolph0eafaf22025-03-16 09:32:54 +0100615 memset(comp, '\0', sizeof(struct acpi_iort_named_component));
Patrick Rudolph1669ce72024-10-23 15:19:54 +0200616
617 comp->node_flags = node_flags;
618 comp->memory_properties = memory_properties;
619 comp->memory_address_limit = memory_address_limit;
620 memcpy(comp->device_name, device_name, strlen(device_name) + 1);
621
622 ctx->current += node->length;
623
624 return offset;
625}
626
627int acpi_iort_add_rc(struct acpi_ctx *ctx,
628 const u64 mem_access_properties,
629 const u32 ats_attributes,
630 const u32 pci_segment_number,
631 const u8 memory_address_size_limit,
632 const int num_mappings,
633 const struct acpi_iort_id_mapping *map)
634{
635 struct acpi_iort_id_mapping *mapping;
Patrick Rudolph9b6b3882025-03-16 09:32:53 +0100636 struct acpi_iort_node *output_node;
Patrick Rudolph1669ce72024-10-23 15:19:54 +0200637 struct acpi_iort_node *node;
638 struct acpi_iort_rc *rc;
639 int offset;
640
641 offset = ctx->current - ctx->tab_start;
642
643 node = ctx->current;
644 memset(node, '\0', sizeof(struct acpi_iort_node));
645
646 node->type = ACPI_IORT_NODE_PCI_ROOT_COMPLEX;
647 node->revision = 2;
Patrick Rudolphfcfbecb2025-03-16 09:32:52 +0100648 node->mapping_count = num_mappings;
Patrick Rudolph15b48eb2025-03-16 09:32:55 +0100649 if (num_mappings)
650 node->mapping_offset = sizeof(struct acpi_iort_node) +
651 sizeof(struct acpi_iort_rc);
Patrick Rudolph1669ce72024-10-23 15:19:54 +0200652
653 node->length = sizeof(struct acpi_iort_node);
654 node->length += sizeof(struct acpi_iort_rc);
655 node->length += sizeof(struct acpi_iort_id_mapping) * num_mappings;
656
657 rc = (struct acpi_iort_rc *)node->node_data;
Patrick Rudolph0eafaf22025-03-16 09:32:54 +0100658 memset(rc, '\0', sizeof(struct acpi_iort_rc));
659
Patrick Rudolph1669ce72024-10-23 15:19:54 +0200660 rc->mem_access_properties = mem_access_properties;
661 rc->ats_attributes = ats_attributes;
662 rc->pci_segment_number = pci_segment_number;
663 rc->memory_address_size_limit = memory_address_size_limit;
664
665 mapping = (struct acpi_iort_id_mapping *)(rc + 1);
666 for (int i = 0; i < num_mappings; i++) {
Patrick Rudolph9b6b3882025-03-16 09:32:53 +0100667 /* Validate input */
668 output_node = (struct acpi_iort_node *)ctx->tab_start + map[i].output_reference;
669 /* ID mappings can use SMMUs or ITS groups as output references */
670 assert(output_node && ((output_node->type == ACPI_IORT_NODE_ITS_GROUP) ||
671 (output_node->type == ACPI_IORT_NODE_SMMU) ||
672 (output_node->type == ACPI_IORT_NODE_SMMU_V3)));
673
Patrick Rudolph1669ce72024-10-23 15:19:54 +0200674 memcpy(mapping, &map[i], sizeof(struct acpi_iort_id_mapping));
675 mapping++;
676 }
677
678 ctx->current += node->length;
679
680 return offset;
681}
682
683int acpi_iort_add_smmu_v3(struct acpi_ctx *ctx,
684 const u64 base_address,
685 const u32 flags,
686 const u64 vatos_address,
687 const u32 model,
688 const u32 event_gsiv,
689 const u32 pri_gsiv,
690 const u32 gerr_gsiv,
691 const u32 sync_gsiv,
692 const u32 pxm,
693 const u32 id_mapping_index,
694 const int num_mappings,
695 const struct acpi_iort_id_mapping *map)
696{
697 struct acpi_iort_node *node;
Patrick Rudolph9b6b3882025-03-16 09:32:53 +0100698 struct acpi_iort_node *output_node;
Patrick Rudolph1669ce72024-10-23 15:19:54 +0200699 struct acpi_iort_smmu_v3 *smmu;
700 struct acpi_iort_id_mapping *mapping;
701 int offset;
702
703 offset = ctx->current - ctx->tab_start;
704
705 node = ctx->current;
706 memset(node, '\0', sizeof(struct acpi_iort_node));
707
708 node->type = ACPI_IORT_NODE_SMMU_V3;
709 node->revision = 5;
710 node->mapping_count = num_mappings;
Patrick Rudolph15b48eb2025-03-16 09:32:55 +0100711 if (num_mappings)
712 node->mapping_offset = sizeof(struct acpi_iort_node) +
713 sizeof(struct acpi_iort_smmu_v3);
Patrick Rudolph1669ce72024-10-23 15:19:54 +0200714
715 node->length = sizeof(struct acpi_iort_node);
716 node->length += sizeof(struct acpi_iort_smmu_v3);
717 node->length += sizeof(struct acpi_iort_id_mapping) * num_mappings;
718
719 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
Patrick Rudolph0eafaf22025-03-16 09:32:54 +0100720 memset(smmu, '\0', sizeof(struct acpi_iort_smmu_v3));
Patrick Rudolph1669ce72024-10-23 15:19:54 +0200721
722 smmu->base_address = base_address;
723 smmu->flags = flags;
724 smmu->vatos_address = vatos_address;
725 smmu->model = model;
726 smmu->event_gsiv = event_gsiv;
727 smmu->pri_gsiv = pri_gsiv;
728 smmu->gerr_gsiv = gerr_gsiv;
729 smmu->sync_gsiv = sync_gsiv;
730 smmu->pxm = pxm;
731 smmu->id_mapping_index = id_mapping_index;
732
733 mapping = (struct acpi_iort_id_mapping *)(smmu + 1);
734 for (int i = 0; i < num_mappings; i++) {
Patrick Rudolph9b6b3882025-03-16 09:32:53 +0100735 /* Validate input */
736 output_node = (struct acpi_iort_node *)ctx->tab_start + map[i].output_reference;
737 /*
738 * ID mappings of an SMMUv3 node can only have ITS group nodes
739 * as output references.
740 */
741 assert(output_node && output_node->type == ACPI_IORT_NODE_ITS_GROUP);
742
Patrick Rudolph1669ce72024-10-23 15:19:54 +0200743 memcpy(mapping, &map[i], sizeof(struct acpi_iort_id_mapping));
744 mapping++;
745 }
746
747 ctx->current += node->length;
748
749 return offset;
750}
751
752static int acpi_write_iort(struct acpi_ctx *ctx, const struct acpi_writer *entry)
753{
754 struct acpi_table_iort *iort;
755 struct acpi_iort_node *node;
756 u32 offset;
757 int ret;
758
759 iort = ctx->current;
760 ctx->tab_start = ctx->current;
761 memset(iort, '\0', sizeof(struct acpi_table_iort));
762
763 acpi_fill_header(&iort->header, "IORT");
764 iort->header.revision = 1;
765 iort->header.creator_revision = 1;
766 iort->header.length = sizeof(struct acpi_table_iort);
767 iort->node_offset = sizeof(struct acpi_table_iort);
768
769 acpi_inc(ctx, sizeof(struct acpi_table_iort));
770
771 offset = sizeof(struct acpi_table_iort);
772 ret = acpi_fill_iort(ctx);
773 if (ret) {
774 ctx->current = iort;
775 return log_msg_ret("fill", ret);
776 }
777
778 /* Count nodes filled in */
779 for (node = (void *)iort + iort->node_offset;
780 node->length > 0 && (void *)node < ctx->current;
781 node = (void *)node + node->length)
782 iort->node_count++;
783
784 /* (Re)calculate length and checksum */
785 iort->header.length = ctx->current - (void *)iort;
Heinrich Schuchardt7b332b52025-03-22 00:21:17 +0100786 acpi_update_checksum(&iort->header);
Patrick Rudolph1669ce72024-10-23 15:19:54 +0200787 log_debug("IORT at %p, length %x\n", iort, iort->header.length);
788
789 /* Drop the table if it is empty */
790 if (iort->header.length == sizeof(struct acpi_table_iort))
791 return log_msg_ret("fill", -ENOENT);
792 acpi_add_table(ctx, iort);
793
794 return 0;
795}
796
797ACPI_WRITER(5iort, "IORT", acpi_write_iort, 0);
Patrick Rudolph158efd62024-10-23 15:19:57 +0200798
799/*
800 * Allocate memory for ACPI tables and write ACPI tables to the
801 * allocated buffer.
802 *
803 * Return: status code
804 */
805static int alloc_write_acpi_tables(void)
806{
807 u64 table_end;
808 void *addr;
809
810 if (IS_ENABLED(CONFIG_X86) ||
811 IS_ENABLED(CONFIG_QFW_ACPI) ||
812 IS_ENABLED(CONFIG_SANDBOX)) {
813 log_debug("Skipping writing ACPI tables as already done\n");
814 return 0;
815 }
816
817 if (!IS_ENABLED(CONFIG_BLOBLIST_TABLES)) {
818 log_debug("Skipping writing ACPI tables as BLOBLIST_TABLES is not selected\n");
819 return 0;
820 }
821
822 /* Align the table to a 4KB boundary to keep EFI happy */
823 addr = bloblist_add(BLOBLISTT_ACPI_TABLES, TABLE_SIZE,
824 ilog2(SZ_4K));
825
826 if (!addr)
827 return log_msg_ret("mem", -ENOMEM);
828
829 gd->arch.table_start_high = virt_to_phys(addr);
830 gd->arch.table_end_high = gd->arch.table_start_high + TABLE_SIZE;
831
832 table_end = write_acpi_tables(gd->arch.table_start_high);
833 if (!table_end) {
834 log_err("Can't create ACPI configuration table\n");
835 return -EINTR;
836 }
837
838 log_debug("- wrote 'acpi' to %lx, end %llx\n", gd->arch.table_start_high, table_end);
839 if (table_end > gd->arch.table_end_high) {
840 log_err("Out of space for configuration tables: need %llx, have %x\n",
841 table_end - gd->arch.table_start_high, TABLE_SIZE);
842 return log_msg_ret("acpi", -ENOSPC);
843 }
844
845 log_debug("- done writing tables\n");
846
847 return 0;
848}
849
850EVENT_SPY_SIMPLE(EVT_LAST_STAGE_INIT, alloc_write_acpi_tables);