Prepare v2023.10
[platform/kernel/u-boot.git] / arch / x86 / lib / tables.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
4  */
5
6 #define LOG_CATEGORY LOGC_ACPI
7
8 #include <common.h>
9 #include <bloblist.h>
10 #include <log.h>
11 #include <malloc.h>
12 #include <smbios.h>
13 #include <acpi/acpi_table.h>
14 #include <asm/global_data.h>
15 #include <asm/sfi.h>
16 #include <asm/mpspec.h>
17 #include <asm/tables.h>
18 #include <asm/coreboot_tables.h>
19
20 DECLARE_GLOBAL_DATA_PTR;
21
22 /**
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  */
28 typedef ulong (*table_write)(ulong addr);
29
30 /**
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
35  * @tag: Bloblist tag if using CONFIG_BLOBLIST_TABLES
36  * @size: Maximum table size
37  * @align: Table alignment in bytes
38  */
39 struct table_info {
40         const char *name;
41         table_write write;
42         enum bloblist_tag_t tag;
43         int size;
44         int align;
45 };
46
47 static struct table_info table_list[] = {
48 #ifdef CONFIG_GENERATE_PIRQ_TABLE
49         { "pirq", write_pirq_routing_table },
50 #endif
51 #ifdef CONFIG_GENERATE_SFI_TABLE
52         { "sfi", write_sfi_table, },
53 #endif
54 #ifdef CONFIG_GENERATE_MP_TABLE
55         { "mp", write_mp_table, },
56 #endif
57         /*
58          * tables which can go in the bloblist must be last in this list, so
59          * that the calculation of gd->table_end works properly
60          */
61 #ifdef CONFIG_GENERATE_ACPI_TABLE
62         { "acpi", write_acpi_tables, BLOBLISTT_ACPI_TABLES, 0x10000, 0x1000},
63 #endif
64 #ifdef CONFIG_GENERATE_SMBIOS_TABLE
65         { "smbios", write_smbios_table, BLOBLISTT_SMBIOS_TABLES, 0x1000, 0x100},
66 #endif
67 };
68
69 void table_fill_string(char *dest, const char *src, size_t n, char pad)
70 {
71         int start, len;
72         int i;
73
74         strncpy(dest, src, n);
75
76         /* Fill the remaining bytes with pad */
77         len = strlen(src);
78         start = len < n ? len : n;
79         for (i = start; i < n; i++)
80                 dest[i] = pad;
81 }
82
83 int write_tables(void)
84 {
85         u32 high_table, table_size;
86         struct memory_area cfg_tables[ARRAY_SIZE(table_list) + 1];
87         bool use_high = false;
88         u32 rom_addr;
89         int i;
90
91         gd->arch.table_start = ROM_TABLE_ADDR;
92         rom_addr = gd->arch.table_start;
93
94         debug("Writing tables to %x:\n", rom_addr);
95         for (i = 0; i < ARRAY_SIZE(table_list); i++) {
96                 const struct table_info *table = &table_list[i];
97                 int size = table->size ? : CONFIG_ROM_TABLE_SIZE;
98                 u32 rom_table_end;
99
100                 if (IS_ENABLED(CONFIG_BLOBLIST_TABLES) && table->tag) {
101                         if (!gd->arch.table_end)
102                                 gd->arch.table_end = rom_addr;
103                         rom_addr = (ulong)bloblist_add(table->tag, size,
104                                                        table->align);
105                         if (!rom_addr)
106                                 return log_msg_ret("bloblist", -ENOBUFS);
107
108                         /* the bloblist is always in high memory */
109                         use_high = true;
110                         if (!gd->arch.table_start_high)
111                                 gd->arch.table_start_high = rom_addr;
112                 }
113                 rom_table_end = table->write(rom_addr);
114                 if (!rom_table_end) {
115                         log_err("Can't create configuration table %d\n", i);
116                         return -EINTR;
117                 }
118
119                 if (IS_ENABLED(CONFIG_SEABIOS)) {
120                         table_size = rom_table_end - rom_addr;
121                         high_table = (u32)(ulong)high_table_malloc(table_size);
122                         if (high_table) {
123                                 if (!table->write(high_table)) {
124                                         log_err("Can't create configuration table %d\n",
125                                                 i);
126                                         return -EINTR;
127                                 }
128
129                                 cfg_tables[i].start = high_table;
130                                 cfg_tables[i].size = table_size;
131                         } else {
132                                 printf("%d: no memory for configuration tables\n",
133                                        i);
134                                 return -ENOSPC;
135                         }
136                 }
137
138                 debug("- wrote '%s' to %x, end %x\n", table->name,
139                       rom_addr, rom_table_end);
140                 if (rom_table_end - rom_addr > size) {
141                         log_err("Out of space for configuration tables: need %x, have %x\n",
142                                 rom_table_end - rom_addr, size);
143                         return log_msg_ret("bloblist", -ENOSPC);
144                 }
145                 rom_addr = rom_table_end;
146         }
147
148         if (use_high)
149                 gd->arch.table_end_high = rom_addr;
150         else
151                 gd->arch.table_end = rom_addr;
152
153         if (IS_ENABLED(CONFIG_SEABIOS)) {
154                 /* make sure the last item is zero */
155                 cfg_tables[i].size = 0;
156                 write_coreboot_table(CB_TABLE_ADDR, cfg_tables);
157         }
158
159         if (IS_ENABLED(CONFIG_BLOBLIST_TABLES)) {
160                 void *ptr = (void *)CONFIG_ROM_TABLE_ADDR;
161
162                 /* Write an RSDP pointing to the tables */
163                 if (IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) {
164                         struct acpi_ctx *ctx = gd_acpi_ctx();
165
166                         acpi_write_rsdp(ptr, ctx->rsdt, ctx->xsdt);
167                         ptr += ALIGN(sizeof(struct acpi_rsdp), 16);
168                 }
169                 if (IS_ENABLED(CONFIG_GENERATE_SMBIOS_TABLE)) {
170                         void *smbios;
171
172                         smbios = bloblist_find(BLOBLISTT_SMBIOS_TABLES, 0);
173                         if (!smbios)
174                                 return log_msg_ret("smbios", -ENOENT);
175                         memcpy(ptr, smbios, sizeof(struct smbios_entry));
176                 }
177         }
178
179         debug("- done writing tables\n");
180
181         return 0;
182 }