soc/intel/tigerlake: Utilize vbt data size Kconfig option
[coreboot.git] / src / lib / ext_stage_cache.c
blobd498597d1359775e53b2dcc8f1409cdcdb8fc0d9
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <cbmem.h>
4 #include <console/console.h>
5 #include <imd.h>
6 #include <stage_cache.h>
7 #include <string.h>
9 static struct imd imd_stage_cache;
11 static void stage_cache_create_empty(void)
13 struct imd *imd;
14 void *base;
15 size_t size;
17 imd = &imd_stage_cache;
18 stage_cache_external_region(&base, &size);
19 imd_handle_init(imd, (void *)(size + (uintptr_t)base));
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");
27 static void stage_cache_recover(void)
29 struct imd *imd;
30 void *base;
31 size_t size;
33 imd = &imd_stage_cache;
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");
40 void stage_cache_add(int stage_id, const struct prog *stage)
42 struct imd *imd;
43 const struct imd_entry *e;
44 struct stage_cache *meta;
45 void *c;
47 imd = &imd_stage_cache;
48 e = imd_entry_add(imd, CBMEM_ID_STAGEx_META + stage_id, sizeof(*meta));
50 if (e == NULL) {
51 printk(BIOS_DEBUG, "Error: Can't add %x metadata to imd\n",
52 CBMEM_ID_STAGEx_META + stage_id);
53 return;
56 meta = imd_entry_at(imd, e);
58 meta->load_addr = (uintptr_t)prog_start(stage);
59 meta->entry_addr = (uintptr_t)prog_entry(stage);
60 meta->arg = (uintptr_t)prog_entry_arg(stage);
62 e = imd_entry_add(imd, CBMEM_ID_STAGEx_CACHE + stage_id,
63 prog_size(stage));
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);
68 return;
71 c = imd_entry_at(imd, e);
73 memcpy(c, prog_start(stage), prog_size(stage));
76 void stage_cache_add_raw(int stage_id, const void *base, const size_t size)
78 struct imd *imd;
79 const struct imd_entry *e;
80 void *c;
82 imd = &imd_stage_cache;
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;
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;
97 memcpy(c, base, size);
100 void stage_cache_get_raw(int stage_id, void **base, size_t *size)
102 struct imd *imd;
103 const struct imd_entry *e;
105 imd = &imd_stage_cache;
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;
113 *base = imd_entry_at(imd, e);
114 *size = imd_entry_size(e);
117 void stage_cache_load_stage(int stage_id, struct prog *stage)
119 struct imd *imd;
120 struct stage_cache *meta;
121 const struct imd_entry *e;
122 void *c;
123 size_t size;
125 imd = &imd_stage_cache;
126 e = imd_entry_find(imd, CBMEM_ID_STAGEx_META + stage_id);
127 if (e == NULL) {
128 printk(BIOS_DEBUG, "Error: Can't find %x metadata in imd\n",
129 CBMEM_ID_STAGEx_META + stage_id);
130 return;
133 meta = imd_entry_at(imd, e);
135 e = imd_entry_find(imd, CBMEM_ID_STAGEx_CACHE + stage_id);
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);
140 return;
143 c = imd_entry_at(imd, e);
144 size = imd_entry_size(e);
146 memcpy((void *)(uintptr_t)meta->load_addr, c, size);
148 prog_set_area(stage, (void *)(uintptr_t)meta->load_addr, size);
149 prog_set_entry(stage, (void *)(uintptr_t)meta->entry_addr,
150 (void *)(uintptr_t)meta->arg);
153 static void stage_cache_setup(int is_recovery)
155 if (is_recovery)
156 stage_cache_recover();
157 else
158 stage_cache_create_empty();
161 ROMSTAGE_CBMEM_INIT_HOOK(stage_cache_setup)
162 RAMSTAGE_CBMEM_INIT_HOOK(stage_cache_setup)
163 POSTCAR_CBMEM_INIT_HOOK(stage_cache_setup)