blob: 1eb4f4361b734babab4e8fd3e366409f13e34152 [file] [log] [blame]
Aaron Durbin76c37002012-10-30 09:03:43 -05001/*
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.
Aaron Durbin76c37002012-10-30 09:03:43 -050014 */
15
16#include <console/console.h>
Iru Cai33642032019-06-11 14:24:43 +080017#include <console/usb.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050018#include <string.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050019#include <cbmem.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050020#include <cbfs.h>
Elyes HAOUAS82d46422019-04-28 18:01:48 +020021#include <cf9_reset.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050022#include <ip_checksum.h>
Matt DeVillier5aaa8ce2016-09-02 13:29:17 -050023#include <memory_info.h>
Arthur Heymansf300f362018-01-27 13:39:12 +010024#include <mrc_cache.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050025#include <device/pci_def.h>
Matt DeVillier5aaa8ce2016-09-02 13:29:17 -050026#include <device/dram/ddr3.h>
27#include <smbios.h>
28#include <spd.h>
Philipp Deppenwiesefea24292017-10-17 17:02:29 +020029#include <security/vboot/vboot_common.h>
Arthur Heymansf300f362018-01-27 13:39:12 +010030#include <commonlib/region.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050031#include "raminit.h"
32#include "pei_data.h"
33#include "haswell.h"
34
Arthur Heymansf300f362018-01-27 13:39:12 +010035#define MRC_CACHE_VERSION 1
36
Aaron Durbin2ad1dba2013-02-07 00:51:18 -060037void save_mrc_data(struct pei_data *pei_data)
Aaron Durbin76c37002012-10-30 09:03:43 -050038{
Aaron Durbin76c37002012-10-30 09:03:43 -050039 /* Save the MRC S3 restore data to cbmem */
Angel Pons1db5bc72020-01-15 00:49:03 +010040 mrc_cache_stash_data(MRC_TRAINING_DATA, MRC_CACHE_VERSION, pei_data->mrc_output,
41 pei_data->mrc_output_len);
Aaron Durbin76c37002012-10-30 09:03:43 -050042}
43
44static void prepare_mrc_cache(struct pei_data *pei_data)
45{
Arthur Heymansf300f362018-01-27 13:39:12 +010046 struct region_device rdev;
Aaron Durbin76c37002012-10-30 09:03:43 -050047
Angel Pons1db5bc72020-01-15 00:49:03 +010048 /* Preset just in case there is an error */
Aaron Durbin76c37002012-10-30 09:03:43 -050049 pei_data->mrc_input = NULL;
50 pei_data->mrc_input_len = 0;
51
Arthur Heymansf300f362018-01-27 13:39:12 +010052 if (mrc_cache_get_current(MRC_TRAINING_DATA, MRC_CACHE_VERSION, &rdev))
Angel Pons1db5bc72020-01-15 00:49:03 +010053 /* Error message printed in find_current_mrc_cache */
Aaron Durbin76c37002012-10-30 09:03:43 -050054 return;
Aaron Durbin76c37002012-10-30 09:03:43 -050055
Arthur Heymansf300f362018-01-27 13:39:12 +010056 pei_data->mrc_input = rdev_mmap_full(&rdev);
57 pei_data->mrc_input_len = region_device_sz(&rdev);
Aaron Durbin76c37002012-10-30 09:03:43 -050058
Angel Pons1db5bc72020-01-15 00:49:03 +010059 printk(BIOS_DEBUG, "%s: at %p, size %x\n", __func__, pei_data->mrc_input,
60 pei_data->mrc_input_len);
Aaron Durbin76c37002012-10-30 09:03:43 -050061}
62
Elyes HAOUAS448d9fb2018-05-22 12:51:27 +020063static const char *ecc_decoder[] = {
Aaron Durbin76c37002012-10-30 09:03:43 -050064 "inactive",
65 "active on IO",
66 "disabled on IO",
Angel Pons1db5bc72020-01-15 00:49:03 +010067 "active",
Aaron Durbin76c37002012-10-30 09:03:43 -050068};
69
Angel Pons1db5bc72020-01-15 00:49:03 +010070/* Print out the memory controller configuration, as per the values in its registers. */
Aaron Durbin76c37002012-10-30 09:03:43 -050071static void report_memory_config(void)
72{
Angel Pons1db5bc72020-01-15 00:49:03 +010073 u32 addr_decoder_common, addr_decode_chan[2];
Aaron Durbin76c37002012-10-30 09:03:43 -050074 int i;
75
Angel Pons1db5bc72020-01-15 00:49:03 +010076 addr_decoder_common = MCHBAR32(MAD_CHNL);
77 addr_decode_chan[0] = MCHBAR32(MAD_DIMM_CH0);
78 addr_decode_chan[1] = MCHBAR32(MAD_DIMM_CH1);
Aaron Durbin76c37002012-10-30 09:03:43 -050079
80 printk(BIOS_DEBUG, "memcfg DDR3 clock %d MHz\n",
Angel Pons1db5bc72020-01-15 00:49:03 +010081 (MCHBAR32(MC_BIOS_DATA) * 13333 * 2 + 50) / 100);
82
Aaron Durbin76c37002012-10-30 09:03:43 -050083 printk(BIOS_DEBUG, "memcfg channel assignment: A: %d, B % d, C % d\n",
Angel Pons1db5bc72020-01-15 00:49:03 +010084 (addr_decoder_common >> 0) & 3,
Aaron Durbin76c37002012-10-30 09:03:43 -050085 (addr_decoder_common >> 2) & 3,
86 (addr_decoder_common >> 4) & 3);
87
Angel Pons1db5bc72020-01-15 00:49:03 +010088 for (i = 0; i < ARRAY_SIZE(addr_decode_chan); i++) {
89 u32 ch_conf = addr_decode_chan[i];
90
91 printk(BIOS_DEBUG, "memcfg channel[%d] config (%8.8x):\n", i, ch_conf);
92 printk(BIOS_DEBUG, " ECC %s\n", ecc_decoder[(ch_conf >> 24) & 3]);
Aaron Durbin76c37002012-10-30 09:03:43 -050093 printk(BIOS_DEBUG, " enhanced interleave mode %s\n",
94 ((ch_conf >> 22) & 1) ? "on" : "off");
Angel Pons1db5bc72020-01-15 00:49:03 +010095
Aaron Durbin76c37002012-10-30 09:03:43 -050096 printk(BIOS_DEBUG, " rank interleave %s\n",
97 ((ch_conf >> 21) & 1) ? "on" : "off");
Angel Pons1db5bc72020-01-15 00:49:03 +010098
Duncan Laurie8d774022013-10-22 16:32:49 -070099 printk(BIOS_DEBUG, " DIMMA %d MB width %s %s rank%s\n",
Aaron Durbin76c37002012-10-30 09:03:43 -0500100 ((ch_conf >> 0) & 0xff) * 256,
Duncan Laurie8d774022013-10-22 16:32:49 -0700101 ((ch_conf >> 19) & 1) ? "x16" : "x8 or x32",
Aaron Durbin76c37002012-10-30 09:03:43 -0500102 ((ch_conf >> 17) & 1) ? "dual" : "single",
103 ((ch_conf >> 16) & 1) ? "" : ", selected");
Angel Pons1db5bc72020-01-15 00:49:03 +0100104
Duncan Laurie8d774022013-10-22 16:32:49 -0700105 printk(BIOS_DEBUG, " DIMMB %d MB width %s %s rank%s\n",
Aaron Durbin76c37002012-10-30 09:03:43 -0500106 ((ch_conf >> 8) & 0xff) * 256,
Ryan Salsamendidab81a42017-06-30 17:36:41 -0700107 ((ch_conf >> 20) & 1) ? "x16" : "x8 or x32",
Aaron Durbin76c37002012-10-30 09:03:43 -0500108 ((ch_conf >> 18) & 1) ? "dual" : "single",
109 ((ch_conf >> 16) & 1) ? ", selected" : "");
110 }
111}
112
113/**
114 * Find PEI executable in coreboot filesystem and execute it.
115 *
116 * @param pei_data: configuration data for UEFI PEI reference code
117 */
118void sdram_initialize(struct pei_data *pei_data)
119{
Aaron Durbin76c37002012-10-30 09:03:43 -0500120 unsigned long entry;
Arthur Heymans8da2fa02018-06-06 10:35:45 +0200121 uint32_t type = CBFS_TYPE_MRC;
122 struct cbfsf f;
Aaron Durbin76c37002012-10-30 09:03:43 -0500123
Aaron Durbin76c37002012-10-30 09:03:43 -0500124 printk(BIOS_DEBUG, "Starting UEFI PEI System Agent\n");
125
Angel Pons1db5bc72020-01-15 00:49:03 +0100126 /* Do not pass MRC data in for recovery mode boot, always pass it in for S3 resume */
Furquan Shaikh0325dc62016-07-25 13:02:36 -0700127 if (!vboot_recovery_mode_enabled() || pei_data->boot_mode == 2)
Aaron Durbin76c37002012-10-30 09:03:43 -0500128 prepare_mrc_cache(pei_data);
129
Angel Pons1db5bc72020-01-15 00:49:03 +0100130 /* If MRC data is not found, we cannot continue S3 resume */
Aaron Durbin76c37002012-10-30 09:03:43 -0500131 if (pei_data->boot_mode == 2 && !pei_data->mrc_input) {
Duncan Laurie727b5452013-08-08 16:28:41 -0700132 post_code(POST_RESUME_FAILURE);
Elyes HAOUAS3cd43272020-03-05 22:01:17 +0100133 printk(BIOS_DEBUG, "Giving up in %s: No MRC data\n", __func__);
Elyes HAOUAS82d46422019-04-28 18:01:48 +0200134 system_reset();
Aaron Durbin76c37002012-10-30 09:03:43 -0500135 }
136
137 /* Pass console handler in pei_data */
Kyösti Mälkki657e0be2014-02-04 19:03:57 +0200138 pei_data->tx_byte = do_putchar;
Aaron Durbin76c37002012-10-30 09:03:43 -0500139
Arthur Heymans8da2fa02018-06-06 10:35:45 +0200140 /*
Angel Pons1db5bc72020-01-15 00:49:03 +0100141 * Locate and call UEFI System Agent binary. The binary needs to be at a fixed offset
142 * in the flash and can therefore only reside in the COREBOOT fmap region.
Arthur Heymans8da2fa02018-06-06 10:35:45 +0200143 */
144 if (cbfs_locate_file_in_region(&f, "COREBOOT", "mrc.bin", &type) < 0)
145 die("mrc.bin not found!");
Angel Pons1db5bc72020-01-15 00:49:03 +0100146
Arthur Heymans8da2fa02018-06-06 10:35:45 +0200147 /* We don't care about leaking the mapping */
148 entry = (unsigned long)rdev_mmap_full(&f.data);
Aaron Durbin76c37002012-10-30 09:03:43 -0500149 if (entry) {
150 int rv;
Angel Pons1db5bc72020-01-15 00:49:03 +0100151 asm volatile ("call *%%ecx\n\t"
Aaron Durbin76c37002012-10-30 09:03:43 -0500152 :"=a" (rv) : "c" (entry), "a" (pei_data));
Iru Cai33642032019-06-11 14:24:43 +0800153
Angel Pons1db5bc72020-01-15 00:49:03 +0100154 /* The mrc.bin reconfigures USB, so usbdebug needs to be reinitialized */
Iru Cai33642032019-06-11 14:24:43 +0800155 if (CONFIG(USBDEBUG_IN_PRE_RAM))
156 usbdebug_hw_init(true);
157
Aaron Durbin76c37002012-10-30 09:03:43 -0500158 if (rv) {
159 switch (rv) {
160 case -1:
161 printk(BIOS_ERR, "PEI version mismatch.\n");
162 break;
163 case -2:
164 printk(BIOS_ERR, "Invalid memory frequency.\n");
165 break;
166 default:
167 printk(BIOS_ERR, "MRC returned %x.\n", rv);
168 }
Keith Shortbb41aba2019-05-16 14:07:43 -0600169 die_with_post_code(POST_INVALID_VENDOR_BINARY,
170 "Nonzero MRC return value.\n");
Aaron Durbin76c37002012-10-30 09:03:43 -0500171 }
172 } else {
173 die("UEFI PEI System Agent not found.\n");
174 }
175
Angel Pons1db5bc72020-01-15 00:49:03 +0100176 /* For reference, print the System Agent version after executing the UEFI PEI stage */
177 u32 version = MCHBAR32(MRC_REVISION);
Aaron Durbin76c37002012-10-30 09:03:43 -0500178 printk(BIOS_DEBUG, "System Agent Version %d.%d.%d Build %d\n",
Angel Pons1db5bc72020-01-15 00:49:03 +0100179 (version >> 24) & 0xff, (version >> 16) & 0xff,
180 (version >> 8) & 0xff, (version >> 0) & 0xff);
Aaron Durbin76c37002012-10-30 09:03:43 -0500181
Aaron Durbin76c37002012-10-30 09:03:43 -0500182 report_memory_config();
Aaron Durbin76c37002012-10-30 09:03:43 -0500183}
Matt DeVillier5aaa8ce2016-09-02 13:29:17 -0500184
185void setup_sdram_meminfo(struct pei_data *pei_data)
186{
Elyes HAOUASce83f312019-05-20 18:31:38 +0200187 u32 addr_decode_ch[2];
Angel Pons1db5bc72020-01-15 00:49:03 +0100188 struct memory_info *mem_info;
Matt DeVillier5aaa8ce2016-09-02 13:29:17 -0500189 struct dimm_info *dimm;
Angel Pons1db5bc72020-01-15 00:49:03 +0100190 int ddr_frequency, dimm_size, ch, d_num;
Matt DeVillier5aaa8ce2016-09-02 13:29:17 -0500191 int dimm_cnt = 0;
192
193 mem_info = cbmem_add(CBMEM_ID_MEMINFO, sizeof(struct memory_info));
Nico Huberacac02d2017-06-20 14:49:04 +0200194 if (!mem_info)
195 die("Failed to add memory info to CBMEM.\n");
Angel Pons1db5bc72020-01-15 00:49:03 +0100196
Matt DeVillier5aaa8ce2016-09-02 13:29:17 -0500197 memset(mem_info, 0, sizeof(struct memory_info));
198
Angel Pons1db5bc72020-01-15 00:49:03 +0100199 /* FIXME: Do we need to read MCHBAR32(MAD_CHNL) ? (Answer: Nope) */
200 MCHBAR32(MAD_CHNL);
201 addr_decode_ch[0] = MCHBAR32(MAD_DIMM_CH0);
202 addr_decode_ch[1] = MCHBAR32(MAD_DIMM_CH1);
Matt DeVillier5aaa8ce2016-09-02 13:29:17 -0500203
Angel Pons1db5bc72020-01-15 00:49:03 +0100204 ddr_frequency = (MCHBAR32(MC_BIOS_DATA) * 13333 * 2 + 50) / 100;
Matt DeVillier5aaa8ce2016-09-02 13:29:17 -0500205
206 for (ch = 0; ch < ARRAY_SIZE(addr_decode_ch); ch++) {
207 u32 ch_conf = addr_decode_ch[ch];
208 /* DIMMs A/B */
209 for (d_num = 0; d_num < 2; d_num++) {
210 dimm_size = ((ch_conf >> (d_num * 8)) & 0xff) * 256;
211 if (dimm_size) {
212 dimm = &mem_info->dimm[dimm_cnt];
213 dimm->dimm_size = dimm_size;
214 dimm->ddr_type = MEMORY_TYPE_DDR3;
215 dimm->ddr_frequency = ddr_frequency;
216 dimm->rank_per_dimm = 1 + ((ch_conf >> (17 + d_num)) & 1);
217 dimm->channel_num = ch;
218 dimm->dimm_num = d_num;
219 dimm->bank_locator = ch * 2;
220 memcpy(dimm->serial,
221 &pei_data->spd_data[dimm_cnt][SPD_DIMM_SERIAL_NUM],
222 SPD_DIMM_SERIAL_LEN);
223 memcpy(dimm->module_part_number,
224 &pei_data->spd_data[dimm_cnt][SPD_DIMM_PART_NUM],
225 SPD_DIMM_PART_LEN);
226 dimm->mod_id =
227 (pei_data->spd_data[dimm_cnt][SPD_DIMM_MOD_ID2] << 8) |
Angel Pons1db5bc72020-01-15 00:49:03 +0100228 (pei_data->spd_data[dimm_cnt][SPD_DIMM_MOD_ID1] & 0xff);
Matt DeVillier5aaa8ce2016-09-02 13:29:17 -0500229 dimm->mod_type = SPD_SODIMM;
230 dimm->bus_width = 0x3; /* 64-bit */
231 dimm_cnt++;
232 }
233 }
234 }
235 mem_info->dimm_cnt = dimm_cnt;
236}