Debian Codegraph @

src/northbridge/intel/sandybridge/raminit.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <console/console.h>
4 #include <cf9_reset.h>
5 #include <string.h>
6 #include <cbfs.h>
7 #include <arch/cpu.h>
8 #include <device/device.h>
9 #include <device/dram/ddr3.h>
10 #include <device/mmio.h>
11 #include <device/pci_ops.h>
12 #include <device/smbus_host.h>
13 #include <cbmem.h>
14 #include <timestamp.h>
15 #include <mrc_cache.h>
16 #include <southbridge/intel/bd82x6x/me.h>
17 #include <southbridge/intel/bd82x6x/pch.h>
18 #include <static.h>
19 #include <cpu/x86/msr.h>
20 #include <types.h>
21
22 #include "raminit.h"
23 #include "raminit_common.h"
24 #include "sandybridge.h"
25 #include "chip.h"
26
27 /* FIXME: no support for 3-channel chipsets */
28
29 static void wait_txt_clear(void)
30 {
31 struct cpuid_result cp = cpuid_ext(1, 0);
32
33 /* Check if TXT is supported */
34 if (!(cp.ecx & (1 << 6)))
35 return;
36
37 /* Some TXT public bit */
38 if (!(read32p(0xfed30010) & 1))
39 return;
40
41 /* Wait for TXT clear */
42 while (!(read8p(0xfed40000) & (1 << 7)))
43 ;
44 }
45
46 /* Disable a channel in ramctr_timing */
47 static void disable_channel(ramctr_timing *ctrl, int channel)
48 {
49 ctrl->rankmap[channel] = 0;
50
51 memset(&ctrl->rank_mirror[channel][0], 0, sizeof(ctrl->rank_mirror[0]));
52
53 ctrl->channel_size_mb[channel] = 0;
54 ctrl->cmd_stretch[channel] = 0;
55 ctrl->mad_dimm[channel] = 0;
56 memset(&ctrl->timings[channel][0], 0, sizeof(ctrl->timings[0]));
57 memset(&ctrl->info.dimm[channel][0], 0, sizeof(ctrl->info.dimm[0]));
58 }
59
60 static uint8_t nb_get_ecc_type(const uint32_t capid0_a)
61 {
62 return capid0_a & CAPID_ECCDIS ? MEMORY_ARRAY_ECC_NONE : MEMORY_ARRAY_ECC_SINGLE_BIT;
63 }
64
65 static uint16_t nb_slots_per_channel(const uint32_t capid0_a)
66 {
67 return !(capid0_a & CAPID_DDPCD) + 1;
68 }
69
70 static uint16_t nb_number_of_channels(const uint32_t capid0_a)
71 {
72 return !(capid0_a & CAPID_PDCD) + 1;
73 }
74
75 static uint32_t nb_max_chan_capacity_mib(const uint32_t capid0_a)
76 {
77 uint32_t ddrsz;
78
79 /* Values from documentation, which assume two DIMMs per channel */
80 switch (CAPID_DDRSZ(capid0_a)) {
81 case 1:
82 ddrsz = 8192;
83 break;
84 case 2:
85 ddrsz = 2048;
86 break;
87 case 3:
88 ddrsz = 512;
89 break;
90 default:
91 ddrsz = 16384;
92 break;
93 }
94
95 /* Account for the maximum number of DIMMs per channel */
96 return (ddrsz / 2) * nb_slots_per_channel(capid0_a);
97 }
98
99 /* Fill cbmem with information for SMBIOS type 16 and type 17 */
100 static void setup_sdram_meminfo(ramctr_timing *ctrl)
101 {
102 int channel, slot;
103 const u16 ddr_freq = (1000 << 8) / ctrl->tCK;
104
105 FOR_ALL_CHANNELS for (slot = 0; slot < NUM_SLOTS; slot++) {
106 enum cb_err ret = spd_add_smbios17(channel, slot, ddr_freq,
107 &ctrl->info.dimm[channel][slot]);
108 if (ret != CB_SUCCESS)
109 printk(BIOS_ERR, "RAMINIT: Failed to add SMBIOS17\n");
110 }
111
112 /* The 'spd_add_smbios17' function allocates this CBMEM area */
113 struct memory_info *m = cbmem_find(CBMEM_ID_MEMINFO);
114 if (!m)
115 return;
116
117 const uint32_t capid0_a = pci_read_config32(HOST_BRIDGE, CAPID0_A);
118
119 const uint16_t channels = nb_number_of_channels(capid0_a);
120
121 m->ecc_type = nb_get_ecc_type(capid0_a);
122 m->max_capacity_mib = channels * nb_max_chan_capacity_mib(capid0_a);
123 m->number_of_devices = channels * nb_slots_per_channel(capid0_a);
124 }
125
126 /* Return CRC16 match for all SPDs */
127 static int verify_crc16_spds_ddr3(spd_ddr3_raw_data *spd, ramctr_timing *ctrl)
128 {
129 int channel, slot, spd_slot;
130 int match = 1;
131
132 FOR_ALL_CHANNELS {
133 for (slot = 0; slot < NUM_SLOTS; slot++) {
134 spd_slot = 2 * channel + slot;
135 match &= ctrl->spd_crc[channel][slot] ==
136 spd_ddr3_calc_unique_crc(spd[spd_slot], sizeof(spd_ddr3_raw_data));
137 }
138 }
139 return match;
140 }
141
142 static void read_spd(spd_ddr3_raw_data *spd, u8 addr, bool id_only)
143 {
144 int j;
145 if (id_only) {
146 u8 *dest = &(*spd)[SPD_DDR3_MOD_ID1];
147 if (i2c_eeprom_read(addr, SPD_DDR3_MOD_ID1, 128 - SPD_DDR3_MOD_ID1, dest) < 0) {
148 for (j = SPD_DDR3_MOD_ID1; j < 128; j++)
149 (*spd)[j] = smbus_read_byte(addr, j);
150 }
151 } else {
152 if (i2c_eeprom_read(addr, 0, SPD_SIZE_MAX_DDR3, *spd) < 0) {
153 for (j = 0; j < SPD_SIZE_MAX_DDR3; j++)
154 (*spd)[j] = smbus_read_byte(addr, j);
155 }
156 }
157 }
158
159 static void mainboard_get_spd(spd_ddr3_raw_data *spd, bool id_only)
160 {
161 const struct northbridge_intel_sandybridge_config *cfg = config_of_soc();
162 unsigned int i;
163
164 if (CONFIG(HAVE_SPD_IN_CBFS)) {
165 struct spd_info spdi = {0};
166
167 mb_get_spd_map(&spdi);
168
169 size_t spd_file_len;
170 uint8_t *spd_file = cbfs_map("spd.bin", &spd_file_len);
171
172 printk(BIOS_DEBUG, "SPD index %d\n", spdi.spd_index);
173
174 /* SPD file sanity check */
175 if (!spd_file)
176 die("SPD data %s!", "not found");
177
178 if (spd_file_len < ((spdi.spd_index + 1) * SPD_SIZE_MAX_DDR3))
179 die("SPD data %s!", "incomplete");
180
181 /*
182 * Copy SPD data specified by spd_info.spd_index to all slots marked as
183 * SPD_MEMORY_DOWN.
184 *
185 * Read SPD data from slots with a real SMBus address.
186 */
187 for (i = 0; i < ARRAY_SIZE(spdi.addresses); i++) {
188 if (spdi.addresses[i] == SPD_MEMORY_DOWN)
189 memcpy(&spd[i], spd_file + (spdi.spd_index * SPD_SIZE_MAX_DDR3), SPD_SIZE_MAX_DDR3);
190 else if (spdi.addresses[i] != 0)
191 read_spd(&spd[i], spdi.addresses[i], id_only);
192 }
193 } else {
194 for (i = 0; i < ARRAY_SIZE(cfg->spd_addresses); i++) {
195 if (cfg->spd_addresses[i] != 0)
196 read_spd(&spd[i], cfg->spd_addresses[i], id_only);
197 }
198 } /* CONFIG(HAVE_SPD_IN_CBFS) */
199 }
200
201 static void dram_find_spds_ddr3(spd_ddr3_raw_data *spd, ramctr_timing *ctrl)
202 {
203 int dimms = 0, ch_dimms;
204 int channel, slot, spd_slot;
205 bool can_use_ecc = ctrl->ecc_supported;
206
207 memset(ctrl->rankmap, 0, sizeof(ctrl->rankmap));
208
209 ctrl->extended_temperature_range = 1;
210 ctrl->auto_self_refresh = 1;
211
212 FOR_ALL_CHANNELS {
213 ctrl->channel_size_mb[channel] = 0;
214
215 ch_dimms = 0;
216 /* Count dimms on channel */
217 for (slot = 0; slot < NUM_SLOTS; slot++) {
218 spd_slot = 2 * channel + slot;
219
220 if (spd[spd_slot][SPD_MEMORY_TYPE] == SPD_MEMORY_TYPE_SDRAM_DDR3)
221 ch_dimms++;
222 }
223
224 for (slot = 0; slot < NUM_SLOTS; slot++) {
225 spd_slot = 2 * channel + slot;
226 printk(BIOS_DEBUG, "SPD probe channel%d, slot%d\n", channel, slot);
227
228 struct dimm_attr_ddr3_st *const dimm = &ctrl->info.dimm[channel][slot];
229
230 /* Search for XMP profile */
231 spd_xmp_decode_ddr3(dimm, spd[spd_slot],
232 DDR3_XMP_PROFILE_1);
233
234 if (dimm->dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3) {
235 printram("No valid XMP profile found.\n");
236 spd_decode_ddr3(dimm, spd[spd_slot]);
237
238 } else if (ch_dimms > dimm->dimms_per_channel) {
239 printram(
240 "XMP profile supports %u DIMMs, but %u DIMMs are installed.\n",
241 dimm->dimms_per_channel, ch_dimms);
242
243 if (CONFIG(NATIVE_RAMINIT_IGNORE_XMP_MAX_DIMMS))
244 printk(BIOS_WARNING,
245 "XMP maximum DIMMs will be ignored.\n");
246 else
247 spd_decode_ddr3(dimm, spd[spd_slot]);
248
249 } else if (dimm->voltage != 1500) {
250 /* TODO: Support DDR3 voltages other than 1500mV */
251 printram("XMP profile's requested %u mV is unsupported.\n",
252 dimm->voltage);
253
254 if (CONFIG(NATIVE_RAMINIT_IGNORE_XMP_REQUESTED_VOLTAGE))
255 printk(BIOS_WARNING,
256 "XMP requested voltage will be ignored.\n");
257 else
258 spd_decode_ddr3(dimm, spd[spd_slot]);
259 }
260
261 /* Fill in CRC16 for MRC cache */
262 ctrl->spd_crc[channel][slot] =
263 spd_ddr3_calc_unique_crc(spd[spd_slot], sizeof(spd_ddr3_raw_data));
264
265 if (dimm->dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3) {
266 /* Mark DIMM as invalid */
267 dimm->ranks = 0;
268 dimm->size_mb = 0;
269 continue;
270 }
271
272 dram_print_spd_ddr3(dimm);
273 dimms++;
274 ctrl->rank_mirror[channel][slot * 2] = 0;
275 ctrl->rank_mirror[channel][slot * 2 + 1] = dimm->flags.pins_mirrored;
276
277 ctrl->channel_size_mb[channel] += dimm->size_mb;
278
279 if (!dimm->flags.is_ecc)
280 can_use_ecc = false;
281
282 ctrl->auto_self_refresh &= dimm->flags.asr;
283
284 ctrl->extended_temperature_range &= dimm->flags.ext_temp_refresh;
285
286 ctrl->rankmap[channel] |= ((1 << dimm->ranks) - 1) << (2 * slot);
287
288 printk(BIOS_DEBUG, "channel[%d] rankmap = 0x%x\n", channel,
289 ctrl->rankmap[channel]);
290 }
291
292 const u8 rc_0 = ctrl->info.dimm[channel][0].reference_card;
293 const u8 rc_1 = ctrl->info.dimm[channel][1].reference_card;
294
295 if (ch_dimms == NUM_SLOTS && rc_0 < 6 && rc_1 < 6) {
296 const int ref_card_offset_table[6][6] = {
297 { 0, 0, 0, 0, 2, 2 },
298 { 0, 0, 0, 0, 2, 2 },
299 { 0, 0, 0, 0, 2, 2 },
300 { 0, 0, 0, 0, 1, 1 },
301 { 2, 2, 2, 1, 0, 0 },
302 { 2, 2, 2, 1, 0, 0 },
303 };
304 ctrl->ref_card_offset[channel] = ref_card_offset_table[rc_0][rc_1];
305 } else {
306 ctrl->ref_card_offset[channel] = 0;
307 }
308 }
309
310 if (ctrl->ecc_forced || CONFIG(RAMINIT_ENABLE_ECC))
311 ctrl->ecc_enabled = can_use_ecc;
312 if (ctrl->ecc_forced && !ctrl->ecc_enabled)
313 die("ECC mode forced but non-ECC DIMM installed!");
314 printk(BIOS_DEBUG, "ECC is %s\n", ctrl->ecc_enabled ? "enabled" : "disabled");
315
316 ctrl->lanes = ctrl->ecc_enabled ? 9 : 8;
317
318 if (!dimms)
319 die("No DIMMs were found");
320 }
321
322 static void save_timings(ramctr_timing *ctrl)
323 {
324 /* Save the MRC S3 restore data to cbmem */
325 mrc_cache_stash_data(MRC_TRAINING_DATA, MRC_CACHE_VERSION, ctrl, sizeof(*ctrl));
326 }
327
328 static void reinit_ctrl(ramctr_timing *ctrl, const u32 cpuid)
329 {
330 /* Reset internal state */
331 memset(ctrl, 0, sizeof(*ctrl));
332
333 /* Get architecture */
334 ctrl->cpu = cpuid;
335
336 /* Get ECC support and mode */
337 ctrl->ecc_forced = get_host_ecc_forced();
338 ctrl->ecc_supported = ctrl->ecc_forced || get_host_ecc_cap();
339 printk(BIOS_DEBUG, "ECC supported: %s ECC forced: %s\n",
340 ctrl->ecc_supported ? "yes" : "no",
341 ctrl->ecc_forced ? "yes" : "no");
342 }
343
344 static void init_dram_ddr3(int s3resume, const u32 cpuid)
345 {
346 int me_uma_size, cbmem_was_inited, fast_boot, err;
347 ramctr_timing ctrl;
348 spd_ddr3_raw_data spds[4];
349 size_t mrc_size = 0;
350 ramctr_timing *ctrl_cached = NULL;
351
352 timestamp_add_now(TS_INITRAM_START);
353
354 mchbar_setbits32(SAPMCTL, 1 << 0);
355
356 /* Wait for ME to be ready */
357 intel_early_me_init();
358 me_uma_size = intel_early_me_uma_size();
359
360 printk(BIOS_DEBUG, "Starting native Platform init\n");
361
362 wait_txt_clear();
363
364 wrmsr(0x2e6, (msr_t) { .lo = 0, .hi = 0 });
365
366 const u32 sskpd = mchbar_read32(SSKPD); // !!! = 0x00000000
367 if ((pci_read_config16(SOUTHBRIDGE, 0xa2) & 0xa0) == 0x20 && sskpd && !s3resume) {
368 mchbar_write32(SSKPD, 0);
369 /* Need reset */
370 system_reset();
371 }
372
373 early_pch_init_native();
374 early_init_dmi();
375 early_thermal_init();
376
377 /* Try to find timings in MRC cache */
378 ctrl_cached = mrc_cache_current_mmap_leak(MRC_TRAINING_DATA,
379 MRC_CACHE_VERSION,
380 &mrc_size);
381 if (mrc_size < sizeof(ctrl))
382 ctrl_cached = NULL;
383
384 /* Before reusing training data, assert that the CPU has not been replaced */
385 if (ctrl_cached && cpuid != ctrl_cached->cpu) {
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();
398 }
399
400 /* Verify MRC cache for fast boot */
401 if (!s3resume && ctrl_cached) {
402 /* Load SPD unique information data. */
403 memset(spds, 0, sizeof(spds));
404 mainboard_get_spd(spds, 1);
405
406 /* 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");
410 } else {
411 fast_boot = s3resume;
412 }
413
414 if (fast_boot && intel_early_me_cpu_replaced())
415 fast_boot = false;
416
417 if (fast_boot) {
418 printk(BIOS_DEBUG, "Trying stored timings.\n");
419 memcpy(&ctrl, ctrl_cached, sizeof(ctrl));
420
421 err = try_init_dram_ddr3(&ctrl, fast_boot, s3resume, me_uma_size);
422 if (err) {
423 if (s3resume) {
424 /* Failed S3 resume, reset to come up cleanly */
425 system_reset();
426 }
427 /* No need to erase bad MRC cache here, it gets overwritten on a
428 successful boot */
429 printk(BIOS_ERR, "Stored timings are invalid !\n");
430 fast_boot = 0;
431 }
432 }
433 if (!fast_boot) {
434 /* Reset internal state */
435 reinit_ctrl(&ctrl, cpuid);
436
437 printk(BIOS_INFO, "ECC RAM %s.\n", ctrl.ecc_forced ? "required" :
438 ctrl.ecc_supported ? "supported" : "unsupported");
439
440 /* Get DDR3 SPD data */
441 memset(spds, 0, sizeof(spds));
442 mainboard_get_spd(spds, 0);
443 dram_find_spds_ddr3(spds, &ctrl);
444
445 err = try_init_dram_ddr3(&ctrl, fast_boot, s3resume, me_uma_size);
446 }
447
448 if (err) {
449 /* Fallback: disable failing channel */
450 printk(BIOS_ERR, "RAM training failed, trying fallback.\n");
451 printram("Disable failing channel.\n");
452
453 /* Reset internal state */
454 reinit_ctrl(&ctrl, cpuid);
455
456 /* Reset DDR3 frequency */
457 dram_find_spds_ddr3(spds, &ctrl);
458
459 /* Disable failing channel */
460 disable_channel(&ctrl, GET_ERR_CHANNEL(err));
461
462 err = try_init_dram_ddr3(&ctrl, fast_boot, s3resume, me_uma_size);
463 }
464
465 if (err)
466 die("raminit failed");
467
468 /* FIXME: should be hardware revision-dependent. The register only exists on IVB. */
469 mchbar_write32(CHANNEL_HASH, 0x00a030ce);
470
471 set_scrambling_seed(&ctrl);
472
473 if (!s3resume && ctrl.ecc_enabled)
474 channel_scrub(&ctrl);
475
476 set_normal_operation(&ctrl);
477
478 final_registers(&ctrl);
479
480 /* can't do this earlier because it needs to be done in normal operation */
481 if (CONFIG(DEBUG_RAM_SETUP) && !s3resume && ctrl.ecc_enabled) {
482 uint32_t i, tseg = pci_read_config32(HOST_BRIDGE, TSEGMB);
483
484 printk(BIOS_INFO, "RAMINIT: ECC scrub test on first channel up to 0x%x\n",
485 tseg);
486
487 /*
488 * This test helps to debug the ECC scrubbing.
489 * It likely tests every channel/rank, as rank interleave and enhanced
490 * interleave are enabled, but there's no guarantee for it.
491 */
492
493 /* Skip first MB to avoid special case for A-seg and test up to TSEG */
494 for (i = 1; i < tseg >> 20; i++) {
495 for (int j = 0; j < 1 * MiB; j += 4096) {
496 uintptr_t addr = i * MiB + j;
497 if (read32((u32 *)addr) == 0)
498 continue;
499
500 printk(BIOS_ERR, "RAMINIT: ECC scrub: DRAM not cleared at"
501 " addr 0x%lx\n", addr);
502 break;
503 }
504 }
505 printk(BIOS_INFO, "RAMINIT: ECC scrub test done.\n");
506 }
507
508 /* Zone config */
509 dram_zones(&ctrl, 0);
510
511 intel_early_me_init_done(ME_INIT_STATUS_SUCCESS);
512 intel_early_me_status();
513
514 report_memory_config();
515
516 timestamp_add_now(TS_INITRAM_END);
517
518 cbmem_was_inited = !cbmem_recovery(s3resume);
519 if (!fast_boot)
520 save_timings(&ctrl);
521 if (s3resume && !cbmem_was_inited) {
522 /* Failed S3 resume, reset to come up cleanly */
523 system_reset();
524 }
525
526 if (!s3resume)
527 setup_sdram_meminfo(&ctrl);
528 }
529
530 void perform_raminit(int s3resume)
531 {
532 post_code(0x3a);
533 init_dram_ddr3(s3resume, cpu_get_cpuid());
534 }