blob: edb2fdfccfd5324d5a438d45be7a6a06448cad10 [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>
Aaron Durbina2671612013-02-06 21:41:01 -060028#include <lib.h>
29#include <timestamp.h>
30#include <arch/io.h>
Aaron Durbin7492ec12013-02-08 22:18:04 -060031#include <arch/stages.h>
Aaron Durbina2671612013-02-06 21:41:01 -060032#include <device/pci_def.h>
33#include <cpu/x86/lapic.h>
Aaron Durbinf7cdfe52013-02-16 00:05:52 -060034#include <cbfs.h>
Aaron Durbin75e29742013-10-10 20:37:04 -050035#include <ramstage_cache.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#include <vendorcode/google/chromeos/chromeos.h>
Duncan Laurie7cced0d2013-06-04 10:03:34 -070039#if CONFIG_EC_GOOGLE_CHROMEEC
40#include <ec/google/chromeec/ec.h>
41#endif
Aaron Durbina2671612013-02-06 21:41:01 -060042#include "haswell.h"
43#include "northbridge/intel/haswell/haswell.h"
44#include "northbridge/intel/haswell/raminit.h"
45#include "southbridge/intel/lynxpoint/pch.h"
46#include "southbridge/intel/lynxpoint/me.h"
Aaron Durbin3d0071b2013-01-18 14:32:50 -060047
Aaron Durbina2671612013-02-06 21:41:01 -060048
Aaron Durbinb86113f2013-02-19 08:59:16 -060049static inline void reset_system(void)
50{
51 hard_reset();
52 while (1) {
53 hlt();
54 }
55}
56
Aaron Durbin38d94232013-02-07 00:03:33 -060057/* The cache-as-ram assembly file calls romstage_main() after setting up
58 * cache-as-ram. romstage_main() will then call the mainboards's
59 * mainboard_romstage_entry() function. That function then calls
60 * romstage_common() below. The reason for the back and forth is to provide
61 * common entry point from cache-as-ram while still allowing for code sharing.
62 * Because we can't use global variables the stack is used for allocations --
63 * thus the need to call back and forth. */
Aaron Durbin3d0071b2013-01-18 14:32:50 -060064
Aaron Durbin38d94232013-02-07 00:03:33 -060065
66static inline u32 *stack_push(u32 *stack, u32 value)
67{
68 stack = &stack[-1];
69 *stack = value;
70 return stack;
71}
72
Aaron Durbinc0cbd6e2013-03-13 13:51:20 -050073/* Romstage needs quite a bit of stack for decompressing images since the lzma
74 * lib keeps its state on the stack during romstage. */
75#define ROMSTAGE_RAM_STACK_SIZE 0x5000
Aaron Durbine2d9e5b2013-02-08 17:38:35 -060076static unsigned long choose_top_of_stack(void)
77{
78 unsigned long stack_top;
Aaron Durbinc0cbd6e2013-03-13 13:51:20 -050079#if CONFIG_DYNAMIC_CBMEM
80 /* cbmem_add() does a find() before add(). */
81 stack_top = (unsigned long)cbmem_add(CBMEM_ID_ROMSTAGE_RAM_STACK,
82 ROMSTAGE_RAM_STACK_SIZE);
83 stack_top += ROMSTAGE_RAM_STACK_SIZE;
Aaron Durbine2d9e5b2013-02-08 17:38:35 -060084#else
Kyösti Mälkki1729cd82014-10-16 12:47:25 +030085 stack_top = CONFIG_RAMTOP;
Aaron Durbine2d9e5b2013-02-08 17:38:35 -060086#endif
87 return stack_top;
88}
89
Aaron Durbin38d94232013-02-07 00:03:33 -060090/* setup_romstage_stack_after_car() determines the stack to use after
91 * cache-as-ram is torn down as well as the MTRR settings to use. */
92static void *setup_romstage_stack_after_car(void)
93{
94 unsigned long top_of_stack;
95 int num_mtrrs;
96 u32 *slot;
97 u32 mtrr_mask_upper;
Aaron Durbin67481ddc2013-02-15 15:08:37 -060098 u32 top_of_ram;
Aaron Durbin38d94232013-02-07 00:03:33 -060099
100 /* Top of stack needs to be aligned to a 4-byte boundary. */
Aaron Durbine2d9e5b2013-02-08 17:38:35 -0600101 top_of_stack = choose_top_of_stack() & ~3;
Aaron Durbin38d94232013-02-07 00:03:33 -0600102 slot = (void *)top_of_stack;
103 num_mtrrs = 0;
104
105 /* The upper bits of the MTRR mask need to set according to the number
106 * of physical address bits. */
107 mtrr_mask_upper = (1 << ((cpuid_eax(0x80000008) & 0xff) - 32)) - 1;
108
Paul Menzel4fe98132014-01-25 15:55:28 +0100109 /* The order for each MTRR is value then base with upper 32-bits of
Aaron Durbin38d94232013-02-07 00:03:33 -0600110 * each value coming before the lower 32-bits. The reasoning for
111 * this ordering is to create a stack layout like the following:
112 * +0: Number of MTRRs
Paul Menzel4fe98132014-01-25 15:55:28 +0100113 * +4: MTRR base 0 31:0
114 * +8: MTRR base 0 63:32
115 * +12: MTRR mask 0 31:0
116 * +16: MTRR mask 0 63:32
117 * +20: MTRR base 1 31:0
118 * +24: MTRR base 1 63:32
119 * +28: MTRR mask 1 31:0
120 * +32: MTRR mask 1 63:32
Aaron Durbin38d94232013-02-07 00:03:33 -0600121 */
122
123 /* Cache the ROM as WP just below 4GiB. */
124 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
Kyösti Mälkki107f72e2014-01-06 11:06:26 +0200125 slot = stack_push(slot, ~(CACHE_ROM_SIZE - 1) | MTRRphysMaskValid);
Aaron Durbin38d94232013-02-07 00:03:33 -0600126 slot = stack_push(slot, 0); /* upper base */
Kyösti Mälkki107f72e2014-01-06 11:06:26 +0200127 slot = stack_push(slot, ~(CACHE_ROM_SIZE - 1) | MTRR_TYPE_WRPROT);
Aaron Durbin38d94232013-02-07 00:03:33 -0600128 num_mtrrs++;
129
130 /* Cache RAM as WB from 0 -> CONFIG_RAMTOP. */
131 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
132 slot = stack_push(slot, ~(CONFIG_RAMTOP - 1) | MTRRphysMaskValid);
133 slot = stack_push(slot, 0); /* upper base */
134 slot = stack_push(slot, 0 | MTRR_TYPE_WRBACK);
135 num_mtrrs++;
136
Aaron Durbin67481ddc2013-02-15 15:08:37 -0600137 top_of_ram = get_top_of_ram();
Aaron Durbin38d94232013-02-07 00:03:33 -0600138 /* Cache 8MiB below the top of ram. On haswell systems the top of
139 * ram under 4GiB is the start of the TSEG region. It is required to
140 * be 8MiB aligned. Set this area as cacheable so it can be used later
141 * for ramstage before setting up the entire RAM as cacheable. */
142 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
143 slot = stack_push(slot, ~((8 << 20) - 1) | MTRRphysMaskValid);
144 slot = stack_push(slot, 0); /* upper base */
Aaron Durbin67481ddc2013-02-15 15:08:37 -0600145 slot = stack_push(slot, (top_of_ram - (8 << 20)) | MTRR_TYPE_WRBACK);
146 num_mtrrs++;
147
148 /* Cache 8MiB at the top of ram. Top of ram on haswell systems
149 * is where the TSEG region resides. However, it is not restricted
150 * to SMM mode until SMM has been relocated. By setting the region
151 * to cacheable it provides faster access when relocating the SMM
152 * handler as well as using the TSEG region for other purposes. */
153 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
154 slot = stack_push(slot, ~((8 << 20) - 1) | MTRRphysMaskValid);
155 slot = stack_push(slot, 0); /* upper base */
156 slot = stack_push(slot, top_of_ram | MTRR_TYPE_WRBACK);
Aaron Durbin38d94232013-02-07 00:03:33 -0600157 num_mtrrs++;
158
Paul Menzel4fe98132014-01-25 15:55:28 +0100159 /* Save the number of MTRRs to setup. Return the stack location
Aaron Durbin38d94232013-02-07 00:03:33 -0600160 * pointing to the number of MTRRs. */
161 slot = stack_push(slot, num_mtrrs);
162
163 return slot;
164}
165
Aaron Durbin39ecc652013-05-02 09:42:13 -0500166void * asmlinkage romstage_main(unsigned long bist)
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600167{
168 int i;
Aaron Durbin38d94232013-02-07 00:03:33 -0600169 void *romstage_stack_after_car;
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600170 const int num_guards = 4;
171 const u32 stack_guard = 0xdeadbeef;
172 u32 *stack_base = (void *)(CONFIG_DCACHE_RAM_BASE +
173 CONFIG_DCACHE_RAM_SIZE -
174 CONFIG_DCACHE_RAM_ROMSTAGE_STACK_SIZE);
175
176 printk(BIOS_DEBUG, "Setting up stack guards.\n");
177 for (i = 0; i < num_guards; i++)
178 stack_base[i] = stack_guard;
179
Aaron Durbina2671612013-02-06 21:41:01 -0600180 mainboard_romstage_entry(bist);
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600181
182 /* Check the stack. */
183 for (i = 0; i < num_guards; i++) {
184 if (stack_base[i] == stack_guard)
185 continue;
186 printk(BIOS_DEBUG, "Smashed stack detected in romstage!\n");
187 }
188
Aaron Durbin38d94232013-02-07 00:03:33 -0600189 /* Get the stack to use after cache-as-ram is torn down. */
190 romstage_stack_after_car = setup_romstage_stack_after_car();
191
Aaron Durbin38d94232013-02-07 00:03:33 -0600192 return romstage_stack_after_car;
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600193}
Aaron Durbina2671612013-02-06 21:41:01 -0600194
195void romstage_common(const struct romstage_params *params)
196{
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600197 int boot_mode;
Aaron Durbina2671612013-02-06 21:41:01 -0600198 int wake_from_s3;
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600199 struct romstage_handoff *handoff;
Aaron Durbina2671612013-02-06 21:41:01 -0600200
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300201 timestamp_init(get_initial_timestamp());
202 timestamp_add_now(TS_START_ROMSTAGE);
Aaron Durbina2671612013-02-06 21:41:01 -0600203
204 if (params->bist == 0)
205 enable_lapic();
206
207 wake_from_s3 = early_pch_init(params->gpio_map, params->rcba_config);
208
Duncan Laurie7cced0d2013-06-04 10:03:34 -0700209#if CONFIG_EC_GOOGLE_CHROMEEC
210 /* Ensure the EC is in the right mode for recovery */
211 google_chromeec_early_init();
212#endif
213
Aaron Durbina2671612013-02-06 21:41:01 -0600214 /* Halt if there was a built in self test failure */
215 report_bist_failure(params->bist);
216
217 /* Perform some early chipset initialization required
218 * before RAM initialization can work
219 */
220 haswell_early_initialization(HASWELL_MOBILE);
221 printk(BIOS_DEBUG, "Back from haswell_early_initialization()\n");
222
223 if (wake_from_s3) {
224#if CONFIG_HAVE_ACPI_RESUME
225 printk(BIOS_DEBUG, "Resume from S3 detected.\n");
Aaron Durbina2671612013-02-06 21:41:01 -0600226#else
227 printk(BIOS_DEBUG, "Resume from S3 detected, but disabled.\n");
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600228 wake_from_s3 = 0;
Aaron Durbina2671612013-02-06 21:41:01 -0600229#endif
230 }
231
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600232 /* There are hard coded assumptions of 2 meaning s3 wake. Normalize
233 * the users of the 2 literal here based off wake_from_s3. */
234 boot_mode = wake_from_s3 ? 2 : 0;
235
Aaron Durbina2671612013-02-06 21:41:01 -0600236 /* Prepare USB controller early in S3 resume */
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600237 if (wake_from_s3)
Aaron Durbina2671612013-02-06 21:41:01 -0600238 enable_usb_bar();
239
240 post_code(0x3a);
241 params->pei_data->boot_mode = boot_mode;
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300242
243 timestamp_add_now(TS_BEFORE_INITRAM);
Aaron Durbina2671612013-02-06 21:41:01 -0600244
245 report_platform_info();
246
Aaron Durbinc7633f42013-06-13 17:29:36 -0700247 if (params->copy_spd != NULL)
248 params->copy_spd(params->pei_data);
249
Aaron Durbina2671612013-02-06 21:41:01 -0600250 sdram_initialize(params->pei_data);
251
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300252 timestamp_add_now(TS_AFTER_INITRAM);
253
Aaron Durbina2671612013-02-06 21:41:01 -0600254 post_code(0x3b);
255
256 intel_early_me_status();
257
258 quick_ram_check();
259 post_code(0x3e);
260
Aaron Durbinc0cbd6e2013-03-13 13:51:20 -0500261 if (!wake_from_s3) {
262 cbmem_initialize_empty();
263 /* Save data returned from MRC on non-S3 resumes. */
Aaron Durbin2ad1dba2013-02-07 00:51:18 -0600264 save_mrc_data(params->pei_data);
Aaron Durbinc0cbd6e2013-03-13 13:51:20 -0500265 } else if (cbmem_initialize()) {
266 #if CONFIG_HAVE_ACPI_RESUME
Aaron Durbina2671612013-02-06 21:41:01 -0600267 /* Failed S3 resume, reset to come up cleanly */
Aaron Durbinb86113f2013-02-19 08:59:16 -0600268 reset_system();
Aaron Durbinc0cbd6e2013-03-13 13:51:20 -0500269 #endif
Aaron Durbina2671612013-02-06 21:41:01 -0600270 }
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600271
272 handoff = romstage_handoff_find_or_add();
273 if (handoff != NULL)
274 handoff->s3_resume = wake_from_s3;
275 else
276 printk(BIOS_DEBUG, "Romstage handoff structure not added!\n");
277
Aaron Durbina2671612013-02-06 21:41:01 -0600278 post_code(0x3f);
279#if CONFIG_CHROMEOS
280 init_chromeos(boot_mode);
281#endif
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
Aaron Durbin7492ec12013-02-08 22:18:04 -0600309 /* Load the ramstage. */
Stefan Reinauer648d1662013-05-06 18:05:39 -0700310 copy_and_run();
Aaron Durbin7492ec12013-02-08 22:18:04 -0600311}
Aaron Durbinf7cdfe52013-02-16 00:05:52 -0600312
313
314#if CONFIG_RELOCATABLE_RAMSTAGE
Aaron Durbin75e29742013-10-10 20:37:04 -0500315#include <ramstage_cache.h>
316
317struct ramstage_cache *ramstage_cache_location(long *size)
Aaron Durbinf7cdfe52013-02-16 00:05:52 -0600318{
Aaron Durbinf7cdfe52013-02-16 00:05:52 -0600319 /* The ramstage cache lives in the TSEG region at RESERVED_SMM_OFFSET.
320 * The top of ram is defined to be the TSEG base address. */
Aaron Durbin75e29742013-10-10 20:37:04 -0500321 *size = RESERVED_SMM_SIZE;
322 return (void *)(get_top_of_ram() + RESERVED_SMM_OFFSET);
Aaron Durbinf7cdfe52013-02-16 00:05:52 -0600323}
324
Aaron Durbin75e29742013-10-10 20:37:04 -0500325void ramstage_cache_invalid(struct ramstage_cache *cache)
Aaron Durbinf7cdfe52013-02-16 00:05:52 -0600326{
Aaron Durbin75e29742013-10-10 20:37:04 -0500327#if CONFIG_RESET_ON_INVALID_RAMSTAGE_CACHE
328 reset_system();
329#endif
Aaron Durbinf7cdfe52013-02-16 00:05:52 -0600330}
331#endif