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