blob: 453b0c1340ba06909db0403efbf5190937da4148 [file] [log] [blame]
Angel Pons118a9c72020-04-02 23:48:34 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Aaron Durbin0dff57d2015-03-05 21:18:33 -06002
Arthur Heymans340e4b82019-10-23 17:25:58 +02003#include <assert.h>
Elyes HAOUAS0edf6a52019-10-26 18:41:47 +02004#include <boot/coreboot_tables.h>
Aaron Durbin0dff57d2015-03-05 21:18:33 -06005#include <bootmem.h>
6#include <console/console.h>
7#include <cbmem.h>
8#include <imd.h>
Kyösti Mälkkif5cf60f2019-03-18 15:26:48 +02009#include <lib.h>
Elyes HAOUAS93a195c2021-12-31 18:46:13 +010010#include <types.h>
Julius Werner3c814b22016-08-19 16:20:40 -070011
Arthur Heymans340e4b82019-10-23 17:25:58 +020012/* 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. */
14uintptr_t _cbmem_top_ptr;
15
Patrick Georgib6161be2019-11-29 12:27:01 +010016static struct imd imd;
17
Arthur Heymans340e4b82019-10-23 17:25:58 +020018void *cbmem_top(void)
19{
Arthur Heymansc4c5d852019-10-29 07:32:48 +010020 if (ENV_ROMSTAGE) {
Kyösti Mälkkifcbbb912020-04-20 10:21:39 +030021 static void *top;
Arthur Heymans340e4b82019-10-23 17:25:58 +020022 if (top)
23 return top;
24 top = cbmem_top_chipset();
25 return top;
26 }
Arthur Heymansc4c5d852019-10-29 07:32:48 +010027 if (ENV_POSTCAR || ENV_RAMSTAGE)
Arthur Heymans340e4b82019-10-23 17:25:58 +020028 return (void *)_cbmem_top_ptr;
29
30 dead_code();
31}
32
Arthur Heymans54a4f172020-02-06 18:27:50 +010033int cbmem_initialized;
34
Aaron Durbin0dff57d2015-03-05 21:18:33 -060035static inline const struct cbmem_entry *imd_to_cbmem(const struct imd_entry *e)
36{
37 return (const struct cbmem_entry *)e;
38}
39
40static inline const struct imd_entry *cbmem_to_imd(const struct cbmem_entry *e)
41{
42 return (const struct imd_entry *)e;
43}
44
Aaron Durbin0dff57d2015-03-05 21:18:33 -060045void cbmem_initialize_empty(void)
46{
Lee Leahy522149c2015-05-08 11:33:55 -070047 cbmem_initialize_empty_id_size(0, 0);
48}
49
Aaron Durbindfdea2a2017-08-01 10:27:10 -060050static void cbmem_top_init_once(void)
51{
52 /* Call one-time hook on expected cbmem init during boot. This sequence
Kyösti Mälkki513a1a82018-06-03 12:29:50 +030053 assumes first init call is in romstage. */
54 if (!ENV_ROMSTAGE)
Aaron Durbindfdea2a2017-08-01 10:27:10 -060055 return;
56
Kyösti Mälkkif5cf60f2019-03-18 15:26:48 +020057 /* The test is only effective on X86 and when address hits UC memory. */
58 if (ENV_X86)
59 quick_ram_check_or_die((uintptr_t)cbmem_top() - sizeof(u32));
Aaron Durbindfdea2a2017-08-01 10:27:10 -060060}
61
Lee Leahy522149c2015-05-08 11:33:55 -070062void cbmem_initialize_empty_id_size(u32 id, u64 size)
63{
Aaron Durbin41607a42015-06-09 13:54:10 -050064 const int no_recovery = 0;
Aaron Durbin0dff57d2015-03-05 21:18:33 -060065
Aaron Durbindfdea2a2017-08-01 10:27:10 -060066 cbmem_top_init_once();
67
Patrick Georgib6161be2019-11-29 12:27:01 +010068 imd_handle_init(&imd, cbmem_top());
Aaron Durbin0dff57d2015-03-05 21:18:33 -060069
70 printk(BIOS_DEBUG, "CBMEM:\n");
71
Patrick Georgib6161be2019-11-29 12:27:01 +010072 if (imd_create_tiered_empty(&imd, CBMEM_ROOT_MIN_SIZE, CBMEM_LG_ALIGN,
Lee Leahy522149c2015-05-08 11:33:55 -070073 CBMEM_SM_ROOT_SIZE, CBMEM_SM_ALIGN)) {
Aaron Durbin0dff57d2015-03-05 21:18:33 -060074 printk(BIOS_DEBUG, "failed.\n");
75 return;
76 }
77
Lee Leahy522149c2015-05-08 11:33:55 -070078 /* Add the specified range first */
79 if (size)
80 cbmem_add(id, size);
81
Aaron Durbin0dff57d2015-03-05 21:18:33 -060082 /* Complete migration to CBMEM. */
Aaron Durbin41607a42015-06-09 13:54:10 -050083 cbmem_run_init_hooks(no_recovery);
Arthur Heymans54a4f172020-02-06 18:27:50 +010084
85 cbmem_initialized = 1;
Aaron Durbin0dff57d2015-03-05 21:18:33 -060086}
87
Aaron Durbin0dff57d2015-03-05 21:18:33 -060088int cbmem_initialize(void)
89{
Lee Leahy522149c2015-05-08 11:33:55 -070090 return cbmem_initialize_id_size(0, 0);
91}
92
93int cbmem_initialize_id_size(u32 id, u64 size)
94{
Aaron Durbin41607a42015-06-09 13:54:10 -050095 const int recovery = 1;
Aaron Durbin0dff57d2015-03-05 21:18:33 -060096
Aaron Durbindfdea2a2017-08-01 10:27:10 -060097 cbmem_top_init_once();
98
Patrick Georgib6161be2019-11-29 12:27:01 +010099 imd_handle_init(&imd, cbmem_top());
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600100
Patrick Georgib6161be2019-11-29 12:27:01 +0100101 if (imd_recover(&imd))
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600102 return 1;
103
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600104 /*
105 * Lock the imd in romstage on a recovery. The assumption is that
106 * if the imd area was recovered in romstage then S3 resume path
107 * is being taken.
108 */
Kyösti Mälkkie3acc8f2019-09-13 10:49:20 +0300109 if (ENV_ROMSTAGE)
Patrick Georgib6161be2019-11-29 12:27:01 +0100110 imd_lockdown(&imd);
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600111
Lee Leahy522149c2015-05-08 11:33:55 -0700112 /* Add the specified range first */
113 if (size)
114 cbmem_add(id, size);
115
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600116 /* Complete migration to CBMEM. */
Aaron Durbin41607a42015-06-09 13:54:10 -0500117 cbmem_run_init_hooks(recovery);
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600118
Arthur Heymans54a4f172020-02-06 18:27:50 +0100119 cbmem_initialized = 1;
120
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600121 /* Recovery successful. */
122 return 0;
123}
124
125int cbmem_recovery(int is_wakeup)
126{
127 int rv = 0;
128 if (!is_wakeup)
129 cbmem_initialize_empty();
130 else
131 rv = cbmem_initialize();
132 return rv;
133}
134
135const struct cbmem_entry *cbmem_entry_add(u32 id, u64 size64)
136{
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600137 const struct imd_entry *e;
138
Patrick Georgib6161be2019-11-29 12:27:01 +0100139 e = imd_entry_find_or_add(&imd, id, size64);
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600140
141 return imd_to_cbmem(e);
142}
143
144void *cbmem_add(u32 id, u64 size)
145{
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600146 const struct imd_entry *e;
147
Patrick Georgib6161be2019-11-29 12:27:01 +0100148 e = imd_entry_find_or_add(&imd, id, size);
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600149
150 if (e == NULL)
151 return NULL;
152
Patrick Georgib6161be2019-11-29 12:27:01 +0100153 return imd_entry_at(&imd, e);
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600154}
155
156/* Retrieve a region provided a given id. */
157const struct cbmem_entry *cbmem_entry_find(u32 id)
158{
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600159 const struct imd_entry *e;
160
Patrick Georgib6161be2019-11-29 12:27:01 +0100161 e = imd_entry_find(&imd, id);
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600162
163 return imd_to_cbmem(e);
164}
165
166void *cbmem_find(u32 id)
167{
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600168 const struct imd_entry *e;
169
Patrick Georgib6161be2019-11-29 12:27:01 +0100170 e = imd_entry_find(&imd, id);
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600171
172 if (e == NULL)
173 return NULL;
174
Patrick Georgib6161be2019-11-29 12:27:01 +0100175 return imd_entry_at(&imd, e);
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600176}
177
178/* Remove a reserved region. Returns 0 on success, < 0 on error. Note: A region
179 * cannot be removed unless it was the last one added. */
180int cbmem_entry_remove(const struct cbmem_entry *entry)
181{
Patrick Georgib6161be2019-11-29 12:27:01 +0100182 return imd_entry_remove(&imd, cbmem_to_imd(entry));
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600183}
184
185u64 cbmem_entry_size(const struct cbmem_entry *entry)
186{
Anna Karas215e7fc2020-07-16 14:12:30 +0200187 return imd_entry_size(cbmem_to_imd(entry));
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600188}
189
190void *cbmem_entry_start(const struct cbmem_entry *entry)
191{
Patrick Georgib6161be2019-11-29 12:27:01 +0100192 return imd_entry_at(&imd, cbmem_to_imd(entry));
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600193}
194
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600195void cbmem_add_bootmem(void)
196{
Aaron Durbinfb532422017-08-02 10:40:25 -0600197 void *baseptr = NULL;
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600198 size_t size = 0;
199
Philipp Deppenwiese84258db2018-08-16 00:31:26 +0200200 cbmem_get_region(&baseptr, &size);
Patrick Rudolph9ab9db02018-04-05 09:14:51 +0200201 bootmem_add_range((uintptr_t)baseptr, size, BM_MEM_TABLE);
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600202}
203
Philipp Deppenwiese84258db2018-08-16 00:31:26 +0200204void cbmem_get_region(void **baseptr, size_t *size)
205{
Patrick Georgib6161be2019-11-29 12:27:01 +0100206 imd_region_used(&imd, baseptr, size);
Philipp Deppenwiese84258db2018-08-16 00:31:26 +0200207}
208
Subrata Banik42c44c22019-05-15 20:27:04 +0530209#if ENV_PAYLOAD_LOADER || (CONFIG(EARLY_CBMEM_LIST) \
Lee Leahye2422e32016-07-24 19:52:15 -0700210 && (ENV_POSTCAR || ENV_ROMSTAGE))
Aaron Durbin1ca2d862015-09-30 12:26:54 -0500211/*
212 * -fdata-sections doesn't work so well on read only strings. They all
213 * get put in the same section even though those strings may never be
214 * referenced in the final binary.
215 */
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600216void cbmem_list(void)
217{
218 static const struct imd_lookup lookup[] = { CBMEM_ID_TO_NAME_TABLE };
219
Patrick Georgib6161be2019-11-29 12:27:01 +0100220 imd_print_entries(&imd, lookup, ARRAY_SIZE(lookup));
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600221}
Aaron Durbin1ca2d862015-09-30 12:26:54 -0500222#endif
223
224void cbmem_add_records_to_cbtable(struct lb_header *header)
225{
226 struct imd_cursor cursor;
Aaron Durbin1ca2d862015-09-30 12:26:54 -0500227
Patrick Georgib6161be2019-11-29 12:27:01 +0100228 if (imd_cursor_init(&imd, &cursor))
Aaron Durbin1ca2d862015-09-30 12:26:54 -0500229 return;
230
231 while (1) {
232 const struct imd_entry *e;
233 struct lb_cbmem_entry *lbe;
234 uint32_t id;
235
236 e = imd_cursor_next(&cursor);
237
238 if (e == NULL)
239 break;
240
Anna Karas215e7fc2020-07-16 14:12:30 +0200241 id = imd_entry_id(e);
Aaron Durbin1ca2d862015-09-30 12:26:54 -0500242 /* Don't add these metadata entries. */
243 if (id == CBMEM_ID_IMD_ROOT || id == CBMEM_ID_IMD_SMALL)
244 continue;
245
246 lbe = (struct lb_cbmem_entry *)lb_new_record(header);
247 lbe->tag = LB_TAG_CBMEM_ENTRY;
248 lbe->size = sizeof(*lbe);
Patrick Georgib6161be2019-11-29 12:27:01 +0100249 lbe->address = (uintptr_t)imd_entry_at(&imd, e);
Anna Karas215e7fc2020-07-16 14:12:30 +0200250 lbe->entry_size = imd_entry_size(e);
Aaron Durbin1ca2d862015-09-30 12:26:54 -0500251 lbe->id = id;
252 }
253}