blob: 6164de54ed4e94134efb37ab16a2e973568cc207 [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>
35#include <cbmem.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 Durbina2671612013-02-06 21:41:01 -060039#if CONFIG_CHROMEOS
40#include <vendorcode/google/chromeos/chromeos.h>
41#endif
42#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 Durbine2d9e5b2013-02-08 17:38:35 -060073static unsigned long choose_top_of_stack(void)
74{
75 unsigned long stack_top;
76#if CONFIG_RELOCATABLE_RAMSTAGE
77 stack_top = (unsigned long)cbmem_add(CBMEM_ID_RESUME_SCRATCH,
78 CONFIG_HIGH_SCRATCH_MEMORY_SIZE);
79 stack_top += CONFIG_HIGH_SCRATCH_MEMORY_SIZE;
80#else
81 stack_top = ROMSTAGE_STACK;
82#endif
83 return stack_top;
84}
85
Aaron Durbin38d94232013-02-07 00:03:33 -060086/* setup_romstage_stack_after_car() determines the stack to use after
87 * cache-as-ram is torn down as well as the MTRR settings to use. */
88static void *setup_romstage_stack_after_car(void)
89{
90 unsigned long top_of_stack;
91 int num_mtrrs;
92 u32 *slot;
93 u32 mtrr_mask_upper;
Aaron Durbin67481ddc2013-02-15 15:08:37 -060094 u32 top_of_ram;
Aaron Durbin38d94232013-02-07 00:03:33 -060095
96 /* Top of stack needs to be aligned to a 4-byte boundary. */
Aaron Durbine2d9e5b2013-02-08 17:38:35 -060097 top_of_stack = choose_top_of_stack() & ~3;
Aaron Durbin38d94232013-02-07 00:03:33 -060098 slot = (void *)top_of_stack;
99 num_mtrrs = 0;
100
101 /* The upper bits of the MTRR mask need to set according to the number
102 * of physical address bits. */
103 mtrr_mask_upper = (1 << ((cpuid_eax(0x80000008) & 0xff) - 32)) - 1;
104
105 /* The order for each MTTR is value then base with upper 32-bits of
106 * each value coming before the lower 32-bits. The reasoning for
107 * this ordering is to create a stack layout like the following:
108 * +0: Number of MTRRs
109 * +4: MTTR base 0 31:0
110 * +8: MTTR base 0 63:32
111 * +12: MTTR mask 0 31:0
112 * +16: MTTR mask 0 63:32
113 * +20: MTTR base 1 31:0
114 * +24: MTTR base 1 63:32
115 * +28: MTTR mask 1 31:0
116 * +32: MTTR mask 1 63:32
117 */
118
119 /* Cache the ROM as WP just below 4GiB. */
120 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
121 slot = stack_push(slot, ~(CONFIG_ROM_SIZE - 1) | MTRRphysMaskValid);
122 slot = stack_push(slot, 0); /* upper base */
123 slot = stack_push(slot, ~(CONFIG_ROM_SIZE - 1) | MTRR_TYPE_WRPROT);
124 num_mtrrs++;
125
126 /* Cache RAM as WB from 0 -> CONFIG_RAMTOP. */
127 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
128 slot = stack_push(slot, ~(CONFIG_RAMTOP - 1) | MTRRphysMaskValid);
129 slot = stack_push(slot, 0); /* upper base */
130 slot = stack_push(slot, 0 | MTRR_TYPE_WRBACK);
131 num_mtrrs++;
132
Aaron Durbin67481ddc2013-02-15 15:08:37 -0600133 top_of_ram = get_top_of_ram();
Aaron Durbin38d94232013-02-07 00:03:33 -0600134 /* Cache 8MiB below the top of ram. On haswell systems the top of
135 * ram under 4GiB is the start of the TSEG region. It is required to
136 * be 8MiB aligned. Set this area as cacheable so it can be used later
137 * for ramstage before setting up the entire RAM as cacheable. */
138 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
139 slot = stack_push(slot, ~((8 << 20) - 1) | MTRRphysMaskValid);
140 slot = stack_push(slot, 0); /* upper base */
Aaron Durbin67481ddc2013-02-15 15:08:37 -0600141 slot = stack_push(slot, (top_of_ram - (8 << 20)) | MTRR_TYPE_WRBACK);
142 num_mtrrs++;
143
144 /* Cache 8MiB at the top of ram. Top of ram on haswell systems
145 * is where the TSEG region resides. However, it is not restricted
146 * to SMM mode until SMM has been relocated. By setting the region
147 * to cacheable it provides faster access when relocating the SMM
148 * handler as well as using the TSEG region for other purposes. */
149 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
150 slot = stack_push(slot, ~((8 << 20) - 1) | MTRRphysMaskValid);
151 slot = stack_push(slot, 0); /* upper base */
152 slot = stack_push(slot, top_of_ram | MTRR_TYPE_WRBACK);
Aaron Durbin38d94232013-02-07 00:03:33 -0600153 num_mtrrs++;
154
155 /* Save the number of MTTRs to setup. Return the stack location
156 * pointing to the number of MTRRs. */
157 slot = stack_push(slot, num_mtrrs);
158
159 return slot;
160}
161
162void * __attribute__((regparm(0))) romstage_main(unsigned long bist)
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600163{
164 int i;
Aaron Durbin38d94232013-02-07 00:03:33 -0600165 void *romstage_stack_after_car;
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600166 const int num_guards = 4;
167 const u32 stack_guard = 0xdeadbeef;
168 u32 *stack_base = (void *)(CONFIG_DCACHE_RAM_BASE +
169 CONFIG_DCACHE_RAM_SIZE -
170 CONFIG_DCACHE_RAM_ROMSTAGE_STACK_SIZE);
171
172 printk(BIOS_DEBUG, "Setting up stack guards.\n");
173 for (i = 0; i < num_guards; i++)
174 stack_base[i] = stack_guard;
175
Aaron Durbina2671612013-02-06 21:41:01 -0600176 mainboard_romstage_entry(bist);
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600177
178 /* Check the stack. */
179 for (i = 0; i < num_guards; i++) {
180 if (stack_base[i] == stack_guard)
181 continue;
182 printk(BIOS_DEBUG, "Smashed stack detected in romstage!\n");
183 }
184
Aaron Durbin38d94232013-02-07 00:03:33 -0600185 /* Get the stack to use after cache-as-ram is torn down. */
186 romstage_stack_after_car = setup_romstage_stack_after_car();
187
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600188#if CONFIG_CONSOLE_CBMEM
189 /* Keep this the last thing this function does. */
190 cbmemc_reinit();
191#endif
Aaron Durbin38d94232013-02-07 00:03:33 -0600192
193 return romstage_stack_after_car;
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600194}
Aaron Durbina2671612013-02-06 21:41:01 -0600195
196void romstage_common(const struct romstage_params *params)
197{
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600198 int boot_mode;
Aaron Durbina2671612013-02-06 21:41:01 -0600199 int wake_from_s3;
200 int cbmem_was_initted;
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600201 struct romstage_handoff *handoff;
Aaron Durbina2671612013-02-06 21:41:01 -0600202
203#if CONFIG_COLLECT_TIMESTAMPS
204 tsc_t start_romstage_time;
205 tsc_t before_dram_time;
206 tsc_t after_dram_time;
207 tsc_t base_time = {
208 .lo = pci_read_config32(PCI_DEV(0, 0x00, 0), 0xdc),
209 .hi = pci_read_config32(PCI_DEV(0, 0x1f, 2), 0xd0)
210 };
211#endif
212
213#if CONFIG_COLLECT_TIMESTAMPS
214 start_romstage_time = rdtsc();
215#endif
216
217 if (params->bist == 0)
218 enable_lapic();
219
220 wake_from_s3 = early_pch_init(params->gpio_map, params->rcba_config);
221
222 /* Halt if there was a built in self test failure */
223 report_bist_failure(params->bist);
224
225 /* Perform some early chipset initialization required
226 * before RAM initialization can work
227 */
228 haswell_early_initialization(HASWELL_MOBILE);
229 printk(BIOS_DEBUG, "Back from haswell_early_initialization()\n");
230
231 if (wake_from_s3) {
232#if CONFIG_HAVE_ACPI_RESUME
233 printk(BIOS_DEBUG, "Resume from S3 detected.\n");
Aaron Durbina2671612013-02-06 21:41:01 -0600234#else
235 printk(BIOS_DEBUG, "Resume from S3 detected, but disabled.\n");
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600236 wake_from_s3 = 0;
Aaron Durbina2671612013-02-06 21:41:01 -0600237#endif
238 }
239
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600240 /* There are hard coded assumptions of 2 meaning s3 wake. Normalize
241 * the users of the 2 literal here based off wake_from_s3. */
242 boot_mode = wake_from_s3 ? 2 : 0;
243
Aaron Durbina2671612013-02-06 21:41:01 -0600244 /* Prepare USB controller early in S3 resume */
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600245 if (wake_from_s3)
Aaron Durbina2671612013-02-06 21:41:01 -0600246 enable_usb_bar();
247
248 post_code(0x3a);
249 params->pei_data->boot_mode = boot_mode;
250#if CONFIG_COLLECT_TIMESTAMPS
251 before_dram_time = rdtsc();
252#endif
253
254 report_platform_info();
255
256 sdram_initialize(params->pei_data);
257
258#if CONFIG_COLLECT_TIMESTAMPS
259 after_dram_time = rdtsc();
260#endif
261 post_code(0x3b);
262
263 intel_early_me_status();
264
265 quick_ram_check();
266 post_code(0x3e);
267
268#if CONFIG_EARLY_CBMEM_INIT
269 cbmem_was_initted = !cbmem_initialize();
270#else
271 cbmem_was_initted = cbmem_reinit((uint64_t) (get_top_of_ram()
272 - HIGH_MEMORY_SIZE));
273#endif
274
Aaron Durbin2ad1dba2013-02-07 00:51:18 -0600275 /* Save data returned from MRC on non-S3 resumes. */
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600276 if (!wake_from_s3)
Aaron Durbin2ad1dba2013-02-07 00:51:18 -0600277 save_mrc_data(params->pei_data);
278
Aaron Durbina2671612013-02-06 21:41:01 -0600279#if CONFIG_HAVE_ACPI_RESUME
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600280 if (wake_from_s3 && !cbmem_was_initted) {
Aaron Durbina2671612013-02-06 21:41:01 -0600281 /* Failed S3 resume, reset to come up cleanly */
Aaron Durbinb86113f2013-02-19 08:59:16 -0600282 reset_system();
Aaron Durbina2671612013-02-06 21:41:01 -0600283 }
284#endif
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600285
286 handoff = romstage_handoff_find_or_add();
287 if (handoff != NULL)
288 handoff->s3_resume = wake_from_s3;
289 else
290 printk(BIOS_DEBUG, "Romstage handoff structure not added!\n");
291
Aaron Durbina2671612013-02-06 21:41:01 -0600292 post_code(0x3f);
293#if CONFIG_CHROMEOS
294 init_chromeos(boot_mode);
295#endif
296#if CONFIG_COLLECT_TIMESTAMPS
297 timestamp_init(base_time);
298 timestamp_add(TS_START_ROMSTAGE, start_romstage_time );
299 timestamp_add(TS_BEFORE_INITRAM, before_dram_time );
300 timestamp_add(TS_AFTER_INITRAM, after_dram_time );
301 timestamp_add_now(TS_END_ROMSTAGE);
302#endif
303}
Aaron Durbin7492ec12013-02-08 22:18:04 -0600304
305static inline void prepare_for_resume(void)
306{
Aaron Durbine2d9e5b2013-02-08 17:38:35 -0600307/* Only need to save memory when ramstage isn't relocatable. */
308#if !CONFIG_RELOCATABLE_RAMSTAGE
Aaron Durbin7492ec12013-02-08 22:18:04 -0600309#if CONFIG_HAVE_ACPI_RESUME
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600310 struct romstage_handoff *handoff = romstage_handoff_find_or_add();
311
Aaron Durbin7492ec12013-02-08 22:18:04 -0600312 /* Back up the OS-controlled memory where ramstage will be loaded. */
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600313 if (handoff != NULL && handoff->s3_resume) {
Aaron Durbin7492ec12013-02-08 22:18:04 -0600314 void *src = (void *)CONFIG_RAMBASE;
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600315 void *dest = cbmem_find(CBMEM_ID_RESUME);
316 if (dest != NULL)
317 memcpy(dest, src, HIGH_MEMORY_SAVE);
Aaron Durbin7492ec12013-02-08 22:18:04 -0600318 }
319#endif
Aaron Durbine2d9e5b2013-02-08 17:38:35 -0600320#endif
Aaron Durbin7492ec12013-02-08 22:18:04 -0600321}
322
323void romstage_after_car(void)
324{
325 prepare_for_resume();
326 /* Load the ramstage. */
327 copy_and_run(0);
328}
Aaron Durbinf7cdfe52013-02-16 00:05:52 -0600329
330
331#if CONFIG_RELOCATABLE_RAMSTAGE
332void cache_loaded_ramstage(struct romstage_handoff *handoff,
333 void *ramstage_base, uint32_t ramstage_size,
334 void *entry_point)
335{
336 struct ramstage_cache *cache;
337 uint32_t total_size;
338
339 /* The ramstage cache lives in the TSEG region at RESERVED_SMM_OFFSET.
340 * The top of ram is defined to be the TSEG base address. */
341 cache = (void *)(get_top_of_ram() + RESERVED_SMM_OFFSET);
342 total_size = sizeof(*cache) + ramstage_size;
343 if (total_size > RESERVED_SMM_SIZE) {
344 printk(BIOS_DEBUG, "0x%08x > RESERVED_SMM_SIZE (0x%08x)\n",
345 total_size, RESERVED_SMM_SIZE);
346 /* Nuke whatever may be there now just in case. */
347 cache->magic = ~RAMSTAGE_CACHE_MAGIC;
348 return;
349 }
350
351 cache->magic = RAMSTAGE_CACHE_MAGIC;
352 cache->entry_point = (uint32_t)entry_point;
353 cache->load_address = (uint32_t)ramstage_base;
354 cache->size = ramstage_size;
355
356 printk(BIOS_DEBUG, "Saving ramstage to SMM space cache.\n");
357
358 /* Copy over the program. */
359 memcpy(&cache->program[0], ramstage_base, ramstage_size);
360
361 /* Do not update reserve region if the handoff structure is not
362 * available. Perhaps the ramstage will fix things up for the resume
363 * path. */
364 if (handoff == NULL)
365 return;
366
367 /* Update entry and reserve region. */
368 handoff->reserve_base = (uint32_t)ramstage_base;
369 handoff->reserve_size = ramstage_size;
370 handoff->ramstage_entry_point = (uint32_t)entry_point;
371}
372
373void *load_cached_ramstage(struct romstage_handoff *handoff)
374{
375 struct ramstage_cache *cache;
376
377 /* The ramstage cache lives in the TSEG region at RESERVED_SMM_OFFSET.
378 * The top of ram is defined to be the TSEG base address. */
379 cache = (void *)(get_top_of_ram() + RESERVED_SMM_OFFSET);
380
381 if (cache->magic != RAMSTAGE_CACHE_MAGIC) {
382 printk(BIOS_DEBUG, "Invalid ramstage cache found.\n");
Aaron Durbinb86113f2013-02-19 08:59:16 -0600383 #if CONFIG_RESET_ON_INVALID_RAMSTAGE_CACHE
384 reset_system();
385 #endif
Aaron Durbinf7cdfe52013-02-16 00:05:52 -0600386 return NULL;
387 }
388
389 printk(BIOS_DEBUG, "Loading ramstage from SMM space cache.\n");
390
391 memcpy((void *)cache->load_address, &cache->program[0], cache->size);
392
393 return (void *)cache->entry_point;
394}
395#endif