blob: a80e164bc820cd50656b20940b3562761d2dfe6d [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 */
199 rsdt->header.checksum = 0;
200 rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
201 rsdt->header.length);
Simon Glass575a5472020-04-26 09:19:50 -0600202 }
203
Patrick Rudolphd69eb292024-10-23 15:19:56 +0200204 if (ctx->xsdt) {
205 /*
206 * And now the same thing for the XSDT. We use the same index as for
207 * now we want the XSDT and RSDT to always be in sync in U-Boot
208 */
209 xsdt = ctx->xsdt;
Simon Glass575a5472020-04-26 09:19:50 -0600210
Patrick Rudolphd69eb292024-10-23 15:19:56 +0200211 /* This should always be MAX_ACPI_TABLES */
212 entries_num = ARRAY_SIZE(xsdt->entry);
Simon Glass575a5472020-04-26 09:19:50 -0600213
Patrick Rudolphd69eb292024-10-23 15:19:56 +0200214 for (i = 0; i < entries_num; i++) {
215 if (xsdt->entry[i] == 0)
216 break;
217 }
Simon Glass575a5472020-04-26 09:19:50 -0600218
Patrick Rudolphd69eb292024-10-23 15:19:56 +0200219 if (i >= entries_num) {
220 log_err("ACPI: Error: too many tables\n");
221 return -E2BIG;
222 }
Simon Glass575a5472020-04-26 09:19:50 -0600223
Patrick Rudolphd69eb292024-10-23 15:19:56 +0200224 /* Add table to the XSDT */
225 xsdt->entry[i] = nomap_to_sysmem(table);
Simon Glass575a5472020-04-26 09:19:50 -0600226
Patrick Rudolphd69eb292024-10-23 15:19:56 +0200227 /* Fix XSDT length */
228 xsdt->header.length = sizeof(struct acpi_table_header) +
229 (sizeof(u64) * (i + 1));
Simon Glass575a5472020-04-26 09:19:50 -0600230
Patrick Rudolphd69eb292024-10-23 15:19:56 +0200231 /* Re-calculate checksum */
232 xsdt->header.checksum = 0;
233 xsdt->header.checksum = table_compute_checksum((u8 *)xsdt,
234 xsdt->header.length);
235 }
Simon Glass575a5472020-04-26 09:19:50 -0600236
237 return 0;
238}
Simon Glass9c442a62020-04-26 09:19:51 -0600239
Maximilian Brune8dc45122024-10-23 15:19:45 +0200240int acpi_write_fadt(struct acpi_ctx *ctx, const struct acpi_writer *entry)
241{
242 struct acpi_table_header *header;
243 struct acpi_fadt *fadt;
244
245 fadt = ctx->current;
246 header = &fadt->header;
247
248 memset((void *)fadt, '\0', sizeof(struct acpi_fadt));
249
250 acpi_fill_header(header, "FACP");
251 header->length = sizeof(struct acpi_fadt);
252 header->revision = acpi_get_table_revision(ACPITAB_FADT);
253 memcpy(header->oem_id, OEM_ID, 6);
254 memcpy(header->oem_table_id, OEM_TABLE_ID, 8);
255 memcpy(header->creator_id, ASLC_ID, 4);
256 header->creator_revision = 1;
Patrick Rudolphf317fce2024-10-23 15:19:52 +0200257 fadt->minor_revision = 2;
Maximilian Brune8dc45122024-10-23 15:19:45 +0200258
Simon Glass4cd81bc2025-03-15 14:26:02 +0000259 fadt->x_firmware_ctrl = nomap_to_sysmem(ctx->facs);
260 fadt->x_dsdt = nomap_to_sysmem(ctx->dsdt);
Maximilian Brune8dc45122024-10-23 15:19:45 +0200261
262 if (fadt->x_firmware_ctrl < 0x100000000ULL)
263 fadt->firmware_ctrl = fadt->x_firmware_ctrl;
264
265 if (fadt->x_dsdt < 0x100000000ULL)
266 fadt->dsdt = fadt->x_dsdt;
267
268 fadt->preferred_pm_profile = ACPI_PM_UNSPECIFIED;
269
270 acpi_fill_fadt(fadt);
271
272 header->checksum = table_compute_checksum(fadt, header->length);
273
274 return acpi_add_fadt(ctx, fadt);
275}
276
Heinrich Schuchardt08839712024-12-20 01:37:59 +0100277#ifndef CONFIG_QFW_ACPI
Maximilian Brune8dc45122024-10-23 15:19:45 +0200278ACPI_WRITER(5fadt, "FADT", acpi_write_fadt, 0);
Heinrich Schuchardt08839712024-12-20 01:37:59 +0100279#endif
Maximilian Brune8dc45122024-10-23 15:19:45 +0200280
Patrick Rudolph97b4c8a2024-10-23 15:19:46 +0200281int acpi_write_madt(struct acpi_ctx *ctx, const struct acpi_writer *entry)
282{
283 struct acpi_table_header *header;
284 struct acpi_madt *madt;
285 void *current;
286
287 madt = ctx->current;
288
289 memset(madt, '\0', sizeof(struct acpi_madt));
290 header = &madt->header;
291
292 /* Fill out header fields */
293 acpi_fill_header(header, "APIC");
294 header->length = sizeof(struct acpi_madt);
Patrick Rudolphf317fce2024-10-23 15:19:52 +0200295 header->revision = acpi_get_table_revision(ACPITAB_MADT);
Patrick Rudolph97b4c8a2024-10-23 15:19:46 +0200296
297 acpi_inc(ctx, sizeof(struct acpi_madt));
Patrick Rudolphbff7c572024-10-23 15:19:51 +0200298 /* TODO: Get rid of acpi_fill_madt and use driver model */
Patrick Rudolph97b4c8a2024-10-23 15:19:46 +0200299 current = acpi_fill_madt(madt, ctx);
300
301 /* (Re)calculate length and checksum */
302 header->length = (uintptr_t)current - (uintptr_t)madt;
Patrick Rudolph2f6f8d92024-10-23 15:20:13 +0200303
304 if (IS_ENABLED(CONFIG_ACPI_PARKING_PROTOCOL))
305 acpi_write_park(madt);
306
Patrick Rudolph97b4c8a2024-10-23 15:19:46 +0200307 header->checksum = table_compute_checksum((void *)madt, header->length);
308 acpi_add_table(ctx, madt);
309 ctx->current = (void *)madt + madt->header.length;
310
311 return 0;
312}
313
Heinrich Schuchardt08839712024-12-20 01:37:59 +0100314#ifndef CONFIG_QFW_ACPI
Patrick Rudolph97b4c8a2024-10-23 15:19:46 +0200315ACPI_WRITER(5madt, "MADT", acpi_write_madt, 0);
Heinrich Schuchardt08839712024-12-20 01:37:59 +0100316#endif
Patrick Rudolph97b4c8a2024-10-23 15:19:46 +0200317
Simon Glass95971892020-09-22 12:45:10 -0600318void acpi_create_dbg2(struct acpi_dbg2_header *dbg2,
319 int port_type, int port_subtype,
320 struct acpi_gen_regaddr *address, u32 address_size,
321 const char *device_path)
322{
323 uintptr_t current;
324 struct acpi_dbg2_device *device;
325 u32 *dbg2_addr_size;
326 struct acpi_table_header *header;
327 size_t path_len;
328 const char *path;
329 char *namespace;
330
331 /* Fill out header fields. */
332 current = (uintptr_t)dbg2;
333 memset(dbg2, '\0', sizeof(struct acpi_dbg2_header));
334 header = &dbg2->header;
335
336 header->revision = acpi_get_table_revision(ACPITAB_DBG2);
337 acpi_fill_header(header, "DBG2");
Simon Glass95971892020-09-22 12:45:10 -0600338
339 /* One debug device defined */
340 dbg2->devices_offset = sizeof(struct acpi_dbg2_header);
341 dbg2->devices_count = 1;
342 current += sizeof(struct acpi_dbg2_header);
343
344 /* Device comes after the header */
345 device = (struct acpi_dbg2_device *)current;
346 memset(device, 0, sizeof(struct acpi_dbg2_device));
347 current += sizeof(struct acpi_dbg2_device);
348
349 device->revision = 0;
350 device->address_count = 1;
351 device->port_type = port_type;
352 device->port_subtype = port_subtype;
353
354 /* Base Address comes after device structure */
355 memcpy((void *)current, address, sizeof(struct acpi_gen_regaddr));
356 device->base_address_offset = current - (uintptr_t)device;
357 current += sizeof(struct acpi_gen_regaddr);
358
359 /* Address Size comes after address structure */
360 dbg2_addr_size = (uint32_t *)current;
361 device->address_size_offset = current - (uintptr_t)device;
362 *dbg2_addr_size = address_size;
363 current += sizeof(uint32_t);
364
365 /* Namespace string comes last, use '.' if not provided */
366 path = device_path ? : ".";
367 /* Namespace string length includes NULL terminator */
368 path_len = strlen(path) + 1;
369 namespace = (char *)current;
370 device->namespace_string_length = path_len;
371 device->namespace_string_offset = current - (uintptr_t)device;
372 strncpy(namespace, path, path_len);
373 current += path_len;
374
375 /* Update structure lengths and checksum */
376 device->length = current - (uintptr_t)device;
377 header->length = current - (uintptr_t)dbg2;
378 header->checksum = table_compute_checksum(dbg2, header->length);
379}
Maximilian Bruned7fa54b2024-10-23 15:19:44 +0200380
381int acpi_write_dbg2_pci_uart(struct acpi_ctx *ctx, struct udevice *dev,
382 uint access_size)
383{
384 struct acpi_dbg2_header *dbg2 = ctx->current;
385 char path[ACPI_PATH_MAX];
386 struct acpi_gen_regaddr address;
387 u64 addr;
388 int ret;
389
390 if (!device_active(dev)) {
391 log_info("Device not enabled\n");
392 return -EACCES;
393 }
394 /*
395 * PCI devices don't remember their resource allocation information in
396 * U-Boot at present. We assume that MMIO is used for the UART and that
397 * the address space is 32 bytes: ns16550 uses 8 registers of up to
398 * 32-bits each. This is only for debugging so it is not a big deal.
399 */
400 addr = dm_pci_read_bar32(dev, 0);
401 log_debug("UART addr %lx\n", (ulong)addr);
402
403 ret = acpi_device_path(dev, path, sizeof(path));
404 if (ret)
405 return log_msg_ret("path", ret);
406
407 memset(&address, '\0', sizeof(address));
408 address.space_id = ACPI_ADDRESS_SPACE_MEMORY;
409 address.addrl = (uint32_t)addr;
410 address.addrh = (uint32_t)((addr >> 32) & 0xffffffff);
411 address.access_size = access_size;
412
413 ret = acpi_device_path(dev, path, sizeof(path));
414 if (ret)
415 return log_msg_ret("path", ret);
416 acpi_create_dbg2(dbg2, ACPI_DBG2_SERIAL_PORT,
417 ACPI_DBG2_16550_COMPATIBLE, &address, 0x1000, path);
418
419 acpi_inc_align(ctx, dbg2->header.length);
420 acpi_add_table(ctx, dbg2);
421
422 return 0;
423}
424
425static int acpi_write_spcr(struct acpi_ctx *ctx, const struct acpi_writer *entry)
426{
427 struct serial_device_info serial_info = {0};
Patrick Rudolph1bf4cb22024-10-30 14:11:46 +0100428 u64 serial_address, serial_offset;
Maximilian Bruned7fa54b2024-10-23 15:19:44 +0200429 struct acpi_table_header *header;
430 struct acpi_spcr *spcr;
431 struct udevice *dev;
432 uint serial_config;
433 uint serial_width;
434 int access_size;
435 int space_id;
436 int ret = -ENODEV;
437
438 spcr = ctx->current;
439 header = &spcr->header;
440
441 memset(spcr, '\0', sizeof(struct acpi_spcr));
442
443 /* Fill out header fields */
444 acpi_fill_header(header, "SPCR");
445 header->length = sizeof(struct acpi_spcr);
446 header->revision = 2;
447
448 /* Read the device once, here. It is reused below */
449 dev = gd->cur_serial_dev;
450 if (dev)
451 ret = serial_getinfo(dev, &serial_info);
452 if (ret)
453 serial_info.type = SERIAL_CHIP_UNKNOWN;
454
455 /* Encode chip type */
456 switch (serial_info.type) {
457 case SERIAL_CHIP_16550_COMPATIBLE:
458 spcr->interface_type = ACPI_DBG2_16550_COMPATIBLE;
459 break;
460 case SERIAL_CHIP_PL01X:
461 spcr->interface_type = ACPI_DBG2_ARM_PL011;
462 break;
463 case SERIAL_CHIP_UNKNOWN:
464 default:
465 spcr->interface_type = ACPI_DBG2_UNKNOWN;
466 break;
467 }
468
469 /* Encode address space */
470 switch (serial_info.addr_space) {
471 case SERIAL_ADDRESS_SPACE_MEMORY:
472 space_id = ACPI_ADDRESS_SPACE_MEMORY;
473 break;
474 case SERIAL_ADDRESS_SPACE_IO:
475 default:
476 space_id = ACPI_ADDRESS_SPACE_IO;
477 break;
478 }
479
480 serial_width = serial_info.reg_width * 8;
Patrick Rudolph1bf4cb22024-10-30 14:11:46 +0100481 serial_offset = ((u64)serial_info.reg_offset) << serial_info.reg_shift;
Maximilian Bruned7fa54b2024-10-23 15:19:44 +0200482 serial_address = serial_info.addr + serial_offset;
483
484 /* Encode register access size */
485 switch (serial_info.reg_shift) {
486 case 0:
487 access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS;
488 break;
489 case 1:
490 access_size = ACPI_ACCESS_SIZE_WORD_ACCESS;
491 break;
492 case 2:
493 access_size = ACPI_ACCESS_SIZE_DWORD_ACCESS;
494 break;
495 case 3:
496 access_size = ACPI_ACCESS_SIZE_QWORD_ACCESS;
497 break;
498 default:
499 access_size = ACPI_ACCESS_SIZE_UNDEFINED;
500 break;
501 }
502
Patrick Rudolph1bf4cb22024-10-30 14:11:46 +0100503 debug("UART type %u @ %llx\n", spcr->interface_type, serial_address);
Maximilian Bruned7fa54b2024-10-23 15:19:44 +0200504
505 /* Fill GAS */
506 spcr->serial_port.space_id = space_id;
507 spcr->serial_port.bit_width = serial_width;
508 spcr->serial_port.bit_offset = 0;
509 spcr->serial_port.access_size = access_size;
510 spcr->serial_port.addrl = lower_32_bits(serial_address);
511 spcr->serial_port.addrh = upper_32_bits(serial_address);
512
513 /* Encode baud rate */
514 switch (serial_info.baudrate) {
515 case 9600:
516 spcr->baud_rate = 3;
517 break;
518 case 19200:
519 spcr->baud_rate = 4;
520 break;
521 case 57600:
522 spcr->baud_rate = 6;
523 break;
524 case 115200:
525 spcr->baud_rate = 7;
526 break;
527 default:
528 spcr->baud_rate = 0;
529 break;
530 }
531
532 serial_config = SERIAL_DEFAULT_CONFIG;
533 if (dev)
534 ret = serial_getconfig(dev, &serial_config);
535
536 spcr->parity = SERIAL_GET_PARITY(serial_config);
537 spcr->stop_bits = SERIAL_GET_STOP(serial_config);
538
539 /* No PCI devices for now */
540 spcr->pci_device_id = 0xffff;
541 spcr->pci_vendor_id = 0xffff;
542
543 /*
544 * SPCR has no clue if the UART base clock speed is different
545 * to the default one. However, the SPCR 1.04 defines baud rate
546 * 0 as a preconfigured state of UART and OS is supposed not
547 * to touch the configuration of the serial device.
548 */
549 if (serial_info.clock != SERIAL_DEFAULT_CLOCK)
550 spcr->baud_rate = 0;
551
552 /* Fix checksum */
553 header->checksum = table_compute_checksum((void *)spcr, header->length);
554
555 acpi_add_table(ctx, spcr);
556 acpi_inc(ctx, spcr->header.length);
557
558 return 0;
559}
560
561ACPI_WRITER(5spcr, "SPCR", acpi_write_spcr, 0);
Patrick Rudolph1669ce72024-10-23 15:19:54 +0200562
563__weak int acpi_fill_iort(struct acpi_ctx *ctx)
564{
565 return 0;
566}
567
568int acpi_iort_add_its_group(struct acpi_ctx *ctx,
569 const u32 its_count,
570 const u32 *identifiers)
571{
572 struct acpi_iort_node *node;
573 struct acpi_iort_its_group *group;
574 int offset;
575
576 offset = ctx->current - ctx->tab_start;
577
578 node = ctx->current;
579 memset(node, '\0', sizeof(struct acpi_iort_node));
580
581 node->type = ACPI_IORT_NODE_ITS_GROUP;
582 node->revision = 1;
583
584 node->length = sizeof(struct acpi_iort_node);
585 node->length += sizeof(struct acpi_iort_its_group);
586 node->length += sizeof(u32) * its_count;
587
588 group = (struct acpi_iort_its_group *)node->node_data;
589 group->its_count = its_count;
590 memcpy(&group->identifiers, identifiers, sizeof(u32) * its_count);
591
592 ctx->current += node->length;
593
594 return offset;
595}
596
597int acpi_iort_add_named_component(struct acpi_ctx *ctx,
598 const u32 node_flags,
599 const u64 memory_properties,
600 const u8 memory_address_limit,
601 const char *device_name)
602{
603 struct acpi_iort_node *node;
604 struct acpi_iort_named_component *comp;
605 int offset;
606
607 offset = ctx->current - ctx->tab_start;
608
609 node = ctx->current;
610 memset(node, '\0', sizeof(struct acpi_iort_node));
611
612 node->type = ACPI_IORT_NODE_NAMED_COMPONENT;
613 node->revision = 4;
614 node->length = sizeof(struct acpi_iort_node);
615 node->length += sizeof(struct acpi_iort_named_component);
616 node->length += strlen(device_name) + 1;
617
618 comp = (struct acpi_iort_named_component *)node->node_data;
619
620 comp->node_flags = node_flags;
621 comp->memory_properties = memory_properties;
622 comp->memory_address_limit = memory_address_limit;
623 memcpy(comp->device_name, device_name, strlen(device_name) + 1);
624
625 ctx->current += node->length;
626
627 return offset;
628}
629
630int acpi_iort_add_rc(struct acpi_ctx *ctx,
631 const u64 mem_access_properties,
632 const u32 ats_attributes,
633 const u32 pci_segment_number,
634 const u8 memory_address_size_limit,
635 const int num_mappings,
636 const struct acpi_iort_id_mapping *map)
637{
638 struct acpi_iort_id_mapping *mapping;
639 struct acpi_iort_node *node;
640 struct acpi_iort_rc *rc;
641 int offset;
642
643 offset = ctx->current - ctx->tab_start;
644
645 node = ctx->current;
646 memset(node, '\0', sizeof(struct acpi_iort_node));
647
648 node->type = ACPI_IORT_NODE_PCI_ROOT_COMPLEX;
649 node->revision = 2;
650
651 node->length = sizeof(struct acpi_iort_node);
652 node->length += sizeof(struct acpi_iort_rc);
653 node->length += sizeof(struct acpi_iort_id_mapping) * num_mappings;
654
655 rc = (struct acpi_iort_rc *)node->node_data;
656 rc->mem_access_properties = mem_access_properties;
657 rc->ats_attributes = ats_attributes;
658 rc->pci_segment_number = pci_segment_number;
659 rc->memory_address_size_limit = memory_address_size_limit;
660
661 mapping = (struct acpi_iort_id_mapping *)(rc + 1);
662 for (int i = 0; i < num_mappings; i++) {
663 memcpy(mapping, &map[i], sizeof(struct acpi_iort_id_mapping));
664 mapping++;
665 }
666
667 ctx->current += node->length;
668
669 return offset;
670}
671
672int acpi_iort_add_smmu_v3(struct acpi_ctx *ctx,
673 const u64 base_address,
674 const u32 flags,
675 const u64 vatos_address,
676 const u32 model,
677 const u32 event_gsiv,
678 const u32 pri_gsiv,
679 const u32 gerr_gsiv,
680 const u32 sync_gsiv,
681 const u32 pxm,
682 const u32 id_mapping_index,
683 const int num_mappings,
684 const struct acpi_iort_id_mapping *map)
685{
686 struct acpi_iort_node *node;
687 struct acpi_iort_smmu_v3 *smmu;
688 struct acpi_iort_id_mapping *mapping;
689 int offset;
690
691 offset = ctx->current - ctx->tab_start;
692
693 node = ctx->current;
694 memset(node, '\0', sizeof(struct acpi_iort_node));
695
696 node->type = ACPI_IORT_NODE_SMMU_V3;
697 node->revision = 5;
698 node->mapping_count = num_mappings;
699 node->mapping_offset = sizeof(struct acpi_iort_node) + sizeof(struct acpi_iort_smmu_v3);
700
701 node->length = sizeof(struct acpi_iort_node);
702 node->length += sizeof(struct acpi_iort_smmu_v3);
703 node->length += sizeof(struct acpi_iort_id_mapping) * num_mappings;
704
705 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
706
707 smmu->base_address = base_address;
708 smmu->flags = flags;
709 smmu->vatos_address = vatos_address;
710 smmu->model = model;
711 smmu->event_gsiv = event_gsiv;
712 smmu->pri_gsiv = pri_gsiv;
713 smmu->gerr_gsiv = gerr_gsiv;
714 smmu->sync_gsiv = sync_gsiv;
715 smmu->pxm = pxm;
716 smmu->id_mapping_index = id_mapping_index;
717
718 mapping = (struct acpi_iort_id_mapping *)(smmu + 1);
719 for (int i = 0; i < num_mappings; i++) {
720 memcpy(mapping, &map[i], sizeof(struct acpi_iort_id_mapping));
721 mapping++;
722 }
723
724 ctx->current += node->length;
725
726 return offset;
727}
728
729static int acpi_write_iort(struct acpi_ctx *ctx, const struct acpi_writer *entry)
730{
731 struct acpi_table_iort *iort;
732 struct acpi_iort_node *node;
733 u32 offset;
734 int ret;
735
736 iort = ctx->current;
737 ctx->tab_start = ctx->current;
738 memset(iort, '\0', sizeof(struct acpi_table_iort));
739
740 acpi_fill_header(&iort->header, "IORT");
741 iort->header.revision = 1;
742 iort->header.creator_revision = 1;
743 iort->header.length = sizeof(struct acpi_table_iort);
744 iort->node_offset = sizeof(struct acpi_table_iort);
745
746 acpi_inc(ctx, sizeof(struct acpi_table_iort));
747
748 offset = sizeof(struct acpi_table_iort);
749 ret = acpi_fill_iort(ctx);
750 if (ret) {
751 ctx->current = iort;
752 return log_msg_ret("fill", ret);
753 }
754
755 /* Count nodes filled in */
756 for (node = (void *)iort + iort->node_offset;
757 node->length > 0 && (void *)node < ctx->current;
758 node = (void *)node + node->length)
759 iort->node_count++;
760
761 /* (Re)calculate length and checksum */
762 iort->header.length = ctx->current - (void *)iort;
763 iort->header.checksum = table_compute_checksum((void *)iort, iort->header.length);
764 log_debug("IORT at %p, length %x\n", iort, iort->header.length);
765
766 /* Drop the table if it is empty */
767 if (iort->header.length == sizeof(struct acpi_table_iort))
768 return log_msg_ret("fill", -ENOENT);
769 acpi_add_table(ctx, iort);
770
771 return 0;
772}
773
774ACPI_WRITER(5iort, "IORT", acpi_write_iort, 0);
Patrick Rudolph158efd62024-10-23 15:19:57 +0200775
776/*
777 * Allocate memory for ACPI tables and write ACPI tables to the
778 * allocated buffer.
779 *
780 * Return: status code
781 */
782static int alloc_write_acpi_tables(void)
783{
784 u64 table_end;
785 void *addr;
786
787 if (IS_ENABLED(CONFIG_X86) ||
788 IS_ENABLED(CONFIG_QFW_ACPI) ||
789 IS_ENABLED(CONFIG_SANDBOX)) {
790 log_debug("Skipping writing ACPI tables as already done\n");
791 return 0;
792 }
793
794 if (!IS_ENABLED(CONFIG_BLOBLIST_TABLES)) {
795 log_debug("Skipping writing ACPI tables as BLOBLIST_TABLES is not selected\n");
796 return 0;
797 }
798
799 /* Align the table to a 4KB boundary to keep EFI happy */
800 addr = bloblist_add(BLOBLISTT_ACPI_TABLES, TABLE_SIZE,
801 ilog2(SZ_4K));
802
803 if (!addr)
804 return log_msg_ret("mem", -ENOMEM);
805
806 gd->arch.table_start_high = virt_to_phys(addr);
807 gd->arch.table_end_high = gd->arch.table_start_high + TABLE_SIZE;
808
809 table_end = write_acpi_tables(gd->arch.table_start_high);
810 if (!table_end) {
811 log_err("Can't create ACPI configuration table\n");
812 return -EINTR;
813 }
814
815 log_debug("- wrote 'acpi' to %lx, end %llx\n", gd->arch.table_start_high, table_end);
816 if (table_end > gd->arch.table_end_high) {
817 log_err("Out of space for configuration tables: need %llx, have %x\n",
818 table_end - gd->arch.table_start_high, TABLE_SIZE);
819 return log_msg_ret("acpi", -ENOSPC);
820 }
821
822 log_debug("- done writing tables\n");
823
824 return 0;
825}
826
827EVENT_SPY_SIMPLE(EVT_LAST_STAGE_INIT, alloc_write_acpi_tables);