blob: c3204e288148281fc046b1411428c24d30bb7aac [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>
Julius Werner3c814b22016-08-19 16:20:40 -070025
Arthur Heymans340e4b82019-10-23 17:25:58 +020026/* The program loader passes on cbmem_top and the program entry point
27 has to fill in the _cbmem_top_ptr symbol based on the calling arguments. */
28uintptr_t _cbmem_top_ptr;
29
30void *cbmem_top(void)
31{
Arthur Heymansc4c5d852019-10-29 07:32:48 +010032 if (ENV_ROMSTAGE) {
Arthur Heymans340e4b82019-10-23 17:25:58 +020033 MAYBE_STATIC_BSS void *top = NULL;
34 if (top)
35 return top;
36 top = cbmem_top_chipset();
37 return top;
38 }
Arthur Heymansc4c5d852019-10-29 07:32:48 +010039 if (ENV_POSTCAR || ENV_RAMSTAGE)
Arthur Heymans340e4b82019-10-23 17:25:58 +020040 return (void *)_cbmem_top_ptr;
41
42 dead_code();
43}
44
45
Patrick Georgicd666d92019-11-29 12:14:30 +010046static struct imd imd_cbmem;
Aaron Durbin0dff57d2015-03-05 21:18:33 -060047
Aaron Durbin0dff57d2015-03-05 21:18:33 -060048static inline const struct cbmem_entry *imd_to_cbmem(const struct imd_entry *e)
49{
50 return (const struct cbmem_entry *)e;
51}
52
53static inline const struct imd_entry *cbmem_to_imd(const struct cbmem_entry *e)
54{
55 return (const struct imd_entry *)e;
56}
57
Aaron Durbin0dff57d2015-03-05 21:18:33 -060058void cbmem_initialize_empty(void)
59{
Lee Leahy522149c2015-05-08 11:33:55 -070060 cbmem_initialize_empty_id_size(0, 0);
61}
62
Aaron Durbindfdea2a2017-08-01 10:27:10 -060063static void cbmem_top_init_once(void)
64{
65 /* Call one-time hook on expected cbmem init during boot. This sequence
Kyösti Mälkki513a1a82018-06-03 12:29:50 +030066 assumes first init call is in romstage. */
67 if (!ENV_ROMSTAGE)
Aaron Durbindfdea2a2017-08-01 10:27:10 -060068 return;
69
Kyösti Mälkkif5cf60f2019-03-18 15:26:48 +020070 /* The test is only effective on X86 and when address hits UC memory. */
71 if (ENV_X86)
72 quick_ram_check_or_die((uintptr_t)cbmem_top() - sizeof(u32));
Aaron Durbindfdea2a2017-08-01 10:27:10 -060073}
74
Lee Leahy522149c2015-05-08 11:33:55 -070075void cbmem_initialize_empty_id_size(u32 id, u64 size)
76{
Patrick Georgi596947c2019-11-29 12:23:35 +010077 struct imd *imd = &imd_cbmem;
Aaron Durbin41607a42015-06-09 13:54:10 -050078 const int no_recovery = 0;
Aaron Durbin0dff57d2015-03-05 21:18:33 -060079
Aaron Durbindfdea2a2017-08-01 10:27:10 -060080 cbmem_top_init_once();
81
Kyösti Mälkkie1fb0522015-05-26 00:30:10 +030082 imd_handle_init(imd, cbmem_top());
Aaron Durbin0dff57d2015-03-05 21:18:33 -060083
84 printk(BIOS_DEBUG, "CBMEM:\n");
85
Lee Leahy522149c2015-05-08 11:33:55 -070086 if (imd_create_tiered_empty(imd, CBMEM_ROOT_MIN_SIZE, CBMEM_LG_ALIGN,
87 CBMEM_SM_ROOT_SIZE, CBMEM_SM_ALIGN)) {
Aaron Durbin0dff57d2015-03-05 21:18:33 -060088 printk(BIOS_DEBUG, "failed.\n");
89 return;
90 }
91
Lee Leahy522149c2015-05-08 11:33:55 -070092 /* Add the specified range first */
93 if (size)
94 cbmem_add(id, size);
95
Aaron Durbin0dff57d2015-03-05 21:18:33 -060096 /* Complete migration to CBMEM. */
Aaron Durbin41607a42015-06-09 13:54:10 -050097 cbmem_run_init_hooks(no_recovery);
Aaron Durbin0dff57d2015-03-05 21:18:33 -060098}
99
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600100int cbmem_initialize(void)
101{
Lee Leahy522149c2015-05-08 11:33:55 -0700102 return cbmem_initialize_id_size(0, 0);
103}
104
105int cbmem_initialize_id_size(u32 id, u64 size)
106{
Patrick Georgi596947c2019-11-29 12:23:35 +0100107 struct imd *imd = &imd_cbmem;
Aaron Durbin41607a42015-06-09 13:54:10 -0500108 const int recovery = 1;
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600109
Aaron Durbindfdea2a2017-08-01 10:27:10 -0600110 cbmem_top_init_once();
111
Kyösti Mälkkie1fb0522015-05-26 00:30:10 +0300112 imd_handle_init(imd, cbmem_top());
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600113
114 if (imd_recover(imd))
115 return 1;
116
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600117 /*
118 * Lock the imd in romstage on a recovery. The assumption is that
119 * if the imd area was recovered in romstage then S3 resume path
120 * is being taken.
121 */
Kyösti Mälkkie3acc8f2019-09-13 10:49:20 +0300122 if (ENV_ROMSTAGE)
123 imd_lockdown(imd);
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600124
Lee Leahy522149c2015-05-08 11:33:55 -0700125 /* Add the specified range first */
126 if (size)
127 cbmem_add(id, size);
128
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600129 /* Complete migration to CBMEM. */
Aaron Durbin41607a42015-06-09 13:54:10 -0500130 cbmem_run_init_hooks(recovery);
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600131
132 /* Recovery successful. */
133 return 0;
134}
135
136int cbmem_recovery(int is_wakeup)
137{
138 int rv = 0;
139 if (!is_wakeup)
140 cbmem_initialize_empty();
141 else
142 rv = cbmem_initialize();
143 return rv;
144}
145
146const struct cbmem_entry *cbmem_entry_add(u32 id, u64 size64)
147{
Patrick Georgi596947c2019-11-29 12:23:35 +0100148 struct imd *imd = &imd_cbmem;
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600149 const struct imd_entry *e;
150
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600151 e = imd_entry_find_or_add(imd, id, size64);
152
153 return imd_to_cbmem(e);
154}
155
156void *cbmem_add(u32 id, u64 size)
157{
Patrick Georgi596947c2019-11-29 12:23:35 +0100158 struct imd *imd = &imd_cbmem;
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600159 const struct imd_entry *e;
160
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600161 e = imd_entry_find_or_add(imd, id, size);
162
163 if (e == NULL)
164 return NULL;
165
166 return imd_entry_at(imd, e);
167}
168
169/* Retrieve a region provided a given id. */
170const struct cbmem_entry *cbmem_entry_find(u32 id)
171{
Patrick Georgi596947c2019-11-29 12:23:35 +0100172 struct imd *imd = &imd_cbmem;
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600173 const struct imd_entry *e;
174
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600175 e = imd_entry_find(imd, id);
176
177 return imd_to_cbmem(e);
178}
179
180void *cbmem_find(u32 id)
181{
Patrick Georgi596947c2019-11-29 12:23:35 +0100182 struct imd *imd = &imd_cbmem;
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600183 const struct imd_entry *e;
184
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600185 e = imd_entry_find(imd, id);
186
187 if (e == NULL)
188 return NULL;
189
190 return imd_entry_at(imd, e);
191}
192
193/* Remove a reserved region. Returns 0 on success, < 0 on error. Note: A region
194 * cannot be removed unless it was the last one added. */
195int cbmem_entry_remove(const struct cbmem_entry *entry)
196{
Patrick Georgi596947c2019-11-29 12:23:35 +0100197 struct imd *imd = &imd_cbmem;
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600198
199 return imd_entry_remove(imd, cbmem_to_imd(entry));
200}
201
202u64 cbmem_entry_size(const struct cbmem_entry *entry)
203{
Patrick Georgi596947c2019-11-29 12:23:35 +0100204 struct imd *imd = &imd_cbmem;
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600205
206 return imd_entry_size(imd, cbmem_to_imd(entry));
207}
208
209void *cbmem_entry_start(const struct cbmem_entry *entry)
210{
Patrick Georgi596947c2019-11-29 12:23:35 +0100211 struct imd *imd = &imd_cbmem;
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600212
213 return imd_entry_at(imd, cbmem_to_imd(entry));
214}
215
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600216void cbmem_add_bootmem(void)
217{
Aaron Durbinfb532422017-08-02 10:40:25 -0600218 void *baseptr = NULL;
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600219 size_t size = 0;
220
Philipp Deppenwiese84258db2018-08-16 00:31:26 +0200221 cbmem_get_region(&baseptr, &size);
Patrick Rudolph9ab9db02018-04-05 09:14:51 +0200222 bootmem_add_range((uintptr_t)baseptr, size, BM_MEM_TABLE);
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600223}
224
Philipp Deppenwiese84258db2018-08-16 00:31:26 +0200225void cbmem_get_region(void **baseptr, size_t *size)
226{
Patrick Georgicd666d92019-11-29 12:14:30 +0100227 imd_region_used(&imd_cbmem, baseptr, size);
Philipp Deppenwiese84258db2018-08-16 00:31:26 +0200228}
229
Subrata Banik42c44c22019-05-15 20:27:04 +0530230#if ENV_PAYLOAD_LOADER || (CONFIG(EARLY_CBMEM_LIST) \
Lee Leahye2422e32016-07-24 19:52:15 -0700231 && (ENV_POSTCAR || ENV_ROMSTAGE))
Aaron Durbin1ca2d862015-09-30 12:26:54 -0500232/*
233 * -fdata-sections doesn't work so well on read only strings. They all
234 * get put in the same section even though those strings may never be
235 * referenced in the final binary.
236 */
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600237void cbmem_list(void)
238{
239 static const struct imd_lookup lookup[] = { CBMEM_ID_TO_NAME_TABLE };
Patrick Georgi596947c2019-11-29 12:23:35 +0100240 struct imd *imd = &imd_cbmem;
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600241
Lee Leahye2422e32016-07-24 19:52:15 -0700242 imd_print_entries(imd, lookup, ARRAY_SIZE(lookup));
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600243}
Aaron Durbin1ca2d862015-09-30 12:26:54 -0500244#endif
245
246void cbmem_add_records_to_cbtable(struct lb_header *header)
247{
248 struct imd_cursor cursor;
249 struct imd *imd;
250
Patrick Georgicd666d92019-11-29 12:14:30 +0100251 imd = &imd_cbmem;
Aaron Durbin1ca2d862015-09-30 12:26:54 -0500252
253 if (imd_cursor_init(imd, &cursor))
254 return;
255
256 while (1) {
257 const struct imd_entry *e;
258 struct lb_cbmem_entry *lbe;
259 uint32_t id;
260
261 e = imd_cursor_next(&cursor);
262
263 if (e == NULL)
264 break;
265
266 id = imd_entry_id(imd, e);
267 /* Don't add these metadata entries. */
268 if (id == CBMEM_ID_IMD_ROOT || id == CBMEM_ID_IMD_SMALL)
269 continue;
270
271 lbe = (struct lb_cbmem_entry *)lb_new_record(header);
272 lbe->tag = LB_TAG_CBMEM_ENTRY;
273 lbe->size = sizeof(*lbe);
274 lbe->address = (uintptr_t)imd_entry_at(imd, e);
275 lbe->entry_size = imd_entry_size(imd, e);
276 lbe->id = id;
277 }
278}