blob: ed98947fccd78b9ae697e2ee82bc32cc50b7b447 [file] [log] [blame]
Aaron Durbin0dff57d2015-03-05 21:18:33 -06001/*
2 * This file is part of the coreboot project.
3 *
Aaron Durbin0dff57d2015-03-05 21:18:33 -06004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Aaron Durbin0dff57d2015-03-05 21:18:33 -060013 */
14
Arthur Heymans340e4b82019-10-23 17:25:58 +020015#include <assert.h>
Elyes HAOUAS0edf6a52019-10-26 18:41:47 +020016#include <boot/coreboot_tables.h>
Aaron Durbin0dff57d2015-03-05 21:18:33 -060017#include <bootstate.h>
18#include <bootmem.h>
19#include <console/console.h>
20#include <cbmem.h>
21#include <imd.h>
Kyösti Mälkkif5cf60f2019-03-18 15:26:48 +020022#include <lib.h>
Aaron Durbin0dff57d2015-03-05 21:18:33 -060023#include <stdlib.h>
Julius Werner3c814b22016-08-19 16:20:40 -070024
Arthur Heymans340e4b82019-10-23 17:25:58 +020025/* The program loader passes on cbmem_top and the program entry point
26 has to fill in the _cbmem_top_ptr symbol based on the calling arguments. */
27uintptr_t _cbmem_top_ptr;
28
Patrick Georgib6161be2019-11-29 12:27:01 +010029static struct imd imd;
30
Arthur Heymans340e4b82019-10-23 17:25:58 +020031void *cbmem_top(void)
32{
Arthur Heymansc4c5d852019-10-29 07:32:48 +010033 if (ENV_ROMSTAGE) {
Arthur Heymans340e4b82019-10-23 17:25:58 +020034 MAYBE_STATIC_BSS void *top = NULL;
35 if (top)
36 return top;
37 top = cbmem_top_chipset();
38 return top;
39 }
Arthur Heymansc4c5d852019-10-29 07:32:48 +010040 if (ENV_POSTCAR || ENV_RAMSTAGE)
Arthur Heymans340e4b82019-10-23 17:25:58 +020041 return (void *)_cbmem_top_ptr;
42
43 dead_code();
44}
45
Aaron Durbin0dff57d2015-03-05 21:18:33 -060046static inline const struct cbmem_entry *imd_to_cbmem(const struct imd_entry *e)
47{
48 return (const struct cbmem_entry *)e;
49}
50
51static inline const struct imd_entry *cbmem_to_imd(const struct cbmem_entry *e)
52{
53 return (const struct imd_entry *)e;
54}
55
Aaron Durbin0dff57d2015-03-05 21:18:33 -060056void cbmem_initialize_empty(void)
57{
Lee Leahy522149c2015-05-08 11:33:55 -070058 cbmem_initialize_empty_id_size(0, 0);
59}
60
Aaron Durbindfdea2a2017-08-01 10:27:10 -060061static void cbmem_top_init_once(void)
62{
63 /* Call one-time hook on expected cbmem init during boot. This sequence
Kyösti Mälkki513a1a82018-06-03 12:29:50 +030064 assumes first init call is in romstage. */
65 if (!ENV_ROMSTAGE)
Aaron Durbindfdea2a2017-08-01 10:27:10 -060066 return;
67
Kyösti Mälkkif5cf60f2019-03-18 15:26:48 +020068 /* The test is only effective on X86 and when address hits UC memory. */
69 if (ENV_X86)
70 quick_ram_check_or_die((uintptr_t)cbmem_top() - sizeof(u32));
Aaron Durbindfdea2a2017-08-01 10:27:10 -060071}
72
Lee Leahy522149c2015-05-08 11:33:55 -070073void cbmem_initialize_empty_id_size(u32 id, u64 size)
74{
Aaron Durbin41607a42015-06-09 13:54:10 -050075 const int no_recovery = 0;
Aaron Durbin0dff57d2015-03-05 21:18:33 -060076
Aaron Durbindfdea2a2017-08-01 10:27:10 -060077 cbmem_top_init_once();
78
Patrick Georgib6161be2019-11-29 12:27:01 +010079 imd_handle_init(&imd, cbmem_top());
Aaron Durbin0dff57d2015-03-05 21:18:33 -060080
81 printk(BIOS_DEBUG, "CBMEM:\n");
82
Patrick Georgib6161be2019-11-29 12:27:01 +010083 if (imd_create_tiered_empty(&imd, CBMEM_ROOT_MIN_SIZE, CBMEM_LG_ALIGN,
Lee Leahy522149c2015-05-08 11:33:55 -070084 CBMEM_SM_ROOT_SIZE, CBMEM_SM_ALIGN)) {
Aaron Durbin0dff57d2015-03-05 21:18:33 -060085 printk(BIOS_DEBUG, "failed.\n");
86 return;
87 }
88
Lee Leahy522149c2015-05-08 11:33:55 -070089 /* Add the specified range first */
90 if (size)
91 cbmem_add(id, size);
92
Aaron Durbin0dff57d2015-03-05 21:18:33 -060093 /* Complete migration to CBMEM. */
Aaron Durbin41607a42015-06-09 13:54:10 -050094 cbmem_run_init_hooks(no_recovery);
Aaron Durbin0dff57d2015-03-05 21:18:33 -060095}
96
Aaron Durbin0dff57d2015-03-05 21:18:33 -060097int cbmem_initialize(void)
98{
Lee Leahy522149c2015-05-08 11:33:55 -070099 return cbmem_initialize_id_size(0, 0);
100}
101
102int cbmem_initialize_id_size(u32 id, u64 size)
103{
Aaron Durbin41607a42015-06-09 13:54:10 -0500104 const int recovery = 1;
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600105
Aaron Durbindfdea2a2017-08-01 10:27:10 -0600106 cbmem_top_init_once();
107
Patrick Georgib6161be2019-11-29 12:27:01 +0100108 imd_handle_init(&imd, cbmem_top());
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600109
Patrick Georgib6161be2019-11-29 12:27:01 +0100110 if (imd_recover(&imd))
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600111 return 1;
112
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600113 /*
114 * Lock the imd in romstage on a recovery. The assumption is that
115 * if the imd area was recovered in romstage then S3 resume path
116 * is being taken.
117 */
Kyösti Mälkkie3acc8f2019-09-13 10:49:20 +0300118 if (ENV_ROMSTAGE)
Patrick Georgib6161be2019-11-29 12:27:01 +0100119 imd_lockdown(&imd);
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600120
Lee Leahy522149c2015-05-08 11:33:55 -0700121 /* Add the specified range first */
122 if (size)
123 cbmem_add(id, size);
124
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600125 /* Complete migration to CBMEM. */
Aaron Durbin41607a42015-06-09 13:54:10 -0500126 cbmem_run_init_hooks(recovery);
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600127
128 /* Recovery successful. */
129 return 0;
130}
131
132int cbmem_recovery(int is_wakeup)
133{
134 int rv = 0;
135 if (!is_wakeup)
136 cbmem_initialize_empty();
137 else
138 rv = cbmem_initialize();
139 return rv;
140}
141
142const struct cbmem_entry *cbmem_entry_add(u32 id, u64 size64)
143{
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600144 const struct imd_entry *e;
145
Patrick Georgib6161be2019-11-29 12:27:01 +0100146 e = imd_entry_find_or_add(&imd, id, size64);
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600147
148 return imd_to_cbmem(e);
149}
150
151void *cbmem_add(u32 id, u64 size)
152{
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600153 const struct imd_entry *e;
154
Patrick Georgib6161be2019-11-29 12:27:01 +0100155 e = imd_entry_find_or_add(&imd, id, size);
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600156
157 if (e == NULL)
158 return NULL;
159
Patrick Georgib6161be2019-11-29 12:27:01 +0100160 return imd_entry_at(&imd, e);
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600161}
162
163/* Retrieve a region provided a given id. */
164const struct cbmem_entry *cbmem_entry_find(u32 id)
165{
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600166 const struct imd_entry *e;
167
Patrick Georgib6161be2019-11-29 12:27:01 +0100168 e = imd_entry_find(&imd, id);
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600169
170 return imd_to_cbmem(e);
171}
172
173void *cbmem_find(u32 id)
174{
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600175 const struct imd_entry *e;
176
Patrick Georgib6161be2019-11-29 12:27:01 +0100177 e = imd_entry_find(&imd, id);
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600178
179 if (e == NULL)
180 return NULL;
181
Patrick Georgib6161be2019-11-29 12:27:01 +0100182 return imd_entry_at(&imd, e);
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600183}
184
185/* Remove a reserved region. Returns 0 on success, < 0 on error. Note: A region
186 * cannot be removed unless it was the last one added. */
187int cbmem_entry_remove(const struct cbmem_entry *entry)
188{
Patrick Georgib6161be2019-11-29 12:27:01 +0100189 return imd_entry_remove(&imd, cbmem_to_imd(entry));
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600190}
191
192u64 cbmem_entry_size(const struct cbmem_entry *entry)
193{
Patrick Georgib6161be2019-11-29 12:27:01 +0100194 return imd_entry_size(&imd, cbmem_to_imd(entry));
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600195}
196
197void *cbmem_entry_start(const struct cbmem_entry *entry)
198{
Patrick Georgib6161be2019-11-29 12:27:01 +0100199 return imd_entry_at(&imd, cbmem_to_imd(entry));
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600200}
201
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600202void cbmem_add_bootmem(void)
203{
Aaron Durbinfb532422017-08-02 10:40:25 -0600204 void *baseptr = NULL;
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600205 size_t size = 0;
206
Philipp Deppenwiese84258db2018-08-16 00:31:26 +0200207 cbmem_get_region(&baseptr, &size);
Patrick Rudolph9ab9db02018-04-05 09:14:51 +0200208 bootmem_add_range((uintptr_t)baseptr, size, BM_MEM_TABLE);
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600209}
210
Philipp Deppenwiese84258db2018-08-16 00:31:26 +0200211void cbmem_get_region(void **baseptr, size_t *size)
212{
Patrick Georgib6161be2019-11-29 12:27:01 +0100213 imd_region_used(&imd, baseptr, size);
Philipp Deppenwiese84258db2018-08-16 00:31:26 +0200214}
215
Subrata Banik42c44c22019-05-15 20:27:04 +0530216#if ENV_PAYLOAD_LOADER || (CONFIG(EARLY_CBMEM_LIST) \
Lee Leahye2422e32016-07-24 19:52:15 -0700217 && (ENV_POSTCAR || ENV_ROMSTAGE))
Aaron Durbin1ca2d862015-09-30 12:26:54 -0500218/*
219 * -fdata-sections doesn't work so well on read only strings. They all
220 * get put in the same section even though those strings may never be
221 * referenced in the final binary.
222 */
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600223void cbmem_list(void)
224{
225 static const struct imd_lookup lookup[] = { CBMEM_ID_TO_NAME_TABLE };
226
Patrick Georgib6161be2019-11-29 12:27:01 +0100227 imd_print_entries(&imd, lookup, ARRAY_SIZE(lookup));
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600228}
Aaron Durbin1ca2d862015-09-30 12:26:54 -0500229#endif
230
231void cbmem_add_records_to_cbtable(struct lb_header *header)
232{
233 struct imd_cursor cursor;
Aaron Durbin1ca2d862015-09-30 12:26:54 -0500234
Patrick Georgib6161be2019-11-29 12:27:01 +0100235 if (imd_cursor_init(&imd, &cursor))
Aaron Durbin1ca2d862015-09-30 12:26:54 -0500236 return;
237
238 while (1) {
239 const struct imd_entry *e;
240 struct lb_cbmem_entry *lbe;
241 uint32_t id;
242
243 e = imd_cursor_next(&cursor);
244
245 if (e == NULL)
246 break;
247
Patrick Georgib6161be2019-11-29 12:27:01 +0100248 id = imd_entry_id(&imd, e);
Aaron Durbin1ca2d862015-09-30 12:26:54 -0500249 /* Don't add these metadata entries. */
250 if (id == CBMEM_ID_IMD_ROOT || id == CBMEM_ID_IMD_SMALL)
251 continue;
252
253 lbe = (struct lb_cbmem_entry *)lb_new_record(header);
254 lbe->tag = LB_TAG_CBMEM_ENTRY;
255 lbe->size = sizeof(*lbe);
Patrick Georgib6161be2019-11-29 12:27:01 +0100256 lbe->address = (uintptr_t)imd_entry_at(&imd, e);
257 lbe->entry_size = imd_entry_size(&imd, e);
Aaron Durbin1ca2d862015-09-30 12:26:54 -0500258 lbe->id = id;
259 }
260}