blob: 0da968d776e69a34ff318b1d974df26d1d3c574d [file] [log] [blame]
Stefan Reinauer00636b02012-04-04 00:08:51 +02001/*
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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#include <console/console.h>
21#include <string.h>
22#include <arch/hlt.h>
23#include <arch/io.h>
24#include <arch/romcc_io.h>
25#include <cbmem.h>
26#include <arch/cbfs.h>
27#include <cbfs.h>
28#include <ip_checksum.h>
29#include <pc80/mc146818rtc.h>
30#include "raminit.h"
31#include "pei_data.h"
32#include "sandybridge.h"
33
34/* Management Engine is in the southbridge */
35#include "southbridge/intel/bd82x6x/me.h"
36#if CONFIG_CHROMEOS
37#include <vendorcode/google/chromeos/chromeos.h>
38#endif
39#if 0
40#include <fdt/libfdt.h>
41#endif
42
43/*
44 * MRC scrambler seed offsets should be reserved in
45 * mainboard cmos.layout and not covered by checksum.
46 */
47#if CONFIG_USE_OPTION_TABLE
48#include "option_table.h"
49#define CMOS_OFFSET_MRC_SEED (CMOS_VSTART_mrc_scrambler_seed >> 3)
50#define CMOS_OFFSET_MRC_SEED_S3 (CMOS_VSTART_mrc_scrambler_seed_s3 >> 3)
51#define CMOS_OFFSET_MRC_SEED_CHK (CMOS_VSTART_mrc_scrambler_seed_chk >> 3)
52#else
53#define CMOS_OFFSET_MRC_SEED 112
54#define CMOS_OFFSET_MRC_SEED_S3 116
55#define CMOS_OFFSET_MRC_SEED_CHK 120
56#endif
57
58#define MRC_DATA_ALIGN 0x1000
59#define MRC_DATA_SIGNATURE (('M'<<0)|('R'<<8)|('C'<<16)|('D'<<24))
60
61struct mrc_data_container {
62 u32 mrc_signature; // "MRCD"
63 u32 mrc_data_size; // Actual total size of this structure
64 u32 mrc_checksum; // IP style checksum
65 u32 reserved; // For header alignment
66 u8 mrc_data[0]; // Variable size, platform/run time dependent.
67} __attribute__ ((packed));
68
69static void save_mrc_data(struct pei_data *pei_data)
70{
71 u16 c1, c2, checksum;
72
73#if CONFIG_EARLY_CBMEM_INIT
74 struct mrc_data_container *mrcdata;
75 int output_len = ALIGN(pei_data->mrc_output_len, 16);
76
77 /* Save the MRC S3 restore data to cbmem */
78 cbmem_initialize();
79 mrcdata = cbmem_add
80 (CBMEM_ID_MRCDATA,
81 output_len + sizeof(struct mrc_data_container));
82
83 printk(BIOS_DEBUG, "Relocate MRC DATA from %p to %p (%u bytes)\n",
84 pei_data->mrc_output, mrcdata, output_len);
85
86 mrcdata->mrc_signature = MRC_DATA_SIGNATURE;
87 mrcdata->mrc_data_size = output_len;
88 mrcdata->reserved = 0;
89 memcpy(mrcdata->mrc_data, pei_data->mrc_output,
90 pei_data->mrc_output_len);
91
92 /* Zero the unused space in aligned buffer. */
93 if (output_len > pei_data->mrc_output_len)
94 memset(mrcdata->mrc_data+pei_data->mrc_output_len, 0,
95 output_len - pei_data->mrc_output_len);
96
97 mrcdata->mrc_checksum = compute_ip_checksum(mrcdata->mrc_data,
98 mrcdata->mrc_data_size);
99#endif
100
101 /* Save the MRC seed values to CMOS */
102 cmos_write32(CMOS_OFFSET_MRC_SEED, pei_data->scrambler_seed);
103 printk(BIOS_DEBUG, "Save scrambler seed 0x%08x to CMOS 0x%02x\n",
104 pei_data->scrambler_seed, CMOS_OFFSET_MRC_SEED);
105
106 cmos_write32(CMOS_OFFSET_MRC_SEED_S3, pei_data->scrambler_seed_s3);
107 printk(BIOS_DEBUG, "Save s3 scrambler seed 0x%08x to CMOS 0x%02x\n",
108 pei_data->scrambler_seed_s3, CMOS_OFFSET_MRC_SEED_S3);
109
110 /* Save a simple checksum of the seed values */
111 c1 = compute_ip_checksum((u8*)&pei_data->scrambler_seed,
112 sizeof(u32));
113 c2 = compute_ip_checksum((u8*)&pei_data->scrambler_seed_s3,
114 sizeof(u32));
115 checksum = add_ip_checksums(sizeof(u32), c1, c2);
116
117 cmos_write(checksum & 0xff, CMOS_OFFSET_MRC_SEED_CHK);
118 cmos_write((checksum >> 8) & 0xff, CMOS_OFFSET_MRC_SEED_CHK+1);
119}
120
121#if CONFIG_CHROMEOS
122static void prepare_mrc_cache(struct pei_data *pei_data)
123{
124 const struct fdt_header *fdt_header;
125 const struct fdt_property *fdtp;
126 int offset, len;
127 const char *compatible = "chromeos,flashmap";
128 const char *subnode = "rw-mrc-cache";
129 const char *property = "reg";
130 u32 *data;
131 struct mrc_data_container *mrc_cache, *mrc_next;
132 u8 *mrc_region, *region_ptr;
133 u16 c1, c2, checksum, seed_checksum;
134 u32 region_size, entry_id = 0;
135 u64 flashrom_base = 0;
136
137 // preset just in case there is an error
138 pei_data->mrc_input = NULL;
139 pei_data->mrc_input_len = 0;
140
141 /* Read scrambler seeds from CMOS */
142 pei_data->scrambler_seed = cmos_read32(CMOS_OFFSET_MRC_SEED);
143 printk(BIOS_DEBUG, "Read scrambler seed 0x%08x from CMOS 0x%02x\n",
144 pei_data->scrambler_seed, CMOS_OFFSET_MRC_SEED);
145
146 pei_data->scrambler_seed_s3 = cmos_read32(CMOS_OFFSET_MRC_SEED_S3);
147 printk(BIOS_DEBUG, "Read S3 scrambler seed 0x%08x from CMOS 0x%02x\n",
148 pei_data->scrambler_seed_s3, CMOS_OFFSET_MRC_SEED_S3);
149
150 /* Compute seed checksum and compare */
151 c1 = compute_ip_checksum((u8*)&pei_data->scrambler_seed,
152 sizeof(u32));
153 c2 = compute_ip_checksum((u8*)&pei_data->scrambler_seed_s3,
154 sizeof(u32));
155 checksum = add_ip_checksums(sizeof(u32), c1, c2);
156
157 seed_checksum = cmos_read(CMOS_OFFSET_MRC_SEED_CHK);
158 seed_checksum |= cmos_read(CMOS_OFFSET_MRC_SEED_CHK+1) << 8;
159
160 if (checksum != seed_checksum) {
161 printk(BIOS_ERR, "%s: invalid seed checksum\n", __func__);
162 pei_data->scrambler_seed = 0;
163 pei_data->scrambler_seed_s3 = 0;
164 return;
165 }
166
167 fdt_header = cbfs_find_file(CONFIG_FDT_FILE_NAME, CBFS_TYPE_FDT);
168
169 if (!fdt_header) {
170 printk(BIOS_ERR, "%s: no FDT found!\n", __func__);
171 return;
172 }
173
174 offset = fdt_node_offset_by_compatible(fdt_header, 0, compatible);
175 if (offset < 0) {
176 printk(BIOS_ERR, "%s: no %s node found!\n",
177 __func__, compatible);
178 return;
179 }
180
181 if (fdt_get_base_addr(fdt_header, offset, &flashrom_base) < 0) {
182 printk(BIOS_ERR, "%s: no base address in node name!\n",
183 __func__);
184 return;
185 }
186
187 offset = fdt_subnode_offset(fdt_header, offset, subnode);
188 if (offset < 0) {
189 printk(BIOS_ERR, "%s: no %s found!\n", __func__, subnode);
190 return;
191 }
192
193 fdtp = fdt_get_property(fdt_header, offset, property, &len);
194 if (!fdtp || (len != 8)) {
195 printk(BIOS_ERR, "%s: property %s at %p, len %d!\n",
196 __func__, property, fdtp, len);
197 return;
198 }
199
200 data = (u32 *)fdtp->data;
201
202 // Calculate actual address of the MRC cache in memory
203 region_size = fdt32_to_cpu(data[1]);
204 mrc_region = region_ptr = (u8*)
205 ((unsigned long)flashrom_base + fdt32_to_cpu(data[0]));
206 mrc_cache = mrc_next = (struct mrc_data_container *)mrc_region;
207
208 if (!mrc_cache || mrc_cache->mrc_signature != MRC_DATA_SIGNATURE) {
209 printk(BIOS_ERR, "%s: invalid MRC data\n", __func__);
210 return;
211 }
212
213 if (mrc_cache->mrc_data_size == -1UL) {
214 printk(BIOS_ERR, "%s: MRC cache not initialized?\n", __func__);
215 return;
216 } else {
217 /* MRC data blocks are aligned within the region */
218 u32 mrc_size = sizeof(*mrc_cache) + mrc_cache->mrc_data_size;
219 if (mrc_size & (MRC_DATA_ALIGN - 1UL)) {
220 mrc_size &= ~(MRC_DATA_ALIGN - 1UL);
221 mrc_size += MRC_DATA_ALIGN;
222 }
223
224 /* Search for the last filled entry in the region */
225 while (mrc_next &&
226 mrc_next->mrc_signature == MRC_DATA_SIGNATURE) {
227 entry_id++;
228 mrc_cache = mrc_next;
229 /* Stay in the mrcdata region defined in fdt */
230 if ((entry_id * mrc_size) > region_size)
231 break;
232 region_ptr += mrc_size;
233 mrc_next = (struct mrc_data_container *)region_ptr;
234 }
235 entry_id--;
236 }
237
238 /* Verify checksum */
239 if (mrc_cache->mrc_checksum !=
240 compute_ip_checksum(mrc_cache->mrc_data,
241 mrc_cache->mrc_data_size)) {
242 printk(BIOS_ERR, "%s: MRC cache checksum mismatch\n", __func__);
243 return;
244 }
245
246 pei_data->mrc_input = mrc_cache->mrc_data;
247 pei_data->mrc_input_len = mrc_cache->mrc_data_size;
248
249 printk(BIOS_DEBUG, "%s: at %p, entry %u size %x checksum %04x\n",
250 __func__, pei_data->mrc_input, entry_id,
251 pei_data->mrc_input_len, mrc_cache->mrc_checksum);
252}
253#endif
254
255static const char* ecc_decoder[] = {
256 "inactive",
257 "active on IO",
258 "disabled on IO",
259 "active"
260};
261
262/*
263 * Dump in the log memory controller configuration as read from the memory
264 * controller registers.
265 */
266static void report_memory_config(void)
267{
268 u32 addr_decoder_common, addr_decode_ch[2];
269 int i;
270
271 addr_decoder_common = MCHBAR32(0x5000);
272 addr_decode_ch[0] = MCHBAR32(0x5004);
273 addr_decode_ch[1] = MCHBAR32(0x5008);
274
275 printk(BIOS_DEBUG, "memcfg DDR3 clock %d MHz\n",
276 (MCHBAR32(0x5e04) * 13333 * 2 + 50)/100);
277 printk(BIOS_DEBUG, "memcfg channel assignment: A: %d, B % d, C % d\n",
278 addr_decoder_common & 3,
279 (addr_decoder_common >> 2) & 3,
280 (addr_decoder_common >> 4) & 3);
281
282 for (i = 0; i < ARRAY_SIZE(addr_decode_ch); i++) {
283 u32 ch_conf = addr_decode_ch[i];
284 printk(BIOS_DEBUG, "memcfg channel[%d] config (%8.8x):\n",
285 i, ch_conf);
286 printk(BIOS_DEBUG, " ECC %s\n",
287 ecc_decoder[(ch_conf >> 24) & 3]);
288 printk(BIOS_DEBUG, " enhanced interleave mode %s\n",
289 ((ch_conf >> 22) & 1) ? "on" : "off");
290 printk(BIOS_DEBUG, " rank interleave %s\n",
291 ((ch_conf >> 21) & 1) ? "on" : "off");
292 printk(BIOS_DEBUG, " DIMMA %d MB width x%d %s rank%s\n",
293 ((ch_conf >> 0) & 0xff) * 256,
294 ((ch_conf >> 19) & 1) ? 16 : 8,
295 ((ch_conf >> 17) & 1) ? "dual" : "single",
296 ((ch_conf >> 16) & 1) ? "" : ", selected");
297 printk(BIOS_DEBUG, " DIMMB %d MB width x%d %s rank%s\n",
298 ((ch_conf >> 8) & 0xff) * 256,
299 ((ch_conf >> 20) & 1) ? 16 : 8,
300 ((ch_conf >> 18) & 1) ? "dual" : "single",
301 ((ch_conf >> 16) & 1) ? ", selected" : "");
302 }
303}
304
305/**
306 * Find PEI executable in coreboot filesystem and execute it.
307 *
308 * @param pei_data: configuration data for UEFI PEI reference code
309 */
310void sdram_initialize(struct pei_data *pei_data)
311{
312 struct sys_info sysinfo;
313 const char *target = "mrc.bin";
314 unsigned long entry;
315
316 /* Wait for ME to be ready */
317 intel_early_me_init();
318 intel_early_me_uma_size();
319
320 printk(BIOS_DEBUG, "Starting UEFI PEI System Agent\n");
321
322 memset(&sysinfo, 0, sizeof(sysinfo));
323
324 sysinfo.boot_path = pei_data->boot_mode;
325
326#if CONFIG_CHROMEOS
327 /*
328 * Do not pass MRC data in for recovery mode boot,
329 * Always pass it in for S3 resume.
330 */
331 if (!recovery_mode_enabled() || pei_data->boot_mode == 2)
332 prepare_mrc_cache(pei_data);
333
334 /* If MRC data is not found we cannot continue S3 resume. */
335 if (pei_data->boot_mode == 2 && !pei_data->mrc_input) {
336 outb(0x6, 0xcf9);
337 hlt();
338 }
339#endif
340
341 /* Locate and call UEFI System Agent binary. */
342 entry = (unsigned long)cbfs_find_file(target, 0xab);
343 if (entry) {
344 int rv;
345 asm volatile (
346 "call *%%ecx\n\t"
347 :"=a" (rv) : "c" (entry), "a" (pei_data));
348 if (rv) {
349 printk(BIOS_ERR, "MRC returned %d\n", rv);
350 die("Nonzero MRC return value\n");
351 }
352 } else {
353 die("UEFI PEI System Agent not found.\n");
354 }
355
356 /* For reference print the System Agent version
357 * after executing the UEFI PEI stage.
358 */
359 u32 version = MCHBAR32(0x5034);
360 printk(BIOS_DEBUG, "System Agent Version %d.%d.%d Build %d\n",
361 version >> 24 , (version >> 16) & 0xff,
362 (version >> 8) & 0xff, version & 0xff);
363
364 intel_early_me_init_done(ME_INIT_STATUS_SUCCESS);
365
366 report_memory_config();
367
368 /* S3 resume: don't save scrambler seed or MRC data */
369 if (pei_data->boot_mode != 2)
370 save_mrc_data(pei_data);
371}
372
373struct cbmem_entry *get_cbmem_toc(void)
374{
375 return (struct cbmem_entry *)(get_top_of_ram() - HIGH_MEMORY_SIZE);
376}
377
378unsigned long get_top_of_ram(void)
379{
380 /* Base of TSEG is top of usable DRAM */
381 u32 tom = pci_read_config32(PCI_DEV(0,0,0), TSEG);
382 return (unsigned long) tom;
383}