Debian Codegraph @

src/soc/intel/alderlake/meminit.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 #include <console/console.h>
4 #include <fsp/util.h>
5 #include <soc/meminit.h>
6 #include <string.h>
7
8 #define LPX_PHYSICAL_CH_WIDTH 16
9 #define LPX_CHANNELS CHANNEL_COUNT(LPX_PHYSICAL_CH_WIDTH)
10
11 #define DDR4_PHYSICAL_CH_WIDTH 64
12 #define DDR4_CHANNELS CHANNEL_COUNT(DDR4_PHYSICAL_CH_WIDTH)
13
14 #define DDR5_PHYSICAL_CH_WIDTH 64 /* 32*2 */
15 #define DDR5_CHANNELS CHANNEL_COUNT(DDR5_PHYSICAL_CH_WIDTH)
16
17 static void set_rcomp_config(FSP_M_CONFIG *mem_cfg, const struct mb_cfg *mb_cfg)
18 {
19 if (mb_cfg->rcomp.resistor != 0)
20 mem_cfg->RcompResistor = mb_cfg->rcomp.resistor;
21
22 for (size_t i = 0; i < ARRAY_SIZE(mem_cfg->RcompTarget); i++) {
23 if (mb_cfg->rcomp.targets[i] != 0)
24 mem_cfg->RcompTarget[i] = mb_cfg->rcomp.targets[i];
25 }
26 }
27
28 static void meminit_lp4x(FSP_M_CONFIG *mem_cfg)
29 {
30 mem_cfg->DqPinsInterleaved = 0;
31 }
32
33 static void meminit_lp5x(FSP_M_CONFIG *mem_cfg, const struct mem_lp5x_config *lp5x_config)
34 {
35 mem_cfg->DqPinsInterleaved = 0;
36 mem_cfg->Lp5CccConfig = lp5x_config->ccc_config;
37 }
38
39 static void meminit_ddr(FSP_M_CONFIG *mem_cfg, const struct mem_ddr_config *ddr_config)
40 {
41 mem_cfg->DqPinsInterleaved = ddr_config->dq_pins_interleaved;
42 }
43
44 static const struct soc_mem_cfg soc_mem_cfg[] = {
45 [MEM_TYPE_DDR4] = {
46 .num_phys_channels = DDR4_CHANNELS,
47 .phys_to_mrc_map = {
48 [0] = 0,
49 [1] = 4,
50 },
51 .md_phy_masks = {
52 /*
53 * Only physical channel 0 is populated in case of half-populated
54 * configuration.
55 */
56 .half_channel = BIT(0),
57 /* In mixed topologies, either channel 0 or 1 can be memory-down. */
58 .mixed_topo = BIT(0) | BIT(1),
59 },
60 },
61 [MEM_TYPE_DDR5] = {
62 .num_phys_channels = DDR5_CHANNELS,
63 .phys_to_mrc_map = {
64 [0] = 0,
65 [1] = 4,
66 },
67 .md_phy_masks = {
68 /*
69 * Only channel 0 is populated in case of half-populated
70 * configuration.
71 */
72 .half_channel = BIT(0),
73 /* In mixed topologies, either channel 0 or 1 can be memory-down. */
74 .mixed_topo = BIT(0) | BIT(1),
75 },
76 },
77 [MEM_TYPE_LP4X] = {
78 .num_phys_channels = LPX_CHANNELS,
79 .phys_to_mrc_map = {
80 [0] = 0,
81 [1] = 1,
82 [2] = 2,
83 [3] = 3,
84 [4] = 4,
85 [5] = 5,
86 [6] = 6,
87 [7] = 7,
88 },
89 .md_phy_masks = {
90 /*
91 * Physical channels 0, 1, 2 and 3 are populated in case of
92 * half-populated configurations.
93 */
94 .half_channel = BIT(0) | BIT(1) | BIT(2) | BIT(3),
95 /* LP4x does not support mixed topologies. */
96 },
97 },
98 [MEM_TYPE_LP5X] = {
99 .num_phys_channels = LPX_CHANNELS,
100 .phys_to_mrc_map = {
101 [0] = 0,
102 [1] = 1,
103 [2] = 2,
104 [3] = 3,
105 [4] = 4,
106 [5] = 5,
107 [6] = 6,
108 [7] = 7,
109 },
110 .md_phy_masks = {
111 /*
112 * Physical channels 0, 1, 2 and 3 are populated in case of
113 * half-populated configurations.
114 */
115 .half_channel = BIT(0) | BIT(1) | BIT(2) | BIT(3),
116 /* LP5x does not support mixed topologies. */
117 },
118 },
119 };
120
121 static void mem_init_spd_upds(FSP_M_CONFIG *mem_cfg, const struct mem_channel_data *data,
122 bool expand_channels)
123 {
124 uint32_t *spd_upds[MRC_CHANNELS][CONFIG_DIMMS_PER_CHANNEL] = {
125 [0] = { &mem_cfg->MemorySpdPtr000, &mem_cfg->MemorySpdPtr001, },
126 [1] = { &mem_cfg->MemorySpdPtr010, &mem_cfg->MemorySpdPtr011, },
127 [2] = { &mem_cfg->MemorySpdPtr020, &mem_cfg->MemorySpdPtr021, },
128 [3] = { &mem_cfg->MemorySpdPtr030, &mem_cfg->MemorySpdPtr031, },
129 [4] = { &mem_cfg->MemorySpdPtr100, &mem_cfg->MemorySpdPtr101, },
130 [5] = { &mem_cfg->MemorySpdPtr110, &mem_cfg->MemorySpdPtr111, },
131 [6] = { &mem_cfg->MemorySpdPtr120, &mem_cfg->MemorySpdPtr121, },
132 [7] = { &mem_cfg->MemorySpdPtr130, &mem_cfg->MemorySpdPtr131, },
133 };
134 uint8_t *disable_channel_upds[MRC_CHANNELS] = {
135 &mem_cfg->DisableMc0Ch0,
136 &mem_cfg->DisableMc0Ch1,
137 &mem_cfg->DisableMc0Ch2,
138 &mem_cfg->DisableMc0Ch3,
139 &mem_cfg->DisableMc1Ch0,
140 &mem_cfg->DisableMc1Ch1,
141 &mem_cfg->DisableMc1Ch2,
142 &mem_cfg->DisableMc1Ch3,
143 };
144 size_t ch, dimm;
145
146 mem_cfg->MemorySpdDataLen = data->spd_len;
147
148 for (ch = 0; ch < MRC_CHANNELS; ch++) {
149 uint8_t *disable_channel_ptr = disable_channel_upds[ch];
150 bool enable_channel = 0;
151
152 for (dimm = 0; dimm < CONFIG_DIMMS_PER_CHANNEL; dimm++) {
153 uint32_t *spd_ptr = spd_upds[ch][dimm];
154
155 /*
156 * In DDR5 systems, since each DIMM has 2 channels,
157 * we need to copy the SPD data such that:
158 * Channel 0 data is used by channel 0 and 1
159 * Channel 2 data is used by channel 2 and 3
160 * Channel 4 data is used by channel 4 and 5
161 * Channel 6 data is used by channel 6 and 7
162 */
163 if (expand_channels)
164 *spd_ptr = data->spd[ch & ~1][dimm];
165 else
166 *spd_ptr = data->spd[ch][dimm];
167
168 if (*spd_ptr)
169 enable_channel = 1;
170 }
171 *disable_channel_ptr = !enable_channel;
172 }
173 }
174
175 static void mem_init_dq_dqs_upds(void *upds[MRC_CHANNELS], const void *map, size_t upd_size,
176 const struct mem_channel_data *data, bool auto_detect)
177 {
178 size_t i;
179
180 for (i = 0; i < MRC_CHANNELS; i++, map += upd_size) {
181 if (auto_detect ||
182 !channel_is_populated(i, MRC_CHANNELS, data->ch_population_flags))
183 memset(upds[i], 0, upd_size);
184 else
185 memcpy(upds[i], map, upd_size);
186 }
187 }
188
189 static void mem_init_dq_upds(FSP_M_CONFIG *mem_cfg, const struct mem_channel_data *data,
190 const struct mb_cfg *mb_cfg, bool auto_detect)
191 {
192 void *dq_upds[MRC_CHANNELS] = {
193 &mem_cfg->DqMapCpu2DramMc0Ch0,
194 &mem_cfg->DqMapCpu2DramMc0Ch1,
195 &mem_cfg->DqMapCpu2DramMc0Ch2,
196 &mem_cfg->DqMapCpu2DramMc0Ch3,
197 &mem_cfg->DqMapCpu2DramMc1Ch0,
198 &mem_cfg->DqMapCpu2DramMc1Ch1,
199 &mem_cfg->DqMapCpu2DramMc1Ch2,
200 &mem_cfg->DqMapCpu2DramMc1Ch3,
201 };
202
203 const size_t upd_size = sizeof(mem_cfg->DqMapCpu2DramMc0Ch0);
204
205 _Static_assert(sizeof(mem_cfg->DqMapCpu2DramMc0Ch0) == CONFIG_MRC_CHANNEL_WIDTH,
206 "Incorrect DQ UPD size!");
207
208 mem_init_dq_dqs_upds(dq_upds, mb_cfg->dq_map, upd_size, data, auto_detect);
209 }
210
211 static void mem_init_dqs_upds(FSP_M_CONFIG *mem_cfg, const struct mem_channel_data *data,
212 const struct mb_cfg *mb_cfg, bool auto_detect)
213 {
214 void *dqs_upds[MRC_CHANNELS] = {
215 &mem_cfg->DqsMapCpu2DramMc0Ch0,
216 &mem_cfg->DqsMapCpu2DramMc0Ch1,
217 &mem_cfg->DqsMapCpu2DramMc0Ch2,
218 &mem_cfg->DqsMapCpu2DramMc0Ch3,
219 &mem_cfg->DqsMapCpu2DramMc1Ch0,
220 &mem_cfg->DqsMapCpu2DramMc1Ch1,
221 &mem_cfg->DqsMapCpu2DramMc1Ch2,
222 &mem_cfg->DqsMapCpu2DramMc1Ch3,
223 };
224
225 const size_t upd_size = sizeof(mem_cfg->DqsMapCpu2DramMc0Ch0);
226
227 _Static_assert(sizeof(mem_cfg->DqsMapCpu2DramMc0Ch0) == CONFIG_MRC_CHANNEL_WIDTH / 8,
228 "Incorrect DQS UPD size!");
229
230 mem_init_dq_dqs_upds(dqs_upds, mb_cfg->dqs_map, upd_size, data, auto_detect);
231 }
232
233
234 /*
235 * A memory channel will be disabled if corresponding bit in
236 * ch_disable_mask is set.
237 */
238 static void mem_init_override_channel_mask(FSP_M_CONFIG *mem_cfg)
239 {
240 uint8_t *disable_channel_upds[MRC_CHANNELS] = {
241 &mem_cfg->DisableMc0Ch0,
242 &mem_cfg->DisableMc0Ch1,
243 &mem_cfg->DisableMc0Ch2,
244 &mem_cfg->DisableMc0Ch3,
245 &mem_cfg->DisableMc1Ch0,
246 &mem_cfg->DisableMc1Ch1,
247 &mem_cfg->DisableMc1Ch2,
248 &mem_cfg->DisableMc1Ch3,
249 };
250
251 uint8_t ch_disable_mask = mb_get_channel_disable_mask();
252 if (ch_disable_mask == 0)
253 return;
254
255 /* Mc0Ch0 cannot be disabled */
256 if (ch_disable_mask & BIT(0)) {
257 printk(BIOS_ERR, "Cannot disable the first memory channel (Mc0Ch0).\n");
258 return;
259 }
260
261 for (size_t ch = 1; ch < MRC_CHANNELS; ch++) {
262 if (ch_disable_mask & BIT(ch))
263 *disable_channel_upds[ch] = 1;
264 }
265 }
266
267 void memcfg_init(FSPM_UPD *memupd, const struct mb_cfg *mb_cfg,
268 const struct mem_spd *spd_info, bool half_populated)
269 {
270 struct mem_channel_data data;
271 bool dq_dqs_auto_detect = false;
272 bool expand_channels = false;
273 FSP_M_CONFIG *mem_cfg = &memupd->FspmConfig;
274
275 #if CONFIG(SOC_INTEL_RAPTORLAKE) && !CONFIG(FSP_USE_REPO)
276 mem_cfg->CsPiStartHighinEct = mb_cfg->cs_pi_start_high_in_ect;
277 #endif
278 mem_cfg->ECT = mb_cfg->ect;
279 mem_cfg->UserBd = mb_cfg->UserBd;
280 set_rcomp_config(mem_cfg, mb_cfg);
281
282 /* Fill command mirror for memory */
283 mem_cfg->CmdMirror = mb_cfg->CmdMirror;
284
285 /* Fill LpDdrrDqDqs Retraining for memory */
286 mem_cfg->LpDdrDqDqsReTraining = mb_cfg->LpDdrDqDqsReTraining;
287
288 switch (mb_cfg->type) {
289 case MEM_TYPE_DDR4:
290 meminit_ddr(mem_cfg, &mb_cfg->ddr_config);
291 dq_dqs_auto_detect = true;
292 break;
293 case MEM_TYPE_DDR5:
294 meminit_ddr(mem_cfg, &mb_cfg->ddr_config);
295 dq_dqs_auto_detect = true;
296 expand_channels = true;
297 break;
298 case MEM_TYPE_LP4X:
299 meminit_lp4x(mem_cfg);
300 break;
301 case MEM_TYPE_LP5X:
302 meminit_lp5x(mem_cfg, &mb_cfg->lp5x_config);
303 break;
304 default:
305 die("Unsupported memory type(%d)\n", mb_cfg->type);
306 }
307
308 mem_populate_channel_data(memupd, &soc_mem_cfg[mb_cfg->type], spd_info, half_populated,
309 &data);
310 mem_init_spd_upds(mem_cfg, &data, expand_channels);
311 mem_init_override_channel_mask(mem_cfg);
312 mem_init_dq_upds(mem_cfg, &data, mb_cfg, dq_dqs_auto_detect);
313 mem_init_dqs_upds(mem_cfg, &data, mb_cfg, dq_dqs_auto_detect);
314 }