blob: d6357bdc4db50f4c933ba5a6153a60e00faf7dca [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
Simon Glasse9629892020-04-08 16:57:39 -06008#include <dm.h>
9#include <cpu.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Simon Glass575a5472020-04-26 09:19:50 -060011#include <mapmem.h>
12#include <tables_csum.h>
Maximilian Bruned7fa54b2024-10-23 15:19:44 +020013#include <serial.h>
Simon Glass90b01272023-04-29 19:21:46 -060014#include <version_string.h>
Simon Glass0e113842020-04-26 09:19:47 -060015#include <acpi/acpi_table.h>
Maximilian Bruned7fa54b2024-10-23 15:19:44 +020016#include <acpi/acpi_device.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060017#include <asm/global_data.h>
Simon Glass0e113842020-04-26 09:19:47 -060018#include <dm/acpi.h>
Simon Glasse9629892020-04-08 16:57:39 -060019
Pali Rohárf0de20e2021-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 Glass90b01272023-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árf0de20e2021-07-10 13:10:01 +020034 0x01)
35
Simon Glasse9629892020-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 Suchanekac12a2f2022-10-12 21:57:59 +020043 ret = uclass_first_device_err(UCLASS_CPU, &cpu);
Simon Glasse9629892020-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 Glassb2672ea2020-04-08 16:57:38 -060061
62int acpi_get_table_revision(enum acpi_tables table)
63{
64 switch (table) {
65 case ACPITAB_FADT:
Patrick Rudolphf317fce2024-10-23 15:19:52 +020066 return ACPI_FADT_REV_ACPI_6_0;
Simon Glassb2672ea2020-04-08 16:57:38 -060067 case ACPITAB_MADT:
Patrick Rudolphf317fce2024-10-23 15:19:52 +020068 return ACPI_MADT_REV_ACPI_6_2;
Simon Glassb2672ea2020-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 Rudolph4d3cf1a2024-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 Glassb2672ea2020-04-08 16:57:38 -0600114 default:
115 return -EINVAL;
116 }
117}
Simon Glass17968c32020-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árf0de20e2021-07-10 13:10:01 +0200124 header->oem_revision = OEM_REVISION;
Heinrich Schuchardt10de8a82024-01-21 12:52:48 +0100125 memcpy(header->creator_id, ASLC_ID, 4);
Heinrich Schuchardt9838d342024-04-18 05:11:13 +0200126 header->creator_revision = ASL_REVISION;
Simon Glass17968c32020-04-26 09:19:46 -0600127}
Simon Glass0e113842020-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 Glass575a5472020-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
Patrick Rudolphd69eb292024-10-23 15:19:56 +0200160 /* On legacy x86 platforms the RSDT is mandatory while the XSDT is not.
161 * On other platforms there might be no memory below 4GiB, thus RSDT is NULL.
162 */
163 if (ctx->rsdt) {
164 rsdt = ctx->rsdt;
Simon Glass575a5472020-04-26 09:19:50 -0600165
Patrick Rudolphd69eb292024-10-23 15:19:56 +0200166 /* This should always be MAX_ACPI_TABLES */
167 entries_num = ARRAY_SIZE(rsdt->entry);
Simon Glass575a5472020-04-26 09:19:50 -0600168
Patrick Rudolphd69eb292024-10-23 15:19:56 +0200169 for (i = 0; i < entries_num; i++) {
170 if (rsdt->entry[i] == 0)
171 break;
172 }
Simon Glass575a5472020-04-26 09:19:50 -0600173
Patrick Rudolphd69eb292024-10-23 15:19:56 +0200174 if (i >= entries_num) {
175 log_err("ACPI: Error: too many tables\n");
176 return -E2BIG;
177 }
178
179 /* Add table to the RSDT */
180 rsdt->entry[i] = nomap_to_sysmem(table);
181
182 /* Fix RSDT length or the kernel will assume invalid entries */
183 rsdt->header.length = sizeof(struct acpi_table_header) +
184 (sizeof(u32) * (i + 1));
185
186 /* Re-calculate checksum */
187 rsdt->header.checksum = 0;
188 rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
189 rsdt->header.length);
Simon Glass575a5472020-04-26 09:19:50 -0600190 }
191
Patrick Rudolphd69eb292024-10-23 15:19:56 +0200192 if (ctx->xsdt) {
193 /*
194 * And now the same thing for the XSDT. We use the same index as for
195 * now we want the XSDT and RSDT to always be in sync in U-Boot
196 */
197 xsdt = ctx->xsdt;
Simon Glass575a5472020-04-26 09:19:50 -0600198
Patrick Rudolphd69eb292024-10-23 15:19:56 +0200199 /* This should always be MAX_ACPI_TABLES */
200 entries_num = ARRAY_SIZE(xsdt->entry);
Simon Glass575a5472020-04-26 09:19:50 -0600201
Patrick Rudolphd69eb292024-10-23 15:19:56 +0200202 for (i = 0; i < entries_num; i++) {
203 if (xsdt->entry[i] == 0)
204 break;
205 }
Simon Glass575a5472020-04-26 09:19:50 -0600206
Patrick Rudolphd69eb292024-10-23 15:19:56 +0200207 if (i >= entries_num) {
208 log_err("ACPI: Error: too many tables\n");
209 return -E2BIG;
210 }
Simon Glass575a5472020-04-26 09:19:50 -0600211
Patrick Rudolphd69eb292024-10-23 15:19:56 +0200212 /* Add table to the XSDT */
213 xsdt->entry[i] = nomap_to_sysmem(table);
Simon Glass575a5472020-04-26 09:19:50 -0600214
Patrick Rudolphd69eb292024-10-23 15:19:56 +0200215 /* Fix XSDT length */
216 xsdt->header.length = sizeof(struct acpi_table_header) +
217 (sizeof(u64) * (i + 1));
Simon Glass575a5472020-04-26 09:19:50 -0600218
Patrick Rudolphd69eb292024-10-23 15:19:56 +0200219 /* Re-calculate checksum */
220 xsdt->header.checksum = 0;
221 xsdt->header.checksum = table_compute_checksum((u8 *)xsdt,
222 xsdt->header.length);
223 }
Simon Glass575a5472020-04-26 09:19:50 -0600224
225 return 0;
226}
Simon Glass9c442a62020-04-26 09:19:51 -0600227
Maximilian Brune8dc45122024-10-23 15:19:45 +0200228int acpi_write_fadt(struct acpi_ctx *ctx, const struct acpi_writer *entry)
229{
230 struct acpi_table_header *header;
231 struct acpi_fadt *fadt;
232
233 fadt = ctx->current;
234 header = &fadt->header;
235
236 memset((void *)fadt, '\0', sizeof(struct acpi_fadt));
237
238 acpi_fill_header(header, "FACP");
239 header->length = sizeof(struct acpi_fadt);
240 header->revision = acpi_get_table_revision(ACPITAB_FADT);
241 memcpy(header->oem_id, OEM_ID, 6);
242 memcpy(header->oem_table_id, OEM_TABLE_ID, 8);
243 memcpy(header->creator_id, ASLC_ID, 4);
244 header->creator_revision = 1;
Patrick Rudolphf317fce2024-10-23 15:19:52 +0200245 fadt->minor_revision = 2;
Maximilian Brune8dc45122024-10-23 15:19:45 +0200246
247 fadt->x_firmware_ctrl = map_to_sysmem(ctx->facs);
248 fadt->x_dsdt = map_to_sysmem(ctx->dsdt);
249
250 if (fadt->x_firmware_ctrl < 0x100000000ULL)
251 fadt->firmware_ctrl = fadt->x_firmware_ctrl;
252
253 if (fadt->x_dsdt < 0x100000000ULL)
254 fadt->dsdt = fadt->x_dsdt;
255
256 fadt->preferred_pm_profile = ACPI_PM_UNSPECIFIED;
257
258 acpi_fill_fadt(fadt);
259
260 header->checksum = table_compute_checksum(fadt, header->length);
261
262 return acpi_add_fadt(ctx, fadt);
263}
264
265ACPI_WRITER(5fadt, "FADT", acpi_write_fadt, 0);
266
Patrick Rudolph97b4c8a2024-10-23 15:19:46 +0200267int acpi_write_madt(struct acpi_ctx *ctx, const struct acpi_writer *entry)
268{
269 struct acpi_table_header *header;
270 struct acpi_madt *madt;
271 void *current;
272
273 madt = ctx->current;
274
275 memset(madt, '\0', sizeof(struct acpi_madt));
276 header = &madt->header;
277
278 /* Fill out header fields */
279 acpi_fill_header(header, "APIC");
280 header->length = sizeof(struct acpi_madt);
Patrick Rudolphf317fce2024-10-23 15:19:52 +0200281 header->revision = acpi_get_table_revision(ACPITAB_MADT);
Patrick Rudolph97b4c8a2024-10-23 15:19:46 +0200282
283 acpi_inc(ctx, sizeof(struct acpi_madt));
Patrick Rudolphbff7c572024-10-23 15:19:51 +0200284 /* TODO: Get rid of acpi_fill_madt and use driver model */
Patrick Rudolph97b4c8a2024-10-23 15:19:46 +0200285 current = acpi_fill_madt(madt, ctx);
286
287 /* (Re)calculate length and checksum */
288 header->length = (uintptr_t)current - (uintptr_t)madt;
Patrick Rudolph97b4c8a2024-10-23 15:19:46 +0200289 header->checksum = table_compute_checksum((void *)madt, header->length);
290 acpi_add_table(ctx, madt);
291 ctx->current = (void *)madt + madt->header.length;
292
293 return 0;
294}
295
296ACPI_WRITER(5madt, "MADT", acpi_write_madt, 0);
297
Simon Glass95971892020-09-22 12:45:10 -0600298void acpi_create_dbg2(struct acpi_dbg2_header *dbg2,
299 int port_type, int port_subtype,
300 struct acpi_gen_regaddr *address, u32 address_size,
301 const char *device_path)
302{
303 uintptr_t current;
304 struct acpi_dbg2_device *device;
305 u32 *dbg2_addr_size;
306 struct acpi_table_header *header;
307 size_t path_len;
308 const char *path;
309 char *namespace;
310
311 /* Fill out header fields. */
312 current = (uintptr_t)dbg2;
313 memset(dbg2, '\0', sizeof(struct acpi_dbg2_header));
314 header = &dbg2->header;
315
316 header->revision = acpi_get_table_revision(ACPITAB_DBG2);
317 acpi_fill_header(header, "DBG2");
Simon Glass95971892020-09-22 12:45:10 -0600318
319 /* One debug device defined */
320 dbg2->devices_offset = sizeof(struct acpi_dbg2_header);
321 dbg2->devices_count = 1;
322 current += sizeof(struct acpi_dbg2_header);
323
324 /* Device comes after the header */
325 device = (struct acpi_dbg2_device *)current;
326 memset(device, 0, sizeof(struct acpi_dbg2_device));
327 current += sizeof(struct acpi_dbg2_device);
328
329 device->revision = 0;
330 device->address_count = 1;
331 device->port_type = port_type;
332 device->port_subtype = port_subtype;
333
334 /* Base Address comes after device structure */
335 memcpy((void *)current, address, sizeof(struct acpi_gen_regaddr));
336 device->base_address_offset = current - (uintptr_t)device;
337 current += sizeof(struct acpi_gen_regaddr);
338
339 /* Address Size comes after address structure */
340 dbg2_addr_size = (uint32_t *)current;
341 device->address_size_offset = current - (uintptr_t)device;
342 *dbg2_addr_size = address_size;
343 current += sizeof(uint32_t);
344
345 /* Namespace string comes last, use '.' if not provided */
346 path = device_path ? : ".";
347 /* Namespace string length includes NULL terminator */
348 path_len = strlen(path) + 1;
349 namespace = (char *)current;
350 device->namespace_string_length = path_len;
351 device->namespace_string_offset = current - (uintptr_t)device;
352 strncpy(namespace, path, path_len);
353 current += path_len;
354
355 /* Update structure lengths and checksum */
356 device->length = current - (uintptr_t)device;
357 header->length = current - (uintptr_t)dbg2;
358 header->checksum = table_compute_checksum(dbg2, header->length);
359}
Maximilian Bruned7fa54b2024-10-23 15:19:44 +0200360
361int acpi_write_dbg2_pci_uart(struct acpi_ctx *ctx, struct udevice *dev,
362 uint access_size)
363{
364 struct acpi_dbg2_header *dbg2 = ctx->current;
365 char path[ACPI_PATH_MAX];
366 struct acpi_gen_regaddr address;
367 u64 addr;
368 int ret;
369
370 if (!device_active(dev)) {
371 log_info("Device not enabled\n");
372 return -EACCES;
373 }
374 /*
375 * PCI devices don't remember their resource allocation information in
376 * U-Boot at present. We assume that MMIO is used for the UART and that
377 * the address space is 32 bytes: ns16550 uses 8 registers of up to
378 * 32-bits each. This is only for debugging so it is not a big deal.
379 */
380 addr = dm_pci_read_bar32(dev, 0);
381 log_debug("UART addr %lx\n", (ulong)addr);
382
383 ret = acpi_device_path(dev, path, sizeof(path));
384 if (ret)
385 return log_msg_ret("path", ret);
386
387 memset(&address, '\0', sizeof(address));
388 address.space_id = ACPI_ADDRESS_SPACE_MEMORY;
389 address.addrl = (uint32_t)addr;
390 address.addrh = (uint32_t)((addr >> 32) & 0xffffffff);
391 address.access_size = access_size;
392
393 ret = acpi_device_path(dev, path, sizeof(path));
394 if (ret)
395 return log_msg_ret("path", ret);
396 acpi_create_dbg2(dbg2, ACPI_DBG2_SERIAL_PORT,
397 ACPI_DBG2_16550_COMPATIBLE, &address, 0x1000, path);
398
399 acpi_inc_align(ctx, dbg2->header.length);
400 acpi_add_table(ctx, dbg2);
401
402 return 0;
403}
404
405static int acpi_write_spcr(struct acpi_ctx *ctx, const struct acpi_writer *entry)
406{
407 struct serial_device_info serial_info = {0};
408 ulong serial_address, serial_offset;
409 struct acpi_table_header *header;
410 struct acpi_spcr *spcr;
411 struct udevice *dev;
412 uint serial_config;
413 uint serial_width;
414 int access_size;
415 int space_id;
416 int ret = -ENODEV;
417
418 spcr = ctx->current;
419 header = &spcr->header;
420
421 memset(spcr, '\0', sizeof(struct acpi_spcr));
422
423 /* Fill out header fields */
424 acpi_fill_header(header, "SPCR");
425 header->length = sizeof(struct acpi_spcr);
426 header->revision = 2;
427
428 /* Read the device once, here. It is reused below */
429 dev = gd->cur_serial_dev;
430 if (dev)
431 ret = serial_getinfo(dev, &serial_info);
432 if (ret)
433 serial_info.type = SERIAL_CHIP_UNKNOWN;
434
435 /* Encode chip type */
436 switch (serial_info.type) {
437 case SERIAL_CHIP_16550_COMPATIBLE:
438 spcr->interface_type = ACPI_DBG2_16550_COMPATIBLE;
439 break;
440 case SERIAL_CHIP_PL01X:
441 spcr->interface_type = ACPI_DBG2_ARM_PL011;
442 break;
443 case SERIAL_CHIP_UNKNOWN:
444 default:
445 spcr->interface_type = ACPI_DBG2_UNKNOWN;
446 break;
447 }
448
449 /* Encode address space */
450 switch (serial_info.addr_space) {
451 case SERIAL_ADDRESS_SPACE_MEMORY:
452 space_id = ACPI_ADDRESS_SPACE_MEMORY;
453 break;
454 case SERIAL_ADDRESS_SPACE_IO:
455 default:
456 space_id = ACPI_ADDRESS_SPACE_IO;
457 break;
458 }
459
460 serial_width = serial_info.reg_width * 8;
461 serial_offset = serial_info.reg_offset << serial_info.reg_shift;
462 serial_address = serial_info.addr + serial_offset;
463
464 /* Encode register access size */
465 switch (serial_info.reg_shift) {
466 case 0:
467 access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS;
468 break;
469 case 1:
470 access_size = ACPI_ACCESS_SIZE_WORD_ACCESS;
471 break;
472 case 2:
473 access_size = ACPI_ACCESS_SIZE_DWORD_ACCESS;
474 break;
475 case 3:
476 access_size = ACPI_ACCESS_SIZE_QWORD_ACCESS;
477 break;
478 default:
479 access_size = ACPI_ACCESS_SIZE_UNDEFINED;
480 break;
481 }
482
483 debug("UART type %u @ %lx\n", spcr->interface_type, serial_address);
484
485 /* Fill GAS */
486 spcr->serial_port.space_id = space_id;
487 spcr->serial_port.bit_width = serial_width;
488 spcr->serial_port.bit_offset = 0;
489 spcr->serial_port.access_size = access_size;
490 spcr->serial_port.addrl = lower_32_bits(serial_address);
491 spcr->serial_port.addrh = upper_32_bits(serial_address);
492
493 /* Encode baud rate */
494 switch (serial_info.baudrate) {
495 case 9600:
496 spcr->baud_rate = 3;
497 break;
498 case 19200:
499 spcr->baud_rate = 4;
500 break;
501 case 57600:
502 spcr->baud_rate = 6;
503 break;
504 case 115200:
505 spcr->baud_rate = 7;
506 break;
507 default:
508 spcr->baud_rate = 0;
509 break;
510 }
511
512 serial_config = SERIAL_DEFAULT_CONFIG;
513 if (dev)
514 ret = serial_getconfig(dev, &serial_config);
515
516 spcr->parity = SERIAL_GET_PARITY(serial_config);
517 spcr->stop_bits = SERIAL_GET_STOP(serial_config);
518
519 /* No PCI devices for now */
520 spcr->pci_device_id = 0xffff;
521 spcr->pci_vendor_id = 0xffff;
522
523 /*
524 * SPCR has no clue if the UART base clock speed is different
525 * to the default one. However, the SPCR 1.04 defines baud rate
526 * 0 as a preconfigured state of UART and OS is supposed not
527 * to touch the configuration of the serial device.
528 */
529 if (serial_info.clock != SERIAL_DEFAULT_CLOCK)
530 spcr->baud_rate = 0;
531
532 /* Fix checksum */
533 header->checksum = table_compute_checksum((void *)spcr, header->length);
534
535 acpi_add_table(ctx, spcr);
536 acpi_inc(ctx, spcr->header.length);
537
538 return 0;
539}
540
541ACPI_WRITER(5spcr, "SPCR", acpi_write_spcr, 0);
Patrick Rudolph1669ce72024-10-23 15:19:54 +0200542
543__weak int acpi_fill_iort(struct acpi_ctx *ctx)
544{
545 return 0;
546}
547
548int acpi_iort_add_its_group(struct acpi_ctx *ctx,
549 const u32 its_count,
550 const u32 *identifiers)
551{
552 struct acpi_iort_node *node;
553 struct acpi_iort_its_group *group;
554 int offset;
555
556 offset = ctx->current - ctx->tab_start;
557
558 node = ctx->current;
559 memset(node, '\0', sizeof(struct acpi_iort_node));
560
561 node->type = ACPI_IORT_NODE_ITS_GROUP;
562 node->revision = 1;
563
564 node->length = sizeof(struct acpi_iort_node);
565 node->length += sizeof(struct acpi_iort_its_group);
566 node->length += sizeof(u32) * its_count;
567
568 group = (struct acpi_iort_its_group *)node->node_data;
569 group->its_count = its_count;
570 memcpy(&group->identifiers, identifiers, sizeof(u32) * its_count);
571
572 ctx->current += node->length;
573
574 return offset;
575}
576
577int acpi_iort_add_named_component(struct acpi_ctx *ctx,
578 const u32 node_flags,
579 const u64 memory_properties,
580 const u8 memory_address_limit,
581 const char *device_name)
582{
583 struct acpi_iort_node *node;
584 struct acpi_iort_named_component *comp;
585 int offset;
586
587 offset = ctx->current - ctx->tab_start;
588
589 node = ctx->current;
590 memset(node, '\0', sizeof(struct acpi_iort_node));
591
592 node->type = ACPI_IORT_NODE_NAMED_COMPONENT;
593 node->revision = 4;
594 node->length = sizeof(struct acpi_iort_node);
595 node->length += sizeof(struct acpi_iort_named_component);
596 node->length += strlen(device_name) + 1;
597
598 comp = (struct acpi_iort_named_component *)node->node_data;
599
600 comp->node_flags = node_flags;
601 comp->memory_properties = memory_properties;
602 comp->memory_address_limit = memory_address_limit;
603 memcpy(comp->device_name, device_name, strlen(device_name) + 1);
604
605 ctx->current += node->length;
606
607 return offset;
608}
609
610int acpi_iort_add_rc(struct acpi_ctx *ctx,
611 const u64 mem_access_properties,
612 const u32 ats_attributes,
613 const u32 pci_segment_number,
614 const u8 memory_address_size_limit,
615 const int num_mappings,
616 const struct acpi_iort_id_mapping *map)
617{
618 struct acpi_iort_id_mapping *mapping;
619 struct acpi_iort_node *node;
620 struct acpi_iort_rc *rc;
621 int offset;
622
623 offset = ctx->current - ctx->tab_start;
624
625 node = ctx->current;
626 memset(node, '\0', sizeof(struct acpi_iort_node));
627
628 node->type = ACPI_IORT_NODE_PCI_ROOT_COMPLEX;
629 node->revision = 2;
630
631 node->length = sizeof(struct acpi_iort_node);
632 node->length += sizeof(struct acpi_iort_rc);
633 node->length += sizeof(struct acpi_iort_id_mapping) * num_mappings;
634
635 rc = (struct acpi_iort_rc *)node->node_data;
636 rc->mem_access_properties = mem_access_properties;
637 rc->ats_attributes = ats_attributes;
638 rc->pci_segment_number = pci_segment_number;
639 rc->memory_address_size_limit = memory_address_size_limit;
640
641 mapping = (struct acpi_iort_id_mapping *)(rc + 1);
642 for (int i = 0; i < num_mappings; i++) {
643 memcpy(mapping, &map[i], sizeof(struct acpi_iort_id_mapping));
644 mapping++;
645 }
646
647 ctx->current += node->length;
648
649 return offset;
650}
651
652int acpi_iort_add_smmu_v3(struct acpi_ctx *ctx,
653 const u64 base_address,
654 const u32 flags,
655 const u64 vatos_address,
656 const u32 model,
657 const u32 event_gsiv,
658 const u32 pri_gsiv,
659 const u32 gerr_gsiv,
660 const u32 sync_gsiv,
661 const u32 pxm,
662 const u32 id_mapping_index,
663 const int num_mappings,
664 const struct acpi_iort_id_mapping *map)
665{
666 struct acpi_iort_node *node;
667 struct acpi_iort_smmu_v3 *smmu;
668 struct acpi_iort_id_mapping *mapping;
669 int offset;
670
671 offset = ctx->current - ctx->tab_start;
672
673 node = ctx->current;
674 memset(node, '\0', sizeof(struct acpi_iort_node));
675
676 node->type = ACPI_IORT_NODE_SMMU_V3;
677 node->revision = 5;
678 node->mapping_count = num_mappings;
679 node->mapping_offset = sizeof(struct acpi_iort_node) + sizeof(struct acpi_iort_smmu_v3);
680
681 node->length = sizeof(struct acpi_iort_node);
682 node->length += sizeof(struct acpi_iort_smmu_v3);
683 node->length += sizeof(struct acpi_iort_id_mapping) * num_mappings;
684
685 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
686
687 smmu->base_address = base_address;
688 smmu->flags = flags;
689 smmu->vatos_address = vatos_address;
690 smmu->model = model;
691 smmu->event_gsiv = event_gsiv;
692 smmu->pri_gsiv = pri_gsiv;
693 smmu->gerr_gsiv = gerr_gsiv;
694 smmu->sync_gsiv = sync_gsiv;
695 smmu->pxm = pxm;
696 smmu->id_mapping_index = id_mapping_index;
697
698 mapping = (struct acpi_iort_id_mapping *)(smmu + 1);
699 for (int i = 0; i < num_mappings; i++) {
700 memcpy(mapping, &map[i], sizeof(struct acpi_iort_id_mapping));
701 mapping++;
702 }
703
704 ctx->current += node->length;
705
706 return offset;
707}
708
709static int acpi_write_iort(struct acpi_ctx *ctx, const struct acpi_writer *entry)
710{
711 struct acpi_table_iort *iort;
712 struct acpi_iort_node *node;
713 u32 offset;
714 int ret;
715
716 iort = ctx->current;
717 ctx->tab_start = ctx->current;
718 memset(iort, '\0', sizeof(struct acpi_table_iort));
719
720 acpi_fill_header(&iort->header, "IORT");
721 iort->header.revision = 1;
722 iort->header.creator_revision = 1;
723 iort->header.length = sizeof(struct acpi_table_iort);
724 iort->node_offset = sizeof(struct acpi_table_iort);
725
726 acpi_inc(ctx, sizeof(struct acpi_table_iort));
727
728 offset = sizeof(struct acpi_table_iort);
729 ret = acpi_fill_iort(ctx);
730 if (ret) {
731 ctx->current = iort;
732 return log_msg_ret("fill", ret);
733 }
734
735 /* Count nodes filled in */
736 for (node = (void *)iort + iort->node_offset;
737 node->length > 0 && (void *)node < ctx->current;
738 node = (void *)node + node->length)
739 iort->node_count++;
740
741 /* (Re)calculate length and checksum */
742 iort->header.length = ctx->current - (void *)iort;
743 iort->header.checksum = table_compute_checksum((void *)iort, iort->header.length);
744 log_debug("IORT at %p, length %x\n", iort, iort->header.length);
745
746 /* Drop the table if it is empty */
747 if (iort->header.length == sizeof(struct acpi_table_iort))
748 return log_msg_ret("fill", -ENOENT);
749 acpi_add_table(ctx, iort);
750
751 return 0;
752}
753
754ACPI_WRITER(5iort, "IORT", acpi_write_iort, 0);