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.
15 #include <asm/global_data.h>
16 #include <asm/sections.h>
18 DECLARE_GLOBAL_DATA_PTR;
20 #define LMB_ALLOC_ANYWHERE 0
22 static void lmb_dump_region(struct lmb_region *rgn, char *name)
24 unsigned long long base, size, end;
28 printf(" %s.cnt = 0x%lx\n", name, rgn->cnt);
30 for (i = 0; i < rgn->cnt; i++) {
31 base = rgn->region[i].base;
32 size = rgn->region[i].size;
33 end = base + size - 1;
34 flags = rgn->region[i].flags;
36 printf(" %s[%d]\t[0x%llx-0x%llx], 0x%08llx bytes flags: %x\n",
37 name, i, base, end, size, flags);
41 void lmb_dump_all_force(struct lmb *lmb)
43 printf("lmb_dump_all:\n");
44 lmb_dump_region(&lmb->memory, "memory");
45 lmb_dump_region(&lmb->reserved, "reserved");
48 void lmb_dump_all(struct lmb *lmb)
51 lmb_dump_all_force(lmb);
55 static long lmb_addrs_overlap(phys_addr_t base1, phys_size_t size1,
56 phys_addr_t base2, phys_size_t size2)
58 const phys_addr_t base1_end = base1 + size1 - 1;
59 const phys_addr_t base2_end = base2 + size2 - 1;
61 return ((base1 <= base2_end) && (base2 <= base1_end));
64 static long lmb_addrs_adjacent(phys_addr_t base1, phys_size_t size1,
65 phys_addr_t base2, phys_size_t size2)
67 if (base2 == base1 + size1)
69 else if (base1 == base2 + size2)
75 static long lmb_regions_adjacent(struct lmb_region *rgn, unsigned long r1,
78 phys_addr_t base1 = rgn->region[r1].base;
79 phys_size_t size1 = rgn->region[r1].size;
80 phys_addr_t base2 = rgn->region[r2].base;
81 phys_size_t size2 = rgn->region[r2].size;
83 return lmb_addrs_adjacent(base1, size1, base2, size2);
86 static void lmb_remove_region(struct lmb_region *rgn, unsigned long r)
90 for (i = r; i < rgn->cnt - 1; i++) {
91 rgn->region[i].base = rgn->region[i + 1].base;
92 rgn->region[i].size = rgn->region[i + 1].size;
93 rgn->region[i].flags = rgn->region[i + 1].flags;
98 /* Assumption: base addr of region 1 < base addr of region 2 */
99 static void lmb_coalesce_regions(struct lmb_region *rgn, unsigned long r1,
102 rgn->region[r1].size += rgn->region[r2].size;
103 lmb_remove_region(rgn, r2);
106 void lmb_init(struct lmb *lmb)
108 #if IS_ENABLED(CONFIG_LMB_USE_MAX_REGIONS)
109 lmb->memory.max = CONFIG_LMB_MAX_REGIONS;
110 lmb->reserved.max = CONFIG_LMB_MAX_REGIONS;
111 #elif defined(CONFIG_LMB_MEMORY_REGIONS)
112 lmb->memory.max = CONFIG_LMB_MEMORY_REGIONS;
113 lmb->reserved.max = CONFIG_LMB_RESERVED_REGIONS;
114 lmb->memory.region = lmb->memory_regions;
115 lmb->reserved.region = lmb->reserved_regions;
118 lmb->reserved.cnt = 0;
121 void arch_lmb_reserve_generic(struct lmb *lmb, ulong sp, ulong end, ulong align)
127 * Reserve memory from aligned address below the bottom of U-Boot stack
128 * until end of U-Boot area using LMB to prevent U-Boot from overwriting
131 debug("## Current stack ends at 0x%08lx ", sp);
133 /* adjust sp by 4K to be safe */
135 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
136 if (!gd->bd->bi_dram[bank].size ||
137 sp < gd->bd->bi_dram[bank].start)
139 /* Watch out for RAM at end of address space! */
140 bank_end = gd->bd->bi_dram[bank].start +
141 gd->bd->bi_dram[bank].size - 1;
147 lmb_reserve(lmb, sp, bank_end - sp + 1);
149 if (gd->flags & GD_FLG_SKIP_RELOC)
150 lmb_reserve(lmb, (phys_addr_t)(uintptr_t)_start, gd->mon_len);
156 static void lmb_reserve_common(struct lmb *lmb, void *fdt_blob)
158 arch_lmb_reserve(lmb);
159 board_lmb_reserve(lmb);
161 if (CONFIG_IS_ENABLED(OF_LIBFDT) && fdt_blob)
162 boot_fdt_add_mem_rsv_regions(lmb, fdt_blob);
165 /* Initialize the struct, add memory and call arch/board reserve functions */
166 void lmb_init_and_reserve(struct lmb *lmb, struct bd_info *bd, void *fdt_blob)
172 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
173 if (bd->bi_dram[i].size) {
174 lmb_add(lmb, bd->bi_dram[i].start,
175 bd->bi_dram[i].size);
179 lmb_reserve_common(lmb, fdt_blob);
182 /* Initialize the struct, add memory and call arch/board reserve functions */
183 void lmb_init_and_reserve_range(struct lmb *lmb, phys_addr_t base,
184 phys_size_t size, void *fdt_blob)
187 lmb_add(lmb, base, size);
188 lmb_reserve_common(lmb, fdt_blob);
191 /* This routine called with relocation disabled. */
192 static long lmb_add_region_flags(struct lmb_region *rgn, phys_addr_t base,
193 phys_size_t size, enum lmb_flags flags)
195 unsigned long coalesced = 0;
199 rgn->region[0].base = base;
200 rgn->region[0].size = size;
201 rgn->region[0].flags = flags;
206 /* First try and coalesce this LMB with another. */
207 for (i = 0; i < rgn->cnt; i++) {
208 phys_addr_t rgnbase = rgn->region[i].base;
209 phys_size_t rgnsize = rgn->region[i].size;
210 phys_size_t rgnflags = rgn->region[i].flags;
212 if (rgnbase == base && rgnsize == size) {
213 if (flags == rgnflags)
214 /* Already have this region, so we're done */
217 return -1; /* regions with new flags */
220 adjacent = lmb_addrs_adjacent(base, size, rgnbase, rgnsize);
222 if (flags != rgnflags)
224 rgn->region[i].base -= size;
225 rgn->region[i].size += size;
228 } else if (adjacent < 0) {
229 if (flags != rgnflags)
231 rgn->region[i].size += size;
234 } else if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) {
235 /* regions overlap */
240 if ((i < rgn->cnt - 1) && lmb_regions_adjacent(rgn, i, i + 1)) {
241 if (rgn->region[i].flags == rgn->region[i + 1].flags) {
242 lmb_coalesce_regions(rgn, i, i + 1);
249 if (rgn->cnt >= rgn->max)
252 /* Couldn't coalesce the LMB, so add it to the sorted table. */
253 for (i = rgn->cnt-1; i >= 0; i--) {
254 if (base < rgn->region[i].base) {
255 rgn->region[i + 1].base = rgn->region[i].base;
256 rgn->region[i + 1].size = rgn->region[i].size;
257 rgn->region[i + 1].flags = rgn->region[i].flags;
259 rgn->region[i + 1].base = base;
260 rgn->region[i + 1].size = size;
261 rgn->region[i + 1].flags = flags;
266 if (base < rgn->region[0].base) {
267 rgn->region[0].base = base;
268 rgn->region[0].size = size;
269 rgn->region[0].flags = flags;
277 static long lmb_add_region(struct lmb_region *rgn, phys_addr_t base,
280 return lmb_add_region_flags(rgn, base, size, LMB_NONE);
283 /* This routine may be called with relocation disabled. */
284 long lmb_add(struct lmb *lmb, phys_addr_t base, phys_size_t size)
286 struct lmb_region *_rgn = &(lmb->memory);
288 return lmb_add_region(_rgn, base, size);
291 long lmb_free(struct lmb *lmb, phys_addr_t base, phys_size_t size)
293 struct lmb_region *rgn = &(lmb->reserved);
294 phys_addr_t rgnbegin, rgnend;
295 phys_addr_t end = base + size - 1;
298 rgnbegin = rgnend = 0; /* supress gcc warnings */
300 /* Find the region where (base, size) belongs to */
301 for (i = 0; i < rgn->cnt; i++) {
302 rgnbegin = rgn->region[i].base;
303 rgnend = rgnbegin + rgn->region[i].size - 1;
305 if ((rgnbegin <= base) && (end <= rgnend))
309 /* Didn't find the region */
313 /* Check to see if we are removing entire region */
314 if ((rgnbegin == base) && (rgnend == end)) {
315 lmb_remove_region(rgn, i);
319 /* Check to see if region is matching at the front */
320 if (rgnbegin == base) {
321 rgn->region[i].base = end + 1;
322 rgn->region[i].size -= size;
326 /* Check to see if the region is matching at the end */
328 rgn->region[i].size -= size;
333 * We need to split the entry - adjust the current one to the
334 * beginging of the hole and add the region after hole.
336 rgn->region[i].size = base - rgn->region[i].base;
337 return lmb_add_region_flags(rgn, end + 1, rgnend - end,
338 rgn->region[i].flags);
341 long lmb_reserve_flags(struct lmb *lmb, phys_addr_t base, phys_size_t size,
342 enum lmb_flags flags)
344 struct lmb_region *_rgn = &(lmb->reserved);
346 return lmb_add_region_flags(_rgn, base, size, flags);
349 long lmb_reserve(struct lmb *lmb, phys_addr_t base, phys_size_t size)
351 return lmb_reserve_flags(lmb, base, size, LMB_NONE);
354 static long lmb_overlaps_region(struct lmb_region *rgn, phys_addr_t base,
359 for (i = 0; i < rgn->cnt; i++) {
360 phys_addr_t rgnbase = rgn->region[i].base;
361 phys_size_t rgnsize = rgn->region[i].size;
362 if (lmb_addrs_overlap(base, size, rgnbase, rgnsize))
366 return (i < rgn->cnt) ? i : -1;
369 phys_addr_t lmb_alloc(struct lmb *lmb, phys_size_t size, ulong align)
371 return lmb_alloc_base(lmb, size, align, LMB_ALLOC_ANYWHERE);
374 phys_addr_t lmb_alloc_base(struct lmb *lmb, phys_size_t size, ulong align, phys_addr_t max_addr)
378 alloc = __lmb_alloc_base(lmb, size, align, max_addr);
381 printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
382 (ulong)size, (ulong)max_addr);
387 static phys_addr_t lmb_align_down(phys_addr_t addr, phys_size_t size)
389 return addr & ~(size - 1);
392 phys_addr_t __lmb_alloc_base(struct lmb *lmb, phys_size_t size, ulong align, phys_addr_t max_addr)
395 phys_addr_t base = 0;
396 phys_addr_t res_base;
398 for (i = lmb->memory.cnt - 1; i >= 0; i--) {
399 phys_addr_t lmbbase = lmb->memory.region[i].base;
400 phys_size_t lmbsize = lmb->memory.region[i].size;
404 if (max_addr == LMB_ALLOC_ANYWHERE)
405 base = lmb_align_down(lmbbase + lmbsize - size, align);
406 else if (lmbbase < max_addr) {
407 base = lmbbase + lmbsize;
410 base = min(base, max_addr);
411 base = lmb_align_down(base - size, align);
415 while (base && lmbbase <= base) {
416 rgn = lmb_overlaps_region(&lmb->reserved, base, size);
418 /* This area isn't reserved, take it */
419 if (lmb_add_region(&lmb->reserved, base,
424 res_base = lmb->reserved.region[rgn].base;
427 base = lmb_align_down(res_base - size, align);
434 * Try to allocate a specific address range: must be in defined memory but not
437 phys_addr_t lmb_alloc_addr(struct lmb *lmb, phys_addr_t base, phys_size_t size)
441 /* Check if the requested address is in one of the memory regions */
442 rgn = lmb_overlaps_region(&lmb->memory, base, size);
445 * Check if the requested end address is in the same memory
448 if (lmb_addrs_overlap(lmb->memory.region[rgn].base,
449 lmb->memory.region[rgn].size,
450 base + size - 1, 1)) {
451 /* ok, reserve the memory */
452 if (lmb_reserve(lmb, base, size) >= 0)
459 /* Return number of bytes from a given address that are free */
460 phys_size_t lmb_get_free_size(struct lmb *lmb, phys_addr_t addr)
465 /* check if the requested address is in the memory regions */
466 rgn = lmb_overlaps_region(&lmb->memory, addr, 1);
468 for (i = 0; i < lmb->reserved.cnt; i++) {
469 if (addr < lmb->reserved.region[i].base) {
470 /* first reserved range > requested address */
471 return lmb->reserved.region[i].base - addr;
473 if (lmb->reserved.region[i].base +
474 lmb->reserved.region[i].size > addr) {
475 /* requested addr is in this reserved range */
479 /* if we come here: no reserved ranges above requested addr */
480 return lmb->memory.region[lmb->memory.cnt - 1].base +
481 lmb->memory.region[lmb->memory.cnt - 1].size - addr;
486 int lmb_is_reserved_flags(struct lmb *lmb, phys_addr_t addr, int flags)
490 for (i = 0; i < lmb->reserved.cnt; i++) {
491 phys_addr_t upper = lmb->reserved.region[i].base +
492 lmb->reserved.region[i].size - 1;
493 if ((addr >= lmb->reserved.region[i].base) && (addr <= upper))
494 return (lmb->reserved.region[i].flags & flags) == flags;
499 int lmb_is_reserved(struct lmb *lmb, phys_addr_t addr)
501 return lmb_is_reserved_flags(lmb, addr, LMB_NONE);
504 __weak void board_lmb_reserve(struct lmb *lmb)
506 /* please define platform specific board_lmb_reserve() */
509 __weak void arch_lmb_reserve(struct lmb *lmb)
511 /* please define platform specific arch_lmb_reserve() */