blob: 7beaa9bc958d95fead28dac249e479a6010b1dab [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 support for 3-channel chipsets */
Stefan Reinauer00636b02012-04-04 00:08:51 +020024
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070025static void wait_txt_clear(void)
26{
Angel Pons7c49cb82020-03-16 23:17:32 +010027 struct cpuid_result cp = cpuid_ext(1, 0);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070028
Angel Pons7c49cb82020-03-16 23:17:32 +010029 /* Check if TXT is supported */
30 if (!(cp.ecx & (1 << 6)))
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070031 return;
Angel Pons7c49cb82020-03-16 23:17:32 +010032
33 /* Some TXT public bit */
Elyes Haouas4b7d4052022-12-03 13:24:03 +010034 if (!(read32p(0xfed30010) & 1))
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070035 return;
Angel Pons7c49cb82020-03-16 23:17:32 +010036
37 /* Wait for TXT clear */
Elyes Haouas4b7d4052022-12-03 13:24:03 +010038 while (!(read8p(0xfed40000) & (1 << 7)))
Angel Pons7c49cb82020-03-16 23:17:32 +010039 ;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070040}
41
Angel Pons7c49cb82020-03-16 23:17:32 +010042/* Disable a channel in ramctr_timing */
43static void disable_channel(ramctr_timing *ctrl, int channel)
44{
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +010045 ctrl->rankmap[channel] = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +010046
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +010047 memset(&ctrl->rank_mirror[channel][0], 0, sizeof(ctrl->rank_mirror[0]));
Angel Pons7c49cb82020-03-16 23:17:32 +010048
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +010049 ctrl->channel_size_mb[channel] = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +010050 ctrl->cmd_stretch[channel] = 0;
51 ctrl->mad_dimm[channel] = 0;
52 memset(&ctrl->timings[channel][0], 0, sizeof(ctrl->timings[0]));
Patrick Rudolph74163d62016-11-17 20:02:43 +010053 memset(&ctrl->info.dimm[channel][0], 0, sizeof(ctrl->info.dimm[0]));
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +010054}
55
Angel Pons6724ba42021-01-31 15:06:59 +010056static uint8_t nb_get_ecc_type(const uint32_t capid0_a)
Patrick Rudolph42609d82020-07-27 16:23:36 +020057{
Angel Pons6724ba42021-01-31 15:06:59 +010058 return capid0_a & CAPID_ECCDIS ? MEMORY_ARRAY_ECC_NONE : MEMORY_ARRAY_ECC_SINGLE_BIT;
Patrick Rudolph42609d82020-07-27 16:23:36 +020059}
60
61static uint16_t nb_slots_per_channel(const uint32_t capid0_a)
62{
63 return !(capid0_a & CAPID_DDPCD) + 1;
64}
65
66static uint16_t nb_number_of_channels(const uint32_t capid0_a)
67{
68 return !(capid0_a & CAPID_PDCD) + 1;
69}
70
71static uint32_t nb_max_chan_capacity_mib(const uint32_t capid0_a)
72{
73 uint32_t ddrsz;
74
75 /* Values from documentation, which assume two DIMMs per channel */
76 switch (CAPID_DDRSZ(capid0_a)) {
77 case 1:
78 ddrsz = 8192;
79 break;
80 case 2:
81 ddrsz = 2048;
82 break;
83 case 3:
84 ddrsz = 512;
85 break;
86 default:
87 ddrsz = 16384;
88 break;
89 }
90
91 /* Account for the maximum number of DIMMs per channel */
92 return (ddrsz / 2) * nb_slots_per_channel(capid0_a);
93}
94
95/* Fill cbmem with information for SMBIOS type 16 and type 17 */
96static void setup_sdram_meminfo(ramctr_timing *ctrl)
Patrick Rudolphb97009e2016-02-28 15:24:04 +010097{
Patrick Rudolphb97009e2016-02-28 15:24:04 +010098 int channel, slot;
Patrick Rudolph24efe732018-08-19 11:06:06 +020099 const u16 ddr_freq = (1000 << 8) / ctrl->tCK;
Patrick Rudolphb97009e2016-02-28 15:24:04 +0100100
Elyes HAOUAS12df9502016-08-23 21:29:48 +0200101 FOR_ALL_CHANNELS for (slot = 0; slot < NUM_SLOTS; slot++) {
Patrick Rudolph24efe732018-08-19 11:06:06 +0200102 enum cb_err ret = spd_add_smbios17(channel, slot, ddr_freq,
103 &ctrl->info.dimm[channel][slot]);
104 if (ret != CB_SUCCESS)
105 printk(BIOS_ERR, "RAMINIT: Failed to add SMBIOS17\n");
Patrick Rudolphb97009e2016-02-28 15:24:04 +0100106 }
Patrick Rudolph42609d82020-07-27 16:23:36 +0200107
108 /* The 'spd_add_smbios17' function allocates this CBMEM area */
109 struct memory_info *m = cbmem_find(CBMEM_ID_MEMINFO);
Elyes Haouas5e6b0f02022-09-13 09:55:49 +0200110 if (!m)
Patrick Rudolph42609d82020-07-27 16:23:36 +0200111 return;
112
113 const uint32_t capid0_a = pci_read_config32(HOST_BRIDGE, CAPID0_A);
114
115 const uint16_t channels = nb_number_of_channels(capid0_a);
116
Angel Pons6724ba42021-01-31 15:06:59 +0100117 m->ecc_type = nb_get_ecc_type(capid0_a);
Patrick Rudolph42609d82020-07-27 16:23:36 +0200118 m->max_capacity_mib = channels * nb_max_chan_capacity_mib(capid0_a);
119 m->number_of_devices = channels * nb_slots_per_channel(capid0_a);
Patrick Rudolphb97009e2016-02-28 15:24:04 +0100120}
121
Angel Pons7c49cb82020-03-16 23:17:32 +0100122/* Return CRC16 match for all SPDs */
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100123static int verify_crc16_spds_ddr3(spd_raw_data *spd, ramctr_timing *ctrl)
124{
125 int channel, slot, spd_slot;
126 int match = 1;
127
128 FOR_ALL_CHANNELS {
129 for (slot = 0; slot < NUM_SLOTS; slot++) {
130 spd_slot = 2 * channel + slot;
131 match &= ctrl->spd_crc[channel][slot] ==
Angel Pons7c49cb82020-03-16 23:17:32 +0100132 spd_ddr3_calc_unique_crc(spd[spd_slot], sizeof(spd_raw_data));
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100133 }
134 }
135 return match;
136}
137
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +0200138void read_spd(spd_raw_data * spd, u8 addr, bool id_only)
Stefan Reinauer00636b02012-04-04 00:08:51 +0200139{
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700140 int j;
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +0200141 if (id_only) {
142 for (j = 117; j < 128; j++)
Kyösti Mälkki1a1b04e2020-01-07 22:34:33 +0200143 (*spd)[j] = smbus_read_byte(addr, j);
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +0200144 } else {
145 for (j = 0; j < 256; j++)
Kyösti Mälkki1a1b04e2020-01-07 22:34:33 +0200146 (*spd)[j] = smbus_read_byte(addr, j);
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +0200147 }
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700148}
149
Patrick Rudolph735ecce2016-03-26 10:42:27 +0100150static void dram_find_spds_ddr3(spd_raw_data *spd, ramctr_timing *ctrl)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700151{
Angel Pons7c49cb82020-03-16 23:17:32 +0100152 int dimms = 0, ch_dimms;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700153 int channel, slot, spd_slot;
Patrick Rudolphdd662872017-10-28 18:20:11 +0200154 bool can_use_ecc = ctrl->ecc_supported;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700155
Elyes HAOUAS0d4b11a2016-10-03 21:57:21 +0200156 memset (ctrl->rankmap, 0, sizeof(ctrl->rankmap));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700157
158 ctrl->extended_temperature_range = 1;
159 ctrl->auto_self_refresh = 1;
160
161 FOR_ALL_CHANNELS {
162 ctrl->channel_size_mb[channel] = 0;
163
Angel Pons7c49cb82020-03-16 23:17:32 +0100164 ch_dimms = 0;
165 /* Count dimms on channel */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700166 for (slot = 0; slot < NUM_SLOTS; slot++) {
167 spd_slot = 2 * channel + slot;
Patrick Rudolph5a061852017-09-22 15:19:26 +0200168
Angel Pons035096c2020-09-17 22:31:19 +0200169 if (spd[spd_slot][SPD_MEMORY_TYPE] == SPD_MEMORY_TYPE_SDRAM_DDR3)
Angel Pons7c49cb82020-03-16 23:17:32 +0100170 ch_dimms++;
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100171 }
172
173 for (slot = 0; slot < NUM_SLOTS; slot++) {
174 spd_slot = 2 * channel + slot;
Angel Pons7c49cb82020-03-16 23:17:32 +0100175 printk(BIOS_DEBUG, "SPD probe channel%d, slot%d\n", channel, slot);
Patrick Rudolph5a061852017-09-22 15:19:26 +0200176
Angel Ponsafb3d7e2021-03-28 13:43:13 +0200177 struct dimm_attr_ddr3_st *const dimm = &ctrl->info.dimm[channel][slot];
Angel Pons323c0ae2020-12-12 16:57:37 +0100178
Angel Pons7c49cb82020-03-16 23:17:32 +0100179 /* Search for XMP profile */
Angel Pons323c0ae2020-12-12 16:57:37 +0100180 spd_xmp_decode_ddr3(dimm, spd[spd_slot],
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100181 DDR3_XMP_PROFILE_1);
182
Angel Pons323c0ae2020-12-12 16:57:37 +0100183 if (dimm->dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3) {
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100184 printram("No valid XMP profile found.\n");
Angel Pons323c0ae2020-12-12 16:57:37 +0100185 spd_decode_ddr3(dimm, spd[spd_slot]);
Angel Pons7c49cb82020-03-16 23:17:32 +0100186
Angel Pons323c0ae2020-12-12 16:57:37 +0100187 } else if (ch_dimms > dimm->dimms_per_channel) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100188 printram(
189 "XMP profile supports %u DIMMs, but %u DIMMs are installed.\n",
Angel Pons323c0ae2020-12-12 16:57:37 +0100190 dimm->dimms_per_channel, ch_dimms);
Angel Pons7c49cb82020-03-16 23:17:32 +0100191
Julius Wernercd49cce2019-03-05 16:53:33 -0800192 if (CONFIG(NATIVE_RAMINIT_IGNORE_XMP_MAX_DIMMS))
Angel Pons7c49cb82020-03-16 23:17:32 +0100193 printk(BIOS_WARNING,
194 "XMP maximum DIMMs will be ignored.\n");
Vagiz Trakhanov771be482017-10-02 10:02:35 +0000195 else
Angel Pons323c0ae2020-12-12 16:57:37 +0100196 spd_decode_ddr3(dimm, spd[spd_slot]);
Angel Pons7c49cb82020-03-16 23:17:32 +0100197
Angel Pons323c0ae2020-12-12 16:57:37 +0100198 } else if (dimm->voltage != 1500) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100199 /* TODO: Support DDR3 voltages other than 1500mV */
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100200 printram("XMP profile's requested %u mV is unsupported.\n",
Angel Pons323c0ae2020-12-12 16:57:37 +0100201 dimm->voltage);
Angel Pons3170e9c2020-12-12 16:22:18 +0100202
203 if (CONFIG(NATIVE_RAMINIT_IGNORE_XMP_REQUESTED_VOLTAGE))
204 printk(BIOS_WARNING,
205 "XMP requested voltage will be ignored.\n");
206 else
207 spd_decode_ddr3(dimm, spd[spd_slot]);
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100208 }
209
Angel Pons7c49cb82020-03-16 23:17:32 +0100210 /* Fill in CRC16 for MRC cache */
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100211 ctrl->spd_crc[channel][slot] =
Angel Pons7c49cb82020-03-16 23:17:32 +0100212 spd_ddr3_calc_unique_crc(spd[spd_slot], sizeof(spd_raw_data));
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100213
Angel Pons323c0ae2020-12-12 16:57:37 +0100214 if (dimm->dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100215 /* Mark DIMM as invalid */
Angel Pons323c0ae2020-12-12 16:57:37 +0100216 dimm->ranks = 0;
217 dimm->size_mb = 0;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700218 continue;
219 }
220
Angel Pons323c0ae2020-12-12 16:57:37 +0100221 dram_print_spd_ddr3(dimm);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700222 dimms++;
223 ctrl->rank_mirror[channel][slot * 2] = 0;
Angel Pons323c0ae2020-12-12 16:57:37 +0100224 ctrl->rank_mirror[channel][slot * 2 + 1] = dimm->flags.pins_mirrored;
Angel Pons7c49cb82020-03-16 23:17:32 +0100225
Angel Pons323c0ae2020-12-12 16:57:37 +0100226 ctrl->channel_size_mb[channel] += dimm->size_mb;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700227
Angel Pons323c0ae2020-12-12 16:57:37 +0100228 if (!dimm->flags.is_ecc)
Patrick Rudolphdd662872017-10-28 18:20:11 +0200229 can_use_ecc = false;
230
Angel Pons323c0ae2020-12-12 16:57:37 +0100231 ctrl->auto_self_refresh &= dimm->flags.asr;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700232
Angel Pons323c0ae2020-12-12 16:57:37 +0100233 ctrl->extended_temperature_range &= dimm->flags.ext_temp_refresh;
Angel Pons7c49cb82020-03-16 23:17:32 +0100234
Angel Pons323c0ae2020-12-12 16:57:37 +0100235 ctrl->rankmap[channel] |= ((1 << dimm->ranks) - 1) << (2 * slot);
Angel Pons7c49cb82020-03-16 23:17:32 +0100236
237 printk(BIOS_DEBUG, "channel[%d] rankmap = 0x%x\n", channel,
238 ctrl->rankmap[channel]);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700239 }
Angel Pons7c49cb82020-03-16 23:17:32 +0100240
Angel Ponsd4d3ba02020-12-12 17:45:14 +0100241 const u8 rc_0 = ctrl->info.dimm[channel][0].reference_card;
242 const u8 rc_1 = ctrl->info.dimm[channel][1].reference_card;
243
244 if (ch_dimms == NUM_SLOTS && rc_0 < 6 && rc_1 < 6) {
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700245 const int ref_card_offset_table[6][6] = {
Angel Pons7c49cb82020-03-16 23:17:32 +0100246 { 0, 0, 0, 0, 2, 2 },
247 { 0, 0, 0, 0, 2, 2 },
248 { 0, 0, 0, 0, 2, 2 },
249 { 0, 0, 0, 0, 1, 1 },
250 { 2, 2, 2, 1, 0, 0 },
251 { 2, 2, 2, 1, 0, 0 },
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700252 };
Angel Ponsd4d3ba02020-12-12 17:45:14 +0100253 ctrl->ref_card_offset[channel] = ref_card_offset_table[rc_0][rc_1];
Angel Pons7c49cb82020-03-16 23:17:32 +0100254 } else {
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700255 ctrl->ref_card_offset[channel] = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100256 }
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700257 }
258
Patrick Rudolphdd662872017-10-28 18:20:11 +0200259 if (ctrl->ecc_forced || CONFIG(RAMINIT_ENABLE_ECC))
260 ctrl->ecc_enabled = can_use_ecc;
261 if (ctrl->ecc_forced && !ctrl->ecc_enabled)
262 die("ECC mode forced but non-ECC DIMM installed!");
263 printk(BIOS_DEBUG, "ECC is %s\n", ctrl->ecc_enabled ? "enabled" : "disabled");
264
265 ctrl->lanes = ctrl->ecc_enabled ? 9 : 8;
266
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700267 if (!dimms)
268 die("No DIMMs were found");
269}
270
Patrick Rudolphbb9c90a2016-05-29 17:05:06 +0200271static void save_timings(ramctr_timing *ctrl)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700272{
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700273 /* Save the MRC S3 restore data to cbmem */
Angel Pons7c49cb82020-03-16 23:17:32 +0100274 mrc_cache_stash_data(MRC_TRAINING_DATA, MRC_CACHE_VERSION, ctrl, sizeof(*ctrl));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700275}
276
Angel Ponsfc930242020-03-24 11:12:09 +0100277static void reinit_ctrl(ramctr_timing *ctrl, const u32 cpuid)
Patrick Rudolph05d4bf7e2017-10-28 16:36:09 +0200278{
279 /* Reset internal state */
280 memset(ctrl, 0, sizeof(*ctrl));
Patrick Rudolph05d4bf7e2017-10-28 16:36:09 +0200281
282 /* Get architecture */
283 ctrl->cpu = cpuid;
284
285 /* Get ECC support and mode */
286 ctrl->ecc_forced = get_host_ecc_forced();
287 ctrl->ecc_supported = ctrl->ecc_forced || get_host_ecc_cap();
288 printk(BIOS_DEBUG, "ECC supported: %s ECC forced: %s\n",
289 ctrl->ecc_supported ? "yes" : "no",
290 ctrl->ecc_forced ? "yes" : "no");
291}
292
Angel Ponsfc930242020-03-24 11:12:09 +0100293static void init_dram_ddr3(int s3resume, const u32 cpuid)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700294{
Angel Pons7c49cb82020-03-16 23:17:32 +0100295 int me_uma_size, cbmem_was_inited, fast_boot, err;
Patrick Rudolph735ecce2016-03-26 10:42:27 +0100296 ramctr_timing ctrl;
Kyösti Mälkki4cb44e52016-11-18 19:11:24 +0200297 spd_raw_data spds[4];
Shelley Chenad9cd682020-07-23 16:10:52 -0700298 size_t mrc_size;
Angel Ponsa6a64182020-03-21 18:06:03 +0100299 ramctr_timing *ctrl_cached = NULL;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700300
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100301 timestamp_add_now(TS_INITRAM_START);
Kyösti Mälkkib33c6fb2021-02-17 20:43:04 +0200302
Angel Pons66780a02021-03-26 13:33:22 +0100303 mchbar_setbits32(SAPMCTL, 1 << 0);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200304
305 /* Wait for ME to be ready */
306 intel_early_me_init();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700307 me_uma_size = intel_early_me_uma_size();
Stefan Reinauer00636b02012-04-04 00:08:51 +0200308
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700309 printk(BIOS_DEBUG, "Starting native Platform init\n");
Stefan Reinauer00636b02012-04-04 00:08:51 +0200310
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700311 wait_txt_clear();
Stefan Reinauer00636b02012-04-04 00:08:51 +0200312
Angel Pons5db1b152020-12-13 16:37:53 +0100313 wrmsr(0x2e6, (msr_t) { .lo = 0, .hi = 0 });
Stefan Reinauer00636b02012-04-04 00:08:51 +0200314
Angel Pons66780a02021-03-26 13:33:22 +0100315 const u32 sskpd = mchbar_read32(SSKPD); // !!! = 0x00000000
Angel Pons7c49cb82020-03-16 23:17:32 +0100316 if ((pci_read_config16(SOUTHBRIDGE, 0xa2) & 0xa0) == 0x20 && sskpd && !s3resume) {
Angel Pons66780a02021-03-26 13:33:22 +0100317 mchbar_write32(SSKPD, 0);
Angel Pons7c49cb82020-03-16 23:17:32 +0100318 /* Need reset */
Elyes HAOUASc0567292019-04-28 17:57:47 +0200319 system_reset();
Stefan Reinauer00636b02012-04-04 00:08:51 +0200320 }
Stefan Reinauer00636b02012-04-04 00:08:51 +0200321
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700322 early_pch_init_native();
Patrick Rudolph6aca7e62019-03-26 18:22:36 +0100323 early_init_dmi();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700324 early_thermal_init();
325
Angel Pons7c49cb82020-03-16 23:17:32 +0100326 /* Try to find timings in MRC cache */
Shelley Chenad9cd682020-07-23 16:10:52 -0700327 ctrl_cached = mrc_cache_current_mmap_leak(MRC_TRAINING_DATA,
328 MRC_CACHE_VERSION,
329 &mrc_size);
330 if (mrc_size < sizeof(ctrl))
331 ctrl_cached = NULL;
Angel Ponsa6a64182020-03-21 18:06:03 +0100332
333 /* Before reusing training data, assert that the CPU has not been replaced */
334 if (ctrl_cached && cpuid != ctrl_cached->cpu) {
335
336 /* It is not really worrying on a cold boot, but fatal when resuming from S3 */
337 printk(s3resume ? BIOS_ALERT : BIOS_NOTICE,
338 "CPUID %x differs from stored CPUID %x, CPU was replaced!\n",
339 cpuid, ctrl_cached->cpu);
340
341 /* Invalidate the stored data, it likely does not apply to the current CPU */
342 ctrl_cached = NULL;
343 }
344
345 if (s3resume && !ctrl_cached) {
346 /* S3 resume is impossible, reset to come up cleanly */
347 system_reset();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700348 }
349
Angel Pons7c49cb82020-03-16 23:17:32 +0100350 /* Verify MRC cache for fast boot */
Kyösti Mälkki38cb8222016-11-18 19:25:52 +0200351 if (!s3resume && ctrl_cached) {
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +0200352 /* Load SPD unique information data. */
353 memset(spds, 0, sizeof(spds));
354 mainboard_get_spd(spds, 1);
355
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100356 /* check SPD CRC16 to make sure the DIMMs haven't been replaced */
357 fast_boot = verify_crc16_spds_ddr3(spds, ctrl_cached);
358 if (!fast_boot)
359 printk(BIOS_DEBUG, "Stored timings CRC16 mismatch.\n");
Kyösti Mälkki38cb8222016-11-18 19:25:52 +0200360 } else {
361 fast_boot = s3resume;
362 }
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100363
364 if (fast_boot) {
365 printk(BIOS_DEBUG, "Trying stored timings.\n");
366 memcpy(&ctrl, ctrl_cached, sizeof(ctrl));
367
Patrick Rudolph588ccaa2016-04-20 18:00:27 +0200368 err = try_init_dram_ddr3(&ctrl, fast_boot, s3resume, me_uma_size);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100369 if (err) {
Patrick Rudolph588ccaa2016-04-20 18:00:27 +0200370 if (s3resume) {
371 /* Failed S3 resume, reset to come up cleanly */
Elyes HAOUASc0567292019-04-28 17:57:47 +0200372 system_reset();
Patrick Rudolph588ccaa2016-04-20 18:00:27 +0200373 }
Angel Pons7c49cb82020-03-16 23:17:32 +0100374 /* No need to erase bad MRC cache here, it gets overwritten on a
375 successful boot */
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100376 printk(BIOS_ERR, "Stored timings are invalid !\n");
377 fast_boot = 0;
378 }
379 }
380 if (!fast_boot) {
Patrick Rudolphe74ad212016-11-16 18:06:50 +0100381 /* Reset internal state */
Angel Ponsfc930242020-03-24 11:12:09 +0100382 reinit_ctrl(&ctrl, cpuid);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100383
Patrick Rudolph05d4bf7e2017-10-28 16:36:09 +0200384 printk(BIOS_INFO, "ECC RAM %s.\n", ctrl.ecc_forced ? "required" :
385 ctrl.ecc_supported ? "supported" : "unsupported");
Patrick Rudolph305035c2016-11-11 18:38:50 +0100386
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100387 /* Get DDR3 SPD data */
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +0200388 memset(spds, 0, sizeof(spds));
389 mainboard_get_spd(spds, 0);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100390 dram_find_spds_ddr3(spds, &ctrl);
391
Patrick Rudolph588ccaa2016-04-20 18:00:27 +0200392 err = try_init_dram_ddr3(&ctrl, fast_boot, s3resume, me_uma_size);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100393 }
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +0100394
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +0100395 if (err) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100396 /* Fallback: disable failing channel */
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +0100397 printk(BIOS_ERR, "RAM training failed, trying fallback.\n");
398 printram("Disable failing channel.\n");
399
Patrick Rudolphe74ad212016-11-16 18:06:50 +0100400 /* Reset internal state */
Angel Ponsfc930242020-03-24 11:12:09 +0100401 reinit_ctrl(&ctrl, cpuid);
Patrick Rudolph305035c2016-11-11 18:38:50 +0100402
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +0100403 /* Reset DDR3 frequency */
404 dram_find_spds_ddr3(spds, &ctrl);
405
Angel Pons7c49cb82020-03-16 23:17:32 +0100406 /* Disable failing channel */
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +0100407 disable_channel(&ctrl, GET_ERR_CHANNEL(err));
408
409 err = try_init_dram_ddr3(&ctrl, fast_boot, s3resume, me_uma_size);
410 }
411
Patrick Rudolph31d19592016-03-26 12:22:34 +0100412 if (err)
413 die("raminit failed");
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700414
Angel Pons88521882020-01-05 20:21:20 +0100415 /* FIXME: should be hardware revision-dependent. The register only exists on IVB. */
Angel Pons66780a02021-03-26 13:33:22 +0100416 mchbar_write32(CHANNEL_HASH, 0x00a030ce);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700417
418 set_scrambling_seed(&ctrl);
419
Patrick Rudolphd0581312020-05-01 18:31:48 +0200420 if (!s3resume && ctrl.ecc_enabled)
421 channel_scrub(&ctrl);
422
Angel Pons88521882020-01-05 20:21:20 +0100423 set_normal_operation(&ctrl);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700424
425 final_registers(&ctrl);
426
Patrick Rudolphd0581312020-05-01 18:31:48 +0200427 /* can't do this earlier because it needs to be done in normal operation */
428 if (CONFIG(DEBUG_RAM_SETUP) && !s3resume && ctrl.ecc_enabled) {
429 uint32_t i, tseg = pci_read_config32(HOST_BRIDGE, TSEGMB);
430
431 printk(BIOS_INFO, "RAMINIT: ECC scrub test on first channel up to 0x%x\n",
432 tseg);
433
434 /*
435 * This test helps to debug the ECC scrubbing.
436 * It likely tests every channel/rank, as rank interleave and enhanced
437 * interleave are enabled, but there's no guarantee for it.
438 */
439
440 /* Skip first MB to avoid special case for A-seg and test up to TSEG */
441 for (i = 1; i < tseg >> 20; i++) {
442 for (int j = 0; j < 1 * MiB; j += 4096) {
443 uintptr_t addr = i * MiB + j;
444 if (read32((u32 *)addr) == 0)
445 continue;
446
447 printk(BIOS_ERR, "RAMINIT: ECC scrub: DRAM not cleared at"
448 " addr 0x%lx\n", addr);
449 break;
450 }
451 }
452 printk(BIOS_INFO, "RAMINIT: ECC scrub test done.\n");
453 }
454
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700455 /* Zone config */
456 dram_zones(&ctrl, 0);
457
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700458 intel_early_me_init_done(ME_INIT_STATUS_SUCCESS);
459 intel_early_me_status();
460
Stefan Reinauer00636b02012-04-04 00:08:51 +0200461 report_memory_config();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700462
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100463 timestamp_add_now(TS_INITRAM_END);
Kyösti Mälkkib33c6fb2021-02-17 20:43:04 +0200464
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700465 cbmem_was_inited = !cbmem_recovery(s3resume);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100466 if (!fast_boot)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700467 save_timings(&ctrl);
468 if (s3resume && !cbmem_was_inited) {
469 /* Failed S3 resume, reset to come up cleanly */
Elyes HAOUASc0567292019-04-28 17:57:47 +0200470 system_reset();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700471 }
Patrick Rudolphb97009e2016-02-28 15:24:04 +0100472
Nico Huber9ce59742018-09-13 10:52:44 +0200473 if (!s3resume)
Patrick Rudolph42609d82020-07-27 16:23:36 +0200474 setup_sdram_meminfo(&ctrl);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200475}
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100476
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100477void perform_raminit(int s3resume)
478{
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100479 post_code(0x3a);
Angel Ponsfc930242020-03-24 11:12:09 +0100480 init_dram_ddr3(s3resume, cpu_get_cpuid());
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100481}