15d00995cSAlexander Graf /* 25d00995cSAlexander Graf * EFI application memory management 35d00995cSAlexander Graf * 45d00995cSAlexander Graf * Copyright (c) 2016 Alexander Graf 55d00995cSAlexander Graf * 65d00995cSAlexander Graf * SPDX-License-Identifier: GPL-2.0+ 75d00995cSAlexander Graf */ 85d00995cSAlexander Graf 95d00995cSAlexander Graf /* #define DEBUG_EFI */ 105d00995cSAlexander Graf 115d00995cSAlexander Graf #include <common.h> 125d00995cSAlexander Graf #include <efi_loader.h> 135d00995cSAlexander Graf #include <malloc.h> 145d00995cSAlexander Graf #include <asm/global_data.h> 155d00995cSAlexander Graf #include <libfdt_env.h> 1638ce65e1SAlexander Graf #include <linux/list_sort.h> 175d00995cSAlexander Graf #include <inttypes.h> 185d00995cSAlexander Graf #include <watchdog.h> 195d00995cSAlexander Graf 205d00995cSAlexander Graf DECLARE_GLOBAL_DATA_PTR; 215d00995cSAlexander Graf 225d00995cSAlexander Graf struct efi_mem_list { 235d00995cSAlexander Graf struct list_head link; 245d00995cSAlexander Graf struct efi_mem_desc desc; 255d00995cSAlexander Graf }; 265d00995cSAlexander Graf 275d00995cSAlexander Graf /* This list contains all memory map items */ 285d00995cSAlexander Graf LIST_HEAD(efi_mem); 295d00995cSAlexander Graf 30*51735ae0SAlexander Graf #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER 31*51735ae0SAlexander Graf void *efi_bounce_buffer; 32*51735ae0SAlexander Graf #endif 33*51735ae0SAlexander Graf 345d00995cSAlexander Graf /* 3538ce65e1SAlexander Graf * Sorts the memory list from highest address to lowest address 3638ce65e1SAlexander Graf * 3738ce65e1SAlexander Graf * When allocating memory we should always start from the highest 3838ce65e1SAlexander Graf * address chunk, so sort the memory list such that the first list 3938ce65e1SAlexander Graf * iterator gets the highest address and goes lower from there. 4038ce65e1SAlexander Graf */ 4138ce65e1SAlexander Graf static int efi_mem_cmp(void *priv, struct list_head *a, struct list_head *b) 4238ce65e1SAlexander Graf { 4338ce65e1SAlexander Graf struct efi_mem_list *mema = list_entry(a, struct efi_mem_list, link); 4438ce65e1SAlexander Graf struct efi_mem_list *memb = list_entry(b, struct efi_mem_list, link); 4538ce65e1SAlexander Graf 4638ce65e1SAlexander Graf if (mema->desc.physical_start == memb->desc.physical_start) 4738ce65e1SAlexander Graf return 0; 4838ce65e1SAlexander Graf else if (mema->desc.physical_start < memb->desc.physical_start) 4938ce65e1SAlexander Graf return 1; 5038ce65e1SAlexander Graf else 5138ce65e1SAlexander Graf return -1; 5238ce65e1SAlexander Graf } 5338ce65e1SAlexander Graf 5438ce65e1SAlexander Graf static void efi_mem_sort(void) 5538ce65e1SAlexander Graf { 5638ce65e1SAlexander Graf list_sort(NULL, &efi_mem, efi_mem_cmp); 5738ce65e1SAlexander Graf } 5838ce65e1SAlexander Graf 5938ce65e1SAlexander Graf /* 605d00995cSAlexander Graf * Unmaps all memory occupied by the carve_desc region from the 615d00995cSAlexander Graf * list entry pointed to by map. 625d00995cSAlexander Graf * 635d00995cSAlexander Graf * Returns 1 if carving was performed or 0 if the regions don't overlap. 645d00995cSAlexander Graf * Returns -1 if it would affect non-RAM regions but overlap_only_ram is set. 655d00995cSAlexander Graf * Carving is only guaranteed to complete when all regions return 0. 665d00995cSAlexander Graf */ 675d00995cSAlexander Graf static int efi_mem_carve_out(struct efi_mem_list *map, 685d00995cSAlexander Graf struct efi_mem_desc *carve_desc, 695d00995cSAlexander Graf bool overlap_only_ram) 705d00995cSAlexander Graf { 715d00995cSAlexander Graf struct efi_mem_list *newmap; 725d00995cSAlexander Graf struct efi_mem_desc *map_desc = &map->desc; 735d00995cSAlexander Graf uint64_t map_start = map_desc->physical_start; 745d00995cSAlexander Graf uint64_t map_end = map_start + (map_desc->num_pages << EFI_PAGE_SHIFT); 755d00995cSAlexander Graf uint64_t carve_start = carve_desc->physical_start; 765d00995cSAlexander Graf uint64_t carve_end = carve_start + 775d00995cSAlexander Graf (carve_desc->num_pages << EFI_PAGE_SHIFT); 785d00995cSAlexander Graf 795d00995cSAlexander Graf /* check whether we're overlapping */ 805d00995cSAlexander Graf if ((carve_end <= map_start) || (carve_start >= map_end)) 815d00995cSAlexander Graf return 0; 825d00995cSAlexander Graf 835d00995cSAlexander Graf /* We're overlapping with non-RAM, warn the caller if desired */ 845d00995cSAlexander Graf if (overlap_only_ram && (map_desc->type != EFI_CONVENTIONAL_MEMORY)) 855d00995cSAlexander Graf return -1; 865d00995cSAlexander Graf 875d00995cSAlexander Graf /* Sanitize carve_start and carve_end to lie within our bounds */ 885d00995cSAlexander Graf carve_start = max(carve_start, map_start); 895d00995cSAlexander Graf carve_end = min(carve_end, map_end); 905d00995cSAlexander Graf 915d00995cSAlexander Graf /* Carving at the beginning of our map? Just move it! */ 925d00995cSAlexander Graf if (carve_start == map_start) { 935d00995cSAlexander Graf if (map_end == carve_end) { 945d00995cSAlexander Graf /* Full overlap, just remove map */ 955d00995cSAlexander Graf list_del(&map->link); 965d00995cSAlexander Graf } 975d00995cSAlexander Graf 985d00995cSAlexander Graf map_desc->physical_start = carve_end; 995d00995cSAlexander Graf map_desc->num_pages = (map_end - carve_end) >> EFI_PAGE_SHIFT; 1005d00995cSAlexander Graf return 1; 1015d00995cSAlexander Graf } 1025d00995cSAlexander Graf 1035d00995cSAlexander Graf /* 1045d00995cSAlexander Graf * Overlapping maps, just split the list map at carve_start, 1055d00995cSAlexander Graf * it will get moved or removed in the next iteration. 1065d00995cSAlexander Graf * 1075d00995cSAlexander Graf * [ map_desc |__carve_start__| newmap ] 1085d00995cSAlexander Graf */ 1095d00995cSAlexander Graf 1105d00995cSAlexander Graf /* Create a new map from [ carve_start ... map_end ] */ 1115d00995cSAlexander Graf newmap = calloc(1, sizeof(*newmap)); 1125d00995cSAlexander Graf newmap->desc = map->desc; 1135d00995cSAlexander Graf newmap->desc.physical_start = carve_start; 1145d00995cSAlexander Graf newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT; 1155d00995cSAlexander Graf list_add_tail(&newmap->link, &efi_mem); 1165d00995cSAlexander Graf 1175d00995cSAlexander Graf /* Shrink the map to [ map_start ... carve_start ] */ 1185d00995cSAlexander Graf map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT; 1195d00995cSAlexander Graf 1205d00995cSAlexander Graf return 1; 1215d00995cSAlexander Graf } 1225d00995cSAlexander Graf 1235d00995cSAlexander Graf uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type, 1245d00995cSAlexander Graf bool overlap_only_ram) 1255d00995cSAlexander Graf { 1265d00995cSAlexander Graf struct list_head *lhandle; 1275d00995cSAlexander Graf struct efi_mem_list *newlist; 1285d00995cSAlexander Graf bool do_carving; 1295d00995cSAlexander Graf 1305d00995cSAlexander Graf if (!pages) 1315d00995cSAlexander Graf return start; 1325d00995cSAlexander Graf 1335d00995cSAlexander Graf newlist = calloc(1, sizeof(*newlist)); 1345d00995cSAlexander Graf newlist->desc.type = memory_type; 1355d00995cSAlexander Graf newlist->desc.physical_start = start; 1365d00995cSAlexander Graf newlist->desc.virtual_start = start; 1375d00995cSAlexander Graf newlist->desc.num_pages = pages; 1385d00995cSAlexander Graf 1395d00995cSAlexander Graf switch (memory_type) { 1405d00995cSAlexander Graf case EFI_RUNTIME_SERVICES_CODE: 1415d00995cSAlexander Graf case EFI_RUNTIME_SERVICES_DATA: 1425d00995cSAlexander Graf newlist->desc.attribute = (1 << EFI_MEMORY_WB_SHIFT) | 1435d00995cSAlexander Graf (1ULL << EFI_MEMORY_RUNTIME_SHIFT); 1445d00995cSAlexander Graf break; 1455d00995cSAlexander Graf case EFI_MMAP_IO: 1465d00995cSAlexander Graf newlist->desc.attribute = 1ULL << EFI_MEMORY_RUNTIME_SHIFT; 1475d00995cSAlexander Graf break; 1485d00995cSAlexander Graf default: 1495d00995cSAlexander Graf newlist->desc.attribute = 1 << EFI_MEMORY_WB_SHIFT; 1505d00995cSAlexander Graf break; 1515d00995cSAlexander Graf } 1525d00995cSAlexander Graf 1535d00995cSAlexander Graf /* Add our new map */ 1545d00995cSAlexander Graf do { 1555d00995cSAlexander Graf do_carving = false; 1565d00995cSAlexander Graf list_for_each(lhandle, &efi_mem) { 1575d00995cSAlexander Graf struct efi_mem_list *lmem; 1585d00995cSAlexander Graf int r; 1595d00995cSAlexander Graf 1605d00995cSAlexander Graf lmem = list_entry(lhandle, struct efi_mem_list, link); 1615d00995cSAlexander Graf r = efi_mem_carve_out(lmem, &newlist->desc, 1625d00995cSAlexander Graf overlap_only_ram); 1635d00995cSAlexander Graf if (r < 0) { 1645d00995cSAlexander Graf return 0; 1655d00995cSAlexander Graf } else if (r) { 1665d00995cSAlexander Graf do_carving = true; 1675d00995cSAlexander Graf break; 1685d00995cSAlexander Graf } 1695d00995cSAlexander Graf } 1705d00995cSAlexander Graf } while (do_carving); 1715d00995cSAlexander Graf 1725d00995cSAlexander Graf /* Add our new map */ 1735d00995cSAlexander Graf list_add_tail(&newlist->link, &efi_mem); 1745d00995cSAlexander Graf 17538ce65e1SAlexander Graf /* And make sure memory is listed in descending order */ 17638ce65e1SAlexander Graf efi_mem_sort(); 17738ce65e1SAlexander Graf 1785d00995cSAlexander Graf return start; 1795d00995cSAlexander Graf } 1805d00995cSAlexander Graf 1815d00995cSAlexander Graf static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr) 1825d00995cSAlexander Graf { 1835d00995cSAlexander Graf struct list_head *lhandle; 1845d00995cSAlexander Graf 1855d00995cSAlexander Graf list_for_each(lhandle, &efi_mem) { 1865d00995cSAlexander Graf struct efi_mem_list *lmem = list_entry(lhandle, 1875d00995cSAlexander Graf struct efi_mem_list, link); 1885d00995cSAlexander Graf struct efi_mem_desc *desc = &lmem->desc; 1895d00995cSAlexander Graf uint64_t desc_len = desc->num_pages << EFI_PAGE_SHIFT; 1905d00995cSAlexander Graf uint64_t desc_end = desc->physical_start + desc_len; 1915d00995cSAlexander Graf uint64_t curmax = min(max_addr, desc_end); 1925d00995cSAlexander Graf uint64_t ret = curmax - len; 1935d00995cSAlexander Graf 1945d00995cSAlexander Graf /* We only take memory from free RAM */ 1955d00995cSAlexander Graf if (desc->type != EFI_CONVENTIONAL_MEMORY) 1965d00995cSAlexander Graf continue; 1975d00995cSAlexander Graf 1985d00995cSAlexander Graf /* Out of bounds for max_addr */ 1995d00995cSAlexander Graf if ((ret + len) > max_addr) 2005d00995cSAlexander Graf continue; 2015d00995cSAlexander Graf 2025d00995cSAlexander Graf /* Out of bounds for upper map limit */ 2035d00995cSAlexander Graf if ((ret + len) > desc_end) 2045d00995cSAlexander Graf continue; 2055d00995cSAlexander Graf 2065d00995cSAlexander Graf /* Out of bounds for lower map limit */ 2075d00995cSAlexander Graf if (ret < desc->physical_start) 2085d00995cSAlexander Graf continue; 2095d00995cSAlexander Graf 2105d00995cSAlexander Graf /* Return the highest address in this map within bounds */ 2115d00995cSAlexander Graf return ret; 2125d00995cSAlexander Graf } 2135d00995cSAlexander Graf 2145d00995cSAlexander Graf return 0; 2155d00995cSAlexander Graf } 2165d00995cSAlexander Graf 2175d00995cSAlexander Graf efi_status_t efi_allocate_pages(int type, int memory_type, 2185d00995cSAlexander Graf unsigned long pages, uint64_t *memory) 2195d00995cSAlexander Graf { 2205d00995cSAlexander Graf u64 len = pages << EFI_PAGE_SHIFT; 2215d00995cSAlexander Graf efi_status_t r = EFI_SUCCESS; 2225d00995cSAlexander Graf uint64_t addr; 2235d00995cSAlexander Graf 2245d00995cSAlexander Graf switch (type) { 2255d00995cSAlexander Graf case 0: 2265d00995cSAlexander Graf /* Any page */ 227dede284dSAndreas Färber addr = efi_find_free_memory(len, gd->start_addr_sp); 2285d00995cSAlexander Graf if (!addr) { 2295d00995cSAlexander Graf r = EFI_NOT_FOUND; 2305d00995cSAlexander Graf break; 2315d00995cSAlexander Graf } 2325d00995cSAlexander Graf break; 2335d00995cSAlexander Graf case 1: 2345d00995cSAlexander Graf /* Max address */ 2355d00995cSAlexander Graf addr = efi_find_free_memory(len, *memory); 2365d00995cSAlexander Graf if (!addr) { 2375d00995cSAlexander Graf r = EFI_NOT_FOUND; 2385d00995cSAlexander Graf break; 2395d00995cSAlexander Graf } 2405d00995cSAlexander Graf break; 2415d00995cSAlexander Graf case 2: 2425d00995cSAlexander Graf /* Exact address, reserve it. The addr is already in *memory. */ 2435d00995cSAlexander Graf addr = *memory; 2445d00995cSAlexander Graf break; 2455d00995cSAlexander Graf default: 2465d00995cSAlexander Graf /* UEFI doesn't specify other allocation types */ 2475d00995cSAlexander Graf r = EFI_INVALID_PARAMETER; 2485d00995cSAlexander Graf break; 2495d00995cSAlexander Graf } 2505d00995cSAlexander Graf 2515d00995cSAlexander Graf if (r == EFI_SUCCESS) { 2525d00995cSAlexander Graf uint64_t ret; 2535d00995cSAlexander Graf 2545d00995cSAlexander Graf /* Reserve that map in our memory maps */ 2555d00995cSAlexander Graf ret = efi_add_memory_map(addr, pages, memory_type, true); 2565d00995cSAlexander Graf if (ret == addr) { 2575d00995cSAlexander Graf *memory = addr; 2585d00995cSAlexander Graf } else { 2595d00995cSAlexander Graf /* Map would overlap, bail out */ 2605d00995cSAlexander Graf r = EFI_OUT_OF_RESOURCES; 2615d00995cSAlexander Graf } 2625d00995cSAlexander Graf } 2635d00995cSAlexander Graf 2645d00995cSAlexander Graf return r; 2655d00995cSAlexander Graf } 2665d00995cSAlexander Graf 2675d00995cSAlexander Graf void *efi_alloc(uint64_t len, int memory_type) 2685d00995cSAlexander Graf { 2695d00995cSAlexander Graf uint64_t ret = 0; 2705d00995cSAlexander Graf uint64_t pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT; 2715d00995cSAlexander Graf efi_status_t r; 2725d00995cSAlexander Graf 2735d00995cSAlexander Graf r = efi_allocate_pages(0, memory_type, pages, &ret); 2745d00995cSAlexander Graf if (r == EFI_SUCCESS) 2755d00995cSAlexander Graf return (void*)(uintptr_t)ret; 2765d00995cSAlexander Graf 2775d00995cSAlexander Graf return NULL; 2785d00995cSAlexander Graf } 2795d00995cSAlexander Graf 2805d00995cSAlexander Graf efi_status_t efi_free_pages(uint64_t memory, unsigned long pages) 2815d00995cSAlexander Graf { 2825d00995cSAlexander Graf /* We don't free, let's cross our fingers we have plenty RAM */ 2835d00995cSAlexander Graf return EFI_SUCCESS; 2845d00995cSAlexander Graf } 2855d00995cSAlexander Graf 2865d00995cSAlexander Graf efi_status_t efi_get_memory_map(unsigned long *memory_map_size, 2875d00995cSAlexander Graf struct efi_mem_desc *memory_map, 2885d00995cSAlexander Graf unsigned long *map_key, 2895d00995cSAlexander Graf unsigned long *descriptor_size, 2905d00995cSAlexander Graf uint32_t *descriptor_version) 2915d00995cSAlexander Graf { 2925d00995cSAlexander Graf ulong map_size = 0; 293cee752faSAlexander Graf int map_entries = 0; 2945d00995cSAlexander Graf struct list_head *lhandle; 2955d00995cSAlexander Graf 2965d00995cSAlexander Graf list_for_each(lhandle, &efi_mem) 297cee752faSAlexander Graf map_entries++; 298cee752faSAlexander Graf 299cee752faSAlexander Graf map_size = map_entries * sizeof(struct efi_mem_desc); 3005d00995cSAlexander Graf 3015d00995cSAlexander Graf *memory_map_size = map_size; 3025d00995cSAlexander Graf 3035d00995cSAlexander Graf if (descriptor_size) 3045d00995cSAlexander Graf *descriptor_size = sizeof(struct efi_mem_desc); 3055d00995cSAlexander Graf 3065d00995cSAlexander Graf if (*memory_map_size < map_size) 3075d00995cSAlexander Graf return EFI_BUFFER_TOO_SMALL; 3085d00995cSAlexander Graf 3095d00995cSAlexander Graf /* Copy list into array */ 3105d00995cSAlexander Graf if (memory_map) { 311cee752faSAlexander Graf /* Return the list in ascending order */ 312cee752faSAlexander Graf memory_map = &memory_map[map_entries - 1]; 3135d00995cSAlexander Graf list_for_each(lhandle, &efi_mem) { 3145d00995cSAlexander Graf struct efi_mem_list *lmem; 3155d00995cSAlexander Graf 3165d00995cSAlexander Graf lmem = list_entry(lhandle, struct efi_mem_list, link); 3175d00995cSAlexander Graf *memory_map = lmem->desc; 318cee752faSAlexander Graf memory_map--; 3195d00995cSAlexander Graf } 3205d00995cSAlexander Graf } 3215d00995cSAlexander Graf 3225d00995cSAlexander Graf return EFI_SUCCESS; 3235d00995cSAlexander Graf } 3245d00995cSAlexander Graf 3255d00995cSAlexander Graf int efi_memory_init(void) 3265d00995cSAlexander Graf { 327dede284dSAndreas Färber unsigned long runtime_start, runtime_end, runtime_pages; 328dede284dSAndreas Färber unsigned long uboot_start, uboot_pages; 329dede284dSAndreas Färber unsigned long uboot_stack_size = 16 * 1024 * 1024; 3305d00995cSAlexander Graf int i; 3315d00995cSAlexander Graf 3325d00995cSAlexander Graf /* Add RAM */ 3335d00995cSAlexander Graf for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { 3345d00995cSAlexander Graf u64 ram_start = gd->bd->bi_dram[i].start; 3355d00995cSAlexander Graf u64 ram_size = gd->bd->bi_dram[i].size; 3365d00995cSAlexander Graf u64 start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK; 3375d00995cSAlexander Graf u64 pages = (ram_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT; 3385d00995cSAlexander Graf 3395d00995cSAlexander Graf efi_add_memory_map(start, pages, EFI_CONVENTIONAL_MEMORY, 3405d00995cSAlexander Graf false); 3415d00995cSAlexander Graf } 3425d00995cSAlexander Graf 3435d00995cSAlexander Graf /* Add U-Boot */ 3445d00995cSAlexander Graf uboot_start = (gd->start_addr_sp - uboot_stack_size) & ~EFI_PAGE_MASK; 3455d00995cSAlexander Graf uboot_pages = (gd->ram_top - uboot_start) >> EFI_PAGE_SHIFT; 3465d00995cSAlexander Graf efi_add_memory_map(uboot_start, uboot_pages, EFI_LOADER_DATA, false); 3475d00995cSAlexander Graf 3485d00995cSAlexander Graf /* Add Runtime Services */ 3495d00995cSAlexander Graf runtime_start = (ulong)&__efi_runtime_start & ~EFI_PAGE_MASK; 3505d00995cSAlexander Graf runtime_end = (ulong)&__efi_runtime_stop; 3515d00995cSAlexander Graf runtime_end = (runtime_end + EFI_PAGE_MASK) & ~EFI_PAGE_MASK; 3525d00995cSAlexander Graf runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT; 3535d00995cSAlexander Graf efi_add_memory_map(runtime_start, runtime_pages, 3545d00995cSAlexander Graf EFI_RUNTIME_SERVICES_CODE, false); 3555d00995cSAlexander Graf 356*51735ae0SAlexander Graf #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER 357*51735ae0SAlexander Graf /* Request a 32bit 64MB bounce buffer region */ 358*51735ae0SAlexander Graf uint64_t efi_bounce_buffer_addr = 0xffffffff; 359*51735ae0SAlexander Graf 360*51735ae0SAlexander Graf if (efi_allocate_pages(1, EFI_LOADER_DATA, 361*51735ae0SAlexander Graf (64 * 1024 * 1024) >> EFI_PAGE_SHIFT, 362*51735ae0SAlexander Graf &efi_bounce_buffer_addr) != EFI_SUCCESS) 363*51735ae0SAlexander Graf return -1; 364*51735ae0SAlexander Graf 365*51735ae0SAlexander Graf efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr; 366*51735ae0SAlexander Graf #endif 367*51735ae0SAlexander Graf 3685d00995cSAlexander Graf return 0; 3695d00995cSAlexander Graf } 370