Tom Rini | 70df9d6 | 2018-05-07 17:02:21 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 2 | /* |
| 3 | * EFI application memory management |
| 4 | * |
| 5 | * Copyright (c) 2016 Alexander Graf |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
Heinrich Schuchardt | 4564025 | 2023-03-19 09:20:22 +0100 | [diff] [blame] | 8 | #define LOG_CATEGORY LOGC_EFI |
| 9 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 10 | #include <common.h> |
| 11 | #include <efi_loader.h> |
Simon Glass | 6980b6b | 2019-11-14 12:57:45 -0700 | [diff] [blame] | 12 | #include <init.h> |
Heinrich Schuchardt | 4564025 | 2023-03-19 09:20:22 +0100 | [diff] [blame] | 13 | #include <log.h> |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 14 | #include <malloc.h> |
Alexander Graf | cd40517 | 2018-06-18 17:23:15 +0200 | [diff] [blame] | 15 | #include <mapmem.h> |
Simon Glass | 3e85217 | 2018-03-08 21:53:27 +0100 | [diff] [blame] | 16 | #include <watchdog.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 17 | #include <asm/cache.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 18 | #include <asm/global_data.h> |
Alexander Graf | de2a13b | 2016-03-30 16:38:29 +0200 | [diff] [blame] | 19 | #include <linux/list_sort.h> |
Alexander Graf | 15937a2 | 2018-09-17 13:54:33 +0200 | [diff] [blame] | 20 | #include <linux/sizes.h> |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 21 | |
| 22 | DECLARE_GLOBAL_DATA_PTR; |
| 23 | |
Heinrich Schuchardt | b4e2ba3 | 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 | 0f233c4 | 2018-07-02 12:53:55 +0200 | [diff] [blame] | 27 | efi_uintn_t efi_memory_map_key; |
| 28 | |
Alexander Graf | 8623f92 | 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 | 66d8623 | 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 | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 38 | /* This list contains all memory map items */ |
Bin Meng | c8025a3 | 2023-04-05 20:15:18 +0800 | [diff] [blame] | 39 | static LIST_HEAD(efi_mem); |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 40 | |
Alexander Graf | 7c00a3c | 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 | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 45 | /** |
Heinrich Schuchardt | 79f882a | 2019-07-14 12:28:56 +0200 | [diff] [blame] | 46 | * struct efi_pool_allocation - memory block allocated from pool |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 47 | * |
| 48 | * @num_pages: number of pages allocated |
| 49 | * @checksum: checksum |
Heinrich Schuchardt | 79f882a | 2019-07-14 12:28:56 +0200 | [diff] [blame] | 50 | * @data: allocated pool memory |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 51 | * |
Heinrich Schuchardt | 79f882a | 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 | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 54 | * to be able to free the correct amount later. |
Heinrich Schuchardt | 79f882a | 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 | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 59 | * EFI requires 8 byte alignment for pool allocations, so we can |
Heinrich Schuchardt | 79f882a | 2019-07-14 12:28:56 +0200 | [diff] [blame] | 60 | * prepend each allocation with these header fields. |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 61 | */ |
| 62 | struct efi_pool_allocation { |
| 63 | u64 num_pages; |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 64 | u64 checksum; |
Rob Clark | e597fd4 | 2017-09-13 18:05:36 -0400 | [diff] [blame] | 65 | char data[] __aligned(ARCH_DMA_MINALIGN); |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 66 | }; |
| 67 | |
Heinrich Schuchardt | b4e2ba3 | 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 | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 84 | /** |
| 85 | * efi_mem_cmp() - comparator function for sorting memory map |
| 86 | * |
Alexander Graf | de2a13b | 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 | b9b838d | 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 | de2a13b | 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 | b9b838d | 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 | d728814 | 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 | b9b838d | 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 | de2a13b | 2016-03-30 16:38:29 +0200 | [diff] [blame] | 127 | static void efi_mem_sort(void) |
| 128 | { |
Alexander Graf | d728814 | 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 | de2a13b | 2016-03-30 16:38:29 +0200 | [diff] [blame] | 133 | list_sort(NULL, &efi_mem, efi_mem_cmp); |
Alexander Graf | d728814 | 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 | de2a13b | 2016-03-30 16:38:29 +0200 | [diff] [blame] | 170 | } |
| 171 | |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 172 | /** |
| 173 | * efi_mem_carve_out() - unmap memory region |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 174 | * |
Heinrich Schuchardt | f00caa5 | 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 | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 178 | * Return: the number of overlapping pages which have been |
Heinrich Schuchardt | f00caa5 | 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 | 1b00dc1 | 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 | f00caa5 | 2018-05-27 16:45:09 +0200 | [diff] [blame] | 191 | * to re-add the already carved out pages to the mapping. |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 192 | */ |
Heinrich Schuchardt | f00caa5 | 2018-05-27 16:45:09 +0200 | [diff] [blame] | 193 | static s64 efi_mem_carve_out(struct efi_mem_list *map, |
Alexander Graf | 8623f92 | 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 | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 207 | return EFI_CARVE_NO_OVERLAP; |
Alexander Graf | 8623f92 | 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 | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 211 | return EFI_CARVE_OVERLAPS_NONRAM; |
Alexander Graf | 8623f92 | 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 | 4e9ad8d | 2016-10-01 23:32:29 +0200 | [diff] [blame] | 222 | free(map); |
| 223 | } else { |
| 224 | map->desc.physical_start = carve_end; |
Heinrich Schuchardt | e83aafa | 2019-04-11 20:08:54 +0200 | [diff] [blame] | 225 | map->desc.virtual_start = carve_end; |
Stefan Brüns | 4e9ad8d | 2016-10-01 23:32:29 +0200 | [diff] [blame] | 226 | map->desc.num_pages = (map_end - carve_end) |
| 227 | >> EFI_PAGE_SHIFT; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 228 | } |
| 229 | |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 230 | return (carve_end - carve_start) >> EFI_PAGE_SHIFT; |
Alexander Graf | 8623f92 | 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 | e83aafa | 2019-04-11 20:08:54 +0200 | [diff] [blame] | 244 | newmap->desc.virtual_start = carve_start; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 245 | newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT; |
Stefan Brüns | 9d65651 | 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 | 8623f92 | 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 | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 252 | return EFI_CARVE_LOOP_AGAIN; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 253 | } |
| 254 | |
Bryan O'Donoghue | 89a22b4 | 2019-07-15 12:00:39 +0100 | [diff] [blame] | 255 | /** |
Michael Walle | 282d386 | 2020-05-17 12:29:19 +0200 | [diff] [blame] | 256 | * efi_add_memory_map_pg() - add pages to the memory map |
Bryan O'Donoghue | 89a22b4 | 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 | ec631ea | 2020-08-28 22:47:41 +0300 | [diff] [blame] | 261 | * @overlap_only_ram: region may only overlap RAM |
Bryan O'Donoghue | 89a22b4 | 2019-07-15 12:00:39 +0100 | [diff] [blame] | 262 | * Return: status code |
| 263 | */ |
Michael Walle | 282d386 | 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 | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 267 | { |
| 268 | struct list_head *lhandle; |
| 269 | struct efi_mem_list *newlist; |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 270 | bool carve_again; |
| 271 | uint64_t carved_pages = 0; |
Heinrich Schuchardt | 485975b | 2019-06-04 20:55:12 +0200 | [diff] [blame] | 272 | struct efi_event *evt; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 273 | |
Heinrich Schuchardt | 706a2fe | 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 | 102f8bd | 2016-07-17 06:57:11 +0200 | [diff] [blame] | 276 | |
Heinrich Schuchardt | 0f233c4 | 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 | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 280 | if (!pages) |
Bryan O'Donoghue | 89a22b4 | 2019-07-15 12:00:39 +0100 | [diff] [blame] | 281 | return EFI_SUCCESS; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 282 | |
Heinrich Schuchardt | 0f233c4 | 2018-07-02 12:53:55 +0200 | [diff] [blame] | 283 | ++efi_memory_map_key; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 284 | newlist = calloc(1, sizeof(*newlist)); |
| 285 | newlist->desc.type = memory_type; |
| 286 | newlist->desc.physical_start = start; |
| 287 | newlist->desc.virtual_start = start; |
| 288 | newlist->desc.num_pages = pages; |
| 289 | |
| 290 | switch (memory_type) { |
| 291 | case EFI_RUNTIME_SERVICES_CODE: |
| 292 | case EFI_RUNTIME_SERVICES_DATA: |
Eugeniu Rosca | c86c081 | 2018-07-14 22:53:30 +0200 | [diff] [blame] | 293 | newlist->desc.attribute = EFI_MEMORY_WB | EFI_MEMORY_RUNTIME; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 294 | break; |
| 295 | case EFI_MMAP_IO: |
Eugeniu Rosca | c86c081 | 2018-07-14 22:53:30 +0200 | [diff] [blame] | 296 | newlist->desc.attribute = EFI_MEMORY_RUNTIME; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 297 | break; |
| 298 | default: |
Eugeniu Rosca | c86c081 | 2018-07-14 22:53:30 +0200 | [diff] [blame] | 299 | newlist->desc.attribute = EFI_MEMORY_WB; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 300 | break; |
| 301 | } |
| 302 | |
| 303 | /* Add our new map */ |
| 304 | do { |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 305 | carve_again = false; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 306 | list_for_each(lhandle, &efi_mem) { |
| 307 | struct efi_mem_list *lmem; |
Heinrich Schuchardt | f00caa5 | 2018-05-27 16:45:09 +0200 | [diff] [blame] | 308 | s64 r; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 309 | |
| 310 | lmem = list_entry(lhandle, struct efi_mem_list, link); |
| 311 | r = efi_mem_carve_out(lmem, &newlist->desc, |
| 312 | overlap_only_ram); |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 313 | switch (r) { |
| 314 | case EFI_CARVE_OVERLAPS_NONRAM: |
| 315 | /* |
| 316 | * The user requested to only have RAM overlaps, |
| 317 | * but we hit a non-RAM region. Error out. |
| 318 | */ |
Bryan O'Donoghue | 89a22b4 | 2019-07-15 12:00:39 +0100 | [diff] [blame] | 319 | return EFI_NO_MAPPING; |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 320 | case EFI_CARVE_NO_OVERLAP: |
| 321 | /* Just ignore this list entry */ |
| 322 | break; |
| 323 | case EFI_CARVE_LOOP_AGAIN: |
| 324 | /* |
| 325 | * We split an entry, but need to loop through |
| 326 | * the list again to actually carve it. |
| 327 | */ |
| 328 | carve_again = true; |
| 329 | break; |
| 330 | default: |
| 331 | /* We carved a number of pages */ |
| 332 | carved_pages += r; |
| 333 | carve_again = true; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 334 | break; |
| 335 | } |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 336 | |
| 337 | if (carve_again) { |
| 338 | /* The list changed, we need to start over */ |
| 339 | break; |
| 340 | } |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 341 | } |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 342 | } while (carve_again); |
| 343 | |
| 344 | if (overlap_only_ram && (carved_pages != pages)) { |
| 345 | /* |
| 346 | * The payload wanted to have RAM overlaps, but we overlapped |
| 347 | * with an unallocated region. Error out. |
| 348 | */ |
Bryan O'Donoghue | 89a22b4 | 2019-07-15 12:00:39 +0100 | [diff] [blame] | 349 | return EFI_NO_MAPPING; |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 350 | } |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 351 | |
| 352 | /* Add our new map */ |
| 353 | list_add_tail(&newlist->link, &efi_mem); |
| 354 | |
Alexander Graf | de2a13b | 2016-03-30 16:38:29 +0200 | [diff] [blame] | 355 | /* And make sure memory is listed in descending order */ |
| 356 | efi_mem_sort(); |
| 357 | |
Heinrich Schuchardt | 485975b | 2019-06-04 20:55:12 +0200 | [diff] [blame] | 358 | /* Notify that the memory map was changed */ |
| 359 | list_for_each_entry(evt, &efi_events, link) { |
| 360 | if (evt->group && |
| 361 | !guidcmp(evt->group, |
| 362 | &efi_guid_event_group_memory_map_change)) { |
Heinrich Schuchardt | 7b4b8d86 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 363 | efi_signal_event(evt); |
Heinrich Schuchardt | 485975b | 2019-06-04 20:55:12 +0200 | [diff] [blame] | 364 | break; |
| 365 | } |
| 366 | } |
| 367 | |
Bryan O'Donoghue | 89a22b4 | 2019-07-15 12:00:39 +0100 | [diff] [blame] | 368 | return EFI_SUCCESS; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 369 | } |
| 370 | |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 371 | /** |
Michael Walle | 282d386 | 2020-05-17 12:29:19 +0200 | [diff] [blame] | 372 | * efi_add_memory_map() - add memory area to the memory map |
| 373 | * |
| 374 | * @start: start address of the memory area |
| 375 | * @size: length in bytes of the memory area |
| 376 | * @memory_type: type of memory added |
| 377 | * |
| 378 | * Return: status code |
| 379 | * |
| 380 | * This function automatically aligns the start and size of the memory area |
| 381 | * to EFI_PAGE_SIZE. |
| 382 | */ |
| 383 | efi_status_t efi_add_memory_map(u64 start, u64 size, int memory_type) |
| 384 | { |
| 385 | u64 pages; |
| 386 | |
| 387 | pages = efi_size_in_pages(size + (start & EFI_PAGE_MASK)); |
| 388 | start &= ~EFI_PAGE_MASK; |
| 389 | |
| 390 | return efi_add_memory_map_pg(start, pages, memory_type, false); |
| 391 | } |
| 392 | |
| 393 | /** |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 394 | * efi_check_allocated() - validate address to be freed |
| 395 | * |
| 396 | * Check that the address is within allocated memory: |
| 397 | * |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 398 | * * The address must be in a range of the memory map. |
| 399 | * * The address may not point to EFI_CONVENTIONAL_MEMORY. |
| 400 | * |
| 401 | * Page alignment is not checked as this is not a requirement of |
| 402 | * efi_free_pool(). |
| 403 | * |
Heinrich Schuchardt | 906f685 | 2019-05-11 08:21:17 +0200 | [diff] [blame] | 404 | * @addr: address of page to be freed |
| 405 | * @must_be_allocated: return success if the page is allocated |
| 406 | * Return: status code |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 407 | */ |
Heinrich Schuchardt | 906f685 | 2019-05-11 08:21:17 +0200 | [diff] [blame] | 408 | static efi_status_t efi_check_allocated(u64 addr, bool must_be_allocated) |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 409 | { |
| 410 | struct efi_mem_list *item; |
| 411 | |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 412 | list_for_each_entry(item, &efi_mem, link) { |
| 413 | u64 start = item->desc.physical_start; |
| 414 | u64 end = start + (item->desc.num_pages << EFI_PAGE_SHIFT); |
| 415 | |
| 416 | if (addr >= start && addr < end) { |
Heinrich Schuchardt | 906f685 | 2019-05-11 08:21:17 +0200 | [diff] [blame] | 417 | if (must_be_allocated ^ |
| 418 | (item->desc.type == EFI_CONVENTIONAL_MEMORY)) |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 419 | return EFI_SUCCESS; |
| 420 | else |
| 421 | return EFI_NOT_FOUND; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | return EFI_NOT_FOUND; |
| 426 | } |
| 427 | |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 428 | /** |
| 429 | * efi_find_free_memory() - find free memory pages |
| 430 | * |
| 431 | * @len: size of memory area needed |
| 432 | * @max_addr: highest address to allocate |
| 433 | * Return: pointer to free memory area or 0 |
| 434 | */ |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 435 | static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr) |
| 436 | { |
| 437 | struct list_head *lhandle; |
| 438 | |
Alexander Graf | c3b2a25 | 2018-11-05 00:30:46 +0100 | [diff] [blame] | 439 | /* |
| 440 | * Prealign input max address, so we simplify our matching |
| 441 | * logic below and can just reuse it as return pointer. |
| 442 | */ |
| 443 | max_addr &= ~EFI_PAGE_MASK; |
| 444 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 445 | list_for_each(lhandle, &efi_mem) { |
| 446 | struct efi_mem_list *lmem = list_entry(lhandle, |
| 447 | struct efi_mem_list, link); |
| 448 | struct efi_mem_desc *desc = &lmem->desc; |
| 449 | uint64_t desc_len = desc->num_pages << EFI_PAGE_SHIFT; |
| 450 | uint64_t desc_end = desc->physical_start + desc_len; |
| 451 | uint64_t curmax = min(max_addr, desc_end); |
| 452 | uint64_t ret = curmax - len; |
| 453 | |
| 454 | /* We only take memory from free RAM */ |
| 455 | if (desc->type != EFI_CONVENTIONAL_MEMORY) |
| 456 | continue; |
| 457 | |
| 458 | /* Out of bounds for max_addr */ |
| 459 | if ((ret + len) > max_addr) |
| 460 | continue; |
| 461 | |
| 462 | /* Out of bounds for upper map limit */ |
| 463 | if ((ret + len) > desc_end) |
| 464 | continue; |
| 465 | |
| 466 | /* Out of bounds for lower map limit */ |
| 467 | if (ret < desc->physical_start) |
| 468 | continue; |
| 469 | |
| 470 | /* Return the highest address in this map within bounds */ |
| 471 | return ret; |
| 472 | } |
| 473 | |
| 474 | return 0; |
| 475 | } |
| 476 | |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 477 | /** |
| 478 | * efi_allocate_pages - allocate memory pages |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 479 | * |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 480 | * @type: type of allocation to be performed |
| 481 | * @memory_type: usage type of the allocated memory |
| 482 | * @pages: number of pages to be allocated |
| 483 | * @memory: allocated memory |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 484 | * Return: status code |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 485 | */ |
Heinrich Schuchardt | 9822894 | 2021-08-17 15:05:31 +0200 | [diff] [blame] | 486 | efi_status_t efi_allocate_pages(enum efi_allocate_type type, |
| 487 | enum efi_memory_type memory_type, |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 488 | efi_uintn_t pages, uint64_t *memory) |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 489 | { |
| 490 | u64 len = pages << EFI_PAGE_SHIFT; |
Heinrich Schuchardt | fbdb93c | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 491 | efi_status_t ret; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 492 | uint64_t addr; |
| 493 | |
Heinrich Schuchardt | f727233 | 2019-04-23 00:30:53 +0200 | [diff] [blame] | 494 | /* Check import parameters */ |
| 495 | if (memory_type >= EFI_PERSISTENT_MEMORY_TYPE && |
| 496 | memory_type <= 0x6FFFFFFF) |
| 497 | return EFI_INVALID_PARAMETER; |
Heinrich Schuchardt | a137c96 | 2018-07-02 12:53:53 +0200 | [diff] [blame] | 498 | if (!memory) |
| 499 | return EFI_INVALID_PARAMETER; |
| 500 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 501 | switch (type) { |
Heinrich Schuchardt | 3b7b3b6 | 2018-01-30 21:08:00 +0100 | [diff] [blame] | 502 | case EFI_ALLOCATE_ANY_PAGES: |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 503 | /* Any page */ |
Stephen Warren | 66208b9 | 2018-08-30 15:43:45 -0600 | [diff] [blame] | 504 | addr = efi_find_free_memory(len, -1ULL); |
Heinrich Schuchardt | fbdb93c | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 505 | if (!addr) |
| 506 | return EFI_OUT_OF_RESOURCES; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 507 | break; |
Heinrich Schuchardt | 3b7b3b6 | 2018-01-30 21:08:00 +0100 | [diff] [blame] | 508 | case EFI_ALLOCATE_MAX_ADDRESS: |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 509 | /* Max address */ |
| 510 | addr = efi_find_free_memory(len, *memory); |
Heinrich Schuchardt | fbdb93c | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 511 | if (!addr) |
| 512 | return EFI_OUT_OF_RESOURCES; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 513 | break; |
Heinrich Schuchardt | 3b7b3b6 | 2018-01-30 21:08:00 +0100 | [diff] [blame] | 514 | case EFI_ALLOCATE_ADDRESS: |
Heinrich Schuchardt | 1c180b8 | 2022-11-06 01:52:13 +0100 | [diff] [blame] | 515 | if (*memory & EFI_PAGE_MASK) |
| 516 | return EFI_NOT_FOUND; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 517 | /* Exact address, reserve it. The addr is already in *memory. */ |
Heinrich Schuchardt | fbdb93c | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 518 | ret = efi_check_allocated(*memory, false); |
| 519 | if (ret != EFI_SUCCESS) |
| 520 | return EFI_NOT_FOUND; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 521 | addr = *memory; |
| 522 | break; |
| 523 | default: |
| 524 | /* UEFI doesn't specify other allocation types */ |
Heinrich Schuchardt | fbdb93c | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 525 | return EFI_INVALID_PARAMETER; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 526 | } |
| 527 | |
Heinrich Schuchardt | fbdb93c | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 528 | /* Reserve that map in our memory maps */ |
Michael Walle | 282d386 | 2020-05-17 12:29:19 +0200 | [diff] [blame] | 529 | ret = efi_add_memory_map_pg(addr, pages, memory_type, true); |
| 530 | if (ret != EFI_SUCCESS) |
Heinrich Schuchardt | fbdb93c | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 531 | /* Map would overlap, bail out */ |
| 532 | return EFI_OUT_OF_RESOURCES; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 533 | |
Heinrich Schuchardt | fbdb93c | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 534 | *memory = addr; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 535 | |
Heinrich Schuchardt | fbdb93c | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 536 | return EFI_SUCCESS; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 537 | } |
| 538 | |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 539 | /** |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 540 | * efi_free_pages() - free memory pages |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 541 | * |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 542 | * @memory: start of the memory area to be freed |
| 543 | * @pages: number of pages to be freed |
| 544 | * Return: status code |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 545 | */ |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 546 | efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages) |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 547 | { |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 548 | efi_status_t ret; |
| 549 | |
Heinrich Schuchardt | 906f685 | 2019-05-11 08:21:17 +0200 | [diff] [blame] | 550 | ret = efi_check_allocated(memory, true); |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 551 | if (ret != EFI_SUCCESS) |
| 552 | return ret; |
Stefan Brüns | a4f4302 | 2016-10-01 23:32:27 +0200 | [diff] [blame] | 553 | |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 554 | /* Sanity check */ |
Heinrich Schuchardt | 7bf4bc9 | 2019-04-25 18:41:40 +0200 | [diff] [blame] | 555 | if (!memory || (memory & EFI_PAGE_MASK) || !pages) { |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 556 | printf("%s: illegal free 0x%llx, 0x%zx\n", __func__, |
| 557 | memory, pages); |
| 558 | return EFI_INVALID_PARAMETER; |
| 559 | } |
| 560 | |
Michael Walle | 282d386 | 2020-05-17 12:29:19 +0200 | [diff] [blame] | 561 | ret = efi_add_memory_map_pg(memory, pages, EFI_CONVENTIONAL_MEMORY, |
| 562 | false); |
Bryan O'Donoghue | 89a22b4 | 2019-07-15 12:00:39 +0100 | [diff] [blame] | 563 | if (ret != EFI_SUCCESS) |
| 564 | return EFI_NOT_FOUND; |
Stefan Brüns | a4f4302 | 2016-10-01 23:32:27 +0200 | [diff] [blame] | 565 | |
Bryan O'Donoghue | 89a22b4 | 2019-07-15 12:00:39 +0100 | [diff] [blame] | 566 | return ret; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 567 | } |
| 568 | |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 569 | /** |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 570 | * efi_alloc_aligned_pages() - allocate aligned memory pages |
Ilias Apalodimas | 6e01c23 | 2021-10-11 15:10:23 +0300 | [diff] [blame] | 571 | * |
| 572 | * @len: len in bytes |
| 573 | * @memory_type: usage type of the allocated memory |
| 574 | * @align: alignment in bytes |
| 575 | * Return: aligned memory or NULL |
| 576 | */ |
| 577 | void *efi_alloc_aligned_pages(u64 len, int memory_type, size_t align) |
| 578 | { |
| 579 | u64 req_pages = efi_size_in_pages(len); |
| 580 | u64 true_pages = req_pages + efi_size_in_pages(align) - 1; |
| 581 | u64 free_pages; |
| 582 | u64 aligned_mem; |
| 583 | efi_status_t r; |
| 584 | u64 mem; |
| 585 | |
| 586 | /* align must be zero or a power of two */ |
| 587 | if (align & (align - 1)) |
| 588 | return NULL; |
| 589 | |
| 590 | /* Check for overflow */ |
| 591 | if (true_pages < req_pages) |
| 592 | return NULL; |
| 593 | |
| 594 | if (align < EFI_PAGE_SIZE) { |
| 595 | r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type, |
| 596 | req_pages, &mem); |
| 597 | return (r == EFI_SUCCESS) ? (void *)(uintptr_t)mem : NULL; |
| 598 | } |
| 599 | |
| 600 | r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type, |
| 601 | true_pages, &mem); |
| 602 | if (r != EFI_SUCCESS) |
| 603 | return NULL; |
| 604 | |
| 605 | aligned_mem = ALIGN(mem, align); |
| 606 | /* Free pages before alignment */ |
| 607 | free_pages = efi_size_in_pages(aligned_mem - mem); |
| 608 | if (free_pages) |
| 609 | efi_free_pages(mem, free_pages); |
| 610 | |
| 611 | /* Free trailing pages */ |
| 612 | free_pages = true_pages - (req_pages + free_pages); |
| 613 | if (free_pages) { |
| 614 | mem = aligned_mem + req_pages * EFI_PAGE_SIZE; |
| 615 | efi_free_pages(mem, free_pages); |
| 616 | } |
| 617 | |
| 618 | return (void *)(uintptr_t)aligned_mem; |
| 619 | } |
| 620 | |
| 621 | /** |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 622 | * efi_allocate_pool - allocate memory from pool |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 623 | * |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 624 | * @pool_type: type of the pool from which memory is to be allocated |
| 625 | * @size: number of bytes to be allocated |
| 626 | * @buffer: allocated memory |
| 627 | * Return: status code |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 628 | */ |
Heinrich Schuchardt | 9822894 | 2021-08-17 15:05:31 +0200 | [diff] [blame] | 629 | efi_status_t efi_allocate_pool(enum efi_memory_type pool_type, efi_uintn_t size, void **buffer) |
Stefan Brüns | 5a09aef | 2016-10-09 22:17:18 +0200 | [diff] [blame] | 630 | { |
| 631 | efi_status_t r; |
Heinrich Schuchardt | 657e979 | 2019-03-18 20:01:59 +0100 | [diff] [blame] | 632 | u64 addr; |
Alexander Graf | cd40517 | 2018-06-18 17:23:15 +0200 | [diff] [blame] | 633 | struct efi_pool_allocation *alloc; |
Heinrich Schuchardt | dcdca29 | 2018-11-18 17:58:49 +0100 | [diff] [blame] | 634 | u64 num_pages = efi_size_in_pages(size + |
| 635 | sizeof(struct efi_pool_allocation)); |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 636 | |
Heinrich Schuchardt | a137c96 | 2018-07-02 12:53:53 +0200 | [diff] [blame] | 637 | if (!buffer) |
| 638 | return EFI_INVALID_PARAMETER; |
| 639 | |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 640 | if (size == 0) { |
| 641 | *buffer = NULL; |
| 642 | return EFI_SUCCESS; |
| 643 | } |
Stefan Brüns | 5a09aef | 2016-10-09 22:17:18 +0200 | [diff] [blame] | 644 | |
Heinrich Schuchardt | 0f7186b | 2018-05-26 10:32:27 +0200 | [diff] [blame] | 645 | r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, pool_type, num_pages, |
Heinrich Schuchardt | 657e979 | 2019-03-18 20:01:59 +0100 | [diff] [blame] | 646 | &addr); |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 647 | if (r == EFI_SUCCESS) { |
Heinrich Schuchardt | 657e979 | 2019-03-18 20:01:59 +0100 | [diff] [blame] | 648 | alloc = (struct efi_pool_allocation *)(uintptr_t)addr; |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 649 | alloc->num_pages = num_pages; |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 650 | alloc->checksum = checksum(alloc); |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 651 | *buffer = alloc->data; |
| 652 | } |
| 653 | |
| 654 | return r; |
| 655 | } |
| 656 | |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 657 | /** |
Heinrich Schuchardt | 4564025 | 2023-03-19 09:20:22 +0100 | [diff] [blame] | 658 | * efi_alloc() - allocate boot services data pool memory |
| 659 | * |
| 660 | * Allocate memory from pool and zero it out. |
| 661 | * |
| 662 | * @size: number of bytes to allocate |
| 663 | * Return: pointer to allocated memory or NULL |
| 664 | */ |
| 665 | void *efi_alloc(size_t size) |
| 666 | { |
| 667 | void *buf; |
| 668 | |
| 669 | if (efi_allocate_pool(EFI_BOOT_SERVICES_DATA, size, &buf) != |
| 670 | EFI_SUCCESS) { |
| 671 | log_err("out of memory"); |
| 672 | return NULL; |
| 673 | } |
| 674 | memset(buf, 0, size); |
| 675 | |
| 676 | return buf; |
| 677 | } |
| 678 | |
| 679 | /** |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 680 | * efi_free_pool() - free memory from pool |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 681 | * |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 682 | * @buffer: start of memory to be freed |
| 683 | * Return: status code |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 684 | */ |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 685 | efi_status_t efi_free_pool(void *buffer) |
| 686 | { |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 687 | efi_status_t ret; |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 688 | struct efi_pool_allocation *alloc; |
| 689 | |
Heinrich Schuchardt | 56b587f7 | 2019-06-12 07:17:04 +0200 | [diff] [blame] | 690 | if (!buffer) |
| 691 | return EFI_INVALID_PARAMETER; |
| 692 | |
Heinrich Schuchardt | 906f685 | 2019-05-11 08:21:17 +0200 | [diff] [blame] | 693 | ret = efi_check_allocated((uintptr_t)buffer, true); |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 694 | if (ret != EFI_SUCCESS) |
| 695 | return ret; |
xypron.glpk@gmx.de | 34ddae7 | 2017-07-14 19:12:39 +0200 | [diff] [blame] | 696 | |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 697 | alloc = container_of(buffer, struct efi_pool_allocation, data); |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 698 | |
| 699 | /* Check that this memory was allocated by efi_allocate_pool() */ |
| 700 | if (((uintptr_t)alloc & EFI_PAGE_MASK) || |
| 701 | alloc->checksum != checksum(alloc)) { |
| 702 | printf("%s: illegal free 0x%p\n", __func__, buffer); |
| 703 | return EFI_INVALID_PARAMETER; |
| 704 | } |
| 705 | /* Avoid double free */ |
| 706 | alloc->checksum = 0; |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 707 | |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 708 | ret = efi_free_pages((uintptr_t)alloc, alloc->num_pages); |
Stefan Brüns | 5a09aef | 2016-10-09 22:17:18 +0200 | [diff] [blame] | 709 | |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 710 | return ret; |
Stefan Brüns | 5a09aef | 2016-10-09 22:17:18 +0200 | [diff] [blame] | 711 | } |
| 712 | |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 713 | /** |
| 714 | * efi_get_memory_map() - get map describing memory usage. |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 715 | * |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 716 | * @memory_map_size: on entry the size, in bytes, of the memory map buffer, |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 717 | * on exit the size of the copied memory map |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 718 | * @memory_map: buffer to which the memory map is written |
| 719 | * @map_key: key for the memory map |
| 720 | * @descriptor_size: size of an individual memory descriptor |
| 721 | * @descriptor_version: version number of the memory descriptor structure |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 722 | * Return: status code |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 723 | */ |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 724 | efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size, |
| 725 | struct efi_mem_desc *memory_map, |
| 726 | efi_uintn_t *map_key, |
| 727 | efi_uintn_t *descriptor_size, |
| 728 | uint32_t *descriptor_version) |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 729 | { |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 730 | efi_uintn_t map_size = 0; |
Alexander Graf | 9b59102 | 2016-04-11 23:51:02 +0200 | [diff] [blame] | 731 | int map_entries = 0; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 732 | struct list_head *lhandle; |
Heinrich Schuchardt | 9b16646 | 2018-08-06 22:28:18 +0200 | [diff] [blame] | 733 | efi_uintn_t provided_map_size; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 734 | |
Heinrich Schuchardt | e1a1a09 | 2018-07-02 12:53:54 +0200 | [diff] [blame] | 735 | if (!memory_map_size) |
| 736 | return EFI_INVALID_PARAMETER; |
| 737 | |
Heinrich Schuchardt | 9b16646 | 2018-08-06 22:28:18 +0200 | [diff] [blame] | 738 | provided_map_size = *memory_map_size; |
| 739 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 740 | list_for_each(lhandle, &efi_mem) |
Alexander Graf | 9b59102 | 2016-04-11 23:51:02 +0200 | [diff] [blame] | 741 | map_entries++; |
| 742 | |
| 743 | map_size = map_entries * sizeof(struct efi_mem_desc); |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 744 | |
Rob Clark | a84c974 | 2017-07-26 14:34:05 -0400 | [diff] [blame] | 745 | *memory_map_size = map_size; |
| 746 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 747 | if (descriptor_size) |
| 748 | *descriptor_size = sizeof(struct efi_mem_desc); |
| 749 | |
Mian Yousaf Kaukab | 338cf56 | 2016-09-05 23:59:22 +0200 | [diff] [blame] | 750 | if (descriptor_version) |
| 751 | *descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION; |
| 752 | |
AKASHI Takahiro | 3ee138d | 2020-03-11 15:18:18 +0900 | [diff] [blame] | 753 | if (provided_map_size < map_size) |
| 754 | return EFI_BUFFER_TOO_SMALL; |
| 755 | |
| 756 | if (!memory_map) |
| 757 | return EFI_INVALID_PARAMETER; |
| 758 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 759 | /* Copy list into array */ |
Heinrich Schuchardt | e1a1a09 | 2018-07-02 12:53:54 +0200 | [diff] [blame] | 760 | /* Return the list in ascending order */ |
| 761 | memory_map = &memory_map[map_entries - 1]; |
| 762 | list_for_each(lhandle, &efi_mem) { |
| 763 | struct efi_mem_list *lmem; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 764 | |
Heinrich Schuchardt | e1a1a09 | 2018-07-02 12:53:54 +0200 | [diff] [blame] | 765 | lmem = list_entry(lhandle, struct efi_mem_list, link); |
| 766 | *memory_map = lmem->desc; |
| 767 | memory_map--; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 768 | } |
| 769 | |
Heinrich Schuchardt | e1a1a09 | 2018-07-02 12:53:54 +0200 | [diff] [blame] | 770 | if (map_key) |
Heinrich Schuchardt | 0f233c4 | 2018-07-02 12:53:55 +0200 | [diff] [blame] | 771 | *map_key = efi_memory_map_key; |
xypron.glpk@gmx.de | e436e63 | 2017-07-21 19:05:44 +0200 | [diff] [blame] | 772 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 773 | return EFI_SUCCESS; |
| 774 | } |
| 775 | |
Park, Aiden | e009e7a | 2019-09-03 17:43:43 +0000 | [diff] [blame] | 776 | /** |
Heinrich Schuchardt | 13ce2d9 | 2023-01-05 18:26:01 +0100 | [diff] [blame] | 777 | * efi_get_memory_map_alloc() - allocate map describing memory usage |
| 778 | * |
| 779 | * The caller is responsible for calling FreePool() if the call succeeds. |
| 780 | * |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 781 | * @map_size: size of the memory map |
| 782 | * @memory_map: buffer to which the memory map is written |
Heinrich Schuchardt | 13ce2d9 | 2023-01-05 18:26:01 +0100 | [diff] [blame] | 783 | * Return: status code |
| 784 | */ |
| 785 | efi_status_t efi_get_memory_map_alloc(efi_uintn_t *map_size, |
| 786 | struct efi_mem_desc **memory_map) |
| 787 | { |
| 788 | efi_status_t ret; |
| 789 | |
| 790 | *memory_map = NULL; |
| 791 | *map_size = 0; |
| 792 | ret = efi_get_memory_map(map_size, *memory_map, NULL, NULL, NULL); |
| 793 | if (ret == EFI_BUFFER_TOO_SMALL) { |
| 794 | *map_size += sizeof(struct efi_mem_desc); /* for the map */ |
| 795 | ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, *map_size, |
| 796 | (void **)memory_map); |
| 797 | if (ret != EFI_SUCCESS) |
| 798 | return ret; |
| 799 | ret = efi_get_memory_map(map_size, *memory_map, |
| 800 | NULL, NULL, NULL); |
| 801 | if (ret != EFI_SUCCESS) { |
| 802 | efi_free_pool(*memory_map); |
| 803 | *memory_map = NULL; |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | return ret; |
| 808 | } |
| 809 | |
| 810 | /** |
Park, Aiden | e009e7a | 2019-09-03 17:43:43 +0000 | [diff] [blame] | 811 | * efi_add_conventional_memory_map() - add a RAM memory area to the map |
| 812 | * |
| 813 | * @ram_start: start address of a RAM memory area |
| 814 | * @ram_end: end address of a RAM memory area |
| 815 | * @ram_top: max address to be used as conventional memory |
| 816 | * Return: status code |
| 817 | */ |
| 818 | efi_status_t efi_add_conventional_memory_map(u64 ram_start, u64 ram_end, |
| 819 | u64 ram_top) |
| 820 | { |
| 821 | u64 pages; |
| 822 | |
| 823 | /* Remove partial pages */ |
| 824 | ram_end &= ~EFI_PAGE_MASK; |
| 825 | ram_start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK; |
| 826 | |
| 827 | if (ram_end <= ram_start) { |
| 828 | /* Invalid mapping */ |
| 829 | return EFI_INVALID_PARAMETER; |
| 830 | } |
| 831 | |
| 832 | pages = (ram_end - ram_start) >> EFI_PAGE_SHIFT; |
| 833 | |
Michael Walle | 282d386 | 2020-05-17 12:29:19 +0200 | [diff] [blame] | 834 | efi_add_memory_map_pg(ram_start, pages, |
| 835 | EFI_CONVENTIONAL_MEMORY, false); |
Park, Aiden | e009e7a | 2019-09-03 17:43:43 +0000 | [diff] [blame] | 836 | |
| 837 | /* |
| 838 | * Boards may indicate to the U-Boot memory core that they |
| 839 | * can not support memory above ram_top. Let's honor this |
| 840 | * in the efi_loader subsystem too by declaring any memory |
| 841 | * above ram_top as "already occupied by firmware". |
| 842 | */ |
| 843 | if (ram_top < ram_start) { |
| 844 | /* ram_top is before this region, reserve all */ |
Michael Walle | 282d386 | 2020-05-17 12:29:19 +0200 | [diff] [blame] | 845 | efi_add_memory_map_pg(ram_start, pages, |
| 846 | EFI_BOOT_SERVICES_DATA, true); |
Heinrich Schuchardt | 3546eb2 | 2022-04-25 23:54:48 +0200 | [diff] [blame] | 847 | } else if (ram_top < ram_end) { |
Park, Aiden | e009e7a | 2019-09-03 17:43:43 +0000 | [diff] [blame] | 848 | /* ram_top is inside this region, reserve parts */ |
| 849 | pages = (ram_end - ram_top) >> EFI_PAGE_SHIFT; |
| 850 | |
Michael Walle | 282d386 | 2020-05-17 12:29:19 +0200 | [diff] [blame] | 851 | efi_add_memory_map_pg(ram_top, pages, |
| 852 | EFI_BOOT_SERVICES_DATA, true); |
Park, Aiden | e009e7a | 2019-09-03 17:43:43 +0000 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | return EFI_SUCCESS; |
| 856 | } |
| 857 | |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 858 | /** |
| 859 | * efi_add_known_memory() - add memory banks to map |
| 860 | * |
| 861 | * This function may be overridden for specific architectures. |
| 862 | */ |
York Sun | a8e9f0a | 2017-03-06 09:02:27 -0800 | [diff] [blame] | 863 | __weak void efi_add_known_memory(void) |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 864 | { |
Alexander Graf | c214edf | 2018-11-30 21:24:56 +0100 | [diff] [blame] | 865 | u64 ram_top = board_get_usable_ram_top(0) & ~EFI_PAGE_MASK; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 866 | int i; |
| 867 | |
Heinrich Schuchardt | eb8edcf | 2019-01-05 23:41:36 +0100 | [diff] [blame] | 868 | /* |
| 869 | * ram_top is just outside mapped memory. So use an offset of one for |
| 870 | * mapping the sandbox address. |
| 871 | */ |
| 872 | ram_top = (uintptr_t)map_sysmem(ram_top - 1, 0) + 1; |
| 873 | |
Alexander Graf | c214edf | 2018-11-30 21:24:56 +0100 | [diff] [blame] | 874 | /* Fix for 32bit targets with ram_top at 4G */ |
| 875 | if (!ram_top) |
| 876 | ram_top = 0x100000000ULL; |
| 877 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 878 | /* Add RAM */ |
| 879 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { |
Park, Aiden | e009e7a | 2019-09-03 17:43:43 +0000 | [diff] [blame] | 880 | u64 ram_end, ram_start; |
Heinrich Schuchardt | 084091a | 2018-11-12 18:55:24 +0100 | [diff] [blame] | 881 | |
Heinrich Schuchardt | 6738fcd | 2018-11-18 17:58:46 +0100 | [diff] [blame] | 882 | ram_start = (uintptr_t)map_sysmem(gd->bd->bi_dram[i].start, 0); |
Heinrich Schuchardt | 084091a | 2018-11-12 18:55:24 +0100 | [diff] [blame] | 883 | ram_end = ram_start + gd->bd->bi_dram[i].size; |
| 884 | |
Park, Aiden | e009e7a | 2019-09-03 17:43:43 +0000 | [diff] [blame] | 885 | efi_add_conventional_memory_map(ram_start, ram_end, ram_top); |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 886 | } |
York Sun | a8e9f0a | 2017-03-06 09:02:27 -0800 | [diff] [blame] | 887 | } |
| 888 | |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 889 | /** |
| 890 | * add_u_boot_and_runtime() - add U-Boot code to memory map |
| 891 | * |
| 892 | * Add memory regions for U-Boot's memory and for the runtime services code. |
| 893 | */ |
Simon Glass | 7863943 | 2018-06-18 17:23:12 +0200 | [diff] [blame] | 894 | static void add_u_boot_and_runtime(void) |
York Sun | a8e9f0a | 2017-03-06 09:02:27 -0800 | [diff] [blame] | 895 | { |
| 896 | unsigned long runtime_start, runtime_end, runtime_pages; |
Alexander Graf | 15937a2 | 2018-09-17 13:54:33 +0200 | [diff] [blame] | 897 | unsigned long runtime_mask = EFI_PAGE_MASK; |
York Sun | a8e9f0a | 2017-03-06 09:02:27 -0800 | [diff] [blame] | 898 | unsigned long uboot_start, uboot_pages; |
Heinrich Schuchardt | c6dcbe1 | 2020-07-29 12:37:35 +0200 | [diff] [blame] | 899 | unsigned long uboot_stack_size = CONFIG_STACK_SIZE; |
York Sun | a8e9f0a | 2017-03-06 09:02:27 -0800 | [diff] [blame] | 900 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 901 | /* Add U-Boot */ |
Heinrich Schuchardt | a69976e | 2019-11-08 20:42:53 +0100 | [diff] [blame] | 902 | uboot_start = ((uintptr_t)map_sysmem(gd->start_addr_sp, 0) - |
| 903 | uboot_stack_size) & ~EFI_PAGE_MASK; |
| 904 | uboot_pages = ((uintptr_t)map_sysmem(gd->ram_top - 1, 0) - |
| 905 | uboot_start + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT; |
Heinrich Schuchardt | 296b55c | 2022-11-29 16:00:41 +0100 | [diff] [blame] | 906 | efi_add_memory_map_pg(uboot_start, uboot_pages, EFI_BOOT_SERVICES_CODE, |
Michael Walle | 282d386 | 2020-05-17 12:29:19 +0200 | [diff] [blame] | 907 | false); |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 908 | |
Alexander Graf | 15937a2 | 2018-09-17 13:54:33 +0200 | [diff] [blame] | 909 | #if defined(__aarch64__) |
| 910 | /* |
| 911 | * Runtime Services must be 64KiB aligned according to the |
| 912 | * "AArch64 Platforms" section in the UEFI spec (2.7+). |
| 913 | */ |
| 914 | |
| 915 | runtime_mask = SZ_64K - 1; |
| 916 | #endif |
| 917 | |
| 918 | /* |
| 919 | * Add Runtime Services. We mark surrounding boottime code as runtime as |
| 920 | * well to fulfill the runtime alignment constraints but avoid padding. |
| 921 | */ |
| 922 | runtime_start = (ulong)&__efi_runtime_start & ~runtime_mask; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 923 | runtime_end = (ulong)&__efi_runtime_stop; |
Alexander Graf | 15937a2 | 2018-09-17 13:54:33 +0200 | [diff] [blame] | 924 | runtime_end = (runtime_end + runtime_mask) & ~runtime_mask; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 925 | runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT; |
Michael Walle | 282d386 | 2020-05-17 12:29:19 +0200 | [diff] [blame] | 926 | efi_add_memory_map_pg(runtime_start, runtime_pages, |
| 927 | EFI_RUNTIME_SERVICES_CODE, false); |
Simon Glass | 7863943 | 2018-06-18 17:23:12 +0200 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | int efi_memory_init(void) |
| 931 | { |
| 932 | efi_add_known_memory(); |
| 933 | |
Heinrich Schuchardt | a69976e | 2019-11-08 20:42:53 +0100 | [diff] [blame] | 934 | add_u_boot_and_runtime(); |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 935 | |
Alexander Graf | 7c00a3c | 2016-05-11 18:25:48 +0200 | [diff] [blame] | 936 | #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER |
| 937 | /* Request a 32bit 64MB bounce buffer region */ |
| 938 | uint64_t efi_bounce_buffer_addr = 0xffffffff; |
| 939 | |
Heinrich Schuchardt | 296b55c | 2022-11-29 16:00:41 +0100 | [diff] [blame] | 940 | if (efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, EFI_BOOT_SERVICES_DATA, |
Alexander Graf | 7c00a3c | 2016-05-11 18:25:48 +0200 | [diff] [blame] | 941 | (64 * 1024 * 1024) >> EFI_PAGE_SHIFT, |
| 942 | &efi_bounce_buffer_addr) != EFI_SUCCESS) |
| 943 | return -1; |
| 944 | |
| 945 | efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr; |
| 946 | #endif |
| 947 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 948 | return 0; |
| 949 | } |