Patrick Georgi | 11f0079 | 2020-03-04 15:10:45 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 2 | |
Kyösti Mälkki | a963acd | 2019-08-16 20:34:25 +0300 | [diff] [blame] | 3 | #include <arch/romstage.h> |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 4 | #include <cbmem.h> |
| 5 | #include <console/console.h> |
| 6 | #include <cpu/x86/msr.h> |
| 7 | #include <cpu/x86/mtrr.h> |
Subrata Banik | 8edc6dc | 2019-08-22 11:30:52 +0530 | [diff] [blame] | 8 | #include <cpu/x86/smm.h> |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 9 | #include <program_loading.h> |
Kyösti Mälkki | 4f14cd8 | 2019-12-18 19:40:48 +0200 | [diff] [blame] | 10 | #include <reset.h> |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 11 | #include <rmodule.h> |
Aaron Durbin | d008413 | 2016-11-29 15:52:08 -0600 | [diff] [blame] | 12 | #include <romstage_handoff.h> |
| 13 | #include <stage_cache.h> |
Subrata Banik | 2847e1e | 2019-02-25 20:31:22 +0530 | [diff] [blame] | 14 | #include <timestamp.h> |
Wim Vervoorn | 1058dd8 | 2019-11-01 10:22:22 +0100 | [diff] [blame] | 15 | #include <security/vboot/vboot_common.h> |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 16 | |
| 17 | static inline void stack_push(struct postcar_frame *pcf, uint32_t val) |
| 18 | { |
| 19 | uint32_t *ptr; |
| 20 | |
| 21 | pcf->stack -= sizeof(val); |
| 22 | ptr = (void *)pcf->stack; |
| 23 | *ptr = val; |
| 24 | } |
| 25 | |
Kyösti Mälkki | 029cebc | 2016-12-02 19:47:07 +0200 | [diff] [blame] | 26 | static void postcar_frame_prepare(struct postcar_frame *pcf) |
| 27 | { |
Aaron Durbin | e1bf065 | 2020-05-28 21:34:43 -0600 | [diff] [blame] | 28 | var_mtrr_context_init(&pcf->ctx, pcf); |
Kyösti Mälkki | 029cebc | 2016-12-02 19:47:07 +0200 | [diff] [blame] | 29 | } |
| 30 | |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 31 | int postcar_frame_init(struct postcar_frame *pcf, size_t stack_size) |
| 32 | { |
| 33 | void *stack; |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 34 | |
Kyösti Mälkki | 6e2d0c1 | 2019-06-28 10:08:51 +0300 | [diff] [blame] | 35 | /* |
| 36 | * Use default postcar stack size of 4 KiB. This value should |
| 37 | * not be decreased, because if mainboards use vboot, 1 KiB will |
| 38 | * not be enough anymore. |
| 39 | */ |
| 40 | |
| 41 | if (stack_size == 0) |
| 42 | stack_size = 4 * KiB; |
| 43 | |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 44 | stack = cbmem_add(CBMEM_ID_ROMSTAGE_RAM_STACK, stack_size); |
| 45 | if (stack == NULL) { |
| 46 | printk(BIOS_ERR, "Couldn't add %zd byte stack in cbmem.\n", |
| 47 | stack_size); |
| 48 | return -1; |
| 49 | } |
| 50 | |
Kyösti Mälkki | 029cebc | 2016-12-02 19:47:07 +0200 | [diff] [blame] | 51 | postcar_frame_prepare(pcf); |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 52 | pcf->stack = (uintptr_t)stack; |
| 53 | pcf->stack += stack_size; |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 54 | return 0; |
| 55 | } |
| 56 | |
Aaron Durbin | e1bf065 | 2020-05-28 21:34:43 -0600 | [diff] [blame] | 57 | static void postcar_var_mtrr_set(const struct var_mtrr_context *ctx, |
| 58 | uintptr_t addr, size_t size, |
| 59 | msr_t base, msr_t mask) |
| 60 | { |
| 61 | struct postcar_frame *pcf = ctx->arg; |
| 62 | |
| 63 | printk(BIOS_DEBUG, "MTRR Range: Start=%lx End=%lx (Size %zx)\n", |
| 64 | addr, addr + size, size); |
| 65 | |
| 66 | stack_push(pcf, mask.hi); |
| 67 | stack_push(pcf, mask.lo); |
| 68 | stack_push(pcf, base.hi); |
| 69 | stack_push(pcf, base.lo); |
| 70 | } |
| 71 | |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 72 | void postcar_frame_add_mtrr(struct postcar_frame *pcf, |
| 73 | uintptr_t addr, size_t size, int type) |
| 74 | { |
Aaron Durbin | e1bf065 | 2020-05-28 21:34:43 -0600 | [diff] [blame] | 75 | var_mtrr_set_with_cb(&pcf->ctx, addr, size, type, postcar_var_mtrr_set); |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 76 | } |
| 77 | |
Nico Huber | 36ec3e9 | 2018-05-27 14:32:27 +0200 | [diff] [blame] | 78 | void postcar_frame_add_romcache(struct postcar_frame *pcf, int type) |
| 79 | { |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 80 | if (!CONFIG(BOOT_DEVICE_MEMORY_MAPPED)) |
Nico Huber | 36ec3e9 | 2018-05-27 14:32:27 +0200 | [diff] [blame] | 81 | return; |
| 82 | postcar_frame_add_mtrr(pcf, CACHE_ROM_BASE, CACHE_ROM_SIZE, type); |
| 83 | } |
| 84 | |
Aaron Durbin | e893674 | 2020-04-29 14:20:05 -0600 | [diff] [blame] | 85 | static void postcar_frame_common_mtrrs(struct postcar_frame *pcf) |
Kyösti Mälkki | 544878b | 2019-08-09 11:41:15 +0300 | [diff] [blame] | 86 | { |
| 87 | if (pcf->skip_common_mtrr) |
| 88 | return; |
| 89 | |
Kyösti Mälkki | 544878b | 2019-08-09 11:41:15 +0300 | [diff] [blame] | 90 | /* Cache the ROM as WP just below 4GiB. */ |
| 91 | postcar_frame_add_romcache(pcf, MTRR_TYPE_WRPROT); |
| 92 | } |
| 93 | |
Kyösti Mälkki | cd7a70f | 2019-08-17 20:51:08 +0300 | [diff] [blame] | 94 | /* prepare_and_run_postcar() determines the stack to use after |
| 95 | * cache-as-ram is torn down as well as the MTRR settings to use. */ |
| 96 | void prepare_and_run_postcar(struct postcar_frame *pcf) |
| 97 | { |
| 98 | if (postcar_frame_init(pcf, 0)) |
| 99 | die("Unable to initialize postcar frame.\n"); |
| 100 | |
| 101 | fill_postcar_frame(pcf); |
| 102 | |
| 103 | postcar_frame_common_mtrrs(pcf); |
| 104 | |
| 105 | run_postcar_phase(pcf); |
| 106 | /* We do not return here. */ |
| 107 | } |
| 108 | |
Aaron Durbin | df2bfb9 | 2019-08-27 20:22:40 -0600 | [diff] [blame] | 109 | static void postcar_commit_mtrrs(struct postcar_frame *pcf) |
Rizwan Qureshi | b1b44d3 | 2016-08-26 21:08:50 +0530 | [diff] [blame] | 110 | { |
| 111 | /* |
| 112 | * Place the number of used variable MTRRs on stack then max number |
| 113 | * of variable MTRRs supported in the system. |
| 114 | */ |
Aaron Durbin | e1bf065 | 2020-05-28 21:34:43 -0600 | [diff] [blame] | 115 | stack_push(pcf, pcf->ctx.used_var_mtrrs); |
| 116 | stack_push(pcf, pcf->ctx.max_var_mtrrs); |
Rizwan Qureshi | b1b44d3 | 2016-08-26 21:08:50 +0530 | [diff] [blame] | 117 | } |
| 118 | |
Kyösti Mälkki | a7421fb | 2017-09-05 22:43:05 +0300 | [diff] [blame] | 119 | static void finalize_load(uintptr_t *stack_top_ptr, uintptr_t stack_top) |
| 120 | { |
| 121 | *stack_top_ptr = stack_top; |
| 122 | /* |
| 123 | * Signal to rest of system that another update was made to the |
| 124 | * postcar program prior to running it. |
| 125 | */ |
| 126 | prog_segment_loaded((uintptr_t)stack_top_ptr, sizeof(uintptr_t), |
| 127 | SEG_FINAL); |
| 128 | } |
| 129 | |
Aaron Durbin | d008413 | 2016-11-29 15:52:08 -0600 | [diff] [blame] | 130 | static void load_postcar_cbfs(struct prog *prog, struct postcar_frame *pcf) |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 131 | { |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 132 | struct rmod_stage_load rsl = { |
| 133 | .cbmem_id = CBMEM_ID_AFTER_CAR, |
Aaron Durbin | d008413 | 2016-11-29 15:52:08 -0600 | [diff] [blame] | 134 | .prog = prog, |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 135 | }; |
| 136 | |
Wim Vervoorn | 1058dd8 | 2019-11-01 10:22:22 +0100 | [diff] [blame] | 137 | vboot_run_logic(); |
| 138 | |
Aaron Durbin | d008413 | 2016-11-29 15:52:08 -0600 | [diff] [blame] | 139 | if (prog_locate(prog)) |
Keith Short | 7006458 | 2019-05-06 16:12:57 -0600 | [diff] [blame] | 140 | die_with_post_code(POST_INVALID_ROM, |
| 141 | "Failed to locate after CAR program.\n"); |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 142 | if (rmodule_stage_load(&rsl)) |
Keith Short | 7006458 | 2019-05-06 16:12:57 -0600 | [diff] [blame] | 143 | die_with_post_code(POST_INVALID_ROM, |
| 144 | "Failed to load after CAR program.\n"); |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 145 | |
| 146 | /* Set the stack pointer within parameters of the program loaded. */ |
| 147 | if (rsl.params == NULL) |
Keith Short | 7006458 | 2019-05-06 16:12:57 -0600 | [diff] [blame] | 148 | die_with_post_code(POST_INVALID_ROM, |
| 149 | "No parameters found in after CAR program.\n"); |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 150 | |
Kyösti Mälkki | a7421fb | 2017-09-05 22:43:05 +0300 | [diff] [blame] | 151 | finalize_load(rsl.params, pcf->stack); |
Aaron Durbin | dd95e00 | 2016-03-31 13:36:33 -0500 | [diff] [blame] | 152 | |
Subrata Banik | 90f750b | 2019-06-11 17:52:06 +0530 | [diff] [blame] | 153 | stage_cache_add(STAGE_POSTCAR, prog); |
Aaron Durbin | d008413 | 2016-11-29 15:52:08 -0600 | [diff] [blame] | 154 | } |
| 155 | |
Subrata Banik | 8edc6dc | 2019-08-22 11:30:52 +0530 | [diff] [blame] | 156 | /* |
| 157 | * Cache the TSEG region at the top of ram. This region is |
| 158 | * not restricted to SMM mode until SMM has been relocated. |
| 159 | * By setting the region to cacheable it provides faster access |
| 160 | * when relocating the SMM handler as well as using the TSEG |
| 161 | * region for other purposes. |
| 162 | */ |
| 163 | void postcar_enable_tseg_cache(struct postcar_frame *pcf) |
| 164 | { |
| 165 | uintptr_t smm_base; |
| 166 | size_t smm_size; |
| 167 | |
| 168 | smm_region(&smm_base, &smm_size); |
| 169 | postcar_frame_add_mtrr(pcf, smm_base, smm_size, |
| 170 | MTRR_TYPE_WRBACK); |
| 171 | } |
| 172 | |
Kyösti Mälkki | 4f14cd8 | 2019-12-18 19:40:48 +0200 | [diff] [blame] | 173 | static void postcar_cache_invalid(void) |
| 174 | { |
| 175 | printk(BIOS_ERR, "postcar cache invalid.\n"); |
| 176 | board_reset(); |
| 177 | } |
| 178 | |
Aaron Durbin | d008413 | 2016-11-29 15:52:08 -0600 | [diff] [blame] | 179 | void run_postcar_phase(struct postcar_frame *pcf) |
| 180 | { |
| 181 | struct prog prog = |
Philipp Deppenwiese | 01797b1 | 2018-11-08 10:39:39 +0100 | [diff] [blame] | 182 | PROG_INIT(PROG_POSTCAR, CONFIG_CBFS_PREFIX "/postcar"); |
Aaron Durbin | d008413 | 2016-11-29 15:52:08 -0600 | [diff] [blame] | 183 | |
| 184 | postcar_commit_mtrrs(pcf); |
| 185 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 186 | if (!CONFIG(NO_STAGE_CACHE) && |
Kyösti Mälkki | a7421fb | 2017-09-05 22:43:05 +0300 | [diff] [blame] | 187 | romstage_handoff_is_resume()) { |
Aaron Durbin | d008413 | 2016-11-29 15:52:08 -0600 | [diff] [blame] | 188 | stage_cache_load_stage(STAGE_POSTCAR, &prog); |
Aaron Durbin | c546c76 | 2018-04-23 14:55:09 -0600 | [diff] [blame] | 189 | /* This is here to allow platforms to pass different stack |
| 190 | parameters between S3 resume and normal boot. On the |
| 191 | platforms where the values are the same it's a nop. */ |
Kyösti Mälkki | a7421fb | 2017-09-05 22:43:05 +0300 | [diff] [blame] | 192 | finalize_load(prog.arg, pcf->stack); |
Kyösti Mälkki | 4f14cd8 | 2019-12-18 19:40:48 +0200 | [diff] [blame] | 193 | |
| 194 | if (prog_entry(&prog) == NULL) |
| 195 | postcar_cache_invalid(); |
Kyösti Mälkki | a7421fb | 2017-09-05 22:43:05 +0300 | [diff] [blame] | 196 | } else |
Aaron Durbin | d008413 | 2016-11-29 15:52:08 -0600 | [diff] [blame] | 197 | load_postcar_cbfs(&prog, pcf); |
| 198 | |
Subrata Banik | 2847e1e | 2019-02-25 20:31:22 +0530 | [diff] [blame] | 199 | /* As postcar exist, it's end of romstage here */ |
| 200 | timestamp_add_now(TS_END_ROMSTAGE); |
| 201 | |
Kyösti Mälkki | 45ddb43 | 2019-11-02 14:12:18 +0200 | [diff] [blame] | 202 | console_time_report(); |
| 203 | |
Arthur Heymans | 5331a7c | 2019-10-23 17:07:15 +0200 | [diff] [blame] | 204 | prog_set_arg(&prog, cbmem_top()); |
| 205 | |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 206 | prog_run(&prog); |
| 207 | } |