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 #define LMB_ALLOC_ANYWHERE 0
17 void lmb_dump_all(struct lmb *lmb)
22 debug("lmb_dump_all:\n");
23 debug(" memory.cnt = 0x%lx\n", lmb->memory.cnt);
24 debug(" memory.size = 0x%llx\n",
25 (unsigned long long)lmb->memory.size);
26 for (i = 0; i < lmb->memory.cnt; i++) {
27 debug(" memory.reg[0x%lx].base = 0x%llx\n", i,
28 (unsigned long long)lmb->memory.region[i].base);
29 debug(" .size = 0x%llx\n",
30 (unsigned long long)lmb->memory.region[i].size);
33 debug("\n reserved.cnt = 0x%lx\n",
35 debug(" reserved.size = 0x%llx\n",
36 (unsigned long long)lmb->reserved.size);
37 for (i = 0; i < lmb->reserved.cnt; i++) {
38 debug(" reserved.reg[0x%lx].base = 0x%llx\n", i,
39 (unsigned long long)lmb->reserved.region[i].base);
40 debug(" .size = 0x%llx\n",
41 (unsigned long long)lmb->reserved.region[i].size);
46 static long lmb_addrs_overlap(phys_addr_t base1, phys_size_t size1,
47 phys_addr_t base2, phys_size_t size2)
49 const phys_addr_t base1_end = base1 + size1 - 1;
50 const phys_addr_t base2_end = base2 + size2 - 1;
52 return ((base1 <= base2_end) && (base2 <= base1_end));
55 static long lmb_addrs_adjacent(phys_addr_t base1, phys_size_t size1,
56 phys_addr_t base2, phys_size_t size2)
58 if (base2 == base1 + size1)
60 else if (base1 == base2 + size2)
66 static long lmb_regions_adjacent(struct lmb_region *rgn, unsigned long r1,
69 phys_addr_t base1 = rgn->region[r1].base;
70 phys_size_t size1 = rgn->region[r1].size;
71 phys_addr_t base2 = rgn->region[r2].base;
72 phys_size_t size2 = rgn->region[r2].size;
74 return lmb_addrs_adjacent(base1, size1, base2, size2);
77 static void lmb_remove_region(struct lmb_region *rgn, unsigned long r)
81 for (i = r; i < rgn->cnt - 1; i++) {
82 rgn->region[i].base = rgn->region[i + 1].base;
83 rgn->region[i].size = rgn->region[i + 1].size;
88 /* Assumption: base addr of region 1 < base addr of region 2 */
89 static void lmb_coalesce_regions(struct lmb_region *rgn, unsigned long r1,
92 rgn->region[r1].size += rgn->region[r2].size;
93 lmb_remove_region(rgn, r2);
96 void lmb_init(struct lmb *lmb)
100 lmb->reserved.cnt = 0;
101 lmb->reserved.size = 0;
104 static void lmb_reserve_common(struct lmb *lmb, void *fdt_blob)
106 arch_lmb_reserve(lmb);
107 board_lmb_reserve(lmb);
109 if (IMAGE_ENABLE_OF_LIBFDT && fdt_blob)
110 boot_fdt_add_mem_rsv_regions(lmb, fdt_blob);
113 /* Initialize the struct, add memory and call arch/board reserve functions */
114 void lmb_init_and_reserve(struct lmb *lmb, struct bd_info *bd, void *fdt_blob)
116 #ifdef CONFIG_NR_DRAM_BANKS
121 #ifdef CONFIG_NR_DRAM_BANKS
122 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
123 if (bd->bi_dram[i].size) {
124 lmb_add(lmb, bd->bi_dram[i].start,
125 bd->bi_dram[i].size);
130 lmb_add(lmb, bd->bi_memstart, bd->bi_memsize);
132 lmb_reserve_common(lmb, fdt_blob);
135 /* Initialize the struct, add memory and call arch/board reserve functions */
136 void lmb_init_and_reserve_range(struct lmb *lmb, phys_addr_t base,
137 phys_size_t size, void *fdt_blob)
140 lmb_add(lmb, base, size);
141 lmb_reserve_common(lmb, fdt_blob);
144 /* This routine called with relocation disabled. */
145 static long lmb_add_region(struct lmb_region *rgn, phys_addr_t base, phys_size_t size)
147 unsigned long coalesced = 0;
151 rgn->region[0].base = base;
152 rgn->region[0].size = size;
157 /* First try and coalesce this LMB with another. */
158 for (i = 0; i < rgn->cnt; i++) {
159 phys_addr_t rgnbase = rgn->region[i].base;
160 phys_size_t rgnsize = rgn->region[i].size;
162 if ((rgnbase == base) && (rgnsize == size))
163 /* Already have this region, so we're done */
166 adjacent = lmb_addrs_adjacent(base, size, rgnbase, rgnsize);
168 rgn->region[i].base -= size;
169 rgn->region[i].size += size;
172 } else if (adjacent < 0) {
173 rgn->region[i].size += size;
176 } else if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) {
177 /* regions overlap */
182 if ((i < rgn->cnt - 1) && lmb_regions_adjacent(rgn, i, i + 1)) {
183 lmb_coalesce_regions(rgn, i, i + 1);
189 if (rgn->cnt >= MAX_LMB_REGIONS)
192 /* Couldn't coalesce the LMB, so add it to the sorted table. */
193 for (i = rgn->cnt-1; i >= 0; i--) {
194 if (base < rgn->region[i].base) {
195 rgn->region[i + 1].base = rgn->region[i].base;
196 rgn->region[i + 1].size = rgn->region[i].size;
198 rgn->region[i + 1].base = base;
199 rgn->region[i + 1].size = size;
204 if (base < rgn->region[0].base) {
205 rgn->region[0].base = base;
206 rgn->region[0].size = size;
214 /* This routine may be called with relocation disabled. */
215 long lmb_add(struct lmb *lmb, phys_addr_t base, phys_size_t size)
217 struct lmb_region *_rgn = &(lmb->memory);
219 return lmb_add_region(_rgn, base, size);
222 long lmb_free(struct lmb *lmb, phys_addr_t base, phys_size_t size)
224 struct lmb_region *rgn = &(lmb->reserved);
225 phys_addr_t rgnbegin, rgnend;
226 phys_addr_t end = base + size - 1;
229 rgnbegin = rgnend = 0; /* supress gcc warnings */
231 /* Find the region where (base, size) belongs to */
232 for (i = 0; i < rgn->cnt; i++) {
233 rgnbegin = rgn->region[i].base;
234 rgnend = rgnbegin + rgn->region[i].size - 1;
236 if ((rgnbegin <= base) && (end <= rgnend))
240 /* Didn't find the region */
244 /* Check to see if we are removing entire region */
245 if ((rgnbegin == base) && (rgnend == end)) {
246 lmb_remove_region(rgn, i);
250 /* Check to see if region is matching at the front */
251 if (rgnbegin == base) {
252 rgn->region[i].base = end + 1;
253 rgn->region[i].size -= size;
257 /* Check to see if the region is matching at the end */
259 rgn->region[i].size -= size;
264 * We need to split the entry - adjust the current one to the
265 * beginging of the hole and add the region after hole.
267 rgn->region[i].size = base - rgn->region[i].base;
268 return lmb_add_region(rgn, end + 1, rgnend - end);
271 long lmb_reserve(struct lmb *lmb, phys_addr_t base, phys_size_t size)
273 struct lmb_region *_rgn = &(lmb->reserved);
275 return lmb_add_region(_rgn, base, size);
278 static long lmb_overlaps_region(struct lmb_region *rgn, phys_addr_t base,
283 for (i = 0; i < rgn->cnt; i++) {
284 phys_addr_t rgnbase = rgn->region[i].base;
285 phys_size_t rgnsize = rgn->region[i].size;
286 if (lmb_addrs_overlap(base, size, rgnbase, rgnsize))
290 return (i < rgn->cnt) ? i : -1;
293 phys_addr_t lmb_alloc(struct lmb *lmb, phys_size_t size, ulong align)
295 return lmb_alloc_base(lmb, size, align, LMB_ALLOC_ANYWHERE);
298 phys_addr_t lmb_alloc_base(struct lmb *lmb, phys_size_t size, ulong align, phys_addr_t max_addr)
302 alloc = __lmb_alloc_base(lmb, size, align, max_addr);
305 printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
306 (ulong)size, (ulong)max_addr);
311 static phys_addr_t lmb_align_down(phys_addr_t addr, phys_size_t size)
313 return addr & ~(size - 1);
316 phys_addr_t __lmb_alloc_base(struct lmb *lmb, phys_size_t size, ulong align, phys_addr_t max_addr)
319 phys_addr_t base = 0;
320 phys_addr_t res_base;
322 for (i = lmb->memory.cnt - 1; i >= 0; i--) {
323 phys_addr_t lmbbase = lmb->memory.region[i].base;
324 phys_size_t lmbsize = lmb->memory.region[i].size;
328 if (max_addr == LMB_ALLOC_ANYWHERE)
329 base = lmb_align_down(lmbbase + lmbsize - size, align);
330 else if (lmbbase < max_addr) {
331 base = lmbbase + lmbsize;
334 base = min(base, max_addr);
335 base = lmb_align_down(base - size, align);
339 while (base && lmbbase <= base) {
340 rgn = lmb_overlaps_region(&lmb->reserved, base, size);
342 /* This area isn't reserved, take it */
343 if (lmb_add_region(&lmb->reserved, base,
348 res_base = lmb->reserved.region[rgn].base;
351 base = lmb_align_down(res_base - size, align);
358 * Try to allocate a specific address range: must be in defined memory but not
361 phys_addr_t lmb_alloc_addr(struct lmb *lmb, phys_addr_t base, phys_size_t size)
365 /* Check if the requested address is in one of the memory regions */
366 rgn = lmb_overlaps_region(&lmb->memory, base, size);
369 * Check if the requested end address is in the same memory
372 if (lmb_addrs_overlap(lmb->memory.region[rgn].base,
373 lmb->memory.region[rgn].size,
374 base + size - 1, 1)) {
375 /* ok, reserve the memory */
376 if (lmb_reserve(lmb, base, size) >= 0)
383 /* Return number of bytes from a given address that are free */
384 phys_size_t lmb_get_free_size(struct lmb *lmb, phys_addr_t addr)
389 /* check if the requested address is in the memory regions */
390 rgn = lmb_overlaps_region(&lmb->memory, addr, 1);
392 for (i = 0; i < lmb->reserved.cnt; i++) {
393 if (addr < lmb->reserved.region[i].base) {
394 /* first reserved range > requested address */
395 return lmb->reserved.region[i].base - addr;
397 if (lmb->reserved.region[i].base +
398 lmb->reserved.region[i].size > addr) {
399 /* requested addr is in this reserved range */
403 /* if we come here: no reserved ranges above requested addr */
404 return lmb->memory.region[lmb->memory.cnt - 1].base +
405 lmb->memory.region[lmb->memory.cnt - 1].size - addr;
410 int lmb_is_reserved(struct lmb *lmb, phys_addr_t addr)
414 for (i = 0; i < lmb->reserved.cnt; i++) {
415 phys_addr_t upper = lmb->reserved.region[i].base +
416 lmb->reserved.region[i].size - 1;
417 if ((addr >= lmb->reserved.region[i].base) && (addr <= upper))
423 __weak void board_lmb_reserve(struct lmb *lmb)
425 /* please define platform specific board_lmb_reserve() */
428 __weak void arch_lmb_reserve(struct lmb *lmb)
430 /* please define platform specific arch_lmb_reserve() */