blob: e62b894bb9226d889b99a8819135f73ab6238c25 [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 Durbinbf396ff2013-02-11 21:50:35 -060036#include <romstage_handoff.h>
Aaron Durbinb86113f2013-02-19 08:59:16 -060037#include <reset.h>
Aaron Durbina2671612013-02-06 21:41:01 -060038#if CONFIG_CHROMEOS
39#include <vendorcode/google/chromeos/chromeos.h>
40#endif
Duncan Laurie7cced0d2013-06-04 10:03:34 -070041#if CONFIG_EC_GOOGLE_CHROMEEC
42#include <ec/google/chromeec/ec.h>
43#endif
Aaron Durbina2671612013-02-06 21:41:01 -060044#include "haswell.h"
45#include "northbridge/intel/haswell/haswell.h"
46#include "northbridge/intel/haswell/raminit.h"
47#include "southbridge/intel/lynxpoint/pch.h"
48#include "southbridge/intel/lynxpoint/me.h"
Aaron Durbin3d0071b2013-01-18 14:32:50 -060049
Aaron Durbina2671612013-02-06 21:41:01 -060050
Aaron Durbinb86113f2013-02-19 08:59:16 -060051static inline void reset_system(void)
52{
53 hard_reset();
54 while (1) {
55 hlt();
56 }
57}
58
Aaron Durbin38d94232013-02-07 00:03:33 -060059/* The cache-as-ram assembly file calls romstage_main() after setting up
60 * cache-as-ram. romstage_main() will then call the mainboards's
61 * mainboard_romstage_entry() function. That function then calls
62 * romstage_common() below. The reason for the back and forth is to provide
63 * common entry point from cache-as-ram while still allowing for code sharing.
64 * Because we can't use global variables the stack is used for allocations --
65 * thus the need to call back and forth. */
Aaron Durbin3d0071b2013-01-18 14:32:50 -060066
Aaron Durbin38d94232013-02-07 00:03:33 -060067
68static inline u32 *stack_push(u32 *stack, u32 value)
69{
70 stack = &stack[-1];
71 *stack = value;
72 return stack;
73}
74
Aaron Durbinc0cbd6e2013-03-13 13:51:20 -050075/* Romstage needs quite a bit of stack for decompressing images since the lzma
76 * lib keeps its state on the stack during romstage. */
77#define ROMSTAGE_RAM_STACK_SIZE 0x5000
Aaron Durbine2d9e5b2013-02-08 17:38:35 -060078static unsigned long choose_top_of_stack(void)
79{
80 unsigned long stack_top;
Aaron Durbinc0cbd6e2013-03-13 13:51:20 -050081#if CONFIG_DYNAMIC_CBMEM
82 /* cbmem_add() does a find() before add(). */
83 stack_top = (unsigned long)cbmem_add(CBMEM_ID_ROMSTAGE_RAM_STACK,
84 ROMSTAGE_RAM_STACK_SIZE);
85 stack_top += ROMSTAGE_RAM_STACK_SIZE;
Aaron Durbine2d9e5b2013-02-08 17:38:35 -060086#else
87 stack_top = ROMSTAGE_STACK;
88#endif
89 return stack_top;
90}
91
Aaron Durbin38d94232013-02-07 00:03:33 -060092/* setup_romstage_stack_after_car() determines the stack to use after
93 * cache-as-ram is torn down as well as the MTRR settings to use. */
94static void *setup_romstage_stack_after_car(void)
95{
96 unsigned long top_of_stack;
97 int num_mtrrs;
98 u32 *slot;
99 u32 mtrr_mask_upper;
Aaron Durbin67481ddc2013-02-15 15:08:37 -0600100 u32 top_of_ram;
Aaron Durbin38d94232013-02-07 00:03:33 -0600101
102 /* Top of stack needs to be aligned to a 4-byte boundary. */
Aaron Durbine2d9e5b2013-02-08 17:38:35 -0600103 top_of_stack = choose_top_of_stack() & ~3;
Aaron Durbin38d94232013-02-07 00:03:33 -0600104 slot = (void *)top_of_stack;
105 num_mtrrs = 0;
106
107 /* The upper bits of the MTRR mask need to set according to the number
108 * of physical address bits. */
109 mtrr_mask_upper = (1 << ((cpuid_eax(0x80000008) & 0xff) - 32)) - 1;
110
111 /* The order for each MTTR is value then base with upper 32-bits of
112 * each value coming before the lower 32-bits. The reasoning for
113 * this ordering is to create a stack layout like the following:
114 * +0: Number of MTRRs
115 * +4: MTTR base 0 31:0
116 * +8: MTTR base 0 63:32
117 * +12: MTTR mask 0 31:0
118 * +16: MTTR mask 0 63:32
119 * +20: MTTR base 1 31:0
120 * +24: MTTR base 1 63:32
121 * +28: MTTR mask 1 31:0
122 * +32: MTTR mask 1 63:32
123 */
124
125 /* Cache the ROM as WP just below 4GiB. */
126 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
127 slot = stack_push(slot, ~(CONFIG_ROM_SIZE - 1) | MTRRphysMaskValid);
128 slot = stack_push(slot, 0); /* upper base */
129 slot = stack_push(slot, ~(CONFIG_ROM_SIZE - 1) | MTRR_TYPE_WRPROT);
130 num_mtrrs++;
131
132 /* Cache RAM as WB from 0 -> CONFIG_RAMTOP. */
133 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
134 slot = stack_push(slot, ~(CONFIG_RAMTOP - 1) | MTRRphysMaskValid);
135 slot = stack_push(slot, 0); /* upper base */
136 slot = stack_push(slot, 0 | MTRR_TYPE_WRBACK);
137 num_mtrrs++;
138
Aaron Durbin67481ddc2013-02-15 15:08:37 -0600139 top_of_ram = get_top_of_ram();
Aaron Durbin38d94232013-02-07 00:03:33 -0600140 /* Cache 8MiB below the top of ram. On haswell systems the top of
141 * ram under 4GiB is the start of the TSEG region. It is required to
142 * be 8MiB aligned. Set this area as cacheable so it can be used later
143 * for ramstage before setting up the entire RAM as cacheable. */
144 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
145 slot = stack_push(slot, ~((8 << 20) - 1) | MTRRphysMaskValid);
146 slot = stack_push(slot, 0); /* upper base */
Aaron Durbin67481ddc2013-02-15 15:08:37 -0600147 slot = stack_push(slot, (top_of_ram - (8 << 20)) | MTRR_TYPE_WRBACK);
148 num_mtrrs++;
149
150 /* Cache 8MiB at the top of ram. Top of ram on haswell systems
151 * is where the TSEG region resides. However, it is not restricted
152 * to SMM mode until SMM has been relocated. By setting the region
153 * to cacheable it provides faster access when relocating the SMM
154 * handler as well as using the TSEG region for other purposes. */
155 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
156 slot = stack_push(slot, ~((8 << 20) - 1) | MTRRphysMaskValid);
157 slot = stack_push(slot, 0); /* upper base */
158 slot = stack_push(slot, top_of_ram | MTRR_TYPE_WRBACK);
Aaron Durbin38d94232013-02-07 00:03:33 -0600159 num_mtrrs++;
160
161 /* Save the number of MTTRs to setup. Return the stack location
162 * pointing to the number of MTRRs. */
163 slot = stack_push(slot, num_mtrrs);
164
165 return slot;
166}
167
Aaron Durbin39ecc652013-05-02 09:42:13 -0500168void * asmlinkage romstage_main(unsigned long bist)
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600169{
170 int i;
Aaron Durbin38d94232013-02-07 00:03:33 -0600171 void *romstage_stack_after_car;
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600172 const int num_guards = 4;
173 const u32 stack_guard = 0xdeadbeef;
174 u32 *stack_base = (void *)(CONFIG_DCACHE_RAM_BASE +
175 CONFIG_DCACHE_RAM_SIZE -
176 CONFIG_DCACHE_RAM_ROMSTAGE_STACK_SIZE);
177
178 printk(BIOS_DEBUG, "Setting up stack guards.\n");
179 for (i = 0; i < num_guards; i++)
180 stack_base[i] = stack_guard;
181
Aaron Durbina2671612013-02-06 21:41:01 -0600182 mainboard_romstage_entry(bist);
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600183
184 /* Check the stack. */
185 for (i = 0; i < num_guards; i++) {
186 if (stack_base[i] == stack_guard)
187 continue;
188 printk(BIOS_DEBUG, "Smashed stack detected in romstage!\n");
189 }
190
Aaron Durbin38d94232013-02-07 00:03:33 -0600191 /* Get the stack to use after cache-as-ram is torn down. */
192 romstage_stack_after_car = setup_romstage_stack_after_car();
193
Aaron Durbin38d94232013-02-07 00:03:33 -0600194 return romstage_stack_after_car;
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600195}
Aaron Durbina2671612013-02-06 21:41:01 -0600196
197void romstage_common(const struct romstage_params *params)
198{
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600199 int boot_mode;
Aaron Durbina2671612013-02-06 21:41:01 -0600200 int wake_from_s3;
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600201 struct romstage_handoff *handoff;
Aaron Durbina2671612013-02-06 21:41:01 -0600202
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300203 timestamp_init(get_initial_timestamp());
204 timestamp_add_now(TS_START_ROMSTAGE);
Aaron Durbina2671612013-02-06 21:41:01 -0600205
206 if (params->bist == 0)
207 enable_lapic();
208
209 wake_from_s3 = early_pch_init(params->gpio_map, params->rcba_config);
210
Duncan Laurie7cced0d2013-06-04 10:03:34 -0700211#if CONFIG_EC_GOOGLE_CHROMEEC
212 /* Ensure the EC is in the right mode for recovery */
213 google_chromeec_early_init();
214#endif
215
Aaron Durbina2671612013-02-06 21:41:01 -0600216 /* Halt if there was a built in self test failure */
217 report_bist_failure(params->bist);
218
219 /* Perform some early chipset initialization required
220 * before RAM initialization can work
221 */
222 haswell_early_initialization(HASWELL_MOBILE);
223 printk(BIOS_DEBUG, "Back from haswell_early_initialization()\n");
224
225 if (wake_from_s3) {
226#if CONFIG_HAVE_ACPI_RESUME
227 printk(BIOS_DEBUG, "Resume from S3 detected.\n");
Aaron Durbina2671612013-02-06 21:41:01 -0600228#else
229 printk(BIOS_DEBUG, "Resume from S3 detected, but disabled.\n");
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600230 wake_from_s3 = 0;
Aaron Durbina2671612013-02-06 21:41:01 -0600231#endif
232 }
233
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600234 /* There are hard coded assumptions of 2 meaning s3 wake. Normalize
235 * the users of the 2 literal here based off wake_from_s3. */
236 boot_mode = wake_from_s3 ? 2 : 0;
237
Aaron Durbina2671612013-02-06 21:41:01 -0600238 /* Prepare USB controller early in S3 resume */
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600239 if (wake_from_s3)
Aaron Durbina2671612013-02-06 21:41:01 -0600240 enable_usb_bar();
241
242 post_code(0x3a);
243 params->pei_data->boot_mode = boot_mode;
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300244
245 timestamp_add_now(TS_BEFORE_INITRAM);
Aaron Durbina2671612013-02-06 21:41:01 -0600246
247 report_platform_info();
248
249 sdram_initialize(params->pei_data);
250
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300251 timestamp_add_now(TS_AFTER_INITRAM);
252
Aaron Durbina2671612013-02-06 21:41:01 -0600253 post_code(0x3b);
254
255 intel_early_me_status();
256
257 quick_ram_check();
258 post_code(0x3e);
259
Aaron Durbinc0cbd6e2013-03-13 13:51:20 -0500260 if (!wake_from_s3) {
261 cbmem_initialize_empty();
262 /* Save data returned from MRC on non-S3 resumes. */
Aaron Durbin2ad1dba2013-02-07 00:51:18 -0600263 save_mrc_data(params->pei_data);
Aaron Durbinc0cbd6e2013-03-13 13:51:20 -0500264 } else if (cbmem_initialize()) {
265 #if CONFIG_HAVE_ACPI_RESUME
Aaron Durbina2671612013-02-06 21:41:01 -0600266 /* Failed S3 resume, reset to come up cleanly */
Aaron Durbinb86113f2013-02-19 08:59:16 -0600267 reset_system();
Aaron Durbinc0cbd6e2013-03-13 13:51:20 -0500268 #endif
Aaron Durbina2671612013-02-06 21:41:01 -0600269 }
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600270
271 handoff = romstage_handoff_find_or_add();
272 if (handoff != NULL)
273 handoff->s3_resume = wake_from_s3;
274 else
275 printk(BIOS_DEBUG, "Romstage handoff structure not added!\n");
276
Aaron Durbina2671612013-02-06 21:41:01 -0600277 post_code(0x3f);
278#if CONFIG_CHROMEOS
279 init_chromeos(boot_mode);
280#endif
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300281 timestamp_sync();
Aaron Durbina2671612013-02-06 21:41:01 -0600282 timestamp_add_now(TS_END_ROMSTAGE);
Aaron Durbina2671612013-02-06 21:41:01 -0600283}
Aaron Durbin7492ec12013-02-08 22:18:04 -0600284
Aaron Durbind02bb622013-03-01 17:40:49 -0600285static inline void prepare_for_resume(struct romstage_handoff *handoff)
Aaron Durbin7492ec12013-02-08 22:18:04 -0600286{
Aaron Durbine2d9e5b2013-02-08 17:38:35 -0600287/* Only need to save memory when ramstage isn't relocatable. */
288#if !CONFIG_RELOCATABLE_RAMSTAGE
Aaron Durbin7492ec12013-02-08 22:18:04 -0600289#if CONFIG_HAVE_ACPI_RESUME
290 /* Back up the OS-controlled memory where ramstage will be loaded. */
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600291 if (handoff != NULL && handoff->s3_resume) {
Aaron Durbin7492ec12013-02-08 22:18:04 -0600292 void *src = (void *)CONFIG_RAMBASE;
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600293 void *dest = cbmem_find(CBMEM_ID_RESUME);
294 if (dest != NULL)
295 memcpy(dest, src, HIGH_MEMORY_SAVE);
Aaron Durbin7492ec12013-02-08 22:18:04 -0600296 }
297#endif
Aaron Durbine2d9e5b2013-02-08 17:38:35 -0600298#endif
Aaron Durbin7492ec12013-02-08 22:18:04 -0600299}
300
301void romstage_after_car(void)
302{
Aaron Durbind02bb622013-03-01 17:40:49 -0600303 struct romstage_handoff *handoff;
304
305 handoff = romstage_handoff_find_or_add();
306
307 prepare_for_resume(handoff);
308
309#if CONFIG_VBOOT_VERIFY_FIRMWARE
310 vboot_verify_firmware(handoff);
311#endif
312
Aaron Durbin7492ec12013-02-08 22:18:04 -0600313 /* Load the ramstage. */
Stefan Reinauer648d1662013-05-06 18:05:39 -0700314 copy_and_run();
Aaron Durbin7492ec12013-02-08 22:18:04 -0600315}
Aaron Durbinf7cdfe52013-02-16 00:05:52 -0600316
317
318#if CONFIG_RELOCATABLE_RAMSTAGE
319void cache_loaded_ramstage(struct romstage_handoff *handoff,
Aaron Durbinc0cbd6e2013-03-13 13:51:20 -0500320 const struct cbmem_entry *ramstage,
Aaron Durbinf7cdfe52013-02-16 00:05:52 -0600321 void *entry_point)
322{
323 struct ramstage_cache *cache;
324 uint32_t total_size;
Aaron Durbinc0cbd6e2013-03-13 13:51:20 -0500325 uint32_t ramstage_size;
326 void *ramstage_base;
327
328 ramstage_size = cbmem_entry_size(ramstage);
329 ramstage_base = cbmem_entry_start(ramstage);
Aaron Durbinf7cdfe52013-02-16 00:05:52 -0600330
331 /* The ramstage cache lives in the TSEG region at RESERVED_SMM_OFFSET.
332 * The top of ram is defined to be the TSEG base address. */
333 cache = (void *)(get_top_of_ram() + RESERVED_SMM_OFFSET);
334 total_size = sizeof(*cache) + ramstage_size;
335 if (total_size > RESERVED_SMM_SIZE) {
336 printk(BIOS_DEBUG, "0x%08x > RESERVED_SMM_SIZE (0x%08x)\n",
337 total_size, RESERVED_SMM_SIZE);
338 /* Nuke whatever may be there now just in case. */
339 cache->magic = ~RAMSTAGE_CACHE_MAGIC;
340 return;
341 }
342
343 cache->magic = RAMSTAGE_CACHE_MAGIC;
344 cache->entry_point = (uint32_t)entry_point;
345 cache->load_address = (uint32_t)ramstage_base;
346 cache->size = ramstage_size;
347
348 printk(BIOS_DEBUG, "Saving ramstage to SMM space cache.\n");
349
350 /* Copy over the program. */
351 memcpy(&cache->program[0], ramstage_base, ramstage_size);
352
Aaron Durbinf7cdfe52013-02-16 00:05:52 -0600353 if (handoff == NULL)
354 return;
355
Aaron Durbinf7cdfe52013-02-16 00:05:52 -0600356 handoff->ramstage_entry_point = (uint32_t)entry_point;
357}
358
Aaron Durbinc0cbd6e2013-03-13 13:51:20 -0500359void *load_cached_ramstage(struct romstage_handoff *handoff,
360 const struct cbmem_entry *ramstage)
Aaron Durbinf7cdfe52013-02-16 00:05:52 -0600361{
362 struct ramstage_cache *cache;
363
364 /* The ramstage cache lives in the TSEG region at RESERVED_SMM_OFFSET.
365 * The top of ram is defined to be the TSEG base address. */
366 cache = (void *)(get_top_of_ram() + RESERVED_SMM_OFFSET);
367
368 if (cache->magic != RAMSTAGE_CACHE_MAGIC) {
369 printk(BIOS_DEBUG, "Invalid ramstage cache found.\n");
Aaron Durbinb86113f2013-02-19 08:59:16 -0600370 #if CONFIG_RESET_ON_INVALID_RAMSTAGE_CACHE
371 reset_system();
372 #endif
Aaron Durbinf7cdfe52013-02-16 00:05:52 -0600373 return NULL;
374 }
375
376 printk(BIOS_DEBUG, "Loading ramstage from SMM space cache.\n");
377
378 memcpy((void *)cache->load_address, &cache->program[0], cache->size);
379
380 return (void *)cache->entry_point;
381}
382#endif