Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 61 | struct 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 | |
| 69 | static 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 |
| 122 | static void prepare_mrc_cache(struct pei_data *pei_data) |
| 123 | { |
Stefan Reinauer | 6ea86b1 | 2012-04-27 22:54:10 +0200 | [diff] [blame] | 124 | #if 0 |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 125 | const struct fdt_header *fdt_header; |
| 126 | const struct fdt_property *fdtp; |
| 127 | int offset, len; |
| 128 | const char *compatible = "chromeos,flashmap"; |
| 129 | const char *subnode = "rw-mrc-cache"; |
| 130 | const char *property = "reg"; |
| 131 | u32 *data; |
| 132 | struct mrc_data_container *mrc_cache, *mrc_next; |
| 133 | u8 *mrc_region, *region_ptr; |
| 134 | u16 c1, c2, checksum, seed_checksum; |
| 135 | u32 region_size, entry_id = 0; |
| 136 | u64 flashrom_base = 0; |
| 137 | |
| 138 | // preset just in case there is an error |
| 139 | pei_data->mrc_input = NULL; |
| 140 | pei_data->mrc_input_len = 0; |
| 141 | |
| 142 | /* Read scrambler seeds from CMOS */ |
| 143 | pei_data->scrambler_seed = cmos_read32(CMOS_OFFSET_MRC_SEED); |
| 144 | printk(BIOS_DEBUG, "Read scrambler seed 0x%08x from CMOS 0x%02x\n", |
| 145 | pei_data->scrambler_seed, CMOS_OFFSET_MRC_SEED); |
| 146 | |
| 147 | pei_data->scrambler_seed_s3 = cmos_read32(CMOS_OFFSET_MRC_SEED_S3); |
| 148 | printk(BIOS_DEBUG, "Read S3 scrambler seed 0x%08x from CMOS 0x%02x\n", |
| 149 | pei_data->scrambler_seed_s3, CMOS_OFFSET_MRC_SEED_S3); |
| 150 | |
| 151 | /* Compute seed checksum and compare */ |
| 152 | c1 = compute_ip_checksum((u8*)&pei_data->scrambler_seed, |
| 153 | sizeof(u32)); |
| 154 | c2 = compute_ip_checksum((u8*)&pei_data->scrambler_seed_s3, |
| 155 | sizeof(u32)); |
| 156 | checksum = add_ip_checksums(sizeof(u32), c1, c2); |
| 157 | |
| 158 | seed_checksum = cmos_read(CMOS_OFFSET_MRC_SEED_CHK); |
| 159 | seed_checksum |= cmos_read(CMOS_OFFSET_MRC_SEED_CHK+1) << 8; |
| 160 | |
| 161 | if (checksum != seed_checksum) { |
| 162 | printk(BIOS_ERR, "%s: invalid seed checksum\n", __func__); |
| 163 | pei_data->scrambler_seed = 0; |
| 164 | pei_data->scrambler_seed_s3 = 0; |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | fdt_header = cbfs_find_file(CONFIG_FDT_FILE_NAME, CBFS_TYPE_FDT); |
| 169 | |
| 170 | if (!fdt_header) { |
| 171 | printk(BIOS_ERR, "%s: no FDT found!\n", __func__); |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | offset = fdt_node_offset_by_compatible(fdt_header, 0, compatible); |
| 176 | if (offset < 0) { |
| 177 | printk(BIOS_ERR, "%s: no %s node found!\n", |
| 178 | __func__, compatible); |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | if (fdt_get_base_addr(fdt_header, offset, &flashrom_base) < 0) { |
| 183 | printk(BIOS_ERR, "%s: no base address in node name!\n", |
| 184 | __func__); |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | offset = fdt_subnode_offset(fdt_header, offset, subnode); |
| 189 | if (offset < 0) { |
| 190 | printk(BIOS_ERR, "%s: no %s found!\n", __func__, subnode); |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | fdtp = fdt_get_property(fdt_header, offset, property, &len); |
| 195 | if (!fdtp || (len != 8)) { |
| 196 | printk(BIOS_ERR, "%s: property %s at %p, len %d!\n", |
| 197 | __func__, property, fdtp, len); |
| 198 | return; |
| 199 | } |
| 200 | |
| 201 | data = (u32 *)fdtp->data; |
| 202 | |
| 203 | // Calculate actual address of the MRC cache in memory |
| 204 | region_size = fdt32_to_cpu(data[1]); |
| 205 | mrc_region = region_ptr = (u8*) |
| 206 | ((unsigned long)flashrom_base + fdt32_to_cpu(data[0])); |
| 207 | mrc_cache = mrc_next = (struct mrc_data_container *)mrc_region; |
| 208 | |
| 209 | if (!mrc_cache || mrc_cache->mrc_signature != MRC_DATA_SIGNATURE) { |
| 210 | printk(BIOS_ERR, "%s: invalid MRC data\n", __func__); |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | if (mrc_cache->mrc_data_size == -1UL) { |
| 215 | printk(BIOS_ERR, "%s: MRC cache not initialized?\n", __func__); |
| 216 | return; |
| 217 | } else { |
| 218 | /* MRC data blocks are aligned within the region */ |
| 219 | u32 mrc_size = sizeof(*mrc_cache) + mrc_cache->mrc_data_size; |
| 220 | if (mrc_size & (MRC_DATA_ALIGN - 1UL)) { |
| 221 | mrc_size &= ~(MRC_DATA_ALIGN - 1UL); |
| 222 | mrc_size += MRC_DATA_ALIGN; |
| 223 | } |
| 224 | |
| 225 | /* Search for the last filled entry in the region */ |
| 226 | while (mrc_next && |
| 227 | mrc_next->mrc_signature == MRC_DATA_SIGNATURE) { |
| 228 | entry_id++; |
| 229 | mrc_cache = mrc_next; |
| 230 | /* Stay in the mrcdata region defined in fdt */ |
| 231 | if ((entry_id * mrc_size) > region_size) |
| 232 | break; |
| 233 | region_ptr += mrc_size; |
| 234 | mrc_next = (struct mrc_data_container *)region_ptr; |
| 235 | } |
| 236 | entry_id--; |
| 237 | } |
| 238 | |
| 239 | /* Verify checksum */ |
| 240 | if (mrc_cache->mrc_checksum != |
| 241 | compute_ip_checksum(mrc_cache->mrc_data, |
| 242 | mrc_cache->mrc_data_size)) { |
| 243 | printk(BIOS_ERR, "%s: MRC cache checksum mismatch\n", __func__); |
| 244 | return; |
| 245 | } |
| 246 | |
| 247 | pei_data->mrc_input = mrc_cache->mrc_data; |
| 248 | pei_data->mrc_input_len = mrc_cache->mrc_data_size; |
| 249 | |
| 250 | printk(BIOS_DEBUG, "%s: at %p, entry %u size %x checksum %04x\n", |
| 251 | __func__, pei_data->mrc_input, entry_id, |
| 252 | pei_data->mrc_input_len, mrc_cache->mrc_checksum); |
Stefan Reinauer | 6ea86b1 | 2012-04-27 22:54:10 +0200 | [diff] [blame] | 253 | #else |
| 254 | printk(BIOS_ERR, "MRC cache handling code has to be redone."); |
| 255 | #endif |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 256 | } |
| 257 | #endif |
| 258 | |
| 259 | static const char* ecc_decoder[] = { |
| 260 | "inactive", |
| 261 | "active on IO", |
| 262 | "disabled on IO", |
| 263 | "active" |
| 264 | }; |
| 265 | |
| 266 | /* |
| 267 | * Dump in the log memory controller configuration as read from the memory |
| 268 | * controller registers. |
| 269 | */ |
| 270 | static void report_memory_config(void) |
| 271 | { |
| 272 | u32 addr_decoder_common, addr_decode_ch[2]; |
| 273 | int i; |
| 274 | |
| 275 | addr_decoder_common = MCHBAR32(0x5000); |
| 276 | addr_decode_ch[0] = MCHBAR32(0x5004); |
| 277 | addr_decode_ch[1] = MCHBAR32(0x5008); |
| 278 | |
| 279 | printk(BIOS_DEBUG, "memcfg DDR3 clock %d MHz\n", |
| 280 | (MCHBAR32(0x5e04) * 13333 * 2 + 50)/100); |
| 281 | printk(BIOS_DEBUG, "memcfg channel assignment: A: %d, B % d, C % d\n", |
| 282 | addr_decoder_common & 3, |
| 283 | (addr_decoder_common >> 2) & 3, |
| 284 | (addr_decoder_common >> 4) & 3); |
| 285 | |
| 286 | for (i = 0; i < ARRAY_SIZE(addr_decode_ch); i++) { |
| 287 | u32 ch_conf = addr_decode_ch[i]; |
| 288 | printk(BIOS_DEBUG, "memcfg channel[%d] config (%8.8x):\n", |
| 289 | i, ch_conf); |
| 290 | printk(BIOS_DEBUG, " ECC %s\n", |
| 291 | ecc_decoder[(ch_conf >> 24) & 3]); |
| 292 | printk(BIOS_DEBUG, " enhanced interleave mode %s\n", |
| 293 | ((ch_conf >> 22) & 1) ? "on" : "off"); |
| 294 | printk(BIOS_DEBUG, " rank interleave %s\n", |
| 295 | ((ch_conf >> 21) & 1) ? "on" : "off"); |
| 296 | printk(BIOS_DEBUG, " DIMMA %d MB width x%d %s rank%s\n", |
| 297 | ((ch_conf >> 0) & 0xff) * 256, |
| 298 | ((ch_conf >> 19) & 1) ? 16 : 8, |
| 299 | ((ch_conf >> 17) & 1) ? "dual" : "single", |
| 300 | ((ch_conf >> 16) & 1) ? "" : ", selected"); |
| 301 | printk(BIOS_DEBUG, " DIMMB %d MB width x%d %s rank%s\n", |
| 302 | ((ch_conf >> 8) & 0xff) * 256, |
| 303 | ((ch_conf >> 20) & 1) ? 16 : 8, |
| 304 | ((ch_conf >> 18) & 1) ? "dual" : "single", |
| 305 | ((ch_conf >> 16) & 1) ? ", selected" : ""); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Find PEI executable in coreboot filesystem and execute it. |
| 311 | * |
| 312 | * @param pei_data: configuration data for UEFI PEI reference code |
| 313 | */ |
| 314 | void sdram_initialize(struct pei_data *pei_data) |
| 315 | { |
| 316 | struct sys_info sysinfo; |
| 317 | const char *target = "mrc.bin"; |
| 318 | unsigned long entry; |
| 319 | |
| 320 | /* Wait for ME to be ready */ |
| 321 | intel_early_me_init(); |
| 322 | intel_early_me_uma_size(); |
| 323 | |
| 324 | printk(BIOS_DEBUG, "Starting UEFI PEI System Agent\n"); |
| 325 | |
| 326 | memset(&sysinfo, 0, sizeof(sysinfo)); |
| 327 | |
| 328 | sysinfo.boot_path = pei_data->boot_mode; |
| 329 | |
| 330 | #if CONFIG_CHROMEOS |
| 331 | /* |
| 332 | * Do not pass MRC data in for recovery mode boot, |
| 333 | * Always pass it in for S3 resume. |
| 334 | */ |
| 335 | if (!recovery_mode_enabled() || pei_data->boot_mode == 2) |
| 336 | prepare_mrc_cache(pei_data); |
| 337 | |
| 338 | /* If MRC data is not found we cannot continue S3 resume. */ |
| 339 | if (pei_data->boot_mode == 2 && !pei_data->mrc_input) { |
| 340 | outb(0x6, 0xcf9); |
| 341 | hlt(); |
| 342 | } |
| 343 | #endif |
| 344 | |
| 345 | /* Locate and call UEFI System Agent binary. */ |
| 346 | entry = (unsigned long)cbfs_find_file(target, 0xab); |
| 347 | if (entry) { |
| 348 | int rv; |
| 349 | asm volatile ( |
| 350 | "call *%%ecx\n\t" |
| 351 | :"=a" (rv) : "c" (entry), "a" (pei_data)); |
| 352 | if (rv) { |
| 353 | printk(BIOS_ERR, "MRC returned %d\n", rv); |
| 354 | die("Nonzero MRC return value\n"); |
| 355 | } |
| 356 | } else { |
| 357 | die("UEFI PEI System Agent not found.\n"); |
| 358 | } |
| 359 | |
| 360 | /* For reference print the System Agent version |
| 361 | * after executing the UEFI PEI stage. |
| 362 | */ |
| 363 | u32 version = MCHBAR32(0x5034); |
| 364 | printk(BIOS_DEBUG, "System Agent Version %d.%d.%d Build %d\n", |
| 365 | version >> 24 , (version >> 16) & 0xff, |
| 366 | (version >> 8) & 0xff, version & 0xff); |
| 367 | |
| 368 | intel_early_me_init_done(ME_INIT_STATUS_SUCCESS); |
| 369 | |
| 370 | report_memory_config(); |
| 371 | |
| 372 | /* S3 resume: don't save scrambler seed or MRC data */ |
| 373 | if (pei_data->boot_mode != 2) |
| 374 | save_mrc_data(pei_data); |
| 375 | } |
| 376 | |
| 377 | struct cbmem_entry *get_cbmem_toc(void) |
| 378 | { |
| 379 | return (struct cbmem_entry *)(get_top_of_ram() - HIGH_MEMORY_SIZE); |
| 380 | } |
| 381 | |
| 382 | unsigned long get_top_of_ram(void) |
| 383 | { |
| 384 | /* Base of TSEG is top of usable DRAM */ |
| 385 | u32 tom = pci_read_config32(PCI_DEV(0,0,0), TSEG); |
| 386 | return (unsigned long) tom; |
| 387 | } |