blob: fa24a7e018c69d10f3ce7c2a3dfc56078fbbc1b2 [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 Banik79274e012023-06-19 11:32:19 +000033static void do_fsp_post_memory_init(bool s3wake, uint32_t version)
Aaron Durbinf0ec8242016-07-18 11:24:36 -050034{
35 struct range_entry fsp_mem;
Reka Norman7b5a9312022-09-13 14:06:52 +100036 uint32_t *fsp_version_cbmem;
Aaron Durbinb4302502016-07-17 17:04:37 -050037
Michael Niewöhnerbc1dbb32019-10-24 22:58:25 +020038 fsp_find_reserved_memory(&fsp_mem);
Aaron Durbinb4302502016-07-17 17:04:37 -050039
40 /* initialize cbmem by adding FSP reserved memory first thing */
41 if (!s3wake) {
42 cbmem_initialize_empty_id_size(CBMEM_ID_FSP_RESERVED_MEMORY,
43 range_entry_size(&fsp_mem));
44 } else if (cbmem_initialize_id_size(CBMEM_ID_FSP_RESERVED_MEMORY,
45 range_entry_size(&fsp_mem))) {
Julius Wernercd49cce2019-03-05 16:53:33 -080046 if (CONFIG(HAVE_ACPI_RESUME)) {
Julius Wernere9665952022-01-21 17:06:20 -080047 printk(BIOS_ERR, "Failed to recover CBMEM in S3 resume.\n");
Aaron Durbinb4302502016-07-17 17:04:37 -050048 /* Failed S3 resume, reset to come up cleanly */
Patrick Rudolphf677d172018-10-01 19:17:11 +020049 /* FIXME: A "system" reset is likely enough: */
50 full_reset();
Aaron Durbinb4302502016-07-17 17:04:37 -050051 }
52 }
53
54 /* make sure FSP memory is reserved in cbmem */
55 if (range_entry_base(&fsp_mem) !=
56 (uintptr_t)cbmem_find(CBMEM_ID_FSP_RESERVED_MEMORY))
Lee Leahy9671faa2016-07-24 18:18:52 -070057 die("Failed to accommodate FSP reserved memory request!\n");
Aaron Durbinb4302502016-07-17 17:04:37 -050058
Reka Norman7b5a9312022-09-13 14:06:52 +100059 /* ramstage uses the FSP-M version when updating the MRC cache */
60 if (CONFIG(CACHE_MRC_SETTINGS) && !s3wake) {
61 fsp_version_cbmem = cbmem_add(CBMEM_ID_FSPM_VERSION,
Subrata Banik79274e012023-06-19 11:32:19 +000062 sizeof(version));
Reka Norman7b5a9312022-09-13 14:06:52 +100063 if (!fsp_version_cbmem)
64 printk(BIOS_ERR, "Failed to add FSP-M version to cbmem.\n");
Subrata Banik79274e012023-06-19 11:32:19 +000065 *fsp_version_cbmem = version;
Reka Norman7b5a9312022-09-13 14:06:52 +100066 }
Aaron Durbinb4302502016-07-17 17:04:37 -050067
68 /* Create romstage handof information */
Aaron Durbin77e13992016-11-29 17:43:04 -060069 romstage_handoff_init(s3wake);
Aaron Durbinb4302502016-07-17 17:04:37 -050070}
71
Subrata Banik79274e012023-06-19 11:32:19 +000072static void fsp_fill_mrc_cache(FSPM_ARCH_UPD *arch_upd, uint32_t version)
Aaron Durbinb4302502016-07-17 17:04:37 -050073{
Aaron Durbin31be2c92016-12-03 22:08:20 -060074 void *data;
Shelley Chenad9cd682020-07-23 16:10:52 -070075 size_t mrc_size;
Aaron Durbinb4302502016-07-17 17:04:37 -050076
Patrick Rudolph31218a42020-11-30 15:50:06 +010077 arch_upd->NvsBufferPtr = 0;
Aaron Durbinf0ec8242016-07-18 11:24:36 -050078
Julius Wernercd49cce2019-03-05 16:53:33 -080079 if (!CONFIG(CACHE_MRC_SETTINGS))
Aaron Durbinf0ec8242016-07-18 11:24:36 -050080 return;
81
Aaron Durbin31be2c92016-12-03 22:08:20 -060082 /* Assume boot device is memory mapped. */
Julius Wernercd49cce2019-03-05 16:53:33 -080083 assert(CONFIG(BOOT_DEVICE_MEMORY_MAPPED));
Aaron Durbin31be2c92016-12-03 22:08:20 -060084
Subrata Banik79274e012023-06-19 11:32:19 +000085 data = mrc_cache_current_mmap_leak(MRC_TRAINING_DATA, version,
Shelley Chenad9cd682020-07-23 16:10:52 -070086 &mrc_size);
Aaron Durbin31be2c92016-12-03 22:08:20 -060087 if (data == NULL)
88 return;
89
Aaron Durbinf0ec8242016-07-18 11:24:36 -050090 /* MRC cache found */
Patrick Rudolph31218a42020-11-30 15:50:06 +010091 arch_upd->NvsBufferPtr = (uintptr_t)data;
Aamir Bohra69cd62c2018-01-08 11:01:34 +053092
Subrata Banik05937082023-03-06 08:18:24 +000093 printk(BIOS_SPEW, "MRC cache found, size %zu bytes\n", mrc_size);
Aaron Durbinf0ec8242016-07-18 11:24:36 -050094}
95
Aaron Durbin02e504c2016-07-18 11:53:10 -050096static enum cb_err check_region_overlap(const struct memranges *ranges,
97 const char *description,
98 uintptr_t begin, uintptr_t end)
Aaron Durbinf0ec8242016-07-18 11:24:36 -050099{
Aaron Durbin02e504c2016-07-18 11:53:10 -0500100 const struct range_entry *r;
101
102 memranges_each_entry(r, ranges) {
103 if (end <= range_entry_base(r))
104 continue;
105 if (begin >= range_entry_end(r))
106 continue;
Lee Leahyb20d4ba2016-07-31 16:49:28 -0700107 printk(BIOS_CRIT, "'%s' overlaps currently running program: "
Aaron Durbin02e504c2016-07-18 11:53:10 -0500108 "[%p, %p)\n", description, (void *)begin, (void *)end);
109 return CB_ERR;
110 }
111
112 return CB_SUCCESS;
113}
Kyösti Mälkkic9871502019-09-03 07:03:39 +0300114
Aamir Bohra6d569e0c2018-08-27 13:36:15 +0530115static enum cb_err setup_fsp_stack_frame(FSPM_ARCH_UPD *arch_upd,
116 const struct memranges *memmap)
Aaron Durbin02e504c2016-07-18 11:53:10 -0500117{
118 uintptr_t stack_begin;
119 uintptr_t stack_end;
120
Aaron Durbinb4302502016-07-17 17:04:37 -0500121 /*
Aamir Bohra6d569e0c2018-08-27 13:36:15 +0530122 * FSPM_UPD passed here is populated with default values
123 * provided by the blob itself. We let FSPM use top of CAR
124 * region of the size it requests.
Aaron Durbinb4302502016-07-17 17:04:37 -0500125 */
Aaron Durbin02e504c2016-07-18 11:53:10 -0500126 stack_end = (uintptr_t)_car_region_end;
127 stack_begin = stack_end - arch_upd->StackSize;
Aaron Durbin02e504c2016-07-18 11:53:10 -0500128 if (check_region_overlap(memmap, "FSPM stack", stack_begin,
129 stack_end) != CB_SUCCESS)
130 return CB_ERR;
131
Patrick Rudolph31218a42020-11-30 15:50:06 +0100132 arch_upd->StackBase = stack_begin;
Aamir Bohra6d569e0c2018-08-27 13:36:15 +0530133 return CB_SUCCESS;
134}
135
136static enum cb_err fsp_fill_common_arch_params(FSPM_ARCH_UPD *arch_upd,
Subrata Banik79274e012023-06-19 11:32:19 +0000137 bool s3wake, uint32_t version,
Aamir Bohra6d569e0c2018-08-27 13:36:15 +0530138 const struct memranges *memmap)
139{
Kyösti Mälkkic9871502019-09-03 07:03:39 +0300140 /*
141 * FSP 2.1 version would use same stack as coreboot instead of
142 * setting up separate stack frame. FSP 2.1 would not relocate stack
143 * top and does not reinitialize stack pointer. The parameters passed
144 * as StackBase and StackSize are actually for temporary RAM and HOBs
145 * and are not related to FSP stack at all.
Felix Held414d7e42020-08-11 22:54:06 +0200146 * Non-CAR FSP 2.0 platforms pass a DRAM location for the FSP stack.
Kyösti Mälkkic9871502019-09-03 07:03:39 +0300147 */
Felix Held414d7e42020-08-11 22:54:06 +0200148 if (CONFIG(FSP_USES_CB_STACK) || !ENV_CACHE_AS_RAM) {
Patrick Rudolph31218a42020-11-30 15:50:06 +0100149 arch_upd->StackBase = (uintptr_t)temp_ram;
Kyösti Mälkkic9871502019-09-03 07:03:39 +0300150 arch_upd->StackSize = sizeof(temp_ram);
151 } else if (setup_fsp_stack_frame(arch_upd, memmap)) {
Aamir Bohra6d569e0c2018-08-27 13:36:15 +0530152 return CB_ERR;
Kyösti Mälkkic9871502019-09-03 07:03:39 +0300153 }
Aaron Durbinb4302502016-07-17 17:04:37 -0500154
Subrata Banik79274e012023-06-19 11:32:19 +0000155 fsp_fill_mrc_cache(arch_upd, version);
Aaron Durbinb4302502016-07-17 17:04:37 -0500156
Aamir Bohra69cd62c2018-01-08 11:01:34 +0530157 /* Configure bootmode */
158 if (s3wake) {
Aamir Bohra69cd62c2018-01-08 11:01:34 +0530159 arch_upd->BootMode = FSP_BOOT_ON_S3_RESUME;
160 } else {
161 if (arch_upd->NvsBufferPtr)
162 arch_upd->BootMode =
163 FSP_BOOT_ASSUMING_NO_CONFIGURATION_CHANGES;
164 else
165 arch_upd->BootMode = FSP_BOOT_WITH_FULL_CONFIGURATION;
166 }
Aaron Durbin02e504c2016-07-18 11:53:10 -0500167
Marshall Dawson22d66ef2019-08-30 14:52:37 -0600168 printk(BIOS_SPEW, "bootmode is set to: %d\n", arch_upd->BootMode);
Aamir Bohra276c06a2017-12-26 17:54:45 +0530169
Aaron Durbin02e504c2016-07-18 11:53:10 -0500170 return CB_SUCCESS;
Aaron Durbinb4302502016-07-17 17:04:37 -0500171}
172
Aaron Durbin64031672018-04-21 14:45:32 -0600173__weak
Aaron Durbina3cecb22017-04-25 21:58:10 -0500174uint8_t fsp_memory_mainboard_version(void)
175{
176 return 0;
177}
178
Aaron Durbin64031672018-04-21 14:45:32 -0600179__weak
Aaron Durbina3cecb22017-04-25 21:58:10 -0500180uint8_t fsp_memory_soc_version(void)
181{
182 return 0;
183}
184
185/*
186 * Allow SoC and/or mainboard to bump the revision of the FSP setting
187 * number. The FSP spec uses the low 8 bits as the build number. Take over
188 * bits 3:0 for the SoC setting and bits 7:4 for the mainboard. That way
189 * a tweak in the settings will bump the version used to track the cached
190 * setting which triggers retraining when the FSP version hasn't changed, but
191 * the SoC or mainboard settings have.
192 */
193static uint32_t fsp_memory_settings_version(const struct fsp_header *hdr)
194{
195 /* Use the full FSP version by default. */
Julian Schroeder8a576f62021-11-02 16:32:28 -0500196 uint32_t ver = hdr->image_revision;
Aaron Durbina3cecb22017-04-25 21:58:10 -0500197
Julius Wernercd49cce2019-03-05 16:53:33 -0800198 if (!CONFIG(FSP_PLATFORM_MEMORY_SETTINGS_VERSIONS))
Aaron Durbina3cecb22017-04-25 21:58:10 -0500199 return ver;
200
201 ver &= ~0xff;
202 ver |= (0xf & fsp_memory_mainboard_version()) << 4;
203 ver |= (0xf & fsp_memory_soc_version()) << 0;
204
205 return ver;
206}
207
Aaron Durbinecbfa992020-05-15 17:01:58 -0600208struct fspm_context {
209 struct fsp_header header;
210 struct memranges memmap;
211};
212
Subrata Banik79274e012023-06-19 11:32:19 +0000213/*
214 * Helper function to read MRC version
215 *
216 * There are multiple ways to read the MRC version using
217 * Intel FSP. Currently the only supported method to get the
218 * MRC version is by reading the FSP_PRODUCDER_DATA_TABLES
219 * from the FSP-M binary (by parsing the FSP header).
220 */
221static uint32_t fsp_mrc_version(void)
222{
223 uint32_t ver = 0;
224#if CONFIG(MRC_CACHE_USING_MRC_VERSION)
225 size_t fspm_blob_size;
226 void *fspm_blob_file = cbfs_map(CONFIG_FSP_M_CBFS, &fspm_blob_size);
227 if (!fspm_blob_file)
228 return 0;
229
230 FSP_PRODUCER_DATA_TABLES *ft = fspm_blob_file + FSP_HDR_OFFSET;
231 FSP_PRODUCER_DATA_TYPE2 *table2 = &ft->FspProduceDataType2;
232 size_t mrc_version_size = sizeof(table2->MrcVersion);
233 for (size_t i = 0; i < mrc_version_size; i++) {
234 ver |= (table2->MrcVersion[i] << ((mrc_version_size - 1) - i) * 8);
235 }
236 cbfs_unmap(fspm_blob_file);
237#endif
238 return ver;
239}
240
Aaron Durbinecbfa992020-05-15 17:01:58 -0600241static void do_fsp_memory_init(const struct fspm_context *context, bool s3wake)
Andrey Petrov465fc132016-02-25 14:16:33 -0800242{
Brandon Breitensteinc31ba0e2016-07-27 17:34:45 -0700243 uint32_t status;
Andrey Petrov465fc132016-02-25 14:16:33 -0800244 fsp_memory_init_fn fsp_raminit;
Brandon Breitensteinc31ba0e2016-07-27 17:34:45 -0700245 FSPM_UPD fspm_upd, *upd;
246 FSPM_ARCH_UPD *arch_upd;
Subrata Banik79274e012023-06-19 11:32:19 +0000247 uint32_t version;
Aaron Durbinecbfa992020-05-15 17:01:58 -0600248 const struct fsp_header *hdr = &context->header;
249 const struct memranges *memmap = &context->memmap;
Andrey Petrov465fc132016-02-25 14:16:33 -0800250
Furquan Shaikh585210a2018-10-16 11:54:37 -0700251 post_code(POST_MEM_PREINIT_PREP_START);
Andrey Petrov465fc132016-02-25 14:16:33 -0800252
Subrata Banik79274e012023-06-19 11:32:19 +0000253 if (CONFIG(MRC_CACHE_USING_MRC_VERSION))
254 version = fsp_mrc_version();
255 else
256 version = fsp_memory_settings_version(hdr);
Aaron Durbina3cecb22017-04-25 21:58:10 -0500257
Patrick Rudolph31218a42020-11-30 15:50:06 +0100258 upd = (FSPM_UPD *)(uintptr_t)(hdr->cfg_region_offset + hdr->image_base);
Andrey Petrov465fc132016-02-25 14:16:33 -0800259
Felix Helda0955952021-02-02 21:30:25 +0100260 /*
261 * Verify UPD region size. We don't have malloc before ramstage, so we
262 * use a static buffer for the FSP-M UPDs which is sizeof(FSPM_UPD)
263 * bytes long, since that is the value known at compile time. If
264 * hdr->cfg_region_size is bigger than that, not all UPD defaults will
265 * be copied, so it'll contain random data at the end, so we just call
266 * die() in that case. If hdr->cfg_region_size is smaller than that,
267 * there's a mismatch between the FSP and the header, but since it will
268 * copy the full UPD defaults to the buffer, we try to continue and
269 * hope that there was no incompatible change in the UPDs.
270 */
271 if (hdr->cfg_region_size > sizeof(FSPM_UPD))
272 die("FSP-M UPD size is larger than FSPM_UPD struct size.\n");
273 if (hdr->cfg_region_size < sizeof(FSPM_UPD))
274 printk(BIOS_ERR, "FSP-M UPD size is smaller than FSPM_UPD struct size. "
275 "Check if the FSP binary matches the FSP headers.\n");
276
Felix Held88995982021-01-28 22:43:52 +0100277 fsp_verify_upd_header_signature(upd->FspUpdHeader.Signature, FSPM_UPD_SIGNATURE);
Andrey Petrov465fc132016-02-25 14:16:33 -0800278
279 /* Copy the default values from the UPD area */
280 memcpy(&fspm_upd, upd, sizeof(fspm_upd));
281
Aaron Durbin02e504c2016-07-18 11:53:10 -0500282 arch_upd = &fspm_upd.FspmArchUpd;
283
Aaron Durbin27928682016-07-15 22:32:28 -0500284 /* Reserve enough memory under TOLUD to save CBMEM header */
Aaron Durbin02e504c2016-07-18 11:53:10 -0500285 arch_upd->BootLoaderTolumSize = cbmem_overhead_size();
Aaron Durbin27928682016-07-15 22:32:28 -0500286
Aaron Durbinb4302502016-07-17 17:04:37 -0500287 /* Fill common settings on behalf of chipset. */
Subrata Banik79274e012023-06-19 11:32:19 +0000288 if (fsp_fill_common_arch_params(arch_upd, s3wake, version,
Aaron Durbin02e504c2016-07-18 11:53:10 -0500289 memmap) != CB_SUCCESS)
Keith Shortbb41aba2019-05-16 14:07:43 -0600290 die_with_post_code(POST_INVALID_VENDOR_BINARY,
291 "FSPM_ARCH_UPD not found!\n");
Aaron Durbinb4302502016-07-17 17:04:37 -0500292
Subrata Banik30a01142023-03-22 00:35:42 +0530293 /* Early caching of RAMTOP region if valid mrc cache data is found */
294#if (CONFIG(SOC_INTEL_COMMON_BASECODE_RAMTOP))
Subrata Banikdbfbfaf2023-02-28 07:01:26 +0000295 if (arch_upd->NvsBufferPtr)
Subrata Banik30a01142023-03-22 00:35:42 +0530296 early_ramtop_enable_cache_range();
Subrata Banikdbfbfaf2023-02-28 07:01:26 +0000297#endif
298
Andrey Petrov465fc132016-02-25 14:16:33 -0800299 /* Give SoC and mainboard a chance to update the UPD */
Subrata Banik79274e012023-06-19 11:32:19 +0000300 platform_fsp_memory_init_params_cb(&fspm_upd, version);
Andrey Petrov465fc132016-02-25 14:16:33 -0800301
Furquan Shaikhdbce8ba2020-06-05 19:17:00 -0700302 /*
303 * For S3 resume case, if valid mrc cache data is not found or
304 * RECOVERY_MRC_CACHE hash verification fails, the S3 data
305 * pointer would be null and S3 resume fails with fsp-m
306 * returning error. Invoking a reset here saves time.
307 */
308 if (s3wake && !arch_upd->NvsBufferPtr)
309 /* FIXME: A "system" reset is likely enough: */
310 full_reset();
311
Julius Wernercd49cce2019-03-05 16:53:33 -0800312 if (CONFIG(MMA))
Pratik Prajapatiffc934d2016-11-18 14:36:34 -0800313 setup_mma(&fspm_upd.FspmConfig);
314
Furquan Shaikh585210a2018-10-16 11:54:37 -0700315 post_code(POST_MEM_PREINIT_PREP_END);
316
Andrey Petrov465fc132016-02-25 14:16:33 -0800317 /* Call FspMemoryInit */
Julian Schroeder8a576f62021-11-02 16:32:28 -0500318 fsp_raminit = (void *)(uintptr_t)(hdr->image_base + hdr->fsp_memory_init_entry_offset);
Lee Leahyac3b0a62016-07-27 07:40:25 -0700319 fsp_debug_before_memory_init(fsp_raminit, upd, &fspm_upd);
Andrey Petrov465fc132016-02-25 14:16:33 -0800320
Arthur Heymansfdf6d122022-05-17 13:07:30 +0200321 /* FSP disables the interrupt handler so remove debug exceptions temporarily */
322 null_breakpoint_disable();
Alexandru Gagniucc4ea8f72016-05-23 12:16:58 -0700323 post_code(POST_FSP_MEMORY_INIT);
Andrey Petrov465fc132016-02-25 14:16:33 -0800324 timestamp_add_now(TS_FSP_MEMORY_INIT_START);
Patrick Rudolph31218a42020-11-30 15:50:06 +0100325 if (ENV_X86_64 && CONFIG(PLATFORM_USES_FSP2_X86_32))
Patrick Rudolph40beb362020-12-01 10:08:38 +0100326 status = protected_mode_call_2arg(fsp_raminit,
327 (uintptr_t)&fspm_upd,
328 (uintptr_t)fsp_get_hob_list_ptr());
329 else
330 status = fsp_raminit(&fspm_upd, fsp_get_hob_list_ptr());
Arthur Heymansfdf6d122022-05-17 13:07:30 +0200331 null_breakpoint_init();
Patrick Rudolph40beb362020-12-01 10:08:38 +0100332
Subrata Banik0755ab92017-07-12 15:31:06 +0530333 post_code(POST_FSP_MEMORY_EXIT);
Andrey Petrov465fc132016-02-25 14:16:33 -0800334 timestamp_add_now(TS_FSP_MEMORY_INIT_END);
335
Lee Leahy9671faa2016-07-24 18:18:52 -0700336 /* Handle any errors returned by FspMemoryInit */
Aaron Durbinf41f2aa2016-07-18 12:03:58 -0500337 fsp_handle_reset(status);
Lee Leahy9671faa2016-07-24 18:18:52 -0700338 if (status != FSP_SUCCESS) {
Keith Short24302632019-05-16 14:08:31 -0600339 die_with_post_code(POST_RAM_FAILURE,
Angel Pons2b1f8d42022-01-01 17:20:00 +0100340 "FspMemoryInit returned with error 0x%08x!\n", status);
Lee Leahy9671faa2016-07-24 18:18:52 -0700341 }
Aaron Durbinf41f2aa2016-07-18 12:03:58 -0500342
Subrata Banik79274e012023-06-19 11:32:19 +0000343 do_fsp_post_memory_init(s3wake, version);
Matthew Garrett78b58a42018-07-28 16:53:16 -0700344
345 /*
346 * fsp_debug_after_memory_init() checks whether the end of the tolum
347 * region is the same as the top of cbmem, so must be called here
348 * after cbmem has been initialised in do_fsp_post_memory_init().
349 */
350 fsp_debug_after_memory_init(status);
Andrey Petrov465fc132016-02-25 14:16:33 -0800351}
352
Julius Werner8205ce62021-03-10 17:25:01 -0800353static void *fspm_allocator(void *arg, size_t size, const union cbfs_mdata *unused)
Aaron Durbind04639b2016-07-17 23:23:59 -0500354{
Julius Werner8205ce62021-03-10 17:25:01 -0800355 const struct fsp_load_descriptor *fspld = arg;
Aaron Durbinecbfa992020-05-15 17:01:58 -0600356 struct fspm_context *context = fspld->arg;
Aaron Durbinecbfa992020-05-15 17:01:58 -0600357 struct memranges *memmap = &context->memmap;
Aaron Durbind04639b2016-07-17 23:23:59 -0500358
Aaron Durbinecbfa992020-05-15 17:01:58 -0600359 /* Non XIP FSP-M uses FSP-M address */
Julius Werner8205ce62021-03-10 17:25:01 -0800360 uintptr_t fspm_begin = (uintptr_t)CONFIG_FSP_M_ADDR;
361 uintptr_t fspm_end = fspm_begin + size;
Aaron Durbinecbfa992020-05-15 17:01:58 -0600362
363 if (check_region_overlap(memmap, "FSPM", fspm_begin, fspm_end) != CB_SUCCESS)
Julius Werner8205ce62021-03-10 17:25:01 -0800364 return NULL;
Aaron Durbinecbfa992020-05-15 17:01:58 -0600365
Julius Werner8205ce62021-03-10 17:25:01 -0800366 return (void *)fspm_begin;
Aaron Durbind04639b2016-07-17 23:23:59 -0500367}
368
Raul E Rangel15928462021-11-05 10:29:24 -0600369void preload_fspm(void)
370{
371 if (!CONFIG(CBFS_PRELOAD))
372 return;
373
374 printk(BIOS_DEBUG, "Preloading %s\n", CONFIG_FSP_M_CBFS);
375 cbfs_preload(CONFIG_FSP_M_CBFS);
376}
377
Lee Leahy9671faa2016-07-24 18:18:52 -0700378void fsp_memory_init(bool s3wake)
Andrey Petrov465fc132016-02-25 14:16:33 -0800379{
Marshall Dawsone3aa4242019-10-16 21:53:21 -0600380 struct range_entry prog_ranges[2];
Aaron Durbinecbfa992020-05-15 17:01:58 -0600381 struct fspm_context context;
382 struct fsp_load_descriptor fspld = {
383 .fsp_prog = PROG_INIT(PROG_REFCODE, CONFIG_FSP_M_CBFS),
Aaron Durbinecbfa992020-05-15 17:01:58 -0600384 .arg = &context,
385 };
386 struct fsp_header *hdr = &context.header;
387 struct memranges *memmap = &context.memmap;
Andrey Petrov465fc132016-02-25 14:16:33 -0800388
Julius Werner8205ce62021-03-10 17:25:01 -0800389 /* For FSP-M XIP we leave alloc NULL to get a direct mapping to flash. */
390 if (!CONFIG(FSP_M_XIP))
391 fspld.alloc = fspm_allocator;
392
Kyösti Mälkki7f50afb2019-09-11 17:12:26 +0300393 elog_boot_notify(s3wake);
Furquan Shaikh5aea5882016-07-30 18:10:05 -0700394
Aaron Durbin02e504c2016-07-18 11:53:10 -0500395 /* Build up memory map of romstage address space including CAR. */
Aaron Durbinecbfa992020-05-15 17:01:58 -0600396 memranges_init_empty(memmap, &prog_ranges[0], ARRAY_SIZE(prog_ranges));
Marshall Dawsone3aa4242019-10-16 21:53:21 -0600397 if (ENV_CACHE_AS_RAM)
Aaron Durbinecbfa992020-05-15 17:01:58 -0600398 memranges_insert(memmap, (uintptr_t)_car_region_start,
Marshall Dawsone3aa4242019-10-16 21:53:21 -0600399 _car_unallocated_start - _car_region_start, 0);
Aaron Durbinecbfa992020-05-15 17:01:58 -0600400 memranges_insert(memmap, (uintptr_t)_program, REGION_SIZE(program), 0);
Aaron Durbin02e504c2016-07-18 11:53:10 -0500401
Martin Roth146508d2021-04-30 16:45:08 -0600402 timestamp_add_now(TS_FSP_MEMORY_INIT_LOAD);
Aaron Durbinecbfa992020-05-15 17:01:58 -0600403 if (fsp_load_component(&fspld, hdr) != CB_SUCCESS)
404 die("FSPM not available or failed to load!\n");
Andrey Petrov465fc132016-02-25 14:16:33 -0800405
Julius Werner8205ce62021-03-10 17:25:01 -0800406 if (CONFIG(FSP_M_XIP) && (uintptr_t)prog_start(&fspld.fsp_prog) != hdr->image_base)
407 die("FSPM XIP base does not match: %p vs %p\n",
408 (void *)(uintptr_t)hdr->image_base, prog_start(&fspld.fsp_prog));
409
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100410 timestamp_add_now(TS_INITRAM_START);
Kyösti Mälkki216db612019-09-11 09:57:14 +0300411
Aaron Durbinecbfa992020-05-15 17:01:58 -0600412 do_fsp_memory_init(&context, s3wake);
Kyösti Mälkki0889e932019-08-18 07:40:43 +0300413
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100414 timestamp_add_now(TS_INITRAM_END);
Andrey Petrov465fc132016-02-25 14:16:33 -0800415}