Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright 2015 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 | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <assert.h> |
| 17 | #include <cbmem.h> |
| 18 | #include <console/console.h> |
| 19 | #include <imd.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
Julius Werner | a214837 | 2019-11-13 19:50:33 -0800 | [diff] [blame] | 22 | #include <types.h> |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 23 | |
| 24 | /* For more details on implementation and usage please see the imd.h header. */ |
| 25 | |
| 26 | static const uint32_t IMD_ROOT_PTR_MAGIC = 0xc0389481; |
| 27 | static const uint32_t IMD_ENTRY_MAGIC = ~0xc0389481; |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 28 | static const uint32_t SMALL_REGION_ID = CBMEM_ID_IMD_SMALL; |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 29 | static const size_t LIMIT_ALIGN = 4096; |
| 30 | |
| 31 | /* In-memory data structures. */ |
| 32 | struct imd_root_pointer { |
| 33 | uint32_t magic; |
| 34 | /* Relative to upper limit/offset. */ |
| 35 | int32_t root_offset; |
Stefan Reinauer | 6a00113 | 2017-07-13 02:20:27 +0200 | [diff] [blame] | 36 | } __packed; |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 37 | |
| 38 | struct imd_entry { |
| 39 | uint32_t magic; |
| 40 | /* start is located relative to imd_root */ |
| 41 | int32_t start_offset; |
| 42 | uint32_t size; |
| 43 | uint32_t id; |
Stefan Reinauer | 6a00113 | 2017-07-13 02:20:27 +0200 | [diff] [blame] | 44 | } __packed; |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 45 | |
| 46 | struct imd_root { |
| 47 | uint32_t max_entries; |
| 48 | uint32_t num_entries; |
| 49 | uint32_t flags; |
| 50 | uint32_t entry_align; |
| 51 | /* Used for fixing the size of an imd. Relative to the root. */ |
| 52 | int32_t max_offset; |
| 53 | struct imd_entry entries[0]; |
Stefan Reinauer | 6a00113 | 2017-07-13 02:20:27 +0200 | [diff] [blame] | 54 | } __packed; |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 55 | |
| 56 | #define IMD_FLAG_LOCKED 1 |
| 57 | |
| 58 | static void *relative_pointer(void *base, ssize_t offset) |
| 59 | { |
| 60 | intptr_t b = (intptr_t)base; |
| 61 | b += offset; |
| 62 | return (void *)b; |
| 63 | } |
| 64 | |
| 65 | static bool imd_root_pointer_valid(const struct imd_root_pointer *rp) |
| 66 | { |
| 67 | return !!(rp->magic == IMD_ROOT_PTR_MAGIC); |
| 68 | } |
| 69 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 70 | static struct imd_root *imdr_root(const struct imdr *imdr) |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 71 | { |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 72 | return imdr->r; |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /* |
| 76 | * The root pointer is relative to the upper limit of the imd. i.e. It sits |
| 77 | * just below the upper limit. |
| 78 | */ |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 79 | static struct imd_root_pointer *imdr_get_root_pointer(const struct imdr *imdr) |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 80 | { |
| 81 | struct imd_root_pointer *rp; |
| 82 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 83 | rp = relative_pointer((void *)imdr->limit, -sizeof(*rp)); |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 84 | |
| 85 | return rp; |
| 86 | } |
| 87 | |
| 88 | static void imd_link_root(struct imd_root_pointer *rp, struct imd_root *r) |
| 89 | { |
| 90 | rp->magic = IMD_ROOT_PTR_MAGIC; |
| 91 | rp->root_offset = (int32_t)((intptr_t)r - (intptr_t)rp); |
| 92 | } |
| 93 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 94 | static struct imd_entry *root_last_entry(struct imd_root *r) |
| 95 | { |
| 96 | return &r->entries[r->num_entries - 1]; |
| 97 | } |
| 98 | |
| 99 | static size_t root_num_entries(size_t root_size) |
| 100 | { |
| 101 | size_t entries_size; |
| 102 | |
| 103 | entries_size = root_size; |
| 104 | entries_size -= sizeof(struct imd_root_pointer); |
| 105 | entries_size -= sizeof(struct imd_root); |
| 106 | |
| 107 | return entries_size / sizeof(struct imd_entry); |
| 108 | } |
| 109 | |
| 110 | static size_t imd_root_data_left(struct imd_root *r) |
| 111 | { |
| 112 | struct imd_entry *last_entry; |
| 113 | |
| 114 | last_entry = root_last_entry(r); |
| 115 | |
| 116 | if (r->max_offset != 0) |
| 117 | return last_entry->start_offset - r->max_offset; |
| 118 | |
| 119 | return ~(size_t)0; |
| 120 | } |
| 121 | |
| 122 | static bool root_is_locked(const struct imd_root *r) |
| 123 | { |
| 124 | return !!(r->flags & IMD_FLAG_LOCKED); |
| 125 | } |
| 126 | |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 127 | static void imd_entry_assign(struct imd_entry *e, uint32_t id, |
| 128 | ssize_t offset, size_t size) |
| 129 | { |
| 130 | e->magic = IMD_ENTRY_MAGIC; |
| 131 | e->start_offset = offset; |
| 132 | e->size = size; |
| 133 | e->id = id; |
| 134 | } |
| 135 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 136 | static void imdr_init(struct imdr *ir, void *upper_limit) |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 137 | { |
| 138 | uintptr_t limit = (uintptr_t)upper_limit; |
| 139 | /* Upper limit is aligned down to 4KiB */ |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 140 | ir->limit = ALIGN_DOWN(limit, LIMIT_ALIGN); |
| 141 | ir->r = NULL; |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 142 | } |
| 143 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 144 | static int imdr_create_empty(struct imdr *imdr, size_t root_size, |
| 145 | size_t entry_align) |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 146 | { |
| 147 | struct imd_root_pointer *rp; |
| 148 | struct imd_root *r; |
| 149 | struct imd_entry *e; |
| 150 | ssize_t root_offset; |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 151 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 152 | if (!imdr->limit) |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 153 | return -1; |
| 154 | |
| 155 | /* root_size and entry_align should be a power of 2. */ |
| 156 | assert(IS_POWER_OF_2(root_size)); |
| 157 | assert(IS_POWER_OF_2(entry_align)); |
| 158 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 159 | if (!imdr->limit) |
| 160 | return -1; |
| 161 | |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 162 | /* |
Martin Roth | 2ed0aa2 | 2016-01-05 20:58:58 -0700 | [diff] [blame] | 163 | * root_size needs to be large enough to accommodate root pointer and |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 164 | * root book keeping structure. The caller needs to ensure there's |
| 165 | * enough room for tracking individual allocations. |
| 166 | */ |
| 167 | if (root_size < (sizeof(*rp) + sizeof(*r))) |
| 168 | return -1; |
| 169 | |
Lee Leahy | 7340217 | 2017-03-10 15:23:24 -0800 | [diff] [blame] | 170 | /* For simplicity don't allow sizes or alignments to exceed LIMIT_ALIGN. |
| 171 | */ |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 172 | if (root_size > LIMIT_ALIGN || entry_align > LIMIT_ALIGN) |
| 173 | return -1; |
| 174 | |
| 175 | /* Additionally, don't handle an entry alignment > root_size. */ |
| 176 | if (entry_align > root_size) |
| 177 | return -1; |
| 178 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 179 | rp = imdr_get_root_pointer(imdr); |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 180 | |
| 181 | root_offset = -(ssize_t)root_size; |
| 182 | /* Set root pointer. */ |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 183 | imdr->r = relative_pointer((void *)imdr->limit, root_offset); |
| 184 | r = imdr_root(imdr); |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 185 | imd_link_root(rp, r); |
| 186 | |
| 187 | memset(r, 0, sizeof(*r)); |
| 188 | r->entry_align = entry_align; |
| 189 | |
| 190 | /* Calculate size left for entries. */ |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 191 | r->max_entries = root_num_entries(root_size); |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 192 | |
| 193 | /* Fill in first entry covering the root region. */ |
| 194 | r->num_entries = 1; |
| 195 | e = &r->entries[0]; |
| 196 | imd_entry_assign(e, CBMEM_ID_IMD_ROOT, 0, root_size); |
| 197 | |
| 198 | printk(BIOS_DEBUG, "IMD: root @ %p %u entries.\n", r, r->max_entries); |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 203 | static int imdr_recover(struct imdr *imdr) |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 204 | { |
| 205 | struct imd_root_pointer *rp; |
| 206 | struct imd_root *r; |
| 207 | uintptr_t low_limit; |
| 208 | size_t i; |
| 209 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 210 | if (!imdr->limit) |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 211 | return -1; |
| 212 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 213 | rp = imdr_get_root_pointer(imdr); |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 214 | |
| 215 | if (!imd_root_pointer_valid(rp)) |
| 216 | return -1; |
| 217 | |
| 218 | r = relative_pointer(rp, rp->root_offset); |
| 219 | |
| 220 | /* Confirm the root and root pointer are just under the limit. */ |
| 221 | if (ALIGN_UP((uintptr_t)&r->entries[r->max_entries], LIMIT_ALIGN) != |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 222 | imdr->limit) |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 223 | return -1; |
| 224 | |
| 225 | if (r->num_entries > r->max_entries) |
| 226 | return -1; |
| 227 | |
| 228 | /* Entry alignment should be power of 2. */ |
| 229 | if (!IS_POWER_OF_2(r->entry_align)) |
| 230 | return -1; |
| 231 | |
| 232 | low_limit = (uintptr_t)relative_pointer(r, r->max_offset); |
| 233 | |
| 234 | /* If no max_offset then lowest limit is 0. */ |
| 235 | if (low_limit == (uintptr_t)r) |
| 236 | low_limit = 0; |
| 237 | |
| 238 | for (i = 0; i < r->num_entries; i++) { |
| 239 | uintptr_t start_addr; |
| 240 | const struct imd_entry *e = &r->entries[i]; |
| 241 | |
| 242 | if (e->magic != IMD_ENTRY_MAGIC) |
| 243 | return -1; |
| 244 | |
| 245 | start_addr = (uintptr_t)relative_pointer(r, e->start_offset); |
| 246 | if (start_addr < low_limit) |
| 247 | return -1; |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 248 | if (start_addr >= imdr->limit || |
| 249 | (start_addr + e->size) > imdr->limit) |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 250 | return -1; |
| 251 | } |
| 252 | |
| 253 | /* Set root pointer. */ |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 254 | imdr->r = r; |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 259 | static const struct imd_entry *imdr_entry_find(const struct imdr *imdr, |
| 260 | uint32_t id) |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 261 | { |
| 262 | struct imd_root *r; |
| 263 | struct imd_entry *e; |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 264 | size_t i; |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 265 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 266 | r = imdr_root(imdr); |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 267 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 268 | if (r == NULL) |
| 269 | return NULL; |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 270 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 271 | e = NULL; |
| 272 | /* Skip first entry covering the root. */ |
| 273 | for (i = 1; i < r->num_entries; i++) { |
| 274 | if (id != r->entries[i].id) |
| 275 | continue; |
| 276 | e = &r->entries[i]; |
| 277 | break; |
| 278 | } |
| 279 | |
| 280 | return e; |
| 281 | } |
| 282 | |
| 283 | static int imdr_limit_size(struct imdr *imdr, size_t max_size) |
| 284 | { |
| 285 | struct imd_root *r; |
| 286 | ssize_t smax_size; |
| 287 | size_t root_size; |
| 288 | |
| 289 | r = imdr_root(imdr); |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 290 | if (r == NULL) |
| 291 | return -1; |
| 292 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 293 | root_size = imdr->limit - (uintptr_t)r; |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 294 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 295 | if (max_size < root_size) |
| 296 | return -1; |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 297 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 298 | /* Take into account the root size. */ |
| 299 | smax_size = max_size - root_size; |
| 300 | smax_size = -smax_size; |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 301 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 302 | r->max_offset = smax_size; |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 307 | static size_t imdr_entry_size(const struct imdr *imdr, |
| 308 | const struct imd_entry *e) |
| 309 | { |
| 310 | return e->size; |
| 311 | } |
| 312 | |
| 313 | static void *imdr_entry_at(const struct imdr *imdr, const struct imd_entry *e) |
| 314 | { |
| 315 | return relative_pointer(imdr_root(imdr), e->start_offset); |
| 316 | } |
| 317 | |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 318 | static struct imd_entry *imd_entry_add_to_root(struct imd_root *r, uint32_t id, |
| 319 | size_t size) |
| 320 | { |
| 321 | struct imd_entry *entry; |
| 322 | struct imd_entry *last_entry; |
| 323 | ssize_t e_offset; |
| 324 | size_t used_size; |
| 325 | |
| 326 | if (r->num_entries == r->max_entries) |
| 327 | return NULL; |
| 328 | |
| 329 | /* Determine total size taken up by entry. */ |
| 330 | used_size = ALIGN_UP(size, r->entry_align); |
| 331 | |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 332 | /* See if size overflows imd total size. */ |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 333 | if (used_size > imd_root_data_left(r)) |
| 334 | return NULL; |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 335 | |
| 336 | /* |
| 337 | * Determine if offset field overflows. All offsets should be lower |
| 338 | * than the previous one. |
| 339 | */ |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 340 | last_entry = root_last_entry(r); |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 341 | e_offset = last_entry->start_offset; |
| 342 | e_offset -= (ssize_t)used_size; |
| 343 | if (e_offset > last_entry->start_offset) |
| 344 | return NULL; |
| 345 | |
| 346 | entry = root_last_entry(r) + 1; |
| 347 | r->num_entries++; |
| 348 | |
| 349 | imd_entry_assign(entry, id, e_offset, size); |
| 350 | |
| 351 | return entry; |
| 352 | } |
| 353 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 354 | static const struct imd_entry *imdr_entry_add(const struct imdr *imdr, |
| 355 | uint32_t id, size_t size) |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 356 | { |
| 357 | struct imd_root *r; |
| 358 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 359 | r = imdr_root(imdr); |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 360 | |
| 361 | if (r == NULL) |
| 362 | return NULL; |
| 363 | |
| 364 | if (root_is_locked(r)) |
| 365 | return NULL; |
| 366 | |
| 367 | return imd_entry_add_to_root(r, id, size); |
| 368 | } |
| 369 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 370 | static bool imdr_has_entry(const struct imdr *imdr, const struct imd_entry *e) |
| 371 | { |
| 372 | struct imd_root *r; |
| 373 | size_t idx; |
| 374 | |
| 375 | r = imdr_root(imdr); |
| 376 | if (r == NULL) |
| 377 | return false; |
| 378 | |
| 379 | /* Determine if the entry is within this root structure. */ |
| 380 | idx = e - &r->entries[0]; |
| 381 | if (idx >= r->num_entries) |
| 382 | return false; |
| 383 | |
| 384 | return true; |
| 385 | } |
| 386 | |
| 387 | static const struct imdr *imd_entry_to_imdr(const struct imd *imd, |
| 388 | const struct imd_entry *entry) |
| 389 | { |
| 390 | if (imdr_has_entry(&imd->lg, entry)) |
| 391 | return &imd->lg; |
| 392 | |
| 393 | if (imdr_has_entry(&imd->sm, entry)) |
| 394 | return &imd->sm; |
| 395 | |
| 396 | return NULL; |
| 397 | } |
| 398 | |
| 399 | /* Initialize imd handle. */ |
| 400 | void imd_handle_init(struct imd *imd, void *upper_limit) |
| 401 | { |
| 402 | imdr_init(&imd->lg, upper_limit); |
| 403 | imdr_init(&imd->sm, NULL); |
| 404 | } |
| 405 | |
| 406 | void imd_handle_init_partial_recovery(struct imd *imd) |
| 407 | { |
| 408 | const struct imd_entry *e; |
| 409 | struct imd_root_pointer *rp; |
| 410 | struct imdr *imdr; |
| 411 | |
Aaron Durbin | 01562b6 | 2015-05-11 14:19:37 -0500 | [diff] [blame] | 412 | if (imd->lg.limit == 0) |
| 413 | return; |
| 414 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 415 | imd_handle_init(imd, (void *)imd->lg.limit); |
| 416 | |
| 417 | /* Initialize root pointer for the large regions. */ |
| 418 | imdr = &imd->lg; |
| 419 | rp = imdr_get_root_pointer(imdr); |
| 420 | imdr->r = relative_pointer(rp, rp->root_offset); |
| 421 | |
| 422 | e = imdr_entry_find(imdr, SMALL_REGION_ID); |
| 423 | |
| 424 | if (e == NULL) |
| 425 | return; |
| 426 | |
| 427 | imd->sm.limit = (uintptr_t)imdr_entry_at(imdr, e); |
| 428 | imd->sm.limit += imdr_entry_size(imdr, e); |
| 429 | imdr = &imd->sm; |
| 430 | rp = imdr_get_root_pointer(imdr); |
| 431 | imdr->r = relative_pointer(rp, rp->root_offset); |
| 432 | } |
| 433 | |
| 434 | int imd_create_empty(struct imd *imd, size_t root_size, size_t entry_align) |
| 435 | { |
| 436 | return imdr_create_empty(&imd->lg, root_size, entry_align); |
| 437 | } |
| 438 | |
| 439 | int imd_create_tiered_empty(struct imd *imd, |
| 440 | size_t lg_root_size, size_t lg_entry_align, |
| 441 | size_t sm_root_size, size_t sm_entry_align) |
| 442 | { |
Lee Leahy | 3e1cab4 | 2017-03-10 17:48:31 -0800 | [diff] [blame] | 443 | size_t sm_region_size; |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 444 | const struct imd_entry *e; |
| 445 | struct imdr *imdr; |
| 446 | |
| 447 | imdr = &imd->lg; |
| 448 | |
| 449 | if (imdr_create_empty(imdr, lg_root_size, lg_entry_align) != 0) |
| 450 | return -1; |
| 451 | |
| 452 | /* Calculate the size of the small region to request. */ |
| 453 | sm_region_size = root_num_entries(sm_root_size) * sm_entry_align; |
| 454 | sm_region_size += sm_root_size; |
| 455 | sm_region_size = ALIGN_UP(sm_region_size, lg_entry_align); |
| 456 | |
| 457 | /* Add a new entry to the large region to cover the root and entries. */ |
| 458 | e = imdr_entry_add(imdr, SMALL_REGION_ID, sm_region_size); |
| 459 | |
| 460 | if (e == NULL) |
| 461 | goto fail; |
| 462 | |
| 463 | imd->sm.limit = (uintptr_t)imdr_entry_at(imdr, e); |
| 464 | imd->sm.limit += sm_region_size; |
| 465 | |
| 466 | if (imdr_create_empty(&imd->sm, sm_root_size, sm_entry_align) != 0 || |
| 467 | imdr_limit_size(&imd->sm, sm_region_size)) |
| 468 | goto fail; |
| 469 | |
| 470 | return 0; |
| 471 | fail: |
| 472 | imd_handle_init(imd, (void *)imdr->limit); |
| 473 | return -1; |
| 474 | } |
| 475 | |
| 476 | int imd_recover(struct imd *imd) |
| 477 | { |
| 478 | const struct imd_entry *e; |
| 479 | uintptr_t small_upper_limit; |
| 480 | struct imdr *imdr; |
| 481 | |
| 482 | imdr = &imd->lg; |
| 483 | if (imdr_recover(imdr) != 0) |
| 484 | return -1; |
| 485 | |
| 486 | /* Determine if small region is region is present. */ |
| 487 | e = imdr_entry_find(imdr, SMALL_REGION_ID); |
| 488 | |
| 489 | if (e == NULL) |
| 490 | return 0; |
| 491 | |
| 492 | small_upper_limit = (uintptr_t)imdr_entry_at(imdr, e); |
| 493 | small_upper_limit += imdr_entry_size(imdr, e); |
| 494 | |
| 495 | imd->sm.limit = small_upper_limit; |
| 496 | |
| 497 | /* Tear down any changes on failure. */ |
| 498 | if (imdr_recover(&imd->sm) != 0) { |
| 499 | imd_handle_init(imd, (void *)imd->lg.limit); |
| 500 | return -1; |
| 501 | } |
| 502 | |
| 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | int imd_limit_size(struct imd *imd, size_t max_size) |
| 507 | { |
| 508 | return imdr_limit_size(&imd->lg, max_size); |
| 509 | } |
| 510 | |
| 511 | int imd_lockdown(struct imd *imd) |
| 512 | { |
| 513 | struct imd_root *r; |
| 514 | |
| 515 | r = imdr_root(&imd->lg); |
| 516 | if (r == NULL) |
| 517 | return -1; |
| 518 | |
| 519 | r->flags |= IMD_FLAG_LOCKED; |
| 520 | |
| 521 | r = imdr_root(&imd->sm); |
| 522 | if (r != NULL) |
| 523 | r->flags |= IMD_FLAG_LOCKED; |
| 524 | |
| 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | int imd_region_used(struct imd *imd, void **base, size_t *size) |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 529 | { |
| 530 | struct imd_root *r; |
| 531 | struct imd_entry *e; |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 532 | void *low_addr; |
| 533 | size_t sz_used; |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 534 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 535 | if (!imd->lg.limit) |
| 536 | return -1; |
| 537 | |
| 538 | r = imdr_root(&imd->lg); |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 539 | |
| 540 | if (r == NULL) |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 541 | return -1; |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 542 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 543 | /* Use last entry to obtain lowest address. */ |
| 544 | e = root_last_entry(r); |
| 545 | |
| 546 | low_addr = relative_pointer(r, e->start_offset); |
| 547 | |
| 548 | /* Total size used is the last entry's base up to the limit. */ |
| 549 | sz_used = imd->lg.limit - (uintptr_t)low_addr; |
| 550 | |
| 551 | *base = low_addr; |
| 552 | *size = sz_used; |
| 553 | |
| 554 | return 0; |
| 555 | } |
| 556 | |
| 557 | const struct imd_entry *imd_entry_add(const struct imd *imd, uint32_t id, |
| 558 | size_t size) |
| 559 | { |
| 560 | struct imd_root *r; |
| 561 | const struct imdr *imdr; |
| 562 | const struct imd_entry *e = NULL; |
| 563 | |
| 564 | /* |
| 565 | * Determine if requested size is less than 1/4 of small data |
| 566 | * region is left. |
| 567 | */ |
| 568 | imdr = &imd->sm; |
| 569 | r = imdr_root(imdr); |
| 570 | |
| 571 | /* No small region. Use the large region. */ |
| 572 | if (r == NULL) |
| 573 | return imdr_entry_add(&imd->lg, id, size); |
| 574 | else if (size <= r->entry_align || size <= imd_root_data_left(r) / 4) |
| 575 | e = imdr_entry_add(imdr, id, size); |
| 576 | |
| 577 | /* Fall back on large region allocation. */ |
| 578 | if (e == NULL) |
| 579 | e = imdr_entry_add(&imd->lg, id, size); |
| 580 | |
| 581 | return e; |
| 582 | } |
| 583 | |
| 584 | const struct imd_entry *imd_entry_find(const struct imd *imd, uint32_t id) |
| 585 | { |
| 586 | const struct imd_entry *e; |
| 587 | |
| 588 | /* Many of the smaller allocations are used a lot. Therefore, try |
| 589 | * the small region first. */ |
| 590 | e = imdr_entry_find(&imd->sm, id); |
| 591 | |
| 592 | if (e == NULL) |
| 593 | e = imdr_entry_find(&imd->lg, id); |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 594 | |
| 595 | return e; |
| 596 | } |
| 597 | |
| 598 | const struct imd_entry *imd_entry_find_or_add(const struct imd *imd, |
| 599 | uint32_t id, size_t size) |
| 600 | { |
| 601 | const struct imd_entry *e; |
| 602 | |
| 603 | e = imd_entry_find(imd, id); |
| 604 | |
| 605 | if (e != NULL) |
| 606 | return e; |
| 607 | |
| 608 | return imd_entry_add(imd, id, size); |
| 609 | } |
| 610 | |
| 611 | size_t imd_entry_size(const struct imd *imd, const struct imd_entry *entry) |
| 612 | { |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 613 | return imdr_entry_size(NULL, entry); |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | void *imd_entry_at(const struct imd *imd, const struct imd_entry *entry) |
| 617 | { |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 618 | const struct imdr *imdr; |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 619 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 620 | imdr = imd_entry_to_imdr(imd, entry); |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 621 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 622 | if (imdr == NULL) |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 623 | return NULL; |
| 624 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 625 | return imdr_entry_at(imdr, entry); |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 626 | } |
| 627 | |
Aaron Durbin | 1ca2d86 | 2015-09-30 12:26:54 -0500 | [diff] [blame] | 628 | uint32_t imd_entry_id(const struct imd *imd, const struct imd_entry *entry) |
| 629 | { |
| 630 | return entry->id; |
| 631 | } |
| 632 | |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 633 | int imd_entry_remove(const struct imd *imd, const struct imd_entry *entry) |
| 634 | { |
| 635 | struct imd_root *r; |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 636 | const struct imdr *imdr; |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 637 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 638 | imdr = imd_entry_to_imdr(imd, entry); |
| 639 | |
| 640 | if (imdr == NULL) |
Lee Leahy | 35af5c4 | 2017-03-09 17:35:28 -0800 | [diff] [blame] | 641 | return -1; |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 642 | |
| 643 | r = imdr_root(imdr); |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 644 | |
| 645 | if (r == NULL) |
| 646 | return -1; |
| 647 | |
| 648 | if (root_is_locked(r)) |
| 649 | return -1; |
| 650 | |
| 651 | if (entry != root_last_entry(r)) |
| 652 | return -1; |
| 653 | |
| 654 | r->num_entries--; |
| 655 | |
| 656 | return 0; |
| 657 | } |
| 658 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 659 | static void imdr_print_entries(const struct imdr *imdr, const char *indent, |
| 660 | const struct imd_lookup *lookup, size_t size) |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 661 | { |
| 662 | struct imd_root *r; |
| 663 | size_t i; |
| 664 | size_t j; |
| 665 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 666 | if (imdr == NULL) |
| 667 | return; |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 668 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 669 | r = imdr_root(imdr); |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 670 | |
| 671 | for (i = 0; i < r->num_entries; i++) { |
| 672 | const char *name = NULL; |
| 673 | const struct imd_entry *e = &r->entries[i]; |
| 674 | |
| 675 | for (j = 0; j < size; j++) { |
| 676 | if (lookup[j].id == e->id) { |
| 677 | name = lookup[j].name; |
| 678 | break; |
| 679 | } |
| 680 | } |
| 681 | |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 682 | printk(BIOS_DEBUG, "%s", indent); |
| 683 | |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 684 | if (name == NULL) |
| 685 | printk(BIOS_DEBUG, "%08x ", e->id); |
| 686 | else |
| 687 | printk(BIOS_DEBUG, "%s", name); |
| 688 | printk(BIOS_DEBUG, "%2zu. ", i); |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 689 | printk(BIOS_DEBUG, "%p ", imdr_entry_at(imdr, e)); |
Julius Werner | 540a980 | 2019-12-09 13:03:29 -0800 | [diff] [blame] | 690 | printk(BIOS_DEBUG, "0x%08zx\n", imdr_entry_size(imdr, e)); |
Aaron Durbin | cac5050 | 2015-03-24 23:14:46 -0500 | [diff] [blame] | 691 | } |
| 692 | } |
| 693 | |
| 694 | int imd_print_entries(const struct imd *imd, const struct imd_lookup *lookup, |
| 695 | size_t size) |
| 696 | { |
| 697 | if (imdr_root(&imd->lg) == NULL) |
| 698 | return -1; |
| 699 | |
| 700 | imdr_print_entries(&imd->lg, "", lookup, size); |
| 701 | if (imdr_root(&imd->sm) != NULL) { |
| 702 | printk(BIOS_DEBUG, "IMD small region:\n"); |
| 703 | imdr_print_entries(&imd->sm, " ", lookup, size); |
Aaron Durbin | 20686d8 | 2015-03-05 14:11:27 -0600 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | return 0; |
| 707 | } |
Aaron Durbin | 1ca2d86 | 2015-09-30 12:26:54 -0500 | [diff] [blame] | 708 | |
| 709 | int imd_cursor_init(const struct imd *imd, struct imd_cursor *cursor) |
| 710 | { |
| 711 | if (imd == NULL || cursor == NULL) |
| 712 | return -1; |
| 713 | |
| 714 | memset(cursor, 0, sizeof(*cursor)); |
| 715 | |
| 716 | cursor->imdr[0] = &imd->lg; |
| 717 | cursor->imdr[1] = &imd->sm; |
| 718 | |
| 719 | return 0; |
| 720 | } |
| 721 | |
| 722 | const struct imd_entry *imd_cursor_next(struct imd_cursor *cursor) |
| 723 | { |
| 724 | struct imd_root *r; |
| 725 | const struct imd_entry *e; |
| 726 | |
| 727 | if (cursor->current_imdr >= ARRAY_SIZE(cursor->imdr)) |
| 728 | return NULL; |
| 729 | |
| 730 | r = imdr_root(cursor->imdr[cursor->current_imdr]); |
| 731 | |
| 732 | if (r == NULL) |
| 733 | return NULL; |
| 734 | |
| 735 | if (cursor->current_entry >= r->num_entries) { |
| 736 | /* Try next imdr. */ |
| 737 | cursor->current_imdr++; |
| 738 | cursor->current_entry = 0; |
| 739 | return imd_cursor_next(cursor); |
| 740 | } |
| 741 | |
| 742 | e = &r->entries[cursor->current_entry]; |
| 743 | cursor->current_entry++; |
| 744 | |
| 745 | return e; |
| 746 | } |