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;
133 debug("%s: 0x%" PRIx64 " 0x%" PRIx64 " %d %s\n", __func__,
134 start, pages, memory_type, overlap_only_ram ? "yes" : "no");
139 newlist = calloc(1, sizeof(*newlist));
140 newlist->desc.type = memory_type;
141 newlist->desc.physical_start = start;
142 newlist->desc.virtual_start = start;
143 newlist->desc.num_pages = pages;
145 switch (memory_type) {
146 case EFI_RUNTIME_SERVICES_CODE:
147 case EFI_RUNTIME_SERVICES_DATA:
148 newlist->desc.attribute = (1 << EFI_MEMORY_WB_SHIFT) |
149 (1ULL << EFI_MEMORY_RUNTIME_SHIFT);
152 newlist->desc.attribute = 1ULL << EFI_MEMORY_RUNTIME_SHIFT;
155 newlist->desc.attribute = 1 << EFI_MEMORY_WB_SHIFT;
159 /* Add our new map */
162 list_for_each(lhandle, &efi_mem) {
163 struct efi_mem_list *lmem;
166 lmem = list_entry(lhandle, struct efi_mem_list, link);
167 r = efi_mem_carve_out(lmem, &newlist->desc,
170 case EFI_CARVE_OVERLAPS_NONRAM:
172 * The user requested to only have RAM overlaps,
173 * but we hit a non-RAM region. Error out.
176 case EFI_CARVE_NO_OVERLAP:
177 /* Just ignore this list entry */
179 case EFI_CARVE_LOOP_AGAIN:
181 * We split an entry, but need to loop through
182 * the list again to actually carve it.
187 /* We carved a number of pages */
194 /* The list changed, we need to start over */
198 } while (carve_again);
200 if (overlap_only_ram && (carved_pages != pages)) {
202 * The payload wanted to have RAM overlaps, but we overlapped
203 * with an unallocated region. Error out.
208 /* Add our new map */
209 list_add_tail(&newlist->link, &efi_mem);
211 /* And make sure memory is listed in descending order */
217 static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr)
219 struct list_head *lhandle;
221 list_for_each(lhandle, &efi_mem) {
222 struct efi_mem_list *lmem = list_entry(lhandle,
223 struct efi_mem_list, link);
224 struct efi_mem_desc *desc = &lmem->desc;
225 uint64_t desc_len = desc->num_pages << EFI_PAGE_SHIFT;
226 uint64_t desc_end = desc->physical_start + desc_len;
227 uint64_t curmax = min(max_addr, desc_end);
228 uint64_t ret = curmax - len;
230 /* We only take memory from free RAM */
231 if (desc->type != EFI_CONVENTIONAL_MEMORY)
234 /* Out of bounds for max_addr */
235 if ((ret + len) > max_addr)
238 /* Out of bounds for upper map limit */
239 if ((ret + len) > desc_end)
242 /* Out of bounds for lower map limit */
243 if (ret < desc->physical_start)
246 /* Return the highest address in this map within bounds */
253 efi_status_t efi_allocate_pages(int type, int memory_type,
254 unsigned long pages, uint64_t *memory)
256 u64 len = pages << EFI_PAGE_SHIFT;
257 efi_status_t r = EFI_SUCCESS;
263 addr = efi_find_free_memory(len, gd->start_addr_sp);
271 addr = efi_find_free_memory(len, *memory);
278 /* Exact address, reserve it. The addr is already in *memory. */
282 /* UEFI doesn't specify other allocation types */
283 r = EFI_INVALID_PARAMETER;
287 if (r == EFI_SUCCESS) {
290 /* Reserve that map in our memory maps */
291 ret = efi_add_memory_map(addr, pages, memory_type, true);
295 /* Map would overlap, bail out */
296 r = EFI_OUT_OF_RESOURCES;
303 void *efi_alloc(uint64_t len, int memory_type)
306 uint64_t pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
309 r = efi_allocate_pages(0, memory_type, pages, &ret);
310 if (r == EFI_SUCCESS)
311 return (void*)(uintptr_t)ret;
316 efi_status_t efi_free_pages(uint64_t memory, unsigned long pages)
318 /* We don't free, let's cross our fingers we have plenty RAM */
322 efi_status_t efi_get_memory_map(unsigned long *memory_map_size,
323 struct efi_mem_desc *memory_map,
324 unsigned long *map_key,
325 unsigned long *descriptor_size,
326 uint32_t *descriptor_version)
330 struct list_head *lhandle;
332 list_for_each(lhandle, &efi_mem)
335 map_size = map_entries * sizeof(struct efi_mem_desc);
337 *memory_map_size = map_size;
340 *descriptor_size = sizeof(struct efi_mem_desc);
342 if (*memory_map_size < map_size)
343 return EFI_BUFFER_TOO_SMALL;
345 /* Copy list into array */
347 /* Return the list in ascending order */
348 memory_map = &memory_map[map_entries - 1];
349 list_for_each(lhandle, &efi_mem) {
350 struct efi_mem_list *lmem;
352 lmem = list_entry(lhandle, struct efi_mem_list, link);
353 *memory_map = lmem->desc;
361 int efi_memory_init(void)
363 unsigned long runtime_start, runtime_end, runtime_pages;
364 unsigned long uboot_start, uboot_pages;
365 unsigned long uboot_stack_size = 16 * 1024 * 1024;
369 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
370 u64 ram_start = gd->bd->bi_dram[i].start;
371 u64 ram_size = gd->bd->bi_dram[i].size;
372 u64 start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
373 u64 pages = (ram_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
375 efi_add_memory_map(start, pages, EFI_CONVENTIONAL_MEMORY,
380 uboot_start = (gd->start_addr_sp - uboot_stack_size) & ~EFI_PAGE_MASK;
381 uboot_pages = (gd->ram_top - uboot_start) >> EFI_PAGE_SHIFT;
382 efi_add_memory_map(uboot_start, uboot_pages, EFI_LOADER_DATA, false);
384 /* Add Runtime Services */
385 runtime_start = (ulong)&__efi_runtime_start & ~EFI_PAGE_MASK;
386 runtime_end = (ulong)&__efi_runtime_stop;
387 runtime_end = (runtime_end + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
388 runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT;
389 efi_add_memory_map(runtime_start, runtime_pages,
390 EFI_RUNTIME_SERVICES_CODE, false);
392 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
393 /* Request a 32bit 64MB bounce buffer region */
394 uint64_t efi_bounce_buffer_addr = 0xffffffff;
396 if (efi_allocate_pages(1, EFI_LOADER_DATA,
397 (64 * 1024 * 1024) >> EFI_PAGE_SHIFT,
398 &efi_bounce_buffer_addr) != EFI_SUCCESS)
401 efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr;