blob: 2fdbe07c8dcf8d6570163b81fc8b699d8fca4f80 [file] [log] [blame]
Aaron Durbin76c37002012-10-30 09:03:43 -05001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2011 Google Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Aaron Durbin76c37002012-10-30 09:03:43 -050014 */
15
16#include <console/console.h>
Iru Cai33642032019-06-11 14:24:43 +080017#include <console/usb.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050018#include <string.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050019#include <cbmem.h>
20#include <arch/cbfs.h>
21#include <cbfs.h>
Elyes HAOUAS82d46422019-04-28 18:01:48 +020022#include <cf9_reset.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050023#include <ip_checksum.h>
Matt DeVillier5aaa8ce2016-09-02 13:29:17 -050024#include <memory_info.h>
Arthur Heymansf300f362018-01-27 13:39:12 +010025#include <mrc_cache.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050026#include <device/pci_def.h>
Matt DeVillier5aaa8ce2016-09-02 13:29:17 -050027#include <device/dram/ddr3.h>
28#include <smbios.h>
29#include <spd.h>
Philipp Deppenwiesefea24292017-10-17 17:02:29 +020030#include <security/vboot/vboot_common.h>
Arthur Heymansf300f362018-01-27 13:39:12 +010031#include <commonlib/region.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050032#include "raminit.h"
33#include "pei_data.h"
34#include "haswell.h"
35
Arthur Heymansf300f362018-01-27 13:39:12 +010036#define MRC_CACHE_VERSION 1
37
Aaron Durbin2ad1dba2013-02-07 00:51:18 -060038void save_mrc_data(struct pei_data *pei_data)
Aaron Durbin76c37002012-10-30 09:03:43 -050039{
Aaron Durbin76c37002012-10-30 09:03:43 -050040 /* Save the MRC S3 restore data to cbmem */
Arthur Heymansf300f362018-01-27 13:39:12 +010041 mrc_cache_stash_data(MRC_TRAINING_DATA, MRC_CACHE_VERSION,
42 pei_data->mrc_output, pei_data->mrc_output_len);
Aaron Durbin76c37002012-10-30 09:03:43 -050043}
44
45static void prepare_mrc_cache(struct pei_data *pei_data)
46{
Arthur Heymansf300f362018-01-27 13:39:12 +010047 struct region_device rdev;
Aaron Durbin76c37002012-10-30 09:03:43 -050048
49 // preset just in case there is an error
50 pei_data->mrc_input = NULL;
51 pei_data->mrc_input_len = 0;
52
Arthur Heymansf300f362018-01-27 13:39:12 +010053 if (mrc_cache_get_current(MRC_TRAINING_DATA, MRC_CACHE_VERSION, &rdev))
Aaron Durbin76c37002012-10-30 09:03:43 -050054 /* error message printed in find_current_mrc_cache */
55 return;
Aaron Durbin76c37002012-10-30 09:03:43 -050056
Arthur Heymansf300f362018-01-27 13:39:12 +010057 pei_data->mrc_input = rdev_mmap_full(&rdev);
58 pei_data->mrc_input_len = region_device_sz(&rdev);
Aaron Durbin76c37002012-10-30 09:03:43 -050059
Arthur Heymansf300f362018-01-27 13:39:12 +010060 printk(BIOS_DEBUG, "%s: at %p, size %x\n",
61 __func__, pei_data->mrc_input, pei_data->mrc_input_len);
Aaron Durbin76c37002012-10-30 09:03:43 -050062}
63
Elyes HAOUAS448d9fb2018-05-22 12:51:27 +020064static const char *ecc_decoder[] = {
Aaron Durbin76c37002012-10-30 09:03:43 -050065 "inactive",
66 "active on IO",
67 "disabled on IO",
68 "active"
69};
70
71/*
72 * Dump in the log memory controller configuration as read from the memory
73 * controller registers.
74 */
75static void report_memory_config(void)
76{
77 u32 addr_decoder_common, addr_decode_ch[2];
78 int i;
79
80 addr_decoder_common = MCHBAR32(0x5000);
81 addr_decode_ch[0] = MCHBAR32(0x5004);
82 addr_decode_ch[1] = MCHBAR32(0x5008);
83
84 printk(BIOS_DEBUG, "memcfg DDR3 clock %d MHz\n",
85 (MCHBAR32(0x5e04) * 13333 * 2 + 50)/100);
86 printk(BIOS_DEBUG, "memcfg channel assignment: A: %d, B % d, C % d\n",
87 addr_decoder_common & 3,
88 (addr_decoder_common >> 2) & 3,
89 (addr_decoder_common >> 4) & 3);
90
91 for (i = 0; i < ARRAY_SIZE(addr_decode_ch); i++) {
92 u32 ch_conf = addr_decode_ch[i];
93 printk(BIOS_DEBUG, "memcfg channel[%d] config (%8.8x):\n",
94 i, ch_conf);
95 printk(BIOS_DEBUG, " ECC %s\n",
96 ecc_decoder[(ch_conf >> 24) & 3]);
97 printk(BIOS_DEBUG, " enhanced interleave mode %s\n",
98 ((ch_conf >> 22) & 1) ? "on" : "off");
99 printk(BIOS_DEBUG, " rank interleave %s\n",
100 ((ch_conf >> 21) & 1) ? "on" : "off");
Duncan Laurie8d774022013-10-22 16:32:49 -0700101 printk(BIOS_DEBUG, " DIMMA %d MB width %s %s rank%s\n",
Aaron Durbin76c37002012-10-30 09:03:43 -0500102 ((ch_conf >> 0) & 0xff) * 256,
Duncan Laurie8d774022013-10-22 16:32:49 -0700103 ((ch_conf >> 19) & 1) ? "x16" : "x8 or x32",
Aaron Durbin76c37002012-10-30 09:03:43 -0500104 ((ch_conf >> 17) & 1) ? "dual" : "single",
105 ((ch_conf >> 16) & 1) ? "" : ", selected");
Duncan Laurie8d774022013-10-22 16:32:49 -0700106 printk(BIOS_DEBUG, " DIMMB %d MB width %s %s rank%s\n",
Aaron Durbin76c37002012-10-30 09:03:43 -0500107 ((ch_conf >> 8) & 0xff) * 256,
Ryan Salsamendidab81a42017-06-30 17:36:41 -0700108 ((ch_conf >> 20) & 1) ? "x16" : "x8 or x32",
Aaron Durbin76c37002012-10-30 09:03:43 -0500109 ((ch_conf >> 18) & 1) ? "dual" : "single",
110 ((ch_conf >> 16) & 1) ? ", selected" : "");
111 }
112}
113
114/**
115 * Find PEI executable in coreboot filesystem and execute it.
116 *
117 * @param pei_data: configuration data for UEFI PEI reference code
118 */
119void sdram_initialize(struct pei_data *pei_data)
120{
Aaron Durbin76c37002012-10-30 09:03:43 -0500121 unsigned long entry;
Arthur Heymans8da2fa02018-06-06 10:35:45 +0200122 uint32_t type = CBFS_TYPE_MRC;
123 struct cbfsf f;
Aaron Durbin76c37002012-10-30 09:03:43 -0500124
Aaron Durbin76c37002012-10-30 09:03:43 -0500125 printk(BIOS_DEBUG, "Starting UEFI PEI System Agent\n");
126
Aaron Durbin76c37002012-10-30 09:03:43 -0500127 /*
128 * Do not pass MRC data in for recovery mode boot,
129 * Always pass it in for S3 resume.
130 */
Furquan Shaikh0325dc62016-07-25 13:02:36 -0700131 if (!vboot_recovery_mode_enabled() || pei_data->boot_mode == 2)
Aaron Durbin76c37002012-10-30 09:03:43 -0500132 prepare_mrc_cache(pei_data);
133
134 /* If MRC data is not found we cannot continue S3 resume. */
135 if (pei_data->boot_mode == 2 && !pei_data->mrc_input) {
Duncan Laurie727b5452013-08-08 16:28:41 -0700136 post_code(POST_RESUME_FAILURE);
137 printk(BIOS_DEBUG, "Giving up in sdram_initialize: "
138 "No MRC data\n");
Elyes HAOUAS82d46422019-04-28 18:01:48 +0200139 system_reset();
Aaron Durbin76c37002012-10-30 09:03:43 -0500140 }
141
142 /* Pass console handler in pei_data */
Kyösti Mälkki657e0be2014-02-04 19:03:57 +0200143 pei_data->tx_byte = do_putchar;
Aaron Durbin76c37002012-10-30 09:03:43 -0500144
Arthur Heymans8da2fa02018-06-06 10:35:45 +0200145 /*
146 * Locate and call UEFI System Agent binary. The binary needs to be at
147 * a fixed offset in the flash and can therefore only reside in the
148 * COREBOOT fmap region
149 */
150 if (cbfs_locate_file_in_region(&f, "COREBOOT", "mrc.bin", &type) < 0)
151 die("mrc.bin not found!");
152 /* We don't care about leaking the mapping */
153 entry = (unsigned long)rdev_mmap_full(&f.data);
Aaron Durbin76c37002012-10-30 09:03:43 -0500154 if (entry) {
155 int rv;
156 asm volatile (
157 "call *%%ecx\n\t"
158 :"=a" (rv) : "c" (entry), "a" (pei_data));
Iru Cai33642032019-06-11 14:24:43 +0800159
160 /* mrc.bin reconfigures USB, so reinit it to have debug */
161 if (CONFIG(USBDEBUG_IN_PRE_RAM))
162 usbdebug_hw_init(true);
163
Aaron Durbin76c37002012-10-30 09:03:43 -0500164 if (rv) {
165 switch (rv) {
166 case -1:
167 printk(BIOS_ERR, "PEI version mismatch.\n");
168 break;
169 case -2:
170 printk(BIOS_ERR, "Invalid memory frequency.\n");
171 break;
172 default:
173 printk(BIOS_ERR, "MRC returned %x.\n", rv);
174 }
Keith Shortbb41aba2019-05-16 14:07:43 -0600175 die_with_post_code(POST_INVALID_VENDOR_BINARY,
176 "Nonzero MRC return value.\n");
Aaron Durbin76c37002012-10-30 09:03:43 -0500177 }
178 } else {
179 die("UEFI PEI System Agent not found.\n");
180 }
181
182 /* For reference print the System Agent version
183 * after executing the UEFI PEI stage.
184 */
185 u32 version = MCHBAR32(0x5034);
186 printk(BIOS_DEBUG, "System Agent Version %d.%d.%d Build %d\n",
Elyes HAOUASa342f392018-10-17 10:56:26 +0200187 version >> 24, (version >> 16) & 0xff,
Aaron Durbin76c37002012-10-30 09:03:43 -0500188 (version >> 8) & 0xff, version & 0xff);
189
Aaron Durbin76c37002012-10-30 09:03:43 -0500190 report_memory_config();
Aaron Durbin76c37002012-10-30 09:03:43 -0500191}
Matt DeVillier5aaa8ce2016-09-02 13:29:17 -0500192
193void setup_sdram_meminfo(struct pei_data *pei_data)
194{
Elyes HAOUASce83f312019-05-20 18:31:38 +0200195 u32 addr_decode_ch[2];
Matt DeVillier5aaa8ce2016-09-02 13:29:17 -0500196 struct memory_info* mem_info;
197 struct dimm_info *dimm;
198 int ddr_frequency;
199 int dimm_size;
200 int ch, d_num;
201 int dimm_cnt = 0;
202
203 mem_info = cbmem_add(CBMEM_ID_MEMINFO, sizeof(struct memory_info));
Nico Huberacac02d2017-06-20 14:49:04 +0200204 if (!mem_info)
205 die("Failed to add memory info to CBMEM.\n");
Matt DeVillier5aaa8ce2016-09-02 13:29:17 -0500206 memset(mem_info, 0, sizeof(struct memory_info));
207
Elyes HAOUASce83f312019-05-20 18:31:38 +0200208 /* FIXME: Do we need to read MCHBAR32(0x5000) ? */
209 MCHBAR32(0x5000);
Matt DeVillier5aaa8ce2016-09-02 13:29:17 -0500210 addr_decode_ch[0] = MCHBAR32(0x5004);
211 addr_decode_ch[1] = MCHBAR32(0x5008);
212
213 ddr_frequency = (MCHBAR32(0x5e04) * 13333 * 2 + 50) / 100;
214
215 for (ch = 0; ch < ARRAY_SIZE(addr_decode_ch); ch++) {
216 u32 ch_conf = addr_decode_ch[ch];
217 /* DIMMs A/B */
218 for (d_num = 0; d_num < 2; d_num++) {
219 dimm_size = ((ch_conf >> (d_num * 8)) & 0xff) * 256;
220 if (dimm_size) {
221 dimm = &mem_info->dimm[dimm_cnt];
222 dimm->dimm_size = dimm_size;
223 dimm->ddr_type = MEMORY_TYPE_DDR3;
224 dimm->ddr_frequency = ddr_frequency;
225 dimm->rank_per_dimm = 1 + ((ch_conf >> (17 + d_num)) & 1);
226 dimm->channel_num = ch;
227 dimm->dimm_num = d_num;
228 dimm->bank_locator = ch * 2;
229 memcpy(dimm->serial,
230 &pei_data->spd_data[dimm_cnt][SPD_DIMM_SERIAL_NUM],
231 SPD_DIMM_SERIAL_LEN);
232 memcpy(dimm->module_part_number,
233 &pei_data->spd_data[dimm_cnt][SPD_DIMM_PART_NUM],
234 SPD_DIMM_PART_LEN);
235 dimm->mod_id =
236 (pei_data->spd_data[dimm_cnt][SPD_DIMM_MOD_ID2] << 8) |
237 (pei_data->spd_data[dimm_cnt][SPD_DIMM_MOD_ID1] & 0xFF);
238 dimm->mod_type = SPD_SODIMM;
239 dimm->bus_width = 0x3; /* 64-bit */
240 dimm_cnt++;
241 }
242 }
243 }
244 mem_info->dimm_cnt = dimm_cnt;
245}