blob: 3077c0ae8fe6fecfea866c6d994a8e4f31564386 [file] [log] [blame]
Angel Pons6e5aabd2020-03-23 23:44:42 +01001/* SPDX-License-Identifier: GPL-2.0-only */
Stefan Reinauer00636b02012-04-04 00:08:51 +02002
3#include <console/console.h>
Arthur Heymans7539b8c2017-12-24 10:42:57 +01004#include <commonlib/region.h>
Elyes HAOUASc0567292019-04-28 17:57:47 +02005#include <cf9_reset.h>
Stefan Reinauer00636b02012-04-04 00:08:51 +02006#include <string.h>
Subrata Banik53b08c32018-12-10 14:11:35 +05307#include <arch/cpu.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +02008#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02009#include <device/pci_ops.h>
Kyösti Mälkki1cae4542020-01-06 12:31:34 +020010#include <device/smbus_host.h>
Stefan Reinauer00636b02012-04-04 00:08:51 +020011#include <cbmem.h>
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +010012#include <timestamp.h>
Arthur Heymans7539b8c2017-12-24 10:42:57 +010013#include <mrc_cache.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010014#include <southbridge/intel/bd82x6x/me.h>
Patrick Rudolphda9302a2019-03-24 17:01:41 +010015#include <southbridge/intel/bd82x6x/pch.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010016#include <cpu/x86/msr.h>
Elyes HAOUAS51401c32019-05-15 21:09:30 +020017#include <types.h>
Elyes HAOUASbf0970e2019-03-21 11:10:03 +010018
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010019#include "raminit_native.h"
20#include "raminit_common.h"
21#include "sandybridge.h"
Stefan Reinauer00636b02012-04-04 00:08:51 +020022
Angel Pons7c49cb82020-03-16 23:17:32 +010023/* FIXME: no ECC support */
24/* FIXME: no support for 3-channel chipsets */
Stefan Reinauer00636b02012-04-04 00:08:51 +020025
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070026static void wait_txt_clear(void)
27{
Angel Pons7c49cb82020-03-16 23:17:32 +010028 struct cpuid_result cp = cpuid_ext(1, 0);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070029
Angel Pons7c49cb82020-03-16 23:17:32 +010030 /* Check if TXT is supported */
31 if (!(cp.ecx & (1 << 6)))
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070032 return;
Angel Pons7c49cb82020-03-16 23:17:32 +010033
34 /* Some TXT public bit */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070035 if (!(read32((void *)0xfed30010) & 1))
36 return;
Angel Pons7c49cb82020-03-16 23:17:32 +010037
38 /* Wait for TXT clear */
39 while (!(read8((void *)0xfed40000) & (1 << 7)))
40 ;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070041}
42
Angel Pons7c49cb82020-03-16 23:17:32 +010043/* Disable a channel in ramctr_timing */
44static void disable_channel(ramctr_timing *ctrl, int channel)
45{
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +010046 ctrl->rankmap[channel] = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +010047
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +010048 memset(&ctrl->rank_mirror[channel][0], 0, sizeof(ctrl->rank_mirror[0]));
Angel Pons7c49cb82020-03-16 23:17:32 +010049
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +010050 ctrl->channel_size_mb[channel] = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +010051 ctrl->cmd_stretch[channel] = 0;
52 ctrl->mad_dimm[channel] = 0;
53 memset(&ctrl->timings[channel][0], 0, sizeof(ctrl->timings[0]));
Patrick Rudolph74163d62016-11-17 20:02:43 +010054 memset(&ctrl->info.dimm[channel][0], 0, sizeof(ctrl->info.dimm[0]));
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +010055}
56
Patrick Rudolph42609d82020-07-27 16:23:36 +020057static bool nb_supports_ecc(const uint32_t capid0_a)
58{
59 return !(capid0_a & CAPID_ECCDIS);
60}
61
62static uint16_t nb_slots_per_channel(const uint32_t capid0_a)
63{
64 return !(capid0_a & CAPID_DDPCD) + 1;
65}
66
67static uint16_t nb_number_of_channels(const uint32_t capid0_a)
68{
69 return !(capid0_a & CAPID_PDCD) + 1;
70}
71
72static uint32_t nb_max_chan_capacity_mib(const uint32_t capid0_a)
73{
74 uint32_t ddrsz;
75
76 /* Values from documentation, which assume two DIMMs per channel */
77 switch (CAPID_DDRSZ(capid0_a)) {
78 case 1:
79 ddrsz = 8192;
80 break;
81 case 2:
82 ddrsz = 2048;
83 break;
84 case 3:
85 ddrsz = 512;
86 break;
87 default:
88 ddrsz = 16384;
89 break;
90 }
91
92 /* Account for the maximum number of DIMMs per channel */
93 return (ddrsz / 2) * nb_slots_per_channel(capid0_a);
94}
95
96/* Fill cbmem with information for SMBIOS type 16 and type 17 */
97static void setup_sdram_meminfo(ramctr_timing *ctrl)
Patrick Rudolphb97009e2016-02-28 15:24:04 +010098{
Patrick Rudolphb97009e2016-02-28 15:24:04 +010099 int channel, slot;
Patrick Rudolph24efe732018-08-19 11:06:06 +0200100 const u16 ddr_freq = (1000 << 8) / ctrl->tCK;
Patrick Rudolphb97009e2016-02-28 15:24:04 +0100101
Elyes HAOUAS12df9502016-08-23 21:29:48 +0200102 FOR_ALL_CHANNELS for (slot = 0; slot < NUM_SLOTS; slot++) {
Patrick Rudolph24efe732018-08-19 11:06:06 +0200103 enum cb_err ret = spd_add_smbios17(channel, slot, ddr_freq,
104 &ctrl->info.dimm[channel][slot]);
105 if (ret != CB_SUCCESS)
106 printk(BIOS_ERR, "RAMINIT: Failed to add SMBIOS17\n");
Patrick Rudolphb97009e2016-02-28 15:24:04 +0100107 }
Patrick Rudolph42609d82020-07-27 16:23:36 +0200108
109 /* The 'spd_add_smbios17' function allocates this CBMEM area */
110 struct memory_info *m = cbmem_find(CBMEM_ID_MEMINFO);
111 if (m == NULL)
112 return;
113
114 const uint32_t capid0_a = pci_read_config32(HOST_BRIDGE, CAPID0_A);
115
116 const uint16_t channels = nb_number_of_channels(capid0_a);
117
118 m->ecc_capable = nb_supports_ecc(capid0_a);
119 m->max_capacity_mib = channels * nb_max_chan_capacity_mib(capid0_a);
120 m->number_of_devices = channels * nb_slots_per_channel(capid0_a);
Patrick Rudolphb97009e2016-02-28 15:24:04 +0100121}
122
Angel Pons7c49cb82020-03-16 23:17:32 +0100123/* Return CRC16 match for all SPDs */
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100124static int verify_crc16_spds_ddr3(spd_raw_data *spd, ramctr_timing *ctrl)
125{
126 int channel, slot, spd_slot;
127 int match = 1;
128
129 FOR_ALL_CHANNELS {
130 for (slot = 0; slot < NUM_SLOTS; slot++) {
131 spd_slot = 2 * channel + slot;
132 match &= ctrl->spd_crc[channel][slot] ==
Angel Pons7c49cb82020-03-16 23:17:32 +0100133 spd_ddr3_calc_unique_crc(spd[spd_slot], sizeof(spd_raw_data));
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100134 }
135 }
136 return match;
137}
138
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +0200139void read_spd(spd_raw_data * spd, u8 addr, bool id_only)
Stefan Reinauer00636b02012-04-04 00:08:51 +0200140{
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700141 int j;
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +0200142 if (id_only) {
143 for (j = 117; j < 128; j++)
Kyösti Mälkki1a1b04e2020-01-07 22:34:33 +0200144 (*spd)[j] = smbus_read_byte(addr, j);
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +0200145 } else {
146 for (j = 0; j < 256; j++)
Kyösti Mälkki1a1b04e2020-01-07 22:34:33 +0200147 (*spd)[j] = smbus_read_byte(addr, j);
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +0200148 }
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700149}
150
Patrick Rudolph735ecce2016-03-26 10:42:27 +0100151static void dram_find_spds_ddr3(spd_raw_data *spd, ramctr_timing *ctrl)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700152{
Angel Pons7c49cb82020-03-16 23:17:32 +0100153 int dimms = 0, ch_dimms;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700154 int channel, slot, spd_slot;
Patrick Rudolphdd662872017-10-28 18:20:11 +0200155 bool can_use_ecc = ctrl->ecc_supported;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700156
Elyes HAOUAS0d4b11a2016-10-03 21:57:21 +0200157 memset (ctrl->rankmap, 0, sizeof(ctrl->rankmap));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700158
159 ctrl->extended_temperature_range = 1;
160 ctrl->auto_self_refresh = 1;
161
162 FOR_ALL_CHANNELS {
163 ctrl->channel_size_mb[channel] = 0;
164
Angel Pons7c49cb82020-03-16 23:17:32 +0100165 ch_dimms = 0;
166 /* Count dimms on channel */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700167 for (slot = 0; slot < NUM_SLOTS; slot++) {
168 spd_slot = 2 * channel + slot;
Patrick Rudolph5a061852017-09-22 15:19:26 +0200169
Angel Pons035096c2020-09-17 22:31:19 +0200170 if (spd[spd_slot][SPD_MEMORY_TYPE] == SPD_MEMORY_TYPE_SDRAM_DDR3)
Angel Pons7c49cb82020-03-16 23:17:32 +0100171 ch_dimms++;
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100172 }
173
174 for (slot = 0; slot < NUM_SLOTS; slot++) {
175 spd_slot = 2 * channel + slot;
Angel Pons7c49cb82020-03-16 23:17:32 +0100176 printk(BIOS_DEBUG, "SPD probe channel%d, slot%d\n", channel, slot);
Patrick Rudolph5a061852017-09-22 15:19:26 +0200177
Angel Pons323c0ae2020-12-12 16:57:37 +0100178 dimm_attr *const dimm = &ctrl->info.dimm[channel][slot];
179
Angel Pons7c49cb82020-03-16 23:17:32 +0100180 /* Search for XMP profile */
Angel Pons323c0ae2020-12-12 16:57:37 +0100181 spd_xmp_decode_ddr3(dimm, spd[spd_slot],
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100182 DDR3_XMP_PROFILE_1);
183
Angel Pons323c0ae2020-12-12 16:57:37 +0100184 if (dimm->dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3) {
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100185 printram("No valid XMP profile found.\n");
Angel Pons323c0ae2020-12-12 16:57:37 +0100186 spd_decode_ddr3(dimm, spd[spd_slot]);
Angel Pons7c49cb82020-03-16 23:17:32 +0100187
Angel Pons323c0ae2020-12-12 16:57:37 +0100188 } else if (ch_dimms > dimm->dimms_per_channel) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100189 printram(
190 "XMP profile supports %u DIMMs, but %u DIMMs are installed.\n",
Angel Pons323c0ae2020-12-12 16:57:37 +0100191 dimm->dimms_per_channel, ch_dimms);
Angel Pons7c49cb82020-03-16 23:17:32 +0100192
Julius Wernercd49cce2019-03-05 16:53:33 -0800193 if (CONFIG(NATIVE_RAMINIT_IGNORE_XMP_MAX_DIMMS))
Angel Pons7c49cb82020-03-16 23:17:32 +0100194 printk(BIOS_WARNING,
195 "XMP maximum DIMMs will be ignored.\n");
Vagiz Trakhanov771be482017-10-02 10:02:35 +0000196 else
Angel Pons323c0ae2020-12-12 16:57:37 +0100197 spd_decode_ddr3(dimm, spd[spd_slot]);
Angel Pons7c49cb82020-03-16 23:17:32 +0100198
Angel Pons323c0ae2020-12-12 16:57:37 +0100199 } else if (dimm->voltage != 1500) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100200 /* TODO: Support DDR3 voltages other than 1500mV */
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100201 printram("XMP profile's requested %u mV is unsupported.\n",
Angel Pons323c0ae2020-12-12 16:57:37 +0100202 dimm->voltage);
203 spd_decode_ddr3(dimm, spd[spd_slot]);
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100204 }
205
Angel Pons7c49cb82020-03-16 23:17:32 +0100206 /* Fill in CRC16 for MRC cache */
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100207 ctrl->spd_crc[channel][slot] =
Angel Pons7c49cb82020-03-16 23:17:32 +0100208 spd_ddr3_calc_unique_crc(spd[spd_slot], sizeof(spd_raw_data));
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100209
Angel Pons323c0ae2020-12-12 16:57:37 +0100210 if (dimm->dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100211 /* Mark DIMM as invalid */
Angel Pons323c0ae2020-12-12 16:57:37 +0100212 dimm->ranks = 0;
213 dimm->size_mb = 0;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700214 continue;
215 }
216
Angel Pons323c0ae2020-12-12 16:57:37 +0100217 dram_print_spd_ddr3(dimm);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700218 dimms++;
219 ctrl->rank_mirror[channel][slot * 2] = 0;
Angel Pons323c0ae2020-12-12 16:57:37 +0100220 ctrl->rank_mirror[channel][slot * 2 + 1] = dimm->flags.pins_mirrored;
Angel Pons7c49cb82020-03-16 23:17:32 +0100221
Angel Pons323c0ae2020-12-12 16:57:37 +0100222 ctrl->channel_size_mb[channel] += dimm->size_mb;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700223
Angel Pons323c0ae2020-12-12 16:57:37 +0100224 if (!dimm->flags.is_ecc)
Patrick Rudolphdd662872017-10-28 18:20:11 +0200225 can_use_ecc = false;
226
Angel Pons323c0ae2020-12-12 16:57:37 +0100227 ctrl->auto_self_refresh &= dimm->flags.asr;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700228
Angel Pons323c0ae2020-12-12 16:57:37 +0100229 ctrl->extended_temperature_range &= dimm->flags.ext_temp_refresh;
Angel Pons7c49cb82020-03-16 23:17:32 +0100230
Angel Pons323c0ae2020-12-12 16:57:37 +0100231 ctrl->rankmap[channel] |= ((1 << dimm->ranks) - 1) << (2 * slot);
Angel Pons7c49cb82020-03-16 23:17:32 +0100232
233 printk(BIOS_DEBUG, "channel[%d] rankmap = 0x%x\n", channel,
234 ctrl->rankmap[channel]);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700235 }
Angel Pons7c49cb82020-03-16 23:17:32 +0100236
Angel Ponsd4d3ba02020-12-12 17:45:14 +0100237 const u8 rc_0 = ctrl->info.dimm[channel][0].reference_card;
238 const u8 rc_1 = ctrl->info.dimm[channel][1].reference_card;
239
240 if (ch_dimms == NUM_SLOTS && rc_0 < 6 && rc_1 < 6) {
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700241 const int ref_card_offset_table[6][6] = {
Angel Pons7c49cb82020-03-16 23:17:32 +0100242 { 0, 0, 0, 0, 2, 2 },
243 { 0, 0, 0, 0, 2, 2 },
244 { 0, 0, 0, 0, 2, 2 },
245 { 0, 0, 0, 0, 1, 1 },
246 { 2, 2, 2, 1, 0, 0 },
247 { 2, 2, 2, 1, 0, 0 },
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700248 };
Angel Ponsd4d3ba02020-12-12 17:45:14 +0100249 ctrl->ref_card_offset[channel] = ref_card_offset_table[rc_0][rc_1];
Angel Pons7c49cb82020-03-16 23:17:32 +0100250 } else {
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700251 ctrl->ref_card_offset[channel] = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100252 }
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700253 }
254
Patrick Rudolphdd662872017-10-28 18:20:11 +0200255 if (ctrl->ecc_forced || CONFIG(RAMINIT_ENABLE_ECC))
256 ctrl->ecc_enabled = can_use_ecc;
257 if (ctrl->ecc_forced && !ctrl->ecc_enabled)
258 die("ECC mode forced but non-ECC DIMM installed!");
259 printk(BIOS_DEBUG, "ECC is %s\n", ctrl->ecc_enabled ? "enabled" : "disabled");
260
261 ctrl->lanes = ctrl->ecc_enabled ? 9 : 8;
262
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700263 if (!dimms)
264 die("No DIMMs were found");
265}
266
Patrick Rudolphbb9c90a2016-05-29 17:05:06 +0200267static void save_timings(ramctr_timing *ctrl)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700268{
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700269 /* Save the MRC S3 restore data to cbmem */
Angel Pons7c49cb82020-03-16 23:17:32 +0100270 mrc_cache_stash_data(MRC_TRAINING_DATA, MRC_CACHE_VERSION, ctrl, sizeof(*ctrl));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700271}
272
Angel Ponsfc930242020-03-24 11:12:09 +0100273static void reinit_ctrl(ramctr_timing *ctrl, const u32 cpuid)
Patrick Rudolph05d4bf7e2017-10-28 16:36:09 +0200274{
275 /* Reset internal state */
276 memset(ctrl, 0, sizeof(*ctrl));
Patrick Rudolph05d4bf7e2017-10-28 16:36:09 +0200277
278 /* Get architecture */
279 ctrl->cpu = cpuid;
280
281 /* Get ECC support and mode */
282 ctrl->ecc_forced = get_host_ecc_forced();
283 ctrl->ecc_supported = ctrl->ecc_forced || get_host_ecc_cap();
284 printk(BIOS_DEBUG, "ECC supported: %s ECC forced: %s\n",
285 ctrl->ecc_supported ? "yes" : "no",
286 ctrl->ecc_forced ? "yes" : "no");
287}
288
Angel Ponsfc930242020-03-24 11:12:09 +0100289static void init_dram_ddr3(int s3resume, const u32 cpuid)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700290{
Angel Pons7c49cb82020-03-16 23:17:32 +0100291 int me_uma_size, cbmem_was_inited, fast_boot, err;
Patrick Rudolph735ecce2016-03-26 10:42:27 +0100292 ramctr_timing ctrl;
Kyösti Mälkki4cb44e52016-11-18 19:11:24 +0200293 spd_raw_data spds[4];
Shelley Chenad9cd682020-07-23 16:10:52 -0700294 size_t mrc_size;
Angel Ponsa6a64182020-03-21 18:06:03 +0100295 ramctr_timing *ctrl_cached = NULL;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700296
Angel Pons88521882020-01-05 20:21:20 +0100297 MCHBAR32(SAPMCTL) |= 1;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200298
299 /* Wait for ME to be ready */
300 intel_early_me_init();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700301 me_uma_size = intel_early_me_uma_size();
Stefan Reinauer00636b02012-04-04 00:08:51 +0200302
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700303 printk(BIOS_DEBUG, "Starting native Platform init\n");
Stefan Reinauer00636b02012-04-04 00:08:51 +0200304
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700305 wait_txt_clear();
Stefan Reinauer00636b02012-04-04 00:08:51 +0200306
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700307 wrmsr(0x000002e6, (msr_t) { .lo = 0, .hi = 0 });
Stefan Reinauer00636b02012-04-04 00:08:51 +0200308
Angel Pons7c49cb82020-03-16 23:17:32 +0100309 const u32 sskpd = MCHBAR32(SSKPD); // !!! = 0x00000000
310 if ((pci_read_config16(SOUTHBRIDGE, 0xa2) & 0xa0) == 0x20 && sskpd && !s3resume) {
311 MCHBAR32(SSKPD) = 0;
312 /* Need reset */
Elyes HAOUASc0567292019-04-28 17:57:47 +0200313 system_reset();
Stefan Reinauer00636b02012-04-04 00:08:51 +0200314 }
Stefan Reinauer00636b02012-04-04 00:08:51 +0200315
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700316 early_pch_init_native();
Patrick Rudolph6aca7e62019-03-26 18:22:36 +0100317 early_init_dmi();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700318 early_thermal_init();
319
Angel Pons7c49cb82020-03-16 23:17:32 +0100320 /* Try to find timings in MRC cache */
Shelley Chenad9cd682020-07-23 16:10:52 -0700321 ctrl_cached = mrc_cache_current_mmap_leak(MRC_TRAINING_DATA,
322 MRC_CACHE_VERSION,
323 &mrc_size);
324 if (mrc_size < sizeof(ctrl))
325 ctrl_cached = NULL;
Angel Ponsa6a64182020-03-21 18:06:03 +0100326
327 /* Before reusing training data, assert that the CPU has not been replaced */
328 if (ctrl_cached && cpuid != ctrl_cached->cpu) {
329
330 /* It is not really worrying on a cold boot, but fatal when resuming from S3 */
331 printk(s3resume ? BIOS_ALERT : BIOS_NOTICE,
332 "CPUID %x differs from stored CPUID %x, CPU was replaced!\n",
333 cpuid, ctrl_cached->cpu);
334
335 /* Invalidate the stored data, it likely does not apply to the current CPU */
336 ctrl_cached = NULL;
337 }
338
339 if (s3resume && !ctrl_cached) {
340 /* S3 resume is impossible, reset to come up cleanly */
341 system_reset();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700342 }
343
Angel Pons7c49cb82020-03-16 23:17:32 +0100344 /* Verify MRC cache for fast boot */
Kyösti Mälkki38cb8222016-11-18 19:25:52 +0200345 if (!s3resume && ctrl_cached) {
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +0200346 /* Load SPD unique information data. */
347 memset(spds, 0, sizeof(spds));
348 mainboard_get_spd(spds, 1);
349
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100350 /* check SPD CRC16 to make sure the DIMMs haven't been replaced */
351 fast_boot = verify_crc16_spds_ddr3(spds, ctrl_cached);
352 if (!fast_boot)
353 printk(BIOS_DEBUG, "Stored timings CRC16 mismatch.\n");
Kyösti Mälkki38cb8222016-11-18 19:25:52 +0200354 } else {
355 fast_boot = s3resume;
356 }
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100357
358 if (fast_boot) {
359 printk(BIOS_DEBUG, "Trying stored timings.\n");
360 memcpy(&ctrl, ctrl_cached, sizeof(ctrl));
361
Patrick Rudolph588ccaa2016-04-20 18:00:27 +0200362 err = try_init_dram_ddr3(&ctrl, fast_boot, s3resume, me_uma_size);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100363 if (err) {
Patrick Rudolph588ccaa2016-04-20 18:00:27 +0200364 if (s3resume) {
365 /* Failed S3 resume, reset to come up cleanly */
Elyes HAOUASc0567292019-04-28 17:57:47 +0200366 system_reset();
Patrick Rudolph588ccaa2016-04-20 18:00:27 +0200367 }
Angel Pons7c49cb82020-03-16 23:17:32 +0100368 /* No need to erase bad MRC cache here, it gets overwritten on a
369 successful boot */
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100370 printk(BIOS_ERR, "Stored timings are invalid !\n");
371 fast_boot = 0;
372 }
373 }
374 if (!fast_boot) {
Patrick Rudolphe74ad212016-11-16 18:06:50 +0100375 /* Reset internal state */
Angel Ponsfc930242020-03-24 11:12:09 +0100376 reinit_ctrl(&ctrl, cpuid);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100377
Patrick Rudolph05d4bf7e2017-10-28 16:36:09 +0200378 printk(BIOS_INFO, "ECC RAM %s.\n", ctrl.ecc_forced ? "required" :
379 ctrl.ecc_supported ? "supported" : "unsupported");
Patrick Rudolph305035c2016-11-11 18:38:50 +0100380
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100381 /* Get DDR3 SPD data */
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +0200382 memset(spds, 0, sizeof(spds));
383 mainboard_get_spd(spds, 0);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100384 dram_find_spds_ddr3(spds, &ctrl);
385
Patrick Rudolph588ccaa2016-04-20 18:00:27 +0200386 err = try_init_dram_ddr3(&ctrl, fast_boot, s3resume, me_uma_size);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100387 }
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +0100388
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +0100389 if (err) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100390 /* Fallback: disable failing channel */
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +0100391 printk(BIOS_ERR, "RAM training failed, trying fallback.\n");
392 printram("Disable failing channel.\n");
393
Patrick Rudolphe74ad212016-11-16 18:06:50 +0100394 /* Reset internal state */
Angel Ponsfc930242020-03-24 11:12:09 +0100395 reinit_ctrl(&ctrl, cpuid);
Patrick Rudolph305035c2016-11-11 18:38:50 +0100396
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +0100397 /* Reset DDR3 frequency */
398 dram_find_spds_ddr3(spds, &ctrl);
399
Angel Pons7c49cb82020-03-16 23:17:32 +0100400 /* Disable failing channel */
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +0100401 disable_channel(&ctrl, GET_ERR_CHANNEL(err));
402
403 err = try_init_dram_ddr3(&ctrl, fast_boot, s3resume, me_uma_size);
404 }
405
Patrick Rudolph31d19592016-03-26 12:22:34 +0100406 if (err)
407 die("raminit failed");
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700408
Angel Pons88521882020-01-05 20:21:20 +0100409 /* FIXME: should be hardware revision-dependent. The register only exists on IVB. */
410 MCHBAR32(CHANNEL_HASH) = 0x00a030ce;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700411
412 set_scrambling_seed(&ctrl);
413
Patrick Rudolphd0581312020-05-01 18:31:48 +0200414 if (!s3resume && ctrl.ecc_enabled)
415 channel_scrub(&ctrl);
416
Angel Pons88521882020-01-05 20:21:20 +0100417 set_normal_operation(&ctrl);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700418
419 final_registers(&ctrl);
420
Patrick Rudolphd0581312020-05-01 18:31:48 +0200421 /* can't do this earlier because it needs to be done in normal operation */
422 if (CONFIG(DEBUG_RAM_SETUP) && !s3resume && ctrl.ecc_enabled) {
423 uint32_t i, tseg = pci_read_config32(HOST_BRIDGE, TSEGMB);
424
425 printk(BIOS_INFO, "RAMINIT: ECC scrub test on first channel up to 0x%x\n",
426 tseg);
427
428 /*
429 * This test helps to debug the ECC scrubbing.
430 * It likely tests every channel/rank, as rank interleave and enhanced
431 * interleave are enabled, but there's no guarantee for it.
432 */
433
434 /* Skip first MB to avoid special case for A-seg and test up to TSEG */
435 for (i = 1; i < tseg >> 20; i++) {
436 for (int j = 0; j < 1 * MiB; j += 4096) {
437 uintptr_t addr = i * MiB + j;
438 if (read32((u32 *)addr) == 0)
439 continue;
440
441 printk(BIOS_ERR, "RAMINIT: ECC scrub: DRAM not cleared at"
442 " addr 0x%lx\n", addr);
443 break;
444 }
445 }
446 printk(BIOS_INFO, "RAMINIT: ECC scrub test done.\n");
447 }
448
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700449 /* Zone config */
450 dram_zones(&ctrl, 0);
451
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700452 intel_early_me_init_done(ME_INIT_STATUS_SUCCESS);
453 intel_early_me_status();
454
Stefan Reinauer00636b02012-04-04 00:08:51 +0200455 report_memory_config();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700456
457 cbmem_was_inited = !cbmem_recovery(s3resume);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100458 if (!fast_boot)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700459 save_timings(&ctrl);
460 if (s3resume && !cbmem_was_inited) {
461 /* Failed S3 resume, reset to come up cleanly */
Elyes HAOUASc0567292019-04-28 17:57:47 +0200462 system_reset();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700463 }
Patrick Rudolphb97009e2016-02-28 15:24:04 +0100464
Nico Huber9ce59742018-09-13 10:52:44 +0200465 if (!s3resume)
Patrick Rudolph42609d82020-07-27 16:23:36 +0200466 setup_sdram_meminfo(&ctrl);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200467}
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100468
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100469void perform_raminit(int s3resume)
470{
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100471 post_code(0x3a);
472
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100473 timestamp_add_now(TS_BEFORE_INITRAM);
474
Angel Ponsfc930242020-03-24 11:12:09 +0100475 init_dram_ddr3(s3resume, cpu_get_cpuid());
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100476}