blob: 07f9decb7442981c4273469506d6712609a29e68 [file] [log] [blame]
Andrey Petrov3f85edb2019-08-01 14:18:06 -07001/*
2 * This file is part of the coreboot project.
3 *
Andrey Petrov3f85edb2019-08-01 14:18:06 -07004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#include <console/console.h>
15#include <cbmem.h>
16#include <device/device.h>
17#include <device/dram/ddr4.h>
18#include <string.h>
19#include <memory_info.h>
20#include <smbios.h>
21#include <types.h>
22
23typedef enum {
24 BLOCK_0, /* Base Configuration and DRAM Parameters */
25 BLOCK_1,
26 BLOCK_1_L, /* Standard Module Parameters */
27 BLOCK_1_H, /* Hybrid Module Parameters */
28 BLOCK_2,
29 BLOCK_2_L, /* Hybrid Module Extended Function Parameters */
30 BLOCK_2_H, /* Manufacturing Information */
31 BLOCK_3 /* End user programmable */
32} spd_block_type;
33
34typedef struct {
35 spd_block_type type;
36 uint16_t start; /* starting offset from beginning of the spd */
37 uint16_t len; /* size of the block */
38 uint16_t crc_start; /* offset from start of crc bytes, 0 if none */
39} spd_block;
40
41/* 'SPD contents architecture' as per datasheet */
42const spd_block spd_blocks[] = {
43 {.type = BLOCK_0, 0, 128, 126}, {.type = BLOCK_1, 128, 128, 126},
44 {.type = BLOCK_1_L, 128, 64, 0}, {.type = BLOCK_1_H, 192, 64, 0},
45 {.type = BLOCK_2_L, 256, 64, 62}, {.type = BLOCK_2_H, 320, 64, 0},
46 {.type = BLOCK_3, 384, 128, 0} };
47
48static bool verify_block(const spd_block *block, spd_raw_data spd)
49{
50 uint16_t crc, spd_crc;
51
52 spd_crc = (spd[block->start + block->crc_start + 1] << 8)
53 | spd[block->start + block->crc_start];
54 crc = ddr_crc16(&spd[block->start], block->len - 2);
55
56 return spd_crc == crc;
57}
58
59/* Check if given block is 'reserved' for a given module type */
60static bool block_exists(spd_block_type type, u8 dimm_type)
61{
62 bool is_hybrid;
63
64 switch (type) {
65 case BLOCK_0: /* fall-through */
66 case BLOCK_1: /* fall-through */
67 case BLOCK_1_L: /* fall-through */
68 case BLOCK_1_H: /* fall-through */
69 case BLOCK_2_H: /* fall-through */
70 case BLOCK_3: /* fall-through */
71 return true;
72 case BLOCK_2_L:
73 is_hybrid = (dimm_type >> 4) & ((1 << 3) - 1);
74 if (is_hybrid)
75 return true;
76 return false;
77 default: /* fall-through */
78 return false;
79 }
80}
81
82
83/**
84 * \brief Decode the raw SPD data
85 *
86 * Decodes a raw SPD data from a DDR4 DIMM, and organizes it into a
87 * @ref dimm_attr structure. The SPD data must first be read in a contiguous
88 * array, and passed to this function.
89 *
90 * @param dimm pointer to @ref dimm_attr structure where the decoded data is to
91 * be stored
92 * @param spd array of raw data previously read from the SPD.
93 *
94 * @return @ref spd_status enumerator
95 * SPD_STATUS_OK -- decoding was successful
96 * SPD_STATUS_INVALID -- invalid SPD or not a DDR4 SPD
97 * SPD_STATUS_CRC_ERROR -- checksum mismatch
98 */
99int spd_decode_ddr4(dimm_attr *dimm, spd_raw_data spd)
100{
101 u8 reg8;
102 u8 bus_width, sdram_width;
103 u16 cap_per_die_mbit;
104 u16 spd_bytes_total, spd_bytes_used;
105 const uint16_t spd_bytes_used_table[] = {0, 128, 256, 384, 512};
106
107 /* Make sure that the SPD dump is indeed from a DDR4 module */
108 if (spd[2] != SPD_MEMORY_TYPE_DDR4_SDRAM) {
109 printk(BIOS_ERR, "Not a DDR4 SPD!\n");
110 dimm->dram_type = SPD_MEMORY_TYPE_UNDEFINED;
111 return SPD_STATUS_INVALID;
112 }
113
Elyes HAOUAS2a0fbe32019-09-24 14:24:45 +0200114 spd_bytes_total = (spd[0] >> 4) & 0x7;
115 spd_bytes_used = spd[0] & 0xf;
Andrey Petrov3f85edb2019-08-01 14:18:06 -0700116
117 if (!spd_bytes_total || !spd_bytes_used) {
118 printk(BIOS_ERR, "SPD failed basic sanity checks\n");
119 return SPD_STATUS_INVALID;
120 }
121
Elyes HAOUAS2a0fbe32019-09-24 14:24:45 +0200122 if (spd_bytes_total >= 3)
123 printk(BIOS_WARNING, "SPD Bytes Total value is reserved\n");
124
Andrey Petrov3f85edb2019-08-01 14:18:06 -0700125 spd_bytes_total = 256 << (spd_bytes_total - 1);
Elyes HAOUAS2a0fbe32019-09-24 14:24:45 +0200126
127 if (spd_bytes_used > 4) {
128 printk(BIOS_ERR, "SPD Bytes Used value is reserved\n");
129 return SPD_STATUS_INVALID;
130 }
131
Andrey Petrov3f85edb2019-08-01 14:18:06 -0700132 spd_bytes_used = spd_bytes_used_table[spd_bytes_used];
133
Elyes HAOUAS2a0fbe32019-09-24 14:24:45 +0200134 if (spd_bytes_used > spd_bytes_total) {
135 printk(BIOS_ERR, "SPD Bytes Used is greater than SPD Bytes Total\n");
136 return SPD_STATUS_INVALID;
137 }
138
Andrey Petrov3f85edb2019-08-01 14:18:06 -0700139 /* Verify CRC of blocks that have them, do not step over 'used' length */
140 for (int i = 0; i < ARRAY_SIZE(spd_blocks); i++) {
141 /* this block is not checksumed */
142 if (spd_blocks[i].crc_start == 0)
143 continue;
144 /* we shouldn't have this block */
145 if (spd_blocks[i].start + spd_blocks[i].len > spd_bytes_used)
146 continue;
147 /* check if block exists in the current schema */
148 if (!block_exists(spd_blocks[i].type, spd[3]))
149 continue;
150 if (!verify_block(&spd_blocks[i], spd)) {
151 printk(BIOS_ERR, "CRC failed for block %d\n", i);
152 return SPD_STATUS_CRC_ERROR;
153 }
154 }
155
156 dimm->dram_type = SPD_MEMORY_TYPE_DDR4_SDRAM;
157 dimm->dimm_type = spd[3] & ((1 << 4) - 1);
158
159 reg8 = spd[13] & ((1 << 4) - 1);
160 dimm->bus_width = reg8;
161 bus_width = 8 << (reg8 & ((1 << 3) - 1));
162
163 reg8 = spd[12] & ((1 << 3) - 1);
164 dimm->sdram_width = reg8;
165 sdram_width = 4 << reg8;
166
167 reg8 = spd[4] & ((1 << 4) - 1);
168 dimm->cap_per_die_mbit = reg8;
169 cap_per_die_mbit = (1 << reg8) * 256;
170
171 reg8 = (spd[12] >> 3) & ((1 << 3) - 1);
172 dimm->ranks = reg8 + 1;
173
174 if (!bus_width || !sdram_width) {
175 printk(BIOS_ERR, "SPD information is invalid");
176 dimm->size_mb = 0;
177 return SPD_STATUS_INVALID;
178 }
179
180 /* seems to be only one, in mV */
181 dimm->vdd_voltage = 1200;
182
183 /* calculate size */
184 dimm->size_mb = cap_per_die_mbit / 8 * bus_width / sdram_width * dimm->ranks;
185
186 /* make sure we have the manufacturing information block */
187 if (spd_bytes_used > 320) {
188 dimm->manufacturer_id = (spd[351] << 8) | spd[350];
189 memcpy(dimm->part_number, &spd[329], SPD_DDR4_PART_LEN);
190 dimm->part_number[SPD_DDR4_PART_LEN] = 0;
191 memcpy(dimm->serial_number, &spd[325], sizeof(dimm->serial_number));
192 }
193 return SPD_STATUS_OK;
194}
195
196enum cb_err spd_add_smbios17_ddr4(const u8 channel, const u8 slot, const u16 selected_freq,
197 const dimm_attr *info)
198{
199 struct memory_info *mem_info;
200 struct dimm_info *dimm;
201
202 /*
203 * Allocate CBMEM area for DIMM information used to populate SMBIOS
204 * table 17
205 */
206 mem_info = cbmem_find(CBMEM_ID_MEMINFO);
207 if (!mem_info) {
208 mem_info = cbmem_add(CBMEM_ID_MEMINFO, sizeof(*mem_info));
209
210 printk(BIOS_DEBUG, "CBMEM entry for DIMM info: 0x%p\n", mem_info);
211 if (!mem_info)
212 return CB_ERR;
213
214 memset(mem_info, 0, sizeof(*mem_info));
215 }
216
217 if (mem_info->dimm_cnt >= ARRAY_SIZE(mem_info->dimm)) {
218 printk(BIOS_WARNING, "BUG: Too many DIMM infos for %s.\n", __func__);
219 return CB_ERR;
220 }
221
222 dimm = &mem_info->dimm[mem_info->dimm_cnt];
223 if (info->size_mb) {
224 dimm->ddr_type = MEMORY_TYPE_DDR4;
225 dimm->ddr_frequency = selected_freq;
226 dimm->dimm_size = info->size_mb;
227 dimm->channel_num = channel;
228 dimm->rank_per_dimm = info->ranks;
229 dimm->dimm_num = slot;
230 memcpy(dimm->module_part_number, info->part_number, SPD_DDR4_PART_LEN);
231 dimm->mod_id = info->manufacturer_id;
232
233 switch (info->dimm_type) {
234 case SPD_DIMM_TYPE_SO_DIMM:
235 dimm->mod_type = SPD_SODIMM;
236 break;
237 case SPD_DIMM_TYPE_72B_SO_RDIMM:
238 dimm->mod_type = SPD_72B_SO_RDIMM;
239 break;
240 case SPD_DIMM_TYPE_UDIMM:
241 dimm->mod_type = SPD_UDIMM;
242 break;
243 case SPD_DIMM_TYPE_RDIMM:
244 dimm->mod_type = SPD_RDIMM;
245 break;
246 default:
247 dimm->mod_type = SPD_UNDEFINED;
248 break;
249 }
250
251 dimm->bus_width = info->bus_width;
252 memcpy(dimm->serial, info->serial_number,
253 MIN(sizeof(dimm->serial), sizeof(info->serial_number)));
254
255 dimm->vdd_voltage = info->vdd_voltage;
256 mem_info->dimm_cnt++;
257 }
258
259 return CB_SUCCESS;
260}