blob: fb780cfbbbcdaa7d95009b20d1ae9c3438b22d12 [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 Wernercd49cce2019-03-05 16:53:33 -080012#if CONFIG(EC_GOOGLE_CHROMEEC)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070013#include <ec/google/chromeec/ec.h>
14#include <ec/google/chromeec/ec_commands.h>
15#endif
16#include <vendorcode/google/chromeos/chromeos.h>
Julius Werner4ee4bd52014-10-20 13:46:39 -070017#include <soc/iomap.h>
18#include <soc/pei_data.h>
19#include <soc/pei_wrapper.h>
20#include <soc/pm.h>
Julius Werner4ee4bd52014-10-20 13:46:39 -070021#include <soc/romstage.h>
Julius Werner4ee4bd52014-10-20 13:46:39 -070022#include <soc/systemagent.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070023
24/*
Angel Pons239c9662020-10-13 21:34:53 +020025 * Dump in the log memory controller configuration as read from the memory
26 * controller registers.
27 */
28static void report_memory_config(void)
29{
30 u32 addr_decoder_common, addr_decode_ch[2];
31 int i;
32
33 addr_decoder_common = MCHBAR32(0x5000);
34 addr_decode_ch[0] = MCHBAR32(0x5004);
35 addr_decode_ch[1] = MCHBAR32(0x5008);
36
37 printk(BIOS_DEBUG, "memcfg DDR3 clock %d MHz\n",
38 (MCHBAR32(0x5e04) * 13333 * 2 + 50)/100);
39 printk(BIOS_DEBUG, "memcfg channel assignment: A: %d, B % d, C % d\n",
40 addr_decoder_common & 3,
41 (addr_decoder_common >> 2) & 3,
42 (addr_decoder_common >> 4) & 3);
43
44 for (i = 0; i < ARRAY_SIZE(addr_decode_ch); i++) {
45 u32 ch_conf = addr_decode_ch[i];
46 printk(BIOS_DEBUG, "memcfg channel[%d] config (%8.8x):\n",
47 i, ch_conf);
48 printk(BIOS_DEBUG, " enhanced interleave mode %s\n",
49 ((ch_conf >> 22) & 1) ? "on" : "off");
50 printk(BIOS_DEBUG, " rank interleave %s\n",
51 ((ch_conf >> 21) & 1) ? "on" : "off");
52 printk(BIOS_DEBUG, " DIMMA %d MB width %s %s rank%s\n",
53 ((ch_conf >> 0) & 0xff) * 256,
54 ((ch_conf >> 19) & 1) ? "x16" : "x8 or x32",
55 ((ch_conf >> 17) & 1) ? "dual" : "single",
56 ((ch_conf >> 16) & 1) ? "" : ", selected");
57 printk(BIOS_DEBUG, " DIMMB %d MB width %s %s rank%s\n",
58 ((ch_conf >> 8) & 0xff) * 256,
59 ((ch_conf >> 19) & 1) ? "x16" : "x8 or x32",
60 ((ch_conf >> 18) & 1) ? "dual" : "single",
61 ((ch_conf >> 16) & 1) ? ", selected" : "");
62 }
63}
64
65/*
Duncan Lauriec88c54c2014-04-30 16:36:13 -070066 * Find PEI executable in coreboot filesystem and execute it.
67 */
68void raminit(struct pei_data *pei_data)
69{
Shelley Chenad9cd682020-07-23 16:10:52 -070070 size_t mrc_size;
Lee Leahy26b7cd02017-03-16 18:47:55 -070071 struct memory_info *mem_info;
Duncan Lauriec88c54c2014-04-30 16:36:13 -070072 pei_wrapper_entry_t entry;
73 int ret;
Arthur Heymans4d56a062018-12-22 16:11:52 +010074 struct cbfsf f;
75 uint32_t type = CBFS_TYPE_MRC;
Duncan Lauriec88c54c2014-04-30 16:36:13 -070076
77 broadwell_fill_pei_data(pei_data);
78
Julius Werner29fbfcc2020-03-02 15:54:43 -080079 if (CONFIG(BROADWELL_VBOOT_IN_BOOTBLOCK) &&
80 vboot_recovery_mode_enabled()) {
Duncan Lauriec88c54c2014-04-30 16:36:13 -070081 /* Recovery mode does not use MRC cache */
82 printk(BIOS_DEBUG, "Recovery mode: not using MRC cache.\n");
Shelley Chenad9cd682020-07-23 16:10:52 -070083 } else {
Aaron Durbin31be2c92016-12-03 22:08:20 -060084 /* Assume boot device is memory mapped. */
Julius Wernercd49cce2019-03-05 16:53:33 -080085 assert(CONFIG(BOOT_DEVICE_MEMORY_MAPPED));
Shelley Chenad9cd682020-07-23 16:10:52 -070086
87 pei_data->saved_data =
88 mrc_cache_current_mmap_leak(MRC_TRAINING_DATA, 0,
89 &mrc_size);
90 if (pei_data->saved_data) {
91 /* MRC cache found */
92 pei_data->saved_data_size = mrc_size;
93 } else if (pei_data->boot_mode == ACPI_S3) {
94 /* Waking from S3 and no cache. */
95 printk(BIOS_DEBUG,
96 "No MRC cache found in S3 resume path.\n");
97 post_code(POST_RESUME_FAILURE);
98 system_reset();
99 } else {
100 printk(BIOS_DEBUG, "No MRC cache found.\n");
101 }
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700102 }
103
Duncan Laurie61680272014-05-05 12:42:35 -0500104 /*
105 * Do not use saved pei data. Can be set by mainboard romstage
106 * to force a full train of memory on every boot.
107 */
108 if (pei_data->disable_saved_data) {
109 printk(BIOS_DEBUG, "Disabling PEI saved data by request\n");
110 pei_data->saved_data = NULL;
111 pei_data->saved_data_size = 0;
112 }
113
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700114 /* Determine if mrc.bin is in the cbfs. */
Arthur Heymans4d56a062018-12-22 16:11:52 +0100115 if (cbfs_locate_file_in_region(&f, "COREBOOT", "mrc.bin", &type) < 0)
116 die("mrc.bin not found!");
117 /* We don't care about leaking the mapping */
118 entry = (pei_wrapper_entry_t)rdev_mmap_full(&f.data);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700119 if (entry == NULL) {
120 printk(BIOS_DEBUG, "Couldn't find mrc.bin\n");
121 return;
122 }
123
124 printk(BIOS_DEBUG, "Starting Memory Reference Code\n");
125
126 ret = entry(pei_data);
127 if (ret < 0)
128 die("pei_data version mismatch\n");
129
130 /* Print the MRC version after executing the UEFI PEI stage. */
131 u32 version = MCHBAR32(MCHBAR_PEI_VERSION);
132 printk(BIOS_DEBUG, "MRC Version %d.%d.%d Build %d\n",
Lee Leahy26b7cd02017-03-16 18:47:55 -0700133 version >> 24, (version >> 16) & 0xff,
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700134 (version >> 8) & 0xff, version & 0xff);
135
136 report_memory_config();
137
Aaron Durbin9e6d1432016-07-13 23:21:41 -0500138 if (pei_data->boot_mode != ACPI_S3) {
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700139 cbmem_initialize_empty();
Aaron Durbin42e68562015-06-09 13:55:51 -0500140 } else if (cbmem_initialize()) {
Aaron Durbin42e68562015-06-09 13:55:51 -0500141 printk(BIOS_DEBUG, "Failed to recover CBMEM in S3 resume.\n");
142 /* Failed S3 resume, reset to come up cleanly */
Patrick Rudolph45022ae2018-10-01 19:17:11 +0200143 system_reset();
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700144 }
145
146 printk(BIOS_DEBUG, "MRC data at %p %d bytes\n", pei_data->data_to_save,
147 pei_data->data_to_save_size);
148
149 if (pei_data->data_to_save != NULL && pei_data->data_to_save_size > 0)
Aaron Durbin31be2c92016-12-03 22:08:20 -0600150 mrc_cache_stash_data(MRC_TRAINING_DATA, 0,
151 pei_data->data_to_save,
152 pei_data->data_to_save_size);
Kane Chenebbb0d42014-07-28 10:54:40 -0700153
154 printk(BIOS_DEBUG, "create cbmem for dimm information\n");
155 mem_info = cbmem_add(CBMEM_ID_MEMINFO, sizeof(struct memory_info));
John Zhao317cbd62019-05-31 10:44:46 -0700156
157 if (!mem_info) {
158 printk(BIOS_ERR, "Error! Failed to add mem_info to cbmem\n");
159 return;
160 }
161
Matt DeVillier9aaf59a2018-05-27 21:51:49 -0500162 memset(mem_info, 0, sizeof(*mem_info));
163 /* Translate pei_memory_info struct data into memory_info struct */
164 mem_info->dimm_cnt = pei_data->meminfo.dimm_cnt;
165 for (int i = 0; i < MIN(DIMM_INFO_TOTAL, PEI_DIMM_INFO_TOTAL); i++) {
166 struct dimm_info *dimm = &mem_info->dimm[i];
167 const struct pei_dimm_info *pei_dimm =
168 &pei_data->meminfo.dimm[i];
169 dimm->dimm_size = pei_dimm->dimm_size;
170 dimm->ddr_type = pei_dimm->ddr_type;
171 dimm->ddr_frequency = pei_dimm->ddr_frequency;
172 dimm->rank_per_dimm = pei_dimm->rank_per_dimm;
173 dimm->channel_num = pei_dimm->channel_num;
174 dimm->dimm_num = pei_dimm->dimm_num;
175 dimm->bank_locator = pei_dimm->bank_locator;
176 memcpy(&dimm->serial, &pei_dimm->serial,
177 MIN(sizeof(dimm->serial), sizeof(pei_dimm->serial)));
178 memcpy(&dimm->module_part_number,
179 &pei_dimm->module_part_number,
180 MIN(sizeof(dimm->module_part_number),
181 sizeof(pei_dimm->module_part_number)));
182 dimm->module_part_number[DIMM_INFO_PART_NUMBER_SIZE - 1] = '\0';
183 dimm->mod_id = pei_dimm->mod_id;
184 dimm->mod_type = pei_dimm->mod_type;
185 dimm->bus_width = pei_dimm->bus_width;
186 }
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700187}