blob: 12eae17c3968dcc8946937c9d926d8b4962ca12e [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>
Simon Glassc2aa4ef2023-12-27 13:07:00 -080019#include <linux/log2.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
48static struct table_info table_list[] = {
Bin Mengef47ed12016-02-27 22:58:01 -080049#ifdef CONFIG_GENERATE_PIRQ_TABLE
Simon Glassf66fb302020-07-16 21:22:31 -060050 { "pirq", write_pirq_routing_table },
Bin Mengef47ed12016-02-27 22:58:01 -080051#endif
52#ifdef CONFIG_GENERATE_SFI_TABLE
Simon Glassf66fb302020-07-16 21:22:31 -060053 { "sfi", write_sfi_table, },
Bin Mengef47ed12016-02-27 22:58:01 -080054#endif
55#ifdef CONFIG_GENERATE_MP_TABLE
Simon Glassf66fb302020-07-16 21:22:31 -060056 { "mp", write_mp_table, },
Bin Mengef47ed12016-02-27 22:58:01 -080057#endif
Simon Glass66ca25c2023-07-15 21:39:10 -060058 /*
59 * tables which can go in the bloblist must be last in this list, so
60 * that the calculation of gd->table_end works properly
61 */
Bin Mengef47ed12016-02-27 22:58:01 -080062#ifdef CONFIG_GENERATE_ACPI_TABLE
Simon Glass5d093f32020-11-04 09:57:25 -070063 { "acpi", write_acpi_tables, BLOBLISTT_ACPI_TABLES, 0x10000, 0x1000},
Bin Mengef47ed12016-02-27 22:58:01 -080064#endif
Heinrich Schuchardt08d931a2023-12-23 02:03:34 +010065#if defined(CONFIG_GENERATE_SMBIOS_TABLE) && !defined(CONFIG_QFW_SMBIOS)
Simon Glass5d093f32020-11-04 09:57:25 -070066 { "smbios", write_smbios_table, BLOBLISTT_SMBIOS_TABLES, 0x1000, 0x100},
Bin Mengef47ed12016-02-27 22:58:01 -080067#endif
68};
69
Bin Mengf91cf6b2015-06-23 12:18:51 +080070void table_fill_string(char *dest, const char *src, size_t n, char pad)
71{
72 int start, len;
73 int i;
74
75 strncpy(dest, src, n);
76
77 /* Fill the remaining bytes with pad */
78 len = strlen(src);
79 start = len < n ? len : n;
80 for (i = start; i < n; i++)
81 dest[i] = pad;
82}
83
Simon Glasse50129a2020-11-04 09:57:18 -070084int write_tables(void)
Bin Mengf17cea62015-04-24 18:10:04 +080085{
Bin Mengca68d772016-02-27 22:58:02 -080086 u32 high_table, table_size;
Simon Glassf66fb302020-07-16 21:22:31 -060087 struct memory_area cfg_tables[ARRAY_SIZE(table_list) + 1];
Simon Glass66ca25c2023-07-15 21:39:10 -060088 bool use_high = false;
Simon Glassb87f76f2023-07-15 21:39:09 -060089 u32 rom_addr;
Bin Mengef47ed12016-02-27 22:58:01 -080090 int i;
Bin Mengf17cea62015-04-24 18:10:04 +080091
Simon Glass66ca25c2023-07-15 21:39:10 -060092 gd->arch.table_start = ROM_TABLE_ADDR;
93 rom_addr = gd->arch.table_start;
Simon Glass5d093f32020-11-04 09:57:25 -070094
Simon Glassb87f76f2023-07-15 21:39:09 -060095 debug("Writing tables to %x:\n", rom_addr);
Simon Glassf66fb302020-07-16 21:22:31 -060096 for (i = 0; i < ARRAY_SIZE(table_list); i++) {
97 const struct table_info *table = &table_list[i];
Simon Glass5d093f32020-11-04 09:57:25 -070098 int size = table->size ? : CONFIG_ROM_TABLE_SIZE;
Simon Glassb87f76f2023-07-15 21:39:09 -060099 u32 rom_table_end;
Simon Glassf66fb302020-07-16 21:22:31 -0600100
Simon Glass9b388102023-09-19 21:00:15 -0600101 if (!strcmp("smbios", table->name))
102 gd->arch.smbios_start = rom_addr;
103
Simon Glass5d093f32020-11-04 09:57:25 -0700104 if (IS_ENABLED(CONFIG_BLOBLIST_TABLES) && table->tag) {
Simon Glass66ca25c2023-07-15 21:39:10 -0600105 if (!gd->arch.table_end)
106 gd->arch.table_end = rom_addr;
Simon Glassb87f76f2023-07-15 21:39:09 -0600107 rom_addr = (ulong)bloblist_add(table->tag, size,
Simon Glassc2aa4ef2023-12-27 13:07:00 -0800108 ilog2(table->align));
Simon Glassb87f76f2023-07-15 21:39:09 -0600109 if (!rom_addr)
Simon Glass5d093f32020-11-04 09:57:25 -0700110 return log_msg_ret("bloblist", -ENOBUFS);
Simon Glass66ca25c2023-07-15 21:39:10 -0600111
112 /* the bloblist is always in high memory */
113 use_high = true;
114 if (!gd->arch.table_start_high)
115 gd->arch.table_start_high = rom_addr;
Simon Glass5d093f32020-11-04 09:57:25 -0700116 }
Simon Glassb87f76f2023-07-15 21:39:09 -0600117 rom_table_end = table->write(rom_addr);
Heinrich Schuchardt21da91f2021-05-15 18:07:47 +0200118 if (!rom_table_end) {
119 log_err("Can't create configuration table %d\n", i);
120 return -EINTR;
121 }
Bin Mengca68d772016-02-27 22:58:02 -0800122
Simon Glass9b90b242020-11-04 09:57:24 -0700123 if (IS_ENABLED(CONFIG_SEABIOS)) {
Simon Glassb87f76f2023-07-15 21:39:09 -0600124 table_size = rom_table_end - rom_addr;
Simon Glass9b90b242020-11-04 09:57:24 -0700125 high_table = (u32)(ulong)high_table_malloc(table_size);
126 if (high_table) {
Heinrich Schuchardt21da91f2021-05-15 18:07:47 +0200127 if (!table->write(high_table)) {
128 log_err("Can't create configuration table %d\n",
129 i);
130 return -EINTR;
131 }
Bin Meng96030fa2016-02-28 23:54:50 -0800132
Simon Glass9b90b242020-11-04 09:57:24 -0700133 cfg_tables[i].start = high_table;
134 cfg_tables[i].size = table_size;
135 } else {
136 printf("%d: no memory for configuration tables\n",
137 i);
138 return -ENOSPC;
139 }
Bin Mengca68d772016-02-27 22:58:02 -0800140 }
141
Simon Glassf66fb302020-07-16 21:22:31 -0600142 debug("- wrote '%s' to %x, end %x\n", table->name,
Simon Glassb87f76f2023-07-15 21:39:09 -0600143 rom_addr, rom_table_end);
144 if (rom_table_end - rom_addr > size) {
Simon Glass5d093f32020-11-04 09:57:25 -0700145 log_err("Out of space for configuration tables: need %x, have %x\n",
Simon Glassb87f76f2023-07-15 21:39:09 -0600146 rom_table_end - rom_addr, size);
Simon Glass5d093f32020-11-04 09:57:25 -0700147 return log_msg_ret("bloblist", -ENOSPC);
148 }
Simon Glassb87f76f2023-07-15 21:39:09 -0600149 rom_addr = rom_table_end;
Bin Mengef47ed12016-02-27 22:58:01 -0800150 }
Bin Meng96030fa2016-02-28 23:54:50 -0800151
Simon Glass66ca25c2023-07-15 21:39:10 -0600152 if (use_high)
153 gd->arch.table_end_high = rom_addr;
154 else
155 gd->arch.table_end = rom_addr;
156
Simon Glass9b90b242020-11-04 09:57:24 -0700157 if (IS_ENABLED(CONFIG_SEABIOS)) {
158 /* make sure the last item is zero */
159 cfg_tables[i].size = 0;
160 write_coreboot_table(CB_TABLE_ADDR, cfg_tables);
161 }
162
Simon Glass5d093f32020-11-04 09:57:25 -0700163 if (IS_ENABLED(CONFIG_BLOBLIST_TABLES)) {
164 void *ptr = (void *)CONFIG_ROM_TABLE_ADDR;
165
166 /* Write an RSDP pointing to the tables */
167 if (IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) {
168 struct acpi_ctx *ctx = gd_acpi_ctx();
169
170 acpi_write_rsdp(ptr, ctx->rsdt, ctx->xsdt);
171 ptr += ALIGN(sizeof(struct acpi_rsdp), 16);
172 }
173 if (IS_ENABLED(CONFIG_GENERATE_SMBIOS_TABLE)) {
174 void *smbios;
175
176 smbios = bloblist_find(BLOBLISTT_SMBIOS_TABLES, 0);
177 if (!smbios)
178 return log_msg_ret("smbios", -ENOENT);
179 memcpy(ptr, smbios, sizeof(struct smbios_entry));
180 }
181 }
182
Simon Glassf66fb302020-07-16 21:22:31 -0600183 debug("- done writing tables\n");
Simon Glasse50129a2020-11-04 09:57:18 -0700184
185 return 0;
Bin Mengf17cea62015-04-24 18:10:04 +0800186}