blob: 772c04f1e55681746ea264f9b623b18071ed09b7 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Bin Mengf17cea62015-04-24 18:10:04 +08002/*
3 * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
Bin Mengf17cea62015-04-24 18:10:04 +08004 */
5
Simon Glassb87f76f2023-07-15 21:39:09 -06006#define LOG_CATEGORY LOGC_ACPI
Heinrich Schuchardt21da91f2021-05-15 18:07:47 +02007
Bin Mengf17cea62015-04-24 18:10:04 +08008#include <common.h>
Simon Glass5d093f32020-11-04 09:57:25 -07009#include <bloblist.h>
Simon Glassf66fb302020-07-16 21:22:31 -060010#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070011#include <malloc.h>
Alexander Graffb228082016-08-19 01:23:23 +020012#include <smbios.h>
Simon Glass858fed12020-04-08 16:57:36 -060013#include <acpi/acpi_table.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060014#include <asm/global_data.h>
Simon Glass07e922a2015-04-28 20:25:10 -060015#include <asm/sfi.h>
Bin Mengc4f407e2015-06-23 12:18:52 +080016#include <asm/mpspec.h>
Bin Mengf17cea62015-04-24 18:10:04 +080017#include <asm/tables.h>
Bin Meng96030fa2016-02-28 23:54:50 -080018#include <asm/coreboot_tables.h>
Bin Mengf17cea62015-04-24 18:10:04 +080019
Simon Glass5d093f32020-11-04 09:57:25 -070020DECLARE_GLOBAL_DATA_PTR;
21
Bin Mengef47ed12016-02-27 22:58:01 -080022/**
23 * Function prototype to write a specific configuration table
24 *
25 * @addr: start address to write the table
26 * @return: end address of the table
27 */
Simon Glassca37a392017-01-16 07:03:35 -070028typedef ulong (*table_write)(ulong addr);
Bin Mengef47ed12016-02-27 22:58:01 -080029
Simon Glassf66fb302020-07-16 21:22:31 -060030/**
31 * struct table_info - Information about each table to write
32 *
33 * @name: Name of table (for debugging)
34 * @write: Function to call to write this table
Simon Glass5d093f32020-11-04 09:57:25 -070035 * @tag: Bloblist tag if using CONFIG_BLOBLIST_TABLES
36 * @size: Maximum table size
37 * @align: Table alignment in bytes
Simon Glassf66fb302020-07-16 21:22:31 -060038 */
39struct table_info {
40 const char *name;
41 table_write write;
Simon Glass5d093f32020-11-04 09:57:25 -070042 enum bloblist_tag_t tag;
43 int size;
44 int align;
Simon Glassf66fb302020-07-16 21:22:31 -060045};
46
47static struct table_info table_list[] = {
Bin Mengef47ed12016-02-27 22:58:01 -080048#ifdef CONFIG_GENERATE_PIRQ_TABLE
Simon Glassf66fb302020-07-16 21:22:31 -060049 { "pirq", write_pirq_routing_table },
Bin Mengef47ed12016-02-27 22:58:01 -080050#endif
51#ifdef CONFIG_GENERATE_SFI_TABLE
Simon Glassf66fb302020-07-16 21:22:31 -060052 { "sfi", write_sfi_table, },
Bin Mengef47ed12016-02-27 22:58:01 -080053#endif
54#ifdef CONFIG_GENERATE_MP_TABLE
Simon Glassf66fb302020-07-16 21:22:31 -060055 { "mp", write_mp_table, },
Bin Mengef47ed12016-02-27 22:58:01 -080056#endif
57#ifdef CONFIG_GENERATE_ACPI_TABLE
Simon Glass5d093f32020-11-04 09:57:25 -070058 { "acpi", write_acpi_tables, BLOBLISTT_ACPI_TABLES, 0x10000, 0x1000},
Bin Mengef47ed12016-02-27 22:58:01 -080059#endif
60#ifdef CONFIG_GENERATE_SMBIOS_TABLE
Simon Glass5d093f32020-11-04 09:57:25 -070061 { "smbios", write_smbios_table, BLOBLISTT_SMBIOS_TABLES, 0x1000, 0x100},
Bin Mengef47ed12016-02-27 22:58:01 -080062#endif
63};
64
Bin Mengf91cf6b2015-06-23 12:18:51 +080065void table_fill_string(char *dest, const char *src, size_t n, char pad)
66{
67 int start, len;
68 int i;
69
70 strncpy(dest, src, n);
71
72 /* Fill the remaining bytes with pad */
73 len = strlen(src);
74 start = len < n ? len : n;
75 for (i = start; i < n; i++)
76 dest[i] = pad;
77}
78
Simon Glasse50129a2020-11-04 09:57:18 -070079int write_tables(void)
Bin Mengf17cea62015-04-24 18:10:04 +080080{
Bin Mengca68d772016-02-27 22:58:02 -080081 u32 high_table, table_size;
Simon Glassf66fb302020-07-16 21:22:31 -060082 struct memory_area cfg_tables[ARRAY_SIZE(table_list) + 1];
Simon Glassb87f76f2023-07-15 21:39:09 -060083 u32 rom_addr;
Bin Mengef47ed12016-02-27 22:58:01 -080084 int i;
Bin Mengf17cea62015-04-24 18:10:04 +080085
Simon Glassb87f76f2023-07-15 21:39:09 -060086 rom_addr = ROM_TABLE_ADDR;
Simon Glass5d093f32020-11-04 09:57:25 -070087
Simon Glassb87f76f2023-07-15 21:39:09 -060088 debug("Writing tables to %x:\n", rom_addr);
Simon Glassf66fb302020-07-16 21:22:31 -060089 for (i = 0; i < ARRAY_SIZE(table_list); i++) {
90 const struct table_info *table = &table_list[i];
Simon Glass5d093f32020-11-04 09:57:25 -070091 int size = table->size ? : CONFIG_ROM_TABLE_SIZE;
Simon Glassb87f76f2023-07-15 21:39:09 -060092 u32 rom_table_end;
Simon Glassf66fb302020-07-16 21:22:31 -060093
Simon Glass5d093f32020-11-04 09:57:25 -070094 if (IS_ENABLED(CONFIG_BLOBLIST_TABLES) && table->tag) {
Simon Glassb87f76f2023-07-15 21:39:09 -060095 rom_addr = (ulong)bloblist_add(table->tag, size,
96 table->align);
97 if (!rom_addr)
Simon Glass5d093f32020-11-04 09:57:25 -070098 return log_msg_ret("bloblist", -ENOBUFS);
99 }
Simon Glassb87f76f2023-07-15 21:39:09 -0600100 rom_table_end = table->write(rom_addr);
Heinrich Schuchardt21da91f2021-05-15 18:07:47 +0200101 if (!rom_table_end) {
102 log_err("Can't create configuration table %d\n", i);
103 return -EINTR;
104 }
Bin Mengca68d772016-02-27 22:58:02 -0800105
Simon Glass9b90b242020-11-04 09:57:24 -0700106 if (IS_ENABLED(CONFIG_SEABIOS)) {
Simon Glassb87f76f2023-07-15 21:39:09 -0600107 table_size = rom_table_end - rom_addr;
Simon Glass9b90b242020-11-04 09:57:24 -0700108 high_table = (u32)(ulong)high_table_malloc(table_size);
109 if (high_table) {
Heinrich Schuchardt21da91f2021-05-15 18:07:47 +0200110 if (!table->write(high_table)) {
111 log_err("Can't create configuration table %d\n",
112 i);
113 return -EINTR;
114 }
Bin Meng96030fa2016-02-28 23:54:50 -0800115
Simon Glass9b90b242020-11-04 09:57:24 -0700116 cfg_tables[i].start = high_table;
117 cfg_tables[i].size = table_size;
118 } else {
119 printf("%d: no memory for configuration tables\n",
120 i);
121 return -ENOSPC;
122 }
Bin Mengca68d772016-02-27 22:58:02 -0800123 }
124
Simon Glassf66fb302020-07-16 21:22:31 -0600125 debug("- wrote '%s' to %x, end %x\n", table->name,
Simon Glassb87f76f2023-07-15 21:39:09 -0600126 rom_addr, rom_table_end);
127 if (rom_table_end - rom_addr > size) {
Simon Glass5d093f32020-11-04 09:57:25 -0700128 log_err("Out of space for configuration tables: need %x, have %x\n",
Simon Glassb87f76f2023-07-15 21:39:09 -0600129 rom_table_end - rom_addr, size);
Simon Glass5d093f32020-11-04 09:57:25 -0700130 return log_msg_ret("bloblist", -ENOSPC);
131 }
Simon Glassb87f76f2023-07-15 21:39:09 -0600132 rom_addr = rom_table_end;
Bin Mengef47ed12016-02-27 22:58:01 -0800133 }
Bin Meng96030fa2016-02-28 23:54:50 -0800134
Simon Glass9b90b242020-11-04 09:57:24 -0700135 if (IS_ENABLED(CONFIG_SEABIOS)) {
136 /* make sure the last item is zero */
137 cfg_tables[i].size = 0;
138 write_coreboot_table(CB_TABLE_ADDR, cfg_tables);
139 }
140
Simon Glass5d093f32020-11-04 09:57:25 -0700141 if (IS_ENABLED(CONFIG_BLOBLIST_TABLES)) {
142 void *ptr = (void *)CONFIG_ROM_TABLE_ADDR;
143
144 /* Write an RSDP pointing to the tables */
145 if (IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) {
146 struct acpi_ctx *ctx = gd_acpi_ctx();
147
148 acpi_write_rsdp(ptr, ctx->rsdt, ctx->xsdt);
149 ptr += ALIGN(sizeof(struct acpi_rsdp), 16);
150 }
151 if (IS_ENABLED(CONFIG_GENERATE_SMBIOS_TABLE)) {
152 void *smbios;
153
154 smbios = bloblist_find(BLOBLISTT_SMBIOS_TABLES, 0);
155 if (!smbios)
156 return log_msg_ret("smbios", -ENOENT);
157 memcpy(ptr, smbios, sizeof(struct smbios_entry));
158 }
159 }
160
Simon Glassf66fb302020-07-16 21:22:31 -0600161 debug("- done writing tables\n");
Simon Glasse50129a2020-11-04 09:57:18 -0700162
163 return 0;
Bin Mengf17cea62015-04-24 18:10:04 +0800164}