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