blob: 066e6356754f2332210649ba7086aa507e81f487 [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;
81
Stefan Reinauer802c9102014-08-28 23:03:15 +020082#define MAX_ACPI_SIZE (144 * 1024)
Kyösti Mälkkiae98e832014-11-28 11:24:19 +020083
Stefan Reinauer3b314022009-10-26 17:04:28 +000084 post_code(0x9c);
Li-Ta Lo8e79fc32004-04-15 17:33:21 +000085
Stefan Reinauerf2152ec2009-05-26 14:07:44 +000086 /* Write ACPI tables to F segment and high tables area */
Stefan Reinauer3b314022009-10-26 17:04:28 +000087
88 /* Ok, this is a bit hacky still, because some day we want to have this
Stefan Reinauer14e22772010-04-27 06:56:47 +000089 * completely dynamic. But right now we are setting fixed sizes.
Stefan Reinauer3b314022009-10-26 17:04:28 +000090 * It's probably still better than the old high_table_base code because
91 * now at least we know when we have an overflow in the area.
92 *
93 * We want to use 1MB - 64K for Resume backup. We use 512B for TOC and
94 * 512 byte for GDT, 4K for PIRQ and 4K for MP table and 8KB for the
95 * coreboot table. This leaves us with 47KB for all of ACPI. Let's see
96 * how far we get.
97 */
Lee Leahy6f80ccc2017-03-16 15:18:22 -070098 high_table_pointer = (unsigned long)cbmem_add(CBMEM_ID_ACPI,
99 MAX_ACPI_SIZE);
Stefan Reinauer3b314022009-10-26 17:04:28 +0000100 if (high_table_pointer) {
101 unsigned long acpi_start = high_table_pointer;
102 unsigned long new_high_table_pointer;
103
Felix Held0f6b51b2019-06-20 14:09:01 +0200104 rom_table_end = ALIGN_UP(rom_table_end, 16);
Stefan Reinauer3b314022009-10-26 17:04:28 +0000105 new_high_table_pointer = write_acpi_tables(high_table_pointer);
Lee Leahy024b13d2017-03-16 13:41:11 -0700106 if (new_high_table_pointer > (high_table_pointer
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700107 + MAX_ACPI_SIZE))
Stefan Reinauer3b314022009-10-26 17:04:28 +0000108 printk(BIOS_ERR, "ERROR: Increase ACPI size\n");
Martin Rothe3690102016-01-06 15:21:02 -0700109 printk(BIOS_DEBUG, "ACPI tables: %ld bytes.\n",
Stefan Reinauer3b314022009-10-26 17:04:28 +0000110 new_high_table_pointer - high_table_pointer);
111
112 /* Now we need to create a low table copy of the RSDP. */
113
114 /* First we look for the high table RSDP */
115 while (acpi_start < new_high_table_pointer) {
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700116 if (memcmp(((acpi_rsdp_t *)acpi_start)->signature,
117 RSDP_SIG, 8) == 0)
Patrick Georgibab4f922009-05-26 19:39:14 +0000118 break;
Patrick Georgibab4f922009-05-26 19:39:14 +0000119 acpi_start++;
120 }
Stefan Reinauer3b314022009-10-26 17:04:28 +0000121
122 /* Now, if we found the RSDP, we take the RSDT and XSDT pointer
123 * from it in order to write the low RSDP
124 */
125 if (acpi_start < new_high_table_pointer) {
Stefan Reinauerb657a3c2009-07-21 21:38:33 +0000126 acpi_rsdp_t *low_rsdp = (acpi_rsdp_t *)rom_table_end,
127 *high_rsdp = (acpi_rsdp_t *)acpi_start;
128
Vladimir Serbinenko351fefc2014-10-26 20:42:08 +0100129 /* Technically rsdp length varies but coreboot always
130 writes longest size available. */
131 memcpy(low_rsdp, high_rsdp, sizeof(acpi_rsdp_t));
Patrick Georgibab4f922009-05-26 19:39:14 +0000132 } else {
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700133 printk(BIOS_ERR,
134 "ERROR: Didn't find RSDP in high table.\n");
Patrick Georgibab4f922009-05-26 19:39:14 +0000135 }
Felix Held0f6b51b2019-06-20 14:09:01 +0200136 rom_table_end = ALIGN_UP(rom_table_end + sizeof(acpi_rsdp_t), 16);
Stefan Reinauerf2152ec2009-05-26 14:07:44 +0000137 } else {
138 rom_table_end = write_acpi_tables(rom_table_end);
Felix Held0f6b51b2019-06-20 14:09:01 +0200139 rom_table_end = ALIGN_UP(rom_table_end, 1024);
Myles Watsonfa12b672009-04-30 22:45:41 +0000140 }
Stefan Reinauer3b314022009-10-26 17:04:28 +0000141
Aaron Durbin86cbfa02016-04-19 15:53:56 -0500142 return rom_table_end;
143}
144
145static unsigned long write_smbios_table(unsigned long rom_table_end)
146{
147 unsigned long high_table_pointer;
148
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200149#define MAX_SMBIOS_SIZE 2048
Aaron Durbin86cbfa02016-04-19 15:53:56 -0500150
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700151 high_table_pointer = (unsigned long)cbmem_add(CBMEM_ID_SMBIOS,
152 MAX_SMBIOS_SIZE);
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200153 if (high_table_pointer) {
154 unsigned long new_high_table_pointer;
Steven J. Magnanid94e1d62005-09-12 18:41:30 +0000155
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700156 new_high_table_pointer =
157 smbios_write_tables(high_table_pointer);
Felix Held0f6b51b2019-06-20 14:09:01 +0200158 rom_table_end = ALIGN_UP(rom_table_end, 16);
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700159 memcpy((void *)rom_table_end, (void *)high_table_pointer,
160 sizeof(struct smbios_entry));
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200161 rom_table_end += sizeof(struct smbios_entry);
162
Lee Leahy024b13d2017-03-16 13:41:11 -0700163 if (new_high_table_pointer > (high_table_pointer
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700164 + MAX_SMBIOS_SIZE))
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200165 printk(BIOS_ERR, "ERROR: Increase SMBIOS size\n");
Martin Rothe3690102016-01-06 15:21:02 -0700166 printk(BIOS_DEBUG, "SMBIOS tables: %ld bytes.\n",
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200167 new_high_table_pointer - high_table_pointer);
168 } else {
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700169 unsigned long new_rom_table_end;
170
171 new_rom_table_end = smbios_write_tables(rom_table_end);
172 printk(BIOS_DEBUG, "SMBIOS size %ld bytes\n", new_rom_table_end
173 - rom_table_end);
Felix Held0f6b51b2019-06-20 14:09:01 +0200174 rom_table_end = ALIGN_UP(new_rom_table_end, 16);
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200175 }
Aaron Durbin86cbfa02016-04-19 15:53:56 -0500176
177 return rom_table_end;
178}
179
Aaron Durbina4db0502016-04-19 21:38:18 -0500180/* Start forwarding table at 0x500, so we don't run into conflicts with the BDA
181 * in case our data structures grow beyond 0x400. Only GDT
182 * and the coreboot table use low_tables.
183 */
Aaron Durbinf46a9a02017-10-17 14:33:05 -0600184#define FORWARDING_TABLE_ADDR ((uintptr_t)0x500)
185static uintptr_t forwarding_table = FORWARDING_TABLE_ADDR;
186
Aaron Durbin5481c962016-04-19 20:37:51 -0500187void arch_write_tables(uintptr_t coreboot_table)
Aaron Durbind4afa932016-04-19 17:25:59 -0500188{
Aaron Durbina4db0502016-04-19 21:38:18 -0500189 size_t sz;
Aaron Durbin5481c962016-04-19 20:37:51 -0500190 unsigned long rom_table_end = 0xf0000;
Aaron Durbin86cbfa02016-04-19 15:53:56 -0500191
192 /* This table must be between 0x0f0000 and 0x100000 */
Julius Wernercd49cce2019-03-05 16:53:33 -0800193 if (CONFIG(GENERATE_PIRQ_TABLE))
Aaron Durbin86cbfa02016-04-19 15:53:56 -0500194 rom_table_end = write_pirq_table(rom_table_end);
195
196 /* The smp table must be in 0-1K, 639K-640K, or 960K-1M */
Julius Wernercd49cce2019-03-05 16:53:33 -0800197 if (CONFIG(GENERATE_MP_TABLE))
Aaron Durbin86cbfa02016-04-19 15:53:56 -0500198 rom_table_end = write_mptable(rom_table_end);
199
Julius Wernercd49cce2019-03-05 16:53:33 -0800200 if (CONFIG(HAVE_ACPI_TABLES))
Aaron Durbin86cbfa02016-04-19 15:53:56 -0500201 rom_table_end = write_acpi_table(rom_table_end);
202
Julius Wernercd49cce2019-03-05 16:53:33 -0800203 if (CONFIG(GENERATE_SMBIOS_TABLES))
Aaron Durbin86cbfa02016-04-19 15:53:56 -0500204 rom_table_end = write_smbios_table(rom_table_end);
Aaron Durbina4db0502016-04-19 21:38:18 -0500205
206 sz = write_coreboot_forwarding_table(forwarding_table, coreboot_table);
207
208 forwarding_table += sz;
209 /* Align up to page boundary for historical consistency. */
210 forwarding_table = ALIGN_UP(forwarding_table, 4*KiB);
Richard Spiegelbb7424f2018-08-08 09:58:34 -0700211
212 /* Tell static analysis we know value is left unused. */
213 (void)rom_table_end;
Aaron Durbin5481c962016-04-19 20:37:51 -0500214}
215
216void bootmem_arch_add_ranges(void)
217{
Aaron Durbina4db0502016-04-19 21:38:18 -0500218 /* Memory from 0 through the forwarding_table is reserved. */
219 const uintptr_t base = 0;
Aaron Durbin5481c962016-04-19 20:37:51 -0500220
Patrick Rudolph9ab9db02018-04-05 09:14:51 +0200221 bootmem_add_range(base, forwarding_table - base, BM_MEM_TABLE);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000222}