This fixes a couple of issues with older Linux kernels (that expect an XSDT as
[coreboot.git] / src / arch / i386 / 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 /* 2006.1 yhlu add mptable cross 0x467 processing */
23
24 #include <console/console.h>
25 #include <cpu/cpu.h>
26 #include <boot/tables.h>
27 #include <boot/coreboot_tables.h>
28 #include <arch/pirq_routing.h>
29 #include <arch/smp/mpspec.h>
30 #include <arch/acpi.h>
31 #include <string.h>
32 #include <cpu/x86/multiboot.h>
33 #include "coreboot_table.h"
34
35 // Global Descriptor Table, defined in c_start.S
36 extern uint8_t gdt;
37 extern uint8_t gdt_end;
38
39 /* i386 lgdt argument */
40 struct gdtarg {
41         unsigned short limit;
42         unsigned int base;
43 } __attribute__((packed));
44
45 // Copy GDT to new location and reload it
46 void move_gdt(unsigned long newgdt)
47 {
48         uint16_t num_gdt_bytes = &gdt_end - &gdt;
49         struct gdtarg gdtarg;
50
51         printk_debug("Moving GDT to %#lx...", newgdt);
52         memcpy((void*)newgdt, &gdt, num_gdt_bytes);
53         gdtarg.base = newgdt;
54         gdtarg.limit = num_gdt_bytes - 1;
55         __asm__ __volatile__ ("lgdt %0\n\t" : : "m" (gdtarg));
56         printk_debug("ok\n");
57 }
58
59 uint64_t high_tables_base = 0;
60 uint64_t high_tables_size;
61
62 struct lb_memory *write_tables(void)
63 {
64         unsigned long low_table_start, low_table_end;
65         unsigned long rom_table_start, rom_table_end;
66
67         /* Even if high tables are configured, some tables are copied both to
68          * the low and the high area, so payloads and OSes don't need to know
69          * about the high tables.
70          */
71         unsigned long high_table_end=0;
72
73         if (high_tables_base) {
74                 printk_debug("High Tables Base is %llx.\n", high_tables_base);
75                 high_table_end = high_tables_base;
76         } else {
77                 printk_err("ERROR: High Tables Base is not set.\n");
78         }
79
80         rom_table_start = 0xf0000; 
81         rom_table_end =   0xf0000;
82
83         /* Start low addr at 0x500, so we don't run into conflicts with the BDA
84          * in case our data structures grow beyound 0x400. Only multiboot, GDT
85          * and the coreboot table use low_tables. 
86          */
87         low_table_start = 0;
88         low_table_end = 0x500;
89
90         post_code(0x99);
91
92         /* This table must be between 0x0f0000 and 0x100000 */
93         rom_table_end = write_pirq_routing_table(rom_table_end);
94         rom_table_end = ALIGN(rom_table_end, 1024);
95
96         /* And add a high table version for those payloads that
97          * want to live in the F segment
98          */
99         if (high_tables_base) {
100                 high_table_end = write_pirq_routing_table(high_table_end);
101                 high_table_end = ALIGN(high_table_end, 1024);
102         }
103
104         post_code(0x9a);
105
106         /* Write ACPI tables to F segment and high tables area */
107 #if CONFIG_HAVE_ACPI_TABLES == 1
108         if (high_tables_base) {
109                 unsigned long acpi_start = high_table_end;
110                 rom_table_end = ALIGN(rom_table_end, 16);
111                 high_table_end = write_acpi_tables(high_table_end);
112                 while (acpi_start < high_table_end) {
113                         if (memcmp(((acpi_rsdp_t *)acpi_start)->signature, RSDP_SIG, 8) == 0) {
114                                 break;
115                         }
116                         acpi_start++;
117                 }
118                 if (acpi_start != high_table_end) {
119                         acpi_rsdp_t *low_rsdp = (acpi_rsdp_t *)rom_table_end,
120                                     *high_rsdp = (acpi_rsdp_t *)acpi_start;
121
122                         acpi_write_rsdp(low_rsdp,
123                                 (acpi_rsdt_t *)(high_rsdp->rsdt_address),
124                                 (acpi_xsdt_t *)(high_rsdp->xsdt_address));
125                 } else {
126                         printk_err("ERROR: Didn't find RSDP in high table.\n");
127                 }
128                 high_table_end = ALIGN(high_table_end, 1024);
129                 rom_table_end = ALIGN(rom_table_end + sizeof(acpi_rsdp_t), 16);
130         } else {
131                 rom_table_end = write_acpi_tables(rom_table_end);
132                 rom_table_end = ALIGN(rom_table_end, 1024);
133         }
134 #endif
135         post_code(0x9b);
136
137 #if CONFIG_HAVE_MP_TABLE == 1
138         /* The smp table must be in 0-1K, 639K-640K, or 960K-1M */
139         rom_table_end = write_smp_table(rom_table_end);
140         rom_table_end = ALIGN(rom_table_end, 1024);
141
142         /* ... and a copy in the high tables */
143         if (high_tables_base) {
144                 high_table_end = write_smp_table(high_table_end);
145                 high_table_end = ALIGN(high_table_end, 1024);
146         }
147 #endif /* CONFIG_HAVE_MP_TABLE */
148
149         post_code(0x9c);
150
151         // Relocate the GDT to reserved memory, so it won't get clobbered
152         if (high_tables_base) {
153                 move_gdt(high_table_end);
154                 high_table_end += &gdt_end - &gdt;
155                 high_table_end = ALIGN(high_table_end, 1024);
156         } else {
157                 move_gdt(low_table_end);
158                 low_table_end += &gdt_end - &gdt;
159         }
160
161         post_code(0x9d);
162
163 #if CONFIG_MULTIBOOT
164         /* The Multiboot information structure */
165         rom_table_end = write_multiboot_info(
166                                 low_table_start, low_table_end,
167                                 rom_table_start, rom_table_end);
168 #endif
169
170         post_code(0x9e);
171
172         if (high_tables_base) {
173                 /* Also put a forwarder entry into 0-4K */
174                 write_coreboot_table(low_table_start, low_table_end,
175                                 high_tables_base, high_table_end);
176                 if (high_table_end > high_tables_base + high_tables_size)
177                         printk_err("%s: High tables didn't fit in %llx (%llx)\n",
178                                    __func__, high_tables_size, high_table_end -
179                                    high_tables_base);
180         } else {
181                 /* The coreboot table must be in 0-4K or 960K-1M */
182                 write_coreboot_table(low_table_start, low_table_end,
183                               rom_table_start, rom_table_end);
184         }
185  
186         post_code(0x9f);
187
188         return get_lb_mem();
189 }