1 // SPDX-License-Identifier: GPL-2.0+
3 * EFI application memory management
5 * Copyright (c) 2016 Alexander Graf
9 #include <efi_loader.h>
13 #include <linux/list_sort.h>
15 DECLARE_GLOBAL_DATA_PTR;
18 struct list_head link;
19 struct efi_mem_desc desc;
22 #define EFI_CARVE_NO_OVERLAP -1
23 #define EFI_CARVE_LOOP_AGAIN -2
24 #define EFI_CARVE_OVERLAPS_NONRAM -3
26 /* This list contains all memory map items */
29 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
30 void *efi_bounce_buffer;
34 * U-Boot services each EFI AllocatePool request as a separate
35 * (multiple) page allocation. We have to track the number of pages
36 * to be able to free the correct amount later.
37 * EFI requires 8 byte alignment for pool allocations, so we can
38 * prepend each allocation with an 64 bit header tracking the
39 * allocation size, and hand out the remainder to the caller.
41 struct efi_pool_allocation {
43 char data[] __aligned(ARCH_DMA_MINALIGN);
47 * Sorts the memory list from highest address to lowest address
49 * When allocating memory we should always start from the highest
50 * address chunk, so sort the memory list such that the first list
51 * iterator gets the highest address and goes lower from there.
53 static int efi_mem_cmp(void *priv, struct list_head *a, struct list_head *b)
55 struct efi_mem_list *mema = list_entry(a, struct efi_mem_list, link);
56 struct efi_mem_list *memb = list_entry(b, struct efi_mem_list, link);
58 if (mema->desc.physical_start == memb->desc.physical_start)
60 else if (mema->desc.physical_start < memb->desc.physical_start)
66 static void efi_mem_sort(void)
68 list_sort(NULL, &efi_mem, efi_mem_cmp);
71 /** efi_mem_carve_out - unmap memory region
74 * @carve_desc: memory region to unmap
75 * @overlap_only_ram: the carved out region may only overlap RAM
76 * Return Value: the number of overlapping pages which have been
77 * removed from the map,
78 * EFI_CARVE_NO_OVERLAP, if the regions don't overlap,
79 * 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 * EFI_CARVE_LOOP_AGAIN, if the mapping list should be
83 * traversed again, as it has been altered.
85 * Unmaps all memory occupied by the carve_desc region from the list entry
88 * In case of EFI_CARVE_OVERLAPS_NONRAM it is the callers responsibility
89 * to re-add the already carved out pages to the mapping.
91 static s64 efi_mem_carve_out(struct efi_mem_list *map,
92 struct efi_mem_desc *carve_desc,
93 bool overlap_only_ram)
95 struct efi_mem_list *newmap;
96 struct efi_mem_desc *map_desc = &map->desc;
97 uint64_t map_start = map_desc->physical_start;
98 uint64_t map_end = map_start + (map_desc->num_pages << EFI_PAGE_SHIFT);
99 uint64_t carve_start = carve_desc->physical_start;
100 uint64_t carve_end = carve_start +
101 (carve_desc->num_pages << EFI_PAGE_SHIFT);
103 /* check whether we're overlapping */
104 if ((carve_end <= map_start) || (carve_start >= map_end))
105 return EFI_CARVE_NO_OVERLAP;
107 /* We're overlapping with non-RAM, warn the caller if desired */
108 if (overlap_only_ram && (map_desc->type != EFI_CONVENTIONAL_MEMORY))
109 return EFI_CARVE_OVERLAPS_NONRAM;
111 /* Sanitize carve_start and carve_end to lie within our bounds */
112 carve_start = max(carve_start, map_start);
113 carve_end = min(carve_end, map_end);
115 /* Carving at the beginning of our map? Just move it! */
116 if (carve_start == map_start) {
117 if (map_end == carve_end) {
118 /* Full overlap, just remove map */
119 list_del(&map->link);
122 map->desc.physical_start = carve_end;
123 map->desc.num_pages = (map_end - carve_end)
127 return (carve_end - carve_start) >> EFI_PAGE_SHIFT;
131 * Overlapping maps, just split the list map at carve_start,
132 * it will get moved or removed in the next iteration.
134 * [ map_desc |__carve_start__| newmap ]
137 /* Create a new map from [ carve_start ... map_end ] */
138 newmap = calloc(1, sizeof(*newmap));
139 newmap->desc = map->desc;
140 newmap->desc.physical_start = carve_start;
141 newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT;
142 /* Insert before current entry (descending address order) */
143 list_add_tail(&newmap->link, &map->link);
145 /* Shrink the map to [ map_start ... carve_start ] */
146 map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT;
148 return EFI_CARVE_LOOP_AGAIN;
151 uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type,
152 bool overlap_only_ram)
154 struct list_head *lhandle;
155 struct efi_mem_list *newlist;
157 uint64_t carved_pages = 0;
159 debug("%s: 0x%" PRIx64 " 0x%" PRIx64 " %d %s\n", __func__,
160 start, pages, memory_type, overlap_only_ram ? "yes" : "no");
165 newlist = calloc(1, sizeof(*newlist));
166 newlist->desc.type = memory_type;
167 newlist->desc.physical_start = start;
168 newlist->desc.virtual_start = start;
169 newlist->desc.num_pages = pages;
171 switch (memory_type) {
172 case EFI_RUNTIME_SERVICES_CODE:
173 case EFI_RUNTIME_SERVICES_DATA:
174 newlist->desc.attribute = (1 << EFI_MEMORY_WB_SHIFT) |
175 (1ULL << EFI_MEMORY_RUNTIME_SHIFT);
178 newlist->desc.attribute = 1ULL << EFI_MEMORY_RUNTIME_SHIFT;
181 newlist->desc.attribute = 1 << EFI_MEMORY_WB_SHIFT;
185 /* Add our new map */
188 list_for_each(lhandle, &efi_mem) {
189 struct efi_mem_list *lmem;
192 lmem = list_entry(lhandle, struct efi_mem_list, link);
193 r = efi_mem_carve_out(lmem, &newlist->desc,
196 case EFI_CARVE_OVERLAPS_NONRAM:
198 * The user requested to only have RAM overlaps,
199 * but we hit a non-RAM region. Error out.
202 case EFI_CARVE_NO_OVERLAP:
203 /* Just ignore this list entry */
205 case EFI_CARVE_LOOP_AGAIN:
207 * We split an entry, but need to loop through
208 * the list again to actually carve it.
213 /* We carved a number of pages */
220 /* The list changed, we need to start over */
224 } while (carve_again);
226 if (overlap_only_ram && (carved_pages != pages)) {
228 * The payload wanted to have RAM overlaps, but we overlapped
229 * with an unallocated region. Error out.
234 /* Add our new map */
235 list_add_tail(&newlist->link, &efi_mem);
237 /* And make sure memory is listed in descending order */
243 static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr)
245 struct list_head *lhandle;
247 list_for_each(lhandle, &efi_mem) {
248 struct efi_mem_list *lmem = list_entry(lhandle,
249 struct efi_mem_list, link);
250 struct efi_mem_desc *desc = &lmem->desc;
251 uint64_t desc_len = desc->num_pages << EFI_PAGE_SHIFT;
252 uint64_t desc_end = desc->physical_start + desc_len;
253 uint64_t curmax = min(max_addr, desc_end);
254 uint64_t ret = curmax - len;
256 /* We only take memory from free RAM */
257 if (desc->type != EFI_CONVENTIONAL_MEMORY)
260 /* Out of bounds for max_addr */
261 if ((ret + len) > max_addr)
264 /* Out of bounds for upper map limit */
265 if ((ret + len) > desc_end)
268 /* Out of bounds for lower map limit */
269 if (ret < desc->physical_start)
272 /* Return the highest address in this map within bounds */
280 * Allocate memory pages.
282 * @type type of allocation to be performed
283 * @memory_type usage type of the allocated memory
284 * @pages number of pages to be allocated
285 * @memory allocated memory
286 * @return status code
288 efi_status_t efi_allocate_pages(int type, int memory_type,
289 efi_uintn_t pages, uint64_t *memory)
291 u64 len = pages << EFI_PAGE_SHIFT;
292 efi_status_t r = EFI_SUCCESS;
296 case EFI_ALLOCATE_ANY_PAGES:
298 addr = efi_find_free_memory(len, gd->start_addr_sp);
304 case EFI_ALLOCATE_MAX_ADDRESS:
306 addr = efi_find_free_memory(len, *memory);
312 case EFI_ALLOCATE_ADDRESS:
313 /* Exact address, reserve it. The addr is already in *memory. */
317 /* UEFI doesn't specify other allocation types */
318 r = EFI_INVALID_PARAMETER;
322 if (r == EFI_SUCCESS) {
325 /* Reserve that map in our memory maps */
326 ret = efi_add_memory_map(addr, pages, memory_type, true);
330 /* Map would overlap, bail out */
331 r = EFI_OUT_OF_RESOURCES;
338 void *efi_alloc(uint64_t len, int memory_type)
341 uint64_t pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
344 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type, pages,
346 if (r == EFI_SUCCESS)
347 return (void*)(uintptr_t)ret;
355 * @memory start of the memory area to be freed
356 * @pages number of pages to be freed
357 * @return status code
359 efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages)
363 r = efi_add_memory_map(memory, pages, EFI_CONVENTIONAL_MEMORY, false);
364 /* Merging of adjacent free regions is missing */
369 return EFI_NOT_FOUND;
373 * Allocate memory from pool.
375 * @pool_type type of the pool from which memory is to be allocated
376 * @size number of bytes to be allocated
377 * @buffer allocated memory
378 * @return status code
380 efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size, void **buffer)
383 efi_physical_addr_t t;
384 u64 num_pages = (size + sizeof(struct efi_pool_allocation) +
385 EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
392 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, pool_type, num_pages,
395 if (r == EFI_SUCCESS) {
396 struct efi_pool_allocation *alloc = (void *)(uintptr_t)t;
397 alloc->num_pages = num_pages;
398 *buffer = alloc->data;
405 * Free memory from pool.
407 * @buffer start of memory to be freed
408 * @return status code
410 efi_status_t efi_free_pool(void *buffer)
413 struct efi_pool_allocation *alloc;
416 return EFI_INVALID_PARAMETER;
418 alloc = container_of(buffer, struct efi_pool_allocation, data);
419 /* Sanity check, was the supplied address returned by allocate_pool */
420 assert(((uintptr_t)alloc & EFI_PAGE_MASK) == 0);
422 r = efi_free_pages((uintptr_t)alloc, alloc->num_pages);
428 * Get map describing memory usage.
430 * @memory_map_size on entry the size, in bytes, of the memory map buffer,
431 * on exit the size of the copied memory map
432 * @memory_map buffer to which the memory map is written
433 * @map_key key for the memory map
434 * @descriptor_size size of an individual memory descriptor
435 * @descriptor_version version number of the memory descriptor structure
436 * @return status code
438 efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size,
439 struct efi_mem_desc *memory_map,
440 efi_uintn_t *map_key,
441 efi_uintn_t *descriptor_size,
442 uint32_t *descriptor_version)
444 efi_uintn_t map_size = 0;
446 struct list_head *lhandle;
447 efi_uintn_t provided_map_size = *memory_map_size;
449 list_for_each(lhandle, &efi_mem)
452 map_size = map_entries * sizeof(struct efi_mem_desc);
454 *memory_map_size = map_size;
456 if (provided_map_size < map_size)
457 return EFI_BUFFER_TOO_SMALL;
460 *descriptor_size = sizeof(struct efi_mem_desc);
462 if (descriptor_version)
463 *descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION;
465 /* Copy list into array */
467 /* Return the list in ascending order */
468 memory_map = &memory_map[map_entries - 1];
469 list_for_each(lhandle, &efi_mem) {
470 struct efi_mem_list *lmem;
472 lmem = list_entry(lhandle, struct efi_mem_list, link);
473 *memory_map = lmem->desc;
483 __weak void efi_add_known_memory(void)
488 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
489 u64 ram_start = gd->bd->bi_dram[i].start;
490 u64 ram_size = gd->bd->bi_dram[i].size;
491 u64 start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
492 u64 pages = (ram_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
494 efi_add_memory_map(start, pages, EFI_CONVENTIONAL_MEMORY,
499 int efi_memory_init(void)
501 unsigned long runtime_start, runtime_end, runtime_pages;
502 unsigned long uboot_start, uboot_pages;
503 unsigned long uboot_stack_size = 16 * 1024 * 1024;
505 efi_add_known_memory();
508 uboot_start = (gd->start_addr_sp - uboot_stack_size) & ~EFI_PAGE_MASK;
509 uboot_pages = (gd->ram_top - uboot_start) >> EFI_PAGE_SHIFT;
510 efi_add_memory_map(uboot_start, uboot_pages, EFI_LOADER_DATA, false);
512 /* Add Runtime Services */
513 runtime_start = (ulong)&__efi_runtime_start & ~EFI_PAGE_MASK;
514 runtime_end = (ulong)&__efi_runtime_stop;
515 runtime_end = (runtime_end + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
516 runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT;
517 efi_add_memory_map(runtime_start, runtime_pages,
518 EFI_RUNTIME_SERVICES_CODE, false);
520 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
521 /* Request a 32bit 64MB bounce buffer region */
522 uint64_t efi_bounce_buffer_addr = 0xffffffff;
524 if (efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, EFI_LOADER_DATA,
525 (64 * 1024 * 1024) >> EFI_PAGE_SHIFT,
526 &efi_bounce_buffer_addr) != EFI_SUCCESS)
529 efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr;