blob: 57a052037e81f59fbe95a7b1eac54b9b77559cd9 [file] [log] [blame]
Patrick Georgiac959032020-05-05 22:49:26 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Andrey Petrov465fc132016-02-25 14:16:33 -08002
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +01003#include <security/vboot/antirollback.h>
Aaron Durbinb4302502016-07-17 17:04:37 -05004#include <arch/symbols.h>
Aaron Durbin31be2c92016-12-03 22:08:20 -06005#include <assert.h>
Joel Kitching9a292282020-03-06 13:44:50 +08006#include <bootmode.h>
Aaron Durbind04639b2016-07-17 23:23:59 -05007#include <cbfs.h>
Aaron Durbin27928682016-07-15 22:32:28 -05008#include <cbmem.h>
Patrick Rudolphf677d172018-10-01 19:17:11 +02009#include <cf9_reset.h>
Andrey Petrov465fc132016-02-25 14:16:33 -080010#include <console/console.h>
Furquan Shaikh5aea5882016-07-30 18:10:05 -070011#include <elog.h>
Andrey Petrov465fc132016-02-25 14:16:33 -080012#include <fsp/api.h>
13#include <fsp/util.h>
14#include <memrange.h>
Aaron Durbindecd0622017-12-15 12:26:40 -070015#include <mrc_cache.h>
Aaron Durbind04639b2016-07-17 23:23:59 -050016#include <program_loading.h>
Aaron Durbinb4302502016-07-17 17:04:37 -050017#include <romstage_handoff.h>
Andrey Petrov465fc132016-02-25 14:16:33 -080018#include <string.h>
Aaron Durbind04639b2016-07-17 23:23:59 -050019#include <symbols.h>
Andrey Petrov465fc132016-02-25 14:16:33 -080020#include <timestamp.h>
Philipp Deppenwiesefea24292017-10-17 17:02:29 +020021#include <security/vboot/vboot_common.h>
Philipp Deppenwiese80961af2018-02-27 22:14:34 +010022#include <security/tpm/tspi.h>
Furquan Shaikh2db5bac2016-11-07 23:57:48 -080023#include <vb2_api.h>
Philipp Deppenwiese80961af2018-02-27 22:14:34 +010024#include <fsp/memory_init.h>
Elyes HAOUASbd1683d2019-05-15 21:05:37 +020025#include <types.h>
Andrey Petrov465fc132016-02-25 14:16:33 -080026
Kyösti Mälkkic9871502019-09-03 07:03:39 +030027static uint8_t temp_ram[CONFIG_FSP_TEMP_RAM_SIZE] __aligned(sizeof(uint64_t));
28
Joel Kitching2c8243c2019-03-11 17:47:24 +080029/* TPM MRC hash functionality depends on vboot starting before memory init. */
30_Static_assert(!CONFIG(FSP2_0_USES_TPM_MRC_HASH) ||
31 CONFIG(VBOOT_STARTS_IN_BOOTBLOCK),
32 "for TPM MRC hash functionality, vboot must start in bootblock");
33
Aaron Durbinf0ec8242016-07-18 11:24:36 -050034static void save_memory_training_data(bool s3wake, uint32_t fsp_version)
Aaron Durbinb4302502016-07-17 17:04:37 -050035{
Aaron Durbinb4302502016-07-17 17:04:37 -050036 size_t mrc_data_size;
37 const void *mrc_data;
Aaron Durbinf0ec8242016-07-18 11:24:36 -050038
Julius Wernercd49cce2019-03-05 16:53:33 -080039 if (!CONFIG(CACHE_MRC_SETTINGS) || s3wake)
Aaron Durbinf0ec8242016-07-18 11:24:36 -050040 return;
41
42 mrc_data = fsp_find_nv_storage_data(&mrc_data_size);
43 if (!mrc_data) {
44 printk(BIOS_ERR, "Couldn't find memory training data HOB.\n");
45 return;
46 }
47
48 /*
49 * Save MRC Data to CBMEM. By always saving the data this forces
50 * a retrain after a trip through Chrome OS recovery path. The
51 * code which saves the data to flash doesn't write if the latest
52 * training data matches this one.
53 */
Aaron Durbin31be2c92016-12-03 22:08:20 -060054 if (mrc_cache_stash_data(MRC_TRAINING_DATA, fsp_version, mrc_data,
55 mrc_data_size) < 0)
56 printk(BIOS_ERR, "Failed to stash MRC data\n");
Furquan Shaikh2db5bac2016-11-07 23:57:48 -080057
Julius Wernercd49cce2019-03-05 16:53:33 -080058 if (CONFIG(FSP2_0_USES_TPM_MRC_HASH))
Philipp Deppenwiese80961af2018-02-27 22:14:34 +010059 mrc_cache_update_hash(mrc_data, mrc_data_size);
Aaron Durbinf0ec8242016-07-18 11:24:36 -050060}
61
Lee Leahy9671faa2016-07-24 18:18:52 -070062static void do_fsp_post_memory_init(bool s3wake, uint32_t fsp_version)
Aaron Durbinf0ec8242016-07-18 11:24:36 -050063{
64 struct range_entry fsp_mem;
Aaron Durbinb4302502016-07-17 17:04:37 -050065
Michael Niewöhnerbc1dbb32019-10-24 22:58:25 +020066 fsp_find_reserved_memory(&fsp_mem);
Aaron Durbinb4302502016-07-17 17:04:37 -050067
68 /* initialize cbmem by adding FSP reserved memory first thing */
69 if (!s3wake) {
70 cbmem_initialize_empty_id_size(CBMEM_ID_FSP_RESERVED_MEMORY,
71 range_entry_size(&fsp_mem));
72 } else if (cbmem_initialize_id_size(CBMEM_ID_FSP_RESERVED_MEMORY,
73 range_entry_size(&fsp_mem))) {
Julius Wernercd49cce2019-03-05 16:53:33 -080074 if (CONFIG(HAVE_ACPI_RESUME)) {
Lee Leahyb20d4ba2016-07-31 16:49:28 -070075 printk(BIOS_ERR,
Lee Leahyac3b0a62016-07-27 07:40:25 -070076 "Failed to recover CBMEM in S3 resume.\n");
Aaron Durbinb4302502016-07-17 17:04:37 -050077 /* Failed S3 resume, reset to come up cleanly */
Patrick Rudolphf677d172018-10-01 19:17:11 +020078 /* FIXME: A "system" reset is likely enough: */
79 full_reset();
Aaron Durbinb4302502016-07-17 17:04:37 -050080 }
81 }
82
83 /* make sure FSP memory is reserved in cbmem */
84 if (range_entry_base(&fsp_mem) !=
85 (uintptr_t)cbmem_find(CBMEM_ID_FSP_RESERVED_MEMORY))
Lee Leahy9671faa2016-07-24 18:18:52 -070086 die("Failed to accommodate FSP reserved memory request!\n");
Aaron Durbinb4302502016-07-17 17:04:37 -050087
Aaron Durbinf0ec8242016-07-18 11:24:36 -050088 save_memory_training_data(s3wake, fsp_version);
Aaron Durbinb4302502016-07-17 17:04:37 -050089
90 /* Create romstage handof information */
Aaron Durbin77e13992016-11-29 17:43:04 -060091 romstage_handoff_init(s3wake);
Aaron Durbinb4302502016-07-17 17:04:37 -050092}
93
Aamir Bohra69cd62c2018-01-08 11:01:34 +053094static void fsp_fill_mrc_cache(FSPM_ARCH_UPD *arch_upd, uint32_t fsp_version)
Aaron Durbinb4302502016-07-17 17:04:37 -050095{
Aaron Durbin31be2c92016-12-03 22:08:20 -060096 struct region_device rdev;
97 void *data;
Aaron Durbinb4302502016-07-17 17:04:37 -050098
Aaron Durbinf0ec8242016-07-18 11:24:36 -050099 arch_upd->NvsBufferPtr = NULL;
100
Julius Wernercd49cce2019-03-05 16:53:33 -0800101 if (!CONFIG(CACHE_MRC_SETTINGS))
Aaron Durbinf0ec8242016-07-18 11:24:36 -0500102 return;
103
Aaron Durbin31be2c92016-12-03 22:08:20 -0600104 /*
105 * In recovery mode, force retraining:
106 * 1. Recovery cache is not supported, or
107 * 2. Memory retrain switch is set.
108 */
109 if (vboot_recovery_mode_enabled()) {
Julius Wernercd49cce2019-03-05 16:53:33 -0800110 if (!CONFIG(HAS_RECOVERY_MRC_CACHE))
Aaron Durbin31be2c92016-12-03 22:08:20 -0600111 return;
Joel Kitching9a292282020-03-06 13:44:50 +0800112 if (get_recovery_mode_retrain_switch())
Aaron Durbin31be2c92016-12-03 22:08:20 -0600113 return;
114 }
Aaron Durbin98ea6362016-07-18 11:31:53 -0500115
Aaron Durbin31be2c92016-12-03 22:08:20 -0600116 if (mrc_cache_get_current(MRC_TRAINING_DATA, fsp_version, &rdev) < 0)
Aaron Durbinf0ec8242016-07-18 11:24:36 -0500117 return;
Aaron Durbinf0ec8242016-07-18 11:24:36 -0500118
Aaron Durbin31be2c92016-12-03 22:08:20 -0600119 /* Assume boot device is memory mapped. */
Julius Wernercd49cce2019-03-05 16:53:33 -0800120 assert(CONFIG(BOOT_DEVICE_MEMORY_MAPPED));
Aaron Durbin31be2c92016-12-03 22:08:20 -0600121 data = rdev_mmap_full(&rdev);
122
123 if (data == NULL)
124 return;
125
Julius Wernercd49cce2019-03-05 16:53:33 -0800126 if (CONFIG(FSP2_0_USES_TPM_MRC_HASH) &&
Philipp Deppenwiese80961af2018-02-27 22:14:34 +0100127 !mrc_cache_verify_hash(data, region_device_sz(&rdev)))
Furquan Shaikh2db5bac2016-11-07 23:57:48 -0800128 return;
129
Aaron Durbinf0ec8242016-07-18 11:24:36 -0500130 /* MRC cache found */
Aaron Durbin31be2c92016-12-03 22:08:20 -0600131 arch_upd->NvsBufferPtr = data;
Aamir Bohra69cd62c2018-01-08 11:01:34 +0530132
133 printk(BIOS_SPEW, "MRC cache found, size %zx\n",
134 region_device_sz(&rdev));
Aaron Durbinf0ec8242016-07-18 11:24:36 -0500135}
136
Aaron Durbin02e504c2016-07-18 11:53:10 -0500137static enum cb_err check_region_overlap(const struct memranges *ranges,
138 const char *description,
139 uintptr_t begin, uintptr_t end)
Aaron Durbinf0ec8242016-07-18 11:24:36 -0500140{
Aaron Durbin02e504c2016-07-18 11:53:10 -0500141 const struct range_entry *r;
142
143 memranges_each_entry(r, ranges) {
144 if (end <= range_entry_base(r))
145 continue;
146 if (begin >= range_entry_end(r))
147 continue;
Lee Leahyb20d4ba2016-07-31 16:49:28 -0700148 printk(BIOS_CRIT, "'%s' overlaps currently running program: "
Aaron Durbin02e504c2016-07-18 11:53:10 -0500149 "[%p, %p)\n", description, (void *)begin, (void *)end);
150 return CB_ERR;
151 }
152
153 return CB_SUCCESS;
154}
Kyösti Mälkkic9871502019-09-03 07:03:39 +0300155
Aamir Bohra6d569e0c2018-08-27 13:36:15 +0530156static enum cb_err setup_fsp_stack_frame(FSPM_ARCH_UPD *arch_upd,
157 const struct memranges *memmap)
Aaron Durbin02e504c2016-07-18 11:53:10 -0500158{
159 uintptr_t stack_begin;
160 uintptr_t stack_end;
161
Aaron Durbinb4302502016-07-17 17:04:37 -0500162 /*
Aamir Bohra6d569e0c2018-08-27 13:36:15 +0530163 * FSPM_UPD passed here is populated with default values
164 * provided by the blob itself. We let FSPM use top of CAR
165 * region of the size it requests.
Aaron Durbinb4302502016-07-17 17:04:37 -0500166 */
Aaron Durbin02e504c2016-07-18 11:53:10 -0500167 stack_end = (uintptr_t)_car_region_end;
168 stack_begin = stack_end - arch_upd->StackSize;
Aaron Durbin02e504c2016-07-18 11:53:10 -0500169 if (check_region_overlap(memmap, "FSPM stack", stack_begin,
170 stack_end) != CB_SUCCESS)
171 return CB_ERR;
172
173 arch_upd->StackBase = (void *)stack_begin;
Aamir Bohra6d569e0c2018-08-27 13:36:15 +0530174 return CB_SUCCESS;
175}
176
177static enum cb_err fsp_fill_common_arch_params(FSPM_ARCH_UPD *arch_upd,
178 bool s3wake, uint32_t fsp_version,
179 const struct memranges *memmap)
180{
Kyösti Mälkkic9871502019-09-03 07:03:39 +0300181 /*
182 * FSP 2.1 version would use same stack as coreboot instead of
183 * setting up separate stack frame. FSP 2.1 would not relocate stack
184 * top and does not reinitialize stack pointer. The parameters passed
185 * as StackBase and StackSize are actually for temporary RAM and HOBs
186 * and are not related to FSP stack at all.
Felix Held414d7e42020-08-11 22:54:06 +0200187 * Non-CAR FSP 2.0 platforms pass a DRAM location for the FSP stack.
Kyösti Mälkkic9871502019-09-03 07:03:39 +0300188 */
Felix Held414d7e42020-08-11 22:54:06 +0200189 if (CONFIG(FSP_USES_CB_STACK) || !ENV_CACHE_AS_RAM) {
Kyösti Mälkkic9871502019-09-03 07:03:39 +0300190 arch_upd->StackBase = temp_ram;
191 arch_upd->StackSize = sizeof(temp_ram);
192 } else if (setup_fsp_stack_frame(arch_upd, memmap)) {
Aamir Bohra6d569e0c2018-08-27 13:36:15 +0530193 return CB_ERR;
Kyösti Mälkkic9871502019-09-03 07:03:39 +0300194 }
Aaron Durbinb4302502016-07-17 17:04:37 -0500195
Aamir Bohra69cd62c2018-01-08 11:01:34 +0530196 fsp_fill_mrc_cache(arch_upd, fsp_version);
Aaron Durbinb4302502016-07-17 17:04:37 -0500197
Aamir Bohra69cd62c2018-01-08 11:01:34 +0530198 /* Configure bootmode */
199 if (s3wake) {
Aamir Bohra69cd62c2018-01-08 11:01:34 +0530200 arch_upd->BootMode = FSP_BOOT_ON_S3_RESUME;
201 } else {
202 if (arch_upd->NvsBufferPtr)
203 arch_upd->BootMode =
204 FSP_BOOT_ASSUMING_NO_CONFIGURATION_CHANGES;
205 else
206 arch_upd->BootMode = FSP_BOOT_WITH_FULL_CONFIGURATION;
207 }
Aaron Durbin02e504c2016-07-18 11:53:10 -0500208
Marshall Dawson22d66ef2019-08-30 14:52:37 -0600209 printk(BIOS_SPEW, "bootmode is set to: %d\n", arch_upd->BootMode);
Aamir Bohra276c06a2017-12-26 17:54:45 +0530210
Aaron Durbin02e504c2016-07-18 11:53:10 -0500211 return CB_SUCCESS;
Aaron Durbinb4302502016-07-17 17:04:37 -0500212}
213
Aaron Durbin64031672018-04-21 14:45:32 -0600214__weak
Aaron Durbina3cecb22017-04-25 21:58:10 -0500215uint8_t fsp_memory_mainboard_version(void)
216{
217 return 0;
218}
219
Aaron Durbin64031672018-04-21 14:45:32 -0600220__weak
Aaron Durbina3cecb22017-04-25 21:58:10 -0500221uint8_t fsp_memory_soc_version(void)
222{
223 return 0;
224}
225
226/*
227 * Allow SoC and/or mainboard to bump the revision of the FSP setting
228 * number. The FSP spec uses the low 8 bits as the build number. Take over
229 * bits 3:0 for the SoC setting and bits 7:4 for the mainboard. That way
230 * a tweak in the settings will bump the version used to track the cached
231 * setting which triggers retraining when the FSP version hasn't changed, but
232 * the SoC or mainboard settings have.
233 */
234static uint32_t fsp_memory_settings_version(const struct fsp_header *hdr)
235{
236 /* Use the full FSP version by default. */
237 uint32_t ver = hdr->fsp_revision;
238
Julius Wernercd49cce2019-03-05 16:53:33 -0800239 if (!CONFIG(FSP_PLATFORM_MEMORY_SETTINGS_VERSIONS))
Aaron Durbina3cecb22017-04-25 21:58:10 -0500240 return ver;
241
242 ver &= ~0xff;
243 ver |= (0xf & fsp_memory_mainboard_version()) << 4;
244 ver |= (0xf & fsp_memory_soc_version()) << 0;
245
246 return ver;
247}
248
Aaron Durbinecbfa992020-05-15 17:01:58 -0600249struct fspm_context {
250 struct fsp_header header;
251 struct memranges memmap;
252};
253
254static void do_fsp_memory_init(const struct fspm_context *context, bool s3wake)
Andrey Petrov465fc132016-02-25 14:16:33 -0800255{
Brandon Breitensteinc31ba0e2016-07-27 17:34:45 -0700256 uint32_t status;
Andrey Petrov465fc132016-02-25 14:16:33 -0800257 fsp_memory_init_fn fsp_raminit;
Brandon Breitensteinc31ba0e2016-07-27 17:34:45 -0700258 FSPM_UPD fspm_upd, *upd;
259 FSPM_ARCH_UPD *arch_upd;
Aaron Durbina3cecb22017-04-25 21:58:10 -0500260 uint32_t fsp_version;
Aaron Durbinecbfa992020-05-15 17:01:58 -0600261 const struct fsp_header *hdr = &context->header;
262 const struct memranges *memmap = &context->memmap;
Andrey Petrov465fc132016-02-25 14:16:33 -0800263
Furquan Shaikh585210a2018-10-16 11:54:37 -0700264 post_code(POST_MEM_PREINIT_PREP_START);
Andrey Petrov465fc132016-02-25 14:16:33 -0800265
Aaron Durbina3cecb22017-04-25 21:58:10 -0500266 fsp_version = fsp_memory_settings_version(hdr);
267
Brandon Breitensteinc31ba0e2016-07-27 17:34:45 -0700268 upd = (FSPM_UPD *)(hdr->cfg_region_offset + hdr->image_base);
Andrey Petrov465fc132016-02-25 14:16:33 -0800269
Lee Leahye686ee82017-03-10 08:45:30 -0800270 if (upd->FspUpdHeader.Signature != FSPM_UPD_SIGNATURE)
Keith Shortbb41aba2019-05-16 14:07:43 -0600271 die_with_post_code(POST_INVALID_VENDOR_BINARY,
272 "Invalid FSPM signature!\n");
Andrey Petrov465fc132016-02-25 14:16:33 -0800273
274 /* Copy the default values from the UPD area */
275 memcpy(&fspm_upd, upd, sizeof(fspm_upd));
276
Aaron Durbin02e504c2016-07-18 11:53:10 -0500277 arch_upd = &fspm_upd.FspmArchUpd;
278
Aaron Durbin27928682016-07-15 22:32:28 -0500279 /* Reserve enough memory under TOLUD to save CBMEM header */
Aaron Durbin02e504c2016-07-18 11:53:10 -0500280 arch_upd->BootLoaderTolumSize = cbmem_overhead_size();
Aaron Durbin27928682016-07-15 22:32:28 -0500281
Aaron Durbinb4302502016-07-17 17:04:37 -0500282 /* Fill common settings on behalf of chipset. */
Aaron Durbina3cecb22017-04-25 21:58:10 -0500283 if (fsp_fill_common_arch_params(arch_upd, s3wake, fsp_version,
Aaron Durbin02e504c2016-07-18 11:53:10 -0500284 memmap) != CB_SUCCESS)
Keith Shortbb41aba2019-05-16 14:07:43 -0600285 die_with_post_code(POST_INVALID_VENDOR_BINARY,
286 "FSPM_ARCH_UPD not found!\n");
Aaron Durbinb4302502016-07-17 17:04:37 -0500287
Andrey Petrov465fc132016-02-25 14:16:33 -0800288 /* Give SoC and mainboard a chance to update the UPD */
Aaron Durbina3cecb22017-04-25 21:58:10 -0500289 platform_fsp_memory_init_params_cb(&fspm_upd, fsp_version);
Andrey Petrov465fc132016-02-25 14:16:33 -0800290
Furquan Shaikhdbce8ba2020-06-05 19:17:00 -0700291 /*
292 * For S3 resume case, if valid mrc cache data is not found or
293 * RECOVERY_MRC_CACHE hash verification fails, the S3 data
294 * pointer would be null and S3 resume fails with fsp-m
295 * returning error. Invoking a reset here saves time.
296 */
297 if (s3wake && !arch_upd->NvsBufferPtr)
298 /* FIXME: A "system" reset is likely enough: */
299 full_reset();
300
Julius Wernercd49cce2019-03-05 16:53:33 -0800301 if (CONFIG(MMA))
Pratik Prajapatiffc934d2016-11-18 14:36:34 -0800302 setup_mma(&fspm_upd.FspmConfig);
303
Furquan Shaikh585210a2018-10-16 11:54:37 -0700304 post_code(POST_MEM_PREINIT_PREP_END);
305
Andrey Petrov465fc132016-02-25 14:16:33 -0800306 /* Call FspMemoryInit */
307 fsp_raminit = (void *)(hdr->image_base + hdr->memory_init_entry_offset);
Lee Leahyac3b0a62016-07-27 07:40:25 -0700308 fsp_debug_before_memory_init(fsp_raminit, upd, &fspm_upd);
Andrey Petrov465fc132016-02-25 14:16:33 -0800309
Alexandru Gagniucc4ea8f72016-05-23 12:16:58 -0700310 post_code(POST_FSP_MEMORY_INIT);
Andrey Petrov465fc132016-02-25 14:16:33 -0800311 timestamp_add_now(TS_FSP_MEMORY_INIT_START);
Lee Leahyac3b0a62016-07-27 07:40:25 -0700312 status = fsp_raminit(&fspm_upd, fsp_get_hob_list_ptr());
Subrata Banik0755ab92017-07-12 15:31:06 +0530313 post_code(POST_FSP_MEMORY_EXIT);
Andrey Petrov465fc132016-02-25 14:16:33 -0800314 timestamp_add_now(TS_FSP_MEMORY_INIT_END);
315
Lee Leahy9671faa2016-07-24 18:18:52 -0700316 /* Handle any errors returned by FspMemoryInit */
Aaron Durbinf41f2aa2016-07-18 12:03:58 -0500317 fsp_handle_reset(status);
Lee Leahy9671faa2016-07-24 18:18:52 -0700318 if (status != FSP_SUCCESS) {
Lee Leahyb20d4ba2016-07-31 16:49:28 -0700319 printk(BIOS_CRIT, "FspMemoryInit returned 0x%08x\n", status);
Keith Short24302632019-05-16 14:08:31 -0600320 die_with_post_code(POST_RAM_FAILURE,
321 "FspMemoryInit returned an error!\n");
Lee Leahy9671faa2016-07-24 18:18:52 -0700322 }
Aaron Durbinf41f2aa2016-07-18 12:03:58 -0500323
Aaron Durbina3cecb22017-04-25 21:58:10 -0500324 do_fsp_post_memory_init(s3wake, fsp_version);
Matthew Garrett78b58a42018-07-28 16:53:16 -0700325
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 Petrov465fc132016-02-25 14:16:33 -0800332}
333
Aaron Durbinecbfa992020-05-15 17:01:58 -0600334static int fspm_get_dest(const struct fsp_load_descriptor *fspld, void **dest,
335 size_t size, const struct region_device *source)
Aaron Durbind04639b2016-07-17 23:23:59 -0500336{
Aaron Durbinecbfa992020-05-15 17:01:58 -0600337 struct fspm_context *context = fspld->arg;
338 struct fsp_header *hdr = &context->header;
339 struct memranges *memmap = &context->memmap;
Aaron Durbind04639b2016-07-17 23:23:59 -0500340 uintptr_t fspm_begin;
341 uintptr_t fspm_end;
342
Aaron Durbinecbfa992020-05-15 17:01:58 -0600343 if (CONFIG(FSP_M_XIP)) {
344 if (fsp_validate_component(hdr, source) != CB_SUCCESS)
345 return -1;
Aaron Durbind04639b2016-07-17 23:23:59 -0500346
Aaron Durbinecbfa992020-05-15 17:01:58 -0600347 *dest = rdev_mmap_full(source);
348 if ((uintptr_t)*dest != hdr->image_base) {
349 printk(BIOS_CRIT, "FSPM XIP base does not match: %p vs %p\n",
350 (void *)(uintptr_t)hdr->image_base, *dest);
351 return -1;
352 }
353 /* Since the component is XIP it's already in the address space.
354 Thus, there's no need to rdev_munmap(). */
355 return 0;
Aaron Durbind04639b2016-07-17 23:23:59 -0500356 }
357
Aaron Durbinecbfa992020-05-15 17:01:58 -0600358 /* Non XIP FSP-M uses FSP-M address */
359 fspm_begin = (uintptr_t)CONFIG_FSP_M_ADDR;
360 fspm_end = fspm_begin + size;
361
362 if (check_region_overlap(memmap, "FSPM", fspm_begin, fspm_end) != CB_SUCCESS)
363 return -1;
364
365 *dest = (void *)fspm_begin;
366
367 return 0;
Aaron Durbind04639b2016-07-17 23:23:59 -0500368}
369
Lee Leahy9671faa2016-07-24 18:18:52 -0700370void fsp_memory_init(bool s3wake)
Andrey Petrov465fc132016-02-25 14:16:33 -0800371{
Marshall Dawsone3aa4242019-10-16 21:53:21 -0600372 struct range_entry prog_ranges[2];
Aaron Durbinecbfa992020-05-15 17:01:58 -0600373 struct fspm_context context;
374 struct fsp_load_descriptor fspld = {
375 .fsp_prog = PROG_INIT(PROG_REFCODE, CONFIG_FSP_M_CBFS),
376 .get_destination = fspm_get_dest,
377 .arg = &context,
378 };
379 struct fsp_header *hdr = &context.header;
380 struct memranges *memmap = &context.memmap;
Andrey Petrov465fc132016-02-25 14:16:33 -0800381
Kyösti Mälkki7f50afb2019-09-11 17:12:26 +0300382 elog_boot_notify(s3wake);
Furquan Shaikh5aea5882016-07-30 18:10:05 -0700383
Aaron Durbin02e504c2016-07-18 11:53:10 -0500384 /* Build up memory map of romstage address space including CAR. */
Aaron Durbinecbfa992020-05-15 17:01:58 -0600385 memranges_init_empty(memmap, &prog_ranges[0], ARRAY_SIZE(prog_ranges));
Marshall Dawsone3aa4242019-10-16 21:53:21 -0600386 if (ENV_CACHE_AS_RAM)
Aaron Durbinecbfa992020-05-15 17:01:58 -0600387 memranges_insert(memmap, (uintptr_t)_car_region_start,
Marshall Dawsone3aa4242019-10-16 21:53:21 -0600388 _car_unallocated_start - _car_region_start, 0);
Aaron Durbinecbfa992020-05-15 17:01:58 -0600389 memranges_insert(memmap, (uintptr_t)_program, REGION_SIZE(program), 0);
Aaron Durbin02e504c2016-07-18 11:53:10 -0500390
Aaron Durbinecbfa992020-05-15 17:01:58 -0600391 if (fsp_load_component(&fspld, hdr) != CB_SUCCESS)
392 die("FSPM not available or failed to load!\n");
Andrey Petrov465fc132016-02-25 14:16:33 -0800393
Kyösti Mälkki216db612019-09-11 09:57:14 +0300394 timestamp_add_now(TS_BEFORE_INITRAM);
395
Aaron Durbinecbfa992020-05-15 17:01:58 -0600396 do_fsp_memory_init(&context, s3wake);
Kyösti Mälkki0889e932019-08-18 07:40:43 +0300397
398 timestamp_add_now(TS_AFTER_INITRAM);
Andrey Petrov465fc132016-02-25 14:16:33 -0800399}