blob: 4ece6c2f9cdb43207807cc7ded2c9ce44ece141a [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 <arch/romcc_io.h>
34#include <device/pci_def.h>
35#include <cpu/x86/lapic.h>
36#include <cbmem.h>
Aaron Durbinf7cdfe52013-02-16 00:05:52 -060037#include <cbfs.h>
Aaron Durbinbf396ff2013-02-11 21:50:35 -060038#include <romstage_handoff.h>
Aaron Durbinb86113f2013-02-19 08:59:16 -060039#include <reset.h>
Aaron Durbina2671612013-02-06 21:41:01 -060040#if CONFIG_CHROMEOS
41#include <vendorcode/google/chromeos/chromeos.h>
42#endif
43#include "haswell.h"
44#include "northbridge/intel/haswell/haswell.h"
45#include "northbridge/intel/haswell/raminit.h"
46#include "southbridge/intel/lynxpoint/pch.h"
47#include "southbridge/intel/lynxpoint/me.h"
Aaron Durbin3d0071b2013-01-18 14:32:50 -060048
Aaron Durbina2671612013-02-06 21:41:01 -060049
Aaron Durbinb86113f2013-02-19 08:59:16 -060050static inline void reset_system(void)
51{
52 hard_reset();
53 while (1) {
54 hlt();
55 }
56}
57
Aaron Durbin38d94232013-02-07 00:03:33 -060058/* The cache-as-ram assembly file calls romstage_main() after setting up
59 * cache-as-ram. romstage_main() will then call the mainboards's
60 * mainboard_romstage_entry() function. That function then calls
61 * romstage_common() below. The reason for the back and forth is to provide
62 * common entry point from cache-as-ram while still allowing for code sharing.
63 * Because we can't use global variables the stack is used for allocations --
64 * thus the need to call back and forth. */
Aaron Durbin3d0071b2013-01-18 14:32:50 -060065
Aaron Durbin38d94232013-02-07 00:03:33 -060066
67static inline u32 *stack_push(u32 *stack, u32 value)
68{
69 stack = &stack[-1];
70 *stack = value;
71 return stack;
72}
73
Aaron Durbine2d9e5b2013-02-08 17:38:35 -060074static unsigned long choose_top_of_stack(void)
75{
76 unsigned long stack_top;
77#if CONFIG_RELOCATABLE_RAMSTAGE
78 stack_top = (unsigned long)cbmem_add(CBMEM_ID_RESUME_SCRATCH,
79 CONFIG_HIGH_SCRATCH_MEMORY_SIZE);
80 stack_top += CONFIG_HIGH_SCRATCH_MEMORY_SIZE;
81#else
82 stack_top = ROMSTAGE_STACK;
83#endif
84 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
106 /* The order for each MTTR is value then base with upper 32-bits of
107 * 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
110 * +4: MTTR base 0 31:0
111 * +8: MTTR base 0 63:32
112 * +12: MTTR mask 0 31:0
113 * +16: MTTR mask 0 63:32
114 * +20: MTTR base 1 31:0
115 * +24: MTTR base 1 63:32
116 * +28: MTTR mask 1 31:0
117 * +32: MTTR mask 1 63:32
118 */
119
120 /* Cache the ROM as WP just below 4GiB. */
121 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
122 slot = stack_push(slot, ~(CONFIG_ROM_SIZE - 1) | MTRRphysMaskValid);
123 slot = stack_push(slot, 0); /* upper base */
124 slot = stack_push(slot, ~(CONFIG_ROM_SIZE - 1) | MTRR_TYPE_WRPROT);
125 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
Aaron Durbin67481ddc2013-02-15 15:08:37 -0600134 top_of_ram = get_top_of_ram();
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
156 /* Save the number of MTTRs to setup. Return the stack location
157 * pointing to the number of MTRRs. */
158 slot = stack_push(slot, num_mtrrs);
159
160 return slot;
161}
162
163void * __attribute__((regparm(0))) 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 Durbin3d0071b2013-01-18 14:32:50 -0600189#if CONFIG_CONSOLE_CBMEM
190 /* Keep this the last thing this function does. */
191 cbmemc_reinit();
192#endif
Aaron Durbin38d94232013-02-07 00:03:33 -0600193
194 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;
201 int cbmem_was_initted;
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600202 struct romstage_handoff *handoff;
Aaron Durbina2671612013-02-06 21:41:01 -0600203
204#if CONFIG_COLLECT_TIMESTAMPS
205 tsc_t start_romstage_time;
206 tsc_t before_dram_time;
207 tsc_t after_dram_time;
208 tsc_t base_time = {
209 .lo = pci_read_config32(PCI_DEV(0, 0x00, 0), 0xdc),
210 .hi = pci_read_config32(PCI_DEV(0, 0x1f, 2), 0xd0)
211 };
212#endif
213
214#if CONFIG_COLLECT_TIMESTAMPS
215 start_romstage_time = rdtsc();
216#endif
217
218 if (params->bist == 0)
219 enable_lapic();
220
221 wake_from_s3 = early_pch_init(params->gpio_map, params->rcba_config);
222
223 /* Halt if there was a built in self test failure */
224 report_bist_failure(params->bist);
225
226 /* Perform some early chipset initialization required
227 * before RAM initialization can work
228 */
229 haswell_early_initialization(HASWELL_MOBILE);
230 printk(BIOS_DEBUG, "Back from haswell_early_initialization()\n");
231
232 if (wake_from_s3) {
233#if CONFIG_HAVE_ACPI_RESUME
234 printk(BIOS_DEBUG, "Resume from S3 detected.\n");
Aaron Durbina2671612013-02-06 21:41:01 -0600235#else
236 printk(BIOS_DEBUG, "Resume from S3 detected, but disabled.\n");
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600237 wake_from_s3 = 0;
Aaron Durbina2671612013-02-06 21:41:01 -0600238#endif
239 }
240
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600241 /* There are hard coded assumptions of 2 meaning s3 wake. Normalize
242 * the users of the 2 literal here based off wake_from_s3. */
243 boot_mode = wake_from_s3 ? 2 : 0;
244
Aaron Durbina2671612013-02-06 21:41:01 -0600245 /* Prepare USB controller early in S3 resume */
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600246 if (wake_from_s3)
Aaron Durbina2671612013-02-06 21:41:01 -0600247 enable_usb_bar();
248
249 post_code(0x3a);
250 params->pei_data->boot_mode = boot_mode;
251#if CONFIG_COLLECT_TIMESTAMPS
252 before_dram_time = rdtsc();
253#endif
254
255 report_platform_info();
256
257 sdram_initialize(params->pei_data);
258
259#if CONFIG_COLLECT_TIMESTAMPS
260 after_dram_time = rdtsc();
261#endif
262 post_code(0x3b);
263
264 intel_early_me_status();
265
266 quick_ram_check();
267 post_code(0x3e);
268
269#if CONFIG_EARLY_CBMEM_INIT
270 cbmem_was_initted = !cbmem_initialize();
271#else
272 cbmem_was_initted = cbmem_reinit((uint64_t) (get_top_of_ram()
273 - HIGH_MEMORY_SIZE));
274#endif
275
Aaron Durbin2ad1dba2013-02-07 00:51:18 -0600276 /* Save data returned from MRC on non-S3 resumes. */
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600277 if (!wake_from_s3)
Aaron Durbin2ad1dba2013-02-07 00:51:18 -0600278 save_mrc_data(params->pei_data);
279
Aaron Durbina2671612013-02-06 21:41:01 -0600280#if CONFIG_HAVE_ACPI_RESUME
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600281 if (wake_from_s3 && !cbmem_was_initted) {
Aaron Durbina2671612013-02-06 21:41:01 -0600282 /* Failed S3 resume, reset to come up cleanly */
Aaron Durbinb86113f2013-02-19 08:59:16 -0600283 reset_system();
Aaron Durbina2671612013-02-06 21:41:01 -0600284 }
285#endif
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600286
287 handoff = romstage_handoff_find_or_add();
288 if (handoff != NULL)
289 handoff->s3_resume = wake_from_s3;
290 else
291 printk(BIOS_DEBUG, "Romstage handoff structure not added!\n");
292
Aaron Durbina2671612013-02-06 21:41:01 -0600293 post_code(0x3f);
294#if CONFIG_CHROMEOS
295 init_chromeos(boot_mode);
296#endif
297#if CONFIG_COLLECT_TIMESTAMPS
298 timestamp_init(base_time);
299 timestamp_add(TS_START_ROMSTAGE, start_romstage_time );
300 timestamp_add(TS_BEFORE_INITRAM, before_dram_time );
301 timestamp_add(TS_AFTER_INITRAM, after_dram_time );
302 timestamp_add_now(TS_END_ROMSTAGE);
303#endif
304}
Aaron Durbin7492ec12013-02-08 22:18:04 -0600305
306static inline void prepare_for_resume(void)
307{
Aaron Durbine2d9e5b2013-02-08 17:38:35 -0600308/* Only need to save memory when ramstage isn't relocatable. */
309#if !CONFIG_RELOCATABLE_RAMSTAGE
Aaron Durbin7492ec12013-02-08 22:18:04 -0600310#if CONFIG_HAVE_ACPI_RESUME
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600311 struct romstage_handoff *handoff = romstage_handoff_find_or_add();
312
Aaron Durbin7492ec12013-02-08 22:18:04 -0600313 /* Back up the OS-controlled memory where ramstage will be loaded. */
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600314 if (handoff != NULL && handoff->s3_resume) {
Aaron Durbin7492ec12013-02-08 22:18:04 -0600315 void *src = (void *)CONFIG_RAMBASE;
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600316 void *dest = cbmem_find(CBMEM_ID_RESUME);
317 if (dest != NULL)
318 memcpy(dest, src, HIGH_MEMORY_SAVE);
Aaron Durbin7492ec12013-02-08 22:18:04 -0600319 }
320#endif
Aaron Durbine2d9e5b2013-02-08 17:38:35 -0600321#endif
Aaron Durbin7492ec12013-02-08 22:18:04 -0600322}
323
324void romstage_after_car(void)
325{
326 prepare_for_resume();
327 /* Load the ramstage. */
328 copy_and_run(0);
329}
Aaron Durbinf7cdfe52013-02-16 00:05:52 -0600330
331
332#if CONFIG_RELOCATABLE_RAMSTAGE
333void cache_loaded_ramstage(struct romstage_handoff *handoff,
334 void *ramstage_base, uint32_t ramstage_size,
335 void *entry_point)
336{
337 struct ramstage_cache *cache;
338 uint32_t total_size;
339
340 /* The ramstage cache lives in the TSEG region at RESERVED_SMM_OFFSET.
341 * The top of ram is defined to be the TSEG base address. */
342 cache = (void *)(get_top_of_ram() + RESERVED_SMM_OFFSET);
343 total_size = sizeof(*cache) + ramstage_size;
344 if (total_size > RESERVED_SMM_SIZE) {
345 printk(BIOS_DEBUG, "0x%08x > RESERVED_SMM_SIZE (0x%08x)\n",
346 total_size, RESERVED_SMM_SIZE);
347 /* Nuke whatever may be there now just in case. */
348 cache->magic = ~RAMSTAGE_CACHE_MAGIC;
349 return;
350 }
351
352 cache->magic = RAMSTAGE_CACHE_MAGIC;
353 cache->entry_point = (uint32_t)entry_point;
354 cache->load_address = (uint32_t)ramstage_base;
355 cache->size = ramstage_size;
356
357 printk(BIOS_DEBUG, "Saving ramstage to SMM space cache.\n");
358
359 /* Copy over the program. */
360 memcpy(&cache->program[0], ramstage_base, ramstage_size);
361
362 /* Do not update reserve region if the handoff structure is not
363 * available. Perhaps the ramstage will fix things up for the resume
364 * path. */
365 if (handoff == NULL)
366 return;
367
368 /* Update entry and reserve region. */
369 handoff->reserve_base = (uint32_t)ramstage_base;
370 handoff->reserve_size = ramstage_size;
371 handoff->ramstage_entry_point = (uint32_t)entry_point;
372}
373
374void *load_cached_ramstage(struct romstage_handoff *handoff)
375{
376 struct ramstage_cache *cache;
377
378 /* The ramstage cache lives in the TSEG region at RESERVED_SMM_OFFSET.
379 * The top of ram is defined to be the TSEG base address. */
380 cache = (void *)(get_top_of_ram() + RESERVED_SMM_OFFSET);
381
382 if (cache->magic != RAMSTAGE_CACHE_MAGIC) {
383 printk(BIOS_DEBUG, "Invalid ramstage cache found.\n");
Aaron Durbinb86113f2013-02-19 08:59:16 -0600384 #if CONFIG_RESET_ON_INVALID_RAMSTAGE_CACHE
385 reset_system();
386 #endif
Aaron Durbinf7cdfe52013-02-16 00:05:52 -0600387 return NULL;
388 }
389
390 printk(BIOS_DEBUG, "Loading ramstage from SMM space cache.\n");
391
392 memcpy((void *)cache->load_address, &cache->program[0], cache->size);
393
394 return (void *)cache->entry_point;
395}
396#endif