Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 1 | /* |
| 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 Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <bootstate.h> |
| 17 | #include <bootmem.h> |
| 18 | #include <console/console.h> |
| 19 | #include <cbmem.h> |
| 20 | #include <imd.h> |
Kyösti Mälkki | f5cf60f | 2019-03-18 15:26:48 +0200 | [diff] [blame^] | 21 | #include <lib.h> |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | #include <arch/early_variables.h> |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 24 | |
Julius Werner | 3c814b2 | 2016-08-19 16:20:40 -0700 | [diff] [blame] | 25 | /* |
Aaron Durbin | 403fdbc | 2017-08-02 10:57:23 -0600 | [diff] [blame] | 26 | * We need special handling on x86 where CAR global migration is employed. One |
| 27 | * cannot use true globals in that circumstance because CAR is where the globals |
| 28 | * are backed -- creating a circular dependency. For non CAR platforms globals |
| 29 | * are free to be used as well as any stages that are purely executing out of |
| 30 | * RAM. For CAR platforms that don't migrate globals the as-linked globals can |
| 31 | * be used, but they need special decoration using CAR_GLOBAL. That ensures |
| 32 | * proper object placement in conjunction with the linker. |
| 33 | * |
| 34 | * For the CAR global migration platforms we have to always try to partially |
| 35 | * recover CBMEM from cbmem_top() whenever we try to access it. In other |
| 36 | * environments we're not so constrained and just keep the backing imd struct |
| 37 | * in a global. This also means that we can easily tell whether CBMEM has |
| 38 | * explicitly been initialized or recovered yet on those platforms, and don't |
| 39 | * need to put the burden on board or chipset code to tell us by returning |
| 40 | * NULL from cbmem_top() before that point. |
Julius Werner | 3c814b2 | 2016-08-19 16:20:40 -0700 | [diff] [blame] | 41 | */ |
Aaron Durbin | 1e9a914 | 2016-09-16 16:23:21 -0500 | [diff] [blame] | 42 | #define CAN_USE_GLOBALS \ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 43 | (!CONFIG(ARCH_X86) || ENV_RAMSTAGE || ENV_POSTCAR || \ |
| 44 | CONFIG(NO_CAR_GLOBAL_MIGRATION)) |
Julius Werner | 3c814b2 | 2016-08-19 16:20:40 -0700 | [diff] [blame] | 45 | |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 46 | static inline struct imd *cbmem_get_imd(void) |
| 47 | { |
Julius Werner | 3c814b2 | 2016-08-19 16:20:40 -0700 | [diff] [blame] | 48 | if (CAN_USE_GLOBALS) { |
Aaron Durbin | 403fdbc | 2017-08-02 10:57:23 -0600 | [diff] [blame] | 49 | static struct imd imd_cbmem CAR_GLOBAL; |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 50 | return &imd_cbmem; |
| 51 | } |
| 52 | return NULL; |
| 53 | } |
| 54 | |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 55 | static inline const struct cbmem_entry *imd_to_cbmem(const struct imd_entry *e) |
| 56 | { |
| 57 | return (const struct cbmem_entry *)e; |
| 58 | } |
| 59 | |
| 60 | static inline const struct imd_entry *cbmem_to_imd(const struct cbmem_entry *e) |
| 61 | { |
| 62 | return (const struct imd_entry *)e; |
| 63 | } |
| 64 | |
| 65 | /* These are the different situations to handle: |
Kyösti Mälkki | 513a1a8 | 2018-06-03 12:29:50 +0300 | [diff] [blame] | 66 | * |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 67 | * In ramstage cbmem_initialize() attempts a recovery of the |
| 68 | * cbmem region set up by romstage. It uses cbmem_top() as the |
| 69 | * starting point of recovery. |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 70 | * |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 71 | * In romstage, similar to ramstage, cbmem_initialize() needs to |
| 72 | * attempt recovery of the cbmem area using cbmem_top() as the limit. |
| 73 | * cbmem_initialize_empty() initializes an empty cbmem area from |
| 74 | * cbmem_top(); |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 75 | * |
| 76 | */ |
| 77 | static struct imd *imd_init_backing(struct imd *backing) |
| 78 | { |
| 79 | struct imd *imd; |
| 80 | |
| 81 | imd = cbmem_get_imd(); |
| 82 | |
| 83 | if (imd != NULL) |
| 84 | return imd; |
| 85 | |
| 86 | imd = backing; |
| 87 | |
| 88 | return imd; |
| 89 | } |
| 90 | |
| 91 | static struct imd *imd_init_backing_with_recover(struct imd *backing) |
| 92 | { |
| 93 | struct imd *imd; |
| 94 | |
| 95 | imd = imd_init_backing(backing); |
Julius Werner | 3c814b2 | 2016-08-19 16:20:40 -0700 | [diff] [blame] | 96 | if (!CAN_USE_GLOBALS) { |
| 97 | /* Always partially recover if we can't keep track of whether |
| 98 | * we have already initialized CBMEM in this stage. */ |
Kyösti Mälkki | e1fb052 | 2015-05-26 00:30:10 +0300 | [diff] [blame] | 99 | imd_handle_init(imd, cbmem_top()); |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 100 | imd_handle_init_partial_recovery(imd); |
| 101 | } |
| 102 | |
| 103 | return imd; |
| 104 | } |
| 105 | |
| 106 | void cbmem_initialize_empty(void) |
| 107 | { |
Lee Leahy | 522149c | 2015-05-08 11:33:55 -0700 | [diff] [blame] | 108 | cbmem_initialize_empty_id_size(0, 0); |
| 109 | } |
| 110 | |
Aaron Durbin | 6403167 | 2018-04-21 14:45:32 -0600 | [diff] [blame] | 111 | void __weak cbmem_top_init(void) |
Aaron Durbin | dfdea2a | 2017-08-01 10:27:10 -0600 | [diff] [blame] | 112 | { |
| 113 | } |
| 114 | |
| 115 | static void cbmem_top_init_once(void) |
| 116 | { |
| 117 | /* 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] | 118 | assumes first init call is in romstage. */ |
| 119 | if (!ENV_ROMSTAGE) |
Aaron Durbin | dfdea2a | 2017-08-01 10:27:10 -0600 | [diff] [blame] | 120 | return; |
| 121 | |
| 122 | cbmem_top_init(); |
Kyösti Mälkki | f5cf60f | 2019-03-18 15:26:48 +0200 | [diff] [blame^] | 123 | |
| 124 | /* The test is only effective on X86 and when address hits UC memory. */ |
| 125 | if (ENV_X86) |
| 126 | quick_ram_check_or_die((uintptr_t)cbmem_top() - sizeof(u32)); |
Aaron Durbin | dfdea2a | 2017-08-01 10:27:10 -0600 | [diff] [blame] | 127 | } |
| 128 | |
Lee Leahy | 522149c | 2015-05-08 11:33:55 -0700 | [diff] [blame] | 129 | void cbmem_initialize_empty_id_size(u32 id, u64 size) |
| 130 | { |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 131 | struct imd *imd; |
| 132 | struct imd imd_backing; |
Aaron Durbin | 41607a4 | 2015-06-09 13:54:10 -0500 | [diff] [blame] | 133 | const int no_recovery = 0; |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 134 | |
Aaron Durbin | dfdea2a | 2017-08-01 10:27:10 -0600 | [diff] [blame] | 135 | cbmem_top_init_once(); |
| 136 | |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 137 | imd = imd_init_backing(&imd_backing); |
Kyösti Mälkki | e1fb052 | 2015-05-26 00:30:10 +0300 | [diff] [blame] | 138 | imd_handle_init(imd, cbmem_top()); |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 139 | |
| 140 | printk(BIOS_DEBUG, "CBMEM:\n"); |
| 141 | |
Lee Leahy | 522149c | 2015-05-08 11:33:55 -0700 | [diff] [blame] | 142 | if (imd_create_tiered_empty(imd, CBMEM_ROOT_MIN_SIZE, CBMEM_LG_ALIGN, |
| 143 | CBMEM_SM_ROOT_SIZE, CBMEM_SM_ALIGN)) { |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 144 | printk(BIOS_DEBUG, "failed.\n"); |
| 145 | return; |
| 146 | } |
| 147 | |
Lee Leahy | 522149c | 2015-05-08 11:33:55 -0700 | [diff] [blame] | 148 | /* Add the specified range first */ |
| 149 | if (size) |
| 150 | cbmem_add(id, size); |
| 151 | |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 152 | /* Complete migration to CBMEM. */ |
Aaron Durbin | 41607a4 | 2015-06-09 13:54:10 -0500 | [diff] [blame] | 153 | cbmem_run_init_hooks(no_recovery); |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 154 | } |
| 155 | |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 156 | int cbmem_initialize(void) |
| 157 | { |
Lee Leahy | 522149c | 2015-05-08 11:33:55 -0700 | [diff] [blame] | 158 | return cbmem_initialize_id_size(0, 0); |
| 159 | } |
| 160 | |
| 161 | int cbmem_initialize_id_size(u32 id, u64 size) |
| 162 | { |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 163 | struct imd *imd; |
| 164 | struct imd imd_backing; |
Aaron Durbin | 41607a4 | 2015-06-09 13:54:10 -0500 | [diff] [blame] | 165 | const int recovery = 1; |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 166 | |
Aaron Durbin | dfdea2a | 2017-08-01 10:27:10 -0600 | [diff] [blame] | 167 | cbmem_top_init_once(); |
| 168 | |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 169 | imd = imd_init_backing(&imd_backing); |
Kyösti Mälkki | e1fb052 | 2015-05-26 00:30:10 +0300 | [diff] [blame] | 170 | imd_handle_init(imd, cbmem_top()); |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 171 | |
| 172 | if (imd_recover(imd)) |
| 173 | return 1; |
| 174 | |
| 175 | #if defined(__PRE_RAM__) |
| 176 | /* |
| 177 | * Lock the imd in romstage on a recovery. The assumption is that |
| 178 | * if the imd area was recovered in romstage then S3 resume path |
| 179 | * is being taken. |
| 180 | */ |
| 181 | imd_lockdown(imd); |
| 182 | #endif |
| 183 | |
Lee Leahy | 522149c | 2015-05-08 11:33:55 -0700 | [diff] [blame] | 184 | /* Add the specified range first */ |
| 185 | if (size) |
| 186 | cbmem_add(id, size); |
| 187 | |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 188 | /* Complete migration to CBMEM. */ |
Aaron Durbin | 41607a4 | 2015-06-09 13:54:10 -0500 | [diff] [blame] | 189 | cbmem_run_init_hooks(recovery); |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 190 | |
| 191 | /* Recovery successful. */ |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | int cbmem_recovery(int is_wakeup) |
| 196 | { |
| 197 | int rv = 0; |
| 198 | if (!is_wakeup) |
| 199 | cbmem_initialize_empty(); |
| 200 | else |
| 201 | rv = cbmem_initialize(); |
| 202 | return rv; |
| 203 | } |
| 204 | |
| 205 | const struct cbmem_entry *cbmem_entry_add(u32 id, u64 size64) |
| 206 | { |
| 207 | struct imd *imd; |
| 208 | struct imd imd_backing; |
| 209 | const struct imd_entry *e; |
| 210 | |
| 211 | imd = imd_init_backing_with_recover(&imd_backing); |
| 212 | |
| 213 | e = imd_entry_find_or_add(imd, id, size64); |
| 214 | |
| 215 | return imd_to_cbmem(e); |
| 216 | } |
| 217 | |
| 218 | void *cbmem_add(u32 id, u64 size) |
| 219 | { |
| 220 | struct imd *imd; |
| 221 | struct imd imd_backing; |
| 222 | const struct imd_entry *e; |
| 223 | |
| 224 | imd = imd_init_backing_with_recover(&imd_backing); |
| 225 | |
| 226 | e = imd_entry_find_or_add(imd, id, size); |
| 227 | |
| 228 | if (e == NULL) |
| 229 | return NULL; |
| 230 | |
| 231 | return imd_entry_at(imd, e); |
| 232 | } |
| 233 | |
| 234 | /* Retrieve a region provided a given id. */ |
| 235 | const struct cbmem_entry *cbmem_entry_find(u32 id) |
| 236 | { |
| 237 | struct imd *imd; |
| 238 | struct imd imd_backing; |
| 239 | const struct imd_entry *e; |
| 240 | |
| 241 | imd = imd_init_backing_with_recover(&imd_backing); |
| 242 | |
| 243 | e = imd_entry_find(imd, id); |
| 244 | |
| 245 | return imd_to_cbmem(e); |
| 246 | } |
| 247 | |
| 248 | void *cbmem_find(u32 id) |
| 249 | { |
| 250 | struct imd *imd; |
| 251 | struct imd imd_backing; |
| 252 | const struct imd_entry *e; |
| 253 | |
| 254 | imd = imd_init_backing_with_recover(&imd_backing); |
| 255 | |
| 256 | e = imd_entry_find(imd, id); |
| 257 | |
| 258 | if (e == NULL) |
| 259 | return NULL; |
| 260 | |
| 261 | return imd_entry_at(imd, e); |
| 262 | } |
| 263 | |
| 264 | /* Remove a reserved region. Returns 0 on success, < 0 on error. Note: A region |
| 265 | * cannot be removed unless it was the last one added. */ |
| 266 | int cbmem_entry_remove(const struct cbmem_entry *entry) |
| 267 | { |
| 268 | struct imd *imd; |
| 269 | struct imd imd_backing; |
| 270 | |
| 271 | imd = imd_init_backing_with_recover(&imd_backing); |
| 272 | |
| 273 | return imd_entry_remove(imd, cbmem_to_imd(entry)); |
| 274 | } |
| 275 | |
| 276 | u64 cbmem_entry_size(const struct cbmem_entry *entry) |
| 277 | { |
| 278 | struct imd *imd; |
| 279 | struct imd imd_backing; |
| 280 | |
| 281 | imd = imd_init_backing_with_recover(&imd_backing); |
| 282 | |
| 283 | return imd_entry_size(imd, cbmem_to_imd(entry)); |
| 284 | } |
| 285 | |
| 286 | void *cbmem_entry_start(const struct cbmem_entry *entry) |
| 287 | { |
| 288 | struct imd *imd; |
| 289 | struct imd imd_backing; |
| 290 | |
| 291 | imd = imd_init_backing_with_recover(&imd_backing); |
| 292 | |
| 293 | return imd_entry_at(imd, cbmem_to_imd(entry)); |
| 294 | } |
| 295 | |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 296 | void cbmem_add_bootmem(void) |
| 297 | { |
Aaron Durbin | fb53242 | 2017-08-02 10:40:25 -0600 | [diff] [blame] | 298 | void *baseptr = NULL; |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 299 | size_t size = 0; |
| 300 | |
Philipp Deppenwiese | 84258db | 2018-08-16 00:31:26 +0200 | [diff] [blame] | 301 | cbmem_get_region(&baseptr, &size); |
Patrick Rudolph | 9ab9db0 | 2018-04-05 09:14:51 +0200 | [diff] [blame] | 302 | bootmem_add_range((uintptr_t)baseptr, size, BM_MEM_TABLE); |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 303 | } |
| 304 | |
Philipp Deppenwiese | 84258db | 2018-08-16 00:31:26 +0200 | [diff] [blame] | 305 | void cbmem_get_region(void **baseptr, size_t *size) |
| 306 | { |
| 307 | imd_region_used(cbmem_get_imd(), baseptr, size); |
| 308 | } |
| 309 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 310 | #if ENV_RAMSTAGE || (CONFIG(EARLY_CBMEM_LIST) \ |
Lee Leahy | e2422e3 | 2016-07-24 19:52:15 -0700 | [diff] [blame] | 311 | && (ENV_POSTCAR || ENV_ROMSTAGE)) |
Aaron Durbin | 1ca2d86 | 2015-09-30 12:26:54 -0500 | [diff] [blame] | 312 | /* |
| 313 | * -fdata-sections doesn't work so well on read only strings. They all |
| 314 | * get put in the same section even though those strings may never be |
| 315 | * referenced in the final binary. |
| 316 | */ |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 317 | void cbmem_list(void) |
| 318 | { |
| 319 | static const struct imd_lookup lookup[] = { CBMEM_ID_TO_NAME_TABLE }; |
Lee Leahy | e2422e3 | 2016-07-24 19:52:15 -0700 | [diff] [blame] | 320 | struct imd *imd; |
| 321 | struct imd imd_backing; |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 322 | |
Lee Leahy | e2422e3 | 2016-07-24 19:52:15 -0700 | [diff] [blame] | 323 | imd = imd_init_backing_with_recover(&imd_backing); |
| 324 | imd_print_entries(imd, lookup, ARRAY_SIZE(lookup)); |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 325 | } |
Aaron Durbin | 1ca2d86 | 2015-09-30 12:26:54 -0500 | [diff] [blame] | 326 | #endif |
| 327 | |
| 328 | void cbmem_add_records_to_cbtable(struct lb_header *header) |
| 329 | { |
| 330 | struct imd_cursor cursor; |
| 331 | struct imd *imd; |
| 332 | |
| 333 | imd = cbmem_get_imd(); |
| 334 | |
| 335 | if (imd_cursor_init(imd, &cursor)) |
| 336 | return; |
| 337 | |
| 338 | while (1) { |
| 339 | const struct imd_entry *e; |
| 340 | struct lb_cbmem_entry *lbe; |
| 341 | uint32_t id; |
| 342 | |
| 343 | e = imd_cursor_next(&cursor); |
| 344 | |
| 345 | if (e == NULL) |
| 346 | break; |
| 347 | |
| 348 | id = imd_entry_id(imd, e); |
| 349 | /* Don't add these metadata entries. */ |
| 350 | if (id == CBMEM_ID_IMD_ROOT || id == CBMEM_ID_IMD_SMALL) |
| 351 | continue; |
| 352 | |
| 353 | lbe = (struct lb_cbmem_entry *)lb_new_record(header); |
| 354 | lbe->tag = LB_TAG_CBMEM_ENTRY; |
| 355 | lbe->size = sizeof(*lbe); |
| 356 | lbe->address = (uintptr_t)imd_entry_at(imd, e); |
| 357 | lbe->entry_size = imd_entry_size(imd, e); |
| 358 | lbe->id = id; |
| 359 | } |
| 360 | } |