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