xref: /rk3399_rockchip-uboot/lib/efi_loader/efi_memory.c (revision cee752fa8dcf3b589baf7141011675a0c3f2ded6)
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 
305d00995cSAlexander Graf /*
3138ce65e1SAlexander Graf  * Sorts the memory list from highest address to lowest address
3238ce65e1SAlexander Graf  *
3338ce65e1SAlexander Graf  * When allocating memory we should always start from the highest
3438ce65e1SAlexander Graf  * address chunk, so sort the memory list such that the first list
3538ce65e1SAlexander Graf  * iterator gets the highest address and goes lower from there.
3638ce65e1SAlexander Graf  */
3738ce65e1SAlexander Graf static int efi_mem_cmp(void *priv, struct list_head *a, struct list_head *b)
3838ce65e1SAlexander Graf {
3938ce65e1SAlexander Graf 	struct efi_mem_list *mema = list_entry(a, struct efi_mem_list, link);
4038ce65e1SAlexander Graf 	struct efi_mem_list *memb = list_entry(b, struct efi_mem_list, link);
4138ce65e1SAlexander Graf 
4238ce65e1SAlexander Graf 	if (mema->desc.physical_start == memb->desc.physical_start)
4338ce65e1SAlexander Graf 		return 0;
4438ce65e1SAlexander Graf 	else if (mema->desc.physical_start < memb->desc.physical_start)
4538ce65e1SAlexander Graf 		return 1;
4638ce65e1SAlexander Graf 	else
4738ce65e1SAlexander Graf 		return -1;
4838ce65e1SAlexander Graf }
4938ce65e1SAlexander Graf 
5038ce65e1SAlexander Graf static void efi_mem_sort(void)
5138ce65e1SAlexander Graf {
5238ce65e1SAlexander Graf 	list_sort(NULL, &efi_mem, efi_mem_cmp);
5338ce65e1SAlexander Graf }
5438ce65e1SAlexander Graf 
5538ce65e1SAlexander Graf /*
565d00995cSAlexander Graf  * Unmaps all memory occupied by the carve_desc region from the
575d00995cSAlexander Graf  * list entry pointed to by map.
585d00995cSAlexander Graf  *
595d00995cSAlexander Graf  * Returns 1 if carving was performed or 0 if the regions don't overlap.
605d00995cSAlexander Graf  * Returns -1 if it would affect non-RAM regions but overlap_only_ram is set.
615d00995cSAlexander Graf  * Carving is only guaranteed to complete when all regions return 0.
625d00995cSAlexander Graf  */
635d00995cSAlexander Graf static int efi_mem_carve_out(struct efi_mem_list *map,
645d00995cSAlexander Graf 			     struct efi_mem_desc *carve_desc,
655d00995cSAlexander Graf 			     bool overlap_only_ram)
665d00995cSAlexander Graf {
675d00995cSAlexander Graf 	struct efi_mem_list *newmap;
685d00995cSAlexander Graf 	struct efi_mem_desc *map_desc = &map->desc;
695d00995cSAlexander Graf 	uint64_t map_start = map_desc->physical_start;
705d00995cSAlexander Graf 	uint64_t map_end = map_start + (map_desc->num_pages << EFI_PAGE_SHIFT);
715d00995cSAlexander Graf 	uint64_t carve_start = carve_desc->physical_start;
725d00995cSAlexander Graf 	uint64_t carve_end = carve_start +
735d00995cSAlexander Graf 			     (carve_desc->num_pages << EFI_PAGE_SHIFT);
745d00995cSAlexander Graf 
755d00995cSAlexander Graf 	/* check whether we're overlapping */
765d00995cSAlexander Graf 	if ((carve_end <= map_start) || (carve_start >= map_end))
775d00995cSAlexander Graf 		return 0;
785d00995cSAlexander Graf 
795d00995cSAlexander Graf 	/* We're overlapping with non-RAM, warn the caller if desired */
805d00995cSAlexander Graf 	if (overlap_only_ram && (map_desc->type != EFI_CONVENTIONAL_MEMORY))
815d00995cSAlexander Graf 		return -1;
825d00995cSAlexander Graf 
835d00995cSAlexander Graf 	/* Sanitize carve_start and carve_end to lie within our bounds */
845d00995cSAlexander Graf 	carve_start = max(carve_start, map_start);
855d00995cSAlexander Graf 	carve_end = min(carve_end, map_end);
865d00995cSAlexander Graf 
875d00995cSAlexander Graf 	/* Carving at the beginning of our map? Just move it! */
885d00995cSAlexander Graf 	if (carve_start == map_start) {
895d00995cSAlexander Graf 		if (map_end == carve_end) {
905d00995cSAlexander Graf 			/* Full overlap, just remove map */
915d00995cSAlexander Graf 			list_del(&map->link);
925d00995cSAlexander Graf 		}
935d00995cSAlexander Graf 
945d00995cSAlexander Graf 		map_desc->physical_start = carve_end;
955d00995cSAlexander Graf 		map_desc->num_pages = (map_end - carve_end) >> EFI_PAGE_SHIFT;
965d00995cSAlexander Graf 		return 1;
975d00995cSAlexander Graf 	}
985d00995cSAlexander Graf 
995d00995cSAlexander Graf 	/*
1005d00995cSAlexander Graf 	 * Overlapping maps, just split the list map at carve_start,
1015d00995cSAlexander Graf 	 * it will get moved or removed in the next iteration.
1025d00995cSAlexander Graf 	 *
1035d00995cSAlexander Graf 	 * [ map_desc |__carve_start__| newmap ]
1045d00995cSAlexander Graf 	 */
1055d00995cSAlexander Graf 
1065d00995cSAlexander Graf 	/* Create a new map from [ carve_start ... map_end ] */
1075d00995cSAlexander Graf 	newmap = calloc(1, sizeof(*newmap));
1085d00995cSAlexander Graf 	newmap->desc = map->desc;
1095d00995cSAlexander Graf 	newmap->desc.physical_start = carve_start;
1105d00995cSAlexander Graf 	newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT;
1115d00995cSAlexander Graf         list_add_tail(&newmap->link, &efi_mem);
1125d00995cSAlexander Graf 
1135d00995cSAlexander Graf 	/* Shrink the map to [ map_start ... carve_start ] */
1145d00995cSAlexander Graf 	map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT;
1155d00995cSAlexander Graf 
1165d00995cSAlexander Graf 	return 1;
1175d00995cSAlexander Graf }
1185d00995cSAlexander Graf 
1195d00995cSAlexander Graf uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type,
1205d00995cSAlexander Graf 			    bool overlap_only_ram)
1215d00995cSAlexander Graf {
1225d00995cSAlexander Graf 	struct list_head *lhandle;
1235d00995cSAlexander Graf 	struct efi_mem_list *newlist;
1245d00995cSAlexander Graf 	bool do_carving;
1255d00995cSAlexander Graf 
1265d00995cSAlexander Graf 	if (!pages)
1275d00995cSAlexander Graf 		return start;
1285d00995cSAlexander Graf 
1295d00995cSAlexander Graf 	newlist = calloc(1, sizeof(*newlist));
1305d00995cSAlexander Graf 	newlist->desc.type = memory_type;
1315d00995cSAlexander Graf 	newlist->desc.physical_start = start;
1325d00995cSAlexander Graf 	newlist->desc.virtual_start = start;
1335d00995cSAlexander Graf 	newlist->desc.num_pages = pages;
1345d00995cSAlexander Graf 
1355d00995cSAlexander Graf 	switch (memory_type) {
1365d00995cSAlexander Graf 	case EFI_RUNTIME_SERVICES_CODE:
1375d00995cSAlexander Graf 	case EFI_RUNTIME_SERVICES_DATA:
1385d00995cSAlexander Graf 		newlist->desc.attribute = (1 << EFI_MEMORY_WB_SHIFT) |
1395d00995cSAlexander Graf 					  (1ULL << EFI_MEMORY_RUNTIME_SHIFT);
1405d00995cSAlexander Graf 		break;
1415d00995cSAlexander Graf 	case EFI_MMAP_IO:
1425d00995cSAlexander Graf 		newlist->desc.attribute = 1ULL << EFI_MEMORY_RUNTIME_SHIFT;
1435d00995cSAlexander Graf 		break;
1445d00995cSAlexander Graf 	default:
1455d00995cSAlexander Graf 		newlist->desc.attribute = 1 << EFI_MEMORY_WB_SHIFT;
1465d00995cSAlexander Graf 		break;
1475d00995cSAlexander Graf 	}
1485d00995cSAlexander Graf 
1495d00995cSAlexander Graf 	/* Add our new map */
1505d00995cSAlexander Graf 	do {
1515d00995cSAlexander Graf 		do_carving = false;
1525d00995cSAlexander Graf 		list_for_each(lhandle, &efi_mem) {
1535d00995cSAlexander Graf 			struct efi_mem_list *lmem;
1545d00995cSAlexander Graf 			int r;
1555d00995cSAlexander Graf 
1565d00995cSAlexander Graf 			lmem = list_entry(lhandle, struct efi_mem_list, link);
1575d00995cSAlexander Graf 			r = efi_mem_carve_out(lmem, &newlist->desc,
1585d00995cSAlexander Graf 					      overlap_only_ram);
1595d00995cSAlexander Graf 			if (r < 0) {
1605d00995cSAlexander Graf 				return 0;
1615d00995cSAlexander Graf 			} else if (r) {
1625d00995cSAlexander Graf 				do_carving = true;
1635d00995cSAlexander Graf 				break;
1645d00995cSAlexander Graf 			}
1655d00995cSAlexander Graf 		}
1665d00995cSAlexander Graf 	} while (do_carving);
1675d00995cSAlexander Graf 
1685d00995cSAlexander Graf 	/* Add our new map */
1695d00995cSAlexander Graf         list_add_tail(&newlist->link, &efi_mem);
1705d00995cSAlexander Graf 
17138ce65e1SAlexander Graf 	/* And make sure memory is listed in descending order */
17238ce65e1SAlexander Graf 	efi_mem_sort();
17338ce65e1SAlexander Graf 
1745d00995cSAlexander Graf 	return start;
1755d00995cSAlexander Graf }
1765d00995cSAlexander Graf 
1775d00995cSAlexander Graf static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr)
1785d00995cSAlexander Graf {
1795d00995cSAlexander Graf 	struct list_head *lhandle;
1805d00995cSAlexander Graf 
1815d00995cSAlexander Graf 	list_for_each(lhandle, &efi_mem) {
1825d00995cSAlexander Graf 		struct efi_mem_list *lmem = list_entry(lhandle,
1835d00995cSAlexander Graf 			struct efi_mem_list, link);
1845d00995cSAlexander Graf 		struct efi_mem_desc *desc = &lmem->desc;
1855d00995cSAlexander Graf 		uint64_t desc_len = desc->num_pages << EFI_PAGE_SHIFT;
1865d00995cSAlexander Graf 		uint64_t desc_end = desc->physical_start + desc_len;
1875d00995cSAlexander Graf 		uint64_t curmax = min(max_addr, desc_end);
1885d00995cSAlexander Graf 		uint64_t ret = curmax - len;
1895d00995cSAlexander Graf 
1905d00995cSAlexander Graf 		/* We only take memory from free RAM */
1915d00995cSAlexander Graf 		if (desc->type != EFI_CONVENTIONAL_MEMORY)
1925d00995cSAlexander Graf 			continue;
1935d00995cSAlexander Graf 
1945d00995cSAlexander Graf 		/* Out of bounds for max_addr */
1955d00995cSAlexander Graf 		if ((ret + len) > max_addr)
1965d00995cSAlexander Graf 			continue;
1975d00995cSAlexander Graf 
1985d00995cSAlexander Graf 		/* Out of bounds for upper map limit */
1995d00995cSAlexander Graf 		if ((ret + len) > desc_end)
2005d00995cSAlexander Graf 			continue;
2015d00995cSAlexander Graf 
2025d00995cSAlexander Graf 		/* Out of bounds for lower map limit */
2035d00995cSAlexander Graf 		if (ret < desc->physical_start)
2045d00995cSAlexander Graf 			continue;
2055d00995cSAlexander Graf 
2065d00995cSAlexander Graf 		/* Return the highest address in this map within bounds */
2075d00995cSAlexander Graf 		return ret;
2085d00995cSAlexander Graf 	}
2095d00995cSAlexander Graf 
2105d00995cSAlexander Graf 	return 0;
2115d00995cSAlexander Graf }
2125d00995cSAlexander Graf 
2135d00995cSAlexander Graf efi_status_t efi_allocate_pages(int type, int memory_type,
2145d00995cSAlexander Graf 				unsigned long pages, uint64_t *memory)
2155d00995cSAlexander Graf {
2165d00995cSAlexander Graf 	u64 len = pages << EFI_PAGE_SHIFT;
2175d00995cSAlexander Graf 	efi_status_t r = EFI_SUCCESS;
2185d00995cSAlexander Graf 	uint64_t addr;
2195d00995cSAlexander Graf 
2205d00995cSAlexander Graf 	switch (type) {
2215d00995cSAlexander Graf 	case 0:
2225d00995cSAlexander Graf 		/* Any page */
2235d00995cSAlexander Graf 		addr = efi_find_free_memory(len, gd->ram_top);
2245d00995cSAlexander Graf 		if (!addr) {
2255d00995cSAlexander Graf 			r = EFI_NOT_FOUND;
2265d00995cSAlexander Graf 			break;
2275d00995cSAlexander Graf 		}
2285d00995cSAlexander Graf 		break;
2295d00995cSAlexander Graf 	case 1:
2305d00995cSAlexander Graf 		/* Max address */
2315d00995cSAlexander Graf 		addr = efi_find_free_memory(len, *memory);
2325d00995cSAlexander Graf 		if (!addr) {
2335d00995cSAlexander Graf 			r = EFI_NOT_FOUND;
2345d00995cSAlexander Graf 			break;
2355d00995cSAlexander Graf 		}
2365d00995cSAlexander Graf 		break;
2375d00995cSAlexander Graf 	case 2:
2385d00995cSAlexander Graf 		/* Exact address, reserve it. The addr is already in *memory. */
2395d00995cSAlexander Graf 		addr = *memory;
2405d00995cSAlexander Graf 		break;
2415d00995cSAlexander Graf 	default:
2425d00995cSAlexander Graf 		/* UEFI doesn't specify other allocation types */
2435d00995cSAlexander Graf 		r = EFI_INVALID_PARAMETER;
2445d00995cSAlexander Graf 		break;
2455d00995cSAlexander Graf 	}
2465d00995cSAlexander Graf 
2475d00995cSAlexander Graf 	if (r == EFI_SUCCESS) {
2485d00995cSAlexander Graf 		uint64_t ret;
2495d00995cSAlexander Graf 
2505d00995cSAlexander Graf 		/* Reserve that map in our memory maps */
2515d00995cSAlexander Graf 		ret = efi_add_memory_map(addr, pages, memory_type, true);
2525d00995cSAlexander Graf 		if (ret == addr) {
2535d00995cSAlexander Graf 			*memory = addr;
2545d00995cSAlexander Graf 		} else {
2555d00995cSAlexander Graf 			/* Map would overlap, bail out */
2565d00995cSAlexander Graf 			r = EFI_OUT_OF_RESOURCES;
2575d00995cSAlexander Graf 		}
2585d00995cSAlexander Graf 	}
2595d00995cSAlexander Graf 
2605d00995cSAlexander Graf 	return r;
2615d00995cSAlexander Graf }
2625d00995cSAlexander Graf 
2635d00995cSAlexander Graf void *efi_alloc(uint64_t len, int memory_type)
2645d00995cSAlexander Graf {
2655d00995cSAlexander Graf 	uint64_t ret = 0;
2665d00995cSAlexander Graf 	uint64_t pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
2675d00995cSAlexander Graf 	efi_status_t r;
2685d00995cSAlexander Graf 
2695d00995cSAlexander Graf 	r = efi_allocate_pages(0, memory_type, pages, &ret);
2705d00995cSAlexander Graf 	if (r == EFI_SUCCESS)
2715d00995cSAlexander Graf 		return (void*)(uintptr_t)ret;
2725d00995cSAlexander Graf 
2735d00995cSAlexander Graf 	return NULL;
2745d00995cSAlexander Graf }
2755d00995cSAlexander Graf 
2765d00995cSAlexander Graf efi_status_t efi_free_pages(uint64_t memory, unsigned long pages)
2775d00995cSAlexander Graf {
2785d00995cSAlexander Graf 	/* We don't free, let's cross our fingers we have plenty RAM */
2795d00995cSAlexander Graf 	return EFI_SUCCESS;
2805d00995cSAlexander Graf }
2815d00995cSAlexander Graf 
2825d00995cSAlexander Graf efi_status_t efi_get_memory_map(unsigned long *memory_map_size,
2835d00995cSAlexander Graf 			       struct efi_mem_desc *memory_map,
2845d00995cSAlexander Graf 			       unsigned long *map_key,
2855d00995cSAlexander Graf 			       unsigned long *descriptor_size,
2865d00995cSAlexander Graf 			       uint32_t *descriptor_version)
2875d00995cSAlexander Graf {
2885d00995cSAlexander Graf 	ulong map_size = 0;
289*cee752faSAlexander Graf 	int map_entries = 0;
2905d00995cSAlexander Graf 	struct list_head *lhandle;
2915d00995cSAlexander Graf 
2925d00995cSAlexander Graf 	list_for_each(lhandle, &efi_mem)
293*cee752faSAlexander Graf 		map_entries++;
294*cee752faSAlexander Graf 
295*cee752faSAlexander Graf 	map_size = map_entries * sizeof(struct efi_mem_desc);
2965d00995cSAlexander Graf 
2975d00995cSAlexander Graf 	*memory_map_size = map_size;
2985d00995cSAlexander Graf 
2995d00995cSAlexander Graf 	if (descriptor_size)
3005d00995cSAlexander Graf 		*descriptor_size = sizeof(struct efi_mem_desc);
3015d00995cSAlexander Graf 
3025d00995cSAlexander Graf 	if (*memory_map_size < map_size)
3035d00995cSAlexander Graf 		return EFI_BUFFER_TOO_SMALL;
3045d00995cSAlexander Graf 
3055d00995cSAlexander Graf 	/* Copy list into array */
3065d00995cSAlexander Graf 	if (memory_map) {
307*cee752faSAlexander Graf 		/* Return the list in ascending order */
308*cee752faSAlexander Graf 		memory_map = &memory_map[map_entries - 1];
3095d00995cSAlexander Graf 		list_for_each(lhandle, &efi_mem) {
3105d00995cSAlexander Graf 			struct efi_mem_list *lmem;
3115d00995cSAlexander Graf 
3125d00995cSAlexander Graf 			lmem = list_entry(lhandle, struct efi_mem_list, link);
3135d00995cSAlexander Graf 			*memory_map = lmem->desc;
314*cee752faSAlexander Graf 			memory_map--;
3155d00995cSAlexander Graf 		}
3165d00995cSAlexander Graf 	}
3175d00995cSAlexander Graf 
3185d00995cSAlexander Graf 	return EFI_SUCCESS;
3195d00995cSAlexander Graf }
3205d00995cSAlexander Graf 
3215d00995cSAlexander Graf int efi_memory_init(void)
3225d00995cSAlexander Graf {
3235d00995cSAlexander Graf 	uint64_t runtime_start, runtime_end, runtime_pages;
3245d00995cSAlexander Graf 	uint64_t uboot_start, uboot_pages;
3255d00995cSAlexander Graf 	uint64_t uboot_stack_size = 16 * 1024 * 1024;
3265d00995cSAlexander Graf 	int i;
3275d00995cSAlexander Graf 
3285d00995cSAlexander Graf 	/* Add RAM */
3295d00995cSAlexander Graf 	for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
3305d00995cSAlexander Graf 		u64 ram_start = gd->bd->bi_dram[i].start;
3315d00995cSAlexander Graf 		u64 ram_size = gd->bd->bi_dram[i].size;
3325d00995cSAlexander Graf 		u64 start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
3335d00995cSAlexander Graf 		u64 pages = (ram_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
3345d00995cSAlexander Graf 
3355d00995cSAlexander Graf 		efi_add_memory_map(start, pages, EFI_CONVENTIONAL_MEMORY,
3365d00995cSAlexander Graf 				   false);
3375d00995cSAlexander Graf 	}
3385d00995cSAlexander Graf 
3395d00995cSAlexander Graf 	/* Add U-Boot */
3405d00995cSAlexander Graf 	uboot_start = (gd->start_addr_sp - uboot_stack_size) & ~EFI_PAGE_MASK;
3415d00995cSAlexander Graf 	uboot_pages = (gd->ram_top - uboot_start) >> EFI_PAGE_SHIFT;
3425d00995cSAlexander Graf 	efi_add_memory_map(uboot_start, uboot_pages, EFI_LOADER_DATA, false);
3435d00995cSAlexander Graf 
3445d00995cSAlexander Graf 	/* Add Runtime Services */
3455d00995cSAlexander Graf 	runtime_start = (ulong)&__efi_runtime_start & ~EFI_PAGE_MASK;
3465d00995cSAlexander Graf 	runtime_end = (ulong)&__efi_runtime_stop;
3475d00995cSAlexander Graf 	runtime_end = (runtime_end + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
3485d00995cSAlexander Graf 	runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT;
3495d00995cSAlexander Graf 	efi_add_memory_map(runtime_start, runtime_pages,
3505d00995cSAlexander Graf 			   EFI_RUNTIME_SERVICES_CODE, false);
3515d00995cSAlexander Graf 
3525d00995cSAlexander Graf 	return 0;
3535d00995cSAlexander Graf }
354