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>
18 #include <asm/global_data.h>
19 #include <asm/sections.h>
20 #include <linux/kernel.h>
21 #include <linux/sizes.h>
23 DECLARE_GLOBAL_DATA_PTR;
25 #define LMB_ALLOC_ANYWHERE 0
26 #define LMB_ALIST_INITIAL_SIZE 4
28 static struct lmb lmb;
30 static void lmb_dump_region(struct alist *lmb_rgn_lst, char *name)
32 struct lmb_region *rgn = lmb_rgn_lst->data;
33 unsigned long long base, size, end;
37 printf(" %s.count = 0x%x\n", name, lmb_rgn_lst->count);
39 for (i = 0; i < lmb_rgn_lst->count; i++) {
42 end = base + size - 1;
45 printf(" %s[%d]\t[0x%llx-0x%llx], 0x%08llx bytes flags: %x\n",
46 name, i, base, end, size, flags);
50 void lmb_dump_all_force(void)
52 printf("lmb_dump_all:\n");
53 lmb_dump_region(&lmb.free_mem, "memory");
54 lmb_dump_region(&lmb.used_mem, "reserved");
57 void lmb_dump_all(void)
64 static long lmb_addrs_overlap(phys_addr_t base1, phys_size_t size1,
65 phys_addr_t base2, phys_size_t size2)
67 const phys_addr_t base1_end = base1 + size1 - 1;
68 const phys_addr_t base2_end = base2 + size2 - 1;
70 return ((base1 <= base2_end) && (base2 <= base1_end));
73 static long lmb_addrs_adjacent(phys_addr_t base1, phys_size_t size1,
74 phys_addr_t base2, phys_size_t size2)
76 if (base2 == base1 + size1)
78 else if (base1 == base2 + size2)
84 static long lmb_regions_overlap(struct alist *lmb_rgn_lst, unsigned long r1,
87 struct lmb_region *rgn = lmb_rgn_lst->data;
89 phys_addr_t base1 = rgn[r1].base;
90 phys_size_t size1 = rgn[r1].size;
91 phys_addr_t base2 = rgn[r2].base;
92 phys_size_t size2 = rgn[r2].size;
94 return lmb_addrs_overlap(base1, size1, base2, size2);
97 static long lmb_regions_adjacent(struct alist *lmb_rgn_lst, unsigned long r1,
100 struct lmb_region *rgn = lmb_rgn_lst->data;
102 phys_addr_t base1 = rgn[r1].base;
103 phys_size_t size1 = rgn[r1].size;
104 phys_addr_t base2 = rgn[r2].base;
105 phys_size_t size2 = rgn[r2].size;
106 return lmb_addrs_adjacent(base1, size1, base2, size2);
109 static void lmb_remove_region(struct alist *lmb_rgn_lst, unsigned long r)
112 struct lmb_region *rgn = lmb_rgn_lst->data;
114 for (i = r; i < lmb_rgn_lst->count - 1; i++) {
115 rgn[i].base = rgn[i + 1].base;
116 rgn[i].size = rgn[i + 1].size;
117 rgn[i].flags = rgn[i + 1].flags;
119 lmb_rgn_lst->count--;
122 /* Assumption: base addr of region 1 < base addr of region 2 */
123 static void lmb_coalesce_regions(struct alist *lmb_rgn_lst, unsigned long r1,
126 struct lmb_region *rgn = lmb_rgn_lst->data;
128 rgn[r1].size += rgn[r2].size;
129 lmb_remove_region(lmb_rgn_lst, r2);
132 /*Assumption : base addr of region 1 < base addr of region 2*/
133 static void lmb_fix_over_lap_regions(struct alist *lmb_rgn_lst,
134 unsigned long r1, unsigned long r2)
136 struct lmb_region *rgn = lmb_rgn_lst->data;
138 phys_addr_t base1 = rgn[r1].base;
139 phys_size_t size1 = rgn[r1].size;
140 phys_addr_t base2 = rgn[r2].base;
141 phys_size_t size2 = rgn[r2].size;
143 if (base1 + size1 > base2 + size2) {
144 printf("This will not be a case any time\n");
147 rgn[r1].size = base2 + size2 - base1;
148 lmb_remove_region(lmb_rgn_lst, r2);
152 * efi_lmb_reserve() - add reservations for EFI memory
154 * Add reservations for all EFI memory areas that are not
155 * EFI_CONVENTIONAL_MEMORY.
157 * Return: 0 on success, 1 on failure
159 static __maybe_unused int efi_lmb_reserve(void)
161 struct efi_mem_desc *memmap = NULL, *map;
162 efi_uintn_t i, map_size = 0;
165 ret = efi_get_memory_map_alloc(&map_size, &memmap);
166 if (ret != EFI_SUCCESS)
169 for (i = 0, map = memmap; i < map_size / sizeof(*map); ++map, ++i) {
170 if (map->type != EFI_CONVENTIONAL_MEMORY) {
171 lmb_reserve_flags(map_to_sysmem((void *)(uintptr_t)
172 map->physical_start),
173 map->num_pages * EFI_PAGE_SIZE,
174 map->type == EFI_RESERVED_MEMORY_TYPE
175 ? LMB_NOMAP : LMB_NONE);
178 efi_free_pool(memmap);
183 static void lmb_reserve_uboot_region(void)
187 phys_addr_t rsv_start;
189 rsv_start = gd->start_addr_sp - CONFIG_STACK_SIZE;
193 * Reserve memory from aligned address below the bottom of U-Boot stack
194 * until end of RAM area to prevent LMB from overwriting that memory.
196 debug("## Current stack ends at 0x%08lx ", (ulong)rsv_start);
198 /* adjust sp by 16K to be safe */
200 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
201 if (!gd->bd->bi_dram[bank].size ||
202 rsv_start < gd->bd->bi_dram[bank].start)
204 /* Watch out for RAM at end of address space! */
205 bank_end = gd->bd->bi_dram[bank].start +
206 gd->bd->bi_dram[bank].size - 1;
207 if (rsv_start > bank_end)
212 lmb_reserve_flags(rsv_start, bank_end - rsv_start + 1,
215 if (gd->flags & GD_FLG_SKIP_RELOC)
216 lmb_reserve_flags((phys_addr_t)(uintptr_t)_start,
217 gd->mon_len, LMB_NOOVERWRITE);
223 static void lmb_reserve_common(void *fdt_blob)
226 lmb_reserve_uboot_region();
228 if (CONFIG_IS_ENABLED(OF_LIBFDT) && fdt_blob)
229 boot_fdt_add_mem_rsv_regions(fdt_blob);
231 if (CONFIG_IS_ENABLED(EFI_LOADER))
235 static __maybe_unused void lmb_reserve_common_spl(void)
237 phys_addr_t rsv_start;
238 phys_size_t rsv_size;
241 * Assume a SPL stack of 16KB. This must be
242 * more than enough for the SPL stage.
244 if (IS_ENABLED(CONFIG_SPL_STACK_R_ADDR)) {
245 rsv_start = gd->start_addr_sp - 16384;
247 lmb_reserve_flags(rsv_start, rsv_size, LMB_NOOVERWRITE);
250 if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS)) {
251 /* Reserve the bss region */
252 rsv_start = (phys_addr_t)(uintptr_t)__bss_start;
253 rsv_size = (phys_addr_t)(uintptr_t)__bss_end -
254 (phys_addr_t)(uintptr_t)__bss_start;
255 lmb_reserve_flags(rsv_start, rsv_size, LMB_NOOVERWRITE);
260 * lmb_add_memory() - Add memory range for LMB allocations
262 * Add the entire available memory range to the pool of memory that
263 * can be used by the LMB module for allocations.
267 void lmb_add_memory(void)
272 u64 ram_top = gd->ram_top;
273 struct bd_info *bd = gd->bd;
275 /* Assume a 4GB ram_top if not defined */
277 ram_top = 0x100000000ULL;
279 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
280 size = bd->bi_dram[i].size;
282 if (bd->bi_dram[i].start > ram_top)
285 rgn_top = bd->bi_dram[i].start +
288 if (rgn_top > ram_top)
289 size -= rgn_top - ram_top;
291 lmb_add(bd->bi_dram[i].start, size);
296 static long lmb_resize_regions(struct alist *lmb_rgn_lst,
297 unsigned long idx_start,
298 phys_addr_t base, phys_size_t size)
301 unsigned long rgn_cnt, idx, idx_end;
302 phys_addr_t rgnbase, rgnend;
303 phys_addr_t mergebase, mergeend;
304 struct lmb_region *rgn = lmb_rgn_lst->data;
311 * First thing to do is to identify how many regions
312 * the requested region overlaps.
313 * If the flags match, combine all these overlapping
314 * regions into a single region, and remove the merged
317 while (idx <= lmb_rgn_lst->count - 1) {
318 rgnbase = rgn[idx].base;
319 rgnsize = rgn[idx].size;
321 if (lmb_addrs_overlap(base, size, rgnbase,
323 if (rgn[idx].flags != LMB_NONE)
331 /* The merged region's base and size */
332 rgnbase = rgn[idx_start].base;
333 mergebase = min(base, rgnbase);
334 rgnend = rgn[idx_end].base + rgn[idx_end].size;
335 mergeend = max(rgnend, (base + size));
337 rgn[idx_start].base = mergebase;
338 rgn[idx_start].size = mergeend - mergebase;
340 /* Now remove the merged regions */
342 lmb_remove_region(lmb_rgn_lst, idx_start + 1);
348 * lmb_add_region_flags() - Add an lmb region to the given list
349 * @lmb_rgn_lst: LMB list to which region is to be added(free/used)
350 * @base: Start address of the region
351 * @size: Size of the region to be added
352 * @flags: Attributes of the LMB region
354 * Add a region of memory to the list. If the region does not exist, add
355 * it to the list. Depending on the attributes of the region to be added,
356 * the function might resize an already existing region or coalesce two
360 * Returns: 0 if the region addition successful, -1 on failure
362 static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base,
363 phys_size_t size, enum lmb_flags flags)
365 unsigned long coalesced = 0;
367 struct lmb_region *rgn = lmb_rgn_lst->data;
369 if (alist_err(lmb_rgn_lst))
372 /* First try and coalesce this LMB with another. */
373 for (i = 0; i < lmb_rgn_lst->count; i++) {
374 phys_addr_t rgnbase = rgn[i].base;
375 phys_size_t rgnsize = rgn[i].size;
376 phys_size_t rgnflags = rgn[i].flags;
377 phys_addr_t end = base + size - 1;
378 phys_addr_t rgnend = rgnbase + rgnsize - 1;
379 if (rgnbase <= base && end <= rgnend) {
380 if (flags == rgnflags)
381 /* Already have this region, so we're done */
384 return -1; /* regions with new flags */
387 ret = lmb_addrs_adjacent(base, size, rgnbase, rgnsize);
389 if (flags != rgnflags)
395 } else if (ret < 0) {
396 if (flags != rgnflags)
401 } else if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) {
402 if (flags == LMB_NONE) {
403 ret = lmb_resize_regions(lmb_rgn_lst, i, base,
416 if (lmb_rgn_lst->count && i < lmb_rgn_lst->count - 1) {
417 rgn = lmb_rgn_lst->data;
418 if (rgn[i].flags == rgn[i + 1].flags) {
419 if (lmb_regions_adjacent(lmb_rgn_lst, i, i + 1)) {
420 lmb_coalesce_regions(lmb_rgn_lst, i, i + 1);
422 } else if (lmb_regions_overlap(lmb_rgn_lst, i, i + 1)) {
423 /* fix overlapping area */
424 lmb_fix_over_lap_regions(lmb_rgn_lst, i, i + 1);
433 if (alist_full(lmb_rgn_lst) &&
434 !alist_expand_by(lmb_rgn_lst, lmb_rgn_lst->alloc))
436 rgn = lmb_rgn_lst->data;
438 /* Couldn't coalesce the LMB, so add it to the sorted table. */
439 for (i = lmb_rgn_lst->count; i >= 0; i--) {
440 if (i && base < rgn[i - 1].base) {
445 rgn[i].flags = flags;
450 lmb_rgn_lst->count++;
455 static long lmb_add_region(struct alist *lmb_rgn_lst, phys_addr_t base,
458 return lmb_add_region_flags(lmb_rgn_lst, base, size, LMB_NONE);
461 /* This routine may be called with relocation disabled. */
462 long lmb_add(phys_addr_t base, phys_size_t size)
464 struct alist *lmb_rgn_lst = &lmb.free_mem;
466 return lmb_add_region(lmb_rgn_lst, base, size);
469 long lmb_free(phys_addr_t base, phys_size_t size)
471 struct lmb_region *rgn;
472 struct alist *lmb_rgn_lst = &lmb.used_mem;
473 phys_addr_t rgnbegin, rgnend;
474 phys_addr_t end = base + size - 1;
477 rgnbegin = rgnend = 0; /* supress gcc warnings */
478 rgn = lmb_rgn_lst->data;
479 /* Find the region where (base, size) belongs to */
480 for (i = 0; i < lmb_rgn_lst->count; i++) {
481 rgnbegin = rgn[i].base;
482 rgnend = rgnbegin + rgn[i].size - 1;
484 if ((rgnbegin <= base) && (end <= rgnend))
488 /* Didn't find the region */
489 if (i == lmb_rgn_lst->count)
492 /* Check to see if we are removing entire region */
493 if ((rgnbegin == base) && (rgnend == end)) {
494 lmb_remove_region(lmb_rgn_lst, i);
498 /* Check to see if region is matching at the front */
499 if (rgnbegin == base) {
500 rgn[i].base = end + 1;
505 /* Check to see if the region is matching at the end */
512 * We need to split the entry - adjust the current one to the
513 * beginging of the hole and add the region after hole.
515 rgn[i].size = base - rgn[i].base;
516 return lmb_add_region_flags(lmb_rgn_lst, end + 1, rgnend - end,
520 long lmb_reserve_flags(phys_addr_t base, phys_size_t size, enum lmb_flags flags)
522 struct alist *lmb_rgn_lst = &lmb.used_mem;
524 return lmb_add_region_flags(lmb_rgn_lst, base, size, flags);
527 long lmb_reserve(phys_addr_t base, phys_size_t size)
529 return lmb_reserve_flags(base, size, LMB_NONE);
532 static long lmb_overlaps_region(struct alist *lmb_rgn_lst, phys_addr_t base,
536 struct lmb_region *rgn = lmb_rgn_lst->data;
538 for (i = 0; i < lmb_rgn_lst->count; i++) {
539 phys_addr_t rgnbase = rgn[i].base;
540 phys_size_t rgnsize = rgn[i].size;
541 if (lmb_addrs_overlap(base, size, rgnbase, rgnsize))
545 return (i < lmb_rgn_lst->count) ? i : -1;
548 static phys_addr_t lmb_align_down(phys_addr_t addr, phys_size_t size)
550 return addr & ~(size - 1);
553 static phys_addr_t __lmb_alloc_base(phys_size_t size, ulong align,
554 phys_addr_t max_addr, enum lmb_flags flags)
557 phys_addr_t base = 0;
558 phys_addr_t res_base;
559 struct lmb_region *lmb_used = lmb.used_mem.data;
560 struct lmb_region *lmb_memory = lmb.free_mem.data;
562 for (i = lmb.free_mem.count - 1; i >= 0; i--) {
563 phys_addr_t lmbbase = lmb_memory[i].base;
564 phys_size_t lmbsize = lmb_memory[i].size;
568 if (max_addr == LMB_ALLOC_ANYWHERE)
569 base = lmb_align_down(lmbbase + lmbsize - size, align);
570 else if (lmbbase < max_addr) {
571 base = lmbbase + lmbsize;
574 base = min(base, max_addr);
575 base = lmb_align_down(base - size, align);
579 while (base && lmbbase <= base) {
580 rgn = lmb_overlaps_region(&lmb.used_mem, base, size);
582 /* This area isn't reserved, take it */
583 if (lmb_add_region_flags(&lmb.used_mem, base,
589 res_base = lmb_used[rgn].base;
592 base = lmb_align_down(res_base - size, align);
598 phys_addr_t lmb_alloc(phys_size_t size, ulong align)
600 return lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE);
603 phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr)
607 alloc = __lmb_alloc_base(size, align, max_addr, LMB_NONE);
610 printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
611 (ulong)size, (ulong)max_addr);
616 static phys_addr_t __lmb_alloc_addr(phys_addr_t base, phys_size_t size,
617 enum lmb_flags flags)
620 struct lmb_region *lmb_memory = lmb.free_mem.data;
622 /* Check if the requested address is in one of the memory regions */
623 rgn = lmb_overlaps_region(&lmb.free_mem, base, size);
626 * Check if the requested end address is in the same memory
629 if (lmb_addrs_overlap(lmb_memory[rgn].base,
630 lmb_memory[rgn].size,
631 base + size - 1, 1)) {
632 /* ok, reserve the memory */
633 if (lmb_reserve_flags(base, size, flags) >= 0)
642 * Try to allocate a specific address range: must be in defined memory but not
645 phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size)
647 return __lmb_alloc_addr(base, size, LMB_NONE);
650 /* Return number of bytes from a given address that are free */
651 phys_size_t lmb_get_free_size(phys_addr_t addr)
655 struct lmb_region *lmb_used = lmb.used_mem.data;
656 struct lmb_region *lmb_memory = lmb.free_mem.data;
658 /* check if the requested address is in the memory regions */
659 rgn = lmb_overlaps_region(&lmb.free_mem, addr, 1);
661 for (i = 0; i < lmb.used_mem.count; i++) {
662 if (addr < lmb_used[i].base) {
663 /* first reserved range > requested address */
664 return lmb_used[i].base - addr;
666 if (lmb_used[i].base +
667 lmb_used[i].size > addr) {
668 /* requested addr is in this reserved range */
672 /* if we come here: no reserved ranges above requested addr */
673 return lmb_memory[lmb.free_mem.count - 1].base +
674 lmb_memory[lmb.free_mem.count - 1].size - addr;
679 int lmb_is_reserved_flags(phys_addr_t addr, int flags)
682 struct lmb_region *lmb_used = lmb.used_mem.data;
684 for (i = 0; i < lmb.used_mem.count; i++) {
685 phys_addr_t upper = lmb_used[i].base +
686 lmb_used[i].size - 1;
687 if (addr >= lmb_used[i].base && addr <= upper)
688 return (lmb_used[i].flags & flags) == flags;
693 __weak void board_lmb_reserve(void)
695 /* please define platform specific board_lmb_reserve() */
698 static int lmb_setup(void)
702 ret = alist_init(&lmb.free_mem, sizeof(struct lmb_region),
703 (uint)LMB_ALIST_INITIAL_SIZE);
705 log_debug("Unable to initialise the list for LMB free memory\n");
709 ret = alist_init(&lmb.used_mem, sizeof(struct lmb_region),
710 (uint)LMB_ALIST_INITIAL_SIZE);
712 log_debug("Unable to initialise the list for LMB used memory\n");
720 * lmb_init() - Initialise the LMB module
722 * Initialise the LMB lists needed for keeping the memory map. There
723 * are two lists, in form of alloced list data structure. One for the
724 * available memory, and one for the used memory. Initialise the two
725 * lists as part of board init. Add memory to the available memory
726 * list and reserve common areas by adding them to the used memory
729 * Return: 0 on success, -ve on error
737 log_info("Unable to init LMB\n");
743 /* Reserve the U-Boot image region once U-Boot has relocated */
744 if (spl_phase() == PHASE_SPL)
745 lmb_reserve_common_spl();
746 else if (spl_phase() == PHASE_BOARD_R)
747 lmb_reserve_common((void *)gd->fdt_blob);
752 #if CONFIG_IS_ENABLED(UNIT_TEST)
753 struct lmb *lmb_get(void)
758 int lmb_push(struct lmb *store)
770 void lmb_pop(struct lmb *store)
772 alist_uninit(&lmb.free_mem);
773 alist_uninit(&lmb.used_mem);
776 #endif /* UNIT_TEST */