blob: d7f183f95dcd864ce3125ce77930c84d72b22a82 [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
Simon Glass5d093f32020-11-04 09:57:25 -07008#include <bloblist.h>
Simon Glassf66fb302020-07-16 21:22:31 -06009#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070010#include <malloc.h>
Alexander Graffb228082016-08-19 01:23:23 +020011#include <smbios.h>
Simon Glass858fed12020-04-08 16:57:36 -060012#include <acpi/acpi_table.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Simon Glass07e922a2015-04-28 20:25:10 -060014#include <asm/sfi.h>
Bin Mengc4f407e2015-06-23 12:18:52 +080015#include <asm/mpspec.h>
Bin Mengf17cea62015-04-24 18:10:04 +080016#include <asm/tables.h>
Bin Meng96030fa2016-02-28 23:54:50 -080017#include <asm/coreboot_tables.h>
Simon Glassc2aa4ef2023-12-27 13:07:00 -080018#include <linux/log2.h>
Simon Glass64038752025-01-10 17:00:23 -070019#include <linux/sizes.h>
Bin Mengf17cea62015-04-24 18:10:04 +080020
Simon Glass5d093f32020-11-04 09:57:25 -070021DECLARE_GLOBAL_DATA_PTR;
22
Bin Mengef47ed12016-02-27 22:58:01 -080023/**
24 * Function prototype to write a specific configuration table
25 *
26 * @addr: start address to write the table
27 * @return: end address of the table
28 */
Simon Glassca37a392017-01-16 07:03:35 -070029typedef ulong (*table_write)(ulong addr);
Bin Mengef47ed12016-02-27 22:58:01 -080030
Simon Glassf66fb302020-07-16 21:22:31 -060031/**
32 * struct table_info - Information about each table to write
33 *
34 * @name: Name of table (for debugging)
35 * @write: Function to call to write this table
Simon Glass5d093f32020-11-04 09:57:25 -070036 * @tag: Bloblist tag if using CONFIG_BLOBLIST_TABLES
37 * @size: Maximum table size
38 * @align: Table alignment in bytes
Simon Glassf66fb302020-07-16 21:22:31 -060039 */
40struct table_info {
41 const char *name;
42 table_write write;
Simon Glass5d093f32020-11-04 09:57:25 -070043 enum bloblist_tag_t tag;
44 int size;
45 int align;
Simon Glassf66fb302020-07-16 21:22:31 -060046};
47
Simon Glassf32df8d2025-03-15 14:25:55 +000048/* QEMU's tables include quite a bit of empty space */
49#ifdef CONFIG_QEMU
50#define ACPI_SIZE (192 << 10)
51#else
52#define ACPI_SIZE SZ_64K
53#endif
54
Simon Glassf66fb302020-07-16 21:22:31 -060055static struct table_info table_list[] = {
Bin Mengef47ed12016-02-27 22:58:01 -080056#ifdef CONFIG_GENERATE_PIRQ_TABLE
Simon Glassf66fb302020-07-16 21:22:31 -060057 { "pirq", write_pirq_routing_table },
Bin Mengef47ed12016-02-27 22:58:01 -080058#endif
59#ifdef CONFIG_GENERATE_SFI_TABLE
Simon Glassf66fb302020-07-16 21:22:31 -060060 { "sfi", write_sfi_table, },
Bin Mengef47ed12016-02-27 22:58:01 -080061#endif
62#ifdef CONFIG_GENERATE_MP_TABLE
Simon Glassf66fb302020-07-16 21:22:31 -060063 { "mp", write_mp_table, },
Bin Mengef47ed12016-02-27 22:58:01 -080064#endif
Simon Glass66ca25c2023-07-15 21:39:10 -060065 /*
66 * tables which can go in the bloblist must be last in this list, so
67 * that the calculation of gd->table_end works properly
68 */
Bin Mengef47ed12016-02-27 22:58:01 -080069#ifdef CONFIG_GENERATE_ACPI_TABLE
Simon Glassf32df8d2025-03-15 14:25:55 +000070 { "acpi", write_acpi_tables, BLOBLISTT_ACPI_TABLES, ACPI_SIZE, SZ_4K},
Bin Mengef47ed12016-02-27 22:58:01 -080071#endif
Simon Glass4d8c5202025-01-10 17:00:17 -070072#ifdef CONFIG_GENERATE_SMBIOS_TABLE
Simon Glass188b9302025-01-10 17:00:24 -070073 /*
74 * align this to a 4K boundary, since UPL adds a reserved-memory node
75 * for it
76 */
77 { "smbios", write_smbios_table, BLOBLISTT_SMBIOS_TABLES, SZ_4K, SZ_4K},
Bin Mengef47ed12016-02-27 22:58:01 -080078#endif
79};
80
Bin Mengf91cf6b2015-06-23 12:18:51 +080081void table_fill_string(char *dest, const char *src, size_t n, char pad)
82{
83 int start, len;
84 int i;
85
86 strncpy(dest, src, n);
87
88 /* Fill the remaining bytes with pad */
89 len = strlen(src);
90 start = len < n ? len : n;
91 for (i = start; i < n; i++)
92 dest[i] = pad;
93}
94
Simon Glasse50129a2020-11-04 09:57:18 -070095int write_tables(void)
Bin Mengf17cea62015-04-24 18:10:04 +080096{
Bin Mengca68d772016-02-27 22:58:02 -080097 u32 high_table, table_size;
Simon Glassf66fb302020-07-16 21:22:31 -060098 struct memory_area cfg_tables[ARRAY_SIZE(table_list) + 1];
Simon Glass66ca25c2023-07-15 21:39:10 -060099 bool use_high = false;
Simon Glassb87f76f2023-07-15 21:39:09 -0600100 u32 rom_addr;
Bin Mengef47ed12016-02-27 22:58:01 -0800101 int i;
Bin Mengf17cea62015-04-24 18:10:04 +0800102
Simon Glass66ca25c2023-07-15 21:39:10 -0600103 gd->arch.table_start = ROM_TABLE_ADDR;
104 rom_addr = gd->arch.table_start;
Simon Glass5d093f32020-11-04 09:57:25 -0700105
Simon Glassb87f76f2023-07-15 21:39:09 -0600106 debug("Writing tables to %x:\n", rom_addr);
Simon Glassf66fb302020-07-16 21:22:31 -0600107 for (i = 0; i < ARRAY_SIZE(table_list); i++) {
108 const struct table_info *table = &table_list[i];
Simon Glass5d093f32020-11-04 09:57:25 -0700109 int size = table->size ? : CONFIG_ROM_TABLE_SIZE;
Simon Glassb87f76f2023-07-15 21:39:09 -0600110 u32 rom_table_end;
Simon Glassf66fb302020-07-16 21:22:31 -0600111
Heinrich Schuchardt8b9c9062024-01-02 00:11:44 +0100112 rom_addr = ALIGN(rom_addr, 16);
113
Simon Glass9b388102023-09-19 21:00:15 -0600114 if (!strcmp("smbios", table->name))
115 gd->arch.smbios_start = rom_addr;
116
Simon Glass5d093f32020-11-04 09:57:25 -0700117 if (IS_ENABLED(CONFIG_BLOBLIST_TABLES) && table->tag) {
Simon Glass66ca25c2023-07-15 21:39:10 -0600118 if (!gd->arch.table_end)
119 gd->arch.table_end = rom_addr;
Simon Glassb87f76f2023-07-15 21:39:09 -0600120 rom_addr = (ulong)bloblist_add(table->tag, size,
Simon Glassc2aa4ef2023-12-27 13:07:00 -0800121 ilog2(table->align));
Simon Glassb87f76f2023-07-15 21:39:09 -0600122 if (!rom_addr)
Simon Glass5d093f32020-11-04 09:57:25 -0700123 return log_msg_ret("bloblist", -ENOBUFS);
Simon Glass66ca25c2023-07-15 21:39:10 -0600124
125 /* the bloblist is always in high memory */
126 use_high = true;
127 if (!gd->arch.table_start_high)
128 gd->arch.table_start_high = rom_addr;
Simon Glass519fe482025-05-23 17:55:04 +0100129 if (table->tag == BLOBLISTT_SMBIOS_TABLES)
130 gd_set_smbios_start(rom_addr);
Simon Glass5d093f32020-11-04 09:57:25 -0700131 }
Simon Glassb87f76f2023-07-15 21:39:09 -0600132 rom_table_end = table->write(rom_addr);
Heinrich Schuchardt21da91f2021-05-15 18:07:47 +0200133 if (!rom_table_end) {
134 log_err("Can't create configuration table %d\n", i);
135 return -EINTR;
136 }
Bin Mengca68d772016-02-27 22:58:02 -0800137
Simon Glass9b90b242020-11-04 09:57:24 -0700138 if (IS_ENABLED(CONFIG_SEABIOS)) {
Simon Glassb87f76f2023-07-15 21:39:09 -0600139 table_size = rom_table_end - rom_addr;
Simon Glass9b90b242020-11-04 09:57:24 -0700140 high_table = (u32)(ulong)high_table_malloc(table_size);
141 if (high_table) {
Heinrich Schuchardt21da91f2021-05-15 18:07:47 +0200142 if (!table->write(high_table)) {
143 log_err("Can't create configuration table %d\n",
144 i);
145 return -EINTR;
146 }
Bin Meng96030fa2016-02-28 23:54:50 -0800147
Simon Glass9b90b242020-11-04 09:57:24 -0700148 cfg_tables[i].start = high_table;
149 cfg_tables[i].size = table_size;
150 } else {
151 printf("%d: no memory for configuration tables\n",
152 i);
153 return -ENOSPC;
154 }
Bin Mengca68d772016-02-27 22:58:02 -0800155 }
156
Simon Glassf66fb302020-07-16 21:22:31 -0600157 debug("- wrote '%s' to %x, end %x\n", table->name,
Simon Glassb87f76f2023-07-15 21:39:09 -0600158 rom_addr, rom_table_end);
159 if (rom_table_end - rom_addr > size) {
Simon Glass5d093f32020-11-04 09:57:25 -0700160 log_err("Out of space for configuration tables: need %x, have %x\n",
Simon Glassb87f76f2023-07-15 21:39:09 -0600161 rom_table_end - rom_addr, size);
Simon Glass5d093f32020-11-04 09:57:25 -0700162 return log_msg_ret("bloblist", -ENOSPC);
163 }
Simon Glassb87f76f2023-07-15 21:39:09 -0600164 rom_addr = rom_table_end;
Bin Mengef47ed12016-02-27 22:58:01 -0800165 }
Bin Meng96030fa2016-02-28 23:54:50 -0800166
Simon Glass66ca25c2023-07-15 21:39:10 -0600167 if (use_high)
168 gd->arch.table_end_high = rom_addr;
169 else
170 gd->arch.table_end = rom_addr;
171
Simon Glass9b90b242020-11-04 09:57:24 -0700172 if (IS_ENABLED(CONFIG_SEABIOS)) {
173 /* make sure the last item is zero */
174 cfg_tables[i].size = 0;
175 write_coreboot_table(CB_TABLE_ADDR, cfg_tables);
176 }
177
Simon Glass5d093f32020-11-04 09:57:25 -0700178 if (IS_ENABLED(CONFIG_BLOBLIST_TABLES)) {
179 void *ptr = (void *)CONFIG_ROM_TABLE_ADDR;
180
181 /* Write an RSDP pointing to the tables */
182 if (IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) {
183 struct acpi_ctx *ctx = gd_acpi_ctx();
184
185 acpi_write_rsdp(ptr, ctx->rsdt, ctx->xsdt);
186 ptr += ALIGN(sizeof(struct acpi_rsdp), 16);
187 }
188 if (IS_ENABLED(CONFIG_GENERATE_SMBIOS_TABLE)) {
189 void *smbios;
190
191 smbios = bloblist_find(BLOBLISTT_SMBIOS_TABLES, 0);
192 if (!smbios)
193 return log_msg_ret("smbios", -ENOENT);
194 memcpy(ptr, smbios, sizeof(struct smbios_entry));
195 }
196 }
197
Simon Glassf66fb302020-07-16 21:22:31 -0600198 debug("- done writing tables\n");
Simon Glasse50129a2020-11-04 09:57:18 -0700199
200 return 0;
Bin Mengf17cea62015-04-24 18:10:04 +0800201}