Debian Codegraph @

src/soc/intel/meteorlake/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 DDR5_PHYSICAL_CH_WIDTH 64 /* 32*2 */
12 #define DDR5_CHANNELS CHANNEL_COUNT(DDR5_PHYSICAL_CH_WIDTH)
13
14 static void set_rcomp_config(FSP_M_CONFIG *mem_cfg, const struct mb_cfg *mb_cfg)
15 {
16 if (mb_cfg->rcomp.resistor != 0)
17 mem_cfg->RcompResistor = mb_cfg->rcomp.resistor;
18
19 for (size_t i = 0; i < ARRAY_SIZE(mem_cfg->RcompTarget); i++) {
20 if (mb_cfg->rcomp.targets[i] != 0)
21 mem_cfg->RcompTarget[i] = mb_cfg->rcomp.targets[i];
22 }
23 }
24
25 static void meminit_lp5x(FSP_M_CONFIG *mem_cfg, const struct mem_lp5x_config *lp5x_config)
26 {
27 mem_cfg->DqPinsInterleaved = 0;
28 mem_cfg->Lp5CccConfig = lp5x_config->ccc_config;
29 }
30
31 static void meminit_ddr(FSP_M_CONFIG *mem_cfg, const struct mem_ddr_config *ddr_config)
32 {
33 mem_cfg->DqPinsInterleaved = ddr_config->dq_pins_interleaved;
34 }
35
36 static const struct soc_mem_cfg soc_mem_cfg[] = {
37 [MEM_TYPE_DDR5] = {
38 .num_phys_channels = DDR5_CHANNELS,
39 .phys_to_mrc_map = {
40 [0] = 0,
41 [1] = 4,
42 },
43 .md_phy_masks = {
44 /*
45 * Only channel 0 is populated in case of half-populated
46 * configuration.
47 */
48 .half_channel = BIT(0),
49 /* In mixed topologies, either channel 0 or 1 can be memory-down. */
50 .mixed_topo = BIT(0) | BIT(1),
51 },
52 },
53 [MEM_TYPE_LP5X] = {
54 .num_phys_channels = LPX_CHANNELS,
55 .phys_to_mrc_map = {
56 [0] = 0,
57 [1] = 1,
58 [2] = 2,
59 [3] = 3,
60 [4] = 4,
61 [5] = 5,
62 [6] = 6,
63 [7] = 7,
64 },
65 .md_phy_masks = {
66 /*
67 * Physical channels 0, 1, 2 and 3 are populated in case of
68 * half-populated configurations.
69 */
70 .half_channel = BIT(0) | BIT(1) | BIT(2) | BIT(3),
71 /* LP5x does not support mixed topologies. */
72 },
73 },
74 };
75
76 static void mem_init_spd_upds(FSP_M_CONFIG *mem_cfg, const struct mem_channel_data *data,
77 bool expand_channels)
78 {
79 efi_uintn_t *spd_upds[MRC_CHANNELS][CONFIG_DIMMS_PER_CHANNEL] = {
80 [0] = { &mem_cfg->MemorySpdPtr000, &mem_cfg->MemorySpdPtr001, },
81 [1] = { &mem_cfg->MemorySpdPtr010, &mem_cfg->MemorySpdPtr011, },
82 [2] = { &mem_cfg->MemorySpdPtr020, &mem_cfg->MemorySpdPtr021, },
83 [3] = { &mem_cfg->MemorySpdPtr030, &mem_cfg->MemorySpdPtr031, },
84 [4] = { &mem_cfg->MemorySpdPtr100, &mem_cfg->MemorySpdPtr101, },
85 [5] = { &mem_cfg->MemorySpdPtr110, &mem_cfg->MemorySpdPtr111, },
86 [6] = { &mem_cfg->MemorySpdPtr120, &mem_cfg->MemorySpdPtr121, },
87 [7] = { &mem_cfg->MemorySpdPtr130, &mem_cfg->MemorySpdPtr131, },
88 };
89 uint8_t *disable_channel_upds[MRC_CHANNELS] = {
90 &mem_cfg->DisableMc0Ch0,
91 &mem_cfg->DisableMc0Ch1,
92 &mem_cfg->DisableMc0Ch2,
93 &mem_cfg->DisableMc0Ch3,
94 &mem_cfg->DisableMc1Ch0,
95 &mem_cfg->DisableMc1Ch1,
96 &mem_cfg->DisableMc1Ch2,
97 &mem_cfg->DisableMc1Ch3,
98 };
99 size_t ch, dimm;
100
101 mem_cfg->MemorySpdDataLen = data->spd_len;
102
103 for (ch = 0; ch < MRC_CHANNELS; ch++) {
104 uint8_t *disable_channel_ptr = disable_channel_upds[ch];
105 bool enable_channel = 0;
106
107 for (dimm = 0; dimm < CONFIG_DIMMS_PER_CHANNEL; dimm++) {
108 efi_uintn_t *spd_ptr = spd_upds[ch][dimm];
109
110 /*
111 * In DDR5 systems, since each DIMM has 2 channels,
112 * we need to copy the SPD data such that:
113 * Channel 0 data is used by channel 0 and 1
114 * Channel 2 data is used by channel 2 and 3
115 * Channel 4 data is used by channel 4 and 5
116 * Channel 6 data is used by channel 6 and 7
117 */
118 if (expand_channels)
119 *spd_ptr = data->spd[ch & ~1][dimm];
120 else
121 *spd_ptr = data->spd[ch][dimm];
122
123 if (*spd_ptr)
124 enable_channel = 1;
125 }
126 *disable_channel_ptr = !enable_channel;
127 }
128 }
129
130 static void mem_init_dq_dqs_upds(void *upds[MRC_CHANNELS], const void *map, size_t upd_size,
131 const struct mem_channel_data *data, bool auto_detect)
132 {
133 size_t i;
134
135 for (i = 0; i < MRC_CHANNELS; i++, map += upd_size) {
136 if (auto_detect ||
137 !channel_is_populated(i, MRC_CHANNELS, data->ch_population_flags))
138 memset(upds[i], 0, upd_size);
139 else
140 memcpy(upds[i], map, upd_size);
141 }
142 }
143
144 static void mem_init_dq_upds(FSP_M_CONFIG *mem_cfg, const struct mem_channel_data *data,
145 const struct mb_cfg *mb_cfg, bool auto_detect)
146 {
147 void *dq_upds[MRC_CHANNELS] = {
148 &mem_cfg->DqMapCpu2DramMc0Ch0,
149 &mem_cfg->DqMapCpu2DramMc0Ch1,
150 &mem_cfg->DqMapCpu2DramMc0Ch2,
151 &mem_cfg->DqMapCpu2DramMc0Ch3,
152 &mem_cfg->DqMapCpu2DramMc1Ch0,
153 &mem_cfg->DqMapCpu2DramMc1Ch1,
154 &mem_cfg->DqMapCpu2DramMc1Ch2,
155 &mem_cfg->DqMapCpu2DramMc1Ch3,
156 };
157
158 const size_t upd_size = sizeof(mem_cfg->DqMapCpu2DramMc0Ch0);
159
160 _Static_assert(sizeof(mem_cfg->DqMapCpu2DramMc0Ch0) == CONFIG_MRC_CHANNEL_WIDTH,
161 "Incorrect DQ UPD size!");
162
163 mem_init_dq_dqs_upds(dq_upds, mb_cfg->dq_map, upd_size, data, auto_detect);
164 }
165
166 static void mem_init_dqs_upds(FSP_M_CONFIG *mem_cfg, const struct mem_channel_data *data,
167 const struct mb_cfg *mb_cfg, bool auto_detect)
168 {
169 void *dqs_upds[MRC_CHANNELS] = {
170 &mem_cfg->DqsMapCpu2DramMc0Ch0,
171 &mem_cfg->DqsMapCpu2DramMc0Ch1,
172 &mem_cfg->DqsMapCpu2DramMc0Ch2,
173 &mem_cfg->DqsMapCpu2DramMc0Ch3,
174 &mem_cfg->DqsMapCpu2DramMc1Ch0,
175 &mem_cfg->DqsMapCpu2DramMc1Ch1,
176 &mem_cfg->DqsMapCpu2DramMc1Ch2,
177 &mem_cfg->DqsMapCpu2DramMc1Ch3,
178 };
179
180 const size_t upd_size = sizeof(mem_cfg->DqsMapCpu2DramMc0Ch0);
181
182 _Static_assert(sizeof(mem_cfg->DqsMapCpu2DramMc0Ch0) == CONFIG_MRC_CHANNEL_WIDTH / 8,
183 "Incorrect DQS UPD size!");
184
185 mem_init_dq_dqs_upds(dqs_upds, mb_cfg->dqs_map, upd_size, data, auto_detect);
186 }
187
188 void memcfg_init(FSPM_UPD *memupd, const struct mb_cfg *mb_cfg,
189 const struct mem_spd *spd_info, bool half_populated)
190 {
191 struct mem_channel_data data;
192 bool dq_dqs_auto_detect = false;
193 bool expand_channels = false;
194 FSP_M_CONFIG *mem_cfg = &memupd->FspmConfig;
195
196 mem_cfg->ECT = mb_cfg->ect;
197 mem_cfg->UserBd = mb_cfg->UserBd;
198 set_rcomp_config(mem_cfg, mb_cfg);
199
200 switch (mb_cfg->type) {
201 case MEM_TYPE_DDR5:
202 meminit_ddr(mem_cfg, &mb_cfg->ddr_config);
203 dq_dqs_auto_detect = true;
204 expand_channels = true;
205 break;
206 case MEM_TYPE_LP5X:
207 meminit_lp5x(mem_cfg, &mb_cfg->lp5x_config);
208 break;
209 default:
210 die("Unsupported memory type(%d)\n", mb_cfg->type);
211 }
212
213 mem_populate_channel_data(memupd, &soc_mem_cfg[mb_cfg->type], spd_info, half_populated,
214 &data);
215 mem_init_spd_upds(mem_cfg, &data, expand_channels);
216 mem_init_dq_upds(mem_cfg, &data, mb_cfg, dq_dqs_auto_detect);
217 mem_init_dqs_upds(mem_cfg, &data, mb_cfg, dq_dqs_auto_detect);
218 }