blob: ac0e848c7d0591c1f639f570a4c9fc373ef78c88 [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>
Aaron Durbinbf396ff2013-02-11 21:50:35 -060037#include <romstage_handoff.h>
Aaron Durbina2671612013-02-06 21:41:01 -060038#if CONFIG_CHROMEOS
39#include <vendorcode/google/chromeos/chromeos.h>
40#endif
41#include "haswell.h"
42#include "northbridge/intel/haswell/haswell.h"
43#include "northbridge/intel/haswell/raminit.h"
44#include "southbridge/intel/lynxpoint/pch.h"
45#include "southbridge/intel/lynxpoint/me.h"
Aaron Durbin3d0071b2013-01-18 14:32:50 -060046
Aaron Durbina2671612013-02-06 21:41:01 -060047
Aaron Durbin38d94232013-02-07 00:03:33 -060048/* The cache-as-ram assembly file calls romstage_main() after setting up
49 * cache-as-ram. romstage_main() will then call the mainboards's
50 * mainboard_romstage_entry() function. That function then calls
51 * romstage_common() below. The reason for the back and forth is to provide
52 * common entry point from cache-as-ram while still allowing for code sharing.
53 * Because we can't use global variables the stack is used for allocations --
54 * thus the need to call back and forth. */
Aaron Durbin3d0071b2013-01-18 14:32:50 -060055
Aaron Durbin38d94232013-02-07 00:03:33 -060056
57static inline u32 *stack_push(u32 *stack, u32 value)
58{
59 stack = &stack[-1];
60 *stack = value;
61 return stack;
62}
63
Aaron Durbine2d9e5b2013-02-08 17:38:35 -060064static unsigned long choose_top_of_stack(void)
65{
66 unsigned long stack_top;
67#if CONFIG_RELOCATABLE_RAMSTAGE
68 stack_top = (unsigned long)cbmem_add(CBMEM_ID_RESUME_SCRATCH,
69 CONFIG_HIGH_SCRATCH_MEMORY_SIZE);
70 stack_top += CONFIG_HIGH_SCRATCH_MEMORY_SIZE;
71#else
72 stack_top = ROMSTAGE_STACK;
73#endif
74 return stack_top;
75}
76
Aaron Durbin38d94232013-02-07 00:03:33 -060077/* setup_romstage_stack_after_car() determines the stack to use after
78 * cache-as-ram is torn down as well as the MTRR settings to use. */
79static void *setup_romstage_stack_after_car(void)
80{
81 unsigned long top_of_stack;
82 int num_mtrrs;
83 u32 *slot;
84 u32 mtrr_mask_upper;
85
86 /* Top of stack needs to be aligned to a 4-byte boundary. */
Aaron Durbine2d9e5b2013-02-08 17:38:35 -060087 top_of_stack = choose_top_of_stack() & ~3;
Aaron Durbin38d94232013-02-07 00:03:33 -060088 slot = (void *)top_of_stack;
89 num_mtrrs = 0;
90
91 /* The upper bits of the MTRR mask need to set according to the number
92 * of physical address bits. */
93 mtrr_mask_upper = (1 << ((cpuid_eax(0x80000008) & 0xff) - 32)) - 1;
94
95 /* The order for each MTTR is value then base with upper 32-bits of
96 * each value coming before the lower 32-bits. The reasoning for
97 * this ordering is to create a stack layout like the following:
98 * +0: Number of MTRRs
99 * +4: MTTR base 0 31:0
100 * +8: MTTR base 0 63:32
101 * +12: MTTR mask 0 31:0
102 * +16: MTTR mask 0 63:32
103 * +20: MTTR base 1 31:0
104 * +24: MTTR base 1 63:32
105 * +28: MTTR mask 1 31:0
106 * +32: MTTR mask 1 63:32
107 */
108
109 /* Cache the ROM as WP just below 4GiB. */
110 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
111 slot = stack_push(slot, ~(CONFIG_ROM_SIZE - 1) | MTRRphysMaskValid);
112 slot = stack_push(slot, 0); /* upper base */
113 slot = stack_push(slot, ~(CONFIG_ROM_SIZE - 1) | MTRR_TYPE_WRPROT);
114 num_mtrrs++;
115
116 /* Cache RAM as WB from 0 -> CONFIG_RAMTOP. */
117 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
118 slot = stack_push(slot, ~(CONFIG_RAMTOP - 1) | MTRRphysMaskValid);
119 slot = stack_push(slot, 0); /* upper base */
120 slot = stack_push(slot, 0 | MTRR_TYPE_WRBACK);
121 num_mtrrs++;
122
123 /* Cache 8MiB below the top of ram. On haswell systems the top of
124 * ram under 4GiB is the start of the TSEG region. It is required to
125 * be 8MiB aligned. Set this area as cacheable so it can be used later
126 * for ramstage before setting up the entire RAM as cacheable. */
127 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
128 slot = stack_push(slot, ~((8 << 20) - 1) | MTRRphysMaskValid);
129 slot = stack_push(slot, 0); /* upper base */
130 slot = stack_push(slot,
131 (get_top_of_ram() - (8 << 20)) | MTRR_TYPE_WRBACK);
132 num_mtrrs++;
133
134 /* Save the number of MTTRs to setup. Return the stack location
135 * pointing to the number of MTRRs. */
136 slot = stack_push(slot, num_mtrrs);
137
138 return slot;
139}
140
141void * __attribute__((regparm(0))) romstage_main(unsigned long bist)
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600142{
143 int i;
Aaron Durbin38d94232013-02-07 00:03:33 -0600144 void *romstage_stack_after_car;
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600145 const int num_guards = 4;
146 const u32 stack_guard = 0xdeadbeef;
147 u32 *stack_base = (void *)(CONFIG_DCACHE_RAM_BASE +
148 CONFIG_DCACHE_RAM_SIZE -
149 CONFIG_DCACHE_RAM_ROMSTAGE_STACK_SIZE);
150
151 printk(BIOS_DEBUG, "Setting up stack guards.\n");
152 for (i = 0; i < num_guards; i++)
153 stack_base[i] = stack_guard;
154
Aaron Durbina2671612013-02-06 21:41:01 -0600155 mainboard_romstage_entry(bist);
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600156
157 /* Check the stack. */
158 for (i = 0; i < num_guards; i++) {
159 if (stack_base[i] == stack_guard)
160 continue;
161 printk(BIOS_DEBUG, "Smashed stack detected in romstage!\n");
162 }
163
Aaron Durbin38d94232013-02-07 00:03:33 -0600164 /* Get the stack to use after cache-as-ram is torn down. */
165 romstage_stack_after_car = setup_romstage_stack_after_car();
166
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600167#if CONFIG_CONSOLE_CBMEM
168 /* Keep this the last thing this function does. */
169 cbmemc_reinit();
170#endif
Aaron Durbin38d94232013-02-07 00:03:33 -0600171
172 return romstage_stack_after_car;
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600173}
Aaron Durbina2671612013-02-06 21:41:01 -0600174
175void romstage_common(const struct romstage_params *params)
176{
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600177 int boot_mode;
Aaron Durbina2671612013-02-06 21:41:01 -0600178 int wake_from_s3;
179 int cbmem_was_initted;
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600180 struct romstage_handoff *handoff;
Aaron Durbina2671612013-02-06 21:41:01 -0600181
182#if CONFIG_COLLECT_TIMESTAMPS
183 tsc_t start_romstage_time;
184 tsc_t before_dram_time;
185 tsc_t after_dram_time;
186 tsc_t base_time = {
187 .lo = pci_read_config32(PCI_DEV(0, 0x00, 0), 0xdc),
188 .hi = pci_read_config32(PCI_DEV(0, 0x1f, 2), 0xd0)
189 };
190#endif
191
192#if CONFIG_COLLECT_TIMESTAMPS
193 start_romstage_time = rdtsc();
194#endif
195
196 if (params->bist == 0)
197 enable_lapic();
198
199 wake_from_s3 = early_pch_init(params->gpio_map, params->rcba_config);
200
201 /* Halt if there was a built in self test failure */
202 report_bist_failure(params->bist);
203
204 /* Perform some early chipset initialization required
205 * before RAM initialization can work
206 */
207 haswell_early_initialization(HASWELL_MOBILE);
208 printk(BIOS_DEBUG, "Back from haswell_early_initialization()\n");
209
210 if (wake_from_s3) {
211#if CONFIG_HAVE_ACPI_RESUME
212 printk(BIOS_DEBUG, "Resume from S3 detected.\n");
Aaron Durbina2671612013-02-06 21:41:01 -0600213#else
214 printk(BIOS_DEBUG, "Resume from S3 detected, but disabled.\n");
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600215 wake_from_s3 = 0;
Aaron Durbina2671612013-02-06 21:41:01 -0600216#endif
217 }
218
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600219 /* There are hard coded assumptions of 2 meaning s3 wake. Normalize
220 * the users of the 2 literal here based off wake_from_s3. */
221 boot_mode = wake_from_s3 ? 2 : 0;
222
Aaron Durbina2671612013-02-06 21:41:01 -0600223 /* Prepare USB controller early in S3 resume */
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600224 if (wake_from_s3)
Aaron Durbina2671612013-02-06 21:41:01 -0600225 enable_usb_bar();
226
227 post_code(0x3a);
228 params->pei_data->boot_mode = boot_mode;
229#if CONFIG_COLLECT_TIMESTAMPS
230 before_dram_time = rdtsc();
231#endif
232
233 report_platform_info();
234
235 sdram_initialize(params->pei_data);
236
237#if CONFIG_COLLECT_TIMESTAMPS
238 after_dram_time = rdtsc();
239#endif
240 post_code(0x3b);
241
242 intel_early_me_status();
243
244 quick_ram_check();
245 post_code(0x3e);
246
247#if CONFIG_EARLY_CBMEM_INIT
248 cbmem_was_initted = !cbmem_initialize();
249#else
250 cbmem_was_initted = cbmem_reinit((uint64_t) (get_top_of_ram()
251 - HIGH_MEMORY_SIZE));
252#endif
253
Aaron Durbin2ad1dba2013-02-07 00:51:18 -0600254 /* Save data returned from MRC on non-S3 resumes. */
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600255 if (!wake_from_s3)
Aaron Durbin2ad1dba2013-02-07 00:51:18 -0600256 save_mrc_data(params->pei_data);
257
Aaron Durbina2671612013-02-06 21:41:01 -0600258#if CONFIG_HAVE_ACPI_RESUME
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600259 if (wake_from_s3 && !cbmem_was_initted) {
Aaron Durbina2671612013-02-06 21:41:01 -0600260 /* Failed S3 resume, reset to come up cleanly */
261 outb(0x6, 0xcf9);
262 while (1) {
263 hlt();
264 }
Aaron Durbina2671612013-02-06 21:41:01 -0600265 }
266#endif
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600267
268 handoff = romstage_handoff_find_or_add();
269 if (handoff != NULL)
270 handoff->s3_resume = wake_from_s3;
271 else
272 printk(BIOS_DEBUG, "Romstage handoff structure not added!\n");
273
Aaron Durbina2671612013-02-06 21:41:01 -0600274 post_code(0x3f);
275#if CONFIG_CHROMEOS
276 init_chromeos(boot_mode);
277#endif
278#if CONFIG_COLLECT_TIMESTAMPS
279 timestamp_init(base_time);
280 timestamp_add(TS_START_ROMSTAGE, start_romstage_time );
281 timestamp_add(TS_BEFORE_INITRAM, before_dram_time );
282 timestamp_add(TS_AFTER_INITRAM, after_dram_time );
283 timestamp_add_now(TS_END_ROMSTAGE);
284#endif
285}
Aaron Durbin7492ec12013-02-08 22:18:04 -0600286
287static inline void prepare_for_resume(void)
288{
Aaron Durbine2d9e5b2013-02-08 17:38:35 -0600289/* Only need to save memory when ramstage isn't relocatable. */
290#if !CONFIG_RELOCATABLE_RAMSTAGE
Aaron Durbin7492ec12013-02-08 22:18:04 -0600291#if CONFIG_HAVE_ACPI_RESUME
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600292 struct romstage_handoff *handoff = romstage_handoff_find_or_add();
293
Aaron Durbin7492ec12013-02-08 22:18:04 -0600294 /* Back up the OS-controlled memory where ramstage will be loaded. */
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600295 if (handoff != NULL && handoff->s3_resume) {
Aaron Durbin7492ec12013-02-08 22:18:04 -0600296 void *src = (void *)CONFIG_RAMBASE;
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600297 void *dest = cbmem_find(CBMEM_ID_RESUME);
298 if (dest != NULL)
299 memcpy(dest, src, HIGH_MEMORY_SAVE);
Aaron Durbin7492ec12013-02-08 22:18:04 -0600300 }
301#endif
Aaron Durbine2d9e5b2013-02-08 17:38:35 -0600302#endif
Aaron Durbin7492ec12013-02-08 22:18:04 -0600303}
304
305void romstage_after_car(void)
306{
307 prepare_for_resume();
308 /* Load the ramstage. */
309 copy_and_run(0);
310}