blob: 852da7aa2648c6b40fba76497e9dfeb5db893afc [file] [log] [blame]
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2011 Google Inc.
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.
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070014 */
15
16#include <console/console.h>
17#include <console/usb.h>
18#include <bootmode.h>
19#include <string.h>
20#include <arch/io.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020021#include <device/pci_ops.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070022#include <cbmem.h>
23#include <arch/cbfs.h>
24#include <cbfs.h>
25#include <ip_checksum.h>
26#include <pc80/mc146818rtc.h>
27#include <device/pci_def.h>
Kyösti Mälkkib697c902019-01-30 08:19:49 +020028#include <lib.h>
Arthur Heymans7539b8c2017-12-24 10:42:57 +010029#include <mrc_cache.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070030#include <halt.h>
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +010031#include <timestamp.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070032#include "raminit.h"
33#include "pei_data.h"
34#include "sandybridge.h"
Philipp Deppenwiesefea24292017-10-17 17:02:29 +020035#include <security/vboot/vboot_common.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070036
37/* Management Engine is in the southbridge */
Elyes HAOUAS21b71ce62018-06-16 18:43:52 +020038#include <southbridge/intel/bd82x6x/me.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070039
40/*
41 * MRC scrambler seed offsets should be reserved in
42 * mainboard cmos.layout and not covered by checksum.
43 */
Julius Wernercd49cce2019-03-05 16:53:33 -080044#if CONFIG(USE_OPTION_TABLE)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070045#include "option_table.h"
46#define CMOS_OFFSET_MRC_SEED (CMOS_VSTART_mrc_scrambler_seed >> 3)
47#define CMOS_OFFSET_MRC_SEED_S3 (CMOS_VSTART_mrc_scrambler_seed_s3 >> 3)
48#define CMOS_OFFSET_MRC_SEED_CHK (CMOS_VSTART_mrc_scrambler_seed_chk >> 3)
49#else
50#define CMOS_OFFSET_MRC_SEED 152
51#define CMOS_OFFSET_MRC_SEED_S3 156
52#define CMOS_OFFSET_MRC_SEED_CHK 160
53#endif
54
Arthur Heymans7539b8c2017-12-24 10:42:57 +010055#define MRC_CACHE_VERSION 0
56
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070057void save_mrc_data(struct pei_data *pei_data)
58{
59 u16 c1, c2, checksum;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070060
61 /* Save the MRC S3 restore data to cbmem */
Arthur Heymans7539b8c2017-12-24 10:42:57 +010062 mrc_cache_stash_data(MRC_TRAINING_DATA, MRC_CACHE_VERSION,
63 pei_data->mrc_output,
64 pei_data->mrc_output_len);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070065
66 /* Save the MRC seed values to CMOS */
67 cmos_write32(CMOS_OFFSET_MRC_SEED, pei_data->scrambler_seed);
68 printk(BIOS_DEBUG, "Save scrambler seed 0x%08x to CMOS 0x%02x\n",
69 pei_data->scrambler_seed, CMOS_OFFSET_MRC_SEED);
70
71 cmos_write32(CMOS_OFFSET_MRC_SEED_S3, pei_data->scrambler_seed_s3);
72 printk(BIOS_DEBUG, "Save s3 scrambler seed 0x%08x to CMOS 0x%02x\n",
73 pei_data->scrambler_seed_s3, CMOS_OFFSET_MRC_SEED_S3);
74
75 /* Save a simple checksum of the seed values */
76 c1 = compute_ip_checksum((u8*)&pei_data->scrambler_seed,
77 sizeof(u32));
78 c2 = compute_ip_checksum((u8*)&pei_data->scrambler_seed_s3,
79 sizeof(u32));
80 checksum = add_ip_checksums(sizeof(u32), c1, c2);
81
82 cmos_write(checksum & 0xff, CMOS_OFFSET_MRC_SEED_CHK);
83 cmos_write((checksum >> 8) & 0xff, CMOS_OFFSET_MRC_SEED_CHK+1);
84}
85
86static void prepare_mrc_cache(struct pei_data *pei_data)
87{
Arthur Heymans7539b8c2017-12-24 10:42:57 +010088 struct region_device rdev;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070089 u16 c1, c2, checksum, seed_checksum;
90
91 // preset just in case there is an error
92 pei_data->mrc_input = NULL;
93 pei_data->mrc_input_len = 0;
94
95 /* Read scrambler seeds from CMOS */
96 pei_data->scrambler_seed = cmos_read32(CMOS_OFFSET_MRC_SEED);
97 printk(BIOS_DEBUG, "Read scrambler seed 0x%08x from CMOS 0x%02x\n",
98 pei_data->scrambler_seed, CMOS_OFFSET_MRC_SEED);
99
100 pei_data->scrambler_seed_s3 = cmos_read32(CMOS_OFFSET_MRC_SEED_S3);
101 printk(BIOS_DEBUG, "Read S3 scrambler seed 0x%08x from CMOS 0x%02x\n",
102 pei_data->scrambler_seed_s3, CMOS_OFFSET_MRC_SEED_S3);
103
104 /* Compute seed checksum and compare */
105 c1 = compute_ip_checksum((u8*)&pei_data->scrambler_seed,
106 sizeof(u32));
107 c2 = compute_ip_checksum((u8*)&pei_data->scrambler_seed_s3,
108 sizeof(u32));
109 checksum = add_ip_checksums(sizeof(u32), c1, c2);
110
111 seed_checksum = cmos_read(CMOS_OFFSET_MRC_SEED_CHK);
112 seed_checksum |= cmos_read(CMOS_OFFSET_MRC_SEED_CHK+1) << 8;
113
114 if (checksum != seed_checksum) {
115 printk(BIOS_ERR, "%s: invalid seed checksum\n", __func__);
116 pei_data->scrambler_seed = 0;
117 pei_data->scrambler_seed_s3 = 0;
118 return;
119 }
120
Arthur Heymans7539b8c2017-12-24 10:42:57 +0100121 if (mrc_cache_get_current(MRC_TRAINING_DATA, MRC_CACHE_VERSION,
122 &rdev)) {
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700123 /* error message printed in find_current_mrc_cache */
124 return;
125 }
126
Arthur Heymans7539b8c2017-12-24 10:42:57 +0100127 pei_data->mrc_input = rdev_mmap_full(&rdev);
128 pei_data->mrc_input_len = region_device_sz(&rdev);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700129
Arthur Heymans7539b8c2017-12-24 10:42:57 +0100130 printk(BIOS_DEBUG, "%s: at %p, size %x\n",
131 __func__, pei_data->mrc_input, pei_data->mrc_input_len);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700132}
133
Elyes HAOUAS448d9fb2018-05-22 12:51:27 +0200134static const char *ecc_decoder[] = {
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700135 "inactive",
136 "active on IO",
137 "disabled on IO",
138 "active"
139};
140
141/*
142 * Dump in the log memory controller configuration as read from the memory
143 * controller registers.
144 */
145static void report_memory_config(void)
146{
147 u32 addr_decoder_common, addr_decode_ch[2];
148 int i;
149
150 addr_decoder_common = MCHBAR32(0x5000);
151 addr_decode_ch[0] = MCHBAR32(0x5004);
152 addr_decode_ch[1] = MCHBAR32(0x5008);
153
154 printk(BIOS_DEBUG, "memcfg DDR3 clock %d MHz\n",
155 (MCHBAR32(0x5e04) * 13333 * 2 + 50)/100);
156 printk(BIOS_DEBUG, "memcfg channel assignment: A: %d, B % d, C % d\n",
157 addr_decoder_common & 3,
158 (addr_decoder_common >> 2) & 3,
159 (addr_decoder_common >> 4) & 3);
160
161 for (i = 0; i < ARRAY_SIZE(addr_decode_ch); i++) {
162 u32 ch_conf = addr_decode_ch[i];
163 printk(BIOS_DEBUG, "memcfg channel[%d] config (%8.8x):\n",
164 i, ch_conf);
165 printk(BIOS_DEBUG, " ECC %s\n",
166 ecc_decoder[(ch_conf >> 24) & 3]);
167 printk(BIOS_DEBUG, " enhanced interleave mode %s\n",
168 ((ch_conf >> 22) & 1) ? "on" : "off");
169 printk(BIOS_DEBUG, " rank interleave %s\n",
170 ((ch_conf >> 21) & 1) ? "on" : "off");
171 printk(BIOS_DEBUG, " DIMMA %d MB width x%d %s rank%s\n",
172 ((ch_conf >> 0) & 0xff) * 256,
173 ((ch_conf >> 19) & 1) ? 16 : 8,
174 ((ch_conf >> 17) & 1) ? "dual" : "single",
175 ((ch_conf >> 16) & 1) ? "" : ", selected");
176 printk(BIOS_DEBUG, " DIMMB %d MB width x%d %s rank%s\n",
177 ((ch_conf >> 8) & 0xff) * 256,
178 ((ch_conf >> 20) & 1) ? 16 : 8,
179 ((ch_conf >> 18) & 1) ? "dual" : "single",
180 ((ch_conf >> 16) & 1) ? ", selected" : "");
181 }
182}
183
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700184/**
185 * Find PEI executable in coreboot filesystem and execute it.
186 *
187 * @param pei_data: configuration data for UEFI PEI reference code
188 */
189void sdram_initialize(struct pei_data *pei_data)
190{
191 struct sys_info sysinfo;
Stefan Reinauer6a001132017-07-13 02:20:27 +0200192 int (*entry) (struct pei_data *pei_data) __attribute__((regparm(1)));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700193
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700194 /* Wait for ME to be ready */
195 intel_early_me_init();
196 intel_early_me_uma_size();
197
198 printk(BIOS_DEBUG, "Starting UEFI PEI System Agent\n");
199
200 memset(&sysinfo, 0, sizeof(sysinfo));
201
202 sysinfo.boot_path = pei_data->boot_mode;
203
204 /*
205 * Do not pass MRC data in for recovery mode boot,
206 * Always pass it in for S3 resume.
207 */
Furquan Shaikh0325dc62016-07-25 13:02:36 -0700208 if (!vboot_recovery_mode_enabled() || pei_data->boot_mode == 2)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700209 prepare_mrc_cache(pei_data);
210
211 /* If MRC data is not found we cannot continue S3 resume. */
212 if (pei_data->boot_mode == 2 && !pei_data->mrc_input) {
213 printk(BIOS_DEBUG, "Giving up in sdram_initialize: No MRC data\n");
214 outb(0x6, 0xcf9);
215 halt();
216 }
217
218 /* Pass console handler in pei_data */
219 pei_data->tx_byte = do_putchar;
220
221 /* Locate and call UEFI System Agent binary. */
222 entry = cbfs_boot_map_with_leak("mrc.bin", CBFS_TYPE_MRC, NULL);
223 if (entry) {
224 int rv;
225 rv = entry (pei_data);
226 if (rv) {
227 switch (rv) {
228 case -1:
229 printk(BIOS_ERR, "PEI version mismatch.\n");
230 break;
231 case -2:
232 printk(BIOS_ERR, "Invalid memory frequency.\n");
233 break;
234 default:
235 printk(BIOS_ERR, "MRC returned %x.\n", rv);
236 }
237 die("Nonzero MRC return value.\n");
238 }
239 } else {
240 die("UEFI PEI System Agent not found.\n");
241 }
242
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700243 /* mrc.bin reconfigures USB, so reinit it to have debug */
Julius Wernercd49cce2019-03-05 16:53:33 -0800244 if (CONFIG(USBDEBUG_IN_PRE_RAM))
Kyösti Mälkki63649d22018-12-29 09:40:40 +0200245 usbdebug_hw_init(true);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700246
247 /* For reference print the System Agent version
248 * after executing the UEFI PEI stage.
249 */
250 u32 version = MCHBAR32(0x5034);
251 printk(BIOS_DEBUG, "System Agent Version %d.%d.%d Build %d\n",
Elyes HAOUASa342f392018-10-17 10:56:26 +0200252 version >> 24, (version >> 16) & 0xff,
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700253 (version >> 8) & 0xff, version & 0xff);
254
255 /* Send ME init done for SandyBridge here. This is done
256 * inside the SystemAgent binary on IvyBridge. */
257 if (BASE_REV_SNB ==
258 (pci_read_config16(PCI_CPU_DEVICE, PCI_DEVICE_ID) & BASE_REV_MASK))
259 intel_early_me_init_done(ME_INIT_STATUS_SUCCESS);
260 else
261 intel_early_me_status();
262
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700263 report_memory_config();
264}
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100265
Kyösti Mälkkib697c902019-01-30 08:19:49 +0200266/* These are the location and structure of MRC_VAR data in CAR. */
267#define DCACHE_RAM_MRC_VAR_BASE \
268 (CONFIG_DCACHE_RAM_BASE + CONFIG_DCACHE_RAM_SIZE)
269
270struct mrc_var_data {
271 u32 acpi_timer_flag;
272 u32 pool_used;
273 u32 pool_base;
274 u32 tx_byte;
275 u32 reserved[4];
276} __packed;
277
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100278void perform_raminit(int s3resume)
279{
280 int cbmem_was_initted;
281 struct pei_data pei_data;
Kyösti Mälkkib697c902019-01-30 08:19:49 +0200282 struct mrc_var_data *mrc_var;
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100283
284 /* Prepare USB controller early in S3 resume */
285 if (!mainboard_should_reset_usb(s3resume))
286 enable_usb_bar();
287
288 mainboard_fill_pei_data(&pei_data);
289
290 post_code(0x3a);
291 pei_data.boot_mode = s3resume ? 2 : 0;
292 timestamp_add_now(TS_BEFORE_INITRAM);
293 sdram_initialize(&pei_data);
Kyösti Mälkkib697c902019-01-30 08:19:49 +0200294
295 mrc_var = (void *)DCACHE_RAM_MRC_VAR_BASE;
296 /* Sanity check mrc_var location by verifying a known field. */
297 if (mrc_var->tx_byte == (uintptr_t)pei_data.tx_byte) {
298 printk(BIOS_DEBUG, "MRC_VAR pool occupied [%08x,%08x]\n",
299 mrc_var->pool_base,
300 mrc_var->pool_base + mrc_var->pool_used);
301 } else {
302 printk(BIOS_ERR, "Could not parse MRC_VAR data\n");
303 hexdump32(BIOS_ERR, mrc_var, sizeof(*mrc_var)/sizeof(u32));
304 }
305
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100306 cbmem_was_initted = !cbmem_recovery(s3resume);
307 if (!s3resume)
308 save_mrc_data(&pei_data);
309
310 if (s3resume && !cbmem_was_initted) {
311 /* Failed S3 resume, reset to come up cleanly */
312 outb(0x6, 0xcf9);
313 halt();
314 }
315}