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);
72 * Unmaps all memory occupied by the carve_desc region from the
73 * list entry pointed to by map.
75 * Returns EFI_CARVE_NO_OVERLAP if the regions don't overlap.
76 * Returns EFI_CARVE_OVERLAPS_NONRAM if the carve and map overlap,
77 * and the map contains anything but free ram.
78 * (only when overlap_only_ram is true)
79 * Returns EFI_CARVE_LOOP_AGAIN if the mapping list should be traversed
80 * again, as it has been altered
81 * Returns the number of overlapping pages. The pages are removed from
84 * In case of EFI_CARVE_OVERLAPS_NONRAM it is the callers responsibility
85 * to readd the already carved out pages to the mapping.
87 static int efi_mem_carve_out(struct efi_mem_list *map,
88 struct efi_mem_desc *carve_desc,
89 bool overlap_only_ram)
91 struct efi_mem_list *newmap;
92 struct efi_mem_desc *map_desc = &map->desc;
93 uint64_t map_start = map_desc->physical_start;
94 uint64_t map_end = map_start + (map_desc->num_pages << EFI_PAGE_SHIFT);
95 uint64_t carve_start = carve_desc->physical_start;
96 uint64_t carve_end = carve_start +
97 (carve_desc->num_pages << EFI_PAGE_SHIFT);
99 /* check whether we're overlapping */
100 if ((carve_end <= map_start) || (carve_start >= map_end))
101 return EFI_CARVE_NO_OVERLAP;
103 /* We're overlapping with non-RAM, warn the caller if desired */
104 if (overlap_only_ram && (map_desc->type != EFI_CONVENTIONAL_MEMORY))
105 return EFI_CARVE_OVERLAPS_NONRAM;
107 /* Sanitize carve_start and carve_end to lie within our bounds */
108 carve_start = max(carve_start, map_start);
109 carve_end = min(carve_end, map_end);
111 /* Carving at the beginning of our map? Just move it! */
112 if (carve_start == map_start) {
113 if (map_end == carve_end) {
114 /* Full overlap, just remove map */
115 list_del(&map->link);
118 map->desc.physical_start = carve_end;
119 map->desc.num_pages = (map_end - carve_end)
123 return (carve_end - carve_start) >> EFI_PAGE_SHIFT;
127 * Overlapping maps, just split the list map at carve_start,
128 * it will get moved or removed in the next iteration.
130 * [ map_desc |__carve_start__| newmap ]
133 /* Create a new map from [ carve_start ... map_end ] */
134 newmap = calloc(1, sizeof(*newmap));
135 newmap->desc = map->desc;
136 newmap->desc.physical_start = carve_start;
137 newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT;
138 /* Insert before current entry (descending address order) */
139 list_add_tail(&newmap->link, &map->link);
141 /* Shrink the map to [ map_start ... carve_start ] */
142 map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT;
144 return EFI_CARVE_LOOP_AGAIN;
147 uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type,
148 bool overlap_only_ram)
150 struct list_head *lhandle;
151 struct efi_mem_list *newlist;
153 uint64_t carved_pages = 0;
155 debug("%s: 0x%" PRIx64 " 0x%" PRIx64 " %d %s\n", __func__,
156 start, pages, memory_type, overlap_only_ram ? "yes" : "no");
161 newlist = calloc(1, sizeof(*newlist));
162 newlist->desc.type = memory_type;
163 newlist->desc.physical_start = start;
164 newlist->desc.virtual_start = start;
165 newlist->desc.num_pages = pages;
167 switch (memory_type) {
168 case EFI_RUNTIME_SERVICES_CODE:
169 case EFI_RUNTIME_SERVICES_DATA:
170 newlist->desc.attribute = (1 << EFI_MEMORY_WB_SHIFT) |
171 (1ULL << EFI_MEMORY_RUNTIME_SHIFT);
174 newlist->desc.attribute = 1ULL << EFI_MEMORY_RUNTIME_SHIFT;
177 newlist->desc.attribute = 1 << EFI_MEMORY_WB_SHIFT;
181 /* Add our new map */
184 list_for_each(lhandle, &efi_mem) {
185 struct efi_mem_list *lmem;
188 lmem = list_entry(lhandle, struct efi_mem_list, link);
189 r = efi_mem_carve_out(lmem, &newlist->desc,
192 case EFI_CARVE_OVERLAPS_NONRAM:
194 * The user requested to only have RAM overlaps,
195 * but we hit a non-RAM region. Error out.
198 case EFI_CARVE_NO_OVERLAP:
199 /* Just ignore this list entry */
201 case EFI_CARVE_LOOP_AGAIN:
203 * We split an entry, but need to loop through
204 * the list again to actually carve it.
209 /* We carved a number of pages */
216 /* The list changed, we need to start over */
220 } while (carve_again);
222 if (overlap_only_ram && (carved_pages != pages)) {
224 * The payload wanted to have RAM overlaps, but we overlapped
225 * with an unallocated region. Error out.
230 /* Add our new map */
231 list_add_tail(&newlist->link, &efi_mem);
233 /* And make sure memory is listed in descending order */
239 static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr)
241 struct list_head *lhandle;
243 list_for_each(lhandle, &efi_mem) {
244 struct efi_mem_list *lmem = list_entry(lhandle,
245 struct efi_mem_list, link);
246 struct efi_mem_desc *desc = &lmem->desc;
247 uint64_t desc_len = desc->num_pages << EFI_PAGE_SHIFT;
248 uint64_t desc_end = desc->physical_start + desc_len;
249 uint64_t curmax = min(max_addr, desc_end);
250 uint64_t ret = curmax - len;
252 /* We only take memory from free RAM */
253 if (desc->type != EFI_CONVENTIONAL_MEMORY)
256 /* Out of bounds for max_addr */
257 if ((ret + len) > max_addr)
260 /* Out of bounds for upper map limit */
261 if ((ret + len) > desc_end)
264 /* Out of bounds for lower map limit */
265 if (ret < desc->physical_start)
268 /* Return the highest address in this map within bounds */
276 * Allocate memory pages.
278 * @type type of allocation to be performed
279 * @memory_type usage type of the allocated memory
280 * @pages number of pages to be allocated
281 * @memory allocated memory
282 * @return status code
284 efi_status_t efi_allocate_pages(int type, int memory_type,
285 efi_uintn_t pages, uint64_t *memory)
287 u64 len = pages << EFI_PAGE_SHIFT;
288 efi_status_t r = EFI_SUCCESS;
292 case EFI_ALLOCATE_ANY_PAGES:
294 addr = efi_find_free_memory(len, gd->start_addr_sp);
300 case EFI_ALLOCATE_MAX_ADDRESS:
302 addr = efi_find_free_memory(len, *memory);
308 case EFI_ALLOCATE_ADDRESS:
309 /* Exact address, reserve it. The addr is already in *memory. */
313 /* UEFI doesn't specify other allocation types */
314 r = EFI_INVALID_PARAMETER;
318 if (r == EFI_SUCCESS) {
321 /* Reserve that map in our memory maps */
322 ret = efi_add_memory_map(addr, pages, memory_type, true);
326 /* Map would overlap, bail out */
327 r = EFI_OUT_OF_RESOURCES;
334 void *efi_alloc(uint64_t len, int memory_type)
337 uint64_t pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
340 r = efi_allocate_pages(0, memory_type, pages, &ret);
341 if (r == EFI_SUCCESS)
342 return (void*)(uintptr_t)ret;
350 * @memory start of the memory area to be freed
351 * @pages number of pages to be freed
352 * @return status code
354 efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages)
358 r = efi_add_memory_map(memory, pages, EFI_CONVENTIONAL_MEMORY, false);
359 /* Merging of adjacent free regions is missing */
364 return EFI_NOT_FOUND;
368 * Allocate memory from pool.
370 * @pool_type type of the pool from which memory is to be allocated
371 * @size number of bytes to be allocated
372 * @buffer allocated memory
373 * @return status code
375 efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size, void **buffer)
378 efi_physical_addr_t t;
379 u64 num_pages = (size + sizeof(struct efi_pool_allocation) +
380 EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
387 r = efi_allocate_pages(0, pool_type, num_pages, &t);
389 if (r == EFI_SUCCESS) {
390 struct efi_pool_allocation *alloc = (void *)(uintptr_t)t;
391 alloc->num_pages = num_pages;
392 *buffer = alloc->data;
399 * Free memory from pool.
401 * @buffer start of memory to be freed
402 * @return status code
404 efi_status_t efi_free_pool(void *buffer)
407 struct efi_pool_allocation *alloc;
410 return EFI_INVALID_PARAMETER;
412 alloc = container_of(buffer, struct efi_pool_allocation, data);
413 /* Sanity check, was the supplied address returned by allocate_pool */
414 assert(((uintptr_t)alloc & EFI_PAGE_MASK) == 0);
416 r = efi_free_pages((uintptr_t)alloc, alloc->num_pages);
422 * Get map describing memory usage.
424 * @memory_map_size on entry the size, in bytes, of the memory map buffer,
425 * on exit the size of the copied memory map
426 * @memory_map buffer to which the memory map is written
427 * @map_key key for the memory map
428 * @descriptor_size size of an individual memory descriptor
429 * @descriptor_version version number of the memory descriptor structure
430 * @return status code
432 efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size,
433 struct efi_mem_desc *memory_map,
434 efi_uintn_t *map_key,
435 efi_uintn_t *descriptor_size,
436 uint32_t *descriptor_version)
438 efi_uintn_t map_size = 0;
440 struct list_head *lhandle;
441 efi_uintn_t provided_map_size = *memory_map_size;
443 list_for_each(lhandle, &efi_mem)
446 map_size = map_entries * sizeof(struct efi_mem_desc);
448 *memory_map_size = map_size;
450 if (provided_map_size < map_size)
451 return EFI_BUFFER_TOO_SMALL;
454 *descriptor_size = sizeof(struct efi_mem_desc);
456 if (descriptor_version)
457 *descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION;
459 /* Copy list into array */
461 /* Return the list in ascending order */
462 memory_map = &memory_map[map_entries - 1];
463 list_for_each(lhandle, &efi_mem) {
464 struct efi_mem_list *lmem;
466 lmem = list_entry(lhandle, struct efi_mem_list, link);
467 *memory_map = lmem->desc;
477 __weak void efi_add_known_memory(void)
482 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
483 u64 ram_start = gd->bd->bi_dram[i].start;
484 u64 ram_size = gd->bd->bi_dram[i].size;
485 u64 start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
486 u64 pages = (ram_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
488 efi_add_memory_map(start, pages, EFI_CONVENTIONAL_MEMORY,
493 int efi_memory_init(void)
495 unsigned long runtime_start, runtime_end, runtime_pages;
496 unsigned long uboot_start, uboot_pages;
497 unsigned long uboot_stack_size = 16 * 1024 * 1024;
499 efi_add_known_memory();
502 uboot_start = (gd->start_addr_sp - uboot_stack_size) & ~EFI_PAGE_MASK;
503 uboot_pages = (gd->ram_top - uboot_start) >> EFI_PAGE_SHIFT;
504 efi_add_memory_map(uboot_start, uboot_pages, EFI_LOADER_DATA, false);
506 /* Add Runtime Services */
507 runtime_start = (ulong)&__efi_runtime_start & ~EFI_PAGE_MASK;
508 runtime_end = (ulong)&__efi_runtime_stop;
509 runtime_end = (runtime_end + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
510 runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT;
511 efi_add_memory_map(runtime_start, runtime_pages,
512 EFI_RUNTIME_SERVICES_CODE, false);
514 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
515 /* Request a 32bit 64MB bounce buffer region */
516 uint64_t efi_bounce_buffer_addr = 0xffffffff;
518 if (efi_allocate_pages(1, EFI_LOADER_DATA,
519 (64 * 1024 * 1024) >> EFI_PAGE_SHIFT,
520 &efi_bounce_buffer_addr) != EFI_SUCCESS)
523 efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr;