blob: 5765fb73117d5a8832d6e65996ef59b7501fdd23 [file] [log] [blame]
Andrey Petrov465fc132016-02-25 14:16:33 -08001/*
2 * This file is part of the coreboot project.
3 *
Lee Leahy47bd2d92016-07-24 18:12:16 -07004 * Copyright (C) 2015-2016 Intel Corp.
Andrey Petrov465fc132016-02-25 14:16:33 -08005 * (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 Deppenwiesec07f8fb2018-02-27 19:40:52 +010014#include <security/vboot/antirollback.h>
Aaron Durbinb4302502016-07-17 17:04:37 -050015#include <arch/symbols.h>
Aaron Durbin31be2c92016-12-03 22:08:20 -060016#include <assert.h>
Aaron Durbind04639b2016-07-17 23:23:59 -050017#include <cbfs.h>
Aaron Durbin27928682016-07-15 22:32:28 -050018#include <cbmem.h>
Patrick Rudolphf677d172018-10-01 19:17:11 +020019#include <cf9_reset.h>
Andrey Petrov465fc132016-02-25 14:16:33 -080020#include <console/console.h>
Furquan Shaikh5aea5882016-07-30 18:10:05 -070021#include <elog.h>
Andrey Petrov465fc132016-02-25 14:16:33 -080022#include <fsp/api.h>
23#include <fsp/util.h>
24#include <memrange.h>
Aaron Durbindecd0622017-12-15 12:26:40 -070025#include <mrc_cache.h>
Aaron Durbind04639b2016-07-17 23:23:59 -050026#include <program_loading.h>
Aaron Durbinb4302502016-07-17 17:04:37 -050027#include <romstage_handoff.h>
Andrey Petrov465fc132016-02-25 14:16:33 -080028#include <string.h>
Aaron Durbind04639b2016-07-17 23:23:59 -050029#include <symbols.h>
Andrey Petrov465fc132016-02-25 14:16:33 -080030#include <timestamp.h>
Philipp Deppenwiesefea24292017-10-17 17:02:29 +020031#include <security/vboot/vboot_common.h>
Philipp Deppenwiese80961af2018-02-27 22:14:34 +010032#include <security/tpm/tspi.h>
Furquan Shaikh2db5bac2016-11-07 23:57:48 -080033#include <vb2_api.h>
Philipp Deppenwiese80961af2018-02-27 22:14:34 +010034#include <fsp/memory_init.h>
Elyes HAOUASbd1683d2019-05-15 21:05:37 +020035#include <types.h>
Andrey Petrov465fc132016-02-25 14:16:33 -080036
Kyösti Mälkkic9871502019-09-03 07:03:39 +030037static uint8_t temp_ram[CONFIG_FSP_TEMP_RAM_SIZE] __aligned(sizeof(uint64_t));
38
Joel Kitching2c8243c2019-03-11 17:47:24 +080039/* TPM MRC hash functionality depends on vboot starting before memory init. */
40_Static_assert(!CONFIG(FSP2_0_USES_TPM_MRC_HASH) ||
41 CONFIG(VBOOT_STARTS_IN_BOOTBLOCK),
42 "for TPM MRC hash functionality, vboot must start in bootblock");
43
Aaron Durbinf0ec8242016-07-18 11:24:36 -050044static void save_memory_training_data(bool s3wake, uint32_t fsp_version)
Aaron Durbinb4302502016-07-17 17:04:37 -050045{
Aaron Durbinb4302502016-07-17 17:04:37 -050046 size_t mrc_data_size;
47 const void *mrc_data;
Aaron Durbinf0ec8242016-07-18 11:24:36 -050048
Julius Wernercd49cce2019-03-05 16:53:33 -080049 if (!CONFIG(CACHE_MRC_SETTINGS) || s3wake)
Aaron Durbinf0ec8242016-07-18 11:24:36 -050050 return;
51
52 mrc_data = fsp_find_nv_storage_data(&mrc_data_size);
53 if (!mrc_data) {
54 printk(BIOS_ERR, "Couldn't find memory training data HOB.\n");
55 return;
56 }
57
58 /*
59 * Save MRC Data to CBMEM. By always saving the data this forces
60 * a retrain after a trip through Chrome OS recovery path. The
61 * code which saves the data to flash doesn't write if the latest
62 * training data matches this one.
63 */
Aaron Durbin31be2c92016-12-03 22:08:20 -060064 if (mrc_cache_stash_data(MRC_TRAINING_DATA, fsp_version, mrc_data,
65 mrc_data_size) < 0)
66 printk(BIOS_ERR, "Failed to stash MRC data\n");
Furquan Shaikh2db5bac2016-11-07 23:57:48 -080067
Julius Wernercd49cce2019-03-05 16:53:33 -080068 if (CONFIG(FSP2_0_USES_TPM_MRC_HASH))
Philipp Deppenwiese80961af2018-02-27 22:14:34 +010069 mrc_cache_update_hash(mrc_data, mrc_data_size);
Aaron Durbinf0ec8242016-07-18 11:24:36 -050070}
71
Lee Leahy9671faa2016-07-24 18:18:52 -070072static void do_fsp_post_memory_init(bool s3wake, uint32_t fsp_version)
Aaron Durbinf0ec8242016-07-18 11:24:36 -050073{
74 struct range_entry fsp_mem;
Aaron Durbinb4302502016-07-17 17:04:37 -050075
Lee Leahy52d0c682016-08-01 15:47:42 -070076 if (fsp_find_reserved_memory(&fsp_mem))
77 die("Failed to find FSP_RESERVED_MEMORY_RESOURCE_HOB!\n");
Aaron Durbinb4302502016-07-17 17:04:37 -050078
79 /* initialize cbmem by adding FSP reserved memory first thing */
80 if (!s3wake) {
81 cbmem_initialize_empty_id_size(CBMEM_ID_FSP_RESERVED_MEMORY,
82 range_entry_size(&fsp_mem));
83 } else if (cbmem_initialize_id_size(CBMEM_ID_FSP_RESERVED_MEMORY,
84 range_entry_size(&fsp_mem))) {
Julius Wernercd49cce2019-03-05 16:53:33 -080085 if (CONFIG(HAVE_ACPI_RESUME)) {
Lee Leahyb20d4ba2016-07-31 16:49:28 -070086 printk(BIOS_ERR,
Lee Leahyac3b0a62016-07-27 07:40:25 -070087 "Failed to recover CBMEM in S3 resume.\n");
Aaron Durbinb4302502016-07-17 17:04:37 -050088 /* Failed S3 resume, reset to come up cleanly */
Patrick Rudolphf677d172018-10-01 19:17:11 +020089 /* FIXME: A "system" reset is likely enough: */
90 full_reset();
Aaron Durbinb4302502016-07-17 17:04:37 -050091 }
92 }
93
94 /* make sure FSP memory is reserved in cbmem */
95 if (range_entry_base(&fsp_mem) !=
96 (uintptr_t)cbmem_find(CBMEM_ID_FSP_RESERVED_MEMORY))
Lee Leahy9671faa2016-07-24 18:18:52 -070097 die("Failed to accommodate FSP reserved memory request!\n");
Aaron Durbinb4302502016-07-17 17:04:37 -050098
Aaron Durbinf0ec8242016-07-18 11:24:36 -050099 save_memory_training_data(s3wake, fsp_version);
Aaron Durbinb4302502016-07-17 17:04:37 -0500100
101 /* Create romstage handof information */
Aaron Durbin77e13992016-11-29 17:43:04 -0600102 romstage_handoff_init(s3wake);
Aaron Durbinb4302502016-07-17 17:04:37 -0500103}
104
Aamir Bohra69cd62c2018-01-08 11:01:34 +0530105static void fsp_fill_mrc_cache(FSPM_ARCH_UPD *arch_upd, uint32_t fsp_version)
Aaron Durbinb4302502016-07-17 17:04:37 -0500106{
Aaron Durbin31be2c92016-12-03 22:08:20 -0600107 struct region_device rdev;
108 void *data;
Aaron Durbinb4302502016-07-17 17:04:37 -0500109
Aaron Durbinf0ec8242016-07-18 11:24:36 -0500110 arch_upd->NvsBufferPtr = NULL;
111
Julius Wernercd49cce2019-03-05 16:53:33 -0800112 if (!CONFIG(CACHE_MRC_SETTINGS))
Aaron Durbinf0ec8242016-07-18 11:24:36 -0500113 return;
114
Aaron Durbin31be2c92016-12-03 22:08:20 -0600115 /*
116 * In recovery mode, force retraining:
117 * 1. Recovery cache is not supported, or
118 * 2. Memory retrain switch is set.
119 */
120 if (vboot_recovery_mode_enabled()) {
Julius Wernercd49cce2019-03-05 16:53:33 -0800121 if (!CONFIG(HAS_RECOVERY_MRC_CACHE))
Aaron Durbin31be2c92016-12-03 22:08:20 -0600122 return;
123 if (vboot_recovery_mode_memory_retrain())
124 return;
125 }
Aaron Durbin98ea6362016-07-18 11:31:53 -0500126
Aaron Durbin31be2c92016-12-03 22:08:20 -0600127 if (mrc_cache_get_current(MRC_TRAINING_DATA, fsp_version, &rdev) < 0)
Aaron Durbinf0ec8242016-07-18 11:24:36 -0500128 return;
Aaron Durbinf0ec8242016-07-18 11:24:36 -0500129
Aaron Durbin31be2c92016-12-03 22:08:20 -0600130 /* Assume boot device is memory mapped. */
Julius Wernercd49cce2019-03-05 16:53:33 -0800131 assert(CONFIG(BOOT_DEVICE_MEMORY_MAPPED));
Aaron Durbin31be2c92016-12-03 22:08:20 -0600132 data = rdev_mmap_full(&rdev);
133
134 if (data == NULL)
135 return;
136
Julius Wernercd49cce2019-03-05 16:53:33 -0800137 if (CONFIG(FSP2_0_USES_TPM_MRC_HASH) &&
Philipp Deppenwiese80961af2018-02-27 22:14:34 +0100138 !mrc_cache_verify_hash(data, region_device_sz(&rdev)))
Furquan Shaikh2db5bac2016-11-07 23:57:48 -0800139 return;
140
Aaron Durbinf0ec8242016-07-18 11:24:36 -0500141 /* MRC cache found */
Aaron Durbin31be2c92016-12-03 22:08:20 -0600142 arch_upd->NvsBufferPtr = data;
Aamir Bohra69cd62c2018-01-08 11:01:34 +0530143
144 printk(BIOS_SPEW, "MRC cache found, size %zx\n",
145 region_device_sz(&rdev));
Aaron Durbinf0ec8242016-07-18 11:24:36 -0500146}
147
Aaron Durbin02e504c2016-07-18 11:53:10 -0500148static enum cb_err check_region_overlap(const struct memranges *ranges,
149 const char *description,
150 uintptr_t begin, uintptr_t end)
Aaron Durbinf0ec8242016-07-18 11:24:36 -0500151{
Aaron Durbin02e504c2016-07-18 11:53:10 -0500152 const struct range_entry *r;
153
154 memranges_each_entry(r, ranges) {
155 if (end <= range_entry_base(r))
156 continue;
157 if (begin >= range_entry_end(r))
158 continue;
Lee Leahyb20d4ba2016-07-31 16:49:28 -0700159 printk(BIOS_CRIT, "'%s' overlaps currently running program: "
Aaron Durbin02e504c2016-07-18 11:53:10 -0500160 "[%p, %p)\n", description, (void *)begin, (void *)end);
161 return CB_ERR;
162 }
163
164 return CB_SUCCESS;
165}
Kyösti Mälkkic9871502019-09-03 07:03:39 +0300166
Aamir Bohra6d569e0c2018-08-27 13:36:15 +0530167static enum cb_err setup_fsp_stack_frame(FSPM_ARCH_UPD *arch_upd,
168 const struct memranges *memmap)
Aaron Durbin02e504c2016-07-18 11:53:10 -0500169{
170 uintptr_t stack_begin;
171 uintptr_t stack_end;
172
Aaron Durbinb4302502016-07-17 17:04:37 -0500173 /*
Aamir Bohra6d569e0c2018-08-27 13:36:15 +0530174 * FSPM_UPD passed here is populated with default values
175 * provided by the blob itself. We let FSPM use top of CAR
176 * region of the size it requests.
Aaron Durbinb4302502016-07-17 17:04:37 -0500177 */
Aaron Durbin02e504c2016-07-18 11:53:10 -0500178 stack_end = (uintptr_t)_car_region_end;
179 stack_begin = stack_end - arch_upd->StackSize;
Aaron Durbin02e504c2016-07-18 11:53:10 -0500180 if (check_region_overlap(memmap, "FSPM stack", stack_begin,
181 stack_end) != CB_SUCCESS)
182 return CB_ERR;
183
184 arch_upd->StackBase = (void *)stack_begin;
Aamir Bohra6d569e0c2018-08-27 13:36:15 +0530185 return CB_SUCCESS;
186}
187
188static enum cb_err fsp_fill_common_arch_params(FSPM_ARCH_UPD *arch_upd,
189 bool s3wake, uint32_t fsp_version,
190 const struct memranges *memmap)
191{
Kyösti Mälkkic9871502019-09-03 07:03:39 +0300192 /*
193 * FSP 2.1 version would use same stack as coreboot instead of
194 * setting up separate stack frame. FSP 2.1 would not relocate stack
195 * top and does not reinitialize stack pointer. The parameters passed
196 * as StackBase and StackSize are actually for temporary RAM and HOBs
197 * and are not related to FSP stack at all.
198 */
199 if (CONFIG(FSP_USES_CB_STACK)) {
200 arch_upd->StackBase = temp_ram;
201 arch_upd->StackSize = sizeof(temp_ram);
202 } else if (setup_fsp_stack_frame(arch_upd, memmap)) {
Aamir Bohra6d569e0c2018-08-27 13:36:15 +0530203 return CB_ERR;
Kyösti Mälkkic9871502019-09-03 07:03:39 +0300204 }
Aaron Durbinb4302502016-07-17 17:04:37 -0500205
Aamir Bohra69cd62c2018-01-08 11:01:34 +0530206 fsp_fill_mrc_cache(arch_upd, fsp_version);
Aaron Durbinb4302502016-07-17 17:04:37 -0500207
Aamir Bohra69cd62c2018-01-08 11:01:34 +0530208 /* Configure bootmode */
209 if (s3wake) {
210 /*
211 * For S3 resume case, if valid mrc cache data is not found or
212 * RECOVERY_MRC_CACHE hash verification fails, the S3 data
213 * pointer would be null and S3 resume fails with fsp-m
214 * returning error. Invoking a reset here saves time.
215 */
216 if (!arch_upd->NvsBufferPtr)
Patrick Rudolphf677d172018-10-01 19:17:11 +0200217 /* FIXME: A "system" reset is likely enough: */
218 full_reset();
Aamir Bohra69cd62c2018-01-08 11:01:34 +0530219 arch_upd->BootMode = FSP_BOOT_ON_S3_RESUME;
220 } else {
221 if (arch_upd->NvsBufferPtr)
222 arch_upd->BootMode =
223 FSP_BOOT_ASSUMING_NO_CONFIGURATION_CHANGES;
224 else
225 arch_upd->BootMode = FSP_BOOT_WITH_FULL_CONFIGURATION;
226 }
Aaron Durbin02e504c2016-07-18 11:53:10 -0500227
Marshall Dawson22d66ef2019-08-30 14:52:37 -0600228 printk(BIOS_SPEW, "bootmode is set to: %d\n", arch_upd->BootMode);
Aamir Bohra276c06a2017-12-26 17:54:45 +0530229
Aaron Durbin02e504c2016-07-18 11:53:10 -0500230 return CB_SUCCESS;
Aaron Durbinb4302502016-07-17 17:04:37 -0500231}
232
Aaron Durbin64031672018-04-21 14:45:32 -0600233__weak
Aaron Durbina3cecb22017-04-25 21:58:10 -0500234uint8_t fsp_memory_mainboard_version(void)
235{
236 return 0;
237}
238
Aaron Durbin64031672018-04-21 14:45:32 -0600239__weak
Aaron Durbina3cecb22017-04-25 21:58:10 -0500240uint8_t fsp_memory_soc_version(void)
241{
242 return 0;
243}
244
245/*
246 * Allow SoC and/or mainboard to bump the revision of the FSP setting
247 * number. The FSP spec uses the low 8 bits as the build number. Take over
248 * bits 3:0 for the SoC setting and bits 7:4 for the mainboard. That way
249 * a tweak in the settings will bump the version used to track the cached
250 * setting which triggers retraining when the FSP version hasn't changed, but
251 * the SoC or mainboard settings have.
252 */
253static uint32_t fsp_memory_settings_version(const struct fsp_header *hdr)
254{
255 /* Use the full FSP version by default. */
256 uint32_t ver = hdr->fsp_revision;
257
Julius Wernercd49cce2019-03-05 16:53:33 -0800258 if (!CONFIG(FSP_PLATFORM_MEMORY_SETTINGS_VERSIONS))
Aaron Durbina3cecb22017-04-25 21:58:10 -0500259 return ver;
260
261 ver &= ~0xff;
262 ver |= (0xf & fsp_memory_mainboard_version()) << 4;
263 ver |= (0xf & fsp_memory_soc_version()) << 0;
264
265 return ver;
266}
267
Lee Leahy9671faa2016-07-24 18:18:52 -0700268static void do_fsp_memory_init(struct fsp_header *hdr, bool s3wake,
Aaron Durbin02e504c2016-07-18 11:53:10 -0500269 const struct memranges *memmap)
Andrey Petrov465fc132016-02-25 14:16:33 -0800270{
Brandon Breitensteinc31ba0e2016-07-27 17:34:45 -0700271 uint32_t status;
Andrey Petrov465fc132016-02-25 14:16:33 -0800272 fsp_memory_init_fn fsp_raminit;
Brandon Breitensteinc31ba0e2016-07-27 17:34:45 -0700273 FSPM_UPD fspm_upd, *upd;
274 FSPM_ARCH_UPD *arch_upd;
Aaron Durbina3cecb22017-04-25 21:58:10 -0500275 uint32_t fsp_version;
Andrey Petrov465fc132016-02-25 14:16:33 -0800276
Furquan Shaikh585210a2018-10-16 11:54:37 -0700277 post_code(POST_MEM_PREINIT_PREP_START);
Andrey Petrov465fc132016-02-25 14:16:33 -0800278
Aaron Durbina3cecb22017-04-25 21:58:10 -0500279 fsp_version = fsp_memory_settings_version(hdr);
280
Brandon Breitensteinc31ba0e2016-07-27 17:34:45 -0700281 upd = (FSPM_UPD *)(hdr->cfg_region_offset + hdr->image_base);
Andrey Petrov465fc132016-02-25 14:16:33 -0800282
Lee Leahye686ee82017-03-10 08:45:30 -0800283 if (upd->FspUpdHeader.Signature != FSPM_UPD_SIGNATURE)
Keith Shortbb41aba2019-05-16 14:07:43 -0600284 die_with_post_code(POST_INVALID_VENDOR_BINARY,
285 "Invalid FSPM signature!\n");
Andrey Petrov465fc132016-02-25 14:16:33 -0800286
287 /* Copy the default values from the UPD area */
288 memcpy(&fspm_upd, upd, sizeof(fspm_upd));
289
Aaron Durbin02e504c2016-07-18 11:53:10 -0500290 arch_upd = &fspm_upd.FspmArchUpd;
291
Aaron Durbin27928682016-07-15 22:32:28 -0500292 /* Reserve enough memory under TOLUD to save CBMEM header */
Aaron Durbin02e504c2016-07-18 11:53:10 -0500293 arch_upd->BootLoaderTolumSize = cbmem_overhead_size();
Aaron Durbin27928682016-07-15 22:32:28 -0500294
Aaron Durbinb4302502016-07-17 17:04:37 -0500295 /* Fill common settings on behalf of chipset. */
Aaron Durbina3cecb22017-04-25 21:58:10 -0500296 if (fsp_fill_common_arch_params(arch_upd, s3wake, fsp_version,
Aaron Durbin02e504c2016-07-18 11:53:10 -0500297 memmap) != CB_SUCCESS)
Keith Shortbb41aba2019-05-16 14:07:43 -0600298 die_with_post_code(POST_INVALID_VENDOR_BINARY,
299 "FSPM_ARCH_UPD not found!\n");
Aaron Durbinb4302502016-07-17 17:04:37 -0500300
Andrey Petrov465fc132016-02-25 14:16:33 -0800301 /* Give SoC and mainboard a chance to update the UPD */
Aaron Durbina3cecb22017-04-25 21:58:10 -0500302 platform_fsp_memory_init_params_cb(&fspm_upd, fsp_version);
Andrey Petrov465fc132016-02-25 14:16:33 -0800303
Julius Wernercd49cce2019-03-05 16:53:33 -0800304 if (CONFIG(MMA))
Pratik Prajapatiffc934d2016-11-18 14:36:34 -0800305 setup_mma(&fspm_upd.FspmConfig);
306
Furquan Shaikh585210a2018-10-16 11:54:37 -0700307 post_code(POST_MEM_PREINIT_PREP_END);
308
Andrey Petrov465fc132016-02-25 14:16:33 -0800309 /* Call FspMemoryInit */
310 fsp_raminit = (void *)(hdr->image_base + hdr->memory_init_entry_offset);
Lee Leahyac3b0a62016-07-27 07:40:25 -0700311 fsp_debug_before_memory_init(fsp_raminit, upd, &fspm_upd);
Andrey Petrov465fc132016-02-25 14:16:33 -0800312
Alexandru Gagniucc4ea8f72016-05-23 12:16:58 -0700313 post_code(POST_FSP_MEMORY_INIT);
Andrey Petrov465fc132016-02-25 14:16:33 -0800314 timestamp_add_now(TS_FSP_MEMORY_INIT_START);
Lee Leahyac3b0a62016-07-27 07:40:25 -0700315 status = fsp_raminit(&fspm_upd, fsp_get_hob_list_ptr());
Subrata Banik0755ab92017-07-12 15:31:06 +0530316 post_code(POST_FSP_MEMORY_EXIT);
Andrey Petrov465fc132016-02-25 14:16:33 -0800317 timestamp_add_now(TS_FSP_MEMORY_INIT_END);
318
Lee Leahy9671faa2016-07-24 18:18:52 -0700319 /* Handle any errors returned by FspMemoryInit */
Aaron Durbinf41f2aa2016-07-18 12:03:58 -0500320 fsp_handle_reset(status);
Lee Leahy9671faa2016-07-24 18:18:52 -0700321 if (status != FSP_SUCCESS) {
Lee Leahyb20d4ba2016-07-31 16:49:28 -0700322 printk(BIOS_CRIT, "FspMemoryInit returned 0x%08x\n", status);
Keith Short24302632019-05-16 14:08:31 -0600323 die_with_post_code(POST_RAM_FAILURE,
324 "FspMemoryInit returned an error!\n");
Lee Leahy9671faa2016-07-24 18:18:52 -0700325 }
Aaron Durbinf41f2aa2016-07-18 12:03:58 -0500326
Aaron Durbina3cecb22017-04-25 21:58:10 -0500327 do_fsp_post_memory_init(s3wake, fsp_version);
Matthew Garrett78b58a42018-07-28 16:53:16 -0700328
329 /*
330 * fsp_debug_after_memory_init() checks whether the end of the tolum
331 * region is the same as the top of cbmem, so must be called here
332 * after cbmem has been initialised in do_fsp_post_memory_init().
333 */
334 fsp_debug_after_memory_init(status);
Andrey Petrov465fc132016-02-25 14:16:33 -0800335}
336
Aaron Durbind04639b2016-07-17 23:23:59 -0500337/* Load the binary into the memory specified by the info header. */
338static enum cb_err load_fspm_mem(struct fsp_header *hdr,
Aaron Durbin02e504c2016-07-18 11:53:10 -0500339 const struct region_device *rdev,
340 const struct memranges *memmap)
Aaron Durbind04639b2016-07-17 23:23:59 -0500341{
Aaron Durbind04639b2016-07-17 23:23:59 -0500342 uintptr_t fspm_begin;
343 uintptr_t fspm_end;
344
345 if (fsp_validate_component(hdr, rdev) != CB_SUCCESS)
346 return CB_ERR;
347
348 fspm_begin = hdr->image_base;
349 fspm_end = fspm_begin + hdr->image_size;
350
Aaron Durbin02e504c2016-07-18 11:53:10 -0500351 if (check_region_overlap(memmap, "FSPM", fspm_begin, fspm_end) !=
352 CB_SUCCESS)
Aaron Durbind04639b2016-07-17 23:23:59 -0500353 return CB_ERR;
Aaron Durbind04639b2016-07-17 23:23:59 -0500354
355 /* Load binary into memory at provided address. */
356 if (rdev_readat(rdev, (void *)fspm_begin, 0, fspm_end - fspm_begin) < 0)
357 return CB_ERR;
358
359 return CB_SUCCESS;
360}
361
362/* Handle the case when FSPM is running XIP. */
363static enum cb_err load_fspm_xip(struct fsp_header *hdr,
364 const struct region_device *rdev)
365{
366 void *base;
367
368 if (fsp_validate_component(hdr, rdev) != CB_SUCCESS)
369 return CB_ERR;
370
371 base = rdev_mmap_full(rdev);
372 if ((uintptr_t)base != hdr->image_base) {
Lee Leahyb20d4ba2016-07-31 16:49:28 -0700373 printk(BIOS_CRIT, "FSPM XIP base does not match: %p vs %p\n",
Aaron Durbind04639b2016-07-17 23:23:59 -0500374 (void *)(uintptr_t)hdr->image_base, base);
375 return CB_ERR;
376 }
377
378 /*
379 * Since the component is XIP it's already in the address space. Thus,
380 * there's no need to rdev_munmap().
381 */
382 return CB_SUCCESS;
383}
384
Lee Leahy9671faa2016-07-24 18:18:52 -0700385void fsp_memory_init(bool s3wake)
Andrey Petrov465fc132016-02-25 14:16:33 -0800386{
387 struct fsp_header hdr;
Aaron Durbind04639b2016-07-17 23:23:59 -0500388 enum cb_err status;
389 struct cbfsf file_desc;
390 struct region_device file_data;
391 const char *name = CONFIG_FSP_M_CBFS;
Aaron Durbin02e504c2016-07-18 11:53:10 -0500392 struct memranges memmap;
393 struct range_entry freeranges[2];
Andrey Petrov465fc132016-02-25 14:16:33 -0800394
Julius Wernercd49cce2019-03-05 16:53:33 -0800395 if (CONFIG(ELOG_BOOT_COUNT) && !s3wake)
Furquan Shaikh5aea5882016-07-30 18:10:05 -0700396 boot_count_increment();
397
Aaron Durbind04639b2016-07-17 23:23:59 -0500398 if (cbfs_boot_locate(&file_desc, name, NULL)) {
Lee Leahyb20d4ba2016-07-31 16:49:28 -0700399 printk(BIOS_CRIT, "Could not locate %s in CBFS\n", name);
Lee Leahy9671faa2016-07-24 18:18:52 -0700400 die("FSPM not available!\n");
Aaron Durbind04639b2016-07-17 23:23:59 -0500401 }
402
403 cbfs_file_data(&file_data, &file_desc);
404
Aaron Durbin02e504c2016-07-18 11:53:10 -0500405 /* Build up memory map of romstage address space including CAR. */
406 memranges_init_empty(&memmap, &freeranges[0], ARRAY_SIZE(freeranges));
407 memranges_insert(&memmap, (uintptr_t)_car_region_start,
Kyösti Mälkki1095bfa2019-08-22 12:56:22 +0300408 _car_unallocated_start - _car_region_start, 0);
Julius Werner7e0dea62019-02-20 18:39:22 -0800409 memranges_insert(&memmap, (uintptr_t)_program, REGION_SIZE(program), 0);
Aaron Durbin02e504c2016-07-18 11:53:10 -0500410
Julius Wernercd49cce2019-03-05 16:53:33 -0800411 if (!CONFIG(FSP_M_XIP))
Aaron Durbin02e504c2016-07-18 11:53:10 -0500412 status = load_fspm_mem(&hdr, &file_data, &memmap);
Aaron Durbind04639b2016-07-17 23:23:59 -0500413 else
414 status = load_fspm_xip(&hdr, &file_data);
415
Lee Leahye686ee82017-03-10 08:45:30 -0800416 if (status != CB_SUCCESS)
Lee Leahy9671faa2016-07-24 18:18:52 -0700417 die("Loading FSPM failed!\n");
Aaron Durbind04639b2016-07-17 23:23:59 -0500418
419 /* Signal that FSP component has been loaded. */
420 prog_segment_loaded(hdr.image_base, hdr.image_size, SEG_FINAL);
Andrey Petrov465fc132016-02-25 14:16:33 -0800421
Kyösti Mälkki216db612019-09-11 09:57:14 +0300422 timestamp_add_now(TS_BEFORE_INITRAM);
423
Lee Leahy9671faa2016-07-24 18:18:52 -0700424 do_fsp_memory_init(&hdr, s3wake, &memmap);
Kyösti Mälkki0889e932019-08-18 07:40:43 +0300425
426 timestamp_add_now(TS_AFTER_INITRAM);
Andrey Petrov465fc132016-02-25 14:16:33 -0800427}