Andrey Petrov | 465fc13 | 2016-02-25 14:16:33 -0800 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
Lee Leahy | 47bd2d9 | 2016-07-24 18:12:16 -0700 | [diff] [blame] | 4 | * Copyright (C) 2015-2016 Intel Corp. |
Andrey Petrov | 465fc13 | 2016-02-25 14:16:33 -0800 | [diff] [blame] | 5 | * (Written by Andrey Petrov <andrey.petrov@intel.com> for Intel Corp.) |
| 6 | * (Written by Alexandru Gagniuc <alexandrux.gagniuc@intel.com> for Intel Corp.) |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | */ |
| 13 | |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 14 | #include <security/vboot/antirollback.h> |
Aaron Durbin | b430250 | 2016-07-17 17:04:37 -0500 | [diff] [blame] | 15 | #include <arch/symbols.h> |
Aaron Durbin | 31be2c9 | 2016-12-03 22:08:20 -0600 | [diff] [blame] | 16 | #include <assert.h> |
Aaron Durbin | d04639b | 2016-07-17 23:23:59 -0500 | [diff] [blame] | 17 | #include <cbfs.h> |
Aaron Durbin | 2792868 | 2016-07-15 22:32:28 -0500 | [diff] [blame] | 18 | #include <cbmem.h> |
Patrick Rudolph | f677d17 | 2018-10-01 19:17:11 +0200 | [diff] [blame] | 19 | #include <cf9_reset.h> |
Andrey Petrov | 465fc13 | 2016-02-25 14:16:33 -0800 | [diff] [blame] | 20 | #include <console/console.h> |
Furquan Shaikh | 5aea588 | 2016-07-30 18:10:05 -0700 | [diff] [blame] | 21 | #include <elog.h> |
Andrey Petrov | 465fc13 | 2016-02-25 14:16:33 -0800 | [diff] [blame] | 22 | #include <fsp/api.h> |
| 23 | #include <fsp/util.h> |
| 24 | #include <memrange.h> |
Aaron Durbin | decd062 | 2017-12-15 12:26:40 -0700 | [diff] [blame] | 25 | #include <mrc_cache.h> |
Aaron Durbin | d04639b | 2016-07-17 23:23:59 -0500 | [diff] [blame] | 26 | #include <program_loading.h> |
Aaron Durbin | b430250 | 2016-07-17 17:04:37 -0500 | [diff] [blame] | 27 | #include <romstage_handoff.h> |
Andrey Petrov | 465fc13 | 2016-02-25 14:16:33 -0800 | [diff] [blame] | 28 | #include <string.h> |
Aaron Durbin | d04639b | 2016-07-17 23:23:59 -0500 | [diff] [blame] | 29 | #include <symbols.h> |
Andrey Petrov | 465fc13 | 2016-02-25 14:16:33 -0800 | [diff] [blame] | 30 | #include <timestamp.h> |
Philipp Deppenwiese | fea2429 | 2017-10-17 17:02:29 +0200 | [diff] [blame] | 31 | #include <security/vboot/vboot_common.h> |
Philipp Deppenwiese | 80961af | 2018-02-27 22:14:34 +0100 | [diff] [blame] | 32 | #include <security/tpm/tspi.h> |
Furquan Shaikh | 2db5bac | 2016-11-07 23:57:48 -0800 | [diff] [blame] | 33 | #include <vb2_api.h> |
Philipp Deppenwiese | 80961af | 2018-02-27 22:14:34 +0100 | [diff] [blame] | 34 | #include <fsp/memory_init.h> |
Elyes HAOUAS | bd1683d | 2019-05-15 21:05:37 +0200 | [diff] [blame] | 35 | #include <types.h> |
Andrey Petrov | 465fc13 | 2016-02-25 14:16:33 -0800 | [diff] [blame] | 36 | |
Joel Kitching | 2c8243c | 2019-03-11 17:47:24 +0800 | [diff] [blame] | 37 | /* TPM MRC hash functionality depends on vboot starting before memory init. */ |
| 38 | _Static_assert(!CONFIG(FSP2_0_USES_TPM_MRC_HASH) || |
| 39 | CONFIG(VBOOT_STARTS_IN_BOOTBLOCK), |
| 40 | "for TPM MRC hash functionality, vboot must start in bootblock"); |
| 41 | |
Aaron Durbin | f0ec824 | 2016-07-18 11:24:36 -0500 | [diff] [blame] | 42 | static void save_memory_training_data(bool s3wake, uint32_t fsp_version) |
Aaron Durbin | b430250 | 2016-07-17 17:04:37 -0500 | [diff] [blame] | 43 | { |
Aaron Durbin | b430250 | 2016-07-17 17:04:37 -0500 | [diff] [blame] | 44 | size_t mrc_data_size; |
| 45 | const void *mrc_data; |
Aaron Durbin | f0ec824 | 2016-07-18 11:24:36 -0500 | [diff] [blame] | 46 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 47 | if (!CONFIG(CACHE_MRC_SETTINGS) || s3wake) |
Aaron Durbin | f0ec824 | 2016-07-18 11:24:36 -0500 | [diff] [blame] | 48 | return; |
| 49 | |
| 50 | mrc_data = fsp_find_nv_storage_data(&mrc_data_size); |
| 51 | if (!mrc_data) { |
| 52 | printk(BIOS_ERR, "Couldn't find memory training data HOB.\n"); |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | * Save MRC Data to CBMEM. By always saving the data this forces |
| 58 | * a retrain after a trip through Chrome OS recovery path. The |
| 59 | * code which saves the data to flash doesn't write if the latest |
| 60 | * training data matches this one. |
| 61 | */ |
Aaron Durbin | 31be2c9 | 2016-12-03 22:08:20 -0600 | [diff] [blame] | 62 | if (mrc_cache_stash_data(MRC_TRAINING_DATA, fsp_version, mrc_data, |
| 63 | mrc_data_size) < 0) |
| 64 | printk(BIOS_ERR, "Failed to stash MRC data\n"); |
Furquan Shaikh | 2db5bac | 2016-11-07 23:57:48 -0800 | [diff] [blame] | 65 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 66 | if (CONFIG(FSP2_0_USES_TPM_MRC_HASH)) |
Philipp Deppenwiese | 80961af | 2018-02-27 22:14:34 +0100 | [diff] [blame] | 67 | mrc_cache_update_hash(mrc_data, mrc_data_size); |
Aaron Durbin | f0ec824 | 2016-07-18 11:24:36 -0500 | [diff] [blame] | 68 | } |
| 69 | |
Lee Leahy | 9671faa | 2016-07-24 18:18:52 -0700 | [diff] [blame] | 70 | static void do_fsp_post_memory_init(bool s3wake, uint32_t fsp_version) |
Aaron Durbin | f0ec824 | 2016-07-18 11:24:36 -0500 | [diff] [blame] | 71 | { |
| 72 | struct range_entry fsp_mem; |
Aaron Durbin | b430250 | 2016-07-17 17:04:37 -0500 | [diff] [blame] | 73 | |
Lee Leahy | 52d0c68 | 2016-08-01 15:47:42 -0700 | [diff] [blame] | 74 | if (fsp_find_reserved_memory(&fsp_mem)) |
| 75 | die("Failed to find FSP_RESERVED_MEMORY_RESOURCE_HOB!\n"); |
Aaron Durbin | b430250 | 2016-07-17 17:04:37 -0500 | [diff] [blame] | 76 | |
| 77 | /* initialize cbmem by adding FSP reserved memory first thing */ |
| 78 | if (!s3wake) { |
| 79 | cbmem_initialize_empty_id_size(CBMEM_ID_FSP_RESERVED_MEMORY, |
| 80 | range_entry_size(&fsp_mem)); |
| 81 | } else if (cbmem_initialize_id_size(CBMEM_ID_FSP_RESERVED_MEMORY, |
| 82 | range_entry_size(&fsp_mem))) { |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 83 | if (CONFIG(HAVE_ACPI_RESUME)) { |
Lee Leahy | b20d4ba | 2016-07-31 16:49:28 -0700 | [diff] [blame] | 84 | printk(BIOS_ERR, |
Lee Leahy | ac3b0a6 | 2016-07-27 07:40:25 -0700 | [diff] [blame] | 85 | "Failed to recover CBMEM in S3 resume.\n"); |
Aaron Durbin | b430250 | 2016-07-17 17:04:37 -0500 | [diff] [blame] | 86 | /* Failed S3 resume, reset to come up cleanly */ |
Patrick Rudolph | f677d17 | 2018-10-01 19:17:11 +0200 | [diff] [blame] | 87 | /* FIXME: A "system" reset is likely enough: */ |
| 88 | full_reset(); |
Aaron Durbin | b430250 | 2016-07-17 17:04:37 -0500 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
| 92 | /* make sure FSP memory is reserved in cbmem */ |
| 93 | if (range_entry_base(&fsp_mem) != |
| 94 | (uintptr_t)cbmem_find(CBMEM_ID_FSP_RESERVED_MEMORY)) |
Lee Leahy | 9671faa | 2016-07-24 18:18:52 -0700 | [diff] [blame] | 95 | die("Failed to accommodate FSP reserved memory request!\n"); |
Aaron Durbin | b430250 | 2016-07-17 17:04:37 -0500 | [diff] [blame] | 96 | |
Aaron Durbin | f0ec824 | 2016-07-18 11:24:36 -0500 | [diff] [blame] | 97 | save_memory_training_data(s3wake, fsp_version); |
Aaron Durbin | b430250 | 2016-07-17 17:04:37 -0500 | [diff] [blame] | 98 | |
| 99 | /* Create romstage handof information */ |
Aaron Durbin | 77e1399 | 2016-11-29 17:43:04 -0600 | [diff] [blame] | 100 | romstage_handoff_init(s3wake); |
Aaron Durbin | b430250 | 2016-07-17 17:04:37 -0500 | [diff] [blame] | 101 | } |
| 102 | |
Aamir Bohra | 69cd62c | 2018-01-08 11:01:34 +0530 | [diff] [blame] | 103 | static void fsp_fill_mrc_cache(FSPM_ARCH_UPD *arch_upd, uint32_t fsp_version) |
Aaron Durbin | b430250 | 2016-07-17 17:04:37 -0500 | [diff] [blame] | 104 | { |
Aaron Durbin | 31be2c9 | 2016-12-03 22:08:20 -0600 | [diff] [blame] | 105 | struct region_device rdev; |
| 106 | void *data; |
Aaron Durbin | b430250 | 2016-07-17 17:04:37 -0500 | [diff] [blame] | 107 | |
Aaron Durbin | f0ec824 | 2016-07-18 11:24:36 -0500 | [diff] [blame] | 108 | arch_upd->NvsBufferPtr = NULL; |
| 109 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 110 | if (!CONFIG(CACHE_MRC_SETTINGS)) |
Aaron Durbin | f0ec824 | 2016-07-18 11:24:36 -0500 | [diff] [blame] | 111 | return; |
| 112 | |
Aaron Durbin | 31be2c9 | 2016-12-03 22:08:20 -0600 | [diff] [blame] | 113 | /* |
| 114 | * In recovery mode, force retraining: |
| 115 | * 1. Recovery cache is not supported, or |
| 116 | * 2. Memory retrain switch is set. |
| 117 | */ |
| 118 | if (vboot_recovery_mode_enabled()) { |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 119 | if (!CONFIG(HAS_RECOVERY_MRC_CACHE)) |
Aaron Durbin | 31be2c9 | 2016-12-03 22:08:20 -0600 | [diff] [blame] | 120 | return; |
| 121 | if (vboot_recovery_mode_memory_retrain()) |
| 122 | return; |
| 123 | } |
Aaron Durbin | 98ea636 | 2016-07-18 11:31:53 -0500 | [diff] [blame] | 124 | |
Aaron Durbin | 31be2c9 | 2016-12-03 22:08:20 -0600 | [diff] [blame] | 125 | if (mrc_cache_get_current(MRC_TRAINING_DATA, fsp_version, &rdev) < 0) |
Aaron Durbin | f0ec824 | 2016-07-18 11:24:36 -0500 | [diff] [blame] | 126 | return; |
Aaron Durbin | f0ec824 | 2016-07-18 11:24:36 -0500 | [diff] [blame] | 127 | |
Aaron Durbin | 31be2c9 | 2016-12-03 22:08:20 -0600 | [diff] [blame] | 128 | /* Assume boot device is memory mapped. */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 129 | assert(CONFIG(BOOT_DEVICE_MEMORY_MAPPED)); |
Aaron Durbin | 31be2c9 | 2016-12-03 22:08:20 -0600 | [diff] [blame] | 130 | data = rdev_mmap_full(&rdev); |
| 131 | |
| 132 | if (data == NULL) |
| 133 | return; |
| 134 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 135 | if (CONFIG(FSP2_0_USES_TPM_MRC_HASH) && |
Philipp Deppenwiese | 80961af | 2018-02-27 22:14:34 +0100 | [diff] [blame] | 136 | !mrc_cache_verify_hash(data, region_device_sz(&rdev))) |
Furquan Shaikh | 2db5bac | 2016-11-07 23:57:48 -0800 | [diff] [blame] | 137 | return; |
| 138 | |
Aaron Durbin | f0ec824 | 2016-07-18 11:24:36 -0500 | [diff] [blame] | 139 | /* MRC cache found */ |
Aaron Durbin | 31be2c9 | 2016-12-03 22:08:20 -0600 | [diff] [blame] | 140 | arch_upd->NvsBufferPtr = data; |
Aamir Bohra | 69cd62c | 2018-01-08 11:01:34 +0530 | [diff] [blame] | 141 | |
| 142 | printk(BIOS_SPEW, "MRC cache found, size %zx\n", |
| 143 | region_device_sz(&rdev)); |
Aaron Durbin | f0ec824 | 2016-07-18 11:24:36 -0500 | [diff] [blame] | 144 | } |
| 145 | |
Aaron Durbin | 02e504c | 2016-07-18 11:53:10 -0500 | [diff] [blame] | 146 | static enum cb_err check_region_overlap(const struct memranges *ranges, |
| 147 | const char *description, |
| 148 | uintptr_t begin, uintptr_t end) |
Aaron Durbin | f0ec824 | 2016-07-18 11:24:36 -0500 | [diff] [blame] | 149 | { |
Aaron Durbin | 02e504c | 2016-07-18 11:53:10 -0500 | [diff] [blame] | 150 | const struct range_entry *r; |
| 151 | |
| 152 | memranges_each_entry(r, ranges) { |
| 153 | if (end <= range_entry_base(r)) |
| 154 | continue; |
| 155 | if (begin >= range_entry_end(r)) |
| 156 | continue; |
Lee Leahy | b20d4ba | 2016-07-31 16:49:28 -0700 | [diff] [blame] | 157 | printk(BIOS_CRIT, "'%s' overlaps currently running program: " |
Aaron Durbin | 02e504c | 2016-07-18 11:53:10 -0500 | [diff] [blame] | 158 | "[%p, %p)\n", description, (void *)begin, (void *)end); |
| 159 | return CB_ERR; |
| 160 | } |
| 161 | |
| 162 | return CB_SUCCESS; |
| 163 | } |
Aamir Bohra | 6d569e0c | 2018-08-27 13:36:15 +0530 | [diff] [blame] | 164 | static enum cb_err setup_fsp_stack_frame(FSPM_ARCH_UPD *arch_upd, |
| 165 | const struct memranges *memmap) |
Aaron Durbin | 02e504c | 2016-07-18 11:53:10 -0500 | [diff] [blame] | 166 | { |
| 167 | uintptr_t stack_begin; |
| 168 | uintptr_t stack_end; |
| 169 | |
Aaron Durbin | b430250 | 2016-07-17 17:04:37 -0500 | [diff] [blame] | 170 | /* |
Aamir Bohra | 6d569e0c | 2018-08-27 13:36:15 +0530 | [diff] [blame] | 171 | * FSP 2.1 version would use same stack as coreboot instead of |
| 172 | * setting up seprate stack frame. FSP 2.1 would not relocate stack |
| 173 | * top and does not reinitialize stack pointer. |
| 174 | */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 175 | if (CONFIG(FSP_USES_CB_STACK)) { |
Subrata Banik | 51c8532 | 2019-03-27 18:17:13 +0530 | [diff] [blame] | 176 | arch_upd->StackBase = (void *)_car_stack_start; |
Aamir Bohra | 6d569e0c | 2018-08-27 13:36:15 +0530 | [diff] [blame] | 177 | arch_upd->StackSize = CONFIG_DCACHE_BSP_STACK_SIZE; |
| 178 | return CB_SUCCESS; |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | * FSPM_UPD passed here is populated with default values |
| 183 | * provided by the blob itself. We let FSPM use top of CAR |
| 184 | * region of the size it requests. |
Aaron Durbin | b430250 | 2016-07-17 17:04:37 -0500 | [diff] [blame] | 185 | */ |
Aaron Durbin | 02e504c | 2016-07-18 11:53:10 -0500 | [diff] [blame] | 186 | stack_end = (uintptr_t)_car_region_end; |
| 187 | stack_begin = stack_end - arch_upd->StackSize; |
Aaron Durbin | 02e504c | 2016-07-18 11:53:10 -0500 | [diff] [blame] | 188 | if (check_region_overlap(memmap, "FSPM stack", stack_begin, |
| 189 | stack_end) != CB_SUCCESS) |
| 190 | return CB_ERR; |
| 191 | |
| 192 | arch_upd->StackBase = (void *)stack_begin; |
Aamir Bohra | 6d569e0c | 2018-08-27 13:36:15 +0530 | [diff] [blame] | 193 | return CB_SUCCESS; |
| 194 | } |
| 195 | |
| 196 | static enum cb_err fsp_fill_common_arch_params(FSPM_ARCH_UPD *arch_upd, |
| 197 | bool s3wake, uint32_t fsp_version, |
| 198 | const struct memranges *memmap) |
| 199 | { |
| 200 | if (setup_fsp_stack_frame(arch_upd, memmap)) |
| 201 | return CB_ERR; |
Aaron Durbin | b430250 | 2016-07-17 17:04:37 -0500 | [diff] [blame] | 202 | |
Aamir Bohra | 69cd62c | 2018-01-08 11:01:34 +0530 | [diff] [blame] | 203 | fsp_fill_mrc_cache(arch_upd, fsp_version); |
Aaron Durbin | b430250 | 2016-07-17 17:04:37 -0500 | [diff] [blame] | 204 | |
Aamir Bohra | 69cd62c | 2018-01-08 11:01:34 +0530 | [diff] [blame] | 205 | /* Configure bootmode */ |
| 206 | if (s3wake) { |
| 207 | /* |
| 208 | * For S3 resume case, if valid mrc cache data is not found or |
| 209 | * RECOVERY_MRC_CACHE hash verification fails, the S3 data |
| 210 | * pointer would be null and S3 resume fails with fsp-m |
| 211 | * returning error. Invoking a reset here saves time. |
| 212 | */ |
| 213 | if (!arch_upd->NvsBufferPtr) |
Patrick Rudolph | f677d17 | 2018-10-01 19:17:11 +0200 | [diff] [blame] | 214 | /* FIXME: A "system" reset is likely enough: */ |
| 215 | full_reset(); |
Aamir Bohra | 69cd62c | 2018-01-08 11:01:34 +0530 | [diff] [blame] | 216 | arch_upd->BootMode = FSP_BOOT_ON_S3_RESUME; |
| 217 | } else { |
| 218 | if (arch_upd->NvsBufferPtr) |
| 219 | arch_upd->BootMode = |
| 220 | FSP_BOOT_ASSUMING_NO_CONFIGURATION_CHANGES; |
| 221 | else |
| 222 | arch_upd->BootMode = FSP_BOOT_WITH_FULL_CONFIGURATION; |
| 223 | } |
Aaron Durbin | 02e504c | 2016-07-18 11:53:10 -0500 | [diff] [blame] | 224 | |
Aamir Bohra | 69cd62c | 2018-01-08 11:01:34 +0530 | [diff] [blame] | 225 | printk(BIOS_SPEW, "bootmode is set to :%d\n", arch_upd->BootMode); |
Aamir Bohra | 276c06a | 2017-12-26 17:54:45 +0530 | [diff] [blame] | 226 | |
Aaron Durbin | 02e504c | 2016-07-18 11:53:10 -0500 | [diff] [blame] | 227 | return CB_SUCCESS; |
Aaron Durbin | b430250 | 2016-07-17 17:04:37 -0500 | [diff] [blame] | 228 | } |
| 229 | |
Aaron Durbin | 6403167 | 2018-04-21 14:45:32 -0600 | [diff] [blame] | 230 | __weak |
Aaron Durbin | a3cecb2 | 2017-04-25 21:58:10 -0500 | [diff] [blame] | 231 | uint8_t fsp_memory_mainboard_version(void) |
| 232 | { |
| 233 | return 0; |
| 234 | } |
| 235 | |
Aaron Durbin | 6403167 | 2018-04-21 14:45:32 -0600 | [diff] [blame] | 236 | __weak |
Aaron Durbin | a3cecb2 | 2017-04-25 21:58:10 -0500 | [diff] [blame] | 237 | uint8_t fsp_memory_soc_version(void) |
| 238 | { |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | /* |
| 243 | * Allow SoC and/or mainboard to bump the revision of the FSP setting |
| 244 | * number. The FSP spec uses the low 8 bits as the build number. Take over |
| 245 | * bits 3:0 for the SoC setting and bits 7:4 for the mainboard. That way |
| 246 | * a tweak in the settings will bump the version used to track the cached |
| 247 | * setting which triggers retraining when the FSP version hasn't changed, but |
| 248 | * the SoC or mainboard settings have. |
| 249 | */ |
| 250 | static uint32_t fsp_memory_settings_version(const struct fsp_header *hdr) |
| 251 | { |
| 252 | /* Use the full FSP version by default. */ |
| 253 | uint32_t ver = hdr->fsp_revision; |
| 254 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 255 | if (!CONFIG(FSP_PLATFORM_MEMORY_SETTINGS_VERSIONS)) |
Aaron Durbin | a3cecb2 | 2017-04-25 21:58:10 -0500 | [diff] [blame] | 256 | return ver; |
| 257 | |
| 258 | ver &= ~0xff; |
| 259 | ver |= (0xf & fsp_memory_mainboard_version()) << 4; |
| 260 | ver |= (0xf & fsp_memory_soc_version()) << 0; |
| 261 | |
| 262 | return ver; |
| 263 | } |
| 264 | |
Lee Leahy | 9671faa | 2016-07-24 18:18:52 -0700 | [diff] [blame] | 265 | static void do_fsp_memory_init(struct fsp_header *hdr, bool s3wake, |
Aaron Durbin | 02e504c | 2016-07-18 11:53:10 -0500 | [diff] [blame] | 266 | const struct memranges *memmap) |
Andrey Petrov | 465fc13 | 2016-02-25 14:16:33 -0800 | [diff] [blame] | 267 | { |
Brandon Breitenstein | c31ba0e | 2016-07-27 17:34:45 -0700 | [diff] [blame] | 268 | uint32_t status; |
Andrey Petrov | 465fc13 | 2016-02-25 14:16:33 -0800 | [diff] [blame] | 269 | fsp_memory_init_fn fsp_raminit; |
Brandon Breitenstein | c31ba0e | 2016-07-27 17:34:45 -0700 | [diff] [blame] | 270 | FSPM_UPD fspm_upd, *upd; |
| 271 | FSPM_ARCH_UPD *arch_upd; |
Aaron Durbin | a3cecb2 | 2017-04-25 21:58:10 -0500 | [diff] [blame] | 272 | uint32_t fsp_version; |
Andrey Petrov | 465fc13 | 2016-02-25 14:16:33 -0800 | [diff] [blame] | 273 | |
Furquan Shaikh | 585210a | 2018-10-16 11:54:37 -0700 | [diff] [blame] | 274 | post_code(POST_MEM_PREINIT_PREP_START); |
Andrey Petrov | 465fc13 | 2016-02-25 14:16:33 -0800 | [diff] [blame] | 275 | |
Aaron Durbin | a3cecb2 | 2017-04-25 21:58:10 -0500 | [diff] [blame] | 276 | fsp_version = fsp_memory_settings_version(hdr); |
| 277 | |
Brandon Breitenstein | c31ba0e | 2016-07-27 17:34:45 -0700 | [diff] [blame] | 278 | upd = (FSPM_UPD *)(hdr->cfg_region_offset + hdr->image_base); |
Andrey Petrov | 465fc13 | 2016-02-25 14:16:33 -0800 | [diff] [blame] | 279 | |
Lee Leahy | e686ee8 | 2017-03-10 08:45:30 -0800 | [diff] [blame] | 280 | if (upd->FspUpdHeader.Signature != FSPM_UPD_SIGNATURE) |
Keith Short | bb41aba | 2019-05-16 14:07:43 -0600 | [diff] [blame] | 281 | die_with_post_code(POST_INVALID_VENDOR_BINARY, |
| 282 | "Invalid FSPM signature!\n"); |
Andrey Petrov | 465fc13 | 2016-02-25 14:16:33 -0800 | [diff] [blame] | 283 | |
| 284 | /* Copy the default values from the UPD area */ |
| 285 | memcpy(&fspm_upd, upd, sizeof(fspm_upd)); |
| 286 | |
Aaron Durbin | 02e504c | 2016-07-18 11:53:10 -0500 | [diff] [blame] | 287 | arch_upd = &fspm_upd.FspmArchUpd; |
| 288 | |
Aaron Durbin | 2792868 | 2016-07-15 22:32:28 -0500 | [diff] [blame] | 289 | /* Reserve enough memory under TOLUD to save CBMEM header */ |
Aaron Durbin | 02e504c | 2016-07-18 11:53:10 -0500 | [diff] [blame] | 290 | arch_upd->BootLoaderTolumSize = cbmem_overhead_size(); |
Aaron Durbin | 2792868 | 2016-07-15 22:32:28 -0500 | [diff] [blame] | 291 | |
Aaron Durbin | b430250 | 2016-07-17 17:04:37 -0500 | [diff] [blame] | 292 | /* Fill common settings on behalf of chipset. */ |
Aaron Durbin | a3cecb2 | 2017-04-25 21:58:10 -0500 | [diff] [blame] | 293 | if (fsp_fill_common_arch_params(arch_upd, s3wake, fsp_version, |
Aaron Durbin | 02e504c | 2016-07-18 11:53:10 -0500 | [diff] [blame] | 294 | memmap) != CB_SUCCESS) |
Keith Short | bb41aba | 2019-05-16 14:07:43 -0600 | [diff] [blame] | 295 | die_with_post_code(POST_INVALID_VENDOR_BINARY, |
| 296 | "FSPM_ARCH_UPD not found!\n"); |
Aaron Durbin | b430250 | 2016-07-17 17:04:37 -0500 | [diff] [blame] | 297 | |
Andrey Petrov | 465fc13 | 2016-02-25 14:16:33 -0800 | [diff] [blame] | 298 | /* Give SoC and mainboard a chance to update the UPD */ |
Aaron Durbin | a3cecb2 | 2017-04-25 21:58:10 -0500 | [diff] [blame] | 299 | platform_fsp_memory_init_params_cb(&fspm_upd, fsp_version); |
Andrey Petrov | 465fc13 | 2016-02-25 14:16:33 -0800 | [diff] [blame] | 300 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 301 | if (CONFIG(MMA)) |
Pratik Prajapati | ffc934d | 2016-11-18 14:36:34 -0800 | [diff] [blame] | 302 | setup_mma(&fspm_upd.FspmConfig); |
| 303 | |
Furquan Shaikh | 585210a | 2018-10-16 11:54:37 -0700 | [diff] [blame] | 304 | post_code(POST_MEM_PREINIT_PREP_END); |
| 305 | |
Andrey Petrov | 465fc13 | 2016-02-25 14:16:33 -0800 | [diff] [blame] | 306 | /* Call FspMemoryInit */ |
| 307 | fsp_raminit = (void *)(hdr->image_base + hdr->memory_init_entry_offset); |
Lee Leahy | ac3b0a6 | 2016-07-27 07:40:25 -0700 | [diff] [blame] | 308 | fsp_debug_before_memory_init(fsp_raminit, upd, &fspm_upd); |
Andrey Petrov | 465fc13 | 2016-02-25 14:16:33 -0800 | [diff] [blame] | 309 | |
Alexandru Gagniuc | c4ea8f7 | 2016-05-23 12:16:58 -0700 | [diff] [blame] | 310 | post_code(POST_FSP_MEMORY_INIT); |
Andrey Petrov | 465fc13 | 2016-02-25 14:16:33 -0800 | [diff] [blame] | 311 | timestamp_add_now(TS_FSP_MEMORY_INIT_START); |
Lee Leahy | ac3b0a6 | 2016-07-27 07:40:25 -0700 | [diff] [blame] | 312 | status = fsp_raminit(&fspm_upd, fsp_get_hob_list_ptr()); |
Subrata Banik | 0755ab9 | 2017-07-12 15:31:06 +0530 | [diff] [blame] | 313 | post_code(POST_FSP_MEMORY_EXIT); |
Andrey Petrov | 465fc13 | 2016-02-25 14:16:33 -0800 | [diff] [blame] | 314 | timestamp_add_now(TS_FSP_MEMORY_INIT_END); |
| 315 | |
Lee Leahy | 9671faa | 2016-07-24 18:18:52 -0700 | [diff] [blame] | 316 | /* Handle any errors returned by FspMemoryInit */ |
Aaron Durbin | f41f2aa | 2016-07-18 12:03:58 -0500 | [diff] [blame] | 317 | fsp_handle_reset(status); |
Lee Leahy | 9671faa | 2016-07-24 18:18:52 -0700 | [diff] [blame] | 318 | if (status != FSP_SUCCESS) { |
Lee Leahy | b20d4ba | 2016-07-31 16:49:28 -0700 | [diff] [blame] | 319 | printk(BIOS_CRIT, "FspMemoryInit returned 0x%08x\n", status); |
Keith Short | 2430263 | 2019-05-16 14:08:31 -0600 | [diff] [blame] | 320 | die_with_post_code(POST_RAM_FAILURE, |
| 321 | "FspMemoryInit returned an error!\n"); |
Lee Leahy | 9671faa | 2016-07-24 18:18:52 -0700 | [diff] [blame] | 322 | } |
Aaron Durbin | f41f2aa | 2016-07-18 12:03:58 -0500 | [diff] [blame] | 323 | |
Aaron Durbin | a3cecb2 | 2017-04-25 21:58:10 -0500 | [diff] [blame] | 324 | do_fsp_post_memory_init(s3wake, fsp_version); |
Matthew Garrett | 78b58a4 | 2018-07-28 16:53:16 -0700 | [diff] [blame] | 325 | |
| 326 | /* |
| 327 | * fsp_debug_after_memory_init() checks whether the end of the tolum |
| 328 | * region is the same as the top of cbmem, so must be called here |
| 329 | * after cbmem has been initialised in do_fsp_post_memory_init(). |
| 330 | */ |
| 331 | fsp_debug_after_memory_init(status); |
Andrey Petrov | 465fc13 | 2016-02-25 14:16:33 -0800 | [diff] [blame] | 332 | } |
| 333 | |
Aaron Durbin | d04639b | 2016-07-17 23:23:59 -0500 | [diff] [blame] | 334 | /* Load the binary into the memory specified by the info header. */ |
| 335 | static enum cb_err load_fspm_mem(struct fsp_header *hdr, |
Aaron Durbin | 02e504c | 2016-07-18 11:53:10 -0500 | [diff] [blame] | 336 | const struct region_device *rdev, |
| 337 | const struct memranges *memmap) |
Aaron Durbin | d04639b | 2016-07-17 23:23:59 -0500 | [diff] [blame] | 338 | { |
Aaron Durbin | d04639b | 2016-07-17 23:23:59 -0500 | [diff] [blame] | 339 | uintptr_t fspm_begin; |
| 340 | uintptr_t fspm_end; |
| 341 | |
| 342 | if (fsp_validate_component(hdr, rdev) != CB_SUCCESS) |
| 343 | return CB_ERR; |
| 344 | |
| 345 | fspm_begin = hdr->image_base; |
| 346 | fspm_end = fspm_begin + hdr->image_size; |
| 347 | |
Aaron Durbin | 02e504c | 2016-07-18 11:53:10 -0500 | [diff] [blame] | 348 | if (check_region_overlap(memmap, "FSPM", fspm_begin, fspm_end) != |
| 349 | CB_SUCCESS) |
Aaron Durbin | d04639b | 2016-07-17 23:23:59 -0500 | [diff] [blame] | 350 | return CB_ERR; |
Aaron Durbin | d04639b | 2016-07-17 23:23:59 -0500 | [diff] [blame] | 351 | |
| 352 | /* Load binary into memory at provided address. */ |
| 353 | if (rdev_readat(rdev, (void *)fspm_begin, 0, fspm_end - fspm_begin) < 0) |
| 354 | return CB_ERR; |
| 355 | |
| 356 | return CB_SUCCESS; |
| 357 | } |
| 358 | |
| 359 | /* Handle the case when FSPM is running XIP. */ |
| 360 | static enum cb_err load_fspm_xip(struct fsp_header *hdr, |
| 361 | const struct region_device *rdev) |
| 362 | { |
| 363 | void *base; |
| 364 | |
| 365 | if (fsp_validate_component(hdr, rdev) != CB_SUCCESS) |
| 366 | return CB_ERR; |
| 367 | |
| 368 | base = rdev_mmap_full(rdev); |
| 369 | if ((uintptr_t)base != hdr->image_base) { |
Lee Leahy | b20d4ba | 2016-07-31 16:49:28 -0700 | [diff] [blame] | 370 | printk(BIOS_CRIT, "FSPM XIP base does not match: %p vs %p\n", |
Aaron Durbin | d04639b | 2016-07-17 23:23:59 -0500 | [diff] [blame] | 371 | (void *)(uintptr_t)hdr->image_base, base); |
| 372 | return CB_ERR; |
| 373 | } |
| 374 | |
| 375 | /* |
| 376 | * Since the component is XIP it's already in the address space. Thus, |
| 377 | * there's no need to rdev_munmap(). |
| 378 | */ |
| 379 | return CB_SUCCESS; |
| 380 | } |
| 381 | |
Lee Leahy | 9671faa | 2016-07-24 18:18:52 -0700 | [diff] [blame] | 382 | void fsp_memory_init(bool s3wake) |
Andrey Petrov | 465fc13 | 2016-02-25 14:16:33 -0800 | [diff] [blame] | 383 | { |
| 384 | struct fsp_header hdr; |
Aaron Durbin | d04639b | 2016-07-17 23:23:59 -0500 | [diff] [blame] | 385 | enum cb_err status; |
| 386 | struct cbfsf file_desc; |
| 387 | struct region_device file_data; |
| 388 | const char *name = CONFIG_FSP_M_CBFS; |
Aaron Durbin | 02e504c | 2016-07-18 11:53:10 -0500 | [diff] [blame] | 389 | struct memranges memmap; |
| 390 | struct range_entry freeranges[2]; |
Andrey Petrov | 465fc13 | 2016-02-25 14:16:33 -0800 | [diff] [blame] | 391 | |
Kyösti Mälkki | 0889e93 | 2019-08-18 07:40:43 +0300 | [diff] [blame^] | 392 | timestamp_add_now(TS_BEFORE_INITRAM); |
| 393 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 394 | if (CONFIG(ELOG_BOOT_COUNT) && !s3wake) |
Furquan Shaikh | 5aea588 | 2016-07-30 18:10:05 -0700 | [diff] [blame] | 395 | boot_count_increment(); |
| 396 | |
Aaron Durbin | d04639b | 2016-07-17 23:23:59 -0500 | [diff] [blame] | 397 | if (cbfs_boot_locate(&file_desc, name, NULL)) { |
Lee Leahy | b20d4ba | 2016-07-31 16:49:28 -0700 | [diff] [blame] | 398 | printk(BIOS_CRIT, "Could not locate %s in CBFS\n", name); |
Lee Leahy | 9671faa | 2016-07-24 18:18:52 -0700 | [diff] [blame] | 399 | die("FSPM not available!\n"); |
Aaron Durbin | d04639b | 2016-07-17 23:23:59 -0500 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | cbfs_file_data(&file_data, &file_desc); |
| 403 | |
Aaron Durbin | 02e504c | 2016-07-18 11:53:10 -0500 | [diff] [blame] | 404 | /* Build up memory map of romstage address space including CAR. */ |
| 405 | memranges_init_empty(&memmap, &freeranges[0], ARRAY_SIZE(freeranges)); |
| 406 | memranges_insert(&memmap, (uintptr_t)_car_region_start, |
| 407 | _car_relocatable_data_end - _car_region_start, 0); |
Julius Werner | 7e0dea6 | 2019-02-20 18:39:22 -0800 | [diff] [blame] | 408 | memranges_insert(&memmap, (uintptr_t)_program, REGION_SIZE(program), 0); |
Aaron Durbin | 02e504c | 2016-07-18 11:53:10 -0500 | [diff] [blame] | 409 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 410 | if (!CONFIG(FSP_M_XIP)) |
Aaron Durbin | 02e504c | 2016-07-18 11:53:10 -0500 | [diff] [blame] | 411 | status = load_fspm_mem(&hdr, &file_data, &memmap); |
Aaron Durbin | d04639b | 2016-07-17 23:23:59 -0500 | [diff] [blame] | 412 | else |
| 413 | status = load_fspm_xip(&hdr, &file_data); |
| 414 | |
Lee Leahy | e686ee8 | 2017-03-10 08:45:30 -0800 | [diff] [blame] | 415 | if (status != CB_SUCCESS) |
Lee Leahy | 9671faa | 2016-07-24 18:18:52 -0700 | [diff] [blame] | 416 | die("Loading FSPM failed!\n"); |
Aaron Durbin | d04639b | 2016-07-17 23:23:59 -0500 | [diff] [blame] | 417 | |
| 418 | /* Signal that FSP component has been loaded. */ |
| 419 | prog_segment_loaded(hdr.image_base, hdr.image_size, SEG_FINAL); |
Andrey Petrov | 465fc13 | 2016-02-25 14:16:33 -0800 | [diff] [blame] | 420 | |
Lee Leahy | 9671faa | 2016-07-24 18:18:52 -0700 | [diff] [blame] | 421 | do_fsp_memory_init(&hdr, s3wake, &memmap); |
Kyösti Mälkki | 0889e93 | 2019-08-18 07:40:43 +0300 | [diff] [blame^] | 422 | |
| 423 | timestamp_add_now(TS_AFTER_INITRAM); |
Andrey Petrov | 465fc13 | 2016-02-25 14:16:33 -0800 | [diff] [blame] | 424 | } |