blob: f5045cde3ef8f0ce5bed8190086bdf4bc917ffe7 [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
Saket Sinha331141a2015-08-22 12:20:55 +053011#include <common.h>
Simon Glass272a7032020-09-22 12:45:32 -060012#include <bloblist.h>
Saket Sinha331141a2015-08-22 12:20:55 +053013#include <cpu.h>
14#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Saket Sinha331141a2015-08-22 12:20:55 +053016#include <dm/uclass-internal.h>
Simon Glass0e113842020-04-26 09:19:47 -060017#include <mapmem.h>
Andy Shevchenko4ca48c92018-11-20 23:52:38 +020018#include <serial.h>
Simon Glassf0a8d682020-07-07 13:12:07 -060019#include <acpi/acpigen.h>
Simon Glass95971892020-09-22 12:45:10 -060020#include <acpi/acpi_device.h>
Simon Glass858fed12020-04-08 16:57:36 -060021#include <acpi/acpi_table.h>
Bin Mengd9050c62016-06-17 02:13:16 -070022#include <asm/acpi/global_nvs.h>
Andy Shevchenko13a5d872017-07-21 22:32:04 +030023#include <asm/ioapic.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060024#include <asm/global_data.h>
Saket Sinha331141a2015-08-22 12:20:55 +053025#include <asm/lapic.h>
Andy Shevchenko13a5d872017-07-21 22:32:04 +030026#include <asm/mpspec.h>
Saket Sinha331141a2015-08-22 12:20:55 +053027#include <asm/tables.h>
Bin Mengd9050c62016-06-17 02:13:16 -070028#include <asm/arch/global_nvs.h>
Simon Glass0e113842020-04-26 09:19:47 -060029#include <dm/acpi.h>
Simon Glass9ed41e72020-07-07 21:32:05 -060030#include <linux/err.h>
Saket Sinha331141a2015-08-22 12:20:55 +053031
Andy Shevchenko66d3e632018-01-10 19:40:15 +020032/* ACPI RSDP address to be used in boot parameters */
Bin Menge1029252018-01-30 05:01:16 -080033static ulong acpi_rsdp_addr;
Andy Shevchenko66d3e632018-01-10 19:40:15 +020034
Saket Sinha331141a2015-08-22 12:20:55 +053035static int acpi_create_madt_lapic(struct acpi_madt_lapic *lapic,
Bin Meng44256b02016-05-07 07:46:25 -070036 u8 cpu, u8 apic)
Saket Sinha331141a2015-08-22 12:20:55 +053037{
Bin Meng44256b02016-05-07 07:46:25 -070038 lapic->type = ACPI_APIC_LAPIC;
Saket Sinha331141a2015-08-22 12:20:55 +053039 lapic->length = sizeof(struct acpi_madt_lapic);
Bin Meng44256b02016-05-07 07:46:25 -070040 lapic->flags = LOCAL_APIC_FLAG_ENABLED;
Saket Sinha331141a2015-08-22 12:20:55 +053041 lapic->processor_id = cpu;
42 lapic->apic_id = apic;
43
44 return lapic->length;
45}
46
Bin Meng3c5234e2016-05-07 07:46:30 -070047int acpi_create_madt_lapics(u32 current)
Saket Sinha331141a2015-08-22 12:20:55 +053048{
49 struct udevice *dev;
George McCollister5a49f872016-06-07 13:40:18 -050050 int total_length = 0;
Simon Glassfcae5472020-09-22 12:45:31 -060051 int cpu_num = 0;
Saket Sinha331141a2015-08-22 12:20:55 +053052
53 for (uclass_find_first_device(UCLASS_CPU, &dev);
54 dev;
55 uclass_find_next_device(&dev)) {
Simon Glassb75b15b2020-12-03 16:55:23 -070056 struct cpu_plat *plat = dev_get_parent_plat(dev);
Simon Glassfcae5472020-09-22 12:45:31 -060057 int length;
58
59 length = acpi_create_madt_lapic(
60 (struct acpi_madt_lapic *)current, cpu_num++,
61 plat->cpu_id);
Bin Meng3c5234e2016-05-07 07:46:30 -070062 current += length;
George McCollister5a49f872016-06-07 13:40:18 -050063 total_length += length;
Bin Meng44256b02016-05-07 07:46:25 -070064 }
65
George McCollister5a49f872016-06-07 13:40:18 -050066 return total_length;
Saket Sinha331141a2015-08-22 12:20:55 +053067}
68
Bin Meng44256b02016-05-07 07:46:25 -070069int acpi_create_madt_ioapic(struct acpi_madt_ioapic *ioapic, u8 id,
70 u32 addr, u32 gsi_base)
Saket Sinha331141a2015-08-22 12:20:55 +053071{
Bin Meng6a421582016-05-07 07:46:21 -070072 ioapic->type = ACPI_APIC_IOAPIC;
Saket Sinha331141a2015-08-22 12:20:55 +053073 ioapic->length = sizeof(struct acpi_madt_ioapic);
74 ioapic->reserved = 0x00;
75 ioapic->gsi_base = gsi_base;
76 ioapic->ioapic_id = id;
77 ioapic->ioapic_addr = addr;
78
79 return ioapic->length;
80}
81
82int acpi_create_madt_irqoverride(struct acpi_madt_irqoverride *irqoverride,
Bin Meng44256b02016-05-07 07:46:25 -070083 u8 bus, u8 source, u32 gsirq, u16 flags)
Saket Sinha331141a2015-08-22 12:20:55 +053084{
Bin Meng6a421582016-05-07 07:46:21 -070085 irqoverride->type = ACPI_APIC_IRQ_SRC_OVERRIDE;
Saket Sinha331141a2015-08-22 12:20:55 +053086 irqoverride->length = sizeof(struct acpi_madt_irqoverride);
87 irqoverride->bus = bus;
88 irqoverride->source = source;
89 irqoverride->gsirq = gsirq;
90 irqoverride->flags = flags;
91
92 return irqoverride->length;
93}
94
95int acpi_create_madt_lapic_nmi(struct acpi_madt_lapic_nmi *lapic_nmi,
Bin Meng44256b02016-05-07 07:46:25 -070096 u8 cpu, u16 flags, u8 lint)
Saket Sinha331141a2015-08-22 12:20:55 +053097{
Bin Meng6a421582016-05-07 07:46:21 -070098 lapic_nmi->type = ACPI_APIC_LAPIC_NMI;
Saket Sinha331141a2015-08-22 12:20:55 +053099 lapic_nmi->length = sizeof(struct acpi_madt_lapic_nmi);
100 lapic_nmi->flags = flags;
101 lapic_nmi->processor_id = cpu;
102 lapic_nmi->lint = lint;
103
104 return lapic_nmi->length;
105}
106
Andy Shevchenko13a5d872017-07-21 22:32:04 +0300107static int acpi_create_madt_irq_overrides(u32 current)
108{
109 struct acpi_madt_irqoverride *irqovr;
110 u16 sci_flags = MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_HIGH;
111 int length = 0;
112
113 irqovr = (void *)current;
114 length += acpi_create_madt_irqoverride(irqovr, 0, 0, 2, 0);
115
116 irqovr = (void *)(current + length);
117 length += acpi_create_madt_irqoverride(irqovr, 0, 9, 9, sci_flags);
118
119 return length;
120}
121
122__weak u32 acpi_fill_madt(u32 current)
123{
124 current += acpi_create_madt_lapics(current);
125
126 current += acpi_create_madt_ioapic((struct acpi_madt_ioapic *)current,
127 io_apic_read(IO_APIC_ID) >> 24, IO_APIC_ADDR, 0);
128
129 current += acpi_create_madt_irq_overrides(current);
130
131 return current;
132}
133
Simon Glassb0d5fab2021-12-01 09:02:58 -0700134int acpi_write_madt(struct acpi_ctx *ctx, const struct acpi_writer *entry)
Saket Sinha331141a2015-08-22 12:20:55 +0530135{
Simon Glassb0d5fab2021-12-01 09:02:58 -0700136 struct acpi_table_header *header;
137 struct acpi_madt *madt;
138 u32 current;
Saket Sinha331141a2015-08-22 12:20:55 +0530139
Simon Glassb0d5fab2021-12-01 09:02:58 -0700140 madt = ctx->current;
141
142 memset(madt, '\0', sizeof(struct acpi_madt));
143 header = &madt->header;
Saket Sinha331141a2015-08-22 12:20:55 +0530144
145 /* Fill out header fields */
Bin Mengb063d5f2016-05-07 07:46:24 -0700146 acpi_fill_header(header, "APIC");
Saket Sinha331141a2015-08-22 12:20:55 +0530147 header->length = sizeof(struct acpi_madt);
Simon Glassf3694aa2020-07-16 21:22:37 -0600148 header->revision = ACPI_MADT_REV_ACPI_3_0;
Saket Sinha331141a2015-08-22 12:20:55 +0530149
150 madt->lapic_addr = LAPIC_DEFAULT_BASE;
Bin Meng6a421582016-05-07 07:46:21 -0700151 madt->flags = ACPI_MADT_PCAT_COMPAT;
Saket Sinha331141a2015-08-22 12:20:55 +0530152
Simon Glassb0d5fab2021-12-01 09:02:58 -0700153 current = (u32)madt + sizeof(struct acpi_madt);
Saket Sinha331141a2015-08-22 12:20:55 +0530154 current = acpi_fill_madt(current);
155
156 /* (Re)calculate length and checksum */
Bin Menga1ec7db2016-05-07 07:46:26 -0700157 header->length = current - (u32)madt;
Saket Sinha331141a2015-08-22 12:20:55 +0530158
159 header->checksum = table_compute_checksum((void *)madt, header->length);
Simon Glassb0d5fab2021-12-01 09:02:58 -0700160 acpi_add_table(ctx, madt);
161 acpi_inc(ctx, madt->header.length);
162
163 return 0;
Saket Sinha331141a2015-08-22 12:20:55 +0530164}
Simon Glassb0d5fab2021-12-01 09:02:58 -0700165ACPI_WRITER(5x86, NULL, acpi_write_madt, 0);
Saket Sinha331141a2015-08-22 12:20:55 +0530166
Andy Shevchenkoc1ae9802017-07-21 22:32:05 +0300167int acpi_create_mcfg_mmconfig(struct acpi_mcfg_mmconfig *mmconfig, u32 base,
168 u16 seg_nr, u8 start, u8 end)
Saket Sinha331141a2015-08-22 12:20:55 +0530169{
170 memset(mmconfig, 0, sizeof(*mmconfig));
Bin Meng6a421582016-05-07 07:46:21 -0700171 mmconfig->base_address_l = base;
172 mmconfig->base_address_h = 0;
Saket Sinha331141a2015-08-22 12:20:55 +0530173 mmconfig->pci_segment_group_number = seg_nr;
174 mmconfig->start_bus_number = start;
175 mmconfig->end_bus_number = end;
176
177 return sizeof(struct acpi_mcfg_mmconfig);
178}
179
Andy Shevchenkoc1ae9802017-07-21 22:32:05 +0300180__weak u32 acpi_fill_mcfg(u32 current)
Saket Sinha331141a2015-08-22 12:20:55 +0530181{
182 current += acpi_create_mcfg_mmconfig
183 ((struct acpi_mcfg_mmconfig *)current,
Bin Meng44256b02016-05-07 07:46:25 -0700184 CONFIG_PCIE_ECAM_BASE, 0x0, 0x0, 255);
Saket Sinha331141a2015-08-22 12:20:55 +0530185
186 return current;
187}
188
Simon Glass28026282020-09-22 12:45:33 -0600189/**
190 * acpi_create_tcpa() - Create a TCPA table
191 *
Simon Glass28026282020-09-22 12:45:33 -0600192 * Trusted Computing Platform Alliance Capabilities Table
193 * TCPA PC Specific Implementation SpecificationTCPA is defined in the PCI
194 * Firmware Specification 3.0
195 */
Simon Glassbb3b6082021-12-01 09:02:59 -0700196int acpi_write_tcpa(struct acpi_ctx *ctx, const struct acpi_writer *entry)
Simon Glass28026282020-09-22 12:45:33 -0600197{
Simon Glassbb3b6082021-12-01 09:02:59 -0700198 struct acpi_table_header *header;
199 struct acpi_tcpa *tcpa;
200 u32 current;
Simon Glass28026282020-09-22 12:45:33 -0600201 int size = 0x10000; /* Use this as the default size */
202 void *log;
203 int ret;
204
Simon Glassbb3b6082021-12-01 09:02:59 -0700205 if (!IS_ENABLED(CONFIG_TPM_V1))
206 return -ENOENT;
Simon Glass28026282020-09-22 12:45:33 -0600207 if (!CONFIG_IS_ENABLED(BLOBLIST))
208 return -ENXIO;
Simon Glassbb3b6082021-12-01 09:02:59 -0700209
210 tcpa = ctx->current;
211 header = &tcpa->header;
Simon Glass28026282020-09-22 12:45:33 -0600212 memset(tcpa, '\0', sizeof(struct acpi_tcpa));
213
214 /* Fill out header fields */
215 acpi_fill_header(header, "TCPA");
216 header->length = sizeof(struct acpi_tcpa);
217 header->revision = 1;
218
219 ret = bloblist_ensure_size_ret(BLOBLISTT_TCPA_LOG, &size, &log);
220 if (ret)
221 return log_msg_ret("blob", ret);
222
223 tcpa->platform_class = 0;
224 tcpa->laml = size;
Simon Glassbb3b6082021-12-01 09:02:59 -0700225 tcpa->lasa = map_to_sysmem(log);
Simon Glass28026282020-09-22 12:45:33 -0600226
227 /* (Re)calculate length and checksum */
Simon Glassbb3b6082021-12-01 09:02:59 -0700228 current = (u32)tcpa + sizeof(struct acpi_tcpa);
Simon Glass28026282020-09-22 12:45:33 -0600229 header->length = current - (u32)tcpa;
Simon Glassbb3b6082021-12-01 09:02:59 -0700230 header->checksum = table_compute_checksum(tcpa, header->length);
231
232 acpi_inc(ctx, tcpa->header.length);
233 acpi_add_table(ctx, tcpa);
Simon Glass28026282020-09-22 12:45:33 -0600234
235 return 0;
236}
Simon Glassbb3b6082021-12-01 09:02:59 -0700237ACPI_WRITER(5tcpa, "TCPA", acpi_write_tcpa, 0);
Simon Glass28026282020-09-22 12:45:33 -0600238
Simon Glass272a7032020-09-22 12:45:32 -0600239static int get_tpm2_log(void **ptrp, int *sizep)
240{
241 const int tpm2_default_log_len = 0x10000;
242 int size;
243 int ret;
244
245 *sizep = 0;
246 size = tpm2_default_log_len;
247 ret = bloblist_ensure_size_ret(BLOBLISTT_TPM2_TCG_LOG, &size, ptrp);
248 if (ret)
249 return log_msg_ret("blob", ret);
250 *sizep = size;
251
252 return 0;
253}
254
Simon Glassc7536942021-12-01 09:02:57 -0700255static int acpi_write_tpm2(struct acpi_ctx *ctx,
256 const struct acpi_writer *entry)
Simon Glass272a7032020-09-22 12:45:32 -0600257{
Simon Glassc7536942021-12-01 09:02:57 -0700258 struct acpi_table_header *header;
259 struct acpi_tpm2 *tpm2;
Simon Glass272a7032020-09-22 12:45:32 -0600260 int tpm2_log_len;
261 void *lasa;
262 int ret;
263
Simon Glassc7536942021-12-01 09:02:57 -0700264 if (!IS_ENABLED(CONFIG_TPM_V2))
265 return log_msg_ret("none", -ENOENT);
266
267 tpm2 = ctx->current;
268 header = &tpm2->header;
269 memset(tpm2, '\0', sizeof(struct acpi_tpm2));
Simon Glass272a7032020-09-22 12:45:32 -0600270
271 /*
272 * Some payloads like SeaBIOS depend on log area to use TPM2.
273 * Get the memory size and address of TPM2 log area or initialize it.
274 */
275 ret = get_tpm2_log(&lasa, &tpm2_log_len);
276 if (ret)
Simon Glassc7536942021-12-01 09:02:57 -0700277 return log_msg_ret("log", ret);
Simon Glass272a7032020-09-22 12:45:32 -0600278
279 /* Fill out header fields. */
280 acpi_fill_header(header, "TPM2");
281 memcpy(header->aslc_id, ASLC_ID, 4);
282
283 header->length = sizeof(struct acpi_tpm2);
284 header->revision = acpi_get_table_revision(ACPITAB_TPM2);
285
Simon Glassc7536942021-12-01 09:02:57 -0700286 /* Hard to detect for U-Boot. Just set it to 0 */
Simon Glass272a7032020-09-22 12:45:32 -0600287 tpm2->platform_class = 0;
288
289 /* Must be set to 0 for FIFO-interface support */
290 tpm2->control_area = 0;
291 tpm2->start_method = 6;
292 memset(tpm2->msp, 0, sizeof(tpm2->msp));
293
294 /* Fill the log area size and start address fields. */
295 tpm2->laml = tpm2_log_len;
Simon Glassc7536942021-12-01 09:02:57 -0700296 tpm2->lasa = map_to_sysmem(lasa);
Simon Glass272a7032020-09-22 12:45:32 -0600297
298 /* Calculate checksum. */
Simon Glassc7536942021-12-01 09:02:57 -0700299 header->checksum = table_compute_checksum(tpm2, header->length);
300
301 acpi_inc(ctx, tpm2->header.length);
302 acpi_add_table(ctx, tpm2);
Simon Glass272a7032020-09-22 12:45:32 -0600303
304 return 0;
305}
Simon Glassc7536942021-12-01 09:02:57 -0700306ACPI_WRITER(5tpm2, "TPM2", acpi_write_tpm2, 0);
Simon Glass272a7032020-09-22 12:45:32 -0600307
Andy Shevchenko607dbd12019-07-14 19:23:57 +0300308__weak u32 acpi_fill_csrt(u32 current)
309{
Simon Glass9eb80042020-07-07 21:32:24 -0600310 return 0;
Andy Shevchenko607dbd12019-07-14 19:23:57 +0300311}
312
Simon Glass9eb80042020-07-07 21:32:24 -0600313static int acpi_create_csrt(struct acpi_csrt *csrt)
Andy Shevchenko607dbd12019-07-14 19:23:57 +0300314{
315 struct acpi_table_header *header = &(csrt->header);
316 u32 current = (u32)csrt + sizeof(struct acpi_csrt);
Simon Glass9eb80042020-07-07 21:32:24 -0600317 uint ptr;
Andy Shevchenko607dbd12019-07-14 19:23:57 +0300318
319 memset((void *)csrt, 0, sizeof(struct acpi_csrt));
320
321 /* Fill out header fields */
322 acpi_fill_header(header, "CSRT");
323 header->length = sizeof(struct acpi_csrt);
324 header->revision = 0;
325
Simon Glass9eb80042020-07-07 21:32:24 -0600326 ptr = acpi_fill_csrt(current);
327 if (!ptr)
328 return -ENOENT;
329 current = ptr;
Andy Shevchenko607dbd12019-07-14 19:23:57 +0300330
331 /* (Re)calculate length and checksum */
332 header->length = current - (u32)csrt;
333 header->checksum = table_compute_checksum((void *)csrt, header->length);
Simon Glass9eb80042020-07-07 21:32:24 -0600334
335 return 0;
Andy Shevchenko607dbd12019-07-14 19:23:57 +0300336}
337
Andy Shevchenko4ca48c92018-11-20 23:52:38 +0200338static void acpi_create_spcr(struct acpi_spcr *spcr)
339{
340 struct acpi_table_header *header = &(spcr->header);
341 struct serial_device_info serial_info = {0};
342 ulong serial_address, serial_offset;
Simon Glassdaaff932018-12-28 14:23:08 -0700343 struct udevice *dev;
Andy Shevchenko4ca48c92018-11-20 23:52:38 +0200344 uint serial_config;
345 uint serial_width;
346 int access_size;
347 int space_id;
Andy Shevchenkobf9c8e32019-02-28 17:19:54 +0200348 int ret = -ENODEV;
Andy Shevchenko4ca48c92018-11-20 23:52:38 +0200349
Wolfgang Wallner13c23e92020-09-16 16:57:53 +0200350 memset((void *)spcr, 0, sizeof(struct acpi_spcr));
351
Andy Shevchenko4ca48c92018-11-20 23:52:38 +0200352 /* Fill out header fields */
353 acpi_fill_header(header, "SPCR");
354 header->length = sizeof(struct acpi_spcr);
355 header->revision = 2;
356
Simon Glass896c1642018-12-28 14:23:10 -0700357 /* Read the device once, here. It is reused below */
Andy Shevchenkobf9c8e32019-02-28 17:19:54 +0200358 dev = gd->cur_serial_dev;
359 if (dev)
Simon Glass896c1642018-12-28 14:23:10 -0700360 ret = serial_getinfo(dev, &serial_info);
Andy Shevchenko4ca48c92018-11-20 23:52:38 +0200361 if (ret)
362 serial_info.type = SERIAL_CHIP_UNKNOWN;
363
364 /* Encode chip type */
365 switch (serial_info.type) {
366 case SERIAL_CHIP_16550_COMPATIBLE:
367 spcr->interface_type = ACPI_DBG2_16550_COMPATIBLE;
368 break;
369 case SERIAL_CHIP_UNKNOWN:
370 default:
371 spcr->interface_type = ACPI_DBG2_UNKNOWN;
372 break;
373 }
374
375 /* Encode address space */
376 switch (serial_info.addr_space) {
377 case SERIAL_ADDRESS_SPACE_MEMORY:
378 space_id = ACPI_ADDRESS_SPACE_MEMORY;
379 break;
380 case SERIAL_ADDRESS_SPACE_IO:
381 default:
382 space_id = ACPI_ADDRESS_SPACE_IO;
383 break;
384 }
385
386 serial_width = serial_info.reg_width * 8;
387 serial_offset = serial_info.reg_offset << serial_info.reg_shift;
388 serial_address = serial_info.addr + serial_offset;
389
390 /* Encode register access size */
391 switch (serial_info.reg_shift) {
392 case 0:
393 access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS;
394 break;
395 case 1:
396 access_size = ACPI_ACCESS_SIZE_WORD_ACCESS;
397 break;
398 case 2:
399 access_size = ACPI_ACCESS_SIZE_DWORD_ACCESS;
400 break;
401 case 3:
402 access_size = ACPI_ACCESS_SIZE_QWORD_ACCESS;
403 break;
404 default:
405 access_size = ACPI_ACCESS_SIZE_UNDEFINED;
406 break;
407 }
408
409 debug("UART type %u @ %lx\n", spcr->interface_type, serial_address);
410
411 /* Fill GAS */
412 spcr->serial_port.space_id = space_id;
413 spcr->serial_port.bit_width = serial_width;
414 spcr->serial_port.bit_offset = 0;
415 spcr->serial_port.access_size = access_size;
416 spcr->serial_port.addrl = lower_32_bits(serial_address);
417 spcr->serial_port.addrh = upper_32_bits(serial_address);
418
419 /* Encode baud rate */
420 switch (serial_info.baudrate) {
421 case 9600:
422 spcr->baud_rate = 3;
423 break;
424 case 19200:
425 spcr->baud_rate = 4;
426 break;
427 case 57600:
428 spcr->baud_rate = 6;
429 break;
430 case 115200:
431 spcr->baud_rate = 7;
432 break;
433 default:
434 spcr->baud_rate = 0;
435 break;
436 }
437
Simon Glass896c1642018-12-28 14:23:10 -0700438 serial_config = SERIAL_DEFAULT_CONFIG;
439 if (dev)
Simon Glassdaaff932018-12-28 14:23:08 -0700440 ret = serial_getconfig(dev, &serial_config);
Andy Shevchenko4ca48c92018-11-20 23:52:38 +0200441
442 spcr->parity = SERIAL_GET_PARITY(serial_config);
443 spcr->stop_bits = SERIAL_GET_STOP(serial_config);
444
445 /* No PCI devices for now */
446 spcr->pci_device_id = 0xffff;
447 spcr->pci_vendor_id = 0xffff;
448
Andy Shevchenko225cc8a2020-02-27 17:21:56 +0200449 /*
450 * SPCR has no clue if the UART base clock speed is different
451 * to the default one. However, the SPCR 1.04 defines baud rate
452 * 0 as a preconfigured state of UART and OS is supposed not
453 * to touch the configuration of the serial device.
454 */
455 if (serial_info.clock != SERIAL_DEFAULT_CLOCK)
456 spcr->baud_rate = 0;
457
Andy Shevchenko4ca48c92018-11-20 23:52:38 +0200458 /* Fix checksum */
459 header->checksum = table_compute_checksum((void *)spcr, header->length);
460}
461
Simon Glassd2a98eb2021-12-01 09:02:53 -0700462int acpi_write_gnvs(struct acpi_ctx *ctx, const struct acpi_writer *entry)
Saket Sinha331141a2015-08-22 12:20:55 +0530463{
Simon Glass0e113842020-04-26 09:19:47 -0600464 ulong addr;
Saket Sinha331141a2015-08-22 12:20:55 +0530465
Simon Glass6fe570a2020-09-22 12:44:53 -0600466 if (!IS_ENABLED(CONFIG_ACPI_GNVS_EXTERNAL)) {
Simon Glassd2a98eb2021-12-01 09:02:53 -0700467 int i;
468
469 /* We need the DSDT to be done */
470 if (!ctx->dsdt)
471 return log_msg_ret("dsdt", -EAGAIN);
472
Simon Glass6fe570a2020-09-22 12:44:53 -0600473 /* Pack GNVS into the ACPI table area */
Simon Glass83c3cb52021-12-01 09:02:52 -0700474 for (i = 0; i < ctx->dsdt->length; i++) {
475 u32 *gnvs = (u32 *)((u32)ctx->dsdt + i);
Simon Glass0e113842020-04-26 09:19:47 -0600476
Simon Glass6fe570a2020-09-22 12:44:53 -0600477 if (*gnvs == ACPI_GNVS_ADDR) {
478 *gnvs = map_to_sysmem(ctx->current);
Simon Glassd2a98eb2021-12-01 09:02:53 -0700479 log_debug("Fix up global NVS in DSDT to %#08x\n",
480 *gnvs);
Simon Glass6fe570a2020-09-22 12:44:53 -0600481 break;
482 }
Bin Mengd9050c62016-06-17 02:13:16 -0700483 }
Simon Glass6fe570a2020-09-22 12:44:53 -0600484
485 /*
Simon Glassd2a98eb2021-12-01 09:02:53 -0700486 * Recalculate the length and update the DSDT checksum since we
487 * patched the GNVS address. Set the checksum to zero since it
488 * is part of the region being checksummed.
Simon Glass6fe570a2020-09-22 12:44:53 -0600489 */
Simon Glassd2a98eb2021-12-01 09:02:53 -0700490 ctx->dsdt->checksum = 0;
491 ctx->dsdt->checksum = table_compute_checksum((void *)ctx->dsdt,
492 ctx->dsdt->length);
Bin Mengd9050c62016-06-17 02:13:16 -0700493 }
494
Simon Glassd2a98eb2021-12-01 09:02:53 -0700495 /* Fill in platform-specific global NVS variables */
Simon Glass9ed41e72020-07-07 21:32:05 -0600496 addr = acpi_create_gnvs(ctx->current);
497 if (IS_ERR_VALUE(addr))
Simon Glassd2a98eb2021-12-01 09:02:53 -0700498 return log_msg_ret("gnvs", (int)addr);
Simon Glass9ed41e72020-07-07 21:32:05 -0600499
Simon Glass0e113842020-04-26 09:19:47 -0600500 acpi_inc_align(ctx, sizeof(struct acpi_global_nvs));
Bin Mengd9050c62016-06-17 02:13:16 -0700501
Simon Glassd2a98eb2021-12-01 09:02:53 -0700502 return 0;
503}
504ACPI_WRITER(4gnvs, "GNVS", acpi_write_gnvs, 0);
505
Simon Glass68000952021-12-01 09:02:56 -0700506/* MCFG is defined in the PCI Firmware Specification 3.0 */
507int acpi_write_mcfg(struct acpi_ctx *ctx, const struct acpi_writer *entry)
508{
509 struct acpi_table_header *header;
510 struct acpi_mcfg *mcfg;
511 u32 current;
512
513 mcfg = ctx->current;
514 header = &mcfg->header;
515
516 current = (u32)mcfg + sizeof(struct acpi_mcfg);
517
518 memset(mcfg, '\0', sizeof(struct acpi_mcfg));
519
520 /* Fill out header fields */
521 acpi_fill_header(header, "MCFG");
522 header->length = sizeof(struct acpi_mcfg);
523 header->revision = 1;
524
525 /* (Re)calculate length and checksum */
526 header->length = current - (u32)mcfg;
527 header->checksum = table_compute_checksum(mcfg, header->length);
528
529 acpi_inc(ctx, mcfg->header.length);
530 acpi_add_table(ctx, mcfg);
531
532 return 0;
533}
534ACPI_WRITER(5mcfg, "MCFG", acpi_write_mcfg, 0);
535
Simon Glassd2a98eb2021-12-01 09:02:53 -0700536/*
537 * QEMU's version of write_acpi_tables is defined in drivers/misc/qfw.c
538 */
539int write_acpi_tables_x86(struct acpi_ctx *ctx,
540 const struct acpi_writer *entry)
541{
Simon Glassd2a98eb2021-12-01 09:02:53 -0700542 struct acpi_csrt *csrt;
543 struct acpi_spcr *spcr;
Simon Glass28026282020-09-22 12:45:33 -0600544
Andy Shevchenko607dbd12019-07-14 19:23:57 +0300545 debug("ACPI: * CSRT\n");
Simon Glass0e113842020-04-26 09:19:47 -0600546 csrt = ctx->current;
Simon Glass9eb80042020-07-07 21:32:24 -0600547 if (!acpi_create_csrt(csrt)) {
548 acpi_inc_align(ctx, csrt->header.length);
549 acpi_add_table(ctx, csrt);
550 }
Andy Shevchenko607dbd12019-07-14 19:23:57 +0300551
Andy Shevchenko4ca48c92018-11-20 23:52:38 +0200552 debug("ACPI: * SPCR\n");
Simon Glass0e113842020-04-26 09:19:47 -0600553 spcr = ctx->current;
Andy Shevchenko4ca48c92018-11-20 23:52:38 +0200554 acpi_create_spcr(spcr);
Simon Glass0e113842020-04-26 09:19:47 -0600555 acpi_inc_align(ctx, spcr->header.length);
Simon Glass575a5472020-04-26 09:19:50 -0600556 acpi_add_table(ctx, spcr);
Andy Shevchenko4ca48c92018-11-20 23:52:38 +0200557
Simon Glass179fb822020-04-26 09:19:48 -0600558 acpi_write_dev_tables(ctx);
559
Simon Glass575a5472020-04-26 09:19:50 -0600560 acpi_rsdp_addr = (unsigned long)ctx->rsdp;
Bin Mengd2d22182016-05-07 07:46:12 -0700561 debug("ACPI: done\n");
Saket Sinha331141a2015-08-22 12:20:55 +0530562
Simon Glass16170882021-12-01 09:02:49 -0700563 return 0;
Saket Sinha331141a2015-08-22 12:20:55 +0530564}
Simon Glass83c3cb52021-12-01 09:02:52 -0700565ACPI_WRITER(9x86, NULL, write_acpi_tables_x86, 0);
Bin Meng34bc74a2017-04-21 07:24:36 -0700566
Bin Menge1029252018-01-30 05:01:16 -0800567ulong acpi_get_rsdp_addr(void)
568{
569 return acpi_rsdp_addr;
570}
Simon Glass4ffe8b02020-09-22 12:45:09 -0600571
572/**
573 * acpi_write_hpet() - Write out a HPET table
574 *
575 * Write out the table for High-Precision Event Timers
576 *
577 * @hpet: Place to put HPET table
578 */
579static int acpi_create_hpet(struct acpi_hpet *hpet)
580{
581 struct acpi_table_header *header = &hpet->header;
582 struct acpi_gen_regaddr *addr = &hpet->addr;
583
584 /*
585 * See IA-PC HPET (High Precision Event Timers) Specification v1.0a
586 * https://www.intel.com/content/dam/www/public/us/en/documents/technical-specifications/software-developers-hpet-spec-1-0a.pdf
587 */
588 memset((void *)hpet, '\0', sizeof(struct acpi_hpet));
589
590 /* Fill out header fields. */
591 acpi_fill_header(header, "HPET");
592
593 header->aslc_revision = ASL_REVISION;
594 header->length = sizeof(struct acpi_hpet);
595 header->revision = acpi_get_table_revision(ACPITAB_HPET);
596
597 /* Fill out HPET address */
598 addr->space_id = 0; /* Memory */
599 addr->bit_width = 64;
600 addr->bit_offset = 0;
601 addr->addrl = CONFIG_HPET_ADDRESS & 0xffffffff;
602 addr->addrh = ((unsigned long long)CONFIG_HPET_ADDRESS) >> 32;
603
604 hpet->id = *(u32 *)CONFIG_HPET_ADDRESS;
605 hpet->number = 0;
606 hpet->min_tick = 0; /* HPET_MIN_TICKS */
607
608 header->checksum = table_compute_checksum(hpet,
609 sizeof(struct acpi_hpet));
610
611 return 0;
612}
613
614int acpi_write_hpet(struct acpi_ctx *ctx)
615{
616 struct acpi_hpet *hpet;
617 int ret;
618
619 log_debug("ACPI: * HPET\n");
620
621 hpet = ctx->current;
622 acpi_inc_align(ctx, sizeof(struct acpi_hpet));
623 acpi_create_hpet(hpet);
624 ret = acpi_add_table(ctx, hpet);
625 if (ret)
626 return log_msg_ret("add", ret);
627
628 return 0;
629}
Simon Glass95971892020-09-22 12:45:10 -0600630
631int acpi_write_dbg2_pci_uart(struct acpi_ctx *ctx, struct udevice *dev,
632 uint access_size)
633{
634 struct acpi_dbg2_header *dbg2 = ctx->current;
635 char path[ACPI_PATH_MAX];
636 struct acpi_gen_regaddr address;
637 phys_addr_t addr;
638 int ret;
639
640 if (!device_active(dev)) {
641 log_info("Device not enabled\n");
642 return -EACCES;
643 }
644 /*
645 * PCI devices don't remember their resource allocation information in
646 * U-Boot at present. We assume that MMIO is used for the UART and that
647 * the address space is 32 bytes: ns16550 uses 8 registers of up to
648 * 32-bits each. This is only for debugging so it is not a big deal.
649 */
650 addr = dm_pci_read_bar32(dev, 0);
Simon Glass75081202020-11-04 09:57:41 -0700651 log_debug("UART addr %lx\n", (ulong)addr);
Simon Glass95971892020-09-22 12:45:10 -0600652
653 memset(&address, '\0', sizeof(address));
654 address.space_id = ACPI_ADDRESS_SPACE_MEMORY;
655 address.addrl = (uint32_t)addr;
656 address.addrh = (uint32_t)((addr >> 32) & 0xffffffff);
657 address.access_size = access_size;
658
659 ret = acpi_device_path(dev, path, sizeof(path));
660 if (ret)
661 return log_msg_ret("path", ret);
662 acpi_create_dbg2(dbg2, ACPI_DBG2_SERIAL_PORT,
663 ACPI_DBG2_16550_COMPATIBLE, &address, 0x1000, path);
664
665 acpi_inc_align(ctx, dbg2->header.length);
666 acpi_add_table(ctx, dbg2);
667
668 return 0;
669}
Simon Glass87cf8d22020-09-22 12:45:16 -0600670
671void acpi_fadt_common(struct acpi_fadt *fadt, struct acpi_facs *facs,
672 void *dsdt)
673{
674 struct acpi_table_header *header = &fadt->header;
675
676 memset((void *)fadt, '\0', sizeof(struct acpi_fadt));
677
678 acpi_fill_header(header, "FACP");
679 header->length = sizeof(struct acpi_fadt);
680 header->revision = 4;
681 memcpy(header->oem_id, OEM_ID, 6);
682 memcpy(header->oem_table_id, OEM_TABLE_ID, 8);
683 memcpy(header->aslc_id, ASLC_ID, 4);
684 header->aslc_revision = 1;
685
686 fadt->firmware_ctrl = (unsigned long)facs;
687 fadt->dsdt = (unsigned long)dsdt;
688
689 fadt->x_firmware_ctl_l = (unsigned long)facs;
690 fadt->x_firmware_ctl_h = 0;
691 fadt->x_dsdt_l = (unsigned long)dsdt;
692 fadt->x_dsdt_h = 0;
693
694 fadt->preferred_pm_profile = ACPI_PM_MOBILE;
695
696 /* Use ACPI 3.0 revision */
697 fadt->header.revision = 4;
698}
699
700void acpi_create_dmar_drhd(struct acpi_ctx *ctx, uint flags, uint segment,
701 u64 bar)
702{
703 struct dmar_entry *drhd = ctx->current;
704
705 memset(drhd, '\0', sizeof(*drhd));
706 drhd->type = DMAR_DRHD;
707 drhd->length = sizeof(*drhd); /* will be fixed up later */
708 drhd->flags = flags;
709 drhd->segment = segment;
710 drhd->bar = bar;
711 acpi_inc(ctx, drhd->length);
712}
713
714void acpi_create_dmar_rmrr(struct acpi_ctx *ctx, uint segment, u64 bar,
715 u64 limit)
716{
717 struct dmar_rmrr_entry *rmrr = ctx->current;
718
719 memset(rmrr, '\0', sizeof(*rmrr));
720 rmrr->type = DMAR_RMRR;
721 rmrr->length = sizeof(*rmrr); /* will be fixed up later */
722 rmrr->segment = segment;
723 rmrr->bar = bar;
724 rmrr->limit = limit;
725 acpi_inc(ctx, rmrr->length);
726}
727
728void acpi_dmar_drhd_fixup(struct acpi_ctx *ctx, void *base)
729{
730 struct dmar_entry *drhd = base;
731
732 drhd->length = ctx->current - base;
733}
734
735void acpi_dmar_rmrr_fixup(struct acpi_ctx *ctx, void *base)
736{
737 struct dmar_rmrr_entry *rmrr = base;
738
739 rmrr->length = ctx->current - base;
740}
741
742static int acpi_create_dmar_ds(struct acpi_ctx *ctx, enum dev_scope_type type,
743 uint enumeration_id, pci_dev_t bdf)
744{
745 /* we don't support longer paths yet */
746 const size_t dev_scope_length = sizeof(struct dev_scope) + 2;
747 struct dev_scope *ds = ctx->current;
748
749 memset(ds, '\0', dev_scope_length);
750 ds->type = type;
751 ds->length = dev_scope_length;
752 ds->enumeration = enumeration_id;
753 ds->start_bus = PCI_BUS(bdf);
754 ds->path[0].dev = PCI_DEV(bdf);
755 ds->path[0].fn = PCI_FUNC(bdf);
756
757 return ds->length;
758}
759
760int acpi_create_dmar_ds_pci_br(struct acpi_ctx *ctx, pci_dev_t bdf)
761{
762 return acpi_create_dmar_ds(ctx, SCOPE_PCI_SUB, 0, bdf);
763}
764
765int acpi_create_dmar_ds_pci(struct acpi_ctx *ctx, pci_dev_t bdf)
766{
767 return acpi_create_dmar_ds(ctx, SCOPE_PCI_ENDPOINT, 0, bdf);
768}
769
770int acpi_create_dmar_ds_ioapic(struct acpi_ctx *ctx, uint enumeration_id,
771 pci_dev_t bdf)
772{
773 return acpi_create_dmar_ds(ctx, SCOPE_IOAPIC, enumeration_id, bdf);
774}
775
776int acpi_create_dmar_ds_msi_hpet(struct acpi_ctx *ctx, uint enumeration_id,
777 pci_dev_t bdf)
778{
779 return acpi_create_dmar_ds(ctx, SCOPE_MSI_HPET, enumeration_id, bdf);
780}