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