| 1 |
|
| 2 |
|
| 3 |
#include <console/console.h> |
| 4 |
#include <intelblocks/meminit.h> |
| 5 |
#include <commonlib/region.h> |
| 6 |
#include <spd_bin.h> |
| 7 |
#include <spd_cache.h> |
| 8 |
#include <string.h> |
| 9 |
#include <types.h> |
| 10 |
|
| 11 |
_Static_assert(CONFIG_MRC_CHANNEL_WIDTH > 0, "MRC channel width must be >0!"); |
| 12 |
_Static_assert(CONFIG_DATA_BUS_WIDTH > 0, "Data bus width must be >0!"); |
| 13 |
_Static_assert(CONFIG_DIMMS_PER_CHANNEL > 0, "DIMMS per channel must be >0!"); |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
static enum channel_population populated_mask_to_flag(uint32_t pop_mask, size_t max_channels) |
| 20 |
{ |
| 21 |
uint32_t full_mask = BIT(max_channels) - 1; |
| 22 |
uint32_t bottom_mask = BIT(max_channels / 2) - 1; |
| 23 |
uint32_t top_mask = ~bottom_mask & full_mask; |
| 24 |
|
| 25 |
if (pop_mask == full_mask) |
| 26 |
return FULLY_POPULATED; |
| 27 |
else if (pop_mask == bottom_mask) |
| 28 |
return BOTTOM_HALF_POPULATED; |
| 29 |
else if (pop_mask == top_mask) |
| 30 |
return TOP_HALF_POPULATED; |
| 31 |
else if (pop_mask == 0) |
| 32 |
return NO_CHANNEL_POPULATED; |
| 33 |
|
| 34 |
die("Unsupported channel population mask(0x%x)\n", pop_mask); |
| 35 |
} |
| 36 |
|
| 37 |
static void read_spd_md(const struct soc_mem_cfg *soc_mem_cfg, const struct mem_spd *info, |
| 38 |
bool half_populated, struct mem_channel_data *channel_data, |
| 39 |
size_t *spd_len) |
| 40 |
{ |
| 41 |
size_t ch; |
| 42 |
size_t num_phys_ch = soc_mem_cfg->num_phys_channels; |
| 43 |
uintptr_t spd_data; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
uint32_t pop_mask = BIT(num_phys_ch) - 1; |
| 51 |
|
| 52 |
if (!(info->topo & MEM_TOPO_MEMORY_DOWN)) |
| 53 |
return; |
| 54 |
|
| 55 |
if (info->topo == MEM_TOPO_MIXED) |
| 56 |
pop_mask &= soc_mem_cfg->md_phy_masks.mixed_topo; |
| 57 |
|
| 58 |
if (half_populated) |
| 59 |
pop_mask &= soc_mem_cfg->md_phy_masks.half_channel; |
| 60 |
|
| 61 |
if (pop_mask == 0) |
| 62 |
die("Memory technology does not support the selected configuration!\n"); |
| 63 |
if (!(info->spd_data.in_mem)) { |
| 64 |
|
| 65 |
printk(BIOS_DEBUG, "SPD index = %zu\n", info->cbfs_index); |
| 66 |
|
| 67 |
|
| 68 |
_Static_assert(CONFIG(BOOT_DEVICE_MEMORY_MAPPED), |
| 69 |
"Function assumes memory-mapped boot media"); |
| 70 |
|
| 71 |
*spd_len = CONFIG_DIMM_SPD_SIZE; |
| 72 |
spd_data = spd_cbfs_map(info->cbfs_index); |
| 73 |
if (!spd_data) |
| 74 |
die("SPD not found in CBFS or incorrect index!\n"); |
| 75 |
} else { |
| 76 |
*spd_len = info->spd_data.len; |
| 77 |
spd_data = info->spd_data.ptr; |
| 78 |
if (!spd_data) |
| 79 |
die("SPD data in memory expected but no buffer provided!\n"); |
| 80 |
} |
| 81 |
|
| 82 |
print_spd_info((uint8_t *)spd_data); |
| 83 |
|
| 84 |
for (ch = 0; ch < num_phys_ch; ch++) { |
| 85 |
if (!(pop_mask & BIT(ch))) |
| 86 |
continue; |
| 87 |
|
| 88 |
int mrc_ch = soc_mem_cfg->phys_to_mrc_map[ch]; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
channel_data->spd[mrc_ch][0] = spd_data; |
| 97 |
} |
| 98 |
|
| 99 |
channel_data->ch_population_flags |= populated_mask_to_flag(pop_mask, num_phys_ch); |
| 100 |
} |
| 101 |
|
| 102 |
#define CH_DIMM_OFFSET(ch, dimm) ((ch) * CONFIG_DIMMS_PER_CHANNEL + (dimm)) |
| 103 |
|
| 104 |
static bool read_spd_dimm(FSPM_UPD *memupd, const struct soc_mem_cfg *soc_mem_cfg, |
| 105 |
const struct mem_spd *info, bool half_populated, |
| 106 |
struct mem_channel_data *channel_data, |
| 107 |
size_t *spd_len) |
| 108 |
{ |
| 109 |
size_t ch, dimm; |
| 110 |
struct spd_block blk = { 0 }; |
| 111 |
size_t num_phys_ch = soc_mem_cfg->num_phys_channels; |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
uint32_t pop_mask = 0; |
| 119 |
|
| 120 |
if (!(info->topo & MEM_TOPO_DIMM_MODULE)) |
| 121 |
return false; |
| 122 |
|
| 123 |
for (ch = 0; ch < num_phys_ch; ch++) { |
| 124 |
for (dimm = 0; dimm < CONFIG_DIMMS_PER_CHANNEL; dimm++) { |
| 125 |
if (CH_DIMM_OFFSET(ch, dimm) < ARRAY_SIZE(blk.addr_map)) |
| 126 |
blk.addr_map[CH_DIMM_OFFSET(ch, dimm)] = |
| 127 |
info->smbus[ch].addr_dimm[dimm]; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
if (CONFIG(SPD_CACHE_ENABLE)) { |
| 132 |
uint8_t *spd_cache; |
| 133 |
size_t spd_cache_sz; |
| 134 |
bool need_update_cache = false; |
| 135 |
bool dimm_changed = true; |
| 136 |
|
| 137 |
|
| 138 |
if (load_spd_cache(&spd_cache, &spd_cache_sz) == CB_SUCCESS) { |
| 139 |
if (!spd_cache_is_valid(spd_cache, spd_cache_sz)) { |
| 140 |
printk(BIOS_WARNING, "Invalid SPD cache\n"); |
| 141 |
} else { |
| 142 |
dimm_changed = check_if_dimm_changed(spd_cache, &blk); |
| 143 |
if (dimm_changed && memupd->FspmArchUpd.NvsBufferPtr != 0) { |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
printk(BIOS_INFO, "DIMM change, invalidate cache.\n"); |
| 149 |
memupd->FspmArchUpd.NvsBufferPtr = 0; |
| 150 |
memupd->FspmArchUpd.BootMode = |
| 151 |
FSP_BOOT_WITH_FULL_CONFIGURATION; |
| 152 |
} |
| 153 |
} |
| 154 |
need_update_cache = true; |
| 155 |
} |
| 156 |
|
| 157 |
if (!dimm_changed) { |
| 158 |
printk(BIOS_INFO, "Use the SPD cache data\n"); |
| 159 |
spd_fill_from_cache(spd_cache, &blk); |
| 160 |
} else { |
| 161 |
|
| 162 |
get_spd_smbus(&blk); |
| 163 |
|
| 164 |
if (need_update_cache && |
| 165 |
update_spd_cache(&blk) == CB_ERR) |
| 166 |
printk(BIOS_ERR, "update SPD cache failed\n"); |
| 167 |
} |
| 168 |
} else { |
| 169 |
get_spd_smbus(&blk); |
| 170 |
} |
| 171 |
|
| 172 |
*spd_len = blk.len; |
| 173 |
|
| 174 |
for (ch = 0; ch < num_phys_ch; ch++) { |
| 175 |
size_t mrc_ch = soc_mem_cfg->phys_to_mrc_map[ch]; |
| 176 |
|
| 177 |
for (dimm = 0; dimm < CONFIG_DIMMS_PER_CHANNEL; dimm++) { |
| 178 |
if (!(CH_DIMM_OFFSET(ch, dimm) < ARRAY_SIZE(blk.spd_array))) |
| 179 |
continue; |
| 180 |
uint8_t *spd_data = blk.spd_array[CH_DIMM_OFFSET(ch, dimm)]; |
| 181 |
if (spd_data == NULL) |
| 182 |
continue; |
| 183 |
|
| 184 |
print_spd_info(spd_data); |
| 185 |
|
| 186 |
channel_data->spd[mrc_ch][dimm] = (uintptr_t)(void *)spd_data; |
| 187 |
pop_mask |= BIT(ch); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
channel_data->ch_population_flags |= populated_mask_to_flag(pop_mask, num_phys_ch); |
| 192 |
|
| 193 |
return pop_mask != 0; |
| 194 |
} |
| 195 |
|
| 196 |
void mem_populate_channel_data(FSPM_UPD *memupd, const struct soc_mem_cfg *soc_mem_cfg, |
| 197 |
const struct mem_spd *spd_info, |
| 198 |
bool half_populated, |
| 199 |
struct mem_channel_data *data) |
| 200 |
{ |
| 201 |
size_t spd_md_len = 0, spd_dimm_len = 0; |
| 202 |
bool have_dimms; |
| 203 |
|
| 204 |
memset(data, 0, sizeof(*data)); |
| 205 |
|
| 206 |
read_spd_md(soc_mem_cfg, spd_info, half_populated, data, &spd_md_len); |
| 207 |
have_dimms = read_spd_dimm(memupd, soc_mem_cfg, spd_info, half_populated, data, |
| 208 |
&spd_dimm_len); |
| 209 |
|
| 210 |
if (data->ch_population_flags == NO_CHANNEL_POPULATED) |
| 211 |
die("No channels are populated. Incorrect memory configuration!\n"); |
| 212 |
|
| 213 |
if (spd_info->topo == MEM_TOPO_MEMORY_DOWN) { |
| 214 |
data->spd_len = spd_md_len; |
| 215 |
} else if (spd_info->topo == MEM_TOPO_DIMM_MODULE) { |
| 216 |
data->spd_len = spd_dimm_len; |
| 217 |
} else { |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
if (have_dimms && spd_md_len != spd_dimm_len) |
| 223 |
die("Length of SPD does not match for mixed topology!\n"); |
| 224 |
|
| 225 |
|
| 226 |
data->spd_len = spd_md_len; |
| 227 |
} |
| 228 |
} |