Debian Codegraph @

src/drivers/intel/fsp2_0/memory_init.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 #include <arch/null_breakpoint.h>
4 #include <arch/stack_canary_breakpoint.h>
5 #include <arch/symbols.h>
6 #include <assert.h>
7 #include <bootsplash.h>
8 #include <cbfs.h>
9 #include <cbmem.h>
10 #include <cf9_reset.h>
11 #include <console/console.h>
12 #include <elog.h>
13 #include <fsp/api.h>
14 #include <fsp/util.h>
15 #include <memrange.h>
16 #include <mode_switch.h>
17 #include <mrc_cache.h>
18 #include <program_loading.h>
19 #include <romstage_handoff.h>
20 #include <security/tpm/tspi.h>
21 #include <security/vboot/antirollback.h>
22 #include <security/vboot/vboot_common.h>
23 #include <soc/intel/common/reset.h>
24 #include <string.h>
25 #include <symbols.h>
26 #include <timestamp.h>
27 #include <types.h>
28 #include <vb2_api.h>
29
30 #if CONFIG(SOC_INTEL_COMMON_BASECODE_RAMTOP)
31 #include <intelbasecode/ramtop.h>
32 #endif
33
34 /* Callbacks for SoC/Mainboard specific overrides */
35 void __weak platform_fsp_memory_multi_phase_init_cb(uint32_t phase_index)
36 {
37 /* Leave for the SoC/Mainboard to implement if necessary. */
38 }
39
40 static uint8_t temp_ram[CONFIG_FSP_TEMP_RAM_SIZE] __aligned(16);
41
42 /*
43 * Helper function to store the MRC cache version into CBMEM
44 *
45 * ramstage uses either the MRC version or FSP-M version (depending on the config)
46 * when updating the MRC cache
47 */
48 static void do_cbmem_version_entry(uint32_t cbmem_id, uint32_t version)
49 {
50 uint32_t *cbmem_version_entry = cbmem_add(cbmem_id, sizeof(version));
51 if (!cbmem_version_entry) {
52 printk(BIOS_ERR, "Failed to add %s version to cbmem.\n",
53 CONFIG(MRC_CACHE_USING_MRC_VERSION) ? "MRC" : "FSP-M");
54 return;
55 }
56 *cbmem_version_entry = version;
57 }
58
59 static void do_fsp_post_memory_init(bool s3wake, uint32_t version)
60 {
61 struct range_entry fsp_mem;
62 uint32_t cbmem_id = CONFIG(MRC_CACHE_USING_MRC_VERSION) ? CBMEM_ID_MRC_VERSION :
63 CBMEM_ID_FSPM_VERSION;
64
65 fsp_find_reserved_memory(&fsp_mem);
66
67 /* initialize cbmem by adding FSP reserved memory first thing */
68 if (!s3wake) {
69 cbmem_initialize_empty_id_size(CBMEM_ID_FSP_RESERVED_MEMORY,
70 range_entry_size(&fsp_mem));
71 } else if (cbmem_initialize_id_size(CBMEM_ID_FSP_RESERVED_MEMORY,
72 range_entry_size(&fsp_mem))) {
73 if (CONFIG(HAVE_ACPI_RESUME)) {
74 printk(BIOS_ERR, "Failed to recover CBMEM in S3 resume.\n");
75 /* Failed S3 resume, reset to come up cleanly */
76 /* FIXME: A "system" reset is likely enough: */
77 full_reset();
78 }
79 }
80
81 /* make sure FSP memory is reserved in cbmem */
82 if (range_entry_base(&fsp_mem) !=
83 (uintptr_t)cbmem_find(CBMEM_ID_FSP_RESERVED_MEMORY))
84 die("Failed to accommodate FSP reserved memory request!\n");
85
86 if (CONFIG(CACHE_MRC_SETTINGS) && !s3wake) {
87 do_cbmem_version_entry(cbmem_id, version);
88 if (!CONFIG(FSP_NVS_DATA_POST_SILICON_INIT))
89 save_memory_training_data();
90 }
91
92 /* Create romstage handoff information */
93 romstage_handoff_init(s3wake);
94 }
95
96 static void fsp_fill_mrc_cache(FSPM_ARCHx_UPD *arch_upd, uint32_t version)
97 {
98 void *data;
99 size_t mrc_size;
100
101 arch_upd->NvsBufferPtr = 0;
102
103 if (!CONFIG(CACHE_MRC_SETTINGS))
104 return;
105
106 /* Assume boot device is memory mapped. */
107 assert(CONFIG(BOOT_DEVICE_MEMORY_MAPPED));
108
109 data = mrc_cache_current_mmap_leak(MRC_TRAINING_DATA, version,
110 &mrc_size);
111 if (data == NULL)
112 return;
113
114 /* MRC cache found */
115 arch_upd->NvsBufferPtr = (uintptr_t)data;
116
117 printk(BIOS_SPEW, "MRC cache found, size %zu bytes\n", mrc_size);
118 }
119
120 static enum cb_err check_region_overlap(const struct memranges *ranges,
121 const char *description,
122 uintptr_t begin, uintptr_t end)
123 {
124 const struct range_entry *r;
125
126 memranges_each_entry(r, ranges) {
127 if (end <= range_entry_base(r))
128 continue;
129 if (begin >= range_entry_end(r))
130 continue;
131 printk(BIOS_CRIT, "'%s' overlaps currently running program: "
132 "[%p, %p)\n", description, (void *)begin, (void *)end);
133 return CB_ERR;
134 }
135
136 return CB_SUCCESS;
137 }
138
139 static enum cb_err setup_fsp_stack_frame(FSPM_ARCHx_UPD *arch_upd,
140 const struct memranges *memmap)
141 {
142 uintptr_t stack_begin;
143 uintptr_t stack_end;
144
145 /*
146 * FSPM_UPD passed here is populated with default values
147 * provided by the blob itself. We let FSPM use top of CAR
148 * region of the size it requests.
149 */
150 stack_end = (uintptr_t)_car_region_end;
151 stack_begin = stack_end - arch_upd->StackSize;
152 if (check_region_overlap(memmap, "FSPM stack", stack_begin,
153 stack_end) != CB_SUCCESS)
154 return CB_ERR;
155
156 arch_upd->StackBase = stack_begin;
157 return CB_SUCCESS;
158 }
159
160 static enum cb_err fsp_fill_common_arch_params(FSPM_ARCHx_UPD *arch_upd,
161 bool s3wake, uint32_t version,
162 const struct memranges *memmap)
163 {
164 /*
165 * FSP 2.1 version would use same stack as coreboot instead of
166 * setting up separate stack frame. FSP 2.1 would not relocate stack
167 * top and does not reinitialize stack pointer. The parameters passed
168 * as StackBase and StackSize are actually for temporary RAM and HOBs
169 * and are not related to FSP stack at all.
170 * Non-CAR FSP 2.0 platforms pass a DRAM location for the FSP stack.
171 */
172 static const char * const fsp_bootmode_strings[] = {
173 [FSP_BOOT_WITH_FULL_CONFIGURATION] = "boot with full config",
174 [FSP_BOOT_WITH_MINIMAL_CONFIGURATION] = "boot with minimal config",
175 [FSP_BOOT_ASSUMING_NO_CONFIGURATION_CHANGES] = "boot assuming no config change",
176 [FSP_BOOT_ON_S4_RESUME] = "boot on s4 resume",
177 [FSP_BOOT_ON_S3_RESUME] = "boot on s3 resume",
178 [FSP_BOOT_ON_FLASH_UPDATE] = "boot on flash update",
179 [FSP_BOOT_IN_RECOVERY_MODE] = "boot in recovery mode",
180 };
181
182 if (CONFIG(FSP_USES_CB_STACK) && ENV_RAMINIT
183 && CONFIG(FSP_SPEC_VIOLATION_XEON_SP_HEAP_WORKAROUND)) {
184 DECLARE_REGION(fspm_heap);
185 arch_upd->StackBase = (uintptr_t)_fspm_heap;
186 arch_upd->StackSize = (size_t)REGION_SIZE(fspm_heap);
187 } else if (CONFIG(FSP_USES_CB_STACK) || !ENV_CACHE_AS_RAM) {
188 arch_upd->StackBase = (uintptr_t)temp_ram;
189 arch_upd->StackSize = sizeof(temp_ram);
190 } else if (setup_fsp_stack_frame(arch_upd, memmap)) {
191 return CB_ERR;
192 }
193
194 fsp_fill_mrc_cache(arch_upd, version);
195
196 /* Configure bootmode */
197 if (s3wake) {
198 arch_upd->BootMode = FSP_BOOT_ON_S3_RESUME;
199 } else {
200 if (arch_upd->NvsBufferPtr)
201 arch_upd->BootMode =
202 FSP_BOOT_ASSUMING_NO_CONFIGURATION_CHANGES;
203 else
204 arch_upd->BootMode = FSP_BOOT_WITH_FULL_CONFIGURATION;
205 }
206
207 if (arch_upd->BootMode < ARRAY_SIZE(fsp_bootmode_strings) &&
208 fsp_bootmode_strings[arch_upd->BootMode] != NULL)
209 printk(BIOS_SPEW, "bootmode is set to: %d (%s)\n", arch_upd->BootMode,
210 fsp_bootmode_strings[arch_upd->BootMode]);
211 else
212 printk(BIOS_SPEW, "bootmode is set to: %d (unknown mode)\n", arch_upd->BootMode);
213
214 return CB_SUCCESS;
215 }
216
217 __weak
218 uint8_t fsp_memory_mainboard_version(void)
219 {
220 return 0;
221 }
222
223 __weak
224 uint8_t fsp_memory_soc_version(void)
225 {
226 return 0;
227 }
228
229 /*
230 * Allow SoC and/or mainboard to bump the revision of the FSP setting
231 * number. The FSP spec uses the low 8 bits as the build number. Take over
232 * bits 3:0 for the SoC setting and bits 7:4 for the mainboard. That way
233 * a tweak in the settings will bump the version used to track the cached
234 * setting which triggers retraining when the FSP version hasn't changed, but
235 * the SoC or mainboard settings have.
236 */
237 static uint32_t fsp_memory_settings_version(const struct fsp_header *hdr)
238 {
239 /* Use the full FSP version by default. */
240 uint32_t ver = hdr->image_revision;
241
242 if (!CONFIG(FSP_PLATFORM_MEMORY_SETTINGS_VERSIONS))
243 return ver;
244
245 ver &= ~0xff;
246 ver |= (0xf & fsp_memory_mainboard_version()) << 4;
247 ver |= (0xf & fsp_memory_soc_version()) << 0;
248
249 return ver;
250 }
251
252 struct fspm_context {
253 struct fsp_header header;
254 struct memranges memmap;
255 };
256
257 /*
258 * Helper function to read MRC version
259 *
260 * There are multiple ways to read the MRC version using
261 * Intel FSP. Currently the only supported method to get the
262 * MRC version is by reading the FSP_PRODUCDER_DATA_TABLES
263 * from the FSP-M binary (by parsing the FSP header).
264 */
265 static uint32_t fsp_mrc_version(const struct fsp_header *hdr)
266 {
267 uint32_t ver = 0;
268 #if CONFIG(MRC_CACHE_USING_MRC_VERSION)
269 void *fspm_blob_file = (void *)(uintptr_t)hdr->image_base;
270 FSP_PRODUCER_DATA_TABLES *ft = fspm_blob_file + FSP_HDR_OFFSET;
271 FSP_PRODUCER_DATA_TYPE2 *table2 = &ft->FspProduceDataType2;
272 size_t mrc_version_size = sizeof(table2->MrcVersion);
273 for (size_t i = 0; i < mrc_version_size; i++) {
274 ver |= (table2->MrcVersion[i] << ((mrc_version_size - 1) - i) * 8);
275 }
276 #endif
277 return ver;
278 }
279
280 static void fspm_return_value_handler(const char *context, efi_return_status_t status,
281 bool die_on_error)
282 {
283 if (status == FSP_SUCCESS)
284 return;
285
286 fsp_handle_reset(status);
287 if (die_on_error)
288 fsp_die_with_post_code(status, POSTCODE_RAM_FAILURE, "%s error", context);
289
290 fsp_printk(status, BIOS_SPEW, "%s", context);
291 }
292
293 static void fspm_multi_phase_init(const struct fsp_header *hdr)
294 {
295 efi_return_status_t status;
296 fsp_multi_phase_init_fn fsp_multi_phase_init;
297 struct fsp_multi_phase_params multi_phase_params;
298 struct fsp_multi_phase_get_number_of_phases_params multi_phase_get_number;
299
300 if (!hdr->fsp_multi_phase_mem_init_entry_offset)
301 return;
302
303 fsp_multi_phase_init = (fsp_multi_phase_init_fn)(uintptr_t)
304 (hdr->image_base + hdr->fsp_multi_phase_mem_init_entry_offset);
305
306 post_code(POSTCODE_FSP_MULTI_PHASE_MEM_INIT_ENTRY);
307 timestamp_add_now(TS_FSP_MULTI_PHASE_MEM_INIT_START);
308
309 /* Get number of phases */
310 multi_phase_params.multi_phase_action = GET_NUMBER_OF_PHASES;
311 multi_phase_params.phase_index = 0;
312 multi_phase_params.multi_phase_param_ptr = &multi_phase_get_number;
313 status = fsp_multi_phase_init(&multi_phase_params);
314 fspm_return_value_handler("FspMultiPhaseMemInit NumberOfPhases", status, false);
315
316 /* Execute all phases */
317 for (uint32_t i = 1; i <= multi_phase_get_number.number_of_phases; i++) {
318 printk(BIOS_SPEW, "Executing Phase %u of FspMultiPhaseMemInit\n", i);
319 /*
320 * Give SoC/mainboard a chance to perform any operation before
321 * Multi Phase Execution
322 */
323 platform_fsp_memory_multi_phase_init_cb(i);
324
325 multi_phase_params.multi_phase_action = EXECUTE_PHASE;
326 multi_phase_params.phase_index = i;
327 multi_phase_params.multi_phase_param_ptr = NULL;
328 status = fsp_multi_phase_init(&multi_phase_params);
329 fspm_return_value_handler("FspMultiPhaseMemInit Execute", status, false);
330 }
331
332 post_code(POSTCODE_FSP_MULTI_PHASE_MEM_INIT_EXIT);
333 timestamp_add_now(TS_FSP_MULTI_PHASE_MEM_INIT_END);
334 }
335
336 /**
337 * Checks for low battery during firmware update and initiates shutdown if necessary.
338 *
339 * This function checks if the system is in firmware update mode (indicated by
340 * a missing MRC cache) and if the battery is critically low. If both conditions
341 * are met, it initiates a shutdown to prevent interruption of the firmware
342 * update process.
343 */
344 static void handle_low_battery_during_firmware_update(bool *defer_shutdown, FSPM_UPD *fspm_upd)
345 {
346 if (!CONFIG(PLATFORM_HAS_EARLY_LOW_BATTERY_INDICATOR) ||
347 !platform_is_low_battery_shutdown_needed())
348 return;
349
350 if (CONFIG(MAINBOARD_HAS_EARLY_LIBGFXINIT)) {
351 platform_display_early_shutdown_notification(NULL);
352 /* User has been notified of low battery; safe to power off. */
353 do_low_battery_poweroff(); /* Do not return */
354 }
355
356 /* Defer shutdown until FSP-M (uGOP) display text message for user notification */
357 platform_display_early_shutdown_notification(fspm_upd);
358 *defer_shutdown = true;
359 }
360
361 static void do_fsp_memory_init(const struct fspm_context *context, bool s3wake)
362 {
363 efi_return_status_t status;
364 fsp_memory_init_fn fsp_raminit;
365 FSPM_UPD fspm_upd, *upd;
366 FSPM_ARCHx_UPD *arch_upd;
367 uint32_t version;
368 bool poweroff_after_fsp_execution = false;
369 const struct fsp_header *hdr = &context->header;
370 const struct memranges *memmap = &context->memmap;
371
372 post_code(POSTCODE_MEM_PREINIT_PREP_START);
373
374 if (CONFIG(MRC_CACHE_USING_MRC_VERSION))
375 version = fsp_mrc_version(hdr);
376 else
377 version = fsp_memory_settings_version(hdr);
378
379 upd = (FSPM_UPD *)(uintptr_t)(hdr->cfg_region_offset + hdr->image_base);
380
381 /*
382 * Verify UPD region size. We don't have malloc before ramstage, so we
383 * use a static buffer for the FSP-M UPDs which is sizeof(FSPM_UPD)
384 * bytes long, since that is the value known at compile time. If
385 * hdr->cfg_region_size is bigger than that, not all UPD defaults will
386 * be copied, so it'll contain random data at the end, so we just call
387 * die() in that case. If hdr->cfg_region_size is smaller than that,
388 * there's a mismatch between the FSP and the header, but since it will
389 * copy the full UPD defaults to the buffer, we try to continue and
390 * hope that there was no incompatible change in the UPDs.
391 */
392 if (hdr->cfg_region_size > sizeof(FSPM_UPD))
393 die("FSP-M UPD size is larger than FSPM_UPD struct size.\n");
394 if (hdr->cfg_region_size < sizeof(FSPM_UPD))
395 printk(BIOS_ERR, "FSP-M UPD size is smaller than FSPM_UPD struct size. "
396 "Check if the FSP binary matches the FSP headers.\n");
397
398 fsp_verify_upd_header_signature(upd->FspUpdHeader.Signature, FSPM_UPD_SIGNATURE);
399
400 /* Copy the default values from the UPD area */
401 memcpy(&fspm_upd, upd, sizeof(fspm_upd));
402
403 arch_upd = &fspm_upd.FspmArchUpd;
404
405 /* Reserve enough memory under TOLUD to save CBMEM header */
406 arch_upd->BootLoaderTolumSize = cbmem_overhead_size();
407
408 /* Fill common settings on behalf of chipset. */
409 if (fsp_fill_common_arch_params(arch_upd, s3wake, version,
410 memmap) != CB_SUCCESS)
411 die_with_post_code(POSTCODE_INVALID_VENDOR_BINARY,
412 "FSPM_ARCH_UPD not found!\n");
413
414 /* Early caching of RAMTOP region if valid mrc cache data is found */
415 #if (CONFIG(SOC_INTEL_COMMON_BASECODE_RAMTOP))
416 if (arch_upd->NvsBufferPtr)
417 early_ramtop_enable_cache_range();
418 #endif
419
420 /* Low battery check during firmware update */
421 if (!arch_upd->NvsBufferPtr)
422 handle_low_battery_during_firmware_update(&poweroff_after_fsp_execution,
423 &fspm_upd);
424
425 /* Give SoC and mainboard a chance to update the UPD */
426 platform_fsp_memory_init_params_cb(&fspm_upd, version);
427
428 /*
429 * For S3 resume case, if valid mrc cache data is not found or
430 * RECOVERY_MRC_CACHE hash verification fails, the S3 data
431 * pointer would be null and S3 resume fails with fsp-m
432 * returning error. Invoking a reset here saves time.
433 */
434 if (s3wake && !arch_upd->NvsBufferPtr)
435 /* FIXME: A "system" reset is likely enough: */
436 full_reset();
437
438 if (CONFIG(MMA))
439 setup_mma(&fspm_upd.FspmConfig);
440
441 post_code(POSTCODE_MEM_PREINIT_PREP_END);
442
443 /* Call FspMemoryInit */
444 fsp_raminit = (void *)(uintptr_t)(hdr->image_base + hdr->fsp_memory_init_entry_offset);
445 fsp_debug_before_memory_init(fsp_raminit, upd, &fspm_upd);
446
447 /* FSP disables the interrupt handler so remove debug exceptions temporarily */
448 null_breakpoint_remove();
449 stack_canary_breakpoint_remove();
450 post_code(POSTCODE_FSP_MEMORY_INIT);
451 timestamp_add_now(TS_FSP_MEMORY_INIT_START);
452 if (ENV_X86_64 && CONFIG(PLATFORM_USES_FSP2_X86_32))
453 status = protected_mode_call_2arg(fsp_raminit,
454 (uintptr_t)&fspm_upd,
455 (uintptr_t)fsp_get_hob_list_ptr());
456 else
457 status = fsp_raminit(&fspm_upd, fsp_get_hob_list_ptr());
458 null_breakpoint_init();
459 stack_canary_breakpoint_init();
460
461 /* User has been notified of low battery; safe to power off. */
462 if (poweroff_after_fsp_execution)
463 do_low_battery_poweroff();
464
465 post_code(POSTCODE_FSP_MEMORY_EXIT);
466 timestamp_add_now(TS_FSP_MEMORY_INIT_END);
467
468 /* Handle any errors returned by FspMemoryInit */
469 fspm_return_value_handler("FspMemoryInit", status, true);
470
471 if (CONFIG(PLATFORM_USES_FSP2_4))
472 fspm_multi_phase_init(hdr);
473
474 do_fsp_post_memory_init(s3wake, version);
475
476 /*
477 * fsp_debug_after_memory_init() checks whether the end of the tolum
478 * region is the same as the top of cbmem, so must be called here
479 * after cbmem has been initialised in do_fsp_post_memory_init().
480 */
481 fsp_debug_after_memory_init(status);
482 }
483
484 static void *fspm_allocator(void *arg, size_t size, const union cbfs_mdata *unused)
485 {
486 const struct fsp_load_descriptor *fspld = arg;
487 struct fspm_context *context = fspld->arg;
488 struct memranges *memmap = &context->memmap;
489
490 /* Non XIP FSP-M uses FSP-M address */
491 uintptr_t fspm_begin = (uintptr_t)CONFIG_FSP_M_ADDR;
492 uintptr_t fspm_end = fspm_begin + size;
493
494 if (check_region_overlap(memmap, "FSPM", fspm_begin, fspm_end) != CB_SUCCESS)
495 return NULL;
496
497 return (void *)fspm_begin;
498 }
499
500 void preload_fspm(void)
501 {
502 if (!CONFIG(CBFS_PRELOAD))
503 return;
504
505 const char *fspm_cbfs = soc_select_fsp_m_cbfs();
506 printk(BIOS_DEBUG, "Preloading %s\n", fspm_cbfs);
507 cbfs_preload(fspm_cbfs);
508 }
509
510 void fsp_memory_init(bool s3wake)
511 {
512 struct range_entry prog_ranges[2];
513 struct fspm_context context;
514 const char *fspm_cbfs = soc_select_fsp_m_cbfs();
515 struct fsp_load_descriptor fspld = {
516 .fsp_prog = PROG_INIT(PROG_REFCODE, fspm_cbfs),
517 .arg = &context,
518 };
519 struct fsp_header *hdr = &context.header;
520 struct memranges *memmap = &context.memmap;
521
522 /* For FSP-M XIP we leave alloc NULL to get a direct mapping to flash. */
523 if (!CONFIG(FSP_M_XIP))
524 fspld.alloc = fspm_allocator;
525
526 elog_boot_notify(s3wake);
527
528 /* Build up memory map of romstage address space including CAR. */
529 memranges_init_empty(memmap, &prog_ranges[0], ARRAY_SIZE(prog_ranges));
530 if (ENV_CACHE_AS_RAM)
531 memranges_insert(memmap, (uintptr_t)_car_region_start,
532 _car_unallocated_start - _car_region_start, 0);
533 memranges_insert(memmap, (uintptr_t)_program, REGION_SIZE(program), 0);
534
535 timestamp_add_now(TS_FSP_MEMORY_INIT_LOAD);
536 if (fsp_load_component(&fspld, hdr) != CB_SUCCESS)
537 die("FSPM not available or failed to load!\n");
538
539 if (CONFIG(FSP_M_XIP) && (uintptr_t)prog_start(&fspld.fsp_prog) != hdr->image_base)
540 die("FSPM XIP base does not match: %p vs %p\n",
541 (void *)(uintptr_t)hdr->image_base, prog_start(&fspld.fsp_prog));
542
543 timestamp_add_now(TS_INITRAM_START);
544
545 do_fsp_memory_init(&context, s3wake);
546
547 timestamp_add_now(TS_INITRAM_END);
548 }