blob: 5d679541251f74b389ecf4ad474bde0b6615c8c8 [file] [log] [blame]
Angel Pons4b429832020-04-02 23:48:50 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Aaron Durbin76c37002012-10-30 09:03:43 -05002
3#include <console/console.h>
Iru Cai33642032019-06-11 14:24:43 +08004#include <console/usb.h>
Aaron Durbin76c37002012-10-30 09:03:43 -05005#include <string.h>
Aaron Durbin76c37002012-10-30 09:03:43 -05006#include <cbmem.h>
Aaron Durbin76c37002012-10-30 09:03:43 -05007#include <cbfs.h>
Elyes HAOUAS82d46422019-04-28 18:01:48 +02008#include <cf9_reset.h>
Aaron Durbin76c37002012-10-30 09:03:43 -05009#include <ip_checksum.h>
Matt DeVillier5aaa8ce2016-09-02 13:29:17 -050010#include <memory_info.h>
Arthur Heymansf300f362018-01-27 13:39:12 +010011#include <mrc_cache.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050012#include <device/pci_def.h>
Matt DeVillier5aaa8ce2016-09-02 13:29:17 -050013#include <device/dram/ddr3.h>
14#include <smbios.h>
15#include <spd.h>
Philipp Deppenwiesefea24292017-10-17 17:02:29 +020016#include <security/vboot/vboot_common.h>
Arthur Heymansf300f362018-01-27 13:39:12 +010017#include <commonlib/region.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050018#include "raminit.h"
19#include "pei_data.h"
20#include "haswell.h"
21
Arthur Heymansf300f362018-01-27 13:39:12 +010022#define MRC_CACHE_VERSION 1
23
Aaron Durbin2ad1dba2013-02-07 00:51:18 -060024void save_mrc_data(struct pei_data *pei_data)
Aaron Durbin76c37002012-10-30 09:03:43 -050025{
Aaron Durbin76c37002012-10-30 09:03:43 -050026 /* Save the MRC S3 restore data to cbmem */
Angel Pons1db5bc72020-01-15 00:49:03 +010027 mrc_cache_stash_data(MRC_TRAINING_DATA, MRC_CACHE_VERSION, pei_data->mrc_output,
28 pei_data->mrc_output_len);
Aaron Durbin76c37002012-10-30 09:03:43 -050029}
30
31static void prepare_mrc_cache(struct pei_data *pei_data)
32{
Arthur Heymansf300f362018-01-27 13:39:12 +010033 struct region_device rdev;
Aaron Durbin76c37002012-10-30 09:03:43 -050034
Angel Pons1db5bc72020-01-15 00:49:03 +010035 /* Preset just in case there is an error */
Aaron Durbin76c37002012-10-30 09:03:43 -050036 pei_data->mrc_input = NULL;
37 pei_data->mrc_input_len = 0;
38
Arthur Heymansf300f362018-01-27 13:39:12 +010039 if (mrc_cache_get_current(MRC_TRAINING_DATA, MRC_CACHE_VERSION, &rdev))
Angel Pons1db5bc72020-01-15 00:49:03 +010040 /* Error message printed in find_current_mrc_cache */
Aaron Durbin76c37002012-10-30 09:03:43 -050041 return;
Aaron Durbin76c37002012-10-30 09:03:43 -050042
Arthur Heymansf300f362018-01-27 13:39:12 +010043 pei_data->mrc_input = rdev_mmap_full(&rdev);
44 pei_data->mrc_input_len = region_device_sz(&rdev);
Aaron Durbin76c37002012-10-30 09:03:43 -050045
Angel Pons1db5bc72020-01-15 00:49:03 +010046 printk(BIOS_DEBUG, "%s: at %p, size %x\n", __func__, pei_data->mrc_input,
47 pei_data->mrc_input_len);
Aaron Durbin76c37002012-10-30 09:03:43 -050048}
49
Elyes HAOUAS448d9fb2018-05-22 12:51:27 +020050static const char *ecc_decoder[] = {
Aaron Durbin76c37002012-10-30 09:03:43 -050051 "inactive",
52 "active on IO",
53 "disabled on IO",
Angel Pons1db5bc72020-01-15 00:49:03 +010054 "active",
Aaron Durbin76c37002012-10-30 09:03:43 -050055};
56
Angel Pons1db5bc72020-01-15 00:49:03 +010057/* Print out the memory controller configuration, as per the values in its registers. */
Aaron Durbin76c37002012-10-30 09:03:43 -050058static void report_memory_config(void)
59{
Angel Pons1db5bc72020-01-15 00:49:03 +010060 u32 addr_decoder_common, addr_decode_chan[2];
Aaron Durbin76c37002012-10-30 09:03:43 -050061 int i;
62
Angel Pons1db5bc72020-01-15 00:49:03 +010063 addr_decoder_common = MCHBAR32(MAD_CHNL);
64 addr_decode_chan[0] = MCHBAR32(MAD_DIMM_CH0);
65 addr_decode_chan[1] = MCHBAR32(MAD_DIMM_CH1);
Aaron Durbin76c37002012-10-30 09:03:43 -050066
67 printk(BIOS_DEBUG, "memcfg DDR3 clock %d MHz\n",
Angel Pons1db5bc72020-01-15 00:49:03 +010068 (MCHBAR32(MC_BIOS_DATA) * 13333 * 2 + 50) / 100);
69
Aaron Durbin76c37002012-10-30 09:03:43 -050070 printk(BIOS_DEBUG, "memcfg channel assignment: A: %d, B % d, C % d\n",
Angel Pons1db5bc72020-01-15 00:49:03 +010071 (addr_decoder_common >> 0) & 3,
Aaron Durbin76c37002012-10-30 09:03:43 -050072 (addr_decoder_common >> 2) & 3,
73 (addr_decoder_common >> 4) & 3);
74
Angel Pons1db5bc72020-01-15 00:49:03 +010075 for (i = 0; i < ARRAY_SIZE(addr_decode_chan); i++) {
76 u32 ch_conf = addr_decode_chan[i];
77
78 printk(BIOS_DEBUG, "memcfg channel[%d] config (%8.8x):\n", i, ch_conf);
79 printk(BIOS_DEBUG, " ECC %s\n", ecc_decoder[(ch_conf >> 24) & 3]);
Aaron Durbin76c37002012-10-30 09:03:43 -050080 printk(BIOS_DEBUG, " enhanced interleave mode %s\n",
81 ((ch_conf >> 22) & 1) ? "on" : "off");
Angel Pons1db5bc72020-01-15 00:49:03 +010082
Aaron Durbin76c37002012-10-30 09:03:43 -050083 printk(BIOS_DEBUG, " rank interleave %s\n",
84 ((ch_conf >> 21) & 1) ? "on" : "off");
Angel Pons1db5bc72020-01-15 00:49:03 +010085
Duncan Laurie8d774022013-10-22 16:32:49 -070086 printk(BIOS_DEBUG, " DIMMA %d MB width %s %s rank%s\n",
Aaron Durbin76c37002012-10-30 09:03:43 -050087 ((ch_conf >> 0) & 0xff) * 256,
Duncan Laurie8d774022013-10-22 16:32:49 -070088 ((ch_conf >> 19) & 1) ? "x16" : "x8 or x32",
Aaron Durbin76c37002012-10-30 09:03:43 -050089 ((ch_conf >> 17) & 1) ? "dual" : "single",
90 ((ch_conf >> 16) & 1) ? "" : ", selected");
Angel Pons1db5bc72020-01-15 00:49:03 +010091
Duncan Laurie8d774022013-10-22 16:32:49 -070092 printk(BIOS_DEBUG, " DIMMB %d MB width %s %s rank%s\n",
Aaron Durbin76c37002012-10-30 09:03:43 -050093 ((ch_conf >> 8) & 0xff) * 256,
Ryan Salsamendidab81a42017-06-30 17:36:41 -070094 ((ch_conf >> 20) & 1) ? "x16" : "x8 or x32",
Aaron Durbin76c37002012-10-30 09:03:43 -050095 ((ch_conf >> 18) & 1) ? "dual" : "single",
96 ((ch_conf >> 16) & 1) ? ", selected" : "");
97 }
98}
99
100/**
101 * Find PEI executable in coreboot filesystem and execute it.
102 *
103 * @param pei_data: configuration data for UEFI PEI reference code
104 */
105void sdram_initialize(struct pei_data *pei_data)
106{
Aaron Durbin76c37002012-10-30 09:03:43 -0500107 unsigned long entry;
Arthur Heymans8da2fa02018-06-06 10:35:45 +0200108 uint32_t type = CBFS_TYPE_MRC;
109 struct cbfsf f;
Aaron Durbin76c37002012-10-30 09:03:43 -0500110
Aaron Durbin76c37002012-10-30 09:03:43 -0500111 printk(BIOS_DEBUG, "Starting UEFI PEI System Agent\n");
112
Angel Pons1db5bc72020-01-15 00:49:03 +0100113 /* Do not pass MRC data in for recovery mode boot, always pass it in for S3 resume */
Julius Werner29fbfcc2020-03-02 15:54:43 -0800114 if (!(CONFIG(HASWELL_VBOOT_IN_BOOTBLOCK) && vboot_recovery_mode_enabled())
115 || pei_data->boot_mode == 2)
Aaron Durbin76c37002012-10-30 09:03:43 -0500116 prepare_mrc_cache(pei_data);
117
Angel Pons1db5bc72020-01-15 00:49:03 +0100118 /* If MRC data is not found, we cannot continue S3 resume */
Aaron Durbin76c37002012-10-30 09:03:43 -0500119 if (pei_data->boot_mode == 2 && !pei_data->mrc_input) {
Duncan Laurie727b5452013-08-08 16:28:41 -0700120 post_code(POST_RESUME_FAILURE);
Elyes HAOUAS3cd43272020-03-05 22:01:17 +0100121 printk(BIOS_DEBUG, "Giving up in %s: No MRC data\n", __func__);
Elyes HAOUAS82d46422019-04-28 18:01:48 +0200122 system_reset();
Aaron Durbin76c37002012-10-30 09:03:43 -0500123 }
124
125 /* Pass console handler in pei_data */
Kyösti Mälkki657e0be2014-02-04 19:03:57 +0200126 pei_data->tx_byte = do_putchar;
Aaron Durbin76c37002012-10-30 09:03:43 -0500127
Arthur Heymans8da2fa02018-06-06 10:35:45 +0200128 /*
Angel Pons1db5bc72020-01-15 00:49:03 +0100129 * Locate and call UEFI System Agent binary. The binary needs to be at a fixed offset
130 * in the flash and can therefore only reside in the COREBOOT fmap region.
Arthur Heymans8da2fa02018-06-06 10:35:45 +0200131 */
132 if (cbfs_locate_file_in_region(&f, "COREBOOT", "mrc.bin", &type) < 0)
133 die("mrc.bin not found!");
Angel Pons1db5bc72020-01-15 00:49:03 +0100134
Arthur Heymans8da2fa02018-06-06 10:35:45 +0200135 /* We don't care about leaking the mapping */
136 entry = (unsigned long)rdev_mmap_full(&f.data);
Aaron Durbin76c37002012-10-30 09:03:43 -0500137 if (entry) {
138 int rv;
Angel Pons1db5bc72020-01-15 00:49:03 +0100139 asm volatile ("call *%%ecx\n\t"
Aaron Durbin76c37002012-10-30 09:03:43 -0500140 :"=a" (rv) : "c" (entry), "a" (pei_data));
Iru Cai33642032019-06-11 14:24:43 +0800141
Angel Pons1db5bc72020-01-15 00:49:03 +0100142 /* The mrc.bin reconfigures USB, so usbdebug needs to be reinitialized */
Iru Cai33642032019-06-11 14:24:43 +0800143 if (CONFIG(USBDEBUG_IN_PRE_RAM))
144 usbdebug_hw_init(true);
145
Aaron Durbin76c37002012-10-30 09:03:43 -0500146 if (rv) {
147 switch (rv) {
148 case -1:
149 printk(BIOS_ERR, "PEI version mismatch.\n");
150 break;
151 case -2:
152 printk(BIOS_ERR, "Invalid memory frequency.\n");
153 break;
154 default:
155 printk(BIOS_ERR, "MRC returned %x.\n", rv);
156 }
Keith Shortbb41aba2019-05-16 14:07:43 -0600157 die_with_post_code(POST_INVALID_VENDOR_BINARY,
158 "Nonzero MRC return value.\n");
Aaron Durbin76c37002012-10-30 09:03:43 -0500159 }
160 } else {
161 die("UEFI PEI System Agent not found.\n");
162 }
163
Angel Pons1db5bc72020-01-15 00:49:03 +0100164 /* For reference, print the System Agent version after executing the UEFI PEI stage */
165 u32 version = MCHBAR32(MRC_REVISION);
Aaron Durbin76c37002012-10-30 09:03:43 -0500166 printk(BIOS_DEBUG, "System Agent Version %d.%d.%d Build %d\n",
Angel Pons1db5bc72020-01-15 00:49:03 +0100167 (version >> 24) & 0xff, (version >> 16) & 0xff,
168 (version >> 8) & 0xff, (version >> 0) & 0xff);
Aaron Durbin76c37002012-10-30 09:03:43 -0500169
Aaron Durbin76c37002012-10-30 09:03:43 -0500170 report_memory_config();
Aaron Durbin76c37002012-10-30 09:03:43 -0500171}
Matt DeVillier5aaa8ce2016-09-02 13:29:17 -0500172
173void setup_sdram_meminfo(struct pei_data *pei_data)
174{
Elyes HAOUASce83f312019-05-20 18:31:38 +0200175 u32 addr_decode_ch[2];
Angel Pons1db5bc72020-01-15 00:49:03 +0100176 struct memory_info *mem_info;
Matt DeVillier5aaa8ce2016-09-02 13:29:17 -0500177 struct dimm_info *dimm;
Angel Pons1db5bc72020-01-15 00:49:03 +0100178 int ddr_frequency, dimm_size, ch, d_num;
Matt DeVillier5aaa8ce2016-09-02 13:29:17 -0500179 int dimm_cnt = 0;
180
181 mem_info = cbmem_add(CBMEM_ID_MEMINFO, sizeof(struct memory_info));
Nico Huberacac02d2017-06-20 14:49:04 +0200182 if (!mem_info)
183 die("Failed to add memory info to CBMEM.\n");
Angel Pons1db5bc72020-01-15 00:49:03 +0100184
Matt DeVillier5aaa8ce2016-09-02 13:29:17 -0500185 memset(mem_info, 0, sizeof(struct memory_info));
186
Angel Pons1db5bc72020-01-15 00:49:03 +0100187 /* FIXME: Do we need to read MCHBAR32(MAD_CHNL) ? (Answer: Nope) */
188 MCHBAR32(MAD_CHNL);
189 addr_decode_ch[0] = MCHBAR32(MAD_DIMM_CH0);
190 addr_decode_ch[1] = MCHBAR32(MAD_DIMM_CH1);
Matt DeVillier5aaa8ce2016-09-02 13:29:17 -0500191
Angel Pons1db5bc72020-01-15 00:49:03 +0100192 ddr_frequency = (MCHBAR32(MC_BIOS_DATA) * 13333 * 2 + 50) / 100;
Matt DeVillier5aaa8ce2016-09-02 13:29:17 -0500193
194 for (ch = 0; ch < ARRAY_SIZE(addr_decode_ch); ch++) {
195 u32 ch_conf = addr_decode_ch[ch];
196 /* DIMMs A/B */
197 for (d_num = 0; d_num < 2; d_num++) {
198 dimm_size = ((ch_conf >> (d_num * 8)) & 0xff) * 256;
199 if (dimm_size) {
200 dimm = &mem_info->dimm[dimm_cnt];
201 dimm->dimm_size = dimm_size;
202 dimm->ddr_type = MEMORY_TYPE_DDR3;
203 dimm->ddr_frequency = ddr_frequency;
204 dimm->rank_per_dimm = 1 + ((ch_conf >> (17 + d_num)) & 1);
205 dimm->channel_num = ch;
206 dimm->dimm_num = d_num;
207 dimm->bank_locator = ch * 2;
208 memcpy(dimm->serial,
209 &pei_data->spd_data[dimm_cnt][SPD_DIMM_SERIAL_NUM],
210 SPD_DIMM_SERIAL_LEN);
211 memcpy(dimm->module_part_number,
212 &pei_data->spd_data[dimm_cnt][SPD_DIMM_PART_NUM],
213 SPD_DIMM_PART_LEN);
214 dimm->mod_id =
215 (pei_data->spd_data[dimm_cnt][SPD_DIMM_MOD_ID2] << 8) |
Angel Pons1db5bc72020-01-15 00:49:03 +0100216 (pei_data->spd_data[dimm_cnt][SPD_DIMM_MOD_ID1] & 0xff);
Matt DeVillier5aaa8ce2016-09-02 13:29:17 -0500217 dimm->mod_type = SPD_SODIMM;
Elyes HAOUAS7d964ae2020-07-19 09:19:59 +0200218 dimm->bus_width = MEMORY_BUS_WIDTH_64;
Matt DeVillier5aaa8ce2016-09-02 13:29:17 -0500219 dimm_cnt++;
220 }
221 }
222 }
223 mem_info->dimm_cnt = dimm_cnt;
224}