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