efi_loader: Change return type of efi_add_memory_map()
[oweals/u-boot.git] / lib / efi_loader / efi_memory.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  EFI application memory management
4  *
5  *  Copyright (c) 2016 Alexander Graf
6  */
7
8 #include <common.h>
9 #include <efi_loader.h>
10 #include <malloc.h>
11 #include <mapmem.h>
12 #include <watchdog.h>
13 #include <linux/list_sort.h>
14 #include <linux/sizes.h>
15
16 DECLARE_GLOBAL_DATA_PTR;
17
18 /* Magic number identifying memory allocated from pool */
19 #define EFI_ALLOC_POOL_MAGIC 0x1fe67ddf6491caa2
20
21 efi_uintn_t efi_memory_map_key;
22
23 struct efi_mem_list {
24         struct list_head link;
25         struct efi_mem_desc desc;
26 };
27
28 #define EFI_CARVE_NO_OVERLAP            -1
29 #define EFI_CARVE_LOOP_AGAIN            -2
30 #define EFI_CARVE_OVERLAPS_NONRAM       -3
31
32 /* This list contains all memory map items */
33 LIST_HEAD(efi_mem);
34
35 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
36 void *efi_bounce_buffer;
37 #endif
38
39 /**
40  * efi_pool_allocation - memory block allocated from pool
41  *
42  * @num_pages:  number of pages allocated
43  * @checksum:   checksum
44  *
45  * U-Boot services each EFI AllocatePool request as a separate
46  * (multiple) page allocation.  We have to track the number of pages
47  * to be able to free the correct amount later.
48  * EFI requires 8 byte alignment for pool allocations, so we can
49  * prepend each allocation with an 64 bit header tracking the
50  * allocation size, and hand out the remainder to the caller.
51  */
52 struct efi_pool_allocation {
53         u64 num_pages;
54         u64 checksum;
55         char data[] __aligned(ARCH_DMA_MINALIGN);
56 };
57
58 /**
59  * checksum() - calculate checksum for memory allocated from pool
60  *
61  * @alloc:      allocation header
62  * Return:      checksum, always non-zero
63  */
64 static u64 checksum(struct efi_pool_allocation *alloc)
65 {
66         u64 addr = (uintptr_t)alloc;
67         u64 ret = (addr >> 32) ^ (addr << 32) ^ alloc->num_pages ^
68                   EFI_ALLOC_POOL_MAGIC;
69         if (!ret)
70                 ++ret;
71         return ret;
72 }
73
74 /*
75  * Sorts the memory list from highest address to lowest address
76  *
77  * When allocating memory we should always start from the highest
78  * address chunk, so sort the memory list such that the first list
79  * iterator gets the highest address and goes lower from there.
80  */
81 static int efi_mem_cmp(void *priv, struct list_head *a, struct list_head *b)
82 {
83         struct efi_mem_list *mema = list_entry(a, struct efi_mem_list, link);
84         struct efi_mem_list *memb = list_entry(b, struct efi_mem_list, link);
85
86         if (mema->desc.physical_start == memb->desc.physical_start)
87                 return 0;
88         else if (mema->desc.physical_start < memb->desc.physical_start)
89                 return 1;
90         else
91                 return -1;
92 }
93
94 static uint64_t desc_get_end(struct efi_mem_desc *desc)
95 {
96         return desc->physical_start + (desc->num_pages << EFI_PAGE_SHIFT);
97 }
98
99 static void efi_mem_sort(void)
100 {
101         struct list_head *lhandle;
102         struct efi_mem_list *prevmem = NULL;
103         bool merge_again = true;
104
105         list_sort(NULL, &efi_mem, efi_mem_cmp);
106
107         /* Now merge entries that can be merged */
108         while (merge_again) {
109                 merge_again = false;
110                 list_for_each(lhandle, &efi_mem) {
111                         struct efi_mem_list *lmem;
112                         struct efi_mem_desc *prev = &prevmem->desc;
113                         struct efi_mem_desc *cur;
114                         uint64_t pages;
115
116                         lmem = list_entry(lhandle, struct efi_mem_list, link);
117                         if (!prevmem) {
118                                 prevmem = lmem;
119                                 continue;
120                         }
121
122                         cur = &lmem->desc;
123
124                         if ((desc_get_end(cur) == prev->physical_start) &&
125                             (prev->type == cur->type) &&
126                             (prev->attribute == cur->attribute)) {
127                                 /* There is an existing map before, reuse it */
128                                 pages = cur->num_pages;
129                                 prev->num_pages += pages;
130                                 prev->physical_start -= pages << EFI_PAGE_SHIFT;
131                                 prev->virtual_start -= pages << EFI_PAGE_SHIFT;
132                                 list_del(&lmem->link);
133                                 free(lmem);
134
135                                 merge_again = true;
136                                 break;
137                         }
138
139                         prevmem = lmem;
140                 }
141         }
142 }
143
144 /** efi_mem_carve_out - unmap memory region
145  *
146  * @map:                memory map
147  * @carve_desc:         memory region to unmap
148  * @overlap_only_ram:   the carved out region may only overlap RAM
149  * Return Value:        the number of overlapping pages which have been
150  *                      removed from the map,
151  *                      EFI_CARVE_NO_OVERLAP, if the regions don't overlap,
152  *                      EFI_CARVE_OVERLAPS_NONRAM, if the carve and map overlap,
153  *                      and the map contains anything but free ram
154  *                      (only when overlap_only_ram is true),
155  *                      EFI_CARVE_LOOP_AGAIN, if the mapping list should be
156  *                      traversed again, as it has been altered.
157  *
158  * Unmaps all memory occupied by the carve_desc region from the list entry
159  * pointed to by map.
160  *
161  * In case of EFI_CARVE_OVERLAPS_NONRAM it is the callers responsibility
162  * to re-add the already carved out pages to the mapping.
163  */
164 static s64 efi_mem_carve_out(struct efi_mem_list *map,
165                              struct efi_mem_desc *carve_desc,
166                              bool overlap_only_ram)
167 {
168         struct efi_mem_list *newmap;
169         struct efi_mem_desc *map_desc = &map->desc;
170         uint64_t map_start = map_desc->physical_start;
171         uint64_t map_end = map_start + (map_desc->num_pages << EFI_PAGE_SHIFT);
172         uint64_t carve_start = carve_desc->physical_start;
173         uint64_t carve_end = carve_start +
174                              (carve_desc->num_pages << EFI_PAGE_SHIFT);
175
176         /* check whether we're overlapping */
177         if ((carve_end <= map_start) || (carve_start >= map_end))
178                 return EFI_CARVE_NO_OVERLAP;
179
180         /* We're overlapping with non-RAM, warn the caller if desired */
181         if (overlap_only_ram && (map_desc->type != EFI_CONVENTIONAL_MEMORY))
182                 return EFI_CARVE_OVERLAPS_NONRAM;
183
184         /* Sanitize carve_start and carve_end to lie within our bounds */
185         carve_start = max(carve_start, map_start);
186         carve_end = min(carve_end, map_end);
187
188         /* Carving at the beginning of our map? Just move it! */
189         if (carve_start == map_start) {
190                 if (map_end == carve_end) {
191                         /* Full overlap, just remove map */
192                         list_del(&map->link);
193                         free(map);
194                 } else {
195                         map->desc.physical_start = carve_end;
196                         map->desc.virtual_start = carve_end;
197                         map->desc.num_pages = (map_end - carve_end)
198                                               >> EFI_PAGE_SHIFT;
199                 }
200
201                 return (carve_end - carve_start) >> EFI_PAGE_SHIFT;
202         }
203
204         /*
205          * Overlapping maps, just split the list map at carve_start,
206          * it will get moved or removed in the next iteration.
207          *
208          * [ map_desc |__carve_start__| newmap ]
209          */
210
211         /* Create a new map from [ carve_start ... map_end ] */
212         newmap = calloc(1, sizeof(*newmap));
213         newmap->desc = map->desc;
214         newmap->desc.physical_start = carve_start;
215         newmap->desc.virtual_start = carve_start;
216         newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT;
217         /* Insert before current entry (descending address order) */
218         list_add_tail(&newmap->link, &map->link);
219
220         /* Shrink the map to [ map_start ... carve_start ] */
221         map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT;
222
223         return EFI_CARVE_LOOP_AGAIN;
224 }
225
226 /**
227  * efi_add_memory_map() - add memory area to the memory map
228  *
229  * @start:              start address, must be a multiple of EFI_PAGE_SIZE
230  * @pages:              number of pages to add
231  * @memory_type:        type of memory added
232  * @overlap_only_ram:   the memory area must overlap existing
233  * Return:              status code
234  */
235 efi_status_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type,
236                                 bool overlap_only_ram)
237 {
238         struct list_head *lhandle;
239         struct efi_mem_list *newlist;
240         bool carve_again;
241         uint64_t carved_pages = 0;
242         struct efi_event *evt;
243
244         EFI_PRINT("%s: 0x%llx 0x%llx %d %s\n", __func__,
245                   start, pages, memory_type, overlap_only_ram ? "yes" : "no");
246
247         if (memory_type >= EFI_MAX_MEMORY_TYPE)
248                 return EFI_INVALID_PARAMETER;
249
250         if (!pages)
251                 return EFI_SUCCESS;
252
253         ++efi_memory_map_key;
254         newlist = calloc(1, sizeof(*newlist));
255         newlist->desc.type = memory_type;
256         newlist->desc.physical_start = start;
257         newlist->desc.virtual_start = start;
258         newlist->desc.num_pages = pages;
259
260         switch (memory_type) {
261         case EFI_RUNTIME_SERVICES_CODE:
262         case EFI_RUNTIME_SERVICES_DATA:
263                 newlist->desc.attribute = EFI_MEMORY_WB | EFI_MEMORY_RUNTIME;
264                 break;
265         case EFI_MMAP_IO:
266                 newlist->desc.attribute = EFI_MEMORY_RUNTIME;
267                 break;
268         default:
269                 newlist->desc.attribute = EFI_MEMORY_WB;
270                 break;
271         }
272
273         /* Add our new map */
274         do {
275                 carve_again = false;
276                 list_for_each(lhandle, &efi_mem) {
277                         struct efi_mem_list *lmem;
278                         s64 r;
279
280                         lmem = list_entry(lhandle, struct efi_mem_list, link);
281                         r = efi_mem_carve_out(lmem, &newlist->desc,
282                                               overlap_only_ram);
283                         switch (r) {
284                         case EFI_CARVE_OVERLAPS_NONRAM:
285                                 /*
286                                  * The user requested to only have RAM overlaps,
287                                  * but we hit a non-RAM region. Error out.
288                                  */
289                                 return EFI_NO_MAPPING;
290                         case EFI_CARVE_NO_OVERLAP:
291                                 /* Just ignore this list entry */
292                                 break;
293                         case EFI_CARVE_LOOP_AGAIN:
294                                 /*
295                                  * We split an entry, but need to loop through
296                                  * the list again to actually carve it.
297                                  */
298                                 carve_again = true;
299                                 break;
300                         default:
301                                 /* We carved a number of pages */
302                                 carved_pages += r;
303                                 carve_again = true;
304                                 break;
305                         }
306
307                         if (carve_again) {
308                                 /* The list changed, we need to start over */
309                                 break;
310                         }
311                 }
312         } while (carve_again);
313
314         if (overlap_only_ram && (carved_pages != pages)) {
315                 /*
316                  * The payload wanted to have RAM overlaps, but we overlapped
317                  * with an unallocated region. Error out.
318                  */
319                 return EFI_NO_MAPPING;
320         }
321
322         /* Add our new map */
323         list_add_tail(&newlist->link, &efi_mem);
324
325         /* And make sure memory is listed in descending order */
326         efi_mem_sort();
327
328         /* Notify that the memory map was changed */
329         list_for_each_entry(evt, &efi_events, link) {
330                 if (evt->group &&
331                     !guidcmp(evt->group,
332                              &efi_guid_event_group_memory_map_change)) {
333                         efi_signal_event(evt);
334                         break;
335                 }
336         }
337
338         return EFI_SUCCESS;
339 }
340
341 /**
342  * efi_check_allocated() - validate address to be freed
343  *
344  * Check that the address is within allocated memory:
345  *
346  * * The address must be in a range of the memory map.
347  * * The address may not point to EFI_CONVENTIONAL_MEMORY.
348  *
349  * Page alignment is not checked as this is not a requirement of
350  * efi_free_pool().
351  *
352  * @addr:               address of page to be freed
353  * @must_be_allocated:  return success if the page is allocated
354  * Return:              status code
355  */
356 static efi_status_t efi_check_allocated(u64 addr, bool must_be_allocated)
357 {
358         struct efi_mem_list *item;
359
360         list_for_each_entry(item, &efi_mem, link) {
361                 u64 start = item->desc.physical_start;
362                 u64 end = start + (item->desc.num_pages << EFI_PAGE_SHIFT);
363
364                 if (addr >= start && addr < end) {
365                         if (must_be_allocated ^
366                             (item->desc.type == EFI_CONVENTIONAL_MEMORY))
367                                 return EFI_SUCCESS;
368                         else
369                                 return EFI_NOT_FOUND;
370                 }
371         }
372
373         return EFI_NOT_FOUND;
374 }
375
376 static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr)
377 {
378         struct list_head *lhandle;
379
380         /*
381          * Prealign input max address, so we simplify our matching
382          * logic below and can just reuse it as return pointer.
383          */
384         max_addr &= ~EFI_PAGE_MASK;
385
386         list_for_each(lhandle, &efi_mem) {
387                 struct efi_mem_list *lmem = list_entry(lhandle,
388                         struct efi_mem_list, link);
389                 struct efi_mem_desc *desc = &lmem->desc;
390                 uint64_t desc_len = desc->num_pages << EFI_PAGE_SHIFT;
391                 uint64_t desc_end = desc->physical_start + desc_len;
392                 uint64_t curmax = min(max_addr, desc_end);
393                 uint64_t ret = curmax - len;
394
395                 /* We only take memory from free RAM */
396                 if (desc->type != EFI_CONVENTIONAL_MEMORY)
397                         continue;
398
399                 /* Out of bounds for max_addr */
400                 if ((ret + len) > max_addr)
401                         continue;
402
403                 /* Out of bounds for upper map limit */
404                 if ((ret + len) > desc_end)
405                         continue;
406
407                 /* Out of bounds for lower map limit */
408                 if (ret < desc->physical_start)
409                         continue;
410
411                 /* Return the highest address in this map within bounds */
412                 return ret;
413         }
414
415         return 0;
416 }
417
418 /*
419  * Allocate memory pages.
420  *
421  * @type                type of allocation to be performed
422  * @memory_type         usage type of the allocated memory
423  * @pages               number of pages to be allocated
424  * @memory              allocated memory
425  * @return              status code
426  */
427 efi_status_t efi_allocate_pages(int type, int memory_type,
428                                 efi_uintn_t pages, uint64_t *memory)
429 {
430         u64 len = pages << EFI_PAGE_SHIFT;
431         efi_status_t ret;
432         uint64_t addr;
433
434         /* Check import parameters */
435         if (memory_type >= EFI_PERSISTENT_MEMORY_TYPE &&
436             memory_type <= 0x6FFFFFFF)
437                 return EFI_INVALID_PARAMETER;
438         if (!memory)
439                 return EFI_INVALID_PARAMETER;
440
441         switch (type) {
442         case EFI_ALLOCATE_ANY_PAGES:
443                 /* Any page */
444                 addr = efi_find_free_memory(len, -1ULL);
445                 if (!addr)
446                         return EFI_OUT_OF_RESOURCES;
447                 break;
448         case EFI_ALLOCATE_MAX_ADDRESS:
449                 /* Max address */
450                 addr = efi_find_free_memory(len, *memory);
451                 if (!addr)
452                         return EFI_OUT_OF_RESOURCES;
453                 break;
454         case EFI_ALLOCATE_ADDRESS:
455                 /* Exact address, reserve it. The addr is already in *memory. */
456                 ret = efi_check_allocated(*memory, false);
457                 if (ret != EFI_SUCCESS)
458                         return EFI_NOT_FOUND;
459                 addr = *memory;
460                 break;
461         default:
462                 /* UEFI doesn't specify other allocation types */
463                 return EFI_INVALID_PARAMETER;
464         }
465
466         /* Reserve that map in our memory maps */
467         if (efi_add_memory_map(addr, pages, memory_type, true) != EFI_SUCCESS)
468                 /* Map would overlap, bail out */
469                 return  EFI_OUT_OF_RESOURCES;
470
471         *memory = addr;
472
473         return EFI_SUCCESS;
474 }
475
476 void *efi_alloc(uint64_t len, int memory_type)
477 {
478         uint64_t ret = 0;
479         uint64_t pages = efi_size_in_pages(len);
480         efi_status_t r;
481
482         r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type, pages,
483                                &ret);
484         if (r == EFI_SUCCESS)
485                 return (void*)(uintptr_t)ret;
486
487         return NULL;
488 }
489
490 /**
491  * efi_free_pages() - free memory pages
492  *
493  * @memory:     start of the memory area to be freed
494  * @pages:      number of pages to be freed
495  * Return:      status code
496  */
497 efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages)
498 {
499         efi_status_t ret;
500
501         ret = efi_check_allocated(memory, true);
502         if (ret != EFI_SUCCESS)
503                 return ret;
504
505         /* Sanity check */
506         if (!memory || (memory & EFI_PAGE_MASK) || !pages) {
507                 printf("%s: illegal free 0x%llx, 0x%zx\n", __func__,
508                        memory, pages);
509                 return EFI_INVALID_PARAMETER;
510         }
511
512         ret = efi_add_memory_map(memory, pages, EFI_CONVENTIONAL_MEMORY, false);
513         /* Merging of adjacent free regions is missing */
514
515         if (ret != EFI_SUCCESS)
516                 return EFI_NOT_FOUND;
517
518         return ret;
519 }
520
521 /**
522  * efi_allocate_pool - allocate memory from pool
523  *
524  * @pool_type:  type of the pool from which memory is to be allocated
525  * @size:       number of bytes to be allocated
526  * @buffer:     allocated memory
527  * Return:      status code
528  */
529 efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size, void **buffer)
530 {
531         efi_status_t r;
532         u64 addr;
533         struct efi_pool_allocation *alloc;
534         u64 num_pages = efi_size_in_pages(size +
535                                           sizeof(struct efi_pool_allocation));
536
537         if (!buffer)
538                 return EFI_INVALID_PARAMETER;
539
540         if (size == 0) {
541                 *buffer = NULL;
542                 return EFI_SUCCESS;
543         }
544
545         r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, pool_type, num_pages,
546                                &addr);
547         if (r == EFI_SUCCESS) {
548                 alloc = (struct efi_pool_allocation *)(uintptr_t)addr;
549                 alloc->num_pages = num_pages;
550                 alloc->checksum = checksum(alloc);
551                 *buffer = alloc->data;
552         }
553
554         return r;
555 }
556
557 /**
558  * efi_free_pool() - free memory from pool
559  *
560  * @buffer:     start of memory to be freed
561  * Return:      status code
562  */
563 efi_status_t efi_free_pool(void *buffer)
564 {
565         efi_status_t ret;
566         struct efi_pool_allocation *alloc;
567
568         if (!buffer)
569                 return EFI_INVALID_PARAMETER;
570
571         ret = efi_check_allocated((uintptr_t)buffer, true);
572         if (ret != EFI_SUCCESS)
573                 return ret;
574
575         alloc = container_of(buffer, struct efi_pool_allocation, data);
576
577         /* Check that this memory was allocated by efi_allocate_pool() */
578         if (((uintptr_t)alloc & EFI_PAGE_MASK) ||
579             alloc->checksum != checksum(alloc)) {
580                 printf("%s: illegal free 0x%p\n", __func__, buffer);
581                 return EFI_INVALID_PARAMETER;
582         }
583         /* Avoid double free */
584         alloc->checksum = 0;
585
586         ret = efi_free_pages((uintptr_t)alloc, alloc->num_pages);
587
588         return ret;
589 }
590
591 /*
592  * Get map describing memory usage.
593  *
594  * @memory_map_size     on entry the size, in bytes, of the memory map buffer,
595  *                      on exit the size of the copied memory map
596  * @memory_map          buffer to which the memory map is written
597  * @map_key             key for the memory map
598  * @descriptor_size     size of an individual memory descriptor
599  * @descriptor_version  version number of the memory descriptor structure
600  * @return              status code
601  */
602 efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size,
603                                 struct efi_mem_desc *memory_map,
604                                 efi_uintn_t *map_key,
605                                 efi_uintn_t *descriptor_size,
606                                 uint32_t *descriptor_version)
607 {
608         efi_uintn_t map_size = 0;
609         int map_entries = 0;
610         struct list_head *lhandle;
611         efi_uintn_t provided_map_size;
612
613         if (!memory_map_size)
614                 return EFI_INVALID_PARAMETER;
615
616         provided_map_size = *memory_map_size;
617
618         list_for_each(lhandle, &efi_mem)
619                 map_entries++;
620
621         map_size = map_entries * sizeof(struct efi_mem_desc);
622
623         *memory_map_size = map_size;
624
625         if (provided_map_size < map_size)
626                 return EFI_BUFFER_TOO_SMALL;
627
628         if (!memory_map)
629                 return EFI_INVALID_PARAMETER;
630
631         if (descriptor_size)
632                 *descriptor_size = sizeof(struct efi_mem_desc);
633
634         if (descriptor_version)
635                 *descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION;
636
637         /* Copy list into array */
638         /* Return the list in ascending order */
639         memory_map = &memory_map[map_entries - 1];
640         list_for_each(lhandle, &efi_mem) {
641                 struct efi_mem_list *lmem;
642
643                 lmem = list_entry(lhandle, struct efi_mem_list, link);
644                 *memory_map = lmem->desc;
645                 memory_map--;
646         }
647
648         if (map_key)
649                 *map_key = efi_memory_map_key;
650
651         return EFI_SUCCESS;
652 }
653
654 __weak void efi_add_known_memory(void)
655 {
656         u64 ram_top = board_get_usable_ram_top(0) & ~EFI_PAGE_MASK;
657         int i;
658
659         /*
660          * ram_top is just outside mapped memory. So use an offset of one for
661          * mapping the sandbox address.
662          */
663         ram_top = (uintptr_t)map_sysmem(ram_top - 1, 0) + 1;
664
665         /* Fix for 32bit targets with ram_top at 4G */
666         if (!ram_top)
667                 ram_top = 0x100000000ULL;
668
669         /* Add RAM */
670         for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
671                 u64 ram_end, ram_start, pages;
672
673                 ram_start = (uintptr_t)map_sysmem(gd->bd->bi_dram[i].start, 0);
674                 ram_end = ram_start + gd->bd->bi_dram[i].size;
675
676                 /* Remove partial pages */
677                 ram_end &= ~EFI_PAGE_MASK;
678                 ram_start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
679
680                 if (ram_end <= ram_start) {
681                         /* Invalid mapping, keep going. */
682                         continue;
683                 }
684
685                 pages = (ram_end - ram_start) >> EFI_PAGE_SHIFT;
686
687                 efi_add_memory_map(ram_start, pages,
688                                    EFI_CONVENTIONAL_MEMORY, false);
689
690                 /*
691                  * Boards may indicate to the U-Boot memory core that they
692                  * can not support memory above ram_top. Let's honor this
693                  * in the efi_loader subsystem too by declaring any memory
694                  * above ram_top as "already occupied by firmware".
695                  */
696                 if (ram_top < ram_start) {
697                         /* ram_top is before this region, reserve all */
698                         efi_add_memory_map(ram_start, pages,
699                                            EFI_BOOT_SERVICES_DATA, true);
700                 } else if ((ram_top >= ram_start) && (ram_top < ram_end)) {
701                         /* ram_top is inside this region, reserve parts */
702                         pages = (ram_end - ram_top) >> EFI_PAGE_SHIFT;
703
704                         efi_add_memory_map(ram_top, pages,
705                                            EFI_BOOT_SERVICES_DATA, true);
706                 }
707         }
708 }
709
710 /* Add memory regions for U-Boot's memory and for the runtime services code */
711 static void add_u_boot_and_runtime(void)
712 {
713         unsigned long runtime_start, runtime_end, runtime_pages;
714         unsigned long runtime_mask = EFI_PAGE_MASK;
715         unsigned long uboot_start, uboot_pages;
716         unsigned long uboot_stack_size = 16 * 1024 * 1024;
717
718         /* Add U-Boot */
719         uboot_start = (gd->start_addr_sp - uboot_stack_size) & ~EFI_PAGE_MASK;
720         uboot_pages = (gd->ram_top - uboot_start) >> EFI_PAGE_SHIFT;
721         efi_add_memory_map(uboot_start, uboot_pages, EFI_LOADER_DATA, false);
722
723 #if defined(__aarch64__)
724         /*
725          * Runtime Services must be 64KiB aligned according to the
726          * "AArch64 Platforms" section in the UEFI spec (2.7+).
727          */
728
729         runtime_mask = SZ_64K - 1;
730 #endif
731
732         /*
733          * Add Runtime Services. We mark surrounding boottime code as runtime as
734          * well to fulfill the runtime alignment constraints but avoid padding.
735          */
736         runtime_start = (ulong)&__efi_runtime_start & ~runtime_mask;
737         runtime_end = (ulong)&__efi_runtime_stop;
738         runtime_end = (runtime_end + runtime_mask) & ~runtime_mask;
739         runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT;
740         efi_add_memory_map(runtime_start, runtime_pages,
741                            EFI_RUNTIME_SERVICES_CODE, false);
742 }
743
744 int efi_memory_init(void)
745 {
746         efi_add_known_memory();
747
748         if (!IS_ENABLED(CONFIG_SANDBOX))
749                 add_u_boot_and_runtime();
750
751 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
752         /* Request a 32bit 64MB bounce buffer region */
753         uint64_t efi_bounce_buffer_addr = 0xffffffff;
754
755         if (efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, EFI_LOADER_DATA,
756                                (64 * 1024 * 1024) >> EFI_PAGE_SHIFT,
757                                &efi_bounce_buffer_addr) != EFI_SUCCESS)
758                 return -1;
759
760         efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr;
761 #endif
762
763         return 0;
764 }