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 <stage_cache.h> |
| 5 | #include <string.h> |
Marshall Dawson | 0876caa | 2018-01-30 15:54:52 -0700 | [diff] [blame] | 6 | #include <console/console.h> |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 7 | |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 8 | /* Stage cache uses cbmem. */ |
Aaron Durbin | 54546c9 | 2015-08-05 00:52:13 -0500 | [diff] [blame] | 9 | void stage_cache_add(int stage_id, const struct prog *stage) |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 10 | { |
| 11 | struct stage_cache *meta; |
| 12 | void *c; |
| 13 | |
| 14 | meta = cbmem_add(CBMEM_ID_STAGEx_META + stage_id, sizeof(*meta)); |
Marshall Dawson | 0876caa | 2018-01-30 15:54:52 -0700 | [diff] [blame] | 15 | if (meta == NULL) { |
Julius Werner | e966595 | 2022-01-21 17:06:20 -0800 | [diff] [blame] | 16 | printk(BIOS_ERR, "Can't add %x metadata to cbmem\n", |
Marshall Dawson | 0876caa | 2018-01-30 15:54:52 -0700 | [diff] [blame] | 17 | CBMEM_ID_STAGEx_META + stage_id); |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 18 | return; |
Marshall Dawson | 0876caa | 2018-01-30 15:54:52 -0700 | [diff] [blame] | 19 | } |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 20 | meta->load_addr = (uintptr_t)prog_start(stage); |
| 21 | meta->entry_addr = (uintptr_t)prog_entry(stage); |
Kyösti Mälkki | d87e4b3 | 2017-09-05 22:43:05 +0300 | [diff] [blame] | 22 | meta->arg = (uintptr_t)prog_entry_arg(stage); |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 23 | |
Patrick Georgi | ed0647a | 2023-12-13 17:57:34 +0100 | [diff] [blame] | 24 | unsigned int p_size = prog_size(stage); |
| 25 | if (stage_id == STAGE_RAMSTAGE) { |
| 26 | /* heap resides at the end of the image and will be |
| 27 | reinitialized, so it doesn't make sense to copy it around. */ |
| 28 | p_size -= CONFIG_HEAP_SIZE; |
| 29 | } |
| 30 | |
| 31 | c = cbmem_add(CBMEM_ID_STAGEx_CACHE + stage_id, p_size); |
Marshall Dawson | 0876caa | 2018-01-30 15:54:52 -0700 | [diff] [blame] | 32 | if (c == NULL) { |
Julius Werner | e966595 | 2022-01-21 17:06:20 -0800 | [diff] [blame] | 33 | printk(BIOS_ERR, "Can't add stage_cache %x to cbmem\n", |
Marshall Dawson | 0876caa | 2018-01-30 15:54:52 -0700 | [diff] [blame] | 34 | CBMEM_ID_STAGEx_CACHE + stage_id); |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 35 | return; |
Marshall Dawson | 0876caa | 2018-01-30 15:54:52 -0700 | [diff] [blame] | 36 | } |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 37 | |
Patrick Georgi | ed0647a | 2023-12-13 17:57:34 +0100 | [diff] [blame] | 38 | memcpy(c, prog_start(stage), p_size); |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 39 | } |
| 40 | |
Marshall Dawson | 8d6e0e0 | 2018-01-30 15:33:23 -0700 | [diff] [blame] | 41 | void stage_cache_add_raw(int stage_id, const void *base, const size_t size) |
| 42 | { |
| 43 | void *c; |
| 44 | |
| 45 | c = cbmem_add(CBMEM_ID_STAGEx_RAW + stage_id, size); |
| 46 | if (c == NULL) { |
| 47 | printk(BIOS_DEBUG, "Error: Can't add %x raw data to cbmem\n", |
| 48 | CBMEM_ID_STAGEx_RAW + stage_id); |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | memcpy(c, base, size); |
| 53 | } |
| 54 | |
| 55 | void stage_cache_get_raw(int stage_id, void **base, size_t *size) |
| 56 | { |
| 57 | const struct cbmem_entry *e; |
| 58 | |
| 59 | e = cbmem_entry_find(CBMEM_ID_STAGEx_RAW + stage_id); |
| 60 | if (e == NULL) { |
Julius Werner | e966595 | 2022-01-21 17:06:20 -0800 | [diff] [blame] | 61 | printk(BIOS_ERR, "Can't find raw %x data in cbmem\n", |
Marshall Dawson | 8d6e0e0 | 2018-01-30 15:33:23 -0700 | [diff] [blame] | 62 | CBMEM_ID_STAGEx_RAW + stage_id); |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | *base = cbmem_entry_start(e); |
| 67 | *size = cbmem_entry_size(e); |
| 68 | } |
| 69 | |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 70 | void stage_cache_load_stage(int stage_id, struct prog *stage) |
| 71 | { |
| 72 | struct stage_cache *meta; |
| 73 | const struct cbmem_entry *e; |
| 74 | void *c; |
| 75 | size_t size; |
| 76 | void *load_addr; |
| 77 | |
| 78 | prog_set_entry(stage, NULL, NULL); |
| 79 | |
| 80 | meta = cbmem_find(CBMEM_ID_STAGEx_META + stage_id); |
Marshall Dawson | 0876caa | 2018-01-30 15:54:52 -0700 | [diff] [blame] | 81 | if (meta == NULL) { |
Julius Werner | e966595 | 2022-01-21 17:06:20 -0800 | [diff] [blame] | 82 | printk(BIOS_ERR, "Can't find %x metadata in cbmem\n", |
Marshall Dawson | 0876caa | 2018-01-30 15:54:52 -0700 | [diff] [blame] | 83 | CBMEM_ID_STAGEx_META + stage_id); |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 84 | return; |
Marshall Dawson | 0876caa | 2018-01-30 15:54:52 -0700 | [diff] [blame] | 85 | } |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 86 | |
| 87 | e = cbmem_entry_find(CBMEM_ID_STAGEx_CACHE + stage_id); |
| 88 | |
Marshall Dawson | 0876caa | 2018-01-30 15:54:52 -0700 | [diff] [blame] | 89 | if (e == NULL) { |
Julius Werner | e966595 | 2022-01-21 17:06:20 -0800 | [diff] [blame] | 90 | printk(BIOS_ERR, "Can't find stage_cache %x in cbmem\n", |
Marshall Dawson | 0876caa | 2018-01-30 15:54:52 -0700 | [diff] [blame] | 91 | CBMEM_ID_STAGEx_CACHE + stage_id); |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 92 | return; |
Marshall Dawson | 0876caa | 2018-01-30 15:54:52 -0700 | [diff] [blame] | 93 | } |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 94 | |
| 95 | c = cbmem_entry_start(e); |
| 96 | size = cbmem_entry_size(e); |
| 97 | load_addr = (void *)(uintptr_t)meta->load_addr; |
| 98 | |
| 99 | memcpy(load_addr, c, size); |
| 100 | |
| 101 | prog_set_area(stage, load_addr, size); |
Kyösti Mälkki | d87e4b3 | 2017-09-05 22:43:05 +0300 | [diff] [blame] | 102 | prog_set_entry(stage, (void *)(uintptr_t)meta->entry_addr, |
| 103 | (void *)(uintptr_t)meta->arg); |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 104 | } |