blob: 28b3f37ed964eb1287fb83895f625ded83770577 [file] [log] [blame]
Angel Ponsf94ac9a2020-04-05 15:46:48 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Duncan Lauriec88c54c2014-04-30 16:36:13 -07002
Aaron Durbin31be2c92016-12-03 22:08:20 -06003#include <assert.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -07004#include <cbfs.h>
5#include <cbmem.h>
Patrick Rudolph45022ae2018-10-01 19:17:11 +02006#include <cf9_reset.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -07007#include <console/console.h>
8#include <device/pci_def.h>
Matt DeVillier9aaf59a2018-05-27 21:51:49 -05009#include <memory_info.h>
Aaron Durbindecd0622017-12-15 12:26:40 -070010#include <mrc_cache.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070011#include <string.h>
Julius Werner4ee4bd52014-10-20 13:46:39 -070012#include <soc/iomap.h>
13#include <soc/pei_data.h>
14#include <soc/pei_wrapper.h>
15#include <soc/pm.h>
Julius Werner4ee4bd52014-10-20 13:46:39 -070016#include <soc/romstage.h>
Julius Werner4ee4bd52014-10-20 13:46:39 -070017#include <soc/systemagent.h>
Angel Ponsdc600732021-06-23 13:11:30 +020018#include <timestamp.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070019
Angel Ponsdc600732021-06-23 13:11:30 +020020static void save_mrc_data(struct pei_data *pei_data)
Angel Ponsd0d528a2021-01-20 23:09:16 +010021{
22 printk(BIOS_DEBUG, "MRC data at %p %d bytes\n", pei_data->data_to_save,
23 pei_data->data_to_save_size);
24
25 if (pei_data->data_to_save != NULL && pei_data->data_to_save_size > 0)
26 mrc_cache_stash_data(MRC_TRAINING_DATA, 0,
27 pei_data->data_to_save,
28 pei_data->data_to_save_size);
29}
30
Angel Pons29a52c82020-10-13 23:32:55 +020031static const char *const ecc_decoder[] = {
32 "inactive",
33 "active on IO",
34 "disabled on IO",
35 "active",
36};
37
Duncan Lauriec88c54c2014-04-30 16:36:13 -070038/*
Angel Pons239c9662020-10-13 21:34:53 +020039 * Dump in the log memory controller configuration as read from the memory
40 * controller registers.
41 */
42static void report_memory_config(void)
43{
Angel Pons239c9662020-10-13 21:34:53 +020044 int i;
45
Angel Ponsa8753e92021-04-17 14:34:37 +020046 const u32 addr_decoder_common = mchbar_read32(MAD_CHNL);
Angel Pons239c9662020-10-13 21:34:53 +020047
48 printk(BIOS_DEBUG, "memcfg DDR3 clock %d MHz\n",
Angel Ponsa8753e92021-04-17 14:34:37 +020049 (mchbar_read32(MC_BIOS_DATA) * 13333 * 2 + 50) / 100);
Angel Pons430f1c52020-10-13 23:01:48 +020050
Angel Pons239c9662020-10-13 21:34:53 +020051 printk(BIOS_DEBUG, "memcfg channel assignment: A: %d, B % d, C % d\n",
Angel Pons430f1c52020-10-13 23:01:48 +020052 (addr_decoder_common >> 0) & 3,
Angel Pons239c9662020-10-13 21:34:53 +020053 (addr_decoder_common >> 2) & 3,
54 (addr_decoder_common >> 4) & 3);
55
Angel Pons162a7372020-10-13 23:37:07 +020056 for (i = 0; i < NUM_CHANNELS; i++) {
Angel Ponsa8753e92021-04-17 14:34:37 +020057 const u32 ch_conf = mchbar_read32(MAD_DIMM(i));
Angel Pons430f1c52020-10-13 23:01:48 +020058
59 printk(BIOS_DEBUG, "memcfg channel[%d] config (%8.8x):\n", i, ch_conf);
Angel Pons29a52c82020-10-13 23:32:55 +020060 printk(BIOS_DEBUG, " ECC %s\n", ecc_decoder[(ch_conf >> 24) & 3]);
Angel Pons239c9662020-10-13 21:34:53 +020061 printk(BIOS_DEBUG, " enhanced interleave mode %s\n",
62 ((ch_conf >> 22) & 1) ? "on" : "off");
Angel Pons430f1c52020-10-13 23:01:48 +020063
Angel Pons239c9662020-10-13 21:34:53 +020064 printk(BIOS_DEBUG, " rank interleave %s\n",
65 ((ch_conf >> 21) & 1) ? "on" : "off");
Angel Pons430f1c52020-10-13 23:01:48 +020066
Angel Pons239c9662020-10-13 21:34:53 +020067 printk(BIOS_DEBUG, " DIMMA %d MB width %s %s rank%s\n",
68 ((ch_conf >> 0) & 0xff) * 256,
69 ((ch_conf >> 19) & 1) ? "x16" : "x8 or x32",
70 ((ch_conf >> 17) & 1) ? "dual" : "single",
71 ((ch_conf >> 16) & 1) ? "" : ", selected");
Angel Pons430f1c52020-10-13 23:01:48 +020072
Angel Pons239c9662020-10-13 21:34:53 +020073 printk(BIOS_DEBUG, " DIMMB %d MB width %s %s rank%s\n",
74 ((ch_conf >> 8) & 0xff) * 256,
Angel Pons973c9d42020-10-13 23:28:23 +020075 ((ch_conf >> 20) & 1) ? "x16" : "x8 or x32",
Angel Pons239c9662020-10-13 21:34:53 +020076 ((ch_conf >> 18) & 1) ? "dual" : "single",
77 ((ch_conf >> 16) & 1) ? ", selected" : "");
78 }
79}
80
81/*
Duncan Lauriec88c54c2014-04-30 16:36:13 -070082 * Find PEI executable in coreboot filesystem and execute it.
83 */
Angel Ponsdc600732021-06-23 13:11:30 +020084static void sdram_initialize(struct pei_data *pei_data)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070085{
Shelley Chenad9cd682020-07-23 16:10:52 -070086 size_t mrc_size;
Duncan Lauriec88c54c2014-04-30 16:36:13 -070087 pei_wrapper_entry_t entry;
88 int ret;
89
90 broadwell_fill_pei_data(pei_data);
91
Shelley Chen6615c6e2020-10-27 15:58:31 -070092 /* Assume boot device is memory mapped. */
93 assert(CONFIG(BOOT_DEVICE_MEMORY_MAPPED));
Shelley Chenad9cd682020-07-23 16:10:52 -070094
Shelley Chen6615c6e2020-10-27 15:58:31 -070095 pei_data->saved_data =
96 mrc_cache_current_mmap_leak(MRC_TRAINING_DATA, 0,
97 &mrc_size);
98 if (pei_data->saved_data) {
99 /* MRC cache found */
100 pei_data->saved_data_size = mrc_size;
101 } else if (pei_data->boot_mode == ACPI_S3) {
102 /* Waking from S3 and no cache. */
103 printk(BIOS_DEBUG,
104 "No MRC cache found in S3 resume path.\n");
105 post_code(POST_RESUME_FAILURE);
106 system_reset();
107 } else {
108 printk(BIOS_DEBUG, "No MRC cache found.\n");
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700109 }
110
Duncan Laurie61680272014-05-05 12:42:35 -0500111 /*
112 * Do not use saved pei data. Can be set by mainboard romstage
113 * to force a full train of memory on every boot.
114 */
115 if (pei_data->disable_saved_data) {
116 printk(BIOS_DEBUG, "Disabling PEI saved data by request\n");
117 pei_data->saved_data = NULL;
118 pei_data->saved_data_size = 0;
119 }
120
Arthur Heymans4d56a062018-12-22 16:11:52 +0100121 /* We don't care about leaking the mapping */
Julius Werner9d0cc2a2020-01-22 18:00:18 -0800122 entry = cbfs_ro_map("mrc.bin", NULL);
123 if (entry == NULL)
124 die("mrc.bin not found!");
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700125
126 printk(BIOS_DEBUG, "Starting Memory Reference Code\n");
127
128 ret = entry(pei_data);
129 if (ret < 0)
130 die("pei_data version mismatch\n");
131
132 /* Print the MRC version after executing the UEFI PEI stage. */
Angel Ponsa8753e92021-04-17 14:34:37 +0200133 u32 version = mchbar_read32(MRC_REVISION);
Angel Ponsc1328a62021-06-14 12:43:11 +0200134 printk(BIOS_DEBUG, "MRC Version %u.%u.%u Build %u\n",
Angel Pons430f1c52020-10-13 23:01:48 +0200135 (version >> 24) & 0xff, (version >> 16) & 0xff,
136 (version >> 8) & 0xff, (version >> 0) & 0xff);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700137
138 report_memory_config();
Angel Ponsd0d528a2021-01-20 23:09:16 +0100139}
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700140
Angel Ponsdc600732021-06-23 13:11:30 +0200141static void setup_sdram_meminfo(struct pei_data *pei_data)
Angel Ponsd0d528a2021-01-20 23:09:16 +0100142{
143 struct memory_info *mem_info;
Kane Chenebbb0d42014-07-28 10:54:40 -0700144
145 printk(BIOS_DEBUG, "create cbmem for dimm information\n");
146 mem_info = cbmem_add(CBMEM_ID_MEMINFO, sizeof(struct memory_info));
John Zhao317cbd62019-05-31 10:44:46 -0700147
148 if (!mem_info) {
149 printk(BIOS_ERR, "Error! Failed to add mem_info to cbmem\n");
150 return;
151 }
152
Matt DeVillier9aaf59a2018-05-27 21:51:49 -0500153 memset(mem_info, 0, sizeof(*mem_info));
154 /* Translate pei_memory_info struct data into memory_info struct */
155 mem_info->dimm_cnt = pei_data->meminfo.dimm_cnt;
156 for (int i = 0; i < MIN(DIMM_INFO_TOTAL, PEI_DIMM_INFO_TOTAL); i++) {
157 struct dimm_info *dimm = &mem_info->dimm[i];
158 const struct pei_dimm_info *pei_dimm =
159 &pei_data->meminfo.dimm[i];
160 dimm->dimm_size = pei_dimm->dimm_size;
161 dimm->ddr_type = pei_dimm->ddr_type;
162 dimm->ddr_frequency = pei_dimm->ddr_frequency;
163 dimm->rank_per_dimm = pei_dimm->rank_per_dimm;
164 dimm->channel_num = pei_dimm->channel_num;
165 dimm->dimm_num = pei_dimm->dimm_num;
166 dimm->bank_locator = pei_dimm->bank_locator;
167 memcpy(&dimm->serial, &pei_dimm->serial,
168 MIN(sizeof(dimm->serial), sizeof(pei_dimm->serial)));
169 memcpy(&dimm->module_part_number,
170 &pei_dimm->module_part_number,
171 MIN(sizeof(dimm->module_part_number),
172 sizeof(pei_dimm->module_part_number)));
173 dimm->module_part_number[DIMM_INFO_PART_NUMBER_SIZE - 1] = '\0';
174 dimm->mod_id = pei_dimm->mod_id;
175 dimm->mod_type = pei_dimm->mod_type;
176 dimm->bus_width = pei_dimm->bus_width;
177 }
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700178}
Angel Ponsdc600732021-06-23 13:11:30 +0200179
180void perform_raminit(const struct chipset_power_state *const power_state)
181{
182 const int s3resume = power_state->prev_sleep_state == ACPI_S3;
183
184 struct pei_data pei_data = { 0 };
185
186 mainboard_fill_pei_data(&pei_data);
187 mainboard_fill_spd_data(&pei_data);
188
189 post_code(0x32);
190
191 timestamp_add_now(TS_BEFORE_INITRAM);
192
193 pei_data.boot_mode = power_state->prev_sleep_state;
194
195 /* Initialize RAM */
196 sdram_initialize(&pei_data);
197
198 timestamp_add_now(TS_AFTER_INITRAM);
199
200 int cbmem_was_initted = !cbmem_recovery(s3resume);
201 if (s3resume && !cbmem_was_initted) {
202 /* Failed S3 resume, reset to come up cleanly */
203 printk(BIOS_CRIT, "Failed to recover CBMEM in S3 resume.\n");
204 system_reset();
205 }
206
207 save_mrc_data(&pei_data);
208
209 setup_sdram_meminfo(&pei_data);
210}