blob: 9c08aa16bab1e94a8d00ddd375022936dc980fdc [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.
Aaron Durbin3d0071b2013-01-18 14:32:50 -060014 */
15
16#include <stdint.h>
Aaron Durbin7492ec12013-02-08 22:18:04 -060017#include <string.h>
Aaron Durbinbd74a4b2015-03-06 23:17:33 -060018#include <cbfs.h>
Aaron Durbin3d0071b2013-01-18 14:32:50 -060019#include <console/console.h>
Aaron Durbina2671612013-02-06 21:41:01 -060020#include <arch/cpu.h>
21#include <cpu/x86/bist.h>
22#include <cpu/x86/msr.h>
Aaron Durbin38d94232013-02-07 00:03:33 -060023#include <cpu/x86/mtrr.h>
Patrick Georgibd79c5e2014-11-28 22:35:36 +010024#include <halt.h>
Aaron Durbina2671612013-02-06 21:41:01 -060025#include <lib.h>
26#include <timestamp.h>
27#include <arch/io.h>
Aaron Durbin7492ec12013-02-08 22:18:04 -060028#include <arch/stages.h>
Aaron Durbina2671612013-02-06 21:41:01 -060029#include <device/pci_def.h>
30#include <cpu/x86/lapic.h>
Kyösti Mälkki465eff62016-06-15 06:07:55 +030031#include <cbmem.h>
Aaron Durbinbf396ff2013-02-11 21:50:35 -060032#include <romstage_handoff.h>
Aaron Durbinb86113f2013-02-19 08:59:16 -060033#include <reset.h>
Aaron Durbina2671612013-02-06 21:41:01 -060034#include <vendorcode/google/chromeos/chromeos.h>
Duncan Laurie7cced0d2013-06-04 10:03:34 -070035#if CONFIG_EC_GOOGLE_CHROMEEC
36#include <ec/google/chromeec/ec.h>
37#endif
Aaron Durbina2671612013-02-06 21:41:01 -060038#include "haswell.h"
39#include "northbridge/intel/haswell/haswell.h"
40#include "northbridge/intel/haswell/raminit.h"
41#include "southbridge/intel/lynxpoint/pch.h"
42#include "southbridge/intel/lynxpoint/me.h"
Vladimir Serbinenko0e90dae2015-05-18 10:29:06 +020043#include <tpm.h>
Aaron Durbina2671612013-02-06 21:41:01 -060044
Aaron Durbinb86113f2013-02-19 08:59:16 -060045static inline void reset_system(void)
46{
47 hard_reset();
Patrick Georgibd79c5e2014-11-28 22:35:36 +010048 halt();
Aaron Durbinb86113f2013-02-19 08:59:16 -060049}
50
Aaron Durbin38d94232013-02-07 00:03:33 -060051/* The cache-as-ram assembly file calls romstage_main() after setting up
52 * cache-as-ram. romstage_main() will then call the mainboards's
53 * mainboard_romstage_entry() function. That function then calls
54 * romstage_common() below. The reason for the back and forth is to provide
55 * common entry point from cache-as-ram while still allowing for code sharing.
56 * Because we can't use global variables the stack is used for allocations --
57 * thus the need to call back and forth. */
Aaron Durbin3d0071b2013-01-18 14:32:50 -060058
Aaron Durbin38d94232013-02-07 00:03:33 -060059
60static inline u32 *stack_push(u32 *stack, u32 value)
61{
62 stack = &stack[-1];
63 *stack = value;
64 return stack;
65}
66
Aaron Durbinc0cbd6e2013-03-13 13:51:20 -050067/* Romstage needs quite a bit of stack for decompressing images since the lzma
68 * lib keeps its state on the stack during romstage. */
69#define ROMSTAGE_RAM_STACK_SIZE 0x5000
Aaron Durbine2d9e5b2013-02-08 17:38:35 -060070static unsigned long choose_top_of_stack(void)
71{
72 unsigned long stack_top;
Kyösti Mälkkiae98e832014-11-28 11:24:19 +020073
Aaron Durbinc0cbd6e2013-03-13 13:51:20 -050074 /* cbmem_add() does a find() before add(). */
75 stack_top = (unsigned long)cbmem_add(CBMEM_ID_ROMSTAGE_RAM_STACK,
76 ROMSTAGE_RAM_STACK_SIZE);
77 stack_top += ROMSTAGE_RAM_STACK_SIZE;
Aaron Durbine2d9e5b2013-02-08 17:38:35 -060078 return stack_top;
79}
80
Aaron Durbin38d94232013-02-07 00:03:33 -060081/* setup_romstage_stack_after_car() determines the stack to use after
82 * cache-as-ram is torn down as well as the MTRR settings to use. */
83static void *setup_romstage_stack_after_car(void)
84{
85 unsigned long top_of_stack;
86 int num_mtrrs;
87 u32 *slot;
88 u32 mtrr_mask_upper;
Aaron Durbin67481ddc2013-02-15 15:08:37 -060089 u32 top_of_ram;
Aaron Durbin38d94232013-02-07 00:03:33 -060090
91 /* Top of stack needs to be aligned to a 4-byte boundary. */
Aaron Durbine2d9e5b2013-02-08 17:38:35 -060092 top_of_stack = choose_top_of_stack() & ~3;
Aaron Durbin38d94232013-02-07 00:03:33 -060093 slot = (void *)top_of_stack;
94 num_mtrrs = 0;
95
96 /* The upper bits of the MTRR mask need to set according to the number
97 * of physical address bits. */
98 mtrr_mask_upper = (1 << ((cpuid_eax(0x80000008) & 0xff) - 32)) - 1;
99
Paul Menzel4fe98132014-01-25 15:55:28 +0100100 /* The order for each MTRR is value then base with upper 32-bits of
Aaron Durbin38d94232013-02-07 00:03:33 -0600101 * each value coming before the lower 32-bits. The reasoning for
102 * this ordering is to create a stack layout like the following:
103 * +0: Number of MTRRs
Paul Menzel4fe98132014-01-25 15:55:28 +0100104 * +4: MTRR base 0 31:0
105 * +8: MTRR base 0 63:32
106 * +12: MTRR mask 0 31:0
107 * +16: MTRR mask 0 63:32
108 * +20: MTRR base 1 31:0
109 * +24: MTRR base 1 63:32
110 * +28: MTRR mask 1 31:0
111 * +32: MTRR mask 1 63:32
Aaron Durbin38d94232013-02-07 00:03:33 -0600112 */
113
114 /* Cache the ROM as WP just below 4GiB. */
115 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700116 slot = stack_push(slot, ~(CACHE_ROM_SIZE - 1) | MTRR_PHYS_MASK_VALID);
Aaron Durbin38d94232013-02-07 00:03:33 -0600117 slot = stack_push(slot, 0); /* upper base */
Kyösti Mälkki107f72e2014-01-06 11:06:26 +0200118 slot = stack_push(slot, ~(CACHE_ROM_SIZE - 1) | MTRR_TYPE_WRPROT);
Aaron Durbin38d94232013-02-07 00:03:33 -0600119 num_mtrrs++;
120
121 /* Cache RAM as WB from 0 -> CONFIG_RAMTOP. */
122 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700123 slot = stack_push(slot, ~(CONFIG_RAMTOP - 1) | MTRR_PHYS_MASK_VALID);
Aaron Durbin38d94232013-02-07 00:03:33 -0600124 slot = stack_push(slot, 0); /* upper base */
125 slot = stack_push(slot, 0 | MTRR_TYPE_WRBACK);
126 num_mtrrs++;
127
Kyösti Mälkkif1e3c762014-12-22 12:28:07 +0200128 top_of_ram = (uint32_t)cbmem_top();
Aaron Durbin38d94232013-02-07 00:03:33 -0600129 /* Cache 8MiB below the top of ram. On haswell systems the top of
130 * ram under 4GiB is the start of the TSEG region. It is required to
131 * be 8MiB aligned. Set this area as cacheable so it can be used later
132 * for ramstage before setting up the entire RAM as cacheable. */
133 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700134 slot = stack_push(slot, ~((8 << 20) - 1) | MTRR_PHYS_MASK_VALID);
Aaron Durbin38d94232013-02-07 00:03:33 -0600135 slot = stack_push(slot, 0); /* upper base */
Aaron Durbin67481ddc2013-02-15 15:08:37 -0600136 slot = stack_push(slot, (top_of_ram - (8 << 20)) | MTRR_TYPE_WRBACK);
137 num_mtrrs++;
138
139 /* Cache 8MiB at the top of ram. Top of ram on haswell systems
140 * is where the TSEG region resides. However, it is not restricted
141 * to SMM mode until SMM has been relocated. By setting the region
142 * to cacheable it provides faster access when relocating the SMM
143 * handler as well as using the TSEG region for other purposes. */
144 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700145 slot = stack_push(slot, ~((8 << 20) - 1) | MTRR_PHYS_MASK_VALID);
Aaron Durbin67481ddc2013-02-15 15:08:37 -0600146 slot = stack_push(slot, 0); /* upper base */
147 slot = stack_push(slot, top_of_ram | MTRR_TYPE_WRBACK);
Aaron Durbin38d94232013-02-07 00:03:33 -0600148 num_mtrrs++;
149
Paul Menzel4fe98132014-01-25 15:55:28 +0100150 /* Save the number of MTRRs to setup. Return the stack location
Aaron Durbin38d94232013-02-07 00:03:33 -0600151 * pointing to the number of MTRRs. */
152 slot = stack_push(slot, num_mtrrs);
153
154 return slot;
155}
156
Aaron Durbin39ecc652013-05-02 09:42:13 -0500157void * asmlinkage romstage_main(unsigned long bist)
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600158{
159 int i;
Aaron Durbin38d94232013-02-07 00:03:33 -0600160 void *romstage_stack_after_car;
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600161 const int num_guards = 4;
162 const u32 stack_guard = 0xdeadbeef;
163 u32 *stack_base = (void *)(CONFIG_DCACHE_RAM_BASE +
164 CONFIG_DCACHE_RAM_SIZE -
165 CONFIG_DCACHE_RAM_ROMSTAGE_STACK_SIZE);
166
167 printk(BIOS_DEBUG, "Setting up stack guards.\n");
168 for (i = 0; i < num_guards; i++)
169 stack_base[i] = stack_guard;
170
Aaron Durbina2671612013-02-06 21:41:01 -0600171 mainboard_romstage_entry(bist);
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600172
173 /* Check the stack. */
174 for (i = 0; i < num_guards; i++) {
175 if (stack_base[i] == stack_guard)
176 continue;
177 printk(BIOS_DEBUG, "Smashed stack detected in romstage!\n");
178 }
179
Aaron Durbin38d94232013-02-07 00:03:33 -0600180 /* Get the stack to use after cache-as-ram is torn down. */
181 romstage_stack_after_car = setup_romstage_stack_after_car();
182
Aaron Durbin38d94232013-02-07 00:03:33 -0600183 return romstage_stack_after_car;
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600184}
Aaron Durbina2671612013-02-06 21:41:01 -0600185
186void romstage_common(const struct romstage_params *params)
187{
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600188 int boot_mode;
Aaron Durbina2671612013-02-06 21:41:01 -0600189 int wake_from_s3;
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600190 struct romstage_handoff *handoff;
Aaron Durbina2671612013-02-06 21:41:01 -0600191
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300192 timestamp_init(get_initial_timestamp());
193 timestamp_add_now(TS_START_ROMSTAGE);
Aaron Durbina2671612013-02-06 21:41:01 -0600194
195 if (params->bist == 0)
196 enable_lapic();
197
198 wake_from_s3 = early_pch_init(params->gpio_map, params->rcba_config);
199
Duncan Laurie7cced0d2013-06-04 10:03:34 -0700200#if CONFIG_EC_GOOGLE_CHROMEEC
201 /* Ensure the EC is in the right mode for recovery */
202 google_chromeec_early_init();
203#endif
204
Aaron Durbina2671612013-02-06 21:41:01 -0600205 /* Halt if there was a built in self test failure */
206 report_bist_failure(params->bist);
207
208 /* Perform some early chipset initialization required
209 * before RAM initialization can work
210 */
211 haswell_early_initialization(HASWELL_MOBILE);
212 printk(BIOS_DEBUG, "Back from haswell_early_initialization()\n");
213
214 if (wake_from_s3) {
215#if CONFIG_HAVE_ACPI_RESUME
216 printk(BIOS_DEBUG, "Resume from S3 detected.\n");
Aaron Durbina2671612013-02-06 21:41:01 -0600217#else
218 printk(BIOS_DEBUG, "Resume from S3 detected, but disabled.\n");
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600219 wake_from_s3 = 0;
Aaron Durbina2671612013-02-06 21:41:01 -0600220#endif
221 }
222
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600223 /* There are hard coded assumptions of 2 meaning s3 wake. Normalize
224 * the users of the 2 literal here based off wake_from_s3. */
225 boot_mode = wake_from_s3 ? 2 : 0;
226
Aaron Durbina2671612013-02-06 21:41:01 -0600227 /* Prepare USB controller early in S3 resume */
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600228 if (wake_from_s3)
Aaron Durbina2671612013-02-06 21:41:01 -0600229 enable_usb_bar();
230
231 post_code(0x3a);
232 params->pei_data->boot_mode = boot_mode;
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300233
234 timestamp_add_now(TS_BEFORE_INITRAM);
Aaron Durbina2671612013-02-06 21:41:01 -0600235
236 report_platform_info();
237
Aaron Durbinc7633f42013-06-13 17:29:36 -0700238 if (params->copy_spd != NULL)
239 params->copy_spd(params->pei_data);
240
Aaron Durbina2671612013-02-06 21:41:01 -0600241 sdram_initialize(params->pei_data);
242
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300243 timestamp_add_now(TS_AFTER_INITRAM);
244
Aaron Durbina2671612013-02-06 21:41:01 -0600245 post_code(0x3b);
246
247 intel_early_me_status();
248
249 quick_ram_check();
250 post_code(0x3e);
251
Aaron Durbinc0cbd6e2013-03-13 13:51:20 -0500252 if (!wake_from_s3) {
253 cbmem_initialize_empty();
254 /* Save data returned from MRC on non-S3 resumes. */
Aaron Durbin2ad1dba2013-02-07 00:51:18 -0600255 save_mrc_data(params->pei_data);
Aaron Durbin42e68562015-06-09 13:55:51 -0500256 } else if (cbmem_initialize()) {
257 #if CONFIG_HAVE_ACPI_RESUME
258 /* Failed S3 resume, reset to come up cleanly */
259 reset_system();
260 #endif
Aaron Durbina2671612013-02-06 21:41:01 -0600261 }
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600262
263 handoff = romstage_handoff_find_or_add();
264 if (handoff != NULL)
265 handoff->s3_resume = wake_from_s3;
266 else
267 printk(BIOS_DEBUG, "Romstage handoff structure not added!\n");
268
Aaron Durbina2671612013-02-06 21:41:01 -0600269 post_code(0x3f);
Denis 'GNUtoo' Carikli0e92bb02016-02-20 17:32:03 +0100270 if (IS_ENABLED(CONFIG_LPC_TPM)) {
Vladimir Serbinenko0e90dae2015-05-18 10:29:06 +0200271 init_tpm(wake_from_s3);
272 }
Aaron Durbina2671612013-02-06 21:41:01 -0600273}
Aaron Durbin7492ec12013-02-08 22:18:04 -0600274
Aaron Durbind02bb622013-03-01 17:40:49 -0600275static inline void prepare_for_resume(struct romstage_handoff *handoff)
Aaron Durbin7492ec12013-02-08 22:18:04 -0600276{
Aaron Durbine2d9e5b2013-02-08 17:38:35 -0600277/* Only need to save memory when ramstage isn't relocatable. */
278#if !CONFIG_RELOCATABLE_RAMSTAGE
Aaron Durbin7492ec12013-02-08 22:18:04 -0600279#if CONFIG_HAVE_ACPI_RESUME
280 /* Back up the OS-controlled memory where ramstage will be loaded. */
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600281 if (handoff != NULL && handoff->s3_resume) {
Aaron Durbin7492ec12013-02-08 22:18:04 -0600282 void *src = (void *)CONFIG_RAMBASE;
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600283 void *dest = cbmem_find(CBMEM_ID_RESUME);
284 if (dest != NULL)
285 memcpy(dest, src, HIGH_MEMORY_SAVE);
Aaron Durbin7492ec12013-02-08 22:18:04 -0600286 }
287#endif
Aaron Durbine2d9e5b2013-02-08 17:38:35 -0600288#endif
Aaron Durbin7492ec12013-02-08 22:18:04 -0600289}
290
291void romstage_after_car(void)
292{
Aaron Durbind02bb622013-03-01 17:40:49 -0600293 struct romstage_handoff *handoff;
294
295 handoff = romstage_handoff_find_or_add();
296
297 prepare_for_resume(handoff);
298
Aaron Durbin7492ec12013-02-08 22:18:04 -0600299 /* Load the ramstage. */
Stefan Reinauer648d1662013-05-06 18:05:39 -0700300 copy_and_run();
Aaron Durbin7492ec12013-02-08 22:18:04 -0600301}
Aaron Durbinf7cdfe52013-02-16 00:05:52 -0600302
303
Aaron Durbinbd74a4b2015-03-06 23:17:33 -0600304#if IS_ENABLED(CONFIG_CACHE_RELOCATED_RAMSTAGE_OUTSIDE_CBMEM)
Aaron Durbinbd74a4b2015-03-06 23:17:33 -0600305void ramstage_cache_invalid(void)
Aaron Durbinf7cdfe52013-02-16 00:05:52 -0600306{
Aaron Durbin75e29742013-10-10 20:37:04 -0500307#if CONFIG_RESET_ON_INVALID_RAMSTAGE_CACHE
308 reset_system();
309#endif
Aaron Durbinf7cdfe52013-02-16 00:05:52 -0600310}
311#endif