blob: 901a083c2b291588a81ee74a4ad0d808fa8663d0 [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
Stefan Reinauer6a001132017-07-13 02:20:27 +020016#include <compiler.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070017#include <console/console.h>
18#include <console/usb.h>
19#include <bootmode.h>
20#include <string.h>
21#include <arch/io.h>
22#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>
Alexander Couzens81c5c762016-03-09 03:13:45 +010028#include <northbridge/intel/common/mrc_cache.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070029#include <halt.h>
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +010030#include <timestamp.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070031#include "raminit.h"
32#include "pei_data.h"
33#include "sandybridge.h"
Philipp Deppenwiesefea24292017-10-17 17:02:29 +020034#include <security/vboot/vboot_common.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070035
36/* Management Engine is in the southbridge */
37#include "southbridge/intel/bd82x6x/me.h"
38
39/*
40 * MRC scrambler seed offsets should be reserved in
41 * mainboard cmos.layout and not covered by checksum.
42 */
Martin Roth33232602017-06-24 14:48:50 -060043#if IS_ENABLED(CONFIG_USE_OPTION_TABLE)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070044#include "option_table.h"
45#define CMOS_OFFSET_MRC_SEED (CMOS_VSTART_mrc_scrambler_seed >> 3)
46#define CMOS_OFFSET_MRC_SEED_S3 (CMOS_VSTART_mrc_scrambler_seed_s3 >> 3)
47#define CMOS_OFFSET_MRC_SEED_CHK (CMOS_VSTART_mrc_scrambler_seed_chk >> 3)
48#else
49#define CMOS_OFFSET_MRC_SEED 152
50#define CMOS_OFFSET_MRC_SEED_S3 156
51#define CMOS_OFFSET_MRC_SEED_CHK 160
52#endif
53
54void save_mrc_data(struct pei_data *pei_data)
55{
56 u16 c1, c2, checksum;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070057
58 /* Save the MRC S3 restore data to cbmem */
Patrick Rudolphbb9c90a2016-05-29 17:05:06 +020059 store_current_mrc_cache(pei_data->mrc_output, pei_data->mrc_output_len);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070060
61 /* Save the MRC seed values to CMOS */
62 cmos_write32(CMOS_OFFSET_MRC_SEED, pei_data->scrambler_seed);
63 printk(BIOS_DEBUG, "Save scrambler seed 0x%08x to CMOS 0x%02x\n",
64 pei_data->scrambler_seed, CMOS_OFFSET_MRC_SEED);
65
66 cmos_write32(CMOS_OFFSET_MRC_SEED_S3, pei_data->scrambler_seed_s3);
67 printk(BIOS_DEBUG, "Save s3 scrambler seed 0x%08x to CMOS 0x%02x\n",
68 pei_data->scrambler_seed_s3, CMOS_OFFSET_MRC_SEED_S3);
69
70 /* Save a simple checksum of the seed values */
71 c1 = compute_ip_checksum((u8*)&pei_data->scrambler_seed,
72 sizeof(u32));
73 c2 = compute_ip_checksum((u8*)&pei_data->scrambler_seed_s3,
74 sizeof(u32));
75 checksum = add_ip_checksums(sizeof(u32), c1, c2);
76
77 cmos_write(checksum & 0xff, CMOS_OFFSET_MRC_SEED_CHK);
78 cmos_write((checksum >> 8) & 0xff, CMOS_OFFSET_MRC_SEED_CHK+1);
79}
80
81static void prepare_mrc_cache(struct pei_data *pei_data)
82{
83 struct mrc_data_container *mrc_cache;
84 u16 c1, c2, checksum, seed_checksum;
85
86 // preset just in case there is an error
87 pei_data->mrc_input = NULL;
88 pei_data->mrc_input_len = 0;
89
90 /* Read scrambler seeds from CMOS */
91 pei_data->scrambler_seed = cmos_read32(CMOS_OFFSET_MRC_SEED);
92 printk(BIOS_DEBUG, "Read scrambler seed 0x%08x from CMOS 0x%02x\n",
93 pei_data->scrambler_seed, CMOS_OFFSET_MRC_SEED);
94
95 pei_data->scrambler_seed_s3 = cmos_read32(CMOS_OFFSET_MRC_SEED_S3);
96 printk(BIOS_DEBUG, "Read S3 scrambler seed 0x%08x from CMOS 0x%02x\n",
97 pei_data->scrambler_seed_s3, CMOS_OFFSET_MRC_SEED_S3);
98
99 /* Compute seed checksum and compare */
100 c1 = compute_ip_checksum((u8*)&pei_data->scrambler_seed,
101 sizeof(u32));
102 c2 = compute_ip_checksum((u8*)&pei_data->scrambler_seed_s3,
103 sizeof(u32));
104 checksum = add_ip_checksums(sizeof(u32), c1, c2);
105
106 seed_checksum = cmos_read(CMOS_OFFSET_MRC_SEED_CHK);
107 seed_checksum |= cmos_read(CMOS_OFFSET_MRC_SEED_CHK+1) << 8;
108
109 if (checksum != seed_checksum) {
110 printk(BIOS_ERR, "%s: invalid seed checksum\n", __func__);
111 pei_data->scrambler_seed = 0;
112 pei_data->scrambler_seed_s3 = 0;
113 return;
114 }
115
116 if ((mrc_cache = find_current_mrc_cache()) == NULL) {
117 /* error message printed in find_current_mrc_cache */
118 return;
119 }
120
121 pei_data->mrc_input = mrc_cache->mrc_data;
122 pei_data->mrc_input_len = mrc_cache->mrc_data_size;
123
124 printk(BIOS_DEBUG, "%s: at %p, size %x checksum %04x\n",
125 __func__, pei_data->mrc_input,
126 pei_data->mrc_input_len, mrc_cache->mrc_checksum);
127}
128
129static const char* ecc_decoder[] = {
130 "inactive",
131 "active on IO",
132 "disabled on IO",
133 "active"
134};
135
136/*
137 * Dump in the log memory controller configuration as read from the memory
138 * controller registers.
139 */
140static void report_memory_config(void)
141{
142 u32 addr_decoder_common, addr_decode_ch[2];
143 int i;
144
145 addr_decoder_common = MCHBAR32(0x5000);
146 addr_decode_ch[0] = MCHBAR32(0x5004);
147 addr_decode_ch[1] = MCHBAR32(0x5008);
148
149 printk(BIOS_DEBUG, "memcfg DDR3 clock %d MHz\n",
150 (MCHBAR32(0x5e04) * 13333 * 2 + 50)/100);
151 printk(BIOS_DEBUG, "memcfg channel assignment: A: %d, B % d, C % d\n",
152 addr_decoder_common & 3,
153 (addr_decoder_common >> 2) & 3,
154 (addr_decoder_common >> 4) & 3);
155
156 for (i = 0; i < ARRAY_SIZE(addr_decode_ch); i++) {
157 u32 ch_conf = addr_decode_ch[i];
158 printk(BIOS_DEBUG, "memcfg channel[%d] config (%8.8x):\n",
159 i, ch_conf);
160 printk(BIOS_DEBUG, " ECC %s\n",
161 ecc_decoder[(ch_conf >> 24) & 3]);
162 printk(BIOS_DEBUG, " enhanced interleave mode %s\n",
163 ((ch_conf >> 22) & 1) ? "on" : "off");
164 printk(BIOS_DEBUG, " rank interleave %s\n",
165 ((ch_conf >> 21) & 1) ? "on" : "off");
166 printk(BIOS_DEBUG, " DIMMA %d MB width x%d %s rank%s\n",
167 ((ch_conf >> 0) & 0xff) * 256,
168 ((ch_conf >> 19) & 1) ? 16 : 8,
169 ((ch_conf >> 17) & 1) ? "dual" : "single",
170 ((ch_conf >> 16) & 1) ? "" : ", selected");
171 printk(BIOS_DEBUG, " DIMMB %d MB width x%d %s rank%s\n",
172 ((ch_conf >> 8) & 0xff) * 256,
173 ((ch_conf >> 20) & 1) ? 16 : 8,
174 ((ch_conf >> 18) & 1) ? "dual" : "single",
175 ((ch_conf >> 16) & 1) ? ", selected" : "");
176 }
177}
178
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700179/**
180 * Find PEI executable in coreboot filesystem and execute it.
181 *
182 * @param pei_data: configuration data for UEFI PEI reference code
183 */
184void sdram_initialize(struct pei_data *pei_data)
185{
186 struct sys_info sysinfo;
Stefan Reinauer6a001132017-07-13 02:20:27 +0200187 int (*entry) (struct pei_data *pei_data) __attribute__((regparm(1)));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700188
189 report_platform_info();
190
191 /* Wait for ME to be ready */
192 intel_early_me_init();
193 intel_early_me_uma_size();
194
195 printk(BIOS_DEBUG, "Starting UEFI PEI System Agent\n");
196
197 memset(&sysinfo, 0, sizeof(sysinfo));
198
199 sysinfo.boot_path = pei_data->boot_mode;
200
201 /*
202 * Do not pass MRC data in for recovery mode boot,
203 * Always pass it in for S3 resume.
204 */
Furquan Shaikh0325dc62016-07-25 13:02:36 -0700205 if (!vboot_recovery_mode_enabled() || pei_data->boot_mode == 2)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700206 prepare_mrc_cache(pei_data);
207
208 /* If MRC data is not found we cannot continue S3 resume. */
209 if (pei_data->boot_mode == 2 && !pei_data->mrc_input) {
210 printk(BIOS_DEBUG, "Giving up in sdram_initialize: No MRC data\n");
211 outb(0x6, 0xcf9);
212 halt();
213 }
214
215 /* Pass console handler in pei_data */
216 pei_data->tx_byte = do_putchar;
217
218 /* Locate and call UEFI System Agent binary. */
219 entry = cbfs_boot_map_with_leak("mrc.bin", CBFS_TYPE_MRC, NULL);
220 if (entry) {
221 int rv;
222 rv = entry (pei_data);
223 if (rv) {
224 switch (rv) {
225 case -1:
226 printk(BIOS_ERR, "PEI version mismatch.\n");
227 break;
228 case -2:
229 printk(BIOS_ERR, "Invalid memory frequency.\n");
230 break;
231 default:
232 printk(BIOS_ERR, "MRC returned %x.\n", rv);
233 }
234 die("Nonzero MRC return value.\n");
235 }
236 } else {
237 die("UEFI PEI System Agent not found.\n");
238 }
239
Martin Roth33232602017-06-24 14:48:50 -0600240#if IS_ENABLED(CONFIG_USBDEBUG_IN_ROMSTAGE)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700241 /* mrc.bin reconfigures USB, so reinit it to have debug */
242 usbdebug_init();
243#endif
244
245 /* For reference print the System Agent version
246 * after executing the UEFI PEI stage.
247 */
248 u32 version = MCHBAR32(0x5034);
249 printk(BIOS_DEBUG, "System Agent Version %d.%d.%d Build %d\n",
250 version >> 24 , (version >> 16) & 0xff,
251 (version >> 8) & 0xff, version & 0xff);
252
253 /* Send ME init done for SandyBridge here. This is done
254 * inside the SystemAgent binary on IvyBridge. */
255 if (BASE_REV_SNB ==
256 (pci_read_config16(PCI_CPU_DEVICE, PCI_DEVICE_ID) & BASE_REV_MASK))
257 intel_early_me_init_done(ME_INIT_STATUS_SUCCESS);
258 else
259 intel_early_me_status();
260
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700261 report_memory_config();
262}
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100263
264void perform_raminit(int s3resume)
265{
266 int cbmem_was_initted;
267 struct pei_data pei_data;
268
269 /* Prepare USB controller early in S3 resume */
270 if (!mainboard_should_reset_usb(s3resume))
271 enable_usb_bar();
272
273 mainboard_fill_pei_data(&pei_data);
274
275 post_code(0x3a);
276 pei_data.boot_mode = s3resume ? 2 : 0;
277 timestamp_add_now(TS_BEFORE_INITRAM);
278 sdram_initialize(&pei_data);
279 cbmem_was_initted = !cbmem_recovery(s3resume);
280 if (!s3resume)
281 save_mrc_data(&pei_data);
282
283 if (s3resume && !cbmem_was_initted) {
284 /* Failed S3 resume, reset to come up cleanly */
285 outb(0x6, 0xcf9);
286 halt();
287 }
288}