1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 #include <arch/null_breakpoint.h>
4 #include <arch/stack_canary_breakpoint.h>
5 #include <arch/symbols.h>
10 #include <console/console.h>
15 #include <mode_switch.h>
16 #include <mrc_cache.h>
17 #include <program_loading.h>
18 #include <romstage_handoff.h>
19 #include <security/tpm/tspi.h>
20 #include <security/vboot/antirollback.h>
21 #include <security/vboot/vboot_common.h>
24 #include <timestamp.h>
28 #if CONFIG(SOC_INTEL_COMMON_BASECODE_RAMTOP)
29 #include <intelbasecode/ramtop.h>
32 /* Callbacks for SoC/Mainboard specific overrides */
33 void __weak
platform_fsp_memory_multi_phase_init_cb(uint32_t phase_index
)
35 /* Leave for the SoC/Mainboard to implement if necessary. */
38 static uint8_t temp_ram
[CONFIG_FSP_TEMP_RAM_SIZE
] __aligned(16);
41 * Helper function to store the MRC cache version into CBMEM
43 * ramstage uses either the MRC version or FSP-M version (depending on the config)
44 * when updating the MRC cache
46 static void do_cbmem_version_entry(uint32_t cbmem_id
, uint32_t version
)
48 uint32_t *cbmem_version_entry
= cbmem_add(cbmem_id
, sizeof(version
));
49 if (!cbmem_version_entry
) {
50 printk(BIOS_ERR
, "Failed to add %s version to cbmem.\n",
51 CONFIG(MRC_CACHE_USING_MRC_VERSION
) ? "MRC" : "FSP-M");
54 *cbmem_version_entry
= version
;
57 static void do_fsp_post_memory_init(bool s3wake
, uint32_t version
)
59 struct range_entry fsp_mem
;
60 uint32_t cbmem_id
= CONFIG(MRC_CACHE_USING_MRC_VERSION
) ? CBMEM_ID_MRC_VERSION
:
61 CBMEM_ID_FSPM_VERSION
;
63 fsp_find_reserved_memory(&fsp_mem
);
65 /* initialize cbmem by adding FSP reserved memory first thing */
67 cbmem_initialize_empty_id_size(CBMEM_ID_FSP_RESERVED_MEMORY
,
68 range_entry_size(&fsp_mem
));
69 } else if (cbmem_initialize_id_size(CBMEM_ID_FSP_RESERVED_MEMORY
,
70 range_entry_size(&fsp_mem
))) {
71 if (CONFIG(HAVE_ACPI_RESUME
)) {
72 printk(BIOS_ERR
, "Failed to recover CBMEM in S3 resume.\n");
73 /* Failed S3 resume, reset to come up cleanly */
74 /* FIXME: A "system" reset is likely enough: */
79 /* make sure FSP memory is reserved in cbmem */
80 if (range_entry_base(&fsp_mem
) !=
81 (uintptr_t)cbmem_find(CBMEM_ID_FSP_RESERVED_MEMORY
))
82 die("Failed to accommodate FSP reserved memory request!\n");
84 if (CONFIG(CACHE_MRC_SETTINGS
) && !s3wake
) {
85 do_cbmem_version_entry(cbmem_id
, version
);
86 if (!CONFIG(FSP_NVS_DATA_POST_SILICON_INIT
))
87 save_memory_training_data();
90 /* Create romstage handoff information */
91 romstage_handoff_init(s3wake
);
94 static void fsp_fill_mrc_cache(FSPM_ARCHx_UPD
*arch_upd
, uint32_t version
)
99 arch_upd
->NvsBufferPtr
= 0;
101 if (!CONFIG(CACHE_MRC_SETTINGS
))
104 /* Assume boot device is memory mapped. */
105 assert(CONFIG(BOOT_DEVICE_MEMORY_MAPPED
));
107 data
= mrc_cache_current_mmap_leak(MRC_TRAINING_DATA
, version
,
112 /* MRC cache found */
113 arch_upd
->NvsBufferPtr
= (uintptr_t)data
;
115 printk(BIOS_SPEW
, "MRC cache found, size %zu bytes\n", mrc_size
);
118 static enum cb_err
check_region_overlap(const struct memranges
*ranges
,
119 const char *description
,
120 uintptr_t begin
, uintptr_t end
)
122 const struct range_entry
*r
;
124 memranges_each_entry(r
, ranges
) {
125 if (end
<= range_entry_base(r
))
127 if (begin
>= range_entry_end(r
))
129 printk(BIOS_CRIT
, "'%s' overlaps currently running program: "
130 "[%p, %p)\n", description
, (void *)begin
, (void *)end
);
137 static enum cb_err
setup_fsp_stack_frame(FSPM_ARCHx_UPD
*arch_upd
,
138 const struct memranges
*memmap
)
140 uintptr_t stack_begin
;
144 * FSPM_UPD passed here is populated with default values
145 * provided by the blob itself. We let FSPM use top of CAR
146 * region of the size it requests.
148 stack_end
= (uintptr_t)_car_region_end
;
149 stack_begin
= stack_end
- arch_upd
->StackSize
;
150 if (check_region_overlap(memmap
, "FSPM stack", stack_begin
,
151 stack_end
) != CB_SUCCESS
)
154 arch_upd
->StackBase
= stack_begin
;
158 static enum cb_err
fsp_fill_common_arch_params(FSPM_ARCHx_UPD
*arch_upd
,
159 bool s3wake
, uint32_t version
,
160 const struct memranges
*memmap
)
163 * FSP 2.1 version would use same stack as coreboot instead of
164 * setting up separate stack frame. FSP 2.1 would not relocate stack
165 * top and does not reinitialize stack pointer. The parameters passed
166 * as StackBase and StackSize are actually for temporary RAM and HOBs
167 * and are not related to FSP stack at all.
168 * Non-CAR FSP 2.0 platforms pass a DRAM location for the FSP stack.
170 static const char * const fsp_bootmode_strings
[] = {
171 [FSP_BOOT_WITH_FULL_CONFIGURATION
] = "boot with full config",
172 [FSP_BOOT_WITH_MINIMAL_CONFIGURATION
] = "boot with minimal config",
173 [FSP_BOOT_ASSUMING_NO_CONFIGURATION_CHANGES
] = "boot assuming no config change",
174 [FSP_BOOT_ON_S4_RESUME
] = "boot on s4 resume",
175 [FSP_BOOT_ON_S3_RESUME
] = "boot on s3 resume",
176 [FSP_BOOT_ON_FLASH_UPDATE
] = "boot on flash update",
177 [FSP_BOOT_IN_RECOVERY_MODE
] = "boot in recovery mode",
180 if (CONFIG(FSP_USES_CB_STACK
) && ENV_RAMINIT
181 && CONFIG(FSP_SPEC_VIOLATION_XEON_SP_HEAP_WORKAROUND
)) {
182 DECLARE_REGION(fspm_heap
);
183 arch_upd
->StackBase
= (uintptr_t)_fspm_heap
;
184 arch_upd
->StackSize
= (size_t)REGION_SIZE(fspm_heap
);
185 } else if (CONFIG(FSP_USES_CB_STACK
) || !ENV_CACHE_AS_RAM
) {
186 arch_upd
->StackBase
= (uintptr_t)temp_ram
;
187 arch_upd
->StackSize
= sizeof(temp_ram
);
188 } else if (setup_fsp_stack_frame(arch_upd
, memmap
)) {
192 fsp_fill_mrc_cache(arch_upd
, version
);
194 /* Configure bootmode */
196 arch_upd
->BootMode
= FSP_BOOT_ON_S3_RESUME
;
198 if (arch_upd
->NvsBufferPtr
)
200 FSP_BOOT_ASSUMING_NO_CONFIGURATION_CHANGES
;
202 arch_upd
->BootMode
= FSP_BOOT_WITH_FULL_CONFIGURATION
;
205 if (arch_upd
->BootMode
< ARRAY_SIZE(fsp_bootmode_strings
) &&
206 fsp_bootmode_strings
[arch_upd
->BootMode
] != NULL
)
207 printk(BIOS_SPEW
, "bootmode is set to: %d (%s)\n", arch_upd
->BootMode
,
208 fsp_bootmode_strings
[arch_upd
->BootMode
]);
210 printk(BIOS_SPEW
, "bootmode is set to: %d (unknown mode)\n", arch_upd
->BootMode
);
216 uint8_t fsp_memory_mainboard_version(void)
222 uint8_t fsp_memory_soc_version(void)
228 * Allow SoC and/or mainboard to bump the revision of the FSP setting
229 * number. The FSP spec uses the low 8 bits as the build number. Take over
230 * bits 3:0 for the SoC setting and bits 7:4 for the mainboard. That way
231 * a tweak in the settings will bump the version used to track the cached
232 * setting which triggers retraining when the FSP version hasn't changed, but
233 * the SoC or mainboard settings have.
235 static uint32_t fsp_memory_settings_version(const struct fsp_header
*hdr
)
237 /* Use the full FSP version by default. */
238 uint32_t ver
= hdr
->image_revision
;
240 if (!CONFIG(FSP_PLATFORM_MEMORY_SETTINGS_VERSIONS
))
244 ver
|= (0xf & fsp_memory_mainboard_version()) << 4;
245 ver
|= (0xf & fsp_memory_soc_version()) << 0;
250 struct fspm_context
{
251 struct fsp_header header
;
252 struct memranges memmap
;
256 * Helper function to read MRC version
258 * There are multiple ways to read the MRC version using
259 * Intel FSP. Currently the only supported method to get the
260 * MRC version is by reading the FSP_PRODUCDER_DATA_TABLES
261 * from the FSP-M binary (by parsing the FSP header).
263 static uint32_t fsp_mrc_version(const struct fsp_header
*hdr
)
266 #if CONFIG(MRC_CACHE_USING_MRC_VERSION)
267 void *fspm_blob_file
= (void *)(uintptr_t)hdr
->image_base
;
268 FSP_PRODUCER_DATA_TABLES
*ft
= fspm_blob_file
+ FSP_HDR_OFFSET
;
269 FSP_PRODUCER_DATA_TYPE2
*table2
= &ft
->FspProduceDataType2
;
270 size_t mrc_version_size
= sizeof(table2
->MrcVersion
);
271 for (size_t i
= 0; i
< mrc_version_size
; i
++) {
272 ver
|= (table2
->MrcVersion
[i
] << ((mrc_version_size
- 1) - i
) * 8);
278 static void fspm_return_value_handler(const char *context
, efi_return_status_t status
,
281 if (status
== FSP_SUCCESS
)
284 fsp_handle_reset(status
);
286 fsp_die_with_post_code(status
, POSTCODE_RAM_FAILURE
, "%s error", context
);
288 fsp_printk(status
, BIOS_SPEW
, "%s", context
);
291 static void fspm_multi_phase_init(const struct fsp_header
*hdr
)
293 efi_return_status_t status
;
294 fsp_multi_phase_init_fn fsp_multi_phase_init
;
295 struct fsp_multi_phase_params multi_phase_params
;
296 struct fsp_multi_phase_get_number_of_phases_params multi_phase_get_number
;
298 if (!hdr
->fsp_multi_phase_mem_init_entry_offset
)
301 fsp_multi_phase_init
= (fsp_multi_phase_init_fn
)(uintptr_t)
302 (hdr
->image_base
+ hdr
->fsp_multi_phase_mem_init_entry_offset
);
304 post_code(POSTCODE_FSP_MULTI_PHASE_MEM_INIT_ENTRY
);
305 timestamp_add_now(TS_FSP_MULTI_PHASE_MEM_INIT_START
);
307 /* Get number of phases */
308 multi_phase_params
.multi_phase_action
= GET_NUMBER_OF_PHASES
;
309 multi_phase_params
.phase_index
= 0;
310 multi_phase_params
.multi_phase_param_ptr
= &multi_phase_get_number
;
311 status
= fsp_multi_phase_init(&multi_phase_params
);
312 fspm_return_value_handler("FspMultiPhaseMemInit NumberOfPhases", status
, false);
314 /* Execute all phases */
315 for (uint32_t i
= 1; i
<= multi_phase_get_number
.number_of_phases
; i
++) {
316 printk(BIOS_SPEW
, "Executing Phase %u of FspMultiPhaseMemInit\n", i
);
318 * Give SoC/mainboard a chance to perform any operation before
319 * Multi Phase Execution
321 platform_fsp_memory_multi_phase_init_cb(i
);
323 multi_phase_params
.multi_phase_action
= EXECUTE_PHASE
;
324 multi_phase_params
.phase_index
= i
;
325 multi_phase_params
.multi_phase_param_ptr
= NULL
;
326 status
= fsp_multi_phase_init(&multi_phase_params
);
327 fspm_return_value_handler("FspMultiPhaseMemInit Execute", status
, false);
330 post_code(POSTCODE_FSP_MULTI_PHASE_MEM_INIT_EXIT
);
331 timestamp_add_now(TS_FSP_MULTI_PHASE_MEM_INIT_END
);
334 static void do_fsp_memory_init(const struct fspm_context
*context
, bool s3wake
)
336 efi_return_status_t status
;
337 fsp_memory_init_fn fsp_raminit
;
338 FSPM_UPD fspm_upd
, *upd
;
339 FSPM_ARCHx_UPD
*arch_upd
;
341 const struct fsp_header
*hdr
= &context
->header
;
342 const struct memranges
*memmap
= &context
->memmap
;
344 post_code(POSTCODE_MEM_PREINIT_PREP_START
);
346 if (CONFIG(MRC_CACHE_USING_MRC_VERSION
))
347 version
= fsp_mrc_version(hdr
);
349 version
= fsp_memory_settings_version(hdr
);
351 upd
= (FSPM_UPD
*)(uintptr_t)(hdr
->cfg_region_offset
+ hdr
->image_base
);
354 * Verify UPD region size. We don't have malloc before ramstage, so we
355 * use a static buffer for the FSP-M UPDs which is sizeof(FSPM_UPD)
356 * bytes long, since that is the value known at compile time. If
357 * hdr->cfg_region_size is bigger than that, not all UPD defaults will
358 * be copied, so it'll contain random data at the end, so we just call
359 * die() in that case. If hdr->cfg_region_size is smaller than that,
360 * there's a mismatch between the FSP and the header, but since it will
361 * copy the full UPD defaults to the buffer, we try to continue and
362 * hope that there was no incompatible change in the UPDs.
364 if (hdr
->cfg_region_size
> sizeof(FSPM_UPD
))
365 die("FSP-M UPD size is larger than FSPM_UPD struct size.\n");
366 if (hdr
->cfg_region_size
< sizeof(FSPM_UPD
))
367 printk(BIOS_ERR
, "FSP-M UPD size is smaller than FSPM_UPD struct size. "
368 "Check if the FSP binary matches the FSP headers.\n");
370 fsp_verify_upd_header_signature(upd
->FspUpdHeader
.Signature
, FSPM_UPD_SIGNATURE
);
372 /* Copy the default values from the UPD area */
373 memcpy(&fspm_upd
, upd
, sizeof(fspm_upd
));
375 arch_upd
= &fspm_upd
.FspmArchUpd
;
377 /* Reserve enough memory under TOLUD to save CBMEM header */
378 arch_upd
->BootLoaderTolumSize
= cbmem_overhead_size();
380 /* Fill common settings on behalf of chipset. */
381 if (fsp_fill_common_arch_params(arch_upd
, s3wake
, version
,
382 memmap
) != CB_SUCCESS
)
383 die_with_post_code(POSTCODE_INVALID_VENDOR_BINARY
,
384 "FSPM_ARCH_UPD not found!\n");
386 /* Early caching of RAMTOP region if valid mrc cache data is found */
387 #if (CONFIG(SOC_INTEL_COMMON_BASECODE_RAMTOP))
388 if (arch_upd
->NvsBufferPtr
)
389 early_ramtop_enable_cache_range();
392 /* Give SoC and mainboard a chance to update the UPD */
393 platform_fsp_memory_init_params_cb(&fspm_upd
, version
);
396 * For S3 resume case, if valid mrc cache data is not found or
397 * RECOVERY_MRC_CACHE hash verification fails, the S3 data
398 * pointer would be null and S3 resume fails with fsp-m
399 * returning error. Invoking a reset here saves time.
401 if (s3wake
&& !arch_upd
->NvsBufferPtr
)
402 /* FIXME: A "system" reset is likely enough: */
406 setup_mma(&fspm_upd
.FspmConfig
);
408 post_code(POSTCODE_MEM_PREINIT_PREP_END
);
410 /* Call FspMemoryInit */
411 fsp_raminit
= (void *)(uintptr_t)(hdr
->image_base
+ hdr
->fsp_memory_init_entry_offset
);
412 fsp_debug_before_memory_init(fsp_raminit
, upd
, &fspm_upd
);
414 /* FSP disables the interrupt handler so remove debug exceptions temporarily */
415 null_breakpoint_remove();
416 stack_canary_breakpoint_remove();
417 post_code(POSTCODE_FSP_MEMORY_INIT
);
418 timestamp_add_now(TS_FSP_MEMORY_INIT_START
);
419 if (ENV_X86_64
&& CONFIG(PLATFORM_USES_FSP2_X86_32
))
420 status
= protected_mode_call_2arg(fsp_raminit
,
421 (uintptr_t)&fspm_upd
,
422 (uintptr_t)fsp_get_hob_list_ptr());
424 status
= fsp_raminit(&fspm_upd
, fsp_get_hob_list_ptr());
425 null_breakpoint_init();
426 stack_canary_breakpoint_init();
428 post_code(POSTCODE_FSP_MEMORY_EXIT
);
429 timestamp_add_now(TS_FSP_MEMORY_INIT_END
);
431 /* Handle any errors returned by FspMemoryInit */
432 fspm_return_value_handler("FspMemoryInit", status
, true);
434 if (CONFIG(PLATFORM_USES_FSP2_4
))
435 fspm_multi_phase_init(hdr
);
437 do_fsp_post_memory_init(s3wake
, version
);
440 * fsp_debug_after_memory_init() checks whether the end of the tolum
441 * region is the same as the top of cbmem, so must be called here
442 * after cbmem has been initialised in do_fsp_post_memory_init().
444 fsp_debug_after_memory_init(status
);
447 static void *fspm_allocator(void *arg
, size_t size
, const union cbfs_mdata
*unused
)
449 const struct fsp_load_descriptor
*fspld
= arg
;
450 struct fspm_context
*context
= fspld
->arg
;
451 struct memranges
*memmap
= &context
->memmap
;
453 /* Non XIP FSP-M uses FSP-M address */
454 uintptr_t fspm_begin
= (uintptr_t)CONFIG_FSP_M_ADDR
;
455 uintptr_t fspm_end
= fspm_begin
+ size
;
457 if (check_region_overlap(memmap
, "FSPM", fspm_begin
, fspm_end
) != CB_SUCCESS
)
460 return (void *)fspm_begin
;
463 void preload_fspm(void)
465 if (!CONFIG(CBFS_PRELOAD
))
468 const char *fspm_cbfs
= soc_select_fsp_m_cbfs();
469 printk(BIOS_DEBUG
, "Preloading %s\n", fspm_cbfs
);
470 cbfs_preload(fspm_cbfs
);
473 void fsp_memory_init(bool s3wake
)
475 struct range_entry prog_ranges
[2];
476 struct fspm_context context
;
477 const char *fspm_cbfs
= soc_select_fsp_m_cbfs();
478 struct fsp_load_descriptor fspld
= {
479 .fsp_prog
= PROG_INIT(PROG_REFCODE
, fspm_cbfs
),
482 struct fsp_header
*hdr
= &context
.header
;
483 struct memranges
*memmap
= &context
.memmap
;
485 /* For FSP-M XIP we leave alloc NULL to get a direct mapping to flash. */
486 if (!CONFIG(FSP_M_XIP
))
487 fspld
.alloc
= fspm_allocator
;
489 elog_boot_notify(s3wake
);
491 /* Build up memory map of romstage address space including CAR. */
492 memranges_init_empty(memmap
, &prog_ranges
[0], ARRAY_SIZE(prog_ranges
));
493 if (ENV_CACHE_AS_RAM
)
494 memranges_insert(memmap
, (uintptr_t)_car_region_start
,
495 _car_unallocated_start
- _car_region_start
, 0);
496 memranges_insert(memmap
, (uintptr_t)_program
, REGION_SIZE(program
), 0);
498 timestamp_add_now(TS_FSP_MEMORY_INIT_LOAD
);
499 if (fsp_load_component(&fspld
, hdr
) != CB_SUCCESS
)
500 die("FSPM not available or failed to load!\n");
502 if (CONFIG(FSP_M_XIP
) && (uintptr_t)prog_start(&fspld
.fsp_prog
) != hdr
->image_base
)
503 die("FSPM XIP base does not match: %p vs %p\n",
504 (void *)(uintptr_t)hdr
->image_base
, prog_start(&fspld
.fsp_prog
));
506 timestamp_add_now(TS_INITRAM_START
);
508 do_fsp_memory_init(&context
, s3wake
);
510 timestamp_add_now(TS_INITRAM_END
);