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