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> |
Duncan Laurie | 7b508dd | 2012-04-09 12:30:43 -0700 | [diff] [blame] | 30 | #include <device/pci_def.h> |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 31 | #include "raminit.h" |
| 32 | #include "pei_data.h" |
| 33 | #include "sandybridge.h" |
| 34 | |
| 35 | /* Management Engine is in the southbridge */ |
| 36 | #include "southbridge/intel/bd82x6x/me.h" |
| 37 | #if CONFIG_CHROMEOS |
| 38 | #include <vendorcode/google/chromeos/chromeos.h> |
Stefan Reinauer | 1244f4b | 2012-05-10 11:31:40 -0700 | [diff] [blame] | 39 | #else |
| 40 | #define recovery_mode_enabled(x) 0 |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 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 |
Duncan Laurie | cf81b82 | 2012-08-08 13:43:55 -0700 | [diff] [blame^] | 53 | #define CMOS_OFFSET_MRC_SEED 152 |
| 54 | #define CMOS_OFFSET_MRC_SEED_S3 156 |
| 55 | #define CMOS_OFFSET_MRC_SEED_CHK 160 |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 56 | #endif |
| 57 | |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 58 | static void save_mrc_data(struct pei_data *pei_data) |
| 59 | { |
| 60 | u16 c1, c2, checksum; |
| 61 | |
| 62 | #if CONFIG_EARLY_CBMEM_INIT |
| 63 | struct mrc_data_container *mrcdata; |
| 64 | int output_len = ALIGN(pei_data->mrc_output_len, 16); |
| 65 | |
| 66 | /* Save the MRC S3 restore data to cbmem */ |
| 67 | cbmem_initialize(); |
| 68 | mrcdata = cbmem_add |
| 69 | (CBMEM_ID_MRCDATA, |
| 70 | output_len + sizeof(struct mrc_data_container)); |
| 71 | |
| 72 | printk(BIOS_DEBUG, "Relocate MRC DATA from %p to %p (%u bytes)\n", |
| 73 | pei_data->mrc_output, mrcdata, output_len); |
| 74 | |
| 75 | mrcdata->mrc_signature = MRC_DATA_SIGNATURE; |
| 76 | mrcdata->mrc_data_size = output_len; |
| 77 | mrcdata->reserved = 0; |
| 78 | memcpy(mrcdata->mrc_data, pei_data->mrc_output, |
| 79 | pei_data->mrc_output_len); |
| 80 | |
| 81 | /* Zero the unused space in aligned buffer. */ |
| 82 | if (output_len > pei_data->mrc_output_len) |
| 83 | memset(mrcdata->mrc_data+pei_data->mrc_output_len, 0, |
| 84 | output_len - pei_data->mrc_output_len); |
| 85 | |
| 86 | mrcdata->mrc_checksum = compute_ip_checksum(mrcdata->mrc_data, |
| 87 | mrcdata->mrc_data_size); |
| 88 | #endif |
| 89 | |
| 90 | /* Save the MRC seed values to CMOS */ |
| 91 | cmos_write32(CMOS_OFFSET_MRC_SEED, pei_data->scrambler_seed); |
| 92 | printk(BIOS_DEBUG, "Save scrambler seed 0x%08x to CMOS 0x%02x\n", |
| 93 | pei_data->scrambler_seed, CMOS_OFFSET_MRC_SEED); |
| 94 | |
| 95 | cmos_write32(CMOS_OFFSET_MRC_SEED_S3, pei_data->scrambler_seed_s3); |
| 96 | printk(BIOS_DEBUG, "Save s3 scrambler seed 0x%08x to CMOS 0x%02x\n", |
| 97 | pei_data->scrambler_seed_s3, CMOS_OFFSET_MRC_SEED_S3); |
| 98 | |
| 99 | /* Save a simple checksum of the seed values */ |
| 100 | c1 = compute_ip_checksum((u8*)&pei_data->scrambler_seed, |
| 101 | sizeof(u32)); |
| 102 | c2 = compute_ip_checksum((u8*)&pei_data->scrambler_seed_s3, |
| 103 | sizeof(u32)); |
| 104 | checksum = add_ip_checksums(sizeof(u32), c1, c2); |
| 105 | |
| 106 | cmos_write(checksum & 0xff, CMOS_OFFSET_MRC_SEED_CHK); |
| 107 | cmos_write((checksum >> 8) & 0xff, CMOS_OFFSET_MRC_SEED_CHK+1); |
| 108 | } |
| 109 | |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 110 | static void prepare_mrc_cache(struct pei_data *pei_data) |
| 111 | { |
Stefan Reinauer | 1244f4b | 2012-05-10 11:31:40 -0700 | [diff] [blame] | 112 | struct mrc_data_container *mrc_cache; |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 113 | u16 c1, c2, checksum, seed_checksum; |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 114 | |
| 115 | // preset just in case there is an error |
| 116 | pei_data->mrc_input = NULL; |
| 117 | pei_data->mrc_input_len = 0; |
| 118 | |
| 119 | /* Read scrambler seeds from CMOS */ |
| 120 | pei_data->scrambler_seed = cmos_read32(CMOS_OFFSET_MRC_SEED); |
| 121 | printk(BIOS_DEBUG, "Read scrambler seed 0x%08x from CMOS 0x%02x\n", |
| 122 | pei_data->scrambler_seed, CMOS_OFFSET_MRC_SEED); |
| 123 | |
| 124 | pei_data->scrambler_seed_s3 = cmos_read32(CMOS_OFFSET_MRC_SEED_S3); |
| 125 | printk(BIOS_DEBUG, "Read S3 scrambler seed 0x%08x from CMOS 0x%02x\n", |
| 126 | pei_data->scrambler_seed_s3, CMOS_OFFSET_MRC_SEED_S3); |
| 127 | |
| 128 | /* Compute seed checksum and compare */ |
| 129 | c1 = compute_ip_checksum((u8*)&pei_data->scrambler_seed, |
| 130 | sizeof(u32)); |
| 131 | c2 = compute_ip_checksum((u8*)&pei_data->scrambler_seed_s3, |
| 132 | sizeof(u32)); |
| 133 | checksum = add_ip_checksums(sizeof(u32), c1, c2); |
| 134 | |
| 135 | seed_checksum = cmos_read(CMOS_OFFSET_MRC_SEED_CHK); |
| 136 | seed_checksum |= cmos_read(CMOS_OFFSET_MRC_SEED_CHK+1) << 8; |
| 137 | |
| 138 | if (checksum != seed_checksum) { |
| 139 | printk(BIOS_ERR, "%s: invalid seed checksum\n", __func__); |
| 140 | pei_data->scrambler_seed = 0; |
| 141 | pei_data->scrambler_seed_s3 = 0; |
| 142 | return; |
| 143 | } |
| 144 | |
Stefan Reinauer | 1244f4b | 2012-05-10 11:31:40 -0700 | [diff] [blame] | 145 | if ((mrc_cache = find_current_mrc_cache()) == NULL) { |
| 146 | /* error message printed in find_current_mrc_cache */ |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 147 | return; |
| 148 | } |
| 149 | |
| 150 | pei_data->mrc_input = mrc_cache->mrc_data; |
| 151 | pei_data->mrc_input_len = mrc_cache->mrc_data_size; |
| 152 | |
Stefan Reinauer | 1244f4b | 2012-05-10 11:31:40 -0700 | [diff] [blame] | 153 | printk(BIOS_DEBUG, "%s: at %p, size %x checksum %04x\n", |
| 154 | __func__, pei_data->mrc_input, |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 155 | pei_data->mrc_input_len, mrc_cache->mrc_checksum); |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 156 | } |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 157 | |
| 158 | static const char* ecc_decoder[] = { |
| 159 | "inactive", |
| 160 | "active on IO", |
| 161 | "disabled on IO", |
| 162 | "active" |
| 163 | }; |
| 164 | |
| 165 | /* |
| 166 | * Dump in the log memory controller configuration as read from the memory |
| 167 | * controller registers. |
| 168 | */ |
| 169 | static void report_memory_config(void) |
| 170 | { |
| 171 | u32 addr_decoder_common, addr_decode_ch[2]; |
| 172 | int i; |
| 173 | |
| 174 | addr_decoder_common = MCHBAR32(0x5000); |
| 175 | addr_decode_ch[0] = MCHBAR32(0x5004); |
| 176 | addr_decode_ch[1] = MCHBAR32(0x5008); |
| 177 | |
| 178 | printk(BIOS_DEBUG, "memcfg DDR3 clock %d MHz\n", |
| 179 | (MCHBAR32(0x5e04) * 13333 * 2 + 50)/100); |
| 180 | printk(BIOS_DEBUG, "memcfg channel assignment: A: %d, B % d, C % d\n", |
| 181 | addr_decoder_common & 3, |
| 182 | (addr_decoder_common >> 2) & 3, |
| 183 | (addr_decoder_common >> 4) & 3); |
| 184 | |
| 185 | for (i = 0; i < ARRAY_SIZE(addr_decode_ch); i++) { |
| 186 | u32 ch_conf = addr_decode_ch[i]; |
| 187 | printk(BIOS_DEBUG, "memcfg channel[%d] config (%8.8x):\n", |
| 188 | i, ch_conf); |
| 189 | printk(BIOS_DEBUG, " ECC %s\n", |
| 190 | ecc_decoder[(ch_conf >> 24) & 3]); |
| 191 | printk(BIOS_DEBUG, " enhanced interleave mode %s\n", |
| 192 | ((ch_conf >> 22) & 1) ? "on" : "off"); |
| 193 | printk(BIOS_DEBUG, " rank interleave %s\n", |
| 194 | ((ch_conf >> 21) & 1) ? "on" : "off"); |
| 195 | printk(BIOS_DEBUG, " DIMMA %d MB width x%d %s rank%s\n", |
| 196 | ((ch_conf >> 0) & 0xff) * 256, |
| 197 | ((ch_conf >> 19) & 1) ? 16 : 8, |
| 198 | ((ch_conf >> 17) & 1) ? "dual" : "single", |
| 199 | ((ch_conf >> 16) & 1) ? "" : ", selected"); |
| 200 | printk(BIOS_DEBUG, " DIMMB %d MB width x%d %s rank%s\n", |
| 201 | ((ch_conf >> 8) & 0xff) * 256, |
| 202 | ((ch_conf >> 20) & 1) ? 16 : 8, |
| 203 | ((ch_conf >> 18) & 1) ? "dual" : "single", |
| 204 | ((ch_conf >> 16) & 1) ? ", selected" : ""); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Find PEI executable in coreboot filesystem and execute it. |
| 210 | * |
| 211 | * @param pei_data: configuration data for UEFI PEI reference code |
| 212 | */ |
| 213 | void sdram_initialize(struct pei_data *pei_data) |
| 214 | { |
| 215 | struct sys_info sysinfo; |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 216 | unsigned long entry; |
| 217 | |
Vadim Bendebury | 7a3f36a | 2012-04-18 15:47:32 -0700 | [diff] [blame] | 218 | report_platform_info(); |
| 219 | |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 220 | /* Wait for ME to be ready */ |
| 221 | intel_early_me_init(); |
| 222 | intel_early_me_uma_size(); |
| 223 | |
| 224 | printk(BIOS_DEBUG, "Starting UEFI PEI System Agent\n"); |
| 225 | |
| 226 | memset(&sysinfo, 0, sizeof(sysinfo)); |
| 227 | |
| 228 | sysinfo.boot_path = pei_data->boot_mode; |
| 229 | |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 230 | /* |
| 231 | * Do not pass MRC data in for recovery mode boot, |
| 232 | * Always pass it in for S3 resume. |
| 233 | */ |
| 234 | if (!recovery_mode_enabled() || pei_data->boot_mode == 2) |
| 235 | prepare_mrc_cache(pei_data); |
| 236 | |
| 237 | /* If MRC data is not found we cannot continue S3 resume. */ |
| 238 | if (pei_data->boot_mode == 2 && !pei_data->mrc_input) { |
Stefan Reinauer | 1244f4b | 2012-05-10 11:31:40 -0700 | [diff] [blame] | 239 | printk(BIOS_DEBUG, "Giving up in sdram_initialize: No MRC data\n"); |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 240 | outb(0x6, 0xcf9); |
| 241 | hlt(); |
| 242 | } |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 243 | |
| 244 | /* Locate and call UEFI System Agent binary. */ |
Stefan Reinauer | 1244f4b | 2012-05-10 11:31:40 -0700 | [diff] [blame] | 245 | entry = (unsigned long)cbfs_find_file("mrc.bin", 0xab); |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 246 | if (entry) { |
| 247 | int rv; |
| 248 | asm volatile ( |
| 249 | "call *%%ecx\n\t" |
| 250 | :"=a" (rv) : "c" (entry), "a" (pei_data)); |
| 251 | if (rv) { |
Stefan Reinauer | 696262b | 2012-06-18 14:38:40 -0700 | [diff] [blame] | 252 | switch (rv) { |
| 253 | case -1: |
| 254 | printk(BIOS_ERR, "PEI version mismatch."); |
| 255 | break; |
| 256 | case -2: |
| 257 | printk(BIOS_ERR, "Invalid memory frequency.\n"); |
| 258 | break; |
| 259 | default: |
| 260 | printk(BIOS_ERR, "MRC returned %x.\n", rv); |
| 261 | } |
| 262 | die("Nonzero MRC return value.\n"); |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 263 | } |
| 264 | } else { |
| 265 | die("UEFI PEI System Agent not found.\n"); |
| 266 | } |
| 267 | |
Sven Schnelle | d4ee808 | 2012-07-28 09:28:56 +0200 | [diff] [blame] | 268 | #if CONFIG_USBDEBUG |
| 269 | /* mrc.bin reconfigures USB, so reinit it to have debug */ |
| 270 | early_usbdebug_init(); |
| 271 | #endif |
| 272 | |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 273 | /* For reference print the System Agent version |
| 274 | * after executing the UEFI PEI stage. |
| 275 | */ |
| 276 | u32 version = MCHBAR32(0x5034); |
| 277 | printk(BIOS_DEBUG, "System Agent Version %d.%d.%d Build %d\n", |
| 278 | version >> 24 , (version >> 16) & 0xff, |
| 279 | (version >> 8) & 0xff, version & 0xff); |
| 280 | |
Duncan Laurie | 7b508dd | 2012-04-09 12:30:43 -0700 | [diff] [blame] | 281 | /* Send ME init done for SandyBridge here. This is done |
| 282 | * inside the SystemAgent binary on IvyBridge. */ |
| 283 | if (BASE_REV_SNB == |
| 284 | (pci_read_config16(PCI_CPU_DEVICE, PCI_DEVICE_ID) & BASE_REV_MASK)) |
| 285 | intel_early_me_init_done(ME_INIT_STATUS_SUCCESS); |
| 286 | else |
| 287 | intel_early_me_status(); |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 288 | |
| 289 | report_memory_config(); |
| 290 | |
| 291 | /* S3 resume: don't save scrambler seed or MRC data */ |
| 292 | if (pei_data->boot_mode != 2) |
| 293 | save_mrc_data(pei_data); |
| 294 | } |
| 295 | |
| 296 | struct cbmem_entry *get_cbmem_toc(void) |
| 297 | { |
| 298 | return (struct cbmem_entry *)(get_top_of_ram() - HIGH_MEMORY_SIZE); |
| 299 | } |
| 300 | |
| 301 | unsigned long get_top_of_ram(void) |
| 302 | { |
| 303 | /* Base of TSEG is top of usable DRAM */ |
| 304 | u32 tom = pci_read_config32(PCI_DEV(0,0,0), TSEG); |
| 305 | return (unsigned long) tom; |
| 306 | } |