blob: 1cff61436df22966e2bc092f2c202f32c9d5553a [file] [log] [blame]
Patrick Georgi11f00792020-03-04 15:10:45 +01001/* SPDX-License-Identifier: GPL-2.0-only */
Stefan Reinauer4f1cb232006-07-19 15:32:49 +00002
Eric Biederman8ca8d762003-04-22 19:02:15 +00003#include <console/console.h>
Aaron Durbind4afa932016-04-19 17:25:59 -05004#include <bootmem.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +00005#include <boot/tables.h>
Stefan Reinauerca374d42008-01-18 16:16:45 +00006#include <boot/coreboot_tables.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +00007#include <arch/pirq_routing.h>
8#include <arch/smp/mpspec.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -07009#include <acpi/acpi.h>
Elyes HAOUASe2d152c2019-06-21 07:06:50 +020010#include <commonlib/helpers.h>
Stefan Reinauer0dff6e32007-10-23 22:17:45 +000011#include <string.h>
Stefan Reinauer3b314022009-10-26 17:04:28 +000012#include <cbmem.h>
Sven Schnelle164bcfd2011-08-14 20:56:34 +020013#include <smbios.h>
Steven J. Magnanid94e1d62005-09-12 18:41:30 +000014
Aaron Durbin86cbfa02016-04-19 15:53:56 -050015static unsigned long write_pirq_table(unsigned long rom_table_end)
Eric Biederman8ca8d762003-04-22 19:02:15 +000016{
Stefan Reinauer3b314022009-10-26 17:04:28 +000017 unsigned long high_table_pointer;
Stefan Reinauer3c7f46b2009-02-27 23:09:55 +000018
Stefan Reinauer3b314022009-10-26 17:04:28 +000019#define MAX_PIRQ_TABLE_SIZE (4 * 1024)
lilacious40cb3fe2023-06-21 23:24:14 +020020 post_code(POSTCODE_X86_WRITE_PIRQ_TABLE);
Stefan Reinauerf2152ec2009-05-26 14:07:44 +000021
22 /* This table must be between 0x0f0000 and 0x100000 */
23 rom_table_end = write_pirq_routing_table(rom_table_end);
Felix Held0f6b51b2019-06-20 14:09:01 +020024 rom_table_end = ALIGN_UP(rom_table_end, 1024);
Stefan Reinauerf2152ec2009-05-26 14:07:44 +000025
26 /* And add a high table version for those payloads that
27 * want to live in the F segment
28 */
Lee Leahy6f80ccc2017-03-16 15:18:22 -070029 high_table_pointer = (unsigned long)cbmem_add(CBMEM_ID_PIRQ,
30 MAX_PIRQ_TABLE_SIZE);
Stefan Reinauer3b314022009-10-26 17:04:28 +000031 if (high_table_pointer) {
32 unsigned long new_high_table_pointer;
Lee Leahy6f80ccc2017-03-16 15:18:22 -070033 new_high_table_pointer =
34 write_pirq_routing_table(high_table_pointer);
Stefan Reinauer3b314022009-10-26 17:04:28 +000035 // FIXME make pirq table code intelligent enough to know how
36 // much space it's going to need.
Lee Leahy9c7c6f72017-03-16 11:24:09 -070037 if (new_high_table_pointer > (high_table_pointer
38 + MAX_PIRQ_TABLE_SIZE))
Julius Wernere9665952022-01-21 17:06:20 -080039 printk(BIOS_ERR, "Increase PIRQ size.\n");
Stefan Reinauer3b314022009-10-26 17:04:28 +000040 printk(BIOS_DEBUG, "PIRQ table: %ld bytes.\n",
41 new_high_table_pointer - high_table_pointer);
Stefan Reinauerf2152ec2009-05-26 14:07:44 +000042 }
Eric Biederman8ca8d762003-04-22 19:02:15 +000043
Aaron Durbin86cbfa02016-04-19 15:53:56 -050044 return rom_table_end;
45}
Stefan Reinauer3b314022009-10-26 17:04:28 +000046
Aaron Durbin86cbfa02016-04-19 15:53:56 -050047static unsigned long write_mptable(unsigned long rom_table_end)
48{
49 unsigned long high_table_pointer;
50
Stefan Reinauer3b314022009-10-26 17:04:28 +000051#define MAX_MP_TABLE_SIZE (4 * 1024)
lilacious40cb3fe2023-06-21 23:24:14 +020052 post_code(POSTCODE_X86_WRITE_MPTABLE);
Stefan Reinauer3b314022009-10-26 17:04:28 +000053
54 /* The smp table must be in 0-1K, 639K-640K, or 960K-1M */
55 rom_table_end = write_smp_table(rom_table_end);
Felix Held0f6b51b2019-06-20 14:09:01 +020056 rom_table_end = ALIGN_UP(rom_table_end, 1024);
Stefan Reinauer3b314022009-10-26 17:04:28 +000057
Lee Leahy6f80ccc2017-03-16 15:18:22 -070058 high_table_pointer = (unsigned long)cbmem_add(CBMEM_ID_MPTABLE,
59 MAX_MP_TABLE_SIZE);
Stefan Reinauer3b314022009-10-26 17:04:28 +000060 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.
Lee Leahy9c7c6f72017-03-16 11:24:09 -070065 if (new_high_table_pointer > (high_table_pointer
66 + MAX_MP_TABLE_SIZE))
Julius Wernere9665952022-01-21 17:06:20 -080067 printk(BIOS_ERR, "Increase MP table size.\n");
Stefan Reinauer3b314022009-10-26 17:04:28 +000068
69 printk(BIOS_DEBUG, "MP table: %ld bytes.\n",
70 new_high_table_pointer - high_table_pointer);
71 }
Stefan Reinauer3b314022009-10-26 17:04:28 +000072
Aaron Durbin86cbfa02016-04-19 15:53:56 -050073 return rom_table_end;
74}
75
76static unsigned long write_acpi_table(unsigned long rom_table_end)
77{
78 unsigned long high_table_pointer;
Duncan Laurief02bf352020-03-17 18:32:54 -070079 const size_t max_acpi_size = CONFIG_MAX_ACPI_TABLE_SIZE_KB * KiB;
Kyösti Mälkkiae98e832014-11-28 11:24:19 +020080
lilacious40cb3fe2023-06-21 23:24:14 +020081 post_code(POSTCODE_X86_WRITE_ACPITABLE);
Li-Ta Lo8e79fc32004-04-15 17:33:21 +000082
Stefan Reinauerf2152ec2009-05-26 14:07:44 +000083 /* Write ACPI tables to F segment and high tables area */
Stefan Reinauer3b314022009-10-26 17:04:28 +000084
85 /* Ok, this is a bit hacky still, because some day we want to have this
Stefan Reinauer14e22772010-04-27 06:56:47 +000086 * completely dynamic. But right now we are setting fixed sizes.
Stefan Reinauer3b314022009-10-26 17:04:28 +000087 * 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 */
Lee Leahy6f80ccc2017-03-16 15:18:22 -070095 high_table_pointer = (unsigned long)cbmem_add(CBMEM_ID_ACPI,
Duncan Laurief02bf352020-03-17 18:32:54 -070096 max_acpi_size);
Stefan Reinauer3b314022009-10-26 17:04:28 +000097 if (high_table_pointer) {
98 unsigned long acpi_start = high_table_pointer;
99 unsigned long new_high_table_pointer;
100
Felix Held0f6b51b2019-06-20 14:09:01 +0200101 rom_table_end = ALIGN_UP(rom_table_end, 16);
Stefan Reinauer3b314022009-10-26 17:04:28 +0000102 new_high_table_pointer = write_acpi_tables(high_table_pointer);
Lee Leahy024b13d2017-03-16 13:41:11 -0700103 if (new_high_table_pointer > (high_table_pointer
Duncan Laurief02bf352020-03-17 18:32:54 -0700104 + max_acpi_size))
Julius Wernere9665952022-01-21 17:06:20 -0800105 printk(BIOS_ERR, "Increase ACPI size\n");
Martin Rothe3690102016-01-06 15:21:02 -0700106 printk(BIOS_DEBUG, "ACPI tables: %ld bytes.\n",
Stefan Reinauer3b314022009-10-26 17:04:28 +0000107 new_high_table_pointer - high_table_pointer);
108
109 /* Now we need to create a low table copy of the RSDP. */
110
111 /* First we look for the high table RSDP */
112 while (acpi_start < new_high_table_pointer) {
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700113 if (memcmp(((acpi_rsdp_t *)acpi_start)->signature,
114 RSDP_SIG, 8) == 0)
Patrick Georgibab4f922009-05-26 19:39:14 +0000115 break;
Patrick Georgibab4f922009-05-26 19:39:14 +0000116 acpi_start++;
117 }
Stefan Reinauer3b314022009-10-26 17:04:28 +0000118
119 /* Now, if we found the RSDP, we take the RSDT and XSDT pointer
120 * from it in order to write the low RSDP
121 */
122 if (acpi_start < new_high_table_pointer) {
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000123 acpi_rsdp_t *low_rsdp = (acpi_rsdp_t *)rom_table_end,
124 *high_rsdp = (acpi_rsdp_t *)acpi_start;
125
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100126 /* Technically rsdp length varies but coreboot always
127 writes longest size available. */
128 memcpy(low_rsdp, high_rsdp, sizeof(acpi_rsdp_t));
Patrick Georgibab4f922009-05-26 19:39:14 +0000129 } else {
Julius Wernere9665952022-01-21 17:06:20 -0800130 printk(BIOS_ERR, "Didn't find RSDP in high table.\n");
Patrick Georgibab4f922009-05-26 19:39:14 +0000131 }
Felix Held0f6b51b2019-06-20 14:09:01 +0200132 rom_table_end = ALIGN_UP(rom_table_end + sizeof(acpi_rsdp_t), 16);
Stefan Reinauerf2152ec2009-05-26 14:07:44 +0000133 } else {
134 rom_table_end = write_acpi_tables(rom_table_end);
Felix Held0f6b51b2019-06-20 14:09:01 +0200135 rom_table_end = ALIGN_UP(rom_table_end, 1024);
Myles Watsonfa12b672009-04-30 22:45:41 +0000136 }
Stefan Reinauer3b314022009-10-26 17:04:28 +0000137
Aaron Durbin86cbfa02016-04-19 15:53:56 -0500138 return rom_table_end;
139}
140
141static unsigned long write_smbios_table(unsigned long rom_table_end)
142{
143 unsigned long high_table_pointer;
144
Arthur Heymans0c949852022-04-07 22:42:42 +0200145#define MAX_SMBIOS_SIZE (32 * KiB)
Aaron Durbin86cbfa02016-04-19 15:53:56 -0500146
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700147 high_table_pointer = (unsigned long)cbmem_add(CBMEM_ID_SMBIOS,
148 MAX_SMBIOS_SIZE);
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200149 if (high_table_pointer) {
150 unsigned long new_high_table_pointer;
Steven J. Magnanid94e1d62005-09-12 18:41:30 +0000151
Jonathon Hall56849412022-08-02 16:00:35 -0400152 /*
153 * Clear the entire region to ensure the unused space doesn't
154 * contain garbage from a previous boot, like stale table
155 * signatures that could be found by the OS.
156 */
157 memset((void *)high_table_pointer, 0, MAX_SMBIOS_SIZE);
158
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700159 new_high_table_pointer =
160 smbios_write_tables(high_table_pointer);
Felix Held0f6b51b2019-06-20 14:09:01 +0200161 rom_table_end = ALIGN_UP(rom_table_end, 16);
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700162 memcpy((void *)rom_table_end, (void *)high_table_pointer,
163 sizeof(struct smbios_entry));
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200164 rom_table_end += sizeof(struct smbios_entry);
165
Lee Leahy024b13d2017-03-16 13:41:11 -0700166 if (new_high_table_pointer > (high_table_pointer
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700167 + MAX_SMBIOS_SIZE))
Julius Wernere9665952022-01-21 17:06:20 -0800168 printk(BIOS_ERR, "Increase SMBIOS size\n");
Martin Rothe3690102016-01-06 15:21:02 -0700169 printk(BIOS_DEBUG, "SMBIOS tables: %ld bytes.\n",
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200170 new_high_table_pointer - high_table_pointer);
171 } else {
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700172 unsigned long new_rom_table_end;
173
174 new_rom_table_end = smbios_write_tables(rom_table_end);
175 printk(BIOS_DEBUG, "SMBIOS size %ld bytes\n", new_rom_table_end
176 - rom_table_end);
Felix Held0f6b51b2019-06-20 14:09:01 +0200177 rom_table_end = ALIGN_UP(new_rom_table_end, 16);
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200178 }
Aaron Durbin86cbfa02016-04-19 15:53:56 -0500179
180 return rom_table_end;
181}
182
Aaron Durbina4db0502016-04-19 21:38:18 -0500183/* Start forwarding table at 0x500, so we don't run into conflicts with the BDA
184 * in case our data structures grow beyond 0x400. Only GDT
185 * and the coreboot table use low_tables.
186 */
Aaron Durbinf46a9a02017-10-17 14:33:05 -0600187#define FORWARDING_TABLE_ADDR ((uintptr_t)0x500)
188static uintptr_t forwarding_table = FORWARDING_TABLE_ADDR;
189
Aaron Durbin5481c962016-04-19 20:37:51 -0500190void arch_write_tables(uintptr_t coreboot_table)
Aaron Durbind4afa932016-04-19 17:25:59 -0500191{
Aaron Durbina4db0502016-04-19 21:38:18 -0500192 size_t sz;
Aaron Durbin5481c962016-04-19 20:37:51 -0500193 unsigned long rom_table_end = 0xf0000;
Aaron Durbin86cbfa02016-04-19 15:53:56 -0500194
195 /* This table must be between 0x0f0000 and 0x100000 */
Julius Wernercd49cce2019-03-05 16:53:33 -0800196 if (CONFIG(GENERATE_PIRQ_TABLE))
Aaron Durbin86cbfa02016-04-19 15:53:56 -0500197 rom_table_end = write_pirq_table(rom_table_end);
198
199 /* The smp table must be in 0-1K, 639K-640K, or 960K-1M */
Julius Wernercd49cce2019-03-05 16:53:33 -0800200 if (CONFIG(GENERATE_MP_TABLE))
Aaron Durbin86cbfa02016-04-19 15:53:56 -0500201 rom_table_end = write_mptable(rom_table_end);
202
Julius Wernercd49cce2019-03-05 16:53:33 -0800203 if (CONFIG(HAVE_ACPI_TABLES))
Aaron Durbin86cbfa02016-04-19 15:53:56 -0500204 rom_table_end = write_acpi_table(rom_table_end);
205
Julius Wernercd49cce2019-03-05 16:53:33 -0800206 if (CONFIG(GENERATE_SMBIOS_TABLES))
Aaron Durbin86cbfa02016-04-19 15:53:56 -0500207 rom_table_end = write_smbios_table(rom_table_end);
Aaron Durbina4db0502016-04-19 21:38:18 -0500208
209 sz = write_coreboot_forwarding_table(forwarding_table, coreboot_table);
210
211 forwarding_table += sz;
212 /* Align up to page boundary for historical consistency. */
213 forwarding_table = ALIGN_UP(forwarding_table, 4*KiB);
Richard Spiegelbb7424f2018-08-08 09:58:34 -0700214
215 /* Tell static analysis we know value is left unused. */
216 (void)rom_table_end;
Aaron Durbin5481c962016-04-19 20:37:51 -0500217}
218
219void bootmem_arch_add_ranges(void)
220{
Aaron Durbina4db0502016-04-19 21:38:18 -0500221 /* Memory from 0 through the forwarding_table is reserved. */
222 const uintptr_t base = 0;
Aaron Durbin5481c962016-04-19 20:37:51 -0500223
Patrick Rudolph9ab9db02018-04-05 09:14:51 +0200224 bootmem_add_range(base, forwarding_table - base, BM_MEM_TABLE);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000225}