Debian Codegraph @

src/northbridge/intel/haswell/native_raminit/raminit_main.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 #include <assert.h>
4 #include <commonlib/bsd/clamp.h>
5 #include <console/console.h>
6 #include <cpu/intel/haswell/haswell.h>
7 #include <delay.h>
8 #include <device/pci_ops.h>
9 #include <northbridge/intel/haswell/chip.h>
10 #include <northbridge/intel/haswell/haswell.h>
11 #include <northbridge/intel/haswell/raminit.h>
12 #include <static.h>
13 #include <string.h>
14 #include <timer.h>
15 #include <types.h>
16
17 #include "raminit_native.h"
18
19 static enum raminit_status pre_training(struct sysinfo *ctrl)
20 {
21 /* Skip on S3 resume */
22 if (ctrl->bootmode == BOOTMODE_S3)
23 return RAMINIT_STATUS_SUCCESS;
24
25 for (uint8_t channel = 0; channel < NUM_CHANNELS; channel++) {
26 for (uint8_t slot = 0; slot < NUM_SLOTS; slot++) {
27 if (!rank_in_ch(ctrl, slot + slot, channel))
28 continue;
29
30 printk(RAM_DEBUG, "C%uS%u:\n", channel, slot);
31 printk(RAM_DEBUG, "\tMR0: 0x%04x\n", ctrl->mr0[channel][slot]);
32 printk(RAM_DEBUG, "\tMR1: 0x%04x\n", ctrl->mr1[channel][slot]);
33 printk(RAM_DEBUG, "\tMR2: 0x%04x\n", ctrl->mr2[channel][slot]);
34 printk(RAM_DEBUG, "\tMR3: 0x%04x\n", ctrl->mr3[channel][slot]);
35 printk(RAM_DEBUG, "\n");
36 }
37 if (ctrl->is_ecc) {
38 union mad_dimm_reg mad_dimm = {
39 .raw = mchbar_read32(MAD_DIMM(channel)),
40 };
41 /* Enable ECC I/O */
42 mad_dimm.ecc_mode = 1;
43 mchbar_write32(MAD_DIMM(channel), mad_dimm.raw);
44 /* Wait 4 usec after enabling the ECC I/O, needed by HW */
45 udelay(4);
46 }
47 }
48 setup_wdb(ctrl);
49 return RAMINIT_STATUS_SUCCESS;
50 }
51
52
53 static uint8_t get_cmd_stretch(const uint8_t tCMD)
54 {
55 switch (tCMD) {
56 case 1:
57 return 0;
58 case 2:
59 return 2;
60 case 3:
61 return 3;
62 default:
63 return 2;
64 }
65 }
66
67 /** TODO: Why do we re-read the MCHBAR registers? Is the cached value outdated? **/
68 static void update_command_rate(struct sysinfo *const ctrl, const uint8_t channel)
69 {
70 const uint8_t old_n = clamp_s32(1, ctrl->tc_bankrank_a[channel].cmd_stretch, 3);
71 const uint8_t new_n = ctrl->tCMD;
72
73 /* Update CmdN timing */
74 ctrl->tc_bankrank_a[channel].raw = mchbar_read32(TC_BANK_RANK_A_ch(channel));
75 ctrl->tc_bankrank_a[channel].cmd_stretch = get_cmd_stretch(ctrl->tCMD);
76 mchbar_write32(TC_BANK_RANK_A_ch(channel), ctrl->tc_bankrank_a[channel].raw);
77
78 /* Switch to regular tXP value */
79 ctrl->tc_bankrank_c[channel].raw = mchbar_read32(TC_BANK_RANK_C_ch(channel));
80 ctrl->tc_bankrank_c[channel].tXP = ctrl->tXP;
81 mchbar_write32(TC_BANK_RANK_C_ch(channel), ctrl->tc_bankrank_c[channel].raw);
82
83 /* Adjust RT values to compensate */
84 const int8_t delta = new_n - old_n;
85 for (uint8_t rank = 0; rank < NUM_SLOTRANKS; rank++) {
86 if (!rank_in_ch(ctrl, rank, channel))
87 continue;
88
89 /*
90 * RT (roundtrip) latency is the time it takes for signals to go
91 * from the memory controller to the DRAM and back to the memory
92 * controller. It's likely the delta is doubled for this reason.
93 */
94 ctrl->rt_latency[channel].rank[rank] += delta * 2;
95 }
96 mchbar_write32(SC_ROUNDT_LAT_ch(channel), ctrl->rt_latency[channel].raw);
97 }
98
99 static enum raminit_status post_training(struct sysinfo *const ctrl)
100 {
101 /* Command rate is always 1N for LPDDR3 */
102 if (ctrl->lpddr) {
103 return RAMINIT_STATUS_SUCCESS;
104 }
105 for (uint8_t channel = 0; channel < NUM_CHANNELS; channel++) {
106 if (!does_ch_exist(ctrl, channel))
107 continue;
108
109 update_command_rate(ctrl, channel);
110 }
111 return RAMINIT_STATUS_SUCCESS;
112 }
113
114 struct task_entry {
115 enum raminit_status (*task)(struct sysinfo *);
116 bool is_enabled;
117 const char *name;
118 };
119
120 static const struct task_entry cold_boot[] = {
121 { collect_spd_info, true, "PROCSPD", },
122 { initialise_mpll, true, "INITMPLL", },
123 { convert_timings, true, "CONVTIM", },
124 { configure_mc, true, "CONFMC", },
125 { configure_memory_map, true, "MEMMAP", },
126 { do_jedec_init, true, "JEDECINIT", },
127 { pre_training, true, "PRETRAIN", },
128 { train_sense_amp_offset, true, "SOT", },
129 { train_receive_enable, true, "RCVET", },
130 { train_read_mpr, true, "RDMPRT", },
131 { train_jedec_write_leveling, true, "JWRL", },
132 { optimise_comp, true, "OPTCOMP", },
133 { post_training, true, "POSTTRAIN", },
134 { activate_mc, true, "ACTIVATE", },
135 { save_training_values, true, "SAVE_TRAIN", },
136 { save_non_training, true, "SAVE_NONT", },
137 { raminit_done, true, "RAMINITEND", },
138 };
139
140 static const struct task_entry fast_boot[] = {
141 { collect_spd_info, true, "PROCSPD", },
142 { restore_non_training, true, "RST_NONT", },
143 { initialise_mpll, true, "INITMPLL", },
144 { configure_mc, true, "CONFMC", },
145 { configure_memory_map, true, "MEMMAP", },
146 { do_jedec_init, true, "JEDECINIT", },
147 { pre_training, true, "PRETRAIN", },
148 { restore_training_values, true, "RST_TRAIN", },
149 { exit_selfrefresh, true, "EXIT_SR", },
150 { normal_state, true, "NORMALMODE", },
151 { raminit_done, true, "RAMINITEND", },
152 };
153
154 _Static_assert(ARRAY_SIZE(cold_boot) >= ARRAY_SIZE(fast_boot),
155 "Code assumes cold boot task list is the longest one");
156
157 /* Return a generic stepping value to make stepping checks simpler */
158 static enum generic_stepping get_stepping(const uint32_t cpuid)
159 {
160 switch (cpuid) {
161 case CPUID_HASWELL_A0:
162 die("Haswell stepping A0 is not supported\n");
163 case CPUID_HASWELL_B0:
164 case CPUID_HASWELL_ULT_B0:
165 case CPUID_CRYSTALWELL_B0:
166 return STEPPING_B0;
167 case CPUID_HASWELL_C0:
168 case CPUID_HASWELL_ULT_C0:
169 case CPUID_CRYSTALWELL_C0:
170 return STEPPING_C0;
171 default:
172 /** TODO: Add Broadwell support someday **/
173 die("Unknown CPUID 0x%x\n", cpuid);
174 }
175 }
176
177 static void initialize_ctrl(struct sysinfo *ctrl)
178 {
179 const struct northbridge_intel_haswell_config *cfg = config_of_soc();
180 const enum raminit_boot_mode bootmode = ctrl->bootmode;
181
182 memset(ctrl, 0, sizeof(*ctrl));
183
184 ctrl->cpu = cpu_get_cpuid();
185 ctrl->stepping = get_stepping(ctrl->cpu);
186 ctrl->vdd_mv = is_hsw_ult() ? 1350 : 1500; /** FIXME: Hardcoded, does it matter? **/
187 ctrl->dq_pins_interleaved = cfg->dq_pins_interleaved;
188 ctrl->restore_mrs = false;
189 ctrl->bootmode = bootmode;
190 }
191
192 /** TODO: Adjust unit scale dynamically? **/
193 #define T_SCALE 1000 /* 1 = usecs, 1000 = msecs */
194
195 static enum raminit_status try_raminit(
196 struct sysinfo *ctrl,
197 const struct task_entry *const schedule,
198 const size_t length)
199 {
200 enum raminit_status status = RAMINIT_STATUS_UNSPECIFIED_ERROR;
201
202 long spent_time[ARRAY_SIZE(cold_boot)] = { 0 };
203 long total = 0;
204
205 size_t i;
206 for (i = 0; i < length; i++) {
207 const struct task_entry *const entry = &schedule[i];
208 assert(entry);
209 assert(entry->name);
210 if (!entry->is_enabled)
211 continue;
212
213 assert(entry->task);
214 printk(RAM_DEBUG, "\nExecuting raminit task %s\n", entry->name);
215 struct mono_time prev, curr;
216 timer_monotonic_get(&prev);
217 status = entry->task(ctrl);
218 timer_monotonic_get(&curr);
219 spent_time[i] = mono_time_diff_microseconds(&prev, &curr);
220 printk(RAM_DEBUG, "\n");
221 if (status) {
222 i++;
223 printk(BIOS_ERR, "raminit failed on step %s\n", entry->name);
224 break;
225 }
226 }
227
228 if (CONFIG(DEBUG_RAM_SETUP)) {
229 const char unit_multiplier = T_SCALE == 1 ? 'u' : 'm';
230 printk(RAM_DEBUG, "+------------------+------------+\n");
231 printk(RAM_DEBUG, "| Task | %csecs |\n", unit_multiplier);
232 printk(RAM_DEBUG, "+------------------+------------+\n");
233 assert(i <= length);
234 for (size_t j = 0; j < i; j++) {
235 char buf[] = " ";
236 strncpy(buf, schedule[j].name, strlen(schedule[j].name));
237 printk(RAM_DEBUG, "| %s | % 10ld |\n", buf, spent_time[j] / T_SCALE);
238 total += spent_time[j];
239 }
240 printk(RAM_DEBUG, "+------------------+------------+\n");
241 printk(RAM_DEBUG, "| Total | % 10ld |\n", total / T_SCALE);
242 printk(RAM_DEBUG, "+------------------+------------+\n");
243 }
244
245 return status;
246 }
247
248 void raminit_main(const enum raminit_boot_mode bootmode)
249 {
250 /*
251 * The mighty_ctrl struct. Will happily nuke the pre-RAM stack
252 * if left unattended. Make it static and pass pointers to it.
253 */
254 static struct sysinfo mighty_ctrl;
255
256 mighty_ctrl.bootmode = bootmode;
257 initialize_ctrl(&mighty_ctrl);
258
259 enum raminit_status status = RAMINIT_STATUS_UNSPECIFIED_ERROR;
260
261 if (bootmode != BOOTMODE_COLD) {
262 status = try_raminit(&mighty_ctrl, fast_boot, ARRAY_SIZE(fast_boot));
263 if (status == RAMINIT_STATUS_SUCCESS)
264 return;
265 }
266
267 /** TODO: Try more than once **/
268 status = try_raminit(&mighty_ctrl, cold_boot, ARRAY_SIZE(cold_boot));
269
270 if (status != RAMINIT_STATUS_SUCCESS)
271 die("Memory initialization was met with utmost failure and misery\n");
272 }