blob: d491c7e4ad8091cc6054d493a882d9768ce8e37d [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 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 Durbin38d94232013-02-07 00:03:33 -060049/* The cache-as-ram assembly file calls romstage_main() after setting up
50 * cache-as-ram. romstage_main() will then call the mainboards's
51 * mainboard_romstage_entry() function. That function then calls
52 * romstage_common() below. The reason for the back and forth is to provide
53 * common entry point from cache-as-ram while still allowing for code sharing.
54 * Because we can't use global variables the stack is used for allocations --
55 * thus the need to call back and forth. */
Aaron Durbin3d0071b2013-01-18 14:32:50 -060056
Aaron Durbin38d94232013-02-07 00:03:33 -060057
58static inline u32 *stack_push(u32 *stack, u32 value)
59{
60 stack = &stack[-1];
61 *stack = value;
62 return stack;
63}
64
Aaron Durbine2d9e5b2013-02-08 17:38:35 -060065static unsigned long choose_top_of_stack(void)
66{
67 unsigned long stack_top;
68#if CONFIG_RELOCATABLE_RAMSTAGE
69 stack_top = (unsigned long)cbmem_add(CBMEM_ID_RESUME_SCRATCH,
70 CONFIG_HIGH_SCRATCH_MEMORY_SIZE);
71 stack_top += CONFIG_HIGH_SCRATCH_MEMORY_SIZE;
72#else
73 stack_top = ROMSTAGE_STACK;
74#endif
75 return stack_top;
76}
77
Aaron Durbin38d94232013-02-07 00:03:33 -060078/* setup_romstage_stack_after_car() determines the stack to use after
79 * cache-as-ram is torn down as well as the MTRR settings to use. */
80static void *setup_romstage_stack_after_car(void)
81{
82 unsigned long top_of_stack;
83 int num_mtrrs;
84 u32 *slot;
85 u32 mtrr_mask_upper;
Aaron Durbin67481ddc2013-02-15 15:08:37 -060086 u32 top_of_ram;
Aaron Durbin38d94232013-02-07 00:03:33 -060087
88 /* Top of stack needs to be aligned to a 4-byte boundary. */
Aaron Durbine2d9e5b2013-02-08 17:38:35 -060089 top_of_stack = choose_top_of_stack() & ~3;
Aaron Durbin38d94232013-02-07 00:03:33 -060090 slot = (void *)top_of_stack;
91 num_mtrrs = 0;
92
93 /* The upper bits of the MTRR mask need to set according to the number
94 * of physical address bits. */
95 mtrr_mask_upper = (1 << ((cpuid_eax(0x80000008) & 0xff) - 32)) - 1;
96
97 /* The order for each MTTR is value then base with upper 32-bits of
98 * each value coming before the lower 32-bits. The reasoning for
99 * this ordering is to create a stack layout like the following:
100 * +0: Number of MTRRs
101 * +4: MTTR base 0 31:0
102 * +8: MTTR base 0 63:32
103 * +12: MTTR mask 0 31:0
104 * +16: MTTR mask 0 63:32
105 * +20: MTTR base 1 31:0
106 * +24: MTTR base 1 63:32
107 * +28: MTTR mask 1 31:0
108 * +32: MTTR mask 1 63:32
109 */
110
111 /* Cache the ROM as WP just below 4GiB. */
112 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
113 slot = stack_push(slot, ~(CONFIG_ROM_SIZE - 1) | MTRRphysMaskValid);
114 slot = stack_push(slot, 0); /* upper base */
115 slot = stack_push(slot, ~(CONFIG_ROM_SIZE - 1) | MTRR_TYPE_WRPROT);
116 num_mtrrs++;
117
118 /* Cache RAM as WB from 0 -> CONFIG_RAMTOP. */
119 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
120 slot = stack_push(slot, ~(CONFIG_RAMTOP - 1) | MTRRphysMaskValid);
121 slot = stack_push(slot, 0); /* upper base */
122 slot = stack_push(slot, 0 | MTRR_TYPE_WRBACK);
123 num_mtrrs++;
124
Aaron Durbin67481ddc2013-02-15 15:08:37 -0600125 top_of_ram = get_top_of_ram();
Aaron Durbin38d94232013-02-07 00:03:33 -0600126 /* Cache 8MiB below the top of ram. On haswell systems the top of
127 * ram under 4GiB is the start of the TSEG region. It is required to
128 * be 8MiB aligned. Set this area as cacheable so it can be used later
129 * for ramstage before setting up the entire RAM as cacheable. */
130 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
131 slot = stack_push(slot, ~((8 << 20) - 1) | MTRRphysMaskValid);
132 slot = stack_push(slot, 0); /* upper base */
Aaron Durbin67481ddc2013-02-15 15:08:37 -0600133 slot = stack_push(slot, (top_of_ram - (8 << 20)) | MTRR_TYPE_WRBACK);
134 num_mtrrs++;
135
136 /* Cache 8MiB at the top of ram. Top of ram on haswell systems
137 * is where the TSEG region resides. However, it is not restricted
138 * to SMM mode until SMM has been relocated. By setting the region
139 * to cacheable it provides faster access when relocating the SMM
140 * handler as well as using the TSEG region for other purposes. */
141 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
142 slot = stack_push(slot, ~((8 << 20) - 1) | MTRRphysMaskValid);
143 slot = stack_push(slot, 0); /* upper base */
144 slot = stack_push(slot, top_of_ram | MTRR_TYPE_WRBACK);
Aaron Durbin38d94232013-02-07 00:03:33 -0600145 num_mtrrs++;
146
147 /* Save the number of MTTRs to setup. Return the stack location
148 * pointing to the number of MTRRs. */
149 slot = stack_push(slot, num_mtrrs);
150
151 return slot;
152}
153
154void * __attribute__((regparm(0))) romstage_main(unsigned long bist)
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600155{
156 int i;
Aaron Durbin38d94232013-02-07 00:03:33 -0600157 void *romstage_stack_after_car;
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600158 const int num_guards = 4;
159 const u32 stack_guard = 0xdeadbeef;
160 u32 *stack_base = (void *)(CONFIG_DCACHE_RAM_BASE +
161 CONFIG_DCACHE_RAM_SIZE -
162 CONFIG_DCACHE_RAM_ROMSTAGE_STACK_SIZE);
163
164 printk(BIOS_DEBUG, "Setting up stack guards.\n");
165 for (i = 0; i < num_guards; i++)
166 stack_base[i] = stack_guard;
167
Aaron Durbina2671612013-02-06 21:41:01 -0600168 mainboard_romstage_entry(bist);
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600169
170 /* Check the stack. */
171 for (i = 0; i < num_guards; i++) {
172 if (stack_base[i] == stack_guard)
173 continue;
174 printk(BIOS_DEBUG, "Smashed stack detected in romstage!\n");
175 }
176
Aaron Durbin38d94232013-02-07 00:03:33 -0600177 /* Get the stack to use after cache-as-ram is torn down. */
178 romstage_stack_after_car = setup_romstage_stack_after_car();
179
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600180#if CONFIG_CONSOLE_CBMEM
181 /* Keep this the last thing this function does. */
182 cbmemc_reinit();
183#endif
Aaron Durbin38d94232013-02-07 00:03:33 -0600184
185 return romstage_stack_after_car;
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600186}
Aaron Durbina2671612013-02-06 21:41:01 -0600187
188void romstage_common(const struct romstage_params *params)
189{
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600190 int boot_mode;
Aaron Durbina2671612013-02-06 21:41:01 -0600191 int wake_from_s3;
192 int cbmem_was_initted;
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600193 struct romstage_handoff *handoff;
Aaron Durbina2671612013-02-06 21:41:01 -0600194
195#if CONFIG_COLLECT_TIMESTAMPS
196 tsc_t start_romstage_time;
197 tsc_t before_dram_time;
198 tsc_t after_dram_time;
199 tsc_t base_time = {
200 .lo = pci_read_config32(PCI_DEV(0, 0x00, 0), 0xdc),
201 .hi = pci_read_config32(PCI_DEV(0, 0x1f, 2), 0xd0)
202 };
203#endif
204
205#if CONFIG_COLLECT_TIMESTAMPS
206 start_romstage_time = rdtsc();
207#endif
208
209 if (params->bist == 0)
210 enable_lapic();
211
212 wake_from_s3 = early_pch_init(params->gpio_map, params->rcba_config);
213
214 /* 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;
242#if CONFIG_COLLECT_TIMESTAMPS
243 before_dram_time = rdtsc();
244#endif
245
246 report_platform_info();
247
248 sdram_initialize(params->pei_data);
249
250#if CONFIG_COLLECT_TIMESTAMPS
251 after_dram_time = rdtsc();
252#endif
253 post_code(0x3b);
254
255 intel_early_me_status();
256
257 quick_ram_check();
258 post_code(0x3e);
259
260#if CONFIG_EARLY_CBMEM_INIT
261 cbmem_was_initted = !cbmem_initialize();
262#else
263 cbmem_was_initted = cbmem_reinit((uint64_t) (get_top_of_ram()
264 - HIGH_MEMORY_SIZE));
265#endif
266
Aaron Durbin2ad1dba2013-02-07 00:51:18 -0600267 /* Save data returned from MRC on non-S3 resumes. */
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600268 if (!wake_from_s3)
Aaron Durbin2ad1dba2013-02-07 00:51:18 -0600269 save_mrc_data(params->pei_data);
270
Aaron Durbina2671612013-02-06 21:41:01 -0600271#if CONFIG_HAVE_ACPI_RESUME
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600272 if (wake_from_s3 && !cbmem_was_initted) {
Aaron Durbina2671612013-02-06 21:41:01 -0600273 /* Failed S3 resume, reset to come up cleanly */
274 outb(0x6, 0xcf9);
275 while (1) {
276 hlt();
277 }
Aaron Durbina2671612013-02-06 21:41:01 -0600278 }
279#endif
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600280
281 handoff = romstage_handoff_find_or_add();
282 if (handoff != NULL)
283 handoff->s3_resume = wake_from_s3;
284 else
285 printk(BIOS_DEBUG, "Romstage handoff structure not added!\n");
286
Aaron Durbina2671612013-02-06 21:41:01 -0600287 post_code(0x3f);
288#if CONFIG_CHROMEOS
289 init_chromeos(boot_mode);
290#endif
291#if CONFIG_COLLECT_TIMESTAMPS
292 timestamp_init(base_time);
293 timestamp_add(TS_START_ROMSTAGE, start_romstage_time );
294 timestamp_add(TS_BEFORE_INITRAM, before_dram_time );
295 timestamp_add(TS_AFTER_INITRAM, after_dram_time );
296 timestamp_add_now(TS_END_ROMSTAGE);
297#endif
298}
Aaron Durbin7492ec12013-02-08 22:18:04 -0600299
300static inline void prepare_for_resume(void)
301{
Aaron Durbine2d9e5b2013-02-08 17:38:35 -0600302/* Only need to save memory when ramstage isn't relocatable. */
303#if !CONFIG_RELOCATABLE_RAMSTAGE
Aaron Durbin7492ec12013-02-08 22:18:04 -0600304#if CONFIG_HAVE_ACPI_RESUME
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600305 struct romstage_handoff *handoff = romstage_handoff_find_or_add();
306
Aaron Durbin7492ec12013-02-08 22:18:04 -0600307 /* Back up the OS-controlled memory where ramstage will be loaded. */
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600308 if (handoff != NULL && handoff->s3_resume) {
Aaron Durbin7492ec12013-02-08 22:18:04 -0600309 void *src = (void *)CONFIG_RAMBASE;
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600310 void *dest = cbmem_find(CBMEM_ID_RESUME);
311 if (dest != NULL)
312 memcpy(dest, src, HIGH_MEMORY_SAVE);
Aaron Durbin7492ec12013-02-08 22:18:04 -0600313 }
314#endif
Aaron Durbine2d9e5b2013-02-08 17:38:35 -0600315#endif
Aaron Durbin7492ec12013-02-08 22:18:04 -0600316}
317
318void romstage_after_car(void)
319{
320 prepare_for_resume();
321 /* Load the ramstage. */
322 copy_and_run(0);
323}
Aaron Durbinf7cdfe52013-02-16 00:05:52 -0600324
325
326#if CONFIG_RELOCATABLE_RAMSTAGE
327void cache_loaded_ramstage(struct romstage_handoff *handoff,
328 void *ramstage_base, uint32_t ramstage_size,
329 void *entry_point)
330{
331 struct ramstage_cache *cache;
332 uint32_t total_size;
333
334 /* The ramstage cache lives in the TSEG region at RESERVED_SMM_OFFSET.
335 * The top of ram is defined to be the TSEG base address. */
336 cache = (void *)(get_top_of_ram() + RESERVED_SMM_OFFSET);
337 total_size = sizeof(*cache) + ramstage_size;
338 if (total_size > RESERVED_SMM_SIZE) {
339 printk(BIOS_DEBUG, "0x%08x > RESERVED_SMM_SIZE (0x%08x)\n",
340 total_size, RESERVED_SMM_SIZE);
341 /* Nuke whatever may be there now just in case. */
342 cache->magic = ~RAMSTAGE_CACHE_MAGIC;
343 return;
344 }
345
346 cache->magic = RAMSTAGE_CACHE_MAGIC;
347 cache->entry_point = (uint32_t)entry_point;
348 cache->load_address = (uint32_t)ramstage_base;
349 cache->size = ramstage_size;
350
351 printk(BIOS_DEBUG, "Saving ramstage to SMM space cache.\n");
352
353 /* Copy over the program. */
354 memcpy(&cache->program[0], ramstage_base, ramstage_size);
355
356 /* Do not update reserve region if the handoff structure is not
357 * available. Perhaps the ramstage will fix things up for the resume
358 * path. */
359 if (handoff == NULL)
360 return;
361
362 /* Update entry and reserve region. */
363 handoff->reserve_base = (uint32_t)ramstage_base;
364 handoff->reserve_size = ramstage_size;
365 handoff->ramstage_entry_point = (uint32_t)entry_point;
366}
367
368void *load_cached_ramstage(struct romstage_handoff *handoff)
369{
370 struct ramstage_cache *cache;
371
372 /* The ramstage cache lives in the TSEG region at RESERVED_SMM_OFFSET.
373 * The top of ram is defined to be the TSEG base address. */
374 cache = (void *)(get_top_of_ram() + RESERVED_SMM_OFFSET);
375
376 if (cache->magic != RAMSTAGE_CACHE_MAGIC) {
377 printk(BIOS_DEBUG, "Invalid ramstage cache found.\n");
378 return NULL;
379 }
380
381 printk(BIOS_DEBUG, "Loading ramstage from SMM space cache.\n");
382
383 memcpy((void *)cache->load_address, &cache->program[0], cache->size);
384
385 return (void *)cache->entry_point;
386}
387#endif