Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [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. |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <console/console.h> |
Kyösti Mälkki | 5687fc9 | 2013-11-28 18:11:49 +0200 | [diff] [blame] | 17 | #include <bootmode.h> |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 18 | #include <string.h> |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 19 | #include <arch/io.h> |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 20 | #include <cbmem.h> |
| 21 | #include <arch/cbfs.h> |
| 22 | #include <cbfs.h> |
Patrick Georgi | bd79c5e | 2014-11-28 22:35:36 +0100 | [diff] [blame] | 23 | #include <halt.h> |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 24 | #include <ip_checksum.h> |
Alexander Couzens | 81c5c76 | 2016-03-09 03:13:45 +0100 | [diff] [blame^] | 25 | #include <northbridge/intel/common/mrc_cache.h> |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 26 | #include <pc80/mc146818rtc.h> |
| 27 | #include <device/pci_def.h> |
| 28 | #include "raminit.h" |
| 29 | #include "pei_data.h" |
| 30 | #include "haswell.h" |
| 31 | |
Aaron Durbin | 2ad1dba | 2013-02-07 00:51:18 -0600 | [diff] [blame] | 32 | void save_mrc_data(struct pei_data *pei_data) |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 33 | { |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 34 | struct mrc_data_container *mrcdata; |
| 35 | int output_len = ALIGN(pei_data->mrc_output_len, 16); |
| 36 | |
| 37 | /* Save the MRC S3 restore data to cbmem */ |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 38 | mrcdata = cbmem_add |
| 39 | (CBMEM_ID_MRCDATA, |
| 40 | output_len + sizeof(struct mrc_data_container)); |
| 41 | |
| 42 | printk(BIOS_DEBUG, "Relocate MRC DATA from %p to %p (%u bytes)\n", |
| 43 | pei_data->mrc_output, mrcdata, output_len); |
| 44 | |
| 45 | mrcdata->mrc_signature = MRC_DATA_SIGNATURE; |
| 46 | mrcdata->mrc_data_size = output_len; |
| 47 | mrcdata->reserved = 0; |
| 48 | memcpy(mrcdata->mrc_data, pei_data->mrc_output, |
| 49 | pei_data->mrc_output_len); |
| 50 | |
| 51 | /* Zero the unused space in aligned buffer. */ |
| 52 | if (output_len > pei_data->mrc_output_len) |
| 53 | memset(mrcdata->mrc_data+pei_data->mrc_output_len, 0, |
| 54 | output_len - pei_data->mrc_output_len); |
| 55 | |
| 56 | mrcdata->mrc_checksum = compute_ip_checksum(mrcdata->mrc_data, |
| 57 | mrcdata->mrc_data_size); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | static void prepare_mrc_cache(struct pei_data *pei_data) |
| 61 | { |
| 62 | struct mrc_data_container *mrc_cache; |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 63 | |
| 64 | // preset just in case there is an error |
| 65 | pei_data->mrc_input = NULL; |
| 66 | pei_data->mrc_input_len = 0; |
| 67 | |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 68 | if ((mrc_cache = find_current_mrc_cache()) == NULL) { |
| 69 | /* error message printed in find_current_mrc_cache */ |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | pei_data->mrc_input = mrc_cache->mrc_data; |
| 74 | pei_data->mrc_input_len = mrc_cache->mrc_data_size; |
| 75 | |
| 76 | printk(BIOS_DEBUG, "%s: at %p, size %x checksum %04x\n", |
| 77 | __func__, pei_data->mrc_input, |
| 78 | pei_data->mrc_input_len, mrc_cache->mrc_checksum); |
| 79 | } |
| 80 | |
| 81 | static const char* ecc_decoder[] = { |
| 82 | "inactive", |
| 83 | "active on IO", |
| 84 | "disabled on IO", |
| 85 | "active" |
| 86 | }; |
| 87 | |
| 88 | /* |
| 89 | * Dump in the log memory controller configuration as read from the memory |
| 90 | * controller registers. |
| 91 | */ |
| 92 | static void report_memory_config(void) |
| 93 | { |
| 94 | u32 addr_decoder_common, addr_decode_ch[2]; |
| 95 | int i; |
| 96 | |
| 97 | addr_decoder_common = MCHBAR32(0x5000); |
| 98 | addr_decode_ch[0] = MCHBAR32(0x5004); |
| 99 | addr_decode_ch[1] = MCHBAR32(0x5008); |
| 100 | |
| 101 | printk(BIOS_DEBUG, "memcfg DDR3 clock %d MHz\n", |
| 102 | (MCHBAR32(0x5e04) * 13333 * 2 + 50)/100); |
| 103 | printk(BIOS_DEBUG, "memcfg channel assignment: A: %d, B % d, C % d\n", |
| 104 | addr_decoder_common & 3, |
| 105 | (addr_decoder_common >> 2) & 3, |
| 106 | (addr_decoder_common >> 4) & 3); |
| 107 | |
| 108 | for (i = 0; i < ARRAY_SIZE(addr_decode_ch); i++) { |
| 109 | u32 ch_conf = addr_decode_ch[i]; |
| 110 | printk(BIOS_DEBUG, "memcfg channel[%d] config (%8.8x):\n", |
| 111 | i, ch_conf); |
| 112 | printk(BIOS_DEBUG, " ECC %s\n", |
| 113 | ecc_decoder[(ch_conf >> 24) & 3]); |
| 114 | printk(BIOS_DEBUG, " enhanced interleave mode %s\n", |
| 115 | ((ch_conf >> 22) & 1) ? "on" : "off"); |
| 116 | printk(BIOS_DEBUG, " rank interleave %s\n", |
| 117 | ((ch_conf >> 21) & 1) ? "on" : "off"); |
Duncan Laurie | 8d77402 | 2013-10-22 16:32:49 -0700 | [diff] [blame] | 118 | printk(BIOS_DEBUG, " DIMMA %d MB width %s %s rank%s\n", |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 119 | ((ch_conf >> 0) & 0xff) * 256, |
Duncan Laurie | 8d77402 | 2013-10-22 16:32:49 -0700 | [diff] [blame] | 120 | ((ch_conf >> 19) & 1) ? "x16" : "x8 or x32", |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 121 | ((ch_conf >> 17) & 1) ? "dual" : "single", |
| 122 | ((ch_conf >> 16) & 1) ? "" : ", selected"); |
Duncan Laurie | 8d77402 | 2013-10-22 16:32:49 -0700 | [diff] [blame] | 123 | printk(BIOS_DEBUG, " DIMMB %d MB width %s %s rank%s\n", |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 124 | ((ch_conf >> 8) & 0xff) * 256, |
Duncan Laurie | 8d77402 | 2013-10-22 16:32:49 -0700 | [diff] [blame] | 125 | ((ch_conf >> 19) & 1) ? "x16" : "x8 or x32", |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 126 | ((ch_conf >> 18) & 1) ? "dual" : "single", |
| 127 | ((ch_conf >> 16) & 1) ? ", selected" : ""); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Find PEI executable in coreboot filesystem and execute it. |
| 133 | * |
| 134 | * @param pei_data: configuration data for UEFI PEI reference code |
| 135 | */ |
| 136 | void sdram_initialize(struct pei_data *pei_data) |
| 137 | { |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 138 | unsigned long entry; |
| 139 | |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 140 | printk(BIOS_DEBUG, "Starting UEFI PEI System Agent\n"); |
| 141 | |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 142 | /* |
| 143 | * Do not pass MRC data in for recovery mode boot, |
| 144 | * Always pass it in for S3 resume. |
| 145 | */ |
| 146 | if (!recovery_mode_enabled() || pei_data->boot_mode == 2) |
| 147 | prepare_mrc_cache(pei_data); |
| 148 | |
| 149 | /* If MRC data is not found we cannot continue S3 resume. */ |
| 150 | if (pei_data->boot_mode == 2 && !pei_data->mrc_input) { |
Duncan Laurie | 727b545 | 2013-08-08 16:28:41 -0700 | [diff] [blame] | 151 | post_code(POST_RESUME_FAILURE); |
| 152 | printk(BIOS_DEBUG, "Giving up in sdram_initialize: " |
| 153 | "No MRC data\n"); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 154 | outb(0x6, 0xcf9); |
Patrick Georgi | bd79c5e | 2014-11-28 22:35:36 +0100 | [diff] [blame] | 155 | halt(); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /* Pass console handler in pei_data */ |
Kyösti Mälkki | 657e0be | 2014-02-04 19:03:57 +0200 | [diff] [blame] | 159 | pei_data->tx_byte = do_putchar; |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 160 | |
| 161 | /* Locate and call UEFI System Agent binary. */ |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 162 | entry = (unsigned long)cbfs_boot_map_with_leak("mrc.bin", |
| 163 | CBFS_TYPE_MRC, NULL); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 164 | if (entry) { |
| 165 | int rv; |
| 166 | asm volatile ( |
| 167 | "call *%%ecx\n\t" |
| 168 | :"=a" (rv) : "c" (entry), "a" (pei_data)); |
| 169 | if (rv) { |
| 170 | switch (rv) { |
| 171 | case -1: |
| 172 | printk(BIOS_ERR, "PEI version mismatch.\n"); |
| 173 | break; |
| 174 | case -2: |
| 175 | printk(BIOS_ERR, "Invalid memory frequency.\n"); |
| 176 | break; |
| 177 | default: |
| 178 | printk(BIOS_ERR, "MRC returned %x.\n", rv); |
| 179 | } |
| 180 | die("Nonzero MRC return value.\n"); |
| 181 | } |
| 182 | } else { |
| 183 | die("UEFI PEI System Agent not found.\n"); |
| 184 | } |
| 185 | |
| 186 | /* For reference print the System Agent version |
| 187 | * after executing the UEFI PEI stage. |
| 188 | */ |
| 189 | u32 version = MCHBAR32(0x5034); |
| 190 | printk(BIOS_DEBUG, "System Agent Version %d.%d.%d Build %d\n", |
| 191 | version >> 24 , (version >> 16) & 0xff, |
| 192 | (version >> 8) & 0xff, version & 0xff); |
| 193 | |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 194 | report_memory_config(); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 195 | } |