Debian Codegraph @

src/lib/spd_cache.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <assert.h>
4 #include <console/console.h>
5 #include <crc_byte.h>
6 #include <device/dram/ddr3.h>
7 #include <device/dram/ddr4.h>
8 #include <fmap.h>
9 #include <spd_cache.h>
10 #include <spd.h>
11 #include <spd_bin.h>
12 #include <string.h>
13
14 /*
15 * SPD_CACHE layout
16 * +==========+ offset 0x00
17 * |DIMM 1 SPD| SPD data length is CONFIG_DIMM_SPD_SIZE.
18 * +----------+ offset CONFIG_DIMM_SPD_SIZE * 1
19 * |DIMM 2 SPD|
20 * +----------+ offset CONFIG_DIMM_SPD_SIZE * 2
21 * ...
22 * +----------+ offset CONFIG_DIMM_SPD_SIZE * (N -1)
23 * |DIMM N SPD| N = CONFIG_DIMM_MAX
24 * +----------+ offset CONFIG_DIMM_SPD_SIZE * CONFIG_DIMM_MAX
25 * | CRC 16 | Use to verify the data correctness.
26 * +==========+
27 *
28 * The size of the RW_SPD_CACHE needs to be aligned with 4KiB.
29 */
30
31 /*
32 * Use to update SPD cache.
33 * *blk : the new SPD data will be stash into the cache.
34 *
35 * return CB_SUCCESS , update SPD cache successfully.
36 * return CB_ERR , update SPD cache unsuccessfully and the cache is invalid
37 */
38 enum cb_err update_spd_cache(struct spd_block *blk)
39 {
40 struct region_device rdev;
41 uint16_t data_crc = 0;
42 int i, j;
43
44 assert(blk->len <= SC_SPD_LEN);
45
46 if (fmap_locate_area_as_rdev_rw(SPD_CACHE_FMAP_NAME, &rdev)) {
47 printk(BIOS_ERR, "SPD_CACHE: Cannot access %s region\n", SPD_CACHE_FMAP_NAME);
48 return CB_ERR;
49 }
50
51 /* Erase whole area, it's for align with 4KiB which is the size of SPI rom sector. */
52 if (rdev_eraseat(&rdev, 0, region_device_sz(&rdev)) < 0) {
53 printk(BIOS_ERR, "SPD_CACHE: Cannot erase %s region\n", SPD_CACHE_FMAP_NAME);
54 return CB_ERR;
55 }
56
57 /* Write SPD data */
58 for (i = 0; i < SC_SPD_NUMS; i++) {
59 if (blk->spd_array[i] == NULL) {
60 /* If DIMM is not present, we calculate the CRC with 0xff. */
61 for (j = 0; j < SC_SPD_LEN; j++)
62 data_crc = crc16_byte(data_crc, 0xff);
63 } else {
64 if (rdev_writeat(&rdev, blk->spd_array[i], SC_SPD_OFFSET(i), blk->len)
65 < 0) {
66 printk(BIOS_ERR, "SPD_CACHE: Cannot write SPD data at %d\n",
67 SC_SPD_OFFSET(i));
68 return CB_ERR;
69 }
70
71 for (j = 0; j < blk->len; j++)
72 data_crc = crc16_byte(data_crc, blk->spd_array[i][j]);
73
74 /* If the blk->len < SC_SPD_LEN, we calculate the CRC with 0xff. */
75 if (blk->len < SC_SPD_LEN)
76 for (j = 0; j < (SC_SPD_LEN - (blk->len)); j++)
77 data_crc = crc16_byte(data_crc, 0xff);
78 }
79 }
80
81 /* Write the crc16 */
82 /* It must be the last step to ensure that the data is written correctly */
83 if (rdev_writeat(&rdev, &data_crc, SC_CRC_OFFSET, SC_CRC_LEN) < 0) {
84 printk(BIOS_ERR, "SPD_CACHE: Cannot write crc at 0x%04x\n", SC_CRC_OFFSET);
85 return CB_ERR;
86 }
87 return CB_SUCCESS;
88 }
89
90 /*
91 * Locate the RW_SPD_CACHE area in the fmap and read SPD_CACHE data.
92 * return CB_SUCCESS ,if the SPD_CACHE data is ready and the pointer return at *spd_cache.
93 * return CB_ERR ,if it cannot locate RW_SPD_CACHE area in the fmap or data cannot be read.
94 */
95 enum cb_err load_spd_cache(uint8_t **spd_cache, size_t *spd_cache_sz)
96 {
97 struct region_device rdev;
98
99 if (fmap_locate_area_as_rdev(SPD_CACHE_FMAP_NAME, &rdev) < 0) {
100 printk(BIOS_ERR, "SPD_CACHE: Cannot find %s region\n", SPD_CACHE_FMAP_NAME);
101 return CB_ERR;
102 }
103
104 /* Assume boot device is memory mapped. */
105 assert(CONFIG(BOOT_DEVICE_MEMORY_MAPPED));
106 *spd_cache = rdev_mmap_full(&rdev);
107
108 if (*spd_cache == NULL)
109 return CB_ERR;
110
111 *spd_cache_sz = region_device_sz(&rdev);
112
113 /* SPD cache found */
114 printk(BIOS_INFO, "SPD_CACHE: cache found, size 0x%zx\n", *spd_cache_sz);
115
116 return CB_SUCCESS;
117 }
118
119 /* Use to verify the cache data is valid. */
120 bool spd_cache_is_valid(uint8_t *spd_cache, size_t spd_cache_sz)
121 {
122 uint16_t data_crc = 0;
123 int i;
124
125 if (spd_cache_sz < SC_SPD_TOTAL_LEN + SC_CRC_LEN)
126 return false;
127
128 /* Check the spd_cache crc */
129 for (i = 0; i < SC_SPD_TOTAL_LEN; i++)
130 data_crc = crc16_byte(data_crc, *(spd_cache + i));
131
132 return *(uint16_t *)(spd_cache + SC_CRC_OFFSET) == data_crc;
133 }
134
135 /*
136 * Check if the DIMM is preset in cache.
137 * return true , DIMM is present.
138 * return false, DIMM is not present.
139 */
140 static bool get_cached_dimm_present(uint8_t *spd_cache, uint8_t idx)
141 {
142 if (*(uint16_t *)(spd_cache + SC_SPD_OFFSET(idx)) == 0xffff)
143 return false;
144 else
145 return true;
146 }
147
148 /*
149 * Use to check if the SODIMM is changed.
150 * spd_cache : it's a valid SPD cache.
151 * blk : it must include the smbus addresses of SODIMM.
152 */
153 bool check_if_dimm_changed(u8 *spd_cache, struct spd_block *blk)
154 {
155 int i;
156 u32 sn;
157 bool dimm_present_in_cache;
158 bool dimm_changed = false;
159 /* Check if the dimm is the same with last system boot. */
160 for (i = 0; i < SC_SPD_NUMS && !dimm_changed; i++) {
161 if (blk->addr_map[i] == 0) {
162 printk(BIOS_NOTICE, "SPD_CACHE: DIMM%d does not exist\n", i);
163 continue;
164 }
165 /* Return true if any error happened here. */
166 if (get_spd_sn(blk->addr_map[i], &sn) == CB_ERR)
167 return true;
168 dimm_present_in_cache = get_cached_dimm_present(spd_cache, i);
169 /* Dimm is not present now. */
170 if (sn == 0xffffffff) {
171 if (!dimm_present_in_cache)
172 printk(BIOS_NOTICE, "SPD_CACHE: DIMM%d is not present\n", i);
173 else {
174 printk(BIOS_NOTICE, "SPD_CACHE: DIMM%d lost\n", i);
175 dimm_changed = true;
176 }
177 } else { /* Dimm is present now. */
178 if (dimm_present_in_cache) {
179 if (memcmp(&sn, spd_cache + SC_SPD_OFFSET(i) + DDR4_SPD_SN_OFF,
180 SPD_SN_LEN) == 0)
181 printk(BIOS_NOTICE, "SPD_CACHE: DIMM%d is the same\n",
182 i);
183 else {
184 printk(BIOS_NOTICE, "SPD_CACHE: DIMM%d is new one\n",
185 i);
186 dimm_changed = true;
187 }
188 } else {
189 printk(BIOS_NOTICE, "SPD_CACHE: DIMM%d is new one\n", i);
190 dimm_changed = true;
191 }
192 }
193 }
194 return dimm_changed;
195 }
196
197 /* Use to fill the struct spd_block with cache data.*/
198 enum cb_err spd_fill_from_cache(uint8_t *spd_cache, struct spd_block *blk)
199 {
200 int i;
201 u8 dram_type;
202
203 /* Find the first present SPD */
204 for (i = 0; i < SC_SPD_NUMS; i++)
205 if (get_cached_dimm_present(spd_cache, i))
206 break;
207
208 if (i == SC_SPD_NUMS) {
209 printk(BIOS_ERR, "SPD_CACHE: No DIMM is present.\n");
210 return CB_ERR;
211 }
212
213 dram_type = *(spd_cache + SC_SPD_OFFSET(i) + SPD_MEMORY_TYPE);
214
215 if (dram_type == SPD_MEMORY_TYPE_DDR5_SDRAM)
216 blk->len = CONFIG_DIMM_SPD_SIZE;
217 else if (dram_type == SPD_MEMORY_TYPE_DDR4_SDRAM)
218 blk->len = SPD_SIZE_MAX_DDR4;
219 else
220 blk->len = SPD_SIZE_MAX_DDR3;
221
222 for (i = 0; i < SC_SPD_NUMS; i++)
223 if (get_cached_dimm_present(spd_cache, i))
224 blk->spd_array[i] = spd_cache + SC_SPD_OFFSET(i);
225 else
226 blk->spd_array[i] = NULL;
227
228 return CB_SUCCESS;
229 }