blob: b8c455bcff4264cde9721ad5532bd95e7319078c [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 Durbinbd74a4b2015-03-06 23:17:33 -060022#include <cbfs.h>
Aaron Durbin3d0071b2013-01-18 14:32:50 -060023#include <cbmem.h>
24#include <console/console.h>
Aaron Durbina2671612013-02-06 21:41:01 -060025#include <arch/cpu.h>
26#include <cpu/x86/bist.h>
27#include <cpu/x86/msr.h>
Aaron Durbin38d94232013-02-07 00:03:33 -060028#include <cpu/x86/mtrr.h>
Patrick Georgibd79c5e2014-11-28 22:35:36 +010029#include <halt.h>
Aaron Durbina2671612013-02-06 21:41:01 -060030#include <lib.h>
31#include <timestamp.h>
32#include <arch/io.h>
Aaron Durbin7492ec12013-02-08 22:18:04 -060033#include <arch/stages.h>
Aaron Durbina2671612013-02-06 21:41:01 -060034#include <device/pci_def.h>
35#include <cpu/x86/lapic.h>
Aaron Durbinf7cdfe52013-02-16 00:05:52 -060036#include <cbfs.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 Durbinbd74a4b2015-03-06 23:17:33 -060039#include <stage_cache.h>
Aaron Durbina2671612013-02-06 21:41:01 -060040#include <vendorcode/google/chromeos/chromeos.h>
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();
Patrick Georgibd79c5e2014-11-28 22:35:36 +010054 halt();
Aaron Durbinb86113f2013-02-19 08:59:16 -060055}
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;
Kyösti Mälkkiae98e832014-11-28 11:24:19 +020079
Aaron Durbinc0cbd6e2013-03-13 13:51:20 -050080 /* 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 return stack_top;
85}
86
Aaron Durbin38d94232013-02-07 00:03:33 -060087/* setup_romstage_stack_after_car() determines the stack to use after
88 * cache-as-ram is torn down as well as the MTRR settings to use. */
89static void *setup_romstage_stack_after_car(void)
90{
91 unsigned long top_of_stack;
92 int num_mtrrs;
93 u32 *slot;
94 u32 mtrr_mask_upper;
Aaron Durbin67481ddc2013-02-15 15:08:37 -060095 u32 top_of_ram;
Aaron Durbin38d94232013-02-07 00:03:33 -060096
97 /* Top of stack needs to be aligned to a 4-byte boundary. */
Aaron Durbine2d9e5b2013-02-08 17:38:35 -060098 top_of_stack = choose_top_of_stack() & ~3;
Aaron Durbin38d94232013-02-07 00:03:33 -060099 slot = (void *)top_of_stack;
100 num_mtrrs = 0;
101
102 /* The upper bits of the MTRR mask need to set according to the number
103 * of physical address bits. */
104 mtrr_mask_upper = (1 << ((cpuid_eax(0x80000008) & 0xff) - 32)) - 1;
105
Paul Menzel4fe98132014-01-25 15:55:28 +0100106 /* The order for each MTRR is value then base with upper 32-bits of
Aaron Durbin38d94232013-02-07 00:03:33 -0600107 * each value coming before the lower 32-bits. The reasoning for
108 * this ordering is to create a stack layout like the following:
109 * +0: Number of MTRRs
Paul Menzel4fe98132014-01-25 15:55:28 +0100110 * +4: MTRR base 0 31:0
111 * +8: MTRR base 0 63:32
112 * +12: MTRR mask 0 31:0
113 * +16: MTRR mask 0 63:32
114 * +20: MTRR base 1 31:0
115 * +24: MTRR base 1 63:32
116 * +28: MTRR mask 1 31:0
117 * +32: MTRR mask 1 63:32
Aaron Durbin38d94232013-02-07 00:03:33 -0600118 */
119
120 /* Cache the ROM as WP just below 4GiB. */
121 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
Kyösti Mälkki107f72e2014-01-06 11:06:26 +0200122 slot = stack_push(slot, ~(CACHE_ROM_SIZE - 1) | MTRRphysMaskValid);
Aaron Durbin38d94232013-02-07 00:03:33 -0600123 slot = stack_push(slot, 0); /* upper base */
Kyösti Mälkki107f72e2014-01-06 11:06:26 +0200124 slot = stack_push(slot, ~(CACHE_ROM_SIZE - 1) | MTRR_TYPE_WRPROT);
Aaron Durbin38d94232013-02-07 00:03:33 -0600125 num_mtrrs++;
126
127 /* Cache RAM as WB from 0 -> CONFIG_RAMTOP. */
128 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
129 slot = stack_push(slot, ~(CONFIG_RAMTOP - 1) | MTRRphysMaskValid);
130 slot = stack_push(slot, 0); /* upper base */
131 slot = stack_push(slot, 0 | MTRR_TYPE_WRBACK);
132 num_mtrrs++;
133
Kyösti Mälkkif1e3c762014-12-22 12:28:07 +0200134 top_of_ram = (uint32_t)cbmem_top();
Aaron Durbin38d94232013-02-07 00:03:33 -0600135 /* Cache 8MiB below the top of ram. On haswell systems the top of
136 * ram under 4GiB is the start of the TSEG region. It is required to
137 * be 8MiB aligned. Set this area as cacheable so it can be used later
138 * for ramstage before setting up the entire RAM as cacheable. */
139 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
140 slot = stack_push(slot, ~((8 << 20) - 1) | MTRRphysMaskValid);
141 slot = stack_push(slot, 0); /* upper base */
Aaron Durbin67481ddc2013-02-15 15:08:37 -0600142 slot = stack_push(slot, (top_of_ram - (8 << 20)) | MTRR_TYPE_WRBACK);
143 num_mtrrs++;
144
145 /* Cache 8MiB at the top of ram. Top of ram on haswell systems
146 * is where the TSEG region resides. However, it is not restricted
147 * to SMM mode until SMM has been relocated. By setting the region
148 * to cacheable it provides faster access when relocating the SMM
149 * handler as well as using the TSEG region for other purposes. */
150 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
151 slot = stack_push(slot, ~((8 << 20) - 1) | MTRRphysMaskValid);
152 slot = stack_push(slot, 0); /* upper base */
153 slot = stack_push(slot, top_of_ram | MTRR_TYPE_WRBACK);
Aaron Durbin38d94232013-02-07 00:03:33 -0600154 num_mtrrs++;
155
Paul Menzel4fe98132014-01-25 15:55:28 +0100156 /* Save the number of MTRRs to setup. Return the stack location
Aaron Durbin38d94232013-02-07 00:03:33 -0600157 * pointing to the number of MTRRs. */
158 slot = stack_push(slot, num_mtrrs);
159
160 return slot;
161}
162
Aaron Durbin39ecc652013-05-02 09:42:13 -0500163void * asmlinkage romstage_main(unsigned long bist)
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600164{
165 int i;
Aaron Durbin38d94232013-02-07 00:03:33 -0600166 void *romstage_stack_after_car;
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600167 const int num_guards = 4;
168 const u32 stack_guard = 0xdeadbeef;
169 u32 *stack_base = (void *)(CONFIG_DCACHE_RAM_BASE +
170 CONFIG_DCACHE_RAM_SIZE -
171 CONFIG_DCACHE_RAM_ROMSTAGE_STACK_SIZE);
172
173 printk(BIOS_DEBUG, "Setting up stack guards.\n");
174 for (i = 0; i < num_guards; i++)
175 stack_base[i] = stack_guard;
176
Aaron Durbina2671612013-02-06 21:41:01 -0600177 mainboard_romstage_entry(bist);
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600178
179 /* Check the stack. */
180 for (i = 0; i < num_guards; i++) {
181 if (stack_base[i] == stack_guard)
182 continue;
183 printk(BIOS_DEBUG, "Smashed stack detected in romstage!\n");
184 }
185
Aaron Durbin38d94232013-02-07 00:03:33 -0600186 /* Get the stack to use after cache-as-ram is torn down. */
187 romstage_stack_after_car = setup_romstage_stack_after_car();
188
Aaron Durbin38d94232013-02-07 00:03:33 -0600189 return romstage_stack_after_car;
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600190}
Aaron Durbina2671612013-02-06 21:41:01 -0600191
192void romstage_common(const struct romstage_params *params)
193{
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600194 int boot_mode;
Aaron Durbina2671612013-02-06 21:41:01 -0600195 int wake_from_s3;
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600196 struct romstage_handoff *handoff;
Aaron Durbina2671612013-02-06 21:41:01 -0600197
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300198 timestamp_init(get_initial_timestamp());
199 timestamp_add_now(TS_START_ROMSTAGE);
Aaron Durbina2671612013-02-06 21:41:01 -0600200
201 if (params->bist == 0)
202 enable_lapic();
203
204 wake_from_s3 = early_pch_init(params->gpio_map, params->rcba_config);
205
Duncan Laurie7cced0d2013-06-04 10:03:34 -0700206#if CONFIG_EC_GOOGLE_CHROMEEC
207 /* Ensure the EC is in the right mode for recovery */
208 google_chromeec_early_init();
209#endif
210
Aaron Durbina2671612013-02-06 21:41:01 -0600211 /* Halt if there was a built in self test failure */
212 report_bist_failure(params->bist);
213
214 /* Perform some early chipset initialization required
215 * before RAM initialization can work
216 */
217 haswell_early_initialization(HASWELL_MOBILE);
218 printk(BIOS_DEBUG, "Back from haswell_early_initialization()\n");
219
220 if (wake_from_s3) {
221#if CONFIG_HAVE_ACPI_RESUME
222 printk(BIOS_DEBUG, "Resume from S3 detected.\n");
Aaron Durbina2671612013-02-06 21:41:01 -0600223#else
224 printk(BIOS_DEBUG, "Resume from S3 detected, but disabled.\n");
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600225 wake_from_s3 = 0;
Aaron Durbina2671612013-02-06 21:41:01 -0600226#endif
227 }
228
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600229 /* There are hard coded assumptions of 2 meaning s3 wake. Normalize
230 * the users of the 2 literal here based off wake_from_s3. */
231 boot_mode = wake_from_s3 ? 2 : 0;
232
Aaron Durbina2671612013-02-06 21:41:01 -0600233 /* Prepare USB controller early in S3 resume */
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600234 if (wake_from_s3)
Aaron Durbina2671612013-02-06 21:41:01 -0600235 enable_usb_bar();
236
237 post_code(0x3a);
238 params->pei_data->boot_mode = boot_mode;
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300239
240 timestamp_add_now(TS_BEFORE_INITRAM);
Aaron Durbina2671612013-02-06 21:41:01 -0600241
242 report_platform_info();
243
Aaron Durbinc7633f42013-06-13 17:29:36 -0700244 if (params->copy_spd != NULL)
245 params->copy_spd(params->pei_data);
246
Aaron Durbina2671612013-02-06 21:41:01 -0600247 sdram_initialize(params->pei_data);
248
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300249 timestamp_add_now(TS_AFTER_INITRAM);
250
Aaron Durbina2671612013-02-06 21:41:01 -0600251 post_code(0x3b);
252
253 intel_early_me_status();
254
255 quick_ram_check();
256 post_code(0x3e);
257
Aaron Durbinc0cbd6e2013-03-13 13:51:20 -0500258 if (!wake_from_s3) {
259 cbmem_initialize_empty();
Aaron Durbinbd74a4b2015-03-06 23:17:33 -0600260 stage_cache_create_empty();
Aaron Durbinc0cbd6e2013-03-13 13:51:20 -0500261 /* Save data returned from MRC on non-S3 resumes. */
Aaron Durbin2ad1dba2013-02-07 00:51:18 -0600262 save_mrc_data(params->pei_data);
Aaron Durbinbd74a4b2015-03-06 23:17:33 -0600263 } else {
264 stage_cache_recover();
265 if (cbmem_initialize()) {
266 #if CONFIG_HAVE_ACPI_RESUME
267 /* Failed S3 resume, reset to come up cleanly */
268 reset_system();
269 #endif
270 }
Aaron Durbina2671612013-02-06 21:41:01 -0600271 }
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600272
273 handoff = romstage_handoff_find_or_add();
274 if (handoff != NULL)
275 handoff->s3_resume = wake_from_s3;
276 else
277 printk(BIOS_DEBUG, "Romstage handoff structure not added!\n");
278
Aaron Durbina2671612013-02-06 21:41:01 -0600279 post_code(0x3f);
280#if CONFIG_CHROMEOS
281 init_chromeos(boot_mode);
282#endif
Aaron Durbina2671612013-02-06 21:41:01 -0600283 timestamp_add_now(TS_END_ROMSTAGE);
Aaron Durbina2671612013-02-06 21:41:01 -0600284}
Aaron Durbin7492ec12013-02-08 22:18:04 -0600285
Aaron Durbind02bb622013-03-01 17:40:49 -0600286static inline void prepare_for_resume(struct romstage_handoff *handoff)
Aaron Durbin7492ec12013-02-08 22:18:04 -0600287{
Aaron Durbine2d9e5b2013-02-08 17:38:35 -0600288/* Only need to save memory when ramstage isn't relocatable. */
289#if !CONFIG_RELOCATABLE_RAMSTAGE
Aaron Durbin7492ec12013-02-08 22:18:04 -0600290#if CONFIG_HAVE_ACPI_RESUME
291 /* Back up the OS-controlled memory where ramstage will be loaded. */
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600292 if (handoff != NULL && handoff->s3_resume) {
Aaron Durbin7492ec12013-02-08 22:18:04 -0600293 void *src = (void *)CONFIG_RAMBASE;
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600294 void *dest = cbmem_find(CBMEM_ID_RESUME);
295 if (dest != NULL)
296 memcpy(dest, src, HIGH_MEMORY_SAVE);
Aaron Durbin7492ec12013-02-08 22:18:04 -0600297 }
298#endif
Aaron Durbine2d9e5b2013-02-08 17:38:35 -0600299#endif
Aaron Durbin7492ec12013-02-08 22:18:04 -0600300}
301
302void romstage_after_car(void)
303{
Aaron Durbind02bb622013-03-01 17:40:49 -0600304 struct romstage_handoff *handoff;
305
306 handoff = romstage_handoff_find_or_add();
307
308 prepare_for_resume(handoff);
309
Aaron Durbin7492ec12013-02-08 22:18:04 -0600310 /* Load the ramstage. */
Stefan Reinauer648d1662013-05-06 18:05:39 -0700311 copy_and_run();
Aaron Durbin7492ec12013-02-08 22:18:04 -0600312}
Aaron Durbinf7cdfe52013-02-16 00:05:52 -0600313
314
Aaron Durbinbd74a4b2015-03-06 23:17:33 -0600315#if IS_ENABLED(CONFIG_CACHE_RELOCATED_RAMSTAGE_OUTSIDE_CBMEM)
Aaron Durbin75e29742013-10-10 20:37:04 -0500316
Aaron Durbinbd74a4b2015-03-06 23:17:33 -0600317void stage_cache_external_region(void **base, size_t *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;
Aaron Durbinbd74a4b2015-03-06 23:17:33 -0600322 *base = (void *)((uint32_t)cbmem_top() + RESERVED_SMM_OFFSET);
Aaron Durbinf7cdfe52013-02-16 00:05:52 -0600323}
324
Aaron Durbinbd74a4b2015-03-06 23:17:33 -0600325void ramstage_cache_invalid(void)
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