blob: d62377e81d4957896783b8ceab26879c8ba6d13e [file] [log] [blame]
Aaron Durbin3d0071b2013-01-18 14:32:50 -06001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2012 ChromeOS Authors
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#include <stdint.h>
Aaron Durbin7492ec12013-02-08 22:18:04 -060021#include <string.h>
Aaron Durbin3d0071b2013-01-18 14:32:50 -060022#include <cbmem.h>
23#include <console/console.h>
Aaron Durbina2671612013-02-06 21:41:01 -060024#include <arch/cpu.h>
25#include <cpu/x86/bist.h>
26#include <cpu/x86/msr.h>
Aaron Durbin38d94232013-02-07 00:03:33 -060027#include <cpu/x86/mtrr.h>
28#include <cpu/x86/stack.h>
Aaron Durbina2671612013-02-06 21:41:01 -060029#include <lib.h>
30#include <timestamp.h>
31#include <arch/io.h>
Aaron Durbin7492ec12013-02-08 22:18:04 -060032#include <arch/stages.h>
Aaron Durbina2671612013-02-06 21:41:01 -060033#include <arch/romcc_io.h>
34#include <device/pci_def.h>
35#include <cpu/x86/lapic.h>
36#include <cbmem.h>
37#if CONFIG_CHROMEOS
38#include <vendorcode/google/chromeos/chromeos.h>
39#endif
40#include "haswell.h"
41#include "northbridge/intel/haswell/haswell.h"
42#include "northbridge/intel/haswell/raminit.h"
43#include "southbridge/intel/lynxpoint/pch.h"
44#include "southbridge/intel/lynxpoint/me.h"
Aaron Durbin3d0071b2013-01-18 14:32:50 -060045
Aaron Durbina2671612013-02-06 21:41:01 -060046
Aaron Durbin38d94232013-02-07 00:03:33 -060047/* The cache-as-ram assembly file calls romstage_main() after setting up
48 * cache-as-ram. romstage_main() will then call the mainboards's
49 * mainboard_romstage_entry() function. That function then calls
50 * romstage_common() below. The reason for the back and forth is to provide
51 * common entry point from cache-as-ram while still allowing for code sharing.
52 * Because we can't use global variables the stack is used for allocations --
53 * thus the need to call back and forth. */
Aaron Durbin3d0071b2013-01-18 14:32:50 -060054
Aaron Durbin38d94232013-02-07 00:03:33 -060055
56static inline u32 *stack_push(u32 *stack, u32 value)
57{
58 stack = &stack[-1];
59 *stack = value;
60 return stack;
61}
62
63/* setup_romstage_stack_after_car() determines the stack to use after
64 * cache-as-ram is torn down as well as the MTRR settings to use. */
65static void *setup_romstage_stack_after_car(void)
66{
67 unsigned long top_of_stack;
68 int num_mtrrs;
69 u32 *slot;
70 u32 mtrr_mask_upper;
71
72 /* Top of stack needs to be aligned to a 4-byte boundary. */
73 top_of_stack = ROMSTAGE_STACK & ~3;
74 slot = (void *)top_of_stack;
75 num_mtrrs = 0;
76
77 /* The upper bits of the MTRR mask need to set according to the number
78 * of physical address bits. */
79 mtrr_mask_upper = (1 << ((cpuid_eax(0x80000008) & 0xff) - 32)) - 1;
80
81 /* The order for each MTTR is value then base with upper 32-bits of
82 * each value coming before the lower 32-bits. The reasoning for
83 * this ordering is to create a stack layout like the following:
84 * +0: Number of MTRRs
85 * +4: MTTR base 0 31:0
86 * +8: MTTR base 0 63:32
87 * +12: MTTR mask 0 31:0
88 * +16: MTTR mask 0 63:32
89 * +20: MTTR base 1 31:0
90 * +24: MTTR base 1 63:32
91 * +28: MTTR mask 1 31:0
92 * +32: MTTR mask 1 63:32
93 */
94
95 /* Cache the ROM as WP just below 4GiB. */
96 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
97 slot = stack_push(slot, ~(CONFIG_ROM_SIZE - 1) | MTRRphysMaskValid);
98 slot = stack_push(slot, 0); /* upper base */
99 slot = stack_push(slot, ~(CONFIG_ROM_SIZE - 1) | MTRR_TYPE_WRPROT);
100 num_mtrrs++;
101
102 /* Cache RAM as WB from 0 -> CONFIG_RAMTOP. */
103 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
104 slot = stack_push(slot, ~(CONFIG_RAMTOP - 1) | MTRRphysMaskValid);
105 slot = stack_push(slot, 0); /* upper base */
106 slot = stack_push(slot, 0 | MTRR_TYPE_WRBACK);
107 num_mtrrs++;
108
109 /* Cache 8MiB below the top of ram. On haswell systems the top of
110 * ram under 4GiB is the start of the TSEG region. It is required to
111 * be 8MiB aligned. Set this area as cacheable so it can be used later
112 * for ramstage before setting up the entire RAM as cacheable. */
113 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
114 slot = stack_push(slot, ~((8 << 20) - 1) | MTRRphysMaskValid);
115 slot = stack_push(slot, 0); /* upper base */
116 slot = stack_push(slot,
117 (get_top_of_ram() - (8 << 20)) | MTRR_TYPE_WRBACK);
118 num_mtrrs++;
119
120 /* Save the number of MTTRs to setup. Return the stack location
121 * pointing to the number of MTRRs. */
122 slot = stack_push(slot, num_mtrrs);
123
124 return slot;
125}
126
127void * __attribute__((regparm(0))) romstage_main(unsigned long bist)
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600128{
129 int i;
Aaron Durbin38d94232013-02-07 00:03:33 -0600130 void *romstage_stack_after_car;
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600131 const int num_guards = 4;
132 const u32 stack_guard = 0xdeadbeef;
133 u32 *stack_base = (void *)(CONFIG_DCACHE_RAM_BASE +
134 CONFIG_DCACHE_RAM_SIZE -
135 CONFIG_DCACHE_RAM_ROMSTAGE_STACK_SIZE);
136
137 printk(BIOS_DEBUG, "Setting up stack guards.\n");
138 for (i = 0; i < num_guards; i++)
139 stack_base[i] = stack_guard;
140
Aaron Durbina2671612013-02-06 21:41:01 -0600141 mainboard_romstage_entry(bist);
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600142
143 /* Check the stack. */
144 for (i = 0; i < num_guards; i++) {
145 if (stack_base[i] == stack_guard)
146 continue;
147 printk(BIOS_DEBUG, "Smashed stack detected in romstage!\n");
148 }
149
Aaron Durbin38d94232013-02-07 00:03:33 -0600150 /* Get the stack to use after cache-as-ram is torn down. */
151 romstage_stack_after_car = setup_romstage_stack_after_car();
152
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600153#if CONFIG_CONSOLE_CBMEM
154 /* Keep this the last thing this function does. */
155 cbmemc_reinit();
156#endif
Aaron Durbin38d94232013-02-07 00:03:33 -0600157
158 return romstage_stack_after_car;
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600159}
Aaron Durbina2671612013-02-06 21:41:01 -0600160
161void romstage_common(const struct romstage_params *params)
162{
163 int boot_mode = 0;
164 int wake_from_s3;
165 int cbmem_was_initted;
166
167#if CONFIG_COLLECT_TIMESTAMPS
168 tsc_t start_romstage_time;
169 tsc_t before_dram_time;
170 tsc_t after_dram_time;
171 tsc_t base_time = {
172 .lo = pci_read_config32(PCI_DEV(0, 0x00, 0), 0xdc),
173 .hi = pci_read_config32(PCI_DEV(0, 0x1f, 2), 0xd0)
174 };
175#endif
176
177#if CONFIG_COLLECT_TIMESTAMPS
178 start_romstage_time = rdtsc();
179#endif
180
181 if (params->bist == 0)
182 enable_lapic();
183
184 wake_from_s3 = early_pch_init(params->gpio_map, params->rcba_config);
185
186 /* Halt if there was a built in self test failure */
187 report_bist_failure(params->bist);
188
189 /* Perform some early chipset initialization required
190 * before RAM initialization can work
191 */
192 haswell_early_initialization(HASWELL_MOBILE);
193 printk(BIOS_DEBUG, "Back from haswell_early_initialization()\n");
194
195 if (wake_from_s3) {
196#if CONFIG_HAVE_ACPI_RESUME
197 printk(BIOS_DEBUG, "Resume from S3 detected.\n");
198 boot_mode = 2;
199#else
200 printk(BIOS_DEBUG, "Resume from S3 detected, but disabled.\n");
201#endif
202 }
203
204 /* Prepare USB controller early in S3 resume */
205 if (boot_mode == 2)
206 enable_usb_bar();
207
208 post_code(0x3a);
209 params->pei_data->boot_mode = boot_mode;
210#if CONFIG_COLLECT_TIMESTAMPS
211 before_dram_time = rdtsc();
212#endif
213
214 report_platform_info();
215
216 sdram_initialize(params->pei_data);
217
218#if CONFIG_COLLECT_TIMESTAMPS
219 after_dram_time = rdtsc();
220#endif
221 post_code(0x3b);
222
223 intel_early_me_status();
224
225 quick_ram_check();
226 post_code(0x3e);
227
228#if CONFIG_EARLY_CBMEM_INIT
229 cbmem_was_initted = !cbmem_initialize();
230#else
231 cbmem_was_initted = cbmem_reinit((uint64_t) (get_top_of_ram()
232 - HIGH_MEMORY_SIZE));
233#endif
234
Aaron Durbin2ad1dba2013-02-07 00:51:18 -0600235 /* Save data returned from MRC on non-S3 resumes. */
236 if (boot_mode != 2)
237 save_mrc_data(params->pei_data);
238
Aaron Durbina2671612013-02-06 21:41:01 -0600239#if CONFIG_HAVE_ACPI_RESUME
240 /* If there is no high memory area, we didn't boot before, so
241 * this is not a resume. In that case we just create the cbmem toc.
242 */
243
244 *(u32 *)CBMEM_BOOT_MODE = 0;
245 *(u32 *)CBMEM_RESUME_BACKUP = 0;
246
247 if ((boot_mode == 2) && cbmem_was_initted) {
248 void *resume_backup_memory = cbmem_find(CBMEM_ID_RESUME);
249 if (resume_backup_memory) {
250 *(u32 *)CBMEM_BOOT_MODE = boot_mode;
251 *(u32 *)CBMEM_RESUME_BACKUP = (u32)resume_backup_memory;
252 }
253 /* Magic for S3 resume */
254 pci_write_config32(PCI_DEV(0, 0x00, 0), SKPAD, 0xcafed00d);
255 } else if (boot_mode == 2) {
256 /* Failed S3 resume, reset to come up cleanly */
257 outb(0x6, 0xcf9);
258 while (1) {
259 hlt();
260 }
261 } else {
262 pci_write_config32(PCI_DEV(0, 0x00, 0), SKPAD, 0xcafebabe);
263 }
264#endif
265 post_code(0x3f);
266#if CONFIG_CHROMEOS
267 init_chromeos(boot_mode);
268#endif
269#if CONFIG_COLLECT_TIMESTAMPS
270 timestamp_init(base_time);
271 timestamp_add(TS_START_ROMSTAGE, start_romstage_time );
272 timestamp_add(TS_BEFORE_INITRAM, before_dram_time );
273 timestamp_add(TS_AFTER_INITRAM, after_dram_time );
274 timestamp_add_now(TS_END_ROMSTAGE);
275#endif
276}
Aaron Durbin7492ec12013-02-08 22:18:04 -0600277
278static inline void prepare_for_resume(void)
279{
280#if CONFIG_HAVE_ACPI_RESUME
281 /* Back up the OS-controlled memory where ramstage will be loaded. */
282 if (*(u32 *)CBMEM_BOOT_MODE == 2) {
283 void *src = (void *)CONFIG_RAMBASE;
284 void *dest = *(void **)CBMEM_RESUME_BACKUP;
285 memcpy(dest, src, HIGH_MEMORY_SAVE);
286 }
287#endif
288}
289
290void romstage_after_car(void)
291{
292 prepare_for_resume();
293 /* Load the ramstage. */
294 copy_and_run(0);
295}