blob: c4a4e8ab2af4d624a85a06d10249f33a556bbccb [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>
37#if CONFIG_CHROMEOS
38#include <vendorcode/google/chromeos/chromeos.h>
39#endif
40#include "haswell.h"
41#include "northbridge/intel/haswell/haswell.h"
42#include "northbridge/intel/haswell/raminit.h"
43#include "southbridge/intel/lynxpoint/pch.h"
44#include "southbridge/intel/lynxpoint/me.h"
Aaron Durbin3d0071b2013-01-18 14:32:50 -060045
Aaron Durbina2671612013-02-06 21:41:01 -060046
Aaron Durbin38d94232013-02-07 00:03:33 -060047/* The cache-as-ram assembly file calls romstage_main() after setting up
48 * cache-as-ram. romstage_main() will then call the mainboards's
49 * mainboard_romstage_entry() function. That function then calls
50 * romstage_common() below. The reason for the back and forth is to provide
51 * common entry point from cache-as-ram while still allowing for code sharing.
52 * Because we can't use global variables the stack is used for allocations --
53 * thus the need to call back and forth. */
Aaron Durbin3d0071b2013-01-18 14:32:50 -060054
Aaron Durbin38d94232013-02-07 00:03:33 -060055
56static inline u32 *stack_push(u32 *stack, u32 value)
57{
58 stack = &stack[-1];
59 *stack = value;
60 return stack;
61}
62
Aaron Durbine2d9e5b2013-02-08 17:38:35 -060063static unsigned long choose_top_of_stack(void)
64{
65 unsigned long stack_top;
66#if CONFIG_RELOCATABLE_RAMSTAGE
67 stack_top = (unsigned long)cbmem_add(CBMEM_ID_RESUME_SCRATCH,
68 CONFIG_HIGH_SCRATCH_MEMORY_SIZE);
69 stack_top += CONFIG_HIGH_SCRATCH_MEMORY_SIZE;
70#else
71 stack_top = ROMSTAGE_STACK;
72#endif
73 return stack_top;
74}
75
Aaron Durbin38d94232013-02-07 00:03:33 -060076/* setup_romstage_stack_after_car() determines the stack to use after
77 * cache-as-ram is torn down as well as the MTRR settings to use. */
78static void *setup_romstage_stack_after_car(void)
79{
80 unsigned long top_of_stack;
81 int num_mtrrs;
82 u32 *slot;
83 u32 mtrr_mask_upper;
84
85 /* Top of stack needs to be aligned to a 4-byte boundary. */
Aaron Durbine2d9e5b2013-02-08 17:38:35 -060086 top_of_stack = choose_top_of_stack() & ~3;
Aaron Durbin38d94232013-02-07 00:03:33 -060087 slot = (void *)top_of_stack;
88 num_mtrrs = 0;
89
90 /* The upper bits of the MTRR mask need to set according to the number
91 * of physical address bits. */
92 mtrr_mask_upper = (1 << ((cpuid_eax(0x80000008) & 0xff) - 32)) - 1;
93
94 /* The order for each MTTR is value then base with upper 32-bits of
95 * each value coming before the lower 32-bits. The reasoning for
96 * this ordering is to create a stack layout like the following:
97 * +0: Number of MTRRs
98 * +4: MTTR base 0 31:0
99 * +8: MTTR base 0 63:32
100 * +12: MTTR mask 0 31:0
101 * +16: MTTR mask 0 63:32
102 * +20: MTTR base 1 31:0
103 * +24: MTTR base 1 63:32
104 * +28: MTTR mask 1 31:0
105 * +32: MTTR mask 1 63:32
106 */
107
108 /* Cache the ROM as WP just below 4GiB. */
109 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
110 slot = stack_push(slot, ~(CONFIG_ROM_SIZE - 1) | MTRRphysMaskValid);
111 slot = stack_push(slot, 0); /* upper base */
112 slot = stack_push(slot, ~(CONFIG_ROM_SIZE - 1) | MTRR_TYPE_WRPROT);
113 num_mtrrs++;
114
115 /* Cache RAM as WB from 0 -> CONFIG_RAMTOP. */
116 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
117 slot = stack_push(slot, ~(CONFIG_RAMTOP - 1) | MTRRphysMaskValid);
118 slot = stack_push(slot, 0); /* upper base */
119 slot = stack_push(slot, 0 | MTRR_TYPE_WRBACK);
120 num_mtrrs++;
121
122 /* Cache 8MiB below the top of ram. On haswell systems the top of
123 * ram under 4GiB is the start of the TSEG region. It is required to
124 * be 8MiB aligned. Set this area as cacheable so it can be used later
125 * for ramstage before setting up the entire RAM as cacheable. */
126 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
127 slot = stack_push(slot, ~((8 << 20) - 1) | MTRRphysMaskValid);
128 slot = stack_push(slot, 0); /* upper base */
129 slot = stack_push(slot,
130 (get_top_of_ram() - (8 << 20)) | MTRR_TYPE_WRBACK);
131 num_mtrrs++;
132
133 /* Save the number of MTTRs to setup. Return the stack location
134 * pointing to the number of MTRRs. */
135 slot = stack_push(slot, num_mtrrs);
136
137 return slot;
138}
139
140void * __attribute__((regparm(0))) romstage_main(unsigned long bist)
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600141{
142 int i;
Aaron Durbin38d94232013-02-07 00:03:33 -0600143 void *romstage_stack_after_car;
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600144 const int num_guards = 4;
145 const u32 stack_guard = 0xdeadbeef;
146 u32 *stack_base = (void *)(CONFIG_DCACHE_RAM_BASE +
147 CONFIG_DCACHE_RAM_SIZE -
148 CONFIG_DCACHE_RAM_ROMSTAGE_STACK_SIZE);
149
150 printk(BIOS_DEBUG, "Setting up stack guards.\n");
151 for (i = 0; i < num_guards; i++)
152 stack_base[i] = stack_guard;
153
Aaron Durbina2671612013-02-06 21:41:01 -0600154 mainboard_romstage_entry(bist);
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600155
156 /* Check the stack. */
157 for (i = 0; i < num_guards; i++) {
158 if (stack_base[i] == stack_guard)
159 continue;
160 printk(BIOS_DEBUG, "Smashed stack detected in romstage!\n");
161 }
162
Aaron Durbin38d94232013-02-07 00:03:33 -0600163 /* Get the stack to use after cache-as-ram is torn down. */
164 romstage_stack_after_car = setup_romstage_stack_after_car();
165
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600166#if CONFIG_CONSOLE_CBMEM
167 /* Keep this the last thing this function does. */
168 cbmemc_reinit();
169#endif
Aaron Durbin38d94232013-02-07 00:03:33 -0600170
171 return romstage_stack_after_car;
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600172}
Aaron Durbina2671612013-02-06 21:41:01 -0600173
174void romstage_common(const struct romstage_params *params)
175{
176 int boot_mode = 0;
177 int wake_from_s3;
178 int cbmem_was_initted;
179
180#if CONFIG_COLLECT_TIMESTAMPS
181 tsc_t start_romstage_time;
182 tsc_t before_dram_time;
183 tsc_t after_dram_time;
184 tsc_t base_time = {
185 .lo = pci_read_config32(PCI_DEV(0, 0x00, 0), 0xdc),
186 .hi = pci_read_config32(PCI_DEV(0, 0x1f, 2), 0xd0)
187 };
188#endif
189
190#if CONFIG_COLLECT_TIMESTAMPS
191 start_romstage_time = rdtsc();
192#endif
193
194 if (params->bist == 0)
195 enable_lapic();
196
197 wake_from_s3 = early_pch_init(params->gpio_map, params->rcba_config);
198
199 /* Halt if there was a built in self test failure */
200 report_bist_failure(params->bist);
201
202 /* Perform some early chipset initialization required
203 * before RAM initialization can work
204 */
205 haswell_early_initialization(HASWELL_MOBILE);
206 printk(BIOS_DEBUG, "Back from haswell_early_initialization()\n");
207
208 if (wake_from_s3) {
209#if CONFIG_HAVE_ACPI_RESUME
210 printk(BIOS_DEBUG, "Resume from S3 detected.\n");
211 boot_mode = 2;
212#else
213 printk(BIOS_DEBUG, "Resume from S3 detected, but disabled.\n");
214#endif
215 }
216
217 /* Prepare USB controller early in S3 resume */
218 if (boot_mode == 2)
219 enable_usb_bar();
220
221 post_code(0x3a);
222 params->pei_data->boot_mode = boot_mode;
223#if CONFIG_COLLECT_TIMESTAMPS
224 before_dram_time = rdtsc();
225#endif
226
227 report_platform_info();
228
229 sdram_initialize(params->pei_data);
230
231#if CONFIG_COLLECT_TIMESTAMPS
232 after_dram_time = rdtsc();
233#endif
234 post_code(0x3b);
235
236 intel_early_me_status();
237
238 quick_ram_check();
239 post_code(0x3e);
240
241#if CONFIG_EARLY_CBMEM_INIT
242 cbmem_was_initted = !cbmem_initialize();
243#else
244 cbmem_was_initted = cbmem_reinit((uint64_t) (get_top_of_ram()
245 - HIGH_MEMORY_SIZE));
246#endif
247
Aaron Durbin2ad1dba2013-02-07 00:51:18 -0600248 /* Save data returned from MRC on non-S3 resumes. */
249 if (boot_mode != 2)
250 save_mrc_data(params->pei_data);
251
Aaron Durbina2671612013-02-06 21:41:01 -0600252#if CONFIG_HAVE_ACPI_RESUME
253 /* If there is no high memory area, we didn't boot before, so
254 * this is not a resume. In that case we just create the cbmem toc.
255 */
256
257 *(u32 *)CBMEM_BOOT_MODE = 0;
258 *(u32 *)CBMEM_RESUME_BACKUP = 0;
259
260 if ((boot_mode == 2) && cbmem_was_initted) {
Aaron Durbine2d9e5b2013-02-08 17:38:35 -0600261 #if !CONFIG_RELOCATABLE_RAMSTAGE
Aaron Durbina2671612013-02-06 21:41:01 -0600262 void *resume_backup_memory = cbmem_find(CBMEM_ID_RESUME);
263 if (resume_backup_memory) {
264 *(u32 *)CBMEM_BOOT_MODE = boot_mode;
265 *(u32 *)CBMEM_RESUME_BACKUP = (u32)resume_backup_memory;
266 }
Aaron Durbine2d9e5b2013-02-08 17:38:35 -0600267 #endif
Aaron Durbina2671612013-02-06 21:41:01 -0600268 /* Magic for S3 resume */
269 pci_write_config32(PCI_DEV(0, 0x00, 0), SKPAD, 0xcafed00d);
270 } else if (boot_mode == 2) {
271 /* Failed S3 resume, reset to come up cleanly */
272 outb(0x6, 0xcf9);
273 while (1) {
274 hlt();
275 }
276 } else {
277 pci_write_config32(PCI_DEV(0, 0x00, 0), SKPAD, 0xcafebabe);
278 }
279#endif
280 post_code(0x3f);
281#if CONFIG_CHROMEOS
282 init_chromeos(boot_mode);
283#endif
284#if CONFIG_COLLECT_TIMESTAMPS
285 timestamp_init(base_time);
286 timestamp_add(TS_START_ROMSTAGE, start_romstage_time );
287 timestamp_add(TS_BEFORE_INITRAM, before_dram_time );
288 timestamp_add(TS_AFTER_INITRAM, after_dram_time );
289 timestamp_add_now(TS_END_ROMSTAGE);
290#endif
291}
Aaron Durbin7492ec12013-02-08 22:18:04 -0600292
293static inline void prepare_for_resume(void)
294{
Aaron Durbine2d9e5b2013-02-08 17:38:35 -0600295/* Only need to save memory when ramstage isn't relocatable. */
296#if !CONFIG_RELOCATABLE_RAMSTAGE
Aaron Durbin7492ec12013-02-08 22:18:04 -0600297#if CONFIG_HAVE_ACPI_RESUME
298 /* Back up the OS-controlled memory where ramstage will be loaded. */
299 if (*(u32 *)CBMEM_BOOT_MODE == 2) {
300 void *src = (void *)CONFIG_RAMBASE;
301 void *dest = *(void **)CBMEM_RESUME_BACKUP;
302 memcpy(dest, src, HIGH_MEMORY_SAVE);
303 }
304#endif
Aaron Durbine2d9e5b2013-02-08 17:38:35 -0600305#endif
Aaron Durbin7492ec12013-02-08 22:18:04 -0600306}
307
308void romstage_after_car(void)
309{
310 prepare_for_resume();
311 /* Load the ramstage. */
312 copy_and_run(0);
313}