blob: b5bfe3e6c96045e0fcc734c8e2c291e0905dc414 [file] [log] [blame]
Patrick Georgi11f00792020-03-04 15:10:45 +01001/* SPDX-License-Identifier: GPL-2.0-only */
Aaron Durbin7f8afe02016-03-18 12:21:23 -05002
Kyösti Mälkkia963acd2019-08-16 20:34:25 +03003#include <arch/romstage.h>
Aaron Durbin7f8afe02016-03-18 12:21:23 -05004#include <cbmem.h>
5#include <console/console.h>
6#include <cpu/x86/msr.h>
7#include <cpu/x86/mtrr.h>
Subrata Banik8edc6dc2019-08-22 11:30:52 +05308#include <cpu/x86/smm.h>
Aaron Durbin7f8afe02016-03-18 12:21:23 -05009#include <program_loading.h>
Kyösti Mälkki4f14cd82019-12-18 19:40:48 +020010#include <reset.h>
Aaron Durbin7f8afe02016-03-18 12:21:23 -050011#include <rmodule.h>
Aaron Durbind0084132016-11-29 15:52:08 -060012#include <stage_cache.h>
Subrata Banik2847e1e2019-02-25 20:31:22 +053013#include <timestamp.h>
Wim Vervoorn1058dd82019-11-01 10:22:22 +010014#include <security/vboot/vboot_common.h>
Aaron Durbin7f8afe02016-03-18 12:21:23 -050015
Arthur Heymans46b409d2021-05-14 13:19:43 +020016static size_t var_mtrr_ctx_size(void)
Aaron Durbin7f8afe02016-03-18 12:21:23 -050017{
Arthur Heymans46b409d2021-05-14 13:19:43 +020018 int mtrr_count = get_var_mtrr_count();
19 return sizeof(struct var_mtrr_context) + mtrr_count * 2 * sizeof(msr_t);
Aaron Durbin7f8afe02016-03-18 12:21:23 -050020}
21
Arthur Heymansba00d102022-02-15 11:04:15 +010022static int postcar_frame_init(struct postcar_frame *pcf)
Kyösti Mälkki029cebc2016-12-02 19:47:07 +020023{
Arthur Heymans46b409d2021-05-14 13:19:43 +020024 struct var_mtrr_context *ctx;
Kyösti Mälkki029cebc2016-12-02 19:47:07 +020025
Arthur Heymans46b409d2021-05-14 13:19:43 +020026 ctx = cbmem_add(CBMEM_ID_ROMSTAGE_RAM_STACK, var_mtrr_ctx_size());
27 if (ctx == NULL) {
28 printk(BIOS_ERR, "Couldn't add var_mtrr_ctx setup in cbmem.\n");
Aaron Durbin7f8afe02016-03-18 12:21:23 -050029 return -1;
30 }
31
Arthur Heymans46b409d2021-05-14 13:19:43 +020032 pcf->mtrr = ctx;
33 var_mtrr_context_init(pcf->mtrr);
34
Aaron Durbin7f8afe02016-03-18 12:21:23 -050035 return 0;
36}
37
38void postcar_frame_add_mtrr(struct postcar_frame *pcf,
39 uintptr_t addr, size_t size, int type)
40{
Arthur Heymans46b409d2021-05-14 13:19:43 +020041 var_mtrr_set(pcf->mtrr, addr, size, type);
Aaron Durbin7f8afe02016-03-18 12:21:23 -050042}
43
Nico Huber36ec3e92018-05-27 14:32:27 +020044void postcar_frame_add_romcache(struct postcar_frame *pcf, int type)
45{
Julius Wernercd49cce2019-03-05 16:53:33 -080046 if (!CONFIG(BOOT_DEVICE_MEMORY_MAPPED))
Nico Huber36ec3e92018-05-27 14:32:27 +020047 return;
48 postcar_frame_add_mtrr(pcf, CACHE_ROM_BASE, CACHE_ROM_SIZE, type);
49}
50
Aaron Durbine8936742020-04-29 14:20:05 -060051static void postcar_frame_common_mtrrs(struct postcar_frame *pcf)
Kyösti Mälkki544878b2019-08-09 11:41:15 +030052{
53 if (pcf->skip_common_mtrr)
54 return;
55
Kyösti Mälkki544878b2019-08-09 11:41:15 +030056 /* Cache the ROM as WP just below 4GiB. */
57 postcar_frame_add_romcache(pcf, MTRR_TYPE_WRPROT);
58}
59
Arthur Heymansba00d102022-02-15 11:04:15 +010060static void run_postcar_phase(struct postcar_frame *pcf);
61
Kyösti Mälkkicd7a70f2019-08-17 20:51:08 +030062/* prepare_and_run_postcar() determines the stack to use after
63 * cache-as-ram is torn down as well as the MTRR settings to use. */
64void prepare_and_run_postcar(struct postcar_frame *pcf)
65{
Arthur Heymans46b409d2021-05-14 13:19:43 +020066 if (postcar_frame_init(pcf))
Kyösti Mälkkicd7a70f2019-08-17 20:51:08 +030067 die("Unable to initialize postcar frame.\n");
68
69 fill_postcar_frame(pcf);
70
71 postcar_frame_common_mtrrs(pcf);
72
73 run_postcar_phase(pcf);
74 /* We do not return here. */
75}
76
Arthur Heymans46b409d2021-05-14 13:19:43 +020077static void finalize_load(uintptr_t *reloc_params, uintptr_t mtrr_frame_ptr)
Rizwan Qureshib1b44d32016-08-26 21:08:50 +053078{
Arthur Heymans46b409d2021-05-14 13:19:43 +020079 *reloc_params = mtrr_frame_ptr;
Kyösti Mälkkia7421fb2017-09-05 22:43:05 +030080 /*
81 * Signal to rest of system that another update was made to the
82 * postcar program prior to running it.
83 */
Arthur Heymans46b409d2021-05-14 13:19:43 +020084 prog_segment_loaded((uintptr_t)reloc_params, sizeof(uintptr_t), SEG_FINAL);
85 prog_segment_loaded((uintptr_t)mtrr_frame_ptr, var_mtrr_ctx_size(), SEG_FINAL);
Kyösti Mälkkia7421fb2017-09-05 22:43:05 +030086}
87
Aaron Durbind0084132016-11-29 15:52:08 -060088static void load_postcar_cbfs(struct prog *prog, struct postcar_frame *pcf)
Aaron Durbin7f8afe02016-03-18 12:21:23 -050089{
Aaron Durbin7f8afe02016-03-18 12:21:23 -050090 struct rmod_stage_load rsl = {
91 .cbmem_id = CBMEM_ID_AFTER_CAR,
Aaron Durbind0084132016-11-29 15:52:08 -060092 .prog = prog,
Aaron Durbin7f8afe02016-03-18 12:21:23 -050093 };
94
Wim Vervoorn1058dd82019-11-01 10:22:22 +010095 vboot_run_logic();
96
Aaron Durbin7f8afe02016-03-18 12:21:23 -050097 if (rmodule_stage_load(&rsl))
Keith Short70064582019-05-06 16:12:57 -060098 die_with_post_code(POST_INVALID_ROM,
99 "Failed to load after CAR program.\n");
Aaron Durbin7f8afe02016-03-18 12:21:23 -0500100
101 /* Set the stack pointer within parameters of the program loaded. */
102 if (rsl.params == NULL)
Keith Short70064582019-05-06 16:12:57 -0600103 die_with_post_code(POST_INVALID_ROM,
104 "No parameters found in after CAR program.\n");
Aaron Durbin7f8afe02016-03-18 12:21:23 -0500105
Arthur Heymans46b409d2021-05-14 13:19:43 +0200106 finalize_load(rsl.params, (uintptr_t)pcf->mtrr);
Aaron Durbindd95e002016-03-31 13:36:33 -0500107
Subrata Banik90f750b2019-06-11 17:52:06 +0530108 stage_cache_add(STAGE_POSTCAR, prog);
Aaron Durbind0084132016-11-29 15:52:08 -0600109}
110
Subrata Banik8edc6dc2019-08-22 11:30:52 +0530111/*
112 * Cache the TSEG region at the top of ram. This region is
113 * not restricted to SMM mode until SMM has been relocated.
114 * By setting the region to cacheable it provides faster access
115 * when relocating the SMM handler as well as using the TSEG
116 * region for other purposes.
117 */
118void postcar_enable_tseg_cache(struct postcar_frame *pcf)
119{
120 uintptr_t smm_base;
121 size_t smm_size;
122
123 smm_region(&smm_base, &smm_size);
124 postcar_frame_add_mtrr(pcf, smm_base, smm_size,
125 MTRR_TYPE_WRBACK);
126}
127
Kyösti Mälkki4f14cd82019-12-18 19:40:48 +0200128static void postcar_cache_invalid(void)
129{
130 printk(BIOS_ERR, "postcar cache invalid.\n");
131 board_reset();
132}
133
Arthur Heymansba00d102022-02-15 11:04:15 +0100134static void run_postcar_phase(struct postcar_frame *pcf)
Aaron Durbind0084132016-11-29 15:52:08 -0600135{
136 struct prog prog =
Philipp Deppenwiese01797b12018-11-08 10:39:39 +0100137 PROG_INIT(PROG_POSTCAR, CONFIG_CBFS_PREFIX "/postcar");
Aaron Durbind0084132016-11-29 15:52:08 -0600138
Kyösti Mälkkie0165fb2021-01-09 13:30:57 +0200139 if (resume_from_stage_cache()) {
Aaron Durbind0084132016-11-29 15:52:08 -0600140 stage_cache_load_stage(STAGE_POSTCAR, &prog);
Aaron Durbinc546c762018-04-23 14:55:09 -0600141 /* This is here to allow platforms to pass different stack
142 parameters between S3 resume and normal boot. On the
143 platforms where the values are the same it's a nop. */
Arthur Heymans46b409d2021-05-14 13:19:43 +0200144 finalize_load(prog.arg, (uintptr_t)pcf->mtrr);
Kyösti Mälkki4f14cd82019-12-18 19:40:48 +0200145
146 if (prog_entry(&prog) == NULL)
147 postcar_cache_invalid();
Kyösti Mälkkia7421fb2017-09-05 22:43:05 +0300148 } else
Aaron Durbind0084132016-11-29 15:52:08 -0600149 load_postcar_cbfs(&prog, pcf);
150
Subrata Banik2847e1e2019-02-25 20:31:22 +0530151 /* As postcar exist, it's end of romstage here */
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100152 timestamp_add_now(TS_ROMSTAGE_END);
Subrata Banik2847e1e2019-02-25 20:31:22 +0530153
Kyösti Mälkki45ddb432019-11-02 14:12:18 +0200154 console_time_report();
155
Arthur Heymans5331a7c2019-10-23 17:07:15 +0200156 prog_set_arg(&prog, cbmem_top());
157
Aaron Durbin7f8afe02016-03-18 12:21:23 -0500158 prog_run(&prog);
159}