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.
12 #define LMB_ALLOC_ANYWHERE 0
14 void lmb_dump_all(struct lmb *lmb)
19 debug("lmb_dump_all:\n");
20 debug(" memory.cnt = 0x%lx\n", lmb->memory.cnt);
21 debug(" memory.size = 0x%llx\n",
22 (unsigned long long)lmb->memory.size);
23 for (i = 0; i < lmb->memory.cnt; i++) {
24 debug(" memory.reg[0x%lx].base = 0x%llx\n", i,
25 (unsigned long long)lmb->memory.region[i].base);
26 debug(" .size = 0x%llx\n",
27 (unsigned long long)lmb->memory.region[i].size);
30 debug("\n reserved.cnt = 0x%lx\n",
32 debug(" reserved.size = 0x%llx\n",
33 (unsigned long long)lmb->reserved.size);
34 for (i = 0; i < lmb->reserved.cnt; i++) {
35 debug(" reserved.reg[0x%lx].base = 0x%llx\n", i,
36 (unsigned long long)lmb->reserved.region[i].base);
37 debug(" .size = 0x%llx\n",
38 (unsigned long long)lmb->reserved.region[i].size);
43 static long lmb_addrs_overlap(phys_addr_t base1, phys_size_t size1,
44 phys_addr_t base2, phys_size_t size2)
46 const phys_addr_t base1_end = base1 + size1 - 1;
47 const phys_addr_t base2_end = base2 + size2 - 1;
49 return ((base1 <= base2_end) && (base2 <= base1_end));
52 static long lmb_addrs_adjacent(phys_addr_t base1, phys_size_t size1,
53 phys_addr_t base2, phys_size_t size2)
55 if (base2 == base1 + size1)
57 else if (base1 == base2 + size2)
63 static long lmb_regions_adjacent(struct lmb_region *rgn, unsigned long r1,
66 phys_addr_t base1 = rgn->region[r1].base;
67 phys_size_t size1 = rgn->region[r1].size;
68 phys_addr_t base2 = rgn->region[r2].base;
69 phys_size_t size2 = rgn->region[r2].size;
71 return lmb_addrs_adjacent(base1, size1, base2, size2);
74 static void lmb_remove_region(struct lmb_region *rgn, unsigned long r)
78 for (i = r; i < rgn->cnt - 1; i++) {
79 rgn->region[i].base = rgn->region[i + 1].base;
80 rgn->region[i].size = rgn->region[i + 1].size;
85 /* Assumption: base addr of region 1 < base addr of region 2 */
86 static void lmb_coalesce_regions(struct lmb_region *rgn, unsigned long r1,
89 rgn->region[r1].size += rgn->region[r2].size;
90 lmb_remove_region(rgn, r2);
93 void lmb_init(struct lmb *lmb)
97 lmb->reserved.cnt = 0;
98 lmb->reserved.size = 0;
101 static void lmb_reserve_common(struct lmb *lmb, void *fdt_blob)
103 arch_lmb_reserve(lmb);
104 board_lmb_reserve(lmb);
106 if (IMAGE_ENABLE_OF_LIBFDT && fdt_blob)
107 boot_fdt_add_mem_rsv_regions(lmb, fdt_blob);
110 /* Initialize the struct, add memory and call arch/board reserve functions */
111 void lmb_init_and_reserve(struct lmb *lmb, bd_t *bd, void *fdt_blob)
113 #ifdef CONFIG_NR_DRAM_BANKS
118 #ifdef CONFIG_NR_DRAM_BANKS
119 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
120 if (bd->bi_dram[i].size) {
121 lmb_add(lmb, bd->bi_dram[i].start,
122 bd->bi_dram[i].size);
127 lmb_add(lmb, bd->bi_memstart, bd->bi_memsize);
129 lmb_reserve_common(lmb, fdt_blob);
132 /* Initialize the struct, add memory and call arch/board reserve functions */
133 void lmb_init_and_reserve_range(struct lmb *lmb, phys_addr_t base,
134 phys_size_t size, void *fdt_blob)
137 lmb_add(lmb, base, size);
138 lmb_reserve_common(lmb, fdt_blob);
141 /* This routine called with relocation disabled. */
142 static long lmb_add_region(struct lmb_region *rgn, phys_addr_t base, phys_size_t size)
144 unsigned long coalesced = 0;
148 rgn->region[0].base = base;
149 rgn->region[0].size = size;
154 /* First try and coalesce this LMB with another. */
155 for (i = 0; i < rgn->cnt; i++) {
156 phys_addr_t rgnbase = rgn->region[i].base;
157 phys_size_t rgnsize = rgn->region[i].size;
159 if ((rgnbase == base) && (rgnsize == size))
160 /* Already have this region, so we're done */
163 adjacent = lmb_addrs_adjacent(base, size, rgnbase, rgnsize);
165 rgn->region[i].base -= size;
166 rgn->region[i].size += size;
169 } else if (adjacent < 0) {
170 rgn->region[i].size += size;
173 } else if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) {
174 /* regions overlap */
179 if ((i < rgn->cnt - 1) && lmb_regions_adjacent(rgn, i, i + 1)) {
180 lmb_coalesce_regions(rgn, i, i + 1);
186 if (rgn->cnt >= MAX_LMB_REGIONS)
189 /* Couldn't coalesce the LMB, so add it to the sorted table. */
190 for (i = rgn->cnt-1; i >= 0; i--) {
191 if (base < rgn->region[i].base) {
192 rgn->region[i + 1].base = rgn->region[i].base;
193 rgn->region[i + 1].size = rgn->region[i].size;
195 rgn->region[i + 1].base = base;
196 rgn->region[i + 1].size = size;
201 if (base < rgn->region[0].base) {
202 rgn->region[0].base = base;
203 rgn->region[0].size = size;
211 /* This routine may be called with relocation disabled. */
212 long lmb_add(struct lmb *lmb, phys_addr_t base, phys_size_t size)
214 struct lmb_region *_rgn = &(lmb->memory);
216 return lmb_add_region(_rgn, base, size);
219 long lmb_free(struct lmb *lmb, phys_addr_t base, phys_size_t size)
221 struct lmb_region *rgn = &(lmb->reserved);
222 phys_addr_t rgnbegin, rgnend;
223 phys_addr_t end = base + size - 1;
226 rgnbegin = rgnend = 0; /* supress gcc warnings */
228 /* Find the region where (base, size) belongs to */
229 for (i = 0; i < rgn->cnt; i++) {
230 rgnbegin = rgn->region[i].base;
231 rgnend = rgnbegin + rgn->region[i].size - 1;
233 if ((rgnbegin <= base) && (end <= rgnend))
237 /* Didn't find the region */
241 /* Check to see if we are removing entire region */
242 if ((rgnbegin == base) && (rgnend == end)) {
243 lmb_remove_region(rgn, i);
247 /* Check to see if region is matching at the front */
248 if (rgnbegin == base) {
249 rgn->region[i].base = end + 1;
250 rgn->region[i].size -= size;
254 /* Check to see if the region is matching at the end */
256 rgn->region[i].size -= size;
261 * We need to split the entry - adjust the current one to the
262 * beginging of the hole and add the region after hole.
264 rgn->region[i].size = base - rgn->region[i].base;
265 return lmb_add_region(rgn, end + 1, rgnend - end);
268 long lmb_reserve(struct lmb *lmb, phys_addr_t base, phys_size_t size)
270 struct lmb_region *_rgn = &(lmb->reserved);
272 return lmb_add_region(_rgn, base, size);
275 static long lmb_overlaps_region(struct lmb_region *rgn, phys_addr_t base,
280 for (i = 0; i < rgn->cnt; i++) {
281 phys_addr_t rgnbase = rgn->region[i].base;
282 phys_size_t rgnsize = rgn->region[i].size;
283 if (lmb_addrs_overlap(base, size, rgnbase, rgnsize))
287 return (i < rgn->cnt) ? i : -1;
290 phys_addr_t lmb_alloc(struct lmb *lmb, phys_size_t size, ulong align)
292 return lmb_alloc_base(lmb, size, align, LMB_ALLOC_ANYWHERE);
295 phys_addr_t lmb_alloc_base(struct lmb *lmb, phys_size_t size, ulong align, phys_addr_t max_addr)
299 alloc = __lmb_alloc_base(lmb, size, align, max_addr);
302 printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
303 (ulong)size, (ulong)max_addr);
308 static phys_addr_t lmb_align_down(phys_addr_t addr, phys_size_t size)
310 return addr & ~(size - 1);
313 phys_addr_t __lmb_alloc_base(struct lmb *lmb, phys_size_t size, ulong align, phys_addr_t max_addr)
316 phys_addr_t base = 0;
317 phys_addr_t res_base;
319 for (i = lmb->memory.cnt - 1; i >= 0; i--) {
320 phys_addr_t lmbbase = lmb->memory.region[i].base;
321 phys_size_t lmbsize = lmb->memory.region[i].size;
325 if (max_addr == LMB_ALLOC_ANYWHERE)
326 base = lmb_align_down(lmbbase + lmbsize - size, align);
327 else if (lmbbase < max_addr) {
328 base = lmbbase + lmbsize;
331 base = min(base, max_addr);
332 base = lmb_align_down(base - size, align);
336 while (base && lmbbase <= base) {
337 rgn = lmb_overlaps_region(&lmb->reserved, base, size);
339 /* This area isn't reserved, take it */
340 if (lmb_add_region(&lmb->reserved, base,
345 res_base = lmb->reserved.region[rgn].base;
348 base = lmb_align_down(res_base - size, align);
355 * Try to allocate a specific address range: must be in defined memory but not
358 phys_addr_t lmb_alloc_addr(struct lmb *lmb, phys_addr_t base, phys_size_t size)
362 /* Check if the requested address is in one of the memory regions */
363 rgn = lmb_overlaps_region(&lmb->memory, base, size);
366 * Check if the requested end address is in the same memory
369 if (lmb_addrs_overlap(lmb->memory.region[rgn].base,
370 lmb->memory.region[rgn].size,
371 base + size - 1, 1)) {
372 /* ok, reserve the memory */
373 if (lmb_reserve(lmb, base, size) >= 0)
380 /* Return number of bytes from a given address that are free */
381 phys_size_t lmb_get_free_size(struct lmb *lmb, phys_addr_t addr)
386 /* check if the requested address is in the memory regions */
387 rgn = lmb_overlaps_region(&lmb->memory, addr, 1);
389 for (i = 0; i < lmb->reserved.cnt; i++) {
390 if (addr < lmb->reserved.region[i].base) {
391 /* first reserved range > requested address */
392 return lmb->reserved.region[i].base - addr;
394 if (lmb->reserved.region[i].base +
395 lmb->reserved.region[i].size > addr) {
396 /* requested addr is in this reserved range */
400 /* if we come here: no reserved ranges above requested addr */
401 return lmb->memory.region[lmb->memory.cnt - 1].base +
402 lmb->memory.region[lmb->memory.cnt - 1].size - addr;
407 int lmb_is_reserved(struct lmb *lmb, phys_addr_t addr)
411 for (i = 0; i < lmb->reserved.cnt; i++) {
412 phys_addr_t upper = lmb->reserved.region[i].base +
413 lmb->reserved.region[i].size - 1;
414 if ((addr >= lmb->reserved.region[i].base) && (addr <= upper))
420 __weak void board_lmb_reserve(struct lmb *lmb)
422 /* please define platform specific board_lmb_reserve() */
425 __weak void arch_lmb_reserve(struct lmb *lmb)
427 /* please define platform specific arch_lmb_reserve() */