blob: 1779bb3e11ae56e25afd31b5ad7d083cdab05587 [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
6#include <common.h>
Simon Glass5d093f32020-11-04 09:57:25 -07007#include <bloblist.h>
Simon Glassf66fb302020-07-16 21:22:31 -06008#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -07009#include <malloc.h>
Alexander Graffb228082016-08-19 01:23:23 +020010#include <smbios.h>
Simon Glass858fed12020-04-08 16:57:36 -060011#include <acpi/acpi_table.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060012#include <asm/global_data.h>
Simon Glass07e922a2015-04-28 20:25:10 -060013#include <asm/sfi.h>
Bin Mengc4f407e2015-06-23 12:18:52 +080014#include <asm/mpspec.h>
Bin Mengf17cea62015-04-24 18:10:04 +080015#include <asm/tables.h>
Bin Meng96030fa2016-02-28 23:54:50 -080016#include <asm/coreboot_tables.h>
Bin Mengf17cea62015-04-24 18:10:04 +080017
Simon Glass5d093f32020-11-04 09:57:25 -070018DECLARE_GLOBAL_DATA_PTR;
19
Bin Mengef47ed12016-02-27 22:58:01 -080020/**
21 * Function prototype to write a specific configuration table
22 *
23 * @addr: start address to write the table
24 * @return: end address of the table
25 */
Simon Glassca37a392017-01-16 07:03:35 -070026typedef ulong (*table_write)(ulong addr);
Bin Mengef47ed12016-02-27 22:58:01 -080027
Simon Glassf66fb302020-07-16 21:22:31 -060028/**
29 * struct table_info - Information about each table to write
30 *
31 * @name: Name of table (for debugging)
32 * @write: Function to call to write this table
Simon Glass5d093f32020-11-04 09:57:25 -070033 * @tag: Bloblist tag if using CONFIG_BLOBLIST_TABLES
34 * @size: Maximum table size
35 * @align: Table alignment in bytes
Simon Glassf66fb302020-07-16 21:22:31 -060036 */
37struct table_info {
38 const char *name;
39 table_write write;
Simon Glass5d093f32020-11-04 09:57:25 -070040 enum bloblist_tag_t tag;
41 int size;
42 int align;
Simon Glassf66fb302020-07-16 21:22:31 -060043};
44
45static struct table_info table_list[] = {
Bin Mengef47ed12016-02-27 22:58:01 -080046#ifdef CONFIG_GENERATE_PIRQ_TABLE
Simon Glassf66fb302020-07-16 21:22:31 -060047 { "pirq", write_pirq_routing_table },
Bin Mengef47ed12016-02-27 22:58:01 -080048#endif
49#ifdef CONFIG_GENERATE_SFI_TABLE
Simon Glassf66fb302020-07-16 21:22:31 -060050 { "sfi", write_sfi_table, },
Bin Mengef47ed12016-02-27 22:58:01 -080051#endif
52#ifdef CONFIG_GENERATE_MP_TABLE
Simon Glassf66fb302020-07-16 21:22:31 -060053 { "mp", write_mp_table, },
Bin Mengef47ed12016-02-27 22:58:01 -080054#endif
55#ifdef CONFIG_GENERATE_ACPI_TABLE
Simon Glass5d093f32020-11-04 09:57:25 -070056 { "acpi", write_acpi_tables, BLOBLISTT_ACPI_TABLES, 0x10000, 0x1000},
Bin Mengef47ed12016-02-27 22:58:01 -080057#endif
58#ifdef CONFIG_GENERATE_SMBIOS_TABLE
Simon Glass5d093f32020-11-04 09:57:25 -070059 { "smbios", write_smbios_table, BLOBLISTT_SMBIOS_TABLES, 0x1000, 0x100},
Bin Mengef47ed12016-02-27 22:58:01 -080060#endif
61};
62
Bin Mengf91cf6b2015-06-23 12:18:51 +080063void table_fill_string(char *dest, const char *src, size_t n, char pad)
64{
65 int start, len;
66 int i;
67
68 strncpy(dest, src, n);
69
70 /* Fill the remaining bytes with pad */
71 len = strlen(src);
72 start = len < n ? len : n;
73 for (i = start; i < n; i++)
74 dest[i] = pad;
75}
76
Simon Glasse50129a2020-11-04 09:57:18 -070077int write_tables(void)
Bin Mengf17cea62015-04-24 18:10:04 +080078{
Simon Glass5d093f32020-11-04 09:57:25 -070079 u32 rom_table_start;
Bin Mengef47ed12016-02-27 22:58:01 -080080 u32 rom_table_end;
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];
Bin Mengef47ed12016-02-27 22:58:01 -080083 int i;
Bin Mengf17cea62015-04-24 18:10:04 +080084
Simon Glass5d093f32020-11-04 09:57:25 -070085 rom_table_start = ROM_TABLE_ADDR;
86
Simon Glassf66fb302020-07-16 21:22:31 -060087 debug("Writing tables to %x:\n", rom_table_start);
88 for (i = 0; i < ARRAY_SIZE(table_list); i++) {
89 const struct table_info *table = &table_list[i];
Simon Glass5d093f32020-11-04 09:57:25 -070090 int size = table->size ? : CONFIG_ROM_TABLE_SIZE;
Simon Glassf66fb302020-07-16 21:22:31 -060091
Simon Glass5d093f32020-11-04 09:57:25 -070092 if (IS_ENABLED(CONFIG_BLOBLIST_TABLES) && table->tag) {
93 rom_table_start = (ulong)bloblist_add(table->tag, size,
94 table->align);
95 if (!rom_table_start)
96 return log_msg_ret("bloblist", -ENOBUFS);
97 }
Simon Glassf66fb302020-07-16 21:22:31 -060098 rom_table_end = table->write(rom_table_start);
Bin Mengef47ed12016-02-27 22:58:01 -080099 rom_table_end = ALIGN(rom_table_end, ROM_TABLE_ALIGN);
Bin Mengca68d772016-02-27 22:58:02 -0800100
Simon Glass9b90b242020-11-04 09:57:24 -0700101 if (IS_ENABLED(CONFIG_SEABIOS)) {
102 table_size = rom_table_end - rom_table_start;
103 high_table = (u32)(ulong)high_table_malloc(table_size);
104 if (high_table) {
105 table->write(high_table);
Bin Meng96030fa2016-02-28 23:54:50 -0800106
Simon Glass9b90b242020-11-04 09:57:24 -0700107 cfg_tables[i].start = high_table;
108 cfg_tables[i].size = table_size;
109 } else {
110 printf("%d: no memory for configuration tables\n",
111 i);
112 return -ENOSPC;
113 }
Bin Mengca68d772016-02-27 22:58:02 -0800114 }
115
Simon Glassf66fb302020-07-16 21:22:31 -0600116 debug("- wrote '%s' to %x, end %x\n", table->name,
117 rom_table_start, rom_table_end);
Simon Glass5d093f32020-11-04 09:57:25 -0700118 if (rom_table_end - rom_table_start > size) {
119 log_err("Out of space for configuration tables: need %x, have %x\n",
120 rom_table_end - rom_table_start, size);
121 return log_msg_ret("bloblist", -ENOSPC);
122 }
Bin Mengef47ed12016-02-27 22:58:01 -0800123 rom_table_start = rom_table_end;
124 }
Bin Meng96030fa2016-02-28 23:54:50 -0800125
Simon Glass9b90b242020-11-04 09:57:24 -0700126 if (IS_ENABLED(CONFIG_SEABIOS)) {
127 /* make sure the last item is zero */
128 cfg_tables[i].size = 0;
129 write_coreboot_table(CB_TABLE_ADDR, cfg_tables);
130 }
131
Simon Glass5d093f32020-11-04 09:57:25 -0700132 if (IS_ENABLED(CONFIG_BLOBLIST_TABLES)) {
133 void *ptr = (void *)CONFIG_ROM_TABLE_ADDR;
134
135 /* Write an RSDP pointing to the tables */
136 if (IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) {
137 struct acpi_ctx *ctx = gd_acpi_ctx();
138
139 acpi_write_rsdp(ptr, ctx->rsdt, ctx->xsdt);
140 ptr += ALIGN(sizeof(struct acpi_rsdp), 16);
141 }
142 if (IS_ENABLED(CONFIG_GENERATE_SMBIOS_TABLE)) {
143 void *smbios;
144
145 smbios = bloblist_find(BLOBLISTT_SMBIOS_TABLES, 0);
146 if (!smbios)
147 return log_msg_ret("smbios", -ENOENT);
148 memcpy(ptr, smbios, sizeof(struct smbios_entry));
149 }
150 }
151
Simon Glassf66fb302020-07-16 21:22:31 -0600152 debug("- done writing tables\n");
Simon Glasse50129a2020-11-04 09:57:18 -0700153
154 return 0;
Bin Mengf17cea62015-04-24 18:10:04 +0800155}