| 1 |
|
| 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 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 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 |
|
| 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 |
|
| 58 |
for (i = 0; i < SC_SPD_NUMS; i++) { |
| 59 |
if (blk->spd_array[i] == NULL) { |
| 60 |
|
| 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 |
|
| 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 |
|
| 82 |
|
| 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 |
|
| 92 |
|
| 93 |
|
| 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 |
|
| 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 |
|
| 114 |
printk(BIOS_INFO, "SPD_CACHE: cache found, size 0x%zx\n", *spd_cache_sz); |
| 115 |
|
| 116 |
return CB_SUCCESS; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 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 |
|
| 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 |
|
| 137 |
|
| 138 |
|
| 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 |
|
| 150 |
|
| 151 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 { |
| 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 |
|
| 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 |
|
| 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 |
} |