Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [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. |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <console/console.h> |
| 17 | #include <console/usb.h> |
| 18 | #include <bootmode.h> |
| 19 | #include <string.h> |
| 20 | #include <arch/io.h> |
| 21 | #include <cbmem.h> |
| 22 | #include <arch/cbfs.h> |
| 23 | #include <cbfs.h> |
| 24 | #include <ip_checksum.h> |
| 25 | #include <pc80/mc146818rtc.h> |
| 26 | #include <device/pci_def.h> |
Arthur Heymans | 7539b8c | 2017-12-24 10:42:57 +0100 | [diff] [blame] | 27 | #include <mrc_cache.h> |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 28 | #include <halt.h> |
Vladimir Serbinenko | ffbb3c0 | 2016-02-10 01:36:25 +0100 | [diff] [blame] | 29 | #include <timestamp.h> |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 30 | #include "raminit.h" |
| 31 | #include "pei_data.h" |
| 32 | #include "sandybridge.h" |
Philipp Deppenwiese | fea2429 | 2017-10-17 17:02:29 +0200 | [diff] [blame] | 33 | #include <security/vboot/vboot_common.h> |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 34 | |
| 35 | /* Management Engine is in the southbridge */ |
Elyes HAOUAS | 21b71ce6 | 2018-06-16 18:43:52 +0200 | [diff] [blame] | 36 | #include <southbridge/intel/bd82x6x/me.h> |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 37 | |
| 38 | /* |
| 39 | * MRC scrambler seed offsets should be reserved in |
| 40 | * mainboard cmos.layout and not covered by checksum. |
| 41 | */ |
Martin Roth | 3323260 | 2017-06-24 14:48:50 -0600 | [diff] [blame] | 42 | #if IS_ENABLED(CONFIG_USE_OPTION_TABLE) |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 43 | #include "option_table.h" |
| 44 | #define CMOS_OFFSET_MRC_SEED (CMOS_VSTART_mrc_scrambler_seed >> 3) |
| 45 | #define CMOS_OFFSET_MRC_SEED_S3 (CMOS_VSTART_mrc_scrambler_seed_s3 >> 3) |
| 46 | #define CMOS_OFFSET_MRC_SEED_CHK (CMOS_VSTART_mrc_scrambler_seed_chk >> 3) |
| 47 | #else |
| 48 | #define CMOS_OFFSET_MRC_SEED 152 |
| 49 | #define CMOS_OFFSET_MRC_SEED_S3 156 |
| 50 | #define CMOS_OFFSET_MRC_SEED_CHK 160 |
| 51 | #endif |
| 52 | |
Arthur Heymans | 7539b8c | 2017-12-24 10:42:57 +0100 | [diff] [blame] | 53 | #define MRC_CACHE_VERSION 0 |
| 54 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 55 | void save_mrc_data(struct pei_data *pei_data) |
| 56 | { |
| 57 | u16 c1, c2, checksum; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 58 | |
| 59 | /* Save the MRC S3 restore data to cbmem */ |
Arthur Heymans | 7539b8c | 2017-12-24 10:42:57 +0100 | [diff] [blame] | 60 | mrc_cache_stash_data(MRC_TRAINING_DATA, MRC_CACHE_VERSION, |
| 61 | pei_data->mrc_output, |
| 62 | pei_data->mrc_output_len); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 63 | |
| 64 | /* Save the MRC seed values to CMOS */ |
| 65 | cmos_write32(CMOS_OFFSET_MRC_SEED, pei_data->scrambler_seed); |
| 66 | printk(BIOS_DEBUG, "Save scrambler seed 0x%08x to CMOS 0x%02x\n", |
| 67 | pei_data->scrambler_seed, CMOS_OFFSET_MRC_SEED); |
| 68 | |
| 69 | cmos_write32(CMOS_OFFSET_MRC_SEED_S3, pei_data->scrambler_seed_s3); |
| 70 | printk(BIOS_DEBUG, "Save s3 scrambler seed 0x%08x to CMOS 0x%02x\n", |
| 71 | pei_data->scrambler_seed_s3, CMOS_OFFSET_MRC_SEED_S3); |
| 72 | |
| 73 | /* Save a simple checksum of the seed values */ |
| 74 | c1 = compute_ip_checksum((u8*)&pei_data->scrambler_seed, |
| 75 | sizeof(u32)); |
| 76 | c2 = compute_ip_checksum((u8*)&pei_data->scrambler_seed_s3, |
| 77 | sizeof(u32)); |
| 78 | checksum = add_ip_checksums(sizeof(u32), c1, c2); |
| 79 | |
| 80 | cmos_write(checksum & 0xff, CMOS_OFFSET_MRC_SEED_CHK); |
| 81 | cmos_write((checksum >> 8) & 0xff, CMOS_OFFSET_MRC_SEED_CHK+1); |
| 82 | } |
| 83 | |
| 84 | static void prepare_mrc_cache(struct pei_data *pei_data) |
| 85 | { |
Arthur Heymans | 7539b8c | 2017-12-24 10:42:57 +0100 | [diff] [blame] | 86 | struct region_device rdev; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 87 | u16 c1, c2, checksum, seed_checksum; |
| 88 | |
| 89 | // preset just in case there is an error |
| 90 | pei_data->mrc_input = NULL; |
| 91 | pei_data->mrc_input_len = 0; |
| 92 | |
| 93 | /* Read scrambler seeds from CMOS */ |
| 94 | pei_data->scrambler_seed = cmos_read32(CMOS_OFFSET_MRC_SEED); |
| 95 | printk(BIOS_DEBUG, "Read scrambler seed 0x%08x from CMOS 0x%02x\n", |
| 96 | pei_data->scrambler_seed, CMOS_OFFSET_MRC_SEED); |
| 97 | |
| 98 | pei_data->scrambler_seed_s3 = cmos_read32(CMOS_OFFSET_MRC_SEED_S3); |
| 99 | printk(BIOS_DEBUG, "Read S3 scrambler seed 0x%08x from CMOS 0x%02x\n", |
| 100 | pei_data->scrambler_seed_s3, CMOS_OFFSET_MRC_SEED_S3); |
| 101 | |
| 102 | /* Compute seed checksum and compare */ |
| 103 | c1 = compute_ip_checksum((u8*)&pei_data->scrambler_seed, |
| 104 | sizeof(u32)); |
| 105 | c2 = compute_ip_checksum((u8*)&pei_data->scrambler_seed_s3, |
| 106 | sizeof(u32)); |
| 107 | checksum = add_ip_checksums(sizeof(u32), c1, c2); |
| 108 | |
| 109 | seed_checksum = cmos_read(CMOS_OFFSET_MRC_SEED_CHK); |
| 110 | seed_checksum |= cmos_read(CMOS_OFFSET_MRC_SEED_CHK+1) << 8; |
| 111 | |
| 112 | if (checksum != seed_checksum) { |
| 113 | printk(BIOS_ERR, "%s: invalid seed checksum\n", __func__); |
| 114 | pei_data->scrambler_seed = 0; |
| 115 | pei_data->scrambler_seed_s3 = 0; |
| 116 | return; |
| 117 | } |
| 118 | |
Arthur Heymans | 7539b8c | 2017-12-24 10:42:57 +0100 | [diff] [blame] | 119 | if (mrc_cache_get_current(MRC_TRAINING_DATA, MRC_CACHE_VERSION, |
| 120 | &rdev)) { |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 121 | /* error message printed in find_current_mrc_cache */ |
| 122 | return; |
| 123 | } |
| 124 | |
Arthur Heymans | 7539b8c | 2017-12-24 10:42:57 +0100 | [diff] [blame] | 125 | pei_data->mrc_input = rdev_mmap_full(&rdev); |
| 126 | pei_data->mrc_input_len = region_device_sz(&rdev); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 127 | |
Arthur Heymans | 7539b8c | 2017-12-24 10:42:57 +0100 | [diff] [blame] | 128 | printk(BIOS_DEBUG, "%s: at %p, size %x\n", |
| 129 | __func__, pei_data->mrc_input, pei_data->mrc_input_len); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Elyes HAOUAS | 448d9fb | 2018-05-22 12:51:27 +0200 | [diff] [blame] | 132 | static const char *ecc_decoder[] = { |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 133 | "inactive", |
| 134 | "active on IO", |
| 135 | "disabled on IO", |
| 136 | "active" |
| 137 | }; |
| 138 | |
| 139 | /* |
| 140 | * Dump in the log memory controller configuration as read from the memory |
| 141 | * controller registers. |
| 142 | */ |
| 143 | static void report_memory_config(void) |
| 144 | { |
| 145 | u32 addr_decoder_common, addr_decode_ch[2]; |
| 146 | int i; |
| 147 | |
| 148 | addr_decoder_common = MCHBAR32(0x5000); |
| 149 | addr_decode_ch[0] = MCHBAR32(0x5004); |
| 150 | addr_decode_ch[1] = MCHBAR32(0x5008); |
| 151 | |
| 152 | printk(BIOS_DEBUG, "memcfg DDR3 clock %d MHz\n", |
| 153 | (MCHBAR32(0x5e04) * 13333 * 2 + 50)/100); |
| 154 | printk(BIOS_DEBUG, "memcfg channel assignment: A: %d, B % d, C % d\n", |
| 155 | addr_decoder_common & 3, |
| 156 | (addr_decoder_common >> 2) & 3, |
| 157 | (addr_decoder_common >> 4) & 3); |
| 158 | |
| 159 | for (i = 0; i < ARRAY_SIZE(addr_decode_ch); i++) { |
| 160 | u32 ch_conf = addr_decode_ch[i]; |
| 161 | printk(BIOS_DEBUG, "memcfg channel[%d] config (%8.8x):\n", |
| 162 | i, ch_conf); |
| 163 | printk(BIOS_DEBUG, " ECC %s\n", |
| 164 | ecc_decoder[(ch_conf >> 24) & 3]); |
| 165 | printk(BIOS_DEBUG, " enhanced interleave mode %s\n", |
| 166 | ((ch_conf >> 22) & 1) ? "on" : "off"); |
| 167 | printk(BIOS_DEBUG, " rank interleave %s\n", |
| 168 | ((ch_conf >> 21) & 1) ? "on" : "off"); |
| 169 | printk(BIOS_DEBUG, " DIMMA %d MB width x%d %s rank%s\n", |
| 170 | ((ch_conf >> 0) & 0xff) * 256, |
| 171 | ((ch_conf >> 19) & 1) ? 16 : 8, |
| 172 | ((ch_conf >> 17) & 1) ? "dual" : "single", |
| 173 | ((ch_conf >> 16) & 1) ? "" : ", selected"); |
| 174 | printk(BIOS_DEBUG, " DIMMB %d MB width x%d %s rank%s\n", |
| 175 | ((ch_conf >> 8) & 0xff) * 256, |
| 176 | ((ch_conf >> 20) & 1) ? 16 : 8, |
| 177 | ((ch_conf >> 18) & 1) ? "dual" : "single", |
| 178 | ((ch_conf >> 16) & 1) ? ", selected" : ""); |
| 179 | } |
| 180 | } |
| 181 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 182 | /** |
| 183 | * Find PEI executable in coreboot filesystem and execute it. |
| 184 | * |
| 185 | * @param pei_data: configuration data for UEFI PEI reference code |
| 186 | */ |
| 187 | void sdram_initialize(struct pei_data *pei_data) |
| 188 | { |
| 189 | struct sys_info sysinfo; |
Stefan Reinauer | 6a00113 | 2017-07-13 02:20:27 +0200 | [diff] [blame] | 190 | int (*entry) (struct pei_data *pei_data) __attribute__((regparm(1))); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 191 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 192 | /* Wait for ME to be ready */ |
| 193 | intel_early_me_init(); |
| 194 | intel_early_me_uma_size(); |
| 195 | |
| 196 | printk(BIOS_DEBUG, "Starting UEFI PEI System Agent\n"); |
| 197 | |
| 198 | memset(&sysinfo, 0, sizeof(sysinfo)); |
| 199 | |
| 200 | sysinfo.boot_path = pei_data->boot_mode; |
| 201 | |
| 202 | /* |
| 203 | * Do not pass MRC data in for recovery mode boot, |
| 204 | * Always pass it in for S3 resume. |
| 205 | */ |
Furquan Shaikh | 0325dc6 | 2016-07-25 13:02:36 -0700 | [diff] [blame] | 206 | if (!vboot_recovery_mode_enabled() || pei_data->boot_mode == 2) |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 207 | prepare_mrc_cache(pei_data); |
| 208 | |
| 209 | /* If MRC data is not found we cannot continue S3 resume. */ |
| 210 | if (pei_data->boot_mode == 2 && !pei_data->mrc_input) { |
| 211 | printk(BIOS_DEBUG, "Giving up in sdram_initialize: No MRC data\n"); |
| 212 | outb(0x6, 0xcf9); |
| 213 | halt(); |
| 214 | } |
| 215 | |
| 216 | /* Pass console handler in pei_data */ |
| 217 | pei_data->tx_byte = do_putchar; |
| 218 | |
| 219 | /* Locate and call UEFI System Agent binary. */ |
| 220 | entry = cbfs_boot_map_with_leak("mrc.bin", CBFS_TYPE_MRC, NULL); |
| 221 | if (entry) { |
| 222 | int rv; |
| 223 | rv = entry (pei_data); |
| 224 | if (rv) { |
| 225 | switch (rv) { |
| 226 | case -1: |
| 227 | printk(BIOS_ERR, "PEI version mismatch.\n"); |
| 228 | break; |
| 229 | case -2: |
| 230 | printk(BIOS_ERR, "Invalid memory frequency.\n"); |
| 231 | break; |
| 232 | default: |
| 233 | printk(BIOS_ERR, "MRC returned %x.\n", rv); |
| 234 | } |
| 235 | die("Nonzero MRC return value.\n"); |
| 236 | } |
| 237 | } else { |
| 238 | die("UEFI PEI System Agent not found.\n"); |
| 239 | } |
| 240 | |
Martin Roth | 3323260 | 2017-06-24 14:48:50 -0600 | [diff] [blame] | 241 | #if IS_ENABLED(CONFIG_USBDEBUG_IN_ROMSTAGE) |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 242 | /* mrc.bin reconfigures USB, so reinit it to have debug */ |
| 243 | usbdebug_init(); |
| 244 | #endif |
| 245 | |
| 246 | /* For reference print the System Agent version |
| 247 | * after executing the UEFI PEI stage. |
| 248 | */ |
| 249 | u32 version = MCHBAR32(0x5034); |
| 250 | printk(BIOS_DEBUG, "System Agent Version %d.%d.%d Build %d\n", |
Elyes HAOUAS | a342f39 | 2018-10-17 10:56:26 +0200 | [diff] [blame] | 251 | version >> 24, (version >> 16) & 0xff, |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 252 | (version >> 8) & 0xff, version & 0xff); |
| 253 | |
| 254 | /* Send ME init done for SandyBridge here. This is done |
| 255 | * inside the SystemAgent binary on IvyBridge. */ |
| 256 | if (BASE_REV_SNB == |
| 257 | (pci_read_config16(PCI_CPU_DEVICE, PCI_DEVICE_ID) & BASE_REV_MASK)) |
| 258 | intel_early_me_init_done(ME_INIT_STATUS_SUCCESS); |
| 259 | else |
| 260 | intel_early_me_status(); |
| 261 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 262 | report_memory_config(); |
| 263 | } |
Vladimir Serbinenko | ffbb3c0 | 2016-02-10 01:36:25 +0100 | [diff] [blame] | 264 | |
| 265 | void perform_raminit(int s3resume) |
| 266 | { |
| 267 | int cbmem_was_initted; |
| 268 | struct pei_data pei_data; |
| 269 | |
| 270 | /* Prepare USB controller early in S3 resume */ |
| 271 | if (!mainboard_should_reset_usb(s3resume)) |
| 272 | enable_usb_bar(); |
| 273 | |
| 274 | mainboard_fill_pei_data(&pei_data); |
| 275 | |
| 276 | post_code(0x3a); |
| 277 | pei_data.boot_mode = s3resume ? 2 : 0; |
| 278 | timestamp_add_now(TS_BEFORE_INITRAM); |
| 279 | sdram_initialize(&pei_data); |
| 280 | cbmem_was_initted = !cbmem_recovery(s3resume); |
| 281 | if (!s3resume) |
| 282 | save_mrc_data(&pei_data); |
| 283 | |
| 284 | if (s3resume && !cbmem_was_initted) { |
| 285 | /* Failed S3 resume, reset to come up cleanly */ |
| 286 | outb(0x6, 0xcf9); |
| 287 | halt(); |
| 288 | } |
| 289 | } |