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