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