1 // SPDX-License-Identifier: GPL-2.0+
3 * EFI application memory management
5 * Copyright (c) 2016 Alexander Graf
9 #include <efi_loader.h>
14 #include <asm/cache.h>
15 #include <asm/global_data.h>
16 #include <linux/list_sort.h>
17 #include <linux/sizes.h>
19 DECLARE_GLOBAL_DATA_PTR;
21 /* Magic number identifying memory allocated from pool */
22 #define EFI_ALLOC_POOL_MAGIC 0x1fe67ddf6491caa2
24 efi_uintn_t efi_memory_map_key;
27 struct list_head link;
28 struct efi_mem_desc desc;
31 #define EFI_CARVE_NO_OVERLAP -1
32 #define EFI_CARVE_LOOP_AGAIN -2
33 #define EFI_CARVE_OVERLAPS_NONRAM -3
35 /* This list contains all memory map items */
38 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
39 void *efi_bounce_buffer;
43 * struct efi_pool_allocation - memory block allocated from pool
45 * @num_pages: number of pages allocated
47 * @data: allocated pool memory
49 * U-Boot services each UEFI AllocatePool() request as a separate
50 * (multiple) page allocation. We have to track the number of pages
51 * to be able to free the correct amount later.
53 * The checksum calculated in function checksum() is used in FreePool() to avoid
54 * freeing memory not allocated by AllocatePool() and duplicate freeing.
56 * EFI requires 8 byte alignment for pool allocations, so we can
57 * prepend each allocation with these header fields.
59 struct efi_pool_allocation {
62 char data[] __aligned(ARCH_DMA_MINALIGN);
66 * checksum() - calculate checksum for memory allocated from pool
68 * @alloc: allocation header
69 * Return: checksum, always non-zero
71 static u64 checksum(struct efi_pool_allocation *alloc)
73 u64 addr = (uintptr_t)alloc;
74 u64 ret = (addr >> 32) ^ (addr << 32) ^ alloc->num_pages ^
82 * Sorts the memory list from highest address to lowest address
84 * When allocating memory we should always start from the highest
85 * address chunk, so sort the memory list such that the first list
86 * iterator gets the highest address and goes lower from there.
88 static int efi_mem_cmp(void *priv, struct list_head *a, struct list_head *b)
90 struct efi_mem_list *mema = list_entry(a, struct efi_mem_list, link);
91 struct efi_mem_list *memb = list_entry(b, struct efi_mem_list, link);
93 if (mema->desc.physical_start == memb->desc.physical_start)
95 else if (mema->desc.physical_start < memb->desc.physical_start)
101 static uint64_t desc_get_end(struct efi_mem_desc *desc)
103 return desc->physical_start + (desc->num_pages << EFI_PAGE_SHIFT);
106 static void efi_mem_sort(void)
108 struct list_head *lhandle;
109 struct efi_mem_list *prevmem = NULL;
110 bool merge_again = true;
112 list_sort(NULL, &efi_mem, efi_mem_cmp);
114 /* Now merge entries that can be merged */
115 while (merge_again) {
117 list_for_each(lhandle, &efi_mem) {
118 struct efi_mem_list *lmem;
119 struct efi_mem_desc *prev = &prevmem->desc;
120 struct efi_mem_desc *cur;
123 lmem = list_entry(lhandle, struct efi_mem_list, link);
131 if ((desc_get_end(cur) == prev->physical_start) &&
132 (prev->type == cur->type) &&
133 (prev->attribute == cur->attribute)) {
134 /* There is an existing map before, reuse it */
135 pages = cur->num_pages;
136 prev->num_pages += pages;
137 prev->physical_start -= pages << EFI_PAGE_SHIFT;
138 prev->virtual_start -= pages << EFI_PAGE_SHIFT;
139 list_del(&lmem->link);
151 /** efi_mem_carve_out - unmap memory region
154 * @carve_desc: memory region to unmap
155 * @overlap_only_ram: the carved out region may only overlap RAM
156 * Return Value: the number of overlapping pages which have been
157 * removed from the map,
158 * EFI_CARVE_NO_OVERLAP, if the regions don't overlap,
159 * EFI_CARVE_OVERLAPS_NONRAM, if the carve and map overlap,
160 * and the map contains anything but free ram
161 * (only when overlap_only_ram is true),
162 * EFI_CARVE_LOOP_AGAIN, if the mapping list should be
163 * traversed again, as it has been altered.
165 * Unmaps all memory occupied by the carve_desc region from the list entry
168 * In case of EFI_CARVE_OVERLAPS_NONRAM it is the callers responsibility
169 * to re-add the already carved out pages to the mapping.
171 static s64 efi_mem_carve_out(struct efi_mem_list *map,
172 struct efi_mem_desc *carve_desc,
173 bool overlap_only_ram)
175 struct efi_mem_list *newmap;
176 struct efi_mem_desc *map_desc = &map->desc;
177 uint64_t map_start = map_desc->physical_start;
178 uint64_t map_end = map_start + (map_desc->num_pages << EFI_PAGE_SHIFT);
179 uint64_t carve_start = carve_desc->physical_start;
180 uint64_t carve_end = carve_start +
181 (carve_desc->num_pages << EFI_PAGE_SHIFT);
183 /* check whether we're overlapping */
184 if ((carve_end <= map_start) || (carve_start >= map_end))
185 return EFI_CARVE_NO_OVERLAP;
187 /* We're overlapping with non-RAM, warn the caller if desired */
188 if (overlap_only_ram && (map_desc->type != EFI_CONVENTIONAL_MEMORY))
189 return EFI_CARVE_OVERLAPS_NONRAM;
191 /* Sanitize carve_start and carve_end to lie within our bounds */
192 carve_start = max(carve_start, map_start);
193 carve_end = min(carve_end, map_end);
195 /* Carving at the beginning of our map? Just move it! */
196 if (carve_start == map_start) {
197 if (map_end == carve_end) {
198 /* Full overlap, just remove map */
199 list_del(&map->link);
202 map->desc.physical_start = carve_end;
203 map->desc.virtual_start = carve_end;
204 map->desc.num_pages = (map_end - carve_end)
208 return (carve_end - carve_start) >> EFI_PAGE_SHIFT;
212 * Overlapping maps, just split the list map at carve_start,
213 * it will get moved or removed in the next iteration.
215 * [ map_desc |__carve_start__| newmap ]
218 /* Create a new map from [ carve_start ... map_end ] */
219 newmap = calloc(1, sizeof(*newmap));
220 newmap->desc = map->desc;
221 newmap->desc.physical_start = carve_start;
222 newmap->desc.virtual_start = carve_start;
223 newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT;
224 /* Insert before current entry (descending address order) */
225 list_add_tail(&newmap->link, &map->link);
227 /* Shrink the map to [ map_start ... carve_start ] */
228 map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT;
230 return EFI_CARVE_LOOP_AGAIN;
234 * efi_add_memory_map_pg() - add pages to the memory map
236 * @start: start address, must be a multiple of EFI_PAGE_SIZE
237 * @pages: number of pages to add
238 * @memory_type: type of memory added
239 * @overlap_only_ram: region may only overlap RAM
240 * Return: status code
242 static efi_status_t efi_add_memory_map_pg(u64 start, u64 pages,
244 bool overlap_only_ram)
246 struct list_head *lhandle;
247 struct efi_mem_list *newlist;
249 uint64_t carved_pages = 0;
250 struct efi_event *evt;
252 EFI_PRINT("%s: 0x%llx 0x%llx %d %s\n", __func__,
253 start, pages, memory_type, overlap_only_ram ? "yes" : "no");
255 if (memory_type >= EFI_MAX_MEMORY_TYPE)
256 return EFI_INVALID_PARAMETER;
261 ++efi_memory_map_key;
262 newlist = calloc(1, sizeof(*newlist));
263 newlist->desc.type = memory_type;
264 newlist->desc.physical_start = start;
265 newlist->desc.virtual_start = start;
266 newlist->desc.num_pages = pages;
268 switch (memory_type) {
269 case EFI_RUNTIME_SERVICES_CODE:
270 case EFI_RUNTIME_SERVICES_DATA:
271 newlist->desc.attribute = EFI_MEMORY_WB | EFI_MEMORY_RUNTIME;
274 newlist->desc.attribute = EFI_MEMORY_RUNTIME;
277 newlist->desc.attribute = EFI_MEMORY_WB;
281 /* Add our new map */
284 list_for_each(lhandle, &efi_mem) {
285 struct efi_mem_list *lmem;
288 lmem = list_entry(lhandle, struct efi_mem_list, link);
289 r = efi_mem_carve_out(lmem, &newlist->desc,
292 case EFI_CARVE_OVERLAPS_NONRAM:
294 * The user requested to only have RAM overlaps,
295 * but we hit a non-RAM region. Error out.
297 return EFI_NO_MAPPING;
298 case EFI_CARVE_NO_OVERLAP:
299 /* Just ignore this list entry */
301 case EFI_CARVE_LOOP_AGAIN:
303 * We split an entry, but need to loop through
304 * the list again to actually carve it.
309 /* We carved a number of pages */
316 /* The list changed, we need to start over */
320 } while (carve_again);
322 if (overlap_only_ram && (carved_pages != pages)) {
324 * The payload wanted to have RAM overlaps, but we overlapped
325 * with an unallocated region. Error out.
327 return EFI_NO_MAPPING;
330 /* Add our new map */
331 list_add_tail(&newlist->link, &efi_mem);
333 /* And make sure memory is listed in descending order */
336 /* Notify that the memory map was changed */
337 list_for_each_entry(evt, &efi_events, link) {
340 &efi_guid_event_group_memory_map_change)) {
341 efi_signal_event(evt);
350 * efi_add_memory_map() - add memory area to the memory map
352 * @start: start address of the memory area
353 * @size: length in bytes of the memory area
354 * @memory_type: type of memory added
356 * Return: status code
358 * This function automatically aligns the start and size of the memory area
361 efi_status_t efi_add_memory_map(u64 start, u64 size, int memory_type)
365 pages = efi_size_in_pages(size + (start & EFI_PAGE_MASK));
366 start &= ~EFI_PAGE_MASK;
368 return efi_add_memory_map_pg(start, pages, memory_type, false);
372 * efi_check_allocated() - validate address to be freed
374 * Check that the address is within allocated memory:
376 * * The address must be in a range of the memory map.
377 * * The address may not point to EFI_CONVENTIONAL_MEMORY.
379 * Page alignment is not checked as this is not a requirement of
382 * @addr: address of page to be freed
383 * @must_be_allocated: return success if the page is allocated
384 * Return: status code
386 static efi_status_t efi_check_allocated(u64 addr, bool must_be_allocated)
388 struct efi_mem_list *item;
390 list_for_each_entry(item, &efi_mem, link) {
391 u64 start = item->desc.physical_start;
392 u64 end = start + (item->desc.num_pages << EFI_PAGE_SHIFT);
394 if (addr >= start && addr < end) {
395 if (must_be_allocated ^
396 (item->desc.type == EFI_CONVENTIONAL_MEMORY))
399 return EFI_NOT_FOUND;
403 return EFI_NOT_FOUND;
406 static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr)
408 struct list_head *lhandle;
411 * Prealign input max address, so we simplify our matching
412 * logic below and can just reuse it as return pointer.
414 max_addr &= ~EFI_PAGE_MASK;
416 list_for_each(lhandle, &efi_mem) {
417 struct efi_mem_list *lmem = list_entry(lhandle,
418 struct efi_mem_list, link);
419 struct efi_mem_desc *desc = &lmem->desc;
420 uint64_t desc_len = desc->num_pages << EFI_PAGE_SHIFT;
421 uint64_t desc_end = desc->physical_start + desc_len;
422 uint64_t curmax = min(max_addr, desc_end);
423 uint64_t ret = curmax - len;
425 /* We only take memory from free RAM */
426 if (desc->type != EFI_CONVENTIONAL_MEMORY)
429 /* Out of bounds for max_addr */
430 if ((ret + len) > max_addr)
433 /* Out of bounds for upper map limit */
434 if ((ret + len) > desc_end)
437 /* Out of bounds for lower map limit */
438 if (ret < desc->physical_start)
441 /* Return the highest address in this map within bounds */
449 * Allocate memory pages.
451 * @type type of allocation to be performed
452 * @memory_type usage type of the allocated memory
453 * @pages number of pages to be allocated
454 * @memory allocated memory
455 * Return: status code
457 efi_status_t efi_allocate_pages(enum efi_allocate_type type,
458 enum efi_memory_type memory_type,
459 efi_uintn_t pages, uint64_t *memory)
461 u64 len = pages << EFI_PAGE_SHIFT;
465 /* Check import parameters */
466 if (memory_type >= EFI_PERSISTENT_MEMORY_TYPE &&
467 memory_type <= 0x6FFFFFFF)
468 return EFI_INVALID_PARAMETER;
470 return EFI_INVALID_PARAMETER;
473 case EFI_ALLOCATE_ANY_PAGES:
475 addr = efi_find_free_memory(len, -1ULL);
477 return EFI_OUT_OF_RESOURCES;
479 case EFI_ALLOCATE_MAX_ADDRESS:
481 addr = efi_find_free_memory(len, *memory);
483 return EFI_OUT_OF_RESOURCES;
485 case EFI_ALLOCATE_ADDRESS:
486 /* Exact address, reserve it. The addr is already in *memory. */
487 ret = efi_check_allocated(*memory, false);
488 if (ret != EFI_SUCCESS)
489 return EFI_NOT_FOUND;
493 /* UEFI doesn't specify other allocation types */
494 return EFI_INVALID_PARAMETER;
497 /* Reserve that map in our memory maps */
498 ret = efi_add_memory_map_pg(addr, pages, memory_type, true);
499 if (ret != EFI_SUCCESS)
500 /* Map would overlap, bail out */
501 return EFI_OUT_OF_RESOURCES;
508 void *efi_alloc(uint64_t len, int memory_type)
511 uint64_t pages = efi_size_in_pages(len);
514 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type, pages,
516 if (r == EFI_SUCCESS)
517 return (void*)(uintptr_t)ret;
523 * efi_free_pages() - free memory pages
525 * @memory: start of the memory area to be freed
526 * @pages: number of pages to be freed
527 * Return: status code
529 efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages)
533 ret = efi_check_allocated(memory, true);
534 if (ret != EFI_SUCCESS)
538 if (!memory || (memory & EFI_PAGE_MASK) || !pages) {
539 printf("%s: illegal free 0x%llx, 0x%zx\n", __func__,
541 return EFI_INVALID_PARAMETER;
544 ret = efi_add_memory_map_pg(memory, pages, EFI_CONVENTIONAL_MEMORY,
546 if (ret != EFI_SUCCESS)
547 return EFI_NOT_FOUND;
553 * efi_alloc_aligned_pages - allocate
556 * @memory_type: usage type of the allocated memory
557 * @align: alignment in bytes
558 * Return: aligned memory or NULL
560 void *efi_alloc_aligned_pages(u64 len, int memory_type, size_t align)
562 u64 req_pages = efi_size_in_pages(len);
563 u64 true_pages = req_pages + efi_size_in_pages(align) - 1;
569 /* align must be zero or a power of two */
570 if (align & (align - 1))
573 /* Check for overflow */
574 if (true_pages < req_pages)
577 if (align < EFI_PAGE_SIZE) {
578 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type,
580 return (r == EFI_SUCCESS) ? (void *)(uintptr_t)mem : NULL;
583 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type,
585 if (r != EFI_SUCCESS)
588 aligned_mem = ALIGN(mem, align);
589 /* Free pages before alignment */
590 free_pages = efi_size_in_pages(aligned_mem - mem);
592 efi_free_pages(mem, free_pages);
594 /* Free trailing pages */
595 free_pages = true_pages - (req_pages + free_pages);
597 mem = aligned_mem + req_pages * EFI_PAGE_SIZE;
598 efi_free_pages(mem, free_pages);
601 return (void *)(uintptr_t)aligned_mem;
605 * efi_allocate_pool - allocate memory from pool
607 * @pool_type: type of the pool from which memory is to be allocated
608 * @size: number of bytes to be allocated
609 * @buffer: allocated memory
610 * Return: status code
612 efi_status_t efi_allocate_pool(enum efi_memory_type pool_type, efi_uintn_t size, void **buffer)
616 struct efi_pool_allocation *alloc;
617 u64 num_pages = efi_size_in_pages(size +
618 sizeof(struct efi_pool_allocation));
621 return EFI_INVALID_PARAMETER;
628 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, pool_type, num_pages,
630 if (r == EFI_SUCCESS) {
631 alloc = (struct efi_pool_allocation *)(uintptr_t)addr;
632 alloc->num_pages = num_pages;
633 alloc->checksum = checksum(alloc);
634 *buffer = alloc->data;
641 * efi_free_pool() - free memory from pool
643 * @buffer: start of memory to be freed
644 * Return: status code
646 efi_status_t efi_free_pool(void *buffer)
649 struct efi_pool_allocation *alloc;
652 return EFI_INVALID_PARAMETER;
654 ret = efi_check_allocated((uintptr_t)buffer, true);
655 if (ret != EFI_SUCCESS)
658 alloc = container_of(buffer, struct efi_pool_allocation, data);
660 /* Check that this memory was allocated by efi_allocate_pool() */
661 if (((uintptr_t)alloc & EFI_PAGE_MASK) ||
662 alloc->checksum != checksum(alloc)) {
663 printf("%s: illegal free 0x%p\n", __func__, buffer);
664 return EFI_INVALID_PARAMETER;
666 /* Avoid double free */
669 ret = efi_free_pages((uintptr_t)alloc, alloc->num_pages);
675 * Get map describing memory usage.
677 * @memory_map_size on entry the size, in bytes, of the memory map buffer,
678 * on exit the size of the copied memory map
679 * @memory_map buffer to which the memory map is written
680 * @map_key key for the memory map
681 * @descriptor_size size of an individual memory descriptor
682 * @descriptor_version version number of the memory descriptor structure
683 * Return: status code
685 efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size,
686 struct efi_mem_desc *memory_map,
687 efi_uintn_t *map_key,
688 efi_uintn_t *descriptor_size,
689 uint32_t *descriptor_version)
691 efi_uintn_t map_size = 0;
693 struct list_head *lhandle;
694 efi_uintn_t provided_map_size;
696 if (!memory_map_size)
697 return EFI_INVALID_PARAMETER;
699 provided_map_size = *memory_map_size;
701 list_for_each(lhandle, &efi_mem)
704 map_size = map_entries * sizeof(struct efi_mem_desc);
706 *memory_map_size = map_size;
709 *descriptor_size = sizeof(struct efi_mem_desc);
711 if (descriptor_version)
712 *descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION;
714 if (provided_map_size < map_size)
715 return EFI_BUFFER_TOO_SMALL;
718 return EFI_INVALID_PARAMETER;
720 /* Copy list into array */
721 /* Return the list in ascending order */
722 memory_map = &memory_map[map_entries - 1];
723 list_for_each(lhandle, &efi_mem) {
724 struct efi_mem_list *lmem;
726 lmem = list_entry(lhandle, struct efi_mem_list, link);
727 *memory_map = lmem->desc;
732 *map_key = efi_memory_map_key;
738 * efi_add_conventional_memory_map() - add a RAM memory area to the map
740 * @ram_start: start address of a RAM memory area
741 * @ram_end: end address of a RAM memory area
742 * @ram_top: max address to be used as conventional memory
743 * Return: status code
745 efi_status_t efi_add_conventional_memory_map(u64 ram_start, u64 ram_end,
750 /* Remove partial pages */
751 ram_end &= ~EFI_PAGE_MASK;
752 ram_start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
754 if (ram_end <= ram_start) {
755 /* Invalid mapping */
756 return EFI_INVALID_PARAMETER;
759 pages = (ram_end - ram_start) >> EFI_PAGE_SHIFT;
761 efi_add_memory_map_pg(ram_start, pages,
762 EFI_CONVENTIONAL_MEMORY, false);
765 * Boards may indicate to the U-Boot memory core that they
766 * can not support memory above ram_top. Let's honor this
767 * in the efi_loader subsystem too by declaring any memory
768 * above ram_top as "already occupied by firmware".
770 if (ram_top < ram_start) {
771 /* ram_top is before this region, reserve all */
772 efi_add_memory_map_pg(ram_start, pages,
773 EFI_BOOT_SERVICES_DATA, true);
774 } else if (ram_top < ram_end) {
775 /* ram_top is inside this region, reserve parts */
776 pages = (ram_end - ram_top) >> EFI_PAGE_SHIFT;
778 efi_add_memory_map_pg(ram_top, pages,
779 EFI_BOOT_SERVICES_DATA, true);
785 __weak void efi_add_known_memory(void)
787 u64 ram_top = board_get_usable_ram_top(0) & ~EFI_PAGE_MASK;
791 * ram_top is just outside mapped memory. So use an offset of one for
792 * mapping the sandbox address.
794 ram_top = (uintptr_t)map_sysmem(ram_top - 1, 0) + 1;
796 /* Fix for 32bit targets with ram_top at 4G */
798 ram_top = 0x100000000ULL;
801 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
802 u64 ram_end, ram_start;
804 ram_start = (uintptr_t)map_sysmem(gd->bd->bi_dram[i].start, 0);
805 ram_end = ram_start + gd->bd->bi_dram[i].size;
807 efi_add_conventional_memory_map(ram_start, ram_end, ram_top);
811 /* Add memory regions for U-Boot's memory and for the runtime services code */
812 static void add_u_boot_and_runtime(void)
814 unsigned long runtime_start, runtime_end, runtime_pages;
815 unsigned long runtime_mask = EFI_PAGE_MASK;
816 unsigned long uboot_start, uboot_pages;
817 unsigned long uboot_stack_size = CONFIG_STACK_SIZE;
820 uboot_start = ((uintptr_t)map_sysmem(gd->start_addr_sp, 0) -
821 uboot_stack_size) & ~EFI_PAGE_MASK;
822 uboot_pages = ((uintptr_t)map_sysmem(gd->ram_top - 1, 0) -
823 uboot_start + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
824 efi_add_memory_map_pg(uboot_start, uboot_pages, EFI_LOADER_DATA,
827 #if defined(__aarch64__)
829 * Runtime Services must be 64KiB aligned according to the
830 * "AArch64 Platforms" section in the UEFI spec (2.7+).
833 runtime_mask = SZ_64K - 1;
837 * Add Runtime Services. We mark surrounding boottime code as runtime as
838 * well to fulfill the runtime alignment constraints but avoid padding.
840 runtime_start = (ulong)&__efi_runtime_start & ~runtime_mask;
841 runtime_end = (ulong)&__efi_runtime_stop;
842 runtime_end = (runtime_end + runtime_mask) & ~runtime_mask;
843 runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT;
844 efi_add_memory_map_pg(runtime_start, runtime_pages,
845 EFI_RUNTIME_SERVICES_CODE, false);
848 int efi_memory_init(void)
850 efi_add_known_memory();
852 add_u_boot_and_runtime();
854 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
855 /* Request a 32bit 64MB bounce buffer region */
856 uint64_t efi_bounce_buffer_addr = 0xffffffff;
858 if (efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, EFI_LOADER_DATA,
859 (64 * 1024 * 1024) >> EFI_PAGE_SHIFT,
860 &efi_bounce_buffer_addr) != EFI_SUCCESS)
863 efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr;