blob: 8b15ed546e10f53cb67f5776f126c207aaf2da61 [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>
Kyösti Mälkkia969ed32016-06-15 06:08:15 +030027#include <arch/acpi.h>
Aaron Durbina2671612013-02-06 21:41:01 -060028#include <arch/io.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>
Kyösti Mälkki65e8f642016-06-27 11:27:56 +030032#include <program_loading.h>
Aaron Durbinbf396ff2013-02-11 21:50:35 -060033#include <romstage_handoff.h>
Aaron Durbinb86113f2013-02-19 08:59:16 -060034#include <reset.h>
Aaron Durbina2671612013-02-06 21:41:01 -060035#include <vendorcode/google/chromeos/chromeos.h>
Duncan Laurie7cced0d2013-06-04 10:03:34 -070036#if CONFIG_EC_GOOGLE_CHROMEEC
37#include <ec/google/chromeec/ec.h>
38#endif
Aaron Durbina2671612013-02-06 21:41:01 -060039#include "haswell.h"
40#include "northbridge/intel/haswell/haswell.h"
41#include "northbridge/intel/haswell/raminit.h"
42#include "southbridge/intel/lynxpoint/pch.h"
43#include "southbridge/intel/lynxpoint/me.h"
Vladimir Serbinenko0e90dae2015-05-18 10:29:06 +020044#include <tpm.h>
Aaron Durbina2671612013-02-06 21:41:01 -060045
Aaron Durbinb86113f2013-02-19 08:59:16 -060046static inline void reset_system(void)
47{
48 hard_reset();
Patrick Georgibd79c5e2014-11-28 22:35:36 +010049 halt();
Aaron Durbinb86113f2013-02-19 08:59:16 -060050}
51
Aaron Durbin38d94232013-02-07 00:03:33 -060052/* The cache-as-ram assembly file calls romstage_main() after setting up
53 * cache-as-ram. romstage_main() will then call the mainboards's
54 * mainboard_romstage_entry() function. That function then calls
55 * romstage_common() below. The reason for the back and forth is to provide
56 * common entry point from cache-as-ram while still allowing for code sharing.
57 * Because we can't use global variables the stack is used for allocations --
58 * thus the need to call back and forth. */
Aaron Durbin3d0071b2013-01-18 14:32:50 -060059
Aaron Durbin38d94232013-02-07 00:03:33 -060060
61static inline u32 *stack_push(u32 *stack, u32 value)
62{
63 stack = &stack[-1];
64 *stack = value;
65 return stack;
66}
67
68/* setup_romstage_stack_after_car() determines the stack to use after
69 * cache-as-ram is torn down as well as the MTRR settings to use. */
70static void *setup_romstage_stack_after_car(void)
71{
72 unsigned long top_of_stack;
73 int num_mtrrs;
74 u32 *slot;
75 u32 mtrr_mask_upper;
Aaron Durbin67481ddc2013-02-15 15:08:37 -060076 u32 top_of_ram;
Aaron Durbin38d94232013-02-07 00:03:33 -060077
78 /* Top of stack needs to be aligned to a 4-byte boundary. */
Kyösti Mälkkie5c00a52016-06-27 14:50:27 +030079 top_of_stack = romstage_ram_stack_top() & ~3;
Aaron Durbin38d94232013-02-07 00:03:33 -060080 slot = (void *)top_of_stack;
81 num_mtrrs = 0;
82
83 /* The upper bits of the MTRR mask need to set according to the number
84 * of physical address bits. */
85 mtrr_mask_upper = (1 << ((cpuid_eax(0x80000008) & 0xff) - 32)) - 1;
86
Paul Menzel4fe98132014-01-25 15:55:28 +010087 /* The order for each MTRR is value then base with upper 32-bits of
Aaron Durbin38d94232013-02-07 00:03:33 -060088 * each value coming before the lower 32-bits. The reasoning for
89 * this ordering is to create a stack layout like the following:
90 * +0: Number of MTRRs
Paul Menzel4fe98132014-01-25 15:55:28 +010091 * +4: MTRR base 0 31:0
92 * +8: MTRR base 0 63:32
93 * +12: MTRR mask 0 31:0
94 * +16: MTRR mask 0 63:32
95 * +20: MTRR base 1 31:0
96 * +24: MTRR base 1 63:32
97 * +28: MTRR mask 1 31:0
98 * +32: MTRR mask 1 63:32
Aaron Durbin38d94232013-02-07 00:03:33 -060099 */
100
101 /* Cache the ROM as WP just below 4GiB. */
102 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700103 slot = stack_push(slot, ~(CACHE_ROM_SIZE - 1) | MTRR_PHYS_MASK_VALID);
Aaron Durbin38d94232013-02-07 00:03:33 -0600104 slot = stack_push(slot, 0); /* upper base */
Kyösti Mälkki107f72e2014-01-06 11:06:26 +0200105 slot = stack_push(slot, ~(CACHE_ROM_SIZE - 1) | MTRR_TYPE_WRPROT);
Aaron Durbin38d94232013-02-07 00:03:33 -0600106 num_mtrrs++;
107
Kyösti Mälkki65cc5262016-06-19 20:38:41 +0300108 /* Cache RAM as WB from 0 -> CACHE_TMP_RAMTOP. */
Aaron Durbin38d94232013-02-07 00:03:33 -0600109 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
Kyösti Mälkki65cc5262016-06-19 20:38:41 +0300110 slot = stack_push(slot, ~(CACHE_TMP_RAMTOP - 1) | MTRR_PHYS_MASK_VALID);
Aaron Durbin38d94232013-02-07 00:03:33 -0600111 slot = stack_push(slot, 0); /* upper base */
112 slot = stack_push(slot, 0 | MTRR_TYPE_WRBACK);
113 num_mtrrs++;
114
Kyösti Mälkkif1e3c762014-12-22 12:28:07 +0200115 top_of_ram = (uint32_t)cbmem_top();
Aaron Durbin38d94232013-02-07 00:03:33 -0600116 /* Cache 8MiB below the top of ram. On haswell systems the top of
117 * ram under 4GiB is the start of the TSEG region. It is required to
118 * be 8MiB aligned. Set this area as cacheable so it can be used later
119 * for ramstage before setting up the entire RAM as cacheable. */
120 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700121 slot = stack_push(slot, ~((8 << 20) - 1) | MTRR_PHYS_MASK_VALID);
Aaron Durbin38d94232013-02-07 00:03:33 -0600122 slot = stack_push(slot, 0); /* upper base */
Aaron Durbin67481ddc2013-02-15 15:08:37 -0600123 slot = stack_push(slot, (top_of_ram - (8 << 20)) | MTRR_TYPE_WRBACK);
124 num_mtrrs++;
125
126 /* Cache 8MiB at the top of ram. Top of ram on haswell systems
127 * is where the TSEG region resides. However, it is not restricted
128 * to SMM mode until SMM has been relocated. By setting the region
129 * to cacheable it provides faster access when relocating the SMM
130 * handler as well as using the TSEG region for other purposes. */
131 slot = stack_push(slot, mtrr_mask_upper); /* upper mask */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700132 slot = stack_push(slot, ~((8 << 20) - 1) | MTRR_PHYS_MASK_VALID);
Aaron Durbin67481ddc2013-02-15 15:08:37 -0600133 slot = stack_push(slot, 0); /* upper base */
134 slot = stack_push(slot, top_of_ram | MTRR_TYPE_WRBACK);
Aaron Durbin38d94232013-02-07 00:03:33 -0600135 num_mtrrs++;
136
Paul Menzel4fe98132014-01-25 15:55:28 +0100137 /* Save the number of MTRRs to setup. Return the stack location
Aaron Durbin38d94232013-02-07 00:03:33 -0600138 * pointing to the number of MTRRs. */
139 slot = stack_push(slot, num_mtrrs);
140
141 return slot;
142}
143
Aaron Durbin39ecc652013-05-02 09:42:13 -0500144void * asmlinkage romstage_main(unsigned long bist)
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600145{
146 int i;
Aaron Durbin38d94232013-02-07 00:03:33 -0600147 void *romstage_stack_after_car;
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600148 const int num_guards = 4;
149 const u32 stack_guard = 0xdeadbeef;
150 u32 *stack_base = (void *)(CONFIG_DCACHE_RAM_BASE +
151 CONFIG_DCACHE_RAM_SIZE -
152 CONFIG_DCACHE_RAM_ROMSTAGE_STACK_SIZE);
153
154 printk(BIOS_DEBUG, "Setting up stack guards.\n");
155 for (i = 0; i < num_guards; i++)
156 stack_base[i] = stack_guard;
157
Aaron Durbina2671612013-02-06 21:41:01 -0600158 mainboard_romstage_entry(bist);
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600159
160 /* Check the stack. */
161 for (i = 0; i < num_guards; i++) {
162 if (stack_base[i] == stack_guard)
163 continue;
164 printk(BIOS_DEBUG, "Smashed stack detected in romstage!\n");
165 }
166
Aaron Durbin38d94232013-02-07 00:03:33 -0600167 /* Get the stack to use after cache-as-ram is torn down. */
168 romstage_stack_after_car = setup_romstage_stack_after_car();
169
Aaron Durbin38d94232013-02-07 00:03:33 -0600170 return romstage_stack_after_car;
Aaron Durbin3d0071b2013-01-18 14:32:50 -0600171}
Aaron Durbina2671612013-02-06 21:41:01 -0600172
173void romstage_common(const struct romstage_params *params)
174{
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600175 int boot_mode;
Aaron Durbina2671612013-02-06 21:41:01 -0600176 int wake_from_s3;
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600177 struct romstage_handoff *handoff;
Aaron Durbina2671612013-02-06 21:41:01 -0600178
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300179 timestamp_init(get_initial_timestamp());
180 timestamp_add_now(TS_START_ROMSTAGE);
Aaron Durbina2671612013-02-06 21:41:01 -0600181
182 if (params->bist == 0)
183 enable_lapic();
184
185 wake_from_s3 = early_pch_init(params->gpio_map, params->rcba_config);
186
Duncan Laurie7cced0d2013-06-04 10:03:34 -0700187#if CONFIG_EC_GOOGLE_CHROMEEC
188 /* Ensure the EC is in the right mode for recovery */
189 google_chromeec_early_init();
190#endif
191
Aaron Durbina2671612013-02-06 21:41:01 -0600192 /* Halt if there was a built in self test failure */
193 report_bist_failure(params->bist);
194
195 /* Perform some early chipset initialization required
196 * before RAM initialization can work
197 */
198 haswell_early_initialization(HASWELL_MOBILE);
199 printk(BIOS_DEBUG, "Back from haswell_early_initialization()\n");
200
201 if (wake_from_s3) {
202#if CONFIG_HAVE_ACPI_RESUME
203 printk(BIOS_DEBUG, "Resume from S3 detected.\n");
Aaron Durbina2671612013-02-06 21:41:01 -0600204#else
205 printk(BIOS_DEBUG, "Resume from S3 detected, but disabled.\n");
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600206 wake_from_s3 = 0;
Aaron Durbina2671612013-02-06 21:41:01 -0600207#endif
208 }
209
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600210 /* There are hard coded assumptions of 2 meaning s3 wake. Normalize
211 * the users of the 2 literal here based off wake_from_s3. */
212 boot_mode = wake_from_s3 ? 2 : 0;
213
Aaron Durbina2671612013-02-06 21:41:01 -0600214 /* Prepare USB controller early in S3 resume */
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600215 if (wake_from_s3)
Aaron Durbina2671612013-02-06 21:41:01 -0600216 enable_usb_bar();
217
218 post_code(0x3a);
219 params->pei_data->boot_mode = boot_mode;
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300220
221 timestamp_add_now(TS_BEFORE_INITRAM);
Aaron Durbina2671612013-02-06 21:41:01 -0600222
223 report_platform_info();
224
Aaron Durbinc7633f42013-06-13 17:29:36 -0700225 if (params->copy_spd != NULL)
226 params->copy_spd(params->pei_data);
227
Aaron Durbina2671612013-02-06 21:41:01 -0600228 sdram_initialize(params->pei_data);
229
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300230 timestamp_add_now(TS_AFTER_INITRAM);
231
Aaron Durbina2671612013-02-06 21:41:01 -0600232 post_code(0x3b);
233
234 intel_early_me_status();
235
236 quick_ram_check();
237 post_code(0x3e);
238
Aaron Durbinc0cbd6e2013-03-13 13:51:20 -0500239 if (!wake_from_s3) {
240 cbmem_initialize_empty();
241 /* Save data returned from MRC on non-S3 resumes. */
Aaron Durbin2ad1dba2013-02-07 00:51:18 -0600242 save_mrc_data(params->pei_data);
Aaron Durbin42e68562015-06-09 13:55:51 -0500243 } else if (cbmem_initialize()) {
244 #if CONFIG_HAVE_ACPI_RESUME
245 /* Failed S3 resume, reset to come up cleanly */
246 reset_system();
247 #endif
Aaron Durbina2671612013-02-06 21:41:01 -0600248 }
Aaron Durbinbf396ff2013-02-11 21:50:35 -0600249
250 handoff = romstage_handoff_find_or_add();
251 if (handoff != NULL)
252 handoff->s3_resume = wake_from_s3;
253 else
254 printk(BIOS_DEBUG, "Romstage handoff structure not added!\n");
255
Aaron Durbina2671612013-02-06 21:41:01 -0600256 post_code(0x3f);
Denis 'GNUtoo' Carikli0e92bb02016-02-20 17:32:03 +0100257 if (IS_ENABLED(CONFIG_LPC_TPM)) {
Vladimir Serbinenko0e90dae2015-05-18 10:29:06 +0200258 init_tpm(wake_from_s3);
259 }
Aaron Durbina2671612013-02-06 21:41:01 -0600260}
Aaron Durbin7492ec12013-02-08 22:18:04 -0600261
Kyösti Mälkkib37d01d32016-07-21 21:08:28 +0300262void asmlinkage romstage_after_car(void)
Aaron Durbin7492ec12013-02-08 22:18:04 -0600263{
Aaron Durbin7492ec12013-02-08 22:18:04 -0600264 /* Load the ramstage. */
Kyösti Mälkki65e8f642016-06-27 11:27:56 +0300265 run_ramstage();
Aaron Durbin7492ec12013-02-08 22:18:04 -0600266}
Aaron Durbinf7cdfe52013-02-16 00:05:52 -0600267
268
Aaron Durbinbd74a4b2015-03-06 23:17:33 -0600269#if IS_ENABLED(CONFIG_CACHE_RELOCATED_RAMSTAGE_OUTSIDE_CBMEM)
Aaron Durbinbd74a4b2015-03-06 23:17:33 -0600270void ramstage_cache_invalid(void)
Aaron Durbinf7cdfe52013-02-16 00:05:52 -0600271{
Aaron Durbin75e29742013-10-10 20:37:04 -0500272#if CONFIG_RESET_ON_INVALID_RAMSTAGE_CACHE
273 reset_system();
274#endif
Aaron Durbinf7cdfe52013-02-16 00:05:52 -0600275}
276#endif