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