blob: d6677b4732a4653b73654b631eac90e3e7db78c0 [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
Arthur Heymansfdf6d122022-05-17 13:07:30 +02003#include <arch/null_breakpoint.h>
Aaron Durbinb4302502016-07-17 17:04:37 -05004#include <arch/symbols.h>
Aaron Durbin31be2c92016-12-03 22:08:20 -06005#include <assert.h>
Aaron Durbind04639b2016-07-17 23:23:59 -05006#include <cbfs.h>
Aaron Durbin27928682016-07-15 22:32:28 -05007#include <cbmem.h>
Patrick Rudolphf677d172018-10-01 19:17:11 +02008#include <cf9_reset.h>
Andrey Petrov465fc132016-02-25 14:16:33 -08009#include <console/console.h>
Furquan Shaikh5aea5882016-07-30 18:10:05 -070010#include <elog.h>
Andrey Petrov465fc132016-02-25 14:16:33 -080011#include <fsp/api.h>
12#include <fsp/util.h>
13#include <memrange.h>
Arthur Heymansfdf6d122022-05-17 13:07:30 +020014#include <mode_switch.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>
Arthur Heymansfdf6d122022-05-17 13:07:30 +020018#include <security/tpm/tspi.h>
19#include <security/vboot/antirollback.h>
20#include <security/vboot/vboot_common.h>
Andrey Petrov465fc132016-02-25 14:16:33 -080021#include <string.h>
Aaron Durbind04639b2016-07-17 23:23:59 -050022#include <symbols.h>
Andrey Petrov465fc132016-02-25 14:16:33 -080023#include <timestamp.h>
Elyes HAOUASbd1683d2019-05-15 21:05:37 +020024#include <types.h>
Arthur Heymansfdf6d122022-05-17 13:07:30 +020025#include <vb2_api.h>
Andrey Petrov465fc132016-02-25 14:16:33 -080026
Subrata Banik30a01142023-03-22 00:35:42 +053027#if CONFIG(SOC_INTEL_COMMON_BASECODE_RAMTOP)
28#include <intelbasecode/ramtop.h>
Subrata Banikdbfbfaf2023-02-28 07:01:26 +000029#endif
30
Kyösti Mälkkic9871502019-09-03 07:03:39 +030031static uint8_t temp_ram[CONFIG_FSP_TEMP_RAM_SIZE] __aligned(sizeof(uint64_t));
32
Subrata Banikf31ab7a2023-06-19 12:07:29 +000033/*
34 * Helper function to store the MRC cache version into CBMEM
35 *
36 * ramstage uses either the MRC version or FSP-M version (depending on the config)
37 * when updating the MRC cache
38 */
39static void do_cbmem_version_entry(uint32_t cbmem_id, uint32_t version)
40{
41 uint32_t *cbmem_version_entry = cbmem_add(cbmem_id, sizeof(version));
42 if (!cbmem_version_entry) {
43 printk(BIOS_ERR, "Failed to add %s version to cbmem.\n",
44 CONFIG(MRC_CACHE_USING_MRC_VERSION) ? "MRC" : "FSP-M");
45 return;
46 }
47 *cbmem_version_entry = version;
48}
49
Subrata Banik79274e012023-06-19 11:32:19 +000050static void do_fsp_post_memory_init(bool s3wake, uint32_t version)
Aaron Durbinf0ec8242016-07-18 11:24:36 -050051{
52 struct range_entry fsp_mem;
Subrata Banikf31ab7a2023-06-19 12:07:29 +000053 uint32_t cbmem_id = CONFIG(MRC_CACHE_USING_MRC_VERSION) ? CBMEM_ID_MRC_VERSION :
54 CBMEM_ID_FSPM_VERSION;
Aaron Durbinb4302502016-07-17 17:04:37 -050055
Michael Niewöhnerbc1dbb32019-10-24 22:58:25 +020056 fsp_find_reserved_memory(&fsp_mem);
Aaron Durbinb4302502016-07-17 17:04:37 -050057
58 /* initialize cbmem by adding FSP reserved memory first thing */
59 if (!s3wake) {
60 cbmem_initialize_empty_id_size(CBMEM_ID_FSP_RESERVED_MEMORY,
61 range_entry_size(&fsp_mem));
62 } else if (cbmem_initialize_id_size(CBMEM_ID_FSP_RESERVED_MEMORY,
63 range_entry_size(&fsp_mem))) {
Julius Wernercd49cce2019-03-05 16:53:33 -080064 if (CONFIG(HAVE_ACPI_RESUME)) {
Julius Wernere9665952022-01-21 17:06:20 -080065 printk(BIOS_ERR, "Failed to recover CBMEM in S3 resume.\n");
Aaron Durbinb4302502016-07-17 17:04:37 -050066 /* Failed S3 resume, reset to come up cleanly */
Patrick Rudolphf677d172018-10-01 19:17:11 +020067 /* FIXME: A "system" reset is likely enough: */
68 full_reset();
Aaron Durbinb4302502016-07-17 17:04:37 -050069 }
70 }
71
72 /* make sure FSP memory is reserved in cbmem */
73 if (range_entry_base(&fsp_mem) !=
74 (uintptr_t)cbmem_find(CBMEM_ID_FSP_RESERVED_MEMORY))
Lee Leahy9671faa2016-07-24 18:18:52 -070075 die("Failed to accommodate FSP reserved memory request!\n");
Aaron Durbinb4302502016-07-17 17:04:37 -050076
Subrata Banikf31ab7a2023-06-19 12:07:29 +000077 if (CONFIG(CACHE_MRC_SETTINGS) && !s3wake)
78 do_cbmem_version_entry(cbmem_id, version);
Aaron Durbinb4302502016-07-17 17:04:37 -050079
80 /* Create romstage handof information */
Aaron Durbin77e13992016-11-29 17:43:04 -060081 romstage_handoff_init(s3wake);
Aaron Durbinb4302502016-07-17 17:04:37 -050082}
83
Subrata Banik79274e012023-06-19 11:32:19 +000084static void fsp_fill_mrc_cache(FSPM_ARCH_UPD *arch_upd, uint32_t version)
Aaron Durbinb4302502016-07-17 17:04:37 -050085{
Aaron Durbin31be2c92016-12-03 22:08:20 -060086 void *data;
Shelley Chenad9cd682020-07-23 16:10:52 -070087 size_t mrc_size;
Aaron Durbinb4302502016-07-17 17:04:37 -050088
Patrick Rudolph31218a42020-11-30 15:50:06 +010089 arch_upd->NvsBufferPtr = 0;
Aaron Durbinf0ec8242016-07-18 11:24:36 -050090
Julius Wernercd49cce2019-03-05 16:53:33 -080091 if (!CONFIG(CACHE_MRC_SETTINGS))
Aaron Durbinf0ec8242016-07-18 11:24:36 -050092 return;
93
Aaron Durbin31be2c92016-12-03 22:08:20 -060094 /* Assume boot device is memory mapped. */
Julius Wernercd49cce2019-03-05 16:53:33 -080095 assert(CONFIG(BOOT_DEVICE_MEMORY_MAPPED));
Aaron Durbin31be2c92016-12-03 22:08:20 -060096
Subrata Banik79274e012023-06-19 11:32:19 +000097 data = mrc_cache_current_mmap_leak(MRC_TRAINING_DATA, version,
Shelley Chenad9cd682020-07-23 16:10:52 -070098 &mrc_size);
Aaron Durbin31be2c92016-12-03 22:08:20 -060099 if (data == NULL)
100 return;
101
Aaron Durbinf0ec8242016-07-18 11:24:36 -0500102 /* MRC cache found */
Patrick Rudolph31218a42020-11-30 15:50:06 +0100103 arch_upd->NvsBufferPtr = (uintptr_t)data;
Aamir Bohra69cd62c2018-01-08 11:01:34 +0530104
Subrata Banik05937082023-03-06 08:18:24 +0000105 printk(BIOS_SPEW, "MRC cache found, size %zu bytes\n", mrc_size);
Aaron Durbinf0ec8242016-07-18 11:24:36 -0500106}
107
Aaron Durbin02e504c2016-07-18 11:53:10 -0500108static enum cb_err check_region_overlap(const struct memranges *ranges,
109 const char *description,
110 uintptr_t begin, uintptr_t end)
Aaron Durbinf0ec8242016-07-18 11:24:36 -0500111{
Aaron Durbin02e504c2016-07-18 11:53:10 -0500112 const struct range_entry *r;
113
114 memranges_each_entry(r, ranges) {
115 if (end <= range_entry_base(r))
116 continue;
117 if (begin >= range_entry_end(r))
118 continue;
Lee Leahyb20d4ba2016-07-31 16:49:28 -0700119 printk(BIOS_CRIT, "'%s' overlaps currently running program: "
Aaron Durbin02e504c2016-07-18 11:53:10 -0500120 "[%p, %p)\n", description, (void *)begin, (void *)end);
121 return CB_ERR;
122 }
123
124 return CB_SUCCESS;
125}
Kyösti Mälkkic9871502019-09-03 07:03:39 +0300126
Aamir Bohra6d569e0c2018-08-27 13:36:15 +0530127static enum cb_err setup_fsp_stack_frame(FSPM_ARCH_UPD *arch_upd,
128 const struct memranges *memmap)
Aaron Durbin02e504c2016-07-18 11:53:10 -0500129{
130 uintptr_t stack_begin;
131 uintptr_t stack_end;
132
Aaron Durbinb4302502016-07-17 17:04:37 -0500133 /*
Aamir Bohra6d569e0c2018-08-27 13:36:15 +0530134 * FSPM_UPD passed here is populated with default values
135 * provided by the blob itself. We let FSPM use top of CAR
136 * region of the size it requests.
Aaron Durbinb4302502016-07-17 17:04:37 -0500137 */
Aaron Durbin02e504c2016-07-18 11:53:10 -0500138 stack_end = (uintptr_t)_car_region_end;
139 stack_begin = stack_end - arch_upd->StackSize;
Aaron Durbin02e504c2016-07-18 11:53:10 -0500140 if (check_region_overlap(memmap, "FSPM stack", stack_begin,
141 stack_end) != CB_SUCCESS)
142 return CB_ERR;
143
Patrick Rudolph31218a42020-11-30 15:50:06 +0100144 arch_upd->StackBase = stack_begin;
Aamir Bohra6d569e0c2018-08-27 13:36:15 +0530145 return CB_SUCCESS;
146}
147
148static enum cb_err fsp_fill_common_arch_params(FSPM_ARCH_UPD *arch_upd,
Subrata Banik79274e012023-06-19 11:32:19 +0000149 bool s3wake, uint32_t version,
Aamir Bohra6d569e0c2018-08-27 13:36:15 +0530150 const struct memranges *memmap)
151{
Kyösti Mälkkic9871502019-09-03 07:03:39 +0300152 /*
153 * FSP 2.1 version would use same stack as coreboot instead of
154 * setting up separate stack frame. FSP 2.1 would not relocate stack
155 * top and does not reinitialize stack pointer. The parameters passed
156 * as StackBase and StackSize are actually for temporary RAM and HOBs
157 * and are not related to FSP stack at all.
Felix Held414d7e42020-08-11 22:54:06 +0200158 * Non-CAR FSP 2.0 platforms pass a DRAM location for the FSP stack.
Kyösti Mälkkic9871502019-09-03 07:03:39 +0300159 */
Felix Held414d7e42020-08-11 22:54:06 +0200160 if (CONFIG(FSP_USES_CB_STACK) || !ENV_CACHE_AS_RAM) {
Patrick Rudolph31218a42020-11-30 15:50:06 +0100161 arch_upd->StackBase = (uintptr_t)temp_ram;
Kyösti Mälkkic9871502019-09-03 07:03:39 +0300162 arch_upd->StackSize = sizeof(temp_ram);
163 } else if (setup_fsp_stack_frame(arch_upd, memmap)) {
Aamir Bohra6d569e0c2018-08-27 13:36:15 +0530164 return CB_ERR;
Kyösti Mälkkic9871502019-09-03 07:03:39 +0300165 }
Aaron Durbinb4302502016-07-17 17:04:37 -0500166
Subrata Banik79274e012023-06-19 11:32:19 +0000167 fsp_fill_mrc_cache(arch_upd, version);
Aaron Durbinb4302502016-07-17 17:04:37 -0500168
Aamir Bohra69cd62c2018-01-08 11:01:34 +0530169 /* Configure bootmode */
170 if (s3wake) {
Aamir Bohra69cd62c2018-01-08 11:01:34 +0530171 arch_upd->BootMode = FSP_BOOT_ON_S3_RESUME;
172 } else {
173 if (arch_upd->NvsBufferPtr)
174 arch_upd->BootMode =
175 FSP_BOOT_ASSUMING_NO_CONFIGURATION_CHANGES;
176 else
177 arch_upd->BootMode = FSP_BOOT_WITH_FULL_CONFIGURATION;
178 }
Aaron Durbin02e504c2016-07-18 11:53:10 -0500179
Marshall Dawson22d66ef2019-08-30 14:52:37 -0600180 printk(BIOS_SPEW, "bootmode is set to: %d\n", arch_upd->BootMode);
Aamir Bohra276c06a2017-12-26 17:54:45 +0530181
Aaron Durbin02e504c2016-07-18 11:53:10 -0500182 return CB_SUCCESS;
Aaron Durbinb4302502016-07-17 17:04:37 -0500183}
184
Aaron Durbin64031672018-04-21 14:45:32 -0600185__weak
Aaron Durbina3cecb22017-04-25 21:58:10 -0500186uint8_t fsp_memory_mainboard_version(void)
187{
188 return 0;
189}
190
Aaron Durbin64031672018-04-21 14:45:32 -0600191__weak
Aaron Durbina3cecb22017-04-25 21:58:10 -0500192uint8_t fsp_memory_soc_version(void)
193{
194 return 0;
195}
196
197/*
198 * Allow SoC and/or mainboard to bump the revision of the FSP setting
199 * number. The FSP spec uses the low 8 bits as the build number. Take over
200 * bits 3:0 for the SoC setting and bits 7:4 for the mainboard. That way
201 * a tweak in the settings will bump the version used to track the cached
202 * setting which triggers retraining when the FSP version hasn't changed, but
203 * the SoC or mainboard settings have.
204 */
205static uint32_t fsp_memory_settings_version(const struct fsp_header *hdr)
206{
207 /* Use the full FSP version by default. */
Julian Schroeder8a576f62021-11-02 16:32:28 -0500208 uint32_t ver = hdr->image_revision;
Aaron Durbina3cecb22017-04-25 21:58:10 -0500209
Julius Wernercd49cce2019-03-05 16:53:33 -0800210 if (!CONFIG(FSP_PLATFORM_MEMORY_SETTINGS_VERSIONS))
Aaron Durbina3cecb22017-04-25 21:58:10 -0500211 return ver;
212
213 ver &= ~0xff;
214 ver |= (0xf & fsp_memory_mainboard_version()) << 4;
215 ver |= (0xf & fsp_memory_soc_version()) << 0;
216
217 return ver;
218}
219
Aaron Durbinecbfa992020-05-15 17:01:58 -0600220struct fspm_context {
221 struct fsp_header header;
222 struct memranges memmap;
223};
224
Subrata Banik79274e012023-06-19 11:32:19 +0000225/*
226 * Helper function to read MRC version
227 *
228 * There are multiple ways to read the MRC version using
229 * Intel FSP. Currently the only supported method to get the
230 * MRC version is by reading the FSP_PRODUCDER_DATA_TABLES
231 * from the FSP-M binary (by parsing the FSP header).
232 */
233static uint32_t fsp_mrc_version(void)
234{
235 uint32_t ver = 0;
236#if CONFIG(MRC_CACHE_USING_MRC_VERSION)
237 size_t fspm_blob_size;
238 void *fspm_blob_file = cbfs_map(CONFIG_FSP_M_CBFS, &fspm_blob_size);
239 if (!fspm_blob_file)
240 return 0;
241
242 FSP_PRODUCER_DATA_TABLES *ft = fspm_blob_file + FSP_HDR_OFFSET;
243 FSP_PRODUCER_DATA_TYPE2 *table2 = &ft->FspProduceDataType2;
244 size_t mrc_version_size = sizeof(table2->MrcVersion);
245 for (size_t i = 0; i < mrc_version_size; i++) {
246 ver |= (table2->MrcVersion[i] << ((mrc_version_size - 1) - i) * 8);
247 }
248 cbfs_unmap(fspm_blob_file);
249#endif
250 return ver;
251}
252
Aaron Durbinecbfa992020-05-15 17:01:58 -0600253static void do_fsp_memory_init(const struct fspm_context *context, bool s3wake)
Andrey Petrov465fc132016-02-25 14:16:33 -0800254{
Brandon Breitensteinc31ba0e2016-07-27 17:34:45 -0700255 uint32_t status;
Andrey Petrov465fc132016-02-25 14:16:33 -0800256 fsp_memory_init_fn fsp_raminit;
Brandon Breitensteinc31ba0e2016-07-27 17:34:45 -0700257 FSPM_UPD fspm_upd, *upd;
258 FSPM_ARCH_UPD *arch_upd;
Subrata Banik79274e012023-06-19 11:32:19 +0000259 uint32_t version;
Aaron Durbinecbfa992020-05-15 17:01:58 -0600260 const struct fsp_header *hdr = &context->header;
261 const struct memranges *memmap = &context->memmap;
Andrey Petrov465fc132016-02-25 14:16:33 -0800262
lilacious40cb3fe2023-06-21 23:24:14 +0200263 post_code(POSTCODE_MEM_PREINIT_PREP_START);
Andrey Petrov465fc132016-02-25 14:16:33 -0800264
Subrata Banik79274e012023-06-19 11:32:19 +0000265 if (CONFIG(MRC_CACHE_USING_MRC_VERSION))
266 version = fsp_mrc_version();
267 else
268 version = fsp_memory_settings_version(hdr);
Aaron Durbina3cecb22017-04-25 21:58:10 -0500269
Patrick Rudolph31218a42020-11-30 15:50:06 +0100270 upd = (FSPM_UPD *)(uintptr_t)(hdr->cfg_region_offset + hdr->image_base);
Andrey Petrov465fc132016-02-25 14:16:33 -0800271
Felix Helda0955952021-02-02 21:30:25 +0100272 /*
273 * Verify UPD region size. We don't have malloc before ramstage, so we
274 * use a static buffer for the FSP-M UPDs which is sizeof(FSPM_UPD)
275 * bytes long, since that is the value known at compile time. If
276 * hdr->cfg_region_size is bigger than that, not all UPD defaults will
277 * be copied, so it'll contain random data at the end, so we just call
278 * die() in that case. If hdr->cfg_region_size is smaller than that,
279 * there's a mismatch between the FSP and the header, but since it will
280 * copy the full UPD defaults to the buffer, we try to continue and
281 * hope that there was no incompatible change in the UPDs.
282 */
283 if (hdr->cfg_region_size > sizeof(FSPM_UPD))
284 die("FSP-M UPD size is larger than FSPM_UPD struct size.\n");
285 if (hdr->cfg_region_size < sizeof(FSPM_UPD))
286 printk(BIOS_ERR, "FSP-M UPD size is smaller than FSPM_UPD struct size. "
287 "Check if the FSP binary matches the FSP headers.\n");
288
Felix Held88995982021-01-28 22:43:52 +0100289 fsp_verify_upd_header_signature(upd->FspUpdHeader.Signature, FSPM_UPD_SIGNATURE);
Andrey Petrov465fc132016-02-25 14:16:33 -0800290
291 /* Copy the default values from the UPD area */
292 memcpy(&fspm_upd, upd, sizeof(fspm_upd));
293
Aaron Durbin02e504c2016-07-18 11:53:10 -0500294 arch_upd = &fspm_upd.FspmArchUpd;
295
Aaron Durbin27928682016-07-15 22:32:28 -0500296 /* Reserve enough memory under TOLUD to save CBMEM header */
Aaron Durbin02e504c2016-07-18 11:53:10 -0500297 arch_upd->BootLoaderTolumSize = cbmem_overhead_size();
Aaron Durbin27928682016-07-15 22:32:28 -0500298
Aaron Durbinb4302502016-07-17 17:04:37 -0500299 /* Fill common settings on behalf of chipset. */
Subrata Banik79274e012023-06-19 11:32:19 +0000300 if (fsp_fill_common_arch_params(arch_upd, s3wake, version,
Aaron Durbin02e504c2016-07-18 11:53:10 -0500301 memmap) != CB_SUCCESS)
lilacious40cb3fe2023-06-21 23:24:14 +0200302 die_with_post_code(POSTCODE_INVALID_VENDOR_BINARY,
Keith Shortbb41aba2019-05-16 14:07:43 -0600303 "FSPM_ARCH_UPD not found!\n");
Aaron Durbinb4302502016-07-17 17:04:37 -0500304
Subrata Banik30a01142023-03-22 00:35:42 +0530305 /* Early caching of RAMTOP region if valid mrc cache data is found */
306#if (CONFIG(SOC_INTEL_COMMON_BASECODE_RAMTOP))
Subrata Banikdbfbfaf2023-02-28 07:01:26 +0000307 if (arch_upd->NvsBufferPtr)
Subrata Banik30a01142023-03-22 00:35:42 +0530308 early_ramtop_enable_cache_range();
Subrata Banikdbfbfaf2023-02-28 07:01:26 +0000309#endif
310
Andrey Petrov465fc132016-02-25 14:16:33 -0800311 /* Give SoC and mainboard a chance to update the UPD */
Subrata Banik79274e012023-06-19 11:32:19 +0000312 platform_fsp_memory_init_params_cb(&fspm_upd, version);
Andrey Petrov465fc132016-02-25 14:16:33 -0800313
Furquan Shaikhdbce8ba2020-06-05 19:17:00 -0700314 /*
315 * For S3 resume case, if valid mrc cache data is not found or
316 * RECOVERY_MRC_CACHE hash verification fails, the S3 data
317 * pointer would be null and S3 resume fails with fsp-m
318 * returning error. Invoking a reset here saves time.
319 */
320 if (s3wake && !arch_upd->NvsBufferPtr)
321 /* FIXME: A "system" reset is likely enough: */
322 full_reset();
323
Julius Wernercd49cce2019-03-05 16:53:33 -0800324 if (CONFIG(MMA))
Pratik Prajapatiffc934d2016-11-18 14:36:34 -0800325 setup_mma(&fspm_upd.FspmConfig);
326
lilacious40cb3fe2023-06-21 23:24:14 +0200327 post_code(POSTCODE_MEM_PREINIT_PREP_END);
Furquan Shaikh585210a2018-10-16 11:54:37 -0700328
Andrey Petrov465fc132016-02-25 14:16:33 -0800329 /* Call FspMemoryInit */
Julian Schroeder8a576f62021-11-02 16:32:28 -0500330 fsp_raminit = (void *)(uintptr_t)(hdr->image_base + hdr->fsp_memory_init_entry_offset);
Lee Leahyac3b0a62016-07-27 07:40:25 -0700331 fsp_debug_before_memory_init(fsp_raminit, upd, &fspm_upd);
Andrey Petrov465fc132016-02-25 14:16:33 -0800332
Arthur Heymansfdf6d122022-05-17 13:07:30 +0200333 /* FSP disables the interrupt handler so remove debug exceptions temporarily */
334 null_breakpoint_disable();
lilacious40cb3fe2023-06-21 23:24:14 +0200335 post_code(POSTCODE_FSP_MEMORY_INIT);
Andrey Petrov465fc132016-02-25 14:16:33 -0800336 timestamp_add_now(TS_FSP_MEMORY_INIT_START);
Patrick Rudolph31218a42020-11-30 15:50:06 +0100337 if (ENV_X86_64 && CONFIG(PLATFORM_USES_FSP2_X86_32))
Patrick Rudolph40beb362020-12-01 10:08:38 +0100338 status = protected_mode_call_2arg(fsp_raminit,
339 (uintptr_t)&fspm_upd,
340 (uintptr_t)fsp_get_hob_list_ptr());
341 else
342 status = fsp_raminit(&fspm_upd, fsp_get_hob_list_ptr());
Arthur Heymansfdf6d122022-05-17 13:07:30 +0200343 null_breakpoint_init();
Patrick Rudolph40beb362020-12-01 10:08:38 +0100344
lilacious40cb3fe2023-06-21 23:24:14 +0200345 post_code(POSTCODE_FSP_MEMORY_EXIT);
Andrey Petrov465fc132016-02-25 14:16:33 -0800346 timestamp_add_now(TS_FSP_MEMORY_INIT_END);
347
Lee Leahy9671faa2016-07-24 18:18:52 -0700348 /* Handle any errors returned by FspMemoryInit */
Aaron Durbinf41f2aa2016-07-18 12:03:58 -0500349 fsp_handle_reset(status);
Lee Leahy9671faa2016-07-24 18:18:52 -0700350 if (status != FSP_SUCCESS) {
lilacious40cb3fe2023-06-21 23:24:14 +0200351 die_with_post_code(POSTCODE_RAM_FAILURE,
Angel Pons2b1f8d42022-01-01 17:20:00 +0100352 "FspMemoryInit returned with error 0x%08x!\n", status);
Lee Leahy9671faa2016-07-24 18:18:52 -0700353 }
Aaron Durbinf41f2aa2016-07-18 12:03:58 -0500354
Subrata Banik79274e012023-06-19 11:32:19 +0000355 do_fsp_post_memory_init(s3wake, version);
Matthew Garrett78b58a42018-07-28 16:53:16 -0700356
357 /*
358 * fsp_debug_after_memory_init() checks whether the end of the tolum
359 * region is the same as the top of cbmem, so must be called here
360 * after cbmem has been initialised in do_fsp_post_memory_init().
361 */
362 fsp_debug_after_memory_init(status);
Andrey Petrov465fc132016-02-25 14:16:33 -0800363}
364
Julius Werner8205ce62021-03-10 17:25:01 -0800365static void *fspm_allocator(void *arg, size_t size, const union cbfs_mdata *unused)
Aaron Durbind04639b2016-07-17 23:23:59 -0500366{
Julius Werner8205ce62021-03-10 17:25:01 -0800367 const struct fsp_load_descriptor *fspld = arg;
Aaron Durbinecbfa992020-05-15 17:01:58 -0600368 struct fspm_context *context = fspld->arg;
Aaron Durbinecbfa992020-05-15 17:01:58 -0600369 struct memranges *memmap = &context->memmap;
Aaron Durbind04639b2016-07-17 23:23:59 -0500370
Aaron Durbinecbfa992020-05-15 17:01:58 -0600371 /* Non XIP FSP-M uses FSP-M address */
Julius Werner8205ce62021-03-10 17:25:01 -0800372 uintptr_t fspm_begin = (uintptr_t)CONFIG_FSP_M_ADDR;
373 uintptr_t fspm_end = fspm_begin + size;
Aaron Durbinecbfa992020-05-15 17:01:58 -0600374
375 if (check_region_overlap(memmap, "FSPM", fspm_begin, fspm_end) != CB_SUCCESS)
Julius Werner8205ce62021-03-10 17:25:01 -0800376 return NULL;
Aaron Durbinecbfa992020-05-15 17:01:58 -0600377
Julius Werner8205ce62021-03-10 17:25:01 -0800378 return (void *)fspm_begin;
Aaron Durbind04639b2016-07-17 23:23:59 -0500379}
380
Raul E Rangel15928462021-11-05 10:29:24 -0600381void preload_fspm(void)
382{
383 if (!CONFIG(CBFS_PRELOAD))
384 return;
385
386 printk(BIOS_DEBUG, "Preloading %s\n", CONFIG_FSP_M_CBFS);
387 cbfs_preload(CONFIG_FSP_M_CBFS);
388}
389
Lee Leahy9671faa2016-07-24 18:18:52 -0700390void fsp_memory_init(bool s3wake)
Andrey Petrov465fc132016-02-25 14:16:33 -0800391{
Marshall Dawsone3aa4242019-10-16 21:53:21 -0600392 struct range_entry prog_ranges[2];
Aaron Durbinecbfa992020-05-15 17:01:58 -0600393 struct fspm_context context;
394 struct fsp_load_descriptor fspld = {
395 .fsp_prog = PROG_INIT(PROG_REFCODE, CONFIG_FSP_M_CBFS),
Aaron Durbinecbfa992020-05-15 17:01:58 -0600396 .arg = &context,
397 };
398 struct fsp_header *hdr = &context.header;
399 struct memranges *memmap = &context.memmap;
Andrey Petrov465fc132016-02-25 14:16:33 -0800400
Julius Werner8205ce62021-03-10 17:25:01 -0800401 /* For FSP-M XIP we leave alloc NULL to get a direct mapping to flash. */
402 if (!CONFIG(FSP_M_XIP))
403 fspld.alloc = fspm_allocator;
404
Kyösti Mälkki7f50afb2019-09-11 17:12:26 +0300405 elog_boot_notify(s3wake);
Furquan Shaikh5aea5882016-07-30 18:10:05 -0700406
Aaron Durbin02e504c2016-07-18 11:53:10 -0500407 /* Build up memory map of romstage address space including CAR. */
Aaron Durbinecbfa992020-05-15 17:01:58 -0600408 memranges_init_empty(memmap, &prog_ranges[0], ARRAY_SIZE(prog_ranges));
Marshall Dawsone3aa4242019-10-16 21:53:21 -0600409 if (ENV_CACHE_AS_RAM)
Aaron Durbinecbfa992020-05-15 17:01:58 -0600410 memranges_insert(memmap, (uintptr_t)_car_region_start,
Marshall Dawsone3aa4242019-10-16 21:53:21 -0600411 _car_unallocated_start - _car_region_start, 0);
Aaron Durbinecbfa992020-05-15 17:01:58 -0600412 memranges_insert(memmap, (uintptr_t)_program, REGION_SIZE(program), 0);
Aaron Durbin02e504c2016-07-18 11:53:10 -0500413
Martin Roth146508d2021-04-30 16:45:08 -0600414 timestamp_add_now(TS_FSP_MEMORY_INIT_LOAD);
Aaron Durbinecbfa992020-05-15 17:01:58 -0600415 if (fsp_load_component(&fspld, hdr) != CB_SUCCESS)
416 die("FSPM not available or failed to load!\n");
Andrey Petrov465fc132016-02-25 14:16:33 -0800417
Julius Werner8205ce62021-03-10 17:25:01 -0800418 if (CONFIG(FSP_M_XIP) && (uintptr_t)prog_start(&fspld.fsp_prog) != hdr->image_base)
419 die("FSPM XIP base does not match: %p vs %p\n",
420 (void *)(uintptr_t)hdr->image_base, prog_start(&fspld.fsp_prog));
421
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100422 timestamp_add_now(TS_INITRAM_START);
Kyösti Mälkki216db612019-09-11 09:57:14 +0300423
Aaron Durbinecbfa992020-05-15 17:01:58 -0600424 do_fsp_memory_init(&context, s3wake);
Kyösti Mälkki0889e932019-08-18 07:40:43 +0300425
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100426 timestamp_add_now(TS_INITRAM_END);
Andrey Petrov465fc132016-02-25 14:16:33 -0800427}