Angel Pons | 118a9c7 | 2020-04-02 23:48:34 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | /* This file is part of the coreboot project. */ |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 3 | |
Arthur Heymans | 340e4b8 | 2019-10-23 17:25:58 +0200 | [diff] [blame] | 4 | #include <assert.h> |
Elyes HAOUAS | 0edf6a5 | 2019-10-26 18:41:47 +0200 | [diff] [blame] | 5 | #include <boot/coreboot_tables.h> |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 6 | #include <bootstate.h> |
| 7 | #include <bootmem.h> |
| 8 | #include <console/console.h> |
| 9 | #include <cbmem.h> |
| 10 | #include <imd.h> |
Kyösti Mälkki | f5cf60f | 2019-03-18 15:26:48 +0200 | [diff] [blame] | 11 | #include <lib.h> |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 12 | #include <stdlib.h> |
Julius Werner | 3c814b2 | 2016-08-19 16:20:40 -0700 | [diff] [blame] | 13 | |
Arthur Heymans | 340e4b8 | 2019-10-23 17:25:58 +0200 | [diff] [blame] | 14 | /* The program loader passes on cbmem_top and the program entry point |
| 15 | has to fill in the _cbmem_top_ptr symbol based on the calling arguments. */ |
| 16 | uintptr_t _cbmem_top_ptr; |
| 17 | |
Patrick Georgi | b6161be | 2019-11-29 12:27:01 +0100 | [diff] [blame] | 18 | static struct imd imd; |
| 19 | |
Arthur Heymans | 340e4b8 | 2019-10-23 17:25:58 +0200 | [diff] [blame] | 20 | void *cbmem_top(void) |
| 21 | { |
Arthur Heymans | c4c5d85 | 2019-10-29 07:32:48 +0100 | [diff] [blame] | 22 | if (ENV_ROMSTAGE) { |
Arthur Heymans | 340e4b8 | 2019-10-23 17:25:58 +0200 | [diff] [blame] | 23 | MAYBE_STATIC_BSS void *top = NULL; |
| 24 | if (top) |
| 25 | return top; |
| 26 | top = cbmem_top_chipset(); |
| 27 | return top; |
| 28 | } |
Arthur Heymans | c4c5d85 | 2019-10-29 07:32:48 +0100 | [diff] [blame] | 29 | if (ENV_POSTCAR || ENV_RAMSTAGE) |
Arthur Heymans | 340e4b8 | 2019-10-23 17:25:58 +0200 | [diff] [blame] | 30 | return (void *)_cbmem_top_ptr; |
| 31 | |
| 32 | dead_code(); |
| 33 | } |
| 34 | |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 35 | static inline const struct cbmem_entry *imd_to_cbmem(const struct imd_entry *e) |
| 36 | { |
| 37 | return (const struct cbmem_entry *)e; |
| 38 | } |
| 39 | |
| 40 | static inline const struct imd_entry *cbmem_to_imd(const struct cbmem_entry *e) |
| 41 | { |
| 42 | return (const struct imd_entry *)e; |
| 43 | } |
| 44 | |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 45 | void cbmem_initialize_empty(void) |
| 46 | { |
Lee Leahy | 522149c | 2015-05-08 11:33:55 -0700 | [diff] [blame] | 47 | cbmem_initialize_empty_id_size(0, 0); |
| 48 | } |
| 49 | |
Aaron Durbin | dfdea2a | 2017-08-01 10:27:10 -0600 | [diff] [blame] | 50 | static void cbmem_top_init_once(void) |
| 51 | { |
| 52 | /* Call one-time hook on expected cbmem init during boot. This sequence |
Kyösti Mälkki | 513a1a8 | 2018-06-03 12:29:50 +0300 | [diff] [blame] | 53 | assumes first init call is in romstage. */ |
| 54 | if (!ENV_ROMSTAGE) |
Aaron Durbin | dfdea2a | 2017-08-01 10:27:10 -0600 | [diff] [blame] | 55 | return; |
| 56 | |
Kyösti Mälkki | f5cf60f | 2019-03-18 15:26:48 +0200 | [diff] [blame] | 57 | /* The test is only effective on X86 and when address hits UC memory. */ |
| 58 | if (ENV_X86) |
| 59 | quick_ram_check_or_die((uintptr_t)cbmem_top() - sizeof(u32)); |
Aaron Durbin | dfdea2a | 2017-08-01 10:27:10 -0600 | [diff] [blame] | 60 | } |
| 61 | |
Lee Leahy | 522149c | 2015-05-08 11:33:55 -0700 | [diff] [blame] | 62 | void cbmem_initialize_empty_id_size(u32 id, u64 size) |
| 63 | { |
Aaron Durbin | 41607a4 | 2015-06-09 13:54:10 -0500 | [diff] [blame] | 64 | const int no_recovery = 0; |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 65 | |
Aaron Durbin | dfdea2a | 2017-08-01 10:27:10 -0600 | [diff] [blame] | 66 | cbmem_top_init_once(); |
| 67 | |
Patrick Georgi | b6161be | 2019-11-29 12:27:01 +0100 | [diff] [blame] | 68 | imd_handle_init(&imd, cbmem_top()); |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 69 | |
| 70 | printk(BIOS_DEBUG, "CBMEM:\n"); |
| 71 | |
Patrick Georgi | b6161be | 2019-11-29 12:27:01 +0100 | [diff] [blame] | 72 | if (imd_create_tiered_empty(&imd, CBMEM_ROOT_MIN_SIZE, CBMEM_LG_ALIGN, |
Lee Leahy | 522149c | 2015-05-08 11:33:55 -0700 | [diff] [blame] | 73 | CBMEM_SM_ROOT_SIZE, CBMEM_SM_ALIGN)) { |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 74 | printk(BIOS_DEBUG, "failed.\n"); |
| 75 | return; |
| 76 | } |
| 77 | |
Lee Leahy | 522149c | 2015-05-08 11:33:55 -0700 | [diff] [blame] | 78 | /* Add the specified range first */ |
| 79 | if (size) |
| 80 | cbmem_add(id, size); |
| 81 | |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 82 | /* Complete migration to CBMEM. */ |
Aaron Durbin | 41607a4 | 2015-06-09 13:54:10 -0500 | [diff] [blame] | 83 | cbmem_run_init_hooks(no_recovery); |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 84 | } |
| 85 | |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 86 | int cbmem_initialize(void) |
| 87 | { |
Lee Leahy | 522149c | 2015-05-08 11:33:55 -0700 | [diff] [blame] | 88 | return cbmem_initialize_id_size(0, 0); |
| 89 | } |
| 90 | |
| 91 | int cbmem_initialize_id_size(u32 id, u64 size) |
| 92 | { |
Aaron Durbin | 41607a4 | 2015-06-09 13:54:10 -0500 | [diff] [blame] | 93 | const int recovery = 1; |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 94 | |
Aaron Durbin | dfdea2a | 2017-08-01 10:27:10 -0600 | [diff] [blame] | 95 | cbmem_top_init_once(); |
| 96 | |
Patrick Georgi | b6161be | 2019-11-29 12:27:01 +0100 | [diff] [blame] | 97 | imd_handle_init(&imd, cbmem_top()); |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 98 | |
Patrick Georgi | b6161be | 2019-11-29 12:27:01 +0100 | [diff] [blame] | 99 | if (imd_recover(&imd)) |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 100 | return 1; |
| 101 | |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 102 | /* |
| 103 | * Lock the imd in romstage on a recovery. The assumption is that |
| 104 | * if the imd area was recovered in romstage then S3 resume path |
| 105 | * is being taken. |
| 106 | */ |
Kyösti Mälkki | e3acc8f | 2019-09-13 10:49:20 +0300 | [diff] [blame] | 107 | if (ENV_ROMSTAGE) |
Patrick Georgi | b6161be | 2019-11-29 12:27:01 +0100 | [diff] [blame] | 108 | imd_lockdown(&imd); |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 109 | |
Lee Leahy | 522149c | 2015-05-08 11:33:55 -0700 | [diff] [blame] | 110 | /* Add the specified range first */ |
| 111 | if (size) |
| 112 | cbmem_add(id, size); |
| 113 | |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 114 | /* Complete migration to CBMEM. */ |
Aaron Durbin | 41607a4 | 2015-06-09 13:54:10 -0500 | [diff] [blame] | 115 | cbmem_run_init_hooks(recovery); |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 116 | |
| 117 | /* Recovery successful. */ |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | int cbmem_recovery(int is_wakeup) |
| 122 | { |
| 123 | int rv = 0; |
| 124 | if (!is_wakeup) |
| 125 | cbmem_initialize_empty(); |
| 126 | else |
| 127 | rv = cbmem_initialize(); |
| 128 | return rv; |
| 129 | } |
| 130 | |
| 131 | const struct cbmem_entry *cbmem_entry_add(u32 id, u64 size64) |
| 132 | { |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 133 | const struct imd_entry *e; |
| 134 | |
Patrick Georgi | b6161be | 2019-11-29 12:27:01 +0100 | [diff] [blame] | 135 | e = imd_entry_find_or_add(&imd, id, size64); |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 136 | |
| 137 | return imd_to_cbmem(e); |
| 138 | } |
| 139 | |
| 140 | void *cbmem_add(u32 id, u64 size) |
| 141 | { |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 142 | const struct imd_entry *e; |
| 143 | |
Patrick Georgi | b6161be | 2019-11-29 12:27:01 +0100 | [diff] [blame] | 144 | e = imd_entry_find_or_add(&imd, id, size); |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 145 | |
| 146 | if (e == NULL) |
| 147 | return NULL; |
| 148 | |
Patrick Georgi | b6161be | 2019-11-29 12:27:01 +0100 | [diff] [blame] | 149 | return imd_entry_at(&imd, e); |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /* Retrieve a region provided a given id. */ |
| 153 | const struct cbmem_entry *cbmem_entry_find(u32 id) |
| 154 | { |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 155 | const struct imd_entry *e; |
| 156 | |
Patrick Georgi | b6161be | 2019-11-29 12:27:01 +0100 | [diff] [blame] | 157 | e = imd_entry_find(&imd, id); |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 158 | |
| 159 | return imd_to_cbmem(e); |
| 160 | } |
| 161 | |
| 162 | void *cbmem_find(u32 id) |
| 163 | { |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 164 | const struct imd_entry *e; |
| 165 | |
Patrick Georgi | b6161be | 2019-11-29 12:27:01 +0100 | [diff] [blame] | 166 | e = imd_entry_find(&imd, id); |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 167 | |
| 168 | if (e == NULL) |
| 169 | return NULL; |
| 170 | |
Patrick Georgi | b6161be | 2019-11-29 12:27:01 +0100 | [diff] [blame] | 171 | return imd_entry_at(&imd, e); |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /* Remove a reserved region. Returns 0 on success, < 0 on error. Note: A region |
| 175 | * cannot be removed unless it was the last one added. */ |
| 176 | int cbmem_entry_remove(const struct cbmem_entry *entry) |
| 177 | { |
Patrick Georgi | b6161be | 2019-11-29 12:27:01 +0100 | [diff] [blame] | 178 | return imd_entry_remove(&imd, cbmem_to_imd(entry)); |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | u64 cbmem_entry_size(const struct cbmem_entry *entry) |
| 182 | { |
Patrick Georgi | b6161be | 2019-11-29 12:27:01 +0100 | [diff] [blame] | 183 | return imd_entry_size(&imd, cbmem_to_imd(entry)); |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | void *cbmem_entry_start(const struct cbmem_entry *entry) |
| 187 | { |
Patrick Georgi | b6161be | 2019-11-29 12:27:01 +0100 | [diff] [blame] | 188 | return imd_entry_at(&imd, cbmem_to_imd(entry)); |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 189 | } |
| 190 | |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 191 | void cbmem_add_bootmem(void) |
| 192 | { |
Aaron Durbin | fb53242 | 2017-08-02 10:40:25 -0600 | [diff] [blame] | 193 | void *baseptr = NULL; |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 194 | size_t size = 0; |
| 195 | |
Philipp Deppenwiese | 84258db | 2018-08-16 00:31:26 +0200 | [diff] [blame] | 196 | cbmem_get_region(&baseptr, &size); |
Patrick Rudolph | 9ab9db0 | 2018-04-05 09:14:51 +0200 | [diff] [blame] | 197 | bootmem_add_range((uintptr_t)baseptr, size, BM_MEM_TABLE); |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 198 | } |
| 199 | |
Philipp Deppenwiese | 84258db | 2018-08-16 00:31:26 +0200 | [diff] [blame] | 200 | void cbmem_get_region(void **baseptr, size_t *size) |
| 201 | { |
Patrick Georgi | b6161be | 2019-11-29 12:27:01 +0100 | [diff] [blame] | 202 | imd_region_used(&imd, baseptr, size); |
Philipp Deppenwiese | 84258db | 2018-08-16 00:31:26 +0200 | [diff] [blame] | 203 | } |
| 204 | |
Subrata Banik | 42c44c2 | 2019-05-15 20:27:04 +0530 | [diff] [blame] | 205 | #if ENV_PAYLOAD_LOADER || (CONFIG(EARLY_CBMEM_LIST) \ |
Lee Leahy | e2422e3 | 2016-07-24 19:52:15 -0700 | [diff] [blame] | 206 | && (ENV_POSTCAR || ENV_ROMSTAGE)) |
Aaron Durbin | 1ca2d86 | 2015-09-30 12:26:54 -0500 | [diff] [blame] | 207 | /* |
| 208 | * -fdata-sections doesn't work so well on read only strings. They all |
| 209 | * get put in the same section even though those strings may never be |
| 210 | * referenced in the final binary. |
| 211 | */ |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 212 | void cbmem_list(void) |
| 213 | { |
| 214 | static const struct imd_lookup lookup[] = { CBMEM_ID_TO_NAME_TABLE }; |
| 215 | |
Patrick Georgi | b6161be | 2019-11-29 12:27:01 +0100 | [diff] [blame] | 216 | imd_print_entries(&imd, lookup, ARRAY_SIZE(lookup)); |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 217 | } |
Aaron Durbin | 1ca2d86 | 2015-09-30 12:26:54 -0500 | [diff] [blame] | 218 | #endif |
| 219 | |
| 220 | void cbmem_add_records_to_cbtable(struct lb_header *header) |
| 221 | { |
| 222 | struct imd_cursor cursor; |
Aaron Durbin | 1ca2d86 | 2015-09-30 12:26:54 -0500 | [diff] [blame] | 223 | |
Patrick Georgi | b6161be | 2019-11-29 12:27:01 +0100 | [diff] [blame] | 224 | if (imd_cursor_init(&imd, &cursor)) |
Aaron Durbin | 1ca2d86 | 2015-09-30 12:26:54 -0500 | [diff] [blame] | 225 | return; |
| 226 | |
| 227 | while (1) { |
| 228 | const struct imd_entry *e; |
| 229 | struct lb_cbmem_entry *lbe; |
| 230 | uint32_t id; |
| 231 | |
| 232 | e = imd_cursor_next(&cursor); |
| 233 | |
| 234 | if (e == NULL) |
| 235 | break; |
| 236 | |
Patrick Georgi | b6161be | 2019-11-29 12:27:01 +0100 | [diff] [blame] | 237 | id = imd_entry_id(&imd, e); |
Aaron Durbin | 1ca2d86 | 2015-09-30 12:26:54 -0500 | [diff] [blame] | 238 | /* Don't add these metadata entries. */ |
| 239 | if (id == CBMEM_ID_IMD_ROOT || id == CBMEM_ID_IMD_SMALL) |
| 240 | continue; |
| 241 | |
| 242 | lbe = (struct lb_cbmem_entry *)lb_new_record(header); |
| 243 | lbe->tag = LB_TAG_CBMEM_ENTRY; |
| 244 | lbe->size = sizeof(*lbe); |
Patrick Georgi | b6161be | 2019-11-29 12:27:01 +0100 | [diff] [blame] | 245 | lbe->address = (uintptr_t)imd_entry_at(&imd, e); |
| 246 | lbe->entry_size = imd_entry_size(&imd, e); |
Aaron Durbin | 1ca2d86 | 2015-09-30 12:26:54 -0500 | [diff] [blame] | 247 | lbe->id = id; |
| 248 | } |
| 249 | } |