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> |
Arthur Heymans | 3134a81 | 2019-11-25 12:20:01 +0100 | [diff] [blame] | 12 | #include <romstage_handoff.h> |
Elyes Haouas | ae1ca82 | 2022-10-07 10:02:38 +0200 | [diff] [blame] | 13 | #include <security/vboot/vboot_common.h> |
Aaron Durbin | d008413 | 2016-11-29 15:52:08 -0600 | [diff] [blame] | 14 | #include <stage_cache.h> |
Subrata Banik | 2847e1e | 2019-02-25 20:31:22 +0530 | [diff] [blame] | 15 | #include <timestamp.h> |
Elyes Haouas | ae1ca82 | 2022-10-07 10:02:38 +0200 | [diff] [blame] | 16 | #include <types.h> |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 17 | |
Arthur Heymans | 46b409d | 2021-05-14 13:19:43 +0200 | [diff] [blame] | 18 | static size_t var_mtrr_ctx_size(void) |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 19 | { |
Arthur Heymans | 46b409d | 2021-05-14 13:19:43 +0200 | [diff] [blame] | 20 | int mtrr_count = get_var_mtrr_count(); |
| 21 | return sizeof(struct var_mtrr_context) + mtrr_count * 2 * sizeof(msr_t); |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 22 | } |
| 23 | |
Elyes Haouas | 9523e3b | 2022-11-08 14:38:41 +0100 | [diff] [blame] | 24 | static enum cb_err postcar_frame_init(struct postcar_frame *pcf) |
Kyösti Mälkki | 029cebc | 2016-12-02 19:47:07 +0200 | [diff] [blame] | 25 | { |
Arthur Heymans | 876a1b4 | 2022-02-15 11:06:10 +0100 | [diff] [blame] | 26 | memset(pcf, 0, sizeof(*pcf)); |
| 27 | |
Arthur Heymans | 46b409d | 2021-05-14 13:19:43 +0200 | [diff] [blame] | 28 | struct var_mtrr_context *ctx; |
Kyösti Mälkki | 029cebc | 2016-12-02 19:47:07 +0200 | [diff] [blame] | 29 | |
Arthur Heymans | 46b409d | 2021-05-14 13:19:43 +0200 | [diff] [blame] | 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"); |
Elyes Haouas | 9523e3b | 2022-11-08 14:38:41 +0100 | [diff] [blame] | 33 | return CB_ERR; |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 34 | } |
| 35 | |
Arthur Heymans | 46b409d | 2021-05-14 13:19:43 +0200 | [diff] [blame] | 36 | pcf->mtrr = ctx; |
| 37 | var_mtrr_context_init(pcf->mtrr); |
| 38 | |
Elyes Haouas | 9523e3b | 2022-11-08 14:38:41 +0100 | [diff] [blame] | 39 | return CB_SUCCESS; |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void postcar_frame_add_mtrr(struct postcar_frame *pcf, |
| 43 | uintptr_t addr, size_t size, int type) |
| 44 | { |
Arthur Heymans | 46b409d | 2021-05-14 13:19:43 +0200 | [diff] [blame] | 45 | var_mtrr_set(pcf->mtrr, addr, size, type); |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 46 | } |
| 47 | |
Nico Huber | 36ec3e9 | 2018-05-27 14:32:27 +0200 | [diff] [blame] | 48 | void postcar_frame_add_romcache(struct postcar_frame *pcf, int type) |
| 49 | { |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 50 | if (!CONFIG(BOOT_DEVICE_MEMORY_MAPPED)) |
Nico Huber | 36ec3e9 | 2018-05-27 14:32:27 +0200 | [diff] [blame] | 51 | return; |
| 52 | postcar_frame_add_mtrr(pcf, CACHE_ROM_BASE, CACHE_ROM_SIZE, type); |
| 53 | } |
| 54 | |
Aaron Durbin | e893674 | 2020-04-29 14:20:05 -0600 | [diff] [blame] | 55 | static void postcar_frame_common_mtrrs(struct postcar_frame *pcf) |
Kyösti Mälkki | 544878b | 2019-08-09 11:41:15 +0300 | [diff] [blame] | 56 | { |
| 57 | if (pcf->skip_common_mtrr) |
| 58 | return; |
| 59 | |
Kyösti Mälkki | 544878b | 2019-08-09 11:41:15 +0300 | [diff] [blame] | 60 | /* Cache the ROM as WP just below 4GiB. */ |
| 61 | postcar_frame_add_romcache(pcf, MTRR_TYPE_WRPROT); |
| 62 | } |
| 63 | |
Arthur Heymans | ba00d10 | 2022-02-15 11:04:15 +0100 | [diff] [blame] | 64 | static void run_postcar_phase(struct postcar_frame *pcf); |
| 65 | |
Kyösti Mälkki | cd7a70f | 2019-08-17 20:51:08 +0300 | [diff] [blame] | 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. */ |
Arthur Heymans | efd2720 | 2022-07-13 07:20:47 +0200 | [diff] [blame] | 68 | void __noreturn prepare_and_run_postcar(void) |
Kyösti Mälkki | cd7a70f | 2019-08-17 20:51:08 +0300 | [diff] [blame] | 69 | { |
Arthur Heymans | 876a1b4 | 2022-02-15 11:06:10 +0100 | [diff] [blame] | 70 | struct postcar_frame pcf; |
| 71 | |
| 72 | if (postcar_frame_init(&pcf)) |
Kyösti Mälkki | cd7a70f | 2019-08-17 20:51:08 +0300 | [diff] [blame] | 73 | die("Unable to initialize postcar frame.\n"); |
| 74 | |
Arthur Heymans | 876a1b4 | 2022-02-15 11:06:10 +0100 | [diff] [blame] | 75 | fill_postcar_frame(&pcf); |
Kyösti Mälkki | cd7a70f | 2019-08-17 20:51:08 +0300 | [diff] [blame] | 76 | |
Arthur Heymans | 876a1b4 | 2022-02-15 11:06:10 +0100 | [diff] [blame] | 77 | postcar_frame_common_mtrrs(&pcf); |
Kyösti Mälkki | cd7a70f | 2019-08-17 20:51:08 +0300 | [diff] [blame] | 78 | |
Arthur Heymans | 876a1b4 | 2022-02-15 11:06:10 +0100 | [diff] [blame] | 79 | run_postcar_phase(&pcf); |
Kyösti Mälkki | cd7a70f | 2019-08-17 20:51:08 +0300 | [diff] [blame] | 80 | /* We do not return here. */ |
Arthur Heymans | efd2720 | 2022-07-13 07:20:47 +0200 | [diff] [blame] | 81 | die("Failed to load postcar\n!"); |
Kyösti Mälkki | cd7a70f | 2019-08-17 20:51:08 +0300 | [diff] [blame] | 82 | } |
| 83 | |
Arthur Heymans | 46b409d | 2021-05-14 13:19:43 +0200 | [diff] [blame] | 84 | static void finalize_load(uintptr_t *reloc_params, uintptr_t mtrr_frame_ptr) |
Rizwan Qureshi | b1b44d3 | 2016-08-26 21:08:50 +0530 | [diff] [blame] | 85 | { |
Arthur Heymans | 46b409d | 2021-05-14 13:19:43 +0200 | [diff] [blame] | 86 | *reloc_params = mtrr_frame_ptr; |
Kyösti Mälkki | a7421fb | 2017-09-05 22:43:05 +0300 | [diff] [blame] | 87 | /* |
| 88 | * Signal to rest of system that another update was made to the |
| 89 | * postcar program prior to running it. |
| 90 | */ |
Arthur Heymans | 46b409d | 2021-05-14 13:19:43 +0200 | [diff] [blame] | 91 | prog_segment_loaded((uintptr_t)reloc_params, sizeof(uintptr_t), SEG_FINAL); |
| 92 | prog_segment_loaded((uintptr_t)mtrr_frame_ptr, var_mtrr_ctx_size(), SEG_FINAL); |
Kyösti Mälkki | a7421fb | 2017-09-05 22:43:05 +0300 | [diff] [blame] | 93 | } |
| 94 | |
Aaron Durbin | d008413 | 2016-11-29 15:52:08 -0600 | [diff] [blame] | 95 | static void load_postcar_cbfs(struct prog *prog, struct postcar_frame *pcf) |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 96 | { |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 97 | struct rmod_stage_load rsl = { |
| 98 | .cbmem_id = CBMEM_ID_AFTER_CAR, |
Aaron Durbin | d008413 | 2016-11-29 15:52:08 -0600 | [diff] [blame] | 99 | .prog = prog, |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 100 | }; |
| 101 | |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 102 | if (rmodule_stage_load(&rsl)) |
lilacious | 40cb3fe | 2023-06-21 23:24:14 +0200 | [diff] [blame] | 103 | die_with_post_code(POSTCODE_INVALID_ROM, |
Keith Short | 7006458 | 2019-05-06 16:12:57 -0600 | [diff] [blame] | 104 | "Failed to load after CAR program.\n"); |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 105 | |
| 106 | /* Set the stack pointer within parameters of the program loaded. */ |
| 107 | if (rsl.params == NULL) |
lilacious | 40cb3fe | 2023-06-21 23:24:14 +0200 | [diff] [blame] | 108 | die_with_post_code(POSTCODE_INVALID_ROM, |
Keith Short | 7006458 | 2019-05-06 16:12:57 -0600 | [diff] [blame] | 109 | "No parameters found in after CAR program.\n"); |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 110 | |
Arthur Heymans | 46b409d | 2021-05-14 13:19:43 +0200 | [diff] [blame] | 111 | finalize_load(rsl.params, (uintptr_t)pcf->mtrr); |
Aaron Durbin | dd95e00 | 2016-03-31 13:36:33 -0500 | [diff] [blame] | 112 | |
Subrata Banik | 90f750b | 2019-06-11 17:52:06 +0530 | [diff] [blame] | 113 | stage_cache_add(STAGE_POSTCAR, prog); |
Aaron Durbin | d008413 | 2016-11-29 15:52:08 -0600 | [diff] [blame] | 114 | } |
| 115 | |
Subrata Banik | 8edc6dc | 2019-08-22 11:30:52 +0530 | [diff] [blame] | 116 | /* |
| 117 | * Cache the TSEG region at the top of ram. This region is |
| 118 | * not restricted to SMM mode until SMM has been relocated. |
| 119 | * By setting the region to cacheable it provides faster access |
| 120 | * when relocating the SMM handler as well as using the TSEG |
| 121 | * region for other purposes. |
| 122 | */ |
| 123 | void postcar_enable_tseg_cache(struct postcar_frame *pcf) |
| 124 | { |
| 125 | uintptr_t smm_base; |
| 126 | size_t smm_size; |
| 127 | |
| 128 | smm_region(&smm_base, &smm_size); |
| 129 | postcar_frame_add_mtrr(pcf, smm_base, smm_size, |
| 130 | MTRR_TYPE_WRBACK); |
| 131 | } |
| 132 | |
Kyösti Mälkki | 4f14cd8 | 2019-12-18 19:40:48 +0200 | [diff] [blame] | 133 | static void postcar_cache_invalid(void) |
| 134 | { |
| 135 | printk(BIOS_ERR, "postcar cache invalid.\n"); |
| 136 | board_reset(); |
| 137 | } |
| 138 | |
Arthur Heymans | 3134a81 | 2019-11-25 12:20:01 +0100 | [diff] [blame] | 139 | /* |
| 140 | * POSTCAR will call invd so don't make assumptions on cbmem |
| 141 | * and external stage cache being UC. |
| 142 | */ |
| 143 | static void postcar_flush_cache(void) |
| 144 | { |
| 145 | uintptr_t cbmem_base; |
| 146 | size_t cbmem_size; |
| 147 | uintptr_t stage_cache_base; |
| 148 | size_t stage_cache_size; |
| 149 | |
| 150 | cbmem_get_region((void **)&cbmem_base, &cbmem_size); |
| 151 | prog_segment_loaded(cbmem_base, cbmem_size, SEG_FINAL); |
| 152 | if (CONFIG(TSEG_STAGE_CACHE) && !romstage_handoff_is_resume()) { |
| 153 | stage_cache_external_region((void **)&stage_cache_base, &stage_cache_size); |
| 154 | prog_segment_loaded(stage_cache_base, stage_cache_size, SEG_FINAL); |
| 155 | } |
| 156 | } |
| 157 | |
Arthur Heymans | ba00d10 | 2022-02-15 11:04:15 +0100 | [diff] [blame] | 158 | static void run_postcar_phase(struct postcar_frame *pcf) |
Aaron Durbin | d008413 | 2016-11-29 15:52:08 -0600 | [diff] [blame] | 159 | { |
| 160 | struct prog prog = |
Philipp Deppenwiese | 01797b1 | 2018-11-08 10:39:39 +0100 | [diff] [blame] | 161 | PROG_INIT(PROG_POSTCAR, CONFIG_CBFS_PREFIX "/postcar"); |
Aaron Durbin | d008413 | 2016-11-29 15:52:08 -0600 | [diff] [blame] | 162 | |
Kyösti Mälkki | 6739a6a | 2023-07-04 10:09:59 +0300 | [diff] [blame] | 163 | vboot_run_logic(); |
| 164 | |
Kyösti Mälkki | e0165fb | 2021-01-09 13:30:57 +0200 | [diff] [blame] | 165 | if (resume_from_stage_cache()) { |
Aaron Durbin | d008413 | 2016-11-29 15:52:08 -0600 | [diff] [blame] | 166 | stage_cache_load_stage(STAGE_POSTCAR, &prog); |
Aaron Durbin | c546c76 | 2018-04-23 14:55:09 -0600 | [diff] [blame] | 167 | /* This is here to allow platforms to pass different stack |
| 168 | parameters between S3 resume and normal boot. On the |
| 169 | platforms where the values are the same it's a nop. */ |
Arthur Heymans | 46b409d | 2021-05-14 13:19:43 +0200 | [diff] [blame] | 170 | finalize_load(prog.arg, (uintptr_t)pcf->mtrr); |
Kyösti Mälkki | 4f14cd8 | 2019-12-18 19:40:48 +0200 | [diff] [blame] | 171 | |
| 172 | if (prog_entry(&prog) == NULL) |
| 173 | postcar_cache_invalid(); |
Kyösti Mälkki | a7421fb | 2017-09-05 22:43:05 +0300 | [diff] [blame] | 174 | } else |
Aaron Durbin | d008413 | 2016-11-29 15:52:08 -0600 | [diff] [blame] | 175 | load_postcar_cbfs(&prog, pcf); |
| 176 | |
Subrata Banik | 2847e1e | 2019-02-25 20:31:22 +0530 | [diff] [blame] | 177 | /* As postcar exist, it's end of romstage here */ |
Jakub Czapiga | ad6157e | 2022-02-15 11:50:31 +0100 | [diff] [blame] | 178 | timestamp_add_now(TS_ROMSTAGE_END); |
Subrata Banik | 2847e1e | 2019-02-25 20:31:22 +0530 | [diff] [blame] | 179 | |
Kyösti Mälkki | 45ddb43 | 2019-11-02 14:12:18 +0200 | [diff] [blame] | 180 | console_time_report(); |
| 181 | |
Arthur Heymans | 3134a81 | 2019-11-25 12:20:01 +0100 | [diff] [blame] | 182 | postcar_flush_cache(); |
| 183 | |
Arthur Heymans | 5331a7c | 2019-10-23 17:07:15 +0200 | [diff] [blame] | 184 | prog_set_arg(&prog, cbmem_top()); |
| 185 | |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 186 | prog_run(&prog); |
| 187 | } |