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 | |
| 24 | c = cbmem_add(CBMEM_ID_STAGEx_CACHE + stage_id, prog_size(stage)); |
Marshall Dawson | 0876caa | 2018-01-30 15:54:52 -0700 | [diff] [blame] | 25 | if (c == NULL) { |
Julius Werner | e966595 | 2022-01-21 17:06:20 -0800 | [diff] [blame] | 26 | printk(BIOS_ERR, "Can't add stage_cache %x to cbmem\n", |
Marshall Dawson | 0876caa | 2018-01-30 15:54:52 -0700 | [diff] [blame] | 27 | CBMEM_ID_STAGEx_CACHE + stage_id); |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 28 | return; |
Marshall Dawson | 0876caa | 2018-01-30 15:54:52 -0700 | [diff] [blame] | 29 | } |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 30 | |
| 31 | memcpy(c, prog_start(stage), prog_size(stage)); |
| 32 | } |
| 33 | |
Marshall Dawson | 8d6e0e0 | 2018-01-30 15:33:23 -0700 | [diff] [blame] | 34 | void stage_cache_add_raw(int stage_id, const void *base, const size_t size) |
| 35 | { |
| 36 | void *c; |
| 37 | |
| 38 | c = cbmem_add(CBMEM_ID_STAGEx_RAW + stage_id, size); |
| 39 | if (c == NULL) { |
| 40 | printk(BIOS_DEBUG, "Error: Can't add %x raw data to cbmem\n", |
| 41 | CBMEM_ID_STAGEx_RAW + stage_id); |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | memcpy(c, base, size); |
| 46 | } |
| 47 | |
| 48 | void stage_cache_get_raw(int stage_id, void **base, size_t *size) |
| 49 | { |
| 50 | const struct cbmem_entry *e; |
| 51 | |
| 52 | e = cbmem_entry_find(CBMEM_ID_STAGEx_RAW + stage_id); |
| 53 | if (e == NULL) { |
Julius Werner | e966595 | 2022-01-21 17:06:20 -0800 | [diff] [blame] | 54 | printk(BIOS_ERR, "Can't find raw %x data in cbmem\n", |
Marshall Dawson | 8d6e0e0 | 2018-01-30 15:33:23 -0700 | [diff] [blame] | 55 | CBMEM_ID_STAGEx_RAW + stage_id); |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | *base = cbmem_entry_start(e); |
| 60 | *size = cbmem_entry_size(e); |
| 61 | } |
| 62 | |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 63 | void stage_cache_load_stage(int stage_id, struct prog *stage) |
| 64 | { |
| 65 | struct stage_cache *meta; |
| 66 | const struct cbmem_entry *e; |
| 67 | void *c; |
| 68 | size_t size; |
| 69 | void *load_addr; |
| 70 | |
| 71 | prog_set_entry(stage, NULL, NULL); |
| 72 | |
| 73 | meta = cbmem_find(CBMEM_ID_STAGEx_META + stage_id); |
Marshall Dawson | 0876caa | 2018-01-30 15:54:52 -0700 | [diff] [blame] | 74 | if (meta == NULL) { |
Julius Werner | e966595 | 2022-01-21 17:06:20 -0800 | [diff] [blame] | 75 | printk(BIOS_ERR, "Can't find %x metadata in cbmem\n", |
Marshall Dawson | 0876caa | 2018-01-30 15:54:52 -0700 | [diff] [blame] | 76 | CBMEM_ID_STAGEx_META + stage_id); |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 77 | return; |
Marshall Dawson | 0876caa | 2018-01-30 15:54:52 -0700 | [diff] [blame] | 78 | } |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 79 | |
| 80 | e = cbmem_entry_find(CBMEM_ID_STAGEx_CACHE + stage_id); |
| 81 | |
Marshall Dawson | 0876caa | 2018-01-30 15:54:52 -0700 | [diff] [blame] | 82 | if (e == NULL) { |
Julius Werner | e966595 | 2022-01-21 17:06:20 -0800 | [diff] [blame] | 83 | printk(BIOS_ERR, "Can't find stage_cache %x in cbmem\n", |
Marshall Dawson | 0876caa | 2018-01-30 15:54:52 -0700 | [diff] [blame] | 84 | CBMEM_ID_STAGEx_CACHE + stage_id); |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 85 | return; |
Marshall Dawson | 0876caa | 2018-01-30 15:54:52 -0700 | [diff] [blame] | 86 | } |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 87 | |
| 88 | c = cbmem_entry_start(e); |
| 89 | size = cbmem_entry_size(e); |
| 90 | load_addr = (void *)(uintptr_t)meta->load_addr; |
| 91 | |
| 92 | memcpy(load_addr, c, size); |
| 93 | |
| 94 | prog_set_area(stage, load_addr, size); |
Kyösti Mälkki | d87e4b3 | 2017-09-05 22:43:05 +0300 | [diff] [blame] | 95 | prog_set_entry(stage, (void *)(uintptr_t)meta->entry_addr, |
| 96 | (void *)(uintptr_t)meta->arg); |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 97 | } |