Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 1 | /* |
| 2 | * EFI application memory management |
| 3 | * |
| 4 | * Copyright (c) 2016 Alexander Graf |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0+ |
| 7 | */ |
| 8 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 9 | #include <common.h> |
| 10 | #include <efi_loader.h> |
| 11 | #include <malloc.h> |
| 12 | #include <asm/global_data.h> |
| 13 | #include <libfdt_env.h> |
Alexander Graf | de2a13b | 2016-03-30 16:38:29 +0200 | [diff] [blame] | 14 | #include <linux/list_sort.h> |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 15 | #include <inttypes.h> |
| 16 | #include <watchdog.h> |
| 17 | |
| 18 | DECLARE_GLOBAL_DATA_PTR; |
| 19 | |
| 20 | struct efi_mem_list { |
| 21 | struct list_head link; |
| 22 | struct efi_mem_desc desc; |
| 23 | }; |
| 24 | |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 25 | #define EFI_CARVE_NO_OVERLAP -1 |
| 26 | #define EFI_CARVE_LOOP_AGAIN -2 |
| 27 | #define EFI_CARVE_OVERLAPS_NONRAM -3 |
| 28 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 29 | /* This list contains all memory map items */ |
| 30 | LIST_HEAD(efi_mem); |
| 31 | |
Alexander Graf | 7c00a3c | 2016-05-11 18:25:48 +0200 | [diff] [blame] | 32 | #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER |
| 33 | void *efi_bounce_buffer; |
| 34 | #endif |
| 35 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 36 | /* |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 37 | * U-Boot services each EFI AllocatePool request as a separate |
| 38 | * (multiple) page allocation. We have to track the number of pages |
| 39 | * to be able to free the correct amount later. |
| 40 | * EFI requires 8 byte alignment for pool allocations, so we can |
| 41 | * prepend each allocation with an 64 bit header tracking the |
| 42 | * allocation size, and hand out the remainder to the caller. |
| 43 | */ |
| 44 | struct efi_pool_allocation { |
| 45 | u64 num_pages; |
Rob Clark | e597fd4 | 2017-09-13 18:05:36 -0400 | [diff] [blame] | 46 | char data[] __aligned(ARCH_DMA_MINALIGN); |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | /* |
Alexander Graf | de2a13b | 2016-03-30 16:38:29 +0200 | [diff] [blame] | 50 | * Sorts the memory list from highest address to lowest address |
| 51 | * |
| 52 | * When allocating memory we should always start from the highest |
| 53 | * address chunk, so sort the memory list such that the first list |
| 54 | * iterator gets the highest address and goes lower from there. |
| 55 | */ |
| 56 | static int efi_mem_cmp(void *priv, struct list_head *a, struct list_head *b) |
| 57 | { |
| 58 | struct efi_mem_list *mema = list_entry(a, struct efi_mem_list, link); |
| 59 | struct efi_mem_list *memb = list_entry(b, struct efi_mem_list, link); |
| 60 | |
| 61 | if (mema->desc.physical_start == memb->desc.physical_start) |
| 62 | return 0; |
| 63 | else if (mema->desc.physical_start < memb->desc.physical_start) |
| 64 | return 1; |
| 65 | else |
| 66 | return -1; |
| 67 | } |
| 68 | |
| 69 | static void efi_mem_sort(void) |
| 70 | { |
| 71 | list_sort(NULL, &efi_mem, efi_mem_cmp); |
| 72 | } |
| 73 | |
| 74 | /* |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 75 | * Unmaps all memory occupied by the carve_desc region from the |
| 76 | * list entry pointed to by map. |
| 77 | * |
Stefan Brüns | 1b00dc1 | 2016-10-01 23:32:23 +0200 | [diff] [blame] | 78 | * Returns EFI_CARVE_NO_OVERLAP if the regions don't overlap. |
| 79 | * Returns EFI_CARVE_OVERLAPS_NONRAM if the carve and map overlap, |
| 80 | * and the map contains anything but free ram. |
| 81 | * (only when overlap_only_ram is true) |
| 82 | * Returns EFI_CARVE_LOOP_AGAIN if the mapping list should be traversed |
| 83 | * again, as it has been altered |
| 84 | * Returns the number of overlapping pages. The pages are removed from |
| 85 | * the mapping list. |
| 86 | * |
| 87 | * In case of EFI_CARVE_OVERLAPS_NONRAM it is the callers responsibility |
| 88 | * to readd the already carved out pages to the mapping. |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 89 | */ |
| 90 | static int efi_mem_carve_out(struct efi_mem_list *map, |
| 91 | struct efi_mem_desc *carve_desc, |
| 92 | bool overlap_only_ram) |
| 93 | { |
| 94 | struct efi_mem_list *newmap; |
| 95 | struct efi_mem_desc *map_desc = &map->desc; |
| 96 | uint64_t map_start = map_desc->physical_start; |
| 97 | uint64_t map_end = map_start + (map_desc->num_pages << EFI_PAGE_SHIFT); |
| 98 | uint64_t carve_start = carve_desc->physical_start; |
| 99 | uint64_t carve_end = carve_start + |
| 100 | (carve_desc->num_pages << EFI_PAGE_SHIFT); |
| 101 | |
| 102 | /* check whether we're overlapping */ |
| 103 | if ((carve_end <= map_start) || (carve_start >= map_end)) |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 104 | return EFI_CARVE_NO_OVERLAP; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 105 | |
| 106 | /* We're overlapping with non-RAM, warn the caller if desired */ |
| 107 | if (overlap_only_ram && (map_desc->type != EFI_CONVENTIONAL_MEMORY)) |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 108 | return EFI_CARVE_OVERLAPS_NONRAM; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 109 | |
| 110 | /* Sanitize carve_start and carve_end to lie within our bounds */ |
| 111 | carve_start = max(carve_start, map_start); |
| 112 | carve_end = min(carve_end, map_end); |
| 113 | |
| 114 | /* Carving at the beginning of our map? Just move it! */ |
| 115 | if (carve_start == map_start) { |
| 116 | if (map_end == carve_end) { |
| 117 | /* Full overlap, just remove map */ |
| 118 | list_del(&map->link); |
Stefan Brüns | 4e9ad8d | 2016-10-01 23:32:29 +0200 | [diff] [blame] | 119 | free(map); |
| 120 | } else { |
| 121 | map->desc.physical_start = carve_end; |
| 122 | map->desc.num_pages = (map_end - carve_end) |
| 123 | >> EFI_PAGE_SHIFT; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 124 | } |
| 125 | |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 126 | return (carve_end - carve_start) >> EFI_PAGE_SHIFT; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /* |
| 130 | * Overlapping maps, just split the list map at carve_start, |
| 131 | * it will get moved or removed in the next iteration. |
| 132 | * |
| 133 | * [ map_desc |__carve_start__| newmap ] |
| 134 | */ |
| 135 | |
| 136 | /* Create a new map from [ carve_start ... map_end ] */ |
| 137 | newmap = calloc(1, sizeof(*newmap)); |
| 138 | newmap->desc = map->desc; |
| 139 | newmap->desc.physical_start = carve_start; |
| 140 | newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT; |
Stefan Brüns | 9d65651 | 2016-10-01 23:32:28 +0200 | [diff] [blame] | 141 | /* Insert before current entry (descending address order) */ |
| 142 | list_add_tail(&newmap->link, &map->link); |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 143 | |
| 144 | /* Shrink the map to [ map_start ... carve_start ] */ |
| 145 | map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT; |
| 146 | |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 147 | return EFI_CARVE_LOOP_AGAIN; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type, |
| 151 | bool overlap_only_ram) |
| 152 | { |
| 153 | struct list_head *lhandle; |
| 154 | struct efi_mem_list *newlist; |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 155 | bool carve_again; |
| 156 | uint64_t carved_pages = 0; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 157 | |
Andreas Färber | 102f8bd | 2016-07-17 06:57:11 +0200 | [diff] [blame] | 158 | debug("%s: 0x%" PRIx64 " 0x%" PRIx64 " %d %s\n", __func__, |
| 159 | start, pages, memory_type, overlap_only_ram ? "yes" : "no"); |
| 160 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 161 | if (!pages) |
| 162 | return start; |
| 163 | |
| 164 | newlist = calloc(1, sizeof(*newlist)); |
| 165 | newlist->desc.type = memory_type; |
| 166 | newlist->desc.physical_start = start; |
| 167 | newlist->desc.virtual_start = start; |
| 168 | newlist->desc.num_pages = pages; |
| 169 | |
| 170 | switch (memory_type) { |
| 171 | case EFI_RUNTIME_SERVICES_CODE: |
| 172 | case EFI_RUNTIME_SERVICES_DATA: |
| 173 | newlist->desc.attribute = (1 << EFI_MEMORY_WB_SHIFT) | |
| 174 | (1ULL << EFI_MEMORY_RUNTIME_SHIFT); |
| 175 | break; |
| 176 | case EFI_MMAP_IO: |
| 177 | newlist->desc.attribute = 1ULL << EFI_MEMORY_RUNTIME_SHIFT; |
| 178 | break; |
| 179 | default: |
| 180 | newlist->desc.attribute = 1 << EFI_MEMORY_WB_SHIFT; |
| 181 | break; |
| 182 | } |
| 183 | |
| 184 | /* Add our new map */ |
| 185 | do { |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 186 | carve_again = false; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 187 | list_for_each(lhandle, &efi_mem) { |
| 188 | struct efi_mem_list *lmem; |
| 189 | int r; |
| 190 | |
| 191 | lmem = list_entry(lhandle, struct efi_mem_list, link); |
| 192 | r = efi_mem_carve_out(lmem, &newlist->desc, |
| 193 | overlap_only_ram); |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 194 | switch (r) { |
| 195 | case EFI_CARVE_OVERLAPS_NONRAM: |
| 196 | /* |
| 197 | * The user requested to only have RAM overlaps, |
| 198 | * but we hit a non-RAM region. Error out. |
| 199 | */ |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 200 | return 0; |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 201 | case EFI_CARVE_NO_OVERLAP: |
| 202 | /* Just ignore this list entry */ |
| 203 | break; |
| 204 | case EFI_CARVE_LOOP_AGAIN: |
| 205 | /* |
| 206 | * We split an entry, but need to loop through |
| 207 | * the list again to actually carve it. |
| 208 | */ |
| 209 | carve_again = true; |
| 210 | break; |
| 211 | default: |
| 212 | /* We carved a number of pages */ |
| 213 | carved_pages += r; |
| 214 | carve_again = true; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 215 | break; |
| 216 | } |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 217 | |
| 218 | if (carve_again) { |
| 219 | /* The list changed, we need to start over */ |
| 220 | break; |
| 221 | } |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 222 | } |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 223 | } while (carve_again); |
| 224 | |
| 225 | if (overlap_only_ram && (carved_pages != pages)) { |
| 226 | /* |
| 227 | * The payload wanted to have RAM overlaps, but we overlapped |
| 228 | * with an unallocated region. Error out. |
| 229 | */ |
| 230 | return 0; |
| 231 | } |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 232 | |
| 233 | /* Add our new map */ |
| 234 | list_add_tail(&newlist->link, &efi_mem); |
| 235 | |
Alexander Graf | de2a13b | 2016-03-30 16:38:29 +0200 | [diff] [blame] | 236 | /* And make sure memory is listed in descending order */ |
| 237 | efi_mem_sort(); |
| 238 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 239 | return start; |
| 240 | } |
| 241 | |
| 242 | static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr) |
| 243 | { |
| 244 | struct list_head *lhandle; |
| 245 | |
| 246 | list_for_each(lhandle, &efi_mem) { |
| 247 | struct efi_mem_list *lmem = list_entry(lhandle, |
| 248 | struct efi_mem_list, link); |
| 249 | struct efi_mem_desc *desc = &lmem->desc; |
| 250 | uint64_t desc_len = desc->num_pages << EFI_PAGE_SHIFT; |
| 251 | uint64_t desc_end = desc->physical_start + desc_len; |
| 252 | uint64_t curmax = min(max_addr, desc_end); |
| 253 | uint64_t ret = curmax - len; |
| 254 | |
| 255 | /* We only take memory from free RAM */ |
| 256 | if (desc->type != EFI_CONVENTIONAL_MEMORY) |
| 257 | continue; |
| 258 | |
| 259 | /* Out of bounds for max_addr */ |
| 260 | if ((ret + len) > max_addr) |
| 261 | continue; |
| 262 | |
| 263 | /* Out of bounds for upper map limit */ |
| 264 | if ((ret + len) > desc_end) |
| 265 | continue; |
| 266 | |
| 267 | /* Out of bounds for lower map limit */ |
| 268 | if (ret < desc->physical_start) |
| 269 | continue; |
| 270 | |
| 271 | /* Return the highest address in this map within bounds */ |
| 272 | return ret; |
| 273 | } |
| 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame^] | 278 | /* |
| 279 | * Allocate memory pages. |
| 280 | * |
| 281 | * @type type of allocation to be performed |
| 282 | * @memory_type usage type of the allocated memory |
| 283 | * @pages number of pages to be allocated |
| 284 | * @memory allocated memory |
| 285 | * @return status code |
| 286 | */ |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 287 | efi_status_t efi_allocate_pages(int type, int memory_type, |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 288 | efi_uintn_t pages, uint64_t *memory) |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 289 | { |
| 290 | u64 len = pages << EFI_PAGE_SHIFT; |
| 291 | efi_status_t r = EFI_SUCCESS; |
| 292 | uint64_t addr; |
| 293 | |
| 294 | switch (type) { |
| 295 | case 0: |
| 296 | /* Any page */ |
Andreas Färber | d40f71e | 2016-04-13 14:04:38 +0200 | [diff] [blame] | 297 | addr = efi_find_free_memory(len, gd->start_addr_sp); |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 298 | if (!addr) { |
| 299 | r = EFI_NOT_FOUND; |
| 300 | break; |
| 301 | } |
| 302 | break; |
| 303 | case 1: |
| 304 | /* Max address */ |
| 305 | addr = efi_find_free_memory(len, *memory); |
| 306 | if (!addr) { |
| 307 | r = EFI_NOT_FOUND; |
| 308 | break; |
| 309 | } |
| 310 | break; |
| 311 | case 2: |
| 312 | /* Exact address, reserve it. The addr is already in *memory. */ |
| 313 | addr = *memory; |
| 314 | break; |
| 315 | default: |
| 316 | /* UEFI doesn't specify other allocation types */ |
| 317 | r = EFI_INVALID_PARAMETER; |
| 318 | break; |
| 319 | } |
| 320 | |
| 321 | if (r == EFI_SUCCESS) { |
| 322 | uint64_t ret; |
| 323 | |
| 324 | /* Reserve that map in our memory maps */ |
| 325 | ret = efi_add_memory_map(addr, pages, memory_type, true); |
| 326 | if (ret == addr) { |
| 327 | *memory = addr; |
| 328 | } else { |
| 329 | /* Map would overlap, bail out */ |
| 330 | r = EFI_OUT_OF_RESOURCES; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | return r; |
| 335 | } |
| 336 | |
| 337 | void *efi_alloc(uint64_t len, int memory_type) |
| 338 | { |
| 339 | uint64_t ret = 0; |
| 340 | uint64_t pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT; |
| 341 | efi_status_t r; |
| 342 | |
| 343 | r = efi_allocate_pages(0, memory_type, pages, &ret); |
| 344 | if (r == EFI_SUCCESS) |
| 345 | return (void*)(uintptr_t)ret; |
| 346 | |
| 347 | return NULL; |
| 348 | } |
| 349 | |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame^] | 350 | /* |
| 351 | * Free memory pages. |
| 352 | * |
| 353 | * @memory start of the memory area to be freed |
| 354 | * @pages number of pages to be freed |
| 355 | * @return status code |
| 356 | */ |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 357 | efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages) |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 358 | { |
Stefan Brüns | a4f4302 | 2016-10-01 23:32:27 +0200 | [diff] [blame] | 359 | uint64_t r = 0; |
| 360 | |
| 361 | r = efi_add_memory_map(memory, pages, EFI_CONVENTIONAL_MEMORY, false); |
| 362 | /* Merging of adjacent free regions is missing */ |
| 363 | |
| 364 | if (r == memory) |
| 365 | return EFI_SUCCESS; |
| 366 | |
| 367 | return EFI_NOT_FOUND; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 368 | } |
| 369 | |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame^] | 370 | /* |
| 371 | * Allocate memory from pool. |
| 372 | * |
| 373 | * @pool_type type of the pool from which memory is to be allocated |
| 374 | * @size number of bytes to be allocated |
| 375 | * @buffer allocated memory |
| 376 | * @return status code |
| 377 | */ |
| 378 | efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size, void **buffer) |
Stefan Brüns | 5a09aef | 2016-10-09 22:17:18 +0200 | [diff] [blame] | 379 | { |
| 380 | efi_status_t r; |
| 381 | efi_physical_addr_t t; |
Rob Clark | e597fd4 | 2017-09-13 18:05:36 -0400 | [diff] [blame] | 382 | u64 num_pages = (size + sizeof(struct efi_pool_allocation) + |
| 383 | EFI_PAGE_MASK) >> EFI_PAGE_SHIFT; |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 384 | |
| 385 | if (size == 0) { |
| 386 | *buffer = NULL; |
| 387 | return EFI_SUCCESS; |
| 388 | } |
Stefan Brüns | 5a09aef | 2016-10-09 22:17:18 +0200 | [diff] [blame] | 389 | |
| 390 | r = efi_allocate_pages(0, pool_type, num_pages, &t); |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 391 | |
| 392 | if (r == EFI_SUCCESS) { |
| 393 | struct efi_pool_allocation *alloc = (void *)(uintptr_t)t; |
| 394 | alloc->num_pages = num_pages; |
| 395 | *buffer = alloc->data; |
| 396 | } |
| 397 | |
| 398 | return r; |
| 399 | } |
| 400 | |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame^] | 401 | /* |
| 402 | * Free memory from pool. |
| 403 | * |
| 404 | * @buffer start of memory to be freed |
| 405 | * @return status code |
| 406 | */ |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 407 | efi_status_t efi_free_pool(void *buffer) |
| 408 | { |
| 409 | efi_status_t r; |
| 410 | struct efi_pool_allocation *alloc; |
| 411 | |
xypron.glpk@gmx.de | 34ddae7 | 2017-07-14 19:12:39 +0200 | [diff] [blame] | 412 | if (buffer == NULL) |
| 413 | return EFI_INVALID_PARAMETER; |
| 414 | |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 415 | alloc = container_of(buffer, struct efi_pool_allocation, data); |
| 416 | /* Sanity check, was the supplied address returned by allocate_pool */ |
| 417 | assert(((uintptr_t)alloc & EFI_PAGE_MASK) == 0); |
| 418 | |
| 419 | r = efi_free_pages((uintptr_t)alloc, alloc->num_pages); |
Stefan Brüns | 5a09aef | 2016-10-09 22:17:18 +0200 | [diff] [blame] | 420 | |
| 421 | return r; |
| 422 | } |
| 423 | |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame^] | 424 | /* |
| 425 | * Get map describing memory usage. |
| 426 | * |
| 427 | * @memory_map_size on entry the size, in bytes, of the memory map buffer, |
| 428 | * on exit the size of the copied memory map |
| 429 | * @memory_map buffer to which the memory map is written |
| 430 | * @map_key key for the memory map |
| 431 | * @descriptor_size size of an individual memory descriptor |
| 432 | * @descriptor_version version number of the memory descriptor structure |
| 433 | * @return status code |
| 434 | */ |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 435 | efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size, |
| 436 | struct efi_mem_desc *memory_map, |
| 437 | efi_uintn_t *map_key, |
| 438 | efi_uintn_t *descriptor_size, |
| 439 | uint32_t *descriptor_version) |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 440 | { |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 441 | efi_uintn_t map_size = 0; |
Alexander Graf | 9b59102 | 2016-04-11 23:51:02 +0200 | [diff] [blame] | 442 | int map_entries = 0; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 443 | struct list_head *lhandle; |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 444 | efi_uintn_t provided_map_size = *memory_map_size; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 445 | |
| 446 | list_for_each(lhandle, &efi_mem) |
Alexander Graf | 9b59102 | 2016-04-11 23:51:02 +0200 | [diff] [blame] | 447 | map_entries++; |
| 448 | |
| 449 | map_size = map_entries * sizeof(struct efi_mem_desc); |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 450 | |
Rob Clark | a84c974 | 2017-07-26 14:34:05 -0400 | [diff] [blame] | 451 | *memory_map_size = map_size; |
| 452 | |
xypron.glpk@gmx.de | 5487ab5 | 2017-07-21 19:04:33 +0200 | [diff] [blame] | 453 | if (provided_map_size < map_size) |
| 454 | return EFI_BUFFER_TOO_SMALL; |
| 455 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 456 | if (descriptor_size) |
| 457 | *descriptor_size = sizeof(struct efi_mem_desc); |
| 458 | |
Mian Yousaf Kaukab | 338cf56 | 2016-09-05 23:59:22 +0200 | [diff] [blame] | 459 | if (descriptor_version) |
| 460 | *descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION; |
| 461 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 462 | /* Copy list into array */ |
| 463 | if (memory_map) { |
Alexander Graf | 9b59102 | 2016-04-11 23:51:02 +0200 | [diff] [blame] | 464 | /* Return the list in ascending order */ |
| 465 | memory_map = &memory_map[map_entries - 1]; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 466 | list_for_each(lhandle, &efi_mem) { |
| 467 | struct efi_mem_list *lmem; |
| 468 | |
| 469 | lmem = list_entry(lhandle, struct efi_mem_list, link); |
| 470 | *memory_map = lmem->desc; |
Alexander Graf | 9b59102 | 2016-04-11 23:51:02 +0200 | [diff] [blame] | 471 | memory_map--; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | |
xypron.glpk@gmx.de | e436e63 | 2017-07-21 19:05:44 +0200 | [diff] [blame] | 475 | *map_key = 0; |
| 476 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 477 | return EFI_SUCCESS; |
| 478 | } |
| 479 | |
York Sun | a8e9f0a | 2017-03-06 09:02:27 -0800 | [diff] [blame] | 480 | __weak void efi_add_known_memory(void) |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 481 | { |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 482 | int i; |
| 483 | |
| 484 | /* Add RAM */ |
| 485 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { |
| 486 | u64 ram_start = gd->bd->bi_dram[i].start; |
| 487 | u64 ram_size = gd->bd->bi_dram[i].size; |
| 488 | u64 start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK; |
| 489 | u64 pages = (ram_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT; |
| 490 | |
| 491 | efi_add_memory_map(start, pages, EFI_CONVENTIONAL_MEMORY, |
| 492 | false); |
| 493 | } |
York Sun | a8e9f0a | 2017-03-06 09:02:27 -0800 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | int efi_memory_init(void) |
| 497 | { |
| 498 | unsigned long runtime_start, runtime_end, runtime_pages; |
| 499 | unsigned long uboot_start, uboot_pages; |
| 500 | unsigned long uboot_stack_size = 16 * 1024 * 1024; |
| 501 | |
| 502 | efi_add_known_memory(); |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 503 | |
| 504 | /* Add U-Boot */ |
| 505 | uboot_start = (gd->start_addr_sp - uboot_stack_size) & ~EFI_PAGE_MASK; |
| 506 | uboot_pages = (gd->ram_top - uboot_start) >> EFI_PAGE_SHIFT; |
| 507 | efi_add_memory_map(uboot_start, uboot_pages, EFI_LOADER_DATA, false); |
| 508 | |
| 509 | /* Add Runtime Services */ |
| 510 | runtime_start = (ulong)&__efi_runtime_start & ~EFI_PAGE_MASK; |
| 511 | runtime_end = (ulong)&__efi_runtime_stop; |
| 512 | runtime_end = (runtime_end + EFI_PAGE_MASK) & ~EFI_PAGE_MASK; |
| 513 | runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT; |
| 514 | efi_add_memory_map(runtime_start, runtime_pages, |
| 515 | EFI_RUNTIME_SERVICES_CODE, false); |
| 516 | |
Alexander Graf | 7c00a3c | 2016-05-11 18:25:48 +0200 | [diff] [blame] | 517 | #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER |
| 518 | /* Request a 32bit 64MB bounce buffer region */ |
| 519 | uint64_t efi_bounce_buffer_addr = 0xffffffff; |
| 520 | |
| 521 | if (efi_allocate_pages(1, EFI_LOADER_DATA, |
| 522 | (64 * 1024 * 1024) >> EFI_PAGE_SHIFT, |
| 523 | &efi_bounce_buffer_addr) != EFI_SUCCESS) |
| 524 | return -1; |
| 525 | |
| 526 | efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr; |
| 527 | #endif |
| 528 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 529 | return 0; |
| 530 | } |