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;
17 efi_uintn_t efi_memory_map_key;
20 struct list_head link;
21 struct efi_mem_desc desc;
24 #define EFI_CARVE_NO_OVERLAP -1
25 #define EFI_CARVE_LOOP_AGAIN -2
26 #define EFI_CARVE_OVERLAPS_NONRAM -3
28 /* This list contains all memory map items */
31 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
32 void *efi_bounce_buffer;
36 * U-Boot services each EFI AllocatePool request as a separate
37 * (multiple) page allocation. We have to track the number of pages
38 * to be able to free the correct amount later.
39 * EFI requires 8 byte alignment for pool allocations, so we can
40 * prepend each allocation with an 64 bit header tracking the
41 * allocation size, and hand out the remainder to the caller.
43 struct efi_pool_allocation {
45 char data[] __aligned(ARCH_DMA_MINALIGN);
49 * Sorts the memory list from highest address to lowest address
51 * When allocating memory we should always start from the highest
52 * address chunk, so sort the memory list such that the first list
53 * iterator gets the highest address and goes lower from there.
55 static int efi_mem_cmp(void *priv, struct list_head *a, struct list_head *b)
57 struct efi_mem_list *mema = list_entry(a, struct efi_mem_list, link);
58 struct efi_mem_list *memb = list_entry(b, struct efi_mem_list, link);
60 if (mema->desc.physical_start == memb->desc.physical_start)
62 else if (mema->desc.physical_start < memb->desc.physical_start)
68 static uint64_t desc_get_end(struct efi_mem_desc *desc)
70 return desc->physical_start + (desc->num_pages << EFI_PAGE_SHIFT);
73 static void efi_mem_sort(void)
75 struct list_head *lhandle;
76 struct efi_mem_list *prevmem = NULL;
77 bool merge_again = true;
79 list_sort(NULL, &efi_mem, efi_mem_cmp);
81 /* Now merge entries that can be merged */
84 list_for_each(lhandle, &efi_mem) {
85 struct efi_mem_list *lmem;
86 struct efi_mem_desc *prev = &prevmem->desc;
87 struct efi_mem_desc *cur;
90 lmem = list_entry(lhandle, struct efi_mem_list, link);
98 if ((desc_get_end(cur) == prev->physical_start) &&
99 (prev->type == cur->type) &&
100 (prev->attribute == cur->attribute)) {
101 /* There is an existing map before, reuse it */
102 pages = cur->num_pages;
103 prev->num_pages += pages;
104 prev->physical_start -= pages << EFI_PAGE_SHIFT;
105 prev->virtual_start -= pages << EFI_PAGE_SHIFT;
106 list_del(&lmem->link);
118 /** efi_mem_carve_out - unmap memory region
121 * @carve_desc: memory region to unmap
122 * @overlap_only_ram: the carved out region may only overlap RAM
123 * Return Value: the number of overlapping pages which have been
124 * removed from the map,
125 * EFI_CARVE_NO_OVERLAP, if the regions don't overlap,
126 * EFI_CARVE_OVERLAPS_NONRAM, if the carve and map overlap,
127 * and the map contains anything but free ram
128 * (only when overlap_only_ram is true),
129 * EFI_CARVE_LOOP_AGAIN, if the mapping list should be
130 * traversed again, as it has been altered.
132 * Unmaps all memory occupied by the carve_desc region from the list entry
135 * In case of EFI_CARVE_OVERLAPS_NONRAM it is the callers responsibility
136 * to re-add the already carved out pages to the mapping.
138 static s64 efi_mem_carve_out(struct efi_mem_list *map,
139 struct efi_mem_desc *carve_desc,
140 bool overlap_only_ram)
142 struct efi_mem_list *newmap;
143 struct efi_mem_desc *map_desc = &map->desc;
144 uint64_t map_start = map_desc->physical_start;
145 uint64_t map_end = map_start + (map_desc->num_pages << EFI_PAGE_SHIFT);
146 uint64_t carve_start = carve_desc->physical_start;
147 uint64_t carve_end = carve_start +
148 (carve_desc->num_pages << EFI_PAGE_SHIFT);
150 /* check whether we're overlapping */
151 if ((carve_end <= map_start) || (carve_start >= map_end))
152 return EFI_CARVE_NO_OVERLAP;
154 /* We're overlapping with non-RAM, warn the caller if desired */
155 if (overlap_only_ram && (map_desc->type != EFI_CONVENTIONAL_MEMORY))
156 return EFI_CARVE_OVERLAPS_NONRAM;
158 /* Sanitize carve_start and carve_end to lie within our bounds */
159 carve_start = max(carve_start, map_start);
160 carve_end = min(carve_end, map_end);
162 /* Carving at the beginning of our map? Just move it! */
163 if (carve_start == map_start) {
164 if (map_end == carve_end) {
165 /* Full overlap, just remove map */
166 list_del(&map->link);
169 map->desc.physical_start = carve_end;
170 map->desc.num_pages = (map_end - carve_end)
174 return (carve_end - carve_start) >> EFI_PAGE_SHIFT;
178 * Overlapping maps, just split the list map at carve_start,
179 * it will get moved or removed in the next iteration.
181 * [ map_desc |__carve_start__| newmap ]
184 /* Create a new map from [ carve_start ... map_end ] */
185 newmap = calloc(1, sizeof(*newmap));
186 newmap->desc = map->desc;
187 newmap->desc.physical_start = carve_start;
188 newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT;
189 /* Insert before current entry (descending address order) */
190 list_add_tail(&newmap->link, &map->link);
192 /* Shrink the map to [ map_start ... carve_start ] */
193 map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT;
195 return EFI_CARVE_LOOP_AGAIN;
198 uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type,
199 bool overlap_only_ram)
201 struct list_head *lhandle;
202 struct efi_mem_list *newlist;
204 uint64_t carved_pages = 0;
206 debug("%s: 0x%llx 0x%llx %d %s\n", __func__,
207 start, pages, memory_type, overlap_only_ram ? "yes" : "no");
209 if (memory_type >= EFI_MAX_MEMORY_TYPE)
210 return EFI_INVALID_PARAMETER;
215 ++efi_memory_map_key;
216 newlist = calloc(1, sizeof(*newlist));
217 newlist->desc.type = memory_type;
218 newlist->desc.physical_start = start;
219 newlist->desc.virtual_start = start;
220 newlist->desc.num_pages = pages;
222 switch (memory_type) {
223 case EFI_RUNTIME_SERVICES_CODE:
224 case EFI_RUNTIME_SERVICES_DATA:
225 newlist->desc.attribute = EFI_MEMORY_WB | EFI_MEMORY_RUNTIME;
228 newlist->desc.attribute = EFI_MEMORY_RUNTIME;
231 newlist->desc.attribute = EFI_MEMORY_WB;
235 /* Add our new map */
238 list_for_each(lhandle, &efi_mem) {
239 struct efi_mem_list *lmem;
242 lmem = list_entry(lhandle, struct efi_mem_list, link);
243 r = efi_mem_carve_out(lmem, &newlist->desc,
246 case EFI_CARVE_OVERLAPS_NONRAM:
248 * The user requested to only have RAM overlaps,
249 * but we hit a non-RAM region. Error out.
252 case EFI_CARVE_NO_OVERLAP:
253 /* Just ignore this list entry */
255 case EFI_CARVE_LOOP_AGAIN:
257 * We split an entry, but need to loop through
258 * the list again to actually carve it.
263 /* We carved a number of pages */
270 /* The list changed, we need to start over */
274 } while (carve_again);
276 if (overlap_only_ram && (carved_pages != pages)) {
278 * The payload wanted to have RAM overlaps, but we overlapped
279 * with an unallocated region. Error out.
284 /* Add our new map */
285 list_add_tail(&newlist->link, &efi_mem);
287 /* And make sure memory is listed in descending order */
293 static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr)
295 struct list_head *lhandle;
298 * Prealign input max address, so we simplify our matching
299 * logic below and can just reuse it as return pointer.
301 max_addr &= ~EFI_PAGE_MASK;
303 list_for_each(lhandle, &efi_mem) {
304 struct efi_mem_list *lmem = list_entry(lhandle,
305 struct efi_mem_list, link);
306 struct efi_mem_desc *desc = &lmem->desc;
307 uint64_t desc_len = desc->num_pages << EFI_PAGE_SHIFT;
308 uint64_t desc_end = desc->physical_start + desc_len;
309 uint64_t curmax = min(max_addr, desc_end);
310 uint64_t ret = curmax - len;
312 /* We only take memory from free RAM */
313 if (desc->type != EFI_CONVENTIONAL_MEMORY)
316 /* Out of bounds for max_addr */
317 if ((ret + len) > max_addr)
320 /* Out of bounds for upper map limit */
321 if ((ret + len) > desc_end)
324 /* Out of bounds for lower map limit */
325 if (ret < desc->physical_start)
328 /* Return the highest address in this map within bounds */
336 * Allocate memory pages.
338 * @type type of allocation to be performed
339 * @memory_type usage type of the allocated memory
340 * @pages number of pages to be allocated
341 * @memory allocated memory
342 * @return status code
344 efi_status_t efi_allocate_pages(int type, int memory_type,
345 efi_uintn_t pages, uint64_t *memory)
347 u64 len = pages << EFI_PAGE_SHIFT;
348 efi_status_t r = EFI_SUCCESS;
352 return EFI_INVALID_PARAMETER;
355 case EFI_ALLOCATE_ANY_PAGES:
357 addr = efi_find_free_memory(len, -1ULL);
363 case EFI_ALLOCATE_MAX_ADDRESS:
365 addr = efi_find_free_memory(len, *memory);
371 case EFI_ALLOCATE_ADDRESS:
372 /* Exact address, reserve it. The addr is already in *memory. */
376 /* UEFI doesn't specify other allocation types */
377 r = EFI_INVALID_PARAMETER;
381 if (r == EFI_SUCCESS) {
384 /* Reserve that map in our memory maps */
385 ret = efi_add_memory_map(addr, pages, memory_type, true);
387 *memory = (uintptr_t)map_sysmem(addr, len);
389 /* Map would overlap, bail out */
390 r = EFI_OUT_OF_RESOURCES;
397 void *efi_alloc(uint64_t len, int memory_type)
400 uint64_t pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
403 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type, pages,
405 if (r == EFI_SUCCESS)
406 return (void*)(uintptr_t)ret;
414 * @memory start of the memory area to be freed
415 * @pages number of pages to be freed
416 * @return status code
418 efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages)
421 uint64_t addr = map_to_sysmem((void *)(uintptr_t)memory);
423 r = efi_add_memory_map(addr, pages, EFI_CONVENTIONAL_MEMORY, false);
424 /* Merging of adjacent free regions is missing */
429 return EFI_NOT_FOUND;
433 * Allocate memory from pool.
435 * @pool_type type of the pool from which memory is to be allocated
436 * @size number of bytes to be allocated
437 * @buffer allocated memory
438 * @return status code
440 efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size, void **buffer)
443 struct efi_pool_allocation *alloc;
444 u64 num_pages = (size + sizeof(struct efi_pool_allocation) +
445 EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
448 return EFI_INVALID_PARAMETER;
455 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, pool_type, num_pages,
458 if (r == EFI_SUCCESS) {
459 alloc->num_pages = num_pages;
460 *buffer = alloc->data;
467 * Free memory from pool.
469 * @buffer start of memory to be freed
470 * @return status code
472 efi_status_t efi_free_pool(void *buffer)
475 struct efi_pool_allocation *alloc;
478 return EFI_INVALID_PARAMETER;
480 alloc = container_of(buffer, struct efi_pool_allocation, data);
481 /* Sanity check, was the supplied address returned by allocate_pool */
482 assert(((uintptr_t)alloc & EFI_PAGE_MASK) == 0);
484 r = efi_free_pages((uintptr_t)alloc, alloc->num_pages);
490 * Get map describing memory usage.
492 * @memory_map_size on entry the size, in bytes, of the memory map buffer,
493 * on exit the size of the copied memory map
494 * @memory_map buffer to which the memory map is written
495 * @map_key key for the memory map
496 * @descriptor_size size of an individual memory descriptor
497 * @descriptor_version version number of the memory descriptor structure
498 * @return status code
500 efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size,
501 struct efi_mem_desc *memory_map,
502 efi_uintn_t *map_key,
503 efi_uintn_t *descriptor_size,
504 uint32_t *descriptor_version)
506 efi_uintn_t map_size = 0;
508 struct list_head *lhandle;
509 efi_uintn_t provided_map_size;
511 if (!memory_map_size)
512 return EFI_INVALID_PARAMETER;
514 provided_map_size = *memory_map_size;
516 list_for_each(lhandle, &efi_mem)
519 map_size = map_entries * sizeof(struct efi_mem_desc);
521 *memory_map_size = map_size;
523 if (provided_map_size < map_size)
524 return EFI_BUFFER_TOO_SMALL;
527 return EFI_INVALID_PARAMETER;
530 *descriptor_size = sizeof(struct efi_mem_desc);
532 if (descriptor_version)
533 *descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION;
535 /* Copy list into array */
536 /* Return the list in ascending order */
537 memory_map = &memory_map[map_entries - 1];
538 list_for_each(lhandle, &efi_mem) {
539 struct efi_mem_list *lmem;
541 lmem = list_entry(lhandle, struct efi_mem_list, link);
542 *memory_map = lmem->desc;
547 *map_key = efi_memory_map_key;
552 __weak void efi_add_known_memory(void)
554 u64 ram_top = board_get_usable_ram_top(0) & ~EFI_PAGE_MASK;
557 /* Fix for 32bit targets with ram_top at 4G */
559 ram_top = 0x100000000ULL;
562 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
563 u64 ram_end, ram_start, pages;
565 ram_start = gd->bd->bi_dram[i].start;
566 ram_end = ram_start + gd->bd->bi_dram[i].size;
568 /* Remove partial pages */
569 ram_end &= ~EFI_PAGE_MASK;
570 ram_start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
572 if (ram_end <= ram_start) {
573 /* Invalid mapping, keep going. */
577 pages = (ram_end - ram_start) >> EFI_PAGE_SHIFT;
579 efi_add_memory_map(ram_start, pages,
580 EFI_CONVENTIONAL_MEMORY, false);
583 * Boards may indicate to the U-Boot memory core that they
584 * can not support memory above ram_top. Let's honor this
585 * in the efi_loader subsystem too by declaring any memory
586 * above ram_top as "already occupied by firmware".
588 if (ram_top < ram_start) {
589 /* ram_top is before this region, reserve all */
590 efi_add_memory_map(ram_start, pages,
591 EFI_BOOT_SERVICES_DATA, true);
592 } else if ((ram_top >= ram_start) && (ram_top < ram_end)) {
593 /* ram_top is inside this region, reserve parts */
594 pages = (ram_end - ram_top) >> EFI_PAGE_SHIFT;
596 efi_add_memory_map(ram_top, pages,
597 EFI_BOOT_SERVICES_DATA, true);
602 /* Add memory regions for U-Boot's memory and for the runtime services code */
603 static void add_u_boot_and_runtime(void)
605 unsigned long runtime_start, runtime_end, runtime_pages;
606 unsigned long uboot_start, uboot_pages;
607 unsigned long uboot_stack_size = 16 * 1024 * 1024;
610 uboot_start = (gd->start_addr_sp - uboot_stack_size) & ~EFI_PAGE_MASK;
611 uboot_pages = (gd->ram_top - uboot_start) >> EFI_PAGE_SHIFT;
612 efi_add_memory_map(uboot_start, uboot_pages, EFI_LOADER_DATA, false);
614 /* Add Runtime Services */
615 runtime_start = (ulong)&__efi_runtime_start & ~EFI_PAGE_MASK;
616 runtime_end = (ulong)&__efi_runtime_stop;
617 runtime_end = (runtime_end + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
618 runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT;
619 efi_add_memory_map(runtime_start, runtime_pages,
620 EFI_RUNTIME_SERVICES_CODE, false);
623 int efi_memory_init(void)
625 efi_add_known_memory();
627 if (!IS_ENABLED(CONFIG_SANDBOX))
628 add_u_boot_and_runtime();
630 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
631 /* Request a 32bit 64MB bounce buffer region */
632 uint64_t efi_bounce_buffer_addr = 0xffffffff;
634 if (efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, EFI_LOADER_DATA,
635 (64 * 1024 * 1024) >> EFI_PAGE_SHIFT,
636 &efi_bounce_buffer_addr) != EFI_SUCCESS)
639 efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr;