blob: b23f07252e2136114f91a250eefaaa687b18e2f5 [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>
Patrick Rudolph42609d82020-07-27 16:23:36 +020013#include <device/pci_ops.h>
Matt DeVillier5aaa8ce2016-09-02 13:29:17 -050014#include <device/dram/ddr3.h>
15#include <smbios.h>
16#include <spd.h>
Philipp Deppenwiesefea24292017-10-17 17:02:29 +020017#include <security/vboot/vboot_common.h>
Arthur Heymansf300f362018-01-27 13:39:12 +010018#include <commonlib/region.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050019#include "raminit.h"
20#include "pei_data.h"
21#include "haswell.h"
22
Arthur Heymansf300f362018-01-27 13:39:12 +010023#define MRC_CACHE_VERSION 1
24
Aaron Durbin2ad1dba2013-02-07 00:51:18 -060025void save_mrc_data(struct pei_data *pei_data)
Aaron Durbin76c37002012-10-30 09:03:43 -050026{
Aaron Durbin76c37002012-10-30 09:03:43 -050027 /* Save the MRC S3 restore data to cbmem */
Angel Pons1db5bc72020-01-15 00:49:03 +010028 mrc_cache_stash_data(MRC_TRAINING_DATA, MRC_CACHE_VERSION, pei_data->mrc_output,
29 pei_data->mrc_output_len);
Aaron Durbin76c37002012-10-30 09:03:43 -050030}
31
32static void prepare_mrc_cache(struct pei_data *pei_data)
33{
Shelley Chenad9cd682020-07-23 16:10:52 -070034 size_t mrc_size;
Aaron Durbin76c37002012-10-30 09:03:43 -050035
Angel Pons1db5bc72020-01-15 00:49:03 +010036 /* Preset just in case there is an error */
Aaron Durbin76c37002012-10-30 09:03:43 -050037 pei_data->mrc_input = NULL;
38 pei_data->mrc_input_len = 0;
39
Shelley Chenad9cd682020-07-23 16:10:52 -070040 pei_data->mrc_input =
41 mrc_cache_current_mmap_leak(MRC_TRAINING_DATA,
42 MRC_CACHE_VERSION,
43 &mrc_size);
44 if (!pei_data->mrc_input)
Angel Pons1db5bc72020-01-15 00:49:03 +010045 /* Error message printed in find_current_mrc_cache */
Aaron Durbin76c37002012-10-30 09:03:43 -050046 return;
Aaron Durbin76c37002012-10-30 09:03:43 -050047
Shelley Chenad9cd682020-07-23 16:10:52 -070048 pei_data->mrc_input_len = mrc_size;
Aaron Durbin76c37002012-10-30 09:03:43 -050049
Shelley Chenad9cd682020-07-23 16:10:52 -070050 printk(BIOS_DEBUG, "%s: at %p, size %zx\n", __func__,
51 pei_data->mrc_input, mrc_size);
Aaron Durbin76c37002012-10-30 09:03:43 -050052}
53
Angel Pons0117e4e2020-10-13 23:34:27 +020054static const char *const ecc_decoder[] = {
Aaron Durbin76c37002012-10-30 09:03:43 -050055 "inactive",
56 "active on IO",
57 "disabled on IO",
Angel Pons1db5bc72020-01-15 00:49:03 +010058 "active",
Aaron Durbin76c37002012-10-30 09:03:43 -050059};
60
Angel Pons1db5bc72020-01-15 00:49:03 +010061/* Print out the memory controller configuration, as per the values in its registers. */
Aaron Durbin76c37002012-10-30 09:03:43 -050062static void report_memory_config(void)
63{
Angel Pons1db5bc72020-01-15 00:49:03 +010064 u32 addr_decoder_common, addr_decode_chan[2];
Aaron Durbin76c37002012-10-30 09:03:43 -050065 int i;
66
Angel Pons1db5bc72020-01-15 00:49:03 +010067 addr_decoder_common = MCHBAR32(MAD_CHNL);
Angel Pons6791ad22020-10-13 21:44:08 +020068 addr_decode_chan[0] = MCHBAR32(MAD_DIMM(0));
69 addr_decode_chan[1] = MCHBAR32(MAD_DIMM(1));
Aaron Durbin76c37002012-10-30 09:03:43 -050070
71 printk(BIOS_DEBUG, "memcfg DDR3 clock %d MHz\n",
Angel Pons1db5bc72020-01-15 00:49:03 +010072 (MCHBAR32(MC_BIOS_DATA) * 13333 * 2 + 50) / 100);
73
Aaron Durbin76c37002012-10-30 09:03:43 -050074 printk(BIOS_DEBUG, "memcfg channel assignment: A: %d, B % d, C % d\n",
Angel Pons1db5bc72020-01-15 00:49:03 +010075 (addr_decoder_common >> 0) & 3,
Aaron Durbin76c37002012-10-30 09:03:43 -050076 (addr_decoder_common >> 2) & 3,
77 (addr_decoder_common >> 4) & 3);
78
Angel Pons1db5bc72020-01-15 00:49:03 +010079 for (i = 0; i < ARRAY_SIZE(addr_decode_chan); i++) {
80 u32 ch_conf = addr_decode_chan[i];
81
82 printk(BIOS_DEBUG, "memcfg channel[%d] config (%8.8x):\n", i, ch_conf);
83 printk(BIOS_DEBUG, " ECC %s\n", ecc_decoder[(ch_conf >> 24) & 3]);
Aaron Durbin76c37002012-10-30 09:03:43 -050084 printk(BIOS_DEBUG, " enhanced interleave mode %s\n",
85 ((ch_conf >> 22) & 1) ? "on" : "off");
Angel Pons1db5bc72020-01-15 00:49:03 +010086
Aaron Durbin76c37002012-10-30 09:03:43 -050087 printk(BIOS_DEBUG, " rank interleave %s\n",
88 ((ch_conf >> 21) & 1) ? "on" : "off");
Angel Pons1db5bc72020-01-15 00:49:03 +010089
Duncan Laurie8d774022013-10-22 16:32:49 -070090 printk(BIOS_DEBUG, " DIMMA %d MB width %s %s rank%s\n",
Aaron Durbin76c37002012-10-30 09:03:43 -050091 ((ch_conf >> 0) & 0xff) * 256,
Duncan Laurie8d774022013-10-22 16:32:49 -070092 ((ch_conf >> 19) & 1) ? "x16" : "x8 or x32",
Aaron Durbin76c37002012-10-30 09:03:43 -050093 ((ch_conf >> 17) & 1) ? "dual" : "single",
94 ((ch_conf >> 16) & 1) ? "" : ", selected");
Angel Pons1db5bc72020-01-15 00:49:03 +010095
Duncan Laurie8d774022013-10-22 16:32:49 -070096 printk(BIOS_DEBUG, " DIMMB %d MB width %s %s rank%s\n",
Aaron Durbin76c37002012-10-30 09:03:43 -050097 ((ch_conf >> 8) & 0xff) * 256,
Ryan Salsamendidab81a42017-06-30 17:36:41 -070098 ((ch_conf >> 20) & 1) ? "x16" : "x8 or x32",
Aaron Durbin76c37002012-10-30 09:03:43 -050099 ((ch_conf >> 18) & 1) ? "dual" : "single",
100 ((ch_conf >> 16) & 1) ? ", selected" : "");
101 }
102}
103
104/**
105 * Find PEI executable in coreboot filesystem and execute it.
106 *
107 * @param pei_data: configuration data for UEFI PEI reference code
108 */
109void sdram_initialize(struct pei_data *pei_data)
110{
Angel Pons1ca6b532020-10-13 23:43:00 +0200111 int (*entry)(struct pei_data *pei_data) __attribute__((regparm(1)));
112
Arthur Heymans8da2fa02018-06-06 10:35:45 +0200113 uint32_t type = CBFS_TYPE_MRC;
114 struct cbfsf f;
Aaron Durbin76c37002012-10-30 09:03:43 -0500115
Aaron Durbin76c37002012-10-30 09:03:43 -0500116 printk(BIOS_DEBUG, "Starting UEFI PEI System Agent\n");
117
Angel Pons1db5bc72020-01-15 00:49:03 +0100118 /* Do not pass MRC data in for recovery mode boot, always pass it in for S3 resume */
Julius Werner29fbfcc2020-03-02 15:54:43 -0800119 if (!(CONFIG(HASWELL_VBOOT_IN_BOOTBLOCK) && vboot_recovery_mode_enabled())
120 || pei_data->boot_mode == 2)
Aaron Durbin76c37002012-10-30 09:03:43 -0500121 prepare_mrc_cache(pei_data);
122
Angel Pons1db5bc72020-01-15 00:49:03 +0100123 /* If MRC data is not found, we cannot continue S3 resume */
Aaron Durbin76c37002012-10-30 09:03:43 -0500124 if (pei_data->boot_mode == 2 && !pei_data->mrc_input) {
Duncan Laurie727b5452013-08-08 16:28:41 -0700125 post_code(POST_RESUME_FAILURE);
Elyes HAOUAS3cd43272020-03-05 22:01:17 +0100126 printk(BIOS_DEBUG, "Giving up in %s: No MRC data\n", __func__);
Elyes HAOUAS82d46422019-04-28 18:01:48 +0200127 system_reset();
Aaron Durbin76c37002012-10-30 09:03:43 -0500128 }
129
130 /* Pass console handler in pei_data */
Kyösti Mälkki657e0be2014-02-04 19:03:57 +0200131 pei_data->tx_byte = do_putchar;
Aaron Durbin76c37002012-10-30 09:03:43 -0500132
Arthur Heymans8da2fa02018-06-06 10:35:45 +0200133 /*
Angel Pons1db5bc72020-01-15 00:49:03 +0100134 * Locate and call UEFI System Agent binary. The binary needs to be at a fixed offset
135 * in the flash and can therefore only reside in the COREBOOT fmap region.
Arthur Heymans8da2fa02018-06-06 10:35:45 +0200136 */
137 if (cbfs_locate_file_in_region(&f, "COREBOOT", "mrc.bin", &type) < 0)
138 die("mrc.bin not found!");
Angel Pons1db5bc72020-01-15 00:49:03 +0100139
Arthur Heymans8da2fa02018-06-06 10:35:45 +0200140 /* We don't care about leaking the mapping */
Angel Pons1ca6b532020-10-13 23:43:00 +0200141 entry = rdev_mmap_full(&f.data);
Aaron Durbin76c37002012-10-30 09:03:43 -0500142 if (entry) {
Angel Pons1ca6b532020-10-13 23:43:00 +0200143 int rv = entry(pei_data);
Iru Cai33642032019-06-11 14:24:43 +0800144
Angel Pons1db5bc72020-01-15 00:49:03 +0100145 /* The mrc.bin reconfigures USB, so usbdebug needs to be reinitialized */
Iru Cai33642032019-06-11 14:24:43 +0800146 if (CONFIG(USBDEBUG_IN_PRE_RAM))
147 usbdebug_hw_init(true);
148
Aaron Durbin76c37002012-10-30 09:03:43 -0500149 if (rv) {
150 switch (rv) {
151 case -1:
152 printk(BIOS_ERR, "PEI version mismatch.\n");
153 break;
154 case -2:
155 printk(BIOS_ERR, "Invalid memory frequency.\n");
156 break;
157 default:
158 printk(BIOS_ERR, "MRC returned %x.\n", rv);
159 }
Keith Shortbb41aba2019-05-16 14:07:43 -0600160 die_with_post_code(POST_INVALID_VENDOR_BINARY,
161 "Nonzero MRC return value.\n");
Aaron Durbin76c37002012-10-30 09:03:43 -0500162 }
163 } else {
164 die("UEFI PEI System Agent not found.\n");
165 }
166
Angel Pons1db5bc72020-01-15 00:49:03 +0100167 /* For reference, print the System Agent version after executing the UEFI PEI stage */
168 u32 version = MCHBAR32(MRC_REVISION);
Aaron Durbin76c37002012-10-30 09:03:43 -0500169 printk(BIOS_DEBUG, "System Agent Version %d.%d.%d Build %d\n",
Angel Pons1db5bc72020-01-15 00:49:03 +0100170 (version >> 24) & 0xff, (version >> 16) & 0xff,
171 (version >> 8) & 0xff, (version >> 0) & 0xff);
Aaron Durbin76c37002012-10-30 09:03:43 -0500172
Aaron Durbin76c37002012-10-30 09:03:43 -0500173 report_memory_config();
Aaron Durbin76c37002012-10-30 09:03:43 -0500174}
Matt DeVillier5aaa8ce2016-09-02 13:29:17 -0500175
Patrick Rudolph42609d82020-07-27 16:23:36 +0200176static bool nb_supports_ecc(const uint32_t capid0_a)
177{
178 return !(capid0_a & CAPID_ECCDIS);
179}
180
181static uint16_t nb_slots_per_channel(const uint32_t capid0_a)
182{
183 return !(capid0_a & CAPID_DDPCD) + 1;
184}
185
186static uint16_t nb_number_of_channels(const uint32_t capid0_a)
187{
188 return !(capid0_a & CAPID_PDCD) + 1;
189}
190
191static uint32_t nb_max_chan_capacity_mib(const uint32_t capid0_a)
192{
193 uint32_t ddrsz;
194
195 /* Values from documentation, which assume two DIMMs per channel */
196 switch (CAPID_DDRSZ(capid0_a)) {
197 case 1:
198 ddrsz = 8192;
199 break;
200 case 2:
201 ddrsz = 2048;
202 break;
203 case 3:
204 ddrsz = 512;
205 break;
206 default:
207 ddrsz = 16384;
208 break;
209 }
210
211 /* Account for the maximum number of DIMMs per channel */
212 return (ddrsz / 2) * nb_slots_per_channel(capid0_a);
213}
214
Matt DeVillier5aaa8ce2016-09-02 13:29:17 -0500215void setup_sdram_meminfo(struct pei_data *pei_data)
216{
Elyes HAOUASce83f312019-05-20 18:31:38 +0200217 u32 addr_decode_ch[2];
Angel Pons1db5bc72020-01-15 00:49:03 +0100218 struct memory_info *mem_info;
Matt DeVillier5aaa8ce2016-09-02 13:29:17 -0500219 struct dimm_info *dimm;
Angel Pons1db5bc72020-01-15 00:49:03 +0100220 int ddr_frequency, dimm_size, ch, d_num;
Matt DeVillier5aaa8ce2016-09-02 13:29:17 -0500221 int dimm_cnt = 0;
222
223 mem_info = cbmem_add(CBMEM_ID_MEMINFO, sizeof(struct memory_info));
Nico Huberacac02d2017-06-20 14:49:04 +0200224 if (!mem_info)
225 die("Failed to add memory info to CBMEM.\n");
Angel Pons1db5bc72020-01-15 00:49:03 +0100226
Matt DeVillier5aaa8ce2016-09-02 13:29:17 -0500227 memset(mem_info, 0, sizeof(struct memory_info));
228
Angel Pons6791ad22020-10-13 21:44:08 +0200229 addr_decode_ch[0] = MCHBAR32(MAD_DIMM(0));
230 addr_decode_ch[1] = MCHBAR32(MAD_DIMM(1));
Matt DeVillier5aaa8ce2016-09-02 13:29:17 -0500231
Angel Pons1db5bc72020-01-15 00:49:03 +0100232 ddr_frequency = (MCHBAR32(MC_BIOS_DATA) * 13333 * 2 + 50) / 100;
Matt DeVillier5aaa8ce2016-09-02 13:29:17 -0500233
234 for (ch = 0; ch < ARRAY_SIZE(addr_decode_ch); ch++) {
235 u32 ch_conf = addr_decode_ch[ch];
236 /* DIMMs A/B */
237 for (d_num = 0; d_num < 2; d_num++) {
238 dimm_size = ((ch_conf >> (d_num * 8)) & 0xff) * 256;
239 if (dimm_size) {
240 dimm = &mem_info->dimm[dimm_cnt];
241 dimm->dimm_size = dimm_size;
242 dimm->ddr_type = MEMORY_TYPE_DDR3;
243 dimm->ddr_frequency = ddr_frequency;
244 dimm->rank_per_dimm = 1 + ((ch_conf >> (17 + d_num)) & 1);
245 dimm->channel_num = ch;
246 dimm->dimm_num = d_num;
247 dimm->bank_locator = ch * 2;
248 memcpy(dimm->serial,
249 &pei_data->spd_data[dimm_cnt][SPD_DIMM_SERIAL_NUM],
250 SPD_DIMM_SERIAL_LEN);
251 memcpy(dimm->module_part_number,
252 &pei_data->spd_data[dimm_cnt][SPD_DIMM_PART_NUM],
253 SPD_DIMM_PART_LEN);
254 dimm->mod_id =
255 (pei_data->spd_data[dimm_cnt][SPD_DIMM_MOD_ID2] << 8) |
Angel Pons1db5bc72020-01-15 00:49:03 +0100256 (pei_data->spd_data[dimm_cnt][SPD_DIMM_MOD_ID1] & 0xff);
Matt DeVillier5aaa8ce2016-09-02 13:29:17 -0500257 dimm->mod_type = SPD_SODIMM;
Elyes HAOUAS7d964ae2020-07-19 09:19:59 +0200258 dimm->bus_width = MEMORY_BUS_WIDTH_64;
Matt DeVillier5aaa8ce2016-09-02 13:29:17 -0500259 dimm_cnt++;
260 }
261 }
262 }
263 mem_info->dimm_cnt = dimm_cnt;
Patrick Rudolph42609d82020-07-27 16:23:36 +0200264
265 const uint32_t capid0_a = pci_read_config32(HOST_BRIDGE, CAPID0_A);
266
267 const uint16_t channels = nb_number_of_channels(capid0_a);
268
269 mem_info->ecc_capable = nb_supports_ecc(capid0_a);
270 mem_info->max_capacity_mib = channels * nb_max_chan_capacity_mib(capid0_a);
271 mem_info->number_of_devices = channels * nb_slots_per_channel(capid0_a);
Matt DeVillier5aaa8ce2016-09-02 13:29:17 -0500272}