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 EFI_CARVE_NO_OVERLAP if the regions don't overlap.
66 * Returns EFI_CARVE_OVERLAPS_NONRAM if the carve and map overlap,
67 * and the map contains anything but free ram.
68 * (only when overlap_only_ram is true)
69 * Returns EFI_CARVE_LOOP_AGAIN if the mapping list should be traversed
70 * again, as it has been altered
71 * Returns the number of overlapping pages. The pages are removed from
74 * In case of EFI_CARVE_OVERLAPS_NONRAM it is the callers responsibility
75 * to readd the already carved out pages to the mapping.
77 static int efi_mem_carve_out(struct efi_mem_list *map,
78 struct efi_mem_desc *carve_desc,
79 bool overlap_only_ram)
81 struct efi_mem_list *newmap;
82 struct efi_mem_desc *map_desc = &map->desc;
83 uint64_t map_start = map_desc->physical_start;
84 uint64_t map_end = map_start + (map_desc->num_pages << EFI_PAGE_SHIFT);
85 uint64_t carve_start = carve_desc->physical_start;
86 uint64_t carve_end = carve_start +
87 (carve_desc->num_pages << EFI_PAGE_SHIFT);
89 /* check whether we're overlapping */
90 if ((carve_end <= map_start) || (carve_start >= map_end))
91 return EFI_CARVE_NO_OVERLAP;
93 /* We're overlapping with non-RAM, warn the caller if desired */
94 if (overlap_only_ram && (map_desc->type != EFI_CONVENTIONAL_MEMORY))
95 return EFI_CARVE_OVERLAPS_NONRAM;
97 /* Sanitize carve_start and carve_end to lie within our bounds */
98 carve_start = max(carve_start, map_start);
99 carve_end = min(carve_end, map_end);
101 /* Carving at the beginning of our map? Just move it! */
102 if (carve_start == map_start) {
103 if (map_end == carve_end) {
104 /* Full overlap, just remove map */
105 list_del(&map->link);
108 map_desc->physical_start = carve_end;
109 map_desc->num_pages = (map_end - carve_end) >> EFI_PAGE_SHIFT;
110 return (carve_end - carve_start) >> EFI_PAGE_SHIFT;
114 * Overlapping maps, just split the list map at carve_start,
115 * it will get moved or removed in the next iteration.
117 * [ map_desc |__carve_start__| newmap ]
120 /* Create a new map from [ carve_start ... map_end ] */
121 newmap = calloc(1, sizeof(*newmap));
122 newmap->desc = map->desc;
123 newmap->desc.physical_start = carve_start;
124 newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT;
125 list_add_tail(&newmap->link, &efi_mem);
127 /* Shrink the map to [ map_start ... carve_start ] */
128 map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT;
130 return EFI_CARVE_LOOP_AGAIN;
133 uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type,
134 bool overlap_only_ram)
136 struct list_head *lhandle;
137 struct efi_mem_list *newlist;
139 uint64_t carved_pages = 0;
141 debug("%s: 0x%" PRIx64 " 0x%" PRIx64 " %d %s\n", __func__,
142 start, pages, memory_type, overlap_only_ram ? "yes" : "no");
147 newlist = calloc(1, sizeof(*newlist));
148 newlist->desc.type = memory_type;
149 newlist->desc.physical_start = start;
150 newlist->desc.virtual_start = start;
151 newlist->desc.num_pages = pages;
153 switch (memory_type) {
154 case EFI_RUNTIME_SERVICES_CODE:
155 case EFI_RUNTIME_SERVICES_DATA:
156 newlist->desc.attribute = (1 << EFI_MEMORY_WB_SHIFT) |
157 (1ULL << EFI_MEMORY_RUNTIME_SHIFT);
160 newlist->desc.attribute = 1ULL << EFI_MEMORY_RUNTIME_SHIFT;
163 newlist->desc.attribute = 1 << EFI_MEMORY_WB_SHIFT;
167 /* Add our new map */
170 list_for_each(lhandle, &efi_mem) {
171 struct efi_mem_list *lmem;
174 lmem = list_entry(lhandle, struct efi_mem_list, link);
175 r = efi_mem_carve_out(lmem, &newlist->desc,
178 case EFI_CARVE_OVERLAPS_NONRAM:
180 * The user requested to only have RAM overlaps,
181 * but we hit a non-RAM region. Error out.
184 case EFI_CARVE_NO_OVERLAP:
185 /* Just ignore this list entry */
187 case EFI_CARVE_LOOP_AGAIN:
189 * We split an entry, but need to loop through
190 * the list again to actually carve it.
195 /* We carved a number of pages */
202 /* The list changed, we need to start over */
206 } while (carve_again);
208 if (overlap_only_ram && (carved_pages != pages)) {
210 * The payload wanted to have RAM overlaps, but we overlapped
211 * with an unallocated region. Error out.
216 /* Add our new map */
217 list_add_tail(&newlist->link, &efi_mem);
219 /* And make sure memory is listed in descending order */
225 static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr)
227 struct list_head *lhandle;
229 list_for_each(lhandle, &efi_mem) {
230 struct efi_mem_list *lmem = list_entry(lhandle,
231 struct efi_mem_list, link);
232 struct efi_mem_desc *desc = &lmem->desc;
233 uint64_t desc_len = desc->num_pages << EFI_PAGE_SHIFT;
234 uint64_t desc_end = desc->physical_start + desc_len;
235 uint64_t curmax = min(max_addr, desc_end);
236 uint64_t ret = curmax - len;
238 /* We only take memory from free RAM */
239 if (desc->type != EFI_CONVENTIONAL_MEMORY)
242 /* Out of bounds for max_addr */
243 if ((ret + len) > max_addr)
246 /* Out of bounds for upper map limit */
247 if ((ret + len) > desc_end)
250 /* Out of bounds for lower map limit */
251 if (ret < desc->physical_start)
254 /* Return the highest address in this map within bounds */
261 efi_status_t efi_allocate_pages(int type, int memory_type,
262 unsigned long pages, uint64_t *memory)
264 u64 len = pages << EFI_PAGE_SHIFT;
265 efi_status_t r = EFI_SUCCESS;
271 addr = efi_find_free_memory(len, gd->start_addr_sp);
279 addr = efi_find_free_memory(len, *memory);
286 /* Exact address, reserve it. The addr is already in *memory. */
290 /* UEFI doesn't specify other allocation types */
291 r = EFI_INVALID_PARAMETER;
295 if (r == EFI_SUCCESS) {
298 /* Reserve that map in our memory maps */
299 ret = efi_add_memory_map(addr, pages, memory_type, true);
303 /* Map would overlap, bail out */
304 r = EFI_OUT_OF_RESOURCES;
311 void *efi_alloc(uint64_t len, int memory_type)
314 uint64_t pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
317 r = efi_allocate_pages(0, memory_type, pages, &ret);
318 if (r == EFI_SUCCESS)
319 return (void*)(uintptr_t)ret;
324 efi_status_t efi_free_pages(uint64_t memory, unsigned long pages)
326 /* We don't free, let's cross our fingers we have plenty RAM */
330 efi_status_t efi_get_memory_map(unsigned long *memory_map_size,
331 struct efi_mem_desc *memory_map,
332 unsigned long *map_key,
333 unsigned long *descriptor_size,
334 uint32_t *descriptor_version)
338 struct list_head *lhandle;
339 unsigned long provided_map_size = *memory_map_size;
341 list_for_each(lhandle, &efi_mem)
344 map_size = map_entries * sizeof(struct efi_mem_desc);
346 *memory_map_size = map_size;
349 *descriptor_size = sizeof(struct efi_mem_desc);
351 if (descriptor_version)
352 *descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION;
354 if (provided_map_size < map_size)
355 return EFI_BUFFER_TOO_SMALL;
357 /* Copy list into array */
359 /* Return the list in ascending order */
360 memory_map = &memory_map[map_entries - 1];
361 list_for_each(lhandle, &efi_mem) {
362 struct efi_mem_list *lmem;
364 lmem = list_entry(lhandle, struct efi_mem_list, link);
365 *memory_map = lmem->desc;
373 int efi_memory_init(void)
375 unsigned long runtime_start, runtime_end, runtime_pages;
376 unsigned long uboot_start, uboot_pages;
377 unsigned long uboot_stack_size = 16 * 1024 * 1024;
381 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
382 u64 ram_start = gd->bd->bi_dram[i].start;
383 u64 ram_size = gd->bd->bi_dram[i].size;
384 u64 start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
385 u64 pages = (ram_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
387 efi_add_memory_map(start, pages, EFI_CONVENTIONAL_MEMORY,
392 uboot_start = (gd->start_addr_sp - uboot_stack_size) & ~EFI_PAGE_MASK;
393 uboot_pages = (gd->ram_top - uboot_start) >> EFI_PAGE_SHIFT;
394 efi_add_memory_map(uboot_start, uboot_pages, EFI_LOADER_DATA, false);
396 /* Add Runtime Services */
397 runtime_start = (ulong)&__efi_runtime_start & ~EFI_PAGE_MASK;
398 runtime_end = (ulong)&__efi_runtime_stop;
399 runtime_end = (runtime_end + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
400 runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT;
401 efi_add_memory_map(runtime_start, runtime_pages,
402 EFI_RUNTIME_SERVICES_CODE, false);
404 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
405 /* Request a 32bit 64MB bounce buffer region */
406 uint64_t efi_bounce_buffer_addr = 0xffffffff;
408 if (efi_allocate_pages(1, EFI_LOADER_DATA,
409 (64 * 1024 * 1024) >> EFI_PAGE_SHIFT,
410 &efi_bounce_buffer_addr) != EFI_SUCCESS)
413 efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr;