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 | 15937a2 | 2018-09-17 13:54:33 +0200 | [diff] [blame] | 14 | #include <linux/sizes.h> |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 15 | |
| 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 18 | /* Magic number identifying memory allocated from pool */ |
| 19 | #define EFI_ALLOC_POOL_MAGIC 0x1fe67ddf6491caa2 |
| 20 | |
Heinrich Schuchardt | 0f233c4 | 2018-07-02 12:53:55 +0200 | [diff] [blame] | 21 | efi_uintn_t efi_memory_map_key; |
| 22 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 23 | struct efi_mem_list { |
| 24 | struct list_head link; |
| 25 | struct efi_mem_desc desc; |
| 26 | }; |
| 27 | |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 28 | #define EFI_CARVE_NO_OVERLAP -1 |
| 29 | #define EFI_CARVE_LOOP_AGAIN -2 |
| 30 | #define EFI_CARVE_OVERLAPS_NONRAM -3 |
| 31 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 32 | /* This list contains all memory map items */ |
| 33 | LIST_HEAD(efi_mem); |
| 34 | |
Alexander Graf | 7c00a3c | 2016-05-11 18:25:48 +0200 | [diff] [blame] | 35 | #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER |
| 36 | void *efi_bounce_buffer; |
| 37 | #endif |
| 38 | |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 39 | /** |
| 40 | * efi_pool_allocation - memory block allocated from pool |
| 41 | * |
| 42 | * @num_pages: number of pages allocated |
| 43 | * @checksum: checksum |
| 44 | * |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 45 | * U-Boot services each EFI AllocatePool request as a separate |
| 46 | * (multiple) page allocation. We have to track the number of pages |
| 47 | * to be able to free the correct amount later. |
| 48 | * EFI requires 8 byte alignment for pool allocations, so we can |
| 49 | * prepend each allocation with an 64 bit header tracking the |
| 50 | * allocation size, and hand out the remainder to the caller. |
| 51 | */ |
| 52 | struct efi_pool_allocation { |
| 53 | u64 num_pages; |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 54 | u64 checksum; |
Rob Clark | e597fd4 | 2017-09-13 18:05:36 -0400 | [diff] [blame] | 55 | char data[] __aligned(ARCH_DMA_MINALIGN); |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 56 | }; |
| 57 | |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 58 | /** |
| 59 | * checksum() - calculate checksum for memory allocated from pool |
| 60 | * |
| 61 | * @alloc: allocation header |
| 62 | * Return: checksum, always non-zero |
| 63 | */ |
| 64 | static u64 checksum(struct efi_pool_allocation *alloc) |
| 65 | { |
| 66 | u64 addr = (uintptr_t)alloc; |
| 67 | u64 ret = (addr >> 32) ^ (addr << 32) ^ alloc->num_pages ^ |
| 68 | EFI_ALLOC_POOL_MAGIC; |
| 69 | if (!ret) |
| 70 | ++ret; |
| 71 | return ret; |
| 72 | } |
| 73 | |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 74 | /* |
Alexander Graf | de2a13b | 2016-03-30 16:38:29 +0200 | [diff] [blame] | 75 | * Sorts the memory list from highest address to lowest address |
| 76 | * |
| 77 | * When allocating memory we should always start from the highest |
| 78 | * address chunk, so sort the memory list such that the first list |
| 79 | * iterator gets the highest address and goes lower from there. |
| 80 | */ |
| 81 | static int efi_mem_cmp(void *priv, struct list_head *a, struct list_head *b) |
| 82 | { |
| 83 | struct efi_mem_list *mema = list_entry(a, struct efi_mem_list, link); |
| 84 | struct efi_mem_list *memb = list_entry(b, struct efi_mem_list, link); |
| 85 | |
| 86 | if (mema->desc.physical_start == memb->desc.physical_start) |
| 87 | return 0; |
| 88 | else if (mema->desc.physical_start < memb->desc.physical_start) |
| 89 | return 1; |
| 90 | else |
| 91 | return -1; |
| 92 | } |
| 93 | |
Alexander Graf | d728814 | 2018-09-16 17:05:29 +0200 | [diff] [blame] | 94 | static uint64_t desc_get_end(struct efi_mem_desc *desc) |
| 95 | { |
| 96 | return desc->physical_start + (desc->num_pages << EFI_PAGE_SHIFT); |
| 97 | } |
| 98 | |
Alexander Graf | de2a13b | 2016-03-30 16:38:29 +0200 | [diff] [blame] | 99 | static void efi_mem_sort(void) |
| 100 | { |
Alexander Graf | d728814 | 2018-09-16 17:05:29 +0200 | [diff] [blame] | 101 | struct list_head *lhandle; |
| 102 | struct efi_mem_list *prevmem = NULL; |
| 103 | bool merge_again = true; |
| 104 | |
Alexander Graf | de2a13b | 2016-03-30 16:38:29 +0200 | [diff] [blame] | 105 | list_sort(NULL, &efi_mem, efi_mem_cmp); |
Alexander Graf | d728814 | 2018-09-16 17:05:29 +0200 | [diff] [blame] | 106 | |
| 107 | /* Now merge entries that can be merged */ |
| 108 | while (merge_again) { |
| 109 | merge_again = false; |
| 110 | list_for_each(lhandle, &efi_mem) { |
| 111 | struct efi_mem_list *lmem; |
| 112 | struct efi_mem_desc *prev = &prevmem->desc; |
| 113 | struct efi_mem_desc *cur; |
| 114 | uint64_t pages; |
| 115 | |
| 116 | lmem = list_entry(lhandle, struct efi_mem_list, link); |
| 117 | if (!prevmem) { |
| 118 | prevmem = lmem; |
| 119 | continue; |
| 120 | } |
| 121 | |
| 122 | cur = &lmem->desc; |
| 123 | |
| 124 | if ((desc_get_end(cur) == prev->physical_start) && |
| 125 | (prev->type == cur->type) && |
| 126 | (prev->attribute == cur->attribute)) { |
| 127 | /* There is an existing map before, reuse it */ |
| 128 | pages = cur->num_pages; |
| 129 | prev->num_pages += pages; |
| 130 | prev->physical_start -= pages << EFI_PAGE_SHIFT; |
| 131 | prev->virtual_start -= pages << EFI_PAGE_SHIFT; |
| 132 | list_del(&lmem->link); |
| 133 | free(lmem); |
| 134 | |
| 135 | merge_again = true; |
| 136 | break; |
| 137 | } |
| 138 | |
| 139 | prevmem = lmem; |
| 140 | } |
| 141 | } |
Alexander Graf | de2a13b | 2016-03-30 16:38:29 +0200 | [diff] [blame] | 142 | } |
| 143 | |
Heinrich Schuchardt | f00caa5 | 2018-05-27 16:45:09 +0200 | [diff] [blame] | 144 | /** efi_mem_carve_out - unmap memory region |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 145 | * |
Heinrich Schuchardt | f00caa5 | 2018-05-27 16:45:09 +0200 | [diff] [blame] | 146 | * @map: memory map |
| 147 | * @carve_desc: memory region to unmap |
| 148 | * @overlap_only_ram: the carved out region may only overlap RAM |
| 149 | * Return Value: the number of overlapping pages which have been |
| 150 | * removed from the map, |
| 151 | * EFI_CARVE_NO_OVERLAP, if the regions don't overlap, |
| 152 | * EFI_CARVE_OVERLAPS_NONRAM, if the carve and map overlap, |
| 153 | * and the map contains anything but free ram |
| 154 | * (only when overlap_only_ram is true), |
| 155 | * EFI_CARVE_LOOP_AGAIN, if the mapping list should be |
| 156 | * traversed again, as it has been altered. |
| 157 | * |
| 158 | * Unmaps all memory occupied by the carve_desc region from the list entry |
| 159 | * pointed to by map. |
Stefan Brüns | 1b00dc1 | 2016-10-01 23:32:23 +0200 | [diff] [blame] | 160 | * |
| 161 | * In case of EFI_CARVE_OVERLAPS_NONRAM it is the callers responsibility |
Heinrich Schuchardt | f00caa5 | 2018-05-27 16:45:09 +0200 | [diff] [blame] | 162 | * to re-add the already carved out pages to the mapping. |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 163 | */ |
Heinrich Schuchardt | f00caa5 | 2018-05-27 16:45:09 +0200 | [diff] [blame] | 164 | static s64 efi_mem_carve_out(struct efi_mem_list *map, |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 165 | struct efi_mem_desc *carve_desc, |
| 166 | bool overlap_only_ram) |
| 167 | { |
| 168 | struct efi_mem_list *newmap; |
| 169 | struct efi_mem_desc *map_desc = &map->desc; |
| 170 | uint64_t map_start = map_desc->physical_start; |
| 171 | uint64_t map_end = map_start + (map_desc->num_pages << EFI_PAGE_SHIFT); |
| 172 | uint64_t carve_start = carve_desc->physical_start; |
| 173 | uint64_t carve_end = carve_start + |
| 174 | (carve_desc->num_pages << EFI_PAGE_SHIFT); |
| 175 | |
| 176 | /* check whether we're overlapping */ |
| 177 | if ((carve_end <= map_start) || (carve_start >= map_end)) |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 178 | return EFI_CARVE_NO_OVERLAP; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 179 | |
| 180 | /* We're overlapping with non-RAM, warn the caller if desired */ |
| 181 | if (overlap_only_ram && (map_desc->type != EFI_CONVENTIONAL_MEMORY)) |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 182 | return EFI_CARVE_OVERLAPS_NONRAM; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 183 | |
| 184 | /* Sanitize carve_start and carve_end to lie within our bounds */ |
| 185 | carve_start = max(carve_start, map_start); |
| 186 | carve_end = min(carve_end, map_end); |
| 187 | |
| 188 | /* Carving at the beginning of our map? Just move it! */ |
| 189 | if (carve_start == map_start) { |
| 190 | if (map_end == carve_end) { |
| 191 | /* Full overlap, just remove map */ |
| 192 | list_del(&map->link); |
Stefan Brüns | 4e9ad8d | 2016-10-01 23:32:29 +0200 | [diff] [blame] | 193 | free(map); |
| 194 | } else { |
| 195 | map->desc.physical_start = carve_end; |
Heinrich Schuchardt | e83aafa | 2019-04-11 20:08:54 +0200 | [diff] [blame] | 196 | map->desc.virtual_start = carve_end; |
Stefan Brüns | 4e9ad8d | 2016-10-01 23:32:29 +0200 | [diff] [blame] | 197 | map->desc.num_pages = (map_end - carve_end) |
| 198 | >> EFI_PAGE_SHIFT; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 199 | } |
| 200 | |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 201 | return (carve_end - carve_start) >> EFI_PAGE_SHIFT; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /* |
| 205 | * Overlapping maps, just split the list map at carve_start, |
| 206 | * it will get moved or removed in the next iteration. |
| 207 | * |
| 208 | * [ map_desc |__carve_start__| newmap ] |
| 209 | */ |
| 210 | |
| 211 | /* Create a new map from [ carve_start ... map_end ] */ |
| 212 | newmap = calloc(1, sizeof(*newmap)); |
| 213 | newmap->desc = map->desc; |
| 214 | newmap->desc.physical_start = carve_start; |
Heinrich Schuchardt | e83aafa | 2019-04-11 20:08:54 +0200 | [diff] [blame] | 215 | newmap->desc.virtual_start = carve_start; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 216 | newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT; |
Stefan Brüns | 9d65651 | 2016-10-01 23:32:28 +0200 | [diff] [blame] | 217 | /* Insert before current entry (descending address order) */ |
| 218 | list_add_tail(&newmap->link, &map->link); |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 219 | |
| 220 | /* Shrink the map to [ map_start ... carve_start ] */ |
| 221 | map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT; |
| 222 | |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 223 | return EFI_CARVE_LOOP_AGAIN; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type, |
| 227 | bool overlap_only_ram) |
| 228 | { |
| 229 | struct list_head *lhandle; |
| 230 | struct efi_mem_list *newlist; |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 231 | bool carve_again; |
| 232 | uint64_t carved_pages = 0; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 233 | |
Heinrich Schuchardt | 706a2fe | 2019-04-04 21:50:02 +0200 | [diff] [blame] | 234 | EFI_PRINT("%s: 0x%llx 0x%llx %d %s\n", __func__, |
| 235 | start, pages, memory_type, overlap_only_ram ? "yes" : "no"); |
Andreas Färber | 102f8bd | 2016-07-17 06:57:11 +0200 | [diff] [blame] | 236 | |
Heinrich Schuchardt | 0f233c4 | 2018-07-02 12:53:55 +0200 | [diff] [blame] | 237 | if (memory_type >= EFI_MAX_MEMORY_TYPE) |
| 238 | return EFI_INVALID_PARAMETER; |
| 239 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 240 | if (!pages) |
| 241 | return start; |
| 242 | |
Heinrich Schuchardt | 0f233c4 | 2018-07-02 12:53:55 +0200 | [diff] [blame] | 243 | ++efi_memory_map_key; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 244 | newlist = calloc(1, sizeof(*newlist)); |
| 245 | newlist->desc.type = memory_type; |
| 246 | newlist->desc.physical_start = start; |
| 247 | newlist->desc.virtual_start = start; |
| 248 | newlist->desc.num_pages = pages; |
| 249 | |
| 250 | switch (memory_type) { |
| 251 | case EFI_RUNTIME_SERVICES_CODE: |
| 252 | case EFI_RUNTIME_SERVICES_DATA: |
Eugeniu Rosca | c86c081 | 2018-07-14 22:53:30 +0200 | [diff] [blame] | 253 | newlist->desc.attribute = EFI_MEMORY_WB | EFI_MEMORY_RUNTIME; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 254 | break; |
| 255 | case EFI_MMAP_IO: |
Eugeniu Rosca | c86c081 | 2018-07-14 22:53:30 +0200 | [diff] [blame] | 256 | newlist->desc.attribute = EFI_MEMORY_RUNTIME; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 257 | break; |
| 258 | default: |
Eugeniu Rosca | c86c081 | 2018-07-14 22:53:30 +0200 | [diff] [blame] | 259 | newlist->desc.attribute = EFI_MEMORY_WB; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 260 | break; |
| 261 | } |
| 262 | |
| 263 | /* Add our new map */ |
| 264 | do { |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 265 | carve_again = false; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 266 | list_for_each(lhandle, &efi_mem) { |
| 267 | struct efi_mem_list *lmem; |
Heinrich Schuchardt | f00caa5 | 2018-05-27 16:45:09 +0200 | [diff] [blame] | 268 | s64 r; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 269 | |
| 270 | lmem = list_entry(lhandle, struct efi_mem_list, link); |
| 271 | r = efi_mem_carve_out(lmem, &newlist->desc, |
| 272 | overlap_only_ram); |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 273 | switch (r) { |
| 274 | case EFI_CARVE_OVERLAPS_NONRAM: |
| 275 | /* |
| 276 | * The user requested to only have RAM overlaps, |
| 277 | * but we hit a non-RAM region. Error out. |
| 278 | */ |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 279 | return 0; |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 280 | case EFI_CARVE_NO_OVERLAP: |
| 281 | /* Just ignore this list entry */ |
| 282 | break; |
| 283 | case EFI_CARVE_LOOP_AGAIN: |
| 284 | /* |
| 285 | * We split an entry, but need to loop through |
| 286 | * the list again to actually carve it. |
| 287 | */ |
| 288 | carve_again = true; |
| 289 | break; |
| 290 | default: |
| 291 | /* We carved a number of pages */ |
| 292 | carved_pages += r; |
| 293 | carve_again = true; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 294 | break; |
| 295 | } |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 296 | |
| 297 | if (carve_again) { |
| 298 | /* The list changed, we need to start over */ |
| 299 | break; |
| 300 | } |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 301 | } |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 302 | } while (carve_again); |
| 303 | |
| 304 | if (overlap_only_ram && (carved_pages != pages)) { |
| 305 | /* |
| 306 | * The payload wanted to have RAM overlaps, but we overlapped |
| 307 | * with an unallocated region. Error out. |
| 308 | */ |
| 309 | return 0; |
| 310 | } |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 311 | |
| 312 | /* Add our new map */ |
| 313 | list_add_tail(&newlist->link, &efi_mem); |
| 314 | |
Alexander Graf | de2a13b | 2016-03-30 16:38:29 +0200 | [diff] [blame] | 315 | /* And make sure memory is listed in descending order */ |
| 316 | efi_mem_sort(); |
| 317 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 318 | return start; |
| 319 | } |
| 320 | |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 321 | /** |
| 322 | * efi_check_allocated() - validate address to be freed |
| 323 | * |
| 324 | * Check that the address is within allocated memory: |
| 325 | * |
| 326 | * * The address cannot be NULL. |
| 327 | * * The address must be in a range of the memory map. |
| 328 | * * The address may not point to EFI_CONVENTIONAL_MEMORY. |
| 329 | * |
| 330 | * Page alignment is not checked as this is not a requirement of |
| 331 | * efi_free_pool(). |
| 332 | * |
Heinrich Schuchardt | 906f685 | 2019-05-11 08:21:17 +0200 | [diff] [blame] | 333 | * @addr: address of page to be freed |
| 334 | * @must_be_allocated: return success if the page is allocated |
| 335 | * Return: status code |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 336 | */ |
Heinrich Schuchardt | 906f685 | 2019-05-11 08:21:17 +0200 | [diff] [blame] | 337 | static efi_status_t efi_check_allocated(u64 addr, bool must_be_allocated) |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 338 | { |
| 339 | struct efi_mem_list *item; |
| 340 | |
| 341 | if (!addr) |
| 342 | return EFI_INVALID_PARAMETER; |
| 343 | list_for_each_entry(item, &efi_mem, link) { |
| 344 | u64 start = item->desc.physical_start; |
| 345 | u64 end = start + (item->desc.num_pages << EFI_PAGE_SHIFT); |
| 346 | |
| 347 | if (addr >= start && addr < end) { |
Heinrich Schuchardt | 906f685 | 2019-05-11 08:21:17 +0200 | [diff] [blame] | 348 | if (must_be_allocated ^ |
| 349 | (item->desc.type == EFI_CONVENTIONAL_MEMORY)) |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 350 | return EFI_SUCCESS; |
| 351 | else |
| 352 | return EFI_NOT_FOUND; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | return EFI_NOT_FOUND; |
| 357 | } |
| 358 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 359 | static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr) |
| 360 | { |
| 361 | struct list_head *lhandle; |
| 362 | |
Alexander Graf | c3b2a25 | 2018-11-05 00:30:46 +0100 | [diff] [blame] | 363 | /* |
| 364 | * Prealign input max address, so we simplify our matching |
| 365 | * logic below and can just reuse it as return pointer. |
| 366 | */ |
| 367 | max_addr &= ~EFI_PAGE_MASK; |
| 368 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 369 | list_for_each(lhandle, &efi_mem) { |
| 370 | struct efi_mem_list *lmem = list_entry(lhandle, |
| 371 | struct efi_mem_list, link); |
| 372 | struct efi_mem_desc *desc = &lmem->desc; |
| 373 | uint64_t desc_len = desc->num_pages << EFI_PAGE_SHIFT; |
| 374 | uint64_t desc_end = desc->physical_start + desc_len; |
| 375 | uint64_t curmax = min(max_addr, desc_end); |
| 376 | uint64_t ret = curmax - len; |
| 377 | |
| 378 | /* We only take memory from free RAM */ |
| 379 | if (desc->type != EFI_CONVENTIONAL_MEMORY) |
| 380 | continue; |
| 381 | |
| 382 | /* Out of bounds for max_addr */ |
| 383 | if ((ret + len) > max_addr) |
| 384 | continue; |
| 385 | |
| 386 | /* Out of bounds for upper map limit */ |
| 387 | if ((ret + len) > desc_end) |
| 388 | continue; |
| 389 | |
| 390 | /* Out of bounds for lower map limit */ |
| 391 | if (ret < desc->physical_start) |
| 392 | continue; |
| 393 | |
| 394 | /* Return the highest address in this map within bounds */ |
| 395 | return ret; |
| 396 | } |
| 397 | |
| 398 | return 0; |
| 399 | } |
| 400 | |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 401 | /* |
| 402 | * Allocate memory pages. |
| 403 | * |
| 404 | * @type type of allocation to be performed |
| 405 | * @memory_type usage type of the allocated memory |
| 406 | * @pages number of pages to be allocated |
| 407 | * @memory allocated memory |
| 408 | * @return status code |
| 409 | */ |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 410 | efi_status_t efi_allocate_pages(int type, int memory_type, |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 411 | efi_uintn_t pages, uint64_t *memory) |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 412 | { |
| 413 | u64 len = pages << EFI_PAGE_SHIFT; |
Heinrich Schuchardt | fbdb93c | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 414 | efi_status_t ret; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 415 | uint64_t addr; |
| 416 | |
Heinrich Schuchardt | f727233 | 2019-04-23 00:30:53 +0200 | [diff] [blame] | 417 | /* Check import parameters */ |
| 418 | if (memory_type >= EFI_PERSISTENT_MEMORY_TYPE && |
| 419 | memory_type <= 0x6FFFFFFF) |
| 420 | return EFI_INVALID_PARAMETER; |
Heinrich Schuchardt | a137c96 | 2018-07-02 12:53:53 +0200 | [diff] [blame] | 421 | if (!memory) |
| 422 | return EFI_INVALID_PARAMETER; |
| 423 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 424 | switch (type) { |
Heinrich Schuchardt | 3b7b3b6 | 2018-01-30 21:08:00 +0100 | [diff] [blame] | 425 | case EFI_ALLOCATE_ANY_PAGES: |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 426 | /* Any page */ |
Stephen Warren | 66208b9 | 2018-08-30 15:43:45 -0600 | [diff] [blame] | 427 | addr = efi_find_free_memory(len, -1ULL); |
Heinrich Schuchardt | fbdb93c | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 428 | if (!addr) |
| 429 | return EFI_OUT_OF_RESOURCES; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 430 | break; |
Heinrich Schuchardt | 3b7b3b6 | 2018-01-30 21:08:00 +0100 | [diff] [blame] | 431 | case EFI_ALLOCATE_MAX_ADDRESS: |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 432 | /* Max address */ |
| 433 | addr = efi_find_free_memory(len, *memory); |
Heinrich Schuchardt | fbdb93c | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 434 | if (!addr) |
| 435 | return EFI_OUT_OF_RESOURCES; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 436 | break; |
Heinrich Schuchardt | 3b7b3b6 | 2018-01-30 21:08:00 +0100 | [diff] [blame] | 437 | case EFI_ALLOCATE_ADDRESS: |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 438 | /* Exact address, reserve it. The addr is already in *memory. */ |
Heinrich Schuchardt | fbdb93c | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 439 | ret = efi_check_allocated(*memory, false); |
| 440 | if (ret != EFI_SUCCESS) |
| 441 | return EFI_NOT_FOUND; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 442 | addr = *memory; |
| 443 | break; |
| 444 | default: |
| 445 | /* UEFI doesn't specify other allocation types */ |
Heinrich Schuchardt | fbdb93c | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 446 | return EFI_INVALID_PARAMETER; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 447 | } |
| 448 | |
Heinrich Schuchardt | fbdb93c | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 449 | /* Reserve that map in our memory maps */ |
| 450 | if (efi_add_memory_map(addr, pages, memory_type, true) != addr) |
| 451 | /* Map would overlap, bail out */ |
| 452 | return EFI_OUT_OF_RESOURCES; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 453 | |
Heinrich Schuchardt | fbdb93c | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 454 | *memory = addr; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 455 | |
Heinrich Schuchardt | fbdb93c | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 456 | return EFI_SUCCESS; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | void *efi_alloc(uint64_t len, int memory_type) |
| 460 | { |
| 461 | uint64_t ret = 0; |
Heinrich Schuchardt | dcdca29 | 2018-11-18 17:58:49 +0100 | [diff] [blame] | 462 | uint64_t pages = efi_size_in_pages(len); |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 463 | efi_status_t r; |
| 464 | |
Heinrich Schuchardt | 0f7186b | 2018-05-26 10:32:27 +0200 | [diff] [blame] | 465 | r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type, pages, |
| 466 | &ret); |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 467 | if (r == EFI_SUCCESS) |
| 468 | return (void*)(uintptr_t)ret; |
| 469 | |
| 470 | return NULL; |
| 471 | } |
| 472 | |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 473 | /** |
| 474 | * efi_free_pages() - free memory pages |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 475 | * |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 476 | * @memory: start of the memory area to be freed |
| 477 | * @pages: number of pages to be freed |
| 478 | * Return: status code |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 479 | */ |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 480 | 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] | 481 | { |
Stefan Brüns | a4f4302 | 2016-10-01 23:32:27 +0200 | [diff] [blame] | 482 | uint64_t r = 0; |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 483 | efi_status_t ret; |
| 484 | |
Heinrich Schuchardt | 906f685 | 2019-05-11 08:21:17 +0200 | [diff] [blame] | 485 | ret = efi_check_allocated(memory, true); |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 486 | if (ret != EFI_SUCCESS) |
| 487 | return ret; |
Stefan Brüns | a4f4302 | 2016-10-01 23:32:27 +0200 | [diff] [blame] | 488 | |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 489 | /* Sanity check */ |
Heinrich Schuchardt | 7bf4bc9 | 2019-04-25 18:41:40 +0200 | [diff] [blame] | 490 | if (!memory || (memory & EFI_PAGE_MASK) || !pages) { |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 491 | printf("%s: illegal free 0x%llx, 0x%zx\n", __func__, |
| 492 | memory, pages); |
| 493 | return EFI_INVALID_PARAMETER; |
| 494 | } |
| 495 | |
Heinrich Schuchardt | 6738fcd | 2018-11-18 17:58:46 +0100 | [diff] [blame] | 496 | r = efi_add_memory_map(memory, pages, EFI_CONVENTIONAL_MEMORY, false); |
Stefan Brüns | a4f4302 | 2016-10-01 23:32:27 +0200 | [diff] [blame] | 497 | /* Merging of adjacent free regions is missing */ |
| 498 | |
Heinrich Schuchardt | 6738fcd | 2018-11-18 17:58:46 +0100 | [diff] [blame] | 499 | if (r == memory) |
Stefan Brüns | a4f4302 | 2016-10-01 23:32:27 +0200 | [diff] [blame] | 500 | return EFI_SUCCESS; |
| 501 | |
| 502 | return EFI_NOT_FOUND; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 503 | } |
| 504 | |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 505 | /** |
| 506 | * efi_allocate_pool - allocate memory from pool |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 507 | * |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 508 | * @pool_type: type of the pool from which memory is to be allocated |
| 509 | * @size: number of bytes to be allocated |
| 510 | * @buffer: allocated memory |
| 511 | * Return: status code |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 512 | */ |
| 513 | 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] | 514 | { |
| 515 | efi_status_t r; |
Heinrich Schuchardt | 657e979 | 2019-03-18 20:01:59 +0100 | [diff] [blame] | 516 | u64 addr; |
Alexander Graf | cd40517 | 2018-06-18 17:23:15 +0200 | [diff] [blame] | 517 | struct efi_pool_allocation *alloc; |
Heinrich Schuchardt | dcdca29 | 2018-11-18 17:58:49 +0100 | [diff] [blame] | 518 | u64 num_pages = efi_size_in_pages(size + |
| 519 | sizeof(struct efi_pool_allocation)); |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 520 | |
Heinrich Schuchardt | a137c96 | 2018-07-02 12:53:53 +0200 | [diff] [blame] | 521 | if (!buffer) |
| 522 | return EFI_INVALID_PARAMETER; |
| 523 | |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 524 | if (size == 0) { |
| 525 | *buffer = NULL; |
| 526 | return EFI_SUCCESS; |
| 527 | } |
Stefan Brüns | 5a09aef | 2016-10-09 22:17:18 +0200 | [diff] [blame] | 528 | |
Heinrich Schuchardt | 0f7186b | 2018-05-26 10:32:27 +0200 | [diff] [blame] | 529 | r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, pool_type, num_pages, |
Heinrich Schuchardt | 657e979 | 2019-03-18 20:01:59 +0100 | [diff] [blame] | 530 | &addr); |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 531 | if (r == EFI_SUCCESS) { |
Heinrich Schuchardt | 657e979 | 2019-03-18 20:01:59 +0100 | [diff] [blame] | 532 | alloc = (struct efi_pool_allocation *)(uintptr_t)addr; |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 533 | alloc->num_pages = num_pages; |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 534 | alloc->checksum = checksum(alloc); |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 535 | *buffer = alloc->data; |
| 536 | } |
| 537 | |
| 538 | return r; |
| 539 | } |
| 540 | |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 541 | /** |
| 542 | * efi_free_pool() - free memory from pool |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 543 | * |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 544 | * @buffer: start of memory to be freed |
| 545 | * Return: status code |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 546 | */ |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 547 | efi_status_t efi_free_pool(void *buffer) |
| 548 | { |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 549 | efi_status_t ret; |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 550 | struct efi_pool_allocation *alloc; |
| 551 | |
Heinrich Schuchardt | 906f685 | 2019-05-11 08:21:17 +0200 | [diff] [blame] | 552 | ret = efi_check_allocated((uintptr_t)buffer, true); |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 553 | if (ret != EFI_SUCCESS) |
| 554 | return ret; |
xypron.glpk@gmx.de | 34ddae7 | 2017-07-14 19:12:39 +0200 | [diff] [blame] | 555 | |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 556 | alloc = container_of(buffer, struct efi_pool_allocation, data); |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 557 | |
| 558 | /* Check that this memory was allocated by efi_allocate_pool() */ |
| 559 | if (((uintptr_t)alloc & EFI_PAGE_MASK) || |
| 560 | alloc->checksum != checksum(alloc)) { |
| 561 | printf("%s: illegal free 0x%p\n", __func__, buffer); |
| 562 | return EFI_INVALID_PARAMETER; |
| 563 | } |
| 564 | /* Avoid double free */ |
| 565 | alloc->checksum = 0; |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 566 | |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 567 | ret = efi_free_pages((uintptr_t)alloc, alloc->num_pages); |
Stefan Brüns | 5a09aef | 2016-10-09 22:17:18 +0200 | [diff] [blame] | 568 | |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 569 | return ret; |
Stefan Brüns | 5a09aef | 2016-10-09 22:17:18 +0200 | [diff] [blame] | 570 | } |
| 571 | |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 572 | /* |
| 573 | * Get map describing memory usage. |
| 574 | * |
| 575 | * @memory_map_size on entry the size, in bytes, of the memory map buffer, |
| 576 | * on exit the size of the copied memory map |
| 577 | * @memory_map buffer to which the memory map is written |
| 578 | * @map_key key for the memory map |
| 579 | * @descriptor_size size of an individual memory descriptor |
| 580 | * @descriptor_version version number of the memory descriptor structure |
| 581 | * @return status code |
| 582 | */ |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 583 | efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size, |
| 584 | struct efi_mem_desc *memory_map, |
| 585 | efi_uintn_t *map_key, |
| 586 | efi_uintn_t *descriptor_size, |
| 587 | uint32_t *descriptor_version) |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 588 | { |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 589 | efi_uintn_t map_size = 0; |
Alexander Graf | 9b59102 | 2016-04-11 23:51:02 +0200 | [diff] [blame] | 590 | int map_entries = 0; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 591 | struct list_head *lhandle; |
Heinrich Schuchardt | 9b16646 | 2018-08-06 22:28:18 +0200 | [diff] [blame] | 592 | efi_uintn_t provided_map_size; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 593 | |
Heinrich Schuchardt | e1a1a09 | 2018-07-02 12:53:54 +0200 | [diff] [blame] | 594 | if (!memory_map_size) |
| 595 | return EFI_INVALID_PARAMETER; |
| 596 | |
Heinrich Schuchardt | 9b16646 | 2018-08-06 22:28:18 +0200 | [diff] [blame] | 597 | provided_map_size = *memory_map_size; |
| 598 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 599 | list_for_each(lhandle, &efi_mem) |
Alexander Graf | 9b59102 | 2016-04-11 23:51:02 +0200 | [diff] [blame] | 600 | map_entries++; |
| 601 | |
| 602 | map_size = map_entries * sizeof(struct efi_mem_desc); |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 603 | |
Rob Clark | a84c974 | 2017-07-26 14:34:05 -0400 | [diff] [blame] | 604 | *memory_map_size = map_size; |
| 605 | |
xypron.glpk@gmx.de | 5487ab5 | 2017-07-21 19:04:33 +0200 | [diff] [blame] | 606 | if (provided_map_size < map_size) |
| 607 | return EFI_BUFFER_TOO_SMALL; |
| 608 | |
Heinrich Schuchardt | e1a1a09 | 2018-07-02 12:53:54 +0200 | [diff] [blame] | 609 | if (!memory_map) |
| 610 | return EFI_INVALID_PARAMETER; |
| 611 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 612 | if (descriptor_size) |
| 613 | *descriptor_size = sizeof(struct efi_mem_desc); |
| 614 | |
Mian Yousaf Kaukab | 338cf56 | 2016-09-05 23:59:22 +0200 | [diff] [blame] | 615 | if (descriptor_version) |
| 616 | *descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION; |
| 617 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 618 | /* Copy list into array */ |
Heinrich Schuchardt | e1a1a09 | 2018-07-02 12:53:54 +0200 | [diff] [blame] | 619 | /* Return the list in ascending order */ |
| 620 | memory_map = &memory_map[map_entries - 1]; |
| 621 | list_for_each(lhandle, &efi_mem) { |
| 622 | struct efi_mem_list *lmem; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 623 | |
Heinrich Schuchardt | e1a1a09 | 2018-07-02 12:53:54 +0200 | [diff] [blame] | 624 | lmem = list_entry(lhandle, struct efi_mem_list, link); |
| 625 | *memory_map = lmem->desc; |
| 626 | memory_map--; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 627 | } |
| 628 | |
Heinrich Schuchardt | e1a1a09 | 2018-07-02 12:53:54 +0200 | [diff] [blame] | 629 | if (map_key) |
Heinrich Schuchardt | 0f233c4 | 2018-07-02 12:53:55 +0200 | [diff] [blame] | 630 | *map_key = efi_memory_map_key; |
xypron.glpk@gmx.de | e436e63 | 2017-07-21 19:05:44 +0200 | [diff] [blame] | 631 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 632 | return EFI_SUCCESS; |
| 633 | } |
| 634 | |
York Sun | a8e9f0a | 2017-03-06 09:02:27 -0800 | [diff] [blame] | 635 | __weak void efi_add_known_memory(void) |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 636 | { |
Alexander Graf | c214edf | 2018-11-30 21:24:56 +0100 | [diff] [blame] | 637 | u64 ram_top = board_get_usable_ram_top(0) & ~EFI_PAGE_MASK; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 638 | int i; |
| 639 | |
Heinrich Schuchardt | eb8edcf | 2019-01-05 23:41:36 +0100 | [diff] [blame] | 640 | /* |
| 641 | * ram_top is just outside mapped memory. So use an offset of one for |
| 642 | * mapping the sandbox address. |
| 643 | */ |
| 644 | ram_top = (uintptr_t)map_sysmem(ram_top - 1, 0) + 1; |
| 645 | |
Alexander Graf | c214edf | 2018-11-30 21:24:56 +0100 | [diff] [blame] | 646 | /* Fix for 32bit targets with ram_top at 4G */ |
| 647 | if (!ram_top) |
| 648 | ram_top = 0x100000000ULL; |
| 649 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 650 | /* Add RAM */ |
| 651 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { |
Heinrich Schuchardt | 084091a | 2018-11-12 18:55:24 +0100 | [diff] [blame] | 652 | u64 ram_end, ram_start, pages; |
| 653 | |
Heinrich Schuchardt | 6738fcd | 2018-11-18 17:58:46 +0100 | [diff] [blame] | 654 | ram_start = (uintptr_t)map_sysmem(gd->bd->bi_dram[i].start, 0); |
Heinrich Schuchardt | 084091a | 2018-11-12 18:55:24 +0100 | [diff] [blame] | 655 | ram_end = ram_start + gd->bd->bi_dram[i].size; |
| 656 | |
| 657 | /* Remove partial pages */ |
| 658 | ram_end &= ~EFI_PAGE_MASK; |
| 659 | ram_start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK; |
| 660 | |
Alexander Graf | c214edf | 2018-11-30 21:24:56 +0100 | [diff] [blame] | 661 | if (ram_end <= ram_start) { |
| 662 | /* Invalid mapping, keep going. */ |
| 663 | continue; |
| 664 | } |
| 665 | |
| 666 | pages = (ram_end - ram_start) >> EFI_PAGE_SHIFT; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 667 | |
Alexander Graf | c214edf | 2018-11-30 21:24:56 +0100 | [diff] [blame] | 668 | efi_add_memory_map(ram_start, pages, |
| 669 | EFI_CONVENTIONAL_MEMORY, false); |
| 670 | |
| 671 | /* |
| 672 | * Boards may indicate to the U-Boot memory core that they |
| 673 | * can not support memory above ram_top. Let's honor this |
| 674 | * in the efi_loader subsystem too by declaring any memory |
| 675 | * above ram_top as "already occupied by firmware". |
| 676 | */ |
| 677 | if (ram_top < ram_start) { |
| 678 | /* ram_top is before this region, reserve all */ |
Heinrich Schuchardt | 084091a | 2018-11-12 18:55:24 +0100 | [diff] [blame] | 679 | efi_add_memory_map(ram_start, pages, |
Alexander Graf | c214edf | 2018-11-30 21:24:56 +0100 | [diff] [blame] | 680 | EFI_BOOT_SERVICES_DATA, true); |
| 681 | } else if ((ram_top >= ram_start) && (ram_top < ram_end)) { |
| 682 | /* ram_top is inside this region, reserve parts */ |
| 683 | pages = (ram_end - ram_top) >> EFI_PAGE_SHIFT; |
| 684 | |
| 685 | efi_add_memory_map(ram_top, pages, |
| 686 | EFI_BOOT_SERVICES_DATA, true); |
Heinrich Schuchardt | 084091a | 2018-11-12 18:55:24 +0100 | [diff] [blame] | 687 | } |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 688 | } |
York Sun | a8e9f0a | 2017-03-06 09:02:27 -0800 | [diff] [blame] | 689 | } |
| 690 | |
Simon Glass | 7863943 | 2018-06-18 17:23:12 +0200 | [diff] [blame] | 691 | /* Add memory regions for U-Boot's memory and for the runtime services code */ |
| 692 | static void add_u_boot_and_runtime(void) |
York Sun | a8e9f0a | 2017-03-06 09:02:27 -0800 | [diff] [blame] | 693 | { |
| 694 | unsigned long runtime_start, runtime_end, runtime_pages; |
Alexander Graf | 15937a2 | 2018-09-17 13:54:33 +0200 | [diff] [blame] | 695 | unsigned long runtime_mask = EFI_PAGE_MASK; |
York Sun | a8e9f0a | 2017-03-06 09:02:27 -0800 | [diff] [blame] | 696 | unsigned long uboot_start, uboot_pages; |
| 697 | unsigned long uboot_stack_size = 16 * 1024 * 1024; |
| 698 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 699 | /* Add U-Boot */ |
| 700 | uboot_start = (gd->start_addr_sp - uboot_stack_size) & ~EFI_PAGE_MASK; |
| 701 | uboot_pages = (gd->ram_top - uboot_start) >> EFI_PAGE_SHIFT; |
| 702 | efi_add_memory_map(uboot_start, uboot_pages, EFI_LOADER_DATA, false); |
| 703 | |
Alexander Graf | 15937a2 | 2018-09-17 13:54:33 +0200 | [diff] [blame] | 704 | #if defined(__aarch64__) |
| 705 | /* |
| 706 | * Runtime Services must be 64KiB aligned according to the |
| 707 | * "AArch64 Platforms" section in the UEFI spec (2.7+). |
| 708 | */ |
| 709 | |
| 710 | runtime_mask = SZ_64K - 1; |
| 711 | #endif |
| 712 | |
| 713 | /* |
| 714 | * Add Runtime Services. We mark surrounding boottime code as runtime as |
| 715 | * well to fulfill the runtime alignment constraints but avoid padding. |
| 716 | */ |
| 717 | runtime_start = (ulong)&__efi_runtime_start & ~runtime_mask; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 718 | runtime_end = (ulong)&__efi_runtime_stop; |
Alexander Graf | 15937a2 | 2018-09-17 13:54:33 +0200 | [diff] [blame] | 719 | runtime_end = (runtime_end + runtime_mask) & ~runtime_mask; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 720 | runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT; |
| 721 | efi_add_memory_map(runtime_start, runtime_pages, |
| 722 | EFI_RUNTIME_SERVICES_CODE, false); |
Simon Glass | 7863943 | 2018-06-18 17:23:12 +0200 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | int efi_memory_init(void) |
| 726 | { |
| 727 | efi_add_known_memory(); |
| 728 | |
| 729 | if (!IS_ENABLED(CONFIG_SANDBOX)) |
| 730 | add_u_boot_and_runtime(); |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 731 | |
Alexander Graf | 7c00a3c | 2016-05-11 18:25:48 +0200 | [diff] [blame] | 732 | #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER |
| 733 | /* Request a 32bit 64MB bounce buffer region */ |
| 734 | uint64_t efi_bounce_buffer_addr = 0xffffffff; |
| 735 | |
Heinrich Schuchardt | 0f7186b | 2018-05-26 10:32:27 +0200 | [diff] [blame] | 736 | if (efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, EFI_LOADER_DATA, |
Alexander Graf | 7c00a3c | 2016-05-11 18:25:48 +0200 | [diff] [blame] | 737 | (64 * 1024 * 1024) >> EFI_PAGE_SHIFT, |
| 738 | &efi_bounce_buffer_addr) != EFI_SUCCESS) |
| 739 | return -1; |
| 740 | |
| 741 | efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr; |
| 742 | #endif |
| 743 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 744 | return 0; |
| 745 | } |