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>
17 #include <asm/global_data.h>
18 #include <asm/sections.h>
20 DECLARE_GLOBAL_DATA_PTR;
22 #define LMB_ALLOC_ANYWHERE 0
24 static void lmb_dump_region(struct lmb_region *rgn, char *name)
26 unsigned long long base, size, end;
30 printf(" %s.cnt = 0x%lx\n", name, rgn->cnt);
32 for (i = 0; i < rgn->cnt; i++) {
33 base = rgn->region[i].base;
34 size = rgn->region[i].size;
35 end = base + size - 1;
36 flags = rgn->region[i].flags;
38 printf(" %s[%d]\t[0x%llx-0x%llx], 0x%08llx bytes flags: %x\n",
39 name, i, base, end, size, flags);
43 void lmb_dump_all_force(struct lmb *lmb)
45 printf("lmb_dump_all:\n");
46 lmb_dump_region(&lmb->memory, "memory");
47 lmb_dump_region(&lmb->reserved, "reserved");
50 void lmb_dump_all(struct lmb *lmb)
53 lmb_dump_all_force(lmb);
57 static long lmb_addrs_overlap(phys_addr_t base1, phys_size_t size1,
58 phys_addr_t base2, phys_size_t size2)
60 const phys_addr_t base1_end = base1 + size1 - 1;
61 const phys_addr_t base2_end = base2 + size2 - 1;
63 return ((base1 <= base2_end) && (base2 <= base1_end));
66 static long lmb_addrs_adjacent(phys_addr_t base1, phys_size_t size1,
67 phys_addr_t base2, phys_size_t size2)
69 if (base2 == base1 + size1)
71 else if (base1 == base2 + size2)
77 static long lmb_regions_adjacent(struct lmb_region *rgn, unsigned long r1,
80 phys_addr_t base1 = rgn->region[r1].base;
81 phys_size_t size1 = rgn->region[r1].size;
82 phys_addr_t base2 = rgn->region[r2].base;
83 phys_size_t size2 = rgn->region[r2].size;
85 return lmb_addrs_adjacent(base1, size1, base2, size2);
88 static void lmb_remove_region(struct lmb_region *rgn, unsigned long r)
92 for (i = r; i < rgn->cnt - 1; i++) {
93 rgn->region[i].base = rgn->region[i + 1].base;
94 rgn->region[i].size = rgn->region[i + 1].size;
95 rgn->region[i].flags = rgn->region[i + 1].flags;
100 /* Assumption: base addr of region 1 < base addr of region 2 */
101 static void lmb_coalesce_regions(struct lmb_region *rgn, unsigned long r1,
104 rgn->region[r1].size += rgn->region[r2].size;
105 lmb_remove_region(rgn, r2);
108 void lmb_init(struct lmb *lmb)
110 #if IS_ENABLED(CONFIG_LMB_USE_MAX_REGIONS)
111 lmb->memory.max = CONFIG_LMB_MAX_REGIONS;
112 lmb->reserved.max = CONFIG_LMB_MAX_REGIONS;
113 #elif defined(CONFIG_LMB_MEMORY_REGIONS)
114 lmb->memory.max = CONFIG_LMB_MEMORY_REGIONS;
115 lmb->reserved.max = CONFIG_LMB_RESERVED_REGIONS;
116 lmb->memory.region = lmb->memory_regions;
117 lmb->reserved.region = lmb->reserved_regions;
120 lmb->reserved.cnt = 0;
123 void arch_lmb_reserve_generic(struct lmb *lmb, ulong sp, ulong end, ulong align)
129 * Reserve memory from aligned address below the bottom of U-Boot stack
130 * until end of U-Boot area using LMB to prevent U-Boot from overwriting
133 debug("## Current stack ends at 0x%08lx ", sp);
135 /* adjust sp by 4K to be safe */
137 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
138 if (!gd->bd->bi_dram[bank].size ||
139 sp < gd->bd->bi_dram[bank].start)
141 /* Watch out for RAM at end of address space! */
142 bank_end = gd->bd->bi_dram[bank].start +
143 gd->bd->bi_dram[bank].size - 1;
149 lmb_reserve(lmb, sp, bank_end - sp + 1);
151 if (gd->flags & GD_FLG_SKIP_RELOC)
152 lmb_reserve(lmb, (phys_addr_t)(uintptr_t)_start, gd->mon_len);
159 * efi_lmb_reserve() - add reservations for EFI memory
161 * Add reservations for all EFI memory areas that are not
162 * EFI_CONVENTIONAL_MEMORY.
164 * @lmb: lmb environment
165 * Return: 0 on success, 1 on failure
167 static __maybe_unused int efi_lmb_reserve(struct lmb *lmb)
169 struct efi_mem_desc *memmap = NULL, *map;
170 efi_uintn_t i, map_size = 0;
173 ret = efi_get_memory_map_alloc(&map_size, &memmap);
174 if (ret != EFI_SUCCESS)
177 for (i = 0, map = memmap; i < map_size / sizeof(*map); ++map, ++i) {
178 if (map->type != EFI_CONVENTIONAL_MEMORY) {
179 lmb_reserve_flags(lmb,
180 map_to_sysmem((void *)(uintptr_t)
181 map->physical_start),
182 map->num_pages * EFI_PAGE_SIZE,
183 map->type == EFI_RESERVED_MEMORY_TYPE
184 ? LMB_NOMAP : LMB_NONE);
187 efi_free_pool(memmap);
192 static void lmb_reserve_common(struct lmb *lmb, void *fdt_blob)
194 arch_lmb_reserve(lmb);
195 board_lmb_reserve(lmb);
197 if (CONFIG_IS_ENABLED(OF_LIBFDT) && fdt_blob)
198 boot_fdt_add_mem_rsv_regions(lmb, fdt_blob);
200 if (CONFIG_IS_ENABLED(EFI_LOADER))
201 efi_lmb_reserve(lmb);
204 /* Initialize the struct, add memory and call arch/board reserve functions */
205 void lmb_init_and_reserve(struct lmb *lmb, struct bd_info *bd, void *fdt_blob)
211 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
212 if (bd->bi_dram[i].size) {
213 lmb_add(lmb, bd->bi_dram[i].start,
214 bd->bi_dram[i].size);
218 lmb_reserve_common(lmb, fdt_blob);
221 /* Initialize the struct, add memory and call arch/board reserve functions */
222 void lmb_init_and_reserve_range(struct lmb *lmb, phys_addr_t base,
223 phys_size_t size, void *fdt_blob)
226 lmb_add(lmb, base, size);
227 lmb_reserve_common(lmb, fdt_blob);
230 /* This routine called with relocation disabled. */
231 static long lmb_add_region_flags(struct lmb_region *rgn, phys_addr_t base,
232 phys_size_t size, enum lmb_flags flags)
234 unsigned long coalesced = 0;
238 rgn->region[0].base = base;
239 rgn->region[0].size = size;
240 rgn->region[0].flags = flags;
245 /* First try and coalesce this LMB with another. */
246 for (i = 0; i < rgn->cnt; i++) {
247 phys_addr_t rgnbase = rgn->region[i].base;
248 phys_size_t rgnsize = rgn->region[i].size;
249 phys_size_t rgnflags = rgn->region[i].flags;
250 phys_addr_t end = base + size - 1;
251 phys_addr_t rgnend = rgnbase + rgnsize - 1;
253 if (rgnbase <= base && end <= rgnend) {
254 if (flags == rgnflags)
255 /* Already have this region, so we're done */
258 return -1; /* regions with new flags */
261 adjacent = lmb_addrs_adjacent(base, size, rgnbase, rgnsize);
263 if (flags != rgnflags)
265 rgn->region[i].base -= size;
266 rgn->region[i].size += size;
269 } else if (adjacent < 0) {
270 if (flags != rgnflags)
272 rgn->region[i].size += size;
275 } else if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) {
276 /* regions overlap */
281 if ((i < rgn->cnt - 1) && lmb_regions_adjacent(rgn, i, i + 1)) {
282 if (rgn->region[i].flags == rgn->region[i + 1].flags) {
283 lmb_coalesce_regions(rgn, i, i + 1);
290 if (rgn->cnt >= rgn->max)
293 /* Couldn't coalesce the LMB, so add it to the sorted table. */
294 for (i = rgn->cnt-1; i >= 0; i--) {
295 if (base < rgn->region[i].base) {
296 rgn->region[i + 1].base = rgn->region[i].base;
297 rgn->region[i + 1].size = rgn->region[i].size;
298 rgn->region[i + 1].flags = rgn->region[i].flags;
300 rgn->region[i + 1].base = base;
301 rgn->region[i + 1].size = size;
302 rgn->region[i + 1].flags = flags;
307 if (base < rgn->region[0].base) {
308 rgn->region[0].base = base;
309 rgn->region[0].size = size;
310 rgn->region[0].flags = flags;
318 static long lmb_add_region(struct lmb_region *rgn, phys_addr_t base,
321 return lmb_add_region_flags(rgn, base, size, LMB_NONE);
324 /* This routine may be called with relocation disabled. */
325 long lmb_add(struct lmb *lmb, phys_addr_t base, phys_size_t size)
327 struct lmb_region *_rgn = &(lmb->memory);
329 return lmb_add_region(_rgn, base, size);
332 long lmb_free(struct lmb *lmb, phys_addr_t base, phys_size_t size)
334 struct lmb_region *rgn = &(lmb->reserved);
335 phys_addr_t rgnbegin, rgnend;
336 phys_addr_t end = base + size - 1;
339 rgnbegin = rgnend = 0; /* supress gcc warnings */
341 /* Find the region where (base, size) belongs to */
342 for (i = 0; i < rgn->cnt; i++) {
343 rgnbegin = rgn->region[i].base;
344 rgnend = rgnbegin + rgn->region[i].size - 1;
346 if ((rgnbegin <= base) && (end <= rgnend))
350 /* Didn't find the region */
354 /* Check to see if we are removing entire region */
355 if ((rgnbegin == base) && (rgnend == end)) {
356 lmb_remove_region(rgn, i);
360 /* Check to see if region is matching at the front */
361 if (rgnbegin == base) {
362 rgn->region[i].base = end + 1;
363 rgn->region[i].size -= size;
367 /* Check to see if the region is matching at the end */
369 rgn->region[i].size -= size;
374 * We need to split the entry - adjust the current one to the
375 * beginging of the hole and add the region after hole.
377 rgn->region[i].size = base - rgn->region[i].base;
378 return lmb_add_region_flags(rgn, end + 1, rgnend - end,
379 rgn->region[i].flags);
382 long lmb_reserve_flags(struct lmb *lmb, phys_addr_t base, phys_size_t size,
383 enum lmb_flags flags)
385 struct lmb_region *_rgn = &(lmb->reserved);
387 return lmb_add_region_flags(_rgn, base, size, flags);
390 long lmb_reserve(struct lmb *lmb, phys_addr_t base, phys_size_t size)
392 return lmb_reserve_flags(lmb, base, size, LMB_NONE);
395 static long lmb_overlaps_region(struct lmb_region *rgn, phys_addr_t base,
400 for (i = 0; i < rgn->cnt; i++) {
401 phys_addr_t rgnbase = rgn->region[i].base;
402 phys_size_t rgnsize = rgn->region[i].size;
403 if (lmb_addrs_overlap(base, size, rgnbase, rgnsize))
407 return (i < rgn->cnt) ? i : -1;
410 phys_addr_t lmb_alloc(struct lmb *lmb, phys_size_t size, ulong align)
412 return lmb_alloc_base(lmb, size, align, LMB_ALLOC_ANYWHERE);
415 phys_addr_t lmb_alloc_base(struct lmb *lmb, phys_size_t size, ulong align, phys_addr_t max_addr)
419 alloc = __lmb_alloc_base(lmb, size, align, max_addr);
422 printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
423 (ulong)size, (ulong)max_addr);
428 static phys_addr_t lmb_align_down(phys_addr_t addr, phys_size_t size)
430 return addr & ~(size - 1);
433 phys_addr_t __lmb_alloc_base(struct lmb *lmb, phys_size_t size, ulong align, phys_addr_t max_addr)
436 phys_addr_t base = 0;
437 phys_addr_t res_base;
439 for (i = lmb->memory.cnt - 1; i >= 0; i--) {
440 phys_addr_t lmbbase = lmb->memory.region[i].base;
441 phys_size_t lmbsize = lmb->memory.region[i].size;
445 if (max_addr == LMB_ALLOC_ANYWHERE)
446 base = lmb_align_down(lmbbase + lmbsize - size, align);
447 else if (lmbbase < max_addr) {
448 base = lmbbase + lmbsize;
451 base = min(base, max_addr);
452 base = lmb_align_down(base - size, align);
456 while (base && lmbbase <= base) {
457 rgn = lmb_overlaps_region(&lmb->reserved, base, size);
459 /* This area isn't reserved, take it */
460 if (lmb_add_region(&lmb->reserved, base,
465 res_base = lmb->reserved.region[rgn].base;
468 base = lmb_align_down(res_base - size, align);
475 * Try to allocate a specific address range: must be in defined memory but not
478 phys_addr_t lmb_alloc_addr(struct lmb *lmb, phys_addr_t base, phys_size_t size)
482 /* Check if the requested address is in one of the memory regions */
483 rgn = lmb_overlaps_region(&lmb->memory, base, size);
486 * Check if the requested end address is in the same memory
489 if (lmb_addrs_overlap(lmb->memory.region[rgn].base,
490 lmb->memory.region[rgn].size,
491 base + size - 1, 1)) {
492 /* ok, reserve the memory */
493 if (lmb_reserve(lmb, base, size) >= 0)
500 /* Return number of bytes from a given address that are free */
501 phys_size_t lmb_get_free_size(struct lmb *lmb, phys_addr_t addr)
506 /* check if the requested address is in the memory regions */
507 rgn = lmb_overlaps_region(&lmb->memory, addr, 1);
509 for (i = 0; i < lmb->reserved.cnt; i++) {
510 if (addr < lmb->reserved.region[i].base) {
511 /* first reserved range > requested address */
512 return lmb->reserved.region[i].base - addr;
514 if (lmb->reserved.region[i].base +
515 lmb->reserved.region[i].size > addr) {
516 /* requested addr is in this reserved range */
520 /* if we come here: no reserved ranges above requested addr */
521 return lmb->memory.region[lmb->memory.cnt - 1].base +
522 lmb->memory.region[lmb->memory.cnt - 1].size - addr;
527 int lmb_is_reserved_flags(struct lmb *lmb, phys_addr_t addr, int flags)
531 for (i = 0; i < lmb->reserved.cnt; i++) {
532 phys_addr_t upper = lmb->reserved.region[i].base +
533 lmb->reserved.region[i].size - 1;
534 if ((addr >= lmb->reserved.region[i].base) && (addr <= upper))
535 return (lmb->reserved.region[i].flags & flags) == flags;
540 int lmb_is_reserved(struct lmb *lmb, phys_addr_t addr)
542 return lmb_is_reserved_flags(lmb, addr, LMB_NONE);
545 __weak void board_lmb_reserve(struct lmb *lmb)
547 /* please define platform specific board_lmb_reserve() */
550 __weak void arch_lmb_reserve(struct lmb *lmb)
552 /* please define platform specific arch_lmb_reserve() */