1
2
3
4
5
6
7
8#include <common.h>
9#include <efi_loader.h>
10#include <init.h>
11#include <malloc.h>
12#include <mapmem.h>
13#include <watchdog.h>
14#include <asm/cache.h>
15#include <asm/global_data.h>
16#include <linux/list_sort.h>
17#include <linux/sizes.h>
18
19DECLARE_GLOBAL_DATA_PTR;
20
21
22#define EFI_ALLOC_POOL_MAGIC 0x1fe67ddf6491caa2
23
24efi_uintn_t efi_memory_map_key;
25
26struct efi_mem_list {
27 struct list_head link;
28 struct efi_mem_desc desc;
29};
30
31#define EFI_CARVE_NO_OVERLAP -1
32#define EFI_CARVE_LOOP_AGAIN -2
33#define EFI_CARVE_OVERLAPS_NONRAM -3
34
35
36LIST_HEAD(efi_mem);
37
38#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
39void *efi_bounce_buffer;
40#endif
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59struct efi_pool_allocation {
60 u64 num_pages;
61 u64 checksum;
62 char data[] __aligned(ARCH_DMA_MINALIGN);
63};
64
65
66
67
68
69
70
71static u64 checksum(struct efi_pool_allocation *alloc)
72{
73 u64 addr = (uintptr_t)alloc;
74 u64 ret = (addr >> 32) ^ (addr << 32) ^ alloc->num_pages ^
75 EFI_ALLOC_POOL_MAGIC;
76 if (!ret)
77 ++ret;
78 return ret;
79}
80
81
82
83
84
85
86
87
88static int efi_mem_cmp(void *priv, struct list_head *a, struct list_head *b)
89{
90 struct efi_mem_list *mema = list_entry(a, struct efi_mem_list, link);
91 struct efi_mem_list *memb = list_entry(b, struct efi_mem_list, link);
92
93 if (mema->desc.physical_start == memb->desc.physical_start)
94 return 0;
95 else if (mema->desc.physical_start < memb->desc.physical_start)
96 return 1;
97 else
98 return -1;
99}
100
101static uint64_t desc_get_end(struct efi_mem_desc *desc)
102{
103 return desc->physical_start + (desc->num_pages << EFI_PAGE_SHIFT);
104}
105
106static void efi_mem_sort(void)
107{
108 struct list_head *lhandle;
109 struct efi_mem_list *prevmem = NULL;
110 bool merge_again = true;
111
112 list_sort(NULL, &efi_mem, efi_mem_cmp);
113
114
115 while (merge_again) {
116 merge_again = false;
117 list_for_each(lhandle, &efi_mem) {
118 struct efi_mem_list *lmem;
119 struct efi_mem_desc *prev = &prevmem->desc;
120 struct efi_mem_desc *cur;
121 uint64_t pages;
122
123 lmem = list_entry(lhandle, struct efi_mem_list, link);
124 if (!prevmem) {
125 prevmem = lmem;
126 continue;
127 }
128
129 cur = &lmem->desc;
130
131 if ((desc_get_end(cur) == prev->physical_start) &&
132 (prev->type == cur->type) &&
133 (prev->attribute == cur->attribute)) {
134
135 pages = cur->num_pages;
136 prev->num_pages += pages;
137 prev->physical_start -= pages << EFI_PAGE_SHIFT;
138 prev->virtual_start -= pages << EFI_PAGE_SHIFT;
139 list_del(&lmem->link);
140 free(lmem);
141
142 merge_again = true;
143 break;
144 }
145
146 prevmem = lmem;
147 }
148 }
149}
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171static s64 efi_mem_carve_out(struct efi_mem_list *map,
172 struct efi_mem_desc *carve_desc,
173 bool overlap_only_ram)
174{
175 struct efi_mem_list *newmap;
176 struct efi_mem_desc *map_desc = &map->desc;
177 uint64_t map_start = map_desc->physical_start;
178 uint64_t map_end = map_start + (map_desc->num_pages << EFI_PAGE_SHIFT);
179 uint64_t carve_start = carve_desc->physical_start;
180 uint64_t carve_end = carve_start +
181 (carve_desc->num_pages << EFI_PAGE_SHIFT);
182
183
184 if ((carve_end <= map_start) || (carve_start >= map_end))
185 return EFI_CARVE_NO_OVERLAP;
186
187
188 if (overlap_only_ram && (map_desc->type != EFI_CONVENTIONAL_MEMORY))
189 return EFI_CARVE_OVERLAPS_NONRAM;
190
191
192 carve_start = max(carve_start, map_start);
193 carve_end = min(carve_end, map_end);
194
195
196 if (carve_start == map_start) {
197 if (map_end == carve_end) {
198
199 list_del(&map->link);
200 free(map);
201 } else {
202 map->desc.physical_start = carve_end;
203 map->desc.virtual_start = carve_end;
204 map->desc.num_pages = (map_end - carve_end)
205 >> EFI_PAGE_SHIFT;
206 }
207
208 return (carve_end - carve_start) >> EFI_PAGE_SHIFT;
209 }
210
211
212
213
214
215
216
217
218
219 newmap = calloc(1, sizeof(*newmap));
220 newmap->desc = map->desc;
221 newmap->desc.physical_start = carve_start;
222 newmap->desc.virtual_start = carve_start;
223 newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT;
224
225 list_add_tail(&newmap->link, &map->link);
226
227
228 map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT;
229
230 return EFI_CARVE_LOOP_AGAIN;
231}
232
233
234
235
236
237
238
239
240
241
242static efi_status_t efi_add_memory_map_pg(u64 start, u64 pages,
243 int memory_type,
244 bool overlap_only_ram)
245{
246 struct list_head *lhandle;
247 struct efi_mem_list *newlist;
248 bool carve_again;
249 uint64_t carved_pages = 0;
250 struct efi_event *evt;
251
252 EFI_PRINT("%s: 0x%llx 0x%llx %d %s\n", __func__,
253 start, pages, memory_type, overlap_only_ram ? "yes" : "no");
254
255 if (memory_type >= EFI_MAX_MEMORY_TYPE)
256 return EFI_INVALID_PARAMETER;
257
258 if (!pages)
259 return EFI_SUCCESS;
260
261 ++efi_memory_map_key;
262 newlist = calloc(1, sizeof(*newlist));
263 newlist->desc.type = memory_type;
264 newlist->desc.physical_start = start;
265 newlist->desc.virtual_start = start;
266 newlist->desc.num_pages = pages;
267
268 switch (memory_type) {
269 case EFI_RUNTIME_SERVICES_CODE:
270 case EFI_RUNTIME_SERVICES_DATA:
271 newlist->desc.attribute = EFI_MEMORY_WB | EFI_MEMORY_RUNTIME;
272 break;
273 case EFI_MMAP_IO:
274 newlist->desc.attribute = EFI_MEMORY_RUNTIME;
275 break;
276 default:
277 newlist->desc.attribute = EFI_MEMORY_WB;
278 break;
279 }
280
281
282 do {
283 carve_again = false;
284 list_for_each(lhandle, &efi_mem) {
285 struct efi_mem_list *lmem;
286 s64 r;
287
288 lmem = list_entry(lhandle, struct efi_mem_list, link);
289 r = efi_mem_carve_out(lmem, &newlist->desc,
290 overlap_only_ram);
291 switch (r) {
292 case EFI_CARVE_OVERLAPS_NONRAM:
293
294
295
296
297 return EFI_NO_MAPPING;
298 case EFI_CARVE_NO_OVERLAP:
299
300 break;
301 case EFI_CARVE_LOOP_AGAIN:
302
303
304
305
306 carve_again = true;
307 break;
308 default:
309
310 carved_pages += r;
311 carve_again = true;
312 break;
313 }
314
315 if (carve_again) {
316
317 break;
318 }
319 }
320 } while (carve_again);
321
322 if (overlap_only_ram && (carved_pages != pages)) {
323
324
325
326
327 return EFI_NO_MAPPING;
328 }
329
330
331 list_add_tail(&newlist->link, &efi_mem);
332
333
334 efi_mem_sort();
335
336
337 list_for_each_entry(evt, &efi_events, link) {
338 if (evt->group &&
339 !guidcmp(evt->group,
340 &efi_guid_event_group_memory_map_change)) {
341 efi_signal_event(evt);
342 break;
343 }
344 }
345
346 return EFI_SUCCESS;
347}
348
349
350
351
352
353
354
355
356
357
358
359
360
361efi_status_t efi_add_memory_map(u64 start, u64 size, int memory_type)
362{
363 u64 pages;
364
365 pages = efi_size_in_pages(size + (start & EFI_PAGE_MASK));
366 start &= ~EFI_PAGE_MASK;
367
368 return efi_add_memory_map_pg(start, pages, memory_type, false);
369}
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386static efi_status_t efi_check_allocated(u64 addr, bool must_be_allocated)
387{
388 struct efi_mem_list *item;
389
390 list_for_each_entry(item, &efi_mem, link) {
391 u64 start = item->desc.physical_start;
392 u64 end = start + (item->desc.num_pages << EFI_PAGE_SHIFT);
393
394 if (addr >= start && addr < end) {
395 if (must_be_allocated ^
396 (item->desc.type == EFI_CONVENTIONAL_MEMORY))
397 return EFI_SUCCESS;
398 else
399 return EFI_NOT_FOUND;
400 }
401 }
402
403 return EFI_NOT_FOUND;
404}
405
406static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr)
407{
408 struct list_head *lhandle;
409
410
411
412
413
414 max_addr &= ~EFI_PAGE_MASK;
415
416 list_for_each(lhandle, &efi_mem) {
417 struct efi_mem_list *lmem = list_entry(lhandle,
418 struct efi_mem_list, link);
419 struct efi_mem_desc *desc = &lmem->desc;
420 uint64_t desc_len = desc->num_pages << EFI_PAGE_SHIFT;
421 uint64_t desc_end = desc->physical_start + desc_len;
422 uint64_t curmax = min(max_addr, desc_end);
423 uint64_t ret = curmax - len;
424
425
426 if (desc->type != EFI_CONVENTIONAL_MEMORY)
427 continue;
428
429
430 if ((ret + len) > max_addr)
431 continue;
432
433
434 if ((ret + len) > desc_end)
435 continue;
436
437
438 if (ret < desc->physical_start)
439 continue;
440
441
442 return ret;
443 }
444
445 return 0;
446}
447
448
449
450
451
452
453
454
455
456
457efi_status_t efi_allocate_pages(enum efi_allocate_type type,
458 enum efi_memory_type memory_type,
459 efi_uintn_t pages, uint64_t *memory)
460{
461 u64 len = pages << EFI_PAGE_SHIFT;
462 efi_status_t ret;
463 uint64_t addr;
464
465
466 if (memory_type >= EFI_PERSISTENT_MEMORY_TYPE &&
467 memory_type <= 0x6FFFFFFF)
468 return EFI_INVALID_PARAMETER;
469 if (!memory)
470 return EFI_INVALID_PARAMETER;
471
472 switch (type) {
473 case EFI_ALLOCATE_ANY_PAGES:
474
475 addr = efi_find_free_memory(len, -1ULL);
476 if (!addr)
477 return EFI_OUT_OF_RESOURCES;
478 break;
479 case EFI_ALLOCATE_MAX_ADDRESS:
480
481 addr = efi_find_free_memory(len, *memory);
482 if (!addr)
483 return EFI_OUT_OF_RESOURCES;
484 break;
485 case EFI_ALLOCATE_ADDRESS:
486
487 ret = efi_check_allocated(*memory, false);
488 if (ret != EFI_SUCCESS)
489 return EFI_NOT_FOUND;
490 addr = *memory;
491 break;
492 default:
493
494 return EFI_INVALID_PARAMETER;
495 }
496
497
498 ret = efi_add_memory_map_pg(addr, pages, memory_type, true);
499 if (ret != EFI_SUCCESS)
500
501 return EFI_OUT_OF_RESOURCES;
502
503 *memory = addr;
504
505 return EFI_SUCCESS;
506}
507
508void *efi_alloc(uint64_t len, int memory_type)
509{
510 uint64_t ret = 0;
511 uint64_t pages = efi_size_in_pages(len);
512 efi_status_t r;
513
514 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type, pages,
515 &ret);
516 if (r == EFI_SUCCESS)
517 return (void*)(uintptr_t)ret;
518
519 return NULL;
520}
521
522
523
524
525
526
527
528
529efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages)
530{
531 efi_status_t ret;
532
533 ret = efi_check_allocated(memory, true);
534 if (ret != EFI_SUCCESS)
535 return ret;
536
537
538 if (!memory || (memory & EFI_PAGE_MASK) || !pages) {
539 printf("%s: illegal free 0x%llx, 0x%zx\n", __func__,
540 memory, pages);
541 return EFI_INVALID_PARAMETER;
542 }
543
544 ret = efi_add_memory_map_pg(memory, pages, EFI_CONVENTIONAL_MEMORY,
545 false);
546 if (ret != EFI_SUCCESS)
547 return EFI_NOT_FOUND;
548
549 return ret;
550}
551
552
553
554
555
556
557
558
559
560void *efi_alloc_aligned_pages(u64 len, int memory_type, size_t align)
561{
562 u64 req_pages = efi_size_in_pages(len);
563 u64 true_pages = req_pages + efi_size_in_pages(align) - 1;
564 u64 free_pages;
565 u64 aligned_mem;
566 efi_status_t r;
567 u64 mem;
568
569
570 if (align & (align - 1))
571 return NULL;
572
573
574 if (true_pages < req_pages)
575 return NULL;
576
577 if (align < EFI_PAGE_SIZE) {
578 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type,
579 req_pages, &mem);
580 return (r == EFI_SUCCESS) ? (void *)(uintptr_t)mem : NULL;
581 }
582
583 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type,
584 true_pages, &mem);
585 if (r != EFI_SUCCESS)
586 return NULL;
587
588 aligned_mem = ALIGN(mem, align);
589
590 free_pages = efi_size_in_pages(aligned_mem - mem);
591 if (free_pages)
592 efi_free_pages(mem, free_pages);
593
594
595 free_pages = true_pages - (req_pages + free_pages);
596 if (free_pages) {
597 mem = aligned_mem + req_pages * EFI_PAGE_SIZE;
598 efi_free_pages(mem, free_pages);
599 }
600
601 return (void *)(uintptr_t)aligned_mem;
602}
603
604
605
606
607
608
609
610
611
612efi_status_t efi_allocate_pool(enum efi_memory_type pool_type, efi_uintn_t size, void **buffer)
613{
614 efi_status_t r;
615 u64 addr;
616 struct efi_pool_allocation *alloc;
617 u64 num_pages = efi_size_in_pages(size +
618 sizeof(struct efi_pool_allocation));
619
620 if (!buffer)
621 return EFI_INVALID_PARAMETER;
622
623 if (size == 0) {
624 *buffer = NULL;
625 return EFI_SUCCESS;
626 }
627
628 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, pool_type, num_pages,
629 &addr);
630 if (r == EFI_SUCCESS) {
631 alloc = (struct efi_pool_allocation *)(uintptr_t)addr;
632 alloc->num_pages = num_pages;
633 alloc->checksum = checksum(alloc);
634 *buffer = alloc->data;
635 }
636
637 return r;
638}
639
640
641
642
643
644
645
646efi_status_t efi_free_pool(void *buffer)
647{
648 efi_status_t ret;
649 struct efi_pool_allocation *alloc;
650
651 if (!buffer)
652 return EFI_INVALID_PARAMETER;
653
654 ret = efi_check_allocated((uintptr_t)buffer, true);
655 if (ret != EFI_SUCCESS)
656 return ret;
657
658 alloc = container_of(buffer, struct efi_pool_allocation, data);
659
660
661 if (((uintptr_t)alloc & EFI_PAGE_MASK) ||
662 alloc->checksum != checksum(alloc)) {
663 printf("%s: illegal free 0x%p\n", __func__, buffer);
664 return EFI_INVALID_PARAMETER;
665 }
666
667 alloc->checksum = 0;
668
669 ret = efi_free_pages((uintptr_t)alloc, alloc->num_pages);
670
671 return ret;
672}
673
674
675
676
677
678
679
680
681
682
683
684
685efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size,
686 struct efi_mem_desc *memory_map,
687 efi_uintn_t *map_key,
688 efi_uintn_t *descriptor_size,
689 uint32_t *descriptor_version)
690{
691 efi_uintn_t map_size = 0;
692 int map_entries = 0;
693 struct list_head *lhandle;
694 efi_uintn_t provided_map_size;
695
696 if (!memory_map_size)
697 return EFI_INVALID_PARAMETER;
698
699 provided_map_size = *memory_map_size;
700
701 list_for_each(lhandle, &efi_mem)
702 map_entries++;
703
704 map_size = map_entries * sizeof(struct efi_mem_desc);
705
706 *memory_map_size = map_size;
707
708 if (descriptor_size)
709 *descriptor_size = sizeof(struct efi_mem_desc);
710
711 if (descriptor_version)
712 *descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION;
713
714 if (provided_map_size < map_size)
715 return EFI_BUFFER_TOO_SMALL;
716
717 if (!memory_map)
718 return EFI_INVALID_PARAMETER;
719
720
721
722 memory_map = &memory_map[map_entries - 1];
723 list_for_each(lhandle, &efi_mem) {
724 struct efi_mem_list *lmem;
725
726 lmem = list_entry(lhandle, struct efi_mem_list, link);
727 *memory_map = lmem->desc;
728 memory_map--;
729 }
730
731 if (map_key)
732 *map_key = efi_memory_map_key;
733
734 return EFI_SUCCESS;
735}
736
737
738
739
740
741
742
743
744
745efi_status_t efi_add_conventional_memory_map(u64 ram_start, u64 ram_end,
746 u64 ram_top)
747{
748 u64 pages;
749
750
751 ram_end &= ~EFI_PAGE_MASK;
752 ram_start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
753
754 if (ram_end <= ram_start) {
755
756 return EFI_INVALID_PARAMETER;
757 }
758
759 pages = (ram_end - ram_start) >> EFI_PAGE_SHIFT;
760
761 efi_add_memory_map_pg(ram_start, pages,
762 EFI_CONVENTIONAL_MEMORY, false);
763
764
765
766
767
768
769
770 if (ram_top < ram_start) {
771
772 efi_add_memory_map_pg(ram_start, pages,
773 EFI_BOOT_SERVICES_DATA, true);
774 } else if (ram_top < ram_end) {
775
776 pages = (ram_end - ram_top) >> EFI_PAGE_SHIFT;
777
778 efi_add_memory_map_pg(ram_top, pages,
779 EFI_BOOT_SERVICES_DATA, true);
780 }
781
782 return EFI_SUCCESS;
783}
784
785__weak void efi_add_known_memory(void)
786{
787 u64 ram_top = board_get_usable_ram_top(0) & ~EFI_PAGE_MASK;
788 int i;
789
790
791
792
793
794 ram_top = (uintptr_t)map_sysmem(ram_top - 1, 0) + 1;
795
796
797 if (!ram_top)
798 ram_top = 0x100000000ULL;
799
800
801 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
802 u64 ram_end, ram_start;
803
804 ram_start = (uintptr_t)map_sysmem(gd->bd->bi_dram[i].start, 0);
805 ram_end = ram_start + gd->bd->bi_dram[i].size;
806
807 efi_add_conventional_memory_map(ram_start, ram_end, ram_top);
808 }
809}
810
811
812static void add_u_boot_and_runtime(void)
813{
814 unsigned long runtime_start, runtime_end, runtime_pages;
815 unsigned long runtime_mask = EFI_PAGE_MASK;
816 unsigned long uboot_start, uboot_pages;
817 unsigned long uboot_stack_size = CONFIG_STACK_SIZE;
818
819
820 uboot_start = ((uintptr_t)map_sysmem(gd->start_addr_sp, 0) -
821 uboot_stack_size) & ~EFI_PAGE_MASK;
822 uboot_pages = ((uintptr_t)map_sysmem(gd->ram_top - 1, 0) -
823 uboot_start + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
824 efi_add_memory_map_pg(uboot_start, uboot_pages, EFI_LOADER_DATA,
825 false);
826
827#if defined(__aarch64__)
828
829
830
831
832
833 runtime_mask = SZ_64K - 1;
834#endif
835
836
837
838
839
840 runtime_start = (ulong)&__efi_runtime_start & ~runtime_mask;
841 runtime_end = (ulong)&__efi_runtime_stop;
842 runtime_end = (runtime_end + runtime_mask) & ~runtime_mask;
843 runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT;
844 efi_add_memory_map_pg(runtime_start, runtime_pages,
845 EFI_RUNTIME_SERVICES_CODE, false);
846}
847
848int efi_memory_init(void)
849{
850 efi_add_known_memory();
851
852 add_u_boot_and_runtime();
853
854#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
855
856 uint64_t efi_bounce_buffer_addr = 0xffffffff;
857
858 if (efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, EFI_LOADER_DATA,
859 (64 * 1024 * 1024) >> EFI_PAGE_SHIFT,
860 &efi_bounce_buffer_addr) != EFI_SUCCESS)
861 return -1;
862
863 efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr;
864#endif
865
866 return 0;
867}
868