Tom Rini | f739fcd | 2018-05-07 17:02:21 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 2 | /* |
| 3 | * EFI application memory management |
| 4 | * |
| 5 | * Copyright (c) 2016 Alexander Graf |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 8 | #include <common.h> |
| 9 | #include <efi_loader.h> |
Simon Glass | 67c4e9f | 2019-11-14 12:57:45 -0700 | [diff] [blame] | 10 | #include <init.h> |
Simon Glass | bdecaeb | 2018-03-08 21:53:27 +0100 | [diff] [blame] | 11 | #include <malloc.h> |
Alexander Graf | 282a06c | 2018-06-18 17:23:15 +0200 | [diff] [blame] | 12 | #include <mapmem.h> |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 13 | #include <watchdog.h> |
Simon Glass | 90526e9 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 14 | #include <asm/cache.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 15 | #include <asm/global_data.h> |
Simon Glass | bdecaeb | 2018-03-08 21:53:27 +0100 | [diff] [blame] | 16 | #include <linux/list_sort.h> |
Alexander Graf | 7a82c30 | 2018-09-17 13:54:33 +0200 | [diff] [blame] | 17 | #include <linux/sizes.h> |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 18 | |
| 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
Heinrich Schuchardt | 2c3ec28 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 21 | /* Magic number identifying memory allocated from pool */ |
| 22 | #define EFI_ALLOC_POOL_MAGIC 0x1fe67ddf6491caa2 |
| 23 | |
Heinrich Schuchardt | 1fcb7ea | 2018-07-02 12:53:55 +0200 | [diff] [blame] | 24 | efi_uintn_t efi_memory_map_key; |
| 25 | |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 26 | struct efi_mem_list { |
| 27 | struct list_head link; |
| 28 | struct efi_mem_desc desc; |
| 29 | }; |
| 30 | |
Alexander Graf | 74c16ac | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 31 | #define EFI_CARVE_NO_OVERLAP -1 |
| 32 | #define EFI_CARVE_LOOP_AGAIN -2 |
| 33 | #define EFI_CARVE_OVERLAPS_NONRAM -3 |
| 34 | |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 35 | /* This list contains all memory map items */ |
| 36 | LIST_HEAD(efi_mem); |
| 37 | |
Alexander Graf | 51735ae | 2016-05-11 18:25:48 +0200 | [diff] [blame] | 38 | #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER |
| 39 | void *efi_bounce_buffer; |
| 40 | #endif |
| 41 | |
Heinrich Schuchardt | 2c3ec28 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 42 | /** |
Heinrich Schuchardt | 4be077b | 2019-07-14 12:28:56 +0200 | [diff] [blame] | 43 | * struct efi_pool_allocation - memory block allocated from pool |
Heinrich Schuchardt | 2c3ec28 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 44 | * |
| 45 | * @num_pages: number of pages allocated |
| 46 | * @checksum: checksum |
Heinrich Schuchardt | 4be077b | 2019-07-14 12:28:56 +0200 | [diff] [blame] | 47 | * @data: allocated pool memory |
Heinrich Schuchardt | 2c3ec28 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 48 | * |
Heinrich Schuchardt | 4be077b | 2019-07-14 12:28:56 +0200 | [diff] [blame] | 49 | * U-Boot services each UEFI AllocatePool() request as a separate |
| 50 | * (multiple) page allocation. We have to track the number of pages |
Stefan Brüns | 42417bc | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 51 | * to be able to free the correct amount later. |
Heinrich Schuchardt | 4be077b | 2019-07-14 12:28:56 +0200 | [diff] [blame] | 52 | * |
| 53 | * The checksum calculated in function checksum() is used in FreePool() to avoid |
| 54 | * freeing memory not allocated by AllocatePool() and duplicate freeing. |
| 55 | * |
Stefan Brüns | 42417bc | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 56 | * EFI requires 8 byte alignment for pool allocations, so we can |
Heinrich Schuchardt | 4be077b | 2019-07-14 12:28:56 +0200 | [diff] [blame] | 57 | * prepend each allocation with these header fields. |
Stefan Brüns | 42417bc | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 58 | */ |
| 59 | struct efi_pool_allocation { |
| 60 | u64 num_pages; |
Heinrich Schuchardt | 2c3ec28 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 61 | u64 checksum; |
Rob Clark | 946160f | 2017-09-13 18:05:36 -0400 | [diff] [blame] | 62 | char data[] __aligned(ARCH_DMA_MINALIGN); |
Stefan Brüns | 42417bc | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 63 | }; |
| 64 | |
Heinrich Schuchardt | 2c3ec28 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 65 | /** |
| 66 | * checksum() - calculate checksum for memory allocated from pool |
| 67 | * |
| 68 | * @alloc: allocation header |
| 69 | * Return: checksum, always non-zero |
| 70 | */ |
| 71 | static u64 checksum(struct efi_pool_allocation *alloc) |
| 72 | { |
| 73 | u64 addr = (uintptr_t)alloc; |
| 74 | u64 ret = (addr >> 32) ^ (addr << 32) ^ alloc->num_pages ^ |
| 75 | EFI_ALLOC_POOL_MAGIC; |
| 76 | if (!ret) |
| 77 | ++ret; |
| 78 | return ret; |
| 79 | } |
| 80 | |
Stefan Brüns | 42417bc | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 81 | /* |
Alexander Graf | 38ce65e | 2016-03-30 16:38:29 +0200 | [diff] [blame] | 82 | * Sorts the memory list from highest address to lowest address |
| 83 | * |
| 84 | * When allocating memory we should always start from the highest |
| 85 | * address chunk, so sort the memory list such that the first list |
| 86 | * iterator gets the highest address and goes lower from there. |
| 87 | */ |
| 88 | static int efi_mem_cmp(void *priv, struct list_head *a, struct list_head *b) |
| 89 | { |
| 90 | struct efi_mem_list *mema = list_entry(a, struct efi_mem_list, link); |
| 91 | struct efi_mem_list *memb = list_entry(b, struct efi_mem_list, link); |
| 92 | |
| 93 | if (mema->desc.physical_start == memb->desc.physical_start) |
| 94 | return 0; |
| 95 | else if (mema->desc.physical_start < memb->desc.physical_start) |
| 96 | return 1; |
| 97 | else |
| 98 | return -1; |
| 99 | } |
| 100 | |
Alexander Graf | 7b05667 | 2018-09-16 17:05:29 +0200 | [diff] [blame] | 101 | static uint64_t desc_get_end(struct efi_mem_desc *desc) |
| 102 | { |
| 103 | return desc->physical_start + (desc->num_pages << EFI_PAGE_SHIFT); |
| 104 | } |
| 105 | |
Alexander Graf | 38ce65e | 2016-03-30 16:38:29 +0200 | [diff] [blame] | 106 | static void efi_mem_sort(void) |
| 107 | { |
Alexander Graf | 7b05667 | 2018-09-16 17:05:29 +0200 | [diff] [blame] | 108 | struct list_head *lhandle; |
| 109 | struct efi_mem_list *prevmem = NULL; |
| 110 | bool merge_again = true; |
| 111 | |
Alexander Graf | 38ce65e | 2016-03-30 16:38:29 +0200 | [diff] [blame] | 112 | list_sort(NULL, &efi_mem, efi_mem_cmp); |
Alexander Graf | 7b05667 | 2018-09-16 17:05:29 +0200 | [diff] [blame] | 113 | |
| 114 | /* Now merge entries that can be merged */ |
| 115 | while (merge_again) { |
| 116 | merge_again = false; |
| 117 | list_for_each(lhandle, &efi_mem) { |
| 118 | struct efi_mem_list *lmem; |
| 119 | struct efi_mem_desc *prev = &prevmem->desc; |
| 120 | struct efi_mem_desc *cur; |
| 121 | uint64_t pages; |
| 122 | |
| 123 | lmem = list_entry(lhandle, struct efi_mem_list, link); |
| 124 | if (!prevmem) { |
| 125 | prevmem = lmem; |
| 126 | continue; |
| 127 | } |
| 128 | |
| 129 | cur = &lmem->desc; |
| 130 | |
| 131 | if ((desc_get_end(cur) == prev->physical_start) && |
| 132 | (prev->type == cur->type) && |
| 133 | (prev->attribute == cur->attribute)) { |
| 134 | /* There is an existing map before, reuse it */ |
| 135 | pages = cur->num_pages; |
| 136 | prev->num_pages += pages; |
| 137 | prev->physical_start -= pages << EFI_PAGE_SHIFT; |
| 138 | prev->virtual_start -= pages << EFI_PAGE_SHIFT; |
| 139 | list_del(&lmem->link); |
| 140 | free(lmem); |
| 141 | |
| 142 | merge_again = true; |
| 143 | break; |
| 144 | } |
| 145 | |
| 146 | prevmem = lmem; |
| 147 | } |
| 148 | } |
Alexander Graf | 38ce65e | 2016-03-30 16:38:29 +0200 | [diff] [blame] | 149 | } |
| 150 | |
Heinrich Schuchardt | 3282614 | 2018-05-27 16:45:09 +0200 | [diff] [blame] | 151 | /** efi_mem_carve_out - unmap memory region |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 152 | * |
Heinrich Schuchardt | 3282614 | 2018-05-27 16:45:09 +0200 | [diff] [blame] | 153 | * @map: memory map |
| 154 | * @carve_desc: memory region to unmap |
| 155 | * @overlap_only_ram: the carved out region may only overlap RAM |
| 156 | * Return Value: the number of overlapping pages which have been |
| 157 | * removed from the map, |
| 158 | * EFI_CARVE_NO_OVERLAP, if the regions don't overlap, |
| 159 | * EFI_CARVE_OVERLAPS_NONRAM, if the carve and map overlap, |
| 160 | * and the map contains anything but free ram |
| 161 | * (only when overlap_only_ram is true), |
| 162 | * EFI_CARVE_LOOP_AGAIN, if the mapping list should be |
| 163 | * traversed again, as it has been altered. |
| 164 | * |
| 165 | * Unmaps all memory occupied by the carve_desc region from the list entry |
| 166 | * pointed to by map. |
Stefan Brüns | 852efbf | 2016-10-01 23:32:23 +0200 | [diff] [blame] | 167 | * |
| 168 | * In case of EFI_CARVE_OVERLAPS_NONRAM it is the callers responsibility |
Heinrich Schuchardt | 3282614 | 2018-05-27 16:45:09 +0200 | [diff] [blame] | 169 | * to re-add the already carved out pages to the mapping. |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 170 | */ |
Heinrich Schuchardt | 3282614 | 2018-05-27 16:45:09 +0200 | [diff] [blame] | 171 | static s64 efi_mem_carve_out(struct efi_mem_list *map, |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 172 | struct efi_mem_desc *carve_desc, |
| 173 | bool overlap_only_ram) |
| 174 | { |
| 175 | struct efi_mem_list *newmap; |
| 176 | struct efi_mem_desc *map_desc = &map->desc; |
| 177 | uint64_t map_start = map_desc->physical_start; |
| 178 | uint64_t map_end = map_start + (map_desc->num_pages << EFI_PAGE_SHIFT); |
| 179 | uint64_t carve_start = carve_desc->physical_start; |
| 180 | uint64_t carve_end = carve_start + |
| 181 | (carve_desc->num_pages << EFI_PAGE_SHIFT); |
| 182 | |
| 183 | /* check whether we're overlapping */ |
| 184 | if ((carve_end <= map_start) || (carve_start >= map_end)) |
Alexander Graf | 74c16ac | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 185 | return EFI_CARVE_NO_OVERLAP; |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 186 | |
| 187 | /* We're overlapping with non-RAM, warn the caller if desired */ |
| 188 | if (overlap_only_ram && (map_desc->type != EFI_CONVENTIONAL_MEMORY)) |
Alexander Graf | 74c16ac | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 189 | return EFI_CARVE_OVERLAPS_NONRAM; |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 190 | |
| 191 | /* Sanitize carve_start and carve_end to lie within our bounds */ |
| 192 | carve_start = max(carve_start, map_start); |
| 193 | carve_end = min(carve_end, map_end); |
| 194 | |
| 195 | /* Carving at the beginning of our map? Just move it! */ |
| 196 | if (carve_start == map_start) { |
| 197 | if (map_end == carve_end) { |
| 198 | /* Full overlap, just remove map */ |
| 199 | list_del(&map->link); |
Stefan Brüns | 511d0b9 | 2016-10-01 23:32:29 +0200 | [diff] [blame] | 200 | free(map); |
| 201 | } else { |
| 202 | map->desc.physical_start = carve_end; |
Heinrich Schuchardt | 9631fa0 | 2019-04-11 20:08:54 +0200 | [diff] [blame] | 203 | map->desc.virtual_start = carve_end; |
Stefan Brüns | 511d0b9 | 2016-10-01 23:32:29 +0200 | [diff] [blame] | 204 | map->desc.num_pages = (map_end - carve_end) |
| 205 | >> EFI_PAGE_SHIFT; |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 206 | } |
| 207 | |
Alexander Graf | 74c16ac | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 208 | return (carve_end - carve_start) >> EFI_PAGE_SHIFT; |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | /* |
| 212 | * Overlapping maps, just split the list map at carve_start, |
| 213 | * it will get moved or removed in the next iteration. |
| 214 | * |
| 215 | * [ map_desc |__carve_start__| newmap ] |
| 216 | */ |
| 217 | |
| 218 | /* Create a new map from [ carve_start ... map_end ] */ |
| 219 | newmap = calloc(1, sizeof(*newmap)); |
| 220 | newmap->desc = map->desc; |
| 221 | newmap->desc.physical_start = carve_start; |
Heinrich Schuchardt | 9631fa0 | 2019-04-11 20:08:54 +0200 | [diff] [blame] | 222 | newmap->desc.virtual_start = carve_start; |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 223 | newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT; |
Stefan Brüns | b6a9517 | 2016-10-01 23:32:28 +0200 | [diff] [blame] | 224 | /* Insert before current entry (descending address order) */ |
| 225 | list_add_tail(&newmap->link, &map->link); |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 226 | |
| 227 | /* Shrink the map to [ map_start ... carve_start ] */ |
| 228 | map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT; |
| 229 | |
Alexander Graf | 74c16ac | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 230 | return EFI_CARVE_LOOP_AGAIN; |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 231 | } |
| 232 | |
Bryan O'Donoghue | b225c92 | 2019-07-15 12:00:39 +0100 | [diff] [blame] | 233 | /** |
Michael Walle | 714497e | 2020-05-17 12:29:19 +0200 | [diff] [blame] | 234 | * efi_add_memory_map_pg() - add pages to the memory map |
Bryan O'Donoghue | b225c92 | 2019-07-15 12:00:39 +0100 | [diff] [blame] | 235 | * |
| 236 | * @start: start address, must be a multiple of EFI_PAGE_SIZE |
| 237 | * @pages: number of pages to add |
| 238 | * @memory_type: type of memory added |
Maxim Uvarov | ffbeafe | 2020-08-28 22:47:41 +0300 | [diff] [blame] | 239 | * @overlap_only_ram: region may only overlap RAM |
Bryan O'Donoghue | b225c92 | 2019-07-15 12:00:39 +0100 | [diff] [blame] | 240 | * Return: status code |
| 241 | */ |
Michael Walle | 714497e | 2020-05-17 12:29:19 +0200 | [diff] [blame] | 242 | static efi_status_t efi_add_memory_map_pg(u64 start, u64 pages, |
| 243 | int memory_type, |
| 244 | bool overlap_only_ram) |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 245 | { |
| 246 | struct list_head *lhandle; |
| 247 | struct efi_mem_list *newlist; |
Alexander Graf | 74c16ac | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 248 | bool carve_again; |
| 249 | uint64_t carved_pages = 0; |
Heinrich Schuchardt | e80474a | 2019-06-04 20:55:12 +0200 | [diff] [blame] | 250 | struct efi_event *evt; |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 251 | |
Heinrich Schuchardt | e301e02 | 2019-04-04 21:50:02 +0200 | [diff] [blame] | 252 | EFI_PRINT("%s: 0x%llx 0x%llx %d %s\n", __func__, |
| 253 | start, pages, memory_type, overlap_only_ram ? "yes" : "no"); |
Andreas Färber | c933ed9 | 2016-07-17 06:57:11 +0200 | [diff] [blame] | 254 | |
Heinrich Schuchardt | 1fcb7ea | 2018-07-02 12:53:55 +0200 | [diff] [blame] | 255 | if (memory_type >= EFI_MAX_MEMORY_TYPE) |
| 256 | return EFI_INVALID_PARAMETER; |
| 257 | |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 258 | if (!pages) |
Bryan O'Donoghue | b225c92 | 2019-07-15 12:00:39 +0100 | [diff] [blame] | 259 | return EFI_SUCCESS; |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 260 | |
Heinrich Schuchardt | 1fcb7ea | 2018-07-02 12:53:55 +0200 | [diff] [blame] | 261 | ++efi_memory_map_key; |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 262 | newlist = calloc(1, sizeof(*newlist)); |
| 263 | newlist->desc.type = memory_type; |
| 264 | newlist->desc.physical_start = start; |
| 265 | newlist->desc.virtual_start = start; |
| 266 | newlist->desc.num_pages = pages; |
| 267 | |
| 268 | switch (memory_type) { |
| 269 | case EFI_RUNTIME_SERVICES_CODE: |
| 270 | case EFI_RUNTIME_SERVICES_DATA: |
Eugeniu Rosca | 9b89183 | 2018-07-14 22:53:30 +0200 | [diff] [blame] | 271 | newlist->desc.attribute = EFI_MEMORY_WB | EFI_MEMORY_RUNTIME; |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 272 | break; |
| 273 | case EFI_MMAP_IO: |
Eugeniu Rosca | 9b89183 | 2018-07-14 22:53:30 +0200 | [diff] [blame] | 274 | newlist->desc.attribute = EFI_MEMORY_RUNTIME; |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 275 | break; |
| 276 | default: |
Eugeniu Rosca | 9b89183 | 2018-07-14 22:53:30 +0200 | [diff] [blame] | 277 | newlist->desc.attribute = EFI_MEMORY_WB; |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 278 | break; |
| 279 | } |
| 280 | |
| 281 | /* Add our new map */ |
| 282 | do { |
Alexander Graf | 74c16ac | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 283 | carve_again = false; |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 284 | list_for_each(lhandle, &efi_mem) { |
| 285 | struct efi_mem_list *lmem; |
Heinrich Schuchardt | 3282614 | 2018-05-27 16:45:09 +0200 | [diff] [blame] | 286 | s64 r; |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 287 | |
| 288 | lmem = list_entry(lhandle, struct efi_mem_list, link); |
| 289 | r = efi_mem_carve_out(lmem, &newlist->desc, |
| 290 | overlap_only_ram); |
Alexander Graf | 74c16ac | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 291 | switch (r) { |
| 292 | case EFI_CARVE_OVERLAPS_NONRAM: |
| 293 | /* |
| 294 | * The user requested to only have RAM overlaps, |
| 295 | * but we hit a non-RAM region. Error out. |
| 296 | */ |
Bryan O'Donoghue | b225c92 | 2019-07-15 12:00:39 +0100 | [diff] [blame] | 297 | return EFI_NO_MAPPING; |
Alexander Graf | 74c16ac | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 298 | case EFI_CARVE_NO_OVERLAP: |
| 299 | /* Just ignore this list entry */ |
| 300 | break; |
| 301 | case EFI_CARVE_LOOP_AGAIN: |
| 302 | /* |
| 303 | * We split an entry, but need to loop through |
| 304 | * the list again to actually carve it. |
| 305 | */ |
| 306 | carve_again = true; |
| 307 | break; |
| 308 | default: |
| 309 | /* We carved a number of pages */ |
| 310 | carved_pages += r; |
| 311 | carve_again = true; |
| 312 | break; |
| 313 | } |
| 314 | |
| 315 | if (carve_again) { |
| 316 | /* The list changed, we need to start over */ |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 317 | break; |
| 318 | } |
| 319 | } |
Alexander Graf | 74c16ac | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 320 | } while (carve_again); |
| 321 | |
| 322 | if (overlap_only_ram && (carved_pages != pages)) { |
| 323 | /* |
| 324 | * The payload wanted to have RAM overlaps, but we overlapped |
| 325 | * with an unallocated region. Error out. |
| 326 | */ |
Bryan O'Donoghue | b225c92 | 2019-07-15 12:00:39 +0100 | [diff] [blame] | 327 | return EFI_NO_MAPPING; |
Alexander Graf | 74c16ac | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 328 | } |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 329 | |
| 330 | /* Add our new map */ |
| 331 | list_add_tail(&newlist->link, &efi_mem); |
| 332 | |
Alexander Graf | 38ce65e | 2016-03-30 16:38:29 +0200 | [diff] [blame] | 333 | /* And make sure memory is listed in descending order */ |
| 334 | efi_mem_sort(); |
| 335 | |
Heinrich Schuchardt | e80474a | 2019-06-04 20:55:12 +0200 | [diff] [blame] | 336 | /* Notify that the memory map was changed */ |
| 337 | list_for_each_entry(evt, &efi_events, link) { |
| 338 | if (evt->group && |
| 339 | !guidcmp(evt->group, |
| 340 | &efi_guid_event_group_memory_map_change)) { |
Heinrich Schuchardt | 7eaa900 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 341 | efi_signal_event(evt); |
Heinrich Schuchardt | e80474a | 2019-06-04 20:55:12 +0200 | [diff] [blame] | 342 | break; |
| 343 | } |
| 344 | } |
| 345 | |
Bryan O'Donoghue | b225c92 | 2019-07-15 12:00:39 +0100 | [diff] [blame] | 346 | return EFI_SUCCESS; |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 347 | } |
| 348 | |
Heinrich Schuchardt | 7d3af58 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 349 | /** |
Michael Walle | 714497e | 2020-05-17 12:29:19 +0200 | [diff] [blame] | 350 | * efi_add_memory_map() - add memory area to the memory map |
| 351 | * |
| 352 | * @start: start address of the memory area |
| 353 | * @size: length in bytes of the memory area |
| 354 | * @memory_type: type of memory added |
| 355 | * |
| 356 | * Return: status code |
| 357 | * |
| 358 | * This function automatically aligns the start and size of the memory area |
| 359 | * to EFI_PAGE_SIZE. |
| 360 | */ |
| 361 | efi_status_t efi_add_memory_map(u64 start, u64 size, int memory_type) |
| 362 | { |
| 363 | u64 pages; |
| 364 | |
| 365 | pages = efi_size_in_pages(size + (start & EFI_PAGE_MASK)); |
| 366 | start &= ~EFI_PAGE_MASK; |
| 367 | |
| 368 | return efi_add_memory_map_pg(start, pages, memory_type, false); |
| 369 | } |
| 370 | |
| 371 | /** |
Heinrich Schuchardt | 7d3af58 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 372 | * efi_check_allocated() - validate address to be freed |
| 373 | * |
| 374 | * Check that the address is within allocated memory: |
| 375 | * |
Heinrich Schuchardt | 7d3af58 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 376 | * * The address must be in a range of the memory map. |
| 377 | * * The address may not point to EFI_CONVENTIONAL_MEMORY. |
| 378 | * |
| 379 | * Page alignment is not checked as this is not a requirement of |
| 380 | * efi_free_pool(). |
| 381 | * |
Heinrich Schuchardt | f756fe8 | 2019-05-11 08:21:17 +0200 | [diff] [blame] | 382 | * @addr: address of page to be freed |
| 383 | * @must_be_allocated: return success if the page is allocated |
| 384 | * Return: status code |
Heinrich Schuchardt | 7d3af58 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 385 | */ |
Heinrich Schuchardt | f756fe8 | 2019-05-11 08:21:17 +0200 | [diff] [blame] | 386 | static efi_status_t efi_check_allocated(u64 addr, bool must_be_allocated) |
Heinrich Schuchardt | 7d3af58 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 387 | { |
| 388 | struct efi_mem_list *item; |
| 389 | |
Heinrich Schuchardt | 7d3af58 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 390 | list_for_each_entry(item, &efi_mem, link) { |
| 391 | u64 start = item->desc.physical_start; |
| 392 | u64 end = start + (item->desc.num_pages << EFI_PAGE_SHIFT); |
| 393 | |
| 394 | if (addr >= start && addr < end) { |
Heinrich Schuchardt | f756fe8 | 2019-05-11 08:21:17 +0200 | [diff] [blame] | 395 | if (must_be_allocated ^ |
| 396 | (item->desc.type == EFI_CONVENTIONAL_MEMORY)) |
Heinrich Schuchardt | 7d3af58 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 397 | return EFI_SUCCESS; |
| 398 | else |
| 399 | return EFI_NOT_FOUND; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | return EFI_NOT_FOUND; |
| 404 | } |
| 405 | |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 406 | static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr) |
| 407 | { |
| 408 | struct list_head *lhandle; |
| 409 | |
Alexander Graf | c2e1ad7 | 2018-11-05 00:30:46 +0100 | [diff] [blame] | 410 | /* |
| 411 | * Prealign input max address, so we simplify our matching |
| 412 | * logic below and can just reuse it as return pointer. |
| 413 | */ |
| 414 | max_addr &= ~EFI_PAGE_MASK; |
| 415 | |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 416 | list_for_each(lhandle, &efi_mem) { |
| 417 | struct efi_mem_list *lmem = list_entry(lhandle, |
| 418 | struct efi_mem_list, link); |
| 419 | struct efi_mem_desc *desc = &lmem->desc; |
| 420 | uint64_t desc_len = desc->num_pages << EFI_PAGE_SHIFT; |
| 421 | uint64_t desc_end = desc->physical_start + desc_len; |
| 422 | uint64_t curmax = min(max_addr, desc_end); |
| 423 | uint64_t ret = curmax - len; |
| 424 | |
| 425 | /* We only take memory from free RAM */ |
| 426 | if (desc->type != EFI_CONVENTIONAL_MEMORY) |
| 427 | continue; |
| 428 | |
| 429 | /* Out of bounds for max_addr */ |
| 430 | if ((ret + len) > max_addr) |
| 431 | continue; |
| 432 | |
| 433 | /* Out of bounds for upper map limit */ |
| 434 | if ((ret + len) > desc_end) |
| 435 | continue; |
| 436 | |
| 437 | /* Out of bounds for lower map limit */ |
| 438 | if (ret < desc->physical_start) |
| 439 | continue; |
| 440 | |
| 441 | /* Return the highest address in this map within bounds */ |
| 442 | return ret; |
| 443 | } |
| 444 | |
| 445 | return 0; |
| 446 | } |
| 447 | |
Heinrich Schuchardt | 474a6f5 | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 448 | /* |
| 449 | * Allocate memory pages. |
| 450 | * |
| 451 | * @type type of allocation to be performed |
| 452 | * @memory_type usage type of the allocated memory |
| 453 | * @pages number of pages to be allocated |
| 454 | * @memory allocated memory |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 455 | * Return: status code |
Heinrich Schuchardt | 474a6f5 | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 456 | */ |
Heinrich Schuchardt | 49d225e | 2021-08-17 15:05:31 +0200 | [diff] [blame] | 457 | efi_status_t efi_allocate_pages(enum efi_allocate_type type, |
| 458 | enum efi_memory_type memory_type, |
Heinrich Schuchardt | f5a2a93 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 459 | efi_uintn_t pages, uint64_t *memory) |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 460 | { |
| 461 | u64 len = pages << EFI_PAGE_SHIFT; |
Heinrich Schuchardt | 8ae3985 | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 462 | efi_status_t ret; |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 463 | uint64_t addr; |
| 464 | |
Heinrich Schuchardt | f12bcc9 | 2019-04-23 00:30:53 +0200 | [diff] [blame] | 465 | /* Check import parameters */ |
| 466 | if (memory_type >= EFI_PERSISTENT_MEMORY_TYPE && |
| 467 | memory_type <= 0x6FFFFFFF) |
| 468 | return EFI_INVALID_PARAMETER; |
Heinrich Schuchardt | 4d5e071 | 2018-07-02 12:53:53 +0200 | [diff] [blame] | 469 | if (!memory) |
| 470 | return EFI_INVALID_PARAMETER; |
| 471 | |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 472 | switch (type) { |
Heinrich Schuchardt | 7c92fd6 | 2018-01-30 21:08:00 +0100 | [diff] [blame] | 473 | case EFI_ALLOCATE_ANY_PAGES: |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 474 | /* Any page */ |
Stephen Warren | 14deb5e | 2018-08-30 15:43:45 -0600 | [diff] [blame] | 475 | addr = efi_find_free_memory(len, -1ULL); |
Heinrich Schuchardt | 8ae3985 | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 476 | if (!addr) |
| 477 | return EFI_OUT_OF_RESOURCES; |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 478 | break; |
Heinrich Schuchardt | 7c92fd6 | 2018-01-30 21:08:00 +0100 | [diff] [blame] | 479 | case EFI_ALLOCATE_MAX_ADDRESS: |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 480 | /* Max address */ |
| 481 | addr = efi_find_free_memory(len, *memory); |
Heinrich Schuchardt | 8ae3985 | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 482 | if (!addr) |
| 483 | return EFI_OUT_OF_RESOURCES; |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 484 | break; |
Heinrich Schuchardt | 7c92fd6 | 2018-01-30 21:08:00 +0100 | [diff] [blame] | 485 | case EFI_ALLOCATE_ADDRESS: |
Heinrich Schuchardt | 53def68 | 2022-11-06 01:52:13 +0100 | [diff] [blame] | 486 | if (*memory & EFI_PAGE_MASK) |
| 487 | return EFI_NOT_FOUND; |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 488 | /* Exact address, reserve it. The addr is already in *memory. */ |
Heinrich Schuchardt | 8ae3985 | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 489 | ret = efi_check_allocated(*memory, false); |
| 490 | if (ret != EFI_SUCCESS) |
| 491 | return EFI_NOT_FOUND; |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 492 | addr = *memory; |
| 493 | break; |
| 494 | default: |
| 495 | /* UEFI doesn't specify other allocation types */ |
Heinrich Schuchardt | 8ae3985 | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 496 | return EFI_INVALID_PARAMETER; |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 497 | } |
| 498 | |
Heinrich Schuchardt | 8ae3985 | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 499 | /* Reserve that map in our memory maps */ |
Michael Walle | 714497e | 2020-05-17 12:29:19 +0200 | [diff] [blame] | 500 | ret = efi_add_memory_map_pg(addr, pages, memory_type, true); |
| 501 | if (ret != EFI_SUCCESS) |
Heinrich Schuchardt | 8ae3985 | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 502 | /* Map would overlap, bail out */ |
| 503 | return EFI_OUT_OF_RESOURCES; |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 504 | |
Heinrich Schuchardt | 8ae3985 | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 505 | *memory = addr; |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 506 | |
Heinrich Schuchardt | 8ae3985 | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 507 | return EFI_SUCCESS; |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | void *efi_alloc(uint64_t len, int memory_type) |
| 511 | { |
| 512 | uint64_t ret = 0; |
Heinrich Schuchardt | c3772ca | 2018-11-18 17:58:49 +0100 | [diff] [blame] | 513 | uint64_t pages = efi_size_in_pages(len); |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 514 | efi_status_t r; |
| 515 | |
Heinrich Schuchardt | e09159c | 2018-05-26 10:32:27 +0200 | [diff] [blame] | 516 | r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type, pages, |
| 517 | &ret); |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 518 | if (r == EFI_SUCCESS) |
| 519 | return (void*)(uintptr_t)ret; |
| 520 | |
| 521 | return NULL; |
| 522 | } |
| 523 | |
Heinrich Schuchardt | 2c3ec28 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 524 | /** |
| 525 | * efi_free_pages() - free memory pages |
Heinrich Schuchardt | 474a6f5 | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 526 | * |
Heinrich Schuchardt | 2c3ec28 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 527 | * @memory: start of the memory area to be freed |
| 528 | * @pages: number of pages to be freed |
| 529 | * Return: status code |
Heinrich Schuchardt | 474a6f5 | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 530 | */ |
Heinrich Schuchardt | f5a2a93 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 531 | efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages) |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 532 | { |
Heinrich Schuchardt | 7d3af58 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 533 | efi_status_t ret; |
| 534 | |
Heinrich Schuchardt | f756fe8 | 2019-05-11 08:21:17 +0200 | [diff] [blame] | 535 | ret = efi_check_allocated(memory, true); |
Heinrich Schuchardt | 7d3af58 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 536 | if (ret != EFI_SUCCESS) |
| 537 | return ret; |
Stefan Brüns | b61d857 | 2016-10-01 23:32:27 +0200 | [diff] [blame] | 538 | |
Heinrich Schuchardt | 2c3ec28 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 539 | /* Sanity check */ |
Heinrich Schuchardt | e00b82d | 2019-04-25 18:41:40 +0200 | [diff] [blame] | 540 | if (!memory || (memory & EFI_PAGE_MASK) || !pages) { |
Heinrich Schuchardt | 2c3ec28 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 541 | printf("%s: illegal free 0x%llx, 0x%zx\n", __func__, |
| 542 | memory, pages); |
| 543 | return EFI_INVALID_PARAMETER; |
| 544 | } |
| 545 | |
Michael Walle | 714497e | 2020-05-17 12:29:19 +0200 | [diff] [blame] | 546 | ret = efi_add_memory_map_pg(memory, pages, EFI_CONVENTIONAL_MEMORY, |
| 547 | false); |
Bryan O'Donoghue | b225c92 | 2019-07-15 12:00:39 +0100 | [diff] [blame] | 548 | if (ret != EFI_SUCCESS) |
| 549 | return EFI_NOT_FOUND; |
Stefan Brüns | b61d857 | 2016-10-01 23:32:27 +0200 | [diff] [blame] | 550 | |
Bryan O'Donoghue | b225c92 | 2019-07-15 12:00:39 +0100 | [diff] [blame] | 551 | return ret; |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 552 | } |
| 553 | |
Heinrich Schuchardt | 2c3ec28 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 554 | /** |
Ilias Apalodimas | ebdea88 | 2021-10-11 15:10:23 +0300 | [diff] [blame] | 555 | * efi_alloc_aligned_pages - allocate |
| 556 | * |
| 557 | * @len: len in bytes |
| 558 | * @memory_type: usage type of the allocated memory |
| 559 | * @align: alignment in bytes |
| 560 | * Return: aligned memory or NULL |
| 561 | */ |
| 562 | void *efi_alloc_aligned_pages(u64 len, int memory_type, size_t align) |
| 563 | { |
| 564 | u64 req_pages = efi_size_in_pages(len); |
| 565 | u64 true_pages = req_pages + efi_size_in_pages(align) - 1; |
| 566 | u64 free_pages; |
| 567 | u64 aligned_mem; |
| 568 | efi_status_t r; |
| 569 | u64 mem; |
| 570 | |
| 571 | /* align must be zero or a power of two */ |
| 572 | if (align & (align - 1)) |
| 573 | return NULL; |
| 574 | |
| 575 | /* Check for overflow */ |
| 576 | if (true_pages < req_pages) |
| 577 | return NULL; |
| 578 | |
| 579 | if (align < EFI_PAGE_SIZE) { |
| 580 | r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type, |
| 581 | req_pages, &mem); |
| 582 | return (r == EFI_SUCCESS) ? (void *)(uintptr_t)mem : NULL; |
| 583 | } |
| 584 | |
| 585 | r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type, |
| 586 | true_pages, &mem); |
| 587 | if (r != EFI_SUCCESS) |
| 588 | return NULL; |
| 589 | |
| 590 | aligned_mem = ALIGN(mem, align); |
| 591 | /* Free pages before alignment */ |
| 592 | free_pages = efi_size_in_pages(aligned_mem - mem); |
| 593 | if (free_pages) |
| 594 | efi_free_pages(mem, free_pages); |
| 595 | |
| 596 | /* Free trailing pages */ |
| 597 | free_pages = true_pages - (req_pages + free_pages); |
| 598 | if (free_pages) { |
| 599 | mem = aligned_mem + req_pages * EFI_PAGE_SIZE; |
| 600 | efi_free_pages(mem, free_pages); |
| 601 | } |
| 602 | |
| 603 | return (void *)(uintptr_t)aligned_mem; |
| 604 | } |
| 605 | |
| 606 | /** |
Heinrich Schuchardt | 2c3ec28 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 607 | * efi_allocate_pool - allocate memory from pool |
Heinrich Schuchardt | 474a6f5 | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 608 | * |
Heinrich Schuchardt | 2c3ec28 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 609 | * @pool_type: type of the pool from which memory is to be allocated |
| 610 | * @size: number of bytes to be allocated |
| 611 | * @buffer: allocated memory |
| 612 | * Return: status code |
Heinrich Schuchardt | 474a6f5 | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 613 | */ |
Heinrich Schuchardt | 49d225e | 2021-08-17 15:05:31 +0200 | [diff] [blame] | 614 | efi_status_t efi_allocate_pool(enum efi_memory_type pool_type, efi_uintn_t size, void **buffer) |
Stefan Brüns | ead1274 | 2016-10-09 22:17:18 +0200 | [diff] [blame] | 615 | { |
| 616 | efi_status_t r; |
Heinrich Schuchardt | 306b167 | 2019-03-18 20:01:59 +0100 | [diff] [blame] | 617 | u64 addr; |
Alexander Graf | 282a06c | 2018-06-18 17:23:15 +0200 | [diff] [blame] | 618 | struct efi_pool_allocation *alloc; |
Heinrich Schuchardt | c3772ca | 2018-11-18 17:58:49 +0100 | [diff] [blame] | 619 | u64 num_pages = efi_size_in_pages(size + |
| 620 | sizeof(struct efi_pool_allocation)); |
Stefan Brüns | 42417bc | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 621 | |
Heinrich Schuchardt | 4d5e071 | 2018-07-02 12:53:53 +0200 | [diff] [blame] | 622 | if (!buffer) |
| 623 | return EFI_INVALID_PARAMETER; |
| 624 | |
Stefan Brüns | 42417bc | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 625 | if (size == 0) { |
| 626 | *buffer = NULL; |
| 627 | return EFI_SUCCESS; |
| 628 | } |
Stefan Brüns | ead1274 | 2016-10-09 22:17:18 +0200 | [diff] [blame] | 629 | |
Heinrich Schuchardt | e09159c | 2018-05-26 10:32:27 +0200 | [diff] [blame] | 630 | r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, pool_type, num_pages, |
Heinrich Schuchardt | 306b167 | 2019-03-18 20:01:59 +0100 | [diff] [blame] | 631 | &addr); |
Stefan Brüns | 42417bc | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 632 | if (r == EFI_SUCCESS) { |
Heinrich Schuchardt | 306b167 | 2019-03-18 20:01:59 +0100 | [diff] [blame] | 633 | alloc = (struct efi_pool_allocation *)(uintptr_t)addr; |
Stefan Brüns | 42417bc | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 634 | alloc->num_pages = num_pages; |
Heinrich Schuchardt | 2c3ec28 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 635 | alloc->checksum = checksum(alloc); |
Stefan Brüns | 42417bc | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 636 | *buffer = alloc->data; |
| 637 | } |
| 638 | |
| 639 | return r; |
| 640 | } |
| 641 | |
Heinrich Schuchardt | 2c3ec28 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 642 | /** |
| 643 | * efi_free_pool() - free memory from pool |
Heinrich Schuchardt | 474a6f5 | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 644 | * |
Heinrich Schuchardt | 2c3ec28 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 645 | * @buffer: start of memory to be freed |
| 646 | * Return: status code |
Heinrich Schuchardt | 474a6f5 | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 647 | */ |
Stefan Brüns | 42417bc | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 648 | efi_status_t efi_free_pool(void *buffer) |
| 649 | { |
Heinrich Schuchardt | 7d3af58 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 650 | efi_status_t ret; |
Stefan Brüns | 42417bc | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 651 | struct efi_pool_allocation *alloc; |
| 652 | |
Heinrich Schuchardt | 0e22c7c | 2019-06-12 07:17:04 +0200 | [diff] [blame] | 653 | if (!buffer) |
| 654 | return EFI_INVALID_PARAMETER; |
| 655 | |
Heinrich Schuchardt | f756fe8 | 2019-05-11 08:21:17 +0200 | [diff] [blame] | 656 | ret = efi_check_allocated((uintptr_t)buffer, true); |
Heinrich Schuchardt | 7d3af58 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 657 | if (ret != EFI_SUCCESS) |
| 658 | return ret; |
xypron.glpk@gmx.de | 71275a3 | 2017-07-14 19:12:39 +0200 | [diff] [blame] | 659 | |
Stefan Brüns | 42417bc | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 660 | alloc = container_of(buffer, struct efi_pool_allocation, data); |
Heinrich Schuchardt | 2c3ec28 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 661 | |
| 662 | /* Check that this memory was allocated by efi_allocate_pool() */ |
| 663 | if (((uintptr_t)alloc & EFI_PAGE_MASK) || |
| 664 | alloc->checksum != checksum(alloc)) { |
| 665 | printf("%s: illegal free 0x%p\n", __func__, buffer); |
| 666 | return EFI_INVALID_PARAMETER; |
| 667 | } |
| 668 | /* Avoid double free */ |
| 669 | alloc->checksum = 0; |
Stefan Brüns | 42417bc | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 670 | |
Heinrich Schuchardt | 7d3af58 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 671 | ret = efi_free_pages((uintptr_t)alloc, alloc->num_pages); |
Stefan Brüns | ead1274 | 2016-10-09 22:17:18 +0200 | [diff] [blame] | 672 | |
Heinrich Schuchardt | 7d3af58 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 673 | return ret; |
Stefan Brüns | ead1274 | 2016-10-09 22:17:18 +0200 | [diff] [blame] | 674 | } |
| 675 | |
Heinrich Schuchardt | 474a6f5 | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 676 | /* |
| 677 | * Get map describing memory usage. |
| 678 | * |
| 679 | * @memory_map_size on entry the size, in bytes, of the memory map buffer, |
| 680 | * on exit the size of the copied memory map |
| 681 | * @memory_map buffer to which the memory map is written |
| 682 | * @map_key key for the memory map |
| 683 | * @descriptor_size size of an individual memory descriptor |
| 684 | * @descriptor_version version number of the memory descriptor structure |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 685 | * Return: status code |
Heinrich Schuchardt | 474a6f5 | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 686 | */ |
Heinrich Schuchardt | f5a2a93 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 687 | efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size, |
| 688 | struct efi_mem_desc *memory_map, |
| 689 | efi_uintn_t *map_key, |
| 690 | efi_uintn_t *descriptor_size, |
| 691 | uint32_t *descriptor_version) |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 692 | { |
Heinrich Schuchardt | f5a2a93 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 693 | efi_uintn_t map_size = 0; |
Alexander Graf | cee752f | 2016-04-11 23:51:02 +0200 | [diff] [blame] | 694 | int map_entries = 0; |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 695 | struct list_head *lhandle; |
Heinrich Schuchardt | fa995d0d | 2018-08-06 22:28:18 +0200 | [diff] [blame] | 696 | efi_uintn_t provided_map_size; |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 697 | |
Heinrich Schuchardt | 8e83555 | 2018-07-02 12:53:54 +0200 | [diff] [blame] | 698 | if (!memory_map_size) |
| 699 | return EFI_INVALID_PARAMETER; |
| 700 | |
Heinrich Schuchardt | fa995d0d | 2018-08-06 22:28:18 +0200 | [diff] [blame] | 701 | provided_map_size = *memory_map_size; |
| 702 | |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 703 | list_for_each(lhandle, &efi_mem) |
Alexander Graf | cee752f | 2016-04-11 23:51:02 +0200 | [diff] [blame] | 704 | map_entries++; |
| 705 | |
| 706 | map_size = map_entries * sizeof(struct efi_mem_desc); |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 707 | |
Rob Clark | a1b2482 | 2017-07-26 14:34:05 -0400 | [diff] [blame] | 708 | *memory_map_size = map_size; |
| 709 | |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 710 | if (descriptor_size) |
| 711 | *descriptor_size = sizeof(struct efi_mem_desc); |
| 712 | |
Mian Yousaf Kaukab | 4c02c11 | 2016-09-05 23:59:22 +0200 | [diff] [blame] | 713 | if (descriptor_version) |
| 714 | *descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION; |
| 715 | |
AKASHI Takahiro | b484296 | 2020-03-11 15:18:18 +0900 | [diff] [blame] | 716 | if (provided_map_size < map_size) |
| 717 | return EFI_BUFFER_TOO_SMALL; |
| 718 | |
| 719 | if (!memory_map) |
| 720 | return EFI_INVALID_PARAMETER; |
| 721 | |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 722 | /* Copy list into array */ |
Heinrich Schuchardt | 8e83555 | 2018-07-02 12:53:54 +0200 | [diff] [blame] | 723 | /* Return the list in ascending order */ |
| 724 | memory_map = &memory_map[map_entries - 1]; |
| 725 | list_for_each(lhandle, &efi_mem) { |
| 726 | struct efi_mem_list *lmem; |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 727 | |
Heinrich Schuchardt | 8e83555 | 2018-07-02 12:53:54 +0200 | [diff] [blame] | 728 | lmem = list_entry(lhandle, struct efi_mem_list, link); |
| 729 | *memory_map = lmem->desc; |
| 730 | memory_map--; |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 731 | } |
| 732 | |
Heinrich Schuchardt | 8e83555 | 2018-07-02 12:53:54 +0200 | [diff] [blame] | 733 | if (map_key) |
Heinrich Schuchardt | 1fcb7ea | 2018-07-02 12:53:55 +0200 | [diff] [blame] | 734 | *map_key = efi_memory_map_key; |
xypron.glpk@gmx.de | c6e3c3e | 2017-07-21 19:05:44 +0200 | [diff] [blame] | 735 | |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 736 | return EFI_SUCCESS; |
| 737 | } |
| 738 | |
Park, Aiden | b5b9eff | 2019-09-03 17:43:43 +0000 | [diff] [blame] | 739 | /** |
| 740 | * efi_add_conventional_memory_map() - add a RAM memory area to the map |
| 741 | * |
| 742 | * @ram_start: start address of a RAM memory area |
| 743 | * @ram_end: end address of a RAM memory area |
| 744 | * @ram_top: max address to be used as conventional memory |
| 745 | * Return: status code |
| 746 | */ |
| 747 | efi_status_t efi_add_conventional_memory_map(u64 ram_start, u64 ram_end, |
| 748 | u64 ram_top) |
| 749 | { |
| 750 | u64 pages; |
| 751 | |
| 752 | /* Remove partial pages */ |
| 753 | ram_end &= ~EFI_PAGE_MASK; |
| 754 | ram_start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK; |
| 755 | |
| 756 | if (ram_end <= ram_start) { |
| 757 | /* Invalid mapping */ |
| 758 | return EFI_INVALID_PARAMETER; |
| 759 | } |
| 760 | |
| 761 | pages = (ram_end - ram_start) >> EFI_PAGE_SHIFT; |
| 762 | |
Michael Walle | 714497e | 2020-05-17 12:29:19 +0200 | [diff] [blame] | 763 | efi_add_memory_map_pg(ram_start, pages, |
| 764 | EFI_CONVENTIONAL_MEMORY, false); |
Park, Aiden | b5b9eff | 2019-09-03 17:43:43 +0000 | [diff] [blame] | 765 | |
| 766 | /* |
| 767 | * Boards may indicate to the U-Boot memory core that they |
| 768 | * can not support memory above ram_top. Let's honor this |
| 769 | * in the efi_loader subsystem too by declaring any memory |
| 770 | * above ram_top as "already occupied by firmware". |
| 771 | */ |
| 772 | if (ram_top < ram_start) { |
| 773 | /* ram_top is before this region, reserve all */ |
Michael Walle | 714497e | 2020-05-17 12:29:19 +0200 | [diff] [blame] | 774 | efi_add_memory_map_pg(ram_start, pages, |
| 775 | EFI_BOOT_SERVICES_DATA, true); |
Heinrich Schuchardt | 8da26f5 | 2022-04-25 23:54:48 +0200 | [diff] [blame] | 776 | } else if (ram_top < ram_end) { |
Park, Aiden | b5b9eff | 2019-09-03 17:43:43 +0000 | [diff] [blame] | 777 | /* ram_top is inside this region, reserve parts */ |
| 778 | pages = (ram_end - ram_top) >> EFI_PAGE_SHIFT; |
| 779 | |
Michael Walle | 714497e | 2020-05-17 12:29:19 +0200 | [diff] [blame] | 780 | efi_add_memory_map_pg(ram_top, pages, |
| 781 | EFI_BOOT_SERVICES_DATA, true); |
Park, Aiden | b5b9eff | 2019-09-03 17:43:43 +0000 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | return EFI_SUCCESS; |
| 785 | } |
| 786 | |
York Sun | 4263374 | 2017-03-06 09:02:27 -0800 | [diff] [blame] | 787 | __weak void efi_add_known_memory(void) |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 788 | { |
Alexander Graf | 7b78d64 | 2018-11-30 21:24:56 +0100 | [diff] [blame] | 789 | u64 ram_top = board_get_usable_ram_top(0) & ~EFI_PAGE_MASK; |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 790 | int i; |
| 791 | |
Heinrich Schuchardt | 23f5f4a | 2019-01-05 23:41:36 +0100 | [diff] [blame] | 792 | /* |
| 793 | * ram_top is just outside mapped memory. So use an offset of one for |
| 794 | * mapping the sandbox address. |
| 795 | */ |
| 796 | ram_top = (uintptr_t)map_sysmem(ram_top - 1, 0) + 1; |
| 797 | |
Alexander Graf | 7b78d64 | 2018-11-30 21:24:56 +0100 | [diff] [blame] | 798 | /* Fix for 32bit targets with ram_top at 4G */ |
| 799 | if (!ram_top) |
| 800 | ram_top = 0x100000000ULL; |
| 801 | |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 802 | /* Add RAM */ |
| 803 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { |
Park, Aiden | b5b9eff | 2019-09-03 17:43:43 +0000 | [diff] [blame] | 804 | u64 ram_end, ram_start; |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 805 | |
Heinrich Schuchardt | 4975974 | 2018-11-18 17:58:46 +0100 | [diff] [blame] | 806 | ram_start = (uintptr_t)map_sysmem(gd->bd->bi_dram[i].start, 0); |
Heinrich Schuchardt | 108bdff | 2018-11-12 18:55:24 +0100 | [diff] [blame] | 807 | ram_end = ram_start + gd->bd->bi_dram[i].size; |
| 808 | |
Park, Aiden | b5b9eff | 2019-09-03 17:43:43 +0000 | [diff] [blame] | 809 | efi_add_conventional_memory_map(ram_start, ram_end, ram_top); |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 810 | } |
York Sun | 4263374 | 2017-03-06 09:02:27 -0800 | [diff] [blame] | 811 | } |
| 812 | |
Simon Glass | 69259b8 | 2018-06-18 17:23:12 +0200 | [diff] [blame] | 813 | /* Add memory regions for U-Boot's memory and for the runtime services code */ |
| 814 | static void add_u_boot_and_runtime(void) |
York Sun | 4263374 | 2017-03-06 09:02:27 -0800 | [diff] [blame] | 815 | { |
| 816 | unsigned long runtime_start, runtime_end, runtime_pages; |
Alexander Graf | 7a82c30 | 2018-09-17 13:54:33 +0200 | [diff] [blame] | 817 | unsigned long runtime_mask = EFI_PAGE_MASK; |
York Sun | 4263374 | 2017-03-06 09:02:27 -0800 | [diff] [blame] | 818 | unsigned long uboot_start, uboot_pages; |
Heinrich Schuchardt | 74b869b | 2020-07-29 12:37:35 +0200 | [diff] [blame] | 819 | unsigned long uboot_stack_size = CONFIG_STACK_SIZE; |
York Sun | 4263374 | 2017-03-06 09:02:27 -0800 | [diff] [blame] | 820 | |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 821 | /* Add U-Boot */ |
Heinrich Schuchardt | 7264e21 | 2019-11-08 20:42:53 +0100 | [diff] [blame] | 822 | uboot_start = ((uintptr_t)map_sysmem(gd->start_addr_sp, 0) - |
| 823 | uboot_stack_size) & ~EFI_PAGE_MASK; |
| 824 | uboot_pages = ((uintptr_t)map_sysmem(gd->ram_top - 1, 0) - |
| 825 | uboot_start + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT; |
Heinrich Schuchardt | 1a12796 | 2022-11-29 16:00:41 +0100 | [diff] [blame] | 826 | efi_add_memory_map_pg(uboot_start, uboot_pages, EFI_BOOT_SERVICES_CODE, |
Michael Walle | 714497e | 2020-05-17 12:29:19 +0200 | [diff] [blame] | 827 | false); |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 828 | |
Alexander Graf | 7a82c30 | 2018-09-17 13:54:33 +0200 | [diff] [blame] | 829 | #if defined(__aarch64__) |
| 830 | /* |
| 831 | * Runtime Services must be 64KiB aligned according to the |
| 832 | * "AArch64 Platforms" section in the UEFI spec (2.7+). |
| 833 | */ |
| 834 | |
| 835 | runtime_mask = SZ_64K - 1; |
| 836 | #endif |
| 837 | |
| 838 | /* |
| 839 | * Add Runtime Services. We mark surrounding boottime code as runtime as |
| 840 | * well to fulfill the runtime alignment constraints but avoid padding. |
| 841 | */ |
| 842 | runtime_start = (ulong)&__efi_runtime_start & ~runtime_mask; |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 843 | runtime_end = (ulong)&__efi_runtime_stop; |
Alexander Graf | 7a82c30 | 2018-09-17 13:54:33 +0200 | [diff] [blame] | 844 | runtime_end = (runtime_end + runtime_mask) & ~runtime_mask; |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 845 | runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT; |
Michael Walle | 714497e | 2020-05-17 12:29:19 +0200 | [diff] [blame] | 846 | efi_add_memory_map_pg(runtime_start, runtime_pages, |
| 847 | EFI_RUNTIME_SERVICES_CODE, false); |
Simon Glass | 69259b8 | 2018-06-18 17:23:12 +0200 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | int efi_memory_init(void) |
| 851 | { |
| 852 | efi_add_known_memory(); |
| 853 | |
Heinrich Schuchardt | 7264e21 | 2019-11-08 20:42:53 +0100 | [diff] [blame] | 854 | add_u_boot_and_runtime(); |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 855 | |
Alexander Graf | 51735ae | 2016-05-11 18:25:48 +0200 | [diff] [blame] | 856 | #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER |
| 857 | /* Request a 32bit 64MB bounce buffer region */ |
| 858 | uint64_t efi_bounce_buffer_addr = 0xffffffff; |
| 859 | |
Heinrich Schuchardt | 1a12796 | 2022-11-29 16:00:41 +0100 | [diff] [blame] | 860 | if (efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, EFI_BOOT_SERVICES_DATA, |
Alexander Graf | 51735ae | 2016-05-11 18:25:48 +0200 | [diff] [blame] | 861 | (64 * 1024 * 1024) >> EFI_PAGE_SHIFT, |
| 862 | &efi_bounce_buffer_addr) != EFI_SUCCESS) |
| 863 | return -1; |
| 864 | |
| 865 | efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr; |
| 866 | #endif |
| 867 | |
Alexander Graf | 5d00995 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 868 | return 0; |
| 869 | } |