Fill out ChromeOS specific coreboot table extensions
[coreboot.git] / src / arch / x86 / boot / tables.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2003 Eric Biederman
5  * Copyright (C) 2005 Steve Magnani
6  * Copyright (C) 2008-2009 coresystems GmbH
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20  */
21
22 #include <console/console.h>
23 #include <cpu/cpu.h>
24 #include <boot/tables.h>
25 #include <boot/coreboot_tables.h>
26 #include <arch/coreboot_tables.h>
27 #include <arch/pirq_routing.h>
28 #include <arch/smp/mpspec.h>
29 #include <arch/acpi.h>
30 #include <string.h>
31 #include <cpu/x86/multiboot.h>
32 #include <cbmem.h>
33 #include <lib.h>
34 #include <smbios.h>
35
36 uint64_t high_tables_base = 0;
37 uint64_t high_tables_size;
38
39 void cbmem_arch_init(void)
40 {
41         /* defined in gdt.c */
42         move_gdt();
43 }
44
45 struct lb_memory *write_tables(void)
46 {
47         unsigned long low_table_start, low_table_end;
48         unsigned long rom_table_start, rom_table_end;
49
50         /* Even if high tables are configured, some tables are copied both to
51          * the low and the high area, so payloads and OSes don't need to know
52          * about the high tables.
53          */
54         unsigned long high_table_pointer;
55
56         if (!high_tables_base) {
57                 printk(BIOS_ERR, "ERROR: High Tables Base is not set.\n");
58                 // Are there any boards without?
59                 // Stepan thinks we should die() here!
60         }
61
62         printk(BIOS_DEBUG, "High Tables Base is %llx.\n", high_tables_base);
63
64         rom_table_start = 0xf0000;
65         rom_table_end =   0xf0000;
66
67         /* Start low addr at 0x500, so we don't run into conflicts with the BDA
68          * in case our data structures grow beyound 0x400. Only multiboot, GDT
69          * and the coreboot table use low_tables.
70          */
71         low_table_start = 0;
72         low_table_end = 0x500;
73
74 #if CONFIG_GENERATE_PIRQ_TABLE == 1
75 #define MAX_PIRQ_TABLE_SIZE (4 * 1024)
76         post_code(0x9a);
77
78         /* This table must be between 0x0f0000 and 0x100000 */
79         rom_table_end = write_pirq_routing_table(rom_table_end);
80         rom_table_end = ALIGN(rom_table_end, 1024);
81
82         /* And add a high table version for those payloads that
83          * want to live in the F segment
84          */
85         high_table_pointer = (unsigned long)cbmem_add(CBMEM_ID_PIRQ, MAX_PIRQ_TABLE_SIZE);
86         if (high_table_pointer) {
87                 unsigned long new_high_table_pointer;
88                 new_high_table_pointer = write_pirq_routing_table(high_table_pointer);
89                 // FIXME make pirq table code intelligent enough to know how
90                 // much space it's going to need.
91                 if (new_high_table_pointer > (high_table_pointer + MAX_PIRQ_TABLE_SIZE)) {
92                         printk(BIOS_ERR, "ERROR: Increase PIRQ size.\n");
93                 }
94                 printk(BIOS_DEBUG, "PIRQ table: %ld bytes.\n",
95                                 new_high_table_pointer - high_table_pointer);
96         }
97
98 #endif
99
100 #if CONFIG_GENERATE_MP_TABLE == 1
101 #define MAX_MP_TABLE_SIZE (4 * 1024)
102         post_code(0x9b);
103
104         /* The smp table must be in 0-1K, 639K-640K, or 960K-1M */
105         rom_table_end = write_smp_table(rom_table_end);
106         rom_table_end = ALIGN(rom_table_end, 1024);
107
108         high_table_pointer = (unsigned long)cbmem_add(CBMEM_ID_MPTABLE, MAX_MP_TABLE_SIZE);
109         if (high_table_pointer) {
110                 unsigned long new_high_table_pointer;
111                 new_high_table_pointer = write_smp_table(high_table_pointer);
112                 // FIXME make mp table code intelligent enough to know how
113                 // much space it's going to need.
114                 if (new_high_table_pointer > (high_table_pointer + MAX_MP_TABLE_SIZE)) {
115                         printk(BIOS_ERR, "ERROR: Increase MP table size.\n");
116                 }
117
118                 printk(BIOS_DEBUG, "MP table: %ld bytes.\n",
119                                 new_high_table_pointer - high_table_pointer);
120         }
121 #endif /* CONFIG_GENERATE_MP_TABLE */
122
123 #if CONFIG_GENERATE_ACPI_TABLES == 1
124 #define MAX_ACPI_SIZE (45 * 1024)
125         post_code(0x9c);
126
127         /* Write ACPI tables to F segment and high tables area */
128
129         /* Ok, this is a bit hacky still, because some day we want to have this
130          * completely dynamic. But right now we are setting fixed sizes.
131          * It's probably still better than the old high_table_base code because
132          * now at least we know when we have an overflow in the area.
133          *
134          * We want to use 1MB - 64K for Resume backup. We use 512B for TOC and
135          * 512 byte for GDT, 4K for PIRQ and 4K for MP table and 8KB for the
136          * coreboot table. This leaves us with 47KB for all of ACPI. Let's see
137          * how far we get.
138          */
139         high_table_pointer = (unsigned long)cbmem_add(CBMEM_ID_ACPI, MAX_ACPI_SIZE);
140         if (high_table_pointer) {
141                 unsigned long acpi_start = high_table_pointer;
142                 unsigned long new_high_table_pointer;
143
144                 rom_table_end = ALIGN(rom_table_end, 16);
145                 new_high_table_pointer = write_acpi_tables(high_table_pointer);
146                 if (new_high_table_pointer > ( high_table_pointer + MAX_ACPI_SIZE)) {
147                         printk(BIOS_ERR, "ERROR: Increase ACPI size\n");
148                 }
149                 printk(BIOS_DEBUG, "ACPI tables: %ld bytes.\n",
150                                 new_high_table_pointer - high_table_pointer);
151
152                 /* Now we need to create a low table copy of the RSDP. */
153
154                 /* First we look for the high table RSDP */
155                 while (acpi_start < new_high_table_pointer) {
156                         if (memcmp(((acpi_rsdp_t *)acpi_start)->signature, RSDP_SIG, 8) == 0) {
157                                 break;
158                         }
159                         acpi_start++;
160                 }
161
162                 /* Now, if we found the RSDP, we take the RSDT and XSDT pointer
163                  * from it in order to write the low RSDP
164                  */
165                 if (acpi_start < new_high_table_pointer) {
166                         acpi_rsdp_t *low_rsdp = (acpi_rsdp_t *)rom_table_end,
167                                     *high_rsdp = (acpi_rsdp_t *)acpi_start;
168
169                         acpi_write_rsdp(low_rsdp,
170                                 (acpi_rsdt_t *)(high_rsdp->rsdt_address),
171                                 (acpi_xsdt_t *)((unsigned long)high_rsdp->xsdt_address));
172                 } else {
173                         printk(BIOS_ERR, "ERROR: Didn't find RSDP in high table.\n");
174                 }
175                 rom_table_end = ALIGN(rom_table_end + sizeof(acpi_rsdp_t), 16);
176         } else {
177                 rom_table_end = write_acpi_tables(rom_table_end);
178                 rom_table_end = ALIGN(rom_table_end, 1024);
179         }
180
181 #endif
182 #define MAX_SMBIOS_SIZE 2048
183 #if CONFIG_GENERATE_SMBIOS_TABLES
184         high_table_pointer = (unsigned long)cbmem_add(CBMEM_ID_SMBIOS, MAX_SMBIOS_SIZE);
185         if (high_table_pointer) {
186                 unsigned long new_high_table_pointer;
187
188                 new_high_table_pointer = smbios_write_tables(high_table_pointer);
189                 rom_table_end = ALIGN(rom_table_end, 16);
190                 memcpy((void *)rom_table_end, (void *)high_table_pointer, sizeof(struct smbios_entry));
191                 rom_table_end += sizeof(struct smbios_entry);
192
193                 if (new_high_table_pointer > ( high_table_pointer + MAX_SMBIOS_SIZE)) {
194                         printk(BIOS_ERR, "ERROR: Increase SMBIOS size\n");
195                 }
196                 printk(BIOS_DEBUG, "SMBIOS tables: %ld bytes.\n",
197                                 new_high_table_pointer - high_table_pointer);
198         } else {
199                 unsigned long new_rom_table_end = smbios_write_tables(rom_table_end);
200                 printk(BIOS_DEBUG, "SMBIOS size %ld bytes\n", new_rom_table_end - rom_table_end);
201                 rom_table_end = ALIGN(new_rom_table_end, 16);
202         }
203 #endif
204
205 #define MAX_COREBOOT_TABLE_SIZE (32 * 1024)
206         post_code(0x9d);
207
208         high_table_pointer = (unsigned long)cbmem_add(CBMEM_ID_CBTABLE, MAX_COREBOOT_TABLE_SIZE);
209
210         if (high_table_pointer) {
211                 unsigned long new_high_table_pointer;
212
213                 /* Also put a forwarder entry into 0-4K */
214                 new_high_table_pointer = write_coreboot_table(low_table_start, low_table_end,
215                                 high_tables_base, high_table_pointer);
216
217                 if (new_high_table_pointer > (high_table_pointer +
218                                         MAX_COREBOOT_TABLE_SIZE))
219                         printk(BIOS_ERR, "%s: coreboot table didn't fit (%lx)\n",
220                                    __func__, new_high_table_pointer -
221                                    high_table_pointer);
222
223                 printk(BIOS_DEBUG, "coreboot table: %ld bytes.\n",
224                                 new_high_table_pointer - high_table_pointer);
225         } else {
226                 /* The coreboot table must be in 0-4K or 960K-1M */
227                 rom_table_end = write_coreboot_table(
228                                      low_table_start, low_table_end,
229                                      rom_table_start, rom_table_end);
230         }
231
232         post_code(0x9e);
233
234 #if CONFIG_HAVE_ACPI_RESUME
235         /* Let's prepare the ACPI S3 Resume area now already, so we can rely on
236          * it begin there during reboot time. We don't need the pointer, nor
237          * the result right now. If it fails, ACPI resume will be disabled.
238          */
239         cbmem_add(CBMEM_ID_RESUME, HIGH_MEMORY_SAVE);
240 #endif
241
242 #if CONFIG_MULTIBOOT
243         post_code(0x9d);
244
245         /* The Multiboot information structure */
246         write_multiboot_info(rom_table_end);
247 #endif
248
249         // Remove before sending upstream
250         cbmem_list();
251
252         return get_lb_mem();
253 }