blob: 5be7dc46f5e343f839fa3d01eebc82eae0fb9736 [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
Patrick Georgib6161be2019-11-29 12:27:01 +010030static struct imd imd;
31
Arthur Heymans340e4b82019-10-23 17:25:58 +020032void *cbmem_top(void)
33{
Arthur Heymansc4c5d852019-10-29 07:32:48 +010034 if (ENV_ROMSTAGE) {
Arthur Heymans340e4b82019-10-23 17:25:58 +020035 MAYBE_STATIC_BSS void *top = NULL;
36 if (top)
37 return top;
38 top = cbmem_top_chipset();
39 return top;
40 }
Arthur Heymansc4c5d852019-10-29 07:32:48 +010041 if (ENV_POSTCAR || ENV_RAMSTAGE)
Arthur Heymans340e4b82019-10-23 17:25:58 +020042 return (void *)_cbmem_top_ptr;
43
44 dead_code();
45}
46
Aaron Durbin0dff57d2015-03-05 21:18:33 -060047static inline const struct cbmem_entry *imd_to_cbmem(const struct imd_entry *e)
48{
49 return (const struct cbmem_entry *)e;
50}
51
52static inline const struct imd_entry *cbmem_to_imd(const struct cbmem_entry *e)
53{
54 return (const struct imd_entry *)e;
55}
56
Aaron Durbin0dff57d2015-03-05 21:18:33 -060057void cbmem_initialize_empty(void)
58{
Lee Leahy522149c2015-05-08 11:33:55 -070059 cbmem_initialize_empty_id_size(0, 0);
60}
61
Aaron Durbindfdea2a2017-08-01 10:27:10 -060062static void cbmem_top_init_once(void)
63{
64 /* Call one-time hook on expected cbmem init during boot. This sequence
Kyösti Mälkki513a1a82018-06-03 12:29:50 +030065 assumes first init call is in romstage. */
66 if (!ENV_ROMSTAGE)
Aaron Durbindfdea2a2017-08-01 10:27:10 -060067 return;
68
Kyösti Mälkkif5cf60f2019-03-18 15:26:48 +020069 /* The test is only effective on X86 and when address hits UC memory. */
70 if (ENV_X86)
71 quick_ram_check_or_die((uintptr_t)cbmem_top() - sizeof(u32));
Aaron Durbindfdea2a2017-08-01 10:27:10 -060072}
73
Lee Leahy522149c2015-05-08 11:33:55 -070074void cbmem_initialize_empty_id_size(u32 id, u64 size)
75{
Aaron Durbin41607a42015-06-09 13:54:10 -050076 const int no_recovery = 0;
Aaron Durbin0dff57d2015-03-05 21:18:33 -060077
Aaron Durbindfdea2a2017-08-01 10:27:10 -060078 cbmem_top_init_once();
79
Patrick Georgib6161be2019-11-29 12:27:01 +010080 imd_handle_init(&imd, cbmem_top());
Aaron Durbin0dff57d2015-03-05 21:18:33 -060081
82 printk(BIOS_DEBUG, "CBMEM:\n");
83
Patrick Georgib6161be2019-11-29 12:27:01 +010084 if (imd_create_tiered_empty(&imd, CBMEM_ROOT_MIN_SIZE, CBMEM_LG_ALIGN,
Lee Leahy522149c2015-05-08 11:33:55 -070085 CBMEM_SM_ROOT_SIZE, CBMEM_SM_ALIGN)) {
Aaron Durbin0dff57d2015-03-05 21:18:33 -060086 printk(BIOS_DEBUG, "failed.\n");
87 return;
88 }
89
Lee Leahy522149c2015-05-08 11:33:55 -070090 /* Add the specified range first */
91 if (size)
92 cbmem_add(id, size);
93
Aaron Durbin0dff57d2015-03-05 21:18:33 -060094 /* Complete migration to CBMEM. */
Aaron Durbin41607a42015-06-09 13:54:10 -050095 cbmem_run_init_hooks(no_recovery);
Aaron Durbin0dff57d2015-03-05 21:18:33 -060096}
97
Aaron Durbin0dff57d2015-03-05 21:18:33 -060098int cbmem_initialize(void)
99{
Lee Leahy522149c2015-05-08 11:33:55 -0700100 return cbmem_initialize_id_size(0, 0);
101}
102
103int cbmem_initialize_id_size(u32 id, u64 size)
104{
Aaron Durbin41607a42015-06-09 13:54:10 -0500105 const int recovery = 1;
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600106
Aaron Durbindfdea2a2017-08-01 10:27:10 -0600107 cbmem_top_init_once();
108
Patrick Georgib6161be2019-11-29 12:27:01 +0100109 imd_handle_init(&imd, cbmem_top());
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600110
Patrick Georgib6161be2019-11-29 12:27:01 +0100111 if (imd_recover(&imd))
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600112 return 1;
113
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600114 /*
115 * Lock the imd in romstage on a recovery. The assumption is that
116 * if the imd area was recovered in romstage then S3 resume path
117 * is being taken.
118 */
Kyösti Mälkkie3acc8f2019-09-13 10:49:20 +0300119 if (ENV_ROMSTAGE)
Patrick Georgib6161be2019-11-29 12:27:01 +0100120 imd_lockdown(&imd);
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600121
Lee Leahy522149c2015-05-08 11:33:55 -0700122 /* Add the specified range first */
123 if (size)
124 cbmem_add(id, size);
125
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600126 /* Complete migration to CBMEM. */
Aaron Durbin41607a42015-06-09 13:54:10 -0500127 cbmem_run_init_hooks(recovery);
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600128
129 /* Recovery successful. */
130 return 0;
131}
132
133int cbmem_recovery(int is_wakeup)
134{
135 int rv = 0;
136 if (!is_wakeup)
137 cbmem_initialize_empty();
138 else
139 rv = cbmem_initialize();
140 return rv;
141}
142
143const struct cbmem_entry *cbmem_entry_add(u32 id, u64 size64)
144{
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600145 const struct imd_entry *e;
146
Patrick Georgib6161be2019-11-29 12:27:01 +0100147 e = imd_entry_find_or_add(&imd, id, size64);
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600148
149 return imd_to_cbmem(e);
150}
151
152void *cbmem_add(u32 id, u64 size)
153{
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600154 const struct imd_entry *e;
155
Patrick Georgib6161be2019-11-29 12:27:01 +0100156 e = imd_entry_find_or_add(&imd, id, size);
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600157
158 if (e == NULL)
159 return NULL;
160
Patrick Georgib6161be2019-11-29 12:27:01 +0100161 return imd_entry_at(&imd, e);
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600162}
163
164/* Retrieve a region provided a given id. */
165const struct cbmem_entry *cbmem_entry_find(u32 id)
166{
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600167 const struct imd_entry *e;
168
Patrick Georgib6161be2019-11-29 12:27:01 +0100169 e = imd_entry_find(&imd, id);
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600170
171 return imd_to_cbmem(e);
172}
173
174void *cbmem_find(u32 id)
175{
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600176 const struct imd_entry *e;
177
Patrick Georgib6161be2019-11-29 12:27:01 +0100178 e = imd_entry_find(&imd, id);
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600179
180 if (e == NULL)
181 return NULL;
182
Patrick Georgib6161be2019-11-29 12:27:01 +0100183 return imd_entry_at(&imd, e);
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600184}
185
186/* Remove a reserved region. Returns 0 on success, < 0 on error. Note: A region
187 * cannot be removed unless it was the last one added. */
188int cbmem_entry_remove(const struct cbmem_entry *entry)
189{
Patrick Georgib6161be2019-11-29 12:27:01 +0100190 return imd_entry_remove(&imd, cbmem_to_imd(entry));
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600191}
192
193u64 cbmem_entry_size(const struct cbmem_entry *entry)
194{
Patrick Georgib6161be2019-11-29 12:27:01 +0100195 return imd_entry_size(&imd, cbmem_to_imd(entry));
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600196}
197
198void *cbmem_entry_start(const struct cbmem_entry *entry)
199{
Patrick Georgib6161be2019-11-29 12:27:01 +0100200 return imd_entry_at(&imd, cbmem_to_imd(entry));
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600201}
202
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600203void cbmem_add_bootmem(void)
204{
Aaron Durbinfb532422017-08-02 10:40:25 -0600205 void *baseptr = NULL;
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600206 size_t size = 0;
207
Philipp Deppenwiese84258db2018-08-16 00:31:26 +0200208 cbmem_get_region(&baseptr, &size);
Patrick Rudolph9ab9db02018-04-05 09:14:51 +0200209 bootmem_add_range((uintptr_t)baseptr, size, BM_MEM_TABLE);
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600210}
211
Philipp Deppenwiese84258db2018-08-16 00:31:26 +0200212void cbmem_get_region(void **baseptr, size_t *size)
213{
Patrick Georgib6161be2019-11-29 12:27:01 +0100214 imd_region_used(&imd, baseptr, size);
Philipp Deppenwiese84258db2018-08-16 00:31:26 +0200215}
216
Subrata Banik42c44c22019-05-15 20:27:04 +0530217#if ENV_PAYLOAD_LOADER || (CONFIG(EARLY_CBMEM_LIST) \
Lee Leahye2422e32016-07-24 19:52:15 -0700218 && (ENV_POSTCAR || ENV_ROMSTAGE))
Aaron Durbin1ca2d862015-09-30 12:26:54 -0500219/*
220 * -fdata-sections doesn't work so well on read only strings. They all
221 * get put in the same section even though those strings may never be
222 * referenced in the final binary.
223 */
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600224void cbmem_list(void)
225{
226 static const struct imd_lookup lookup[] = { CBMEM_ID_TO_NAME_TABLE };
227
Patrick Georgib6161be2019-11-29 12:27:01 +0100228 imd_print_entries(&imd, lookup, ARRAY_SIZE(lookup));
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600229}
Aaron Durbin1ca2d862015-09-30 12:26:54 -0500230#endif
231
232void cbmem_add_records_to_cbtable(struct lb_header *header)
233{
234 struct imd_cursor cursor;
Aaron Durbin1ca2d862015-09-30 12:26:54 -0500235
Patrick Georgib6161be2019-11-29 12:27:01 +0100236 if (imd_cursor_init(&imd, &cursor))
Aaron Durbin1ca2d862015-09-30 12:26:54 -0500237 return;
238
239 while (1) {
240 const struct imd_entry *e;
241 struct lb_cbmem_entry *lbe;
242 uint32_t id;
243
244 e = imd_cursor_next(&cursor);
245
246 if (e == NULL)
247 break;
248
Patrick Georgib6161be2019-11-29 12:27:01 +0100249 id = imd_entry_id(&imd, e);
Aaron Durbin1ca2d862015-09-30 12:26:54 -0500250 /* Don't add these metadata entries. */
251 if (id == CBMEM_ID_IMD_ROOT || id == CBMEM_ID_IMD_SMALL)
252 continue;
253
254 lbe = (struct lb_cbmem_entry *)lb_new_record(header);
255 lbe->tag = LB_TAG_CBMEM_ENTRY;
256 lbe->size = sizeof(*lbe);
Patrick Georgib6161be2019-11-29 12:27:01 +0100257 lbe->address = (uintptr_t)imd_entry_at(&imd, e);
258 lbe->entry_size = imd_entry_size(&imd, e);
Aaron Durbin1ca2d862015-09-30 12:26:54 -0500259 lbe->id = id;
260 }
261}