Angel Pons | 118a9c7 | 2020-04-02 23:48:34 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | /* This file is part of the coreboot project. */ |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 3 | |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 4 | #include <bootstate.h> |
| 5 | #include <cbmem.h> |
| 6 | #include <console/console.h> |
| 7 | #include <imd.h> |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 8 | #include <stage_cache.h> |
| 9 | #include <string.h> |
| 10 | |
Arthur Heymans | 7c2994b | 2019-11-20 20:01:11 +0100 | [diff] [blame] | 11 | static struct imd imd_stage_cache; |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 12 | |
Aaron Durbin | 42e6856 | 2015-06-09 13:55:51 -0500 | [diff] [blame] | 13 | static void stage_cache_create_empty(void) |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 14 | { |
| 15 | struct imd *imd; |
| 16 | void *base; |
| 17 | size_t size; |
| 18 | |
Arthur Heymans | 7c2994b | 2019-11-20 20:01:11 +0100 | [diff] [blame] | 19 | imd = &imd_stage_cache; |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 20 | stage_cache_external_region(&base, &size); |
| 21 | imd_handle_init(imd, (void *)(size + (uintptr_t)base)); |
| 22 | |
| 23 | printk(BIOS_DEBUG, "External stage cache:\n"); |
| 24 | imd_create_tiered_empty(imd, 4096, 4096, 1024, 32); |
| 25 | if (imd_limit_size(imd, size)) |
| 26 | printk(BIOS_DEBUG, "Could not limit stage cache size.\n"); |
| 27 | } |
| 28 | |
Aaron Durbin | 42e6856 | 2015-06-09 13:55:51 -0500 | [diff] [blame] | 29 | static void stage_cache_recover(void) |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 30 | { |
| 31 | struct imd *imd; |
| 32 | void *base; |
| 33 | size_t size; |
| 34 | |
Arthur Heymans | 7c2994b | 2019-11-20 20:01:11 +0100 | [diff] [blame] | 35 | imd = &imd_stage_cache; |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 36 | stage_cache_external_region(&base, &size); |
| 37 | imd_handle_init(imd, (void *)(size + (uintptr_t)base)); |
| 38 | if (imd_recover(imd)) |
| 39 | printk(BIOS_DEBUG, "Unable to recover external stage cache.\n"); |
| 40 | } |
| 41 | |
Aaron Durbin | 54546c9 | 2015-08-05 00:52:13 -0500 | [diff] [blame] | 42 | void stage_cache_add(int stage_id, const struct prog *stage) |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 43 | { |
| 44 | struct imd *imd; |
| 45 | const struct imd_entry *e; |
| 46 | struct stage_cache *meta; |
| 47 | void *c; |
| 48 | |
Arthur Heymans | 7c2994b | 2019-11-20 20:01:11 +0100 | [diff] [blame] | 49 | imd = &imd_stage_cache; |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 50 | e = imd_entry_add(imd, CBMEM_ID_STAGEx_META + stage_id, sizeof(*meta)); |
| 51 | |
Marshall Dawson | 0876caa | 2018-01-30 15:54:52 -0700 | [diff] [blame] | 52 | if (e == NULL) { |
| 53 | printk(BIOS_DEBUG, "Error: Can't add %x metadata to imd\n", |
| 54 | CBMEM_ID_STAGEx_META + stage_id); |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 55 | return; |
Marshall Dawson | 0876caa | 2018-01-30 15:54:52 -0700 | [diff] [blame] | 56 | } |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 57 | |
| 58 | meta = imd_entry_at(imd, e); |
| 59 | |
| 60 | meta->load_addr = (uintptr_t)prog_start(stage); |
| 61 | meta->entry_addr = (uintptr_t)prog_entry(stage); |
Aaron Durbin | 3b16e78 | 2018-04-23 14:47:07 -0600 | [diff] [blame] | 62 | meta->arg = (uintptr_t)prog_entry_arg(stage); |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 63 | |
| 64 | e = imd_entry_add(imd, CBMEM_ID_STAGEx_CACHE + stage_id, |
| 65 | prog_size(stage)); |
| 66 | |
Marshall Dawson | 0876caa | 2018-01-30 15:54:52 -0700 | [diff] [blame] | 67 | if (e == NULL) { |
| 68 | printk(BIOS_DEBUG, "Error: Can't add stage_cache %x to imd\n", |
| 69 | CBMEM_ID_STAGEx_CACHE + stage_id); |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 70 | return; |
Marshall Dawson | 0876caa | 2018-01-30 15:54:52 -0700 | [diff] [blame] | 71 | } |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 72 | |
| 73 | c = imd_entry_at(imd, e); |
| 74 | |
| 75 | memcpy(c, prog_start(stage), prog_size(stage)); |
| 76 | } |
| 77 | |
Marshall Dawson | 8d6e0e0 | 2018-01-30 15:33:23 -0700 | [diff] [blame] | 78 | void stage_cache_add_raw(int stage_id, const void *base, const size_t size) |
| 79 | { |
| 80 | struct imd *imd; |
| 81 | const struct imd_entry *e; |
| 82 | void *c; |
| 83 | |
Arthur Heymans | 7c2994b | 2019-11-20 20:01:11 +0100 | [diff] [blame] | 84 | imd = &imd_stage_cache; |
Marshall Dawson | 8d6e0e0 | 2018-01-30 15:33:23 -0700 | [diff] [blame] | 85 | e = imd_entry_add(imd, CBMEM_ID_STAGEx_RAW + stage_id, size); |
| 86 | if (e == NULL) { |
| 87 | printk(BIOS_DEBUG, "Error: Can't add %x raw data to imd\n", |
| 88 | CBMEM_ID_STAGEx_RAW + stage_id); |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | c = imd_entry_at(imd, e); |
| 93 | if (c == NULL) { |
| 94 | printk(BIOS_DEBUG, "Error: Can't get %x raw entry in imd\n", |
| 95 | CBMEM_ID_STAGEx_RAW + stage_id); |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | memcpy(c, base, size); |
| 100 | } |
| 101 | |
| 102 | void stage_cache_get_raw(int stage_id, void **base, size_t *size) |
| 103 | { |
| 104 | struct imd *imd; |
| 105 | const struct imd_entry *e; |
| 106 | |
Arthur Heymans | 7c2994b | 2019-11-20 20:01:11 +0100 | [diff] [blame] | 107 | imd = &imd_stage_cache; |
Marshall Dawson | 8d6e0e0 | 2018-01-30 15:33:23 -0700 | [diff] [blame] | 108 | e = imd_entry_find(imd, CBMEM_ID_STAGEx_RAW + stage_id); |
| 109 | if (e == NULL) { |
| 110 | printk(BIOS_DEBUG, "Error: Can't find %x raw data to imd\n", |
| 111 | CBMEM_ID_STAGEx_RAW + stage_id); |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | *base = imd_entry_at(imd, e); |
| 116 | *size = imd_entry_size(imd, e); |
| 117 | } |
| 118 | |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 119 | void stage_cache_load_stage(int stage_id, struct prog *stage) |
| 120 | { |
| 121 | struct imd *imd; |
| 122 | struct stage_cache *meta; |
| 123 | const struct imd_entry *e; |
| 124 | void *c; |
| 125 | size_t size; |
| 126 | |
Arthur Heymans | 7c2994b | 2019-11-20 20:01:11 +0100 | [diff] [blame] | 127 | imd = &imd_stage_cache; |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 128 | e = imd_entry_find(imd, CBMEM_ID_STAGEx_META + stage_id); |
Marshall Dawson | 0876caa | 2018-01-30 15:54:52 -0700 | [diff] [blame] | 129 | if (e == NULL) { |
| 130 | printk(BIOS_DEBUG, "Error: Can't find %x metadata in imd\n", |
| 131 | CBMEM_ID_STAGEx_META + stage_id); |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 132 | return; |
Marshall Dawson | 0876caa | 2018-01-30 15:54:52 -0700 | [diff] [blame] | 133 | } |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 134 | |
| 135 | meta = imd_entry_at(imd, e); |
| 136 | |
| 137 | e = imd_entry_find(imd, CBMEM_ID_STAGEx_CACHE + stage_id); |
| 138 | |
Marshall Dawson | 0876caa | 2018-01-30 15:54:52 -0700 | [diff] [blame] | 139 | if (e == NULL) { |
| 140 | printk(BIOS_DEBUG, "Error: Can't find stage_cache %x in imd\n", |
| 141 | CBMEM_ID_STAGEx_CACHE + stage_id); |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 142 | return; |
Marshall Dawson | 0876caa | 2018-01-30 15:54:52 -0700 | [diff] [blame] | 143 | } |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 144 | |
| 145 | c = imd_entry_at(imd, e); |
| 146 | size = imd_entry_size(imd, e); |
| 147 | |
| 148 | memcpy((void *)(uintptr_t)meta->load_addr, c, size); |
| 149 | |
| 150 | prog_set_area(stage, (void *)(uintptr_t)meta->load_addr, size); |
Aaron Durbin | 3b16e78 | 2018-04-23 14:47:07 -0600 | [diff] [blame] | 151 | prog_set_entry(stage, (void *)(uintptr_t)meta->entry_addr, |
| 152 | (void *)(uintptr_t)meta->arg); |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 153 | } |
| 154 | |
Aaron Durbin | 42e6856 | 2015-06-09 13:55:51 -0500 | [diff] [blame] | 155 | static void stage_cache_setup(int is_recovery) |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 156 | { |
Aaron Durbin | 42e6856 | 2015-06-09 13:55:51 -0500 | [diff] [blame] | 157 | if (is_recovery) |
| 158 | stage_cache_recover(); |
| 159 | else |
| 160 | stage_cache_create_empty(); |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 161 | } |
Aaron Durbin | 42e6856 | 2015-06-09 13:55:51 -0500 | [diff] [blame] | 162 | |
| 163 | ROMSTAGE_CBMEM_INIT_HOOK(stage_cache_setup) |
| 164 | RAMSTAGE_CBMEM_INIT_HOOK(stage_cache_setup) |
Brandon Breitenstein | 135eae9 | 2016-09-30 13:57:12 -0700 | [diff] [blame] | 165 | POSTCAR_CBMEM_INIT_HOOK(stage_cache_setup) |