mb/amd/mandolin: Drop SPD eeproms in devicetree
[coreboot.git] / src / lib / imd_cbmem.c
blobcb66c3b219125d9d367a70e89146180d1664f266
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <assert.h>
4 #include <boot/coreboot_tables.h>
5 #include <bootmem.h>
6 #include <console/console.h>
7 #include <cbmem.h>
8 #include <imd.h>
9 #include <lib.h>
10 #include <stdlib.h>
12 /* The program loader passes on cbmem_top and the program entry point
13 has to fill in the _cbmem_top_ptr symbol based on the calling arguments. */
14 uintptr_t _cbmem_top_ptr;
16 static struct imd imd;
18 void *cbmem_top(void)
20 if (ENV_ROMSTAGE) {
21 static void *top;
22 if (top)
23 return top;
24 top = cbmem_top_chipset();
25 return top;
27 if (ENV_POSTCAR || ENV_RAMSTAGE)
28 return (void *)_cbmem_top_ptr;
30 dead_code();
33 static inline const struct cbmem_entry *imd_to_cbmem(const struct imd_entry *e)
35 return (const struct cbmem_entry *)e;
38 static inline const struct imd_entry *cbmem_to_imd(const struct cbmem_entry *e)
40 return (const struct imd_entry *)e;
43 void cbmem_initialize_empty(void)
45 cbmem_initialize_empty_id_size(0, 0);
48 static void cbmem_top_init_once(void)
50 /* Call one-time hook on expected cbmem init during boot. This sequence
51 assumes first init call is in romstage. */
52 if (!ENV_ROMSTAGE)
53 return;
55 /* The test is only effective on X86 and when address hits UC memory. */
56 if (ENV_X86)
57 quick_ram_check_or_die((uintptr_t)cbmem_top() - sizeof(u32));
60 void cbmem_initialize_empty_id_size(u32 id, u64 size)
62 const int no_recovery = 0;
64 cbmem_top_init_once();
66 imd_handle_init(&imd, cbmem_top());
68 printk(BIOS_DEBUG, "CBMEM:\n");
70 if (imd_create_tiered_empty(&imd, CBMEM_ROOT_MIN_SIZE, CBMEM_LG_ALIGN,
71 CBMEM_SM_ROOT_SIZE, CBMEM_SM_ALIGN)) {
72 printk(BIOS_DEBUG, "failed.\n");
73 return;
76 /* Add the specified range first */
77 if (size)
78 cbmem_add(id, size);
80 /* Complete migration to CBMEM. */
81 cbmem_run_init_hooks(no_recovery);
84 int cbmem_initialize(void)
86 return cbmem_initialize_id_size(0, 0);
89 int cbmem_initialize_id_size(u32 id, u64 size)
91 const int recovery = 1;
93 cbmem_top_init_once();
95 imd_handle_init(&imd, cbmem_top());
97 if (imd_recover(&imd))
98 return 1;
101 * Lock the imd in romstage on a recovery. The assumption is that
102 * if the imd area was recovered in romstage then S3 resume path
103 * is being taken.
105 if (ENV_ROMSTAGE)
106 imd_lockdown(&imd);
108 /* Add the specified range first */
109 if (size)
110 cbmem_add(id, size);
112 /* Complete migration to CBMEM. */
113 cbmem_run_init_hooks(recovery);
115 /* Recovery successful. */
116 return 0;
119 int cbmem_recovery(int is_wakeup)
121 int rv = 0;
122 if (!is_wakeup)
123 cbmem_initialize_empty();
124 else
125 rv = cbmem_initialize();
126 return rv;
129 const struct cbmem_entry *cbmem_entry_add(u32 id, u64 size64)
131 const struct imd_entry *e;
133 e = imd_entry_find_or_add(&imd, id, size64);
135 return imd_to_cbmem(e);
138 void *cbmem_add(u32 id, u64 size)
140 const struct imd_entry *e;
142 e = imd_entry_find_or_add(&imd, id, size);
144 if (e == NULL)
145 return NULL;
147 return imd_entry_at(&imd, e);
150 /* Retrieve a region provided a given id. */
151 const struct cbmem_entry *cbmem_entry_find(u32 id)
153 const struct imd_entry *e;
155 e = imd_entry_find(&imd, id);
157 return imd_to_cbmem(e);
160 void *cbmem_find(u32 id)
162 const struct imd_entry *e;
164 e = imd_entry_find(&imd, id);
166 if (e == NULL)
167 return NULL;
169 return imd_entry_at(&imd, e);
172 /* Remove a reserved region. Returns 0 on success, < 0 on error. Note: A region
173 * cannot be removed unless it was the last one added. */
174 int cbmem_entry_remove(const struct cbmem_entry *entry)
176 return imd_entry_remove(&imd, cbmem_to_imd(entry));
179 u64 cbmem_entry_size(const struct cbmem_entry *entry)
181 return imd_entry_size(&imd, cbmem_to_imd(entry));
184 void *cbmem_entry_start(const struct cbmem_entry *entry)
186 return imd_entry_at(&imd, cbmem_to_imd(entry));
189 void cbmem_add_bootmem(void)
191 void *baseptr = NULL;
192 size_t size = 0;
194 cbmem_get_region(&baseptr, &size);
195 bootmem_add_range((uintptr_t)baseptr, size, BM_MEM_TABLE);
198 void cbmem_get_region(void **baseptr, size_t *size)
200 imd_region_used(&imd, baseptr, size);
203 #if ENV_PAYLOAD_LOADER || (CONFIG(EARLY_CBMEM_LIST) \
204 && (ENV_POSTCAR || ENV_ROMSTAGE))
206 * -fdata-sections doesn't work so well on read only strings. They all
207 * get put in the same section even though those strings may never be
208 * referenced in the final binary.
210 void cbmem_list(void)
212 static const struct imd_lookup lookup[] = { CBMEM_ID_TO_NAME_TABLE };
214 imd_print_entries(&imd, lookup, ARRAY_SIZE(lookup));
216 #endif
218 void cbmem_add_records_to_cbtable(struct lb_header *header)
220 struct imd_cursor cursor;
222 if (imd_cursor_init(&imd, &cursor))
223 return;
225 while (1) {
226 const struct imd_entry *e;
227 struct lb_cbmem_entry *lbe;
228 uint32_t id;
230 e = imd_cursor_next(&cursor);
232 if (e == NULL)
233 break;
235 id = imd_entry_id(&imd, e);
236 /* Don't add these metadata entries. */
237 if (id == CBMEM_ID_IMD_ROOT || id == CBMEM_ID_IMD_SMALL)
238 continue;
240 lbe = (struct lb_cbmem_entry *)lb_new_record(header);
241 lbe->tag = LB_TAG_CBMEM_ENTRY;
242 lbe->size = sizeof(*lbe);
243 lbe->address = (uintptr_t)imd_entry_at(&imd, e);
244 lbe->entry_size = imd_entry_size(&imd, e);
245 lbe->id = id;