1 /* 2 * EFI application memory management 3 * 4 * Copyright (c) 2016 Alexander Graf 5 * 6 * SPDX-License-Identifier: GPL-2.0+ 7 */ 8 9 #include <common.h> 10 #include <efi_loader.h> 11 #include <malloc.h> 12 #include <asm/global_data.h> 13 #include <libfdt_env.h> 14 #include <linux/list_sort.h> 15 #include <inttypes.h> 16 #include <watchdog.h> 17 18 DECLARE_GLOBAL_DATA_PTR; 19 20 struct efi_mem_list { 21 struct list_head link; 22 struct efi_mem_desc desc; 23 }; 24 25 #define EFI_CARVE_NO_OVERLAP -1 26 #define EFI_CARVE_LOOP_AGAIN -2 27 #define EFI_CARVE_OVERLAPS_NONRAM -3 28 29 /* This list contains all memory map items */ 30 LIST_HEAD(efi_mem); 31 32 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER 33 void *efi_bounce_buffer; 34 #endif 35 36 /* 37 * U-Boot services each EFI AllocatePool request as a separate 38 * (multiple) page allocation. We have to track the number of pages 39 * to be able to free the correct amount later. 40 * EFI requires 8 byte alignment for pool allocations, so we can 41 * prepend each allocation with an 64 bit header tracking the 42 * allocation size, and hand out the remainder to the caller. 43 */ 44 struct efi_pool_allocation { 45 u64 num_pages; 46 char data[]; 47 }; 48 49 /* 50 * Sorts the memory list from highest address to lowest address 51 * 52 * When allocating memory we should always start from the highest 53 * address chunk, so sort the memory list such that the first list 54 * iterator gets the highest address and goes lower from there. 55 */ 56 static int efi_mem_cmp(void *priv, struct list_head *a, struct list_head *b) 57 { 58 struct efi_mem_list *mema = list_entry(a, struct efi_mem_list, link); 59 struct efi_mem_list *memb = list_entry(b, struct efi_mem_list, link); 60 61 if (mema->desc.physical_start == memb->desc.physical_start) 62 return 0; 63 else if (mema->desc.physical_start < memb->desc.physical_start) 64 return 1; 65 else 66 return -1; 67 } 68 69 static void efi_mem_sort(void) 70 { 71 list_sort(NULL, &efi_mem, efi_mem_cmp); 72 } 73 74 /* 75 * Unmaps all memory occupied by the carve_desc region from the 76 * list entry pointed to by map. 77 * 78 * Returns EFI_CARVE_NO_OVERLAP if the regions don't overlap. 79 * Returns EFI_CARVE_OVERLAPS_NONRAM if the carve and map overlap, 80 * and the map contains anything but free ram. 81 * (only when overlap_only_ram is true) 82 * Returns EFI_CARVE_LOOP_AGAIN if the mapping list should be traversed 83 * again, as it has been altered 84 * Returns the number of overlapping pages. The pages are removed from 85 * the mapping list. 86 * 87 * In case of EFI_CARVE_OVERLAPS_NONRAM it is the callers responsibility 88 * to readd the already carved out pages to the mapping. 89 */ 90 static int efi_mem_carve_out(struct efi_mem_list *map, 91 struct efi_mem_desc *carve_desc, 92 bool overlap_only_ram) 93 { 94 struct efi_mem_list *newmap; 95 struct efi_mem_desc *map_desc = &map->desc; 96 uint64_t map_start = map_desc->physical_start; 97 uint64_t map_end = map_start + (map_desc->num_pages << EFI_PAGE_SHIFT); 98 uint64_t carve_start = carve_desc->physical_start; 99 uint64_t carve_end = carve_start + 100 (carve_desc->num_pages << EFI_PAGE_SHIFT); 101 102 /* check whether we're overlapping */ 103 if ((carve_end <= map_start) || (carve_start >= map_end)) 104 return EFI_CARVE_NO_OVERLAP; 105 106 /* We're overlapping with non-RAM, warn the caller if desired */ 107 if (overlap_only_ram && (map_desc->type != EFI_CONVENTIONAL_MEMORY)) 108 return EFI_CARVE_OVERLAPS_NONRAM; 109 110 /* Sanitize carve_start and carve_end to lie within our bounds */ 111 carve_start = max(carve_start, map_start); 112 carve_end = min(carve_end, map_end); 113 114 /* Carving at the beginning of our map? Just move it! */ 115 if (carve_start == map_start) { 116 if (map_end == carve_end) { 117 /* Full overlap, just remove map */ 118 list_del(&map->link); 119 } 120 121 map_desc->physical_start = carve_end; 122 map_desc->num_pages = (map_end - carve_end) >> EFI_PAGE_SHIFT; 123 return (carve_end - carve_start) >> EFI_PAGE_SHIFT; 124 } 125 126 /* 127 * Overlapping maps, just split the list map at carve_start, 128 * it will get moved or removed in the next iteration. 129 * 130 * [ map_desc |__carve_start__| newmap ] 131 */ 132 133 /* Create a new map from [ carve_start ... map_end ] */ 134 newmap = calloc(1, sizeof(*newmap)); 135 newmap->desc = map->desc; 136 newmap->desc.physical_start = carve_start; 137 newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT; 138 list_add_tail(&newmap->link, &efi_mem); 139 140 /* Shrink the map to [ map_start ... carve_start ] */ 141 map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT; 142 143 return EFI_CARVE_LOOP_AGAIN; 144 } 145 146 uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type, 147 bool overlap_only_ram) 148 { 149 struct list_head *lhandle; 150 struct efi_mem_list *newlist; 151 bool carve_again; 152 uint64_t carved_pages = 0; 153 154 debug("%s: 0x%" PRIx64 " 0x%" PRIx64 " %d %s\n", __func__, 155 start, pages, memory_type, overlap_only_ram ? "yes" : "no"); 156 157 if (!pages) 158 return start; 159 160 newlist = calloc(1, sizeof(*newlist)); 161 newlist->desc.type = memory_type; 162 newlist->desc.physical_start = start; 163 newlist->desc.virtual_start = start; 164 newlist->desc.num_pages = pages; 165 166 switch (memory_type) { 167 case EFI_RUNTIME_SERVICES_CODE: 168 case EFI_RUNTIME_SERVICES_DATA: 169 newlist->desc.attribute = (1 << EFI_MEMORY_WB_SHIFT) | 170 (1ULL << EFI_MEMORY_RUNTIME_SHIFT); 171 break; 172 case EFI_MMAP_IO: 173 newlist->desc.attribute = 1ULL << EFI_MEMORY_RUNTIME_SHIFT; 174 break; 175 default: 176 newlist->desc.attribute = 1 << EFI_MEMORY_WB_SHIFT; 177 break; 178 } 179 180 /* Add our new map */ 181 do { 182 carve_again = false; 183 list_for_each(lhandle, &efi_mem) { 184 struct efi_mem_list *lmem; 185 int r; 186 187 lmem = list_entry(lhandle, struct efi_mem_list, link); 188 r = efi_mem_carve_out(lmem, &newlist->desc, 189 overlap_only_ram); 190 switch (r) { 191 case EFI_CARVE_OVERLAPS_NONRAM: 192 /* 193 * The user requested to only have RAM overlaps, 194 * but we hit a non-RAM region. Error out. 195 */ 196 return 0; 197 case EFI_CARVE_NO_OVERLAP: 198 /* Just ignore this list entry */ 199 break; 200 case EFI_CARVE_LOOP_AGAIN: 201 /* 202 * We split an entry, but need to loop through 203 * the list again to actually carve it. 204 */ 205 carve_again = true; 206 break; 207 default: 208 /* We carved a number of pages */ 209 carved_pages += r; 210 carve_again = true; 211 break; 212 } 213 214 if (carve_again) { 215 /* The list changed, we need to start over */ 216 break; 217 } 218 } 219 } while (carve_again); 220 221 if (overlap_only_ram && (carved_pages != pages)) { 222 /* 223 * The payload wanted to have RAM overlaps, but we overlapped 224 * with an unallocated region. Error out. 225 */ 226 return 0; 227 } 228 229 /* Add our new map */ 230 list_add_tail(&newlist->link, &efi_mem); 231 232 /* And make sure memory is listed in descending order */ 233 efi_mem_sort(); 234 235 return start; 236 } 237 238 static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr) 239 { 240 struct list_head *lhandle; 241 242 list_for_each(lhandle, &efi_mem) { 243 struct efi_mem_list *lmem = list_entry(lhandle, 244 struct efi_mem_list, link); 245 struct efi_mem_desc *desc = &lmem->desc; 246 uint64_t desc_len = desc->num_pages << EFI_PAGE_SHIFT; 247 uint64_t desc_end = desc->physical_start + desc_len; 248 uint64_t curmax = min(max_addr, desc_end); 249 uint64_t ret = curmax - len; 250 251 /* We only take memory from free RAM */ 252 if (desc->type != EFI_CONVENTIONAL_MEMORY) 253 continue; 254 255 /* Out of bounds for max_addr */ 256 if ((ret + len) > max_addr) 257 continue; 258 259 /* Out of bounds for upper map limit */ 260 if ((ret + len) > desc_end) 261 continue; 262 263 /* Out of bounds for lower map limit */ 264 if (ret < desc->physical_start) 265 continue; 266 267 /* Return the highest address in this map within bounds */ 268 return ret; 269 } 270 271 return 0; 272 } 273 274 efi_status_t efi_allocate_pages(int type, int memory_type, 275 unsigned long pages, uint64_t *memory) 276 { 277 u64 len = pages << EFI_PAGE_SHIFT; 278 efi_status_t r = EFI_SUCCESS; 279 uint64_t addr; 280 281 switch (type) { 282 case 0: 283 /* Any page */ 284 addr = efi_find_free_memory(len, gd->start_addr_sp); 285 if (!addr) { 286 r = EFI_NOT_FOUND; 287 break; 288 } 289 break; 290 case 1: 291 /* Max address */ 292 addr = efi_find_free_memory(len, *memory); 293 if (!addr) { 294 r = EFI_NOT_FOUND; 295 break; 296 } 297 break; 298 case 2: 299 /* Exact address, reserve it. The addr is already in *memory. */ 300 addr = *memory; 301 break; 302 default: 303 /* UEFI doesn't specify other allocation types */ 304 r = EFI_INVALID_PARAMETER; 305 break; 306 } 307 308 if (r == EFI_SUCCESS) { 309 uint64_t ret; 310 311 /* Reserve that map in our memory maps */ 312 ret = efi_add_memory_map(addr, pages, memory_type, true); 313 if (ret == addr) { 314 *memory = addr; 315 } else { 316 /* Map would overlap, bail out */ 317 r = EFI_OUT_OF_RESOURCES; 318 } 319 } 320 321 return r; 322 } 323 324 void *efi_alloc(uint64_t len, int memory_type) 325 { 326 uint64_t ret = 0; 327 uint64_t pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT; 328 efi_status_t r; 329 330 r = efi_allocate_pages(0, memory_type, pages, &ret); 331 if (r == EFI_SUCCESS) 332 return (void*)(uintptr_t)ret; 333 334 return NULL; 335 } 336 337 efi_status_t efi_free_pages(uint64_t memory, unsigned long pages) 338 { 339 /* We don't free, let's cross our fingers we have plenty RAM */ 340 return EFI_SUCCESS; 341 } 342 343 efi_status_t efi_allocate_pool(int pool_type, unsigned long size, 344 void **buffer) 345 { 346 efi_status_t r; 347 efi_physical_addr_t t; 348 u64 num_pages = (size + sizeof(u64) + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT; 349 350 if (size == 0) { 351 *buffer = NULL; 352 return EFI_SUCCESS; 353 } 354 355 r = efi_allocate_pages(0, pool_type, num_pages, &t); 356 357 if (r == EFI_SUCCESS) { 358 struct efi_pool_allocation *alloc = (void *)(uintptr_t)t; 359 alloc->num_pages = num_pages; 360 *buffer = alloc->data; 361 } 362 363 return r; 364 } 365 366 efi_status_t efi_free_pool(void *buffer) 367 { 368 efi_status_t r; 369 struct efi_pool_allocation *alloc; 370 371 alloc = container_of(buffer, struct efi_pool_allocation, data); 372 /* Sanity check, was the supplied address returned by allocate_pool */ 373 assert(((uintptr_t)alloc & EFI_PAGE_MASK) == 0); 374 375 r = efi_free_pages((uintptr_t)alloc, alloc->num_pages); 376 377 return r; 378 } 379 380 efi_status_t efi_get_memory_map(unsigned long *memory_map_size, 381 struct efi_mem_desc *memory_map, 382 unsigned long *map_key, 383 unsigned long *descriptor_size, 384 uint32_t *descriptor_version) 385 { 386 ulong map_size = 0; 387 int map_entries = 0; 388 struct list_head *lhandle; 389 unsigned long provided_map_size = *memory_map_size; 390 391 list_for_each(lhandle, &efi_mem) 392 map_entries++; 393 394 map_size = map_entries * sizeof(struct efi_mem_desc); 395 396 *memory_map_size = map_size; 397 398 if (descriptor_size) 399 *descriptor_size = sizeof(struct efi_mem_desc); 400 401 if (descriptor_version) 402 *descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION; 403 404 if (provided_map_size < map_size) 405 return EFI_BUFFER_TOO_SMALL; 406 407 /* Copy list into array */ 408 if (memory_map) { 409 /* Return the list in ascending order */ 410 memory_map = &memory_map[map_entries - 1]; 411 list_for_each(lhandle, &efi_mem) { 412 struct efi_mem_list *lmem; 413 414 lmem = list_entry(lhandle, struct efi_mem_list, link); 415 *memory_map = lmem->desc; 416 memory_map--; 417 } 418 } 419 420 return EFI_SUCCESS; 421 } 422 423 int efi_memory_init(void) 424 { 425 unsigned long runtime_start, runtime_end, runtime_pages; 426 unsigned long uboot_start, uboot_pages; 427 unsigned long uboot_stack_size = 16 * 1024 * 1024; 428 int i; 429 430 /* Add RAM */ 431 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { 432 u64 ram_start = gd->bd->bi_dram[i].start; 433 u64 ram_size = gd->bd->bi_dram[i].size; 434 u64 start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK; 435 u64 pages = (ram_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT; 436 437 efi_add_memory_map(start, pages, EFI_CONVENTIONAL_MEMORY, 438 false); 439 } 440 441 /* Add U-Boot */ 442 uboot_start = (gd->start_addr_sp - uboot_stack_size) & ~EFI_PAGE_MASK; 443 uboot_pages = (gd->ram_top - uboot_start) >> EFI_PAGE_SHIFT; 444 efi_add_memory_map(uboot_start, uboot_pages, EFI_LOADER_DATA, false); 445 446 /* Add Runtime Services */ 447 runtime_start = (ulong)&__efi_runtime_start & ~EFI_PAGE_MASK; 448 runtime_end = (ulong)&__efi_runtime_stop; 449 runtime_end = (runtime_end + EFI_PAGE_MASK) & ~EFI_PAGE_MASK; 450 runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT; 451 efi_add_memory_map(runtime_start, runtime_pages, 452 EFI_RUNTIME_SERVICES_CODE, false); 453 454 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER 455 /* Request a 32bit 64MB bounce buffer region */ 456 uint64_t efi_bounce_buffer_addr = 0xffffffff; 457 458 if (efi_allocate_pages(1, EFI_LOADER_DATA, 459 (64 * 1024 * 1024) >> EFI_PAGE_SHIFT, 460 &efi_bounce_buffer_addr) != EFI_SUCCESS) 461 return -1; 462 463 efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr; 464 #endif 465 466 return 0; 467 } 468