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