2 * EFI application memory management
4 * Copyright (c) 2016 Alexander Graf
6 * SPDX-License-Identifier: GPL-2.0+
9 /* #define DEBUG_EFI */
12 #include <efi_loader.h>
14 #include <asm/global_data.h>
15 #include <libfdt_env.h>
16 #include <linux/list_sort.h>
20 DECLARE_GLOBAL_DATA_PTR;
23 struct list_head link;
24 struct efi_mem_desc desc;
27 /* This list contains all memory map items */
30 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
31 void *efi_bounce_buffer;
35 * Sorts the memory list from highest address to lowest address
37 * When allocating memory we should always start from the highest
38 * address chunk, so sort the memory list such that the first list
39 * iterator gets the highest address and goes lower from there.
41 static int efi_mem_cmp(void *priv, struct list_head *a, struct list_head *b)
43 struct efi_mem_list *mema = list_entry(a, struct efi_mem_list, link);
44 struct efi_mem_list *memb = list_entry(b, struct efi_mem_list, link);
46 if (mema->desc.physical_start == memb->desc.physical_start)
48 else if (mema->desc.physical_start < memb->desc.physical_start)
54 static void efi_mem_sort(void)
56 list_sort(NULL, &efi_mem, efi_mem_cmp);
60 * Unmaps all memory occupied by the carve_desc region from the
61 * list entry pointed to by map.
63 * Returns 1 if carving was performed or 0 if the regions don't overlap.
64 * Returns -1 if it would affect non-RAM regions but overlap_only_ram is set.
65 * Carving is only guaranteed to complete when all regions return 0.
67 static int efi_mem_carve_out(struct efi_mem_list *map,
68 struct efi_mem_desc *carve_desc,
69 bool overlap_only_ram)
71 struct efi_mem_list *newmap;
72 struct efi_mem_desc *map_desc = &map->desc;
73 uint64_t map_start = map_desc->physical_start;
74 uint64_t map_end = map_start + (map_desc->num_pages << EFI_PAGE_SHIFT);
75 uint64_t carve_start = carve_desc->physical_start;
76 uint64_t carve_end = carve_start +
77 (carve_desc->num_pages << EFI_PAGE_SHIFT);
79 /* check whether we're overlapping */
80 if ((carve_end <= map_start) || (carve_start >= map_end))
83 /* We're overlapping with non-RAM, warn the caller if desired */
84 if (overlap_only_ram && (map_desc->type != EFI_CONVENTIONAL_MEMORY))
87 /* Sanitize carve_start and carve_end to lie within our bounds */
88 carve_start = max(carve_start, map_start);
89 carve_end = min(carve_end, map_end);
91 /* Carving at the beginning of our map? Just move it! */
92 if (carve_start == map_start) {
93 if (map_end == carve_end) {
94 /* Full overlap, just remove map */
98 map_desc->physical_start = carve_end;
99 map_desc->num_pages = (map_end - carve_end) >> EFI_PAGE_SHIFT;
104 * Overlapping maps, just split the list map at carve_start,
105 * it will get moved or removed in the next iteration.
107 * [ map_desc |__carve_start__| newmap ]
110 /* Create a new map from [ carve_start ... map_end ] */
111 newmap = calloc(1, sizeof(*newmap));
112 newmap->desc = map->desc;
113 newmap->desc.physical_start = carve_start;
114 newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT;
115 list_add_tail(&newmap->link, &efi_mem);
117 /* Shrink the map to [ map_start ... carve_start ] */
118 map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT;
123 uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type,
124 bool overlap_only_ram)
126 struct list_head *lhandle;
127 struct efi_mem_list *newlist;
133 newlist = calloc(1, sizeof(*newlist));
134 newlist->desc.type = memory_type;
135 newlist->desc.physical_start = start;
136 newlist->desc.virtual_start = start;
137 newlist->desc.num_pages = pages;
139 switch (memory_type) {
140 case EFI_RUNTIME_SERVICES_CODE:
141 case EFI_RUNTIME_SERVICES_DATA:
142 newlist->desc.attribute = (1 << EFI_MEMORY_WB_SHIFT) |
143 (1ULL << EFI_MEMORY_RUNTIME_SHIFT);
146 newlist->desc.attribute = 1ULL << EFI_MEMORY_RUNTIME_SHIFT;
149 newlist->desc.attribute = 1 << EFI_MEMORY_WB_SHIFT;
153 /* Add our new map */
156 list_for_each(lhandle, &efi_mem) {
157 struct efi_mem_list *lmem;
160 lmem = list_entry(lhandle, struct efi_mem_list, link);
161 r = efi_mem_carve_out(lmem, &newlist->desc,
170 } while (do_carving);
172 /* Add our new map */
173 list_add_tail(&newlist->link, &efi_mem);
175 /* And make sure memory is listed in descending order */
181 static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr)
183 struct list_head *lhandle;
185 list_for_each(lhandle, &efi_mem) {
186 struct efi_mem_list *lmem = list_entry(lhandle,
187 struct efi_mem_list, link);
188 struct efi_mem_desc *desc = &lmem->desc;
189 uint64_t desc_len = desc->num_pages << EFI_PAGE_SHIFT;
190 uint64_t desc_end = desc->physical_start + desc_len;
191 uint64_t curmax = min(max_addr, desc_end);
192 uint64_t ret = curmax - len;
194 /* We only take memory from free RAM */
195 if (desc->type != EFI_CONVENTIONAL_MEMORY)
198 /* Out of bounds for max_addr */
199 if ((ret + len) > max_addr)
202 /* Out of bounds for upper map limit */
203 if ((ret + len) > desc_end)
206 /* Out of bounds for lower map limit */
207 if (ret < desc->physical_start)
210 /* Return the highest address in this map within bounds */
217 efi_status_t efi_allocate_pages(int type, int memory_type,
218 unsigned long pages, uint64_t *memory)
220 u64 len = pages << EFI_PAGE_SHIFT;
221 efi_status_t r = EFI_SUCCESS;
227 addr = efi_find_free_memory(len, gd->start_addr_sp);
235 addr = efi_find_free_memory(len, *memory);
242 /* Exact address, reserve it. The addr is already in *memory. */
246 /* UEFI doesn't specify other allocation types */
247 r = EFI_INVALID_PARAMETER;
251 if (r == EFI_SUCCESS) {
254 /* Reserve that map in our memory maps */
255 ret = efi_add_memory_map(addr, pages, memory_type, true);
259 /* Map would overlap, bail out */
260 r = EFI_OUT_OF_RESOURCES;
267 void *efi_alloc(uint64_t len, int memory_type)
270 uint64_t pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
273 r = efi_allocate_pages(0, memory_type, pages, &ret);
274 if (r == EFI_SUCCESS)
275 return (void*)(uintptr_t)ret;
280 efi_status_t efi_free_pages(uint64_t memory, unsigned long pages)
282 /* We don't free, let's cross our fingers we have plenty RAM */
286 efi_status_t efi_get_memory_map(unsigned long *memory_map_size,
287 struct efi_mem_desc *memory_map,
288 unsigned long *map_key,
289 unsigned long *descriptor_size,
290 uint32_t *descriptor_version)
294 struct list_head *lhandle;
296 list_for_each(lhandle, &efi_mem)
299 map_size = map_entries * sizeof(struct efi_mem_desc);
301 *memory_map_size = map_size;
304 *descriptor_size = sizeof(struct efi_mem_desc);
306 if (*memory_map_size < map_size)
307 return EFI_BUFFER_TOO_SMALL;
309 /* Copy list into array */
311 /* Return the list in ascending order */
312 memory_map = &memory_map[map_entries - 1];
313 list_for_each(lhandle, &efi_mem) {
314 struct efi_mem_list *lmem;
316 lmem = list_entry(lhandle, struct efi_mem_list, link);
317 *memory_map = lmem->desc;
325 int efi_memory_init(void)
327 unsigned long runtime_start, runtime_end, runtime_pages;
328 unsigned long uboot_start, uboot_pages;
329 unsigned long uboot_stack_size = 16 * 1024 * 1024;
333 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
334 u64 ram_start = gd->bd->bi_dram[i].start;
335 u64 ram_size = gd->bd->bi_dram[i].size;
336 u64 start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
337 u64 pages = (ram_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
339 efi_add_memory_map(start, pages, EFI_CONVENTIONAL_MEMORY,
344 uboot_start = (gd->start_addr_sp - uboot_stack_size) & ~EFI_PAGE_MASK;
345 uboot_pages = (gd->ram_top - uboot_start) >> EFI_PAGE_SHIFT;
346 efi_add_memory_map(uboot_start, uboot_pages, EFI_LOADER_DATA, false);
348 /* Add Runtime Services */
349 runtime_start = (ulong)&__efi_runtime_start & ~EFI_PAGE_MASK;
350 runtime_end = (ulong)&__efi_runtime_stop;
351 runtime_end = (runtime_end + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
352 runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT;
353 efi_add_memory_map(runtime_start, runtime_pages,
354 EFI_RUNTIME_SERVICES_CODE, false);
356 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
357 /* Request a 32bit 64MB bounce buffer region */
358 uint64_t efi_bounce_buffer_addr = 0xffffffff;
360 if (efi_allocate_pages(1, EFI_LOADER_DATA,
361 (64 * 1024 * 1024) >> EFI_PAGE_SHIFT,
362 &efi_bounce_buffer_addr) != EFI_SUCCESS)
365 efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr;