blob: 7e653f724529b64be650b747c9940f701912ae36 [file] [log] [blame]
Patrick Georgi11f00792020-03-04 15:10:45 +01001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Stefan Reinauer4f1cb232006-07-19 15:32:49 +00003
Eric Biederman8ca8d762003-04-22 19:02:15 +00004#include <console/console.h>
Aaron Durbind4afa932016-04-19 17:25:59 -05005#include <bootmem.h>
Aaron Durbinf46a9a02017-10-17 14:33:05 -06006#include <bootstate.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +00007#include <boot/tables.h>
Stefan Reinauerca374d42008-01-18 16:16:45 +00008#include <boot/coreboot_tables.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +00009#include <arch/pirq_routing.h>
10#include <arch/smp/mpspec.h>
Stefan Reinauer688b3852004-01-28 16:56:14 +000011#include <arch/acpi.h>
Elyes HAOUASe2d152c2019-06-21 07:06:50 +020012#include <commonlib/helpers.h>
Stefan Reinauer0dff6e32007-10-23 22:17:45 +000013#include <string.h>
Stefan Reinauer3b314022009-10-26 17:04:28 +000014#include <cbmem.h>
Sven Schnelle164bcfd2011-08-14 20:56:34 +020015#include <smbios.h>
Steven J. Magnanid94e1d62005-09-12 18:41:30 +000016
Aaron Durbin86cbfa02016-04-19 15:53:56 -050017static unsigned long write_pirq_table(unsigned long rom_table_end)
Eric Biederman8ca8d762003-04-22 19:02:15 +000018{
Stefan Reinauer3b314022009-10-26 17:04:28 +000019 unsigned long high_table_pointer;
Stefan Reinauer3c7f46b2009-02-27 23:09:55 +000020
Stefan Reinauer3b314022009-10-26 17:04:28 +000021#define MAX_PIRQ_TABLE_SIZE (4 * 1024)
22 post_code(0x9a);
Stefan Reinauerf2152ec2009-05-26 14:07:44 +000023
24 /* This table must be between 0x0f0000 and 0x100000 */
25 rom_table_end = write_pirq_routing_table(rom_table_end);
Felix Held0f6b51b2019-06-20 14:09:01 +020026 rom_table_end = ALIGN_UP(rom_table_end, 1024);
Stefan Reinauerf2152ec2009-05-26 14:07:44 +000027
28 /* And add a high table version for those payloads that
29 * want to live in the F segment
30 */
Lee Leahy6f80ccc2017-03-16 15:18:22 -070031 high_table_pointer = (unsigned long)cbmem_add(CBMEM_ID_PIRQ,
32 MAX_PIRQ_TABLE_SIZE);
Stefan Reinauer3b314022009-10-26 17:04:28 +000033 if (high_table_pointer) {
34 unsigned long new_high_table_pointer;
Lee Leahy6f80ccc2017-03-16 15:18:22 -070035 new_high_table_pointer =
36 write_pirq_routing_table(high_table_pointer);
Stefan Reinauer3b314022009-10-26 17:04:28 +000037 // FIXME make pirq table code intelligent enough to know how
38 // much space it's going to need.
Lee Leahy9c7c6f72017-03-16 11:24:09 -070039 if (new_high_table_pointer > (high_table_pointer
40 + MAX_PIRQ_TABLE_SIZE))
Stefan Reinauer3b314022009-10-26 17:04:28 +000041 printk(BIOS_ERR, "ERROR: Increase PIRQ size.\n");
Stefan Reinauer3b314022009-10-26 17:04:28 +000042 printk(BIOS_DEBUG, "PIRQ table: %ld bytes.\n",
43 new_high_table_pointer - high_table_pointer);
Stefan Reinauerf2152ec2009-05-26 14:07:44 +000044 }
Eric Biederman8ca8d762003-04-22 19:02:15 +000045
Aaron Durbin86cbfa02016-04-19 15:53:56 -050046 return rom_table_end;
47}
Stefan Reinauer3b314022009-10-26 17:04:28 +000048
Aaron Durbin86cbfa02016-04-19 15:53:56 -050049static unsigned long write_mptable(unsigned long rom_table_end)
50{
51 unsigned long high_table_pointer;
52
Stefan Reinauer3b314022009-10-26 17:04:28 +000053#define MAX_MP_TABLE_SIZE (4 * 1024)
54 post_code(0x9b);
55
56 /* The smp table must be in 0-1K, 639K-640K, or 960K-1M */
57 rom_table_end = write_smp_table(rom_table_end);
Felix Held0f6b51b2019-06-20 14:09:01 +020058 rom_table_end = ALIGN_UP(rom_table_end, 1024);
Stefan Reinauer3b314022009-10-26 17:04:28 +000059
Lee Leahy6f80ccc2017-03-16 15:18:22 -070060 high_table_pointer = (unsigned long)cbmem_add(CBMEM_ID_MPTABLE,
61 MAX_MP_TABLE_SIZE);
Stefan Reinauer3b314022009-10-26 17:04:28 +000062 if (high_table_pointer) {
63 unsigned long new_high_table_pointer;
64 new_high_table_pointer = write_smp_table(high_table_pointer);
65 // FIXME make mp table code intelligent enough to know how
66 // much space it's going to need.
Lee Leahy9c7c6f72017-03-16 11:24:09 -070067 if (new_high_table_pointer > (high_table_pointer
68 + MAX_MP_TABLE_SIZE))
Stefan Reinauer3b314022009-10-26 17:04:28 +000069 printk(BIOS_ERR, "ERROR: Increase MP table size.\n");
Stefan Reinauer3b314022009-10-26 17:04:28 +000070
71 printk(BIOS_DEBUG, "MP table: %ld bytes.\n",
72 new_high_table_pointer - high_table_pointer);
73 }
Stefan Reinauer3b314022009-10-26 17:04:28 +000074
Aaron Durbin86cbfa02016-04-19 15:53:56 -050075 return rom_table_end;
76}
77
78static unsigned long write_acpi_table(unsigned long rom_table_end)
79{
80 unsigned long high_table_pointer;
Duncan Laurief02bf352020-03-17 18:32:54 -070081 const size_t max_acpi_size = CONFIG_MAX_ACPI_TABLE_SIZE_KB * KiB;
Kyösti Mälkkiae98e832014-11-28 11:24:19 +020082
Stefan Reinauer3b314022009-10-26 17:04:28 +000083 post_code(0x9c);
Li-Ta Lo8e79fc32004-04-15 17:33:21 +000084
Stefan Reinauerf2152ec2009-05-26 14:07:44 +000085 /* Write ACPI tables to F segment and high tables area */
Stefan Reinauer3b314022009-10-26 17:04:28 +000086
87 /* Ok, this is a bit hacky still, because some day we want to have this
Stefan Reinauer14e22772010-04-27 06:56:47 +000088 * completely dynamic. But right now we are setting fixed sizes.
Stefan Reinauer3b314022009-10-26 17:04:28 +000089 * It's probably still better than the old high_table_base code because
90 * now at least we know when we have an overflow in the area.
91 *
92 * We want to use 1MB - 64K for Resume backup. We use 512B for TOC and
93 * 512 byte for GDT, 4K for PIRQ and 4K for MP table and 8KB for the
94 * coreboot table. This leaves us with 47KB for all of ACPI. Let's see
95 * how far we get.
96 */
Lee Leahy6f80ccc2017-03-16 15:18:22 -070097 high_table_pointer = (unsigned long)cbmem_add(CBMEM_ID_ACPI,
Duncan Laurief02bf352020-03-17 18:32:54 -070098 max_acpi_size);
Stefan Reinauer3b314022009-10-26 17:04:28 +000099 if (high_table_pointer) {
100 unsigned long acpi_start = high_table_pointer;
101 unsigned long new_high_table_pointer;
102
Felix Held0f6b51b2019-06-20 14:09:01 +0200103 rom_table_end = ALIGN_UP(rom_table_end, 16);
Stefan Reinauer3b314022009-10-26 17:04:28 +0000104 new_high_table_pointer = write_acpi_tables(high_table_pointer);
Lee Leahy024b13d2017-03-16 13:41:11 -0700105 if (new_high_table_pointer > (high_table_pointer
Duncan Laurief02bf352020-03-17 18:32:54 -0700106 + max_acpi_size))
Stefan Reinauer3b314022009-10-26 17:04:28 +0000107 printk(BIOS_ERR, "ERROR: Increase ACPI size\n");
Martin Rothe3690102016-01-06 15:21:02 -0700108 printk(BIOS_DEBUG, "ACPI tables: %ld bytes.\n",
Stefan Reinauer3b314022009-10-26 17:04:28 +0000109 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) {
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700115 if (memcmp(((acpi_rsdp_t *)acpi_start)->signature,
116 RSDP_SIG, 8) == 0)
Patrick Georgibab4f922009-05-26 19:39:14 +0000117 break;
Patrick Georgibab4f922009-05-26 19:39:14 +0000118 acpi_start++;
119 }
Stefan Reinauer3b314022009-10-26 17:04:28 +0000120
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) {
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000125 acpi_rsdp_t *low_rsdp = (acpi_rsdp_t *)rom_table_end,
126 *high_rsdp = (acpi_rsdp_t *)acpi_start;
127
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100128 /* Technically rsdp length varies but coreboot always
129 writes longest size available. */
130 memcpy(low_rsdp, high_rsdp, sizeof(acpi_rsdp_t));
Patrick Georgibab4f922009-05-26 19:39:14 +0000131 } else {
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700132 printk(BIOS_ERR,
133 "ERROR: Didn't find RSDP in high table.\n");
Patrick Georgibab4f922009-05-26 19:39:14 +0000134 }
Felix Held0f6b51b2019-06-20 14:09:01 +0200135 rom_table_end = ALIGN_UP(rom_table_end + sizeof(acpi_rsdp_t), 16);
Stefan Reinauerf2152ec2009-05-26 14:07:44 +0000136 } else {
137 rom_table_end = write_acpi_tables(rom_table_end);
Felix Held0f6b51b2019-06-20 14:09:01 +0200138 rom_table_end = ALIGN_UP(rom_table_end, 1024);
Myles Watsonfa12b672009-04-30 22:45:41 +0000139 }
Stefan Reinauer3b314022009-10-26 17:04:28 +0000140
Aaron Durbin86cbfa02016-04-19 15:53:56 -0500141 return rom_table_end;
142}
143
144static unsigned long write_smbios_table(unsigned long rom_table_end)
145{
146 unsigned long high_table_pointer;
147
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200148#define MAX_SMBIOS_SIZE 2048
Aaron Durbin86cbfa02016-04-19 15:53:56 -0500149
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700150 high_table_pointer = (unsigned long)cbmem_add(CBMEM_ID_SMBIOS,
151 MAX_SMBIOS_SIZE);
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200152 if (high_table_pointer) {
153 unsigned long new_high_table_pointer;
Steven J. Magnanid94e1d62005-09-12 18:41:30 +0000154
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700155 new_high_table_pointer =
156 smbios_write_tables(high_table_pointer);
Felix Held0f6b51b2019-06-20 14:09:01 +0200157 rom_table_end = ALIGN_UP(rom_table_end, 16);
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700158 memcpy((void *)rom_table_end, (void *)high_table_pointer,
159 sizeof(struct smbios_entry));
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200160 rom_table_end += sizeof(struct smbios_entry);
161
Lee Leahy024b13d2017-03-16 13:41:11 -0700162 if (new_high_table_pointer > (high_table_pointer
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700163 + MAX_SMBIOS_SIZE))
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200164 printk(BIOS_ERR, "ERROR: Increase SMBIOS size\n");
Martin Rothe3690102016-01-06 15:21:02 -0700165 printk(BIOS_DEBUG, "SMBIOS tables: %ld bytes.\n",
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200166 new_high_table_pointer - high_table_pointer);
167 } else {
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700168 unsigned long new_rom_table_end;
169
170 new_rom_table_end = smbios_write_tables(rom_table_end);
171 printk(BIOS_DEBUG, "SMBIOS size %ld bytes\n", new_rom_table_end
172 - rom_table_end);
Felix Held0f6b51b2019-06-20 14:09:01 +0200173 rom_table_end = ALIGN_UP(new_rom_table_end, 16);
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200174 }
Aaron Durbin86cbfa02016-04-19 15:53:56 -0500175
176 return rom_table_end;
177}
178
Aaron Durbina4db0502016-04-19 21:38:18 -0500179/* Start forwarding table at 0x500, so we don't run into conflicts with the BDA
180 * in case our data structures grow beyond 0x400. Only GDT
181 * and the coreboot table use low_tables.
182 */
Aaron Durbinf46a9a02017-10-17 14:33:05 -0600183#define FORWARDING_TABLE_ADDR ((uintptr_t)0x500)
184static uintptr_t forwarding_table = FORWARDING_TABLE_ADDR;
185
Aaron Durbin5481c962016-04-19 20:37:51 -0500186void arch_write_tables(uintptr_t coreboot_table)
Aaron Durbind4afa932016-04-19 17:25:59 -0500187{
Aaron Durbina4db0502016-04-19 21:38:18 -0500188 size_t sz;
Aaron Durbin5481c962016-04-19 20:37:51 -0500189 unsigned long rom_table_end = 0xf0000;
Aaron Durbin86cbfa02016-04-19 15:53:56 -0500190
191 /* This table must be between 0x0f0000 and 0x100000 */
Julius Wernercd49cce2019-03-05 16:53:33 -0800192 if (CONFIG(GENERATE_PIRQ_TABLE))
Aaron Durbin86cbfa02016-04-19 15:53:56 -0500193 rom_table_end = write_pirq_table(rom_table_end);
194
195 /* The smp table must be in 0-1K, 639K-640K, or 960K-1M */
Julius Wernercd49cce2019-03-05 16:53:33 -0800196 if (CONFIG(GENERATE_MP_TABLE))
Aaron Durbin86cbfa02016-04-19 15:53:56 -0500197 rom_table_end = write_mptable(rom_table_end);
198
Julius Wernercd49cce2019-03-05 16:53:33 -0800199 if (CONFIG(HAVE_ACPI_TABLES))
Aaron Durbin86cbfa02016-04-19 15:53:56 -0500200 rom_table_end = write_acpi_table(rom_table_end);
201
Julius Wernercd49cce2019-03-05 16:53:33 -0800202 if (CONFIG(GENERATE_SMBIOS_TABLES))
Aaron Durbin86cbfa02016-04-19 15:53:56 -0500203 rom_table_end = write_smbios_table(rom_table_end);
Aaron Durbina4db0502016-04-19 21:38:18 -0500204
205 sz = write_coreboot_forwarding_table(forwarding_table, coreboot_table);
206
207 forwarding_table += sz;
208 /* Align up to page boundary for historical consistency. */
209 forwarding_table = ALIGN_UP(forwarding_table, 4*KiB);
Richard Spiegelbb7424f2018-08-08 09:58:34 -0700210
211 /* Tell static analysis we know value is left unused. */
212 (void)rom_table_end;
Aaron Durbin5481c962016-04-19 20:37:51 -0500213}
214
215void bootmem_arch_add_ranges(void)
216{
Aaron Durbina4db0502016-04-19 21:38:18 -0500217 /* Memory from 0 through the forwarding_table is reserved. */
218 const uintptr_t base = 0;
Aaron Durbin5481c962016-04-19 20:37:51 -0500219
Patrick Rudolph9ab9db02018-04-05 09:14:51 +0200220 bootmem_add_range(base, forwarding_table - base, BM_MEM_TABLE);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000221}