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