blob: 3186e48d63b08a96d1669dfe6282c172066971f5 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Saket Sinha331141a2015-08-22 12:20:55 +05302/*
3 * Based on acpi.c from coreboot
4 *
5 * Copyright (C) 2015, Saket Sinha <saket.sinha89@gmail.com>
Bin Meng44256b02016-05-07 07:46:25 -07006 * Copyright (C) 2016, Bin Meng <bmeng.cn@gmail.com>
Saket Sinha331141a2015-08-22 12:20:55 +05307 */
8
Simon Glass2326a8b2020-09-22 12:45:34 -06009#define LOG_CATEGORY LOGC_ACPI
10
Simon Glass272a7032020-09-22 12:45:32 -060011#include <bloblist.h>
Saket Sinha331141a2015-08-22 12:20:55 +053012#include <cpu.h>
13#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Saket Sinha331141a2015-08-22 12:20:55 +053015#include <dm/uclass-internal.h>
Simon Glass0e113842020-04-26 09:19:47 -060016#include <mapmem.h>
Andy Shevchenko4ca48c92018-11-20 23:52:38 +020017#include <serial.h>
Simon Glassf0a8d682020-07-07 13:12:07 -060018#include <acpi/acpigen.h>
Simon Glass95971892020-09-22 12:45:10 -060019#include <acpi/acpi_device.h>
Simon Glass858fed12020-04-08 16:57:36 -060020#include <acpi/acpi_table.h>
Bin Mengd9050c62016-06-17 02:13:16 -070021#include <asm/acpi/global_nvs.h>
Andy Shevchenko13a5d872017-07-21 22:32:04 +030022#include <asm/ioapic.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060023#include <asm/global_data.h>
Saket Sinha331141a2015-08-22 12:20:55 +053024#include <asm/lapic.h>
Andy Shevchenko13a5d872017-07-21 22:32:04 +030025#include <asm/mpspec.h>
Saket Sinha331141a2015-08-22 12:20:55 +053026#include <asm/tables.h>
Bin Mengd9050c62016-06-17 02:13:16 -070027#include <asm/arch/global_nvs.h>
Simon Glass0e113842020-04-26 09:19:47 -060028#include <dm/acpi.h>
Simon Glass9ed41e72020-07-07 21:32:05 -060029#include <linux/err.h>
Saket Sinha331141a2015-08-22 12:20:55 +053030
Saket Sinha331141a2015-08-22 12:20:55 +053031static int acpi_create_madt_lapic(struct acpi_madt_lapic *lapic,
Bin Meng44256b02016-05-07 07:46:25 -070032 u8 cpu, u8 apic)
Saket Sinha331141a2015-08-22 12:20:55 +053033{
Bin Meng44256b02016-05-07 07:46:25 -070034 lapic->type = ACPI_APIC_LAPIC;
Saket Sinha331141a2015-08-22 12:20:55 +053035 lapic->length = sizeof(struct acpi_madt_lapic);
Bin Meng44256b02016-05-07 07:46:25 -070036 lapic->flags = LOCAL_APIC_FLAG_ENABLED;
Saket Sinha331141a2015-08-22 12:20:55 +053037 lapic->processor_id = cpu;
38 lapic->apic_id = apic;
39
40 return lapic->length;
41}
42
Patrick Rudolph97b4c8a2024-10-23 15:19:46 +020043int acpi_create_madt_lapics(void *current)
Saket Sinha331141a2015-08-22 12:20:55 +053044{
45 struct udevice *dev;
George McCollister5a49f872016-06-07 13:40:18 -050046 int total_length = 0;
Simon Glassfcae5472020-09-22 12:45:31 -060047 int cpu_num = 0;
Saket Sinha331141a2015-08-22 12:20:55 +053048
49 for (uclass_find_first_device(UCLASS_CPU, &dev);
50 dev;
51 uclass_find_next_device(&dev)) {
Simon Glassb75b15b2020-12-03 16:55:23 -070052 struct cpu_plat *plat = dev_get_parent_plat(dev);
Simon Glassfcae5472020-09-22 12:45:31 -060053 int length;
54
55 length = acpi_create_madt_lapic(
56 (struct acpi_madt_lapic *)current, cpu_num++,
57 plat->cpu_id);
Bin Meng3c5234e2016-05-07 07:46:30 -070058 current += length;
George McCollister5a49f872016-06-07 13:40:18 -050059 total_length += length;
Bin Meng44256b02016-05-07 07:46:25 -070060 }
61
George McCollister5a49f872016-06-07 13:40:18 -050062 return total_length;
Saket Sinha331141a2015-08-22 12:20:55 +053063}
64
Bin Meng44256b02016-05-07 07:46:25 -070065int acpi_create_madt_ioapic(struct acpi_madt_ioapic *ioapic, u8 id,
66 u32 addr, u32 gsi_base)
Saket Sinha331141a2015-08-22 12:20:55 +053067{
Bin Meng6a421582016-05-07 07:46:21 -070068 ioapic->type = ACPI_APIC_IOAPIC;
Saket Sinha331141a2015-08-22 12:20:55 +053069 ioapic->length = sizeof(struct acpi_madt_ioapic);
70 ioapic->reserved = 0x00;
71 ioapic->gsi_base = gsi_base;
72 ioapic->ioapic_id = id;
73 ioapic->ioapic_addr = addr;
74
75 return ioapic->length;
76}
77
78int acpi_create_madt_irqoverride(struct acpi_madt_irqoverride *irqoverride,
Bin Meng44256b02016-05-07 07:46:25 -070079 u8 bus, u8 source, u32 gsirq, u16 flags)
Saket Sinha331141a2015-08-22 12:20:55 +053080{
Bin Meng6a421582016-05-07 07:46:21 -070081 irqoverride->type = ACPI_APIC_IRQ_SRC_OVERRIDE;
Saket Sinha331141a2015-08-22 12:20:55 +053082 irqoverride->length = sizeof(struct acpi_madt_irqoverride);
83 irqoverride->bus = bus;
84 irqoverride->source = source;
85 irqoverride->gsirq = gsirq;
86 irqoverride->flags = flags;
87
88 return irqoverride->length;
89}
90
91int acpi_create_madt_lapic_nmi(struct acpi_madt_lapic_nmi *lapic_nmi,
Bin Meng44256b02016-05-07 07:46:25 -070092 u8 cpu, u16 flags, u8 lint)
Saket Sinha331141a2015-08-22 12:20:55 +053093{
Bin Meng6a421582016-05-07 07:46:21 -070094 lapic_nmi->type = ACPI_APIC_LAPIC_NMI;
Saket Sinha331141a2015-08-22 12:20:55 +053095 lapic_nmi->length = sizeof(struct acpi_madt_lapic_nmi);
96 lapic_nmi->flags = flags;
97 lapic_nmi->processor_id = cpu;
98 lapic_nmi->lint = lint;
99
100 return lapic_nmi->length;
101}
102
Patrick Rudolph97b4c8a2024-10-23 15:19:46 +0200103static int acpi_create_madt_irq_overrides(void *current)
Andy Shevchenko13a5d872017-07-21 22:32:04 +0300104{
105 struct acpi_madt_irqoverride *irqovr;
106 u16 sci_flags = MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_HIGH;
107 int length = 0;
108
Patrick Rudolph97b4c8a2024-10-23 15:19:46 +0200109 irqovr = current;
Andy Shevchenko13a5d872017-07-21 22:32:04 +0300110 length += acpi_create_madt_irqoverride(irqovr, 0, 0, 2, 0);
111
Patrick Rudolph97b4c8a2024-10-23 15:19:46 +0200112 irqovr = current + length;
Andy Shevchenko13a5d872017-07-21 22:32:04 +0300113 length += acpi_create_madt_irqoverride(irqovr, 0, 9, 9, sci_flags);
114
115 return length;
116}
117
Patrick Rudolph97b4c8a2024-10-23 15:19:46 +0200118__weak void *acpi_fill_madt(struct acpi_madt *madt, struct acpi_ctx *ctx)
Andy Shevchenko13a5d872017-07-21 22:32:04 +0300119{
Patrick Rudolph97b4c8a2024-10-23 15:19:46 +0200120 void *current = ctx->current;
121
122 madt->lapic_addr = LAPIC_DEFAULT_BASE;
123 madt->flags = ACPI_MADT_PCAT_COMPAT;
124
Andy Shevchenko13a5d872017-07-21 22:32:04 +0300125 current += acpi_create_madt_lapics(current);
126
127 current += acpi_create_madt_ioapic((struct acpi_madt_ioapic *)current,
128 io_apic_read(IO_APIC_ID) >> 24, IO_APIC_ADDR, 0);
129
130 current += acpi_create_madt_irq_overrides(current);
131
132 return current;
133}
Saket Sinha331141a2015-08-22 12:20:55 +0530134
Simon Glass28026282020-09-22 12:45:33 -0600135/**
136 * acpi_create_tcpa() - Create a TCPA table
137 *
Simon Glass28026282020-09-22 12:45:33 -0600138 * Trusted Computing Platform Alliance Capabilities Table
139 * TCPA PC Specific Implementation SpecificationTCPA is defined in the PCI
140 * Firmware Specification 3.0
141 */
Simon Glassbb3b6082021-12-01 09:02:59 -0700142int acpi_write_tcpa(struct acpi_ctx *ctx, const struct acpi_writer *entry)
Simon Glass28026282020-09-22 12:45:33 -0600143{
Simon Glassbb3b6082021-12-01 09:02:59 -0700144 struct acpi_table_header *header;
145 struct acpi_tcpa *tcpa;
146 u32 current;
Simon Glass28026282020-09-22 12:45:33 -0600147 int size = 0x10000; /* Use this as the default size */
148 void *log;
149 int ret;
150
Simon Glassbb3b6082021-12-01 09:02:59 -0700151 if (!IS_ENABLED(CONFIG_TPM_V1))
152 return -ENOENT;
Simon Glass28026282020-09-22 12:45:33 -0600153 if (!CONFIG_IS_ENABLED(BLOBLIST))
154 return -ENXIO;
Simon Glassbb3b6082021-12-01 09:02:59 -0700155
156 tcpa = ctx->current;
157 header = &tcpa->header;
Simon Glass28026282020-09-22 12:45:33 -0600158 memset(tcpa, '\0', sizeof(struct acpi_tcpa));
159
160 /* Fill out header fields */
161 acpi_fill_header(header, "TCPA");
162 header->length = sizeof(struct acpi_tcpa);
163 header->revision = 1;
164
165 ret = bloblist_ensure_size_ret(BLOBLISTT_TCPA_LOG, &size, &log);
166 if (ret)
167 return log_msg_ret("blob", ret);
168
169 tcpa->platform_class = 0;
170 tcpa->laml = size;
Simon Glass919f8352023-12-31 08:25:54 -0700171 tcpa->lasa = nomap_to_sysmem(log);
Simon Glass28026282020-09-22 12:45:33 -0600172
173 /* (Re)calculate length and checksum */
Simon Glassbb3b6082021-12-01 09:02:59 -0700174 current = (u32)tcpa + sizeof(struct acpi_tcpa);
Simon Glass28026282020-09-22 12:45:33 -0600175 header->length = current - (u32)tcpa;
Simon Glassbb3b6082021-12-01 09:02:59 -0700176 header->checksum = table_compute_checksum(tcpa, header->length);
177
178 acpi_inc(ctx, tcpa->header.length);
179 acpi_add_table(ctx, tcpa);
Simon Glass28026282020-09-22 12:45:33 -0600180
181 return 0;
182}
Simon Glassbb3b6082021-12-01 09:02:59 -0700183ACPI_WRITER(5tcpa, "TCPA", acpi_write_tcpa, 0);
Simon Glass28026282020-09-22 12:45:33 -0600184
Simon Glass272a7032020-09-22 12:45:32 -0600185static int get_tpm2_log(void **ptrp, int *sizep)
186{
187 const int tpm2_default_log_len = 0x10000;
188 int size;
189 int ret;
190
191 *sizep = 0;
192 size = tpm2_default_log_len;
193 ret = bloblist_ensure_size_ret(BLOBLISTT_TPM2_TCG_LOG, &size, ptrp);
194 if (ret)
195 return log_msg_ret("blob", ret);
196 *sizep = size;
197
198 return 0;
199}
200
Simon Glassc7536942021-12-01 09:02:57 -0700201static int acpi_write_tpm2(struct acpi_ctx *ctx,
202 const struct acpi_writer *entry)
Simon Glass272a7032020-09-22 12:45:32 -0600203{
Simon Glassc7536942021-12-01 09:02:57 -0700204 struct acpi_table_header *header;
205 struct acpi_tpm2 *tpm2;
Simon Glass272a7032020-09-22 12:45:32 -0600206 int tpm2_log_len;
207 void *lasa;
208 int ret;
209
Simon Glassc7536942021-12-01 09:02:57 -0700210 if (!IS_ENABLED(CONFIG_TPM_V2))
211 return log_msg_ret("none", -ENOENT);
212
213 tpm2 = ctx->current;
214 header = &tpm2->header;
215 memset(tpm2, '\0', sizeof(struct acpi_tpm2));
Simon Glass272a7032020-09-22 12:45:32 -0600216
217 /*
218 * Some payloads like SeaBIOS depend on log area to use TPM2.
219 * Get the memory size and address of TPM2 log area or initialize it.
220 */
221 ret = get_tpm2_log(&lasa, &tpm2_log_len);
222 if (ret)
Simon Glassc7536942021-12-01 09:02:57 -0700223 return log_msg_ret("log", ret);
Simon Glass272a7032020-09-22 12:45:32 -0600224
225 /* Fill out header fields. */
226 acpi_fill_header(header, "TPM2");
Heinrich Schuchardt10de8a82024-01-21 12:52:48 +0100227 memcpy(header->creator_id, ASLC_ID, 4);
Simon Glass272a7032020-09-22 12:45:32 -0600228
229 header->length = sizeof(struct acpi_tpm2);
230 header->revision = acpi_get_table_revision(ACPITAB_TPM2);
231
Simon Glassc7536942021-12-01 09:02:57 -0700232 /* Hard to detect for U-Boot. Just set it to 0 */
Simon Glass272a7032020-09-22 12:45:32 -0600233 tpm2->platform_class = 0;
234
235 /* Must be set to 0 for FIFO-interface support */
236 tpm2->control_area = 0;
237 tpm2->start_method = 6;
238 memset(tpm2->msp, 0, sizeof(tpm2->msp));
239
240 /* Fill the log area size and start address fields. */
241 tpm2->laml = tpm2_log_len;
Simon Glass919f8352023-12-31 08:25:54 -0700242 tpm2->lasa = nomap_to_sysmem(lasa);
Simon Glass272a7032020-09-22 12:45:32 -0600243
244 /* Calculate checksum. */
Simon Glassc7536942021-12-01 09:02:57 -0700245 header->checksum = table_compute_checksum(tpm2, header->length);
246
247 acpi_inc(ctx, tpm2->header.length);
248 acpi_add_table(ctx, tpm2);
Simon Glass272a7032020-09-22 12:45:32 -0600249
250 return 0;
251}
Simon Glassc7536942021-12-01 09:02:57 -0700252ACPI_WRITER(5tpm2, "TPM2", acpi_write_tpm2, 0);
Simon Glass272a7032020-09-22 12:45:32 -0600253
Simon Glassd2a98eb2021-12-01 09:02:53 -0700254int acpi_write_gnvs(struct acpi_ctx *ctx, const struct acpi_writer *entry)
Saket Sinha331141a2015-08-22 12:20:55 +0530255{
Simon Glass0e113842020-04-26 09:19:47 -0600256 ulong addr;
Saket Sinha331141a2015-08-22 12:20:55 +0530257
Simon Glass6fe570a2020-09-22 12:44:53 -0600258 if (!IS_ENABLED(CONFIG_ACPI_GNVS_EXTERNAL)) {
Simon Glassd2a98eb2021-12-01 09:02:53 -0700259 int i;
260
261 /* We need the DSDT to be done */
262 if (!ctx->dsdt)
263 return log_msg_ret("dsdt", -EAGAIN);
264
Simon Glass6fe570a2020-09-22 12:44:53 -0600265 /* Pack GNVS into the ACPI table area */
Simon Glass83c3cb52021-12-01 09:02:52 -0700266 for (i = 0; i < ctx->dsdt->length; i++) {
267 u32 *gnvs = (u32 *)((u32)ctx->dsdt + i);
Simon Glass0e113842020-04-26 09:19:47 -0600268
Simon Glass6fe570a2020-09-22 12:44:53 -0600269 if (*gnvs == ACPI_GNVS_ADDR) {
Simon Glass919f8352023-12-31 08:25:54 -0700270 *gnvs = nomap_to_sysmem(ctx->current);
Simon Glassd2a98eb2021-12-01 09:02:53 -0700271 log_debug("Fix up global NVS in DSDT to %#08x\n",
272 *gnvs);
Simon Glass6fe570a2020-09-22 12:44:53 -0600273 break;
274 }
Bin Mengd9050c62016-06-17 02:13:16 -0700275 }
Simon Glass6fe570a2020-09-22 12:44:53 -0600276
277 /*
Simon Glassd2a98eb2021-12-01 09:02:53 -0700278 * Recalculate the length and update the DSDT checksum since we
279 * patched the GNVS address. Set the checksum to zero since it
280 * is part of the region being checksummed.
Simon Glass6fe570a2020-09-22 12:44:53 -0600281 */
Simon Glassd2a98eb2021-12-01 09:02:53 -0700282 ctx->dsdt->checksum = 0;
283 ctx->dsdt->checksum = table_compute_checksum((void *)ctx->dsdt,
284 ctx->dsdt->length);
Bin Mengd9050c62016-06-17 02:13:16 -0700285 }
286
Simon Glassd2a98eb2021-12-01 09:02:53 -0700287 /* Fill in platform-specific global NVS variables */
Simon Glass9ed41e72020-07-07 21:32:05 -0600288 addr = acpi_create_gnvs(ctx->current);
289 if (IS_ERR_VALUE(addr))
Simon Glassd2a98eb2021-12-01 09:02:53 -0700290 return log_msg_ret("gnvs", (int)addr);
Simon Glass9ed41e72020-07-07 21:32:05 -0600291
Simon Glass0e113842020-04-26 09:19:47 -0600292 acpi_inc_align(ctx, sizeof(struct acpi_global_nvs));
Bin Mengd9050c62016-06-17 02:13:16 -0700293
Simon Glassd2a98eb2021-12-01 09:02:53 -0700294 return 0;
295}
296ACPI_WRITER(4gnvs, "GNVS", acpi_write_gnvs, 0);
297
Simon Glass4ffe8b02020-09-22 12:45:09 -0600298/**
299 * acpi_write_hpet() - Write out a HPET table
300 *
301 * Write out the table for High-Precision Event Timers
302 *
303 * @hpet: Place to put HPET table
304 */
305static int acpi_create_hpet(struct acpi_hpet *hpet)
306{
307 struct acpi_table_header *header = &hpet->header;
308 struct acpi_gen_regaddr *addr = &hpet->addr;
309
310 /*
311 * See IA-PC HPET (High Precision Event Timers) Specification v1.0a
312 * https://www.intel.com/content/dam/www/public/us/en/documents/technical-specifications/software-developers-hpet-spec-1-0a.pdf
313 */
314 memset((void *)hpet, '\0', sizeof(struct acpi_hpet));
315
316 /* Fill out header fields. */
317 acpi_fill_header(header, "HPET");
318
Simon Glass4ffe8b02020-09-22 12:45:09 -0600319 header->length = sizeof(struct acpi_hpet);
320 header->revision = acpi_get_table_revision(ACPITAB_HPET);
321
322 /* Fill out HPET address */
323 addr->space_id = 0; /* Memory */
324 addr->bit_width = 64;
325 addr->bit_offset = 0;
326 addr->addrl = CONFIG_HPET_ADDRESS & 0xffffffff;
327 addr->addrh = ((unsigned long long)CONFIG_HPET_ADDRESS) >> 32;
328
329 hpet->id = *(u32 *)CONFIG_HPET_ADDRESS;
330 hpet->number = 0;
331 hpet->min_tick = 0; /* HPET_MIN_TICKS */
332
333 header->checksum = table_compute_checksum(hpet,
334 sizeof(struct acpi_hpet));
335
336 return 0;
337}
338
339int acpi_write_hpet(struct acpi_ctx *ctx)
340{
341 struct acpi_hpet *hpet;
342 int ret;
343
344 log_debug("ACPI: * HPET\n");
345
346 hpet = ctx->current;
347 acpi_inc_align(ctx, sizeof(struct acpi_hpet));
348 acpi_create_hpet(hpet);
349 ret = acpi_add_table(ctx, hpet);
350 if (ret)
351 return log_msg_ret("add", ret);
352
353 return 0;
354}
Simon Glass95971892020-09-22 12:45:10 -0600355
Simon Glass87cf8d22020-09-22 12:45:16 -0600356void acpi_create_dmar_drhd(struct acpi_ctx *ctx, uint flags, uint segment,
357 u64 bar)
358{
359 struct dmar_entry *drhd = ctx->current;
360
361 memset(drhd, '\0', sizeof(*drhd));
362 drhd->type = DMAR_DRHD;
363 drhd->length = sizeof(*drhd); /* will be fixed up later */
364 drhd->flags = flags;
365 drhd->segment = segment;
366 drhd->bar = bar;
367 acpi_inc(ctx, drhd->length);
368}
369
370void acpi_create_dmar_rmrr(struct acpi_ctx *ctx, uint segment, u64 bar,
371 u64 limit)
372{
373 struct dmar_rmrr_entry *rmrr = ctx->current;
374
375 memset(rmrr, '\0', sizeof(*rmrr));
376 rmrr->type = DMAR_RMRR;
377 rmrr->length = sizeof(*rmrr); /* will be fixed up later */
378 rmrr->segment = segment;
379 rmrr->bar = bar;
380 rmrr->limit = limit;
381 acpi_inc(ctx, rmrr->length);
382}
383
384void acpi_dmar_drhd_fixup(struct acpi_ctx *ctx, void *base)
385{
386 struct dmar_entry *drhd = base;
387
388 drhd->length = ctx->current - base;
389}
390
391void acpi_dmar_rmrr_fixup(struct acpi_ctx *ctx, void *base)
392{
393 struct dmar_rmrr_entry *rmrr = base;
394
395 rmrr->length = ctx->current - base;
396}
397
398static int acpi_create_dmar_ds(struct acpi_ctx *ctx, enum dev_scope_type type,
399 uint enumeration_id, pci_dev_t bdf)
400{
401 /* we don't support longer paths yet */
402 const size_t dev_scope_length = sizeof(struct dev_scope) + 2;
403 struct dev_scope *ds = ctx->current;
404
405 memset(ds, '\0', dev_scope_length);
406 ds->type = type;
407 ds->length = dev_scope_length;
408 ds->enumeration = enumeration_id;
409 ds->start_bus = PCI_BUS(bdf);
410 ds->path[0].dev = PCI_DEV(bdf);
411 ds->path[0].fn = PCI_FUNC(bdf);
412
413 return ds->length;
414}
415
416int acpi_create_dmar_ds_pci_br(struct acpi_ctx *ctx, pci_dev_t bdf)
417{
418 return acpi_create_dmar_ds(ctx, SCOPE_PCI_SUB, 0, bdf);
419}
420
421int acpi_create_dmar_ds_pci(struct acpi_ctx *ctx, pci_dev_t bdf)
422{
423 return acpi_create_dmar_ds(ctx, SCOPE_PCI_ENDPOINT, 0, bdf);
424}
425
426int acpi_create_dmar_ds_ioapic(struct acpi_ctx *ctx, uint enumeration_id,
427 pci_dev_t bdf)
428{
429 return acpi_create_dmar_ds(ctx, SCOPE_IOAPIC, enumeration_id, bdf);
430}
431
432int acpi_create_dmar_ds_msi_hpet(struct acpi_ctx *ctx, uint enumeration_id,
433 pci_dev_t bdf)
434{
435 return acpi_create_dmar_ds(ctx, SCOPE_MSI_HPET, enumeration_id, bdf);
436}