blob: 60a1c3a155997a40e6dd34b4a3bf3bc2b3e46f1d [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 <device/pci_def.h>
34#include <cpu/x86/lapic.h>
Aaron Durbinf7cdfe52013-02-16 00:05:52 -060035#include <cbfs.h>
Aaron Durbin75e29742013-10-10 20:37:04 -050036#include <ramstage_cache.h>
Aaron Durbinbf396ff2013-02-11 21:50:35 -060037#include <romstage_handoff.h>
Aaron Durbinb86113f2013-02-19 08:59:16 -060038#include <reset.h>
Aaron Durbina2671612013-02-06 21:41:01 -060039#if CONFIG_CHROMEOS
40#include <vendorcode/google/chromeos/chromeos.h>
41#endif
Duncan Laurie7cced0d2013-06-04 10:03:34 -070042#if CONFIG_EC_GOOGLE_CHROMEEC
43#include <ec/google/chromeec/ec.h>
44#endif
Aaron Durbina2671612013-02-06 21:41:01 -060045#include "haswell.h"
46#include "northbridge/intel/haswell/haswell.h"
47#include "northbridge/intel/haswell/raminit.h"
48#include "southbridge/intel/lynxpoint/pch.h"
49#include "southbridge/intel/lynxpoint/me.h"
Aaron Durbin3d0071b2013-01-18 14:32:50 -060050
Aaron Durbina2671612013-02-06 21:41:01 -060051
Aaron Durbinb86113f2013-02-19 08:59:16 -060052static inline void reset_system(void)
53{
54 hard_reset();
55 while (1) {
56 hlt();
57 }
58}
59
Aaron Durbin38d94232013-02-07 00:03:33 -060060/* The cache-as-ram assembly file calls romstage_main() after setting up
61 * cache-as-ram. romstage_main() will then call the mainboards's
62 * mainboard_romstage_entry() function. That function then calls
63 * romstage_common() below. The reason for the back and forth is to provide
64 * common entry point from cache-as-ram while still allowing for code sharing.
65 * Because we can't use global variables the stack is used for allocations --
66 * thus the need to call back and forth. */
Aaron Durbin3d0071b2013-01-18 14:32:50 -060067
Aaron Durbin38d94232013-02-07 00:03:33 -060068
69static inline u32 *stack_push(u32 *stack, u32 value)
70{
71 stack = &stack[-1];
72 *stack = value;
73 return stack;
74}
75
Aaron Durbinc0cbd6e2013-03-13 13:51:20 -050076/* Romstage needs quite a bit of stack for decompressing images since the lzma
77 * lib keeps its state on the stack during romstage. */
78#define ROMSTAGE_RAM_STACK_SIZE 0x5000
Aaron Durbine2d9e5b2013-02-08 17:38:35 -060079static unsigned long choose_top_of_stack(void)
80{
81 unsigned long stack_top;
Aaron Durbinc0cbd6e2013-03-13 13:51:20 -050082#if CONFIG_DYNAMIC_CBMEM
83 /* cbmem_add() does a find() before add(). */
84 stack_top = (unsigned long)cbmem_add(CBMEM_ID_ROMSTAGE_RAM_STACK,
85 ROMSTAGE_RAM_STACK_SIZE);
86 stack_top += ROMSTAGE_RAM_STACK_SIZE;
Aaron Durbine2d9e5b2013-02-08 17:38:35 -060087#else
88 stack_top = ROMSTAGE_STACK;
89#endif
90 return stack_top;
91}
92
Aaron Durbin38d94232013-02-07 00:03:33 -060093/* setup_romstage_stack_after_car() determines the stack to use after
94 * cache-as-ram is torn down as well as the MTRR settings to use. */
95static void *setup_romstage_stack_after_car(void)
96{
97 unsigned long top_of_stack;
98 int num_mtrrs;
99 u32 *slot;
100 u32 mtrr_mask_upper;
Aaron Durbin67481ddc2013-02-15 15:08:37 -0600101 u32 top_of_ram;
Aaron Durbin38d94232013-02-07 00:03:33 -0600102
103 /* Top of stack needs to be aligned to a 4-byte boundary. */
Aaron Durbine2d9e5b2013-02-08 17:38:35 -0600104 top_of_stack = choose_top_of_stack() & ~3;
Aaron Durbin38d94232013-02-07 00:03:33 -0600105 slot = (void *)top_of_stack;
106 num_mtrrs = 0;
107
108 /* The upper bits of the MTRR mask need to set according to the number
109 * of physical address bits. */
110 mtrr_mask_upper = (1 << ((cpuid_eax(0x80000008) & 0xff) - 32)) - 1;
111
Paul Menzel4fe98132014-01-25 15:55:28 +0100112 /* The order for each MTRR is value then base with upper 32-bits of
Aaron Durbin38d94232013-02-07 00:03:33 -0600113 * each value coming before the lower 32-bits. The reasoning for
114 * this ordering is to create a stack layout like the following:
115 * +0: Number of MTRRs
Paul Menzel4fe98132014-01-25 15:55:28 +0100116 * +4: MTRR base 0 31:0
117 * +8: MTRR base 0 63:32
118 * +12: MTRR mask 0 31:0
119 * +16: MTRR mask 0 63:32
120 * +20: MTRR base 1 31:0
121 * +24: MTRR base 1 63:32
122 * +28: MTRR mask 1 31:0
123 * +32: MTRR mask 1 63:32
Aaron Durbin38d94232013-02-07 00:03:33 -0600124 */
125
126 /* Cache the ROM as WP just below 4GiB. */
127 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
Kyösti Mälkki107f72e2014-01-06 11:06:26 +0200128 slot = stack_push(slot, ~(CACHE_ROM_SIZE - 1) | MTRRphysMaskValid);
Aaron Durbin38d94232013-02-07 00:03:33 -0600129 slot = stack_push(slot, 0); /* upper base */
Kyösti Mälkki107f72e2014-01-06 11:06:26 +0200130 slot = stack_push(slot, ~(CACHE_ROM_SIZE - 1) | MTRR_TYPE_WRPROT);
Aaron Durbin38d94232013-02-07 00:03:33 -0600131 num_mtrrs++;
132
133 /* Cache RAM as WB from 0 -> CONFIG_RAMTOP. */
134 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
135 slot = stack_push(slot, ~(CONFIG_RAMTOP - 1) | MTRRphysMaskValid);
136 slot = stack_push(slot, 0); /* upper base */
137 slot = stack_push(slot, 0 | MTRR_TYPE_WRBACK);
138 num_mtrrs++;
139
Aaron Durbin67481ddc2013-02-15 15:08:37 -0600140 top_of_ram = get_top_of_ram();
Aaron Durbin38d94232013-02-07 00:03:33 -0600141 /* Cache 8MiB below the top of ram. On haswell systems the top of
142 * ram under 4GiB is the start of the TSEG region. It is required to
143 * be 8MiB aligned. Set this area as cacheable so it can be used later
144 * for ramstage before setting up the entire RAM as cacheable. */
145 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
146 slot = stack_push(slot, ~((8 << 20) - 1) | MTRRphysMaskValid);
147 slot = stack_push(slot, 0); /* upper base */
Aaron Durbin67481ddc2013-02-15 15:08:37 -0600148 slot = stack_push(slot, (top_of_ram - (8 << 20)) | MTRR_TYPE_WRBACK);
149 num_mtrrs++;
150
151 /* Cache 8MiB at the top of ram. Top of ram on haswell systems
152 * is where the TSEG region resides. However, it is not restricted
153 * to SMM mode until SMM has been relocated. By setting the region
154 * to cacheable it provides faster access when relocating the SMM
155 * handler as well as using the TSEG region for other purposes. */
156 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
157 slot = stack_push(slot, ~((8 << 20) - 1) | MTRRphysMaskValid);
158 slot = stack_push(slot, 0); /* upper base */
159 slot = stack_push(slot, top_of_ram | MTRR_TYPE_WRBACK);
Aaron Durbin38d94232013-02-07 00:03:33 -0600160 num_mtrrs++;
161
Paul Menzel4fe98132014-01-25 15:55:28 +0100162 /* Save the number of MTRRs to setup. Return the stack location
Aaron Durbin38d94232013-02-07 00:03:33 -0600163 * pointing to the number of MTRRs. */
164 slot = stack_push(slot, num_mtrrs);
165
166 return slot;
167}
168
Aaron Durbin39ecc652013-05-02 09:42:13 -0500169void * asmlinkage romstage_main(unsigned long bist)
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600170{
171 int i;
Aaron Durbin38d94232013-02-07 00:03:33 -0600172 void *romstage_stack_after_car;
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600173 const int num_guards = 4;
174 const u32 stack_guard = 0xdeadbeef;
175 u32 *stack_base = (void *)(CONFIG_DCACHE_RAM_BASE +
176 CONFIG_DCACHE_RAM_SIZE -
177 CONFIG_DCACHE_RAM_ROMSTAGE_STACK_SIZE);
178
179 printk(BIOS_DEBUG, "Setting up stack guards.\n");
180 for (i = 0; i < num_guards; i++)
181 stack_base[i] = stack_guard;
182
Aaron Durbina2671612013-02-06 21:41:01 -0600183 mainboard_romstage_entry(bist);
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600184
185 /* Check the stack. */
186 for (i = 0; i < num_guards; i++) {
187 if (stack_base[i] == stack_guard)
188 continue;
189 printk(BIOS_DEBUG, "Smashed stack detected in romstage!\n");
190 }
191
Aaron Durbin38d94232013-02-07 00:03:33 -0600192 /* Get the stack to use after cache-as-ram is torn down. */
193 romstage_stack_after_car = setup_romstage_stack_after_car();
194
Aaron Durbin38d94232013-02-07 00:03:33 -0600195 return romstage_stack_after_car;
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600196}
Aaron Durbina2671612013-02-06 21:41:01 -0600197
198void romstage_common(const struct romstage_params *params)
199{
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600200 int boot_mode;
Aaron Durbina2671612013-02-06 21:41:01 -0600201 int wake_from_s3;
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600202 struct romstage_handoff *handoff;
Aaron Durbina2671612013-02-06 21:41:01 -0600203
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300204 timestamp_init(get_initial_timestamp());
205 timestamp_add_now(TS_START_ROMSTAGE);
Aaron Durbina2671612013-02-06 21:41:01 -0600206
207 if (params->bist == 0)
208 enable_lapic();
209
210 wake_from_s3 = early_pch_init(params->gpio_map, params->rcba_config);
211
Duncan Laurie7cced0d2013-06-04 10:03:34 -0700212#if CONFIG_EC_GOOGLE_CHROMEEC
213 /* Ensure the EC is in the right mode for recovery */
214 google_chromeec_early_init();
215#endif
216
Aaron Durbina2671612013-02-06 21:41:01 -0600217 /* Halt if there was a built in self test failure */
218 report_bist_failure(params->bist);
219
220 /* Perform some early chipset initialization required
221 * before RAM initialization can work
222 */
223 haswell_early_initialization(HASWELL_MOBILE);
224 printk(BIOS_DEBUG, "Back from haswell_early_initialization()\n");
225
226 if (wake_from_s3) {
227#if CONFIG_HAVE_ACPI_RESUME
228 printk(BIOS_DEBUG, "Resume from S3 detected.\n");
Aaron Durbina2671612013-02-06 21:41:01 -0600229#else
230 printk(BIOS_DEBUG, "Resume from S3 detected, but disabled.\n");
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600231 wake_from_s3 = 0;
Aaron Durbina2671612013-02-06 21:41:01 -0600232#endif
233 }
234
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600235 /* There are hard coded assumptions of 2 meaning s3 wake. Normalize
236 * the users of the 2 literal here based off wake_from_s3. */
237 boot_mode = wake_from_s3 ? 2 : 0;
238
Aaron Durbina2671612013-02-06 21:41:01 -0600239 /* Prepare USB controller early in S3 resume */
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600240 if (wake_from_s3)
Aaron Durbina2671612013-02-06 21:41:01 -0600241 enable_usb_bar();
242
243 post_code(0x3a);
244 params->pei_data->boot_mode = boot_mode;
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300245
246 timestamp_add_now(TS_BEFORE_INITRAM);
Aaron Durbina2671612013-02-06 21:41:01 -0600247
248 report_platform_info();
249
Aaron Durbinc7633f42013-06-13 17:29:36 -0700250 if (params->copy_spd != NULL)
251 params->copy_spd(params->pei_data);
252
Aaron Durbina2671612013-02-06 21:41:01 -0600253 sdram_initialize(params->pei_data);
254
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300255 timestamp_add_now(TS_AFTER_INITRAM);
256
Aaron Durbina2671612013-02-06 21:41:01 -0600257 post_code(0x3b);
258
259 intel_early_me_status();
260
261 quick_ram_check();
262 post_code(0x3e);
263
Aaron Durbinc0cbd6e2013-03-13 13:51:20 -0500264 if (!wake_from_s3) {
265 cbmem_initialize_empty();
266 /* Save data returned from MRC on non-S3 resumes. */
Aaron Durbin2ad1dba2013-02-07 00:51:18 -0600267 save_mrc_data(params->pei_data);
Aaron Durbinc0cbd6e2013-03-13 13:51:20 -0500268 } else if (cbmem_initialize()) {
269 #if CONFIG_HAVE_ACPI_RESUME
Aaron Durbina2671612013-02-06 21:41:01 -0600270 /* Failed S3 resume, reset to come up cleanly */
Aaron Durbinb86113f2013-02-19 08:59:16 -0600271 reset_system();
Aaron Durbinc0cbd6e2013-03-13 13:51:20 -0500272 #endif
Aaron Durbina2671612013-02-06 21:41:01 -0600273 }
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600274
275 handoff = romstage_handoff_find_or_add();
276 if (handoff != NULL)
277 handoff->s3_resume = wake_from_s3;
278 else
279 printk(BIOS_DEBUG, "Romstage handoff structure not added!\n");
280
Aaron Durbina2671612013-02-06 21:41:01 -0600281 post_code(0x3f);
282#if CONFIG_CHROMEOS
283 init_chromeos(boot_mode);
284#endif
Aaron Durbina2671612013-02-06 21:41:01 -0600285 timestamp_add_now(TS_END_ROMSTAGE);
Aaron Durbina2671612013-02-06 21:41:01 -0600286}
Aaron Durbin7492ec12013-02-08 22:18:04 -0600287
Aaron Durbind02bb622013-03-01 17:40:49 -0600288static inline void prepare_for_resume(struct romstage_handoff *handoff)
Aaron Durbin7492ec12013-02-08 22:18:04 -0600289{
Aaron Durbine2d9e5b2013-02-08 17:38:35 -0600290/* Only need to save memory when ramstage isn't relocatable. */
291#if !CONFIG_RELOCATABLE_RAMSTAGE
Aaron Durbin7492ec12013-02-08 22:18:04 -0600292#if CONFIG_HAVE_ACPI_RESUME
293 /* Back up the OS-controlled memory where ramstage will be loaded. */
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600294 if (handoff != NULL && handoff->s3_resume) {
Aaron Durbin7492ec12013-02-08 22:18:04 -0600295 void *src = (void *)CONFIG_RAMBASE;
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600296 void *dest = cbmem_find(CBMEM_ID_RESUME);
297 if (dest != NULL)
298 memcpy(dest, src, HIGH_MEMORY_SAVE);
Aaron Durbin7492ec12013-02-08 22:18:04 -0600299 }
300#endif
Aaron Durbine2d9e5b2013-02-08 17:38:35 -0600301#endif
Aaron Durbin7492ec12013-02-08 22:18:04 -0600302}
303
304void romstage_after_car(void)
305{
Aaron Durbind02bb622013-03-01 17:40:49 -0600306 struct romstage_handoff *handoff;
307
308 handoff = romstage_handoff_find_or_add();
309
310 prepare_for_resume(handoff);
311
Aaron Durbind02bb622013-03-01 17:40:49 -0600312 vboot_verify_firmware(handoff);
Aaron Durbind02bb622013-03-01 17:40:49 -0600313
Aaron Durbin7492ec12013-02-08 22:18:04 -0600314 /* Load the ramstage. */
Stefan Reinauer648d1662013-05-06 18:05:39 -0700315 copy_and_run();
Aaron Durbin7492ec12013-02-08 22:18:04 -0600316}
Aaron Durbinf7cdfe52013-02-16 00:05:52 -0600317
318
319#if CONFIG_RELOCATABLE_RAMSTAGE
Aaron Durbin75e29742013-10-10 20:37:04 -0500320#include <ramstage_cache.h>
321
322struct ramstage_cache *ramstage_cache_location(long *size)
Aaron Durbinf7cdfe52013-02-16 00:05:52 -0600323{
Aaron Durbinf7cdfe52013-02-16 00:05:52 -0600324 /* The ramstage cache lives in the TSEG region at RESERVED_SMM_OFFSET.
325 * The top of ram is defined to be the TSEG base address. */
Aaron Durbin75e29742013-10-10 20:37:04 -0500326 *size = RESERVED_SMM_SIZE;
327 return (void *)(get_top_of_ram() + RESERVED_SMM_OFFSET);
Aaron Durbinf7cdfe52013-02-16 00:05:52 -0600328}
329
Aaron Durbin75e29742013-10-10 20:37:04 -0500330void ramstage_cache_invalid(struct ramstage_cache *cache)
Aaron Durbinf7cdfe52013-02-16 00:05:52 -0600331{
Aaron Durbin75e29742013-10-10 20:37:04 -0500332#if CONFIG_RESET_ON_INVALID_RAMSTAGE_CACHE
333 reset_system();
334#endif
Aaron Durbinf7cdfe52013-02-16 00:05:52 -0600335}
336#endif