blob: ba00c2e9dbefd2a0edd1ef6e99d73d919e6dd6a7 [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
16#include <bootstate.h>
17#include <bootmem.h>
18#include <console/console.h>
19#include <cbmem.h>
20#include <imd.h>
21#include <rules.h>
22#include <string.h>
23#include <stdlib.h>
24#include <arch/early_variables.h>
Aaron Durbin0dff57d2015-03-05 21:18:33 -060025
Julius Werner3c814b22016-08-19 16:20:40 -070026/*
Aaron Durbin403fdbc2017-08-02 10:57:23 -060027 * We need special handling on x86 where CAR global migration is employed. One
28 * cannot use true globals in that circumstance because CAR is where the globals
29 * are backed -- creating a circular dependency. For non CAR platforms globals
30 * are free to be used as well as any stages that are purely executing out of
31 * RAM. For CAR platforms that don't migrate globals the as-linked globals can
32 * be used, but they need special decoration using CAR_GLOBAL. That ensures
33 * proper object placement in conjunction with the linker.
34 *
35 * For the CAR global migration platforms we have to always try to partially
36 * recover CBMEM from cbmem_top() whenever we try to access it. In other
37 * environments we're not so constrained and just keep the backing imd struct
38 * in a global. This also means that we can easily tell whether CBMEM has
39 * explicitly been initialized or recovered yet on those platforms, and don't
40 * need to put the burden on board or chipset code to tell us by returning
41 * NULL from cbmem_top() before that point.
Julius Werner3c814b22016-08-19 16:20:40 -070042 */
Aaron Durbin1e9a9142016-09-16 16:23:21 -050043#define CAN_USE_GLOBALS \
Aaron Durbin403fdbc2017-08-02 10:57:23 -060044 (!IS_ENABLED(CONFIG_ARCH_X86) || ENV_RAMSTAGE || ENV_POSTCAR || \
45 IS_ENABLED(CONFIG_NO_CAR_GLOBAL_MIGRATION))
Julius Werner3c814b22016-08-19 16:20:40 -070046
Aaron Durbin0dff57d2015-03-05 21:18:33 -060047static inline struct imd *cbmem_get_imd(void)
48{
Julius Werner3c814b22016-08-19 16:20:40 -070049 if (CAN_USE_GLOBALS) {
Aaron Durbin403fdbc2017-08-02 10:57:23 -060050 static struct imd imd_cbmem CAR_GLOBAL;
Aaron Durbin0dff57d2015-03-05 21:18:33 -060051 return &imd_cbmem;
52 }
53 return NULL;
54}
55
Aaron Durbin0dff57d2015-03-05 21:18:33 -060056static inline const struct cbmem_entry *imd_to_cbmem(const struct imd_entry *e)
57{
58 return (const struct cbmem_entry *)e;
59}
60
61static inline const struct imd_entry *cbmem_to_imd(const struct cbmem_entry *e)
62{
63 return (const struct imd_entry *)e;
64}
65
66/* These are the different situations to handle:
67 * CONFIG_EARLY_CBMEM_INIT:
Lee Leahye20a3192017-03-09 16:21:34 -080068 * In ramstage cbmem_initialize() attempts a recovery of the
69 * cbmem region set up by romstage. It uses cbmem_top() as the
70 * starting point of recovery.
Aaron Durbin0dff57d2015-03-05 21:18:33 -060071 *
Lee Leahye20a3192017-03-09 16:21:34 -080072 * In romstage, similar to ramstage, cbmem_initialize() needs to
73 * attempt recovery of the cbmem area using cbmem_top() as the limit.
74 * cbmem_initialize_empty() initializes an empty cbmem area from
75 * cbmem_top();
Aaron Durbin0dff57d2015-03-05 21:18:33 -060076 *
77 */
78static struct imd *imd_init_backing(struct imd *backing)
79{
80 struct imd *imd;
81
82 imd = cbmem_get_imd();
83
84 if (imd != NULL)
85 return imd;
86
87 imd = backing;
88
89 return imd;
90}
91
92static struct imd *imd_init_backing_with_recover(struct imd *backing)
93{
94 struct imd *imd;
95
96 imd = imd_init_backing(backing);
Julius Werner3c814b22016-08-19 16:20:40 -070097 if (!CAN_USE_GLOBALS) {
98 /* Always partially recover if we can't keep track of whether
99 * we have already initialized CBMEM in this stage. */
Kyösti Mälkkie1fb0522015-05-26 00:30:10 +0300100 imd_handle_init(imd, cbmem_top());
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600101 imd_handle_init_partial_recovery(imd);
102 }
103
104 return imd;
105}
106
107void cbmem_initialize_empty(void)
108{
Lee Leahy522149c2015-05-08 11:33:55 -0700109 cbmem_initialize_empty_id_size(0, 0);
110}
111
112void cbmem_initialize_empty_id_size(u32 id, u64 size)
113{
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600114 struct imd *imd;
115 struct imd imd_backing;
Aaron Durbin41607a42015-06-09 13:54:10 -0500116 const int no_recovery = 0;
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600117
118 imd = imd_init_backing(&imd_backing);
Kyösti Mälkkie1fb0522015-05-26 00:30:10 +0300119 imd_handle_init(imd, cbmem_top());
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600120
121 printk(BIOS_DEBUG, "CBMEM:\n");
122
Lee Leahy522149c2015-05-08 11:33:55 -0700123 if (imd_create_tiered_empty(imd, CBMEM_ROOT_MIN_SIZE, CBMEM_LG_ALIGN,
124 CBMEM_SM_ROOT_SIZE, CBMEM_SM_ALIGN)) {
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600125 printk(BIOS_DEBUG, "failed.\n");
126 return;
127 }
128
Lee Leahy522149c2015-05-08 11:33:55 -0700129 /* Add the specified range first */
130 if (size)
131 cbmem_add(id, size);
132
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600133 /* Complete migration to CBMEM. */
Aaron Durbin41607a42015-06-09 13:54:10 -0500134 cbmem_run_init_hooks(no_recovery);
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600135}
136
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600137int cbmem_initialize(void)
138{
Lee Leahy522149c2015-05-08 11:33:55 -0700139 return cbmem_initialize_id_size(0, 0);
140}
141
142int cbmem_initialize_id_size(u32 id, u64 size)
143{
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600144 struct imd *imd;
145 struct imd imd_backing;
Aaron Durbin41607a42015-06-09 13:54:10 -0500146 const int recovery = 1;
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600147
148 imd = imd_init_backing(&imd_backing);
Kyösti Mälkkie1fb0522015-05-26 00:30:10 +0300149 imd_handle_init(imd, cbmem_top());
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600150
151 if (imd_recover(imd))
152 return 1;
153
154#if defined(__PRE_RAM__)
155 /*
156 * Lock the imd in romstage on a recovery. The assumption is that
157 * if the imd area was recovered in romstage then S3 resume path
158 * is being taken.
159 */
160 imd_lockdown(imd);
161#endif
162
Lee Leahy522149c2015-05-08 11:33:55 -0700163 /* Add the specified range first */
164 if (size)
165 cbmem_add(id, size);
166
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600167 /* Complete migration to CBMEM. */
Aaron Durbin41607a42015-06-09 13:54:10 -0500168 cbmem_run_init_hooks(recovery);
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600169
170 /* Recovery successful. */
171 return 0;
172}
173
174int cbmem_recovery(int is_wakeup)
175{
176 int rv = 0;
177 if (!is_wakeup)
178 cbmem_initialize_empty();
179 else
180 rv = cbmem_initialize();
181 return rv;
182}
183
184const struct cbmem_entry *cbmem_entry_add(u32 id, u64 size64)
185{
186 struct imd *imd;
187 struct imd imd_backing;
188 const struct imd_entry *e;
189
190 imd = imd_init_backing_with_recover(&imd_backing);
191
192 e = imd_entry_find_or_add(imd, id, size64);
193
194 return imd_to_cbmem(e);
195}
196
197void *cbmem_add(u32 id, u64 size)
198{
199 struct imd *imd;
200 struct imd imd_backing;
201 const struct imd_entry *e;
202
203 imd = imd_init_backing_with_recover(&imd_backing);
204
205 e = imd_entry_find_or_add(imd, id, size);
206
207 if (e == NULL)
208 return NULL;
209
210 return imd_entry_at(imd, e);
211}
212
213/* Retrieve a region provided a given id. */
214const struct cbmem_entry *cbmem_entry_find(u32 id)
215{
216 struct imd *imd;
217 struct imd imd_backing;
218 const struct imd_entry *e;
219
220 imd = imd_init_backing_with_recover(&imd_backing);
221
222 e = imd_entry_find(imd, id);
223
224 return imd_to_cbmem(e);
225}
226
227void *cbmem_find(u32 id)
228{
229 struct imd *imd;
230 struct imd imd_backing;
231 const struct imd_entry *e;
232
233 imd = imd_init_backing_with_recover(&imd_backing);
234
235 e = imd_entry_find(imd, id);
236
237 if (e == NULL)
238 return NULL;
239
240 return imd_entry_at(imd, e);
241}
242
243/* Remove a reserved region. Returns 0 on success, < 0 on error. Note: A region
244 * cannot be removed unless it was the last one added. */
245int cbmem_entry_remove(const struct cbmem_entry *entry)
246{
247 struct imd *imd;
248 struct imd imd_backing;
249
250 imd = imd_init_backing_with_recover(&imd_backing);
251
252 return imd_entry_remove(imd, cbmem_to_imd(entry));
253}
254
255u64 cbmem_entry_size(const struct cbmem_entry *entry)
256{
257 struct imd *imd;
258 struct imd imd_backing;
259
260 imd = imd_init_backing_with_recover(&imd_backing);
261
262 return imd_entry_size(imd, cbmem_to_imd(entry));
263}
264
265void *cbmem_entry_start(const struct cbmem_entry *entry)
266{
267 struct imd *imd;
268 struct imd imd_backing;
269
270 imd = imd_init_backing_with_recover(&imd_backing);
271
272 return imd_entry_at(imd, cbmem_to_imd(entry));
273}
274
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600275void cbmem_add_bootmem(void)
276{
Aaron Durbinfb532422017-08-02 10:40:25 -0600277 void *baseptr = NULL;
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600278 size_t size = 0;
279
Aaron Durbinfb532422017-08-02 10:40:25 -0600280 imd_region_used(cbmem_get_imd(), &baseptr, &size);
281 bootmem_add_range((uintptr_t)baseptr, size, LB_MEM_TABLE);
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600282}
283
Lee Leahye2422e32016-07-24 19:52:15 -0700284#if ENV_RAMSTAGE || (IS_ENABLED(CONFIG_EARLY_CBMEM_LIST) \
285 && (ENV_POSTCAR || ENV_ROMSTAGE))
Aaron Durbin1ca2d862015-09-30 12:26:54 -0500286/*
287 * -fdata-sections doesn't work so well on read only strings. They all
288 * get put in the same section even though those strings may never be
289 * referenced in the final binary.
290 */
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600291void cbmem_list(void)
292{
293 static const struct imd_lookup lookup[] = { CBMEM_ID_TO_NAME_TABLE };
Lee Leahye2422e32016-07-24 19:52:15 -0700294 struct imd *imd;
295 struct imd imd_backing;
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600296
Lee Leahye2422e32016-07-24 19:52:15 -0700297 imd = imd_init_backing_with_recover(&imd_backing);
298 imd_print_entries(imd, lookup, ARRAY_SIZE(lookup));
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600299}
Aaron Durbin1ca2d862015-09-30 12:26:54 -0500300#endif
301
302void cbmem_add_records_to_cbtable(struct lb_header *header)
303{
304 struct imd_cursor cursor;
305 struct imd *imd;
306
307 imd = cbmem_get_imd();
308
309 if (imd_cursor_init(imd, &cursor))
310 return;
311
312 while (1) {
313 const struct imd_entry *e;
314 struct lb_cbmem_entry *lbe;
315 uint32_t id;
316
317 e = imd_cursor_next(&cursor);
318
319 if (e == NULL)
320 break;
321
322 id = imd_entry_id(imd, e);
323 /* Don't add these metadata entries. */
324 if (id == CBMEM_ID_IMD_ROOT || id == CBMEM_ID_IMD_SMALL)
325 continue;
326
327 lbe = (struct lb_cbmem_entry *)lb_new_record(header);
328 lbe->tag = LB_TAG_CBMEM_ENTRY;
329 lbe->size = sizeof(*lbe);
330 lbe->address = (uintptr_t)imd_entry_at(imd, e);
331 lbe->entry_size = imd_entry_size(imd, e);
332 lbe->id = id;
333 }
334}