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