blob: 8a8bd8310b5598212ffacf07e363ee2658452b3f [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>
Elyes HAOUASc0567292019-04-28 17:57:47 +02004#include <cf9_reset.h>
Stefan Reinauer00636b02012-04-04 00:08:51 +02005#include <string.h>
Keith Hui1e9601c2023-07-15 12:08:51 -04006#include <cbfs.h>
Subrata Banik53b08c32018-12-10 14:11:35 +05307#include <arch/cpu.h>
Keith Hui1e9601c2023-07-15 12:08:51 -04008#include <device/device.h>
9#include <device/dram/ddr3.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +020010#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020011#include <device/pci_ops.h>
Kyösti Mälkki1cae4542020-01-06 12:31:34 +020012#include <device/smbus_host.h>
Stefan Reinauer00636b02012-04-04 00:08:51 +020013#include <cbmem.h>
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +010014#include <timestamp.h>
Arthur Heymans7539b8c2017-12-24 10:42:57 +010015#include <mrc_cache.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010016#include <southbridge/intel/bd82x6x/me.h>
Patrick Rudolphda9302a2019-03-24 17:01:41 +010017#include <southbridge/intel/bd82x6x/pch.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010018#include <cpu/x86/msr.h>
Elyes HAOUAS51401c32019-05-15 21:09:30 +020019#include <types.h>
Elyes HAOUASbf0970e2019-03-21 11:10:03 +010020
Keith Hui1e9601c2023-07-15 12:08:51 -040021#include "raminit.h"
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010022#include "raminit_common.h"
23#include "sandybridge.h"
Keith Hui1e9601c2023-07-15 12:08:51 -040024#include "chip.h"
Stefan Reinauer00636b02012-04-04 00:08:51 +020025
Angel Pons7c49cb82020-03-16 23:17:32 +010026/* FIXME: no support for 3-channel chipsets */
Stefan Reinauer00636b02012-04-04 00:08:51 +020027
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070028static void wait_txt_clear(void)
29{
Angel Pons7c49cb82020-03-16 23:17:32 +010030 struct cpuid_result cp = cpuid_ext(1, 0);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070031
Angel Pons7c49cb82020-03-16 23:17:32 +010032 /* Check if TXT is supported */
33 if (!(cp.ecx & (1 << 6)))
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070034 return;
Angel Pons7c49cb82020-03-16 23:17:32 +010035
36 /* Some TXT public bit */
Elyes Haouas4b7d4052022-12-03 13:24:03 +010037 if (!(read32p(0xfed30010) & 1))
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070038 return;
Angel Pons7c49cb82020-03-16 23:17:32 +010039
40 /* Wait for TXT clear */
Elyes Haouas4b7d4052022-12-03 13:24:03 +010041 while (!(read8p(0xfed40000) & (1 << 7)))
Angel Pons7c49cb82020-03-16 23:17:32 +010042 ;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070043}
44
Angel Pons7c49cb82020-03-16 23:17:32 +010045/* Disable a channel in ramctr_timing */
46static void disable_channel(ramctr_timing *ctrl, int channel)
47{
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +010048 ctrl->rankmap[channel] = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +010049
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +010050 memset(&ctrl->rank_mirror[channel][0], 0, sizeof(ctrl->rank_mirror[0]));
Angel Pons7c49cb82020-03-16 23:17:32 +010051
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +010052 ctrl->channel_size_mb[channel] = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +010053 ctrl->cmd_stretch[channel] = 0;
54 ctrl->mad_dimm[channel] = 0;
55 memset(&ctrl->timings[channel][0], 0, sizeof(ctrl->timings[0]));
Patrick Rudolph74163d62016-11-17 20:02:43 +010056 memset(&ctrl->info.dimm[channel][0], 0, sizeof(ctrl->info.dimm[0]));
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +010057}
58
Angel Pons6724ba42021-01-31 15:06:59 +010059static uint8_t nb_get_ecc_type(const uint32_t capid0_a)
Patrick Rudolph42609d82020-07-27 16:23:36 +020060{
Angel Pons6724ba42021-01-31 15:06:59 +010061 return capid0_a & CAPID_ECCDIS ? MEMORY_ARRAY_ECC_NONE : MEMORY_ARRAY_ECC_SINGLE_BIT;
Patrick Rudolph42609d82020-07-27 16:23:36 +020062}
63
64static uint16_t nb_slots_per_channel(const uint32_t capid0_a)
65{
66 return !(capid0_a & CAPID_DDPCD) + 1;
67}
68
69static uint16_t nb_number_of_channels(const uint32_t capid0_a)
70{
71 return !(capid0_a & CAPID_PDCD) + 1;
72}
73
74static uint32_t nb_max_chan_capacity_mib(const uint32_t capid0_a)
75{
76 uint32_t ddrsz;
77
78 /* Values from documentation, which assume two DIMMs per channel */
79 switch (CAPID_DDRSZ(capid0_a)) {
80 case 1:
81 ddrsz = 8192;
82 break;
83 case 2:
84 ddrsz = 2048;
85 break;
86 case 3:
87 ddrsz = 512;
88 break;
89 default:
90 ddrsz = 16384;
91 break;
92 }
93
94 /* Account for the maximum number of DIMMs per channel */
95 return (ddrsz / 2) * nb_slots_per_channel(capid0_a);
96}
97
98/* Fill cbmem with information for SMBIOS type 16 and type 17 */
99static void setup_sdram_meminfo(ramctr_timing *ctrl)
Patrick Rudolphb97009e2016-02-28 15:24:04 +0100100{
Patrick Rudolphb97009e2016-02-28 15:24:04 +0100101 int channel, slot;
Patrick Rudolph24efe732018-08-19 11:06:06 +0200102 const u16 ddr_freq = (1000 << 8) / ctrl->tCK;
Patrick Rudolphb97009e2016-02-28 15:24:04 +0100103
Elyes HAOUAS12df9502016-08-23 21:29:48 +0200104 FOR_ALL_CHANNELS for (slot = 0; slot < NUM_SLOTS; slot++) {
Patrick Rudolph24efe732018-08-19 11:06:06 +0200105 enum cb_err ret = spd_add_smbios17(channel, slot, ddr_freq,
106 &ctrl->info.dimm[channel][slot]);
107 if (ret != CB_SUCCESS)
108 printk(BIOS_ERR, "RAMINIT: Failed to add SMBIOS17\n");
Patrick Rudolphb97009e2016-02-28 15:24:04 +0100109 }
Patrick Rudolph42609d82020-07-27 16:23:36 +0200110
111 /* The 'spd_add_smbios17' function allocates this CBMEM area */
112 struct memory_info *m = cbmem_find(CBMEM_ID_MEMINFO);
Elyes Haouas5e6b0f02022-09-13 09:55:49 +0200113 if (!m)
Patrick Rudolph42609d82020-07-27 16:23:36 +0200114 return;
115
116 const uint32_t capid0_a = pci_read_config32(HOST_BRIDGE, CAPID0_A);
117
118 const uint16_t channels = nb_number_of_channels(capid0_a);
119
Angel Pons6724ba42021-01-31 15:06:59 +0100120 m->ecc_type = nb_get_ecc_type(capid0_a);
Patrick Rudolph42609d82020-07-27 16:23:36 +0200121 m->max_capacity_mib = channels * nb_max_chan_capacity_mib(capid0_a);
122 m->number_of_devices = channels * nb_slots_per_channel(capid0_a);
Patrick Rudolphb97009e2016-02-28 15:24:04 +0100123}
124
Angel Pons7c49cb82020-03-16 23:17:32 +0100125/* Return CRC16 match for all SPDs */
Elyes Haouas78ba7a72024-05-06 05:11:28 +0200126static int verify_crc16_spds_ddr3(spd_ddr3_raw_data *spd, ramctr_timing *ctrl)
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100127{
128 int channel, slot, spd_slot;
129 int match = 1;
130
131 FOR_ALL_CHANNELS {
132 for (slot = 0; slot < NUM_SLOTS; slot++) {
133 spd_slot = 2 * channel + slot;
134 match &= ctrl->spd_crc[channel][slot] ==
Elyes Haouas78ba7a72024-05-06 05:11:28 +0200135 spd_ddr3_calc_unique_crc(spd[spd_slot], sizeof(spd_ddr3_raw_data));
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100136 }
137 }
138 return match;
139}
140
Elyes Haouas78ba7a72024-05-06 05:11:28 +0200141static void read_spd(spd_ddr3_raw_data *spd, u8 addr, bool id_only)
Stefan Reinauer00636b02012-04-04 00:08:51 +0200142{
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700143 int j;
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +0200144 if (id_only) {
Elyes Haouas8bcd8212024-05-06 11:48:41 +0200145 for (j = SPD_DDR3_MOD_ID1; j < 128; 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 } else {
Keith Hui1e9601c2023-07-15 12:08:51 -0400148 for (j = 0; j < SPD_SIZE_MAX_DDR3; j++)
Kyösti Mälkki1a1b04e2020-01-07 22:34:33 +0200149 (*spd)[j] = smbus_read_byte(addr, j);
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +0200150 }
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700151}
152
Elyes Haouas78ba7a72024-05-06 05:11:28 +0200153static void mainboard_get_spd(spd_ddr3_raw_data *spd, bool id_only)
Keith Hui1e9601c2023-07-15 12:08:51 -0400154{
155 const struct northbridge_intel_sandybridge_config *cfg = config_of_soc();
156 unsigned int i;
157
158 if (CONFIG(HAVE_SPD_IN_CBFS)) {
159 struct spd_info spdi = {0};
160
161 mb_get_spd_map(&spdi);
162
163 size_t spd_file_len;
164 uint8_t *spd_file = cbfs_map("spd.bin", &spd_file_len);
165
166 printk(BIOS_DEBUG, "SPD index %d\n", spdi.spd_index);
167
168 /* SPD file sanity check */
169 if (!spd_file)
170 die("SPD data %s!", "not found");
171
172 if (spd_file_len < ((spdi.spd_index + 1) * SPD_SIZE_MAX_DDR3))
173 die("SPD data %s!", "incomplete");
174
175 /*
176 * Copy SPD data specified by spd_info.spd_index to all slots marked as
177 * SPD_MEMORY_DOWN.
178 *
179 * Read SPD data from slots with a real SMBus address.
180 */
181 for (i = 0; i < ARRAY_SIZE(spdi.addresses); i++) {
182 if (spdi.addresses[i] == SPD_MEMORY_DOWN)
183 memcpy(&spd[i], spd_file + (spdi.spd_index * SPD_SIZE_MAX_DDR3), SPD_SIZE_MAX_DDR3);
184 else if (spdi.addresses[i] != 0)
185 read_spd(&spd[i], spdi.addresses[i], id_only);
186 }
187 } else {
188 for (i = 0; i < ARRAY_SIZE(cfg->spd_addresses); i++) {
189 if (cfg->spd_addresses[i] != 0)
190 read_spd(&spd[i], cfg->spd_addresses[i], id_only);
191 }
192 } /* CONFIG(HAVE_SPD_IN_CBFS) */
193}
194
Elyes Haouas78ba7a72024-05-06 05:11:28 +0200195static void dram_find_spds_ddr3(spd_ddr3_raw_data *spd, ramctr_timing *ctrl)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700196{
Angel Pons7c49cb82020-03-16 23:17:32 +0100197 int dimms = 0, ch_dimms;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700198 int channel, slot, spd_slot;
Patrick Rudolphdd662872017-10-28 18:20:11 +0200199 bool can_use_ecc = ctrl->ecc_supported;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700200
Elyes Haouas9d450b22023-09-10 10:30:29 +0200201 memset(ctrl->rankmap, 0, sizeof(ctrl->rankmap));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700202
203 ctrl->extended_temperature_range = 1;
204 ctrl->auto_self_refresh = 1;
205
206 FOR_ALL_CHANNELS {
207 ctrl->channel_size_mb[channel] = 0;
208
Angel Pons7c49cb82020-03-16 23:17:32 +0100209 ch_dimms = 0;
210 /* Count dimms on channel */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700211 for (slot = 0; slot < NUM_SLOTS; slot++) {
212 spd_slot = 2 * channel + slot;
Patrick Rudolph5a061852017-09-22 15:19:26 +0200213
Angel Pons035096c2020-09-17 22:31:19 +0200214 if (spd[spd_slot][SPD_MEMORY_TYPE] == SPD_MEMORY_TYPE_SDRAM_DDR3)
Angel Pons7c49cb82020-03-16 23:17:32 +0100215 ch_dimms++;
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100216 }
217
218 for (slot = 0; slot < NUM_SLOTS; slot++) {
219 spd_slot = 2 * channel + slot;
Angel Pons7c49cb82020-03-16 23:17:32 +0100220 printk(BIOS_DEBUG, "SPD probe channel%d, slot%d\n", channel, slot);
Patrick Rudolph5a061852017-09-22 15:19:26 +0200221
Angel Ponsafb3d7e2021-03-28 13:43:13 +0200222 struct dimm_attr_ddr3_st *const dimm = &ctrl->info.dimm[channel][slot];
Angel Pons323c0ae2020-12-12 16:57:37 +0100223
Angel Pons7c49cb82020-03-16 23:17:32 +0100224 /* Search for XMP profile */
Angel Pons323c0ae2020-12-12 16:57:37 +0100225 spd_xmp_decode_ddr3(dimm, spd[spd_slot],
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100226 DDR3_XMP_PROFILE_1);
227
Angel Pons323c0ae2020-12-12 16:57:37 +0100228 if (dimm->dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3) {
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100229 printram("No valid XMP profile found.\n");
Angel Pons323c0ae2020-12-12 16:57:37 +0100230 spd_decode_ddr3(dimm, spd[spd_slot]);
Angel Pons7c49cb82020-03-16 23:17:32 +0100231
Angel Pons323c0ae2020-12-12 16:57:37 +0100232 } else if (ch_dimms > dimm->dimms_per_channel) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100233 printram(
234 "XMP profile supports %u DIMMs, but %u DIMMs are installed.\n",
Angel Pons323c0ae2020-12-12 16:57:37 +0100235 dimm->dimms_per_channel, ch_dimms);
Angel Pons7c49cb82020-03-16 23:17:32 +0100236
Julius Wernercd49cce2019-03-05 16:53:33 -0800237 if (CONFIG(NATIVE_RAMINIT_IGNORE_XMP_MAX_DIMMS))
Angel Pons7c49cb82020-03-16 23:17:32 +0100238 printk(BIOS_WARNING,
239 "XMP maximum DIMMs will be ignored.\n");
Vagiz Trakhanov771be482017-10-02 10:02:35 +0000240 else
Angel Pons323c0ae2020-12-12 16:57:37 +0100241 spd_decode_ddr3(dimm, spd[spd_slot]);
Angel Pons7c49cb82020-03-16 23:17:32 +0100242
Angel Pons323c0ae2020-12-12 16:57:37 +0100243 } else if (dimm->voltage != 1500) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100244 /* TODO: Support DDR3 voltages other than 1500mV */
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100245 printram("XMP profile's requested %u mV is unsupported.\n",
Angel Pons323c0ae2020-12-12 16:57:37 +0100246 dimm->voltage);
Angel Pons3170e9c2020-12-12 16:22:18 +0100247
248 if (CONFIG(NATIVE_RAMINIT_IGNORE_XMP_REQUESTED_VOLTAGE))
249 printk(BIOS_WARNING,
250 "XMP requested voltage will be ignored.\n");
251 else
252 spd_decode_ddr3(dimm, spd[spd_slot]);
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100253 }
254
Angel Pons7c49cb82020-03-16 23:17:32 +0100255 /* Fill in CRC16 for MRC cache */
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100256 ctrl->spd_crc[channel][slot] =
Elyes Haouas78ba7a72024-05-06 05:11:28 +0200257 spd_ddr3_calc_unique_crc(spd[spd_slot], sizeof(spd_ddr3_raw_data));
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100258
Angel Pons323c0ae2020-12-12 16:57:37 +0100259 if (dimm->dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100260 /* Mark DIMM as invalid */
Angel Pons323c0ae2020-12-12 16:57:37 +0100261 dimm->ranks = 0;
262 dimm->size_mb = 0;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700263 continue;
264 }
265
Angel Pons323c0ae2020-12-12 16:57:37 +0100266 dram_print_spd_ddr3(dimm);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700267 dimms++;
268 ctrl->rank_mirror[channel][slot * 2] = 0;
Angel Pons323c0ae2020-12-12 16:57:37 +0100269 ctrl->rank_mirror[channel][slot * 2 + 1] = dimm->flags.pins_mirrored;
Angel Pons7c49cb82020-03-16 23:17:32 +0100270
Angel Pons323c0ae2020-12-12 16:57:37 +0100271 ctrl->channel_size_mb[channel] += dimm->size_mb;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700272
Angel Pons323c0ae2020-12-12 16:57:37 +0100273 if (!dimm->flags.is_ecc)
Patrick Rudolphdd662872017-10-28 18:20:11 +0200274 can_use_ecc = false;
275
Angel Pons323c0ae2020-12-12 16:57:37 +0100276 ctrl->auto_self_refresh &= dimm->flags.asr;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700277
Angel Pons323c0ae2020-12-12 16:57:37 +0100278 ctrl->extended_temperature_range &= dimm->flags.ext_temp_refresh;
Angel Pons7c49cb82020-03-16 23:17:32 +0100279
Angel Pons323c0ae2020-12-12 16:57:37 +0100280 ctrl->rankmap[channel] |= ((1 << dimm->ranks) - 1) << (2 * slot);
Angel Pons7c49cb82020-03-16 23:17:32 +0100281
282 printk(BIOS_DEBUG, "channel[%d] rankmap = 0x%x\n", channel,
283 ctrl->rankmap[channel]);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700284 }
Angel Pons7c49cb82020-03-16 23:17:32 +0100285
Angel Ponsd4d3ba02020-12-12 17:45:14 +0100286 const u8 rc_0 = ctrl->info.dimm[channel][0].reference_card;
287 const u8 rc_1 = ctrl->info.dimm[channel][1].reference_card;
288
289 if (ch_dimms == NUM_SLOTS && rc_0 < 6 && rc_1 < 6) {
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700290 const int ref_card_offset_table[6][6] = {
Angel Pons7c49cb82020-03-16 23:17:32 +0100291 { 0, 0, 0, 0, 2, 2 },
292 { 0, 0, 0, 0, 2, 2 },
293 { 0, 0, 0, 0, 2, 2 },
294 { 0, 0, 0, 0, 1, 1 },
295 { 2, 2, 2, 1, 0, 0 },
296 { 2, 2, 2, 1, 0, 0 },
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700297 };
Angel Ponsd4d3ba02020-12-12 17:45:14 +0100298 ctrl->ref_card_offset[channel] = ref_card_offset_table[rc_0][rc_1];
Angel Pons7c49cb82020-03-16 23:17:32 +0100299 } else {
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700300 ctrl->ref_card_offset[channel] = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100301 }
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700302 }
303
Patrick Rudolphdd662872017-10-28 18:20:11 +0200304 if (ctrl->ecc_forced || CONFIG(RAMINIT_ENABLE_ECC))
305 ctrl->ecc_enabled = can_use_ecc;
306 if (ctrl->ecc_forced && !ctrl->ecc_enabled)
307 die("ECC mode forced but non-ECC DIMM installed!");
308 printk(BIOS_DEBUG, "ECC is %s\n", ctrl->ecc_enabled ? "enabled" : "disabled");
309
310 ctrl->lanes = ctrl->ecc_enabled ? 9 : 8;
311
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700312 if (!dimms)
313 die("No DIMMs were found");
314}
315
Patrick Rudolphbb9c90a2016-05-29 17:05:06 +0200316static void save_timings(ramctr_timing *ctrl)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700317{
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700318 /* Save the MRC S3 restore data to cbmem */
Angel Pons7c49cb82020-03-16 23:17:32 +0100319 mrc_cache_stash_data(MRC_TRAINING_DATA, MRC_CACHE_VERSION, ctrl, sizeof(*ctrl));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700320}
321
Angel Ponsfc930242020-03-24 11:12:09 +0100322static void reinit_ctrl(ramctr_timing *ctrl, const u32 cpuid)
Patrick Rudolph05d4bf7e2017-10-28 16:36:09 +0200323{
324 /* Reset internal state */
325 memset(ctrl, 0, sizeof(*ctrl));
Patrick Rudolph05d4bf7e2017-10-28 16:36:09 +0200326
327 /* Get architecture */
328 ctrl->cpu = cpuid;
329
330 /* Get ECC support and mode */
331 ctrl->ecc_forced = get_host_ecc_forced();
332 ctrl->ecc_supported = ctrl->ecc_forced || get_host_ecc_cap();
333 printk(BIOS_DEBUG, "ECC supported: %s ECC forced: %s\n",
334 ctrl->ecc_supported ? "yes" : "no",
335 ctrl->ecc_forced ? "yes" : "no");
336}
337
Angel Ponsfc930242020-03-24 11:12:09 +0100338static void init_dram_ddr3(int s3resume, const u32 cpuid)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700339{
Angel Pons7c49cb82020-03-16 23:17:32 +0100340 int me_uma_size, cbmem_was_inited, fast_boot, err;
Patrick Rudolph735ecce2016-03-26 10:42:27 +0100341 ramctr_timing ctrl;
Elyes Haouas78ba7a72024-05-06 05:11:28 +0200342 spd_ddr3_raw_data spds[4];
Shelley Chenad9cd682020-07-23 16:10:52 -0700343 size_t mrc_size;
Angel Ponsa6a64182020-03-21 18:06:03 +0100344 ramctr_timing *ctrl_cached = NULL;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700345
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100346 timestamp_add_now(TS_INITRAM_START);
Kyösti Mälkkib33c6fb2021-02-17 20:43:04 +0200347
Angel Pons66780a02021-03-26 13:33:22 +0100348 mchbar_setbits32(SAPMCTL, 1 << 0);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200349
350 /* Wait for ME to be ready */
351 intel_early_me_init();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700352 me_uma_size = intel_early_me_uma_size();
Stefan Reinauer00636b02012-04-04 00:08:51 +0200353
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700354 printk(BIOS_DEBUG, "Starting native Platform init\n");
Stefan Reinauer00636b02012-04-04 00:08:51 +0200355
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700356 wait_txt_clear();
Stefan Reinauer00636b02012-04-04 00:08:51 +0200357
Angel Pons5db1b152020-12-13 16:37:53 +0100358 wrmsr(0x2e6, (msr_t) { .lo = 0, .hi = 0 });
Stefan Reinauer00636b02012-04-04 00:08:51 +0200359
Angel Pons66780a02021-03-26 13:33:22 +0100360 const u32 sskpd = mchbar_read32(SSKPD); // !!! = 0x00000000
Angel Pons7c49cb82020-03-16 23:17:32 +0100361 if ((pci_read_config16(SOUTHBRIDGE, 0xa2) & 0xa0) == 0x20 && sskpd && !s3resume) {
Angel Pons66780a02021-03-26 13:33:22 +0100362 mchbar_write32(SSKPD, 0);
Angel Pons7c49cb82020-03-16 23:17:32 +0100363 /* Need reset */
Elyes HAOUASc0567292019-04-28 17:57:47 +0200364 system_reset();
Stefan Reinauer00636b02012-04-04 00:08:51 +0200365 }
Stefan Reinauer00636b02012-04-04 00:08:51 +0200366
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700367 early_pch_init_native();
Patrick Rudolph6aca7e62019-03-26 18:22:36 +0100368 early_init_dmi();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700369 early_thermal_init();
370
Angel Pons7c49cb82020-03-16 23:17:32 +0100371 /* Try to find timings in MRC cache */
Shelley Chenad9cd682020-07-23 16:10:52 -0700372 ctrl_cached = mrc_cache_current_mmap_leak(MRC_TRAINING_DATA,
373 MRC_CACHE_VERSION,
374 &mrc_size);
375 if (mrc_size < sizeof(ctrl))
376 ctrl_cached = NULL;
Angel Ponsa6a64182020-03-21 18:06:03 +0100377
378 /* Before reusing training data, assert that the CPU has not been replaced */
379 if (ctrl_cached && cpuid != ctrl_cached->cpu) {
Angel Ponsa6a64182020-03-21 18:06:03 +0100380 /* It is not really worrying on a cold boot, but fatal when resuming from S3 */
381 printk(s3resume ? BIOS_ALERT : BIOS_NOTICE,
382 "CPUID %x differs from stored CPUID %x, CPU was replaced!\n",
383 cpuid, ctrl_cached->cpu);
384
385 /* Invalidate the stored data, it likely does not apply to the current CPU */
386 ctrl_cached = NULL;
387 }
388
389 if (s3resume && !ctrl_cached) {
390 /* S3 resume is impossible, reset to come up cleanly */
391 system_reset();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700392 }
393
Angel Pons7c49cb82020-03-16 23:17:32 +0100394 /* Verify MRC cache for fast boot */
Kyösti Mälkki38cb8222016-11-18 19:25:52 +0200395 if (!s3resume && ctrl_cached) {
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +0200396 /* Load SPD unique information data. */
397 memset(spds, 0, sizeof(spds));
398 mainboard_get_spd(spds, 1);
399
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100400 /* check SPD CRC16 to make sure the DIMMs haven't been replaced */
401 fast_boot = verify_crc16_spds_ddr3(spds, ctrl_cached);
402 if (!fast_boot)
403 printk(BIOS_DEBUG, "Stored timings CRC16 mismatch.\n");
Kyösti Mälkki38cb8222016-11-18 19:25:52 +0200404 } else {
405 fast_boot = s3resume;
406 }
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100407
408 if (fast_boot) {
409 printk(BIOS_DEBUG, "Trying stored timings.\n");
410 memcpy(&ctrl, ctrl_cached, sizeof(ctrl));
411
Patrick Rudolph588ccaa2016-04-20 18:00:27 +0200412 err = try_init_dram_ddr3(&ctrl, fast_boot, s3resume, me_uma_size);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100413 if (err) {
Patrick Rudolph588ccaa2016-04-20 18:00:27 +0200414 if (s3resume) {
415 /* Failed S3 resume, reset to come up cleanly */
Elyes HAOUASc0567292019-04-28 17:57:47 +0200416 system_reset();
Patrick Rudolph588ccaa2016-04-20 18:00:27 +0200417 }
Angel Pons7c49cb82020-03-16 23:17:32 +0100418 /* No need to erase bad MRC cache here, it gets overwritten on a
419 successful boot */
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100420 printk(BIOS_ERR, "Stored timings are invalid !\n");
421 fast_boot = 0;
422 }
423 }
424 if (!fast_boot) {
Patrick Rudolphe74ad212016-11-16 18:06:50 +0100425 /* Reset internal state */
Angel Ponsfc930242020-03-24 11:12:09 +0100426 reinit_ctrl(&ctrl, cpuid);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100427
Patrick Rudolph05d4bf7e2017-10-28 16:36:09 +0200428 printk(BIOS_INFO, "ECC RAM %s.\n", ctrl.ecc_forced ? "required" :
429 ctrl.ecc_supported ? "supported" : "unsupported");
Patrick Rudolph305035c2016-11-11 18:38:50 +0100430
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100431 /* Get DDR3 SPD data */
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +0200432 memset(spds, 0, sizeof(spds));
433 mainboard_get_spd(spds, 0);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100434 dram_find_spds_ddr3(spds, &ctrl);
435
Patrick Rudolph588ccaa2016-04-20 18:00:27 +0200436 err = try_init_dram_ddr3(&ctrl, fast_boot, s3resume, me_uma_size);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100437 }
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +0100438
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +0100439 if (err) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100440 /* Fallback: disable failing channel */
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +0100441 printk(BIOS_ERR, "RAM training failed, trying fallback.\n");
442 printram("Disable failing channel.\n");
443
Patrick Rudolphe74ad212016-11-16 18:06:50 +0100444 /* Reset internal state */
Angel Ponsfc930242020-03-24 11:12:09 +0100445 reinit_ctrl(&ctrl, cpuid);
Patrick Rudolph305035c2016-11-11 18:38:50 +0100446
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +0100447 /* Reset DDR3 frequency */
448 dram_find_spds_ddr3(spds, &ctrl);
449
Angel Pons7c49cb82020-03-16 23:17:32 +0100450 /* Disable failing channel */
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +0100451 disable_channel(&ctrl, GET_ERR_CHANNEL(err));
452
453 err = try_init_dram_ddr3(&ctrl, fast_boot, s3resume, me_uma_size);
454 }
455
Patrick Rudolph31d19592016-03-26 12:22:34 +0100456 if (err)
457 die("raminit failed");
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700458
Angel Pons88521882020-01-05 20:21:20 +0100459 /* FIXME: should be hardware revision-dependent. The register only exists on IVB. */
Angel Pons66780a02021-03-26 13:33:22 +0100460 mchbar_write32(CHANNEL_HASH, 0x00a030ce);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700461
462 set_scrambling_seed(&ctrl);
463
Patrick Rudolphd0581312020-05-01 18:31:48 +0200464 if (!s3resume && ctrl.ecc_enabled)
465 channel_scrub(&ctrl);
466
Angel Pons88521882020-01-05 20:21:20 +0100467 set_normal_operation(&ctrl);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700468
469 final_registers(&ctrl);
470
Patrick Rudolphd0581312020-05-01 18:31:48 +0200471 /* can't do this earlier because it needs to be done in normal operation */
472 if (CONFIG(DEBUG_RAM_SETUP) && !s3resume && ctrl.ecc_enabled) {
473 uint32_t i, tseg = pci_read_config32(HOST_BRIDGE, TSEGMB);
474
475 printk(BIOS_INFO, "RAMINIT: ECC scrub test on first channel up to 0x%x\n",
476 tseg);
477
478 /*
479 * This test helps to debug the ECC scrubbing.
480 * It likely tests every channel/rank, as rank interleave and enhanced
481 * interleave are enabled, but there's no guarantee for it.
482 */
483
484 /* Skip first MB to avoid special case for A-seg and test up to TSEG */
485 for (i = 1; i < tseg >> 20; i++) {
486 for (int j = 0; j < 1 * MiB; j += 4096) {
487 uintptr_t addr = i * MiB + j;
488 if (read32((u32 *)addr) == 0)
489 continue;
490
491 printk(BIOS_ERR, "RAMINIT: ECC scrub: DRAM not cleared at"
492 " addr 0x%lx\n", addr);
493 break;
494 }
495 }
496 printk(BIOS_INFO, "RAMINIT: ECC scrub test done.\n");
497 }
498
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700499 /* Zone config */
500 dram_zones(&ctrl, 0);
501
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700502 intel_early_me_init_done(ME_INIT_STATUS_SUCCESS);
503 intel_early_me_status();
504
Stefan Reinauer00636b02012-04-04 00:08:51 +0200505 report_memory_config();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700506
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100507 timestamp_add_now(TS_INITRAM_END);
Kyösti Mälkkib33c6fb2021-02-17 20:43:04 +0200508
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700509 cbmem_was_inited = !cbmem_recovery(s3resume);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100510 if (!fast_boot)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700511 save_timings(&ctrl);
512 if (s3resume && !cbmem_was_inited) {
513 /* Failed S3 resume, reset to come up cleanly */
Elyes HAOUASc0567292019-04-28 17:57:47 +0200514 system_reset();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700515 }
Patrick Rudolphb97009e2016-02-28 15:24:04 +0100516
Nico Huber9ce59742018-09-13 10:52:44 +0200517 if (!s3resume)
Patrick Rudolph42609d82020-07-27 16:23:36 +0200518 setup_sdram_meminfo(&ctrl);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200519}
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100520
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100521void perform_raminit(int s3resume)
522{
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100523 post_code(0x3a);
Angel Ponsfc930242020-03-24 11:12:09 +0100524 init_dram_ddr3(s3resume, cpu_get_cpuid());
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100525}