blob: a35d9d814e8c213117843aafe052587b174e4e33 [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>
Elyes HAOUASc0567292019-04-28 17:57:47 +020019#include <cf9_reset.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070020#include <string.h>
21#include <arch/io.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020022#include <device/pci_ops.h>
Patrick Rudolph5709e032019-03-25 10:12:14 +010023#include <arch/cpu.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070024#include <cbmem.h>
25#include <arch/cbfs.h>
26#include <cbfs.h>
27#include <ip_checksum.h>
28#include <pc80/mc146818rtc.h>
29#include <device/pci_def.h>
Kyösti Mälkkib697c902019-01-30 08:19:49 +020030#include <lib.h>
Arthur Heymans7539b8c2017-12-24 10:42:57 +010031#include <mrc_cache.h>
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +010032#include <timestamp.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070033#include "raminit.h"
34#include "pei_data.h"
35#include "sandybridge.h"
Patrick Rudolph5709e032019-03-25 10:12:14 +010036#include "chip.h"
Philipp Deppenwiesefea24292017-10-17 17:02:29 +020037#include <security/vboot/vboot_common.h>
Patrick Georgi27fbbcf2019-04-23 12:33:23 +020038#include <southbridge/intel/bd82x6x/pch.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070039
40/* Management Engine is in the southbridge */
Elyes HAOUAS21b71ce62018-06-16 18:43:52 +020041#include <southbridge/intel/bd82x6x/me.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070042
43/*
44 * MRC scrambler seed offsets should be reserved in
45 * mainboard cmos.layout and not covered by checksum.
46 */
Julius Wernercd49cce2019-03-05 16:53:33 -080047#if CONFIG(USE_OPTION_TABLE)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070048#include "option_table.h"
49#define CMOS_OFFSET_MRC_SEED (CMOS_VSTART_mrc_scrambler_seed >> 3)
50#define CMOS_OFFSET_MRC_SEED_S3 (CMOS_VSTART_mrc_scrambler_seed_s3 >> 3)
51#define CMOS_OFFSET_MRC_SEED_CHK (CMOS_VSTART_mrc_scrambler_seed_chk >> 3)
52#else
53#define CMOS_OFFSET_MRC_SEED 152
54#define CMOS_OFFSET_MRC_SEED_S3 156
55#define CMOS_OFFSET_MRC_SEED_CHK 160
56#endif
57
Arthur Heymans7539b8c2017-12-24 10:42:57 +010058#define MRC_CACHE_VERSION 0
59
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070060void save_mrc_data(struct pei_data *pei_data)
61{
62 u16 c1, c2, checksum;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070063
64 /* Save the MRC S3 restore data to cbmem */
Arthur Heymans7539b8c2017-12-24 10:42:57 +010065 mrc_cache_stash_data(MRC_TRAINING_DATA, MRC_CACHE_VERSION,
66 pei_data->mrc_output,
67 pei_data->mrc_output_len);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070068
69 /* Save the MRC seed values to CMOS */
70 cmos_write32(CMOS_OFFSET_MRC_SEED, pei_data->scrambler_seed);
71 printk(BIOS_DEBUG, "Save scrambler seed 0x%08x to CMOS 0x%02x\n",
72 pei_data->scrambler_seed, CMOS_OFFSET_MRC_SEED);
73
74 cmos_write32(CMOS_OFFSET_MRC_SEED_S3, pei_data->scrambler_seed_s3);
75 printk(BIOS_DEBUG, "Save s3 scrambler seed 0x%08x to CMOS 0x%02x\n",
76 pei_data->scrambler_seed_s3, CMOS_OFFSET_MRC_SEED_S3);
77
78 /* Save a simple checksum of the seed values */
79 c1 = compute_ip_checksum((u8*)&pei_data->scrambler_seed,
80 sizeof(u32));
81 c2 = compute_ip_checksum((u8*)&pei_data->scrambler_seed_s3,
82 sizeof(u32));
83 checksum = add_ip_checksums(sizeof(u32), c1, c2);
84
85 cmos_write(checksum & 0xff, CMOS_OFFSET_MRC_SEED_CHK);
86 cmos_write((checksum >> 8) & 0xff, CMOS_OFFSET_MRC_SEED_CHK+1);
87}
88
89static void prepare_mrc_cache(struct pei_data *pei_data)
90{
Arthur Heymans7539b8c2017-12-24 10:42:57 +010091 struct region_device rdev;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070092 u16 c1, c2, checksum, seed_checksum;
93
94 // preset just in case there is an error
95 pei_data->mrc_input = NULL;
96 pei_data->mrc_input_len = 0;
97
98 /* Read scrambler seeds from CMOS */
99 pei_data->scrambler_seed = cmos_read32(CMOS_OFFSET_MRC_SEED);
100 printk(BIOS_DEBUG, "Read scrambler seed 0x%08x from CMOS 0x%02x\n",
101 pei_data->scrambler_seed, CMOS_OFFSET_MRC_SEED);
102
103 pei_data->scrambler_seed_s3 = cmos_read32(CMOS_OFFSET_MRC_SEED_S3);
104 printk(BIOS_DEBUG, "Read S3 scrambler seed 0x%08x from CMOS 0x%02x\n",
105 pei_data->scrambler_seed_s3, CMOS_OFFSET_MRC_SEED_S3);
106
107 /* Compute seed checksum and compare */
108 c1 = compute_ip_checksum((u8*)&pei_data->scrambler_seed,
109 sizeof(u32));
110 c2 = compute_ip_checksum((u8*)&pei_data->scrambler_seed_s3,
111 sizeof(u32));
112 checksum = add_ip_checksums(sizeof(u32), c1, c2);
113
114 seed_checksum = cmos_read(CMOS_OFFSET_MRC_SEED_CHK);
115 seed_checksum |= cmos_read(CMOS_OFFSET_MRC_SEED_CHK+1) << 8;
116
117 if (checksum != seed_checksum) {
118 printk(BIOS_ERR, "%s: invalid seed checksum\n", __func__);
119 pei_data->scrambler_seed = 0;
120 pei_data->scrambler_seed_s3 = 0;
121 return;
122 }
123
Arthur Heymans7539b8c2017-12-24 10:42:57 +0100124 if (mrc_cache_get_current(MRC_TRAINING_DATA, MRC_CACHE_VERSION,
125 &rdev)) {
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700126 /* error message printed in find_current_mrc_cache */
127 return;
128 }
129
Arthur Heymans7539b8c2017-12-24 10:42:57 +0100130 pei_data->mrc_input = rdev_mmap_full(&rdev);
131 pei_data->mrc_input_len = region_device_sz(&rdev);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700132
Arthur Heymans7539b8c2017-12-24 10:42:57 +0100133 printk(BIOS_DEBUG, "%s: at %p, size %x\n",
134 __func__, pei_data->mrc_input, pei_data->mrc_input_len);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700135}
136
Elyes HAOUAS448d9fb2018-05-22 12:51:27 +0200137static const char *ecc_decoder[] = {
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700138 "inactive",
139 "active on IO",
140 "disabled on IO",
141 "active"
142};
143
144/*
145 * Dump in the log memory controller configuration as read from the memory
146 * controller registers.
147 */
148static void report_memory_config(void)
149{
150 u32 addr_decoder_common, addr_decode_ch[2];
151 int i;
152
153 addr_decoder_common = MCHBAR32(0x5000);
154 addr_decode_ch[0] = MCHBAR32(0x5004);
155 addr_decode_ch[1] = MCHBAR32(0x5008);
156
157 printk(BIOS_DEBUG, "memcfg DDR3 clock %d MHz\n",
158 (MCHBAR32(0x5e04) * 13333 * 2 + 50)/100);
159 printk(BIOS_DEBUG, "memcfg channel assignment: A: %d, B % d, C % d\n",
160 addr_decoder_common & 3,
161 (addr_decoder_common >> 2) & 3,
162 (addr_decoder_common >> 4) & 3);
163
164 for (i = 0; i < ARRAY_SIZE(addr_decode_ch); i++) {
165 u32 ch_conf = addr_decode_ch[i];
166 printk(BIOS_DEBUG, "memcfg channel[%d] config (%8.8x):\n",
167 i, ch_conf);
168 printk(BIOS_DEBUG, " ECC %s\n",
169 ecc_decoder[(ch_conf >> 24) & 3]);
170 printk(BIOS_DEBUG, " enhanced interleave mode %s\n",
171 ((ch_conf >> 22) & 1) ? "on" : "off");
172 printk(BIOS_DEBUG, " rank interleave %s\n",
173 ((ch_conf >> 21) & 1) ? "on" : "off");
174 printk(BIOS_DEBUG, " DIMMA %d MB width x%d %s rank%s\n",
175 ((ch_conf >> 0) & 0xff) * 256,
176 ((ch_conf >> 19) & 1) ? 16 : 8,
177 ((ch_conf >> 17) & 1) ? "dual" : "single",
178 ((ch_conf >> 16) & 1) ? "" : ", selected");
179 printk(BIOS_DEBUG, " DIMMB %d MB width x%d %s rank%s\n",
180 ((ch_conf >> 8) & 0xff) * 256,
181 ((ch_conf >> 20) & 1) ? 16 : 8,
182 ((ch_conf >> 18) & 1) ? "dual" : "single",
183 ((ch_conf >> 16) & 1) ? ", selected" : "");
184 }
185}
186
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700187/**
188 * Find PEI executable in coreboot filesystem and execute it.
189 *
190 * @param pei_data: configuration data for UEFI PEI reference code
191 */
192void sdram_initialize(struct pei_data *pei_data)
193{
194 struct sys_info sysinfo;
Stefan Reinauer6a001132017-07-13 02:20:27 +0200195 int (*entry) (struct pei_data *pei_data) __attribute__((regparm(1)));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700196
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700197 /* Wait for ME to be ready */
198 intel_early_me_init();
199 intel_early_me_uma_size();
200
201 printk(BIOS_DEBUG, "Starting UEFI PEI System Agent\n");
202
203 memset(&sysinfo, 0, sizeof(sysinfo));
204
205 sysinfo.boot_path = pei_data->boot_mode;
206
207 /*
208 * Do not pass MRC data in for recovery mode boot,
209 * Always pass it in for S3 resume.
210 */
Furquan Shaikh0325dc62016-07-25 13:02:36 -0700211 if (!vboot_recovery_mode_enabled() || pei_data->boot_mode == 2)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700212 prepare_mrc_cache(pei_data);
213
214 /* If MRC data is not found we cannot continue S3 resume. */
215 if (pei_data->boot_mode == 2 && !pei_data->mrc_input) {
216 printk(BIOS_DEBUG, "Giving up in sdram_initialize: No MRC data\n");
Elyes HAOUASc0567292019-04-28 17:57:47 +0200217 system_reset();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700218 }
219
220 /* Pass console handler in pei_data */
221 pei_data->tx_byte = do_putchar;
222
223 /* Locate and call UEFI System Agent binary. */
224 entry = cbfs_boot_map_with_leak("mrc.bin", CBFS_TYPE_MRC, NULL);
225 if (entry) {
226 int rv;
227 rv = entry (pei_data);
228 if (rv) {
229 switch (rv) {
230 case -1:
231 printk(BIOS_ERR, "PEI version mismatch.\n");
232 break;
233 case -2:
234 printk(BIOS_ERR, "Invalid memory frequency.\n");
235 break;
236 default:
237 printk(BIOS_ERR, "MRC returned %x.\n", rv);
238 }
239 die("Nonzero MRC return value.\n");
240 }
241 } else {
242 die("UEFI PEI System Agent not found.\n");
243 }
244
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700245 /* mrc.bin reconfigures USB, so reinit it to have debug */
Julius Wernercd49cce2019-03-05 16:53:33 -0800246 if (CONFIG(USBDEBUG_IN_PRE_RAM))
Kyösti Mälkki63649d22018-12-29 09:40:40 +0200247 usbdebug_hw_init(true);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700248
249 /* For reference print the System Agent version
250 * after executing the UEFI PEI stage.
251 */
252 u32 version = MCHBAR32(0x5034);
253 printk(BIOS_DEBUG, "System Agent Version %d.%d.%d Build %d\n",
Elyes HAOUASa342f392018-10-17 10:56:26 +0200254 version >> 24, (version >> 16) & 0xff,
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700255 (version >> 8) & 0xff, version & 0xff);
256
257 /* Send ME init done for SandyBridge here. This is done
258 * inside the SystemAgent binary on IvyBridge. */
259 if (BASE_REV_SNB ==
260 (pci_read_config16(PCI_CPU_DEVICE, PCI_DEVICE_ID) & BASE_REV_MASK))
261 intel_early_me_init_done(ME_INIT_STATUS_SUCCESS);
262 else
263 intel_early_me_status();
264
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700265 report_memory_config();
266}
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100267
Kyösti Mälkkib697c902019-01-30 08:19:49 +0200268/* These are the location and structure of MRC_VAR data in CAR. */
269#define DCACHE_RAM_MRC_VAR_BASE \
270 (CONFIG_DCACHE_RAM_BASE + CONFIG_DCACHE_RAM_SIZE)
271
272struct mrc_var_data {
273 u32 acpi_timer_flag;
274 u32 pool_used;
275 u32 pool_base;
276 u32 tx_byte;
277 u32 reserved[4];
278} __packed;
279
Patrick Rudolph5709e032019-03-25 10:12:14 +0100280static void northbridge_fill_pei_data(struct pei_data *pei_data)
281{
282 pei_data->mchbar = (uintptr_t)DEFAULT_MCHBAR;
283 pei_data->dmibar = (uintptr_t)DEFAULT_DMIBAR;
284 pei_data->epbar = DEFAULT_EPBAR;
285 pei_data->pciexbar = CONFIG_MMCONF_BASE_ADDRESS;
286 pei_data->hpet_address = CONFIG_HPET_ADDRESS;
287 pei_data->thermalbase = 0xfed08000;
288 pei_data->system_type = get_platform_type() == PLATFORM_MOBILE ? 0 : 1;
289 pei_data->tseg_size = CONFIG_SMM_TSEG_SIZE;
290
291 if ((cpu_get_cpuid() & 0xffff0) == 0x306a0) {
292 const struct device *dev = pcidev_on_root(1, 0);
293 pei_data->pcie_init = dev && dev->enabled;
294 } else {
295 pei_data->pcie_init = 0;
296 }
297}
298
299static void southbridge_fill_pei_data(struct pei_data *pei_data)
300{
301 const struct device *dev = pcidev_on_root(0x19, 0);
302
303 pei_data->smbusbar = SMBUS_IO_BASE;
304 pei_data->wdbbar = 0x4000000;
305 pei_data->wdbsize = 0x1000;
306 pei_data->rcba = (uintptr_t)DEFAULT_RCBABASE;
307 pei_data->pmbase = DEFAULT_PMBASE;
308 pei_data->gpiobase = DEFAULT_GPIOBASE;
309 pei_data->gbe_enable = dev && dev->enabled;
310}
311
312static void devicetree_fill_pei_data(struct pei_data *pei_data)
313{
314 const struct northbridge_intel_sandybridge_config *cfg;
315
316 const struct device *dev = pcidev_on_root(0, 0);
317 if (!dev || !dev->chip_info)
318 return;
319
320 cfg = dev->chip_info;
321
322 switch (cfg->max_mem_clock_mhz) {
323 /* MRC only supports fixed numbers of frequencies */
324 default:
325 printk(BIOS_WARNING, "RAMINIT: Limiting DDR3 clock to 800 Mhz\n");
326 /* fallthrough */
327 case 400:
328 pei_data->max_ddr3_freq = 800;
329 break;
330 case 533:
331 pei_data->max_ddr3_freq = 1066;
332 break;
333 case 666:
334 pei_data->max_ddr3_freq = 1333;
335 break;
336 case 800:
337 pei_data->max_ddr3_freq = 1600;
338 break;
339
340 }
341
342 memcpy(pei_data->spd_addresses, cfg->spd_addresses,
343 sizeof(pei_data->spd_addresses));
344
345 memcpy(pei_data->ts_addresses, cfg->ts_addresses,
346 sizeof(pei_data->ts_addresses));
347
348 pei_data->ec_present = cfg->ec_present;
349 pei_data->ddr3lv_support = cfg->ddr3lv_support;
350
351 pei_data->nmode = cfg->nmode;
352 pei_data->ddr_refresh_rate_config = cfg->ddr_refresh_rate_config;
353
354 memcpy(pei_data->usb_port_config, cfg->usb_port_config,
355 sizeof(pei_data->usb_port_config));
356
357 pei_data->usb3.mode = cfg->usb3.mode;
358 pei_data->usb3.hs_port_switch_mask = cfg->usb3.hs_port_switch_mask;
359 pei_data->usb3.preboot_support = cfg->usb3.preboot_support;
360 pei_data->usb3.xhci_streams = cfg->usb3.xhci_streams;
361}
362
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100363void perform_raminit(int s3resume)
364{
365 int cbmem_was_initted;
366 struct pei_data pei_data;
Kyösti Mälkkib697c902019-01-30 08:19:49 +0200367 struct mrc_var_data *mrc_var;
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100368
369 /* Prepare USB controller early in S3 resume */
370 if (!mainboard_should_reset_usb(s3resume))
371 enable_usb_bar();
372
Patrick Rudolph5709e032019-03-25 10:12:14 +0100373 memset(&pei_data, 0, sizeof(pei_data));
374 pei_data.pei_version = PEI_VERSION,
375
376 northbridge_fill_pei_data(&pei_data);
377 southbridge_fill_pei_data(&pei_data);
378 devicetree_fill_pei_data(&pei_data);
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100379 mainboard_fill_pei_data(&pei_data);
380
381 post_code(0x3a);
Patrick Rudolph59b42552019-05-08 12:44:15 +0200382
Patrick Rudolph5709e032019-03-25 10:12:14 +0100383 /* Fill after mainboard_fill_pei_data as it might provide spd_data */
384 pei_data.dimm_channel0_disabled =
385 (!pei_data.spd_addresses[0] && !pei_data.spd_data[0][0]) +
386 (!pei_data.spd_addresses[1] && !pei_data.spd_data[1][0]) * 2;
387
388 pei_data.dimm_channel1_disabled =
389 (!pei_data.spd_addresses[2] && !pei_data.spd_data[2][0]) +
390 (!pei_data.spd_addresses[3] && !pei_data.spd_data[3][0]) * 2;
391
Patrick Rudolph59b42552019-05-08 12:44:15 +0200392 /* Fix spd_data. MRC only uses spd_data[0] and ignores the other */
393 for (size_t i = 1; i < ARRAY_SIZE(pei_data.spd_data); i++) {
394 if (pei_data.spd_data[i][0] && !pei_data.spd_data[0][0]) {
395 memcpy(pei_data.spd_data[0], pei_data.spd_data[i],
396 sizeof(pei_data.spd_data[0]));
397 } else if (pei_data.spd_data[i][0] && pei_data.spd_data[0][0]) {
398 if (memcmp(pei_data.spd_data[i], pei_data.spd_data[0],
399 sizeof(pei_data.spd_data[0])) != 0)
400 die("Onboard SPDs must match each other");
401 }
402 }
403
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100404 pei_data.boot_mode = s3resume ? 2 : 0;
405 timestamp_add_now(TS_BEFORE_INITRAM);
406 sdram_initialize(&pei_data);
Kyösti Mälkkib697c902019-01-30 08:19:49 +0200407
408 mrc_var = (void *)DCACHE_RAM_MRC_VAR_BASE;
409 /* Sanity check mrc_var location by verifying a known field. */
410 if (mrc_var->tx_byte == (uintptr_t)pei_data.tx_byte) {
411 printk(BIOS_DEBUG, "MRC_VAR pool occupied [%08x,%08x]\n",
412 mrc_var->pool_base,
413 mrc_var->pool_base + mrc_var->pool_used);
414 } else {
415 printk(BIOS_ERR, "Could not parse MRC_VAR data\n");
416 hexdump32(BIOS_ERR, mrc_var, sizeof(*mrc_var)/sizeof(u32));
417 }
418
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100419 cbmem_was_initted = !cbmem_recovery(s3resume);
420 if (!s3resume)
421 save_mrc_data(&pei_data);
422
423 if (s3resume && !cbmem_was_initted) {
424 /* Failed S3 resume, reset to come up cleanly */
Elyes HAOUASc0567292019-04-28 17:57:47 +0200425 system_reset();
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100426 }
427}