blob: 71441c28fdc86723e2fffd501f829699855be821 [file] [log] [blame]
Patrick Georgi02363b52020-05-05 20:48:50 +02001/* This file is part of the coreboot project. */
Patrick Georgiac959032020-05-05 22:49:26 +02002/* SPDX-License-Identifier: GPL-2.0-or-later */
Andrey Petrov465fc132016-02-25 14:16:33 -08003
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +01004#include <security/vboot/antirollback.h>
Aaron Durbinb4302502016-07-17 17:04:37 -05005#include <arch/symbols.h>
Aaron Durbin31be2c92016-12-03 22:08:20 -06006#include <assert.h>
Joel Kitching9a292282020-03-06 13:44:50 +08007#include <bootmode.h>
Aaron Durbind04639b2016-07-17 23:23:59 -05008#include <cbfs.h>
Aaron Durbin27928682016-07-15 22:32:28 -05009#include <cbmem.h>
Patrick Rudolphf677d172018-10-01 19:17:11 +020010#include <cf9_reset.h>
Andrey Petrov465fc132016-02-25 14:16:33 -080011#include <console/console.h>
Furquan Shaikh5aea5882016-07-30 18:10:05 -070012#include <elog.h>
Andrey Petrov465fc132016-02-25 14:16:33 -080013#include <fsp/api.h>
14#include <fsp/util.h>
15#include <memrange.h>
Aaron Durbindecd0622017-12-15 12:26:40 -070016#include <mrc_cache.h>
Aaron Durbind04639b2016-07-17 23:23:59 -050017#include <program_loading.h>
Aaron Durbinb4302502016-07-17 17:04:37 -050018#include <romstage_handoff.h>
Andrey Petrov465fc132016-02-25 14:16:33 -080019#include <string.h>
Aaron Durbind04639b2016-07-17 23:23:59 -050020#include <symbols.h>
Andrey Petrov465fc132016-02-25 14:16:33 -080021#include <timestamp.h>
Philipp Deppenwiesefea24292017-10-17 17:02:29 +020022#include <security/vboot/vboot_common.h>
Philipp Deppenwiese80961af2018-02-27 22:14:34 +010023#include <security/tpm/tspi.h>
Furquan Shaikh2db5bac2016-11-07 23:57:48 -080024#include <vb2_api.h>
Philipp Deppenwiese80961af2018-02-27 22:14:34 +010025#include <fsp/memory_init.h>
Elyes HAOUASbd1683d2019-05-15 21:05:37 +020026#include <types.h>
Andrey Petrov465fc132016-02-25 14:16:33 -080027
Kyösti Mälkkic9871502019-09-03 07:03:39 +030028static uint8_t temp_ram[CONFIG_FSP_TEMP_RAM_SIZE] __aligned(sizeof(uint64_t));
29
Joel Kitching2c8243c2019-03-11 17:47:24 +080030/* TPM MRC hash functionality depends on vboot starting before memory init. */
31_Static_assert(!CONFIG(FSP2_0_USES_TPM_MRC_HASH) ||
32 CONFIG(VBOOT_STARTS_IN_BOOTBLOCK),
33 "for TPM MRC hash functionality, vboot must start in bootblock");
34
Aaron Durbinf0ec8242016-07-18 11:24:36 -050035static void save_memory_training_data(bool s3wake, uint32_t fsp_version)
Aaron Durbinb4302502016-07-17 17:04:37 -050036{
Aaron Durbinb4302502016-07-17 17:04:37 -050037 size_t mrc_data_size;
38 const void *mrc_data;
Aaron Durbinf0ec8242016-07-18 11:24:36 -050039
Julius Wernercd49cce2019-03-05 16:53:33 -080040 if (!CONFIG(CACHE_MRC_SETTINGS) || s3wake)
Aaron Durbinf0ec8242016-07-18 11:24:36 -050041 return;
42
43 mrc_data = fsp_find_nv_storage_data(&mrc_data_size);
44 if (!mrc_data) {
45 printk(BIOS_ERR, "Couldn't find memory training data HOB.\n");
46 return;
47 }
48
49 /*
50 * Save MRC Data to CBMEM. By always saving the data this forces
51 * a retrain after a trip through Chrome OS recovery path. The
52 * code which saves the data to flash doesn't write if the latest
53 * training data matches this one.
54 */
Aaron Durbin31be2c92016-12-03 22:08:20 -060055 if (mrc_cache_stash_data(MRC_TRAINING_DATA, fsp_version, mrc_data,
56 mrc_data_size) < 0)
57 printk(BIOS_ERR, "Failed to stash MRC data\n");
Furquan Shaikh2db5bac2016-11-07 23:57:48 -080058
Julius Wernercd49cce2019-03-05 16:53:33 -080059 if (CONFIG(FSP2_0_USES_TPM_MRC_HASH))
Philipp Deppenwiese80961af2018-02-27 22:14:34 +010060 mrc_cache_update_hash(mrc_data, mrc_data_size);
Aaron Durbinf0ec8242016-07-18 11:24:36 -050061}
62
Lee Leahy9671faa2016-07-24 18:18:52 -070063static void do_fsp_post_memory_init(bool s3wake, uint32_t fsp_version)
Aaron Durbinf0ec8242016-07-18 11:24:36 -050064{
65 struct range_entry fsp_mem;
Aaron Durbinb4302502016-07-17 17:04:37 -050066
Michael Niewöhnerbc1dbb32019-10-24 22:58:25 +020067 fsp_find_reserved_memory(&fsp_mem);
Aaron Durbinb4302502016-07-17 17:04:37 -050068
69 /* initialize cbmem by adding FSP reserved memory first thing */
70 if (!s3wake) {
71 cbmem_initialize_empty_id_size(CBMEM_ID_FSP_RESERVED_MEMORY,
72 range_entry_size(&fsp_mem));
73 } else if (cbmem_initialize_id_size(CBMEM_ID_FSP_RESERVED_MEMORY,
74 range_entry_size(&fsp_mem))) {
Julius Wernercd49cce2019-03-05 16:53:33 -080075 if (CONFIG(HAVE_ACPI_RESUME)) {
Lee Leahyb20d4ba2016-07-31 16:49:28 -070076 printk(BIOS_ERR,
Lee Leahyac3b0a62016-07-27 07:40:25 -070077 "Failed to recover CBMEM in S3 resume.\n");
Aaron Durbinb4302502016-07-17 17:04:37 -050078 /* Failed S3 resume, reset to come up cleanly */
Patrick Rudolphf677d172018-10-01 19:17:11 +020079 /* FIXME: A "system" reset is likely enough: */
80 full_reset();
Aaron Durbinb4302502016-07-17 17:04:37 -050081 }
82 }
83
84 /* make sure FSP memory is reserved in cbmem */
85 if (range_entry_base(&fsp_mem) !=
86 (uintptr_t)cbmem_find(CBMEM_ID_FSP_RESERVED_MEMORY))
Lee Leahy9671faa2016-07-24 18:18:52 -070087 die("Failed to accommodate FSP reserved memory request!\n");
Aaron Durbinb4302502016-07-17 17:04:37 -050088
Aaron Durbinf0ec8242016-07-18 11:24:36 -050089 save_memory_training_data(s3wake, fsp_version);
Aaron Durbinb4302502016-07-17 17:04:37 -050090
91 /* Create romstage handof information */
Aaron Durbin77e13992016-11-29 17:43:04 -060092 romstage_handoff_init(s3wake);
Aaron Durbinb4302502016-07-17 17:04:37 -050093}
94
Aamir Bohra69cd62c2018-01-08 11:01:34 +053095static void fsp_fill_mrc_cache(FSPM_ARCH_UPD *arch_upd, uint32_t fsp_version)
Aaron Durbinb4302502016-07-17 17:04:37 -050096{
Aaron Durbin31be2c92016-12-03 22:08:20 -060097 struct region_device rdev;
98 void *data;
Aaron Durbinb4302502016-07-17 17:04:37 -050099
Aaron Durbinf0ec8242016-07-18 11:24:36 -0500100 arch_upd->NvsBufferPtr = NULL;
101
Julius Wernercd49cce2019-03-05 16:53:33 -0800102 if (!CONFIG(CACHE_MRC_SETTINGS))
Aaron Durbinf0ec8242016-07-18 11:24:36 -0500103 return;
104
Aaron Durbin31be2c92016-12-03 22:08:20 -0600105 /*
106 * In recovery mode, force retraining:
107 * 1. Recovery cache is not supported, or
108 * 2. Memory retrain switch is set.
109 */
110 if (vboot_recovery_mode_enabled()) {
Julius Wernercd49cce2019-03-05 16:53:33 -0800111 if (!CONFIG(HAS_RECOVERY_MRC_CACHE))
Aaron Durbin31be2c92016-12-03 22:08:20 -0600112 return;
Joel Kitching9a292282020-03-06 13:44:50 +0800113 if (get_recovery_mode_retrain_switch())
Aaron Durbin31be2c92016-12-03 22:08:20 -0600114 return;
115 }
Aaron Durbin98ea6362016-07-18 11:31:53 -0500116
Aaron Durbin31be2c92016-12-03 22:08:20 -0600117 if (mrc_cache_get_current(MRC_TRAINING_DATA, fsp_version, &rdev) < 0)
Aaron Durbinf0ec8242016-07-18 11:24:36 -0500118 return;
Aaron Durbinf0ec8242016-07-18 11:24:36 -0500119
Aaron Durbin31be2c92016-12-03 22:08:20 -0600120 /* Assume boot device is memory mapped. */
Julius Wernercd49cce2019-03-05 16:53:33 -0800121 assert(CONFIG(BOOT_DEVICE_MEMORY_MAPPED));
Aaron Durbin31be2c92016-12-03 22:08:20 -0600122 data = rdev_mmap_full(&rdev);
123
124 if (data == NULL)
125 return;
126
Julius Wernercd49cce2019-03-05 16:53:33 -0800127 if (CONFIG(FSP2_0_USES_TPM_MRC_HASH) &&
Philipp Deppenwiese80961af2018-02-27 22:14:34 +0100128 !mrc_cache_verify_hash(data, region_device_sz(&rdev)))
Furquan Shaikh2db5bac2016-11-07 23:57:48 -0800129 return;
130
Aaron Durbinf0ec8242016-07-18 11:24:36 -0500131 /* MRC cache found */
Aaron Durbin31be2c92016-12-03 22:08:20 -0600132 arch_upd->NvsBufferPtr = data;
Aamir Bohra69cd62c2018-01-08 11:01:34 +0530133
134 printk(BIOS_SPEW, "MRC cache found, size %zx\n",
135 region_device_sz(&rdev));
Aaron Durbinf0ec8242016-07-18 11:24:36 -0500136}
137
Aaron Durbin02e504c2016-07-18 11:53:10 -0500138static enum cb_err check_region_overlap(const struct memranges *ranges,
139 const char *description,
140 uintptr_t begin, uintptr_t end)
Aaron Durbinf0ec8242016-07-18 11:24:36 -0500141{
Aaron Durbin02e504c2016-07-18 11:53:10 -0500142 const struct range_entry *r;
143
144 memranges_each_entry(r, ranges) {
145 if (end <= range_entry_base(r))
146 continue;
147 if (begin >= range_entry_end(r))
148 continue;
Lee Leahyb20d4ba2016-07-31 16:49:28 -0700149 printk(BIOS_CRIT, "'%s' overlaps currently running program: "
Aaron Durbin02e504c2016-07-18 11:53:10 -0500150 "[%p, %p)\n", description, (void *)begin, (void *)end);
151 return CB_ERR;
152 }
153
154 return CB_SUCCESS;
155}
Kyösti Mälkkic9871502019-09-03 07:03:39 +0300156
Aamir Bohra6d569e0c2018-08-27 13:36:15 +0530157static enum cb_err setup_fsp_stack_frame(FSPM_ARCH_UPD *arch_upd,
158 const struct memranges *memmap)
Aaron Durbin02e504c2016-07-18 11:53:10 -0500159{
160 uintptr_t stack_begin;
161 uintptr_t stack_end;
162
Aaron Durbinb4302502016-07-17 17:04:37 -0500163 /*
Aamir Bohra6d569e0c2018-08-27 13:36:15 +0530164 * FSPM_UPD passed here is populated with default values
165 * provided by the blob itself. We let FSPM use top of CAR
166 * region of the size it requests.
Aaron Durbinb4302502016-07-17 17:04:37 -0500167 */
Aaron Durbin02e504c2016-07-18 11:53:10 -0500168 stack_end = (uintptr_t)_car_region_end;
169 stack_begin = stack_end - arch_upd->StackSize;
Aaron Durbin02e504c2016-07-18 11:53:10 -0500170 if (check_region_overlap(memmap, "FSPM stack", stack_begin,
171 stack_end) != CB_SUCCESS)
172 return CB_ERR;
173
174 arch_upd->StackBase = (void *)stack_begin;
Aamir Bohra6d569e0c2018-08-27 13:36:15 +0530175 return CB_SUCCESS;
176}
177
178static enum cb_err fsp_fill_common_arch_params(FSPM_ARCH_UPD *arch_upd,
179 bool s3wake, uint32_t fsp_version,
180 const struct memranges *memmap)
181{
Kyösti Mälkkic9871502019-09-03 07:03:39 +0300182 /*
183 * FSP 2.1 version would use same stack as coreboot instead of
184 * setting up separate stack frame. FSP 2.1 would not relocate stack
185 * top and does not reinitialize stack pointer. The parameters passed
186 * as StackBase and StackSize are actually for temporary RAM and HOBs
187 * and are not related to FSP stack at all.
188 */
189 if (CONFIG(FSP_USES_CB_STACK)) {
190 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) {
200 /*
201 * For S3 resume case, if valid mrc cache data is not found or
202 * RECOVERY_MRC_CACHE hash verification fails, the S3 data
203 * pointer would be null and S3 resume fails with fsp-m
204 * returning error. Invoking a reset here saves time.
205 */
206 if (!arch_upd->NvsBufferPtr)
Patrick Rudolphf677d172018-10-01 19:17:11 +0200207 /* FIXME: A "system" reset is likely enough: */
208 full_reset();
Aamir Bohra69cd62c2018-01-08 11:01:34 +0530209 arch_upd->BootMode = FSP_BOOT_ON_S3_RESUME;
210 } else {
211 if (arch_upd->NvsBufferPtr)
212 arch_upd->BootMode =
213 FSP_BOOT_ASSUMING_NO_CONFIGURATION_CHANGES;
214 else
215 arch_upd->BootMode = FSP_BOOT_WITH_FULL_CONFIGURATION;
216 }
Aaron Durbin02e504c2016-07-18 11:53:10 -0500217
Marshall Dawson22d66ef2019-08-30 14:52:37 -0600218 printk(BIOS_SPEW, "bootmode is set to: %d\n", arch_upd->BootMode);
Aamir Bohra276c06a2017-12-26 17:54:45 +0530219
Aaron Durbin02e504c2016-07-18 11:53:10 -0500220 return CB_SUCCESS;
Aaron Durbinb4302502016-07-17 17:04:37 -0500221}
222
Aaron Durbin64031672018-04-21 14:45:32 -0600223__weak
Aaron Durbina3cecb22017-04-25 21:58:10 -0500224uint8_t fsp_memory_mainboard_version(void)
225{
226 return 0;
227}
228
Aaron Durbin64031672018-04-21 14:45:32 -0600229__weak
Aaron Durbina3cecb22017-04-25 21:58:10 -0500230uint8_t fsp_memory_soc_version(void)
231{
232 return 0;
233}
234
235/*
236 * Allow SoC and/or mainboard to bump the revision of the FSP setting
237 * number. The FSP spec uses the low 8 bits as the build number. Take over
238 * bits 3:0 for the SoC setting and bits 7:4 for the mainboard. That way
239 * a tweak in the settings will bump the version used to track the cached
240 * setting which triggers retraining when the FSP version hasn't changed, but
241 * the SoC or mainboard settings have.
242 */
243static uint32_t fsp_memory_settings_version(const struct fsp_header *hdr)
244{
245 /* Use the full FSP version by default. */
246 uint32_t ver = hdr->fsp_revision;
247
Julius Wernercd49cce2019-03-05 16:53:33 -0800248 if (!CONFIG(FSP_PLATFORM_MEMORY_SETTINGS_VERSIONS))
Aaron Durbina3cecb22017-04-25 21:58:10 -0500249 return ver;
250
251 ver &= ~0xff;
252 ver |= (0xf & fsp_memory_mainboard_version()) << 4;
253 ver |= (0xf & fsp_memory_soc_version()) << 0;
254
255 return ver;
256}
257
Lee Leahy9671faa2016-07-24 18:18:52 -0700258static void do_fsp_memory_init(struct fsp_header *hdr, bool s3wake,
Aaron Durbin02e504c2016-07-18 11:53:10 -0500259 const struct memranges *memmap)
Andrey Petrov465fc132016-02-25 14:16:33 -0800260{
Brandon Breitensteinc31ba0e2016-07-27 17:34:45 -0700261 uint32_t status;
Andrey Petrov465fc132016-02-25 14:16:33 -0800262 fsp_memory_init_fn fsp_raminit;
Brandon Breitensteinc31ba0e2016-07-27 17:34:45 -0700263 FSPM_UPD fspm_upd, *upd;
264 FSPM_ARCH_UPD *arch_upd;
Aaron Durbina3cecb22017-04-25 21:58:10 -0500265 uint32_t fsp_version;
Andrey Petrov465fc132016-02-25 14:16:33 -0800266
Furquan Shaikh585210a2018-10-16 11:54:37 -0700267 post_code(POST_MEM_PREINIT_PREP_START);
Andrey Petrov465fc132016-02-25 14:16:33 -0800268
Aaron Durbina3cecb22017-04-25 21:58:10 -0500269 fsp_version = fsp_memory_settings_version(hdr);
270
Brandon Breitensteinc31ba0e2016-07-27 17:34:45 -0700271 upd = (FSPM_UPD *)(hdr->cfg_region_offset + hdr->image_base);
Andrey Petrov465fc132016-02-25 14:16:33 -0800272
Lee Leahye686ee82017-03-10 08:45:30 -0800273 if (upd->FspUpdHeader.Signature != FSPM_UPD_SIGNATURE)
Keith Shortbb41aba2019-05-16 14:07:43 -0600274 die_with_post_code(POST_INVALID_VENDOR_BINARY,
275 "Invalid FSPM signature!\n");
Andrey Petrov465fc132016-02-25 14:16:33 -0800276
277 /* Copy the default values from the UPD area */
278 memcpy(&fspm_upd, upd, sizeof(fspm_upd));
279
Aaron Durbin02e504c2016-07-18 11:53:10 -0500280 arch_upd = &fspm_upd.FspmArchUpd;
281
Aaron Durbin27928682016-07-15 22:32:28 -0500282 /* Reserve enough memory under TOLUD to save CBMEM header */
Aaron Durbin02e504c2016-07-18 11:53:10 -0500283 arch_upd->BootLoaderTolumSize = cbmem_overhead_size();
Aaron Durbin27928682016-07-15 22:32:28 -0500284
Aaron Durbinb4302502016-07-17 17:04:37 -0500285 /* Fill common settings on behalf of chipset. */
Aaron Durbina3cecb22017-04-25 21:58:10 -0500286 if (fsp_fill_common_arch_params(arch_upd, s3wake, fsp_version,
Aaron Durbin02e504c2016-07-18 11:53:10 -0500287 memmap) != CB_SUCCESS)
Keith Shortbb41aba2019-05-16 14:07:43 -0600288 die_with_post_code(POST_INVALID_VENDOR_BINARY,
289 "FSPM_ARCH_UPD not found!\n");
Aaron Durbinb4302502016-07-17 17:04:37 -0500290
Andrey Petrov465fc132016-02-25 14:16:33 -0800291 /* Give SoC and mainboard a chance to update the UPD */
Aaron Durbina3cecb22017-04-25 21:58:10 -0500292 platform_fsp_memory_init_params_cb(&fspm_upd, fsp_version);
Andrey Petrov465fc132016-02-25 14:16:33 -0800293
Julius Wernercd49cce2019-03-05 16:53:33 -0800294 if (CONFIG(MMA))
Pratik Prajapatiffc934d2016-11-18 14:36:34 -0800295 setup_mma(&fspm_upd.FspmConfig);
296
Furquan Shaikh585210a2018-10-16 11:54:37 -0700297 post_code(POST_MEM_PREINIT_PREP_END);
298
Andrey Petrov465fc132016-02-25 14:16:33 -0800299 /* Call FspMemoryInit */
300 fsp_raminit = (void *)(hdr->image_base + hdr->memory_init_entry_offset);
Lee Leahyac3b0a62016-07-27 07:40:25 -0700301 fsp_debug_before_memory_init(fsp_raminit, upd, &fspm_upd);
Andrey Petrov465fc132016-02-25 14:16:33 -0800302
Alexandru Gagniucc4ea8f72016-05-23 12:16:58 -0700303 post_code(POST_FSP_MEMORY_INIT);
Andrey Petrov465fc132016-02-25 14:16:33 -0800304 timestamp_add_now(TS_FSP_MEMORY_INIT_START);
Lee Leahyac3b0a62016-07-27 07:40:25 -0700305 status = fsp_raminit(&fspm_upd, fsp_get_hob_list_ptr());
Subrata Banik0755ab92017-07-12 15:31:06 +0530306 post_code(POST_FSP_MEMORY_EXIT);
Andrey Petrov465fc132016-02-25 14:16:33 -0800307 timestamp_add_now(TS_FSP_MEMORY_INIT_END);
308
Lee Leahy9671faa2016-07-24 18:18:52 -0700309 /* Handle any errors returned by FspMemoryInit */
Aaron Durbinf41f2aa2016-07-18 12:03:58 -0500310 fsp_handle_reset(status);
Lee Leahy9671faa2016-07-24 18:18:52 -0700311 if (status != FSP_SUCCESS) {
Lee Leahyb20d4ba2016-07-31 16:49:28 -0700312 printk(BIOS_CRIT, "FspMemoryInit returned 0x%08x\n", status);
Keith Short24302632019-05-16 14:08:31 -0600313 die_with_post_code(POST_RAM_FAILURE,
314 "FspMemoryInit returned an error!\n");
Lee Leahy9671faa2016-07-24 18:18:52 -0700315 }
Aaron Durbinf41f2aa2016-07-18 12:03:58 -0500316
Aaron Durbina3cecb22017-04-25 21:58:10 -0500317 do_fsp_post_memory_init(s3wake, fsp_version);
Matthew Garrett78b58a42018-07-28 16:53:16 -0700318
319 /*
320 * fsp_debug_after_memory_init() checks whether the end of the tolum
321 * region is the same as the top of cbmem, so must be called here
322 * after cbmem has been initialised in do_fsp_post_memory_init().
323 */
324 fsp_debug_after_memory_init(status);
Andrey Petrov465fc132016-02-25 14:16:33 -0800325}
326
Aaron Durbind04639b2016-07-17 23:23:59 -0500327/* Load the binary into the memory specified by the info header. */
328static enum cb_err load_fspm_mem(struct fsp_header *hdr,
Aaron Durbin02e504c2016-07-18 11:53:10 -0500329 const struct region_device *rdev,
330 const struct memranges *memmap)
Aaron Durbind04639b2016-07-17 23:23:59 -0500331{
Aaron Durbind04639b2016-07-17 23:23:59 -0500332 uintptr_t fspm_begin;
333 uintptr_t fspm_end;
334
335 if (fsp_validate_component(hdr, rdev) != CB_SUCCESS)
336 return CB_ERR;
337
338 fspm_begin = hdr->image_base;
339 fspm_end = fspm_begin + hdr->image_size;
340
Aaron Durbin02e504c2016-07-18 11:53:10 -0500341 if (check_region_overlap(memmap, "FSPM", fspm_begin, fspm_end) !=
342 CB_SUCCESS)
Aaron Durbind04639b2016-07-17 23:23:59 -0500343 return CB_ERR;
Aaron Durbind04639b2016-07-17 23:23:59 -0500344
345 /* Load binary into memory at provided address. */
346 if (rdev_readat(rdev, (void *)fspm_begin, 0, fspm_end - fspm_begin) < 0)
347 return CB_ERR;
348
349 return CB_SUCCESS;
350}
351
352/* Handle the case when FSPM is running XIP. */
353static enum cb_err load_fspm_xip(struct fsp_header *hdr,
354 const struct region_device *rdev)
355{
356 void *base;
357
358 if (fsp_validate_component(hdr, rdev) != CB_SUCCESS)
359 return CB_ERR;
360
361 base = rdev_mmap_full(rdev);
362 if ((uintptr_t)base != hdr->image_base) {
Lee Leahyb20d4ba2016-07-31 16:49:28 -0700363 printk(BIOS_CRIT, "FSPM XIP base does not match: %p vs %p\n",
Aaron Durbind04639b2016-07-17 23:23:59 -0500364 (void *)(uintptr_t)hdr->image_base, base);
365 return CB_ERR;
366 }
367
368 /*
369 * Since the component is XIP it's already in the address space. Thus,
370 * there's no need to rdev_munmap().
371 */
372 return CB_SUCCESS;
373}
374
Lee Leahy9671faa2016-07-24 18:18:52 -0700375void fsp_memory_init(bool s3wake)
Andrey Petrov465fc132016-02-25 14:16:33 -0800376{
377 struct fsp_header hdr;
Aaron Durbind04639b2016-07-17 23:23:59 -0500378 enum cb_err status;
379 struct cbfsf file_desc;
380 struct region_device file_data;
381 const char *name = CONFIG_FSP_M_CBFS;
Aaron Durbin02e504c2016-07-18 11:53:10 -0500382 struct memranges memmap;
Marshall Dawsone3aa4242019-10-16 21:53:21 -0600383 struct range_entry prog_ranges[2];
Andrey Petrov465fc132016-02-25 14:16:33 -0800384
Kyösti Mälkki7f50afb2019-09-11 17:12:26 +0300385 elog_boot_notify(s3wake);
Furquan Shaikh5aea5882016-07-30 18:10:05 -0700386
Aaron Durbind04639b2016-07-17 23:23:59 -0500387 if (cbfs_boot_locate(&file_desc, name, NULL)) {
Lee Leahyb20d4ba2016-07-31 16:49:28 -0700388 printk(BIOS_CRIT, "Could not locate %s in CBFS\n", name);
Lee Leahy9671faa2016-07-24 18:18:52 -0700389 die("FSPM not available!\n");
Aaron Durbind04639b2016-07-17 23:23:59 -0500390 }
391
392 cbfs_file_data(&file_data, &file_desc);
393
Aaron Durbin02e504c2016-07-18 11:53:10 -0500394 /* Build up memory map of romstage address space including CAR. */
Marshall Dawsone3aa4242019-10-16 21:53:21 -0600395 memranges_init_empty(&memmap, &prog_ranges[0], ARRAY_SIZE(prog_ranges));
396 if (ENV_CACHE_AS_RAM)
397 memranges_insert(&memmap, (uintptr_t)_car_region_start,
398 _car_unallocated_start - _car_region_start, 0);
Julius Werner7e0dea62019-02-20 18:39:22 -0800399 memranges_insert(&memmap, (uintptr_t)_program, REGION_SIZE(program), 0);
Aaron Durbin02e504c2016-07-18 11:53:10 -0500400
Julius Wernercd49cce2019-03-05 16:53:33 -0800401 if (!CONFIG(FSP_M_XIP))
Aaron Durbin02e504c2016-07-18 11:53:10 -0500402 status = load_fspm_mem(&hdr, &file_data, &memmap);
Aaron Durbind04639b2016-07-17 23:23:59 -0500403 else
404 status = load_fspm_xip(&hdr, &file_data);
405
Lee Leahye686ee82017-03-10 08:45:30 -0800406 if (status != CB_SUCCESS)
Lee Leahy9671faa2016-07-24 18:18:52 -0700407 die("Loading FSPM failed!\n");
Aaron Durbind04639b2016-07-17 23:23:59 -0500408
409 /* Signal that FSP component has been loaded. */
410 prog_segment_loaded(hdr.image_base, hdr.image_size, SEG_FINAL);
Andrey Petrov465fc132016-02-25 14:16:33 -0800411
Kyösti Mälkki216db612019-09-11 09:57:14 +0300412 timestamp_add_now(TS_BEFORE_INITRAM);
413
Lee Leahy9671faa2016-07-24 18:18:52 -0700414 do_fsp_memory_init(&hdr, s3wake, &memmap);
Kyösti Mälkki0889e932019-08-18 07:40:43 +0300415
416 timestamp_add_now(TS_AFTER_INITRAM);
Andrey Petrov465fc132016-02-25 14:16:33 -0800417}