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