1 // SPDX-License-Identifier: GPL-2.0+
3 * Procedures for maintaining information about logical memory blocks.
5 * Peter Bergner, IBM Corp. June 2001.
6 * Copyright (C) 2001 Peter Bergner.
10 #include <efi_loader.h>
19 #include <asm/global_data.h>
20 #include <asm/sections.h>
21 #include <linux/kernel.h>
22 #include <linux/sizes.h>
24 DECLARE_GLOBAL_DATA_PTR;
26 #define MAP_OP_RESERVE (u8)0x1
27 #define MAP_OP_FREE (u8)0x2
28 #define MAP_OP_ADD (u8)0x3
31 * The following low level LMB functions must not access the global LMB memory
32 * map since they are also used to manage IOVA memory maps in iommu drivers like
36 static long lmb_addrs_overlap(phys_addr_t base1, phys_size_t size1,
37 phys_addr_t base2, phys_size_t size2)
39 const phys_addr_t base1_end = base1 + size1 - 1;
40 const phys_addr_t base2_end = base2 + size2 - 1;
42 return ((base1 <= base2_end) && (base2 <= base1_end));
45 static long lmb_addrs_adjacent(phys_addr_t base1, phys_size_t size1,
46 phys_addr_t base2, phys_size_t size2)
48 if (base2 == base1 + size1)
50 else if (base1 == base2 + size2)
56 static long lmb_regions_overlap(struct alist *lmb_rgn_lst, unsigned long r1,
59 struct lmb_region *rgn = lmb_rgn_lst->data;
61 phys_addr_t base1 = rgn[r1].base;
62 phys_size_t size1 = rgn[r1].size;
63 phys_addr_t base2 = rgn[r2].base;
64 phys_size_t size2 = rgn[r2].size;
66 return lmb_addrs_overlap(base1, size1, base2, size2);
69 static long lmb_regions_adjacent(struct alist *lmb_rgn_lst, unsigned long r1,
72 struct lmb_region *rgn = lmb_rgn_lst->data;
74 phys_addr_t base1 = rgn[r1].base;
75 phys_size_t size1 = rgn[r1].size;
76 phys_addr_t base2 = rgn[r2].base;
77 phys_size_t size2 = rgn[r2].size;
78 return lmb_addrs_adjacent(base1, size1, base2, size2);
81 static void lmb_remove_region(struct alist *lmb_rgn_lst, unsigned long r)
84 struct lmb_region *rgn = lmb_rgn_lst->data;
86 for (i = r; i < lmb_rgn_lst->count - 1; i++) {
87 rgn[i].base = rgn[i + 1].base;
88 rgn[i].size = rgn[i + 1].size;
89 rgn[i].flags = rgn[i + 1].flags;
94 /* Assumption: base addr of region 1 < base addr of region 2 */
95 static void lmb_coalesce_regions(struct alist *lmb_rgn_lst, unsigned long r1,
98 struct lmb_region *rgn = lmb_rgn_lst->data;
100 rgn[r1].size += rgn[r2].size;
101 lmb_remove_region(lmb_rgn_lst, r2);
104 /*Assumption : base addr of region 1 < base addr of region 2*/
105 static void lmb_fix_over_lap_regions(struct alist *lmb_rgn_lst,
106 unsigned long r1, unsigned long r2)
108 struct lmb_region *rgn = lmb_rgn_lst->data;
110 phys_addr_t base1 = rgn[r1].base;
111 phys_size_t size1 = rgn[r1].size;
112 phys_addr_t base2 = rgn[r2].base;
113 phys_size_t size2 = rgn[r2].size;
115 if (base1 + size1 > base2 + size2) {
116 printf("This will not be a case any time\n");
119 rgn[r1].size = base2 + size2 - base1;
120 lmb_remove_region(lmb_rgn_lst, r2);
123 static long lmb_resize_regions(struct alist *lmb_rgn_lst,
124 unsigned long idx_start,
125 phys_addr_t base, phys_size_t size)
128 unsigned long rgn_cnt, idx, idx_end;
129 phys_addr_t rgnbase, rgnend;
130 phys_addr_t mergebase, mergeend;
131 struct lmb_region *rgn = lmb_rgn_lst->data;
138 * First thing to do is to identify how many regions
139 * the requested region overlaps.
140 * If the flags match, combine all these overlapping
141 * regions into a single region, and remove the merged
144 while (idx <= lmb_rgn_lst->count - 1) {
145 rgnbase = rgn[idx].base;
146 rgnsize = rgn[idx].size;
148 if (lmb_addrs_overlap(base, size, rgnbase,
150 if (rgn[idx].flags != LMB_NONE)
158 /* The merged region's base and size */
159 rgnbase = rgn[idx_start].base;
160 mergebase = min(base, rgnbase);
161 rgnend = rgn[idx_end].base + rgn[idx_end].size;
162 mergeend = max(rgnend, (base + size));
164 rgn[idx_start].base = mergebase;
165 rgn[idx_start].size = mergeend - mergebase;
167 /* Now remove the merged regions */
169 lmb_remove_region(lmb_rgn_lst, idx_start + 1);
175 * lmb_add_region_flags() - Add an lmb region to the given list
176 * @lmb_rgn_lst: LMB list to which region is to be added(free/used)
177 * @base: Start address of the region
178 * @size: Size of the region to be added
179 * @flags: Attributes of the LMB region
181 * Add a region of memory to the list. If the region does not exist, add
182 * it to the list. Depending on the attributes of the region to be added,
183 * the function might resize an already existing region or coalesce two
187 * Returns: 0 if the region addition successful, -1 on failure
189 static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base,
190 phys_size_t size, enum lmb_flags flags)
192 unsigned long coalesced = 0;
194 struct lmb_region *rgn = lmb_rgn_lst->data;
196 if (alist_err(lmb_rgn_lst))
199 /* First try and coalesce this LMB with another. */
200 for (i = 0; i < lmb_rgn_lst->count; i++) {
201 phys_addr_t rgnbase = rgn[i].base;
202 phys_size_t rgnsize = rgn[i].size;
203 phys_size_t rgnflags = rgn[i].flags;
204 phys_addr_t end = base + size - 1;
205 phys_addr_t rgnend = rgnbase + rgnsize - 1;
206 if (rgnbase <= base && end <= rgnend) {
207 if (flags == rgnflags)
208 /* Already have this region, so we're done */
211 return -1; /* regions with new flags */
214 ret = lmb_addrs_adjacent(base, size, rgnbase, rgnsize);
216 if (flags != rgnflags)
222 } else if (ret < 0) {
223 if (flags != rgnflags)
228 } else if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) {
229 if (flags == LMB_NONE) {
230 ret = lmb_resize_regions(lmb_rgn_lst, i, base,
243 if (lmb_rgn_lst->count && i < lmb_rgn_lst->count - 1) {
244 rgn = lmb_rgn_lst->data;
245 if (rgn[i].flags == rgn[i + 1].flags) {
246 if (lmb_regions_adjacent(lmb_rgn_lst, i, i + 1)) {
247 lmb_coalesce_regions(lmb_rgn_lst, i, i + 1);
249 } else if (lmb_regions_overlap(lmb_rgn_lst, i, i + 1)) {
250 /* fix overlapping area */
251 lmb_fix_over_lap_regions(lmb_rgn_lst, i, i + 1);
260 if (alist_full(lmb_rgn_lst) &&
261 !alist_expand_by(lmb_rgn_lst, lmb_rgn_lst->alloc))
263 rgn = lmb_rgn_lst->data;
265 /* Couldn't coalesce the LMB, so add it to the sorted table. */
266 for (i = lmb_rgn_lst->count; i >= 0; i--) {
267 if (i && base < rgn[i - 1].base) {
272 rgn[i].flags = flags;
277 lmb_rgn_lst->count++;
282 static long _lmb_free(struct alist *lmb_rgn_lst, phys_addr_t base,
285 struct lmb_region *rgn;
286 phys_addr_t rgnbegin, rgnend;
287 phys_addr_t end = base + size - 1;
290 rgnbegin = rgnend = 0; /* supress gcc warnings */
291 rgn = lmb_rgn_lst->data;
292 /* Find the region where (base, size) belongs to */
293 for (i = 0; i < lmb_rgn_lst->count; i++) {
294 rgnbegin = rgn[i].base;
295 rgnend = rgnbegin + rgn[i].size - 1;
297 if ((rgnbegin <= base) && (end <= rgnend))
301 /* Didn't find the region */
302 if (i == lmb_rgn_lst->count)
305 /* Check to see if we are removing entire region */
306 if ((rgnbegin == base) && (rgnend == end)) {
307 lmb_remove_region(lmb_rgn_lst, i);
311 /* Check to see if region is matching at the front */
312 if (rgnbegin == base) {
313 rgn[i].base = end + 1;
318 /* Check to see if the region is matching at the end */
325 * We need to split the entry - adjust the current one to the
326 * beginging of the hole and add the region after hole.
328 rgn[i].size = base - rgn[i].base;
329 return lmb_add_region_flags(lmb_rgn_lst, end + 1, rgnend - end,
333 static long lmb_overlaps_region(struct alist *lmb_rgn_lst, phys_addr_t base,
337 struct lmb_region *rgn = lmb_rgn_lst->data;
339 for (i = 0; i < lmb_rgn_lst->count; i++) {
340 phys_addr_t rgnbase = rgn[i].base;
341 phys_size_t rgnsize = rgn[i].size;
342 if (lmb_addrs_overlap(base, size, rgnbase, rgnsize))
346 return (i < lmb_rgn_lst->count) ? i : -1;
349 static phys_addr_t lmb_align_down(phys_addr_t addr, phys_size_t size)
351 return addr & ~(size - 1);
355 * IOVA LMB memory maps using lmb pointers instead of the global LMB memory map.
358 int io_lmb_setup(struct lmb *io_lmb)
362 ret = alist_init(&io_lmb->free_mem, sizeof(struct lmb_region),
363 (uint)LMB_ALIST_INITIAL_SIZE);
365 log_debug("Unable to initialise the list for LMB free IOVA\n");
369 ret = alist_init(&io_lmb->used_mem, sizeof(struct lmb_region),
370 (uint)LMB_ALIST_INITIAL_SIZE);
372 log_debug("Unable to initialise the list for LMB used IOVA\n");
376 io_lmb->test = false;
381 void io_lmb_teardown(struct lmb *io_lmb)
383 alist_uninit(&io_lmb->free_mem);
384 alist_uninit(&io_lmb->used_mem);
387 long io_lmb_add(struct lmb *io_lmb, phys_addr_t base, phys_size_t size)
389 return lmb_add_region_flags(&io_lmb->free_mem, base, size, LMB_NONE);
392 /* derived and simplified from _lmb_alloc_base() */
393 phys_addr_t io_lmb_alloc(struct lmb *io_lmb, phys_size_t size, ulong align)
396 phys_addr_t base = 0;
397 phys_addr_t res_base;
398 struct lmb_region *lmb_used = io_lmb->used_mem.data;
399 struct lmb_region *lmb_memory = io_lmb->free_mem.data;
401 for (i = io_lmb->free_mem.count - 1; i >= 0; i--) {
402 phys_addr_t lmbbase = lmb_memory[i].base;
403 phys_size_t lmbsize = lmb_memory[i].size;
407 base = lmb_align_down(lmbbase + lmbsize - size, align);
409 while (base && lmbbase <= base) {
410 rgn = lmb_overlaps_region(&io_lmb->used_mem, base, size);
412 /* This area isn't reserved, take it */
413 if (lmb_add_region_flags(&io_lmb->used_mem, base,
420 res_base = lmb_used[rgn].base;
423 base = lmb_align_down(res_base - size, align);
429 long io_lmb_free(struct lmb *io_lmb, phys_addr_t base, phys_size_t size)
431 return _lmb_free(&io_lmb->used_mem, base, size);
435 * Low level LMB functions are used to manage IOVA memory maps for the Apple
436 * dart iommu. They must not access the global LMB memory map.
437 * So keep the global LMB variable declaration unreachable from them.
440 static struct lmb lmb;
442 static bool lmb_should_notify(enum lmb_flags flags)
444 return !lmb.test && !(flags & LMB_NONOTIFY) &&
445 CONFIG_IS_ENABLED(EFI_LOADER);
448 static int lmb_map_update_notify(phys_addr_t addr, phys_size_t size, u8 op,
449 enum lmb_flags flags)
455 if (op != MAP_OP_RESERVE && op != MAP_OP_FREE && op != MAP_OP_ADD) {
456 log_err("Invalid map update op received (%d)\n", op);
460 if (!lmb_should_notify(flags))
463 efi_addr = (uintptr_t)map_sysmem(addr, 0);
464 pages = efi_size_in_pages(size + (efi_addr & EFI_PAGE_MASK));
465 efi_addr &= ~EFI_PAGE_MASK;
467 status = efi_add_memory_map_pg(efi_addr, pages,
468 op == MAP_OP_RESERVE ?
469 EFI_BOOT_SERVICES_DATA :
470 EFI_CONVENTIONAL_MEMORY,
472 if (status != EFI_SUCCESS) {
473 log_err("%s: LMB Map notify failure %lu\n", __func__,
474 status & ~EFI_ERROR_MASK);
477 unmap_sysmem((void *)(uintptr_t)efi_addr);
482 static void lmb_print_region_flags(enum lmb_flags flags)
484 const char *flag_str[] = { "none", "no-map", "no-overwrite", "no-notify" };
485 unsigned int pflags = flags &
486 (LMB_NOMAP | LMB_NOOVERWRITE | LMB_NONOTIFY);
488 if (flags != pflags) {
489 printf("invalid %#x\n", flags);
494 int bitpos = pflags ? fls(pflags) - 1 : 0;
496 printf("%s", flag_str[bitpos]);
497 pflags &= ~(1u << bitpos);
498 puts(pflags ? ", " : "\n");
502 static void lmb_dump_region(struct alist *lmb_rgn_lst, char *name)
504 struct lmb_region *rgn = lmb_rgn_lst->data;
505 unsigned long long base, size, end;
506 enum lmb_flags flags;
509 printf(" %s.count = %#x\n", name, lmb_rgn_lst->count);
511 for (i = 0; i < lmb_rgn_lst->count; i++) {
514 end = base + size - 1;
515 flags = rgn[i].flags;
517 printf(" %s[%d]\t[%#llx-%#llx], %#llx bytes, flags: ",
518 name, i, base, end, size);
519 lmb_print_region_flags(flags);
523 void lmb_dump_all_force(void)
525 printf("lmb_dump_all:\n");
526 lmb_dump_region(&lmb.free_mem, "memory");
527 lmb_dump_region(&lmb.used_mem, "reserved");
530 void lmb_dump_all(void)
533 lmb_dump_all_force();
537 static void lmb_reserve_uboot_region(void)
541 phys_addr_t rsv_start;
543 rsv_start = gd->start_addr_sp - CONFIG_STACK_SIZE;
547 * Reserve memory from aligned address below the bottom of U-Boot stack
548 * until end of RAM area to prevent LMB from overwriting that memory.
550 debug("## Current stack ends at 0x%08lx ", (ulong)rsv_start);
552 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
553 if (!gd->bd->bi_dram[bank].size ||
554 rsv_start < gd->bd->bi_dram[bank].start)
556 /* Watch out for RAM at end of address space! */
557 bank_end = gd->bd->bi_dram[bank].start +
558 gd->bd->bi_dram[bank].size - 1;
559 if (rsv_start > bank_end)
564 lmb_reserve_flags(rsv_start, bank_end - rsv_start + 1,
567 if (gd->flags & GD_FLG_SKIP_RELOC)
568 lmb_reserve_flags((phys_addr_t)(uintptr_t)_start,
569 gd->mon_len, LMB_NOOVERWRITE);
575 static void lmb_reserve_common(void *fdt_blob)
577 lmb_reserve_uboot_region();
579 if (CONFIG_IS_ENABLED(OF_LIBFDT) && fdt_blob)
580 boot_fdt_add_mem_rsv_regions(fdt_blob);
583 static __maybe_unused void lmb_reserve_common_spl(void)
585 phys_addr_t rsv_start;
586 phys_size_t rsv_size;
589 * Assume a SPL stack of 16KB. This must be
590 * more than enough for the SPL stage.
592 if (IS_ENABLED(CONFIG_SPL_STACK_R_ADDR)) {
593 rsv_start = gd->start_addr_sp - 16384;
595 lmb_reserve_flags(rsv_start, rsv_size, LMB_NOOVERWRITE);
598 if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS)) {
599 /* Reserve the bss region */
600 rsv_start = (phys_addr_t)(uintptr_t)__bss_start;
601 rsv_size = (phys_addr_t)(uintptr_t)__bss_end -
602 (phys_addr_t)(uintptr_t)__bss_start;
603 lmb_reserve_flags(rsv_start, rsv_size, LMB_NOOVERWRITE);
608 * lmb_add_memory() - Add memory range for LMB allocations
610 * Add the entire available memory range to the pool of memory that
611 * can be used by the LMB module for allocations.
615 void lmb_add_memory(void)
619 u64 ram_top = gd->ram_top;
620 struct bd_info *bd = gd->bd;
622 if (CONFIG_IS_ENABLED(LMB_ARCH_MEM_MAP))
623 return lmb_arch_add_memory();
625 /* Assume a 4GB ram_top if not defined */
627 ram_top = 0x100000000ULL;
629 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
630 size = bd->bi_dram[i].size;
632 lmb_add(bd->bi_dram[i].start, size);
635 * Reserve memory above ram_top as
636 * no-overwrite so that it cannot be
639 if (bd->bi_dram[i].start >= ram_top)
640 lmb_reserve_flags(bd->bi_dram[i].start, size,
646 static long lmb_add_region(struct alist *lmb_rgn_lst, phys_addr_t base,
649 return lmb_add_region_flags(lmb_rgn_lst, base, size, LMB_NONE);
652 /* This routine may be called with relocation disabled. */
653 long lmb_add(phys_addr_t base, phys_size_t size)
656 struct alist *lmb_rgn_lst = &lmb.free_mem;
658 ret = lmb_add_region(lmb_rgn_lst, base, size);
662 return lmb_map_update_notify(base, size, MAP_OP_ADD, LMB_NONE);
666 * lmb_free_flags() - Free up a region of memory
667 * @base: Base Address of region to be freed
668 * @size: Size of the region to be freed
669 * @flags: Memory region attributes
671 * Free up a region of memory.
673 * Return: 0 if successful, -1 on failure
675 long lmb_free_flags(phys_addr_t base, phys_size_t size,
680 ret = _lmb_free(&lmb.used_mem, base, size);
684 return lmb_map_update_notify(base, size, MAP_OP_FREE, flags);
687 long lmb_free(phys_addr_t base, phys_size_t size)
689 return lmb_free_flags(base, size, LMB_NONE);
692 long lmb_reserve_flags(phys_addr_t base, phys_size_t size, enum lmb_flags flags)
695 struct alist *lmb_rgn_lst = &lmb.used_mem;
697 ret = lmb_add_region_flags(lmb_rgn_lst, base, size, flags);
701 return lmb_map_update_notify(base, size, MAP_OP_RESERVE, flags);
704 long lmb_reserve(phys_addr_t base, phys_size_t size)
706 return lmb_reserve_flags(base, size, LMB_NONE);
709 static phys_addr_t _lmb_alloc_base(phys_size_t size, ulong align,
710 phys_addr_t max_addr, enum lmb_flags flags)
714 phys_addr_t base = 0;
715 phys_addr_t res_base;
716 struct lmb_region *lmb_used = lmb.used_mem.data;
717 struct lmb_region *lmb_memory = lmb.free_mem.data;
719 for (i = lmb.free_mem.count - 1; i >= 0; i--) {
720 phys_addr_t lmbbase = lmb_memory[i].base;
721 phys_size_t lmbsize = lmb_memory[i].size;
725 if (max_addr == LMB_ALLOC_ANYWHERE)
726 base = lmb_align_down(lmbbase + lmbsize - size, align);
727 else if (lmbbase < max_addr) {
728 base = lmbbase + lmbsize;
731 base = min(base, max_addr);
732 base = lmb_align_down(base - size, align);
736 while (base && lmbbase <= base) {
737 rgn = lmb_overlaps_region(&lmb.used_mem, base, size);
739 /* This area isn't reserved, take it */
740 if (lmb_add_region_flags(&lmb.used_mem, base,
744 ret = lmb_map_update_notify(base, size,
753 res_base = lmb_used[rgn].base;
756 base = lmb_align_down(res_base - size, align);
762 phys_addr_t lmb_alloc(phys_size_t size, ulong align)
764 return lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE);
767 phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr)
771 alloc = _lmb_alloc_base(size, align, max_addr, LMB_NONE);
774 printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
775 (ulong)size, (ulong)max_addr);
781 * lmb_alloc_base_flags() - Allocate specified memory region with specified attributes
782 * @size: Size of the region requested
783 * @align: Alignment of the memory region requested
784 * @max_addr: Maximum address of the requested region
785 * @flags: Memory region attributes to be set
787 * Allocate a region of memory with the attributes specified through the
788 * parameter. The max_addr parameter is used to specify the maximum address
789 * below which the requested region should be allocated.
791 * Return: base address on success, 0 on error
793 phys_addr_t lmb_alloc_base_flags(phys_size_t size, ulong align,
794 phys_addr_t max_addr, uint flags)
798 alloc = _lmb_alloc_base(size, align, max_addr, flags);
801 printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
802 (ulong)size, (ulong)max_addr);
807 static phys_addr_t _lmb_alloc_addr(phys_addr_t base, phys_size_t size,
808 enum lmb_flags flags)
811 struct lmb_region *lmb_memory = lmb.free_mem.data;
813 /* Check if the requested address is in one of the memory regions */
814 rgn = lmb_overlaps_region(&lmb.free_mem, base, size);
817 * Check if the requested end address is in the same memory
820 if (lmb_addrs_overlap(lmb_memory[rgn].base,
821 lmb_memory[rgn].size,
822 base + size - 1, 1)) {
823 /* ok, reserve the memory */
824 if (lmb_reserve_flags(base, size, flags) >= 0)
833 * Try to allocate a specific address range: must be in defined memory but not
836 phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size)
838 return _lmb_alloc_addr(base, size, LMB_NONE);
842 * lmb_alloc_addr_flags() - Allocate specified memory address with specified attributes
843 * @base: Base Address requested
844 * @size: Size of the region requested
845 * @flags: Memory region attributes to be set
847 * Allocate a region of memory with the attributes specified through the
848 * parameter. The base parameter is used to specify the base address
849 * of the requested region.
851 * Return: base address on success, 0 on error
853 phys_addr_t lmb_alloc_addr_flags(phys_addr_t base, phys_size_t size,
856 return _lmb_alloc_addr(base, size, flags);
859 /* Return number of bytes from a given address that are free */
860 phys_size_t lmb_get_free_size(phys_addr_t addr)
864 struct lmb_region *lmb_used = lmb.used_mem.data;
865 struct lmb_region *lmb_memory = lmb.free_mem.data;
867 /* check if the requested address is in the memory regions */
868 rgn = lmb_overlaps_region(&lmb.free_mem, addr, 1);
870 for (i = 0; i < lmb.used_mem.count; i++) {
871 if (addr < lmb_used[i].base) {
872 /* first reserved range > requested address */
873 return lmb_used[i].base - addr;
875 if (lmb_used[i].base +
876 lmb_used[i].size > addr) {
877 /* requested addr is in this reserved range */
881 /* if we come here: no reserved ranges above requested addr */
882 return lmb_memory[lmb.free_mem.count - 1].base +
883 lmb_memory[lmb.free_mem.count - 1].size - addr;
888 int lmb_is_reserved_flags(phys_addr_t addr, int flags)
891 struct lmb_region *lmb_used = lmb.used_mem.data;
893 for (i = 0; i < lmb.used_mem.count; i++) {
894 phys_addr_t upper = lmb_used[i].base +
895 lmb_used[i].size - 1;
896 if (addr >= lmb_used[i].base && addr <= upper)
897 return (lmb_used[i].flags & flags) == flags;
902 static int lmb_setup(bool test)
906 ret = alist_init(&lmb.free_mem, sizeof(struct lmb_region),
907 (uint)LMB_ALIST_INITIAL_SIZE);
909 log_debug("Unable to initialise the list for LMB free memory\n");
913 ret = alist_init(&lmb.used_mem, sizeof(struct lmb_region),
914 (uint)LMB_ALIST_INITIAL_SIZE);
916 log_debug("Unable to initialise the list for LMB used memory\n");
926 * lmb_init() - Initialise the LMB module
928 * Initialise the LMB lists needed for keeping the memory map. There
929 * are two lists, in form of alloced list data structure. One for the
930 * available memory, and one for the used memory. Initialise the two
931 * lists as part of board init. Add memory to the available memory
932 * list and reserve common areas by adding them to the used memory
935 * Return: 0 on success, -ve on error
941 ret = lmb_setup(false);
943 log_info("Unable to init LMB\n");
949 /* Reserve the U-Boot image region once U-Boot has relocated */
950 if (xpl_phase() == PHASE_SPL)
951 lmb_reserve_common_spl();
952 else if (xpl_phase() == PHASE_BOARD_R)
953 lmb_reserve_common((void *)gd->fdt_blob);
958 struct lmb *lmb_get(void)
963 #if CONFIG_IS_ENABLED(UNIT_TEST)
964 int lmb_push(struct lmb *store)
969 ret = lmb_setup(true);
976 void lmb_pop(struct lmb *store)
978 alist_uninit(&lmb.free_mem);
979 alist_uninit(&lmb.used_mem);
982 #endif /* UNIT_TEST */