blob: cae5574fd3c0d334c1e3861055b56c9f8420d6d4 [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
16static inline void stack_push(struct postcar_frame *pcf, uint32_t val)
17{
18 uint32_t *ptr;
19
20 pcf->stack -= sizeof(val);
21 ptr = (void *)pcf->stack;
22 *ptr = val;
23}
24
Kyösti Mälkki029cebc2016-12-02 19:47:07 +020025static void postcar_frame_prepare(struct postcar_frame *pcf)
26{
Aaron Durbine1bf0652020-05-28 21:34:43 -060027 var_mtrr_context_init(&pcf->ctx, pcf);
Kyösti Mälkki029cebc2016-12-02 19:47:07 +020028}
29
Aaron Durbin7f8afe02016-03-18 12:21:23 -050030int postcar_frame_init(struct postcar_frame *pcf, size_t stack_size)
31{
32 void *stack;
Aaron Durbin7f8afe02016-03-18 12:21:23 -050033
Kyösti Mälkki6e2d0c12019-06-28 10:08:51 +030034 /*
35 * Use default postcar stack size of 4 KiB. This value should
36 * not be decreased, because if mainboards use vboot, 1 KiB will
37 * not be enough anymore.
38 */
39
40 if (stack_size == 0)
41 stack_size = 4 * KiB;
42
Aaron Durbin7f8afe02016-03-18 12:21:23 -050043 stack = cbmem_add(CBMEM_ID_ROMSTAGE_RAM_STACK, stack_size);
44 if (stack == NULL) {
45 printk(BIOS_ERR, "Couldn't add %zd byte stack in cbmem.\n",
46 stack_size);
47 return -1;
48 }
49
Kyösti Mälkki029cebc2016-12-02 19:47:07 +020050 postcar_frame_prepare(pcf);
Aaron Durbin7f8afe02016-03-18 12:21:23 -050051 pcf->stack = (uintptr_t)stack;
52 pcf->stack += stack_size;
Aaron Durbin7f8afe02016-03-18 12:21:23 -050053 return 0;
54}
55
Aaron Durbine1bf0652020-05-28 21:34:43 -060056static void postcar_var_mtrr_set(const struct var_mtrr_context *ctx,
57 uintptr_t addr, size_t size,
58 msr_t base, msr_t mask)
59{
60 struct postcar_frame *pcf = ctx->arg;
61
62 printk(BIOS_DEBUG, "MTRR Range: Start=%lx End=%lx (Size %zx)\n",
63 addr, addr + size, size);
64
65 stack_push(pcf, mask.hi);
66 stack_push(pcf, mask.lo);
67 stack_push(pcf, base.hi);
68 stack_push(pcf, base.lo);
69}
70
Aaron Durbin7f8afe02016-03-18 12:21:23 -050071void postcar_frame_add_mtrr(struct postcar_frame *pcf,
72 uintptr_t addr, size_t size, int type)
73{
Aaron Durbine1bf0652020-05-28 21:34:43 -060074 var_mtrr_set_with_cb(&pcf->ctx, addr, size, type, postcar_var_mtrr_set);
Aaron Durbin7f8afe02016-03-18 12:21:23 -050075}
76
Nico Huber36ec3e92018-05-27 14:32:27 +020077void postcar_frame_add_romcache(struct postcar_frame *pcf, int type)
78{
Julius Wernercd49cce2019-03-05 16:53:33 -080079 if (!CONFIG(BOOT_DEVICE_MEMORY_MAPPED))
Nico Huber36ec3e92018-05-27 14:32:27 +020080 return;
81 postcar_frame_add_mtrr(pcf, CACHE_ROM_BASE, CACHE_ROM_SIZE, type);
82}
83
Aaron Durbine8936742020-04-29 14:20:05 -060084static void postcar_frame_common_mtrrs(struct postcar_frame *pcf)
Kyösti Mälkki544878b2019-08-09 11:41:15 +030085{
86 if (pcf->skip_common_mtrr)
87 return;
88
Kyösti Mälkki544878b2019-08-09 11:41:15 +030089 /* Cache the ROM as WP just below 4GiB. */
90 postcar_frame_add_romcache(pcf, MTRR_TYPE_WRPROT);
91}
92
Kyösti Mälkkicd7a70f2019-08-17 20:51:08 +030093/* prepare_and_run_postcar() determines the stack to use after
94 * cache-as-ram is torn down as well as the MTRR settings to use. */
95void prepare_and_run_postcar(struct postcar_frame *pcf)
96{
97 if (postcar_frame_init(pcf, 0))
98 die("Unable to initialize postcar frame.\n");
99
100 fill_postcar_frame(pcf);
101
102 postcar_frame_common_mtrrs(pcf);
103
104 run_postcar_phase(pcf);
105 /* We do not return here. */
106}
107
Aaron Durbindf2bfb92019-08-27 20:22:40 -0600108static void postcar_commit_mtrrs(struct postcar_frame *pcf)
Rizwan Qureshib1b44d32016-08-26 21:08:50 +0530109{
110 /*
111 * Place the number of used variable MTRRs on stack then max number
112 * of variable MTRRs supported in the system.
113 */
Aaron Durbine1bf0652020-05-28 21:34:43 -0600114 stack_push(pcf, pcf->ctx.used_var_mtrrs);
115 stack_push(pcf, pcf->ctx.max_var_mtrrs);
Rizwan Qureshib1b44d32016-08-26 21:08:50 +0530116}
117
Kyösti Mälkkia7421fb2017-09-05 22:43:05 +0300118static void finalize_load(uintptr_t *stack_top_ptr, uintptr_t stack_top)
119{
120 *stack_top_ptr = stack_top;
121 /*
122 * Signal to rest of system that another update was made to the
123 * postcar program prior to running it.
124 */
125 prog_segment_loaded((uintptr_t)stack_top_ptr, sizeof(uintptr_t),
126 SEG_FINAL);
127}
128
Aaron Durbind0084132016-11-29 15:52:08 -0600129static void load_postcar_cbfs(struct prog *prog, struct postcar_frame *pcf)
Aaron Durbin7f8afe02016-03-18 12:21:23 -0500130{
Aaron Durbin7f8afe02016-03-18 12:21:23 -0500131 struct rmod_stage_load rsl = {
132 .cbmem_id = CBMEM_ID_AFTER_CAR,
Aaron Durbind0084132016-11-29 15:52:08 -0600133 .prog = prog,
Aaron Durbin7f8afe02016-03-18 12:21:23 -0500134 };
135
Wim Vervoorn1058dd82019-11-01 10:22:22 +0100136 vboot_run_logic();
137
Aaron Durbind0084132016-11-29 15:52:08 -0600138 if (prog_locate(prog))
Keith Short70064582019-05-06 16:12:57 -0600139 die_with_post_code(POST_INVALID_ROM,
140 "Failed to locate after CAR program.\n");
Aaron Durbin7f8afe02016-03-18 12:21:23 -0500141 if (rmodule_stage_load(&rsl))
Keith Short70064582019-05-06 16:12:57 -0600142 die_with_post_code(POST_INVALID_ROM,
143 "Failed to load after CAR program.\n");
Aaron Durbin7f8afe02016-03-18 12:21:23 -0500144
145 /* Set the stack pointer within parameters of the program loaded. */
146 if (rsl.params == NULL)
Keith Short70064582019-05-06 16:12:57 -0600147 die_with_post_code(POST_INVALID_ROM,
148 "No parameters found in after CAR program.\n");
Aaron Durbin7f8afe02016-03-18 12:21:23 -0500149
Kyösti Mälkkia7421fb2017-09-05 22:43:05 +0300150 finalize_load(rsl.params, pcf->stack);
Aaron Durbindd95e002016-03-31 13:36:33 -0500151
Subrata Banik90f750b2019-06-11 17:52:06 +0530152 stage_cache_add(STAGE_POSTCAR, prog);
Aaron Durbind0084132016-11-29 15:52:08 -0600153}
154
Subrata Banik8edc6dc2019-08-22 11:30:52 +0530155/*
156 * Cache the TSEG region at the top of ram. This region is
157 * not restricted to SMM mode until SMM has been relocated.
158 * By setting the region to cacheable it provides faster access
159 * when relocating the SMM handler as well as using the TSEG
160 * region for other purposes.
161 */
162void postcar_enable_tseg_cache(struct postcar_frame *pcf)
163{
164 uintptr_t smm_base;
165 size_t smm_size;
166
167 smm_region(&smm_base, &smm_size);
168 postcar_frame_add_mtrr(pcf, smm_base, smm_size,
169 MTRR_TYPE_WRBACK);
170}
171
Kyösti Mälkki4f14cd82019-12-18 19:40:48 +0200172static void postcar_cache_invalid(void)
173{
174 printk(BIOS_ERR, "postcar cache invalid.\n");
175 board_reset();
176}
177
Aaron Durbind0084132016-11-29 15:52:08 -0600178void run_postcar_phase(struct postcar_frame *pcf)
179{
180 struct prog prog =
Philipp Deppenwiese01797b12018-11-08 10:39:39 +0100181 PROG_INIT(PROG_POSTCAR, CONFIG_CBFS_PREFIX "/postcar");
Aaron Durbind0084132016-11-29 15:52:08 -0600182
183 postcar_commit_mtrrs(pcf);
184
Kyösti Mälkkie0165fb2021-01-09 13:30:57 +0200185 if (resume_from_stage_cache()) {
Aaron Durbind0084132016-11-29 15:52:08 -0600186 stage_cache_load_stage(STAGE_POSTCAR, &prog);
Aaron Durbinc546c762018-04-23 14:55:09 -0600187 /* This is here to allow platforms to pass different stack
188 parameters between S3 resume and normal boot. On the
189 platforms where the values are the same it's a nop. */
Kyösti Mälkkia7421fb2017-09-05 22:43:05 +0300190 finalize_load(prog.arg, pcf->stack);
Kyösti Mälkki4f14cd82019-12-18 19:40:48 +0200191
192 if (prog_entry(&prog) == NULL)
193 postcar_cache_invalid();
Kyösti Mälkkia7421fb2017-09-05 22:43:05 +0300194 } else
Aaron Durbind0084132016-11-29 15:52:08 -0600195 load_postcar_cbfs(&prog, pcf);
196
Subrata Banik2847e1e2019-02-25 20:31:22 +0530197 /* As postcar exist, it's end of romstage here */
198 timestamp_add_now(TS_END_ROMSTAGE);
199
Kyösti Mälkki45ddb432019-11-02 14:12:18 +0200200 console_time_report();
201
Arthur Heymans5331a7c2019-10-23 17:07:15 +0200202 prog_set_arg(&prog, cbmem_top());
203
Aaron Durbin7f8afe02016-03-18 12:21:23 -0500204 prog_run(&prog);
205}