2 * EFI application memory management
4 * Copyright (c) 2016 Alexander Graf
6 * SPDX-License-Identifier: GPL-2.0+
10 #include <efi_loader.h>
12 #include <asm/global_data.h>
13 #include <libfdt_env.h>
14 #include <linux/list_sort.h>
18 DECLARE_GLOBAL_DATA_PTR;
21 struct list_head link;
22 struct efi_mem_desc desc;
25 #define EFI_CARVE_NO_OVERLAP -1
26 #define EFI_CARVE_LOOP_AGAIN -2
27 #define EFI_CARVE_OVERLAPS_NONRAM -3
29 /* This list contains all memory map items */
32 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
33 void *efi_bounce_buffer;
37 * Sorts the memory list from highest address to lowest address
39 * When allocating memory we should always start from the highest
40 * address chunk, so sort the memory list such that the first list
41 * iterator gets the highest address and goes lower from there.
43 static int efi_mem_cmp(void *priv, struct list_head *a, struct list_head *b)
45 struct efi_mem_list *mema = list_entry(a, struct efi_mem_list, link);
46 struct efi_mem_list *memb = list_entry(b, struct efi_mem_list, link);
48 if (mema->desc.physical_start == memb->desc.physical_start)
50 else if (mema->desc.physical_start < memb->desc.physical_start)
56 static void efi_mem_sort(void)
58 list_sort(NULL, &efi_mem, efi_mem_cmp);
62 * Unmaps all memory occupied by the carve_desc region from the
63 * list entry pointed to by map.
65 * Returns 1 if carving was performed or 0 if the regions don't overlap.
66 * Returns -1 if it would affect non-RAM regions but overlap_only_ram is set.
67 * Carving is only guaranteed to complete when all regions return 0.
69 static int efi_mem_carve_out(struct efi_mem_list *map,
70 struct efi_mem_desc *carve_desc,
71 bool overlap_only_ram)
73 struct efi_mem_list *newmap;
74 struct efi_mem_desc *map_desc = &map->desc;
75 uint64_t map_start = map_desc->physical_start;
76 uint64_t map_end = map_start + (map_desc->num_pages << EFI_PAGE_SHIFT);
77 uint64_t carve_start = carve_desc->physical_start;
78 uint64_t carve_end = carve_start +
79 (carve_desc->num_pages << EFI_PAGE_SHIFT);
81 /* check whether we're overlapping */
82 if ((carve_end <= map_start) || (carve_start >= map_end))
83 return EFI_CARVE_NO_OVERLAP;
85 /* We're overlapping with non-RAM, warn the caller if desired */
86 if (overlap_only_ram && (map_desc->type != EFI_CONVENTIONAL_MEMORY))
87 return EFI_CARVE_OVERLAPS_NONRAM;
89 /* Sanitize carve_start and carve_end to lie within our bounds */
90 carve_start = max(carve_start, map_start);
91 carve_end = min(carve_end, map_end);
93 /* Carving at the beginning of our map? Just move it! */
94 if (carve_start == map_start) {
95 if (map_end == carve_end) {
96 /* Full overlap, just remove map */
100 map_desc->physical_start = carve_end;
101 map_desc->num_pages = (map_end - carve_end) >> EFI_PAGE_SHIFT;
102 return (carve_end - carve_start) >> EFI_PAGE_SHIFT;
106 * Overlapping maps, just split the list map at carve_start,
107 * it will get moved or removed in the next iteration.
109 * [ map_desc |__carve_start__| newmap ]
112 /* Create a new map from [ carve_start ... map_end ] */
113 newmap = calloc(1, sizeof(*newmap));
114 newmap->desc = map->desc;
115 newmap->desc.physical_start = carve_start;
116 newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT;
117 list_add_tail(&newmap->link, &efi_mem);
119 /* Shrink the map to [ map_start ... carve_start ] */
120 map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT;
122 return EFI_CARVE_LOOP_AGAIN;
125 uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type,
126 bool overlap_only_ram)
128 struct list_head *lhandle;
129 struct efi_mem_list *newlist;
131 uint64_t carved_pages = 0;
136 newlist = calloc(1, sizeof(*newlist));
137 newlist->desc.type = memory_type;
138 newlist->desc.physical_start = start;
139 newlist->desc.virtual_start = start;
140 newlist->desc.num_pages = pages;
142 switch (memory_type) {
143 case EFI_RUNTIME_SERVICES_CODE:
144 case EFI_RUNTIME_SERVICES_DATA:
145 newlist->desc.attribute = (1 << EFI_MEMORY_WB_SHIFT) |
146 (1ULL << EFI_MEMORY_RUNTIME_SHIFT);
149 newlist->desc.attribute = 1ULL << EFI_MEMORY_RUNTIME_SHIFT;
152 newlist->desc.attribute = 1 << EFI_MEMORY_WB_SHIFT;
156 /* Add our new map */
159 list_for_each(lhandle, &efi_mem) {
160 struct efi_mem_list *lmem;
163 lmem = list_entry(lhandle, struct efi_mem_list, link);
164 r = efi_mem_carve_out(lmem, &newlist->desc,
167 case EFI_CARVE_OVERLAPS_NONRAM:
169 * The user requested to only have RAM overlaps,
170 * but we hit a non-RAM region. Error out.
173 case EFI_CARVE_NO_OVERLAP:
174 /* Just ignore this list entry */
176 case EFI_CARVE_LOOP_AGAIN:
178 * We split an entry, but need to loop through
179 * the list again to actually carve it.
184 /* We carved a number of pages */
191 /* The list changed, we need to start over */
195 } while (carve_again);
197 if (overlap_only_ram && (carved_pages != pages)) {
199 * The payload wanted to have RAM overlaps, but we overlapped
200 * with an unallocated region. Error out.
205 /* Add our new map */
206 list_add_tail(&newlist->link, &efi_mem);
208 /* And make sure memory is listed in descending order */
214 static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr)
216 struct list_head *lhandle;
218 list_for_each(lhandle, &efi_mem) {
219 struct efi_mem_list *lmem = list_entry(lhandle,
220 struct efi_mem_list, link);
221 struct efi_mem_desc *desc = &lmem->desc;
222 uint64_t desc_len = desc->num_pages << EFI_PAGE_SHIFT;
223 uint64_t desc_end = desc->physical_start + desc_len;
224 uint64_t curmax = min(max_addr, desc_end);
225 uint64_t ret = curmax - len;
227 /* We only take memory from free RAM */
228 if (desc->type != EFI_CONVENTIONAL_MEMORY)
231 /* Out of bounds for max_addr */
232 if ((ret + len) > max_addr)
235 /* Out of bounds for upper map limit */
236 if ((ret + len) > desc_end)
239 /* Out of bounds for lower map limit */
240 if (ret < desc->physical_start)
243 /* Return the highest address in this map within bounds */
250 efi_status_t efi_allocate_pages(int type, int memory_type,
251 unsigned long pages, uint64_t *memory)
253 u64 len = pages << EFI_PAGE_SHIFT;
254 efi_status_t r = EFI_SUCCESS;
260 addr = efi_find_free_memory(len, gd->start_addr_sp);
268 addr = efi_find_free_memory(len, *memory);
275 /* Exact address, reserve it. The addr is already in *memory. */
279 /* UEFI doesn't specify other allocation types */
280 r = EFI_INVALID_PARAMETER;
284 if (r == EFI_SUCCESS) {
287 /* Reserve that map in our memory maps */
288 ret = efi_add_memory_map(addr, pages, memory_type, true);
292 /* Map would overlap, bail out */
293 r = EFI_OUT_OF_RESOURCES;
300 void *efi_alloc(uint64_t len, int memory_type)
303 uint64_t pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
306 r = efi_allocate_pages(0, memory_type, pages, &ret);
307 if (r == EFI_SUCCESS)
308 return (void*)(uintptr_t)ret;
313 efi_status_t efi_free_pages(uint64_t memory, unsigned long pages)
315 /* We don't free, let's cross our fingers we have plenty RAM */
319 efi_status_t efi_get_memory_map(unsigned long *memory_map_size,
320 struct efi_mem_desc *memory_map,
321 unsigned long *map_key,
322 unsigned long *descriptor_size,
323 uint32_t *descriptor_version)
327 struct list_head *lhandle;
329 list_for_each(lhandle, &efi_mem)
332 map_size = map_entries * sizeof(struct efi_mem_desc);
334 *memory_map_size = map_size;
337 *descriptor_size = sizeof(struct efi_mem_desc);
339 if (*memory_map_size < map_size)
340 return EFI_BUFFER_TOO_SMALL;
342 /* Copy list into array */
344 /* Return the list in ascending order */
345 memory_map = &memory_map[map_entries - 1];
346 list_for_each(lhandle, &efi_mem) {
347 struct efi_mem_list *lmem;
349 lmem = list_entry(lhandle, struct efi_mem_list, link);
350 *memory_map = lmem->desc;
358 int efi_memory_init(void)
360 unsigned long runtime_start, runtime_end, runtime_pages;
361 unsigned long uboot_start, uboot_pages;
362 unsigned long uboot_stack_size = 16 * 1024 * 1024;
366 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
367 u64 ram_start = gd->bd->bi_dram[i].start;
368 u64 ram_size = gd->bd->bi_dram[i].size;
369 u64 start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
370 u64 pages = (ram_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
372 efi_add_memory_map(start, pages, EFI_CONVENTIONAL_MEMORY,
377 uboot_start = (gd->start_addr_sp - uboot_stack_size) & ~EFI_PAGE_MASK;
378 uboot_pages = (gd->ram_top - uboot_start) >> EFI_PAGE_SHIFT;
379 efi_add_memory_map(uboot_start, uboot_pages, EFI_LOADER_DATA, false);
381 /* Add Runtime Services */
382 runtime_start = (ulong)&__efi_runtime_start & ~EFI_PAGE_MASK;
383 runtime_end = (ulong)&__efi_runtime_stop;
384 runtime_end = (runtime_end + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
385 runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT;
386 efi_add_memory_map(runtime_start, runtime_pages,
387 EFI_RUNTIME_SERVICES_CODE, false);
389 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
390 /* Request a 32bit 64MB bounce buffer region */
391 uint64_t efi_bounce_buffer_addr = 0xffffffff;
393 if (efi_allocate_pages(1, EFI_LOADER_DATA,
394 (64 * 1024 * 1024) >> EFI_PAGE_SHIFT,
395 &efi_bounce_buffer_addr) != EFI_SUCCESS)
398 efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr;