1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <console/console.h>
4 #include <bootmem.h>
5 #include <boot/tables.h>
6 #include <boot/coreboot_tables.h>
7 #include <arch/pirq_routing.h>
8 #include <arch/smp/mpspec.h>
9 #include <acpi/acpi.h>
10 #include <commonlib/helpers.h>
11 #include <string.h>
12 #include <cbmem.h>
13 #include <smbios.h>
14
write_pirq_table(unsigned long rom_table_end)15 static unsigned long write_pirq_table(unsigned long rom_table_end)
16 {
17 unsigned long high_table_pointer;
18
19 #define MAX_PIRQ_TABLE_SIZE (4 * 1024)
20 post_code(POSTCODE_X86_WRITE_PIRQ_TABLE);
21
22 /* This table must be between 0x0f0000 and 0x100000 */
23 rom_table_end = write_pirq_routing_table(rom_table_end);
24 rom_table_end = ALIGN_UP(rom_table_end, 1024);
25
26 /* And add a high table version for those payloads that
27 * want to live in the F segment
28 */
29 high_table_pointer = (unsigned long)cbmem_add(CBMEM_ID_PIRQ,
30 MAX_PIRQ_TABLE_SIZE);
31 if (high_table_pointer) {
32 unsigned long new_high_table_pointer;
33 new_high_table_pointer =
34 write_pirq_routing_table(high_table_pointer);
35 // FIXME make pirq table code intelligent enough to know how
36 // much space it's going to need.
37 if (new_high_table_pointer > (high_table_pointer
38 + MAX_PIRQ_TABLE_SIZE))
39 printk(BIOS_ERR, "Increase PIRQ size.\n");
40 printk(BIOS_DEBUG, "PIRQ table: %ld bytes.\n",
41 new_high_table_pointer - high_table_pointer);
42 }
43
44 return rom_table_end;
45 }
46
write_mptable(unsigned long rom_table_end)47 static unsigned long write_mptable(unsigned long rom_table_end)
48 {
49 unsigned long high_table_pointer;
50
51 #define MAX_MP_TABLE_SIZE (4 * 1024)
52 post_code(POSTCODE_X86_WRITE_MPTABLE);
53
54 /* The smp table must be in 0-1K, 639K-640K, or 960K-1M */
55 rom_table_end = write_smp_table(rom_table_end);
56 rom_table_end = ALIGN_UP(rom_table_end, 1024);
57
58 high_table_pointer = (unsigned long)cbmem_add(CBMEM_ID_MPTABLE,
59 MAX_MP_TABLE_SIZE);
60 if (high_table_pointer) {
61 unsigned long new_high_table_pointer;
62 new_high_table_pointer = write_smp_table(high_table_pointer);
63 // FIXME make mp table code intelligent enough to know how
64 // much space it's going to need.
65 if (new_high_table_pointer > (high_table_pointer
66 + MAX_MP_TABLE_SIZE))
67 printk(BIOS_ERR, "Increase MP table size.\n");
68
69 printk(BIOS_DEBUG, "MP table: %ld bytes.\n",
70 new_high_table_pointer - high_table_pointer);
71 }
72
73 return rom_table_end;
74 }
75
write_acpi_table(unsigned long rom_table_end)76 static unsigned long write_acpi_table(unsigned long rom_table_end)
77 {
78 unsigned long high_table_pointer;
79 const size_t max_acpi_size = CONFIG_MAX_ACPI_TABLE_SIZE_KB * KiB;
80
81 post_code(POSTCODE_X86_WRITE_ACPITABLE);
82
83 /* Write ACPI tables to F segment and high tables area */
84
85 /* Ok, this is a bit hacky still, because some day we want to have this
86 * completely dynamic. But right now we are setting fixed sizes.
87 * It's probably still better than the old high_table_base code because
88 * now at least we know when we have an overflow in the area.
89 *
90 * We want to use 1MB - 64K for Resume backup. We use 512B for TOC and
91 * 512 byte for GDT, 4K for PIRQ and 4K for MP table and 8KB for the
92 * coreboot table. This leaves us with 47KB for all of ACPI. Let's see
93 * how far we get.
94 */
95 high_table_pointer = (unsigned long)cbmem_add(CBMEM_ID_ACPI,
96 max_acpi_size);
97 if (high_table_pointer) {
98 unsigned long acpi_start = high_table_pointer;
99 unsigned long new_high_table_pointer;
100
101 rom_table_end = ALIGN_UP(rom_table_end, 16);
102 new_high_table_pointer = write_acpi_tables(high_table_pointer);
103 if (new_high_table_pointer > (high_table_pointer
104 + max_acpi_size)) {
105 printk(BIOS_CRIT, "ACPI tables overflowed and corrupted CBMEM!\n");
106 printk(BIOS_ERR, "Increase config MAX_ACPI_TABLE_SIZE_KB!\n");
107 }
108 printk(BIOS_DEBUG, "ACPI tables: %ld bytes.\n",
109 new_high_table_pointer - high_table_pointer);
110
111 /* Now we need to create a low table copy of the RSDP. */
112
113 /* First we look for the high table RSDP */
114 while (acpi_start < new_high_table_pointer) {
115 if (memcmp(((acpi_rsdp_t *)acpi_start)->signature,
116 RSDP_SIG, 8) == 0)
117 break;
118 acpi_start++;
119 }
120
121 /* Now, if we found the RSDP, we take the RSDT and XSDT pointer
122 * from it in order to write the low RSDP
123 */
124 if (acpi_start < new_high_table_pointer) {
125 acpi_rsdp_t *low_rsdp = (acpi_rsdp_t *)rom_table_end,
126 *high_rsdp = (acpi_rsdp_t *)acpi_start;
127
128 /* Technically rsdp length varies but coreboot always
129 writes longest size available. */
130 memcpy(low_rsdp, high_rsdp, sizeof(acpi_rsdp_t));
131 } else {
132 printk(BIOS_ERR, "Didn't find RSDP in high table.\n");
133 }
134 rom_table_end = ALIGN_UP(rom_table_end + sizeof(acpi_rsdp_t), 16);
135 } else {
136 rom_table_end = write_acpi_tables(rom_table_end);
137 rom_table_end = ALIGN_UP(rom_table_end, 1024);
138 }
139
140 return rom_table_end;
141 }
142
write_smbios_table(unsigned long rom_table_end)143 static unsigned long write_smbios_table(unsigned long rom_table_end)
144 {
145 unsigned long high_table_pointer;
146
147 #define MAX_SMBIOS_SIZE (32 * KiB)
148
149 high_table_pointer = (unsigned long)cbmem_add(CBMEM_ID_SMBIOS,
150 MAX_SMBIOS_SIZE);
151 if (high_table_pointer) {
152 unsigned long new_high_table_pointer;
153
154 /*
155 * Clear the entire region to ensure the unused space doesn't
156 * contain garbage from a previous boot, like stale table
157 * signatures that could be found by the OS.
158 */
159 memset((void *)high_table_pointer, 0, MAX_SMBIOS_SIZE);
160
161 new_high_table_pointer =
162 smbios_write_tables(high_table_pointer);
163 rom_table_end = ALIGN_UP(rom_table_end, 16);
164 memcpy((void *)rom_table_end, (void *)high_table_pointer,
165 sizeof(struct smbios_entry));
166 rom_table_end += sizeof(struct smbios_entry);
167
168 if (new_high_table_pointer > (high_table_pointer
169 + MAX_SMBIOS_SIZE))
170 printk(BIOS_ERR, "Increase SMBIOS size\n");
171 printk(BIOS_DEBUG, "SMBIOS tables: %ld bytes.\n",
172 new_high_table_pointer - high_table_pointer);
173 } else {
174 unsigned long new_rom_table_end;
175
176 new_rom_table_end = smbios_write_tables(rom_table_end);
177 printk(BIOS_DEBUG, "SMBIOS size %ld bytes\n", new_rom_table_end
178 - rom_table_end);
179 rom_table_end = ALIGN_UP(new_rom_table_end, 16);
180 }
181
182 return rom_table_end;
183 }
184
185 /* Start forwarding table at 0x500, so we don't run into conflicts with the BDA
186 * in case our data structures grow beyond 0x400. Only GDT
187 * and the coreboot table use low_tables.
188 */
189 #define FORWARDING_TABLE_ADDR ((uintptr_t)0x500)
190 static uintptr_t forwarding_table = FORWARDING_TABLE_ADDR;
191
arch_write_tables(uintptr_t coreboot_table)192 void arch_write_tables(uintptr_t coreboot_table)
193 {
194 size_t sz;
195 unsigned long rom_table_end = 0xf0000;
196
197 /* This table must be between 0x0f0000 and 0x100000 */
198 if (CONFIG(GENERATE_PIRQ_TABLE))
199 rom_table_end = write_pirq_table(rom_table_end);
200
201 /* The smp table must be in 0-1K, 639K-640K, or 960K-1M */
202 if (CONFIG(GENERATE_MP_TABLE))
203 rom_table_end = write_mptable(rom_table_end);
204
205 if (CONFIG(HAVE_ACPI_TABLES))
206 rom_table_end = write_acpi_table(rom_table_end);
207
208 if (CONFIG(GENERATE_SMBIOS_TABLES))
209 rom_table_end = write_smbios_table(rom_table_end);
210
211 sz = write_coreboot_forwarding_table(forwarding_table, coreboot_table);
212
213 forwarding_table += sz;
214 /* Align up to page boundary for historical consistency. */
215 forwarding_table = ALIGN_UP(forwarding_table, 4*KiB);
216
217 /* Tell static analysis we know value is left unused. */
218 (void)rom_table_end;
219 }
220
bootmem_arch_add_ranges(void)221 void bootmem_arch_add_ranges(void)
222 {
223 /* Memory from 0 through the forwarding_table is reserved. */
224 const uintptr_t base = 0;
225
226 bootmem_add_range(base, forwarding_table - base, BM_MEM_TABLE);
227 }
228