blob: cbd4b8f8870efae7ced75a44c0da84388206b546 [file] [log] [blame]
Aaron Durbin0dff57d2015-03-05 21:18:33 -06001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2013 Google, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Aaron Durbin0dff57d2015-03-05 21:18:33 -060014 */
15
Arthur Heymans340e4b82019-10-23 17:25:58 +020016#include <assert.h>
Elyes HAOUAS0edf6a52019-10-26 18:41:47 +020017#include <boot/coreboot_tables.h>
Aaron Durbin0dff57d2015-03-05 21:18:33 -060018#include <bootstate.h>
19#include <bootmem.h>
20#include <console/console.h>
21#include <cbmem.h>
22#include <imd.h>
Kyösti Mälkkif5cf60f2019-03-18 15:26:48 +020023#include <lib.h>
Aaron Durbin0dff57d2015-03-05 21:18:33 -060024#include <stdlib.h>
25#include <arch/early_variables.h>
Aaron Durbin0dff57d2015-03-05 21:18:33 -060026
Julius Werner3c814b22016-08-19 16:20:40 -070027/*
Aaron Durbin403fdbc2017-08-02 10:57:23 -060028 * We need special handling on x86 where CAR global migration is employed. One
29 * cannot use true globals in that circumstance because CAR is where the globals
30 * are backed -- creating a circular dependency. For non CAR platforms globals
31 * are free to be used as well as any stages that are purely executing out of
32 * RAM. For CAR platforms that don't migrate globals the as-linked globals can
33 * be used, but they need special decoration using CAR_GLOBAL. That ensures
34 * proper object placement in conjunction with the linker.
35 *
36 * For the CAR global migration platforms we have to always try to partially
37 * recover CBMEM from cbmem_top() whenever we try to access it. In other
38 * environments we're not so constrained and just keep the backing imd struct
39 * in a global. This also means that we can easily tell whether CBMEM has
40 * explicitly been initialized or recovered yet on those platforms, and don't
41 * need to put the burden on board or chipset code to tell us by returning
42 * NULL from cbmem_top() before that point.
Julius Werner3c814b22016-08-19 16:20:40 -070043 */
Aaron Durbin1e9a9142016-09-16 16:23:21 -050044#define CAN_USE_GLOBALS \
Julius Wernercd49cce2019-03-05 16:53:33 -080045 (!CONFIG(ARCH_X86) || ENV_RAMSTAGE || ENV_POSTCAR || \
Kyösti Mälkki0f5e01a2019-08-09 07:11:07 +030046 !CONFIG(CAR_GLOBAL_MIGRATION))
Julius Werner3c814b22016-08-19 16:20:40 -070047
Arthur Heymans340e4b82019-10-23 17:25:58 +020048/* The program loader passes on cbmem_top and the program entry point
49 has to fill in the _cbmem_top_ptr symbol based on the calling arguments. */
50uintptr_t _cbmem_top_ptr;
51
52void *cbmem_top(void)
53{
54 if (ENV_ROMSTAGE
55 || ((ENV_POSTCAR || ENV_RAMSTAGE)
56 && !CONFIG(RAMSTAGE_CBMEM_TOP_ARG))) {
57 MAYBE_STATIC_BSS void *top = NULL;
58 if (top)
59 return top;
60 top = cbmem_top_chipset();
61 return top;
62 }
63 if ((ENV_POSTCAR || ENV_RAMSTAGE) && CONFIG(RAMSTAGE_CBMEM_TOP_ARG))
64 return (void *)_cbmem_top_ptr;
65
66 dead_code();
67}
68
69
Aaron Durbin0dff57d2015-03-05 21:18:33 -060070static inline struct imd *cbmem_get_imd(void)
71{
Julius Werner3c814b22016-08-19 16:20:40 -070072 if (CAN_USE_GLOBALS) {
Aaron Durbin403fdbc2017-08-02 10:57:23 -060073 static struct imd imd_cbmem CAR_GLOBAL;
Aaron Durbin0dff57d2015-03-05 21:18:33 -060074 return &imd_cbmem;
75 }
76 return NULL;
77}
78
Aaron Durbin0dff57d2015-03-05 21:18:33 -060079static inline const struct cbmem_entry *imd_to_cbmem(const struct imd_entry *e)
80{
81 return (const struct cbmem_entry *)e;
82}
83
84static inline const struct imd_entry *cbmem_to_imd(const struct cbmem_entry *e)
85{
86 return (const struct imd_entry *)e;
87}
88
89/* These are the different situations to handle:
Kyösti Mälkki513a1a82018-06-03 12:29:50 +030090 *
Lee Leahye20a3192017-03-09 16:21:34 -080091 * In ramstage cbmem_initialize() attempts a recovery of the
92 * cbmem region set up by romstage. It uses cbmem_top() as the
93 * starting point of recovery.
Aaron Durbin0dff57d2015-03-05 21:18:33 -060094 *
Lee Leahye20a3192017-03-09 16:21:34 -080095 * In romstage, similar to ramstage, cbmem_initialize() needs to
96 * attempt recovery of the cbmem area using cbmem_top() as the limit.
97 * cbmem_initialize_empty() initializes an empty cbmem area from
98 * cbmem_top();
Aaron Durbin0dff57d2015-03-05 21:18:33 -060099 *
100 */
101static struct imd *imd_init_backing(struct imd *backing)
102{
103 struct imd *imd;
104
105 imd = cbmem_get_imd();
106
107 if (imd != NULL)
108 return imd;
109
110 imd = backing;
111
112 return imd;
113}
114
115static struct imd *imd_init_backing_with_recover(struct imd *backing)
116{
117 struct imd *imd;
118
119 imd = imd_init_backing(backing);
Julius Werner3c814b22016-08-19 16:20:40 -0700120 if (!CAN_USE_GLOBALS) {
121 /* Always partially recover if we can't keep track of whether
122 * we have already initialized CBMEM in this stage. */
Kyösti Mälkkie1fb0522015-05-26 00:30:10 +0300123 imd_handle_init(imd, cbmem_top());
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600124 imd_handle_init_partial_recovery(imd);
125 }
126
127 return imd;
128}
129
130void cbmem_initialize_empty(void)
131{
Lee Leahy522149c2015-05-08 11:33:55 -0700132 cbmem_initialize_empty_id_size(0, 0);
133}
134
Aaron Durbin64031672018-04-21 14:45:32 -0600135void __weak cbmem_top_init(void)
Aaron Durbindfdea2a2017-08-01 10:27:10 -0600136{
137}
138
139static void cbmem_top_init_once(void)
140{
141 /* Call one-time hook on expected cbmem init during boot. This sequence
Kyösti Mälkki513a1a82018-06-03 12:29:50 +0300142 assumes first init call is in romstage. */
143 if (!ENV_ROMSTAGE)
Aaron Durbindfdea2a2017-08-01 10:27:10 -0600144 return;
145
146 cbmem_top_init();
Kyösti Mälkkif5cf60f2019-03-18 15:26:48 +0200147
148 /* The test is only effective on X86 and when address hits UC memory. */
149 if (ENV_X86)
150 quick_ram_check_or_die((uintptr_t)cbmem_top() - sizeof(u32));
Aaron Durbindfdea2a2017-08-01 10:27:10 -0600151}
152
Lee Leahy522149c2015-05-08 11:33:55 -0700153void cbmem_initialize_empty_id_size(u32 id, u64 size)
154{
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600155 struct imd *imd;
156 struct imd imd_backing;
Aaron Durbin41607a42015-06-09 13:54:10 -0500157 const int no_recovery = 0;
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600158
Aaron Durbindfdea2a2017-08-01 10:27:10 -0600159 cbmem_top_init_once();
160
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600161 imd = imd_init_backing(&imd_backing);
Kyösti Mälkkie1fb0522015-05-26 00:30:10 +0300162 imd_handle_init(imd, cbmem_top());
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600163
164 printk(BIOS_DEBUG, "CBMEM:\n");
165
Lee Leahy522149c2015-05-08 11:33:55 -0700166 if (imd_create_tiered_empty(imd, CBMEM_ROOT_MIN_SIZE, CBMEM_LG_ALIGN,
167 CBMEM_SM_ROOT_SIZE, CBMEM_SM_ALIGN)) {
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600168 printk(BIOS_DEBUG, "failed.\n");
169 return;
170 }
171
Lee Leahy522149c2015-05-08 11:33:55 -0700172 /* Add the specified range first */
173 if (size)
174 cbmem_add(id, size);
175
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600176 /* Complete migration to CBMEM. */
Aaron Durbin41607a42015-06-09 13:54:10 -0500177 cbmem_run_init_hooks(no_recovery);
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600178}
179
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600180int cbmem_initialize(void)
181{
Lee Leahy522149c2015-05-08 11:33:55 -0700182 return cbmem_initialize_id_size(0, 0);
183}
184
185int cbmem_initialize_id_size(u32 id, u64 size)
186{
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600187 struct imd *imd;
188 struct imd imd_backing;
Aaron Durbin41607a42015-06-09 13:54:10 -0500189 const int recovery = 1;
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600190
Aaron Durbindfdea2a2017-08-01 10:27:10 -0600191 cbmem_top_init_once();
192
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600193 imd = imd_init_backing(&imd_backing);
Kyösti Mälkkie1fb0522015-05-26 00:30:10 +0300194 imd_handle_init(imd, cbmem_top());
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600195
196 if (imd_recover(imd))
197 return 1;
198
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600199 /*
200 * Lock the imd in romstage on a recovery. The assumption is that
201 * if the imd area was recovered in romstage then S3 resume path
202 * is being taken.
203 */
Kyösti Mälkkie3acc8f2019-09-13 10:49:20 +0300204 if (ENV_ROMSTAGE)
205 imd_lockdown(imd);
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600206
Lee Leahy522149c2015-05-08 11:33:55 -0700207 /* Add the specified range first */
208 if (size)
209 cbmem_add(id, size);
210
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600211 /* Complete migration to CBMEM. */
Aaron Durbin41607a42015-06-09 13:54:10 -0500212 cbmem_run_init_hooks(recovery);
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600213
214 /* Recovery successful. */
215 return 0;
216}
217
218int cbmem_recovery(int is_wakeup)
219{
220 int rv = 0;
221 if (!is_wakeup)
222 cbmem_initialize_empty();
223 else
224 rv = cbmem_initialize();
225 return rv;
226}
227
228const struct cbmem_entry *cbmem_entry_add(u32 id, u64 size64)
229{
230 struct imd *imd;
231 struct imd imd_backing;
232 const struct imd_entry *e;
233
234 imd = imd_init_backing_with_recover(&imd_backing);
235
236 e = imd_entry_find_or_add(imd, id, size64);
237
238 return imd_to_cbmem(e);
239}
240
241void *cbmem_add(u32 id, u64 size)
242{
243 struct imd *imd;
244 struct imd imd_backing;
245 const struct imd_entry *e;
246
247 imd = imd_init_backing_with_recover(&imd_backing);
248
249 e = imd_entry_find_or_add(imd, id, size);
250
251 if (e == NULL)
252 return NULL;
253
254 return imd_entry_at(imd, e);
255}
256
257/* Retrieve a region provided a given id. */
258const struct cbmem_entry *cbmem_entry_find(u32 id)
259{
260 struct imd *imd;
261 struct imd imd_backing;
262 const struct imd_entry *e;
263
264 imd = imd_init_backing_with_recover(&imd_backing);
265
266 e = imd_entry_find(imd, id);
267
268 return imd_to_cbmem(e);
269}
270
271void *cbmem_find(u32 id)
272{
273 struct imd *imd;
274 struct imd imd_backing;
275 const struct imd_entry *e;
276
277 imd = imd_init_backing_with_recover(&imd_backing);
278
279 e = imd_entry_find(imd, id);
280
281 if (e == NULL)
282 return NULL;
283
284 return imd_entry_at(imd, e);
285}
286
287/* Remove a reserved region. Returns 0 on success, < 0 on error. Note: A region
288 * cannot be removed unless it was the last one added. */
289int cbmem_entry_remove(const struct cbmem_entry *entry)
290{
291 struct imd *imd;
292 struct imd imd_backing;
293
294 imd = imd_init_backing_with_recover(&imd_backing);
295
296 return imd_entry_remove(imd, cbmem_to_imd(entry));
297}
298
299u64 cbmem_entry_size(const struct cbmem_entry *entry)
300{
301 struct imd *imd;
302 struct imd imd_backing;
303
304 imd = imd_init_backing_with_recover(&imd_backing);
305
306 return imd_entry_size(imd, cbmem_to_imd(entry));
307}
308
309void *cbmem_entry_start(const struct cbmem_entry *entry)
310{
311 struct imd *imd;
312 struct imd imd_backing;
313
314 imd = imd_init_backing_with_recover(&imd_backing);
315
316 return imd_entry_at(imd, cbmem_to_imd(entry));
317}
318
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600319void cbmem_add_bootmem(void)
320{
Aaron Durbinfb532422017-08-02 10:40:25 -0600321 void *baseptr = NULL;
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600322 size_t size = 0;
323
Philipp Deppenwiese84258db2018-08-16 00:31:26 +0200324 cbmem_get_region(&baseptr, &size);
Patrick Rudolph9ab9db02018-04-05 09:14:51 +0200325 bootmem_add_range((uintptr_t)baseptr, size, BM_MEM_TABLE);
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600326}
327
Philipp Deppenwiese84258db2018-08-16 00:31:26 +0200328void cbmem_get_region(void **baseptr, size_t *size)
329{
330 imd_region_used(cbmem_get_imd(), baseptr, size);
331}
332
Subrata Banik42c44c22019-05-15 20:27:04 +0530333#if ENV_PAYLOAD_LOADER || (CONFIG(EARLY_CBMEM_LIST) \
Lee Leahye2422e32016-07-24 19:52:15 -0700334 && (ENV_POSTCAR || ENV_ROMSTAGE))
Aaron Durbin1ca2d862015-09-30 12:26:54 -0500335/*
336 * -fdata-sections doesn't work so well on read only strings. They all
337 * get put in the same section even though those strings may never be
338 * referenced in the final binary.
339 */
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600340void cbmem_list(void)
341{
342 static const struct imd_lookup lookup[] = { CBMEM_ID_TO_NAME_TABLE };
Lee Leahye2422e32016-07-24 19:52:15 -0700343 struct imd *imd;
344 struct imd imd_backing;
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600345
Lee Leahye2422e32016-07-24 19:52:15 -0700346 imd = imd_init_backing_with_recover(&imd_backing);
347 imd_print_entries(imd, lookup, ARRAY_SIZE(lookup));
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600348}
Aaron Durbin1ca2d862015-09-30 12:26:54 -0500349#endif
350
351void cbmem_add_records_to_cbtable(struct lb_header *header)
352{
353 struct imd_cursor cursor;
354 struct imd *imd;
355
356 imd = cbmem_get_imd();
357
358 if (imd_cursor_init(imd, &cursor))
359 return;
360
361 while (1) {
362 const struct imd_entry *e;
363 struct lb_cbmem_entry *lbe;
364 uint32_t id;
365
366 e = imd_cursor_next(&cursor);
367
368 if (e == NULL)
369 break;
370
371 id = imd_entry_id(imd, e);
372 /* Don't add these metadata entries. */
373 if (id == CBMEM_ID_IMD_ROOT || id == CBMEM_ID_IMD_SMALL)
374 continue;
375
376 lbe = (struct lb_cbmem_entry *)lb_new_record(header);
377 lbe->tag = LB_TAG_CBMEM_ENTRY;
378 lbe->size = sizeof(*lbe);
379 lbe->address = (uintptr_t)imd_entry_at(imd, e);
380 lbe->entry_size = imd_entry_size(imd, e);
381 lbe->id = id;
382 }
383}