Debian Codegraph @

src/arch/x86/postcar_loader.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <arch/romstage.h>
4 #include <cbmem.h>
5 #include <console/console.h>
6 #include <cpu/x86/msr.h>
7 #include <cpu/x86/mtrr.h>
8 #include <cpu/x86/smm.h>
9 #include <program_loading.h>
10 #include <reset.h>
11 #include <rmodule.h>
12 #include <romstage_handoff.h>
13 #include <security/vboot/vboot_common.h>
14 #include <stage_cache.h>
15 #include <timestamp.h>
16 #include <types.h>
17
18 static size_t var_mtrr_ctx_size(void)
19 {
20 int mtrr_count = get_var_mtrr_count();
21 return sizeof(struct var_mtrr_context) + mtrr_count * 2 * sizeof(msr_t);
22 }
23
24 static enum cb_err postcar_frame_init(struct postcar_frame *pcf)
25 {
26 memset(pcf, 0, sizeof(*pcf));
27
28 struct var_mtrr_context *ctx;
29
30 ctx = cbmem_add(CBMEM_ID_ROMSTAGE_RAM_STACK, var_mtrr_ctx_size());
31 if (ctx == NULL) {
32 printk(BIOS_ERR, "Couldn't add var_mtrr_ctx setup in cbmem.\n");
33 return CB_ERR;
34 }
35
36 pcf->mtrr = ctx;
37 var_mtrr_context_init(pcf->mtrr);
38
39 return CB_SUCCESS;
40 }
41
42 void postcar_frame_add_mtrr(struct postcar_frame *pcf,
43 uintptr_t addr, size_t size, int type)
44 {
45 var_mtrr_set(pcf->mtrr, addr, size, type);
46 }
47
48 void postcar_frame_add_romcache(struct postcar_frame *pcf, int type)
49 {
50 if (!CONFIG(BOOT_DEVICE_MEMORY_MAPPED))
51 return;
52 postcar_frame_add_mtrr(pcf, CACHE_ROM_BASE, CACHE_ROM_SIZE, type);
53 }
54
55 static void postcar_frame_common_mtrrs(struct postcar_frame *pcf)
56 {
57 if (pcf->skip_common_mtrr)
58 return;
59
60 /* Cache the ROM as WP just below 4GiB. */
61 postcar_frame_add_romcache(pcf, MTRR_TYPE_WRPROT);
62 }
63
64 static void run_postcar_phase(struct postcar_frame *pcf);
65
66 /* prepare_and_run_postcar() determines the stack to use after
67 * cache-as-ram is torn down as well as the MTRR settings to use. */
68 void __noreturn prepare_and_run_postcar(void)
69 {
70 struct postcar_frame pcf;
71
72 if (postcar_frame_init(&pcf))
73 die("Unable to initialize postcar frame.\n");
74
75 fill_postcar_frame(&pcf);
76
77 postcar_frame_common_mtrrs(&pcf);
78
79 run_postcar_phase(&pcf);
80 /* We do not return here. */
81 die("Failed to load postcar\n!");
82 }
83
84 static void finalize_load(uintptr_t *reloc_params, uintptr_t mtrr_frame_ptr)
85 {
86 /*
87 * set postcar(rmodule)'s parameter.
88 * the parameter is defined as post_car_mtrrs in cpu/x86/mtrr/earlymtrr.c
89 */
90 *reloc_params = mtrr_frame_ptr;
91
92 /*
93 * Signal to rest of system that another update was made to the
94 * postcar program prior to running it.
95 */
96 prog_segment_loaded((uintptr_t)reloc_params, sizeof(uintptr_t), SEG_FINAL);
97 prog_segment_loaded((uintptr_t)mtrr_frame_ptr, var_mtrr_ctx_size(), SEG_FINAL);
98 }
99
100 static void load_postcar_cbfs(struct prog *prog, struct postcar_frame *pcf)
101 {
102 struct rmod_stage_load rsl = {
103 .cbmem_id = CBMEM_ID_AFTER_CAR,
104 .prog = prog,
105 };
106
107 if (rmodule_stage_load(&rsl))
108 die_with_post_code(POSTCODE_INVALID_ROM,
109 "Failed to load after CAR program.\n");
110
111 /* Set the stack pointer within parameters of the program loaded. */
112 if (rsl.params == NULL)
113 die_with_post_code(POSTCODE_INVALID_ROM,
114 "No parameters found in after CAR program.\n");
115
116 finalize_load(rsl.params, (uintptr_t)pcf->mtrr);
117
118 stage_cache_add(STAGE_POSTCAR, prog);
119 }
120
121 /*
122 * Cache the TSEG region at the top of ram. This region is
123 * not restricted to SMM mode until SMM has been relocated.
124 * By setting the region to cacheable it provides faster access
125 * when relocating the SMM handler as well as using the TSEG
126 * region for other purposes.
127 */
128 void postcar_enable_tseg_cache(struct postcar_frame *pcf)
129 {
130 uintptr_t smm_base;
131 size_t smm_size;
132
133 smm_region(&smm_base, &smm_size);
134 postcar_frame_add_mtrr(pcf, smm_base, smm_size,
135 MTRR_TYPE_WRBACK);
136 }
137
138 static void postcar_cache_invalid(void)
139 {
140 printk(BIOS_ERR, "postcar cache invalid.\n");
141 board_reset();
142 }
143
144 /*
145 * POSTCAR will call invd so don't make assumptions on cbmem
146 * and external stage cache being UC.
147 */
148 static void postcar_flush_cache(void)
149 {
150 uintptr_t cbmem_base;
151 size_t cbmem_size;
152 uintptr_t stage_cache_base;
153 size_t stage_cache_size;
154
155 if (cbmem_get_region((void **)&cbmem_base, &cbmem_size))
156 die("Could not find cbmem region");
157 prog_segment_loaded(cbmem_base, cbmem_size, SEG_FINAL);
158 if (CONFIG(TSEG_STAGE_CACHE) && !romstage_handoff_is_resume()) {
159 stage_cache_external_region((void **)&stage_cache_base, &stage_cache_size);
160 prog_segment_loaded(stage_cache_base, stage_cache_size, SEG_FINAL);
161 }
162 }
163
164 static void run_postcar_phase(struct postcar_frame *pcf)
165 {
166 struct prog prog =
167 PROG_INIT(PROG_POSTCAR, CONFIG_CBFS_PREFIX "/postcar");
168
169 vboot_run_logic();
170
171 if (resume_from_stage_cache()) {
172 stage_cache_load_stage(STAGE_POSTCAR, &prog);
173 /* This is here to allow platforms to pass different stack
174 parameters between S3 resume and normal boot. On the
175 platforms where the values are the same it's a nop. */
176 finalize_load(prog.arg, (uintptr_t)pcf->mtrr);
177
178 if (prog_entry(&prog) == NULL)
179 postcar_cache_invalid();
180 } else
181 load_postcar_cbfs(&prog, pcf);
182
183 /* As postcar exist, it's end of romstage here */
184 timestamp_add_now(TS_ROMSTAGE_END);
185
186 console_time_report();
187
188 postcar_flush_cache();
189
190 prog_set_arg(&prog, (void *)cbmem_top());
191
192 prog_run(&prog);
193 }