Debian Codegraph @

src/lib/spd_bin.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <cbfs.h>
4 #include <console/console.h>
5 #include <device/dram/ddr3.h>
6 #include <device/dram/ddr4.h>
7 #include <memory_info.h>
8 #include <spd.h>
9 #include <spd_bin.h>
10 #include <string.h>
11
12 void dump_spd_info(struct spd_block *blk)
13 {
14 u8 i;
15
16 for (i = 0; i < CONFIG_DIMM_MAX; i++)
17 if (blk->spd_array[i] != NULL && blk->spd_array[i][0] != 0) {
18 printk(BIOS_DEBUG, "SPD @ 0x%02X\n", blk->addr_map[i]);
19 print_spd_info(blk->spd_array[i]);
20 }
21 }
22
23 const char * __weak mainboard_get_dram_part_num(void)
24 {
25 /* Default weak implementation, no need to override part number. */
26 return NULL;
27 }
28
29 static bool use_ddr4_params(int dram_type)
30 {
31 switch (dram_type) {
32 case SPD_MEMORY_TYPE_SDRAM_DDR3:
33 case SPD_MEMORY_TYPE_LPDDR3_INTEL:
34 return false;
35 /* Below DDR type share the same attributes */
36 case SPD_MEMORY_TYPE_LPDDR3_SDRAM:
37 case SPD_MEMORY_TYPE_DDR4_SDRAM:
38 case SPD_MEMORY_TYPE_DDR5_SDRAM:
39 case SPD_MEMORY_TYPE_LPDDR5_SDRAM:
40 case SPD_MEMORY_TYPE_LPDDR5X_SDRAM:
41 case SPD_MEMORY_TYPE_LPDDR4_SDRAM:
42 case SPD_MEMORY_TYPE_LPDDR4X_SDRAM:
43 return true;
44 default:
45 printk(BIOS_NOTICE, "Defaulting to using DDR4 params. Please add dram_type check for %d to %s\n",
46 dram_type, __func__);
47 return true;
48 }
49 }
50
51 static const char *spd_get_module_type_string(int dram_type)
52 {
53 switch (dram_type) {
54 case SPD_MEMORY_TYPE_SDRAM_DDR3:
55 return "DDR3";
56 case SPD_MEMORY_TYPE_LPDDR3_INTEL:
57 case SPD_MEMORY_TYPE_LPDDR3_SDRAM:
58 return "LPDDR3";
59 case SPD_MEMORY_TYPE_DDR4_SDRAM:
60 return "DDR4";
61 case SPD_MEMORY_TYPE_LPDDR4_SDRAM:
62 return "LPDDR4";
63 case SPD_MEMORY_TYPE_LPDDR4X_SDRAM:
64 return "LPDDR4X";
65 case SPD_MEMORY_TYPE_DDR5_SDRAM:
66 return "DDR5";
67 case SPD_MEMORY_TYPE_LPDDR5_SDRAM:
68 return "LPDDR5";
69 case SPD_MEMORY_TYPE_LPDDR5X_SDRAM:
70 return "LPDDR5X";
71 }
72 return "UNKNOWN";
73 }
74
75 static int spd_get_banks(const uint8_t spd[], int dram_type)
76 {
77 static const int ddr3_banks[4] = { 8, 16, 32, 64 };
78 static const int ddr4_banks[10] = { 4, 8, -1, -1, 8, 16, -1, -1, 16, 32 };
79 int index = (spd[SPD_DENSITY_BANKS] >> 4) & 0xf;
80
81 if (use_ddr4_params(dram_type)) {
82 if (index >= ARRAY_SIZE(ddr4_banks))
83 return -1;
84 return ddr4_banks[index];
85 } else {
86 if (index >= ARRAY_SIZE(ddr3_banks))
87 return -1;
88 return ddr3_banks[index];
89 }
90 }
91
92 static int spd_get_capmb(const uint8_t spd[])
93 {
94 static const int spd_capmb[13] = { 1, 2, 4, 8, 16, 32, 64,
95 128, 48, 96, 12, 24, 72 };
96 int index = spd[SPD_DENSITY_BANKS] & 0xf;
97 if (index >= ARRAY_SIZE(spd_capmb))
98 return -1;
99 return spd_capmb[index] * 256;
100 }
101
102 static int spd_get_rows(const uint8_t spd[])
103 {
104 static const int spd_rows[7] = { 12, 13, 14, 15, 16, 17, 18 };
105 int index = (spd[SPD_ADDRESSING] >> 3) & 7;
106 if (index >= ARRAY_SIZE(spd_rows))
107 return -1;
108 return spd_rows[index];
109 }
110
111 static int spd_get_cols(const uint8_t spd[])
112 {
113 static const int spd_cols[4] = { 9, 10, 11, 12 };
114 int index = spd[SPD_ADDRESSING] & 7;
115 if (index >= ARRAY_SIZE(spd_cols))
116 return -1;
117 return spd_cols[index];
118 }
119
120 static int spd_get_ranks(const uint8_t spd[], int dram_type)
121 {
122 static const int spd_ranks[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
123 int organ_offset = use_ddr4_params(dram_type) ? DDR4_ORGANIZATION
124 : DDR3_ORGANIZATION;
125 int index = (spd[organ_offset] >> 3) & 7;
126 if (index >= ARRAY_SIZE(spd_ranks))
127 return -1;
128 return spd_ranks[index];
129 }
130
131 static int spd_get_devw(const uint8_t spd[], int dram_type)
132 {
133 static const int spd_devw[4] = { 4, 8, 16, 32 };
134 int organ_offset = use_ddr4_params(dram_type) ? DDR4_ORGANIZATION
135 : DDR3_ORGANIZATION;
136 int index = spd[organ_offset] & 7;
137 if (index >= ARRAY_SIZE(spd_devw))
138 return -1;
139 return spd_devw[index];
140 }
141
142 static int spd_get_busw(const uint8_t spd[], int dram_type)
143 {
144 static const int spd_busw[4] = { 8, 16, 32, 64 };
145 int busw_offset = use_ddr4_params(dram_type) ? DDR4_BUS_DEV_WIDTH
146 : DDR3_BUS_DEV_WIDTH;
147 int index = spd[busw_offset] & 7;
148 if (index >= ARRAY_SIZE(spd_busw))
149 return -1;
150 return spd_busw[index];
151 }
152
153 static void spd_get_name(const uint8_t spd[], int type, const char **spd_name, size_t *len)
154 {
155 *spd_name = mainboard_get_dram_part_num();
156 if (*spd_name != NULL) {
157 *len = strlen(*spd_name);
158 return;
159 }
160
161 switch (type) {
162 case SPD_MEMORY_TYPE_SDRAM_DDR3:
163 case SPD_MEMORY_TYPE_LPDDR3_INTEL:
164 *spd_name = (const char *) &spd[SPD_DDR3_PART_NUM];
165 *len = SPD_DDR3_PART_LEN;
166 break;
167 /* LPDDR3, LPDDR4 and DDR4 have same part number offset and length */
168 case SPD_MEMORY_TYPE_LPDDR3_SDRAM:
169 case SPD_MEMORY_TYPE_DDR4_SDRAM:
170 case SPD_MEMORY_TYPE_DDR5_SDRAM:
171 case SPD_MEMORY_TYPE_LPDDR5_SDRAM:
172 case SPD_MEMORY_TYPE_LPDDR4_SDRAM:
173 case SPD_MEMORY_TYPE_LPDDR4X_SDRAM:
174 if (spd[SPD_DDR4_PART_OFF]) {
175 *spd_name = (const char *) &spd[SPD_DDR4_PART_OFF];
176 *len = SPD_DDR4_PART_LEN;
177 }
178 break;
179 default:
180 *len = 0;
181 break;
182 }
183 }
184
185 void print_spd_info(uint8_t spd[])
186 {
187 const char *nameptr = NULL;
188 size_t len;
189 int type = spd[SPD_MEMORY_TYPE];
190 int banks = spd_get_banks(spd, type);
191 int capmb = spd_get_capmb(spd);
192 int rows = spd_get_rows(spd);
193 int cols = spd_get_cols(spd);
194 int ranks = spd_get_ranks(spd, type);
195 int devw = spd_get_devw(spd, type);
196 int busw = spd_get_busw(spd, type);
197
198 /* Module type */
199 printk(BIOS_INFO, "SPD: module type is %s\n",
200 spd_get_module_type_string(type));
201 /* Module Part Number */
202 spd_get_name(spd, type, &nameptr, &len);
203 if (nameptr)
204 printk(BIOS_INFO, "SPD: module part number is %.*s\n", (int) len, nameptr);
205
206 printk(BIOS_INFO,
207 "SPD: banks %d, ranks %d, rows %d, columns %d, density %d Mb\n",
208 banks, ranks, rows, cols, capmb);
209 printk(BIOS_INFO, "SPD: device width %d bits, bus width %d bits\n",
210 devw, busw);
211
212 if (capmb > 0 && busw > 0 && devw > 0 && ranks > 0) {
213 /* SIZE = DENSITY / 8 * BUS_WIDTH / SDRAM_WIDTH * RANKS */
214 printk(BIOS_INFO, "SPD: module size is %u MB (per channel)\n",
215 capmb / 8 * busw / devw * ranks);
216 }
217 }
218
219 uintptr_t spd_cbfs_map(u8 spd_index)
220 {
221 enum cbfs_type cbfs_type = CBFS_TYPE_SPD;
222 size_t size;
223
224 void *map = cbfs_type_map("spd.bin", &size, &cbfs_type);
225 if (!map || size < (spd_index + 1) * CONFIG_DIMM_SPD_SIZE)
226 return 0;
227
228 return (uintptr_t)map + spd_index * CONFIG_DIMM_SPD_SIZE;
229 }
230
231 #if CONFIG_DIMM_SPD_SIZE == 128
232 int read_ddr3_spd_from_cbfs(u8 *buf, int idx)
233 {
234 const int SPD_CRC_HI = 127;
235 const int SPD_CRC_LO = 126;
236
237 char *spd_file;
238 size_t spd_file_len = 0;
239 size_t min_len = (idx + 1) * CONFIG_DIMM_SPD_SIZE;
240
241 spd_file = cbfs_map("spd.bin", &spd_file_len);
242 if (!spd_file)
243 printk(BIOS_EMERG, "file [spd.bin] not found in CBFS");
244 if (spd_file_len < min_len)
245 printk(BIOS_EMERG, "Missing SPD data.");
246 if (!spd_file || spd_file_len < min_len)
247 return -1;
248
249 memcpy(buf, spd_file + (idx * CONFIG_DIMM_SPD_SIZE),
250 CONFIG_DIMM_SPD_SIZE);
251 cbfs_unmap(spd_file);
252
253 u16 crc = spd_ddr3_calc_crc(buf, CONFIG_DIMM_SPD_SIZE);
254
255 if (((buf[SPD_CRC_LO] == 0) && (buf[SPD_CRC_HI] == 0))
256 || (buf[SPD_CRC_LO] != (crc & 0xff))
257 || (buf[SPD_CRC_HI] != (crc >> 8))) {
258 printk(BIOS_WARNING,
259 "SPD CRC %02x%02x is invalid, should be %04x\n",
260 buf[SPD_CRC_HI], buf[SPD_CRC_LO], crc);
261 buf[SPD_CRC_LO] = crc & 0xff;
262 buf[SPD_CRC_HI] = crc >> 8;
263 u16 i;
264 printk(BIOS_WARNING, "\nDisplay the SPD");
265 for (i = 0; i < CONFIG_DIMM_SPD_SIZE; i++) {
266 if ((i % 16) == 0x00)
267 printk(BIOS_WARNING, "\n%02x: ", i);
268 printk(BIOS_WARNING, "%02x ", buf[i]);
269 }
270 printk(BIOS_WARNING, "\n");
271 }
272 return 0;
273 }
274 #endif