blob: 35b51c5b5903a35865b6055bf45de7c822b3daa3 [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
Aaron Durbinc7633f42013-06-13 17:29:36 -0700249 if (params->copy_spd != NULL)
250 params->copy_spd(params->pei_data);
251
Aaron Durbina2671612013-02-06 21:41:01 -0600252 sdram_initialize(params->pei_data);
253
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300254 timestamp_add_now(TS_AFTER_INITRAM);
255
Aaron Durbina2671612013-02-06 21:41:01 -0600256 post_code(0x3b);
257
258 intel_early_me_status();
259
260 quick_ram_check();
261 post_code(0x3e);
262
Aaron Durbinc0cbd6e2013-03-13 13:51:20 -0500263 if (!wake_from_s3) {
264 cbmem_initialize_empty();
265 /* Save data returned from MRC on non-S3 resumes. */
Aaron Durbin2ad1dba2013-02-07 00:51:18 -0600266 save_mrc_data(params->pei_data);
Aaron Durbinc0cbd6e2013-03-13 13:51:20 -0500267 } else if (cbmem_initialize()) {
268 #if CONFIG_HAVE_ACPI_RESUME
Aaron Durbina2671612013-02-06 21:41:01 -0600269 /* Failed S3 resume, reset to come up cleanly */
Aaron Durbinb86113f2013-02-19 08:59:16 -0600270 reset_system();
Aaron Durbinc0cbd6e2013-03-13 13:51:20 -0500271 #endif
Aaron Durbina2671612013-02-06 21:41:01 -0600272 }
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600273
274 handoff = romstage_handoff_find_or_add();
275 if (handoff != NULL)
276 handoff->s3_resume = wake_from_s3;
277 else
278 printk(BIOS_DEBUG, "Romstage handoff structure not added!\n");
279
Aaron Durbina2671612013-02-06 21:41:01 -0600280 post_code(0x3f);
281#if CONFIG_CHROMEOS
282 init_chromeos(boot_mode);
283#endif
Aaron Durbina2671612013-02-06 21:41:01 -0600284 timestamp_add_now(TS_END_ROMSTAGE);
Aaron Durbina2671612013-02-06 21:41:01 -0600285}
Aaron Durbin7492ec12013-02-08 22:18:04 -0600286
Aaron Durbind02bb622013-03-01 17:40:49 -0600287static inline void prepare_for_resume(struct romstage_handoff *handoff)
Aaron Durbin7492ec12013-02-08 22:18:04 -0600288{
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
292 /* Back up the OS-controlled memory where ramstage will be loaded. */
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600293 if (handoff != NULL && handoff->s3_resume) {
Aaron Durbin7492ec12013-02-08 22:18:04 -0600294 void *src = (void *)CONFIG_RAMBASE;
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600295 void *dest = cbmem_find(CBMEM_ID_RESUME);
296 if (dest != NULL)
297 memcpy(dest, src, HIGH_MEMORY_SAVE);
Aaron Durbin7492ec12013-02-08 22:18:04 -0600298 }
299#endif
Aaron Durbine2d9e5b2013-02-08 17:38:35 -0600300#endif
Aaron Durbin7492ec12013-02-08 22:18:04 -0600301}
302
303void romstage_after_car(void)
304{
Aaron Durbind02bb622013-03-01 17:40:49 -0600305 struct romstage_handoff *handoff;
306
307 handoff = romstage_handoff_find_or_add();
308
309 prepare_for_resume(handoff);
310
311#if CONFIG_VBOOT_VERIFY_FIRMWARE
312 vboot_verify_firmware(handoff);
313#endif
314
Aaron Durbin7492ec12013-02-08 22:18:04 -0600315 /* Load the ramstage. */
Stefan Reinauer648d1662013-05-06 18:05:39 -0700316 copy_and_run();
Aaron Durbin7492ec12013-02-08 22:18:04 -0600317}
Aaron Durbinf7cdfe52013-02-16 00:05:52 -0600318
319
320#if CONFIG_RELOCATABLE_RAMSTAGE
321void cache_loaded_ramstage(struct romstage_handoff *handoff,
Aaron Durbinc0cbd6e2013-03-13 13:51:20 -0500322 const struct cbmem_entry *ramstage,
Aaron Durbinf7cdfe52013-02-16 00:05:52 -0600323 void *entry_point)
324{
325 struct ramstage_cache *cache;
326 uint32_t total_size;
Aaron Durbinc0cbd6e2013-03-13 13:51:20 -0500327 uint32_t ramstage_size;
328 void *ramstage_base;
329
330 ramstage_size = cbmem_entry_size(ramstage);
331 ramstage_base = cbmem_entry_start(ramstage);
Aaron Durbinf7cdfe52013-02-16 00:05:52 -0600332
333 /* The ramstage cache lives in the TSEG region at RESERVED_SMM_OFFSET.
334 * The top of ram is defined to be the TSEG base address. */
335 cache = (void *)(get_top_of_ram() + RESERVED_SMM_OFFSET);
336 total_size = sizeof(*cache) + ramstage_size;
337 if (total_size > RESERVED_SMM_SIZE) {
338 printk(BIOS_DEBUG, "0x%08x > RESERVED_SMM_SIZE (0x%08x)\n",
339 total_size, RESERVED_SMM_SIZE);
340 /* Nuke whatever may be there now just in case. */
341 cache->magic = ~RAMSTAGE_CACHE_MAGIC;
342 return;
343 }
344
345 cache->magic = RAMSTAGE_CACHE_MAGIC;
346 cache->entry_point = (uint32_t)entry_point;
347 cache->load_address = (uint32_t)ramstage_base;
348 cache->size = ramstage_size;
349
350 printk(BIOS_DEBUG, "Saving ramstage to SMM space cache.\n");
351
352 /* Copy over the program. */
353 memcpy(&cache->program[0], ramstage_base, ramstage_size);
354
Aaron Durbinf7cdfe52013-02-16 00:05:52 -0600355 if (handoff == NULL)
356 return;
357
Aaron Durbinf7cdfe52013-02-16 00:05:52 -0600358 handoff->ramstage_entry_point = (uint32_t)entry_point;
359}
360
Aaron Durbinc0cbd6e2013-03-13 13:51:20 -0500361void *load_cached_ramstage(struct romstage_handoff *handoff,
362 const struct cbmem_entry *ramstage)
Aaron Durbinf7cdfe52013-02-16 00:05:52 -0600363{
364 struct ramstage_cache *cache;
365
366 /* The ramstage cache lives in the TSEG region at RESERVED_SMM_OFFSET.
367 * The top of ram is defined to be the TSEG base address. */
368 cache = (void *)(get_top_of_ram() + RESERVED_SMM_OFFSET);
369
370 if (cache->magic != RAMSTAGE_CACHE_MAGIC) {
371 printk(BIOS_DEBUG, "Invalid ramstage cache found.\n");
Aaron Durbinb86113f2013-02-19 08:59:16 -0600372 #if CONFIG_RESET_ON_INVALID_RAMSTAGE_CACHE
373 reset_system();
374 #endif
Aaron Durbinf7cdfe52013-02-16 00:05:52 -0600375 return NULL;
376 }
377
378 printk(BIOS_DEBUG, "Loading ramstage from SMM space cache.\n");
379
380 memcpy((void *)cache->load_address, &cache->program[0], cache->size);
381
382 return (void *)cache->entry_point;
383}
384#endif