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 <stage_cache.h> |
Subrata Banik | 2847e1e | 2019-02-25 20:31:22 +0530 | [diff] [blame] | 13 | #include <timestamp.h> |
Wim Vervoorn | 1058dd8 | 2019-11-01 10:22:22 +0100 | [diff] [blame] | 14 | #include <security/vboot/vboot_common.h> |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 15 | |
| 16 | static 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älkki | 029cebc | 2016-12-02 19:47:07 +0200 | [diff] [blame] | 25 | static void postcar_frame_prepare(struct postcar_frame *pcf) |
| 26 | { |
Aaron Durbin | e1bf065 | 2020-05-28 21:34:43 -0600 | [diff] [blame] | 27 | var_mtrr_context_init(&pcf->ctx, pcf); |
Kyösti Mälkki | 029cebc | 2016-12-02 19:47:07 +0200 | [diff] [blame] | 28 | } |
| 29 | |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 30 | int postcar_frame_init(struct postcar_frame *pcf, size_t stack_size) |
| 31 | { |
| 32 | void *stack; |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 33 | |
Kyösti Mälkki | 6e2d0c1 | 2019-06-28 10:08:51 +0300 | [diff] [blame] | 34 | /* |
| 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 Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 43 | 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älkki | 029cebc | 2016-12-02 19:47:07 +0200 | [diff] [blame] | 50 | postcar_frame_prepare(pcf); |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 51 | pcf->stack = (uintptr_t)stack; |
| 52 | pcf->stack += stack_size; |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 53 | return 0; |
| 54 | } |
| 55 | |
Aaron Durbin | e1bf065 | 2020-05-28 21:34:43 -0600 | [diff] [blame] | 56 | static 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", |
Werner Zeh | eaf11c9 | 2022-04-11 08:35:06 +0200 | [diff] [blame] | 63 | addr, addr + size - 1, size); |
Aaron Durbin | e1bf065 | 2020-05-28 21:34:43 -0600 | [diff] [blame] | 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 Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 71 | void postcar_frame_add_mtrr(struct postcar_frame *pcf, |
| 72 | uintptr_t addr, size_t size, int type) |
| 73 | { |
Aaron Durbin | e1bf065 | 2020-05-28 21:34:43 -0600 | [diff] [blame] | 74 | 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] | 75 | } |
| 76 | |
Nico Huber | 36ec3e9 | 2018-05-27 14:32:27 +0200 | [diff] [blame] | 77 | void postcar_frame_add_romcache(struct postcar_frame *pcf, int type) |
| 78 | { |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 79 | if (!CONFIG(BOOT_DEVICE_MEMORY_MAPPED)) |
Nico Huber | 36ec3e9 | 2018-05-27 14:32:27 +0200 | [diff] [blame] | 80 | return; |
| 81 | postcar_frame_add_mtrr(pcf, CACHE_ROM_BASE, CACHE_ROM_SIZE, type); |
| 82 | } |
| 83 | |
Aaron Durbin | e893674 | 2020-04-29 14:20:05 -0600 | [diff] [blame] | 84 | static void postcar_frame_common_mtrrs(struct postcar_frame *pcf) |
Kyösti Mälkki | 544878b | 2019-08-09 11:41:15 +0300 | [diff] [blame] | 85 | { |
| 86 | if (pcf->skip_common_mtrr) |
| 87 | return; |
| 88 | |
Kyösti Mälkki | 544878b | 2019-08-09 11:41:15 +0300 | [diff] [blame] | 89 | /* Cache the ROM as WP just below 4GiB. */ |
| 90 | postcar_frame_add_romcache(pcf, MTRR_TYPE_WRPROT); |
| 91 | } |
| 92 | |
Kyösti Mälkki | cd7a70f | 2019-08-17 20:51:08 +0300 | [diff] [blame] | 93 | /* 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. */ |
| 95 | void 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 Durbin | df2bfb9 | 2019-08-27 20:22:40 -0600 | [diff] [blame] | 108 | static void postcar_commit_mtrrs(struct postcar_frame *pcf) |
Rizwan Qureshi | b1b44d3 | 2016-08-26 21:08:50 +0530 | [diff] [blame] | 109 | { |
| 110 | /* |
| 111 | * Place the number of used variable MTRRs on stack then max number |
| 112 | * of variable MTRRs supported in the system. |
| 113 | */ |
Aaron Durbin | e1bf065 | 2020-05-28 21:34:43 -0600 | [diff] [blame] | 114 | stack_push(pcf, pcf->ctx.used_var_mtrrs); |
| 115 | stack_push(pcf, pcf->ctx.max_var_mtrrs); |
Rizwan Qureshi | b1b44d3 | 2016-08-26 21:08:50 +0530 | [diff] [blame] | 116 | } |
| 117 | |
Kyösti Mälkki | a7421fb | 2017-09-05 22:43:05 +0300 | [diff] [blame] | 118 | static 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 Durbin | d008413 | 2016-11-29 15:52:08 -0600 | [diff] [blame] | 129 | static void load_postcar_cbfs(struct prog *prog, struct postcar_frame *pcf) |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 130 | { |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 131 | struct rmod_stage_load rsl = { |
| 132 | .cbmem_id = CBMEM_ID_AFTER_CAR, |
Aaron Durbin | d008413 | 2016-11-29 15:52:08 -0600 | [diff] [blame] | 133 | .prog = prog, |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 134 | }; |
| 135 | |
Wim Vervoorn | 1058dd8 | 2019-11-01 10:22:22 +0100 | [diff] [blame] | 136 | vboot_run_logic(); |
| 137 | |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 138 | if (rmodule_stage_load(&rsl)) |
Keith Short | 7006458 | 2019-05-06 16:12:57 -0600 | [diff] [blame] | 139 | die_with_post_code(POST_INVALID_ROM, |
| 140 | "Failed to load after CAR program.\n"); |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 141 | |
| 142 | /* Set the stack pointer within parameters of the program loaded. */ |
| 143 | if (rsl.params == NULL) |
Keith Short | 7006458 | 2019-05-06 16:12:57 -0600 | [diff] [blame] | 144 | die_with_post_code(POST_INVALID_ROM, |
| 145 | "No parameters found in after CAR program.\n"); |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 146 | |
Kyösti Mälkki | a7421fb | 2017-09-05 22:43:05 +0300 | [diff] [blame] | 147 | finalize_load(rsl.params, pcf->stack); |
Aaron Durbin | dd95e00 | 2016-03-31 13:36:33 -0500 | [diff] [blame] | 148 | |
Subrata Banik | 90f750b | 2019-06-11 17:52:06 +0530 | [diff] [blame] | 149 | stage_cache_add(STAGE_POSTCAR, prog); |
Aaron Durbin | d008413 | 2016-11-29 15:52:08 -0600 | [diff] [blame] | 150 | } |
| 151 | |
Subrata Banik | 8edc6dc | 2019-08-22 11:30:52 +0530 | [diff] [blame] | 152 | /* |
| 153 | * Cache the TSEG region at the top of ram. This region is |
| 154 | * not restricted to SMM mode until SMM has been relocated. |
| 155 | * By setting the region to cacheable it provides faster access |
| 156 | * when relocating the SMM handler as well as using the TSEG |
| 157 | * region for other purposes. |
| 158 | */ |
| 159 | void postcar_enable_tseg_cache(struct postcar_frame *pcf) |
| 160 | { |
| 161 | uintptr_t smm_base; |
| 162 | size_t smm_size; |
| 163 | |
| 164 | smm_region(&smm_base, &smm_size); |
| 165 | postcar_frame_add_mtrr(pcf, smm_base, smm_size, |
| 166 | MTRR_TYPE_WRBACK); |
| 167 | } |
| 168 | |
Kyösti Mälkki | 4f14cd8 | 2019-12-18 19:40:48 +0200 | [diff] [blame] | 169 | static void postcar_cache_invalid(void) |
| 170 | { |
| 171 | printk(BIOS_ERR, "postcar cache invalid.\n"); |
| 172 | board_reset(); |
| 173 | } |
| 174 | |
Aaron Durbin | d008413 | 2016-11-29 15:52:08 -0600 | [diff] [blame] | 175 | void run_postcar_phase(struct postcar_frame *pcf) |
| 176 | { |
| 177 | struct prog prog = |
Philipp Deppenwiese | 01797b1 | 2018-11-08 10:39:39 +0100 | [diff] [blame] | 178 | PROG_INIT(PROG_POSTCAR, CONFIG_CBFS_PREFIX "/postcar"); |
Aaron Durbin | d008413 | 2016-11-29 15:52:08 -0600 | [diff] [blame] | 179 | |
| 180 | postcar_commit_mtrrs(pcf); |
| 181 | |
Kyösti Mälkki | e0165fb | 2021-01-09 13:30:57 +0200 | [diff] [blame] | 182 | if (resume_from_stage_cache()) { |
Aaron Durbin | d008413 | 2016-11-29 15:52:08 -0600 | [diff] [blame] | 183 | stage_cache_load_stage(STAGE_POSTCAR, &prog); |
Aaron Durbin | c546c76 | 2018-04-23 14:55:09 -0600 | [diff] [blame] | 184 | /* This is here to allow platforms to pass different stack |
| 185 | parameters between S3 resume and normal boot. On the |
| 186 | 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] | 187 | finalize_load(prog.arg, pcf->stack); |
Kyösti Mälkki | 4f14cd8 | 2019-12-18 19:40:48 +0200 | [diff] [blame] | 188 | |
| 189 | if (prog_entry(&prog) == NULL) |
| 190 | postcar_cache_invalid(); |
Kyösti Mälkki | a7421fb | 2017-09-05 22:43:05 +0300 | [diff] [blame] | 191 | } else |
Aaron Durbin | d008413 | 2016-11-29 15:52:08 -0600 | [diff] [blame] | 192 | load_postcar_cbfs(&prog, pcf); |
| 193 | |
Subrata Banik | 2847e1e | 2019-02-25 20:31:22 +0530 | [diff] [blame] | 194 | /* As postcar exist, it's end of romstage here */ |
Jakub Czapiga | ad6157e | 2022-02-15 11:50:31 +0100 | [diff] [blame] | 195 | timestamp_add_now(TS_ROMSTAGE_END); |
Subrata Banik | 2847e1e | 2019-02-25 20:31:22 +0530 | [diff] [blame] | 196 | |
Kyösti Mälkki | 45ddb43 | 2019-11-02 14:12:18 +0200 | [diff] [blame] | 197 | console_time_report(); |
| 198 | |
Arthur Heymans | 5331a7c | 2019-10-23 17:07:15 +0200 | [diff] [blame] | 199 | prog_set_arg(&prog, cbmem_top()); |
| 200 | |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 201 | prog_run(&prog); |
| 202 | } |