blob: 2575a9c6fc08e68e19d20e80a64d0e48cf4224e7 [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>
Keith Hui1e9601c2023-07-15 12:08:51 -04007#include <cbfs.h>
Subrata Banik53b08c32018-12-10 14:11:35 +05308#include <arch/cpu.h>
Keith Hui1e9601c2023-07-15 12:08:51 -04009#include <device/device.h>
10#include <device/dram/ddr3.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +020011#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020012#include <device/pci_ops.h>
Kyösti Mälkki1cae4542020-01-06 12:31:34 +020013#include <device/smbus_host.h>
Stefan Reinauer00636b02012-04-04 00:08:51 +020014#include <cbmem.h>
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +010015#include <timestamp.h>
Arthur Heymans7539b8c2017-12-24 10:42:57 +010016#include <mrc_cache.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010017#include <southbridge/intel/bd82x6x/me.h>
Patrick Rudolphda9302a2019-03-24 17:01:41 +010018#include <southbridge/intel/bd82x6x/pch.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010019#include <cpu/x86/msr.h>
Elyes HAOUAS51401c32019-05-15 21:09:30 +020020#include <types.h>
Elyes HAOUASbf0970e2019-03-21 11:10:03 +010021
Keith Hui1e9601c2023-07-15 12:08:51 -040022#include "raminit.h"
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010023#include "raminit_native.h"
24#include "raminit_common.h"
25#include "sandybridge.h"
Keith Hui1e9601c2023-07-15 12:08:51 -040026#include "chip.h"
Stefan Reinauer00636b02012-04-04 00:08:51 +020027
Angel Pons7c49cb82020-03-16 23:17:32 +010028/* FIXME: no support for 3-channel chipsets */
Stefan Reinauer00636b02012-04-04 00:08:51 +020029
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070030static void wait_txt_clear(void)
31{
Angel Pons7c49cb82020-03-16 23:17:32 +010032 struct cpuid_result cp = cpuid_ext(1, 0);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070033
Angel Pons7c49cb82020-03-16 23:17:32 +010034 /* Check if TXT is supported */
35 if (!(cp.ecx & (1 << 6)))
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070036 return;
Angel Pons7c49cb82020-03-16 23:17:32 +010037
38 /* Some TXT public bit */
Elyes Haouas4b7d4052022-12-03 13:24:03 +010039 if (!(read32p(0xfed30010) & 1))
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070040 return;
Angel Pons7c49cb82020-03-16 23:17:32 +010041
42 /* Wait for TXT clear */
Elyes Haouas4b7d4052022-12-03 13:24:03 +010043 while (!(read8p(0xfed40000) & (1 << 7)))
Angel Pons7c49cb82020-03-16 23:17:32 +010044 ;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070045}
46
Angel Pons7c49cb82020-03-16 23:17:32 +010047/* Disable a channel in ramctr_timing */
48static void disable_channel(ramctr_timing *ctrl, int channel)
49{
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +010050 ctrl->rankmap[channel] = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +010051
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +010052 memset(&ctrl->rank_mirror[channel][0], 0, sizeof(ctrl->rank_mirror[0]));
Angel Pons7c49cb82020-03-16 23:17:32 +010053
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +010054 ctrl->channel_size_mb[channel] = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +010055 ctrl->cmd_stretch[channel] = 0;
56 ctrl->mad_dimm[channel] = 0;
57 memset(&ctrl->timings[channel][0], 0, sizeof(ctrl->timings[0]));
Patrick Rudolph74163d62016-11-17 20:02:43 +010058 memset(&ctrl->info.dimm[channel][0], 0, sizeof(ctrl->info.dimm[0]));
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +010059}
60
Angel Pons6724ba42021-01-31 15:06:59 +010061static uint8_t nb_get_ecc_type(const uint32_t capid0_a)
Patrick Rudolph42609d82020-07-27 16:23:36 +020062{
Angel Pons6724ba42021-01-31 15:06:59 +010063 return capid0_a & CAPID_ECCDIS ? MEMORY_ARRAY_ECC_NONE : MEMORY_ARRAY_ECC_SINGLE_BIT;
Patrick Rudolph42609d82020-07-27 16:23:36 +020064}
65
66static uint16_t nb_slots_per_channel(const uint32_t capid0_a)
67{
68 return !(capid0_a & CAPID_DDPCD) + 1;
69}
70
71static uint16_t nb_number_of_channels(const uint32_t capid0_a)
72{
73 return !(capid0_a & CAPID_PDCD) + 1;
74}
75
76static uint32_t nb_max_chan_capacity_mib(const uint32_t capid0_a)
77{
78 uint32_t ddrsz;
79
80 /* Values from documentation, which assume two DIMMs per channel */
81 switch (CAPID_DDRSZ(capid0_a)) {
82 case 1:
83 ddrsz = 8192;
84 break;
85 case 2:
86 ddrsz = 2048;
87 break;
88 case 3:
89 ddrsz = 512;
90 break;
91 default:
92 ddrsz = 16384;
93 break;
94 }
95
96 /* Account for the maximum number of DIMMs per channel */
97 return (ddrsz / 2) * nb_slots_per_channel(capid0_a);
98}
99
100/* Fill cbmem with information for SMBIOS type 16 and type 17 */
101static void setup_sdram_meminfo(ramctr_timing *ctrl)
Patrick Rudolphb97009e2016-02-28 15:24:04 +0100102{
Patrick Rudolphb97009e2016-02-28 15:24:04 +0100103 int channel, slot;
Patrick Rudolph24efe732018-08-19 11:06:06 +0200104 const u16 ddr_freq = (1000 << 8) / ctrl->tCK;
Patrick Rudolphb97009e2016-02-28 15:24:04 +0100105
Elyes HAOUAS12df9502016-08-23 21:29:48 +0200106 FOR_ALL_CHANNELS for (slot = 0; slot < NUM_SLOTS; slot++) {
Patrick Rudolph24efe732018-08-19 11:06:06 +0200107 enum cb_err ret = spd_add_smbios17(channel, slot, ddr_freq,
108 &ctrl->info.dimm[channel][slot]);
109 if (ret != CB_SUCCESS)
110 printk(BIOS_ERR, "RAMINIT: Failed to add SMBIOS17\n");
Patrick Rudolphb97009e2016-02-28 15:24:04 +0100111 }
Patrick Rudolph42609d82020-07-27 16:23:36 +0200112
113 /* The 'spd_add_smbios17' function allocates this CBMEM area */
114 struct memory_info *m = cbmem_find(CBMEM_ID_MEMINFO);
Elyes Haouas5e6b0f02022-09-13 09:55:49 +0200115 if (!m)
Patrick Rudolph42609d82020-07-27 16:23:36 +0200116 return;
117
118 const uint32_t capid0_a = pci_read_config32(HOST_BRIDGE, CAPID0_A);
119
120 const uint16_t channels = nb_number_of_channels(capid0_a);
121
Angel Pons6724ba42021-01-31 15:06:59 +0100122 m->ecc_type = nb_get_ecc_type(capid0_a);
Patrick Rudolph42609d82020-07-27 16:23:36 +0200123 m->max_capacity_mib = channels * nb_max_chan_capacity_mib(capid0_a);
124 m->number_of_devices = channels * nb_slots_per_channel(capid0_a);
Patrick Rudolphb97009e2016-02-28 15:24:04 +0100125}
126
Angel Pons7c49cb82020-03-16 23:17:32 +0100127/* Return CRC16 match for all SPDs */
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100128static int verify_crc16_spds_ddr3(spd_raw_data *spd, ramctr_timing *ctrl)
129{
130 int channel, slot, spd_slot;
131 int match = 1;
132
133 FOR_ALL_CHANNELS {
134 for (slot = 0; slot < NUM_SLOTS; slot++) {
135 spd_slot = 2 * channel + slot;
136 match &= ctrl->spd_crc[channel][slot] ==
Angel Pons7c49cb82020-03-16 23:17:32 +0100137 spd_ddr3_calc_unique_crc(spd[spd_slot], sizeof(spd_raw_data));
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100138 }
139 }
140 return match;
141}
142
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +0200143void read_spd(spd_raw_data * spd, u8 addr, bool id_only)
Stefan Reinauer00636b02012-04-04 00:08:51 +0200144{
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700145 int j;
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +0200146 if (id_only) {
Keith Hui1e9601c2023-07-15 12:08:51 -0400147 for (j = SPD_DIMM_MOD_ID1; j < 128; j++)
Kyösti Mälkki1a1b04e2020-01-07 22:34:33 +0200148 (*spd)[j] = smbus_read_byte(addr, j);
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +0200149 } else {
Keith Hui1e9601c2023-07-15 12:08:51 -0400150 for (j = 0; j < SPD_SIZE_MAX_DDR3; j++)
Kyösti Mälkki1a1b04e2020-01-07 22:34:33 +0200151 (*spd)[j] = smbus_read_byte(addr, j);
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +0200152 }
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700153}
154
Keith Hui1e9601c2023-07-15 12:08:51 -0400155/* Temporary stub */
156__weak void mb_get_spd_map(struct spd_info *spdi) {}
157
158__weak void mainboard_get_spd(spd_raw_data *spd, bool id_only)
159{
160 const struct northbridge_intel_sandybridge_config *cfg = config_of_soc();
161 unsigned int i;
162
163 if (CONFIG(HAVE_SPD_IN_CBFS)) {
164 struct spd_info spdi = {0};
165
166 mb_get_spd_map(&spdi);
167
168 size_t spd_file_len;
169 uint8_t *spd_file = cbfs_map("spd.bin", &spd_file_len);
170
171 printk(BIOS_DEBUG, "SPD index %d\n", spdi.spd_index);
172
173 /* SPD file sanity check */
174 if (!spd_file)
175 die("SPD data %s!", "not found");
176
177 if (spd_file_len < ((spdi.spd_index + 1) * SPD_SIZE_MAX_DDR3))
178 die("SPD data %s!", "incomplete");
179
180 /*
181 * Copy SPD data specified by spd_info.spd_index to all slots marked as
182 * SPD_MEMORY_DOWN.
183 *
184 * Read SPD data from slots with a real SMBus address.
185 */
186 for (i = 0; i < ARRAY_SIZE(spdi.addresses); i++) {
187 if (spdi.addresses[i] == SPD_MEMORY_DOWN)
188 memcpy(&spd[i], spd_file + (spdi.spd_index * SPD_SIZE_MAX_DDR3), SPD_SIZE_MAX_DDR3);
189 else if (spdi.addresses[i] != 0)
190 read_spd(&spd[i], spdi.addresses[i], id_only);
191 }
192 } else {
193 for (i = 0; i < ARRAY_SIZE(cfg->spd_addresses); i++) {
194 if (cfg->spd_addresses[i] != 0)
195 read_spd(&spd[i], cfg->spd_addresses[i], id_only);
196 }
197 } /* CONFIG(HAVE_SPD_IN_CBFS) */
198}
199
Patrick Rudolph735ecce2016-03-26 10:42:27 +0100200static void dram_find_spds_ddr3(spd_raw_data *spd, ramctr_timing *ctrl)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700201{
Angel Pons7c49cb82020-03-16 23:17:32 +0100202 int dimms = 0, ch_dimms;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700203 int channel, slot, spd_slot;
Patrick Rudolphdd662872017-10-28 18:20:11 +0200204 bool can_use_ecc = ctrl->ecc_supported;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700205
Elyes Haouas9d450b22023-09-10 10:30:29 +0200206 memset(ctrl->rankmap, 0, sizeof(ctrl->rankmap));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700207
208 ctrl->extended_temperature_range = 1;
209 ctrl->auto_self_refresh = 1;
210
211 FOR_ALL_CHANNELS {
212 ctrl->channel_size_mb[channel] = 0;
213
Angel Pons7c49cb82020-03-16 23:17:32 +0100214 ch_dimms = 0;
215 /* Count dimms on channel */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700216 for (slot = 0; slot < NUM_SLOTS; slot++) {
217 spd_slot = 2 * channel + slot;
Patrick Rudolph5a061852017-09-22 15:19:26 +0200218
Angel Pons035096c2020-09-17 22:31:19 +0200219 if (spd[spd_slot][SPD_MEMORY_TYPE] == SPD_MEMORY_TYPE_SDRAM_DDR3)
Angel Pons7c49cb82020-03-16 23:17:32 +0100220 ch_dimms++;
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100221 }
222
223 for (slot = 0; slot < NUM_SLOTS; slot++) {
224 spd_slot = 2 * channel + slot;
Angel Pons7c49cb82020-03-16 23:17:32 +0100225 printk(BIOS_DEBUG, "SPD probe channel%d, slot%d\n", channel, slot);
Patrick Rudolph5a061852017-09-22 15:19:26 +0200226
Angel Ponsafb3d7e2021-03-28 13:43:13 +0200227 struct dimm_attr_ddr3_st *const dimm = &ctrl->info.dimm[channel][slot];
Angel Pons323c0ae2020-12-12 16:57:37 +0100228
Angel Pons7c49cb82020-03-16 23:17:32 +0100229 /* Search for XMP profile */
Angel Pons323c0ae2020-12-12 16:57:37 +0100230 spd_xmp_decode_ddr3(dimm, spd[spd_slot],
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100231 DDR3_XMP_PROFILE_1);
232
Angel Pons323c0ae2020-12-12 16:57:37 +0100233 if (dimm->dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3) {
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100234 printram("No valid XMP profile found.\n");
Angel Pons323c0ae2020-12-12 16:57:37 +0100235 spd_decode_ddr3(dimm, spd[spd_slot]);
Angel Pons7c49cb82020-03-16 23:17:32 +0100236
Angel Pons323c0ae2020-12-12 16:57:37 +0100237 } else if (ch_dimms > dimm->dimms_per_channel) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100238 printram(
239 "XMP profile supports %u DIMMs, but %u DIMMs are installed.\n",
Angel Pons323c0ae2020-12-12 16:57:37 +0100240 dimm->dimms_per_channel, ch_dimms);
Angel Pons7c49cb82020-03-16 23:17:32 +0100241
Julius Wernercd49cce2019-03-05 16:53:33 -0800242 if (CONFIG(NATIVE_RAMINIT_IGNORE_XMP_MAX_DIMMS))
Angel Pons7c49cb82020-03-16 23:17:32 +0100243 printk(BIOS_WARNING,
244 "XMP maximum DIMMs will be ignored.\n");
Vagiz Trakhanov771be482017-10-02 10:02:35 +0000245 else
Angel Pons323c0ae2020-12-12 16:57:37 +0100246 spd_decode_ddr3(dimm, spd[spd_slot]);
Angel Pons7c49cb82020-03-16 23:17:32 +0100247
Angel Pons323c0ae2020-12-12 16:57:37 +0100248 } else if (dimm->voltage != 1500) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100249 /* TODO: Support DDR3 voltages other than 1500mV */
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100250 printram("XMP profile's requested %u mV is unsupported.\n",
Angel Pons323c0ae2020-12-12 16:57:37 +0100251 dimm->voltage);
Angel Pons3170e9c2020-12-12 16:22:18 +0100252
253 if (CONFIG(NATIVE_RAMINIT_IGNORE_XMP_REQUESTED_VOLTAGE))
254 printk(BIOS_WARNING,
255 "XMP requested voltage will be ignored.\n");
256 else
257 spd_decode_ddr3(dimm, spd[spd_slot]);
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100258 }
259
Angel Pons7c49cb82020-03-16 23:17:32 +0100260 /* Fill in CRC16 for MRC cache */
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100261 ctrl->spd_crc[channel][slot] =
Angel Pons7c49cb82020-03-16 23:17:32 +0100262 spd_ddr3_calc_unique_crc(spd[spd_slot], sizeof(spd_raw_data));
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100263
Angel Pons323c0ae2020-12-12 16:57:37 +0100264 if (dimm->dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100265 /* Mark DIMM as invalid */
Angel Pons323c0ae2020-12-12 16:57:37 +0100266 dimm->ranks = 0;
267 dimm->size_mb = 0;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700268 continue;
269 }
270
Angel Pons323c0ae2020-12-12 16:57:37 +0100271 dram_print_spd_ddr3(dimm);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700272 dimms++;
273 ctrl->rank_mirror[channel][slot * 2] = 0;
Angel Pons323c0ae2020-12-12 16:57:37 +0100274 ctrl->rank_mirror[channel][slot * 2 + 1] = dimm->flags.pins_mirrored;
Angel Pons7c49cb82020-03-16 23:17:32 +0100275
Angel Pons323c0ae2020-12-12 16:57:37 +0100276 ctrl->channel_size_mb[channel] += dimm->size_mb;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700277
Angel Pons323c0ae2020-12-12 16:57:37 +0100278 if (!dimm->flags.is_ecc)
Patrick Rudolphdd662872017-10-28 18:20:11 +0200279 can_use_ecc = false;
280
Angel Pons323c0ae2020-12-12 16:57:37 +0100281 ctrl->auto_self_refresh &= dimm->flags.asr;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700282
Angel Pons323c0ae2020-12-12 16:57:37 +0100283 ctrl->extended_temperature_range &= dimm->flags.ext_temp_refresh;
Angel Pons7c49cb82020-03-16 23:17:32 +0100284
Angel Pons323c0ae2020-12-12 16:57:37 +0100285 ctrl->rankmap[channel] |= ((1 << dimm->ranks) - 1) << (2 * slot);
Angel Pons7c49cb82020-03-16 23:17:32 +0100286
287 printk(BIOS_DEBUG, "channel[%d] rankmap = 0x%x\n", channel,
288 ctrl->rankmap[channel]);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700289 }
Angel Pons7c49cb82020-03-16 23:17:32 +0100290
Angel Ponsd4d3ba02020-12-12 17:45:14 +0100291 const u8 rc_0 = ctrl->info.dimm[channel][0].reference_card;
292 const u8 rc_1 = ctrl->info.dimm[channel][1].reference_card;
293
294 if (ch_dimms == NUM_SLOTS && rc_0 < 6 && rc_1 < 6) {
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700295 const int ref_card_offset_table[6][6] = {
Angel Pons7c49cb82020-03-16 23:17:32 +0100296 { 0, 0, 0, 0, 2, 2 },
297 { 0, 0, 0, 0, 2, 2 },
298 { 0, 0, 0, 0, 2, 2 },
299 { 0, 0, 0, 0, 1, 1 },
300 { 2, 2, 2, 1, 0, 0 },
301 { 2, 2, 2, 1, 0, 0 },
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700302 };
Angel Ponsd4d3ba02020-12-12 17:45:14 +0100303 ctrl->ref_card_offset[channel] = ref_card_offset_table[rc_0][rc_1];
Angel Pons7c49cb82020-03-16 23:17:32 +0100304 } else {
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700305 ctrl->ref_card_offset[channel] = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100306 }
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700307 }
308
Patrick Rudolphdd662872017-10-28 18:20:11 +0200309 if (ctrl->ecc_forced || CONFIG(RAMINIT_ENABLE_ECC))
310 ctrl->ecc_enabled = can_use_ecc;
311 if (ctrl->ecc_forced && !ctrl->ecc_enabled)
312 die("ECC mode forced but non-ECC DIMM installed!");
313 printk(BIOS_DEBUG, "ECC is %s\n", ctrl->ecc_enabled ? "enabled" : "disabled");
314
315 ctrl->lanes = ctrl->ecc_enabled ? 9 : 8;
316
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700317 if (!dimms)
318 die("No DIMMs were found");
319}
320
Patrick Rudolphbb9c90a2016-05-29 17:05:06 +0200321static void save_timings(ramctr_timing *ctrl)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700322{
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700323 /* Save the MRC S3 restore data to cbmem */
Angel Pons7c49cb82020-03-16 23:17:32 +0100324 mrc_cache_stash_data(MRC_TRAINING_DATA, MRC_CACHE_VERSION, ctrl, sizeof(*ctrl));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700325}
326
Angel Ponsfc930242020-03-24 11:12:09 +0100327static void reinit_ctrl(ramctr_timing *ctrl, const u32 cpuid)
Patrick Rudolph05d4bf7e2017-10-28 16:36:09 +0200328{
329 /* Reset internal state */
330 memset(ctrl, 0, sizeof(*ctrl));
Patrick Rudolph05d4bf7e2017-10-28 16:36:09 +0200331
332 /* Get architecture */
333 ctrl->cpu = cpuid;
334
335 /* Get ECC support and mode */
336 ctrl->ecc_forced = get_host_ecc_forced();
337 ctrl->ecc_supported = ctrl->ecc_forced || get_host_ecc_cap();
338 printk(BIOS_DEBUG, "ECC supported: %s ECC forced: %s\n",
339 ctrl->ecc_supported ? "yes" : "no",
340 ctrl->ecc_forced ? "yes" : "no");
341}
342
Angel Ponsfc930242020-03-24 11:12:09 +0100343static void init_dram_ddr3(int s3resume, const u32 cpuid)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700344{
Angel Pons7c49cb82020-03-16 23:17:32 +0100345 int me_uma_size, cbmem_was_inited, fast_boot, err;
Patrick Rudolph735ecce2016-03-26 10:42:27 +0100346 ramctr_timing ctrl;
Kyösti Mälkki4cb44e52016-11-18 19:11:24 +0200347 spd_raw_data spds[4];
Shelley Chenad9cd682020-07-23 16:10:52 -0700348 size_t mrc_size;
Angel Ponsa6a64182020-03-21 18:06:03 +0100349 ramctr_timing *ctrl_cached = NULL;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700350
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100351 timestamp_add_now(TS_INITRAM_START);
Kyösti Mälkkib33c6fb2021-02-17 20:43:04 +0200352
Angel Pons66780a02021-03-26 13:33:22 +0100353 mchbar_setbits32(SAPMCTL, 1 << 0);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200354
355 /* Wait for ME to be ready */
356 intel_early_me_init();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700357 me_uma_size = intel_early_me_uma_size();
Stefan Reinauer00636b02012-04-04 00:08:51 +0200358
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700359 printk(BIOS_DEBUG, "Starting native Platform init\n");
Stefan Reinauer00636b02012-04-04 00:08:51 +0200360
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700361 wait_txt_clear();
Stefan Reinauer00636b02012-04-04 00:08:51 +0200362
Angel Pons5db1b152020-12-13 16:37:53 +0100363 wrmsr(0x2e6, (msr_t) { .lo = 0, .hi = 0 });
Stefan Reinauer00636b02012-04-04 00:08:51 +0200364
Angel Pons66780a02021-03-26 13:33:22 +0100365 const u32 sskpd = mchbar_read32(SSKPD); // !!! = 0x00000000
Angel Pons7c49cb82020-03-16 23:17:32 +0100366 if ((pci_read_config16(SOUTHBRIDGE, 0xa2) & 0xa0) == 0x20 && sskpd && !s3resume) {
Angel Pons66780a02021-03-26 13:33:22 +0100367 mchbar_write32(SSKPD, 0);
Angel Pons7c49cb82020-03-16 23:17:32 +0100368 /* Need reset */
Elyes HAOUASc0567292019-04-28 17:57:47 +0200369 system_reset();
Stefan Reinauer00636b02012-04-04 00:08:51 +0200370 }
Stefan Reinauer00636b02012-04-04 00:08:51 +0200371
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700372 early_pch_init_native();
Patrick Rudolph6aca7e62019-03-26 18:22:36 +0100373 early_init_dmi();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700374 early_thermal_init();
375
Angel Pons7c49cb82020-03-16 23:17:32 +0100376 /* Try to find timings in MRC cache */
Shelley Chenad9cd682020-07-23 16:10:52 -0700377 ctrl_cached = mrc_cache_current_mmap_leak(MRC_TRAINING_DATA,
378 MRC_CACHE_VERSION,
379 &mrc_size);
380 if (mrc_size < sizeof(ctrl))
381 ctrl_cached = NULL;
Angel Ponsa6a64182020-03-21 18:06:03 +0100382
383 /* Before reusing training data, assert that the CPU has not been replaced */
384 if (ctrl_cached && cpuid != ctrl_cached->cpu) {
385
386 /* It is not really worrying on a cold boot, but fatal when resuming from S3 */
387 printk(s3resume ? BIOS_ALERT : BIOS_NOTICE,
388 "CPUID %x differs from stored CPUID %x, CPU was replaced!\n",
389 cpuid, ctrl_cached->cpu);
390
391 /* Invalidate the stored data, it likely does not apply to the current CPU */
392 ctrl_cached = NULL;
393 }
394
395 if (s3resume && !ctrl_cached) {
396 /* S3 resume is impossible, reset to come up cleanly */
397 system_reset();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700398 }
399
Angel Pons7c49cb82020-03-16 23:17:32 +0100400 /* Verify MRC cache for fast boot */
Kyösti Mälkki38cb8222016-11-18 19:25:52 +0200401 if (!s3resume && ctrl_cached) {
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +0200402 /* Load SPD unique information data. */
403 memset(spds, 0, sizeof(spds));
404 mainboard_get_spd(spds, 1);
405
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100406 /* check SPD CRC16 to make sure the DIMMs haven't been replaced */
407 fast_boot = verify_crc16_spds_ddr3(spds, ctrl_cached);
408 if (!fast_boot)
409 printk(BIOS_DEBUG, "Stored timings CRC16 mismatch.\n");
Kyösti Mälkki38cb8222016-11-18 19:25:52 +0200410 } else {
411 fast_boot = s3resume;
412 }
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100413
414 if (fast_boot) {
415 printk(BIOS_DEBUG, "Trying stored timings.\n");
416 memcpy(&ctrl, ctrl_cached, sizeof(ctrl));
417
Patrick Rudolph588ccaa2016-04-20 18:00:27 +0200418 err = try_init_dram_ddr3(&ctrl, fast_boot, s3resume, me_uma_size);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100419 if (err) {
Patrick Rudolph588ccaa2016-04-20 18:00:27 +0200420 if (s3resume) {
421 /* Failed S3 resume, reset to come up cleanly */
Elyes HAOUASc0567292019-04-28 17:57:47 +0200422 system_reset();
Patrick Rudolph588ccaa2016-04-20 18:00:27 +0200423 }
Angel Pons7c49cb82020-03-16 23:17:32 +0100424 /* No need to erase bad MRC cache here, it gets overwritten on a
425 successful boot */
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100426 printk(BIOS_ERR, "Stored timings are invalid !\n");
427 fast_boot = 0;
428 }
429 }
430 if (!fast_boot) {
Patrick Rudolphe74ad212016-11-16 18:06:50 +0100431 /* Reset internal state */
Angel Ponsfc930242020-03-24 11:12:09 +0100432 reinit_ctrl(&ctrl, cpuid);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100433
Patrick Rudolph05d4bf7e2017-10-28 16:36:09 +0200434 printk(BIOS_INFO, "ECC RAM %s.\n", ctrl.ecc_forced ? "required" :
435 ctrl.ecc_supported ? "supported" : "unsupported");
Patrick Rudolph305035c2016-11-11 18:38:50 +0100436
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100437 /* Get DDR3 SPD data */
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +0200438 memset(spds, 0, sizeof(spds));
439 mainboard_get_spd(spds, 0);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100440 dram_find_spds_ddr3(spds, &ctrl);
441
Patrick Rudolph588ccaa2016-04-20 18:00:27 +0200442 err = try_init_dram_ddr3(&ctrl, fast_boot, s3resume, me_uma_size);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100443 }
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +0100444
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +0100445 if (err) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100446 /* Fallback: disable failing channel */
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +0100447 printk(BIOS_ERR, "RAM training failed, trying fallback.\n");
448 printram("Disable failing channel.\n");
449
Patrick Rudolphe74ad212016-11-16 18:06:50 +0100450 /* Reset internal state */
Angel Ponsfc930242020-03-24 11:12:09 +0100451 reinit_ctrl(&ctrl, cpuid);
Patrick Rudolph305035c2016-11-11 18:38:50 +0100452
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +0100453 /* Reset DDR3 frequency */
454 dram_find_spds_ddr3(spds, &ctrl);
455
Angel Pons7c49cb82020-03-16 23:17:32 +0100456 /* Disable failing channel */
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +0100457 disable_channel(&ctrl, GET_ERR_CHANNEL(err));
458
459 err = try_init_dram_ddr3(&ctrl, fast_boot, s3resume, me_uma_size);
460 }
461
Patrick Rudolph31d19592016-03-26 12:22:34 +0100462 if (err)
463 die("raminit failed");
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700464
Angel Pons88521882020-01-05 20:21:20 +0100465 /* FIXME: should be hardware revision-dependent. The register only exists on IVB. */
Angel Pons66780a02021-03-26 13:33:22 +0100466 mchbar_write32(CHANNEL_HASH, 0x00a030ce);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700467
468 set_scrambling_seed(&ctrl);
469
Patrick Rudolphd0581312020-05-01 18:31:48 +0200470 if (!s3resume && ctrl.ecc_enabled)
471 channel_scrub(&ctrl);
472
Angel Pons88521882020-01-05 20:21:20 +0100473 set_normal_operation(&ctrl);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700474
475 final_registers(&ctrl);
476
Patrick Rudolphd0581312020-05-01 18:31:48 +0200477 /* can't do this earlier because it needs to be done in normal operation */
478 if (CONFIG(DEBUG_RAM_SETUP) && !s3resume && ctrl.ecc_enabled) {
479 uint32_t i, tseg = pci_read_config32(HOST_BRIDGE, TSEGMB);
480
481 printk(BIOS_INFO, "RAMINIT: ECC scrub test on first channel up to 0x%x\n",
482 tseg);
483
484 /*
485 * This test helps to debug the ECC scrubbing.
486 * It likely tests every channel/rank, as rank interleave and enhanced
487 * interleave are enabled, but there's no guarantee for it.
488 */
489
490 /* Skip first MB to avoid special case for A-seg and test up to TSEG */
491 for (i = 1; i < tseg >> 20; i++) {
492 for (int j = 0; j < 1 * MiB; j += 4096) {
493 uintptr_t addr = i * MiB + j;
494 if (read32((u32 *)addr) == 0)
495 continue;
496
497 printk(BIOS_ERR, "RAMINIT: ECC scrub: DRAM not cleared at"
498 " addr 0x%lx\n", addr);
499 break;
500 }
501 }
502 printk(BIOS_INFO, "RAMINIT: ECC scrub test done.\n");
503 }
504
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700505 /* Zone config */
506 dram_zones(&ctrl, 0);
507
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700508 intel_early_me_init_done(ME_INIT_STATUS_SUCCESS);
509 intel_early_me_status();
510
Stefan Reinauer00636b02012-04-04 00:08:51 +0200511 report_memory_config();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700512
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100513 timestamp_add_now(TS_INITRAM_END);
Kyösti Mälkkib33c6fb2021-02-17 20:43:04 +0200514
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700515 cbmem_was_inited = !cbmem_recovery(s3resume);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100516 if (!fast_boot)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700517 save_timings(&ctrl);
518 if (s3resume && !cbmem_was_inited) {
519 /* Failed S3 resume, reset to come up cleanly */
Elyes HAOUASc0567292019-04-28 17:57:47 +0200520 system_reset();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700521 }
Patrick Rudolphb97009e2016-02-28 15:24:04 +0100522
Nico Huber9ce59742018-09-13 10:52:44 +0200523 if (!s3resume)
Patrick Rudolph42609d82020-07-27 16:23:36 +0200524 setup_sdram_meminfo(&ctrl);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200525}
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100526
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100527void perform_raminit(int s3resume)
528{
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100529 post_code(0x3a);
Angel Ponsfc930242020-03-24 11:12:09 +0100530 init_dram_ddr3(s3resume, cpu_get_cpuid());
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100531}