]> git.ipfire.org Git - people/ms/u-boot.git/blame - lib/efi_loader/efi_memory.c
efi_loader: Keep memory mapping sorted when splitting an entry
[people/ms/u-boot.git] / lib / efi_loader / efi_memory.c
CommitLineData
5d00995c
AG
1/*
2 * EFI application memory management
3 *
4 * Copyright (c) 2016 Alexander Graf
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
5d00995c
AG
9#include <common.h>
10#include <efi_loader.h>
11#include <malloc.h>
12#include <asm/global_data.h>
13#include <libfdt_env.h>
38ce65e1 14#include <linux/list_sort.h>
5d00995c
AG
15#include <inttypes.h>
16#include <watchdog.h>
17
18DECLARE_GLOBAL_DATA_PTR;
19
20struct efi_mem_list {
21 struct list_head link;
22 struct efi_mem_desc desc;
23};
24
74c16acc
AG
25#define EFI_CARVE_NO_OVERLAP -1
26#define EFI_CARVE_LOOP_AGAIN -2
27#define EFI_CARVE_OVERLAPS_NONRAM -3
28
5d00995c
AG
29/* This list contains all memory map items */
30LIST_HEAD(efi_mem);
31
51735ae0
AG
32#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
33void *efi_bounce_buffer;
34#endif
35
42417bc8
SB
36/*
37 * U-Boot services each EFI AllocatePool request as a separate
38 * (multiple) page allocation. We have to track the number of pages
39 * to be able to free the correct amount later.
40 * EFI requires 8 byte alignment for pool allocations, so we can
41 * prepend each allocation with an 64 bit header tracking the
42 * allocation size, and hand out the remainder to the caller.
43 */
44struct efi_pool_allocation {
45 u64 num_pages;
46 char data[];
47};
48
38ce65e1
AG
49/*
50 * Sorts the memory list from highest address to lowest address
51 *
52 * When allocating memory we should always start from the highest
53 * address chunk, so sort the memory list such that the first list
54 * iterator gets the highest address and goes lower from there.
55 */
56static int efi_mem_cmp(void *priv, struct list_head *a, struct list_head *b)
57{
58 struct efi_mem_list *mema = list_entry(a, struct efi_mem_list, link);
59 struct efi_mem_list *memb = list_entry(b, struct efi_mem_list, link);
60
61 if (mema->desc.physical_start == memb->desc.physical_start)
62 return 0;
63 else if (mema->desc.physical_start < memb->desc.physical_start)
64 return 1;
65 else
66 return -1;
67}
68
69static void efi_mem_sort(void)
70{
71 list_sort(NULL, &efi_mem, efi_mem_cmp);
72}
73
5d00995c
AG
74/*
75 * Unmaps all memory occupied by the carve_desc region from the
76 * list entry pointed to by map.
77 *
852efbf5
SB
78 * Returns EFI_CARVE_NO_OVERLAP if the regions don't overlap.
79 * Returns EFI_CARVE_OVERLAPS_NONRAM if the carve and map overlap,
80 * and the map contains anything but free ram.
81 * (only when overlap_only_ram is true)
82 * Returns EFI_CARVE_LOOP_AGAIN if the mapping list should be traversed
83 * again, as it has been altered
84 * Returns the number of overlapping pages. The pages are removed from
85 * the mapping list.
86 *
87 * In case of EFI_CARVE_OVERLAPS_NONRAM it is the callers responsibility
88 * to readd the already carved out pages to the mapping.
5d00995c
AG
89 */
90static int efi_mem_carve_out(struct efi_mem_list *map,
91 struct efi_mem_desc *carve_desc,
92 bool overlap_only_ram)
93{
94 struct efi_mem_list *newmap;
95 struct efi_mem_desc *map_desc = &map->desc;
96 uint64_t map_start = map_desc->physical_start;
97 uint64_t map_end = map_start + (map_desc->num_pages << EFI_PAGE_SHIFT);
98 uint64_t carve_start = carve_desc->physical_start;
99 uint64_t carve_end = carve_start +
100 (carve_desc->num_pages << EFI_PAGE_SHIFT);
101
102 /* check whether we're overlapping */
103 if ((carve_end <= map_start) || (carve_start >= map_end))
74c16acc 104 return EFI_CARVE_NO_OVERLAP;
5d00995c
AG
105
106 /* We're overlapping with non-RAM, warn the caller if desired */
107 if (overlap_only_ram && (map_desc->type != EFI_CONVENTIONAL_MEMORY))
74c16acc 108 return EFI_CARVE_OVERLAPS_NONRAM;
5d00995c
AG
109
110 /* Sanitize carve_start and carve_end to lie within our bounds */
111 carve_start = max(carve_start, map_start);
112 carve_end = min(carve_end, map_end);
113
114 /* Carving at the beginning of our map? Just move it! */
115 if (carve_start == map_start) {
116 if (map_end == carve_end) {
117 /* Full overlap, just remove map */
118 list_del(&map->link);
119 }
120
121 map_desc->physical_start = carve_end;
122 map_desc->num_pages = (map_end - carve_end) >> EFI_PAGE_SHIFT;
74c16acc 123 return (carve_end - carve_start) >> EFI_PAGE_SHIFT;
5d00995c
AG
124 }
125
126 /*
127 * Overlapping maps, just split the list map at carve_start,
128 * it will get moved or removed in the next iteration.
129 *
130 * [ map_desc |__carve_start__| newmap ]
131 */
132
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;
b6a95172
SB
138 /* Insert before current entry (descending address order) */
139 list_add_tail(&newmap->link, &map->link);
5d00995c
AG
140
141 /* Shrink the map to [ map_start ... carve_start ] */
142 map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT;
143
74c16acc 144 return EFI_CARVE_LOOP_AGAIN;
5d00995c
AG
145}
146
147uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type,
148 bool overlap_only_ram)
149{
150 struct list_head *lhandle;
151 struct efi_mem_list *newlist;
74c16acc
AG
152 bool carve_again;
153 uint64_t carved_pages = 0;
5d00995c 154
c933ed94
AF
155 debug("%s: 0x%" PRIx64 " 0x%" PRIx64 " %d %s\n", __func__,
156 start, pages, memory_type, overlap_only_ram ? "yes" : "no");
157
5d00995c
AG
158 if (!pages)
159 return start;
160
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;
166
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);
172 break;
173 case EFI_MMAP_IO:
174 newlist->desc.attribute = 1ULL << EFI_MEMORY_RUNTIME_SHIFT;
175 break;
176 default:
177 newlist->desc.attribute = 1 << EFI_MEMORY_WB_SHIFT;
178 break;
179 }
180
181 /* Add our new map */
182 do {
74c16acc 183 carve_again = false;
5d00995c
AG
184 list_for_each(lhandle, &efi_mem) {
185 struct efi_mem_list *lmem;
186 int r;
187
188 lmem = list_entry(lhandle, struct efi_mem_list, link);
189 r = efi_mem_carve_out(lmem, &newlist->desc,
190 overlap_only_ram);
74c16acc
AG
191 switch (r) {
192 case EFI_CARVE_OVERLAPS_NONRAM:
193 /*
194 * The user requested to only have RAM overlaps,
195 * but we hit a non-RAM region. Error out.
196 */
5d00995c 197 return 0;
74c16acc
AG
198 case EFI_CARVE_NO_OVERLAP:
199 /* Just ignore this list entry */
200 break;
201 case EFI_CARVE_LOOP_AGAIN:
202 /*
203 * We split an entry, but need to loop through
204 * the list again to actually carve it.
205 */
206 carve_again = true;
207 break;
208 default:
209 /* We carved a number of pages */
210 carved_pages += r;
211 carve_again = true;
212 break;
213 }
214
215 if (carve_again) {
216 /* The list changed, we need to start over */
5d00995c
AG
217 break;
218 }
219 }
74c16acc
AG
220 } while (carve_again);
221
222 if (overlap_only_ram && (carved_pages != pages)) {
223 /*
224 * The payload wanted to have RAM overlaps, but we overlapped
225 * with an unallocated region. Error out.
226 */
227 return 0;
228 }
5d00995c
AG
229
230 /* Add our new map */
231 list_add_tail(&newlist->link, &efi_mem);
232
38ce65e1
AG
233 /* And make sure memory is listed in descending order */
234 efi_mem_sort();
235
5d00995c
AG
236 return start;
237}
238
239static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr)
240{
241 struct list_head *lhandle;
242
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;
251
252 /* We only take memory from free RAM */
253 if (desc->type != EFI_CONVENTIONAL_MEMORY)
254 continue;
255
256 /* Out of bounds for max_addr */
257 if ((ret + len) > max_addr)
258 continue;
259
260 /* Out of bounds for upper map limit */
261 if ((ret + len) > desc_end)
262 continue;
263
264 /* Out of bounds for lower map limit */
265 if (ret < desc->physical_start)
266 continue;
267
268 /* Return the highest address in this map within bounds */
269 return ret;
270 }
271
272 return 0;
273}
274
275efi_status_t efi_allocate_pages(int type, int memory_type,
276 unsigned long pages, uint64_t *memory)
277{
278 u64 len = pages << EFI_PAGE_SHIFT;
279 efi_status_t r = EFI_SUCCESS;
280 uint64_t addr;
281
282 switch (type) {
283 case 0:
284 /* Any page */
dede284d 285 addr = efi_find_free_memory(len, gd->start_addr_sp);
5d00995c
AG
286 if (!addr) {
287 r = EFI_NOT_FOUND;
288 break;
289 }
290 break;
291 case 1:
292 /* Max address */
293 addr = efi_find_free_memory(len, *memory);
294 if (!addr) {
295 r = EFI_NOT_FOUND;
296 break;
297 }
298 break;
299 case 2:
300 /* Exact address, reserve it. The addr is already in *memory. */
301 addr = *memory;
302 break;
303 default:
304 /* UEFI doesn't specify other allocation types */
305 r = EFI_INVALID_PARAMETER;
306 break;
307 }
308
309 if (r == EFI_SUCCESS) {
310 uint64_t ret;
311
312 /* Reserve that map in our memory maps */
313 ret = efi_add_memory_map(addr, pages, memory_type, true);
314 if (ret == addr) {
315 *memory = addr;
316 } else {
317 /* Map would overlap, bail out */
318 r = EFI_OUT_OF_RESOURCES;
319 }
320 }
321
322 return r;
323}
324
325void *efi_alloc(uint64_t len, int memory_type)
326{
327 uint64_t ret = 0;
328 uint64_t pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
329 efi_status_t r;
330
331 r = efi_allocate_pages(0, memory_type, pages, &ret);
332 if (r == EFI_SUCCESS)
333 return (void*)(uintptr_t)ret;
334
335 return NULL;
336}
337
338efi_status_t efi_free_pages(uint64_t memory, unsigned long pages)
339{
b61d857b
SB
340 uint64_t r = 0;
341
342 r = efi_add_memory_map(memory, pages, EFI_CONVENTIONAL_MEMORY, false);
343 /* Merging of adjacent free regions is missing */
344
345 if (r == memory)
346 return EFI_SUCCESS;
347
348 return EFI_NOT_FOUND;
5d00995c
AG
349}
350
ead1274b
SB
351efi_status_t efi_allocate_pool(int pool_type, unsigned long size,
352 void **buffer)
353{
354 efi_status_t r;
355 efi_physical_addr_t t;
42417bc8
SB
356 u64 num_pages = (size + sizeof(u64) + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
357
358 if (size == 0) {
359 *buffer = NULL;
360 return EFI_SUCCESS;
361 }
ead1274b
SB
362
363 r = efi_allocate_pages(0, pool_type, num_pages, &t);
42417bc8
SB
364
365 if (r == EFI_SUCCESS) {
366 struct efi_pool_allocation *alloc = (void *)(uintptr_t)t;
367 alloc->num_pages = num_pages;
368 *buffer = alloc->data;
369 }
370
371 return r;
372}
373
374efi_status_t efi_free_pool(void *buffer)
375{
376 efi_status_t r;
377 struct efi_pool_allocation *alloc;
378
379 alloc = container_of(buffer, struct efi_pool_allocation, data);
380 /* Sanity check, was the supplied address returned by allocate_pool */
381 assert(((uintptr_t)alloc & EFI_PAGE_MASK) == 0);
382
383 r = efi_free_pages((uintptr_t)alloc, alloc->num_pages);
ead1274b
SB
384
385 return r;
386}
387
5d00995c
AG
388efi_status_t efi_get_memory_map(unsigned long *memory_map_size,
389 struct efi_mem_desc *memory_map,
390 unsigned long *map_key,
391 unsigned long *descriptor_size,
392 uint32_t *descriptor_version)
393{
394 ulong map_size = 0;
cee752fa 395 int map_entries = 0;
5d00995c 396 struct list_head *lhandle;
bdf5c1b3 397 unsigned long provided_map_size = *memory_map_size;
5d00995c
AG
398
399 list_for_each(lhandle, &efi_mem)
cee752fa
AG
400 map_entries++;
401
402 map_size = map_entries * sizeof(struct efi_mem_desc);
5d00995c
AG
403
404 *memory_map_size = map_size;
405
406 if (descriptor_size)
407 *descriptor_size = sizeof(struct efi_mem_desc);
408
4c02c11d
MYK
409 if (descriptor_version)
410 *descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION;
411
bdf5c1b3 412 if (provided_map_size < map_size)
5d00995c
AG
413 return EFI_BUFFER_TOO_SMALL;
414
415 /* Copy list into array */
416 if (memory_map) {
cee752fa
AG
417 /* Return the list in ascending order */
418 memory_map = &memory_map[map_entries - 1];
5d00995c
AG
419 list_for_each(lhandle, &efi_mem) {
420 struct efi_mem_list *lmem;
421
422 lmem = list_entry(lhandle, struct efi_mem_list, link);
423 *memory_map = lmem->desc;
cee752fa 424 memory_map--;
5d00995c
AG
425 }
426 }
427
428 return EFI_SUCCESS;
429}
430
431int efi_memory_init(void)
432{
dede284d
AF
433 unsigned long runtime_start, runtime_end, runtime_pages;
434 unsigned long uboot_start, uboot_pages;
435 unsigned long uboot_stack_size = 16 * 1024 * 1024;
5d00995c
AG
436 int i;
437
438 /* Add RAM */
439 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
440 u64 ram_start = gd->bd->bi_dram[i].start;
441 u64 ram_size = gd->bd->bi_dram[i].size;
442 u64 start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
443 u64 pages = (ram_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
444
445 efi_add_memory_map(start, pages, EFI_CONVENTIONAL_MEMORY,
446 false);
447 }
448
449 /* Add U-Boot */
450 uboot_start = (gd->start_addr_sp - uboot_stack_size) & ~EFI_PAGE_MASK;
451 uboot_pages = (gd->ram_top - uboot_start) >> EFI_PAGE_SHIFT;
452 efi_add_memory_map(uboot_start, uboot_pages, EFI_LOADER_DATA, false);
453
454 /* Add Runtime Services */
455 runtime_start = (ulong)&__efi_runtime_start & ~EFI_PAGE_MASK;
456 runtime_end = (ulong)&__efi_runtime_stop;
457 runtime_end = (runtime_end + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
458 runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT;
459 efi_add_memory_map(runtime_start, runtime_pages,
460 EFI_RUNTIME_SERVICES_CODE, false);
461
51735ae0
AG
462#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
463 /* Request a 32bit 64MB bounce buffer region */
464 uint64_t efi_bounce_buffer_addr = 0xffffffff;
465
466 if (efi_allocate_pages(1, EFI_LOADER_DATA,
467 (64 * 1024 * 1024) >> EFI_PAGE_SHIFT,
468 &efi_bounce_buffer_addr) != EFI_SUCCESS)
469 return -1;
470
471 efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr;
472#endif
473
5d00995c
AG
474 return 0;
475}